// Code generated for windows/386 by 'generator -mlong-double-64 --package-name libsqlite3 --prefix-enumerator=_ --prefix-external=x_ --prefix-field=F --prefix-static-internal=_ --prefix-static-none=_ --prefix-tagged-enum=_ --prefix-tagged-struct=T --prefix-tagged-union=T --prefix-typename=T --prefix-undefined=_ -ignore-unsupported-alignment -ignore-link-errors -import=sync -DHAVE_USLEEP -DLONGDOUBLE_TYPE=double -DNDEBUG -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DISABLE_INTRINSIC -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_GEOPOLY -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MATH_FUNCTIONS -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_OFFSET_SQL_FUNC -DSQLITE_ENABLE_PREUPDATE_HOOK -DSQLITE_ENABLE_RBU -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_SNAPSHOT -DSQLITE_ENABLE_STAT4 -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_HAVE_ZLIB=1 -DSQLITE_LIKE_DOESNT_MATCH_BLOBS -DSQLITE_SOUNDEX -DSQLITE_THREADSAFE=1 -DSQLITE_WITHOUT_ZONEMALLOC -D_LARGEFILE64_SOURCE -I /home/jnml/src/modernc.org/builder/.exclude/modernc.org/libc/include/windows/386 -I /home/jnml/src/modernc.org/builder/.exclude/modernc.org/libz/include/windows/386 -I /home/jnml/src/modernc.org/builder/.exclude/modernc.org/libtcl8.6/include/windows/386 -extended-errors -o sqlite3.go sqlite3.c -DSQLITE_MUTEX_NOOP --cpp /usr/bin/i686-w64-mingw32-gcc --goarch 386 --goos windows -DSQLITE_HAVE_C99_MATH_FUNCS=(1) -DSQLITE_OS_WIN=1 -DSQLITE_OMIT_SEH -map gcc=i686-w64-mingw32-gcc -eval-all-macros', DO NOT EDIT.

//go:build windows && 386

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

const ADDRESS_TAG_BIT = 2147483648

const APIENTRY = "WINAPI"

const APIPRIVATE = "__stdcall"

const CALLBACK = "__stdcall"

const CONSOLE_APPLICATION_16BIT = 1

const CONTEXT_ALL = 65599

const CONTEXT_CONTROL = 65537

const CONTEXT_DEBUG_REGISTERS = 65552

const CONTEXT_EXTENDED_REGISTERS = 65568

const CONTEXT_FLOATING_POINT = 65544

const CONTEXT_FULL = 65543

const CONTEXT_INTEGER = 65538

const CONTEXT_SEGMENTS = 65540

const CONTEXT_i386 = 65536

const CONTEXT_i486 = 65536

const DWL_DLGPROC = 4

const DWL_MSGRESULT = 0

const DWL_USER = 8

const EXPENTRY = "CALLBACK"

const FASTCALL = "__fastcall"

type FLOATING_SAVE_AREA = TFLOATING_SAVE_AREA

const GCL_HBRBACKGROUND = -10

const GCL_HCURSOR = -12

const GCL_HICON = -14

const GCL_HICONSM = -34

const GCL_HMODULE = -16

const GCL_MENUNAME = -8

const GCL_WNDPROC = -24

const GUI_16BITTASK = 32

const GWL_HINSTANCE = -6

const GWL_HWNDPARENT = -8

const GWL_USERDATA = -21

const GWL_WNDPROC = -4

const GetClassLongPtrA = 0

const GetClassLongPtrW = 0

const GetWindowLongPtrA = 0

const GetWindowLongPtrW = 0

const IFACEMETHODIMP = "STDMETHODIMP"

const IMAGE_NT_OPTIONAL_HDR_MAGIC = 267

const IMAGE_ORDINAL_FLAG = 2147483648

const IMAGE_SIZEOF_NT_OPTIONAL_HEADER = 224

const MAXIMUM_PROCESSORS = 32

const MAXIMUM_PROC_PER_GROUP = 32

const MAXIMUM_SUPPORTED_EXTENSION = 512

const MEMORY_ALLOCATION_ALIGNMENT = 8

const NTAPI = "__stdcall"

const NTAPI_INLINE = "NTAPI"

const PASCAL = "__stdcall"

type PFLOATING_SAVE_AREA = TPFLOATING_SAVE_AREA

const PcTeb = 24

const RPC_ENTRY = "__RPC_API"

const SCS_THIS_PLATFORM_BINARY = 0

const SIZE_OF_80387_REGISTERS = 80

const SSIZE_MAX = 2147483647

const STDAPICALLTYPE = "WINAPI"

const STDMETHODCALLTYPE = "WINAPI"

const SetClassLongPtrA = 0

const SetClassLongPtrW = 0

const SetWindowLongPtrA = 0

const SetWindowLongPtrW = 0

type TACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION = struct {
	F__ccgo_align                      [0]uint32
	FulFlags                           TDWORD
	FulEncodedAssemblyIdentityLength   TDWORD
	FulManifestPathType                TDWORD
	FulManifestPathLength              TDWORD
	FliManifestLastWriteTime           TLARGE_INTEGER
	FulPolicyPathType                  TDWORD
	FulPolicyPathLength                TDWORD
	FliPolicyLastWriteTime             TLARGE_INTEGER
	FulMetadataSatelliteRosterIndex    TDWORD
	FulManifestVersionMajor            TDWORD
	FulManifestVersionMinor            TDWORD
	FulPolicyVersionMajor              TDWORD
	FulPolicyVersionMinor              TDWORD
	FulAssemblyDirectoryNameLength     TDWORD
	FlpAssemblyEncodedAssemblyIdentity TPCWSTR
	FlpAssemblyManifestPath            TPCWSTR
	FlpAssemblyPolicyPath              TPCWSTR
	FlpAssemblyDirectoryName           TPCWSTR
	FulFileCount                       TDWORD
	F__ccgo_pad19                      [4]byte
}

type TAPPLICATIONLAUNCH_SETTING_VALUE = struct {
	F__ccgo_align     [0]uint32
	FActivationTime   TLARGE_INTEGER
	FFlags            TDWORD
	FButtonInstanceID TDWORD
}

type TAPP_MEMORY_INFORMATION = struct {
	F__ccgo_align           [0]uint32
	FAvailableCommit        TULONG64
	FPrivateCommitUsage     TULONG64
	FPeakPrivateCommitUsage TULONG64
	FTotalCommitUsage       TULONG64
}

type TASYNC_STGMEDIUM = struct {
	Ftymed     TDWORD
	F__ccgo1_4 struct {
		FhMetaFilePict [0]THMETAFILEPICT
		FhEnhMetaFile  [0]THENHMETAFILE
		FhGlobal       [0]THGLOBAL
		FlpszFileName  [0]TLPOLESTR
		Fpstm          [0]uintptr
		Fpstg          [0]uintptr
		FhBitmap       THBITMAP
	}
	FpUnkForRelease uintptr
}

type TBCRYPT_AUTHENTICATED_CIPHER_MODE_INFO = struct {
	F__ccgo_align   [0]uint32
	FcbSize         TULONG
	FdwInfoVersion  TULONG
	FpbNonce        TPUCHAR
	FcbNonce        TULONG
	FpbAuthData     TPUCHAR
	FcbAuthData     TULONG
	FpbTag          TPUCHAR
	FcbTag          TULONG
	FpbMacContext   TPUCHAR
	FcbMacContext   TULONG
	FcbAAD          TULONG
	F__ccgo_align11 [4]byte
	FcbData         TULONGLONG
	FdwFlags        TULONG
	F__ccgo_pad13   [4]byte
}

type TBIDI_DATA = struct {
	FdwBidiType TDWORD
	Fu          struct {
		FiData       [0]TLONG
		FsData       [0]TLPWSTR
		FfData       [0]TFLOAT
		FbiData      [0]TBINARY_CONTAINER
		FbData       TWINBOOL
		F__ccgo_pad5 [4]byte
	}
}

type TBIN_COUNT = struct {
	F__ccgo_align [0]uint32
	FBinRange     TBIN_RANGE
	FBinCount     TDWORD
	F__ccgo_pad2  [4]byte
}

type TBIN_RANGE = struct {
	F__ccgo_align [0]uint32
	FStartValue   TLARGE_INTEGER
	FLength       TLARGE_INTEGER
}

type TBIN_RESULTS = struct {
	F__ccgo_align  [0]uint32
	FNumberOfBins  TDWORD
	F__ccgo_align1 [4]byte
	FBinCounts     [1]TBIN_COUNT
}

type TBOOT_AREA_INFO = struct {
	F__ccgo_align    [0]uint32
	FBootSectorCount TULONG
	F__ccgo_align1   [4]byte
	FBootSectors     [2]struct {
		F__ccgo_align [0]uint32
		FOffset       TLARGE_INTEGER
	}
}

type TCCRYPT_OID_INFO = struct {
	FcbSize     TDWORD
	FpszOID     TLPCSTR
	FpwszName   TLPCWSTR
	FdwGroupId  TDWORD
	F__ccgo4_16 struct {
		FAlgid    [0]TALG_ID
		FdwLength [0]TDWORD
		FdwValue  TDWORD
	}
	FExtraInfo TCRYPT_DATA_BLOB
}

type TCERT_ALT_NAME_ENTRY = struct {
	FdwAltNameChoice TDWORD
	F__ccgo1_4       struct {
		FpwszRfc822Name  [0]TLPWSTR
		FpwszDNSName     [0]TLPWSTR
		FDirectoryName   [0]TCERT_NAME_BLOB
		FpwszURL         [0]TLPWSTR
		FIPAddress       [0]TCRYPT_DATA_BLOB
		FpszRegisteredID [0]TLPSTR
		FpOtherName      TPCERT_OTHER_NAME
		F__ccgo_pad7     [4]byte
	}
}

type TCERT_BIOMETRIC_DATA = struct {
	FdwTypeOfBiometricDataChoice TDWORD
	F__ccgo1_4                   struct {
		FpszObjId     [0]TLPSTR
		FdwPredefined TDWORD
	}
	FHashedUrl TCERT_HASHED_URL
}

type TCERT_ID = struct {
	FdwIdChoice TDWORD
	F__ccgo1_4  struct {
		FKeyId              [0]TCRYPT_HASH_BLOB
		FHashId             [0]TCRYPT_HASH_BLOB
		FIssuerSerialNumber TCERT_ISSUER_SERIAL_NUMBER
	}
}

type TCERT_KEY_CONTEXT = struct {
	FcbSize    TDWORD
	F__ccgo1_4 struct {
		FhNCryptKey [0]TNCRYPT_KEY_HANDLE
		FhCryptProv THCRYPTPROV
	}
	FdwKeySpec TDWORD
}

type TCERT_LOGOTYPE_INFO = struct {
	FdwLogotypeInfoChoice TDWORD
	F__ccgo1_4            struct {
		FpLogotypeIndirectInfo [0]TPCERT_LOGOTYPE_REFERENCE
		FpLogotypeDirectInfo   TPCERT_LOGOTYPE_DATA
	}
}

type TCERT_SYSTEM_STORE_RELOCATE_PARA = struct {
	F__ccgo0_0 struct {
		FpvBase   [0]uintptr
		FhKeyBase THKEY
	}
	F__ccgo1_4 struct {
		FpszSystemStore  [0]TLPCSTR
		FpwszSystemStore [0]TLPCWSTR
		FpvSystemStore   uintptr
	}
}

type TCLAIM_SECURITY_ATTRIBUTE_FQBN_VALUE = struct {
	F__ccgo_align [0]uint32
	FVersion      TDWORD64
	FName         TPWSTR
	F__ccgo_pad2  [4]byte
}

type TCMC_STATUS_INFO = struct {
	FdwStatus          TDWORD
	FcBodyList         TDWORD
	FrgdwBodyList      uintptr
	FpwszStatusString  TLPWSTR
	FdwOtherInfoChoice TDWORD
	F__ccgo5_20        struct {
		FpPendInfo  [0]TPCMC_PEND_INFO
		FdwFailInfo TDWORD
	}
}

type TCMC_TAGGED_REQUEST = struct {
	FdwTaggedRequestChoice TDWORD
	F__ccgo1_4             struct {
		FpTaggedCertRequest TPCMC_TAGGED_CERT_REQUEST
	}
}

type TCMSG_CMS_RECIPIENT_INFO = struct {
	FdwRecipientChoice TDWORD
	F__ccgo1_4         struct {
		FpKeyAgree [0]TPCMSG_KEY_AGREE_RECIPIENT_INFO
		FpMailList [0]TPCMSG_MAIL_LIST_RECIPIENT_INFO
		FpKeyTrans TPCMSG_KEY_TRANS_RECIPIENT_INFO
	}
}

type TCMSG_CONTENT_ENCRYPT_INFO = struct {
	FcbSize                     TDWORD
	FhCryptProv                 THCRYPTPROV_LEGACY
	FContentEncryptionAlgorithm TCRYPT_ALGORITHM_IDENTIFIER
	FpvEncryptionAuxInfo        uintptr
	FcRecipients                TDWORD
	FrgCmsRecipients            TPCMSG_RECIPIENT_ENCODE_INFO
	FpfnAlloc                   TPFN_CMSG_ALLOC
	FpfnFree                    TPFN_CMSG_FREE
	FdwEncryptFlags             TDWORD
	F__ccgo9_44                 struct {
		FhCNGContentEncryptKey [0]TBCRYPT_KEY_HANDLE
		FhContentEncryptKey    THCRYPTKEY
	}
	FdwFlags                      TDWORD
	FfCNG                         TWINBOOL
	FpbCNGContentEncryptKeyObject uintptr
	FpbContentEncryptKey          uintptr
	FcbContentEncryptKey          TDWORD
}

type TCMSG_CTRL_DECRYPT_PARA = struct {
	FcbSize    TDWORD
	F__ccgo1_4 struct {
		FhNCryptKey [0]TNCRYPT_KEY_HANDLE
		FhCryptProv THCRYPTPROV
	}
	FdwKeySpec        TDWORD
	FdwRecipientIndex TDWORD
}

type TCMSG_CTRL_KEY_AGREE_DECRYPT_PARA = struct {
	FcbSize    TDWORD
	F__ccgo1_4 struct {
		FhNCryptKey [0]TNCRYPT_KEY_HANDLE
		FhCryptProv THCRYPTPROV
	}
	FdwKeySpec                    TDWORD
	FpKeyAgree                    TPCMSG_KEY_AGREE_RECIPIENT_INFO
	FdwRecipientIndex             TDWORD
	FdwRecipientEncryptedKeyIndex TDWORD
	FOriginatorPublicKey          TCRYPT_BIT_BLOB
}

type TCMSG_CTRL_KEY_TRANS_DECRYPT_PARA = struct {
	FcbSize    TDWORD
	F__ccgo1_4 struct {
		FhNCryptKey [0]TNCRYPT_KEY_HANDLE
		FhCryptProv THCRYPTPROV
	}
	FdwKeySpec        TDWORD
	FpKeyTrans        TPCMSG_KEY_TRANS_RECIPIENT_INFO
	FdwRecipientIndex TDWORD
}

type TCMSG_CTRL_MAIL_LIST_DECRYPT_PARA = struct {
	FcbSize           TDWORD
	FhCryptProv       THCRYPTPROV
	FpMailList        TPCMSG_MAIL_LIST_RECIPIENT_INFO
	FdwRecipientIndex TDWORD
	FdwKeyChoice      TDWORD
	F__ccgo5_20       struct {
		FpvKeyEncryptionKey [0]uintptr
		FhKeyEncryptionKey  THCRYPTKEY
	}
}

type TCMSG_KEY_AGREE_ENCRYPT_INFO = struct {
	FcbSize                 TDWORD
	FdwRecipientIndex       TDWORD
	FKeyEncryptionAlgorithm TCRYPT_ALGORITHM_IDENTIFIER
	FUserKeyingMaterial     TCRYPT_DATA_BLOB
	FdwOriginatorChoice     TDWORD
	F__ccgo5_32             struct {
		FOriginatorPublicKeyInfo [0]TCERT_PUBLIC_KEY_INFO
		FOriginatorCertId        TCERT_ID
		F__ccgo_pad2             [4]byte
	}
	FcKeyAgreeKeyEncryptInfo   TDWORD
	FrgpKeyAgreeKeyEncryptInfo uintptr
	FdwFlags                   TDWORD
}

type TCMSG_KEY_AGREE_RECIPIENT_ENCODE_INFO = struct {
	FcbSize                 TDWORD
	FKeyEncryptionAlgorithm TCRYPT_ALGORITHM_IDENTIFIER
	FpvKeyEncryptionAuxInfo uintptr
	FKeyWrapAlgorithm       TCRYPT_ALGORITHM_IDENTIFIER
	FpvKeyWrapAuxInfo       uintptr
	FhCryptProv             THCRYPTPROV_LEGACY
	FdwKeySpec              TDWORD
	FdwKeyChoice            TDWORD
	F__ccgo8_48             struct {
		FpSenderId           [0]TPCERT_ID
		FpEphemeralAlgorithm TPCRYPT_ALGORITHM_IDENTIFIER
	}
	FUserKeyingMaterial        TCRYPT_DATA_BLOB
	FcRecipientEncryptedKeys   TDWORD
	FrgpRecipientEncryptedKeys uintptr
}

type TCMSG_KEY_AGREE_RECIPIENT_INFO = struct {
	FdwVersion          TDWORD
	FdwOriginatorChoice TDWORD
	F__ccgo2_8          struct {
		FOriginatorPublicKeyInfo [0]TCERT_PUBLIC_KEY_INFO
		FOriginatorCertId        TCERT_ID
		F__ccgo_pad2             [4]byte
	}
	FUserKeyingMaterial        TCRYPT_DATA_BLOB
	FKeyEncryptionAlgorithm    TCRYPT_ALGORITHM_IDENTIFIER
	FcRecipientEncryptedKeys   TDWORD
	FrgpRecipientEncryptedKeys uintptr
}

type TCMSG_MAIL_LIST_RECIPIENT_ENCODE_INFO = struct {
	FcbSize                 TDWORD
	FKeyEncryptionAlgorithm TCRYPT_ALGORITHM_IDENTIFIER
	FpvKeyEncryptionAuxInfo uintptr
	FhCryptProv             THCRYPTPROV
	FdwKeyChoice            TDWORD
	F__ccgo5_28             struct {
		FpvKeyEncryptionKey [0]uintptr
		FhKeyEncryptionKey  THCRYPTKEY
	}
	FKeyId      TCRYPT_DATA_BLOB
	FDate       TFILETIME
	FpOtherAttr TPCRYPT_ATTRIBUTE_TYPE_VALUE
}

type TCMSG_RECIPIENT_ENCODE_INFO = struct {
	FdwRecipientChoice TDWORD
	F__ccgo1_4         struct {
		FpKeyAgree [0]TPCMSG_KEY_AGREE_RECIPIENT_ENCODE_INFO
		FpMailList [0]TPCMSG_MAIL_LIST_RECIPIENT_ENCODE_INFO
		FpKeyTrans TPCMSG_KEY_TRANS_RECIPIENT_ENCODE_INFO
	}
}

type TCMSG_SIGNER_ENCODE_INFO = struct {
	FcbSize    TDWORD
	FpCertInfo TPCERT_INFO
	F__ccgo2_8 struct {
		FhNCryptKey [0]TNCRYPT_KEY_HANDLE
		FhCryptProv THCRYPTPROV
	}
	FdwKeySpec     TDWORD
	FHashAlgorithm TCRYPT_ALGORITHM_IDENTIFIER
	FpvHashAuxInfo uintptr
	FcAuthAttr     TDWORD
	FrgAuthAttr    TPCRYPT_ATTRIBUTE
	FcUnauthAttr   TDWORD
	FrgUnauthAttr  TPCRYPT_ATTRIBUTE
}

type TCONTEXT = struct {
	FContextFlags      TDWORD
	FDr0               TDWORD
	FDr1               TDWORD
	FDr2               TDWORD
	FDr3               TDWORD
	FDr6               TDWORD
	FDr7               TDWORD
	FFloatSave         TFLOATING_SAVE_AREA
	FSegGs             TDWORD
	FSegFs             TDWORD
	FSegEs             TDWORD
	FSegDs             TDWORD
	FEdi               TDWORD
	FEsi               TDWORD
	FEbx               TDWORD
	FEdx               TDWORD
	FEcx               TDWORD
	FEax               TDWORD
	FEbp               TDWORD
	FEip               TDWORD
	FSegCs             TDWORD
	FEFlags            TDWORD
	FEsp               TDWORD
	FSegSs             TDWORD
	FExtendedRegisters [512]TBYTE
}

type TCOPYFILE2_MESSAGE = struct {
	F__ccgo_align [0]uint32
	FType         TCOPYFILE2_MESSAGE_TYPE
	FdwPadding    TDWORD
	FInfo         struct {
		F__ccgo_align  [0]uint32
		FChunkFinished [0]struct {
			F__ccgo_align              [0]uint32
			FdwStreamNumber            TDWORD
			FdwFlags                   TDWORD
			FhSourceFile               THANDLE
			FhDestinationFile          THANDLE
			FuliChunkNumber            TULARGE_INTEGER
			FuliChunkSize              TULARGE_INTEGER
			FuliStreamSize             TULARGE_INTEGER
			FuliStreamBytesTransferred TULARGE_INTEGER
			FuliTotalFileSize          TULARGE_INTEGER
			FuliTotalBytesTransferred  TULARGE_INTEGER
		}
		FStreamStarted [0]struct {
			F__ccgo_align     [0]uint32
			FdwStreamNumber   TDWORD
			FdwReserved       TDWORD
			FhSourceFile      THANDLE
			FhDestinationFile THANDLE
			FuliStreamSize    TULARGE_INTEGER
			FuliTotalFileSize TULARGE_INTEGER
		}
		FStreamFinished [0]struct {
			F__ccgo_align              [0]uint32
			FdwStreamNumber            TDWORD
			FdwReserved                TDWORD
			FhSourceFile               THANDLE
			FhDestinationFile          THANDLE
			FuliStreamSize             TULARGE_INTEGER
			FuliStreamBytesTransferred TULARGE_INTEGER
			FuliTotalFileSize          TULARGE_INTEGER
			FuliTotalBytesTransferred  TULARGE_INTEGER
		}
		FPollContinue [0]struct {
			FdwReserved TDWORD
		}
		FError [0]struct {
			F__ccgo_align              [0]uint32
			FCopyPhase                 TCOPYFILE2_COPY_PHASE
			FdwStreamNumber            TDWORD
			FhrFailure                 THRESULT
			FdwReserved                TDWORD
			FuliChunkNumber            TULARGE_INTEGER
			FuliStreamSize             TULARGE_INTEGER
			FuliStreamBytesTransferred TULARGE_INTEGER
			FuliTotalFileSize          TULARGE_INTEGER
			FuliTotalBytesTransferred  TULARGE_INTEGER
		}
		FChunkStarted struct {
			F__ccgo_align     [0]uint32
			FdwStreamNumber   TDWORD
			FdwReserved       TDWORD
			FhSourceFile      THANDLE
			FhDestinationFile THANDLE
			FuliChunkNumber   TULARGE_INTEGER
			FuliChunkSize     TULARGE_INTEGER
			FuliStreamSize    TULARGE_INTEGER
			FuliTotalFileSize TULARGE_INTEGER
		}
		F__ccgo_pad6 [16]byte
	}
}

type TCORE_PRINTER_DRIVER = struct {
	F__ccgo_align     [0]uint32
	FCoreDriverGUID   TGUID
	FftDriverDate     TFILETIME
	FdwlDriverVersion TDWORDLONG
	FszPackageID      [260]TCHAR
	F__ccgo_pad4      [4]byte
}

type TCORE_PRINTER_DRIVERA = struct {
	F__ccgo_align     [0]uint32
	FCoreDriverGUID   TGUID
	FftDriverDate     TFILETIME
	FdwlDriverVersion TDWORDLONG
	FszPackageID      [260]TCHAR
	F__ccgo_pad4      [4]byte
}

type TCORE_PRINTER_DRIVERW = struct {
	F__ccgo_align     [0]uint32
	FCoreDriverGUID   TGUID
	FftDriverDate     TFILETIME
	FdwlDriverVersion TDWORDLONG
	FszPackageID      [260]TWCHAR
}

type TCREATE_USN_JOURNAL_DATA = struct {
	F__ccgo_align    [0]uint32
	FMaximumSize     TDWORDLONG
	FAllocationDelta TDWORDLONG
}

type TCREATE_VIRTUAL_DISK_PARAMETERS = struct {
	F__ccgo_align  [0]uint32
	FVersion       TCREATE_VIRTUAL_DISK_VERSION
	F__ccgo_align1 [4]byte
	F__ccgo1_8     struct {
		F__ccgo_align [0]uint32
		FVersion2     [0]struct {
			F__ccgo_align             [0]uint32
			FUniqueId                 TGUID
			FMaximumSize              TULONGLONG
			FBlockSizeInBytes         TULONG
			FSectorSizeInBytes        TULONG
			FParentPath               TPCWSTR
			FSourcePath               TPCWSTR
			FOpenFlags                TOPEN_VIRTUAL_DISK_FLAG
			FParentVirtualStorageType TVIRTUAL_STORAGE_TYPE
			FSourceVirtualStorageType TVIRTUAL_STORAGE_TYPE
			FResiliencyGuid           TGUID
			F__ccgo_pad10             [4]byte
		}
		FVersion1 struct {
			F__ccgo_align      [0]uint32
			FUniqueId          TGUID
			FMaximumSize       TULONGLONG
			FBlockSizeInBytes  TULONG
			FSectorSizeInBytes TULONG
			FParentPath        TPCWSTR
			FSourcePath        TPCWSTR
		}
		F__ccgo_pad2 [64]byte
	}
}

type TCRL_DIST_POINT_NAME = struct {
	FdwDistPointNameChoice TDWORD
	F__ccgo1_4             struct {
		FFullName TCERT_ALT_NAME_INFO
	}
}

type TCRYPT_OID_INFO = struct {
	FcbSize     TDWORD
	FpszOID     TLPCSTR
	FpwszName   TLPCWSTR
	FdwGroupId  TDWORD
	F__ccgo4_16 struct {
		FAlgid    [0]TALG_ID
		FdwLength [0]TDWORD
		FdwValue  TDWORD
	}
	FExtraInfo TCRYPT_DATA_BLOB
}

type TCSV_NAMESPACE_INFO = struct {
	F__ccgo_align   [0]uint32
	FVersion        TULONG
	FDeviceNumber   TULONG
	FStartingOffset TLARGE_INTEGER
	FSectorSize     TULONG
	F__ccgo_pad4    [4]byte
}

type TCURRENCY = struct {
	F__ccgo_align [0]uint32
	Fint641       [0]TLONGLONG
	F__ccgo0_0    struct {
		FLo uint32
		FHi int32
	}
}

type TCUSTDATAITEM = struct {
	F__ccgo_align [0]uint32
	Fguid         TGUID
	FvarValue     TVARIANTARG
}

type TCY = struct {
	F__ccgo_align [0]uint32
	Fint641       [0]TLONGLONG
	F__ccgo0_0    struct {
		FLo uint32
		FHi int32
	}
}

type TDECIMAL = struct {
	F__ccgo_align [0]uint32
	FwReserved    TUSHORT
	F__ccgo1_2    struct {
		Fsignscale [0]TUSHORT
		F__ccgo0_0 struct {
			Fscale TBYTE
			Fsign  TBYTE
		}
	}
	FHi32      TULONG
	F__ccgo3_8 struct {
		F__ccgo_align [0]uint32
		FLo64         [0]TULONGLONG
		F__ccgo0_0    struct {
			FLo32  TULONG
			FMid32 TULONG
		}
	}
}

type TDELETE_USN_JOURNAL_DATA = struct {
	F__ccgo_align [0]uint32
	FUsnJournalID TDWORDLONG
	FDeleteFlags  TDWORD
	F__ccgo_pad2  [4]byte
}

type TDEVICE_COPY_OFFLOAD_DESCRIPTOR = struct {
	F__ccgo_align                       [0]uint32
	FVersion                            TDWORD
	FSize                               TDWORD
	FMaximumTokenLifetime               TDWORD
	FDefaultTokenLifetime               TDWORD
	FMaximumTransferSize                TDWORDLONG
	FOptimalTransferCount               TDWORDLONG
	FMaximumDataDescriptors             TDWORD
	FMaximumTransferLengthPerDescriptor TDWORD
	FOptimalTransferLengthPerDescriptor TDWORD
	FOptimalTransferLengthGranularity   TWORD
	FReserved                           [2]TBYTE
}

type TDEVICE_DATA_SET_RANGE = struct {
	F__ccgo_align   [0]uint32
	FStartingOffset TLONGLONG
	FLengthInBytes  TDWORDLONG
}

type TDEVICE_LB_PROVISIONING_DESCRIPTOR = struct {
	F__ccgo_align                 [0]uint32
	FVersion                      TDWORD
	FSize                         TDWORD
	F__ccgo8                      uint8
	FReserved1                    [7]TBYTE
	FOptimalUnmapGranularity      TDWORDLONG
	FUnmapGranularityAlignment    TDWORDLONG
	FMaxUnmapLbaCount             TDWORD
	FMaxUnmapBlockDescriptorCount TDWORD
}

type TDEVICE_MEDIA_INFO = struct {
	F__ccgo_align   [0]uint32
	FDeviceSpecific struct {
		F__ccgo_align      [0]uint32
		FRemovableDiskInfo [0]struct {
			F__ccgo_align         [0]uint32
			FCylinders            TLARGE_INTEGER
			FMediaType            TSTORAGE_MEDIA_TYPE
			FTracksPerCylinder    TDWORD
			FSectorsPerTrack      TDWORD
			FBytesPerSector       TDWORD
			FNumberMediaSides     TDWORD
			FMediaCharacteristics TDWORD
		}
		FTapeInfo [0]struct {
			FMediaType            TSTORAGE_MEDIA_TYPE
			FMediaCharacteristics TDWORD
			FCurrentBlockSize     TDWORD
			FBusType              TSTORAGE_BUS_TYPE
			FBusSpecificData      struct {
				FScsiInformation struct {
					FMediumType  TBYTE
					FDensityCode TBYTE
				}
			}
		}
		FDiskInfo struct {
			F__ccgo_align         [0]uint32
			FCylinders            TLARGE_INTEGER
			FMediaType            TSTORAGE_MEDIA_TYPE
			FTracksPerCylinder    TDWORD
			FSectorsPerTrack      TDWORD
			FBytesPerSector       TDWORD
			FNumberMediaSides     TDWORD
			FMediaCharacteristics TDWORD
		}
	}
}

type TDISK_DETECTION_INFO = struct {
	F__ccgo_align     [0]uint32
	FSizeOfDetectInfo TDWORD
	FDetectionType    TDETECTION_TYPE
	F__ccgo2_8        struct {
		F__ccgo_align [0]uint32
		F__ccgo0_0    struct {
			F__ccgo_align [0]uint32
			FInt13        TDISK_INT13_INFO
			FExInt13      TDISK_EX_INT13_INFO
		}
	}
}

type TDISK_EXTENT = struct {
	F__ccgo_align   [0]uint32
	FDiskNumber     TDWORD
	F__ccgo_align1  [4]byte
	FStartingOffset TLARGE_INTEGER
	FExtentLength   TLARGE_INTEGER
}

type TDISK_EX_INT13_INFO = struct {
	F__ccgo_align      [0]uint32
	FExBufferSize      TWORD
	FExFlags           TWORD
	FExCylinders       TDWORD
	FExHeads           TDWORD
	FExSectorsPerTrack TDWORD
	FExSectorsPerDrive TDWORD64
	FExSectorSize      TWORD
	FExReserved        TWORD
	F__ccgo_pad8       [4]byte
}

type TDISK_GEOMETRY = struct {
	F__ccgo_align      [0]uint32
	FCylinders         TLARGE_INTEGER
	FMediaType         TMEDIA_TYPE
	FTracksPerCylinder TDWORD
	FSectorsPerTrack   TDWORD
	FBytesPerSector    TDWORD
}

type TDISK_GEOMETRY_EX = struct {
	F__ccgo_align [0]uint32
	FGeometry     TDISK_GEOMETRY
	FDiskSize     TLARGE_INTEGER
	FData         [1]TBYTE
	F__ccgo_pad3  [7]byte
}

type TDISK_GROW_PARTITION = struct {
	F__ccgo_align    [0]uint32
	FPartitionNumber TDWORD
	F__ccgo_align1   [4]byte
	FBytesToGrow     TLARGE_INTEGER
}

type TDISK_HISTOGRAM = struct {
	F__ccgo_align [0]uint32
	FDiskSize     TLARGE_INTEGER
	FStart        TLARGE_INTEGER
	FEnd          TLARGE_INTEGER
	FAverage      TLARGE_INTEGER
	FAverageRead  TLARGE_INTEGER
	FAverageWrite TLARGE_INTEGER
	FGranularity  TDWORD
	FSize         TDWORD
	FReadCount    TDWORD
	FWriteCount   TDWORD
	FHistogram    TPHISTOGRAM_BUCKET
	F__ccgo_pad11 [4]byte
}

type TDISK_PERFORMANCE = struct {
	F__ccgo_align        [0]uint32
	FBytesRead           TLARGE_INTEGER
	FBytesWritten        TLARGE_INTEGER
	FReadTime            TLARGE_INTEGER
	FWriteTime           TLARGE_INTEGER
	FIdleTime            TLARGE_INTEGER
	FReadCount           TDWORD
	FWriteCount          TDWORD
	FQueueDepth          TDWORD
	FSplitCount          TDWORD
	FQueryTime           TLARGE_INTEGER
	FStorageDeviceNumber TDWORD
	FStorageManagerName  [8]TWCHAR
	F__ccgo_pad12        [4]byte
}

type TDISK_RECORD = struct {
	F__ccgo_align   [0]uint32
	FByteOffset     TLARGE_INTEGER
	FStartTime      TLARGE_INTEGER
	FEndTime        TLARGE_INTEGER
	FVirtualAddress TPVOID
	FNumberOfBytes  TDWORD
	FDeviceNumber   TBYTE
	FReadRequest    TBOOLEAN
	F__ccgo_pad7    [6]byte
}

type TDISPLAYCONFIG_MODE_INFO = struct {
	F__ccgo_align [0]uint32
	FinfoType     TDISPLAYCONFIG_MODE_INFO_TYPE
	Fid           TUINT32
	FadapterId    TLUID
	F__ccgo3_16   struct {
		F__ccgo_align [0]uint32
		FsourceMode   [0]TDISPLAYCONFIG_SOURCE_MODE
		FtargetMode   TDISPLAYCONFIG_TARGET_MODE
	}
}

type TDISPLAYCONFIG_TARGET_MODE = struct {
	F__ccgo_align          [0]uint32
	FtargetVideoSignalInfo TDISPLAYCONFIG_VIDEO_SIGNAL_INFO
}

type TDISPLAYCONFIG_TARGET_PREFERRED_MODE = struct {
	F__ccgo_align  [0]uint32
	Fheader        TDISPLAYCONFIG_DEVICE_INFO_HEADER
	Fwidth         TUINT32
	Fheight        TUINT32
	F__ccgo_align3 [4]byte
	FtargetMode    TDISPLAYCONFIG_TARGET_MODE
}

type TDISPLAYCONFIG_VIDEO_SIGNAL_INFO = struct {
	F__ccgo_align [0]uint32
	FpixelRate    TUINT64
	FhSyncFreq    TDISPLAYCONFIG_RATIONAL
	FvSyncFreq    TDISPLAYCONFIG_RATIONAL
	FactiveSize   TDISPLAYCONFIG_2DREGION
	FtotalSize    TDISPLAYCONFIG_2DREGION
	F__ccgo5_40   struct {
		FvideoStandard        [0]TUINT32
		FAdditionalSignalInfo struct {
			F__ccgo0 uint32
		}
	}
	FscanLineOrdering TDISPLAYCONFIG_SCANLINE_ORDERING
}

type TDRIVER_INFO_6 = struct {
	F__ccgo_align      [0]uint32
	FcVersion          TDWORD
	FpName             TLPSTR
	FpEnvironment      TLPSTR
	FpDriverPath       TLPSTR
	FpDataFile         TLPSTR
	FpConfigFile       TLPSTR
	FpHelpFile         TLPSTR
	FpDependentFiles   TLPSTR
	FpMonitorName      TLPSTR
	FpDefaultDataType  TLPSTR
	FpszzPreviousNames TLPSTR
	FftDriverDate      TFILETIME
	F__ccgo_align12    [4]byte
	FdwlDriverVersion  TDWORDLONG
	FpszMfgName        TLPSTR
	FpszOEMUrl         TLPSTR
	FpszHardwareID     TLPSTR
	FpszProvider       TLPSTR
}

type TDRIVER_INFO_6A = struct {
	F__ccgo_align      [0]uint32
	FcVersion          TDWORD
	FpName             TLPSTR
	FpEnvironment      TLPSTR
	FpDriverPath       TLPSTR
	FpDataFile         TLPSTR
	FpConfigFile       TLPSTR
	FpHelpFile         TLPSTR
	FpDependentFiles   TLPSTR
	FpMonitorName      TLPSTR
	FpDefaultDataType  TLPSTR
	FpszzPreviousNames TLPSTR
	FftDriverDate      TFILETIME
	F__ccgo_align12    [4]byte
	FdwlDriverVersion  TDWORDLONG
	FpszMfgName        TLPSTR
	FpszOEMUrl         TLPSTR
	FpszHardwareID     TLPSTR
	FpszProvider       TLPSTR
}

type TDRIVER_INFO_6W = struct {
	F__ccgo_align      [0]uint32
	FcVersion          TDWORD
	FpName             TLPWSTR
	FpEnvironment      TLPWSTR
	FpDriverPath       TLPWSTR
	FpDataFile         TLPWSTR
	FpConfigFile       TLPWSTR
	FpHelpFile         TLPWSTR
	FpDependentFiles   TLPWSTR
	FpMonitorName      TLPWSTR
	FpDefaultDataType  TLPWSTR
	FpszzPreviousNames TLPWSTR
	FftDriverDate      TFILETIME
	F__ccgo_align12    [4]byte
	FdwlDriverVersion  TDWORDLONG
	FpszMfgName        TLPWSTR
	FpszOEMUrl         TLPWSTR
	FpszHardwareID     TLPWSTR
	FpszProvider       TLPWSTR
}

type TDRIVER_INFO_8 = struct {
	F__ccgo_align                [0]uint32
	FcVersion                    TDWORD
	FpName                       TLPSTR
	FpEnvironment                TLPSTR
	FpDriverPath                 TLPSTR
	FpDataFile                   TLPSTR
	FpConfigFile                 TLPSTR
	FpHelpFile                   TLPSTR
	FpDependentFiles             TLPSTR
	FpMonitorName                TLPSTR
	FpDefaultDataType            TLPSTR
	FpszzPreviousNames           TLPSTR
	FftDriverDate                TFILETIME
	F__ccgo_align12              [4]byte
	FdwlDriverVersion            TDWORDLONG
	FpszMfgName                  TLPSTR
	FpszOEMUrl                   TLPSTR
	FpszHardwareID               TLPSTR
	FpszProvider                 TLPSTR
	FpszPrintProcessor           TLPSTR
	FpszVendorSetup              TLPSTR
	FpszzColorProfiles           TLPSTR
	FpszInfPath                  TLPSTR
	FdwPrinterDriverAttributes   TDWORD
	FpszzCoreDriverDependencies  TLPSTR
	FftMinInboxDriverVerDate     TFILETIME
	FdwlMinInboxDriverVerVersion TDWORDLONG
}

type TDRIVER_INFO_8A = struct {
	F__ccgo_align                [0]uint32
	FcVersion                    TDWORD
	FpName                       TLPSTR
	FpEnvironment                TLPSTR
	FpDriverPath                 TLPSTR
	FpDataFile                   TLPSTR
	FpConfigFile                 TLPSTR
	FpHelpFile                   TLPSTR
	FpDependentFiles             TLPSTR
	FpMonitorName                TLPSTR
	FpDefaultDataType            TLPSTR
	FpszzPreviousNames           TLPSTR
	FftDriverDate                TFILETIME
	F__ccgo_align12              [4]byte
	FdwlDriverVersion            TDWORDLONG
	FpszMfgName                  TLPSTR
	FpszOEMUrl                   TLPSTR
	FpszHardwareID               TLPSTR
	FpszProvider                 TLPSTR
	FpszPrintProcessor           TLPSTR
	FpszVendorSetup              TLPSTR
	FpszzColorProfiles           TLPSTR
	FpszInfPath                  TLPSTR
	FdwPrinterDriverAttributes   TDWORD
	FpszzCoreDriverDependencies  TLPSTR
	FftMinInboxDriverVerDate     TFILETIME
	FdwlMinInboxDriverVerVersion TDWORDLONG
}

type TDRIVER_INFO_8W = struct {
	F__ccgo_align                [0]uint32
	FcVersion                    TDWORD
	FpName                       TLPWSTR
	FpEnvironment                TLPWSTR
	FpDriverPath                 TLPWSTR
	FpDataFile                   TLPWSTR
	FpConfigFile                 TLPWSTR
	FpHelpFile                   TLPWSTR
	FpDependentFiles             TLPWSTR
	FpMonitorName                TLPWSTR
	FpDefaultDataType            TLPWSTR
	FpszzPreviousNames           TLPWSTR
	FftDriverDate                TFILETIME
	F__ccgo_align12              [4]byte
	FdwlDriverVersion            TDWORDLONG
	FpszMfgName                  TLPWSTR
	FpszOEMUrl                   TLPWSTR
	FpszHardwareID               TLPWSTR
	FpszProvider                 TLPWSTR
	FpszPrintProcessor           TLPWSTR
	FpszVendorSetup              TLPWSTR
	FpszzColorProfiles           TLPWSTR
	FpszInfPath                  TLPWSTR
	FdwPrinterDriverAttributes   TDWORD
	FpszzCoreDriverDependencies  TLPWSTR
	FftMinInboxDriverVerDate     TFILETIME
	FdwlMinInboxDriverVerVersion TDWORDLONG
}

type TDRIVE_LAYOUT_INFORMATION = struct {
	F__ccgo_align   [0]uint32
	FPartitionCount TDWORD
	FSignature      TDWORD
	FPartitionEntry [1]TPARTITION_INFORMATION
}

type TDRIVE_LAYOUT_INFORMATION_EX = struct {
	F__ccgo_align   [0]uint32
	FPartitionStyle TDWORD
	FPartitionCount TDWORD
	F__ccgo2_8      struct {
		F__ccgo_align [0]uint32
		FGpt          [0]TDRIVE_LAYOUT_INFORMATION_GPT
		FMbr          TDRIVE_LAYOUT_INFORMATION_MBR
		F__ccgo_pad2  [36]byte
	}
	FPartitionEntry [1]TPARTITION_INFORMATION_EX
}

type TDRIVE_LAYOUT_INFORMATION_GPT = struct {
	F__ccgo_align         [0]uint32
	FDiskId               TGUID
	FStartingUsableOffset TLARGE_INTEGER
	FUsableLength         TLARGE_INTEGER
	FMaxPartitionCount    TDWORD
	F__ccgo_pad4          [4]byte
}

type TDWORD_PTR = uint32

// C documentation
//
//	/*
//	** A structure for holding a single date and time.
//	*/
type TDateTime = struct {
	F__ccgo_align [0]uint32
	FiJD          Tsqlite3_int64
	FY            int32
	FM            int32
	FD            int32
	Fh            int32
	Fm            int32
	Ftz           int32
	Fs            float64
	FvalidJD      int8
	FvalidYMD     int8
	FvalidHMS     int8
	FnFloor       int8
	F__ccgo44     uint8
	F__ccgo_pad17 [3]byte
}

type TELEMDESC = struct {
	Ftdesc     TTYPEDESC
	F__ccgo1_8 struct {
		Fparamdesc [0]TPARAMDESC
		Fidldesc   TIDLDESC
	}
}

type TENCRYPTED_DATA_INFO = struct {
	F__ccgo_align               [0]uint32
	FStartingFileOffset         TDWORDLONG
	FOutputBufferOffset         TDWORD
	FBytesWithinFileSize        TDWORD
	FBytesWithinValidDataLength TDWORD
	FCompressionFormat          TWORD
	FDataUnitShift              TBYTE
	FChunkShift                 TBYTE
	FClusterShift               TBYTE
	FEncryptionFormat           TBYTE
	FNumberOfDataBlocks         TWORD
	FDataBlockSize              [1]TDWORD
}

type TEXCEPTION_RECORD64 = struct {
	F__ccgo_align         [0]uint32
	FExceptionCode        TDWORD
	FExceptionFlags       TDWORD
	FExceptionRecord      TDWORD64
	FExceptionAddress     TDWORD64
	FNumberParameters     TDWORD
	F__unusedAlignment    TDWORD
	FExceptionInformation [15]TDWORD64
}

type TEXCEPTION_REGISTRATION = struct {
	F__ccgo0_0 struct {
		Fprev [0]uintptr
		FNext uintptr
	}
	F__ccgo1_4 struct {
		Fhandler [0]TPEXCEPTION_ROUTINE
		FHandler TPEXCEPTION_ROUTINE
	}
}

type TEXCEPTION_REGISTRATION_RECORD = struct {
	F__ccgo0_0 struct {
		Fprev [0]uintptr
		FNext uintptr
	}
	F__ccgo1_4 struct {
		Fhandler [0]TPEXCEPTION_ROUTINE
		FHandler TPEXCEPTION_ROUTINE
	}
}

type TEXPAND_VIRTUAL_DISK_PARAMETERS = struct {
	F__ccgo_align  [0]uint32
	FVersion       TEXPAND_VIRTUAL_DISK_VERSION
	F__ccgo_align1 [4]byte
	F__ccgo1_8     struct {
		F__ccgo_align [0]uint32
		FVersion1     struct {
			F__ccgo_align [0]uint32
			FNewSize      TULONGLONG
		}
	}
}

type TFILE_ALLOCATED_RANGE_BUFFER = struct {
	F__ccgo_align [0]uint32
	FFileOffset   TLARGE_INTEGER
	FLength       TLARGE_INTEGER
}

type TFILE_ALLOCATION_INFO = struct {
	F__ccgo_align   [0]uint32
	FAllocationSize TLARGE_INTEGER
}

type TFILE_BASIC_INFO = struct {
	F__ccgo_align   [0]uint32
	FCreationTime   TLARGE_INTEGER
	FLastAccessTime TLARGE_INTEGER
	FLastWriteTime  TLARGE_INTEGER
	FChangeTime     TLARGE_INTEGER
	FFileAttributes TDWORD
	F__ccgo_pad5    [4]byte
}

type TFILE_COMPRESSION_INFO = struct {
	F__ccgo_align         [0]uint32
	FCompressedFileSize   TLARGE_INTEGER
	FCompressionFormat    TWORD
	FCompressionUnitShift TUCHAR
	FChunkShift           TUCHAR
	FClusterShift         TUCHAR
	FReserved             [3]TUCHAR
}

type TFILE_END_OF_FILE_INFO = struct {
	F__ccgo_align [0]uint32
	FEndOfFile    TLARGE_INTEGER
}

type TFILE_FULL_DIR_INFO = struct {
	F__ccgo_align    [0]uint32
	FNextEntryOffset TULONG
	FFileIndex       TULONG
	FCreationTime    TLARGE_INTEGER
	FLastAccessTime  TLARGE_INTEGER
	FLastWriteTime   TLARGE_INTEGER
	FChangeTime      TLARGE_INTEGER
	FEndOfFile       TLARGE_INTEGER
	FAllocationSize  TLARGE_INTEGER
	FFileAttributes  TULONG
	FFileNameLength  TULONG
	FEaSize          TULONG
	FFileName        [1]TWCHAR
	F__ccgo_pad12    [2]byte
}

type TFILE_ID_BOTH_DIR_INFO = struct {
	F__ccgo_align    [0]uint32
	FNextEntryOffset TDWORD
	FFileIndex       TDWORD
	FCreationTime    TLARGE_INTEGER
	FLastAccessTime  TLARGE_INTEGER
	FLastWriteTime   TLARGE_INTEGER
	FChangeTime      TLARGE_INTEGER
	FEndOfFile       TLARGE_INTEGER
	FAllocationSize  TLARGE_INTEGER
	FFileAttributes  TDWORD
	FFileNameLength  TDWORD
	FEaSize          TDWORD
	FShortNameLength TCCHAR
	FShortName       [12]TWCHAR
	F__ccgo_align13  [2]byte
	FFileId          TLARGE_INTEGER
	FFileName        [1]TWCHAR
	F__ccgo_pad15    [6]byte
}

type TFILE_ID_DESCRIPTOR = struct {
	F__ccgo_align [0]uint32
	FdwSize       TDWORD
	FType         TFILE_ID_TYPE
	F__ccgo2_8    struct {
		F__ccgo_align   [0]uint32
		FObjectId       [0]TGUID
		FExtendedFileId [0]TFILE_ID_128
		FFileId         TLARGE_INTEGER
		F__ccgo_pad3    [8]byte
	}
}

type TFILE_ID_EXTD_DIR_INFO = struct {
	F__ccgo_align    [0]uint32
	FNextEntryOffset TULONG
	FFileIndex       TULONG
	FCreationTime    TLARGE_INTEGER
	FLastAccessTime  TLARGE_INTEGER
	FLastWriteTime   TLARGE_INTEGER
	FChangeTime      TLARGE_INTEGER
	FEndOfFile       TLARGE_INTEGER
	FAllocationSize  TLARGE_INTEGER
	FFileAttributes  TULONG
	FFileNameLength  TULONG
	FEaSize          TULONG
	FReparsePointTag TULONG
	FFileId          TFILE_ID_128
	FFileName        [1]TWCHAR
	F__ccgo_pad14    [6]byte
}

type TFILE_ID_INFO = struct {
	F__ccgo_align       [0]uint32
	FVolumeSerialNumber TULONGLONG
	FFileId             TFILE_ID_128
}

type TFILE_PREFETCH = struct {
	F__ccgo_align [0]uint32
	FType         TDWORD
	FCount        TDWORD
	FPrefetch     [1]TDWORDLONG
}

type TFILE_PREFETCH_EX = struct {
	F__ccgo_align  [0]uint32
	FType          TULONG
	FCount         TULONG
	FContext       TPVOID
	F__ccgo_align3 [4]byte
	FPrefetch      [1]TULONGLONG
}

type TFILE_QUERY_ON_DISK_VOL_INFO_BUFFER = struct {
	F__ccgo_align                    [0]uint32
	FDirectoryCount                  TLARGE_INTEGER
	FFileCount                       TLARGE_INTEGER
	FFsFormatMajVersion              TWORD
	FFsFormatMinVersion              TWORD
	FFsFormatName                    [12]TWCHAR
	F__ccgo_align5                   [4]byte
	FFormatTime                      TLARGE_INTEGER
	FLastUpdateTime                  TLARGE_INTEGER
	FCopyrightInfo                   [34]TWCHAR
	FAbstractInfo                    [34]TWCHAR
	FFormattingImplementationInfo    [34]TWCHAR
	FLastModifyingImplementationInfo [34]TWCHAR
}

type TFILE_SEGMENT_ELEMENT = struct {
	F__ccgo_align [0]uint32
	FAlignment    [0]TULONGLONG
	FBuffer       TPVOID64
	F__ccgo_pad2  [4]byte
}

type TFILE_STANDARD_INFO = struct {
	F__ccgo_align   [0]uint32
	FAllocationSize TLARGE_INTEGER
	FEndOfFile      TLARGE_INTEGER
	FNumberOfLinks  TDWORD
	FDeletePending  TBOOLEAN
	FDirectory      TBOOLEAN
	F__ccgo_pad5    [2]byte
}

type TFILE_STREAM_INFO = struct {
	F__ccgo_align         [0]uint32
	FNextEntryOffset      TDWORD
	FStreamNameLength     TDWORD
	FStreamSize           TLARGE_INTEGER
	FStreamAllocationSize TLARGE_INTEGER
	FStreamName           [1]TWCHAR
	F__ccgo_pad5          [6]byte
}

type TFILE_ZERO_DATA_INFORMATION = struct {
	F__ccgo_align    [0]uint32
	FFileOffset      TLARGE_INTEGER
	FBeyondFinalZero TLARGE_INTEGER
}

type TFLOAT128 = struct {
	F__ccgo_align [0]uint32
	FLowPart      int64
	FHighPart     int64
}

type TFLOATING_SAVE_AREA = struct {
	FControlWord   TDWORD
	FStatusWord    TDWORD
	FTagWord       TDWORD
	FErrorOffset   TDWORD
	FErrorSelector TDWORD
	FDataOffset    TDWORD
	FDataSelector  TDWORD
	FRegisterArea  [80]TBYTE
	FCr0NpxState   TDWORD
}

type TGESTUREINFO = struct {
	F__ccgo_align  [0]uint32
	FcbSize        TUINT
	FdwFlags       TDWORD
	FdwID          TDWORD
	FhwndTarget    THWND
	FptsLocation   TPOINTS
	FdwInstanceID  TDWORD
	FdwSequenceID  TDWORD
	F__ccgo_align7 [4]byte
	FullArguments  TULONGLONG
	FcbExtraArgs   TUINT
	F__ccgo_pad9   [4]byte
}

type TGET_LENGTH_INFORMATION = struct {
	F__ccgo_align [0]uint32
	FLength       TLARGE_INTEGER
}

type TGET_MEDIA_TYPES = struct {
	F__ccgo_align   [0]uint32
	FDeviceType     TDWORD
	FMediaInfoCount TDWORD
	FMediaInfo      [1]TDEVICE_MEDIA_INFO
}

type TGET_VIRTUAL_DISK_INFO = struct {
	F__ccgo_align  [0]uint32
	FVersion       TGET_VIRTUAL_DISK_INFO_VERSION
	F__ccgo_align1 [4]byte
	F__ccgo1_8     struct {
		F__ccgo_align   [0]uint32
		FIdentifier     [0]TGUID
		FParentLocation [0]struct {
			FParentResolved       TWINBOOL
			FParentLocationBuffer [1]TWCHAR
		}
		FParentIdentifier   [0]TGUID
		FParentTimestamp    [0]TULONG
		FVirtualStorageType [0]TVIRTUAL_STORAGE_TYPE
		FProviderSubtype    [0]TULONG
		FIs4kAligned        [0]TWINBOOL
		FIsLoaded           [0]TWINBOOL
		FPhysicalDisk       [0]struct {
			FLogicalSectorSize  TULONG
			FPhysicalSectorSize TULONG
			FIsRemote           TWINBOOL
		}
		FVhdPhysicalSectorSize   [0]TULONG
		FSmallestSafeVirtualSize [0]TULONGLONG
		FFragmentationPercentage [0]TULONG
		FVirtualDiskId           [0]TGUID
		FChangeTrackingState     [0]struct {
			FEnabled      TWINBOOL
			FNewerChanges TWINBOOL
			FMostRecentId [1]TWCHAR
		}
		FSize struct {
			F__ccgo_align [0]uint32
			FVirtualSize  TULONGLONG
			FPhysicalSize TULONGLONG
			FBlockSize    TULONG
			FSectorSize   TULONG
		}
	}
}

type THALF_PTR = int16

type THANDLE_PTR = uint32

type THARDWARE_COUNTER_DATA = struct {
	F__ccgo_align [0]uint32
	FType         THARDWARE_COUNTER_TYPE
	FReserved     TDWORD
	FValue        TDWORD64
}

type THCRYPTHASH = uint32

type THCRYPTKEY = uint32

type THCRYPTPROV = uint32

type THCRYPTPROV_LEGACY = uint32

type THCRYPTPROV_OR_NCRYPT_KEY_HANDLE = uint32

type TIMAGE_ALPHA64_RUNTIME_FUNCTION_ENTRY = struct {
	F__ccgo_align     [0]uint32
	FBeginAddress     TULONGLONG
	FEndAddress       TULONGLONG
	FExceptionHandler TULONGLONG
	FHandlerData      TULONGLONG
	FPrologEndAddress TULONGLONG
}

type TIMAGE_FUNCTION_ENTRY64 = struct {
	F__ccgo_align    [0]uint32
	FStartingAddress TULONGLONG
	FEndingAddress   TULONGLONG
	F__ccgo2_16      struct {
		F__ccgo_align      [0]uint32
		FUnwindInfoAddress [0]TULONGLONG
		FEndOfPrologue     TULONGLONG
	}
}

type TIMAGE_LOAD_CONFIG_DIRECTORY = struct {
	FSize                          TDWORD
	FTimeDateStamp                 TDWORD
	FMajorVersion                  TWORD
	FMinorVersion                  TWORD
	FGlobalFlagsClear              TDWORD
	FGlobalFlagsSet                TDWORD
	FCriticalSectionDefaultTimeout TDWORD
	FDeCommitFreeBlockThreshold    TDWORD
	FDeCommitTotalFreeThreshold    TDWORD
	FLockPrefixTable               TDWORD
	FMaximumAllocationSize         TDWORD
	FVirtualMemoryThreshold        TDWORD
	FProcessHeapFlags              TDWORD
	FProcessAffinityMask           TDWORD
	FCSDVersion                    TWORD
	FReserved1                     TWORD
	FEditList                      TDWORD
	FSecurityCookie                TDWORD
	FSEHandlerTable                TDWORD
	FSEHandlerCount                TDWORD
}

type TIMAGE_LOAD_CONFIG_DIRECTORY64 = struct {
	F__ccgo_align                  [0]uint32
	FSize                          TDWORD
	FTimeDateStamp                 TDWORD
	FMajorVersion                  TWORD
	FMinorVersion                  TWORD
	FGlobalFlagsClear              TDWORD
	FGlobalFlagsSet                TDWORD
	FCriticalSectionDefaultTimeout TDWORD
	FDeCommitFreeBlockThreshold    TULONGLONG
	FDeCommitTotalFreeThreshold    TULONGLONG
	FLockPrefixTable               TULONGLONG
	FMaximumAllocationSize         TULONGLONG
	FVirtualMemoryThreshold        TULONGLONG
	FProcessAffinityMask           TULONGLONG
	FProcessHeapFlags              TDWORD
	FCSDVersion                    TWORD
	FReserved1                     TWORD
	FEditList                      TULONGLONG
	FSecurityCookie                TULONGLONG
	FSEHandlerTable                TULONGLONG
	FSEHandlerCount                TULONGLONG
}

type TIMAGE_NT_HEADERS = struct {
	FSignature      TDWORD
	FFileHeader     TIMAGE_FILE_HEADER
	FOptionalHeader TIMAGE_OPTIONAL_HEADER32
}

type TIMAGE_NT_HEADERS64 = struct {
	F__ccgo_align   [0]uint32
	FSignature      TDWORD
	FFileHeader     TIMAGE_FILE_HEADER
	FOptionalHeader TIMAGE_OPTIONAL_HEADER64
}

type TIMAGE_OPTIONAL_HEADER = struct {
	FMagic                       TWORD
	FMajorLinkerVersion          TBYTE
	FMinorLinkerVersion          TBYTE
	FSizeOfCode                  TDWORD
	FSizeOfInitializedData       TDWORD
	FSizeOfUninitializedData     TDWORD
	FAddressOfEntryPoint         TDWORD
	FBaseOfCode                  TDWORD
	FBaseOfData                  TDWORD
	FImageBase                   TDWORD
	FSectionAlignment            TDWORD
	FFileAlignment               TDWORD
	FMajorOperatingSystemVersion TWORD
	FMinorOperatingSystemVersion TWORD
	FMajorImageVersion           TWORD
	FMinorImageVersion           TWORD
	FMajorSubsystemVersion       TWORD
	FMinorSubsystemVersion       TWORD
	FWin32VersionValue           TDWORD
	FSizeOfImage                 TDWORD
	FSizeOfHeaders               TDWORD
	FCheckSum                    TDWORD
	FSubsystem                   TWORD
	FDllCharacteristics          TWORD
	FSizeOfStackReserve          TDWORD
	FSizeOfStackCommit           TDWORD
	FSizeOfHeapReserve           TDWORD
	FSizeOfHeapCommit            TDWORD
	FLoaderFlags                 TDWORD
	FNumberOfRvaAndSizes         TDWORD
	FDataDirectory               [16]TIMAGE_DATA_DIRECTORY
}

type TIMAGE_OPTIONAL_HEADER64 = struct {
	F__ccgo_align                [0]uint32
	FMagic                       TWORD
	FMajorLinkerVersion          TBYTE
	FMinorLinkerVersion          TBYTE
	FSizeOfCode                  TDWORD
	FSizeOfInitializedData       TDWORD
	FSizeOfUninitializedData     TDWORD
	FAddressOfEntryPoint         TDWORD
	FBaseOfCode                  TDWORD
	FImageBase                   TULONGLONG
	FSectionAlignment            TDWORD
	FFileAlignment               TDWORD
	FMajorOperatingSystemVersion TWORD
	FMinorOperatingSystemVersion TWORD
	FMajorImageVersion           TWORD
	FMinorImageVersion           TWORD
	FMajorSubsystemVersion       TWORD
	FMinorSubsystemVersion       TWORD
	FWin32VersionValue           TDWORD
	FSizeOfImage                 TDWORD
	FSizeOfHeaders               TDWORD
	FCheckSum                    TDWORD
	FSubsystem                   TWORD
	FDllCharacteristics          TWORD
	FSizeOfStackReserve          TULONGLONG
	FSizeOfStackCommit           TULONGLONG
	FSizeOfHeapReserve           TULONGLONG
	FSizeOfHeapCommit            TULONGLONG
	FLoaderFlags                 TDWORD
	FNumberOfRvaAndSizes         TDWORD
	FDataDirectory               [16]TIMAGE_DATA_DIRECTORY
}

type TIMAGE_THUNK_DATA = struct {
	Fu1 struct {
		FFunction        [0]TDWORD
		FOrdinal         [0]TDWORD
		FAddressOfData   [0]TDWORD
		FForwarderString TDWORD
	}
}

type TIMAGE_THUNK_DATA64 = struct {
	F__ccgo_align [0]uint32
	Fu1           struct {
		F__ccgo_align    [0]uint32
		FFunction        [0]TULONGLONG
		FOrdinal         [0]TULONGLONG
		FAddressOfData   [0]TULONGLONG
		FForwarderString TULONGLONG
	}
}

type TIMAGE_TLS_DIRECTORY = struct {
	FStartAddressOfRawData TDWORD
	FEndAddressOfRawData   TDWORD
	FAddressOfIndex        TDWORD
	FAddressOfCallBacks    TDWORD
	FSizeOfZeroFill        TDWORD
	FCharacteristics       TDWORD
}

type TIMAGE_TLS_DIRECTORY64 = struct {
	F__ccgo_align          [0]uint32
	FStartAddressOfRawData TULONGLONG
	FEndAddressOfRawData   TULONGLONG
	FAddressOfIndex        TULONGLONG
	FAddressOfCallBacks    TULONGLONG
	FSizeOfZeroFill        TDWORD
	FCharacteristics       TDWORD
}

type TINPUT = struct {
	Ftype1     TDWORD
	F__ccgo1_4 struct {
		Fki [0]TKEYBDINPUT
		Fhi [0]THARDWAREINPUT
		Fmi TMOUSEINPUT
	}
}

type TINT_PTR = int32

type TIO_COUNTERS = struct {
	F__ccgo_align        [0]uint32
	FReadOperationCount  TULONGLONG
	FWriteOperationCount TULONGLONG
	FOtherOperationCount TULONGLONG
	FReadTransferCount   TULONGLONG
	FWriteTransferCount  TULONGLONG
	FOtherTransferCount  TULONGLONG
}

type TJIT_DEBUG_INFO = struct {
	F__ccgo_align            [0]uint32
	FdwSize                  TDWORD
	FdwProcessorArchitecture TDWORD
	FdwThreadID              TDWORD
	FdwReserved0             TDWORD
	FlpExceptionAddress      TULONG64
	FlpExceptionRecord       TULONG64
	FlpContextRecord         TULONG64
}

type TJIT_DEBUG_INFO32 = struct {
	F__ccgo_align            [0]uint32
	FdwSize                  TDWORD
	FdwProcessorArchitecture TDWORD
	FdwThreadID              TDWORD
	FdwReserved0             TDWORD
	FlpExceptionAddress      TULONG64
	FlpExceptionRecord       TULONG64
	FlpContextRecord         TULONG64
}

type TJIT_DEBUG_INFO64 = struct {
	F__ccgo_align            [0]uint32
	FdwSize                  TDWORD
	FdwProcessorArchitecture TDWORD
	FdwThreadID              TDWORD
	FdwReserved0             TDWORD
	FlpExceptionAddress      TULONG64
	FlpExceptionRecord       TULONG64
	FlpContextRecord         TULONG64
}

type TJOBOBJECT_BASIC_ACCOUNTING_INFORMATION = struct {
	F__ccgo_align              [0]uint32
	FTotalUserTime             TLARGE_INTEGER
	FTotalKernelTime           TLARGE_INTEGER
	FThisPeriodTotalUserTime   TLARGE_INTEGER
	FThisPeriodTotalKernelTime TLARGE_INTEGER
	FTotalPageFaultCount       TDWORD
	FTotalProcesses            TDWORD
	FActiveProcesses           TDWORD
	FTotalTerminatedProcesses  TDWORD
}

type TJOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION = struct {
	F__ccgo_align [0]uint32
	FBasicInfo    TJOBOBJECT_BASIC_ACCOUNTING_INFORMATION
	FIoInfo       TIO_COUNTERS
}

type TJOBOBJECT_BASIC_LIMIT_INFORMATION = struct {
	F__ccgo_align            [0]uint32
	FPerProcessUserTimeLimit TLARGE_INTEGER
	FPerJobUserTimeLimit     TLARGE_INTEGER
	FLimitFlags              TDWORD
	FMinimumWorkingSetSize   TSIZE_T
	FMaximumWorkingSetSize   TSIZE_T
	FActiveProcessLimit      TDWORD
	FAffinity                TULONG_PTR
	FPriorityClass           TDWORD
	FSchedulingClass         TDWORD
	F__ccgo_pad9             [4]byte
}

type TJOBOBJECT_EXTENDED_LIMIT_INFORMATION = struct {
	F__ccgo_align          [0]uint32
	FBasicLimitInformation TJOBOBJECT_BASIC_LIMIT_INFORMATION
	FIoInfo                TIO_COUNTERS
	FProcessMemoryLimit    TSIZE_T
	FJobMemoryLimit        TSIZE_T
	FPeakProcessMemoryUsed TSIZE_T
	FPeakJobMemoryUsed     TSIZE_T
}

type TJOBOBJECT_LIMIT_VIOLATION_INFORMATION = struct {
	F__ccgo_align              [0]uint32
	FLimitFlags                TDWORD
	FViolationLimitFlags       TDWORD
	FIoReadBytes               TDWORD64
	FIoReadBytesLimit          TDWORD64
	FIoWriteBytes              TDWORD64
	FIoWriteBytesLimit         TDWORD64
	FPerJobUserTime            TLARGE_INTEGER
	FPerJobUserTimeLimit       TLARGE_INTEGER
	FJobMemory                 TDWORD64
	FJobMemoryLimit            TDWORD64
	FRateControlTolerance      TJOBOBJECT_RATE_CONTROL_TOLERANCE
	FRateControlToleranceLimit TJOBOBJECT_RATE_CONTROL_TOLERANCE_INTERVAL
}

type TJOBOBJECT_NOTIFICATION_LIMIT_INFORMATION = struct {
	F__ccgo_align                 [0]uint32
	FIoReadBytesLimit             TDWORD64
	FIoWriteBytesLimit            TDWORD64
	FPerJobUserTimeLimit          TLARGE_INTEGER
	FJobMemoryLimit               TDWORD64
	FRateControlTolerance         TJOBOBJECT_RATE_CONTROL_TOLERANCE
	FRateControlToleranceInterval TJOBOBJECT_RATE_CONTROL_TOLERANCE_INTERVAL
	FLimitFlags                   TDWORD
	F__ccgo_pad7                  [4]byte
}

type TJsonString = struct {
	F__ccgo_align [0]uint32
	FpCtx         uintptr
	FzBuf         uintptr
	FnAlloc       Tu64
	FnUsed        Tu64
	FbStatic      Tu8
	FeErr         Tu8
	FzSpace       [100]int8
	F__ccgo_pad7  [2]byte
}

type TKAFFINITY = uint32

type TKSPIN_LOCK = uint32

type TLARGE_INTEGER = struct {
	F__ccgo_align [0]uint32
	Fu            [0]struct {
		FLowPart  TDWORD
		FHighPart TLONG
	}
	FQuadPart  [0]TLONGLONG
	F__ccgo0_0 struct {
		FLowPart  TDWORD
		FHighPart TLONG
	}
}

type TLIST_ENTRY64 = struct {
	F__ccgo_align [0]uint32
	FFlink        TULONGLONG
	FBlink        TULONGLONG
}

type TLONG_PTR = int32

type TLOOKUP_STREAM_FROM_CLUSTER_ENTRY = struct {
	F__ccgo_align [0]uint32
	FOffsetToNext TDWORD
	FFlags        TDWORD
	FReserved     TLARGE_INTEGER
	FCluster      TLARGE_INTEGER
	FFileName     [1]TWCHAR
	F__ccgo_pad5  [6]byte
}

type TLOOKUP_STREAM_FROM_CLUSTER_INPUT = struct {
	F__ccgo_align     [0]uint32
	FFlags            TDWORD
	FNumberOfClusters TDWORD
	FCluster          [1]TLARGE_INTEGER
}

type TLPARAM = int32

type TLRESULT = int32

type TM128A = struct {
	F__ccgo_align [0]uint32
	FLow          TULONGLONG
	FHigh         TLONGLONG
}

type TMEMORYSTATUSEX = struct {
	F__ccgo_align            [0]uint32
	FdwLength                TDWORD
	FdwMemoryLoad            TDWORD
	FullTotalPhys            TDWORDLONG
	FullAvailPhys            TDWORDLONG
	FullTotalPageFile        TDWORDLONG
	FullAvailPageFile        TDWORDLONG
	FullTotalVirtual         TDWORDLONG
	FullAvailVirtual         TDWORDLONG
	FullAvailExtendedVirtual TDWORDLONG
}

type TMEMORY_BASIC_INFORMATION64 = struct {
	F__ccgo_align      [0]uint32
	FBaseAddress       TULONGLONG
	FAllocationBase    TULONGLONG
	FAllocationProtect TDWORD
	F__alignment1      TDWORD
	FRegionSize        TULONGLONG
	FState             TDWORD
	FProtect           TDWORD
	FType              TDWORD
	F__alignment2      TDWORD
}

type TMEM_EXTENDED_PARAMETER = struct {
	F__ccgo_align [0]uint32
	F__ccgo0_0    struct {
		F__ccgo_align [0]uint32
		F__ccgo0      uint64
	}
	F__ccgo1_8 struct {
		F__ccgo_align [0]uint32
		FPointer      [0]TPVOID
		FSize         [0]TSIZE_T
		FHandle       [0]THANDLE
		FULong        [0]TDWORD
		FULong64      TDWORD64
	}
}

type TMENUBARINFO = struct {
	FcbSize   TDWORD
	FrcBar    TRECT
	FhMenu    THMENU
	FhwndMenu THWND
	F__ccgo28 uint8
}

type TMFT_ENUM_DATA = struct {
	F__ccgo_align             [0]uint32
	FStartFileReferenceNumber TDWORDLONG
	FLowUsn                   TUSN
	FHighUsn                  TUSN
}

type TMIDL_STUB_MESSAGE = struct {
	FRpcMsg                 TPRPC_MESSAGE
	FBuffer                 uintptr
	FBufferStart            uintptr
	FBufferEnd              uintptr
	FBufferMark             uintptr
	FBufferLength           uint32
	FMemorySize             uint32
	FMemory                 uintptr
	FIsClient               uint8
	FPad                    uint8
	FuFlags2                uint16
	FReuseBuffer            int32
	FpAllocAllNodesContext  uintptr
	FpPointerQueueState     uintptr
	FIgnoreEmbeddedPointers int32
	FPointerBufferMark      uintptr
	FfBufferValid           uint8
	FuFlags                 uint8
	FUniquePtrCount         uint16
	FMaxCount               TULONG_PTR
	FOffset                 uint32
	FActualCount            uint32
	FpfnAllocate            uintptr
	FpfnFree                uintptr
	FStackTop               uintptr
	FpPresentedType         uintptr
	FpTransmitType          uintptr
	FSavedHandle            Thandle_t
	FStubDesc               uintptr
	FFullPtrXlatTables      uintptr
	FFullPtrRefId           uint32
	FPointerLength          uint32
	F__ccgo112              uint32
	FdwDestContext          uint32
	FpvDestContext          uintptr
	FSavedContextHandles    uintptr
	FParamNumber            int32
	FpRpcChannelBuffer      uintptr
	FpArrayInfo             TPARRAY_INFO
	FSizePtrCountArray      uintptr
	FSizePtrOffsetArray     uintptr
	FSizePtrLengthArray     uintptr
	FpArgQueue              uintptr
	FdwStubPhase            uint32
	FLowStackMark           uintptr
	FpAsyncMsg              TPNDR_ASYNC_MESSAGE
	FpCorrInfo              TPNDR_CORRELATION_INFO
	FpCorrMemory            uintptr
	FpMemoryList            uintptr
	FpCSInfo                uintptr
	FConformanceMark        uintptr
	FVarianceMark           uintptr
	FUnused                 TINT_PTR
	FpContext               uintptr
	FpUserMarshalList       uintptr
	FReserved51_2           TINT_PTR
	FReserved51_3           TINT_PTR
	FReserved51_4           TINT_PTR
	FReserved51_5           TINT_PTR
}

type TMIRROR_VIRTUAL_DISK_PARAMETERS = struct {
	FVersion   TMIRROR_VIRTUAL_DISK_VERSION
	F__ccgo1_4 struct {
		FVersion1 struct {
			FMirrorVirtualDiskPath TPCWSTR
		}
	}
}

type TMIXERCONTROLDETAILS = struct {
	FcbStruct    TDWORD
	FdwControlID TDWORD
	FcChannels   TDWORD
	F__ccgo3_12  struct {
		FcMultipleItems [0]TDWORD
		FhwndOwner      THWND
	}
	FcbDetails TDWORD
	FpaDetails TLPVOID
}

type TMODIFY_VHDSET_PARAMETERS = struct {
	FVersion   TMODIFY_VHDSET_VERSION
	F__ccgo1_4 struct {
		FSnapshotId      [0]TGUID
		FDefaultFilePath [0]TPCWSTR
		FSnapshotPath    struct {
			FSnapshotId       TGUID
			FSnapshotFilePath TPCWSTR
		}
	}
}

type TMOVE_FILE_DATA = struct {
	F__ccgo_align  [0]uint32
	FFileHandle    THANDLE
	F__ccgo_align1 [4]byte
	FStartingVcn   TLARGE_INTEGER
	FStartingLcn   TLARGE_INTEGER
	FClusterCount  TDWORD
	F__ccgo_pad4   [4]byte
}

type TMOVE_FILE_RECORD_DATA = struct {
	F__ccgo_align     [0]uint32
	FFileHandle       THANDLE
	F__ccgo_align1    [4]byte
	FSourceFileRecord TLARGE_INTEGER
	FTargetFileRecord TLARGE_INTEGER
}

type TNCB = struct {
	Fncb_command  TUCHAR
	Fncb_retcode  TUCHAR
	Fncb_lsn      TUCHAR
	Fncb_num      TUCHAR
	Fncb_buffer   TPUCHAR
	Fncb_length   TWORD
	Fncb_callname [16]TUCHAR
	Fncb_name     [16]TUCHAR
	Fncb_rto      TUCHAR
	Fncb_sto      TUCHAR
	Fncb_post     uintptr
	Fncb_lana_num TUCHAR
	Fncb_cmd_cplt TUCHAR
	Fncb_reserve  [10]TUCHAR
	Fncb_event    THANDLE
}

type TNCRYPT_HANDLE = uint32

type TNCRYPT_HASH_HANDLE = uint32

type TNCRYPT_KEY_HANDLE = uint32

type TNCRYPT_PROV_HANDLE = uint32

type TNCRYPT_SECRET_HANDLE = uint32

type TNDR_USER_MARSHAL_INFO = struct {
	FInformationLevel uint32
	F__ccgo1_4        struct {
		FLevel1 TNDR_USER_MARSHAL_INFO_LEVEL1
	}
}

type TNON_PAGED_DEBUG_INFO = struct {
	F__ccgo_align    [0]uint32
	FSignature       TWORD
	FFlags           TWORD
	FSize            TDWORD
	FMachine         TWORD
	FCharacteristics TWORD
	FTimeDateStamp   TDWORD
	FCheckSum        TDWORD
	FSizeOfImage     TDWORD
	FImageBase       TULONGLONG
}

type TNOTIFYICONDATA = struct {
	FcbSize           TDWORD
	FhWnd             THWND
	FuID              TUINT
	FuFlags           TUINT
	FuCallbackMessage TUINT
	FhIcon            THICON
	FszTip            [128]TCHAR
	FdwState          TDWORD
	FdwStateMask      TDWORD
	FszInfo           [256]TCHAR
	F__ccgo10_416     struct {
		FuVersion [0]TUINT
		FuTimeout TUINT
	}
	FszInfoTitle  [64]TCHAR
	FdwInfoFlags  TDWORD
	FguidItem     TGUID
	FhBalloonIcon THICON
}

type TNOTIFYICONDATAA = struct {
	FcbSize           TDWORD
	FhWnd             THWND
	FuID              TUINT
	FuFlags           TUINT
	FuCallbackMessage TUINT
	FhIcon            THICON
	FszTip            [128]TCHAR
	FdwState          TDWORD
	FdwStateMask      TDWORD
	FszInfo           [256]TCHAR
	F__ccgo10_416     struct {
		FuVersion [0]TUINT
		FuTimeout TUINT
	}
	FszInfoTitle  [64]TCHAR
	FdwInfoFlags  TDWORD
	FguidItem     TGUID
	FhBalloonIcon THICON
}

type TNOTIFYICONDATAW = struct {
	FcbSize           TDWORD
	FhWnd             THWND
	FuID              TUINT
	FuFlags           TUINT
	FuCallbackMessage TUINT
	FhIcon            THICON
	FszTip            [128]TWCHAR
	FdwState          TDWORD
	FdwStateMask      TDWORD
	FszInfo           [256]TWCHAR
	F__ccgo10_800     struct {
		FuVersion [0]TUINT
		FuTimeout TUINT
	}
	FszInfoTitle  [64]TWCHAR
	FdwInfoFlags  TDWORD
	FguidItem     TGUID
	FhBalloonIcon THICON
}

type TNTFS_FILE_RECORD_INPUT_BUFFER = struct {
	F__ccgo_align        [0]uint32
	FFileReferenceNumber TLARGE_INTEGER
}

type TNTFS_FILE_RECORD_OUTPUT_BUFFER = struct {
	F__ccgo_align        [0]uint32
	FFileReferenceNumber TLARGE_INTEGER
	FFileRecordLength    TDWORD
	FFileRecordBuffer    [1]TBYTE
	F__ccgo_pad3         [3]byte
}

type TNTFS_VOLUME_DATA_BUFFER = struct {
	F__ccgo_align                 [0]uint32
	FVolumeSerialNumber           TLARGE_INTEGER
	FNumberSectors                TLARGE_INTEGER
	FTotalClusters                TLARGE_INTEGER
	FFreeClusters                 TLARGE_INTEGER
	FTotalReserved                TLARGE_INTEGER
	FBytesPerSector               TDWORD
	FBytesPerCluster              TDWORD
	FBytesPerFileRecordSegment    TDWORD
	FClustersPerFileRecordSegment TDWORD
	FMftValidDataLength           TLARGE_INTEGER
	FMftStartLcn                  TLARGE_INTEGER
	FMft2StartLcn                 TLARGE_INTEGER
	FMftZoneStart                 TLARGE_INTEGER
	FMftZoneEnd                   TLARGE_INTEGER
}

type TNT_TIB = struct {
	FExceptionList uintptr
	FStackBase     TPVOID
	FStackLimit    TPVOID
	FSubSystemTib  TPVOID
	F__ccgo4_16    struct {
		FVersion   [0]TDWORD
		FFiberData TPVOID
	}
	FArbitraryUserPointer TPVOID
	FSelf                 uintptr
}

type TNT_TIB64 = struct {
	F__ccgo_align  [0]uint32
	FExceptionList TDWORD64
	FStackBase     TDWORD64
	FStackLimit    TDWORD64
	FSubSystemTib  TDWORD64
	F__ccgo4_32    struct {
		F__ccgo_align [0]uint32
		FVersion      [0]TDWORD
		FFiberData    TDWORD64
	}
	FArbitraryUserPointer TDWORD64
	FSelf                 TDWORD64
}

type TOCSP_BASIC_RESPONSE_ENTRY = struct {
	FCertId       TOCSP_CERT_ID
	FdwCertStatus TDWORD
	F__ccgo2_40   struct {
		FpRevokedInfo TPOCSP_BASIC_REVOKED_INFO
	}
	FThisUpdate  TFILETIME
	FNextUpdate  TFILETIME
	FcExtension  TDWORD
	FrgExtension TPCERT_EXTENSION
}

type TOVERLAPPED = struct {
	FInternal     TULONG_PTR
	FInternalHigh TULONG_PTR
	F__ccgo2_8    struct {
		FPointer   [0]TPVOID
		F__ccgo0_0 struct {
			FOffset     TDWORD
			FOffsetHigh TDWORD
		}
	}
	FhEvent THANDLE
}

type TPARAMDESCEX = struct {
	F__ccgo_align    [0]uint32
	FcBytes          TULONG
	F__ccgo_align1   [4]byte
	FvarDefaultValue TVARIANTARG
}

type TPARTITION_INFORMATION = struct {
	F__ccgo_align        [0]uint32
	FStartingOffset      TLARGE_INTEGER
	FPartitionLength     TLARGE_INTEGER
	FHiddenSectors       TDWORD
	FPartitionNumber     TDWORD
	FPartitionType       TBYTE
	FBootIndicator       TBOOLEAN
	FRecognizedPartition TBOOLEAN
	FRewritePartition    TBOOLEAN
	F__ccgo_pad8         [4]byte
}

type TPARTITION_INFORMATION_EX = struct {
	F__ccgo_align     [0]uint32
	FPartitionStyle   TPARTITION_STYLE
	F__ccgo_align1    [4]byte
	FStartingOffset   TLARGE_INTEGER
	FPartitionLength  TLARGE_INTEGER
	FPartitionNumber  TDWORD
	FRewritePartition TBOOLEAN
	F__ccgo_align5    [3]byte
	F__ccgo5_32       struct {
		F__ccgo_align [0]uint32
		FGpt          [0]TPARTITION_INFORMATION_GPT
		FMbr          TPARTITION_INFORMATION_MBR
		F__ccgo_pad2  [88]byte
	}
}

type TPARTITION_INFORMATION_GPT = struct {
	F__ccgo_align  [0]uint32
	FPartitionType TGUID
	FPartitionId   TGUID
	FAttributes    TDWORD64
	FName          [36]TWCHAR
}

type TPERFORMANCE_DATA = struct {
	F__ccgo_align       [0]uint32
	FSize               TWORD
	FVersion            TBYTE
	FHwCountersCount    TBYTE
	FContextSwitchCount TDWORD
	FWaitReasonBitMap   TDWORD64
	FCycleTime          TDWORD64
	FRetryCount         TDWORD
	FReserved           TDWORD
	FHwCounters         [16]THARDWARE_COUNTER_DATA
}

type TPERF_BIN = struct {
	F__ccgo_align [0]uint32
	FNumberOfBins TDWORD
	FTypeOfBin    TDWORD
	FBinsRanges   [1]TBIN_RANGE
}

type TPERF_COUNTER_DEFINITION = struct {
	FByteLength            TDWORD
	FCounterNameTitleIndex TDWORD
	FCounterNameTitle      TLPWSTR
	FCounterHelpTitleIndex TDWORD
	FCounterHelpTitle      TLPWSTR
	FDefaultScale          TLONG
	FDetailLevel           TDWORD
	FCounterType           TDWORD
	FCounterSize           TDWORD
	FCounterOffset         TDWORD
}

type TPERF_DATA_BLOCK = struct {
	F__ccgo_align     [0]uint32
	FSignature        [4]TWCHAR
	FLittleEndian     TDWORD
	FVersion          TDWORD
	FRevision         TDWORD
	FTotalByteLength  TDWORD
	FHeaderLength     TDWORD
	FNumObjectTypes   TDWORD
	FDefaultObject    TLONG
	FSystemTime       TSYSTEMTIME
	F__ccgo_align9    [4]byte
	FPerfTime         TLARGE_INTEGER
	FPerfFreq         TLARGE_INTEGER
	FPerfTime100nSec  TLARGE_INTEGER
	FSystemNameLength TDWORD
	FSystemNameOffset TDWORD
}

type TPERF_OBJECT_TYPE = struct {
	F__ccgo_align         [0]uint32
	FTotalByteLength      TDWORD
	FDefinitionLength     TDWORD
	FHeaderLength         TDWORD
	FObjectNameTitleIndex TDWORD
	FObjectNameTitle      TLPWSTR
	FObjectHelpTitleIndex TDWORD
	FObjectHelpTitle      TLPWSTR
	FDetailLevel          TDWORD
	FNumCounters          TDWORD
	FDefaultCounter       TLONG
	FNumInstances         TLONG
	FCodePage             TDWORD
	FPerfTime             TLARGE_INTEGER
	FPerfFreq             TLARGE_INTEGER
}

type TPFLOATING_SAVE_AREA = uintptr

type TPLEX_READ_DATA_REQUEST = struct {
	F__ccgo_align [0]uint32
	FByteOffset   TLARGE_INTEGER
	FByteLength   TDWORD
	FPlexNumber   TDWORD
}

type TPOINTER_64_INT = uint32

type TPOINTER_INFO = struct {
	F__ccgo_align          [0]uint32
	FpointerType           TPOINTER_INPUT_TYPE
	FpointerId             TUINT32
	FframeId               TUINT32
	FpointerFlags          TPOINTER_FLAGS
	FsourceDevice          THANDLE
	FhwndTarget            THWND
	FptPixelLocation       TPOINT
	FptHimetricLocation    TPOINT
	FptPixelLocationRaw    TPOINT
	FptHimetricLocationRaw TPOINT
	FdwTime                TDWORD
	FhistoryCount          TUINT32
	FInputData             TINT32
	FdwKeyStates           TDWORD
	FPerformanceCount      TUINT64
	FButtonChangeType      TPOINTER_BUTTON_CHANGE_TYPE
	F__ccgo_pad16          [4]byte
}

type TPOINTER_PEN_INFO = struct {
	F__ccgo_align [0]uint32
	FpointerInfo  TPOINTER_INFO
	FpenFlags     TPEN_FLAGS
	FpenMask      TPEN_MASK
	Fpressure     TUINT32
	Frotation     TUINT32
	FtiltX        TINT32
	FtiltY        TINT32
}

type TPOINTER_TOUCH_INFO = struct {
	F__ccgo_align [0]uint32
	FpointerInfo  TPOINTER_INFO
	FtouchFlags   TTOUCH_FLAGS
	FtouchMask    TTOUCH_MASK
	FrcContact    TRECT
	FrcContactRaw TRECT
	Forientation  TUINT32
	Fpressure     TUINT32
}

type TPOINTER_TYPE_INFO = struct {
	F__ccgo_align  [0]uint32
	Ftype1         TPOINTER_INPUT_TYPE
	F__ccgo_align1 [4]byte
	F__ccgo1_8     struct {
		F__ccgo_align [0]uint32
		FpenInfo      [0]TPOINTER_PEN_INFO
		FtouchInfo    TPOINTER_TOUCH_INFO
	}
}

type TPPM_IDLESTATE_EVENT = struct {
	F__ccgo_align [0]uint32
	FNewState     TDWORD
	FOldState     TDWORD
	FProcessors   TDWORD64
}

type TPPM_IDLE_ACCOUNTING = struct {
	F__ccgo_align     [0]uint32
	FStateCount       TDWORD
	FTotalTransitions TDWORD
	FResetCount       TDWORD
	F__ccgo_align3    [4]byte
	FStartTime        TDWORD64
	FState            [1]TPPM_IDLE_STATE_ACCOUNTING
}

type TPPM_IDLE_ACCOUNTING_EX = struct {
	F__ccgo_align     [0]uint32
	FStateCount       TDWORD
	FTotalTransitions TDWORD
	FResetCount       TDWORD
	FAbortCount       TDWORD
	FStartTime        TDWORD64
	FState            [1]TPPM_IDLE_STATE_ACCOUNTING_EX
}

type TPPM_IDLE_STATE_ACCOUNTING = struct {
	F__ccgo_align       [0]uint32
	FIdleTransitions    TDWORD
	FFailedTransitions  TDWORD
	FInvalidBucketIndex TDWORD
	F__ccgo_align3      [4]byte
	FTotalTime          TDWORD64
	FIdleTimeBuckets    [6]TDWORD
}

type TPPM_IDLE_STATE_ACCOUNTING_EX = struct {
	F__ccgo_align         [0]uint32
	FTotalTime            TDWORD64
	FIdleTransitions      TDWORD
	FFailedTransitions    TDWORD
	FInvalidBucketIndex   TDWORD
	FMinTimeUs            TDWORD
	FMaxTimeUs            TDWORD
	FCancelledTransitions TDWORD
	FIdleTimeBuckets      [16]TPPM_IDLE_STATE_BUCKET_EX
}

type TPPM_IDLE_STATE_BUCKET_EX = struct {
	F__ccgo_align [0]uint32
	FTotalTimeUs  TDWORD64
	FMinTimeUs    TDWORD
	FMaxTimeUs    TDWORD
	FCount        TDWORD
	F__ccgo_pad4  [4]byte
}

type TPPM_PERFSTATE_DOMAIN_EVENT = struct {
	F__ccgo_align  [0]uint32
	FState         TDWORD
	FLatency       TDWORD
	FSpeed         TDWORD
	F__ccgo_align3 [4]byte
	FProcessors    TDWORD64
}

type TPPM_THERMALCHANGE_EVENT = struct {
	F__ccgo_align      [0]uint32
	FThermalConstraint TDWORD
	F__ccgo_align1     [4]byte
	FProcessors        TDWORD64
}

type TPPM_THERMAL_POLICY_EVENT = struct {
	F__ccgo_align  [0]uint32
	FMode          TBYTE
	F__ccgo_align1 [7]byte
	FProcessors    TDWORD64
}

type TPPM_WMI_IDLE_STATES = struct {
	F__ccgo_align     [0]uint32
	FType             TDWORD
	FCount            TDWORD
	FTargetState      TDWORD
	FOldState         TDWORD
	FTargetProcessors TDWORD64
	FState            [1]TPPM_WMI_IDLE_STATE
}

type TPPM_WMI_PERF_STATE = struct {
	F__ccgo_align     [0]uint32
	FFrequency        TDWORD
	FPower            TDWORD
	FPercentFrequency TBYTE
	FIncreaseLevel    TBYTE
	FDecreaseLevel    TBYTE
	FType             TBYTE
	FIncreaseTime     TDWORD
	FDecreaseTime     TDWORD
	F__ccgo_align8    [4]byte
	FControl          TDWORD64
	FStatus           TDWORD64
	FHitCount         TDWORD
	FReserved1        TDWORD
	FReserved2        TDWORD64
	FReserved3        TDWORD64
}

type TPPM_WMI_PERF_STATES = struct {
	F__ccgo_align      [0]uint32
	FCount             TDWORD
	FMaxFrequency      TDWORD
	FCurrentState      TDWORD
	FMaxPerfState      TDWORD
	FMinPerfState      TDWORD
	FLowestPerfState   TDWORD
	FThermalConstraint TDWORD
	FBusyAdjThreshold  TBYTE
	FPolicyType        TBYTE
	FType              TBYTE
	FReserved          TBYTE
	FTimerInterval     TDWORD
	F__ccgo_align12    [4]byte
	FTargetProcessors  TDWORD64
	FPStateHandler     TDWORD
	FPStateContext     TDWORD
	FTStateHandler     TDWORD
	FTStateContext     TDWORD
	FFeedbackHandler   TDWORD
	FReserved1         TDWORD
	FReserved2         TDWORD64
	FState             [1]TPPM_WMI_PERF_STATE
}

type TPPM_WMI_PERF_STATES_EX = struct {
	F__ccgo_align      [0]uint32
	FCount             TDWORD
	FMaxFrequency      TDWORD
	FCurrentState      TDWORD
	FMaxPerfState      TDWORD
	FMinPerfState      TDWORD
	FLowestPerfState   TDWORD
	FThermalConstraint TDWORD
	FBusyAdjThreshold  TBYTE
	FPolicyType        TBYTE
	FType              TBYTE
	FReserved          TBYTE
	FTimerInterval     TDWORD
	FTargetProcessors  TPVOID
	FPStateHandler     TDWORD
	FPStateContext     TDWORD
	FTStateHandler     TDWORD
	FTStateContext     TDWORD
	FFeedbackHandler   TDWORD
	FReserved1         TDWORD
	FReserved2         TDWORD64
	FState             [1]TPPM_WMI_PERF_STATE
}

type TPRINTER_NOTIFY_INFO_DATA = struct {
	FType       TWORD
	FField      TWORD
	FReserved   TDWORD
	FId         TDWORD
	FNotifyData struct {
		FData [0]struct {
			FcbBuf TDWORD
			FpBuf  TLPVOID
		}
		FadwData [2]TDWORD
	}
}

type TPROCESS_HEAP_ENTRY = struct {
	FlpData       TPVOID
	FcbData       TDWORD
	FcbOverhead   TBYTE
	FiRegionIndex TBYTE
	FwFlags       TWORD
	F__ccgo5_12   struct {
		FRegion [0]struct {
			FdwCommittedSize   TDWORD
			FdwUnCommittedSize TDWORD
			FlpFirstBlock      TLPVOID
			FlpLastBlock       TLPVOID
		}
		FBlock struct {
			FhMem       THANDLE
			FdwReserved [3]TDWORD
		}
	}
}

type TPROPSHEETHEADERA = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhwndParent THWND
	FhInstance  THINSTANCE
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCSTR
		FhIcon   THICON
	}
	FpszCaption TLPCSTR
	FnPages     TUINT
	F__ccgo7_28 struct {
		FpStartPage [0]TLPCSTR
		FnStartPage TUINT
	}
	F__ccgo8_32 struct {
		Fphpage [0]uintptr
		Fppsp   TLPCPROPSHEETPAGEA
	}
	FpfnCallback TPFNPROPSHEETCALLBACK
	F__ccgo10_40 struct {
		FpszbmWatermark [0]TLPCSTR
		FhbmWatermark   THBITMAP
	}
	FhplWatermark THPALETTE
	F__ccgo12_48  struct {
		FpszbmHeader [0]TLPCSTR
		FhbmHeader   THBITMAP
	}
}

type TPROPSHEETHEADERW = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhwndParent THWND
	FhInstance  THINSTANCE
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCWSTR
		FhIcon   THICON
	}
	FpszCaption TLPCWSTR
	FnPages     TUINT
	F__ccgo7_28 struct {
		FpStartPage [0]TLPCWSTR
		FnStartPage TUINT
	}
	F__ccgo8_32 struct {
		Fphpage [0]uintptr
		Fppsp   TLPCPROPSHEETPAGEW
	}
	FpfnCallback TPFNPROPSHEETCALLBACK
	F__ccgo10_40 struct {
		FpszbmWatermark [0]TLPCWSTR
		FhbmWatermark   THBITMAP
	}
	FhplWatermark THPALETTE
	F__ccgo12_48  struct {
		FpszbmHeader [0]TLPCWSTR
		FhbmHeader   THBITMAP
	}
}

type TPROPSHEETPAGEA = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKA
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCSTR
	FpszHeaderSubTitle TLPCSTR
	FhActCtx           THANDLE
}

type TPROPSHEETPAGEA_LATEST = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKA
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCSTR
	FpszHeaderSubTitle TLPCSTR
	FhActCtx           THANDLE
}

type TPROPSHEETPAGEA_V1 = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCSTR
		FhIcon   THICON
	}
	FpszTitle    TLPCSTR
	FpfnDlgProc  TDLGPROC
	FlParam      TLPARAM
	FpfnCallback TLPFNPSPCALLBACKA
	FpcRefParent uintptr
}

type TPROPSHEETPAGEA_V2 = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKA
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCSTR
	FpszHeaderSubTitle TLPCSTR
}

type TPROPSHEETPAGEA_V3 = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKA
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCSTR
	FpszHeaderSubTitle TLPCSTR
	FhActCtx           THANDLE
}

type TPROPSHEETPAGEW = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCWSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCWSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCWSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKW
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCWSTR
	FpszHeaderSubTitle TLPCWSTR
	FhActCtx           THANDLE
}

type TPROPSHEETPAGEW_LATEST = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCWSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCWSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCWSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKW
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCWSTR
	FpszHeaderSubTitle TLPCWSTR
	FhActCtx           THANDLE
}

type TPROPSHEETPAGEW_V1 = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCWSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCWSTR
		FhIcon   THICON
	}
	FpszTitle    TLPCWSTR
	FpfnDlgProc  TDLGPROC
	FlParam      TLPARAM
	FpfnCallback TLPFNPSPCALLBACKW
	FpcRefParent uintptr
}

type TPROPSHEETPAGEW_V2 = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCWSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCWSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCWSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKW
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCWSTR
	FpszHeaderSubTitle TLPCWSTR
}

type TPROPSHEETPAGEW_V3 = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCWSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCWSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCWSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKW
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCWSTR
	FpszHeaderSubTitle TLPCWSTR
	FhActCtx           THANDLE
}

type TPROPSPEC = struct {
	FulKind    TULONG
	F__ccgo1_4 struct {
		Flpwstr [0]TLPOLESTR
		Fpropid TPROPID
	}
}

type TPROPVARIANT = struct {
	F__ccgo_align [0]uint32
	F__ccgo0_0    struct {
		F__ccgo_align [0]uint32
		FdecVal       [0]TDECIMAL
		F__ccgo0_0    struct {
			F__ccgo_align [0]uint32
			Fvt           TVARTYPE
			FwReserved1   TPROPVAR_PAD1
			FwReserved2   TPROPVAR_PAD2
			FwReserved3   TPROPVAR_PAD3
			F__ccgo4_8    struct {
				F__ccgo_align     [0]uint32
				FbVal             [0]TUCHAR
				FiVal             [0]TSHORT
				FuiVal            [0]TUSHORT
				FlVal             [0]TLONG
				FulVal            [0]TULONG
				FintVal           [0]TINT
				FuintVal          [0]TUINT
				FhVal             [0]TLARGE_INTEGER
				FuhVal            [0]TULARGE_INTEGER
				FfltVal           [0]TFLOAT
				FdblVal           [0]TDOUBLE
				FboolVal          [0]TVARIANT_BOOL
				Fscode            [0]TSCODE
				FcyVal            [0]TCY
				Fdate             [0]TDATE
				Ffiletime         [0]TFILETIME
				Fpuuid            [0]uintptr
				Fpclipdata        [0]uintptr
				FbstrVal          [0]TBSTR
				FbstrblobVal      [0]TBSTRBLOB
				Fblob             [0]TBLOB
				FpszVal           [0]TLPSTR
				FpwszVal          [0]TLPWSTR
				FpunkVal          [0]uintptr
				FpdispVal         [0]uintptr
				FpStream          [0]uintptr
				FpStorage         [0]uintptr
				FpVersionedStream [0]TLPVERSIONEDSTREAM
				Fparray           [0]TLPSAFEARRAY
				Fcac              [0]TCAC
				Fcaub             [0]TCAUB
				Fcai              [0]TCAI
				Fcaui             [0]TCAUI
				Fcal              [0]TCAL
				Fcaul             [0]TCAUL
				Fcah              [0]TCAH
				Fcauh             [0]TCAUH
				Fcaflt            [0]TCAFLT
				Fcadbl            [0]TCADBL
				Fcabool           [0]TCABOOL
				Fcascode          [0]TCASCODE
				Fcacy             [0]TCACY
				Fcadate           [0]TCADATE
				Fcafiletime       [0]TCAFILETIME
				Fcauuid           [0]TCACLSID
				Fcaclipdata       [0]TCACLIPDATA
				Fcabstr           [0]TCABSTR
				Fcabstrblob       [0]TCABSTRBLOB
				Fcalpstr          [0]TCALPSTR
				Fcalpwstr         [0]TCALPWSTR
				Fcapropvar        [0]TCAPROPVARIANT
				FpcVal            [0]uintptr
				FpbVal            [0]uintptr
				FpiVal            [0]uintptr
				FpuiVal           [0]uintptr
				FplVal            [0]uintptr
				FpulVal           [0]uintptr
				FpintVal          [0]uintptr
				FpuintVal         [0]uintptr
				FpfltVal          [0]uintptr
				FpdblVal          [0]uintptr
				FpboolVal         [0]uintptr
				FpdecVal          [0]uintptr
				Fpscode           [0]uintptr
				FpcyVal           [0]uintptr
				Fpdate            [0]uintptr
				FpbstrVal         [0]uintptr
				FppunkVal         [0]uintptr
				FppdispVal        [0]uintptr
				Fpparray          [0]uintptr
				FpvarVal          [0]uintptr
				FcVal             TCHAR
				F__ccgo_pad72     [7]byte
			}
		}
	}
}

// C documentation
//
//	/*
//	** Each open file is managed by a separate instance of the "Pager" structure.
//	*/
type TPager = struct {
	F__ccgo_align      [0]uint32
	FpVfs              uintptr
	FexclusiveMode     Tu8
	FjournalMode       Tu8
	FuseJournal        Tu8
	FnoSync            Tu8
	FfullSync          Tu8
	FextraSync         Tu8
	FsyncFlags         Tu8
	FwalSyncFlags      Tu8
	FtempFile          Tu8
	FnoLock            Tu8
	FreadOnly          Tu8
	FmemDb             Tu8
	FmemVfs            Tu8
	FeState            Tu8
	FeLock             Tu8
	FchangeCountDone   Tu8
	FsetSuper          Tu8
	FdoNotSpill        Tu8
	FsubjInMemory      Tu8
	FbUseFetch         Tu8
	FhasHeldSharedLock Tu8
	FdbSize            TPgno
	FdbOrigSize        TPgno
	FdbFileSize        TPgno
	FdbHintSize        TPgno
	FerrCode           int32
	FnRec              int32
	FcksumInit         Tu32
	FnSubRec           Tu32
	FpInJournal        uintptr
	Ffd                uintptr
	Fjfd               uintptr
	Fsjfd              uintptr
	F__ccgo_align34    [4]byte
	FjournalOff        Ti64
	FjournalHdr        Ti64
	FpBackup           uintptr
	FaSavepoint        uintptr
	FnSavepoint        int32
	FiDataVersion      Tu32
	FdbFileVers        [16]int8
	FnMmapOut          int32
	F__ccgo_align42    [4]byte
	FszMmap            Tsqlite3_int64
	FpMmapFreelist     uintptr
	FnExtra            Tu16
	FnReserve          Ti16
	FvfsFlags          Tu32
	FsectorSize        Tu32
	FmxPgno            TPgno
	FlckPgno           TPgno
	FpageSize          Ti64
	FjournalSizeLimit  Ti64
	FzFilename         uintptr
	FzJournal          uintptr
	FxBusyHandler      uintptr
	FpBusyHandlerArg   uintptr
	FaStat             [4]Tu32
	FxReiniter         uintptr
	FxGet              uintptr
	FpTmpSpace         uintptr
	FpPCache           uintptr
	FpWal              uintptr
	FzWal              uintptr
}

type TQUERY_CHANGES_VIRTUAL_DISK_RANGE = struct {
	F__ccgo_align [0]uint32
	FByteOffset   TULONG64
	FByteLength   TULONG64
	FReserved     TULONG64
}

type TQUOTA_LIMITS = struct {
	F__ccgo_align          [0]uint32
	FPagedPoolLimit        TSIZE_T
	FNonPagedPoolLimit     TSIZE_T
	FMinimumWorkingSetSize TSIZE_T
	FMaximumWorkingSetSize TSIZE_T
	FPagefileLimit         TSIZE_T
	F__ccgo_align5         [4]byte
	FTimeLimit             TLARGE_INTEGER
}

type TQUOTA_LIMITS_EX = struct {
	F__ccgo_align          [0]uint32
	FPagedPoolLimit        TSIZE_T
	FNonPagedPoolLimit     TSIZE_T
	FMinimumWorkingSetSize TSIZE_T
	FMaximumWorkingSetSize TSIZE_T
	FPagefileLimit         TSIZE_T
	F__ccgo_align5         [4]byte
	FTimeLimit             TLARGE_INTEGER
	FWorkingSetLimit       TSIZE_T
	FReserved2             TSIZE_T
	FReserved3             TSIZE_T
	FReserved4             TSIZE_T
	FFlags                 TDWORD
	FCpuRateLimit          TRATE_QUOTA_LIMIT
}

type TREAD_USN_JOURNAL_DATA = struct {
	F__ccgo_align      [0]uint32
	FStartUsn          TUSN
	FReasonMask        TDWORD
	FReturnOnlyOnClose TDWORD
	FTimeout           TDWORDLONG
	FBytesToWaitFor    TDWORDLONG
	FUsnJournalID      TDWORDLONG
}

type TREASSIGN_BLOCKS_EX = struct {
	F__ccgo_align  [0]uint32
	FReserved      TWORD
	FCount         TWORD
	F__ccgo_align2 [4]byte
	FBlockNumber   [1]TLARGE_INTEGER
}

type TREQUEST_RAW_ENCRYPTED_DATA = struct {
	F__ccgo_align [0]uint32
	FFileOffset   TLONGLONG
	FLength       TDWORD
	F__ccgo_pad2  [4]byte
}

type TRESIZE_VIRTUAL_DISK_PARAMETERS = struct {
	F__ccgo_align  [0]uint32
	FVersion       TRESIZE_VIRTUAL_DISK_VERSION
	F__ccgo_align1 [4]byte
	F__ccgo1_8     struct {
		F__ccgo_align [0]uint32
		FVersion1     struct {
			F__ccgo_align [0]uint32
			FNewSize      TULONGLONG
		}
	}
}

type TRESUME_PERFORMANCE = struct {
	F__ccgo_align            [0]uint32
	FPostTimeMs              TDWORD
	F__ccgo_align1           [4]byte
	FTotalResumeTimeMs       TULONGLONG
	FResumeCompleteTimestamp TULONGLONG
}

type TRETRIEVAL_POINTERS_BUFFER = struct {
	F__ccgo_align  [0]uint32
	FExtentCount   TDWORD
	F__ccgo_align1 [4]byte
	FStartingVcn   TLARGE_INTEGER
	FExtents       [1]struct {
		F__ccgo_align [0]uint32
		FNextVcn      TLARGE_INTEGER
		FLcn          TLARGE_INTEGER
	}
}

type TRETRIEVAL_POINTER_BASE = struct {
	F__ccgo_align   [0]uint32
	FFileAreaOffset TLARGE_INTEGER
}

type TRPC_ASYNC_NOTIFICATION_INFO = struct {
	FIOC [0]struct {
		FhIOPort                    THANDLE
		FdwNumberOfBytesTransferred TDWORD
		FdwCompletionKey            TDWORD_PTR
		FlpOverlapped               TLPOVERLAPPED
	}
	FHWND [0]struct {
		FhWnd THWND
		FMsg  TUINT
	}
	FhEvent              [0]THANDLE
	FNotificationRoutine [0]TPFN_RPCNOTIFICATION_ROUTINE
	FAPC                 struct {
		FNotificationRoutine TPFN_RPCNOTIFICATION_ROUTINE
		FhThread             THANDLE
	}
	F__ccgo_pad5 [8]byte
}

type TRPC_ASYNC_STATE = struct {
	FSize             uint32
	FSignature        uint32
	FLock             int32
	FFlags            uint32
	FStubInfo         uintptr
	FUserInfo         uintptr
	FRuntimeInfo      uintptr
	FEvent            TRPC_ASYNC_EVENT
	FNotificationType TRPC_NOTIFICATION_TYPES
	Fu                struct {
		FIOC [0]struct {
			FhIOPort                    THANDLE
			FdwNumberOfBytesTransferred TDWORD
			FdwCompletionKey            TDWORD_PTR
			FlpOverlapped               TLPOVERLAPPED
		}
		FHWND [0]struct {
			FhWnd THWND
			FMsg  TUINT
		}
		FhEvent              [0]THANDLE
		FNotificationRoutine [0]TPFN_RPCNOTIFICATION_ROUTINE
		FAPC                 struct {
			FNotificationRoutine TPFN_RPCNOTIFICATION_ROUTINE
			FhThread             THANDLE
		}
		F__ccgo_pad5 [8]byte
	}
	FReserved [4]TLONG_PTR
}

type TRPC_EE_INFO_PARAM = struct {
	F__ccgo_align  [0]uint32
	FParameterType TExtendedErrorParamTypes
	F__ccgo_align1 [4]byte
	Fu             struct {
		F__ccgo_align  [0]uint32
		FUnicodeString [0]TLPWSTR
		FLVal          [0]int32
		FSVal          [0]int16
		FPVal          [0]TULONGLONG
		FBVal          [0]TBinaryParam
		FAnsiString    TLPSTR
		F__ccgo_pad6   [4]byte
	}
}

type TRPC_EXTENDED_ERROR_INFO = struct {
	F__ccgo_align [0]uint32
	FVersion      TULONG
	FComputerName TLPWSTR
	FProcessID    TULONG
	Fu            struct {
		FFileTime   [0]TFILETIME
		FSystemTime TSYSTEMTIME
	}
	FGeneratingComponent TULONG
	FStatus              TULONG
	FDetectionLocation   TUSHORT
	FFlags               TUSHORT
	FNumberOfParameters  int32
	F__ccgo_align9       [4]byte
	FParameters          [4]TRPC_EE_INFO_PARAM
}

type TSCARDCONTEXT = uint32

type TSCARDHANDLE = uint32

type TSCRUB_DATA_OUTPUT = struct {
	F__ccgo_align          [0]uint32
	FSize                  TDWORD
	FFlags                 TDWORD
	FStatus                TDWORD
	F__ccgo_align3         [4]byte
	FErrorFileOffset       TULONGLONG
	FErrorLength           TULONGLONG
	FNumberOfBytesRepaired TULONGLONG
	FNumberOfBytesFailed   TULONGLONG
	FInternalFileReference TULONGLONG
	FReserved              [6]TDWORD
	FResumeContext         [816]TBYTE
}

type TSD_CHANGE_MACHINE_SID_OUTPUT = struct {
	F__ccgo_align           [0]uint32
	FNumSDChangedSuccess    TULONGLONG
	FNumSDChangedFail       TULONGLONG
	FNumSDUnused            TULONGLONG
	FNumSDTotal             TULONGLONG
	FNumMftSDChangedSuccess TULONGLONG
	FNumMftSDChangedFail    TULONGLONG
	FNumMftSDTotal          TULONGLONG
}

type TSERVENT = struct {
	Fs_name    uintptr
	Fs_aliases uintptr
	Fs_port    int16
	Fs_proto   uintptr
}

type TSET_PARTITION_INFORMATION_EX = struct {
	F__ccgo_align   [0]uint32
	FPartitionStyle TPARTITION_STYLE
	F__ccgo_align1  [4]byte
	F__ccgo1_8      struct {
		F__ccgo_align [0]uint32
		FGpt          [0]TSET_PARTITION_INFORMATION_GPT
		FMbr          TSET_PARTITION_INFORMATION_MBR
		F__ccgo_pad2  [111]byte
	}
}

type TSET_PARTITION_INFORMATION_GPT = struct {
	F__ccgo_align  [0]uint32
	FPartitionType TGUID
	FPartitionId   TGUID
	FAttributes    TDWORD64
	FName          [36]TWCHAR
}

type TSET_VIRTUAL_DISK_INFO = struct {
	FVersion   TSET_VIRTUAL_DISK_INFO_VERSION
	F__ccgo1_4 struct {
		FUniqueIdentifier        [0]TGUID
		FParentPathWithDepthInfo [0]struct {
			FChildDepth     TULONG
			FParentFilePath TPCWSTR
		}
		FVhdPhysicalSectorSize [0]TULONG
		FVirtualDiskId         [0]TGUID
		FChangeTrackingEnabled [0]TWINBOOL
		FParentLocator         [0]struct {
			FLinkageId      TGUID
			FParentFilePath TPCWSTR
		}
		FParentFilePath TPCWSTR
		F__ccgo_pad7    [16]byte
	}
}

type TSHANDLE_PTR = int32

type TSHELLEXECUTEINFO = struct {
	FcbSize       TDWORD
	FfMask        TULONG
	Fhwnd         THWND
	FlpVerb       TLPCSTR
	FlpFile       TLPCSTR
	FlpParameters TLPCSTR
	FlpDirectory  TLPCSTR
	FnShow        int32
	FhInstApp     THINSTANCE
	FlpIDList     uintptr
	FlpClass      TLPCSTR
	FhkeyClass    THKEY
	FdwHotKey     TDWORD
	F__ccgo13_52  struct {
		FhMonitor [0]THANDLE
		FhIcon    THANDLE
	}
	FhProcess THANDLE
}

type TSHELLEXECUTEINFOA = struct {
	FcbSize       TDWORD
	FfMask        TULONG
	Fhwnd         THWND
	FlpVerb       TLPCSTR
	FlpFile       TLPCSTR
	FlpParameters TLPCSTR
	FlpDirectory  TLPCSTR
	FnShow        int32
	FhInstApp     THINSTANCE
	FlpIDList     uintptr
	FlpClass      TLPCSTR
	FhkeyClass    THKEY
	FdwHotKey     TDWORD
	F__ccgo13_52  struct {
		FhMonitor [0]THANDLE
		FhIcon    THANDLE
	}
	FhProcess THANDLE
}

type TSHELLEXECUTEINFOW = struct {
	FcbSize       TDWORD
	FfMask        TULONG
	Fhwnd         THWND
	FlpVerb       TLPCWSTR
	FlpFile       TLPCWSTR
	FlpParameters TLPCWSTR
	FlpDirectory  TLPCWSTR
	FnShow        int32
	FhInstApp     THINSTANCE
	FlpIDList     uintptr
	FlpClass      TLPCWSTR
	FhkeyClass    THKEY
	FdwHotKey     TDWORD
	F__ccgo13_52  struct {
		FhMonitor [0]THANDLE
		FhIcon    THANDLE
	}
	FhProcess THANDLE
}

type TSHQUERYRBINFO = struct {
	F__ccgo_align  [0]uint32
	FcbSize        TDWORD
	F__ccgo_align1 [4]byte
	Fi64Size       int64
	Fi64NumItems   int64
}

type TSHRINK_VOLUME_INFORMATION = struct {
	F__ccgo_align       [0]uint32
	FShrinkRequestType  TSHRINK_VOLUME_REQUEST_TYPES
	F__ccgo_align1      [4]byte
	FFlags              TDWORDLONG
	FNewNumberOfSectors TLONGLONG
}

type TSID_HASH_ENTRY = uint32

type TSIZE_T = uint32

type TSLIST_HEADER = struct {
	F__ccgo_align [0]uint32
	F__ccgo1_0    [0]struct {
		FNext     TSLIST_ENTRY
		FDepth    TWORD
		FSequence TWORD
	}
	FAlignment TULONGLONG
}

type TSOCKET = uint32

type TSSIZE_T = int32

type TSTARTING_LCN_INPUT_BUFFER = struct {
	F__ccgo_align [0]uint32
	FStartingLcn  TLARGE_INTEGER
}

type TSTARTING_VCN_INPUT_BUFFER = struct {
	F__ccgo_align [0]uint32
	FStartingVcn  TLARGE_INTEGER
}

type TSTATSTG = struct {
	F__ccgo_align      [0]uint32
	FpwcsName          TLPOLESTR
	Ftype1             TDWORD
	FcbSize            TULARGE_INTEGER
	Fmtime             TFILETIME
	Fctime             TFILETIME
	Fatime             TFILETIME
	FgrfMode           TDWORD
	FgrfLocksSupported TDWORD
	Fclsid             TCLSID
	FgrfStateBits      TDWORD
	Freserved          TDWORD
}

type TSTGMEDIUM = struct {
	Ftymed     TDWORD
	F__ccgo1_4 struct {
		FhMetaFilePict [0]THMETAFILEPICT
		FhEnhMetaFile  [0]THENHMETAFILE
		FhGlobal       [0]THGLOBAL
		FlpszFileName  [0]TLPOLESTR
		Fpstm          [0]uintptr
		Fpstg          [0]uintptr
		FhBitmap       THBITMAP
	}
	FpUnkForRelease uintptr
}

type TSTORAGE_ALLOCATE_BC_STREAM_OUTPUT = struct {
	F__ccgo_align           [0]uint32
	FRequestSize            TULONGLONG
	FNumOutStandingRequests TULONG
	F__ccgo_pad2            [4]byte
}

type TSTORAGE_DEPENDENCY_INFO = struct {
	FVersion       TSTORAGE_DEPENDENCY_INFO_VERSION
	FNumberEntries TULONG
	F__ccgo2_8     struct {
		FVersion2Entries [0][1]TSTORAGE_DEPENDENCY_INFO_TYPE_2
		FVersion1Entries [1]TSTORAGE_DEPENDENCY_INFO_TYPE_1
		F__ccgo_pad2     [20]byte
	}
}

type TSTORAGE_DEVICE_TIERING_DESCRIPTOR = struct {
	F__ccgo_align          [0]uint32
	FVersion               TDWORD
	FSize                  TDWORD
	FFlags                 TDWORD
	FTotalNumberOfTiers    TDWORD
	FNumberOfTiersReturned TDWORD
	F__ccgo_align5         [4]byte
	FTiers                 [1]TSTORAGE_TIER
}

type TSTORAGE_GET_BC_PROPERTIES_OUTPUT = struct {
	F__ccgo_align             [0]uint32
	FMaximumRequestsPerPeriod TULONG
	FMinimumPeriod            TULONG
	FMaximumRequestSize       TULONGLONG
	FEstimatedTimePerRequest  TULONG
	FNumOutStandingRequests   TULONG
	FRequestSize              TULONGLONG
}

type TSTORAGE_LB_PROVISIONING_MAP_RESOURCES = struct {
	F__ccgo_align              [0]uint32
	FSize                      TDWORD
	FVersion                   TDWORD
	F__ccgo8                   uint8
	FReserved1                 [3]TBYTE
	F__ccgo12                  uint8
	FReserved3                 [3]TBYTE
	FAvailableMappingResources TDWORDLONG
	FUsedMappingResources      TDWORDLONG
}

type TSTORAGE_READ_CAPACITY = struct {
	F__ccgo_align   [0]uint32
	FVersion        TULONG
	FSize           TULONG
	FBlockLength    TULONG
	F__ccgo_align3  [4]byte
	FNumberOfBlocks TLARGE_INTEGER
	FDiskLength     TLARGE_INTEGER
}

type TSTORAGE_TIER = struct {
	F__ccgo_align        [0]uint32
	FId                  TGUID
	FName                [256]TWCHAR
	FDescription         [256]TWCHAR
	FFlags               TDWORDLONG
	FProvisionedCapacity TDWORDLONG
	FMediaType           TSTORAGE_TIER_MEDIA_TYPE
	FClass               TSTORAGE_TIER_CLASS
}

type TSYSTEM_LOGICAL_PROCESSOR_INFORMATION = struct {
	F__ccgo_align  [0]uint32
	FProcessorMask TULONG_PTR
	FRelationship  TLOGICAL_PROCESSOR_RELATIONSHIP
	F__ccgo2_8     struct {
		F__ccgo_align [0]uint32
		FNumaNode     [0]struct {
			FNodeNumber TDWORD
		}
		FCache         [0]TCACHE_DESCRIPTOR
		FReserved      [0][2]TULONGLONG
		FProcessorCore struct {
			FFlags TBYTE
		}
		F__ccgo_pad4 [15]byte
	}
}

type TSYSTEM_PROCESSOR_CYCLE_TIME_INFORMATION = struct {
	F__ccgo_align [0]uint32
	FCycleTime    TDWORD64
}

type TServerInformation = struct {
	F__ccgo_align      [0]uint32
	FdwServerPid       TDWORD
	FdwServerTid       TDWORD
	Fui64ServerAddress TUINT64
}

type TStorageLayout = struct {
	F__ccgo_align    [0]uint32
	FLayoutType      TDWORD
	FpwcsElementName uintptr
	FcOffset         TLARGE_INTEGER
	FcBytes          TLARGE_INTEGER
}

type TTAPE_GET_MEDIA_PARAMETERS = struct {
	F__ccgo_align   [0]uint32
	FCapacity       TLARGE_INTEGER
	FRemaining      TLARGE_INTEGER
	FBlockSize      TDWORD
	FPartitionCount TDWORD
	FWriteProtected TBOOLEAN
	F__ccgo_pad5    [7]byte
}

type TTAPE_GET_POSITION = struct {
	F__ccgo_align [0]uint32
	FType         TDWORD
	FPartition    TDWORD
	FOffset       TLARGE_INTEGER
}

type TTAPE_SET_POSITION = struct {
	F__ccgo_align [0]uint32
	FMethod       TDWORD
	FPartition    TDWORD
	FOffset       TLARGE_INTEGER
	FImmediate    TBOOLEAN
	F__ccgo_pad4  [7]byte
}

type TTAPE_STATISTICS = struct {
	F__ccgo_align           [0]uint32
	FVersion                TDWORD
	FFlags                  TDWORD
	FRecoveredWrites        TLARGE_INTEGER
	FUnrecoveredWrites      TLARGE_INTEGER
	FRecoveredReads         TLARGE_INTEGER
	FUnrecoveredReads       TLARGE_INTEGER
	FCompressionRatioReads  TBYTE
	FCompressionRatioWrites TBYTE
	F__ccgo_pad8            [6]byte
}

type TTOKEN_STATISTICS = struct {
	F__ccgo_align       [0]uint32
	FTokenId            TLUID
	FAuthenticationId   TLUID
	FExpirationTime     TLARGE_INTEGER
	FTokenType          TTOKEN_TYPE
	FImpersonationLevel TSECURITY_IMPERSONATION_LEVEL
	FDynamicCharged     TDWORD
	FDynamicAvailable   TDWORD
	FGroupCount         TDWORD
	FPrivilegeCount     TDWORD
	FModifiedId         TLUID
}

type TTRANSACTIONMANAGER_BASIC_INFORMATION = struct {
	F__ccgo_align [0]uint32
	FTmIdentity   TGUID
	FVirtualClock TLARGE_INTEGER
}

type TTRANSACTIONMANAGER_RECOVERY_INFORMATION = struct {
	F__ccgo_align     [0]uint32
	FLastRecoveredLsn TULONGLONG
}

type TTRANSACTION_NOTIFICATION = struct {
	F__ccgo_align            [0]uint32
	FTransactionKey          TPVOID
	FTransactionNotification TULONG
	FTmVirtualClock          TLARGE_INTEGER
	FArgumentLength          TULONG
	F__ccgo_pad4             [4]byte
}

type TTRANSACTION_PROPERTIES_INFORMATION = struct {
	F__ccgo_align      [0]uint32
	FIsolationLevel    TDWORD
	FIsolationFlags    TDWORD
	FTimeout           TLARGE_INTEGER
	FOutcome           TDWORD
	FDescriptionLength TDWORD
	FDescription       [1]TWCHAR
	F__ccgo_pad6       [6]byte
}

type TTXFS_GET_METADATA_INFO_OUT = struct {
	F__ccgo_align [0]uint32
	FTxfFileId    struct {
		F__ccgo_align [0]uint32
		FLowPart      TLONGLONG
		FHighPart     TLONGLONG
	}
	FLockingTransaction TGUID
	FLastLsn            TULONGLONG
	FTransactionState   TULONG
	F__ccgo_pad4        [4]byte
}

type TTXFS_LIST_TRANSACTIONS = struct {
	F__ccgo_align         [0]uint32
	FNumberOfTransactions TULONGLONG
	FBufferSizeRequired   TULONGLONG
}

type TTXFS_LIST_TRANSACTIONS_ENTRY = struct {
	F__ccgo_align     [0]uint32
	FTransactionId    TGUID
	FTransactionState TULONG
	FReserved1        TULONG
	FReserved2        TULONG
	F__ccgo_align4    [4]byte
	FReserved3        TLONGLONG
}

type TTXFS_LIST_TRANSACTION_LOCKED_FILES = struct {
	F__ccgo_align       [0]uint32
	FKtmTransaction     TGUID
	FNumberOfFiles      TULONGLONG
	FBufferSizeRequired TULONGLONG
	FOffset             TULONGLONG
}

type TTXFS_LIST_TRANSACTION_LOCKED_FILES_ENTRY = struct {
	F__ccgo_align  [0]uint32
	FOffset        TULONGLONG
	FNameFlags     TULONG
	F__ccgo_align2 [4]byte
	FFileId        TLONGLONG
	FReserved1     TULONG
	FReserved2     TULONG
	FReserved3     TLONGLONG
	FFileName      [1]TWCHAR
	F__ccgo_pad7   [6]byte
}

type TTXFS_MODIFY_RM = struct {
	F__ccgo_align            [0]uint32
	FFlags                   TULONG
	FLogContainerCountMax    TULONG
	FLogContainerCountMin    TULONG
	FLogContainerCount       TULONG
	FLogGrowthIncrement      TULONG
	FLogAutoShrinkPercentage TULONG
	FReserved                TULONGLONG
	FLoggingMode             TUSHORT
	F__ccgo_pad8             [6]byte
}

type TTXFS_QUERY_RM_INFORMATION = struct {
	F__ccgo_align            [0]uint32
	FBytesRequired           TULONG
	F__ccgo_align1           [4]byte
	FTailLsn                 TULONGLONG
	FCurrentLsn              TULONGLONG
	FArchiveTailLsn          TULONGLONG
	FLogContainerSize        TULONGLONG
	FHighestVirtualClock     TLARGE_INTEGER
	FLogContainerCount       TULONG
	FLogContainerCountMax    TULONG
	FLogContainerCountMin    TULONG
	FLogGrowthIncrement      TULONG
	FLogAutoShrinkPercentage TULONG
	FFlags                   TULONG
	FLoggingMode             TUSHORT
	FReserved                TUSHORT
	FRmState                 TULONG
	FLogCapacity             TULONGLONG
	FLogFree                 TULONGLONG
	FTopsSize                TULONGLONG
	FTopsUsed                TULONGLONG
	FTransactionCount        TULONGLONG
	FOnePCCount              TULONGLONG
	FTwoPCCount              TULONGLONG
	FNumberLogFileFull       TULONGLONG
	FOldestTransactionAge    TULONGLONG
	FRMName                  TGUID
	FTmLogPathOffset         TULONG
	F__ccgo_pad26            [4]byte
}

type TTXFS_ROLLFORWARD_REDO_INFORMATION = struct {
	F__ccgo_align       [0]uint32
	FLastVirtualClock   TLARGE_INTEGER
	FLastRedoLsn        TULONGLONG
	FHighestRecoveryLsn TULONGLONG
	FFlags              TULONG
	F__ccgo_pad4        [4]byte
}

type TTXFS_START_RM_INFORMATION = struct {
	F__ccgo_align            [0]uint32
	FFlags                   TULONG
	F__ccgo_align1           [4]byte
	FLogContainerSize        TULONGLONG
	FLogContainerCountMin    TULONG
	FLogContainerCountMax    TULONG
	FLogGrowthIncrement      TULONG
	FLogAutoShrinkPercentage TULONG
	FTmLogPathOffset         TULONG
	FTmLogPathLength         TUSHORT
	FLoggingMode             TUSHORT
	FLogPathLength           TUSHORT
	FReserved                TUSHORT
	FLogPath                 [1]TWCHAR
	F__ccgo_pad12            [2]byte
}

type TUHALF_PTR = uint16

type TUINT_PTR = uint32

type TULARGE_INTEGER = struct {
	F__ccgo_align [0]uint32
	Fu            [0]struct {
		FLowPart  TDWORD
		FHighPart TDWORD
	}
	FQuadPart  [0]TULONGLONG
	F__ccgo0_0 struct {
		FLowPart  TDWORD
		FHighPart TDWORD
	}
}

type TULONG_PTR = uint32

type TUSN_JOURNAL_DATA = struct {
	F__ccgo_align    [0]uint32
	FUsnJournalID    TDWORDLONG
	FFirstUsn        TUSN
	FNextUsn         TUSN
	FLowestValidUsn  TUSN
	FMaxUsn          TUSN
	FMaximumSize     TDWORDLONG
	FAllocationDelta TDWORDLONG
}

type TUSN_RECORD = struct {
	F__ccgo_align              [0]uint32
	FRecordLength              TDWORD
	FMajorVersion              TWORD
	FMinorVersion              TWORD
	FFileReferenceNumber       TDWORDLONG
	FParentFileReferenceNumber TDWORDLONG
	FUsn                       TUSN
	FTimeStamp                 TLARGE_INTEGER
	FReason                    TDWORD
	FSourceInfo                TDWORD
	FSecurityId                TDWORD
	FFileAttributes            TDWORD
	FFileNameLength            TWORD
	FFileNameOffset            TWORD
	FFileName                  [1]TWCHAR
	F__ccgo_pad14              [2]byte
}

type TVARDESC = struct {
	Fmemid       TMEMBERID
	FlpstrSchema TLPOLESTR
	F__ccgo2_8   struct {
		FlpvarValue [0]uintptr
		FoInst      TULONG
	}
	FelemdescVar TELEMDESC
	FwVarFlags   TWORD
	Fvarkind     TVARKIND
}

type TVARIANT = struct {
	F__ccgo_align [0]uint32
	F__ccgo0_0    struct {
		F__ccgo_align [0]uint32
		FdecVal       [0]TDECIMAL
		F__ccgo0_0    struct {
			F__ccgo_align [0]uint32
			Fvt           TVARTYPE
			FwReserved1   TWORD
			FwReserved2   TWORD
			FwReserved3   TWORD
			F__ccgo4_8    struct {
				F__ccgo_align [0]uint32
				FlVal         [0]TLONG
				FbVal         [0]TBYTE
				FiVal         [0]TSHORT
				FfltVal       [0]TFLOAT
				FdblVal       [0]TDOUBLE
				FboolVal      [0]TVARIANT_BOOL
				Fscode        [0]TSCODE
				FcyVal        [0]TCY
				Fdate         [0]TDATE
				FbstrVal      [0]TBSTR
				FpunkVal      [0]uintptr
				FpdispVal     [0]uintptr
				Fparray       [0]uintptr
				FpbVal        [0]uintptr
				FpiVal        [0]uintptr
				FplVal        [0]uintptr
				FpllVal       [0]uintptr
				FpfltVal      [0]uintptr
				FpdblVal      [0]uintptr
				FpboolVal     [0]uintptr
				Fpscode       [0]uintptr
				FpcyVal       [0]uintptr
				Fpdate        [0]uintptr
				FpbstrVal     [0]uintptr
				FppunkVal     [0]uintptr
				FppdispVal    [0]uintptr
				Fpparray      [0]uintptr
				FpvarVal      [0]uintptr
				Fbyref        [0]TPVOID
				FcVal         [0]TCHAR
				FuiVal        [0]TUSHORT
				FulVal        [0]TULONG
				FullVal       [0]TULONGLONG
				FintVal       [0]TINT
				FuintVal      [0]TUINT
				FpdecVal      [0]uintptr
				FpcVal        [0]uintptr
				FpuiVal       [0]uintptr
				FpulVal       [0]uintptr
				FpullVal      [0]uintptr
				FpintVal      [0]uintptr
				FpuintVal     [0]uintptr
				F__ccgo43_0   [0]struct {
					FpvRecord TPVOID
					FpRecInfo uintptr
				}
				FllVal TLONGLONG
			}
		}
	}
}

type TVARIANTARG = struct {
	F__ccgo_align [0]uint32
	F__ccgo0_0    struct {
		F__ccgo_align [0]uint32
		FdecVal       [0]TDECIMAL
		F__ccgo0_0    struct {
			F__ccgo_align [0]uint32
			Fvt           TVARTYPE
			FwReserved1   TWORD
			FwReserved2   TWORD
			FwReserved3   TWORD
			F__ccgo4_8    struct {
				F__ccgo_align [0]uint32
				FlVal         [0]TLONG
				FbVal         [0]TBYTE
				FiVal         [0]TSHORT
				FfltVal       [0]TFLOAT
				FdblVal       [0]TDOUBLE
				FboolVal      [0]TVARIANT_BOOL
				Fscode        [0]TSCODE
				FcyVal        [0]TCY
				Fdate         [0]TDATE
				FbstrVal      [0]TBSTR
				FpunkVal      [0]uintptr
				FpdispVal     [0]uintptr
				Fparray       [0]uintptr
				FpbVal        [0]uintptr
				FpiVal        [0]uintptr
				FplVal        [0]uintptr
				FpllVal       [0]uintptr
				FpfltVal      [0]uintptr
				FpdblVal      [0]uintptr
				FpboolVal     [0]uintptr
				Fpscode       [0]uintptr
				FpcyVal       [0]uintptr
				Fpdate        [0]uintptr
				FpbstrVal     [0]uintptr
				FppunkVal     [0]uintptr
				FppdispVal    [0]uintptr
				Fpparray      [0]uintptr
				FpvarVal      [0]uintptr
				Fbyref        [0]TPVOID
				FcVal         [0]TCHAR
				FuiVal        [0]TUSHORT
				FulVal        [0]TULONG
				FullVal       [0]TULONGLONG
				FintVal       [0]TINT
				FuintVal      [0]TUINT
				FpdecVal      [0]uintptr
				FpcVal        [0]uintptr
				FpuiVal       [0]uintptr
				FpulVal       [0]uintptr
				FpullVal      [0]uintptr
				FpintVal      [0]uintptr
				FpuintVal     [0]uintptr
				F__ccgo43_0   [0]struct {
					FpvRecord TPVOID
					FpRecInfo uintptr
				}
				FllVal TLONGLONG
			}
		}
	}
}

type TVERIFY_INFORMATION = struct {
	F__ccgo_align   [0]uint32
	FStartingOffset TLARGE_INTEGER
	FLength         TDWORD
	F__ccgo_pad2    [4]byte
}

type TVIRTUAL_DISK_PROGRESS = struct {
	F__ccgo_align    [0]uint32
	FOperationStatus TDWORD
	F__ccgo_align1   [4]byte
	FCurrentValue    TULONGLONG
	FCompletionValue TULONGLONG
}

type TVOLUME_BITMAP_BUFFER = struct {
	F__ccgo_align [0]uint32
	FStartingLcn  TLARGE_INTEGER
	FBitmapSize   TLARGE_INTEGER
	FBuffer       [1]TBYTE
	F__ccgo_pad3  [7]byte
}

type TVOLUME_DISK_EXTENTS = struct {
	F__ccgo_align        [0]uint32
	FNumberOfDiskExtents TDWORD
	F__ccgo_align1       [4]byte
	FExtents             [1]TDISK_EXTENT
}

type TVOLUME_GET_GPT_ATTRIBUTES_INFORMATION = struct {
	F__ccgo_align  [0]uint32
	FGptAttributes TULONGLONG
}

type TWIN32_FIND_STREAM_DATA = struct {
	F__ccgo_align [0]uint32
	FStreamSize   TLARGE_INTEGER
	FcStreamName  [296]TWCHAR
}

type TWIN32_STREAM_ID = struct {
	F__ccgo_align       [0]uint32
	FdwStreamId         TDWORD
	FdwStreamAttributes TDWORD
	FSize               TLARGE_INTEGER
	FdwStreamNameSize   TDWORD
	FcStreamName        [1]TWCHAR
	F__ccgo_pad5        [2]byte
}

type TWPARAM = uint32

type TWSADATA = struct {
	FwVersion       TWORD
	FwHighVersion   TWORD
	FszDescription  [257]int8
	FszSystemStatus [129]int8
	FiMaxSockets    uint16
	FiMaxUdpDg      uint16
	FlpVendorInfo   uintptr
}

type TXSAVE_AREA = struct {
	F__ccgo_align [0]uint32
	FLegacyState  TXSAVE_FORMAT
	FHeader       TXSAVE_AREA_HEADER
}

type TXSAVE_AREA_HEADER = struct {
	F__ccgo_align [0]uint32
	FMask         TDWORD64
	FReserved     [7]TDWORD64
}

type TXSAVE_FORMAT = struct {
	F__ccgo_align   [0]uint32
	FControlWord    TWORD
	FStatusWord     TWORD
	FTagWord        TBYTE
	FReserved1      TBYTE
	FErrorOpcode    TWORD
	FErrorOffset    TDWORD
	FErrorSelector  TWORD
	FReserved2      TWORD
	FDataOffset     TDWORD
	FDataSelector   TWORD
	FReserved3      TWORD
	FMxCsr          TDWORD
	FMxCsr_Mask     TDWORD
	FFloatRegisters [8]TM128A
	FXmmRegisters   [8]TM128A
	FReserved4      [220]TBYTE
	FCr0NpxState    TDWORD
}

type TXSTATE_CONFIGURATION = struct {
	F__ccgo_align            [0]uint32
	FEnabledFeatures         TDWORD64
	FEnabledVolatileFeatures TDWORD64
	FSize                    TDWORD
	F__ccgo20                uint8
	FFeatures                [64]TXSTATE_FEATURE
}

type TXSTATE_CONTEXT = struct {
	F__ccgo_align [0]uint32
	FMask         TDWORD64
	FLength       TDWORD
	FReserved1    TDWORD
	FArea         TPXSAVE_AREA
	FReserved2    TDWORD
	FBuffer       TPVOID
	FReserved3    TDWORD
}

type T_CONTEXT = struct {
	FContextFlags      TDWORD
	FDr0               TDWORD
	FDr1               TDWORD
	FDr2               TDWORD
	FDr3               TDWORD
	FDr6               TDWORD
	FDr7               TDWORD
	FFloatSave         TFLOATING_SAVE_AREA
	FSegGs             TDWORD
	FSegFs             TDWORD
	FSegEs             TDWORD
	FSegDs             TDWORD
	FEdi               TDWORD
	FEsi               TDWORD
	FEbx               TDWORD
	FEdx               TDWORD
	FEcx               TDWORD
	FEax               TDWORD
	FEbp               TDWORD
	FEip               TDWORD
	FSegCs             TDWORD
	FEFlags            TDWORD
	FEsp               TDWORD
	FSegSs             TDWORD
	FExtendedRegisters [512]TBYTE
}

type T_CRT_DOUBLE = struct {
	F__ccgo_align [0]uint32
	Fx            float64
}

type T_FLOATING_SAVE_AREA = TFLOATING_SAVE_AREA

type T_LONGDOUBLE = struct {
	F__ccgo_align [0]uint32
	Fx            float64
}

type T_MIDL_STUB_MESSAGE = struct {
	FRpcMsg                 TPRPC_MESSAGE
	FBuffer                 uintptr
	FBufferStart            uintptr
	FBufferEnd              uintptr
	FBufferMark             uintptr
	FBufferLength           uint32
	FMemorySize             uint32
	FMemory                 uintptr
	FIsClient               uint8
	FPad                    uint8
	FuFlags2                uint16
	FReuseBuffer            int32
	FpAllocAllNodesContext  uintptr
	FpPointerQueueState     uintptr
	FIgnoreEmbeddedPointers int32
	FPointerBufferMark      uintptr
	FfBufferValid           uint8
	FuFlags                 uint8
	FUniquePtrCount         uint16
	FMaxCount               TULONG_PTR
	FOffset                 uint32
	FActualCount            uint32
	FpfnAllocate            uintptr
	FpfnFree                uintptr
	FStackTop               uintptr
	FpPresentedType         uintptr
	FpTransmitType          uintptr
	FSavedHandle            Thandle_t
	FStubDesc               uintptr
	FFullPtrXlatTables      uintptr
	FFullPtrRefId           uint32
	FPointerLength          uint32
	F__ccgo112              uint32
	FdwDestContext          uint32
	FpvDestContext          uintptr
	FSavedContextHandles    uintptr
	FParamNumber            int32
	FpRpcChannelBuffer      uintptr
	FpArrayInfo             TPARRAY_INFO
	FSizePtrCountArray      uintptr
	FSizePtrOffsetArray     uintptr
	FSizePtrLengthArray     uintptr
	FpArgQueue              uintptr
	FdwStubPhase            uint32
	FLowStackMark           uintptr
	FpAsyncMsg              TPNDR_ASYNC_MESSAGE
	FpCorrInfo              TPNDR_CORRELATION_INFO
	FpCorrMemory            uintptr
	FpMemoryList            uintptr
	FpCSInfo                uintptr
	FConformanceMark        uintptr
	FVarianceMark           uintptr
	FUnused                 TINT_PTR
	FpContext               uintptr
	FpUserMarshalList       uintptr
	FReserved51_2           TINT_PTR
	FReserved51_3           TINT_PTR
	FReserved51_4           TINT_PTR
	FReserved51_5           TINT_PTR
}

type T_PROPSHEETPAGEA = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKA
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCSTR
	FpszHeaderSubTitle TLPCSTR
	FhActCtx           THANDLE
}

type T_PROPSHEETPAGEW = struct {
	FdwSize     TDWORD
	FdwFlags    TDWORD
	FhInstance  THINSTANCE
	F__ccgo3_12 struct {
		FpResource   [0]TPROPSHEETPAGE_RESOURCE
		FpszTemplate TLPCWSTR
	}
	F__ccgo4_16 struct {
		FpszIcon [0]TLPCWSTR
		FhIcon   THICON
	}
	FpszTitle          TLPCWSTR
	FpfnDlgProc        TDLGPROC
	FlParam            TLPARAM
	FpfnCallback       TLPFNPSPCALLBACKW
	FpcRefParent       uintptr
	FpszHeaderTitle    TLPCWSTR
	FpszHeaderSubTitle TLPCWSTR
	FhActCtx           THANDLE
}

type T_RPC_ASYNC_STATE = struct {
	FSize             uint32
	FSignature        uint32
	FLock             int32
	FFlags            uint32
	FStubInfo         uintptr
	FUserInfo         uintptr
	FRuntimeInfo      uintptr
	FEvent            TRPC_ASYNC_EVENT
	FNotificationType TRPC_NOTIFICATION_TYPES
	Fu                struct {
		FIOC [0]struct {
			FhIOPort                    THANDLE
			FdwNumberOfBytesTransferred TDWORD
			FdwCompletionKey            TDWORD_PTR
			FlpOverlapped               TLPOVERLAPPED
		}
		FHWND [0]struct {
			FhWnd THWND
			FMsg  TUINT
		}
		FhEvent              [0]THANDLE
		FNotificationRoutine [0]TPFN_RPCNOTIFICATION_ROUTINE
		FAPC                 struct {
			FNotificationRoutine TPFN_RPCNOTIFICATION_ROUTINE
			FhThread             THANDLE
		}
		F__ccgo_pad5 [8]byte
	}
	FReserved [4]TLONG_PTR
}

type T_complex = struct {
	F__ccgo_align [0]uint32
	Fx            float64
	Fy            float64
}

type T_exception = struct {
	F__ccgo_align [0]uint32
	Ftype1        int32
	Fname         uintptr
	Farg1         float64
	Farg2         float64
	Fretval       float64
}

type T_pid_t = int32

type T_sigset_t = uint32

type T_timespec64 = struct {
	F__ccgo_align [0]uint32
	Ftv_sec       t__time64_t
	Ftv_nsec      int32
	F__ccgo_pad2  [4]byte
}

type T_wireVARIANT = struct {
	F__ccgo_align [0]uint32
	FclSize       TDWORD
	FrpcReserved  TDWORD
	Fvt           TUSHORT
	FwReserved1   TUSHORT
	FwReserved2   TUSHORT
	FwReserved3   TUSHORT
	F__ccgo6_16   struct {
		F__ccgo_align [0]uint32
		FlVal         [0]TLONG
		FbVal         [0]TBYTE
		FiVal         [0]TSHORT
		FfltVal       [0]TFLOAT
		FdblVal       [0]TDOUBLE
		FboolVal      [0]TVARIANT_BOOL
		Fscode        [0]TSCODE
		FcyVal        [0]TCY
		Fdate         [0]TDATE
		FbstrVal      [0]TwireBSTR
		FpunkVal      [0]uintptr
		FpdispVal     [0]uintptr
		Fparray       [0]TwirePSAFEARRAY
		FbrecVal      [0]TwireBRECORD
		FpbVal        [0]uintptr
		FpiVal        [0]uintptr
		FplVal        [0]uintptr
		FpllVal       [0]uintptr
		FpfltVal      [0]uintptr
		FpdblVal      [0]uintptr
		FpboolVal     [0]uintptr
		Fpscode       [0]uintptr
		FpcyVal       [0]uintptr
		Fpdate        [0]uintptr
		FpbstrVal     [0]uintptr
		FppunkVal     [0]uintptr
		FppdispVal    [0]uintptr
		Fpparray      [0]uintptr
		FpvarVal      [0]uintptr
		FcVal         [0]TCHAR
		FuiVal        [0]TUSHORT
		FulVal        [0]TULONG
		FullVal       [0]TULONGLONG
		FintVal       [0]TINT
		FuintVal      [0]TUINT
		FdecVal       [0]TDECIMAL
		FpdecVal      [0]uintptr
		FpcVal        [0]uintptr
		FpuiVal       [0]uintptr
		FpulVal       [0]uintptr
		FpullVal      [0]uintptr
		FpintVal      [0]uintptr
		FpuintVal     [0]uintptr
		FllVal        TLONGLONG
		F__ccgo_pad44 [8]byte
	}
}

type Tmax_align_t = struct {
	F__ccgo_align     [0]uint32
	F__max_align_ll   int64
	F__max_align_ld   float64
	F__max_align_f128 [2]uint64
}

type Tservent = struct {
	Fs_name    uintptr
	Fs_aliases uintptr
	Fs_port    int16
	Fs_proto   uintptr
}

type Tsqlite3rbu = struct {
	F__ccgo_align   [0]uint32
	FeStage         int32
	FdbMain         uintptr
	FdbRbu          uintptr
	FzTarget        uintptr
	FzRbu           uintptr
	FzState         uintptr
	FzStateDb       [5]int8
	Frc             int32
	FzErrmsg        uintptr
	FnStep          int32
	F__ccgo_align10 [4]byte
	FnProgress      Tsqlite3_int64
	Fobjiter        TRbuObjIter
	FzVfsName       uintptr
	FpTargetFd      uintptr
	FnPagePerSector int32
	FiOalSz         Ti64
	FnPhaseOneStep  Ti64
	FpRenameArg     uintptr
	FxRename        uintptr
	FiMaxFrame      Tu32
	FmLock          Tu32
	FnFrame         int32
	FnFrameAlloc    int32
	FaFrame         uintptr
	Fpgsz           int32
	FaBuf           uintptr
	F__ccgo_align26 [4]byte
	FiWalCksum      Ti64
	FszTemp         Ti64
	FszTempLimit    Ti64
	FnRbu           int32
	FpRbuFd         uintptr
}

type TuSTGMEDIUM = struct {
	Ftymed     TDWORD
	F__ccgo1_4 struct {
		FhMetaFilePict [0]THMETAFILEPICT
		FhEnhMetaFile  [0]THENHMETAFILE
		FhGlobal       [0]THGLOBAL
		FlpszFileName  [0]TLPOLESTR
		Fpstm          [0]uintptr
		Fpstg          [0]uintptr
		FhBitmap       THBITMAP
	}
	FpUnkForRelease uintptr
}

type TuserHBITMAP = struct {
	F__ccgo_align  [0]uint32
	FfContext      TLONG
	F__ccgo_align1 [4]byte
	Fu             t__WIDL_wtypes_generated_name_00000007
}

type TuserHENHMETAFILE = struct {
	F__ccgo_align  [0]uint32
	FfContext      TLONG
	F__ccgo_align1 [4]byte
	Fu             t__WIDL_wtypes_generated_name_00000006
}

type TuserHGLOBAL = struct {
	F__ccgo_align  [0]uint32
	FfContext      TLONG
	F__ccgo_align1 [4]byte
	Fu             t__WIDL_wtypes_generated_name_00000003
}

type TuserHMETAFILE = struct {
	F__ccgo_align  [0]uint32
	FfContext      TLONG
	F__ccgo_align1 [4]byte
	Fu             t__WIDL_wtypes_generated_name_00000004
}

type TuserHMETAFILEPICT = struct {
	F__ccgo_align  [0]uint32
	FfContext      TLONG
	F__ccgo_align1 [4]byte
	Fu             t__WIDL_wtypes_generated_name_00000005
}

type TuserHPALETTE = struct {
	F__ccgo_align  [0]uint32
	FfContext      TLONG
	F__ccgo_align1 [4]byte
	Fu             t__WIDL_wtypes_generated_name_00000008
}

// C documentation
//
//	/*
//	** The winFile structure is a subclass of sqlite3_file* specific to the win32
//	** portability layer.
//	*/
type TwinFile = struct {
	F__ccgo_align   [0]uint32
	FpMethod        uintptr
	FpVfs           uintptr
	Fh              THANDLE
	Flocktype       Tu8
	FsharedLockByte int16
	FctrlFlags      Tu8
	FlastErrno      TDWORD
	FpShm           uintptr
	FzPath          uintptr
	FszChunk        int32
	FnFetchOut      int32
	FhMap           THANDLE
	FpMapRegion     uintptr
	FmmapSize       Tsqlite3_int64
	FmmapSizeMax    Tsqlite3_int64
}

const WINAPI = "__stdcall"

const WINAPI_INLINE = "WINAPI"

const XSTATE_MASK_ALLOWED = 4611686018427390463

// C documentation
//
//	/*
//	** Create an sqlite3_backup process to copy the contents of zSrcDb from
//	** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
//	** a pointer to the new sqlite3_backup object.
//	**
//	** If an error occurs, NULL is returned and an error code and error message
//	** stored in database handle pDestDb.
//	*/
func Xsqlite3_backup_init(tls *libc.TLS, pDestDb uintptr, zDestDb uintptr, pSrcDb uintptr, zSrcDb uintptr) (r uintptr) {
	var nDest int32
	var p, pDest uintptr
	_, _, _ = nDest, p, pDest /* Value to return */
	/* Lock the source database handle. The destination database
	 ** handle is not locked in this routine, but it is locked in
	 ** sqlite3_backup_step(). The user is required to ensure that no
	 ** other thread accesses the destination handle for the duration
	 ** of the backup operation.  Any attempt to use the destination
	 ** database connection while a backup is in progress may cause
	 ** a malfunction or a deadlock.
	 */
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(pSrcDb)).Fmutex)
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(pDestDb)).Fmutex)
	if pSrcDb == pDestDb {
		_sqlite3ErrorWithMsg(tls, pDestDb, int32(SQLITE_ERROR), __ccgo_ts+6393, 0)
		p = uintptr(0)
	} else {
		nDest = _sqlite3Strlen30(tls, zDestDb)
		/* Allocate space for a new sqlite3_backup object...
		 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
		 ** call to sqlite3_backup_init() and is destroyed by a call to
		 ** sqlite3_backup_finish(). */
		p = _sqlite3MallocZero(tls, uint64(uint32(52)+uint32(nDest)+uint32(1)))
		if !(p != 0) {
			_sqlite3Error(tls, pDestDb, int32(SQLITE_NOMEM))
		} else {
			(*Tsqlite3_backup)(unsafe.Pointer(p)).FzDestDb = p + 1*52
			libc.Xmemcpy(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FzDestDb, zDestDb, uint32(nDest))
		}
	}
	/* If the allocation succeeded, populate the new object. */
	if p != 0 {
		/* Do not store the pointer to the destination b-tree at this point.
		 ** This is because there is nothing preventing it from being detached
		 ** or otherwise freed before the first call to sqlite3_backup_step()
		 ** on this object. The source b-tree does not have this problem, as
		 ** incrementing Btree.nBackup (see below) effectively locks the object. */
		pDest = _findBtree(tls, pDestDb, pDestDb, zDestDb)
		(*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc = _findBtree(tls, pDestDb, pSrcDb, zSrcDb)
		(*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb = pDestDb
		(*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrcDb = pSrcDb
		(*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext = uint32(1)
		(*Tsqlite3_backup)(unsafe.Pointer(p)).FisAttached = 0
		if uintptr(0) == (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc || uintptr(0) == pDest || _checkReadTransaction(tls, pDestDb, pDest) != SQLITE_OK {
			/* One (or both) of the named databases did not exist or an OOM
			 ** error was hit. Or there is a transaction open on the destination
			 ** database. The error has already been written into the pDestDb
			 ** handle. All that is left to do here is free the sqlite3_backup
			 ** structure.  */
			Xsqlite3_free(tls, p)
			p = uintptr(0)
		}
	}
	if p != 0 {
		(*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)).FnBackup = (*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)).FnBackup + 1
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(pDestDb)).Fmutex)
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(pSrcDb)).Fmutex)
	return p
}

// C documentation
//
//	/*
//	** Copy nPage pages from the source b-tree to the destination.
//	*/
func Xsqlite3_backup_step(tls *libc.TLS, p uintptr, nPage int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bCloseTrans, destMode, ii, nDestTruncate, nSrcPage, pgszDest, pgszSrc, ratio, rc, v2 int32
	var iEnd, iOff, iSize Ti64
	var iPg, iSrcPg, iSrcPg1 TPgno
	var pDest, pDestPager, pFile, pSrcPager, zData, v1 uintptr
	var v3 bool
	var v6 int64
	var _ /* nDstPage at bp+4 */ int32
	var _ /* pPg at bp+8 */ uintptr
	var _ /* pSrcPg at bp+0 */ uintptr
	var _ /* pSrcPg at bp+12 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bCloseTrans, destMode, iEnd, iOff, iPg, iSize, iSrcPg, iSrcPg1, ii, nDestTruncate, nSrcPage, pDest, pDestPager, pFile, pSrcPager, pgszDest, pgszSrc, ratio, rc, zData, v1, v2, v3, v6
	destMode = 0 /* Destination journal mode */
	pgszSrc = 0  /* Source page size */
	pgszDest = 0 /* Destination page size */
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrcDb)).Fmutex)
	_sqlite3BtreeEnter(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)
	if (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb != 0 {
		Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb)).Fmutex)
	}
	rc = (*Tsqlite3_backup)(unsafe.Pointer(p)).Frc
	if !(_isFatalError(tls, rc) != 0) {
		pSrcPager = _sqlite3BtreePager(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc) /* Source pager */
		pDest = uintptr(0)                                                               /* Dest btree */
		pDestPager = uintptr(0)                                                          /* Iterator variable */
		nSrcPage = -int32(1)                                                             /* Size of source db in pages */
		bCloseTrans = 0                                                                  /* True if src db requires unlocking */
		/* If the source pager is currently in a write-transaction, return
		 ** SQLITE_BUSY immediately.
		 */
		if (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb != 0 && int32((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)).FpBt)).FinTransaction) == int32(TRANS_WRITE) {
			rc = int32(SQLITE_BUSY)
		} else {
			rc = SQLITE_OK
		}
		/* If there is no open read-transaction on the source database, open
		 ** one now. If a transaction is opened here, then it will be closed
		 ** before this function exits.
		 */
		if rc == SQLITE_OK && SQLITE_TXN_NONE == _sqlite3BtreeTxnState(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc) {
			rc = _sqlite3BtreeBeginTrans(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc, 0, uintptr(0))
			bCloseTrans = int32(1)
		}
		/* Locate the destination btree and pager. */
		v1 = (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest
		pDest = v1
		if v1 == uintptr(0) {
			pDest = _findBtree(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb, (*Tsqlite3_backup)(unsafe.Pointer(p)).FzDestDb)
		}
		if pDest == uintptr(0) {
			rc = int32(SQLITE_ERROR)
		} else {
			pDestPager = _sqlite3BtreePager(tls, pDest)
		}
		/* If the destination database has not yet been locked (i.e. if this
		 ** is the first call to backup_step() for the current backup operation),
		 ** try to set its page size to the same as the source database. This
		 ** is especially important on ZipVFS systems, as in that case it is
		 ** not possible to create a database file that uses one page size by
		 ** writing to it with another.  */
		if (*Tsqlite3_backup)(unsafe.Pointer(p)).FbDestLocked == 0 && rc == SQLITE_OK && _setDestPgsz(tls, pDest, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc) == int32(SQLITE_NOMEM) {
			rc = int32(SQLITE_NOMEM)
		}
		/* Lock the destination database, if it is not locked already. */
		if v3 = SQLITE_OK == rc && (*Tsqlite3_backup)(unsafe.Pointer(p)).FbDestLocked == 0; v3 {
			v2 = _sqlite3BtreeBeginTrans(tls, pDest, int32(2), p+12)
			rc = v2
		}
		if v3 && SQLITE_OK == v2 {
			(*Tsqlite3_backup)(unsafe.Pointer(p)).FbDestLocked = int32(1)
			(*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest = pDest
		}
		/* Do not allow backup if the destination database is in WAL mode
		 ** and the page sizes are different between source and destination */
		if rc == SQLITE_OK {
			pgszSrc = _sqlite3BtreeGetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)
			pgszDest = _sqlite3BtreeGetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)
			destMode = _sqlite3PagerGetJournalMode(tls, _sqlite3BtreePager(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest))
			if (destMode == int32(PAGER_JOURNALMODE_WAL) || _sqlite3PagerIsMemdb(tls, pDestPager) != 0) && pgszSrc != pgszDest {
				rc = int32(SQLITE_READONLY)
			}
		}
		/* Now that there is a read-lock on the source database, query the
		 ** source pager for the number of pages in the database.
		 */
		nSrcPage = int32(_sqlite3BtreeLastPage(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc))
		ii = 0
		for {
			if !((nPage < 0 || ii < nPage) && (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext <= uint32(nSrcPage) && !(rc != 0)) {
				break
			}
			iSrcPg = (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext                                                                                                                                  /* Source page number */
			if iSrcPg != uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)).FpBt)).FpageSize+libc.Uint32FromInt32(1) { /* Source page object */
				rc = _sqlite3PagerGet(tls, pSrcPager, iSrcPg, bp, int32(PAGER_GET_READONLY))
				if rc == SQLITE_OK {
					rc = _backupOnePage(tls, p, iSrcPg, _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp))), 0)
					_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp)))
				}
			}
			(*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext = (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext + 1
			goto _4
		_4:
			;
			ii = ii + 1
		}
		if rc == SQLITE_OK {
			(*Tsqlite3_backup)(unsafe.Pointer(p)).FnPagecount = uint32(nSrcPage)
			(*Tsqlite3_backup)(unsafe.Pointer(p)).FnRemaining = uint32(nSrcPage+int32(1)) - (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext
			if (*Tsqlite3_backup)(unsafe.Pointer(p)).FiNext > uint32(nSrcPage) {
				rc = int32(SQLITE_DONE)
			} else {
				if !((*Tsqlite3_backup)(unsafe.Pointer(p)).FisAttached != 0) {
					_attachBackupObject(tls, p)
				}
			}
		}
		/* Update the schema version field in the destination database. This
		 ** is to make sure that the schema-version really does change in
		 ** the case where the source and destination databases have the
		 ** same schema version.
		 */
		if rc == int32(SQLITE_DONE) {
			if nSrcPage == 0 {
				rc = _sqlite3BtreeNewDb(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)
				nSrcPage = int32(1)
			}
			if rc == SQLITE_OK || rc == int32(SQLITE_DONE) {
				rc = _sqlite3BtreeUpdateMeta(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest, int32(1), (*Tsqlite3_backup)(unsafe.Pointer(p)).FiDestSchema+uint32(1))
			}
			if rc == SQLITE_OK {
				if (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb != 0 {
					_sqlite3ResetAllSchemasOfConnection(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb)
				}
				if destMode == int32(PAGER_JOURNALMODE_WAL) {
					rc = _sqlite3BtreeSetVersion(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest, int32(2))
				}
			}
			if rc == SQLITE_OK {
				/* Set nDestTruncate to the final number of pages in the destination
				 ** database. The complication here is that the destination page
				 ** size may be different to the source page size.
				 **
				 ** If the source page size is smaller than the destination page size,
				 ** round up. In this case the call to sqlite3OsTruncate() below will
				 ** fix the size of the file. However it is important to call
				 ** sqlite3PagerTruncateImage() here so that any pages in the
				 ** destination file that lie beyond the nDestTruncate page mark are
				 ** journalled by PagerCommitPhaseOne() before they are destroyed
				 ** by the file truncation.
				 */
				if pgszSrc < pgszDest {
					ratio = pgszDest / pgszSrc
					nDestTruncate = (nSrcPage + ratio - int32(1)) / ratio
					if nDestTruncate == int32(uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)).FpBt)).FpageSize+libc.Uint32FromInt32(1)) {
						nDestTruncate = nDestTruncate - 1
					}
				} else {
					nDestTruncate = nSrcPage * (pgszSrc / pgszDest)
				}
				if pgszSrc < pgszDest {
					/* If the source page-size is smaller than the destination page-size,
					 ** two extra things may need to happen:
					 **
					 **   * The destination may need to be truncated, and
					 **
					 **   * Data stored on the pages immediately following the
					 **     pending-byte page in the source database may need to be
					 **     copied into the destination database.
					 */
					iSize = int64(pgszSrc) * int64(nSrcPage)
					pFile = _sqlite3PagerFile(tls, pDestPager)
					/* This block ensures that all data required to recreate the original
					 ** database has been stored in the journal for pDestPager and the
					 ** journal synced to disk. So at this point we may safely modify
					 ** the database file in any way, knowing that if a power failure
					 ** occurs, the original database will be reconstructed from the
					 ** journal file.  */
					_sqlite3PagerPagecount(tls, pDestPager, bp+4)
					iPg = uint32(nDestTruncate)
					for {
						if !(rc == SQLITE_OK && iPg <= uint32(**(**int32)(__ccgo_up(bp + 4)))) {
							break
						}
						if iPg != uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)).FpBt)).FpageSize+libc.Uint32FromInt32(1) {
							rc = _sqlite3PagerGet(tls, pDestPager, iPg, bp+8, 0)
							if rc == SQLITE_OK {
								rc = _sqlite3PagerWrite(tls, **(**uintptr)(__ccgo_up(bp + 8)))
								_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp + 8)))
							}
						}
						goto _5
					_5:
						;
						iPg = iPg + 1
					}
					if rc == SQLITE_OK {
						rc = _sqlite3PagerCommitPhaseOne(tls, pDestPager, uintptr(0), int32(1))
					}
					/* Write the extra pages and truncate the database file as required */
					if int64(_sqlite3PendingByte+pgszDest) < iSize {
						v6 = int64(_sqlite3PendingByte + pgszDest)
					} else {
						v6 = iSize
					}
					iEnd = v6
					iOff = int64(_sqlite3PendingByte + pgszSrc)
					for {
						if !(rc == SQLITE_OK && iOff < iEnd) {
							break
						}
						**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
						iSrcPg1 = uint32(iOff/int64(pgszSrc) + libc.Int64FromInt32(1))
						rc = _sqlite3PagerGet(tls, pSrcPager, iSrcPg1, bp+12, 0)
						if rc == SQLITE_OK {
							zData = _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp + 12)))
							rc = _sqlite3OsWrite(tls, pFile, zData, pgszSrc, iOff)
						}
						_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp + 12)))
						goto _7
					_7:
						;
						iOff = iOff + int64(pgszSrc)
					}
					if rc == SQLITE_OK {
						rc = _backupTruncateFile(tls, pFile, iSize)
					}
					/* Sync the database file to disk. */
					if rc == SQLITE_OK {
						rc = _sqlite3PagerSync(tls, pDestPager, uintptr(0))
					}
				} else {
					_sqlite3PagerTruncateImage(tls, pDestPager, uint32(nDestTruncate))
					rc = _sqlite3PagerCommitPhaseOne(tls, pDestPager, uintptr(0), 0)
				}
				/* Finish committing the transaction to the destination database. */
				if v3 = SQLITE_OK == rc; v3 {
					v2 = _sqlite3BtreeCommitPhaseTwo(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest, 0)
					rc = v2
				}
				if v3 && SQLITE_OK == v2 {
					rc = int32(SQLITE_DONE)
				}
			}
		}
		/* If bCloseTrans is true, then this function opened a read transaction
		 ** on the source database. Close the read transaction here. There is
		 ** no need to check the return values of the btree methods here, as
		 ** "committing" a read-only transaction cannot fail.
		 */
		if bCloseTrans != 0 {
			_sqlite3BtreeCommitPhaseOne(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc, uintptr(0))
			_sqlite3BtreeCommitPhaseTwo(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc, 0)
		}
		if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<<libc.Int32FromInt32(8) {
			rc = int32(SQLITE_NOMEM)
		}
		(*Tsqlite3_backup)(unsafe.Pointer(p)).Frc = rc
	}
	if (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb != 0 {
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpDestDb)).Fmutex)
	}
	_sqlite3BtreeLeave(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrcDb)).Fmutex)
	return rc
}

func Xsqlite3_bind_double(tls *libc.TLS, pStmt uintptr, i int32, rValue float64) (r int32) {
	var p uintptr
	var rc int32
	_, _ = p, rc
	p = pStmt
	rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1)))
	if rc == SQLITE_OK {
		/* tag-20240917-01 */
		_sqlite3VdbeMemSetDouble(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar+uintptr(i-int32(1))*40, rValue)
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
	}
	return rc
}

func Xsqlite3_bind_int64(tls *libc.TLS, pStmt uintptr, i int32, iValue Tsqlite_int64) (r int32) {
	var p uintptr
	var rc int32
	_, _ = p, rc
	p = pStmt
	rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1)))
	if rc == SQLITE_OK {
		/* tag-20240917-01 */
		_sqlite3VdbeMemSetInt64(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar+uintptr(i-int32(1))*40, iValue)
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
	}
	return rc
}

func Xsqlite3_bind_pointer(tls *libc.TLS, pStmt uintptr, i int32, pPtr uintptr, zPTtype uintptr, __ccgo_fp_xDestructor uintptr) (r int32) {
	var p uintptr
	var rc int32
	_, _ = p, rc
	p = pStmt
	rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1)))
	if rc == SQLITE_OK {
		/* tag-20240917-01 */
		_sqlite3VdbeMemSetPointer(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar+uintptr(i-int32(1))*40, pPtr, zPTtype, __ccgo_fp_xDestructor)
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
	} else {
		if __ccgo_fp_xDestructor != 0 {
			(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xDestructor})))(tls, pPtr)
		}
	}
	return rc
}

func Xsqlite3_bind_zeroblob(tls *libc.TLS, pStmt uintptr, i int32, n int32) (r int32) {
	var p uintptr
	var rc int32
	_, _ = p, rc
	p = pStmt
	rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1)))
	if rc == SQLITE_OK {
		/* tag-20240917-01 */
		_sqlite3VdbeMemSetZeroBlob(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar+uintptr(i-int32(1))*40, n)
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
	}
	return rc
}

func Xsqlite3_bind_zeroblob64(tls *libc.TLS, pStmt uintptr, i int32, n Tsqlite3_uint64) (r int32) {
	var p uintptr
	var rc int32
	_, _ = p, rc
	p = pStmt
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
	if n > uint64(**(**int32)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).Fdb + 120))) {
		rc = int32(SQLITE_TOOBIG)
	} else {
		rc = Xsqlite3_bind_zeroblob(tls, pStmt, i, int32(n))
	}
	rc = _sqlite3ApiExit(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, rc)
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Open a blob handle.
//	*/
func Xsqlite3_blob_open(tls *libc.TLS, db uintptr, zDb uintptr, zTable uintptr, zColumn uintptr, iRow Tsqlite_int64, wrFlag int32, ppBlob uintptr) (r int32) {
	bp := tls.Alloc(304)
	defer tls.Free(304)
	var aOp, pBlob, pFKey, pIdx, pTab, v, zFault, v8 uintptr
	var iCol, iDb, j, j1, nAttempt, rc, v1 int32
	var v2 bool
	var _ /* sParse at bp+4 */ TParse
	var _ /* zErr at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aOp, iCol, iDb, j, j1, nAttempt, pBlob, pFKey, pIdx, pTab, rc, v, zFault, v1, v2, v8
	nAttempt = 0 /* Index of zColumn in row-record */
	rc = SQLITE_OK
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	pBlob = uintptr(0)
	**(**uintptr)(__ccgo_up(ppBlob)) = uintptr(0)
	wrFlag = libc.BoolInt32(!!(wrFlag != 0)) /* wrFlag = (wrFlag ? 1 : 0); */
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	pBlob = _sqlite3DbMallocZero(tls, db, uint64(32))
	for int32(1) != 0 {
		_sqlite3ParseObjectInit(tls, bp+4, db)
		if !(pBlob != 0) {
			goto blob_open_out
		}
		_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		_sqlite3BtreeEnterAll(tls, db)
		pTab = _sqlite3LocateTable(tls, bp+4, uint32(0), zTable, zDb)
		if pTab != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
			pTab = uintptr(0)
			_sqlite3ErrorMsg(tls, bp+4, __ccgo_ts+7766, libc.VaList(bp+296, zTable))
		}
		if pTab != 0 && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
			pTab = uintptr(0)
			_sqlite3ErrorMsg(tls, bp+4, __ccgo_ts+7796, libc.VaList(bp+296, zTable))
		}
		if pTab != 0 && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) {
			pTab = uintptr(0)
			_sqlite3ErrorMsg(tls, bp+4, __ccgo_ts+7832, libc.VaList(bp+296, zTable))
		}
		if pTab != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
			pTab = uintptr(0)
			_sqlite3ErrorMsg(tls, bp+4, __ccgo_ts+7877, libc.VaList(bp+296, zTable))
		}
		if v2 = pTab == uintptr(0); !v2 {
			v1 = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
			iDb = v1
		}
		if v2 || v1 == int32(1) && _sqlite3OpenTempDatabase(tls, bp+4) != 0 {
			if (**(**TParse)(__ccgo_up(bp + 4))).FzErrMsg != 0 {
				_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
				**(**uintptr)(__ccgo_up(bp)) = (**(**TParse)(__ccgo_up(bp + 4))).FzErrMsg
				(**(**TParse)(__ccgo_up(bp + 4))).FzErrMsg = uintptr(0)
			}
			rc = int32(SQLITE_ERROR)
			_sqlite3BtreeLeaveAll(tls, db)
			goto blob_open_out
		}
		(*TIncrblob)(unsafe.Pointer(pBlob)).FpTab = pTab
		(*TIncrblob)(unsafe.Pointer(pBlob)).FzDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
		/* Now search pTab for the exact column. */
		iCol = _sqlite3ColumnIndex(tls, pTab, zColumn)
		if iCol < 0 {
			_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
			**(**uintptr)(__ccgo_up(bp)) = _sqlite3MPrintf(tls, db, __ccgo_ts+7898, libc.VaList(bp+296, zColumn))
			rc = int32(SQLITE_ERROR)
			_sqlite3BtreeLeaveAll(tls, db)
			goto blob_open_out
		}
		/* If the value is being opened for writing, check that the
		 ** column is not indexed, and that it is not part of a foreign key.
		 */
		if wrFlag != 0 {
			zFault = uintptr(0)
			if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0 {
				pFKey = (*(*struct {
					FaddColOffset int32
					FpFKey        uintptr
					FpDfltList    uintptr
				})(unsafe.Pointer(pTab + 44))).FpFKey
				for {
					if !(pFKey != 0) {
						break
					}
					j = 0
					for {
						if !(j < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) {
							break
						}
						if (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(j)*8))).FiFrom == iCol {
							zFault = __ccgo_ts + 7919
						}
						goto _4
					_4:
						;
						j = j + 1
					}
					goto _3
				_3:
					;
					pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom
				}
			}
			pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
			for {
				if !(pIdx != 0) {
					break
				}
				j1 = 0
				for {
					if !(j1 < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
						break
					}
					/* FIXME: Be smarter about indexes that use expressions */
					if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j1)*2))) == iCol || int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j1)*2))) == -int32(2) {
						zFault = __ccgo_ts + 7931
					}
					goto _6
				_6:
					;
					j1 = j1 + 1
				}
				goto _5
			_5:
				;
				pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
			}
			if zFault != 0 {
				_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
				**(**uintptr)(__ccgo_up(bp)) = _sqlite3MPrintf(tls, db, __ccgo_ts+7939, libc.VaList(bp+296, zFault))
				rc = int32(SQLITE_ERROR)
				_sqlite3BtreeLeaveAll(tls, db)
				goto blob_open_out
			}
		}
		(*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt = _sqlite3VdbeCreate(tls, bp+4)
		if (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt != 0 {
			v = (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_Transaction), iDb, wrFlag, (*TSchema)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FpSchema)).Fschema_cookie, (*TSchema)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FpSchema)).FiGeneration)
			_sqlite3VdbeChangeP5(tls, v, uint16(1))
			aOp = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(24)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_openBlob)), _iLn)
			/* Make sure a mutex is held on the table to be accessed */
			_sqlite3VdbeUsesBtree(tls, v, iDb)
			if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
				/* Configure the OP_TableLock instruction */
				(**(**TVdbeOp)(__ccgo_up(aOp))).Fp1 = iDb
				(**(**TVdbeOp)(__ccgo_up(aOp))).Fp2 = int32((*TTable)(unsafe.Pointer(pTab)).Ftnum)
				(**(**TVdbeOp)(__ccgo_up(aOp))).Fp3 = wrFlag
				_sqlite3VdbeChangeP4(tls, v, int32(2), (*TTable)(unsafe.Pointer(pTab)).FzName, P4_TRANSIENT)
			}
			if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
				/* Remove either the OP_OpenWrite or OpenRead. Set the P2
				 ** parameter of the other to pTab->tnum.  */
				if wrFlag != 0 {
					(**(**TVdbeOp)(__ccgo_up(aOp + 1*20))).Fopcode = uint8(OP_OpenWrite)
				}
				(**(**TVdbeOp)(__ccgo_up(aOp + 1*20))).Fp2 = int32((*TTable)(unsafe.Pointer(pTab)).Ftnum)
				(**(**TVdbeOp)(__ccgo_up(aOp + 1*20))).Fp3 = iDb
				/* Configure the number of columns. Configure the cursor to
				 ** think that the table has one more column than it really
				 ** does. An OP_Column to retrieve this imaginary column will
				 ** always return an SQL NULL. This is useful because it means
				 ** we can invoke OP_Column to fill in the vdbe cursors type
				 ** and offset cache without causing any IO.
				 */
				(**(**TVdbeOp)(__ccgo_up(aOp + 1*20))).Fp4type = int8(-libc.Int32FromInt32(3))
				*(*int32)(unsafe.Pointer(aOp + 1*20 + 16)) = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) + int32(1)
				(**(**TVdbeOp)(__ccgo_up(aOp + 3*20))).Fp2 = int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
				(**(**TParse)(__ccgo_up(bp + 4))).FnVar = 0
				(**(**TParse)(__ccgo_up(bp + 4))).FnMem = int32(1)
				(**(**TParse)(__ccgo_up(bp + 4))).FnTab = int32(1)
				_sqlite3VdbeMakeReady(tls, v, bp+4)
			}
		}
		(*TIncrblob)(unsafe.Pointer(pBlob)).FiCol = uint16(iCol)
		(*TIncrblob)(unsafe.Pointer(pBlob)).Fdb = db
		_sqlite3BtreeLeaveAll(tls, db)
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			goto blob_open_out
		}
		rc = _blobSeekToRow(tls, pBlob, iRow, bp)
		nAttempt = nAttempt + 1
		v1 = nAttempt
		if v1 >= int32(SQLITE_MAX_SCHEMA_RETRY) || rc != int32(SQLITE_SCHEMA) {
			break
		}
		_sqlite3ParseObjectReset(tls, bp+4)
	}
	goto blob_open_out
blob_open_out:
	;
	if rc == SQLITE_OK && int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
		**(**uintptr)(__ccgo_up(ppBlob)) = pBlob
	} else {
		if pBlob != 0 && (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt != 0 {
			_sqlite3VdbeFinalize(tls, (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt)
		}
		_sqlite3DbFree(tls, db, pBlob)
	}
	if **(**uintptr)(__ccgo_up(bp)) != 0 {
		v8 = __ccgo_ts + 4729
	} else {
		v8 = libc.UintptrFromInt32(0)
	}
	_sqlite3ErrorWithMsg(tls, db, rc, v8, libc.VaList(bp+296, **(**uintptr)(__ccgo_up(bp))))
	_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
	_sqlite3ParseObjectReset(tls, bp+4)
	rc = _sqlite3ApiExit(tls, db, rc)
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Cancel a prior call to sqlite3_auto_extension.  Remove xInit from the
//	** set of routines that is invoked for each new database connection, if it
//	** is currently on the list.  If xInit is not on the list, then this
//	** routine is a no-op.
//	**
//	** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
//	** was not on the list.
//	*/
func Xsqlite3_cancel_auto_extension(tls *libc.TLS, __ccgo_fp_xInit uintptr) (r int32) {
	var i, n int32
	var mutex uintptr
	_, _, _ = i, mutex, n
	mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN))
	n = 0
	Xsqlite3_mutex_enter(tls, mutex)
	i = int32(_sqlite3Autoext.FnExt) - int32(1)
	for {
		if !(i >= 0) {
			break
		}
		if **(**uintptr)(__ccgo_up(_sqlite3Autoext.FaExt + uintptr(i)*4)) == __ccgo_fp_xInit {
			_sqlite3Autoext.FnExt = _sqlite3Autoext.FnExt - 1
			**(**uintptr)(__ccgo_up(_sqlite3Autoext.FaExt + uintptr(i)*4)) = **(**uintptr)(__ccgo_up(_sqlite3Autoext.FaExt + uintptr(_sqlite3Autoext.FnExt)*4))
			n = n + 1
			break
		}
		goto _1
	_1:
		;
		i = i - 1
	}
	Xsqlite3_mutex_leave(tls, mutex)
	return n
}

func Xsqlite3_column_value(tls *libc.TLS, pStmt uintptr, i int32) (r uintptr) {
	var pOut, v1 uintptr
	_, _ = pOut, v1
	pOut = _columnMem(tls, pStmt, i)
	if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Static) != 0 {
		v1 = pOut + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(MEM_Static))
		v1 = pOut + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Ephem))
	}
	_columnMallocFailure(tls, pStmt)
	return pOut
}

// C documentation
//
//	/*
//	** Given the name of a compile-time option, return true if that option
//	** was used and false if not.
//	**
//	** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
//	** is not required for a match.
//	*/
func Xsqlite3_compileoption_used(tls *libc.TLS, zOptName uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var azCompileOpt uintptr
	var i, n int32
	var _ /* nOpt at bp+0 */ int32
	_, _, _ = azCompileOpt, i, n
	azCompileOpt = _sqlite3CompileOptions(tls, bp)
	if Xsqlite3_strnicmp(tls, zOptName, __ccgo_ts+27605, int32(7)) == 0 {
		zOptName = zOptName + uintptr(7)
	}
	n = _sqlite3Strlen30(tls, zOptName)
	/* Since nOpt is normally in single digits, a linear search is
	 ** adequate. No need for a binary search. */
	i = 0
	for {
		if !(i < **(**int32)(__ccgo_up(bp))) {
			break
		}
		if Xsqlite3_strnicmp(tls, zOptName, **(**uintptr)(__ccgo_up(azCompileOpt + uintptr(i)*4)), n) == 0 && _sqlite3IsIdChar(tls, uint8(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(azCompileOpt + uintptr(i)*4)) + uintptr(n))))) == 0 {
			return int32(1)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** This API allows applications to modify the global configuration of
//	** the SQLite library at run-time.
//	**
//	** This routine should only be called when there are no outstanding
//	** database connections or memory allocations.  This routine is not
//	** threadsafe.  Failure to heed these warnings can lead to unpredictable
//	** behavior.
//	*/
func Xsqlite3_config(tls *libc.TLS, op int32, va uintptr) (r int32) {
	var ap Tva_list
	var bOpenUri, rc int32
	var mxMmap, szMmap Tsqlite3_int64
	var pLogArg, pVal, xLog uintptr
	_, _, _, _, _, _, _, _ = ap, bOpenUri, mxMmap, pLogArg, pVal, rc, szMmap, xLog
	rc = SQLITE_OK
	/* sqlite3_config() normally returns SQLITE_MISUSE if it is invoked while
	 ** the SQLite library is in use.  Except, a few selected opcodes
	 ** are allowed.
	 */
	if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+208) != 0 {
		if op < 0 || op > int32(63) || libc.Uint64FromInt32(1)<<op&_mAnytimeConfigOption == uint64(0) {
			return _sqlite3MisuseError(tls, int32(187803))
		}
	}
	ap = va
	switch op {
	/* Mutex configuration options are only available in a threadsafe
	 ** compile.
	 */
	case int32(SQLITE_CONFIG_SINGLETHREAD):
		/* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
		 ** Single-thread. */
		_sqlite3Config.FbCoreMutex = uint8(0) /* Disable mutex on core */
		_sqlite3Config.FbFullMutex = uint8(0) /* Disable mutex on connections */
	case int32(SQLITE_CONFIG_MULTITHREAD):
		/* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
		 ** Multi-thread. */
		_sqlite3Config.FbCoreMutex = uint8(1) /* Enable mutex on core */
		_sqlite3Config.FbFullMutex = uint8(0) /* Disable mutex on connections */
	case int32(SQLITE_CONFIG_SERIALIZED):
		/* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
		 ** Serialized. */
		_sqlite3Config.FbCoreMutex = uint8(1) /* Enable mutex on core */
		_sqlite3Config.FbFullMutex = uint8(1) /* Enable mutex on connections */
	case int32(SQLITE_CONFIG_MUTEX):
		/* Specify an alternative mutex implementation */
		_sqlite3Config.Fmutex = **(**Tsqlite3_mutex_methods)(__ccgo_up(libc.VaUintptr(&ap)))
	case int32(SQLITE_CONFIG_GETMUTEX):
		/* Retrieve the current mutex implementation */
		**(**Tsqlite3_mutex_methods)(__ccgo_up(libc.VaUintptr(&ap))) = _sqlite3Config.Fmutex
	case int32(SQLITE_CONFIG_MALLOC):
		/* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
		 ** single argument which is a pointer to an instance of the
		 ** sqlite3_mem_methods structure. The argument specifies alternative
		 ** low-level memory allocation routines to be used in place of the memory
		 ** allocation routines built into SQLite. */
		_sqlite3Config.Fm = **(**Tsqlite3_mem_methods)(__ccgo_up(libc.VaUintptr(&ap)))
	case int32(SQLITE_CONFIG_GETMALLOC):
		/* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
		 ** single argument which is a pointer to an instance of the
		 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
		 ** filled with the currently defined memory allocation routines. */
		if _sqlite3Config.Fm.FxMalloc == uintptr(0) {
			_sqlite3MemSetDefault(tls)
		}
		**(**Tsqlite3_mem_methods)(__ccgo_up(libc.VaUintptr(&ap))) = _sqlite3Config.Fm
	case int32(SQLITE_CONFIG_MEMSTATUS):
		/* Cannot change at runtime */
		/* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
		 ** single argument of type int, interpreted as a boolean, which enables
		 ** or disables the collection of memory allocation statistics. */
		_sqlite3Config.FbMemstat = libc.VaInt32(&ap)
	case int32(SQLITE_CONFIG_SMALL_MALLOC):
		_sqlite3Config.FbSmallMalloc = uint8(libc.VaInt32(&ap))
	case int32(SQLITE_CONFIG_PAGECACHE):
		/* EVIDENCE-OF: R-18761-36601 There are three arguments to
		 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
		 ** the size of each page cache line (sz), and the number of cache lines
		 ** (N). */
		_sqlite3Config.FpPage = libc.VaUintptr(&ap)
		_sqlite3Config.FszPage = libc.VaInt32(&ap)
		_sqlite3Config.FnPage = libc.VaInt32(&ap)
	case int32(SQLITE_CONFIG_PCACHE_HDRSZ):
		/* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
		 ** a single parameter which is a pointer to an integer and writes into
		 ** that integer the number of extra bytes per page required for each page
		 ** in SQLITE_CONFIG_PAGECACHE. */
		**(**int32)(__ccgo_up(libc.VaUintptr(&ap))) = _sqlite3HeaderSizeBtree(tls) + _sqlite3HeaderSizePcache(tls) + _sqlite3HeaderSizePcache1(tls)
	case int32(SQLITE_CONFIG_PCACHE):
		/* no-op */
	case int32(SQLITE_CONFIG_GETPCACHE):
		/* now an error */
		rc = int32(SQLITE_ERROR)
	case int32(SQLITE_CONFIG_PCACHE2):
		/* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
		 ** single argument which is a pointer to an sqlite3_pcache_methods2
		 ** object. This object specifies the interface to a custom page cache
		 ** implementation. */
		_sqlite3Config.Fpcache2 = **(**Tsqlite3_pcache_methods2)(__ccgo_up(libc.VaUintptr(&ap)))
	case int32(SQLITE_CONFIG_GETPCACHE2):
		/* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
		 ** single argument which is a pointer to an sqlite3_pcache_methods2
		 ** object. SQLite copies of the current page cache implementation into
		 ** that object. */
		if _sqlite3Config.Fpcache2.FxInit == uintptr(0) {
			_sqlite3PCacheSetDefault(tls)
		}
		**(**Tsqlite3_pcache_methods2)(__ccgo_up(libc.VaUintptr(&ap))) = _sqlite3Config.Fpcache2
		break
		/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
		 ** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
		 ** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
		fallthrough
	case int32(SQLITE_CONFIG_LOOKASIDE):
		_sqlite3Config.FszLookaside = libc.VaInt32(&ap)
		_sqlite3Config.FnLookaside = libc.VaInt32(&ap)
		break
		/* Record a pointer to the logger function and its first argument.
		 ** The default is NULL.  Logging is disabled if the function pointer is
		 ** NULL.
		 */
		fallthrough
	case int32(SQLITE_CONFIG_LOG):
		xLog = libc.VaUintptr(&ap)
		pLogArg = libc.VaUintptr(&ap)
		libc.AtomicStoreNUintptr(uintptr(unsafe.Pointer(&_sqlite3Config))+236, xLog, libc.Int32FromInt32(__ATOMIC_RELAXED))
		libc.AtomicStoreNUintptr(uintptr(unsafe.Pointer(&_sqlite3Config))+240, pLogArg, libc.Int32FromInt32(__ATOMIC_RELAXED))
		break
		/* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
		 ** can be changed at start-time using the
		 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
		 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
		 */
		fallthrough
	case int32(SQLITE_CONFIG_URI):
		/* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
		 ** argument of type int. If non-zero, then URI handling is globally
		 ** enabled. If the parameter is zero, then URI handling is globally
		 ** disabled. */
		bOpenUri = libc.VaInt32(&ap)
		libc.AtomicStoreNUint8(uintptr(unsafe.Pointer(&_sqlite3Config))+6, uint8(bOpenUri), libc.Int32FromInt32(__ATOMIC_RELAXED))
	case int32(SQLITE_CONFIG_COVERING_INDEX_SCAN):
		/* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
		 ** option takes a single integer argument which is interpreted as a
		 ** boolean in order to enable or disable the use of covering indices for
		 ** full table scans in the query optimizer. */
		_sqlite3Config.FbUseCis = uint8(libc.VaInt32(&ap))
	case int32(SQLITE_CONFIG_MMAP_SIZE):
		/* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
		 ** integer (sqlite3_int64) values that are the default mmap size limit
		 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
		 ** mmap size limit. */
		szMmap = libc.VaInt64(&ap)
		mxMmap = libc.VaInt64(&ap)
		/* EVIDENCE-OF: R-53367-43190 If either argument to this option is
		 ** negative, then that argument is changed to its compile-time default.
		 **
		 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
		 ** silently truncated if necessary so that it does not exceed the
		 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
		 ** compile-time option.
		 */
		if mxMmap < 0 || mxMmap > int64(SQLITE_MAX_MMAP_SIZE) {
			mxMmap = int64(SQLITE_MAX_MMAP_SIZE)
		}
		if szMmap < 0 {
			szMmap = SQLITE_DEFAULT_MMAP_SIZE
		}
		if szMmap > mxMmap {
			szMmap = mxMmap
		}
		_sqlite3Config.FmxMmap = mxMmap
		_sqlite3Config.FszMmap = szMmap
	case int32(SQLITE_CONFIG_PMASZ):
		_sqlite3Config.FszPma = libc.VaUint32(&ap)
	case int32(SQLITE_CONFIG_STMTJRNL_SPILL):
		_sqlite3Config.FnStmtSpill = libc.VaInt32(&ap)
	case int32(SQLITE_CONFIG_MEMDB_MAXSIZE):
		_sqlite3Config.FmxMemdbSize = libc.VaInt64(&ap)
	case int32(SQLITE_CONFIG_ROWID_IN_VIEW):
		pVal = libc.VaUintptr(&ap)
		**(**int32)(__ccgo_up(pVal)) = 0
	default:
		rc = int32(SQLITE_ERROR)
		break
	}
	_ = ap
	return rc
}

// C documentation
//
//	/*
//	** Allocate memory to hold names for a database, journal file, WAL file,
//	** and query parameters.  The pointer returned is valid for use by
//	** sqlite3_filename_database() and sqlite3_uri_parameter() and related
//	** functions.
//	**
//	** Memory layout must be compatible with that generated by the pager
//	** and expected by sqlite3_uri_parameter() and databaseName().
//	*/
func Xsqlite3_create_filename(tls *libc.TLS, zDatabase uintptr, zJournal uintptr, zWal uintptr, nParam int32, azParam uintptr) (r uintptr) {
	var i int32
	var nByte Tsqlite3_int64
	var p, pResult, v2 uintptr
	_, _, _, _, _ = i, nByte, p, pResult, v2
	nByte = int64(libc.Xstrlen(tls, zDatabase) + libc.Xstrlen(tls, zJournal) + libc.Xstrlen(tls, zWal) + uint32(10))
	i = 0
	for {
		if !(i < nParam*int32(2)) {
			break
		}
		nByte = nByte + int64(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(azParam + uintptr(i)*4)))+uint32(1))
		goto _1
	_1:
		;
		i = i + 1
	}
	v2 = Xsqlite3_malloc64(tls, uint64(nByte))
	p = v2
	pResult = v2
	if p == uintptr(0) {
		return uintptr(0)
	}
	libc.Xmemset(tls, p, 0, uint32(4))
	p = p + uintptr(4)
	p = _appendText(tls, p, zDatabase)
	i = 0
	for {
		if !(i < nParam*int32(2)) {
			break
		}
		p = _appendText(tls, p, **(**uintptr)(__ccgo_up(azParam + uintptr(i)*4)))
		goto _3
	_3:
		;
		i = i + 1
	}
	v2 = p
	p = p + 1
	**(**int8)(__ccgo_up(v2)) = 0
	p = _appendText(tls, p, zJournal)
	p = _appendText(tls, p, zWal)
	v2 = p
	p = p + 1
	**(**int8)(__ccgo_up(v2)) = 0
	v2 = p
	p = p + 1
	**(**int8)(__ccgo_up(v2)) = 0
	return pResult + uintptr(4)
}

// C documentation
//
//	/*
//	** Configuration settings for an individual database connection
//	*/
func Xsqlite3_db_config(tls *libc.TLS, db uintptr, op int32, va uintptr) (r int32) {
	var ap Tva_list
	var cnt, nIn, onoff, rc, sz int32
	var i uint32
	var oldFlags Tu64
	var pBuf, pOut, pRes uintptr
	_, _, _, _, _, _, _, _, _, _, _ = ap, cnt, i, nIn, oldFlags, onoff, pBuf, pOut, pRes, rc, sz
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	ap = va
	switch op {
	case int32(SQLITE_DBCONFIG_MAINDBNAME):
		/* IMP: R-06824-28531 */
		/* IMP: R-36257-52125 */
		(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FzDbSName = libc.VaUintptr(&ap)
		rc = SQLITE_OK
	case int32(SQLITE_DBCONFIG_LOOKASIDE):
		pBuf = libc.VaUintptr(&ap) /* IMP: R-26835-10964 */
		sz = libc.VaInt32(&ap)     /* IMP: R-47871-25994 */
		cnt = libc.VaInt32(&ap)    /* IMP: R-04460-53386 */
		rc = _setupLookaside(tls, db, pBuf, sz, cnt)
	case int32(SQLITE_DBCONFIG_FP_DIGITS):
		nIn = libc.VaInt32(&ap)
		pOut = libc.VaUintptr(&ap)
		if nIn > int32(3) && nIn < int32(24) {
			(*Tsqlite3)(unsafe.Pointer(db)).FnFpDigit = uint8(nIn)
		}
		if pOut != 0 {
			**(**int32)(__ccgo_up(pOut)) = int32((*Tsqlite3)(unsafe.Pointer(db)).FnFpDigit)
		}
		rc = SQLITE_OK
	default:
		rc = int32(SQLITE_ERROR) /* IMP: R-42790-23372 */
		i = uint32(0)
		for {
			if !(i < uint32(int32(libc.Uint32FromInt64(336)/libc.Uint32FromInt64(16)))) {
				break
			}
			if _aFlagOp[i].Fop == op {
				onoff = libc.VaInt32(&ap)
				pRes = libc.VaUintptr(&ap)
				oldFlags = (*Tsqlite3)(unsafe.Pointer(db)).Fflags
				if onoff > 0 {
					**(**Tu64)(__ccgo_up(db + 32)) |= _aFlagOp[i].Fmask
				} else {
					if onoff == 0 {
						**(**Tu64)(__ccgo_up(db + 32)) &= ^_aFlagOp[i].Fmask
					}
				}
				if oldFlags != (*Tsqlite3)(unsafe.Pointer(db)).Fflags {
					_sqlite3ExpirePreparedStatements(tls, db, 0)
				}
				if pRes != 0 {
					**(**int32)(__ccgo_up(pRes)) = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&_aFlagOp[i].Fmask != uint64(0))
				}
				rc = SQLITE_OK
				break
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		break
	}
	_ = ap
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Query status information for a single database connection
//	*/
func Xsqlite3_db_status64(tls *libc.TLS, db uintptr, op int32, pCurrent uintptr, pHighwtr uintptr, resetFlag int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, i1, i2, nByte, rc int32
	var p, p1, pBt, pPager, pPager1, pPager2, pSchema, pVdbe uintptr
	var totalUsed Tsqlite3_int64
	var _ /* H at bp+0 */ int32
	var _ /* nByte at bp+4 */ int32
	var _ /* nByte at bp+8 */ int32
	var _ /* nRet at bp+16 */ Tu64
	var _ /* nRet at bp+24 */ Tu64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, i1, i2, nByte, p, p1, pBt, pPager, pPager1, pPager2, pSchema, pVdbe, rc, totalUsed
	rc = SQLITE_OK /* Return code */
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	switch op {
	case SQLITE_DBSTATUS_LOOKASIDE_USED:
		**(**int32)(__ccgo_up(bp)) = 0
		**(**Tsqlite3_int64)(__ccgo_up(pCurrent)) = int64(_sqlite3LookasideUsed(tls, db, bp))
		**(**Tsqlite3_int64)(__ccgo_up(pHighwtr)) = int64(**(**int32)(__ccgo_up(bp)))
		if resetFlag != 0 {
			p = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree
			if p != 0 {
				for (*TLookasideSlot)(unsafe.Pointer(p)).FpNext != 0 {
					p = (*TLookasideSlot)(unsafe.Pointer(p)).FpNext
				}
				(*TLookasideSlot)(unsafe.Pointer(p)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit
				(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree
				(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree = uintptr(0)
			}
			p = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree
			if p != 0 {
				for (*TLookasideSlot)(unsafe.Pointer(p)).FpNext != 0 {
					p = (*TLookasideSlot)(unsafe.Pointer(p)).FpNext
				}
				(*TLookasideSlot)(unsafe.Pointer(p)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit
				(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree
				(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = uintptr(0)
			}
		}
	case int32(SQLITE_DBSTATUS_LOOKASIDE_HIT):
		fallthrough
	case int32(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE):
		fallthrough
	case int32(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL):
		**(**Tsqlite3_int64)(__ccgo_up(pCurrent)) = 0
		**(**Tsqlite3_int64)(__ccgo_up(pHighwtr)) = int64(**(**Tu32)(__ccgo_up(db + 320 + 16 + uintptr(op-int32(SQLITE_DBSTATUS_LOOKASIDE_HIT))*4)))
		if resetFlag != 0 {
			**(**Tu32)(__ccgo_up(db + 320 + 16 + uintptr(op-int32(SQLITE_DBSTATUS_LOOKASIDE_HIT))*4)) = uint32(0)
		}
		break
		/*
		 ** Return an approximation for the amount of memory currently used
		 ** by all pagers associated with the given database connection.  The
		 ** highwater mark is meaningless and is returned as zero.
		 */
		fallthrough
	case int32(SQLITE_DBSTATUS_CACHE_USED_SHARED):
		fallthrough
	case int32(SQLITE_DBSTATUS_CACHE_USED):
		totalUsed = 0
		_sqlite3BtreeEnterAll(tls, db)
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
			if pBt != 0 {
				pPager = _sqlite3BtreePager(tls, pBt)
				nByte = _sqlite3PagerMemUsed(tls, pPager)
				if op == int32(SQLITE_DBSTATUS_CACHE_USED_SHARED) {
					nByte = nByte / _sqlite3BtreeConnectionCount(tls, pBt)
				}
				totalUsed = totalUsed + int64(nByte)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		_sqlite3BtreeLeaveAll(tls, db)
		**(**Tsqlite3_int64)(__ccgo_up(pCurrent)) = totalUsed
		**(**Tsqlite3_int64)(__ccgo_up(pHighwtr)) = 0
		break
		/*
		 ** *pCurrent gets an accurate estimate of the amount of memory used
		 ** to store the schema for all databases (main, temp, and any ATTACHed
		 ** databases.  *pHighwtr is set to zero.
		 */
		fallthrough
	case int32(SQLITE_DBSTATUS_SCHEMA_USED): /* Used to iterate through schemas */
		**(**int32)(__ccgo_up(bp + 4)) = 0 /* Used to accumulate return value */
		_sqlite3BtreeEnterAll(tls, db)
		(*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = bp + 4
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart
		i1 = 0
		for {
			if !(i1 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			pSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i1)*16))).FpSchema
			if pSchema != uintptr(0) {
				**(**int32)(__ccgo_up(bp + 4)) = int32(uint32(**(**int32)(__ccgo_up(bp + 4))) + uint32((*(*func(*libc.TLS, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxRoundup})))(tls, int32(20)))*((*TSchema)(unsafe.Pointer(pSchema)).FtblHash.Fcount+(*TSchema)(unsafe.Pointer(pSchema)).FtrigHash.Fcount+(*TSchema)(unsafe.Pointer(pSchema)).FidxHash.Fcount+(*TSchema)(unsafe.Pointer(pSchema)).FfkeyHash.Fcount))
				**(**int32)(__ccgo_up(bp + 4)) = int32(uint64(**(**int32)(__ccgo_up(bp + 4))) + Xsqlite3_msize(tls, (*TSchema)(unsafe.Pointer(pSchema)).FtblHash.Fht))
				**(**int32)(__ccgo_up(bp + 4)) = int32(uint64(**(**int32)(__ccgo_up(bp + 4))) + Xsqlite3_msize(tls, (*TSchema)(unsafe.Pointer(pSchema)).FtrigHash.Fht))
				**(**int32)(__ccgo_up(bp + 4)) = int32(uint64(**(**int32)(__ccgo_up(bp + 4))) + Xsqlite3_msize(tls, (*TSchema)(unsafe.Pointer(pSchema)).FidxHash.Fht))
				**(**int32)(__ccgo_up(bp + 4)) = int32(uint64(**(**int32)(__ccgo_up(bp + 4))) + Xsqlite3_msize(tls, (*TSchema)(unsafe.Pointer(pSchema)).FfkeyHash.Fht))
				p1 = (*THash)(unsafe.Pointer(pSchema + 40)).Ffirst
				for {
					if !(p1 != 0) {
						break
					}
					_sqlite3DeleteTrigger(tls, db, (*THashElem)(unsafe.Pointer(p1)).Fdata)
					goto _3
				_3:
					;
					p1 = (*THashElem)(unsafe.Pointer(p1)).Fnext
				}
				p1 = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst
				for {
					if !(p1 != 0) {
						break
					}
					_sqlite3DeleteTable(tls, db, (*THashElem)(unsafe.Pointer(p1)).Fdata)
					goto _4
				_4:
					;
					p1 = (*THashElem)(unsafe.Pointer(p1)).Fnext
				}
			}
			goto _2
		_2:
			;
			i1 = i1 + 1
		}
		(*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = uintptr(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd
		_sqlite3BtreeLeaveAll(tls, db)
		**(**Tsqlite3_int64)(__ccgo_up(pHighwtr)) = 0
		**(**Tsqlite3_int64)(__ccgo_up(pCurrent)) = int64(**(**int32)(__ccgo_up(bp + 4)))
		break
		/*
		 ** *pCurrent gets an accurate estimate of the amount of memory used
		 ** to store all prepared statements.
		 ** *pHighwtr is set to zero.
		 */
		fallthrough
	case int32(SQLITE_DBSTATUS_STMT_USED): /* Used to iterate through VMs */
		**(**int32)(__ccgo_up(bp + 8)) = 0 /* Used to accumulate return value */
		(*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = bp + 8
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart
		pVdbe = (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe
		for {
			if !(pVdbe != 0) {
				break
			}
			_sqlite3VdbeDelete(tls, pVdbe)
			goto _5
		_5:
			;
			pVdbe = (*TVdbe)(unsafe.Pointer(pVdbe)).FpVNext
		}
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd
		(*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = uintptr(0)
		**(**Tsqlite3_int64)(__ccgo_up(pHighwtr)) = 0 /* IMP: R-64479-57858 */
		**(**Tsqlite3_int64)(__ccgo_up(pCurrent)) = int64(**(**int32)(__ccgo_up(bp + 8)))
		break
		/*
		 ** Set *pCurrent to the total cache hits or misses encountered by all
		 ** pagers the database handle is connected to. *pHighwtr is always set
		 ** to zero.
		 */
		fallthrough
	case int32(SQLITE_DBSTATUS_CACHE_SPILL):
		op = libc.Int32FromInt32(SQLITE_DBSTATUS_CACHE_WRITE) + libc.Int32FromInt32(1)
		fallthrough
	case int32(SQLITE_DBSTATUS_CACHE_HIT):
		fallthrough
	case int32(SQLITE_DBSTATUS_CACHE_MISS):
		fallthrough
	case int32(SQLITE_DBSTATUS_CACHE_WRITE):
		**(**Tu64)(__ccgo_up(bp + 16)) = uint64(0)
		i2 = 0
		for {
			if !(i2 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i2)*16))).FpBt != 0 {
				pPager1 = _sqlite3BtreePager(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i2)*16))).FpBt)
				_sqlite3PagerCacheStat(tls, pPager1, op, resetFlag, bp+16)
			}
			goto _6
		_6:
			;
			i2 = i2 + 1
		}
		**(**Tsqlite3_int64)(__ccgo_up(pHighwtr)) = 0 /* IMP: R-42420-56072 */
		/* IMP: R-54100-20147 */
		/* IMP: R-29431-39229 */
		**(**Tsqlite3_int64)(__ccgo_up(pCurrent)) = int64(**(**Tu64)(__ccgo_up(bp + 16)))
		break
		/* Set *pCurrent to the number of bytes that the db database connection
		 ** has spilled to the filesystem in temporary files that could have been
		 ** stored in memory, had sufficient memory been available.
		 ** The *pHighwater is always set to zero.
		 */
		fallthrough
	case int32(SQLITE_DBSTATUS_TEMPBUF_SPILL):
		**(**Tu64)(__ccgo_up(bp + 24)) = uint64(0)
		if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt != 0 {
			pPager2 = _sqlite3BtreePager(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt)
			_sqlite3PagerCacheStat(tls, pPager2, int32(SQLITE_DBSTATUS_CACHE_WRITE), resetFlag, bp+24)
			**(**Tu64)(__ccgo_up(bp + 24)) = **(**Tu64)(__ccgo_up(bp + 24)) * uint64(_sqlite3BtreeGetPageSize(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt))
		}
		**(**Tu64)(__ccgo_up(bp + 24)) = **(**Tu64)(__ccgo_up(bp + 24)) + (*Tsqlite3)(unsafe.Pointer(db)).FnSpill
		if resetFlag != 0 {
			(*Tsqlite3)(unsafe.Pointer(db)).FnSpill = uint64(0)
		}
		**(**Tsqlite3_int64)(__ccgo_up(pHighwtr)) = 0
		**(**Tsqlite3_int64)(__ccgo_up(pCurrent)) = int64(**(**Tu64)(__ccgo_up(bp + 24)))
		break
		/* Set *pCurrent to non-zero if there are unresolved deferred foreign
		 ** key constraints.  Set *pCurrent to zero if all foreign key constraints
		 ** have been satisfied.  The *pHighwtr is always set to zero.
		 */
		fallthrough
	case int32(SQLITE_DBSTATUS_DEFERRED_FKS):
		**(**Tsqlite3_int64)(__ccgo_up(pHighwtr)) = 0 /* IMP: R-11967-56545 */
		**(**Tsqlite3_int64)(__ccgo_up(pCurrent)) = libc.BoolInt64((*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons > 0 || (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons > 0)
	default:
		rc = int32(SQLITE_ERROR)
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** This function is used to set the schema of a virtual table.  It is only
//	** valid to call this function from within the xCreate() or xConnect() of a
//	** virtual table module.
//	*/
func Xsqlite3_declare_vtab(tls *libc.TLS, db uintptr, zCreateTable uintptr) (r int32) {
	bp := tls.Alloc(304)
	defer tls.Free(304)
	var i, initBusy, rc int32
	var pCtx, pIdx, pNew, pTab, z, v3 uintptr
	var v2 Ti16
	var _ /* sParse at bp+0 */ TParse
	var _ /* tokenType at bp+284 */ int32
	_, _, _, _, _, _, _, _, _, _ = i, initBusy, pCtx, pIdx, pNew, pTab, rc, z, v2, v3
	rc = SQLITE_OK
	/* Verify that the first two keywords in the CREATE TABLE statement
	 ** really are "CREATE" and "TABLE".  If this is not the case, then
	 ** sqlite3_declare_vtab() is being misused.
	 */
	z = zCreateTable
	i = 0
	for {
		if !(_aKeyword1[i] != 0) {
			break
		}
		**(**int32)(__ccgo_up(bp + 284)) = 0
		for cond := true; cond; cond = **(**int32)(__ccgo_up(bp + 284)) == int32(TK_SPACE) || **(**int32)(__ccgo_up(bp + 284)) == int32(TK_COMMENT) {
			z = z + uintptr(_sqlite3GetToken(tls, z, bp+284))
		}
		if **(**int32)(__ccgo_up(bp + 284)) != int32(_aKeyword1[i]) {
			_sqlite3ErrorWithMsg(tls, db, int32(SQLITE_ERROR), __ccgo_ts+24839, 0)
			return int32(SQLITE_ERROR)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	pCtx = (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx
	if !(pCtx != 0) || (*TVtabCtx)(unsafe.Pointer(pCtx)).FbDeclared != 0 {
		_sqlite3Error(tls, db, _sqlite3MisuseError(tls, int32(162730)))
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
		return _sqlite3MisuseError(tls, int32(162732))
	}
	pTab = (*TVtabCtx)(unsafe.Pointer(pCtx)).FpTab
	_sqlite3ParseObjectInit(tls, bp, db)
	(**(**TParse)(__ccgo_up(bp))).FeParseMode = uint8(PARSE_MODE_DECLARE_VTAB)
	libc.SetBitFieldPtr16Uint32(bp+28, libc.Uint32FromInt32(1), 0, 0x1)
	/* We should never be able to reach this point while loading the
	 ** schema.  Nevertheless, defend against that (turn off db->init.busy)
	 ** in case a bug arises. */
	initBusy = int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy)
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(0)
	(**(**TParse)(__ccgo_up(bp))).FnQueryLoop = int16(1)
	if SQLITE_OK == _sqlite3RunParser(tls, bp, zCreateTable) {
		if !((*TTable)(unsafe.Pointer(pTab)).FaCol != 0) {
			pNew = (**(**TParse)(__ccgo_up(bp))).FpNewTable
			(*TTable)(unsafe.Pointer(pTab)).FaCol = (*TTable)(unsafe.Pointer(pNew)).FaCol
			_sqlite3ExprListDelete(tls, db, (*(*struct {
				FaddColOffset int32
				FpFKey        uintptr
				FpDfltList    uintptr
			})(unsafe.Pointer(pNew + 44))).FpDfltList)
			v2 = (*TTable)(unsafe.Pointer(pNew)).FnCol
			(*TTable)(unsafe.Pointer(pTab)).FnCol = v2
			(*TTable)(unsafe.Pointer(pTab)).FnNVCol = v2
			**(**Tu32)(__ccgo_up(pTab + 28)) |= (*TTable)(unsafe.Pointer(pNew)).FtabFlags & uint32(libc.Int32FromInt32(TF_WithoutRowid)|libc.Int32FromInt32(TF_NoVisibleRowid))
			(*TTable)(unsafe.Pointer(pNew)).FnCol = 0
			(*TTable)(unsafe.Pointer(pNew)).FaCol = uintptr(0)
			if !((*TTable)(unsafe.Pointer(pNew)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer((*TVtabCtx)(unsafe.Pointer(pCtx)).FpVTable)).FpMod)).FpModule)).FxUpdate != uintptr(0) && int32((*TIndex)(unsafe.Pointer(_sqlite3PrimaryKeyIndex(tls, pNew))).FnKeyCol) != int32(1) {
				/* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
				 ** or else must have a single-column PRIMARY KEY */
				rc = int32(SQLITE_ERROR)
			}
			pIdx = (*TTable)(unsafe.Pointer(pNew)).FpIndex
			if pIdx != 0 {
				(*TTable)(unsafe.Pointer(pTab)).FpIndex = pIdx
				(*TTable)(unsafe.Pointer(pNew)).FpIndex = uintptr(0)
				(*TIndex)(unsafe.Pointer(pIdx)).FpTable = pTab
			}
		}
		(*TVtabCtx)(unsafe.Pointer(pCtx)).FbDeclared = int32(1)
	} else {
		if (**(**TParse)(__ccgo_up(bp))).FzErrMsg != 0 {
			v3 = __ccgo_ts + 4729
		} else {
			v3 = uintptr(0)
		}
		_sqlite3ErrorWithMsg(tls, db, int32(SQLITE_ERROR), v3, libc.VaList(bp+296, (**(**TParse)(__ccgo_up(bp))).FzErrMsg))
		_sqlite3DbFree(tls, db, (**(**TParse)(__ccgo_up(bp))).FzErrMsg)
		rc = int32(SQLITE_ERROR)
	}
	(**(**TParse)(__ccgo_up(bp))).FeParseMode = uint8(PARSE_MODE_NORMAL)
	if (**(**TParse)(__ccgo_up(bp))).FpVdbe != 0 {
		_sqlite3VdbeFinalize(tls, (**(**TParse)(__ccgo_up(bp))).FpVdbe)
	}
	_sqlite3DeleteTable(tls, db, (**(**TParse)(__ccgo_up(bp))).FpNewTable)
	_sqlite3ParseObjectReset(tls, bp)
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(initBusy)
	rc = _sqlite3ApiExit(tls, db, rc)
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/* Convert zSchema to a MemDB and initialize its content.
//	*/
func Xsqlite3_deserialize(tls *libc.TLS, db uintptr, zSchema uintptr, pData uintptr, szDb Tsqlite3_int64, szBuf Tsqlite3_int64, mFlags uint32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var iDb, rc int32
	var p, pStore, zSql uintptr
	var _ /* pStmt at bp+0 */ uintptr
	_, _, _, _, _ = iDb, p, pStore, rc, zSql
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	if zSchema == uintptr(0) {
		zSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FzDbSName
	}
	iDb = _sqlite3FindDbName(tls, db, zSchema)
	if iDb < int32(2) && iDb != 0 {
		rc = int32(SQLITE_ERROR)
		goto end_deserialize
	}
	zSql = Xsqlite3_mprintf(tls, __ccgo_ts+5398, libc.VaList(bp+16, zSchema))
	if zSql == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp, uintptr(0))
		Xsqlite3_free(tls, zSql)
	}
	if rc != 0 {
		goto end_deserialize
	}
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(iDb)
	libc.SetBitFieldPtr8Uint32(db+176+8, libc.Uint32FromInt32(1), 3, 0x8)
	Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp)))
	libc.SetBitFieldPtr8Uint32(db+176+8, libc.Uint32FromInt32(0), 3, 0x8)
	rc = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	if rc != SQLITE_OK {
		goto end_deserialize
	}
	p = _memdbFromDbSchema(tls, db, zSchema)
	if p == uintptr(0) {
		rc = int32(SQLITE_ERROR)
	} else {
		pStore = (*TMemFile)(unsafe.Pointer(p)).FpStore
		(*TMemStore)(unsafe.Pointer(pStore)).FaData = pData
		pData = uintptr(0)
		(*TMemStore)(unsafe.Pointer(pStore)).Fsz = szDb
		(*TMemStore)(unsafe.Pointer(pStore)).FszAlloc = szBuf
		(*TMemStore)(unsafe.Pointer(pStore)).FszMax = szBuf
		if (*TMemStore)(unsafe.Pointer(pStore)).FszMax < _sqlite3Config.FmxMemdbSize {
			(*TMemStore)(unsafe.Pointer(pStore)).FszMax = _sqlite3Config.FmxMemdbSize
		}
		(*TMemStore)(unsafe.Pointer(pStore)).FmFlags = mFlags
		rc = SQLITE_OK
	}
	goto end_deserialize
end_deserialize:
	;
	if pData != 0 && mFlags&uint32(SQLITE_DESERIALIZE_FREEONCLOSE) != uint32(0) {
		Xsqlite3_free(tls, pData)
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Enable or disable extension loading.  Extension loading is disabled by
//	** default so as not to open security holes in older applications.
//	*/
func Xsqlite3_enable_load_extension(tls *libc.TLS, db uintptr, onoff int32) (r int32) {
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	if onoff != 0 {
		**(**Tu64)(__ccgo_up(db + 32)) |= uint64(libc.Int32FromInt32(SQLITE_LoadExtension) | libc.Int32FromInt32(SQLITE_LoadExtFunc))
	} else {
		**(**Tu64)(__ccgo_up(db + 32)) &= ^uint64(libc.Int32FromInt32(SQLITE_LoadExtension) | libc.Int32FromInt32(SQLITE_LoadExtFunc))
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Execute SQL code.  Return one of the SQLITE_ success/failure
//	** codes.  Also write an error message into memory obtained from
//	** malloc() and make *pzErrMsg point to that message.
//	**
//	** If the SQL is a query, then for each row in the query result
//	** the xCallback() function is called.  pArg becomes the first
//	** argument to xCallback().  If xCallback=NULL then no callback
//	** is invoked, even for queries.
//	*/
func Xsqlite3_exec(tls *libc.TLS, db uintptr, zSql uintptr, __ccgo_fp_xCallback Tsqlite3_callback, pArg uintptr, pzErrMsg uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var azCols, azVals uintptr
	var callbackIsInit, i, nCol, rc int32
	var _ /* pStmt at bp+4 */ uintptr
	var _ /* zLeftover at bp+0 */ uintptr
	_, _, _, _, _, _ = azCols, azVals, callbackIsInit, i, nCol, rc
	rc = SQLITE_OK                                /* Tail of unprocessed SQL */
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* The current SQL statement */
	azCols = uintptr(0)                           /* True if callback data is initialized */
	if !(_sqlite3SafetyCheckOk(tls, db) != 0) {
		return _sqlite3MisuseError(tls, int32(142334))
	}
	if zSql == uintptr(0) {
		zSql = __ccgo_ts + 1711
	}
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	_sqlite3Error(tls, db, SQLITE_OK)
	for rc == SQLITE_OK && **(**int8)(__ccgo_up(zSql)) != 0 {
		nCol = 0
		azVals = uintptr(0)
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
		rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp+4, bp)
		if rc != SQLITE_OK {
			continue
		}
		if !(**(**uintptr)(__ccgo_up(bp + 4)) != 0) {
			/* this happens for a comment or white-space */
			zSql = **(**uintptr)(__ccgo_up(bp))
			continue
		}
		callbackIsInit = 0
		for int32(1) != 0 {
			rc = Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4)))
			/* Invoke the callback function if required */
			if __ccgo_fp_xCallback != 0 && (int32(SQLITE_ROW) == rc || int32(SQLITE_DONE) == rc && !(callbackIsInit != 0) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_NullCallback) != 0) {
				if !(callbackIsInit != 0) {
					nCol = Xsqlite3_column_count(tls, **(**uintptr)(__ccgo_up(bp + 4)))
					azCols = _sqlite3DbMallocRaw(tls, db, uint64(uint32(libc.Int32FromInt32(2)*nCol+libc.Int32FromInt32(1))*uint32(4)))
					if azCols == uintptr(0) {
						goto exec_out
					}
					i = 0
					for {
						if !(i < nCol) {
							break
						}
						**(**uintptr)(__ccgo_up(azCols + uintptr(i)*4)) = Xsqlite3_column_name(tls, **(**uintptr)(__ccgo_up(bp + 4)), i)
						/* sqlite3VdbeSetColName() installs column names as UTF8
						 ** strings so there is no way for sqlite3_column_name() to fail. */
						goto _1
					_1:
						;
						i = i + 1
					}
					callbackIsInit = int32(1)
				}
				if rc == int32(SQLITE_ROW) {
					azVals = azCols + uintptr(nCol)*4
					i = 0
					for {
						if !(i < nCol) {
							break
						}
						**(**uintptr)(__ccgo_up(azVals + uintptr(i)*4)) = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp + 4)), i)
						if !(**(**uintptr)(__ccgo_up(azVals + uintptr(i)*4)) != 0) && Xsqlite3_column_type(tls, **(**uintptr)(__ccgo_up(bp + 4)), i) != int32(SQLITE_NULL) {
							_sqlite3OomFault(tls, db)
							goto exec_out
						}
						goto _2
					_2:
						;
						i = i + 1
					}
					**(**uintptr)(__ccgo_up(azVals + uintptr(i)*4)) = uintptr(0)
				}
				if (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xCallback})))(tls, pArg, nCol, azVals, azCols) != 0 {
					/* EVIDENCE-OF: R-38229-40159 If the callback function to
					 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
					 ** return SQLITE_ABORT. */
					rc = int32(SQLITE_ABORT)
					_sqlite3VdbeFinalize(tls, **(**uintptr)(__ccgo_up(bp + 4)))
					**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
					_sqlite3Error(tls, db, int32(SQLITE_ABORT))
					goto exec_out
				}
			}
			if rc != int32(SQLITE_ROW) {
				rc = _sqlite3VdbeFinalize(tls, **(**uintptr)(__ccgo_up(bp + 4)))
				**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
				zSql = **(**uintptr)(__ccgo_up(bp))
				for int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zSql)))])&int32(0x01) != 0 {
					zSql = zSql + 1
				}
				break
			}
		}
		_sqlite3DbFree(tls, db, azCols)
		azCols = uintptr(0)
	}
	goto exec_out
exec_out:
	;
	if **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
		_sqlite3VdbeFinalize(tls, **(**uintptr)(__ccgo_up(bp + 4)))
	}
	_sqlite3DbFree(tls, db, azCols)
	rc = _sqlite3ApiExit(tls, db, rc)
	if rc != SQLITE_OK && pzErrMsg != 0 {
		**(**uintptr)(__ccgo_up(pzErrMsg)) = _sqlite3DbStrDup(tls, uintptr(0), Xsqlite3_errmsg(tls, db))
		if **(**uintptr)(__ccgo_up(pzErrMsg)) == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
			_sqlite3Error(tls, db, int32(SQLITE_NOMEM))
		}
	} else {
		if pzErrMsg != 0 {
			**(**uintptr)(__ccgo_up(pzErrMsg)) = uintptr(0)
		}
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Query the database.  But instead of invoking a callback for each row,
//	** malloc() for space to hold the result and return the entire results
//	** at the conclusion of the call.
//	**
//	** The result that is written to ***pazResult is held in memory obtained
//	** from malloc().  But the caller cannot free this memory directly.
//	** Instead, the entire table should be passed to sqlite3_free_table() when
//	** the calling procedure is finished using it.
//	*/
func Xsqlite3_get_table(tls *libc.TLS, db uintptr, zSql uintptr, pazResult uintptr, pnRow uintptr, pnColumn uintptr, pzErrMsg uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var azNew uintptr
	var rc int32
	var _ /* res at bp+0 */ TTabResult
	_, _ = azNew, rc
	**(**uintptr)(__ccgo_up(pazResult)) = uintptr(0)
	if pnColumn != 0 {
		**(**int32)(__ccgo_up(pnColumn)) = 0
	}
	if pnRow != 0 {
		**(**int32)(__ccgo_up(pnRow)) = 0
	}
	if pzErrMsg != 0 {
		**(**uintptr)(__ccgo_up(pzErrMsg)) = uintptr(0)
	}
	(**(**TTabResult)(__ccgo_up(bp))).FzErrMsg = uintptr(0)
	(**(**TTabResult)(__ccgo_up(bp))).FnRow = uint32(0)
	(**(**TTabResult)(__ccgo_up(bp))).FnColumn = uint32(0)
	(**(**TTabResult)(__ccgo_up(bp))).FnData = uint32(1)
	(**(**TTabResult)(__ccgo_up(bp))).FnAlloc = uint32(20)
	(**(**TTabResult)(__ccgo_up(bp))).Frc = SQLITE_OK
	(**(**TTabResult)(__ccgo_up(bp))).FazResult = Xsqlite3_malloc64(tls, uint64(uint32(4)*(**(**TTabResult)(__ccgo_up(bp))).FnAlloc))
	if (**(**TTabResult)(__ccgo_up(bp))).FazResult == uintptr(0) {
		(*Tsqlite3)(unsafe.Pointer(db)).FerrCode = int32(SQLITE_NOMEM)
		return int32(SQLITE_NOMEM)
	}
	**(**uintptr)(__ccgo_up((**(**TTabResult)(__ccgo_up(bp))).FazResult)) = uintptr(0)
	rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_sqlite3_get_table_cb), bp, pzErrMsg)
	**(**uintptr)(__ccgo_up((**(**TTabResult)(__ccgo_up(bp))).FazResult)) = uintptr(int32((**(**TTabResult)(__ccgo_up(bp))).FnData))
	if rc&int32(0xff) == int32(SQLITE_ABORT) {
		Xsqlite3_free_table(tls, (**(**TTabResult)(__ccgo_up(bp))).FazResult+1*4)
		if (**(**TTabResult)(__ccgo_up(bp))).FzErrMsg != 0 {
			if pzErrMsg != 0 {
				Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(pzErrMsg)))
				**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+40, (**(**TTabResult)(__ccgo_up(bp))).FzErrMsg))
			}
			Xsqlite3_free(tls, (**(**TTabResult)(__ccgo_up(bp))).FzErrMsg)
		}
		(*Tsqlite3)(unsafe.Pointer(db)).FerrCode = (**(**TTabResult)(__ccgo_up(bp))).Frc /* Assume 32-bit assignment is atomic */
		return (**(**TTabResult)(__ccgo_up(bp))).Frc
	}
	Xsqlite3_free(tls, (**(**TTabResult)(__ccgo_up(bp))).FzErrMsg)
	if rc != SQLITE_OK {
		Xsqlite3_free_table(tls, (**(**TTabResult)(__ccgo_up(bp))).FazResult+1*4)
		return rc
	}
	if (**(**TTabResult)(__ccgo_up(bp))).FnAlloc > (**(**TTabResult)(__ccgo_up(bp))).FnData {
		azNew = _sqlite3Realloc(tls, (**(**TTabResult)(__ccgo_up(bp))).FazResult, uint64(uint32(4)*(**(**TTabResult)(__ccgo_up(bp))).FnData))
		if azNew == uintptr(0) {
			Xsqlite3_free_table(tls, (**(**TTabResult)(__ccgo_up(bp))).FazResult+1*4)
			(*Tsqlite3)(unsafe.Pointer(db)).FerrCode = int32(SQLITE_NOMEM)
			return int32(SQLITE_NOMEM)
		}
		(**(**TTabResult)(__ccgo_up(bp))).FazResult = azNew
	}
	**(**uintptr)(__ccgo_up(pazResult)) = (**(**TTabResult)(__ccgo_up(bp))).FazResult + 1*4
	if pnColumn != 0 {
		**(**int32)(__ccgo_up(pnColumn)) = int32((**(**TTabResult)(__ccgo_up(bp))).FnColumn)
	}
	if pnRow != 0 {
		**(**int32)(__ccgo_up(pnRow)) = int32((**(**TTabResult)(__ccgo_up(bp))).FnRow)
	}
	return rc
}

// C documentation
//
//	/*
//	** Initialize and deinitialize the operating system interface.
//	*/
func Xsqlite3_os_init(tls *libc.TLS) (r int32) {
	/* Double-check that the aSyscall[] array has been constructed
	 ** correctly.  See ticket [bb3a86e890c8e96ab] */
	/* get memory map allocation granularity */
	libc.Xmemset(tls, uintptr(unsafe.Pointer(&_winSysInfo)), 0, uint32(36))
	(*(*func(*libc.TLS, TLPSYSTEM_INFO))(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(28)].FpCurrent})))(tls, uintptr(unsafe.Pointer(&_winSysInfo)))
	Xsqlite3_vfs_register(tls, uintptr(unsafe.Pointer(&_winVfs)), int32(1))
	Xsqlite3_vfs_register(tls, uintptr(unsafe.Pointer(&_winLongPathVfs)), 0)
	Xsqlite3_vfs_register(tls, uintptr(unsafe.Pointer(&_winNolockVfs)), 0)
	Xsqlite3_vfs_register(tls, uintptr(unsafe.Pointer(&_winLongPathNolockVfs)), 0)
	_winBigLock = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This function is called from within a pre-update callback to retrieve
//	** a field of the row currently being updated or inserted.
//	*/
func Xsqlite3_preupdate_new(tls *libc.TLS, db uintptr, iIdx int32, ppValue uintptr) (r int32) {
	var iStore, rc, v1 int32
	var p, pData, pMem, pUnpack uintptr
	_, _, _, _, _, _, _ = iStore, p, pData, pMem, pUnpack, rc, v1
	rc = SQLITE_OK
	iStore = 0
	p = (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate
	if !(p != 0) || (*TPreUpdate)(unsafe.Pointer(p)).Fop == int32(SQLITE_DELETE) {
		rc = _sqlite3MisuseError(tls, int32(96071))
		goto preupdate_new_out
	}
	if (*TPreUpdate)(unsafe.Pointer(p)).FpPk != 0 && (*TPreUpdate)(unsafe.Pointer(p)).Fop != int32(SQLITE_UPDATE) {
		iStore = _sqlite3TableColumnToIndex(tls, (*TPreUpdate)(unsafe.Pointer(p)).FpPk, iIdx)
	} else {
		if iIdx >= int32((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FnCol) {
			return _sqlite3MisuseError(tls, int32(96077))
		} else {
			iStore = int32(_sqlite3TableColumnToStorage(tls, (*TPreUpdate)(unsafe.Pointer(p)).FpTab, int16(iIdx)))
		}
	}
	if iStore >= int32((*TVdbeCursor)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr)).FnField) || iStore < 0 {
		rc = int32(SQLITE_RANGE)
		goto preupdate_new_out
	}
	if (*TPreUpdate)(unsafe.Pointer(p)).Fop == int32(SQLITE_INSERT) {
		/* For an INSERT, memory cell p->iNewReg contains the serialized record
		 ** that is being inserted. Deserialize it. */
		pUnpack = (*TPreUpdate)(unsafe.Pointer(p)).FpNewUnpacked
		if !(pUnpack != 0) {
			pData = (*TVdbe)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).Fv)).FaMem + uintptr((*TPreUpdate)(unsafe.Pointer(p)).FiNewReg)*40
			if int32((*TMem)(unsafe.Pointer(pData)).Fflags)&int32(MEM_Zero) != 0 {
				v1 = _sqlite3VdbeMemExpandBlob(tls, pData)
			} else {
				v1 = 0
			}
			rc = v1
			if rc != SQLITE_OK {
				goto preupdate_new_out
			}
			pUnpack = _vdbeUnpackRecord(tls, (*TPreUpdate)(unsafe.Pointer(p)).FpKeyinfo, (*TMem)(unsafe.Pointer(pData)).Fn, (*TMem)(unsafe.Pointer(pData)).Fz)
			if !(pUnpack != 0) {
				rc = int32(SQLITE_NOMEM)
				goto preupdate_new_out
			}
			(*TPreUpdate)(unsafe.Pointer(p)).FpNewUnpacked = pUnpack
		}
		pMem = (*TUnpackedRecord)(unsafe.Pointer(pUnpack)).FaMem + uintptr(iStore)*40
		if iIdx == int32((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FiPKey) {
			_sqlite3VdbeMemSetInt64(tls, pMem, (*TPreUpdate)(unsafe.Pointer(p)).FiKey2)
		} else {
			if iStore >= int32((*TUnpackedRecord)(unsafe.Pointer(pUnpack)).FnField) {
				pMem = _columnNullValue(tls)
			}
		}
	} else {
		/* For an UPDATE, memory cell (p->iNewReg+1+iStore) contains the required
		 ** value. Make a copy of the cell contents and return a pointer to it.
		 ** It is not safe to return a pointer to the memory cell itself as the
		 ** caller may modify the value text encoding.
		 */
		if !((*TPreUpdate)(unsafe.Pointer(p)).FaNew != 0) {
			(*TPreUpdate)(unsafe.Pointer(p)).FaNew = _sqlite3DbMallocZero(tls, db, uint64(uint32(40)*uint32((*TVdbeCursor)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr)).FnField)))
			if !((*TPreUpdate)(unsafe.Pointer(p)).FaNew != 0) {
				rc = int32(SQLITE_NOMEM)
				goto preupdate_new_out
			}
		}
		pMem = (*TPreUpdate)(unsafe.Pointer(p)).FaNew + uintptr(iStore)*40
		if int32((*TMem)(unsafe.Pointer(pMem)).Fflags) == 0 {
			if iIdx == int32((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FiPKey) {
				_sqlite3VdbeMemSetInt64(tls, pMem, (*TPreUpdate)(unsafe.Pointer(p)).FiKey2)
			} else {
				rc = _sqlite3VdbeMemCopy(tls, pMem, (*TVdbe)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).Fv)).FaMem+uintptr((*TPreUpdate)(unsafe.Pointer(p)).FiNewReg+int32(1)+iStore)*40)
				if rc != SQLITE_OK {
					goto preupdate_new_out
				}
			}
		}
	}
	**(**uintptr)(__ccgo_up(ppValue)) = pMem
	goto preupdate_new_out
preupdate_new_out:
	;
	_sqlite3Error(tls, db, rc)
	return _sqlite3ApiExit(tls, db, rc)
}

/************** End of vdbeapi.c *********************************************/
/************** Begin file vdbetrace.c ***************************************/
/*
** 2009 November 25
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains code used to insert the values of host parameters
** (aka "wildcards") into the SQL text output by sqlite3_trace().
**
** The Vdbe parse-tree explainer is also found here.
 */
/* #include "sqliteInt.h" */
/* #include "vdbeInt.h" */

// C documentation
//
//	/*
//	** This function is called from within a pre-update callback to retrieve
//	** a field of the row currently being updated or deleted.
//	*/
func Xsqlite3_preupdate_old(tls *libc.TLS, db uintptr, iIdx int32, ppValue uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aRec, p, pCol, pDflt, pMem, v1 uintptr
	var iStore, nByte, rc int32
	var nRec Tu32
	var _ /* pVal at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _ = aRec, iStore, nByte, nRec, p, pCol, pDflt, pMem, rc, v1
	rc = SQLITE_OK
	iStore = 0
	p = (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate
	/* Test that this call is being made from within an SQLITE_DELETE or
	 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
	if !(p != 0) || (*TPreUpdate)(unsafe.Pointer(p)).Fop == int32(SQLITE_INSERT) {
		rc = _sqlite3MisuseError(tls, int32(95913))
		goto preupdate_old_out
	}
	if (*TPreUpdate)(unsafe.Pointer(p)).FpPk != 0 {
		iStore = _sqlite3TableColumnToIndex(tls, (*TPreUpdate)(unsafe.Pointer(p)).FpPk, iIdx)
	} else {
		if iIdx >= int32((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FnCol) {
			rc = _sqlite3MisuseError(tls, int32(95919))
			goto preupdate_old_out
		} else {
			iStore = int32(_sqlite3TableColumnToStorage(tls, (*TPreUpdate)(unsafe.Pointer(p)).FpTab, int16(iIdx)))
		}
	}
	if iStore >= int32((*TVdbeCursor)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr)).FnField) || iStore < 0 {
		rc = int32(SQLITE_RANGE)
		goto preupdate_old_out
	}
	if iIdx == int32((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FiPKey) {
		v1 = p + 56
		pMem = v1
		**(**uintptr)(__ccgo_up(ppValue)) = v1
		_sqlite3VdbeMemSetInt64(tls, pMem, (*TPreUpdate)(unsafe.Pointer(p)).FiKey1)
	} else {
		/* If the old.* record has not yet been loaded into memory, do so now. */
		if (*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked == uintptr(0) {
			nRec = _sqlite3BtreePayloadSize(tls, *(*uintptr)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr + 36)))
			aRec = _sqlite3DbMallocRaw(tls, db, uint64(nRec))
			if !(aRec != 0) {
				goto preupdate_old_out
			}
			rc = _sqlite3BtreePayload(tls, *(*uintptr)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr + 36)), uint32(0), nRec, aRec)
			if rc == SQLITE_OK {
				(*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked = _vdbeUnpackRecord(tls, (*TPreUpdate)(unsafe.Pointer(p)).FpKeyinfo, int32(nRec), aRec)
				if !((*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked != 0) {
					rc = int32(SQLITE_NOMEM)
				}
			}
			if rc != SQLITE_OK {
				_sqlite3DbFree(tls, db, aRec)
				goto preupdate_old_out
			}
			(*TPreUpdate)(unsafe.Pointer(p)).FaRecord = aRec
		}
		v1 = (*TUnpackedRecord)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked)).FaMem + uintptr(iStore)*40
		**(**uintptr)(__ccgo_up(ppValue)) = v1
		pMem = v1
		if iStore >= int32((*TUnpackedRecord)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked)).FnField) {
			/* This occurs when the table has been extended using ALTER TABLE
			 ** ADD COLUMN. The value to return is the default value of the column. */
			pCol = (*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FaCol + uintptr(iIdx)*12
			if int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) > 0 {
				if (*TPreUpdate)(unsafe.Pointer(p)).FapDflt == uintptr(0) {
					nByte = int32(uint32(4) * uint32((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FnCol))
					(*TPreUpdate)(unsafe.Pointer(p)).FapDflt = _sqlite3DbMallocZero(tls, db, uint64(nByte))
					if (*TPreUpdate)(unsafe.Pointer(p)).FapDflt == uintptr(0) {
						goto preupdate_old_out
					}
				}
				if **(**uintptr)(__ccgo_up((*TPreUpdate)(unsafe.Pointer(p)).FapDflt + uintptr(iIdx)*4)) == uintptr(0) {
					**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
					pDflt = (*(*TExprList_item)(unsafe.Pointer((*(*struct {
						FaddColOffset int32
						FpFKey        uintptr
						FpDfltList    uintptr
					})(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab + 44))).FpDfltList + 8 + uintptr(int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt)-int32(1))*20))).FpExpr
					rc = _sqlite3ValueFromExpr(tls, db, pDflt, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uint8((*TColumn)(unsafe.Pointer(pCol)).Faffinity), bp)
					if rc == SQLITE_OK && **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
						rc = _sqlite3CorruptError(tls, int32(95975))
					}
					**(**uintptr)(__ccgo_up((*TPreUpdate)(unsafe.Pointer(p)).FapDflt + uintptr(iIdx)*4)) = **(**uintptr)(__ccgo_up(bp))
				}
				**(**uintptr)(__ccgo_up(ppValue)) = **(**uintptr)(__ccgo_up((*TPreUpdate)(unsafe.Pointer(p)).FapDflt + uintptr(iIdx)*4))
			} else {
				**(**uintptr)(__ccgo_up(ppValue)) = _columnNullValue(tls)
			}
		} else {
			if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpTab)).FaCol + uintptr(iIdx)*12))).Faffinity) == int32(SQLITE_AFF_REAL) {
				if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
					_sqlite3VdbeMemRealify(tls, pMem)
				}
			}
		}
	}
	goto preupdate_old_out
preupdate_old_out:
	;
	_sqlite3Error(tls, db, rc)
	return _sqlite3ApiExit(tls, db, rc)
}

// C documentation
//
//	/*
//	** Return N random bytes.
//	*/
func Xsqlite3_randomness(tls *libc.TLS, N int32, pBuf uintptr) {
	var mutex, pVfs, zBuf, v1 uintptr
	_, _, _, _ = mutex, pVfs, zBuf, v1
	zBuf = pBuf
	if Xsqlite3_initialize(tls) != 0 {
		return
	}
	mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_PRNG))
	Xsqlite3_mutex_enter(tls, mutex)
	if N <= 0 || pBuf == uintptr(0) {
		**(**Tu32)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Prng)))) = uint32(0)
		Xsqlite3_mutex_leave(tls, mutex)
		return
	}
	/* Initialize the state of the random number generator once,
	 ** the first time this routine is called.
	 */
	if **(**Tu32)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Prng)))) == uint32(0) {
		pVfs = Xsqlite3_vfs_find(tls, uintptr(0))
		libc.Xmemcpy(tls, uintptr(unsafe.Pointer(&_sqlite3Prng)), uintptr(unsafe.Pointer(&_chacha20_init)), uint32(16))
		if pVfs == uintptr(0) {
			libc.Xmemset(tls, uintptr(unsafe.Pointer(&_sqlite3Prng))+4*4, 0, uint32(44))
		} else {
			_sqlite3OsRandomness(tls, pVfs, int32(44), uintptr(unsafe.Pointer(&_sqlite3Prng))+4*4)
		}
		**(**Tu32)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Prng)) + 15*4)) = **(**Tu32)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Prng)) + 12*4))
		**(**Tu32)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Prng)) + 12*4)) = uint32(0)
		_sqlite3Prng.Fn = uint8(0)
	}
	for int32(1) != 0 {
		if N <= int32(_sqlite3Prng.Fn) {
			libc.Xmemcpy(tls, zBuf, uintptr(unsafe.Pointer(&_sqlite3Prng))+64+uintptr(int32(_sqlite3Prng.Fn)-N), uint32(N))
			v1 = uintptr(unsafe.Pointer(&_sqlite3Prng)) + 128
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) - N)
			break
		}
		if int32(_sqlite3Prng.Fn) > 0 {
			libc.Xmemcpy(tls, zBuf, uintptr(unsafe.Pointer(&_sqlite3Prng))+64, uint32(_sqlite3Prng.Fn))
			N = N - int32(_sqlite3Prng.Fn)
			zBuf = zBuf + uintptr(_sqlite3Prng.Fn)
		}
		**(**Tu32)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Prng)) + 12*4)) = **(**Tu32)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Prng)) + 12*4)) + 1
		_chacha_block(tls, uintptr(unsafe.Pointer(&_sqlite3Prng))+64, uintptr(unsafe.Pointer(&_sqlite3Prng)))
		_sqlite3Prng.Fn = uint8(64)
	}
	Xsqlite3_mutex_leave(tls, mutex)
}

func Xsqlite3_result_zeroblob64(tls *libc.TLS, pCtx uintptr, n Tu64) (r int32) {
	var pOut uintptr
	_ = pOut
	pOut = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut
	if n > uint64(**(**int32)(__ccgo_up((*TMem)(unsafe.Pointer(pOut)).Fdb + 120))) {
		Xsqlite3_result_error_toobig(tls, pCtx)
		return int32(SQLITE_TOOBIG)
	}
	_sqlite3VdbeMemSetZeroBlob(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, int32(n))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Return the serialization of a database
//	*/
func Xsqlite3_serialize(tls *libc.TLS, db uintptr, zSchema uintptr, piSize uintptr, mFlags uint32) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var iDb, nPage, pgno, rc, szPage, v1 int32
	var p, pBt, pOut, pPager, pStore, pTo, zSql uintptr
	var sz Tsqlite3_int64
	var _ /* pPage at bp+4 */ uintptr
	var _ /* pStmt at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = iDb, nPage, p, pBt, pOut, pPager, pStore, pTo, pgno, rc, sz, szPage, zSql, v1
	szPage = 0
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	pOut = uintptr(0)
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	if zSchema == uintptr(0) {
		zSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FzDbSName
	}
	p = _memdbFromDbSchema(tls, db, zSchema)
	iDb = _sqlite3FindDbName(tls, db, zSchema)
	if piSize != 0 {
		**(**Tsqlite3_int64)(__ccgo_up(piSize)) = int64(-int32(1))
	}
	if iDb < 0 {
		goto serialize_out
	}
	if p != 0 {
		pStore = (*TMemFile)(unsafe.Pointer(p)).FpStore
		if piSize != 0 {
			**(**Tsqlite3_int64)(__ccgo_up(piSize)) = (*TMemStore)(unsafe.Pointer(pStore)).Fsz
		}
		if mFlags&uint32(SQLITE_SERIALIZE_NOCOPY) != 0 {
			pOut = (*TMemStore)(unsafe.Pointer(pStore)).FaData
		} else {
			pOut = Xsqlite3_malloc64(tls, uint64((*TMemStore)(unsafe.Pointer(pStore)).Fsz))
			if pOut != 0 {
				libc.Xmemcpy(tls, pOut, (*TMemStore)(unsafe.Pointer(pStore)).FaData, uint32((*TMemStore)(unsafe.Pointer(pStore)).Fsz))
			}
		}
		goto serialize_out
	}
	pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt
	if pBt == uintptr(0) {
		goto serialize_out
	}
	szPage = _sqlite3BtreeGetPageSize(tls, pBt)
	zSql = Xsqlite3_mprintf(tls, __ccgo_ts+5350, libc.VaList(bp+16, zSchema))
	if zSql != 0 {
		v1 = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp, uintptr(0))
	} else {
		v1 = int32(SQLITE_NOMEM)
	}
	rc = v1
	Xsqlite3_free(tls, zSql)
	if rc != 0 {
		goto serialize_out
	}
	rc = Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp)))
	if rc == int32(SQLITE_ROW) {
		sz = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp)), 0) * int64(szPage)
		if sz == 0 {
			Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp)))
			Xsqlite3_exec(tls, db, __ccgo_ts+5373, uintptr(0), uintptr(0), uintptr(0))
			rc = Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp)))
			if rc == int32(SQLITE_ROW) {
				sz = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp)), 0) * int64(szPage)
			}
		}
		if piSize != 0 {
			**(**Tsqlite3_int64)(__ccgo_up(piSize)) = sz
		}
		if mFlags&uint32(SQLITE_SERIALIZE_NOCOPY) != 0 {
			pOut = uintptr(0)
		} else {
			pOut = Xsqlite3_malloc64(tls, uint64(sz))
			if pOut != 0 {
				nPage = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), 0)
				pPager = _sqlite3BtreePager(tls, pBt)
				pgno = int32(1)
				for {
					if !(pgno <= nPage) {
						break
					}
					**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
					pTo = pOut + uintptr(int64(szPage)*int64(pgno-libc.Int32FromInt32(1)))
					rc = _sqlite3PagerGet(tls, pPager, uint32(pgno), bp+4, 0)
					if rc == SQLITE_OK {
						libc.Xmemcpy(tls, pTo, _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp + 4))), uint32(szPage))
					} else {
						libc.Xmemset(tls, pTo, 0, uint32(szPage))
					}
					_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp + 4)))
					goto _2
				_2:
					;
					pgno = pgno + 1
				}
			}
		}
	}
	Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	goto serialize_out
serialize_out:
	;
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return pOut
}

// C documentation
//
//	/*
//	** Obtain a snapshot handle for the snapshot of database zDb currently
//	** being read by handle db.
//	*/
func Xsqlite3_snapshot_get(tls *libc.TLS, db uintptr, zDb uintptr, ppSnapshot uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iDb, rc int32
	var pBt, pPager uintptr
	var _ /* dummy at bp+0 */ Ti64
	_, _, _, _ = iDb, pBt, pPager, rc
	rc = int32(SQLITE_ERROR)
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	if int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) == 0 {
		iDb = _sqlite3FindDbName(tls, db, zDb)
		if iDb == 0 || iDb > int32(1) {
			pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt
			if int32(SQLITE_TXN_WRITE) != _sqlite3BtreeTxnState(tls, pBt) {
				pPager = _sqlite3BtreePager(tls, pBt)
				**(**Ti64)(__ccgo_up(bp)) = 0
				_sqlite3PagerSnapshotOpen(tls, pPager, bp)
				rc = _sqlite3BtreeBeginTrans(tls, pBt, 0, uintptr(0))
				_sqlite3PagerSnapshotOpen(tls, pPager, uintptr(0))
				if rc == SQLITE_OK {
					rc = _sqlite3PagerSnapshotGet(tls, _sqlite3BtreePager(tls, pBt), ppSnapshot)
				}
			}
		}
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Open a read-transaction on the snapshot identified by pSnapshot.
//	*/
func Xsqlite3_snapshot_open(tls *libc.TLS, db uintptr, zDb uintptr, pSnapshot uintptr) (r int32) {
	var bUnlock, iDb, rc int32
	var pBt, pPager uintptr
	_, _, _, _, _ = bUnlock, iDb, pBt, pPager, rc
	rc = int32(SQLITE_ERROR)
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	if int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) == 0 {
		iDb = _sqlite3FindDbName(tls, db, zDb)
		if iDb == 0 || iDb > int32(1) {
			pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt
			if _sqlite3BtreeTxnState(tls, pBt) != int32(SQLITE_TXN_WRITE) {
				pPager = _sqlite3BtreePager(tls, pBt)
				bUnlock = 0
				if _sqlite3BtreeTxnState(tls, pBt) != SQLITE_TXN_NONE {
					if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive == 0 {
						rc = _sqlite3PagerSnapshotCheck(tls, pPager, pSnapshot)
						if rc == SQLITE_OK {
							bUnlock = int32(1)
							rc = _sqlite3BtreeCommit(tls, pBt)
						}
					}
				} else {
					rc = SQLITE_OK
				}
				if rc == SQLITE_OK {
					rc = _sqlite3PagerSnapshotOpen(tls, pPager, pSnapshot)
				}
				if rc == SQLITE_OK {
					rc = _sqlite3BtreeBeginTrans(tls, pBt, 0, uintptr(0))
					_sqlite3PagerSnapshotOpen(tls, pPager, uintptr(0))
				}
				if bUnlock != 0 {
					_sqlite3PagerSnapshotUnlock(tls, pPager)
				}
			}
		}
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Query status information.
//	*/
func Xsqlite3_status64(tls *libc.TLS, op int32, pCurrent uintptr, pHighwater uintptr, resetFlag int32) (r int32) {
	var pMutex, v1 uintptr
	_, _ = pMutex, v1
	if op < 0 || op >= int32(libc.Uint32FromInt64(40)/libc.Uint32FromInt64(4)) {
		return _sqlite3MisuseError(tls, int32(25154))
	}
	if _statMutex[op] != 0 {
		v1 = _sqlite3Pcache1Mutex(tls)
	} else {
		v1 = _sqlite3MallocMutex(tls)
	}
	pMutex = v1
	Xsqlite3_mutex_enter(tls, pMutex)
	**(**Tsqlite3_int64)(__ccgo_up(pCurrent)) = int64(**(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)))
	**(**Tsqlite3_int64)(__ccgo_up(pHighwater)) = int64(**(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)))
	if resetFlag != 0 {
		**(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) = **(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4))
	}
	Xsqlite3_mutex_leave(tls, pMutex)
	_ = pMutex /* Prevent warning when SQLITE_THREADSAFE=0 */
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Set the explain mode for a statement.
//	*/
func Xsqlite3_stmt_explain(tls *libc.TLS, pStmt uintptr, eMode int32) (r int32) {
	var rc int32
	var v uintptr
	_, _ = rc, v
	v = pStmt
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).Fdb)).Fmutex)
	if int32(Tbft(*(*uint16)(unsafe.Pointer(v + 152))&0xc>>2)) == eMode {
		rc = SQLITE_OK
	} else {
		if eMode < 0 || eMode > int32(2) {
			rc = int32(SQLITE_ERROR)
		} else {
			if int32((*TVdbe)(unsafe.Pointer(v)).FprepFlags)&int32(SQLITE_PREPARE_SAVESQL) == 0 {
				rc = int32(SQLITE_ERROR)
			} else {
				if int32((*TVdbe)(unsafe.Pointer(v)).FeVdbeState) != int32(VDBE_READY_STATE) {
					rc = int32(SQLITE_BUSY)
				} else {
					if (*TVdbe)(unsafe.Pointer(v)).FnMem >= int32(10) && (eMode != int32(2) || int32(Tbft(*(*uint16)(unsafe.Pointer(v + 152))&0x100>>8)) != 0) {
						/* No reprepare necessary */
						libc.SetBitFieldPtr16Uint32(v+152, uint32(eMode), 2, 0xc)
						rc = SQLITE_OK
					} else {
						libc.SetBitFieldPtr16Uint32(v+152, uint32(eMode), 2, 0xc)
						rc = _sqlite3Reprepare(tls, v)
						libc.SetBitFieldPtr16Uint32(v+152, libc.BoolUint32(eMode == libc.Int32FromInt32(2)), 8, 0x100)
					}
				}
			}
		}
	}
	if int32(Tbft(*(*uint16)(unsafe.Pointer(v + 152))&0xc>>2)) != 0 {
		(*TVdbe)(unsafe.Pointer(v)).FnResColumn = uint16(int32(12) - int32(4)*int32(Tbft(*(*uint16)(unsafe.Pointer(v + 152))&0xc>>2)))
	} else {
		(*TVdbe)(unsafe.Pointer(v)).FnResColumn = (*TVdbe)(unsafe.Pointer(v)).FnResAlloc
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).Fdb)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Return the value of a status counter for a prepared statement
//	*/
func Xsqlite3_stmt_status(tls *libc.TLS, pStmt uintptr, op int32, resetFlag int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pVdbe uintptr
	var _ /* v at bp+0 */ Tu32
	_, _ = db, pVdbe
	pVdbe = pStmt
	if op == int32(SQLITE_STMTSTATUS_MEMUSED) {
		db = (*TVdbe)(unsafe.Pointer(pVdbe)).Fdb
		Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
		**(**Tu32)(__ccgo_up(bp)) = uint32(0)
		(*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = bp
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart
		_sqlite3VdbeDelete(tls, pVdbe)
		(*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed = uintptr(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	} else {
		**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(pVdbe + 164 + uintptr(op)*4))
		if resetFlag != 0 {
			**(**Tu32)(__ccgo_up(pVdbe + 164 + uintptr(op)*4)) = uint32(0)
		}
	}
	return int32(**(**Tu32)(__ccgo_up(bp)))
}

// C documentation
//
//	/*
//	** Append N bytes of text from z to the StrAccum object.  Increase the
//	** size of the memory allocation for StrAccum if necessary.
//	*/
func Xsqlite3_str_append(tls *libc.TLS, p uintptr, z uintptr, N int32) {
	if (*Tsqlite3_str)(unsafe.Pointer(p)).FnChar+uint32(N) >= (*Tsqlite3_str)(unsafe.Pointer(p)).FnAlloc {
		_enlargeAndAppend(tls, p, z, N)
	} else {
		if N != 0 {
			**(**Tu32)(__ccgo_up(p + 16)) += uint32(N)
			libc.Xmemcpy(tls, (*Tsqlite3_str)(unsafe.Pointer(p)).FzText+uintptr((*Tsqlite3_str)(unsafe.Pointer(p)).FnChar-uint32(N)), z, uint32(N))
		}
	}
}

// C documentation
//
//	/*
//	** Append N copies of character c to the given string buffer.
//	*/
func Xsqlite3_str_appendchar(tls *libc.TLS, p uintptr, N int32, c int8) {
	var v1 int32
	var v2 bool
	var v4 Tu32
	var v5 uintptr
	_, _, _, _ = v1, v2, v4, v5
	if v2 = int64((*Tsqlite3_str)(unsafe.Pointer(p)).FnChar)+int64(N) >= int64((*Tsqlite3_str)(unsafe.Pointer(p)).FnAlloc); v2 {
		v1 = _sqlite3StrAccumEnlarge(tls, p, int64(N))
		N = v1
	}
	if v2 && v1 <= 0 {
		return
	}
	for {
		v1 = N
		N = N - 1
		if !(v1 > 0) {
			break
		}
		v5 = p + 16
		v4 = *(*Tu32)(unsafe.Pointer(v5))
		*(*Tu32)(unsafe.Pointer(v5)) = *(*Tu32)(unsafe.Pointer(v5)) + 1
		**(**int8)(__ccgo_up((*Tsqlite3_str)(unsafe.Pointer(p)).FzText + uintptr(v4))) = c
	}
}

// C documentation
//
//	/*
//	** Reset an StrAccum string.  Reclaim all malloced memory.
//	*/
func Xsqlite3_str_reset(tls *libc.TLS, p uintptr) {
	var v1 uintptr
	_ = v1
	if int32((*TStrAccum)(unsafe.Pointer(p)).FprintfFlags)&int32(SQLITE_PRINTF_MALLOCED) != 0 {
		_sqlite3DbFree(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, (*TStrAccum)(unsafe.Pointer(p)).FzText)
		v1 = p + 21
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED))
	}
	(*TStrAccum)(unsafe.Pointer(p)).FnAlloc = uint32(0)
	(*TStrAccum)(unsafe.Pointer(p)).FnChar = uint32(0)
	(*TStrAccum)(unsafe.Pointer(p)).FzText = uintptr(0)
}

// C documentation
//
//	/*
//	** Render a string given by "fmt" into the StrAccum object.
//	*/
func Xsqlite3_str_vappendf(tls *libc.TLS, pAccum uintptr, fmt uintptr, ap Tva_list) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var adj, c, e2, exp, iRound, idx, ii, ix, j, length, nOut, needQuote, nn, nn1, nn2, nn3, precision, width, x, v2, v3 int32
	var bArgList, base Tu8
	var bufpt, cset, escarg, infop, pArgList, pExpr, pItem, pSel, pToken, pre, z, zExtra, zOut, v4 uintptr
	var cThousand, done, flag_alternateform, flag_altform2, flag_dp, flag_leftjustify, flag_long, flag_prefix, flag_rtz, flag_zeropad, xtype, v5, v6, v7, v8, v9 TetByte
	var ch, px, wx uint32
	var ch1, prefix, q, x1, v54 int8
	var i, j1, k, n1, nBack, nCopyBytes, nCtrl, nPad, nPrior, szBufNeeded, v, v90 Ti64
	var longvalue Tsqlite_uint64
	var n Tu64
	var realvalue float64
	var v12 bool
	var _ /* buf at bp+0 */ [70]int8
	var _ /* s at bp+72 */ TFpDecode
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = adj, bArgList, base, bufpt, c, cThousand, ch, ch1, cset, done, e2, escarg, exp, flag_alternateform, flag_altform2, flag_dp, flag_leftjustify, flag_long, flag_prefix, flag_rtz, flag_zeropad, i, iRound, idx, ii, infop, ix, j, j1, k, length, longvalue, n, n1, nBack, nCopyBytes, nCtrl, nOut, nPad, nPrior, needQuote, nn, nn1, nn2, nn3, pArgList, pExpr, pItem, pSel, pToken, pre, precision, prefix, px, q, realvalue, szBufNeeded, v, width, wx, x, x1, xtype, z, zExtra, zOut, v12, v2, v3, v4, v5, v54, v6, v7, v8, v9, v90 /* Thousands separator for %d and %u */
	xtype = uint8(etINVALID)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     /* Size of the rendering buffer */
	zExtra = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          /* True if trailing zeros should be removed */
	pArgList = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        /* Conversion buffer */
	/* pAccum never starts out with an empty buffer that was obtained from
	 ** malloc().  This precondition is required by the mprintf("%z...")
	 ** optimization. */
	bufpt = uintptr(0)
	if int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FprintfFlags)&int32(SQLITE_PRINTF_SQLFUNC) != 0 {
		pArgList = libc.VaUintptr(&ap)
		bArgList = uint8(1)
	} else {
		bArgList = uint8(0)
	}
	for {
		v2 = int32(**(**int8)(__ccgo_up(fmt)))
		c = v2
		if !(v2 != 0) {
			break
		}
		if c != int32('%') {
			bufpt = fmt
			fmt = libc.Xstrchr(tls, fmt, int32('%'))
			if fmt == uintptr(0) {
				fmt = bufpt + uintptr(libc.Xstrlen(tls, bufpt))
			}
			Xsqlite3_str_append(tls, pAccum, bufpt, int32(fmt)-int32(bufpt))
			if int32(**(**int8)(__ccgo_up(fmt))) == 0 {
				break
			}
		}
		fmt = fmt + 1
		v4 = fmt
		v2 = int32(**(**int8)(__ccgo_up(v4)))
		c = v2
		if v2 == 0 {
			Xsqlite3_str_append(tls, pAccum, __ccgo_ts+1695, int32(1))
			break
		}
		/* Find out what flags are present */
		v9 = libc.Uint8FromInt32(0)
		flag_zeropad = v9
		v8 = v9
		flag_altform2 = v8
		v7 = v8
		flag_alternateform = v7
		v6 = v7
		cThousand = v6
		v5 = v6
		flag_prefix = v5
		flag_leftjustify = v5
		done = uint8(0)
		width = 0
		flag_long = uint8(0)
		precision = -int32(1)
		for {
			switch c {
			case int32('-'):
				flag_leftjustify = uint8(1)
			case int32('+'):
				flag_prefix = uint8('+')
			case int32(' '):
				flag_prefix = uint8(' ')
			case int32('#'):
				flag_alternateform = uint8(1)
			case int32('!'):
				flag_altform2 = uint8(1)
			case int32('0'):
				flag_zeropad = uint8(1)
			case int32(','):
				cThousand = uint8(',')
			default:
				done = uint8(1)
			case int32('l'):
				flag_long = uint8(1)
				fmt = fmt + 1
				v4 = fmt
				c = int32(**(**int8)(__ccgo_up(v4)))
				if c == int32('l') {
					fmt = fmt + 1
					v4 = fmt
					c = int32(**(**int8)(__ccgo_up(v4)))
					flag_long = uint8(2)
				}
				done = uint8(1)
			case int32('1'):
				fallthrough
			case int32('2'):
				fallthrough
			case int32('3'):
				fallthrough
			case int32('4'):
				fallthrough
			case int32('5'):
				fallthrough
			case int32('6'):
				fallthrough
			case int32('7'):
				fallthrough
			case int32('8'):
				fallthrough
			case int32('9'):
				wx = uint32(c - int32('0'))
				for {
					fmt = fmt + 1
					v4 = fmt
					v2 = int32(**(**int8)(__ccgo_up(v4)))
					c = v2
					if !(v2 >= int32('0') && c <= int32('9')) {
						break
					}
					wx = wx*uint32(10) + uint32(c) - uint32('0')
				}
				width = int32(wx & uint32(0x7fffffff))
				if c != int32('.') && c != int32('l') {
					done = uint8(1)
				} else {
					fmt = fmt - 1
				}
			case int32('*'):
				if bArgList != 0 {
					width = int32(_getIntArg(tls, pArgList))
				} else {
					width = libc.VaInt32(&ap)
				}
				if width < 0 {
					flag_leftjustify = uint8(1)
					if width >= -int32(2147483647) {
						v2 = -width
					} else {
						v2 = 0
					}
					width = v2
				}
				v2 = int32(**(**int8)(__ccgo_up(fmt + 1)))
				c = v2
				if v2 != int32('.') && c != int32('l') {
					fmt = fmt + 1
					v4 = fmt
					c = int32(**(**int8)(__ccgo_up(v4)))
					done = uint8(1)
				}
			case int32('.'):
				fmt = fmt + 1
				v4 = fmt
				c = int32(**(**int8)(__ccgo_up(v4)))
				if c == int32('*') {
					if bArgList != 0 {
						precision = int32(_getIntArg(tls, pArgList))
					} else {
						precision = libc.VaInt32(&ap)
					}
					if precision < 0 {
						if precision >= -int32(2147483647) {
							v2 = -precision
						} else {
							v2 = -int32(1)
						}
						precision = v2
					}
					fmt = fmt + 1
					v4 = fmt
					c = int32(**(**int8)(__ccgo_up(v4)))
				} else {
					px = uint32(0)
					for c >= int32('0') && c <= int32('9') {
						px = px*uint32(10) + uint32(c) - uint32('0')
						fmt = fmt + 1
						v4 = fmt
						c = int32(**(**int8)(__ccgo_up(v4)))
					}
					precision = int32(px & uint32(0x7fffffff))
				}
				if c == int32('l') {
					fmt = fmt - 1
				} else {
					done = uint8(1)
				}
				break
			}
			goto _13
		_13:
			;
			if v12 = !(done != 0); v12 {
				fmt = fmt + 1
				v4 = fmt
				v2 = int32(**(**int8)(__ccgo_up(v4)))
				c = v2
			}
			if !(v12 && v2 != 0) {
				break
			}
		}
		/* Fetch the info entry for the field */
		/* Fast hash-table lookup */
		idx = int32(uint32(c) % uint32(23))
		if v12 = int32(_fmtinfo[idx].Ffmttype) == c; !v12 {
			v2 = int32(_fmtinfo[idx].FiNxt)
			idx = v2
		}
		if v12 || int32(_fmtinfo[v2].Ffmttype) == c {
			infop = uintptr(unsafe.Pointer(&_fmtinfo)) + uintptr(idx)*7
			xtype = (*Tet_info)(unsafe.Pointer(infop)).Ftype1
		} else {
			infop = uintptr(unsafe.Pointer(&_fmtinfo))
			xtype = uint8(etINVALID)
		}
		/*
		 ** At this point, variables are initialized as follows:
		 **
		 **   flag_alternateform          TRUE if a '#' is present.
		 **   flag_altform2               TRUE if a '!' is present.
		 **   flag_prefix                 '+' or ' ' or zero
		 **   flag_leftjustify            TRUE if a '-' is present or if the
		 **                               field width was negative.
		 **   flag_zeropad                TRUE if the width began with 0.
		 **   flag_long                   1 for "l", 2 for "ll"
		 **   width                       The specified field width.  This is
		 **                               always non-negative.  Zero is the default.
		 **   precision                   The specified precision.  The default
		 **                               is -1.
		 **   xtype                       The class of the conversion.
		 **   infop                       Pointer to the appropriate info struct.
		 */
		switch int32(xtype) {
		case int32(etPOINTER):
			goto _27
		case etRADIX:
			goto _28
		case int32(etORDINAL):
			goto _29
		case int32(etDECIMAL):
			goto _30
		case int32(etGENERIC):
			goto _31
		case int32(etEXP):
			goto _32
		case int32(etFLOAT):
			goto _33
		case int32(etSIZE):
			goto _34
		case int32(etPERCENT):
			goto _35
		case int32(etCHARX):
			goto _36
		case int32(etDYNSTRING):
			goto _37
		case int32(etSTRING):
			goto _38
		case int32(etESCAPE_w):
			goto _39
		case int32(etESCAPE_Q):
			goto _40
		case int32(etESCAPE_q):
			goto _41
		case int32(etTOKEN):
			goto _42
		case int32(etSRCITEM):
			goto _43
		default:
			goto _44
		}
		goto _45
	_27:
		;
		flag_long = uint8(1)
	_29:
		;
	_28:
		;
		cThousand = uint8(0)
	_30:
		;
		if int32((*Tet_info)(unsafe.Pointer(infop)).Fflags)&int32(FLAG_SIGNED) != 0 {
			if bArgList != 0 {
				v = _getIntArg(tls, pArgList)
			} else {
				if flag_long != 0 {
					if int32(flag_long) == int32(2) {
						v = libc.VaInt64(&ap)
					} else {
						v = int64(libc.VaInt32(&ap))
					}
				} else {
					v = int64(libc.VaInt32(&ap))
				}
			}
			if v < 0 {
				longvalue = uint64(^v)
				longvalue = longvalue + 1
				prefix = int8('-')
			} else {
				longvalue = uint64(v)
				prefix = int8(flag_prefix)
			}
		} else {
			if bArgList != 0 {
				longvalue = uint64(_getIntArg(tls, pArgList))
			} else {
				if flag_long != 0 {
					if int32(flag_long) == int32(2) {
						longvalue = libc.VaUint64(&ap)
					} else {
						longvalue = uint64(libc.VaUint32(&ap))
					}
				} else {
					longvalue = uint64(libc.VaUint32(&ap))
				}
			}
			prefix = 0
		}
		if longvalue == uint64(0) {
			flag_alternateform = uint8(0)
		}
		if flag_zeropad != 0 && precision < width-libc.BoolInt32(int32(prefix) != 0) {
			precision = width - libc.BoolInt32(int32(prefix) != 0)
		}
		if precision < libc.Int32FromInt32(SQLITE_PRINT_BUF_SIZE)-libc.Int32FromInt32(10)-libc.Int32FromInt32(SQLITE_PRINT_BUF_SIZE)/libc.Int32FromInt32(3) {
			nOut = int32(SQLITE_PRINT_BUF_SIZE)
			zOut = bp
		} else {
			n = uint64(precision) + uint64(10)
			if cThousand != 0 {
				n = n + uint64(precision/int32(3))
			}
			v4 = _printfTempBuf(tls, pAccum, int64(n))
			zExtra = v4
			zOut = v4
			if zOut == uintptr(0) {
				return
			}
			nOut = int32(n)
		}
		bufpt = zOut + uintptr(nOut-int32(1))
		if int32(xtype) == int32(etORDINAL) {
			x = int32(longvalue % libc.Uint64FromInt32(10))
			if x >= int32(4) || longvalue/uint64(10)%uint64(10) == uint64(1) {
				x = 0
			}
			bufpt = bufpt - 1
			v4 = bufpt
			**(**int8)(__ccgo_up(v4)) = _zOrd[x*int32(2)+int32(1)]
			bufpt = bufpt - 1
			v4 = bufpt
			**(**int8)(__ccgo_up(v4)) = _zOrd[x*int32(2)]
		}
		cset = uintptr(unsafe.Pointer(&_aDigits)) + uintptr((*Tet_info)(unsafe.Pointer(infop)).Fcharset)
		base = (*Tet_info)(unsafe.Pointer(infop)).Fbase
		for cond := true; cond; cond = longvalue > uint64(0) { /* Convert to ascii */
			bufpt = bufpt - 1
			v4 = bufpt
			**(**int8)(__ccgo_up(v4)) = **(**int8)(__ccgo_up(cset + uintptr(longvalue%uint64(base))))
			longvalue = longvalue / uint64(base)
		}
		length = t__predefined_ptrdiff_t(zOut+uintptr(nOut-int32(1))) - int32(bufpt)
		if precision > length { /* zero pad */
			nn = precision - length
			bufpt = bufpt - uintptr(nn)
			libc.Xmemset(tls, bufpt, int32('0'), uint32(nn))
			length = precision
		}
		if cThousand != 0 {
			nn1 = (length - int32(1)) / int32(3) /* Number of "," to insert */
			ix = (length-int32(1))%int32(3) + int32(1)
			bufpt = bufpt - uintptr(nn1)
			idx = 0
			for {
				if !(nn1 > 0) {
					break
				}
				**(**int8)(__ccgo_up(bufpt + uintptr(idx))) = **(**int8)(__ccgo_up(bufpt + uintptr(idx+nn1)))
				ix = ix - 1
				if ix == 0 {
					idx = idx + 1
					v2 = idx
					**(**int8)(__ccgo_up(bufpt + uintptr(v2))) = int8(cThousand)
					nn1 = nn1 - 1
					ix = int32(3)
				}
				goto _50
			_50:
				;
				idx = idx + 1
			}
		}
		if prefix != 0 {
			bufpt = bufpt - 1
			v4 = bufpt
			**(**int8)(__ccgo_up(v4)) = prefix
		} /* Add sign */
		if flag_alternateform != 0 && (*Tet_info)(unsafe.Pointer(infop)).Fprefix != 0 {
			pre = uintptr(unsafe.Pointer(&_aPrefix)) + uintptr((*Tet_info)(unsafe.Pointer(infop)).Fprefix)
			for {
				v54 = **(**int8)(__ccgo_up(pre))
				x1 = v54
				if !(int32(v54) != 0) {
					break
				}
				bufpt = bufpt - 1
				v4 = bufpt
				**(**int8)(__ccgo_up(v4)) = x1
				goto _53
			_53:
				;
				pre = pre + 1
			}
		}
		length = t__predefined_ptrdiff_t(zOut+uintptr(nOut-int32(1))) - int32(bufpt)
		goto _45
	_33:
		;
	_32:
		;
	_31:
		; /* Size needed to hold the output */
		if bArgList != 0 {
			realvalue = _getDoubleArg(tls, pArgList)
		} else {
			realvalue = libc.VaFloat64(&ap)
		}
		if precision < 0 {
			precision = int32(6)
		} /* Set default precision */
		if precision > int32(SQLITE_FP_PRECISION_LIMIT) {
			precision = int32(SQLITE_FP_PRECISION_LIMIT)
		}
		if int32(xtype) == int32(etFLOAT) {
			iRound = -precision
		} else {
			if int32(xtype) == int32(etGENERIC) {
				if precision == 0 {
					precision = int32(1)
				}
				iRound = precision
			} else {
				iRound = precision + int32(1)
			}
		}
		if flag_altform2 != 0 {
			v2 = int32(20)
		} else {
			v2 = int32(16)
		}
		_sqlite3FpDecode(tls, bp+72, realvalue, iRound, v2)
		if (**(**TFpDecode)(__ccgo_up(bp + 72))).FisSpecial != 0 {
			if int32((**(**TFpDecode)(__ccgo_up(bp + 72))).FisSpecial) == int32(2) {
				if flag_zeropad != 0 {
					v4 = __ccgo_ts + 1697
				} else {
					v4 = __ccgo_ts + 1702
				}
				bufpt = v4
				length = _sqlite3Strlen30(tls, bufpt)
				goto _45
			} else {
				if flag_zeropad != 0 {
					**(**int8)(__ccgo_up((**(**TFpDecode)(__ccgo_up(bp + 72))).Fz)) = int8('9')
					(**(**TFpDecode)(__ccgo_up(bp + 72))).FiDP = int32(1000)
					(**(**TFpDecode)(__ccgo_up(bp + 72))).Fn = int32(1)
				} else {
					libc.Xmemcpy(tls, bp, __ccgo_ts+1706, uint32(5))
					bufpt = bp
					if int32((**(**TFpDecode)(__ccgo_up(bp + 72))).Fsign) == int32('-') {
						/* no-op */
					} else {
						if flag_prefix != 0 {
							(**(**[70]int8)(__ccgo_up(bp)))[0] = int8(flag_prefix)
						} else {
							bufpt = bufpt + 1
						}
					}
					length = _sqlite3Strlen30(tls, bufpt)
					goto _45
				}
			}
		}
		if int32((**(**TFpDecode)(__ccgo_up(bp + 72))).Fsign) == int32('-') {
			if flag_alternateform != 0 && !(flag_prefix != 0) && int32(xtype) == int32(etFLOAT) && (**(**TFpDecode)(__ccgo_up(bp + 72))).FiDP <= iRound {
				/* Suppress the minus sign if all of the following are true:
				 **   *  The value displayed is zero
				 **   *  The '#' flag is used
				 **   *  The '+' flag is not used, and
				 **   *  The format is %f
				 */
				prefix = 0
			} else {
				prefix = int8('-')
			}
		} else {
			prefix = int8(flag_prefix)
		}
		exp = (**(**TFpDecode)(__ccgo_up(bp + 72))).FiDP - int32(1)
		/*
		 ** If the field type is etGENERIC, then convert to either etEXP
		 ** or etFLOAT, as appropriate.
		 */
		if int32(xtype) == int32(etGENERIC) {
			precision = precision - 1
			flag_rtz = libc.BoolUint8(!(flag_alternateform != 0))
			if exp < -int32(4) || exp > precision {
				xtype = uint8(etEXP)
			} else {
				precision = precision - exp
				xtype = uint8(etFLOAT)
			}
		} else {
			flag_rtz = flag_altform2
		}
		if int32(xtype) == int32(etEXP) {
			e2 = 0
		} else {
			e2 = (**(**TFpDecode)(__ccgo_up(bp + 72))).FiDP - int32(1)
		}
		if e2 > 0 {
			v2 = e2
		} else {
			v2 = 0
		}
		szBufNeeded = int64(v2) + int64(precision) + int64(width) + int64(10)
		if cThousand != 0 && e2 > 0 {
			szBufNeeded = szBufNeeded + int64((e2+int32(2))/int32(3))
		}
		if szBufNeeded+int64((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar) >= int64((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnAlloc) {
			if (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FmxAlloc == uint32(0) && int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FaccError) == 0 {
				/* Unable to allocate space in pAccum, perhaps because it
				 ** is coming from sqlite3_snprintf() or similar.  We'll have
				 ** to render into temporary space and the memcpy() it over. */
				bufpt = Xsqlite3_malloc(tls, int32(szBufNeeded))
				if bufpt == uintptr(0) {
					_sqlite3StrAccumSetError(tls, pAccum, uint8(SQLITE_NOMEM))
					return
				}
				zExtra = bufpt
			} else {
				if int64(_sqlite3StrAccumEnlarge(tls, pAccum, szBufNeeded)) < szBufNeeded {
					v2 = libc.Int32FromInt32(0)
					length = v2
					width = v2
					goto _45
				} else {
					bufpt = (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FzText + uintptr((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar)
				}
			}
		} else {
			bufpt = (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FzText + uintptr((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar)
		}
		zOut = bufpt
		if precision > 0 {
			v2 = int32(1)
		} else {
			v2 = 0
		}
		flag_dp = uint8(v2 | int32(flag_alternateform) | int32(flag_altform2))
		/* The sign in front of the number */
		if prefix != 0 {
			v4 = bufpt
			bufpt = bufpt + 1
			**(**int8)(__ccgo_up(v4)) = prefix
		}
		/* Digits prior to the decimal point */
		j = 0
		if e2 < 0 {
			v4 = bufpt
			bufpt = bufpt + 1
			**(**int8)(__ccgo_up(v4)) = int8('0')
		} else {
			if cThousand != 0 {
				for {
					if !(e2 >= 0) {
						break
					}
					v4 = bufpt
					bufpt = bufpt + 1
					if j < (**(**TFpDecode)(__ccgo_up(bp + 72))).Fn {
						v3 = j
						j = j + 1
						v2 = int32(**(**int8)(__ccgo_up((**(**TFpDecode)(__ccgo_up(bp + 72))).Fz + uintptr(v3))))
					} else {
						v2 = int32('0')
					}
					**(**int8)(__ccgo_up(v4)) = int8(v2)
					if e2%int32(3) == 0 && e2 > int32(1) {
						v4 = bufpt
						bufpt = bufpt + 1
						**(**int8)(__ccgo_up(v4)) = int8(',')
					}
					goto _63
				_63:
					;
					e2 = e2 - 1
				}
			} else {
				j = e2 + int32(1)
				if j > (**(**TFpDecode)(__ccgo_up(bp + 72))).Fn {
					j = (**(**TFpDecode)(__ccgo_up(bp + 72))).Fn
				}
				libc.Xmemcpy(tls, bufpt, (**(**TFpDecode)(__ccgo_up(bp + 72))).Fz, uint32(j))
				bufpt = bufpt + uintptr(j)
				e2 = e2 - j
				if e2 >= 0 {
					libc.Xmemset(tls, bufpt, int32('0'), uint32(e2+int32(1)))
					bufpt = bufpt + uintptr(e2+int32(1))
					e2 = -int32(1)
				}
			}
		}
		/* The decimal point */
		if flag_dp != 0 {
			v4 = bufpt
			bufpt = bufpt + 1
			**(**int8)(__ccgo_up(v4)) = int8('.')
		}
		/* "0" digits after the decimal point but before the first
		 ** significant digit of the number */
		if e2 < -int32(1) && precision > 0 {
			nn2 = -int32(1) - e2
			if nn2 > precision {
				nn2 = precision
			}
			libc.Xmemset(tls, bufpt, int32('0'), uint32(nn2))
			bufpt = bufpt + uintptr(nn2)
			precision = precision - nn2
		}
		/* Significant digits after the decimal point */
		if precision > 0 {
			nn3 = (**(**TFpDecode)(__ccgo_up(bp + 72))).Fn - j
			if nn3 > precision {
				nn3 = precision
			}
			if nn3 > 0 {
				libc.Xmemcpy(tls, bufpt, (**(**TFpDecode)(__ccgo_up(bp + 72))).Fz+uintptr(j), uint32(nn3))
				bufpt = bufpt + uintptr(nn3)
				precision = precision - nn3
			}
			if precision > 0 && !(flag_rtz != 0) {
				libc.Xmemset(tls, bufpt, int32('0'), uint32(precision))
				bufpt = bufpt + uintptr(precision)
			}
		}
		/* Remove trailing zeros and the "." if no digits follow the "." */
		if flag_rtz != 0 && flag_dp != 0 {
			for int32(**(**int8)(__ccgo_up(bufpt + uintptr(-libc.Int32FromInt32(1))))) == int32('0') {
				bufpt = bufpt - 1
				v4 = bufpt
				**(**int8)(__ccgo_up(v4)) = 0
			}
			if int32(**(**int8)(__ccgo_up(bufpt + uintptr(-libc.Int32FromInt32(1))))) == int32('.') {
				if flag_altform2 != 0 {
					v4 = bufpt
					bufpt = bufpt + 1
					**(**int8)(__ccgo_up(v4)) = int8('0')
				} else {
					bufpt = bufpt - 1
					v4 = bufpt
					**(**int8)(__ccgo_up(v4)) = 0
				}
			}
		}
		/* Add the "eNNN" suffix */
		if int32(xtype) == int32(etEXP) {
			exp = (**(**TFpDecode)(__ccgo_up(bp + 72))).FiDP - int32(1)
			v4 = bufpt
			bufpt = bufpt + 1
			**(**int8)(__ccgo_up(v4)) = _aDigits[(*Tet_info)(unsafe.Pointer(infop)).Fcharset]
			if exp < 0 {
				v4 = bufpt
				bufpt = bufpt + 1
				**(**int8)(__ccgo_up(v4)) = int8('-')
				exp = -exp
			} else {
				v4 = bufpt
				bufpt = bufpt + 1
				**(**int8)(__ccgo_up(v4)) = int8('+')
			}
			if exp >= int32(100) {
				v4 = bufpt
				bufpt = bufpt + 1
				**(**int8)(__ccgo_up(v4)) = int8(exp/libc.Int32FromInt32(100) + libc.Int32FromUint8('0')) /* 100's digit */
				exp = exp % int32(100)
			}
			v4 = bufpt
			bufpt = bufpt + 1
			**(**int8)(__ccgo_up(v4)) = int8(exp/libc.Int32FromInt32(10) + libc.Int32FromUint8('0')) /* 10's digit */
			v4 = bufpt
			bufpt = bufpt + 1
			**(**int8)(__ccgo_up(v4)) = int8(exp%libc.Int32FromInt32(10) + libc.Int32FromUint8('0')) /* 1's digit */
		}
		length = int32(bufpt) - int32(zOut)
		if length < width {
			nPad = int64(width - length)
			if flag_leftjustify != 0 {
				libc.Xmemset(tls, bufpt, int32(' '), uint32(nPad))
			} else {
				if !(flag_zeropad != 0) {
					libc.Xmemmove(tls, zOut+uintptr(nPad), zOut, uint32(length))
					libc.Xmemset(tls, zOut, int32(' '), uint32(nPad))
				} else {
					adj = libc.BoolInt32(int32(prefix) != 0)
					libc.Xmemmove(tls, zOut+uintptr(nPad)+uintptr(adj), zOut+uintptr(adj), uint32(length-adj))
					libc.Xmemset(tls, zOut+uintptr(adj), int32('0'), uint32(nPad))
				}
			}
			length = width
		}
		if zExtra == uintptr(0) {
			/* The result is being rendered directory into pAccum.  This
			 ** is the command and fast case */
			**(**Tu32)(__ccgo_up(pAccum + 16)) += uint32(length)
			**(**int8)(__ccgo_up(zOut + uintptr(length))) = 0
			goto _1
		} else {
			/* We were unable to render directly into pAccum because we
			 ** couldn't allocate sufficient memory.  We need to memcpy()
			 ** the rendering (or some prefix thereof) into the output
			 ** buffer. */
			**(**int8)(__ccgo_up(bufpt)) = 0
			bufpt = zExtra
			goto _45
		}
	_34:
		;
		if !(bArgList != 0) {
			**(**int32)(__ccgo_up(libc.VaUintptr(&ap))) = int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar)
		}
		v2 = libc.Int32FromInt32(0)
		width = v2
		length = v2
		goto _45
	_35:
		;
		(**(**[70]int8)(__ccgo_up(bp)))[0] = int8('%')
		bufpt = bp
		length = int32(1)
		goto _45
	_36:
		;
		if bArgList != 0 {
			bufpt = _getTextArg(tls, pArgList)
			length = int32(1)
			if bufpt != 0 {
				v4 = bufpt
				bufpt = bufpt + 1
				v2 = int32(**(**int8)(__ccgo_up(v4)))
				c = v2
				(**(**[70]int8)(__ccgo_up(bp)))[0] = int8(v2)
				if c&int32(0xc0) == int32(0xc0) {
					for length < int32(4) && int32(**(**int8)(__ccgo_up(bufpt)))&int32(0xc0) == int32(0x80) {
						v2 = length
						length = length + 1
						v4 = bufpt
						bufpt = bufpt + 1
						(**(**[70]int8)(__ccgo_up(bp)))[v2] = **(**int8)(__ccgo_up(v4))
					}
				}
			} else {
				(**(**[70]int8)(__ccgo_up(bp)))[0] = 0
			}
		} else {
			ch = libc.VaUint32(&ap)
			length = _sqlite3AppendOneUtf8Character(tls, bp, ch)
		}
		if precision > int32(1) {
			nPrior = int64(1)
			width = width - (precision - int32(1))
			if width > int32(1) && !(flag_leftjustify != 0) {
				Xsqlite3_str_appendchar(tls, pAccum, width-int32(1), int8(' '))
				width = 0
			}
			Xsqlite3_str_append(tls, pAccum, bp, length)
			precision = precision - 1
			for precision > int32(1) {
				if nPrior > int64(precision-int32(1)) {
					nPrior = int64(precision - int32(1))
				}
				nCopyBytes = int64(length) * nPrior
				if _sqlite3StrAccumEnlargeIfNeeded(tls, pAccum, nCopyBytes) != 0 {
					break
				}
				Xsqlite3_str_append(tls, pAccum, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FzText+uintptr(int64((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar)-nCopyBytes), int32(nCopyBytes))
				precision = int32(int64(precision) - nPrior)
				nPrior = nPrior * int64(2)
			}
		}
		bufpt = bp
		flag_altform2 = uint8(1)
		goto adjust_width_for_utf8
	_38:
		;
	_37:
		;
		if bArgList != 0 {
			bufpt = _getTextArg(tls, pArgList)
			xtype = uint8(etSTRING)
		} else {
			bufpt = libc.VaUintptr(&ap)
		}
		if bufpt == uintptr(0) {
			bufpt = __ccgo_ts + 1711
		} else {
			if int32(xtype) == int32(etDYNSTRING) {
				if (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar == uint32(0) && (*Tsqlite3_str)(unsafe.Pointer(pAccum)).FmxAlloc != 0 && width == 0 && precision < 0 && int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FaccError) == 0 {
					/* Special optimization for sqlite3_mprintf("%z..."):
					 ** Extend an existing memory allocation rather than creating
					 ** a new one. */
					(*Tsqlite3_str)(unsafe.Pointer(pAccum)).FzText = bufpt
					(*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnAlloc = uint32(_sqlite3DbMallocSize(tls, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).Fdb, bufpt))
					(*Tsqlite3_str)(unsafe.Pointer(pAccum)).FnChar = uint32(int32(0x7fffffff) & int32(libc.Xstrlen(tls, bufpt)))
					v4 = pAccum + 21
					*(*Tu8)(unsafe.Pointer(v4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v4))) | libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED))
					length = 0
					goto _45
				}
				zExtra = bufpt
			}
		}
		if precision >= 0 {
			if flag_altform2 != 0 {
				/* Set length to the number of bytes needed in order to display
				 ** precision characters */
				z = bufpt
				for {
					v2 = precision
					precision = precision - 1
					if !(v2 > 0 && **(**uint8)(__ccgo_up(z)) != 0) {
						break
					}
					v4 = z
					z = z + 1
					if int32(**(**uint8)(__ccgo_up(v4))) >= int32(0xc0) {
						for int32(**(**uint8)(__ccgo_up(z)))&int32(0xc0) == int32(0x80) {
							z = z + 1
						}
					}
				}
				length = int32(z) - int32(bufpt)
			} else {
				length = 0
				for {
					if !(length < precision && **(**int8)(__ccgo_up(bufpt + uintptr(length))) != 0) {
						break
					}
					goto _86
				_86:
					;
					length = length + 1
				}
			}
		} else {
			length = int32(0x7fffffff) & int32(libc.Xstrlen(tls, bufpt))
		}
		goto adjust_width_for_utf8
	adjust_width_for_utf8:
		;
		if flag_altform2 != 0 && width > 0 {
			/* Adjust width to account for extra bytes in UTF-8 characters */
			ii = length - int32(1)
			for ii >= 0 {
				v2 = ii
				ii = ii - 1
				if int32(**(**int8)(__ccgo_up(bufpt + uintptr(v2))))&int32(0xc0) == int32(0x80) {
					width = width + 1
				}
			}
		}
		goto _45
	_41:
		; /* %q: Escape ' characters */
	_40:
		; /* %Q: Escape ' and enclose in '...' */
	_39:
		;
		needQuote = 0
		if bArgList != 0 {
			escarg = _getTextArg(tls, pArgList)
		} else {
			escarg = libc.VaUintptr(&ap)
		}
		if escarg == uintptr(0) {
			if int32(xtype) == int32(etESCAPE_Q) {
				v4 = __ccgo_ts + 1712
			} else {
				v4 = __ccgo_ts + 1717
			}
			escarg = v4
		} else {
			if int32(xtype) == int32(etESCAPE_Q) {
				needQuote = int32(1)
			}
		}
		if int32(xtype) == int32(etESCAPE_w) {
			q = int8('"')
			flag_alternateform = uint8(0)
		} else {
			q = int8('\'')
		}
		/* For %q, %Q, and %w, the precision is the number of bytes (or
		 ** characters if the ! flags is present) to use from the input.
		 ** Because of the extra quoting characters inserted, the number
		 ** of output characters may be larger than the precision.
		 */
		k = int64(precision)
		v90 = libc.Int64FromInt32(0)
		n1 = v90
		i = v90
		for {
			if v12 = k != 0; v12 {
				v54 = **(**int8)(__ccgo_up(escarg + uintptr(i)))
				ch1 = v54
			}
			if !(v12 && int32(v54) != 0) {
				break
			}
			if int32(ch1) == int32(q) {
				n1 = n1 + 1
			}
			if flag_altform2 != 0 && int32(ch1)&int32(0xc0) == int32(0xc0) {
				for int32(**(**int8)(__ccgo_up(escarg + uintptr(i+int64(1)))))&int32(0xc0) == int32(0x80) {
					i = i + 1
				}
			}
			goto _89
		_89:
			;
			i = i + 1
			k = k - 1
		}
		if flag_alternateform != 0 {
			/* For %#q, do unistr()-style backslash escapes for
			 ** all control characters, and for backslash itself.
			 ** For %#Q, do the same but only if there is at least
			 ** one control character. */
			nBack = 0
			nCtrl = 0
			k = 0
			for {
				if !(k < i) {
					break
				}
				if int32(**(**int8)(__ccgo_up(escarg + uintptr(k)))) == int32('\\') {
					nBack = nBack + 1
				} else {
					if int32(**(**Tu8)(__ccgo_up(escarg + uintptr(k)))) <= int32(0x1f) {
						nCtrl = nCtrl + 1
					}
				}
				goto _93
			_93:
				;
				k = k + 1
			}
			if nCtrl != 0 || int32(xtype) == int32(etESCAPE_q) {
				n1 = n1 + (nBack + int64(5)*nCtrl)
				if int32(xtype) == int32(etESCAPE_Q) {
					n1 = n1 + int64(10)
					needQuote = int32(2)
				}
			} else {
				flag_alternateform = uint8(0)
			}
		}
		n1 = n1 + (i + int64(3))
		if n1 > int64(SQLITE_PRINT_BUF_SIZE) {
			v4 = _printfTempBuf(tls, pAccum, n1)
			zExtra = v4
			bufpt = v4
			if bufpt == uintptr(0) {
				return
			}
		} else {
			bufpt = bp
		}
		j1 = 0
		if needQuote != 0 {
			if needQuote == int32(2) {
				libc.Xmemcpy(tls, bufpt+uintptr(j1), __ccgo_ts+1724, uint32(8))
				j1 = j1 + int64(8)
			} else {
				v90 = j1
				j1 = j1 + 1
				**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = int8('\'')
			}
		}
		k = i
		if flag_alternateform != 0 {
			i = 0
			for {
				if !(i < k) {
					break
				}
				v90 = j1
				j1 = j1 + 1
				v54 = **(**int8)(__ccgo_up(escarg + uintptr(i)))
				ch1 = v54
				**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = v54
				if int32(ch1) == int32(q) {
					v90 = j1
					j1 = j1 + 1
					**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = ch1
				} else {
					if int32(ch1) == int32('\\') {
						v90 = j1
						j1 = j1 + 1
						**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = int8('\\')
					} else {
						if int32(uint8(ch1)) <= int32(0x1f) {
							**(**int8)(__ccgo_up(bufpt + uintptr(j1-int64(1)))) = int8('\\')
							v90 = j1
							j1 = j1 + 1
							**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = int8('u')
							v90 = j1
							j1 = j1 + 1
							**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = int8('0')
							v90 = j1
							j1 = j1 + 1
							**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = int8('0')
							v90 = j1
							j1 = j1 + 1
							if int32(ch1) >= int32(0x10) {
								v2 = int32('1')
							} else {
								v2 = int32('0')
							}
							**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = int8(v2)
							v90 = j1
							j1 = j1 + 1
							**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = **(**int8)(__ccgo_up(__ccgo_ts + 1733 + uintptr(int32(ch1)&int32(0xf))))
						}
					}
				}
				goto _96
			_96:
				;
				i = i + 1
			}
		} else {
			i = 0
			for {
				if !(i < k) {
					break
				}
				v90 = j1
				j1 = j1 + 1
				v54 = **(**int8)(__ccgo_up(escarg + uintptr(i)))
				ch1 = v54
				**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = v54
				if int32(ch1) == int32(q) {
					v90 = j1
					j1 = j1 + 1
					**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = ch1
				}
				goto _107
			_107:
				;
				i = i + 1
			}
		}
		if needQuote != 0 {
			v90 = j1
			j1 = j1 + 1
			**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = int8('\'')
			if needQuote == int32(2) {
				v90 = j1
				j1 = j1 + 1
				**(**int8)(__ccgo_up(bufpt + uintptr(v90))) = int8(')')
			}
		}
		**(**int8)(__ccgo_up(bufpt + uintptr(j1))) = 0
		length = int32(j1)
		goto adjust_width_for_utf8
	_42:
		;
		if int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FprintfFlags)&int32(SQLITE_PRINTF_INTERNAL) == 0 {
			return
		}
		if flag_alternateform != 0 {
			/* %#T means an Expr pointer that uses Expr.u.zToken */
			pExpr = libc.VaUintptr(&ap)
			if pExpr != 0 && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != libc.Uint32FromInt32(0)) {
				Xsqlite3_str_appendall(tls, pAccum, *(*uintptr)(unsafe.Pointer(pExpr + 8)))
				_sqlite3RecordErrorOffsetOfExpr(tls, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).Fdb, pExpr)
			}
		} else {
			/* %T means a Token pointer */
			pToken = libc.VaUintptr(&ap)
			if pToken != 0 && (*TToken)(unsafe.Pointer(pToken)).Fn != 0 {
				Xsqlite3_str_append(tls, pAccum, (*TToken)(unsafe.Pointer(pToken)).Fz, int32((*TToken)(unsafe.Pointer(pToken)).Fn))
				_sqlite3RecordErrorByteOffset(tls, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).Fdb, (*TToken)(unsafe.Pointer(pToken)).Fz)
			}
		}
		v2 = libc.Int32FromInt32(0)
		width = v2
		length = v2
		goto _45
	_43:
		;
		if int32((*Tsqlite3_str)(unsafe.Pointer(pAccum)).FprintfFlags)&int32(SQLITE_PRINTF_INTERNAL) == 0 {
			return
		}
		pItem = libc.VaUintptr(&ap)
		if (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != 0 && !(flag_altform2 != 0) {
			Xsqlite3_str_appendall(tls, pAccum, (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias)
		} else {
			if (*TSrcItem)(unsafe.Pointer(pItem)).FzName != 0 {
				if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x10000>>16) == 0 && int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4>>2) == 0 && *(*uintptr)(unsafe.Pointer(pItem + 44)) != uintptr(0) {
					Xsqlite3_str_appendall(tls, pAccum, *(*uintptr)(unsafe.Pointer(pItem + 44)))
					Xsqlite3_str_append(tls, pAccum, __ccgo_ts+1750, int32(1))
				}
				Xsqlite3_str_appendall(tls, pAccum, (*TSrcItem)(unsafe.Pointer(pItem)).FzName)
			} else {
				if (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != 0 {
					Xsqlite3_str_appendall(tls, pAccum, (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias)
				} else {
					if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4>>2) != 0 { /* Because of tag-20240424-1 */
						pSel = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 44)))).FpSelect
						if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_NestedFrom) != 0 {
							Xsqlite3_str_appendf(tls, pAccum, __ccgo_ts+1752, libc.VaList(bp+120, (*TSelect)(unsafe.Pointer(pSel)).FselId))
						} else {
							if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_MultiValue) != 0 {
								Xsqlite3_str_appendf(tls, pAccum, __ccgo_ts+1762, libc.VaList(bp+120, *(*Tu32)(unsafe.Pointer(pItem + 32))))
							} else {
								Xsqlite3_str_appendf(tls, pAccum, __ccgo_ts+1783, libc.VaList(bp+120, (*TSelect)(unsafe.Pointer(pSel)).FselId))
							}
						}
					}
				}
			}
		}
		v2 = libc.Int32FromInt32(0)
		width = v2
		length = v2
		goto _45
	_44:
		;
		return
	_45:
		; /* End switch over the format type */
		/*
		 ** The text of the conversion is pointed to by "bufpt" and is
		 ** "length" characters long.  The field width is "width".  Do
		 ** the output.  Both length and width are in bytes, not characters,
		 ** at this point.  If the "!" flag was present on string conversions
		 ** indicating that width and precision should be expressed in characters,
		 ** then the values have been translated prior to reaching this point.
		 */
		width = width - length
		if width > 0 {
			if !(flag_leftjustify != 0) {
				Xsqlite3_str_appendchar(tls, pAccum, width, int8(' '))
			}
			Xsqlite3_str_append(tls, pAccum, bufpt, length)
			if flag_leftjustify != 0 {
				Xsqlite3_str_appendchar(tls, pAccum, width, int8(' '))
			}
		} else {
			Xsqlite3_str_append(tls, pAccum, bufpt, length)
		}
		if zExtra != 0 {
			_sqlite3DbFree(tls, (*Tsqlite3_str)(unsafe.Pointer(pAccum)).Fdb, zExtra)
			zExtra = uintptr(0)
		}
		goto _1
	_1:
		;
		fmt = fmt + 1
	} /* End for loop over the format string */
}

// C documentation
//
//	/*
//	** Return meta information about a specific column of a database table.
//	** See comment in sqlite3.h (sqlite.h.in) for details.
//	*/
func Xsqlite3_table_column_metadata(tls *libc.TLS, db uintptr, zDbName uintptr, zTableName uintptr, zColumnName uintptr, pzDataType uintptr, pzCollSeq uintptr, pNotNull uintptr, pPrimaryKey uintptr, pAutoinc uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var autoinc, iCol, notnull, primarykey, rc int32
	var pCol, pTab, zCollSeq, zDataType, v1 uintptr
	var _ /* zErrMsg at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _ = autoinc, iCol, notnull, pCol, pTab, primarykey, rc, zCollSeq, zDataType, v1
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	pTab = uintptr(0)
	pCol = uintptr(0)
	iCol = 0
	zDataType = uintptr(0)
	zCollSeq = uintptr(0)
	notnull = 0
	primarykey = 0
	autoinc = 0
	/* Ensure the database schema has been loaded */
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	_sqlite3BtreeEnterAll(tls, db)
	rc = _sqlite3Init(tls, db, bp)
	if SQLITE_OK != rc {
		goto error_out
	}
	/* Locate the table in question */
	pTab = _sqlite3FindTable(tls, db, zTableName, zDbName)
	if !(pTab != 0) || int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
		pTab = uintptr(0)
		goto error_out
	}
	/* Find the column for which info is requested */
	if zColumnName == uintptr(0) {
		/* Query for existence of table only */
	} else {
		iCol = _sqlite3ColumnIndex(tls, pTab, zColumnName)
		if iCol >= 0 {
			pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12
		} else {
			if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) && _sqlite3IsRowid(tls, zColumnName) != 0 {
				iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey)
				if iCol >= 0 {
					v1 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12
				} else {
					v1 = uintptr(0)
				}
				pCol = v1
			} else {
				pTab = uintptr(0)
				goto error_out
			}
		}
	}
	/* The following block stores the meta information that will be returned
	 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
	 ** and autoinc. At this point there are two possibilities:
	 **
	 **     1. The specified column name was rowid", "oid" or "_rowid_"
	 **        and there is no explicitly declared IPK column.
	 **
	 **     2. The table is not a view and the column name identified an
	 **        explicitly declared column. Copy meta information from *pCol.
	 */
	if pCol != 0 {
		zDataType = _sqlite3ColumnType(tls, pCol, uintptr(0))
		zCollSeq = _sqlite3ColumnColl(tls, pCol)
		notnull = libc.BoolInt32(int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) != 0)
		primarykey = libc.BoolInt32(int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0)
		autoinc = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) == iCol && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Autoincrement) != uint32(0))
	} else {
		zDataType = __ccgo_ts + 1185
		primarykey = int32(1)
	}
	if !(zCollSeq != 0) {
		zCollSeq = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
	}
	goto error_out
error_out:
	;
	_sqlite3BtreeLeaveAll(tls, db)
	/* Whether the function call succeeded or failed, set the output parameters
	 ** to whatever their local counterparts contain. If an error did occur,
	 ** this has the effect of zeroing all output parameters.
	 */
	if pzDataType != 0 {
		**(**uintptr)(__ccgo_up(pzDataType)) = zDataType
	}
	if pzCollSeq != 0 {
		**(**uintptr)(__ccgo_up(pzCollSeq)) = zCollSeq
	}
	if pNotNull != 0 {
		**(**int32)(__ccgo_up(pNotNull)) = notnull
	}
	if pPrimaryKey != 0 {
		**(**int32)(__ccgo_up(pPrimaryKey)) = primarykey
	}
	if pAutoinc != 0 {
		**(**int32)(__ccgo_up(pAutoinc)) = autoinc
	}
	if SQLITE_OK == rc && !(pTab != 0) {
		_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
		**(**uintptr)(__ccgo_up(bp)) = _sqlite3MPrintf(tls, db, __ccgo_ts+27577, libc.VaList(bp+16, zTableName, zColumnName))
		rc = int32(SQLITE_ERROR)
	}
	if **(**uintptr)(__ccgo_up(bp)) != 0 {
		v1 = __ccgo_ts + 4729
	} else {
		v1 = uintptr(0)
	}
	_sqlite3ErrorWithMsg(tls, db, rc, v1, libc.VaList(bp+16, **(**uintptr)(__ccgo_up(bp))))
	_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
	rc = _sqlite3ApiExit(tls, db, rc)
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Interface to the testing logic.
//	*/
func Xsqlite3_test_control(tls *libc.TLS, op int32, va uintptr) (r int32) {
	var aProg, db, db1, db2, db3, db4, db5, db6, db7, pCtx, pI1, pI2, pN, pR, pU64, pn, ptr, xBenignBegin, xBenignEnd, z uintptr
	var ap Tva_list
	var b, iDb, opTrace, rc, sz, x, x1, x2, y, v1 int32
	var newVal uint32
	var rIn float64
	var rLogEst TLogEst
	var v2 bool
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aProg, ap, b, db, db1, db2, db3, db4, db5, db6, db7, iDb, newVal, opTrace, pCtx, pI1, pI2, pN, pR, pU64, pn, ptr, rIn, rLogEst, rc, sz, x, x1, x2, xBenignBegin, xBenignEnd, y, z, v1, v2
	rc = 0
	ap = va
	switch op {
	/*
	 ** Save the current state of the PRNG.
	 */
	case int32(SQLITE_TESTCTRL_PRNG_SAVE):
		_sqlite3PrngSaveState(tls)
		break
		/*
		 ** Restore the state of the PRNG to the last state saved using
		 ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
		 ** this verb acts like PRNG_RESET.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_PRNG_RESTORE):
		_sqlite3PrngRestoreState(tls)
		break
		/*  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
		 **
		 ** Control the seed for the pseudo-random number generator (PRNG) that
		 ** is built into SQLite.  Cases:
		 **
		 **    x!=0 && db!=0       Seed the PRNG to the current value of the
		 **                        schema cookie in the main database for db, or
		 **                        x if the schema cookie is zero.  This case
		 **                        is convenient to use with database fuzzers
		 **                        as it allows the fuzzer some control over the
		 **                        the PRNG seed.
		 **
		 **    x!=0 && db==0       Seed the PRNG to the value of x.
		 **
		 **    x==0 && db==0       Revert to default behavior of using the
		 **                        xRandomness method on the primary VFS.
		 **
		 ** This test-control also resets the PRNG so that the new seed will
		 ** be used for the next call to sqlite3_randomness().
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_PRNG_SEED):
		x = libc.VaInt32(&ap)
		db = libc.VaUintptr(&ap)
		if v2 = db != 0; v2 {
			v1 = (*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).Fschema_cookie
			y = v1
		}
		if v2 && v1 != 0 {
			x = y
		}
		_sqlite3Config.FiPrngSeed = uint32(x)
		Xsqlite3_randomness(tls, 0, uintptr(0))
		break
		/*  sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, sqlite3 *db, int b);
		 **
		 ** If b is true, then activate the SQLITE_FkNoAction setting.  If b is
		 ** false then clear that setting.  If the SQLITE_FkNoAction setting is
		 ** enabled, all foreign key ON DELETE and ON UPDATE actions behave as if
		 ** they were NO ACTION, regardless of how they are defined.
		 **
		 ** NB:  One must usually run "PRAGMA writable_schema=RESET" after
		 ** using this test-control, before it will take full effect.  failing
		 ** to reset the schema can result in some unexpected behavior.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_FK_NO_ACTION):
		db1 = libc.VaUintptr(&ap)
		b = libc.VaInt32(&ap)
		if b != 0 {
			**(**Tu64)(__ccgo_up(db1 + 32)) |= uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32)
		} else {
			**(**Tu64)(__ccgo_up(db1 + 32)) &= ^(uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32))
		}
		break
		/*
		 **  sqlite3_test_control(BITVEC_TEST, size, program)
		 **
		 ** Run a test against a Bitvec object of size.  The program argument
		 ** is an array of integers that defines the test.  Return -1 on a
		 ** memory allocation error, 0 on success, or non-zero for an error.
		 ** See the sqlite3BitvecBuiltinTest() for additional information.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_BITVEC_TEST):
		sz = libc.VaInt32(&ap)
		aProg = libc.VaUintptr(&ap)
		rc = _sqlite3BitvecBuiltinTest(tls, sz, aProg)
		break
		/*
		 **  sqlite3_test_control(FAULT_INSTALL, xCallback)
		 **
		 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
		 ** if xCallback is not NULL.
		 **
		 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
		 ** is called immediately after installing the new callback and the return
		 ** value from sqlite3FaultSim(0) becomes the return from
		 ** sqlite3_test_control().
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_FAULT_INSTALL):
		_sqlite3Config.FxTestCallback = libc.VaUintptr(&ap)
		rc = _sqlite3FaultSim(tls, 0)
		break
		/*
		 **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
		 **
		 ** Register hooks to call to indicate which malloc() failures
		 ** are benign.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS):
		xBenignBegin = libc.VaUintptr(&ap)
		xBenignEnd = libc.VaUintptr(&ap)
		_sqlite3BenignMallocHooks(tls, xBenignBegin, xBenignEnd)
		break
		/*
		 **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
		 **
		 ** Set the PENDING byte to the value in the argument, if X>0.
		 ** Make no changes if X==0.  Return the value of the pending byte
		 ** as it existing before this routine was called.
		 **
		 ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
		 ** an incompatible database file format.  Changing the PENDING byte
		 ** while any database connection is open results in undefined and
		 ** deleterious behavior.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_PENDING_BYTE):
		rc = _sqlite3PendingByte
		newVal = libc.VaUint32(&ap)
		if newVal != 0 {
			_sqlite3PendingByte = int32(newVal)
		}
		break
		/*
		 **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
		 **
		 ** This action provides a run-time test to see whether or not
		 ** assert() was enabled at compile-time.  If X is true and assert()
		 ** is enabled, then the return value is true.  If X is true and
		 ** assert() is disabled, then the return value is zero.  If X is
		 ** false and assert() is enabled, then the assertion fires and the
		 ** process aborts.  If X is false and assert() is disabled, then the
		 ** return value is zero.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_ASSERT):
		x1 = 0
		rc = x1
		break
		/*
		 **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
		 **
		 ** This action provides a run-time test to see how the ALWAYS and
		 ** NEVER macros were defined at compile-time.
		 **
		 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
		 **
		 ** The recommended test is X==2.  If the return value is 2, that means
		 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
		 ** default setting.  If the return value is 1, then ALWAYS() is either
		 ** hard-coded to true or else it asserts if its argument is false.
		 ** The first behavior (hard-coded to true) is the case if
		 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
		 ** behavior (assert if the argument to ALWAYS() is false) is the case if
		 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
		 **
		 ** The run-time test procedure might look something like this:
		 **
		 **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
		 **      // ALWAYS() and NEVER() are no-op pass-through macros
		 **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
		 **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
		 **    }else{
		 **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
		 **    }
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_ALWAYS):
		x2 = libc.VaInt32(&ap)
		if x2 != 0 {
			v1 = x2
		} else {
			v1 = 0
		}
		rc = v1
		break
		/*
		 **   sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
		 **
		 ** The integer returned reveals the byte-order of the computer on which
		 ** SQLite is running:
		 **
		 **       1     big-endian,    determined at run-time
		 **      10     little-endian, determined at run-time
		 **  432101     big-endian,    determined at compile-time
		 **  123410     little-endian, determined at compile-time
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_BYTEORDER):
		rc = libc.Int32FromInt32(SQLITE_BYTEORDER)*libc.Int32FromInt32(100) + libc.Int32FromInt32(SQLITE_LITTLEENDIAN)*libc.Int32FromInt32(10) + libc.Int32FromInt32(SQLITE_BIGENDIAN)
		break
		/*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
		 **
		 ** Enable or disable various optimizations for testing purposes.  The
		 ** argument N is a bitmask of optimizations to be disabled.  For normal
		 ** operation N should be 0.  The idea is that a test program (like the
		 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
		 ** with various optimizations disabled to verify that the same answer
		 ** is obtained in every case.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_OPTIMIZATIONS):
		db2 = libc.VaUintptr(&ap)
		(*Tsqlite3)(unsafe.Pointer(db2)).FdbOptFlags = libc.VaUint32(&ap)
		break
		/*  sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, sqlite3 *db, int *N)
		 **
		 ** Write the current optimization settings into *N.  A zero bit means that
		 ** the optimization is on, and a 1 bit means that the optimization is off.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_GETOPT):
		db3 = libc.VaUintptr(&ap)
		pN = libc.VaUintptr(&ap)
		**(**int32)(__ccgo_up(pN)) = int32((*Tsqlite3)(unsafe.Pointer(db3)).FdbOptFlags)
		break
		/*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt);
		 **
		 ** If parameter onoff is 1, subsequent calls to localtime() fail.
		 ** If 2, then invoke xAlt() instead of localtime().  If 0, normal
		 ** processing.
		 **
		 ** xAlt arguments are void pointers, but they really want to be:
		 **
		 **    int xAlt(const time_t*, struct tm*);
		 **
		 ** xAlt should write results in to struct tm object of its 2nd argument
		 ** and return zero on success, or return non-zero on failure.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_LOCALTIME_FAULT):
		_sqlite3Config.FbLocaltimeFault = libc.VaInt32(&ap)
		if _sqlite3Config.FbLocaltimeFault == int32(2) {
			_sqlite3Config.FxAltLocaltime = libc.VaUintptr(&ap)
		} else {
			_sqlite3Config.FxAltLocaltime = uintptr(0)
		}
		break
		/*   sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*);
		 **
		 ** Toggle the ability to use internal functions on or off for
		 ** the database connection given in the argument.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS):
		db4 = libc.VaUintptr(&ap)
		**(**Tu32)(__ccgo_up(db4 + 24)) ^= uint32(DBFLAG_InternalFunc)
		break
		/*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
		 **
		 ** Set or clear a flag that indicates that the database file is always well-
		 ** formed and never corrupt.  This flag is clear by default, indicating that
		 ** database files might have arbitrary corruption.  Setting the flag during
		 ** testing causes certain assert() statements in the code to be activated
		 ** that demonstrate invariants on well-formed database files.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_NEVER_CORRUPT):
		_sqlite3Config.FneverCorrupt = libc.VaInt32(&ap)
		break
		/*   sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
		 **
		 ** Set or clear a flag that causes SQLite to verify that type, name,
		 ** and tbl_name fields of the sqlite_schema table.  This is normally
		 ** on, but it is sometimes useful to turn it off for testing.
		 **
		 ** 2020-07-22:  Disabling EXTRA_SCHEMA_CHECKS also disables the
		 ** verification of rootpage numbers when parsing the schema.  This
		 ** is useful to make it easier to reach strange internal error states
		 ** during testing.  The EXTRA_SCHEMA_CHECKS setting is always enabled
		 ** in production.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS):
		_sqlite3Config.FbExtraSchemaChecks = uint8(libc.VaInt32(&ap))
		break
		/* Set the threshold at which OP_Once counters reset back to zero.
		 ** By default this is 0x7ffffffe (over 2 billion), but that value is
		 ** too big to test in a reasonable amount of time, so this control is
		 ** provided to set a small and easily reachable reset value.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD):
		_sqlite3Config.FiOnceResetThreshold = libc.VaInt32(&ap)
		break
		/*   sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
		 **
		 ** Set the VDBE coverage callback function to xCallback with context
		 ** pointer ptr.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_VDBE_COVERAGE):
		break
		/*   sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
		fallthrough
	case int32(SQLITE_TESTCTRL_SORTER_MMAP):
		db5 = libc.VaUintptr(&ap)
		(*Tsqlite3)(unsafe.Pointer(db5)).FnMaxSorterMmap = libc.VaInt32(&ap)
		break
		/*   sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
		 **
		 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
		 ** not.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_ISINIT):
		if libc.AtomicLoadPInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+208) == 0 {
			rc = int32(SQLITE_ERROR)
		}
		break
		/*  sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, mode, tnum);
		 **
		 ** This test control is used to create imposter tables.  "db" is a pointer
		 ** to the database connection.  dbName is the database name (ex: "main" or
		 ** "temp") which will receive the imposter.  "mode" turns imposter mode on
		 ** or off.  mode==0 means imposter mode is off.  mode==1 means imposter mode
		 ** is on.  mode==2 means imposter mode is on but results in an imposter
		 ** table that is read-only unless writable_schema is on.  "tnum" is the
		 ** root page of the b-tree to which the imposter table should connect.
		 **
		 ** Enable imposter mode only when the schema has already been parsed.  Then
		 ** run a single CREATE TABLE statement to construct the imposter table in
		 ** the parsed schema.  Then turn imposter mode back off again.
		 **
		 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
		 ** the schema to be reparsed the next time it is needed.  This has the
		 ** effect of erasing all imposter tables.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_IMPOSTER):
		db6 = libc.VaUintptr(&ap)
		Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db6)).Fmutex)
		iDb = _sqlite3FindDbName(tls, db6, libc.VaUintptr(&ap))
		if iDb >= 0 {
			(*Tsqlite3)(unsafe.Pointer(db6)).Finit1.FiDb = uint8(iDb)
			(*Tsqlite3)(unsafe.Pointer(db6)).Finit1.Fbusy = uint8(libc.AssignBitFieldPtr8Uint32(db6+176+8, uint32(libc.VaInt32(&ap)), 2, 1, 0x6))
			(*Tsqlite3)(unsafe.Pointer(db6)).Finit1.FnewTnum = uint32(libc.VaInt32(&ap))
			if int32((*Tsqlite3)(unsafe.Pointer(db6)).Finit1.Fbusy) == 0 && (*Tsqlite3)(unsafe.Pointer(db6)).Finit1.FnewTnum > uint32(0) {
				_sqlite3ResetAllSchemasOfConnection(tls, db6)
			}
		}
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db6)).Fmutex)
		break
		/*  sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
		 **
		 ** This test-control causes the most recent sqlite3_result_int64() value
		 ** to be interpreted as a MEM_IntReal instead of as an MEM_Int.  Normally,
		 ** MEM_IntReal values only arise during an INSERT operation of integer
		 ** values into a REAL column, so they can be challenging to test.  This
		 ** test-control enables us to write an intreal() SQL function that can
		 ** inject an intreal() value at arbitrary places in an SQL statement,
		 ** for testing purposes.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_RESULT_INTREAL):
		pCtx = libc.VaUintptr(&ap)
		_sqlite3ResultIntReal(tls, pCtx)
		break
		/*  sqlite3_test_control(SQLITE_TESTCTRL_SEEK_COUNT,
		 **    sqlite3 *db,    // Database connection
		 **    u64 *pnSeek     // Write seek count here
		 **  );
		 **
		 ** This test-control queries the seek-counter on the "main" database
		 ** file.  The seek-counter is written into *pnSeek and is then reset.
		 ** The seek-count is only available if compiled with SQLITE_DEBUG.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_SEEK_COUNT):
		db7 = libc.VaUintptr(&ap)
		pn = libc.VaUintptr(&ap)
		**(**Tu64)(__ccgo_up(pn)) = uint64(0)
		_ = db7 /* Silence harmless unused variable warning */
		break
		/*  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, op, ptr)
		 **
		 **  "ptr" is a pointer to a u32.
		 **
		 **   op==0       Store the current sqlite3TreeTrace in *ptr
		 **   op==1       Set sqlite3TreeTrace to the value *ptr
		 **   op==2       Store the current sqlite3WhereTrace in *ptr
		 **   op==3       Set sqlite3WhereTrace to the value *ptr
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_TRACEFLAGS):
		opTrace = libc.VaInt32(&ap)
		ptr = libc.VaUintptr(&ap)
		switch opTrace {
		case 0:
			**(**Tu32)(__ccgo_up(ptr)) = _sqlite3TreeTrace
		case int32(1):
			_sqlite3TreeTrace = **(**Tu32)(__ccgo_up(ptr))
		case int32(2):
			**(**Tu32)(__ccgo_up(ptr)) = _sqlite3WhereTrace
		case int32(3):
			_sqlite3WhereTrace = **(**Tu32)(__ccgo_up(ptr))
			break
		}
		break
		/* sqlite3_test_control(SQLITE_TESTCTRL_LOGEST,
		 **      double fIn,     // Input value
		 **      int *pLogEst,   // sqlite3LogEstFromDouble(fIn)
		 **      u64 *pInt,      // sqlite3LogEstToInt(*pLogEst)
		 **      int *pLogEst2   // sqlite3LogEst(*pInt)
		 ** );
		 **
		 ** Test access for the LogEst conversion routines.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_LOGEST):
		rIn = libc.VaFloat64(&ap)
		rLogEst = _sqlite3LogEstFromDouble(tls, rIn)
		pI1 = libc.VaUintptr(&ap)
		pU64 = libc.VaUintptr(&ap)
		pI2 = libc.VaUintptr(&ap)
		**(**int32)(__ccgo_up(pI1)) = int32(rLogEst)
		**(**Tu64)(__ccgo_up(pU64)) = _sqlite3LogEstToInt(tls, rLogEst)
		**(**int32)(__ccgo_up(pI2)) = int32(_sqlite3LogEst(tls, **(**Tu64)(__ccgo_up(pU64))))
		break
		/* sqlite3_test_control(SQLITE_TESTCTRL_ATOF, const char *z, double *p);
		 **
		 ** Test access to the sqlite3AtoF() routine.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_ATOF):
		z = libc.VaUintptr(&ap)
		pR = libc.VaUintptr(&ap)
		rc = _sqlite3AtoF(tls, z, pR)
		break
		/* sqlite3_test_control(SQLITE_TESTCTRL_JSON_SELFCHECK, &onOff);
		 **
		 ** Activate or deactivate validation of JSONB that is generated from
		 ** text.  Off by default, as the validation is slow.  Validation is
		 ** only available if compiled using SQLITE_DEBUG.
		 **
		 ** If onOff is initially 1, then turn it on.  If onOff is initially
		 ** off, turn it off.  If onOff is initially -1, then change onOff
		 ** to be the current setting.
		 */
		fallthrough
	case int32(SQLITE_TESTCTRL_JSON_SELFCHECK):
		break
	}
	_ = ap
	return rc
}

// C documentation
//
//	/*
//	** Register a trace function.  The pArg from the previously registered trace
//	** is returned.
//	**
//	** A NULL trace function means that no tracing is executes.  A non-NULL
//	** trace is a pointer to a function that is invoked at the start of each
//	** SQL statement.
//	*/
func Xsqlite3_trace(tls *libc.TLS, db uintptr, __ccgo_fp_xTrace uintptr, pArg uintptr) (r uintptr) {
	var pOld uintptr
	var v1 int32
	_, _ = pOld, v1
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	pOld = (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg
	if __ccgo_fp_xTrace != 0 {
		v1 = int32(SQLITE_TRACE_LEGACY)
	} else {
		v1 = 0
	}
	(*Tsqlite3)(unsafe.Pointer(db)).FmTrace = uint8(v1)
	*(*uintptr)(unsafe.Pointer(db + 220)) = __ccgo_fp_xTrace
	(*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg = pArg
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return pOld
}

// C documentation
//
//	/**************************** sqlite3_value_  *******************************
//	** The following routines extract information from a Mem or sqlite3_value
//	** structure.
//	*/
func Xsqlite3_value_blob(tls *libc.TLS, pVal uintptr) (r uintptr) {
	var p, v2 uintptr
	var v1 int32
	_, _, _ = p, v1, v2
	p = pVal
	if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Blob)|libc.Int32FromInt32(MEM_Str)) != 0 {
		if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Zero) != 0 {
			v1 = _sqlite3VdbeMemExpandBlob(tls, p)
		} else {
			v1 = 0
		}
		if v1 != SQLITE_OK {
			return uintptr(0)
		}
		v2 = p + 16
		*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(MEM_Blob))
		if (*TMem)(unsafe.Pointer(p)).Fn != 0 {
			v2 = (*TMem)(unsafe.Pointer(p)).Fz
		} else {
			v2 = uintptr(0)
		}
		return v2
	} else {
		return Xsqlite3_value_text(tls, pVal)
	}
	return r
}

// C documentation
//
//	/* Make a copy of an sqlite3_value object
//	*/
func Xsqlite3_value_dup(tls *libc.TLS, pOrig uintptr) (r uintptr) {
	var pNew, v1 uintptr
	_, _ = pNew, v1
	if pOrig == uintptr(0) {
		return uintptr(0)
	}
	pNew = Xsqlite3_malloc(tls, int32(40))
	if pNew == uintptr(0) {
		return uintptr(0)
	}
	libc.Xmemset(tls, pNew, 0, uint32(40))
	libc.Xmemcpy(tls, pNew, pOrig, uint32(libc.UintptrFromInt32(0)+20))
	v1 = pNew + 16
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(MEM_Dyn))
	(*Tsqlite3_value)(unsafe.Pointer(pNew)).Fdb = uintptr(0)
	if int32((*Tsqlite3_value)(unsafe.Pointer(pNew)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 {
		v1 = pNew + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Dyn)))
		v1 = pNew + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Ephem))
		if _sqlite3VdbeMemMakeWriteable(tls, pNew) != SQLITE_OK {
			_sqlite3ValueFree(tls, pNew)
			pNew = uintptr(0)
		}
	} else {
		if int32((*Tsqlite3_value)(unsafe.Pointer(pNew)).Fflags)&int32(MEM_Null) != 0 {
			/* Do not duplicate pointer values */
			v1 = pNew + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(MEM_Term) | libc.Int32FromInt32(MEM_Subtype)))
		}
	}
	return pNew
}

func Xsqlite3_value_pointer(tls *libc.TLS, pVal uintptr, zPType uintptr) (r uintptr) {
	var p uintptr
	_ = p
	p = pVal
	if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Term)|libc.Int32FromInt32(MEM_Subtype)) == libc.Int32FromInt32(MEM_Null)|libc.Int32FromInt32(MEM_Term)|libc.Int32FromInt32(MEM_Subtype) && zPType != uintptr(0) && int32((*TMem)(unsafe.Pointer(p)).FeSubtype) == int32('p') && libc.Xstrcmp(tls, *(*uintptr)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(p)).Fu)), zPType) == 0 {
		return (*TMem)(unsafe.Pointer(p)).Fz
	} else {
		return uintptr(0)
	}
	return r
}

// C documentation
//
//	/*
//	** Checkpoint database zDb.
//	*/
func Xsqlite3_wal_checkpoint_v2(tls *libc.TLS, db uintptr, zDb uintptr, eMode int32, pnLog uintptr, pnCkpt uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iDb, rc int32
	_, _ = iDb, rc /* Schema to checkpoint */
	/* Initialize the output variables to -1 in case an error occurs. */
	if pnLog != 0 {
		**(**int32)(__ccgo_up(pnLog)) = -int32(1)
	}
	if pnCkpt != 0 {
		**(**int32)(__ccgo_up(pnCkpt)) = -int32(1)
	}
	if eMode < -int32(1) || eMode > int32(SQLITE_CHECKPOINT_TRUNCATE) {
		/* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
		 ** mode: */
		return _sqlite3MisuseError(tls, int32(189958))
	}
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	if zDb != 0 && **(**int8)(__ccgo_up(zDb)) != 0 {
		iDb = _sqlite3FindDbName(tls, db, zDb)
	} else {
		iDb = libc.Int32FromInt32(SQLITE_MAX_ATTACHED) + libc.Int32FromInt32(2) /* This means process all schemas */
	}
	if iDb < 0 {
		rc = int32(SQLITE_ERROR)
		_sqlite3ErrorWithMsg(tls, db, int32(SQLITE_ERROR), __ccgo_ts+27259, libc.VaList(bp+8, zDb))
	} else {
		(*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FnBusy = 0
		rc = _sqlite3Checkpoint(tls, db, iDb, eMode, pnLog, pnCkpt)
		_sqlite3Error(tls, db, rc)
	}
	rc = _sqlite3ApiExit(tls, db, rc)
	/* If there are no active statements, clear the interrupt flag at this
	 ** point.  */
	if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive == 0 {
		libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED))
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

/*
** This function outputs the specified (ANSI) string to the Win32 debugger
** (if available).
 */
func Xsqlite3_win32_write_debug(tls *libc.TLS, zBuf uintptr, nBuf int32) {
	bp := tls.Alloc(4096)
	defer tls.Free(4096)
	var nMin, v1 int32
	var _ /* zDbgBuf at bp+0 */ [4092]int8
	_, _ = nMin, v1
	if nBuf < int32(libc.Uint32FromInt32(4096)-libc.Uint32FromInt64(4))-libc.Int32FromInt32(1) {
		v1 = nBuf
	} else {
		v1 = int32(libc.Uint32FromInt32(4096)-libc.Uint32FromInt64(4)) - libc.Int32FromInt32(1)
	}
	nMin = v1 /* may be negative. */
	if nMin < -int32(1) {
		nMin = -int32(1)
	} /* all negative values become -1. */
	if nMin > 0 {
		libc.Xmemset(tls, bp, 0, uint32(int32(libc.Uint32FromInt32(4096)-libc.Uint32FromInt64(4))))
		libc.Xmemcpy(tls, bp, zBuf, uint32(nMin))
		(*(*func(*libc.TLS, TLPCSTR))(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(65)].FpCurrent})))(tls, bp)
	} else {
		(*(*func(*libc.TLS, TLPCSTR))(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(65)].FpCurrent})))(tls, zBuf)
	}
}

// C documentation
//
//	/*
//	** Begin adding a change to a changegroup object.
//	*/
func Xsqlite3changegroup_change_begin(tls *libc.TLS, pGrp uintptr, eOp int32, zTab uintptr, bIndirect int32, pzErr uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aBuf uintptr
	var nReq, rc, v1 int32
	var _ /* pTab at bp+0 */ uintptr
	_, _, _, _ = aBuf, nReq, rc, v1
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	rc = SQLITE_OK
	if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab != 0 {
		rc = int32(SQLITE_MISUSE)
	} else {
		if eOp != int32(SQLITE_INSERT) && eOp != int32(SQLITE_UPDATE) && eOp != int32(SQLITE_DELETE) {
			rc = int32(SQLITE_ERROR)
		} else {
			rc = _sessionChangesetFindTable(tls, pGrp, zTab, uintptr(0), bp)
		}
	}
	if rc == SQLITE_OK {
		if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
			if pzErr != 0 {
				**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+22573, libc.VaList(bp+16, zTab))
			}
			rc = int32(SQLITE_ERROR)
		} else {
			if eOp == int32(SQLITE_UPDATE) {
				v1 = int32(2)
			} else {
				v1 = int32(1)
			}
			nReq = (*TSessionTable)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnCol * v1
			(*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab = **(**uintptr)(__ccgo_up(bp))
			(*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FeOp = eOp
			(*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FbIndirect = bIndirect
			if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FnBufAlloc < nReq {
				aBuf = Xsqlite3_realloc(tls, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FaBuf, int32(uint32(nReq)*uint32(12)))
				if aBuf == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
				} else {
					libc.Xmemset(tls, aBuf+uintptr((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FnBufAlloc)*12, 0, uint32(12)*uint32(nReq-(*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FnBufAlloc))
					(*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FaBuf = aBuf
					(*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FnBufAlloc = nReq
				}
			}
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Configure the change currently under construction with a blob value.
//	*/
func Xsqlite3changegroup_change_blob(tls *libc.TLS, pGrp uintptr, bNew int32, iCol int32, pVal uintptr, nVal int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nByte Tsqlite3_int64
	var rc, v1 int32
	var _ /* pBuf at bp+0 */ uintptr
	_, _, _ = nByte, rc, v1
	nByte = int64(int32(1)+_sessionVarintLen(tls, nVal)) + int64(nVal)
	rc = SQLITE_OK
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	v1 = _checkChangeParams(tls, pGrp, bNew, iCol, nByte, bp)
	rc = v1
	if SQLITE_OK != v1 {
		return rc
	}
	**(**Tu8)(__ccgo_up((*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaBuf)) = uint8(SQLITE_BLOB)
	(*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnBuf = int32(1) + _sessionVarintPut(tls, (*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaBuf+1, nVal)
	libc.Xmemcpy(tls, (*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnBuf), pVal, uint32(nVal))
	**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + 4)) += nVal
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Finish any change currently being constructed by the changegroup object.
//	*/
func Xsqlite3changegroup_change_finish(tls *libc.TLS, pGrp uintptr, bDiscard int32, pzErr uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var aBuf, p, v2, v3 uintptr
	var eUndef Tu8
	var ii, isPK, nBuf, nZero, v7 int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _ = aBuf, eUndef, ii, isPK, nBuf, nZero, p, v2, v3, v7
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab != 0 {
		aBuf = (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FaBuf
		if bDiscard == 0 {
			nBuf = (*TSessionTable)(unsafe.Pointer((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab)).FnCol
			eUndef = uint8(SQLITE_NULL)
			if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FeOp == int32(SQLITE_UPDATE) {
				ii = 0
				for {
					if !(ii < nBuf) {
						break
					}
					if **(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab)).FabPK + uintptr(ii))) != 0 {
						if (**(**TSessionBuffer)(__ccgo_up(aBuf + uintptr(ii)*12))).FnBuf <= int32(1) {
							if (**(**TSessionBuffer)(__ccgo_up(aBuf + uintptr(ii)*12))).FnBuf == int32(1) {
								v2 = __ccgo_ts + 1697
							} else {
								v2 = __ccgo_ts + 38067
							}
							**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38077, libc.VaList(bp+16, v2))
							**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
							break
						} else {
							if (**(**TSessionBuffer)(__ccgo_up(aBuf + uintptr(ii+nBuf)*12))).FnBuf > 0 {
								**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38124, 0)
								**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
								break
							}
						}
					} else {
						if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch == 0 && libc.BoolInt32((**(**TSessionBuffer)(__ccgo_up(aBuf + uintptr(ii)*12))).FnBuf > 0) != libc.BoolInt32((**(**TSessionBuffer)(__ccgo_up(aBuf + uintptr(ii+nBuf)*12))).FnBuf > 0) {
							if (**(**TSessionBuffer)(__ccgo_up(aBuf + uintptr(ii)*12))).FnBuf != 0 {
								v2 = __ccgo_ts + 1711
							} else {
								v2 = __ccgo_ts + 38176
							}
							if (**(**TSessionBuffer)(__ccgo_up(aBuf + uintptr(ii+nBuf)*12))).FnBuf != 0 {
								v3 = __ccgo_ts + 1711
							} else {
								v3 = __ccgo_ts + 38176
							}
							**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38179, libc.VaList(bp+16, ii, v2, v3))
							**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
							break
						}
					}
					goto _1
				_1:
					;
					ii = ii + 1
				}
				eUndef = uint8(0x00)
				if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch == 0 {
					nBuf = nBuf * int32(2)
				}
			} else {
				ii = 0
				for {
					if !(ii < nBuf) {
						break
					}
					isPK = int32(**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab)).FabPK + uintptr(ii))))
					if ((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FeOp == int32(SQLITE_INSERT) || (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch == 0 || isPK != 0) && (**(**TSessionBuffer)(__ccgo_up(aBuf + uintptr(ii)*12))).FnBuf == 0 {
						**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38255, libc.VaList(bp+16, ii))
						**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
						break
					}
					if (**(**TSessionBuffer)(__ccgo_up(aBuf + uintptr(ii)*12))).FnBuf == int32(1) && isPK != 0 {
						**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38294, 0)
						**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
						break
					}
					goto _5
				_5:
					;
					ii = ii + 1
				}
			}
			(*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.Frecord.FnBuf = 0
			ii = 0
			for {
				if !(ii < nBuf) {
					break
				}
				p = (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FaBuf + uintptr(ii)*12
				if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch != 0 {
					if int32(**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab)).FabPK + uintptr(ii)))) == 0 {
						if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FeOp == int32(SQLITE_UPDATE) {
							p = p + uintptr((*TSessionTable)(unsafe.Pointer((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab)).FnCol)*12
						} else {
							if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FeOp == int32(SQLITE_DELETE) {
								goto _6
							}
						}
					}
				}
				if (*TSessionBuffer)(unsafe.Pointer(p)).FnBuf != 0 {
					v7 = (*TSessionBuffer)(unsafe.Pointer(p)).FnBuf
				} else {
					v7 = int32(1)
				}
				if 0 == _sessionBufferGrow(tls, pGrp+32+20, int64(v7), bp) {
					if (*TSessionBuffer)(unsafe.Pointer(p)).FnBuf != 0 {
						libc.Xmemcpy(tls, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.Frecord.FaBuf+uintptr((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.Frecord.FnBuf), (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf, uint32((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf))
						(*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.Frecord.FnBuf += (*TSessionBuffer)(unsafe.Pointer(p)).FnBuf
					} else {
						v2 = pGrp + 32 + 20 + 4
						v7 = *(*int32)(unsafe.Pointer(v2))
						*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
						**(**Tu8)(__ccgo_up((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.Frecord.FaBuf + uintptr(v7))) = eUndef
					}
				}
				goto _6
			_6:
				;
				ii = ii + 1
			}
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp)) = _sessionOneChangeToHash(tls, pGrp, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FeOp, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FbIndirect, (*TSessionTable)(unsafe.Pointer((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab)).FnCol, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.Frecord.FaBuf, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.Frecord.FnBuf, 0)
			}
		}
		/* Reset all aBuf[] entries to "undefined". */
		nZero = (*TSessionTable)(unsafe.Pointer((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab)).FnCol
		if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FeOp == int32(SQLITE_UPDATE) {
			nZero = nZero + nZero
		}
		ii = 0
		for {
			if !(ii < nZero) {
				break
			}
			(**(**TSessionBuffer)(__ccgo_up((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FaBuf + uintptr(ii)*12))).FnBuf = 0
			goto _10
		_10:
			;
			ii = ii + 1
		}
		(*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fcd.FpTab = uintptr(0)
	}
	return **(**int32)(__ccgo_up(bp))
}

/************** End of sqlite3session.c **************************************/
/************** Begin file fts5.c ********************************************/

/*
** This, the "fts5.c" source file, is a composite file that is itself
** assembled from the following files:
**
**    fts5.h
**    fts5Int.h
**    fts5parse.h          <--- Generated from fts5parse.y by Lemon
**    fts5parse.c          <--- Generated from fts5parse.y by Lemon
**    fts5_aux.c
**    fts5_buffer.c
**    fts5_config.c
**    fts5_expr.c
**    fts5_hash.c
**    fts5_index.c
**    fts5_main.c
**    fts5_storage.c
**    fts5_tokenize.c
**    fts5_unicode2.c
**    fts5_varint.c
**    fts5_vocab.c
 */

/*
** 2014 May 31
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
**
** Interfaces to extend FTS5. Using the interfaces defined in this file,
** FTS5 may be extended with:
**
**     * custom tokenizers, and
**     * custom auxiliary functions.
 */

/*
** 2014 May 31
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
**
 */

/* #include "fts5.h" */
/* #include "sqlite3ext.h" */

/* #include <string.h> */
/* #include <assert.h> */
/* #include <stddef.h> */

/*
** Constants for the largest and smallest possible 32-bit signed integers.
 */

/* Truncate very long tokens to this many bytes. Hard limit is
** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
** field that occurs at the start of each leaf page (see fts5_index.c). */

/*
** Maximum number of prefix indexes on single FTS5 table. This must be
** less than 32. If it is set to anything large than that, an #error
** directive in fts5_index.c will cause the build to fail.
 */

/*
** Maximum segments permitted in a single index
 */

/* Name of rank and rowid columns */

/*
** The assert_nc() macro is similar to the assert() macro, except that it
** is used for assert() conditions that are true only if it can be
** guranteed that the database is not corrupt.
 */

/*
** A version of memcmp() that does not cause asan errors if one of the pointer
** parameters is NULL and the number of bytes to compare is zero.
 */

/* Mark a function parameter as unused, to suppress nuisance compiler
** warnings. */

// C documentation
//
//	/*
//	** Configure the change currently under construction with a text value.
//	*/
func Xsqlite3changegroup_change_text(tls *libc.TLS, pGrp uintptr, bNew int32, iCol int32, pVal uintptr, nVal int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nByte Tsqlite3_int64
	var nText, rc, v2 int32
	var v1 uint32
	var _ /* pBuf at bp+0 */ uintptr
	_, _, _, _, _ = nByte, nText, rc, v1, v2
	if nVal >= 0 {
		v1 = uint32(nVal)
	} else {
		v1 = libc.Xstrlen(tls, pVal)
	}
	nText = int32(v1)
	nByte = int64(int32(1) + _sessionVarintLen(tls, nText) + nText)
	rc = SQLITE_OK
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	v2 = _checkChangeParams(tls, pGrp, bNew, iCol, nByte, bp)
	rc = v2
	if SQLITE_OK != v2 {
		return rc
	}
	**(**Tu8)(__ccgo_up((*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaBuf)) = uint8(SQLITE_TEXT)
	(*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnBuf = int32(1) + _sessionVarintPut(tls, (*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaBuf+1, nText)
	libc.Xmemcpy(tls, (*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnBuf), pVal, uint32(nText))
	**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + 4)) += nText
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Close the RBU handle.
//	*/
func Xsqlite3rbu_close(tls *libc.TLS, p uintptr, pzErrmsg uintptr) (r int32) {
	var pDb uintptr
	var rc, rc2 int32
	_, _, _ = pDb, rc, rc2
	if p != 0 {
		/* Commit the transaction to the *-oal file. */
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+17340, uintptr(0), uintptr(0), p+36)
		}
		/* Sync the db file if currently doing an incremental checkpoint */
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_CKPT) {
			pDb = (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpReal
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pDb)).FpMethods)).FxSync})))(tls, pDb, int32(SQLITE_SYNC_NORMAL))
		}
		_rbuSaveState(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage)
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+17340, uintptr(0), uintptr(0), p+36)
		}
		/* Close any open statement handles. */
		_rbuObjIterFinalize(tls, p+56)
		/* If this is an RBU vacuum handle and the vacuum has either finished
		 ** successfully or encountered an error, delete the contents of the
		 ** state table. This causes the next call to sqlite3rbu_vacuum()
		 ** specifying the current target and state databases to start a new
		 ** vacuum from scratch.  */
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu != 0 {
			rc2 = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+35921, uintptr(0), uintptr(0), uintptr(0))
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == int32(SQLITE_DONE) && rc2 != SQLITE_OK {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc2
			}
		}
		/* Close the open database handle and VFS object. */
		Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu)
		Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain)
		_rbuDeleteVfs(tls, p)
		Xsqlite3_free(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FaBuf)
		Xsqlite3_free(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FaFrame)
		_rbuEditErrmsg(tls, p)
		rc = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
		if pzErrmsg != 0 {
			**(**uintptr)(__ccgo_up(pzErrmsg)) = (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg
		} else {
			Xsqlite3_free(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg)
		}
		Xsqlite3_free(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState)
		Xsqlite3_free(tls, p)
	} else {
		rc = int32(SQLITE_NOMEM)
		**(**uintptr)(__ccgo_up(pzErrmsg)) = uintptr(0)
	}
	return rc
}

// C documentation
//
//	/*
//	** Create an RBU VFS named zName that accesses the underlying file-system
//	** via existing VFS zParent. The new object is registered as a non-default
//	** VFS with SQLite before returning.
//	*/
func Xsqlite3rbu_create_vfs(tls *libc.TLS, zName uintptr, zParent uintptr) (r int32) {
	var nByte, nName Tsize_t
	var pNew, pParent, zSpace, v1 uintptr
	var rc int32
	_, _, _, _, _, _, _ = nByte, nName, pNew, pParent, rc, zSpace, v1
	pNew = uintptr(0) /* Newly allocated VFS */
	rc = SQLITE_OK
	nName = libc.Xstrlen(tls, zName)
	nByte = uint32(108) + nName + uint32(1)
	pNew = Xsqlite3_malloc64(tls, uint64(nByte))
	if pNew == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	} else { /* Parent VFS */
		libc.Xmemset(tls, pNew, 0, nByte)
		pParent = Xsqlite3_vfs_find(tls, zParent)
		if pParent == uintptr(0) {
			rc = int32(SQLITE_NOTFOUND)
		} else {
			libc.Xmemcpy(tls, pNew, uintptr(unsafe.Pointer(&_vfs_template)), uint32(88))
			(*Trbu_vfs)(unsafe.Pointer(pNew)).Fbase.FmxPathname = (*Tsqlite3_vfs)(unsafe.Pointer(pParent)).FmxPathname
			(*Trbu_vfs)(unsafe.Pointer(pNew)).Fbase.FszOsFile = int32(uint32(64) + uint32((*Tsqlite3_vfs)(unsafe.Pointer(pParent)).FszOsFile))
			(*Trbu_vfs)(unsafe.Pointer(pNew)).FpRealVfs = pParent
			v1 = pNew + 1*108
			zSpace = v1
			(*Trbu_vfs)(unsafe.Pointer(pNew)).Fbase.FzName = v1
			libc.Xmemcpy(tls, zSpace, zName, nName)
			/* Allocate the mutex and register the new VFS (not as the default) */
			(*Trbu_vfs)(unsafe.Pointer(pNew)).Fmutex = Xsqlite3_mutex_alloc(tls, int32(SQLITE_MUTEX_RECURSIVE))
			if (*Trbu_vfs)(unsafe.Pointer(pNew)).Fmutex == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				rc = Xsqlite3_vfs_register(tls, pNew, 0)
			}
		}
		if rc != SQLITE_OK {
			Xsqlite3_mutex_free(tls, (*Trbu_vfs)(unsafe.Pointer(pNew)).Fmutex)
			Xsqlite3_free(tls, pNew)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Step the RBU object.
//	*/
func Xsqlite3rbu_step(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var iSector Tu32
	var pDb, pFrame, pIter uintptr
	var rc int32
	var _ /* ptr at bp+0 */ uintptr
	_, _, _, _, _ = iSector, pDb, pFrame, pIter, rc
	if p != 0 {
		switch (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage {
		case int32(RBU_STAGE_OAL):
			pIter = p + 56
			/* If this is an RBU vacuum operation and the state table was empty
			 ** when this handle was opened, create the target database schema. */
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) && (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress == 0 && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				_rbuCreateTargetSchema(tls, p)
				_rbuCopyPragma(tls, p, __ccgo_ts+20180)
				_rbuCopyPragma(tls, p, __ccgo_ts+19270)
			}
			for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl != 0 {
				if (*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup != 0 {
					/* Clean up the rbu_tmp_xxx table for the previous table. It
					 ** cannot be dropped as there are currently active SQL statements.
					 ** But the contents can be deleted.  */
					if libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed != 0 {
						_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+35514, libc.VaList(bp+16, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl))
					}
				} else {
					_rbuObjIterPrepareAll(tls, p, pIter, 0)
					/* Advance to the next row to process. */
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
						rc = Xsqlite3_step(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect)
						if rc == int32(SQLITE_ROW) {
							(*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress = (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress + 1
							(*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep + 1
							return _rbuStep(tls, p)
						}
						(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect)
						(*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = 0
					}
				}
				_rbuObjIterNext(tls, p, pIter)
			}
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				_rbuSaveState(tls, p, int32(RBU_STAGE_MOVE))
				_rbuIncrSchemaCookie(tls, p)
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+17340, uintptr(0), uintptr(0), p+36)
				}
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+17340, uintptr(0), uintptr(0), p+36)
				}
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_MOVE)
			}
		case int32(RBU_STAGE_MOVE):
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				_rbuMoveOalFile(tls, p)
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress = (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress + 1
			}
		case int32(RBU_STAGE_CKPT):
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep >= (*Tsqlite3rbu)(unsafe.Pointer(p)).FnFrame {
					pDb = (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpReal
					/* Sync the db file */
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pDb)).FpMethods)).FxSync})))(tls, pDb, int32(SQLITE_SYNC_NORMAL))
					/* Update nBackfill */
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
						(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, int32, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pDb)).FpMethods)).FxShmMap})))(tls, pDb, 0, libc.Int32FromInt32(32)*libc.Int32FromInt32(1024), 0, bp)
						if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
							**(**Tu32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + 24*4)) = (*Tsqlite3rbu)(unsafe.Pointer(p)).FiMaxFrame
						}
					}
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
						(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_DONE)
						(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_DONE)
					}
				} else {
					for cond := true; cond; cond = (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep < (*Tsqlite3rbu)(unsafe.Pointer(p)).FnFrame && iSector == ((**(**TRbuFrame)(__ccgo_up((*Tsqlite3rbu)(unsafe.Pointer(p)).FaFrame + uintptr((*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep)*8))).FiDbPage-uint32(1))/uint32((*Tsqlite3rbu)(unsafe.Pointer(p)).FnPagePerSector) && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
						pFrame = (*Tsqlite3rbu)(unsafe.Pointer(p)).FaFrame + uintptr((*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep)*8
						iSector = ((*TRbuFrame)(unsafe.Pointer(pFrame)).FiDbPage - uint32(1)) / uint32((*Tsqlite3rbu)(unsafe.Pointer(p)).FnPagePerSector)
						_rbuCheckpointFrame(tls, p, pFrame)
						(*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep + 1
					}
				}
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress = (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress + 1
			}
		default:
			break
		}
		return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
	} else {
		return int32(SQLITE_NOMEM)
	}
	return r
}

// C documentation
//
//	/*
//	** Open a handle to begin or resume an RBU VACUUM operation.
//	*/
func Xsqlite3rbu_vacuum(tls *libc.TLS, zTarget uintptr, zState uintptr) (r uintptr) {
	var n Tsize_t
	_ = n
	if zTarget == uintptr(0) {
		return _rbuMisuseError(tls)
	}
	if zState != 0 {
		n = libc.Xstrlen(tls, zState)
		if n >= uint32(7) && 0 == libc.Xmemcmp(tls, __ccgo_ts+35913, zState+uintptr(n-uint32(7)), uint32(7)) {
			return _rbuMisuseError(tls)
		}
	}
	/* TODO: Check that both arguments are non-NULL */
	return _openRbuHandle(tls, uintptr(0), zTarget, zState)
}

// C documentation
//
//	/*
//	** Attach a table to a session. All subsequent changes made to the table
//	** while the session object is enabled will be recorded.
//	**
//	** Only tables that have a PRIMARY KEY defined may be attached. It does
//	** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
//	** or not.
//	*/
func Xsqlite3session_attach(tls *libc.TLS, pSession uintptr, zName uintptr) (r int32) {
	var nByte, nName, rc int32
	var pTab, ppTab uintptr
	_, _, _, _, _ = nByte, nName, pTab, ppTab, rc
	rc = SQLITE_OK
	Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb))
	if !(zName != 0) {
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).FbAutoAttach = int32(1)
	} else { /* Number of bytes in string zName */
		/* First search for an existing entry. If one is found, this call is
		 ** a no-op. Return early. */
		nName = _sqlite3Strlen30(tls, zName)
		pTab = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable
		for {
			if !(pTab != 0) {
				break
			}
			if 0 == Xsqlite3_strnicmp(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zName, nName+int32(1)) {
				break
			}
			goto _1
		_1:
			;
			pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext
		}
		if !(pTab != 0) {
			/* Allocate new SessionTable object. */
			nByte = int32(uint32(56) + uint32(nName) + uint32(1))
			pTab = _sessionMalloc64(tls, pSession, int64(nByte))
			if !(pTab != 0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				libc.Xmemset(tls, pTab, 0, uint32(56))
				(*TSessionTable)(unsafe.Pointer(pTab)).FzName = pTab + 1*56
				libc.Xmemcpy(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zName, uint32(nName+int32(1)))
				ppTab = pSession + 64
				for {
					if !(**(**uintptr)(__ccgo_up(ppTab)) != 0) {
						break
					}
					goto _2
				_2:
					;
					ppTab = **(**uintptr)(__ccgo_up(ppTab))
				}
				**(**uintptr)(__ccgo_up(ppTab)) = pTab
			}
		}
	}
	Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb))
	return rc
}

// C documentation
//
//	/*
//	** Create a session object. This session object will record changes to
//	** database zDb attached to connection db.
//	*/
func Xsqlite3session_create(tls *libc.TLS, db uintptr, zDb uintptr, ppSession uintptr) (r int32) {
	var nDb int32
	var pNew, pOld uintptr
	_, _, _ = nDb, pNew, pOld        /* Session object already attached to db */
	nDb = _sqlite3Strlen30(tls, zDb) /* Length of zDb in bytes */
	/* Zero the output value in case an error occurs. */
	**(**uintptr)(__ccgo_up(ppSession)) = uintptr(0)
	/* Allocate and populate the new session object. */
	pNew = Xsqlite3_malloc64(tls, uint64(uint32(88)+uint32(nDb)+uint32(1)))
	if !(pNew != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pNew, 0, uint32(88))
	(*Tsqlite3_session)(unsafe.Pointer(pNew)).Fdb = db
	(*Tsqlite3_session)(unsafe.Pointer(pNew)).FzDb = pNew + 1*88
	(*Tsqlite3_session)(unsafe.Pointer(pNew)).FbEnable = int32(1)
	libc.Xmemcpy(tls, (*Tsqlite3_session)(unsafe.Pointer(pNew)).FzDb, zDb, uint32(nDb+int32(1)))
	_sessionPreupdateHooks(tls, pNew)
	/* Add the new session object to the linked list of session objects
	 ** attached to database handle $db. Do this under the cover of the db
	 ** handle mutex.  */
	Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, db))
	pOld = Xsqlite3_preupdate_hook(tls, db, __ccgo_fp(_xPreUpdate), pNew)
	(*Tsqlite3_session)(unsafe.Pointer(pNew)).FpNext = pOld
	Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, db))
	**(**uintptr)(__ccgo_up(ppSession)) = pNew
	return SQLITE_OK
}

func Xsqlite3session_diff(tls *libc.TLS, pSession uintptr, zFrom uintptr, zTbl uintptr, pzErrMsg uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var bHasPk, bMismatch, i, rc int32
	var db, zDb, zDbExists, zExpr, v1 uintptr
	var _ /* abPK at bp+24 */ uintptr
	var _ /* azCol at bp+28 */ uintptr
	var _ /* bRowid at bp+20 */ int32
	var _ /* d at bp+0 */ TSessionDiffCtx
	var _ /* nCol at bp+16 */ int32
	var _ /* pDbExists at bp+32 */ uintptr
	var _ /* pTo at bp+12 */ uintptr
	_, _, _, _, _, _, _, _, _ = bHasPk, bMismatch, db, i, rc, zDb, zDbExists, zExpr, v1
	zDb = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb
	rc = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc
	libc.Xmemset(tls, bp, 0, uint32(12))
	_sessionDiffHooks(tls, pSession, bp)
	Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb))
	if pzErrMsg != 0 {
		**(**uintptr)(__ccgo_up(pzErrMsg)) = uintptr(0)
	}
	if rc == SQLITE_OK {
		zExpr = uintptr(0)
		db = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb /* Table zTbl */
		/* Locate and if necessary initialize the target table object */
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).FbAutoAttach = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbAutoAttach + 1
		rc = _sessionFindTable(tls, pSession, zTbl, bp+12)
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).FbAutoAttach = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbAutoAttach - 1
		if **(**uintptr)(__ccgo_up(bp + 12)) == uintptr(0) {
			goto diff_out
		}
		if _sessionInitTable(tls, pSession, **(**uintptr)(__ccgo_up(bp + 12)), (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb) != 0 {
			rc = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc
			goto diff_out
		}
		/* Check the table schemas match */
		if rc == SQLITE_OK {
			bHasPk = 0
			bMismatch = 0
			**(**int32)(__ccgo_up(bp + 16)) = 0 /* Columns in zFrom.zTbl */
			**(**int32)(__ccgo_up(bp + 20)) = 0
			**(**uintptr)(__ccgo_up(bp + 24)) = uintptr(0)
			**(**uintptr)(__ccgo_up(bp + 28)) = uintptr(0)
			zDbExists = uintptr(0)
			/* Check that database zFrom is attached.  */
			zDbExists = Xsqlite3_mprintf(tls, __ccgo_ts+36844, libc.VaList(bp+48, zFrom))
			if zDbExists == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				**(**uintptr)(__ccgo_up(bp + 32)) = uintptr(0)
				rc = Xsqlite3_prepare_v2(tls, db, zDbExists, -int32(1), bp+32, uintptr(0))
				if rc == int32(SQLITE_ERROR) {
					rc = SQLITE_OK
					**(**int32)(__ccgo_up(bp + 16)) = -int32(1)
				}
				Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 32)))
				Xsqlite3_free(tls, zDbExists)
			}
			if rc == SQLITE_OK && **(**int32)(__ccgo_up(bp + 16)) == 0 {
				if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK != 0 {
					v1 = bp + 20
				} else {
					v1 = uintptr(0)
				}
				rc = _sessionTableInfo(tls, uintptr(0), db, zFrom, zTbl, bp+16, uintptr(0), uintptr(0), bp+28, uintptr(0), uintptr(0), bp+24, v1)
			}
			if rc == SQLITE_OK {
				if (*TSessionTable)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FnCol != **(**int32)(__ccgo_up(bp + 16)) {
					if **(**int32)(__ccgo_up(bp + 16)) <= 0 {
						rc = int32(SQLITE_SCHEMA)
						if pzErrMsg != 0 {
							**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+36875, libc.VaList(bp+48, zFrom, zTbl))
						}
					} else {
						bMismatch = int32(1)
					}
				} else {
					i = 0
					for {
						if !(i < **(**int32)(__ccgo_up(bp + 16))) {
							break
						}
						if int32(**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FabPK + uintptr(i)))) != int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 24)) + uintptr(i)))) {
							bMismatch = int32(1)
						}
						if Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 28)) + uintptr(i)*4)), **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FazCol + uintptr(i)*4))) != 0 {
							bMismatch = int32(1)
						}
						if **(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 24)) + uintptr(i))) != 0 {
							bHasPk = int32(1)
						}
						goto _2
					_2:
						;
						i = i + 1
					}
				}
			}
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 28)))
			if bMismatch != 0 {
				if pzErrMsg != 0 {
					**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+36896, 0)
				}
				rc = int32(SQLITE_SCHEMA)
			}
			if bHasPk == 0 {
				/* Ignore tables with no primary keys */
				goto diff_out
			}
		}
		if rc == SQLITE_OK {
			zExpr = _sessionExprComparePK(tls, (*TSessionTable)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FnCol, zDb, zFrom, (*TSessionTable)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FzName, (*TSessionTable)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FazCol, (*TSessionTable)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FabPK)
		}
		/* Find new rows */
		if rc == SQLITE_OK {
			rc = _sessionDiffFindNew(tls, int32(SQLITE_INSERT), pSession, **(**uintptr)(__ccgo_up(bp + 12)), zDb, zFrom, zExpr)
		}
		/* Find old rows */
		if rc == SQLITE_OK {
			rc = _sessionDiffFindNew(tls, int32(SQLITE_DELETE), pSession, **(**uintptr)(__ccgo_up(bp + 12)), zFrom, zDb, zExpr)
		}
		/* Find modified rows */
		if rc == SQLITE_OK {
			rc = _sessionDiffFindModified(tls, pSession, **(**uintptr)(__ccgo_up(bp + 12)), zFrom, zExpr)
		}
		Xsqlite3_free(tls, zExpr)
	}
	goto diff_out
diff_out:
	;
	_sessionPreupdateHooks(tls, pSession)
	Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb))
	return rc
}

const _ALLOCA_S_MARKER_SIZE = 8

type _FLOATING_SAVE_AREA = T_FLOATING_SAVE_AREA

const _HEAP_MAXREQ = 4294967264

const _M_IX86 = 600

// C documentation
//
//	/*
//	** Arguments aIdx, aCell and aSpare all point to arrays of size
//	** nIdx. The aIdx array contains the set of integers from 0 to
//	** (nIdx-1) in no particular order. This function sorts the values
//	** in aIdx according to dimension iDim of the cells in aCell. The
//	** minimum value of dimension iDim is considered first, the
//	** maximum used to break ties.
//	**
//	** The aSpare array is used as temporary working space by the
//	** sorting algorithm.
//	*/
func _SortByDimension(tls *libc.TLS, pRtree uintptr, aIdx uintptr, nIdx int32, iDim int32, aCell uintptr, aSpare uintptr) {
	var aLeft, aRight uintptr
	var iLeft, iRight, nLeft, nRight int32
	var xleft1, xleft2, xright1, xright2 TRtreeDValue
	var v1, v2, v3, v4 float64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = aLeft, aRight, iLeft, iRight, nLeft, nRight, xleft1, xleft2, xright1, xright2, v1, v2, v3, v4
	if nIdx > int32(1) {
		iLeft = 0
		iRight = 0
		nLeft = nIdx / int32(2)
		nRight = nIdx - nLeft
		aLeft = aIdx
		aRight = aIdx + uintptr(nLeft)*4
		_SortByDimension(tls, pRtree, aLeft, nLeft, iDim, aCell, aSpare)
		_SortByDimension(tls, pRtree, aRight, nRight, iDim, aCell, aSpare)
		libc.Xmemcpy(tls, aSpare, aLeft, uint32(4)*uint32(nLeft))
		aLeft = aSpare
		for iLeft < nLeft || iRight < nRight {
			if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
				v1 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(**(**int32)(__ccgo_up(aLeft + uintptr(iLeft)*4)))*48 + 8 + uintptr(iDim*int32(2))*4)))
			} else {
				v1 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(**(**int32)(__ccgo_up(aLeft + uintptr(iLeft)*4)))*48 + 8 + uintptr(iDim*int32(2))*4)))
			}
			xleft1 = v1
			if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
				v2 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(**(**int32)(__ccgo_up(aLeft + uintptr(iLeft)*4)))*48 + 8 + uintptr(iDim*int32(2)+int32(1))*4)))
			} else {
				v2 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(**(**int32)(__ccgo_up(aLeft + uintptr(iLeft)*4)))*48 + 8 + uintptr(iDim*int32(2)+int32(1))*4)))
			}
			xleft2 = v2
			if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
				v3 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(**(**int32)(__ccgo_up(aRight + uintptr(iRight)*4)))*48 + 8 + uintptr(iDim*int32(2))*4)))
			} else {
				v3 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(**(**int32)(__ccgo_up(aRight + uintptr(iRight)*4)))*48 + 8 + uintptr(iDim*int32(2))*4)))
			}
			xright1 = v3
			if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
				v4 = float64(*(*TRtreeValue)(unsafe.Pointer(aCell + uintptr(**(**int32)(__ccgo_up(aRight + uintptr(iRight)*4)))*48 + 8 + uintptr(iDim*int32(2)+int32(1))*4)))
			} else {
				v4 = float64(*(*int32)(unsafe.Pointer(aCell + uintptr(**(**int32)(__ccgo_up(aRight + uintptr(iRight)*4)))*48 + 8 + uintptr(iDim*int32(2)+int32(1))*4)))
			}
			xright2 = v4
			if iLeft != nLeft && (iRight == nRight || xleft1 < xright1 || xleft1 == xright1 && xleft2 < xright2) {
				**(**int32)(__ccgo_up(aIdx + uintptr(iLeft+iRight)*4)) = **(**int32)(__ccgo_up(aLeft + uintptr(iLeft)*4))
				iLeft = iLeft + 1
			} else {
				**(**int32)(__ccgo_up(aIdx + uintptr(iLeft+iRight)*4)) = **(**int32)(__ccgo_up(aRight + uintptr(iRight)*4))
				iRight = iRight + 1
			}
		}
	}
}

func _SplitNode(tls *libc.TLS, pRtree uintptr, pNode uintptr, pCell uintptr, iHeight int32) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var aCell, aiUsed, pLeft, pParent, pRight uintptr
	var i, nCell, newCellIsRight, rc, v2, v3 int32
	var iRowid, iRowid1 Ti64
	var v4, v5 bool
	var _ /* iCell at bp+96 */ int32
	var _ /* leftbbox at bp+0 */ TRtreeCell
	var _ /* rightbbox at bp+48 */ TRtreeCell
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aCell, aiUsed, i, iRowid, iRowid1, nCell, newCellIsRight, pLeft, pParent, pRight, rc, v2, v3, v4, v5
	newCellIsRight = 0
	rc = SQLITE_OK
	nCell = _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2)
	pLeft = uintptr(0)
	pRight = uintptr(0)
	/* Allocate an array and populate it with a copy of pCell and
	 ** all cells from node pLeft. Then zero the original node.
	 */
	aCell = Xsqlite3_malloc64(tls, uint64((libc.Uint32FromInt64(48)+libc.Uint32FromInt64(4))*uint32(nCell+libc.Int32FromInt32(1))))
	if !(aCell != 0) {
		rc = int32(SQLITE_NOMEM)
		goto splitnode_out
	}
	aiUsed = aCell + uintptr(nCell+int32(1))*48
	libc.Xmemset(tls, aiUsed, 0, uint32(4)*uint32(nCell+libc.Int32FromInt32(1)))
	i = 0
	for {
		if !(i < nCell) {
			break
		}
		_nodeGetCell(tls, pRtree, pNode, i, aCell+uintptr(i)*48)
		goto _1
	_1:
		;
		i = i + 1
	}
	_nodeZero(tls, pRtree, pNode)
	libc.Xmemcpy(tls, aCell+uintptr(nCell)*48, pCell, uint32(48))
	nCell = nCell + 1
	if (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode == int64(1) {
		pRight = _nodeNew(tls, pRtree, pNode)
		pLeft = _nodeNew(tls, pRtree, pNode)
		(*TRtree)(unsafe.Pointer(pRtree)).FiDepth = (*TRtree)(unsafe.Pointer(pRtree)).FiDepth + 1
		(*TRtreeNode)(unsafe.Pointer(pNode)).FisDirty = int32(1)
		_writeInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData, (*TRtree)(unsafe.Pointer(pRtree)).FiDepth)
	} else {
		pLeft = pNode
		pRight = _nodeNew(tls, pRtree, (*TRtreeNode)(unsafe.Pointer(pLeft)).FpParent)
		(*TRtreeNode)(unsafe.Pointer(pLeft)).FnRef = (*TRtreeNode)(unsafe.Pointer(pLeft)).FnRef + 1
	}
	if !(pLeft != 0) || !(pRight != 0) {
		rc = int32(SQLITE_NOMEM)
		goto splitnode_out
	}
	libc.Xmemset(tls, (*TRtreeNode)(unsafe.Pointer(pLeft)).FzData, 0, uint32((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize))
	libc.Xmemset(tls, (*TRtreeNode)(unsafe.Pointer(pRight)).FzData, 0, uint32((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize))
	rc = _splitNodeStartree(tls, pRtree, aCell, nCell, pLeft, pRight, bp, bp+48)
	if rc != SQLITE_OK {
		goto splitnode_out
	}
	/* Ensure both child nodes have node numbers assigned to them by calling
	 ** nodeWrite(). Node pRight always needs a node number, as it was created
	 ** by nodeNew() above. But node pLeft sometimes already has a node number.
	 ** In this case avoid the all to nodeWrite().
	 */
	v2 = _nodeWrite(tls, pRtree, pRight)
	rc = v2
	if v5 = SQLITE_OK != v2; !v5 {
		if v4 = 0 == (*TRtreeNode)(unsafe.Pointer(pLeft)).FiNode; v4 {
			v3 = _nodeWrite(tls, pRtree, pLeft)
			rc = v3
		}
	}
	if v5 || v4 && SQLITE_OK != v3 {
		goto splitnode_out
	}
	(**(**TRtreeCell)(__ccgo_up(bp + 48))).FiRowid = (*TRtreeNode)(unsafe.Pointer(pRight)).FiNode
	(**(**TRtreeCell)(__ccgo_up(bp))).FiRowid = (*TRtreeNode)(unsafe.Pointer(pLeft)).FiNode
	if (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode == int64(1) {
		rc = _rtreeInsertCell(tls, pRtree, (*TRtreeNode)(unsafe.Pointer(pLeft)).FpParent, bp, iHeight+int32(1))
		if rc != SQLITE_OK {
			goto splitnode_out
		}
	} else {
		pParent = (*TRtreeNode)(unsafe.Pointer(pLeft)).FpParent
		rc = _nodeParentIndex(tls, pRtree, pLeft, bp+96)
		if rc == SQLITE_OK {
			_nodeOverwriteCell(tls, pRtree, pParent, bp, **(**int32)(__ccgo_up(bp + 96)))
			rc = _AdjustTree(tls, pRtree, pParent, bp)
		}
		if rc != SQLITE_OK {
			goto splitnode_out
		}
	}
	v2 = _rtreeInsertCell(tls, pRtree, (*TRtreeNode)(unsafe.Pointer(pRight)).FpParent, bp+48, iHeight+int32(1))
	rc = v2
	if v2 != 0 {
		goto splitnode_out
	}
	i = 0
	for {
		if !(i < _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pRight)).FzData+2)) {
			break
		}
		iRowid = _nodeGetRowid(tls, pRtree, pRight, i)
		rc = _updateMapping(tls, pRtree, iRowid, pRight, iHeight)
		if iRowid == (*TRtreeCell)(unsafe.Pointer(pCell)).FiRowid {
			newCellIsRight = int32(1)
		}
		if rc != SQLITE_OK {
			goto splitnode_out
		}
		goto _7
	_7:
		;
		i = i + 1
	}
	if (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode == int64(1) {
		i = 0
		for {
			if !(i < _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pLeft)).FzData+2)) {
				break
			}
			iRowid1 = _nodeGetRowid(tls, pRtree, pLeft, i)
			rc = _updateMapping(tls, pRtree, iRowid1, pLeft, iHeight)
			if rc != SQLITE_OK {
				goto splitnode_out
			}
			goto _8
		_8:
			;
			i = i + 1
		}
	} else {
		if newCellIsRight == 0 {
			rc = _updateMapping(tls, pRtree, (*TRtreeCell)(unsafe.Pointer(pCell)).FiRowid, pLeft, iHeight)
		}
	}
	goto splitnode_out
splitnode_out:
	;
	_nodeRelease(tls, pRtree, pRight)
	_nodeRelease(tls, pRtree, pLeft)
	Xsqlite3_free(tls, aCell)
	return rc
}

const _X86_ = 1

const __RPC_API = "__stdcall"

const __RPC_CALLEE = "__stdcall"

const __RPC_STUB = "__RPC_API"

const __RPC_USER = "__RPC_API"

const __int3264 = 0

/*
 ** The following array holds FuncDef structures for all of the functions
 ** defined in this file.
 **
 ** The array cannot be constant since changes are made to the
 ** FuncDef.pHash elements at start-time.  The elements of this array
 ** are read-only after initialization is complete.
 **
 ** For peak efficiency, put the most frequently used function last.
 */
var _aBuiltinFunc = [106]TFuncDef{
	0: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_FUNC_TEST) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_implies_nonnull_row)),
		FzName:     __ccgo_ts + 17832,
	},
	1: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_FUNC_TEST) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_expr_compare)),
		FzName:     __ccgo_ts + 17852,
	},
	2: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_FUNC_TEST) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_expr_implies_expr)),
		FzName:     __ccgo_ts + 17865,
	},
	3: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INTERNAL) | libc.Int32FromInt32(SQLITE_FUNC_TEST) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_affinity)),
		FzName:     __ccgo_ts + 17883,
	},
	4: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 17892,
	},
	5: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DIRECTONLY) | libc.Int32FromInt32(SQLITE_FUNC_UNSAFE)),
		FzName:     __ccgo_ts + 17900,
	},
	6: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DIRECTONLY) | libc.Int32FromInt32(SQLITE_FUNC_UNSAFE)),
		FzName:     __ccgo_ts + 17900,
	},
	7: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 17915,
	},
	8: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 17941,
	},
	9: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_FUNC_UNLIKELY)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_unlikely)),
		FzName:     __ccgo_ts + 17966,
	},
	10: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_FUNC_UNLIKELY)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_unlikely)),
		FzName:     __ccgo_ts + 17975,
	},
	11: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_FUNC_UNLIKELY)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_unlikely)),
		FzName:     __ccgo_ts + 17986,
	},
	12: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_sqlite_offset)),
		FzName:     __ccgo_ts + 17993,
	},
	13: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(1)),
		FzName:     __ccgo_ts + 18007,
	},
	14: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(1)),
		FzName:     __ccgo_ts + 18007,
	},
	15: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(2)),
		FzName:     __ccgo_ts + 18013,
	},
	16: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(2)),
		FzName:     __ccgo_ts + 18013,
	},
	17: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(3)),
		FzName:     __ccgo_ts + 18019,
	},
	18: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(3)),
		FzName:     __ccgo_ts + 18019,
	},
	19: {
		FnArg:      int16(-int32(3)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18024,
	},
	20: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_MINMAX) | libc.Int32FromInt32(SQLITE_FUNC_ANYORDER)),
		FzName:     __ccgo_ts + 18024,
	},
	21: {
		FnArg:      int16(-int32(3)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(1)),
		FzName:     __ccgo_ts + 18028,
	},
	22: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_MINMAX) | libc.Int32FromInt32(SQLITE_FUNC_ANYORDER)),
		FpUserData: uintptr(libc.Int32FromInt32(1)),
		FzName:     __ccgo_ts + 18028,
	},
	23: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_TYPEOF)),
		FzName:     __ccgo_ts + 18032,
	},
	24: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_TYPEOF) | libc.Int32FromInt32(SQLITE_SUBTYPE)),
		FzName:     __ccgo_ts + 18039,
	},
	25: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_LENGTH)),
		FzName:     __ccgo_ts + 18047,
	},
	26: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_BYTELEN)),
		FzName:     __ccgo_ts + 18054,
	},
	27: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18067,
	},
	28: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18073,
	},
	29: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18080,
	},
	30: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18087,
	},
	31: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18095,
	},
	32: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18100,
	},
	33: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18104,
	},
	34: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18104,
	},
	35: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18110,
	},
	36: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18116,
	},
	37: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18122,
	},
	38: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18126,
	},
	39: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18126,
	},
	40: {
		FnArg:      int16(-int32(3)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18132,
	},
	41: {
		FnArg:      int16(-int32(4)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18139,
	},
	42: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)),
		FzName:     __ccgo_ts + 18149,
	},
	43: {
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18156,
	},
	44: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18163,
	},
	45: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18174,
	},
	46: {
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18181,
	},
	47: {
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18196,
	},
	48: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18213,
	},
	49: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18224,
	},
	50: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18231,
	},
	51: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(1)),
		FzName:     __ccgo_ts + 18237,
	},
	52: {
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18250,
	},
	53: {
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18268,
	},
	54: {
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18276,
	},
	55: {
		FnArg:      int16(3),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18290,
	},
	56: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18298,
	},
	57: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18307,
	},
	58: {
		FnArg:      int16(3),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18307,
	},
	59: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18314,
	},
	60: {
		FnArg:      int16(3),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18314,
	},
	61: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)),
		FzName:     __ccgo_ts + 18324,
	},
	62: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)),
		FzName:     __ccgo_ts + 18328,
	},
	63: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)),
		FzName:     __ccgo_ts + 18334,
	},
	64: {
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_COUNT) | libc.Int32FromInt32(SQLITE_FUNC_ANYORDER)),
		FzName:     __ccgo_ts + 18338,
	},
	65: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_FUNC_ANYORDER)),
		FzName:     __ccgo_ts + 18338,
	},
	66: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)),
		FzName:     __ccgo_ts + 18344,
	},
	67: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)),
		FzName:     __ccgo_ts + 18344,
	},
	68: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(0)),
		FzName:     __ccgo_ts + 18357,
	},
	69: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_LIKE) | libc.Int32FromInt32(SQLITE_FUNC_CASE)),
		FpUserData: uintptr(unsafe.Pointer(&_globInfo)),
		FzName:     __ccgo_ts + 18368,
	},
	70: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_LIKE)),
		FpUserData: uintptr(unsafe.Pointer(&_likeInfoNorm)),
		FzName:     __ccgo_ts + 17827,
	},
	71: {
		FnArg:      int16(3),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_LIKE)),
		FpUserData: uintptr(unsafe.Pointer(&_likeInfoNorm)),
		FzName:     __ccgo_ts + 17827,
	},
	72: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18373,
	},
	73: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 1284,
	},
	74: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 1292,
	},
	75: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18378,
	},
	76: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18384,
	},
	77: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(1)),
		FzName:     __ccgo_ts + 18387,
	},
	78: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(1)),
		FzName:     __ccgo_ts + 18391,
	},
	79: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FpUserData: uintptr(libc.Int32FromInt32(2)),
		FzName:     __ccgo_ts + 18397,
	},
	80: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18387,
	},
	81: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18402,
	},
	82: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18406,
	},
	83: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18410,
	},
	84: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18416,
	},
	85: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18420,
	},
	86: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18425,
	},
	87: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18430,
	},
	88: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18435,
	},
	89: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18441,
	},
	90: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18445,
	},
	91: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18449,
	},
	92: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18453,
	},
	93: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18458,
	},
	94: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18463,
	},
	95: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18468,
	},
	96: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18474,
	},
	97: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18480,
	},
	98: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18486,
	},
	99: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18491,
	},
	100: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18499,
	},
	101: {
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)),
		FzName:     __ccgo_ts + 18507,
	},
	102: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)),
		FzName:     __ccgo_ts + 18510,
	},
	103: {
		FnArg:      int16(-int32(4)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)),
		FzName:     __ccgo_ts + 8178,
	},
	104: {
		FnArg:      int16(-int32(4)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_iif)),
		FzName:     __ccgo_ts + 18515,
	},
	105: {
		FnArg:      int16(-int32(4)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_FUNC_INLINE) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(0)),
		FpUserData: uintptr(libc.Int32FromInt32(INLINEFUNC_iif)),
		FzName:     __ccgo_ts + 18519,
	},
}

var _aFlagOp = [21]struct {
	F__ccgo_align  [0]uint32
	Fop            int32
	F__ccgo_align1 [4]byte
	Fmask          Tu64
}{
	0: {
		Fop:   int32(SQLITE_DBCONFIG_ENABLE_FKEY),
		Fmask: uint64(SQLITE_ForeignKeys),
	},
	1: {
		Fop:   int32(SQLITE_DBCONFIG_ENABLE_TRIGGER),
		Fmask: uint64(SQLITE_EnableTrigger),
	},
	2: {
		Fop:   int32(SQLITE_DBCONFIG_ENABLE_VIEW),
		Fmask: uint64(SQLITE_EnableView),
	},
	3: {
		Fop:   int32(SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER),
		Fmask: uint64(SQLITE_Fts3Tokenizer),
	},
	4: {
		Fop:   int32(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION),
		Fmask: uint64(SQLITE_LoadExtension),
	},
	5: {
		Fop:   int32(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE),
		Fmask: uint64(SQLITE_NoCkptOnClose),
	},
	6: {
		Fop:   int32(SQLITE_DBCONFIG_ENABLE_QPSG),
		Fmask: uint64(SQLITE_EnableQPSG),
	},
	7: {
		Fop:   int32(SQLITE_DBCONFIG_TRIGGER_EQP),
		Fmask: uint64(SQLITE_TriggerEQP),
	},
	8: {
		Fop:   int32(SQLITE_DBCONFIG_RESET_DATABASE),
		Fmask: uint64(SQLITE_ResetDatabase),
	},
	9: {
		Fop:   int32(SQLITE_DBCONFIG_DEFENSIVE),
		Fmask: uint64(SQLITE_Defensive),
	},
	10: {
		Fop:   int32(SQLITE_DBCONFIG_WRITABLE_SCHEMA),
		Fmask: uint64(libc.Int32FromInt32(SQLITE_WriteSchema) | libc.Int32FromInt32(SQLITE_NoSchemaError)),
	},
	11: {
		Fop:   int32(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE),
		Fmask: uint64(SQLITE_LegacyAlter),
	},
	12: {
		Fop:   int32(SQLITE_DBCONFIG_DQS_DDL),
		Fmask: uint64(SQLITE_DqsDDL),
	},
	13: {
		Fop:   int32(SQLITE_DBCONFIG_DQS_DML),
		Fmask: uint64(SQLITE_DqsDML),
	},
	14: {
		Fop:   int32(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT),
		Fmask: uint64(SQLITE_LegacyFileFmt),
	},
	15: {
		Fop:   int32(SQLITE_DBCONFIG_TRUSTED_SCHEMA),
		Fmask: uint64(SQLITE_TrustedSchema),
	},
	16: {
		Fop:   int32(SQLITE_DBCONFIG_STMT_SCANSTATUS),
		Fmask: uint64(SQLITE_StmtScanStatus),
	},
	17: {
		Fop:   int32(SQLITE_DBCONFIG_REVERSE_SCANORDER),
		Fmask: uint64(SQLITE_ReverseOrder),
	},
	18: {
		Fop:   int32(SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE),
		Fmask: uint64(libc.Int32FromInt32(0x00010)) << libc.Int32FromInt32(32),
	},
	19: {
		Fop:   int32(SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE),
		Fmask: uint64(libc.Int32FromInt32(0x00020)) << libc.Int32FromInt32(32),
	},
	20: {
		Fop:   int32(SQLITE_DBCONFIG_ENABLE_COMMENTS),
		Fmask: uint64(libc.Int32FromInt32(0x00040)) << libc.Int32FromInt32(32),
	},
}

var _aJsonFunc = [36]TFuncDef{
	0: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 14911,
	},
	1: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28205,
	},
	2: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28211,
	},
	3: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28222,
	},
	4: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(JSON_AINS) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28234,
	},
	5: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(JSON_AINS) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28252,
	},
	6: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28271,
	},
	7: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28271,
	},
	8: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28289,
	},
	9: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28309,
	},
	10: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28322,
	},
	11: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(JSON_JSON) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28336,
	},
	12: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(JSON_SQL) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28339,
	},
	13: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28343,
	},
	14: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28355,
	},
	15: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28368,
	},
	16: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28380,
	},
	17: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28393,
	},
	18: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28404,
	},
	19: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28416,
	},
	20: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28416,
	},
	21: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28428,
	},
	22: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28439,
	},
	23: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28451,
	},
	24: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28464,
	},
	25: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28477,
	},
	26: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(JSON_ISSET) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28491,
	},
	27: {
		FnArg:      int16(-int32(1)),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FpUserData: uintptr(libc.Int32FromInt32(JSON_ISSET) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28500,
	},
	28: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28510,
	},
	29: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28510,
	},
	30: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28520,
	},
	31: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)),
		FzName:     __ccgo_ts + 28520,
	},
	32: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC)),
		FzName:     __ccgo_ts + 28531,
	},
	33: {
		FnArg:      int16(1),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC)),
		FpUserData: uintptr(libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28548,
	},
	34: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC)),
		FzName:     __ccgo_ts + 28566,
	},
	35: {
		FnArg:      int16(2),
		FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC)),
		FpUserData: uintptr(libc.Int32FromInt32(JSON_BLOB)),
		FzName:     __ccgo_ts + 28584,
	},
}

// C documentation
//
//	/*
//	** This function is used to read or overwrite payload information
//	** for the entry that the pCur cursor is pointing to. The eOp
//	** argument is interpreted as follows:
//	**
//	**   0: The operation is a read. Populate the overflow cache.
//	**   1: The operation is a write. Populate the overflow cache.
//	**
//	** A total of "amt" bytes are read or written beginning at "offset".
//	** Data is read to or from the buffer pBuf.
//	**
//	** The content being read or written might appear on the main page
//	** or be scattered out on multiple overflow pages.
//	**
//	** If the current cursor entry uses one or more overflow pages
//	** this function may allocate space for and lazily populate
//	** the overflow page-list cache array (BtCursor.aOverflow).
//	** Subsequent calls use this cache to make seeking to the supplied offset
//	** more efficient.
//	**
//	** Once an overflow page-list cache has been allocated, it must be
//	** invalidated if some other cursor writes to the same table, or if
//	** the cursor is moved to a different row. Additionally, in auto-vacuum
//	** mode, the following events may invalidate an overflow page-list cache.
//	**
//	**   * An incremental vacuum,
//	**   * A commit in auto_vacuum="full" mode,
//	**   * Creating a table (may require moving an overflow page).
//	*/
func _accessPayload(tls *libc.TLS, pCur uintptr, offset Tu32, amt Tu32, pBuf uintptr, eOp int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a, a1, iIdx, rc, v2 int32
	var aNew, aPayload, aWrite, fd, pBt, pBufStart, pPage, v1 uintptr
	var nOvfl Ti64
	var ovflSize Tu32
	var _ /* aSave at bp+4 */ [4]Tu8
	var _ /* nextPage at bp+0 */ TPgno
	var _ /* pDbPage at bp+8 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a, a1, aNew, aPayload, aWrite, fd, iIdx, nOvfl, ovflSize, pBt, pBufStart, pPage, rc, v1, v2
	rc = SQLITE_OK
	iIdx = 0
	pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage /* Btree page of current entry */
	pBt = (*TBtCursor)(unsafe.Pointer(pCur)).FpBt     /* Btree this cursor belongs to */
	pBufStart = pBuf                                  /* Start of original out buffer */
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
		return _sqlite3CorruptError(tls, int32(78371))
	}
	_getCellInfo(tls, pCur)
	aPayload = (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload
	if uint32(int32(aPayload)-int32((*TMemPage)(unsafe.Pointer(pPage)).FaData)) > (*TBtShared)(unsafe.Pointer(pBt)).FusableSize-uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) {
		/* Trying to read or write past the end of the data is an error.  The
		 ** conditional above is really:
		 **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
		 ** but is recast into its current form to avoid integer overflow problems
		 */
		return _sqlite3CorruptError(tls, int32(78386))
	}
	/* Check if data must be read/written to/from the btree page itself. */
	if offset < uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) {
		a = int32(amt)
		if uint32(a)+offset > uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) {
			a = int32(uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) - offset)
		}
		rc = _copyPayload(tls, aPayload+uintptr(offset), pBuf, a, eOp, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage)
		offset = uint32(0)
		pBuf = pBuf + uintptr(a)
		amt = amt - uint32(a)
	} else {
		offset = offset - uint32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal)
	}
	if rc == SQLITE_OK && amt > uint32(0) {
		ovflSize = (*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(4)
		**(**TPgno)(__ccgo_up(bp)) = _sqlite3Get4byte(tls, aPayload+uintptr((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal))
		/* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
		 **
		 ** The aOverflow[] array is sized at one entry for each overflow page
		 ** in the overflow chain. The page number of the first overflow page is
		 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
		 ** means "not yet known" (the cache is lazily populated).
		 */
		if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_ValidOvfl) == 0 {
			nOvfl = int64((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnPayload)
			nOvfl = (nOvfl - int64((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) + int64(ovflSize) - int64(1)) / int64(ovflSize)
			if (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow == uintptr(0) || nOvfl*int64(libc.Int32FromInt64(4)) > int64(_sqlite3MallocSize(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow)) {
				if _sqlite3FaultSim(tls, int32(413)) != 0 {
					aNew = uintptr(0)
				} else {
					aNew = _sqlite3Realloc(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow, uint64(nOvfl*int64(2)*int64(4)))
				}
				if aNew == uintptr(0) {
					return int32(SQLITE_NOMEM)
				} else {
					(*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow = aNew
				}
			}
			libc.Xmemset(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow, 0, uint32(nOvfl*int64(4)))
			v1 = pCur + 1
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTCF_ValidOvfl))
		} else {
			/* Sanity check the validity of the overflow page cache */
			/* If the overflow page-list cache has been allocated and the
			 ** entry for the first required overflow page is valid, skip
			 ** directly to it.
			 */
			if **(**TPgno)(__ccgo_up((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(offset/ovflSize)*4)) != 0 {
				iIdx = int32(offset / ovflSize)
				**(**TPgno)(__ccgo_up(bp)) = **(**TPgno)(__ccgo_up((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(iIdx)*4))
				offset = offset % ovflSize
			}
		}
		for **(**TPgno)(__ccgo_up(bp)) != 0 {
			/* If required, populate the overflow page-list cache. */
			if **(**TPgno)(__ccgo_up(bp)) > (*TBtShared)(unsafe.Pointer(pBt)).FnPage {
				return _sqlite3CorruptError(tls, int32(78459))
			}
			**(**TPgno)(__ccgo_up((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(iIdx)*4)) = **(**TPgno)(__ccgo_up(bp))
			if offset >= ovflSize {
				/* The only reason to read this page is to obtain the page
				 ** number for the next page in the overflow chain. The page
				 ** data is not required. So first try to lookup the overflow
				 ** page-list cache, if any, then fall back to the getOverflowPage()
				 ** function.
				 */
				if **(**TPgno)(__ccgo_up((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(iIdx+int32(1))*4)) != 0 {
					**(**TPgno)(__ccgo_up(bp)) = **(**TPgno)(__ccgo_up((*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow + uintptr(iIdx+int32(1))*4))
				} else {
					rc = _getOverflowPage(tls, pBt, **(**TPgno)(__ccgo_up(bp)), uintptr(0), bp)
				}
				offset = offset - ovflSize
			} else {
				/* Need to read this page properly. It contains some of the
				 ** range of data that is being read (eOp==0) or written (eOp!=0).
				 */
				a1 = int32(amt)
				if uint32(a1)+offset > ovflSize {
					a1 = int32(ovflSize - offset)
				}
				/* If all the following are true:
				 **
				 **   1) this is a read operation, and
				 **   2) data is required from the start of this overflow page, and
				 **   3) there are no dirty pages in the page-cache
				 **   4) the database is file-backed, and
				 **   5) the page is not in the WAL file
				 **   6) at least 4 bytes have already been read into the output buffer
				 **
				 ** then data can be read directly from the database file into the
				 ** output buffer, bypassing the page-cache altogether. This speeds
				 ** up loading large records that span many overflow pages.
				 */
				if eOp == 0 && offset == uint32(0) && _sqlite3PagerDirectReadOk(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, **(**TPgno)(__ccgo_up(bp))) != 0 && pBuf+uintptr(-libc.Int32FromInt32(4)) >= pBufStart {
					fd = _sqlite3PagerFile(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager)
					aWrite = pBuf + uintptr(-libc.Int32FromInt32(4))
					/* due to (6) */
					libc.Xmemcpy(tls, bp+4, aWrite, uint32(4))
					rc = _sqlite3OsRead(tls, fd, aWrite, a1+int32(4), int64((*TBtShared)(unsafe.Pointer(pBt)).FpageSize)*int64(**(**TPgno)(__ccgo_up(bp))-libc.Uint32FromInt32(1)))
					**(**TPgno)(__ccgo_up(bp)) = _sqlite3Get4byte(tls, aWrite)
					libc.Xmemcpy(tls, aWrite, bp+4, uint32(4))
				} else {
					if eOp == 0 {
						v2 = int32(PAGER_GET_READONLY)
					} else {
						v2 = 0
					}
					rc = _sqlite3PagerGet(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, **(**TPgno)(__ccgo_up(bp)), bp+8, v2)
					if rc == SQLITE_OK {
						if eOp != 0 && (_sqlite3PagerPageRefcount(tls, **(**uintptr)(__ccgo_up(bp + 8))) != int32(1) || (*TMemPage)(unsafe.Pointer(_sqlite3PagerGetExtra(tls, **(**uintptr)(__ccgo_up(bp + 8))))).FisInit != 0) {
							_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp + 8)))
							return _sqlite3CorruptError(tls, int32(78529))
						}
						aPayload = _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp + 8)))
						**(**TPgno)(__ccgo_up(bp)) = _sqlite3Get4byte(tls, aPayload)
						rc = _copyPayload(tls, aPayload+uintptr(offset+uint32(4)), pBuf, a1, eOp, **(**uintptr)(__ccgo_up(bp + 8)))
						_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp + 8)))
						offset = uint32(0)
					}
				}
				amt = amt - uint32(a1)
				if amt == uint32(0) {
					return rc
				}
				pBuf = pBuf + uintptr(a1)
			}
			if rc != 0 {
				break
			}
			iIdx = iIdx + 1
		}
	}
	if rc == SQLITE_OK && amt > uint32(0) {
		/* Overflow chain ends prematurely */
		return _sqlite3CorruptError(tls, int32(78549))
	}
	return rc
}

// C documentation
//
//	/*
//	** Internal SQL function:
//	**
//	**     sqlite_add_constraint(SQL, CONSTRAINT-TEXT, ICOL)
//	**
//	** SQL is a CREATE TABLE statement.  Return a modified version of
//	** SQL that adds CONSTRAINT-TEXT at the end of the ICOL-th column
//	** definition.  (The left-most column defintion is 0.)
//	*/
func _addConstraintFunc(tls *libc.TLS, ctx uintptr, NotUsed int32, argv uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var db, zCons, zNew, zSql uintptr
	var iCol, ii, nTok int32
	var _ /* iOff at bp+0 */ int32
	var _ /* t at bp+4 */ int32
	_, _, _, _, _, _, _ = db, iCol, ii, nTok, zCons, zNew, zSql
	zSql = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	zCons = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	iCol = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 2*4)))
	**(**int32)(__ccgo_up(bp)) = 0
	zNew = uintptr(0)
	**(**int32)(__ccgo_up(bp + 4)) = 0
	_ = NotUsed
	if _skipCreateTable(tls, ctx, zSql, bp) != 0 {
		return
	}
	ii = 0
	for {
		if !(ii <= iCol || iCol < 0 && **(**int32)(__ccgo_up(bp + 4)) != int32(TK_RP)) {
			break
		}
		**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + _getConstraintToken(tls, zSql+uintptr(**(**int32)(__ccgo_up(bp))), bp+4)
		for int32(1) != 0 {
			nTok = _getConstraintToken(tls, zSql+uintptr(**(**int32)(__ccgo_up(bp))), bp+4)
			if **(**int32)(__ccgo_up(bp + 4)) == int32(TK_COMMA) || **(**int32)(__ccgo_up(bp + 4)) == int32(TK_RP) {
				break
			}
			if **(**int32)(__ccgo_up(bp + 4)) == int32(TK_ILLEGAL) {
				Xsqlite3_result_error_code(tls, ctx, _sqlite3CorruptError(tls, int32(123226)))
				return
			}
			**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + nTok
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + _getWhitespace(tls, zSql+uintptr(**(**int32)(__ccgo_up(bp))))
	db = Xsqlite3_context_db_handle(tls, ctx)
	if iCol < 0 {
		zNew = _sqlite3MPrintf(tls, db, __ccgo_ts+13102, libc.VaList(bp+16, **(**int32)(__ccgo_up(bp)), zSql, zCons, zSql+uintptr(**(**int32)(__ccgo_up(bp)))))
	} else {
		zNew = _sqlite3MPrintf(tls, db, __ccgo_ts+13113, libc.VaList(bp+16, **(**int32)(__ccgo_up(bp)), zSql, zCons, zSql+uintptr(**(**int32)(__ccgo_up(bp)))))
	}
	Xsqlite3_result_text(tls, ctx, zNew, -int32(1), __ccgo_fp(_sqlite3RowSetClear))
}

// C documentation
//
//	/*
//	** Add a new module argument to pTable->u.vtab.azArg[].
//	** The string is not copied - the pointer is stored.  The
//	** string will be freed automatically when the table is
//	** deleted.
//	*/
func _addModuleArgument(tls *libc.TLS, pParse uintptr, pTable uintptr, zArg uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var azModuleArg, db, v2 uintptr
	var i, v1 int32
	var nBytes Tsqlite3_int64
	_, _, _, _, _, _ = azModuleArg, db, i, nBytes, v1, v2
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	nBytes = int64(uint32(4) * uint32(libc.Int32FromInt32(2)+(*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTable + 44))).FnArg))
	if (*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTable + 44))).FnArg+int32(3) >= **(**int32)(__ccgo_up(db + 120 + 2*4)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15173, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTable)).FzName))
	}
	azModuleArg = _sqlite3DbRealloc(tls, db, (*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTable + 44))).FazArg, uint64(nBytes))
	if azModuleArg == uintptr(0) {
		_sqlite3DbFree(tls, db, zArg)
	} else {
		v2 = pTable + 44
		v1 = *(*int32)(unsafe.Pointer(v2))
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		i = v1
		**(**uintptr)(__ccgo_up(azModuleArg + uintptr(i)*4)) = zArg
		**(**uintptr)(__ccgo_up(azModuleArg + uintptr(i+int32(1))*4)) = uintptr(0)
		(*(*struct {
			FnArg  int32
			FazArg uintptr
			Fp     uintptr
		})(unsafe.Pointer(pTable + 44))).FazArg = azModuleArg
	}
}

func _addOp4IntSlow(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32, p4 int32) (r int32) {
	var addr int32
	var pOp uintptr
	_, _ = addr, pOp
	addr = _sqlite3VdbeAddOp3(tls, p, op, p1, p2, p3)
	if int32((*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FmallocFailed) == 0 {
		pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(addr)*20
		(*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(3))
		*(*int32)(unsafe.Pointer(pOp + 16)) = p4
	}
	return addr
}

// C documentation
//
//	/*
//	** The pOrderBy->a[].u.x.iOrderByCol values might be incorrect because
//	** columns might have been rearranged in the result set.  This routine
//	** fixes them up.
//	**
//	** pEList is the new result set.  The pEList->a[].u.x.iOrderByCol values
//	** contain the *old* locations of each expression.  This is a temporary
//	** use of u.x.iOrderByCol, not its intended use.  The caller must reset
//	** u.x.iOrderByCol back to zero for all entries in pEList before the
//	** caller returns.
//	**
//	** This routine changes pOrderBy->a[].u.x.iOrderByCol values from
//	** pEList->a[N].u.x.iOrderByCol into N+1.  (The "+1" is because of the 1-based
//	** indexing used by iOrderByCol.)  Or if no match, iOrderByCol is set to zero.
//	*/
func _adjustOrderByCol(tls *libc.TLS, pOrderBy uintptr, pEList uintptr) {
	var i, j, t int32
	_, _, _ = i, j, t
	if pOrderBy == uintptr(0) {
		return
	}
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) {
			break
		}
		t = int32(*(*Tu16)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20 + 16)))
		if t == 0 {
			goto _1
		}
		j = 0
		for {
			if !(j < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
				break
			}
			if int32(*(*Tu16)(unsafe.Pointer(pEList + 8 + uintptr(j)*20 + 16))) == t {
				*(*Tu16)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20 + 16)) = uint16(j + int32(1))
				break
			}
			goto _2
		_2:
			;
			j = j + 1
		}
		if j >= (*TExprList)(unsafe.Pointer(pEList)).FnExpr {
			*(*Tu16)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20 + 16)) = uint16(0)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** This is a Walker expression node callback.
//	**
//	** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo
//	** object that is referenced does not refer directly to the Expr.  If
//	** it does, make a copy.  This is done because the pExpr argument is
//	** subject to change.
//	**
//	** The copy is scheduled for deletion using the sqlite3ExprDeferredDelete()
//	** which builds on the sqlite3ParserAddCleanup() mechanism.
//	*/
func _agginfoPersistExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var db, pAggInfo, pParse uintptr
	var iAgg int32
	_, _, _, _ = db, iAgg, pAggInfo, pParse
	if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Reduced)) != libc.Uint32FromInt32(0)) && (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo != uintptr(0) {
		pAggInfo = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo
		iAgg = int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)
		pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse
		db = (*TParse)(unsafe.Pointer(pParse)).Fdb
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_AGG_FUNCTION) {
			if iAgg < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn && (**(**TAggInfo_col)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(iAgg)*20))).FpCExpr == pExpr {
				pExpr = _sqlite3ExprDup(tls, db, pExpr, 0)
				if pExpr != 0 && !(_sqlite3ExprDeferredDelete(tls, pParse, pExpr) != 0) {
					(**(**TAggInfo_col)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(iAgg)*20))).FpCExpr = pExpr
				}
			}
		} else {
			if iAgg < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc && (**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(iAgg)*24))).FpFExpr == pExpr {
				pExpr = _sqlite3ExprDup(tls, db, pExpr, 0)
				if pExpr != 0 && !(_sqlite3ExprDeferredDelete(tls, pParse, pExpr) != 0) {
					(**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(iAgg)*24))).FpFExpr = pExpr
				}
			}
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Walker callback for aggregateConvertIndexedExprRefToColumn().
//	*/
func _aggregateIdxEprRefToColCallback(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var pAggInfo, pCol uintptr
	_, _ = pAggInfo, pCol
	_ = pWalker
	if (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo == uintptr(0) {
		return WRC_Continue
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_COLUMN) {
		return WRC_Continue
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_FUNCTION) {
		return WRC_Continue
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IF_NULL_ROW) {
		return WRC_Continue
	}
	pAggInfo = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo
	if int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) >= (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn {
		return WRC_Continue
	}
	pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)*20
	(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_AGG_COLUMN)
	(*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TAggInfo_col)(unsafe.Pointer(pCol)).FiTable
	(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16((*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn)
	**(**Tu32)(__ccgo_up(pExpr + 4)) &= ^uint32(libc.Int32FromInt32(EP_Skip) | libc.Int32FromInt32(EP_Collate) | libc.Int32FromInt32(EP_Unlikely))
	return int32(WRC_Prune)
}

// C documentation
//
//	/*
//	** Allocate a new page from the database file.
//	**
//	** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
//	** has already been called on the new page.)  The new page has also
//	** been referenced and the calling routine is responsible for calling
//	** sqlite3PagerUnref() on the new page when it is done.
//	**
//	** SQLITE_OK is returned on success.  Any other return value indicates
//	** an error.  *ppPage is set to NULL in the event of an error.
//	**
//	** If the "nearby" parameter is not 0, then an effort is made to
//	** locate a page close to the page number "nearby".  This can be used in an
//	** attempt to keep related pages close to each other in the database file,
//	** which in turn can make database access faster.
//	**
//	** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
//	** anywhere on the free-list, then it is guaranteed to be returned.  If
//	** eMode is BTALLOC_LT then the page returned will be less than or equal
//	** to nearby if any such page exists.  If eMode is BTALLOC_ANY then there
//	** are no restrictions on which page is returned.
//	*/
func _allocateBtreePage(tls *libc.TLS, pBt uintptr, ppPage uintptr, pPgno uintptr, nearby TPgno, eMode Tu8) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aData, pPage1, pPrevTrunk uintptr
	var bNoContent, d2, dist, noContent, rc, v5 int32
	var closest, i, k, n, nSearch, v1 Tu32
	var iNewTrunk, iPage, iTrunk, mxPage TPgno
	var searchList Tu8
	var v2 bool
	var _ /* eType at bp+4 */ Tu8
	var _ /* pNewTrunk at bp+8 */ uintptr
	var _ /* pPg at bp+12 */ uintptr
	var _ /* pTrunk at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, bNoContent, closest, d2, dist, i, iNewTrunk, iPage, iTrunk, k, mxPage, n, nSearch, noContent, pPage1, pPrevTrunk, rc, searchList, v1, v2, v5 /* Number of leaves on the trunk of the freelist */
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	pPrevTrunk = uintptr(0) /* Total size of the database file */
	pPage1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1
	mxPage = _btreePagecount(tls, pBt)
	/* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
	 ** stores the total number of pages on the freelist. */
	n = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+36)
	if n >= mxPage {
		return _sqlite3CorruptError(tls, int32(79764))
	}
	if n > uint32(0) {
		searchList = uint8(0) /* If the free-list must be searched for 'nearby' */
		nSearch = uint32(0)   /* Count of the number of search attempts */
		/* If eMode==BTALLOC_EXACT and a query of the pointer-map
		 ** shows that the page 'nearby' is somewhere on the free-list, then
		 ** the entire-list will be searched for that page.
		 */
		if int32(eMode) == int32(BTALLOC_EXACT) {
			if nearby <= mxPage {
				rc = _ptrmapGet(tls, pBt, nearby, bp+4, uintptr(0))
				if rc != 0 {
					return rc
				}
				if int32(**(**Tu8)(__ccgo_up(bp + 4))) == int32(PTRMAP_FREEPAGE) {
					searchList = uint8(1)
				}
			}
		} else {
			if int32(eMode) == int32(BTALLOC_LE) {
				searchList = uint8(1)
			}
		}
		/* Decrement the free-list count by 1. Set iTrunk to the index of the
		 ** first free-list trunk page. iPrevTrunk is initially 1.
		 */
		rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FpDbPage)
		if rc != 0 {
			return rc
		}
		_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+36, n-uint32(1))
		/* The code within this loop is run only once if the 'searchList' variable
		 ** is not true. Otherwise, it runs once for each trunk-page on the
		 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
		 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
		 */
		for cond := true; cond; cond = searchList != 0 {
			pPrevTrunk = **(**uintptr)(__ccgo_up(bp))
			if pPrevTrunk != 0 {
				/* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
				 ** is the page number of the next freelist trunk page in the list or
				 ** zero if this is the last freelist trunk page. */
				iTrunk = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FaData)
			} else {
				/* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
				 ** stores the page number of the first page of the freelist, or zero if
				 ** the freelist is empty. */
				iTrunk = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32)
			}
			if v2 = iTrunk > mxPage; !v2 {
				v1 = nSearch
				nSearch = nSearch + 1
			}
			if v2 || v1 > n {
				rc = _sqlite3CorruptError(tls, int32(79820))
			} else {
				rc = _btreeGetUnusedPage(tls, pBt, iTrunk, bp, 0)
			}
			if rc != 0 {
				**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
				goto end_allocate_page
			}
			/* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
			 ** is the number of leaf page pointers to follow. */
			k = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData+4)
			if k == uint32(0) && !(searchList != 0) {
				/* The trunk has no leaves and the list is not being searched.
				 ** So extract the trunk page itself and use it as the newly
				 ** allocated page */
				rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpDbPage)
				if rc != 0 {
					goto end_allocate_page
				}
				**(**TPgno)(__ccgo_up(pPgno)) = iTrunk
				libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData, uint32(4))
				**(**uintptr)(__ccgo_up(ppPage)) = **(**uintptr)(__ccgo_up(bp))
				**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			} else {
				if k > (*TBtShared)(unsafe.Pointer(pBt)).FusableSize/libc.Uint32FromInt32(4)-libc.Uint32FromInt32(2) {
					/* Value of k is out of range.  Database corruption */
					rc = _sqlite3CorruptError(tls, int32(79849))
					goto end_allocate_page
				} else {
					if searchList != 0 && (nearby == iTrunk || iTrunk < nearby && int32(eMode) == int32(BTALLOC_LE)) {
						/* The list is being searched and this trunk page is the page
						 ** to allocate, regardless of whether it has leaves.
						 */
						**(**TPgno)(__ccgo_up(pPgno)) = iTrunk
						**(**uintptr)(__ccgo_up(ppPage)) = **(**uintptr)(__ccgo_up(bp))
						searchList = uint8(0)
						rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpDbPage)
						if rc != 0 {
							goto end_allocate_page
						}
						if k == uint32(0) {
							if !(pPrevTrunk != 0) {
								libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData, uint32(4))
							} else {
								rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FpDbPage)
								if rc != SQLITE_OK {
									goto end_allocate_page
								}
								libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FaData, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData, uint32(4))
							}
						} else {
							iNewTrunk = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData+8)
							if iNewTrunk > mxPage {
								rc = _sqlite3CorruptError(tls, int32(79883))
								goto end_allocate_page
							}
							rc = _btreeGetUnusedPage(tls, pBt, iNewTrunk, bp+8, 0)
							if rc != SQLITE_OK {
								goto end_allocate_page
							}
							rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FpDbPage)
							if rc != SQLITE_OK {
								_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 8)))
								goto end_allocate_page
							}
							libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FaData, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData, uint32(4))
							_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FaData+4, k-uint32(1))
							libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FaData+8, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData+12, (k-uint32(1))*uint32(4))
							_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 8)))
							if !(pPrevTrunk != 0) {
								_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, iNewTrunk)
							} else {
								rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FpDbPage)
								if rc != 0 {
									goto end_allocate_page
								}
								_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FaData, iNewTrunk)
							}
						}
						**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
					} else {
						if k > uint32(0) {
							aData = (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData
							if nearby > uint32(0) {
								closest = uint32(0)
								if int32(eMode) == int32(BTALLOC_LE) {
									i = uint32(0)
									for {
										if !(i < k) {
											break
										}
										iPage = _sqlite3Get4byte(tls, aData+uintptr(uint32(8)+i*uint32(4)))
										if iPage <= nearby {
											closest = i
											break
										}
										goto _3
									_3:
										;
										i = i + 1
									}
								} else {
									dist = _sqlite3AbsInt32(tls, int32(_sqlite3Get4byte(tls, aData+8)-nearby))
									i = uint32(1)
									for {
										if !(i < k) {
											break
										}
										d2 = _sqlite3AbsInt32(tls, int32(_sqlite3Get4byte(tls, aData+uintptr(uint32(8)+i*uint32(4)))-nearby))
										if d2 < dist {
											closest = i
											dist = d2
										}
										goto _4
									_4:
										;
										i = i + 1
									}
								}
							} else {
								closest = uint32(0)
							}
							iPage = _sqlite3Get4byte(tls, aData+uintptr(uint32(8)+closest*uint32(4)))
							if iPage > mxPage || iPage < uint32(2) {
								rc = _sqlite3CorruptError(tls, int32(79948))
								goto end_allocate_page
							}
							if !(searchList != 0) || (iPage == nearby || iPage < nearby && int32(eMode) == int32(BTALLOC_LE)) {
								**(**TPgno)(__ccgo_up(pPgno)) = iPage
								rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpDbPage)
								if rc != 0 {
									goto end_allocate_page
								}
								if closest < k-uint32(1) {
									libc.Xmemcpy(tls, aData+uintptr(uint32(8)+closest*uint32(4)), aData+uintptr(uint32(4)+k*uint32(4)), uint32(4))
								}
								_sqlite3Put4byte(tls, aData+4, k-uint32(1))
								if !(_btreeGetHasContent(tls, pBt, **(**TPgno)(__ccgo_up(pPgno))) != 0) {
									v5 = int32(PAGER_GET_NOCONTENT)
								} else {
									v5 = 0
								}
								noContent = v5
								rc = _btreeGetUnusedPage(tls, pBt, **(**TPgno)(__ccgo_up(pPgno)), ppPage, noContent)
								if rc == SQLITE_OK {
									rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(ppPage)))).FpDbPage)
									if rc != SQLITE_OK {
										_releasePage(tls, **(**uintptr)(__ccgo_up(ppPage)))
										**(**uintptr)(__ccgo_up(ppPage)) = uintptr(0)
									}
								}
								searchList = uint8(0)
							}
						}
					}
				}
			}
			_releasePage(tls, pPrevTrunk)
			pPrevTrunk = uintptr(0)
		}
	} else {
		if 0 == int32((*TBtShared)(unsafe.Pointer(pBt)).FbDoTruncate) {
			v5 = int32(PAGER_GET_NOCONTENT)
		} else {
			v5 = 0
		}
		/* There are no pages on the freelist, so append a new page to the
		 ** database image.
		 **
		 ** Normally, new pages allocated by this block can be requested from the
		 ** pager layer with the 'no-content' flag set. This prevents the pager
		 ** from trying to read the pages content from disk. However, if the
		 ** current transaction has already run one or more incremental-vacuum
		 ** steps, then the page we are about to allocate may contain content
		 ** that is required in the event of a rollback. In this case, do
		 ** not set the no-content flag. This causes the pager to load and journal
		 ** the current page content before overwriting it.
		 **
		 ** Note that the pager will not actually attempt to load or journal
		 ** content for any page that really does lie past the end of the database
		 ** file on disk. So the effects of disabling the no-content optimization
		 ** here are confined to those pages that lie between the end of the
		 ** database image and the end of the database file.
		 */
		bNoContent = v5
		rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FpDbPage)
		if rc != 0 {
			return rc
		}
		(*TBtShared)(unsafe.Pointer(pBt)).FnPage = (*TBtShared)(unsafe.Pointer(pBt)).FnPage + 1
		if (*TBtShared)(unsafe.Pointer(pBt)).FnPage == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) {
			(*TBtShared)(unsafe.Pointer(pBt)).FnPage = (*TBtShared)(unsafe.Pointer(pBt)).FnPage + 1
		}
		if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && _ptrmapPageno(tls, pBt, (*TBtShared)(unsafe.Pointer(pBt)).FnPage) == (*TBtShared)(unsafe.Pointer(pBt)).FnPage {
			/* If *pPgno refers to a pointer-map page, allocate two new pages
			 ** at the end of the file instead of one. The first allocated page
			 ** becomes a new pointer-map page, the second is used by the caller.
			 */
			**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
			rc = _btreeGetUnusedPage(tls, pBt, (*TBtShared)(unsafe.Pointer(pBt)).FnPage, bp+12, bNoContent)
			if rc == SQLITE_OK {
				rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FpDbPage)
				_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 12)))
			}
			if rc != 0 {
				return rc
			}
			(*TBtShared)(unsafe.Pointer(pBt)).FnPage = (*TBtShared)(unsafe.Pointer(pBt)).FnPage + 1
			if (*TBtShared)(unsafe.Pointer(pBt)).FnPage == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) {
				(*TBtShared)(unsafe.Pointer(pBt)).FnPage = (*TBtShared)(unsafe.Pointer(pBt)).FnPage + 1
			}
		}
		_sqlite3Put4byte(tls, uintptr(28)+(*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData, (*TBtShared)(unsafe.Pointer(pBt)).FnPage)
		**(**TPgno)(__ccgo_up(pPgno)) = (*TBtShared)(unsafe.Pointer(pBt)).FnPage
		rc = _btreeGetUnusedPage(tls, pBt, **(**TPgno)(__ccgo_up(pPgno)), ppPage, bNoContent)
		if rc != 0 {
			return rc
		}
		rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(ppPage)))).FpDbPage)
		if rc != SQLITE_OK {
			_releasePage(tls, **(**uintptr)(__ccgo_up(ppPage)))
			**(**uintptr)(__ccgo_up(ppPage)) = uintptr(0)
		}
	}
	goto end_allocate_page
end_allocate_page:
	;
	_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
	_releasePage(tls, pPrevTrunk)
	return rc
}

// C documentation
//
//	/*
//	** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
//	** if we run out of memory.
//	*/
func _allocateCursor(tls *libc.TLS, p uintptr, iCur int32, nField int32, eCurType Tu8) (r uintptr) {
	var nByte Ti64
	var pCx, pMem, v1 uintptr
	_, _, _, _ = nByte, pCx, pMem, v1
	if iCur > 0 {
		v1 = (*TVdbe)(unsafe.Pointer(p)).FaMem + uintptr((*TVdbe)(unsafe.Pointer(p)).FnMem-iCur)*40
	} else {
		v1 = (*TVdbe)(unsafe.Pointer(p)).FaMem
	}
	/* Find the memory cell that will be used to store the blob of memory
	 ** required for this VdbeCursor structure. It is convenient to use a
	 ** vdbe memory cell to manage the memory allocation required for a
	 ** VdbeCursor structure for the following reasons:
	 **
	 **   * Sometimes cursor numbers are used for a couple of different
	 **     purposes in a vdbe program. The different uses might require
	 **     different sized allocations. Memory cells provide growable
	 **     allocations.
	 **
	 **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
	 **     be freed lazily via the sqlite3_release_memory() API. This
	 **     minimizes the number of malloc calls made by the system.
	 **
	 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
	 ** the top of the register space.  Cursor 1 is at Mem[p->nMem-1].
	 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
	 */
	pMem = v1
	pCx = uintptr(0)
	nByte = int64((uint32(libc.UintptrFromInt32(0)+84)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)) + uint32(nField+libc.Int32FromInt32(1))*libc.Uint32FromInt64(8))
	if int32(eCurType) == CURTYPE_BTREE {
		nByte = nByte + int64(_sqlite3BtreeCursorSize(tls))
	}
	if **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*4)) != 0 { /*OPTIMIZATION-IF-FALSE*/
		_sqlite3VdbeFreeCursorNN(tls, p, **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*4)))
		**(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*4)) = uintptr(0)
	}
	/* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure
	 ** the pMem used to hold space for the cursor has enough storage available
	 ** in pMem->zMalloc.  But for the special case of the aMem[] entries used
	 ** to hold cursors, it is faster to in-line the logic. */
	if int64((*TMem)(unsafe.Pointer(pMem)).FszMalloc) < nByte {
		if (*TMem)(unsafe.Pointer(pMem)).FszMalloc > 0 {
			_sqlite3DbFreeNN(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc)
		}
		v1 = _sqlite3DbMallocRaw(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, uint64(nByte))
		(*TMem)(unsafe.Pointer(pMem)).FzMalloc = v1
		(*TMem)(unsafe.Pointer(pMem)).Fz = v1
		if (*TMem)(unsafe.Pointer(pMem)).FzMalloc == uintptr(0) {
			(*TMem)(unsafe.Pointer(pMem)).FszMalloc = 0
			return uintptr(0)
		}
		(*TMem)(unsafe.Pointer(pMem)).FszMalloc = int32(nByte)
	}
	v1 = (*TMem)(unsafe.Pointer(pMem)).FzMalloc
	pCx = v1
	**(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*4)) = v1
	libc.Xmemset(tls, pCx, 0, uint32(libc.UintptrFromInt32(0)+32))
	(*TVdbeCursor)(unsafe.Pointer(pCx)).FeCurType = eCurType
	(*TVdbeCursor)(unsafe.Pointer(pCx)).FnField = int16(nField)
	(*TVdbeCursor)(unsafe.Pointer(pCx)).FaOffset = pCx + 84 + uintptr(nField)*4
	if int32(eCurType) == CURTYPE_BTREE {
		*(*uintptr)(unsafe.Pointer(pCx + 36)) = (*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((uint32(libc.UintptrFromInt32(0)+84)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))+uint32(nField+libc.Int32FromInt32(1))*libc.Uint32FromInt64(8))
		_sqlite3BtreeCursorZero(tls, *(*uintptr)(unsafe.Pointer(pCx + 36)))
	}
	return pCx
}

// C documentation
//
//	/*
//	** Allocate and populate an sqlite3_index_info structure. It is the
//	** responsibility of the caller to eventually release the structure
//	** by passing the pointer returned by this function to freeIndexInfo().
//	*/
func _allocateIndexInfo(tls *libc.TLS, pWInfo uintptr, pWC uintptr, mUnusable TBitmask, pSrc uintptr, pmNoOmit uintptr) (r uintptr) {
	var bSortByGroup, eDistinct, i, iCol, j, n, nLast, nOrderBy, nTerm, v10 int32
	var mNoOmit, op Tu16
	var p, pE2, pExpr, pExpr1, pHidden, pIdxCons, pIdxInfo, pIdxOrderBy, pOrderBy, pParse, pPk, pTab, pTerm, pUsage, zColl, v3 uintptr
	var v12 uint32
	var v7 bool
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bSortByGroup, eDistinct, i, iCol, j, mNoOmit, n, nLast, nOrderBy, nTerm, op, p, pE2, pExpr, pExpr1, pHidden, pIdxCons, pIdxInfo, pIdxOrderBy, pOrderBy, pParse, pPk, pTab, pTerm, pUsage, zColl, v10, v12, v3, v7
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse
	mNoOmit = uint16(0)
	eDistinct = 0
	pOrderBy = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy
	pTab = (*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab
	/* Find all WHERE clause constraints referring to this virtual table.
	 ** Mark each term with the TERM_OK flag.  Set nTerm to the number of
	 ** terms found.
	 */
	p = pWC
	nTerm = libc.Int32FromInt32(0)
	for {
		if !(p != 0) {
			break
		}
		i = 0
		pTerm = (*TWhereClause)(unsafe.Pointer(p)).Fa
		for {
			if !(i < (*TWhereClause)(unsafe.Pointer(p)).FnTerm) {
				break
			}
			v3 = pTerm + 10
			*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) & ^libc.Int32FromInt32(TERM_OK))
			if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor != (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor {
				goto _2
			}
			if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&mUnusable != 0 {
				goto _2
			}
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) & ^libc.Int32FromInt32(WO_EQUIV) == 0 {
				goto _2
			}
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VNULL) != 0 {
				goto _2
			}
			if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 && !(_constraintCompatibleWithOuterJoin(tls, pTerm, pSrc) != 0) {
				goto _2
			}
			nTerm = nTerm + 1
			v3 = pTerm + 10
			*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | libc.Int32FromInt32(TERM_OK))
			goto _2
		_2:
			;
			i = i + 1
			pTerm += 48
		}
		goto _1
	_1:
		;
		p = (*TWhereClause)(unsafe.Pointer(p)).FpOuter
	}
	/* If the ORDER BY clause contains only columns in the current
	 ** virtual table then allocate space for the aOrderBy part of
	 ** the sqlite3_index_info structure.
	 */
	nOrderBy = 0
	if pOrderBy != 0 {
		n = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr
		i = 0
		for {
			if !(i < n) {
				break
			}
			pExpr = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr
			/* Skip over constant terms in the ORDER BY clause */
			if _sqlite3ExprIsConstant(tls, uintptr(0), pExpr) != 0 {
				goto _5
			}
			/* Virtual tables are unable to deal with NULLS FIRST */
			if int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 {
				break
			}
			/* First case - a direct column references without a COLLATE operator */
			if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor {
				goto _5
			}
			/* 2nd case - a column reference with a COLLATE operator.  Only match
			 ** of the COLLATE operator matches the collation of the column. */
			if v7 = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE); v7 {
				v3 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
				pE2 = v3
			}
			if v7 && int32((*TExpr)(unsafe.Pointer(v3)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pE2)).FiTable == (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor { /* The collating sequence name */
				(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = (*TExpr)(unsafe.Pointer(pE2)).FiColumn
				if int32((*TExpr)(unsafe.Pointer(pE2)).FiColumn) < 0 {
					goto _5
				} /* Collseq does not matter for rowid */
				zColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr((*TExpr)(unsafe.Pointer(pE2)).FiColumn)*12)
				if zColl == uintptr(0) {
					zColl = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
				}
				if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), zColl) == 0 {
					goto _5
				}
			}
			/* No matches cause a break out of the loop */
			break
			goto _5
		_5:
			;
			i = i + 1
		}
		if i == n {
			bSortByGroup = libc.BoolInt32(int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_SORTBYGROUP) != 0)
			nOrderBy = n
			if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DISTINCTBY) != 0 && !(int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x8000>>15) != 0) {
				eDistinct = int32(2) + bSortByGroup
			} else {
				if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_GROUPBY) != 0 {
					eDistinct = int32(1) - bSortByGroup
				} else {
					if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 {
						eDistinct = int32(3)
					}
				}
			}
		}
	}
	/* Allocate the sqlite3_index_info structure
	 */
	pIdxInfo = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(72)+(libc.Uint32FromInt64(12)+libc.Uint32FromInt64(8))*uint32(nTerm)+uint32(8)*uint32(nOrderBy)+(uint32(libc.UintptrFromInt32(0)+20)+uint32(nTerm)*uint32(4))))
	if pIdxInfo == uintptr(0) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1681, 0)
		return uintptr(0)
	}
	pHidden = pIdxInfo + 1*72
	pIdxCons = pHidden + 20 + uintptr(nTerm)*4
	pIdxOrderBy = pIdxCons + uintptr(nTerm)*12
	pUsage = pIdxOrderBy + uintptr(nOrderBy)*8
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint = pIdxCons
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy = pIdxOrderBy
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage = pUsage
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FcolUsed = uint64(int64((*TSrcItem)(unsafe.Pointer(pSrc)).FcolUsed))
	if libc.BoolInt32((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0)) == 0 {
		/* Ensure that all bits associated with PK columns are set. This is to
		 ** ensure they are available for cases like RIGHT joins or OR loops. */
		pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
		i = 0
		for {
			if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
				break
			}
			iCol = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2)))
			if iCol >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) {
				iCol = int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1)
			}
			**(**Tsqlite3_uint64)(__ccgo_up(pIdxInfo + 64)) |= libc.Uint64FromInt32(1) << iCol
			goto _8
		_8:
			;
			i = i + 1
		}
	}
	(*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FpWC = pWC
	(*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FpParse = pParse
	(*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FeDistinct = eDistinct
	(*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FmIn = uint32(0)
	p = pWC
	v10 = libc.Int32FromInt32(0)
	j = v10
	i = v10
	for {
		if !(p != 0) {
			break
		}
		nLast = i + (*TWhereClause)(unsafe.Pointer(p)).FnTerm
		pTerm = (*TWhereClause)(unsafe.Pointer(p)).Fa
		for {
			if !(i < nLast) {
				break
			}
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_OK) == 0 {
				goto _11
			}
			(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).FiColumn = (*(*struct {
				FleftColumn int32
				FiField     int32
			})(unsafe.Pointer(pTerm + 24))).FleftColumn
			(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).FiTermOffset = i
			op = uint16(int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) & int32(WO_ALL))
			if int32(op) == int32(WO_IN) {
				if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_SLICE) == 0 {
					if j <= int32(31) {
						v12 = libc.Uint32FromInt32(1) << j
					} else {
						v12 = uint32(0)
					}
					**(**Tu32)(__ccgo_up(pHidden + 12)) |= v12
				}
				op = uint16(WO_EQ)
			}
			if int32(op) == int32(WO_AUX) {
				(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).Fop = (*TWhereTerm)(unsafe.Pointer(pTerm)).FeMatchOp
			} else {
				if int32(op)&(libc.Int32FromInt32(WO_ISNULL)|libc.Int32FromInt32(WO_IS)) != 0 {
					if int32(op) == int32(WO_ISNULL) {
						(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).Fop = uint8(SQLITE_INDEX_CONSTRAINT_ISNULL)
					} else {
						(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).Fop = uint8(SQLITE_INDEX_CONSTRAINT_IS)
					}
				} else {
					(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).Fop = uint8(op)
					/* The direct assignment in the previous line is possible only because
					 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
					 ** following asserts verify this fact. */
					if int32(op)&(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) != 0 && _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight) != 0 {
						if j < int32(16) {
							mNoOmit = uint16(int32(mNoOmit) | libc.Int32FromInt32(1)<<j)
						}
						if int32(op) == libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) {
							(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).Fop = uint8(libc.Int32FromInt32(WO_EQ) << (libc.Int32FromInt32(TK_LE) - libc.Int32FromInt32(TK_EQ)))
						}
						if int32(op) == libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ)) {
							(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).Fop = uint8(libc.Int32FromInt32(WO_EQ) << (libc.Int32FromInt32(TK_GE) - libc.Int32FromInt32(TK_EQ)))
						}
					}
				}
			}
			j = j + 1
			goto _11
		_11:
			;
			i = i + 1
			pTerm += 48
		}
		goto _9
	_9:
		;
		p = (*TWhereClause)(unsafe.Pointer(p)).FpOuter
	}
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint = j
	v10 = libc.Int32FromInt32(0)
	j = v10
	i = v10
	for {
		if !(i < nOrderBy) {
			break
		}
		pExpr1 = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr
		if _sqlite3ExprIsConstant(tls, uintptr(0), pExpr1) != 0 {
			goto _13
		}
		(**(**Tsqlite3_index_orderby)(__ccgo_up(pIdxOrderBy + uintptr(j)*8))).FiColumn = int32((*TExpr)(unsafe.Pointer(pExpr1)).FiColumn)
		(**(**Tsqlite3_index_orderby)(__ccgo_up(pIdxOrderBy + uintptr(j)*8))).Fdesc = uint8(int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags) & int32(KEYINFO_ORDER_DESC))
		j = j + 1
		goto _13
	_13:
		;
		i = i + 1
	}
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnOrderBy = j
	**(**Tu16)(__ccgo_up(pmNoOmit)) = mNoOmit
	return pIdxInfo
}

// C documentation
//
//	/*
//	** Allocate nByte bytes of space from within the B-Tree page passed
//	** as the first argument. Write into *pIdx the index into pPage->aData[]
//	** of the first byte of allocated space. Return either SQLITE_OK or
//	** an error code (usually SQLITE_CORRUPT).
//	**
//	** The caller guarantees that there is sufficient space to make the
//	** allocation.  This routine might need to defragment in order to bring
//	** all the space together, however.  This routine will avoid using
//	** the first two bytes past the cell pointer area since presumably this
//	** allocation is being made in order to insert a new cell, so we will
//	** also end up needing a new cell pointer.
//	*/
func _allocateSpace(tls *libc.TLS, pPage uintptr, nByte int32, pIdx uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var data, pSpace, pTmp uintptr
	var g2, gap, hdr, top, v1 int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _ = data, g2, gap, hdr, pSpace, pTmp, top, v1
	hdr = int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset) /* Local cache of pPage->hdrOffset */
	data = (*TMemPage)(unsafe.Pointer(pPage)).FaData           /* First byte of cell content area */
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK                     /* First byte of gap between cell pointers and cell content */
	/* Minimum cell size is 4 */
	gap = int32((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset) + int32(2)*int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)
	/* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
	 ** and the reserved space is zero (the usual value for reserved space)
	 ** then the cell content offset of an empty page wants to be 65536.
	 ** However, that integer is too large to be stored in a 2-byte unsigned
	 ** integer, so a value of 0 is used in its place. */
	pTmp = data + uintptr(hdr+int32(5))
	top = int32(**(**Tu8)(__ccgo_up(pTmp)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(pTmp + 1)))
	if gap > top {
		if top == 0 && (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize == uint32(65536) {
			top = int32(65536)
		} else {
			return _sqlite3CorruptError(tls, int32(75075))
		}
	} else {
		if top > int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) {
			return _sqlite3CorruptError(tls, int32(75078))
		}
	}
	/* If there is enough space between gap and top for one more cell pointer,
	 ** and if the freelist is not empty, then search the
	 ** freelist looking for a slot big enough to satisfy the request.
	 */
	if (**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(2)))) != 0 || **(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(1)))) != 0) && gap+int32(2) <= top {
		pSpace = _pageFindSlot(tls, pPage, nByte, bp)
		if pSpace != 0 {
			v1 = int32(pSpace) - int32(data)
			g2 = v1
			**(**int32)(__ccgo_up(pIdx)) = v1
			if g2 <= gap {
				return _sqlite3CorruptError(tls, int32(75095))
			} else {
				return SQLITE_OK
			}
		} else {
			if **(**int32)(__ccgo_up(bp)) != 0 {
				return **(**int32)(__ccgo_up(bp))
			}
		}
	}
	/* The request could not be fulfilled using a freelist slot.  Check
	 ** to see if defragmentation is necessary.
	 */
	if gap+int32(2)+nByte > top {
		if int32(4) < (*TMemPage)(unsafe.Pointer(pPage)).FnFree-(int32(2)+nByte) {
			v1 = int32(4)
		} else {
			v1 = (*TMemPage)(unsafe.Pointer(pPage)).FnFree - (int32(2) + nByte)
		}
		**(**int32)(__ccgo_up(bp)) = _defragmentPage(tls, pPage, v1)
		if **(**int32)(__ccgo_up(bp)) != 0 {
			return **(**int32)(__ccgo_up(bp))
		}
		top = (int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(5)))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(5)) + 1)))-int32(1))&int32(0xffff) + int32(1)
	}
	/* Allocate memory from the gap in between the cell pointer array
	 ** and the cell content area.  The btreeComputeFreeSpace() call has already
	 ** validated the freelist.  Given that the freelist is valid, there
	 ** is no way that the allocation can extend off the end of the page.
	 ** The assert() below verifies the previous sentence.
	 */
	top = top - nByte
	**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(5)))) = uint8(top >> libc.Int32FromInt32(8))
	**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(5)) + 1)) = uint8(top)
	**(**int32)(__ccgo_up(pIdx)) = top
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Make sure pBt->pTmpSpace points to an allocation of
//	** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
//	** pointer.
//	*/
func _allocateTempSpace(tls *libc.TLS, pBt uintptr) (r int32) {
	var pCur uintptr
	_ = pCur
	/* This routine is called only by btreeCursor() when allocating the
	 ** first write cursor for the BtShared object */
	(*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace = _sqlite3PageMalloc(tls, int32((*TBtShared)(unsafe.Pointer(pBt)).FpageSize))
	if (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace == uintptr(0) {
		pCur = (*TBtShared)(unsafe.Pointer(pBt)).FpCursor
		(*TBtShared)(unsafe.Pointer(pBt)).FpCursor = (*TBtCursor)(unsafe.Pointer(pCur)).FpNext /* Unlink the cursor */
		libc.Xmemset(tls, pCur, 0, uint32(200))
		return int32(SQLITE_NOMEM)
	}
	/* One of the uses of pBt->pTmpSpace is to format cells before
	 ** inserting them into a leaf page (function fillInCell()). If
	 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
	 ** by the various routines that manipulate binary cells. Which
	 ** can mean that fillInCell() only initializes the first 2 or 3
	 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
	 ** it into a database page. This is not actually a problem, but it
	 ** does cause a valgrind error when the 1 or 2 bytes of uninitialized
	 ** data is passed to system call write(). So to avoid this error,
	 ** zero the first 4 bytes of temp space here.
	 **
	 ** Also:  Provide four bytes of initialized space before the
	 ** beginning of pTmpSpace as an area available to prepend the
	 ** left-child pointer to the beginning of a cell.
	 */
	libc.Xmemset(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace, 0, uint32(8))
	**(**uintptr)(__ccgo_up(pBt + 84)) += uintptr(4)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Find a column named pCol in table pTab. If successful, set output
//	** parameter *piCol to the index of the column in the table and return
//	** SQLITE_OK. Otherwise, set *piCol to -1 and return an SQLite error
//	** code.
//	*/
func _alterFindCol(tls *libc.TLS, pParse uintptr, pTab uintptr, pCol uintptr, piCol uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, zCol, zDb, zName uintptr
	var iCol, rc int32
	_, _, _, _, _, _ = db, iCol, rc, zCol, zDb, zName
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	zName = _sqlite3NameFromToken(tls, db, pCol)
	rc = int32(SQLITE_NOMEM)
	iCol = -int32(1)
	if zName != 0 {
		iCol = _sqlite3ColumnIndex(tls, pTab, zName)
		if iCol < 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13123, libc.VaList(bp+8, zName))
			rc = int32(SQLITE_ERROR)
		} else {
			rc = SQLITE_OK
		}
	}
	if rc == SQLITE_OK {
		zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(_sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema))*16))).FzDbSName
		zCol = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName
		if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ALTER_TABLE), zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, zCol) != 0 {
			pTab = uintptr(0)
		}
	}
	_sqlite3DbFree(tls, db, zName)
	**(**int32)(__ccgo_up(piCol)) = iCol
	return rc
}

// C documentation
//
//	/*
//	** This callback is invoked once for each index when reading the
//	** sqlite_stat1 table.
//	**
//	**     argv[0] = name of the table
//	**     argv[1] = name of the index (might be NULL)
//	**     argv[2] = results of analysis - on integer for each column
//	**
//	** Entries for which argv[1]==NULL simply record the number of rows in
//	** the table.
//	*/
func _analysisLoader(tls *libc.TLS, pData uintptr, argc int32, argv uintptr, NotUsed uintptr) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var aiRowEst, pIndex, pInfo, pTable, z uintptr
	var nCol int32
	var _ /* fakeIdx at bp+0 */ TIndex
	_, _, _, _, _, _ = aiRowEst, nCol, pIndex, pInfo, pTable, z
	pInfo = pData
	_ = NotUsed
	_ = argc
	if argv == uintptr(0) || **(**uintptr)(__ccgo_up(argv)) == uintptr(0) || **(**uintptr)(__ccgo_up(argv + 2*4)) == uintptr(0) {
		return 0
	}
	pTable = _sqlite3FindTable(tls, (*TanalysisInfo)(unsafe.Pointer(pInfo)).Fdb, **(**uintptr)(__ccgo_up(argv)), (*TanalysisInfo)(unsafe.Pointer(pInfo)).FzDatabase)
	if pTable == uintptr(0) {
		return 0
	}
	if **(**uintptr)(__ccgo_up(argv + 1*4)) == uintptr(0) {
		pIndex = uintptr(0)
	} else {
		if Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(argv)), **(**uintptr)(__ccgo_up(argv + 1*4))) == 0 {
			pIndex = _sqlite3PrimaryKeyIndex(tls, pTable)
		} else {
			pIndex = _sqlite3FindIndex(tls, (*TanalysisInfo)(unsafe.Pointer(pInfo)).Fdb, **(**uintptr)(__ccgo_up(argv + 1*4)), (*TanalysisInfo)(unsafe.Pointer(pInfo)).FzDatabase)
		}
	}
	z = **(**uintptr)(__ccgo_up(argv + 2*4))
	if pIndex != 0 {
		aiRowEst = uintptr(0)
		nCol = int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol) + int32(1)
		/* Index.aiRowEst may already be set here if there are duplicate
		 ** sqlite_stat1 entries for this index. In that case just clobber
		 ** the old data with the new instead of allocating a new array.  */
		if (*TIndex)(unsafe.Pointer(pIndex)).FaiRowEst == uintptr(0) {
			(*TIndex)(unsafe.Pointer(pIndex)).FaiRowEst = _sqlite3MallocZero(tls, uint64(uint32(8)*uint32(nCol)))
			if (*TIndex)(unsafe.Pointer(pIndex)).FaiRowEst == uintptr(0) {
				_sqlite3OomFault(tls, (*TanalysisInfo)(unsafe.Pointer(pInfo)).Fdb)
			}
		}
		aiRowEst = (*TIndex)(unsafe.Pointer(pIndex)).FaiRowEst
		libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 2, 0x4)
		_decodeIntArray(tls, z, nCol, aiRowEst, (*TIndex)(unsafe.Pointer(pIndex)).FaiRowLogEst, pIndex)
		libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 7, 0x80)
		if (*TIndex)(unsafe.Pointer(pIndex)).FpPartIdxWhere == uintptr(0) {
			(*TTable)(unsafe.Pointer(pTable)).FnRowLogEst = **(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaiRowLogEst))
			**(**Tu32)(__ccgo_up(pTable + 28)) |= uint32(TF_HasStat1)
		}
	} else {
		(**(**TIndex)(__ccgo_up(bp))).FszIdxRow = (*TTable)(unsafe.Pointer(pTable)).FszTabRow
		_decodeIntArray(tls, z, int32(1), uintptr(0), pTable+38, bp)
		(*TTable)(unsafe.Pointer(pTable)).FszTabRow = (**(**TIndex)(__ccgo_up(bp))).FszIdxRow
		**(**Tu32)(__ccgo_up(pTable + 28)) |= uint32(TF_HasStat1)
	}
	return 0
}

// C documentation
//
//	/*
//	** Analyze the arguments to aggregate functions.  Create new pAggInfo->aCol[]
//	** entries for columns that are arguments to aggregate functions but which
//	** are not otherwise used.
//	**
//	** The aCol[] entries in AggInfo prior to nAccumulator are columns that
//	** are referenced outside of aggregate functions.  These might be columns
//	** that are part of the GROUP by clause, for example.  Other database engines
//	** would throw an error if there is a column reference that is not in the
//	** GROUP BY clause and that is not part of an aggregate function argument.
//	** But SQLite allows this.
//	**
//	** The aCol[] entries beginning with the aCol[nAccumulator] and following
//	** are column references that are used exclusively as arguments to
//	** aggregate functions.  This routine is responsible for computing
//	** (or recomputing) those aCol[] entries.
//	*/
func _analyzeAggFuncArgs(tls *libc.TLS, pAggInfo uintptr, pNC uintptr) {
	var i int32
	var pExpr uintptr
	_, _ = i, pExpr
	**(**int32)(__ccgo_up(pNC + 24)) |= int32(NC_InAggFunc)
	i = 0
	for {
		if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) {
			break
		}
		pExpr = (**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*24))).FpFExpr
		_sqlite3ExprAnalyzeAggList(tls, pNC, *(*uintptr)(unsafe.Pointer(pExpr + 20)))
		if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 {
			_sqlite3ExprAnalyzeAggList(tls, pNC, *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20)))
		}
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
			_sqlite3ExprAnalyzeAggregates(tls, pNC, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FpFilter)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	**(**int32)(__ccgo_up(pNC + 24)) &= ^libc.Int32FromInt32(NC_InAggFunc)
}

// C documentation
//
//	/*
//	** This is the xExprCallback for a tree walker.  It is used to
//	** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
//	** for additional information.
//	*/
func _analyzeAggregate(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var enc Tu8
	var i, iDataCur, mxTerm, nArg, v5 int32
	var pAggInfo, pIEpr, pItem, pItem1, pNC, pOBList, pParse, pSrcList, v7 uintptr
	var _ /* tmp at bp+0 */ TExpr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = enc, i, iDataCur, mxTerm, nArg, pAggInfo, pIEpr, pItem, pItem1, pNC, pOBList, pParse, pSrcList, v5, v7
	pNC = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	pParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse
	pSrcList = (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList
	pAggInfo = *(*uintptr)(unsafe.Pointer(pNC + 8))
	switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) {
	default:
		if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_InAggFunc) == 0 {
			break
		}
		if (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr == uintptr(0) {
			break
		}
		pIEpr = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr
		for {
			if !(pIEpr != 0) {
				break
			}
			iDataCur = (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FiDataCur
			if iDataCur < 0 {
				goto _1
			}
			if _sqlite3ExprCompare(tls, uintptr(0), pExpr, (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FpExpr, iDataCur) == 0 {
				break
			}
			goto _1
		_1:
			;
			pIEpr = (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FpIENext
		}
		if pIEpr == uintptr(0) {
			break
		}
		if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == libc.Uint32FromInt32(0)) {
			break
		}
		i = 0
		for {
			if !(i < (*TSrcList)(unsafe.Pointer(pSrcList)).FnSrc) {
				break
			}
			if (*(*TSrcItem)(unsafe.Pointer(pSrcList + 8 + uintptr(i)*48))).FiCursor == (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FiDataCur {
				break
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		if i >= (*TSrcList)(unsafe.Pointer(pSrcList)).FnSrc {
			break
		}
		if (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo != uintptr(0) {
			break
		} /* Resolved by outer context */
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			return int32(WRC_Abort)
		}
		/* If we reach this point, it means that expression pExpr can be
		 ** translated into a reference to an index column as described by
		 ** pIEpr.
		 */
		libc.Xmemset(tls, bp, 0, uint32(52))
		(**(**TExpr)(__ccgo_up(bp))).Fop = uint8(TK_AGG_COLUMN)
		(**(**TExpr)(__ccgo_up(bp))).FiTable = (*TIndexedExpr)(unsafe.Pointer(pIEpr)).FiIdxCur
		(**(**TExpr)(__ccgo_up(bp))).FiColumn = int16((*TIndexedExpr)(unsafe.Pointer(pIEpr)).FiIdxCol)
		_findOrCreateAggInfoColumn(tls, pParse, pAggInfo, bp)
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			return int32(WRC_Abort)
		}
		(**(**TAggInfo_col)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr((**(**TExpr)(__ccgo_up(bp))).FiAgg)*20))).FpCExpr = pExpr
		(*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo = pAggInfo
		(*TExpr)(unsafe.Pointer(pExpr)).FiAgg = (**(**TExpr)(__ccgo_up(bp))).FiAgg
		return int32(WRC_Prune)
	case int32(TK_IF_NULL_ROW):
		fallthrough
	case int32(TK_AGG_COLUMN):
		fallthrough
	case int32(TK_COLUMN):
		/* Check to see if the column is in one of the tables in the FROM
		 ** clause of the aggregate query */
		if pSrcList != uintptr(0) {
			pItem = pSrcList + 8
			i = 0
			for {
				if !(i < (*TSrcList)(unsafe.Pointer(pSrcList)).FnSrc) {
					break
				}
				if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor {
					_findOrCreateAggInfoColumn(tls, pParse, pAggInfo, pExpr)
					break
				} /* endif pExpr->iTable==pItem->iCursor */
				goto _3
			_3:
				;
				i = i + 1
				pItem += 48
			} /* end loop over pSrcList */
		}
		return WRC_Continue
	case int32(TK_AGG_FUNCTION):
		if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_InAggFunc) == 0 && (*TWalker)(unsafe.Pointer(pWalker)).FwalkerDepth == int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) && (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo == uintptr(0) {
			/* Check to see if pExpr is a duplicate of another aggregate
			 ** function that is already in the pAggInfo structure
			 */
			pItem1 = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc
			mxTerm = **(**int32)(__ccgo_up((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 2*4))
			i = 0
			for {
				if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) {
					break
				}
				if (*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFExpr == pExpr {
					break
				}
				if _sqlite3ExprCompare(tls, uintptr(0), (*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFExpr, pExpr, -int32(1)) == 0 {
					break
				}
				goto _4
			_4:
				;
				i = i + 1
				pItem1 += 24
			}
			if i > mxTerm {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9831, libc.VaList(bp+64, mxTerm))
				i = mxTerm
			} else {
				if i >= (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc {
					/* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
					 */
					enc = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fenc
					i = _addAggInfoFunc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pAggInfo)
					if i >= 0 {
						pItem1 = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*24
						(*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFExpr = pExpr
						if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0 {
							v5 = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr
						} else {
							v5 = 0
						}
						nArg = v5
						(*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFunc = _sqlite3FindFunction(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(pExpr + 8)), nArg, enc, uint8(0))
						if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 && (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) == uint32(0) {
							v7 = pParse + 44
							v5 = *(*int32)(unsafe.Pointer(v7))
							*(*int32)(unsafe.Pointer(v7)) = *(*int32)(unsafe.Pointer(v7)) + 1
							(*TAggInfo_func)(unsafe.Pointer(pItem1)).FiOBTab = v5
							pOBList = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20))
							if (*TExprList)(unsafe.Pointer(pOBList)).FnExpr == int32(1) && nArg == int32(1) && _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pOBList + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr, 0) == 0 {
								(*TAggInfo_func)(unsafe.Pointer(pItem1)).FbOBPayload = uint8(0)
								(*TAggInfo_func)(unsafe.Pointer(pItem1)).FbOBUnique = libc.BoolUint8((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)) != libc.Uint32FromInt32(0))
							} else {
								(*TAggInfo_func)(unsafe.Pointer(pItem1)).FbOBPayload = uint8(1)
							}
							(*TAggInfo_func)(unsafe.Pointer(pItem1)).FbUseSubtype = libc.BoolUint8((*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFunc)).FfuncFlags&uint32(SQLITE_SUBTYPE) != uint32(0))
						} else {
							(*TAggInfo_func)(unsafe.Pointer(pItem1)).FiOBTab = -int32(1)
						}
						if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)) != uint32(0) && !((*TAggInfo_func)(unsafe.Pointer(pItem1)).FbOBUnique != 0) {
							v7 = pParse + 44
							v5 = *(*int32)(unsafe.Pointer(v7))
							*(*int32)(unsafe.Pointer(v7)) = *(*int32)(unsafe.Pointer(v7)) + 1
							(*TAggInfo_func)(unsafe.Pointer(pItem1)).FiDistinct = v5
						} else {
							(*TAggInfo_func)(unsafe.Pointer(pItem1)).FiDistinct = -int32(1)
						}
					}
				}
			}
			/* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
			 */
			(*TExpr)(unsafe.Pointer(pExpr)).FiAgg = int16(i)
			(*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo = pAggInfo
			return int32(WRC_Prune)
		} else {
			return WRC_Continue
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Generate code to do an analysis of all indices associated with
//	** a single table.
//	*/
func _analyzeOneTable(tls *libc.TLS, pParse uintptr, pTab uintptr, pOnlyIdx uintptr, iStatCur int32, iMem int32, iTab int32) {
	var aGotoChng, db, pColl, pIdx, pPk, pStat1, pX, v, zIdxName uintptr
	var addrGotoEnd, addrIsNull, addrNext, addrNextRow, doOnce, endDistinctTest, i, iDb, iIdxCur, iTabCur, j, j1, j2, j3, jZeroRows, k, mxCol, nCol, nColTest, nColX, regChng, regCol, regDLt, regEq, regIdxname, regKey, regLt, regNewRowid, regPrev, regRowid, regSample, regSampleRowid, regStat, regStat1, regTabname, regTemp, regTemp2, v1, v2, v3, v4, v5, v6, v7, v8, v9 int32
	var needTableCnt, seekOp Tu8
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aGotoChng, addrGotoEnd, addrIsNull, addrNext, addrNextRow, db, doOnce, endDistinctTest, i, iDb, iIdxCur, iTabCur, j, j1, j2, j3, jZeroRows, k, mxCol, nCol, nColTest, nColX, needTableCnt, pColl, pIdx, pPk, pStat1, pX, regChng, regCol, regDLt, regEq, regIdxname, regKey, regLt, regNewRowid, regPrev, regRowid, regSample, regSampleRowid, regStat, regStat1, regTabname, regTemp, regTemp2, seekOp, v, zIdxName, v1, v2, v3, v4, v5, v6, v7, v8, v9
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Loop counter */
	jZeroRows = -int32(1)                      /* Index of database containing pTab */
	needTableCnt = uint8(1)
	v1 = iMem
	iMem = iMem + 1 /* True to count the table */
	regNewRowid = v1
	v2 = iMem
	iMem = iMem + 1 /* Rowid for the inserted record */
	regStat = v2
	v3 = iMem
	iMem = iMem + 1 /* Register to hold StatAccum object */
	regChng = v3
	v4 = iMem
	iMem = iMem + 1 /* Index of changed index field */
	regRowid = v4
	v5 = iMem
	iMem = iMem + 1 /* Rowid argument passed to stat_push() */
	regTemp = v5
	v6 = iMem
	iMem = iMem + 1 /* Temporary use register */
	regTemp2 = v6
	v7 = iMem
	iMem = iMem + 1 /* Second temporary use register */
	regTabname = v7
	v8 = iMem
	iMem = iMem + 1 /* Register containing table name */
	regIdxname = v8
	v9 = iMem
	iMem = iMem + 1   /* Register containing index name */
	regStat1 = v9     /* Value for the stat column of sqlite_stat1 */
	regPrev = iMem    /* MUST BE LAST (see below) */
	doOnce = int32(1) /* Flag for a one-time computation */
	pStat1 = uintptr(0)
	_sqlite3TouchRegister(tls, pParse, iMem)
	v = _sqlite3GetVdbe(tls, pParse)
	if v == uintptr(0) || pTab == uintptr(0) {
		return
	}
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
		/* Do not gather statistics on views or virtual tables */
		return
	}
	if Xsqlite3_strlike(tls, __ccgo_ts+14247, (*TTable)(unsafe.Pointer(pTab)).FzName, uint32('\\')) == 0 {
		/* Do not gather statistics on system tables */
		return
	}
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ANALYZE), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) != 0 {
		return
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 {
		pStat1 = _sqlite3DbMallocZero(tls, db, uint64(libc.Uint32FromInt64(80)+libc.Uint32FromInt32(13)))
		if pStat1 == uintptr(0) {
			return
		}
		(*TTable)(unsafe.Pointer(pStat1)).FzName = pStat1 + 1*80
		libc.Xmemcpy(tls, (*TTable)(unsafe.Pointer(pStat1)).FzName, __ccgo_ts+14050, uint32(13))
		(*TTable)(unsafe.Pointer(pStat1)).FnCol = int16(3)
		(*TTable)(unsafe.Pointer(pStat1)).FiPKey = int16(-int32(1))
		_sqlite3VdbeAddOp4(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Noop), 0, 0, 0, pStat1, -int32(7))
	}
	/* Establish a read-lock on the table at the shared-cache level.
	 ** Open a read-only cursor on the table. Also allocate a cursor number
	 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
	 ** this time though.  */
	_sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab)).FzName)
	v1 = iTab
	iTab = iTab + 1
	iTabCur = v1
	v1 = iTab
	iTab = iTab + 1
	iIdxCur = v1
	if (*TParse)(unsafe.Pointer(pParse)).FnTab > iTab {
		v1 = (*TParse)(unsafe.Pointer(pParse)).FnTab
	} else {
		v1 = iTab
	}
	(*TParse)(unsafe.Pointer(pParse)).FnTab = v1
	_sqlite3OpenTable(tls, pParse, iTabCur, iDb, pTab, int32(OP_OpenRead))
	_sqlite3VdbeLoadString(tls, v, regTabname, (*TTable)(unsafe.Pointer(pTab)).FzName)
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		} /* Number of columns to test for changes */
		if pOnlyIdx != 0 && pOnlyIdx != pIdx {
			goto _13
		}
		if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere == uintptr(0) {
			needTableCnt = uint8(0)
		}
		if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
			nCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
			zIdxName = (*TTable)(unsafe.Pointer(pTab)).FzName
			nColTest = nCol - int32(1)
		} else {
			nCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
			zIdxName = (*TIndex)(unsafe.Pointer(pIdx)).FzName
			if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 {
				v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) - int32(1)
			} else {
				v1 = nCol - int32(1)
			}
			nColTest = v1
		}
		/* Populate the register containing the index name. */
		_sqlite3VdbeLoadString(tls, v, regIdxname, zIdxName)
		/*
		 ** Pseudo-code for loop that calls stat_push():
		 **
		 **   regChng = 0
		 **   Rewind csr
		 **   if eof(csr){
		 **      stat_init() with count = 0;
		 **      goto end_of_scan;
		 **   }
		 **   count()
		 **   stat_init()
		 **   goto chng_addr_0;
		 **
		 **  next_row:
		 **   regChng = 0
		 **   if( idx(0) != regPrev(0) ) goto chng_addr_0
		 **   regChng = 1
		 **   if( idx(1) != regPrev(1) ) goto chng_addr_1
		 **   ...
		 **   regChng = N
		 **   goto chng_addr_N
		 **
		 **  chng_addr_0:
		 **   regPrev(0) = idx(0)
		 **  chng_addr_1:
		 **   regPrev(1) = idx(1)
		 **  ...
		 **
		 **  endDistinctTest:
		 **   regRowid = idx(rowid)
		 **   stat_push(P, regChng, regRowid)
		 **   Next csr
		 **   if !eof(csr) goto next_row;
		 **
		 **  end_of_scan:
		 */
		/* Make sure there are enough memory cells allocated to accommodate
		 ** the regPrev array and a trailing rowid (the rowid slot is required
		 ** when building a record to insert into the sample column of
		 ** the sqlite_stat4 table.  */
		_sqlite3TouchRegister(tls, pParse, regPrev+nColTest)
		/* Open a read-only cursor on the index being analyzed. */
		_sqlite3VdbeAddOp3(tls, v, int32(OP_OpenRead), iIdxCur, int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb)
		_sqlite3VdbeSetP4KeyInfo(tls, pParse, pIdx)
		/* Implementation of the following:
		 **
		 **   regChng = 0
		 **   Rewind csr
		 **   if eof(csr){
		 **      stat_init() with count = 0;
		 **      goto end_of_scan;
		 **   }
		 **   count()
		 **   stat_init()
		 **   goto chng_addr_0;
		 */
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit, regTemp2)
		/* Arguments to stat_init():
		 **    (1) the number of columns in the index including the rowid
		 **        (or for a WITHOUT ROWID table, the number of PK columns),
		 **    (2) the number of columns in the key without the rowid/pk
		 **    (3) estimated number of rows in the index. */
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), nCol, regStat+int32(1))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol), regRowid)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Count), iIdxCur, regTemp, libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) != uint32(0)))
		_sqlite3VdbeAddFunctionCall(tls, pParse, 0, regStat+int32(1), regStat, int32(4), uintptr(unsafe.Pointer(&_statInitFuncdef)), 0)
		addrGotoEnd = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iIdxCur)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regChng)
		addrNextRow = _sqlite3VdbeCurrentAddr(tls, v)
		if nColTest > 0 {
			endDistinctTest = _sqlite3VdbeMakeLabel(tls, pParse) /* Array of jump instruction addresses */
			aGotoChng = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(nColTest)))
			if aGotoChng == uintptr(0) {
				goto _13
			}
			/*
			 **  next_row:
			 **   regChng = 0
			 **   if( idx(0) != regPrev(0) ) goto chng_addr_0
			 **   regChng = 1
			 **   if( idx(1) != regPrev(1) ) goto chng_addr_1
			 **   ...
			 **   regChng = N
			 **   goto endDistinctTest
			 */
			_sqlite3VdbeAddOp0(tls, v, int32(OP_Goto))
			addrNextRow = _sqlite3VdbeCurrentAddr(tls, v)
			if nColTest == int32(1) && int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) == int32(1) && int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != OE_None {
				/* For a single-column UNIQUE index, once we have found a non-NULL
				 ** row, we know that all the rest will be distinct, so skip
				 ** subsequent distinctness tests. */
				_sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), regPrev, endDistinctTest)
			}
			i = 0
			for {
				if !(i < nColTest) {
					break
				}
				pColl = _sqlite3LocateCollSeq(tls, pParse, **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i)*4)))
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), i, regChng)
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, i, regTemp)
				**(**int32)(__ccgo_up(aGotoChng + uintptr(i)*4)) = _sqlite3VdbeAddOp4(tls, v, int32(OP_Ne), regTemp, 0, regPrev+i, pColl, -int32(2))
				_sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NULLEQ))
				goto _15
			_15:
				;
				i = i + 1
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), nColTest, regChng)
			_sqlite3VdbeGoto(tls, v, endDistinctTest)
			/*
			 **  chng_addr_0:
			 **   regPrev(0) = idx(0)
			 **  chng_addr_1:
			 **   regPrev(1) = idx(1)
			 **  ...
			 */
			_sqlite3VdbeJumpHere(tls, v, addrNextRow-int32(1))
			i = 0
			for {
				if !(i < nColTest) {
					break
				}
				_sqlite3VdbeJumpHere(tls, v, **(**int32)(__ccgo_up(aGotoChng + uintptr(i)*4)))
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, i, regPrev+i)
				goto _16
			_16:
				;
				i = i + 1
			}
			_sqlite3VdbeResolveLabel(tls, v, endDistinctTest)
			_sqlite3DbFree(tls, db, aGotoChng)
		}
		/*
		 **  chng_addr_N:
		 **   regRowid = idx(rowid)            // STAT4 only
		 **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT4 only
		 **   Next csr
		 **   if !eof(csr) goto next_row;
		 */
		if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) {
			if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxRowid), iIdxCur, regRowid)
			} else {
				pPk = _sqlite3PrimaryKeyIndex(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpTable)
				regKey = _sqlite3GetTempRange(tls, pParse, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol))
				j = 0
				for {
					if !(j < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
						break
					}
					k = _sqlite3TableColumnToIndex(tls, pIdx, int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(j)*2))))
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, k, regKey+j)
					goto _17
				_17:
					;
					j = j + 1
				}
				_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regKey, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol), regRowid)
				_sqlite3ReleaseTempRange(tls, pParse, regKey, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol))
			}
		}
		_sqlite3VdbeAddFunctionCall(tls, pParse, int32(1), regStat, regTemp, libc.Int32FromInt32(2)+libc.Int32FromInt32(IsStat4), uintptr(unsafe.Pointer(&_statPushFuncdef)), 0)
		if (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit != 0 {
			j1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), regTemp)
			j2 = _sqlite3VdbeAddOp1(tls, v, int32(OP_If), regTemp)
			j3 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_SeekGT), iIdxCur, 0, regPrev, int32(1))
			_sqlite3VdbeJumpHere(tls, v, j1)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iIdxCur, addrNextRow)
			_sqlite3VdbeJumpHere(tls, v, j2)
			_sqlite3VdbeJumpHere(tls, v, j3)
		} else {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iIdxCur, addrNextRow)
		}
		/* Add the entry to the stat1 table. */
		if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 {
			/* Partial indexes might get a zero-entry in sqlite_stat1.  But
			 ** an empty table is omitted from sqlite_stat1. */
			_sqlite3VdbeJumpHere(tls, v, addrGotoEnd)
			addrGotoEnd = 0
		}
		_callStatGet(tls, pParse, regStat, STAT_GET_STAT1, regStat1)
		_sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regTabname, int32(3), regTemp, __ccgo_ts+14257, 0)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iStatCur, regNewRowid)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iStatCur, regTemp, regNewRowid)
		_sqlite3VdbeChangeP4(tls, v, -int32(1), pStat1, -int32(5))
		_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND))
		/* Add the entries to the stat4 table. */
		if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) && (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit == 0 {
			regEq = regStat1
			regLt = regStat1 + int32(1)
			regDLt = regStat1 + int32(2)
			regSample = regStat1 + int32(3)
			regCol = regStat1 + int32(4)
			regSampleRowid = regCol + nCol
			if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
				v1 = int32(OP_NotExists)
			} else {
				v1 = int32(OP_NotFound)
			}
			seekOp = uint8(v1)
			/* No STAT4 data is generated if the number of rows is zero */
			if addrGotoEnd == 0 {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Cast), regStat1, int32(SQLITE_AFF_INTEGER))
				addrGotoEnd = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), regStat1)
			}
			if doOnce != 0 {
				mxCol = nCol
				/* Compute the maximum number of columns in any index */
				pX = (*TTable)(unsafe.Pointer(pTab)).FpIndex
				for {
					if !(pX != 0) {
						break
					} /* Number of columns in pX */
					if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pX + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
						nColX = int32((*TIndex)(unsafe.Pointer(pX)).FnKeyCol)
					} else {
						nColX = int32((*TIndex)(unsafe.Pointer(pX)).FnColumn)
					}
					if nColX > mxCol {
						mxCol = nColX
					}
					goto _19
				_19:
					;
					pX = (*TIndex)(unsafe.Pointer(pX)).FpNext
				}
				/* Allocate space to compute results for the largest index */
				_sqlite3TouchRegister(tls, pParse, regCol+mxCol)
				doOnce = 0
				_sqlite3ClearTempRegCache(tls, pParse) /* tag-20230325-1 */
			}
			addrNext = _sqlite3VdbeCurrentAddr(tls, v)
			_callStatGet(tls, pParse, regStat, int32(STAT_GET_ROWID), regSampleRowid)
			addrIsNull = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), regSampleRowid)
			_callStatGet(tls, pParse, regStat, int32(STAT_GET_NEQ), regEq)
			_callStatGet(tls, pParse, regStat, int32(STAT_GET_NLT), regLt)
			_callStatGet(tls, pParse, regStat, int32(STAT_GET_NDLT), regDLt)
			_sqlite3VdbeAddOp4Int(tls, v, int32(seekOp), iTabCur, addrNext, regSampleRowid, 0)
			i = 0
			for {
				if !(i < nCol) {
					break
				}
				_sqlite3ExprCodeLoadIndexColumn(tls, pParse, pIdx, iTabCur, i, regCol+i)
				goto _20
			_20:
				;
				i = i + 1
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regCol, nCol, regSample)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regTabname, int32(6), regTemp)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iStatCur+int32(1), regNewRowid)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iStatCur+int32(1), regTemp, regNewRowid)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), int32(1), addrNext) /* P1==1 for end-of-loop */
			_sqlite3VdbeJumpHere(tls, v, addrIsNull)
		}
		/* End of analysis */
		if addrGotoEnd != 0 {
			_sqlite3VdbeJumpHere(tls, v, addrGotoEnd)
		}
		goto _13
	_13:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
	}
	/* Create a single sqlite_stat1 entry containing NULL as the index
	 ** name and the row count as the content.
	 */
	if pOnlyIdx == uintptr(0) && needTableCnt != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Count), iTabCur, regStat1)
		jZeroRows = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), regStat1)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regIdxname)
		_sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regTabname, int32(3), regTemp, __ccgo_ts+14257, 0)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iStatCur, regNewRowid)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iStatCur, regTemp, regNewRowid)
		_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND))
		_sqlite3VdbeChangeP4(tls, v, -int32(1), pStat1, -int32(5))
		_sqlite3VdbeJumpHere(tls, v, jZeroRows)
	}
}

// C documentation
//
//	/*
//	** Generate code that will do an analysis of a single table in
//	** a database.  If pOnlyIdx is not NULL then it is a single index
//	** in pTab that should be analyzed.
//	*/
func _analyzeTable(tls *libc.TLS, pParse uintptr, pTab uintptr, pOnlyIdx uintptr) {
	var iDb, iStatCur int32
	_, _ = iDb, iStatCur
	iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	_sqlite3BeginWriteOperation(tls, pParse, 0, iDb)
	iStatCur = (*TParse)(unsafe.Pointer(pParse)).FnTab
	**(**int32)(__ccgo_up(pParse + 44)) += int32(3)
	if pOnlyIdx != 0 {
		_openStatTable(tls, pParse, iDb, iStatCur, (*TIndex)(unsafe.Pointer(pOnlyIdx)).FzName, __ccgo_ts+14261)
	} else {
		_openStatTable(tls, pParse, iDb, iStatCur, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+14265)
	}
	_analyzeOneTable(tls, pParse, pTab, pOnlyIdx, iStatCur, (*TParse)(unsafe.Pointer(pParse)).FnMem+int32(1), (*TParse)(unsafe.Pointer(pParse)).FnTab)
	_loadAnalysis(tls, pParse, iDb)
}

// C documentation
//
//	/*
//	** Processing is determine by the affinity parameter:
//	**
//	** SQLITE_AFF_INTEGER:
//	** SQLITE_AFF_REAL:
//	** SQLITE_AFF_NUMERIC:
//	**    Try to convert pRec to an integer representation or a
//	**    floating-point representation if an integer representation
//	**    is not possible.  Note that the integer representation is
//	**    always preferred, even if the affinity is REAL, because
//	**    an integer representation is more space efficient on disk.
//	**
//	** SQLITE_AFF_FLEXNUM:
//	**    If the value is text, then try to convert it into a number of
//	**    some kind (integer or real) but do not make any other changes.
//	**
//	** SQLITE_AFF_TEXT:
//	**    Convert pRec to a text representation.
//	**
//	** SQLITE_AFF_BLOB:
//	** SQLITE_AFF_NONE:
//	**    No-op.  pRec is unchanged.
//	*/
func _applyAffinity(tls *libc.TLS, pRec uintptr, affinity int8, enc Tu8) {
	var v1 uintptr
	_ = v1
	if int32(affinity) >= int32(SQLITE_AFF_NUMERIC) {
		if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Int) == 0 { /*OPTIMIZATION-IF-FALSE*/
			if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&(libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)) == 0 {
				if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Str) != 0 {
					_applyNumericAffinity(tls, pRec, int32(1))
				}
			} else {
				if int32(affinity) <= int32(SQLITE_AFF_REAL) {
					_sqlite3VdbeIntegerAffinity(tls, pRec)
				}
			}
		}
	} else {
		if int32(affinity) == int32(SQLITE_AFF_TEXT) {
			/* Only attempt the conversion to TEXT if there is an integer or real
			 ** representation (blob and NULL do not get converted) but no string
			 ** representation.  It would be harmless to repeat the conversion if
			 ** there is already a string rep, but it is pointless to waste those
			 ** CPU cycles. */
			if 0 == int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Str) { /*OPTIMIZATION-IF-FALSE*/
				if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&(libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
					_sqlite3VdbeMemStringify(tls, pRec, enc, uint8(1))
				}
			}
			v1 = pRec + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_IntReal)))
		}
	}
}

// C documentation
//
//	/*
//	** An SQL user-function registered to do the work of an ATTACH statement. The
//	** three arguments to the function come directly from an attach statement:
//	**
//	**     ATTACH DATABASE x AS y KEY z
//	**
//	**     SELECT sqlite_attach(x, y, z)
//	**
//	** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
//	** third argument.
//	**
//	** If the db->init.reopenMemdb flags is set, then instead of attaching a
//	** new database, close the database on db->init.iDb and reopen it as an
//	** empty MemDB.
//	*/
func _attachFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var aNew, db, pNew, pNewSchema, pPager, zFile, zName uintptr
	var i, iDb, rc int32
	var _ /* flags at bp+8 */ uint32
	var _ /* pNewBt at bp+20 */ uintptr
	var _ /* pVfs at bp+16 */ uintptr
	var _ /* zErr at bp+4 */ uintptr
	var _ /* zErrDyn at bp+12 */ uintptr
	var _ /* zPath at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _ = aNew, db, i, iDb, pNew, pNewSchema, pPager, rc, zFile, zName
	rc = 0
	db = Xsqlite3_context_db_handle(tls, context)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* New array of Db pointers */
	pNew = uintptr(0)                             /* Db object for the newly attached database */
	**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
	_ = NotUsed
	zFile = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	zName = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	if zFile == uintptr(0) {
		zFile = __ccgo_ts + 1711
	}
	if zName == uintptr(0) {
		zName = __ccgo_ts + 1711
	}
	if int32(uint32(*(*uint8)(unsafe.Pointer(db + 176 + 8))&0x8>>3)) != 0 {
		/* This is not a real ATTACH.  Instead, this routine is being called
		 ** from sqlite3_deserialize() to close database db->init.iDb and
		 ** reopen it as a MemDB */
		**(**uintptr)(__ccgo_up(bp + 20)) = uintptr(0)
		pNew = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb)*16
		if _sqlite3BtreeTxnState(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt) != SQLITE_TXN_NONE || _sqlite3BtreeIsInBackup(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt) != 0 {
			rc = int32(SQLITE_BUSY)
			goto attach_error
		}
		**(**uintptr)(__ccgo_up(bp + 16)) = Xsqlite3_vfs_find(tls, __ccgo_ts+5329)
		if **(**uintptr)(__ccgo_up(bp + 16)) == uintptr(0) {
			return
		}
		rc = _sqlite3BtreeOpen(tls, **(**uintptr)(__ccgo_up(bp + 16)), __ccgo_ts+14464, db, bp+20, 0, int32(SQLITE_OPEN_MAIN_DB))
		if rc == SQLITE_OK {
			pNewSchema = _sqlite3SchemaGet(tls, db, **(**uintptr)(__ccgo_up(bp + 20)))
			if pNewSchema != 0 {
				/* Both the Btree and the new Schema were allocated successfully.
				 ** Close the old db and update the aDb[] slot with the new memdb
				 ** values.  */
				_sqlite3BtreeClose(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt)
				(*TDb)(unsafe.Pointer(pNew)).FpBt = **(**uintptr)(__ccgo_up(bp + 20))
				(*TDb)(unsafe.Pointer(pNew)).FpSchema = pNewSchema
			} else {
				_sqlite3BtreeClose(tls, **(**uintptr)(__ccgo_up(bp + 20)))
				rc = int32(SQLITE_NOMEM)
			}
		}
		if rc != 0 {
			goto attach_error
		}
	} else {
		/* This is a real ATTACH
		 **
		 ** Check for the following errors:
		 **
		 **     * Too many attached databases,
		 **     * Transaction currently open
		 **     * Specified database name already being used.
		 */
		if (*Tsqlite3)(unsafe.Pointer(db)).FnDb >= **(**int32)(__ccgo_up(db + 120 + 7*4))+int32(2) {
			**(**uintptr)(__ccgo_up(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+14467, libc.VaList(bp+32, **(**int32)(__ccgo_up(db + 120 + 7*4))))
			goto attach_error
		}
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			if _sqlite3DbIsNamed(tls, db, i, zName) != 0 {
				**(**uintptr)(__ccgo_up(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+14504, libc.VaList(bp+32, zName))
				goto attach_error
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		/* Allocate the new entry in the db->aDb[] array and initialize the schema
		 ** hash tables.
		 */
		if (*Tsqlite3)(unsafe.Pointer(db)).FaDb == db+476 {
			aNew = _sqlite3DbMallocRawNN(tls, db, uint64(libc.Uint32FromInt64(16)*libc.Uint32FromInt32(3)))
			if aNew == uintptr(0) {
				return
			}
			libc.Xmemcpy(tls, aNew, (*Tsqlite3)(unsafe.Pointer(db)).FaDb, libc.Uint32FromInt64(16)*libc.Uint32FromInt32(2))
		} else {
			aNew = _sqlite3DbRealloc(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaDb, uint64(int64(16)*(int64(1)+int64((*Tsqlite3)(unsafe.Pointer(db)).FnDb))))
			if aNew == uintptr(0) {
				return
			}
		}
		(*Tsqlite3)(unsafe.Pointer(db)).FaDb = aNew
		pNew = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*Tsqlite3)(unsafe.Pointer(db)).FnDb)*16
		libc.Xmemset(tls, pNew, 0, uint32(16))
		/* Open the database file. If the btree is successfully opened, use
		 ** it to obtain the database schema. At this point the schema may
		 ** or may not be initialized.
		 */
		**(**uint32)(__ccgo_up(bp + 8)) = (*Tsqlite3)(unsafe.Pointer(db)).FopenFlags
		rc = _sqlite3ParseUri(tls, (*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpVfs)).FzName, zFile, bp+8, bp+16, bp, bp+4)
		if rc != SQLITE_OK {
			if rc == int32(SQLITE_NOMEM) {
				_sqlite3OomFault(tls, db)
			}
			Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp + 4)), -int32(1))
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 4)))
			return
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00020))<<libc.Int32FromInt32(32)) == uint64(0) {
			**(**uint32)(__ccgo_up(bp + 8)) = **(**uint32)(__ccgo_up(bp + 8)) & uint32(^(libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE)))
			**(**uint32)(__ccgo_up(bp + 8)) = **(**uint32)(__ccgo_up(bp + 8)) | uint32(SQLITE_OPEN_READONLY)
		} else {
			if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00010))<<libc.Int32FromInt32(32)) == uint64(0) {
				**(**uint32)(__ccgo_up(bp + 8)) = **(**uint32)(__ccgo_up(bp + 8)) & uint32(^libc.Int32FromInt32(SQLITE_OPEN_CREATE))
			}
		}
		**(**uint32)(__ccgo_up(bp + 8)) = **(**uint32)(__ccgo_up(bp + 8)) | uint32(SQLITE_OPEN_MAIN_DB)
		rc = _sqlite3BtreeOpen(tls, **(**uintptr)(__ccgo_up(bp + 16)), **(**uintptr)(__ccgo_up(bp)), db, pNew+4, 0, int32(**(**uint32)(__ccgo_up(bp + 8))))
		(*Tsqlite3)(unsafe.Pointer(db)).FnDb = (*Tsqlite3)(unsafe.Pointer(db)).FnDb + 1
		(*TDb)(unsafe.Pointer(pNew)).FzDbSName = _sqlite3DbStrDup(tls, db, zName)
	}
	(*Tsqlite3)(unsafe.Pointer(db)).FnoSharedCache = uint8(0)
	if rc == int32(SQLITE_CONSTRAINT) {
		rc = int32(SQLITE_ERROR)
		**(**uintptr)(__ccgo_up(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+14534, 0)
	} else {
		if rc == SQLITE_OK {
			(*TDb)(unsafe.Pointer(pNew)).FpSchema = _sqlite3SchemaGet(tls, db, (*TDb)(unsafe.Pointer(pNew)).FpBt)
			if !((*TDb)(unsafe.Pointer(pNew)).FpSchema != 0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				if (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pNew)).FpSchema)).Ffile_format != 0 && int32((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pNew)).FpSchema)).Fenc) != int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc) {
					**(**uintptr)(__ccgo_up(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+14563, 0)
					rc = int32(SQLITE_ERROR)
				}
			}
			_sqlite3BtreeEnter(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt)
			pPager = _sqlite3BtreePager(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt)
			_sqlite3PagerLockingMode(tls, pPager, int32((*Tsqlite3)(unsafe.Pointer(db)).FdfltLockMode))
			_sqlite3BtreeSecureDelete(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt, _sqlite3BtreeSecureDelete(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpBt, -int32(1)))
			_sqlite3BtreeSetPagerFlags(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt, uint32(uint64(PAGER_SYNCHRONOUS_FULL)|(*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(PAGER_FLAGS_MASK)))
			_sqlite3BtreeLeave(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt)
		}
	}
	(*TDb)(unsafe.Pointer(pNew)).Fsafety_level = uint8(libc.Int32FromInt32(SQLITE_DEFAULT_SYNCHRONOUS) + libc.Int32FromInt32(1))
	if rc == SQLITE_OK && (*TDb)(unsafe.Pointer(pNew)).FzDbSName == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	}
	Xsqlite3_free_filename(tls, **(**uintptr)(__ccgo_up(bp)))
	/* If the file was opened successfully, read the schema for the new database.
	 ** If this fails, or if opening the file failed, then close the file and
	 ** remove the entry from the db->aDb[] array. i.e. put everything back the
	 ** way we found it.
	 */
	if rc == SQLITE_OK {
		_sqlite3BtreeEnterAll(tls, db)
		(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(0)
		**(**Tu32)(__ccgo_up(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaKnownOk))
		if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 176 + 8))&0x8>>3)) != 0) {
			rc = _sqlite3Init(tls, db, bp+12)
		}
		_sqlite3BtreeLeaveAll(tls, db)
	}
	if rc != 0 {
		if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 176 + 8))&0x8>>3)) != 0) {
			iDb = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1)
			if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt != 0 {
				_sqlite3BtreeClose(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt)
				(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt = uintptr(0)
				(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema = uintptr(0)
			}
			_sqlite3ResetAllSchemasOfConnection(tls, db)
			(*Tsqlite3)(unsafe.Pointer(db)).FnDb = iDb
			if rc == int32(SQLITE_NOMEM) || rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<<libc.Int32FromInt32(8) {
				_sqlite3OomFault(tls, db)
				_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp + 12)))
				**(**uintptr)(__ccgo_up(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+1681, 0)
			} else {
				if **(**uintptr)(__ccgo_up(bp + 12)) == uintptr(0) {
					**(**uintptr)(__ccgo_up(bp + 12)) = _sqlite3MPrintf(tls, db, __ccgo_ts+14631, libc.VaList(bp+32, zFile))
				}
			}
		}
		goto attach_error
	}
	return
	goto attach_error
attach_error:
	;
	/* Return an error if we get here */
	if **(**uintptr)(__ccgo_up(bp + 12)) != 0 {
		Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp + 12)), -int32(1))
		_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp + 12)))
	}
	if rc != 0 {
		Xsqlite3_result_error_code(tls, context, rc)
	}
}

// C documentation
//
//	/*
//	** Locate or create an AutoincInfo structure associated with table pTab
//	** which is in database iDb.  Return the register number for the register
//	** that holds the maximum rowid.  Return zero if pTab is not an AUTOINCREMENT
//	** table.  (Also return zero when doing a VACUUM since we do not want to
//	** update the AUTOINCREMENT counters during a VACUUM.)
//	**
//	** There is at most one AutoincInfo structure per table even if the
//	** same table is autoincremented multiple times due to inserts within
//	** triggers.  A new AutoincInfo structure is created if this is the
//	** first use of table pTab.  On 2nd and subsequent uses, the original
//	** AutoincInfo structure is used.
//	**
//	** Four consecutive registers are allocated:
//	**
//	**   (1)  The name of the pTab table.
//	**   (2)  The maximum ROWID of pTab.
//	**   (3)  The rowid in sqlite_sequence of pTab
//	**   (4)  The original value of the max ROWID in pTab, or NULL if none
//	**
//	** The 2nd register is the one that is returned.  That is all the
//	** insert routine needs to know about.
//	*/
func _autoIncBegin(tls *libc.TLS, pParse uintptr, iDb int32, pTab uintptr) (r int32) {
	var memId, v2 int32
	var pInfo, pSeqTab, pToplevel, v1 uintptr
	_, _, _, _, _, _ = memId, pInfo, pSeqTab, pToplevel, v1, v2
	memId = 0 /* Register holding maximum rowid */
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Autoincrement) != uint32(0) && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) {
		if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 {
			v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel
		} else {
			v1 = pParse
		}
		pToplevel = v1
		pSeqTab = (*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FpSchema)).FpSeqTab
		/* Verify that the sqlite_sequence table exists and is an ordinary
		 ** rowid table with exactly two columns.
		 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
		if pSeqTab == uintptr(0) || !((*TTable)(unsafe.Pointer(pSeqTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) || int32((*TTable)(unsafe.Pointer(pSeqTab)).FeTabType) == int32(TABTYP_VTAB) || int32((*TTable)(unsafe.Pointer(pSeqTab)).FnCol) != int32(2) {
			(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
			(*TParse)(unsafe.Pointer(pParse)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
			return 0
		}
		pInfo = (*TParse)(unsafe.Pointer(pToplevel)).FpAinc
		for pInfo != 0 && (*TAutoincInfo)(unsafe.Pointer(pInfo)).FpTab != pTab {
			pInfo = (*TAutoincInfo)(unsafe.Pointer(pInfo)).FpNext
		}
		if pInfo == uintptr(0) {
			pInfo = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(16))
			_sqlite3ParserAddCleanup(tls, pToplevel, __ccgo_fp(_sqlite3DbFree), pInfo)
			if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
				return 0
			}
			(*TAutoincInfo)(unsafe.Pointer(pInfo)).FpNext = (*TParse)(unsafe.Pointer(pToplevel)).FpAinc
			(*TParse)(unsafe.Pointer(pToplevel)).FpAinc = pInfo
			(*TAutoincInfo)(unsafe.Pointer(pInfo)).FpTab = pTab
			(*TAutoincInfo)(unsafe.Pointer(pInfo)).FiDb = iDb
			(*TParse)(unsafe.Pointer(pToplevel)).FnMem = (*TParse)(unsafe.Pointer(pToplevel)).FnMem + 1 /* Register to hold name of table */
			v1 = pToplevel + 48
			*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
			v2 = *(*int32)(unsafe.Pointer(v1))
			(*TAutoincInfo)(unsafe.Pointer(pInfo)).FregCtr = v2 /* Max rowid register */
			**(**int32)(__ccgo_up(pToplevel + 48)) += int32(2)  /* Rowid in sqlite_sequence + orig max val */
		}
		memId = (*TAutoincInfo)(unsafe.Pointer(pInfo)).FregCtr
	}
	return memId
}

// C documentation
//
//	/*
//	** This routine generates the code needed to write autoincrement
//	** maximum rowid values back into the sqlite_sequence register.
//	** Every statement that might do an INSERT into an autoincrement
//	** table (either directly or through triggers) needs to call this
//	** routine just before the "exit" code.
//	*/
func _autoIncrementEnd(tls *libc.TLS, pParse uintptr) {
	var aOp, db, p, pDb, v uintptr
	var iRec, memId int32
	_, _, _, _, _, _, _ = aOp, db, iRec, memId, p, pDb, v
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	p = (*TParse)(unsafe.Pointer(pParse)).FpAinc
	for {
		if !(p != 0) {
			break
		}
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TAutoincInfo)(unsafe.Pointer(p)).FiDb)*16
		memId = (*TAutoincInfo)(unsafe.Pointer(p)).FregCtr
		iRec = _sqlite3GetTempReg(tls, pParse)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Le), memId+int32(2), _sqlite3VdbeCurrentAddr(tls, v)+int32(7), memId)
		_sqlite3OpenTable(tls, pParse, 0, (*TAutoincInfo)(unsafe.Pointer(p)).FiDb, (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).FpSeqTab, int32(OP_OpenWrite))
		aOp = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(20)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_autoIncEnd)), _iLn2)
		if aOp == uintptr(0) {
			break
		}
		(**(**TVdbeOp)(__ccgo_up(aOp))).Fp1 = memId + int32(1)
		(**(**TVdbeOp)(__ccgo_up(aOp + 1*20))).Fp2 = memId + int32(1)
		(**(**TVdbeOp)(__ccgo_up(aOp + 2*20))).Fp1 = memId - int32(1)
		(**(**TVdbeOp)(__ccgo_up(aOp + 2*20))).Fp3 = iRec
		(**(**TVdbeOp)(__ccgo_up(aOp + 3*20))).Fp2 = iRec
		(**(**TVdbeOp)(__ccgo_up(aOp + 3*20))).Fp3 = memId + int32(1)
		(**(**TVdbeOp)(__ccgo_up(aOp + 3*20))).Fp5 = uint16(OPFLAG_APPEND)
		_sqlite3ReleaseTempReg(tls, pParse, iRec)
		goto _1
	_1:
		;
		p = (*TAutoincInfo)(unsafe.Pointer(p)).FpNext
	}
}

// C documentation
//
//	/*
//	** This routine is called prior to sqlite3PagerCommit when a transaction
//	** is committed for an auto-vacuum database.
//	*/
func _autoVacuumCommit(tls *libc.TLS, p uintptr) (r int32) {
	var db, pBt, pPager uintptr
	var iDb, rc int32
	var iFree, nFin, nFree, nOrig, nVac TPgno
	_, _, _, _, _, _, _, _, _, _ = db, iDb, iFree, nFin, nFree, nOrig, nVac, pBt, pPager, rc
	rc = SQLITE_OK
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt
	pPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager
	_invalidateAllOverflowCache(tls, pBt)
	if !((*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum != 0) { /* Database size before freeing */
		nOrig = _btreePagecount(tls, pBt)
		if _ptrmapPageno(tls, pBt, nOrig) == nOrig || nOrig == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) {
			/* It is not possible to create a database for which the final page
			 ** is either a pointer-map page or the pending-byte page. If one
			 ** is encountered, this indicates corruption.
			 */
			return _sqlite3CorruptError(tls, int32(77456))
		}
		nFree = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+36)
		db = (*TBtree)(unsafe.Pointer(p)).Fdb
		if (*Tsqlite3)(unsafe.Pointer(db)).FxAutovacPages != 0 {
			iDb = 0
			for {
				if !(iDb < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
					break
				}
				if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt == p {
					break
				}
				goto _1
			_1:
				;
				iDb = iDb + 1
			}
			nVac = (*(*func(*libc.TLS, uintptr, uintptr, Tu32, Tu32, Tu32) uint32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxAutovacPages})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpAutovacPagesArg, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, nOrig, nFree, (*TBtShared)(unsafe.Pointer(pBt)).FpageSize)
			if nVac > nFree {
				nVac = nFree
			}
			if nVac == uint32(0) {
				return SQLITE_OK
			}
		} else {
			nVac = nFree
		}
		nFin = _finalDbSize(tls, pBt, nOrig, nVac)
		if nFin > nOrig {
			return _sqlite3CorruptError(tls, int32(77483))
		}
		if nFin < nOrig {
			rc = _saveAllCursors(tls, pBt, uint32(0), uintptr(0))
		}
		iFree = nOrig
		for {
			if !(iFree > nFin && rc == SQLITE_OK) {
				break
			}
			rc = _incrVacuumStep(tls, pBt, nFin, iFree, libc.BoolInt32(nVac == nFree))
			goto _2
		_2:
			;
			iFree = iFree - 1
		}
		if (rc == int32(SQLITE_DONE) || rc == SQLITE_OK) && nFree > uint32(0) {
			rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FpDbPage)
			if nVac == nFree {
				_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+32, uint32(0))
				_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+36, uint32(0))
			}
			_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+28, nFin)
			(*TBtShared)(unsafe.Pointer(pBt)).FbDoTruncate = uint8(1)
			(*TBtShared)(unsafe.Pointer(pBt)).FnPage = nFin
		}
		if rc != SQLITE_OK {
			_sqlite3PagerRollback(tls, pPager)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Parameter zSrcData points to a buffer containing the data for
//	** page iSrcPg from the source database. Copy this data into the
//	** destination database.
//	*/
func _backupOnePage(tls *libc.TLS, p uintptr, iSrcPg TPgno, zSrcData uintptr, bUpdate int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iDest TPgno
	var iEnd, iOff Ti64
	var nCopy, nDestPgsz, nSrcPgsz, rc, v1, v3 int32
	var pDestPager, zDestData, zIn, zOut uintptr
	var v5 bool
	var _ /* pDestPg at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = iDest, iEnd, iOff, nCopy, nDestPgsz, nSrcPgsz, pDestPager, rc, zDestData, zIn, zOut, v1, v3, v5
	pDestPager = _sqlite3BtreePager(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)
	nSrcPgsz = _sqlite3BtreeGetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc)
	nDestPgsz = _sqlite3BtreeGetPageSize(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)
	if nSrcPgsz < nDestPgsz {
		v1 = nSrcPgsz
	} else {
		v1 = nDestPgsz
	}
	nCopy = v1
	iEnd = int64(iSrcPg) * int64(nSrcPgsz)
	rc = SQLITE_OK
	/* This loop runs once for each destination page spanned by the source
	 ** page. For each iteration, variable iOff is set to the byte offset
	 ** of the destination page.
	 */
	iOff = iEnd - int64(nSrcPgsz)
	for {
		if !(rc == SQLITE_OK && iOff < iEnd) {
			break
		}
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		iDest = uint32(iOff/int64(nDestPgsz)) + uint32(1)
		if iDest == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer((*Tsqlite3_backup)(unsafe.Pointer(p)).FpDest)).FpBt)).FpageSize+libc.Uint32FromInt32(1) {
			goto _2
		}
		v1 = _sqlite3PagerGet(tls, pDestPager, iDest, bp, 0)
		rc = v1
		if v5 = SQLITE_OK == v1; v5 {
			v3 = _sqlite3PagerWrite(tls, **(**uintptr)(__ccgo_up(bp)))
			rc = v3
		}
		if v5 && SQLITE_OK == v3 {
			zIn = zSrcData + uintptr(iOff%int64(nSrcPgsz))
			zDestData = _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp)))
			zOut = zDestData + uintptr(iOff%int64(nDestPgsz))
			/* Copy the data from the source page into the destination page.
			 ** Then clear the Btree layer MemPage.isInit flag. Both this module
			 ** and the pager code use this trick (clearing the first byte
			 ** of the page 'extra' space to invalidate the Btree layers
			 ** cached parse of the page). MemPage.isInit is marked
			 ** "MUST BE FIRST" for this purpose.
			 */
			libc.Xmemcpy(tls, zOut, zIn, uint32(nCopy))
			**(**Tu8)(__ccgo_up(_sqlite3PagerGetExtra(tls, **(**uintptr)(__ccgo_up(bp))))) = uint8(0)
			if iOff == 0 && bUpdate == 0 {
				_sqlite3Put4byte(tls, zOut+28, _sqlite3BtreeLastPage(tls, (*Tsqlite3_backup)(unsafe.Pointer(p)).FpSrc))
			}
		}
		_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp)))
		goto _2
	_2:
		;
		iOff = iOff + int64(nDestPgsz)
	}
	return rc
}

// C documentation
//
//	/*
//	** The page that pCur currently points to has just been modified in
//	** some way. This function figures out if this modification means the
//	** tree needs to be balanced, and if so calls the appropriate balancing
//	** routine. Balancing routines are:
//	**
//	**   balance_quick()
//	**   balance_deeper()
//	**   balance_nonroot()
//	*/
func _balance(tls *libc.TLS, pCur uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iIdx, iPage, rc, v1, v2 int32
	var pFree, pPage, pParent, pSpace uintptr
	var v3 bool
	var _ /* aBalanceQuickSpace at bp+0 */ [13]Tu8
	_, _, _, _, _, _, _, _, _, _ = iIdx, iPage, pFree, pPage, pParent, pSpace, rc, v1, v2, v3
	rc = SQLITE_OK
	pFree = uintptr(0)
	for cond := true; cond; cond = rc == SQLITE_OK {
		pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
		if (*TMemPage)(unsafe.Pointer(pPage)).FnFree < 0 && _btreeComputeFreeSpace(tls, pPage) != 0 {
			break
		}
		if int32((*TMemPage)(unsafe.Pointer(pPage)).FnOverflow) == 0 && (*TMemPage)(unsafe.Pointer(pPage)).FnFree*int32(3) <= int32((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FusableSize)*int32(2) {
			/* No rebalance required as long as:
			 **   (1) There are no overflow cells
			 **   (2) The amount of free space on the page is less than 2/3rds of
			 **       the total usable space on the page. */
			break
		} else {
			v1 = int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)
			iPage = v1
			if v1 == 0 {
				if v3 = (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow != 0; v3 {
					v2 = _anotherValidCursor(tls, pCur)
					rc = v2
				}
				if v3 && v2 == SQLITE_OK {
					/* The root page of the b-tree is overfull. In this case call the
					 ** balance_deeper() function to create a new child for the root-page
					 ** and copy the current contents of the root-page to it. The
					 ** next iteration of the do-loop will balance the child page.
					 */
					rc = _balance_deeper(tls, pPage, pCur+120+1*4)
					if rc == SQLITE_OK {
						(*TBtCursor)(unsafe.Pointer(pCur)).FiPage = int8(1)
						(*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(0)
						**(**Tu16)(__ccgo_up(pCur + 72)) = uint16(0)
						**(**uintptr)(__ccgo_up(pCur + 120)) = pPage
						(*TBtCursor)(unsafe.Pointer(pCur)).FpPage = **(**uintptr)(__ccgo_up(pCur + 120 + 1*4))
					}
				} else {
					break
				}
			} else {
				if _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) > int32(1) {
					/* The page being written is not a root page, and there is currently
					 ** more than one reference to it. This only happens if the page is one
					 ** of its own ancestor pages. Corruption. */
					rc = _sqlite3CorruptError(tls, int32(82400))
				} else {
					pParent = **(**uintptr)(__ccgo_up(pCur + 120 + uintptr(iPage-int32(1))*4))
					iIdx = int32(**(**Tu16)(__ccgo_up(pCur + 72 + uintptr(iPage-int32(1))*2)))
					rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pParent)).FpDbPage)
					if rc == SQLITE_OK && (*TMemPage)(unsafe.Pointer(pParent)).FnFree < 0 {
						rc = _btreeComputeFreeSpace(tls, pParent)
					}
					if rc == SQLITE_OK {
						if (*TMemPage)(unsafe.Pointer(pPage)).FintKeyLeaf != 0 && int32((*TMemPage)(unsafe.Pointer(pPage)).FnOverflow) == int32(1) && int32(**(**Tu16)(__ccgo_up(pPage + 28))) == int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) && (*TMemPage)(unsafe.Pointer(pParent)).Fpgno != uint32(1) && int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell) == iIdx {
							/* Call balance_quick() to create a new sibling of pPage on which
							 ** to store the overflow cell. balance_quick() inserts a new cell
							 ** into pParent, which may cause pParent overflow. If this
							 ** happens, the next iteration of the do-loop will balance pParent
							 ** use either balance_nonroot() or balance_deeper(). Until this
							 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
							 ** buffer.
							 **
							 ** The purpose of the following assert() is to check that only a
							 ** single call to balance_quick() is made for each call to this
							 ** function. If this were not verified, a subtle bug involving reuse
							 ** of the aBalanceQuickSpace[] might sneak in.
							 */
							rc = _balance_quick(tls, pParent, pPage, bp)
						} else {
							/* In this case, call balance_nonroot() to redistribute cells
							 ** between pPage and up to 2 of its sibling pages. This involves
							 ** modifying the contents of pParent, which may cause pParent to
							 ** become overfull or underfull. The next iteration of the do-loop
							 ** will balance the parent page to correct this.
							 **
							 ** If the parent page becomes overfull, the overflow cell or cells
							 ** are stored in the pSpace buffer allocated immediately below.
							 ** A subsequent iteration of the do-loop will deal with this by
							 ** calling balance_nonroot() (balance_deeper() may be called first,
							 ** but it doesn't deal with overflow cells - just moves them to a
							 ** different page). Once this subsequent call to balance_nonroot()
							 ** has completed, it is safe to release the pSpace buffer used by
							 ** the previous call, as the overflow cell data will have been
							 ** copied either into the body of a database page or into the new
							 ** pSpace buffer passed to the latter call to balance_nonroot().
							 */
							pSpace = _sqlite3PageMalloc(tls, int32((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FpageSize))
							rc = _balance_nonroot(tls, pParent, iIdx, pSpace, libc.BoolInt32(iPage == int32(1)), int32((*TBtCursor)(unsafe.Pointer(pCur)).Fhints)&int32(BTREE_BULKLOAD))
							if pFree != 0 {
								/* If pFree is not NULL, it points to the pSpace buffer used
								 ** by a previous call to balance_nonroot(). Its contents are
								 ** now stored either on real database pages or within the
								 ** new pSpace buffer, so it may be safely freed here. */
								_sqlite3PageFree(tls, pFree)
							}
							/* The pSpace buffer will be freed after the next call to
							 ** balance_nonroot(), or just before this function returns, whichever
							 ** comes first. */
							pFree = pSpace
						}
					}
					(*TMemPage)(unsafe.Pointer(pPage)).FnOverflow = uint8(0)
					/* The next iteration of the do-loop balances the parent page. */
					_releasePage(tls, pPage)
					(*TBtCursor)(unsafe.Pointer(pCur)).FiPage = (*TBtCursor)(unsafe.Pointer(pCur)).FiPage - 1
					(*TBtCursor)(unsafe.Pointer(pCur)).FpPage = **(**uintptr)(__ccgo_up(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4))
				}
			}
		}
	}
	if pFree != 0 {
		_sqlite3PageFree(tls, pFree)
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is called when the root page of a b-tree structure is
//	** overfull (has one or more overflow pages).
//	**
//	** A new child page is allocated and the contents of the current root
//	** page, including overflow cells, are copied into the child. The root
//	** page is then overwritten to make it an empty page with the right-child
//	** pointer pointing to the new page.
//	**
//	** Before returning, all pointer-map entries corresponding to pages
//	** that the new child-page now contains pointers to are updated. The
//	** entry corresponding to the new right-child pointer of the root
//	** page is also updated.
//	**
//	** If successful, *ppChild is set to contain a reference to the child
//	** page and SQLITE_OK is returned. In this case the caller is required
//	** to call releasePage() on *ppChild exactly once. If an error occurs,
//	** an error code is returned and *ppChild is set to 0.
//	*/
func _balance_deeper(tls *libc.TLS, pRoot uintptr, ppChild uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pBt uintptr
	var _ /* pChild at bp+4 */ uintptr
	var _ /* pgnoChild at bp+8 */ TPgno
	var _ /* rc at bp+0 */ int32
	_ = pBt                                       /* Return value from subprocedures */
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* Pointer to a new child page */
	**(**TPgno)(__ccgo_up(bp + 8)) = uint32(0)    /* Page number of the new child page */
	pBt = (*TMemPage)(unsafe.Pointer(pRoot)).FpBt /* The BTree */
	/* Make pRoot, the root page of the b-tree, writable. Allocate a new
	 ** page that will become the new right-child of pPage. Copy the contents
	 ** of the node stored on pRoot into the new child page.
	 */
	**(**int32)(__ccgo_up(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pRoot)).FpDbPage)
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp)) = _allocateBtreePage(tls, pBt, bp+4, bp+8, (*TMemPage)(unsafe.Pointer(pRoot)).Fpgno, uint8(0))
		_copyNodeContent(tls, pRoot, **(**uintptr)(__ccgo_up(bp + 4)), bp)
		if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
			_ptrmapPut(tls, pBt, **(**TPgno)(__ccgo_up(bp + 8)), uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(pRoot)).Fpgno, bp)
		}
	}
	if **(**int32)(__ccgo_up(bp)) != 0 {
		**(**uintptr)(__ccgo_up(ppChild)) = uintptr(0)
		_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 4)))
		return **(**int32)(__ccgo_up(bp))
	}
	/* Copy the overflow cells from pRoot to pChild */
	libc.Xmemcpy(tls, **(**uintptr)(__ccgo_up(bp + 4))+28, pRoot+28, uint32((*TMemPage)(unsafe.Pointer(pRoot)).FnOverflow)*uint32(2))
	libc.Xmemcpy(tls, **(**uintptr)(__ccgo_up(bp + 4))+36, pRoot+36, uint32((*TMemPage)(unsafe.Pointer(pRoot)).FnOverflow)*uint32(4))
	(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FnOverflow = (*TMemPage)(unsafe.Pointer(pRoot)).FnOverflow
	/* Zero the contents of pRoot. Then install pChild as the right-child. */
	_zeroPage(tls, pRoot, int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FaData))) & ^libc.Int32FromInt32(PTF_LEAF))
	_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pRoot)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pRoot)).FhdrOffset)+int32(8)), **(**TPgno)(__ccgo_up(bp + 8)))
	**(**uintptr)(__ccgo_up(ppChild)) = **(**uintptr)(__ccgo_up(bp + 4))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This routine redistributes cells on the iParentIdx'th child of pParent
//	** (hereafter "the page") and up to 2 siblings so that all pages have about the
//	** same amount of free space. Usually a single sibling on either side of the
//	** page are used in the balancing, though both siblings might come from one
//	** side if the page is the first or last child of its parent. If the page
//	** has fewer than 2 siblings (something which can only happen if the page
//	** is a root page or a child of a root page) then all available siblings
//	** participate in the balancing.
//	**
//	** The number of siblings of the page might be increased or decreased by
//	** one or two in an effort to keep pages nearly full but not over full.
//	**
//	** Note that when this routine is called, some of the cells on the page
//	** might not actually be stored in MemPage.aData[]. This can happen
//	** if the page is overfull. This routine ensures that all cells allocated
//	** to the page and its siblings fit into MemPage.aData[] before returning.
//	**
//	** In the course of balancing the page and its siblings, cells may be
//	** inserted into or removed from the parent page (pParent). Doing so
//	** may cause the parent page to become overfull or underfull. If this
//	** happens, it is the responsibility of the caller to invoke the correct
//	** balancing routine to fix this problem (see the balance() routine).
//	**
//	** If this routine fails for any reason, it might leave the database
//	** in a corrupted state. So if this routine fails, the database should
//	** be rolled back.
//	**
//	** The third argument to this function, aOvflSpace, is a pointer to a
//	** buffer big enough to hold one page. If while inserting cells into the parent
//	** page (pParent) the parent page becomes overfull, this buffer is
//	** used to store the parent's overflow cells. Because this function inserts
//	** a maximum of four divider cells into the parent page, and the maximum
//	** size of a cell stored within an internal node is always less than 1/4
//	** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
//	** enough for all overflow cells.
//	**
//	** If aOvflSpace is set to a null pointer, this function returns
//	** SQLITE_NOMEM.
//	*/
func _balance_nonroot(tls *libc.TLS, pParent uintptr, iParentIdx int32, aOvflSpace uintptr, isRoot int32, bBulk int32) (r1 int32) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var aData, aSpace1, p, pBt, pCell, pCell1, pNew1, pNew2, pOld, pOld1, pOld2, pRight, pSrcEnd, pTemp, pTemp1, piCell, piEnd, v17 uintptr
	var aPgno [5]TPgno
	var apDiv [2]uintptr
	var apNew [5]uintptr
	var cntNew, cntOld [5]int32
	var cntOldNext, d, i, iB, iNew, iNew1, iOff, iOld, iOld1, iOvflSpace, iPg, iSpace1, j, k, leafData, limit, nMaxCells, nNew, nNewCell, nOld, nxDiv, pageFlags, r, sz1, sz2, szD, szLeft, szR, szRight, usableSpace, v1 int32
	var fgA, fgB, leafCorrection, maskPage, sz Tu16
	var key Tu32
	var pgnoA, pgnoB, pgnoTemp TPgno
	var szScratch Tu64
	var v13, v14 bool
	var v18 uint32
	var _ /* abDone at bp+40 */ [5]Tu8
	var _ /* apOld at bp+4 */ [3]uintptr
	var _ /* b at bp+48 */ TCellArray
	var _ /* info at bp+120 */ TCellInfo
	var _ /* pNew at bp+112 */ uintptr
	var _ /* pgno at bp+36 */ TPgno
	var _ /* rc at bp+0 */ int32
	var _ /* szNew at bp+16 */ [5]int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, aPgno, aSpace1, apDiv, apNew, cntNew, cntOld, cntOldNext, d, fgA, fgB, i, iB, iNew, iNew1, iOff, iOld, iOld1, iOvflSpace, iPg, iSpace1, j, k, key, leafCorrection, leafData, limit, maskPage, nMaxCells, nNew, nNewCell, nOld, nxDiv, p, pBt, pCell, pCell1, pNew1, pNew2, pOld, pOld1, pOld2, pRight, pSrcEnd, pTemp, pTemp1, pageFlags, pgnoA, pgnoB, pgnoTemp, piCell, piEnd, r, sz, sz1, sz2, szD, szLeft, szR, szRight, szScratch, usableSpace, v1, v13, v14, v17, v18 /* The whole database */
	nMaxCells = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                /* Allocated size of apCell, szCell, aFrom. */
	nNew = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     /* Next divider slot in pParent->aCell[] */
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       /* Value of pPage->aData[0] */
	iSpace1 = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  /* First unused byte of aSpace1[] */
	iOvflSpace = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               /* Parsed information on cells being balanced */
	libc.Xmemset(tls, bp+40, 0, uint32(5))
	libc.Xmemset(tls, bp+48, 0, libc.Uint32FromInt64(64)-libc.Uint32FromInt64(4))
	**(**int32)(__ccgo_up(bp + 48 + 40 + uintptr(libc.Int32FromInt32(NB)*libc.Int32FromInt32(2)-libc.Int32FromInt32(1))*4)) = int32(0x7fffffff)
	pBt = (*TMemPage)(unsafe.Pointer(pParent)).FpBt
	/* At this point pParent may have at most one overflow cell. And if
	 ** this overflow cell is present, it must be the cell with
	 ** index iParentIdx. This scenario comes about when this function
	 ** is called (indirectly) from sqlite3BtreeDelete().
	 */
	if !(aOvflSpace != 0) {
		return int32(SQLITE_NOMEM)
	}
	/* Find the sibling pages to balance. Also locate the cells in pParent
	 ** that divide the siblings. An attempt is made to find NN siblings on
	 ** either side of pPage. More siblings are taken from one side, however,
	 ** if there are fewer than NN siblings on the other side. If pParent
	 ** has NB or fewer children then all children of pParent are taken.
	 **
	 ** This loop also drops the divider cells from the parent page. This
	 ** way, the remainder of the function does not have to deal with any
	 ** overflow cells in the parent page, since if any existed they will
	 ** have already been removed.
	 */
	i = int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow) + int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell)
	if i < int32(2) {
		nxDiv = 0
	} else {
		if iParentIdx == 0 {
			nxDiv = 0
		} else {
			if iParentIdx == i {
				nxDiv = i - int32(2) + bBulk
			} else {
				nxDiv = iParentIdx - int32(1)
			}
		}
		i = int32(2) - bBulk
	}
	nOld = i + int32(1)
	if i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow) == int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell) {
		pRight = (*TMemPage)(unsafe.Pointer(pParent)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pParent)).FhdrOffset)+int32(8))
	} else {
		pRight = (*TMemPage)(unsafe.Pointer(pParent)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pParent)).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pParent)).FaCellIdx + uintptr(int32(2)*(i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow))))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pParent)).FaCellIdx + uintptr(int32(2)*(i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow))) + 1)))))
	}
	**(**TPgno)(__ccgo_up(bp + 36)) = _sqlite3Get4byte(tls, pRight)
	for int32(1) != 0 {
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp)) = _getAndInitPage(tls, pBt, **(**TPgno)(__ccgo_up(bp + 36)), bp+4+uintptr(i)*4, 0)
		}
		if **(**int32)(__ccgo_up(bp)) != 0 {
			libc.Xmemset(tls, bp+4, 0, uint32(i+libc.Int32FromInt32(1))*uint32(4))
			goto balance_cleanup
		}
		if (*TMemPage)(unsafe.Pointer((**(**[3]uintptr)(__ccgo_up(bp + 4)))[i])).FnFree < 0 {
			**(**int32)(__ccgo_up(bp)) = _btreeComputeFreeSpace(tls, (**(**[3]uintptr)(__ccgo_up(bp + 4)))[i])
			if **(**int32)(__ccgo_up(bp)) != 0 {
				libc.Xmemset(tls, bp+4, 0, uint32(i)*uint32(4))
				goto balance_cleanup
			}
		}
		nMaxCells = nMaxCells + (int32((*TMemPage)(unsafe.Pointer((**(**[3]uintptr)(__ccgo_up(bp + 4)))[i])).FnCell) + int32(libc.Uint32FromInt64(16)/libc.Uint32FromInt64(4)))
		v1 = i
		i = i - 1
		if v1 == 0 {
			break
		}
		if (*TMemPage)(unsafe.Pointer(pParent)).FnOverflow != 0 && i+nxDiv == int32(**(**Tu16)(__ccgo_up(pParent + 28))) {
			apDiv[i] = **(**uintptr)(__ccgo_up(pParent + 36))
			**(**TPgno)(__ccgo_up(bp + 36)) = _sqlite3Get4byte(tls, apDiv[i])
			(**(**[5]int32)(__ccgo_up(bp + 16)))[i] = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pParent)).FxCellSize})))(tls, pParent, apDiv[i]))
			(*TMemPage)(unsafe.Pointer(pParent)).FnOverflow = uint8(0)
		} else {
			apDiv[i] = (*TMemPage)(unsafe.Pointer(pParent)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pParent)).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pParent)).FaCellIdx + uintptr(int32(2)*(i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow))))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pParent)).FaCellIdx + uintptr(int32(2)*(i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow))) + 1)))))
			**(**TPgno)(__ccgo_up(bp + 36)) = _sqlite3Get4byte(tls, apDiv[i])
			(**(**[5]int32)(__ccgo_up(bp + 16)))[i] = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pParent)).FxCellSize})))(tls, pParent, apDiv[i]))
			/* Drop the cell from the parent page. apDiv[i] still points to
			 ** the cell within the parent, even though it has been dropped.
			 ** This is safe because dropping a cell only overwrites the first
			 ** four bytes of it, and this function does not need the first
			 ** four bytes of the divider cell. So the pointer is safe to use
			 ** later on.
			 **
			 ** But not if we are in secure-delete mode. In secure-delete mode,
			 ** the dropCell() routine will overwrite the entire cell with zeroes.
			 ** In this case, temporarily copy the cell into the aOvflSpace[]
			 ** buffer. It will be copied out again as soon as the aSpace[] buffer
			 ** is allocated.  */
			if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_FAST_SECURE) != 0 {
				/* If the following if() condition is not true, the db is corrupted.
				 ** The call to dropCell() below will detect this.  */
				iOff = int32(apDiv[i]) - int32((*TMemPage)(unsafe.Pointer(pParent)).FaData)
				if iOff+(**(**[5]int32)(__ccgo_up(bp + 16)))[i] <= int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize) {
					libc.Xmemcpy(tls, aOvflSpace+uintptr(iOff), apDiv[i], uint32((**(**[5]int32)(__ccgo_up(bp + 16)))[i]))
					apDiv[i] = aOvflSpace + uintptr(int32(apDiv[i])-int32((*TMemPage)(unsafe.Pointer(pParent)).FaData))
				}
			}
			_dropCell(tls, pParent, i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow), (**(**[5]int32)(__ccgo_up(bp + 16)))[i], bp)
		}
	}
	/* Make nMaxCells a multiple of 4 in order to preserve 8-byte
	 ** alignment */
	nMaxCells = (nMaxCells + int32(3)) & ^libc.Int32FromInt32(3)
	/*
	 ** Allocate space for memory structures
	 */
	szScratch = uint64(uint32(nMaxCells)*uint32(4) + uint32(nMaxCells)*uint32(2) + (*TBtShared)(unsafe.Pointer(pBt)).FpageSize) /* aSpace1 */
	(**(**TCellArray)(__ccgo_up(bp + 48))).FapCell = _sqlite3DbMallocRaw(tls, uintptr(0), szScratch)
	if (**(**TCellArray)(__ccgo_up(bp + 48))).FapCell == uintptr(0) {
		**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
		goto balance_cleanup
	}
	(**(**TCellArray)(__ccgo_up(bp + 48))).FszCell = (**(**TCellArray)(__ccgo_up(bp + 48))).FapCell + uintptr(nMaxCells)*4
	aSpace1 = (**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr(nMaxCells)*2
	/*
	 ** Load pointers to all cells on sibling pages and the divider cells
	 ** into the local b.apCell[] array.  Make copies of the divider cells
	 ** into space obtained from aSpace1[]. The divider cells have already
	 ** been removed from pParent.
	 **
	 ** If the siblings are on leaf pages, then the child pointers of the
	 ** divider cells are stripped from the cells before they are copied
	 ** into aSpace1[].  In this way, all cells in b.apCell[] are without
	 ** child pointers.  If siblings are not leaves, then all cell in
	 ** b.apCell[] include child pointers.  Either way, all cells in b.apCell[]
	 ** are alike.
	 **
	 ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
	 **       leafData:  1 if pPage holds key+data and pParent holds only keys.
	 */
	(**(**TCellArray)(__ccgo_up(bp + 48))).FpRef = (**(**[3]uintptr)(__ccgo_up(bp + 4)))[0]
	leafCorrection = uint16(int32((*TMemPage)(unsafe.Pointer((**(**TCellArray)(__ccgo_up(bp + 48))).FpRef)).Fleaf) * int32(4))
	leafData = int32((*TMemPage)(unsafe.Pointer((**(**TCellArray)(__ccgo_up(bp + 48))).FpRef)).FintKeyLeaf)
	i = 0
	for {
		if !(i < nOld) {
			break
		}
		pOld = (**(**[3]uintptr)(__ccgo_up(bp + 4)))[i]
		limit = int32((*TMemPage)(unsafe.Pointer(pOld)).FnCell)
		aData = (*TMemPage)(unsafe.Pointer(pOld)).FaData
		maskPage = (*TMemPage)(unsafe.Pointer(pOld)).FmaskPage
		piCell = aData + uintptr((*TMemPage)(unsafe.Pointer(pOld)).FcellOffset)
		/* Verify that all sibling pages are of the same "type" (table-leaf,
		 ** table-interior, index-leaf, or index-interior).
		 */
		if int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pOld)).FaData))) != int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer((**(**[3]uintptr)(__ccgo_up(bp + 4)))[0])).FaData))) {
			**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(81667))
			goto balance_cleanup
		}
		/* Load b.apCell[] with pointers to all cells in pOld.  If pOld
		 ** contains overflow cells, include them in the b.apCell[] array
		 ** in the correct spot.
		 **
		 ** Note that when there are multiple overflow cells, it is always the
		 ** case that they are sequential and adjacent.  This invariant arises
		 ** because multiple overflows can only occurs when inserting divider
		 ** cells into a parent on a prior balance, and divider cells are always
		 ** adjacent and are inserted in order.  There is an assert() tagged
		 ** with "NOTE 1" in the overflow cell insertion loop to prove this
		 ** invariant.
		 **
		 ** This must be done in advance.  Once the balance starts, the cell
		 ** offset section of the btree page will be overwritten and we will no
		 ** long be able to find the cells if a pointer to each cell is not saved
		 ** first.
		 */
		libc.Xmemset(tls, (**(**TCellArray)(__ccgo_up(bp + 48))).FszCell+uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*2, 0, uint32(2)*uint32(limit+int32((*TMemPage)(unsafe.Pointer(pOld)).FnOverflow)))
		if int32((*TMemPage)(unsafe.Pointer(pOld)).FnOverflow) > 0 {
			if limit < int32(**(**Tu16)(__ccgo_up(pOld + 28))) {
				**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(81691))
				goto balance_cleanup
			}
			limit = int32(**(**Tu16)(__ccgo_up(pOld + 28)))
			j = 0
			for {
				if !(j < limit) {
					break
				}
				**(**uintptr)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FapCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*4)) = aData + uintptr(int32(maskPage)&(int32(**(**Tu8)(__ccgo_up(piCell)))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up(piCell + 1)))))
				piCell = piCell + uintptr(2)
				(**(**TCellArray)(__ccgo_up(bp + 48))).FnCell = (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell + 1
				goto _3
			_3:
				;
				j = j + 1
			}
			k = 0
			for {
				if !(k < int32((*TMemPage)(unsafe.Pointer(pOld)).FnOverflow)) {
					break
				}
				/* NOTE 1 */
				**(**uintptr)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FapCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*4)) = **(**uintptr)(__ccgo_up(pOld + 36 + uintptr(k)*4))
				(**(**TCellArray)(__ccgo_up(bp + 48))).FnCell = (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell + 1
				goto _4
			_4:
				;
				k = k + 1
			}
		}
		piEnd = aData + uintptr((*TMemPage)(unsafe.Pointer(pOld)).FcellOffset) + uintptr(int32(2)*int32((*TMemPage)(unsafe.Pointer(pOld)).FnCell))
		for piCell < piEnd {
			**(**uintptr)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FapCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*4)) = aData + uintptr(int32(maskPage)&(int32(**(**Tu8)(__ccgo_up(piCell)))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up(piCell + 1)))))
			piCell = piCell + uintptr(2)
			(**(**TCellArray)(__ccgo_up(bp + 48))).FnCell = (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell + 1
		}
		cntOld[i] = (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell
		if i < nOld-int32(1) && !(leafData != 0) {
			sz = uint16((**(**[5]int32)(__ccgo_up(bp + 16)))[i])
			**(**Tu16)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*2)) = sz
			pTemp = aSpace1 + uintptr(iSpace1)
			iSpace1 = iSpace1 + int32(sz)
			libc.Xmemcpy(tls, pTemp, apDiv[i], uint32(sz))
			**(**uintptr)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FapCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*4)) = pTemp + uintptr(leafCorrection)
			**(**Tu16)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*2)) = uint16(int32(**(**Tu16)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*2))) - int32(leafCorrection))
			if !((*TMemPage)(unsafe.Pointer(pOld)).Fleaf != 0) {
				/* The right pointer of the child page pOld becomes the left
				 ** pointer of the divider cell */
				libc.Xmemcpy(tls, **(**uintptr)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FapCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*4)), (*TMemPage)(unsafe.Pointer(pOld)).FaData+8, uint32(4))
			} else {
				for int32(**(**Tu16)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*2))) < int32(4) {
					/* Do not allow any cells smaller than 4 bytes. If a smaller cell
					 ** does exist, pad it with 0x00 bytes. */
					v1 = iSpace1
					iSpace1 = iSpace1 + 1
					**(**Tu8)(__ccgo_up(aSpace1 + uintptr(v1))) = uint8(0x00)
					**(**Tu16)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*2)) = **(**Tu16)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr((**(**TCellArray)(__ccgo_up(bp + 48))).FnCell)*2)) + 1
				}
			}
			(**(**TCellArray)(__ccgo_up(bp + 48))).FnCell = (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell + 1
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	/*
	 ** Figure out the number of pages needed to hold all b.nCell cells.
	 ** Store this number in "k".  Also compute szNew[] which is the total
	 ** size of all cells on the i-th page and cntNew[] which is the index
	 ** in b.apCell[] of the cell that divides page i from page i+1.
	 ** cntNew[k] should equal b.nCell.
	 **
	 ** Values computed by this block:
	 **
	 **           k: The total number of sibling pages
	 **    szNew[i]: Spaced used on the i-th sibling page.
	 **   cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
	 **              the right of the i-th sibling page.
	 ** usableSpace: Number of bytes of space available on each sibling.
	 **
	 */
	usableSpace = int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(12) + uint32(leafCorrection))
	v1 = libc.Int32FromInt32(0)
	k = v1
	i = v1
	for {
		if !(i < nOld) {
			break
		}
		p = (**(**[3]uintptr)(__ccgo_up(bp + 4)))[i]
		**(**uintptr)(__ccgo_up(bp + 48 + 16 + uintptr(k)*4)) = (*TMemPage)(unsafe.Pointer(p)).FaDataEnd
		**(**int32)(__ccgo_up(bp + 48 + 40 + uintptr(k)*4)) = cntOld[i]
		if k != 0 && **(**int32)(__ccgo_up(bp + 48 + 40 + uintptr(k)*4)) == **(**int32)(__ccgo_up(bp + 48 + 40 + uintptr(k-int32(1))*4)) {
			k = k - 1 /* Omit b.ixNx[] entry for child pages with no cells */
		}
		if !(leafData != 0) {
			k = k + 1
			**(**uintptr)(__ccgo_up(bp + 48 + 16 + uintptr(k)*4)) = (*TMemPage)(unsafe.Pointer(pParent)).FaDataEnd
			**(**int32)(__ccgo_up(bp + 48 + 40 + uintptr(k)*4)) = cntOld[i] + int32(1)
		}
		(**(**[5]int32)(__ccgo_up(bp + 16)))[i] = usableSpace - (*TMemPage)(unsafe.Pointer(p)).FnFree
		j = 0
		for {
			if !(j < int32((*TMemPage)(unsafe.Pointer(p)).FnOverflow)) {
				break
			}
			**(**int32)(__ccgo_up(bp + 16 + uintptr(i)*4)) += int32(2) + int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(p)).FxCellSize})))(tls, p, **(**uintptr)(__ccgo_up(p + 36 + uintptr(j)*4))))
			goto _8
		_8:
			;
			j = j + 1
		}
		cntNew[i] = cntOld[i]
		goto _6
	_6:
		;
		i = i + 1
		k = k + 1
	}
	k = nOld
	i = 0
	for {
		if !(i < k) {
			break
		}
		for (**(**[5]int32)(__ccgo_up(bp + 16)))[i] > usableSpace {
			if i+int32(1) >= k {
				k = i + int32(2)
				if k > libc.Int32FromInt32(NB)+libc.Int32FromInt32(2) {
					**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(81792))
					goto balance_cleanup
				}
				(**(**[5]int32)(__ccgo_up(bp + 16)))[k-int32(1)] = 0
				cntNew[k-int32(1)] = (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell
			}
			sz1 = int32(2) + int32(_cachedCellSize(tls, bp+48, cntNew[i]-int32(1)))
			**(**int32)(__ccgo_up(bp + 16 + uintptr(i)*4)) -= sz1
			if !(leafData != 0) {
				if cntNew[i] < (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell {
					sz1 = int32(2) + int32(_cachedCellSize(tls, bp+48, cntNew[i]))
				} else {
					sz1 = 0
				}
			}
			**(**int32)(__ccgo_up(bp + 16 + uintptr(i+int32(1))*4)) += sz1
			cntNew[i] = cntNew[i] - 1
		}
		for cntNew[i] < (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell {
			sz1 = int32(2) + int32(_cachedCellSize(tls, bp+48, cntNew[i]))
			if (**(**[5]int32)(__ccgo_up(bp + 16)))[i]+sz1 > usableSpace {
				break
			}
			**(**int32)(__ccgo_up(bp + 16 + uintptr(i)*4)) += sz1
			cntNew[i] = cntNew[i] + 1
			if !(leafData != 0) {
				if cntNew[i] < (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell {
					sz1 = int32(2) + int32(_cachedCellSize(tls, bp+48, cntNew[i]))
				} else {
					sz1 = 0
				}
			}
			**(**int32)(__ccgo_up(bp + 16 + uintptr(i+int32(1))*4)) -= sz1
		}
		if cntNew[i] >= (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell {
			k = i + int32(1)
		} else {
			if i > 0 {
				v1 = cntNew[i-int32(1)]
			} else {
				v1 = 0
			}
			if cntNew[i] <= v1 {
				**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(81825))
				goto balance_cleanup
			}
		}
		goto _9
	_9:
		;
		i = i + 1
	}
	/*
	 ** The packing computed by the previous block is biased toward the siblings
	 ** on the left side (siblings with smaller keys). The left siblings are
	 ** always nearly full, while the right-most sibling might be nearly empty.
	 ** The next block of code attempts to adjust the packing of siblings to
	 ** get a better balance.
	 **
	 ** This adjustment is more than an optimization.  The packing above might
	 ** be so out of balance as to be illegal.  For example, the right-most
	 ** sibling might be completely empty.  This adjustment is not optional.
	 */
	i = k - int32(1)
	for {
		if !(i > 0) {
			break
		}
		szRight = (**(**[5]int32)(__ccgo_up(bp + 16)))[i]         /* Size of sibling on the right */
		szLeft = (**(**[5]int32)(__ccgo_up(bp + 16)))[i-int32(1)] /* Index of first cell to the left of right sibling */
		r = cntNew[i-int32(1)] - int32(1)
		d = r + int32(1) - leafData
		_cachedCellSize(tls, bp+48, d)
		for cond := true; cond; cond = r >= 0 {
			szR = int32(_cachedCellSize(tls, bp+48, r))
			szD = int32(**(**Tu16)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr(d)*2)))
			if v14 = szRight != 0; v14 {
				if v13 = bBulk != 0; !v13 {
					if i == k-int32(1) {
						v1 = 0
					} else {
						v1 = int32(2)
					}
				}
			}
			if v14 && (v13 || szRight+szD+int32(2) > szLeft-(szR+v1)) {
				break
			}
			szRight = szRight + (szD + int32(2))
			szLeft = szLeft - (szR + int32(2))
			cntNew[i-int32(1)] = r
			r = r - 1
			d = d - 1
		}
		(**(**[5]int32)(__ccgo_up(bp + 16)))[i] = szRight
		(**(**[5]int32)(__ccgo_up(bp + 16)))[i-int32(1)] = szLeft
		if i > int32(1) {
			v1 = cntNew[i-int32(2)]
		} else {
			v1 = 0
		}
		if cntNew[i-int32(1)] <= v1 {
			**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(81869))
			goto balance_cleanup
		}
		goto _11
	_11:
		;
		i = i - 1
	}
	/* Sanity check:  For a non-corrupt database file one of the following
	 ** must be true:
	 **    (1) We found one or more cells (cntNew[0])>0), or
	 **    (2) pPage is a virtual root page.  A virtual root page is when
	 **        the real root page is page 1 and we are the only child of
	 **        that page.
	 */
	/*
	 ** Allocate k new pages.  Reuse old pages where possible.
	 */
	pageFlags = int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer((**(**[3]uintptr)(__ccgo_up(bp + 4)))[0])).FaData)))
	i = 0
	for {
		if !(i < k) {
			break
		}
		if i < nOld {
			v17 = (**(**[3]uintptr)(__ccgo_up(bp + 4)))[i]
			apNew[i] = v17
			**(**uintptr)(__ccgo_up(bp + 112)) = v17
			(**(**[3]uintptr)(__ccgo_up(bp + 4)))[i] = uintptr(0)
			**(**int32)(__ccgo_up(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 112)))).FpDbPage)
			nNew = nNew + 1
			if _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 112)))).FpDbPage) != int32(1)+libc.BoolInt32(i == iParentIdx-nxDiv) && **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(81902))
			}
			if **(**int32)(__ccgo_up(bp)) != 0 {
				goto balance_cleanup
			}
		} else {
			if bBulk != 0 {
				v18 = uint32(1)
			} else {
				v18 = **(**TPgno)(__ccgo_up(bp + 36))
			}
			**(**int32)(__ccgo_up(bp)) = _allocateBtreePage(tls, pBt, bp+112, bp+36, v18, uint8(0))
			if **(**int32)(__ccgo_up(bp)) != 0 {
				goto balance_cleanup
			}
			_zeroPage(tls, **(**uintptr)(__ccgo_up(bp + 112)), pageFlags)
			apNew[i] = **(**uintptr)(__ccgo_up(bp + 112))
			nNew = nNew + 1
			cntOld[i] = (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell
			/* Set the pointer-map entry for the new sibling page. */
			if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
				_ptrmapPut(tls, pBt, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 112)))).Fpgno, uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(pParent)).Fpgno, bp)
				if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
					goto balance_cleanup
				}
			}
		}
		goto _16
	_16:
		;
		i = i + 1
	}
	/*
	 ** Reassign page numbers so that the new pages are in ascending order.
	 ** This helps to keep entries in the disk file in order so that a scan
	 ** of the table is closer to a linear scan through the file. That in turn
	 ** helps the operating system to deliver pages from the disk more rapidly.
	 **
	 ** An O(N*N) sort algorithm is used, but since N is never more than NB+2
	 ** (5), that is not a performance concern.
	 **
	 ** When NB==3, this one optimization makes the database about 25% faster
	 ** for large insertions and deletions.
	 */
	i = 0
	for {
		if !(i < nNew) {
			break
		}
		aPgno[i] = (*TMemPage)(unsafe.Pointer(apNew[i])).Fpgno
		goto _19
	_19:
		;
		i = i + 1
	}
	i = 0
	for {
		if !(i < nNew-int32(1)) {
			break
		}
		iB = i
		j = i + int32(1)
		for {
			if !(j < nNew) {
				break
			}
			if (*TMemPage)(unsafe.Pointer(apNew[j])).Fpgno < (*TMemPage)(unsafe.Pointer(apNew[iB])).Fpgno {
				iB = j
			}
			goto _21
		_21:
			;
			j = j + 1
		}
		/* If apNew[i] has a page number that is bigger than any of the
		 ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent
		 ** entry that has the smallest page number (which we know to be
		 ** entry apNew[iB]).
		 */
		if iB != i {
			pgnoA = (*TMemPage)(unsafe.Pointer(apNew[i])).Fpgno
			pgnoB = (*TMemPage)(unsafe.Pointer(apNew[iB])).Fpgno
			pgnoTemp = uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize + uint32(1)
			fgA = (*TDbPage)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(apNew[i])).FpDbPage)).Fflags
			fgB = (*TDbPage)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(apNew[iB])).FpDbPage)).Fflags
			_sqlite3PagerRekey(tls, (*TMemPage)(unsafe.Pointer(apNew[i])).FpDbPage, pgnoTemp, fgB)
			_sqlite3PagerRekey(tls, (*TMemPage)(unsafe.Pointer(apNew[iB])).FpDbPage, pgnoA, fgA)
			_sqlite3PagerRekey(tls, (*TMemPage)(unsafe.Pointer(apNew[i])).FpDbPage, pgnoB, fgB)
			(*TMemPage)(unsafe.Pointer(apNew[i])).Fpgno = pgnoB
			(*TMemPage)(unsafe.Pointer(apNew[iB])).Fpgno = pgnoA
		}
		goto _20
	_20:
		;
		i = i + 1
	}
	_sqlite3Put4byte(tls, pRight, (*TMemPage)(unsafe.Pointer(apNew[nNew-int32(1)])).Fpgno)
	/* If the sibling pages are not leaves, ensure that the right-child pointer
	 ** of the right-most new sibling page is set to the value that was
	 ** originally in the same field of the right-most old sibling page. */
	if pageFlags&int32(PTF_LEAF) == 0 && nOld != nNew {
		if nNew > nOld {
			pOld1 = apNew[nOld-int32(1)]
		} else {
			pOld1 = (**(**[3]uintptr)(__ccgo_up(bp + 4)))[nOld-int32(1)]
		}
		libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(apNew[nNew-int32(1)])).FaData+8, (*TMemPage)(unsafe.Pointer(pOld1)).FaData+8, uint32(4))
	}
	/* Make any required updates to pointer map entries associated with
	 ** cells stored on sibling pages following the balance operation. Pointer
	 ** map entries associated with divider cells are set by the insertCell()
	 ** routine. The associated pointer map entries are:
	 **
	 **   a) if the cell contains a reference to an overflow chain, the
	 **      entry associated with the first page in the overflow chain, and
	 **
	 **   b) if the sibling pages are not leaves, the child page associated
	 **      with the cell.
	 **
	 ** If the sibling pages are not leaves, then the pointer map entry
	 ** associated with the right-child of each sibling may also need to be
	 ** updated. This happens below, after the sibling pages have been
	 ** populated, not here.
	 */
	if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
		v17 = apNew[0]
		pOld2 = v17
		pNew1 = v17
		cntOldNext = int32((*TMemPage)(unsafe.Pointer(pNew1)).FnCell) + int32((*TMemPage)(unsafe.Pointer(pNew1)).FnOverflow)
		iNew = 0
		iOld = 0
		i = 0
		for {
			if !(i < (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell) {
				break
			}
			pCell = **(**uintptr)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FapCell + uintptr(i)*4))
			for i == cntOldNext {
				iOld = iOld + 1
				if iOld < nNew {
					v17 = apNew[iOld]
				} else {
					v17 = (**(**[3]uintptr)(__ccgo_up(bp + 4)))[iOld]
				}
				pOld2 = v17
				cntOldNext = cntOldNext + (int32((*TMemPage)(unsafe.Pointer(pOld2)).FnCell) + int32((*TMemPage)(unsafe.Pointer(pOld2)).FnOverflow) + libc.BoolInt32(!(leafData != 0)))
			}
			if i == cntNew[iNew] {
				iNew = iNew + 1
				v1 = iNew
				pNew1 = apNew[v1]
				if !(leafData != 0) {
					goto _23
				}
			}
			/* Cell pCell is destined for new sibling page pNew. Originally, it
			 ** was either part of sibling page iOld (possibly an overflow cell),
			 ** or else the divider cell to the left of sibling page iOld. So,
			 ** if sibling page iOld had the same page number as pNew, and if
			 ** pCell really was a part of sibling page iOld (not a divider or
			 ** overflow cell), we can skip updating the pointer map entries.  */
			if iOld >= nNew || (*TMemPage)(unsafe.Pointer(pNew1)).Fpgno != aPgno[iOld] || !(uint32(pCell) >= uint32((*TMemPage)(unsafe.Pointer(pOld2)).FaData) && uint32(pCell) < uint32((*TMemPage)(unsafe.Pointer(pOld2)).FaDataEnd)) {
				if !(leafCorrection != 0) {
					_ptrmapPut(tls, pBt, _sqlite3Get4byte(tls, pCell), uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(pNew1)).Fpgno, bp)
				}
				if int32(_cachedCellSize(tls, bp+48, i)) > int32((*TMemPage)(unsafe.Pointer(pNew1)).FminLocal) {
					_ptrmapPutOvflPtr(tls, pNew1, pOld2, pCell, bp)
				}
				if **(**int32)(__ccgo_up(bp)) != 0 {
					goto balance_cleanup
				}
			}
			goto _23
		_23:
			;
			i = i + 1
		}
	}
	/* Insert new divider cells into pParent. */
	i = 0
	for {
		if !(i < nNew-int32(1)) {
			break
		}
		pNew2 = apNew[i]
		j = cntNew[i]
		pCell1 = **(**uintptr)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FapCell + uintptr(j)*4))
		sz2 = int32(**(**Tu16)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr(j)*2))) + int32(leafCorrection)
		pTemp1 = aOvflSpace + uintptr(iOvflSpace)
		if !((*TMemPage)(unsafe.Pointer(pNew2)).Fleaf != 0) {
			libc.Xmemcpy(tls, (*TMemPage)(unsafe.Pointer(pNew2)).FaData+8, pCell1, uint32(4))
		} else {
			if leafData != 0 {
				j = j - 1
				(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pNew2)).FxParseCell})))(tls, pNew2, **(**uintptr)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FapCell + uintptr(j)*4)), bp+120)
				pCell1 = pTemp1
				sz2 = int32(4) + _sqlite3PutVarint(tls, pCell1+4, uint64((**(**TCellInfo)(__ccgo_up(bp + 120))).FnKey))
				pTemp1 = uintptr(0)
			} else {
				pCell1 = pCell1 - uintptr(4)
				/* Obscure case for non-leaf-data trees: If the cell at pCell was
				 ** previously stored on a leaf node, and its reported size was 4
				 ** bytes, then it may actually be smaller than this
				 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
				 ** any cell). But it is important to pass the correct size to
				 ** insertCell(), so reparse the cell now.
				 **
				 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
				 ** and WITHOUT ROWID tables with exactly one column which is the
				 ** primary key.
				 */
				if int32(**(**Tu16)(__ccgo_up((**(**TCellArray)(__ccgo_up(bp + 48))).FszCell + uintptr(j)*2))) == int32(4) {
					sz2 = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pParent)).FxCellSize})))(tls, pParent, pCell1))
				}
			}
		}
		iOvflSpace = iOvflSpace + sz2
		k = 0
		for {
			if !(**(**int32)(__ccgo_up(bp + 48 + 40 + uintptr(k)*4)) <= j) {
				break
			}
			goto _27
		_27:
			;
			k = k + 1
		}
		pSrcEnd = **(**uintptr)(__ccgo_up(bp + 48 + 16 + uintptr(k)*4))
		if uint32(pCell1) < uint32(pSrcEnd) && uint32(pCell1+uintptr(sz2)) > uint32(pSrcEnd) {
			**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(82108))
			goto balance_cleanup
		}
		**(**int32)(__ccgo_up(bp)) = _insertCell(tls, pParent, nxDiv+i, pCell1, sz2, pTemp1, (*TMemPage)(unsafe.Pointer(pNew2)).Fpgno)
		if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
			goto balance_cleanup
		}
		goto _26
	_26:
		;
		i = i + 1
	}
	/* Now update the actual sibling pages. The order in which they are updated
	 ** is important, as this code needs to avoid disrupting any page from which
	 ** cells may still to be read. In practice, this means:
	 **
	 **  (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
	 **      then it is not safe to update page apNew[iPg] until after
	 **      the left-hand sibling apNew[iPg-1] has been updated.
	 **
	 **  (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
	 **      then it is not safe to update page apNew[iPg] until after
	 **      the right-hand sibling apNew[iPg+1] has been updated.
	 **
	 ** If neither of the above apply, the page is safe to update.
	 **
	 ** The iPg value in the following loop starts at nNew-1 goes down
	 ** to 0, then back up to nNew-1 again, thus making two passes over
	 ** the pages.  On the initial downward pass, only condition (1) above
	 ** needs to be tested because (2) will always be true from the previous
	 ** step.  On the upward pass, both conditions are always true, so the
	 ** upwards pass simply processes pages that were missed on the downward
	 ** pass.
	 */
	i = int32(1) - nNew
	for {
		if !(i < nNew) {
			break
		}
		if i < 0 {
			v1 = -i
		} else {
			v1 = i
		}
		iPg = v1
		if (**(**[5]Tu8)(__ccgo_up(bp + 40)))[iPg] != 0 {
			goto _28
		} /* Skip pages already processed */
		if i >= 0 || cntOld[iPg-int32(1)] >= cntNew[iPg-int32(1)] {
			/* Verify condition (1):  If cells are moving left, update iPg
			 ** only after iPg-1 has already been updated. */
			/* Verify condition (2):  If cells are moving right, update iPg
			 ** only after iPg+1 has already been updated. */
			if iPg == 0 {
				v1 = libc.Int32FromInt32(0)
				iOld1 = v1
				iNew1 = v1
				nNewCell = cntNew[0]
			} else {
				if iPg < nOld {
					v1 = cntOld[iPg-int32(1)] + libc.BoolInt32(!(leafData != 0))
				} else {
					v1 = (**(**TCellArray)(__ccgo_up(bp + 48))).FnCell
				}
				iOld1 = v1
				iNew1 = cntNew[iPg-int32(1)] + libc.BoolInt32(!(leafData != 0))
				nNewCell = cntNew[iPg] - iNew1
			}
			**(**int32)(__ccgo_up(bp)) = _editPage(tls, apNew[iPg], iOld1, iNew1, nNewCell, bp+48)
			if **(**int32)(__ccgo_up(bp)) != 0 {
				goto balance_cleanup
			}
			(**(**[5]Tu8)(__ccgo_up(bp + 40)))[iPg] = (**(**[5]Tu8)(__ccgo_up(bp + 40)))[iPg] + 1
			(*TMemPage)(unsafe.Pointer(apNew[iPg])).FnFree = usableSpace - (**(**[5]int32)(__ccgo_up(bp + 16)))[iPg]
		}
		goto _28
	_28:
		;
		i = i + 1
	}
	/* All pages have been processed exactly once */
	if isRoot != 0 && int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell) == 0 && int32((*TMemPage)(unsafe.Pointer(pParent)).FhdrOffset) <= (*TMemPage)(unsafe.Pointer(apNew[0])).FnFree {
		/* The root page of the b-tree now contains no cells. The only sibling
		 ** page is the right-child of the parent. Copy the contents of the
		 ** child page into the parent, decreasing the overall height of the
		 ** b-tree structure by one. This is described as the "balance-shallower"
		 ** sub-algorithm in some documentation.
		 **
		 ** If this is an auto-vacuum database, the call to copyNodeContent()
		 ** sets all pointer-map entries corresponding to database image pages
		 ** for which the pointer is stored within the content being copied.
		 **
		 ** It is critical that the child page be defragmented before being
		 ** copied into the parent, because if the parent is page 1 then it will
		 ** by smaller than the child due to the database header, and so all the
		 ** free space needs to be up front.
		 */
		**(**int32)(__ccgo_up(bp)) = _defragmentPage(tls, apNew[0], -int32(1))
		_copyNodeContent(tls, apNew[0], pParent, bp)
		_freePage(tls, apNew[0], bp)
	} else {
		if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && !(leafCorrection != 0) {
			/* Fix the pointer map entries associated with the right-child of each
			 ** sibling page. All other pointer map entries have already been taken
			 ** care of.  */
			i = 0
			for {
				if !(i < nNew) {
					break
				}
				key = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(apNew[i])).FaData+8)
				_ptrmapPut(tls, pBt, key, uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(apNew[i])).Fpgno, bp)
				goto _32
			_32:
				;
				i = i + 1
			}
		}
	}
	/* Free any old pages that were not reused as new pages.
	 */
	i = nNew
	for {
		if !(i < nOld) {
			break
		}
		_freePage(tls, (**(**[3]uintptr)(__ccgo_up(bp + 4)))[i], bp)
		goto _33
	_33:
		;
		i = i + 1
	}
	/*
	 ** Cleanup before returning.
	 */
	goto balance_cleanup
balance_cleanup:
	;
	_sqlite3DbFree(tls, uintptr(0), (**(**TCellArray)(__ccgo_up(bp + 48))).FapCell)
	i = 0
	for {
		if !(i < nOld) {
			break
		}
		_releasePage(tls, (**(**[3]uintptr)(__ccgo_up(bp + 4)))[i])
		goto _34
	_34:
		;
		i = i + 1
	}
	i = 0
	for {
		if !(i < nNew) {
			break
		}
		_releasePage(tls, apNew[i])
		goto _35
	_35:
		;
		i = i + 1
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** This version of balance() handles the common special case where
//	** a new entry is being inserted on the extreme right-end of the
//	** tree, in other words, when the new entry will become the largest
//	** entry in the tree.
//	**
//	** Instead of trying to balance the 3 right-most leaf pages, just add
//	** a new page to the right-hand side and put the one new entry in
//	** that page.  This leaves the right side of the tree somewhat
//	** unbalanced.  But odds are that we will be inserting new entries
//	** at the end soon afterwards so the nearly empty page will quickly
//	** fill up.  On average.
//	**
//	** pPage is the leaf page which is the right-most page in the tree.
//	** pParent is its parent.  pPage must have a single overflow entry
//	** which is also the right-most entry on the page.
//	**
//	** The pSpace buffer is used to store a temporary copy of the divider
//	** cell that will be inserted into pParent. Such a cell consists of a 4
//	** byte page number followed by a variable length integer. In other
//	** words, at most 13 bytes. Hence the pSpace buffer must be at
//	** least 13 bytes in size.
//	*/
func _balance_quick(tls *libc.TLS, pParent uintptr, pPage uintptr, pSpace uintptr) (r int32) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var pBt, pOut, pStop, v1, v3 uintptr
	var v2 Tu8
	var _ /* b at bp+20 */ TCellArray
	var _ /* pCell at bp+12 */ uintptr
	var _ /* pNew at bp+0 */ uintptr
	var _ /* pgnoNew at bp+8 */ TPgno
	var _ /* rc at bp+4 */ int32
	var _ /* szCell at bp+16 */ Tu16
	_, _, _, _, _, _ = pBt, pOut, pStop, v1, v2, v3
	pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt /* Page number of pNew */
	if int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) == 0 {
		return _sqlite3CorruptError(tls, int32(81243))
	} /* dbfuzz001.test */
	/* Allocate a new page. This page will become the right-sibling of
	 ** pPage. Make the parent page writable, so that the new divider cell
	 ** may be inserted. If both these operations are successful, proceed.
	 */
	**(**int32)(__ccgo_up(bp + 4)) = _allocateBtreePage(tls, pBt, bp, bp+8, uint32(0), uint8(0))
	if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
		pOut = pSpace + 4
		**(**uintptr)(__ccgo_up(bp + 12)) = **(**uintptr)(__ccgo_up(pPage + 36))
		**(**Tu16)(__ccgo_up(bp + 16)) = (*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxCellSize})))(tls, pPage, **(**uintptr)(__ccgo_up(bp + 12)))
		_zeroPage(tls, **(**uintptr)(__ccgo_up(bp)), libc.Int32FromInt32(PTF_INTKEY)|libc.Int32FromInt32(PTF_LEAFDATA)|libc.Int32FromInt32(PTF_LEAF))
		(**(**TCellArray)(__ccgo_up(bp + 20))).FnCell = int32(1)
		(**(**TCellArray)(__ccgo_up(bp + 20))).FpRef = pPage
		(**(**TCellArray)(__ccgo_up(bp + 20))).FapCell = bp + 12
		(**(**TCellArray)(__ccgo_up(bp + 20))).FszCell = bp + 16
		**(**uintptr)(__ccgo_up(bp + 20 + 16)) = (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd
		**(**int32)(__ccgo_up(bp + 20 + 40)) = int32(2)
		**(**int32)(__ccgo_up(bp + 20 + 40 + uintptr(libc.Int32FromInt32(NB)*libc.Int32FromInt32(2)-libc.Int32FromInt32(1))*4)) = int32(0x7fffffff)
		**(**int32)(__ccgo_up(bp + 4)) = _rebuildPage(tls, bp+20, 0, int32(1), **(**uintptr)(__ccgo_up(bp)))
		if **(**int32)(__ccgo_up(bp + 4)) != 0 {
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
			return **(**int32)(__ccgo_up(bp + 4))
		}
		(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnFree = int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FcellOffset) - uint32(2) - uint32(**(**Tu16)(__ccgo_up(bp + 16))))
		/* If this is an auto-vacuum database, update the pointer map
		 ** with entries for the new page, and any pointer from the
		 ** cell on the page to an overflow page. If either of these
		 ** operations fails, the return code is set, but the contents
		 ** of the parent page are still manipulated by the code below.
		 ** That is Ok, at this point the parent page is guaranteed to
		 ** be marked as dirty. Returning an error code will cause a
		 ** rollback, undoing any changes made to the parent page.
		 */
		if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
			_ptrmapPut(tls, pBt, **(**TPgno)(__ccgo_up(bp + 8)), uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(pParent)).Fpgno, bp+4)
			if int32(**(**Tu16)(__ccgo_up(bp + 16))) > int32((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FminLocal) {
				_ptrmapPutOvflPtr(tls, **(**uintptr)(__ccgo_up(bp)), **(**uintptr)(__ccgo_up(bp)), **(**uintptr)(__ccgo_up(bp + 12)), bp+4)
			}
		}
		/* Create a divider cell to insert into pParent. The divider cell
		 ** consists of a 4-byte page number (the page number of pPage) and
		 ** a variable length key value (which must be the same value as the
		 ** largest key on pPage).
		 **
		 ** To find the largest key value on pPage, first find the right-most
		 ** cell on pPage. The first two fields of this cell are the
		 ** record-length (a variable length integer at most 32-bits in size)
		 ** and the key value (a variable length integer, may have any value).
		 ** The first of the while(...) loops below skips over the record-length
		 ** field. The second while(...) loop copies the key value from the
		 ** cell on pPage into the pSpace buffer.
		 */
		**(**uintptr)(__ccgo_up(bp + 12)) = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-int32(1))))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-int32(1))) + 1)))))
		pStop = **(**uintptr)(__ccgo_up(bp + 12)) + 9
		for {
			v1 = **(**uintptr)(__ccgo_up(bp + 12))
			**(**uintptr)(__ccgo_up(bp + 12)) = **(**uintptr)(__ccgo_up(bp + 12)) + 1
			if !(int32(**(**Tu8)(__ccgo_up(v1)))&int32(0x80) != 0 && **(**uintptr)(__ccgo_up(bp + 12)) < pStop) {
				break
			}
		}
		pStop = **(**uintptr)(__ccgo_up(bp + 12)) + 9
		for {
			v1 = **(**uintptr)(__ccgo_up(bp + 12))
			**(**uintptr)(__ccgo_up(bp + 12)) = **(**uintptr)(__ccgo_up(bp + 12)) + 1
			v2 = **(**Tu8)(__ccgo_up(v1))
			v3 = pOut
			pOut = pOut + 1
			**(**Tu8)(__ccgo_up(v3)) = v2
			if !(int32(v2)&int32(0x80) != 0 && **(**uintptr)(__ccgo_up(bp + 12)) < pStop) {
				break
			}
		}
		/* Insert the new divider cell into pParent. */
		if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp + 4)) = _insertCell(tls, pParent, int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell), pSpace, int32(pOut)-int32(pSpace), uintptr(0), (*TMemPage)(unsafe.Pointer(pPage)).Fpgno)
		}
		/* Set the right-child pointer of pParent to point to the new page. */
		_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pParent)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pParent)).FhdrOffset)+int32(8)), **(**TPgno)(__ccgo_up(bp + 8)))
		/* Release the reference to the new page. */
		_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	return **(**int32)(__ccgo_up(bp + 4))
}

// C documentation
//
//	/*
//	** This is the default collating function named "BINARY" which is always
//	** available.
//	*/
func _binCollFunc(tls *libc.TLS, NotUsed uintptr, nKey1 int32, pKey1 uintptr, nKey2 int32, pKey2 uintptr) (r int32) {
	var n, rc, v1 int32
	_, _, _ = n, rc, v1
	_ = NotUsed
	if nKey1 < nKey2 {
		v1 = nKey1
	} else {
		v1 = nKey2
	}
	n = v1
	/* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
	 ** strings byte by byte using the memcmp() function from the standard C
	 ** library. */
	rc = libc.Xmemcmp(tls, pKey1, pKey2, uint32(n))
	if rc == 0 {
		rc = nKey1 - nKey2
	}
	return rc
}

// C documentation
//
//	/*
//	** Bind a text or BLOB value.
//	*/
func _bindText(tls *libc.TLS, pStmt uintptr, i int32, zData uintptr, nData Ti64, __ccgo_fp_xDel uintptr, encoding Tu8) (r int32) {
	var p, pVar, v1 uintptr
	var rc int32
	_, _, _, _ = p, pVar, rc, v1
	p = pStmt
	rc = _vdbeUnbind(tls, p, uint32(i-libc.Int32FromInt32(1)))
	if rc == SQLITE_OK {
		/* tag-20240917-01 */
		if zData != uintptr(0) {
			pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr(i-int32(1))*40
			if int32(encoding) == int32(SQLITE_UTF8) {
				rc = _sqlite3VdbeMemSetText(tls, pVar, zData, nData, __ccgo_fp_xDel)
			} else {
				if int32(encoding) == int32(SQLITE_UTF8_ZT) {
					/* It is usually consider improper to assert() on an input.
					 ** However, the following assert() is checking for inputs
					 ** that are documented to result in undefined behavior. */
					rc = _sqlite3VdbeMemSetText(tls, pVar, zData, nData, __ccgo_fp_xDel)
					v1 = pVar + 16
					*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
				} else {
					rc = _sqlite3VdbeMemSetStr(tls, pVar, zData, nData, encoding, __ccgo_fp_xDel)
					if int32(encoding) == 0 {
						(*TMem)(unsafe.Pointer(pVar)).Fenc = (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fenc
					}
				}
			}
			if rc == SQLITE_OK && int32(encoding) != 0 {
				rc = _sqlite3VdbeChangeEncoding(tls, pVar, int32((*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fenc))
			}
			if rc != 0 {
				_sqlite3Error(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, rc)
				rc = _sqlite3ApiExit(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, rc)
			}
		}
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
	} else {
		if __ccgo_fp_xDel != libc.UintptrFromInt32(0) && __ccgo_fp_xDel != uintptr(-libc.Int32FromInt32(1)) {
			(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xDel})))(tls, zData)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is used by both blob_open() and blob_reopen(). It seeks
//	** the b-tree cursor associated with blob handle p to point to row iRow.
//	** If successful, SQLITE_OK is returned and subsequent calls to
//	** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
//	**
//	** If an error occurs, or if the specified row does not exist or does not
//	** contain a value of type TEXT or BLOB in the column nominated when the
//	** blob handle was opened, then an error code is returned and *pzErr may
//	** be set to point to a buffer containing an error message. It is the
//	** responsibility of the caller to free the error message buffer using
//	** sqlite3DbFree().
//	**
//	** If an error does occur, then the b-tree cursor is closed. All subsequent
//	** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
//	** immediately return SQLITE_ABORT.
//	*/
func _blobSeekToRow(tls *libc.TLS, p uintptr, iRow Tsqlite3_int64, pzErr uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pC, v, zErr, v2, v3 uintptr
	var rc int32
	var type1 Tu32
	var v1 uint32
	_, _, _, _, _, _, _, _ = pC, rc, type1, v, zErr, v1, v2, v3 /* Error code */
	zErr = uintptr(0)                                           /* Error message */
	v = (*TIncrblob)(unsafe.Pointer(p)).FpStmt
	/* Set the value of register r[1] in the SQL statement to integer iRow.
	 ** This is done directly as a performance optimization
	 */
	_sqlite3VdbeMemSetInt64(tls, (*TVdbe)(unsafe.Pointer(v)).FaMem+1*40, iRow)
	/* If the statement has been run before (and is paused at the OP_ResultRow)
	 ** then back it up to the point where it does the OP_NotExists.  This could
	 ** have been down with an extra OP_Goto, but simply setting the program
	 ** counter is faster. */
	if (*TVdbe)(unsafe.Pointer(v)).Fpc > int32(4) {
		(*TVdbe)(unsafe.Pointer(v)).Fpc = int32(4)
		rc = _sqlite3VdbeExec(tls, v)
	} else {
		rc = Xsqlite3_step(tls, (*TIncrblob)(unsafe.Pointer(p)).FpStmt)
	}
	if rc == int32(SQLITE_ROW) {
		pC = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(v)).FapCsr))
		if int32((*TVdbeCursor)(unsafe.Pointer(pC)).FnHdrParsed) > int32((*TIncrblob)(unsafe.Pointer(p)).FiCol) {
			v1 = *(*Tu32)(unsafe.Pointer(pC + 84 + uintptr((*TIncrblob)(unsafe.Pointer(p)).FiCol)*4))
		} else {
			v1 = uint32(0)
		}
		type1 = v1
		if type1 < uint32(12) {
			if type1 == uint32(0) {
				v2 = __ccgo_ts + 1697
			} else {
				if type1 == uint32(7) {
					v3 = __ccgo_ts + 7704
				} else {
					v3 = __ccgo_ts + 7709
				}
				v2 = v3
			}
			zErr = _sqlite3MPrintf(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb, __ccgo_ts+7717, libc.VaList(bp+8, v2))
			rc = int32(SQLITE_ERROR)
			Xsqlite3_finalize(tls, (*TIncrblob)(unsafe.Pointer(p)).FpStmt)
			(*TIncrblob)(unsafe.Pointer(p)).FpStmt = uintptr(0)
		} else {
			(*TIncrblob)(unsafe.Pointer(p)).FiOffset = int32(*(*Tu32)(unsafe.Pointer(pC + 84 + uintptr(int32((*TIncrblob)(unsafe.Pointer(p)).FiCol)+int32((*TVdbeCursor)(unsafe.Pointer(pC)).FnField))*4)))
			(*TIncrblob)(unsafe.Pointer(p)).FnByte = int32(_sqlite3VdbeSerialTypeLen(tls, type1))
			(*TIncrblob)(unsafe.Pointer(p)).FpCsr = *(*uintptr)(unsafe.Pointer(pC + 36))
			_sqlite3BtreeIncrblobCursor(tls, (*TIncrblob)(unsafe.Pointer(p)).FpCsr)
		}
	}
	if rc == int32(SQLITE_ROW) {
		rc = SQLITE_OK
	} else {
		if (*TIncrblob)(unsafe.Pointer(p)).FpStmt != 0 {
			rc = Xsqlite3_finalize(tls, (*TIncrblob)(unsafe.Pointer(p)).FpStmt)
			(*TIncrblob)(unsafe.Pointer(p)).FpStmt = uintptr(0)
			if rc == SQLITE_OK {
				zErr = _sqlite3MPrintf(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb, __ccgo_ts+7746, libc.VaList(bp+8, iRow))
				rc = int32(SQLITE_ERROR)
			} else {
				zErr = _sqlite3MPrintf(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb, __ccgo_ts+4729, libc.VaList(bp+8, Xsqlite3_errmsg(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb)))
			}
		}
	}
	**(**uintptr)(__ccgo_up(pzErr)) = zErr
	return rc
}

// C documentation
//
//	/*
//	** Attempt to start a new transaction. A write-transaction
//	** is started if the second argument is nonzero, otherwise a read-
//	** transaction.  If the second argument is 2 or more and exclusive
//	** transaction is started, meaning that no other process is allowed
//	** to access the database.  A preexisting transaction may not be
//	** upgraded to exclusive by calling this routine a second time - the
//	** exclusivity flag only works for a new transaction.
//	**
//	** A write-transaction must be started before attempting any
//	** changes to the database.  None of the following routines
//	** will work unless a transaction is started first:
//	**
//	**      sqlite3BtreeCreateTable()
//	**      sqlite3BtreeCreateIndex()
//	**      sqlite3BtreeClearTable()
//	**      sqlite3BtreeDropTable()
//	**      sqlite3BtreeInsert()
//	**      sqlite3BtreeDelete()
//	**      sqlite3BtreeUpdateMeta()
//	**
//	** If an initial attempt to acquire the lock fails because of lock contention
//	** and the database was previously unlocked, then invoke the busy handler
//	** if there is one.  But if there was previously a read-lock, do not
//	** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is
//	** returned when there is already a read-lock in order to avoid a deadlock.
//	**
//	** Suppose there are two processes A and B.  A has a read lock and B has
//	** a reserved lock.  B tries to promote to exclusive but is blocked because
//	** of A's read lock.  A tries to promote to reserved but is blocked by B.
//	** One or the other of the two processes must give way or there can be
//	** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
//	** when A already has a read lock, we encourage A to give up and let B
//	** proceed.
//	*/
func _btreeBeginTrans(tls *libc.TLS, p uintptr, wrflag int32, pSchemaVersion uintptr) (r int32) {
	var pBlock, pBt, pIter, pPage1, pPager, v1 uintptr
	var rc, v5 int32
	var v6 bool
	_, _, _, _, _, _, _, _, _ = pBlock, pBt, pIter, pPage1, pPager, rc, v1, v5, v6
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt
	pPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager
	rc = SQLITE_OK
	_sqlite3BtreeEnter(tls, p)
	/* If the btree is already in a write-transaction, or it
	 ** is already in a read-transaction and a read-transaction
	 ** is requested, this is a no-op.
	 */
	if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_WRITE) || int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_READ) && !(wrflag != 0) {
		goto trans_begun
	}
	if (*Tsqlite3)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).Fdb)).Fflags&uint64(SQLITE_ResetDatabase) != 0 && int32(_sqlite3PagerIsreadonly(tls, pPager)) == 0 {
		v1 = pBt + 24
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(BTS_READ_ONLY))
	}
	/* Write transactions are not possible on a read-only database */
	if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_READ_ONLY) != 0 && wrflag != 0 {
		rc = int32(SQLITE_READONLY)
		goto trans_begun
	}
	pBlock = uintptr(0)
	/* If another database handle has already opened a write transaction
	 ** on this shared-btree structure and a second write transaction is
	 ** requested, return SQLITE_LOCKED.
	 */
	if wrflag != 0 && int32((*TBtShared)(unsafe.Pointer(pBt)).FinTransaction) == int32(TRANS_WRITE) || int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_PENDING) != 0 {
		pBlock = (*TBtree)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpWriter)).Fdb
	} else {
		if wrflag > int32(1) {
			pIter = (*TBtShared)(unsafe.Pointer(pBt)).FpLock
			for {
				if !(pIter != 0) {
					break
				}
				if (*TBtLock)(unsafe.Pointer(pIter)).FpBtree != p {
					pBlock = (*TBtree)(unsafe.Pointer((*TBtLock)(unsafe.Pointer(pIter)).FpBtree)).Fdb
					break
				}
				goto _2
			_2:
				;
				pIter = (*TBtLock)(unsafe.Pointer(pIter)).FpNext
			}
		}
	}
	if pBlock != 0 {
		_sqlite3ConnectionBlocked(tls, (*TBtree)(unsafe.Pointer(p)).Fdb, pBlock)
		rc = libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		goto trans_begun
	}
	/* Any read-only or read-write transaction implies a read-lock on
	 ** page 1. So if some other shared-cache client already has a write-lock
	 ** on page 1, the transaction cannot be opened. */
	rc = _querySharedCacheTableLock(tls, p, uint32(SCHEMA_ROOT), uint8(READ_LOCK))
	if SQLITE_OK != rc {
		goto trans_begun
	}
	v1 = pBt + 24
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(BTS_INITIALLY_EMPTY))
	if (*TBtShared)(unsafe.Pointer(pBt)).FnPage == uint32(0) {
		v1 = pBt + 24
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTS_INITIALLY_EMPTY))
	}
	for cond := true; cond; cond = rc&int32(0xFF) == int32(SQLITE_BUSY) && int32((*TBtShared)(unsafe.Pointer(pBt)).FinTransaction) == TRANS_NONE && _btreeInvokeBusyHandler(tls, pBt) != 0 {
		/* Call lockBtree() until either pBt->pPage1 is populated or
		 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
		 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
		 ** reading page 1 it discovers that the page-size of the database
		 ** file is not pBt->pageSize. In this case lockBtree() will update
		 ** pBt->pageSize to the page-size of the file on disk.
		 */
		for {
			if v6 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 == uintptr(0); v6 {
				v5 = _lockBtree(tls, pBt)
				rc = v5
			}
			if !(v6 && SQLITE_OK == v5) {
				break
			}
		}
		if rc == SQLITE_OK && wrflag != 0 {
			if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_READ_ONLY) != 0 {
				rc = int32(SQLITE_READONLY)
			} else {
				rc = _sqlite3PagerBegin(tls, pPager, libc.BoolInt32(wrflag > int32(1)), _sqlite3TempInMemory(tls, (*TBtree)(unsafe.Pointer(p)).Fdb))
				if rc == SQLITE_OK {
					rc = _newDatabase(tls, pBt)
				} else {
					if rc == libc.Int32FromInt32(SQLITE_BUSY)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8) && int32((*TBtShared)(unsafe.Pointer(pBt)).FinTransaction) == TRANS_NONE {
						/* if there was no transaction opened when this function was
						 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
						 ** code to SQLITE_BUSY. */
						rc = int32(SQLITE_BUSY)
					}
				}
			}
		}
		if rc != SQLITE_OK {
			_ = SQLITE_OK
			_unlockBtreeIfUnused(tls, pBt)
		}
	}
	if rc == SQLITE_OK {
		if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == TRANS_NONE {
			(*TBtShared)(unsafe.Pointer(pBt)).FnTransaction = (*TBtShared)(unsafe.Pointer(pBt)).FnTransaction + 1
			if (*TBtree)(unsafe.Pointer(p)).Fsharable != 0 {
				(*TBtree)(unsafe.Pointer(p)).Flock.FeLock = uint8(READ_LOCK)
				(*TBtree)(unsafe.Pointer(p)).Flock.FpNext = (*TBtShared)(unsafe.Pointer(pBt)).FpLock
				(*TBtShared)(unsafe.Pointer(pBt)).FpLock = p + 32
			}
		}
		if wrflag != 0 {
			v5 = int32(TRANS_WRITE)
		} else {
			v5 = int32(TRANS_READ)
		}
		(*TBtree)(unsafe.Pointer(p)).FinTrans = uint8(v5)
		if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) > int32((*TBtShared)(unsafe.Pointer(pBt)).FinTransaction) {
			(*TBtShared)(unsafe.Pointer(pBt)).FinTransaction = (*TBtree)(unsafe.Pointer(p)).FinTrans
		}
		if wrflag != 0 {
			pPage1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1
			(*TBtShared)(unsafe.Pointer(pBt)).FpWriter = p
			v1 = pBt + 24
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(BTS_EXCLUSIVE))
			if wrflag > int32(1) {
				v1 = pBt + 24
				*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTS_EXCLUSIVE))
			}
			/* If the db-size header field is incorrect (as it may be if an old
			 ** client has been writing the database file), update it now. Doing
			 ** this sooner rather than later means the database size can safely
			 ** re-read the database size from page 1 if a savepoint or transaction
			 ** rollback occurs within the transaction.
			 */
			if (*TBtShared)(unsafe.Pointer(pBt)).FnPage != _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+28) {
				rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FpDbPage)
				if rc == SQLITE_OK {
					_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+28, (*TBtShared)(unsafe.Pointer(pBt)).FnPage)
				}
			}
		}
	}
	goto trans_begun
trans_begun:
	;
	if rc == SQLITE_OK {
		if pSchemaVersion != 0 {
			**(**int32)(__ccgo_up(pSchemaVersion)) = int32(_sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+40))
		}
		if wrflag != 0 {
			/* This call makes sure that the pager has the correct number of
			 ** open savepoints. If the second parameter is greater than 0 and
			 ** the sub-journal is not already open, then it will be opened here.
			 */
			rc = _sqlite3PagerOpenSavepoint(tls, pPager, (*Tsqlite3)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).Fdb)).FnSavepoint)
		}
	}
	_sqlite3BtreeLeave(tls, p)
	return rc
}

// C documentation
//
//	/*
//	** Create a new BTree table.  Write into *piTable the page
//	** number for the root page of the new table.
//	**
//	** The type of type is determined by the flags parameter.  Only the
//	** following values of flags are currently in use.  Other values for
//	** flags might not work:
//	**
//	**     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
//	**     BTREE_ZERODATA                  Used for SQL indices
//	*/
func _btreeCreateTable(tls *libc.TLS, p uintptr, piTable uintptr, createTabFlags int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pBt uintptr
	var ptfFlags int32
	var _ /* eType at bp+20 */ Tu8
	var _ /* iPtrPage at bp+24 */ TPgno
	var _ /* pPageMove at bp+16 */ uintptr
	var _ /* pRoot at bp+0 */ uintptr
	var _ /* pgnoMove at bp+12 */ TPgno
	var _ /* pgnoRoot at bp+4 */ TPgno
	var _ /* rc at bp+8 */ int32
	_, _ = pBt, ptfFlags
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt                 /* Page-type flags for the root page of new table */
	if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { /* The page to move to. */
		/* Creating a new table may probably require moving an existing database
		 ** to make room for the new tables root page. In case this page turns
		 ** out to be an overflow page, delete all overflow page-map caches
		 ** held by open cursors.
		 */
		_invalidateAllOverflowCache(tls, pBt)
		/* Read the value of meta[3] from the database to determine where the
		 ** root page of the new table should go. meta[3] is the largest root-page
		 ** created so far, so the new root-page is (meta[3]+1).
		 */
		_sqlite3BtreeGetMeta(tls, p, int32(BTREE_LARGEST_ROOT_PAGE), bp+4)
		if **(**TPgno)(__ccgo_up(bp + 4)) > _btreePagecount(tls, pBt) {
			return _sqlite3CorruptError(tls, int32(83314))
		}
		**(**TPgno)(__ccgo_up(bp + 4)) = **(**TPgno)(__ccgo_up(bp + 4)) + 1
		/* The new root-page may not be allocated on a pointer-map page, or the
		 ** PENDING_BYTE page.
		 */
		for **(**TPgno)(__ccgo_up(bp + 4)) == _ptrmapPageno(tls, pBt, **(**TPgno)(__ccgo_up(bp + 4))) || **(**TPgno)(__ccgo_up(bp + 4)) == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) {
			**(**TPgno)(__ccgo_up(bp + 4)) = **(**TPgno)(__ccgo_up(bp + 4)) + 1
		}
		/* Allocate a page. The page that currently resides at pgnoRoot will
		 ** be moved to the allocated page (unless the allocated page happens
		 ** to reside at pgnoRoot).
		 */
		**(**int32)(__ccgo_up(bp + 8)) = _allocateBtreePage(tls, pBt, bp+16, bp+12, **(**TPgno)(__ccgo_up(bp + 4)), uint8(BTALLOC_EXACT))
		if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
			return **(**int32)(__ccgo_up(bp + 8))
		}
		if **(**TPgno)(__ccgo_up(bp + 12)) != **(**TPgno)(__ccgo_up(bp + 4)) {
			/* pgnoRoot is the page that will be used for the root-page of
			 ** the new table (assuming an error did not occur). But we were
			 ** allocated pgnoMove. If required (i.e. if it was not allocated
			 ** by extending the file), the current page at position pgnoMove
			 ** is already journaled.
			 */
			**(**Tu8)(__ccgo_up(bp + 20)) = uint8(0)
			**(**TPgno)(__ccgo_up(bp + 24)) = uint32(0)
			/* Save the positions of any open cursors. This is required in
			 ** case they are holding a reference to an xFetch reference
			 ** corresponding to page pgnoRoot.  */
			**(**int32)(__ccgo_up(bp + 8)) = _saveAllCursors(tls, pBt, uint32(0), uintptr(0))
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 16)))
			if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
				return **(**int32)(__ccgo_up(bp + 8))
			}
			/* Move the page currently at pgnoRoot to pgnoMove. */
			**(**int32)(__ccgo_up(bp + 8)) = _btreeGetPage(tls, pBt, **(**TPgno)(__ccgo_up(bp + 4)), bp, 0)
			if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
				return **(**int32)(__ccgo_up(bp + 8))
			}
			**(**int32)(__ccgo_up(bp + 8)) = _ptrmapGet(tls, pBt, **(**TPgno)(__ccgo_up(bp + 4)), bp+20, bp+24)
			if int32(**(**Tu8)(__ccgo_up(bp + 20))) == int32(PTRMAP_ROOTPAGE) || int32(**(**Tu8)(__ccgo_up(bp + 20))) == int32(PTRMAP_FREEPAGE) {
				**(**int32)(__ccgo_up(bp + 8)) = _sqlite3CorruptError(tls, int32(83362))
			}
			if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
				_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
				return **(**int32)(__ccgo_up(bp + 8))
			}
			**(**int32)(__ccgo_up(bp + 8)) = _relocatePage(tls, pBt, **(**uintptr)(__ccgo_up(bp)), **(**Tu8)(__ccgo_up(bp + 20)), **(**TPgno)(__ccgo_up(bp + 24)), **(**TPgno)(__ccgo_up(bp + 12)), 0)
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
			/* Obtain the page at pgnoRoot */
			if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
				return **(**int32)(__ccgo_up(bp + 8))
			}
			**(**int32)(__ccgo_up(bp + 8)) = _btreeGetPage(tls, pBt, **(**TPgno)(__ccgo_up(bp + 4)), bp, 0)
			if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
				return **(**int32)(__ccgo_up(bp + 8))
			}
			**(**int32)(__ccgo_up(bp + 8)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpDbPage)
			if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
				_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
				return **(**int32)(__ccgo_up(bp + 8))
			}
		} else {
			**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp + 16))
		}
		/* Update the pointer-map and meta-data with the new root-page number. */
		_ptrmapPut(tls, pBt, **(**TPgno)(__ccgo_up(bp + 4)), uint8(PTRMAP_ROOTPAGE), uint32(0), bp+8)
		if **(**int32)(__ccgo_up(bp + 8)) != 0 {
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
			return **(**int32)(__ccgo_up(bp + 8))
		}
		/* When the new root page was allocated, page 1 was made writable in
		 ** order either to increase the database filesize, or to decrement the
		 ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
		 */
		**(**int32)(__ccgo_up(bp + 8)) = _sqlite3BtreeUpdateMeta(tls, p, int32(4), **(**TPgno)(__ccgo_up(bp + 4)))
		if **(**int32)(__ccgo_up(bp + 8)) != 0 {
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
			return **(**int32)(__ccgo_up(bp + 8))
		}
	} else {
		**(**int32)(__ccgo_up(bp + 8)) = _allocateBtreePage(tls, pBt, bp, bp+4, uint32(1), uint8(0))
		if **(**int32)(__ccgo_up(bp + 8)) != 0 {
			return **(**int32)(__ccgo_up(bp + 8))
		}
	}
	if createTabFlags&int32(BTREE_INTKEY) != 0 {
		ptfFlags = libc.Int32FromInt32(PTF_INTKEY) | libc.Int32FromInt32(PTF_LEAFDATA) | libc.Int32FromInt32(PTF_LEAF)
	} else {
		ptfFlags = libc.Int32FromInt32(PTF_ZERODATA) | libc.Int32FromInt32(PTF_LEAF)
	}
	_zeroPage(tls, **(**uintptr)(__ccgo_up(bp)), ptfFlags)
	_sqlite3PagerUnref(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpDbPage)
	**(**TPgno)(__ccgo_up(piTable)) = **(**TPgno)(__ccgo_up(bp + 4))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Erase all information in a table and add the root of the table to
//	** the freelist.  Except, the root of the principle table (the one on
//	** page 1) is never added to the freelist.
//	**
//	** This routine will fail with SQLITE_LOCKED if there are any open
//	** cursors on the table.
//	**
//	** If AUTOVACUUM is enabled and the page at iTable is not the last
//	** root page in the database file, then the last root page
//	** in the database file is moved into the slot formerly occupied by
//	** iTable and that last slot formerly occupied by the last root page
//	** is added to the freelist instead of iTable.  In this say, all
//	** root pages are kept at the beginning of the database file, which
//	** is necessary for AUTOVACUUM to work right.  *piMoved is set to the
//	** page number that used to be the last root page in the file before
//	** the move.  If no page gets moved, *piMoved is set to 0.
//	** The last root page is recorded in meta[3] and the value of
//	** meta[3] is updated by this procedure.
//	*/
func _btreeDropTable(tls *libc.TLS, p uintptr, iTable TPgno, piMoved uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pBt uintptr
	var _ /* maxRootPgno at bp+8 */ TPgno
	var _ /* pMove at bp+12 */ uintptr
	var _ /* pPage at bp+4 */ uintptr
	var _ /* rc at bp+0 */ int32
	_ = pBt
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt
	if iTable > _btreePagecount(tls, pBt) {
		return _sqlite3CorruptError(tls, int32(83563))
	}
	**(**int32)(__ccgo_up(bp)) = _sqlite3BtreeClearTable(tls, p, int32(iTable), uintptr(0))
	if **(**int32)(__ccgo_up(bp)) != 0 {
		return **(**int32)(__ccgo_up(bp))
	}
	**(**int32)(__ccgo_up(bp)) = _btreeGetPage(tls, pBt, iTable, bp+4, 0)
	if **(**int32)(__ccgo_up(bp)) != 0 {
		_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 4)))
		return **(**int32)(__ccgo_up(bp))
	}
	**(**int32)(__ccgo_up(piMoved)) = 0
	if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
		_sqlite3BtreeGetMeta(tls, p, int32(BTREE_LARGEST_ROOT_PAGE), bp+8)
		if iTable == **(**TPgno)(__ccgo_up(bp + 8)) {
			/* If the table being dropped is the table with the largest root-page
			 ** number in the database, put the root page on the free list.
			 */
			_freePage(tls, **(**uintptr)(__ccgo_up(bp + 4)), bp)
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 4)))
			if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
				return **(**int32)(__ccgo_up(bp))
			}
		} else {
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 4)))
			**(**int32)(__ccgo_up(bp)) = _btreeGetPage(tls, pBt, **(**TPgno)(__ccgo_up(bp + 8)), bp+12, 0)
			if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
				return **(**int32)(__ccgo_up(bp))
			}
			**(**int32)(__ccgo_up(bp)) = _relocatePage(tls, pBt, **(**uintptr)(__ccgo_up(bp + 12)), uint8(PTRMAP_ROOTPAGE), uint32(0), iTable, 0)
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 12)))
			if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
				return **(**int32)(__ccgo_up(bp))
			}
			**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
			**(**int32)(__ccgo_up(bp)) = _btreeGetPage(tls, pBt, **(**TPgno)(__ccgo_up(bp + 8)), bp+12, 0)
			_freePage(tls, **(**uintptr)(__ccgo_up(bp + 12)), bp)
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 12)))
			if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
				return **(**int32)(__ccgo_up(bp))
			}
			**(**int32)(__ccgo_up(piMoved)) = int32(**(**TPgno)(__ccgo_up(bp + 8)))
		}
		/* Set the new 'max-root-page' value in the database header. This
		 ** is the old value less one, less one more if that happens to
		 ** be a root-page number, less one again if that is the
		 ** PENDING_BYTE_PAGE.
		 */
		**(**TPgno)(__ccgo_up(bp + 8)) = **(**TPgno)(__ccgo_up(bp + 8)) - 1
		for **(**TPgno)(__ccgo_up(bp + 8)) == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) || _ptrmapPageno(tls, pBt, **(**TPgno)(__ccgo_up(bp + 8))) == **(**TPgno)(__ccgo_up(bp + 8)) {
			**(**TPgno)(__ccgo_up(bp + 8)) = **(**TPgno)(__ccgo_up(bp + 8)) - 1
		}
		**(**int32)(__ccgo_up(bp)) = _sqlite3BtreeUpdateMeta(tls, p, int32(4), **(**TPgno)(__ccgo_up(bp + 8)))
	} else {
		_freePage(tls, **(**uintptr)(__ccgo_up(bp + 4)), bp)
		_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 4)))
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Advance the cursor to the next entry in the database.
//	** Return value:
//	**
//	**    SQLITE_OK        success
//	**    SQLITE_DONE      cursor is already pointing at the last element
//	**    otherwise        some kind of error occurred
//	**
//	** The main entry point is sqlite3BtreeNext().  That routine is optimized
//	** for the common case of merely incrementing the cell counter BtCursor.aiIdx
//	** to the next cell on the current page.  The (slower) btreeNext() helper
//	** routine is called when it is necessary to move to a different page or
//	** to restore the cursor.
//	**
//	** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
//	** cursor corresponds to an SQL index and this routine could have been
//	** skipped if the SQL index had been a unique index.  The F argument
//	** is a hint to the implement.  SQLite btree implementation does not use
//	** this hint, but COMDB2 does.
//	*/
func _btreeNext(tls *libc.TLS, pCur uintptr) (r int32) {
	var idx, rc, v1 int32
	var pPage, v3 uintptr
	var v2 Tu16
	_, _, _, _, _, _ = idx, pPage, rc, v1, v2, v3
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID {
		if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) {
			v1 = _btreeRestoreCursorPosition(tls, pCur)
		} else {
			v1 = SQLITE_OK
		}
		rc = v1
		if rc != SQLITE_OK {
			return rc
		}
		if int32(CURSOR_INVALID) == int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) {
			return int32(SQLITE_DONE)
		}
		if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == int32(CURSOR_SKIPNEXT) {
			(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_VALID)
			if (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext > 0 {
				return SQLITE_OK
			}
		}
	}
	pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
	v3 = pCur + 70
	*(*Tu16)(unsafe.Pointer(v3)) = *(*Tu16)(unsafe.Pointer(v3)) + 1
	v2 = *(*Tu16)(unsafe.Pointer(v3))
	idx = int32(v2)
	if _sqlite3FaultSim(tls, int32(412)) != 0 {
		(*TMemPage)(unsafe.Pointer(pPage)).FisInit = uint8(0)
	}
	if !((*TMemPage)(unsafe.Pointer(pPage)).FisInit != 0) {
		return _sqlite3CorruptError(tls, int32(79581))
	}
	if idx >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
		if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
			rc = _moveToChild(tls, pCur, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8))))
			if rc != 0 {
				return rc
			}
			return _moveToLeftmost(tls, pCur)
		}
		for cond := true; cond; cond = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
			if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) == 0 {
				(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID)
				return int32(SQLITE_DONE)
			}
			_moveToParent(tls, pCur)
			pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
		}
		if (*TMemPage)(unsafe.Pointer(pPage)).FintKey != 0 {
			return _sqlite3BtreeNext(tls, pCur, 0)
		} else {
			return SQLITE_OK
		}
	}
	if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 {
		return SQLITE_OK
	} else {
		return _moveToLeftmost(tls, pCur)
	}
	return r
}

// C documentation
//
//	/* Overwrite content from pX into pDest.  Only do the write if the
//	** content is different from what is already there.
//	*/
func _btreeOverwriteContent(tls *libc.TLS, pPage uintptr, pDest uintptr, pX uintptr, iOffset int32, iAmt int32) (r int32) {
	var i, nData, rc, rc1, rc2 int32
	_, _, _, _, _ = i, nData, rc, rc1, rc2
	nData = (*TBtreePayload)(unsafe.Pointer(pX)).FnData - iOffset
	if nData <= 0 {
		i = 0
		for {
			if !(i < iAmt && int32(**(**Tu8)(__ccgo_up(pDest + uintptr(i)))) == 0) {
				break
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if i < iAmt {
			rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage)
			if rc != 0 {
				return rc
			}
			libc.Xmemset(tls, pDest+uintptr(i), 0, uint32(iAmt-i))
		}
	} else {
		if nData < iAmt {
			/* Mixed read data and zeros at the end.  Make a recursive call
			 ** to write the zeros then fall through to write the real data */
			rc1 = _btreeOverwriteContent(tls, pPage, pDest+uintptr(nData), pX, iOffset+nData, iAmt-nData)
			if rc1 != 0 {
				return rc1
			}
			iAmt = nData
		}
		if libc.Xmemcmp(tls, pDest, (*TBtreePayload)(unsafe.Pointer(pX)).FpData+uintptr(iOffset), uint32(iAmt)) != 0 {
			rc2 = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage)
			if rc2 != 0 {
				return rc2
			}
			/* In a corrupt database, it is possible for the source and destination
			 ** buffers to overlap.  This is harmless since the database is already
			 ** corrupt but it does cause valgrind and ASAN warnings.  So use
			 ** memmove(). */
			libc.Xmemmove(tls, pDest, (*TBtreePayload)(unsafe.Pointer(pX)).FpData+uintptr(iOffset), uint32(iAmt))
		}
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This is common tail processing for btreeParseCellPtr() and
//	** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
//	** on a single B-tree page.  Make necessary adjustments to the CellInfo
//	** structure.
//	*/
func _btreeParseCellAdjustSizeForOverflow(tls *libc.TLS, pPage uintptr, pCell uintptr, pInfo uintptr) {
	var maxLocal, minLocal, surplus int32
	_, _, _ = maxLocal, minLocal, surplus /* Overflow payload available for local storage */
	minLocal = int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal)
	maxLocal = int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal)
	surplus = int32(uint32(minLocal) + ((*TCellInfo)(unsafe.Pointer(pInfo)).FnPayload-uint32(minLocal))%((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize-uint32(4)))
	if surplus <= maxLocal {
		(*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal = uint16(surplus)
	} else {
		(*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal = uint16(minLocal)
	}
	(*TCellInfo)(unsafe.Pointer(pInfo)).FnSize = uint16(int32(uint16(t__predefined_ptrdiff_t((*TCellInfo)(unsafe.Pointer(pInfo)).FpPayload+uintptr((*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal))-int32(pCell))) + int32(4))
}

func _btreeParseCellPtr(tls *libc.TLS, pPage uintptr, pCell uintptr, pInfo uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nPayload Tu64
	var pEnd, pIter, v1 uintptr
	var x, v2 Tu8
	var _ /* iKey at bp+0 */ Tu64
	_, _, _, _, _, _ = nPayload, pEnd, pIter, x, v1, v2 /* Extracted Key value */
	pIter = pCell
	/* The next block of code is equivalent to:
	 **
	 **     pIter += getVarint32(pIter, nPayload);
	 **
	 ** The code is inlined to avoid a function call.
	 */
	nPayload = uint64(**(**Tu8)(__ccgo_up(pIter)))
	if nPayload >= uint64(0x80) {
		pEnd = pIter + 8
		nPayload = nPayload & uint64(0x7f)
		for cond := true; cond; cond = int32(**(**Tu8)(__ccgo_up(pIter))) >= int32(0x80) && pIter < pEnd {
			pIter = pIter + 1
			v1 = pIter
			nPayload = nPayload<<libc.Int32FromInt32(7) | uint64(int32(**(**Tu8)(__ccgo_up(v1)))&libc.Int32FromInt32(0x7f))
		}
		nPayload = nPayload & uint64(0xffffffff)
	}
	pIter = pIter + 1
	/* The next block of code is equivalent to:
	 **
	 **     pIter += getVarint(pIter, (u64*)&pInfo->nKey);
	 **
	 ** The code is inlined and the loop is unrolled for performance.
	 ** This routine is a high-runner.
	 */
	**(**Tu64)(__ccgo_up(bp)) = uint64(**(**Tu8)(__ccgo_up(pIter)))
	if **(**Tu64)(__ccgo_up(bp)) >= uint64(0x80) {
		pIter = pIter + 1
		v1 = pIter
		v2 = **(**Tu8)(__ccgo_up(v1))
		x = v2
		**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(7) ^ uint64(v2)
		if int32(x) >= int32(0x80) {
			pIter = pIter + 1
			v1 = pIter
			v2 = **(**Tu8)(__ccgo_up(v1))
			x = v2
			**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(7) ^ uint64(v2)
			if int32(x) >= int32(0x80) {
				pIter = pIter + 1
				v1 = pIter
				v2 = **(**Tu8)(__ccgo_up(v1))
				x = v2
				**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(7) ^ uint64(0x10204000) ^ uint64(v2)
				if int32(x) >= int32(0x80) {
					pIter = pIter + 1
					v1 = pIter
					v2 = **(**Tu8)(__ccgo_up(v1))
					x = v2
					**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(7) ^ uint64(0x4000) ^ uint64(v2)
					if int32(x) >= int32(0x80) {
						pIter = pIter + 1
						v1 = pIter
						v2 = **(**Tu8)(__ccgo_up(v1))
						x = v2
						**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(7) ^ uint64(0x4000) ^ uint64(v2)
						if int32(x) >= int32(0x80) {
							pIter = pIter + 1
							v1 = pIter
							v2 = **(**Tu8)(__ccgo_up(v1))
							x = v2
							**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(7) ^ uint64(0x4000) ^ uint64(v2)
							if int32(x) >= int32(0x80) {
								pIter = pIter + 1
								v1 = pIter
								v2 = **(**Tu8)(__ccgo_up(v1))
								x = v2
								**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(7) ^ uint64(0x4000) ^ uint64(v2)
								if int32(x) >= int32(0x80) {
									pIter = pIter + 1
									v1 = pIter
									**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(8) ^ uint64(0x8000) ^ uint64(**(**Tu8)(__ccgo_up(v1)))
								}
							}
						}
					}
				}
			} else {
				**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp)) ^ uint64(0x204000)
			}
		} else {
			**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp)) ^ uint64(0x4000)
		}
	}
	pIter = pIter + 1
	(*TCellInfo)(unsafe.Pointer(pInfo)).FnKey = **(**Ti64)(__ccgo_up(bp))
	(*TCellInfo)(unsafe.Pointer(pInfo)).FnPayload = uint32(nPayload)
	(*TCellInfo)(unsafe.Pointer(pInfo)).FpPayload = pIter
	if nPayload <= uint64((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
		/* This is the (easy) common case where the entire payload fits
		 ** on the local page.  No overflow is required.
		 */
		(*TCellInfo)(unsafe.Pointer(pInfo)).FnSize = uint16(int32(uint16(nPayload)) + int32(uint16(int32(pIter)-int32(pCell))))
		if int32((*TCellInfo)(unsafe.Pointer(pInfo)).FnSize) < int32(4) {
			(*TCellInfo)(unsafe.Pointer(pInfo)).FnSize = uint16(4)
		}
		(*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal = uint16(nPayload)
	} else {
		_btreeParseCellAdjustSizeForOverflow(tls, pPage, pCell, pInfo)
	}
}

func _btreeParseCellPtrIndex(tls *libc.TLS, pPage uintptr, pCell uintptr, pInfo uintptr) {
	var nPayload Tu32
	var pEnd, pIter, v1 uintptr
	_, _, _, _ = nPayload, pEnd, pIter, v1 /* Number of bytes of cell payload */
	pIter = pCell + uintptr((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize)
	nPayload = uint32(**(**Tu8)(__ccgo_up(pIter)))
	if nPayload >= uint32(0x80) {
		pEnd = pIter + 8
		nPayload = nPayload & uint32(0x7f)
		for cond := true; cond; cond = int32(**(**Tu8)(__ccgo_up(pIter))) >= int32(0x80) && pIter < pEnd {
			pIter = pIter + 1
			v1 = pIter
			nPayload = nPayload<<libc.Int32FromInt32(7) | uint32(int32(**(**Tu8)(__ccgo_up(v1)))&libc.Int32FromInt32(0x7f))
		}
	}
	pIter = pIter + 1
	(*TCellInfo)(unsafe.Pointer(pInfo)).FnKey = int64(nPayload)
	(*TCellInfo)(unsafe.Pointer(pInfo)).FnPayload = nPayload
	(*TCellInfo)(unsafe.Pointer(pInfo)).FpPayload = pIter
	if nPayload <= uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
		/* This is the (easy) common case where the entire payload fits
		 ** on the local page.  No overflow is required.
		 */
		(*TCellInfo)(unsafe.Pointer(pInfo)).FnSize = uint16(int32(uint16(nPayload)) + int32(uint16(int32(pIter)-int32(pCell))))
		if int32((*TCellInfo)(unsafe.Pointer(pInfo)).FnSize) < int32(4) {
			(*TCellInfo)(unsafe.Pointer(pInfo)).FnSize = uint16(4)
		}
		(*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal = uint16(nPayload)
	} else {
		_btreeParseCellAdjustSizeForOverflow(tls, pPage, pCell, pInfo)
	}
}

// C documentation
//
//	/*
//	** Check to see if the FROM clause term pFrom has table-valued function
//	** arguments.  If it does, leave an error message in pParse and return
//	** non-zero, since pFrom is not allowed to be a table-valued function.
//	*/
func _cannotBeFunction(tls *libc.TLS, pParse uintptr, pFrom uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x8>>3) != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22266, libc.VaList(bp+8, (*TSrcItem)(unsafe.Pointer(pFrom)).FzName))
		return int32(1)
	}
	return 0
}

// C documentation
//
//	/*
//	** The following routines are implementations of the MemPage.xCellSize
//	** method.
//	**
//	** Compute the total number of bytes that a Cell needs in the cell
//	** data area of the btree-page.  The return number includes the cell
//	** data header and the local payload, but not any overflow page or
//	** the space used by the cell pointer.
//	**
//	** cellSizePtrNoPayload()    =>   table internal nodes
//	** cellSizePtrTableLeaf()    =>   table leaf nodes
//	** cellSizePtr()             =>   index internal nodes
//	** cellSizeIdxLeaf()         =>   index leaf nodes
//	*/
func _cellSizePtr(tls *libc.TLS, pPage uintptr, pCell uintptr) (r Tu16) {
	var minLocal int32
	var nSize Tu32
	var pEnd, pIter, v1 uintptr
	_, _, _, _, _ = minLocal, nSize, pEnd, pIter, v1
	pIter = pCell + uintptr(4) /* Size value to return */
	nSize = uint32(**(**Tu8)(__ccgo_up(pIter)))
	if nSize >= uint32(0x80) {
		pEnd = pIter + 8
		nSize = nSize & uint32(0x7f)
		for cond := true; cond; cond = int32(**(**Tu8)(__ccgo_up(pIter))) >= int32(0x80) && pIter < pEnd {
			pIter = pIter + 1
			v1 = pIter
			nSize = nSize<<libc.Int32FromInt32(7) | uint32(int32(**(**Tu8)(__ccgo_up(v1)))&libc.Int32FromInt32(0x7f))
		}
	}
	pIter = pIter + 1
	if nSize <= uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
		nSize = nSize + uint32(int32(pIter)-int32(pCell))
	} else {
		minLocal = int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal)
		nSize = uint32(minLocal) + (nSize-uint32(minLocal))%((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize-uint32(4))
		if nSize > uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
			nSize = uint32(minLocal)
		}
		nSize = nSize + uint32(int32(4)+int32(uint16(int32(pIter)-int32(pCell))))
	}
	return uint16(nSize)
}

func _cellSizePtrIdxLeaf(tls *libc.TLS, pPage uintptr, pCell uintptr) (r Tu16) {
	var minLocal int32
	var nSize Tu32
	var pEnd, pIter, v1 uintptr
	_, _, _, _, _ = minLocal, nSize, pEnd, pIter, v1
	pIter = pCell /* Size value to return */
	nSize = uint32(**(**Tu8)(__ccgo_up(pIter)))
	if nSize >= uint32(0x80) {
		pEnd = pIter + 8
		nSize = nSize & uint32(0x7f)
		for cond := true; cond; cond = int32(**(**Tu8)(__ccgo_up(pIter))) >= int32(0x80) && pIter < pEnd {
			pIter = pIter + 1
			v1 = pIter
			nSize = nSize<<libc.Int32FromInt32(7) | uint32(int32(**(**Tu8)(__ccgo_up(v1)))&libc.Int32FromInt32(0x7f))
		}
	}
	pIter = pIter + 1
	if nSize <= uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
		nSize = nSize + uint32(int32(pIter)-int32(pCell))
		if nSize < uint32(4) {
			nSize = uint32(4)
		}
	} else {
		minLocal = int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal)
		nSize = uint32(minLocal) + (nSize-uint32(minLocal))%((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize-uint32(4))
		if nSize > uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
			nSize = uint32(minLocal)
		}
		nSize = nSize + uint32(int32(4)+int32(uint16(int32(pIter)-int32(pCell))))
	}
	return uint16(nSize)
}

func _cellSizePtrNoPayload(tls *libc.TLS, pPage uintptr, pCell uintptr) (r Tu16) {
	var pEnd, pIter, v1 uintptr
	_, _, _ = pEnd, pIter, v1
	pIter = pCell + uintptr(4) /* End mark for a varint */
	_ = pPage
	pEnd = pIter + uintptr(9)
	for {
		v1 = pIter
		pIter = pIter + 1
		if !(int32(**(**Tu8)(__ccgo_up(v1)))&int32(0x80) != 0 && pIter < pEnd) {
			break
		}
	}
	return uint16(int32(pIter) - int32(pCell))
}

func _cellSizePtrTableLeaf(tls *libc.TLS, pPage uintptr, pCell uintptr) (r Tu16) {
	var minLocal int32
	var nSize Tu32
	var pEnd, pIter, v1, v11, v13, v2, v3, v5, v7, v9 uintptr
	var v10, v12, v14, v16, v4, v6, v8 bool
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = minLocal, nSize, pEnd, pIter, v1, v10, v11, v12, v13, v14, v16, v2, v3, v4, v5, v6, v7, v8, v9
	pIter = pCell /* Size value to return */
	nSize = uint32(**(**Tu8)(__ccgo_up(pIter)))
	if nSize >= uint32(0x80) {
		pEnd = pIter + 8
		nSize = nSize & uint32(0x7f)
		for cond := true; cond; cond = int32(**(**Tu8)(__ccgo_up(pIter))) >= int32(0x80) && pIter < pEnd {
			pIter = pIter + 1
			v1 = pIter
			nSize = nSize<<libc.Int32FromInt32(7) | uint32(int32(**(**Tu8)(__ccgo_up(v1)))&libc.Int32FromInt32(0x7f))
		}
	}
	pIter = pIter + 1
	/* pIter now points at the 64-bit integer key value, a variable length
	 ** integer. The following block moves pIter to point at the first byte
	 ** past the end of the key value. */
	v1 = pIter
	pIter = pIter + 1
	if v4 = int32(**(**Tu8)(__ccgo_up(v1)))&int32(0x80) != 0; v4 {
		v2 = pIter
		pIter = pIter + 1
	}
	if v6 = v4 && int32(**(**Tu8)(__ccgo_up(v2)))&int32(0x80) != 0; v6 {
		v3 = pIter
		pIter = pIter + 1
	}
	if v8 = v6 && int32(**(**Tu8)(__ccgo_up(v3)))&int32(0x80) != 0; v8 {
		v5 = pIter
		pIter = pIter + 1
	}
	if v10 = v8 && int32(**(**Tu8)(__ccgo_up(v5)))&int32(0x80) != 0; v10 {
		v7 = pIter
		pIter = pIter + 1
	}
	if v12 = v10 && int32(**(**Tu8)(__ccgo_up(v7)))&int32(0x80) != 0; v12 {
		v9 = pIter
		pIter = pIter + 1
	}
	if v14 = v12 && int32(**(**Tu8)(__ccgo_up(v9)))&int32(0x80) != 0; v14 {
		v11 = pIter
		pIter = pIter + 1
	}
	if v16 = v14 && int32(**(**Tu8)(__ccgo_up(v11)))&int32(0x80) != 0; v16 {
		v13 = pIter
		pIter = pIter + 1
	}
	if v16 && int32(**(**Tu8)(__ccgo_up(v13)))&int32(0x80) != 0 {
		pIter = pIter + 1
	}
	if nSize <= uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
		nSize = nSize + uint32(int32(pIter)-int32(pCell))
		if nSize < uint32(4) {
			nSize = uint32(4)
		}
	} else {
		minLocal = int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal)
		nSize = uint32(minLocal) + (nSize-uint32(minLocal))%((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize-uint32(4))
		if nSize > uint32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
			nSize = uint32(minLocal)
		}
		nSize = nSize + uint32(int32(4)+int32(uint16(int32(pIter)-int32(pCell))))
	}
	return uint16(nSize)
}

// C documentation
//
//	/*
//	** The char() function takes zero or more arguments, each of which is
//	** an integer.  It constructs a string where each character of the string
//	** is the unicode character for the corresponding integer argument.
//	*/
func _charFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var c uint32
	var i int32
	var x Tsqlite3_int64
	var z, zOut, v1 uintptr
	_, _, _, _, _, _ = c, i, x, z, zOut, v1
	v1 = Xsqlite3_malloc64(tls, uint64(argc*int32(4)+int32(1)))
	z = v1
	zOut = v1
	if z == uintptr(0) {
		Xsqlite3_result_error_nomem(tls, context)
		return
	}
	i = 0
	for {
		if !(i < argc) {
			break
		}
		x = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
		if x < 0 || x > int64(0x10ffff) {
			x = int64(0xfffd)
		}
		c = uint32(x & libc.Int64FromInt32(0x1fffff))
		if c < uint32(0x00080) {
			v1 = zOut
			zOut = zOut + 1
			**(**uint8)(__ccgo_up(v1)) = uint8(c & libc.Uint32FromInt32(0xFF))
		} else {
			if c < uint32(0x00800) {
				v1 = zOut
				zOut = zOut + 1
				**(**uint8)(__ccgo_up(v1)) = uint8(int32(0xC0) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1F))))
				v1 = zOut
				zOut = zOut + 1
				**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F))))
			} else {
				if c < uint32(0x10000) {
					v1 = zOut
					zOut = zOut + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(int32(0xE0) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0F))))
					v1 = zOut
					zOut = zOut + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
					v1 = zOut
					zOut = zOut + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F))))
				} else {
					v1 = zOut
					zOut = zOut + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(int32(0xF0) + int32(uint8(c>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07))))
					v1 = zOut
					zOut = zOut + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3F))))
					v1 = zOut
					zOut = zOut + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
					v1 = zOut
					zOut = zOut + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F))))
				}
			}
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	**(**uint8)(__ccgo_up(zOut)) = uint8(0)
	Xsqlite3_result_text64(tls, context, z, uint64(int32(zOut)-int32(z)), __ccgo_fp(Xsqlite3_free), uint8(SQLITE_UTF8_ZT))
}

// C documentation
//
//	/*
//	** Append a message to the error message string.
//	*/
func _checkAppendMsg(tls *libc.TLS, pCheck uintptr, zFormat uintptr, va uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var ap Tva_list
	_ = ap
	_checkProgress(tls, pCheck)
	if !((*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr != 0) {
		return
	}
	(*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr = (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr - 1
	(*TIntegrityCk)(unsafe.Pointer(pCheck)).FnErr = (*TIntegrityCk)(unsafe.Pointer(pCheck)).FnErr + 1
	ap = va
	if (*TIntegrityCk)(unsafe.Pointer(pCheck)).FerrMsg.FnChar != 0 {
		Xsqlite3_str_append(tls, pCheck+48, __ccgo_ts+5569, int32(1))
	}
	if (*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx != 0 {
		Xsqlite3_str_appendf(tls, pCheck+48, (*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx, libc.VaList(bp+8, (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv0, (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv1, (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv2))
	}
	Xsqlite3_str_vappendf(tls, pCheck+48, zFormat, ap)
	_ = ap
	if int32((*TIntegrityCk)(unsafe.Pointer(pCheck)).FerrMsg.FaccError) == int32(SQLITE_NOMEM) {
		_checkOom(tls, pCheck)
	}
}

// C documentation
//
//	/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
//	*  Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
//	** expression node references any of the
//	** columns that are being modified by an UPDATE statement.
//	*/
func _checkConstraintExprNode(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var v1 uintptr
	_ = v1
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) >= 0 {
			if **(**int32)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pWalker + 24)) + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)*4)) >= 0 {
				v1 = pWalker + 20
				*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(CKCNSTRNT_COLUMN))
			}
		} else {
			v1 = pWalker + 20
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(CKCNSTRNT_ROWID))
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Do various sanity checks on a single page of a tree.  Return
//	** the tree depth.  Root pages return 0.  Parents of root pages
//	** return 1, and so forth.
//	**
//	** These checks are done:
//	**
//	**      1.  Make sure that cells and freeblocks do not overlap
//	**          but combine to completely cover the page.
//	**      2.  Make sure integer cell keys are in order.
//	**      3.  Check the integrity of overflow pages.
//	**      4.  Recursively call checkTreePage on all children.
//	**      5.  Verify that the depth of all children is the same.
//	*/
func _checkTreePage(tls *libc.TLS, pCheck uintptr, iPage TPgno, piMinKey uintptr, _maxKey Ti64) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	*(*Ti64)(unsafe.Pointer(bp)) = _maxKey
	var cellStart, d2, depth, doCoverageCheck, hdr, i, j, keyCanBeEqual, nCell, nFrag, pgno, rc, saved_v1, saved_v2, size1, v1 int32
	var contentOffset, nPage, pc, prev, size, usableSize Tu32
	var data, heap, pBt, pCell, pCellIdx, saved_zPfx uintptr
	var pgnoOvfl TPgno
	var savedIsInit Tu8
	var _ /* info at bp+16 */ TCellInfo
	var _ /* pPage at bp+8 */ uintptr
	var _ /* x at bp+12 */ Tu32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = cellStart, contentOffset, d2, data, depth, doCoverageCheck, hdr, heap, i, j, keyCanBeEqual, nCell, nFrag, nPage, pBt, pCell, pCellIdx, pc, pgno, pgnoOvfl, prev, rc, savedIsInit, saved_v1, saved_v2, saved_zPfx, size, size1, usableSize, v1
	**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0) /* Result code from subroutine call */
	depth = -int32(1)                             /* Number of cells */
	doCoverageCheck = int32(1)                    /* True if cell coverage checking should be done */
	keyCanBeEqual = int32(1)                      /* Offset to the start of the cell content area */
	heap = uintptr(0)
	prev = uint32(0) /* Next and previous entry on the min-heap */
	saved_zPfx = (*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx
	saved_v1 = int32((*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv1)
	saved_v2 = (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv2
	savedIsInit = uint8(0)
	/* Check that the page exists
	 */
	_checkProgress(tls, pCheck)
	if (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr == 0 {
		goto end_of_check
	}
	pBt = (*TIntegrityCk)(unsafe.Pointer(pCheck)).FpBt
	usableSize = (*TBtShared)(unsafe.Pointer(pBt)).FusableSize
	if iPage == uint32(0) {
		return 0
	}
	if _checkRef(tls, pCheck, iPage) != 0 {
		return 0
	}
	(*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx = __ccgo_ts + 5815
	(*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv1 = iPage
	v1 = _btreeGetPage(tls, pBt, iPage, bp+8, 0)
	rc = v1
	if v1 != 0 {
		_checkAppendMsg(tls, pCheck, __ccgo_ts+5833, libc.VaList(bp+48, rc))
		if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<<libc.Int32FromInt32(8) {
			(*TIntegrityCk)(unsafe.Pointer(pCheck)).Frc = int32(SQLITE_NOMEM)
		}
		goto end_of_check
	}
	/* Clear MemPage.isInit to make sure the corruption detection code in
	 ** btreeInitPage() is executed.  */
	savedIsInit = (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FisInit
	(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FisInit = uint8(0)
	v1 = _btreeInitPage(tls, **(**uintptr)(__ccgo_up(bp + 8)))
	rc = v1
	if v1 != 0 {
		/* The only possible error from InitPage */
		_checkAppendMsg(tls, pCheck, __ccgo_ts+5871, libc.VaList(bp+48, rc))
		goto end_of_check
	}
	v1 = _btreeComputeFreeSpace(tls, **(**uintptr)(__ccgo_up(bp + 8)))
	rc = v1
	if v1 != 0 {
		_checkAppendMsg(tls, pCheck, __ccgo_ts+5909, libc.VaList(bp+48, rc))
		goto end_of_check
	}
	data = (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FaData
	hdr = int32((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FhdrOffset)
	/* Set up for cell analysis */
	(*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx = __ccgo_ts + 5931
	contentOffset = uint32((int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(5)))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(5)) + 1)))-libc.Int32FromInt32(1))&libc.Int32FromInt32(0xffff) + libc.Int32FromInt32(1))
	/* Enforced by btreeInitPage() */
	/* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
	 ** number of cells on the page. */
	nCell = int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(3)))))<<int32(8) | int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(3)) + 1)))
	if (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).Fleaf != 0 || int32((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FintKey) == 0 {
		**(**Ti64)(__ccgo_up(pCheck + 80)) += int64(nCell)
	}
	/* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
	 ** immediately follows the b-tree page header. */
	cellStart = hdr + int32(12) - int32(4)*int32((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).Fleaf)
	pCellIdx = data + uintptr(cellStart+int32(2)*(nCell-int32(1)))
	if !((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).Fleaf != 0) {
		/* Analyze the right-child page of internal pages */
		pgno = int32(_sqlite3Get4byte(tls, data+uintptr(hdr+int32(8))))
		if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
			(*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx = __ccgo_ts + 5957
			_checkPtrmap(tls, pCheck, uint32(pgno), uint8(PTRMAP_BTREE), iPage)
		}
		depth = _checkTreePage(tls, pCheck, uint32(pgno), bp, **(**Ti64)(__ccgo_up(bp)))
		keyCanBeEqual = 0
	} else {
		/* For leaf pages, the coverage check will occur in the same loop
		 ** as the other cell checks, so initialize the heap.  */
		heap = (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fheap
		**(**Tu32)(__ccgo_up(heap)) = uint32(0)
	}
	/* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
	 ** integer offsets to the cell contents. */
	i = nCell - int32(1)
	for {
		if !(i >= 0 && (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr != 0) {
			break
		}
		/* Check cell size */
		(*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv2 = i
		pc = uint32(int32(**(**Tu8)(__ccgo_up(pCellIdx)))<<libc.Int32FromInt32(8) | int32(**(**Tu8)(__ccgo_up(pCellIdx + 1))))
		pCellIdx = pCellIdx - uintptr(2)
		if pc < contentOffset || pc > usableSize-uint32(4) {
			_checkAppendMsg(tls, pCheck, __ccgo_ts+5987, libc.VaList(bp+48, pc, contentOffset, usableSize-uint32(4)))
			doCoverageCheck = 0
			goto _4
		}
		pCell = data + uintptr(pc)
		(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FxParseCell})))(tls, **(**uintptr)(__ccgo_up(bp + 8)), pCell, bp+16)
		if pc+uint32((**(**TCellInfo)(__ccgo_up(bp + 16))).FnSize) > usableSize {
			_checkAppendMsg(tls, pCheck, __ccgo_ts+6017, 0)
			doCoverageCheck = 0
			goto _4
		}
		/* Check for integer primary key out of range */
		if (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FintKey != 0 {
			if keyCanBeEqual != 0 {
				v1 = libc.BoolInt32((**(**TCellInfo)(__ccgo_up(bp + 16))).FnKey > **(**Ti64)(__ccgo_up(bp)))
			} else {
				v1 = libc.BoolInt32((**(**TCellInfo)(__ccgo_up(bp + 16))).FnKey >= **(**Ti64)(__ccgo_up(bp)))
			}
			if v1 != 0 {
				_checkAppendMsg(tls, pCheck, __ccgo_ts+6041, libc.VaList(bp+48, (**(**TCellInfo)(__ccgo_up(bp + 16))).FnKey))
			}
			**(**Ti64)(__ccgo_up(bp)) = (**(**TCellInfo)(__ccgo_up(bp + 16))).FnKey
			keyCanBeEqual = 0 /* Only the first key on the page may ==maxKey */
		}
		/* Check the content overflow list */
		if (**(**TCellInfo)(__ccgo_up(bp + 16))).FnPayload > uint32((**(**TCellInfo)(__ccgo_up(bp + 16))).FnLocal) { /* First page of the overflow chain */
			nPage = ((**(**TCellInfo)(__ccgo_up(bp + 16))).FnPayload - uint32((**(**TCellInfo)(__ccgo_up(bp + 16))).FnLocal) + usableSize - uint32(5)) / (usableSize - uint32(4))
			pgnoOvfl = _sqlite3Get4byte(tls, pCell+uintptr(int32((**(**TCellInfo)(__ccgo_up(bp + 16))).FnSize)-int32(4)))
			if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
				_checkPtrmap(tls, pCheck, pgnoOvfl, uint8(PTRMAP_OVERFLOW1), iPage)
			}
			_checkList(tls, pCheck, 0, pgnoOvfl, nPage)
		}
		if !((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).Fleaf != 0) {
			/* Check sanity of left child page for internal pages */
			pgno = int32(_sqlite3Get4byte(tls, pCell))
			if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
				_checkPtrmap(tls, pCheck, uint32(pgno), uint8(PTRMAP_BTREE), iPage)
			}
			d2 = _checkTreePage(tls, pCheck, uint32(pgno), bp, **(**Ti64)(__ccgo_up(bp)))
			keyCanBeEqual = 0
			if d2 != depth {
				_checkAppendMsg(tls, pCheck, __ccgo_ts+6065, 0)
				depth = d2
			}
		} else {
			/* Populate the coverage-checking heap for leaf pages */
			_btreeHeapInsert(tls, heap, pc<<libc.Int32FromInt32(16)|(pc+uint32((**(**TCellInfo)(__ccgo_up(bp + 16))).FnSize)-uint32(1)))
		}
		goto _4
	_4:
		;
		i = i - 1
	}
	**(**Ti64)(__ccgo_up(piMinKey)) = **(**Ti64)(__ccgo_up(bp))
	/* Check for complete coverage of the page
	 */
	(*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx = uintptr(0)
	if doCoverageCheck != 0 && (*TIntegrityCk)(unsafe.Pointer(pCheck)).FmxErr > 0 {
		/* For leaf pages, the min-heap has already been initialized and the
		 ** cells have already been inserted.  But for internal pages, that has
		 ** not yet been done, so do it now */
		if !((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).Fleaf != 0) {
			heap = (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fheap
			**(**Tu32)(__ccgo_up(heap)) = uint32(0)
			i = nCell - int32(1)
			for {
				if !(i >= 0) {
					break
				}
				pc = uint32(int32(**(**Tu8)(__ccgo_up(data + uintptr(cellStart+i*int32(2)))))<<libc.Int32FromInt32(8) | int32(**(**Tu8)(__ccgo_up(data + uintptr(cellStart+i*int32(2)) + 1))))
				size = uint32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FxCellSize})))(tls, **(**uintptr)(__ccgo_up(bp + 8)), data+uintptr(pc)))
				_btreeHeapInsert(tls, heap, pc<<libc.Int32FromInt32(16)|(pc+size-uint32(1)))
				goto _6
			_6:
				;
				i = i - 1
			}
		}
		/* Add the freeblocks to the min-heap
		 **
		 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
		 ** is the offset of the first freeblock, or zero if there are no
		 ** freeblocks on the page.
		 */
		i = int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(1)))))<<int32(8) | int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(1)) + 1)))
		for i > 0 {
			/* Enforced by btreeComputeFreeSpace() */
			size1 = int32(**(**Tu8)(__ccgo_up(data + uintptr(i+int32(2)))))<<int32(8) | int32(**(**Tu8)(__ccgo_up(data + uintptr(i+int32(2)) + 1)))
			/* due to btreeComputeFreeSpace() */
			_btreeHeapInsert(tls, heap, uint32(i)<<libc.Int32FromInt32(16)|uint32(i+size1-libc.Int32FromInt32(1)))
			/* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
			 ** big-endian integer which is the offset in the b-tree page of the next
			 ** freeblock in the chain, or zero if the freeblock is the last on the
			 ** chain. */
			j = int32(**(**Tu8)(__ccgo_up(data + uintptr(i))))<<int32(8) | int32(**(**Tu8)(__ccgo_up(data + uintptr(i) + 1)))
			/* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
			 ** increasing offset. */
			/* Enforced by btreeComputeFreeSpace() */
			/* Enforced by btreeComputeFreeSpace() */
			i = j
		}
		/* Analyze the min-heap looking for overlap between cells and/or
		 ** freeblocks, and counting the number of untracked bytes in nFrag.
		 **
		 ** Each min-heap entry is of the form:    (start_address<<16)|end_address.
		 ** There is an implied first entry the covers the page header, the cell
		 ** pointer index, and the gap between the cell pointer index and the start
		 ** of cell content.
		 **
		 ** The loop below pulls entries from the min-heap in order and compares
		 ** the start_address against the previous end_address.  If there is an
		 ** overlap, that means bytes are used multiple times.  If there is a gap,
		 ** that gap is added to the fragmentation count.
		 */
		nFrag = 0
		prev = contentOffset - uint32(1) /* Implied first min-heap entry */
		for _btreeHeapPull(tls, heap, bp+12) != 0 {
			if prev&uint32(0xffff) >= **(**Tu32)(__ccgo_up(bp + 12))>>libc.Int32FromInt32(16) {
				_checkAppendMsg(tls, pCheck, __ccgo_ts+6090, libc.VaList(bp+48, **(**Tu32)(__ccgo_up(bp + 12))>>int32(16), iPage))
				break
			} else {
				nFrag = int32(uint32(nFrag) + (**(**Tu32)(__ccgo_up(bp + 12))>>libc.Int32FromInt32(16) - prev&libc.Uint32FromInt32(0xffff) - libc.Uint32FromInt32(1)))
				prev = **(**Tu32)(__ccgo_up(bp + 12))
			}
		}
		nFrag = int32(uint32(nFrag) + (usableSize - prev&libc.Uint32FromInt32(0xffff) - libc.Uint32FromInt32(1)))
		/* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
		 ** is stored in the fifth field of the b-tree page header.
		 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
		 ** number of fragmented free bytes within the cell content area.
		 */
		if **(**Tu32)(__ccgo_up(heap)) == uint32(0) && nFrag != int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(7))))) {
			_checkAppendMsg(tls, pCheck, __ccgo_ts+6127, libc.VaList(bp+48, nFrag, int32(**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(7))))), iPage))
		}
	}
	goto end_of_check
end_of_check:
	;
	if !(doCoverageCheck != 0) {
		(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FisInit = savedIsInit
	}
	_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 8)))
	(*TIntegrityCk)(unsafe.Pointer(pCheck)).FzPfx = saved_zPfx
	(*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv1 = uint32(saved_v1)
	(*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv2 = saved_v2
	return depth + int32(1)
}

// C documentation
//
//	/*
//	** Free the overflow pages associated with the given Cell.
//	*/
func _clearCellOverflow(tls *libc.TLS, pPage uintptr, pCell uintptr, pInfo uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nOvfl, rc, v1 int32
	var ovflPageSize Tu32
	var ovflPgno TPgno
	var pBt, v2 uintptr
	var v3 bool
	var _ /* iNext at bp+0 */ TPgno
	var _ /* pOvfl at bp+4 */ uintptr
	_, _, _, _, _, _, _, _ = nOvfl, ovflPageSize, ovflPgno, pBt, rc, v1, v2, v3
	if pCell+uintptr((*TCellInfo)(unsafe.Pointer(pInfo)).FnSize) > (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd {
		/* Cell extends past end of page */
		return _sqlite3CorruptError(tls, int32(80222))
	}
	ovflPgno = _sqlite3Get4byte(tls, pCell+uintptr((*TCellInfo)(unsafe.Pointer(pInfo)).FnSize)-uintptr(4))
	pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt
	ovflPageSize = (*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(4)
	nOvfl = int32(((*TCellInfo)(unsafe.Pointer(pInfo)).FnPayload - uint32((*TCellInfo)(unsafe.Pointer(pInfo)).FnLocal) + ovflPageSize - uint32(1)) / ovflPageSize)
	for {
		v1 = nOvfl
		nOvfl = nOvfl - 1
		if !(v1 != 0) {
			break
		}
		**(**TPgno)(__ccgo_up(bp)) = uint32(0)
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
		if ovflPgno < uint32(2) || ovflPgno > _btreePagecount(tls, pBt) {
			/* 0 is not a legal page number and page 1 cannot be an
			 ** overflow page. Therefore if ovflPgno<2 or past the end of the
			 ** file the database must be corrupt. */
			return _sqlite3CorruptError(tls, int32(80239))
		}
		if nOvfl != 0 {
			rc = _getOverflowPage(tls, pBt, ovflPgno, bp+4, bp)
			if rc != 0 {
				return rc
			}
		}
		if v3 = **(**uintptr)(__ccgo_up(bp + 4)) != 0; !v3 {
			v2 = _btreePageLookup(tls, pBt, ovflPgno)
			**(**uintptr)(__ccgo_up(bp + 4)) = v2
		}
		if (v3 || v2 != uintptr(0)) && _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpDbPage) != int32(1) {
			/* There is no reason any cursor should have an outstanding reference
			 ** to an overflow page belonging to a cell that is being deleted/updated.
			 ** So if there exists more than one reference to this page, then it
			 ** must not really be an overflow page and the database must be corrupt.
			 ** It is helpful to detect this before calling freePage2(), as
			 ** freePage2() may zero the page contents if secure-delete mode is
			 ** enabled. If this 'overflow' page happens to be a page that the
			 ** caller is iterating through or using in some other way, this
			 ** can be problematic.
			 */
			rc = _sqlite3CorruptError(tls, int32(80259))
		} else {
			rc = _freePage2(tls, pBt, **(**uintptr)(__ccgo_up(bp + 4)), ovflPgno)
		}
		if **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
			_sqlite3PagerUnref(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpDbPage)
		}
		if rc != 0 {
			return rc
		}
		ovflPgno = **(**TPgno)(__ccgo_up(bp))
	}
	return SQLITE_OK
}

/* Call xParseCell to compute the size of a cell.  If the cell contains
** overflow, then invoke cellClearOverflow to clear out that overflow.
** Store the result code (SQLITE_OK or some error code) in rc.
**
** Implemented as macro to force inlining for performance.
 */

// C documentation
//
//	/*
//	** Erase the given database page and all its children.  Return
//	** the page to the freelist.
//	*/
func _clearDatabasePage(tls *libc.TLS, pBt uintptr, pgno TPgno, freePageFlag int32, pnChange uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var hdr, i, v2 int32
	var pCell uintptr
	var _ /* info at bp+8 */ TCellInfo
	var _ /* pPage at bp+0 */ uintptr
	var _ /* rc at bp+4 */ int32
	_, _, _, _ = hdr, i, pCell, v2
	if pgno > _btreePagecount(tls, pBt) {
		return _sqlite3CorruptError(tls, int32(83452))
	}
	**(**int32)(__ccgo_up(bp + 4)) = _getAndInitPage(tls, pBt, pgno, bp, 0)
	if **(**int32)(__ccgo_up(bp + 4)) != 0 {
		return **(**int32)(__ccgo_up(bp + 4))
	}
	if int32((*TBtShared)(unsafe.Pointer(pBt)).FopenFlags)&int32(BTREE_SINGLE) == 0 && _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpDbPage) != int32(1)+libc.BoolInt32(pgno == uint32(1)) {
		**(**int32)(__ccgo_up(bp + 4)) = _sqlite3CorruptError(tls, int32(83459))
		goto cleardatabasepage_out
	}
	hdr = int32((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FhdrOffset)
	i = 0
	for {
		if !(i < int32((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnCell)) {
			break
		}
		pCell = (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaCellIdx + uintptr(int32(2)*i))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaCellIdx + uintptr(int32(2)*i) + 1)))))
		if !((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fleaf != 0) {
			**(**int32)(__ccgo_up(bp + 4)) = _clearDatabasePage(tls, pBt, _sqlite3Get4byte(tls, pCell), int32(1), pnChange)
			if **(**int32)(__ccgo_up(bp + 4)) != 0 {
				goto cleardatabasepage_out
			}
		}
		(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FxParseCell})))(tls, **(**uintptr)(__ccgo_up(bp)), pCell, bp+8)
		if uint32((**(**TCellInfo)(__ccgo_up(bp + 8))).FnLocal) != (**(**TCellInfo)(__ccgo_up(bp + 8))).FnPayload {
			**(**int32)(__ccgo_up(bp + 4)) = _clearCellOverflow(tls, **(**uintptr)(__ccgo_up(bp)), pCell, bp+8)
		} else {
			**(**int32)(__ccgo_up(bp + 4)) = SQLITE_OK
		}
		if **(**int32)(__ccgo_up(bp + 4)) != 0 {
			goto cleardatabasepage_out
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if !((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fleaf != 0) {
		**(**int32)(__ccgo_up(bp + 4)) = _clearDatabasePage(tls, pBt, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData+uintptr(hdr+int32(8))), int32(1), pnChange)
		if **(**int32)(__ccgo_up(bp + 4)) != 0 {
			goto cleardatabasepage_out
		}
		if (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FintKey != 0 {
			pnChange = uintptr(0)
		}
	}
	if pnChange != 0 {
		**(**Ti64)(__ccgo_up(pnChange)) += int64((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnCell)
	}
	if freePageFlag != 0 {
		_freePage(tls, **(**uintptr)(__ccgo_up(bp)), bp+4)
	} else {
		v2 = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpDbPage)
		**(**int32)(__ccgo_up(bp + 4)) = v2
		if v2 == 0 {
			_zeroPage(tls, **(**uintptr)(__ccgo_up(bp)), int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData + uintptr(hdr))))|int32(PTF_LEAF))
		}
	}
	goto cleardatabasepage_out
cleardatabasepage_out:
	;
	_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
	return **(**int32)(__ccgo_up(bp + 4))
}

// C documentation
//
//	/*
//	** Generate code that will evaluate all == and IN constraints for an
//	** index scan.
//	**
//	** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
//	** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
//	** The index has as many as three equality constraints, but in this
//	** example, the third "c" value is an inequality.  So only two
//	** constraints are coded.  This routine will generate code to evaluate
//	** a==5 and b IN (1,2,3).  The current values for a and b will be stored
//	** in consecutive registers and the index of the first register is returned.
//	**
//	** In the example above nEq==2.  But this subroutine works for any value
//	** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
//	** The only thing it does is allocate the pLevel->iMem memory cell and
//	** compute the affinity string.
//	**
//	** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
//	** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
//	** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
//	** occurs after the nEq quality constraints.
//	**
//	** This routine allocates a range of nEq+nExtraReg memory cells and returns
//	** the index of the first memory cell in that range. The code that
//	** calls this routine will use that memory range to store keys for
//	** start and termination conditions of the loop.
//	** key value of the loop.  If one or more IN operators appear, then
//	** this routine allocates an additional nEq memory cells for internal
//	** use.
//	**
//	** Before returning, *pzAff is set to point to a buffer containing a
//	** copy of the column affinity string of the index allocated using
//	** sqlite3DbMalloc(). Except, entries in the copy of the string associated
//	** with equality constraints that use BLOB or NONE affinity are set to
//	** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
//	**
//	**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
//	**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
//	**
//	** In the example above, the index on t1(a) has TEXT affinity. But since
//	** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
//	** no conversion should be attempted before using a t2.b value as part of
//	** a key to search the index. Hence the first byte in the returned affinity
//	** string in this example would be set to SQLITE_AFF_BLOB.
//	*/
func _codeAllEqualityTerms(tls *libc.TLS, pParse uintptr, pLevel uintptr, bRev int32, nExtraReg int32, pzAff uintptr) (r int32) {
	var iIdxCur, j, nReg, r1, regBase, v1 int32
	var nEq, nSkip Tu16
	var pIdx, pLoop, pRight, pTerm, v, zAff uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = iIdxCur, j, nEq, nReg, nSkip, pIdx, pLoop, pRight, pTerm, r1, regBase, v, zAff, v1 /* Number of left-most columns to skip */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe                                                                                  /* Affinity string to return */
	/* This module is only called on query plans that use an index. */
	pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
	nEq = (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FnEq
	nSkip = (*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip
	pIdx = (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FpIndex
	/* Figure out how many memory cells we will need then allocate them.
	 */
	regBase = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
	nReg = int32(nEq) + nExtraReg
	**(**int32)(__ccgo_up(pParse + 48)) += nReg
	zAff = _sqlite3DbStrDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, _sqlite3IndexAffinityStr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pIdx))
	if nSkip != 0 {
		iIdxCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, regBase, regBase+int32(nSkip)-int32(1))
		if bRev != 0 {
			v1 = int32(OP_Last)
		} else {
			v1 = int32(OP_Rewind)
		}
		_sqlite3VdbeAddOp1(tls, v, v1, iIdxCur)
		j = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto))
		if bRev != 0 {
			v1 = int32(OP_SeekLT)
		} else {
			v1 = int32(OP_SeekGT)
		}
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip = _sqlite3VdbeAddOp4Int(tls, v, v1, iIdxCur, 0, regBase, int32(nSkip))
		_sqlite3VdbeJumpHere(tls, v, j)
		j = 0
		for {
			if !(j < int32(nSkip)) {
				break
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, j, regBase+j)
			goto _3
		_3:
			;
			j = j + 1
		}
	}
	/* Evaluate the equality constraints
	 */
	j = int32(nSkip)
	for {
		if !(j < int32(nEq)) {
			break
		}
		pTerm = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4))
		/* The following testcase is true for indices with redundant columns.
		 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
		r1 = _codeEqualityTerm(tls, pParse, pTerm, pLevel, j, bRev, regBase+j)
		if r1 != regBase+j {
			if nReg == int32(1) {
				_sqlite3ReleaseTempReg(tls, pParse, regBase)
				regBase = r1
			} else {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), r1, regBase+j)
			}
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_IN) != 0 {
			if (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(EP_xIsSelect) != 0 {
				/* No affinity ever needs to be (or should be) applied to a value
				 ** from the RHS of an "? IN (SELECT ...)" expression. The
				 ** sqlite3FindInIndex() routine has already ensured that the
				 ** affinity of the comparison has been applied to the value.  */
				if zAff != 0 {
					**(**int8)(__ccgo_up(zAff + uintptr(j))) = int8(SQLITE_AFF_BLOB)
				}
			}
		} else {
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_ISNULL) == 0 {
				pRight = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight
				if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_IS) == 0 && _sqlite3ExprCanBeNull(tls, pRight) != 0 {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regBase+j, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk)
				}
				if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
					if int32(_sqlite3CompareAffinity(tls, pRight, **(**int8)(__ccgo_up(zAff + uintptr(j))))) == int32(SQLITE_AFF_BLOB) {
						**(**int8)(__ccgo_up(zAff + uintptr(j))) = int8(SQLITE_AFF_BLOB)
					}
					if _sqlite3ExprNeedsNoAffinityChange(tls, pRight, **(**int8)(__ccgo_up(zAff + uintptr(j)))) != 0 {
						**(**int8)(__ccgo_up(zAff + uintptr(j))) = int8(SQLITE_AFF_BLOB)
					}
				}
			}
		}
		goto _4
	_4:
		;
		j = j + 1
	}
	**(**uintptr)(__ccgo_up(pzAff)) = zAff
	return regBase
}

// C documentation
//
//	/*
//	** This procedure generates VDBE code for a single invocation of either the
//	** sqlite_detach() or sqlite_attach() SQL user functions.
//	*/
func _codeAttach(tls *libc.TLS, pParse uintptr, type1 int32, pFunc uintptr, pAuthArg uintptr, pFilename uintptr, pDbname uintptr, pKey uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var db, v, zAuthArg uintptr
	var rc, regArgs int32
	var _ /* sName at bp+0 */ TNameContext
	_, _, _, _, _ = db, rc, regArgs, v, zAuthArg
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) {
		goto attach_end
	}
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		goto attach_end
	}
	libc.Xmemset(tls, bp, 0, uint32(36))
	(**(**TNameContext)(__ccgo_up(bp))).FpParse = pParse
	if SQLITE_OK != _resolveAttachExpr(tls, bp, pFilename) || SQLITE_OK != _resolveAttachExpr(tls, bp, pDbname) || SQLITE_OK != _resolveAttachExpr(tls, bp, pKey) {
		goto attach_end
	}
	if pAuthArg != 0 {
		if int32((*TExpr)(unsafe.Pointer(pAuthArg)).Fop) == int32(TK_STRING) {
			zAuthArg = *(*uintptr)(unsafe.Pointer(pAuthArg + 8))
		} else {
			zAuthArg = uintptr(0)
		}
		rc = _sqlite3AuthCheck(tls, pParse, type1, zAuthArg, uintptr(0), uintptr(0))
		if rc != SQLITE_OK {
			goto attach_end
		}
	}
	v = _sqlite3GetVdbe(tls, pParse)
	regArgs = _sqlite3GetTempRange(tls, pParse, int32(4))
	_sqlite3ExprCode(tls, pParse, pFilename, regArgs)
	_sqlite3ExprCode(tls, pParse, pDbname, regArgs+int32(1))
	_sqlite3ExprCode(tls, pParse, pKey, regArgs+int32(2))
	if v != 0 {
		_sqlite3VdbeAddFunctionCall(tls, pParse, 0, regArgs+int32(3)-int32((*TFuncDef)(unsafe.Pointer(pFunc)).FnArg), regArgs+int32(3), int32((*TFuncDef)(unsafe.Pointer(pFunc)).FnArg), pFunc, 0)
		/* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
		 ** statement only). For DETACH, set it to false (expire all existing
		 ** statements).
		 */
		_sqlite3VdbeAddOp1(tls, v, int32(OP_Expire), libc.BoolInt32(type1 == int32(SQLITE_ATTACH)))
	}
	goto attach_end
attach_end:
	;
	_sqlite3ExprDelete(tls, db, pFilename)
	_sqlite3ExprDelete(tls, db, pDbname)
	_sqlite3ExprDelete(tls, db, pKey)
}

// C documentation
//
//	/*
//	** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
//	** a rowid value just read from cursor iIdxCur, open on index pIdx. This
//	** function generates code to do a deferred seek of cursor iCur to the
//	** rowid stored in register iRowid.
//	**
//	** Normally, this is just:
//	**
//	**   OP_DeferredSeek $iCur $iRowid
//	**
//	** Which causes a seek on $iCur to the row with rowid $iRowid.
//	**
//	** However, if the scan currently being coded is a branch of an OR-loop and
//	** the statement currently being coded is a SELECT, then additional information
//	** is added that might allow OP_Column to omit the seek and instead do its
//	** lookup on the index, thus avoiding an expensive seek operation.  To
//	** enable this optimization, the P3 of OP_DeferredSeek is set to iIdxCur
//	** and P4 is set to an array of integers containing one entry for each column
//	** in the table.  For each table column, if the column is the i'th
//	** column of the index, then the corresponding array entry is set to (i+1).
//	** If the column does not appear in the index at all, the array entry is set
//	** to 0.  The OP_Column opcode can check this array to see if the column it
//	** wants is in the index and if it is, it will substitute the index cursor
//	** and column number and continue with those new values, rather than seeking
//	** the table cursor.
//	*/
func _codeDeferredSeek(tls *libc.TLS, pWInfo uintptr, pIdx uintptr, iCur int32, iIdxCur int32) {
	var ai, pParse, pTab, v, v1 uintptr
	var i, x1, x2 int32
	var v2 bool
	_, _, _, _, _, _, _, _, _ = ai, i, pParse, pTab, v, x1, x2, v1, v2
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse /* Parse context */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe           /* Vdbe to generate code within */
	libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 0, 0x1)
	_sqlite3VdbeAddOp3(tls, v, int32(OP_DeferredSeek), iIdxCur, 0, iCur)
	if v2 = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&(libc.Int32FromInt32(WHERE_OR_SUBCLAUSE)|libc.Int32FromInt32(WHERE_RIGHT_JOIN)) != 0; v2 {
		if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 {
			v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel
		} else {
			v1 = pParse
		}
	}
	if v2 && (*TParse)(unsafe.Pointer(v1)).FwriteMask == uint32(0) {
		pTab = (*TIndex)(unsafe.Pointer(pIdx)).FpTable
		ai = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(4)*uint32(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+libc.Int32FromInt32(1))))
		if ai != 0 {
			**(**Tu32)(__ccgo_up(ai)) = uint32((*TTable)(unsafe.Pointer(pTab)).FnCol)
			i = 0
			for {
				if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)-int32(1)) {
					break
				}
				x1 = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2)))
				x2 = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(x1)))
				if x1 >= 0 {
					**(**Tu32)(__ccgo_up(ai + uintptr(x2+int32(1))*4)) = uint32(i + int32(1))
				}
				goto _3
			_3:
				;
				i = i + 1
			}
			_sqlite3VdbeChangeP4(tls, v, -int32(1), ai, -int32(15))
		}
	}
}

// C documentation
//
//	/*
//	** Generate code for a single X IN (....) term of the WHERE clause.
//	**
//	** This is a special-case of codeEqualityTerm() that works for IN operators
//	** only.  It is broken out into a subroutine because this case is
//	** uncommon and by splitting it off into a subroutine, the common case
//	** runs faster.
//	**
//	** The current value for the constraint is left in  register iTarget.
//	** This routine sets up a loop that will iterate over all values of X.
//	*/
func _codeINTerm(tls *libc.TLS, pParse uintptr, pTerm uintptr, pLevel uintptr, iEq int32, bRev int32, iTarget int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aiMap, db, pIn, pLoop, pX, pXMod, v uintptr
	var eType, i, iCol, iMap, iOut, nEq, v3, v5 int32
	var _ /* iTab at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aiMap, db, eType, i, iCol, iMap, iOut, nEq, pIn, pLoop, pX, pXMod, v, v3, v5
	pX = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
	eType = int32(IN_INDEX_NOOP)
	pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	nEq = 0
	aiMap = uintptr(0)
	if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) == uint32(0) && (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FpIndex != uintptr(0) && **(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer((*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FpIndex)).FaSortOrder + uintptr(iEq))) != 0 {
		bRev = libc.BoolInt32(!(bRev != 0))
	}
	i = 0
	for {
		if !(i < iEq) {
			break
		}
		if **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)) != 0 && (*TWhereTerm)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX {
			_disableTerm(tls, pLevel, pTerm)
			return
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	i = iEq
	for {
		if !(i < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm)) {
			break
		}
		if (*TWhereTerm)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX {
			nEq = nEq + 1
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	**(**int32)(__ccgo_up(bp)) = 0
	if !((*TExpr)(unsafe.Pointer(pX)).Fflags&libc.Uint32FromInt32(EP_xIsSelect) != libc.Uint32FromInt32(0)) || (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pX + 20)))).FpEList)).FnExpr == int32(1) {
		eType = _sqlite3FindInIndex(tls, pParse, pX, uint32(IN_INDEX_LOOP), uintptr(0), uintptr(0), bp)
	} else {
		db = (*TParse)(unsafe.Pointer(pParse)).Fdb
		pXMod = _removeUnindexableInClauseTerms(tls, pParse, iEq, pLoop, pX)
		if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
			aiMap = _sqlite3DbMallocZero(tls, db, uint64(uint32(4)*uint32(nEq)))
			eType = _sqlite3FindInIndex(tls, pParse, pXMod, uint32(IN_INDEX_LOOP), uintptr(0), aiMap, bp)
		}
		_sqlite3ExprDelete(tls, db, pXMod)
	}
	if eType == int32(IN_INDEX_INDEX_DESC) {
		bRev = libc.BoolInt32(!(bRev != 0))
	}
	if bRev != 0 {
		v3 = int32(OP_Last)
	} else {
		v3 = int32(OP_Rewind)
	}
	_sqlite3VdbeAddOp2(tls, v, v3, **(**int32)(__ccgo_up(bp)), 0)
	**(**Tu32)(__ccgo_up(pLoop + 40)) |= uint32(WHERE_IN_ABLE)
	if (*(*struct {
		FnIn     int32
		FaInLoop uintptr
	})(unsafe.Pointer(pLevel + 68))).FnIn == 0 {
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt = _sqlite3VdbeMakeLabel(tls, pParse)
	}
	if iEq > 0 && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_SEEKSCAN) == uint32(0) {
		**(**Tu32)(__ccgo_up(pLoop + 40)) |= uint32(WHERE_IN_EARLYOUT)
	}
	i = (*(*struct {
		FnIn     int32
		FaInLoop uintptr
	})(unsafe.Pointer(pLevel + 68))).FnIn
	(*(*struct {
		FnIn     int32
		FaInLoop uintptr
	})(unsafe.Pointer(pLevel + 68))).FnIn += nEq
	(*(*struct {
		FnIn     int32
		FaInLoop uintptr
	})(unsafe.Pointer(pLevel + 68))).FaInLoop = _sqlite3WhereRealloc(tls, (*TWhereClause)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpWC)).FpWInfo, (*(*struct {
		FnIn     int32
		FaInLoop uintptr
	})(unsafe.Pointer(pLevel + 68))).FaInLoop, uint64(uint32(20)*uint32((*(*struct {
		FnIn     int32
		FaInLoop uintptr
	})(unsafe.Pointer(pLevel + 68))).FnIn)))
	pIn = (*(*struct {
		FnIn     int32
		FaInLoop uintptr
	})(unsafe.Pointer(pLevel + 68))).FaInLoop
	if pIn != 0 {
		iMap = 0 /* Index in aiMap[] */
		pIn = pIn + uintptr(i)*20
		i = iEq
		for {
			if !(i < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm)) {
				break
			}
			if (*TWhereTerm)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX {
				iOut = iTarget + i - iEq
				if eType == int32(IN_INDEX_ROWID) {
					(*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), **(**int32)(__ccgo_up(bp)), iOut)
				} else {
					if aiMap != 0 {
						v5 = iMap
						iMap = iMap + 1
						v3 = **(**int32)(__ccgo_up(aiMap + uintptr(v5)*4))
					} else {
						v3 = 0
					}
					iCol = v3
					(*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop = _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), **(**int32)(__ccgo_up(bp)), iCol, iOut)
				}
				_sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), iOut)
				if i == iEq {
					(*TInLoop)(unsafe.Pointer(pIn)).FiCur = **(**int32)(__ccgo_up(bp))
					if bRev != 0 {
						v3 = int32(OP_Prev)
					} else {
						v3 = int32(OP_Next)
					}
					(*TInLoop)(unsafe.Pointer(pIn)).FeEndLoopOp = uint8(v3)
					if iEq > 0 {
						(*TInLoop)(unsafe.Pointer(pIn)).FiBase = iTarget - i
						(*TInLoop)(unsafe.Pointer(pIn)).FnPrefix = i
					} else {
						(*TInLoop)(unsafe.Pointer(pIn)).FnPrefix = 0
					}
				} else {
					(*TInLoop)(unsafe.Pointer(pIn)).FeEndLoopOp = uint8(OP_Noop)
				}
				pIn += 20
			}
			goto _4
		_4:
			;
			i = i + 1
		}
		if iEq > 0 && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_IN_SEEKSCAN)|libc.Int32FromInt32(WHERE_VIRTUALTABLE)) == uint32(0) {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_SeekHit), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, 0, iEq)
		}
	} else {
		(*(*struct {
			FnIn     int32
			FaInLoop uintptr
		})(unsafe.Pointer(pLevel + 68))).FnIn = 0
	}
	_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, aiMap)
}

// C documentation
//
//	/*
//	** Generate an instruction that will put the integer describe by
//	** text z[0..n-1] into register iMem.
//	**
//	** Expr.u.zToken is always UTF8 and zero-terminated.
//	*/
func _codeInteger(tls *libc.TLS, pParse uintptr, pExpr uintptr, negFlag int32, iMem int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var c, i int32
	var v, z, v1 uintptr
	var v2 int64
	var _ /* value at bp+0 */ Ti64
	_, _, _, _, _, _ = c, i, v, z, v1, v2
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_IntValue) != 0 {
		i = *(*int32)(unsafe.Pointer(pExpr + 8))
		if negFlag != 0 {
			i = -i
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), i, iMem)
	} else {
		z = *(*uintptr)(unsafe.Pointer(pExpr + 8))
		c = _sqlite3DecOrHexToI64(tls, z, bp)
		if c == int32(3) && !(negFlag != 0) || c == int32(2) || negFlag != 0 && **(**Ti64)(__ccgo_up(bp)) == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)) {
			if Xsqlite3_strnicmp(tls, z, __ccgo_ts+9639, int32(2)) == 0 {
				if negFlag != 0 {
					v1 = __ccgo_ts + 6442
				} else {
					v1 = __ccgo_ts + 1711
				}
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9642, libc.VaList(bp+16, v1, pExpr))
			} else {
				_codeReal(tls, v, z, negFlag, iMem)
			}
		} else {
			if negFlag != 0 {
				if c == int32(3) {
					v2 = int64(-libc.Int32FromInt32(1)) - (libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32))
				} else {
					v2 = -**(**Ti64)(__ccgo_up(bp))
				}
				**(**Ti64)(__ccgo_up(bp)) = v2
			}
			_sqlite3VdbeAddOp4Dup8(tls, v, int32(OP_Int64), 0, iMem, 0, bp, -int32(14))
		}
	}
}

// C documentation
//
//	/*
//	** Generate code for the RETURNING trigger.  Unlike other triggers
//	** that invoke a subprogram in the bytecode, the code for RETURNING
//	** is generated in-line.
//	*/
func _codeReturningTrigger(tls *libc.TLS, pParse uintptr, pTrigger uintptr, pTab uintptr, regIn int32) {
	bp := tls.Alloc(176)
	defer tls.Free(176)
	var db, pCol, pFrom, pNew, pReturning, v, v2 uintptr
	var i, nCol, reg, v1 int32
	var _ /* sNC at bp+128 */ TNameContext
	var _ /* sSelect at bp+0 */ TSelect
	var _ /* uSrc at bp+72 */ struct {
		F__ccgo_align [0]uint32
		FfromSpace    [0][56]Tu8
		FsSrc         TSrcList
		F__ccgo_pad2  [48]byte
	}
	_, _, _, _, _, _, _, _, _, _, _ = db, i, nCol, pCol, pFrom, pNew, pReturning, reg, v, v1, v2
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if !(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x8>>3)) != 0) {
		/* This RETURNING trigger must be for a different statement as
		 ** this statement lacks a RETURNING clause. */
		return
	}
	pReturning = (*(*struct {
		FpReturning uintptr
	})(unsafe.Pointer(&(*TParse)(unsafe.Pointer(pParse)).Fu1))).FpReturning
	if pTrigger != pReturning+8 {
		/* This RETURNING trigger is for a different statement */
		return
	}
	libc.Xmemset(tls, bp, 0, uint32(68))
	libc.Xmemset(tls, bp+72, 0, uint32(56))
	pFrom = bp + 72
	(**(**TSelect)(__ccgo_up(bp))).FpEList = _sqlite3ExprListDup(tls, db, (*TReturning)(unsafe.Pointer(pReturning)).FpReturnEL, 0)
	(**(**TSelect)(__ccgo_up(bp))).FpSrc = pFrom
	(*TSrcList)(unsafe.Pointer(pFrom)).FnSrc = int32(1)
	(*(*TSrcItem)(unsafe.Pointer(pFrom + 8))).FpSTab = pTab
	(*(*TSrcItem)(unsafe.Pointer(pFrom + 8))).FzName = (*TTable)(unsafe.Pointer(pTab)).FzName /* tag-20240424-1 */
	(*(*TSrcItem)(unsafe.Pointer(pFrom + 8))).FiCursor = -int32(1)
	_sqlite3SelectPrep(tls, pParse, bp, uintptr(0))
	if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
		_sqlite3GenerateColumnNames(tls, pParse, bp)
	}
	_sqlite3ExprListDelete(tls, db, (**(**TSelect)(__ccgo_up(bp))).FpEList)
	pNew = _sqlite3ExpandReturning(tls, pParse, (*TReturning)(unsafe.Pointer(pReturning)).FpReturnEL, pTab)
	if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
		libc.Xmemset(tls, bp+128, 0, uint32(36))
		if (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol == 0 {
			(*TReturning)(unsafe.Pointer(pReturning)).FnRetCol = (*TExprList)(unsafe.Pointer(pNew)).FnExpr
			v2 = pParse + 44
			v1 = *(*int32)(unsafe.Pointer(v2))
			*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
			(*TReturning)(unsafe.Pointer(pReturning)).FiRetCur = v1
		}
		(**(**TNameContext)(__ccgo_up(bp + 128))).FpParse = pParse
		*(*int32)(unsafe.Pointer(bp + 128 + 8)) = regIn
		(**(**TNameContext)(__ccgo_up(bp + 128))).FncFlags = int32(NC_UBaseReg)
		(*TParse)(unsafe.Pointer(pParse)).FeTriggerOp = (*TTrigger)(unsafe.Pointer(pTrigger)).Fop
		(*TParse)(unsafe.Pointer(pParse)).FpTriggerTab = pTab
		if _sqlite3ResolveExprListNames(tls, bp+128, pNew) == SQLITE_OK && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
			nCol = (*TExprList)(unsafe.Pointer(pNew)).FnExpr
			reg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
			_sqlite3ProcessReturningSubqueries(tls, pNew, pTab)
			**(**int32)(__ccgo_up(pParse + 48)) += nCol + int32(2)
			(*TReturning)(unsafe.Pointer(pReturning)).FiRetReg = reg
			i = 0
			for {
				if !(i < nCol) {
					break
				}
				pCol = (*(*TExprList_item)(unsafe.Pointer(pNew + 8 + uintptr(i)*20))).FpExpr
				/* Due to !db->mallocFailed ~9 lines above */
				_sqlite3ExprCodeFactorable(tls, pParse, pCol, reg+i)
				if int32(_sqlite3ExprAffinity(tls, pCol)) == int32(SQLITE_AFF_REAL) {
					_sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), reg+i)
				}
				goto _3
			_3:
				;
				i = i + 1
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), reg, i, reg+i)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur, reg+i+int32(1))
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur, reg+i, reg+i+int32(1))
		}
	}
	_sqlite3ExprListDelete(tls, db, pNew)
	(*TParse)(unsafe.Pointer(pParse)).FeTriggerOp = uint8(0)
	(*TParse)(unsafe.Pointer(pParse)).FpTriggerTab = uintptr(0)
}

// C documentation
//
//	/*
//	** Create and populate a new TriggerPrg object with a sub-program
//	** implementing trigger pTrigger with ON CONFLICT policy orconf.
//	*/
func _codeRowTrigger(tls *libc.TLS, pParse uintptr, pTrigger uintptr, pTab uintptr, orconf int32) (r uintptr) {
	bp := tls.Alloc(336)
	defer tls.Free(336)
	var db, pPrg, pProgram, pTop, pWhen, v, v2 uintptr
	var iEndTrigger, nDepth int32
	var _ /* sNC at bp+0 */ TNameContext
	var _ /* sSubParse at bp+36 */ TParse
	_, _, _, _, _, _, _, _, _ = db, iEndTrigger, nDepth, pPrg, pProgram, pTop, pWhen, v, v2 /* Top level Parse object */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb                                              /* Value to return */
	pWhen = uintptr(0)                                                                      /* Name context for sub-vdbe */
	pProgram = uintptr(0)                                                                   /* Sub-vdbe for trigger program */
	iEndTrigger = 0                                                                         /* Trigger depth */
	/* Ensure that triggers are not chained too deep.  This test is linear
	 ** in the chaining depth, but sensible code ought not be chaining
	 ** triggers excessively, so that shouldn't be a problem.
	 */
	pTop = pParse
	nDepth = 0
	for {
		if !((*TParse)(unsafe.Pointer(pTop)).FpOuterParse != 0) {
			break
		}
		goto _1
	_1:
		;
		pTop = (*TParse)(unsafe.Pointer(pTop)).FpOuterParse
		nDepth = nDepth + 1
	}
	if nDepth >= **(**int32)(__ccgo_up(db + 120 + 10*4)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23798, 0)
		return uintptr(0)
	}
	if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 {
		v2 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel
	} else {
		v2 = pParse
	}
	pTop = v2
	/* Allocate the TriggerPrg and SubProgram objects. To ensure that they
	 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
	 ** list of the top-level Parse object sooner rather than later.  */
	pPrg = _sqlite3DbMallocZero(tls, db, uint64(24))
	if !(pPrg != 0) {
		return uintptr(0)
	}
	(*TTriggerPrg)(unsafe.Pointer(pPrg)).FpNext = (*TParse)(unsafe.Pointer(pTop)).FpTriggerPrg
	(*TParse)(unsafe.Pointer(pTop)).FpTriggerPrg = pPrg
	v2 = _sqlite3DbMallocZero(tls, db, uint64(28))
	pProgram = v2
	(*TTriggerPrg)(unsafe.Pointer(pPrg)).FpProgram = v2
	if !(pProgram != 0) {
		return uintptr(0)
	}
	_sqlite3VdbeLinkSubProgram(tls, (*TParse)(unsafe.Pointer(pTop)).FpVdbe, pProgram)
	(*TTriggerPrg)(unsafe.Pointer(pPrg)).FpTrigger = pTrigger
	(*TTriggerPrg)(unsafe.Pointer(pPrg)).Forconf = orconf
	**(**Tu32)(__ccgo_up(pPrg + 16)) = uint32(0xffffffff)
	**(**Tu32)(__ccgo_up(pPrg + 16 + 1*4)) = uint32(0xffffffff)
	/* Allocate and populate a new Parse context to use for coding the
	 ** trigger sub-program.  */
	_sqlite3ParseObjectInit(tls, bp+36, db)
	libc.Xmemset(tls, bp, 0, uint32(36))
	(**(**TNameContext)(__ccgo_up(bp))).FpParse = bp + 36
	(**(**TParse)(__ccgo_up(bp + 36))).FpTriggerTab = pTab
	(**(**TParse)(__ccgo_up(bp + 36))).FpToplevel = pTop
	(**(**TParse)(__ccgo_up(bp + 36))).FzAuthContext = (*TTrigger)(unsafe.Pointer(pTrigger)).FzName
	(**(**TParse)(__ccgo_up(bp + 36))).FeTriggerOp = (*TTrigger)(unsafe.Pointer(pTrigger)).Fop
	(**(**TParse)(__ccgo_up(bp + 36))).FnQueryLoop = (*TParse)(unsafe.Pointer(pParse)).FnQueryLoop
	(**(**TParse)(__ccgo_up(bp + 36))).FprepFlags = (*TParse)(unsafe.Pointer(pParse)).FprepFlags
	(**(**TParse)(__ccgo_up(bp + 36))).Foldmask = uint32(0)
	(**(**TParse)(__ccgo_up(bp + 36))).Fnewmask = uint32(0)
	v = _sqlite3GetVdbe(tls, bp+36)
	if v != 0 {
		if (*TTrigger)(unsafe.Pointer(pTrigger)).FzName != 0 {
			_sqlite3VdbeChangeP4(tls, v, -int32(1), _sqlite3MPrintf(tls, db, __ccgo_ts+23823, libc.VaList(bp+328, (*TTrigger)(unsafe.Pointer(pTrigger)).FzName)), -int32(7))
		}
		/* If one was specified, code the WHEN clause. If it evaluates to false
		 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
		 ** OP_Halt inserted at the end of the program.  */
		if (*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen != 0 {
			pWhen = _sqlite3ExprDup(tls, db, (*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen, 0)
			if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 && SQLITE_OK == _sqlite3ResolveExprNames(tls, bp, pWhen) {
				iEndTrigger = _sqlite3VdbeMakeLabel(tls, bp+36)
				_sqlite3ExprIfFalse(tls, bp+36, pWhen, iEndTrigger, int32(SQLITE_JUMPIFNULL))
			}
			_sqlite3ExprDelete(tls, db, pWhen)
		}
		/* Code the trigger program into the sub-vdbe. */
		_codeTriggerProgram(tls, bp+36, (*TTrigger)(unsafe.Pointer(pTrigger)).Fstep_list, orconf)
		/* Insert an OP_Halt at the end of the sub-program. */
		if iEndTrigger != 0 {
			_sqlite3VdbeResolveLabel(tls, v, iEndTrigger)
		}
		_sqlite3VdbeAddOp0(tls, v, int32(OP_Halt))
		_transferParseError(tls, pParse, bp+36)
		if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
			(*TSubProgram)(unsafe.Pointer(pProgram)).FaOp = _sqlite3VdbeTakeOpArray(tls, v, pProgram+4, pTop+96)
		}
		(*TSubProgram)(unsafe.Pointer(pProgram)).FnMem = (**(**TParse)(__ccgo_up(bp + 36))).FnMem
		(*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr = (**(**TParse)(__ccgo_up(bp + 36))).FnTab
		(*TSubProgram)(unsafe.Pointer(pProgram)).Ftoken = pTrigger
		**(**Tu32)(__ccgo_up(pPrg + 16)) = (**(**TParse)(__ccgo_up(bp + 36))).Foldmask
		**(**Tu32)(__ccgo_up(pPrg + 16 + 1*4)) = (**(**TParse)(__ccgo_up(bp + 36))).Fnewmask
		_sqlite3VdbeDelete(tls, v)
	} else {
		_transferParseError(tls, pParse, bp+36)
	}
	_sqlite3ParseObjectReset(tls, bp+36)
	return pPrg
}

// C documentation
//
//	/*
//	** Code an OP_TableLock instruction for each table locked by the
//	** statement (configured by calls to sqlite3TableLock()).
//	*/
func _codeTableLocks(tls *libc.TLS, pParse uintptr) {
	var i, p1 int32
	var p, pVdbe uintptr
	_, _, _, _ = i, p, p1, pVdbe
	pVdbe = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	i = 0
	for {
		if !(i < (*TParse)(unsafe.Pointer(pParse)).FnTableLock) {
			break
		}
		p = (*TParse)(unsafe.Pointer(pParse)).FaTableLock + uintptr(i)*16
		p1 = (*TTableLock)(unsafe.Pointer(p)).FiDb
		_sqlite3VdbeAddOp4(tls, pVdbe, int32(OP_TableLock), p1, int32((*TTableLock)(unsafe.Pointer(p)).FiTab), int32((*TTableLock)(unsafe.Pointer(p)).FisWriteLock), (*TTableLock)(unsafe.Pointer(p)).FzLockName, -int32(1))
		goto _1
	_1:
		;
		i = i + 1
	}
}

/*
** Return TRUE if the given yDbMask object is empty - if it contains no
** 1 bits.  This routine is used by the DbMaskAllZero() and DbMaskNotZero()
** macros when SQLITE_MAX_ATTACHED is greater than 30.
 */

// C documentation
//
//	/*
//	** Generate VDBE code for the statements inside the body of a single
//	** trigger.
//	*/
func _codeTriggerProgram(tls *libc.TLS, pParse uintptr, pStepList uintptr, orconf int32) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var db, pSelect, pStep, v uintptr
	var v2 int32
	var _ /* sDest at bp+0 */ TSelectDest
	_, _, _, _, _ = db, pSelect, pStep, v, v2
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pStep = pStepList
	for {
		if !(pStep != 0) {
			break
		}
		/* Figure out the ON CONFLICT policy that will be used for this step
		 ** of the trigger program. If the statement that caused this trigger
		 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
		 ** the ON CONFLICT policy that was specified as part of the trigger
		 ** step statement. Example:
		 **
		 **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
		 **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
		 **   END;
		 **
		 **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
		 **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
		 */
		if orconf == int32(OE_Default) {
			v2 = int32((*TTriggerStep)(unsafe.Pointer(pStep)).Forconf)
		} else {
			v2 = int32(uint8(orconf))
		}
		(*TParse)(unsafe.Pointer(pParse)).FeOrconf = uint8(v2)
		if (*TTriggerStep)(unsafe.Pointer(pStep)).FzSpan != 0 {
			_sqlite3VdbeAddOp4(tls, v, int32(OP_Trace), int32(0x7fffffff), int32(1), 0, _sqlite3MPrintf(tls, db, __ccgo_ts+7698, libc.VaList(bp+40, (*TTriggerStep)(unsafe.Pointer(pStep)).FzSpan)), -int32(7))
		}
		switch int32((*TTriggerStep)(unsafe.Pointer(pStep)).Fop) {
		case int32(TK_UPDATE):
			_sqlite3Update(tls, pParse, _sqlite3SrcListDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc, 0), _sqlite3ExprListDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, 0), _sqlite3ExprDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere, 0), int32((*TParse)(unsafe.Pointer(pParse)).FeOrconf), uintptr(0), uintptr(0), uintptr(0))
			_sqlite3VdbeAddOp0(tls, v, int32(OP_ResetCount))
		case int32(TK_INSERT):
			_sqlite3Insert(tls, pParse, _sqlite3SrcListDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc, 0), _sqlite3SelectDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect, 0), _sqlite3IdListDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpIdList), int32((*TParse)(unsafe.Pointer(pParse)).FeOrconf), _sqlite3UpsertDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert))
			_sqlite3VdbeAddOp0(tls, v, int32(OP_ResetCount))
		case int32(TK_DELETE):
			_sqlite3DeleteFrom(tls, pParse, _sqlite3SrcListDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc, 0), _sqlite3ExprDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere, 0), uintptr(0), uintptr(0))
			_sqlite3VdbeAddOp0(tls, v, int32(OP_ResetCount))
		default:
			pSelect = _sqlite3SelectDup(tls, db, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect, 0)
			_sqlite3SelectDestInit(tls, bp, int32(SRT_Discard), 0)
			_sqlite3Select(tls, pParse, pSelect, bp)
			_sqlite3SelectDelete(tls, db, pSelect)
			break
		}
		goto _1
	_1:
		;
		pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext
	}
	return 0
}

// C documentation
//
//	/*
//	** Expression pExpr is a comparison between two vector values. Compute
//	** the result of the comparison (1, 0, or NULL) and write that
//	** result into register dest.
//	**
//	** The caller must satisfy the following preconditions:
//	**
//	**    if pExpr->op==TK_IS:      op==TK_EQ and p5==SQLITE_NULLEQ
//	**    if pExpr->op==TK_ISNOT:   op==TK_NE and p5==SQLITE_NULLEQ
//	**    otherwise:                op==pExpr->op and p5==0
//	*/
func _codeVectorCompare(tls *libc.TLS, pParse uintptr, pExpr uintptr, dest int32, op Tu8, p5 Tu8) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var addrCmp, addrDone, i, isCommuted, nLeft, r1, r2, regLeft, regRight int32
	var opx Tu8
	var pLeft, pRight, v uintptr
	var _ /* pL at bp+8 */ uintptr
	var _ /* pR at bp+12 */ uintptr
	var _ /* regFree1 at bp+0 */ int32
	var _ /* regFree2 at bp+4 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _ = addrCmp, addrDone, i, isCommuted, nLeft, opx, pLeft, pRight, r1, r2, regLeft, regRight, v
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
	pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight
	nLeft = _sqlite3ExprVectorSize(tls, pLeft)
	regLeft = 0
	regRight = 0
	opx = op
	addrCmp = 0
	addrDone = _sqlite3VdbeMakeLabel(tls, pParse)
	isCommuted = libc.BoolInt32((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Commuted)) != uint32(0))
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return
	}
	if nLeft != _sqlite3ExprVectorSize(tls, pRight) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8125, 0)
		return
	}
	if int32(op) == int32(TK_LE) {
		opx = uint8(TK_LT)
	}
	if int32(op) == int32(TK_GE) {
		opx = uint8(TK_GT)
	}
	if int32(op) == int32(TK_NE) {
		opx = uint8(TK_EQ)
	}
	regLeft = _exprCodeSubselect(tls, pParse, pLeft)
	regRight = _exprCodeSubselect(tls, pParse, pRight)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), dest)
	i = 0
	for {
		if !(int32(1) != 0) {
			break
		}
		**(**int32)(__ccgo_up(bp)) = 0
		**(**int32)(__ccgo_up(bp + 4)) = 0
		**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
		**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
		if addrCmp != 0 {
			_sqlite3VdbeJumpHere(tls, v, addrCmp)
		}
		r1 = _exprVectorRegister(tls, pParse, pLeft, i, regLeft, bp+8, bp)
		r2 = _exprVectorRegister(tls, pParse, pRight, i, regRight, bp+12, bp+4)
		addrCmp = _sqlite3VdbeCurrentAddr(tls, v)
		_codeCompare(tls, pParse, **(**uintptr)(__ccgo_up(bp + 8)), **(**uintptr)(__ccgo_up(bp + 12)), int32(opx), r1, r2, addrDone, int32(p5), isCommuted)
		_sqlite3ReleaseTempReg(tls, pParse, **(**int32)(__ccgo_up(bp)))
		_sqlite3ReleaseTempReg(tls, pParse, **(**int32)(__ccgo_up(bp + 4)))
		if (int32(opx) == int32(TK_LT) || int32(opx) == int32(TK_GT)) && i < nLeft-int32(1) {
			addrCmp = _sqlite3VdbeAddOp0(tls, v, int32(OP_ElseEq))
		}
		if int32(p5) == int32(SQLITE_NULLEQ) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, dest)
		} else {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_ZeroOrNull), r1, dest, r2)
		}
		if i == nLeft-int32(1) {
			break
		}
		if int32(opx) == int32(TK_EQ) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), dest, addrDone)
		} else {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrDone)
			if i == nLeft-int32(2) {
				opx = op
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	_sqlite3VdbeJumpHere(tls, v, addrCmp)
	_sqlite3VdbeResolveLabel(tls, v, addrDone)
	if int32(op) == int32(TK_NE) {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Not), dest, dest)
	}
}

// C documentation
//
//	/*
//	** Return true if any column of pIndex uses the zColl collation
//	*/
func _collationMatch(tls *libc.TLS, zColl uintptr, pIndex uintptr) (r int32) {
	var i int32
	var z uintptr
	_, _ = i, z
	i = 0
	for {
		if !(i < int32((*TIndex)(unsafe.Pointer(pIndex)).FnColumn)) {
			break
		}
		z = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(i)*4))
		if 0 == _sqlite3StrICmp(tls, z, zColl) {
			return int32(1)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** Return true if column iCol of table pTab seem like it might be a
//	** good column to use as part of a query-time index.
//	**
//	** Current algorithm (subject to improvement!):
//	**
//	**   1.   If iCol is already the left-most column of some other index,
//	**        then return false.
//	**
//	**   2.   If iCol is part of an existing index that has an aiRowLogEst of
//	**        more than 20, then return false.
//	**
//	**   3.   If no disqualifying conditions above are found, return true.
//	**
//	** 2025-01-03: I experimented with a new rule that returns false if the
//	** the datatype of the column is "BOOLEAN". This did not improve
//	** performance on any queries at hand, but it did burn CPU cycles, so the
//	** idea was not committed.
//	*/
func _columnIsGoodIndexCandidate(tls *libc.TLS, pTab uintptr, iCol int32) (r int32) {
	var j int32
	var pIdx uintptr
	_, _ = j, pIdx
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != uintptr(0)) {
			break
		}
		j = 0
		for {
			if !(j < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
				break
			}
			if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2))) == iCol {
				if j == 0 {
					return 0
				}
				if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x80>>7)) != 0 && int32(**(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst + uintptr(j+int32(1))*2))) > int32(20) {
					return 0
				}
				break
			}
			goto _2
		_2:
			;
			j = j + 1
		}
		goto _1
	_1:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** Check to see if column iCol of the given statement is valid.  If
//	** it is, return a pointer to the Mem for the value of that column.
//	** If iCol is not valid, return a pointer to a Mem which has a value
//	** of NULL.
//	*/
func _columnMem(tls *libc.TLS, pStmt uintptr, i int32) (r uintptr) {
	var pOut, pVm uintptr
	_, _ = pOut, pVm
	pVm = pStmt
	if pVm == uintptr(0) {
		return _columnNullValue(tls)
	}
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(pVm)).Fdb)).Fmutex)
	if (*TVdbe)(unsafe.Pointer(pVm)).FpResultRow != uintptr(0) && i < int32((*TVdbe)(unsafe.Pointer(pVm)).FnResColumn) && i >= 0 {
		pOut = (*TVdbe)(unsafe.Pointer(pVm)).FpResultRow + uintptr(i)*40
	} else {
		_sqlite3Error(tls, (*TVdbe)(unsafe.Pointer(pVm)).Fdb, int32(SQLITE_RANGE))
		pOut = _columnNullValue(tls)
	}
	return pOut
}

// C documentation
//
//	/*
//	** Convert the N-th element of pStmt->pColName[] into a string using
//	** xFunc() then return that string.  If N is out of range, return 0.
//	**
//	** There are up to 5 names for each column.  useType determines which
//	** name is returned.  Here are the names:
//	**
//	**    0      The column name as it should be displayed for output
//	**    1      The datatype name for the column
//	**    2      The name of the database that the column derives from
//	**    3      The name of the table that the column derives from
//	**    4      The name of the table column that the result column derives from
//	**
//	** If the result is not a simple column reference (if it is an expression
//	** or a constant) then useTypes 2, 3, and 4 return NULL.
//	*/
func _columnName(tls *libc.TLS, pStmt uintptr, N int32, useUtf16 int32, useType int32) (r uintptr) {
	var db, p, ret uintptr
	var i, n, v1 int32
	var prior_mallocFailed Tu8
	_, _, _, _, _, _, _ = db, i, n, p, prior_mallocFailed, ret, v1
	if N < 0 {
		return uintptr(0)
	}
	ret = uintptr(0)
	p = pStmt
	db = (*TVdbe)(unsafe.Pointer(p)).Fdb
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) != 0 {
		if useType > 0 {
			goto columnName_end
		}
		if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) == int32(1) {
			v1 = int32(8)
		} else {
			v1 = int32(4)
		}
		n = v1
		if N >= n {
			goto columnName_end
		}
		if useUtf16 != 0 {
			i = int32(_iExplainColNames16[N+int32(8)*int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2))-int32(8)])
			ret = uintptr(unsafe.Pointer(&_azExplainColNames16data)) + uintptr(i)*2
		} else {
			ret = _azExplainColNames8[N+int32(8)*int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2))-int32(8)]
		}
		goto columnName_end
	}
	n = int32((*TVdbe)(unsafe.Pointer(p)).FnResColumn)
	if N < n {
		prior_mallocFailed = (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed
		N = N + useType*n
		if useUtf16 != 0 {
			ret = Xsqlite3_value_text16(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName+uintptr(N)*40)
		} else {
			ret = Xsqlite3_value_text(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName+uintptr(N)*40)
		}
		/* A malloc may have failed inside of the _text() call. If this
		 ** is the case, clear the mallocFailed flag and return NULL.
		 */
		if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) > int32(prior_mallocFailed) {
			_sqlite3OomClear(tls, db)
			ret = uintptr(0)
		}
	}
	goto columnName_end
columnName_end:
	;
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return ret
}

// C documentation
//
//	/*
//	** Return a pointer to a string containing the 'declaration type' of the
//	** expression pExpr. The string may be treated as static by the caller.
//	**
//	** The declaration type is the exact datatype definition extracted from the
//	** original CREATE TABLE statement if the expression is a column. The
//	** declaration type for a ROWID field is INTEGER. Exactly when an expression
//	** is considered a column can be complex in the presence of subqueries. The
//	** result-set expression in all of the following SELECT statements is
//	** considered a column by this function.
//	**
//	**   SELECT col FROM tbl;
//	**   SELECT (SELECT col FROM tbl;
//	**   SELECT (SELECT col FROM tbl);
//	**   SELECT abc FROM (SELECT col AS abc FROM tbl);
//	**
//	** The declaration type for any expression other than a column is NULL.
//	**
//	** This routine has either 3 or 6 parameters depending on whether or not
//	** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
//	*/
func _columnTypeImpl(tls *libc.TLS, pNC uintptr, pExpr uintptr, pzOrigDb uintptr, pzOrigTab uintptr, pzOrigCol uintptr) (r uintptr) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var iCol, iDb, j int32
	var p, p1, pS, pS1, pTab, pTabList, zType uintptr
	var _ /* sNC at bp+12 */ TNameContext
	var _ /* sNC at bp+48 */ TNameContext
	var _ /* zOrigCol at bp+8 */ uintptr
	var _ /* zOrigDb at bp+0 */ uintptr
	var _ /* zOrigTab at bp+4 */ uintptr
	_, _, _, _, _, _, _, _, _, _ = iCol, iDb, j, p, p1, pS, pS1, pTab, pTabList, zType
	zType = uintptr(0)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
	switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) {
	case int32(TK_COLUMN):
		/* The expression is a column. Locate the table the column is being
		 ** extracted from in NameContext.pSrcList. This table may be real
		 ** database table or a subquery.
		 */
		pTab = uintptr(0)                                      /* Table structure column is extracted from */
		pS = uintptr(0)                                        /* Select the column is extracted from */
		iCol = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) /* Index of column in pTab */
		for pNC != 0 && !(pTab != 0) {
			pTabList = (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList
			j = 0
			for {
				if !(j < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc && (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(j)*48))).FiCursor != (*TExpr)(unsafe.Pointer(pExpr)).FiTable) {
					break
				}
				goto _1
			_1:
				;
				j = j + 1
			}
			if j < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc {
				pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(j)*48))).FpSTab
				if int32(*(*uint32)(unsafe.Pointer(pTabList + 8 + uintptr(j)*48 + 12 + 4))&0x4>>2) != 0 {
					pS = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pTabList + 8 + uintptr(j)*48 + 44)))).FpSelect
				} else {
					pS = uintptr(0)
				}
			} else {
				pNC = (*TNameContext)(unsafe.Pointer(pNC)).FpNext
			}
		}
		if pTab == uintptr(0) {
			/* At one time, code such as "SELECT new.x" within a trigger would
			 ** cause this condition to run.  Since then, we have restructured how
			 ** trigger code is generated and so this condition is no longer
			 ** possible. However, it can still be true for statements like
			 ** the following:
			 **
			 **   CREATE TABLE t1(col INTEGER);
			 **   SELECT (SELECT t1.col) FROM FROM t1;
			 **
			 ** when columnType() is called on the expression "t1.col" in the
			 ** sub-select. In this case, set the column type to NULL, even
			 ** though it should really be "INTEGER".
			 **
			 ** This is not a problem, as the column type of "t1.col" is never
			 ** used. When columnType() is called on the expression
			 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
			 ** branch below.  */
			break
		}
		if pS != 0 {
			/* The "table" is actually a sub-select or a view in the FROM clause
			 ** of the SELECT statement. Return the declaration type and origin
			 ** data for the result-set column of the sub-select.
			 */
			if iCol < (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS)).FpEList)).FnExpr && libc.Bool(libc.Bool(!(libc.Int32FromInt32(ViewCanHaveRowid) != 0)) || iCol >= 0) {
				p = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS)).FpEList + 8 + uintptr(iCol)*20))).FpExpr
				(**(**TNameContext)(__ccgo_up(bp + 12))).FpSrcList = (*TSelect)(unsafe.Pointer(pS)).FpSrc
				(**(**TNameContext)(__ccgo_up(bp + 12))).FpNext = pNC
				(**(**TNameContext)(__ccgo_up(bp + 12))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse
				zType = _columnTypeImpl(tls, bp+12, p, bp, bp+4, bp+8)
			}
		} else {
			/* A real table or a CTE table */
			if iCol < 0 {
				iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey)
			}
			if iCol < 0 {
				zType = __ccgo_ts + 1185
				**(**uintptr)(__ccgo_up(bp + 8)) = __ccgo_ts + 19186
			} else {
				**(**uintptr)(__ccgo_up(bp + 8)) = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName
				zType = _sqlite3ColumnType(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(iCol)*12, uintptr(0))
			}
			**(**uintptr)(__ccgo_up(bp + 4)) = (*TTable)(unsafe.Pointer(pTab)).FzName
			if (*TNameContext)(unsafe.Pointer(pNC)).FpParse != 0 && (*TTable)(unsafe.Pointer(pTab)).FpSchema != 0 {
				iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer((*TNameContext)(unsafe.Pointer(pNC)).FpParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
				**(**uintptr)(__ccgo_up(bp)) = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TNameContext)(unsafe.Pointer(pNC)).FpParse)).Fdb)).FaDb + uintptr(iDb)*16))).FzDbSName
			}
		}
	case int32(TK_SELECT):
		pS1 = *(*uintptr)(unsafe.Pointer(pExpr + 20))
		p1 = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS1)).FpEList + 8))).FpExpr
		(**(**TNameContext)(__ccgo_up(bp + 48))).FpSrcList = (*TSelect)(unsafe.Pointer(pS1)).FpSrc
		(**(**TNameContext)(__ccgo_up(bp + 48))).FpNext = pNC
		(**(**TNameContext)(__ccgo_up(bp + 48))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse
		zType = _columnTypeImpl(tls, bp+48, p1, bp, bp+4, bp+8)
		break
	}
	if pzOrigDb != 0 {
		**(**uintptr)(__ccgo_up(pzOrigDb)) = **(**uintptr)(__ccgo_up(bp))
		**(**uintptr)(__ccgo_up(pzOrigTab)) = **(**uintptr)(__ccgo_up(bp + 4))
		**(**uintptr)(__ccgo_up(pzOrigCol)) = **(**uintptr)(__ccgo_up(bp + 8))
	}
	return zType
}

// C documentation
//
//	/*
//	** Return a pointer to the column affinity string associated with index
//	** pIdx. A column affinity string has one character for each column in
//	** the table, according to the affinity of the column:
//	**
//	**  Character      Column affinity
//	**  ------------------------------
//	**  'A'            BLOB
//	**  'B'            TEXT
//	**  'C'            NUMERIC
//	**  'D'            INTEGER
//	**  'F'            REAL
//	**
//	** An extra 'D' is appended to the end of the string to cover the
//	** rowid that appears as the last column in every index.
//	**
//	** Memory for the buffer containing the column index affinity string
//	** is managed along with the rest of the Index structure. It will be
//	** released when sqlite3DeleteIndex() is called.
//	*/
func _computeIndexAffStr(tls *libc.TLS, db uintptr, pIdx uintptr) (r uintptr) {
	var aff int8
	var n int32
	var pTab uintptr
	var x Ti16
	_, _, _, _ = aff, n, pTab, x
	pTab = (*TIndex)(unsafe.Pointer(pIdx)).FpTable
	(*TIndex)(unsafe.Pointer(pIdx)).FzColAff = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)+int32(1)))
	if !((*TIndex)(unsafe.Pointer(pIdx)).FzColAff != 0) {
		_sqlite3OomFault(tls, db)
		return uintptr(0)
	}
	n = 0
	for {
		if !(n < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) {
			break
		}
		x = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(n)*2))
		if int32(x) >= 0 {
			aff = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(x)*12))).Faffinity
		} else {
			if int32(x) == -int32(1) {
				aff = int8(SQLITE_AFF_INTEGER)
			} else {
				aff = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(n)*20))).FpExpr)
			}
		}
		if int32(aff) < int32(SQLITE_AFF_BLOB) {
			aff = int8(SQLITE_AFF_BLOB)
		}
		if int32(aff) > int32(SQLITE_AFF_NUMERIC) {
			aff = int8(SQLITE_AFF_NUMERIC)
		}
		**(**int8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FzColAff + uintptr(n))) = aff
		goto _1
	_1:
		;
		n = n + 1
	}
	**(**int8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FzColAff + uintptr(n))) = 0
	return (*TIndex)(unsafe.Pointer(pIdx)).FzColAff
}

// C documentation
//
//	/*
//	** Compute the iLimit and iOffset fields of the SELECT based on the
//	** pLimit expressions.  pLimit->pLeft and pLimit->pRight hold the expressions
//	** that appear in the original SQL statement after the LIMIT and OFFSET
//	** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset
//	** are the integer memory register numbers for counters used to compute
//	** the limit and offset.  If there is no limit and/or offset, then
//	** iLimit and iOffset are negative.
//	**
//	** This routine changes the values of iLimit and iOffset only if
//	** a limit or offset is defined by pLimit->pLeft and pLimit->pRight.  iLimit
//	** and iOffset should have been preset to appropriate default values (zero)
//	** prior to calling this routine.
//	**
//	** The iOffset register (if it exists) is initialized to the value
//	** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
//	** iOffset+1 is initialized to LIMIT+OFFSET.
//	**
//	** Only if pLimit->pLeft!=0 do the limit registers get
//	** redefined.  The UNION ALL operator uses this property to force
//	** the reuse of the same limit and offset registers across multiple
//	** SELECT statements.
//	*/
func _computeLimitRegisters(tls *libc.TLS, pParse uintptr, p uintptr, iBreak int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iLimit, iOffset, v1, v2 int32
	var pLimit, v, v3 uintptr
	var _ /* n at bp+0 */ int32
	_, _, _, _, _, _, _ = iLimit, iOffset, pLimit, v, v1, v2, v3
	v = uintptr(0)
	iLimit = 0
	pLimit = (*TSelect)(unsafe.Pointer(p)).FpLimit
	if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 {
		return
	}
	/*
	 ** "LIMIT -1" always shows all rows.  There is some
	 ** controversy about what the correct behavior should be.
	 ** The current implementation interprets "LIMIT 0" to mean
	 ** no rows.
	 */
	if pLimit != 0 {
		v3 = pParse + 48
		*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
		v2 = *(*int32)(unsafe.Pointer(v3))
		v1 = v2
		iLimit = v1
		(*TSelect)(unsafe.Pointer(p)).FiLimit = v1
		v = _sqlite3GetVdbe(tls, pParse)
		if _sqlite3ExprIsInteger(tls, (*TExpr)(unsafe.Pointer(pLimit)).FpLeft, bp, pParse) != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), **(**int32)(__ccgo_up(bp)), iLimit)
			if **(**int32)(__ccgo_up(bp)) == 0 {
				_sqlite3VdbeGoto(tls, v, iBreak)
			} else {
				if **(**int32)(__ccgo_up(bp)) >= 0 && int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32(_sqlite3LogEst(tls, uint64(**(**int32)(__ccgo_up(bp))))) {
					(*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEst(tls, uint64(**(**int32)(__ccgo_up(bp))))
					**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_FixedLimit)
				}
			}
		} else {
			_sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pLimit)).FpLeft, iLimit)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), iLimit)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IfNot), iLimit, iBreak)
		}
		if (*TExpr)(unsafe.Pointer(pLimit)).FpRight != 0 {
			v3 = pParse + 48
			*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
			v2 = *(*int32)(unsafe.Pointer(v3))
			v1 = v2
			iOffset = v1
			(*TSelect)(unsafe.Pointer(p)).FiOffset = v1
			(*TParse)(unsafe.Pointer(pParse)).FnMem = (*TParse)(unsafe.Pointer(pParse)).FnMem + 1 /* Allocate an extra register for limit+offset */
			_sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pLimit)).FpRight, iOffset)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), iOffset)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_OffsetLimit), iLimit, iOffset+int32(1), iOffset)
		}
	}
}

// C documentation
//
//	/*
//	** Compute the maximum number of paths in the solver algorithm, for
//	** queries that have three or more terms in the FROM clause.  Queries with
//	** two or fewer FROM clause terms are handled by the caller.
//	**
//	** Query planning is NP-hard.  We must limit the number of paths at
//	** each step of the solver search algorithm to avoid exponential behavior.
//	**
//	** The value returned is a tuning parameter.  Currently the value is:
//	**
//	**     18    for star queries
//	**     12    otherwise
//	**
//	** For the purposes of this heuristic, a star-query is defined as a query
//	** with a central "fact" table that is joined against multiple
//	** "dimension" tables, subject to the following constraints:
//	**
//	**   (aa)  Only a five-way or larger join is considered for this
//	**         optimization.  If there are fewer than four terms in the FROM
//	**         clause, this heuristic does not apply.
//	**
//	**   (bb)  The join between the fact table and the dimension tables must
//	**         be an INNER join.  CROSS and OUTER JOINs do not qualify.
//	**
//	**   (cc)  A table must have 3 or more dimension tables in order to be
//	**         considered a fact table. (Was 4 prior to 2026-02-10.)
//	**
//	**   (dd)  A table that is a self-join cannot be a dimension table.
//	**         Dimension tables are joined against fact tables.
//	**
//	** SIDE EFFECT:  (and really the whole point of this subroutine)
//	**
//	** If pWInfo describes a star-query, then the cost for SCANs of dimension
//	** WhereLoops is increased to be slightly larger than the cost of a SCAN
//	** in the fact table.  Only SCAN costs are increased.  SEARCH costs are
//	** unchanged. This heuristic helps keep fact tables in outer loops. Without
//	** this heuristic, paths with fact tables in outer loops tend to get pruned
//	** by the mxChoice limit on the number of paths, resulting in poor query
//	** plans.  See the starschema1.test test module for examples of queries
//	** that need this heuristic to find good query plans.
//	**
//	** This heuristic can be completely disabled, so that no query is
//	** considered a star-query, using SQLITE_TESTCTRL_OPTIMIZATION to
//	** disable the SQLITE_StarQuery optimization.  In the CLI, the command
//	** to do that is:  ".testctrl opt -starquery".
//	**
//	** HISTORICAL NOTES:
//	**
//	** This optimization was first added on 2024-05-09 by check-in 38db9b5c83d.
//	** The original optimization reduced the cost and output size estimate for
//	** fact tables to help them move to outer loops.  But months later (as people
//	** started upgrading) performance regression reports started caming in,
//	** including:
//	**
//	**    forum post b18ef983e68d06d1 (2024-12-21)
//	**    forum post 0025389d0860af82 (2025-01-14)
//	**    forum post d87570a145599033 (2025-01-17)
//	**
//	** To address these, the criteria for a star-query was tightened to exclude
//	** cases where the fact and dimensions are separated by an outer join, and
//	** the affect of star-schema detection was changed to increase the rRun cost
//	** on just full table scans of dimension tables, rather than reducing costs
//	** in the all access methods of the fact table.
//	*/
func _computeMxChoice(tls *libc.TLS, pWInfo uintptr) (r int32) {
	var aFromTabs, pFactTab, pStart, pWLoop uintptr
	var iFromIdx, nDep, nLoop, v5 int32
	var m, mSeen, mSelfJoin TBitmask
	var mxRun TLogEst
	_, _, _, _, _, _, _, _, _, _, _, _ = aFromTabs, iFromIdx, m, mSeen, mSelfJoin, mxRun, nDep, nLoop, pFactTab, pStart, pWLoop, v5
	nLoop = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)                                                                                                                                                                                                                        /* For looping over WhereLoops */
	if nLoop >= int32(4) && !(int32(uint32(*(*uint8)(unsafe.Pointer(pWInfo + 48))&0x10>>4)) != 0) && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_StarQuery)) == uint32(0) { /* Bitmask for candidate fact-table */
		mSelfJoin = uint64(0)                                                   /* Where to start searching for dimension-tables */
		libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 4, 0x10) /* Only do this computation once */
		/* Look for fact tables with three or more dimensions where the
		 ** dimension tables are not separately from the fact tables by an outer
		 ** or cross join.  Adjust cost weights if found.
		 */
		aFromTabs = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8
		pStart = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops
		iFromIdx = 0
		m = libc.Uint64FromInt32(1)
		for {
			if !(iFromIdx < nLoop) {
				break
			}
			nDep = 0          /* Maximum SCAN cost of a fact table */
			mSeen = uint64(0) /* The candidate fact table */
			pFactTab = aFromTabs + uintptr(iFromIdx)*48
			if int32((*TSrcItem)(unsafe.Pointer(pFactTab)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)) != 0 {
				/* If the candidate fact-table is the right table of an outer join
				 ** restrict the search for dimension-tables to be tables to the right
				 ** of the fact-table.  Constraint (bb) */
				if iFromIdx+int32(3) > nLoop {
					break /* ^-- Impossible to reach nDep>=2 - Constraint (cc) */
				}
				for pStart != 0 && int32((*TWhereLoop)(unsafe.Pointer(pStart)).FiTab) <= iFromIdx {
					pStart = (*TWhereLoop)(unsafe.Pointer(pStart)).FpNextLoop
				}
			}
			pWLoop = pStart
			for {
				if !(pWLoop != 0) {
					break
				}
				if int32((**(**TSrcItem)(__ccgo_up(aFromTabs + uintptr((*TWhereLoop)(unsafe.Pointer(pWLoop)).FiTab)*48))).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)) != 0 {
					break /* Constraint (bb) */
				}
				if (*TWhereLoop)(unsafe.Pointer(pWLoop)).Fprereq&m != uint64(0) && (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf&mSeen == uint64(0) && (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf&mSelfJoin == uint64(0) {
					if (**(**TSrcItem)(__ccgo_up(aFromTabs + uintptr((*TWhereLoop)(unsafe.Pointer(pWLoop)).FiTab)*48))).FpSTab == (*TSrcItem)(unsafe.Pointer(pFactTab)).FpSTab {
						mSelfJoin = mSelfJoin | m
					} else {
						nDep = nDep + 1
						mSeen = mSeen | (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf
					}
				}
				goto _2
			_2:
				;
				pWLoop = (*TWhereLoop)(unsafe.Pointer(pWLoop)).FpNextLoop
			}
			if nDep <= int32(2) {
				goto _1 /* Constraint (cc) */
			}
			/* If we reach this point, it means that pFactTab is a fact table
			 ** with four or more dimensions connected by inner joins.  Proceed
			 ** to make cost adjustments. */
			libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 5, 0x20)
			/* Compute the maximum cost of any WhereLoop for the
			 ** fact table plus one epsilon */
			mxRun = int16(-libc.Int32FromInt32(32768))
			pWLoop = pStart
			for {
				if !(pWLoop != 0) {
					break
				}
				if int32((*TWhereLoop)(unsafe.Pointer(pWLoop)).FiTab) < iFromIdx {
					goto _3
				}
				if int32((*TWhereLoop)(unsafe.Pointer(pWLoop)).FiTab) > iFromIdx {
					break
				}
				if int32((*TWhereLoop)(unsafe.Pointer(pWLoop)).FrRun) > int32(mxRun) {
					mxRun = (*TWhereLoop)(unsafe.Pointer(pWLoop)).FrRun
				}
				goto _3
			_3:
				;
				pWLoop = (*TWhereLoop)(unsafe.Pointer(pWLoop)).FpNextLoop
			}
			if int32(mxRun) < int32(LOGEST_MAX) {
				mxRun = mxRun + 1
			}
			/* Increase the cost of table scans for dimension tables to be
			 ** slightly more than the maximum cost of the fact table */
			pWLoop = pStart
			for {
				if !(pWLoop != 0) {
					break
				}
				if (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf&mSeen == uint64(0) {
					goto _4
				}
				if (*TWhereLoop)(unsafe.Pointer(pWLoop)).FnLTerm != 0 {
					goto _4
				}
				if int32((*TWhereLoop)(unsafe.Pointer(pWLoop)).FrRun) < int32(mxRun) {
					(*TWhereLoop)(unsafe.Pointer(pWLoop)).FrRun = mxRun
				}
				goto _4
			_4:
				;
				pWLoop = (*TWhereLoop)(unsafe.Pointer(pWLoop)).FpNextLoop
			}
			goto _1
		_1:
			;
			iFromIdx = iFromIdx + 1
			m = m << uint64(1)
		}
	}
	if int32(uint32(*(*uint8)(unsafe.Pointer(pWInfo + 48))&0x20>>5)) != 0 {
		v5 = int32(18)
	} else {
		v5 = int32(12)
	}
	return v5
}

// C documentation
//
//	/* The core implementation of the CONCAT(...) and CONCAT_WS(SEP,...)
//	** functions.
//	**
//	** Return a string value that is the concatenation of all non-null
//	** entries in argv[].  Use zSep as the separator.
//	*/
func _concatFuncCore(tls *libc.TLS, context uintptr, argc int32, argv uintptr, nSep int32, zSep uintptr) {
	var bNotNull, i, k int32
	var j, n Ti64
	var v, z uintptr
	_, _, _, _, _, _, _ = bNotNull, i, j, k, n, v, z
	n = 0
	bNotNull = 0
	i = 0
	for {
		if !(i < argc) {
			break
		}
		n = n + int64(Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4))))
		goto _1
	_1:
		;
		i = i + 1
	}
	n = n + int64(argc-libc.Int32FromInt32(1))*int64(nSep)
	z = Xsqlite3_malloc64(tls, uint64(n+int64(1)))
	if z == uintptr(0) {
		Xsqlite3_result_error_nomem(tls, context)
		return
	}
	j = 0
	i = 0
	for {
		if !(i < argc) {
			break
		}
		if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4))) != int32(SQLITE_NULL) {
			k = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
			v = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
			if v != uintptr(0) {
				if bNotNull != 0 && nSep > 0 {
					libc.Xmemcpy(tls, z+uintptr(j), zSep, uint32(nSep))
					j = j + int64(nSep)
				}
				libc.Xmemcpy(tls, z+uintptr(j), v, uint32(k))
				j = j + int64(k)
				bNotNull = int32(1)
			}
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	**(**int8)(__ccgo_up(z + uintptr(j))) = 0
	Xsqlite3_result_text64(tls, context, z, uint64(j), __ccgo_fp(Xsqlite3_free), uint8(SQLITE_UTF8_ZT))
}

// C documentation
//
//	/*
//	** Add a new entry to the pConst object.  Except, do not add duplicate
//	** pColumn entries.  Also, do not add if doing so would not be appropriate.
//	**
//	** The caller guarantees the pColumn is a column and pValue is a constant.
//	** This routine has to do some additional checks before completing the
//	** insert.
//	*/
func _constInsert(tls *libc.TLS, pConst uintptr, pColumn uintptr, pValue uintptr, pExpr uintptr) {
	var i int32
	var pE2 uintptr
	_, _ = i, pE2
	if (*TExpr)(unsafe.Pointer(pColumn)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != uint32(0) {
		return
	}
	if int32(_sqlite3ExprAffinity(tls, pValue)) != 0 {
		return
	}
	if !(_sqlite3IsBinary(tls, _sqlite3ExprCompareCollSeq(tls, (*TWhereConst)(unsafe.Pointer(pConst)).FpParse, pExpr)) != 0) {
		return
	}
	/* 2018-10-25 ticket [cf5ed20f]
	 ** Make sure the same pColumn is not inserted more than once */
	i = 0
	for {
		if !(i < (*TWhereConst)(unsafe.Pointer(pConst)).FnConst) {
			break
		}
		pE2 = **(**uintptr)(__ccgo_up((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr(i*int32(2))*4))
		if (*TExpr)(unsafe.Pointer(pE2)).FiTable == (*TExpr)(unsafe.Pointer(pColumn)).FiTable && int32((*TExpr)(unsafe.Pointer(pE2)).FiColumn) == int32((*TExpr)(unsafe.Pointer(pColumn)).FiColumn) {
			return /* Already present.  Return without doing anything. */
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if int32(_sqlite3ExprAffinity(tls, pColumn)) <= int32(SQLITE_AFF_BLOB) {
		(*TWhereConst)(unsafe.Pointer(pConst)).FbHasAffBlob = int32(1)
	}
	(*TWhereConst)(unsafe.Pointer(pConst)).FnConst = (*TWhereConst)(unsafe.Pointer(pConst)).FnConst + 1
	(*TWhereConst)(unsafe.Pointer(pConst)).FapExpr = _sqlite3DbReallocOrFree(tls, (*TParse)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FpParse)).Fdb, (*TWhereConst)(unsafe.Pointer(pConst)).FapExpr, uint64(uint32((*TWhereConst)(unsafe.Pointer(pConst)).FnConst*int32(2))*uint32(4)))
	if (*TWhereConst)(unsafe.Pointer(pConst)).FapExpr == uintptr(0) {
		(*TWhereConst)(unsafe.Pointer(pConst)).FnConst = 0
	} else {
		**(**uintptr)(__ccgo_up((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr((*TWhereConst)(unsafe.Pointer(pConst)).FnConst*int32(2)-int32(2))*4)) = pColumn
		**(**uintptr)(__ccgo_up((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr((*TWhereConst)(unsafe.Pointer(pConst)).FnConst*int32(2)-int32(1))*4)) = pValue
	}
}

// C documentation
//
//	/*
//	** We know that pSrc is an operand of an outer join.  Return true if
//	** pTerm is a constraint that is compatible with that join.
//	**
//	** pTerm must be EP_OuterON if pSrc is the right operand of an
//	** outer join.  pTerm can be either EP_OuterON or EP_InnerON if pSrc
//	** is the left operand of a RIGHT join.
//	**
//	** See https://sqlite.org/forum/forumpost/206d99a16dd9212f
//	** for an example of a WHERE clause constraints that may not be used on
//	** the right table of a RIGHT JOIN because the constraint implies a
//	** not-NULL condition on the left table of the RIGHT JOIN.
//	*/
func _constraintCompatibleWithOuterJoin(tls *libc.TLS, pTerm uintptr, pSrc uintptr) (r int32) {
	/* By caller */
	if !((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != libc.Uint32FromInt32(0)) || *(*int32)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr + 36)) != (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor {
		return 0
	}
	if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_RIGHT)) != 0 && (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)) != uint32(0) {
		return 0
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** Generate code to construct the Index object for an automatic index
//	** and to set up the WhereLevel object pLevel so that the code generator
//	** makes use of the automatic index.
//	*/
func _constructAutomaticIndex(tls *libc.TLS, pParse uintptr, pWC uintptr, notReady TBitmask, pLevel uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var addrCounter, addrInit, addrTop, i, iCol, iCol1, iContinue, mxBitCol, n, nKeyCol, regBase, regRecord, regYield, v3 int32
	var cMask, cMask1, extraCols, idxCols TBitmask
	var pColl, pExpr, pIdx, pLoop, pPartial, pSrc, pSubq, pTabList, pTable, pTerm, pWCEnd, pX, v, v10 uintptr
	var sentWarning, useBloomFilter Tu8
	var v2 uint64
	var v4 Tu16
	var _ /* zNotUsed at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrCounter, addrInit, addrTop, cMask, cMask1, extraCols, i, iCol, iCol1, iContinue, idxCols, mxBitCol, n, nKeyCol, pColl, pExpr, pIdx, pLoop, pPartial, pSrc, pSubq, pTabList, pTable, pTerm, pWCEnd, pX, regBase, regRecord, regYield, sentWarning, useBloomFilter, v, v10, v2, v3, v4 /* Bitmap of additional columns */
	sentWarning = uint8(0)                                                                                                                                                                                                                                                                                                                                                                                /* True if a warning has been issued */
	useBloomFilter = uint8(0)                                                                                                                                                                                                                                                                                                                                                                             /* True to also add a Bloom filter */
	pPartial = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                 /* Partial Index Expression */
	iContinue = 0                                                                                                                                                                                                                                                                                                                                                                                         /* The FROM clause term to get the next index */
	addrCounter = 0                                                                                                                                                                                                                                                                                                                                                                                       /* Array of registers where record is assembled */
	/* Generate code to skip over the creation and initialization of the
	 ** transient index on 2nd and subsequent iterations of the loop. */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	addrInit = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
	/* Count the number of columns that will be added to the index
	 ** and used to match WHERE clause constraints */
	nKeyCol = 0
	pTabList = (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpTabList
	pSrc = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
	pTable = (*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab
	pWCEnd = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm)*48
	pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
	idxCols = uint64(0)
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
	for {
		if !(pTerm < pWCEnd) {
			break
		}
		pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
		/* Make the automatic index a partial index if there are terms in the
		 ** WHERE clause (or the ON clause of a LEFT join) that constrain which
		 ** rows of the target table (pSrc) that can be used. */
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VIRTUAL) == 0 && _sqlite3ExprIsSingleTableConstraint(tls, pExpr, pTabList, int32((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom), 0) != 0 {
			pPartial = _sqlite3ExprAnd(tls, pParse, pPartial, _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, 0))
		}
		if _termCanDriveIndex(tls, pTerm, pSrc, notReady) != 0 {
			iCol = (*(*struct {
				FleftColumn int32
				FiField     int32
			})(unsafe.Pointer(pTerm + 24))).FleftColumn
			if iCol >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
				v2 = libc.Uint64FromInt32(1) << (int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1))
			} else {
				v2 = libc.Uint64FromInt32(1) << iCol
			}
			cMask = v2
			if !(sentWarning != 0) {
				Xsqlite3_log(tls, libc.Int32FromInt32(SQLITE_WARNING)|libc.Int32FromInt32(1)<<libc.Int32FromInt32(8), __ccgo_ts+25218, libc.VaList(bp+16, (*TTable)(unsafe.Pointer(pTable)).FzName, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTable)).FaCol + uintptr(iCol)*12))).FzCnName))
				sentWarning = uint8(1)
			}
			if idxCols&cMask == uint64(0) {
				if _whereLoopResize(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pLoop, nKeyCol+int32(1)) != 0 {
					goto end_auto_index_create
				}
				v3 = nKeyCol
				nKeyCol = nKeyCol + 1
				**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(v3)*4)) = pTerm
				idxCols = idxCols | cMask
			}
		}
		goto _1
	_1:
		;
		pTerm += 48
	}
	v4 = uint16(nKeyCol)
	(*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm = v4
	(*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FnEq = v4
	(*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ) | libc.Int32FromInt32(WHERE_IDX_ONLY) | libc.Int32FromInt32(WHERE_INDEXED) | libc.Int32FromInt32(WHERE_AUTO_INDEX))
	/* Count the number of additional columns needed to create a
	 ** covering index.  A "covering index" is an index that contains all
	 ** columns that are needed by the query.  With a covering index, the
	 ** original table never needs to be accessed.  Automatic indices must
	 ** be a covering index because the index will not be updated if the
	 ** original table changes and the index and table cannot both be used
	 ** if they go out of sync.
	 */
	if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == int32(TABTYP_VIEW) {
		extraCols = uint64(-libc.Int32FromInt32(1)) & ^idxCols
	} else {
		extraCols = (*TSrcItem)(unsafe.Pointer(pSrc)).FcolUsed & (^idxCols | libc.Uint64FromInt32(1)<<(int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1)))
	}
	if !((*TTable)(unsafe.Pointer(pTable)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
		/* For WITHOUT ROWID tables, ensure that all PRIMARY KEY columns are
		 ** either in the idxCols mask or in the extraCols mask */
		i = 0
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTable)).FnCol)) {
				break
			}
			if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTable)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_PRIMKEY) == 0 {
				goto _5
			}
			if i >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) {
				extraCols = extraCols | libc.Uint64FromInt32(1)<<(int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1))
				break
			}
			if idxCols&(libc.Uint64FromInt32(1)<<i) != 0 {
				goto _5
			}
			extraCols = extraCols | libc.Uint64FromInt32(1)<<i
			goto _5
		_5:
			;
			i = i + 1
		}
	}
	if int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) < int32((*TTable)(unsafe.Pointer(pTable)).FnCol) {
		v3 = int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1)
	} else {
		v3 = int32((*TTable)(unsafe.Pointer(pTable)).FnCol)
	}
	mxBitCol = v3
	i = 0
	for {
		if !(i < mxBitCol) {
			break
		}
		if extraCols&(libc.Uint64FromInt32(1)<<i) != 0 {
			nKeyCol = nKeyCol + 1
		}
		goto _7
	_7:
		;
		i = i + 1
	}
	if (*TSrcItem)(unsafe.Pointer(pSrc)).FcolUsed&(libc.Uint64FromInt32(1)<<(int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1))) != 0 {
		nKeyCol = nKeyCol + (int32((*TTable)(unsafe.Pointer(pTable)).FnCol) - int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) + int32(1))
	}
	/* Construct the Index object to describe this index */
	/* ^-- This guarantees that the number of index columns will fit in the u16 */
	pIdx = _sqlite3AllocateIndexObject(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, nKeyCol+libc.BoolInt32((*TTable)(unsafe.Pointer(pTable)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0)), 0, bp)
	if pIdx == uintptr(0) {
		goto end_auto_index_create
	}
	(*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FpIndex = pIdx
	(*TIndex)(unsafe.Pointer(pIdx)).FzName = __ccgo_ts + 25244
	(*TIndex)(unsafe.Pointer(pIdx)).FpTable = pTable
	n = 0
	idxCols = uint64(0)
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
	for {
		if !(pTerm < pWCEnd) {
			break
		}
		if _termCanDriveIndex(tls, pTerm, pSrc, notReady) != 0 {
			iCol1 = (*(*struct {
				FleftColumn int32
				FiField     int32
			})(unsafe.Pointer(pTerm + 24))).FleftColumn
			if iCol1 >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
				v2 = libc.Uint64FromInt32(1) << (int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1))
			} else {
				v2 = libc.Uint64FromInt32(1) << iCol1
			}
			cMask1 = v2
			if idxCols&cMask1 == uint64(0) {
				pX = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
				idxCols = idxCols | cMask1
				**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(n)*2)) = int16((*(*struct {
					FleftColumn int32
					FiField     int32
				})(unsafe.Pointer(pTerm + 24))).FleftColumn)
				pColl = _sqlite3ExprCompareCollSeq(tls, pParse, pX)
				/* TH3 collate01.800 */
				if pColl != 0 {
					v10 = (*TCollSeq)(unsafe.Pointer(pColl)).FzName
				} else {
					v10 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
				}
				**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(n)*4)) = v10
				n = n + 1
				if (*TExpr)(unsafe.Pointer(pX)).FpLeft != uintptr(0) && int32(_sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft)) != int32(SQLITE_AFF_TEXT) {
					/* TUNING: only use a Bloom filter on an automatic index
					 ** if one or more key columns has the ability to hold numeric
					 ** values, since strings all have the same hash in the Bloom
					 ** filter implementation and hence a Bloom filter on a text column
					 ** is not usually helpful. */
					useBloomFilter = uint8(1)
				}
			}
		}
		goto _8
	_8:
		;
		pTerm += 48
	}
	/* Add additional columns needed to make the automatic index into
	 ** a covering index */
	i = 0
	for {
		if !(i < mxBitCol) {
			break
		}
		if extraCols&(libc.Uint64FromInt32(1)<<i) != 0 {
			**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(n)*2)) = int16(i)
			**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(n)*4)) = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
			n = n + 1
		}
		goto _11
	_11:
		;
		i = i + 1
	}
	if (*TSrcItem)(unsafe.Pointer(pSrc)).FcolUsed&(libc.Uint64FromInt32(1)<<(int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1))) != 0 {
		i = int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1)
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTable)).FnCol)) {
				break
			}
			**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(n)*2)) = int16(i)
			**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(n)*4)) = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
			n = n + 1
			goto _12
		_12:
			;
			i = i + 1
		}
	}
	if (*TTable)(unsafe.Pointer(pTable)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
		**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(n)*2)) = int16(-libc.Int32FromInt32(1))
		**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(n)*4)) = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
	}
	/* Create the automatic index */
	v10 = pParse + 44
	v3 = *(*int32)(unsafe.Pointer(v10))
	*(*int32)(unsafe.Pointer(v10)) = *(*int32)(unsafe.Pointer(v10)) + 1
	(*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur = v3
	_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenAutoindex), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, nKeyCol+int32(1))
	_sqlite3VdbeSetP4KeyInfo(tls, pParse, pIdx)
	if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_BloomFilter)) == uint32(0) && useBloomFilter != 0 {
		_sqlite3WhereExplainBloomFilter(tls, pParse, (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo, pLevel)
		v10 = pParse + 48
		*(*int32)(unsafe.Pointer(v10)) = *(*int32)(unsafe.Pointer(v10)) + 1
		v3 = *(*int32)(unsafe.Pointer(v10))
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter = v3
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Blob), int32(10000), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter)
	}
	/* Fill the automatic index with content */
	if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x40>>6) != 0 {
		pSubq = *(*uintptr)(unsafe.Pointer(pSrc + 44))
		regYield = (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn
		addrCounter = _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, 0)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regYield, 0, (*TSubquery)(unsafe.Pointer(pSubq)).FaddrFillSub)
		addrTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), regYield)
	} else {
		addrTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrHalt)
	}
	if pPartial != 0 {
		iContinue = _sqlite3VdbeMakeLabel(tls, pParse)
		_sqlite3ExprIfFalse(tls, pParse, pPartial, iContinue, int32(SQLITE_JUMPIFNULL))
		**(**Tu32)(__ccgo_up(pLoop + 40)) |= uint32(WHERE_PARTIALIDX)
	}
	regRecord = _sqlite3GetTempReg(tls, pParse)
	regBase = _sqlite3GenerateIndexKey(tls, pParse, pIdx, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, regRecord, 0, uintptr(0), uintptr(0), 0)
	if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter != 0 {
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, 0, regBase, int32((*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pLoop + 24))).FnEq))
	}
	_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, regRecord)
	_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT))
	if pPartial != 0 {
		_sqlite3VdbeResolveLabel(tls, v, iContinue)
	}
	if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x40>>6) != 0 {
		_sqlite3VdbeChangeP2(tls, v, addrCounter, regBase+n)
		_translateColumnToCopy(tls, pParse, addrTop, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pSrc + 44)))).FregResult, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur)
		_sqlite3VdbeGoto(tls, v, addrTop)
		libc.SetBitFieldPtr32Uint32(pSrc+12+4, libc.Uint32FromInt32(0), 6, 0x40)
		_sqlite3VdbeJumpHere(tls, v, addrTop)
	} else {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, addrTop+int32(1))
		_sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_STMTSTATUS_AUTOINDEX))
		if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&int32(JT_LEFT) != 0 {
			_sqlite3VdbeJumpHere(tls, v, addrTop)
		}
	}
	_sqlite3ReleaseTempReg(tls, pParse, regRecord)
	/* Jump here when skipping the initialization */
	_sqlite3VdbeJumpHere(tls, v, addrInit)
	goto end_auto_index_create
end_auto_index_create:
	;
	_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pPartial)
}

// C documentation
//
//	/*
//	** Allocate nByte bytes of space using sqlite3Malloc(). If the
//	** allocation fails, call sqlite3_result_error_nomem() to notify
//	** the database handle that malloc() has failed and return NULL.
//	** If nByte is larger than the maximum string or blob length, then
//	** raise an SQLITE_TOOBIG exception and return NULL.
//	*/
func _contextMalloc(tls *libc.TLS, context uintptr, nByte Ti64) (r uintptr) {
	var db, z uintptr
	_, _ = db, z
	db = Xsqlite3_context_db_handle(tls, context)
	if nByte > int64(**(**int32)(__ccgo_up(db + 120))) {
		Xsqlite3_result_error_toobig(tls, context)
		z = uintptr(0)
	} else {
		z = _sqlite3Malloc(tls, uint64(nByte))
		if !(z != 0) {
			Xsqlite3_result_error_nomem(tls, context)
		}
	}
	return z
}

// C documentation
//
//	/*
//	** Detect compound SELECT statements that use an ORDER BY clause with
//	** an alternative collating sequence.
//	**
//	**    SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
//	**
//	** These are rewritten as a subquery:
//	**
//	**    SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
//	**     ORDER BY ... COLLATE ...
//	**
//	** This transformation is necessary because the multiSelectByMerge() routine
//	** above that generates the code for a compound SELECT with an ORDER BY clause
//	** uses a merge algorithm that requires the same collating sequence on the
//	** result columns as on the ORDER BY clause.  See ticket
//	** http://sqlite.org/src/info/6709574d2a
//	**
//	** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
//	** The UNION ALL operator works fine with multiSelectByMerge() even when
//	** there are COLLATE terms in the ORDER BY.
//	*/
func _convertCompoundSelectToSubquery(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a, db, pNew, pNewSrc, pParse, pX uintptr
	var i int32
	var _ /* dummy at bp+0 */ TToken
	_, _, _, _, _, _, _ = a, db, i, pNew, pNewSrc, pParse, pX
	if (*TSelect)(unsafe.Pointer(p)).FpPrior == uintptr(0) {
		return WRC_Continue
	}
	if (*TSelect)(unsafe.Pointer(p)).FpOrderBy == uintptr(0) {
		return WRC_Continue
	}
	pX = p
	for {
		if !(pX != 0 && (int32((*TSelect)(unsafe.Pointer(pX)).Fop) == int32(TK_ALL) || int32((*TSelect)(unsafe.Pointer(pX)).Fop) == int32(TK_SELECT))) {
			break
		}
		goto _1
	_1:
		;
		pX = (*TSelect)(unsafe.Pointer(pX)).FpPrior
	}
	if pX == uintptr(0) {
		return WRC_Continue
	}
	a = (*TSelect)(unsafe.Pointer(p)).FpOrderBy + 8
	/* If iOrderByCol is already non-zero, then it has already been matched
	 ** to a result column of the SELECT statement. This occurs when the
	 ** SELECT is rewritten for window-functions processing and then passed
	 ** to sqlite3SelectPrep() and similar a second time. The rewriting done
	 ** by this function is not required in this case. */
	if *(*Tu16)(unsafe.Pointer(a + 16)) != 0 {
		return WRC_Continue
	}
	i = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy)).FnExpr - int32(1)
	for {
		if !(i >= 0) {
			break
		}
		if (*TExpr)(unsafe.Pointer((**(**TExprList_item)(__ccgo_up(a + uintptr(i)*20))).FpExpr)).Fflags&uint32(EP_Collate) != 0 {
			break
		}
		goto _2
	_2:
		;
		i = i - 1
	}
	if i < 0 {
		return WRC_Continue
	}
	/* If we reach this point, that means the transformation is required. */
	pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pNew = _sqlite3DbMallocZero(tls, db, uint64(68))
	if pNew == uintptr(0) {
		return int32(WRC_Abort)
	}
	libc.Xmemset(tls, bp, 0, uint32(8))
	pNewSrc = _sqlite3SrcListAppendFromTerm(tls, pParse, uintptr(0), uintptr(0), uintptr(0), bp, pNew, uintptr(0))
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		_sqlite3SrcListDelete(tls, db, pNewSrc)
		return int32(WRC_Abort)
	}
	**(**TSelect)(__ccgo_up(pNew)) = **(**TSelect)(__ccgo_up(p))
	(*TSelect)(unsafe.Pointer(p)).FpSrc = pNewSrc
	(*TSelect)(unsafe.Pointer(p)).FpEList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3Expr(tls, db, int32(TK_ASTERISK), uintptr(0)))
	(*TSelect)(unsafe.Pointer(p)).Fop = uint8(TK_SELECT)
	(*TSelect)(unsafe.Pointer(p)).FpWhere = uintptr(0)
	(*TSelect)(unsafe.Pointer(pNew)).FpGroupBy = uintptr(0)
	(*TSelect)(unsafe.Pointer(pNew)).FpHaving = uintptr(0)
	(*TSelect)(unsafe.Pointer(pNew)).FpOrderBy = uintptr(0)
	(*TSelect)(unsafe.Pointer(p)).FpPrior = uintptr(0)
	(*TSelect)(unsafe.Pointer(p)).FpNext = uintptr(0)
	(*TSelect)(unsafe.Pointer(p)).FpWith = uintptr(0)
	(*TSelect)(unsafe.Pointer(p)).FpWinDefn = uintptr(0)
	**(**Tu32)(__ccgo_up(p + 4)) &= ^libc.Uint32FromInt32(SF_Compound)
	**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_Converted)
	(*TSelect)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pNew)).FpPrior)).FpNext = pNew
	(*TSelect)(unsafe.Pointer(pNew)).FpLimit = uintptr(0)
	return WRC_Continue
}

// C documentation
//
//	/*
//	** This routine runs at the end of parsing a CREATE TABLE statement that
//	** has a WITHOUT ROWID clause.  The job of this routine is to convert both
//	** internal schema data structures and the generated VDBE code so that they
//	** are appropriate for a WITHOUT ROWID table instead of a rowid table.
//	** Changes include:
//	**
//	**     (1)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
//	**     (2)  Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
//	**          into BTREE_BLOBKEY.
//	**     (3)  Bypass the creation of the sqlite_schema table entry
//	**          for the PRIMARY KEY as the primary key index is now
//	**          identified by the sqlite_schema table entry of the table itself.
//	**     (4)  Set the Index.tnum of the PRIMARY KEY Index object in the
//	**          schema to the rootpage from the main table.
//	**     (5)  Add all table columns to the PRIMARY KEY Index object
//	**          so that the PRIMARY KEY is a covering index.  The surplus
//	**          columns are part of KeyInfo.nAllField and are not used for
//	**          sorting or lookup or uniqueness checks.
//	**     (6)  Replace the rowid tail on all automatically generated UNIQUE
//	**          indices with the PRIMARY KEY columns.
//	**
//	** For virtual tables, only (1) is performed.
//	*/
func _convertToWithoutRowidTable(tls *libc.TLS, pParse uintptr, pTab uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pIdx, pList, pPk, v, zColl, v12 uintptr
	var i, j, n, nExtra, nPk, v3 int32
	var v5 Tu16
	var _ /* ipkToken at bp+0 */ TToken
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, j, n, nExtra, nPk, pIdx, pList, pPk, v, zColl, v12, v3, v5
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	/* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
	 */
	if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 176 + 8))&0x6>>1)) != 0) {
		i = 0
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12 + 4))&0xf>>0)) == OE_None {
				libc.SetBitFieldPtr8Uint32((*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12+4, libc.Uint32FromInt32(OE_Abort), 0, 0xf)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(TF_HasNotNull)
	}
	/* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
	 ** into BTREE_BLOBKEY.
	 */
	if (*(*struct {
		FaddrCrTab      int32
		FregRowid       int32
		FregRoot        int32
		FconstraintName TToken
	})(unsafe.Pointer(pParse + 188))).FaddrCrTab != 0 {
		_sqlite3VdbeChangeP3(tls, v, (*(*struct {
			FaddrCrTab      int32
			FregRowid       int32
			FregRoot        int32
			FconstraintName TToken
		})(unsafe.Pointer(pParse + 188))).FaddrCrTab, int32(BTREE_BLOBKEY))
	}
	/* Locate the PRIMARY KEY index.  Or, if this table was originally
	 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
	 */
	if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) >= 0 {
		_sqlite3TokenInit(tls, bp, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*12))).FzCnName)
		pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp, 0))
		if pList == uintptr(0) {
			**(**Tu32)(__ccgo_up(pTab + 28)) &= uint32(^libc.Int32FromInt32(TF_WithoutRowid))
			return
		}
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
			_sqlite3RenameTokenRemap(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr, pTab+32)
		}
		(*(*TExprList_item)(unsafe.Pointer(pList + 8))).Ffg.FsortFlags = (*TParse)(unsafe.Pointer(pParse)).FiPkSortOrder
		(*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1))
		_sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), pList, int32((*TTable)(unsafe.Pointer(pTab)).FkeyConf), uintptr(0), uintptr(0), 0, 0, uint8(SQLITE_IDXTYPE_PRIMARYKEY))
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			**(**Tu32)(__ccgo_up(pTab + 28)) &= uint32(^libc.Int32FromInt32(TF_WithoutRowid))
			return
		}
		pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
	} else {
		pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
		/*
		 ** Remove all redundant columns from the PRIMARY KEY.  For example, change
		 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
		 ** code assumes the PRIMARY KEY contains no repeated columns.
		 */
		v3 = libc.Int32FromInt32(1)
		j = v3
		i = v3
		for {
			if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
				break
			}
			if _isDupColumn(tls, pPk, j, pPk, i) != 0 {
				(*TIndex)(unsafe.Pointer(pPk)).FnColumn = (*TIndex)(unsafe.Pointer(pPk)).FnColumn - 1
			} else {
				**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(j)*4)) = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(i)*4))
				**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaSortOrder + uintptr(j))) = **(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaSortOrder + uintptr(i)))
				v3 = j
				j = j + 1
				**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(v3)*2)) = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		(*TIndex)(unsafe.Pointer(pPk)).FnKeyCol = uint16(j)
	}
	libc.SetBitFieldPtr16Uint32(pPk+56, libc.Uint32FromInt32(1), 5, 0x20)
	if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 176 + 8))&0x6>>1)) != 0) {
		libc.SetBitFieldPtr16Uint32(pPk+56, libc.Uint32FromInt32(1), 3, 0x8)
	}
	v5 = (*TIndex)(unsafe.Pointer(pPk)).FnKeyCol
	(*TIndex)(unsafe.Pointer(pPk)).FnColumn = v5
	nPk = int32(v5)
	/* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema
	 ** table entry. This is only required if currently generating VDBE
	 ** code for a CREATE TABLE (not when parsing one as part of reading
	 ** a database schema).  */
	if v != 0 && (*TIndex)(unsafe.Pointer(pPk)).Ftnum > uint32(0) {
		_sqlite3VdbeChangeOpcode(tls, v, int32((*TIndex)(unsafe.Pointer(pPk)).Ftnum), uint8(OP_Goto))
	}
	/* The root page of the PRIMARY KEY is the table root page */
	(*TIndex)(unsafe.Pointer(pPk)).Ftnum = (*TTable)(unsafe.Pointer(pTab)).Ftnum
	/* Update the in-memory representation of all UNIQUE indices by converting
	 ** the final rowid column into one or more columns of the PRIMARY KEY.
	 */
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
			goto _6
		}
		v3 = libc.Int32FromInt32(0)
		n = v3
		i = v3
		for {
			if !(i < nPk) {
				break
			}
			if !(_isDupColumn(tls, pIdx, int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol), pPk, i) != 0) {
				n = n + 1
			}
			goto _7
		_7:
			;
			i = i + 1
		}
		if n == 0 {
			/* This index is a superset of the primary key */
			(*TIndex)(unsafe.Pointer(pIdx)).FnColumn = (*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol
			goto _6
		}
		if _resizeIndexObject(tls, pParse, pIdx, int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)+n) != 0 {
			return
		}
		i = 0
		j = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
		for {
			if !(i < nPk) {
				break
			}
			if !(_isDupColumn(tls, pIdx, int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol), pPk, i) != 0) {
				**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2)) = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))
				**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*4)) = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(i)*4))
				if **(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaSortOrder + uintptr(i))) != 0 {
					/* See ticket https://sqlite.org/src/info/bba7b69f9849b5bf */
					libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(1), 9, 0x200)
				}
				j = j + 1
			}
			goto _9
		_9:
			;
			i = i + 1
		}
		goto _6
	_6:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
	}
	/* Add all table columns to the PRIMARY KEY index
	 */
	nExtra = 0
	i = 0
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
			break
		}
		if !(_hasColumn(tls, (*TIndex)(unsafe.Pointer(pPk)).FaiColumn, nPk, i) != 0) && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 {
			nExtra = nExtra + 1
		}
		goto _10
	_10:
		;
		i = i + 1
	}
	if _resizeIndexObject(tls, pParse, pPk, nPk+nExtra) != 0 {
		return
	}
	i = 0
	j = nPk
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
			break
		}
		if !(_hasColumn(tls, (*TIndex)(unsafe.Pointer(pPk)).FaiColumn, j, i) != 0) && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 {
			zColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12)
			**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(j)*2)) = int16(i)
			if zColl != 0 {
				v12 = zColl
			} else {
				v12 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
			}
			**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(j)*4)) = v12
			j = j + 1
		}
		goto _11
	_11:
		;
		i = i + 1
	}
	_recomputeColumnsNotIndexed(tls, pPk)
}

// C documentation
//
//	/*
//	** This function is used to copy the contents of the b-tree node stored
//	** on page pFrom to page pTo. If page pFrom was not a leaf page, then
//	** the pointer-map entries for each child page are updated so that the
//	** parent page stored in the pointer map is page pTo. If pFrom contained
//	** any cells with overflow page pointers, then the corresponding pointer
//	** map entries are also updated so that the parent page is page pTo.
//	**
//	** If pFrom is currently carrying any overflow cells (entries in the
//	** MemPage.apOvfl[] array), they are not copied to pTo.
//	**
//	** Before returning, page pTo is reinitialized using btreeInitPage().
//	**
//	** The performance of this function is not critical. It is only used by
//	** the balance_shallower() and balance_deeper() procedures, neither of
//	** which are called often under normal circumstances.
//	*/
func _copyNodeContent(tls *libc.TLS, pFrom uintptr, pTo uintptr, pRC uintptr) {
	var aFrom, aTo, pBt uintptr
	var iData, iFromHdr, iToHdr, rc, v1 int32
	_, _, _, _, _, _, _, _ = aFrom, aTo, iData, iFromHdr, iToHdr, pBt, rc, v1
	if **(**int32)(__ccgo_up(pRC)) == SQLITE_OK {
		pBt = (*TMemPage)(unsafe.Pointer(pFrom)).FpBt
		aFrom = (*TMemPage)(unsafe.Pointer(pFrom)).FaData
		aTo = (*TMemPage)(unsafe.Pointer(pTo)).FaData
		iFromHdr = int32((*TMemPage)(unsafe.Pointer(pFrom)).FhdrOffset)
		if (*TMemPage)(unsafe.Pointer(pTo)).Fpgno == uint32(1) {
			v1 = int32(100)
		} else {
			v1 = 0
		}
		iToHdr = v1
		/* Copy the b-tree node content from page pFrom to page pTo. */
		iData = int32(**(**Tu8)(__ccgo_up(aFrom + uintptr(iFromHdr+int32(5)))))<<int32(8) | int32(**(**Tu8)(__ccgo_up(aFrom + uintptr(iFromHdr+int32(5)) + 1)))
		libc.Xmemcpy(tls, aTo+uintptr(iData), aFrom+uintptr(iData), (*TBtShared)(unsafe.Pointer(pBt)).FusableSize-uint32(iData))
		libc.Xmemcpy(tls, aTo+uintptr(iToHdr), aFrom+uintptr(iFromHdr), uint32(int32((*TMemPage)(unsafe.Pointer(pFrom)).FcellOffset)+int32(2)*int32((*TMemPage)(unsafe.Pointer(pFrom)).FnCell)))
		/* Reinitialize page pTo so that the contents of the MemPage structure
		 ** match the new data. The initialization of pTo can actually fail under
		 ** fairly obscure circumstances, even though it is a copy of initialized
		 ** page pFrom.
		 */
		(*TMemPage)(unsafe.Pointer(pTo)).FisInit = uint8(0)
		rc = _btreeInitPage(tls, pTo)
		if rc == SQLITE_OK {
			rc = _btreeComputeFreeSpace(tls, pTo)
		}
		if rc != SQLITE_OK {
			**(**int32)(__ccgo_up(pRC)) = rc
			return
		}
		/* If this is an auto-vacuum database, update the pointer-map entries
		 ** for any b-tree or overflow pages that pTo now contains the pointers to.
		 */
		if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
			**(**int32)(__ccgo_up(pRC)) = _setChildPtrmaps(tls, pTo)
		}
	}
}

// C documentation
//
//	/*
//	** Copy data from a buffer to a page, or from a page to a buffer.
//	**
//	** pPayload is a pointer to data stored on database page pDbPage.
//	** If argument eOp is false, then nByte bytes of data are copied
//	** from pPayload to the buffer pointed at by pBuf. If eOp is true,
//	** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
//	** of data are copied from the buffer pBuf to pPayload.
//	**
//	** SQLITE_OK is returned on success, otherwise an error code.
//	*/
func _copyPayload(tls *libc.TLS, pPayload uintptr, pBuf uintptr, nByte int32, eOp int32, pDbPage uintptr) (r int32) {
	var rc int32
	_ = rc
	if eOp != 0 {
		/* Copy data from buffer to page (a write operation) */
		rc = _sqlite3PagerWrite(tls, pDbPage)
		if rc != SQLITE_OK {
			return rc
		}
		libc.Xmemcpy(tls, pPayload, pBuf, uint32(nByte))
	} else {
		/* Copy data from page to buffer (a read operation) */
		libc.Xmemcpy(tls, pBuf, pPayload, uint32(nByte))
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Fill the InitData structure with an error message that indicates
//	** that the database is corrupt.
//	*/
func _corruptSchema(tls *libc.TLS, pData uintptr, azObj uintptr, zExtra uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var db, z, zObj, v1 uintptr
	_, _, _, _ = db, z, zObj, v1
	db = (*TInitData)(unsafe.Pointer(pData)).Fdb
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		(*TInitData)(unsafe.Pointer(pData)).Frc = int32(SQLITE_NOMEM)
	} else {
		if **(**uintptr)(__ccgo_up((*TInitData)(unsafe.Pointer(pData)).FpzErrMsg)) != uintptr(0) {
			/* A error message has already been generated.  Do not overwrite it */
		} else {
			if (*TInitData)(unsafe.Pointer(pData)).FmInitFlags&uint32(libc.Int32FromInt32(INITFLAG_AlterMask)) != 0 {
				**(**uintptr)(__ccgo_up((*TInitData)(unsafe.Pointer(pData)).FpzErrMsg)) = _sqlite3MPrintf(tls, db, __ccgo_ts+21257, libc.VaList(bp+8, **(**uintptr)(__ccgo_up(azObj)), **(**uintptr)(__ccgo_up(azObj + 1*4)), _azAlterType[(*TInitData)(unsafe.Pointer(pData)).FmInitFlags&uint32(INITFLAG_AlterMask)-uint32(1)], zExtra))
				(*TInitData)(unsafe.Pointer(pData)).Frc = int32(SQLITE_ERROR)
			} else {
				if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_WriteSchema) != 0 {
					(*TInitData)(unsafe.Pointer(pData)).Frc = _sqlite3CorruptError(tls, int32(147944))
				} else {
					if **(**uintptr)(__ccgo_up(azObj + 1*4)) != 0 {
						v1 = **(**uintptr)(__ccgo_up(azObj + 1*4))
					} else {
						v1 = __ccgo_ts + 6476
					}
					zObj = v1
					z = _sqlite3MPrintf(tls, db, __ccgo_ts+21285, libc.VaList(bp+8, zObj))
					if zExtra != 0 && **(**int8)(__ccgo_up(zExtra)) != 0 {
						z = _sqlite3MPrintf(tls, db, __ccgo_ts+21316, libc.VaList(bp+8, z, zExtra))
					}
					**(**uintptr)(__ccgo_up((*TInitData)(unsafe.Pointer(pData)).FpzErrMsg)) = z
					(*TInitData)(unsafe.Pointer(pData)).Frc = _sqlite3CorruptError(tls, int32(147951))
				}
			}
		}
	}
}

// C documentation
//
//	/*
//	** Attempt to transform a query of the form
//	**
//	**    SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
//	**
//	** Into this:
//	**
//	**    SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
//	**
//	** The transformation only works if all of the following are true:
//	**
//	**   *  The subquery is a UNION ALL of two or more terms
//	**   *  The subquery does not have a LIMIT clause
//	**   *  There is no WHERE or GROUP BY or HAVING clauses on the subqueries
//	**   *  The outer query is a simple count(*) with no WHERE clause or other
//	**      extraneous syntax.
//	**   *  None of the subqueries are DISTINCT (forumpost/a860f5fb2e 2025-03-10)
//	**
//	** Return TRUE if the optimization is undertaken.
//	*/
func _countOfViewOptimization(tls *libc.TLS, pParse uintptr, p uintptr) (r int32) {
	var db, pCount, pExpr, pFrom, pPrior, pSub, pTerm, v1 uintptr
	_, _, _, _, _, _, _, _ = db, pCount, pExpr, pFrom, pPrior, pSub, pTerm, v1
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) == uint32(0) {
		return 0
	} /* This is an aggregate */
	if (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr != int32(1) {
		return 0
	} /* Single result column */
	if (*TSelect)(unsafe.Pointer(p)).FpWhere != 0 {
		return 0
	}
	if (*TSelect)(unsafe.Pointer(p)).FpHaving != 0 {
		return 0
	}
	if (*TSelect)(unsafe.Pointer(p)).FpGroupBy != 0 {
		return 0
	}
	if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 {
		return 0
	}
	pExpr = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8))).FpExpr
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_AGG_FUNCTION) {
		return 0
	} /* Result is an aggregate */
	if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), __ccgo_ts+18338) != 0 {
		return 0
	} /* Is count() */
	if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != uintptr(0) {
		return 0
	} /* Must be count(*) */
	if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc != int32(1) {
		return 0
	} /* One table in FROM  */
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
		return 0
	} /* Not a window function */
	pFrom = (*TSelect)(unsafe.Pointer(p)).FpSrc + 8
	if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x4>>2) == 0 {
		return 0
	} /* FROM is a subquery */
	pSub = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pFrom + 44)))).FpSelect
	if (*TSelect)(unsafe.Pointer(pSub)).FpPrior == uintptr(0) {
		return 0
	} /* Must be a compound */
	if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(SF_CopyCte) != 0 {
		return 0
	} /* Not a CTE */
	for cond := true; cond; cond = pSub != 0 {
		if int32((*TSelect)(unsafe.Pointer(pSub)).Fop) != int32(TK_ALL) && (*TSelect)(unsafe.Pointer(pSub)).FpPrior != 0 {
			return 0
		} /* Must be UNION ALL */
		if (*TSelect)(unsafe.Pointer(pSub)).FpWhere != 0 {
			return 0
		} /* No WHERE clause */
		if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 {
			return 0
		} /* No LIMIT clause */
		if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(libc.Int32FromInt32(SF_Aggregate)|libc.Int32FromInt32(SF_Distinct)) != 0 {
			return 0 /* Not an aggregate nor DISTINCT */
		}
		/* Due to the previous */
		pSub = (*TSelect)(unsafe.Pointer(pSub)).FpPrior /* Repeat over compound */
	}
	/* If we reach this point then it is OK to perform the transformation */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pCount = pExpr
	pExpr = uintptr(0)
	pSub = _sqlite3SubqueryDetach(tls, db, pFrom)
	_sqlite3SrcListDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpSrc)
	(*TSelect)(unsafe.Pointer(p)).FpSrc = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(libc.UintptrFromInt32(0)+8)+libc.Uint32FromInt64(48)))
	for pSub != 0 {
		pPrior = (*TSelect)(unsafe.Pointer(pSub)).FpPrior
		(*TSelect)(unsafe.Pointer(pSub)).FpPrior = uintptr(0)
		(*TSelect)(unsafe.Pointer(pSub)).FpNext = uintptr(0)
		**(**Tu32)(__ccgo_up(pSub + 4)) |= uint32(SF_Aggregate)
		**(**Tu32)(__ccgo_up(pSub + 4)) &= ^libc.Uint32FromInt32(SF_Compound)
		(*TSelect)(unsafe.Pointer(pSub)).FnSelectRow = 0
		_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3ExprListDeleteGeneric), (*TSelect)(unsafe.Pointer(pSub)).FpEList)
		if pPrior != 0 {
			v1 = _sqlite3ExprDup(tls, db, pCount, 0)
		} else {
			v1 = pCount
		}
		pTerm = v1
		(*TSelect)(unsafe.Pointer(pSub)).FpEList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), pTerm)
		pTerm = _sqlite3PExpr(tls, pParse, int32(TK_SELECT), uintptr(0), uintptr(0))
		_sqlite3PExprAddSelect(tls, pParse, pTerm, pSub)
		if pExpr == uintptr(0) {
			pExpr = pTerm
		} else {
			pExpr = _sqlite3PExpr(tls, pParse, int32(TK_PLUS), pTerm, pExpr)
		}
		pSub = pPrior
	}
	(*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8))).FpExpr = pExpr
	**(**Tu32)(__ccgo_up(p + 4)) &= ^libc.Uint32FromInt32(SF_Aggregate)
	return int32(1)
}

// C documentation
//
//	/*
//	** Create a new aggregate context for p and return a pointer to
//	** its pMem->z element.
//	*/
func _createAggContext(tls *libc.TLS, p uintptr, nByte int32) (r uintptr) {
	var pMem uintptr
	_ = pMem
	pMem = (*Tsqlite3_context)(unsafe.Pointer(p)).FpMem
	if nByte <= 0 {
		_sqlite3VdbeMemSetNull(tls, pMem)
		(*TMem)(unsafe.Pointer(pMem)).Fz = uintptr(0)
	} else {
		_sqlite3VdbeMemClearAndResize(tls, pMem, nByte)
		(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Agg)
		*(*uintptr)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu)) = (*Tsqlite3_context)(unsafe.Pointer(p)).FpFunc
		if (*TMem)(unsafe.Pointer(pMem)).Fz != 0 {
			libc.Xmemset(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, 0, uint32(nByte))
		}
	}
	return (*TMem)(unsafe.Pointer(pMem)).Fz
}

// C documentation
//
//	/*
//	** Create a new collating function for database "db".  The name is zName
//	** and the encoding is enc.
//	*/
func _createCollation(tls *libc.TLS, db uintptr, zName uintptr, enc Tu8, pCtx uintptr, __ccgo_fp_xCompare uintptr, __ccgo_fp_xDel uintptr) (r int32) {
	var aColl, p, pColl uintptr
	var enc2, j int32
	_, _, _, _, _ = aColl, enc2, j, p, pColl
	/* If SQLITE_UTF16 is specified as the encoding type, transform this
	 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
	 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
	 */
	enc2 = int32(enc)
	if enc2 == int32(SQLITE_UTF16) || enc2 == int32(SQLITE_UTF16_ALIGNED) {
		enc2 = int32(SQLITE_UTF16LE)
	}
	if enc2 < int32(SQLITE_UTF8) || enc2 > int32(SQLITE_UTF16BE) {
		return _sqlite3MisuseError(tls, int32(190273))
	}
	/* Check if this call is removing or replacing an existing collation
	 ** sequence. If so, and there are active VMs, return busy. If there
	 ** are no active VMs, invalidate any pre-compiled statements.
	 */
	pColl = _sqlite3FindCollSeq(tls, db, uint8(enc2), zName, 0)
	if pColl != 0 && (*TCollSeq)(unsafe.Pointer(pColl)).FxCmp != 0 {
		if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive != 0 {
			_sqlite3ErrorWithMsg(tls, db, int32(SQLITE_BUSY), __ccgo_ts+27280, 0)
			return int32(SQLITE_BUSY)
		}
		_sqlite3ExpirePreparedStatements(tls, db, 0)
		/* If collation sequence pColl was created directly by a call to
		 ** sqlite3_create_collation, and not generated by synthCollSeq(),
		 ** then any copies made by synthCollSeq() need to be invalidated.
		 ** Also, collation destructor - CollSeq.xDel() - function may need
		 ** to be called.
		 */
		if int32((*TCollSeq)(unsafe.Pointer(pColl)).Fenc) & ^libc.Int32FromInt32(SQLITE_UTF16_ALIGNED) == enc2 {
			aColl = _sqlite3HashFind(tls, db+448, zName)
			j = 0
			for {
				if !(j < int32(3)) {
					break
				}
				p = aColl + uintptr(j)*20
				if int32((*TCollSeq)(unsafe.Pointer(p)).Fenc) == int32((*TCollSeq)(unsafe.Pointer(pColl)).Fenc) {
					if (*TCollSeq)(unsafe.Pointer(p)).FxDel != 0 {
						(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TCollSeq)(unsafe.Pointer(p)).FxDel})))(tls, (*TCollSeq)(unsafe.Pointer(p)).FpUser)
					}
					(*TCollSeq)(unsafe.Pointer(p)).FxCmp = uintptr(0)
				}
				goto _1
			_1:
				;
				j = j + 1
			}
		}
	}
	pColl = _sqlite3FindCollSeq(tls, db, uint8(enc2), zName, int32(1))
	if pColl == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	(*TCollSeq)(unsafe.Pointer(pColl)).FxCmp = __ccgo_fp_xCompare
	(*TCollSeq)(unsafe.Pointer(pColl)).FpUser = pCtx
	(*TCollSeq)(unsafe.Pointer(pColl)).FxDel = __ccgo_fp_xDel
	(*TCollSeq)(unsafe.Pointer(pColl)).Fenc = uint8(enc2 | int32(enc)&libc.Int32FromInt32(SQLITE_UTF16_ALIGNED))
	_sqlite3Error(tls, db, SQLITE_OK)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Generate a CREATE TABLE statement appropriate for the given
//	** table.  Memory to hold the text of the statement is obtained
//	** from sqliteMalloc() and must be freed by the calling function.
//	*/
func _createTableStmt(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, len1, v2 int32
	var n Ti64
	var pCol, zEnd, zSep, zSep2, zStmt, zType uintptr
	var _ /* k at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _ = i, len1, n, pCol, zEnd, zSep, zSep2, zStmt, zType, v2
	n = 0
	pCol = (*TTable)(unsafe.Pointer(p)).FaCol
	i = libc.Int32FromInt32(0)
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(p)).FnCol)) {
			break
		}
		n = n + (_identLength(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) + int64(5))
		goto _1
	_1:
		;
		i = i + 1
		pCol += 12
	}
	n = n + _identLength(tls, (*TTable)(unsafe.Pointer(p)).FzName)
	if n < int64(50) {
		zSep = __ccgo_ts + 1711
		zSep2 = __ccgo_ts + 15563
		zEnd = __ccgo_ts + 6474
	} else {
		zSep = __ccgo_ts + 15565
		zSep2 = __ccgo_ts + 15569
		zEnd = __ccgo_ts + 15574
	}
	n = n + int64(int32(35)+int32(6)*int32((*TTable)(unsafe.Pointer(p)).FnCol))
	zStmt = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(n))
	if zStmt == uintptr(0) {
		_sqlite3OomFault(tls, db)
		return uintptr(0)
	}
	libc.Xmemcpy(tls, zStmt, __ccgo_ts+15577, uint32(13))
	**(**int32)(__ccgo_up(bp)) = int32(13)
	_identPut(tls, zStmt, bp, (*TTable)(unsafe.Pointer(p)).FzName)
	v2 = **(**int32)(__ccgo_up(bp))
	**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + 1
	**(**int8)(__ccgo_up(zStmt + uintptr(v2))) = int8('(')
	pCol = (*TTable)(unsafe.Pointer(p)).FaCol
	i = libc.Int32FromInt32(0)
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(p)).FnCol)) {
			break
		}
		len1 = _sqlite3Strlen30(tls, zSep)
		libc.Xmemcpy(tls, zStmt+uintptr(**(**int32)(__ccgo_up(bp))), zSep, uint32(len1))
		**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + len1
		zSep = zSep2
		_identPut(tls, zStmt, bp, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)
		zType = _azType1[int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity)-int32(SQLITE_AFF_BLOB)]
		len1 = _sqlite3Strlen30(tls, zType)
		libc.Xmemcpy(tls, zStmt+uintptr(**(**int32)(__ccgo_up(bp))), zType, uint32(len1))
		**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + len1
		goto _3
	_3:
		;
		i = i + 1
		pCol += 12
	}
	len1 = _sqlite3Strlen30(tls, zEnd)
	libc.Xmemcpy(tls, zStmt+uintptr(**(**int32)(__ccgo_up(bp))), zEnd, uint32(len1+int32(1)))
	return zStmt
}

// C documentation
//
//	/*
//	** Return true (non-zero) if pCur is current pointing to the last
//	** page of a table.
//	*/
func _cursorOnLastPage(tls *libc.TLS, pCur uintptr) (r int32) {
	var i int32
	var pPage uintptr
	_, _ = i, pPage
	i = 0
	for {
		if !(i < int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)) {
			break
		}
		pPage = **(**uintptr)(__ccgo_up(pCur + 120 + uintptr(i)*4))
		if int32(**(**Tu16)(__ccgo_up(pCur + 72 + uintptr(i)*2))) < int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
			return 0
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return int32(1)
}

func _dbReallocFinish(tls *libc.TLS, db uintptr, p uintptr, n Tu64) (r uintptr) {
	var pNew uintptr
	_ = pNew
	pNew = uintptr(0)
	if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
		if _isLookaside(tls, db, p) != 0 {
			pNew = _sqlite3DbMallocRawNN(tls, db, n)
			if pNew != 0 {
				libc.Xmemcpy(tls, pNew, p, uint32(_lookasideMallocSize(tls, db, p)))
				_sqlite3DbFree(tls, db, p)
			}
		} else {
			pNew = _sqlite3Realloc(tls, p, n)
			if !(pNew != 0) {
				_sqlite3OomFault(tls, db)
			}
		}
	}
	return pNew
}

func _dbpageColumn(tls *libc.TLS, pCursor uintptr, ctx uintptr, i int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pCsr uintptr
	var rc int32
	var _ /* pDbPage at bp+0 */ uintptr
	_, _, _ = db, pCsr, rc
	pCsr = pCursor
	rc = SQLITE_OK
	switch i {
	case 0: /* pgno */
		Xsqlite3_result_int64(tls, ctx, int64((*TDbpageCursor)(unsafe.Pointer(pCsr)).Fpgno))
	case int32(1): /* data */
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		if (*TDbpageCursor)(unsafe.Pointer(pCsr)).Fpgno == uint32(_sqlite3PendingByte/(*TDbpageCursor)(unsafe.Pointer(pCsr)).FszPage+libc.Int32FromInt32(1)) {
			/* The pending byte page. Assume it is zeroed out. Attempting to
			 ** request this page from the page is an SQLITE_CORRUPT error. */
			Xsqlite3_result_zeroblob(tls, ctx, (*TDbpageCursor)(unsafe.Pointer(pCsr)).FszPage)
		} else {
			rc = _sqlite3PagerGet(tls, (*TDbpageCursor)(unsafe.Pointer(pCsr)).FpPager, (*TDbpageCursor)(unsafe.Pointer(pCsr)).Fpgno, bp, 0)
			if rc == SQLITE_OK {
				Xsqlite3_result_blob(tls, ctx, _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp))), (*TDbpageCursor)(unsafe.Pointer(pCsr)).FszPage, uintptr(-libc.Int32FromInt32(1)))
			}
			_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp)))
		}
	default: /* schema */
		db = Xsqlite3_context_db_handle(tls, ctx)
		Xsqlite3_result_text(tls, ctx, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TDbpageCursor)(unsafe.Pointer(pCsr)).FiDb)*16))).FzDbSName, -int32(1), libc.UintptrFromInt32(0))
		break
	}
	return rc
}

// C documentation
//
//	/*
//	** Connect to or create a dbpagevfs virtual table.
//	*/
func _dbpageConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) {
	var pTab uintptr
	var rc int32
	_, _ = pTab, rc
	pTab = uintptr(0)
	rc = SQLITE_OK
	_ = pAux
	_ = argc
	_ = argv
	_ = pzErr
	Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_DIRECTONLY), 0)
	Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_USES_ALL_SCHEMAS), 0)
	rc = Xsqlite3_declare_vtab(tls, db, __ccgo_ts+36239)
	if rc == SQLITE_OK {
		pTab = Xsqlite3_malloc64(tls, uint64(24))
		if pTab == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		}
	}
	if rc == SQLITE_OK {
		libc.Xmemset(tls, pTab, 0, uint32(24))
		(*TDbpageTable)(unsafe.Pointer(pTab)).Fdb = db
	}
	**(**uintptr)(__ccgo_up(ppVtab)) = pTab
	return rc
}

// C documentation
//
//	/*
//	** idxNum:
//	**
//	**     0     schema=main, full table scan
//	**     1     schema=main, pgno=?1
//	**     2     schema=?1, full table scan
//	**     3     schema=?1, pgno=?2
//	**
//	** idxStr is not used
//	*/
func _dbpageFilter(tls *libc.TLS, pCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) {
	var db, pBt, pCsr, pTab, zSchema uintptr
	var iPg Ti64
	var rc int32
	_, _, _, _, _, _, _ = db, iPg, pBt, pCsr, pTab, rc, zSchema
	pCsr = pCursor
	pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab
	db = (*TDbpageTable)(unsafe.Pointer(pTab)).Fdb
	_ = idxStr
	_ = argc
	/* Default setting is no rows of result */
	(*TDbpageCursor)(unsafe.Pointer(pCsr)).Fpgno = uint32(1)
	(*TDbpageCursor)(unsafe.Pointer(pCsr)).FmxPgno = uint32(0)
	if idxNum&int32(2) != 0 {
		zSchema = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		(*TDbpageCursor)(unsafe.Pointer(pCsr)).FiDb = _sqlite3FindDbName(tls, db, zSchema)
		if (*TDbpageCursor)(unsafe.Pointer(pCsr)).FiDb < 0 {
			return SQLITE_OK
		}
	} else {
		(*TDbpageCursor)(unsafe.Pointer(pCsr)).FiDb = 0
	}
	pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TDbpageCursor)(unsafe.Pointer(pCsr)).FiDb)*16))).FpBt
	if pBt == uintptr(0) {
		return SQLITE_OK
	}
	(*TDbpageCursor)(unsafe.Pointer(pCsr)).FpPager = _sqlite3BtreePager(tls, pBt)
	(*TDbpageCursor)(unsafe.Pointer(pCsr)).FszPage = _sqlite3BtreeGetPageSize(tls, pBt)
	(*TDbpageCursor)(unsafe.Pointer(pCsr)).FmxPgno = _sqlite3BtreeLastPage(tls, pBt)
	if idxNum&int32(1) != 0 {
		iPg = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + uintptr(idxNum>>int32(1))*4)))
		if iPg < int64(1) || iPg > int64((*TDbpageCursor)(unsafe.Pointer(pCsr)).FmxPgno) {
			(*TDbpageCursor)(unsafe.Pointer(pCsr)).Fpgno = uint32(1)
			(*TDbpageCursor)(unsafe.Pointer(pCsr)).FmxPgno = uint32(0)
		} else {
			(*TDbpageCursor)(unsafe.Pointer(pCsr)).Fpgno = uint32(iPg)
			(*TDbpageCursor)(unsafe.Pointer(pCsr)).FmxPgno = (*TDbpageCursor)(unsafe.Pointer(pCsr)).Fpgno
		}
	} else {
	}
	if (*TDbpageCursor)(unsafe.Pointer(pCsr)).FpPage1 != 0 {
		_sqlite3PagerUnrefPageOne(tls, (*TDbpageCursor)(unsafe.Pointer(pCsr)).FpPage1)
	}
	rc = _sqlite3PagerGet(tls, (*TDbpageCursor)(unsafe.Pointer(pCsr)).FpPager, uint32(1), pCsr+16, 0)
	return rc
}

func _dbpageUpdate(tls *libc.TLS, pVtab uintptr, argc int32, argv uintptr, pRowid uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aPage, pBt, pData, pPager, pTab, zErr, zSchema uintptr
	var iDb, isInsert, rc, szPage, v1 int32
	var pgno TPgno
	var _ /* pDbPage at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _ = aPage, iDb, isInsert, pBt, pData, pPager, pTab, pgno, rc, szPage, zErr, zSchema, v1
	pTab = pVtab
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	rc = SQLITE_OK
	zErr = uintptr(0)
	_ = pRowid
	if (*Tsqlite3)(unsafe.Pointer((*TDbpageTable)(unsafe.Pointer(pTab)).Fdb)).Fflags&uint64(SQLITE_Defensive) != 0 {
		zErr = __ccgo_ts + 36306
		goto update_fail
	}
	if argc == int32(1) {
		zErr = __ccgo_ts + 36316
		goto update_fail
	}
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv))) == int32(SQLITE_NULL) {
		pgno = uint32(Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 2*4))))
		isInsert = int32(1)
	} else {
		pgno = uint32(Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv))))
		if uint32(Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))) != pgno {
			zErr = __ccgo_ts + 36330
			goto update_fail
		}
		isInsert = 0
	}
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 4*4))) == int32(SQLITE_NULL) {
		iDb = 0
	} else {
		zSchema = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 4*4)))
		iDb = _sqlite3FindDbName(tls, (*TDbpageTable)(unsafe.Pointer(pTab)).Fdb, zSchema)
		if iDb < 0 {
			zErr = __ccgo_ts + 36344
			goto update_fail
		}
	}
	pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TDbpageTable)(unsafe.Pointer(pTab)).Fdb)).FaDb + uintptr(iDb)*16))).FpBt
	if pgno < uint32(1) || pBt == uintptr(0) {
		zErr = __ccgo_ts + 36359
		goto update_fail
	}
	szPage = _sqlite3BtreeGetPageSize(tls, pBt)
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 3*4))) != int32(SQLITE_BLOB) || Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 3*4))) != szPage {
		if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 3*4))) == int32(SQLITE_NULL) && isInsert != 0 && pgno > uint32(1) {
			/* "INSERT INTO dbpage($PGNO,NULL)" causes page number $PGNO and
			 ** all subsequent pages to be deleted. */
			(*TDbpageTable)(unsafe.Pointer(pTab)).FiDbTrunc = iDb
			(*TDbpageTable)(unsafe.Pointer(pTab)).FpgnoTrunc = pgno - uint32(1)
			pgno = uint32(1)
		} else {
			zErr = __ccgo_ts + 36375
			goto update_fail
		}
	}
	if _dbpageBeginTrans(tls, pTab) != SQLITE_OK {
		zErr = __ccgo_ts + 36390
		goto update_fail
	}
	pPager = _sqlite3BtreePager(tls, pBt)
	rc = _sqlite3PagerGet(tls, pPager, pgno, bp, 0)
	if rc == SQLITE_OK {
		pData = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv + 3*4)))
		v1 = _sqlite3PagerWrite(tls, **(**uintptr)(__ccgo_up(bp)))
		rc = v1
		if v1 == SQLITE_OK && pData != 0 {
			aPage = _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp)))
			libc.Xmemcpy(tls, aPage, pData, uint32(szPage))
			(*TDbpageTable)(unsafe.Pointer(pTab)).FpgnoTrunc = uint32(0)
		}
	}
	if rc != SQLITE_OK {
		(*TDbpageTable)(unsafe.Pointer(pTab)).FpgnoTrunc = uint32(0)
	}
	_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp)))
	return rc
	goto update_fail
update_fail:
	;
	(*TDbpageTable)(unsafe.Pointer(pTab)).FpgnoTrunc = uint32(0)
	Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg)
	(*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+16, zErr))
	return int32(SQLITE_ERROR)
}

// C documentation
//
//	/*
//	** The first argument points to a nul-terminated string containing a
//	** list of space separated integers. Read the first nOut of these into
//	** the array aOut[].
//	*/
func _decodeIntArray(tls *libc.TLS, zIntArray uintptr, nOut int32, aOut uintptr, aLog uintptr, pIndex uintptr) {
	var c, i, sz, v2 int32
	var v TtRowcnt
	var z uintptr
	_, _, _, _, _, _ = c, i, sz, v, z, v2
	z = zIntArray
	if z == uintptr(0) {
		z = __ccgo_ts + 1711
	}
	i = 0
	for {
		if !(**(**int8)(__ccgo_up(z)) != 0 && i < nOut) {
			break
		}
		v = uint64(0)
		for {
			v2 = int32(**(**int8)(__ccgo_up(z)))
			c = v2
			if !(v2 >= int32('0') && c <= int32('9')) {
				break
			}
			v = v*uint64(10) + uint64(c) - uint64('0')
			z = z + 1
		}
		if aOut != 0 {
			**(**TtRowcnt)(__ccgo_up(aOut + uintptr(i)*8)) = v
		}
		if aLog != 0 {
			**(**TLogEst)(__ccgo_up(aLog + uintptr(i)*2)) = _sqlite3LogEst(tls, v)
		}
		if int32(**(**int8)(__ccgo_up(z))) == int32(' ') {
			z = z + 1
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if pIndex != 0 {
		libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 2, 0x4)
		libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 6, 0x40)
		for **(**int8)(__ccgo_up(z)) != 0 {
			if Xsqlite3_strglob(tls, __ccgo_ts+14269, z) == 0 {
				libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 2, 0x4)
			} else {
				if Xsqlite3_strglob(tls, __ccgo_ts+14280, z) == 0 {
					sz = _sqlite3Atoi(tls, z+uintptr(3))
					if sz < int32(2) {
						sz = int32(2)
					}
					(*TIndex)(unsafe.Pointer(pIndex)).FszIdxRow = _sqlite3LogEst(tls, uint64(sz))
				} else {
					if Xsqlite3_strglob(tls, __ccgo_ts+14290, z) == 0 {
						libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 6, 0x40)
					}
				}
			}
			for int32(**(**int8)(__ccgo_up(z))) != 0 && int32(**(**int8)(__ccgo_up(z))) != int32(' ') {
				z = z + 1
			}
			for int32(**(**int8)(__ccgo_up(z))) == int32(' ') {
				z = z + 1
			}
		}
	}
}

// C documentation
//
//	/*
//	** Defragment the page given. This routine reorganizes cells within the
//	** page so that there are no free-blocks on the free-block list.
//	**
//	** Parameter nMaxFrag is the maximum amount of fragmented space that may be
//	** present in the page after this routine returns.
//	**
//	** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
//	** b-tree page so that there are no freeblocks or fragment bytes, all
//	** unused bytes are contained in the unallocated space region, and all
//	** cells are packed tightly at the end of the page.
//	*/
func _defragmentPage(tls *libc.TLS, pPage uintptr, nMaxFrag int32) (r int32) {
	var cbrk, cellOffset, hdr, i, iCellFirst, iCellLast, iCellStart, iFree, iFree2, nCell, pc, size, sz, sz2, top, usableSize int32
	var data, pAddr, pAddr1, pEnd, src, temp uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = cbrk, cellOffset, data, hdr, i, iCellFirst, iCellLast, iCellStart, iFree, iFree2, nCell, pAddr, pAddr1, pEnd, pc, size, src, sz, sz2, temp, top, usableSize /* First cell offset in input */
	data = (*TMemPage)(unsafe.Pointer(pPage)).FaData
	hdr = int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)
	cellOffset = int32((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset)
	nCell = int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)
	iCellFirst = cellOffset + int32(2)*nCell
	usableSize = int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize)
	/* This block handles pages with two or fewer free blocks and nMaxFrag
	 ** or fewer fragmented bytes. In this case it is faster to move the
	 ** two (or one) blocks of cells using memmove() and add the required
	 ** offsets to each pointer in the cell-pointer array than it is to
	 ** reconstruct the entire page.  */
	if int32(**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(7))))) <= nMaxFrag {
		iFree = int32(**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(1)))))<<int32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(1)) + 1)))
		if iFree > usableSize-int32(4) {
			return _sqlite3CorruptError(tls, int32(74875))
		}
		if iFree != 0 {
			iFree2 = int32(**(**uint8)(__ccgo_up(data + uintptr(iFree))))<<int32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(iFree) + 1)))
			if iFree2 > usableSize-int32(4) {
				return _sqlite3CorruptError(tls, int32(74878))
			}
			if 0 == iFree2 || int32(**(**uint8)(__ccgo_up(data + uintptr(iFree2)))) == 0 && int32(**(**uint8)(__ccgo_up(data + uintptr(iFree2+int32(1))))) == 0 {
				pEnd = data + uintptr(cellOffset+nCell*int32(2))
				sz2 = 0
				sz = int32(**(**uint8)(__ccgo_up(data + uintptr(iFree+int32(2)))))<<int32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(iFree+int32(2)) + 1)))
				top = int32(**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(5)))))<<int32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(5)) + 1)))
				if top >= iFree {
					return _sqlite3CorruptError(tls, int32(74886))
				}
				if iFree2 != 0 {
					if iFree+sz > iFree2 {
						return _sqlite3CorruptError(tls, int32(74889))
					}
					sz2 = int32(**(**uint8)(__ccgo_up(data + uintptr(iFree2+int32(2)))))<<int32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(iFree2+int32(2)) + 1)))
					if iFree2+sz2 > usableSize {
						return _sqlite3CorruptError(tls, int32(74891))
					}
					libc.Xmemmove(tls, data+uintptr(iFree+sz+sz2), data+uintptr(iFree+sz), uint32(iFree2-(iFree+sz)))
					sz = sz + sz2
				} else {
					if iFree+sz > usableSize {
						return _sqlite3CorruptError(tls, int32(74895))
					}
				}
				cbrk = top + sz
				libc.Xmemmove(tls, data+uintptr(cbrk), data+uintptr(top), uint32(iFree-top))
				pAddr = data + uintptr(cellOffset)
				for {
					if !(pAddr < pEnd) {
						break
					}
					pc = int32(**(**Tu8)(__ccgo_up(pAddr)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(pAddr + 1)))
					if pc < iFree {
						**(**Tu8)(__ccgo_up(pAddr)) = uint8((pc + sz) >> libc.Int32FromInt32(8))
						**(**Tu8)(__ccgo_up(pAddr + 1)) = uint8(pc + sz)
					} else {
						if pc < iFree2 {
							**(**Tu8)(__ccgo_up(pAddr)) = uint8((pc + sz2) >> libc.Int32FromInt32(8))
							**(**Tu8)(__ccgo_up(pAddr + 1)) = uint8(pc + sz2)
						}
					}
					goto _1
				_1:
					;
					pAddr = pAddr + uintptr(2)
				}
				goto defragment_out
			}
		}
	}
	cbrk = usableSize
	iCellLast = usableSize - int32(4)
	iCellStart = int32(**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(5)))))<<int32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(5)) + 1)))
	if nCell > 0 {
		temp = _sqlite3PagerTempSpace(tls, (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FpPager)
		libc.Xmemcpy(tls, temp, data, uint32(usableSize))
		src = temp
		i = 0
		for {
			if !(i < nCell) {
				break
			} /* The i-th cell pointer */
			pAddr1 = data + uintptr(cellOffset+i*int32(2))
			pc = int32(**(**Tu8)(__ccgo_up(pAddr1)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(pAddr1 + 1)))
			/* These conditions have already been verified in btreeInitPage()
			 ** if PRAGMA cell_size_check=ON.
			 */
			if pc > iCellLast {
				return _sqlite3CorruptError(tls, int32(74928))
			}
			size = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxCellSize})))(tls, pPage, src+uintptr(pc)))
			cbrk = cbrk - size
			if cbrk < iCellStart || pc+size > usableSize {
				return _sqlite3CorruptError(tls, int32(74934))
			}
			**(**Tu8)(__ccgo_up(pAddr1)) = uint8(cbrk >> libc.Int32FromInt32(8))
			**(**Tu8)(__ccgo_up(pAddr1 + 1)) = uint8(cbrk)
			libc.Xmemcpy(tls, data+uintptr(cbrk), src+uintptr(pc), uint32(size))
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(7)))) = uint8(0)
	goto defragment_out
defragment_out:
	;
	if int32(**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(7)))))+cbrk-iCellFirst != (*TMemPage)(unsafe.Pointer(pPage)).FnFree {
		return _sqlite3CorruptError(tls, int32(74948))
	}
	**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(5)))) = uint8(cbrk >> libc.Int32FromInt32(8))
	**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(5)) + 1)) = uint8(cbrk)
	**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(1)))) = uint8(0)
	**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(2)))) = uint8(0)
	libc.Xmemset(tls, data+uintptr(iCellFirst), 0, uint32(cbrk-iCellFirst))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Remove the memory data structures associated with the given
//	** Table.  No changes are made to disk by this routine.
//	**
//	** This routine just deletes the data structure.  It does not unlink
//	** the table data structure from the hash table.  But it does destroy
//	** memory structures of the indices and foreign keys associated with
//	** the table.
//	**
//	** The db parameter is optional.  It is needed if the Table object
//	** contains lookaside memory.  (Table objects in the schema do not use
//	** lookaside memory, but some ephemeral Table objects do.)  Or the
//	** db parameter can be used with db->pnBytesFreed to measure the memory
//	** used by the Table object.
//	*/
func _deleteTable(tls *libc.TLS, db uintptr, pTable uintptr) {
	var pIndex, pNext, zName uintptr
	_, _, _ = pIndex, pNext, zName
	/* Delete all indices associated with this table. */
	pIndex = (*TTable)(unsafe.Pointer(pTable)).FpIndex
	for {
		if !(pIndex != 0) {
			break
		}
		pNext = (*TIndex)(unsafe.Pointer(pIndex)).FpNext
		if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) && !(int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
			zName = (*TIndex)(unsafe.Pointer(pIndex)).FzName
			_sqlite3HashInsert(tls, (*TIndex)(unsafe.Pointer(pIndex)).FpSchema+24, zName, uintptr(0))
		}
		_sqlite3FreeIndex(tls, db, pIndex)
		goto _1
	_1:
		;
		pIndex = pNext
	}
	if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == TABTYP_NORM {
		_sqlite3FkDelete(tls, db, pTable)
	} else {
		if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == int32(TABTYP_VTAB) {
			_sqlite3VtabClear(tls, db, pTable)
		} else {
			_sqlite3SelectDelete(tls, db, (*(*struct {
				FpSelect uintptr
			})(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTable)).Fu))).FpSelect)
		}
	}
	/* Delete the Table structure itself.
	 */
	_sqlite3DeleteColumnNames(tls, db, pTable)
	_sqlite3DbFree(tls, db, (*TTable)(unsafe.Pointer(pTable)).FzName)
	_sqlite3DbFree(tls, db, (*TTable)(unsafe.Pointer(pTable)).FzColAff)
	_sqlite3ExprListDelete(tls, db, (*TTable)(unsafe.Pointer(pTable)).FpCheck)
	_sqlite3DbFree(tls, db, pTable)
	/* Verify that no lookaside memory was used by schema tables */
}

// C documentation
//
//	/*
//	** This function is called to configure the RtreeConstraint object passed
//	** as the second argument for a MATCH constraint. The value passed as the
//	** first argument to this function is the right-hand operand to the MATCH
//	** operator.
//	*/
func _deserializeGeometry(tls *libc.TLS, pValue uintptr, pCons uintptr) (r int32) {
	var pBlob, pInfo, pSrc uintptr
	_, _, _ = pBlob, pInfo, pSrc /* Callback information */
	pSrc = Xsqlite3_value_pointer(tls, pValue, __ccgo_ts+28732)
	if pSrc == uintptr(0) {
		return int32(SQLITE_ERROR)
	}
	pInfo = Xsqlite3_malloc64(tls, uint64(uint32(80)+(*TRtreeMatchArg)(unsafe.Pointer(pSrc)).FiSize))
	if !(pInfo != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pInfo, 0, uint32(80))
	pBlob = pInfo + 1*80
	libc.Xmemcpy(tls, pBlob, pSrc, (*TRtreeMatchArg)(unsafe.Pointer(pSrc)).FiSize)
	(*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FpContext = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FpContext
	(*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FnParam = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FnParam
	(*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FaParam = pBlob + 32
	(*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FapSqlParam = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam
	if (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FxGeom != 0 {
		*(*uintptr)(unsafe.Pointer(&(*TRtreeConstraint)(unsafe.Pointer(pCons)).Fu)) = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FxGeom
	} else {
		(*TRtreeConstraint)(unsafe.Pointer(pCons)).Fop = int32(RTREE_QUERY)
		*(*uintptr)(unsafe.Pointer(&(*TRtreeConstraint)(unsafe.Pointer(pCons)).Fu)) = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FxQueryFunc
	}
	(*TRtreeConstraint)(unsafe.Pointer(pCons)).FpInfo = pInfo
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Write code to erase the table with root-page iTable from database iDb.
//	** Also write code to modify the sqlite_schema table and internal schema
//	** if a root-page of another table is moved by the btree-layer whilst
//	** erasing iTable (this can happen with an auto-vacuum database).
//	*/
func _destroyRootPage(tls *libc.TLS, pParse uintptr, iTable int32, iDb int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var r1 int32
	var v uintptr
	_, _ = r1, v
	v = _sqlite3GetVdbe(tls, pParse)
	r1 = _sqlite3GetTempReg(tls, pParse)
	if iTable < int32(2) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16086, 0)
	}
	_sqlite3VdbeAddOp3(tls, v, int32(OP_Destroy), iTable, r1, iDb)
	_sqlite3MayAbort(tls, pParse)
	/* OP_Destroy stores an in integer r1. If this integer
	 ** is non-zero, then it is the root page number of a table moved to
	 ** location iTable. The following code modifies the sqlite_schema table to
	 ** reflect this.
	 **
	 ** The "#NNN" in the SQL is a special constant that means whatever value
	 ** is in register NNN.  See grammar rules associated with the TK_REGISTER
	 ** token for additional information.
	 */
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+16101, libc.VaList(bp+8, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FzDbSName, iTable, r1, r1))
	_sqlite3ReleaseTempReg(tls, pParse, r1)
}

// C documentation
//
//	/*
//	** An SQL user-function registered to do the work of an DETACH statement. The
//	** three arguments to the function come directly from a detach statement:
//	**
//	**     DETACH DATABASE x
//	**
//	**     SELECT sqlite_detach(x)
//	*/
func _detachFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var db, pDb, pEntry, pTrig, zName uintptr
	var i int32
	var _ /* zErr at bp+0 */ [128]int8
	_, _, _, _, _, _ = db, i, pDb, pEntry, pTrig, zName
	zName = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	db = Xsqlite3_context_db_handle(tls, context)
	pDb = uintptr(0)
	_ = NotUsed
	if zName == uintptr(0) {
		zName = __ccgo_ts + 1711
	}
	i = 0
	for {
		if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
			break
		}
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16
		if (*TDb)(unsafe.Pointer(pDb)).FpBt == uintptr(0) {
			goto _1
		}
		if _sqlite3DbIsNamed(tls, db, i, zName) != 0 {
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if i >= (*Tsqlite3)(unsafe.Pointer(db)).FnDb {
		Xsqlite3_snprintf(tls, int32(128), bp, __ccgo_ts+14659, libc.VaList(bp+136, zName))
		goto detach_error
	}
	if i < int32(2) {
		Xsqlite3_snprintf(tls, int32(128), bp, __ccgo_ts+14680, libc.VaList(bp+136, zName))
		goto detach_error
	}
	if _sqlite3BtreeTxnState(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) != SQLITE_TXN_NONE || _sqlite3BtreeIsInBackup(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) != 0 {
		Xsqlite3_snprintf(tls, int32(128), bp, __ccgo_ts+14706, libc.VaList(bp+136, zName))
		goto detach_error
	}
	/* If any TEMP triggers reference the schema being detached, move those
	 ** triggers to reference the TEMP schema itself. */
	pEntry = (*THash)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema + 40)).Ffirst
	for pEntry != 0 {
		pTrig = (*THashElem)(unsafe.Pointer(pEntry)).Fdata
		if (*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema == (*TDb)(unsafe.Pointer(pDb)).FpSchema {
			(*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema = (*TTrigger)(unsafe.Pointer(pTrig)).FpSchema
		}
		pEntry = (*THashElem)(unsafe.Pointer(pEntry)).Fnext
	}
	_sqlite3BtreeClose(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt)
	(*TDb)(unsafe.Pointer(pDb)).FpBt = uintptr(0)
	(*TDb)(unsafe.Pointer(pDb)).FpSchema = uintptr(0)
	_sqlite3CollapseDatabaseArray(tls, db)
	return
	goto detach_error
detach_error:
	;
	Xsqlite3_result_error(tls, context, bp, -int32(1))
}

// C documentation
//
//	/*
//	** Disable a term in the WHERE clause.  Except, do not disable the term
//	** if it controls a LEFT OUTER JOIN and it did not originate in the ON
//	** or USING clause of that join.
//	**
//	** Consider the term t2.z='ok' in the following queries:
//	**
//	**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
//	**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
//	**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
//	**
//	** The t2.z='ok' is disabled in the in (2) because it originates
//	** in the ON clause.  The term is disabled in (3) because it is not part
//	** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
//	**
//	** Disabling a term causes that term to not be tested in the inner loop
//	** of the join.  Disabling is an optimization.  When terms are satisfied
//	** by indices, we disable them to prevent redundant tests in the inner
//	** loop.  We would get the correct results if nothing were ever disabled,
//	** but joins might run a little slower.  The trick is to disable as much
//	** as we can without disabling too much.  If we disabled in (1), we'd get
//	** the wrong answer.  See ticket #813.
//	**
//	** If all the children of a term are disabled, then that term is also
//	** automatically disabled.  In this way, terms get disabled if derived
//	** virtual terms are tested first.  For example:
//	**
//	**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
//	**      \___________/     \______/     \_____/
//	**         parent          child1       child2
//	**
//	** Only the parent term was in the original WHERE clause.  The child1
//	** and child2 terms were added by the LIKE optimization.  If both of
//	** the virtual child terms are valid, then testing of the parent can be
//	** skipped.
//	**
//	** Usually the parent term is marked as TERM_CODED.  But if the parent
//	** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
//	** The TERM_LIKECOND marking indicates that the term should be coded inside
//	** a conditional such that is only evaluated on the second pass of a
//	** LIKE-optimization loop, when scanning BLOBs instead of strings.
//	*/
func _disableTerm(tls *libc.TLS, pLevel uintptr, pTerm uintptr) {
	var nLoop int32
	var v1 uintptr
	_, _ = nLoop, v1
	nLoop = 0
	for int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_CODED) == 0 && ((*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin == 0 || (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0)) && (*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady&(*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll == uint64(0) {
		if nLoop != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_LIKE) != 0 {
			v1 = pTerm + 10
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_LIKECOND))
		} else {
			v1 = pTerm + 10
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_CODED))
		}
		if (*TWhereTerm)(unsafe.Pointer(pTerm)).FiParent < 0 {
			break
		}
		pTerm = (*TWhereClause)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpWC)).Fa + uintptr((*TWhereTerm)(unsafe.Pointer(pTerm)).FiParent)*48
		(*TWhereTerm)(unsafe.Pointer(pTerm)).FnChild = (*TWhereTerm)(unsafe.Pointer(pTerm)).FnChild - 1
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FnChild) != 0 {
			break
		}
		nLoop = nLoop + 1
	}
}

// C documentation
//
//	/*
//	** Check to see if a subquery contains result-set columns that are
//	** never used.  If it does, change the value of those result-set columns
//	** to NULL so that they do not cause unnecessary work to compute.
//	**
//	** Return the number of column that were changed to NULL.
//	*/
func _disableUnusedSubqueryResultColumns(tls *libc.TLS, pItem uintptr) (r int32) {
	var colUsed, m TBitmask
	var iCol Tu16
	var j, nChng, nCol, v3 int32
	var pList, pSub, pTab, pX, pY uintptr
	var v5 uint64
	_, _, _, _, _, _, _, _, _, _, _, _, _ = colUsed, iCol, j, m, nChng, nCol, pList, pSub, pTab, pX, pY, v3, v5 /* Column number */
	nChng = 0                                                                                                   /* Columns that may not be NULLed out */
	if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x10>>4) != 0 || int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x200>>9) != 0 {
		return 0
	}
	pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
	pSub = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 44)))).FpSelect
	pX = pSub
	for {
		if !(pX != 0) {
			break
		}
		if (*TSelect)(unsafe.Pointer(pX)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) != uint32(0) {
			return 0
		}
		if (*TSelect)(unsafe.Pointer(pX)).FpPrior != 0 && int32((*TSelect)(unsafe.Pointer(pX)).Fop) != int32(TK_ALL) {
			/* This optimization does not work for compound subqueries that
			 ** use UNION, INTERSECT, or EXCEPT.  Only UNION ALL is allowed. */
			return 0
		}
		if (*TSelect)(unsafe.Pointer(pX)).FpWin != 0 {
			/* This optimization does not work for subqueries that use window
			 ** functions. */
			return 0
		}
		goto _1
	_1:
		;
		pX = (*TSelect)(unsafe.Pointer(pX)).FpPrior
	}
	colUsed = (*TSrcItem)(unsafe.Pointer(pItem)).FcolUsed
	if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 {
		pList = (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy
		j = 0
		for {
			if !(j < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
				break
			}
			iCol = *(*Tu16)(unsafe.Pointer(pList + 8 + uintptr(j)*20 + 16))
			if int32(iCol) > 0 {
				iCol = iCol - 1
				if int32(iCol) >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
					v3 = int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1)
				} else {
					v3 = int32(iCol)
				}
				colUsed = colUsed | libc.Uint64FromInt32(1)<<v3
			}
			goto _2
		_2:
			;
			j = j + 1
		}
	}
	nCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
	j = 0
	for {
		if !(j < nCol) {
			break
		}
		if j < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) {
			v5 = libc.Uint64FromInt32(1) << j
		} else {
			v5 = libc.Uint64FromInt32(1) << (int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1))
		}
		m = v5
		if m&colUsed != uint64(0) {
			goto _4
		}
		pX = pSub
		for {
			if !(pX != 0) {
				break
			}
			pY = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pX)).FpEList + 8 + uintptr(j)*20))).FpExpr
			if int32((*TExpr)(unsafe.Pointer(pY)).Fop) == int32(TK_NULL) {
				goto _6
			}
			(*TExpr)(unsafe.Pointer(pY)).Fop = uint8(TK_NULL)
			**(**Tu32)(__ccgo_up(pY + 4)) &= ^uint32(libc.Int32FromInt32(EP_Skip) | libc.Int32FromInt32(EP_Unlikely))
			**(**Tu32)(__ccgo_up(pX + 4)) |= uint32(SF_PushDown)
			nChng = nChng + 1
			goto _6
		_6:
			;
			pX = (*TSelect)(unsafe.Pointer(pX)).FpPrior
		}
		goto _4
	_4:
		;
		j = j + 1
	}
	return nChng
}

// C documentation
//
//	/*
//	** Disconnect all sqlite3_vtab objects that belong to database connection
//	** db. This is called when db is being closed.
//	*/
func _disconnectAllVtab(tls *libc.TLS, db uintptr) {
	var i int32
	var p, pMod, pSchema, pTab uintptr
	_, _, _, _, _ = i, p, pMod, pSchema, pTab
	_sqlite3BtreeEnterAll(tls, db)
	i = 0
	for {
		if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
			break
		}
		pSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema
		if pSchema != 0 {
			p = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst
			for {
				if !(p != 0) {
					break
				}
				pTab = (*THashElem)(unsafe.Pointer(p)).Fdata
				if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
					_sqlite3VtabDisconnect(tls, db, pTab)
				}
				goto _2
			_2:
				;
				p = (*THashElem)(unsafe.Pointer(p)).Fnext
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	p = (*THash)(unsafe.Pointer(db + 404)).Ffirst
	for {
		if !(p != 0) {
			break
		}
		pMod = (*THashElem)(unsafe.Pointer(p)).Fdata
		if (*TModule)(unsafe.Pointer(pMod)).FpEpoTab != 0 {
			_sqlite3VtabDisconnect(tls, db, (*TModule)(unsafe.Pointer(pMod)).FpEpoTab)
		}
		goto _3
	_3:
		;
		p = (*THashElem)(unsafe.Pointer(p)).Fnext
	}
	_sqlite3VtabUnlockList(tls, db)
	_sqlite3BtreeLeaveAll(tls, db)
}

// C documentation
//
//	/*
//	** Remove the i-th cell from pPage.  This routine effects pPage only.
//	** The cell content is not freed or deallocated.  It is assumed that
//	** the cell content has been copied someplace else.  This routine just
//	** removes the reference to the cell from pPage.
//	**
//	** "sz" must be the number of bytes in the cell.
//	*/
func _dropCell(tls *libc.TLS, pPage uintptr, idx int32, sz int32, pRC uintptr) {
	var data, ptr uintptr
	var hdr, rc int32
	var pc Tu32
	_, _, _, _, _ = data, hdr, pc, ptr, rc /* Beginning of the header.  0 most pages.  100 page 1 */
	if **(**int32)(__ccgo_up(pRC)) != 0 {
		return
	}
	data = (*TMemPage)(unsafe.Pointer(pPage)).FaData
	ptr = (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx)
	pc = uint32(int32(**(**Tu8)(__ccgo_up(ptr)))<<libc.Int32FromInt32(8) | int32(**(**Tu8)(__ccgo_up(ptr + 1))))
	hdr = int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)
	if pc+uint32(sz) > (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize {
		**(**int32)(__ccgo_up(pRC)) = _sqlite3CorruptError(tls, int32(80515))
		return
	}
	rc = _freeSpace(tls, pPage, int32(pc), sz)
	if rc != 0 {
		**(**int32)(__ccgo_up(pRC)) = rc
		return
	}
	(*TMemPage)(unsafe.Pointer(pPage)).FnCell = (*TMemPage)(unsafe.Pointer(pPage)).FnCell - 1
	if int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) == 0 {
		libc.Xmemset(tls, data+uintptr(hdr+int32(1)), 0, uint32(4))
		**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(7)))) = uint8(0)
		**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(5)))) = uint8((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize >> libc.Int32FromInt32(8))
		**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(5)) + 1)) = uint8((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize)
		(*TMemPage)(unsafe.Pointer(pPage)).FnFree = int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize - uint32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset) - uint32((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize) - uint32(8))
	} else {
		libc.Xmemmove(tls, ptr, ptr+uintptr(2), uint32(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-idx)))
		**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(3)))) = uint8(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) >> libc.Int32FromInt32(8))
		**(**Tu8)(__ccgo_up(data + uintptr(hdr+int32(3)) + 1)) = uint8((*TMemPage)(unsafe.Pointer(pPage)).FnCell)
		**(**int32)(__ccgo_up(pPage + 20)) += int32(2)
	}
}

// C documentation
//
//	/*
//	** The implementation of internal UDF sqlite_drop_column().
//	**
//	** Arguments:
//	**
//	**  argv[0]: An integer - the index of the schema containing the table
//	**  argv[1]: CREATE TABLE statement to modify.
//	**  argv[2]: An integer - the index of the column to remove.
//	**
//	** The value returned is a string containing the CREATE TABLE statement
//	** with column argv[2] removed.
//	*/
func _dropColumnFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) {
	bp := tls.Alloc(320)
	defer tls.Free(320)
	var db, pCol, pEnd, pTab, zDb, zEnd, zNew, zSql uintptr
	var iCol, iSchema, rc int32
	var xAuth Tsqlite3_xauth
	var _ /* eTok at bp+284 */ int32
	var _ /* sParse at bp+0 */ TParse
	_, _, _, _, _, _, _, _, _, _, _, _ = db, iCol, iSchema, pCol, pEnd, pTab, rc, xAuth, zDb, zEnd, zNew, zSql
	db = Xsqlite3_context_db_handle(tls, context)
	iSchema = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv)))
	zSql = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	iCol = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 2*4)))
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iSchema)*16))).FzDbSName
	zNew = uintptr(0)
	xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0)
	_ = NotUsed
	rc = _renameParseSql(tls, bp, zDb, db, zSql, libc.BoolInt32(iSchema == int32(1)))
	if rc != SQLITE_OK {
		goto drop_column_done
	}
	pTab = (**(**TParse)(__ccgo_up(bp))).FpNewTable
	if pTab == uintptr(0) || int32((*TTable)(unsafe.Pointer(pTab)).FnCol) == int32(1) || iCol >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) {
		/* This can happen if the sqlite_schema table is corrupt */
		rc = _sqlite3CorruptError(tls, int32(122753))
		goto drop_column_done
	}
	if iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-int32(1) {
		pCol = _renameTokenFind(tls, bp, uintptr(0), (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName)
		pEnd = _renameTokenFind(tls, bp, uintptr(0), (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol+int32(1))*12))).FzCnName)
		zEnd = (*TRenameToken)(unsafe.Pointer(pEnd)).Ft.Fz
	} else {
		/* Point pCol->t.z at the "," immediately preceding the definition of
		 ** the column being dropped. To do this, start at the name of the
		 ** previous column, and tokenize until the next ",".  */
		pCol = _renameTokenFind(tls, bp, uintptr(0), (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol-int32(1))*12))).FzCnName)
		for cond := true; cond; cond = **(**int32)(__ccgo_up(bp + 284)) != int32(TK_COMMA) {
			(*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz += uintptr(_getConstraintToken(tls, (*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz, bp+284))
		}
		(*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz = (*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz - 1
		zEnd = zSql + uintptr((*(*struct {
			FaddColOffset int32
			FpFKey        uintptr
			FpDfltList    uintptr
		})(unsafe.Pointer(pTab + 44))).FaddColOffset)
	}
	zNew = _sqlite3MPrintf(tls, db, __ccgo_ts+12802, libc.VaList(bp+296, int32((*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz)-int32(zSql), zSql, zEnd))
	Xsqlite3_result_text(tls, context, zNew, -int32(1), uintptr(-libc.Int32FromInt32(1)))
	Xsqlite3_free(tls, zNew)
	goto drop_column_done
drop_column_done:
	;
	_renameParseCleanup(tls, bp)
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth
	if rc != SQLITE_OK {
		Xsqlite3_result_error_code(tls, context, rc)
	}
}

// C documentation
//
//	/*
//	** Internal SQL function sqlite3_drop_constraint():  Given an input
//	** CREATE TABLE statement, return a revised CREATE TABLE statement
//	** with a constraint removed.  Two forms, depending on the datatype
//	** of argv[2]:
//	**
//	**   sqlite_drop_constraint(SQL, INT)  -- Omit NOT NULL from the INT-th column
//	**   sqlite_drop_constraint(SQL, TEXT) -- OMIT constraint with name TEXT
//	**
//	** In the first case, the left-most column is 0.
//	*/
func _dropConstraintFunc(tls *libc.TLS, ctx uintptr, NotUsed int32, argv uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var db, zCons, zNew, zSpace, zSql uintptr
	var iEnd, iNotNull, iStart, ii, nTok int32
	var _ /* cmp at bp+8 */ int32
	var _ /* iOff at bp+0 */ int32
	var _ /* t at bp+4 */ int32
	_, _, _, _, _, _, _, _, _, _ = db, iEnd, iNotNull, iStart, ii, nTok, zCons, zNew, zSpace, zSql
	zSql = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	zCons = uintptr(0)
	iNotNull = -int32(1)
	**(**int32)(__ccgo_up(bp)) = 0
	iStart = 0
	iEnd = 0
	zNew = uintptr(0)
	**(**int32)(__ccgo_up(bp + 4)) = 0
	_ = NotUsed
	if zSql == uintptr(0) {
		return
	}
	/* Jump past the "CREATE TABLE" bit. */
	if _skipCreateTable(tls, ctx, zSql, bp) != 0 {
		return
	}
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 1*4))) == int32(SQLITE_INTEGER) {
		iNotNull = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	} else {
		zCons = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	}
	/* Search for the named constraint within column definitions. */
	ii = 0
	for {
		if !(iEnd == 0) {
			break
		}
		/* Now parse the column or table constraint definition. Search
		 ** for the token CONSTRAINT if this is a DROP CONSTRAINT command, or
		 ** NOT in the right column if this is a DROP NOT NULL. */
		for int32(1) != 0 {
			iStart = **(**int32)(__ccgo_up(bp))
			**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + _getConstraintToken(tls, zSql+uintptr(**(**int32)(__ccgo_up(bp))), bp+4)
			if **(**int32)(__ccgo_up(bp + 4)) == int32(TK_CONSTRAINT) && (zCons != 0 || iNotNull == ii) {
				/* Check if this is the constraint we are searching for. */
				nTok = 0
				**(**int32)(__ccgo_up(bp + 8)) = int32(1)
				/* Skip past any whitespace. */
				**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + _getWhitespace(tls, zSql+uintptr(**(**int32)(__ccgo_up(bp))))
				/* Compare the next token - which may be quoted - with the name of
				 ** the constraint being dropped.  */
				nTok = _getConstraintToken(tls, zSql+uintptr(**(**int32)(__ccgo_up(bp))), bp+4)
				if zCons != 0 {
					if _quotedCompare(tls, ctx, **(**int32)(__ccgo_up(bp + 4)), zSql+uintptr(**(**int32)(__ccgo_up(bp))), nTok, zCons, bp+8) != 0 {
						return
					}
				}
				**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + nTok
				/* The next token is usually the first token of the constraint
				 ** definition. This is enough to tell the type of the constraint -
				 ** TK_NOT means it is a NOT NULL, TK_CHECK a CHECK constraint etc.
				 **
				 ** There is also the chance that the next token is TK_CONSTRAINT
				 ** (or TK_DEFAULT or TK_COLLATE), for example if a table has been
				 ** created as follows:
				 **
				 **    CREATE TABLE t1(cols, CONSTRAINT one CONSTRAINT two NOT NULL);
				 **
				 ** In this case, allow the "CONSTRAINT one" bit to be dropped by
				 ** this command if that is what is requested, or to advance to
				 ** the next iteration of the loop with &zSql[iOff] still pointing
				 ** to the CONSTRAINT keyword.  */
				nTok = _getConstraintToken(tls, zSql+uintptr(**(**int32)(__ccgo_up(bp))), bp+4)
				if **(**int32)(__ccgo_up(bp + 4)) == int32(TK_CONSTRAINT) || **(**int32)(__ccgo_up(bp + 4)) == int32(TK_DEFAULT) || **(**int32)(__ccgo_up(bp + 4)) == int32(TK_COLLATE) || **(**int32)(__ccgo_up(bp + 4)) == int32(TK_COMMA) || **(**int32)(__ccgo_up(bp + 4)) == int32(TK_RP) || **(**int32)(__ccgo_up(bp + 4)) == int32(TK_GENERATED) || **(**int32)(__ccgo_up(bp + 4)) == int32(TK_AS) {
					**(**int32)(__ccgo_up(bp + 4)) = int32(TK_CHECK)
				} else {
					**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + nTok
					**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + _getConstraint(tls, zSql+uintptr(**(**int32)(__ccgo_up(bp))))
				}
				if **(**int32)(__ccgo_up(bp + 8)) == 0 || iNotNull >= 0 && **(**int32)(__ccgo_up(bp + 4)) == int32(TK_NOT) {
					if **(**int32)(__ccgo_up(bp + 4)) != int32(TK_NOT) && **(**int32)(__ccgo_up(bp + 4)) != int32(TK_CHECK) {
						_errorMPrintf(tls, ctx, __ccgo_ts+13036, libc.VaList(bp+24, zCons))
						return
					}
					iEnd = **(**int32)(__ccgo_up(bp))
					break
				}
			} else {
				if **(**int32)(__ccgo_up(bp + 4)) == int32(TK_NOT) && iNotNull == ii {
					iEnd = **(**int32)(__ccgo_up(bp)) + _getConstraint(tls, zSql+uintptr(**(**int32)(__ccgo_up(bp))))
					break
				} else {
					if **(**int32)(__ccgo_up(bp + 4)) == int32(TK_RP) || **(**int32)(__ccgo_up(bp + 4)) == int32(TK_ILLEGAL) {
						iEnd = -int32(1)
						break
					} else {
						if **(**int32)(__ccgo_up(bp + 4)) == int32(TK_COMMA) {
							break
						}
					}
				}
			}
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	/* If the constraint has not been found it is an error. */
	if iEnd <= 0 {
		if zCons != 0 {
			_errorMPrintf(tls, ctx, __ccgo_ts+13070, libc.VaList(bp+24, zCons))
		} else {
			/* SQLite follows postgres in that a DROP NOT NULL on a column that is
			 ** not NOT NULL is not an error. So just return the original SQL here. */
			Xsqlite3_result_text(tls, ctx, zSql, -int32(1), uintptr(-libc.Int32FromInt32(1)))
		}
	} else {
		/* Figure out if an extra space should be inserted after the constraint
		 ** is removed. And if an additional comma preceding the constraint
		 ** should be removed. */
		zSpace = __ccgo_ts + 12758
		iEnd = iEnd + _getWhitespace(tls, zSql+uintptr(iEnd))
		_sqlite3GetToken(tls, zSql+uintptr(iEnd), bp+4)
		if **(**int32)(__ccgo_up(bp + 4)) == int32(TK_RP) || **(**int32)(__ccgo_up(bp + 4)) == int32(TK_COMMA) {
			zSpace = __ccgo_ts + 1711
			if int32(**(**Tu8)(__ccgo_up(zSql + uintptr(iStart-int32(1))))) == int32(',') {
				iStart = iStart - 1
			}
		}
		db = Xsqlite3_context_db_handle(tls, ctx)
		zNew = _sqlite3MPrintf(tls, db, __ccgo_ts+13093, libc.VaList(bp+24, iStart, zSql, zSpace, zSql+uintptr(iEnd)))
		Xsqlite3_result_text(tls, ctx, zNew, -int32(1), __ccgo_fp(_sqlite3RowSetClear))
	}
}

// C documentation
//
//	/*
//	** This function returns the space in bytes required to store the copy
//	** of the Expr structure and a copy of the Expr.u.zToken string (if that
//	** string is defined.)
//	*/
func _dupedExprNodeSize(tls *libc.TLS, p uintptr, flags int32) (r int32) {
	var nByte int32
	_ = nByte
	nByte = _dupedExprStructSize(tls, p, flags) & int32(0xfff)
	if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != libc.Uint32FromInt32(0)) && *(*uintptr)(unsafe.Pointer(p + 8)) != 0 {
		nByte = int32(uint32(nByte) + (libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(p + 8)))&libc.Uint32FromInt32(0x3fffffff) + libc.Uint32FromInt32(1)))
	}
	return (nByte + int32(7)) & ^libc.Int32FromInt32(7)
}

// C documentation
//
//	/*
//	** The dupedExpr*Size() routines each return the number of bytes required
//	** to store a copy of an expression or expression tree.  They differ in
//	** how much of the tree is measured.
//	**
//	**     dupedExprStructSize()     Size of only the Expr structure
//	**     dupedExprNodeSize()       Size of Expr + space for token
//	**     dupedExprSize()           Expr + token + subtree components
//	**
//	***************************************************************************
//	**
//	** The dupedExprStructSize() function returns two values OR-ed together:
//	** (1) the space required for a copy of the Expr structure only and
//	** (2) the EP_xxx flags that indicate what the structure size should be.
//	** The return values is always one of:
//	**
//	**      EXPR_FULLSIZE
//	**      EXPR_REDUCEDSIZE   | EP_Reduced
//	**      EXPR_TOKENONLYSIZE | EP_TokenOnly
//	**
//	** The size of the structure can be found by masking the return value
//	** of this routine with 0xfff.  The flags can be found by masking the
//	** return value with EP_Reduced|EP_TokenOnly.
//	**
//	** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
//	** (unreduced) Expr objects as they or originally constructed by the parser.
//	** During expression analysis, extra information is computed and moved into
//	** later parts of the Expr object and that extra information might get chopped
//	** off if the expression is reduced.  Note also that it does not work to
//	** make an EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
//	** to reduce a pristine expression tree from the parser.  The implementation
//	** of dupedExprStructSize() contain multiple assert() statements that attempt
//	** to enforce this constraint.
//	*/
func _dupedExprStructSize(tls *libc.TLS, p uintptr, flags int32) (r int32) {
	var nSize int32
	_ = nSize
	/* Only one flag value allowed */
	if 0 == flags || (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_FullSize)) != uint32(0) {
		nSize = int32(52)
	} else {
		if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 || *(*uintptr)(unsafe.Pointer(p + 20)) != 0 {
			nSize = int32(uint32(libc.UintptrFromInt32(0)+28) | libc.Uint32FromInt32(EP_Reduced))
		} else {
			nSize = int32(uint32(libc.UintptrFromInt32(0)+12) | libc.Uint32FromInt32(EP_TokenOnly))
		}
	}
	return nSize
}

// C documentation
//
//	/*
//	** pCArray contains pointers to and sizes of all cells in the page being
//	** balanced.  The current page, pPg, has pPg->nCell cells starting with
//	** pCArray->apCell[iOld].  After balancing, this page should hold nNew cells
//	** starting at apCell[iNew].
//	**
//	** This routine makes the necessary adjustments to pPg so that it contains
//	** the correct cells after being balanced.
//	**
//	** The pPg->nFree field is invalid when this function returns. It is the
//	** responsibility of the caller to set it correctly.
//	*/
func _editPage(tls *libc.TLS, pPg uintptr, iOld int32, iNew int32, nNew int32, pCArray uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aData, pBegin, pCellptr uintptr
	var hdr, i, iCell, iNewEnd, iOldEnd, nAdd, nCell, nShift, nTail, v1 int32
	var _ /* pData at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _ = aData, hdr, i, iCell, iNewEnd, iOldEnd, nAdd, nCell, nShift, nTail, pBegin, pCellptr, v1
	aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData
	hdr = int32((*TMemPage)(unsafe.Pointer(pPg)).FhdrOffset)
	pBegin = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx + uintptr(nNew*int32(2))
	nCell = int32((*TMemPage)(unsafe.Pointer(pPg)).FnCell)
	iOldEnd = iOld + int32((*TMemPage)(unsafe.Pointer(pPg)).FnCell) + int32((*TMemPage)(unsafe.Pointer(pPg)).FnOverflow)
	iNewEnd = iNew + nNew
	/* Remove cells from the start and end of the page */
	if iOld < iNew {
		nShift = _pageFreeArray(tls, pPg, iOld, iNew-iOld, pCArray)
		if nShift > nCell {
			return _sqlite3CorruptError(tls, int32(81125))
		}
		libc.Xmemmove(tls, (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx, (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx+uintptr(nShift*int32(2)), uint32(nCell*int32(2)))
		nCell = nCell - nShift
	}
	if iNewEnd < iOldEnd {
		nTail = _pageFreeArray(tls, pPg, iNewEnd, iOldEnd-iNewEnd, pCArray)
		nCell = nCell - nTail
	}
	**(**uintptr)(__ccgo_up(bp)) = aData + uintptr(int32(**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(5)))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(5)) + 1))))
	if **(**uintptr)(__ccgo_up(bp)) < pBegin {
		goto editpage_fail
	}
	if **(**uintptr)(__ccgo_up(bp)) > (*TMemPage)(unsafe.Pointer(pPg)).FaDataEnd {
		goto editpage_fail
	}
	/* Add cells to the start of the page */
	if iNew < iOld {
		if nNew < iOld-iNew {
			v1 = nNew
		} else {
			v1 = iOld - iNew
		}
		nAdd = v1
		pCellptr = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx
		libc.Xmemmove(tls, pCellptr+uintptr(nAdd*int32(2)), pCellptr, uint32(nCell*int32(2)))
		if _pageInsertArray(tls, pPg, pBegin, bp, pCellptr, iNew, nAdd, pCArray) != 0 {
			goto editpage_fail
		}
		nCell = nCell + nAdd
	}
	/* Add any overflow cells */
	i = 0
	for {
		if !(i < int32((*TMemPage)(unsafe.Pointer(pPg)).FnOverflow)) {
			break
		}
		iCell = iOld + int32(**(**Tu16)(__ccgo_up(pPg + 28 + uintptr(i)*2))) - iNew
		if iCell >= 0 && iCell < nNew {
			pCellptr = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx + uintptr(iCell*int32(2))
			if nCell > iCell {
				libc.Xmemmove(tls, pCellptr+2, pCellptr, uint32((nCell-iCell)*int32(2)))
			}
			nCell = nCell + 1
			_cachedCellSize(tls, pCArray, iCell+iNew)
			if _pageInsertArray(tls, pPg, pBegin, bp, pCellptr, iCell+iNew, int32(1), pCArray) != 0 {
				goto editpage_fail
			}
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	/* Append cells to the end of the page */
	pCellptr = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx + uintptr(nCell*int32(2))
	if _pageInsertArray(tls, pPg, pBegin, bp, pCellptr, iNew+nCell, nNew-nCell, pCArray) != 0 {
		goto editpage_fail
	}
	(*TMemPage)(unsafe.Pointer(pPg)).FnCell = uint16(nNew)
	(*TMemPage)(unsafe.Pointer(pPg)).FnOverflow = uint8(0)
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(3)))) = uint8(int32((*TMemPage)(unsafe.Pointer(pPg)).FnCell) >> libc.Int32FromInt32(8))
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(3)) + 1)) = uint8((*TMemPage)(unsafe.Pointer(pPg)).FnCell)
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(5)))) = uint8((int32(**(**uintptr)(__ccgo_up(bp))) - int32(aData)) >> libc.Int32FromInt32(8))
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(5)) + 1)) = uint8(int32(**(**uintptr)(__ccgo_up(bp))) - int32(aData))
	return SQLITE_OK
	goto editpage_fail
editpage_fail:
	;
	/* Unable to edit this page. Rebuild it from scratch instead. */
	if nNew < int32(1) {
		return _sqlite3CorruptError(tls, int32(81203))
	}
	_populateCellCache(tls, pCArray, iNew, nNew)
	return _rebuildPage(tls, pCArray, iNew, nNew, pPg)
}

// C documentation
//
//	/*
//	** The StrAccum "p" is not large enough to accept N new bytes of z[].
//	** So enlarge if first, then do the append.
//	**
//	** This is a helper routine to sqlite3_str_append() that does special-case
//	** work (enlarging the buffer) using tail recursion, so that the
//	** sqlite3_str_append() routine can use fast calling semantics.
//	*/
func _enlargeAndAppend(tls *libc.TLS, p uintptr, z uintptr, N int32) {
	N = _sqlite3StrAccumEnlarge(tls, p, int64(N))
	if N > 0 {
		libc.Xmemcpy(tls, (*TStrAccum)(unsafe.Pointer(p)).FzText+uintptr((*TStrAccum)(unsafe.Pointer(p)).FnChar), z, uint32(N))
		**(**Tu32)(__ccgo_up(p + 16)) += uint32(N)
	}
}

// C documentation
//
//	/*
//	** Implementation of the sqlite_log() function.  This is a wrapper around
//	** sqlite3_log().  The return value is NULL.  The function exists purely for
//	** its side-effects.
//	*/
func _errlogFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	_ = argc
	_ = context
	Xsqlite3_log(tls, Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv))), __ccgo_ts+4729, libc.VaList(bp+8, Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))))
}

// C documentation
//
//	/*
//	** Estimate the average size of a row for an index.
//	*/
func _estimateIndexWidth(tls *libc.TLS, pIdx uintptr) {
	var aCol uintptr
	var i, v2 int32
	var wIndex uint32
	var x Ti16
	_, _, _, _, _ = aCol, i, wIndex, x, v2
	wIndex = uint32(0)
	aCol = (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol
	i = 0
	for {
		if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) {
			break
		}
		x = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))
		if int32(x) < 0 {
			v2 = int32(1)
		} else {
			v2 = int32((**(**TColumn)(__ccgo_up(aCol + uintptr(x)*12))).FszEst)
		}
		wIndex = wIndex + uint32(v2)
		goto _1
	_1:
		;
		i = i + 1
	}
	(*TIndex)(unsafe.Pointer(pIdx)).FszIdxRow = _sqlite3LogEst(tls, uint64(wIndex*uint32(4)))
}

// C documentation
//
//	/*
//	** Execute zSql on database db.
//	**
//	** If zSql returns rows, then each row will have exactly one
//	** column.  (This will only happen if zSql begins with "SELECT".)
//	** Take each row of result and call execSql() again recursively.
//	**
//	** The execSqlF() routine does the same thing, except it accepts
//	** a format string as its third argument
//	*/
func _execSql(tls *libc.TLS, db uintptr, pzErrMsg uintptr, zSql uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var rc, v1 int32
	var zSubSql uintptr
	var _ /* pStmt at bp+0 */ uintptr
	_, _, _ = rc, zSubSql, v1
	/* printf("SQL: [%s]\n", zSql); fflush(stdout); */
	rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp, uintptr(0))
	if rc != SQLITE_OK {
		return rc
	}
	for {
		v1 = Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp)))
		rc = v1
		if !(int32(SQLITE_ROW) == v1) {
			break
		}
		zSubSql = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), 0)
		/* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
		 ** or INSERT.  Historically there have been attacks that first
		 ** corrupt the sqlite_schema.sql field with other kinds of statements
		 ** then run VACUUM to get those statements to execute at inappropriate
		 ** times. */
		if zSubSql != 0 && (libc.Xstrncmp(tls, zSubSql, __ccgo_ts+23963, uint32(3)) == 0 || libc.Xstrncmp(tls, zSubSql, __ccgo_ts+23967, uint32(3)) == 0) {
			rc = _execSql(tls, db, pzErrMsg, zSubSql)
			if rc != SQLITE_OK {
				break
			}
		}
	}
	if rc == int32(SQLITE_DONE) {
		rc = SQLITE_OK
	}
	if rc != 0 {
		_sqlite3SetString(tls, pzErrMsg, db, Xsqlite3_errmsg(tls, db))
	}
	Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	return rc
}

// C documentation
//
//	/*
//	** Argument pWhere is the WHERE clause belonging to SELECT statement p. This
//	** function attempts to transform expressions of the form:
//	**
//	**     EXISTS (SELECT ...)
//	**
//	** into joins. For example, given
//	**
//	**    CREATE TABLE sailors(sid INTEGER PRIMARY KEY, name TEXT);
//	**    CREATE TABLE reserves(sid INT, day DATE, PRIMARY KEY(sid, day));
//	**
//	**    SELECT name FROM sailors AS S WHERE EXISTS (
//	**      SELECT * FROM reserves AS R WHERE S.sid = R.sid AND R.day = '2022-10-25'
//	**    );
//	**
//	** the SELECT statement may be transformed as follows:
//	**
//	**    SELECT name FROM sailors AS S, reserves AS R
//	**      WHERE S.sid = R.sid AND R.day = '2022-10-25';
//	**
//	** **Approximately**.  Really, we have to ensure that the FROM-clause term
//	** that was formerly inside the EXISTS is only executed once.  This is handled
//	** by setting the SrcItem.fg.fromExists flag, which then causes code in
//	** the where.c file to exit the corresponding loop after the first successful
//	** match (if any).
//	*/
func _existsToJoin(tls *libc.TLS, pParse uintptr, p uintptr, pWhere uintptr) {
	var aCsrMap, db, pRight, pSub, pSubWhere uintptr
	_, _, _, _, _ = aCsrMap, db, pRight, pSub, pSubWhere
	if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 && pWhere != uintptr(0) && !((*TExpr)(unsafe.Pointer(pWhere)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != libc.Uint32FromInt32(0)) && (*TSelect)(unsafe.Pointer(p)).FpSrc != uintptr(0) && (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) && ((*TSelect)(unsafe.Pointer(p)).FpLimit == uintptr(0) || (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpLimit)).FpRight == uintptr(0)) {
		if int32((*TExpr)(unsafe.Pointer(pWhere)).Fop) == int32(TK_AND) {
			pRight = (*TExpr)(unsafe.Pointer(pWhere)).FpRight
			_existsToJoin(tls, pParse, p, (*TExpr)(unsafe.Pointer(pWhere)).FpLeft)
			_existsToJoin(tls, pParse, p, pRight)
		} else {
			if int32((*TExpr)(unsafe.Pointer(pWhere)).Fop) == int32(TK_EXISTS) {
				pSub = *(*uintptr)(unsafe.Pointer(pWhere + 20))
				pSubWhere = (*TSelect)(unsafe.Pointer(pSub)).FpWhere
				if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub)).FpSrc)).FnSrc == int32(1) && (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(SF_Aggregate) == uint32(0) && !(int32(*(*uint32)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub)).FpSrc + 8 + 12 + 4))&0x4>>2) != 0) && (*TSelect)(unsafe.Pointer(pSub)).FpLimit == uintptr(0) && (*TSelect)(unsafe.Pointer(pSub)).FpPrior == uintptr(0) {
					/* Before combining the sub-select with the parent, renumber the
					 ** cursor used by the subselect. This is because the EXISTS expression
					 ** might be a copy of another EXISTS expression from somewhere
					 ** else in the tree, and in this case it is important that it use
					 ** a unique cursor number.  */
					db = (*TParse)(unsafe.Pointer(pParse)).Fdb
					aCsrMap = _sqlite3DbMallocZero(tls, db, uint64(uint32((*TParse)(unsafe.Pointer(pParse)).FnTab+libc.Int32FromInt32(2))*uint32(4)))
					if aCsrMap == uintptr(0) {
						return
					}
					**(**int32)(__ccgo_up(aCsrMap)) = (*TParse)(unsafe.Pointer(pParse)).FnTab + int32(1)
					_renumberCursors(tls, pParse, pSub, -int32(1), aCsrMap)
					_sqlite3DbFree(tls, db, aCsrMap)
					libc.Xmemset(tls, pWhere, 0, uint32(52))
					(*TExpr)(unsafe.Pointer(pWhere)).Fop = uint8(TK_INTEGER)
					*(*int32)(unsafe.Pointer(pWhere + 8)) = int32(1)
					**(**Tu32)(__ccgo_up(pWhere + 4)) |= uint32(libc.Int32FromInt32(EP_IntValue))
					libc.SetBitFieldPtr32Uint32((*TSelect)(unsafe.Pointer(pSub)).FpSrc+8+12+4, libc.Uint32FromInt32(1), 18, 0x40000)
					(*TSelect)(unsafe.Pointer(p)).FpSrc = _sqlite3SrcListAppendList(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpSrc, (*TSelect)(unsafe.Pointer(pSub)).FpSrc)
					if pSubWhere != 0 {
						(*TSelect)(unsafe.Pointer(p)).FpWhere = _sqlite3PExpr(tls, pParse, int32(TK_AND), (*TSelect)(unsafe.Pointer(p)).FpWhere, pSubWhere)
						(*TSelect)(unsafe.Pointer(pSub)).FpWhere = uintptr(0)
					}
					(*TSelect)(unsafe.Pointer(pSub)).FpSrc = uintptr(0)
					_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3SelectDeleteGeneric), pSub)
				}
			}
		}
	}
}

// C documentation
//
//	/*
//	** Return the name of the i-th column of the pIdx index.
//	*/
func _explainIndexColumnName(tls *libc.TLS, pIdx uintptr, i int32) (r uintptr) {
	i = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2)))
	if i == -int32(2) {
		return __ccgo_ts + 24852
	}
	if i == -int32(1) {
		return __ccgo_ts + 19186
	}
	return (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(i)*12))).FzCnName
}

// C documentation
//
//	/*
//	** Add a single OP_Explain instruction to the VDBE to explain a simple
//	** count(*) query ("SELECT count(*) FROM pTab").
//	*/
func _explainSimpleCount(tls *libc.TLS, pParse uintptr, pTab uintptr, pIdx uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bCover int32
	var v1, v2 uintptr
	_, _, _ = bCover, v1, v2
	if int32((*TParse)(unsafe.Pointer(pParse)).Fexplain) == int32(2) {
		bCover = libc.BoolInt32(pIdx != uintptr(0) && ((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) || !(int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == libc.Int32FromInt32(SQLITE_IDXTYPE_PRIMARYKEY))))
		if bCover != 0 {
			v1 = __ccgo_ts + 22759
		} else {
			v1 = __ccgo_ts + 1711
		}
		if bCover != 0 {
			v2 = (*TIndex)(unsafe.Pointer(pIdx)).FzName
		} else {
			v2 = __ccgo_ts + 1711
		}
		_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+22782, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, v1, v2))
	}
}

// C documentation
//
//	/*
//	** The input to this routine is an WhereTerm structure with only the
//	** "pExpr" field filled in.  The job of this routine is to analyze the
//	** subexpression and populate all the other fields of the WhereTerm
//	** structure.
//	**
//	** If the expression is of the form "<expr> <op> X" it gets commuted
//	** to the standard form of "X <op> <expr>".
//	**
//	** If the expression is of the form "X <op> Y" where both X and Y are
//	** columns, then the original expression is unchanged and a new virtual
//	** term of the form "Y <op> X" is added to the WHERE clause and
//	** analyzed separately.  The original term is marked with TERM_COPIED
//	** and the new term is marked with TERM_DYNAMIC (because it's pExpr
//	** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
//	** is a commuted copy of a prior term.)  The original term has nChild=1
//	** and the copy has idxParent set to the index of the original term.
//	*/
func _exprAnalyze(tls *libc.TLS, pSrc uintptr, pWC uintptr, idxTerm int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var c, v12 int8
	var db, pC, pDup, pExpr, pLeft, pLeft1, pLeft2, pLeft3, pList, pMaskSet, pNew, pNew1, pNewExpr, pNewExpr1, pNewExpr11, pNewExpr2, pNewExpr21, pNewTerm, pNewTerm1, pParse, pRight, pRight1, pStr2, pTerm, pWInfo, t, zCollSeqName, v1 uintptr
	var eExtraOp, opMask, wtFlags Tu16
	var extraRight, prereqAll, prereqColumn, prereqExpr, prereqLeft, x TBitmask
	var i, i1, i2, i3, idxNew, idxNew1, idxNew11, idxNew2, idxNew21, idxNew3, idxNew4, idxNew5, nLeft, op, res, v2 int32
	var v15 bool
	var _ /* aiCurCol at bp+16 */ [2]int32
	var _ /* eOp2 at bp+12 */ uint8
	var _ /* isComplete at bp+4 */ int32
	var _ /* noCase at bp+8 */ int32
	var _ /* pLeft at bp+28 */ uintptr
	var _ /* pRight at bp+24 */ uintptr
	var _ /* pStr1 at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, db, eExtraOp, extraRight, i, i1, i2, i3, idxNew, idxNew1, idxNew11, idxNew2, idxNew21, idxNew3, idxNew4, idxNew5, nLeft, op, opMask, pC, pDup, pExpr, pLeft, pLeft1, pLeft2, pLeft3, pList, pMaskSet, pNew, pNew1, pNewExpr, pNewExpr1, pNewExpr11, pNewExpr2, pNewExpr21, pNewTerm, pNewTerm1, pParse, pRight, pRight1, pStr2, pTerm, pWInfo, prereqAll, prereqColumn, prereqExpr, prereqLeft, res, t, wtFlags, x, zCollSeqName, v1, v12, v15, v2
	pWInfo = (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo  /* Prerequisites of pExpr */
	extraRight = uint64(0)                                 /* Extra dependencies on LEFT JOIN */
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)              /* RHS of LIKE/GLOB operator */
	**(**int32)(__ccgo_up(bp + 4)) = 0                     /* RHS of LIKE/GLOB ends with wildcard */
	**(**int32)(__ccgo_up(bp + 8)) = 0                     /* Top-level operator.  pExpr->op */
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse /* Parsing context */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb             /* Database connection */
	**(**uint8)(__ccgo_up(bp + 12)) = uint8(0)             /* Number of elements on left side vector */
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		return
	}
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48
	pMaskSet = pWInfo + 496
	pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
	/* Because malloc() has not failed */
	(*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).FbVarSelect = 0
	prereqLeft = _sqlite3WhereExprUsage(tls, pMaskSet, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
	op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop)
	if op == int32(TK_IN) {
		if _sqlite3ExprCheckIN(tls, pParse, pExpr) != 0 {
			return
		}
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
			(*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight = _exprSelectUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(pExpr + 20)))
		} else {
			(*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight = _sqlite3WhereExprListUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(pExpr + 20)))
		}
		prereqAll = prereqLeft | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight
	} else {
		(*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight = _sqlite3WhereExprUsage(tls, pMaskSet, (*TExpr)(unsafe.Pointer(pExpr)).FpRight)
		if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft == uintptr(0) || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_xIsSelect)|libc.Int32FromInt32(EP_IfNullRow)) != uint32(0) || *(*uintptr)(unsafe.Pointer(pExpr + 20)) != uintptr(0) {
			prereqAll = _sqlite3WhereExprUsageNN(tls, pMaskSet, pExpr)
		} else {
			prereqAll = prereqLeft | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight
		}
	}
	if (*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).FbVarSelect != 0 {
		v1 = pTerm + 10
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_VARSELECT))
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) {
		x = _sqlite3WhereGetMask(tls, pMaskSet, *(*int32)(unsafe.Pointer(pExpr + 36)))
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) {
			prereqAll = prereqAll | x
			extraRight = x - uint64(1) /* ON clause terms may not be used with an index
			 ** on left table of a LEFT JOIN.  Ticket #3015 */
		} else {
			if prereqAll>>libc.Int32FromInt32(1) >= x {
				**(**Tu32)(__ccgo_up(pExpr + 4)) &= ^uint32(libc.Int32FromInt32(EP_InnerON))
			}
		}
	}
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll = prereqAll
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor = -int32(1)
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FiParent = -int32(1)
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(0)
	if _allowedOp(tls, op) != 0 {
		pLeft = _sqlite3ExprSkipCollate(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		pRight = _sqlite3ExprSkipCollate(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight)
		if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&prereqLeft == uint64(0) {
			v2 = int32(WO_ALL)
		} else {
			v2 = int32(WO_EQUIV)
		}
		opMask = uint16(v2)
		if (*(*struct {
			FleftColumn int32
			FiField     int32
		})(unsafe.Pointer(pTerm + 24))).FiField > 0 {
			pLeft = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 20)) + 8 + uintptr((*(*struct {
				FleftColumn int32
				FiField     int32
			})(unsafe.Pointer(pTerm + 24))).FiField-int32(1))*20))).FpExpr
		}
		if _exprMightBeIndexed(tls, pSrc, bp+16, pLeft, op) != 0 {
			(*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor = (**(**[2]int32)(__ccgo_up(bp + 16)))[0]
			(*(*struct {
				FleftColumn int32
				FiField     int32
			})(unsafe.Pointer(pTerm + 24))).FleftColumn = (**(**[2]int32)(__ccgo_up(bp + 16)))[int32(1)]
			(*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(int32(_operatorMask(tls, op)) & int32(opMask))
		}
		if op == int32(TK_IS) {
			v1 = pTerm + 10
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_IS))
		}
		if pRight != 0 && _exprMightBeIndexed(tls, pSrc, bp+16, pRight, op) != 0 && !((*TExpr)(unsafe.Pointer(pRight)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != libc.Uint32FromInt32(0)) {
			eExtraOp = uint16(0) /* Extra bits for pNew->eOperator */
			if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor >= 0 {
				pDup = _sqlite3ExprDup(tls, db, pExpr, 0)
				if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
					_sqlite3ExprDelete(tls, db, pDup)
					return
				}
				idxNew = _whereClauseInsert(tls, pWC, pDup, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC)))
				if idxNew == 0 {
					return
				}
				pNew = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxNew)*48
				_markTermAsChild(tls, pWC, idxNew, idxTerm)
				if op == int32(TK_IS) {
					v1 = pNew + 10
					*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_IS))
				}
				pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48
				v1 = pTerm + 10
				*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_COPIED))
				if _termIsEquivalence(tls, pParse, pDup, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList) != 0 {
					v1 = pTerm + 12
					*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(WO_EQUIV))
					eExtraOp = uint16(WO_EQUIV)
				}
			} else {
				pDup = pExpr
				pNew = pTerm
			}
			v1 = pNew + 10
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | int32(_exprCommute(tls, pParse, pDup)))
			(*TWhereTerm)(unsafe.Pointer(pNew)).FleftCursor = (**(**[2]int32)(__ccgo_up(bp + 16)))[0]
			(*(*struct {
				FleftColumn int32
				FiField     int32
			})(unsafe.Pointer(pNew + 24))).FleftColumn = (**(**[2]int32)(__ccgo_up(bp + 16)))[int32(1)]
			(*TWhereTerm)(unsafe.Pointer(pNew)).FprereqRight = prereqLeft | extraRight
			(*TWhereTerm)(unsafe.Pointer(pNew)).FprereqAll = prereqAll
			(*TWhereTerm)(unsafe.Pointer(pNew)).FeOperator = uint16((int32(_operatorMask(tls, int32((*TExpr)(unsafe.Pointer(pDup)).Fop))) + int32(eExtraOp)) & int32(opMask))
		} else {
			if op == int32(TK_ISNULL) && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) && 0 == _sqlite3ExprCanBeNull(tls, pLeft) {
				(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_TRUEFALSE) /* See tag-20230504-1 */
				*(*uintptr)(unsafe.Pointer(pExpr + 8)) = __ccgo_ts + 9400
				**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_IsFalse))
				(*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll = uint64(0)
				(*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(0)
			}
		}
	} else {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_BETWEEN) && int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) {
			pList = *(*uintptr)(unsafe.Pointer(pExpr + 20))
			i = 0
			for {
				if !(i < int32(2)) {
					break
				}
				pNewExpr = _sqlite3PExpr(tls, pParse, int32(_ops[i]), _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, 0), _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr, 0))
				_transferJoinMarkings(tls, pNewExpr, pExpr)
				idxNew1 = _whereClauseInsert(tls, pWC, pNewExpr, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC)))
				_exprAnalyze(tls, pSrc, pWC, idxNew1)
				pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48
				_markTermAsChild(tls, pWC, idxNew1, idxTerm)
				goto _8
			_8:
				;
				i = i + 1
			}
		} else {
			if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_OR) && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Collate)) != libc.Uint32FromInt32(0)) {
				_exprAnalyzeOrTerm(tls, pSrc, pWC, idxTerm)
				pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48
			} else {
				if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NOTNULL) {
					if int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).Fop) == int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).FiColumn) >= 0 && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) {
						pLeft1 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
						pNewExpr1 = _sqlite3PExpr(tls, pParse, int32(TK_GT), _sqlite3ExprDup(tls, db, pLeft1, 0), _sqlite3ExprAlloc(tls, db, int32(TK_NULL), uintptr(0), 0))
						idxNew2 = _whereClauseInsert(tls, pWC, pNewExpr1, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC)|libc.Int32FromInt32(TERM_VNULL)))
						if idxNew2 != 0 {
							pNewTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxNew2)*48
							(*TWhereTerm)(unsafe.Pointer(pNewTerm)).FprereqRight = uint64(0)
							(*TWhereTerm)(unsafe.Pointer(pNewTerm)).FleftCursor = (*TExpr)(unsafe.Pointer(pLeft1)).FiTable
							(*(*struct {
								FleftColumn int32
								FiField     int32
							})(unsafe.Pointer(pNewTerm + 24))).FleftColumn = int32((*TExpr)(unsafe.Pointer(pLeft1)).FiColumn)
							(*TWhereTerm)(unsafe.Pointer(pNewTerm)).FeOperator = uint16(libc.Int32FromInt32(WO_EQ) << (libc.Int32FromInt32(TK_GT) - libc.Int32FromInt32(TK_EQ)))
							_markTermAsChild(tls, pWC, idxNew2, idxTerm)
							pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48
							v1 = pTerm + 10
							*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_COPIED))
							(*TWhereTerm)(unsafe.Pointer(pNewTerm)).FprereqAll = (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll
						}
					}
				} else {
					if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) && int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) && _isLikeOrGlob(tls, pParse, pExpr, bp, bp+4, bp+8) != 0 { /* Name of collating sequence */
						wtFlags = uint16(libc.Int32FromInt32(TERM_LIKEOPT) | libc.Int32FromInt32(TERM_VIRTUAL) | libc.Int32FromInt32(TERM_DYNAMIC))
						pLeft2 = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 1*20))).FpExpr
						pStr2 = _sqlite3ExprDup(tls, db, **(**uintptr)(__ccgo_up(bp)), 0)
						/* Convert the lower bound to upper-case and the upper bound to
						 ** lower-case (upper-case is less than lower-case in ASCII) so that
						 ** the range constraints also work for BLOBs
						 */
						if **(**int32)(__ccgo_up(bp + 8)) != 0 && !((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0) {
							v1 = pTerm + 10
							*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_LIKE))
							i1 = 0
							for {
								v12 = **(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)) + 8)) + uintptr(i1)))
								c = v12
								if !(int32(v12) != 0) {
									break
								}
								**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)) + 8)) + uintptr(i1))) = int8(int32(c) & ^(int32(_sqlite3CtypeMap[uint8(c)]) & libc.Int32FromInt32(0x20)))
								**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pStr2 + 8)) + uintptr(i1))) = int8(_sqlite3UpperToLower[uint8(c)])
								goto _11
							_11:
								;
								i1 = i1 + 1
							}
						}
						if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { /* Last character before the first wildcard */
							pC = *(*uintptr)(unsafe.Pointer(pStr2 + 8)) + uintptr(_sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(pStr2 + 8)))-int32(1))
							if **(**int32)(__ccgo_up(bp + 8)) != 0 {
								/* The point is to increment the last character before the first
								 ** wildcard.  But if we increment '@', that will push it into the
								 ** alphabetic range where case conversions will mess up the
								 ** inequality.  To avoid this, make sure to also run the full
								 ** LIKE on all candidate expressions by clearing the isComplete flag
								 */
								if int32(**(**Tu8)(__ccgo_up(pC))) == libc.Int32FromUint8('A')-libc.Int32FromInt32(1) {
									**(**int32)(__ccgo_up(bp + 4)) = 0
								}
								**(**Tu8)(__ccgo_up(pC)) = _sqlite3UpperToLower[**(**Tu8)(__ccgo_up(pC))]
							}
							/* Increment the value of the last utf8 character in the prefix. */
							for int32(**(**Tu8)(__ccgo_up(pC))) == int32(0xBF) && pC > *(*uintptr)(unsafe.Pointer(pStr2 + 8)) {
								**(**Tu8)(__ccgo_up(pC)) = uint8(0x80)
								pC = pC - 1
							}
							/* isLikeOrGlob() guarantees this */
							**(**Tu8)(__ccgo_up(pC)) = **(**Tu8)(__ccgo_up(pC)) + 1
						}
						if **(**int32)(__ccgo_up(bp + 8)) != 0 {
							v1 = __ccgo_ts + 25175
						} else {
							v1 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
						}
						zCollSeqName = v1
						pNewExpr11 = _sqlite3ExprDup(tls, db, pLeft2, 0)
						pNewExpr11 = _sqlite3PExpr(tls, pParse, int32(TK_GE), _sqlite3ExprAddCollateString(tls, pParse, pNewExpr11, zCollSeqName), **(**uintptr)(__ccgo_up(bp)))
						_transferJoinMarkings(tls, pNewExpr11, pExpr)
						idxNew11 = _whereClauseInsert(tls, pWC, pNewExpr11, wtFlags)
						pNewExpr21 = _sqlite3ExprDup(tls, db, pLeft2, 0)
						pNewExpr21 = _sqlite3PExpr(tls, pParse, int32(TK_LT), _sqlite3ExprAddCollateString(tls, pParse, pNewExpr21, zCollSeqName), pStr2)
						_transferJoinMarkings(tls, pNewExpr21, pExpr)
						idxNew21 = _whereClauseInsert(tls, pWC, pNewExpr21, wtFlags)
						_exprAnalyze(tls, pSrc, pWC, idxNew11)
						_exprAnalyze(tls, pSrc, pWC, idxNew21)
						pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48
						if **(**int32)(__ccgo_up(bp + 4)) != 0 {
							_markTermAsChild(tls, pWC, idxNew11, idxTerm)
							_markTermAsChild(tls, pWC, idxNew21, idxTerm)
						}
					}
				}
			}
		}
	}
	/* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
	 ** new terms for each component comparison - "a = ?" and "b = ?".  The
	 ** new terms completely replace the original vector comparison, which is
	 ** no longer used.
	 **
	 ** This is only required if at least one side of the comparison operation
	 ** is not a sub-select.
	 **
	 ** tag-20220128a
	 */
	if v15 = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_EQ) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IS); v15 {
		v2 = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		nLeft = v2
	}
	if v15 && v2 > int32(1) && _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) == nLeft && ((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).Fflags&uint32(EP_xIsSelect) == uint32(0) || (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpRight)).Fflags&uint32(EP_xIsSelect) == uint32(0)) && int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) {
		i2 = 0
		for {
			if !(i2 < nLeft) {
				break
			}
			pLeft3 = _sqlite3ExprForVectorField(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, i2, nLeft)
			pRight1 = _sqlite3ExprForVectorField(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, i2, nLeft)
			pNew1 = _sqlite3PExpr(tls, pParse, int32((*TExpr)(unsafe.Pointer(pExpr)).Fop), pLeft3, pRight1)
			_transferJoinMarkings(tls, pNew1, pExpr)
			idxNew3 = _whereClauseInsert(tls, pWC, pNew1, uint16(libc.Int32FromInt32(TERM_DYNAMIC)|libc.Int32FromInt32(TERM_SLICE)))
			_exprAnalyze(tls, pSrc, pWC, idxNew3)
			goto _16
		_16:
			;
			i2 = i2 + 1
		}
		pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48
		v1 = pTerm + 10
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | (libc.Int32FromInt32(TERM_CODED) | libc.Int32FromInt32(TERM_VIRTUAL))) /* Disable the original */
		(*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(WO_ROWVAL)
	} else {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IN) && (*(*struct {
			FleftColumn int32
			FiField     int32
		})(unsafe.Pointer(pTerm + 24))).FiField == 0 && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft)).Fop) == int32(TK_VECTOR) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) && ((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpPrior == uintptr(0) || (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FselFlags&uint32(SF_Values) != 0) && (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpWin == uintptr(0) && int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) && int64((*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList)).FnExpr) <= libc.Int64FromInt32(1)<<(libc.Uint32FromInt64(1)*libc.Uint32FromInt32(8))-libc.Int64FromInt32(1) {
			i3 = 0
			for {
				if !(i3 < _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)) {
					break
				}
				idxNew4 = _whereClauseInsert(tls, pWC, pExpr, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_SLICE)))
				*(*int32)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxNew4)*48 + 24 + 4)) = i3 + int32(1)
				_exprAnalyze(tls, pSrc, pWC, idxNew4)
				_markTermAsChild(tls, pWC, idxNew4, idxTerm)
				goto _18
			_18:
				;
				i3 = i3 + 1
			}
		} else {
			if int32((*TWhereClause)(unsafe.Pointer(pWC)).Fop) == int32(TK_AND) {
				**(**uintptr)(__ccgo_up(bp + 24)) = uintptr(0)
				**(**uintptr)(__ccgo_up(bp + 28)) = uintptr(0)
				res = _isAuxiliaryVtabOperator(tls, db, pExpr, bp+12, bp+28, bp+24)
				for {
					v2 = res
					res = res - 1
					if !(v2 > 0) {
						break
					}
					prereqExpr = _sqlite3WhereExprUsage(tls, pMaskSet, **(**uintptr)(__ccgo_up(bp + 24)))
					prereqColumn = _sqlite3WhereExprUsage(tls, pMaskSet, **(**uintptr)(__ccgo_up(bp + 28)))
					if prereqExpr&prereqColumn == uint64(0) {
						pNewExpr2 = _sqlite3PExpr(tls, pParse, int32(TK_MATCH), uintptr(0), _sqlite3ExprDup(tls, db, **(**uintptr)(__ccgo_up(bp + 24)), 0))
						if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) && pNewExpr2 != 0 {
							**(**Tu32)(__ccgo_up(pNewExpr2 + 4)) |= uint32(libc.Int32FromInt32(EP_OuterON))
							*(*int32)(unsafe.Pointer(pNewExpr2 + 36)) = *(*int32)(unsafe.Pointer(pExpr + 36))
						}
						idxNew5 = _whereClauseInsert(tls, pWC, pNewExpr2, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC)))
						pNewTerm1 = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxNew5)*48
						(*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FprereqRight = prereqExpr | extraRight
						(*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FleftCursor = (*TExpr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 28)))).FiTable
						(*(*struct {
							FleftColumn int32
							FiField     int32
						})(unsafe.Pointer(pNewTerm1 + 24))).FleftColumn = int32((*TExpr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 28)))).FiColumn)
						(*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FeOperator = uint16(WO_AUX)
						(*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FeMatchOp = **(**uint8)(__ccgo_up(bp + 12))
						_markTermAsChild(tls, pWC, idxNew5, idxTerm)
						pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48
						v1 = pTerm + 10
						*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_COPIED))
						(*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FprereqAll = (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll
					}
					t = **(**uintptr)(__ccgo_up(bp + 28))
					**(**uintptr)(__ccgo_up(bp + 28)) = **(**uintptr)(__ccgo_up(bp + 24))
					**(**uintptr)(__ccgo_up(bp + 24)) = t
				}
			}
		}
	}
	/* Prevent ON clause terms of a LEFT JOIN from being used to drive
	 ** an index for tables to the left of the join.
	 */
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48
	**(**TBitmask)(__ccgo_up(pTerm + 32)) |= extraRight
}

// C documentation
//
//	/*
//	** Analyze a term that consists of two or more OR-connected
//	** subterms.  So in:
//	**
//	**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
//	**                          ^^^^^^^^^^^^^^^^^^^^
//	**
//	** This routine analyzes terms such as the middle term in the above example.
//	** A WhereOrTerm object is computed and attached to the term under
//	** analysis, regardless of the outcome of the analysis.  Hence:
//	**
//	**     WhereTerm.wtFlags   |=  TERM_ORINFO
//	**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
//	**
//	** The term being analyzed must have two or more of OR-connected subterms.
//	** A single subterm might be a set of AND-connected sub-subterms.
//	** Examples of terms under analysis:
//	**
//	**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
//	**     (B)     x=expr1 OR expr2=x OR x=expr3
//	**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
//	**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
//	**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
//	**     (F)     x>A OR (x=A AND y>=B)
//	**
//	** CASE 1:
//	**
//	** If all subterms are of the form T.C=expr for some single column of C and
//	** a single table T (as shown in example B above) then create a new virtual
//	** term that is an equivalent IN expression.  In other words, if the term
//	** being analyzed is:
//	**
//	**      x = expr1  OR  expr2 = x  OR  x = expr3
//	**
//	** then create a new virtual term like this:
//	**
//	**      x IN (expr1,expr2,expr3)
//	**
//	** CASE 2:
//	**
//	** If there are exactly two disjuncts and one side has x>A and the other side
//	** has x=A (for the same x and A) then add a new virtual conjunct term to the
//	** WHERE clause of the form "x>=A".  Example:
//	**
//	**      x>A OR (x=A AND y>B)    adds:    x>=A
//	**
//	** The added conjunct can sometimes be helpful in query planning.
//	**
//	** CASE 3:
//	**
//	** If all subterms are indexable by a single table T, then set
//	**
//	**     WhereTerm.eOperator              =  WO_OR
//	**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
//	**
//	** A subterm is "indexable" if it is of the form
//	** "T.C <op> <expr>" where C is any column of table T and
//	** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
//	** A subterm is also indexable if it is an AND of two or more
//	** subsubterms at least one of which is indexable.  Indexable AND
//	** subterms have their eOperator set to WO_AND and they have
//	** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
//	**
//	** From another point of view, "indexable" means that the subterm could
//	** potentially be used with an index if an appropriate index exists.
//	** This analysis does not consider whether or not the index exists; that
//	** is decided elsewhere.  This analysis only looks at whether subterms
//	** appropriate for indexing exist.
//	**
//	** All examples A through E above satisfy case 3.  But if a term
//	** also satisfies case 1 (such as B) we know that the optimizer will
//	** always prefer case 1, so in that case we pretend that case 3 is not
//	** satisfied.
//	**
//	** It might be the case that multiple tables are indexable.  For example,
//	** (E) above is indexable on tables P, Q, and R.
//	**
//	** Terms that satisfy case 3 are candidates for lookup by using
//	** separate indices to find rowids for each subterm and composing
//	** the union of all rowids using a RowSet object.  This is similar
//	** to "bitmap indices" in other database engines.
//	**
//	** OTHERWISE:
//	**
//	** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
//	** zero.  This term is not useful for search.
//	*/
func _exprAnalyzeOrTerm(tls *libc.TLS, pSrc uintptr, pWC uintptr, idxTerm int32) {
	var affLeft, affRight, i, iColumn, iCursor, iOne, iTwo, idxNew, j, j1, okToChngToIN, v7, v9 int32
	var b, b1, chngToIN, indexable TBitmask
	var db, pAndInfo, pAndTerm, pAndWC, pDup, pExpr, pLeft, pLeft1, pList, pNew, pOne, pOrInfo, pOrTerm, pOrWc, pOther, pParse, pTerm, pTwo, pWInfo, v1, v2 uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = affLeft, affRight, b, b1, chngToIN, db, i, iColumn, iCursor, iOne, iTwo, idxNew, indexable, j, j1, okToChngToIN, pAndInfo, pAndTerm, pAndWC, pDup, pExpr, pLeft, pLeft1, pList, pNew, pOne, pOrInfo, pOrTerm, pOrWc, pOther, pParse, pTerm, pTwo, pWInfo, v1, v2, v7, v9
	pWInfo = (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo                 /* WHERE clause processing context */
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse                /* Parser context */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb                            /* Database connection */
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*48 /* The term to be analyzed */
	pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr                   /* Tables that are indexable, satisfying case 2 */
	/*
	 ** Break the OR clause into its separate subterms.  The subterms are
	 ** stored in a WhereClause structure containing within the WhereOrInfo
	 ** object that is attached to the original OR clause term.
	 */
	v1 = _sqlite3DbMallocZero(tls, db, uint64(424))
	pOrInfo = v1
	*(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)) = v1
	if pOrInfo == uintptr(0) {
		return
	}
	v1 = pTerm + 10
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_ORINFO))
	pOrWc = pOrInfo
	libc.Xmemset(tls, pOrWc+32, 0, uint32(384))
	_sqlite3WhereClauseInit(tls, pOrWc, pWInfo)
	_sqlite3WhereSplit(tls, pOrWc, pExpr, uint8(TK_OR))
	_sqlite3WhereExprAnalyze(tls, pSrc, pOrWc)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		return
	}
	/*
	 ** Compute the set of tables that might satisfy cases 1 or 3.
	 */
	indexable = ^libc.Uint64FromInt32(0)
	chngToIN = ^libc.Uint64FromInt32(0)
	i = (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm - int32(1)
	pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa
	for {
		if !(i >= 0 && indexable != 0) {
			break
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator)&int32(WO_SINGLE) == 0 {
			chngToIN = uint64(0)
			pAndInfo = _sqlite3DbMallocRawNN(tls, db, uint64(416))
			if pAndInfo != 0 {
				b = uint64(0)
				*(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pOrTerm)).Fu)) = pAndInfo
				v1 = pOrTerm + 10
				*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_ANDINFO))
				(*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator = uint16(WO_AND)
				(*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor = -int32(1)
				pAndWC = pAndInfo
				libc.Xmemset(tls, pAndWC+32, 0, uint32(384))
				_sqlite3WhereClauseInit(tls, pAndWC, (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)
				_sqlite3WhereSplit(tls, pAndWC, (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr, uint8(TK_AND))
				_sqlite3WhereExprAnalyze(tls, pSrc, pAndWC)
				(*TWhereClause)(unsafe.Pointer(pAndWC)).FpOuter = pWC
				if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
					j = 0
					pAndTerm = (*TWhereClause)(unsafe.Pointer(pAndWC)).Fa
					for {
						if !(j < (*TWhereClause)(unsafe.Pointer(pAndWC)).FnTerm) {
							break
						}
						if _allowedOp(tls, int32((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pAndTerm)).FpExpr)).Fop)) != 0 || int32((*TWhereTerm)(unsafe.Pointer(pAndTerm)).FeOperator) == int32(WO_AUX) {
							b = b | _sqlite3WhereGetMask(tls, pWInfo+496, (*TWhereTerm)(unsafe.Pointer(pAndTerm)).FleftCursor)
						}
						goto _5
					_5:
						;
						j = j + 1
						pAndTerm += 48
					}
				}
				indexable = indexable & b
			}
		} else {
			if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FwtFlags)&int32(TERM_COPIED) != 0 {
				/* Skip this term for now.  We revisit it when we process the
				 ** corresponding TERM_VIRTUAL term */
			} else {
				b1 = _sqlite3WhereGetMask(tls, pWInfo+496, (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor)
				if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FwtFlags)&int32(TERM_VIRTUAL) != 0 {
					pOther = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa + uintptr((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FiParent)*48
					b1 = b1 | _sqlite3WhereGetMask(tls, pWInfo+496, (*TWhereTerm)(unsafe.Pointer(pOther)).FleftCursor)
				}
				indexable = indexable & b1
				if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator)&int32(WO_EQ) == 0 {
					chngToIN = uint64(0)
				} else {
					chngToIN = chngToIN & b1
				}
			}
		}
		goto _3
	_3:
		;
		i = i - 1
		pOrTerm += 48
	}
	/*
	 ** Record the set of tables that satisfy case 3.  The set might be
	 ** empty.
	 */
	(*TWhereOrInfo)(unsafe.Pointer(pOrInfo)).Findexable = indexable
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(WO_OR)
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor = -int32(1)
	if indexable != 0 {
		(*TWhereClause)(unsafe.Pointer(pWC)).FhasOr = uint8(1)
	}
	/* For a two-way OR, attempt to implementation case 2.
	 */
	if indexable != 0 && (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm == int32(2) {
		iOne = 0
		for {
			v7 = iOne
			iOne = iOne + 1
			v1 = _whereNthSubterm(tls, (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa, v7)
			pOne = v1
			if !(v1 != uintptr(0)) {
				break
			}
			iTwo = 0
			for {
				v9 = iTwo
				iTwo = iTwo + 1
				v2 = _whereNthSubterm(tls, (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa+1*48, v9)
				pTwo = v2
				if !(v2 != uintptr(0)) {
					break
				}
				_whereCombineDisjuncts(tls, pSrc, pWC, pOne, pTwo)
			}
		}
	}
	/*
	 ** chngToIN holds a set of tables that *might* satisfy case 1.  But
	 ** we have to do some additional checking to see if case 1 really
	 ** is satisfied.
	 **
	 ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
	 ** that there is no possibility of transforming the OR clause into an
	 ** IN operator because one or more terms in the OR clause contain
	 ** something other than == on a column in the single table.  The 1-bit
	 ** case means that every term of the OR clause is of the form
	 ** "table.column=expr" for some single table.  The one bit that is set
	 ** will correspond to the common table.  We still need to check to make
	 ** sure the same column is used on all terms.  The 2-bit case is when
	 ** the all terms are of the form "table1.column=table2.column".  It
	 ** might be possible to form an IN operator with either table1.column
	 ** or table2.column as the LHS if either is common to every term of
	 ** the OR clause.
	 **
	 ** Note that terms of the form "table.column1=table.column2" (the
	 ** same table on both sizes of the ==) cannot be optimized.
	 */
	if chngToIN != 0 {
		okToChngToIN = 0    /* True if the conversion to IN is valid */
		iColumn = -int32(1) /* Column index on lhs of IN operator */
		iCursor = -int32(1) /* Table cursor common to all terms */
		j1 = 0              /* Loop counter */
		/* Search for a table and column that appears on one side or the
		 ** other of the == operator in every subterm.  That table and column
		 ** will be recorded in iCursor and iColumn.  There might not be any
		 ** such table and column.  Set okToChngToIN if an appropriate table
		 ** and column is found but leave okToChngToIN false if not found.
		 */
		j1 = 0
		for {
			if !(j1 < int32(2) && !(okToChngToIN != 0)) {
				break
			}
			pLeft = uintptr(0)
			pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa
			i = (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm - int32(1)
			for {
				if !(i >= 0) {
					break
				}
				v1 = pOrTerm + 10
				*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(TERM_OK))
				if (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor == iCursor {
					/* This is the 2-bit case and we are on the second iteration and
					 ** current term is from the first iteration.  So skip this term. */
					goto _11
				}
				if chngToIN&_sqlite3WhereGetMask(tls, pWInfo+496, (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor) == uint64(0) {
					/* This term must be of the form t1.a==t2.b where t2 is in the
					 ** chngToIN set but t1 is not.  This term will be either preceded
					 ** or followed by an inverted copy (t2.b==t1.a).  Skip this term
					 ** and use its inversion. */
					goto _11
				}
				iColumn = (*(*struct {
					FleftColumn int32
					FiField     int32
				})(unsafe.Pointer(pOrTerm + 24))).FleftColumn
				iCursor = (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor
				pLeft = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpLeft
				break
				goto _11
			_11:
				;
				i = i - 1
				pOrTerm += 48
			}
			if i < 0 {
				/* No candidate table+column was found.  This can only occur
				 ** on the second iteration */
				break
			}
			/* We have found a candidate table and column.  Check to see if that
			 ** table and column is common to every term in the OR clause */
			okToChngToIN = int32(1)
			for {
				if !(i >= 0 && okToChngToIN != 0) {
					break
				}
				if (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor != iCursor {
					v1 = pOrTerm + 10
					*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(TERM_OK))
				} else {
					if (*(*struct {
						FleftColumn int32
						FiField     int32
					})(unsafe.Pointer(pOrTerm + 24))).FleftColumn != iColumn || iColumn == -int32(2) && _sqlite3ExprCompare(tls, pParse, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpLeft, pLeft, -int32(1)) != 0 {
						okToChngToIN = 0
					} else {
						/* If the right-hand side is also a column, then the affinities
						 ** of both right and left sides must be such that no type
						 ** conversions are required on the right.  (Ticket #2249)
						 */
						affRight = int32(_sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpRight))
						affLeft = int32(_sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpLeft))
						if affRight != 0 && affRight != affLeft {
							okToChngToIN = 0
						} else {
							v1 = pOrTerm + 10
							*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(TERM_OK))
						}
					}
				}
				goto _13
			_13:
				;
				i = i - 1
				pOrTerm += 48
			}
			goto _10
		_10:
			;
			j1 = j1 + 1
		}
		/* At this point, okToChngToIN is true if original pTerm satisfies
		 ** case 1.  In that case, construct a new virtual term that is
		 ** pTerm converted into an IN operator.
		 */
		if okToChngToIN != 0 { /* A transient duplicate expression */
			pList = uintptr(0)  /* The RHS of the IN operator */
			pLeft1 = uintptr(0) /* The complete IN operator */
			i = (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm - int32(1)
			pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa
			for {
				if !(i >= 0) {
					break
				}
				if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FwtFlags)&int32(TERM_OK) == 0 {
					goto _16
				}
				pDup = _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpRight, 0)
				pList = _sqlite3ExprListAppend(tls, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse, pList, pDup)
				pLeft1 = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpLeft
				goto _16
			_16:
				;
				i = i - 1
				pOrTerm += 48
			}
			pDup = _sqlite3ExprDup(tls, db, pLeft1, 0)
			pNew = _sqlite3PExpr(tls, pParse, int32(TK_IN), pDup, uintptr(0))
			if pNew != 0 {
				_transferJoinMarkings(tls, pNew, pExpr)
				*(*uintptr)(unsafe.Pointer(pNew + 20)) = pList
				idxNew = _whereClauseInsert(tls, pWC, pNew, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC)))
				_exprAnalyze(tls, pSrc, pWC, idxNew)
				/* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
				_markTermAsChild(tls, pWC, idxNew, idxTerm)
			} else {
				_sqlite3ExprListDelete(tls, db, pList)
			}
		}
	}
}

// C documentation
//
//	/*
//	** Generate code for a BETWEEN operator.
//	**
//	**    x BETWEEN y AND z
//	**
//	** The above is equivalent to
//	**
//	**    x>=y AND x<=z
//	**
//	** Code it as such, taking care to do the common subexpression
//	** elimination of x.
//	**
//	** The xJumpIf parameter determines details:
//	**
//	**    NULL:                   Store the boolean result in reg[dest]
//	**    sqlite3ExprIfTrue:      Jump to dest if true
//	**    sqlite3ExprIfFalse:     Jump to dest if false
//	**
//	** The jumpIfNull parameter is ignored if xJumpIf is NULL.
//	*/
func _exprCodeBetween(tls *libc.TLS, pParse uintptr, pExpr uintptr, dest int32, __ccgo_fp_xJump uintptr, jumpIfNull int32) {
	bp := tls.Alloc(160)
	defer tls.Free(160)
	var db, pDel uintptr
	var _ /* compLeft at bp+52 */ TExpr
	var _ /* compRight at bp+104 */ TExpr
	var _ /* exprAnd at bp+0 */ TExpr
	var _ /* regFree1 at bp+156 */ int32
	_, _ = db, pDel                      /* The  x<=z  term */
	**(**int32)(__ccgo_up(bp + 156)) = 0 /* Temporary use register */
	pDel = uintptr(0)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	libc.Xmemset(tls, bp+52, 0, uint32(52))
	libc.Xmemset(tls, bp+104, 0, uint32(52))
	libc.Xmemset(tls, bp, 0, uint32(52))
	pDel = _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, 0)
	if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
		(**(**TExpr)(__ccgo_up(bp))).Fop = uint8(TK_AND)
		(**(**TExpr)(__ccgo_up(bp))).FpLeft = bp + 52
		(**(**TExpr)(__ccgo_up(bp))).FpRight = bp + 104
		(**(**TExpr)(__ccgo_up(bp + 52))).Fop = uint8(TK_GE)
		(**(**TExpr)(__ccgo_up(bp + 52))).FpLeft = pDel
		(**(**TExpr)(__ccgo_up(bp + 52))).FpRight = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr
		(**(**TExpr)(__ccgo_up(bp + 104))).Fop = uint8(TK_LE)
		(**(**TExpr)(__ccgo_up(bp + 104))).FpLeft = pDel
		(**(**TExpr)(__ccgo_up(bp + 104))).FpRight = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 1*20))).FpExpr
		_sqlite3ExprToRegister(tls, pDel, _exprCodeVector(tls, pParse, pDel, bp+156))
		if __ccgo_fp_xJump != 0 {
			(*(*func(*libc.TLS, uintptr, uintptr, int32, int32))(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xJump})))(tls, pParse, bp, dest, jumpIfNull)
		} else {
			/* Mark the expression is being from the ON or USING clause of a join
			 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
			 ** it into the Parse.pConstExpr list.  We should use a new bit for this,
			 ** for clarity, but we are out of bits in the Expr.flags field so we
			 ** have to reuse the EP_OuterON bit.  Bummer. */
			**(**Tu32)(__ccgo_up(pDel + 4)) |= uint32(EP_OuterON)
			_sqlite3ExprCodeTarget(tls, pParse, bp, dest)
		}
		_sqlite3ReleaseTempReg(tls, pParse, **(**int32)(__ccgo_up(bp + 156)))
	}
	_sqlite3ExprDelete(tls, db, pDel)
	/* Ensure adequate test coverage */
}

// C documentation
//
//	/*
//	** Generate code to implement special SQL functions that are implemented
//	** in-line rather than by using the usual callbacks.
//	*/
func _exprCodeInlineFunction(tls *libc.TLS, pParse uintptr, pFarg uintptr, iFuncId int32, target int32) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var aff int8
	var azAff [6]uintptr
	var endCoalesce, i, nFarg int32
	var pA1, pArg, v, v2 uintptr
	var _ /* caseExpr at bp+0 */ TExpr
	_, _, _, _, _, _, _, _, _ = aff, azAff, endCoalesce, i, nFarg, pA1, pArg, v, v2
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	nFarg = (*TExprList)(unsafe.Pointer(pFarg)).FnExpr
	/* All in-line functions have at least one argument */
	switch iFuncId {
	case INLINEFUNC_coalesce:
		/* Attempt a direct implementation of the built-in COALESCE() and
		 ** IFNULL() functions.  This avoids unnecessary evaluation of
		 ** arguments past the first non-NULL argument.
		 */
		endCoalesce = _sqlite3VdbeMakeLabel(tls, pParse)
		_sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, target)
		i = int32(1)
		for {
			if !(i < nFarg) {
				break
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), target, endCoalesce)
			_sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + uintptr(i)*20))).FpExpr, target)
			goto _1
		_1:
			;
			i = i + 1
		}
		_setDoNotMergeFlagOnCopy(tls, v)
		_sqlite3VdbeResolveLabel(tls, v, endCoalesce)
	case int32(INLINEFUNC_iif):
		libc.Xmemset(tls, bp, 0, uint32(52))
		(**(**TExpr)(__ccgo_up(bp))).Fop = uint8(TK_CASE)
		*(*uintptr)(unsafe.Pointer(bp + 20)) = pFarg
		return _sqlite3ExprCodeTarget(tls, pParse, bp, target)
	case int32(INLINEFUNC_sqlite_offset):
		pArg = (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr
		if int32((*TExpr)(unsafe.Pointer(pArg)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pArg)).FiTable >= 0 {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Offset), (*TExpr)(unsafe.Pointer(pArg)).FiTable, int32((*TExpr)(unsafe.Pointer(pArg)).FiColumn), target)
		} else {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target)
		}
	default:
		/* The UNLIKELY() function is a no-op.  The result is the value
		 ** of the first argument.
		 */
		target = _sqlite3ExprCodeTarget(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, target)
		break
		/***********************************************************************
		 ** Test-only SQL functions that are only usable if enabled
		 ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
		 */
		fallthrough
	case int32(INLINEFUNC_expr_compare):
		/* Compare two expressions using sqlite3ExprCompare() */
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*20))).FpExpr, -int32(1)), target)
	case int32(INLINEFUNC_expr_implies_expr):
		/* Compare two expressions using sqlite3ExprImpliesExpr() */
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprImpliesExpr(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*20))).FpExpr, -int32(1)), target)
	case int32(INLINEFUNC_implies_nonnull_row):
		pA1 = (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*20))).FpExpr
		if int32((*TExpr)(unsafe.Pointer(pA1)).Fop) == int32(TK_COLUMN) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprImpliesNonNullRow(tls, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, (*TExpr)(unsafe.Pointer(pA1)).FiTable, int32(1)), target)
		} else {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target)
		}
	case int32(INLINEFUNC_affinity):
		/* The AFFINITY() function evaluates to a string that describes
		 ** the type affinity of the argument.  This is used for testing of
		 ** the SQLite type logic.
		 */
		azAff = [6]uintptr{
			0: __ccgo_ts + 9699,
			1: __ccgo_ts + 9704,
			2: __ccgo_ts + 9709,
			3: __ccgo_ts + 7709,
			4: __ccgo_ts + 7704,
			5: __ccgo_ts + 9717,
		}
		aff = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr)
		if int32(aff) <= int32(SQLITE_AFF_NONE) {
			v2 = __ccgo_ts + 9725
		} else {
			v2 = azAff[int32(aff)-int32(SQLITE_AFF_BLOB)]
		}
		_sqlite3VdbeLoadString(tls, v, target, v2)
		break
	}
	return target
}

// C documentation
//
//	/*
//	** Evaluate an expression (either a vector or a scalar expression) and store
//	** the result in contiguous temporary registers.  Return the index of
//	** the first register used to store the result.
//	**
//	** If the returned result register is a temporary scalar, then also write
//	** that register number into *piFreeable.  If the returned result register
//	** is not a temporary or if the expression is a vector set *piFreeable
//	** to 0.
//	*/
func _exprCodeVector(tls *libc.TLS, pParse uintptr, p uintptr, piFreeable uintptr) (r int32) {
	var i, iResult, nResult int32
	_, _, _ = i, iResult, nResult
	nResult = _sqlite3ExprVectorSize(tls, p)
	if nResult == int32(1) {
		iResult = _sqlite3ExprCodeTemp(tls, pParse, p, piFreeable)
	} else {
		**(**int32)(__ccgo_up(piFreeable)) = 0
		if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_SELECT) {
			iResult = _sqlite3CodeSubselect(tls, pParse, p)
		} else {
			iResult = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
			**(**int32)(__ccgo_up(pParse + 48)) += nResult
			i = 0
			for {
				if !(i < nResult) {
					break
				}
				_sqlite3ExprCodeFactorable(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8 + uintptr(i)*20))).FpExpr, i+iResult)
				goto _1
			_1:
				;
				i = i + 1
			}
		}
	}
	return iResult
}

// C documentation
//
//	/* This walker callback will compute the union of colFlags flags for all
//	** referenced columns in a CHECK constraint or generated column expression.
//	*/
func _exprColumnFlagUnion(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var v1 uintptr
	_ = v1
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) >= 0 {
		v1 = pWalker + 20
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 24)))).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)*12))).FcolFlags))
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** This function is similar to sqlite3ExprDup(), except that if pEdupBuf
//	** is not NULL then it points to memory that can be used to store a copy
//	** of the input Expr p together with its p->u.zToken (if any).  pEdupBuf
//	** is updated with the new buffer tail prior to returning.
//	*/
func _exprDup(tls *libc.TLS, db uintptr, p uintptr, dupFlags int32, pEdupBuf uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nAlloc, nNewSize, nToken, v2 int32
	var nSize, staticFlag Tu32
	var nStructSize uint32
	var pNew, zToken, v1 uintptr
	var _ /* sEdupBuf at bp+0 */ TEdupBuf
	_, _, _, _, _, _, _, _, _, _ = nAlloc, nNewSize, nSize, nStructSize, nToken, pNew, staticFlag, zToken, v1, v2 /* EP_Static if space not obtained from malloc */
	nToken = -int32(1)                                                                                            /* Space needed for p->u.zToken.  -1 means unknown */
	/* Figure out where to write the new Expr structure. */
	if pEdupBuf != 0 {
		(**(**TEdupBuf)(__ccgo_up(bp))).FzAlloc = (*TEdupBuf)(unsafe.Pointer(pEdupBuf)).FzAlloc
		staticFlag = uint32(EP_Static)
	} else {
		if dupFlags != 0 {
			nAlloc = _dupedExprSize(tls, p)
		} else {
			if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != libc.Uint32FromInt32(0)) && *(*uintptr)(unsafe.Pointer(p + 8)) != 0 {
				nToken = int32(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(p + 8)))&uint32(0x3fffffff) + uint32(1))
				nAlloc = int32((libc.Uint32FromInt64(52) + uint32(nToken) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7)))
			} else {
				nToken = 0
				nAlloc = int32((libc.Uint32FromInt64(52) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7)))
			}
		}
		(**(**TEdupBuf)(__ccgo_up(bp))).FzAlloc = _sqlite3DbMallocRawNN(tls, db, uint64(nAlloc))
		staticFlag = uint32(0)
	}
	pNew = (**(**TEdupBuf)(__ccgo_up(bp))).FzAlloc
	if pNew != 0 {
		/* Set nNewSize to the size allocated for the structure pointed to
		 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
		 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
		 ** by the copy of the p->u.zToken string (if any).
		 */
		nStructSize = uint32(_dupedExprStructSize(tls, p, dupFlags))
		nNewSize = int32(nStructSize & uint32(0xfff))
		if nToken < 0 {
			if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != libc.Uint32FromInt32(0)) && *(*uintptr)(unsafe.Pointer(p + 8)) != 0 {
				nToken = _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(p + 8))) + int32(1)
			} else {
				nToken = 0
			}
		}
		if dupFlags != 0 {
			libc.Xmemcpy(tls, (**(**TEdupBuf)(__ccgo_up(bp))).FzAlloc, p, uint32(nNewSize))
		} else {
			nSize = uint32(_exprStructSize(tls, p))
			libc.Xmemcpy(tls, (**(**TEdupBuf)(__ccgo_up(bp))).FzAlloc, p, nSize)
			if nSize < uint32(52) {
				libc.Xmemset(tls, (**(**TEdupBuf)(__ccgo_up(bp))).FzAlloc+uintptr(nSize), 0, uint32(52)-nSize)
			}
			nNewSize = int32(52)
		}
		/* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
		**(**Tu32)(__ccgo_up(pNew + 4)) &= uint32(^(libc.Int32FromInt32(EP_Reduced) | libc.Int32FromInt32(EP_TokenOnly) | libc.Int32FromInt32(EP_Static)))
		**(**Tu32)(__ccgo_up(pNew + 4)) |= nStructSize & uint32(libc.Int32FromInt32(EP_Reduced)|libc.Int32FromInt32(EP_TokenOnly))
		**(**Tu32)(__ccgo_up(pNew + 4)) |= staticFlag
		if dupFlags != 0 {
		}
		/* Copy the p->u.zToken string, if any. */
		if nToken > 0 {
			v1 = (**(**TEdupBuf)(__ccgo_up(bp))).FzAlloc + uintptr(nNewSize)
			*(*uintptr)(unsafe.Pointer(pNew + 8)) = v1
			zToken = v1
			libc.Xmemcpy(tls, zToken, *(*uintptr)(unsafe.Pointer(p + 8)), uint32(nToken))
			nNewSize = nNewSize + nToken
		}
		(**(**TEdupBuf)(__ccgo_up(bp))).FzAlloc += uintptr((nNewSize + libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7))
		if ((*TExpr)(unsafe.Pointer(p)).Fflags|(*TExpr)(unsafe.Pointer(pNew)).Fflags)&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Leaf)) == uint32(0) {
			/* Fill in the pNew->x.pSelect or pNew->x.pList member. */
			if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
				*(*uintptr)(unsafe.Pointer(pNew + 20)) = _sqlite3SelectDup(tls, db, *(*uintptr)(unsafe.Pointer(p + 20)), dupFlags)
			} else {
				if int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_ORDER) {
					v2 = dupFlags
				} else {
					v2 = 0
				}
				*(*uintptr)(unsafe.Pointer(pNew + 20)) = _sqlite3ExprListDup(tls, db, *(*uintptr)(unsafe.Pointer(p + 20)), v2)
			}
			if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
				*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pNew)).Fy)) = _sqlite3WindowDup(tls, db, pNew, *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)))
			}
			/* Fill in pNew->pLeft and pNew->pRight. */
			if dupFlags != 0 {
				if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_SELECT_COLUMN) {
					(*TExpr)(unsafe.Pointer(pNew)).FpLeft = (*TExpr)(unsafe.Pointer(p)).FpLeft
				} else {
					if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 {
						v1 = _exprDup(tls, db, (*TExpr)(unsafe.Pointer(p)).FpLeft, int32(EXPRDUP_REDUCE), bp)
					} else {
						v1 = uintptr(0)
					}
					(*TExpr)(unsafe.Pointer(pNew)).FpLeft = v1
				}
				if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 {
					v1 = _exprDup(tls, db, (*TExpr)(unsafe.Pointer(p)).FpRight, int32(EXPRDUP_REDUCE), bp)
				} else {
					v1 = uintptr(0)
				}
				(*TExpr)(unsafe.Pointer(pNew)).FpRight = v1
			} else {
				if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_SELECT_COLUMN) {
					(*TExpr)(unsafe.Pointer(pNew)).FpLeft = (*TExpr)(unsafe.Pointer(p)).FpLeft
				} else {
					(*TExpr)(unsafe.Pointer(pNew)).FpLeft = _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer(p)).FpLeft, 0)
				}
				(*TExpr)(unsafe.Pointer(pNew)).FpRight = _sqlite3ExprDup(tls, db, (*TExpr)(unsafe.Pointer(p)).FpRight, 0)
			}
		}
	}
	if pEdupBuf != 0 {
		libc.Xmemcpy(tls, pEdupBuf, bp, uint32(4))
	}
	return pNew
}

// C documentation
//
//	/*
//	** Argument pExpr is an (?, ?...) IN(...) expression. This
//	** function allocates and returns a nul-terminated string containing
//	** the affinities to be used for each column of the comparison.
//	**
//	** It is the responsibility of the caller to ensure that the returned
//	** string is eventually freed using sqlite3DbFree().
//	*/
func _exprINAffinity(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r uintptr) {
	var a int8
	var i, nVal int32
	var pA, pLeft, pSelect, zRet, v1 uintptr
	_, _, _, _, _, _, _, _ = a, i, nVal, pA, pLeft, pSelect, zRet, v1
	pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
	nVal = _sqlite3ExprVectorSize(tls, pLeft)
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
		v1 = *(*uintptr)(unsafe.Pointer(pExpr + 20))
	} else {
		v1 = uintptr(0)
	}
	pSelect = v1
	zRet = _sqlite3DbMallocRaw(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(int64(1)+int64(nVal)))
	if zRet != 0 {
		i = 0
		for {
			if !(i < nVal) {
				break
			}
			pA = _sqlite3VectorFieldSubexpr(tls, pLeft, i)
			a = _sqlite3ExprAffinity(tls, pA)
			if pSelect != 0 {
				**(**int8)(__ccgo_up(zRet + uintptr(i))) = _sqlite3CompareAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpEList + 8 + uintptr(i)*20))).FpExpr, a)
			} else {
				**(**int8)(__ccgo_up(zRet + uintptr(i))) = a
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		**(**int8)(__ccgo_up(zRet + uintptr(nVal))) = int8('\000')
	}
	return zRet
}

// C documentation
//
//	/*
//	** Check to see if there are references to columns in table
//	** pWalker->u.pIdxCover->iCur can be satisfied using the index
//	** pWalker->u.pIdxCover->pIdx.
//	*/
func _exprIdxCover(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TIdxCover)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 24)))).FiCur && _sqlite3TableColumnToIndex(tls, (*TIdxCover)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 24)))).FpIdx, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)) < 0 {
		(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1)
		return int32(WRC_Abort)
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Return non-zero if Expr p can only be true if pNN is not NULL.
//	**
//	** Or if seenNot is true, return non-zero if Expr p can only be
//	** non-NULL if pNN is not NULL
//	*/
func _exprImpliesNotNull(tls *libc.TLS, pParse uintptr, p uintptr, pNN uintptr, iTab int32, seenNot int32) (r int32) {
	var pList uintptr
	_ = pList
	if _sqlite3ExprCompare(tls, pParse, p, pNN, iTab) == 0 {
		return libc.BoolInt32(int32((*TExpr)(unsafe.Pointer(pNN)).Fop) != int32(TK_NULL))
	}
	switch int32((*TExpr)(unsafe.Pointer(p)).Fop) {
	case int32(TK_IN):
		if seenNot != 0 && (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_xIsSelect)) != uint32(0) {
			return 0
		}
		return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, int32(1))
	case int32(TK_BETWEEN):
		pList = *(*uintptr)(unsafe.Pointer(p + 20))
		if seenNot != 0 {
			return 0
		}
		if _exprImpliesNotNull(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr, pNN, iTab, int32(1)) != 0 || _exprImpliesNotNull(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr, pNN, iTab, int32(1)) != 0 {
			return int32(1)
		}
		return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, int32(1))
	case int32(TK_EQ):
		fallthrough
	case int32(TK_NE):
		fallthrough
	case int32(TK_LT):
		fallthrough
	case int32(TK_LE):
		fallthrough
	case int32(TK_GT):
		fallthrough
	case int32(TK_GE):
		fallthrough
	case int32(TK_PLUS):
		fallthrough
	case int32(TK_MINUS):
		fallthrough
	case int32(TK_BITOR):
		fallthrough
	case int32(TK_LSHIFT):
		fallthrough
	case int32(TK_RSHIFT):
		fallthrough
	case int32(TK_CONCAT):
		seenNot = int32(1)
		fallthrough
	case int32(TK_STAR):
		fallthrough
	case int32(TK_REM):
		fallthrough
	case int32(TK_BITAND):
		fallthrough
	case int32(TK_SLASH):
		if _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpRight, pNN, iTab, seenNot) != 0 {
			return int32(1)
		}
		fallthrough
	case int32(TK_SPAN):
		fallthrough
	case int32(TK_COLLATE):
		fallthrough
	case int32(TK_UPLUS):
		fallthrough
	case int32(TK_UMINUS):
		return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, seenNot)
	case int32(TK_TRUTH):
		if seenNot != 0 {
			return 0
		}
		if int32((*TExpr)(unsafe.Pointer(p)).Fop2) != int32(TK_IS) {
			return 0
		}
		return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, int32(1))
	case int32(TK_BITNOT):
		fallthrough
	case int32(TK_NOT):
		return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, int32(1))
	}
	return 0
}

func _exprIsConst(tls *libc.TLS, pParse uintptr, p uintptr, initFlag int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var _ /* w at bp+0 */ TWalker
	(**(**TWalker)(__ccgo_up(bp))).FeCode = uint16(initFlag)
	(**(**TWalker)(__ccgo_up(bp))).FpParse = pParse
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_exprNodeIsConstant)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkFail)
	_sqlite3WalkExpr(tls, bp, p)
	return int32((**(**TWalker)(__ccgo_up(bp))).FeCode)
}

// C documentation
//
//	/*
//	** pIdx is an index containing expressions.  Check it see if any of the
//	** expressions in the index match the pExpr expression.
//	*/
func _exprIsCoveredByIndex(tls *libc.TLS, pExpr uintptr, pIdx uintptr, iTabCur int32) (r int32) {
	var i int32
	_ = i
	i = 0
	for {
		if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) {
			break
		}
		if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) == -int32(2) && _sqlite3ExprCompare(tls, uintptr(0), pExpr, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*20))).FpExpr, iTabCur) == 0 {
			return int32(1)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** Return true if the expression contains no non-deterministic SQL
//	** functions. Do not consider non-deterministic SQL functions that are
//	** part of sub-select statements.
//	*/
func _exprIsDeterministic(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var _ /* w at bp+0 */ TWalker
	libc.Xmemset(tls, bp, 0, uint32(28))
	(**(**TWalker)(__ccgo_up(bp))).FeCode = uint16(1)
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_exprNodeIsDeterministic)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkFail)
	_sqlite3WalkExpr(tls, bp, p)
	return int32((**(**TWalker)(__ccgo_up(bp))).FeCode)
}

// C documentation
//
//	/*
//	** Append a copy of each expression in expression-list pAppend to
//	** expression list pList. Return a pointer to the result list.
//	*/
func _exprListAppendList(tls *libc.TLS, pParse uintptr, pList uintptr, pAppend uintptr, bIntToNull int32) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pDup, pSub uintptr
	var i, nInit, v1 int32
	var _ /* iDummy at bp+0 */ int32
	_, _, _, _, _, _ = db, i, nInit, pDup, pSub, v1
	if pAppend != 0 {
		if pList != 0 {
			v1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr
		} else {
			v1 = 0
		}
		nInit = v1
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pAppend)).FnExpr) {
				break
			}
			db = (*TParse)(unsafe.Pointer(pParse)).Fdb
			pDup = _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pAppend + 8 + uintptr(i)*20))).FpExpr, 0)
			if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				_sqlite3ExprDelete(tls, db, pDup)
				break
			}
			if bIntToNull != 0 {
				pSub = _sqlite3ExprSkipCollateAndLikely(tls, pDup)
				if _sqlite3ExprIsInteger(tls, pSub, bp, uintptr(0)) != 0 {
					(*TExpr)(unsafe.Pointer(pSub)).Fop = uint8(TK_NULL)
					**(**Tu32)(__ccgo_up(pSub + 4)) &= uint32(^(libc.Int32FromInt32(EP_IntValue) | libc.Int32FromInt32(EP_IsTrue) | libc.Int32FromInt32(EP_IsFalse)))
					*(*uintptr)(unsafe.Pointer(pSub + 8)) = uintptr(0)
				}
			}
			pList = _sqlite3ExprListAppend(tls, pParse, pList, pDup)
			if pList != 0 {
				(*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(nInit+i)*20))).Ffg.FsortFlags = (*(*TExprList_item)(unsafe.Pointer(pAppend + 8 + uintptr(i)*20))).Ffg.FsortFlags
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	return pList
}

func _exprMightBeIndexed(tls *libc.TLS, pFrom uintptr, aiCurCol uintptr, pExpr uintptr, op int32) (r int32) {
	var i int32
	var pIdx uintptr
	_, _ = i, pIdx
	/* If this expression is a vector to the left or right of a
	 ** inequality constraint (>, <, >= or <=), perform the processing
	 ** on the first element of the vector.  */
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_VECTOR) && (op >= int32(TK_GT) && op <= int32(TK_GE)) {
		pExpr = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) {
		**(**int32)(__ccgo_up(aiCurCol)) = (*TExpr)(unsafe.Pointer(pExpr)).FiTable
		**(**int32)(__ccgo_up(aiCurCol + 1*4)) = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
		return int32(1)
	}
	i = 0
	for {
		if !(i < (*TSrcList)(unsafe.Pointer(pFrom)).FnSrc) {
			break
		}
		pIdx = (*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pFrom + 8 + uintptr(i)*48))).FpSTab)).FpIndex
		for {
			if !(pIdx != 0) {
				break
			}
			if (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr != 0 {
				return _exprMightBeIndexed2(tls, pFrom, aiCurCol, pExpr, i)
			}
			goto _2
		_2:
			;
			pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** Expression pExpr is one operand of a comparison operator that might
//	** be useful for indexing.  This routine checks to see if pExpr appears
//	** in any index.  Return TRUE (1) if pExpr is an indexed term and return
//	** FALSE (0) if not.  If TRUE is returned, also set aiCurCol[0] to the cursor
//	** number of the table that is indexed and aiCurCol[1] to the column number
//	** of the column that is indexed, or XN_EXPR (-2) if an expression is being
//	** indexed.
//	**
//	** If pExpr is a TK_COLUMN column reference, then this routine always returns
//	** true even if that particular column is not indexed, because the column
//	** might be added to an automatic index later.
//	*/
func _exprMightBeIndexed2(tls *libc.TLS, pFrom uintptr, aiCurCol uintptr, pExpr uintptr, j int32) (r int32) {
	var i, iCur, v1 int32
	var pIdx uintptr
	_, _, _, _ = i, iCur, pIdx, v1
	for {
		iCur = (*(*TSrcItem)(unsafe.Pointer(pFrom + 8 + uintptr(j)*48))).FiCursor
		pIdx = (*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pFrom + 8 + uintptr(j)*48))).FpSTab)).FpIndex
		for {
			if !(pIdx != 0) {
				break
			}
			if (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr == uintptr(0) {
				goto _3
			}
			i = 0
			for {
				if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
					break
				}
				if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) != -int32(2) {
					goto _4
				}
				if _sqlite3ExprCompareSkip(tls, pExpr, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*20))).FpExpr, iCur) == 0 && !(_sqlite3ExprIsConstant(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*20))).FpExpr) != 0) {
					**(**int32)(__ccgo_up(aiCurCol)) = iCur
					**(**int32)(__ccgo_up(aiCurCol + 1*4)) = -int32(2)
					return int32(1)
				}
				goto _4
			_4:
				;
				i = i + 1
			}
			goto _3
		_3:
			;
			pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		}
		goto _2
	_2:
		;
		j = j + 1
		v1 = j
		if !(v1 < (*TSrcList)(unsafe.Pointer(pFrom)).FnSrc) {
			break
		}
	}
	return 0
}

// C documentation
//
//	/*
//	** Expression Node callback for sqlite3ExprCanReturnSubtype().  If
//	** pExpr is able to return a subtype, set pWalker->eCode and abort
//	** the search.  If pExpr can never return a subtype, prune search.
//	**
//	** The only expressions that can return a subtype are:
//	**
//	**    1.  A function
//	**    2.  The no-op "+" operator
//	**    3.  A CASE...END expression
//	**    4.  A CAST() expression
//	**    5.  A "expr COLLATE colseq" expression.
//	**
//	** For any other kind of expression, prune the search.
//	**
//	** For case 1, the expression can yield a subtype if the function has
//	** the SQLITE_RESULT_SUBTYPE property.  Functions can also return
//	** a subtype (via sqlite3_result_value()) if any of the arguments can
//	** return a subtype.
//	**
//	** In all cases 1 through 5, the expression might also return a subtype
//	** if any operand can return a subtype.
//	*/
func _exprNodeCanReturnSubtype(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var db, pDef uintptr
	var n, v1 int32
	_, _, _, _ = db, n, pDef, v1
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_CASE) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_UPLUS) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_CAST) {
		return WRC_Continue
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_FUNCTION) {
		return int32(WRC_Prune)
	}
	db = (*TParse)(unsafe.Pointer((*TWalker)(unsafe.Pointer(pWalker)).FpParse)).Fdb
	if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0 {
		v1 = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr
	} else {
		v1 = 0
	}
	n = v1
	pDef = _sqlite3FindFunction(tls, db, *(*uintptr)(unsafe.Pointer(pExpr + 8)), n, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uint8(0))
	if pDef == uintptr(0) || (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_RESULT_SUBTYPE) != uint32(0) {
		(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1)
		return int32(WRC_Abort)
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** These routines are Walker callbacks used to check expressions to
//	** see if they are "constant" for some definition of constant.  The
//	** Walker.eCode value determines the type of "constant" we are looking
//	** for.
//	**
//	** These callback routines are used to implement the following:
//	**
//	**     sqlite3ExprIsConstant()                  pWalker->eCode==1
//	**     sqlite3ExprIsConstantNotJoin()           pWalker->eCode==2
//	**     sqlite3ExprIsTableConstant()             pWalker->eCode==3
//	**     sqlite3ExprIsConstantOrFunction()        pWalker->eCode==4 or 5
//	**
//	** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
//	** is found to not be a constant.
//	**
//	** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT
//	** expressions in a CREATE TABLE statement.  The Walker.eCode value is 5
//	** when parsing an existing schema out of the sqlite_schema table and 4
//	** when processing a new CREATE TABLE statement.  A bound parameter raises
//	** an error for new statements, but is silently converted
//	** to NULL for existing schemas.  This allows sqlite_schema tables that
//	** contain a bound parameter because they were generated by older versions
//	** of SQLite to be parsed by newer versions of SQLite without raising a
//	** malformed schema error.
//	*/
func _exprNodeIsConstant(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	/* If pWalker->eCode is 2 then any term of the expression that comes from
	 ** the ON or USING clauses of an outer join disqualifies the expression
	 ** from being considered constant. */
	if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(2) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) {
		(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0)
		return int32(WRC_Abort)
	}
	switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) {
	/* Consider functions to be constant if all their arguments are constant
	 ** and either pWalker->eCode==4 or 5 or the function has the
	 ** SQLITE_FUNC_CONST flag. */
	case int32(TK_FUNCTION):
		if (int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) >= int32(4) || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_ConstFunc)) != uint32(0)) && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != libc.Uint32FromInt32(0)) {
			if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(5) {
				**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_FromDDL))
			}
			return WRC_Continue
		} else {
			if (*TWalker)(unsafe.Pointer(pWalker)).FpParse != 0 {
				return _exprNodeIsConstantFunction(tls, pWalker, pExpr)
			} else {
				(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0)
				return int32(WRC_Abort)
			}
		}
		fallthrough
	case int32(TK_ID):
		/* Convert "true" or "false" in a DEFAULT clause into the
		 ** appropriate TK_TRUEFALSE operator */
		if _sqlite3ExprIdToTrueFalse(tls, pExpr) != 0 {
			return int32(WRC_Prune)
		}
		fallthrough
	case int32(TK_COLUMN):
		fallthrough
	case int32(TK_AGG_FUNCTION):
		fallthrough
	case int32(TK_AGG_COLUMN):
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != uint32(0) && int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) != int32(2) {
			return WRC_Continue
		}
		if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(3) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == *(*int32)(unsafe.Pointer(pWalker + 24)) {
			return WRC_Continue
		}
		fallthrough
	case int32(TK_IF_NULL_ROW):
		fallthrough
	case int32(TK_REGISTER):
		fallthrough
	case int32(TK_DOT):
		fallthrough
	case int32(TK_RAISE):
		(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0)
		return int32(WRC_Abort)
	case int32(TK_VARIABLE):
		if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(5) {
			/* Silently convert bound parameters that appear inside of CREATE
			 ** statements into a NULL when parsing the CREATE statement text out
			 ** of the sqlite_schema table */
			(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_NULL)
		} else {
			if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == int32(4) {
				/* A bound parameter in a CREATE statement that originates from
				 ** sqlite3_prepare() causes an error */
				(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0)
				return int32(WRC_Abort)
			}
		}
		fallthrough
	default:
		/* sqlite3SelectWalkFail() disallows */
		/* sqlite3SelectWalkFail() disallows */
		return WRC_Continue
	}
	return r
}

// C documentation
//
//	/*
//	** pExpr is a TK_FUNCTION node.  Try to determine whether or not the
//	** function is a constant function.  A function is constant if all of
//	** the following are true:
//	**
//	**    (1)  It is a scalar function (not an aggregate or window function)
//	**    (2)  It has either the SQLITE_FUNC_CONSTANT or SQLITE_FUNC_SLOCHNG
//	**         property.
//	**    (3)  All of its arguments are constants
//	**
//	** This routine sets pWalker->eCode to 0 if pExpr is not a constant.
//	** It makes no changes to pWalker->eCode if pExpr is constant.  In
//	** every case, it returns WRC_Abort.
//	**
//	** Called as a service subroutine from exprNodeIsConstant().
//	*/
func _exprNodeIsConstantFunction(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var db, pDef, pList, v1 uintptr
	var n int32
	var v2 bool
	_, _, _, _, _, _ = db, n, pDef, pList, v1, v2 /* The database */
	if v2 = (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)) != uint32(0); !v2 {
		v1 = *(*uintptr)(unsafe.Pointer(pExpr + 20))
		pList = v1
	}
	if v2 || v1 == uintptr(0) {
		n = 0
	} else {
		n = (*TExprList)(unsafe.Pointer(pList)).FnExpr
		_sqlite3WalkExprList(tls, pWalker, pList)
		if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == 0 {
			return int32(WRC_Abort)
		}
	}
	db = (*TParse)(unsafe.Pointer((*TWalker)(unsafe.Pointer(pWalker)).FpParse)).Fdb
	pDef = _sqlite3FindFunction(tls, db, *(*uintptr)(unsafe.Pointer(pExpr + 8)), n, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uint8(0))
	if pDef == uintptr(0) || (*TFuncDef)(unsafe.Pointer(pDef)).FxFinalize != uintptr(0) || (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)|libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG)) == uint32(0) || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
		(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0)
		return int32(WRC_Abort)
	}
	return int32(WRC_Prune)
}

// C documentation
//
//	/*
//	** Callback for estLikePatternLength().
//	**
//	** If this node is a string literal that is longer pWalker->sz, then set
//	** pWalker->sz to the byte length of that string literal.
//	**
//	** pWalker->eCode indicates how to count characters:
//	**
//	**    eCode==0     Count as a GLOB pattern
//	**    eCode==1     Count as a LIKE pattern
//	*/
func _exprNodePatternLengthEst(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var c, c1, c2, c3, v1 Tu8
	var sz int32
	var z, v2 uintptr
	_, _, _, _, _, _, _, _ = c, c1, c2, c3, sz, z, v1, v2
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_STRING) {
		sz = 0                                     /* Pattern size in bytes */
		z = *(*uintptr)(unsafe.Pointer(pExpr + 8)) /* Wildcards */
		if (*TWalker)(unsafe.Pointer(pWalker)).FeCode != 0 {
			c1 = uint8('%')
			c2 = uint8('_')
			c3 = uint8(0)
		} else {
			c1 = uint8('*')
			c2 = uint8('?')
			c3 = uint8('[')
		}
		for {
			v2 = z
			z = z + 1
			v1 = **(**Tu8)(__ccgo_up(v2))
			c = v1
			if !(int32(v1) != 0) {
				break
			}
			if int32(c) == int32(c3) {
				if **(**Tu8)(__ccgo_up(z)) != 0 {
					z = z + 1
				}
				for **(**Tu8)(__ccgo_up(z)) != 0 && int32(**(**Tu8)(__ccgo_up(z))) != int32(']') {
					z = z + 1
				}
			} else {
				if int32(c) != int32(c1) && int32(c) != int32(c2) {
					sz = sz + 1
				}
			}
		}
		if sz > *(*int32)(unsafe.Pointer(pWalker + 24)) {
			*(*int32)(unsafe.Pointer(pWalker + 24)) = sz
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/* This is the Walker EXPR callback for sqlite3ReferencesSrcList().
//	**
//	** Set the 0x01 bit of pWalker->eCode if there is a reference to any
//	** of the tables shown in RefSrcList.pRef.
//	**
//	** Set the 0x02 bit of pWalker->eCode if there is a reference to a
//	** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude.
//	*/
func _exprRefToSrcList(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var i, nSrc, v1 int32
	var p, pSrc, v3 uintptr
	_, _, _, _, _, _ = i, nSrc, p, pSrc, v1, v3
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_COLUMN) {
		p = *(*uintptr)(unsafe.Pointer(pWalker + 24))
		pSrc = (*TRefSrcList)(unsafe.Pointer(p)).FpRef
		if pSrc != 0 {
			v1 = (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc
		} else {
			v1 = 0
		}
		nSrc = v1
		i = 0
		for {
			if !(i < nSrc) {
				break
			}
			if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48))).FiCursor {
				v3 = pWalker + 20
				*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | libc.Int32FromInt32(1))
				return WRC_Continue
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		i = 0
		for {
			if !(int64(i) < (*TRefSrcList)(unsafe.Pointer(p)).FnExclude && **(**int32)(__ccgo_up((*TRefSrcList)(unsafe.Pointer(p)).FaiExclude + uintptr(i)*4)) != (*TExpr)(unsafe.Pointer(pExpr)).FiTable) {
				break
			}
			goto _4
		_4:
			;
			i = i + 1
		}
		if int64(i) >= (*TRefSrcList)(unsafe.Pointer(p)).FnExclude {
			v3 = pWalker + 20
			*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | libc.Int32FromInt32(2))
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Set the Expr.nHeight variable in the structure passed as an
//	** argument. An expression with no children, Expr.pList or
//	** Expr.pSelect member has a height of 1. Any other expression
//	** has a height equal to the maximum height of any other
//	** referenced Expr plus one.
//	**
//	** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
//	** if appropriate.
//	*/
func _exprSetHeight(tls *libc.TLS, p uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var v1 int32
	var _ /* nHeight at bp+0 */ int32
	_ = v1
	if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 {
		v1 = (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpLeft)).FnHeight
	} else {
		v1 = 0
	}
	**(**int32)(__ccgo_up(bp)) = v1
	if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 && (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpRight)).FnHeight > **(**int32)(__ccgo_up(bp)) {
		**(**int32)(__ccgo_up(bp)) = (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpRight)).FnHeight
	}
	if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
		_heightOfSelect(tls, *(*uintptr)(unsafe.Pointer(p + 20)), bp)
	} else {
		if *(*uintptr)(unsafe.Pointer(p + 20)) != 0 {
			_heightOfExprList(tls, *(*uintptr)(unsafe.Pointer(p + 20)), bp)
			**(**Tu32)(__ccgo_up(p + 4)) |= uint32(libc.Int32FromInt32(EP_Collate)|libc.Int32FromInt32(EP_Subquery)|libc.Int32FromInt32(EP_HasFunc)) & _sqlite3ExprListFlags(tls, *(*uintptr)(unsafe.Pointer(p + 20)))
		}
	}
	(*TExpr)(unsafe.Pointer(p)).FnHeight = **(**int32)(__ccgo_up(bp)) + int32(1)
}

// C documentation
//
//	/*
//	** Return the number of bytes allocated for the expression structure
//	** passed as the first argument. This is always one of EXPR_FULLSIZE,
//	** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
//	*/
func _exprStructSize(tls *libc.TLS, p uintptr) (r int32) {
	if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)) != uint32(0) {
		return int32(uint32(libc.UintptrFromInt32(0) + 12))
	}
	if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Reduced)) != uint32(0) {
		return int32(uint32(libc.UintptrFromInt32(0) + 28))
	}
	return int32(52)
}

// C documentation
//
//	/*
//	** Argument pVector points to a vector expression - either a TK_VECTOR
//	** or TK_SELECT that returns more than one column. This function returns
//	** the register number of a register that contains the value of
//	** element iField of the vector.
//	**
//	** If pVector is a TK_SELECT expression, then code for it must have
//	** already been generated using the exprCodeSubselect() routine. In this
//	** case parameter regSelect should be the first in an array of registers
//	** containing the results of the sub-select.
//	**
//	** If pVector is of type TK_VECTOR, then code for the requested field
//	** is generated. In this case (*pRegFree) may be set to the number of
//	** a temporary register to be freed by the caller before returning.
//	**
//	** Before returning, output parameter (*ppExpr) is set to point to the
//	** Expr object corresponding to element iElem of the vector.
//	*/
func _exprVectorRegister(tls *libc.TLS, pParse uintptr, pVector uintptr, iField int32, regSelect int32, ppExpr uintptr, pRegFree uintptr) (r int32) {
	var op Tu8
	_ = op
	op = (*TExpr)(unsafe.Pointer(pVector)).Fop
	if int32(op) == int32(TK_REGISTER) {
		**(**uintptr)(__ccgo_up(ppExpr)) = _sqlite3VectorFieldSubexpr(tls, pVector, iField)
		return (*TExpr)(unsafe.Pointer(pVector)).FiTable + iField
	}
	if int32(op) == int32(TK_SELECT) {
		**(**uintptr)(__ccgo_up(ppExpr)) = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 20)))).FpEList + 8 + uintptr(iField)*20))).FpExpr
		return regSelect + iField
	}
	if int32(op) == int32(TK_VECTOR) {
		**(**uintptr)(__ccgo_up(ppExpr)) = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 20)) + 8 + uintptr(iField)*20))).FpExpr
		return _sqlite3ExprCodeTemp(tls, pParse, **(**uintptr)(__ccgo_up(ppExpr)), pRegFree)
	}
	return 0
}

// C documentation
//
//	/*
//	** Create a new expression term for the column specified by pMatch and
//	** iColumn.  Append this new expression term to the FULL JOIN Match set
//	** in *ppList.  Create a new *ppList if this is the first term in the
//	** set.
//	*/
func _extendFJMatch(tls *libc.TLS, pParse uintptr, ppList uintptr, pMatch uintptr, iColumn Ti16) {
	var pNew uintptr
	_ = pNew
	pNew = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_COLUMN), uintptr(0), 0)
	if pNew != 0 {
		(*TExpr)(unsafe.Pointer(pNew)).FiTable = (*TSrcItem)(unsafe.Pointer(pMatch)).FiCursor
		(*TExpr)(unsafe.Pointer(pNew)).FiColumn = iColumn
		(*TExpr)(unsafe.Pointer(pNew)).Fy.FpTab = (*TSrcItem)(unsafe.Pointer(pMatch)).FpSTab
		**(**Tu32)(__ccgo_up(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_CanBeNull))
		**(**uintptr)(__ccgo_up(ppList)) = _sqlite3ExprListAppend(tls, pParse, **(**uintptr)(__ccgo_up(ppList)), pNew)
	}
}

// C documentation
//
//	/*
//	** Return a pointer to payload information from the entry that the
//	** pCur cursor is pointing to.  The pointer is to the beginning of
//	** the key if index btrees (pPage->intKey==0) and is the data for
//	** table btrees (pPage->intKey==1). The number of bytes of available
//	** key/data is written into *pAmt.  If *pAmt==0, then the value
//	** returned will not be a valid pointer.
//	**
//	** This routine is an optimization.  It is common for the entire key
//	** and data to fit on the local page and for there to be no overflow
//	** pages.  When that is so, this routine can be used to access the
//	** key and data without making a copy.  If the key and/or data spills
//	** onto overflow pages, then accessPayload() must be used to reassemble
//	** the key/data and copy it into a preallocated buffer.
//	**
//	** The pointer returned by this routine looks directly into the cached
//	** page of the database.  The data might change or move the next time
//	** any btree routine is called.
//	*/
func _fetchPayload(tls *libc.TLS, pCur uintptr, pAmt uintptr) (r uintptr) {
	var amt, v1 int32
	_, _ = amt, v1
	amt = int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal)
	if amt > int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaDataEnd)-int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload) {
		/* There is too little space on the page for the expected amount
		 ** of local content. Database must be corrupt. */
		if 0 > int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaDataEnd)-int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload) {
			v1 = 0
		} else {
			v1 = int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaDataEnd) - int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload)
		}
		amt = v1
	}
	**(**Tu32)(__ccgo_up(pAmt)) = uint32(amt)
	return (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload
}

// C documentation
//
//	/*
//	** Create the byte sequence used to represent a cell on page pPage
//	** and write that byte sequence into pCell[].  Overflow pages are
//	** allocated and filled in as necessary.  The calling procedure
//	** is responsible for making sure sufficient space has been allocated
//	** for pCell[].
//	**
//	** Note that pCell does not necessary need to point to the pPage->aData
//	** area.  pCell might point to some temporary storage.  The cell will
//	** be constructed in this temporary area then copied into pPage->aData
//	** later.
//	*/
func _fillInCell(tls *libc.TLS, pPage uintptr, pCell uintptr, pX uintptr, pnSize uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var eType Tu8
	var mn, n, nHeader, nPayload, nSrc, spaceLeft, v1 int32
	var pBt, pPayload, pPrior, pSrc, pToRelease uintptr
	var pgnoPtrmap TPgno
	var _ /* pOvfl at bp+8 */ uintptr
	var _ /* pgnoOvfl at bp+4 */ TPgno
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = eType, mn, n, nHeader, nPayload, nSrc, pBt, pPayload, pPrior, pSrc, pToRelease, pgnoPtrmap, spaceLeft, v1
	/* pPage is not necessarily writeable since pCell might be auxiliary
	 ** buffer space that is separate from the pPage buffer area */
	/* Fill in the header. */
	nHeader = int32((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize)
	if (*TMemPage)(unsafe.Pointer(pPage)).FintKey != 0 {
		nPayload = (*TBtreePayload)(unsafe.Pointer(pX)).FnData + (*TBtreePayload)(unsafe.Pointer(pX)).FnZero
		pSrc = (*TBtreePayload)(unsafe.Pointer(pX)).FpData
		nSrc = (*TBtreePayload)(unsafe.Pointer(pX)).FnData
		/* fillInCell() only called for leaves */
		if uint32(nPayload) < libc.Uint32FromInt32(0x80) {
			**(**uint8)(__ccgo_up(pCell + uintptr(nHeader))) = uint8(nPayload)
			v1 = libc.Int32FromInt32(1)
		} else {
			v1 = _sqlite3PutVarint(tls, pCell+uintptr(nHeader), uint64(nPayload))
		}
		nHeader = nHeader + int32(uint8(v1))
		nHeader = nHeader + _sqlite3PutVarint(tls, pCell+uintptr(nHeader), **(**Tu64)(__ccgo_up(pX + 8)))
	} else {
		v1 = int32((*TBtreePayload)(unsafe.Pointer(pX)).FnKey)
		nPayload = v1
		nSrc = v1
		pSrc = (*TBtreePayload)(unsafe.Pointer(pX)).FpKey
		if uint32(nPayload) < libc.Uint32FromInt32(0x80) {
			**(**uint8)(__ccgo_up(pCell + uintptr(nHeader))) = uint8(nPayload)
			v1 = libc.Int32FromInt32(1)
		} else {
			v1 = _sqlite3PutVarint(tls, pCell+uintptr(nHeader), uint64(nPayload))
		}
		nHeader = nHeader + int32(uint8(v1))
	}
	/* Fill in the payload */
	pPayload = pCell + uintptr(nHeader)
	if nPayload <= int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
		/* This is the common case where everything fits on the btree page
		 ** and no overflow pages are required. */
		n = nHeader + nPayload
		if n < int32(4) {
			n = int32(4)
			**(**uint8)(__ccgo_up(pPayload + uintptr(nPayload))) = uint8(0)
		}
		**(**int32)(__ccgo_up(pnSize)) = n
		libc.Xmemcpy(tls, pPayload, pSrc, uint32(nSrc))
		libc.Xmemset(tls, pPayload+uintptr(nSrc), 0, uint32(nPayload-nSrc))
		return SQLITE_OK
	}
	/* If we reach this point, it means that some of the content will need
	 ** to spill onto overflow pages.
	 */
	mn = int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal)
	n = int32(uint32(mn) + uint32(nPayload-mn)%((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize-uint32(4)))
	if n > int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
		n = mn
	}
	spaceLeft = n
	**(**int32)(__ccgo_up(pnSize)) = n + nHeader + int32(4)
	pPrior = pCell + uintptr(nHeader+n)
	pToRelease = uintptr(0)
	**(**TPgno)(__ccgo_up(bp + 4)) = uint32(0)
	pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt
	/* At this point variables should be set as follows:
	 **
	 **   nPayload           Total payload size in bytes
	 **   pPayload           Begin writing payload here
	 **   spaceLeft          Space available at pPayload.  If nPayload>spaceLeft,
	 **                      that means content must spill into overflow pages.
	 **   *pnSize            Size of the local cell (not counting overflow pages)
	 **   pPrior             Where to write the pgno of the first overflow page
	 **
	 ** Use a call to btreeParseCellPtr() to verify that the values above
	 ** were computed correctly.
	 */
	/* Write the payload into the local Cell and any extra into overflow pages */
	for int32(1) != 0 {
		n = nPayload
		if n > spaceLeft {
			n = spaceLeft
		}
		/* If pToRelease is not zero than pPayload points into the data area
		 ** of pToRelease.  Make sure pToRelease is still writeable. */
		/* If pPayload is part of the data area of pPage, then make sure pPage
		 ** is still writeable */
		if nSrc >= n {
			libc.Xmemcpy(tls, pPayload, pSrc, uint32(n))
		} else {
			if nSrc > 0 {
				n = nSrc
				libc.Xmemcpy(tls, pPayload, pSrc, uint32(n))
			} else {
				libc.Xmemset(tls, pPayload, 0, uint32(n))
			}
		}
		nPayload = nPayload - n
		if nPayload <= 0 {
			break
		}
		pPayload = pPayload + uintptr(n)
		pSrc = pSrc + uintptr(n)
		nSrc = nSrc - n
		spaceLeft = spaceLeft - n
		if spaceLeft == 0 {
			**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
			pgnoPtrmap = **(**TPgno)(__ccgo_up(bp + 4)) /* Overflow page pointer-map entry page */
			if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
				for cond := true; cond; cond = _ptrmapPageno(tls, pBt, **(**TPgno)(__ccgo_up(bp + 4))) == **(**TPgno)(__ccgo_up(bp + 4)) || **(**TPgno)(__ccgo_up(bp + 4)) == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) {
					**(**TPgno)(__ccgo_up(bp + 4)) = **(**TPgno)(__ccgo_up(bp + 4)) + 1
				}
			}
			**(**int32)(__ccgo_up(bp)) = _allocateBtreePage(tls, pBt, bp+8, bp+4, **(**TPgno)(__ccgo_up(bp + 4)), uint8(0))
			/* If the database supports auto-vacuum, and the second or subsequent
			 ** overflow page is being allocated, add an entry to the pointer-map
			 ** for that page now.
			 **
			 ** If this is the first overflow page, then write a partial entry
			 ** to the pointer-map. If we write nothing to this pointer-map slot,
			 ** then the optimistic overflow chain processing in clearCell()
			 ** may misinterpret the uninitialized values and delete the
			 ** wrong pages from the database.
			 */
			if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				if pgnoPtrmap != 0 {
					v1 = int32(PTRMAP_OVERFLOW2)
				} else {
					v1 = int32(PTRMAP_OVERFLOW1)
				}
				eType = uint8(v1)
				_ptrmapPut(tls, pBt, **(**TPgno)(__ccgo_up(bp + 4)), eType, pgnoPtrmap, bp)
				if **(**int32)(__ccgo_up(bp)) != 0 {
					_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 8)))
				}
			}
			if **(**int32)(__ccgo_up(bp)) != 0 {
				_releasePage(tls, pToRelease)
				return **(**int32)(__ccgo_up(bp))
			}
			/* If pToRelease is not zero than pPrior points into the data area
			 ** of pToRelease.  Make sure pToRelease is still writeable. */
			/* If pPrior is part of the data area of pPage, then make sure pPage
			 ** is still writeable */
			_sqlite3Put4byte(tls, pPrior, **(**TPgno)(__ccgo_up(bp + 4)))
			_releasePage(tls, pToRelease)
			pToRelease = **(**uintptr)(__ccgo_up(bp + 8))
			pPrior = (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FaData
			_sqlite3Put4byte(tls, pPrior, uint32(0))
			pPayload = (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 8)))).FaData + 4
			spaceLeft = int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(4))
		}
	}
	_releasePage(tls, pToRelease)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
//	** with pOp->p3.  Return the hash.
//	*/
func _filterHash(tls *libc.TLS, aMem uintptr, pOp uintptr) (r Tu64) {
	var h Tu64
	var i, mx int32
	var p uintptr
	_, _, _, _ = h, i, mx, p
	h = uint64(0)
	i = (*TOp)(unsafe.Pointer(pOp)).Fp3
	mx = i + *(*int32)(unsafe.Pointer(pOp + 16))
	for {
		if !(i < mx) {
			break
		}
		p = aMem + uintptr(i)*40
		if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
			h = h + uint64(*(*Ti64)(unsafe.Pointer(p)))
		} else {
			if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Real) != 0 {
				h = h + uint64(_sqlite3VdbeIntValue(tls, p))
			} else {
				if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 {
					/* All strings have the same hash and all blobs have the same hash,
					 ** though, at least, those hashes are different from each other and
					 ** from NULL. */
					h = h + uint64(int32(4093)+int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)))
				}
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return h
}

// C documentation
//
//	/*
//	** This routine is called right after An OP_Filter has been generated and
//	** before the corresponding index search has been performed.  This routine
//	** checks to see if there are additional Bloom filters in inner loops that
//	** can be checked prior to doing the index lookup.  If there are available
//	** inner-loop Bloom filters, then evaluate those filters now, before the
//	** index lookup.  The idea is that a Bloom filter check is way faster than
//	** an index lookup, and the Bloom filter might return false, meaning that
//	** the index lookup can be skipped.
//	**
//	** We know that an inner loop uses a Bloom filter because it has the
//	** WhereLevel.regFilter set.  If an inner-loop Bloom filter is checked,
//	** then clear the WhereLevel.regFilter value to prevent the Bloom filter
//	** from being checked a second time when the inner loop is evaluated.
//	*/
func _filterPullDown(tls *libc.TLS, pParse uintptr, pWInfo uintptr, iLevel int32, addrNxt int32, notReady TBitmask) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nEq Tu16
	var pLevel, pLoop, pTerm uintptr
	var r1, regRowid, saved_addrBrk, v1 int32
	var _ /* zStartAff at bp+0 */ uintptr
	_, _, _, _, _, _, _, _ = nEq, pLevel, pLoop, pTerm, r1, regRowid, saved_addrBrk, v1
	for {
		iLevel = iLevel + 1
		v1 = iLevel
		if !(v1 < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) {
			break
		}
		pLevel = pWInfo + 760 + uintptr(iLevel)*88
		pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
		if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter == 0 {
			continue
		}
		if (*TWhereLoop)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop)).FnSkip != 0 {
			continue
		}
		/*         ,--- Because sqlite3ConstructBloomFilter() has will not have set
		 **  vvvvv--'    pLevel->regFilter if this were true. */
		if (*TWhereLoop)(unsafe.Pointer(pLoop)).Fprereq&notReady != 0 {
			continue
		}
		saved_addrBrk = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk = addrNxt
		if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != 0 {
			pTerm = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm))
			regRowid = _sqlite3GetTempReg(tls, pParse)
			regRowid = _codeEqualityTerm(tls, pParse, pTerm, pLevel, 0, 0, regRowid)
			_sqlite3VdbeAddOp2(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_MustBeInt), regRowid, addrNxt)
			_sqlite3VdbeAddOp4Int(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Filter), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, addrNxt, regRowid, int32(1))
		} else {
			nEq = (*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FnEq
			r1 = _codeAllEqualityTerms(tls, pParse, pLevel, 0, 0, bp)
			_codeApplyAffinity(tls, pParse, r1, int32(nEq), **(**uintptr)(__ccgo_up(bp)))
			_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, **(**uintptr)(__ccgo_up(bp)))
			_sqlite3VdbeAddOp4Int(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Filter), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, addrNxt, r1, int32(nEq))
		}
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter = 0
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk = saved_addrBrk
	}
}

// C documentation
//
//	/*
//	** Invoke the OP_AggFinalize opcode for every aggregate function
//	** in the AggInfo structure.
//	*/
func _finalizeAggFunctions(tls *libc.TLS, pParse uintptr, pAggInfo uintptr) {
	var i, iBaseCol, iTop, j, nArg, nKey, regAgg, regSubtype, v4 int32
	var pF, pList, v uintptr
	_, _, _, _, _, _, _, _, _, _, _, _ = i, iBaseCol, iTop, j, nArg, nKey, pF, pList, regAgg, regSubtype, v, v4
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	i = 0
	pF = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc
	for {
		if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) {
			break
		}
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			return
		}
		pList = *(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr + 20))
		if (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab >= 0 { /* Loop counter */
			nArg = (*TExprList)(unsafe.Pointer(pList)).FnExpr
			regAgg = _sqlite3GetTempRange(tls, pParse, nArg)
			if int32((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload) == 0 {
				nKey = 0
			} else {
				nKey = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).FpLeft + 20)))).FnExpr
				if !((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBUnique != 0) {
					nKey = nKey + 1
				}
			}
			iTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab)
			j = nArg - int32(1)
			for {
				if !(j >= 0) {
					break
				}
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, nKey+j, regAgg+j)
				goto _2
			_2:
				;
				j = j - 1
			}
			if (*TAggInfo_func)(unsafe.Pointer(pF)).FbUseSubtype != 0 {
				regSubtype = _sqlite3GetTempReg(tls, pParse)
				iBaseCol = nKey + nArg + libc.BoolInt32(int32((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload) == 0 && int32((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBUnique) == 0)
				j = nArg - int32(1)
				for {
					if !(j >= 0) {
						break
					}
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, iBaseCol+j, regSubtype)
					_sqlite3VdbeAddOp2(tls, v, int32(OP_SetSubtype), regSubtype, regAgg+j)
					goto _3
				_3:
					;
					j = j - 1
				}
				_sqlite3ReleaseTempReg(tls, pParse, regSubtype)
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_AggStep), 0, regAgg, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn+i)
			_sqlite3VdbeAppendP4(tls, v, (*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc, -int32(8))
			_sqlite3VdbeChangeP5(tls, v, uint16(nArg))
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, iTop+int32(1))
			_sqlite3VdbeJumpHere(tls, v, iTop)
			_sqlite3ReleaseTempRange(tls, pParse, regAgg, nArg)
		}
		if pList != 0 {
			v4 = (*TExprList)(unsafe.Pointer(pList)).FnExpr
		} else {
			v4 = 0
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_AggFinal), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn+i, v4)
		_sqlite3VdbeAppendP4(tls, v, (*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc, -int32(8))
		goto _1
	_1:
		;
		i = i + 1
		pF += 24
	}
}

// C documentation
//
//	/*
//	** Return a pointer corresponding to database zDb (i.e. "main", "temp")
//	** in connection handle pDb. If such a database cannot be found, return
//	** a NULL pointer and write an error message to pErrorDb.
//	**
//	** If the "temp" database is requested, it may need to be opened by this
//	** function. If an error occurs while doing so, return 0 and write an
//	** error message to pErrorDb.
//	*/
func _findBtree(tls *libc.TLS, pErrorDb uintptr, pDb uintptr, zDb uintptr) (r uintptr) {
	bp := tls.Alloc(304)
	defer tls.Free(304)
	var i, rc int32
	var _ /* sParse at bp+0 */ TParse
	_, _ = i, rc
	i = _sqlite3FindDbName(tls, pDb, zDb)
	if i == int32(1) {
		rc = 0
		_sqlite3ParseObjectInit(tls, bp, pDb)
		if _sqlite3OpenTempDatabase(tls, bp) != 0 {
			_sqlite3ErrorWithMsg(tls, pErrorDb, (**(**TParse)(__ccgo_up(bp))).Frc, __ccgo_ts+4729, libc.VaList(bp+296, (**(**TParse)(__ccgo_up(bp))).FzErrMsg))
			rc = int32(SQLITE_ERROR)
		}
		_sqlite3DbFree(tls, pErrorDb, (**(**TParse)(__ccgo_up(bp))).FzErrMsg)
		_sqlite3ParseObjectReset(tls, bp)
		if rc != 0 {
			return uintptr(0)
		}
	}
	if i < 0 {
		_sqlite3ErrorWithMsg(tls, pErrorDb, int32(SQLITE_ERROR), __ccgo_ts+6342, libc.VaList(bp+296, zDb))
		return uintptr(0)
	}
	return (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(pDb)).FaDb + uintptr(i)*16))).FpBt
}

// C documentation
//
//	/*
//	** Locate and return an entry from the db.aCollSeq hash table. If the entry
//	** specified by zName and nName is not found and parameter 'create' is
//	** true, then create a new entry. Otherwise return NULL.
//	**
//	** Each pointer stored in the sqlite3.aCollSeq hash table contains an
//	** array of three CollSeq structures. The first is the collation sequence
//	** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
//	**
//	** Stored immediately after the three collation sequences is a copy of
//	** the collation sequence name. A pointer to this string is stored in
//	** each collation sequence structure.
//	*/
func _findCollSeqEntry(tls *libc.TLS, db uintptr, zName uintptr, create int32) (r uintptr) {
	var nName int32
	var pColl, pDel uintptr
	_, _, _ = nName, pColl, pDel
	pColl = _sqlite3HashFind(tls, db+448, zName)
	if uintptr(0) == pColl && create != 0 {
		nName = _sqlite3Strlen30(tls, zName) + int32(1)
		pColl = _sqlite3DbMallocZero(tls, db, uint64(libc.Uint32FromInt32(3)*libc.Uint32FromInt64(20)+uint32(nName)))
		if pColl != 0 {
			pDel = uintptr(0)
			(**(**TCollSeq)(__ccgo_up(pColl))).FzName = pColl + 3*20
			(**(**TCollSeq)(__ccgo_up(pColl))).Fenc = uint8(SQLITE_UTF8)
			(**(**TCollSeq)(__ccgo_up(pColl + 1*20))).FzName = pColl + 3*20
			(**(**TCollSeq)(__ccgo_up(pColl + 1*20))).Fenc = uint8(SQLITE_UTF16LE)
			(**(**TCollSeq)(__ccgo_up(pColl + 2*20))).FzName = pColl + 3*20
			(**(**TCollSeq)(__ccgo_up(pColl + 2*20))).Fenc = uint8(SQLITE_UTF16BE)
			libc.Xmemcpy(tls, (**(**TCollSeq)(__ccgo_up(pColl))).FzName, zName, uint32(nName))
			pDel = _sqlite3HashInsert(tls, db+448, (**(**TCollSeq)(__ccgo_up(pColl))).FzName, pColl)
			/* If a malloc() failure occurred in sqlite3HashInsert(), it will
			 ** return the pColl pointer to be deleted (because it wasn't added
			 ** to the hash table).
			 */
			if pDel != uintptr(0) {
				_sqlite3OomFault(tls, db)
				_sqlite3DbFree(tls, db, pDel)
				pColl = uintptr(0)
			}
		}
	}
	return pColl
}

// C documentation
//
//	/*
//	** Scan all previously generated bytecode looking for an OP_BeginSubrtn
//	** that is compatible with pExpr.  If found, add the y.sub values
//	** to pExpr and return true.  If not found, return false.
//	*/
func _findCompatibleInRhsSubrtn(tls *libc.TLS, pParse uintptr, pExpr uintptr, pNewSig uintptr) (r int32) {
	var pEnd, pOp, pSig, v uintptr
	_, _, _, _ = pEnd, pOp, pSig, v
	if pNewSig == uintptr(0) {
		return 0
	}
	if int32((*TParse)(unsafe.Pointer(pParse)).FmSubrtnSig)&(int32(1)<<((*TSubrtnSig)(unsafe.Pointer(pNewSig)).FselId&int32(7))) == 0 {
		return 0
	}
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	pOp = _sqlite3VdbeGetOp(tls, v, int32(1))
	pEnd = _sqlite3VdbeGetLastOp(tls, v)
	for {
		if !(pOp < pEnd) {
			break
		}
		if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type) != -int32(18) {
			goto _1
		}
		pSig = *(*uintptr)(unsafe.Pointer(pOp + 16))
		if !((*TSubrtnSig)(unsafe.Pointer(pSig)).FbComplete != 0) {
			goto _1
		}
		if (*TSubrtnSig)(unsafe.Pointer(pNewSig)).FselId != (*TSubrtnSig)(unsafe.Pointer(pSig)).FselId {
			goto _1
		}
		if libc.Xstrcmp(tls, (*TSubrtnSig)(unsafe.Pointer(pNewSig)).FzAff, (*TSubrtnSig)(unsafe.Pointer(pSig)).FzAff) != 0 {
			goto _1
		}
		(*(*struct {
			FiAddr     int32
			FregReturn int32
		})(unsafe.Pointer(pExpr + 44))).FiAddr = (*TSubrtnSig)(unsafe.Pointer(pSig)).FiAddr
		(*(*struct {
			FiAddr     int32
			FregReturn int32
		})(unsafe.Pointer(pExpr + 44))).FregReturn = (*TSubrtnSig)(unsafe.Pointer(pSig)).FregReturn
		(*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TSubrtnSig)(unsafe.Pointer(pSig)).FiTable
		**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Subrtn))
		return int32(1)
		goto _1
	_1:
		;
		pOp += 20
	}
	return 0
}

// C documentation
//
//	/*
//	** This function searches pList for an entry that matches the iCol-th column
//	** of index pIdx.
//	**
//	** If such an expression is found, its index in pList->a[] is returned. If
//	** no expression is found, -1 is returned.
//	*/
func _findIndexCol(tls *libc.TLS, pParse uintptr, pList uintptr, iBase int32, pIdx uintptr, iCol int32) (r int32) {
	var i int32
	var p, pColl, zColl uintptr
	_, _, _, _ = i, p, pColl, zColl
	zColl = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(iCol)*4))
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
			break
		}
		p = _sqlite3ExprSkipCollateAndLikely(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr)
		if p != uintptr(0) && (int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_AGG_COLUMN)) && int32((*TExpr)(unsafe.Pointer(p)).FiColumn) == int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(iCol)*2))) && (*TExpr)(unsafe.Pointer(p)).FiTable == iBase {
			pColl = _sqlite3ExprNNCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr)
			if 0 == _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FzName, zColl) {
				return i
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return -int32(1)
}

// C documentation
//
//	/*
//	** Search the AggInfo object for an aCol[] entry that has iTable and iColumn.
//	** Return the index in aCol[] of the entry that describes that column.
//	**
//	** If no prior entry is found, create a new one and return -1.  The
//	** new column will have an index of pAggInfo->nColumn-1.
//	*/
func _findOrCreateAggInfoColumn(tls *libc.TLS, pParse uintptr, pAggInfo uintptr, pExpr uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var j, k, mxTerm, n int32
	var pCol, pE, pGB, pTerm, v4 uintptr
	var v3 Tu32
	_, _, _, _, _, _, _, _, _, _ = j, k, mxTerm, n, pCol, pE, pGB, pTerm, v3, v4
	mxTerm = **(**int32)(__ccgo_up((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 2*4))
	pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol
	k = 0
	for {
		if !(k < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn) {
			break
		}
		if (*TAggInfo_col)(unsafe.Pointer(pCol)).FpCExpr == pExpr {
			return
		}
		if (*TAggInfo_col)(unsafe.Pointer(pCol)).FiTable == (*TExpr)(unsafe.Pointer(pExpr)).FiTable && (*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn == int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_IF_NULL_ROW) {
			goto fix_up_expr
		}
		goto _1
	_1:
		;
		k = k + 1
		pCol += 20
	}
	k = _addAggInfoColumn(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pAggInfo)
	if k < 0 {
		/* OOM on resize */
		return
	}
	if k > mxTerm {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9831, libc.VaList(bp+8, mxTerm))
		k = mxTerm
	}
	pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(k)*20
	(*TAggInfo_col)(unsafe.Pointer(pCol)).FpTab = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab
	(*TAggInfo_col)(unsafe.Pointer(pCol)).FiTable = (*TExpr)(unsafe.Pointer(pExpr)).FiTable
	(*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
	(*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn = -int32(1)
	(*TAggInfo_col)(unsafe.Pointer(pCol)).FpCExpr = pExpr
	if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FpGroupBy != 0 && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_IF_NULL_ROW) {
		pGB = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FpGroupBy
		pTerm = pGB + 8
		n = (*TExprList)(unsafe.Pointer(pGB)).FnExpr
		j = 0
		for {
			if !(j < n) {
				break
			}
			pE = (*TExprList_item)(unsafe.Pointer(pTerm)).FpExpr
			if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pE)).FiTable == (*TExpr)(unsafe.Pointer(pExpr)).FiTable && int32((*TExpr)(unsafe.Pointer(pE)).FiColumn) == int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) {
				(*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn = j
				break
			}
			goto _2
		_2:
			;
			j = j + 1
			pTerm += 20
		}
	}
	if (*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn < 0 {
		v4 = pAggInfo + 4
		v3 = *(*Tu32)(unsafe.Pointer(v4))
		*(*Tu32)(unsafe.Pointer(v4)) = *(*Tu32)(unsafe.Pointer(v4)) + 1
		(*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn = int32(v3)
	}
	goto fix_up_expr
fix_up_expr:
	;
	(*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo = pAggInfo
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) {
		(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_AGG_COLUMN)
	}
	(*TExpr)(unsafe.Pointer(pExpr)).FiAgg = int16(k)
}

// C documentation
//
//	/*
//	** Expression callback used by sqlite3FixAAAA() routines.
//	*/
func _fixExprCb(tls *libc.TLS, p uintptr, pExpr uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pFix uintptr
	_ = pFix
	pFix = *(*uintptr)(unsafe.Pointer(p + 24))
	if !((*TDbFixer)(unsafe.Pointer(pFix)).FbTemp != 0) {
		**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_FromDDL))
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_VARIABLE) {
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TDbFixer)(unsafe.Pointer(pFix)).FpParse)).Fdb)).Finit1.Fbusy != 0 {
			(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_NULL)
		} else {
			_sqlite3ErrorMsg(tls, (*TDbFixer)(unsafe.Pointer(pFix)).FpParse, __ccgo_ts+14756, libc.VaList(bp+8, (*TDbFixer)(unsafe.Pointer(pFix)).FzType))
			return int32(WRC_Abort)
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Select callback used by sqlite3FixAAAA() routines.
//	*/
func _fixSelectCb(tls *libc.TLS, p uintptr, pSelect uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pFix, pItem, pList uintptr
	var i, iDb int32
	_, _, _, _, _, _ = db, i, iDb, pFix, pItem, pList
	pFix = *(*uintptr)(unsafe.Pointer(p + 24))
	db = (*TParse)(unsafe.Pointer((*TDbFixer)(unsafe.Pointer(pFix)).FpParse)).Fdb
	iDb = _sqlite3FindDbName(tls, db, (*TDbFixer)(unsafe.Pointer(pFix)).FzDb)
	pList = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc
	if pList == uintptr(0) {
		return WRC_Continue
	}
	i = 0
	pItem = pList + 8
	for {
		if !(i < (*TSrcList)(unsafe.Pointer(pList)).FnSrc) {
			break
		}
		if int32((*TDbFixer)(unsafe.Pointer(pFix)).FbTemp) == 0 && int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4>>2) == 0 {
			if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x10000>>16) == 0 && *(*uintptr)(unsafe.Pointer(pItem + 44)) != uintptr(0) {
				if iDb != _sqlite3FindDbName(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 44))) {
					_sqlite3ErrorMsg(tls, (*TDbFixer)(unsafe.Pointer(pFix)).FpParse, __ccgo_ts+14780, libc.VaList(bp+8, (*TDbFixer)(unsafe.Pointer(pFix)).FzType, (*TDbFixer)(unsafe.Pointer(pFix)).FpName, *(*uintptr)(unsafe.Pointer(pItem + 44))))
					return int32(WRC_Abort)
				}
				_sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 44)))
				libc.SetBitFieldPtr32Uint32(pItem+12+4, libc.Uint32FromInt32(1), 10, 0x400)
				libc.SetBitFieldPtr32Uint32(pItem+12+4, libc.Uint32FromInt32(1), 17, 0x20000)
			}
			*(*uintptr)(unsafe.Pointer(pItem + 44)) = (*TDbFixer)(unsafe.Pointer(pFix)).FpSchema
			libc.SetBitFieldPtr32Uint32(pItem+12+4, libc.Uint32FromInt32(1), 8, 0x100)
			libc.SetBitFieldPtr32Uint32(pItem+12+4, libc.Uint32FromInt32(1), 16, 0x10000)
		}
		if int32(*(*uint32)(unsafe.Pointer(pList + 8 + uintptr(i)*48 + 12 + 4))&0x800>>11) == 0 && _sqlite3WalkExpr(tls, pFix+4, *(*uintptr)(unsafe.Pointer(pList + 8 + uintptr(i)*48 + 40))) != 0 {
			return int32(WRC_Abort)
		}
		goto _1
	_1:
		;
		i = i + 1
		pItem += 48
	}
	if (*TSelect)(unsafe.Pointer(pSelect)).FpWith != 0 {
		i = 0
		for {
			if !(i < (*TWith)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpWith)).FnCte) {
				break
			}
			if _sqlite3WalkSelect(tls, p, (*(*TCte)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpWith + 12 + uintptr(i)*24))).FpSelect) != 0 {
				return int32(WRC_Abort)
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** This function is called when an UPDATE or DELETE operation is being
//	** compiled on table pTab, which is the parent table of foreign-key pFKey.
//	** If the current operation is an UPDATE, then the pChanges parameter is
//	** passed a pointer to the list of columns being modified. If it is a
//	** DELETE, pChanges is passed a NULL pointer.
//	**
//	** It returns a pointer to a Trigger structure containing a trigger
//	** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
//	** If the action is "NO ACTION" then a NULL pointer is returned (these actions
//	** require no special handling by the triggers sub-system, code for them is
//	** created by fkScanChildren()).
//	**
//	** For example, if pFKey is the foreign key and pTab is table "p" in
//	** the following schema:
//	**
//	**   CREATE TABLE p(pk PRIMARY KEY);
//	**   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
//	**
//	** then the returned trigger structure is equivalent to:
//	**
//	**   CREATE TRIGGER ... DELETE ON p BEGIN
//	**     DELETE FROM c WHERE ck = old.pk;
//	**   END;
//	**
//	** The returned pointer is cached as part of the foreign key object. It
//	** is eventually freed along with the rest of the foreign key object by
//	** sqlite3FkDelete().
//	*/
func _fkActionTrigger(tls *libc.TLS, pParse uintptr, pTab uintptr, pFKey uintptr, pChanges uintptr) (r uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var action, i, iAction, iFromCol, nFrom, v2 int32
	var db, pCol, pDflt, pEq, pItem, pItem1, pList, pNew, pRaise, pSelect, pSrc, pStep, pTrigger, pWhen, pWhere, zFrom, v4 uintptr
	var _ /* aiCol at bp+4 */ uintptr
	var _ /* pIdx at bp+0 */ uintptr
	var _ /* tFromCol at bp+24 */ TToken
	var _ /* tNew at bp+16 */ TToken
	var _ /* tOld at bp+8 */ TToken
	var _ /* tToCol at bp+32 */ TToken
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = action, db, i, iAction, iFromCol, nFrom, pCol, pDflt, pEq, pItem, pItem1, pList, pNew, pRaise, pSelect, pSrc, pStep, pTrigger, pWhen, pWhere, zFrom, v2, v4
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb       /* Trigger definition to return */
	iAction = libc.BoolInt32(pChanges != uintptr(0)) /* 1 for UPDATE, 0 for DELETE */
	action = int32(**(**Tu8)(__ccgo_up(pFKey + 25 + uintptr(iAction))))
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00008))<<libc.Int32FromInt32(32)) != 0 {
		action = OE_None
	}
	if action == int32(OE_Restrict) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DeferFKs) != 0 {
		return uintptr(0)
	}
	pTrigger = **(**uintptr)(__ccgo_up(pFKey + 28 + uintptr(iAction)*4))
	if action != OE_None && !(pTrigger != 0) { /* Length in bytes of zFrom */
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)     /* Parent key index for this FK */
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* child table cols -> parent key cols */
		pStep = uintptr(0)                            /* First (only) step of trigger program */
		pWhere = uintptr(0)                           /* WHERE clause of trigger step */
		pList = uintptr(0)                            /* Changes list if ON UPDATE CASCADE */
		pSelect = uintptr(0)                          /* Iterator variable */
		pWhen = uintptr(0)                            /* WHEN clause for the trigger */
		if _sqlite3FkLocateIndex(tls, pParse, pTab, pFKey, bp, bp+4) != 0 {
			return uintptr(0)
		}
		i = 0
		for {
			if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) {
				break
			}
			**(**TToken)(__ccgo_up(bp + 8)) = TToken{
				Fz: __ccgo_ts + 8044,
				Fn: uint32(3),
			} /* Literal "old" token */
			**(**TToken)(__ccgo_up(bp + 16)) = TToken{
				Fz: __ccgo_ts + 8040,
				Fn: uint32(3),
			} /* tFromCol = OLD.tToCol */
			if **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
				v2 = **(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4)) + uintptr(i)*4))
			} else {
				v2 = (*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FiFrom
			}
			iFromCol = v2
			if **(**uintptr)(__ccgo_up(bp)) != 0 {
				v2 = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaiColumn + uintptr(i)*2)))
			} else {
				v2 = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey)
			}
			_sqlite3TokenInit(tls, bp+32, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(v2)*12))).FzCnName)
			_sqlite3TokenInit(tls, bp+24, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FaCol + uintptr(iFromCol)*12))).FzCnName)
			/* Create the expression "OLD.zToCol = zFromCol". It is important
			 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
			 ** that the affinity and collation sequence associated with the
			 ** parent table are used for the comparison. */
			pEq = _sqlite3PExpr(tls, pParse, int32(TK_EQ), _sqlite3PExpr(tls, pParse, int32(TK_DOT), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+8, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+32, 0)), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+24, 0))
			pWhere = _sqlite3ExprAnd(tls, pParse, pWhere, pEq)
			/* For ON UPDATE, construct the next term of the WHEN clause.
			 ** The final WHEN clause will be like this:
			 **
			 **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
			 */
			if pChanges != 0 {
				pEq = _sqlite3PExpr(tls, pParse, int32(TK_IS), _sqlite3PExpr(tls, pParse, int32(TK_DOT), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+8, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+32, 0)), _sqlite3PExpr(tls, pParse, int32(TK_DOT), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+16, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+32, 0)))
				pWhen = _sqlite3ExprAnd(tls, pParse, pWhen, pEq)
			}
			if action != int32(OE_Restrict) && (action != int32(OE_Cascade) || pChanges != 0) {
				if action == int32(OE_Cascade) {
					pNew = _sqlite3PExpr(tls, pParse, int32(TK_DOT), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+16, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+32, 0))
				} else {
					if action == int32(OE_SetDflt) {
						pCol = (*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FaCol + uintptr(iFromCol)*12
						if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
							pDflt = uintptr(0)
						} else {
							pDflt = _sqlite3ColumnExpr(tls, (*TFKey)(unsafe.Pointer(pFKey)).FpFrom, pCol)
						}
						if pDflt != 0 {
							pNew = _sqlite3ExprDup(tls, db, pDflt, 0)
						} else {
							pNew = _sqlite3ExprAlloc(tls, db, int32(TK_NULL), uintptr(0), 0)
						}
					} else {
						pNew = _sqlite3ExprAlloc(tls, db, int32(TK_NULL), uintptr(0), 0)
					}
				}
				pList = _sqlite3ExprListAppend(tls, pParse, pList, pNew)
				_sqlite3ExprListSetName(tls, pParse, pList, bp+24, 0)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp + 4)))
		zFrom = (*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FzName
		nFrom = _sqlite3Strlen30(tls, zFrom)
		if action == int32(OE_Restrict) {
			pRaise = _sqlite3Expr(tls, db, int32(TK_STRING), __ccgo_ts+6609)
			pRaise = _sqlite3PExpr(tls, pParse, int32(TK_RAISE), pRaise, uintptr(0))
			if pRaise != 0 {
				(*TExpr)(unsafe.Pointer(pRaise)).FaffExpr = int8(OE_Abort)
			}
			pSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0))
			if pSrc != 0 {
				pItem = pSrc + 8
				(*TSrcItem)(unsafe.Pointer(pItem)).FzName = _sqlite3DbStrDup(tls, db, zFrom)
				libc.SetBitFieldPtr32Uint32(pItem+12+4, libc.Uint32FromInt32(1), 16, 0x10000)
				*(*uintptr)(unsafe.Pointer(pItem + 44)) = (*TTable)(unsafe.Pointer(pTab)).FpSchema
			}
			pSelect = _sqlite3SelectNew(tls, pParse, _sqlite3ExprListAppend(tls, pParse, uintptr(0), pRaise), pSrc, pWhere, uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0))
			pWhere = uintptr(0)
		}
		/* Disable lookaside memory allocation */
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable + 1
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0)
		pTrigger = _sqlite3DbMallocZero(tls, db, uint64(libc.Uint32FromInt64(36)+libc.Uint32FromInt64(44)))
		if pTrigger != 0 {
			v4 = pTrigger + 1*36
			(*TTrigger)(unsafe.Pointer(pTrigger)).Fstep_list = v4
			pStep = v4
			(*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0))
			if (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc != 0 {
				pItem1 = (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc + 8
				(*TSrcItem)(unsafe.Pointer(pItem1)).FzName = _sqlite3DbStrNDup(tls, db, zFrom, uint64(nFrom))
				*(*uintptr)(unsafe.Pointer(pItem1 + 44)) = (*TTable)(unsafe.Pointer(pTab)).FpSchema
				libc.SetBitFieldPtr32Uint32(pItem1+12+4, libc.Uint32FromInt32(1), 16, 0x10000)
			}
			(*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere = _sqlite3ExprDup(tls, db, pWhere, int32(EXPRDUP_REDUCE))
			(*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList = _sqlite3ExprListDup(tls, db, pList, int32(EXPRDUP_REDUCE))
			(*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect = _sqlite3SelectDup(tls, db, pSelect, int32(EXPRDUP_REDUCE))
			if pWhen != 0 {
				pWhen = _sqlite3PExpr(tls, pParse, int32(TK_NOT), pWhen, uintptr(0))
				(*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen = _sqlite3ExprDup(tls, db, pWhen, int32(EXPRDUP_REDUCE))
			}
		}
		/* Re-enable the lookaside buffer, if it was disabled earlier. */
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable - 1
		if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0 {
			v2 = 0
		} else {
			v2 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue)
		}
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(v2)
		_sqlite3ExprDelete(tls, db, pWhere)
		_sqlite3ExprDelete(tls, db, pWhen)
		_sqlite3ExprListDelete(tls, db, pList)
		_sqlite3SelectDelete(tls, db, pSelect)
		if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == int32(1) {
			_fkTriggerDelete(tls, db, pTrigger)
			return uintptr(0)
		}
		switch action {
		case int32(OE_Restrict):
			(*TTriggerStep)(unsafe.Pointer(pStep)).Fop = uint8(TK_SELECT)
		case int32(OE_Cascade):
			if !(pChanges != 0) {
				(*TTriggerStep)(unsafe.Pointer(pStep)).Fop = uint8(TK_DELETE)
				break
			}
			fallthrough
		default:
			(*TTriggerStep)(unsafe.Pointer(pStep)).Fop = uint8(TK_UPDATE)
		}
		(*TTriggerStep)(unsafe.Pointer(pStep)).FpTrig = pTrigger
		(*TTrigger)(unsafe.Pointer(pTrigger)).FpSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema
		(*TTrigger)(unsafe.Pointer(pTrigger)).FpTabSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema
		**(**uintptr)(__ccgo_up(pFKey + 28 + uintptr(iAction)*4)) = pTrigger
		if pChanges != 0 {
			v2 = int32(TK_UPDATE)
		} else {
			v2 = int32(TK_DELETE)
		}
		(*TTrigger)(unsafe.Pointer(pTrigger)).Fop = uint8(v2)
	}
	return pTrigger
}

// C documentation
//
//	/*
//	** The second argument points to an FKey object representing a foreign key
//	** for which pTab is the parent table. An UPDATE statement against pTab
//	** is currently being processed. For each column of the table that is
//	** actually updated, the corresponding element in the aChange[] array
//	** is zero or greater (if a column is unmodified the corresponding element
//	** is set to -1). If the rowid column is modified by the UPDATE statement
//	** the bChngRowid argument is non-zero.
//	**
//	** This function returns true if any of the columns that are part of the
//	** parent key for FK constraint *p are modified.
//	*/
func _fkParentIsModified(tls *libc.TLS, pTab uintptr, p uintptr, aChange uintptr, bChngRowid int32) (r int32) {
	var i, iKey int32
	var pCol, zKey uintptr
	_, _, _, _ = i, iKey, pCol, zKey
	i = 0
	for {
		if !(i < (*TFKey)(unsafe.Pointer(p)).FnCol) {
			break
		}
		zKey = (*(*TsColMap)(unsafe.Pointer(p + 36 + uintptr(i)*8))).FzCol
		iKey = 0
		for {
			if !(iKey < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			if **(**int32)(__ccgo_up(aChange + uintptr(iKey)*4)) >= 0 || iKey == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) && bChngRowid != 0 {
				pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iKey)*12
				if zKey != 0 {
					if 0 == _sqlite3StrICmp(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, zKey) {
						return int32(1)
					}
				} else {
					if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 {
						return int32(1)
					}
				}
			}
			goto _2
		_2:
			;
			iKey = iKey + 1
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** This function is called to generate code executed when a row is deleted
//	** from the parent table of foreign key constraint pFKey and, if pFKey is
//	** deferred, when a row is inserted into the same table. When generating
//	** code for an SQL UPDATE operation, this function may be called twice -
//	** once to "delete" the old row and once to "insert" the new row.
//	**
//	** Parameter nIncr is passed -1 when inserting a row (as this may decrease
//	** the number of FK violations in the db) or +1 when deleting one (as this
//	** may increase the number of FK constraint problems).
//	**
//	** The code generated by this function scans through the rows in the child
//	** table that correspond to the parent table row being deleted or inserted.
//	** For each child row found, one of the following actions is taken:
//	**
//	**   Operation | FK type   | Action taken
//	**   --------------------------------------------------------------------------
//	**   DELETE      immediate   Increment the "immediate constraint counter".
//	**
//	**   INSERT      immediate   Decrement the "immediate constraint counter".
//	**
//	**   DELETE      deferred    Increment the "deferred constraint counter".
//	**
//	**   INSERT      deferred    Decrement the "deferred constraint counter".
//	**
//	** These operations are identified in the comment at the top of this file
//	** (fkey.c) as "I.2" and "D.2".
//	*/
func _fkScanChildren(tls *libc.TLS, pParse uintptr, pSrc uintptr, pTab uintptr, pIdx uintptr, pFKey uintptr, aiCol uintptr, regData int32, nIncr int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var db, pAll, pEq, pEq1, pLeft, pLeft1, pNe, pRight, pRight1, pWInfo, pWhere, v, zCol uintptr
	var i, iFkIfZero, v2 int32
	var iCol, iCol1 Ti16
	var _ /* sNameContext at bp+0 */ TNameContext
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, iCol, iCol1, iFkIfZero, pAll, pEq, pEq1, pLeft, pLeft1, pNe, pRight, pRight1, pWInfo, pWhere, v, zCol, v2
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Iterator variable */
	pWhere = uintptr(0)                        /* Context used by sqlite3WhereXXX() */
	iFkIfZero = 0                              /* Address of OP_FkIfZero */
	v = _sqlite3GetVdbe(tls, pParse)
	if nIncr < 0 {
		iFkIfZero = _sqlite3VdbeAddOp2(tls, v, int32(OP_FkIfZero), int32((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred), 0)
	}
	/* Create an Expr object representing an SQL expression like:
	 **
	 **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
	 **
	 ** The collation sequence used for the comparison should be that of
	 ** the parent key columns. The affinity of the parent key column should
	 ** be applied to each child key value before the comparison takes place.
	 */
	i = 0
	for {
		if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) {
			break
		} /* Name of column in child table */
		if pIdx != 0 {
			v2 = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2)))
		} else {
			v2 = -int32(1)
		}
		iCol = int16(v2)
		pLeft = _exprTableRegister(tls, pParse, pTab, regData, iCol)
		if aiCol != 0 {
			v2 = **(**int32)(__ccgo_up(aiCol + uintptr(i)*4))
		} else {
			v2 = (*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FiFrom
		}
		iCol = int16(v2)
		zCol = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FaCol + uintptr(iCol)*12))).FzCnName
		pRight = _sqlite3Expr(tls, db, int32(TK_ID), zCol)
		pEq = _sqlite3PExpr(tls, pParse, int32(TK_EQ), pLeft, pRight)
		pWhere = _sqlite3ExprAnd(tls, pParse, pWhere, pEq)
		goto _1
	_1:
		;
		i = i + 1
	}
	/* If the child table is the same as the parent table, then add terms
	 ** to the WHERE clause that prevent this entry from being scanned.
	 ** The added WHERE clause terms are like this:
	 **
	 **     $current_rowid!=rowid
	 **     NOT( $current_a==a AND $current_b==b AND ... )
	 **
	 ** The first form is used for rowid tables.  The second form is used
	 ** for WITHOUT ROWID tables. In the second form, the *parent* key is
	 ** (a,b,...). Either the parent or primary key could be used to
	 ** uniquely identify the current row, but the parent key is more convenient
	 ** as the required values have already been loaded into registers
	 ** by the caller.
	 */
	if pTab == (*TFKey)(unsafe.Pointer(pFKey)).FpFrom && nIncr > 0 { /* Column ref to child table */
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			pLeft1 = _exprTableRegister(tls, pParse, pTab, regData, int16(-int32(1)))
			pRight1 = _exprTableColumn(tls, db, pTab, (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FiCursor, int16(-int32(1)))
			pNe = _sqlite3PExpr(tls, pParse, int32(TK_NE), pLeft1, pRight1)
		} else {
			pAll = uintptr(0)
			i = 0
			for {
				if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
					break
				}
				iCol1 = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))
				pLeft1 = _exprTableRegister(tls, pParse, pTab, regData, iCol1)
				pRight1 = _sqlite3Expr(tls, db, int32(TK_ID), (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol1)*12))).FzCnName)
				pEq1 = _sqlite3PExpr(tls, pParse, int32(TK_IS), pLeft1, pRight1)
				pAll = _sqlite3ExprAnd(tls, pParse, pAll, pEq1)
				goto _4
			_4:
				;
				i = i + 1
			}
			pNe = _sqlite3PExpr(tls, pParse, int32(TK_NOT), pAll, uintptr(0))
		}
		pWhere = _sqlite3ExprAnd(tls, pParse, pWhere, pNe)
	}
	/* Resolve the references in the WHERE clause. */
	libc.Xmemset(tls, bp, 0, uint32(36))
	(**(**TNameContext)(__ccgo_up(bp))).FpSrcList = pSrc
	(**(**TNameContext)(__ccgo_up(bp))).FpParse = pParse
	_sqlite3ResolveExprNames(tls, bp, pWhere)
	/* Create VDBE to loop through the entries in pSrc that match the WHERE
	 ** clause. For each row found, increment either the deferred or immediate
	 ** foreign key constraint counter. */
	if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
		pWInfo = _sqlite3WhereBegin(tls, pParse, pSrc, pWhere, uintptr(0), uintptr(0), uintptr(0), uint16(0), 0)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_FkCounter), int32((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred), nIncr)
		if pWInfo != 0 {
			_sqlite3WhereEnd(tls, pWInfo)
		}
	}
	/* Clean up the WHERE clause constructed above. */
	_sqlite3ExprDelete(tls, db, pWhere)
	if iFkIfZero != 0 {
		_sqlite3VdbeJumpHereOrPopInst(tls, v, iFkIfZero)
	}
}

// C documentation
//
//	/*
//	** This routine attempts to flatten subqueries as a performance optimization.
//	** This routine returns 1 if it makes changes and 0 if no flattening occurs.
//	**
//	** To understand the concept of flattening, consider the following
//	** query:
//	**
//	**     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
//	**
//	** The default way of implementing this query is to execute the
//	** subquery first and store the results in a temporary table, then
//	** run the outer query on that temporary table.  This requires two
//	** passes over the data.  Furthermore, because the temporary table
//	** has no indices, the WHERE clause on the outer query cannot be
//	** optimized.
//	**
//	** This routine attempts to rewrite queries such as the above into
//	** a single flat select, like this:
//	**
//	**     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
//	**
//	** The code generated for this simplification gives the same result
//	** but only has to scan the data once.  And because indices might
//	** exist on the table t1, a complete scan of the data might be
//	** avoided.
//	**
//	** Flattening is subject to the following constraints:
//	**
//	**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
//	**        The subquery and the outer query cannot both be aggregates.
//	**
//	**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
//	**        (2) If the subquery is an aggregate then
//	**        (2a) the outer query must not be a join and
//	**        (2b) the outer query must not use subqueries
//	**             other than the one FROM-clause subquery that is a candidate
//	**             for flattening.  (This is due to ticket [2f7170d73bf9abf80]
//	**             from 2015-02-09.)
//	**
//	**   (3)  If the subquery is the right operand of a LEFT JOIN then
//	**        (3a) the subquery may not be a join
//	**        (**) Was (3b): "the FROM clause of the subquery may not contain
//	**             a virtual table"
//	**        (**) Was: "The outer query may not have a GROUP BY." This case
//	**             is now managed correctly
//	**        (3d) the outer query may not be DISTINCT.
//	**        See also (26) for restrictions on RIGHT JOIN.
//	**
//	**   (4)  The subquery can not be DISTINCT.
//	**
//	**  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
//	**        sub-queries that were excluded from this optimization. Restriction
//	**        (4) has since been expanded to exclude all DISTINCT subqueries.
//	**
//	**  (**)  We no longer attempt to flatten aggregate subqueries.  Was:
//	**        If the subquery is aggregate, the outer query may not be DISTINCT.
//	**
//	**   (7)  The subquery must have a FROM clause.  TODO:  For subqueries without
//	**        A FROM clause, consider adding a FROM clause with the special
//	**        table sqlite_once that consists of a single row containing a
//	**        single NULL.
//	**
//	**   (8)  If the subquery uses LIMIT then the outer query may not be a join.
//	**
//	**   (9)  If the subquery uses LIMIT then the outer query may not be aggregate.
//	**
//	**  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
//	**        accidentally carried the comment forward until 2014-09-15.  Original
//	**        constraint: "If the subquery is aggregate then the outer query
//	**        may not use LIMIT."
//	**
//	**  (11)  The subquery and the outer query may not both have ORDER BY clauses.
//	**
//	**  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
//	**        a separate restriction deriving from ticket #350.
//	**
//	**  (13)  The subquery and outer query may not both use LIMIT.
//	**
//	**  (14)  The subquery may not use OFFSET.
//	**
//	**  (15)  If the outer query is part of a compound select, then the
//	**        subquery may not use LIMIT.
//	**        (See ticket #2339 and ticket [02a8e81d44]).
//	**
//	**  (16)  If the outer query is aggregate, then the subquery may not
//	**        use ORDER BY.  (Ticket #2942)  This used to not matter
//	**        until we introduced the group_concat() function.
//	**
//	**  (17)  If the subquery is a compound select, then
//	**        (17a) all compound operators must be a UNION ALL, and
//	**        (17b) no terms within the subquery compound may be aggregate
//	**              or DISTINCT, and
//	**        (17c) every term within the subquery compound must have a FROM clause
//	**        (17d) the outer query may not be
//	**              (17d1) aggregate, or
//	**              (17d2) DISTINCT
//	**        (17e) the subquery may not contain window functions, and
//	**        (17f) the subquery must not be the RHS of a LEFT JOIN.
//	**        (17g) either the subquery is the first element of the outer
//	**              query or there are no RIGHT or FULL JOINs in any arm
//	**              of the subquery.  (This is a duplicate of condition (27b).)
//	**        (17h) The corresponding result set expressions in all arms of the
//	**              compound must have the same affinity.
//	**
//	**        The parent and sub-query may contain WHERE clauses. Subject to
//	**        rules (11), (13) and (14), they may also contain ORDER BY,
//	**        LIMIT and OFFSET clauses.  The subquery cannot use any compound
//	**        operator other than UNION ALL because all the other compound
//	**        operators have an implied DISTINCT which is disallowed by
//	**        restriction (4).
//	**
//	**        Also, each component of the sub-query must return the same number
//	**        of result columns. This is actually a requirement for any compound
//	**        SELECT statement, but all the code here does is make sure that no
//	**        such (illegal) sub-query is flattened. The caller will detect the
//	**        syntax error and return a detailed message.
//	**
//	**  (18)  If the sub-query is a compound select, then all terms of the
//	**        ORDER BY clause of the parent must be copies of a term returned
//	**        by the parent query.
//	**
//	**  (19)  If the subquery uses LIMIT then the outer query may not
//	**        have a WHERE clause.
//	**
//	**  (20)  If the sub-query is a compound select, then it must not use
//	**        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
//	**        somewhat by saying that the terms of the ORDER BY clause must
//	**        appear as unmodified result columns in the outer query.  But we
//	**        have other optimizations in mind to deal with that case.
//	**
//	**  (21)  If the subquery uses LIMIT then the outer query may not be
//	**        DISTINCT.  (See ticket [752e1646fc]).
//	**
//	**  (22)  The subquery may not be a recursive CTE.
//	**
//	**  (23)  If the outer query is a recursive CTE, then the sub-query may not be
//	**        a compound query.  This restriction is because transforming the
//	**        parent to a compound query confuses the code that handles
//	**        recursive queries in multiSelect().
//	**
//	**  (**)  We no longer attempt to flatten aggregate subqueries.  Was:
//	**        The subquery may not be an aggregate that uses the built-in min() or
//	**        or max() functions.  (Without this restriction, a query like:
//	**        "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
//	**        return the value X for which Y was maximal.)
//	**
//	**  (25)  If either the subquery or the parent query contains a window
//	**        function in the select list or ORDER BY clause, flattening
//	**        is not attempted.
//	**
//	**  (26)  The subquery may not be the right operand of a RIGHT JOIN.
//	**        See also (3) for restrictions on LEFT JOIN.
//	**
//	**  (27)  The subquery may not contain a FULL or RIGHT JOIN unless it
//	**        is the first element of the parent query.  Two subcases:
//	**        (27a) the subquery is not a compound query.
//	**        (27b) the subquery is a compound query and the RIGHT JOIN occurs
//	**              in any arm of the compound query.  (See also (17g).)
//	**
//	**  (28)  The subquery is not a MATERIALIZED CTE.  (This is handled
//	**        in the caller before ever reaching this routine.)
//	**
//	**
//	** In this routine, the "p" parameter is a pointer to the outer query.
//	** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
//	** uses aggregates.
//	**
//	** If flattening is not attempted, this routine is a no-op and returns 0.
//	** If flattening is attempted this routine returns 1.
//	**
//	** All of the expression analysis must occur on both the outer query and
//	** the subquery before this routine runs.
//	*/
func _flattenSubquery(tls *libc.TLS, pParse uintptr, p uintptr, iFrom int32, isAgg int32) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var aCsrMap, db, pItem, pItemTab, pLimit, pNew, pOrderBy, pOrderBy1, pParent, pPrior, pSrc, pSub, pSub1, pSubSrc, pSubitem, pTabToDel, pToplevel, pWhere, zSavedAuthContext, v5 uintptr
	var i, iNewParent, iParent, ii, isOuterJoin, nSubSrc, v4 int32
	var jointype Tu8
	var _ /* w at bp+0 */ TWalker
	var _ /* x at bp+28 */ TSubstContext
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aCsrMap, db, i, iNewParent, iParent, ii, isOuterJoin, jointype, nSubSrc, pItem, pItemTab, pLimit, pNew, pOrderBy, pOrderBy1, pParent, pPrior, pSrc, pSub, pSub1, pSubSrc, pSubitem, pTabToDel, pToplevel, pWhere, zSavedAuthContext, v4, v5
	zSavedAuthContext = (*TParse)(unsafe.Pointer(pParse)).FzAuthContext /* VDBE cursor number of the pSub result set temp table */
	iNewParent = -int32(1)                                              /* Replacement table for iParent */
	isOuterJoin = 0                                                     /* The subquery */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb                          /* Walker to persist agginfo data */
	aCsrMap = uintptr(0)
	/* Check to see if flattening is permitted.  Return 0 if not.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_QueryFlattener)) != uint32(0) {
		return 0
	}
	pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc
	pSubitem = pSrc + 8 + uintptr(iFrom)*48
	iParent = (*TSrcItem)(unsafe.Pointer(pSubitem)).FiCursor
	pSub = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pSubitem + 44)))).FpSelect
	if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 || (*TSelect)(unsafe.Pointer(pSub)).FpWin != 0 {
		return 0
	} /* Restriction (25) */
	pSubSrc = (*TSelect)(unsafe.Pointer(pSub)).FpSrc
	/* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
	 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
	 ** because they could be computed at compile-time.  But when LIMIT and OFFSET
	 ** became arbitrary expressions, we were forced to add restrictions (13)
	 ** and (14). */
	if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && (*TSelect)(unsafe.Pointer(p)).FpLimit != 0 {
		return 0
	} /* Restriction (13) */
	if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub)).FpLimit)).FpRight != 0 {
		return 0
	} /* Restriction (14) */
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Compound) != uint32(0) && (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 {
		return 0 /* Restriction (15) */
	}
	if (*TSrcList)(unsafe.Pointer(pSubSrc)).FnSrc == 0 {
		return 0
	} /* Restriction (7)  */
	if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(SF_Distinct) != 0 {
		return 0
	} /* Restriction (4)  */
	if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && ((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc > int32(1) || isAgg != 0) {
		return 0 /* Restrictions (8)(9) */
	}
	if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 && (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 {
		return 0 /* Restriction (11) */
	}
	if isAgg != 0 && (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 {
		return 0
	} /* Restriction (16) */
	if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && (*TSelect)(unsafe.Pointer(p)).FpWhere != 0 {
		return 0
	} /* Restriction (19) */
	if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != uint32(0) {
		return 0 /* Restriction (21) */
	}
	if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(libc.Int32FromInt32(SF_Recursive)) != 0 {
		return 0 /* Restrictions (22) */
	}
	/*
	 ** If the subquery is the right operand of a LEFT JOIN, then the
	 ** subquery may not be a join itself (3a). Example of why this is not
	 ** allowed:
	 **
	 **         t1 LEFT OUTER JOIN (t2 JOIN t3)
	 **
	 ** If we flatten the above, we would get
	 **
	 **         (t1 LEFT OUTER JOIN t2) JOIN t3
	 **
	 ** which is not at all the same thing.
	 **
	 ** See also tickets #306, #350, and #3300.
	 */
	if int32((*TSrcItem)(unsafe.Pointer(pSubitem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_LTORJ)) != 0 {
		if (*TSrcList)(unsafe.Pointer(pSubSrc)).FnSrc > int32(1) || (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != uint32(0) || int32((*TSrcItem)(unsafe.Pointer(pSubitem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 {
			return 0
		}
		isOuterJoin = int32(1)
	}
	/* True by restriction (7) */
	if iFrom > 0 && int32((*(*TSrcItem)(unsafe.Pointer(pSubSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
		return 0 /* Restriction (27a) */
	}
	/* Condition (28) is blocked by the caller */
	/* Restriction (17): If the sub-query is a compound SELECT, then it must
	 ** use only the UNION ALL operator. And none of the simple select queries
	 ** that make up the compound SELECT are allowed to be aggregate or distinct
	 ** queries.
	 */
	if (*TSelect)(unsafe.Pointer(pSub)).FpPrior != 0 {
		if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 {
			return 0 /* Restriction (20) */
		}
		if isAgg != 0 || (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != uint32(0) || isOuterJoin > 0 {
			return 0 /* (17d1), (17d2), or (17f) */
		}
		pSub1 = pSub
		for {
			if !(pSub1 != 0) {
				break
			}
			if (*TSelect)(unsafe.Pointer(pSub1)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) != uint32(0) || (*TSelect)(unsafe.Pointer(pSub1)).FpPrior != 0 && int32((*TSelect)(unsafe.Pointer(pSub1)).Fop) != int32(TK_ALL) || (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub1)).FpSrc)).FnSrc < int32(1) || (*TSelect)(unsafe.Pointer(pSub1)).FpWin != 0 {
				return 0
			}
			if iFrom > 0 && int32((*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub1)).FpSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
				/* Without this restriction, the JT_LTORJ flag would end up being
				 ** omitted on left-hand tables of the right join that is being
				 ** flattened. */
				return 0 /* Restrictions (17g), (27b) */
			}
			goto _1
		_1:
			;
			pSub1 = (*TSelect)(unsafe.Pointer(pSub1)).FpPrior
		}
		/* Restriction (18). */
		if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 {
			ii = 0
			for {
				if !(ii < (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy)).FnExpr) {
					break
				}
				if int32(*(*Tu16)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy + 8 + uintptr(ii)*20 + 16))) == 0 {
					return 0
				}
				goto _2
			_2:
				;
				ii = ii + 1
			}
		}
		/* Restriction (23) */
		if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Recursive) != 0 {
			return 0
		}
		/* Restriction (17h) */
		if _compoundHasDifferentAffinities(tls, pSub) != 0 {
			return 0
		}
		if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc > int32(1) {
			if (*TParse)(unsafe.Pointer(pParse)).FnSelect > int32(500) {
				return 0
			}
			if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_FlttnUnionAll)) != uint32(0) {
				return 0
			}
			aCsrMap = _sqlite3DbMallocZero(tls, db, uint64((int64((*TParse)(unsafe.Pointer(pParse)).FnTab)+int64(1))*int64(4)))
			if aCsrMap != 0 {
				**(**int32)(__ccgo_up(aCsrMap)) = (*TParse)(unsafe.Pointer(pParse)).FnTab
			}
		}
	}
	/***** If we reach this point, flattening is permitted. *****/
	/* Authorize the subquery */
	(*TParse)(unsafe.Pointer(pParse)).FzAuthContext = (*TSrcItem)(unsafe.Pointer(pSubitem)).FzName
	_sqlite3AuthCheck(tls, pParse, int32(SQLITE_SELECT), uintptr(0), uintptr(0), uintptr(0))
	(*TParse)(unsafe.Pointer(pParse)).FzAuthContext = zSavedAuthContext
	/* Delete the transient structures associated with the subquery */
	if int32(*(*uint32)(unsafe.Pointer(pSubitem + 12 + 4))&0x4>>2) != 0 {
		pSub1 = _sqlite3SubqueryDetach(tls, db, pSubitem)
	} else {
		pSub1 = uintptr(0)
	}
	_sqlite3DbFree(tls, db, (*TSrcItem)(unsafe.Pointer(pSubitem)).FzName)
	_sqlite3DbFree(tls, db, (*TSrcItem)(unsafe.Pointer(pSubitem)).FzAlias)
	(*TSrcItem)(unsafe.Pointer(pSubitem)).FzName = uintptr(0)
	(*TSrcItem)(unsafe.Pointer(pSubitem)).FzAlias = uintptr(0)
	/* If the sub-query is a compound SELECT statement, then (by restrictions
	 ** 17 and 18 above) it must be a UNION ALL and the parent query must
	 ** be of the form:
	 **
	 **     SELECT <expr-list> FROM (<sub-query>) <where-clause>
	 **
	 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
	 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
	 ** OFFSET clauses and joins them to the left-hand-side of the original
	 ** using UNION ALL operators. In this case N is the number of simple
	 ** select statements in the compound sub-query.
	 **
	 ** Example:
	 **
	 **     SELECT a+1 FROM (
	 **        SELECT x FROM tab
	 **        UNION ALL
	 **        SELECT y FROM tab
	 **        UNION ALL
	 **        SELECT abs(z*2) FROM tab2
	 **     ) WHERE a!=5 ORDER BY 1
	 **
	 ** Transformed into:
	 **
	 **     SELECT x+1 FROM tab WHERE x+1!=5
	 **     UNION ALL
	 **     SELECT y+1 FROM tab WHERE y+1!=5
	 **     UNION ALL
	 **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
	 **     ORDER BY 1
	 **
	 ** We call this the "compound-subquery flattening".
	 */
	pSub = (*TSelect)(unsafe.Pointer(pSub)).FpPrior
	for {
		if !(pSub != 0) {
			break
		}
		pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy
		pLimit = (*TSelect)(unsafe.Pointer(p)).FpLimit
		pPrior = (*TSelect)(unsafe.Pointer(p)).FpPrior
		pItemTab = (*TSrcItem)(unsafe.Pointer(pSubitem)).FpSTab
		(*TSrcItem)(unsafe.Pointer(pSubitem)).FpSTab = uintptr(0)
		(*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0)
		(*TSelect)(unsafe.Pointer(p)).FpPrior = uintptr(0)
		(*TSelect)(unsafe.Pointer(p)).FpLimit = uintptr(0)
		pNew = _sqlite3SelectDup(tls, db, p, 0)
		(*TSelect)(unsafe.Pointer(p)).FpLimit = pLimit
		(*TSelect)(unsafe.Pointer(p)).FpOrderBy = pOrderBy
		(*TSelect)(unsafe.Pointer(p)).Fop = uint8(TK_ALL)
		(*TSrcItem)(unsafe.Pointer(pSubitem)).FpSTab = pItemTab
		if pNew == uintptr(0) {
			(*TSelect)(unsafe.Pointer(p)).FpPrior = pPrior
		} else {
			v5 = pParse + 100
			*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
			v4 = *(*int32)(unsafe.Pointer(v5))
			(*TSelect)(unsafe.Pointer(pNew)).FselId = uint32(v4)
			if aCsrMap != 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
				_renumberCursors(tls, pParse, pNew, iFrom, aCsrMap)
			}
			(*TSelect)(unsafe.Pointer(pNew)).FpPrior = pPrior
			if pPrior != 0 {
				(*TSelect)(unsafe.Pointer(pPrior)).FpNext = pNew
			}
			(*TSelect)(unsafe.Pointer(pNew)).FpNext = p
			(*TSelect)(unsafe.Pointer(p)).FpPrior = pNew
		}
		goto _3
	_3:
		;
		pSub = (*TSelect)(unsafe.Pointer(pSub)).FpPrior
	}
	_sqlite3DbFree(tls, db, aCsrMap)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_sqlite3SrcItemAttachSubquery(tls, pParse, pSubitem, pSub1, 0)
		return int32(1)
	}
	/* Defer deleting the Table object associated with the
	 ** subquery until code generation is
	 ** complete, since there may still exist Expr.pTab entries that
	 ** refer to the subquery even after flattening.  Ticket #3346.
	 **
	 ** pSubitem->pSTab is always non-NULL by test restrictions and tests above.
	 */
	if (*TSrcItem)(unsafe.Pointer(pSubitem)).FpSTab != uintptr(0) {
		pTabToDel = (*TSrcItem)(unsafe.Pointer(pSubitem)).FpSTab
		if (*TTable)(unsafe.Pointer(pTabToDel)).FnTabRef == uint32(1) {
			if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 {
				v5 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel
			} else {
				v5 = pParse
			}
			pToplevel = v5
			_sqlite3ParserAddCleanup(tls, pToplevel, __ccgo_fp(_sqlite3DeleteTableGeneric), pTabToDel)
		} else {
			(*TTable)(unsafe.Pointer(pTabToDel)).FnTabRef = (*TTable)(unsafe.Pointer(pTabToDel)).FnTabRef - 1
		}
		(*TSrcItem)(unsafe.Pointer(pSubitem)).FpSTab = uintptr(0)
	}
	/* The following loop runs once for each term in a compound-subquery
	 ** flattening (as described above).  If we are doing a different kind
	 ** of flattening - a flattening other than a compound-subquery flattening -
	 ** then this loop only runs once.
	 **
	 ** This loop moves all of the FROM elements of the subquery into the
	 ** the FROM clause of the outer query.  Before doing this, remember
	 ** the cursor number for the original outer query FROM element in
	 ** iParent.  The iParent cursor will never be used.  Subsequent code
	 ** will scan expressions looking for iParent references and replace
	 ** those references with expressions that resolve to the subquery FROM
	 ** elements we are now copying in.
	 */
	pSub = pSub1
	pParent = p
	for {
		if !(pParent != 0) {
			break
		}
		jointype = (*TSrcItem)(unsafe.Pointer(pSubitem)).Ffg.Fjointype
		pSubSrc = (*TSelect)(unsafe.Pointer(pSub)).FpSrc     /* FROM clause of subquery */
		nSubSrc = (*TSrcList)(unsafe.Pointer(pSubSrc)).FnSrc /* Number of terms in subquery FROM clause */
		pSrc = (*TSelect)(unsafe.Pointer(pParent)).FpSrc     /* FROM clause of the outer query */
		/* The subquery uses a single slot of the FROM clause of the outer
		 ** query.  If the subquery has more than one element in its FROM clause,
		 ** then expand the outer query to make space for it to hold all elements
		 ** of the subquery.
		 **
		 ** Example:
		 **
		 **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
		 **
		 ** The outer query has 3 slots in its FROM clause.  One slot of the
		 ** outer query (the middle slot) is used by the subquery.  The next
		 ** block of code will expand the outer query FROM clause to 4 slots.
		 ** The middle slot is expanded to two slots in order to make space
		 ** for the two elements in the FROM clause of the subquery.
		 */
		if nSubSrc > int32(1) {
			pSrc = _sqlite3SrcListEnlarge(tls, pParse, pSrc, nSubSrc-int32(1), iFrom+int32(1))
			if pSrc == uintptr(0) {
				break
			}
			(*TSelect)(unsafe.Pointer(pParent)).FpSrc = pSrc
			pSubitem = pSrc + 8 + uintptr(iFrom)*48
		}
		/* Transfer the FROM clause terms from the subquery into the
		 ** outer query.
		 */
		iNewParent = (*(*TSrcItem)(unsafe.Pointer(pSubSrc + 8))).FiCursor
		i = 0
		for {
			if !(i < nSubSrc) {
				break
			}
			pItem = pSrc + 8 + uintptr(i+iFrom)*48
			if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x800>>11) != 0 {
				_sqlite3IdListDelete(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 40)))
			}
			**(**TSrcItem)(__ccgo_up(pItem)) = *(*TSrcItem)(unsafe.Pointer(pSubSrc + 8 + uintptr(i)*48))
			v5 = pItem + 12
			*(*Tu8)(unsafe.Pointer(v5)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v5))) | int32(jointype)&libc.Int32FromInt32(JT_LTORJ))
			libc.Xmemset(tls, pSubSrc+8+uintptr(i)*48, 0, uint32(48))
			goto _8
		_8:
			;
			i = i + 1
		}
		v5 = pSubitem + 12
		*(*Tu8)(unsafe.Pointer(v5)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v5))) | int32(jointype))
		/* Begin substituting subquery result set expressions for
		 ** references to the iParent in the outer query.
		 **
		 ** Example:
		 **
		 **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
		 **   \                     \_____________ subquery __________/          /
		 **    \_____________________ outer query ______________________________/
		 **
		 ** We look at every expression in the outer query and every place we see
		 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
		 */
		if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != 0 {
			/* At this point, any non-zero iOrderByCol values indicate that the
			 ** ORDER BY column expression is identical to the iOrderByCol'th
			 ** expression returned by SELECT statement pSub. Since these values
			 ** do not necessarily correspond to columns in SELECT statement pParent,
			 ** zero them before transferring the ORDER BY clause.
			 **
			 ** Not doing this may cause an error if a subsequent call to this
			 ** function attempts to flatten a compound sub-query into pParent.
			 ** See ticket [d11a6e908f].
			 */
			pOrderBy1 = (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy
			i = 0
			for {
				if !(i < (*TExprList)(unsafe.Pointer(pOrderBy1)).FnExpr) {
					break
				}
				*(*Tu16)(unsafe.Pointer(pOrderBy1 + 8 + uintptr(i)*20 + 16)) = uint16(0)
				goto _11
			_11:
				;
				i = i + 1
			}
			(*TSelect)(unsafe.Pointer(pParent)).FpOrderBy = pOrderBy1
			(*TSelect)(unsafe.Pointer(pSub)).FpOrderBy = uintptr(0)
		}
		pWhere = (*TSelect)(unsafe.Pointer(pSub)).FpWhere
		(*TSelect)(unsafe.Pointer(pSub)).FpWhere = uintptr(0)
		if isOuterJoin > 0 {
			_sqlite3SetJoinExpr(tls, pWhere, iNewParent, uint32(EP_OuterON))
		}
		if pWhere != 0 {
			if (*TSelect)(unsafe.Pointer(pParent)).FpWhere != 0 {
				(*TSelect)(unsafe.Pointer(pParent)).FpWhere = _sqlite3PExpr(tls, pParse, int32(TK_AND), pWhere, (*TSelect)(unsafe.Pointer(pParent)).FpWhere)
			} else {
				(*TSelect)(unsafe.Pointer(pParent)).FpWhere = pWhere
			}
		}
		if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
			(**(**TSubstContext)(__ccgo_up(bp + 28))).FpParse = pParse
			(**(**TSubstContext)(__ccgo_up(bp + 28))).FiTable = iParent
			(**(**TSubstContext)(__ccgo_up(bp + 28))).FiNewTable = iNewParent
			(**(**TSubstContext)(__ccgo_up(bp + 28))).FisOuterJoin = isOuterJoin
			(**(**TSubstContext)(__ccgo_up(bp + 28))).FnSelDepth = 0
			(**(**TSubstContext)(__ccgo_up(bp + 28))).FpEList = (*TSelect)(unsafe.Pointer(pSub)).FpEList
			(**(**TSubstContext)(__ccgo_up(bp + 28))).FpCList = _findLeftmostExprlist(tls, pSub)
			_substSelect(tls, bp+28, pParent, 0)
		}
		/* The flattened query is a compound if either the inner or the
		 ** outer query is a compound. */
		**(**Tu32)(__ccgo_up(pParent + 4)) |= (*TSelect)(unsafe.Pointer(pSub)).FselFlags & uint32(SF_Compound)
		/* restriction (17b) */
		/*
		 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
		 **
		 ** One is tempted to try to add a and b to combine the limits.  But this
		 ** does not work if either limit is negative.
		 */
		if (*TSelect)(unsafe.Pointer(pSub)).FpLimit != 0 {
			(*TSelect)(unsafe.Pointer(pParent)).FpLimit = (*TSelect)(unsafe.Pointer(pSub)).FpLimit
			(*TSelect)(unsafe.Pointer(pSub)).FpLimit = uintptr(0)
		}
		/* Recompute the SrcItem.colUsed masks for the flattened
		 ** tables. */
		i = 0
		for {
			if !(i < nSubSrc) {
				break
			}
			_recomputeColumnsUsed(tls, pParent, pSrc+8+uintptr(i+iFrom)*48)
			goto _12
		_12:
			;
			i = i + 1
		}
		goto _7
	_7:
		;
		pParent = (*TSelect)(unsafe.Pointer(pParent)).FpPrior
		pSub = (*TSelect)(unsafe.Pointer(pSub)).FpPrior
	}
	/* Finally, delete what is left of the subquery and return success.
	 */
	_sqlite3AggInfoPersistWalkerInit(tls, bp, pParse)
	_sqlite3WalkSelect(tls, bp, pSub1)
	_sqlite3SelectDelete(tls, db, pSub1)
	return int32(1)
}

// C documentation
//
//	/*
//	** This function is used to add page iPage to the database file free-list.
//	** It is assumed that the page is not already a part of the free-list.
//	**
//	** The value passed as the second argument to this function is optional.
//	** If the caller happens to have a pointer to the MemPage object
//	** corresponding to page iPage handy, it may pass it as the second value.
//	** Otherwise, it may pass NULL.
//	**
//	** If a pointer to a MemPage object is passed as the second argument,
//	** its reference count is not altered by this function.
//	*/
func _freePage2(tls *libc.TLS, pBt uintptr, pMemPage uintptr, iPage TPgno) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iTrunk TPgno
	var nFree, nLeaf Tu32
	var pPage1 uintptr
	var v1, v3 int32
	var v2, v4 bool
	var _ /* pPage at bp+4 */ uintptr
	var _ /* pTrunk at bp+0 */ uintptr
	var _ /* rc at bp+8 */ int32
	_, _, _, _, _, _, _, _ = iTrunk, nFree, nLeaf, pPage1, v1, v2, v3, v4
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)          /* Free-list trunk page */
	iTrunk = uint32(0)                                 /* Page number of free-list trunk page */
	pPage1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 /* Initial number of pages on free-list */
	if iPage < uint32(2) || iPage > (*TBtShared)(unsafe.Pointer(pBt)).FnPage {
		return _sqlite3CorruptError(tls, int32(80075))
	}
	if pMemPage != 0 {
		**(**uintptr)(__ccgo_up(bp + 4)) = pMemPage
		_sqlite3PagerRef(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpDbPage)
	} else {
		**(**uintptr)(__ccgo_up(bp + 4)) = _btreePageLookup(tls, pBt, iPage)
	}
	/* Increment the free page count on pPage1 */
	**(**int32)(__ccgo_up(bp + 8)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FpDbPage)
	if **(**int32)(__ccgo_up(bp + 8)) != 0 {
		goto freepage_out
	}
	nFree = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+36)
	_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+36, nFree+uint32(1))
	if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_SECURE_DELETE) != 0 {
		/* If the secure_delete option is enabled, then
		 ** always fully overwrite deleted information with zeros.
		 */
		if v2 = !(**(**uintptr)(__ccgo_up(bp + 4)) != 0); v2 {
			v1 = _btreeGetPage(tls, pBt, iPage, bp+4, 0)
			**(**int32)(__ccgo_up(bp + 8)) = v1
		}
		if v4 = v2 && v1 != 0; !v4 {
			v3 = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpDbPage)
			**(**int32)(__ccgo_up(bp + 8)) = v3
		}
		if v4 || v3 != 0 {
			goto freepage_out
		}
		libc.Xmemset(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FaData, 0, (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpBt)).FpageSize)
	}
	/* If the database supports auto-vacuum, write an entry in the pointer-map
	 ** to indicate that the page is free.
	 */
	if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
		_ptrmapPut(tls, pBt, iPage, uint8(PTRMAP_FREEPAGE), uint32(0), bp+8)
		if **(**int32)(__ccgo_up(bp + 8)) != 0 {
			goto freepage_out
		}
	}
	/* Now manipulate the actual database free-list structure. There are two
	 ** possibilities. If the free-list is currently empty, or if the first
	 ** trunk page in the free-list is full, then this page will become a
	 ** new free-list trunk page. Otherwise, it will become a leaf of the
	 ** first trunk page in the current free-list. This block tests if it
	 ** is possible to add the page as a new free-list leaf.
	 */
	if nFree != uint32(0) { /* Initial number of leaf cells on trunk page */
		iTrunk = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32)
		if iTrunk > _btreePagecount(tls, pBt) {
			**(**int32)(__ccgo_up(bp + 8)) = _sqlite3CorruptError(tls, int32(80122))
			goto freepage_out
		}
		**(**int32)(__ccgo_up(bp + 8)) = _btreeGetPage(tls, pBt, iTrunk, bp, 0)
		if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
			goto freepage_out
		}
		nLeaf = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData+4)
		if nLeaf > (*TBtShared)(unsafe.Pointer(pBt)).FusableSize/uint32(4)-uint32(2) {
			**(**int32)(__ccgo_up(bp + 8)) = _sqlite3CorruptError(tls, int32(80133))
			goto freepage_out
		}
		if nLeaf < (*TBtShared)(unsafe.Pointer(pBt)).FusableSize/uint32(4)-uint32(8) {
			/* In this case there is room on the trunk page to insert the page
			 ** being freed as a new leaf.
			 **
			 ** Note that the trunk page is not really full until it contains
			 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
			 ** coded.  But due to a coding error in versions of SQLite prior to
			 ** 3.6.0, databases with freelist trunk pages holding more than
			 ** usableSize/4 - 8 entries will be reported as corrupt.  In order
			 ** to maintain backwards compatibility with older versions of SQLite,
			 ** we will continue to restrict the number of entries to usableSize/4 - 8
			 ** for now.  At some point in the future (once everyone has upgraded
			 ** to 3.6.0 or later) we should consider fixing the conditional above
			 ** to read "usableSize/4-2" instead of "usableSize/4-8".
			 **
			 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
			 ** avoid using the last six entries in the freelist trunk page array in
			 ** order that database files created by newer versions of SQLite can be
			 ** read by older versions of SQLite.
			 */
			**(**int32)(__ccgo_up(bp + 8)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpDbPage)
			if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK {
				_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData+4, nLeaf+uint32(1))
				_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData+uintptr(uint32(8)+nLeaf*uint32(4)), iPage)
				if **(**uintptr)(__ccgo_up(bp + 4)) != 0 && int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_SECURE_DELETE) == 0 {
					_sqlite3PagerDontWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpDbPage)
				}
				**(**int32)(__ccgo_up(bp + 8)) = _btreeSetHasContent(tls, pBt, iPage)
			}
			goto freepage_out
		}
	}
	/* If control flows to this point, then it was not possible to add the
	 ** the page being freed as a leaf page of the first trunk in the free-list.
	 ** Possibly because the free-list is empty, or possibly because the
	 ** first trunk in the free-list is full. Either way, the page being freed
	 ** will become the new first trunk page in the free-list.
	 */
	if v2 = **(**uintptr)(__ccgo_up(bp + 4)) == uintptr(0); v2 {
		v1 = _btreeGetPage(tls, pBt, iPage, bp+4, 0)
		**(**int32)(__ccgo_up(bp + 8)) = v1
	}
	if v2 && SQLITE_OK != v1 {
		goto freepage_out
	}
	**(**int32)(__ccgo_up(bp + 8)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpDbPage)
	if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
		goto freepage_out
	}
	_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FaData, iTrunk)
	_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FaData+4, uint32(0))
	_sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, iPage)
	goto freepage_out
freepage_out:
	;
	if **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
		(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FisInit = uint8(0)
	}
	_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 4)))
	_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
	return **(**int32)(__ccgo_up(bp + 8))
}

// C documentation
//
//	/*
//	** Return a section of the pPage->aData to the freelist.
//	** The first byte of the new free block is pPage->aData[iStart]
//	** and the size of the block is iSize bytes.
//	**
//	** Adjacent freeblocks are coalesced.
//	**
//	** Even though the freeblock list was checked by btreeComputeFreeSpace(),
//	** that routine will not detect overlap between cells or freeblocks.  Nor
//	** does it detect cells or freeblocks that encroach into the reserved bytes
//	** at the end of the page.  So do additional corruption checks inside this
//	** routine and return SQLITE_CORRUPT if any problems are found.
//	*/
func _freeSpace(tls *libc.TLS, pPage uintptr, iStart int32, iSize int32) (r int32) {
	var data, pTmp, v2 uintptr
	var hdr Tu8
	var iEnd, iFreeBlk, iOrigSize, iPtr, iPtrEnd, nFrag, x, v1 int32
	_, _, _, _, _, _, _, _, _, _, _, _ = data, hdr, iEnd, iFreeBlk, iOrigSize, iPtr, iPtrEnd, nFrag, pTmp, x, v1, v2 /* Page header size.  0 or 100 */
	nFrag = 0                                                                                                        /* Reduction in fragmentation */
	iOrigSize = iSize                                                                                                /* Offset to cell content area */
	iEnd = iStart + iSize                                                                                            /* First byte past the iStart buffer */
	data = (*TMemPage)(unsafe.Pointer(pPage)).FaData                                                                 /* Temporary ptr into data[] */
	/* Minimum cell size is 4 */
	/* The list of freeblocks must be in ascending order.  Find the
	 ** spot on the list where iStart should be inserted.
	 */
	hdr = (*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset
	iPtr = int32(hdr) + int32(1)
	if int32(**(**uint8)(__ccgo_up(data + uintptr(iPtr+int32(1))))) == 0 && int32(**(**uint8)(__ccgo_up(data + uintptr(iPtr)))) == 0 {
		iFreeBlk = 0 /* Shortcut for the case when the freelist is empty */
	} else {
		for {
			v1 = int32(**(**uint8)(__ccgo_up(data + uintptr(iPtr))))<<libc.Int32FromInt32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(iPtr) + 1)))
			iFreeBlk = v1
			if !(v1 < iStart) {
				break
			}
			if iFreeBlk <= iPtr {
				if iFreeBlk == 0 {
					break
				} /* TH3: corrupt082.100 */
				return _sqlite3CorruptError(tls, int32(75174))
			}
			iPtr = iFreeBlk
		}
		if iFreeBlk > int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize)-int32(4) { /* TH3: corrupt081.100 */
			return _sqlite3CorruptError(tls, int32(75179))
		}
		/* At this point:
		 **    iFreeBlk:   First freeblock after iStart, or zero if none
		 **    iPtr:       The address of a pointer to iFreeBlk
		 **
		 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
		 */
		if iFreeBlk != 0 && iEnd+int32(3) >= iFreeBlk {
			nFrag = iFreeBlk - iEnd
			if iEnd > iFreeBlk {
				return _sqlite3CorruptError(tls, int32(75191))
			}
			iEnd = iFreeBlk + (int32(**(**uint8)(__ccgo_up(data + uintptr(iFreeBlk+int32(2)))))<<int32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(iFreeBlk+int32(2)) + 1))))
			if iEnd > int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) {
				return _sqlite3CorruptError(tls, int32(75194))
			}
			iSize = iEnd - iStart
			iFreeBlk = int32(**(**uint8)(__ccgo_up(data + uintptr(iFreeBlk))))<<int32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(iFreeBlk) + 1)))
		}
		/* If iPtr is another freeblock (that is, if iPtr is not the freelist
		 ** pointer in the page header) then check to see if iStart should be
		 ** coalesced onto the end of iPtr.
		 */
		if iPtr > int32(hdr)+int32(1) {
			iPtrEnd = iPtr + (int32(**(**uint8)(__ccgo_up(data + uintptr(iPtr+int32(2)))))<<int32(8) | int32(**(**uint8)(__ccgo_up(data + uintptr(iPtr+int32(2)) + 1))))
			if iPtrEnd+int32(3) >= iStart {
				if iPtrEnd > iStart {
					return _sqlite3CorruptError(tls, int32(75207))
				}
				nFrag = nFrag + (iStart - iPtrEnd)
				iSize = iEnd - iPtr
				iStart = iPtr
			}
		}
		if nFrag > int32(**(**uint8)(__ccgo_up(data + uintptr(int32(hdr)+int32(7))))) {
			return _sqlite3CorruptError(tls, int32(75213))
		}
		v2 = data + uintptr(int32(hdr)+int32(7))
		*(*uint8)(unsafe.Pointer(v2)) = uint8(int32(*(*uint8)(unsafe.Pointer(v2))) - int32(uint8(nFrag)))
	}
	pTmp = data + uintptr(int32(hdr)+int32(5))
	x = int32(**(**Tu8)(__ccgo_up(pTmp)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(pTmp + 1)))
	if int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FbtsFlags)&int32(BTS_FAST_SECURE) != 0 {
		/* Overwrite deleted information with zeros when the secure_delete
		 ** option is enabled */
		libc.Xmemset(tls, data+uintptr(iStart), 0, uint32(iSize))
	}
	if iStart <= x {
		/* The new freeblock is at the beginning of the cell content area,
		 ** so just extend the cell content area rather than create another
		 ** freelist entry */
		if iStart < x {
			return _sqlite3CorruptError(tls, int32(75227))
		}
		if iPtr != int32(hdr)+int32(1) {
			return _sqlite3CorruptError(tls, int32(75228))
		}
		**(**uint8)(__ccgo_up(data + uintptr(int32(hdr)+int32(1)))) = uint8(iFreeBlk >> libc.Int32FromInt32(8))
		**(**uint8)(__ccgo_up(data + uintptr(int32(hdr)+int32(1)) + 1)) = uint8(iFreeBlk)
		**(**uint8)(__ccgo_up(data + uintptr(int32(hdr)+int32(5)))) = uint8(iEnd >> libc.Int32FromInt32(8))
		**(**uint8)(__ccgo_up(data + uintptr(int32(hdr)+int32(5)) + 1)) = uint8(iEnd)
	} else {
		/* Insert the new freeblock into the freelist */
		**(**uint8)(__ccgo_up(data + uintptr(iPtr))) = uint8(iStart >> libc.Int32FromInt32(8))
		**(**uint8)(__ccgo_up(data + uintptr(iPtr) + 1)) = uint8(iStart)
		**(**uint8)(__ccgo_up(data + uintptr(iStart))) = uint8(iFreeBlk >> libc.Int32FromInt32(8))
		**(**uint8)(__ccgo_up(data + uintptr(iStart) + 1)) = uint8(iFreeBlk)
		**(**uint8)(__ccgo_up(data + uintptr(iStart+int32(2)))) = uint8(int32(uint16(iSize)) >> libc.Int32FromInt32(8))
		**(**uint8)(__ccgo_up(data + uintptr(iStart+int32(2)) + 1)) = uint8(uint16(iSize))
	}
	**(**int32)(__ccgo_up(pPage + 20)) += iOrigSize
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
//	** be implemented as a co-routine.  The i-th entry is guaranteed to be
//	** a subquery.
//	**
//	** The subquery is implemented as a co-routine if all of the following are
//	** true:
//	**
//	**    (1)  The subquery will likely be implemented in the outer loop of
//	**         the query.  This will be the case if any one of the following
//	**         conditions hold:
//	**         (a)  The subquery is the only term in the FROM clause
//	**         (b)  The subquery is the left-most term and a CROSS JOIN or similar
//	**              requires it to be the outer loop
//	**         (c)  All of the following are true:
//	**                (i) The subquery is the left-most subquery in the FROM clause
//	**               (ii) There is nothing that would prevent the subquery from
//	**                    being used as the outer loop if the sqlite3WhereBegin()
//	**                    routine nominates it to that position.
//	**              (iii) The query is not a UPDATE ... FROM
//	**    (2)  The subquery is not a CTE that should be materialized because
//	**         (a) the AS MATERIALIZED keyword is used, or
//	**         (b) the CTE is used multiple times and does not have the
//	**             NOT MATERIALIZED keyword
//	**    (3)  The subquery is not part of a left operand for a RIGHT JOIN
//	**    (4)  The SQLITE_Coroutine optimization disable flag is not set
//	**    (5)  The subquery is not self-joined
//	*/
func _fromClauseTermCanBeCoroutine(tls *libc.TLS, pParse uintptr, pTabList uintptr, i int32, selFlags int32) (r int32) {
	var pCteUse, pItem uintptr
	_, _ = pCteUse, pItem
	pItem = pTabList + 8 + uintptr(i)*48
	if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x200>>9) != 0 {
		pCteUse = *(*uintptr)(unsafe.Pointer(pItem + 36))
		if int32((*TCteUse)(unsafe.Pointer(pCteUse)).FeM10d) == M10d_Yes {
			return 0
		} /* (2a) */
		if (*TCteUse)(unsafe.Pointer(pCteUse)).FnUse >= int32(2) && int32((*TCteUse)(unsafe.Pointer(pCteUse)).FeM10d) != int32(M10d_No) {
			return 0
		} /* (2b) */
	}
	if int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
		return 0
	} /* (3)  */
	if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Coroutines)) != uint32(0) {
		return 0
	} /* (4)  */
	if _isSelfJoinView(tls, pTabList, pItem, i+int32(1), (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) != uintptr(0) {
		return 0 /* (5) */
	}
	if i == 0 {
		if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc == int32(1) {
			return int32(1)
		} /* (1a) */
		if int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + 1*48))).Ffg.Fjointype)&int32(JT_CROSS) != 0 {
			return int32(1)
		} /* (1b) */
		if selFlags&int32(SF_UpdateFrom) != 0 {
			return 0
		} /* (1c-iii) */
		return int32(1)
	}
	if selFlags&int32(SF_UpdateFrom) != 0 {
		return 0
	} /* (1c-iii) */
	for int32(1) != 0 {
		if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)) != 0 {
			return 0
		} /* (1c-ii) */
		if i == 0 {
			break
		}
		i = i - 1
		pItem -= 48
		if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4>>2) != 0 {
			return 0
		} /* (1c-i) */
	}
	return int32(1)
}

func _fts5ApiCallback(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iCsrId Ti64
	var pAux, pCsr, pTab uintptr
	_, _, _, _ = iCsrId, pAux, pCsr, pTab
	pAux = Xsqlite3_user_data(tls, context)
	iCsrId = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv)))
	pCsr = _fts5CursorFromCsrid(tls, (*TFts5Auxiliary)(unsafe.Pointer(pAux)).FpGlobal, iCsrId)
	if pCsr == uintptr(0) || ((*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan == 0 || (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan == int32(FTS5_PLAN_SPECIAL)) {
		_fts5ResultError(tls, context, __ccgo_ts+41659, libc.VaList(bp+8, iCsrId))
	} else {
		pTab = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab
		_fts5ApiInvoke(tls, pAux, pCsr, context, argc-int32(1), argv+1*4)
		Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer(pTab)).FzErrMsg)
		(*Tsqlite3_vtab)(unsafe.Pointer(pTab)).FzErrMsg = uintptr(0)
	}
}

// C documentation
//
//	/*
//	** The xColumnLocale() API.
//	*/
func _fts5ApiColumnLocale(tls *libc.TLS, pCtx uintptr, iCol int32, pzLocale uintptr, pnLocale uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pConfig, pCsr uintptr
	var rc int32
	var _ /* nDummy at bp+4 */ int32
	var _ /* zDummy at bp+0 */ uintptr
	_, _, _ = pConfig, pCsr, rc
	rc = SQLITE_OK
	pCsr = pCtx
	pConfig = (*TFts5Table)(unsafe.Pointer((*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab)).FpConfig
	**(**uintptr)(__ccgo_up(pzLocale)) = uintptr(0)
	**(**int32)(__ccgo_up(pnLocale)) = 0
	if iCol < 0 || iCol >= (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol {
		rc = int32(SQLITE_RANGE)
	} else {
		if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr(iCol)))) == 0 && 0 == _fts5IsContentless(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab, int32(1)) && (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale != 0 {
			rc = _fts5SeekCursor(tls, pCsr, 0)
			if rc == SQLITE_OK {
				**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
				**(**int32)(__ccgo_up(bp + 4)) = 0
				rc = _fts5TextFromStmt(tls, pConfig, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, iCol, bp, bp+4)
				if rc == SQLITE_OK {
					**(**uintptr)(__ccgo_up(pzLocale)) = (*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FpLocale
					**(**int32)(__ccgo_up(pnLocale)) = (*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FnLocale
				}
				_sqlite3Fts5ClearLocale(tls, pConfig)
			}
		}
	}
	return rc
}

func _fts5ApiColumnSize(tls *libc.TLS, pCtx uintptr, iCol int32, pnToken uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, i1, i2, rc int32
	var iRowid Ti64
	var pConfig, pCsr, pTab uintptr
	var _ /* n at bp+4 */ int32
	var _ /* z at bp+0 */ uintptr
	_, _, _, _, _, _, _, _ = i, i1, i2, iRowid, pConfig, pCsr, pTab, rc
	pCsr = pCtx
	pTab = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab
	pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig
	rc = SQLITE_OK
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fcsrflags&int32(FTS5CSR_REQUIRE_DOCSIZE) != 0 {
		if (*TFts5Config)(unsafe.Pointer(pConfig)).FbColumnsize != 0 {
			iRowid = _fts5CursorRowid(tls, pCsr)
			rc = _sqlite3Fts5StorageDocsize(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, iRowid, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaColumnSize)
		} else {
			if !((*TFts5Config)(unsafe.Pointer(pConfig)).FzContent != 0) || (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == int32(FTS5_CONTENT_UNINDEXED) {
				i = 0
				for {
					if !(i < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
						break
					}
					if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr(i)))) == 0 {
						**(**int32)(__ccgo_up((*TFts5Cursor)(unsafe.Pointer(pCsr)).FaColumnSize + uintptr(i)*4)) = -int32(1)
					}
					goto _1
				_1:
					;
					i = i + 1
				}
			} else {
				rc = _fts5SeekCursor(tls, pCsr, 0)
				i1 = 0
				for {
					if !(rc == SQLITE_OK && i1 < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
						break
					}
					if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr(i1)))) == 0 {
						**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
						**(**int32)(__ccgo_up(bp + 4)) = 0
						**(**int32)(__ccgo_up((*TFts5Cursor)(unsafe.Pointer(pCsr)).FaColumnSize + uintptr(i1)*4)) = 0
						rc = _fts5TextFromStmt(tls, pConfig, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, i1, bp, bp+4)
						if rc == SQLITE_OK {
							rc = _sqlite3Fts5Tokenize(tls, pConfig, int32(FTS5_TOKENIZE_AUX), **(**uintptr)(__ccgo_up(bp)), **(**int32)(__ccgo_up(bp + 4)), (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaColumnSize+uintptr(i1)*4, __ccgo_fp(_fts5ColumnSizeCb))
						}
						_sqlite3Fts5ClearLocale(tls, pConfig)
					}
					goto _2
				_2:
					;
					i1 = i1 + 1
				}
			}
		}
		**(**int32)(__ccgo_up(pCsr + 60)) &= ^libc.Int32FromInt32(FTS5CSR_REQUIRE_DOCSIZE)
	}
	if iCol < 0 {
		**(**int32)(__ccgo_up(pnToken)) = 0
		i2 = 0
		for {
			if !(i2 < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
				break
			}
			**(**int32)(__ccgo_up(pnToken)) += **(**int32)(__ccgo_up((*TFts5Cursor)(unsafe.Pointer(pCsr)).FaColumnSize + uintptr(i2)*4))
			goto _3
		_3:
			;
			i2 = i2 + 1
		}
	} else {
		if iCol < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol {
			**(**int32)(__ccgo_up(pnToken)) = **(**int32)(__ccgo_up((*TFts5Cursor)(unsafe.Pointer(pCsr)).FaColumnSize + uintptr(iCol)*4))
		} else {
			**(**int32)(__ccgo_up(pnToken)) = 0
			rc = int32(SQLITE_RANGE)
		}
	}
	return rc
}

func _fts5ApiPhraseFirstColumn(tls *libc.TLS, pCtx uintptr, iPhrase int32, pIter uintptr, piCol uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i1, rc, v1 int32
	var pConfig, pCsr, pSorter, v2 uintptr
	var _ /* n at bp+0 */ int32
	var _ /* n at bp+4 */ int32
	_, _, _, _, _, _, _ = i1, pConfig, pCsr, pSorter, rc, v1, v2
	rc = SQLITE_OK
	pCsr = pCtx
	pConfig = (*TFts5Table)(unsafe.Pointer((*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab)).FpConfig
	if (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail == int32(FTS5_DETAIL_COLUMNS) {
		pSorter = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter
		if pSorter != 0 {
			if iPhrase == 0 {
				v1 = 0
			} else {
				v1 = *(*int32)(unsafe.Pointer(pSorter + 24 + uintptr(iPhrase-int32(1))*4))
			}
			i1 = v1
			**(**int32)(__ccgo_up(bp)) = *(*int32)(unsafe.Pointer(pSorter + 24 + uintptr(iPhrase)*4)) - i1
			(*TFts5PhraseIter)(unsafe.Pointer(pIter)).Fa = (*TFts5Sorter)(unsafe.Pointer(pSorter)).FaPoslist + uintptr(i1)
		} else {
			rc = _sqlite3Fts5ExprPhraseCollist(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr, iPhrase, pIter, bp)
		}
		if rc == SQLITE_OK {
			if (*TFts5PhraseIter)(unsafe.Pointer(pIter)).Fa != 0 {
				v2 = (*TFts5PhraseIter)(unsafe.Pointer(pIter)).Fa + uintptr(**(**int32)(__ccgo_up(bp)))
			} else {
				v2 = uintptr(0)
			}
			(*TFts5PhraseIter)(unsafe.Pointer(pIter)).Fb = v2
			**(**int32)(__ccgo_up(piCol)) = 0
			_fts5ApiPhraseNextColumn(tls, pCtx, pIter, piCol)
		}
	} else {
		rc = _fts5CsrPoslist(tls, pCsr, iPhrase, pIter, bp+4)
		if rc == SQLITE_OK {
			if (*TFts5PhraseIter)(unsafe.Pointer(pIter)).Fa != 0 {
				v2 = (*TFts5PhraseIter)(unsafe.Pointer(pIter)).Fa + uintptr(**(**int32)(__ccgo_up(bp + 4)))
			} else {
				v2 = uintptr(0)
			}
			(*TFts5PhraseIter)(unsafe.Pointer(pIter)).Fb = v2
			if **(**int32)(__ccgo_up(bp + 4)) <= 0 {
				**(**int32)(__ccgo_up(piCol)) = -int32(1)
			} else {
				if int32(**(**uint8)(__ccgo_up((*TFts5PhraseIter)(unsafe.Pointer(pIter)).Fa))) == int32(0x01) {
					**(**uintptr)(__ccgo_up(pIter)) += uintptr(int32(1) + _sqlite3Fts5GetVarint32(tls, (*TFts5PhraseIter)(unsafe.Pointer(pIter)).Fa+1, piCol))
				} else {
					**(**int32)(__ccgo_up(piCol)) = 0
				}
			}
		}
	}
	return rc
}

func _fts5AppendPoslist(tls *libc.TLS, p uintptr, iDelta Tu64, pMulti uintptr, pBuf uintptr) {
	var nByte, nData, v1 int32
	var v2 bool
	_, _, _, _ = nByte, nData, v1, v2
	nData = (*TFts5Iter)(unsafe.Pointer(pMulti)).Fbase.FnData
	nByte = nData + int32(9) + int32(9) + int32(FTS5_DATA_ZERO_PADDING)
	if v2 = (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK; v2 {
		if uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)+uint32(nByte) <= uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace) {
			v1 = 0
		} else {
			v1 = _sqlite3Fts5BufferSize(tls, p+44, pBuf, uint32(nByte+(*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn))
		}
	}
	if v2 && 0 == v1 {
		**(**int32)(__ccgo_up(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), iDelta)
		**(**int32)(__ccgo_up(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(nData*libc.Int32FromInt32(2)))
		libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), (*TFts5Iter)(unsafe.Pointer(pMulti)).Fbase.FpData, uint32(nData))
		**(**int32)(__ccgo_up(pBuf + 4)) += nData
		libc.Xmemset(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), 0, uint32(FTS5_DATA_ZERO_PADDING))
	}
}

func _fts5AppendRowid(tls *libc.TLS, p uintptr, iDelta Tu64, pUnused uintptr, pBuf uintptr) {
	_ = pUnused
	_sqlite3Fts5BufferAppendVarint(tls, p+44, pBuf, int64(iDelta))
}

// C documentation
//
//	/*
//	** This function appends iterator pAppend to Fts5TokenDataIter pIn and
//	** returns the result.
//	*/
func _fts5AppendTokendataIter(tls *libc.TLS, p uintptr, pIn uintptr, pAppend uintptr) (r uintptr) {
	var nAlloc, nByte, v2 Ti64
	var pNew, pRet, v3 uintptr
	var v1 int64
	_, _, _, _, _, _, _ = nAlloc, nByte, pNew, pRet, v1, v2, v3
	pRet = pIn
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		if pIn == uintptr(0) || (*TFts5TokenDataIter)(unsafe.Pointer(pIn)).FnIter == (*TFts5TokenDataIter)(unsafe.Pointer(pIn)).FnIterAlloc {
			if pIn != 0 {
				v1 = (*TFts5TokenDataIter)(unsafe.Pointer(pIn)).FnIterAlloc * int64(2)
			} else {
				v1 = int64(16)
			}
			nAlloc = v1
			nByte = int64(uint32(libc.UintptrFromInt32(0)+56)) + (nAlloc+libc.Int64FromInt32(1))*libc.Int64FromInt64(80)
			pNew = Xsqlite3_realloc64(tls, pIn, uint64(nByte))
			if pNew == uintptr(0) {
				(*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
			} else {
				if pIn == uintptr(0) {
					libc.Xmemset(tls, pNew, 0, uint32(nByte))
				}
				pRet = pNew
				(*TFts5TokenDataIter)(unsafe.Pointer(pNew)).FnIterAlloc = nAlloc
			}
		}
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
		_fts5IterClose(tls, pAppend)
	} else {
		v3 = pRet + 32
		v2 = *(*Ti64)(unsafe.Pointer(v3))
		*(*Ti64)(unsafe.Pointer(v3)) = *(*Ti64)(unsafe.Pointer(v3)) + 1
		*(*uintptr)(unsafe.Pointer(pRet + 56 + uintptr(v2)*4)) = pAppend
	}
	return pRet
}

// C documentation
//
//	/*
//	** Create an "ascii" tokenizer.
//	*/
func _fts5AsciiCreate(tls *libc.TLS, pUnused uintptr, azArg uintptr, nArg int32, ppOut uintptr) (r int32) {
	var i, rc int32
	var p, zArg uintptr
	_, _, _, _ = i, p, rc, zArg
	rc = SQLITE_OK
	p = uintptr(0)
	_ = pUnused
	if nArg%int32(2) != 0 {
		rc = int32(SQLITE_ERROR)
	} else {
		p = Xsqlite3_malloc64(tls, uint64(128))
		if p == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		} else {
			libc.Xmemset(tls, p, 0, uint32(128))
			libc.Xmemcpy(tls, p, uintptr(unsafe.Pointer(&_aAsciiTokenChar)), uint32(128))
			i = 0
			for {
				if !(rc == SQLITE_OK && i < nArg) {
					break
				}
				zArg = **(**uintptr)(__ccgo_up(azArg + uintptr(i+int32(1))*4))
				if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4)), __ccgo_ts+43071) {
					_fts5AsciiAddExceptions(tls, p, zArg, int32(1))
				} else {
					if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4)), __ccgo_ts+43082) {
						_fts5AsciiAddExceptions(tls, p, zArg, 0)
					} else {
						rc = int32(SQLITE_ERROR)
					}
				}
				goto _1
			_1:
				;
				i = i + int32(2)
			}
			if rc != SQLITE_OK {
				_fts5AsciiDelete(tls, p)
				p = uintptr(0)
			}
		}
	}
	**(**uintptr)(__ccgo_up(ppOut)) = p
	return rc
}

// C documentation
//
//	/*
//	** Implementation of the xBestIndex method for FTS5 tables. Within the
//	** WHERE constraint, it searches for the following:
//	**
//	**   1. A MATCH constraint against the table column.
//	**   2. A MATCH constraint against the "rank" column.
//	**   3. A MATCH constraint against some other column.
//	**   4. An == constraint against the rowid column.
//	**   5. A < or <= constraint against the rowid column.
//	**   6. A > or >= constraint against the rowid column.
//	**
//	** Within the ORDER BY, the following are supported:
//	**
//	**   5. ORDER BY rank [ASC|DESC]
//	**   6. ORDER BY rowid [ASC|DESC]
//	**
//	** Information for the xFilter call is passed via both the idxNum and
//	** idxStr variables. Specifically, idxNum is a bitmask of the following
//	** flags used to encode the ORDER BY clause:
//	**
//	**     FTS5_BI_ORDER_RANK
//	**     FTS5_BI_ORDER_ROWID
//	**     FTS5_BI_ORDER_DESC
//	**
//	** idxStr is used to encode data from the WHERE clause. For each argument
//	** passed to the xFilter method, the following is appended to idxStr:
//	**
//	**   Match against table column:            "m"
//	**   Match against rank column:             "r"
//	**   Match against other column:            "M<column-number>"
//	**   LIKE  against other column:            "L<column-number>"
//	**   GLOB  against other column:            "G<column-number>"
//	**   Equality constraint against the rowid: "="
//	**   A < or <= against the rowid:           "<"
//	**   A > or >= against the rowid:           ">"
//	**
//	** This function ensures that there is at most one "r" or "=". And that if
//	** there exists an "=" then there is no "<" or ">".
//	**
//	** If an unusable MATCH operator is present in the WHERE clause, then
//	** SQLITE_CONSTRAINT is returned.
//	**
//	** Costs are assigned as follows:
//	**
//	**  a) If a MATCH operator is present, the cost depends on the other
//	**     constraints also present. As follows:
//	**
//	**       * No other constraints:         cost=50000.0
//	**       * One rowid range constraint:   cost=37500.0
//	**       * Both rowid range constraints: cost=30000.0
//	**       * An == rowid constraint:       cost=25000.0
//	**
//	**  b) Otherwise, if there is no MATCH:
//	**
//	**       * No other constraints:         cost=3000000.0
//	**       * One rowid range constraints:  cost=2250000.0
//	**       * Both rowid range constraint:  cost=750000.0
//	**       * An == rowid constraint:       cost=25.0
//	**
//	** Costs are not modified by the ORDER BY clause.
//	**
//	** The ratios used in case (a) are based on informal results obtained from
//	** the tool/fts5cost.tcl script. The "MATCH and ==" combination has the
//	** cost set quite high because the query may be a prefix query. Unless
//	** there is a prefix index, prefix queries with rowid constraints are much
//	** more expensive than non-prefix queries with rowid constraints.
//	**
//	** The estimated rows returned is set to the cost/40. For simple queries,
//	** experimental results show that cost/4 might be about right. But for
//	** more complex queries that use multiple terms the number of rows might
//	** be far fewer than this. So we compromise and use cost/40.
//	*/
func _fts5BestIndexMethod(tls *libc.TLS, pVTab uintptr, pInfo uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bSeenEq, bSeenGt, bSeenLt, bSeenRank, i, iCol, iCons, iIdxStr, iSort, idxFlags, nCol, nSeenMatch, op, v2, v3 int32
	var idxStr, p, p1, pConfig, pTab uintptr
	var nEstRows Ti64
	var v15 float64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bSeenEq, bSeenGt, bSeenLt, bSeenRank, i, iCol, iCons, iIdxStr, iSort, idxFlags, idxStr, nCol, nEstRows, nSeenMatch, op, p, p1, pConfig, pTab, v15, v2, v3
	pTab = pVTab
	pConfig = (*TFts5Table)(unsafe.Pointer(pTab)).FpConfig
	nCol = (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol
	idxFlags = 0
	iIdxStr = 0
	iCons = 0
	bSeenEq = 0
	bSeenGt = 0
	bSeenLt = 0
	nSeenMatch = 0
	bSeenRank = 0
	if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock != 0 {
		(*TFts5Table)(unsafe.Pointer(pTab)).Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+40953, 0)
		return int32(SQLITE_ERROR)
	}
	idxStr = Xsqlite3_malloc64(tls, uint64(int64((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FnConstraint)*int64(8)+int64(1)))
	if idxStr == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FidxStr = idxStr
	(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FneedToFreeIdxStr = int32(1)
	i = 0
	for {
		if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FnConstraint) {
			break
		}
		p = (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraint + uintptr(i)*12
		iCol = (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn
		if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_MATCH) || int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_EQ) && iCol >= nCol {
			/* A MATCH operator or equivalent */
			if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fusable) == 0 || iCol < 0 {
				/* As there exists an unusable MATCH constraint this is an
				 ** unusable plan. Return SQLITE_CONSTRAINT. */
				**(**int8)(__ccgo_up(idxStr + uintptr(iIdxStr))) = 0
				return int32(SQLITE_CONSTRAINT)
			} else {
				if iCol == nCol+int32(1) {
					if bSeenRank != 0 {
						goto _1
					}
					v2 = iIdxStr
					iIdxStr = iIdxStr + 1
					**(**int8)(__ccgo_up(idxStr + uintptr(v2))) = int8('r')
					bSeenRank = int32(1)
				} else {
					nSeenMatch = nSeenMatch + 1
					v2 = iIdxStr
					iIdxStr = iIdxStr + 1
					**(**int8)(__ccgo_up(idxStr + uintptr(v2))) = int8('M')
					Xsqlite3_snprintf(tls, int32(6), idxStr+uintptr(iIdxStr), __ccgo_ts+6506, libc.VaList(bp+8, iCol))
					iIdxStr = iIdxStr + int32(libc.Xstrlen(tls, idxStr+uintptr(iIdxStr)))
				}
				iCons = iCons + 1
				v2 = iCons
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v2
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).Fomit = uint8(1)
			}
		} else {
			if (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fusable != 0 {
				if iCol >= 0 && iCol < nCol && _fts5UsePatternMatch(tls, pConfig, p) != 0 {
					v2 = iIdxStr
					iIdxStr = iIdxStr + 1
					if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(FTS5_PATTERN_LIKE) {
						v3 = int32('L')
					} else {
						v3 = int32('G')
					}
					**(**int8)(__ccgo_up(idxStr + uintptr(v2))) = int8(v3)
					Xsqlite3_snprintf(tls, int32(6), idxStr+uintptr(iIdxStr), __ccgo_ts+6506, libc.VaList(bp+8, iCol))
					idxStr = idxStr + uintptr(libc.Xstrlen(tls, idxStr+uintptr(iIdxStr)))
					iCons = iCons + 1
					v2 = iCons
					(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v2
					nSeenMatch = nSeenMatch + 1
				} else {
					if bSeenEq == 0 && int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_EQ) && iCol < 0 {
						v2 = iIdxStr
						iIdxStr = iIdxStr + 1
						**(**int8)(__ccgo_up(idxStr + uintptr(v2))) = int8('=')
						bSeenEq = int32(1)
						iCons = iCons + 1
						v2 = iCons
						(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v2
						(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).Fomit = uint8(1)
					}
				}
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if bSeenEq == 0 {
		i = 0
		for {
			if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FnConstraint) {
				break
			}
			p1 = (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraint + uintptr(i)*12
			if (*Tsqlite3_index_constraint)(unsafe.Pointer(p1)).FiColumn < 0 && (*Tsqlite3_index_constraint)(unsafe.Pointer(p1)).Fusable != 0 {
				op = int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p1)).Fop)
				if op == int32(SQLITE_INDEX_CONSTRAINT_LT) || op == int32(SQLITE_INDEX_CONSTRAINT_LE) {
					if bSeenLt != 0 {
						goto _10
					}
					v2 = iIdxStr
					iIdxStr = iIdxStr + 1
					**(**int8)(__ccgo_up(idxStr + uintptr(v2))) = int8('<')
					iCons = iCons + 1
					v2 = iCons
					(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v2
					bSeenLt = int32(1)
				} else {
					if op == int32(SQLITE_INDEX_CONSTRAINT_GT) || op == int32(SQLITE_INDEX_CONSTRAINT_GE) {
						if bSeenGt != 0 {
							goto _10
						}
						v2 = iIdxStr
						iIdxStr = iIdxStr + 1
						**(**int8)(__ccgo_up(idxStr + uintptr(v2))) = int8('>')
						iCons = iCons + 1
						v2 = iCons
						(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaConstraintUsage + uintptr(i)*8))).FargvIndex = v2
						bSeenGt = int32(1)
					}
				}
			}
			goto _10
		_10:
			;
			i = i + 1
		}
	}
	**(**int8)(__ccgo_up(idxStr + uintptr(iIdxStr))) = int8('\000')
	/* Set idxFlags flags for the ORDER BY clause
	 **
	 ** Note that tokendata=1 tables cannot currently handle "ORDER BY rowid DESC".
	 */
	if (*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FnOrderBy == int32(1) {
		iSort = (**(**Tsqlite3_index_orderby)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaOrderBy))).FiColumn
		if iSort == (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol+int32(1) && nSeenMatch > 0 {
			idxFlags = idxFlags | int32(FTS5_BI_ORDER_RANK)
		} else {
			if iSort == -int32(1) && (!((**(**Tsqlite3_index_orderby)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaOrderBy))).Fdesc != 0) || !((*TFts5Config)(unsafe.Pointer(pConfig)).FbTokendata != 0)) {
				idxFlags = idxFlags | int32(FTS5_BI_ORDER_ROWID)
			}
		}
		if idxFlags&(libc.Int32FromInt32(FTS5_BI_ORDER_RANK)|libc.Int32FromInt32(FTS5_BI_ORDER_ROWID)) != 0 {
			(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).ForderByConsumed = int32(1)
			if (**(**Tsqlite3_index_orderby)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FaOrderBy))).Fdesc != 0 {
				idxFlags = idxFlags | int32(FTS5_BI_ORDER_DESC)
			}
		}
	}
	/* Calculate the estimated cost based on the flags set in idxFlags. */
	if bSeenEq != 0 {
		if nSeenMatch != 0 {
			v15 = float64(25000)
		} else {
			v15 = float64(25)
		}
		(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = v15
		_fts5SetEstimatedRows(tls, pInfo, int64(1))
		_fts5SetUniqueFlag(tls, pInfo)
	} else {
		if nSeenMatch != 0 {
			if bSeenLt != 0 && bSeenGt != 0 {
				(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = float64(50000)
			} else {
				if bSeenLt != 0 || bSeenGt != 0 {
					(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = float64(37500)
				} else {
					(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = float64(50000)
				}
			}
			nEstRows = int64((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost / libc.Float64FromFloat64(40))
			i = int32(1)
			for {
				if !(i < nSeenMatch) {
					break
				}
				**(**float64)(__ccgo_up(pInfo + 40)) *= float64(2.5)
				nEstRows = nEstRows / int64(2)
				goto _16
			_16:
				;
				i = i + 1
			}
		} else {
			if bSeenLt != 0 && bSeenGt != 0 {
				(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = float64(750000)
			} else {
				if bSeenLt != 0 || bSeenGt != 0 {
					(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = float64(2.25e+06)
				} else {
					(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost = float64(3e+06)
				}
			}
			nEstRows = int64((*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FestimatedCost / libc.Float64FromFloat64(4))
		}
		_fts5SetEstimatedRows(tls, pInfo, nEstRows)
	}
	(*Tsqlite3_index_info)(unsafe.Pointer(pInfo)).FidxNum = idxFlags
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Implementation of bm25() function.
//	*/
func _fts5Bm25Function(tls *libc.TLS, pApi uintptr, pFts uintptr, pCtx uintptr, nVal int32, apVal uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var D, b, k1, score, w, v2 float64
	var aFreq uintptr
	var i, rc int32
	var _ /* ic at bp+12 */ int32
	var _ /* io at bp+16 */ int32
	var _ /* ip at bp+8 */ int32
	var _ /* nInst at bp+4 */ int32
	var _ /* nTok at bp+20 */ int32
	var _ /* pData at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _ = D, aFreq, b, i, k1, rc, score, w, v2
	k1 = float64(1.2)                  /* Constant "k1" from BM25 formula */
	b = float64(0.75)                  /* Error code */
	score = float64(0)                 /* Iterator variable */
	**(**int32)(__ccgo_up(bp + 4)) = 0 /* Value returned by xInstCount() */
	D = float64(0)                     /* Total number of tokens in row */
	aFreq = uintptr(0)                 /* Array of phrase freq. for current row */
	/* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
	 ** for each phrase in the query for the current row. */
	rc = _fts5Bm25GetData(tls, pApi, pFts, bp)
	if rc == SQLITE_OK {
		aFreq = (*TFts5Bm25Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaFreq
		libc.Xmemset(tls, aFreq, 0, uint32(8)*uint32((*TFts5Bm25Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnPhrase))
		rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInstCount})))(tls, pFts, bp+4)
	}
	i = 0
	for {
		if !(rc == SQLITE_OK && i < **(**int32)(__ccgo_up(bp + 4))) {
			break
		}
		rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInst})))(tls, pFts, i, bp+8, bp+12, bp+16)
		if rc == SQLITE_OK {
			if nVal > **(**int32)(__ccgo_up(bp + 12)) {
				v2 = Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(apVal + uintptr(**(**int32)(__ccgo_up(bp + 12)))*4)))
			} else {
				v2 = float64(1)
			}
			w = v2
			**(**float64)(__ccgo_up(aFreq + uintptr(**(**int32)(__ccgo_up(bp + 8)))*8)) += w
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	/* Figure out the total size of the current row in tokens. */
	if rc == SQLITE_OK {
		rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnSize})))(tls, pFts, -int32(1), bp+20)
		D = float64(**(**int32)(__ccgo_up(bp + 20)))
	}
	/* Determine and return the BM25 score for the current row. Or, if an
	 ** error has occurred, throw an exception. */
	if rc == SQLITE_OK {
		i = 0
		for {
			if !(i < (*TFts5Bm25Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnPhrase) {
				break
			}
			score = score + float64(**(**float64)(__ccgo_up((*TFts5Bm25Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaIDF + uintptr(i)*8))*(float64(**(**float64)(__ccgo_up(aFreq + uintptr(i)*8))*(k1+libc.Float64FromFloat64(1)))/(**(**float64)(__ccgo_up(aFreq + uintptr(i)*8))+float64(k1*(libc.Float64FromInt32(1)-b+float64(b*D)/(*TFts5Bm25Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Favgdl)))))
			goto _3
		_3:
			;
			i = i + 1
		}
		Xsqlite3_result_double(tls, pCtx, float64(-libc.Float64FromFloat64(1)*score))
	} else {
		Xsqlite3_result_error_code(tls, pCtx, rc)
	}
}

// C documentation
//
//	/*
//	** Set *ppData to point to the Fts5Bm25Data object for the current query.
//	** If the object has not already been allocated, allocate and populate it
//	** now.
//	*/
func _fts5Bm25GetData(tls *libc.TLS, pApi uintptr, pFts uintptr, ppData uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, nPhrase, rc int32
	var idf float64
	var nByte Tsqlite3_int64
	var p uintptr
	var _ /* nHit at bp+16 */ Tsqlite3_int64
	var _ /* nRow at bp+0 */ Tsqlite3_int64
	var _ /* nToken at bp+8 */ Tsqlite3_int64
	_, _, _, _, _, _ = i, idf, nByte, nPhrase, p, rc
	rc = SQLITE_OK /* Object to return */
	p = (*(*func(*libc.TLS, uintptr, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxGetAuxdata})))(tls, pFts, 0)
	if p == uintptr(0) { /* Number of phrases in query */
		**(**Tsqlite3_int64)(__ccgo_up(bp)) = 0 /* Number of rows in table */
		**(**Tsqlite3_int64)(__ccgo_up(bp + 8)) = 0
		/* Allocate the Fts5Bm25Data object */
		nPhrase = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxPhraseCount})))(tls, pFts)
		nByte = int64(uint32(24) + uint32(nPhrase*int32(2))*uint32(8))
		p = Xsqlite3_malloc64(tls, uint64(nByte))
		if p == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		} else {
			libc.Xmemset(tls, p, 0, uint32(nByte))
			(*TFts5Bm25Data)(unsafe.Pointer(p)).FnPhrase = nPhrase
			(*TFts5Bm25Data)(unsafe.Pointer(p)).FaIDF = p + 1*24
			(*TFts5Bm25Data)(unsafe.Pointer(p)).FaFreq = (*TFts5Bm25Data)(unsafe.Pointer(p)).FaIDF + uintptr(nPhrase)*8
		}
		/* Calculate the average document length for this FTS5 table */
		if rc == SQLITE_OK {
			rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxRowCount})))(tls, pFts, bp)
		}
		if rc == SQLITE_OK {
			rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnTotalSize})))(tls, pFts, -int32(1), bp+8)
		}
		if rc == SQLITE_OK {
			(*TFts5Bm25Data)(unsafe.Pointer(p)).Favgdl = float64(**(**Tsqlite3_int64)(__ccgo_up(bp + 8))) / float64(**(**Tsqlite3_int64)(__ccgo_up(bp)))
		}
		/* Calculate an IDF for each phrase in the query */
		i = 0
		for {
			if !(rc == SQLITE_OK && i < nPhrase) {
				break
			}
			**(**Tsqlite3_int64)(__ccgo_up(bp + 16)) = 0
			rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxQueryPhrase})))(tls, pFts, i, bp+16, __ccgo_fp(_fts5CountCb))
			if rc == SQLITE_OK {
				/* Calculate the IDF (Inverse Document Frequency) for phrase i.
				 ** This is done using the standard BM25 formula as found on wikipedia:
				 **
				 **   IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
				 **
				 ** where "N" is the total number of documents in the set and nHit
				 ** is the number that contain at least one instance of the phrase
				 ** under consideration.
				 **
				 ** The problem with this is that if (N < 2*nHit), the IDF is
				 ** negative. Which is undesirable. So the minimum allowable IDF is
				 ** (1e-6) - roughly the same as a term that appears in just over
				 ** half of set of 5,000,000 documents.  */
				idf = libc.Xlog(tls, (float64(**(**Tsqlite3_int64)(__ccgo_up(bp))-**(**Tsqlite3_int64)(__ccgo_up(bp + 16)))+float64(0.5))/(float64(**(**Tsqlite3_int64)(__ccgo_up(bp + 16)))+float64(0.5)))
				if idf <= float64(0) {
					idf = float64(1e-06)
				}
				**(**float64)(__ccgo_up((*TFts5Bm25Data)(unsafe.Pointer(p)).FaIDF + uintptr(i)*8)) = idf
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if rc != SQLITE_OK {
			Xsqlite3_free(tls, p)
		} else {
			rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxSetAuxdata})))(tls, pFts, p, __ccgo_fp(Xsqlite3_free))
		}
		if rc != SQLITE_OK {
			p = uintptr(0)
		}
	}
	**(**uintptr)(__ccgo_up(ppData)) = p
	return rc
}

// C documentation
//
//	/*
//	** Compare the contents of the two buffers using memcmp(). If one buffer
//	** is a prefix of the other, it is considered the lesser.
//	**
//	** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
//	** +ve if pRight is smaller than pLeft. In other words:
//	**
//	**     res = *pLeft - *pRight
//	*/
func _fts5BufferCompare(tls *libc.TLS, pLeft uintptr, pRight uintptr) (r int32) {
	var nCmp, res, v1 int32
	_, _, _ = nCmp, res, v1
	if (*TFts5Buffer)(unsafe.Pointer(pLeft)).Fn < (*TFts5Buffer)(unsafe.Pointer(pRight)).Fn {
		v1 = (*TFts5Buffer)(unsafe.Pointer(pLeft)).Fn
	} else {
		v1 = (*TFts5Buffer)(unsafe.Pointer(pRight)).Fn
	}
	nCmp = v1
	if nCmp <= 0 {
		v1 = 0
	} else {
		v1 = libc.Xmemcmp(tls, (*TFts5Buffer)(unsafe.Pointer(pLeft)).Fp, (*TFts5Buffer)(unsafe.Pointer(pRight)).Fp, uint32(nCmp))
	}
	res = v1
	if res == 0 {
		v1 = (*TFts5Buffer)(unsafe.Pointer(pLeft)).Fn - (*TFts5Buffer)(unsafe.Pointer(pRight)).Fn
	} else {
		v1 = res
	}
	return v1
}

// C documentation
//
//	/*
//	** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
//	** correctly for the current view. Return SQLITE_OK if successful, or an
//	** SQLite error code otherwise.
//	*/
func _fts5CacheInstArray(tls *libc.TLS, pCsr uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aInst, aIter uintptr
	var i, iBest, nCol, nInst, nIter, nNewSize, v3 int32
	var nByte Tsqlite3_int64
	var _ /* a at bp+4 */ uintptr
	var _ /* n at bp+8 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _ = aInst, aIter, i, iBest, nByte, nCol, nInst, nIter, nNewSize, v3
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK /* Number of iterators/phrases */
	nCol = (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer((*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab)).FpConfig)).FnCol
	nIter = _sqlite3Fts5ExprPhraseCount(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr)
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInstIter == uintptr(0) {
		nByte = int64(uint32(24) * uint32(nIter))
		(*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInstIter = _sqlite3Fts5MallocZero(tls, bp, nByte)
	}
	aIter = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInstIter
	if aIter != 0 {
		nInst = 0
		/* Initialize all iterators */
		i = 0
		for {
			if !(i < nIter && **(**int32)(__ccgo_up(bp)) == SQLITE_OK) {
				break
			}
			**(**int32)(__ccgo_up(bp)) = _fts5CsrPoslist(tls, pCsr, i, bp+4, bp+8)
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				_sqlite3Fts5PoslistReaderInit(tls, **(**uintptr)(__ccgo_up(bp + 4)), **(**int32)(__ccgo_up(bp + 8)), aIter+uintptr(i)*24)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			for int32(1) != 0 {
				iBest = -int32(1)
				i = 0
				for {
					if !(i < nIter) {
						break
					}
					if int32((**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).FbEof) == 0 && (iBest < 0 || (**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).FiPos < (**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(iBest)*24))).FiPos) {
						iBest = i
					}
					goto _2
				_2:
					;
					i = i + 1
				}
				if iBest < 0 {
					break
				}
				nInst = nInst + 1
				if nInst >= (*TFts5Cursor)(unsafe.Pointer(pCsr)).FnInstAlloc {
					if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FnInstAlloc != 0 {
						v3 = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FnInstAlloc * int32(2)
					} else {
						v3 = int32(32)
					}
					nNewSize = v3
					aInst = Xsqlite3_realloc64(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInst, uint64(uint32(nNewSize)*uint32(4)*uint32(3)))
					if aInst != 0 {
						(*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInst = aInst
						(*TFts5Cursor)(unsafe.Pointer(pCsr)).FnInstAlloc = nNewSize
					} else {
						nInst = nInst - 1
						**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
						break
					}
				}
				aInst = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInst + uintptr(int32(3)*(nInst-int32(1)))*4
				**(**int32)(__ccgo_up(aInst)) = iBest
				**(**int32)(__ccgo_up(aInst + 1*4)) = int32((**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(iBest)*24))).FiPos >> libc.Int32FromInt32(32) & libc.Int64FromInt32(0x7FFFFFFF))
				**(**int32)(__ccgo_up(aInst + 2*4)) = int32((**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(iBest)*24))).FiPos & libc.Int64FromInt32(0x7FFFFFFF))
				if **(**int32)(__ccgo_up(aInst + 1*4)) >= nCol {
					**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
					break
				}
				_sqlite3Fts5PoslistReaderNext(tls, aIter+uintptr(iBest)*24)
			}
		}
		(*TFts5Cursor)(unsafe.Pointer(pCsr)).FnInstCount = nInst
		**(**int32)(__ccgo_up(pCsr + 60)) &= ^libc.Int32FromInt32(FTS5CSR_REQUIRE_INST)
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** If argument pOrig is NULL, or if (*pRc) is set to anything other than
//	** SQLITE_OK when this function is called, NULL is returned.
//	**
//	** Otherwise, a copy of (*pOrig) is made into memory obtained from
//	** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
//	** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
//	*/
func _fts5CloneColset(tls *libc.TLS, pRc uintptr, pOrig uintptr) (r uintptr) {
	var nByte Tsqlite3_int64
	var pRet uintptr
	_, _ = nByte, pRet
	if pOrig != 0 {
		nByte = int64(libc.Uint32FromInt64(8) * uint32(((*TFts5Colset)(unsafe.Pointer(pOrig)).FnCol+libc.Int32FromInt32(2))/libc.Int32FromInt32(2)))
		pRet = _sqlite3Fts5MallocZero(tls, pRc, nByte)
		if pRet != 0 {
			libc.Xmemcpy(tls, pRet, pOrig, uint32(nByte))
		}
	} else {
		pRet = uintptr(0)
	}
	return pRet
}

// C documentation
//
//	/*
//	** Gobble up the first bareword or quoted word from the input buffer zIn.
//	** Return a pointer to the character immediately following the last in
//	** the gobbled word if successful, or a NULL pointer otherwise (failed
//	** to find close-quote character).
//	**
//	** Before returning, set pzOut to point to a new buffer containing a
//	** nul-terminated, dequoted copy of the gobbled word. If the word was
//	** quoted, *pbQuoted is also set to 1 before returning.
//	**
//	** If *pRc is other than SQLITE_OK when this function is called, it is
//	** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
//	** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
//	** set if a parse error (failed to find close quote) occurs.
//	*/
func _fts5ConfigGobbleWord(tls *libc.TLS, pRc uintptr, zIn uintptr, pzOut uintptr, pbQuoted uintptr) (r uintptr) {
	var ii int32
	var nIn Tsqlite3_int64
	var zOut, zRet uintptr
	_, _, _, _ = ii, nIn, zOut, zRet
	zRet = uintptr(0)
	nIn = int64(libc.Xstrlen(tls, zIn))
	zOut = Xsqlite3_malloc64(tls, uint64(nIn+int64(1)))
	**(**int32)(__ccgo_up(pbQuoted)) = 0
	**(**uintptr)(__ccgo_up(pzOut)) = uintptr(0)
	if zOut == uintptr(0) {
		**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
	} else {
		libc.Xmemcpy(tls, zOut, zIn, uint32(nIn+libc.Int64FromInt32(1)))
		if _fts5_isopenquote(tls, **(**int8)(__ccgo_up(zOut))) != 0 {
			ii = _fts5Dequote(tls, zOut)
			zRet = zIn + uintptr(ii)
			**(**int32)(__ccgo_up(pbQuoted)) = int32(1)
		} else {
			zRet = _fts5ConfigSkipBareword(tls, zIn)
			if zRet != 0 {
				**(**int8)(__ccgo_up(zOut + uintptr(int32(zRet)-int32(zIn)))) = int8('\000')
			}
		}
	}
	if zRet == uintptr(0) {
		Xsqlite3_free(tls, zOut)
	} else {
		**(**uintptr)(__ccgo_up(pzOut)) = zOut
	}
	return zRet
}

// C documentation
//
//	/*
//	** Populate the Fts5Config.zContentExprlist string.
//	*/
func _fts5ConfigMakeExprlist(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i int32
	var _ /* buf at bp+4 */ TFts5Buffer
	var _ /* rc at bp+0 */ int32
	_ = i
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	**(**TFts5Buffer)(__ccgo_up(bp + 4)) = TFts5Buffer{}
	_sqlite3Fts5BufferAppendPrintf(tls, bp, bp+4, __ccgo_ts+39281, libc.VaList(bp+24, (*TFts5Config)(unsafe.Pointer(p)).FzContentRowid))
	if (*TFts5Config)(unsafe.Pointer(p)).FeContent != int32(FTS5_CONTENT_NONE) {
		i = 0
		for {
			if !(i < (*TFts5Config)(unsafe.Pointer(p)).FnCol) {
				break
			}
			if (*TFts5Config)(unsafe.Pointer(p)).FeContent == int32(FTS5_CONTENT_EXTERNAL) {
				_sqlite3Fts5BufferAppendPrintf(tls, bp, bp+4, __ccgo_ts+39286, libc.VaList(bp+24, **(**uintptr)(__ccgo_up((*TFts5Config)(unsafe.Pointer(p)).FazCol + uintptr(i)*4))))
			} else {
				if (*TFts5Config)(unsafe.Pointer(p)).FeContent == FTS5_CONTENT_NORMAL || **(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(p)).FabUnindexed + uintptr(i))) != 0 {
					_sqlite3Fts5BufferAppendPrintf(tls, bp, bp+4, __ccgo_ts+39293, libc.VaList(bp+24, i))
				} else {
					_sqlite3Fts5BufferAppendPrintf(tls, bp, bp+4, __ccgo_ts+39301, 0)
				}
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	if (*TFts5Config)(unsafe.Pointer(p)).FeContent == FTS5_CONTENT_NORMAL && (*TFts5Config)(unsafe.Pointer(p)).FbLocale != 0 {
		i = 0
		for {
			if !(i < (*TFts5Config)(unsafe.Pointer(p)).FnCol) {
				break
			}
			if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(p)).FabUnindexed + uintptr(i)))) == 0 {
				_sqlite3Fts5BufferAppendPrintf(tls, bp, bp+4, __ccgo_ts+39308, libc.VaList(bp+24, i))
			} else {
				_sqlite3Fts5BufferAppendPrintf(tls, bp, bp+4, __ccgo_ts+39301, 0)
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	(*TFts5Config)(unsafe.Pointer(p)).FzContentExprlist = (**(**TFts5Buffer)(__ccgo_up(bp + 4))).Fp
	return **(**int32)(__ccgo_up(bp))
}

func _fts5ConfigParseColumn(tls *libc.TLS, p uintptr, zCol uintptr, zArg uintptr, pzErr uintptr, pbUnindexed uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var rc, v1 int32
	var v2 uintptr
	_, _, _ = rc, v1, v2
	rc = SQLITE_OK
	if 0 == Xsqlite3_stricmp(tls, zCol, __ccgo_ts+39205) || 0 == Xsqlite3_stricmp(tls, zCol, __ccgo_ts+19186) {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39210, libc.VaList(bp+8, zCol))
		rc = int32(SQLITE_ERROR)
	} else {
		if zArg != 0 {
			if 0 == Xsqlite3_stricmp(tls, zArg, __ccgo_ts+39240) {
				**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(p)).FabUnindexed + uintptr((*TFts5Config)(unsafe.Pointer(p)).FnCol))) = uint8(1)
				**(**int32)(__ccgo_up(pbUnindexed)) = int32(1)
			} else {
				**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39250, libc.VaList(bp+8, zArg))
				rc = int32(SQLITE_ERROR)
			}
		}
	}
	v2 = p + 16
	v1 = *(*int32)(unsafe.Pointer(v2))
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	**(**uintptr)(__ccgo_up((*TFts5Config)(unsafe.Pointer(p)).FazCol + uintptr(v1)*4)) = zCol
	return rc
}

// C documentation
//
//	/*
//	** Parse a "special" CREATE VIRTUAL TABLE directive and update
//	** configuration object pConfig as appropriate.
//	**
//	** If successful, object pConfig is updated and SQLITE_OK returned. If
//	** an error occurs, an SQLite error code is returned and an error message
//	** may be left in *pzErr. It is the responsibility of the caller to
//	** eventually free any such error message using sqlite3_free().
//	*/
func _fts5ConfigParseSpecial(tls *libc.TLS, pConfig uintptr, zCmd uintptr, zArg uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var azArg, p, p1, p2, pSpace uintptr
	var bFirst, nByte, nCmd, nPre, v2 int32
	var nArg Tsqlite3_int64
	var _ /* aDetail at bp+4 */ [4]TFts5Enum
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _ = azArg, bFirst, nArg, nByte, nCmd, nPre, p, p1, p2, pSpace, v2
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	nCmd = int32(libc.Xstrlen(tls, zCmd))
	if Xsqlite3_strnicmp(tls, __ccgo_ts+38644, zCmd, nCmd) == 0 {
		nByte = int32(libc.Uint32FromInt64(4) * libc.Uint32FromInt32(FTS5_MAX_PREFIX_INDEXES))
		bFirst = int32(1)
		if (*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix == uintptr(0) {
			(*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix = _sqlite3Fts5MallocZero(tls, bp, int64(nByte))
			if **(**int32)(__ccgo_up(bp)) != 0 {
				return **(**int32)(__ccgo_up(bp))
			}
		}
		p = zArg
		for int32(1) != 0 {
			nPre = 0
			for int32(**(**int8)(__ccgo_up(p))) == int32(' ') {
				p = p + 1
			}
			if bFirst == 0 && int32(**(**int8)(__ccgo_up(p))) == int32(',') {
				p = p + 1
				for int32(**(**int8)(__ccgo_up(p))) == int32(' ') {
					p = p + 1
				}
			} else {
				if int32(**(**int8)(__ccgo_up(p))) == int32('\000') {
					break
				}
			}
			if int32(**(**int8)(__ccgo_up(p))) < int32('0') || int32(**(**int8)(__ccgo_up(p))) > int32('9') {
				**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38651, 0)
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
				break
			}
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix == int32(FTS5_MAX_PREFIX_INDEXES) {
				**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38682, libc.VaList(bp+48, int32(FTS5_MAX_PREFIX_INDEXES)))
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
				break
			}
			for int32(**(**int8)(__ccgo_up(p))) >= int32('0') && int32(**(**int8)(__ccgo_up(p))) <= int32('9') && nPre < int32(1000) {
				nPre = nPre*int32(10) + (int32(**(**int8)(__ccgo_up(p))) - int32('0'))
				p = p + 1
			}
			if nPre <= 0 || nPre >= int32(1000) {
				**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38715, 0)
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
				break
			}
			**(**int32)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix + uintptr((*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix)*4)) = nPre
			(*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix = (*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix + 1
			bFirst = 0
		}
		return **(**int32)(__ccgo_up(bp))
	}
	if Xsqlite3_strnicmp(tls, __ccgo_ts+38752, zCmd, nCmd) == 0 {
		p1 = zArg
		nArg = int64(libc.Xstrlen(tls, zArg) + uint32(1))
		azArg = _sqlite3Fts5MallocZero(tls, bp, int64(libc.Uint32FromInt64(4)+libc.Uint32FromInt32(2))*nArg)
		if azArg != 0 {
			pSpace = azArg + uintptr(nArg)*4
			if (*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FazArg != 0 {
				**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38761, 0)
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
			} else {
				nArg = 0
				for {
					if !(p1 != 0 && **(**int8)(__ccgo_up(p1)) != 0) {
						break
					}
					p2 = _fts5ConfigSkipWhitespace(tls, p1)
					if int32(**(**int8)(__ccgo_up(p2))) == int32('\'') {
						p1 = _fts5ConfigSkipLiteral(tls, p2)
					} else {
						p1 = _fts5ConfigSkipBareword(tls, p2)
					}
					if p1 != 0 {
						libc.Xmemcpy(tls, pSpace, p2, uint32(int32(p1)-int32(p2)))
						**(**uintptr)(__ccgo_up(azArg + uintptr(nArg)*4)) = pSpace
						_sqlite3Fts5Dequote(tls, pSpace)
						pSpace = pSpace + uintptr(int32(p1)-int32(p2)+int32(1))
						p1 = _fts5ConfigSkipWhitespace(tls, p1)
					}
					goto _1
				_1:
					;
					nArg = nArg + 1
				}
				if p1 == uintptr(0) {
					**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38794, 0)
					**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
				} else {
					(*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FazArg = azArg
					(*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FnArg = int32(nArg)
					azArg = uintptr(0)
				}
			}
		}
		Xsqlite3_free(tls, azArg)
		return **(**int32)(__ccgo_up(bp))
	}
	if Xsqlite3_strnicmp(tls, __ccgo_ts+38828, zCmd, nCmd) == 0 {
		if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent != FTS5_CONTENT_NORMAL {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38836, 0)
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
		} else {
			if **(**int8)(__ccgo_up(zArg)) != 0 {
				(*TFts5Config)(unsafe.Pointer(pConfig)).FeContent = int32(FTS5_CONTENT_EXTERNAL)
				(*TFts5Config)(unsafe.Pointer(pConfig)).FzContent = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+38868, libc.VaList(bp+48, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, zArg))
			} else {
				(*TFts5Config)(unsafe.Pointer(pConfig)).FeContent = int32(FTS5_CONTENT_NONE)
			}
		}
		return **(**int32)(__ccgo_up(bp))
	}
	if Xsqlite3_strnicmp(tls, __ccgo_ts+38874, zCmd, nCmd) == 0 {
		if int32(**(**int8)(__ccgo_up(zArg))) != int32('0') && int32(**(**int8)(__ccgo_up(zArg))) != int32('1') || int32(**(**int8)(__ccgo_up(zArg + 1))) != int32('\000') {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38893, 0)
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
		} else {
			(*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete = libc.BoolInt32(int32(**(**int8)(__ccgo_up(zArg))) == int32('1'))
		}
		return **(**int32)(__ccgo_up(bp))
	}
	if Xsqlite3_strnicmp(tls, __ccgo_ts+38936, zCmd, nCmd) == 0 {
		if int32(**(**int8)(__ccgo_up(zArg))) != int32('0') && int32(**(**int8)(__ccgo_up(zArg))) != int32('1') || int32(**(**int8)(__ccgo_up(zArg + 1))) != int32('\000') {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38893, 0)
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
		} else {
			(*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessUnindexed = libc.BoolInt32(int32(**(**int8)(__ccgo_up(zArg))) == int32('1'))
		}
		return **(**int32)(__ccgo_up(bp))
	}
	if Xsqlite3_strnicmp(tls, __ccgo_ts+38958, zCmd, nCmd) == 0 {
		if (*TFts5Config)(unsafe.Pointer(pConfig)).FzContentRowid != 0 {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+38972, 0)
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
		} else {
			(*TFts5Config)(unsafe.Pointer(pConfig)).FzContentRowid = _sqlite3Fts5Strndup(tls, bp, zArg, -int32(1))
		}
		return **(**int32)(__ccgo_up(bp))
	}
	if Xsqlite3_strnicmp(tls, __ccgo_ts+39010, zCmd, nCmd) == 0 {
		if int32(**(**int8)(__ccgo_up(zArg))) != int32('0') && int32(**(**int8)(__ccgo_up(zArg))) != int32('1') || int32(**(**int8)(__ccgo_up(zArg + 1))) != int32('\000') {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39021, 0)
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
		} else {
			(*TFts5Config)(unsafe.Pointer(pConfig)).FbColumnsize = libc.BoolInt32(int32(**(**int8)(__ccgo_up(zArg))) == int32('1'))
		}
		return **(**int32)(__ccgo_up(bp))
	}
	if Xsqlite3_strnicmp(tls, __ccgo_ts+39056, zCmd, nCmd) == 0 {
		if int32(**(**int8)(__ccgo_up(zArg))) != int32('0') && int32(**(**int8)(__ccgo_up(zArg))) != int32('1') || int32(**(**int8)(__ccgo_up(zArg + 1))) != int32('\000') {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39063, 0)
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
		} else {
			(*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale = libc.BoolInt32(int32(**(**int8)(__ccgo_up(zArg))) == int32('1'))
		}
		return **(**int32)(__ccgo_up(bp))
	}
	if Xsqlite3_strnicmp(tls, __ccgo_ts+6883, zCmd, nCmd) == 0 {
		**(**[4]TFts5Enum)(__ccgo_up(bp + 4)) = [4]TFts5Enum{
			0: {
				FzName: __ccgo_ts + 9725,
				FeVal:  int32(FTS5_DETAIL_NONE),
			},
			1: {
				FzName: __ccgo_ts + 20250,
			},
			2: {
				FzName: __ccgo_ts + 39094,
				FeVal:  int32(FTS5_DETAIL_COLUMNS),
			},
			3: {},
		}
		v2 = _fts5ConfigSetEnum(tls, bp+4, zArg, pConfig+68)
		**(**int32)(__ccgo_up(bp)) = v2
		if v2 != 0 {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39102, 0)
		}
		return **(**int32)(__ccgo_up(bp))
	}
	if Xsqlite3_strnicmp(tls, __ccgo_ts+39133, zCmd, nCmd) == 0 {
		if int32(**(**int8)(__ccgo_up(zArg))) != int32('0') && int32(**(**int8)(__ccgo_up(zArg))) != int32('1') || int32(**(**int8)(__ccgo_up(zArg + 1))) != int32('\000') {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39143, 0)
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
		} else {
			(*TFts5Config)(unsafe.Pointer(pConfig)).FbTokendata = libc.BoolInt32(int32(**(**int8)(__ccgo_up(zArg))) == int32('1'))
		}
		return **(**int32)(__ccgo_up(bp))
	}
	**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39177, libc.VaList(bp+48, nCmd, zCmd))
	return int32(SQLITE_ERROR)
}

func _fts5ConfigSetEnum(tls *libc.TLS, aEnum uintptr, zEnum uintptr, peVal uintptr) (r int32) {
	var i, iVal, nEnum, v2 int32
	_, _, _, _ = i, iVal, nEnum, v2
	nEnum = int32(libc.Xstrlen(tls, zEnum))
	iVal = -int32(1)
	i = 0
	for {
		if !((**(**TFts5Enum)(__ccgo_up(aEnum + uintptr(i)*8))).FzName != 0) {
			break
		}
		if Xsqlite3_strnicmp(tls, (**(**TFts5Enum)(__ccgo_up(aEnum + uintptr(i)*8))).FzName, zEnum, nEnum) == 0 {
			if iVal >= 0 {
				return int32(SQLITE_ERROR)
			}
			iVal = (**(**TFts5Enum)(__ccgo_up(aEnum + uintptr(i)*8))).FeVal
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	**(**int32)(__ccgo_up(peVal)) = iVal
	if iVal < 0 {
		v2 = int32(SQLITE_ERROR)
	} else {
		v2 = SQLITE_OK
	}
	return v2
}

func _fts5ConfigSkipLiteral(tls *libc.TLS, pIn uintptr) (r uintptr) {
	var p uintptr
	_ = p
	p = pIn
	switch int32(**(**int8)(__ccgo_up(p))) {
	case int32('n'):
		fallthrough
	case int32('N'):
		if Xsqlite3_strnicmp(tls, __ccgo_ts+1697, p, int32(4)) == 0 {
			p = p + 4
		} else {
			p = uintptr(0)
		}
	case int32('x'):
		fallthrough
	case int32('X'):
		p = p + 1
		if int32(**(**int8)(__ccgo_up(p))) == int32('\'') {
			p = p + 1
			for int32(**(**int8)(__ccgo_up(p))) >= int32('a') && int32(**(**int8)(__ccgo_up(p))) <= int32('f') || int32(**(**int8)(__ccgo_up(p))) >= int32('A') && int32(**(**int8)(__ccgo_up(p))) <= int32('F') || int32(**(**int8)(__ccgo_up(p))) >= int32('0') && int32(**(**int8)(__ccgo_up(p))) <= int32('9') {
				p = p + 1
			}
			if int32(**(**int8)(__ccgo_up(p))) == int32('\'') && 0 == (int32(p)-int32(pIn))%int32(2) {
				p = p + 1
			} else {
				p = uintptr(0)
			}
		} else {
			p = uintptr(0)
		}
	case int32('\''):
		p = p + 1
		for p != 0 {
			if int32(**(**int8)(__ccgo_up(p))) == int32('\'') {
				p = p + 1
				if int32(**(**int8)(__ccgo_up(p))) != int32('\'') {
					break
				}
			}
			p = p + 1
			if int32(**(**int8)(__ccgo_up(p))) == 0 {
				p = uintptr(0)
			}
		}
	default:
		/* maybe a number */
		if int32(**(**int8)(__ccgo_up(p))) == int32('+') || int32(**(**int8)(__ccgo_up(p))) == int32('-') {
			p = p + 1
		}
		for _fts5_isdigit(tls, **(**int8)(__ccgo_up(p))) != 0 {
			p = p + 1
		}
		/* At this point, if the literal was an integer, the parse is
		 ** finished. Or, if it is a floating point value, it may continue
		 ** with either a decimal point or an 'E' character. */
		if int32(**(**int8)(__ccgo_up(p))) == int32('.') && _fts5_isdigit(tls, **(**int8)(__ccgo_up(p + 1))) != 0 {
			p = p + uintptr(2)
			for _fts5_isdigit(tls, **(**int8)(__ccgo_up(p))) != 0 {
				p = p + 1
			}
		}
		if p == pIn {
			p = uintptr(0)
		}
		break
	}
	return p
}

// C documentation
//
//	/*
//	**
//	** This function is called when the user attempts an UPDATE on a contentless
//	** table. Parameter bRowidModified is true if the UPDATE statement modifies
//	** the rowid value. Parameter apVal[] contains the new values for each user
//	** defined column of the fts5 table. pConfig is the configuration object of the
//	** table being updated (guaranteed to be contentless). The contentless_delete=1
//	** and contentless_unindexed=1 options may or may not be set.
//	**
//	** This function returns SQLITE_OK if the UPDATE can go ahead, or an SQLite
//	** error code if it cannot. In this case an error message is also loaded into
//	** pConfig. Output parameter (*pbContent) is set to true if the caller should
//	** update the %_content table only - not the FTS index or any other shadow
//	** table. This occurs when an UPDATE modifies only UNINDEXED columns of the
//	** table.
//	**
//	** An UPDATE may proceed if:
//	**
//	**   * The only columns modified are UNINDEXED columns, or
//	**
//	**   * The contentless_delete=1 option was specified and all of the indexed
//	**     columns (not a subset) have been modified.
//	*/
func _fts5ContentlessUpdate(tls *libc.TLS, pConfig uintptr, apVal uintptr, bRowidModified int32, pbContent uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bSeenIndex, bSeenIndexNC, ii, rc int32
	var v2 uintptr
	_, _, _, _, _ = bSeenIndex, bSeenIndexNC, ii, rc, v2
	bSeenIndex = 0   /* Have seen modified indexed column */
	bSeenIndexNC = 0 /* Have seen unmodified indexed column */
	rc = SQLITE_OK
	ii = 0
	for {
		if !(ii < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
			break
		}
		if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr(ii)))) == 0 {
			if Xsqlite3_value_nochange(tls, **(**uintptr)(__ccgo_up(apVal + uintptr(ii)*4))) != 0 {
				bSeenIndexNC = bSeenIndexNC + 1
			} else {
				bSeenIndex = bSeenIndex + 1
			}
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	if bSeenIndex == 0 && bRowidModified == 0 {
		**(**int32)(__ccgo_up(pbContent)) = int32(1)
	} else {
		if bSeenIndexNC != 0 || (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete == 0 {
			rc = int32(SQLITE_ERROR)
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete != 0 {
				v2 = __ccgo_ts + 41418
			} else {
				v2 = __ccgo_ts + 41478
			}
			_sqlite3Fts5ConfigErrmsg(tls, pConfig, v2, libc.VaList(bp+8, __ccgo_ts+41508, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName))
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Register a new auxiliary function with global context pGlobal.
//	*/
func _fts5CreateAux(tls *libc.TLS, pApi uintptr, zName uintptr, pUserData uintptr, __ccgo_fp_xFunc Tfts5_extension_function, __ccgo_fp_xDestroy uintptr) (r int32) {
	var nByte, nName Tsqlite3_int64
	var pAux, pGlobal uintptr
	var rc int32
	_, _, _, _, _ = nByte, nName, pAux, pGlobal, rc
	pGlobal = pApi
	rc = Xsqlite3_overload_function(tls, (*TFts5Global)(unsafe.Pointer(pGlobal)).Fdb, zName, -int32(1))
	if rc == SQLITE_OK { /* Bytes of space to allocate */
		nName = int64(libc.Xstrlen(tls, zName) + uint32(1))
		nByte = int64(24) + nName
		pAux = Xsqlite3_malloc64(tls, uint64(nByte))
		if pAux != 0 {
			libc.Xmemset(tls, pAux, 0, uint32(nByte))
			(*TFts5Auxiliary)(unsafe.Pointer(pAux)).FzFunc = pAux + 1*24
			libc.Xmemcpy(tls, (*TFts5Auxiliary)(unsafe.Pointer(pAux)).FzFunc, zName, uint32(nName))
			(*TFts5Auxiliary)(unsafe.Pointer(pAux)).FpGlobal = pGlobal
			(*TFts5Auxiliary)(unsafe.Pointer(pAux)).FpUserData = pUserData
			(*TFts5Auxiliary)(unsafe.Pointer(pAux)).FxFunc = __ccgo_fp_xFunc
			(*TFts5Auxiliary)(unsafe.Pointer(pAux)).FxDestroy = __ccgo_fp_xDestroy
			(*TFts5Auxiliary)(unsafe.Pointer(pAux)).FpNext = (*TFts5Global)(unsafe.Pointer(pGlobal)).FpAux
			(*TFts5Global)(unsafe.Pointer(pGlobal)).FpAux = pAux
		} else {
			rc = int32(SQLITE_NOMEM)
		}
	}
	return rc
}

func _fts5CursorFirstSorted(tls *libc.TLS, pTab uintptr, pCsr uintptr, bDesc int32) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var nByte Tsqlite3_int64
	var nPhrase, rc int32
	var pConfig, pSorter, zRank, zRankArgs, v1, v2, v3 uintptr
	_, _, _, _, _, _, _, _, _, _ = nByte, nPhrase, pConfig, pSorter, rc, zRank, zRankArgs, v1, v2, v3
	pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig
	zRank = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank
	zRankArgs = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs
	nPhrase = _sqlite3Fts5ExprPhraseCount(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr)
	nByte = int64(uint32(libc.UintptrFromInt32(0)+20) + uint32((nPhrase+libc.Int32FromInt32(2))/libc.Int32FromInt32(2))*libc.Uint32FromInt64(8))
	pSorter = Xsqlite3_malloc64(tls, uint64(nByte))
	if pSorter == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pSorter, 0, uint32(nByte))
	(*TFts5Sorter)(unsafe.Pointer(pSorter)).FnIdx = nPhrase
	/* TODO: It would be better to have some system for reusing statement
	 ** handles here, rather than preparing a new one for each query. But that
	 ** is not possible as SQLite reference counts the virtual table objects.
	 ** And since the statement required here reads from this very virtual
	 ** table, saving it creates a circular reference.
	 **
	 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
	if zRankArgs != 0 {
		v1 = __ccgo_ts + 17436
	} else {
		v1 = __ccgo_ts + 1711
	}
	if zRankArgs != 0 {
		v2 = zRankArgs
	} else {
		v2 = __ccgo_ts + 1711
	}
	if bDesc != 0 {
		v3 = __ccgo_ts + 40992
	} else {
		v3 = __ccgo_ts + 40997
	}
	rc = _fts5PrepareStatement(tls, pSorter, pConfig, __ccgo_ts+41001, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName, zRank, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName, v1, v2, v3))
	(*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter = pSorter
	if rc == SQLITE_OK {
		(*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr = pCsr
		rc = _fts5SorterNext(tls, pCsr)
		(*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr = uintptr(0)
	}
	if rc != SQLITE_OK {
		Xsqlite3_finalize(tls, (*TFts5Sorter)(unsafe.Pointer(pSorter)).FpStmt)
		Xsqlite3_free(tls, pSorter)
		(*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter = uintptr(0)
	}
	return rc
}

func _fts5CursorParseRank(tls *libc.TLS, pConfig uintptr, pCsr uintptr, pRank uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var rc int32
	var z uintptr
	var _ /* zRank at bp+0 */ uintptr
	var _ /* zRankArgs at bp+4 */ uintptr
	_, _ = rc, z
	rc = SQLITE_OK
	if pRank != 0 {
		z = Xsqlite3_value_text(tls, pRank)
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
		if z == uintptr(0) {
			if Xsqlite3_value_type(tls, pRank) == int32(SQLITE_NULL) {
				rc = int32(SQLITE_ERROR)
			}
		} else {
			rc = _sqlite3Fts5ConfigParseRank(tls, z, bp, bp+4)
		}
		if rc == SQLITE_OK {
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank = **(**uintptr)(__ccgo_up(bp))
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs = **(**uintptr)(__ccgo_up(bp + 4))
			**(**int32)(__ccgo_up(pCsr + 60)) |= int32(FTS5CSR_FREE_ZRANK)
		} else {
			if rc == int32(SQLITE_ERROR) {
				(*Tsqlite3_vtab)(unsafe.Pointer((*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+41121, libc.VaList(bp+16, z))
			}
		}
	} else {
		if (*TFts5Config)(unsafe.Pointer(pConfig)).FzRank != 0 {
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank = (*TFts5Config)(unsafe.Pointer(pConfig)).FzRank
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs = (*TFts5Config)(unsafe.Pointer(pConfig)).FzRankArgs
		} else {
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank = __ccgo_ts + 38623
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs = uintptr(0)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Execute the following SQL:
//	**
//	**     DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
//	*/
func _fts5DataDelete(tls *libc.TLS, p uintptr, iFirst Ti64, iLast Ti64) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pConfig, zSql uintptr
	_, _ = pConfig, zSql
	if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK {
		return
	}
	if (*TFts5Index)(unsafe.Pointer(p)).FpDeleter == uintptr(0) {
		pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
		zSql = Xsqlite3_mprintf(tls, __ccgo_ts+40236, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName))
		if _fts5IndexPrepareStmt(tls, p, p+60, zSql) != 0 {
			return
		}
	}
	Xsqlite3_bind_int64(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleter, int32(1), iFirst)
	Xsqlite3_bind_int64(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleter, int32(2), iLast)
	Xsqlite3_step(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleter)
	(*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleter)
}

// C documentation
//
//	/*
//	** Retrieve a record from the %_data table.
//	**
//	** If an error occurs, NULL is returned and an error left in the
//	** Fts5Index object.
//	*/
func _fts5DataRead(tls *libc.TLS, p uintptr, iRowid Ti64) (r uintptr) {
	var aOut, pBlob, pConfig, pRet, v1 uintptr
	var nAlloc, nByte, szData Ti64
	var rc int32
	_, _, _, _, _, _, _, _, _ = aOut, nAlloc, nByte, pBlob, pConfig, pRet, rc, szData, v1
	pRet = uintptr(0)
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		rc = SQLITE_OK
		if (*TFts5Index)(unsafe.Pointer(p)).FpReader != 0 {
			/* This call may return SQLITE_ABORT if there has been a savepoint
			 ** rollback since it was last used. In this case a new blob handle
			 ** is required.  */
			pBlob = (*TFts5Index)(unsafe.Pointer(p)).FpReader
			(*TFts5Index)(unsafe.Pointer(p)).FpReader = uintptr(0)
			rc = Xsqlite3_blob_reopen(tls, pBlob, iRowid)
			(*TFts5Index)(unsafe.Pointer(p)).FpReader = pBlob
			if rc != SQLITE_OK {
				_fts5IndexCloseReader(tls, p)
			}
			if rc == int32(SQLITE_ABORT) {
				rc = SQLITE_OK
			}
		}
		/* If the blob handle is not open at this point, open it and seek
		 ** to the requested entry.  */
		if (*TFts5Index)(unsafe.Pointer(p)).FpReader == uintptr(0) && rc == SQLITE_OK {
			pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
			rc = Xsqlite3_blob_open(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Index)(unsafe.Pointer(p)).FzDataTbl, __ccgo_ts+40179, iRowid, 0, p+52)
		}
		/* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
		 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
		 ** All the reasons those functions might return SQLITE_ERROR - missing
		 ** table, missing row, non-blob/text in block column - indicate
		 ** backing store corruption.  */
		if rc == int32(SQLITE_ERROR) {
			rc = _fts5IndexCorruptRowid(tls, p, iRowid)
		}
		if rc == SQLITE_OK {
			aOut = uintptr(0) /* Read blob data into this buffer */
			nByte = int64(Xsqlite3_blob_bytes(tls, (*TFts5Index)(unsafe.Pointer(p)).FpReader))
			szData = int64((libc.Uint32FromInt64(12) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7)))
			nAlloc = szData + nByte + int64(FTS5_DATA_PADDING)
			pRet = Xsqlite3_malloc64(tls, uint64(nAlloc))
			if pRet != 0 {
				(*TFts5Data)(unsafe.Pointer(pRet)).Fnn = int32(nByte)
				v1 = pRet + uintptr(szData)
				(*TFts5Data)(unsafe.Pointer(pRet)).Fp = v1
				aOut = v1
			} else {
				rc = int32(SQLITE_NOMEM)
			}
			if rc == SQLITE_OK {
				rc = Xsqlite3_blob_read(tls, (*TFts5Index)(unsafe.Pointer(p)).FpReader, aOut, int32(nByte), 0)
			}
			if rc != SQLITE_OK {
				Xsqlite3_free(tls, pRet)
				pRet = uintptr(0)
			} else {
				/* TODO1: Fix this */
				**(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer(pRet)).Fp + uintptr(nByte))) = uint8(0x00)
				**(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer(pRet)).Fp + uintptr(nByte+int64(1)))) = uint8(0x00)
				(*TFts5Data)(unsafe.Pointer(pRet)).FszLeaf = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pRet)).Fp+2))
			}
		}
		(*TFts5Index)(unsafe.Pointer(p)).Frc = rc
		(*TFts5Index)(unsafe.Pointer(p)).FnRead = (*TFts5Index)(unsafe.Pointer(p)).FnRead + 1
	}
	return pRet
}

// C documentation
//
//	/*
//	** Remove all records associated with segment iSegid.
//	*/
func _fts5DataRemoveSegment(tls *libc.TLS, p uintptr, pSeg uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var iFirst, iLast, iTomb1, iTomb2 Ti64
	var iSegid int32
	var pConfig uintptr
	_, _, _, _, _, _ = iFirst, iLast, iSegid, iTomb1, iTomb2, pConfig
	iSegid = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid
	iFirst = int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(libc.Int32FromInt32(0))
	iLast = int64(iSegid+libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(libc.Int32FromInt32(0)) - int64(1)
	_fts5DataDelete(tls, p, iFirst, iLast)
	if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone != 0 {
		iTomb1 = int64(iSegid+libc.Int32FromInt32(1)<<libc.Int32FromInt32(16))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(libc.Int32FromInt32(0))
		iTomb2 = int64(iSegid+libc.Int32FromInt32(1)<<libc.Int32FromInt32(16))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone-libc.Int32FromInt32(1))
		_fts5DataDelete(tls, p, iTomb1, iTomb2)
	}
	if (*TFts5Index)(unsafe.Pointer(p)).FpIdxDeleter == uintptr(0) {
		pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
		_fts5IndexPrepareStmt(tls, p, p+68, Xsqlite3_mprintf(tls, __ccgo_ts+40285, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName)))
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		Xsqlite3_bind_int(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxDeleter, int32(1), iSegid)
		Xsqlite3_step(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxDeleter)
		(*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxDeleter)
	}
}

// C documentation
//
//	/*
//	** INSERT OR REPLACE a record into the %_data table.
//	*/
func _fts5DataWrite(tls *libc.TLS, p uintptr, iRowid Ti64, pData uintptr, nData int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pConfig uintptr
	_ = pConfig
	if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK {
		return
	}
	if (*TFts5Index)(unsafe.Pointer(p)).FpWriter == uintptr(0) {
		pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
		_fts5IndexPrepareStmt(tls, p, p+56, Xsqlite3_mprintf(tls, __ccgo_ts+40185, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName)))
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
			return
		}
	}
	Xsqlite3_bind_int64(tls, (*TFts5Index)(unsafe.Pointer(p)).FpWriter, int32(1), iRowid)
	Xsqlite3_bind_blob(tls, (*TFts5Index)(unsafe.Pointer(p)).FpWriter, int32(2), pData, nData, libc.UintptrFromInt32(0))
	Xsqlite3_step(tls, (*TFts5Index)(unsafe.Pointer(p)).FpWriter)
	(*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, (*TFts5Index)(unsafe.Pointer(p)).FpWriter)
	Xsqlite3_bind_null(tls, (*TFts5Index)(unsafe.Pointer(p)).FpWriter, int32(2))
}

func _fts5DlidxIterInit(tls *libc.TLS, p uintptr, bRev int32, iSegid int32, iLeafPg int32) (r uintptr) {
	var bDone, i int32
	var iRowid Ti64
	var nByte Tsqlite3_int64
	var pIter, pLvl, pNew uintptr
	_, _, _, _, _, _, _ = bDone, i, iRowid, nByte, pIter, pLvl, pNew
	pIter = uintptr(0)
	bDone = 0
	i = 0
	for {
		if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bDone == 0) {
			break
		}
		nByte = int64(uint32(libc.UintptrFromInt32(0)+8) + uint32(i+libc.Int32FromInt32(1))*libc.Uint32FromInt64(32))
		pNew = Xsqlite3_realloc64(tls, pIter, uint64(nByte))
		if pNew == uintptr(0) {
			(*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
		} else {
			iRowid = int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(i)<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(iLeafPg)
			pLvl = pNew + 8 + uintptr(i)*32
			pIter = pNew
			libc.Xmemset(tls, pLvl, 0, uint32(32))
			(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData = _fts5DataRead(tls, p, iRowid)
			if (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData != 0 && int32(**(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer((*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData)).Fp)))&int32(0x0001) == 0 {
				bDone = int32(1)
			}
			(*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl = i + int32(1)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*TFts5DlidxIter)(unsafe.Pointer(pIter)).FiSegid = iSegid
		if bRev == 0 {
			_fts5DlidxIterFirst(tls, pIter)
		} else {
			_fts5DlidxIterLast(tls, p, pIter)
		}
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK {
		_fts5DlidxIterFree(tls, pIter)
		pIter = uintptr(0)
	}
	return pIter
}

// C documentation
//
//	/*
//	** Advance the iterator passed as the only argument. If the end of the
//	** doclist-index page is reached, return non-zero.
//	*/
func _fts5DlidxLvlNext(tls *libc.TLS, pLvl uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iOff int32
	var pData, v2 uintptr
	var _ /* iVal at bp+0 */ Tu64
	_, _, _ = iOff, pData, v2
	pData = (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData
	if (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff == 0 {
		(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff = int32(1)
		**(**int32)(__ccgo_up(pLvl + 4)) += _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+1, pLvl+16)
		**(**int32)(__ccgo_up(pLvl + 4)) += int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+uintptr((*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff), pLvl+24))
		(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiFirstOff = (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff
	} else {
		iOff = (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff
		for {
			if !(iOff < (*TFts5Data)(unsafe.Pointer(pData)).Fnn) {
				break
			}
			if **(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer(pData)).Fp + uintptr(iOff))) != 0 {
				break
			}
			goto _1
		_1:
			;
			iOff = iOff + 1
		}
		if iOff < (*TFts5Data)(unsafe.Pointer(pData)).Fnn {
			**(**int32)(__ccgo_up(pLvl + 16)) += iOff - (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff + int32(1)
			iOff = iOff + int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+uintptr(iOff), bp))
			v2 = pLvl + 24
			*(*Ti64)(unsafe.Pointer(v2)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(v2))) + **(**Tu64)(__ccgo_up(bp)))
			(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff = iOff
		} else {
			(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof = int32(1)
		}
	}
	return (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof
}

// C documentation
//
//	/*
//	** Move the iterator passed as the only argument to the previous entry.
//	*/
func _fts5DlidxLvlPrev(tls *libc.TLS, pLvl uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a, v1 uintptr
	var iOff, ii, nZero int32
	var _ /* delta at bp+0 */ Tu64
	_, _, _, _, _ = a, iOff, ii, nZero, v1
	iOff = (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff
	if iOff <= (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiFirstOff {
		(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof = int32(1)
	} else {
		a = (*TFts5Data)(unsafe.Pointer((*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FpData)).Fp
		(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff = 0
		_fts5DlidxLvlNext(tls, pLvl)
		for int32(1) != 0 {
			nZero = 0
			ii = (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff
			**(**Tu64)(__ccgo_up(bp)) = uint64(0)
			for int32(**(**Tu8)(__ccgo_up(a + uintptr(ii)))) == 0 {
				nZero = nZero + 1
				ii = ii + 1
			}
			ii = ii + int32(_sqlite3Fts5GetVarint(tls, a+uintptr(ii), bp))
			if ii >= iOff {
				break
			}
			**(**int32)(__ccgo_up(pLvl + 16)) += nZero + int32(1)
			v1 = pLvl + 24
			*(*Ti64)(unsafe.Pointer(v1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(v1))) + **(**Tu64)(__ccgo_up(bp)))
			(*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff = ii
		}
	}
	return (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FbEof
}

// C documentation
//
//	/*
//	** Completely remove the entry that pSeg currently points to from
//	** the database.
//	*/
func _fts5DoSecureDelete(tls *libc.TLS, p uintptr, pSeg uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var aIdx, aPg, aTermIdx, pPg, pTerm uintptr
	var bDetailNone, bEmpty, iDelKeyOff, iIdx, iKey, iKeyIn, iKeyOff, iKeyOff1, iKeyOut, iNextOff, iOff, iPgIdx, iPgno, iPrevKeyOut, iSOP, iSegid, iStart, iTermIdx, nByte, nIdx, nMove, nPg, nShift, nTermIdx, pgno, v2 int32
	var iId, iTermOff Ti64
	var v5 uint64
	var _ /* bLastInDoclist at bp+8 */ int32
	var _ /* iDelta at bp+0 */ Tu64
	var _ /* iNextDelta at bp+24 */ Tu64
	var _ /* iVal at bp+16 */ Tu32
	var _ /* iVal at bp+32 */ Tu32
	var _ /* iVal at bp+72 */ Tu32
	var _ /* iVal at bp+76 */ Tu32
	var _ /* nPos at bp+12 */ int32
	var _ /* nPrefix at bp+40 */ Tu64
	var _ /* nPrefix2 at bp+56 */ Tu64
	var _ /* nSuffix at bp+48 */ Tu64
	var _ /* nSuffix2 at bp+64 */ Tu64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aIdx, aPg, aTermIdx, bDetailNone, bEmpty, iDelKeyOff, iId, iIdx, iKey, iKeyIn, iKeyOff, iKeyOff1, iKeyOut, iNextOff, iOff, iPgIdx, iPgno, iPrevKeyOut, iSOP, iSegid, iStart, iTermIdx, iTermOff, nByte, nIdx, nMove, nPg, nShift, nTermIdx, pPg, pTerm, pgno, v2, v5
	bDetailNone = libc.BoolInt32((*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == libc.Int32FromInt32(FTS5_DETAIL_NONE))
	iSegid = (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FiSegid
	aPg = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).Fp
	nPg = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).Fnn
	iPgIdx = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).FszLeaf /* Offset of page footer */
	**(**Tu64)(__ccgo_up(bp)) = uint64(0)
	iNextOff = 0
	iOff = 0
	nIdx = 0
	aIdx = uintptr(0)
	**(**int32)(__ccgo_up(bp + 8)) = 0
	iIdx = 0
	iStart = 0
	iDelKeyOff = 0 /* Offset of deleted key, if any */
	nIdx = nPg - iPgIdx
	aIdx = _sqlite3Fts5MallocZero(tls, p+44, int64(nIdx)+int64(16))
	if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
		return
	}
	libc.Xmemcpy(tls, aIdx, aPg+uintptr(iPgIdx), uint32(nIdx))
	/* At this point segment iterator pSeg points to the entry
	 ** this function should remove from the b-tree segment.
	 **
	 ** In detail=full or detail=column mode, pSeg->iLeafOffset is the
	 ** offset of the first byte in the position-list for the entry to
	 ** remove. Immediately before this comes two varints that will also
	 ** need to be removed:
	 **
	 **     + the rowid or delta rowid value for the entry, and
	 **     + the size of the position list in bytes.
	 **
	 ** Or, in detail=none mode, there is a single varint prior to
	 ** pSeg->iLeafOffset - the rowid or delta rowid value.
	 **
	 ** This block sets the following variables:
	 **
	 **   iStart:
	 **     The offset of the first byte of the rowid or delta-rowid
	 **     value for the doclist entry being removed.
	 **
	 **   iDelta:
	 **     The value of the rowid or delta-rowid value for the doclist
	 **     entry being removed.
	 **
	 **   iNextOff:
	 **     The offset of the next entry following the position list
	 **     for the one being removed. If the position list for this
	 **     entry overflows onto the next leaf page, this value will be
	 **     greater than pLeaf->szLeaf.
	 */
	/* Start-Of-Position-list */
	if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno == (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno {
		iStart = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafOffset
	} else {
		iStart = int32(_fts5GetU16(tls, aPg))
	}
	if iStart > nPg {
		_fts5IndexCorruptIdx(tls, p)
		Xsqlite3_free(tls, aIdx)
		return
	}
	iSOP = iStart + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iStart), bp))
	if bDetailNone != 0 {
		for int64(iSOP) < (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset {
			if int32(**(**Tu8)(__ccgo_up(aPg + uintptr(iSOP)))) == 0x00 {
				iSOP = iSOP + 1
			}
			if int32(**(**Tu8)(__ccgo_up(aPg + uintptr(iSOP)))) == 0x00 {
				iSOP = iSOP + 1
			}
			iStart = iSOP
			iSOP = iStart + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iStart), bp))
		}
		iNextOff = iSOP
		if iNextOff < (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiEndofDoclist && int32(**(**Tu8)(__ccgo_up(aPg + uintptr(iNextOff)))) == 0x00 {
			iNextOff = iNextOff + 1
		}
		if iNextOff < (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiEndofDoclist && int32(**(**Tu8)(__ccgo_up(aPg + uintptr(iNextOff)))) == 0x00 {
			iNextOff = iNextOff + 1
		}
	} else {
		**(**int32)(__ccgo_up(bp + 12)) = 0
		iSOP = iSOP + _sqlite3Fts5GetVarint32(tls, aPg+uintptr(iSOP), bp+12)
		for int64(iSOP) < (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset {
			iStart = iSOP + **(**int32)(__ccgo_up(bp + 12))/int32(2)
			iSOP = iStart + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iStart), bp))
			iSOP = iSOP + _sqlite3Fts5GetVarint32(tls, aPg+uintptr(iSOP), bp+12)
		}
		iNextOff = iSOP + (*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos
	}
	iOff = iStart
	/* If the position-list for the entry being removed flows over past
	 ** the end of this page, delete the portion of the position-list on the
	 ** next page and beyond.
	 **
	 ** Set variable bLastInDoclist to true if this entry happens
	 ** to be the last rowid in the doclist for its term.  */
	if iNextOff >= iPgIdx {
		pgno = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno + int32(1)
		_fts5SecureDeleteOverflow(tls, p, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg, pgno, bp+8)
		iNextOff = iPgIdx
	}
	if int32((*TFts5SegIter)(unsafe.Pointer(pSeg)).FbDel) == 0 {
		if iNextOff != iPgIdx {
			/* Loop through the page-footer. If iNextOff (offset of the
			 ** entry following the one we are removing) is equal to the
			 ** offset of a key on this page, then the entry is the last
			 ** in its doclist. */
			iKeyOff = 0
			iIdx = 0
			for {
				if !(iIdx < nIdx) {
					break
				}
				**(**Tu32)(__ccgo_up(bp + 16)) = uint32(0)
				iIdx = iIdx + _sqlite3Fts5GetVarint32(tls, aIdx+uintptr(iIdx), bp+16)
				iKeyOff = int32(uint32(iKeyOff) + **(**Tu32)(__ccgo_up(bp + 16)))
				if iKeyOff == iNextOff {
					**(**int32)(__ccgo_up(bp + 8)) = int32(1)
				}
				goto _1
			_1:
			}
		}
		/* If this is (a) the first rowid on a page and (b) is not followed by
		 ** another position list on the same page, set the "first-rowid" field
		 ** of the header to 0.  */
		if int32(_fts5GetU16(tls, aPg)) == iStart && (**(**int32)(__ccgo_up(bp + 8)) != 0 || iNextOff == iPgIdx) {
			_fts5PutU16(tls, aPg, uint16(0))
		}
	}
	if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FbDel != 0 {
		iOff = iOff + _sqlite3Fts5PutVarint(tls, aPg+uintptr(iOff), **(**Tu64)(__ccgo_up(bp)))
		v2 = iOff
		iOff = iOff + 1
		**(**Tu8)(__ccgo_up(aPg + uintptr(v2))) = uint8(0x01)
	} else {
		if **(**int32)(__ccgo_up(bp + 8)) == 0 {
			if iNextOff != iPgIdx {
				**(**Tu64)(__ccgo_up(bp + 24)) = uint64(0)
				iNextOff = iNextOff + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iNextOff), bp+24))
				iOff = iOff + _sqlite3Fts5PutVarint(tls, aPg+uintptr(iOff), **(**Tu64)(__ccgo_up(bp))+**(**Tu64)(__ccgo_up(bp + 24)))
			}
		} else {
			if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno == (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno && iStart == (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafOffset {
				/* The entry being removed was the only position list in its
				 ** doclist. Therefore the term needs to be removed as well. */
				iKey = 0
				iKeyOff1 = 0
				/* Set iKeyOff to the offset of the term that will be removed - the
				 ** last offset in the footer that is not greater than iStart. */
				iIdx = 0
				for {
					if !(iIdx < nIdx) {
						break
					}
					**(**Tu32)(__ccgo_up(bp + 32)) = uint32(0)
					iIdx = iIdx + _sqlite3Fts5GetVarint32(tls, aIdx+uintptr(iIdx), bp+32)
					if uint32(iKeyOff1)+**(**Tu32)(__ccgo_up(bp + 32)) > uint32(iStart) {
						break
					}
					iKeyOff1 = int32(uint32(iKeyOff1) + **(**Tu32)(__ccgo_up(bp + 32)))
					goto _3
				_3:
					;
					iKey = iKey + 1
				}
				/* Set iDelKeyOff to the value of the footer entry to remove from
				 ** the page. */
				v2 = iKeyOff1
				iOff = v2
				iDelKeyOff = v2
				if iNextOff != iPgIdx {
					/* This is the only position-list associated with the term, and there
					 ** is another term following it on this page. So the subsequent term
					 ** needs to be moved to replace the term associated with the entry
					 ** being removed. */
					**(**Tu64)(__ccgo_up(bp + 40)) = uint64(0)
					**(**Tu64)(__ccgo_up(bp + 48)) = uint64(0)
					**(**Tu64)(__ccgo_up(bp + 56)) = uint64(0)
					**(**Tu64)(__ccgo_up(bp + 64)) = uint64(0)
					iDelKeyOff = iNextOff
					iNextOff = iNextOff + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iNextOff), bp+56))
					iNextOff = iNextOff + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iNextOff), bp+64))
					if iKey != int32(1) {
						iKeyOff1 = iKeyOff1 + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iKeyOff1), bp+40))
					}
					iKeyOff1 = iKeyOff1 + int32(_sqlite3Fts5GetVarint(tls, aPg+uintptr(iKeyOff1), bp+48))
					if **(**Tu64)(__ccgo_up(bp + 40)) < **(**Tu64)(__ccgo_up(bp + 56)) {
						v5 = **(**Tu64)(__ccgo_up(bp + 40))
					} else {
						v5 = **(**Tu64)(__ccgo_up(bp + 56))
					}
					**(**Tu64)(__ccgo_up(bp + 40)) = v5
					**(**Tu64)(__ccgo_up(bp + 48)) = **(**Tu64)(__ccgo_up(bp + 56)) + **(**Tu64)(__ccgo_up(bp + 64)) - **(**Tu64)(__ccgo_up(bp + 40))
					if uint64(iKeyOff1)+**(**Tu64)(__ccgo_up(bp + 48)) > uint64(iPgIdx) || uint64(iNextOff)+**(**Tu64)(__ccgo_up(bp + 64)) > uint64(iPgIdx) {
						_fts5IndexCorruptIdx(tls, p)
					} else {
						if iKey != int32(1) {
							iOff = iOff + _sqlite3Fts5PutVarint(tls, aPg+uintptr(iOff), **(**Tu64)(__ccgo_up(bp + 40)))
						}
						iOff = iOff + _sqlite3Fts5PutVarint(tls, aPg+uintptr(iOff), **(**Tu64)(__ccgo_up(bp + 48)))
						if **(**Tu64)(__ccgo_up(bp + 56)) > uint64((*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fn) {
							_fts5IndexCorruptIdx(tls, p)
						} else {
							if **(**Tu64)(__ccgo_up(bp + 56)) > **(**Tu64)(__ccgo_up(bp + 40)) {
								libc.Xmemcpy(tls, aPg+uintptr(iOff), (*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fp+uintptr(**(**Tu64)(__ccgo_up(bp + 40))), uint32(**(**Tu64)(__ccgo_up(bp + 56))-**(**Tu64)(__ccgo_up(bp + 40))))
								iOff = int32(uint64(iOff) + (**(**Tu64)(__ccgo_up(bp + 56)) - **(**Tu64)(__ccgo_up(bp + 40))))
							}
						}
						libc.Xmemmove(tls, aPg+uintptr(iOff), aPg+uintptr(iNextOff), uint32(**(**Tu64)(__ccgo_up(bp + 64))))
						iOff = int32(uint64(iOff) + **(**Tu64)(__ccgo_up(bp + 64)))
						iNextOff = int32(uint64(iNextOff) + **(**Tu64)(__ccgo_up(bp + 64)))
					}
				}
			} else {
				if iStart == int32(4) {
					/* The entry being removed may be the only position list in
					 ** its doclist. */
					iPgno = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno - int32(1)
					for {
						if !(iPgno > (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno) {
							break
						}
						pPg = _fts5DataRead(tls, p, int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64(iPgno))
						bEmpty = libc.BoolInt32(pPg != 0 && (*TFts5Data)(unsafe.Pointer(pPg)).Fnn == int32(4))
						_fts5DataRelease(tls, pPg)
						if bEmpty == 0 {
							break
						}
						goto _6
					_6:
						;
						iPgno = iPgno - 1
					}
					if iPgno == (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno {
						iId = int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno)
						pTerm = _fts5DataRead(tls, p, iId)
						if pTerm != 0 && (*TFts5Data)(unsafe.Pointer(pTerm)).FszLeaf == (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafOffset {
							aTermIdx = (*TFts5Data)(unsafe.Pointer(pTerm)).Fp + uintptr((*TFts5Data)(unsafe.Pointer(pTerm)).FszLeaf)
							nTermIdx = (*TFts5Data)(unsafe.Pointer(pTerm)).Fnn - (*TFts5Data)(unsafe.Pointer(pTerm)).FszLeaf
							iTermIdx = 0
							iTermOff = 0
							for int32(1) != 0 {
								**(**Tu32)(__ccgo_up(bp + 72)) = uint32(0)
								nByte = _sqlite3Fts5GetVarint32(tls, aTermIdx+uintptr(iTermIdx), bp+72)
								iTermOff = iTermOff + int64(**(**Tu32)(__ccgo_up(bp + 72)))
								if iTermIdx+nByte >= nTermIdx {
									break
								}
								iTermIdx = iTermIdx + nByte
							}
							nTermIdx = iTermIdx
							if iTermOff > int64((*TFts5Data)(unsafe.Pointer(pTerm)).FszLeaf) {
								_fts5IndexCorruptIdx(tls, p)
							} else {
								libc.Xmemmove(tls, (*TFts5Data)(unsafe.Pointer(pTerm)).Fp+uintptr(iTermOff), (*TFts5Data)(unsafe.Pointer(pTerm)).Fp+uintptr((*TFts5Data)(unsafe.Pointer(pTerm)).FszLeaf), uint32(nTermIdx))
								_fts5PutU16(tls, (*TFts5Data)(unsafe.Pointer(pTerm)).Fp+2, uint16(iTermOff))
								_fts5DataWrite(tls, p, iId, (*TFts5Data)(unsafe.Pointer(pTerm)).Fp, int32(iTermOff+int64(nTermIdx)))
								if nTermIdx == 0 {
									_fts5SecureDeleteIdxEntry(tls, p, iSegid, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno)
								}
							}
						}
						_fts5DataRelease(tls, pTerm)
					}
				}
			}
		}
	}
	/* Assuming no error has occurred, this block does final edits to the
	 ** leaf page before writing it back to disk. Input variables are:
	 **
	 **   nPg: Total initial size of leaf page.
	 **   iPgIdx: Initial offset of page footer.
	 **
	 **   iOff: Offset to move data to
	 **   iNextOff: Offset to move data from
	 */
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		nMove = nPg - iNextOff   /* Number of bytes to move */
		nShift = iNextOff - iOff /* Distance to move them */
		iPrevKeyOut = 0
		iKeyIn = 0
		if nMove > 0 {
			libc.Xmemmove(tls, aPg+uintptr(iOff), aPg+uintptr(iNextOff), uint32(nMove))
		}
		iPgIdx = iPgIdx - nShift
		nPg = iPgIdx
		_fts5PutU16(tls, aPg+2, uint16(iPgIdx))
		iIdx = 0
		for {
			if !(iIdx < nIdx) {
				break
			}
			**(**Tu32)(__ccgo_up(bp + 76)) = uint32(0)
			iIdx = iIdx + _sqlite3Fts5GetVarint32(tls, aIdx+uintptr(iIdx), bp+76)
			iKeyIn = int32(uint32(iKeyIn) + **(**Tu32)(__ccgo_up(bp + 76)))
			if iKeyIn != iDelKeyOff {
				if iKeyIn > iOff {
					v2 = nShift
				} else {
					v2 = 0
				}
				iKeyOut = iKeyIn - v2
				nPg = nPg + _sqlite3Fts5PutVarint(tls, aPg+uintptr(nPg), uint64(iKeyOut-iPrevKeyOut))
				iPrevKeyOut = iKeyOut
			}
			goto _7
		_7:
		}
		if iPgIdx == nPg && nIdx > 0 && (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno != int32(1) {
			_fts5SecureDeleteIdxEntry(tls, p, iSegid, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno)
		}
		_fts5DataWrite(tls, p, int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno), aPg, nPg)
	}
	Xsqlite3_free(tls, aIdx)
}

// C documentation
//
//	/*
//	** Add pSub as a child of p.
//	*/
func _fts5ExprAddChildren(tls *libc.TLS, p uintptr, pSub uintptr) {
	var ii, nByte, v1 int32
	var v2 uintptr
	_, _, _, _ = ii, nByte, v1, v2
	ii = (*TFts5ExprNode)(unsafe.Pointer(p)).FnChild
	if (*TFts5ExprNode)(unsafe.Pointer(p)).FeType != int32(FTS5_NOT) && (*TFts5ExprNode)(unsafe.Pointer(pSub)).FeType == (*TFts5ExprNode)(unsafe.Pointer(p)).FeType {
		nByte = int32(uint32(4) * uint32((*TFts5ExprNode)(unsafe.Pointer(pSub)).FnChild))
		libc.Xmemcpy(tls, p+40+uintptr((*TFts5ExprNode)(unsafe.Pointer(p)).FnChild)*4, pSub+40, uint32(nByte))
		**(**int32)(__ccgo_up(p + 36)) += (*TFts5ExprNode)(unsafe.Pointer(pSub)).FnChild
		Xsqlite3_free(tls, pSub)
	} else {
		v2 = p + 36
		v1 = *(*int32)(unsafe.Pointer(v2))
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		*(*uintptr)(unsafe.Pointer(p + 40 + uintptr(v1)*4)) = pSub
	}
	for {
		if !(ii < (*TFts5ExprNode)(unsafe.Pointer(p)).FnChild) {
			break
		}
		if (*TFts5ExprNode)(unsafe.Pointer(p)).FiHeight > (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 40 + uintptr(ii)*4)))).FiHeight+int32(1) {
			v1 = (*TFts5ExprNode)(unsafe.Pointer(p)).FiHeight
		} else {
			v1 = (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 40 + uintptr(ii)*4)))).FiHeight + int32(1)
		}
		(*TFts5ExprNode)(unsafe.Pointer(p)).FiHeight = v1
		goto _3
	_3:
		;
		ii = ii + 1
	}
}

func _fts5ExprAssignXNext(tls *libc.TLS, pNode uintptr) {
	var pNear uintptr
	_ = pNear
	switch (*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType {
	case int32(FTS5_STRING):
		pNear = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear
		if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase == int32(1) && (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)))).FnTerm == int32(1) && (*(*TFts5ExprTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)) + 20))).FpSynonym == uintptr(0) && int32((*(*TFts5ExprTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)) + 20))).FbFirst) == 0 {
			(*TFts5ExprNode)(unsafe.Pointer(pNode)).FeType = int32(FTS5_TERM)
			(*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_TERM)
		} else {
			(*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_STRING)
		}
	case int32(FTS5_OR):
		(*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_OR)
	case int32(FTS5_AND):
		(*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_AND)
	default:
		(*TFts5ExprNode)(unsafe.Pointer(pNode)).FxNext = __ccgo_fp(_fts5ExprNodeNext_NOT)
		break
	}
}

// C documentation
//
//	/*
//	** Read the first token from the nul-terminated string at *pz.
//	*/
func _fts5ExprGetToken(tls *libc.TLS, pParse uintptr, pz uintptr, pToken uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var tok int32
	var z, z2, z21 uintptr
	_, _, _, _ = tok, z, z2, z21
	z = **(**uintptr)(__ccgo_up(pz))
	/* Skip past any whitespace */
	for _fts5ExprIsspace(tls, **(**int8)(__ccgo_up(z))) != 0 {
		z = z + 1
	}
	(*TFts5Token)(unsafe.Pointer(pToken)).Fp = z
	(*TFts5Token)(unsafe.Pointer(pToken)).Fn = int32(1)
	switch int32(**(**int8)(__ccgo_up(z))) {
	case int32('('):
		tok = int32(FTS5_LP)
	case int32(')'):
		tok = int32(FTS5_RP)
	case int32('{'):
		tok = int32(FTS5_LCP)
	case int32('}'):
		tok = int32(FTS5_RCP)
	case int32(':'):
		tok = int32(FTS5_COLON)
	case int32(','):
		tok = int32(FTS5_COMMA)
	case int32('+'):
		tok = int32(FTS5_PLUS)
	case int32('*'):
		tok = int32(FTS5_STAR)
	case int32('-'):
		tok = int32(FTS5_MINUS)
	case int32('^'):
		tok = int32(FTS5_CARET)
	case int32('\000'):
		tok = FTS5_EOF
	case int32('"'):
		tok = int32(FTS5_STRING)
		z2 = z + 1
		for {
			if !(int32(1) != 0) {
				break
			}
			if int32(**(**int8)(__ccgo_up(z2))) == int32('"') {
				z2 = z2 + 1
				if int32(**(**int8)(__ccgo_up(z2))) != int32('"') {
					break
				}
			}
			if int32(**(**int8)(__ccgo_up(z2))) == int32('\000') {
				_sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+39784, 0)
				return FTS5_EOF
			}
			goto _1
		_1:
			;
			z2 = z2 + 1
		}
		(*TFts5Token)(unsafe.Pointer(pToken)).Fn = int32(z2) - int32(z)
	default:
		if _sqlite3Fts5IsBareword(tls, **(**int8)(__ccgo_up(z))) == 0 {
			_sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+39804, libc.VaList(bp+8, z))
			return FTS5_EOF
		}
		tok = int32(FTS5_STRING)
		z21 = z + 1
		for {
			if !(_sqlite3Fts5IsBareword(tls, **(**int8)(__ccgo_up(z21))) != 0) {
				break
			}
			goto _2
		_2:
			;
			z21 = z21 + 1
		}
		(*TFts5Token)(unsafe.Pointer(pToken)).Fn = int32(z21) - int32(z)
		if (*TFts5Token)(unsafe.Pointer(pToken)).Fn == int32(2) && libc.Xmemcmp(tls, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, __ccgo_ts+39835, uint32(2)) == 0 {
			tok = int32(FTS5_OR)
		}
		if (*TFts5Token)(unsafe.Pointer(pToken)).Fn == int32(3) && libc.Xmemcmp(tls, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, __ccgo_ts+39838, uint32(3)) == 0 {
			tok = int32(FTS5_NOT)
		}
		if (*TFts5Token)(unsafe.Pointer(pToken)).Fn == int32(3) && libc.Xmemcmp(tls, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, __ccgo_ts+33645, uint32(3)) == 0 {
			tok = int32(FTS5_AND)
		}
		break
	}
	**(**uintptr)(__ccgo_up(pz)) = (*TFts5Token)(unsafe.Pointer(pToken)).Fp + uintptr((*TFts5Token)(unsafe.Pointer(pToken)).Fn)
	return tok
}

// C documentation
//
//	/*
//	** Initialize all term iterators in the pNear object. If any term is found
//	** to match no documents at all, return immediately without initializing any
//	** further iterators.
//	**
//	** If an error occurs, return an SQLite error code. Otherwise, return
//	** SQLITE_OK. It is not considered an error if some term matches zero
//	** documents.
//	*/
func _fts5ExprNearInitAll(tls *libc.TLS, pExpr uintptr, pNode uintptr) (r int32) {
	var bHit, i, j, rc, v4, v5 int32
	var p, pNear, pPhrase, pTerm uintptr
	_, _, _, _, _, _, _, _, _, _ = bHit, i, j, p, pNear, pPhrase, pTerm, rc, v4, v5
	pNear = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear
	i = 0
	for {
		if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) {
			break
		}
		pPhrase = *(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(i)*4))
		if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm == 0 {
			(*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = int32(1)
			return SQLITE_OK
		} else {
			j = 0
			for {
				if !(j < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) {
					break
				}
				pTerm = pPhrase + 20 + uintptr(j)*24
				bHit = 0
				p = pTerm
				for {
					if !(p != 0) {
						break
					}
					if (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter != 0 {
						_sqlite3Fts5IterClose(tls, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)
						(*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter = uintptr(0)
					}
					if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FbPrefix != 0 {
						v4 = int32(FTS5INDEX_QUERY_PREFIX)
					} else {
						v4 = 0
					}
					if (*TFts5Expr)(unsafe.Pointer(pExpr)).FbDesc != 0 {
						v5 = int32(FTS5INDEX_QUERY_DESC)
					} else {
						v5 = 0
					}
					rc = _sqlite3Fts5IndexQuery(tls, (*TFts5Expr)(unsafe.Pointer(pExpr)).FpIndex, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpTerm, (*TFts5ExprTerm)(unsafe.Pointer(p)).FnQueryTerm, v4|v5, (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset, p+16)
					if rc != SQLITE_OK {
						return rc
					}
					if 0 == int32((*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FbEof) {
						bHit = int32(1)
					}
					goto _3
				_3:
					;
					p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym
				}
				if bHit == 0 {
					(*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = int32(1)
					return SQLITE_OK
				}
				goto _2
			_2:
				;
				j = j + 1
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	(*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = 0
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** The near-set object passed as the first argument contains more than
//	** one phrase. All phrases currently point to the same row. The
//	** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
//	** tests if the current row contains instances of each phrase sufficiently
//	** close together to meet the NEAR constraint. Non-zero is returned if it
//	** does, or zero otherwise.
//	**
//	** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
//	** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
//	** occurs within this function (*pRc) is set accordingly before returning.
//	** The return value is undefined in both these cases.
//	**
//	** If no error occurs and non-zero (a match) is returned, the position-list
//	** of each phrase object is edited to contain only those entries that
//	** meet the constraint before returning.
//	*/
func _fts5ExprNearIsMatch(tls *libc.TLS, pRc uintptr, pNear uintptr) (r int32) {
	bp := tls.Alloc(208)
	defer tls.Free(208)
	var a, apPhrase, pPos, pPoslist, pWriter uintptr
	var bMatch, bRet, i, iAdv int32
	var iMax, iMin, iPos Ti64
	var nByte Tsqlite3_int64
	var _ /* aStatic at bp+0 */ [4]TFts5NearTrimmer
	var _ /* rc at bp+192 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _ = a, apPhrase, bMatch, bRet, i, iAdv, iMax, iMin, iPos, nByte, pPos, pPoslist, pWriter
	a = bp
	apPhrase = pNear + 12
	**(**int32)(__ccgo_up(bp + 192)) = **(**int32)(__ccgo_up(pRc))
	/* If the aStatic[] array is not large enough, allocate a large array
	 ** using sqlite3_malloc(). This approach could be improved upon. */
	if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase > int32(libc.Uint32FromInt64(192)/libc.Uint32FromInt64(48)) {
		nByte = int64(uint32(48) * uint32((*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase))
		a = _sqlite3Fts5MallocZero(tls, bp+192, nByte)
	} else {
		libc.Xmemset(tls, bp, 0, uint32(192))
	}
	if **(**int32)(__ccgo_up(bp + 192)) != SQLITE_OK {
		**(**int32)(__ccgo_up(pRc)) = **(**int32)(__ccgo_up(bp + 192))
		return 0
	}
	/* Initialize a lookahead iterator for each phrase. After passing the
	 ** buffer and buffer size to the lookaside-reader init function, zero
	 ** the phrase poslist buffer. The new poslist for the phrase (containing
	 ** the same entries as the original with some entries removed on account
	 ** of the NEAR constraint) is written over the original even as it is
	 ** being read. This is safe as the entries for the new poslist are a
	 ** subset of the old, so it is not possible for data yet to be read to
	 ** be overwritten.  */
	i = 0
	for {
		if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) {
			break
		}
		pPoslist = **(**uintptr)(__ccgo_up(apPhrase + uintptr(i)*4)) + 4
		_fts5LookaheadReaderInit(tls, (*TFts5Buffer)(unsafe.Pointer(pPoslist)).Fp, (*TFts5Buffer)(unsafe.Pointer(pPoslist)).Fn, a+uintptr(i)*48)
		(*TFts5Buffer)(unsafe.Pointer(pPoslist)).Fn = 0
		(**(**TFts5NearTrimmer)(__ccgo_up(a + uintptr(i)*48))).FpOut = pPoslist
		goto _1
	_1:
		;
		i = i + 1
	}
	for int32(1) != 0 {
		/* This block advances the phrase iterators until they point to a set of
		 ** entries that together comprise a match.  */
		iMax = (**(**TFts5NearTrimmer)(__ccgo_up(a))).Freader.FiPos
		for cond := true; cond; cond = bMatch == 0 {
			bMatch = int32(1)
			i = 0
			for {
				if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) {
					break
				}
				pPos = a + uintptr(i)*48
				iMin = iMax - int64((*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(i)*4)))).FnTerm) - int64((*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnNear)
				if (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos < iMin || (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos > iMax {
					bMatch = 0
					for (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos < iMin {
						if _fts5LookaheadReaderNext(tls, pPos) != 0 {
							goto ismatch_out
						}
					}
					if (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos > iMax {
						iMax = (*TFts5LookaheadReader)(unsafe.Pointer(pPos)).FiPos
					}
				}
				goto _2
			_2:
				;
				i = i + 1
			}
		}
		/* Add an entry to each output position list */
		i = 0
		for {
			if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) {
				break
			}
			iPos = (**(**TFts5NearTrimmer)(__ccgo_up(a + uintptr(i)*48))).Freader.FiPos
			pWriter = a + uintptr(i)*48 + 32
			if (*TFts5Buffer)(unsafe.Pointer((**(**TFts5NearTrimmer)(__ccgo_up(a + uintptr(i)*48))).FpOut)).Fn == 0 || iPos != (*TFts5PoslistWriter)(unsafe.Pointer(pWriter)).FiPrev {
				_sqlite3Fts5PoslistSafeAppend(tls, (**(**TFts5NearTrimmer)(__ccgo_up(a + uintptr(i)*48))).FpOut, pWriter, iPos)
			}
			goto _3
		_3:
			;
			i = i + 1
		}
		iAdv = 0
		iMin = (**(**TFts5NearTrimmer)(__ccgo_up(a))).Freader.FiLookahead
		i = 0
		for {
			if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) {
				break
			}
			if (**(**TFts5NearTrimmer)(__ccgo_up(a + uintptr(i)*48))).Freader.FiLookahead < iMin {
				iMin = (**(**TFts5NearTrimmer)(__ccgo_up(a + uintptr(i)*48))).Freader.FiLookahead
				iAdv = i
			}
			goto _4
		_4:
			;
			i = i + 1
		}
		if _fts5LookaheadReaderNext(tls, a+uintptr(iAdv)*48) != 0 {
			goto ismatch_out
		}
	}
	goto ismatch_out
ismatch_out:
	;
	bRet = libc.BoolInt32((*TFts5Buffer)(unsafe.Pointer((**(**TFts5NearTrimmer)(__ccgo_up(a))).FpOut)).Fn > 0)
	**(**int32)(__ccgo_up(pRc)) = **(**int32)(__ccgo_up(bp + 192))
	if a != bp {
		Xsqlite3_free(tls, a)
	}
	return bRet
	return r
}

func _fts5ExprNearTest(tls *libc.TLS, pRc uintptr, pExpr uintptr, pNode uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i int32
	var pIter, pIter1, pNear, pPhrase, pPhrase1, pTerm uintptr
	var _ /* bMatch at bp+4 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _ = i, pIter, pIter1, pNear, pPhrase, pPhrase1, pTerm
	pNear = (*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear
	**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(pRc))
	if (*TFts5Config)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig)).FeDetail != FTS5_DETAIL_FULL {
		pPhrase = *(*uintptr)(unsafe.Pointer(pNear + 12))
		(*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn = 0
		pTerm = pPhrase + 20
		for {
			if !(pTerm != 0) {
				break
			}
			pIter = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter
			if int32((*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof) == 0 {
				if (*TFts5IndexIter)(unsafe.Pointer(pIter)).FiRowid == (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid && (*TFts5IndexIter)(unsafe.Pointer(pIter)).FnData > 0 {
					(*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn = int32(1)
				}
			}
			goto _1
		_1:
			;
			pTerm = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym
		}
		return (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn
	} else {
		/* Check that each phrase in the nearset matches the current row.
		 ** Populate the pPhrase->poslist buffers at the same time. If any
		 ** phrase is not a match, break out of the loop early.  */
		i = 0
		for {
			if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) {
				break
			}
			pPhrase1 = *(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(i)*4))
			if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase1)).FnTerm > int32(1) || (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase1 + 20))).FpSynonym != 0 || (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset != 0 || (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase1 + 20))).FbFirst != 0 {
				**(**int32)(__ccgo_up(bp + 4)) = 0
				**(**int32)(__ccgo_up(bp)) = _fts5ExprPhraseIsMatch(tls, pNode, pPhrase1, bp+4)
				if **(**int32)(__ccgo_up(bp + 4)) == 0 {
					break
				}
			} else {
				pIter1 = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase1 + 20))).FpIter
				_sqlite3Fts5BufferSet(tls, bp, pPhrase1+4, (*TFts5IndexIter)(unsafe.Pointer(pIter1)).FnData, (*TFts5IndexIter)(unsafe.Pointer(pIter1)).FpData)
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		**(**int32)(__ccgo_up(pRc)) = **(**int32)(__ccgo_up(bp))
		if i == (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase && (i == int32(1) || _fts5ExprNearIsMatch(tls, pRc, pNear) != 0) {
			return int32(1)
		}
		return 0
	}
	return r
}

// C documentation
//
//	/*
//	** Advance the first term iterator in the first phrase of pNear. Set output
//	** variable *pbEof to true if it reaches EOF or if an error occurs.
//	**
//	** Return SQLITE_OK if successful, or an SQLite error code if an error
//	** occurs.
//	*/
func _fts5ExprNodeNext_STRING(tls *libc.TLS, pExpr uintptr, pNode uintptr, bFromValid int32, iFrom Ti64) (r int32) {
	var bEof, rc int32
	var iRowid, ii Ti64
	var p, pIter, pTerm uintptr
	_, _, _, _, _, _, _ = bEof, iRowid, ii, p, pIter, pTerm, rc
	pTerm = *(*uintptr)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear + 12)) + 20
	rc = SQLITE_OK
	(*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0
	if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym != 0 {
		bEof = int32(1)
		/* Find the firstest rowid any synonym points to. */
		iRowid = _fts5ExprSynonymRowid(tls, pTerm, (*TFts5Expr)(unsafe.Pointer(pExpr)).FbDesc, uintptr(0))
		/* Advance each iterator that currently points to iRowid. Or, if iFrom
		 ** is valid - each iterator that points to a rowid before iFrom.  */
		p = pTerm
		for {
			if !(p != 0) {
				break
			}
			if int32((*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FbEof) == 0 {
				ii = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FiRowid
				if ii == iRowid || bFromValid != 0 && ii != iFrom && libc.BoolInt32(ii > iFrom) == (*TFts5Expr)(unsafe.Pointer(pExpr)).FbDesc {
					if bFromValid != 0 {
						rc = _sqlite3Fts5IterNextFrom(tls, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter, iFrom)
					} else {
						rc = _sqlite3Fts5IterNext(tls, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)
					}
					if rc != SQLITE_OK {
						break
					}
					if int32((*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter)).FbEof) == 0 {
						bEof = 0
					}
				} else {
					bEof = 0
				}
			}
			goto _1
		_1:
			;
			p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym
		}
		/* Set the EOF flag if either all synonym iterators are at EOF or an
		 ** error has occurred.  */
		(*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = libc.BoolInt32(rc != 0 || bEof != 0)
	} else {
		pIter = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter
		if bFromValid != 0 {
			rc = _sqlite3Fts5IterNextFrom(tls, pIter, iFrom)
		} else {
			rc = _sqlite3Fts5IterNext(tls, pIter)
		}
		(*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = libc.BoolInt32(rc != 0 || (*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof != 0)
	}
	if (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof == 0 {
		rc = _fts5ExprNodeTest_STRING(tls, pExpr, pNode)
	}
	return rc
}

// C documentation
//
//	/*
//	** xNext() method for a node of type FTS5_TERM.
//	*/
func _fts5ExprNodeNext_TERM(tls *libc.TLS, pExpr uintptr, pNode uintptr, bFromValid int32, iFrom Ti64) (r int32) {
	var pIter uintptr
	var rc int32
	_, _ = pIter, rc
	pIter = (*(*TFts5ExprTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pNode)).FpNear + 12)) + 20))).FpIter
	if bFromValid != 0 {
		rc = _sqlite3Fts5IterNextFrom(tls, pIter, iFrom)
	} else {
		rc = _sqlite3Fts5IterNext(tls, pIter)
	}
	if rc == SQLITE_OK && int32((*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof) == 0 {
		rc = _fts5ExprNodeTest_TERM(tls, pExpr, pNode)
	} else {
		(*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = int32(1)
		(*TFts5ExprNode)(unsafe.Pointer(pNode)).FbNomatch = 0
	}
	return rc
}

// C documentation
//
//	/*
//	** All individual term iterators in pPhrase are guaranteed to be valid and
//	** pointing to the same rowid when this function is called. This function
//	** checks if the current rowid really is a match, and if so populates
//	** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
//	** is set to true if this is really a match, or false otherwise.
//	**
//	** SQLITE_OK is returned if an error occurs, or an SQLite error code
//	** otherwise. It is not considered an error code if the current rowid is
//	** not a match.
//	*/
func _fts5ExprPhraseIsMatch(tls *libc.TLS, pNode uintptr, pPhrase uintptr, pbMatch uintptr) (r int32) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var aIter, pPos, pTerm uintptr
	var bFirst, bFlag, bMatch, i, rc int32
	var iAdj, iPos Ti64
	var nByte Tsqlite3_int64
	var _ /* a at bp+108 */ uintptr
	var _ /* aStatic at bp+8 */ [4]TFts5PoslistReader
	var _ /* buf at bp+112 */ TFts5Buffer
	var _ /* n at bp+104 */ int32
	var _ /* writer at bp+0 */ TFts5PoslistWriter
	_, _, _, _, _, _, _, _, _, _, _ = aIter, bFirst, bFlag, bMatch, i, iAdj, iPos, nByte, pPos, pTerm, rc
	**(**TFts5PoslistWriter)(__ccgo_up(bp)) = TFts5PoslistWriter{}
	aIter = bp + 8
	rc = SQLITE_OK
	bFirst = int32((*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20))).FbFirst)
	_sqlite3Fts5BufferZero(tls, pPhrase+4)
	/* If the aStatic[] array is not large enough, allocate a large array
	 ** using sqlite3_malloc(). This approach could be improved upon. */
	if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm > int32(libc.Uint32FromInt64(96)/libc.Uint32FromInt64(24)) {
		nByte = int64(uint32(24) * uint32((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm))
		aIter = Xsqlite3_malloc64(tls, uint64(nByte))
		if !(aIter != 0) {
			return int32(SQLITE_NOMEM)
		}
	}
	libc.Xmemset(tls, aIter, 0, uint32(24)*uint32((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm))
	/* Initialize a term iterator for each term in the phrase */
	i = 0
	for {
		if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) {
			break
		}
		pTerm = pPhrase + 20 + uintptr(i)*24
		**(**int32)(__ccgo_up(bp + 104)) = 0
		bFlag = 0
		**(**uintptr)(__ccgo_up(bp + 108)) = uintptr(0)
		if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym != 0 {
			**(**TFts5Buffer)(__ccgo_up(bp + 112)) = TFts5Buffer{}
			rc = _fts5ExprSynonymList(tls, pTerm, (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid, bp+112, bp+108, bp+104)
			if rc != 0 {
				Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 108)))
				goto ismatch_out
			}
			if **(**uintptr)(__ccgo_up(bp + 108)) == (**(**TFts5Buffer)(__ccgo_up(bp + 112))).Fp {
				bFlag = int32(1)
			}
		} else {
			**(**uintptr)(__ccgo_up(bp + 108)) = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter)).FpData
			**(**int32)(__ccgo_up(bp + 104)) = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter)).FnData
		}
		_sqlite3Fts5PoslistReaderInit(tls, **(**uintptr)(__ccgo_up(bp + 108)), **(**int32)(__ccgo_up(bp + 104)), aIter+uintptr(i)*24)
		(**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).FbFlag = uint8(bFlag)
		if (**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).FbEof != 0 {
			goto ismatch_out
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	for int32(1) != 0 {
		iPos = (**(**TFts5PoslistReader)(__ccgo_up(aIter))).FiPos
		for cond := true; cond; cond = bMatch == 0 {
			bMatch = int32(1)
			i = 0
			for {
				if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) {
					break
				}
				pPos = aIter + uintptr(i)*24
				iAdj = iPos + int64(i)
				if (*TFts5PoslistReader)(unsafe.Pointer(pPos)).FiPos != iAdj {
					bMatch = 0
					for (*TFts5PoslistReader)(unsafe.Pointer(pPos)).FiPos < iAdj {
						if _sqlite3Fts5PoslistReaderNext(tls, pPos) != 0 {
							goto ismatch_out
						}
					}
					if (*TFts5PoslistReader)(unsafe.Pointer(pPos)).FiPos > iAdj {
						iPos = (*TFts5PoslistReader)(unsafe.Pointer(pPos)).FiPos - int64(i)
					}
				}
				goto _2
			_2:
				;
				i = i + 1
			}
		}
		/* Append position iPos to the output */
		if bFirst == 0 || int32(iPos&libc.Int64FromInt32(0x7FFFFFFF)) == 0 {
			rc = _sqlite3Fts5PoslistWriterAppend(tls, pPhrase+4, bp, iPos)
			if rc != SQLITE_OK {
				goto ismatch_out
			}
		}
		i = 0
		for {
			if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) {
				break
			}
			if _sqlite3Fts5PoslistReaderNext(tls, aIter+uintptr(i)*24) != 0 {
				goto ismatch_out
			}
			goto _3
		_3:
			;
			i = i + 1
		}
	}
	goto ismatch_out
ismatch_out:
	;
	**(**int32)(__ccgo_up(pbMatch)) = libc.BoolInt32((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn > 0)
	i = 0
	for {
		if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) {
			break
		}
		if (**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).FbFlag != 0 {
			Xsqlite3_free(tls, (**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).Fa)
		}
		goto _4
	_4:
		;
		i = i + 1
	}
	if aIter != bp+8 {
		Xsqlite3_free(tls, aIter)
	}
	return rc
}

func _fts5ExprPopulatePoslistsCb(tls *libc.TLS, pCtx uintptr, tflags int32, pToken uintptr, nToken int32, iUnused1 int32, iUnused2 int32) (r int32) {
	var i, iCol, iTokOff, nQuery, rc int32
	var iRowid Ti64
	var p, pExpr, pT uintptr
	_, _, _, _, _, _, _, _, _ = i, iCol, iRowid, iTokOff, nQuery, p, pExpr, pT, rc
	p = pCtx
	pExpr = (*TFts5ExprCtx)(unsafe.Pointer(p)).FpExpr
	nQuery = nToken
	iRowid = (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpRoot)).FiRowid
	_ = iUnused1
	_ = iUnused2
	if nQuery > int32(FTS5_MAX_TOKEN_SIZE) {
		nQuery = int32(FTS5_MAX_TOKEN_SIZE)
	}
	if (*TFts5Config)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig)).FbTokendata != 0 {
		nQuery = _fts5QueryTerm(tls, pToken, nQuery)
	}
	if tflags&int32(FTS5_TOKEN_COLOCATED) == 0 {
		(*TFts5ExprCtx)(unsafe.Pointer(p)).FiOff = (*TFts5ExprCtx)(unsafe.Pointer(p)).FiOff + 1
	}
	i = 0
	for {
		if !(i < (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase) {
			break
		}
		if (**(**TFts5PoslistPopulator)(__ccgo_up((*TFts5ExprCtx)(unsafe.Pointer(p)).FaPopulator + uintptr(i)*16))).FbOk == 0 {
			goto _1
		}
		pT = **(**uintptr)(__ccgo_up((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*4)) + 20
		for {
			if !(pT != 0) {
				break
			}
			if ((*TFts5ExprTerm)(unsafe.Pointer(pT)).FnQueryTerm == nQuery || (*TFts5ExprTerm)(unsafe.Pointer(pT)).FnQueryTerm < nQuery && (*TFts5ExprTerm)(unsafe.Pointer(pT)).FbPrefix != 0) && libc.Xmemcmp(tls, (*TFts5ExprTerm)(unsafe.Pointer(pT)).FpTerm, pToken, uint32((*TFts5ExprTerm)(unsafe.Pointer(pT)).FnQueryTerm)) == 0 {
				rc = _sqlite3Fts5PoslistWriterAppend(tls, **(**uintptr)(__ccgo_up((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*4))+4, (*TFts5ExprCtx)(unsafe.Pointer(p)).FaPopulator+uintptr(i)*16, (*TFts5ExprCtx)(unsafe.Pointer(p)).FiOff)
				if rc == SQLITE_OK && ((*TFts5Config)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig)).FbTokendata != 0 || (*TFts5ExprTerm)(unsafe.Pointer(pT)).FbPrefix != 0) {
					iCol = int32((*TFts5ExprCtx)(unsafe.Pointer(p)).FiOff >> int32(32))
					iTokOff = int32((*TFts5ExprCtx)(unsafe.Pointer(p)).FiOff & int64(0x7FFFFFFF))
					rc = _sqlite3Fts5IndexIterWriteTokendata(tls, (*TFts5ExprTerm)(unsafe.Pointer(pT)).FpIter, pToken, nToken, iRowid, iCol, iTokOff)
				}
				if rc != 0 {
					return rc
				}
				break
			}
			goto _2
		_2:
			;
			pT = (*TFts5ExprTerm)(unsafe.Pointer(pT)).FpSynonym
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Argument pTerm must be a synonym iterator.
//	*/
func _fts5ExprSynonymList(tls *libc.TLS, pTerm uintptr, iRowid Ti64, pBuf uintptr, pa uintptr, pn uintptr) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var aIter, aNew, p, pIter uintptr
	var i, nAlloc, nIter, rc int32
	var iMin, iPrev Ti64
	var nByte Tsqlite3_int64
	var _ /* aStatic at bp+0 */ [4]TFts5PoslistReader
	var _ /* writer at bp+96 */ TFts5PoslistWriter
	_, _, _, _, _, _, _, _, _, _, _ = aIter, aNew, i, iMin, iPrev, nAlloc, nByte, nIter, p, pIter, rc
	aIter = bp
	nIter = 0
	nAlloc = int32(4)
	rc = SQLITE_OK
	p = pTerm
	for {
		if !(p != 0) {
			break
		}
		pIter = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpIter
		if int32((*TFts5IndexIter)(unsafe.Pointer(pIter)).FbEof) == 0 && (*TFts5IndexIter)(unsafe.Pointer(pIter)).FiRowid == iRowid {
			if (*TFts5IndexIter)(unsafe.Pointer(pIter)).FnData == 0 {
				goto _1
			}
			if nIter == nAlloc {
				nByte = int64(uint32(24) * uint32(nAlloc) * uint32(2))
				aNew = Xsqlite3_malloc64(tls, uint64(nByte))
				if aNew == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
					goto synonym_poslist_out
				}
				libc.Xmemcpy(tls, aNew, aIter, uint32(24)*uint32(nIter))
				nAlloc = nAlloc * int32(2)
				if aIter != bp {
					Xsqlite3_free(tls, aIter)
				}
				aIter = aNew
			}
			_sqlite3Fts5PoslistReaderInit(tls, (*TFts5IndexIter)(unsafe.Pointer(pIter)).FpData, (*TFts5IndexIter)(unsafe.Pointer(pIter)).FnData, aIter+uintptr(nIter)*24)
			nIter = nIter + 1
		}
		goto _1
	_1:
		;
		p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym
	}
	if nIter == int32(1) {
		**(**uintptr)(__ccgo_up(pa)) = (**(**TFts5PoslistReader)(__ccgo_up(aIter))).Fa
		**(**int32)(__ccgo_up(pn)) = (**(**TFts5PoslistReader)(__ccgo_up(aIter))).Fn
	} else {
		**(**TFts5PoslistWriter)(__ccgo_up(bp + 96)) = TFts5PoslistWriter{}
		iPrev = int64(-int32(1))
		_sqlite3Fts5BufferZero(tls, pBuf)
		for int32(1) != 0 {
			iMin = libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)
			i = 0
			for {
				if !(i < nIter) {
					break
				}
				if int32((**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).FbEof) == 0 {
					if (**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).FiPos == iPrev {
						if _sqlite3Fts5PoslistReaderNext(tls, aIter+uintptr(i)*24) != 0 {
							goto _2
						}
					}
					if (**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).FiPos < iMin {
						iMin = (**(**TFts5PoslistReader)(__ccgo_up(aIter + uintptr(i)*24))).FiPos
					}
				}
				goto _2
			_2:
				;
				i = i + 1
			}
			if iMin == libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32) || rc != SQLITE_OK {
				break
			}
			rc = _sqlite3Fts5PoslistWriterAppend(tls, pBuf, bp+96, iMin)
			iPrev = iMin
		}
		if rc == SQLITE_OK {
			**(**uintptr)(__ccgo_up(pa)) = (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp
			**(**int32)(__ccgo_up(pn)) = (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn
		}
	}
	goto synonym_poslist_out
synonym_poslist_out:
	;
	if aIter != bp {
		Xsqlite3_free(tls, aIter)
	}
	return rc
}

// C documentation
//
//	/*
//	** Argument pVal is the text of a full-text search expression. It may or
//	** may not have been wrapped by fts5_locale(). This function extracts
//	** the text of the expression, and sets output variable (*pzText) to
//	** point to a nul-terminated buffer containing the expression.
//	**
//	** If pVal was an fts5_locale() value, then sqlite3Fts5SetLocale() is called
//	** to set the tokenizer to use the specified locale.
//	**
//	** If output variable (*pbFreeAndReset) is set to true, then the caller
//	** is required to (a) call sqlite3Fts5ClearLocale() to reset the tokenizer
//	** locale, and (b) call sqlite3_free() to free (*pzText).
//	*/
func _fts5ExtractExprText(tls *libc.TLS, pConfig uintptr, pVal uintptr, pzText uintptr, pbFreeAndReset uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var _ /* nLoc at bp+16 */ int32
	var _ /* nText at bp+8 */ int32
	var _ /* pLoc at bp+12 */ uintptr
	var _ /* pText at bp+4 */ uintptr
	var _ /* rc at bp+0 */ int32
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	if _sqlite3Fts5IsLocaleValue(tls, pConfig, pVal) != 0 {
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
		**(**int32)(__ccgo_up(bp + 8)) = 0
		**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
		**(**int32)(__ccgo_up(bp + 16)) = 0
		**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5DecodeLocaleValue(tls, pVal, bp+4, bp+8, bp+12, bp+16)
		**(**uintptr)(__ccgo_up(pzText)) = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+13263, libc.VaList(bp+32, **(**int32)(__ccgo_up(bp + 8)), **(**uintptr)(__ccgo_up(bp + 4))))
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			_sqlite3Fts5SetLocale(tls, pConfig, **(**uintptr)(__ccgo_up(bp + 12)), **(**int32)(__ccgo_up(bp + 16)))
		}
		**(**int32)(__ccgo_up(pbFreeAndReset)) = int32(1)
	} else {
		**(**uintptr)(__ccgo_up(pzText)) = Xsqlite3_value_text(tls, pVal)
		**(**int32)(__ccgo_up(pbFreeAndReset)) = 0
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** This is the xFilter interface for the virtual table.  See
//	** the virtual table xFilter method documentation for additional
//	** information.
//	**
//	** There are three possible query strategies:
//	**
//	**   1. Full-text search using a MATCH operator.
//	**   2. A by-rowid lookup.
//	**   3. A full-table scan.
//	*/
func _fts5FilterMethod(tls *libc.TLS, pCursor uintptr, idxNum int32, idxStr uintptr, nVal int32, apVal uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bDesc, bGlob, bInternal, bOrderByRank, bPrefixInsttoken, i, iCol, iIdxStr, rc, v2, v3 int32
	var pConfig, pCsr, pRank, pRowidEq, pRowidGe, pRowidLe, pTab, pzErr, pzErrmsg, zText1, v6 uintptr
	var _ /* bFreeAndReset at bp+8 */ int32
	var _ /* pExpr at bp+0 */ uintptr
	var _ /* zText at bp+4 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bDesc, bGlob, bInternal, bOrderByRank, bPrefixInsttoken, i, iCol, iIdxStr, pConfig, pCsr, pRank, pRowidEq, pRowidGe, pRowidLe, pTab, pzErr, pzErrmsg, rc, zText1, v2, v3, v6
	pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab
	pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig
	pCsr = pCursor
	rc = SQLITE_OK        /* True if ORDER BY rank */
	pRank = uintptr(0)    /* rank MATCH ? expression (or NULL) */
	pRowidEq = uintptr(0) /* rowid = ? expression (or NULL) */
	pRowidLe = uintptr(0) /* rowid <= ? expression (or NULL) */
	pRowidGe = uintptr(0) /* Column on LHS of MATCH operator */
	pzErrmsg = (*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg
	bPrefixInsttoken = (*TFts5Config)(unsafe.Pointer(pConfig)).FbPrefixInsttoken
	iIdxStr = 0
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan != 0 {
		_fts5FreeCursorComponents(tls, pCsr)
		libc.Xmemset(tls, pCsr+24, 0, uint32(120)-uint32(int32(pCsr+24)-int32(pCsr)))
	}
	(*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg = pTab + 8
	/* Decode the arguments passed through to this function. */
	i = 0
	for {
		if !(i < nVal) {
			break
		}
		v2 = iIdxStr
		iIdxStr = iIdxStr + 1
		switch int32(**(**int8)(__ccgo_up(idxStr + uintptr(v2)))) {
		case int32('r'):
			pRank = **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4))
		case int32('M'):
			**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
			**(**int32)(__ccgo_up(bp + 8)) = 0
			bInternal = 0
			rc = _fts5ExtractExprText(tls, pConfig, **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4)), bp+4, bp+8)
			if rc != SQLITE_OK {
				goto filter_out
			}
			if **(**uintptr)(__ccgo_up(bp + 4)) == uintptr(0) {
				**(**uintptr)(__ccgo_up(bp + 4)) = __ccgo_ts + 1711
			}
			if Xsqlite3_value_subtype(tls, **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4))) == uint32(FTS5_INSTTOKEN_SUBTYPE) {
				(*TFts5Config)(unsafe.Pointer(pConfig)).FbPrefixInsttoken = int32(1)
			}
			iCol = 0
			for cond := true; cond; cond = int32(**(**int8)(__ccgo_up(idxStr + uintptr(iIdxStr)))) >= int32('0') && int32(**(**int8)(__ccgo_up(idxStr + uintptr(iIdxStr)))) <= int32('9') {
				iCol = iCol*int32(10) + (int32(**(**int8)(__ccgo_up(idxStr + uintptr(iIdxStr)))) - int32('0'))
				iIdxStr = iIdxStr + 1
			}
			if int32(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4))))) == int32('*') {
				/* The user has issued a query of the form "MATCH '*...'". This
				 ** indicates that the MATCH expression is not a full text query,
				 ** but a request for an internal parameter.  */
				rc = _fts5SpecialMatch(tls, pTab, pCsr, **(**uintptr)(__ccgo_up(bp + 4))+1)
				bInternal = int32(1)
			} else {
				pzErr = pTab + 8
				rc = _sqlite3Fts5ExprNew(tls, pConfig, 0, iCol, **(**uintptr)(__ccgo_up(bp + 4)), bp, pzErr)
				if rc == SQLITE_OK {
					rc = _sqlite3Fts5ExprAnd(tls, pCsr+52, **(**uintptr)(__ccgo_up(bp)))
					**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
				}
			}
			if **(**int32)(__ccgo_up(bp + 8)) != 0 {
				Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 4)))
				_sqlite3Fts5ClearLocale(tls, pConfig)
			}
			if bInternal != 0 || rc != SQLITE_OK {
				goto filter_out
			}
		case int32('L'):
			fallthrough
		case int32('G'):
			bGlob = libc.BoolInt32(int32(**(**int8)(__ccgo_up(idxStr + uintptr(iIdxStr-int32(1))))) == int32('G'))
			zText1 = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4)))
			iCol = 0
			for cond := true; cond; cond = int32(**(**int8)(__ccgo_up(idxStr + uintptr(iIdxStr)))) >= int32('0') && int32(**(**int8)(__ccgo_up(idxStr + uintptr(iIdxStr)))) <= int32('9') {
				iCol = iCol*int32(10) + (int32(**(**int8)(__ccgo_up(idxStr + uintptr(iIdxStr)))) - int32('0'))
				iIdxStr = iIdxStr + 1
			}
			if zText1 != 0 {
				rc = _sqlite3Fts5ExprPattern(tls, pConfig, bGlob, iCol, zText1, bp)
			}
			if rc == SQLITE_OK {
				rc = _sqlite3Fts5ExprAnd(tls, pCsr+52, **(**uintptr)(__ccgo_up(bp)))
				**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			}
			if rc != SQLITE_OK {
				goto filter_out
			}
		case int32('='):
			pRowidEq = **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4))
		case int32('<'):
			pRowidLe = **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4))
		default:
			pRowidGe = **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4))
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if idxNum&int32(FTS5_BI_ORDER_RANK) != 0 {
		v2 = int32(1)
	} else {
		v2 = 0
	}
	bOrderByRank = v2
	if idxNum&int32(FTS5_BI_ORDER_DESC) != 0 {
		v3 = int32(1)
	} else {
		v3 = 0
	}
	v2 = v3
	bDesc = v2
	(*TFts5Cursor)(unsafe.Pointer(pCsr)).FbDesc = v2
	/* Set the cursor upper and lower rowid limits. Only some strategies
	 ** actually use them. This is ok, as the xBestIndex() method leaves the
	 ** sqlite3_index_constraint.omit flag clear for range constraints
	 ** on the rowid field.  */
	if pRowidEq != 0 {
		v6 = pRowidEq
		pRowidGe = v6
		pRowidLe = v6
	}
	if bDesc != 0 {
		(*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid = _fts5GetRowidLimit(tls, pRowidLe, libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32))
		(*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid = _fts5GetRowidLimit(tls, pRowidGe, int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)))
	} else {
		(*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid = _fts5GetRowidLimit(tls, pRowidLe, libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32))
		(*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid = _fts5GetRowidLimit(tls, pRowidGe, int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)))
	}
	rc = _sqlite3Fts5IndexLoadConfig(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex)
	if rc != SQLITE_OK {
		goto filter_out
	}
	if (*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr != 0 {
		/* If pSortCsr is non-NULL, then this call is being made as part of
		 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
		 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
		 ** return results to the user for this query. The current cursor
		 ** (pCursor) is used to execute the query issued by function
		 ** fts5CursorFirstSorted() above.  */
		if (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FbDesc != 0 {
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FiFirstRowid
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FiLastRowid
		} else {
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FiLastRowid
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FiFirstRowid
		}
		(*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = int32(FTS5_PLAN_SOURCE)
		(*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr = (*TFts5Cursor)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).FpSortCsr)).FpExpr
		rc = _fts5CursorFirst(tls, pTab, pCsr, bDesc)
	} else {
		if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr != 0 {
			rc = _fts5CursorParseRank(tls, pConfig, pCsr, pRank)
			if rc == SQLITE_OK {
				if bOrderByRank != 0 {
					(*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = int32(FTS5_PLAN_SORTED_MATCH)
					rc = _fts5CursorFirstSorted(tls, pTab, pCsr, bDesc)
				} else {
					(*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = int32(FTS5_PLAN_MATCH)
					rc = _fts5CursorFirst(tls, pTab, pCsr, bDesc)
				}
			}
		} else {
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FzContent == uintptr(0) {
				_fts5SetVtabError(tls, pTab, __ccgo_ts+41154, libc.VaList(bp+24, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName))
				rc = int32(SQLITE_ERROR)
			} else {
				/* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
				 ** by rowid (ePlan==FTS5_PLAN_ROWID).  */
				if pRowidEq != 0 {
					v2 = int32(FTS5_PLAN_ROWID)
				} else {
					v2 = int32(FTS5_PLAN_SCAN)
				}
				(*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = v2
				rc = _sqlite3Fts5StorageStmt(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, _fts5StmtType(tls, pCsr), pCsr+48, pTab+8)
				if rc == SQLITE_OK {
					if pRowidEq != uintptr(0) {
						Xsqlite3_bind_value(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, int32(1), pRowidEq)
					} else {
						Xsqlite3_bind_int64(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, int32(1), (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiFirstRowid)
						Xsqlite3_bind_int64(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, int32(2), (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid)
					}
					rc = _fts5NextMethod(tls, pCursor)
				}
			}
		}
	}
	goto filter_out
filter_out:
	;
	_sqlite3Fts5ExprFree(tls, **(**uintptr)(__ccgo_up(bp)))
	(*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg = pzErrmsg
	(*TFts5Config)(unsafe.Pointer(pConfig)).FbPrefixInsttoken = bPrefixInsttoken
	return rc
}

func _fts5FindRankFunction(tls *libc.TLS, pCsr uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i int32
	var nByte Tsqlite3_int64
	var pAux, pConfig, pTab, zRank, zRankArgs, zSql uintptr
	var _ /* pStmt at bp+4 */ uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _ = i, nByte, pAux, pConfig, pTab, zRank, zRankArgs, zSql
	pTab = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab
	pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	pAux = uintptr(0)
	zRank = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank
	zRankArgs = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs
	if zRankArgs != 0 {
		zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+41090, libc.VaList(bp+16, zRankArgs))
		if zSql != 0 {
			**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
			**(**int32)(__ccgo_up(bp)) = Xsqlite3_prepare_v3(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, zSql, -int32(1), uint32(SQLITE_PREPARE_PERSISTENT), bp+4, uintptr(0))
			Xsqlite3_free(tls, zSql)
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				if int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4))) {
					(*TFts5Cursor)(unsafe.Pointer(pCsr)).FnRankArg = Xsqlite3_column_count(tls, **(**uintptr)(__ccgo_up(bp + 4)))
					nByte = int64(uint32(4) * uint32((*TFts5Cursor)(unsafe.Pointer(pCsr)).FnRankArg))
					(*TFts5Cursor)(unsafe.Pointer(pCsr)).FapRankArg = _sqlite3Fts5MallocZero(tls, bp, nByte)
					if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
						i = 0
						for {
							if !(i < (*TFts5Cursor)(unsafe.Pointer(pCsr)).FnRankArg) {
								break
							}
							**(**uintptr)(__ccgo_up((*TFts5Cursor)(unsafe.Pointer(pCsr)).FapRankArg + uintptr(i)*4)) = Xsqlite3_column_value(tls, **(**uintptr)(__ccgo_up(bp + 4)), i)
							goto _1
						_1:
							;
							i = i + 1
						}
					}
					(*TFts5Cursor)(unsafe.Pointer(pCsr)).FpRankArgStmt = **(**uintptr)(__ccgo_up(bp + 4))
				} else {
					**(**int32)(__ccgo_up(bp)) = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 4)))
				}
			}
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		pAux = _fts5FindAuxiliary(tls, pTab, zRank)
		if pAux == uintptr(0) {
			(*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+41100, libc.VaList(bp+16, zRank))
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
		}
	}
	(*TFts5Cursor)(unsafe.Pointer(pCsr)).FpRank = pAux
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Flush the contents of in-memory hash table iHash to a new level-0
//	** segment on disk. Also update the corresponding structure record.
//	**
//	** If an error occurs, set the Fts5Index.rc error code. If an error has
//	** already occurred, this function is a no-op.
//	*/
func _fts5FlushOneHash(tls *libc.TLS, p uintptr) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var bSecureDelete, bTermWritten, eDetail, iOff, iPos, iSegid, n, nCopy, nSpace, pgsz, v1 int32
	var iPrev, iRowid Ti64
	var iRowidDelta Tu64
	var pBuf, pHash, pPgidx, pPoslist, pSeg, v2 uintptr
	var _ /* bDel at bp+128 */ int32
	var _ /* iDelta at bp+120 */ Tu64
	var _ /* nDoclist at bp+116 */ int32
	var _ /* nPos at bp+132 */ int32
	var _ /* nTerm at bp+108 */ int32
	var _ /* pDoclist at bp+112 */ uintptr
	var _ /* pStruct at bp+0 */ uintptr
	var _ /* pgnoLast at bp+4 */ int32
	var _ /* writer at bp+8 */ TFts5SegWriter
	var _ /* zTerm at bp+104 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bSecureDelete, bTermWritten, eDetail, iOff, iPos, iPrev, iRowid, iRowidDelta, iSegid, n, nCopy, nSpace, pBuf, pHash, pPgidx, pPoslist, pSeg, pgsz, v1, v2
	pHash = (*TFts5Index)(unsafe.Pointer(p)).FpHash
	**(**int32)(__ccgo_up(bp + 4)) = 0 /* Last leaf page number in segment */
	/* Obtain a reference to the index structure and allocate a new segment-id
	 ** for the new level-0 segment.  */
	**(**uintptr)(__ccgo_up(bp)) = _fts5StructureRead(tls, p)
	_fts5StructureInvalidate(tls, p)
	if _sqlite3Fts5HashIsEmpty(tls, pHash) == 0 {
		iSegid = _fts5AllocateSegid(tls, p, **(**uintptr)(__ccgo_up(bp)))
		if iSegid != 0 {
			pgsz = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz
			eDetail = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail
			bSecureDelete = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FbSecureDelete
			_fts5WriteInit(tls, p, bp+8, iSegid)
			pBuf = bp + 8 + 4 + 8
			pPgidx = bp + 8 + 4 + 20
			/* fts5WriteInit() should have initialized the buffers to (most likely)
			 ** the maximum space required. */
			/* Begin scanning through hash table entries. This loop runs once for each
			 ** term/doclist currently stored within the hash table. */
			if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				(*TFts5Index)(unsafe.Pointer(p)).Frc = _sqlite3Fts5HashScanInit(tls, pHash, uintptr(0), 0)
			}
			for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && 0 == _sqlite3Fts5HashScanEof(tls, pHash) { /* Size of doclist in bytes */
				/* Get the term and doclist for this entry. */
				_sqlite3Fts5HashScanEntry(tls, pHash, bp+104, bp+108, bp+112, bp+116)
				if bSecureDelete == 0 {
					_fts5WriteAppendTerm(tls, p, bp+8, **(**int32)(__ccgo_up(bp + 108)), **(**uintptr)(__ccgo_up(bp + 104)))
					if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK {
						break
					}
				}
				if !(bSecureDelete != 0) && pgsz >= (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn+**(**int32)(__ccgo_up(bp + 116))+int32(1) {
					/* The entire doclist will fit on the current leaf. */
					libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), **(**uintptr)(__ccgo_up(bp + 112)), uint32(**(**int32)(__ccgo_up(bp + 116))))
					**(**int32)(__ccgo_up(pBuf + 4)) += **(**int32)(__ccgo_up(bp + 116))
				} else {
					bTermWritten = libc.BoolInt32(!(bSecureDelete != 0))
					iRowid = 0
					iPrev = 0
					iOff = 0
					/* The entire doclist will not fit on this leaf. The following
					 ** loop iterates through the poslists that make up the current
					 ** doclist.  */
					for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && iOff < **(**int32)(__ccgo_up(bp + 116)) {
						**(**Tu64)(__ccgo_up(bp + 120)) = uint64(0)
						iOff = iOff + int32(_sqlite3Fts5GetVarint(tls, **(**uintptr)(__ccgo_up(bp + 112))+uintptr(iOff), bp+120))
						iRowid = int64(uint64(iRowid) + **(**Tu64)(__ccgo_up(bp + 120)))
						/* If in secure delete mode, and if this entry in the poslist is
						 ** in fact a delete, then edit the existing segments directly
						 ** using fts5FlushSecureDelete().  */
						if bSecureDelete != 0 {
							if eDetail == int32(FTS5_DETAIL_NONE) {
								if iOff < **(**int32)(__ccgo_up(bp + 116)) && int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 112)) + uintptr(iOff)))) == 0x00 && !(_fts5FlushSecureDelete(tls, p, **(**uintptr)(__ccgo_up(bp)), **(**uintptr)(__ccgo_up(bp + 104)), **(**int32)(__ccgo_up(bp + 108)), iRowid) != 0) {
									iOff = iOff + 1
									if iOff < **(**int32)(__ccgo_up(bp + 116)) && int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 112)) + uintptr(iOff)))) == 0x00 {
										iOff = iOff + 1
										**(**int32)(__ccgo_up(bp + 116)) = 0
									} else {
										continue
									}
								}
							} else {
								if int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 112)) + uintptr(iOff))))&int32(0x01) != 0 && !(_fts5FlushSecureDelete(tls, p, **(**uintptr)(__ccgo_up(bp)), **(**uintptr)(__ccgo_up(bp + 104)), **(**int32)(__ccgo_up(bp + 108)), iRowid) != 0) {
									if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK || int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 112)) + uintptr(iOff)))) == int32(0x01) {
										iOff = iOff + 1
										continue
									}
								}
							}
						}
						if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bTermWritten == 0 {
							_fts5WriteAppendTerm(tls, p, bp+8, **(**int32)(__ccgo_up(bp + 108)), **(**uintptr)(__ccgo_up(bp + 104)))
							bTermWritten = int32(1)
						}
						if (**(**TFts5SegWriter)(__ccgo_up(bp + 8))).FbFirstRowidInPage != 0 {
							_fts5PutU16(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp, uint16((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)) /* first rowid on page */
							**(**int32)(__ccgo_up(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(iRowid))
							(**(**TFts5SegWriter)(__ccgo_up(bp + 8))).FbFirstRowidInPage = uint8(0)
							_fts5WriteDlidxAppend(tls, p, bp+8, iRowid)
						} else {
							iRowidDelta = uint64(iRowid) - uint64(iPrev)
							**(**int32)(__ccgo_up(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), iRowidDelta)
						}
						if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK {
							break
						}
						iPrev = iRowid
						if eDetail == int32(FTS5_DETAIL_NONE) {
							if iOff < **(**int32)(__ccgo_up(bp + 116)) && int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 112)) + uintptr(iOff)))) == 0 {
								v2 = pBuf + 4
								v1 = *(*int32)(unsafe.Pointer(v2))
								*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
								**(**Tu8)(__ccgo_up((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr(v1))) = uint8(0)
								iOff = iOff + 1
								if iOff < **(**int32)(__ccgo_up(bp + 116)) && int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 112)) + uintptr(iOff)))) == 0 {
									v2 = pBuf + 4
									v1 = *(*int32)(unsafe.Pointer(v2))
									*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
									**(**Tu8)(__ccgo_up((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr(v1))) = uint8(0)
									iOff = iOff + 1
								}
							}
							if (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn >= pgsz {
								_fts5WriteFlushLeaf(tls, p, bp+8)
							}
						} else {
							**(**int32)(__ccgo_up(bp + 128)) = 0
							**(**int32)(__ccgo_up(bp + 132)) = 0
							nCopy = _fts5GetPoslistSize(tls, **(**uintptr)(__ccgo_up(bp + 112))+uintptr(iOff), bp+132, bp+128)
							if **(**int32)(__ccgo_up(bp + 128)) != 0 && bSecureDelete != 0 {
								_sqlite3Fts5BufferAppendVarint(tls, p+44, pBuf, int64(**(**int32)(__ccgo_up(bp + 132)))*int64(2))
								iOff = iOff + nCopy
								nCopy = **(**int32)(__ccgo_up(bp + 132))
							} else {
								nCopy = nCopy + **(**int32)(__ccgo_up(bp + 132))
							}
							if (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn+nCopy <= pgsz {
								/* The entire poslist will fit on the current leaf. So copy
								 ** it in one go. */
								libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), **(**uintptr)(__ccgo_up(bp + 112))+uintptr(iOff), uint32(nCopy))
								**(**int32)(__ccgo_up(pBuf + 4)) += nCopy
							} else {
								/* The entire poslist will not fit on this leaf. So it needs
								 ** to be broken into sections. The only qualification being
								 ** that each varint must be stored contiguously.  */
								pPoslist = **(**uintptr)(__ccgo_up(bp + 112)) + uintptr(iOff)
								iPos = 0
								for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
									nSpace = pgsz - (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn - (*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn
									n = 0
									if nCopy-iPos <= nSpace {
										n = nCopy - iPos
									} else {
										n = _fts5PoslistPrefix(tls, pPoslist+uintptr(iPos), nSpace)
									}
									libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), pPoslist+uintptr(iPos), uint32(n))
									**(**int32)(__ccgo_up(pBuf + 4)) += n
									iPos = iPos + n
									if (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn >= pgsz {
										_fts5WriteFlushLeaf(tls, p, bp+8)
									}
									if iPos >= nCopy {
										break
									}
								}
							}
							iOff = iOff + nCopy
						}
					}
				}
				/* TODO2: Doclist terminator written here. */
				/* pBuf->p[pBuf->n++] = '\0'; */
				if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					_sqlite3Fts5HashScanNext(tls, pHash)
				}
			}
			_fts5WriteFinish(tls, p, bp+8, bp+4)
			if **(**int32)(__ccgo_up(bp + 4)) > 0 {
				/* Update the Fts5Structure. It is written back to the database by the
				 ** fts5StructureRelease() call below.  */
				if (*TFts5Structure)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnLevel == 0 {
					_fts5StructureAddLevel(tls, p+44, bp)
				}
				_fts5StructureExtendLevel(tls, p+44, **(**uintptr)(__ccgo_up(bp)), 0, int32(1), 0)
				if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					v2 = **(**uintptr)(__ccgo_up(bp)) + 32 + 4
					v1 = *(*int32)(unsafe.Pointer(v2))
					*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
					pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)) + 32))).FaSeg + uintptr(v1)*56
					(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid = iSegid
					(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst = int32(1)
					(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast = **(**int32)(__ccgo_up(bp + 4))
					if (*TFts5Structure)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnOriginCntr > uint64(0) {
						(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin1 = (*TFts5Structure)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnOriginCntr
						(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2 = (*TFts5Structure)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnOriginCntr
						(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntry = uint64((*TFts5Index)(unsafe.Pointer(p)).FnPendingRow)
						(*TFts5Structure)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnOriginCntr = (*TFts5Structure)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnOriginCntr + 1
					}
					(*TFts5Structure)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnSegment = (*TFts5Structure)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnSegment + 1
				}
				_fts5StructurePromote(tls, p, 0, **(**uintptr)(__ccgo_up(bp)))
			}
		}
	}
	_fts5IndexAutomerge(tls, p, bp, **(**int32)(__ccgo_up(bp + 4))+(*TFts5Index)(unsafe.Pointer(p)).FnContentlessDelete)
	_fts5IndexCrisismerge(tls, p, bp)
	_fts5StructureWrite(tls, p, **(**uintptr)(__ccgo_up(bp)))
	_fts5StructureRelease(tls, **(**uintptr)(__ccgo_up(bp)))
}

// C documentation
//
//	/*
//	** This is called as part of flushing a delete to disk in 'secure-delete'
//	** mode. It edits the segments within the database described by argument
//	** pStruct to remove the entries for term zTerm, rowid iRowid.
//	**
//	** Return SQLITE_OK if successful, or an SQLite error code if an error
//	** has occurred. Any error code is also stored in the Fts5Index handle.
//	*/
func _fts5FlushSecureDelete(tls *libc.TLS, p uintptr, pStruct uintptr, zTerm uintptr, nTerm int32, iRowid Ti64) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var f, rc int32
	var iThis Ti64
	var pConfig, pSeg uintptr
	var _ /* pIter at bp+0 */ uintptr
	var _ /* pStmt at bp+4 */ uintptr
	_, _, _, _, _ = f, iThis, pConfig, pSeg, rc
	f = int32(FTS5INDEX_QUERY_SKIPHASH)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* Used to find term instance */
	/* If the version number has not been set to SECUREDELETE, do so now. */
	if (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FiVersion != int32(FTS5_CURRENT_VERSION_SECUREDELETE) {
		pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
		_fts5IndexPrepareStmt(tls, p, bp+4, Xsqlite3_mprintf(tls, __ccgo_ts+40683, libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName, int32(FTS5_CURRENT_VERSION_SECUREDELETE))))
		if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4)))
			rc = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 4)))
			if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				(*TFts5Index)(unsafe.Pointer(p)).Frc = rc
			}
			(*TFts5Config)(unsafe.Pointer(pConfig)).FiCookie = (*TFts5Config)(unsafe.Pointer(pConfig)).FiCookie + 1
			(*TFts5Config)(unsafe.Pointer(pConfig)).FiVersion = int32(FTS5_CURRENT_VERSION_SECUREDELETE)
		}
	}
	_fts5MultiIterNew(tls, p, pStruct, f, uintptr(0), zTerm, nTerm, -int32(1), 0, bp)
	if _fts5MultiIterEof(tls, p, **(**uintptr)(__ccgo_up(bp))) == 0 {
		iThis = _fts5MultiIterRowid(tls, **(**uintptr)(__ccgo_up(bp)))
		if iThis < iRowid {
			_fts5MultiIterNextFrom(tls, p, **(**uintptr)(__ccgo_up(bp)), iRowid)
		}
		if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && _fts5MultiIterEof(tls, p, **(**uintptr)(__ccgo_up(bp))) == 0 && iRowid == _fts5MultiIterRowid(tls, **(**uintptr)(__ccgo_up(bp))) {
			pSeg = **(**uintptr)(__ccgo_up(bp)) + 80 + uintptr((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaFirst + 1*4))).FiFirst)*104
			_fts5DoSecureDelete(tls, p, pSeg)
		}
	}
	_fts5MultiIterFree(tls, **(**uintptr)(__ccgo_up(bp)))
	return (*TFts5Index)(unsafe.Pointer(p)).Frc
}

func _fts5FreeCursorComponents(tls *libc.TLS, pCsr uintptr) {
	var eStmt int32
	var pData, pNext, pSorter, pTab uintptr
	_, _, _, _, _ = eStmt, pData, pNext, pSorter, pTab
	pTab = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab
	Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInstIter)
	Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaInst)
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt != 0 {
		eStmt = _fts5StmtType(tls, pCsr)
		_sqlite3Fts5StorageStmtRelease(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, eStmt, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt)
	}
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter != 0 {
		pSorter = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpSorter
		Xsqlite3_finalize(tls, (*TFts5Sorter)(unsafe.Pointer(pSorter)).FpStmt)
		Xsqlite3_free(tls, pSorter)
	}
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan != int32(FTS5_PLAN_SOURCE) {
		_sqlite3Fts5ExprFree(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr)
	}
	pData = (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpAuxdata
	for {
		if !(pData != 0) {
			break
		}
		pNext = (*TFts5Auxdata)(unsafe.Pointer(pData)).FpNext
		if (*TFts5Auxdata)(unsafe.Pointer(pData)).FxDelete != 0 {
			(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Auxdata)(unsafe.Pointer(pData)).FxDelete})))(tls, (*TFts5Auxdata)(unsafe.Pointer(pData)).FpPtr)
		}
		Xsqlite3_free(tls, pData)
		goto _1
	_1:
		;
		pData = pNext
	}
	Xsqlite3_finalize(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpRankArgStmt)
	Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FapRankArg)
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fcsrflags&int32(FTS5CSR_FREE_ZRANK) != 0 {
		Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank)
		Xsqlite3_free(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs)
	}
	_sqlite3Fts5IndexCloseReader(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex)
	libc.Xmemset(tls, pCsr+24, 0, uint32(120)-uint32(int32(pCsr+24)-int32(pCsr)))
}

// C documentation
//
//	/*
//	** Implementation of fts5_get_locale() function.
//	*/
func _fts5GetLocaleFunction(tls *libc.TLS, pApi uintptr, pFts uintptr, pCtx uintptr, nVal int32, apVal uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var eType, iCol, rc int32
	var z, z1 uintptr
	var _ /* nLocale at bp+4 */ int32
	var _ /* zLocale at bp+0 */ uintptr
	_, _, _, _, _ = eType, iCol, rc, z, z1
	iCol = 0
	eType = 0
	rc = SQLITE_OK
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 4)) = 0
	/* xColumnLocale() must be available */
	if nVal != int32(1) {
		z = __ccgo_ts + 38491
		Xsqlite3_result_error(tls, pCtx, z, -int32(1))
		return
	}
	eType = Xsqlite3_value_numeric_type(tls, **(**uintptr)(__ccgo_up(apVal)))
	if eType != int32(SQLITE_INTEGER) {
		z1 = __ccgo_ts + 38547
		Xsqlite3_result_error(tls, pCtx, z1, -int32(1))
		return
	}
	iCol = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(apVal)))
	if iCol < 0 || iCol >= (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnCount})))(tls, pFts) {
		Xsqlite3_result_error_code(tls, pCtx, int32(SQLITE_RANGE))
		return
	}
	rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnLocale})))(tls, pFts, iCol, bp, bp+4)
	if rc != SQLITE_OK {
		Xsqlite3_result_error_code(tls, pCtx, rc)
		return
	}
	Xsqlite3_result_text(tls, pCtx, **(**uintptr)(__ccgo_up(bp)), **(**int32)(__ccgo_up(bp + 4)), uintptr(-libc.Int32FromInt32(1)))
}

func _fts5HashAddPoslistSize(tls *libc.TLS, pHash uintptr, p uintptr, p2 uintptr) (r int32) {
	var nByte, nData, nPos, nRet, nSz, v2 int32
	var pPtr, v1 uintptr
	_, _, _, _, _, _, _, _ = nByte, nData, nPos, nRet, nSz, pPtr, v1, v2
	nRet = 0
	if (*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist != 0 {
		if p2 != 0 {
			v1 = p2
		} else {
			v1 = p
		}
		pPtr = v1
		nData = (*TFts5HashEntry)(unsafe.Pointer(p)).FnData
		if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == int32(FTS5_DETAIL_NONE) {
			if (*TFts5HashEntry)(unsafe.Pointer(p)).FbDel != 0 {
				v2 = nData
				nData = nData + 1
				**(**Tu8)(__ccgo_up(pPtr + uintptr(v2))) = uint8(0x00)
				if (*TFts5HashEntry)(unsafe.Pointer(p)).FbContent != 0 {
					v2 = nData
					nData = nData + 1
					**(**Tu8)(__ccgo_up(pPtr + uintptr(v2))) = uint8(0x00)
				}
			}
		} else {
			nSz = nData - (*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist - int32(1) /* Size in bytes */
			nPos = nSz*int32(2) + int32((*TFts5HashEntry)(unsafe.Pointer(p)).FbDel)   /* Value of nPos field */
			if nPos <= int32(127) {
				**(**Tu8)(__ccgo_up(pPtr + uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist))) = uint8(nPos)
			} else {
				nByte = _sqlite3Fts5GetVarintLen(tls, uint32(nPos))
				libc.Xmemmove(tls, pPtr+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist+nByte), pPtr+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist+int32(1)), uint32(nSz))
				_sqlite3Fts5PutVarint(tls, pPtr+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist), uint64(nPos))
				nData = nData + (nByte - int32(1))
			}
		}
		nRet = nData - (*TFts5HashEntry)(unsafe.Pointer(p)).FnData
		if p2 == uintptr(0) {
			(*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist = 0
			(*TFts5HashEntry)(unsafe.Pointer(p)).FbDel = uint8(0)
			(*TFts5HashEntry)(unsafe.Pointer(p)).FbContent = uint8(0)
			(*TFts5HashEntry)(unsafe.Pointer(p)).FnData = nData
		}
	}
	return nRet
}

// C documentation
//
//	/*
//	** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
//	** each sorted in key order. This function merges the two lists into a
//	** single list and returns a pointer to its first element.
//	*/
func _fts5HashEntryMerge(tls *libc.TLS, pLeft uintptr, pRight uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var cmp, nMin, v1 int32
	var p1, p2, ppOut, zKey1, zKey2 uintptr
	var _ /* pRet at bp+0 */ uintptr
	_, _, _, _, _, _, _, _ = cmp, nMin, p1, p2, ppOut, zKey1, zKey2, v1
	p1 = pLeft
	p2 = pRight
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	ppOut = bp
	for p1 != 0 || p2 != 0 {
		if p1 == uintptr(0) {
			**(**uintptr)(__ccgo_up(ppOut)) = p2
			p2 = uintptr(0)
		} else {
			if p2 == uintptr(0) {
				**(**uintptr)(__ccgo_up(ppOut)) = p1
				p1 = uintptr(0)
			} else {
				zKey1 = p1 + 1*40
				zKey2 = p2 + 1*40
				if (*TFts5HashEntry)(unsafe.Pointer(p1)).FnKey < (*TFts5HashEntry)(unsafe.Pointer(p2)).FnKey {
					v1 = (*TFts5HashEntry)(unsafe.Pointer(p1)).FnKey
				} else {
					v1 = (*TFts5HashEntry)(unsafe.Pointer(p2)).FnKey
				}
				nMin = v1
				cmp = libc.Xmemcmp(tls, zKey1, zKey2, uint32(nMin))
				if cmp == 0 {
					cmp = (*TFts5HashEntry)(unsafe.Pointer(p1)).FnKey - (*TFts5HashEntry)(unsafe.Pointer(p2)).FnKey
				}
				if cmp > 0 {
					/* p2 is smaller */
					**(**uintptr)(__ccgo_up(ppOut)) = p2
					ppOut = p2 + 4
					p2 = (*TFts5HashEntry)(unsafe.Pointer(p2)).FpScanNext
				} else {
					/* p1 is smaller */
					**(**uintptr)(__ccgo_up(ppOut)) = p1
					ppOut = p1 + 4
					p1 = (*TFts5HashEntry)(unsafe.Pointer(p1)).FpScanNext
				}
				**(**uintptr)(__ccgo_up(ppOut)) = uintptr(0)
			}
		}
	}
	return **(**uintptr)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Link all tokens from hash table iHash into a list in sorted order. The
//	** tokens are not removed from the hash table.
//	*/
func _fts5HashEntrySort(tls *libc.TLS, pHash uintptr, pTerm uintptr, nTerm int32, ppSorted uintptr) (r int32) {
	var ap, pEntry, pIter, pList uintptr
	var i, iSlot, nMergeSlot int32
	_, _, _, _, _, _, _ = ap, i, iSlot, nMergeSlot, pEntry, pIter, pList
	nMergeSlot = int32(32)
	**(**uintptr)(__ccgo_up(ppSorted)) = uintptr(0)
	ap = Xsqlite3_malloc64(tls, uint64(uint32(4)*uint32(nMergeSlot)))
	if !(ap != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, ap, 0, uint32(4)*uint32(nMergeSlot))
	iSlot = 0
	for {
		if !(iSlot < (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot) {
			break
		}
		pIter = **(**uintptr)(__ccgo_up((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iSlot)*4))
		for {
			if !(pIter != 0) {
				break
			}
			if pTerm == uintptr(0) || (*TFts5HashEntry)(unsafe.Pointer(pIter)).FnKey >= nTerm && 0 == libc.Xmemcmp(tls, pIter+1*40, pTerm, uint32(nTerm)) {
				pEntry = pIter
				(*TFts5HashEntry)(unsafe.Pointer(pEntry)).FpScanNext = uintptr(0)
				i = 0
				for {
					if !(**(**uintptr)(__ccgo_up(ap + uintptr(i)*4)) != 0) {
						break
					}
					pEntry = _fts5HashEntryMerge(tls, pEntry, **(**uintptr)(__ccgo_up(ap + uintptr(i)*4)))
					**(**uintptr)(__ccgo_up(ap + uintptr(i)*4)) = uintptr(0)
					goto _3
				_3:
					;
					i = i + 1
				}
				**(**uintptr)(__ccgo_up(ap + uintptr(i)*4)) = pEntry
			}
			goto _2
		_2:
			;
			pIter = (*TFts5HashEntry)(unsafe.Pointer(pIter)).FpHashNext
		}
		goto _1
	_1:
		;
		iSlot = iSlot + 1
	}
	pList = uintptr(0)
	i = 0
	for {
		if !(i < nMergeSlot) {
			break
		}
		pList = _fts5HashEntryMerge(tls, pList, **(**uintptr)(__ccgo_up(ap + uintptr(i)*4)))
		goto _4
	_4:
		;
		i = i + 1
	}
	Xsqlite3_free(tls, ap)
	**(**uintptr)(__ccgo_up(ppSorted)) = pList
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Resize the hash table by doubling the number of slots.
//	*/
func _fts5HashResize(tls *libc.TLS, pHash uintptr) (r int32) {
	var apNew, apOld, p uintptr
	var i, nNew int32
	var iHash uint32
	_, _, _, _, _, _ = apNew, apOld, i, iHash, nNew, p
	nNew = (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot * int32(2)
	apOld = (*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot
	apNew = Xsqlite3_malloc64(tls, uint64(uint32(nNew)*uint32(4)))
	if !(apNew != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, apNew, 0, uint32(nNew)*uint32(4))
	i = 0
	for {
		if !(i < (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot) {
			break
		}
		for **(**uintptr)(__ccgo_up(apOld + uintptr(i)*4)) != 0 {
			p = **(**uintptr)(__ccgo_up(apOld + uintptr(i)*4))
			**(**uintptr)(__ccgo_up(apOld + uintptr(i)*4)) = (*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext
			iHash = _fts5HashKey(tls, nNew, p+1*40, (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey)
			(*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext = **(**uintptr)(__ccgo_up(apNew + uintptr(iHash)*4))
			**(**uintptr)(__ccgo_up(apNew + uintptr(iHash)*4)) = p
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	Xsqlite3_free(tls, apOld)
	(*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot = nNew
	(*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot = apNew
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Implementation of highlight() function.
//	*/
func _fts5HighlightFunction(tls *libc.TLS, pApi uintptr, pFts uintptr, pCtx uintptr, nVal int32, apVal uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var iCol int32
	var zErr uintptr
	var _ /* ctx at bp+0 */ THighlightContext
	var _ /* nLoc at bp+76 */ int32
	var _ /* pLoc at bp+72 */ uintptr
	var _ /* rc at bp+68 */ int32
	_, _ = iCol, zErr
	if nVal != int32(3) {
		zErr = __ccgo_ts + 38393
		Xsqlite3_result_error(tls, pCtx, zErr, -int32(1))
		return
	}
	iCol = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(apVal)))
	libc.Xmemset(tls, bp, 0, uint32(68))
	(**(**THighlightContext)(__ccgo_up(bp))).FzOpen = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(apVal + 1*4)))
	(**(**THighlightContext)(__ccgo_up(bp))).FzClose = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(apVal + 2*4)))
	(**(**THighlightContext)(__ccgo_up(bp))).FiRangeEnd = -int32(1)
	**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnText})))(tls, pFts, iCol, bp+16, bp+20)
	if **(**int32)(__ccgo_up(bp + 68)) == int32(SQLITE_RANGE) {
		Xsqlite3_result_text(tls, pCtx, __ccgo_ts+1711, -int32(1), libc.UintptrFromInt32(0))
		**(**int32)(__ccgo_up(bp + 68)) = SQLITE_OK
	} else {
		if (**(**THighlightContext)(__ccgo_up(bp))).FzIn != 0 {
			**(**uintptr)(__ccgo_up(bp + 72)) = uintptr(0) /* Locale of column iCol */
			**(**int32)(__ccgo_up(bp + 76)) = 0            /* Size of pLoc in bytes */
			if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp + 68)) = _fts5CInstIterInit(tls, pApi, pFts, iCol, bp+24)
			}
			if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnLocale})))(tls, pFts, iCol, bp+72, bp+76)
			}
			if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxTokenize_v2})))(tls, pFts, (**(**THighlightContext)(__ccgo_up(bp))).FzIn, (**(**THighlightContext)(__ccgo_up(bp))).FnIn, **(**uintptr)(__ccgo_up(bp + 72)), **(**int32)(__ccgo_up(bp + 76)), bp, __ccgo_fp(_fts5HighlightCb))
			}
			if (**(**THighlightContext)(__ccgo_up(bp))).FbOpen != 0 {
				_fts5HighlightAppend(tls, bp+68, bp, (**(**THighlightContext)(__ccgo_up(bp))).FzClose, -int32(1))
			}
			_fts5HighlightAppend(tls, bp+68, bp, (**(**THighlightContext)(__ccgo_up(bp))).FzIn+uintptr((**(**THighlightContext)(__ccgo_up(bp))).FiOff), (**(**THighlightContext)(__ccgo_up(bp))).FnIn-(**(**THighlightContext)(__ccgo_up(bp))).FiOff)
			if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
				Xsqlite3_result_text(tls, pCtx, (**(**THighlightContext)(__ccgo_up(bp))).FzOut, -int32(1), uintptr(-libc.Int32FromInt32(1)))
			}
			Xsqlite3_free(tls, (**(**THighlightContext)(__ccgo_up(bp))).FzOut)
		}
	}
	if **(**int32)(__ccgo_up(bp + 68)) != SQLITE_OK {
		Xsqlite3_result_error_code(tls, pCtx, **(**int32)(__ccgo_up(bp + 68)))
	}
}

/*
** End of highlight() implementation.
**************************************************************************/

// C documentation
//
//	/*
//	** SQL used by fts5SegIterNextInit() to find the page to open.
//	*/
func _fts5IdxNextStmt(tls *libc.TLS, p uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pConfig uintptr
	_ = pConfig
	if (*TFts5Index)(unsafe.Pointer(p)).FpIdxNextSelect == uintptr(0) {
		pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
		_fts5IndexPrepareStmt(tls, p, p+76, Xsqlite3_mprintf(tls, __ccgo_ts+40483, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName)))
	}
	return (*TFts5Index)(unsafe.Pointer(p)).FpIdxNextSelect
}

func _fts5IdxSelectStmt(tls *libc.TLS, p uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pConfig uintptr
	_ = pConfig
	if (*TFts5Index)(unsafe.Pointer(p)).FpIdxSelect == uintptr(0) {
		pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
		_fts5IndexPrepareStmt(tls, p, p+72, Xsqlite3_mprintf(tls, __ccgo_ts+40399, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName)))
	}
	return (*TFts5Index)(unsafe.Pointer(p)).FpIdxSelect
}

func _fts5IndexDataVersion(tls *libc.TLS, p uintptr) (r Ti64) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iVersion Ti64
	_ = iVersion
	iVersion = 0
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		if (*TFts5Index)(unsafe.Pointer(p)).FpDataVersion == uintptr(0) {
			(*TFts5Index)(unsafe.Pointer(p)).Frc = _fts5IndexPrepareStmt(tls, p, p+88, Xsqlite3_mprintf(tls, __ccgo_ts+40376, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FzDb)))
			if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
				return 0
			}
		}
		if int32(SQLITE_ROW) == Xsqlite3_step(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDataVersion) {
			iVersion = Xsqlite3_column_int64(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDataVersion, 0)
		}
		(*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDataVersion)
	}
	return iVersion
}

// C documentation
//
//	/*
//	** Parameter pPos points to a buffer containing a position list, size nPos.
//	** This function filters it according to pColset (which must be non-NULL)
//	** and sets pIter->base.pData/nData to point to the new position list.
//	** If memory is required for the new position list, use buffer pIter->poslist.
//	** Or, if the new position list is a contiguous subset of the input, set
//	** pIter->base.pData/nData to point directly to it.
//	**
//	** This function is a no-op if *pRc is other than SQLITE_OK when it is
//	** called. If an OOM error is encountered, *pRc is set to SQLITE_NOMEM
//	** before returning.
//	*/
func _fts5IndexExtractColset(tls *libc.TLS, pRc uintptr, pColset uintptr, pPos uintptr, nPos int32, pIter uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aCopy, p, pEnd, v1 uintptr
	var i int32
	var v2 bool
	var _ /* iCurrent at bp+0 */ int32
	_, _, _, _, _, _ = aCopy, i, p, pEnd, v1, v2
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK {
		p = pPos
		aCopy = p
		pEnd = p + uintptr(nPos) /* One byte past end of position list */
		i = 0
		**(**int32)(__ccgo_up(bp)) = 0
		if (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol > int32(1) && _sqlite3Fts5BufferSize(tls, pRc, pIter+32, uint32(nPos)) != 0 {
			return
		}
		for int32(1) != 0 {
			for *(*int32)(unsafe.Pointer(pColset + 4 + uintptr(i)*4)) < **(**int32)(__ccgo_up(bp)) {
				i = i + 1
				if i == (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol {
					(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp
					(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fn
					return
				}
			}
			/* Advance pointer p until it points to pEnd or an 0x01 byte that is
			 ** not part of a varint */
			for p < pEnd && int32(**(**Tu8)(__ccgo_up(p))) != int32(0x01) {
				for {
					if v2 = p < pEnd; v2 {
						v1 = p
						p = p + 1
					}
					if !(v2 && int32(**(**Tu8)(__ccgo_up(v1)))&int32(0x80) != 0) {
						break
					}
				}
			}
			if *(*int32)(unsafe.Pointer(pColset + 4 + uintptr(i)*4)) == **(**int32)(__ccgo_up(bp)) {
				if (*TFts5Colset)(unsafe.Pointer(pColset)).FnCol == int32(1) {
					(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = aCopy
					(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = int32(p) - int32(aCopy)
					return
				}
				libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pIter+32)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pIter+32)).Fn), aCopy, uint32(int32(p)-int32(aCopy)))
				**(**int32)(__ccgo_up(pIter + 32 + 4)) += int32(p) - int32(aCopy)
			}
			if p >= pEnd {
				(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp
				(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fn
				return
			}
			v1 = p
			p = p + 1
			aCopy = v1
			v1 = p
			p = p + 1
			**(**int32)(__ccgo_up(bp)) = int32(**(**Tu8)(__ccgo_up(v1)))
			if **(**int32)(__ccgo_up(bp))&int32(0x80) != 0 {
				p = p - 1
				p = p + uintptr(_sqlite3Fts5GetVarint32(tls, p, bp))
			}
		}
	}
}

// C documentation
//
//	/*
//	** If this is not a contentless_delete=1 table, or if the 'deletemerge'
//	** configuration option is set to 0, then this function always returns -1.
//	** Otherwise, it searches the structure object passed as the second argument
//	** for a level suitable for merging due to having a large number of
//	** tombstones in the tombstone hash. If one is found, its index is returned.
//	** Otherwise, if there is no suitable level, -1.
//	*/
func _fts5IndexFindDeleteMerge(tls *libc.TLS, p uintptr, pStruct uintptr) (r int32) {
	var iRet, iSeg, ii, nBest, nPercent int32
	var nEntry, nTomb Ti64
	var pConfig, pLvl uintptr
	_, _, _, _, _, _, _, _, _ = iRet, iSeg, ii, nBest, nEntry, nPercent, nTomb, pConfig, pLvl
	pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
	iRet = -int32(1)
	if (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete != 0 && (*TFts5Config)(unsafe.Pointer(pConfig)).FnDeleteMerge > 0 {
		nBest = 0
		ii = 0
		for {
			if !(ii < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) {
				break
			}
			pLvl = pStruct + 32 + uintptr(ii)*12
			nEntry = 0
			nTomb = 0
			iSeg = 0
			for {
				if !(iSeg < (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg) {
					break
				}
				nEntry = int64(uint64(nEntry) + (**(**TFts5StructureSegment)(__ccgo_up((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSeg)*56))).FnEntry)
				nTomb = int64(uint64(nTomb) + (**(**TFts5StructureSegment)(__ccgo_up((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSeg)*56))).FnEntryTombstone)
				goto _2
			_2:
				;
				iSeg = iSeg + 1
			}
			if nEntry > 0 {
				nPercent = int32(nTomb * int64(100) / nEntry)
				if nPercent >= (*TFts5Config)(unsafe.Pointer(pConfig)).FnDeleteMerge && nPercent > nBest {
					iRet = ii
					nBest = nPercent
				}
			}
			/* If pLvl is already the input level to an ongoing merge, look no
			 ** further for a merge candidate. The caller should be allowed to
			 ** continue merging from pLvl first.  */
			if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge != 0 {
				break
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
	}
	return iRet
}

func _fts5IndexIntegrityCheckSegment(tls *libc.TLS, p uintptr, pSeg uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var bIdxDlidx, bSecureDelete, iDlidxPrevLeaf, iIdxLeaf, iIdxPrevLeaf, iOff, iPg, iPrevLeaf, iRowidOff, iRowidOff1, iSegid, nIdxTerm, rc2, res, v1, v2, v3 int32
	var iDlRowid, iKey, iRow Ti64
	var pConfig, pDlidx, pLeaf, zIdxTerm uintptr
	var _ /* iRowid at bp+8 */ Ti64
	var _ /* nTerm at bp+4 */ int32
	var _ /* pStmt at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bIdxDlidx, bSecureDelete, iDlRowid, iDlidxPrevLeaf, iIdxLeaf, iIdxPrevLeaf, iKey, iOff, iPg, iPrevLeaf, iRow, iRowidOff, iRowidOff1, iSegid, nIdxTerm, pConfig, pDlidx, pLeaf, rc2, res, zIdxTerm, v1, v2, v3
	pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
	bSecureDelete = libc.BoolInt32((*TFts5Config)(unsafe.Pointer(pConfig)).FiVersion == int32(FTS5_CURRENT_VERSION_SECUREDELETE))
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	iIdxPrevLeaf = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst - int32(1)
	iDlidxPrevLeaf = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast
	if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst == 0 {
		return
	}
	_fts5IndexPrepareStmt(tls, p, bp, Xsqlite3_mprintf(tls, __ccgo_ts+40823, libc.VaList(bp+24, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName, (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)))
	/* Iterate through the b-tree hierarchy.  */
	for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) { /* Data for this leaf */
		zIdxTerm = Xsqlite3_column_blob(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		nIdxTerm = Xsqlite3_column_bytes(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		iIdxLeaf = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(2))
		bIdxDlidx = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(3))
		/* If the leaf in question has already been trimmed from the segment,
		 ** ignore this b-tree entry. Otherwise, load it into memory. */
		if iIdxLeaf < (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst {
			continue
		}
		iRow = int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(iIdxLeaf)
		pLeaf = _fts5LeafRead(tls, p, iRow)
		if pLeaf == uintptr(0) {
			break
		}
		/* Check that the leaf contains at least one term, and that it is equal
		 ** to or larger than the split-key in zIdxTerm.  Also check that if there
		 ** is also a rowid pointer within the leaf page header, it points to a
		 ** location before the term.  */
		if (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn <= (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
			if nIdxTerm == 0 && (*TFts5Config)(unsafe.Pointer(pConfig)).FiVersion == int32(FTS5_CURRENT_VERSION_SECUREDELETE) && (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn == (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf && (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn == int32(4) {
				/* special case - the very first page in a segment keeps its %_idx
				 ** entry even if all the terms are removed from it by secure-delete
				 ** operations. */
			} else {
				_fts5IndexCorruptRowid(tls, p, iRow)
			}
		} else { /* Comparison of term and split-key */
			iOff = _fts5LeafFirstTermOff(tls, pLeaf)
			iRowidOff = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp))
			if iRowidOff >= iOff || iOff >= (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
				_fts5IndexCorruptRowid(tls, p, iRow)
			} else {
				iOff = iOff + _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(iOff), bp+4)
				if iOff+**(**int32)(__ccgo_up(bp + 4)) > (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
					_fts5IndexCorruptRowid(tls, p, iRow)
				} else {
					if **(**int32)(__ccgo_up(bp + 4)) < nIdxTerm {
						v2 = **(**int32)(__ccgo_up(bp + 4))
					} else {
						v2 = nIdxTerm
					}
					if v2 <= 0 {
						v1 = 0
					} else {
						if **(**int32)(__ccgo_up(bp + 4)) < nIdxTerm {
							v3 = **(**int32)(__ccgo_up(bp + 4))
						} else {
							v3 = nIdxTerm
						}
						v1 = libc.Xmemcmp(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(iOff), zIdxTerm, uint32(v3))
					}
					res = v1
					if res == 0 {
						res = **(**int32)(__ccgo_up(bp + 4)) - nIdxTerm
					}
					if res < 0 {
						_fts5IndexCorruptRowid(tls, p, iRow)
					}
				}
			}
			_fts5IntegrityCheckPgidx(tls, p, iRow, pLeaf)
		}
		_fts5DataRelease(tls, pLeaf)
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
			break
		}
		/* Now check that the iter.nEmpty leaves following the current leaf
		 ** (a) exist and (b) contain no terms. */
		_fts5IndexIntegrityCheckEmpty(tls, p, pSeg, iIdxPrevLeaf+int32(1), iDlidxPrevLeaf+int32(1), iIdxLeaf-int32(1))
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
			break
		}
		/* If there is a doclist-index, check that it looks right. */
		if bIdxDlidx != 0 {
			pDlidx = uintptr(0) /* For iterating through doclist index */
			iPrevLeaf = iIdxLeaf
			iSegid = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid
			iPg = 0
			pDlidx = _fts5DlidxIterInit(tls, p, 0, iSegid, iIdxLeaf)
			for {
				if !(_fts5DlidxIterEof(tls, p, pDlidx) == 0) {
					break
				}
				/* Check any rowid-less pages that occur before the current leaf. */
				iPg = iPrevLeaf + int32(1)
				for {
					if !(iPg < _fts5DlidxIterPgno(tls, pDlidx)) {
						break
					}
					iKey = int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(iPg)
					pLeaf = _fts5LeafRead(tls, p, iKey)
					if pLeaf != 0 {
						if int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp)) != 0 {
							_fts5IndexCorruptRowid(tls, p, iKey)
						}
						_fts5DataRelease(tls, pLeaf)
					}
					goto _5
				_5:
					;
					iPg = iPg + 1
				}
				iPrevLeaf = _fts5DlidxIterPgno(tls, pDlidx)
				/* Check that the leaf page indicated by the iterator really does
				 ** contain the rowid suggested by the same. */
				iKey = int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(iPrevLeaf)
				pLeaf = _fts5LeafRead(tls, p, iKey)
				if pLeaf != 0 {
					iRowidOff1 = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp))
					if iRowidOff1 >= (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
						_fts5IndexCorruptRowid(tls, p, iKey)
					} else {
						if bSecureDelete == 0 || iRowidOff1 > 0 {
							iDlRowid = _fts5DlidxIterRowid(tls, pDlidx)
							_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(iRowidOff1), bp+8)
							if **(**Ti64)(__ccgo_up(bp + 8)) < iDlRowid || bSecureDelete == 0 && **(**Ti64)(__ccgo_up(bp + 8)) != iDlRowid {
								_fts5IndexCorruptRowid(tls, p, iKey)
							}
						}
					}
					_fts5DataRelease(tls, pLeaf)
				}
				goto _4
			_4:
				;
				_fts5DlidxIterNext(tls, p, pDlidx)
			}
			iDlidxPrevLeaf = iPg
			_fts5DlidxIterFree(tls, pDlidx)
		} else {
			iDlidxPrevLeaf = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast
			/* TODO: Check there is no doclist index */
		}
		iIdxPrevLeaf = iIdxLeaf
	}
	rc2 = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*TFts5Index)(unsafe.Pointer(p)).Frc = rc2
	}
	/* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
}

// C documentation
//
//	/*
//	**
//	*/
func _fts5IndexMergeLevel(tls *libc.TLS, p uintptr, ppStruct uintptr, iLvl int32, pnRem uintptr) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var bOldest, bTermWritten, eDetail, flags, i, iSegid, nInput, nMove, nPos, nRem, v1 int32
	var pLvl, pLvlOut, pOld, pSeg, pSegIter, pStruct, pTerm uintptr
	var v4 bool
	var _ /* nTerm at bp+116 */ int32
	var _ /* pIter at bp+0 */ uintptr
	var _ /* term at bp+104 */ TFts5Buffer
	var _ /* writer at bp+8 */ TFts5SegWriter
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bOldest, bTermWritten, eDetail, flags, i, iSegid, nInput, nMove, nPos, nRem, pLvl, pLvlOut, pOld, pSeg, pSegIter, pStruct, pTerm, v1, v4
	pStruct = **(**uintptr)(__ccgo_up(ppStruct))
	pLvl = pStruct + 32 + uintptr(iLvl)*12
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	if pnRem != 0 {
		v1 = **(**int32)(__ccgo_up(pnRem))
	} else {
		v1 = 0
	} /* Iterator to read input data */
	nRem = v1 /* True if the output segment is the oldest */
	eDetail = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail
	flags = int32(FTS5INDEX_QUERY_NOOUTPUT)
	bTermWritten = 0 /* True if current term already output */
	libc.Xmemset(tls, bp+8, 0, uint32(96))
	libc.Xmemset(tls, bp+104, 0, uint32(12))
	if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge != 0 {
		pLvlOut = pStruct + 32 + uintptr(iLvl+int32(1))*12
		nInput = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge
		pSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FaSeg + uintptr((*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FnSeg-int32(1))*56
		_fts5WriteInit(tls, p, bp+8, (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)
		(**(**TFts5SegWriter)(__ccgo_up(bp + 8))).Fwriter.Fpgno = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast + int32(1)
		(**(**TFts5SegWriter)(__ccgo_up(bp + 8))).FiBtPage = 0
	} else {
		iSegid = _fts5AllocateSegid(tls, p, pStruct)
		/* Extend the Fts5Structure object as required to ensure the output
		 ** segment exists. */
		if iLvl == (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel-int32(1) {
			_fts5StructureAddLevel(tls, p+44, ppStruct)
			pStruct = **(**uintptr)(__ccgo_up(ppStruct))
		}
		_fts5StructureExtendLevel(tls, p+44, pStruct, iLvl+int32(1), int32(1), 0)
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
			return
		}
		pLvl = pStruct + 32 + uintptr(iLvl)*12
		pLvlOut = pStruct + 32 + uintptr(iLvl+int32(1))*12
		_fts5WriteInit(tls, p, bp+8, iSegid)
		/* Add the new segment to the output level */
		pSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FaSeg + uintptr((*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FnSeg)*56
		(*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FnSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FnSeg + 1
		(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst = int32(1)
		(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid = iSegid
		(*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment + 1
		/* Read input from all segments in the input level */
		nInput = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg
		/* Set the range of origins that will go into the output segment. */
		if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnOriginCntr > uint64(0) {
			(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin1 = (**(**TFts5StructureSegment)(__ccgo_up((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg))).FiOrigin1
			(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2 = (**(**TFts5StructureSegment)(__ccgo_up((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg-int32(1))*56))).FiOrigin2
		}
	}
	bOldest = libc.BoolInt32((*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FnSeg == int32(1) && (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel == iLvl+int32(2))
	_fts5MultiIterNew(tls, p, pStruct, flags, uintptr(0), uintptr(0), 0, iLvl, nInput, bp)
	for {
		if !(_fts5MultiIterEof(tls, p, **(**uintptr)(__ccgo_up(bp))) == 0) {
			break
		}
		pSegIter = **(**uintptr)(__ccgo_up(bp)) + 80 + uintptr((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaFirst + 1*4))).FiFirst)*104
		pTerm = _fts5MultiIterTerm(tls, **(**uintptr)(__ccgo_up(bp)), bp+116)
		if v4 = **(**int32)(__ccgo_up(bp + 116)) != (**(**TFts5Buffer)(__ccgo_up(bp + 104))).Fn; !v4 {
			if **(**int32)(__ccgo_up(bp + 116)) <= 0 {
				v1 = 0
			} else {
				v1 = libc.Xmemcmp(tls, pTerm, (**(**TFts5Buffer)(__ccgo_up(bp + 104))).Fp, uint32(**(**int32)(__ccgo_up(bp + 116))))
			}
		}
		if v4 || v1 != 0 {
			if pnRem != 0 && (**(**TFts5SegWriter)(__ccgo_up(bp + 8))).FnLeafWritten > nRem {
				break
			}
			_sqlite3Fts5BufferSet(tls, p+44, bp+104, **(**int32)(__ccgo_up(bp + 116)), pTerm)
			bTermWritten = 0
		}
		/* Check for key annihilation. */
		if (*TFts5SegIter)(unsafe.Pointer(pSegIter)).FnPos == 0 && (bOldest != 0 || int32((*TFts5SegIter)(unsafe.Pointer(pSegIter)).FbDel) == 0) {
			goto _2
		}
		if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bTermWritten == 0 {
			/* This is a new term. Append a term to the output segment. */
			_fts5WriteAppendTerm(tls, p, bp+8, **(**int32)(__ccgo_up(bp + 116)), pTerm)
			bTermWritten = int32(1)
		}
		/* Append the rowid to the output */
		/* WRITEPOSLISTSIZE */
		_fts5WriteAppendRowid(tls, p, bp+8, _fts5MultiIterRowid(tls, **(**uintptr)(__ccgo_up(bp))))
		if eDetail == int32(FTS5_DETAIL_NONE) {
			if (*TFts5SegIter)(unsafe.Pointer(pSegIter)).FbDel != 0 {
				_sqlite3Fts5BufferAppendVarint(tls, p+44, bp+8+4+8, libc.Int64FromInt32(0))
				if (*TFts5SegIter)(unsafe.Pointer(pSegIter)).FnPos > 0 {
					_sqlite3Fts5BufferAppendVarint(tls, p+44, bp+8+4+8, libc.Int64FromInt32(0))
				}
			}
		} else {
			/* Append the position-list data to the output */
			nPos = (*TFts5SegIter)(unsafe.Pointer(pSegIter)).FnPos*int32(2) + int32((*TFts5SegIter)(unsafe.Pointer(pSegIter)).FbDel)
			_sqlite3Fts5BufferAppendVarint(tls, p+44, bp+8+4+8, int64(nPos))
			_fts5ChunkIterate(tls, p, pSegIter, bp+8, __ccgo_fp(_fts5MergeChunkCallback))
		}
		goto _2
	_2:
		;
		_fts5MultiIterNext(tls, p, **(**uintptr)(__ccgo_up(bp)), 0, 0)
	}
	/* Flush the last leaf page to disk. Set the output segment b-tree height
	 ** and last leaf page number at the same time.  */
	_fts5WriteFinish(tls, p, bp+8, pSeg+8)
	if _fts5MultiIterEof(tls, p, **(**uintptr)(__ccgo_up(bp))) != 0 {
		/* Remove the redundant segments from the %_data table */
		i = 0
		for {
			if !(i < nInput) {
				break
			}
			pOld = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(i)*56
			**(**Tu64)(__ccgo_up(pSeg + 48)) += (*TFts5StructureSegment)(unsafe.Pointer(pOld)).FnEntry - (*TFts5StructureSegment)(unsafe.Pointer(pOld)).FnEntryTombstone
			_fts5DataRemoveSegment(tls, p, pOld)
			goto _5
		_5:
			;
			i = i + 1
		}
		/* Remove the redundant segments from the input level */
		if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg != nInput {
			nMove = int32(uint32((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg-nInput) * uint32(56))
			libc.Xmemmove(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(nInput)*56, uint32(nMove))
		}
		**(**int32)(__ccgo_up(pStruct + 24)) -= nInput
		**(**int32)(__ccgo_up(pLvl + 4)) -= nInput
		(*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge = 0
		if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast == 0 {
			(*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FnSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvlOut)).FnSeg - 1
			(*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment - 1
		}
	} else {
		_fts5TrimSegments(tls, p, **(**uintptr)(__ccgo_up(bp)))
		(*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge = nInput
	}
	_fts5MultiIterFree(tls, **(**uintptr)(__ccgo_up(bp)))
	_sqlite3Fts5BufferFree(tls, bp+104)
	if pnRem != 0 {
		**(**int32)(__ccgo_up(pnRem)) -= (**(**TFts5SegWriter)(__ccgo_up(bp + 8))).FnLeafWritten
	}
}

func _fts5IndexOptimizeStruct(tls *libc.TLS, p uintptr, pStruct uintptr) (r uintptr) {
	var i, iLvl, iSeg, iSegOut, nMerge, nSeg, nThis, v2 int32
	var nByte Tsqlite3_int64
	var pLvl, pNew uintptr
	_, _, _, _, _, _, _, _, _, _, _ = i, iLvl, iSeg, iSegOut, nByte, nMerge, nSeg, nThis, pLvl, pNew, v2
	pNew = uintptr(0)
	nByte = int64(uint32(libc.UintptrFromInt32(0)+32) + uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(12))
	nSeg = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment
	/* Figure out if this structure requires optimization. A structure does
	 ** not require optimization if either:
	 **
	 **  1. it consists of fewer than two segments, or
	 **  2. all segments are on the same level, or
	 **  3. all segments except one are currently inputs to a merge operation.
	 **
	 ** In the first case, if there are no tombstone hash pages, return NULL. In
	 ** the second, increment the ref-count on *pStruct and return a copy of the
	 ** pointer to it.
	 */
	if nSeg == 0 {
		return uintptr(0)
	}
	i = 0
	for {
		if !(i < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) {
			break
		}
		nThis = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(i)*12))).FnSeg
		nMerge = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(i)*12))).FnMerge
		if nThis > 0 && (nThis == nSeg || nThis == nSeg-int32(1) && nMerge == nThis) {
			if nSeg == int32(1) && nThis == int32(1) && (**(**TFts5StructureSegment)(__ccgo_up((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(i)*12))).FaSeg))).FnPgTombstone == 0 {
				return uintptr(0)
			}
			_fts5StructureRef(tls, pStruct)
			return pStruct
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	nByte = nByte + (int64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel)+int64(1))*int64(12)
	pNew = _sqlite3Fts5MallocZero(tls, p+44, nByte)
	if pNew != 0 {
		nByte = int64(uint32(nSeg) * uint32(56))
		if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel+int32(1) < int32(FTS5_MAX_LEVEL) {
			v2 = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel + int32(1)
		} else {
			v2 = int32(FTS5_MAX_LEVEL)
		}
		(*TFts5Structure)(unsafe.Pointer(pNew)).FnLevel = v2
		(*TFts5Structure)(unsafe.Pointer(pNew)).FnRef = int32(1)
		(*TFts5Structure)(unsafe.Pointer(pNew)).FnWriteCounter = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnWriteCounter
		(*TFts5Structure)(unsafe.Pointer(pNew)).FnOriginCntr = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnOriginCntr
		pLvl = pNew + 32 + uintptr((*TFts5Structure)(unsafe.Pointer(pNew)).FnLevel-int32(1))*12
		(*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg = _sqlite3Fts5MallocZero(tls, p+44, nByte)
		if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg != 0 {
			iSegOut = 0
			/* Iterate through all segments, from oldest to newest. Add them to
			 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
			 ** segment in the data structure.  */
			iLvl = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel - int32(1)
			for {
				if !(iLvl >= 0) {
					break
				}
				iSeg = 0
				for {
					if !(iSeg < (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg) {
						break
					}
					**(**TFts5StructureSegment)(__ccgo_up((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSegOut)*56)) = **(**TFts5StructureSegment)(__ccgo_up((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FaSeg + uintptr(iSeg)*56))
					iSegOut = iSegOut + 1
					goto _4
				_4:
					;
					iSeg = iSeg + 1
				}
				goto _3
			_3:
				;
				iLvl = iLvl - 1
			}
			v2 = nSeg
			(*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg = v2
			(*TFts5Structure)(unsafe.Pointer(pNew)).FnSegment = v2
		} else {
			Xsqlite3_free(tls, pNew)
			pNew = uintptr(0)
		}
	}
	return pNew
}

// C documentation
//
//	/*
//	** Add a tombstone for rowid iRowid to segment pSeg.
//	*/
func _fts5IndexTombstoneAdd(tls *libc.TLS, p uintptr, pSeg uintptr, iRowid Tu64) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iPg, ii, szKey, v1, v2 int32
	var iTombstoneRowid Ti64
	var pPg uintptr
	var _ /* apHash at bp+4 */ uintptr
	var _ /* nHash at bp+0 */ int32
	_, _, _, _, _, _, _ = iPg, iTombstoneRowid, ii, pPg, szKey, v1, v2
	pPg = uintptr(0)
	iPg = -int32(1)
	szKey = 0
	**(**int32)(__ccgo_up(bp)) = 0
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	(*TFts5Index)(unsafe.Pointer(p)).FnContentlessDelete = (*TFts5Index)(unsafe.Pointer(p)).FnContentlessDelete + 1
	if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone > 0 {
		iPg = int32(iRowid % uint64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone))
		pPg = _fts5DataRead(tls, p, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid+libc.Int32FromInt32(1)<<libc.Int32FromInt32(16))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64(iPg))
		if pPg == uintptr(0) {
			return
		}
		if 0 == _fts5IndexTombstoneAddToPage(tls, pPg, 0, (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone, iRowid) {
			_fts5DataWrite(tls, p, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid+libc.Int32FromInt32(1)<<libc.Int32FromInt32(16))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64(iPg), (*TFts5Data)(unsafe.Pointer(pPg)).Fp, (*TFts5Data)(unsafe.Pointer(pPg)).Fnn)
			_fts5DataRelease(tls, pPg)
			return
		}
	}
	/* Have to rebuild the hash table. First figure out the key-size (4 or 8). */
	if pPg != 0 {
		if int32(**(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer(pPg)).Fp))) == int32(4) {
			v2 = int32(4)
		} else {
			v2 = int32(8)
		}
		v1 = v2
	} else {
		v1 = int32(4)
	}
	szKey = v1
	if iRowid > uint64(0xFFFFFFFF) {
		szKey = int32(8)
	}
	/* Rebuild the hash table */
	_fts5IndexTombstoneRebuild(tls, p, pSeg, pPg, iPg, szKey, bp, bp+4)
	/* If all has succeeded, write the new rowid into one of the new hash
	 ** table pages, then write them all out to disk. */
	if **(**int32)(__ccgo_up(bp)) != 0 {
		ii = 0
		_fts5IndexTombstoneAddToPage(tls, **(**uintptr)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4)) + uintptr(iRowid%uint64(**(**int32)(__ccgo_up(bp))))*4)), int32(1), **(**int32)(__ccgo_up(bp)), iRowid)
		ii = 0
		for {
			if !(ii < **(**int32)(__ccgo_up(bp))) {
				break
			}
			iTombstoneRowid = int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid+libc.Int32FromInt32(1)<<libc.Int32FromInt32(16))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(ii)
			_fts5DataWrite(tls, p, iTombstoneRowid, (*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4)) + uintptr(ii)*4)))).Fp, (*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4)) + uintptr(ii)*4)))).Fnn)
			goto _3
		_3:
			;
			ii = ii + 1
		}
		(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone = **(**int32)(__ccgo_up(bp))
		_fts5StructureWrite(tls, p, (*TFts5Index)(unsafe.Pointer(p)).FpStruct)
	}
	_fts5DataRelease(tls, pPg)
	_fts5IndexFreeArray(tls, **(**uintptr)(__ccgo_up(bp + 4)), **(**int32)(__ccgo_up(bp)))
}

// C documentation
//
//	/*
//	** This is called to rebuild the hash table belonging to segment pSeg.
//	** If parameter pData1 is not NULL, then one page of the existing hash table
//	** has already been loaded - pData1, which is page iPg1. The key-size for
//	** the new hash table is szKey (4 or 8).
//	**
//	** If successful, the new hash table is not written to disk. Instead,
//	** output parameter (*pnOut) is set to the number of pages in the new
//	** hash table, and (*papOut) to point to an array of buffers containing
//	** the new page data.
//	**
//	** If an error occurs, an error code is left in the Fts5Index object and
//	** both output parameters set to 0 before returning.
//	*/
func _fts5IndexTombstoneRebuild(tls *libc.TLS, p uintptr, pSeg uintptr, pData1 uintptr, iPg1 int32, szKey int32, pnOut uintptr, papOut uintptr) {
	var MINSLOT, nSlotPerPage, res, v1 int32
	var apOut, pNew uintptr
	var ii, nOut, nSlot, szPage Ti64
	var nElem Tu32
	var v2 int64
	_, _, _, _, _, _, _, _, _, _, _, _ = MINSLOT, apOut, ii, nElem, nOut, nSlot, nSlotPerPage, pNew, res, szPage, v1, v2
	MINSLOT = int32(32)
	if MINSLOT > ((*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz-int32(8))/szKey {
		v1 = MINSLOT
	} else {
		v1 = ((*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz - int32(8)) / szKey
	}
	nSlotPerPage = v1
	nSlot = 0 /* Number of slots in each output page */
	nOut = 0
	/* Figure out how many output pages (nOut) and how many slots per
	 ** page (nSlot).  There are three possibilities:
	 **
	 **   1. The hash table does not yet exist. In this case the new hash
	 **      table will consist of a single page with MINSLOT slots.
	 **
	 **   2. The hash table exists but is currently a single page. In this
	 **      case an attempt is made to grow the page to accommodate the new
	 **      entry. The page is allowed to grow up to nSlotPerPage (see above)
	 **      slots.
	 **
	 **   3. The hash table already consists of more than one page, or of
	 **      a single page already so large that it cannot be grown. In this
	 **      case the new hash consists of (nPg*2+1) pages of nSlotPerPage
	 **      slots each, where nPg is the current number of pages in the
	 **      hash table.
	 */
	if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone == 0 {
		/* Case 1. */
		nOut = int64(1)
		nSlot = int64(MINSLOT)
	} else {
		if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone == int32(1) {
			/* Case 2. */
			nElem = _fts5GetU32(tls, (*TFts5Data)(unsafe.Pointer(pData1)).Fp+4)
			if nElem > uint32(nSlotPerPage)/uint32(4) {
				nOut = 0
			} else {
				nOut = int64(1)
				if int64(nElem)*int64(4) > int64(MINSLOT) {
					v2 = int64(nElem) * int64(4)
				} else {
					v2 = int64(MINSLOT)
				}
				nSlot = v2
			}
		}
	}
	if nOut == 0 {
		/* Case 3. */
		nOut = int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone)*libc.Int64FromInt32(2) + libc.Int64FromInt32(1)
		nSlot = int64(nSlotPerPage)
	}
	/* Allocate the required array and output pages */
	for int32(1) != 0 {
		res = 0
		ii = 0
		szPage = 0
		apOut = uintptr(0)
		/* Allocate space for the new hash table */
		apOut = _sqlite3Fts5MallocZero(tls, p+44, int64(4)*nOut)
		szPage = int64(8) + nSlot*int64(szKey)
		ii = 0
		for {
			if !(ii < nOut) {
				break
			}
			pNew = _sqlite3Fts5MallocZero(tls, p+44, int64(12)+szPage)
			if pNew != 0 {
				(*TFts5Data)(unsafe.Pointer(pNew)).Fnn = int32(szPage)
				(*TFts5Data)(unsafe.Pointer(pNew)).Fp = pNew + 1*12
				**(**uintptr)(__ccgo_up(apOut + uintptr(ii)*4)) = pNew
			}
			goto _3
		_3:
			;
			ii = ii + 1
		}
		/* Rebuild the hash table. */
		if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			res = _fts5IndexTombstoneRehash(tls, p, pSeg, pData1, iPg1, szKey, int32(nOut), apOut)
		}
		if res == 0 {
			if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
				_fts5IndexFreeArray(tls, apOut, int32(nOut))
				apOut = uintptr(0)
				nOut = 0
			}
			**(**int32)(__ccgo_up(pnOut)) = int32(nOut)
			**(**uintptr)(__ccgo_up(papOut)) = apOut
			break
		}
		/* If control flows to here, it was not possible to rebuild the hash
		 ** table. Free all buffers and then try again with more pages. */
		_fts5IndexFreeArray(tls, apOut, int32(nOut))
		nSlot = int64(nSlotPerPage)
		nOut = nOut*int64(2) + int64(1)
	}
}

// C documentation
//
//	/*
//	** This function attempts to build a new hash containing all the keys
//	** currently in the tombstone hash table for segment pSeg. The new
//	** hash will be stored in the nOut buffers passed in array apOut[].
//	** All pages of the new hash use key-size szKey (4 or 8).
//	**
//	** Return 0 if the hash is successfully rebuilt into the nOut pages.
//	** Or non-zero if it is not (because one page became overfull). In this
//	** case the caller should retry with a larger nOut parameter.
//	**
//	** Parameter pData1 is page iPg1 of the hash table being rebuilt.
//	*/
func _fts5IndexTombstoneRehash(tls *libc.TLS, p uintptr, pSeg uintptr, pData1 uintptr, iPg1 int32, szKey int32, nOut int32, apOut uintptr) (r int32) {
	var aSlot, aSlot1, pData, pFree, pPg, v3 uintptr
	var iIn, ii, nSlotIn, res, szKeyIn, v4 int32
	var iVal Tu64
	_, _, _, _, _, _, _, _, _, _, _, _, _ = aSlot, aSlot1, iIn, iVal, ii, nSlotIn, pData, pFree, pPg, res, szKeyIn, v3, v4
	res = 0
	/* Initialize the headers of all the output pages */
	ii = 0
	for {
		if !(ii < nOut) {
			break
		}
		**(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(apOut + uintptr(ii)*4)))).Fp)) = uint8(szKey)
		_fts5PutU32(tls, (*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(apOut + uintptr(ii)*4)))).Fp+4, uint32(0))
		goto _1
	_1:
		;
		ii = ii + 1
	}
	/* Loop through the current pages of the hash table. */
	ii = 0
	for {
		if !(res == 0 && ii < (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone) {
			break
		}
		pData = uintptr(0) /* Page ii of the current hash table */
		pFree = uintptr(0) /* Free this at the end of the loop */
		if iPg1 == ii {
			pData = pData1
		} else {
			v3 = _fts5DataRead(tls, p, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid+libc.Int32FromInt32(1)<<libc.Int32FromInt32(16))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64(ii))
			pData = v3
			pFree = v3
		}
		if pData != 0 {
			if int32(**(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer(pData)).Fp))) == int32(4) {
				v4 = int32(4)
			} else {
				v4 = int32(8)
			}
			szKeyIn = v4
			nSlotIn = ((*TFts5Data)(unsafe.Pointer(pData)).Fnn - int32(8)) / szKeyIn
			iIn = 0
			for {
				if !(iIn < nSlotIn) {
					break
				}
				iVal = uint64(0)
				/* Read the value from slot iIn of the input page into iVal. */
				if szKeyIn == int32(4) {
					aSlot = (*TFts5Data)(unsafe.Pointer(pData)).Fp + 8
					if **(**Tu32)(__ccgo_up(aSlot + uintptr(iIn)*4)) != 0 {
						iVal = uint64(_fts5GetU32(tls, aSlot+uintptr(iIn)*4))
					}
				} else {
					aSlot1 = (*TFts5Data)(unsafe.Pointer(pData)).Fp + 8
					if **(**Tu64)(__ccgo_up(aSlot1 + uintptr(iIn)*8)) != 0 {
						iVal = _fts5GetU64(tls, aSlot1+uintptr(iIn)*8)
					}
				}
				/* If iVal is not 0 at this point, insert it into the new hash table */
				if iVal != 0 {
					pPg = **(**uintptr)(__ccgo_up(apOut + uintptr(iVal%uint64(nOut))*4))
					res = _fts5IndexTombstoneAddToPage(tls, pPg, 0, nOut, iVal)
					if res != 0 {
						break
					}
				}
				goto _5
			_5:
				;
				iIn = iIn + 1
			}
			/* If this is page 0 of the old hash, copy the rowid-0-flag from the
			 ** old hash to the new.  */
			if ii == 0 {
				**(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(apOut)))).Fp + 1)) = **(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer(pData)).Fp + 1))
			}
		}
		_fts5DataRelease(tls, pFree)
		goto _2
	_2:
		;
		ii = ii + 1
	}
	return res
}

func _fts5Init(tls *libc.TLS, db uintptr) (r int32) {
	var p, pGlobal uintptr
	var rc int32
	_, _, _ = p, pGlobal, rc
	pGlobal = uintptr(0)
	pGlobal = Xsqlite3_malloc64(tls, uint64(72))
	if pGlobal == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		p = pGlobal
		libc.Xmemset(tls, pGlobal, 0, uint32(72))
		(*TFts5Global)(unsafe.Pointer(pGlobal)).Fdb = db
		(*TFts5Global)(unsafe.Pointer(pGlobal)).Fapi.FiVersion = int32(3)
		(*TFts5Global)(unsafe.Pointer(pGlobal)).Fapi.FxCreateFunction = __ccgo_fp(_fts5CreateAux)
		(*TFts5Global)(unsafe.Pointer(pGlobal)).Fapi.FxCreateTokenizer = __ccgo_fp(_fts5CreateTokenizer)
		(*TFts5Global)(unsafe.Pointer(pGlobal)).Fapi.FxFindTokenizer = __ccgo_fp(_fts5FindTokenizer)
		(*TFts5Global)(unsafe.Pointer(pGlobal)).Fapi.FxCreateTokenizer_v2 = __ccgo_fp(_fts5CreateTokenizer_v2)
		(*TFts5Global)(unsafe.Pointer(pGlobal)).Fapi.FxFindTokenizer_v2 = __ccgo_fp(_fts5FindTokenizer_v2)
		/* Initialize pGlobal->aLocaleHdr[] to a 128-bit pseudo-random vector.
		 ** The constants below were generated randomly.  */
		Xsqlite3_randomness(tls, int32(16), pGlobal+56)
		**(**Tu32)(__ccgo_up(pGlobal + 56)) ^= uint32(0xF924976D)
		**(**Tu32)(__ccgo_up(pGlobal + 56 + 1*4)) ^= uint32(0x16596E13)
		**(**Tu32)(__ccgo_up(pGlobal + 56 + 2*4)) ^= uint32(0x7C80BEAA)
		**(**Tu32)(__ccgo_up(pGlobal + 56 + 3*4)) ^= uint32(0x9B03A67F)
		rc = Xsqlite3_create_module_v2(tls, db, __ccgo_ts+41953, uintptr(unsafe.Pointer(&_fts5Mod)), p, __ccgo_fp(_fts5ModuleDestroy))
		if rc == SQLITE_OK {
			rc = _sqlite3Fts5IndexInit(tls, db)
		}
		if rc == SQLITE_OK {
			rc = _sqlite3Fts5ExprInit(tls, pGlobal, db)
		}
		if rc == SQLITE_OK {
			rc = _sqlite3Fts5AuxInit(tls, pGlobal)
		}
		if rc == SQLITE_OK {
			rc = _sqlite3Fts5TokenizerInit(tls, pGlobal)
		}
		if rc == SQLITE_OK {
			rc = _sqlite3Fts5VocabInit(tls, pGlobal, db)
		}
		if rc == SQLITE_OK {
			rc = Xsqlite3_create_function(tls, db, __ccgo_ts+41953, int32(1), int32(SQLITE_UTF8), p, __ccgo_fp(_fts5Fts5Func), uintptr(0), uintptr(0))
		}
		if rc == SQLITE_OK {
			rc = Xsqlite3_create_function(tls, db, __ccgo_ts+41958, 0, libc.Int32FromInt32(SQLITE_UTF8)|libc.Int32FromInt32(SQLITE_DETERMINISTIC)|libc.Int32FromInt32(SQLITE_INNOCUOUS), p, __ccgo_fp(_fts5SourceIdFunc), uintptr(0), uintptr(0))
		}
		if rc == SQLITE_OK {
			rc = Xsqlite3_create_function(tls, db, __ccgo_ts+41973, int32(2), libc.Int32FromInt32(SQLITE_UTF8)|libc.Int32FromInt32(SQLITE_INNOCUOUS)|libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)|libc.Int32FromInt32(SQLITE_SUBTYPE), p, __ccgo_fp(_fts5LocaleFunc), uintptr(0), uintptr(0))
		}
		if rc == SQLITE_OK {
			rc = Xsqlite3_create_function(tls, db, __ccgo_ts+41985, int32(1), libc.Int32FromInt32(SQLITE_UTF8)|libc.Int32FromInt32(SQLITE_INNOCUOUS)|libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE), p, __ccgo_fp(_fts5InsttokenFunc), uintptr(0), uintptr(0))
		}
	}
	/* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
	 ** fts5_test_mi.c is compiled and linked into the executable. And call
	 ** its entry point to enable the matchinfo() demo.  */
	return rc
}

func _fts5IntegrityCheckPgidx(tls *libc.TLS, p uintptr, iRowid Ti64, pLeaf uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var iOff, iTermOff Ti64
	var ii, res int32
	var _ /* buf1 at bp+0 */ TFts5Buffer
	var _ /* buf2 at bp+12 */ TFts5Buffer
	var _ /* nByte at bp+28 */ int32
	var _ /* nByte at bp+36 */ int32
	var _ /* nIncr at bp+24 */ int32
	var _ /* nKeep at bp+32 */ int32
	_, _, _, _ = iOff, iTermOff, ii, res
	iTermOff = 0
	**(**TFts5Buffer)(__ccgo_up(bp)) = TFts5Buffer{}
	**(**TFts5Buffer)(__ccgo_up(bp + 12)) = TFts5Buffer{}
	ii = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf
	for ii < (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn && (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		ii = ii + _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(ii), bp+24)
		iTermOff = iTermOff + int64(**(**int32)(__ccgo_up(bp + 24)))
		iOff = iTermOff
		if iOff >= int64((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf) {
			_fts5IndexCorruptRowid(tls, p, iRowid)
		} else {
			if iTermOff == int64(**(**int32)(__ccgo_up(bp + 24))) {
				iOff = iOff + int64(_sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(iOff), bp+28))
				if iOff+int64(**(**int32)(__ccgo_up(bp + 28))) > int64((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf) {
					_fts5IndexCorruptRowid(tls, p, iRowid)
				} else {
					_sqlite3Fts5BufferSet(tls, p+44, bp, **(**int32)(__ccgo_up(bp + 28)), (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(iOff))
				}
			} else {
				iOff = iOff + int64(_sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(iOff), bp+32))
				iOff = iOff + int64(_sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(iOff), bp+36))
				if **(**int32)(__ccgo_up(bp + 32)) > (**(**TFts5Buffer)(__ccgo_up(bp))).Fn || iOff+int64(**(**int32)(__ccgo_up(bp + 36))) > int64((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf) {
					_fts5IndexCorruptRowid(tls, p, iRowid)
				} else {
					(**(**TFts5Buffer)(__ccgo_up(bp))).Fn = **(**int32)(__ccgo_up(bp + 32))
					_sqlite3Fts5BufferAppendBlob(tls, p+44, bp, uint32(**(**int32)(__ccgo_up(bp + 36))), (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(iOff))
				}
				if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					res = _fts5BufferCompare(tls, bp, bp+12)
					if res <= 0 {
						_fts5IndexCorruptRowid(tls, p, iRowid)
					}
				}
			}
		}
		_sqlite3Fts5BufferSet(tls, p+44, bp+12, (**(**TFts5Buffer)(__ccgo_up(bp))).Fn, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp)
	}
	_sqlite3Fts5BufferFree(tls, bp)
	_sqlite3Fts5BufferFree(tls, bp+12)
}

// C documentation
//
//	/*
//	** pToken points to a buffer of size nToken bytes containing a search
//	** term, including the index number at the start, used on a tokendata=1
//	** table. This function returns true if the term in buffer pBuf matches
//	** token pToken/nToken.
//	*/
func _fts5IsTokendataPrefix(tls *libc.TLS, pBuf uintptr, pToken uintptr, nToken int32) (r int32) {
	return libc.BoolInt32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn >= nToken && 0 == libc.Xmemcmp(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp, pToken, uint32(nToken)) && ((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn == nToken || int32(**(**Tu8)(__ccgo_up((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr(nToken)))) == 0x00))
}

func _fts5IterSetOutputCb(tls *libc.TLS, pRc uintptr, pIter uintptr) {
	var pConfig uintptr
	_ = pConfig
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK {
		pConfig = (*TFts5Index)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex)).FpConfig
		if (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail == int32(FTS5_DETAIL_NONE) {
			(*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_None)
		} else {
			if (*TFts5Iter)(unsafe.Pointer(pIter)).FpColset == uintptr(0) {
				(*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Nocolset)
			} else {
				if (*TFts5Colset)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpColset)).FnCol == 0 {
					(*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_ZeroColset)
				} else {
					if (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail == FTS5_DETAIL_FULL {
						(*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Full)
					} else {
						if (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol <= int32(100) {
							(*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Col100)
							_sqlite3Fts5BufferSize(tls, pRc, pIter+32, uint32((*TFts5Config)(unsafe.Pointer(pConfig)).FnCol))
						} else {
							(*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Col)
						}
					}
				}
			}
		}
	}
}

// C documentation
//
//	/*
//	** The iterator passed as the only argument must be a tokendata=1 iterator
//	** (pIter->pTokenDataIter!=0). This function sets the iterator output
//	** variables (pIter->base.*) according to the contents of the current
//	** row.
//	*/
func _fts5IterSetOutputsTokendata(tls *libc.TLS, pIter uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aNew, p, p1, pReader, pT uintptr
	var eDetail, iMin, ii, nByte, nHit, nReader, v3 int32
	var iMinPos, iRowid, nNew Ti64
	var _ /* iPrev at bp+0 */ Ti64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aNew, eDetail, iMin, iMinPos, iRowid, ii, nByte, nHit, nNew, nReader, p, p1, pReader, pT, v3
	nHit = 0
	iRowid = int64(-libc.Int32FromInt32(1)) - (libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32))
	iMin = 0
	pT = (*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter
	(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = 0
	(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = uintptr(0)
	ii = 0
	for {
		if !(int64(ii) < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter) {
			break
		}
		p = *(*uintptr)(unsafe.Pointer(pT + 56 + uintptr(ii)*4))
		if int32((*TFts5Iter)(unsafe.Pointer(p)).Fbase.FbEof) == 0 {
			if nHit == 0 || (*TFts5Iter)(unsafe.Pointer(p)).Fbase.FiRowid < iRowid {
				iRowid = (*TFts5Iter)(unsafe.Pointer(p)).Fbase.FiRowid
				nHit = int32(1)
				(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(p)).Fbase.FpData
				(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5Iter)(unsafe.Pointer(p)).Fbase.FnData
				iMin = ii
			} else {
				if (*TFts5Iter)(unsafe.Pointer(p)).Fbase.FiRowid == iRowid {
					nHit = nHit + 1
				}
			}
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	if nHit == 0 {
		(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FbEof = uint8(1)
	} else {
		eDetail = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex)).FpConfig)).FeDetail
		(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FbEof = uint8(0)
		(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FiRowid = iRowid
		if nHit == int32(1) && eDetail == FTS5_DETAIL_FULL {
			_fts5TokendataIterAppendMap(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, pT, iMin, 0, iRowid, int64(-int32(1)))
		} else {
			if nHit > int32(1) && eDetail != int32(FTS5_DETAIL_NONE) {
				nReader = 0
				nByte = 0
				**(**Ti64)(__ccgo_up(bp)) = 0
				/* Allocate array of iterators if they are not already allocated. */
				if (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader == uintptr(0) {
					(*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader = _sqlite3Fts5MallocZero(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex+44, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter*int64(libc.Uint32FromInt64(24)+libc.Uint32FromInt64(4)))
					if (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader == uintptr(0) {
						return
					}
					(*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistToIter = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter)*24
				}
				/* Populate an iterator for each poslist that will be merged */
				ii = 0
				for {
					if !(int64(ii) < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter) {
						break
					}
					p1 = *(*uintptr)(unsafe.Pointer(pT + 56 + uintptr(ii)*4))
					if iRowid == (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FiRowid {
						**(**int32)(__ccgo_up((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistToIter + uintptr(nReader)*4)) = ii
						v3 = nReader
						nReader = nReader + 1
						_sqlite3Fts5PoslistReaderInit(tls, (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FpData, (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FnData, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader+uintptr(v3)*24)
						nByte = nByte + (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FnData
					}
					goto _2
				_2:
					;
					ii = ii + 1
				}
				/* Ensure the output buffer is large enough */
				if uint32((*TFts5Buffer)(unsafe.Pointer(pIter+32)).Fn)+uint32(nByte+nHit*libc.Int32FromInt32(10)) <= uint32((*TFts5Buffer)(unsafe.Pointer(pIter+32)).FnSpace) {
					v3 = 0
				} else {
					v3 = _sqlite3Fts5BufferSize(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex+44, pIter+32, uint32(nByte+nHit*int32(10)+(*TFts5Buffer)(unsafe.Pointer(pIter+32)).Fn))
				}
				if v3 != 0 {
					return
				}
				/* Ensure the token-mapping is large enough */
				if eDetail == FTS5_DETAIL_FULL && (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap+int64(nByte) {
					nNew = ((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc + int64(nByte)) * int64(2)
					aNew = Xsqlite3_realloc64(tls, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap, uint64(nNew*int64(24)))
					if aNew == uintptr(0) {
						(*TFts5Index)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex)).Frc = int32(SQLITE_NOMEM)
						return
					}
					(*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap = aNew
					(*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc = nNew
				}
				(*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fn = 0
				for int32(1) != 0 {
					iMinPos = libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)
					/* Find smallest position */
					iMin = 0
					ii = 0
					for {
						if !(ii < nReader) {
							break
						}
						pReader = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader + uintptr(ii)*24
						if int32((*TFts5PoslistReader)(unsafe.Pointer(pReader)).FbEof) == 0 {
							if (*TFts5PoslistReader)(unsafe.Pointer(pReader)).FiPos < iMinPos {
								iMinPos = (*TFts5PoslistReader)(unsafe.Pointer(pReader)).FiPos
								iMin = ii
							}
						}
						goto _5
					_5:
						;
						ii = ii + 1
					}
					/* If all readers were at EOF, break out of the loop. */
					if iMinPos == libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32) {
						break
					}
					_sqlite3Fts5PoslistSafeAppend(tls, pIter+32, bp, iMinPos)
					_sqlite3Fts5PoslistReaderNext(tls, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistReader+uintptr(iMin)*24)
					if eDetail == FTS5_DETAIL_FULL {
						(**(**TFts5TokenDataMap)(__ccgo_up((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FiPos = iMinPos
						(**(**TFts5TokenDataMap)(__ccgo_up((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FiIter = **(**int32)(__ccgo_up((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaPoslistToIter + uintptr(iMin)*4))
						(**(**TFts5TokenDataMap)(__ccgo_up((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FiRowid = iRowid
						(*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap + 1
					}
				}
				(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp
				(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fn
			}
		}
	}
}

// C documentation
//
//	/*
//	** xSetOutputs callback used when:
//	**
//	**   * detail=col,
//	**   * there is a column filter, and
//	**   * the table contains 100 or fewer columns.
//	**
//	** The last point is to ensure all column numbers are stored as
//	** single-byte varints.
//	*/
func _fts5IterSetOutputs_Col100(tls *libc.TLS, pIter uintptr, pSeg uintptr) {
	var a, aOut, aiCol, aiColEnd, pEnd, v1 uintptr
	var iPrev, iPrevOut int32
	_, _, _, _, _, _, _, _ = a, aOut, aiCol, aiColEnd, iPrev, iPrevOut, pEnd, v1
	if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset+int64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos) > int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).FszLeaf) || (*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos > (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex)).FpConfig)).FnCol {
		_fts5IterSetOutputs_Col(tls, pIter, pSeg)
	} else {
		a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)).Fp + uintptr((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafOffset)
		pEnd = a + uintptr((*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos)
		iPrev = 0
		aiCol = (*TFts5Iter)(unsafe.Pointer(pIter)).FpColset + 4
		aiColEnd = aiCol + uintptr((*TFts5Colset)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FpColset)).FnCol)*4
		aOut = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp
		iPrevOut = 0
		(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FiRowid = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid
		for a < pEnd {
			v1 = a
			a = a + 1
			iPrev = iPrev + (int32(**(**Tu8)(__ccgo_up(v1))) - int32(2))
			for **(**int32)(__ccgo_up(aiCol)) < iPrev {
				aiCol += 4
				if aiCol == aiColEnd {
					goto setoutputs_col_out
				}
			}
			if **(**int32)(__ccgo_up(aiCol)) == iPrev {
				v1 = aOut
				aOut = aOut + 1
				**(**Tu8)(__ccgo_up(v1)) = uint8(iPrev - iPrevOut + libc.Int32FromInt32(2))
				iPrevOut = iPrev
			}
		}
		goto setoutputs_col_out
	setoutputs_col_out:
		;
		(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FpData = (*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp
		(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FnData = int32(aOut) - int32((*TFts5Iter)(unsafe.Pointer(pIter)).Fposlist.Fp)
	}
}

// C documentation
//
//	/*
//	** The iterator object passed as the second argument currently contains
//	** no valid values except for the Fts5SegIter.pLeaf member variable. This
//	** function searches the leaf page for a term matching (pTerm/nTerm).
//	**
//	** If the specified term is found on the page, then the iterator is left
//	** pointing to it. If argument bGe is zero and the term is not found,
//	** the iterator is left pointing at EOF.
//	**
//	** If bGe is non-zero and the specified term is not found, then the
//	** iterator is left pointing to the smallest term in the segment that
//	** is larger than the specified term, even if this term is not on the
//	** current page.
//	*/
func _fts5LeafSeek(tls *libc.TLS, p uintptr, bGe int32, pIter uintptr, pTerm uintptr, nTerm int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var a uintptr
	var bEndOfPage int32
	var i, iPgidx, n, nCmp, nMatch, v1 Tu32
	var v2 uint32
	var _ /* iOff at bp+0 */ Tu32
	var _ /* iTermOff at bp+12 */ Tu32
	var _ /* nExtra at bp+16 */ int32
	var _ /* nKeep at bp+4 */ Tu32
	var _ /* nNew at bp+8 */ Tu32
	_, _, _, _, _, _, _, _, _ = a, bEndOfPage, i, iPgidx, n, nCmp, nMatch, v1, v2
	a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp
	n = uint32((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn)
	nMatch = uint32(0)
	**(**Tu32)(__ccgo_up(bp + 4)) = uint32(0)
	**(**Tu32)(__ccgo_up(bp + 8)) = uint32(0) /* Current offset in pgidx */
	bEndOfPage = 0
	iPgidx = uint32((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf)
	iPgidx = iPgidx + uint32(_sqlite3Fts5GetVarint32(tls, a+uintptr(iPgidx), bp+12))
	**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp + 12))
	if **(**Tu32)(__ccgo_up(bp)) > n {
		_fts5IndexCorruptIter(tls, p, pIter)
		return
	}
	for int32(1) != 0 {
		/* Figure out how many new bytes are in this term */
		v1 = **(**Tu32)(__ccgo_up(bp))
		**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + 1
		**(**Tu32)(__ccgo_up(bp + 8)) = uint32(**(**Tu8)(__ccgo_up(a + uintptr(v1))))
		if **(**Tu32)(__ccgo_up(bp + 8))&uint32(0x80) != 0 {
			**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) - 1
			**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + uint32(_sqlite3Fts5GetVarint32(tls, a+uintptr(**(**Tu32)(__ccgo_up(bp))), bp+8))
		}
		if **(**Tu32)(__ccgo_up(bp + 4)) < nMatch {
			goto search_failed
		}
		if **(**Tu32)(__ccgo_up(bp))+**(**Tu32)(__ccgo_up(bp + 8)) > n {
			_fts5IndexCorruptIter(tls, p, pIter)
			return
		}
		if **(**Tu32)(__ccgo_up(bp + 4)) == nMatch {
			if **(**Tu32)(__ccgo_up(bp + 8)) < uint32(nTerm)-nMatch {
				v2 = **(**Tu32)(__ccgo_up(bp + 8))
			} else {
				v2 = uint32(nTerm) - nMatch
			}
			nCmp = v2
			i = uint32(0)
			for {
				if !(i < nCmp) {
					break
				}
				if int32(**(**Tu8)(__ccgo_up(a + uintptr(**(**Tu32)(__ccgo_up(bp))+i)))) != int32(**(**Tu8)(__ccgo_up(pTerm + uintptr(nMatch+i)))) {
					break
				}
				goto _3
			_3:
				;
				i = i + 1
			}
			nMatch = nMatch + i
			if uint32(nTerm) == nMatch {
				if i == **(**Tu32)(__ccgo_up(bp + 8)) {
					goto search_success
				} else {
					goto search_failed
				}
			} else {
				if i < **(**Tu32)(__ccgo_up(bp + 8)) && int32(**(**Tu8)(__ccgo_up(a + uintptr(**(**Tu32)(__ccgo_up(bp))+i)))) > int32(**(**Tu8)(__ccgo_up(pTerm + uintptr(nMatch)))) {
					goto search_failed
				}
			}
		}
		if iPgidx >= n {
			bEndOfPage = int32(1)
			break
		}
		iPgidx = iPgidx + uint32(_sqlite3Fts5GetVarint32(tls, a+uintptr(iPgidx), bp+4))
		**(**Tu32)(__ccgo_up(bp + 12)) = **(**Tu32)(__ccgo_up(bp + 12)) + **(**Tu32)(__ccgo_up(bp + 4))
		**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp + 12))
		if **(**Tu32)(__ccgo_up(bp)) >= n {
			_fts5IndexCorruptIter(tls, p, pIter)
			return
		}
		/* Read the nKeep field of the next term. */
		v1 = **(**Tu32)(__ccgo_up(bp))
		**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + 1
		**(**Tu32)(__ccgo_up(bp + 4)) = uint32(**(**Tu8)(__ccgo_up(a + uintptr(v1))))
		if **(**Tu32)(__ccgo_up(bp + 4))&uint32(0x80) != 0 {
			**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) - 1
			**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + uint32(_sqlite3Fts5GetVarint32(tls, a+uintptr(**(**Tu32)(__ccgo_up(bp))), bp+4))
		}
	}
	goto search_failed
search_failed:
	;
	if bGe == 0 {
		_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = uintptr(0)
		return
	} else {
		if bEndOfPage != 0 {
			for cond := true; cond; cond = int32(1) != 0 {
				_fts5SegIterNextPage(tls, p, pIter)
				if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf == uintptr(0) {
					return
				}
				a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp
				if libc.BoolInt32((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf >= (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn) == 0 {
					iPgidx = uint32((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf)
					iPgidx = iPgidx + uint32(_sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp+uintptr(iPgidx), bp))
					if **(**Tu32)(__ccgo_up(bp)) < uint32(4) || int64(**(**Tu32)(__ccgo_up(bp))) >= int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf) {
						_fts5IndexCorruptIter(tls, p, pIter)
						return
					} else {
						**(**Tu32)(__ccgo_up(bp + 4)) = uint32(0)
						**(**Tu32)(__ccgo_up(bp + 12)) = **(**Tu32)(__ccgo_up(bp))
						n = uint32((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn)
						**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + uint32(_sqlite3Fts5GetVarint32(tls, a+uintptr(**(**Tu32)(__ccgo_up(bp))), bp+8))
						break
					}
				}
			}
		}
	}
	goto search_success
search_success:
	;
	if int64(**(**Tu32)(__ccgo_up(bp)))+int64(**(**Tu32)(__ccgo_up(bp + 8))) > int64(n) || **(**Tu32)(__ccgo_up(bp + 8)) < uint32(1) {
		_fts5IndexCorruptIter(tls, p, pIter)
		return
	}
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(**(**Tu32)(__ccgo_up(bp)) + **(**Tu32)(__ccgo_up(bp + 8)))
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafOffset = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset)
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafPgno = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno
	_sqlite3Fts5BufferSet(tls, p+44, pIter+72, int32(**(**Tu32)(__ccgo_up(bp + 4))), pTerm)
	_sqlite3Fts5BufferAppendBlob(tls, p+44, pIter+72, **(**Tu32)(__ccgo_up(bp + 8)), a+uintptr(**(**Tu32)(__ccgo_up(bp))))
	if iPgidx >= n {
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn + int32(1)
	} else {
		iPgidx = iPgidx + uint32(_sqlite3Fts5GetVarint32(tls, a+uintptr(iPgidx), bp+16))
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = int32(**(**Tu32)(__ccgo_up(bp + 12)) + uint32(**(**int32)(__ccgo_up(bp + 16))))
	}
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = int32(iPgidx)
	_fts5SegIterLoadRowid(tls, p, pIter)
	_fts5SegIterLoadNPos(tls, p, pIter)
}

// C documentation
//
//	/*
//	** Implementation of fts5_locale(LOCALE, TEXT) function.
//	**
//	** If parameter LOCALE is NULL, or a zero-length string, then a copy of
//	** TEXT is returned. Otherwise, both LOCALE and TEXT are interpreted as
//	** text, and the value returned is a blob consisting of:
//	**
//	**     * The 4 bytes 0x00, 0xE0, 0xB2, 0xEb (FTS5_LOCALE_HEADER).
//	**     * The LOCALE, as utf-8 text, followed by
//	**     * 0x00, followed by
//	**     * The TEXT, as utf-8 text.
//	**
//	** There is no final nul-terminator following the TEXT value.
//	*/
func _fts5LocaleFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) {
	var nBlob, nLocale, nText Ti64
	var p, pBlob, pCsr, zLocale, zText, v1 uintptr
	_, _, _, _, _, _, _, _, _ = nBlob, nLocale, nText, p, pBlob, pCsr, zLocale, zText, v1
	zLocale = uintptr(0)
	nLocale = 0
	zText = uintptr(0)
	nText = 0
	_ = nArg
	zLocale = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(apArg)))
	nLocale = int64(Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(apArg))))
	zText = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(apArg + 1*4)))
	nText = int64(Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(apArg + 1*4))))
	if zLocale == uintptr(0) || int32(**(**int8)(__ccgo_up(zLocale))) == int32('\000') {
		Xsqlite3_result_text(tls, pCtx, zText, int32(nText), uintptr(-libc.Int32FromInt32(1)))
	} else {
		p = Xsqlite3_user_data(tls, pCtx)
		pBlob = uintptr(0)
		pCsr = uintptr(0)
		nBlob = 0
		nBlob = int64(libc.Int32FromInt64(16)) + nLocale + int64(1) + nText
		pBlob = Xsqlite3_malloc64(tls, uint64(nBlob))
		if pBlob == uintptr(0) {
			Xsqlite3_result_error_nomem(tls, pCtx)
			return
		}
		pCsr = pBlob
		libc.Xmemcpy(tls, pCsr, p+56, uint32(libc.Int32FromInt64(16)))
		pCsr = pCsr + uintptr(libc.Int32FromInt64(16))
		libc.Xmemcpy(tls, pCsr, zLocale, uint32(nLocale))
		pCsr = pCsr + uintptr(nLocale)
		v1 = pCsr
		pCsr = pCsr + 1
		**(**Tu8)(__ccgo_up(v1)) = uint8(0x00)
		if zText != 0 {
			libc.Xmemcpy(tls, pCsr, zText, uint32(nText))
		}
		Xsqlite3_result_blob(tls, pCtx, pBlob, int32(nBlob), __ccgo_fp(Xsqlite3_free))
	}
}

// C documentation
//
//	/*
//	** Array aBuf[] contains nBuf doclists. These are all merged in with the
//	** doclist in buffer p1.
//	*/
func _fts5MergePrefixLists(tls *libc.TLS, p uintptr, p1 uintptr, nBuf int32, aBuf uintptr) {
	bp := tls.Alloc(944)
	defer tls.Free(944)
	var i, nMerge, nOut, nTail, nTmp int32
	var iLastRowid Ti64
	var pI, pNext, pSave, pThis, pThis1, pX uintptr
	var _ /* aMerger at bp+0 */ [16]TPrefixMerger
	var _ /* iPrev at bp+928 */ Ti64
	var _ /* out at bp+900 */ TFts5Buffer
	var _ /* pHead at bp+896 */ uintptr
	var _ /* tmp at bp+912 */ TFts5Buffer
	_, _, _, _, _, _, _, _, _, _, _, _ = i, iLastRowid, nMerge, nOut, nTail, nTmp, pI, pNext, pSave, pThis, pThis1, pX
	**(**uintptr)(__ccgo_up(bp + 896)) = uintptr(0)
	nOut = 0
	**(**TFts5Buffer)(__ccgo_up(bp + 900)) = TFts5Buffer{}
	**(**TFts5Buffer)(__ccgo_up(bp + 912)) = TFts5Buffer{}
	iLastRowid = 0
	/* Initialize a doclist-iterator for each input buffer. Arrange them in
	 ** a linked-list starting at pHead in ascending order of rowid. Avoid
	 ** linking any iterators already at EOF into the linked list at all. */
	libc.Xmemset(tls, bp, 0, uint32(56)*uint32(nBuf+libc.Int32FromInt32(1)))
	**(**uintptr)(__ccgo_up(bp + 896)) = bp + uintptr(nBuf)*56
	_fts5DoclistIterInit(tls, p1, **(**uintptr)(__ccgo_up(bp + 896)))
	i = 0
	for {
		if !(i < nBuf) {
			break
		}
		_fts5DoclistIterInit(tls, aBuf+uintptr(i)*12, bp+uintptr(i)*56)
		_fts5PrefixMergerInsertByRowid(tls, bp+896, bp+uintptr(i)*56)
		nOut = nOut + (**(**TFts5Buffer)(__ccgo_up(aBuf + uintptr(i)*12))).Fn
		goto _1
	_1:
		;
		i = i + 1
	}
	if nOut == 0 {
		return
	}
	nOut = nOut + ((*TFts5Buffer)(unsafe.Pointer(p1)).Fn + int32(9) + int32(10)*nBuf)
	/* The maximum size of the output is equal to the sum of the
	 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
	 ** first rowid in one input is a large negative number, and the first in
	 ** the other a non-negative number, the delta for the non-negative
	 ** number will be larger on disk than the literal integer value
	 ** was.
	 **
	 ** Or, if the input position-lists are corrupt, then the output might
	 ** include up to (nBuf+1) extra 10-byte positions created by interpreting -1
	 ** (the value PoslistNext64() uses for EOF) as a position and appending
	 ** it to the output. This can happen at most once for each input
	 ** position-list, hence (nBuf+1) 10 byte paddings.  */
	if _sqlite3Fts5BufferSize(tls, p+44, bp+900, uint32(nOut)) != 0 {
		return
	}
	for **(**uintptr)(__ccgo_up(bp + 896)) != 0 {
		**(**int32)(__ccgo_up(bp + 900 + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+900)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+900)).Fn), uint64((*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).Fiter.FiRowid)-uint64(iLastRowid))
		iLastRowid = (*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).Fiter.FiRowid
		if (*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).FpNext != 0 && iLastRowid == (*TPrefixMerger)(unsafe.Pointer((*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).FpNext)).Fiter.FiRowid {
			/* Merge data from two or more poslists */
			**(**Ti64)(__ccgo_up(bp + 928)) = 0
			nTmp = int32(FTS5_DATA_ZERO_PADDING)
			nMerge = 0
			pSave = **(**uintptr)(__ccgo_up(bp + 896))
			pThis = uintptr(0)
			nTail = 0
			**(**uintptr)(__ccgo_up(bp + 896)) = uintptr(0)
			for pSave != 0 && (*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FiRowid == iLastRowid {
				pNext = (*TPrefixMerger)(unsafe.Pointer(pSave)).FpNext
				(*TPrefixMerger)(unsafe.Pointer(pSave)).FiOff = 0
				(*TPrefixMerger)(unsafe.Pointer(pSave)).FiPos = 0
				(*TPrefixMerger)(unsafe.Pointer(pSave)).FaPos = (*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FaPoslist + uintptr((*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FnSize)
				_sqlite3Fts5PoslistNext64(tls, (*TPrefixMerger)(unsafe.Pointer(pSave)).FaPos, (*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FnPoslist, pSave+40, pSave+32)
				nTmp = nTmp + ((*TPrefixMerger)(unsafe.Pointer(pSave)).Fiter.FnPoslist + int32(10))
				nMerge = nMerge + 1
				_fts5PrefixMergerInsertByPosition(tls, bp+896, pSave)
				pSave = pNext
			}
			if **(**uintptr)(__ccgo_up(bp + 896)) == uintptr(0) || (*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).FpNext == uintptr(0) {
				_fts5IndexCorruptIdx(tls, p)
				break
			}
			/* See the earlier comment in this function for an explanation of why
			 ** corrupt input position lists might cause the output to consume
			 ** at most nMerge*10 bytes of unexpected space. */
			if _sqlite3Fts5BufferSize(tls, p+44, bp+912, uint32(nTmp+nMerge*int32(10))) != 0 {
				break
			}
			_sqlite3Fts5BufferZero(tls, bp+912)
			pThis = **(**uintptr)(__ccgo_up(bp + 896))
			**(**uintptr)(__ccgo_up(bp + 896)) = (*TPrefixMerger)(unsafe.Pointer(pThis)).FpNext
			_sqlite3Fts5PoslistSafeAppend(tls, bp+912, bp+928, (*TPrefixMerger)(unsafe.Pointer(pThis)).FiPos)
			_sqlite3Fts5PoslistNext64(tls, (*TPrefixMerger)(unsafe.Pointer(pThis)).FaPos, (*TPrefixMerger)(unsafe.Pointer(pThis)).Fiter.FnPoslist, pThis+40, pThis+32)
			_fts5PrefixMergerInsertByPosition(tls, bp+896, pThis)
			for (*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).FpNext != 0 {
				pThis = **(**uintptr)(__ccgo_up(bp + 896))
				if (*TPrefixMerger)(unsafe.Pointer(pThis)).FiPos != **(**Ti64)(__ccgo_up(bp + 928)) {
					_sqlite3Fts5PoslistSafeAppend(tls, bp+912, bp+928, (*TPrefixMerger)(unsafe.Pointer(pThis)).FiPos)
				}
				_sqlite3Fts5PoslistNext64(tls, (*TPrefixMerger)(unsafe.Pointer(pThis)).FaPos, (*TPrefixMerger)(unsafe.Pointer(pThis)).Fiter.FnPoslist, pThis+40, pThis+32)
				**(**uintptr)(__ccgo_up(bp + 896)) = (*TPrefixMerger)(unsafe.Pointer(pThis)).FpNext
				_fts5PrefixMergerInsertByPosition(tls, bp+896, pThis)
			}
			if (*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).FiPos != **(**Ti64)(__ccgo_up(bp + 928)) {
				_sqlite3Fts5PoslistSafeAppend(tls, bp+912, bp+928, (*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).FiPos)
			}
			nTail = (*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).Fiter.FnPoslist - (*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).FiOff
			/* WRITEPOSLISTSIZE */
			if (**(**TFts5Buffer)(__ccgo_up(bp + 912))).Fn+nTail > nTmp-int32(FTS5_DATA_ZERO_PADDING) {
				if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					_fts5IndexCorruptIdx(tls, p)
				}
				break
			}
			**(**int32)(__ccgo_up(bp + 900 + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+900)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+900)).Fn), uint64(((**(**TFts5Buffer)(__ccgo_up(bp + 912))).Fn+nTail)*libc.Int32FromInt32(2)))
			libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(bp+900)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+900)).Fn), (**(**TFts5Buffer)(__ccgo_up(bp + 912))).Fp, uint32((**(**TFts5Buffer)(__ccgo_up(bp + 912))).Fn))
			**(**int32)(__ccgo_up(bp + 900 + 4)) += (**(**TFts5Buffer)(__ccgo_up(bp + 912))).Fn
			if nTail > 0 {
				libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(bp+900)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+900)).Fn), (*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).FaPos+uintptr((*TPrefixMerger)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 896)))).FiOff), uint32(nTail))
				**(**int32)(__ccgo_up(bp + 900 + 4)) += nTail
			}
			**(**uintptr)(__ccgo_up(bp + 896)) = pSave
			i = 0
			for {
				if !(i < nBuf+int32(1)) {
					break
				}
				pX = bp + uintptr(i)*56
				if (*TPrefixMerger)(unsafe.Pointer(pX)).Fiter.FaPoslist != 0 && (*TPrefixMerger)(unsafe.Pointer(pX)).Fiter.FiRowid == iLastRowid {
					_fts5DoclistIterNext(tls, pX)
					_fts5PrefixMergerInsertByRowid(tls, bp+896, pX)
				}
				goto _2
			_2:
				;
				i = i + 1
			}
		} else {
			/* Copy poslist from pHead to output */
			pThis1 = **(**uintptr)(__ccgo_up(bp + 896))
			pI = pThis1
			libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(bp+900)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+900)).Fn), (*TFts5DoclistIter)(unsafe.Pointer(pI)).FaPoslist, uint32((*TFts5DoclistIter)(unsafe.Pointer(pI)).FnPoslist+(*TFts5DoclistIter)(unsafe.Pointer(pI)).FnSize))
			**(**int32)(__ccgo_up(bp + 900 + 4)) += (*TFts5DoclistIter)(unsafe.Pointer(pI)).FnPoslist + (*TFts5DoclistIter)(unsafe.Pointer(pI)).FnSize
			_fts5DoclistIterNext(tls, pI)
			**(**uintptr)(__ccgo_up(bp + 896)) = (*TPrefixMerger)(unsafe.Pointer(pThis1)).FpNext
			_fts5PrefixMergerInsertByRowid(tls, bp+896, pThis1)
		}
	}
	_sqlite3Fts5BufferFree(tls, p1)
	_sqlite3Fts5BufferFree(tls, bp+912)
	libc.Xmemset(tls, (**(**TFts5Buffer)(__ccgo_up(bp + 900))).Fp+uintptr((**(**TFts5Buffer)(__ccgo_up(bp + 900))).Fn), 0, uint32(FTS5_DATA_ZERO_PADDING))
	**(**TFts5Buffer)(__ccgo_up(p1)) = **(**TFts5Buffer)(__ccgo_up(bp + 900))
}

// C documentation
//
//	/*
//	** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
//	** In this case the buffers consist of a delta-encoded list of rowids only.
//	*/
func _fts5MergeRowidLists(tls *libc.TLS, p uintptr, p1 uintptr, nBuf int32, aBuf uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var iOut Ti64
	var p2 uintptr
	var _ /* i1 at bp+0 */ int32
	var _ /* i2 at bp+4 */ int32
	var _ /* iRowid1 at bp+8 */ Ti64
	var _ /* iRowid2 at bp+16 */ Ti64
	var _ /* out at bp+24 */ TFts5Buffer
	_, _ = iOut, p2
	**(**int32)(__ccgo_up(bp)) = 0
	**(**int32)(__ccgo_up(bp + 4)) = 0
	**(**Ti64)(__ccgo_up(bp + 8)) = 0
	**(**Ti64)(__ccgo_up(bp + 16)) = 0
	iOut = 0
	p2 = aBuf
	_ = nBuf
	libc.Xmemset(tls, bp+24, 0, uint32(12))
	_sqlite3Fts5BufferSize(tls, p+44, bp+24, uint32((*TFts5Buffer)(unsafe.Pointer(p1)).Fn+(*TFts5Buffer)(unsafe.Pointer(p2)).Fn))
	if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
		return
	}
	_fts5NextRowid(tls, p1, bp, bp+8)
	_fts5NextRowid(tls, p2, bp+4, bp+16)
	for **(**int32)(__ccgo_up(bp)) >= 0 || **(**int32)(__ccgo_up(bp + 4)) >= 0 {
		if **(**int32)(__ccgo_up(bp)) >= 0 && (**(**int32)(__ccgo_up(bp + 4)) < 0 || **(**Ti64)(__ccgo_up(bp + 8)) < **(**Ti64)(__ccgo_up(bp + 16))) {
			**(**int32)(__ccgo_up(bp + 24 + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+24)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+24)).Fn), uint64(**(**Ti64)(__ccgo_up(bp + 8))-iOut))
			iOut = **(**Ti64)(__ccgo_up(bp + 8))
			_fts5NextRowid(tls, p1, bp, bp+8)
		} else {
			**(**int32)(__ccgo_up(bp + 24 + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+24)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+24)).Fn), uint64(**(**Ti64)(__ccgo_up(bp + 16))-iOut))
			iOut = **(**Ti64)(__ccgo_up(bp + 16))
			if **(**int32)(__ccgo_up(bp)) >= 0 && **(**Ti64)(__ccgo_up(bp + 8)) == **(**Ti64)(__ccgo_up(bp + 16)) {
				_fts5NextRowid(tls, p1, bp, bp+8)
			}
			_fts5NextRowid(tls, p2, bp+4, bp+16)
		}
	}
	_fts5BufferSwap(tls, bp+24, p1)
	_sqlite3Fts5BufferFree(tls, bp+24)
}

// C documentation
//
//	/*
//	** Sub-iterator iChanged of iterator pIter has just been advanced. It still
//	** points to the same term though - just a different rowid. This function
//	** attempts to update the contents of the pIter->aFirst[] accordingly.
//	** If it does so successfully, 0 is returned. Otherwise 1.
//	**
//	** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
//	** on the iterator instead. That function does the same as this one, except
//	** that it deals with more complicated cases as well.
//	*/
func _fts5MultiIterAdvanceRowid(tls *libc.TLS, pIter uintptr, iChanged int32, ppFirst uintptr) (r int32) {
	var i int32
	var pNew, pOther, pRes uintptr
	var v1 int64
	_, _, _, _, _ = i, pNew, pOther, pRes, v1
	pNew = pIter + 80 + uintptr(iChanged)*104
	if (*TFts5SegIter)(unsafe.Pointer(pNew)).FiRowid == (*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid || libc.BoolInt32((*TFts5SegIter)(unsafe.Pointer(pNew)).FiRowid < (*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid) == (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev {
		pOther = pIter + 80 + uintptr(iChanged^int32(0x0001))*104
		if (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev != 0 {
			v1 = int64(-libc.Int32FromInt32(1)) - (libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32))
		} else {
			v1 = libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)
		}
		(*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid = v1
		i = ((*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg + iChanged) / int32(2)
		for {
			if !(int32(1) != 0) {
				break
			}
			pRes = (*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(i)*4
			if (*TFts5CResult)(unsafe.Pointer(pRes)).FbTermEq != 0 {
				if (*TFts5SegIter)(unsafe.Pointer(pNew)).FiRowid == (*TFts5SegIter)(unsafe.Pointer(pOther)).FiRowid {
					return int32(1)
				} else {
					if libc.BoolInt32((*TFts5SegIter)(unsafe.Pointer(pOther)).FiRowid > (*TFts5SegIter)(unsafe.Pointer(pNew)).FiRowid) == (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev {
						(*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid = (*TFts5SegIter)(unsafe.Pointer(pOther)).FiRowid
						pNew = pOther
					} else {
						if libc.BoolInt32((*TFts5SegIter)(unsafe.Pointer(pOther)).FiRowid > (*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid) == (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev {
							(*TFts5Iter)(unsafe.Pointer(pIter)).FiSwitchRowid = (*TFts5SegIter)(unsafe.Pointer(pOther)).FiRowid
						}
					}
				}
			}
			(*TFts5CResult)(unsafe.Pointer(pRes)).FiFirst = uint16((int32(pNew) - t__predefined_ptrdiff_t(pIter+80)) / 104)
			if i == int32(1) {
				break
			}
			pOther = pIter + 80 + uintptr((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(i^int32(0x0001))*4))).FiFirst)*104
			goto _2
		_2:
			;
			i = i / int32(2)
		}
	}
	**(**uintptr)(__ccgo_up(ppFirst)) = pNew
	return 0
}

func _fts5MultiIterAlloc(tls *libc.TLS, p uintptr, nSeg int32) (r uintptr) {
	var nSlot Ti64
	var pNew uintptr
	_, _ = nSlot, pNew /* Power of two >= nSeg */
	nSlot = int64(2)
	for {
		if !(nSlot < int64(nSeg)) {
			break
		}
		goto _1
	_1:
		;
		nSlot = nSlot * int64(2)
	}
	pNew = _fts5IdxMalloc(tls, p, int64(uint32(libc.UintptrFromInt32(0)+80))+nSlot*int64(104)+int64(4)*nSlot)
	if pNew != 0 {
		(*TFts5Iter)(unsafe.Pointer(pNew)).FnSeg = int32(nSlot)
		(*TFts5Iter)(unsafe.Pointer(pNew)).FaFirst = pNew + 80 + uintptr(nSlot)*104
		(*TFts5Iter)(unsafe.Pointer(pNew)).FpIndex = p
		(*TFts5Iter)(unsafe.Pointer(pNew)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Noop)
	}
	return pNew
}

// C documentation
//
//	/*
//	** Do the comparison necessary to populate pIter->aFirst[iOut].
//	**
//	** If the returned value is non-zero, then it is the index of an entry
//	** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
//	** to a key that is a duplicate of another, higher priority,
//	** segment-iterator in the pSeg->aSeg[] array.
//	*/
func _fts5MultiIterDoCompare(tls *libc.TLS, pIter uintptr, iOut int32) (r int32) {
	var i1, i2, iRes, res, v1 int32
	var p1, p2, pRes uintptr
	_, _, _, _, _, _, _, _ = i1, i2, iRes, p1, p2, pRes, res, v1 /* Right-hand Fts5SegIter */
	pRes = (*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(iOut)*4
	if iOut >= (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg/int32(2) {
		i1 = (iOut - (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg/int32(2)) * int32(2)
		i2 = i1 + int32(1)
	} else {
		i1 = int32((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(iOut*int32(2))*4))).FiFirst)
		i2 = int32((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(iOut*int32(2)+int32(1))*4))).FiFirst)
	}
	p1 = pIter + 80 + uintptr(i1)*104
	p2 = pIter + 80 + uintptr(i2)*104
	(*TFts5CResult)(unsafe.Pointer(pRes)).FbTermEq = uint8(0)
	if (*TFts5SegIter)(unsafe.Pointer(p1)).FpLeaf == uintptr(0) { /* If p1 is at EOF */
		iRes = i2
	} else {
		if (*TFts5SegIter)(unsafe.Pointer(p2)).FpLeaf == uintptr(0) { /* If p2 is at EOF */
			iRes = i1
		} else {
			res = _fts5BufferCompare(tls, p1+72, p2+72)
			if res == 0 {
				(*TFts5CResult)(unsafe.Pointer(pRes)).FbTermEq = uint8(1)
				if (*TFts5SegIter)(unsafe.Pointer(p1)).FiRowid == (*TFts5SegIter)(unsafe.Pointer(p2)).FiRowid {
					return i2
				}
				if libc.BoolInt32((*TFts5SegIter)(unsafe.Pointer(p1)).FiRowid > (*TFts5SegIter)(unsafe.Pointer(p2)).FiRowid) == (*TFts5Iter)(unsafe.Pointer(pIter)).FbRev {
					v1 = -int32(1)
				} else {
					v1 = +libc.Int32FromInt32(1)
				}
				res = v1
			}
			if res < 0 {
				iRes = i1
			} else {
				iRes = i2
			}
		}
	}
	(*TFts5CResult)(unsafe.Pointer(pRes)).FiFirst = uint16(iRes)
	return 0
}

// C documentation
//
//	/*
//	** All the component segment-iterators of pIter have been set up. This
//	** functions finishes setup for iterator pIter itself.
//	*/
func _fts5MultiIterFinishSetup(tls *libc.TLS, p uintptr, pIter uintptr) {
	var iEq, iIter, v2 int32
	var pSeg, pSeg1 uintptr
	_, _, _, _, _ = iEq, iIter, pSeg, pSeg1, v2
	iIter = (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg - int32(1)
	for {
		if !(iIter > 0) {
			break
		}
		v2 = _fts5MultiIterDoCompare(tls, pIter, iIter)
		iEq = v2
		if v2 != 0 {
			pSeg = pIter + 80 + uintptr(iEq)*104
			if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5SegIter)(unsafe.Pointer(pSeg)).FxNext})))(tls, p, pSeg, uintptr(0))
			}
			_fts5MultiIterAdvanced(tls, p, pIter, iEq, iIter)
		}
		goto _1
	_1:
		;
		iIter = iIter - 1
	}
	_fts5MultiIterSetEof(tls, pIter)
	if (*TFts5Iter)(unsafe.Pointer(pIter)).FbSkipEmpty != 0 && _fts5MultiIterIsEmpty(tls, p, pIter) != 0 || _fts5MultiIterIsDeleted(tls, pIter) != 0 {
		_fts5MultiIterNext(tls, p, pIter, 0, 0)
	} else {
		if int32((*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FbEof) == 0 {
			pSeg1 = pIter + 80 + uintptr((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*104
			(*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs})))(tls, pIter, pSeg1)
		}
	}
}

// C documentation
//
//	/*
//	** Return true if the iterator passed as the only argument points
//	** to an segment entry for which there is a tombstone. Return false
//	** if there is no tombstone or if the iterator is already at EOF.
//	*/
func _fts5MultiIterIsDeleted(tls *libc.TLS, pIter uintptr) (r int32) {
	var iFirst, iPg int32
	var pArray, pSeg uintptr
	_, _, _, _ = iFirst, iPg, pArray, pSeg
	iFirst = int32((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)
	pSeg = pIter + 80 + uintptr(iFirst)*104
	pArray = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpTombArray
	if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf != 0 && pArray != 0 {
		/* Figure out which page the rowid might be present on. */
		iPg = int32(uint64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid) % uint64((*TFts5TombstoneArray)(unsafe.Pointer(pArray)).FnTombstone))
		/* If tombstone hash page iPg has not yet been loaded from the
		 ** database, load it now. */
		if *(*uintptr)(unsafe.Pointer(pArray + 8 + uintptr(iPg)*4)) == uintptr(0) {
			*(*uintptr)(unsafe.Pointer(pArray + 8 + uintptr(iPg)*4)) = _fts5DataRead(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, int64((*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FiSegid+libc.Int32FromInt32(1)<<libc.Int32FromInt32(16))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64(iPg))
			if *(*uintptr)(unsafe.Pointer(pArray + 8 + uintptr(iPg)*4)) == uintptr(0) {
				return 0
			}
		}
		return _fts5IndexTombstoneQuery(tls, *(*uintptr)(unsafe.Pointer(pArray + 8 + uintptr(iPg)*4)), (*TFts5TombstoneArray)(unsafe.Pointer(pArray)).FnTombstone, uint64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiRowid))
	}
	return 0
}

// C documentation
//
//	/*
//	** Create an Fts5Iter that iterates through the doclist provided
//	** as the second argument.
//	*/
func _fts5MultiIterNew2(tls *libc.TLS, p uintptr, pData uintptr, bDesc int32, ppOut uintptr) {
	var pIter, pNew uintptr
	_, _ = pIter, pNew
	pNew = _fts5MultiIterAlloc(tls, p, int32(2))
	if pNew != 0 {
		pIter = pNew + 80 + 1*104
		(*TFts5SegIter)(unsafe.Pointer(pIter)).Fflags = int32(FTS5_SEGITER_ONETERM)
		if (*TFts5Data)(unsafe.Pointer(pData)).FszLeaf > 0 {
			(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = pData
			(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp, pIter+88))
			(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer(pData)).Fnn
			(**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pNew)).FaFirst + 1*4))).FiFirst = uint16(1)
			if bDesc != 0 {
				(*TFts5Iter)(unsafe.Pointer(pNew)).FbRev = int32(1)
				**(**int32)(__ccgo_up(pIter + 4)) |= int32(FTS5_SEGITER_REVERSE)
				_fts5SegIterReverseInitPage(tls, p, pIter)
			} else {
				_fts5SegIterLoadNPos(tls, p, pIter)
			}
			pData = uintptr(0)
		} else {
			(*TFts5Iter)(unsafe.Pointer(pNew)).Fbase.FbEof = uint8(1)
		}
		_fts5SegIterSetNext(tls, p, pIter)
		**(**uintptr)(__ccgo_up(ppOut)) = pNew
	}
	_fts5DataRelease(tls, pData)
}

// C documentation
//
//	/*
//	** Move the iterator to the next entry.
//	**
//	** If an error occurs, an error code is left in Fts5Index.rc. It is not
//	** considered an error if the iterator reaches EOF, or if it is already at
//	** EOF when this function is called.
//	*/
func _fts5MultiIterNext(tls *libc.TLS, p uintptr, pIter uintptr, bFrom int32, iFrom Ti64) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bUseFrom, iFirst int32
	var _ /* bNewTerm at bp+0 */ int32
	var _ /* pSeg at bp+4 */ uintptr
	_, _ = bUseFrom, iFirst
	bUseFrom = bFrom
	for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		iFirst = int32((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)
		**(**int32)(__ccgo_up(bp)) = 0
		**(**uintptr)(__ccgo_up(bp + 4)) = pIter + 80 + uintptr(iFirst)*104
		if bUseFrom != 0 && (*TFts5SegIter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpDlidx != 0 {
			_fts5SegIterNextFrom(tls, p, **(**uintptr)(__ccgo_up(bp + 4)), iFrom)
		} else {
			(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5SegIter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FxNext})))(tls, p, **(**uintptr)(__ccgo_up(bp + 4)), bp)
		}
		if (*TFts5SegIter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpLeaf == uintptr(0) || **(**int32)(__ccgo_up(bp)) != 0 || _fts5MultiIterAdvanceRowid(tls, pIter, iFirst, bp+4) != 0 {
			_fts5MultiIterAdvanced(tls, p, pIter, iFirst, int32(1))
			_fts5MultiIterSetEof(tls, pIter)
			**(**uintptr)(__ccgo_up(bp + 4)) = pIter + 80 + uintptr((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*104
			if (*TFts5SegIter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FpLeaf == uintptr(0) {
				return
			}
		}
		if (int32((*TFts5Iter)(unsafe.Pointer(pIter)).FbSkipEmpty) == 0 || (*TFts5SegIter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 4)))).FnPos != 0) && 0 == _fts5MultiIterIsDeleted(tls, pIter) {
			(*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs})))(tls, pIter, **(**uintptr)(__ccgo_up(bp + 4)))
			return
		}
		bUseFrom = 0
	}
}

func _fts5MultiIterNext2(tls *libc.TLS, p uintptr, pIter uintptr, pbNewTerm uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iFirst int32
	var _ /* bNewTerm at bp+4 */ int32
	var _ /* pSeg at bp+0 */ uintptr
	_ = iFirst
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		**(**int32)(__ccgo_up(pbNewTerm)) = 0
		for cond := true; cond; cond = (_fts5MultiIterIsEmpty(tls, p, pIter) != 0 || _fts5MultiIterIsDeleted(tls, pIter) != 0) && (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			iFirst = int32((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)
			**(**uintptr)(__ccgo_up(bp)) = pIter + 80 + uintptr(iFirst)*104
			**(**int32)(__ccgo_up(bp + 4)) = 0
			(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5SegIter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FxNext})))(tls, p, **(**uintptr)(__ccgo_up(bp)), bp+4)
			if (*TFts5SegIter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpLeaf == uintptr(0) || **(**int32)(__ccgo_up(bp + 4)) != 0 || _fts5MultiIterAdvanceRowid(tls, pIter, iFirst, bp) != 0 {
				_fts5MultiIterAdvanced(tls, p, pIter, iFirst, int32(1))
				_fts5MultiIterSetEof(tls, pIter)
				**(**int32)(__ccgo_up(pbNewTerm)) = int32(1)
			}
		}
	}
}

// C documentation
//
//	/*
//	** This function is used by xCreateTokenizer_v2() and xCreateTokenizer().
//	** It allocates and partially populates a new Fts5TokenizerModule object.
//	** The new object is already linked into the Fts5Global context before
//	** returning.
//	**
//	** If successful, SQLITE_OK is returned and a pointer to the new
//	** Fts5TokenizerModule object returned via output parameter (*ppNew). All
//	** that is required is for the caller to fill in the methods in
//	** Fts5TokenizerModule.x1 and x2, and to set Fts5TokenizerModule.bV2Native
//	** as appropriate.
//	**
//	** If an error occurs, an SQLite error code is returned and the final value
//	** of (*ppNew) undefined.
//	*/
func _fts5NewTokenizerModule(tls *libc.TLS, pGlobal uintptr, zName uintptr, pUserData uintptr, __ccgo_fp_xDestroy uintptr, ppNew uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nByte, nName Tsqlite3_int64
	var pNew, v1 uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _ = nByte, nName, pNew, v1
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK /* Bytes of space to allocate */
	nName = int64(libc.Xstrlen(tls, zName) + uint32(1))
	nByte = int64(48) + nName
	v1 = _sqlite3Fts5MallocZero(tls, bp, nByte)
	pNew = v1
	**(**uintptr)(__ccgo_up(ppNew)) = v1
	if pNew != 0 {
		(*TFts5TokenizerModule)(unsafe.Pointer(pNew)).FzName = pNew + 1*48
		libc.Xmemcpy(tls, (*TFts5TokenizerModule)(unsafe.Pointer(pNew)).FzName, zName, uint32(nName))
		(*TFts5TokenizerModule)(unsafe.Pointer(pNew)).FpUserData = pUserData
		(*TFts5TokenizerModule)(unsafe.Pointer(pNew)).FxDestroy = __ccgo_fp_xDestroy
		(*TFts5TokenizerModule)(unsafe.Pointer(pNew)).FpNext = (*TFts5Global)(unsafe.Pointer(pGlobal)).FpTok
		(*TFts5Global)(unsafe.Pointer(pGlobal)).FpTok = pNew
		if (*TFts5TokenizerModule)(unsafe.Pointer(pNew)).FpNext == uintptr(0) {
			(*TFts5Global)(unsafe.Pointer(pGlobal)).FpDfltTok = pNew
		}
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Advance the cursor to the next row in the table that matches the
//	** search criteria.
//	**
//	** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
//	** even if we reach end-of-file.  The fts5EofMethod() will be called
//	** subsequently to determine whether or not an EOF was hit.
//	*/
func _fts5NextMethod(tls *libc.TLS, pCursor uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pConfig, pCsr uintptr
	var rc, v1 int32
	var _ /* bSkip at bp+0 */ int32
	_, _, _, _ = pConfig, pCsr, rc, v1
	pCsr = pCursor
	/* If this cursor uses FTS5_PLAN_MATCH and this is a tokendata=1 table,
	 ** clear any token mappings accumulated at the fts5_index.c level. In
	 ** other cases, specifically FTS5_PLAN_SOURCE and FTS5_PLAN_SORTED_MATCH,
	 ** we need to retain the mappings for the entire query.  */
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan == int32(FTS5_PLAN_MATCH) && (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab)).FpConfig)).FbTokendata != 0 {
		_sqlite3Fts5ExprClearTokens(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr)
	}
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan < int32(3) {
		**(**int32)(__ccgo_up(bp)) = 0
		v1 = _fts5CursorReseek(tls, pCsr, bp)
		rc = v1
		if v1 != 0 || **(**int32)(__ccgo_up(bp)) != 0 {
			return rc
		}
		rc = _sqlite3Fts5ExprNext(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid)
		**(**int32)(__ccgo_up(pCsr + 60)) |= _sqlite3Fts5ExprEof(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr)
		_fts5CsrNewrow(tls, pCsr)
	} else {
		switch (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan {
		case int32(FTS5_PLAN_SPECIAL):
			**(**int32)(__ccgo_up(pCsr + 60)) |= int32(FTS5CSR_EOF)
			rc = SQLITE_OK
		case int32(FTS5_PLAN_SORTED_MATCH):
			rc = _fts5SorterNext(tls, pCsr)
		default:
			pConfig = (*TFts5Table)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab)).FpConfig
			(*TFts5Config)(unsafe.Pointer(pConfig)).FbLock = (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock + 1
			rc = Xsqlite3_step(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt)
			(*TFts5Config)(unsafe.Pointer(pConfig)).FbLock = (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock - 1
			if rc != int32(SQLITE_ROW) {
				**(**int32)(__ccgo_up(pCsr + 60)) |= int32(FTS5CSR_EOF)
				rc = Xsqlite3_reset(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt)
				if rc != SQLITE_OK {
					(*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+16, Xsqlite3_errmsg(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb)))
				}
			} else {
				rc = SQLITE_OK
				**(**int32)(__ccgo_up(pCsr + 60)) |= int32(FTS5CSR_REQUIRE_DOCSIZE)
			}
			break
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Implementation of xOpen method.
//	*/
func _fts5OpenMethod(tls *libc.TLS, pVTab uintptr, ppCsr uintptr) (r int32) {
	var nByte Tsqlite3_int64
	var pConfig, pCsr, pGlobal, pTab, v2 uintptr
	var rc int32
	var v1 Ti64
	_, _, _, _, _, _, _, _ = nByte, pConfig, pCsr, pGlobal, pTab, rc, v1, v2
	pTab = pVTab
	pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig
	pCsr = uintptr(0) /* Return code */
	rc = _fts5NewTransaction(tls, pTab)
	if rc == SQLITE_OK {
		nByte = int64(uint32(120) + uint32((*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)*uint32(4))
		pCsr = Xsqlite3_malloc64(tls, uint64(nByte))
		if pCsr != 0 {
			pGlobal = (*TFts5FullTable)(unsafe.Pointer(pTab)).FpGlobal
			libc.Xmemset(tls, pCsr, 0, uint32(nByte))
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FaColumnSize = pCsr + 1*120
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FpNext = (*TFts5Global)(unsafe.Pointer(pGlobal)).FpCsr
			(*TFts5Global)(unsafe.Pointer(pGlobal)).FpCsr = pCsr
			v2 = pGlobal + 32
			*(*Ti64)(unsafe.Pointer(v2)) = *(*Ti64)(unsafe.Pointer(v2)) + 1
			v1 = *(*Ti64)(unsafe.Pointer(v2))
			(*TFts5Cursor)(unsafe.Pointer(pCsr)).FiCsrId = v1
		} else {
			rc = int32(SQLITE_NOMEM)
		}
	}
	**(**uintptr)(__ccgo_up(ppCsr)) = pCsr
	return rc
}

// C documentation
//
//	/*
//	** The second argument passed to this function may be NULL, or it may be
//	** an existing Fts5Colset object. This function returns a pointer to
//	** a new colset object containing the contents of (p) with new value column
//	** number iCol appended.
//	**
//	** If an OOM error occurs, store an error code in pParse and return NULL.
//	** The old colset object (if any) is not freed in this case.
//	*/
func _fts5ParseColset(tls *libc.TLS, pParse uintptr, p uintptr, iCol int32) (r uintptr) {
	var aiCol, pNew uintptr
	var i, j, nCol, v1 int32
	_, _, _, _, _, _ = aiCol, i, j, nCol, pNew, v1
	if p != 0 {
		v1 = (*TFts5Colset)(unsafe.Pointer(p)).FnCol
	} else {
		v1 = 0
	}
	nCol = v1 /* New colset object to return */
	pNew = Xsqlite3_realloc64(tls, p, uint64(libc.Uint32FromInt64(8)*uint32((nCol+libc.Int32FromInt32(1)+libc.Int32FromInt32(2))/libc.Int32FromInt32(2))))
	if pNew == uintptr(0) {
		(*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM)
	} else {
		aiCol = pNew + 4
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			if **(**int32)(__ccgo_up(aiCol + uintptr(i)*4)) == iCol {
				return pNew
			}
			if **(**int32)(__ccgo_up(aiCol + uintptr(i)*4)) > iCol {
				break
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		j = nCol
		for {
			if !(j > i) {
				break
			}
			**(**int32)(__ccgo_up(aiCol + uintptr(j)*4)) = **(**int32)(__ccgo_up(aiCol + uintptr(j-int32(1))*4))
			goto _3
		_3:
			;
			j = j - 1
		}
		**(**int32)(__ccgo_up(aiCol + uintptr(i)*4)) = iCol
		(*TFts5Colset)(unsafe.Pointer(pNew)).FnCol = nCol + int32(1)
	}
	return pNew
}

// C documentation
//
//	/*
//	** This function is used when parsing LIKE or GLOB patterns against
//	** trigram indexes that specify either detail=column or detail=none.
//	** It converts a phrase:
//	**
//	**     abc + def + ghi
//	**
//	** into an AND tree:
//	**
//	**     abc AND def AND ghi
//	*/
func _fts5ParsePhraseToAnd(tls *libc.TLS, pParse uintptr, pNear uintptr) (r uintptr) {
	var ii, nByte, nTerm, v2 int32
	var p, pPhrase, pRet, pTo, v3 uintptr
	_, _, _, _, _, _, _, _, _ = ii, nByte, nTerm, p, pPhrase, pRet, pTo, v2, v3
	nTerm = (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)))).FnTerm
	nByte = int32(uint32(libc.UintptrFromInt32(0)+40) + uint32(nTerm+libc.Int32FromInt32(1))*libc.Uint32FromInt64(4))
	pRet = _sqlite3Fts5MallocZero(tls, pParse+8, int64(nByte))
	if pRet != 0 {
		(*TFts5ExprNode)(unsafe.Pointer(pRet)).FeType = int32(FTS5_AND)
		(*TFts5ExprNode)(unsafe.Pointer(pRet)).FnChild = nTerm
		(*TFts5ExprNode)(unsafe.Pointer(pRet)).FiHeight = int32(1)
		_fts5ExprAssignXNext(tls, pRet)
		(*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase = (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase - 1
		ii = 0
		for {
			if !(ii < nTerm) {
				break
			}
			pPhrase = _sqlite3Fts5MallocZero(tls, pParse+8, int64(uint32(libc.UintptrFromInt32(0)+20)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(24)))
			if pPhrase != 0 {
				if _parseGrowPhraseArray(tls, pParse) != 0 {
					_fts5ExprPhraseFree(tls, pPhrase)
				} else {
					p = *(*uintptr)(unsafe.Pointer(pNear + 12)) + 20 + uintptr(ii)*24
					pTo = pPhrase + 20
					v3 = pParse + 12
					v2 = *(*int32)(unsafe.Pointer(v3))
					*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
					**(**uintptr)(__ccgo_up((*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr(v2)*4)) = pPhrase
					(*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm = int32(1)
					(*TFts5ExprTerm)(unsafe.Pointer(pTo)).FpTerm = _sqlite3Fts5Strndup(tls, pParse+8, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpTerm, (*TFts5ExprTerm)(unsafe.Pointer(p)).FnFullTerm)
					(*TFts5ExprTerm)(unsafe.Pointer(pTo)).FnQueryTerm = (*TFts5ExprTerm)(unsafe.Pointer(p)).FnQueryTerm
					(*TFts5ExprTerm)(unsafe.Pointer(pTo)).FnFullTerm = (*TFts5ExprTerm)(unsafe.Pointer(p)).FnFullTerm
					*(*uintptr)(unsafe.Pointer(pRet + 40 + uintptr(ii)*4)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_STRING), uintptr(0), uintptr(0), _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), pPhrase))
				}
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
		if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc != 0 {
			_sqlite3Fts5ParseNodeFree(tls, pRet)
			pRet = uintptr(0)
		} else {
			_sqlite3Fts5ParseNearsetFree(tls, pNear)
		}
	}
	return pRet
}

// C documentation
//
//	/*
//	** Callback for tokenizing terms used by ParseTerm().
//	*/
func _fts5ParseTokenize(tls *libc.TLS, pContext uintptr, tflags int32, pToken uintptr, nToken int32, iUnused1 int32, iUnused2 int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var SZALLOC, nNew, v1 int32
	var nByte Tsqlite3_int64
	var pCtx, pNew, pPhrase, pSyn, pTerm, v3 uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _ = SZALLOC, nByte, nNew, pCtx, pNew, pPhrase, pSyn, pTerm, v1, v3
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	SZALLOC = int32(8)
	pCtx = pContext
	pPhrase = (*TTokenCtx)(unsafe.Pointer(pCtx)).FpPhrase
	_ = iUnused1
	_ = iUnused2
	/* If an error has already occurred, this is a no-op */
	if (*TTokenCtx)(unsafe.Pointer(pCtx)).Frc != SQLITE_OK {
		return (*TTokenCtx)(unsafe.Pointer(pCtx)).Frc
	}
	if nToken > int32(FTS5_MAX_TOKEN_SIZE) {
		nToken = int32(FTS5_MAX_TOKEN_SIZE)
	}
	if pPhrase != 0 && (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm > 0 && tflags&int32(FTS5_TOKEN_COLOCATED) != 0 {
		nByte = int64(libc.Uint32FromInt64(24) + libc.Uint32FromInt64(12) + uint32(nToken) + uint32(1))
		pSyn = Xsqlite3_malloc64(tls, uint64(nByte))
		if pSyn == uintptr(0) {
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
		} else {
			libc.Xmemset(tls, pSyn, 0, uint32(nByte))
			(*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpTerm = pSyn + uintptr(24) + uintptr(12)
			v1 = nToken
			(*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FnQueryTerm = v1
			(*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FnFullTerm = v1
			libc.Xmemcpy(tls, (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpTerm, pToken, uint32(nToken))
			if (*TFts5Config)(unsafe.Pointer((*TTokenCtx)(unsafe.Pointer(pCtx)).FpConfig)).FbTokendata != 0 {
				(*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FnQueryTerm = int32(libc.Xstrlen(tls, (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpTerm))
			}
			(*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpSynonym = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20 + uintptr((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm-int32(1))*24))).FpSynonym
			(*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20 + uintptr((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm-int32(1))*24))).FpSynonym = pSyn
		}
	} else {
		if pPhrase == uintptr(0) || (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm%SZALLOC == 0 {
			if pPhrase != 0 {
				v1 = (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm
			} else {
				v1 = 0
			}
			nNew = SZALLOC + v1
			pNew = Xsqlite3_realloc64(tls, pPhrase, uint64(uint32(libc.UintptrFromInt32(0)+20)+uint32(nNew+libc.Int32FromInt32(1))*libc.Uint32FromInt64(24)))
			if pNew == uintptr(0) {
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
			} else {
				if pPhrase == uintptr(0) {
					libc.Xmemset(tls, pNew, 0, uint32(libc.UintptrFromInt32(0)+20)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(24))
				}
				v3 = pNew
				pPhrase = v3
				(*TTokenCtx)(unsafe.Pointer(pCtx)).FpPhrase = v3
				(*TFts5ExprPhrase)(unsafe.Pointer(pNew)).FnTerm = nNew - SZALLOC
			}
		}
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			v3 = pPhrase + 16
			v1 = *(*int32)(unsafe.Pointer(v3))
			*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
			pTerm = pPhrase + 20 + uintptr(v1)*24
			libc.Xmemset(tls, pTerm, 0, uint32(24))
			(*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpTerm = _sqlite3Fts5Strndup(tls, bp, pToken, nToken)
			v1 = nToken
			(*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FnQueryTerm = v1
			(*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FnFullTerm = v1
			if (*TFts5Config)(unsafe.Pointer((*TTokenCtx)(unsafe.Pointer(pCtx)).FpConfig)).FbTokendata != 0 && **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				(*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FnQueryTerm = int32(libc.Xstrlen(tls, (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpTerm))
			}
		}
	}
	(*TTokenCtx)(unsafe.Pointer(pCtx)).Frc = **(**int32)(__ccgo_up(bp))
	return **(**int32)(__ccgo_up(bp))
}

func _fts5PorterCb(tls *libc.TLS, pCtx uintptr, tflags int32, pToken uintptr, nToken int32, iStart int32, iEnd int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aBuf, p uintptr
	var c int8
	var v1 int32
	var _ /* nBuf at bp+0 */ int32
	_, _, _, _ = aBuf, c, p, v1
	p = pCtx
	if nToken > int32(FTS5_PORTER_MAX_TOKEN) || nToken < int32(3) {
		goto pass_through
	}
	aBuf = (*TPorterContext)(unsafe.Pointer(p)).FaBuf
	**(**int32)(__ccgo_up(bp)) = nToken
	libc.Xmemcpy(tls, aBuf, pToken, uint32(**(**int32)(__ccgo_up(bp))))
	/* Step 1. */
	_fts5PorterStep1A(tls, aBuf, bp)
	if _fts5PorterStep1B(tls, aBuf, bp) != 0 {
		if _fts5PorterStep1B2(tls, aBuf, bp) == 0 {
			c = **(**int8)(__ccgo_up(aBuf + uintptr(**(**int32)(__ccgo_up(bp))-int32(1))))
			if _fts5PorterIsVowel(tls, c, 0) == 0 && int32(c) != int32('l') && int32(c) != int32('s') && int32(c) != int32('z') && int32(c) == int32(**(**int8)(__ccgo_up(aBuf + uintptr(**(**int32)(__ccgo_up(bp))-int32(2))))) {
				**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) - 1
			} else {
				if _fts5Porter_MEq1(tls, aBuf, **(**int32)(__ccgo_up(bp))) != 0 && _fts5Porter_Ostar(tls, aBuf, **(**int32)(__ccgo_up(bp))) != 0 {
					v1 = **(**int32)(__ccgo_up(bp))
					**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + 1
					**(**int8)(__ccgo_up(aBuf + uintptr(v1))) = int8('e')
				}
			}
		}
	}
	/* Step 1C. */
	if int32(**(**int8)(__ccgo_up(aBuf + uintptr(**(**int32)(__ccgo_up(bp))-int32(1))))) == int32('y') && _fts5Porter_Vowel(tls, aBuf, **(**int32)(__ccgo_up(bp))-int32(1)) != 0 {
		**(**int8)(__ccgo_up(aBuf + uintptr(**(**int32)(__ccgo_up(bp))-int32(1)))) = int8('i')
	}
	/* Steps 2 through 4. */
	_fts5PorterStep2(tls, aBuf, bp)
	_fts5PorterStep3(tls, aBuf, bp)
	_fts5PorterStep4(tls, aBuf, bp)
	/* Step 5a. */
	if int32(**(**int8)(__ccgo_up(aBuf + uintptr(**(**int32)(__ccgo_up(bp))-int32(1))))) == int32('e') {
		if _fts5Porter_MGt1(tls, aBuf, **(**int32)(__ccgo_up(bp))-int32(1)) != 0 || _fts5Porter_MEq1(tls, aBuf, **(**int32)(__ccgo_up(bp))-int32(1)) != 0 && !(_fts5Porter_Ostar(tls, aBuf, **(**int32)(__ccgo_up(bp))-int32(1)) != 0) {
			**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) - 1
		}
	}
	/* Step 5b. */
	if **(**int32)(__ccgo_up(bp)) > int32(1) && int32(**(**int8)(__ccgo_up(aBuf + uintptr(**(**int32)(__ccgo_up(bp))-int32(1))))) == int32('l') && int32(**(**int8)(__ccgo_up(aBuf + uintptr(**(**int32)(__ccgo_up(bp))-int32(2))))) == int32('l') && _fts5Porter_MGt1(tls, aBuf, **(**int32)(__ccgo_up(bp))-int32(1)) != 0 {
		**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) - 1
	}
	return (*(*func(*libc.TLS, uintptr, int32, uintptr, int32, int32, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TPorterContext)(unsafe.Pointer(p)).FxToken})))(tls, (*TPorterContext)(unsafe.Pointer(p)).FpCtx, tflags, aBuf, **(**int32)(__ccgo_up(bp)), iStart, iEnd)
	goto pass_through
pass_through:
	;
	return (*(*func(*libc.TLS, uintptr, int32, uintptr, int32, int32, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TPorterContext)(unsafe.Pointer(p)).FxToken})))(tls, (*TPorterContext)(unsafe.Pointer(p)).FpCtx, tflags, pToken, nToken, iStart, iEnd)
	return r
}

// C documentation
//
//	/*
//	** Create a "porter" tokenizer.
//	*/
func _fts5PorterCreate(tls *libc.TLS, pCtx uintptr, azArg uintptr, nArg int32, ppOut uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var az2, pApi, pRet, zBase, v2 uintptr
	var nArg2, rc, v1 int32
	var _ /* pUserdata at bp+0 */ uintptr
	var _ /* pV2 at bp+4 */ uintptr
	_, _, _, _, _, _, _, _ = az2, nArg2, pApi, pRet, rc, zBase, v1, v2
	pApi = pCtx
	rc = SQLITE_OK
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	zBase = __ccgo_ts + 43131
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	for nArg > 0 {
		if Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg)), __ccgo_ts+43141) == 0 {
			nArg = nArg - 1
			azArg += 4
		} else {
			zBase = **(**uintptr)(__ccgo_up(azArg))
			break
		}
	}
	pRet = Xsqlite3_malloc64(tls, uint64(148))
	if pRet != 0 {
		libc.Xmemset(tls, pRet, 0, uint32(148))
		rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tfts5_api)(unsafe.Pointer(pApi)).FxFindTokenizer_v2})))(tls, pApi, zBase, bp, bp+4)
	} else {
		rc = int32(SQLITE_NOMEM)
	}
	if rc == SQLITE_OK {
		if nArg > 0 {
			v1 = nArg - int32(1)
		} else {
			v1 = 0
		}
		nArg2 = v1
		if nArg2 != 0 {
			v2 = azArg + 1*4
		} else {
			v2 = uintptr(0)
		}
		az2 = v2
		libc.Xmemcpy(tls, pRet, **(**uintptr)(__ccgo_up(bp + 4)), uint32(16))
		rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TPorterTokenizer)(unsafe.Pointer(pRet)).Ftokenizer_v2.FxCreate})))(tls, **(**uintptr)(__ccgo_up(bp)), az2, nArg2, pRet+16)
	}
	if rc != SQLITE_OK {
		_fts5PorterDelete(tls, pRet)
		pRet = uintptr(0)
	}
	**(**uintptr)(__ccgo_up(ppOut)) = pRet
	return rc
}

func _fts5PorterStep1B(tls *libc.TLS, aBuf uintptr, pnBuf uintptr) (r int32) {
	var nBuf, ret int32
	_, _ = nBuf, ret
	ret = 0
	nBuf = **(**int32)(__ccgo_up(pnBuf))
	switch int32(**(**int8)(__ccgo_up(aBuf + uintptr(nBuf-int32(2))))) {
	case int32('e'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43413, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(3)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(3)), __ccgo_ts+43417, uint32(2))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3) + int32(2)
			}
		} else {
			if nBuf > int32(2) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43420, aBuf+uintptr(nBuf-int32(2)), uint32(2)) {
				if _fts5Porter_Vowel(tls, aBuf, nBuf-int32(2)) != 0 {
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(2)
					ret = int32(1)
				}
			}
		}
	case int32('n'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43423, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_Vowel(tls, aBuf, nBuf-int32(3)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
				ret = int32(1)
			}
		}
		break
	}
	return ret
}

/*
** GENERATED CODE ENDS HERE (mkportersteps.tcl)
***************************************************************************
**************************************************************************/

func _fts5PorterStep1B2(tls *libc.TLS, aBuf uintptr, pnBuf uintptr) (r int32) {
	var nBuf, ret int32
	_, _ = nBuf, ret
	ret = 0
	nBuf = **(**int32)(__ccgo_up(pnBuf))
	switch int32(**(**int8)(__ccgo_up(aBuf + uintptr(nBuf-int32(2))))) {
	case int32('a'):
		if nBuf > int32(2) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43227, aBuf+uintptr(nBuf-int32(2)), uint32(2)) {
			libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(2)), __ccgo_ts+43207, uint32(3))
			**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(2) + int32(3)
			ret = int32(1)
		}
	case int32('b'):
		if nBuf > int32(2) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43230, aBuf+uintptr(nBuf-int32(2)), uint32(2)) {
			libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(2)), __ccgo_ts+43233, uint32(3))
			**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(2) + int32(3)
			ret = int32(1)
		}
	case int32('i'):
		if nBuf > int32(2) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43237, aBuf+uintptr(nBuf-int32(2)), uint32(2)) {
			libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(2)), __ccgo_ts+43223, uint32(3))
			**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(2) + int32(3)
			ret = int32(1)
		}
		break
	}
	return ret
}

func _fts5PorterStep2(tls *libc.TLS, aBuf uintptr, pnBuf uintptr) (r int32) {
	var nBuf, ret int32
	_, _ = nBuf, ret
	ret = 0
	nBuf = **(**int32)(__ccgo_up(pnBuf))
	switch int32(**(**int8)(__ccgo_up(aBuf + uintptr(nBuf-int32(2))))) {
	case int32('a'):
		if nBuf > int32(7) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43240, aBuf+uintptr(nBuf-int32(7)), uint32(7)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(7)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(7)), __ccgo_ts+43207, uint32(3))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(7) + int32(3)
			}
		} else {
			if nBuf > int32(6) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43248, aBuf+uintptr(nBuf-int32(6)), uint32(6)) {
				if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(6)) != 0 {
					libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(6)), __ccgo_ts+43255, uint32(4))
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(6) + int32(4)
				}
			}
		}
	case int32('c'):
		if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43260, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(4)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(4)), __ccgo_ts+43156, uint32(4))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4) + int32(4)
			}
		} else {
			if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43265, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
				if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(4)) != 0 {
					libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(4)), __ccgo_ts+43151, uint32(4))
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4) + int32(4)
				}
			}
		}
	case int32('e'):
		if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43270, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(4)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(4)), __ccgo_ts+43223, uint32(3))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4) + int32(3)
			}
		}
	case int32('g'):
		if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43275, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(4)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(4)), __ccgo_ts+18387, uint32(3))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4) + int32(3)
			}
		}
	case int32('l'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43280, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(3)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(3)), __ccgo_ts+43233, uint32(3))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3) + int32(3)
			}
		} else {
			if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43284, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
				if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(4)) != 0 {
					libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(4)), __ccgo_ts+43148, uint32(2))
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4) + int32(2)
				}
			} else {
				if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43289, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
					if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
						libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(5)), __ccgo_ts+43192, uint32(3))
						**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5) + int32(3)
					}
				} else {
					if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43295, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
						if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(3)) != 0 {
							libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(3)), __ccgo_ts+43299, uint32(1))
							**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3) + int32(1)
						}
					} else {
						if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43301, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
							if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
								libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(5)), __ccgo_ts+43215, uint32(3))
								**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5) + int32(3)
							}
						}
					}
				}
			}
		}
	case int32('o'):
		if nBuf > int32(7) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43307, aBuf+uintptr(nBuf-int32(7)), uint32(7)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(7)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(7)), __ccgo_ts+43223, uint32(3))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(7) + int32(3)
			}
		} else {
			if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43315, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
				if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
					libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(5)), __ccgo_ts+43207, uint32(3))
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5) + int32(3)
				}
			} else {
				if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43321, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
					if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(4)) != 0 {
						libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(4)), __ccgo_ts+43207, uint32(3))
						**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4) + int32(3)
					}
				}
			}
		}
	case int32('s'):
		if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43326, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(5)), __ccgo_ts+43148, uint32(2))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5) + int32(2)
			}
		} else {
			if nBuf > int32(7) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43332, aBuf+uintptr(nBuf-int32(7)), uint32(7)) {
				if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(7)) != 0 {
					libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(7)), __ccgo_ts+43219, uint32(3))
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(7) + int32(3)
				}
			} else {
				if nBuf > int32(7) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43340, aBuf+uintptr(nBuf-int32(7)), uint32(7)) {
					if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(7)) != 0 {
						libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(7)), __ccgo_ts+43348, uint32(3))
						**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(7) + int32(3)
					}
				} else {
					if nBuf > int32(7) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43352, aBuf+uintptr(nBuf-int32(7)), uint32(7)) {
						if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(7)) != 0 {
							libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(7)), __ccgo_ts+43215, uint32(3))
							**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(7) + int32(3)
						}
					}
				}
			}
		}
	case int32('t'):
		if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43360, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(5)), __ccgo_ts+43148, uint32(2))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5) + int32(2)
			}
		} else {
			if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43366, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
				if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
					libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(5)), __ccgo_ts+43219, uint32(3))
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5) + int32(3)
				}
			} else {
				if nBuf > int32(6) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43372, aBuf+uintptr(nBuf-int32(6)), uint32(6)) {
					if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(6)) != 0 {
						libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(6)), __ccgo_ts+43233, uint32(3))
						**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(6) + int32(3)
					}
				}
			}
		}
		break
	}
	return ret
}

func _fts5PorterStep3(tls *libc.TLS, aBuf uintptr, pnBuf uintptr) (r int32) {
	var nBuf, ret int32
	_, _ = nBuf, ret
	ret = 0
	nBuf = **(**int32)(__ccgo_up(pnBuf))
	switch int32(**(**int8)(__ccgo_up(aBuf + uintptr(nBuf-int32(2))))) {
	case int32('a'):
		if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43379, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(4)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(4)), __ccgo_ts+43164, uint32(2))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4) + int32(2)
			}
		}
	case int32('s'):
		if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43384, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(4)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4)
			}
		}
	case int32('t'):
		if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43389, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(5)), __ccgo_ts+43164, uint32(2))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5) + int32(2)
			}
		} else {
			if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43395, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
				if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
					libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(5)), __ccgo_ts+43164, uint32(2))
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5) + int32(2)
				}
			}
		}
	case int32('u'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43348, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(3)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
			}
		}
	case int32('v'):
		if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43401, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5)
			}
		}
	case int32('z'):
		if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43407, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
			if _fts5Porter_MGt0(tls, aBuf, nBuf-int32(5)) != 0 {
				libc.Xmemcpy(tls, aBuf+uintptr(nBuf-int32(5)), __ccgo_ts+43148, uint32(2))
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5) + int32(2)
			}
		}
		break
	}
	return ret
}

func _fts5PorterStep4(tls *libc.TLS, aBuf uintptr, pnBuf uintptr) (r int32) {
	var nBuf, ret int32
	_, _ = nBuf, ret
	ret = 0
	nBuf = **(**int32)(__ccgo_up(pnBuf))
	switch int32(**(**int8)(__ccgo_up(aBuf + uintptr(nBuf-int32(2))))) {
	case int32('a'):
		if nBuf > int32(2) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43148, aBuf+uintptr(nBuf-int32(2)), uint32(2)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(2)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(2)
			}
		}
	case int32('c'):
		if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43151, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(4)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4)
			}
		} else {
			if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43156, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
				if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(4)) != 0 {
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4)
				}
			}
		}
	case int32('e'):
		if nBuf > int32(2) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43161, aBuf+uintptr(nBuf-int32(2)), uint32(2)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(2)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(2)
			}
		}
	case int32('i'):
		if nBuf > int32(2) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43164, aBuf+uintptr(nBuf-int32(2)), uint32(2)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(2)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(2)
			}
		}
	case int32('l'):
		if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43167, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(4)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4)
			}
		} else {
			if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43172, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
				if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(4)) != 0 {
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4)
				}
			}
		}
	case int32('n'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43177, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(3)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
			}
		} else {
			if nBuf > int32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43181, aBuf+uintptr(nBuf-int32(5)), uint32(5)) {
				if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(5)) != 0 {
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(5)
				}
			} else {
				if nBuf > int32(4) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43187, aBuf+uintptr(nBuf-int32(4)), uint32(4)) {
					if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(4)) != 0 {
						**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(4)
					}
				} else {
					if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43192, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
						if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(3)) != 0 {
							**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
						}
					}
				}
			}
		}
	case int32('o'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43196, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt1_and_S_or_T(tls, aBuf, nBuf-int32(3)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
			}
		} else {
			if nBuf > int32(2) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43200, aBuf+uintptr(nBuf-int32(2)), uint32(2)) {
				if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(2)) != 0 {
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(2)
				}
			}
		}
	case int32('s'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43203, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(3)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
			}
		}
	case int32('t'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43207, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(3)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
			}
		} else {
			if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43211, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
				if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(3)) != 0 {
					**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
				}
			}
		}
	case int32('u'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43215, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(3)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
			}
		}
	case int32('v'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43219, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(3)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
			}
		}
	case int32('z'):
		if nBuf > int32(3) && 0 == libc.Xmemcmp(tls, __ccgo_ts+43223, aBuf+uintptr(nBuf-int32(3)), uint32(3)) {
			if _fts5Porter_MGt1(tls, aBuf, nBuf-int32(3)) != 0 {
				**(**int32)(__ccgo_up(pnBuf)) = nBuf - int32(3)
			}
		}
		break
	}
	return ret
}

// C documentation
//
//	/*
//	** Return a "position-list blob" corresponding to the current position of
//	** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
//	** the current position-list for each phrase in the query associated with
//	** cursor pCsr.
//	**
//	** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
//	** the number of phrases in the query. Following the varints are the
//	** concatenated position lists for each phrase, in order.
//	**
//	** The first varint (if it exists) contains the size of the position list
//	** for phrase 0. The second (same disclaimer) contains the size of position
//	** list 1. And so on. There is no size field for the final position list,
//	** as it can be derived from the total size of the blob.
//	*/
func _fts5PoslistBlob(tls *libc.TLS, pCtx uintptr, pCsr uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i, nByte, nPhrase, nPoslist int32
	var _ /* dummy at bp+16 */ uintptr
	var _ /* dummy at bp+24 */ uintptr
	var _ /* nByte at bp+28 */ int32
	var _ /* nPoslist at bp+36 */ int32
	var _ /* pPoslist at bp+20 */ uintptr
	var _ /* pPoslist at bp+32 */ uintptr
	var _ /* rc at bp+0 */ int32
	var _ /* val at bp+4 */ TFts5Buffer
	_, _, _, _ = i, nByte, nPhrase, nPoslist
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	nPhrase = _sqlite3Fts5ExprPhraseCount(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr)
	libc.Xmemset(tls, bp+4, 0, uint32(12))
	switch (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer((*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab)).FpConfig)).FeDetail {
	case FTS5_DETAIL_FULL:
		goto _1
	case int32(FTS5_DETAIL_COLUMNS):
		goto _2
	default:
		goto _3
	}
	goto _4
_1:
	;
	/* Append the varints */
	i = 0
_7:
	;
	if !(i < nPhrase-int32(1)) {
		goto _5
	}
	nByte = _sqlite3Fts5ExprPoslist(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr, i, bp+16)
	_sqlite3Fts5BufferAppendVarint(tls, bp, bp+4, int64(nByte))
	goto _6
_6:
	;
	i = i + 1
	goto _7
	goto _5
_5:
	;
	/* Append the position lists */
	i = 0
	for {
		if !(i < nPhrase) {
			break
		}
		nPoslist = _sqlite3Fts5ExprPoslist(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr, i, bp+20)
		_sqlite3Fts5BufferAppendBlob(tls, bp, bp+4, uint32(nPoslist), **(**uintptr)(__ccgo_up(bp + 20)))
		goto _8
	_8:
		;
		i = i + 1
	}
	goto _4
_2:
	;
	/* Append the varints */
	i = 0
	for {
		if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && i < nPhrase-int32(1)) {
			break
		}
		**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5ExprPhraseCollist(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr, i, bp+24, bp+28)
		_sqlite3Fts5BufferAppendVarint(tls, bp, bp+4, int64(**(**int32)(__ccgo_up(bp + 28))))
		goto _9
	_9:
		;
		i = i + 1
	}
	/* Append the position lists */
	i = 0
	for {
		if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && i < nPhrase) {
			break
		}
		**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5ExprPhraseCollist(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr, i, bp+32, bp+36)
		_sqlite3Fts5BufferAppendBlob(tls, bp, bp+4, uint32(**(**int32)(__ccgo_up(bp + 36))), **(**uintptr)(__ccgo_up(bp + 32)))
		goto _10
	_10:
		;
		i = i + 1
	}
	goto _4
_3:
	;
	goto _4
_4:
	;
	Xsqlite3_result_blob(tls, pCtx, (**(**TFts5Buffer)(__ccgo_up(bp + 4))).Fp, (**(**TFts5Buffer)(__ccgo_up(bp + 4))).Fn, __ccgo_fp(Xsqlite3_free))
	return **(**int32)(__ccgo_up(bp))
}

func _fts5PoslistCallback(tls *libc.TLS, pUnused uintptr, pContext uintptr, pChunk uintptr, nChunk int32) {
	_ = pUnused
	if nChunk > 0 {
		libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pContext)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pContext)).Fn), pChunk, uint32(nChunk))
		**(**int32)(__ccgo_up(pContext + 4)) += nChunk
	}
}

func _fts5PoslistFilterCallback(tls *libc.TLS, pUnused uintptr, pContext uintptr, pChunk uintptr, nChunk int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, iEnd, iStart, v1 int32
	var pCtx uintptr
	var _ /* iCol at bp+0 */ int32
	var _ /* iCol at bp+4 */ int32
	_, _, _, _, _ = i, iEnd, iStart, pCtx, v1
	pCtx = pContext
	_ = pUnused
	if nChunk > 0 {
		/* Search through to find the first varint with value 1. This is the
		 ** start of the next columns hits. */
		i = 0
		iStart = 0
		if (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState == int32(2) {
			v1 = i
			i = i + 1
			**(**int32)(__ccgo_up(bp)) = int32(**(**Tu8)(__ccgo_up(pChunk + uintptr(v1))))
			if **(**int32)(__ccgo_up(bp))&int32(0x80) != 0 {
				i = i - 1
				i = i + _sqlite3Fts5GetVarint32(tls, pChunk+uintptr(i), bp)
			}
			if _fts5IndexColsetTest(tls, (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpColset, **(**int32)(__ccgo_up(bp))) != 0 {
				(*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState = int32(1)
				**(**int32)(__ccgo_up((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), uint64(libc.Int32FromInt32(1)))
			} else {
				(*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState = 0
			}
		}
		for cond := true; cond; cond = i < nChunk {
			for i < nChunk && int32(**(**Tu8)(__ccgo_up(pChunk + uintptr(i)))) != int32(0x01) {
				iEnd = i + int32(9)
				for {
					v1 = i
					i = i + 1
					if !(int32(**(**Tu8)(__ccgo_up(pChunk + uintptr(v1))))&int32(0x80) != 0 && i < iEnd) {
						break
					}
				}
			}
			if (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState != 0 {
				libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), pChunk+uintptr(iStart), uint32(i-iStart))
				**(**int32)(__ccgo_up((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf + 4)) += i - iStart
			}
			if i < nChunk {
				iStart = i
				i = i + 1
				if i >= nChunk {
					(*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState = int32(2)
				} else {
					v1 = i
					i = i + 1
					**(**int32)(__ccgo_up(bp + 4)) = int32(**(**Tu8)(__ccgo_up(pChunk + uintptr(v1))))
					if **(**int32)(__ccgo_up(bp + 4))&int32(0x80) != 0 {
						i = i - 1
						i = i + _sqlite3Fts5GetVarint32(tls, pChunk+uintptr(i), bp+4)
					}
					(*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState = _fts5IndexColsetTest(tls, (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpColset, **(**int32)(__ccgo_up(bp + 4)))
					if (*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FeState != 0 {
						libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), pChunk+uintptr(iStart), uint32(i-iStart))
						**(**int32)(__ccgo_up((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf + 4)) += i - iStart
						iStart = i
					}
				}
			}
		}
	}
}

func _fts5PoslistOffsetsCallback(tls *libc.TLS, pUnused uintptr, pContext uintptr, pChunk uintptr, nChunk int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i int32
	var pCtx uintptr
	var _ /* iVal at bp+0 */ int32
	_, _ = i, pCtx
	pCtx = pContext
	_ = pUnused
	if nChunk > 0 {
		i = 0
		for i < nChunk {
			i = i + _sqlite3Fts5GetVarint32(tls, pChunk+uintptr(i), bp)
			**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + ((*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FiRead - int32(2))
			(*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FiRead = **(**int32)(__ccgo_up(bp))
			if _fts5IndexColsetTest(tls, (*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FpColset, **(**int32)(__ccgo_up(bp))) != 0 {
				**(**int32)(__ccgo_up((*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FpBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), uint64(**(**int32)(__ccgo_up(bp))+libc.Int32FromInt32(2)-(*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FiWrite))
				(*TPoslistOffsetsCtx)(unsafe.Pointer(pCtx)).FiWrite = **(**int32)(__ccgo_up(bp))
			}
		}
	}
}

// C documentation
//
//	/*
//	** Execute the SQL statement:
//	**
//	**    DELETE FROM %_idx WHERE (segid, (pgno/2)) = ($iSegid, $iPgno);
//	**
//	** This is used when a secure-delete operation removes the last term
//	** from a segment leaf page. In that case the %_idx entry is removed
//	** too. This is done to ensure that if all instances of a token are
//	** removed from an fts5 database in secure-delete mode, no trace of
//	** the token itself remains in the database.
//	*/
func _fts5SecureDeleteIdxEntry(tls *libc.TLS, p uintptr, iSegid int32, iPgno int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	if iPgno != int32(1) {
		if (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx == uintptr(0) {
			_fts5IndexPrepareStmt(tls, p, p+84, Xsqlite3_mprintf(tls, __ccgo_ts+40622, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FzDb, (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FzName)))
		}
		if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			Xsqlite3_bind_int(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx, int32(1), iSegid)
			Xsqlite3_bind_int(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx, int32(2), iPgno)
			Xsqlite3_step(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx)
			(*TFts5Index)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx)
		}
	}
}

// C documentation
//
//	/*
//	** This is called when a secure-delete operation removes a position-list
//	** that overflows onto segment page iPgno of segment pSeg. This function
//	** rewrites node iPgno, and possibly one or more of its right-hand peers,
//	** to remove this portion of the position list.
//	**
//	** Output variable (*pbLastInDoclist) is set to true if the position-list
//	** removed is followed by a new term or the end-of-segment, or false if
//	** it is followed by another rowid/position list.
//	*/
func _fts5SecureDeleteOverflow(tls *libc.TLS, p uintptr, pSeg uintptr, iPgno int32, pbLastInDoclist uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aIdx, aPg, pLeaf uintptr
	var bDetailNone, i1, i2, nIdx, nPg, nShift, pgno int32
	var iRowid Ti64
	var _ /* aEmpty at bp+4 */ [4]Tu8
	var _ /* iFirst at bp+8 */ int32
	var _ /* iNext at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _ = aIdx, aPg, bDetailNone, i1, i2, iRowid, nIdx, nPg, nShift, pLeaf, pgno
	bDetailNone = libc.BoolInt32((*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == libc.Int32FromInt32(FTS5_DETAIL_NONE))
	pLeaf = uintptr(0)
	**(**int32)(__ccgo_up(pbLastInDoclist)) = int32(1)
	pgno = iPgno
	for {
		if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && pgno <= (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast) {
			break
		}
		iRowid = int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(pgno)
		**(**int32)(__ccgo_up(bp)) = 0
		aPg = uintptr(0)
		pLeaf = _fts5DataRead(tls, p, iRowid)
		if pLeaf == uintptr(0) {
			break
		}
		aPg = (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp
		**(**int32)(__ccgo_up(bp)) = int32(_fts5GetU16(tls, aPg))
		if **(**int32)(__ccgo_up(bp)) != 0 {
			**(**int32)(__ccgo_up(pbLastInDoclist)) = 0
		}
		if **(**int32)(__ccgo_up(bp)) == 0 && (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf != (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn {
			_sqlite3Fts5GetVarint32(tls, aPg+uintptr((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf), bp)
		}
		if **(**int32)(__ccgo_up(bp)) == 0 {
			/* The page contains no terms or rowids. Replace it with an empty
			 ** page and move on to the right-hand peer.  */
			**(**[4]Tu8)(__ccgo_up(bp + 4)) = [4]Tu8{
				3: uint8(0x04),
			}
			if bDetailNone == 0 {
				_fts5DataWrite(tls, p, iRowid, bp+4, int32(4))
			}
			_fts5DataRelease(tls, pLeaf)
			pLeaf = uintptr(0)
		} else {
			if bDetailNone != 0 {
				break
			} else {
				if **(**int32)(__ccgo_up(bp)) >= (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf || (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn < (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf || **(**int32)(__ccgo_up(bp)) < int32(4) {
					_fts5IndexCorruptRowid(tls, p, iRowid)
					break
				} else {
					nShift = **(**int32)(__ccgo_up(bp)) - int32(4)
					nIdx = 0
					aIdx = uintptr(0)
					/* Unless the current page footer is 0 bytes in size (in which case
					 ** the new page footer will be as well), allocate and populate a
					 ** buffer containing the new page footer. Set stack variables aIdx
					 ** and nIdx accordingly.  */
					if (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn > (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
						**(**int32)(__ccgo_up(bp + 8)) = 0
						i1 = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf
						i2 = 0
						i1 = i1 + _sqlite3Fts5GetVarint32(tls, aPg+uintptr(i1), bp+8)
						if **(**int32)(__ccgo_up(bp + 8)) < **(**int32)(__ccgo_up(bp)) {
							_fts5IndexCorruptRowid(tls, p, iRowid)
							break
						}
						aIdx = _sqlite3Fts5MallocZero(tls, p+44, int64((*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn-(*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf+int32(2)))
						if aIdx == uintptr(0) {
							break
						}
						i2 = _sqlite3Fts5PutVarint(tls, aIdx, uint64(**(**int32)(__ccgo_up(bp + 8))-nShift))
						if i1 < (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn {
							libc.Xmemcpy(tls, aIdx+uintptr(i2), aPg+uintptr(i1), uint32((*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn-i1))
							i2 = i2 + ((*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn - i1)
						}
						nIdx = i2
					}
					/* Modify the contents of buffer aPg[]. Set nPg to the new size
					 ** in bytes. The new page is always smaller than the old.  */
					nPg = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf - nShift
					libc.Xmemmove(tls, aPg+4, aPg+uintptr(int32(4)+nShift), uint32(nPg-int32(4)))
					_fts5PutU16(tls, aPg+2, uint16(nPg))
					if _fts5GetU16(tls, aPg) != 0 {
						_fts5PutU16(tls, aPg, uint16(4))
					}
					if nIdx > 0 {
						libc.Xmemcpy(tls, aPg+uintptr(nPg), aIdx, uint32(nIdx))
						nPg = nPg + nIdx
					}
					Xsqlite3_free(tls, aIdx)
					/* Write the new page to disk and exit the loop */
					_fts5DataWrite(tls, p, iRowid, aPg, nPg)
					break
				}
			}
		}
		goto _1
	_1:
		;
		pgno = pgno + 1
	}
	_fts5DataRelease(tls, pLeaf)
}

// C documentation
//
//	/*
//	** If the cursor requires seeking (bSeekRequired flag is set), seek it.
//	** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
//	**
//	** If argument bErrormsg is true and an error occurs, an error message may
//	** be left in sqlite3_vtab.zErrMsg.
//	*/
func _fts5SeekCursor(tls *libc.TLS, pCsr uintptr, bErrormsg int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var eStmt, rc int32
	var pTab, pTab1, v1 uintptr
	_, _, _, _, _ = eStmt, pTab, pTab1, rc, v1
	rc = SQLITE_OK
	/* If the cursor does not yet have a statement handle, obtain one now. */
	if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt == uintptr(0) {
		pTab = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab
		eStmt = _fts5StmtType(tls, pCsr)
		if bErrormsg != 0 {
			v1 = pTab + 8
		} else {
			v1 = uintptr(0)
		}
		rc = _sqlite3Fts5StorageStmt(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, eStmt, pCsr+48, v1)
	}
	if rc == SQLITE_OK && (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fcsrflags&int32(FTS5CSR_REQUIRE_CONTENT) != 0 {
		pTab1 = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab
		Xsqlite3_reset(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt)
		Xsqlite3_bind_int64(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt, int32(1), _fts5CursorRowid(tls, pCsr))
		(*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pTab1)).FpConfig)).FbLock = (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pTab1)).FpConfig)).FbLock + 1
		rc = Xsqlite3_step(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt)
		(*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pTab1)).FpConfig)).FbLock = (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pTab1)).FpConfig)).FbLock - 1
		if rc == int32(SQLITE_ROW) {
			rc = SQLITE_OK
			**(**int32)(__ccgo_up(pCsr + 60)) &= ^libc.Int32FromInt32(FTS5CSR_REQUIRE_CONTENT)
		} else {
			rc = Xsqlite3_reset(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt)
			if rc == SQLITE_OK {
				rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
				_fts5SetVtabError(tls, pTab1, __ccgo_ts+41190, libc.VaList(bp+8, _fts5CursorRowid(tls, pCsr), (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pTab1)).FpConfig)).FzContent))
			} else {
				if (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pTab1)).FpConfig)).FpzErrmsg != 0 {
					_fts5SetVtabError(tls, pTab1, __ccgo_ts+4729, libc.VaList(bp+8, Xsqlite3_errmsg(tls, (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pTab1)).FpConfig)).Fdb)))
				}
			}
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Allocate a tombstone hash page array object (pIter->pTombArray) for
//	** the iterator passed as the second argument. If an OOM error occurs,
//	** leave an error in the Fts5Index object.
//	*/
func _fts5SegIterAllocTombstone(tls *libc.TLS, p uintptr, pIter uintptr) {
	var nByte, nTomb Ti64
	var pNew uintptr
	_, _, _ = nByte, nTomb, pNew
	nTomb = int64((*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg)).FnPgTombstone)
	if nTomb > 0 {
		nByte = int64(uint32(libc.UintptrFromInt32(0)+8)) + (nTomb+libc.Int64FromInt32(1))*libc.Int64FromInt64(4)
		pNew = _sqlite3Fts5MallocZero(tls, p+44, nByte)
		if pNew != 0 {
			(*TFts5TombstoneArray)(unsafe.Pointer(pNew)).FnTombstone = int32(nTomb)
			(*TFts5TombstoneArray)(unsafe.Pointer(pNew)).FnRef = int32(1)
			(*TFts5SegIter)(unsafe.Pointer(pIter)).FpTombArray = pNew
		}
	}
}

// C documentation
//
//	/*
//	** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
//	** It is an error if leaf iLeafPgno does not exist. Unless the db is
//	** a 'secure-delete' db, if it contains no rowids then this is also an error.
//	*/
func _fts5SegIterGotoPage(tls *libc.TLS, p uintptr, pIter uintptr, iLeafPgno int32) {
	var a uintptr
	var iOff, n int32
	_, _, _ = a, iOff, n
	if iLeafPgno > (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg)).FpgnoLast {
		_fts5IndexCorruptIdx(tls, p)
	} else {
		_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpNextLeaf)
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FpNextLeaf = uintptr(0)
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = iLeafPgno - int32(1)
		for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			_fts5SegIterNextPage(tls, p, pIter)
			if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf == uintptr(0) {
				break
			}
			iOff = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp))
			if iOff > 0 {
				a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp
				n = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf
				if iOff < int32(4) || iOff >= n {
					_fts5IndexCorruptIdx(tls, p)
				} else {
					iOff = iOff + int32(_sqlite3Fts5GetVarint(tls, a+uintptr(iOff), pIter+88))
					(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(iOff)
					_fts5SegIterLoadNPos(tls, p, pIter)
				}
				break
			}
		}
	}
}

// C documentation
//
//	/*
//	** Initialize the object pIter to point to term pTerm/nTerm within the
//	** in-memory hash table. If there is no such term in the hash-table, the
//	** iterator is set to EOF.
//	**
//	** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
//	** an error has already occurred when this function is called, it is a no-op.
//	*/
func _fts5SegIterHashInit(tls *libc.TLS, p uintptr, pTerm uintptr, nTerm int32, flags int32, pIter uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var v1 int32
	var _ /* n at bp+8 */ int32
	var _ /* nList at bp+0 */ int32
	var _ /* pLeaf at bp+12 */ uintptr
	var _ /* pList at bp+16 */ uintptr
	var _ /* z at bp+4 */ uintptr
	_ = v1
	**(**int32)(__ccgo_up(bp)) = 0
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 8)) = 0
	**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
	if pTerm == uintptr(0) || flags&int32(FTS5INDEX_QUERY_SCAN) != 0 {
		**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)
		(*TFts5Index)(unsafe.Pointer(p)).Frc = _sqlite3Fts5HashScanInit(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, pTerm, nTerm)
		_sqlite3Fts5HashScanEntry(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, bp+4, bp+8, bp+16, bp)
		if **(**uintptr)(__ccgo_up(bp + 16)) != 0 {
			**(**uintptr)(__ccgo_up(bp + 12)) = _fts5IdxMalloc(tls, p, int64(12))
			if **(**uintptr)(__ccgo_up(bp + 12)) != 0 {
				(*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).Fp = **(**uintptr)(__ccgo_up(bp + 16))
			}
		}
		/* The call to sqlite3Fts5HashScanInit() causes the hash table to
		 ** fill the size field of all existing position lists. This means they
		 ** can no longer be appended to. Since the only scenario in which they
		 ** can be appended to is if the previous operation on this table was
		 ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this
		 ** possibility altogether.  */
		(*TFts5Index)(unsafe.Pointer(p)).FbDelete = 0
	} else {
		(*TFts5Index)(unsafe.Pointer(p)).Frc = _sqlite3Fts5HashQuery(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, int32(12), pTerm, nTerm, bp+12, bp)
		if **(**uintptr)(__ccgo_up(bp + 12)) != 0 {
			(*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).Fp = **(**uintptr)(__ccgo_up(bp + 12)) + 1*12
		}
		**(**uintptr)(__ccgo_up(bp + 4)) = pTerm
		**(**int32)(__ccgo_up(bp + 8)) = nTerm
		**(**int32)(__ccgo_up(pIter + 4)) |= int32(FTS5_SEGITER_ONETERM)
	}
	if **(**uintptr)(__ccgo_up(bp + 12)) != 0 {
		_sqlite3Fts5BufferSet(tls, p+44, pIter+72, **(**int32)(__ccgo_up(bp + 8)), **(**uintptr)(__ccgo_up(bp + 4)))
		v1 = **(**int32)(__ccgo_up(bp))
		(*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FszLeaf = v1
		(*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).Fnn = v1
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = **(**uintptr)(__ccgo_up(bp + 12))
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).Fp, pIter+88))
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).Fnn
		if flags&int32(FTS5INDEX_QUERY_DESC) != 0 {
			**(**int32)(__ccgo_up(pIter + 4)) |= int32(FTS5_SEGITER_REVERSE)
			_fts5SegIterReverseInitPage(tls, p, pIter)
		} else {
			_fts5SegIterLoadNPos(tls, p, pIter)
		}
	}
	_fts5SegIterSetNext(tls, p, pIter)
}

func _fts5SegIterLoadRowid(tls *libc.TLS, p uintptr, pIter uintptr) {
	var a uintptr
	var iOff Ti64
	_, _ = a, iOff
	a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp /* Buffer to read data from */
	iOff = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset
	for iOff >= int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf) {
		_fts5SegIterNextPage(tls, p, pIter)
		if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf == uintptr(0) {
			if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				_fts5IndexCorruptIter(tls, p, pIter)
			}
			return
		}
		iOff = int64(4)
		a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp
	}
	iOff = iOff + int64(_sqlite3Fts5GetVarint(tls, a+uintptr(iOff), pIter+88))
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = iOff
}

// C documentation
//
//	/*
//	** Fts5SegIter.iLeafOffset currently points to the first byte of the
//	** "nSuffix" field of a term. Function parameter nKeep contains the value
//	** of the "nPrefix" field (if there was one - it is passed 0 if this is
//	** the first term in the segment).
//	**
//	** This function populates:
//	**
//	**   Fts5SegIter.term
//	**   Fts5SegIter.rowid
//	**
//	** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
//	** the first position list. The position list belonging to document
//	** (Fts5SegIter.iRowid).
//	*/
func _fts5SegIterLoadTerm(tls *libc.TLS, p uintptr, pIter uintptr, nKeep int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a uintptr
	var iOff Ti64
	var _ /* nExtra at bp+4 */ int32
	var _ /* nNew at bp+0 */ int32
	_, _ = a, iOff
	a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp /* Buffer to read data from */
	iOff = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset                         /* Bytes of new data */
	iOff = iOff + int64(_sqlite3Fts5GetVarint32(tls, a+uintptr(iOff), bp))
	if iOff+int64(**(**int32)(__ccgo_up(bp))) > int64((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf) || nKeep > (*TFts5SegIter)(unsafe.Pointer(pIter)).Fterm.Fn || **(**int32)(__ccgo_up(bp)) == 0 {
		_fts5IndexCorruptIter(tls, p, pIter)
		return
	}
	(*TFts5SegIter)(unsafe.Pointer(pIter)).Fterm.Fn = nKeep
	_sqlite3Fts5BufferAppendBlob(tls, p+44, pIter+72, uint32(**(**int32)(__ccgo_up(bp))), a+uintptr(iOff))
	iOff = iOff + int64(**(**int32)(__ccgo_up(bp)))
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafOffset = int32(iOff)
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafPgno = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = iOff
	if (*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff >= (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn {
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn + int32(1)
	} else {
		**(**int32)(__ccgo_up(pIter + 48)) += _sqlite3Fts5GetVarint32(tls, a+uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff), bp+4)
		**(**int32)(__ccgo_up(pIter + 52)) += **(**int32)(__ccgo_up(bp + 4))
	}
	_fts5SegIterLoadRowid(tls, p, pIter)
}

// C documentation
//
//	/*
//	** Advance iterator pIter to the next entry.
//	**
//	** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
//	** is not considered an error if the iterator reaches EOF. If an error has
//	** already occurred when this function is called, it is a no-op.
//	*/
func _fts5SegIterNext(tls *libc.TLS, p uintptr, pIter uintptr, pbNewTerm uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var a, pLeaf, v1 uintptr
	var bNewTerm, n, v2 int32
	var v3 Ti64
	var _ /* iDelta at bp+8 */ Tu64
	var _ /* iOff at bp+0 */ int32
	var _ /* nKeep at bp+4 */ int32
	var _ /* nList at bp+28 */ int32
	var _ /* nSz at bp+32 */ int32
	var _ /* nTerm at bp+24 */ int32
	var _ /* pList at bp+16 */ uintptr
	var _ /* zTerm at bp+20 */ uintptr
	_, _, _, _, _, _, _ = a, bNewTerm, n, pLeaf, v1, v2, v3
	pLeaf = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf
	bNewTerm = 0
	**(**int32)(__ccgo_up(bp + 4)) = 0
	/* Search for the end of the position list within the current page. */
	a = (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp
	n = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf
	**(**int32)(__ccgo_up(bp)) = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset + int64((*TFts5SegIter)(unsafe.Pointer(pIter)).FnPos))
	if **(**int32)(__ccgo_up(bp)) < n {
		/* The next entry is on the current page. */
		if **(**int32)(__ccgo_up(bp)) >= (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist {
			bNewTerm = int32(1)
			if **(**int32)(__ccgo_up(bp)) != _fts5LeafFirstTermOff(tls, pLeaf) {
				**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + _sqlite3Fts5GetVarint32(tls, a+uintptr(**(**int32)(__ccgo_up(bp))), bp+4)
			}
		} else {
			**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + int32(_sqlite3Fts5GetVarint(tls, a+uintptr(**(**int32)(__ccgo_up(bp))), bp+8))
			v1 = pIter + 88
			*(*Ti64)(unsafe.Pointer(v1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(v1))) + **(**Tu64)(__ccgo_up(bp + 8)))
		}
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(**(**int32)(__ccgo_up(bp)))
	} else {
		if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg == uintptr(0) {
			**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)
			**(**uintptr)(__ccgo_up(bp + 20)) = uintptr(0)
			**(**int32)(__ccgo_up(bp + 24)) = 0
			**(**int32)(__ccgo_up(bp + 28)) = 0
			if 0 == (*TFts5SegIter)(unsafe.Pointer(pIter)).Fflags&int32(FTS5_SEGITER_ONETERM) {
				_sqlite3Fts5HashScanNext(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash)
				_sqlite3Fts5HashScanEntry(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, bp+20, bp+24, bp+16, bp+28)
			}
			if **(**uintptr)(__ccgo_up(bp + 16)) == uintptr(0) {
				_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = uintptr(0)
			} else {
				(*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp = **(**uintptr)(__ccgo_up(bp + 16))
				(*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn = **(**int32)(__ccgo_up(bp + 28))
				(*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf = **(**int32)(__ccgo_up(bp + 28))
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = **(**int32)(__ccgo_up(bp + 28)) + int32(1)
				_sqlite3Fts5BufferSet(tls, p+44, pIter+72, **(**int32)(__ccgo_up(bp + 24)), **(**uintptr)(__ccgo_up(bp + 20)))
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, **(**uintptr)(__ccgo_up(bp + 16)), pIter+88))
				**(**int32)(__ccgo_up(pbNewTerm)) = int32(1)
			}
		} else {
			**(**int32)(__ccgo_up(bp)) = 0
			/* Next entry is not on the current page */
			for **(**int32)(__ccgo_up(bp)) == 0 {
				_fts5SegIterNextPage(tls, p, pIter)
				pLeaf = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf
				if pLeaf == uintptr(0) {
					break
				}
				v2 = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp))
				**(**int32)(__ccgo_up(bp)) = v2
				if v2 != 0 && **(**int32)(__ccgo_up(bp)) < (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
					**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr(**(**int32)(__ccgo_up(bp))), pIter+88))
					(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(**(**int32)(__ccgo_up(bp)))
					if (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn > (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
						(*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf + _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf), pIter+52)
					}
				} else {
					if (*TFts5Data)(unsafe.Pointer(pLeaf)).Fnn > (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
						(*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff = (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf + _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr((*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf), bp)
						(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(**(**int32)(__ccgo_up(bp)))
						(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = **(**int32)(__ccgo_up(bp))
						bNewTerm = int32(1)
					}
				}
				if **(**int32)(__ccgo_up(bp)) > (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
					_fts5IndexCorruptIter(tls, p, pIter)
					return
				}
			}
		}
	}
	/* Check if the iterator is now at EOF. If so, return early. */
	if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 {
		if bNewTerm != 0 {
			if (*TFts5SegIter)(unsafe.Pointer(pIter)).Fflags&int32(FTS5_SEGITER_ONETERM) != 0 {
				_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = uintptr(0)
			} else {
				_fts5SegIterLoadTerm(tls, p, pIter, **(**int32)(__ccgo_up(bp + 4)))
				_fts5SegIterLoadNPos(tls, p, pIter)
				if pbNewTerm != 0 {
					**(**int32)(__ccgo_up(pbNewTerm)) = int32(1)
				}
			}
		} else {
			v1 = pIter + 24
			v3 = *(*Ti64)(unsafe.Pointer(v1))
			*(*Ti64)(unsafe.Pointer(v1)) = *(*Ti64)(unsafe.Pointer(v1)) + 1
			**(**int32)(__ccgo_up(bp + 32)) = int32(**(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp + uintptr(v3))))
			if **(**int32)(__ccgo_up(bp + 32))&int32(0x80) != 0 {
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset - 1
				**(**Ti64)(__ccgo_up(pIter + 24)) += int64(_sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp+uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset), bp+32))
			}
			(*TFts5SegIter)(unsafe.Pointer(pIter)).FbDel = uint8(**(**int32)(__ccgo_up(bp + 32)) & libc.Int32FromInt32(0x0001))
			(*TFts5SegIter)(unsafe.Pointer(pIter)).FnPos = **(**int32)(__ccgo_up(bp + 32)) >> int32(1)
		}
	}
}

// C documentation
//
//	/*
//	** Advance iterator pIter to the next entry.
//	**
//	** This version of fts5SegIterNext() is only used if detail=none and the
//	** iterator is not a reverse direction iterator.
//	*/
func _fts5SegIterNext_None(tls *libc.TLS, p uintptr, pIter uintptr, pbNewTerm uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var iOff int32
	var v1 uintptr
	var _ /* iDelta at bp+0 */ Tu64
	var _ /* nKeep at bp+8 */ int32
	var _ /* nList at bp+24 */ int32
	var _ /* nTerm at bp+20 */ int32
	var _ /* pList at bp+12 */ uintptr
	var _ /* zTerm at bp+16 */ uintptr
	_, _ = iOff, v1
	iOff = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset)
	/* Next entry is on the next page */
	for (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg != 0 && iOff >= (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf {
		_fts5SegIterNextPage(tls, p, pIter)
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 || (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf == uintptr(0) {
			return
		}
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiRowid = 0
		iOff = int32(4)
	}
	if iOff < (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist {
		iOff = iOff + int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp+uintptr(iOff), bp))
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(iOff)
		v1 = pIter + 88
		*(*Ti64)(unsafe.Pointer(v1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(v1))) + **(**Tu64)(__ccgo_up(bp)))
	} else {
		if (*TFts5SegIter)(unsafe.Pointer(pIter)).Fflags&int32(FTS5_SEGITER_ONETERM) == 0 {
			if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg != 0 {
				**(**int32)(__ccgo_up(bp + 8)) = 0
				if iOff != _fts5LeafFirstTermOff(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf) {
					iOff = iOff + _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp+uintptr(iOff), bp+8)
				}
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(iOff)
				_fts5SegIterLoadTerm(tls, p, pIter, **(**int32)(__ccgo_up(bp + 8)))
			} else {
				**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
				**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)
				**(**int32)(__ccgo_up(bp + 20)) = 0
				_sqlite3Fts5HashScanNext(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash)
				_sqlite3Fts5HashScanEntry(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, bp+16, bp+20, bp+12, bp+24)
				if **(**uintptr)(__ccgo_up(bp + 12)) == uintptr(0) {
					goto next_none_eof
				}
				(*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp = **(**uintptr)(__ccgo_up(bp + 12))
				(*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn = **(**int32)(__ccgo_up(bp + 24))
				(*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf = **(**int32)(__ccgo_up(bp + 24))
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = **(**int32)(__ccgo_up(bp + 24))
				_sqlite3Fts5BufferSet(tls, p+44, pIter+72, **(**int32)(__ccgo_up(bp + 20)), **(**uintptr)(__ccgo_up(bp + 16)))
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, **(**uintptr)(__ccgo_up(bp + 12)), pIter+88))
			}
			if pbNewTerm != 0 {
				**(**int32)(__ccgo_up(pbNewTerm)) = int32(1)
			}
		} else {
			goto next_none_eof
		}
	}
	_fts5SegIterLoadNPos(tls, p, pIter)
	return
	goto next_none_eof
next_none_eof:
	;
	_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = uintptr(0)
}

// C documentation
//
//	/*
//	** Iterator pIter currently points to the first rowid in a doclist. This
//	** function sets the iterator up so that iterates in reverse order through
//	** the doclist.
//	*/
func _fts5SegIterReverse(tls *libc.TLS, p uintptr, pIter uintptr) {
	var bTermless, iEnd, iOff, iPoslist, iRowid, iSegid, pgno, pgnoLast, v1 int32
	var iAbs Ti64
	var pDlidx, pLast, pLeaf, pNew, pSeg, tmp uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bTermless, iAbs, iEnd, iOff, iPoslist, iRowid, iSegid, pDlidx, pLast, pLeaf, pNew, pSeg, pgno, pgnoLast, tmp, v1
	pDlidx = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpDlidx
	pLast = uintptr(0)
	pgnoLast = 0
	if pDlidx != 0 && (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FiVersion == int32(FTS5_CURRENT_VERSION) {
		iSegid = (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg)).FiSegid
		pgnoLast = _fts5DlidxIterPgno(tls, pDlidx)
		pLast = _fts5LeafRead(tls, p, int64(iSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64(pgnoLast))
	} else {
		pLeaf = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf
		if (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafPgno == (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno {
			iPoslist = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafOffset
		} else {
			iPoslist = int32(4)
		}
		iEnd = iPoslist + int32(9)
		for {
			v1 = iPoslist
			iPoslist = iPoslist + 1
			if !(int32(**(**Tu8)(__ccgo_up((*TFts5Data)(unsafe.Pointer(pLeaf)).Fp + uintptr(v1))))&int32(0x80) != 0 && iPoslist < iEnd) {
				break
			}
		}
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(iPoslist)
		/* If this condition is true then the largest rowid for the current
		 ** term may not be stored on the current page. So search forward to
		 ** see where said rowid really is.  */
		if (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist >= (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf {
			pSeg = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg
			/* The last rowid in the doclist may not be on the current page. Search
			 ** forward to find the page containing the last rowid.  */
			pgno = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno + int32(1)
			for {
				if !(!((*TFts5Index)(unsafe.Pointer(p)).Frc != 0) && pgno <= (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast) {
					break
				}
				iAbs = int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64(pgno)
				pNew = _fts5LeafRead(tls, p, iAbs)
				if pNew != 0 {
					iRowid = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pNew)).Fp))
					bTermless = libc.BoolInt32((*TFts5Data)(unsafe.Pointer(pNew)).FszLeaf >= (*TFts5Data)(unsafe.Pointer(pNew)).Fnn)
					if iRowid != 0 {
						tmp = pNew
						pNew = pLast
						pLast = tmp
						pgnoLast = pgno
					}
					_fts5DataRelease(tls, pNew)
					if bTermless == 0 {
						break
					}
				}
				goto _2
			_2:
				;
				pgno = pgno + 1
			}
		}
	}
	/* If pLast is NULL at this point, then the last rowid for this doclist
	 ** lies on the page currently indicated by the iterator. In this case
	 ** pIter->iLeafOffset is already set to point to the position-list size
	 ** field associated with the first relevant rowid on the page.
	 **
	 ** Or, if pLast is non-NULL, then it is the page that contains the last
	 ** rowid. In this case configure the iterator so that it points to the
	 ** first rowid on this page.
	 */
	if pLast != 0 {
		_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = pLast
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = pgnoLast
		if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			iOff = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pLast)).Fp))
			if iOff > (*TFts5Data)(unsafe.Pointer(pLast)).FszLeaf {
				_fts5IndexCorruptIter(tls, p, pIter)
				return
			}
			iOff = iOff + int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pLast)).Fp+uintptr(iOff), pIter+88))
			(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(iOff)
			if (*TFts5Data)(unsafe.Pointer(pLast)).FszLeaf >= (*TFts5Data)(unsafe.Pointer(pLast)).Fnn {
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer(pLast)).Fnn + int32(1)
			} else {
				(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = _fts5LeafFirstTermOff(tls, pLast)
			}
		}
	}
	_fts5SegIterReverseInitPage(tls, p, pIter)
}

// C documentation
//
//	/*
//	** This function is only ever called on iterators created by calls to
//	** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
//	**
//	** The iterator is in an unusual state when this function is called: the
//	** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
//	** the position-list size field for the first relevant rowid on the page.
//	** Fts5SegIter.rowid is set, but nPos and bDel are not.
//	**
//	** This function advances the iterator so that it points to the last
//	** relevant rowid on the page and, if necessary, initializes the
//	** aRowidOffset[] and iRowidOffset variables. At this point the iterator
//	** is in its regular state - Fts5SegIter.iLeafOffset points to the first
//	** byte of the position list content associated with said rowid.
//	*/
func _fts5SegIterReverseInitPage(tls *libc.TLS, p uintptr, pIter uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a, aNew, v1 uintptr
	var eDetail, i, iRowidOffset, n, v2 int32
	var nNew Ti64
	var _ /* bDummy at bp+12 */ int32
	var _ /* iDelta at bp+0 */ Tu64
	var _ /* nPos at bp+8 */ int32
	_, _, _, _, _, _, _, _, _ = a, aNew, eDetail, i, iRowidOffset, n, nNew, v1, v2
	eDetail = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail
	n = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf
	i = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset)
	a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp
	iRowidOffset = 0
	if n > (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist {
		n = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist
	}
	for int32(1) != 0 {
		**(**Tu64)(__ccgo_up(bp)) = uint64(0)
		if i >= n {
			break
		}
		if eDetail == int32(FTS5_DETAIL_NONE) {
			/* todo */
			if i < n && int32(**(**Tu8)(__ccgo_up(a + uintptr(i)))) == 0 {
				i = i + 1
				if i < n && int32(**(**Tu8)(__ccgo_up(a + uintptr(i)))) == 0 {
					i = i + 1
				}
			}
		} else {
			i = i + _fts5GetPoslistSize(tls, a+uintptr(i), bp+8, bp+12)
			i = i + **(**int32)(__ccgo_up(bp + 8))
		}
		if i >= n {
			break
		}
		i = i + int32(_sqlite3Fts5GetVarint(tls, a+uintptr(i), bp))
		v1 = pIter + 88
		*(*Ti64)(unsafe.Pointer(v1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(v1))) + **(**Tu64)(__ccgo_up(bp)))
		/* If necessary, grow the pIter->aRowidOffset[] array. */
		if iRowidOffset >= (*TFts5SegIter)(unsafe.Pointer(pIter)).FnRowidOffset {
			nNew = int64((*TFts5SegIter)(unsafe.Pointer(pIter)).FnRowidOffset + int32(8))
			aNew = Xsqlite3_realloc64(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FaRowidOffset, uint64(nNew*int64(4)))
			if aNew == uintptr(0) {
				(*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
				break
			}
			(*TFts5SegIter)(unsafe.Pointer(pIter)).FaRowidOffset = aNew
			(*TFts5SegIter)(unsafe.Pointer(pIter)).FnRowidOffset = int32(nNew)
		}
		v2 = iRowidOffset
		iRowidOffset = iRowidOffset + 1
		**(**int32)(__ccgo_up((*TFts5SegIter)(unsafe.Pointer(pIter)).FaRowidOffset + uintptr(v2)*4)) = int32((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset)
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(i)
	}
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FiRowidOffset = iRowidOffset
	_fts5SegIterLoadNPos(tls, p, pIter)
}

// C documentation
//
//	/*
//	**
//	*/
func _fts5SegIterReverseNewPage(tls *libc.TLS, p uintptr, pIter uintptr) {
	var a, pNew uintptr
	var iRowidOff int32
	_, _, _ = a, iRowidOff, pNew
	_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)
	(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = uintptr(0)
	for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno > (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafPgno {
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno = (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno - 1
		pNew = _fts5LeafRead(tls, p, int64((*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno))
		if pNew != 0 {
			/* iTermLeafOffset may be equal to szLeaf if the term is the last
			 ** thing on the page - i.e. the first rowid is on the following page.
			 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
			if (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno == (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafPgno {
				if (*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafOffset < (*TFts5Data)(unsafe.Pointer(pNew)).FszLeaf {
					(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = pNew
					(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64((*TFts5SegIter)(unsafe.Pointer(pIter)).FiTermLeafOffset)
				}
			} else {
				iRowidOff = int32(_fts5GetU16(tls, (*TFts5Data)(unsafe.Pointer(pNew)).Fp))
				if iRowidOff != 0 {
					if iRowidOff >= (*TFts5Data)(unsafe.Pointer(pNew)).FszLeaf {
						_fts5IndexCorruptIter(tls, p, pIter)
					} else {
						(*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = pNew
						(*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(iRowidOff)
					}
				}
			}
			if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 {
				a = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fp + uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset)
				**(**Ti64)(__ccgo_up(pIter + 24)) += int64(_sqlite3Fts5GetVarint(tls, a, pIter+88))
				break
			} else {
				_fts5DataRelease(tls, pNew)
			}
		}
	}
	if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 {
		(*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).Fnn + int32(1)
		_fts5SegIterReverseInitPage(tls, p, pIter)
	}
}

// C documentation
//
//	/*
//	** Iterator pIter currently points to a valid entry (not EOF). This
//	** function appends the position list data for the current entry to
//	** buffer pBuf. It does not make a copy of the position-list size
//	** field.
//	*/
func _fts5SegiterPoslist(tls *libc.TLS, p uintptr, pSeg uintptr, pColset uintptr, pBuf uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var v1 int32
	var _ /* sCtx at bp+0 */ TPoslistCallbackCtx
	var _ /* sCtx at bp+12 */ TPoslistOffsetsCtx
	_ = v1
	if uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)+uint32((*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos+libc.Int32FromInt32(FTS5_DATA_ZERO_PADDING)) <= uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace) {
		v1 = 0
	} else {
		v1 = _sqlite3Fts5BufferSize(tls, p+44, pBuf, uint32((*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos+int32(FTS5_DATA_ZERO_PADDING)+(*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn))
	}
	if 0 == v1 {
		libc.Xmemset(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5SegIter)(unsafe.Pointer(pSeg)).FnPos), 0, uint32(FTS5_DATA_ZERO_PADDING))
		if pColset == uintptr(0) {
			_fts5ChunkIterate(tls, p, pSeg, pBuf, __ccgo_fp(_fts5PoslistCallback))
		} else {
			if (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == FTS5_DETAIL_FULL {
				(**(**TPoslistCallbackCtx)(__ccgo_up(bp))).FpBuf = pBuf
				(**(**TPoslistCallbackCtx)(__ccgo_up(bp))).FpColset = pColset
				(**(**TPoslistCallbackCtx)(__ccgo_up(bp))).FeState = _fts5IndexColsetTest(tls, pColset, 0)
				_fts5ChunkIterate(tls, p, pSeg, bp, __ccgo_fp(_fts5PoslistFilterCallback))
			} else {
				libc.Xmemset(tls, bp+12, 0, uint32(16))
				(**(**TPoslistOffsetsCtx)(__ccgo_up(bp + 12))).FpBuf = pBuf
				(**(**TPoslistOffsetsCtx)(__ccgo_up(bp + 12))).FpColset = pColset
				_fts5ChunkIterate(tls, p, pSeg, bp+12, __ccgo_fp(_fts5PoslistOffsetsCallback))
			}
		}
	}
}

// C documentation
//
//	/*
//	** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
//	** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
//	** error occurs.
//	*/
func _fts5SentenceFinderAdd(tls *libc.TLS, p uintptr, iAdd int32) (r int32) {
	var aNew, v3 uintptr
	var nNew, v1 int32
	_, _, _, _ = aNew, nNew, v1, v3
	if (*TFts5SFinder)(unsafe.Pointer(p)).FnFirstAlloc == (*TFts5SFinder)(unsafe.Pointer(p)).FnFirst {
		if (*TFts5SFinder)(unsafe.Pointer(p)).FnFirstAlloc != 0 {
			v1 = (*TFts5SFinder)(unsafe.Pointer(p)).FnFirstAlloc * int32(2)
		} else {
			v1 = int32(64)
		}
		nNew = v1
		aNew = Xsqlite3_realloc64(tls, (*TFts5SFinder)(unsafe.Pointer(p)).FaFirst, uint64(uint32(nNew)*uint32(4)))
		if aNew == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		(*TFts5SFinder)(unsafe.Pointer(p)).FaFirst = aNew
		(*TFts5SFinder)(unsafe.Pointer(p)).FnFirstAlloc = nNew
	}
	v3 = p + 8
	v1 = *(*int32)(unsafe.Pointer(v3))
	*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
	**(**int32)(__ccgo_up((*TFts5SFinder)(unsafe.Pointer(p)).FaFirst + uintptr(v1)*4)) = iAdd
	return SQLITE_OK
}

func _fts5SetupPrefixIter(tls *libc.TLS, p uintptr, bDesc int32, iIdx int32, pToken uintptr, nToken int32, pColset uintptr, ppIter uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var i, iFree, v3 int32
	var pCtx, pData, pStruct uintptr
	var _ /* s at bp+0 */ TPrefixSetupCtx
	var _ /* s2 at bp+48 */ TTokendataSetupCtx
	_, _, _, _, _, _ = i, iFree, pCtx, pData, pStruct, v3
	libc.Xmemset(tls, bp, 0, uint32(48))
	libc.Xmemset(tls, bp+48, 0, uint32(12))
	(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnMerge = int32(1)
	(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FiLastRowid = 0
	(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnBuf = int32(32)
	if iIdx == 0 && (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == FTS5_DETAIL_FULL && (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FbPrefixInsttoken != 0 {
		(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FpTokendata = bp + 48
		(**(**TTokendataSetupCtx)(__ccgo_up(bp + 48))).FpT = _fts5IdxMalloc(tls, p, int64(uint32(libc.UintptrFromInt32(0)+56)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(80)))
	}
	if (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail == int32(FTS5_DETAIL_NONE) {
		(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FxMerge = __ccgo_fp(_fts5MergeRowidLists)
		(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FxAppend = __ccgo_fp(_fts5AppendRowid)
	} else {
		(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnMerge = libc.Int32FromInt32(FTS5_MERGE_NLIST) - libc.Int32FromInt32(1)
		(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnBuf = (**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnMerge * int32(8) /* Sufficient to merge (16^8)==(2^32) lists */
		(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FxMerge = __ccgo_fp(_fts5MergePrefixLists)
		(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FxAppend = __ccgo_fp(_fts5AppendPoslist)
	}
	(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FaBuf = _fts5IdxMalloc(tls, p, int64(uint32(12)*uint32((**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnBuf)))
	pStruct = _fts5StructureRead(tls, p)
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		pCtx = bp
		/* If iIdx is non-zero, then it is the number of a prefix-index for
		 ** prefixes 1 character longer than the prefix being queried for. That
		 ** index contains all the doclists required, except for the one
		 ** corresponding to the prefix itself. That one is extracted from the
		 ** main term index here.  */
		if iIdx != 0 {
			**(**Tu8)(__ccgo_up(pToken)) = uint8('0')
			_fts5VisitEntries(tls, p, pColset, pToken, nToken, 0, __ccgo_fp(_prefixIterSetupCb), pCtx)
		}
		**(**Tu8)(__ccgo_up(pToken)) = uint8(int32('0') + iIdx)
		_fts5VisitEntries(tls, p, pColset, pToken, nToken, int32(1), __ccgo_fp(_prefixIterSetupCb), pCtx)
		i = 0
		for {
			if !(i < (**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnBuf) {
				break
			}
			if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				(*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FxMerge})))(tls, p, bp+28, (**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnMerge, (**(**TPrefixSetupCtx)(__ccgo_up(bp))).FaBuf+uintptr(i)*12)
			}
			iFree = i
			for {
				if !(iFree < i+(**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnMerge) {
					break
				}
				_sqlite3Fts5BufferFree(tls, (**(**TPrefixSetupCtx)(__ccgo_up(bp))).FaBuf+uintptr(iFree)*12)
				goto _2
			_2:
				;
				iFree = iFree + 1
			}
			goto _1
		_1:
			;
			i = i + (**(**TPrefixSetupCtx)(__ccgo_up(bp))).FnMerge
		}
		pData = _fts5IdxMalloc(tls, p, int64(12)+int64((**(**TPrefixSetupCtx)(__ccgo_up(bp))).Fdoclist.Fn)+int64(FTS5_DATA_ZERO_PADDING))
		if pData != 0 {
			(*TFts5Data)(unsafe.Pointer(pData)).Fp = pData + 1*12
			v3 = (**(**TPrefixSetupCtx)(__ccgo_up(bp))).Fdoclist.Fn
			(*TFts5Data)(unsafe.Pointer(pData)).FszLeaf = v3
			(*TFts5Data)(unsafe.Pointer(pData)).Fnn = v3
			if (**(**TPrefixSetupCtx)(__ccgo_up(bp))).Fdoclist.Fn != 0 {
				libc.Xmemcpy(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp, (**(**TPrefixSetupCtx)(__ccgo_up(bp))).Fdoclist.Fp, uint32((**(**TPrefixSetupCtx)(__ccgo_up(bp))).Fdoclist.Fn))
			}
			_fts5MultiIterNew2(tls, p, pData, bDesc, ppIter)
		}
		if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (**(**TPrefixSetupCtx)(__ccgo_up(bp))).FpTokendata != 0 {
			_fts5TokendataIterSortMap(tls, p, (**(**TTokendataSetupCtx)(__ccgo_up(bp + 48))).FpT)
			(*TFts5Iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(ppIter)))).FpTokenDataIter = (**(**TTokendataSetupCtx)(__ccgo_up(bp + 48))).FpT
			(**(**TTokendataSetupCtx)(__ccgo_up(bp + 48))).FpT = uintptr(0)
		}
	}
	_fts5TokendataIterDelete(tls, (**(**TTokendataSetupCtx)(__ccgo_up(bp + 48))).FpT)
	_sqlite3Fts5BufferFree(tls, bp+28)
	_fts5StructureRelease(tls, pStruct)
	Xsqlite3_free(tls, (**(**TPrefixSetupCtx)(__ccgo_up(bp))).FaBuf)
}

// C documentation
//
//	/*
//	** pIter is a prefix query. This function populates pIter->pTokenDataIter
//	** with an Fts5TokenDataIter object containing mappings for all rows
//	** matched by the query.
//	*/
func _fts5SetupPrefixIterTokendata(tls *libc.TLS, pIter uintptr, pToken uintptr, nToken int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var p uintptr
	var _ /* ctx at bp+12 */ TTokendataSetupCtx
	var _ /* token at bp+0 */ TFts5Buffer
	_ = p
	p = (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex
	**(**TFts5Buffer)(__ccgo_up(bp)) = TFts5Buffer{}
	libc.Xmemset(tls, bp+12, 0, uint32(12))
	if !(uint32((*TFts5Buffer)(unsafe.Pointer(bp)).Fn)+uint32(nToken+libc.Int32FromInt32(1)) <= uint32((*TFts5Buffer)(unsafe.Pointer(bp)).FnSpace)) {
		_sqlite3Fts5BufferSize(tls, p+44, bp, uint32(nToken+int32(1)+(*TFts5Buffer)(unsafe.Pointer(bp)).Fn))
	}
	(**(**TTokendataSetupCtx)(__ccgo_up(bp + 12))).FpT = _sqlite3Fts5MallocZero(tls, p+44, int64(uint32(libc.UintptrFromInt32(0)+56)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(80)))
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		/* Fill in the token prefix to search for */
		**(**Tu8)(__ccgo_up((**(**TFts5Buffer)(__ccgo_up(bp))).Fp)) = uint8('0')
		libc.Xmemcpy(tls, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp+1, pToken, uint32(nToken))
		(**(**TFts5Buffer)(__ccgo_up(bp))).Fn = nToken + int32(1)
		_fts5VisitEntries(tls, p, uintptr(0), (**(**TFts5Buffer)(__ccgo_up(bp))).Fp, (**(**TFts5Buffer)(__ccgo_up(bp))).Fn, int32(1), __ccgo_fp(_prefixIterSetupTokendataCb), bp+12)
		_fts5TokendataIterSortMap(tls, p, (**(**TTokendataSetupCtx)(__ccgo_up(bp + 12))).FpT)
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter = (**(**TTokendataSetupCtx)(__ccgo_up(bp + 12))).FpT
	} else {
		_fts5TokendataIterDelete(tls, (**(**TTokendataSetupCtx)(__ccgo_up(bp + 12))).FpT)
	}
	_sqlite3Fts5BufferFree(tls, bp)
	return _fts5IndexReturn(tls, p)
}

// C documentation
//
//	/*
//	** This function sets up an iterator to use for a non-prefix query on a
//	** tokendata=1 table.
//	*/
func _fts5SetupTokendataIter(tls *libc.TLS, p uintptr, pToken uintptr, nToken int32, pColset uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bDone, flags, iLvl, iSeg, iSeg1, ii, ii1 int32
	var pII, pIter, pNew, pNewIter, pPrev, pPrevIter, pRet, pSeg, pSet, pSmall, pStruct, v1 uintptr
	var _ /* bSeek at bp+0 */ TFts5Buffer
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bDone, flags, iLvl, iSeg, iSeg1, ii, ii1, pII, pIter, pNew, pNewIter, pPrev, pPrevIter, pRet, pSeg, pSet, pSmall, pStruct, v1
	pRet = uintptr(0)
	pSet = uintptr(0)
	pStruct = uintptr(0)
	flags = libc.Int32FromInt32(FTS5INDEX_QUERY_SCANONETERM) | libc.Int32FromInt32(FTS5INDEX_QUERY_SCAN)
	**(**TFts5Buffer)(__ccgo_up(bp)) = TFts5Buffer{}
	pSmall = uintptr(0)
	_fts5IndexFlush(tls, p)
	pStruct = _fts5StructureRead(tls, p)
	for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		if pSet != 0 {
			v1 = *(*uintptr)(unsafe.Pointer(pSet + 56 + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pSet)).FnIter-int64(1))*4))
		} else {
			v1 = uintptr(0)
		}
		pPrev = v1
		pNew = uintptr(0)
		pNewIter = uintptr(0)
		pPrevIter = uintptr(0)
		pNew = _fts5MultiIterAlloc(tls, p, (*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment)
		if pSmall != 0 {
			_sqlite3Fts5BufferSet(tls, p+44, bp, (*TFts5Buffer)(unsafe.Pointer(pSmall)).Fn, (*TFts5Buffer)(unsafe.Pointer(pSmall)).Fp)
			_sqlite3Fts5BufferAppendBlob(tls, p+44, bp, uint32(1), __ccgo_ts+40821)
		} else {
			_sqlite3Fts5BufferSet(tls, p+44, bp, nToken, pToken)
		}
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
			_fts5IterClose(tls, pNew)
			break
		}
		pNewIter = pNew + 80
		if pPrev != 0 {
			v1 = pPrev + 80
		} else {
			v1 = uintptr(0)
		}
		pPrevIter = v1
		iLvl = 0
		for {
			if !(iLvl < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) {
				break
			}
			iSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg - int32(1)
			for {
				if !(iSeg >= 0) {
					break
				}
				pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FaSeg + uintptr(iSeg)*56
				bDone = 0
				if pPrevIter != 0 {
					if _fts5BufferCompare(tls, pSmall, pPrevIter+72) != 0 {
						libc.Xmemcpy(tls, pNewIter, pPrevIter, uint32(104))
						libc.Xmemset(tls, pPrevIter, 0, uint32(104))
						bDone = int32(1)
					} else {
						if (*TFts5SegIter)(unsafe.Pointer(pPrevIter)).FiEndofDoclist > (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pPrevIter)).FpLeaf)).FszLeaf {
							_fts5SegIterNextInit(tls, p, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp, (**(**TFts5Buffer)(__ccgo_up(bp))).Fn-int32(1), pSeg, pNewIter)
							bDone = int32(1)
						}
					}
				}
				if bDone == 0 {
					_fts5SegIterSeekInit(tls, p, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp, (**(**TFts5Buffer)(__ccgo_up(bp))).Fn, flags, pSeg, pNewIter)
				}
				if pPrevIter != 0 {
					if (*TFts5SegIter)(unsafe.Pointer(pPrevIter)).FpTombArray != 0 {
						(*TFts5SegIter)(unsafe.Pointer(pNewIter)).FpTombArray = (*TFts5SegIter)(unsafe.Pointer(pPrevIter)).FpTombArray
						(*TFts5TombstoneArray)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pNewIter)).FpTombArray)).FnRef = (*TFts5TombstoneArray)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pNewIter)).FpTombArray)).FnRef + 1
					}
				} else {
					_fts5SegIterAllocTombstone(tls, p, pNewIter)
				}
				pNewIter += 104
				if pPrevIter != 0 {
					pPrevIter += 104
				}
				if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
					break
				}
				goto _4
			_4:
				;
				iSeg = iSeg - 1
			}
			goto _3
		_3:
			;
			iLvl = iLvl + 1
		}
		_fts5TokendataSetTermIfEof(tls, pPrev, pSmall)
		(*TFts5Iter)(unsafe.Pointer(pNew)).FbSkipEmpty = uint8(1)
		(*TFts5Iter)(unsafe.Pointer(pNew)).FpColset = pColset
		_fts5IterSetOutputCb(tls, p+44, pNew)
		/* Loop through all segments in the new iterator. Find the smallest
		 ** term that any segment-iterator points to. Iterator pNew will be
		 ** used for this term. Also, set any iterator that points to a term that
		 ** does not match pToken/nToken to point to EOF */
		pSmall = uintptr(0)
		ii = 0
		for {
			if !(ii < (*TFts5Iter)(unsafe.Pointer(pNew)).FnSeg) {
				break
			}
			pII = pNew + 80 + uintptr(ii)*104
			if 0 == _fts5IsTokendataPrefix(tls, pII+72, pToken, nToken) {
				_fts5SegIterSetEOF(tls, pII)
			}
			if (*TFts5SegIter)(unsafe.Pointer(pII)).FpLeaf != 0 && (!(pSmall != 0) || _fts5BufferCompare(tls, pSmall, pII+72) > 0) {
				pSmall = pII + 72
			}
			goto _5
		_5:
			;
			ii = ii + 1
		}
		/* If pSmall is still NULL at this point, then the new iterator does
		 ** not point to any terms that match the query. So delete it and break
		 ** out of the loop - all required iterators have been collected.  */
		if pSmall == uintptr(0) {
			_fts5IterClose(tls, pNew)
			break
		}
		/* Append this iterator to the set and continue. */
		pSet = _fts5AppendTokendataIter(tls, p, pSet, pNew)
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && pSet != 0 {
		ii1 = 0
		for {
			if !(int64(ii1) < (*TFts5TokenDataIter)(unsafe.Pointer(pSet)).FnIter) {
				break
			}
			pIter = *(*uintptr)(unsafe.Pointer(pSet + 56 + uintptr(ii1)*4))
			iSeg1 = 0
			for {
				if !(iSeg1 < (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg) {
					break
				}
				(*(*TFts5SegIter)(unsafe.Pointer(pIter + 80 + uintptr(iSeg1)*104))).Fflags |= int32(FTS5_SEGITER_ONETERM)
				goto _7
			_7:
				;
				iSeg1 = iSeg1 + 1
			}
			_fts5MultiIterFinishSetup(tls, p, pIter)
			goto _6
		_6:
			;
			ii1 = ii1 + 1
		}
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		pRet = _fts5MultiIterAlloc(tls, p, 0)
	}
	if pRet != 0 {
		(*TFts5Iter)(unsafe.Pointer(pRet)).FnSeg = 0
		(*TFts5Iter)(unsafe.Pointer(pRet)).FpTokenDataIter = pSet
		if pSet != 0 {
			_fts5IterSetOutputsTokendata(tls, pRet)
		} else {
			(*TFts5Iter)(unsafe.Pointer(pRet)).Fbase.FbEof = uint8(1)
		}
	} else {
		_fts5TokendataIterDelete(tls, pSet)
	}
	_fts5StructureRelease(tls, pStruct)
	_sqlite3Fts5BufferFree(tls, bp)
	return pRet
}

// C documentation
//
//	/*
//	** Implementation of snippet() function.
//	*/
func _fts5SnippetFunction(tls *libc.TLS, pApi uintptr, pFts uintptr, pCtx uintptr, nVal int32, apVal uintptr) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var aSeen, zEllips, zErr uintptr
	var i, iBestCol, iBestStart, iCol, ii, jj, nBestScore, nCol, nPhrase, v4 int32
	var nToken Ti64
	var v1, v2, v3 int64
	var _ /* ctx at bp+0 */ THighlightContext
	var _ /* iAdj at bp+128 */ int32
	var _ /* ic at bp+120 */ int32
	var _ /* io at bp+124 */ int32
	var _ /* ip at bp+116 */ int32
	var _ /* nColSize at bp+76 */ int32
	var _ /* nDoc at bp+108 */ int32
	var _ /* nDocsize at bp+112 */ int32
	var _ /* nInst at bp+72 */ int32
	var _ /* nLoc at bp+104 */ int32
	var _ /* nLoc at bp+140 */ int32
	var _ /* nScore at bp+132 */ int32
	var _ /* pLoc at bp+100 */ uintptr
	var _ /* pLoc at bp+136 */ uintptr
	var _ /* rc at bp+68 */ int32
	var _ /* sFinder at bp+80 */ TFts5SFinder
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aSeen, i, iBestCol, iBestStart, iCol, ii, jj, nBestScore, nCol, nPhrase, nToken, zEllips, zErr, v1, v2, v3, v4
	**(**int32)(__ccgo_up(bp + 68)) = SQLITE_OK /* 5th argument to snippet() */
	**(**int32)(__ccgo_up(bp + 72)) = 0         /* Column containing best snippet */
	iBestStart = 0                              /* First token of best snippet */
	nBestScore = 0                              /* Score of best snippet */
	**(**int32)(__ccgo_up(bp + 76)) = 0
	if nVal != int32(5) {
		zErr = __ccgo_ts + 38443
		Xsqlite3_result_error(tls, pCtx, zErr, -int32(1))
		return
	}
	nCol = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnCount})))(tls, pFts)
	libc.Xmemset(tls, bp, 0, uint32(68))
	iCol = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(apVal)))
	(**(**THighlightContext)(__ccgo_up(bp))).FzOpen = _fts5ValueToText(tls, **(**uintptr)(__ccgo_up(apVal + 1*4)))
	(**(**THighlightContext)(__ccgo_up(bp))).FzClose = _fts5ValueToText(tls, **(**uintptr)(__ccgo_up(apVal + 2*4)))
	(**(**THighlightContext)(__ccgo_up(bp))).FiRangeEnd = -int32(1)
	zEllips = _fts5ValueToText(tls, **(**uintptr)(__ccgo_up(apVal + 3*4)))
	if Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apVal + 4*4))) > int64(libc.Int32FromInt32(0)) {
		v2 = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apVal + 4*4)))
	} else {
		v2 = int64(libc.Int32FromInt32(0))
	}
	if v2 < int64(libc.Int32FromInt32(64)) {
		if Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apVal + 4*4))) > int64(libc.Int32FromInt32(0)) {
			v3 = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apVal + 4*4)))
		} else {
			v3 = int64(libc.Int32FromInt32(0))
		}
		v1 = v3
	} else {
		v1 = int64(libc.Int32FromInt32(64))
	}
	nToken = int64(int32(v1))
	if iCol >= 0 {
		v4 = iCol
	} else {
		v4 = 0
	}
	iBestCol = v4
	nPhrase = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxPhraseCount})))(tls, pFts)
	aSeen = Xsqlite3_malloc64(tls, uint64(nPhrase))
	if aSeen == uintptr(0) {
		**(**int32)(__ccgo_up(bp + 68)) = int32(SQLITE_NOMEM)
	}
	if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInstCount})))(tls, pFts, bp+72)
	}
	libc.Xmemset(tls, bp+80, 0, uint32(20))
	i = 0
	for {
		if !(i < nCol) {
			break
		}
		if iCol < 0 || iCol == i {
			**(**uintptr)(__ccgo_up(bp + 100)) = uintptr(0) /* Locale of column iCol */
			**(**int32)(__ccgo_up(bp + 104)) = 0
			(**(**TFts5SFinder)(__ccgo_up(bp + 80))).FiPos = 0
			(**(**TFts5SFinder)(__ccgo_up(bp + 80))).FnFirst = 0
			**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnText})))(tls, pFts, i, bp+80+16, bp+108)
			if **(**int32)(__ccgo_up(bp + 68)) != SQLITE_OK {
				break
			}
			**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnLocale})))(tls, pFts, i, bp+100, bp+104)
			if **(**int32)(__ccgo_up(bp + 68)) != SQLITE_OK {
				break
			}
			**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxTokenize_v2})))(tls, pFts, (**(**TFts5SFinder)(__ccgo_up(bp + 80))).FzDoc, **(**int32)(__ccgo_up(bp + 108)), **(**uintptr)(__ccgo_up(bp + 100)), **(**int32)(__ccgo_up(bp + 104)), bp+80, __ccgo_fp(_fts5SentenceFinderCb))
			if **(**int32)(__ccgo_up(bp + 68)) != SQLITE_OK {
				break
			}
			**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnSize})))(tls, pFts, i, bp+112)
			if **(**int32)(__ccgo_up(bp + 68)) != SQLITE_OK {
				break
			}
			ii = 0
			for {
				if !(**(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK && ii < **(**int32)(__ccgo_up(bp + 72))) {
					break
				}
				**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInst})))(tls, pFts, ii, bp+116, bp+120, bp+124)
				if **(**int32)(__ccgo_up(bp + 120)) != i {
					goto _6
				}
				if **(**int32)(__ccgo_up(bp + 124)) > **(**int32)(__ccgo_up(bp + 112)) {
					**(**int32)(__ccgo_up(bp + 68)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
				}
				if **(**int32)(__ccgo_up(bp + 68)) != SQLITE_OK {
					goto _6
				}
				libc.Xmemset(tls, aSeen, 0, uint32(nPhrase))
				**(**int32)(__ccgo_up(bp + 68)) = _fts5SnippetScore(tls, pApi, pFts, **(**int32)(__ccgo_up(bp + 112)), aSeen, i, **(**int32)(__ccgo_up(bp + 124)), int32(nToken), bp+132, bp+128)
				if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK && **(**int32)(__ccgo_up(bp + 132)) > nBestScore {
					nBestScore = **(**int32)(__ccgo_up(bp + 132))
					iBestCol = i
					iBestStart = **(**int32)(__ccgo_up(bp + 128))
					**(**int32)(__ccgo_up(bp + 76)) = **(**int32)(__ccgo_up(bp + 112))
				}
				if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK && (**(**TFts5SFinder)(__ccgo_up(bp + 80))).FnFirst != 0 && int64(**(**int32)(__ccgo_up(bp + 112))) > nToken {
					jj = 0
					for {
						if !(jj < (**(**TFts5SFinder)(__ccgo_up(bp + 80))).FnFirst-int32(1)) {
							break
						}
						if **(**int32)(__ccgo_up((**(**TFts5SFinder)(__ccgo_up(bp + 80))).FaFirst + uintptr(jj+int32(1))*4)) > **(**int32)(__ccgo_up(bp + 124)) {
							break
						}
						goto _7
					_7:
						;
						jj = jj + 1
					}
					if **(**int32)(__ccgo_up((**(**TFts5SFinder)(__ccgo_up(bp + 80))).FaFirst + uintptr(jj)*4)) < **(**int32)(__ccgo_up(bp + 124)) {
						libc.Xmemset(tls, aSeen, 0, uint32(nPhrase))
						**(**int32)(__ccgo_up(bp + 68)) = _fts5SnippetScore(tls, pApi, pFts, **(**int32)(__ccgo_up(bp + 112)), aSeen, i, **(**int32)(__ccgo_up((**(**TFts5SFinder)(__ccgo_up(bp + 80))).FaFirst + uintptr(jj)*4)), int32(nToken), bp+132, uintptr(0))
						if **(**int32)(__ccgo_up((**(**TFts5SFinder)(__ccgo_up(bp + 80))).FaFirst + uintptr(jj)*4)) == 0 {
							v4 = int32(120)
						} else {
							v4 = int32(100)
						}
						**(**int32)(__ccgo_up(bp + 132)) = **(**int32)(__ccgo_up(bp + 132)) + v4
						if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK && **(**int32)(__ccgo_up(bp + 132)) > nBestScore {
							nBestScore = **(**int32)(__ccgo_up(bp + 132))
							iBestCol = i
							iBestStart = **(**int32)(__ccgo_up((**(**TFts5SFinder)(__ccgo_up(bp + 80))).FaFirst + uintptr(jj)*4))
							**(**int32)(__ccgo_up(bp + 76)) = **(**int32)(__ccgo_up(bp + 112))
						}
					}
				}
				goto _6
			_6:
				;
				ii = ii + 1
			}
		}
		goto _5
	_5:
		;
		i = i + 1
	}
	if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnText})))(tls, pFts, iBestCol, bp+16, bp+20)
	}
	if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK && **(**int32)(__ccgo_up(bp + 76)) == 0 {
		**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnSize})))(tls, pFts, iBestCol, bp+76)
	}
	if (**(**THighlightContext)(__ccgo_up(bp))).FzIn != 0 {
		**(**uintptr)(__ccgo_up(bp + 136)) = uintptr(0) /* Locale of column iBestCol */
		**(**int32)(__ccgo_up(bp + 140)) = 0            /* Bytes in pLoc */
		if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp + 68)) = _fts5CInstIterInit(tls, pApi, pFts, iBestCol, bp+24)
		}
		(**(**THighlightContext)(__ccgo_up(bp))).FiRangeStart = iBestStart
		(**(**THighlightContext)(__ccgo_up(bp))).FiRangeEnd = int32(int64(iBestStart) + nToken - int64(1))
		if iBestStart > 0 {
			_fts5HighlightAppend(tls, bp+68, bp, zEllips, -int32(1))
		}
		/* Advance iterator ctx.iter so that it points to the first coalesced
		 ** phrase instance at or following position iBestStart. */
		for (**(**THighlightContext)(__ccgo_up(bp))).Fiter.FiStart >= 0 && (**(**THighlightContext)(__ccgo_up(bp))).Fiter.FiStart < iBestStart && **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp + 68)) = _fts5CInstIterNext(tls, bp+24)
		}
		if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnLocale})))(tls, pFts, iBestCol, bp+136, bp+140)
		}
		if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp + 68)) = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxTokenize_v2})))(tls, pFts, (**(**THighlightContext)(__ccgo_up(bp))).FzIn, (**(**THighlightContext)(__ccgo_up(bp))).FnIn, **(**uintptr)(__ccgo_up(bp + 136)), **(**int32)(__ccgo_up(bp + 140)), bp, __ccgo_fp(_fts5HighlightCb))
		}
		if (**(**THighlightContext)(__ccgo_up(bp))).FbOpen != 0 {
			_fts5HighlightAppend(tls, bp+68, bp, (**(**THighlightContext)(__ccgo_up(bp))).FzClose, -int32(1))
		}
		if (**(**THighlightContext)(__ccgo_up(bp))).FiRangeEnd >= **(**int32)(__ccgo_up(bp + 76))-int32(1) {
			_fts5HighlightAppend(tls, bp+68, bp, (**(**THighlightContext)(__ccgo_up(bp))).FzIn+uintptr((**(**THighlightContext)(__ccgo_up(bp))).FiOff), (**(**THighlightContext)(__ccgo_up(bp))).FnIn-(**(**THighlightContext)(__ccgo_up(bp))).FiOff)
		} else {
			_fts5HighlightAppend(tls, bp+68, bp, zEllips, -int32(1))
		}
	}
	if **(**int32)(__ccgo_up(bp + 68)) == SQLITE_OK {
		Xsqlite3_result_text(tls, pCtx, (**(**THighlightContext)(__ccgo_up(bp))).FzOut, -int32(1), uintptr(-libc.Int32FromInt32(1)))
	} else {
		Xsqlite3_result_error_code(tls, pCtx, **(**int32)(__ccgo_up(bp + 68)))
	}
	Xsqlite3_free(tls, (**(**THighlightContext)(__ccgo_up(bp))).FzOut)
	Xsqlite3_free(tls, aSeen)
	Xsqlite3_free(tls, (**(**TFts5SFinder)(__ccgo_up(bp + 80))).FaFirst)
}

/************************************************************************/

// C documentation
//
//	/*
//	** If a row with rowid iDel is present in the %_content table, add the
//	** delete-markers to the FTS index necessary to delete it. Do not actually
//	** remove the %_content row at this time though.
//	**
//	** If parameter bSaveRow is true, then Fts5Storage.pSavedRow is left
//	** pointing to a statement (FTS5_STMT_LOOKUP2) that may be used to access
//	** the original values of the row being deleted. This is used by UPDATE
//	** statements.
//	*/
func _fts5StorageDeleteFromIndex(tls *libc.TLS, p uintptr, iDel Ti64, apVal uintptr, bSaveRow int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var iCol, rc, rc2 int32
	var pConfig, pFree, pVal, v2 uintptr
	var _ /* ctx at bp+4 */ TFts5InsertCtx
	var _ /* nLoc at bp+28 */ int32
	var _ /* nText at bp+20 */ int32
	var _ /* pLoc at bp+24 */ uintptr
	var _ /* pSeek at bp+0 */ uintptr
	var _ /* pText at bp+16 */ uintptr
	_, _, _, _, _, _, _ = iCol, pConfig, pFree, pVal, rc, rc2, v2
	pConfig = (*TFts5Storage)(unsafe.Pointer(p)).FpConfig
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* SELECT to read row iDel from %_data */
	rc = SQLITE_OK
	if apVal == uintptr(0) {
		if (*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow != 0 && bSaveRow != 0 {
			**(**uintptr)(__ccgo_up(bp)) = (*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow
			(*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow = uintptr(0)
		} else {
			rc = _fts5StorageGetStmt(tls, p, int32(FTS5_STMT_LOOKUP)+bSaveRow, bp, uintptr(0))
			if rc != SQLITE_OK {
				return rc
			}
			Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(1), iDel)
			if Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) != int32(SQLITE_ROW) {
				return Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp)))
			}
		}
	}
	(**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FpStorage = p
	(**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol = -int32(1)
	iCol = int32(1)
	for {
		if !(rc == SQLITE_OK && iCol <= (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
			break
		}
		if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr(iCol-int32(1))))) == 0 {
			pVal = uintptr(0)
			pFree = uintptr(0)
			**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)
			**(**int32)(__ccgo_up(bp + 20)) = 0
			**(**uintptr)(__ccgo_up(bp + 24)) = uintptr(0)
			**(**int32)(__ccgo_up(bp + 28)) = 0
			if **(**uintptr)(__ccgo_up(bp)) != 0 {
				pVal = Xsqlite3_column_value(tls, **(**uintptr)(__ccgo_up(bp)), iCol)
			} else {
				pVal = **(**uintptr)(__ccgo_up(apVal + uintptr(iCol-int32(1))*4))
			}
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale != 0 && _sqlite3Fts5IsLocaleValue(tls, pConfig, pVal) != 0 {
				rc = _sqlite3Fts5DecodeLocaleValue(tls, pVal, bp+16, bp+20, bp+24, bp+28)
			} else {
				if Xsqlite3_value_type(tls, pVal) != int32(SQLITE_TEXT) {
					/* Make a copy of the value to work with. This is because the call
					 ** to sqlite3_value_text() below forces the type of the value to
					 ** SQLITE_TEXT, and we may need to use it again later. */
					v2 = Xsqlite3_value_dup(tls, pVal)
					pVal = v2
					pFree = v2
					if pVal == uintptr(0) {
						rc = int32(SQLITE_NOMEM)
					}
				}
				if rc == SQLITE_OK {
					**(**uintptr)(__ccgo_up(bp + 16)) = Xsqlite3_value_text(tls, pVal)
					**(**int32)(__ccgo_up(bp + 20)) = Xsqlite3_value_bytes(tls, pVal)
					if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale != 0 && **(**uintptr)(__ccgo_up(bp)) != 0 {
						**(**uintptr)(__ccgo_up(bp + 24)) = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), iCol+(*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)
						**(**int32)(__ccgo_up(bp + 28)) = Xsqlite3_column_bytes(tls, **(**uintptr)(__ccgo_up(bp)), iCol+(*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)
					}
				}
			}
			if rc == SQLITE_OK {
				_sqlite3Fts5SetLocale(tls, pConfig, **(**uintptr)(__ccgo_up(bp + 24)), **(**int32)(__ccgo_up(bp + 28)))
				(**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FszCol = 0
				rc = _sqlite3Fts5Tokenize(tls, pConfig, int32(FTS5_TOKENIZE_DOCUMENT), **(**uintptr)(__ccgo_up(bp + 16)), **(**int32)(__ccgo_up(bp + 20)), bp+4, __ccgo_fp(_fts5StorageInsertCallback))
				**(**Ti64)(__ccgo_up((*TFts5Storage)(unsafe.Pointer(p)).FaTotalSize + uintptr(iCol-int32(1))*8)) -= int64((**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FszCol)
				if rc == SQLITE_OK && **(**Ti64)(__ccgo_up((*TFts5Storage)(unsafe.Pointer(p)).FaTotalSize + uintptr(iCol-int32(1))*8)) < 0 {
					rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
				}
				_sqlite3Fts5ClearLocale(tls, pConfig)
			}
			Xsqlite3_value_free(tls, pFree)
		}
		goto _1
	_1:
		;
		iCol = iCol + 1
	}
	if rc == SQLITE_OK && (*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow < int64(1) {
		rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
	} else {
		(*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow = (*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow - 1
	}
	if rc == SQLITE_OK && bSaveRow != 0 {
		(*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow = **(**uintptr)(__ccgo_up(bp))
	} else {
		rc2 = Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp)))
		if rc == SQLITE_OK {
			rc = rc2
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Prepare the two insert statements - Fts5Storage.pInsertContent and
//	** Fts5Storage.pInsertDocsize - if they have not already been prepared.
//	** Return SQLITE_OK if successful, or an SQLite error code if an error
//	** occurs.
//	*/
func _fts5StorageGetStmt(tls *libc.TLS, p uintptr, eStmt int32, ppStmt uintptr, pzErrMsg uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var azStmt [12]uintptr
	var f, i int32
	var pC, zBind, zSql, v2 uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _ = azStmt, f, i, pC, zBind, zSql, v2
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	/* If there is no %_docsize table, there should be no requests for
	 ** statements to operate on it.  */
	if **(**uintptr)(__ccgo_up(p + 32 + uintptr(eStmt)*4)) == uintptr(0) {
		azStmt = [12]uintptr{
			0:  __ccgo_ts + 42000,
			1:  __ccgo_ts + 42068,
			2:  __ccgo_ts + 42137,
			3:  __ccgo_ts + 42137,
			4:  __ccgo_ts + 42170,
			5:  __ccgo_ts + 42209,
			6:  __ccgo_ts + 42249,
			7:  __ccgo_ts + 42288,
			8:  __ccgo_ts + 42331,
			9:  __ccgo_ts + 42370,
			10: __ccgo_ts + 42414,
			11: __ccgo_ts + 42454,
		}
		pC = (*TFts5Storage)(unsafe.Pointer(p)).FpConfig
		zSql = uintptr(0)
		switch eStmt {
		case int32(FTS5_STMT_SCAN):
			zSql = Xsqlite3_mprintf(tls, azStmt[eStmt], libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pC)).FzContentExprlist, (*TFts5Config)(unsafe.Pointer(pC)).FzContent))
		case FTS5_STMT_SCAN_ASC:
			fallthrough
		case int32(FTS5_STMT_SCAN_DESC):
			zSql = Xsqlite3_mprintf(tls, azStmt[eStmt], libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pC)).FzContentExprlist, (*TFts5Config)(unsafe.Pointer(pC)).FzContent, (*TFts5Config)(unsafe.Pointer(pC)).FzContentRowid, (*TFts5Config)(unsafe.Pointer(pC)).FzContentRowid, (*TFts5Config)(unsafe.Pointer(pC)).FzContentRowid))
		case int32(FTS5_STMT_LOOKUP):
			fallthrough
		case int32(FTS5_STMT_LOOKUP2):
			zSql = Xsqlite3_mprintf(tls, azStmt[eStmt], libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pC)).FzContentExprlist, (*TFts5Config)(unsafe.Pointer(pC)).FzContent, (*TFts5Config)(unsafe.Pointer(pC)).FzContentRowid))
		case int32(FTS5_STMT_INSERT_CONTENT):
			fallthrough
		case int32(FTS5_STMT_REPLACE_CONTENT):
			zBind = uintptr(0)
			/* Add bindings for the "c*" columns - those that store the actual
			 ** table content. If eContent==NORMAL, then there is one binding
			 ** for each column. Or, if eContent==UNINDEXED, then there are only
			 ** bindings for the UNINDEXED columns. */
			i = 0
			for {
				if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && i < (*TFts5Config)(unsafe.Pointer(pC)).FnCol+int32(1)) {
					break
				}
				if !(i != 0) || (*TFts5Config)(unsafe.Pointer(pC)).FeContent == FTS5_CONTENT_NORMAL || **(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pC)).FabUnindexed + uintptr(i-int32(1)))) != 0 {
					if zBind != 0 {
						v2 = __ccgo_ts + 15563
					} else {
						v2 = __ccgo_ts + 1711
					}
					zBind = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+42477, libc.VaList(bp+16, zBind, v2, i+int32(1)))
				}
				goto _1
			_1:
				;
				i = i + 1
			}
			/* Add bindings for any "l*" columns. Only non-UNINDEXED columns
			 ** require these.  */
			if (*TFts5Config)(unsafe.Pointer(pC)).FbLocale != 0 && (*TFts5Config)(unsafe.Pointer(pC)).FeContent == FTS5_CONTENT_NORMAL {
				i = 0
				for {
					if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && i < (*TFts5Config)(unsafe.Pointer(pC)).FnCol) {
						break
					}
					if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pC)).FabUnindexed + uintptr(i)))) == 0 {
						zBind = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+42485, libc.VaList(bp+16, zBind, (*TFts5Config)(unsafe.Pointer(pC)).FnCol+i+int32(2)))
					}
					goto _3
				_3:
					;
					i = i + 1
				}
			}
			zSql = _sqlite3Fts5Mprintf(tls, bp, azStmt[eStmt], libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pC)).FzDb, (*TFts5Config)(unsafe.Pointer(pC)).FzName, zBind))
			Xsqlite3_free(tls, zBind)
		case int32(FTS5_STMT_REPLACE_DOCSIZE):
			if (*TFts5Config)(unsafe.Pointer(pC)).FbContentlessDelete != 0 {
				v2 = __ccgo_ts + 42492
			} else {
				v2 = __ccgo_ts + 1711
			}
			zSql = Xsqlite3_mprintf(tls, azStmt[eStmt], libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pC)).FzDb, (*TFts5Config)(unsafe.Pointer(pC)).FzName, v2))
		case int32(FTS5_STMT_LOOKUP_DOCSIZE):
			if (*TFts5Config)(unsafe.Pointer(pC)).FbContentlessDelete != 0 {
				v2 = __ccgo_ts + 42495
			} else {
				v2 = __ccgo_ts + 1711
			}
			zSql = Xsqlite3_mprintf(tls, azStmt[eStmt], libc.VaList(bp+16, v2, (*TFts5Config)(unsafe.Pointer(pC)).FzDb, (*TFts5Config)(unsafe.Pointer(pC)).FzName))
		default:
			zSql = Xsqlite3_mprintf(tls, azStmt[eStmt], libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pC)).FzDb, (*TFts5Config)(unsafe.Pointer(pC)).FzName))
			break
		}
		if zSql == uintptr(0) {
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
		} else {
			f = int32(SQLITE_PREPARE_PERSISTENT)
			if eStmt > int32(FTS5_STMT_LOOKUP2) {
				f = f | int32(SQLITE_PREPARE_NO_VTAB)
			}
			(*TFts5Config)(unsafe.Pointer((*TFts5Storage)(unsafe.Pointer(p)).FpConfig)).FbLock = (*TFts5Config)(unsafe.Pointer((*TFts5Storage)(unsafe.Pointer(p)).FpConfig)).FbLock + 1
			**(**int32)(__ccgo_up(bp)) = Xsqlite3_prepare_v3(tls, (*TFts5Config)(unsafe.Pointer(pC)).Fdb, zSql, -int32(1), uint32(f), p+32+uintptr(eStmt)*4, uintptr(0))
			(*TFts5Config)(unsafe.Pointer((*TFts5Storage)(unsafe.Pointer(p)).FpConfig)).FbLock = (*TFts5Config)(unsafe.Pointer((*TFts5Storage)(unsafe.Pointer(p)).FpConfig)).FbLock - 1
			Xsqlite3_free(tls, zSql)
			if **(**int32)(__ccgo_up(bp)) != SQLITE_OK && pzErrMsg != 0 {
				**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+16, Xsqlite3_errmsg(tls, (*TFts5Config)(unsafe.Pointer(pC)).Fdb)))
			}
			if **(**int32)(__ccgo_up(bp)) == int32(SQLITE_ERROR) && eStmt > int32(FTS5_STMT_LOOKUP2) && eStmt < int32(FTS5_STMT_SCAN) {
				/* One of the internal tables - not the %_content table - is missing.
				 ** This counts as a corrupted table.  */
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_CORRUPT)
			}
		}
	}
	**(**uintptr)(__ccgo_up(ppStmt)) = **(**uintptr)(__ccgo_up(p + 32 + uintptr(eStmt)*4))
	Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(ppStmt)))
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Add a level to the Fts5Structure.aLevel[] array of structure object
//	** (*ppStruct).
//	*/
func _fts5StructureAddLevel(tls *libc.TLS, pRc uintptr, ppStruct uintptr) {
	var nByte Tsqlite3_int64
	var nLevel int32
	var pStruct uintptr
	_, _, _ = nByte, nLevel, pStruct
	_fts5StructureMakeWritable(tls, pRc, ppStruct)
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK {
		pStruct = **(**uintptr)(__ccgo_up(ppStruct))
		nLevel = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel
		nByte = int64(uint32(libc.UintptrFromInt32(0)+32) + uint32(nLevel+libc.Int32FromInt32(2))*libc.Uint32FromInt64(12))
		pStruct = Xsqlite3_realloc64(tls, pStruct, uint64(nByte))
		if pStruct != 0 {
			libc.Xmemset(tls, pStruct+32+uintptr(nLevel)*12, 0, uint32(12))
			(*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel + 1
			**(**uintptr)(__ccgo_up(ppStruct)) = pStruct
		} else {
			**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
		}
	}
}

// C documentation
//
//	/*
//	** Deserialize and return the structure record currently stored in serialized
//	** form within buffer pData/nData.
//	**
//	** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
//	** are over-allocated by one slot. This allows the structure contents
//	** to be more easily edited.
//	**
//	** If an error occurs, *ppOut is set to NULL and an SQLite error code
//	** returned. Otherwise, *ppOut is set to point to the new object and
//	** SQLITE_OK returned.
//	*/
func _fts5StructureDecode(tls *libc.TLS, pData uintptr, nData int32, piCookie uintptr, ppOut uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bStructureV2, i, iLvl, iSeg int32
	var nByte Tsqlite3_int64
	var nOriginCntr Tu64
	var pLvl, pRet, pSeg uintptr
	var v3 uint64
	var _ /* nLevel at bp+4 */ int32
	var _ /* nSegment at bp+8 */ int32
	var _ /* nTotal at bp+12 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _ = bStructureV2, i, iLvl, iSeg, nByte, nOriginCntr, pLvl, pRet, pSeg, v3
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	i = 0
	**(**int32)(__ccgo_up(bp + 4)) = 0
	**(**int32)(__ccgo_up(bp + 8)) = 0 /* Bytes of space to allocate at pRet */
	pRet = uintptr(0)                  /* Structure object to return */
	bStructureV2 = 0                   /* True for FTS5_STRUCTURE_V2 */
	nOriginCntr = uint64(0)            /* Largest origin value seen so far */
	/* Grab the cookie value */
	if piCookie != 0 {
		**(**int32)(__ccgo_up(piCookie)) = _sqlite3Fts5Get32(tls, pData)
	}
	i = int32(4)
	/* Check if this is a V2 structure record. Set bStructureV2 if it is. */
	if 0 == libc.Xmemcmp(tls, pData+uintptr(i), __ccgo_ts+40325, uint32(4)) {
		i = i + int32(4)
		bStructureV2 = int32(1)
	}
	/* Read the total number of levels and segments from the start of the
	 ** structure record.  */
	i = i + _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), bp+4)
	i = i + _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), bp+8)
	if **(**int32)(__ccgo_up(bp + 4)) > int32(FTS5_MAX_SEGMENT) || **(**int32)(__ccgo_up(bp + 4)) < 0 || **(**int32)(__ccgo_up(bp + 8)) > int32(FTS5_MAX_SEGMENT) || **(**int32)(__ccgo_up(bp + 8)) < 0 {
		return libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
	}
	nByte = int64(uint32(libc.UintptrFromInt32(0)+32) + uint32(**(**int32)(__ccgo_up(bp + 4)))*libc.Uint32FromInt64(12))
	pRet = _sqlite3Fts5MallocZero(tls, bp, nByte)
	if pRet != 0 {
		(*TFts5Structure)(unsafe.Pointer(pRet)).FnRef = int32(1)
		(*TFts5Structure)(unsafe.Pointer(pRet)).FnLevel = **(**int32)(__ccgo_up(bp + 4))
		(*TFts5Structure)(unsafe.Pointer(pRet)).FnSegment = **(**int32)(__ccgo_up(bp + 8))
		i = i + int32(_sqlite3Fts5GetVarint(tls, pData+uintptr(i), pRet+8))
		iLvl = 0
		for {
			if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && iLvl < **(**int32)(__ccgo_up(bp + 4))) {
				break
			}
			pLvl = pRet + 32 + uintptr(iLvl)*12
			**(**int32)(__ccgo_up(bp + 12)) = 0
			if i >= nData {
				**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
			} else {
				i = i + _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), pLvl)
				i = i + _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), bp+12)
				if **(**int32)(__ccgo_up(bp + 12)) < (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge {
					**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
				}
				(*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg = _sqlite3Fts5MallocZero(tls, bp, int64(**(**int32)(__ccgo_up(bp + 12)))*int64(56))
				**(**int32)(__ccgo_up(bp + 8)) = **(**int32)(__ccgo_up(bp + 8)) - **(**int32)(__ccgo_up(bp + 12))
			}
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				(*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg = **(**int32)(__ccgo_up(bp + 12))
				iSeg = 0
				for {
					if !(iSeg < **(**int32)(__ccgo_up(bp + 12))) {
						break
					}
					pSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSeg)*56
					if i >= nData {
						**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
						break
					}
					i = i + _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), pSeg)
					i = i + _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), pSeg+4)
					i = i + _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), pSeg+8)
					if bStructureV2 != 0 {
						i = i + int32(_sqlite3Fts5GetVarint(tls, pData+uintptr(i), pSeg+16))
						i = i + int32(_sqlite3Fts5GetVarint(tls, pData+uintptr(i), pSeg+24))
						i = i + _sqlite3Fts5GetVarint32(tls, pData+uintptr(i), pSeg+32)
						i = i + int32(_sqlite3Fts5GetVarint(tls, pData+uintptr(i), pSeg+40))
						i = i + int32(_sqlite3Fts5GetVarint(tls, pData+uintptr(i), pSeg+48))
						if nOriginCntr > (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2 {
							v3 = nOriginCntr
						} else {
							v3 = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2
						}
						nOriginCntr = v3
					}
					if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast < (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst {
						**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
						break
					}
					goto _2
				_2:
					;
					iSeg = iSeg + 1
				}
				if iLvl > 0 && (**(**TFts5StructureLevel)(__ccgo_up(pLvl + uintptr(-libc.Int32FromInt32(1))*12))).FnMerge != 0 && **(**int32)(__ccgo_up(bp + 12)) == 0 {
					**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
				}
				if iLvl == **(**int32)(__ccgo_up(bp + 4))-int32(1) && (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge != 0 {
					**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
				}
			}
			goto _1
		_1:
			;
			iLvl = iLvl + 1
		}
		if **(**int32)(__ccgo_up(bp + 8)) != 0 && **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		}
		if bStructureV2 != 0 {
			(*TFts5Structure)(unsafe.Pointer(pRet)).FnOriginCntr = nOriginCntr + uint64(1)
		}
		if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
			_fts5StructureRelease(tls, pRet)
			pRet = uintptr(0)
		}
	}
	**(**uintptr)(__ccgo_up(ppOut)) = pRet
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Extend level iLvl so that there is room for at least nExtra more
//	** segments.
//	*/
func _fts5StructureExtendLevel(tls *libc.TLS, pRc uintptr, pStruct uintptr, iLvl int32, nExtra int32, bInsert int32) {
	var aNew, pLvl uintptr
	var nByte Tsqlite3_int64
	var nMove int32
	_, _, _, _ = aNew, nByte, nMove, pLvl
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK {
		pLvl = pStruct + 32 + uintptr(iLvl)*12
		nByte = int64(uint32((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg+nExtra) * uint32(56))
		aNew = Xsqlite3_realloc64(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg, uint64(nByte))
		if aNew != 0 {
			if bInsert == 0 {
				libc.Xmemset(tls, aNew+uintptr((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg)*56, 0, uint32(56)*uint32(nExtra))
			} else {
				nMove = int32(uint32((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg) * uint32(56))
				libc.Xmemmove(tls, aNew+uintptr(nExtra)*56, aNew, uint32(nMove))
				libc.Xmemset(tls, aNew, 0, uint32(56)*uint32(nExtra))
			}
			(*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg = aNew
		} else {
			**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
		}
	}
}

// C documentation
//
//	/*
//	** Ensure that structure object (*pp) is writable.
//	**
//	** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. If
//	** an error occurs, (*pRc) is set to an SQLite error code before returning.
//	*/
func _fts5StructureMakeWritable(tls *libc.TLS, pRc uintptr, pp uintptr) {
	var i int32
	var nByte Ti64
	var p, pLvl, pNew uintptr
	_, _, _, _, _ = i, nByte, p, pLvl, pNew
	p = **(**uintptr)(__ccgo_up(pp))
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK && (*TFts5Structure)(unsafe.Pointer(p)).FnRef > int32(1) {
		nByte = int64(uint32(libc.UintptrFromInt32(0)+32) + uint32((*TFts5Structure)(unsafe.Pointer(p)).FnLevel)*libc.Uint32FromInt64(12))
		pNew = _sqlite3Fts5MallocZero(tls, pRc, nByte)
		if pNew != 0 {
			libc.Xmemcpy(tls, pNew, p, uint32(nByte))
			i = 0
			for {
				if !(i < (*TFts5Structure)(unsafe.Pointer(p)).FnLevel) {
					break
				}
				(*(*TFts5StructureLevel)(unsafe.Pointer(pNew + 32 + uintptr(i)*12))).FaSeg = uintptr(0)
				goto _1
			_1:
				;
				i = i + 1
			}
			i = 0
			for {
				if !(i < (*TFts5Structure)(unsafe.Pointer(p)).FnLevel) {
					break
				}
				pLvl = pNew + 32 + uintptr(i)*12
				nByte = int64(uint32(56) * uint32((*(*TFts5StructureLevel)(unsafe.Pointer(pNew + 32 + uintptr(i)*12))).FnSeg))
				(*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg = _sqlite3Fts5MallocZero(tls, pRc, nByte)
				if (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg == uintptr(0) {
					i = 0
					for {
						if !(i < (*TFts5Structure)(unsafe.Pointer(p)).FnLevel) {
							break
						}
						Xsqlite3_free(tls, (*(*TFts5StructureLevel)(unsafe.Pointer(pNew + 32 + uintptr(i)*12))).FaSeg)
						goto _3
					_3:
						;
						i = i + 1
					}
					Xsqlite3_free(tls, pNew)
					return
				}
				libc.Xmemcpy(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg, (*(*TFts5StructureLevel)(unsafe.Pointer(p + 32 + uintptr(i)*12))).FaSeg, uint32(nByte))
				goto _2
			_2:
				;
				i = i + 1
			}
			(*TFts5Structure)(unsafe.Pointer(p)).FnRef = (*TFts5Structure)(unsafe.Pointer(p)).FnRef - 1
			(*TFts5Structure)(unsafe.Pointer(pNew)).FnRef = int32(1)
		}
		**(**uintptr)(__ccgo_up(pp)) = pNew
	}
}

func _fts5StructureReadUncached(tls *libc.TLS, p uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pConfig, pData uintptr
	var _ /* iCookie at bp+4 */ int32
	var _ /* pRet at bp+0 */ uintptr
	_, _ = pConfig, pData
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
	pData = _fts5DataRead(tls, p, int64(FTS5_STRUCTURE_ROWID))
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		/* TODO: Do we need this if the leaf-index is appended? Probably... */
		libc.Xmemset(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+uintptr((*TFts5Data)(unsafe.Pointer(pData)).Fnn), 0, uint32(FTS5_DATA_PADDING))
		(*TFts5Index)(unsafe.Pointer(p)).Frc = _fts5StructureDecode(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp, (*TFts5Data)(unsafe.Pointer(pData)).Fnn, bp+4, bp)
		if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			if (*TFts5Config)(unsafe.Pointer(pConfig)).Fpgsz == 0 || (*TFts5Config)(unsafe.Pointer(pConfig)).FiCookie != **(**int32)(__ccgo_up(bp + 4)) {
				(*TFts5Index)(unsafe.Pointer(p)).Frc = _sqlite3Fts5ConfigLoad(tls, pConfig, **(**int32)(__ccgo_up(bp + 4)))
			}
		} else {
			if (*TFts5Index)(unsafe.Pointer(p)).Frc == libc.Int32FromInt32(SQLITE_CORRUPT)|libc.Int32FromInt32(1)<<libc.Int32FromInt32(8) {
				_sqlite3Fts5ConfigErrmsg(tls, (*TFts5Index)(unsafe.Pointer(p)).FpConfig, __ccgo_ts+40330, libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FzName))
			}
		}
		_fts5DataRelease(tls, pData)
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK {
			_fts5StructureRelease(tls, **(**uintptr)(__ccgo_up(bp)))
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		}
	}
	return **(**uintptr)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Serialize and store the "structure" record.
//	**
//	** If an error occurs, leave an error code in the Fts5Index object. If an
//	** error has already occurred, this function is a no-op.
//	*/
func _fts5StructureWrite(tls *libc.TLS, p uintptr, pStruct uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iCookie, iLvl, iSeg, nHdr, v1 int32
	var pLvl, pSeg uintptr
	var _ /* buf at bp+0 */ TFts5Buffer
	_, _, _, _, _, _, _ = iCookie, iLvl, iSeg, nHdr, pLvl, pSeg, v1
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnOriginCntr > uint64(0) {
			v1 = libc.Int32FromInt32(4) + libc.Int32FromInt32(4) + libc.Int32FromInt32(9) + libc.Int32FromInt32(9) + libc.Int32FromInt32(9)
		} else {
			v1 = libc.Int32FromInt32(4) + libc.Int32FromInt32(9) + libc.Int32FromInt32(9)
		} /* Cookie value to store */
		nHdr = v1
		libc.Xmemset(tls, bp, 0, uint32(12))
		/* Append the current configuration cookie */
		iCookie = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FiCookie
		if iCookie < 0 {
			iCookie = 0
		}
		if 0 == _sqlite3Fts5BufferSize(tls, p+44, bp, uint32(nHdr)) {
			_sqlite3Fts5Put32(tls, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp, iCookie)
			(**(**TFts5Buffer)(__ccgo_up(bp))).Fn = int32(4)
			if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnOriginCntr > uint64(0) {
				libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), __ccgo_ts+40325, uint32(4))
				**(**int32)(__ccgo_up(bp + 4)) += int32(4)
			}
			**(**int32)(__ccgo_up(bp + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), uint64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel))
			**(**int32)(__ccgo_up(bp + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), uint64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment))
			**(**int32)(__ccgo_up(bp + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), uint64(int64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnWriteCounter)))
		}
		iLvl = 0
		for {
			if !(iLvl < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) {
				break
			} /* Used to iterate through segments */
			pLvl = pStruct + 32 + uintptr(iLvl)*12
			_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge))
			_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg))
			iSeg = 0
			for {
				if !(iSeg < (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg) {
					break
				}
				pSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSeg)*56
				_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid))
				_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst))
				_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast))
				if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnOriginCntr > uint64(0) {
					_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin1))
					_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2))
					_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone))
					_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntryTombstone))
					_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntry))
				}
				goto _3
			_3:
				;
				iSeg = iSeg + 1
			}
			goto _2
		_2:
			;
			iLvl = iLvl + 1
		}
		_fts5DataWrite(tls, p, int64(FTS5_STRUCTURE_ROWID), (**(**TFts5Buffer)(__ccgo_up(bp))).Fp, (**(**TFts5Buffer)(__ccgo_up(bp))).Fn)
		_sqlite3Fts5BufferFree(tls, bp)
	}
}

// C documentation
//
//	/*
//	** Append a mapping to the token-map belonging to object pT.
//	*/
func _fts5TokendataIterAppendMap(tls *libc.TLS, p uintptr, pT uintptr, iIter int32, nByte int32, iRowid Ti64, iPos Ti64) {
	var aNew uintptr
	var nAlloc, nNew Ti64
	var v1 int64
	_, _, _, _ = aNew, nAlloc, nNew, v1
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		if (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap == (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc {
			if (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc != 0 {
				v1 = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc * int64(2)
			} else {
				v1 = int64(64)
			}
			nNew = v1
			nAlloc = nNew * int64(24)
			aNew = Xsqlite3_realloc64(tls, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap, uint64(nAlloc))
			if aNew == uintptr(0) {
				(*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
				return
			}
			(*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap = aNew
			(*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMapAlloc = nNew
		}
		(**(**TFts5TokenDataMap)(__ccgo_up((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FiRowid = iRowid
		(**(**TFts5TokenDataMap)(__ccgo_up((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FiPos = iPos
		(**(**TFts5TokenDataMap)(__ccgo_up((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FiIter = iIter
		(**(**TFts5TokenDataMap)(__ccgo_up((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap)*24))).FnByte = nByte
		(*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap + 1
	}
}

// C documentation
//
//	/*
//	** The iterator passed as the only argument must be a tokendata=1 iterator
//	** (pIter->pTokenDataIter!=0). This function advances the iterator. If
//	** argument bFrom is false, then the iterator is advanced to the next
//	** entry. Or, if bFrom is true, it is advanced to the first entry with
//	** a rowid of iFrom or greater.
//	*/
func _fts5TokendataIterNext(tls *libc.TLS, pIter uintptr, bFrom int32, iFrom Ti64) {
	var ii int32
	var p, pIndex, pT uintptr
	_, _, _, _ = ii, p, pIndex, pT
	pT = (*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter
	pIndex = (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex
	ii = 0
	for {
		if !(int64(ii) < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter) {
			break
		}
		p = *(*uintptr)(unsafe.Pointer(pT + 56 + uintptr(ii)*4))
		if int32((*TFts5Iter)(unsafe.Pointer(p)).Fbase.FbEof) == 0 && ((*TFts5Iter)(unsafe.Pointer(p)).Fbase.FiRowid == (*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FiRowid || bFrom != 0 && (*TFts5Iter)(unsafe.Pointer(p)).Fbase.FiRowid < iFrom) {
			_fts5MultiIterNext(tls, pIndex, p, bFrom, iFrom)
			for bFrom != 0 && int32((*TFts5Iter)(unsafe.Pointer(p)).Fbase.FbEof) == 0 && (*TFts5Iter)(unsafe.Pointer(p)).Fbase.FiRowid < iFrom && (*TFts5Index)(unsafe.Pointer(pIndex)).Frc == SQLITE_OK {
				_fts5MultiIterNext(tls, pIndex, p, 0, 0)
			}
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	if (*TFts5Index)(unsafe.Pointer(pIndex)).Frc == SQLITE_OK {
		_fts5IterSetOutputsTokendata(tls, pIter)
	}
}

// C documentation
//
//	/*
//	** Sort the contents of the pT->aMap[] array.
//	**
//	** The sorting algorithm requires a malloc(). If this fails, an error code
//	** is left in Fts5Index.rc before returning.
//	*/
func _fts5TokendataIterSortMap(tls *libc.TLS, p uintptr, pT uintptr) {
	var a1, a2, aTmp, tmp uintptr
	var i1, n1, n2 int32
	var nByte, nHalf Ti64
	var v3, v4 int64
	_, _, _, _, _, _, _, _, _, _, _ = a1, a2, aTmp, i1, n1, n2, nByte, nHalf, tmp, v3, v4
	aTmp = uintptr(0)
	nByte = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap * int64(24)
	aTmp = _sqlite3Fts5MallocZero(tls, p+44, nByte)
	if aTmp != 0 {
		a1 = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap
		a2 = aTmp
		nHalf = int64(1)
		for {
			if !(nHalf < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap) {
				break
			}
			i1 = 0
			for {
				if !(int64(i1) < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap) {
					break
				}
				if nHalf < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap-int64(i1) {
					v3 = nHalf
				} else {
					v3 = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap - int64(i1)
				}
				n1 = int32(v3)
				if nHalf < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap-int64(i1)-int64(n1) {
					v4 = nHalf
				} else {
					v4 = (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap - int64(i1) - int64(n1)
				}
				n2 = int32(v4)
				_fts5TokendataMerge(tls, a1+uintptr(i1)*24, n1, a1+uintptr(i1+n1)*24, n2, a2+uintptr(i1)*24)
				goto _2
			_2:
				;
				i1 = int32(int64(i1) + nHalf*libc.Int64FromInt32(2))
			}
			tmp = a1
			a1 = a2
			a2 = tmp
			goto _1
		_1:
			;
			nHalf = nHalf * int64(2)
		}
		if a1 != (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap {
			libc.Xmemcpy(tls, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FaMap, a1, uint32((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnMap*int64(24)))
		}
		Xsqlite3_free(tls, aTmp)
	}
}

// C documentation
//
//	/*
//	** Allocate a trigram tokenizer.
//	*/
func _fts5TriCreate(tls *libc.TLS, pUnused uintptr, azArg uintptr, nArg int32, ppOut uintptr) (r int32) {
	var i, rc, v2 int32
	var pNew, zArg uintptr
	_, _, _, _, _ = i, pNew, rc, zArg, v2
	rc = SQLITE_OK
	pNew = uintptr(0)
	_ = pUnused
	if nArg%int32(2) != 0 {
		rc = int32(SQLITE_ERROR)
	} else {
		pNew = Xsqlite3_malloc64(tls, uint64(8))
		if pNew == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		} else {
			(*TTrigramTokenizer)(unsafe.Pointer(pNew)).FbFold = int32(1)
			(*TTrigramTokenizer)(unsafe.Pointer(pNew)).FiFoldParam = 0
			i = 0
			for {
				if !(rc == SQLITE_OK && i < nArg) {
					break
				}
				zArg = **(**uintptr)(__ccgo_up(azArg + uintptr(i+int32(1))*4))
				if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4)), __ccgo_ts+43427) {
					if int32(**(**int8)(__ccgo_up(zArg))) != int32('0') && int32(**(**int8)(__ccgo_up(zArg))) != int32('1') || **(**int8)(__ccgo_up(zArg + 1)) != 0 {
						rc = int32(SQLITE_ERROR)
					} else {
						(*TTrigramTokenizer)(unsafe.Pointer(pNew)).FbFold = libc.BoolInt32(int32(**(**int8)(__ccgo_up(zArg))) == int32('0'))
					}
				} else {
					if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4)), __ccgo_ts+43113) {
						if int32(**(**int8)(__ccgo_up(zArg))) != int32('0') && int32(**(**int8)(__ccgo_up(zArg))) != int32('1') && int32(**(**int8)(__ccgo_up(zArg))) != int32('2') || **(**int8)(__ccgo_up(zArg + 1)) != 0 {
							rc = int32(SQLITE_ERROR)
						} else {
							if int32(**(**int8)(__ccgo_up(zArg))) != int32('0') {
								v2 = int32(2)
							} else {
								v2 = 0
							}
							(*TTrigramTokenizer)(unsafe.Pointer(pNew)).FiFoldParam = v2
						}
					} else {
						rc = int32(SQLITE_ERROR)
					}
				}
				goto _1
			_1:
				;
				i = i + int32(2)
			}
			if (*TTrigramTokenizer)(unsafe.Pointer(pNew)).FiFoldParam != 0 && (*TTrigramTokenizer)(unsafe.Pointer(pNew)).FbFold == 0 {
				rc = int32(SQLITE_ERROR)
			}
			if rc != SQLITE_OK {
				_fts5TriDelete(tls, pNew)
				pNew = uintptr(0)
			}
		}
	}
	**(**uintptr)(__ccgo_up(ppOut)) = pNew
	return rc
}

// C documentation
//
//	/*
//	** Trigram tokenizer tokenize routine.
//	*/
func _fts5TriTokenize(tls *libc.TLS, pTok uintptr, pCtx uintptr, unusedFlags int32, pText uintptr, nText int32, __ccgo_fp_xToken uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aStart [3]int32
	var iCode Tu32
	var iNext, ii, rc int32
	var p, z1, zEof, zIn, zOut, v1 uintptr
	var _ /* aBuf at bp+0 */ [32]int8
	_, _, _, _, _, _, _, _, _, _, _ = aStart, iCode, iNext, ii, p, rc, z1, zEof, zIn, zOut, v1
	p = pTok
	rc = SQLITE_OK
	zOut = bp
	zIn = pText
	if zIn != 0 {
		v1 = zIn + uintptr(nText)
	} else {
		v1 = uintptr(0)
	}
	zEof = v1
	iCode = uint32(0) /* Input offset of each character in aBuf[] */
	_ = unusedFlags
	/* Populate aBuf[] with the characters for the first trigram. */
	ii = 0
	for {
		if !(ii < int32(3)) {
			break
		}
		for cond := true; cond; cond = iCode == uint32(0) {
			aStart[ii] = int32(zIn) - int32(pText)
			if zIn >= zEof {
				return SQLITE_OK
			}
			v1 = zIn
			zIn = zIn + 1
			iCode = uint32(**(**uint8)(__ccgo_up(v1)))
			if iCode >= uint32(0xc0) {
				iCode = uint32(_sqlite3Utf8Trans1[iCode-uint32(0xc0)])
				for zIn < zEof && int32(**(**uint8)(__ccgo_up(zIn)))&int32(0xc0) == int32(0x80) {
					v1 = zIn
					zIn = zIn + 1
					iCode = iCode<<libc.Int32FromInt32(6) + uint32(libc.Int32FromInt32(0x3f)&int32(**(**uint8)(__ccgo_up(v1))))
				}
				if iCode < uint32(0x80) || iCode&uint32(0xFFFFF800) == uint32(0xD800) || iCode&uint32(0xFFFFFFFE) == uint32(0xFFFE) {
					iCode = uint32(0xFFFD)
				}
			}
			if (*TTrigramTokenizer)(unsafe.Pointer(p)).FbFold != 0 {
				iCode = uint32(_sqlite3Fts5UnicodeFold(tls, int32(iCode), (*TTrigramTokenizer)(unsafe.Pointer(p)).FiFoldParam))
			}
		}
		if iCode < uint32(0x00080) {
			v1 = zOut
			zOut = zOut + 1
			**(**int8)(__ccgo_up(v1)) = int8(uint8(iCode & libc.Uint32FromInt32(0xFF)))
		} else {
			if iCode < uint32(0x00800) {
				v1 = zOut
				zOut = zOut + 1
				**(**int8)(__ccgo_up(v1)) = int8(int32(0xC0) + int32(uint8(iCode>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1F))))
				v1 = zOut
				zOut = zOut + 1
				**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode&libc.Uint32FromInt32(0x3F))))
			} else {
				if iCode < uint32(0x10000) {
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0xE0) + int32(uint8(iCode>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0F))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode&libc.Uint32FromInt32(0x3F))))
				} else {
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0xF0) + int32(uint8(iCode>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3F))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode&libc.Uint32FromInt32(0x3F))))
				}
			}
		}
		goto _2
	_2:
		;
		ii = ii + 1
	}
	/* At the start of each iteration of this loop:
	 **
	 **  aBuf:      Contains 3 characters. The 3 characters of the next trigram.
	 **  zOut:      Points to the byte following the last character in aBuf.
	 **  aStart[3]: Contains the byte offset in the input text corresponding
	 **             to the start of each of the three characters in the buffer.
	 */
	for int32(1) != 0 {
		/* Read characters from the input up until the first non-diacritic */
		for cond := true; cond; cond = iCode == uint32(0) {
			iNext = int32(zIn) - int32(pText)
			if zIn >= zEof {
				iCode = uint32(0)
				break
			}
			v1 = zIn
			zIn = zIn + 1
			iCode = uint32(**(**uint8)(__ccgo_up(v1)))
			if iCode >= uint32(0xc0) {
				iCode = uint32(_sqlite3Utf8Trans1[iCode-uint32(0xc0)])
				for zIn < zEof && int32(**(**uint8)(__ccgo_up(zIn)))&int32(0xc0) == int32(0x80) {
					v1 = zIn
					zIn = zIn + 1
					iCode = iCode<<libc.Int32FromInt32(6) + uint32(libc.Int32FromInt32(0x3f)&int32(**(**uint8)(__ccgo_up(v1))))
				}
				if iCode < uint32(0x80) || iCode&uint32(0xFFFFF800) == uint32(0xD800) || iCode&uint32(0xFFFFFFFE) == uint32(0xFFFE) {
					iCode = uint32(0xFFFD)
				}
			}
			if (*TTrigramTokenizer)(unsafe.Pointer(p)).FbFold != 0 {
				iCode = uint32(_sqlite3Fts5UnicodeFold(tls, int32(iCode), (*TTrigramTokenizer)(unsafe.Pointer(p)).FiFoldParam))
			}
		}
		/* Pass the current trigram back to fts5 */
		rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, int32, int32, int32) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xToken})))(tls, pCtx, 0, bp, int32(zOut)-t__predefined_ptrdiff_t(bp), aStart[0], iNext)
		if iCode == uint32(0) || rc != SQLITE_OK {
			break
		}
		/* Remove the first character from buffer aBuf[]. Append the character
		 ** with codepoint iCode.  */
		z1 = bp
		v1 = z1
		z1 = z1 + 1
		if int32(uint8(**(**int8)(__ccgo_up(v1)))) >= int32(0xc0) {
			for int32(uint8(**(**int8)(__ccgo_up(z1))))&int32(0xc0) == int32(0x80) {
				z1 = z1 + 1
			}
		}
		libc.Xmemmove(tls, bp, z1, uint32(int32(zOut)-int32(z1)))
		zOut = zOut - uintptr(int32(z1)-t__predefined_ptrdiff_t(bp))
		if iCode < uint32(0x00080) {
			v1 = zOut
			zOut = zOut + 1
			**(**int8)(__ccgo_up(v1)) = int8(uint8(iCode & libc.Uint32FromInt32(0xFF)))
		} else {
			if iCode < uint32(0x00800) {
				v1 = zOut
				zOut = zOut + 1
				**(**int8)(__ccgo_up(v1)) = int8(int32(0xC0) + int32(uint8(iCode>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1F))))
				v1 = zOut
				zOut = zOut + 1
				**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode&libc.Uint32FromInt32(0x3F))))
			} else {
				if iCode < uint32(0x10000) {
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0xE0) + int32(uint8(iCode>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0F))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode&libc.Uint32FromInt32(0x3F))))
				} else {
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0xF0) + int32(uint8(iCode>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3F))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8(int32(0x80) + int32(uint8(iCode&libc.Uint32FromInt32(0x3F))))
				}
			}
		}
		/* Update the aStart[] array */
		aStart[0] = aStart[int32(1)]
		aStart[int32(1)] = aStart[int32(2)]
		aStart[int32(2)] = iNext
	}
	return rc
}

// C documentation
//
//	/*
//	** Iterator pIter was used to iterate through the input segments of on an
//	** incremental merge operation. This function is called if the incremental
//	** merge step has finished but the input has not been completely exhausted.
//	*/
func _fts5TrimSegments(tls *libc.TLS, p uintptr, pIter uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, iId, iOff, nDiff int32
	var iLeafRowid Ti64
	var pData, pSeg uintptr
	var _ /* aHdr at bp+12 */ [4]Tu8
	var _ /* buf at bp+0 */ TFts5Buffer
	_, _, _, _, _, _, _ = i, iId, iLeafRowid, iOff, nDiff, pData, pSeg
	libc.Xmemset(tls, bp, 0, uint32(12))
	i = 0
	for {
		if !(i < (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg && (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK) {
			break
		}
		pSeg = pIter + 80 + uintptr(i)*104
		if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg == uintptr(0) {
			/* no-op */
		} else {
			if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf == uintptr(0) {
				/* All keys from this input segment have been transfered to the output.
				 ** Set both the first and last page-numbers to 0 to indicate that the
				 ** segment is now empty. */
				(*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FpgnoLast = 0
				(*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FpgnoFirst = 0
			} else {
				iOff = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafOffset
				iId = (*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FiSegid
				**(**[4]Tu8)(__ccgo_up(bp + 12)) = [4]Tu8{}
				iLeafRowid = int64(iId)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno)
				pData = _fts5LeafRead(tls, p, iLeafRowid)
				if pData != 0 {
					if iOff > (*TFts5Data)(unsafe.Pointer(pData)).FszLeaf {
						/* This can occur if the pages that the segments occupy overlap - if
						 ** a single page has been assigned to more than one segment. In
						 ** this case a prior iteration of this loop may have corrupted the
						 ** segment currently being trimmed.  */
						_fts5IndexCorruptRowid(tls, p, iLeafRowid)
					} else {
						_sqlite3Fts5BufferZero(tls, bp)
						if !(uint32((*TFts5Buffer)(unsafe.Pointer(bp)).Fn)+uint32((*TFts5Data)(unsafe.Pointer(pData)).Fnn) <= uint32((*TFts5Buffer)(unsafe.Pointer(bp)).FnSpace)) {
							_sqlite3Fts5BufferSize(tls, p+44, bp, uint32((*TFts5Data)(unsafe.Pointer(pData)).Fnn+(*TFts5Buffer)(unsafe.Pointer(bp)).Fn))
						}
						_sqlite3Fts5BufferAppendBlob(tls, p+44, bp, uint32(4), bp+12)
						_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fn))
						_sqlite3Fts5BufferAppendBlob(tls, p+44, bp, uint32((*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fn), (*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fp)
						_sqlite3Fts5BufferAppendBlob(tls, p+44, bp, uint32((*TFts5Data)(unsafe.Pointer(pData)).FszLeaf-iOff), (*TFts5Data)(unsafe.Pointer(pData)).Fp+uintptr(iOff))
						if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
							/* Set the szLeaf field */
							_fts5PutU16(tls, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp+2, uint16((**(**TFts5Buffer)(__ccgo_up(bp))).Fn))
						}
						/* Set up the new page-index array */
						_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, libc.Int64FromInt32(4))
						if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiLeafPgno == (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno && (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiEndofDoclist < (*TFts5Data)(unsafe.Pointer(pData)).FszLeaf && (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiPgidxOff <= (*TFts5Data)(unsafe.Pointer(pData)).Fnn {
							nDiff = (*TFts5Data)(unsafe.Pointer(pData)).FszLeaf - (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiEndofDoclist
							_sqlite3Fts5BufferAppendVarint(tls, p+44, bp, int64((**(**TFts5Buffer)(__ccgo_up(bp))).Fn)-int64(1)-int64(nDiff)-int64(4))
							_sqlite3Fts5BufferAppendBlob(tls, p+44, bp, uint32((*TFts5Data)(unsafe.Pointer(pData)).Fnn-(*TFts5SegIter)(unsafe.Pointer(pSeg)).FiPgidxOff), (*TFts5Data)(unsafe.Pointer(pData)).Fp+uintptr((*TFts5SegIter)(unsafe.Pointer(pSeg)).FiPgidxOff))
						}
						(*TFts5StructureSegment)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pSeg)).FpSeg)).FpgnoFirst = (*TFts5SegIter)(unsafe.Pointer(pSeg)).FiTermLeafPgno
						_fts5DataDelete(tls, p, int64(iId)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64(libc.Int32FromInt32(1)), iLeafRowid)
						_fts5DataWrite(tls, p, iLeafRowid, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp, (**(**TFts5Buffer)(__ccgo_up(bp))).Fn)
					}
					_fts5DataRelease(tls, pData)
				}
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	_sqlite3Fts5BufferFree(tls, bp)
}

func _fts5UnicodeAddExceptions(tls *libc.TLS, p uintptr, z uintptr, bTokenChars int32) (r int32) {
	var aNew, zCsr, zTerm, v1 uintptr
	var bToken, i, n, nNew, rc int32
	var iCode Tu32
	_, _, _, _, _, _, _, _, _, _ = aNew, bToken, i, iCode, n, nNew, rc, zCsr, zTerm, v1
	rc = SQLITE_OK
	n = int32(libc.Xstrlen(tls, z))
	if n > 0 {
		aNew = Xsqlite3_realloc64(tls, (*TUnicode61Tokenizer)(unsafe.Pointer(p)).FaiException, uint64(uint32(n+(*TUnicode61Tokenizer)(unsafe.Pointer(p)).FnException)*uint32(4)))
		if aNew != 0 {
			nNew = (*TUnicode61Tokenizer)(unsafe.Pointer(p)).FnException
			zCsr = z
			zTerm = z + uintptr(n)
			for zCsr < zTerm {
				v1 = zCsr
				zCsr = zCsr + 1
				iCode = uint32(**(**uint8)(__ccgo_up(v1)))
				if iCode >= uint32(0xc0) {
					iCode = uint32(_sqlite3Utf8Trans1[iCode-uint32(0xc0)])
					for zCsr < zTerm && int32(**(**uint8)(__ccgo_up(zCsr)))&int32(0xc0) == int32(0x80) {
						v1 = zCsr
						zCsr = zCsr + 1
						iCode = iCode<<libc.Int32FromInt32(6) + uint32(libc.Int32FromInt32(0x3f)&int32(**(**uint8)(__ccgo_up(v1))))
					}
					if iCode < uint32(0x80) || iCode&uint32(0xFFFFF800) == uint32(0xD800) || iCode&uint32(0xFFFFFFFE) == uint32(0xFFFE) {
						iCode = uint32(0xFFFD)
					}
				}
				if iCode < uint32(128) {
					**(**uint8)(__ccgo_up(p + uintptr(iCode))) = uint8(bTokenChars)
				} else {
					bToken = int32(**(**uint8)(__ccgo_up(p + 148 + uintptr(_sqlite3Fts5UnicodeCategory(tls, iCode)))))
					if bToken != bTokenChars && _sqlite3Fts5UnicodeIsdiacritic(tls, int32(iCode)) == 0 {
						i = 0
						for {
							if !(i < nNew) {
								break
							}
							if uint32(**(**int32)(__ccgo_up(aNew + uintptr(i)*4))) > iCode {
								break
							}
							goto _3
						_3:
							;
							i = i + 1
						}
						libc.Xmemmove(tls, aNew+uintptr(i+int32(1))*4, aNew+uintptr(i)*4, uint32(nNew-i)*uint32(4))
						**(**int32)(__ccgo_up(aNew + uintptr(i)*4)) = int32(iCode)
						nNew = nNew + 1
					}
				}
			}
			(*TUnicode61Tokenizer)(unsafe.Pointer(p)).FaiException = aNew
			(*TUnicode61Tokenizer)(unsafe.Pointer(p)).FnException = nNew
		} else {
			rc = int32(SQLITE_NOMEM)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Create a "unicode61" tokenizer.
//	*/
func _fts5UnicodeCreate(tls *libc.TLS, pUnused uintptr, azArg uintptr, nArg int32, ppOut uintptr) (r int32) {
	var i, rc int32
	var p, zArg, zCat uintptr
	_, _, _, _, _ = i, p, rc, zArg, zCat
	rc = SQLITE_OK /* Return code */
	p = uintptr(0) /* New tokenizer object */
	_ = pUnused
	if nArg%int32(2) != 0 {
		rc = int32(SQLITE_ERROR)
	} else {
		p = Xsqlite3_malloc64(tls, uint64(180))
		if p != 0 {
			zCat = __ccgo_ts + 43093
			libc.Xmemset(tls, p, 0, uint32(180))
			(*TUnicode61Tokenizer)(unsafe.Pointer(p)).FeRemoveDiacritic = int32(FTS5_REMOVE_DIACRITICS_SIMPLE)
			(*TUnicode61Tokenizer)(unsafe.Pointer(p)).FnFold = int32(64)
			(*TUnicode61Tokenizer)(unsafe.Pointer(p)).FaFold = Xsqlite3_malloc64(tls, uint64(uint32((*TUnicode61Tokenizer)(unsafe.Pointer(p)).FnFold)*uint32(1)))
			if (*TUnicode61Tokenizer)(unsafe.Pointer(p)).FaFold == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
			}
			/* Search for a "categories" argument */
			i = 0
			for {
				if !(rc == SQLITE_OK && i < nArg) {
					break
				}
				if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4)), __ccgo_ts+43102) {
					zCat = **(**uintptr)(__ccgo_up(azArg + uintptr(i+int32(1))*4))
				}
				goto _1
			_1:
				;
				i = i + int32(2)
			}
			if rc == SQLITE_OK {
				rc = _unicodeSetCategories(tls, p, zCat)
			}
			i = 0
			for {
				if !(rc == SQLITE_OK && i < nArg) {
					break
				}
				zArg = **(**uintptr)(__ccgo_up(azArg + uintptr(i+int32(1))*4))
				if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4)), __ccgo_ts+43113) {
					if int32(**(**int8)(__ccgo_up(zArg))) != int32('0') && int32(**(**int8)(__ccgo_up(zArg))) != int32('1') && int32(**(**int8)(__ccgo_up(zArg))) != int32('2') || **(**int8)(__ccgo_up(zArg + 1)) != 0 {
						rc = int32(SQLITE_ERROR)
					} else {
						(*TUnicode61Tokenizer)(unsafe.Pointer(p)).FeRemoveDiacritic = int32(**(**int8)(__ccgo_up(zArg))) - int32('0')
					}
				} else {
					if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4)), __ccgo_ts+43071) {
						rc = _fts5UnicodeAddExceptions(tls, p, zArg, int32(1))
					} else {
						if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4)), __ccgo_ts+43082) {
							rc = _fts5UnicodeAddExceptions(tls, p, zArg, 0)
						} else {
							if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4)), __ccgo_ts+43102) {
								/* no-op */
							} else {
								rc = int32(SQLITE_ERROR)
							}
						}
					}
				}
				goto _2
			_2:
				;
				i = i + int32(2)
			}
		} else {
			rc = int32(SQLITE_NOMEM)
		}
		if rc != SQLITE_OK {
			_fts5UnicodeDelete(tls, p)
			p = uintptr(0)
		}
		**(**uintptr)(__ccgo_up(ppOut)) = p
	}
	return rc
}

// C documentation
//
//	/*
//	** Return true if, for the purposes of tokenizing with the tokenizer
//	** passed as the first argument, codepoint iCode is considered a token
//	** character (not a separator).
//	*/
func _fts5UnicodeIsAlnum(tls *libc.TLS, p uintptr, iCode int32) (r int32) {
	return int32(**(**uint8)(__ccgo_up(p + 148 + uintptr(_sqlite3Fts5UnicodeCategory(tls, uint32(iCode)))))) ^ _fts5UnicodeIsException(tls, p, iCode)
}

func _fts5UnicodeTokenize(tls *libc.TLS, pTokenizer uintptr, pCtx uintptr, iUnused int32, pText uintptr, nText int32, __ccgo_fp_xToken uintptr) (r int32) {
	var a, aFold, p, pEnd, zCsr, zOut, zTerm, v3 uintptr
	var iCode Tu32
	var ie, is, nFold, rc, v7 int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = a, aFold, iCode, ie, is, nFold, p, pEnd, rc, zCsr, zOut, zTerm, v3, v7
	p = pTokenizer
	rc = SQLITE_OK
	a = p
	zTerm = pText + uintptr(nText)
	zCsr = pText
	/* Output buffer */
	aFold = (*TUnicode61Tokenizer)(unsafe.Pointer(p)).FaFold
	nFold = (*TUnicode61Tokenizer)(unsafe.Pointer(p)).FnFold
	pEnd = aFold + uintptr(nFold-int32(6))
	_ = iUnused
	/* Each iteration of this loop gobbles up a contiguous run of separators,
	 ** then the next token.  */
_2:
	;
	if !(rc == SQLITE_OK) {
		goto _1
	} /* non-ASCII codepoint read from input */
	zOut = aFold
	/* Skip any separator characters. */
	for int32(1) != 0 {
		if zCsr >= zTerm {
			goto tokenize_done
		}
		if int32(**(**uint8)(__ccgo_up(zCsr)))&int32(0x80) != 0 {
			/* A character outside of the ascii range. Skip past it if it is
			 ** a separator character. Or break out of the loop if it is not. */
			is = int32(zCsr) - int32(pText)
			v3 = zCsr
			zCsr = zCsr + 1
			iCode = uint32(**(**uint8)(__ccgo_up(v3)))
			if iCode >= uint32(0xc0) {
				iCode = uint32(_sqlite3Utf8Trans1[iCode-uint32(0xc0)])
				for zCsr < zTerm && int32(**(**uint8)(__ccgo_up(zCsr)))&int32(0xc0) == int32(0x80) {
					v3 = zCsr
					zCsr = zCsr + 1
					iCode = iCode<<libc.Int32FromInt32(6) + uint32(libc.Int32FromInt32(0x3f)&int32(**(**uint8)(__ccgo_up(v3))))
				}
				if iCode < uint32(0x80) || iCode&uint32(0xFFFFF800) == uint32(0xD800) || iCode&uint32(0xFFFFFFFE) == uint32(0xFFFE) {
					iCode = uint32(0xFFFD)
				}
			}
			if _fts5UnicodeIsAlnum(tls, p, int32(iCode)) != 0 {
				goto non_ascii_tokenchar
			}
		} else {
			if **(**uint8)(__ccgo_up(a + uintptr(**(**uint8)(__ccgo_up(zCsr))))) != 0 {
				is = int32(zCsr) - int32(pText)
				goto ascii_tokenchar
			}
			zCsr = zCsr + 1
		}
	}
	/* Run through the tokenchars. Fold them into the output buffer along
	 ** the way.  */
_6:
	;
	if !(zCsr < zTerm) {
		goto _5
	}
	/* Grow the output buffer so that there is sufficient space to fit the
	 ** largest possible utf-8 character.  */
	if zOut > pEnd {
		aFold = Xsqlite3_malloc64(tls, uint64(int64(nFold)*int64(2)))
		if aFold == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
			goto tokenize_done
		}
		zOut = aFold + uintptr(int32(zOut)-int32((*TUnicode61Tokenizer)(unsafe.Pointer(p)).FaFold))
		libc.Xmemcpy(tls, aFold, (*TUnicode61Tokenizer)(unsafe.Pointer(p)).FaFold, uint32(nFold))
		Xsqlite3_free(tls, (*TUnicode61Tokenizer)(unsafe.Pointer(p)).FaFold)
		(*TUnicode61Tokenizer)(unsafe.Pointer(p)).FaFold = aFold
		v7 = nFold * libc.Int32FromInt32(2)
		nFold = v7
		(*TUnicode61Tokenizer)(unsafe.Pointer(p)).FnFold = v7
		pEnd = aFold + uintptr(nFold-int32(6))
	}
	if !(int32(**(**uint8)(__ccgo_up(zCsr)))&int32(0x80) != 0) {
		goto _8
	}
	/* An non-ascii-range character. Fold it into the output buffer if
	 ** it is a token character, or break out of the loop if it is not. */
	v3 = zCsr
	zCsr = zCsr + 1
	iCode = uint32(**(**uint8)(__ccgo_up(v3)))
	if iCode >= uint32(0xc0) {
		iCode = uint32(_sqlite3Utf8Trans1[iCode-uint32(0xc0)])
		for zCsr < zTerm && int32(**(**uint8)(__ccgo_up(zCsr)))&int32(0xc0) == int32(0x80) {
			v3 = zCsr
			zCsr = zCsr + 1
			iCode = iCode<<libc.Int32FromInt32(6) + uint32(libc.Int32FromInt32(0x3f)&int32(**(**uint8)(__ccgo_up(v3))))
		}
		if iCode < uint32(0x80) || iCode&uint32(0xFFFFF800) == uint32(0xD800) || iCode&uint32(0xFFFFFFFE) == uint32(0xFFFE) {
			iCode = uint32(0xFFFD)
		}
	}
	if !(_fts5UnicodeIsAlnum(tls, p, int32(iCode)) != 0 || _sqlite3Fts5UnicodeIsdiacritic(tls, int32(iCode)) != 0) {
		goto _12
	}
	goto non_ascii_tokenchar
non_ascii_tokenchar:
	;
	iCode = uint32(_sqlite3Fts5UnicodeFold(tls, int32(iCode), (*TUnicode61Tokenizer)(unsafe.Pointer(p)).FeRemoveDiacritic))
	if iCode != 0 {
		if iCode < uint32(0x00080) {
			v3 = zOut
			zOut = zOut + 1
			**(**int8)(__ccgo_up(v3)) = int8(uint8(iCode & libc.Uint32FromInt32(0xFF)))
		} else {
			if iCode < uint32(0x00800) {
				v3 = zOut
				zOut = zOut + 1
				**(**int8)(__ccgo_up(v3)) = int8(int32(0xC0) + int32(uint8(iCode>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1F))))
				v3 = zOut
				zOut = zOut + 1
				**(**int8)(__ccgo_up(v3)) = int8(int32(0x80) + int32(uint8(iCode&libc.Uint32FromInt32(0x3F))))
			} else {
				if iCode < uint32(0x10000) {
					v3 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v3)) = int8(int32(0xE0) + int32(uint8(iCode>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0F))))
					v3 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v3)) = int8(int32(0x80) + int32(uint8(iCode>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
					v3 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v3)) = int8(int32(0x80) + int32(uint8(iCode&libc.Uint32FromInt32(0x3F))))
				} else {
					v3 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v3)) = int8(int32(0xF0) + int32(uint8(iCode>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07))))
					v3 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v3)) = int8(int32(0x80) + int32(uint8(iCode>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3F))))
					v3 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v3)) = int8(int32(0x80) + int32(uint8(iCode>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
					v3 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v3)) = int8(int32(0x80) + int32(uint8(iCode&libc.Uint32FromInt32(0x3F))))
				}
			}
		}
	}
	goto _13
_12:
	;
	goto _5
_13:
	;
	goto _9
_8:
	;
	if !(int32(**(**uint8)(__ccgo_up(a + uintptr(**(**uint8)(__ccgo_up(zCsr)))))) == 0) {
		goto _24
	}
	/* An ascii-range separator character. End of token. */
	goto _5
	goto _25
_24:
	;
	goto ascii_tokenchar
ascii_tokenchar:
	;
	if int32(**(**uint8)(__ccgo_up(zCsr))) >= int32('A') && int32(**(**uint8)(__ccgo_up(zCsr))) <= int32('Z') {
		v3 = zOut
		zOut = zOut + 1
		**(**int8)(__ccgo_up(v3)) = int8(int32(**(**uint8)(__ccgo_up(zCsr))) + int32(32))
	} else {
		v3 = zOut
		zOut = zOut + 1
		**(**int8)(__ccgo_up(v3)) = int8(**(**uint8)(__ccgo_up(zCsr)))
	}
	zCsr = zCsr + 1
_25:
	;
_9:
	;
	ie = int32(zCsr) - int32(pText)
	goto _6
_5:
	;
	/* Invoke the token callback */
	rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, int32, int32, int32) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xToken})))(tls, pCtx, 0, aFold, int32(zOut)-int32(aFold), is, ie)
	goto _2
_1:
	;
	goto tokenize_done
tokenize_done:
	;
	if rc == int32(SQLITE_DONE) {
		rc = SQLITE_OK
	}
	return rc
}

/**************************************************************************
** Start of porter stemmer implementation.
 */

/* Any tokens larger than this (in bytes) are passed through without
** stemming. */

// C documentation
//
//	/*
//	** This function is the implementation of the xUpdate callback used by
//	** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
//	** inserted, updated or deleted.
//	**
//	** A delete specifies a single argument - the rowid of the row to remove.
//	**
//	** Update and insert operations pass:
//	**
//	**   1. The "old" rowid, or NULL.
//	**   2. The "new" rowid.
//	**   3. Values for each of the nCol matchable columns.
//	**   4. Values for the two hidden columns (<tablename> and "rank").
//	*/
func _fts5UpdateMethod(tls *libc.TLS, pVtab uintptr, nArg int32, apVal uintptr, pRowid uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var eConflict, eType0, eType1, ii int32
	var iDel, iNew, iNew1, iOld Ti64
	var pConfig, pStorage, pTab, pVal, z uintptr
	var _ /* bContent at bp+4 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _ = eConflict, eType0, eType1, iDel, iNew, iNew1, iOld, ii, pConfig, pStorage, pTab, pVal, z
	pTab = pVtab
	pConfig = (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig /* value_type() of apVal[0] */
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK                        /* Return code */
	/* A transaction must be open when this is called. */
	if (*TFts5Config)(unsafe.Pointer(pConfig)).Fpgsz == 0 {
		**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5ConfigLoad(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig, (*TFts5Config)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig)).FiCookie)
		if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
			return **(**int32)(__ccgo_up(bp))
		}
	}
	(*TFts5Config)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig)).FpzErrmsg = pTab + 8
	/* Put any active cursors into REQUIRE_SEEK state. */
	_fts5TripCursors(tls, pTab)
	eType0 = Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(apVal)))
	if eType0 == int32(SQLITE_NULL) && Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(apVal + uintptr(int32(2)+(*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)*4))) != int32(SQLITE_NULL) {
		/* A "special" INSERT op. These are handled separately. */
		z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(apVal + uintptr(int32(2)+(*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)*4)))
		if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent != FTS5_CONTENT_NORMAL && 0 == Xsqlite3_stricmp(tls, __ccgo_ts+20384, z) {
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete != 0 {
				_fts5SetVtabError(tls, pTab, __ccgo_ts+41522, 0)
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
			} else {
				**(**int32)(__ccgo_up(bp)) = _fts5SpecialDelete(tls, pTab, apVal)
			}
		} else {
			**(**int32)(__ccgo_up(bp)) = _fts5SpecialInsert(tls, pTab, z, **(**uintptr)(__ccgo_up(apVal + uintptr(int32(2)+(*TFts5Config)(unsafe.Pointer(pConfig)).FnCol+int32(1))*4)))
		}
	} else {
		/* A regular INSERT, UPDATE or DELETE statement. The trick here is that
		 ** any conflict on the rowid value must be detected before any
		 ** modifications are made to the database file. There are 4 cases:
		 **
		 **   1) DELETE
		 **   2) UPDATE (rowid not modified)
		 **   3) UPDATE (rowid modified)
		 **   4) INSERT
		 **
		 ** Cases 3 and 4 may violate the rowid constraint.
		 */
		eConflict = int32(SQLITE_ABORT)
		if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == FTS5_CONTENT_NORMAL || (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete != 0 {
			eConflict = Xsqlite3_vtab_on_conflict(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb)
		}
		/* DELETE */
		if nArg == int32(1) {
			/* It is only possible to DELETE from a contentless table if the
			 ** contentless_delete=1 flag is set. */
			if _fts5IsContentless(tls, pTab, int32(1)) != 0 && (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete == 0 {
				_fts5SetVtabError(tls, pTab, __ccgo_ts+41581, libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName))
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
			} else {
				iDel = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apVal))) /* Rowid to delete */
				**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageDelete(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, iDel, uintptr(0), 0)
			}
		} else {
			eType1 = Xsqlite3_value_numeric_type(tls, **(**uintptr)(__ccgo_up(apVal + 1*4)))
			/* It is an error to write an fts5_locale() value to a table without
			 ** the locale=1 option. */
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale == 0 {
				ii = 0
				for {
					if !(ii < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
						break
					}
					pVal = **(**uintptr)(__ccgo_up(apVal + uintptr(ii+int32(2))*4))
					if _sqlite3Fts5IsLocaleValue(tls, pConfig, pVal) != 0 {
						_fts5SetVtabError(tls, pTab, __ccgo_ts+41627, 0)
						**(**int32)(__ccgo_up(bp)) = int32(SQLITE_MISMATCH)
						goto update_out
					}
					goto _1
				_1:
					;
					ii = ii + 1
				}
			}
			if eType0 != int32(SQLITE_INTEGER) {
				/* An INSERT statement. If the conflict-mode is REPLACE, first remove
				 ** the current entry (if any). */
				if eConflict == int32(SQLITE_REPLACE) && eType1 == int32(SQLITE_INTEGER) {
					iNew = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apVal + 1*4))) /* Rowid to delete */
					**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageDelete(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, iNew, uintptr(0), 0)
				}
				_fts5StorageInsert(tls, bp, pTab, apVal, pRowid)
			} else {
				pStorage = (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage
				iOld = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apVal)))        /* Old rowid */
				iNew1 = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apVal + 1*4))) /* New rowid */
				**(**int32)(__ccgo_up(bp + 4)) = 0                                       /* Content only update */
				/* If this is a contentless table (including contentless_unindexed=1
				 ** tables), check if the UPDATE may proceed.  */
				if _fts5IsContentless(tls, pTab, int32(1)) != 0 {
					**(**int32)(__ccgo_up(bp)) = _fts5ContentlessUpdate(tls, pConfig, apVal+2*4, libc.BoolInt32(iOld != iNew1), bp+4)
					if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
						goto update_out
					}
				}
				if eType1 != int32(SQLITE_INTEGER) {
					**(**int32)(__ccgo_up(bp)) = int32(SQLITE_MISMATCH)
				} else {
					if iOld != iNew1 {
						if eConflict == int32(SQLITE_REPLACE) {
							**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageDelete(tls, pStorage, iOld, uintptr(0), int32(1))
							if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
								**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageDelete(tls, pStorage, iNew1, uintptr(0), 0)
							}
							_fts5StorageInsert(tls, bp, pTab, apVal, pRowid)
						} else {
							**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageFindDeleteRow(tls, pStorage, iOld)
							if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
								**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageContentInsert(tls, pStorage, 0, apVal, pRowid)
							}
							if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
								**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageDelete(tls, pStorage, iOld, uintptr(0), 0)
							}
							if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
								**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageIndexInsert(tls, pStorage, apVal, **(**Tsqlite_int64)(__ccgo_up(pRowid)))
							}
						}
					} else {
						if **(**int32)(__ccgo_up(bp + 4)) != 0 {
							/* This occurs when an UPDATE on a contentless table affects *only*
							 ** UNINDEXED columns. This is a no-op for contentless_unindexed=0
							 ** tables, or a write to the %_content table only for =1 tables.  */
							**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageFindDeleteRow(tls, pStorage, iOld)
							if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
								**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageContentInsert(tls, pStorage, int32(1), apVal, pRowid)
							}
						} else {
							**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StorageDelete(tls, pStorage, iOld, uintptr(0), int32(1))
							_fts5StorageInsert(tls, bp, pTab, apVal, pRowid)
						}
					}
				}
				_sqlite3Fts5StorageReleaseDeleteRow(tls, pStorage)
			}
		}
	}
	goto update_out
update_out:
	;
	_sqlite3Fts5IndexCloseReader(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex)
	(*TFts5Config)(unsafe.Pointer((*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig)).FpzErrmsg = uintptr(0)
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Iterate through a range of entries in the FTS index, invoking the xVisit
//	** callback for each of them.
//	**
//	** Parameter pToken points to an nToken buffer containing an FTS index term
//	** (i.e. a document term with the preceding 1 byte index identifier -
//	** FTS5_MAIN_PREFIX or similar). If bPrefix is true, then the call visits
//	** all entries for terms that have pToken/nToken as a prefix. If bPrefix
//	** is false, then only entries with pToken/nToken as the entire key are
//	** visited.
//	**
//	** If the current table is a tokendata=1 table, then if bPrefix is true then
//	** each index term is treated separately. However, if bPrefix is false, then
//	** all index terms corresponding to pToken/nToken are collapsed into a single
//	** term before the callback is invoked.
//	**
//	** The callback invoked for each entry visited is specified by paramter xVisit.
//	** Each time it is invoked, it is passed a pointer to the Fts5Index object,
//	** a copy of the 7th paramter to this function (pCtx) and a pointer to the
//	** iterator that indicates the current entry. If the current entry is the
//	** first with a new term (i.e. different from that of the previous entry,
//	** including the very first term), then the final two parameters are passed
//	** a pointer to the term and its size in bytes, respectively. If the current
//	** entry is not the first associated with its term, these two parameters
//	** are passed 0.
//	**
//	** If parameter pColset is not NULL, then it is used to filter entries before
//	** the callback is invoked.
//	*/
func _fts5VisitEntries(tls *libc.TLS, p uintptr, pColset uintptr, pToken uintptr, nToken int32, bPrefix int32, __ccgo_fp_xVisit uintptr, pCtx uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var flags, nNew, v1 int32
	var pNew, pSeg, pStruct uintptr
	var _ /* bNewTerm at bp+4 */ int32
	var _ /* p1 at bp+0 */ uintptr
	_, _, _, _, _, _ = flags, nNew, pNew, pSeg, pStruct, v1
	if bPrefix != 0 {
		v1 = int32(FTS5INDEX_QUERY_SCAN)
	} else {
		v1 = 0
	}
	flags = v1 | int32(FTS5INDEX_QUERY_SKIPEMPTY) | int32(FTS5INDEX_QUERY_NOOUTPUT)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* Iterator used to gather data from index */
	**(**int32)(__ccgo_up(bp + 4)) = int32(1)
	pStruct = _fts5StructureRead(tls, p)
	_fts5MultiIterNew(tls, p, pStruct, flags, pColset, pToken, nToken, -int32(1), 0, bp)
	_fts5IterSetOutputCb(tls, p+44, **(**uintptr)(__ccgo_up(bp)))
	for {
		if !(_fts5MultiIterEof(tls, p, **(**uintptr)(__ccgo_up(bp))) == 0) {
			break
		}
		pSeg = **(**uintptr)(__ccgo_up(bp)) + 80 + uintptr((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaFirst + 1*4))).FiFirst)*104
		nNew = 0
		pNew = uintptr(0)
		(*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FxSetOutputs})))(tls, **(**uintptr)(__ccgo_up(bp)), pSeg)
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
			break
		}
		if **(**int32)(__ccgo_up(bp + 4)) != 0 {
			nNew = (*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fn
			pNew = (*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fp
			if nNew < nToken || libc.Xmemcmp(tls, pToken, pNew, uint32(nToken)) != 0 {
				break
			}
		}
		(*(*func(*libc.TLS, uintptr, uintptr, uintptr, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xVisit})))(tls, p, pCtx, **(**uintptr)(__ccgo_up(bp)), pNew, nNew)
		goto _2
	_2:
		;
		_fts5MultiIterNext2(tls, p, **(**uintptr)(__ccgo_up(bp)), bp+4)
	}
	_fts5MultiIterFree(tls, **(**uintptr)(__ccgo_up(bp)))
	_fts5StructureRelease(tls, pStruct)
	return (*TFts5Index)(unsafe.Pointer(p)).Frc
}

/* Size in bytes of an Fts5TokenDataIter object holding up to N iterators */

// C documentation
//
//	/*
//	** This is the xFilter implementation for the virtual table.
//	*/
func _fts5VocabFilterMethod(tls *libc.TLS, pCursor uintptr, idxNum int32, zUnused uintptr, nUnused int32, apVal uintptr) (r int32) {
	var eType, f, iVal, nTerm, rc, v1 int32
	var pCsr, pEq, pGe, pIndex, pLe, pTab, zCopy, zTerm uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = eType, f, iVal, nTerm, pCsr, pEq, pGe, pIndex, pLe, pTab, rc, zCopy, zTerm, v1
	pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab
	pCsr = pCursor
	eType = (*TFts5VocabTable)(unsafe.Pointer(pTab)).FeType
	rc = SQLITE_OK
	iVal = 0
	f = int32(FTS5INDEX_QUERY_SCAN)
	zTerm = uintptr(0)
	nTerm = 0
	pEq = uintptr(0)
	pGe = uintptr(0)
	pLe = uintptr(0)
	_ = zUnused
	_ = nUnused
	_fts5VocabResetCursor(tls, pCsr)
	if idxNum&int32(FTS5_VOCAB_TERM_EQ) != 0 {
		v1 = iVal
		iVal = iVal + 1
		pEq = **(**uintptr)(__ccgo_up(apVal + uintptr(v1)*4))
	}
	if idxNum&int32(FTS5_VOCAB_TERM_GE) != 0 {
		v1 = iVal
		iVal = iVal + 1
		pGe = **(**uintptr)(__ccgo_up(apVal + uintptr(v1)*4))
	}
	if idxNum&int32(FTS5_VOCAB_TERM_LE) != 0 {
		v1 = iVal
		iVal = iVal + 1
		pLe = **(**uintptr)(__ccgo_up(apVal + uintptr(v1)*4))
	}
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FcolUsed = idxNum & int32(FTS5_VOCAB_COLUSED_MASK)
	if pEq != 0 {
		zTerm = Xsqlite3_value_text(tls, pEq)
		nTerm = Xsqlite3_value_bytes(tls, pEq)
		f = int32(FTS5INDEX_QUERY_NOTOKENDATA)
	} else {
		if pGe != 0 {
			zTerm = Xsqlite3_value_text(tls, pGe)
			nTerm = Xsqlite3_value_bytes(tls, pGe)
		}
		if pLe != 0 {
			zCopy = Xsqlite3_value_text(tls, pLe)
			if zCopy == uintptr(0) {
				zCopy = __ccgo_ts + 1711
			}
			(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm = Xsqlite3_value_bytes(tls, pLe)
			(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FzLeTerm = Xsqlite3_malloc64(tls, uint64(int64((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm)+int64(1)))
			if (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FzLeTerm == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				libc.Xmemcpy(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FzLeTerm, zCopy, uint32((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm+int32(1)))
			}
		}
	}
	if rc == SQLITE_OK {
		pIndex = (*TFts5Table)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpFts5)).FpIndex
		rc = _sqlite3Fts5IndexQuery(tls, pIndex, zTerm, nTerm, f, uintptr(0), pCsr+16)
		if rc == SQLITE_OK {
			(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpStruct = _sqlite3Fts5StructureRef(tls, pIndex)
		}
	}
	if rc == SQLITE_OK && eType == int32(FTS5_VOCAB_INSTANCE) {
		rc = _fts5VocabInstanceNewTerm(tls, pCsr)
	}
	if rc == SQLITE_OK && !((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FbEof != 0) && (eType != int32(FTS5_VOCAB_INSTANCE) || (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpFts5)).FpConfig)).FeDetail != int32(FTS5_DETAIL_NONE)) {
		rc = _fts5VocabNextMethod(tls, pCursor)
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is the implementation of both the xConnect and xCreate
//	** methods of the FTS3 virtual table.
//	**
//	** The argv[] array contains the following:
//	**
//	**   argv[0]   -> module name  ("fts5vocab")
//	**   argv[1]   -> database name
//	**   argv[2]   -> table name
//	**
//	** then:
//	**
//	**   argv[3]   -> name of fts5 table
//	**   argv[4]   -> type of fts5vocab table
//	**
//	** or, for tables in the TEMP schema only.
//	**
//	**   argv[3]   -> name of fts5 tables database
//	**   argv[4]   -> name of fts5 table
//	**   argv[5]   -> type of fts5vocab table
//	*/
func _fts5VocabInitVtab(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVTab uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var azSchema [3]uintptr
	var bDb int32
	var nByte, nDb, nTab Ti64
	var pRet, zDb, zTab, zType, v1, v2, v3 uintptr
	var _ /* eType at bp+4 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _ = azSchema, bDb, nByte, nDb, nTab, pRet, zDb, zTab, zType, v1, v2, v3
	azSchema = [3]uintptr{
		0: __ccgo_ts + 43507,
		1: __ccgo_ts + 43547,
		2: __ccgo_ts + 43582,
	}
	pRet = uintptr(0)
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	bDb = libc.BoolInt32(argc == int32(6) && libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + 1*4))) == uint32(4) && libc.Xmemcmp(tls, __ccgo_ts+26494, **(**uintptr)(__ccgo_up(argv + 1*4)), uint32(4)) == 0)
	if argc != int32(5) && bDb == 0 {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+43625, 0)
		**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
	} else {
		if bDb != 0 {
			v1 = **(**uintptr)(__ccgo_up(argv + 3*4))
		} else {
			v1 = **(**uintptr)(__ccgo_up(argv + 1*4))
		} /* Bytes of space to allocate */
		zDb = v1
		if bDb != 0 {
			v2 = **(**uintptr)(__ccgo_up(argv + 4*4))
		} else {
			v2 = **(**uintptr)(__ccgo_up(argv + 3*4))
		}
		zTab = v2
		if bDb != 0 {
			v3 = **(**uintptr)(__ccgo_up(argv + 5*4))
		} else {
			v3 = **(**uintptr)(__ccgo_up(argv + 4*4))
		}
		zType = v3
		nDb = int64(libc.Xstrlen(tls, zDb) + uint32(1))
		nTab = int64(libc.Xstrlen(tls, zTab) + uint32(1))
		**(**int32)(__ccgo_up(bp + 4)) = 0
		**(**int32)(__ccgo_up(bp)) = _fts5VocabTableType(tls, zType, pzErr, bp+4)
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp)) = Xsqlite3_declare_vtab(tls, db, azSchema[**(**int32)(__ccgo_up(bp + 4))])
		}
		nByte = int64(36) + nDb + nTab
		pRet = _sqlite3Fts5MallocZero(tls, bp, nByte)
		if pRet != 0 {
			(*TFts5VocabTable)(unsafe.Pointer(pRet)).FpGlobal = pAux
			(*TFts5VocabTable)(unsafe.Pointer(pRet)).FeType = **(**int32)(__ccgo_up(bp + 4))
			(*TFts5VocabTable)(unsafe.Pointer(pRet)).Fdb = db
			(*TFts5VocabTable)(unsafe.Pointer(pRet)).FzFts5Tbl = pRet + 1*36
			(*TFts5VocabTable)(unsafe.Pointer(pRet)).FzFts5Db = (*TFts5VocabTable)(unsafe.Pointer(pRet)).FzFts5Tbl + uintptr(nTab)
			libc.Xmemcpy(tls, (*TFts5VocabTable)(unsafe.Pointer(pRet)).FzFts5Tbl, zTab, uint32(nTab))
			libc.Xmemcpy(tls, (*TFts5VocabTable)(unsafe.Pointer(pRet)).FzFts5Db, zDb, uint32(nDb))
			_sqlite3Fts5Dequote(tls, (*TFts5VocabTable)(unsafe.Pointer(pRet)).FzFts5Tbl)
			_sqlite3Fts5Dequote(tls, (*TFts5VocabTable)(unsafe.Pointer(pRet)).FzFts5Db)
		}
	}
	**(**uintptr)(__ccgo_up(ppVTab)) = pRet
	return **(**int32)(__ccgo_up(bp))
}

func _fts5VocabInstanceNewTerm(tls *libc.TLS, pCsr uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bCmp, nCmp, v1 int32
	var zTerm uintptr
	var _ /* nTerm at bp+4 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _ = bCmp, nCmp, zTerm, v1
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	if (*TFts5IndexIter)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter)).FbEof != 0 {
		(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FbEof = int32(1)
	} else {
		zTerm = _sqlite3Fts5IterTerm(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter, bp+4)
		if (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm >= 0 {
			if **(**int32)(__ccgo_up(bp + 4)) < (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm {
				v1 = **(**int32)(__ccgo_up(bp + 4))
			} else {
				v1 = (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm
			}
			nCmp = v1
			bCmp = libc.Xmemcmp(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FzLeTerm, zTerm, uint32(nCmp))
			if bCmp < 0 || bCmp == 0 && (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm < **(**int32)(__ccgo_up(bp + 4)) {
				(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FbEof = int32(1)
			}
		}
		_sqlite3Fts5BufferSet(tls, bp, pCsr+56, **(**int32)(__ccgo_up(bp + 4)), zTerm)
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Advance the cursor to the next row in the table.
//	*/
func _fts5VocabNextMethod(tls *libc.TLS, pCursor uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bCmp, eDetail, iCol, ii1, nCmp, nCol, nPos, v2 int32
	var pCsr, pPos, pTab, zTerm uintptr
	var v3 Ti64
	var _ /* iOff at bp+16 */ int32
	var _ /* iPos at bp+8 */ Ti64
	var _ /* ii at bp+20 */ Tu32
	var _ /* nTerm at bp+4 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _ = bCmp, eDetail, iCol, ii1, nCmp, nCol, nPos, pCsr, pPos, pTab, zTerm, v2, v3
	pCsr = pCursor
	pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab
	nCol = (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpFts5)).FpConfig)).FnCol
	**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5StructureTest(tls, (*TFts5Table)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpFts5)).FpIndex, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpStruct)
	if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
		return **(**int32)(__ccgo_up(bp))
	}
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).Frowid = (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).Frowid + 1
	if (*TFts5VocabTable)(unsafe.Pointer(pTab)).FeType == int32(FTS5_VOCAB_INSTANCE) {
		return _fts5VocabInstanceNext(tls, pCsr)
	}
	if (*TFts5VocabTable)(unsafe.Pointer(pTab)).FeType == FTS5_VOCAB_COL {
		(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol = (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol + 1
		for {
			if !((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol < nCol) {
				break
			}
			if **(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc + uintptr((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol)*8)) != 0 {
				break
			}
			goto _1
		_1:
			;
			(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol = (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol + 1
		}
	}
	if (*TFts5VocabTable)(unsafe.Pointer(pTab)).FeType != FTS5_VOCAB_COL || (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol >= nCol {
		if (*TFts5IndexIter)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter)).FbEof != 0 {
			(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FbEof = int32(1)
		} else {
			zTerm = _sqlite3Fts5IterTerm(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter, bp+4)
			if (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm >= 0 {
				if **(**int32)(__ccgo_up(bp + 4)) < (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm {
					v2 = **(**int32)(__ccgo_up(bp + 4))
				} else {
					v2 = (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm
				}
				nCmp = v2
				bCmp = libc.Xmemcmp(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FzLeTerm, zTerm, uint32(nCmp))
				if bCmp < 0 || bCmp == 0 && (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm < **(**int32)(__ccgo_up(bp + 4)) {
					(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FbEof = int32(1)
					return SQLITE_OK
				}
			}
			_sqlite3Fts5BufferSet(tls, bp, pCsr+56, **(**int32)(__ccgo_up(bp + 4)), zTerm)
			libc.Xmemset(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaCnt, 0, uint32(nCol)*uint32(8))
			libc.Xmemset(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc, 0, uint32(nCol)*uint32(8))
			(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol = 0
			for **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				eDetail = (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpFts5)).FpConfig)).FeDetail /* Position list */
				**(**Ti64)(__ccgo_up(bp + 8)) = 0                                                                                                           /* 64-bit position read from poslist */
				**(**int32)(__ccgo_up(bp + 16)) = 0                                                                                                         /* Current offset within position list */
				pPos = (*TFts5IndexIter)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter)).FpData
				nPos = (*TFts5IndexIter)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter)).FnData
				switch (*TFts5VocabTable)(unsafe.Pointer(pTab)).FeType {
				case int32(FTS5_VOCAB_ROW):
					/* Do not bother counting the number of instances if the "cnt"
					 ** column is not being read (according to colUsed).  */
					if eDetail == FTS5_DETAIL_FULL && (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FcolUsed&int32(0x04) != 0 {
						for **(**Ti64)(__ccgo_up(bp + 8)) < int64(nPos) {
							v3 = **(**Ti64)(__ccgo_up(bp + 8))
							**(**Ti64)(__ccgo_up(bp + 8)) = **(**Ti64)(__ccgo_up(bp + 8)) + 1
							**(**Tu32)(__ccgo_up(bp + 20)) = uint32(**(**Tu8)(__ccgo_up(pPos + uintptr(v3))))
							if **(**Tu32)(__ccgo_up(bp + 20))&uint32(0x80) != 0 {
								**(**Ti64)(__ccgo_up(bp + 8)) = **(**Ti64)(__ccgo_up(bp + 8)) - 1
								**(**Ti64)(__ccgo_up(bp + 8)) = **(**Ti64)(__ccgo_up(bp + 8)) + int64(_sqlite3Fts5GetVarint32(tls, pPos+uintptr(**(**Ti64)(__ccgo_up(bp + 8))), bp+20))
							}
							if **(**Tu32)(__ccgo_up(bp + 20)) == uint32(1) {
								/* New column in the position list */
								v3 = **(**Ti64)(__ccgo_up(bp + 8))
								**(**Ti64)(__ccgo_up(bp + 8)) = **(**Ti64)(__ccgo_up(bp + 8)) + 1
								**(**Tu32)(__ccgo_up(bp + 20)) = uint32(**(**Tu8)(__ccgo_up(pPos + uintptr(v3))))
								if **(**Tu32)(__ccgo_up(bp + 20))&uint32(0x80) != 0 {
									**(**Ti64)(__ccgo_up(bp + 8)) = **(**Ti64)(__ccgo_up(bp + 8)) - 1
									**(**Ti64)(__ccgo_up(bp + 8)) = **(**Ti64)(__ccgo_up(bp + 8)) + int64(_sqlite3Fts5GetVarint32(tls, pPos+uintptr(**(**Ti64)(__ccgo_up(bp + 8))), bp+20))
								}
							} else {
								/* An instance - increment pCsr->aCnt[] */
								**(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaCnt)) = **(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaCnt)) + 1
							}
						}
					}
					**(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc)) = **(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc)) + 1
				case FTS5_VOCAB_COL:
					if eDetail == FTS5_DETAIL_FULL {
						iCol = -int32(1)
						for 0 == _sqlite3Fts5PoslistNext64(tls, pPos, nPos, bp+16, bp+8) {
							ii1 = int32(**(**Ti64)(__ccgo_up(bp + 8)) >> libc.Int32FromInt32(32) & libc.Int64FromInt32(0x7FFFFFFF))
							if iCol != ii1 {
								if ii1 >= nCol {
									**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
									break
								}
								**(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc + uintptr(ii1)*8)) = **(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc + uintptr(ii1)*8)) + 1
								iCol = ii1
							}
							**(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaCnt + uintptr(ii1)*8)) = **(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaCnt + uintptr(ii1)*8)) + 1
						}
					} else {
						if eDetail == int32(FTS5_DETAIL_COLUMNS) {
							for 0 == _sqlite3Fts5PoslistNext64(tls, pPos, nPos, bp+16, bp+8) {
								if **(**Ti64)(__ccgo_up(bp + 8)) >= int64(nCol) {
									**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
									break
								}
								**(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc + uintptr(**(**Ti64)(__ccgo_up(bp + 8)))*8)) = **(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc + uintptr(**(**Ti64)(__ccgo_up(bp + 8)))*8)) + 1
							}
						} else {
							**(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc)) = **(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc)) + 1
						}
					}
				default:
					break
				}
				if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
					**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5IterNextScan(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter)
				}
				if (*TFts5VocabTable)(unsafe.Pointer(pTab)).FeType == int32(FTS5_VOCAB_INSTANCE) {
					break
				}
				if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
					zTerm = _sqlite3Fts5IterTerm(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter, bp+4)
					if **(**int32)(__ccgo_up(bp + 4)) != (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).Fterm.Fn || **(**int32)(__ccgo_up(bp + 4)) > 0 && libc.Xmemcmp(tls, zTerm, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).Fterm.Fp, uint32(**(**int32)(__ccgo_up(bp + 4)))) != 0 {
						break
					}
					if (*TFts5IndexIter)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter)).FbEof != 0 {
						break
					}
				}
			}
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FbEof == 0 && (*TFts5VocabTable)(unsafe.Pointer(pTab)).FeType == FTS5_VOCAB_COL {
		for {
			if !((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol < nCol && **(**Ti64)(__ccgo_up((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc + uintptr((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol)*8)) == 0) {
				break
			}
			goto _5
		_5:
			;
			(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol = (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol + 1
		}
		if (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol == nCol {
			**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		}
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Implementation of xOpen method.
//	*/
func _fts5VocabOpenMethod(tls *libc.TLS, pVTab uintptr, ppCsr uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var iId, nByte Ti64
	var pCsr, pFts5, pTab, zSql uintptr
	var _ /* pStmt at bp+4 */ uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _ = iId, nByte, pCsr, pFts5, pTab, zSql
	pTab = pVTab
	pFts5 = uintptr(0)
	pCsr = uintptr(0)
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	zSql = uintptr(0)
	if (*TFts5VocabTable)(unsafe.Pointer(pTab)).FbBusy != 0 {
		(*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+43658, libc.VaList(bp+16, (*TFts5VocabTable)(unsafe.Pointer(pTab)).FzFts5Db, (*TFts5VocabTable)(unsafe.Pointer(pTab)).FzFts5Tbl))
		return int32(SQLITE_ERROR)
	}
	zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+43689, libc.VaList(bp+16, (*TFts5VocabTable)(unsafe.Pointer(pTab)).FzFts5Tbl, (*TFts5VocabTable)(unsafe.Pointer(pTab)).FzFts5Db, (*TFts5VocabTable)(unsafe.Pointer(pTab)).FzFts5Tbl, (*TFts5VocabTable)(unsafe.Pointer(pTab)).FzFts5Tbl))
	if zSql != 0 {
		**(**int32)(__ccgo_up(bp)) = Xsqlite3_prepare_v2(tls, (*TFts5VocabTable)(unsafe.Pointer(pTab)).Fdb, zSql, -int32(1), bp+4, uintptr(0))
	}
	Xsqlite3_free(tls, zSql)
	if **(**int32)(__ccgo_up(bp)) == int32(SQLITE_ERROR) {
		**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	}
	(*TFts5VocabTable)(unsafe.Pointer(pTab)).FbBusy = uint32(1)
	if **(**uintptr)(__ccgo_up(bp + 4)) != 0 && Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4))) == int32(SQLITE_ROW) {
		iId = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp + 4)), 0)
		pFts5 = _sqlite3Fts5TableFromCsrid(tls, (*TFts5VocabTable)(unsafe.Pointer(pTab)).FpGlobal, iId)
	}
	(*TFts5VocabTable)(unsafe.Pointer(pTab)).FbBusy = uint32(0)
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		if pFts5 == uintptr(0) {
			**(**int32)(__ccgo_up(bp)) = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 4)))
			**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				(*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+43740, libc.VaList(bp+16, (*TFts5VocabTable)(unsafe.Pointer(pTab)).FzFts5Db, (*TFts5VocabTable)(unsafe.Pointer(pTab)).FzFts5Tbl))
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
			}
		} else {
			**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5FlushToDisk(tls, pFts5)
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		nByte = int64(uint32((*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pFts5)).FpConfig)).FnCol)*uint32(8)*uint32(2) + uint32(88))
		pCsr = _sqlite3Fts5MallocZero(tls, bp, nByte)
	}
	if pCsr != 0 {
		(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpFts5 = pFts5
		(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpStmt = **(**uintptr)(__ccgo_up(bp + 4))
		(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaCnt = pCsr + 1*88
		(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc = (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaCnt + uintptr((*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer(pFts5)).FpConfig)).FnCol)*8
	} else {
		Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 4)))
	}
	**(**uintptr)(__ccgo_up(ppCsr)) = pCsr
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Restore cursor pCsr to the state it was in immediately after being
//	** created by the xOpen() method.
//	*/
func _fts5VocabResetCursor(tls *libc.TLS, pCsr uintptr) {
	var nCol int32
	_ = nCol
	nCol = (*TFts5Config)(unsafe.Pointer((*TFts5Table)(unsafe.Pointer((*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpFts5)).FpConfig)).FnCol
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).Frowid = 0
	_sqlite3Fts5IterClose(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter)
	_sqlite3Fts5StructureRelease(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpStruct)
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpStruct = uintptr(0)
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FpIter = uintptr(0)
	Xsqlite3_free(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FzLeTerm)
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FnLeTerm = -int32(1)
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FzLeTerm = uintptr(0)
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FbEof = 0
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiCol = 0
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiInstPos = 0
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FiInstOff = 0
	(*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FcolUsed = 0
	libc.Xmemset(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaCnt, 0, uint32(8)*uint32(nCol))
	libc.Xmemset(tls, (*TFts5VocabCursor)(unsafe.Pointer(pCsr)).FaDoc, 0, uint32(8)*uint32(nCol))
}

func _fts5WriteAppendPoslistData(tls *libc.TLS, p uintptr, pWriter uintptr, aData uintptr, nData int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a, pPage uintptr
	var n, nCopy, nReq int32
	var _ /* dummy at bp+0 */ Ti64
	_, _, _, _, _ = a, n, nCopy, nReq, pPage
	pPage = pWriter + 4
	a = aData
	n = nData
	for (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn+(*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgidx.Fn+n >= (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz {
		nReq = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz - (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn - (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgidx.Fn
		nCopy = 0
		for nCopy < nReq {
			nCopy = nCopy + int32(_sqlite3Fts5GetVarint(tls, a+uintptr(nCopy), bp))
		}
		_sqlite3Fts5BufferAppendBlob(tls, p+44, pPage+8, uint32(nCopy), a)
		a = a + uintptr(nCopy)
		n = n - nCopy
		_fts5WriteFlushLeaf(tls, p, pWriter)
	}
	if n > 0 {
		_sqlite3Fts5BufferAppendBlob(tls, p+44, pPage+8, uint32(n), a)
	}
}

// C documentation
//
//	/*
//	** Append a rowid and position-list size field to the writers output.
//	*/
func _fts5WriteAppendRowid(tls *libc.TLS, p uintptr, pWriter uintptr, iRowid Ti64) {
	var pPage uintptr
	_ = pPage
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		pPage = pWriter + 4
		if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn+(*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgidx.Fn >= (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz {
			_fts5WriteFlushLeaf(tls, p, pWriter)
		}
		/* If this is to be the first rowid written to the page, set the
		 ** rowid-pointer in the page-header. Also append a value to the dlidx
		 ** buffer, in case a doclist-index is required.  */
		if (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage != 0 {
			_fts5PutU16(tls, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fp, uint16((*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn))
			_fts5WriteDlidxAppend(tls, p, pWriter, iRowid)
		}
		/* Write the rowid. */
		if (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInDoclist != 0 || (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage != 0 {
			_sqlite3Fts5BufferAppendVarint(tls, p+44, pPage+8, iRowid)
		} else {
			_sqlite3Fts5BufferAppendVarint(tls, p+44, pPage+8, int64(uint64(int64(uint64(iRowid)))-uint64((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiPrevRowid)))
		}
		(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiPrevRowid = iRowid
		(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInDoclist = uint8(0)
		(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage = uint8(0)
	}
}

// C documentation
//
//	/*
//	** Append term pTerm/nTerm to the segment being written by the writer passed
//	** as the second argument.
//	**
//	** If an error occurs, set the Fts5Index.rc error code. If an error has
//	** already occurred, this function is a no-op.
//	*/
func _fts5WriteAppendTerm(tls *libc.TLS, p uintptr, pWriter uintptr, nTerm int32, pTerm uintptr) {
	var n, nMin, nPrefix, v1 int32
	var pPage, pPgidx uintptr
	_, _, _, _, _, _ = n, nMin, nPrefix, pPage, pPgidx, v1 /* Bytes of prefix compression for term */
	pPage = pWriter + 4
	pPgidx = pWriter + 4 + 20
	if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fterm.Fn < nTerm {
		v1 = (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fterm.Fn
	} else {
		v1 = nTerm
	}
	nMin = v1
	/* If the current leaf page is full, flush it to disk. */
	if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn+nTerm+int32(2) >= (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz {
		if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn > int32(4) {
			_fts5WriteFlushLeaf(tls, p, pWriter)
			if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK {
				return
			}
		}
		if !(uint32((*TFts5Buffer)(unsafe.Pointer(pPage+8)).Fn)+uint32(nTerm+libc.Int32FromInt32(FTS5_DATA_PADDING)) <= uint32((*TFts5Buffer)(unsafe.Pointer(pPage+8)).FnSpace)) {
			_sqlite3Fts5BufferSize(tls, p+44, pPage+8, uint32(nTerm+int32(FTS5_DATA_PADDING)+(*TFts5Buffer)(unsafe.Pointer(pPage+8)).Fn))
		}
	}
	/* TODO1: Updating pgidx here. */
	**(**int32)(__ccgo_up(pPgidx + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn), uint64((*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn-(*TFts5PageWriter)(unsafe.Pointer(pPage)).FiPrevPgidx))
	(*TFts5PageWriter)(unsafe.Pointer(pPage)).FiPrevPgidx = (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn
	if (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstTermInPage != 0 {
		nPrefix = 0
		if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgno != int32(1) {
			/* This is the first term on a leaf that is not the leftmost leaf in
			 ** the segment b-tree. In this case it is necessary to add a term to
			 ** the b-tree hierarchy that is (a) larger than the largest term
			 ** already written to the segment and (b) smaller than or equal to
			 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
			 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
			 ** previous term.
			 **
			 ** Usually, the previous term is available in pPage->term. The exception
			 ** is if this is the first term written in an incremental-merge step.
			 ** In this case the previous term is not available, so just write a
			 ** copy of (pTerm/nTerm) into the parent node. This is slightly
			 ** inefficient, but still correct.  */
			n = nTerm
			if (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fterm.Fn != 0 {
				n = int32(1) + _fts5PrefixCompress(tls, nMin, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fterm.Fp, pTerm)
			}
			_fts5WriteBtreeTerm(tls, p, pWriter, n, pTerm)
			if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK {
				return
			}
			pPage = pWriter + 4
		}
	} else {
		nPrefix = _fts5PrefixCompress(tls, nMin, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fterm.Fp, pTerm)
		_sqlite3Fts5BufferAppendVarint(tls, p+44, pPage+8, int64(nPrefix))
	}
	/* Append the number of bytes of new data, then the term data itself
	 ** to the page. */
	_sqlite3Fts5BufferAppendVarint(tls, p+44, pPage+8, int64(nTerm)-int64(nPrefix))
	_sqlite3Fts5BufferAppendBlob(tls, p+44, pPage+8, uint32(nTerm-nPrefix), pTerm+uintptr(nPrefix))
	/* Update the Fts5PageWriter.term field. */
	_sqlite3Fts5BufferSet(tls, p+44, pPage+32, nTerm, pTerm)
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstTermInPage = uint8(0)
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage = uint8(0)
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInDoclist = uint8(1)
	(**(**TFts5DlidxWriter)(__ccgo_up((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx))).Fpgno = (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgno
}

// C documentation
//
//	/*
//	** Rowid iRowid has just been appended to the current leaf page. It is the
//	** first on the page. This function appends an appropriate entry to the current
//	** doclist-index.
//	*/
func _fts5WriteDlidxAppend(tls *libc.TLS, p uintptr, pWriter uintptr, iRowid Ti64) {
	var bDone, i, v2 int32
	var iFirst, iPgno, iVal Ti64
	var pDlidx uintptr
	_, _, _, _, _, _, _ = bDone, i, iFirst, iPgno, iVal, pDlidx, v2
	bDone = 0
	i = 0
	for {
		if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bDone == 0) {
			break
		}
		pDlidx = (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx + uintptr(i)*32
		if (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fn >= (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz {
			/* The current doclist-index page is full. Write it to disk and push
			 ** a copy of iRowid (which will become the first rowid on the next
			 ** doclist-index leaf page) up into the next level of the b-tree
			 ** hierarchy. If the node being flushed is currently the root node,
			 ** also push its first rowid upwards. */
			**(**Tu8)(__ccgo_up((*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fp)) = uint8(0x01) /* Not the root node */
			_fts5DataWrite(tls, p, int64((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(i)<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64((*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fpgno), (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fp, (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fn)
			_fts5WriteDlidxGrow(tls, p, pWriter, i+int32(2))
			pDlidx = (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx + uintptr(i)*32
			if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (**(**TFts5DlidxWriter)(__ccgo_up(pDlidx + 1*32))).Fbuf.Fn == 0 {
				iFirst = _fts5DlidxExtractFirstRowid(tls, pDlidx+16)
				/* This was the root node. Push its first rowid up to the new root. */
				(**(**TFts5DlidxWriter)(__ccgo_up(pDlidx + 1*32))).Fpgno = (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fpgno
				_sqlite3Fts5BufferAppendVarint(tls, p+44, pDlidx+1*32+16, 0)
				_sqlite3Fts5BufferAppendVarint(tls, p+44, pDlidx+1*32+16, int64((*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fpgno))
				_sqlite3Fts5BufferAppendVarint(tls, p+44, pDlidx+1*32+16, iFirst)
				(**(**TFts5DlidxWriter)(__ccgo_up(pDlidx + 1*32))).FbPrevValid = int32(1)
				(**(**TFts5DlidxWriter)(__ccgo_up(pDlidx + 1*32))).FiPrev = iFirst
			}
			_sqlite3Fts5BufferZero(tls, pDlidx+16)
			(*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).FbPrevValid = 0
			(*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fpgno = (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fpgno + 1
		} else {
			bDone = int32(1)
		}
		if (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).FbPrevValid != 0 {
			iVal = int64(uint64(iRowid) - uint64((*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).FiPrev))
		} else {
			if i == 0 {
				v2 = (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fwriter.Fpgno
			} else {
				v2 = (**(**TFts5DlidxWriter)(__ccgo_up(pDlidx + uintptr(-libc.Int32FromInt32(1))*32))).Fpgno
			}
			iPgno = int64(v2)
			_sqlite3Fts5BufferAppendVarint(tls, p+44, pDlidx+16, libc.BoolInt64(!(bDone != 0)))
			_sqlite3Fts5BufferAppendVarint(tls, p+44, pDlidx+16, iPgno)
			iVal = iRowid
		}
		_sqlite3Fts5BufferAppendVarint(tls, p+44, pDlidx+16, iVal)
		(*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).FbPrevValid = int32(1)
		(*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).FiPrev = iRowid
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
//	** Any new array elements are zeroed before returning.
//	*/
func _fts5WriteDlidxGrow(tls *libc.TLS, p uintptr, pWriter uintptr, nLvl int32) (r int32) {
	var aDlidx uintptr
	var nByte Tsize_t
	_, _ = aDlidx, nByte
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && nLvl >= (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx {
		aDlidx = Xsqlite3_realloc64(tls, (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx, uint64(uint32(32)*uint32(nLvl)))
		if aDlidx == uintptr(0) {
			(*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
		} else {
			nByte = uint32(32) * uint32(nLvl-(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx)
			libc.Xmemset(tls, aDlidx+uintptr((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx)*32, 0, nByte)
			(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx = aDlidx
			(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx = nLvl
		}
	}
	return (*TFts5Index)(unsafe.Pointer(p)).Frc
}

func _fts5WriteFlushLeaf(tls *libc.TLS, p uintptr, pWriter uintptr) {
	var iRowid Ti64
	var pPage uintptr
	_, _ = iRowid, pPage
	pPage = pWriter + 4
	/* Set the szLeaf header field. */
	_fts5PutU16(tls, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fp+2, uint16((*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn))
	if (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstTermInPage != 0 {
		/* No term was written to this page. */
		_fts5WriteBtreeNoTerm(tls, p, pWriter)
	} else {
		/* Append the pgidx to the page buffer. Set the szLeaf header field. */
		_sqlite3Fts5BufferAppendBlob(tls, p+44, pPage+8, uint32((*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgidx.Fn), (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgidx.Fp)
	}
	/* Write the page out to disk */
	iRowid = int64((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B)) + int64(libc.Int32FromInt32(0))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)) + int64(libc.Int32FromInt32(0))<<libc.Int32FromInt32(FTS5_DATA_PAGE_B) + int64((*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgno)
	_fts5DataWrite(tls, p, iRowid, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fp, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fbuf.Fn)
	/* Initialize the next page. */
	_sqlite3Fts5BufferZero(tls, pPage+8)
	_sqlite3Fts5BufferZero(tls, pPage+20)
	_sqlite3Fts5BufferAppendBlob(tls, p+44, pPage+8, uint32(4), uintptr(unsafe.Pointer(&_zero)))
	(*TFts5PageWriter)(unsafe.Pointer(pPage)).FiPrevPgidx = 0
	(*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgno = (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fpgno + 1
	/* Increase the leaves written counter */
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnLeafWritten = (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnLeafWritten + 1
	/* The new leaf holds no terms or rowids */
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstTermInPage = uint8(1)
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage = uint8(1)
}

func _fts5WriteInit(tls *libc.TLS, p uintptr, pWriter uintptr, iSegid int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var nBuffer int32
	var pConfig uintptr
	_, _ = nBuffer, pConfig
	nBuffer = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).Fpgsz + int32(FTS5_DATA_PADDING)
	libc.Xmemset(tls, pWriter, 0, uint32(96))
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiSegid = iSegid
	_fts5WriteDlidxGrow(tls, p, pWriter, int32(1))
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fwriter.Fpgno = int32(1)
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstTermInPage = uint8(1)
	(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiBtPage = int32(1)
	/* Grow the two buffers to pgsz + padding bytes in size. */
	_sqlite3Fts5BufferSize(tls, p+44, pWriter+4+20, uint32(nBuffer))
	_sqlite3Fts5BufferSize(tls, p+44, pWriter+4+8, uint32(nBuffer))
	if (*TFts5Index)(unsafe.Pointer(p)).FpIdxWriter == uintptr(0) {
		pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
		_fts5IndexPrepareStmt(tls, p, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+40565, libc.VaList(bp+8, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName)))
	}
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		/* Initialize the 4-byte leaf-page header to 0x00. */
		libc.Xmemset(tls, (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fwriter.Fbuf.Fp, 0, uint32(4))
		(*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fwriter.Fbuf.Fn = int32(4)
		/* Bind the current output segment id to the index-writer. This is an
		 ** optimization over binding the same value over and over as rows are
		 ** inserted into %_idx by the current writer.  */
		Xsqlite3_bind_int(tls, (*TFts5Index)(unsafe.Pointer(p)).FpIdxWriter, int32(1), (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiSegid)
	}
}

// C documentation
//
//	/*
//	** If the argument is a codepoint corresponding to a lowercase letter
//	** in the ASCII range with a diacritic added, return the codepoint
//	** of the ASCII letter only. For example, if passed 235 - "LATIN
//	** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
//	** E"). The resuls of passing a codepoint that corresponds to an
//	** uppercase letter are undefined.
//	*/
func _fts5_remove_diacritic(tls *libc.TLS, c int32, bComplex int32) (r int32) {
	var aChar [126]uint8
	var aDia [126]uint16
	var iHi, iLo, iRes, iTest, v1 int32
	var key uint32
	_, _, _, _, _, _, _, _ = aChar, aDia, iHi, iLo, iRes, iTest, key, v1
	aDia = [126]uint16{
		1:   uint16(1797),
		2:   uint16(1848),
		3:   uint16(1859),
		4:   uint16(1891),
		5:   uint16(1928),
		6:   uint16(1940),
		7:   uint16(1995),
		8:   uint16(2024),
		9:   uint16(2040),
		10:  uint16(2060),
		11:  uint16(2110),
		12:  uint16(2168),
		13:  uint16(2206),
		14:  uint16(2264),
		15:  uint16(2286),
		16:  uint16(2344),
		17:  uint16(2383),
		18:  uint16(2472),
		19:  uint16(2488),
		20:  uint16(2516),
		21:  uint16(2596),
		22:  uint16(2668),
		23:  uint16(2732),
		24:  uint16(2782),
		25:  uint16(2842),
		26:  uint16(2894),
		27:  uint16(2954),
		28:  uint16(2984),
		29:  uint16(3000),
		30:  uint16(3028),
		31:  uint16(3336),
		32:  uint16(3456),
		33:  uint16(3696),
		34:  uint16(3712),
		35:  uint16(3728),
		36:  uint16(3744),
		37:  uint16(3766),
		38:  uint16(3832),
		39:  uint16(3896),
		40:  uint16(3912),
		41:  uint16(3928),
		42:  uint16(3944),
		43:  uint16(3968),
		44:  uint16(4008),
		45:  uint16(4040),
		46:  uint16(4056),
		47:  uint16(4106),
		48:  uint16(4138),
		49:  uint16(4170),
		50:  uint16(4202),
		51:  uint16(4234),
		52:  uint16(4266),
		53:  uint16(4296),
		54:  uint16(4312),
		55:  uint16(4344),
		56:  uint16(4408),
		57:  uint16(4424),
		58:  uint16(4442),
		59:  uint16(4472),
		60:  uint16(4488),
		61:  uint16(4504),
		62:  uint16(6148),
		63:  uint16(6198),
		64:  uint16(6264),
		65:  uint16(6280),
		66:  uint16(6360),
		67:  uint16(6429),
		68:  uint16(6505),
		69:  uint16(6529),
		70:  uint16(61448),
		71:  uint16(61468),
		72:  uint16(61512),
		73:  uint16(61534),
		74:  uint16(61592),
		75:  uint16(61610),
		76:  uint16(61642),
		77:  uint16(61672),
		78:  uint16(61688),
		79:  uint16(61704),
		80:  uint16(61726),
		81:  uint16(61784),
		82:  uint16(61800),
		83:  uint16(61816),
		84:  uint16(61836),
		85:  uint16(61880),
		86:  uint16(61896),
		87:  uint16(61914),
		88:  uint16(61948),
		89:  uint16(61998),
		90:  uint16(62062),
		91:  uint16(62122),
		92:  uint16(62154),
		93:  uint16(62184),
		94:  uint16(62200),
		95:  uint16(62218),
		96:  uint16(62252),
		97:  uint16(62302),
		98:  uint16(62364),
		99:  uint16(62410),
		100: uint16(62442),
		101: uint16(62478),
		102: uint16(62536),
		103: uint16(62554),
		104: uint16(62584),
		105: uint16(62604),
		106: uint16(62640),
		107: uint16(62648),
		108: uint16(62656),
		109: uint16(62664),
		110: uint16(62730),
		111: uint16(62766),
		112: uint16(62830),
		113: uint16(62890),
		114: uint16(62924),
		115: uint16(62974),
		116: uint16(63032),
		117: uint16(63050),
		118: uint16(63082),
		119: uint16(63118),
		120: uint16(63182),
		121: uint16(63242),
		122: uint16(63274),
		123: uint16(63310),
		124: uint16(63368),
		125: uint16(63390),
	}
	aChar = [126]uint8{
		1:   uint8('a'),
		2:   uint8('c'),
		3:   uint8('e'),
		4:   uint8('i'),
		5:   uint8('n'),
		6:   uint8('o'),
		7:   uint8('u'),
		8:   uint8('y'),
		9:   uint8('y'),
		10:  uint8('a'),
		11:  uint8('c'),
		12:  uint8('d'),
		13:  uint8('e'),
		14:  uint8('e'),
		15:  uint8('g'),
		16:  uint8('h'),
		17:  uint8('i'),
		18:  uint8('j'),
		19:  uint8('k'),
		20:  uint8('l'),
		21:  uint8('n'),
		22:  uint8('o'),
		23:  uint8('r'),
		24:  uint8('s'),
		25:  uint8('t'),
		26:  uint8('u'),
		27:  uint8('u'),
		28:  uint8('w'),
		29:  uint8('y'),
		30:  uint8('z'),
		31:  uint8('o'),
		32:  uint8('u'),
		33:  uint8('a'),
		34:  uint8('i'),
		35:  uint8('o'),
		36:  uint8('u'),
		37:  uint8(libc.Int32FromUint8('u') | int32(libc.Uint8FromInt32(0x80))),
		38:  uint8(libc.Int32FromUint8('a') | int32(libc.Uint8FromInt32(0x80))),
		39:  uint8('g'),
		40:  uint8('k'),
		41:  uint8('o'),
		42:  uint8(libc.Int32FromUint8('o') | int32(libc.Uint8FromInt32(0x80))),
		43:  uint8('j'),
		44:  uint8('g'),
		45:  uint8('n'),
		46:  uint8(libc.Int32FromUint8('a') | int32(libc.Uint8FromInt32(0x80))),
		47:  uint8('a'),
		48:  uint8('e'),
		49:  uint8('i'),
		50:  uint8('o'),
		51:  uint8('r'),
		52:  uint8('u'),
		53:  uint8('s'),
		54:  uint8('t'),
		55:  uint8('h'),
		56:  uint8('a'),
		57:  uint8('e'),
		58:  uint8(libc.Int32FromUint8('o') | int32(libc.Uint8FromInt32(0x80))),
		59:  uint8('o'),
		60:  uint8(libc.Int32FromUint8('o') | int32(libc.Uint8FromInt32(0x80))),
		61:  uint8('y'),
		70:  uint8('a'),
		71:  uint8('b'),
		72:  uint8(libc.Int32FromUint8('c') | int32(libc.Uint8FromInt32(0x80))),
		73:  uint8('d'),
		74:  uint8('d'),
		75:  uint8(libc.Int32FromUint8('e') | int32(libc.Uint8FromInt32(0x80))),
		76:  uint8('e'),
		77:  uint8(libc.Int32FromUint8('e') | int32(libc.Uint8FromInt32(0x80))),
		78:  uint8('f'),
		79:  uint8('g'),
		80:  uint8('h'),
		81:  uint8('h'),
		82:  uint8('i'),
		83:  uint8(libc.Int32FromUint8('i') | int32(libc.Uint8FromInt32(0x80))),
		84:  uint8('k'),
		85:  uint8('l'),
		86:  uint8(libc.Int32FromUint8('l') | int32(libc.Uint8FromInt32(0x80))),
		87:  uint8('l'),
		88:  uint8('m'),
		89:  uint8('n'),
		90:  uint8(libc.Int32FromUint8('o') | int32(libc.Uint8FromInt32(0x80))),
		91:  uint8('p'),
		92:  uint8('r'),
		93:  uint8(libc.Int32FromUint8('r') | int32(libc.Uint8FromInt32(0x80))),
		94:  uint8('r'),
		95:  uint8('s'),
		96:  uint8(libc.Int32FromUint8('s') | int32(libc.Uint8FromInt32(0x80))),
		97:  uint8('t'),
		98:  uint8('u'),
		99:  uint8(libc.Int32FromUint8('u') | int32(libc.Uint8FromInt32(0x80))),
		100: uint8('v'),
		101: uint8('w'),
		102: uint8('w'),
		103: uint8('x'),
		104: uint8('y'),
		105: uint8('z'),
		106: uint8('h'),
		107: uint8('t'),
		108: uint8('w'),
		109: uint8('y'),
		110: uint8('a'),
		111: uint8(libc.Int32FromUint8('a') | int32(libc.Uint8FromInt32(0x80))),
		112: uint8(libc.Int32FromUint8('a') | int32(libc.Uint8FromInt32(0x80))),
		113: uint8(libc.Int32FromUint8('a') | int32(libc.Uint8FromInt32(0x80))),
		114: uint8('e'),
		115: uint8(libc.Int32FromUint8('e') | int32(libc.Uint8FromInt32(0x80))),
		116: uint8(libc.Int32FromUint8('e') | int32(libc.Uint8FromInt32(0x80))),
		117: uint8('i'),
		118: uint8('o'),
		119: uint8(libc.Int32FromUint8('o') | int32(libc.Uint8FromInt32(0x80))),
		120: uint8(libc.Int32FromUint8('o') | int32(libc.Uint8FromInt32(0x80))),
		121: uint8(libc.Int32FromUint8('o') | int32(libc.Uint8FromInt32(0x80))),
		122: uint8('u'),
		123: uint8(libc.Int32FromUint8('u') | int32(libc.Uint8FromInt32(0x80))),
		124: uint8(libc.Int32FromUint8('u') | int32(libc.Uint8FromInt32(0x80))),
		125: uint8('y'),
	}
	key = uint32(c)<<int32(3) | uint32(0x00000007)
	iRes = 0
	iHi = int32(libc.Uint32FromInt64(252)/libc.Uint32FromInt64(2) - libc.Uint32FromInt32(1))
	iLo = 0
	for iHi >= iLo {
		iTest = (iHi + iLo) / int32(2)
		if key >= uint32(aDia[iTest]) {
			iRes = iTest
			iLo = iTest + int32(1)
		} else {
			iHi = iTest - int32(1)
		}
	}
	if bComplex == 0 && int32(aChar[iRes])&int32(0x80) != 0 {
		return c
	}
	if c > int32(aDia[iRes])>>int32(3)+int32(aDia[iRes])&int32(0x07) {
		v1 = c
	} else {
		v1 = int32(aChar[iRes]) & int32(0x7F)
	}
	return v1
}

// C documentation
//
//	/*
//	** Perform a reduce action and the shift that must immediately
//	** follow the reduce.
//	**
//	** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
//	** access to the lookahead token (if any).  The fts5yyLookahead will be fts5YYNOCODE
//	** if the lookahead token has already been consumed.  As this procedure is
//	** only called from one place, optimizing compilers will in-line it, which
//	** means that the extra parameters have no performance impact.
//	*/
func _fts5yy_reduce(tls *libc.TLS, fts5yypParser uintptr, fts5yyruleno uint32, fts5yyLookahead int32, fts5yyLookaheadToken TFts5Token) (r uint8) {
	var fts5yyact uint8
	var fts5yygoto, fts5yysize int32
	var fts5yylhsminor Tfts5YYMINORTYPE
	var fts5yymsp, pParse uintptr
	_, _, _, _, _, _ = fts5yyact, fts5yygoto, fts5yylhsminor, fts5yymsp, fts5yysize, pParse /* Amount to pop the stack */
	pParse = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse
	_ = fts5yyLookahead
	_ = fts5yyLookaheadToken
	fts5yymsp = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos
	switch fts5yyruleno {
	case uint32(0):
		goto _1
	case uint32(1):
		goto _2
	case uint32(2):
		goto _3
	case uint32(3):
		goto _4
	case uint32(4):
		goto _5
	case uint32(5):
		goto _6
	case uint32(6):
		goto _7
	case uint32(7):
		goto _8
	case uint32(8):
		goto _9
	case uint32(9):
		goto _10
	case uint32(10):
		goto _11
	case uint32(11):
		goto _12
	case uint32(13):
		goto _13
	case uint32(12):
		goto _14
	case uint32(14):
		goto _15
	case uint32(15):
		goto _16
	case uint32(16):
		goto _17
	case uint32(17):
		goto _18
	case uint32(18):
		goto _19
	case uint32(19):
		goto _20
	case uint32(20):
		goto _21
	case uint32(21):
		goto _22
	case uint32(22):
		goto _23
	case uint32(23):
		goto _24
	case uint32(24):
		goto _25
	case uint32(25):
		goto _26
	case uint32(26):
		goto _27
	case uint32(27):
		goto _28
	default:
		goto _29
	}
	goto _30
_1:
	; /* input ::= expr */
	_sqlite3Fts5ParseFinished(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)))
	goto _30
_2:
	; /* colset ::= MINUS LCP colsetlist RCP */
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3Fts5ParseColsetInvert(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _30
_3:
	; /* colset ::= LCP colsetlist RCP */
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _30
_4:
	; /* colset ::= STRING */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseColset(tls, pParse, uintptr(0), fts5yymsp+4)
	*(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_5:
	; /* colset ::= MINUS STRING */
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3Fts5ParseColset(tls, pParse, uintptr(0), fts5yymsp+4)
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3Fts5ParseColsetInvert(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _30
_6:
	; /* colsetlist ::= colsetlist STRING */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseColset(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), fts5yymsp+4)
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_7:
	; /* colsetlist ::= STRING */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseColset(tls, pParse, uintptr(0), fts5yymsp+4)
	*(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_8:
	; /* expr ::= expr AND expr */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_AND), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)), uintptr(0))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_9:
	; /* expr ::= expr OR expr */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_OR), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)), uintptr(0))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_10:
	; /* expr ::= expr NOT expr */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_NOT), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)), uintptr(0))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_11:
	; /* expr ::= colset COLON LP expr RP */
	_sqlite3Fts5ParseSetColset(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_12:
	; /* expr ::= LP expr RP */
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _30
_14:
	; /* expr ::= exprlist */
_13:
	;
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + 4))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_15:
	; /* exprlist ::= exprlist cnearset */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseImplicitAnd(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_16:
	; /* cnearset ::= nearset */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_STRING), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_17:
	; /* cnearset ::= colset COLON nearset */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_STRING), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)))
	_sqlite3Fts5ParseSetColset(tls, pParse, *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_18:
	; /* nearset ::= phrase */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_19:
	; /* nearset ::= CARET phrase */
	_sqlite3Fts5ParseSetCaret(tls, *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)))
	goto _30
_20:
	; /* nearset ::= STRING LP nearphrases neardist_opt RP */
	_sqlite3Fts5ParseNear(tls, pParse, fts5yymsp+uintptr(-libc.Int32FromInt32(4))*12+4)
	_sqlite3Fts5ParseSetDistance(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), fts5yymsp+uintptr(-libc.Int32FromInt32(1))*12+4)
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_21:
	; /* nearphrases ::= phrase */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_22:
	; /* nearphrases ::= nearphrases phrase */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNearset(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_23:
	; /* neardist_opt ::= */
	*(*uintptr)(unsafe.Pointer(fts5yymsp + 1*12 + 4)) = uintptr(0)
	*(*int32)(unsafe.Pointer(fts5yymsp + 1*12 + 4 + 4)) = 0
	goto _30
_24:
	; /* neardist_opt ::= COMMA STRING */
	*(*TFts5Token)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TFts5Token)(unsafe.Pointer(fts5yymsp + 4))
	goto _30
_25:
	; /* phrase ::= phrase PLUS STRING star_opt */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), fts5yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*int32)(unsafe.Pointer(fts5yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_26:
	; /* phrase ::= STRING star_opt */
	*(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseTerm(tls, pParse, uintptr(0), fts5yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*int32)(unsafe.Pointer(fts5yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor))
	goto _30
_27:
	; /* star_opt ::= STAR */
	*(*int32)(unsafe.Pointer(fts5yymsp + 4)) = int32(1)
	goto _30
_28:
	; /* star_opt ::= */
	*(*int32)(unsafe.Pointer(fts5yymsp + 1*12 + 4)) = 0
	goto _30
_29:
	;
	goto _30
	/********** End reduce actions ************************************************/
_30:
	;
	fts5yygoto = int32(_fts5yyRuleInfoLhs[fts5yyruleno])
	fts5yysize = int32(_fts5yyRuleInfoNRhs[fts5yyruleno])
	fts5yyact = _fts5yy_find_reduce_action(tls, (**(**Tfts5yyStackEntry)(__ccgo_up(fts5yymsp + uintptr(fts5yysize)*12))).Fstateno, uint8(fts5yygoto))
	/* There are no SHIFTREDUCE actions on nonterminals because the table
	 ** generator has simplified them to pure REDUCE actions. */
	/* It is not possible for a REDUCE to be followed by an error */
	fts5yymsp = fts5yymsp + uintptr(fts5yysize+int32(1))*12
	(*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos = fts5yymsp
	(*Tfts5yyStackEntry)(unsafe.Pointer(fts5yymsp)).Fstateno = fts5yyact
	(*Tfts5yyStackEntry)(unsafe.Pointer(fts5yymsp)).Fmajor = uint8(fts5yygoto)
	return fts5yyact
}

/*
** The following code executes when the parse fails
 */

// C documentation
//
//	/*
//	** Perform a shift action.
//	*/
func _fts5yy_shift(tls *libc.TLS, fts5yypParser uintptr, fts5yyNewState uint8, fts5yyMajor uint8, fts5yyMinor TFts5Token) {
	var fts5yytos uintptr
	_ = fts5yytos
	(*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos += 12
	fts5yytos = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos
	if fts5yytos > (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystackEnd {
		if int32(1) != 0 {
			(*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos -= 12
			_fts5yyStackOverflow(tls, fts5yypParser)
			return
		}
		fts5yytos = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos
	}
	if int32(fts5yyNewState) > int32(fts5YY_MAX_SHIFT) {
		fts5yyNewState = uint8(int32(fts5yyNewState) + (libc.Int32FromInt32(fts5YY_MIN_REDUCE) - libc.Int32FromInt32(fts5YY_MIN_SHIFTREDUCE)))
	}
	(*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fstateno = fts5yyNewState
	(*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fmajor = fts5yyMajor
	*(*TFts5Token)(unsafe.Pointer(fts5yytos + 4)) = fts5yyMinor
}

// C documentation
//
//	/*
//	** The gatherSelectWindows() procedure and its helper routine
//	** gatherSelectWindowsCallback() are used to scan all the expressions
//	** an a newly duplicated SELECT statement and gather all of the Window
//	** objects found there, assembling them onto the linked list at Select->pWin.
//	*/
func _gatherSelectWindowsCallback(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var pSelect, pWin uintptr
	_, _ = pSelect, pWin
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
		pSelect = *(*uintptr)(unsafe.Pointer(pWalker + 24))
		pWin = *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy))
		_sqlite3WindowLink(tls, pSelect, pWin)
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Code an output subroutine for a coroutine implementation of a
//	** SELECT statement.
//	**
//	** The data to be output is contained in an array of pIn->nSdst registers
//	** starting at register pIn->iSdst.  pDest is where the output should
//	** be sent.
//	**
//	** regReturn is the number of the register holding the subroutine
//	** return address.
//	**
//	** If regPrev>0 then it is the first register in a vector that
//	** records the previous output.  mem[regPrev] is a flag that is false
//	** if there has been no previous output.  If regPrev>0 then code is
//	** generated to suppress duplicates.  pKeyInfo is used for comparing
//	** keys.
//	**
//	** If the LIMIT found in p->iLimit is reached, jump immediately to
//	** iBreak.
//	*/
func _generateOutputSubroutine(tls *libc.TLS, pParse uintptr, p uintptr, pIn uintptr, pDest uintptr, regReturn int32, regPrev int32, pKeyInfo uintptr, iBreak int32) (r int32) {
	var addr, addr1, addr2, iContinue, iParm, iParm1, ii, nKey, r1, r11, r12, r2, r21, r3 int32
	var pSO, v uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addr1, addr2, iContinue, iParm, iParm1, ii, nKey, pSO, r1, r11, r12, r2, r21, r3, v
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	addr = _sqlite3VdbeCurrentAddr(tls, v)
	iContinue = _sqlite3VdbeMakeLabel(tls, pParse)
	/* Suppress duplicates for UNION, EXCEPT, and INTERSECT
	 */
	if regPrev != 0 {
		addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), regPrev)
		addr2 = _sqlite3VdbeAddOp4(tls, v, int32(OP_Compare), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, regPrev+int32(1), (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst, _sqlite3KeyInfoRef(tls, pKeyInfo), -int32(9))
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addr2+int32(2), iContinue, addr2+int32(2))
		_sqlite3VdbeJumpHere(tls, v, addr1)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, regPrev+int32(1), (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst-int32(1))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), regPrev)
	}
	if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
		return 0
	}
	/* Suppress the first OFFSET entries if there is an OFFSET clause
	 */
	_codeOffset(tls, v, (*TSelect)(unsafe.Pointer(p)).FiOffset, iContinue)
	switch int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) {
	/* Store the result as data using a unique key.
	 */
	case int32(SRT_Fifo):
		fallthrough
	case int32(SRT_DistFifo):
		fallthrough
	case int32(SRT_Table):
		fallthrough
	case int32(SRT_EphemTab):
		r1 = _sqlite3GetTempReg(tls, pParse)
		r2 = _sqlite3GetTempReg(tls, pParse)
		iParm = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst, r1)
		if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) == int32(SRT_DistFifo) {
			/* If the destination is DistFifo, then cursor (iParm+1) is open
			 ** on an ephemeral index that is used to enforce uniqueness on the
			 ** total result.  At this point, we are processing the setup portion
			 ** of the recursive CTE using the merge algorithm, so the results are
			 ** guaranteed to be unique anyhow.  But we still need to populate the
			 ** (iParm+1) cursor for use by the subsequent recursive phase.
			 */
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm+int32(1), r1, (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst)
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iParm, r2)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iParm, r1, r2)
		_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND))
		_sqlite3ReleaseTempReg(tls, pParse, r2)
		_sqlite3ReleaseTempReg(tls, pParse, r1)
		break
		/* If any row exist in the result set, record that fact and abort.
		 */
		fallthrough
	case int32(SRT_Exists):
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm)
		/* The LIMIT clause will terminate the loop for us */
		break
		/* If we are creating a set for an "expr IN (SELECT ...)".
		 */
		fallthrough
	case int32(SRT_Set):
		r11 = _sqlite3GetTempReg(tls, pParse)
		_sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst, r11, (*TSelectDest)(unsafe.Pointer(pDest)).FzAffSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst)
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm, r11, (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst)
		if (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2 > 0 {
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2, 0, (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst)
			_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+21699, 0)
		}
		_sqlite3ReleaseTempReg(tls, pParse, r11)
		break
		/* If this is a scalar select that is part of an expression, then
		 ** store the results in the appropriate memory cell and break out
		 ** of the scan loop.  Note that the select might return multiple columns
		 ** if it is the RHS of a row-value IN operator.
		 */
		fallthrough
	case int32(SRT_Mem):
		_sqlite3ExprCodeMove(tls, pParse, (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst)
		/* The LIMIT clause will jump out of the loop for us */
		break
		/* The results are stored in a sequence of registers
		 ** starting at pDest->iSdst.  Then the co-routine yields.
		 */
		fallthrough
	case int32(SRT_Coroutine):
		if (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst == 0 {
			(*TSelectDest)(unsafe.Pointer(pDest)).FiSdst = _sqlite3GetTempRange(tls, pParse, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst)
			(*TSelectDest)(unsafe.Pointer(pDest)).FnSdst = (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst
		}
		_sqlite3ExprCodeMove(tls, pParse, (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst)
		_sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm)
		break
		/* Write the results into a priority queue that is order according to
		 ** pDest->pOrderBy (in pSO).  pDest->iSDParm (in iParm) is the cursor for an
		 ** index with pSO->nExpr+2 columns.  Build a key using pSO for the first
		 ** pSO->nExpr columns, then make sure all keys are unique by adding a
		 ** final OP_Sequence column.  The last column is the record as a blob.
		 */
		fallthrough
	case int32(SRT_DistQueue):
		fallthrough
	case int32(SRT_Queue):
		iParm1 = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm
		pSO = (*TSelectDest)(unsafe.Pointer(pDest)).FpOrderBy
		nKey = (*TExprList)(unsafe.Pointer(pSO)).FnExpr
		r12 = _sqlite3GetTempReg(tls, pParse)
		r21 = _sqlite3GetTempRange(tls, pParse, nKey+int32(2))
		r3 = r21 + nKey + int32(1)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst, r3)
		if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) == int32(SRT_DistQueue) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), iParm1+int32(1), r3)
		}
		ii = 0
		for {
			if !(ii < nKey) {
				break
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst+int32(*(*Tu16)(unsafe.Pointer(pSO + 8 + uintptr(ii)*20 + 16)))-int32(1), r21+ii)
			goto _1
		_1:
			;
			ii = ii + 1
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Sequence), iParm1, r21+nKey)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), r21, nKey+int32(2), r12)
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm1, r12, r21, nKey+int32(2))
		_sqlite3ReleaseTempReg(tls, pParse, r12)
		_sqlite3ReleaseTempRange(tls, pParse, r21, nKey+int32(2))
		break
		/* Ignore the output */
		fallthrough
	case int32(SRT_Discard):
		break
		/* If none of the above, then the result destination must be
		 ** SRT_Output.
		 **
		 ** For SRT_Output, results are stored in a sequence of registers.
		 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
		 ** return the next row of result.
		 */
		fallthrough
	default:
		_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), (*TSelectDest)(unsafe.Pointer(pIn)).FiSdst, (*TSelectDest)(unsafe.Pointer(pIn)).FnSdst)
		break
	}
	/* Jump to the end of the loop if the LIMIT is reached.
	 */
	if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_DecrJumpZero), (*TSelect)(unsafe.Pointer(p)).FiLimit, iBreak)
	}
	/* Generate the subroutine return
	 */
	_sqlite3VdbeResolveLabel(tls, v, iContinue)
	_sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regReturn)
	return addr
}

// C documentation
//
//	/*
//	** If the inner loop was generated using a non-null pOrderBy argument,
//	** then the results were placed in a sorter.  After the loop is terminated
//	** we need to run the sorter and output the results.  The following
//	** routine generates the code needed to do that.
//	*/
func _generateSortTail(tls *libc.TLS, pParse uintptr, p uintptr, pSort uintptr, nColumn int32, pDest uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aOutEx, pOrderBy, v, v1, v3 uintptr
	var addr, addrBreak, addrContinue, addrOnce, bSeq, eDest, i, i2, iCol, iParm, iRead, iSortTab, iTab, nKey, nRefKey, r1, regRow, regRowid, regSortOut, v2, v4 int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aOutEx, addr, addrBreak, addrContinue, addrOnce, bSeq, eDest, i, i2, iCol, iParm, iRead, iSortTab, iTab, nKey, nRefKey, pOrderBy, r1, regRow, regRowid, regSortOut, v, v1, v2, v3, v4
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe              /* The prepared statement */
	addrBreak = (*TSortCtx)(unsafe.Pointer(pSort)).FlabelDone /* Jump here to exit loop */
	addrContinue = _sqlite3VdbeMakeLabel(tls, pParse)         /* Top of output loop. Jump for Next. */
	addrOnce = 0
	pOrderBy = (*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy
	eDest = int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest)
	iParm = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm /* True if sorter record includes seq. no. */
	nRefKey = 0
	aOutEx = (*TSelect)(unsafe.Pointer(p)).FpEList + 8
	nKey = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr - (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat
	if (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat == 0 || nKey == int32(1) {
		if (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat != 0 {
			v1 = __ccgo_ts + 21775
		} else {
			v1 = __ccgo_ts + 1711
		}
		_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+21789, libc.VaList(bp+8, v1))
	} else {
		_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+21820, libc.VaList(bp+8, nKey))
	}
	if (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TSortCtx)(unsafe.Pointer(pSort)).FregReturn, (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut)
		_sqlite3VdbeGoto(tls, v, addrBreak)
		_sqlite3VdbeResolveLabel(tls, v, (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut)
	}
	iTab = (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor
	if eDest == int32(SRT_Output) || eDest == int32(SRT_Coroutine) || eDest == int32(SRT_Mem) {
		if eDest == int32(SRT_Mem) && (*TSelect)(unsafe.Pointer(p)).FiOffset != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst)
		}
		regRowid = 0
		regRow = (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst
	} else {
		regRowid = _sqlite3GetTempReg(tls, pParse)
		if eDest == int32(SRT_EphemTab) || eDest == int32(SRT_Table) {
			regRow = _sqlite3GetTempReg(tls, pParse)
			nColumn = 0
		} else {
			regRow = _sqlite3GetTempRange(tls, pParse, nColumn)
		}
	}
	if int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&int32(SORTFLAG_UseSorter) != 0 {
		v1 = pParse + 48
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		v2 = *(*int32)(unsafe.Pointer(v1))
		regSortOut = v2
		v3 = pParse + 44
		v4 = *(*int32)(unsafe.Pointer(v3))
		*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
		iSortTab = v4
		if (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut != 0 {
			addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_OpenPseudo), iSortTab, regSortOut, nKey+int32(1)+nColumn+nRefKey)
		if addrOnce != 0 {
			_sqlite3VdbeJumpHere(tls, v, addrOnce)
		}
		addr = int32(1) + _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterSort), iTab, addrBreak)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_SorterData), iTab, regSortOut, iSortTab)
		bSeq = 0
	} else {
		addr = int32(1) + _sqlite3VdbeAddOp2(tls, v, int32(OP_Sort), iTab, addrBreak)
		_codeOffset(tls, v, (*TSelect)(unsafe.Pointer(p)).FiOffset, addrContinue)
		iSortTab = iTab
		bSeq = int32(1)
		if (*TSelect)(unsafe.Pointer(p)).FiOffset > 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), (*TSelect)(unsafe.Pointer(p)).FiLimit, -int32(1))
		}
	}
	i = 0
	iCol = nKey + bSeq - libc.Int32FromInt32(1)
	for {
		if !(i < nColumn) {
			break
		}
		if int32(*(*Tu16)(unsafe.Pointer(aOutEx + uintptr(i)*20 + 16))) == 0 {
			iCol = iCol + 1
		}
		goto _6
	_6:
		;
		i = i + 1
	}
	i = nColumn - int32(1)
	for {
		if !(i >= 0) {
			break
		}
		if *(*Tu16)(unsafe.Pointer(aOutEx + uintptr(i)*20 + 16)) != 0 {
			iRead = int32(*(*Tu16)(unsafe.Pointer(aOutEx + uintptr(i)*20 + 16))) - int32(1)
		} else {
			v2 = iCol
			iCol = iCol - 1
			iRead = v2
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iSortTab, iRead, regRow+i)
		goto _7
	_7:
		;
		i = i - 1
	}
	switch eDest {
	case int32(SRT_Table):
		fallthrough
	case int32(SRT_EphemTab):
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iSortTab, nKey+bSeq, regRow)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iParm, regRowid)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iParm, regRow, regRowid)
		_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND))
	case int32(SRT_Set):
		_sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regRow, nColumn, regRowid, (*TSelectDest)(unsafe.Pointer(pDest)).FzAffSdst, nColumn)
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, regRowid, regRow, nColumn)
	case int32(SRT_Mem):
		/* The LIMIT clause will terminate the loop for us */
	case int32(SRT_Upfrom):
		i2 = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2
		r1 = _sqlite3GetTempReg(tls, pParse)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regRow+libc.BoolInt32(i2 < 0), nColumn-libc.BoolInt32(i2 < 0), r1)
		if i2 < 0 {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iParm, r1, regRow)
		} else {
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, r1, regRow, i2)
		}
	default:
		if eDest == int32(SRT_Output) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst, nColumn)
		} else {
			_sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm)
		}
		break
	}
	if regRowid != 0 {
		if eDest == int32(SRT_Set) {
			_sqlite3ReleaseTempRange(tls, pParse, regRow, nColumn)
		} else {
			_sqlite3ReleaseTempReg(tls, pParse, regRow)
		}
		_sqlite3ReleaseTempReg(tls, pParse, regRowid)
	}
	/* The bottom of the loop
	 */
	_sqlite3VdbeResolveLabel(tls, v, addrContinue)
	if int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&int32(SORTFLAG_UseSorter) != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_SorterNext), iTab, addr)
	} else {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iTab, addr)
	}
	if (*TSortCtx)(unsafe.Pointer(pSort)).FregReturn != 0 {
		_sqlite3VdbeAddOp1(tls, v, int32(OP_Return), (*TSortCtx)(unsafe.Pointer(pSort)).FregReturn)
	}
	_sqlite3VdbeResolveLabel(tls, v, addrBreak)
}

// C documentation
//
//	/*
//	** This routine generates VDBE code to compute the content of a WITH RECURSIVE
//	** query of the form:
//	**
//	**   <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
//	**                         \___________/             \_______________/
//	**                           p->pPrior                      p
//	**
//	**
//	** There is exactly one reference to the recursive-table in the FROM clause
//	** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
//	**
//	** The setup-query runs once to generate an initial set of rows that go
//	** into a Queue table.  Rows are extracted from the Queue table one by
//	** one.  Each row extracted from Queue is output to pDest.  Then the single
//	** extracted row (now in the iCurrent table) becomes the content of the
//	** recursive-table for a recursive-query run.  The output of the recursive-query
//	** is added back into the Queue table.  Then another row is extracted from Queue
//	** and the iteration continues until the Queue table is empty.
//	**
//	** If the compound query operator is UNION then no duplicate rows are ever
//	** inserted into the Queue table.  The iDistinct table keeps a copy of all rows
//	** that have ever been inserted into Queue and causes duplicates to be
//	** discarded.  If the operator is UNION ALL, then duplicates are allowed.
//	**
//	** If the query has an ORDER BY, then entries in the Queue table are kept in
//	** ORDER BY order and the first entry is extracted for each cycle.  Without
//	** an ORDER BY, the Queue table is just a FIFO.
//	**
//	** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
//	** have been output to pDest.  A LIMIT of zero means to output no rows and a
//	** negative LIMIT means to output all rows.  If there is also an OFFSET clause
//	** with a positive value, then the first OFFSET outputs are discarded rather
//	** than being sent to pDest.  The LIMIT count does not begin until after OFFSET
//	** rows have been skipped.
//	*/
func _generateWithRecursiveQuery(tls *libc.TLS, pParse uintptr, p uintptr, pDest uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var addrBreak, addrCont, addrTop, eDest, i, iCurrent, iDistinct, iQueue, nCol, rc, regCurrent, regLimit, regOffset, v1 int32
	var apColl, pFirstRec, pKeyInfo, pKeyInfo1, pLimit, pOrderBy, pSetup, pSrc, v, v4 uintptr
	var _ /* destQueue at bp+0 */ TSelectDest
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrBreak, addrCont, addrTop, apColl, eDest, i, iCurrent, iDistinct, iQueue, nCol, pFirstRec, pKeyInfo, pKeyInfo1, pLimit, pOrderBy, pSetup, pSrc, rc, regCurrent, regLimit, regOffset, v, v1, v4
	pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc                                        /* The FROM clause of the recursive query */
	nCol = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr /* Number of columns in the recursive table */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe                                      /* CONTINUE and BREAK addresses */
	iCurrent = 0                                                                      /* The Queue table */
	iDistinct = 0                                                                     /* To ensure unique results if UNION */
	eDest = int32(SRT_Fifo)                                                           /* Registers used by LIMIT and OFFSET */
	if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21927, 0)
		return
	}
	/* Obtain authorization to do a recursive query */
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_RECURSIVE), uintptr(0), uintptr(0), uintptr(0)) != 0 {
		return
	}
	/* Process the LIMIT and OFFSET clauses, if they exist */
	addrBreak = _sqlite3VdbeMakeLabel(tls, pParse)
	(*TSelect)(unsafe.Pointer(p)).FnSelectRow = int16(320) /* 4 billion rows */
	_computeLimitRegisters(tls, pParse, p, addrBreak)
	pLimit = (*TSelect)(unsafe.Pointer(p)).FpLimit
	regLimit = (*TSelect)(unsafe.Pointer(p)).FiLimit
	regOffset = (*TSelect)(unsafe.Pointer(p)).FiOffset
	(*TSelect)(unsafe.Pointer(p)).FpLimit = uintptr(0)
	v1 = libc.Int32FromInt32(0)
	(*TSelect)(unsafe.Pointer(p)).FiOffset = v1
	(*TSelect)(unsafe.Pointer(p)).FiLimit = v1
	pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy
	/* Locate the cursor number of the Current table */
	i = 0
	for {
		if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) {
			break
		}
		if int32(*(*uint32)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48 + 12 + 4))&0x80>>7) != 0 {
			iCurrent = (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48))).FiCursor
			break
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	/* Allocate cursors numbers for Queue and Distinct.  The cursor number for
	 ** the Distinct table must be exactly one greater than Queue in order
	 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
	v4 = pParse + 44
	v1 = *(*int32)(unsafe.Pointer(v4))
	*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
	iQueue = v1
	if int32((*TSelect)(unsafe.Pointer(p)).Fop) == int32(TK_UNION) {
		if pOrderBy != 0 {
			v1 = int32(SRT_DistQueue)
		} else {
			v1 = int32(SRT_DistFifo)
		}
		eDest = v1
		v4 = pParse + 44
		v1 = *(*int32)(unsafe.Pointer(v4))
		*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
		iDistinct = v1
	} else {
		if pOrderBy != 0 {
			v1 = int32(SRT_Queue)
		} else {
			v1 = int32(SRT_Fifo)
		}
		eDest = v1
	}
	_sqlite3SelectDestInit(tls, bp, eDest, iQueue)
	/* Allocate cursors for Current, Queue, and Distinct. */
	v4 = pParse + 48
	*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
	v1 = *(*int32)(unsafe.Pointer(v4))
	regCurrent = v1
	_sqlite3VdbeAddOp3(tls, v, int32(OP_OpenPseudo), iCurrent, regCurrent, nCol)
	if pOrderBy != 0 {
		pKeyInfo = _multiSelectByMergeKeyInfo(tls, pParse, p, int32(1))
		_sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), iQueue, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr+int32(2), 0, pKeyInfo, -int32(9))
		(**(**TSelectDest)(__ccgo_up(bp))).FpOrderBy = pOrderBy
	} else {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), iQueue, nCol)
	}
	if iDistinct != 0 { /* For looping through pKeyInfo->aColl[] */
		nCol = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr
		pKeyInfo1 = _sqlite3KeyInfoAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, nCol, int32(1))
		if pKeyInfo1 != 0 {
			i = 0
			apColl = pKeyInfo1 + 20
			for {
				if !(i < nCol) {
					break
				}
				**(**uintptr)(__ccgo_up(apColl)) = _multiSelectCollSeq(tls, pParse, p, i)
				if uintptr(0) == **(**uintptr)(__ccgo_up(apColl)) {
					**(**uintptr)(__ccgo_up(apColl)) = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FpDfltColl
				}
				goto _11
			_11:
				;
				i = i + 1
				apColl += 4
			}
			_sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), iDistinct, nCol, 0, pKeyInfo1, -int32(9))
		} else {
		}
	}
	/* Detach the ORDER BY clause from the compound SELECT */
	(*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0)
	/* Figure out how many elements of the compound SELECT are part of the
	 ** recursive query.  Make sure no recursive elements use aggregate
	 ** functions.  Mark the recursive elements as UNION ALL even if they
	 ** are really UNION because the distinctness will be enforced by the
	 ** iDistinct table.  pFirstRec is left pointing to the left-most
	 ** recursive term of the CTE.
	 */
	pFirstRec = p
	for {
		if !(pFirstRec != uintptr(0)) {
			break
		}
		if (*TSelect)(unsafe.Pointer(pFirstRec)).FselFlags&uint32(SF_Aggregate) != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21976, 0)
			goto end_of_recursive_query
		}
		(*TSelect)(unsafe.Pointer(pFirstRec)).Fop = uint8(TK_ALL)
		if (*TSelect)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior)).FselFlags&uint32(SF_Recursive) == uint32(0) {
			break
		}
		goto _12
	_12:
		;
		pFirstRec = (*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior
	}
	/* Store the results of the setup-query in Queue. */
	pSetup = (*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior
	(*TSelect)(unsafe.Pointer(pSetup)).FpNext = uintptr(0)
	_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22018, 0)
	rc = _sqlite3Select(tls, pParse, pSetup, bp)
	(*TSelect)(unsafe.Pointer(pSetup)).FpNext = p
	if rc != 0 {
		goto end_of_recursive_query
	}
	/* Find the next row in the Queue and output that row */
	addrTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iQueue, addrBreak)
	/* Transfer the next row in Queue over to Current */
	_sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), iCurrent) /* To reset column cache */
	if pOrderBy != 0 {
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iQueue, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr+int32(1), regCurrent)
	} else {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_RowData), iQueue, regCurrent)
	}
	_sqlite3VdbeAddOp1(tls, v, int32(OP_Delete), iQueue)
	/* Output the single row in Current */
	addrCont = _sqlite3VdbeMakeLabel(tls, pParse)
	_codeOffset(tls, v, regOffset, addrCont)
	_selectInnerLoop(tls, pParse, p, iCurrent, uintptr(0), uintptr(0), pDest, addrCont, addrBreak)
	if regLimit != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_DecrJumpZero), regLimit, addrBreak)
	}
	_sqlite3VdbeResolveLabel(tls, v, addrCont)
	/* Execute the recursive SELECT taking the single row in Current as
	 ** the value for the recursive-table. Store the results in the Queue.
	 */
	(*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior = uintptr(0)
	_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22024, 0)
	_sqlite3Select(tls, pParse, p, bp)
	(*TSelect)(unsafe.Pointer(pFirstRec)).FpPrior = pSetup
	/* Keep running the loop until the Queue is empty */
	_sqlite3VdbeGoto(tls, v, addrTop)
	_sqlite3VdbeResolveLabel(tls, v, addrBreak)
	goto end_of_recursive_query
end_of_recursive_query:
	;
	_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TSelect)(unsafe.Pointer(p)).FpOrderBy)
	(*TSelect)(unsafe.Pointer(p)).FpOrderBy = pOrderBy
	(*TSelect)(unsafe.Pointer(p)).FpLimit = pLimit
	return
}

// C documentation
//
//	/*
//	** Each call to sqlite3_rtree_geometry_callback() or
//	** sqlite3_rtree_query_callback() creates an ordinary SQLite
//	** scalar function that is implemented by this routine.
//	**
//	** All this function does is construct an RtreeMatchArg object that
//	** contains the geometry-checking callback routines and a list of
//	** parameters to this function, then return that RtreeMatchArg object
//	** as a BLOB.
//	**
//	** The R-Tree MATCH operator will read the returned BLOB, deserialize
//	** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
//	** out which elements of the R-Tree should be returned by the query.
//	*/
func _geomCallback(tls *libc.TLS, ctx uintptr, nArg int32, aArg uintptr) {
	var i, memErr int32
	var nBlob Tsqlite3_int64
	var pBlob, pGeomCtx uintptr
	_, _, _, _, _ = i, memErr, nBlob, pBlob, pGeomCtx
	pGeomCtx = Xsqlite3_user_data(tls, ctx)
	memErr = 0
	nBlob = int64(uint32(libc.UintptrFromInt32(0)+32) + uint32(nArg)*uint32(8) + uint32(nArg)*uint32(4))
	pBlob = Xsqlite3_malloc64(tls, uint64(nBlob))
	if !(pBlob != 0) {
		Xsqlite3_result_error_nomem(tls, ctx)
	} else {
		(*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FiSize = uint32(nBlob)
		(*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb = **(**TRtreeGeomCallback)(__ccgo_up(pGeomCtx))
		(*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam = pBlob + 32 + uintptr(nArg)*8
		(*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FnParam = nArg
		i = 0
		for {
			if !(i < nArg) {
				break
			}
			**(**uintptr)(__ccgo_up((*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam + uintptr(i)*4)) = Xsqlite3_value_dup(tls, **(**uintptr)(__ccgo_up(aArg + uintptr(i)*4)))
			if **(**uintptr)(__ccgo_up((*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam + uintptr(i)*4)) == uintptr(0) {
				memErr = int32(1)
			}
			*(*TRtreeDValue)(unsafe.Pointer(pBlob + 32 + uintptr(i)*8)) = Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(aArg + uintptr(i)*4)))
			goto _1
		_1:
			;
			i = i + 1
		}
		if memErr != 0 {
			Xsqlite3_result_error_nomem(tls, ctx)
			_rtreeMatchArgFree(tls, pBlob)
		} else {
			Xsqlite3_result_pointer(tls, ctx, pBlob, __ccgo_ts+28732, __ccgo_fp(_rtreeMatchArgFree))
		}
	}
}

// C documentation
//
//	/*
//	** If pPoly is a polygon, compute its bounding box. Then:
//	**
//	**    (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
//	**    (2) otherwise, compute a GeoPoly for the bounding box and return the
//	**        new GeoPoly
//	**
//	** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
//	** the bounding box in aCoord and return a pointer to that GeoPoly.
//	*/
func _geopolyBBox(tls *libc.TLS, context uintptr, pPoly uintptr, aCoord uintptr, pRc uintptr) (r1 uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var mnX, mnY, mxX, mxY, v3 float32
	var p, pOut uintptr
	var r float64
	var _ /* ii at bp+0 */ int32
	_, _, _, _, _, _, _, _ = mnX, mnY, mxX, mxY, p, pOut, r, v3
	pOut = uintptr(0)
	if pPoly == uintptr(0) && aCoord != uintptr(0) {
		p = uintptr(0)
		mnX = *(*TRtreeValue)(unsafe.Pointer(aCoord))
		mxX = *(*TRtreeValue)(unsafe.Pointer(aCoord + 1*4))
		mnY = *(*TRtreeValue)(unsafe.Pointer(aCoord + 2*4))
		mxY = *(*TRtreeValue)(unsafe.Pointer(aCoord + 3*4))
		goto geopolyBboxFill
	} else {
		p = _geopolyFuncParam(tls, context, pPoly, pRc)
	}
	if !(p != 0) {
		goto _1
	}
	v3 = **(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2))*4))
	mxX = v3
	mnX = v3
	v3 = **(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4))
	mxY = v3
	mnY = v3
	**(**int32)(__ccgo_up(bp)) = int32(1)
	for {
		if !(**(**int32)(__ccgo_up(bp)) < (*TGeoPoly)(unsafe.Pointer(p)).FnVertex) {
			break
		}
		r = float64(**(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(**(**int32)(__ccgo_up(bp))*int32(2))*4)))
		if r < float64(mnX) {
			mnX = float32(r)
		} else {
			if r > float64(mxX) {
				mxX = float32(r)
			}
		}
		r = float64(**(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(**(**int32)(__ccgo_up(bp))*int32(2)+int32(1))*4)))
		if r < float64(mnY) {
			mnY = float32(r)
		} else {
			if r > float64(mxY) {
				mxY = float32(r)
			}
		}
		goto _5
	_5:
		;
		**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + 1
	}
	if pRc != 0 {
		**(**int32)(__ccgo_up(pRc)) = SQLITE_OK
	}
	if !(aCoord == uintptr(0)) {
		goto _6
	}
	goto geopolyBboxFill
geopolyBboxFill:
	;
	pOut = Xsqlite3_realloc64(tls, p, uint64(libc.Uint32FromInt64(40)+libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2)*uint32(libc.Int32FromInt32(4)-libc.Int32FromInt32(4))))
	if pOut == uintptr(0) {
		Xsqlite3_free(tls, p)
		if context != 0 {
			Xsqlite3_result_error_nomem(tls, context)
		}
		if pRc != 0 {
			**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
		}
		return uintptr(0)
	}
	(*TGeoPoly)(unsafe.Pointer(pOut)).FnVertex = int32(4)
	**(**int32)(__ccgo_up(bp)) = int32(1)
	**(**uint8)(__ccgo_up(pOut + 4)) = **(**uint8)(__ccgo_up(bp))
	**(**uint8)(__ccgo_up(pOut + 4 + 1)) = uint8(0)
	**(**uint8)(__ccgo_up(pOut + 4 + 2)) = uint8(0)
	**(**uint8)(__ccgo_up(pOut + 4 + 3)) = uint8(4)
	**(**TGeoCoord)(__ccgo_up(pOut + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2))*4)) = mnX
	**(**TGeoCoord)(__ccgo_up(pOut + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)) = mnY
	**(**TGeoCoord)(__ccgo_up(pOut + 8 + uintptr(libc.Int32FromInt32(1)*libc.Int32FromInt32(2))*4)) = mxX
	**(**TGeoCoord)(__ccgo_up(pOut + 8 + uintptr(libc.Int32FromInt32(1)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)) = mnY
	**(**TGeoCoord)(__ccgo_up(pOut + 8 + uintptr(libc.Int32FromInt32(2)*libc.Int32FromInt32(2))*4)) = mxX
	**(**TGeoCoord)(__ccgo_up(pOut + 8 + uintptr(libc.Int32FromInt32(2)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)) = mxY
	**(**TGeoCoord)(__ccgo_up(pOut + 8 + uintptr(libc.Int32FromInt32(3)*libc.Int32FromInt32(2))*4)) = mnX
	**(**TGeoCoord)(__ccgo_up(pOut + 8 + uintptr(libc.Int32FromInt32(3)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)) = mxY
	goto _7
_6:
	;
	Xsqlite3_free(tls, p)
	*(*TRtreeValue)(unsafe.Pointer(aCoord)) = mnX
	*(*TRtreeValue)(unsafe.Pointer(aCoord + 1*4)) = mxX
	*(*TRtreeValue)(unsafe.Pointer(aCoord + 2*4)) = mnY
	*(*TRtreeValue)(unsafe.Pointer(aCoord + 3*4)) = mxY
_7:
	;
	goto _2
_1:
	;
	if aCoord != 0 {
		libc.Xmemset(tls, aCoord, 0, libc.Uint32FromInt64(4)*libc.Uint32FromInt32(4))
	}
_2:
	;
	return pOut
}

// C documentation
//
//	/*
//	** GEOPOLY virtual table module xColumn method.
//	*/
func _geopolyColumn(tls *libc.TLS, cur uintptr, ctx uintptr, i int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var p, pCsr, pNode, pRtree uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _ = p, pCsr, pNode, pRtree
	pRtree = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab
	pCsr = cur
	p = _rtreeSearchPointFirst(tls, pCsr)
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	pNode = _rtreeNodeOfFirstSearchPoint(tls, pCsr, bp)
	if **(**int32)(__ccgo_up(bp)) != 0 {
		return **(**int32)(__ccgo_up(bp))
	}
	if p == uintptr(0) {
		return SQLITE_OK
	}
	if i == 0 && Xsqlite3_vtab_nochange(tls, ctx) != 0 {
		return SQLITE_OK
	}
	if i <= int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux) {
		if !((*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid != 0) {
			if (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux == uintptr(0) {
				**(**int32)(__ccgo_up(bp)) = Xsqlite3_prepare_v3(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb, (*TRtree)(unsafe.Pointer(pRtree)).FzReadAuxSql, -int32(1), uint32(0), pCsr+36, uintptr(0))
				if **(**int32)(__ccgo_up(bp)) != 0 {
					return **(**int32)(__ccgo_up(bp))
				}
			}
			Xsqlite3_bind_int64(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux, int32(1), _nodeGetRowid(tls, pRtree, pNode, int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell)))
			**(**int32)(__ccgo_up(bp)) = Xsqlite3_step(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux)
			if **(**int32)(__ccgo_up(bp)) == int32(SQLITE_ROW) {
				(*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid = uint8(1)
			} else {
				Xsqlite3_reset(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux)
				if **(**int32)(__ccgo_up(bp)) == int32(SQLITE_DONE) {
					**(**int32)(__ccgo_up(bp)) = SQLITE_OK
				}
				return **(**int32)(__ccgo_up(bp))
			}
		}
		Xsqlite3_result_value(tls, ctx, Xsqlite3_column_value(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux, i+int32(2)))
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** GEOPOLY virtual table module xFilter method.
//	**
//	** Query plans:
//	**
//	**      1         rowid lookup
//	**      2         search for objects overlapping the same bounding box
//	**                that contains polygon argv[0]
//	**      3         search for objects overlapping the same bounding box
//	**                that contains polygon argv[0]
//	**      4         full table scan
//	*/
func _geopolyFilter(tls *libc.TLS, pVtabCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var iRowid Ti64
	var p, p1, pCsr, pNew, pRtree, v1 uintptr
	var _ /* bbox at bp+24 */ [4]TRtreeCoord
	var _ /* iCell at bp+8 */ int32
	var _ /* iNode at bp+16 */ Ti64
	var _ /* pLeaf at bp+12 */ uintptr
	var _ /* pRoot at bp+0 */ uintptr
	var _ /* rc at bp+4 */ int32
	_, _, _, _, _, _, _ = iRowid, p, p1, pCsr, pNew, pRtree, v1
	pRtree = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVtabCursor)).FpVtab
	pCsr = pVtabCursor
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 4)) = SQLITE_OK
	**(**int32)(__ccgo_up(bp + 8)) = 0
	_ = idxStr
	_rtreeReference(tls, pRtree)
	/* Reset the cursor to the same state as rtreeOpen() leaves it in. */
	_resetCursor(tls, pCsr)
	(*TRtreeCursor)(unsafe.Pointer(pCsr)).FiStrategy = idxNum
	if idxNum == int32(1) { /* Search point for the leaf */
		iRowid = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv)))
		**(**Ti64)(__ccgo_up(bp + 16)) = 0
		**(**int32)(__ccgo_up(bp + 4)) = _findLeafNode(tls, pRtree, iRowid, bp+12, bp+16)
		if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK && **(**uintptr)(__ccgo_up(bp + 12)) != uintptr(0) {
			p = _rtreeSearchPointNew(tls, pCsr, float64(0), uint8(0))
			/* Always returns pCsr->sPoint */
			**(**uintptr)(__ccgo_up(pCsr + 64)) = **(**uintptr)(__ccgo_up(bp + 12))
			(*TRtreeSearchPoint)(unsafe.Pointer(p)).Fid = **(**Ti64)(__ccgo_up(bp + 16))
			(*TRtreeSearchPoint)(unsafe.Pointer(p)).FeWithin = uint8(PARTLY_WITHIN)
			**(**int32)(__ccgo_up(bp + 4)) = _nodeRowidIndex(tls, pRtree, **(**uintptr)(__ccgo_up(bp + 12)), iRowid, bp+8)
			(*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell = uint8(**(**int32)(__ccgo_up(bp + 8)))
		} else {
			(*TRtreeCursor)(unsafe.Pointer(pCsr)).FatEOF = uint8(1)
		}
	} else {
		/* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
		 ** with the configured constraints.
		 */
		**(**int32)(__ccgo_up(bp + 4)) = _nodeAcquire(tls, pRtree, int64(1), uintptr(0), bp)
		if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK && idxNum <= int32(3) {
			_geopolyBBox(tls, uintptr(0), **(**uintptr)(__ccgo_up(argv)), bp+24, bp+4)
			if **(**int32)(__ccgo_up(bp + 4)) != 0 {
				goto geopoly_filter_end
			}
			v1 = Xsqlite3_malloc(tls, int32(libc.Uint32FromInt64(24)*libc.Uint32FromInt32(4)))
			p1 = v1
			(*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint = v1
			(*TRtreeCursor)(unsafe.Pointer(pCsr)).FnConstraint = int32(4)
			if p1 == uintptr(0) {
				**(**int32)(__ccgo_up(bp + 4)) = int32(SQLITE_NOMEM)
			} else {
				libc.Xmemset(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint, 0, libc.Uint32FromInt64(24)*libc.Uint32FromInt32(4))
				libc.Xmemset(tls, pCsr+84, 0, uint32(4)*uint32((*TRtree)(unsafe.Pointer(pRtree)).FiDepth+libc.Int32FromInt32(1)))
				if idxNum == int32(2) {
					/* Overlap query */
					(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('B')
					(*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = 0
					*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 1*4)))
					p1 += 24
					(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('D')
					(*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(1)
					*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24)))
					p1 += 24
					(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('B')
					(*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(2)
					*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 3*4)))
					p1 += 24
					(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('D')
					(*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(3)
					*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 2*4)))
				} else {
					/* Within query */
					(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('D')
					(*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = 0
					*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24)))
					p1 += 24
					(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('B')
					(*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(1)
					*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 1*4)))
					p1 += 24
					(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('D')
					(*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(2)
					*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 2*4)))
					p1 += 24
					(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32('B')
					(*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(3)
					*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(*(*TRtreeValue)(unsafe.Pointer(bp + 24 + 3*4)))
				}
			}
		}
		if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
			pNew = _rtreeSearchPointNew(tls, pCsr, float64(0), uint8((*TRtree)(unsafe.Pointer(pRtree)).FiDepth+libc.Int32FromInt32(1)))
			if pNew == uintptr(0) {
				**(**int32)(__ccgo_up(bp + 4)) = int32(SQLITE_NOMEM)
				goto geopoly_filter_end
			}
			(*TRtreeSearchPoint)(unsafe.Pointer(pNew)).Fid = int64(1)
			(*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FiCell = uint8(0)
			(*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FeWithin = uint8(PARTLY_WITHIN)
			**(**uintptr)(__ccgo_up(pCsr + 64)) = **(**uintptr)(__ccgo_up(bp))
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			**(**int32)(__ccgo_up(bp + 4)) = _rtreeStepToLeaf(tls, pCsr)
		}
	}
	goto geopoly_filter_end
geopoly_filter_end:
	;
	_nodeRelease(tls, pRtree, **(**uintptr)(__ccgo_up(bp)))
	_rtreeRelease(tls, pRtree)
	return **(**int32)(__ccgo_up(bp + 4))
}

// C documentation
//
//	/*
//	** Given a function parameter, try to interpret it as a polygon, either
//	** in the binary format or JSON text.  Compute a GeoPoly object and
//	** return a pointer to that object.  Or if the input is not a well-formed
//	** polygon, put an error message in sqlite3_context and return NULL.
//	*/
func _geopolyFuncParam(tls *libc.TLS, pCtx uintptr, pVal uintptr, pRc uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a, p, zJson, v4 uintptr
	var ii, nByte, nVertex, v1 int32
	var v2 bool
	var _ /* x at bp+0 */ int32
	_, _, _, _, _, _, _, _, _ = a, ii, nByte, nVertex, p, zJson, v1, v2, v4
	p = uintptr(0)
	if v2 = Xsqlite3_value_type(tls, pVal) == int32(SQLITE_BLOB); v2 {
		v1 = Xsqlite3_value_bytes(tls, pVal)
		nByte = v1
	}
	if v2 && v1 >= int32(libc.Uint32FromInt32(4)+libc.Uint32FromInt32(6)*libc.Uint32FromInt64(4)) {
		a = Xsqlite3_value_blob(tls, pVal)
		if a == uintptr(0) {
			if pCtx != 0 {
				Xsqlite3_result_error_nomem(tls, pCtx)
			}
			return uintptr(0)
		}
		nVertex = int32(**(**uint8)(__ccgo_up(a + 1)))<<int32(16) + int32(**(**uint8)(__ccgo_up(a + 2)))<<int32(8) + int32(**(**uint8)(__ccgo_up(a + 3)))
		if (int32(**(**uint8)(__ccgo_up(a))) == 0 || int32(**(**uint8)(__ccgo_up(a))) == int32(1)) && uint32(nVertex*int32(2))*uint32(4)+uint32(4) == uint32(nByte) {
			p = Xsqlite3_malloc64(tls, uint64(uint32(40)+uint32((nVertex-int32(1))*int32(2))*uint32(4)))
			if p == uintptr(0) {
				if pRc != 0 {
					**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
				}
				if pCtx != 0 {
					Xsqlite3_result_error_nomem(tls, pCtx)
				}
			} else {
				**(**int32)(__ccgo_up(bp)) = int32(1)
				(*TGeoPoly)(unsafe.Pointer(p)).FnVertex = nVertex
				libc.Xmemcpy(tls, p+4, a, uint32(nByte))
				if int32(**(**uint8)(__ccgo_up(a))) != int32(**(**uint8)(__ccgo_up(bp))) {
					ii = 0
					for {
						if !(ii < nVertex) {
							break
						}
						_geopolySwab32(tls, p+8+uintptr(ii*int32(2))*4)
						_geopolySwab32(tls, p+8+uintptr(ii*int32(2)+int32(1))*4)
						goto _3
					_3:
						;
						ii = ii + 1
					}
					v4 = p + 4
					*(*uint8)(unsafe.Pointer(v4)) = uint8(int32(*(*uint8)(unsafe.Pointer(v4))) ^ libc.Int32FromInt32(1))
				}
			}
		}
		if pRc != 0 {
			**(**int32)(__ccgo_up(pRc)) = SQLITE_OK
		}
		return p
	} else {
		if Xsqlite3_value_type(tls, pVal) == int32(SQLITE_TEXT) {
			zJson = Xsqlite3_value_text(tls, pVal)
			if zJson == uintptr(0) {
				if pRc != 0 {
					**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
				}
				return uintptr(0)
			}
			return _geopolyParseJson(tls, zJson, pRc)
		} else {
			if pRc != 0 {
				**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_ERROR)
			}
			return uintptr(0)
		}
	}
	return r
}

// C documentation
//
//	/*
//	** This function is the implementation of both the xConnect and xCreate
//	** methods of the geopoly virtual table.
//	**
//	**   argv[0]   -> module name
//	**   argv[1]   -> database name
//	**   argv[2]   -> table name
//	**   argv[...] -> column names...
//	*/
func _geopolyInit(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr, isCreate int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var ii, rc, v2 int32
	var nDb, nName Tsqlite3_int64
	var pRtree, pSql, zSql uintptr
	_, _, _, _, _, _, _, _ = ii, nDb, nName, pRtree, pSql, rc, zSql, v2
	rc = SQLITE_OK
	_ = pAux
	if argc >= libc.Int32FromInt32(RTREE_MAX_AUX_COLUMN)+libc.Int32FromInt32(4) {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+31147, 0)
		return int32(SQLITE_ERROR)
	}
	Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_CONSTRAINT_SUPPORT), libc.VaList(bp+8, int32(1)))
	Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_INNOCUOUS), 0)
	/* Allocate the sqlite3_vtab structure */
	nDb = int64(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + 1*4))))
	nName = int64(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + 2*4))))
	pRtree = Xsqlite3_malloc64(tls, uint64(int64(512)+nDb+nName*int64(2)+int64(8)))
	if !(pRtree != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pRtree, 0, uint32(int64(512)+nDb+nName*int64(2)+int64(8)))
	(*TRtree)(unsafe.Pointer(pRtree)).FnBusy = uint32(1)
	(*TRtree)(unsafe.Pointer(pRtree)).Fbase.FpModule = uintptr(unsafe.Pointer(&_rtreeModule))
	(*TRtree)(unsafe.Pointer(pRtree)).FzDb = pRtree + 1*512
	(*TRtree)(unsafe.Pointer(pRtree)).FzName = (*TRtree)(unsafe.Pointer(pRtree)).FzDb + uintptr(nDb+int64(1))
	(*TRtree)(unsafe.Pointer(pRtree)).FzNodeName = (*TRtree)(unsafe.Pointer(pRtree)).FzName + uintptr(nName+int64(1))
	(*TRtree)(unsafe.Pointer(pRtree)).FeCoordType = uint8(RTREE_COORD_REAL32)
	(*TRtree)(unsafe.Pointer(pRtree)).FnDim = uint8(2)
	(*TRtree)(unsafe.Pointer(pRtree)).FnDim2 = uint8(4)
	libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, **(**uintptr)(__ccgo_up(argv + 1*4)), uint32(nDb))
	libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzName, **(**uintptr)(__ccgo_up(argv + 2*4)), uint32(nName))
	libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName, **(**uintptr)(__ccgo_up(argv + 2*4)), uint32(nName))
	libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName+uintptr(nName), __ccgo_ts+30189, uint32(6))
	/* Create/Connect to the underlying relational database schema. If
	 ** that is successful, call sqlite3_declare_vtab() to configure
	 ** the r-tree table schema.
	 */
	pSql = Xsqlite3_str_new(tls, db)
	Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+31184, 0)
	(*TRtree)(unsafe.Pointer(pRtree)).FnAux = uint16(1)       /* Add one for _shape */
	(*TRtree)(unsafe.Pointer(pRtree)).FnAuxNotNull = uint8(1) /* The _shape column is always not-null */
	ii = int32(3)
	for {
		if !(ii < argc) {
			break
		}
		(*TRtree)(unsafe.Pointer(pRtree)).FnAux = (*TRtree)(unsafe.Pointer(pRtree)).FnAux + 1
		Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+31206, libc.VaList(bp+8, **(**uintptr)(__ccgo_up(argv + uintptr(ii)*4))))
		goto _1
	_1:
		;
		ii = ii + 1
	}
	Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+30246, 0)
	zSql = Xsqlite3_str_finish(tls, pSql)
	if !(zSql != 0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		v2 = Xsqlite3_declare_vtab(tls, db, zSql)
		rc = v2
		if SQLITE_OK != v2 {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db)))
		}
	}
	Xsqlite3_free(tls, zSql)
	if rc != 0 {
		goto geopolyInit_fail
	}
	(*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell = uint8(int32(8) + int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)*int32(4))
	/* Figure out the node size to use. */
	rc = _getNodeSize(tls, db, pRtree, isCreate, pzErr)
	if rc != 0 {
		goto geopolyInit_fail
	}
	rc = _rtreeSqlInit(tls, pRtree, db, **(**uintptr)(__ccgo_up(argv + 1*4)), **(**uintptr)(__ccgo_up(argv + 2*4)), isCreate)
	if rc != 0 {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db)))
		goto geopolyInit_fail
	}
	**(**uintptr)(__ccgo_up(ppVtab)) = pRtree
	return SQLITE_OK
	goto geopolyInit_fail
geopolyInit_fail:
	;
	if rc == SQLITE_OK {
		rc = int32(SQLITE_ERROR)
	}
	_rtreeRelease(tls, pRtree)
	return rc
}

// C documentation
//
//	/*
//	** Determine the overlap between two polygons
//	*/
func _geopolyOverlap(tls *libc.TLS, p1 uintptr, p2 uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iMask, needSort, rc, v1 int32
	var nByte, nVertex Tsqlite3_int64
	var p, pActive, pPrev, pSeg, pThisEvent, v5 uintptr
	var rX, y, v2 float64
	var _ /* aOverlap at bp+0 */ [4]uint8
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = iMask, nByte, nVertex, needSort, p, pActive, pPrev, pSeg, pThisEvent, rX, rc, y, v1, v2, v5
	nVertex = int64((*TGeoPoly)(unsafe.Pointer(p1)).FnVertex + (*TGeoPoly)(unsafe.Pointer(p2)).FnVertex + int32(2))
	rc = 0
	needSort = 0
	pActive = uintptr(0)
	nByte = int64(24)*nVertex*int64(2) + int64(40)*nVertex + int64(16)
	p = Xsqlite3_malloc64(tls, uint64(nByte))
	if p == uintptr(0) {
		return -int32(1)
	}
	(*TGeoOverlap)(unsafe.Pointer(p)).FaEvent = p + 1*16
	(*TGeoOverlap)(unsafe.Pointer(p)).FaSegment = (*TGeoOverlap)(unsafe.Pointer(p)).FaEvent + uintptr(nVertex*int64(2))*24
	v1 = libc.Int32FromInt32(0)
	(*TGeoOverlap)(unsafe.Pointer(p)).FnSegment = v1
	(*TGeoOverlap)(unsafe.Pointer(p)).FnEvent = v1
	_geopolyAddSegments(tls, p, p1, uint8(1))
	_geopolyAddSegments(tls, p, p2, uint8(2))
	pThisEvent = _geopolySortEventsByX(tls, (*TGeoOverlap)(unsafe.Pointer(p)).FaEvent, (*TGeoOverlap)(unsafe.Pointer(p)).FnEvent)
	if pThisEvent != 0 && (*TGeoEvent)(unsafe.Pointer(pThisEvent)).Fx == float64(0) {
		v2 = -libc.Float64FromFloat64(1)
	} else {
		v2 = float64(0)
	}
	rX = v2
	libc.Xmemset(tls, bp, 0, uint32(4))
	for pThisEvent != 0 {
		if (*TGeoEvent)(unsafe.Pointer(pThisEvent)).Fx != rX {
			pPrev = uintptr(0)
			iMask = 0
			rX = (*TGeoEvent)(unsafe.Pointer(pThisEvent)).Fx
			if needSort != 0 {
				pActive = _geopolySortSegmentsByYAndC(tls, pActive)
				needSort = 0
			}
			pSeg = pActive
			for {
				if !(pSeg != 0) {
					break
				}
				if pPrev != 0 {
					if (*TGeoSegment)(unsafe.Pointer(pPrev)).Fy != (*TGeoSegment)(unsafe.Pointer(pSeg)).Fy {
						(**(**[4]uint8)(__ccgo_up(bp)))[iMask] = uint8(1)
					}
				}
				iMask = iMask ^ int32((*TGeoSegment)(unsafe.Pointer(pSeg)).Fside)
				pPrev = pSeg
				goto _3
			_3:
				;
				pSeg = (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext
			}
			pPrev = uintptr(0)
			pSeg = pActive
			for {
				if !(pSeg != 0) {
					break
				}
				y = float64((*TGeoSegment)(unsafe.Pointer(pSeg)).FC*rX) + (*TGeoSegment)(unsafe.Pointer(pSeg)).FB
				(*TGeoSegment)(unsafe.Pointer(pSeg)).Fy = y
				if pPrev != 0 {
					if (*TGeoSegment)(unsafe.Pointer(pPrev)).Fy > (*TGeoSegment)(unsafe.Pointer(pSeg)).Fy && int32((*TGeoSegment)(unsafe.Pointer(pPrev)).Fside) != int32((*TGeoSegment)(unsafe.Pointer(pSeg)).Fside) {
						rc = int32(1)
						goto geopolyOverlapDone
					} else {
						if (*TGeoSegment)(unsafe.Pointer(pPrev)).Fy != (*TGeoSegment)(unsafe.Pointer(pSeg)).Fy {
							(**(**[4]uint8)(__ccgo_up(bp)))[iMask] = uint8(1)
						}
					}
				}
				iMask = iMask ^ int32((*TGeoSegment)(unsafe.Pointer(pSeg)).Fside)
				pPrev = pSeg
				goto _4
			_4:
				;
				pSeg = (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext
			}
		}
		if (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FeType == 0 {
			/* Add a segment */
			pSeg = (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FpSeg
			(*TGeoSegment)(unsafe.Pointer(pSeg)).Fy = float64((*TGeoSegment)(unsafe.Pointer(pSeg)).Fy0)
			(*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext = pActive
			pActive = pSeg
			needSort = int32(1)
		} else {
			/* Remove a segment */
			if pActive == (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FpSeg {
				if pActive != 0 {
					v5 = (*TGeoSegment)(unsafe.Pointer(pActive)).FpNext
				} else {
					v5 = uintptr(0)
				}
				pActive = v5
			} else {
				pSeg = pActive
				for {
					if !(pSeg != 0) {
						break
					}
					if (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext == (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FpSeg {
						if (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext != 0 {
							v5 = (*TGeoSegment)(unsafe.Pointer((*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext)).FpNext
						} else {
							v5 = uintptr(0)
						}
						(*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext = v5
						break
					}
					goto _6
				_6:
					;
					pSeg = (*TGeoSegment)(unsafe.Pointer(pSeg)).FpNext
				}
			}
		}
		pThisEvent = (*TGeoEvent)(unsafe.Pointer(pThisEvent)).FpNext
	}
	if int32((**(**[4]uint8)(__ccgo_up(bp)))[int32(3)]) == 0 {
		rc = 0
	} else {
		if int32((**(**[4]uint8)(__ccgo_up(bp)))[int32(1)]) != 0 && int32((**(**[4]uint8)(__ccgo_up(bp)))[int32(2)]) == 0 {
			rc = int32(3)
		} else {
			if int32((**(**[4]uint8)(__ccgo_up(bp)))[int32(1)]) == 0 && int32((**(**[4]uint8)(__ccgo_up(bp)))[int32(2)]) != 0 {
				rc = int32(2)
			} else {
				if int32((**(**[4]uint8)(__ccgo_up(bp)))[int32(1)]) == 0 && int32((**(**[4]uint8)(__ccgo_up(bp)))[int32(2)]) == 0 {
					rc = int32(4)
				} else {
					rc = int32(1)
				}
			}
		}
	}
	goto geopolyOverlapDone
geopolyOverlapDone:
	;
	Xsqlite3_free(tls, p)
	return rc
}

// C documentation
//
//	/*
//	** If the input is a well-formed JSON array of coordinates with at least
//	** four coordinates and where each coordinate is itself a two-value array,
//	** then convert the JSON into a GeoPoly object and return a pointer to
//	** that object.
//	**
//	** If any error occurs, return NULL.
//	*/
func _geopolyParseJson(tls *libc.TLS, z uintptr, pRc uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aNew, pOut, v1 uintptr
	var c int8
	var ii, rc int32
	var v2 bool
	var _ /* s at bp+0 */ TGeoParse
	var _ /* x at bp+20 */ int32
	_, _, _, _, _, _, _ = aNew, c, ii, pOut, rc, v1, v2
	rc = SQLITE_OK
	libc.Xmemset(tls, bp, 0, uint32(20))
	(**(**TGeoParse)(__ccgo_up(bp))).Fz = z
	if int32(_geopolySkipSpace(tls, bp)) == int32('[') {
		(**(**TGeoParse)(__ccgo_up(bp))).Fz = (**(**TGeoParse)(__ccgo_up(bp))).Fz + 1
		for int32(_geopolySkipSpace(tls, bp)) == int32('[') {
			ii = 0
			(**(**TGeoParse)(__ccgo_up(bp))).Fz = (**(**TGeoParse)(__ccgo_up(bp))).Fz + 1
			if (**(**TGeoParse)(__ccgo_up(bp))).FnVertex >= (**(**TGeoParse)(__ccgo_up(bp))).FnAlloc {
				(**(**TGeoParse)(__ccgo_up(bp))).FnAlloc = (**(**TGeoParse)(__ccgo_up(bp))).FnAlloc*int32(2) + int32(16)
				aNew = Xsqlite3_realloc64(tls, (**(**TGeoParse)(__ccgo_up(bp))).Fa, uint64(uint32((**(**TGeoParse)(__ccgo_up(bp))).FnAlloc)*uint32(4)*uint32(2)))
				if aNew == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
					(**(**TGeoParse)(__ccgo_up(bp))).FnErr = (**(**TGeoParse)(__ccgo_up(bp))).FnErr + 1
					break
				}
				(**(**TGeoParse)(__ccgo_up(bp))).Fa = aNew
			}
			for {
				if ii <= int32(1) {
					v1 = (**(**TGeoParse)(__ccgo_up(bp))).Fa + uintptr((**(**TGeoParse)(__ccgo_up(bp))).FnVertex*int32(2)+ii)*4
				} else {
					v1 = uintptr(0)
				}
				if !(_geopolyParseNumber(tls, bp, v1) != 0) {
					break
				}
				ii = ii + 1
				if ii == int32(2) {
					(**(**TGeoParse)(__ccgo_up(bp))).FnVertex = (**(**TGeoParse)(__ccgo_up(bp))).FnVertex + 1
				}
				c = _geopolySkipSpace(tls, bp)
				(**(**TGeoParse)(__ccgo_up(bp))).Fz = (**(**TGeoParse)(__ccgo_up(bp))).Fz + 1
				if int32(c) == int32(',') {
					continue
				}
				if int32(c) == int32(']') && ii >= int32(2) {
					break
				}
				(**(**TGeoParse)(__ccgo_up(bp))).FnErr = (**(**TGeoParse)(__ccgo_up(bp))).FnErr + 1
				rc = int32(SQLITE_ERROR)
				goto parse_json_err
			}
			if int32(_geopolySkipSpace(tls, bp)) == int32(',') {
				(**(**TGeoParse)(__ccgo_up(bp))).Fz = (**(**TGeoParse)(__ccgo_up(bp))).Fz + 1
				continue
			}
			break
		}
		if v2 = int32(_geopolySkipSpace(tls, bp)) == int32(']') && (**(**TGeoParse)(__ccgo_up(bp))).FnVertex >= int32(4) && **(**TGeoCoord)(__ccgo_up((**(**TGeoParse)(__ccgo_up(bp))).Fa)) == **(**TGeoCoord)(__ccgo_up((**(**TGeoParse)(__ccgo_up(bp))).Fa + uintptr((**(**TGeoParse)(__ccgo_up(bp))).FnVertex*int32(2)-int32(2))*4)) && **(**TGeoCoord)(__ccgo_up((**(**TGeoParse)(__ccgo_up(bp))).Fa + 1*4)) == **(**TGeoCoord)(__ccgo_up((**(**TGeoParse)(__ccgo_up(bp))).Fa + uintptr((**(**TGeoParse)(__ccgo_up(bp))).FnVertex*int32(2)-int32(1))*4)); v2 {
			(**(**TGeoParse)(__ccgo_up(bp))).Fz = (**(**TGeoParse)(__ccgo_up(bp))).Fz + 1
		}
		if v2 && int32(_geopolySkipSpace(tls, bp)) == libc.Int32FromInt32(0) {
			**(**int32)(__ccgo_up(bp + 20)) = int32(1)
			(**(**TGeoParse)(__ccgo_up(bp))).FnVertex = (**(**TGeoParse)(__ccgo_up(bp))).FnVertex - 1 /* Remove the redundant vertex at the end */
			pOut = Xsqlite3_malloc64(tls, uint64(libc.Int64FromInt64(40)+int64(libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2))*(int64((**(**TGeoParse)(__ccgo_up(bp))).FnVertex)-libc.Int64FromInt32(4))))
			**(**int32)(__ccgo_up(bp + 20)) = int32(1)
			if pOut == uintptr(0) {
				goto parse_json_err
			}
			(*TGeoPoly)(unsafe.Pointer(pOut)).FnVertex = (**(**TGeoParse)(__ccgo_up(bp))).FnVertex
			libc.Xmemcpy(tls, pOut+8, (**(**TGeoParse)(__ccgo_up(bp))).Fa, uint32((**(**TGeoParse)(__ccgo_up(bp))).FnVertex*int32(2))*uint32(4))
			**(**uint8)(__ccgo_up(pOut + 4)) = **(**uint8)(__ccgo_up(bp + 20))
			**(**uint8)(__ccgo_up(pOut + 4 + 1)) = uint8((**(**TGeoParse)(__ccgo_up(bp))).FnVertex >> int32(16) & int32(0xff))
			**(**uint8)(__ccgo_up(pOut + 4 + 2)) = uint8((**(**TGeoParse)(__ccgo_up(bp))).FnVertex >> int32(8) & int32(0xff))
			**(**uint8)(__ccgo_up(pOut + 4 + 3)) = uint8((**(**TGeoParse)(__ccgo_up(bp))).FnVertex & int32(0xff))
			Xsqlite3_free(tls, (**(**TGeoParse)(__ccgo_up(bp))).Fa)
			if pRc != 0 {
				**(**int32)(__ccgo_up(pRc)) = SQLITE_OK
			}
			return pOut
		} else {
			(**(**TGeoParse)(__ccgo_up(bp))).FnErr = (**(**TGeoParse)(__ccgo_up(bp))).FnErr + 1
			rc = int32(SQLITE_ERROR)
		}
	}
	goto parse_json_err
parse_json_err:
	;
	if pRc != 0 {
		**(**int32)(__ccgo_up(pRc)) = rc
	}
	Xsqlite3_free(tls, (**(**TGeoParse)(__ccgo_up(bp))).Fa)
	return uintptr(0)
}

// C documentation
//
//	/*
//	** Function:   geopoly_regular(X,Y,R,N)
//	**
//	** Construct a simple, convex, regular polygon centered at X, Y
//	** with circumradius R and with N sides.
//	*/
func _geopolyRegularFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var n int32
	var p uintptr
	var r, rAngle, x, y float64
	var _ /* i at bp+0 */ int32
	_, _, _, _, _, _ = n, p, r, rAngle, x, y
	x = Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv)))
	y = Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	r = Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv + 2*4)))
	n = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 3*4)))
	_ = argc
	if n < int32(3) || r <= float64(0) {
		return
	}
	if n > int32(1000) {
		n = int32(1000)
	}
	p = Xsqlite3_malloc64(tls, uint64(uint32(40)+uint32((n-int32(1))*int32(2))*uint32(4)))
	if p == uintptr(0) {
		Xsqlite3_result_error_nomem(tls, context)
		return
	}
	**(**int32)(__ccgo_up(bp)) = int32(1)
	**(**uint8)(__ccgo_up(p + 4)) = **(**uint8)(__ccgo_up(bp))
	**(**uint8)(__ccgo_up(p + 4 + 1)) = uint8(0)
	**(**uint8)(__ccgo_up(p + 4 + 2)) = uint8(n >> int32(8) & int32(0xff))
	**(**uint8)(__ccgo_up(p + 4 + 3)) = uint8(n & int32(0xff))
	**(**int32)(__ccgo_up(bp)) = 0
	for {
		if !(**(**int32)(__ccgo_up(bp)) < n) {
			break
		}
		rAngle = float64(float64(libc.Float64FromFloat64(2)*libc.Float64FromFloat64(3.141592653589793))*float64(**(**int32)(__ccgo_up(bp)))) / float64(n)
		**(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(**(**int32)(__ccgo_up(bp))*int32(2))*4)) = float32(x - float64(r*_geopolySine(tls, rAngle-float64(libc.Float64FromFloat64(0.5)*libc.Float64FromFloat64(3.141592653589793)))))
		**(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(**(**int32)(__ccgo_up(bp))*int32(2)+int32(1))*4)) = float32(y + float64(r*_geopolySine(tls, rAngle)))
		goto _1
	_1:
		;
		**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + 1
	}
	Xsqlite3_result_blob(tls, context, p+4, int32(4)+int32(8)*n, uintptr(-libc.Int32FromInt32(1)))
	Xsqlite3_free(tls, p)
}

// C documentation
//
//	/*
//	** SQL function:     geopoly_svg(X, ....)
//	**
//	** Interpret X as a polygon and render it as a SVG <polyline>.
//	** Additional arguments are added as attributes to the <polyline>.
//	*/
func _geopolySvgFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var cSep int8
	var db, p, x, z uintptr
	var i int32
	_, _, _, _, _, _ = cSep, db, i, p, x, z
	if argc < int32(1) {
		return
	}
	p = _geopolyFuncParam(tls, context, **(**uintptr)(__ccgo_up(argv)), uintptr(0))
	if p != 0 {
		db = Xsqlite3_context_db_handle(tls, context)
		x = Xsqlite3_str_new(tls, db)
		cSep = int8('\'')
		Xsqlite3_str_appendf(tls, x, __ccgo_ts+31096, 0)
		i = 0
		for {
			if !(i < (*TGeoPoly)(unsafe.Pointer(p)).FnVertex) {
				break
			}
			Xsqlite3_str_appendf(tls, x, __ccgo_ts+31114, libc.VaList(bp+8, int32(cSep), float64(**(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(i*int32(2))*4))), float64(**(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(i*int32(2)+int32(1))*4)))))
			cSep = int8(' ')
			goto _1
		_1:
			;
			i = i + 1
		}
		Xsqlite3_str_appendf(tls, x, __ccgo_ts+31122, libc.VaList(bp+8, float64(**(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2))*4))), float64(**(**TGeoCoord)(__ccgo_up(p + 8 + uintptr(libc.Int32FromInt32(0)*libc.Int32FromInt32(2)+libc.Int32FromInt32(1))*4)))))
		i = int32(1)
		for {
			if !(i < argc) {
				break
			}
			z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
			if z != 0 && **(**int8)(__ccgo_up(z)) != 0 {
				Xsqlite3_str_appendf(tls, x, __ccgo_ts+31130, libc.VaList(bp+8, z))
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		Xsqlite3_str_appendf(tls, x, __ccgo_ts+31134, 0)
		Xsqlite3_result_text(tls, context, Xsqlite3_str_finish(tls, x), -int32(1), __ccgo_fp(Xsqlite3_free))
		Xsqlite3_free(tls, p)
	}
}

// C documentation
//
//	/*
//	** The xUpdate method for GEOPOLY module virtual tables.
//	**
//	** For DELETE:
//	**
//	**     argv[0] = the rowid to be deleted
//	**
//	** For INSERT:
//	**
//	**     argv[0] = SQL NULL
//	**     argv[1] = rowid to insert, or an SQL NULL to select automatically
//	**     argv[2] = _shape column
//	**     argv[3] = first application-defined column....
//	**
//	** For UPDATE:
//	**
//	**     argv[0] = rowid to modify.  Never NULL
//	**     argv[1] = rowid after the change.  Never NULL
//	**     argv[2] = new value for _shape
//	**     argv[3] = new value for first application-defined column....
//	*/
func _geopolyUpdate(tls *libc.TLS, pVtab uintptr, nData int32, aData uintptr, pRowid uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var coordChange, jj, nChange, newRowidValid, oldRowidValid, rc2, steprc int32
	var newRowid, oldRowid Ti64
	var p, pRtree, pUp, v3 uintptr
	var v1 int64
	var v4 bool
	var _ /* cell at bp+8 */ TRtreeCell
	var _ /* pLeaf at bp+56 */ uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = coordChange, jj, nChange, newRowid, newRowidValid, oldRowid, oldRowidValid, p, pRtree, pUp, rc2, steprc, v1, v3, v4
	pRtree = pVtab
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK /* True if newRowid is valid */
	coordChange = 0                        /* Change in coordinates */
	if (*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef != 0 {
		/* Unable to write to the btree while another cursor is reading from it,
		 ** since the write might do a rebalance which would disrupt the read
		 ** cursor. */
		return libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
	}
	_rtreeReference(tls, pRtree)
	oldRowidValid = libc.BoolInt32(Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(aData))) != int32(SQLITE_NULL))
	if oldRowidValid != 0 {
		v1 = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(aData)))
	} else {
		v1 = 0
	}
	oldRowid = v1
	newRowidValid = libc.BoolInt32(nData > int32(1) && Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(aData + 1*4))) != int32(SQLITE_NULL))
	if newRowidValid != 0 {
		v1 = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(aData + 1*4)))
	} else {
		v1 = 0
	}
	newRowid = v1
	(**(**TRtreeCell)(__ccgo_up(bp + 8))).FiRowid = newRowid
	if nData > int32(1) && (!(oldRowidValid != 0) || !(Xsqlite3_value_nochange(tls, **(**uintptr)(__ccgo_up(aData + 2*4))) != 0) || oldRowid != newRowid) {
		_geopolyBBox(tls, uintptr(0), **(**uintptr)(__ccgo_up(aData + 2*4)), bp+8+8, bp)
		if **(**int32)(__ccgo_up(bp)) != 0 {
			if **(**int32)(__ccgo_up(bp)) == int32(SQLITE_ERROR) {
				(*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+31225, 0)
			}
			goto geopoly_update_end
		}
		coordChange = int32(1)
		/* If a rowid value was supplied, check if it is already present in
		 ** the table. If so, the constraint has failed. */
		if newRowidValid != 0 && (!(oldRowidValid != 0) || oldRowid != newRowid) {
			Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid, int32(1), (**(**TRtreeCell)(__ccgo_up(bp + 8))).FiRowid)
			steprc = Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid)
			**(**int32)(__ccgo_up(bp)) = Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid)
			if int32(SQLITE_ROW) == steprc {
				if Xsqlite3_vtab_on_conflict(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb) == int32(SQLITE_REPLACE) {
					**(**int32)(__ccgo_up(bp)) = _rtreeDeleteRowid(tls, pRtree, (**(**TRtreeCell)(__ccgo_up(bp + 8))).FiRowid)
				} else {
					**(**int32)(__ccgo_up(bp)) = _rtreeConstraintError(tls, pRtree, 0)
				}
			}
		}
	}
	/* If aData[0] is not an SQL NULL value, it is the rowid of a
	 ** record to delete from the r-tree table. The following block does
	 ** just that.
	 */
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && (nData == int32(1) || coordChange != 0 && oldRowidValid != 0) {
		**(**int32)(__ccgo_up(bp)) = _rtreeDeleteRowid(tls, pRtree, oldRowid)
	}
	/* If the aData[] array contains more than one element, elements
	 ** (aData[2]..aData[argc-1]) contain a new record to insert into
	 ** the r-tree structure.
	 */
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && nData > int32(1) && coordChange != 0 {
		/* Insert the new record into the r-tree */
		**(**uintptr)(__ccgo_up(bp + 56)) = uintptr(0)
		if !(newRowidValid != 0) {
			**(**int32)(__ccgo_up(bp)) = _rtreeNewRowid(tls, pRtree, bp+8)
		}
		**(**Tsqlite_int64)(__ccgo_up(pRowid)) = (**(**TRtreeCell)(__ccgo_up(bp + 8))).FiRowid
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp)) = _ChooseLeaf(tls, pRtree, bp+8, 0, bp+56)
		}
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp)) = _rtreeInsertCell(tls, pRtree, **(**uintptr)(__ccgo_up(bp + 56)), bp+8, 0)
			rc2 = _nodeRelease(tls, pRtree, **(**uintptr)(__ccgo_up(bp + 56)))
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp)) = rc2
			}
		}
	}
	/* Change the data */
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && nData > int32(1) {
		pUp = (*TRtree)(unsafe.Pointer(pRtree)).FpWriteAux
		nChange = 0
		Xsqlite3_bind_int64(tls, pUp, int32(1), (**(**TRtreeCell)(__ccgo_up(bp + 8))).FiRowid)
		if Xsqlite3_value_nochange(tls, **(**uintptr)(__ccgo_up(aData + 2*4))) != 0 {
			Xsqlite3_bind_null(tls, pUp, int32(2))
		} else {
			p = uintptr(0)
			if v4 = Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(aData + 2*4))) == int32(SQLITE_TEXT); v4 {
				v3 = _geopolyFuncParam(tls, uintptr(0), **(**uintptr)(__ccgo_up(aData + 2*4)), bp)
				p = v3
			}
			if v4 && v3 != uintptr(0) && **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				Xsqlite3_bind_blob(tls, pUp, int32(2), p+4, int32(4)+int32(8)*(*TGeoPoly)(unsafe.Pointer(p)).FnVertex, uintptr(-libc.Int32FromInt32(1)))
			} else {
				Xsqlite3_bind_value(tls, pUp, int32(2), **(**uintptr)(__ccgo_up(aData + 2*4)))
			}
			Xsqlite3_free(tls, p)
			nChange = int32(1)
		}
		jj = int32(1)
		for {
			if !(jj < nData-int32(2)) {
				break
			}
			nChange = nChange + 1
			Xsqlite3_bind_value(tls, pUp, jj+int32(2), **(**uintptr)(__ccgo_up(aData + uintptr(jj+int32(2))*4)))
			goto _5
		_5:
			;
			jj = jj + 1
		}
		if nChange != 0 {
			Xsqlite3_step(tls, pUp)
			**(**int32)(__ccgo_up(bp)) = Xsqlite3_reset(tls, pUp)
		}
	}
	goto geopoly_update_end
geopoly_update_end:
	;
	_rtreeRelease(tls, pRtree)
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Make sure the BtCursor* given in the argument has a valid
//	** BtCursor.info structure.  If it is not already valid, call
//	** btreeParseCell() to fill it in.
//	**
//	** BtCursor.info is a cache of the information in the current cell.
//	** Using this cache reduces the number of calls to btreeParseCell().
//	*/
func _getCellInfo(tls *libc.TLS, pCur uintptr) {
	var v1 uintptr
	_ = v1
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize) == 0 {
		v1 = pCur + 1
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTCF_ValidNKey))
		_btreeParseCell(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage, int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix), pCur+32)
	} else {
	}
}

// C documentation
//
//	/*
//	** This function is called from within the xConnect() or xCreate() method to
//	** determine the node-size used by the rtree table being created or connected
//	** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
//	** Otherwise, an SQLite error code is returned.
//	**
//	** If this function is being called as part of an xConnect(), then the rtree
//	** table already exists. In this case the node-size is determined by inspecting
//	** the root node of the tree.
//	**
//	** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
//	** This ensures that each node is stored on a single database page. If the
//	** database page-size is so large that more than RTREE_MAXCELLS entries
//	** would fit in a single node, use a smaller node-size.
//	*/
func _getNodeSize(tls *libc.TLS, db uintptr, pRtree uintptr, isCreate int32, pzErr uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var rc int32
	var zSql uintptr
	var _ /* iPageSize at bp+0 */ int32
	_, _ = rc, zSql
	if isCreate != 0 {
		**(**int32)(__ccgo_up(bp)) = 0
		zSql = Xsqlite3_mprintf(tls, __ccgo_ts+29926, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzDb))
		rc = _getIntFromStmt(tls, db, zSql, bp)
		if rc == SQLITE_OK {
			(*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize = **(**int32)(__ccgo_up(bp)) - int32(64)
			if int32(4)+int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)*int32(RTREE_MAXCELLS) < (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize {
				(*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize = int32(4) + int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)*int32(RTREE_MAXCELLS)
			}
		} else {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+16, Xsqlite3_errmsg(tls, db)))
		}
	} else {
		zSql = Xsqlite3_mprintf(tls, __ccgo_ts+29946, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName))
		rc = _getIntFromStmt(tls, db, zSql, pRtree+16)
		if rc != SQLITE_OK {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+16, Xsqlite3_errmsg(tls, db)))
		} else {
			if (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize < libc.Int32FromInt32(512)-libc.Int32FromInt32(64) {
				rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
				**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+30003, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzName))
			}
		}
	}
	Xsqlite3_free(tls, zSql)
	return rc
}

// C documentation
//
//	/*
//	** Given the page number of an overflow page in the database (parameter
//	** ovfl), this function finds the page number of the next page in the
//	** linked list of overflow pages. If possible, it uses the auto-vacuum
//	** pointer-map data instead of reading the content of page ovfl to do so.
//	**
//	** If an error occurs an SQLite error code is returned. Otherwise:
//	**
//	** The page number of the next overflow page in the linked list is
//	** written to *pPgnoNext. If page ovfl is the last page in its linked
//	** list, *pPgnoNext is set to zero.
//	**
//	** If ppPage is not NULL, and a reference to the MemPage object corresponding
//	** to page number pOvfl was obtained, then *ppPage is set to point to that
//	** reference. It is the responsibility of the caller to call releasePage()
//	** on *ppPage to free the reference. In no reference was obtained (because
//	** the pointer-map was used to obtain the value for *pPgnoNext), then
//	** *ppPage is set to zero.
//	*/
func _getOverflowPage(tls *libc.TLS, pBt uintptr, ovfl TPgno, ppPage uintptr, pPgnoNext uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iGuess, next TPgno
	var rc, v1 int32
	var _ /* eType at bp+8 */ Tu8
	var _ /* pPage at bp+0 */ uintptr
	var _ /* pgno at bp+4 */ TPgno
	_, _, _, _ = iGuess, next, rc, v1
	next = uint32(0)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	rc = SQLITE_OK
	/* Try to find the next page in the overflow list using the
	 ** autovacuum pointer-map pages. Guess that the next page in
	 ** the overflow list is page number (ovfl+1). If that guess turns
	 ** out to be wrong, fall back to loading the data of page
	 ** number ovfl to determine the next page number.
	 */
	if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
		iGuess = ovfl + uint32(1)
		for _ptrmapPageno(tls, pBt, iGuess) == iGuess || iGuess == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) {
			iGuess = iGuess + 1
		}
		if iGuess <= _btreePagecount(tls, pBt) {
			rc = _ptrmapGet(tls, pBt, iGuess, bp+8, bp+4)
			if rc == SQLITE_OK && int32(**(**Tu8)(__ccgo_up(bp + 8))) == int32(PTRMAP_OVERFLOW2) && **(**TPgno)(__ccgo_up(bp + 4)) == ovfl {
				next = iGuess
				rc = int32(SQLITE_DONE)
			}
		}
	}
	if rc == SQLITE_OK {
		if ppPage == uintptr(0) {
			v1 = int32(PAGER_GET_READONLY)
		} else {
			v1 = 0
		}
		rc = _btreeGetPage(tls, pBt, ovfl, bp, v1)
		if rc == SQLITE_OK {
			next = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData)
		}
	}
	**(**TPgno)(__ccgo_up(pPgnoNext)) = next
	if ppPage != 0 {
		**(**uintptr)(__ccgo_up(ppPage)) = **(**uintptr)(__ccgo_up(bp))
	} else {
		_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	if rc == int32(SQLITE_DONE) {
		v1 = SQLITE_OK
	} else {
		v1 = rc
	}
	return v1
}

// C documentation
//
//	/* The page getter for when memory-mapped I/O is enabled */
func _getPageMMap(tls *libc.TLS, pPager uintptr, pgno TPgno, ppPage uintptr, flags int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bMmapOk, rc int32
	var _ /* iFrame at bp+4 */ Tu32
	var _ /* pData at bp+8 */ uintptr
	var _ /* pPg at bp+0 */ uintptr
	_, _ = bMmapOk, rc
	rc = SQLITE_OK
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**Tu32)(__ccgo_up(bp + 4)) = uint32(0) /* Frame to read from WAL file */
	/* It is acceptable to use a read-only (mmap) page for any page except
	 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
	 ** flag was specified by the caller. And so long as the db is not a
	 ** temporary or in-memory database.  */
	bMmapOk = libc.BoolInt32(pgno > uint32(1) && (int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_READER) || flags&int32(PAGER_GET_READONLY) != 0))
	/* Optimization note:  Adding the "pgno<=1" term before "pgno==0" here
	 ** allows the compiler optimizer to reuse the results of the "pgno>1"
	 ** test in the previous statement, and avoid testing pgno==0 in the
	 ** common case where pgno is large. */
	if pgno <= uint32(1) && pgno == uint32(0) {
		return _sqlite3CorruptError(tls, int32(65348))
	}
	if bMmapOk != 0 && (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) {
		rc = _sqlite3WalFindFrame(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, pgno, bp+4)
		if rc != SQLITE_OK {
			**(**uintptr)(__ccgo_up(ppPage)) = uintptr(0)
			return rc
		}
	}
	if bMmapOk != 0 && **(**Tu32)(__ccgo_up(bp + 4)) == uint32(0) {
		**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
		rc = _sqlite3OsFetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int64(pgno-libc.Uint32FromInt32(1))*(*TPager)(unsafe.Pointer(pPager)).FpageSize, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), bp+8)
		if rc == SQLITE_OK && **(**uintptr)(__ccgo_up(bp + 8)) != 0 {
			if int32((*TPager)(unsafe.Pointer(pPager)).FeState) > int32(PAGER_READER) || (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 {
				**(**uintptr)(__ccgo_up(bp)) = _sqlite3PagerLookup(tls, pPager, pgno)
			}
			if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
				rc = _pagerAcquireMapPage(tls, pPager, pgno, **(**uintptr)(__ccgo_up(bp + 8)), bp)
			} else {
				_sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int64(pgno-libc.Uint32FromInt32(1))*(*TPager)(unsafe.Pointer(pPager)).FpageSize, **(**uintptr)(__ccgo_up(bp + 8)))
			}
			if **(**uintptr)(__ccgo_up(bp)) != 0 {
				**(**uintptr)(__ccgo_up(ppPage)) = **(**uintptr)(__ccgo_up(bp))
				return SQLITE_OK
			}
		}
		if rc != SQLITE_OK {
			**(**uintptr)(__ccgo_up(ppPage)) = uintptr(0)
			return rc
		}
	}
	return _getPageNormal(tls, pPager, pgno, ppPage, flags)
}

// C documentation
//
//	/*
//	** The page getter methods each try to acquire a reference to a
//	** page with page number pgno. If the requested reference is
//	** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
//	**
//	** There are different implementations of the getter method depending
//	** on the current state of the pager.
//	**
//	**     getPageNormal()         --  The normal getter
//	**     getPageError()          --  Used if the pager is in an error state
//	**     getPageMmap()           --  Used if memory-mapped I/O is enabled
//	**
//	** If the requested page is already in the cache, it is returned.
//	** Otherwise, a new page object is allocated and populated with data
//	** read from the database file. In some cases, the pcache module may
//	** choose not to allocate a new page object and may reuse an existing
//	** object with no outstanding references.
//	**
//	** The extra data appended to a page is always initialized to zeros the
//	** first time a page is loaded into memory. If the page requested is
//	** already in the cache when this function is called, then the extra
//	** data is left as it was when the page object was last used.
//	**
//	** If the database image is smaller than the requested page or if
//	** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
//	** requested page is not already stored in the cache, then no
//	** actual disk read occurs. In this case the memory image of the
//	** page is initialized to all zeros.
//	**
//	** If PAGER_GET_NOCONTENT is true, it means that we do not care about
//	** the contents of the page. This occurs in two scenarios:
//	**
//	**   a) When reading a free-list leaf page from the database, and
//	**
//	**   b) When a savepoint is being rolled back and we need to load
//	**      a new page into the cache to be filled with the data read
//	**      from the savepoint journal.
//	**
//	** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
//	** of being read from the database. Additionally, the bits corresponding
//	** to pgno in Pager.pInJournal (bitvec of pages already written to the
//	** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
//	** savepoints are set. This means if the page is made writable at any
//	** point in the future, using a call to sqlite3PagerWrite(), its contents
//	** will not be journaled. This saves IO.
//	**
//	** The acquisition might fail for several reasons.  In all cases,
//	** an appropriate error code is returned and *ppPage is set to NULL.
//	**
//	** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
//	** to find a page in the in-memory cache first.  If the page is not already
//	** in memory, this routine goes to disk to read it in whereas Lookup()
//	** just returns 0.  This routine acquires a read-lock the first time it
//	** has to go to disk, and could also playback an old journal if necessary.
//	** Since Lookup() never goes to disk, it never has to deal with locks
//	** or journal files.
//	*/
func _getPageNormal(tls *libc.TLS, pPager uintptr, pgno TPgno, ppPage uintptr, flags int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var noContent Tu8
	var pPg, v1 uintptr
	var rc int32
	var _ /* pBase at bp+0 */ uintptr
	_, _, _, _ = noContent, pPg, rc, v1
	rc = SQLITE_OK
	if pgno == uint32(0) {
		return _sqlite3CorruptError(tls, int32(65233))
	}
	**(**uintptr)(__ccgo_up(bp)) = _sqlite3PcacheFetch(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, pgno, int32(3))
	if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
		pPg = uintptr(0)
		rc = _sqlite3PcacheFetchStress(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, pgno, bp)
		if rc != SQLITE_OK {
			goto pager_acquire_err
		}
		if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
			goto pager_acquire_err
		}
	}
	v1 = _sqlite3PcacheFetchFinish(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, pgno, **(**uintptr)(__ccgo_up(bp)))
	**(**uintptr)(__ccgo_up(ppPage)) = v1
	pPg = v1
	noContent = libc.BoolUint8(flags&int32(PAGER_GET_NOCONTENT) != 0)
	if (*TPgHdr)(unsafe.Pointer(pPg)).FpPager != 0 && !(noContent != 0) {
		/* In this case the pcache already contains an initialized copy of
		 ** the page. Return without further ado.  */
		**(**Tu32)(__ccgo_up(pPager + 200)) = **(**Tu32)(__ccgo_up(pPager + 200)) + 1
		return SQLITE_OK
	} else {
		/* The pager cache has created a new page. Its content needs to
		 ** be initialized. But first some error checks:
		 **
		 ** (*) obsolete.  Was: maximum page number is 2^31
		 ** (2) Never try to fetch the locking page
		 */
		if pgno == (*TPager)(unsafe.Pointer(pPager)).FlckPgno {
			rc = _sqlite3CorruptError(tls, int32(65265))
			goto pager_acquire_err
		}
		(*TPgHdr)(unsafe.Pointer(pPg)).FpPager = pPager
		if !((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != libc.UintptrFromInt32(0)) || (*TPager)(unsafe.Pointer(pPager)).FdbSize < pgno || noContent != 0 {
			if pgno > (*TPager)(unsafe.Pointer(pPager)).FmxPgno {
				rc = int32(SQLITE_FULL)
				if pgno <= (*TPager)(unsafe.Pointer(pPager)).FdbSize {
					_sqlite3PcacheRelease(tls, pPg)
					pPg = uintptr(0)
				}
				goto pager_acquire_err
			}
			if noContent != 0 {
				/* Failure to set the bits in the InJournal bit-vectors is benign.
				 ** It merely means that we might do some extra work to journal a
				 ** page that does not need to be journaled.  Nevertheless, be sure
				 ** to test the case where a malloc error occurs while trying to set
				 ** a bit in a bit vector.
				 */
				_sqlite3BeginBenignMalloc(tls)
				if pgno <= (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize {
					_sqlite3BitvecSet(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, pgno)
				}
				_addToSavepointBitvecs(tls, pPager, pgno)
				_sqlite3EndBenignMalloc(tls)
			}
			libc.Xmemset(tls, (*TPgHdr)(unsafe.Pointer(pPg)).FpData, 0, uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize))
		} else {
			**(**Tu32)(__ccgo_up(pPager + 200 + 1*4)) = **(**Tu32)(__ccgo_up(pPager + 200 + 1*4)) + 1
			rc = _readDbPage(tls, pPg)
			if rc != SQLITE_OK {
				goto pager_acquire_err
			}
		}
	}
	return SQLITE_OK
	goto pager_acquire_err
pager_acquire_err:
	;
	if pPg != 0 {
		_sqlite3PcacheDrop(tls, pPg)
	}
	_pagerUnlockIfUnused(tls, pPager)
	**(**uintptr)(__ccgo_up(ppPage)) = uintptr(0)
	return rc
}

// C documentation
//
//	/*
//	** Interpret the given string as a safety level.  Return 0 for OFF,
//	** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA.  Return 1 for an empty or
//	** unrecognized string argument.  The FULL and EXTRA option is disallowed
//	** if the omitFull parameter it 1.
//	**
//	** Note that the values returned are one less that the values that
//	** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
//	** to support legacy SQL code.  The safety level used to be boolean
//	** and older scripts may have used numbers 0 for OFF and 1 for ON.
//	*/
func _getSafetyLevel(tls *libc.TLS, z uintptr, omitFull int32, dflt Tu8) (r Tu8) {
	var i, n int32
	_, _ = i, n
	if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z)))])&int32(0x04) != 0 {
		return uint8(_sqlite3Atoi(tls, z))
	}
	n = _sqlite3Strlen30(tls, z)
	i = 0
	for {
		if !(i < int32(libc.Uint32FromInt64(8)/libc.Uint32FromInt64(1))) {
			break
		}
		if int32(_iLength[i]) == n && Xsqlite3_strnicmp(tls, uintptr(unsafe.Pointer(&_zText))+uintptr(_iOffset[i]), z, n) == 0 && (!(omitFull != 0) || int32(_iValue[i]) <= int32(1)) {
			return _iValue[i]
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return dflt
}

func _groupConcatInverse(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var nVS int32
	var pGCC uintptr
	_, _ = nVS, pGCC
	_ = argc /* Suppress unused parameter warning */
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv))) == int32(SQLITE_NULL) {
		return
	}
	pGCC = Xsqlite3_aggregate_context(tls, context, int32(36))
	/* pGCC is always non-NULL since groupConcatStep() will have always
	 ** run first to initialize it */
	if pGCC != 0 { /* Number of characters to remove */
		/* Must call sqlite3_value_text() to convert the argument into text prior
		 ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */
		Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		nVS = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
		**(**int32)(__ccgo_up(pGCC + 24)) -= int32(1)
		if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths != uintptr(0) {
			if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum > 0 {
				nVS = nVS + **(**int32)(__ccgo_up((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths))
				libc.Xmemmove(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths+uintptr(1)*4, uint32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum-libc.Int32FromInt32(1))*uint32(4))
			}
		} else {
			/* If removing single accumulated string, harmlessly over-do. */
			nVS = nVS + (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength
		}
		if nVS >= int32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar) {
			(*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar = uint32(0)
		} else {
			(*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar -= uint32(nVS)
			libc.Xmemmove(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FzText, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FzText+uintptr(nVS), (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar)
		}
		if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar == uint32(0) {
			(*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FmxAlloc = uint32(0)
			Xsqlite3_free(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths)
			(*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths = uintptr(0)
		}
	}
}

func _groupConcatStep(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var db, pGCC, pnsl, zSep, zVal uintptr
	var firstTerm, i, nA, nSep, nVal, v1 int32
	_, _, _, _, _, _, _, _, _, _, _ = db, firstTerm, i, nA, nSep, nVal, pGCC, pnsl, zSep, zVal, v1
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv))) == int32(SQLITE_NULL) {
		return
	}
	pGCC = Xsqlite3_aggregate_context(tls, context, int32(36))
	if pGCC != 0 {
		db = Xsqlite3_context_db_handle(tls, context)
		firstTerm = libc.BoolInt32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FmxAlloc == uint32(0))
		(*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FmxAlloc = uint32(**(**int32)(__ccgo_up(db + 120)))
		if argc == int32(1) {
			if !(firstTerm != 0) {
				Xsqlite3_str_appendchar(tls, pGCC, int32(1), int8(','))
			} else {
				(*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength = int32(1)
			}
		} else {
			if !(firstTerm != 0) {
				zSep = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
				nSep = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
				if zSep != 0 {
					Xsqlite3_str_append(tls, pGCC, zSep, nSep)
				} else {
					nSep = 0
				}
				if nSep != (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength || (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths != uintptr(0) {
					pnsl = (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths
					if pnsl == uintptr(0) {
						/* First separator length variation seen, start tracking them. */
						pnsl = Xsqlite3_malloc64(tls, uint64(uint32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum+libc.Int32FromInt32(1))*uint32(4)))
						if pnsl != uintptr(0) {
							i = 0
							nA = (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum - int32(1)
							for i < nA {
								v1 = i
								i = i + 1
								**(**int32)(__ccgo_up(pnsl + uintptr(v1)*4)) = (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength
							}
						}
					} else {
						pnsl = Xsqlite3_realloc64(tls, pnsl, uint64(uint32((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum)*uint32(4)))
					}
					if pnsl != uintptr(0) {
						if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum > 0 {
							**(**int32)(__ccgo_up(pnsl + uintptr((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum-int32(1))*4)) = nSep
						}
						(*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths = pnsl
					} else {
						_sqlite3StrAccumSetError(tls, pGCC, uint8(SQLITE_NOMEM))
					}
				}
			} else {
				(*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
			}
		}
		**(**int32)(__ccgo_up(pGCC + 24)) += int32(1)
		zVal = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		nVal = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
		if zVal != 0 {
			Xsqlite3_str_append(tls, pGCC, zVal, nVal)
		}
	}
}

// C documentation
//
//	/*
//	** Resize the Vdbe.aOp array so that it is at least nOp elements larger
//	** than its current size. nOp is guaranteed to be less than or equal
//	** to 1024/sizeof(Op).
//	**
//	** If an out-of-memory error occurs while resizing the array, return
//	** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
//	** unchanged (this is so that any opcodes already allocated can be
//	** correctly deallocated along with the rest of the Vdbe).
//	*/
func _growOpArray(tls *libc.TLS, v uintptr, nOp int32) (r int32) {
	var nNew Tsqlite3_int64
	var p, pNew uintptr
	var v1 int64
	var v2 int32
	_, _, _, _, _ = nNew, p, pNew, v1, v2
	p = (*TVdbe)(unsafe.Pointer(v)).FpParse
	if (*TVdbe)(unsafe.Pointer(v)).FnOpAlloc != 0 {
		v1 = int64(2) * int64((*TVdbe)(unsafe.Pointer(v)).FnOpAlloc)
	} else {
		v1 = int64(libc.Uint32FromInt32(1024) / libc.Uint32FromInt64(20))
	}
	/* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
	 ** more frequent reallocs and hence provide more opportunities for
	 ** simulated OOM faults.  SQLITE_TEST_REALLOC_STRESS is generally used
	 ** during testing only.  With SQLITE_TEST_REALLOC_STRESS grow the op array
	 ** by the minimum* amount required until the size reaches 512.  Normal
	 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
	 ** size of the op array or add 1KB of space, whichever is smaller. */
	nNew = v1
	_ = nOp
	/* Ensure that the size of a VDBE does not grow too large */
	if nNew > int64(**(**int32)(__ccgo_up((*TParse)(unsafe.Pointer(p)).Fdb + 120 + 5*4))) {
		_sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(p)).Fdb)
		return int32(SQLITE_NOMEM)
	}
	pNew = _sqlite3DbRealloc(tls, (*TParse)(unsafe.Pointer(p)).Fdb, (*TVdbe)(unsafe.Pointer(v)).FaOp, uint64(nNew*int64(20)))
	if pNew != 0 {
		(*TParse)(unsafe.Pointer(p)).FszOpAlloc = _sqlite3DbMallocSize(tls, (*TParse)(unsafe.Pointer(p)).Fdb, pNew)
		(*TVdbe)(unsafe.Pointer(v)).FnOpAlloc = int32(uint32((*TParse)(unsafe.Pointer(p)).FszOpAlloc) / uint32(20))
		(*TVdbe)(unsafe.Pointer(v)).FaOp = pNew
	}
	if pNew != 0 {
		v2 = SQLITE_OK
	} else {
		v2 = int32(SQLITE_NOMEM)
	}
	return v2
}

// C documentation
//
//	/*
//	** Grow the db->aVTrans[] array so that there is room for at least one
//	** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
//	*/
func _growVTrans(tls *libc.TLS, db uintptr) (r int32) {
	var ARRAY_INCR int32
	var aVTrans uintptr
	var nBytes Tsqlite3_int64
	_, _, _ = ARRAY_INCR, aVTrans, nBytes
	ARRAY_INCR = int32(5)
	/* Grow the sqlite3.aVTrans array if required */
	if (*Tsqlite3)(unsafe.Pointer(db)).FnVTrans%ARRAY_INCR == 0 {
		nBytes = int64(4) * (int64((*Tsqlite3)(unsafe.Pointer(db)).FnVTrans) + int64(ARRAY_INCR))
		aVTrans = _sqlite3DbRealloc(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans, uint64(nBytes))
		if !(aVTrans != 0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, aVTrans+uintptr((*Tsqlite3)(unsafe.Pointer(db)).FnVTrans)*4, 0, uint32(4)*uint32(ARRAY_INCR))
		(*Tsqlite3)(unsafe.Pointer(db)).FaVTrans = aVTrans
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** sqlite3WalkExpr() callback used by havingToWhere().
//	**
//	** If the node passed to the callback is a TK_AND node, return
//	** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
//	**
//	** Otherwise, return WRC_Prune. In this case, also check if the
//	** sub-expression matches the criteria for being moved to the WHERE
//	** clause. If so, add it to the WHERE clause and replace the sub-expression
//	** within the HAVING expression with a constant "1".
//	*/
func _havingToWhereExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var db, pNew, pS, pWhere uintptr
	var t TExpr
	_, _, _, _, _ = db, pNew, pS, pWhere, t
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_AND) {
		pS = *(*uintptr)(unsafe.Pointer(pWalker + 24))
		/* This routine is called before the HAVING clause of the current
		 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
		 ** here, it indicates that the expression is a correlated reference to a
		 ** column from an outer aggregate query, or an aggregate function that
		 ** belongs to an outer query. Do not move the expression to the WHERE
		 ** clause in this obscure case, as doing so may corrupt the outer Select
		 ** statements AggInfo structure.  */
		if _sqlite3ExprIsConstantOrGroupBy(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, pExpr, (*TSelect)(unsafe.Pointer(pS)).FpGroupBy) != 0 && libc.BoolInt32((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_IsFalse)) == uint32(EP_IsFalse)) == 0 && (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo == uintptr(0) {
			db = (*TParse)(unsafe.Pointer((*TWalker)(unsafe.Pointer(pWalker)).FpParse)).Fdb
			pNew = _sqlite3ExprInt32(tls, db, int32(1))
			if pNew != 0 {
				pWhere = (*TSelect)(unsafe.Pointer(pS)).FpWhere
				t = **(**TExpr)(__ccgo_up(pNew))
				**(**TExpr)(__ccgo_up(pNew)) = **(**TExpr)(__ccgo_up(pExpr))
				**(**TExpr)(__ccgo_up(pExpr)) = t
				pNew = _sqlite3ExprAnd(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, pWhere, pNew)
				(*TSelect)(unsafe.Pointer(pS)).FpWhere = pNew
				(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1)
			}
		}
		return int32(WRC_Prune)
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** The hex() function.  Interpret the argument as a blob.  Return
//	** a hexadecimal rendering as text.
//	*/
func _hexFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var c uint8
	var i, n int32
	var pBlob, z, zHex, v1 uintptr
	_, _, _, _, _, _, _ = c, i, n, pBlob, z, zHex, v1
	_ = argc
	pBlob = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv)))
	n = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
	/* No encoding change */
	v1 = _contextMalloc(tls, context, int64(n)*int64(2)+int64(1))
	zHex = v1
	z = v1
	if zHex != 0 {
		i = 0
		for {
			if !(i < n) {
				break
			}
			c = **(**uint8)(__ccgo_up(pBlob))
			v1 = z
			z = z + 1
			**(**int8)(__ccgo_up(v1)) = _hexdigits[int32(c)>>int32(4)&int32(0xf)]
			v1 = z
			z = z + 1
			**(**int8)(__ccgo_up(v1)) = _hexdigits[int32(c)&int32(0xf)]
			goto _2
		_2:
			;
			i = i + 1
			pBlob = pBlob + 1
		}
		**(**int8)(__ccgo_up(z)) = 0
		Xsqlite3_result_text64(tls, context, zHex, uint64(int32(z)-int32(zHex)), __ccgo_fp(Xsqlite3_free), uint8(SQLITE_UTF8_ZT))
	}
}

// C documentation
//
//	/*
//	** This is the Expr node callback for sqlite3ExprImpliesNonNullRow().
//	** If the expression node requires that the table at pWalker->iCur
//	** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
//	**
//	** pWalker->mWFlags is non-zero if this inquiry is being undertaking on
//	** behalf of a RIGHT JOIN (or FULL JOIN).  That makes a difference when
//	** evaluating terms in the ON clause of an inner join.
//	**
//	** This routine controls an optimization.  False positives (setting
//	** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
//	** (never setting pWalker->eCode) is a harmless missed optimization.
//	*/
func _impliesNotNullRow(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var pLeft, pRight uintptr
	_, _ = pLeft, pRight
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) {
		return int32(WRC_Prune)
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)) != uint32(0) && (*TWalker)(unsafe.Pointer(pWalker)).FmWFlags != 0 {
		/* If iCur is used in an inner-join ON clause to the left of a
		 ** RIGHT JOIN, that does *not* mean that the table must be non-null.
		 ** But it is difficult to check for that condition precisely.
		 ** To keep things simple, any use of iCur from any inner-join is
		 ** ignored while attempting to simplify a RIGHT JOIN. */
		return int32(WRC_Prune)
	}
	switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) {
	case int32(TK_ISNOT):
		fallthrough
	case int32(TK_ISNULL):
		fallthrough
	case int32(TK_NOTNULL):
		fallthrough
	case int32(TK_IS):
		fallthrough
	case int32(TK_VECTOR):
		fallthrough
	case int32(TK_FUNCTION):
		fallthrough
	case int32(TK_TRUTH):
		fallthrough
	case int32(TK_CASE):
		return int32(WRC_Prune)
	case int32(TK_COLUMN):
		if *(*int32)(unsafe.Pointer(pWalker + 24)) == (*TExpr)(unsafe.Pointer(pExpr)).FiTable {
			(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1)
			return int32(WRC_Abort)
		}
		return int32(WRC_Prune)
	case int32(TK_OR):
		fallthrough
	case int32(TK_AND):
		/* Both sides of an AND or OR must separately imply non-null-row.
		 ** Consider these cases:
		 **    1.  NOT (x AND y)
		 **    2.  x OR y
		 ** If only one of x or y is non-null-row, then the overall expression
		 ** can be true if the other arm is false (case 1) or true (case 2).
		 */
		_bothImplyNotNullRow(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, (*TExpr)(unsafe.Pointer(pExpr)).FpRight)
		return int32(WRC_Prune)
	case int32(TK_IN):
		/* Beware of "x NOT IN ()" and "x NOT IN (SELECT 1 WHERE false)",
		 ** both of which can be true.  But apart from these cases, if
		 ** the left-hand side of the IN is NULL then the IN itself will be
		 ** NULL. */
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) == uint32(0) && (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr > 0 {
			_sqlite3WalkExpr(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		}
		return int32(WRC_Prune)
	case int32(TK_BETWEEN):
		/* In "x NOT BETWEEN y AND z" either x must be non-null-row or else
		 ** both y and z must be non-null row */
		_sqlite3WalkExpr(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		_bothImplyNotNullRow(tls, pWalker, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 1*20))).FpExpr)
		return int32(WRC_Prune)
		/* Virtual tables are allowed to use constraints like x=NULL.  So
		 ** a term of the form x=y does not prove that y is not null if x
		 ** is the column of a virtual table */
		fallthrough
	case int32(TK_EQ):
		fallthrough
	case int32(TK_NE):
		fallthrough
	case int32(TK_LT):
		fallthrough
	case int32(TK_LE):
		fallthrough
	case int32(TK_GT):
		fallthrough
	case int32(TK_GE):
		pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
		pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight
		/* The y.pTab=0 assignment in wherecode.c always happens after the
		 ** impliesNotNullRow() test */
		if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab != uintptr(0) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) || int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pRight)).Fy.FpTab != uintptr(0) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pRight)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) {
			return int32(WRC_Prune)
		}
		fallthrough
	default:
		return WRC_Continue
	}
	return r
}

// C documentation
//
//	/*
//	** Walk the expression tree pExpr and increase the aggregate function
//	** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
//	** This needs to occur when copying a TK_AGG_FUNCTION node from an
//	** outer query into an inner subquery.
//	**
//	** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
//	** is a helper function - a callback for the tree walker.
//	**
//	** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
//	*/
func _incrAggDepth(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var v1 uintptr
	_ = v1
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_FUNCTION) {
		v1 = pExpr + 2
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) + *(*int32)(unsafe.Pointer(pWalker + 24)))
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Perform a single step of an incremental-vacuum. If successful, return
//	** SQLITE_OK. If there is no work to do (and therefore no point in
//	** calling this function again), return SQLITE_DONE. Or, if an error
//	** occurs, return some other error code.
//	**
//	** More specifically, this function attempts to re-organize the database so
//	** that the last page of the file currently in use is no longer in use.
//	**
//	** Parameter nFin is the number of pages that this database would contain
//	** were this function called until it returns SQLITE_DONE.
//	**
//	** If the bCommit parameter is non-zero, this function assumes that the
//	** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
//	** or an error. bCommit is passed true for an auto-vacuum-on-commit
//	** operation, or false for an incremental vacuum.
//	*/
func _incrVacuumStep(tls *libc.TLS, pBt uintptr, nFin TPgno, iLastPg TPgno, bCommit int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var dbSize, iNear, nFreeList TPgno
	var eMode Tu8
	var rc int32
	var _ /* eType at bp+0 */ Tu8
	var _ /* iFreePg at bp+16 */ TPgno
	var _ /* iFreePg at bp+8 */ TPgno
	var _ /* iPtrPage at bp+4 */ TPgno
	var _ /* pFreePg at bp+12 */ uintptr
	var _ /* pFreePg at bp+24 */ uintptr
	var _ /* pLastPg at bp+20 */ uintptr
	_, _, _, _, _ = dbSize, eMode, iNear, nFreeList, rc
	if !(_ptrmapPageno(tls, pBt, iLastPg) == iLastPg) && iLastPg != uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) {
		nFreeList = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+36)
		if nFreeList == uint32(0) {
			return int32(SQLITE_DONE)
		}
		rc = _ptrmapGet(tls, pBt, iLastPg, bp, bp+4)
		if rc != SQLITE_OK {
			return rc
		}
		if int32(**(**Tu8)(__ccgo_up(bp))) == int32(PTRMAP_ROOTPAGE) {
			return _sqlite3CorruptError(tls, int32(77285))
		}
		if int32(**(**Tu8)(__ccgo_up(bp))) == int32(PTRMAP_FREEPAGE) {
			if bCommit == 0 {
				rc = _allocateBtreePage(tls, pBt, bp+12, bp+8, iLastPg, uint8(BTALLOC_EXACT))
				if rc != SQLITE_OK {
					return rc
				}
				_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 12)))
			}
		} else {
			eMode = uint8(BTALLOC_ANY) /* Mode parameter for allocateBtreePage() */
			iNear = uint32(0)          /* nearby parameter for allocateBtreePage() */
			rc = _btreeGetPage(tls, pBt, iLastPg, bp+20, 0)
			if rc != SQLITE_OK {
				return rc
			}
			/* If bCommit is zero, this loop runs exactly once and page pLastPg
			 ** is swapped with the first free page pulled off the free list.
			 **
			 ** On the other hand, if bCommit is greater than zero, then keep
			 ** looping until a free-page located within the first nFin pages
			 ** of the file is found.
			 */
			if bCommit == 0 {
				eMode = uint8(BTALLOC_LE)
				iNear = nFin
			}
			for cond := true; cond; cond = bCommit != 0 && **(**TPgno)(__ccgo_up(bp + 16)) > nFin {
				dbSize = _btreePagecount(tls, pBt)
				rc = _allocateBtreePage(tls, pBt, bp+24, bp+16, iNear, eMode)
				if rc != SQLITE_OK {
					_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 20)))
					return rc
				}
				_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 24)))
				if **(**TPgno)(__ccgo_up(bp + 16)) > dbSize {
					_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 20)))
					return _sqlite3CorruptError(tls, int32(77337))
				}
			}
			rc = _relocatePage(tls, pBt, **(**uintptr)(__ccgo_up(bp + 20)), **(**Tu8)(__ccgo_up(bp)), **(**TPgno)(__ccgo_up(bp + 4)), **(**TPgno)(__ccgo_up(bp + 16)), bCommit)
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp + 20)))
			if rc != SQLITE_OK {
				return rc
			}
		}
	}
	if bCommit == 0 {
		for cond := true; cond; cond = iLastPg == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) || _ptrmapPageno(tls, pBt, iLastPg) == iLastPg {
			iLastPg = iLastPg - 1
		}
		(*TBtShared)(unsafe.Pointer(pBt)).FbDoTruncate = uint8(1)
		(*TBtShared)(unsafe.Pointer(pBt)).FnPage = iLastPg
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Term pTerm is guaranteed to be a WO_IN term. It may be a component term
//	** of a vector IN expression of the form "(x, y, ...) IN (SELECT ...)".
//	** This function checks to see if the term is compatible with an index
//	** column with affinity idxaff (one of the SQLITE_AFF_XYZ values). If so,
//	** it returns a pointer to the name of the collation sequence (e.g. "BINARY"
//	** or "NOCASE") used by the comparison in pTerm. If it is not compatible
//	** with affinity idxaff, NULL is returned.
//	*/
func _indexInAffinityOk(tls *libc.TLS, pParse uintptr, pTerm uintptr, idxaff Tu8) (r uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var iField int32
	var pRet, pX, v1 uintptr
	var _ /* inexpr at bp+0 */ TExpr
	_, _, _, _ = iField, pRet, pX, v1
	pX = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
	if _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft) != 0 {
		iField = (*(*struct {
			FleftColumn int32
			FiField     int32
		})(unsafe.Pointer(pTerm + 24))).FiField - int32(1)
		(**(**TExpr)(__ccgo_up(bp))).Fflags = uint32(0)
		(**(**TExpr)(__ccgo_up(bp))).Fop = uint8(TK_EQ)
		(**(**TExpr)(__ccgo_up(bp))).FpLeft = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pX)).FpLeft + 20)) + 8 + uintptr(iField)*20))).FpExpr
		(**(**TExpr)(__ccgo_up(bp))).FpRight = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pX + 20)))).FpEList + 8 + uintptr(iField)*20))).FpExpr
		pX = bp
	}
	if _sqlite3IndexAffinityOk(tls, pX, int8(idxaff)) != 0 {
		pRet = _sqlite3ExprCompareCollSeq(tls, pParse, pX)
		if pRet != 0 {
			v1 = (*TCollSeq)(unsafe.Pointer(pRet)).FzName
		} else {
			v1 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
		}
		return v1
	}
	return uintptr(0)
}

// C documentation
//
//	/*
//	** Return True if it is possible that pIndex might be useful in
//	** implementing the ORDER BY clause in pBuilder.
//	**
//	** Return False if pBuilder does not contain an ORDER BY clause or
//	** if there is no way for pIndex to be useful in implementing that
//	** ORDER BY clause.
//	*/
func _indexMightHelpWithOrderBy(tls *libc.TLS, pBuilder uintptr, pIndex uintptr, iCursor int32) (r int32) {
	var aColExpr, pExpr, pOB, v1 uintptr
	var ii, jj int32
	_, _, _, _, _, _ = aColExpr, ii, jj, pExpr, pOB, v1
	if int32(uint32(*(*uint16)(unsafe.Pointer(pIndex + 56))&0x4>>2)) != 0 {
		return 0
	}
	v1 = (*TWhereInfo)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo)).FpOrderBy
	pOB = v1
	if v1 == uintptr(0) {
		return 0
	}
	ii = 0
	for {
		if !(ii < (*TExprList)(unsafe.Pointer(pOB)).FnExpr) {
			break
		}
		pExpr = _sqlite3ExprSkipCollateAndLikely(tls, (*(*TExprList_item)(unsafe.Pointer(pOB + 8 + uintptr(ii)*20))).FpExpr)
		if pExpr == uintptr(0) {
			goto _2
		}
		if (int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_COLUMN)) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == iCursor {
			if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) < 0 {
				return int32(1)
			}
			jj = 0
			for {
				if !(jj < int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol)) {
					break
				}
				if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) == int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(jj)*2))) {
					return int32(1)
				}
				goto _3
			_3:
				;
				jj = jj + 1
			}
		} else {
			v1 = (*TIndex)(unsafe.Pointer(pIndex)).FaColExpr
			aColExpr = v1
			if v1 != uintptr(0) {
				jj = 0
				for {
					if !(jj < int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol)) {
						break
					}
					if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(jj)*2))) != -int32(2) {
						goto _5
					}
					if _sqlite3ExprCompareSkip(tls, pExpr, (*(*TExprList_item)(unsafe.Pointer(aColExpr + 8 + uintptr(jj)*20))).FpExpr, iCursor) == 0 {
						return int32(1)
					}
					goto _5
				_5:
					;
					jj = jj + 1
				}
			}
		}
		goto _2
	_2:
		;
		ii = ii + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** Populate the pIdx->aAvgEq[] array based on the samples currently
//	** stored in pIdx->aSample[].
//	*/
func _initAvgEq(tls *libc.TLS, pIdx uintptr) {
	var aSample, pFinal uintptr
	var avgEq, nRow, sumEq TtRowcnt
	var i, iCol, nCol, nSample int32
	var nDist100, nSum100 Ti64
	_, _, _, _, _, _, _, _, _, _, _ = aSample, avgEq, i, iCol, nCol, nDist100, nRow, nSample, nSum100, pFinal, sumEq
	if pIdx != 0 {
		aSample = (*TIndex)(unsafe.Pointer(pIdx)).FaSample
		pFinal = aSample + uintptr((*TIndex)(unsafe.Pointer(pIdx)).FnSample-int32(1))*20
		nCol = int32(1)
		if (*TIndex)(unsafe.Pointer(pIdx)).FnSampleCol > int32(1) {
			/* If this is stat4 data, then calculate aAvgEq[] values for all
			 ** sample columns except the last. The last is always set to 1, as
			 ** once the trailing PK fields are considered all index keys are
			 ** unique.  */
			nCol = (*TIndex)(unsafe.Pointer(pIdx)).FnSampleCol - int32(1)
			**(**TtRowcnt)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaAvgEq + uintptr(nCol)*8)) = uint64(1)
		}
		iCol = 0
		for {
			if !(iCol < nCol) {
				break
			}
			nSample = (*TIndex)(unsafe.Pointer(pIdx)).FnSample /* Used to iterate through samples */
			sumEq = uint64(0)                                  /* Sum of the nEq values */
			avgEq = uint64(0)                                  /* Number of rows in index */
			nSum100 = 0                                        /* Number of distinct values in index */
			if !((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst != 0) || iCol >= int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) || **(**TtRowcnt)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst + uintptr(iCol+int32(1))*8)) == uint64(0) {
				nRow = **(**TtRowcnt)(__ccgo_up((*TIndexSample)(unsafe.Pointer(pFinal)).FanLt + uintptr(iCol)*8))
				nDist100 = int64(uint64(libc.Int64FromInt32(100)) * **(**TtRowcnt)(__ccgo_up((*TIndexSample)(unsafe.Pointer(pFinal)).FanDLt + uintptr(iCol)*8)))
				nSample = nSample - 1
			} else {
				nRow = **(**TtRowcnt)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst))
				nDist100 = int64(uint64(libc.Int64FromInt32(100)) * **(**TtRowcnt)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst)) / **(**TtRowcnt)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiRowEst + uintptr(iCol+int32(1))*8)))
			}
			(*TIndex)(unsafe.Pointer(pIdx)).FnRowEst0 = nRow
			/* Set nSum to the number of distinct (iCol+1) field prefixes that
			 ** occur in the stat4 table for this index. Set sumEq to the sum of
			 ** the nEq values for column iCol for the same set (adding the value
			 ** only once where there exist duplicate prefixes).  */
			i = 0
			for {
				if !(i < nSample) {
					break
				}
				if i == (*TIndex)(unsafe.Pointer(pIdx)).FnSample-int32(1) || **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(i)*20))).FanDLt + uintptr(iCol)*8)) != **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(i+int32(1))*20))).FanDLt + uintptr(iCol)*8)) {
					sumEq = sumEq + **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(i)*20))).FanEq + uintptr(iCol)*8))
					nSum100 = nSum100 + int64(100)
				}
				goto _2
			_2:
				;
				i = i + 1
			}
			if nDist100 > nSum100 && sumEq < nRow {
				avgEq = uint64(libc.Int64FromInt32(100)) * (nRow - sumEq) / uint64(nDist100-nSum100)
			}
			if avgEq == uint64(0) {
				avgEq = uint64(1)
			}
			**(**TtRowcnt)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaAvgEq + uintptr(iCol)*8)) = avgEq
			goto _1
		_1:
			;
			iCol = iCol + 1
		}
	}
}

// C documentation
//
//	/*
//	** Insert a new cell on pPage at cell index "i".  pCell points to the
//	** content of the cell.
//	**
//	** If the cell content will fit on the page, then put it there.  If it
//	** will not fit, then make a copy of the cell content into pTemp if
//	** pTemp is not null.  Regardless of pTemp, allocate a new entry
//	** in pPage->apOvfl[] and make it point to the cell content (either
//	** in pTemp or the original pCell) and also record its index.
//	** Allocating a new entry in pPage->aCell[] implies that
//	** pPage->nOverflow is incremented.
//	**
//	** The insertCellFast() routine below works exactly the same as
//	** insertCell() except that it lacks the pTemp and iChild parameters
//	** which are assumed zero.  Other than that, the two routines are the
//	** same.
//	**
//	** Fixes or enhancements to this routine should be reflected in
//	** insertCellFast()!
//	*/
func _insertCell(tls *libc.TLS, pPage uintptr, i int32, pCell uintptr, sz int32, pTemp uintptr, iChild TPgno) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var data, pIns, v2 uintptr
	var j, rc int32
	var v1 Tu8
	var _ /* idx at bp+0 */ int32
	var _ /* rc2 at bp+4 */ int32
	_, _, _, _, _, _ = data, j, pIns, rc, v1, v2
	**(**int32)(__ccgo_up(bp)) = 0 /* The point in pPage->aCellIdx[] where no cell inserted */
	if (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow != 0 || sz+int32(2) > (*TMemPage)(unsafe.Pointer(pPage)).FnFree {
		if pTemp != 0 {
			libc.Xmemcpy(tls, pTemp, pCell, uint32(sz))
			pCell = pTemp
		}
		_sqlite3Put4byte(tls, pCell, iChild)
		v2 = pPage + 12
		v1 = *(*Tu8)(unsafe.Pointer(v2))
		*(*Tu8)(unsafe.Pointer(v2)) = *(*Tu8)(unsafe.Pointer(v2)) + 1
		j = int32(v1)
		/* Comparison against ArraySize-1 since we hold back one extra slot
		 ** as a contingency.  In other words, never need more than 3 overflow
		 ** slots but 4 are allocated, just to be safe. */
		**(**uintptr)(__ccgo_up(pPage + 36 + uintptr(j)*4)) = pCell
		**(**Tu16)(__ccgo_up(pPage + 28 + uintptr(j)*2)) = uint16(i)
		/* When multiple overflows occur, they are always sequential and in
		 ** sorted order.  This invariants arise because multiple overflows can
		 ** only occur when inserting divider cells into the parent page during
		 ** balancing, and the dividers are adjacent and sorted.
		 */
		/* Overflows in sorted order */
		/* Overflows are sequential */
	} else {
		rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage)
		if rc != SQLITE_OK {
			return rc
		}
		data = (*TMemPage)(unsafe.Pointer(pPage)).FaData
		rc = _allocateSpace(tls, pPage, sz, bp)
		if rc != 0 {
			return rc
		}
		/* The allocateSpace() routine guarantees the following properties
		 ** if it returns successfully */
		**(**int32)(__ccgo_up(pPage + 20)) -= int32(uint16(libc.Int32FromInt32(2) + sz))
		/* In a corrupt database where an entry in the cell index section of
		 ** a btree page has a value of 3 or less, the pCell value might point
		 ** as many as 4 bytes in front of the start of the aData buffer for
		 ** the source page.  Make sure this does not cause problems by not
		 ** reading the first 4 bytes */
		libc.Xmemcpy(tls, data+uintptr(**(**int32)(__ccgo_up(bp))+int32(4)), pCell+uintptr(4), uint32(sz-int32(4)))
		_sqlite3Put4byte(tls, data+uintptr(**(**int32)(__ccgo_up(bp))), iChild)
		pIns = (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(i*int32(2))
		libc.Xmemmove(tls, pIns+uintptr(2), pIns, uint32(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-i)))
		**(**Tu8)(__ccgo_up(pIns)) = uint8(**(**int32)(__ccgo_up(bp)) >> libc.Int32FromInt32(8))
		**(**Tu8)(__ccgo_up(pIns + 1)) = uint8(**(**int32)(__ccgo_up(bp)))
		(*TMemPage)(unsafe.Pointer(pPage)).FnCell = (*TMemPage)(unsafe.Pointer(pPage)).FnCell + 1
		/* increment the cell count */
		v2 = data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(4))
		*(*Tu8)(unsafe.Pointer(v2)) = *(*Tu8)(unsafe.Pointer(v2)) + 1
		v1 = *(*Tu8)(unsafe.Pointer(v2))
		if int32(v1) == 0 {
			**(**Tu8)(__ccgo_up(data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(3)))) = **(**Tu8)(__ccgo_up(data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(3)))) + 1
		}
		if (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FautoVacuum != 0 {
			**(**int32)(__ccgo_up(bp + 4)) = SQLITE_OK
			/* The cell may contain a pointer to an overflow page. If so, write
			 ** the entry for the overflow page into the pointer map.
			 */
			_ptrmapPutOvflPtr(tls, pPage, pPage, pCell, bp+4)
			if **(**int32)(__ccgo_up(bp + 4)) != 0 {
				return **(**int32)(__ccgo_up(bp + 4))
			}
		}
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This variant of insertCell() assumes that the pTemp and iChild
//	** parameters are both zero.  Use this variant in sqlite3BtreeInsert()
//	** for performance improvement, and also so that this variant is only
//	** called from that one place, and is thus inlined, and thus runs must
//	** faster.
//	**
//	** Fixes or enhancements to this routine should be reflected into
//	** the insertCell() routine.
//	*/
func _insertCellFast(tls *libc.TLS, pPage uintptr, i int32, pCell uintptr, sz int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var data, pIns, v2 uintptr
	var j, rc int32
	var v1 Tu8
	var _ /* idx at bp+0 */ int32
	var _ /* rc2 at bp+4 */ int32
	_, _, _, _, _, _ = data, j, pIns, rc, v1, v2
	**(**int32)(__ccgo_up(bp)) = 0 /* The point in pPage->aCellIdx[] where no cell inserted */
	if sz+int32(2) > (*TMemPage)(unsafe.Pointer(pPage)).FnFree {
		v2 = pPage + 12
		v1 = *(*Tu8)(unsafe.Pointer(v2))
		*(*Tu8)(unsafe.Pointer(v2)) = *(*Tu8)(unsafe.Pointer(v2)) + 1
		j = int32(v1)
		/* Comparison against ArraySize-1 since we hold back one extra slot
		 ** as a contingency.  In other words, never need more than 3 overflow
		 ** slots but 4 are allocated, just to be safe. */
		**(**uintptr)(__ccgo_up(pPage + 36 + uintptr(j)*4)) = pCell
		**(**Tu16)(__ccgo_up(pPage + 28 + uintptr(j)*2)) = uint16(i)
		/* When multiple overflows occur, they are always sequential and in
		 ** sorted order.  This invariants arise because multiple overflows can
		 ** only occur when inserting divider cells into the parent page during
		 ** balancing, and the dividers are adjacent and sorted.
		 */
		/* Overflows in sorted order */
		/* Overflows are sequential */
	} else {
		rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage)
		if rc != SQLITE_OK {
			return rc
		}
		data = (*TMemPage)(unsafe.Pointer(pPage)).FaData
		rc = _allocateSpace(tls, pPage, sz, bp)
		if rc != 0 {
			return rc
		}
		/* The allocateSpace() routine guarantees the following properties
		 ** if it returns successfully */
		**(**int32)(__ccgo_up(pPage + 20)) -= int32(uint16(libc.Int32FromInt32(2) + sz))
		libc.Xmemcpy(tls, data+uintptr(**(**int32)(__ccgo_up(bp))), pCell, uint32(sz))
		pIns = (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(i*int32(2))
		libc.Xmemmove(tls, pIns+uintptr(2), pIns, uint32(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-i)))
		**(**Tu8)(__ccgo_up(pIns)) = uint8(**(**int32)(__ccgo_up(bp)) >> libc.Int32FromInt32(8))
		**(**Tu8)(__ccgo_up(pIns + 1)) = uint8(**(**int32)(__ccgo_up(bp)))
		(*TMemPage)(unsafe.Pointer(pPage)).FnCell = (*TMemPage)(unsafe.Pointer(pPage)).FnCell + 1
		/* increment the cell count */
		v2 = data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(4))
		*(*Tu8)(unsafe.Pointer(v2)) = *(*Tu8)(unsafe.Pointer(v2)) + 1
		v1 = *(*Tu8)(unsafe.Pointer(v2))
		if int32(v1) == 0 {
			**(**Tu8)(__ccgo_up(data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(3)))) = **(**Tu8)(__ccgo_up(data + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(3)))) + 1
		}
		if (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FautoVacuum != 0 {
			**(**int32)(__ccgo_up(bp + 4)) = SQLITE_OK
			/* The cell may contain a pointer to an overflow page. If so, write
			 ** the entry for the overflow page into the pointer map.
			 */
			_ptrmapPutOvflPtr(tls, pPage, pPage, pCell, bp+4)
			if **(**int32)(__ccgo_up(bp + 4)) != 0 {
				return **(**int32)(__ccgo_up(bp + 4))
			}
		}
	}
	return SQLITE_OK
}

/*
** The following parameters determine how many adjacent pages get involved
** in a balancing operation.  NN is the number of neighbors on either side
** of the page that participate in the balancing operation.  NB is the
** total number of pages that participate, including the target page and
** NN neighbors on either side.
**
** The minimum value of NN is 1 (of course).  Increasing NN above 1
** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
** in exchange for a larger degradation in INSERT and UPDATE performance.
** The value of NN appears to give the best results overall.
**
** (Later:) The description above makes it seem as if these values are
** tunable - as if you could change them and recompile and it would all work.
** But that is unlikely.  NB has been 3 since the inception of SQLite and
** we have never tested any other value.
 */

// C documentation
//
//	/*
//	** Implementation of the instr() function.
//	**
//	** instr(haystack,needle) finds the first occurrence of needle
//	** in haystack and returns the number of previous characters plus 1,
//	** or 0 if needle does not occur within haystack.
//	**
//	** If both haystack and needle are BLOBs, then the result is one more than
//	** the number of bytes in haystack prior to the first occurrence of needle,
//	** or 0 if needle never occurs in haystack.
//	*/
func _instrFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var N, isText, nHaystack, nNeedle, typeHaystack, typeNeedle int32
	var firstChar uint8
	var pC1, pC2, zHaystack, zNeedle uintptr
	_, _, _, _, _, _, _, _, _, _, _ = N, firstChar, isText, nHaystack, nNeedle, pC1, pC2, typeHaystack, typeNeedle, zHaystack, zNeedle
	N = int32(1)
	pC1 = uintptr(0)
	pC2 = uintptr(0)
	_ = argc
	typeHaystack = Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv)))
	typeNeedle = Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	if typeHaystack == int32(SQLITE_NULL) || typeNeedle == int32(SQLITE_NULL) {
		return
	}
	nHaystack = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
	nNeedle = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	if nNeedle > 0 {
		if typeHaystack == int32(SQLITE_BLOB) && typeNeedle == int32(SQLITE_BLOB) {
			zHaystack = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv)))
			zNeedle = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
			isText = 0
		} else {
			if typeHaystack != int32(SQLITE_BLOB) && typeNeedle != int32(SQLITE_BLOB) {
				zHaystack = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
				zNeedle = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
				isText = int32(1)
			} else {
				pC1 = Xsqlite3_value_dup(tls, **(**uintptr)(__ccgo_up(argv)))
				zHaystack = Xsqlite3_value_text(tls, pC1)
				if zHaystack == uintptr(0) {
					goto endInstrOOM
				}
				nHaystack = Xsqlite3_value_bytes(tls, pC1)
				pC2 = Xsqlite3_value_dup(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
				zNeedle = Xsqlite3_value_text(tls, pC2)
				if zNeedle == uintptr(0) {
					goto endInstrOOM
				}
				nNeedle = Xsqlite3_value_bytes(tls, pC2)
				isText = int32(1)
			}
		}
		if zNeedle == uintptr(0) || nHaystack != 0 && zHaystack == uintptr(0) {
			goto endInstrOOM
		}
		firstChar = **(**uint8)(__ccgo_up(zNeedle))
		for nNeedle <= nHaystack && (int32(**(**uint8)(__ccgo_up(zHaystack))) != int32(firstChar) || libc.Xmemcmp(tls, zHaystack, zNeedle, uint32(nNeedle)) != 0) {
			N = N + 1
			for cond := true; cond; cond = isText != 0 && int32(**(**uint8)(__ccgo_up(zHaystack)))&int32(0xc0) == int32(0x80) {
				nHaystack = nHaystack - 1
				zHaystack = zHaystack + 1
			}
		}
		if nNeedle > nHaystack {
			N = 0
		}
	}
	Xsqlite3_result_int(tls, context, N)
	goto endInstr
endInstr:
	;
	Xsqlite3_value_free(tls, pC1)
	Xsqlite3_value_free(tls, pC2)
	return
	goto endInstrOOM
endInstrOOM:
	;
	Xsqlite3_result_error_nomem(tls, context)
	goto endInstr
}

// C documentation
//
//	/*
//	** Invalidate temp storage, either when the temp storage is changed
//	** from default, or when 'file' and the temp_store_directory has changed
//	*/
func _invalidateTempStorage(tls *libc.TLS, pParse uintptr) (r int32) {
	var db uintptr
	_ = db
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt != uintptr(0) {
		if !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0) || _sqlite3BtreeTxnState(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt) != SQLITE_TXN_NONE {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20274, 0)
			return int32(SQLITE_ERROR)
		}
		_sqlite3BtreeClose(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt)
		(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt = uintptr(0)
		_sqlite3ResetAllSchemasOfConnection(tls, db)
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Check to see if the pExpr expression is a form that needs to be passed
//	** to the xBestIndex method of virtual tables.  Forms of interest include:
//	**
//	**          Expression                   Virtual Table Operator
//	**          -----------------------      ---------------------------------
//	**      1.  column MATCH expr            SQLITE_INDEX_CONSTRAINT_MATCH
//	**      2.  column GLOB expr             SQLITE_INDEX_CONSTRAINT_GLOB
//	**      3.  column LIKE expr             SQLITE_INDEX_CONSTRAINT_LIKE
//	**      4.  column REGEXP expr           SQLITE_INDEX_CONSTRAINT_REGEXP
//	**      5.  column != expr               SQLITE_INDEX_CONSTRAINT_NE
//	**      6.  expr != column               SQLITE_INDEX_CONSTRAINT_NE
//	**      7.  column IS NOT expr           SQLITE_INDEX_CONSTRAINT_ISNOT
//	**      8.  expr IS NOT column           SQLITE_INDEX_CONSTRAINT_ISNOT
//	**      9.  column IS NOT NULL           SQLITE_INDEX_CONSTRAINT_ISNOTNULL
//	**
//	** In every case, "column" must be a column of a virtual table.  If there
//	** is a match, set *ppLeft to the "column" expression, set *ppRight to the
//	** "expr" expression (even though in forms (6) and (8) the column is on the
//	** right and the expression is on the left).  Also set *peOp2 to the
//	** appropriate virtual table operator.  The return value is 1 or 2 if there
//	** is a match.  The usual return is 1, but if the RHS is also a column
//	** of virtual table in forms (5) or (7) then return 2.
//	**
//	** If the expression matches none of the patterns above, return 0.
//	*/
func _isAuxiliaryVtabOperator(tls *libc.TLS, db uintptr, pExpr uintptr, peOp2 uintptr, ppLeft uintptr, ppRight uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, res, v1 int32
	var pCol, pLeft, pList, pMod, pRight, pVtab, t uintptr
	var v2 bool
	var _ /* pNotUsed at bp+4 */ uintptr
	var _ /* xNotUsed at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _ = i, pCol, pLeft, pList, pMod, pRight, pVtab, res, t, v1, v2
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) {
		pList = *(*uintptr)(unsafe.Pointer(pExpr + 20))
		if pList == uintptr(0) || (*TExprList)(unsafe.Pointer(pList)).FnExpr != int32(2) {
			return 0
		}
		/* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
		 ** virtual table on their second argument, which is the same as
		 ** the left-hand side operand in their in-fix form.
		 **
		 **       vtab_column MATCH expression
		 **       MATCH(expression,vtab_column)
		 */
		pCol = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr
		if v2 = int32((*TExpr)(unsafe.Pointer(pCol)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pCol)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB); v2 {
			v1 = _sqlite3ExprIsLikeOperator(tls, pExpr)
			i = v1
		}
		if v2 && v1 != 0 {
			**(**uint8)(__ccgo_up(peOp2)) = uint8(i)
			**(**uintptr)(__ccgo_up(ppRight)) = (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr
			**(**uintptr)(__ccgo_up(ppLeft)) = pCol
			return int32(1)
		}
		/* We can also match against the first column of overloaded
		 ** functions where xFindFunction returns a value of at least
		 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
		 **
		 **      OVERLOADED(vtab_column,expression)
		 **
		 ** Historically, xFindFunction expected to see lower-case function
		 ** names.  But for this use case, xFindFunction is expected to deal
		 ** with function names in an arbitrary case.
		 */
		pCol = (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr
		if int32((*TExpr)(unsafe.Pointer(pCol)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pCol)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) {
			pVtab = (*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, db, (*TExpr)(unsafe.Pointer(pCol)).Fy.FpTab))).FpVtab
			pMod = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule
			if (*Tsqlite3_module)(unsafe.Pointer(pMod)).FxFindFunction != uintptr(0) {
				i = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pMod)).FxFindFunction})))(tls, pVtab, int32(2), *(*uintptr)(unsafe.Pointer(pExpr + 8)), bp, bp+4)
				if i >= int32(SQLITE_INDEX_CONSTRAINT_FUNCTION) {
					**(**uint8)(__ccgo_up(peOp2)) = uint8(i)
					**(**uintptr)(__ccgo_up(ppRight)) = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr
					**(**uintptr)(__ccgo_up(ppLeft)) = pCol
					return int32(1)
				}
			}
		}
	} else {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) >= int32(TK_EQ) {
			/* Comparison operators are a common case.  Save a few comparisons for
			 ** that common case by terminating early. */
			return 0
		} else {
			if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NE) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_ISNOT) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NOTNULL) {
				res = 0
				pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
				pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight
				if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) {
					res = res + 1
				}
				if pRight != 0 && (int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pRight)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB)) {
					res = res + 1
					t = pLeft
					pLeft = pRight
					pRight = t
				}
				**(**uintptr)(__ccgo_up(ppLeft)) = pLeft
				**(**uintptr)(__ccgo_up(ppRight)) = pRight
				if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NE) {
					**(**uint8)(__ccgo_up(peOp2)) = uint8(SQLITE_INDEX_CONSTRAINT_NE)
				}
				if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_ISNOT) {
					**(**uint8)(__ccgo_up(peOp2)) = uint8(SQLITE_INDEX_CONSTRAINT_ISNOT)
				}
				if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NOTNULL) {
					**(**uint8)(__ccgo_up(peOp2)) = uint8(SQLITE_INDEX_CONSTRAINT_ISNOTNULL)
				}
				return res
			}
		}
	}
	return 0
}

// C documentation
//
//	/*
//	** pX is the RHS of an IN operator.  If pX is a SELECT statement
//	** that can be simplified to a direct table access, then return
//	** a pointer to the SELECT statement.  If pX is not a SELECT statement,
//	** or if the SELECT statement needs to be materialized into a transient
//	** table, then return NULL.
//	*/
func _isCandidateForInOpt(tls *libc.TLS, pX uintptr) (r uintptr) {
	var i int32
	var p, pEList, pRes, pSrc, pTab uintptr
	_, _, _, _, _, _ = i, p, pEList, pRes, pSrc, pTab
	if !((*TExpr)(unsafe.Pointer(pX)).Fflags&libc.Uint32FromInt32(EP_xIsSelect) != libc.Uint32FromInt32(0)) {
		return uintptr(0)
	} /* Not a subquery */
	if (*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(libc.Int32FromInt32(EP_VarSelect)) != uint32(0) {
		return uintptr(0)
	} /* Correlated subq */
	p = *(*uintptr)(unsafe.Pointer(pX + 20))
	if (*TSelect)(unsafe.Pointer(p)).FpPrior != 0 {
		return uintptr(0)
	} /* Not a compound SELECT */
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) != 0 {
		return uintptr(0) /* No DISTINCT keyword and no aggregate functions */
	}
	/* Has no GROUP BY clause */
	if (*TSelect)(unsafe.Pointer(p)).FpLimit != 0 {
		return uintptr(0)
	} /* Has no LIMIT clause */
	if (*TSelect)(unsafe.Pointer(p)).FpWhere != 0 {
		return uintptr(0)
	} /* Has no WHERE clause */
	pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc
	if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc != int32(1) {
		return uintptr(0)
	} /* Single term in FROM clause */
	if int32(*(*uint32)(unsafe.Pointer(pSrc + 8 + 12 + 4))&0x4>>2) != 0 {
		return uintptr(0)
	} /* FROM is not a subquery or view */
	pTab = (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpSTab
	/* FROM clause is not a view */
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		return uintptr(0)
	} /* FROM clause not a virtual table */
	pEList = (*TSelect)(unsafe.Pointer(p)).FpEList
	/* All SELECT results must be columns. */
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
			break
		}
		pRes = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr
		if int32((*TExpr)(unsafe.Pointer(pRes)).Fop) != int32(TK_COLUMN) {
			return uintptr(0)
		}
		/* Not a correlated subquery */
		goto _1
	_1:
		;
		i = i + 1
	}
	return p
}

// C documentation
//
//	/*
//	** Return true if the DISTINCT expression-list passed as the third argument
//	** is redundant.
//	**
//	** A DISTINCT list is redundant if any subset of the columns in the
//	** DISTINCT list are collectively unique and individually non-null.
//	*/
func _isDistinctRedundant(tls *libc.TLS, pParse uintptr, pTabList uintptr, pWC uintptr, pDistinct uintptr) (r int32) {
	var i, iBase int32
	var p, pIdx, pTab uintptr
	_, _, _, _, _ = i, iBase, p, pIdx, pTab
	/* If there is more than one table or sub-select in the FROM clause of
	 ** this query, then it will not be possible to show that the DISTINCT
	 ** clause is redundant. */
	if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc != int32(1) {
		return 0
	}
	iBase = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor
	pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpSTab
	/* If any of the expressions is an IPK column on table iBase, then return
	 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
	 ** current SELECT is a correlated sub-query.
	 */
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pDistinct)).FnExpr) {
			break
		}
		p = _sqlite3ExprSkipCollateAndLikely(tls, (*(*TExprList_item)(unsafe.Pointer(pDistinct + 8 + uintptr(i)*20))).FpExpr)
		if p == uintptr(0) {
			goto _1
		}
		if int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_AGG_COLUMN) {
			goto _1
		}
		if (*TExpr)(unsafe.Pointer(p)).FiTable == iBase && int32((*TExpr)(unsafe.Pointer(p)).FiColumn) < 0 {
			return int32(1)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	/* Loop through all indices on the table, checking each to see if it makes
	 ** the DISTINCT qualifier redundant. It does so if:
	 **
	 **   1. The index is itself UNIQUE, and
	 **
	 **   2. All of the columns in the index are either part of the pDistinct
	 **      list, or else the WHERE clause contains a term of the form "col=X",
	 **      where X is a constant value. The collation sequences of the
	 **      comparison and select-list expressions must match those of the index.
	 **
	 **   3. All of those index columns for which the WHERE clause does not
	 **      contain a "col=X" term are subject to a NOT NULL constraint.
	 */
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		if !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != libc.Int32FromInt32(OE_None)) {
			goto _2
		}
		if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 {
			goto _2
		}
		i = 0
		for {
			if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
				break
			}
			if uintptr(0) == _sqlite3WhereFindTerm(tls, pWC, iBase, i, ^libc.Uint64FromInt32(0), uint32(WO_EQ), pIdx) {
				if _findIndexCol(tls, pParse, pDistinct, iBase, pIdx, i) < 0 {
					break
				}
				if _indexColumnNotNull(tls, pIdx, i) == 0 {
					break
				}
			}
			goto _3
		_3:
			;
			i = i + 1
		}
		if i == int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) {
			/* This index implies that the DISTINCT qualifier is redundant. */
			return int32(1)
		}
		goto _2
	_2:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
	}
	return 0
}

// C documentation
//
//	/*
//	** Check to see if the given expression is a LIKE or GLOB operator that
//	** can be optimized using inequality constraints.  Return TRUE if it is
//	** so and false if not.
//	**
//	** In order for the operator to be optimizible, the RHS must be a string
//	** literal that does not begin with a wildcard.  The LHS must be a column
//	** that may only be NULL, a string, or a BLOB, never a number. (This means
//	** that virtual tables cannot participate in the LIKE optimization.)  The
//	** collating sequence for the column on the LHS must be appropriate for
//	** the operator.
//	*/
func _isLikeOrGlob(tls *libc.TLS, pParse uintptr, pExpr uintptr, ppPrefix uintptr, pisComplete uintptr, pnoCase uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var c, v1 Tu8
	var cnt, iCol, iFrom, iTo, isNum, op, r1, rc, v3 int32
	var db, pLeft, pList, pPrefix, pReprepare, pRight, pVal, v, z, zNew uintptr
	var _ /* rDummy at bp+8 */ float64
	var _ /* wc at bp+0 */ [4]Tu8
	var _ /* z2 at bp+4 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, cnt, db, iCol, iFrom, iTo, isNum, op, pLeft, pList, pPrefix, pReprepare, pRight, pVal, r1, rc, v, z, zNew, v1, v3
	z = uintptr(0)                             /* Wildcard characters */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database connection */
	pVal = uintptr(0)                          /* Result code to return */
	if !(_sqlite3IsLikeFunction(tls, db, pExpr, pnoCase, bp) != 0) {
		return 0
	}
	pList = *(*uintptr)(unsafe.Pointer(pExpr + 20))
	pLeft = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr
	pRight = _sqlite3ExprSkipCollate(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr)
	op = int32((*TExpr)(unsafe.Pointer(pRight)).Fop)
	if op == int32(TK_VARIABLE) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_EnableQPSG) == uint64(0) {
		pReprepare = (*TParse)(unsafe.Pointer(pParse)).FpReprepare
		iCol = int32((*TExpr)(unsafe.Pointer(pRight)).FiColumn)
		pVal = _sqlite3VdbeGetBoundValue(tls, pReprepare, iCol, uint8(SQLITE_AFF_BLOB))
		if pVal != 0 && Xsqlite3_value_type(tls, pVal) == int32(SQLITE_TEXT) {
			z = Xsqlite3_value_text(tls, pVal)
		}
		_sqlite3VdbeSetVarmask(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, iCol)
	} else {
		if op == int32(TK_STRING) {
			z = *(*uintptr)(unsafe.Pointer(pRight + 8))
		}
	}
	if z != 0 {
		/* Count the number of prefix bytes prior to the first wildcard,
		 ** U+fffd character, or malformed utf-8. If the underlying database
		 ** has a UTF16LE encoding, then only consider ASCII characters.  Note that
		 ** the encoding of z[] is UTF8 - we are dealing with only UTF8 here in this
		 ** code, but the database engine itself might be processing content using a
		 ** different encoding. */
		cnt = 0
		for {
			v1 = **(**Tu8)(__ccgo_up(z + uintptr(cnt)))
			c = v1
			if !(int32(v1) != 0 && int32(c) != int32((**(**[4]Tu8)(__ccgo_up(bp)))[0]) && int32(c) != int32((**(**[4]Tu8)(__ccgo_up(bp)))[int32(1)]) && int32(c) != int32((**(**[4]Tu8)(__ccgo_up(bp)))[int32(2)])) {
				break
			}
			cnt = cnt + 1
			if int32(c) == int32((**(**[4]Tu8)(__ccgo_up(bp)))[int32(3)]) && int32(**(**Tu8)(__ccgo_up(z + uintptr(cnt)))) > 0 && int32(**(**Tu8)(__ccgo_up(z + uintptr(cnt)))) < int32(0x80) {
				cnt = cnt + 1
			} else {
				if int32(c) >= int32(0x80) {
					**(**uintptr)(__ccgo_up(bp + 4)) = z + uintptr(cnt) - uintptr(1)
					if int32(c) == int32(0xff) || _sqlite3Utf8Read(tls, bp+4) == uint32(0xfffd) || int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc) == int32(SQLITE_UTF16LE) {
						cnt = cnt - 1
						break
					} else {
						cnt = int32(**(**uintptr)(__ccgo_up(bp + 4))) - int32(z)
					}
				}
			}
		}
		/* The optimization is possible only if (1) the pattern does not begin
		 ** with a wildcard and if (2) the non-wildcard prefix does not end with
		 ** an (illegal 0xff) character, or (3) the pattern does not consist of
		 ** a single escape character. The second condition is necessary so
		 ** that we can increment the prefix key to find an upper bound for the
		 ** range search. The third is because the caller assumes that the pattern
		 ** consists of at least one character after all escapes have been
		 ** removed.  */
		if (cnt > int32(1) || cnt > 0 && int32(**(**Tu8)(__ccgo_up(z))) != int32((**(**[4]Tu8)(__ccgo_up(bp)))[int32(3)])) && int32(255) != int32(**(**Tu8)(__ccgo_up(z + uintptr(cnt-int32(1))))) {
			/* A "complete" match if the pattern ends with "*" or "%" */
			**(**int32)(__ccgo_up(pisComplete)) = libc.BoolInt32(int32(c) == int32((**(**[4]Tu8)(__ccgo_up(bp)))[0]) && int32(**(**Tu8)(__ccgo_up(z + uintptr(cnt+int32(1))))) == 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc) != int32(SQLITE_UTF16LE))
			/* Get the pattern prefix.  Remove all escapes from the prefix. */
			pPrefix = _sqlite3Expr(tls, db, int32(TK_STRING), z)
			if pPrefix != 0 {
				zNew = *(*uintptr)(unsafe.Pointer(pPrefix + 8))
				**(**int8)(__ccgo_up(zNew + uintptr(cnt))) = 0
				v3 = libc.Int32FromInt32(0)
				iTo = v3
				iFrom = v3
				for {
					if !(iFrom < cnt) {
						break
					}
					if int32(**(**int8)(__ccgo_up(zNew + uintptr(iFrom)))) == int32((**(**[4]Tu8)(__ccgo_up(bp)))[int32(3)]) {
						iFrom = iFrom + 1
					}
					v3 = iTo
					iTo = iTo + 1
					**(**int8)(__ccgo_up(zNew + uintptr(v3))) = **(**int8)(__ccgo_up(zNew + uintptr(iFrom)))
					goto _2
				_2:
					;
					iFrom = iFrom + 1
				}
				**(**int8)(__ccgo_up(zNew + uintptr(iTo))) = 0
				/* If the LHS is not an ordinary column with TEXT affinity, then the
				 ** pattern prefix boundaries (both the start and end boundaries) must
				 ** not look like a number.  Otherwise the pattern might be treated as
				 ** a number, which will invalidate the LIKE optimization.
				 **
				 ** Getting this right has been a persistent source of bugs in the
				 ** LIKE optimization.  See, for example:
				 **    2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
				 **    2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
				 **    2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
				 **    2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
				 **    2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
				 */
				if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) != int32(TK_COLUMN) || int32(_sqlite3ExprAffinity(tls, pLeft)) != int32(SQLITE_AFF_TEXT) || (*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) && (*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab != 0 && int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pLeft)).Fy.FpTab)).FeTabType) == int32(TABTYP_VTAB) {
					isNum = _sqlite3AtoF(tls, zNew, bp+8)
					if isNum <= 0 {
						if iTo == int32(1) && int32(**(**int8)(__ccgo_up(zNew))) == int32('-') {
							isNum = +libc.Int32FromInt32(1)
						} else {
							**(**int8)(__ccgo_up(zNew + uintptr(iTo-int32(1)))) = **(**int8)(__ccgo_up(zNew + uintptr(iTo-int32(1)))) + 1
							isNum = _sqlite3AtoF(tls, zNew, bp+8)
							**(**int8)(__ccgo_up(zNew + uintptr(iTo-int32(1)))) = **(**int8)(__ccgo_up(zNew + uintptr(iTo-int32(1)))) - 1
						}
					}
					if isNum > 0 {
						_sqlite3ExprDelete(tls, db, pPrefix)
						_sqlite3ValueFree(tls, pVal)
						return 0
					}
				}
			}
			**(**uintptr)(__ccgo_up(ppPrefix)) = pPrefix
			/* If the RHS pattern is a bound parameter, make arrangements to
			 ** reprepare the statement when that parameter is rebound */
			if op == int32(TK_VARIABLE) {
				v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
				_sqlite3VdbeSetVarmask(tls, v, int32((*TExpr)(unsafe.Pointer(pRight)).FiColumn))
				if **(**int32)(__ccgo_up(pisComplete)) != 0 && **(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pRight + 8)) + 1)) != 0 {
					/* If the rhs of the LIKE expression is a variable, and the current
					 ** value of the variable means there is no need to invoke the LIKE
					 ** function, then no OP_Variable will be added to the program.
					 ** This causes problems for the sqlite3_bind_parameter_name()
					 ** API. To work around them, add a dummy OP_Variable here.
					 */
					r1 = _sqlite3GetTempReg(tls, pParse)
					_sqlite3ExprCodeTarget(tls, pParse, pRight, r1)
					_sqlite3VdbeChangeP3(tls, v, _sqlite3VdbeCurrentAddr(tls, v)-int32(1), 0)
					_sqlite3ReleaseTempReg(tls, pParse, r1)
				}
			}
		} else {
			z = uintptr(0)
		}
	}
	rc = libc.BoolInt32(z != uintptr(0))
	_sqlite3ValueFree(tls, pVal)
	return rc
}

// C documentation
//
//	/*
//	** Return true if the parser passed as the first argument is being
//	** used to code a trigger that is really a "SET NULL" action belonging
//	** to trigger pFKey.
//	*/
func _isSetNullAction(tls *libc.TLS, pParse uintptr, pFKey uintptr) (r int32) {
	var p, pTop, v1 uintptr
	_, _, _ = p, pTop, v1
	if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 {
		v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel
	} else {
		v1 = pParse
	}
	pTop = v1
	if (*TParse)(unsafe.Pointer(pTop)).FpTriggerPrg != 0 {
		p = (*TTriggerPrg)(unsafe.Pointer((*TParse)(unsafe.Pointer(pTop)).FpTriggerPrg)).FpTrigger
		if p == **(**uintptr)(__ccgo_up(pFKey + 28)) && int32(**(**Tu8)(__ccgo_up(pFKey + 25))) == int32(OE_SetNull) || p == **(**uintptr)(__ccgo_up(pFKey + 28 + 1*4)) && int32(**(**Tu8)(__ccgo_up(pFKey + 25 + 1))) == int32(OE_SetNull) {
			return int32(1)
		}
	}
	return 0
}

// C documentation
//
//	/*
//	** The select statement passed as the first argument is an aggregate query.
//	** The second argument is the associated aggregate-info object. This
//	** function tests if the SELECT is of the form:
//	**
//	**   SELECT count(*) FROM <tbl>
//	**
//	** where table is a database table, not a sub-select or view. If the query
//	** does match this pattern, then a pointer to the Table object representing
//	** <tbl> is returned. Otherwise, NULL is returned.
//	**
//	** This routine checks to see if it is safe to use the count optimization.
//	** A correct answer is still obtained (though perhaps more slowly) if
//	** this routine returns NULL when it could have returned a table pointer.
//	** But returning the pointer when NULL should have been returned can
//	** result in incorrect answers and/or crashes.  So, when in doubt, return NULL.
//	*/
func _isSimpleCount(tls *libc.TLS, p uintptr, pAggInfo uintptr) (r uintptr) {
	var pExpr, pTab uintptr
	_, _ = pExpr, pTab
	if (*TSelect)(unsafe.Pointer(p)).FpWhere != 0 || (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr != int32(1) || (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc != int32(1) || int32(*(*uint32)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8 + 12 + 4))&0x4>>2) != 0 || (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc != int32(1) || (*TSelect)(unsafe.Pointer(p)).FpHaving != 0 {
		return uintptr(0)
	}
	pTab = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSTab
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
		return uintptr(0)
	}
	pExpr = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8))).FpExpr
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_AGG_FUNCTION) {
		return uintptr(0)
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo != pAggInfo {
		return uintptr(0)
	}
	if (*TFuncDef)(unsafe.Pointer((**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_COUNT) == uint32(0) {
		return uintptr(0)
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)|libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
		return uintptr(0)
	}
	return pTab
}

// C documentation
//
//	/*
//	** Edit the payload size of the element at iRoot by the amount in
//	** pParse->delta.
//	*/
func _jsonAfterEditSizeAdjust(tls *libc.TLS, pParse uintptr, iRoot Tu32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nBlob Tu32
	var _ /* sz at bp+0 */ Tu32
	_ = nBlob
	**(**Tu32)(__ccgo_up(bp)) = uint32(0)
	nBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob
	(*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc
	_jsonbPayloadSize(tls, pParse, iRoot, bp)
	(*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = nBlob
	**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + uint32((*TJsonParse)(unsafe.Pointer(pParse)).Fdelta)
	**(**int32)(__ccgo_up(pParse + 40)) += _jsonBlobChangePayloadSize(tls, pParse, iRoot, **(**Tu32)(__ccgo_up(bp)))
}

// C documentation
//
//	/* c is a control character.  Append the canonical JSON representation
//	** of that control character to p.
//	**
//	** This routine assumes that the output buffer has already been enlarged
//	** sufficiently to hold the worst-case encoding plus a nul terminator.
//	*/
func _jsonAppendControlChar(tls *libc.TLS, p uintptr, c Tu8) {
	if _aSpecial[c] != 0 {
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed))) = int8('\\')
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(1)))) = _aSpecial[c]
		**(**Tu64)(__ccgo_up(p + 16)) += uint64(2)
	} else {
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed))) = int8('\\')
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(1)))) = int8('u')
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(2)))) = int8('0')
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(3)))) = int8('0')
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(4)))) = **(**int8)(__ccgo_up(__ccgo_ts + 1733 + uintptr(int32(c)>>int32(4))))
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(5)))) = **(**int8)(__ccgo_up(__ccgo_ts + 1733 + uintptr(int32(c)&int32(0xf))))
		**(**Tu64)(__ccgo_up(p + 16)) += uint64(6)
	}
}

// C documentation
//
//	/*
//	** Append the path name for the current element.
//	*/
func _jsonAppendPathName(tls *libc.TLS, p uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, k, n Tu32
	var needQuote int32
	var z uintptr
	var _ /* sz at bp+0 */ Tu32
	_, _, _, _, _ = i, k, n, needQuote, z
	if int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeType) == int32(JSONB_ARRAY) {
		_jsonPrintf(tls, int32(30), p+40, __ccgo_ts+28182, libc.VaList(bp+16, (**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiKey))
	} else {
		**(**Tu32)(__ccgo_up(bp)) = uint32(0)
		needQuote = 0
		n = _jsonbPayloadSize(tls, p+168, (*TJsonEachCursor)(unsafe.Pointer(p)).Fi, bp)
		k = (*TJsonEachCursor)(unsafe.Pointer(p)).Fi + n
		z = (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(k)
		if **(**Tu32)(__ccgo_up(bp)) == uint32(0) || !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z)))])&libc.Int32FromInt32(0x02) != 0) {
			needQuote = int32(1)
		} else {
			i = uint32(0)
			for {
				if !(i < **(**Tu32)(__ccgo_up(bp))) {
					break
				}
				if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i))))])&libc.Int32FromInt32(0x06) != 0) {
					needQuote = int32(1)
					break
				}
				goto _1
			_1:
				;
				i = i + 1
			}
		}
		if needQuote != 0 {
			_jsonPrintf(tls, int32(**(**Tu32)(__ccgo_up(bp))+uint32(4)), p+40, __ccgo_ts+28189, libc.VaList(bp+16, **(**Tu32)(__ccgo_up(bp)), z))
		} else {
			_jsonPrintf(tls, int32(**(**Tu32)(__ccgo_up(bp))+uint32(2)), p+40, __ccgo_ts+28197, libc.VaList(bp+16, **(**Tu32)(__ccgo_up(bp)), z))
		}
	}
}

// C documentation
//
//	/*
//	** Append an sqlite3_value (such as a function parameter) to the JSON
//	** string under construction in p.
//	*/
func _jsonAppendSqlValue(tls *libc.TLS, p uintptr, pValue uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var n, n1 Tu32
	var z, z1 uintptr
	var _ /* px at bp+0 */ TJsonParse
	_, _, _, _ = n, n1, z, z1
	switch Xsqlite3_value_type(tls, pValue) {
	case int32(SQLITE_NULL):
		_jsonAppendRawNZ(tls, p, __ccgo_ts+1697, uint32(4))
	case int32(SQLITE_FLOAT):
		_jsonPrintf(tls, int32(100), p, __ccgo_ts+17781, libc.VaList(bp+64, Xsqlite3_value_double(tls, pValue)))
	case int32(SQLITE_INTEGER):
		z = Xsqlite3_value_text(tls, pValue)
		n = uint32(Xsqlite3_value_bytes(tls, pValue))
		_jsonAppendRaw(tls, p, z, n)
	case int32(SQLITE_TEXT):
		z1 = Xsqlite3_value_text(tls, pValue)
		n1 = uint32(Xsqlite3_value_bytes(tls, pValue))
		if Xsqlite3_value_subtype(tls, pValue) == uint32(JSON_SUBTYPE) {
			_jsonAppendRaw(tls, p, z1, n1)
		} else {
			_jsonAppendString(tls, p, z1, n1)
		}
	default:
		libc.Xmemset(tls, bp, 0, uint32(56))
		if _jsonArgIsJsonb(tls, pValue, bp) != 0 {
			_jsonTranslateBlobToText(tls, bp, uint32(0), p)
		} else {
			if int32((*TJsonString)(unsafe.Pointer(p)).FeErr) == 0 {
				Xsqlite3_result_error(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx, __ccgo_ts+27670, -int32(1))
				(*TJsonString)(unsafe.Pointer(p)).FeErr = uint8(JSTRING_ERR)
				_jsonStringReset(tls, p)
			}
		}
		break
	}
}

// C documentation
//
//	/* Append the N-byte string in zIn to the end of the JsonString string
//	** under construction.  Enclose the string in double-quotes ("...") and
//	** escape any double-quotes or backslash characters contained within the
//	** string.
//	**
//	** This routine is a high-runner.  There is a measurable performance
//	** increase associated with unwinding the jsonIsOk[] loop.
//	*/
func _jsonAppendString(tls *libc.TLS, p uintptr, zIn uintptr, N Tu32) {
	var c Tu8
	var k Tu32
	var z, v2 uintptr
	var v1 Tu64
	_, _, _, _, _ = c, k, z, v1, v2
	z = zIn
	if z == uintptr(0) {
		return
	}
	if uint64(N)+(*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(2) >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, N+uint32(2)) != 0 {
		return
	}
	v2 = p + 16
	v1 = *(*Tu64)(unsafe.Pointer(v2))
	*(*Tu64)(unsafe.Pointer(v2)) = *(*Tu64)(unsafe.Pointer(v2)) + 1
	**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = int8('"')
	for int32(1) != 0 {
		k = uint32(0)
		/* The following while() is the 4-way unwound equivalent of
		 **
		 **     while( k<N && jsonIsOk[z[k]] ){ k++; }
		 */
		for int32(1) != 0 {
			if k+uint32(3) >= N {
				for k < N && _jsonIsOk[**(**Tu8)(__ccgo_up(z + uintptr(k)))] != 0 {
					k = k + 1
				}
				break
			}
			if !(_jsonIsOk[**(**Tu8)(__ccgo_up(z + uintptr(k)))] != 0) {
				break
			}
			if !(_jsonIsOk[**(**Tu8)(__ccgo_up(z + uintptr(k+uint32(1))))] != 0) {
				k = k + uint32(1)
				break
			}
			if !(_jsonIsOk[**(**Tu8)(__ccgo_up(z + uintptr(k+uint32(2))))] != 0) {
				k = k + uint32(2)
				break
			}
			if !(_jsonIsOk[**(**Tu8)(__ccgo_up(z + uintptr(k+uint32(3))))] != 0) {
				k = k + uint32(3)
				break
			} else {
				k = k + uint32(4)
			}
		}
		if k >= N {
			if k > uint32(0) {
				libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), z, k)
				**(**Tu64)(__ccgo_up(p + 16)) += uint64(k)
			}
			break
		}
		if k > uint32(0) {
			libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), z, k)
			**(**Tu64)(__ccgo_up(p + 16)) += uint64(k)
			z = z + uintptr(k)
			N = N - k
		}
		c = **(**Tu8)(__ccgo_up(z))
		if int32(c) == int32('"') || int32(c) == int32('\\') {
			if (*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(N)+uint64(3) > (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, N+uint32(3)) != 0 {
				return
			}
			v2 = p + 16
			v1 = *(*Tu64)(unsafe.Pointer(v2))
			*(*Tu64)(unsafe.Pointer(v2)) = *(*Tu64)(unsafe.Pointer(v2)) + 1
			**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = int8('\\')
			v2 = p + 16
			v1 = *(*Tu64)(unsafe.Pointer(v2))
			*(*Tu64)(unsafe.Pointer(v2)) = *(*Tu64)(unsafe.Pointer(v2)) + 1
			**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = int8(c)
		} else {
			if int32(c) == int32('\'') {
				v2 = p + 16
				v1 = *(*Tu64)(unsafe.Pointer(v2))
				*(*Tu64)(unsafe.Pointer(v2)) = *(*Tu64)(unsafe.Pointer(v2)) + 1
				**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = int8(c)
			} else {
				if (*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(N)+uint64(7) > (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, N+uint32(7)) != 0 {
					return
				}
				_jsonAppendControlChar(tls, p, c)
			}
		}
		z = z + 1
		N = N - 1
	}
	v2 = p + 16
	v1 = *(*Tu64)(unsafe.Pointer(v2))
	*(*Tu64)(unsafe.Pointer(v2)) = *(*Tu64)(unsafe.Pointer(v2)) + 1
	**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = int8('"')
}

func _jsonArrayCompute(tls *libc.TLS, ctx uintptr, isFinal int32) {
	var flags int32
	var pStr uintptr
	var v1 Tsqlite3_destructor_type
	_, _, _ = flags, pStr, v1
	flags = int32(Xsqlite3_user_data(tls, ctx))
	pStr = Xsqlite3_aggregate_context(tls, ctx, 0)
	if pStr != 0 {
		(*TJsonString)(unsafe.Pointer(pStr)).FpCtx = ctx
		_jsonAppendRawNZ(tls, pStr, __ccgo_ts+6529, uint32(2))
		_jsonStringTrimOneChar(tls, pStr)
		if (*TJsonString)(unsafe.Pointer(pStr)).FeErr != 0 {
			_jsonReturnString(tls, pStr, uintptr(0), uintptr(0))
			return
		} else {
			if flags&int32(JSON_BLOB) != 0 {
				_jsonReturnStringAsBlob(tls, pStr)
				if isFinal != 0 {
					if !((*TJsonString)(unsafe.Pointer(pStr)).FbStatic != 0) {
						_sqlite3RCStrUnref(tls, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf)
					}
				} else {
					_jsonStringTrimOneChar(tls, pStr)
				}
				return
			} else {
				if isFinal != 0 {
					if (*TJsonString)(unsafe.Pointer(pStr)).FbStatic != 0 {
						v1 = uintptr(-libc.Int32FromInt32(1))
					} else {
						v1 = __ccgo_fp(_sqlite3RCStrUnref)
					}
					Xsqlite3_result_text(tls, ctx, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf, int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed), v1)
					(*TJsonString)(unsafe.Pointer(pStr)).FbStatic = uint8(1)
				} else {
					Xsqlite3_result_text(tls, ctx, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf, int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed), uintptr(-libc.Int32FromInt32(1)))
					_jsonStringTrimOneChar(tls, pStr)
				}
			}
		}
	} else {
		if flags&int32(JSON_BLOB) != 0 {
			Xsqlite3_result_blob(tls, ctx, uintptr(unsafe.Pointer(&_emptyArray)), int32(1), libc.UintptrFromInt32(0))
		} else {
			Xsqlite3_result_text(tls, ctx, __ccgo_ts+28091, int32(2), libc.UintptrFromInt32(0))
		}
	}
	Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
}

// C documentation
//
//	/*
//	** json_array_length(JSON)
//	** json_array_length(JSON, PATH)
//	**
//	** Return the number of elements in the top-level JSON array.
//	** Return 0 if the input is not a well-formed JSON array.
//	*/
func _jsonArrayLengthFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	var cnt Tsqlite3_int64
	var eErr Tu8
	var i Tu32
	var p, zPath, v1 uintptr
	_, _, _, _, _, _ = cnt, eErr, i, p, zPath, v1 /* The parse */
	cnt = 0
	eErr = uint8(0)
	p = _jsonParseFuncArg(tls, ctx, **(**uintptr)(__ccgo_up(argv)), uint32(0))
	if p == uintptr(0) {
		return
	}
	if argc == int32(2) {
		zPath = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
		if zPath == uintptr(0) {
			_jsonParseFree(tls, p)
			return
		}
		if int32(**(**int8)(__ccgo_up(zPath))) == int32('$') {
			v1 = zPath + uintptr(1)
		} else {
			v1 = __ccgo_ts + 27909
		}
		i = _jsonLookupStep(tls, p, uint32(0), v1, uint32(0))
		if i >= uint32(JSON_LOOKUP_PATHERROR) {
			if i == uint32(JSON_LOOKUP_NOTFOUND) {
				/* no-op */
			} else {
				_jsonBadPathError(tls, ctx, zPath, int32(i))
			}
			eErr = uint8(1)
			i = uint32(0)
		}
	} else {
		i = uint32(0)
	}
	if int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(p)).FaBlob + uintptr(i))))&int32(0x0f) == int32(JSONB_ARRAY) {
		cnt = int64(_jsonbArrayCount(tls, p, i))
	}
	if !(eErr != 0) {
		Xsqlite3_result_int64(tls, ctx, cnt)
	}
	_jsonParseFree(tls, p)
}

// C documentation
//
//	/* Append a node type byte together with the payload size and
//	** possibly also the payload.
//	**
//	** If aPayload is not NULL, then it is a pointer to the payload which
//	** is also appended.  If aPayload is NULL, the pParse->aBlob[] array
//	** is resized (if necessary) so that it is big enough to hold the
//	** payload, but the payload is not appended and pParse->nBlob is left
//	** pointing to where the first byte of payload will eventually be.
//	*/
func _jsonBlobAppendNode(tls *libc.TLS, pParse uintptr, eType Tu8, szPayload Tu64, aPayload uintptr) {
	var a, v1 uintptr
	_, _ = a, v1
	if uint64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob)+szPayload+uint64(9) > uint64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc) {
		_jsonBlobExpandAndAppendNode(tls, pParse, eType, szPayload, aPayload)
		return
	}
	a = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob)
	if szPayload <= uint64(11) {
		**(**Tu8)(__ccgo_up(a)) = uint8(uint64(eType) | szPayload<<libc.Int32FromInt32(4))
		**(**Tu32)(__ccgo_up(pParse + 4)) += uint32(1)
	} else {
		if szPayload <= uint64(0xff) {
			**(**Tu8)(__ccgo_up(a)) = uint8(int32(eType) | int32(0xc0))
			**(**Tu8)(__ccgo_up(a + 1)) = uint8(szPayload & uint64(0xff))
			**(**Tu32)(__ccgo_up(pParse + 4)) += uint32(2)
		} else {
			if szPayload <= uint64(0xffff) {
				**(**Tu8)(__ccgo_up(a)) = uint8(int32(eType) | int32(0xd0))
				**(**Tu8)(__ccgo_up(a + 1)) = uint8(szPayload >> libc.Int32FromInt32(8) & uint64(0xff))
				**(**Tu8)(__ccgo_up(a + 2)) = uint8(szPayload & uint64(0xff))
				**(**Tu32)(__ccgo_up(pParse + 4)) += uint32(3)
			} else {
				**(**Tu8)(__ccgo_up(a)) = uint8(int32(eType) | int32(0xe0))
				**(**Tu8)(__ccgo_up(a + 1)) = uint8(szPayload >> libc.Int32FromInt32(24) & uint64(0xff))
				**(**Tu8)(__ccgo_up(a + 2)) = uint8(szPayload >> libc.Int32FromInt32(16) & uint64(0xff))
				**(**Tu8)(__ccgo_up(a + 3)) = uint8(szPayload >> libc.Int32FromInt32(8) & uint64(0xff))
				**(**Tu8)(__ccgo_up(a + 4)) = uint8(szPayload & uint64(0xff))
				**(**Tu32)(__ccgo_up(pParse + 4)) += uint32(5)
			}
		}
	}
	if aPayload != 0 {
		v1 = pParse + 4
		*(*Tu32)(unsafe.Pointer(v1)) = Tu32(uint64(*(*Tu32)(unsafe.Pointer(v1))) + szPayload)
		libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(uint64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob)-szPayload), aPayload, uint32(szPayload))
	}
}

// C documentation
//
//	/* Change the payload size for the node at index i to be szPayload.
//	*/
func _jsonBlobChangePayloadSize(tls *libc.TLS, pParse uintptr, i Tu32, szPayload Tu32) (r int32) {
	var a uintptr
	var delta int32
	var nExtra, nNeeded, szType Tu8
	var newSize Tu32
	_, _, _, _, _, _ = a, delta, nExtra, nNeeded, newSize, szType
	if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 {
		return 0
	}
	a = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i)
	szType = uint8(int32(**(**Tu8)(__ccgo_up(a))) >> int32(4))
	if int32(szType) <= int32(11) {
		nExtra = uint8(0)
	} else {
		if int32(szType) == int32(12) {
			nExtra = uint8(1)
		} else {
			if int32(szType) == int32(13) {
				nExtra = uint8(2)
			} else {
				if int32(szType) == int32(14) {
					nExtra = uint8(4)
				} else {
					nExtra = uint8(8)
				}
			}
		}
	}
	if szPayload <= uint32(11) {
		nNeeded = uint8(0)
	} else {
		if szPayload <= uint32(0xff) {
			nNeeded = uint8(1)
		} else {
			if szPayload <= uint32(0xffff) {
				nNeeded = uint8(2)
			} else {
				nNeeded = uint8(4)
			}
		}
	}
	delta = int32(nNeeded) - int32(nExtra)
	if delta != 0 {
		newSize = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob + uint32(delta)
		if delta > 0 {
			if newSize > (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc && _jsonBlobExpand(tls, pParse, newSize) != 0 {
				return 0 /* OOM error.  Error state recorded in pParse->oom. */
			}
			a = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i)
			libc.Xmemmove(tls, a+uintptr(int32(1)+delta), a+1, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-(i+uint32(1)))
		} else {
			libc.Xmemmove(tls, a+1, a+uintptr(int32(1)-delta), (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-(i+uint32(1)-uint32(delta)))
		}
		(*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = newSize
	}
	if int32(nNeeded) == 0 {
		**(**Tu8)(__ccgo_up(a)) = uint8(uint32(int32(**(**Tu8)(__ccgo_up(a)))&libc.Int32FromInt32(0x0f)) | szPayload<<libc.Int32FromInt32(4))
	} else {
		if int32(nNeeded) == int32(1) {
			**(**Tu8)(__ccgo_up(a)) = uint8(int32(**(**Tu8)(__ccgo_up(a)))&int32(0x0f) | int32(0xc0))
			**(**Tu8)(__ccgo_up(a + 1)) = uint8(szPayload & uint32(0xff))
		} else {
			if int32(nNeeded) == int32(2) {
				**(**Tu8)(__ccgo_up(a)) = uint8(int32(**(**Tu8)(__ccgo_up(a)))&int32(0x0f) | int32(0xd0))
				**(**Tu8)(__ccgo_up(a + 1)) = uint8(szPayload >> libc.Int32FromInt32(8) & uint32(0xff))
				**(**Tu8)(__ccgo_up(a + 2)) = uint8(szPayload & uint32(0xff))
			} else {
				**(**Tu8)(__ccgo_up(a)) = uint8(int32(**(**Tu8)(__ccgo_up(a)))&int32(0x0f) | int32(0xe0))
				**(**Tu8)(__ccgo_up(a + 1)) = uint8(szPayload >> libc.Int32FromInt32(24) & uint32(0xff))
				**(**Tu8)(__ccgo_up(a + 2)) = uint8(szPayload >> libc.Int32FromInt32(16) & uint32(0xff))
				**(**Tu8)(__ccgo_up(a + 3)) = uint8(szPayload >> libc.Int32FromInt32(8) & uint32(0xff))
				**(**Tu8)(__ccgo_up(a + 4)) = uint8(szPayload & uint32(0xff))
			}
		}
	}
	return delta
}

// C documentation
//
//	/*
//	** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of
//	** content beginning at iDel, and replacing them with nIns bytes of
//	** content given by aIns.
//	**
//	** nDel may be zero, in which case no bytes are removed.  But iDel is
//	** still important as new bytes will be insert beginning at iDel.
//	**
//	** aIns may be zero, in which case space is created to hold nIns bytes
//	** beginning at iDel, but that space is uninitialized.
//	**
//	** Set pParse->oom if an OOM occurs.
//	*/
func _jsonBlobEdit(tls *libc.TLS, pParse uintptr, iDel Tu32, nDel Tu32, aIns uintptr, nIns Tu32) {
	var d Ti64
	var v1 uintptr
	_, _ = d, v1
	d = int64(nIns) - int64(nDel)
	if d < 0 && d >= int64(-libc.Int32FromInt32(8)) && aIns != uintptr(0) && _jsonBlobOverwrite(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel), aIns, nIns, uint32(int32(-d))) != 0 {
		return
	}
	if d != 0 {
		if int64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob)+d > int64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc) {
			_jsonBlobExpand(tls, pParse, uint32(int64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob)+d))
			if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 {
				return
			}
		}
		libc.Xmemmove(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel+nIns), (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel+nDel), (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-(iDel+nDel))
		v1 = pParse + 4
		*(*Tu32)(unsafe.Pointer(v1)) = Tu32(int64(*(*Tu32)(unsafe.Pointer(v1))) + d)
		v1 = pParse + 40
		*(*int32)(unsafe.Pointer(v1)) = int32(int64(*(*int32)(unsafe.Pointer(v1))) + d)
	}
	if nIns != 0 && aIns != 0 {
		libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel), aIns, nIns)
	}
}

// C documentation
//
//	/* Expand pParse->aBlob and append one bytes.
//	*/
func _jsonBlobExpandAndAppendOneByte(tls *libc.TLS, pParse uintptr, c Tu8) {
	var v1 Tu32
	var v2 uintptr
	_, _ = v1, v2
	_jsonBlobExpand(tls, pParse, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob+uint32(1))
	if int32((*TJsonParse)(unsafe.Pointer(pParse)).Foom) == 0 {
		v2 = pParse + 4
		v1 = *(*Tu32)(unsafe.Pointer(v2))
		*(*Tu32)(unsafe.Pointer(v2)) = *(*Tu32)(unsafe.Pointer(v2)) + 1
		**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(v1))) = c
	}
}

// C documentation
//
//	/*
//	** If the JSONB at aIns[0..nIns-1] can be expanded (by denormalizing the
//	** size field) by d bytes, then write the expansion into aOut[] and
//	** return true.  In this way, an overwrite happens without changing the
//	** size of the JSONB, which reduces memcpy() operations and also make it
//	** faster and easier to update the B-Tree entry that contains the JSONB
//	** in the database.
//	**
//	** If the expansion of aIns[] by d bytes cannot be (easily) accomplished
//	** then return false.
//	**
//	** The d parameter is guaranteed to be between 1 and 8.
//	**
//	** This routine is an optimization.  A correct answer is obtained if it
//	** always leaves the output unchanged and returns false.
//	*/
func _jsonBlobOverwrite(tls *libc.TLS, aOut uintptr, aIns uintptr, nIns Tu32, d Tu32) (r int32) {
	var i, szPayload Tu32
	var szHdr Tu8
	_, _, _ = i, szHdr, szPayload /* Size of header before expansion */
	if int32(**(**Tu8)(__ccgo_up(aIns)))&int32(0x0f) <= int32(2) {
		return 0
	} /* Cannot enlarge NULL, true, false */
	switch int32(**(**Tu8)(__ccgo_up(aIns))) >> libc.Int32FromInt32(4) {
	default: /* aIns[] header size 1 */
		if int32(1)<<d&int32(0x116) == 0 {
			return 0
		} /* d must be 1, 2, 4, or 8 */
		i = d + uint32(1) /* New hdr sz: 2, 3, 5, or 9 */
		szHdr = uint8(1)
	case int32(12): /* aIns[] header size is 2 */
		if int32(1)<<d&int32(0x8a) == 0 {
			return 0
		} /* d must be 1, 3, or 7 */
		i = d + uint32(2) /* New hdr sz: 2, 5, or 9 */
		szHdr = uint8(2)
	case int32(13): /* aIns[] header size is 3 */
		if d != uint32(2) && d != uint32(6) {
			return 0
		} /* d must be 2 or 6 */
		i = d + uint32(3) /* New hdr sz: 5 or 9 */
		szHdr = uint8(3)
	case int32(14): /* aIns[] header size is 5 */
		if d != uint32(4) {
			return 0
		} /* d must be 4 */
		i = uint32(9) /* New hdr sz: 9 */
		szHdr = uint8(5)
	case int32(15): /* aIns[] header size is 9 */
		return 0 /* No solution */
	}
	**(**Tu8)(__ccgo_up(aOut)) = uint8(int32(**(**Tu8)(__ccgo_up(aIns)))&int32(0x0f) | int32(_aType1[i-uint32(2)]))
	libc.Xmemcpy(tls, aOut+uintptr(i), aIns+uintptr(szHdr), nIns-uint32(szHdr))
	szPayload = nIns - uint32(szHdr)
	for int32(1) != 0 {
		i = i - 1
		**(**Tu8)(__ccgo_up(aOut + uintptr(i))) = uint8(szPayload & uint32(0xff))
		if i == uint32(1) {
			break
		}
		szPayload = szPayload >> uint32(8)
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** Insert a new entry into the cache.  If the cache is full, expel
//	** the least recently used entry.  Return SQLITE_OK on success or a
//	** result code otherwise.
//	**
//	** Cache entries are stored in age order, oldest first.
//	*/
func _jsonCacheInsert(tls *libc.TLS, ctx uintptr, pParse uintptr) (r int32) {
	var db, p uintptr
	_, _ = db, p
	p = Xsqlite3_get_auxdata(tls, ctx, -int32(429938))
	if p == uintptr(0) {
		db = Xsqlite3_context_db_handle(tls, ctx)
		p = _sqlite3DbMallocZero(tls, db, uint64(24))
		if p == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		(*TJsonCache)(unsafe.Pointer(p)).Fdb = db
		Xsqlite3_set_auxdata(tls, ctx, -int32(429938), p, __ccgo_fp(_jsonCacheDeleteGeneric))
		p = Xsqlite3_get_auxdata(tls, ctx, -int32(429938))
		if p == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
	}
	if (*TJsonCache)(unsafe.Pointer(p)).FnUsed >= int32(JSON_CACHE_SIZE) {
		_jsonParseFree(tls, **(**uintptr)(__ccgo_up(p + 8)))
		libc.Xmemmove(tls, p+8, p+8+1*4, uint32(libc.Int32FromInt32(JSON_CACHE_SIZE)-libc.Int32FromInt32(1))*libc.Uint32FromInt64(4))
		(*TJsonCache)(unsafe.Pointer(p)).FnUsed = libc.Int32FromInt32(JSON_CACHE_SIZE) - libc.Int32FromInt32(1)
	}
	(*TJsonParse)(unsafe.Pointer(pParse)).FeEdit = uint8(0)
	(*TJsonParse)(unsafe.Pointer(pParse)).FnJPRef = (*TJsonParse)(unsafe.Pointer(pParse)).FnJPRef + 1
	(*TJsonParse)(unsafe.Pointer(pParse)).FbReadOnly = uint8(1)
	**(**uintptr)(__ccgo_up(p + 8 + uintptr((*TJsonCache)(unsafe.Pointer(p)).FnUsed)*4)) = pParse
	(*TJsonCache)(unsafe.Pointer(p)).FnUsed = (*TJsonCache)(unsafe.Pointer(p)).FnUsed + 1
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Search for a cached translation the json text supplied by pArg.  Return
//	** the JsonParse object if found.  Return NULL if not found.
//	**
//	** When a match if found, the matching entry is moved to become the
//	** most-recently used entry if it isn't so already.
//	**
//	** The JsonParse object returned still belongs to the Cache and might
//	** be deleted at any moment.  If the caller wants the JsonParse to
//	** linger, it needs to increment the nPJRef reference counter.
//	*/
func _jsonCacheSearch(tls *libc.TLS, ctx uintptr, pArg uintptr) (r uintptr) {
	var i, nJson int32
	var p, tmp, zJson uintptr
	_, _, _, _, _ = i, nJson, p, tmp, zJson
	if Xsqlite3_value_type(tls, pArg) != int32(SQLITE_TEXT) {
		return uintptr(0)
	}
	zJson = Xsqlite3_value_text(tls, pArg)
	if zJson == uintptr(0) {
		return uintptr(0)
	}
	nJson = Xsqlite3_value_bytes(tls, pArg)
	p = Xsqlite3_get_auxdata(tls, ctx, -int32(429938))
	if p == uintptr(0) {
		return uintptr(0)
	}
	i = 0
	for {
		if !(i < (*TJsonCache)(unsafe.Pointer(p)).FnUsed) {
			break
		}
		if (*TJsonParse)(unsafe.Pointer(**(**uintptr)(__ccgo_up(p + 8 + uintptr(i)*4)))).FzJson == zJson {
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if i >= (*TJsonCache)(unsafe.Pointer(p)).FnUsed {
		i = 0
		for {
			if !(i < (*TJsonCache)(unsafe.Pointer(p)).FnUsed) {
				break
			}
			if (*TJsonParse)(unsafe.Pointer(**(**uintptr)(__ccgo_up(p + 8 + uintptr(i)*4)))).FnJson != nJson {
				goto _2
			}
			if libc.Xmemcmp(tls, (*TJsonParse)(unsafe.Pointer(**(**uintptr)(__ccgo_up(p + 8 + uintptr(i)*4)))).FzJson, zJson, uint32(nJson)) == 0 {
				break
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	if i < (*TJsonCache)(unsafe.Pointer(p)).FnUsed {
		if i < (*TJsonCache)(unsafe.Pointer(p)).FnUsed-int32(1) {
			/* Make the matching entry the most recently used entry */
			tmp = **(**uintptr)(__ccgo_up(p + 8 + uintptr(i)*4))
			libc.Xmemmove(tls, p+8+uintptr(i)*4, p+8+uintptr(i+int32(1))*4, uint32((*TJsonCache)(unsafe.Pointer(p)).FnUsed-i-libc.Int32FromInt32(1))*uint32(4))
			**(**uintptr)(__ccgo_up(p + 8 + uintptr((*TJsonCache)(unsafe.Pointer(p)).FnUsed-int32(1))*4)) = tmp
			i = (*TJsonCache)(unsafe.Pointer(p)).FnUsed - int32(1)
		}
		return **(**uintptr)(__ccgo_up(p + 8 + uintptr(i)*4))
	} else {
		return uintptr(0)
	}
	return r
}

/**************************************************************************
** Utility routines for dealing with JsonString objects
**************************************************************************/

// C documentation
//
//	/* This helper routine for jsonLookupStep() populates pIns with
//	** binary data that is to be inserted into pParse.
//	**
//	** In the common case, pIns just points to pParse->aIns and pParse->nIns.
//	** But if the zPath of the original edit operation includes path elements
//	** that go deeper, additional substructure must be created.
//	**
//	** For example:
//	**
//	**     json_insert('{}', '$.a.b.c', 123);
//	**
//	** The search stops at '$.a'  But additional substructure must be
//	** created for the ".b.c" part of the patch so that the final result
//	** is:  {"a":{"b":{"c"::123}}}.  This routine populates pIns with
//	** the binary equivalent of {"b":{"c":123}} so that it can be inserted.
//	**
//	** The caller is responsible for resetting pIns when it has finished
//	** using the substructure.
//	*/
func _jsonCreateEditSubstructure(tls *libc.TLS, pParse uintptr, pIns uintptr, zTail uintptr) (r Tu32) {
	var rc int32
	var v1 uintptr
	_, _ = rc, v1
	libc.Xmemset(tls, pIns, 0, uint32(56))
	(*TJsonParse)(unsafe.Pointer(pIns)).Fdb = (*TJsonParse)(unsafe.Pointer(pParse)).Fdb
	if int32(**(**int8)(__ccgo_up(zTail))) == 0 {
		/* No substructure.  Just insert what is given in pParse. */
		(*TJsonParse)(unsafe.Pointer(pIns)).FaBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FaIns
		(*TJsonParse)(unsafe.Pointer(pIns)).FnBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FnIns
		rc = 0
	} else {
		/* Construct the binary substructure */
		(*TJsonParse)(unsafe.Pointer(pIns)).FnBlob = uint32(1)
		(*TJsonParse)(unsafe.Pointer(pIns)).FaBlob = uintptr(unsafe.Pointer(&_emptyObject)) + libc.BoolUintptr(int32(**(**int8)(__ccgo_up(zTail))) == int32('.'))
		(*TJsonParse)(unsafe.Pointer(pIns)).FeEdit = (*TJsonParse)(unsafe.Pointer(pParse)).FeEdit
		(*TJsonParse)(unsafe.Pointer(pIns)).FnIns = (*TJsonParse)(unsafe.Pointer(pParse)).FnIns
		(*TJsonParse)(unsafe.Pointer(pIns)).FaIns = (*TJsonParse)(unsafe.Pointer(pParse)).FaIns
		(*TJsonParse)(unsafe.Pointer(pIns)).FiDepth = uint16(int32((*TJsonParse)(unsafe.Pointer(pParse)).FiDepth) + int32(1))
		if int32((*TJsonParse)(unsafe.Pointer(pIns)).FiDepth) >= int32(JSON_MAX_DEPTH) {
			return uint32(JSON_LOOKUP_TOODEEP)
		}
		rc = int32(_jsonLookupStep(tls, pIns, uint32(0), zTail, uint32(0)))
		(*TJsonParse)(unsafe.Pointer(pParse)).FiDepth = (*TJsonParse)(unsafe.Pointer(pParse)).FiDepth - 1
		v1 = pParse + 35
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | int32((*TJsonParse)(unsafe.Pointer(pIns)).Foom))
	}
	return uint32(rc) /* Error code only */
}

// C documentation
//
//	/* Return the value of a column */
func _jsonEachColumn(tls *libc.TLS, cur uintptr, ctx uintptr, iColumn int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var eType Tu8
	var i, i1, i2, j, n, n1 Tu32
	var nBase Tu64
	var p uintptr
	var _ /* x at bp+0 */ Ti64
	_, _, _, _, _, _, _, _, _ = eType, i, i1, i2, j, n, n1, nBase, p
	p = cur
	switch iColumn {
	case JEACH_KEY:
		if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent == uint32(0) {
			if (*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot == uint32(1) {
				break
			}
			j = uint32(_jsonEachPathLength(tls, p))
			n = (*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot - j
			if n == uint32(0) {
				break
			} else {
				if int32(**(**int8)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf + uintptr(j)))) == int32('[') {
					_sqlite3Atoi64(tls, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf+uintptr(j+uint32(1)), bp, int32(n-uint32(1)), uint8(SQLITE_UTF8))
					Xsqlite3_result_int64(tls, ctx, **(**Ti64)(__ccgo_up(bp)))
				} else {
					if int32(**(**int8)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf + uintptr(j+uint32(1))))) == int32('"') {
						Xsqlite3_result_text(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf+uintptr(j+uint32(2)), int32(n-uint32(3)), uintptr(-libc.Int32FromInt32(1)))
					} else {
						Xsqlite3_result_text(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf+uintptr(j+uint32(1)), int32(n-uint32(1)), uintptr(-libc.Int32FromInt32(1)))
					}
				}
			}
			break
		}
		if int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeType) == int32(JSONB_OBJECT) {
			_jsonReturnFromBlob(tls, p+168, (*TJsonEachCursor)(unsafe.Pointer(p)).Fi, ctx, int32(1))
		} else {
			Xsqlite3_result_int64(tls, ctx, (**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiKey)
		}
	case int32(JEACH_VALUE):
		i = uint32(_jsonSkipLabel(tls, p))
		_jsonReturnFromBlob(tls, p+168, i, ctx, int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeMode))
		if int32(**(**Tu8)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i))))&int32(0x0f) >= int32(JSONB_ARRAY) {
			Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
		}
	case int32(JEACH_TYPE):
		i1 = uint32(_jsonSkipLabel(tls, p))
		eType = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i1)))) & int32(0x0f))
		Xsqlite3_result_text(tls, ctx, _jsonbType[eType], -int32(1), libc.UintptrFromInt32(0))
	case int32(JEACH_ATOM):
		i2 = uint32(_jsonSkipLabel(tls, p))
		if int32(**(**Tu8)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i2))))&int32(0x0f) < int32(JSONB_ARRAY) {
			_jsonReturnFromBlob(tls, p+168, i2, ctx, int32(1))
		}
	case int32(JEACH_ID):
		Xsqlite3_result_int64(tls, ctx, int64((*TJsonEachCursor)(unsafe.Pointer(p)).Fi))
	case int32(JEACH_PARENT):
		if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent > uint32(0) && (*TJsonEachCursor)(unsafe.Pointer(p)).FbRecursive != 0 {
			Xsqlite3_result_int64(tls, ctx, int64((**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiHead))
		}
	case int32(JEACH_FULLKEY):
		nBase = (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed
		if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent != 0 {
			_jsonAppendPathName(tls, p)
		}
		Xsqlite3_result_text64(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed, uintptr(-libc.Int32FromInt32(1)), uint8(SQLITE_UTF8))
		(*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed = nBase
	case int32(JEACH_PATH):
		n1 = uint32(_jsonEachPathLength(tls, p))
		Xsqlite3_result_text64(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf, uint64(n1), uintptr(-libc.Int32FromInt32(1)), uint8(SQLITE_UTF8))
	default:
		Xsqlite3_result_text(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf, int32((*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot), libc.UintptrFromInt32(0))
	case int32(JEACH_JSON):
		if (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FzJson == uintptr(0) {
			Xsqlite3_result_blob(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob, int32((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FnBlob), uintptr(-libc.Int32FromInt32(1)))
		} else {
			Xsqlite3_result_text(tls, ctx, (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FzJson, -int32(1), uintptr(-libc.Int32FromInt32(1)))
		}
		break
	}
	return SQLITE_OK
}

// C documentation
//
//	/* Constructor for the json_each virtual table */
func _jsonEachConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) {
	var pNew uintptr
	var rc, v1 int32
	_, _, _ = pNew, rc, v1
	/* Column numbers */
	/* The xBestIndex method assumes that the JSON and ROOT columns are
	 ** the last two columns in the table.  Should this ever changes, be
	 ** sure to update the xBestIndex method. */
	_ = pzErr
	_ = argv
	_ = argc
	_ = pAux
	rc = Xsqlite3_declare_vtab(tls, db, __ccgo_ts+28099)
	if rc == SQLITE_OK {
		pNew = _sqlite3DbMallocZero(tls, db, uint64(20))
		**(**uintptr)(__ccgo_up(ppVtab)) = pNew
		if pNew == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_INNOCUOUS), 0)
		(*TJsonEachConnection)(unsafe.Pointer(pNew)).Fdb = db
		if int32(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(argv)) + 4))) == int32('b') {
			v1 = int32(2)
		} else {
			v1 = int32(1)
		}
		(*TJsonEachConnection)(unsafe.Pointer(pNew)).FeMode = uint8(v1)
		(*TJsonEachConnection)(unsafe.Pointer(pNew)).FbRecursive = libc.BoolUint8(int32(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(argv)) + uintptr(int32(4)+int32((*TJsonEachConnection)(unsafe.Pointer(pNew)).FeMode))))) == int32('t'))
	}
	return rc
}

// C documentation
//
//	/* Start a search on a new JSON string */
func _jsonEachFilter(tls *libc.TLS, cur uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, n, v1 Tu32
	var p, zRoot uintptr
	var v2 int32
	var _ /* sz at bp+0 */ Tu32
	_, _, _, _, _, _ = i, n, p, zRoot, v1, v2
	p = cur
	zRoot = uintptr(0)
	_ = idxStr
	_ = argc
	_jsonEachCursorReset(tls, p)
	if idxNum == 0 {
		return SQLITE_OK
	}
	libc.Xmemset(tls, p+168, 0, uint32(56))
	(*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FnJPRef = uint32(1)
	(*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.Fdb = (*TJsonEachCursor)(unsafe.Pointer(p)).Fdb
	if _jsonArgIsJsonb(tls, **(**uintptr)(__ccgo_up(argv)), p+168) != 0 {
		/* We have JSONB */
	} else {
		(*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FzJson = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		(*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FnJson = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
		if (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FzJson == uintptr(0) {
			v1 = libc.Uint32FromInt32(0)
			(*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd = v1
			(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = v1
			return SQLITE_OK
		}
		if _jsonConvertTextToBlob(tls, p+168, uintptr(0)) != 0 {
			if (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.Foom != 0 {
				return int32(SQLITE_NOMEM)
			}
			goto json_each_malformed_input
		}
	}
	if idxNum == int32(3) {
		zRoot = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
		if zRoot == uintptr(0) {
			return SQLITE_OK
		}
		if int32(**(**int8)(__ccgo_up(zRoot))) != int32('$') {
			Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg)
			(*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg = _jsonBadPathError(tls, uintptr(0), zRoot, 0)
			_jsonEachCursorReset(tls, p)
			if (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg != 0 {
				v2 = int32(SQLITE_ERROR)
			} else {
				v2 = int32(SQLITE_NOMEM)
			}
			return v2
		}
		(*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot = uint32(_sqlite3Strlen30(tls, zRoot))
		if int32(**(**int8)(__ccgo_up(zRoot + 1))) == 0 {
			v1 = libc.Uint32FromInt32(0)
			(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = v1
			i = v1
			(*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(0)
		} else {
			i = _jsonLookupStep(tls, p+168, uint32(0), zRoot+uintptr(1), uint32(0))
			if i >= uint32(JSON_LOOKUP_PATHERROR) {
				if i == uint32(JSON_LOOKUP_NOTFOUND) {
					(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = uint32(0)
					(*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(0)
					(*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd = uint32(0)
					return SQLITE_OK
				}
				Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg)
				(*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg = _jsonBadPathError(tls, uintptr(0), zRoot, 0)
				_jsonEachCursorReset(tls, p)
				if (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg != 0 {
					v2 = int32(SQLITE_ERROR)
				} else {
					v2 = int32(SQLITE_NOMEM)
				}
				return v2
			}
			if (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FiLabel != 0 {
				(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = (*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FiLabel
				(*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(JSONB_OBJECT)
			} else {
				(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i
				(*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(JSONB_ARRAY)
			}
		}
		_jsonAppendRaw(tls, p+40, zRoot, (*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot)
	} else {
		v1 = libc.Uint32FromInt32(0)
		(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = v1
		i = v1
		(*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(0)
		(*TJsonEachCursor)(unsafe.Pointer(p)).FnRoot = uint32(1)
		_jsonAppendRaw(tls, p+40, __ccgo_ts+28203, uint32(1))
	}
	(*TJsonEachCursor)(unsafe.Pointer(p)).FnParent = uint32(0)
	n = _jsonbPayloadSize(tls, p+168, i, bp)
	(*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd = i + n + **(**Tu32)(__ccgo_up(bp))
	if int32(**(**Tu8)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i))))&int32(0x0f) >= int32(JSONB_ARRAY) && !((*TJsonEachCursor)(unsafe.Pointer(p)).FbRecursive != 0) {
		(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i + n
		(*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i)))) & int32(0x0f))
		(*TJsonEachCursor)(unsafe.Pointer(p)).FaParent = _sqlite3DbMallocZero(tls, (*TJsonEachCursor)(unsafe.Pointer(p)).Fdb, uint64(24))
		if (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		(*TJsonEachCursor)(unsafe.Pointer(p)).FnParent = uint32(1)
		(*TJsonEachCursor)(unsafe.Pointer(p)).FnParentAlloc = uint32(1)
		(**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent))).FiKey = 0
		(**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent))).FiEnd = (*TJsonEachCursor)(unsafe.Pointer(p)).FiEnd
		(**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent))).FiHead = (*TJsonEachCursor)(unsafe.Pointer(p)).Fi
		(**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent))).FiValue = i
	}
	return SQLITE_OK
	goto json_each_malformed_input
json_each_malformed_input:
	;
	Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg)
	(*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+27699, 0)
	_jsonEachCursorReset(tls, p)
	if (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab)).FzErrMsg != 0 {
		v2 = int32(SQLITE_ERROR)
	} else {
		v2 = int32(SQLITE_NOMEM)
	}
	return v2
}

// C documentation
//
//	/* Advance the cursor to the next element for json_tree() */
func _jsonEachNext(tls *libc.TLS, cur uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, i1, iVal, n, n1 Tu32
	var levelChange, x Tu8
	var nNew Tu64
	var p, pNew, pParent, pParent1 uintptr
	var rc int32
	var _ /* sz at bp+0 */ Tu32
	var _ /* sz at bp+4 */ Tu32
	_, _, _, _, _, _, _, _, _, _, _, _, _ = i, i1, iVal, levelChange, n, n1, nNew, p, pNew, pParent, pParent1, rc, x
	p = cur
	rc = SQLITE_OK
	if (*TJsonEachCursor)(unsafe.Pointer(p)).FbRecursive != 0 {
		levelChange = uint8(0)
		**(**Tu32)(__ccgo_up(bp)) = uint32(0)
		i = uint32(_jsonSkipLabel(tls, p))
		x = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(i)))) & int32(0x0f))
		n = _jsonbPayloadSize(tls, p+168, i, bp)
		if int32(x) == int32(JSONB_OBJECT) || int32(x) == int32(JSONB_ARRAY) {
			if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent >= (*TJsonEachCursor)(unsafe.Pointer(p)).FnParentAlloc {
				nNew = uint64((*TJsonEachCursor)(unsafe.Pointer(p)).FnParentAlloc*uint32(2) + uint32(3))
				pNew = _sqlite3DbRealloc(tls, (*TJsonEachCursor)(unsafe.Pointer(p)).Fdb, (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent, uint64(24)*nNew)
				if pNew == uintptr(0) {
					return int32(SQLITE_NOMEM)
				}
				(*TJsonEachCursor)(unsafe.Pointer(p)).FnParentAlloc = uint32(nNew)
				(*TJsonEachCursor)(unsafe.Pointer(p)).FaParent = pNew
			}
			levelChange = uint8(1)
			pParent = (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent)*24
			(*TJsonParent)(unsafe.Pointer(pParent)).FiHead = (*TJsonEachCursor)(unsafe.Pointer(p)).Fi
			(*TJsonParent)(unsafe.Pointer(pParent)).FiValue = i
			(*TJsonParent)(unsafe.Pointer(pParent)).FiEnd = i + n + **(**Tu32)(__ccgo_up(bp))
			(*TJsonParent)(unsafe.Pointer(pParent)).FiKey = int64(-int32(1))
			(*TJsonParent)(unsafe.Pointer(pParent)).FnPath = uint32((*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed)
			if (*TJsonEachCursor)(unsafe.Pointer(p)).FeType != 0 && (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent != 0 {
				_jsonAppendPathName(tls, p)
				if (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FeErr != 0 {
					rc = int32(SQLITE_NOMEM)
				}
			}
			(*TJsonEachCursor)(unsafe.Pointer(p)).FnParent = (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent + 1
			(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i + n
		} else {
			(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i + n + **(**Tu32)(__ccgo_up(bp))
		}
		for (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent > uint32(0) && (*TJsonEachCursor)(unsafe.Pointer(p)).Fi >= (**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiEnd {
			(*TJsonEachCursor)(unsafe.Pointer(p)).FnParent = (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent - 1
			(*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed = uint64((**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent)*24))).FnPath)
			levelChange = uint8(1)
		}
		if levelChange != 0 {
			if (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent > uint32(0) {
				pParent1 = (*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24
				iVal = (*TJsonParent)(unsafe.Pointer(pParent1)).FiValue
				(*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FsParse.FaBlob + uintptr(iVal)))) & int32(0x0f))
			} else {
				(*TJsonEachCursor)(unsafe.Pointer(p)).FeType = uint8(0)
			}
		}
	} else {
		**(**Tu32)(__ccgo_up(bp + 4)) = uint32(0)
		i1 = uint32(_jsonSkipLabel(tls, p))
		n1 = _jsonbPayloadSize(tls, p+168, i1, bp+4)
		(*TJsonEachCursor)(unsafe.Pointer(p)).Fi = i1 + n1 + **(**Tu32)(__ccgo_up(bp + 4))
	}
	if int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeType) == int32(JSONB_ARRAY) && (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent != 0 {
		(**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiKey = (**(**TJsonParent)(__ccgo_up((*TJsonEachCursor)(unsafe.Pointer(p)).FaParent + uintptr((*TJsonEachCursor)(unsafe.Pointer(p)).FnParent-uint32(1))*24))).FiKey + 1
	}
	(*TJsonEachCursor)(unsafe.Pointer(p)).FiRowid = (*TJsonEachCursor)(unsafe.Pointer(p)).FiRowid + 1
	return rc
}

// C documentation
//
//	/* Length of the path for rowid==0 in bRecursive mode.
//	*/
func _jsonEachPathLength(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var cSaved int8
	var n, x Tu32
	var z uintptr
	var _ /* sz at bp+0 */ Tu32
	_, _, _, _ = cSaved, n, x, z
	n = uint32((*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FnUsed)
	z = (*TJsonEachCursor)(unsafe.Pointer(p)).Fpath.FzBuf
	if (*TJsonEachCursor)(unsafe.Pointer(p)).FiRowid == uint32(0) && (*TJsonEachCursor)(unsafe.Pointer(p)).FbRecursive != 0 && n >= uint32(2) {
		for n > uint32(1) {
			n = n - 1
			if int32(**(**int8)(__ccgo_up(z + uintptr(n)))) == int32('[') || int32(**(**int8)(__ccgo_up(z + uintptr(n)))) == int32('.') {
				**(**Tu32)(__ccgo_up(bp)) = uint32(0)
				cSaved = **(**int8)(__ccgo_up(z + uintptr(n)))
				**(**int8)(__ccgo_up(z + uintptr(n))) = 0
				x = _jsonLookupStep(tls, p+168, uint32(0), z+uintptr(1), uint32(0))
				**(**int8)(__ccgo_up(z + uintptr(n))) = cSaved
				if x >= uint32(JSON_LOOKUP_PATHERROR) {
					continue
				}
				if x+_jsonbPayloadSize(tls, p+168, x, bp) == (*TJsonEachCursor)(unsafe.Pointer(p)).Fi {
					break
				}
			}
		}
	}
	return int32(n)
}

// C documentation
//
//	/*
//	** json_error_position(JSON)
//	**
//	** If the argument is NULL, return NULL
//	**
//	** If the argument is BLOB, do a full validity check and return non-zero
//	** if the check fails.  The return value is the approximate 1-based offset
//	** to the byte of the element that contains the first error.
//	**
//	** Otherwise interpret the argument is TEXT (even if it is numeric) and
//	** return the 1-based character position for where the parser first recognized
//	** that the input was not valid JSON, or return 0 if the input text looks
//	** ok.  JSON-5 extensions are accepted.
//	*/
func _jsonErrorFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var iErrPos Ti64
	var k Tu32
	var _ /* s at bp+0 */ TJsonParse
	_, _ = iErrPos, k
	iErrPos = 0
	_ = argc
	libc.Xmemset(tls, bp, 0, uint32(56))
	(**(**TJsonParse)(__ccgo_up(bp))).Fdb = Xsqlite3_context_db_handle(tls, ctx)
	if _jsonArgIsJsonb(tls, **(**uintptr)(__ccgo_up(argv)), bp) != 0 {
		iErrPos = int64(_jsonbValidityCheck(tls, bp, uint32(0), (**(**TJsonParse)(__ccgo_up(bp))).FnBlob, uint32(1)))
	} else {
		(**(**TJsonParse)(__ccgo_up(bp))).FzJson = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		if (**(**TJsonParse)(__ccgo_up(bp))).FzJson == uintptr(0) {
			return
		} /* NULL input or OOM */
		(**(**TJsonParse)(__ccgo_up(bp))).FnJson = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
		if _jsonConvertTextToBlob(tls, bp, uintptr(0)) != 0 {
			if (**(**TJsonParse)(__ccgo_up(bp))).Foom != 0 {
				iErrPos = int64(-int32(1))
			} else {
				/* Because s.oom is false */
				k = uint32(0)
				for {
					if !(k < (**(**TJsonParse)(__ccgo_up(bp))).FiErr && **(**int8)(__ccgo_up((**(**TJsonParse)(__ccgo_up(bp))).FzJson + uintptr(k))) != 0) {
						break
					}
					if int32(**(**int8)(__ccgo_up((**(**TJsonParse)(__ccgo_up(bp))).FzJson + uintptr(k))))&int32(0xc0) != int32(0x80) {
						iErrPos = iErrPos + 1
					}
					goto _1
				_1:
					;
					k = k + 1
				}
				iErrPos = iErrPos + 1
			}
		}
	}
	_jsonParseReset(tls, bp)
	if iErrPos < 0 {
		Xsqlite3_result_error_nomem(tls, ctx)
	} else {
		Xsqlite3_result_int64(tls, ctx, iErrPos)
	}
}

// C documentation
//
//	/*
//	** json_extract(JSON, PATH, ...)
//	** "->"(JSON,PATH)
//	** "->>"(JSON,PATH)
//	**
//	** Return the element described by PATH.  Return NULL if that PATH element
//	** is not found.
//	**
//	** If JSON_JSON is set or if more that one PATH argument is supplied then
//	** always return a JSON representation of the result.  If JSON_SQL is set,
//	** then always return an SQL representation of the result.  If neither flag
//	** is present and argc==2, then return JSON for objects and arrays and SQL
//	** for all other values.
//	**
//	** When multiple PATH arguments are supplied, the result is a JSON array
//	** containing the result of each PATH.
//	**
//	** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
//	** compatibility with PG.
//	*/
func _jsonExtractFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var flags, i, nPath int32
	var j Tu32
	var p, zPath uintptr
	var _ /* jx at bp+0 */ TJsonString
	_, _, _, _, _, _ = flags, i, j, nPath, p, zPath
	p = uintptr(0) /* String for array result */
	if argc < int32(2) {
		return
	}
	p = _jsonParseFuncArg(tls, ctx, **(**uintptr)(__ccgo_up(argv)), uint32(0))
	if p == uintptr(0) {
		return
	}
	flags = int32(Xsqlite3_user_data(tls, ctx))
	_jsonStringInit(tls, bp, ctx)
	if argc > int32(2) {
		_jsonAppendChar(tls, bp, int8('['))
	}
	i = int32(1)
	for {
		if !(i < argc) {
			break
		}
		/* With a single PATH argument */
		zPath = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
		if zPath == uintptr(0) {
			goto json_extract_error
		}
		nPath = _sqlite3Strlen30(tls, zPath)
		if int32(**(**int8)(__ccgo_up(zPath))) == int32('$') {
			j = _jsonLookupStep(tls, p, uint32(0), zPath+uintptr(1), uint32(0))
		} else {
			if flags&int32(JSON_ABPATH) != 0 {
				/* The -> and ->> operators accept abbreviated PATH arguments.  This
				 ** is mostly for compatibility with PostgreSQL, but also for
				 ** convenience.
				 **
				 **     NUMBER   ==>  $[NUMBER]     // PG compatible
				 **     LABEL    ==>  $.LABEL       // PG compatible
				 **     [NUMBER] ==>  $[NUMBER]     // Not PG.  Purely for convenience
				 **
				 ** Updated 2024-05-27:  If the NUMBER is negative, then PG counts from
				 ** the right of the array.  Hence for negative NUMBER:
				 **
				 **     NUMBER   ==>  $[#NUMBER]    // PG compatible
				 */
				_jsonStringInit(tls, bp, ctx)
				if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4))) == int32(SQLITE_INTEGER) {
					_jsonAppendRawNZ(tls, bp, __ccgo_ts+27911, uint32(1))
					if int32(**(**int8)(__ccgo_up(zPath))) == int32('-') {
						_jsonAppendRawNZ(tls, bp, __ccgo_ts+27913, uint32(1))
					}
					_jsonAppendRaw(tls, bp, zPath, uint32(nPath))
					_jsonAppendRawNZ(tls, bp, __ccgo_ts+6529, uint32(2))
				} else {
					if _jsonAllAlphanum(tls, zPath, nPath) != 0 {
						_jsonAppendRawNZ(tls, bp, __ccgo_ts+1750, uint32(1))
						_jsonAppendRaw(tls, bp, zPath, uint32(nPath))
					} else {
						if int32(**(**int8)(__ccgo_up(zPath))) == int32('[') && nPath >= int32(3) && int32(**(**int8)(__ccgo_up(zPath + uintptr(nPath-int32(1))))) == int32(']') {
							_jsonAppendRaw(tls, bp, zPath, uint32(nPath))
						} else {
							_jsonAppendRawNZ(tls, bp, __ccgo_ts+27915, uint32(2))
							_jsonAppendRaw(tls, bp, zPath, uint32(nPath))
							_jsonAppendRawNZ(tls, bp, __ccgo_ts+27918, uint32(1))
						}
					}
				}
				_jsonStringTerminate(tls, bp)
				j = _jsonLookupStep(tls, p, uint32(0), (**(**TJsonString)(__ccgo_up(bp))).FzBuf, uint32(0))
				_jsonStringReset(tls, bp)
			} else {
				_jsonBadPathError(tls, ctx, zPath, 0)
				goto json_extract_error
			}
		}
		if j < (*TJsonParse)(unsafe.Pointer(p)).FnBlob {
			if argc == int32(2) {
				if flags&int32(JSON_JSON) != 0 {
					_jsonStringInit(tls, bp, ctx)
					_jsonTranslateBlobToText(tls, p, j, bp)
					_jsonReturnString(tls, bp, uintptr(0), uintptr(0))
					_jsonStringReset(tls, bp)
					Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
				} else {
					_jsonReturnFromBlob(tls, p, j, ctx, 0)
					if flags&(libc.Int32FromInt32(JSON_SQL)|libc.Int32FromInt32(JSON_BLOB)) == 0 && int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(p)).FaBlob + uintptr(j))))&int32(0x0f) >= int32(JSONB_ARRAY) {
						Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
					}
				}
			} else {
				_jsonAppendSeparator(tls, bp)
				_jsonTranslateBlobToText(tls, p, j, bp)
			}
		} else {
			if j == uint32(JSON_LOOKUP_NOTFOUND) {
				if argc == int32(2) {
					goto json_extract_error /* Return NULL if not found */
				} else {
					_jsonAppendSeparator(tls, bp)
					_jsonAppendRawNZ(tls, bp, __ccgo_ts+1697, uint32(4))
				}
			} else {
				_jsonBadPathError(tls, ctx, zPath, int32(j))
				goto json_extract_error
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if argc > int32(2) {
		_jsonAppendChar(tls, bp, int8(']'))
		_jsonReturnString(tls, bp, uintptr(0), uintptr(0))
		if flags&int32(JSON_BLOB) == 0 {
			Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
		}
	}
	goto json_extract_error
json_extract_error:
	;
	_jsonStringReset(tls, bp)
	_jsonParseFree(tls, p)
	return
}

/*
** Return codes for jsonMergePatch()
 */

// C documentation
//
//	/*
//	** pArg is a function argument that might be an SQL value or a JSON
//	** value.  Figure out what it is and encode it as a JSONB blob.
//	** Return the results in pParse.
//	**
//	** pParse is uninitialized upon entry.  This routine will handle the
//	** initialization of pParse.  The result will be contained in
//	** pParse->aBlob and pParse->nBlob.  pParse->aBlob might be dynamically
//	** allocated (if pParse->nBlobAlloc is greater than zero) in which case
//	** the caller is responsible for freeing the space allocated to pParse->aBlob
//	** when it has finished with it.  Or pParse->aBlob might be a static string
//	** or a value obtained from sqlite3_value_blob(pArg).
//	**
//	** If the argument is a BLOB that is clearly not a JSONB, then this
//	** function might set an error message in ctx and return non-zero.
//	** It might also set an error message and return non-zero on an OOM error.
//	*/
func _jsonFunctionArgToBlob(tls *libc.TLS, ctx uintptr, pArg uintptr, pParse uintptr) (r1 int32) {
	var eType, n, n1, nJson int32
	var r float64
	var z, z1, zJson uintptr
	_, _, _, _, _, _, _, _ = eType, n, n1, nJson, r, z, z1, zJson
	eType = Xsqlite3_value_type(tls, pArg)
	libc.Xmemset(tls, pParse, 0, uint32(56))
	(*TJsonParse)(unsafe.Pointer(pParse)).Fdb = Xsqlite3_context_db_handle(tls, ctx)
	switch eType {
	default:
		(*TJsonParse)(unsafe.Pointer(pParse)).FaBlob = uintptr(unsafe.Pointer(&_aNull))
		(*TJsonParse)(unsafe.Pointer(pParse)).FnBlob = uint32(1)
		return 0
	case int32(SQLITE_BLOB):
		if !(_jsonArgIsJsonb(tls, pArg, pParse) != 0) {
			Xsqlite3_result_error(tls, ctx, __ccgo_ts+27670, -int32(1))
			return int32(1)
		}
	case int32(SQLITE_TEXT):
		zJson = Xsqlite3_value_text(tls, pArg)
		nJson = Xsqlite3_value_bytes(tls, pArg)
		if zJson == uintptr(0) {
			return int32(1)
		}
		if Xsqlite3_value_subtype(tls, pArg) == uint32(JSON_SUBTYPE) {
			(*TJsonParse)(unsafe.Pointer(pParse)).FzJson = zJson
			(*TJsonParse)(unsafe.Pointer(pParse)).FnJson = nJson
			if _jsonConvertTextToBlob(tls, pParse, ctx) != 0 {
				Xsqlite3_result_error(tls, ctx, __ccgo_ts+27699, -int32(1))
				_sqlite3DbFree(tls, (*TJsonParse)(unsafe.Pointer(pParse)).Fdb, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob)
				libc.Xmemset(tls, pParse, 0, uint32(56))
				return int32(1)
			}
		} else {
			_jsonBlobAppendNode(tls, pParse, uint8(JSONB_TEXTRAW), uint64(nJson), zJson)
		}
	case int32(SQLITE_FLOAT):
		r = Xsqlite3_value_double(tls, pArg)
		if _sqlite3IsNaN(tls, r) != 0 {
			_jsonBlobAppendNode(tls, pParse, uint8(JSONB_NULL), uint64(0), uintptr(0))
		} else {
			n = Xsqlite3_value_bytes(tls, pArg)
			z = Xsqlite3_value_text(tls, pArg)
			if z == uintptr(0) {
				return int32(1)
			}
			if int32(**(**int8)(__ccgo_up(z))) == int32('I') {
				_jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint64(5), __ccgo_ts+27804)
			} else {
				if int32(**(**int8)(__ccgo_up(z))) == int32('-') && int32(**(**int8)(__ccgo_up(z + 1))) == int32('I') {
					_jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint64(6), __ccgo_ts+27797)
				} else {
					_jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint64(n), z)
				}
			}
		}
	case int32(SQLITE_INTEGER):
		n1 = Xsqlite3_value_bytes(tls, pArg)
		z1 = Xsqlite3_value_text(tls, pArg)
		if z1 == uintptr(0) {
			return int32(1)
		}
		_jsonBlobAppendNode(tls, pParse, uint8(JSONB_INT), uint64(n1), z1)
		break
	}
	if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 {
		Xsqlite3_result_error_nomem(tls, ctx)
		return int32(1)
	} else {
		return 0
	}
	return r1
}

// C documentation
//
//	/* argv[0] is a BLOB that seems likely to be a JSONB.  Subsequent
//	** arguments come in pairs where each pair contains a JSON path and
//	** content to insert or set at that patch.  Do the updates
//	** and return the result.
//	**
//	** The specific operation is determined by eEdit, which can be one
//	** of JEDIT_INS, JEDIT_REPL, JEDIT_SET, or JEDIT_AINS.
//	*/
func _jsonInsertIntoBlob(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr, eEdit int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var flgs, i, v1 int32
	var p, zPath uintptr
	var rc Tu32
	var _ /* ax at bp+0 */ TJsonParse
	_, _, _, _, _, _ = flgs, i, p, rc, zPath, v1
	rc = uint32(0)
	zPath = uintptr(0)
	if argc == int32(1) {
		v1 = 0
	} else {
		v1 = int32(JSON_EDITABLE)
	}
	flgs = v1
	p = _jsonParseFuncArg(tls, ctx, **(**uintptr)(__ccgo_up(argv)), uint32(flgs))
	if p == uintptr(0) {
		return
	}
	i = int32(1)
	for {
		if !(i < argc-int32(1)) {
			break
		}
		if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4))) == int32(SQLITE_NULL) {
			goto _2
		}
		zPath = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
		if zPath == uintptr(0) {
			Xsqlite3_result_error_nomem(tls, ctx)
			_jsonParseFree(tls, p)
			return
		}
		if int32(**(**int8)(__ccgo_up(zPath))) != int32('$') {
			goto jsonInsertIntoBlob_patherror
		}
		if _jsonFunctionArgToBlob(tls, ctx, **(**uintptr)(__ccgo_up(argv + uintptr(i+int32(1))*4)), bp) != 0 {
			_jsonParseReset(tls, bp)
			_jsonParseFree(tls, p)
			return
		}
		if int32(**(**int8)(__ccgo_up(zPath + 1))) == 0 {
			if eEdit == int32(JEDIT_REPL) || eEdit == int32(JEDIT_SET) {
				_jsonBlobEdit(tls, p, uint32(0), (*TJsonParse)(unsafe.Pointer(p)).FnBlob, (**(**TJsonParse)(__ccgo_up(bp))).FaBlob, (**(**TJsonParse)(__ccgo_up(bp))).FnBlob)
			}
			rc = uint32(0)
		} else {
			(*TJsonParse)(unsafe.Pointer(p)).FeEdit = uint8(eEdit)
			(*TJsonParse)(unsafe.Pointer(p)).FnIns = (**(**TJsonParse)(__ccgo_up(bp))).FnBlob
			(*TJsonParse)(unsafe.Pointer(p)).FaIns = (**(**TJsonParse)(__ccgo_up(bp))).FaBlob
			(*TJsonParse)(unsafe.Pointer(p)).Fdelta = 0
			(*TJsonParse)(unsafe.Pointer(p)).FiDepth = uint16(0)
			rc = _jsonLookupStep(tls, p, uint32(0), zPath+uintptr(1), uint32(0))
		}
		_jsonParseReset(tls, bp)
		if rc == uint32(JSON_LOOKUP_NOTFOUND) {
			goto _2
		}
		if rc >= uint32(JSON_LOOKUP_PATHERROR) {
			goto jsonInsertIntoBlob_patherror
		}
		goto _2
	_2:
		;
		i = i + int32(2)
	}
	_jsonReturnParse(tls, ctx, p)
	_jsonParseFree(tls, p)
	return
	goto jsonInsertIntoBlob_patherror
jsonInsertIntoBlob_patherror:
	;
	_jsonParseFree(tls, p)
	_jsonBadPathError(tls, ctx, zPath, int32(rc))
	return
}

// C documentation
//
//	/*
//	** Search along zPath to find the Json element specified.  Return an
//	** index into pParse->aBlob[] for the start of that element's value.
//	**
//	** If the value found by this routine is the value half of label/value pair
//	** within an object, then set pPath->iLabel to the start of the corresponding
//	** label, before returning.
//	**
//	** Return one of the JSON_LOOKUP error codes if problems are seen.
//	**
//	** This routine will also modify the blob.  If pParse->eEdit is one of
//	** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, JEDIT_SET, or JEDIT_AINS, then changes
//	** might be made to the selected value. If an edit is performed, then the
//	** return value does not necessarily point to the select element. If an edit
//	** is performed, the return value is only useful for detecting error
//	** conditions.
//	*/
func _jsonLookupStep(tls *libc.TLS, pParse uintptr, iRoot Tu32, zPath uintptr, iLabel Tu32) (r Tu32) {
	bp := tls.Alloc(176)
	defer tls.Free(176)
	var i, iEnd, j, k, n, nIns, nKey, rc, v Tu32
	var kk, nn Tu64
	var rawKey, rawLabel, v5 int32
	var x Tu8
	var zKey, zLabel, v4 uintptr
	var v3 Tu16
	var _ /* ix at bp+60 */ TJsonParse
	var _ /* sz at bp+0 */ Tu32
	var _ /* v at bp+116 */ TJsonParse
	var _ /* v at bp+4 */ TJsonParse
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iEnd, j, k, kk, n, nIns, nKey, nn, rawKey, rawLabel, rc, v, x, zKey, zLabel, v3, v4, v5
	if int32(**(**int8)(__ccgo_up(zPath))) == 0 {
		if (*TJsonParse)(unsafe.Pointer(pParse)).FeEdit != 0 && _jsonBlobMakeEditable(tls, pParse, (*TJsonParse)(unsafe.Pointer(pParse)).FnIns) != 0 {
			n = _jsonbPayloadSize(tls, pParse, iRoot, bp)
			**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + n
			if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) == int32(JEDIT_DEL) {
				if iLabel > uint32(0) {
					**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + (iRoot - iLabel)
					iRoot = iLabel
				}
				_jsonBlobEdit(tls, pParse, iRoot, **(**Tu32)(__ccgo_up(bp)), uintptr(0), uint32(0))
			} else {
				if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) == int32(JEDIT_INS) {
					/* Already exists, so json_insert() is a no-op */
				} else {
					if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) == int32(JEDIT_AINS) {
						/* json_array_insert() */
						if int32(**(**int8)(__ccgo_up(zPath + uintptr(-libc.Int32FromInt32(1))))) != int32(']') {
							return uint32(JSON_LOOKUP_NOTARRAY)
						} else {
							_jsonBlobEdit(tls, pParse, iRoot, uint32(0), (*TJsonParse)(unsafe.Pointer(pParse)).FaIns, (*TJsonParse)(unsafe.Pointer(pParse)).FnIns)
						}
					} else {
						/* json_set() or json_replace() */
						_jsonBlobEdit(tls, pParse, iRoot, **(**Tu32)(__ccgo_up(bp)), (*TJsonParse)(unsafe.Pointer(pParse)).FaIns, (*TJsonParse)(unsafe.Pointer(pParse)).FnIns)
					}
				}
			}
		}
		(*TJsonParse)(unsafe.Pointer(pParse)).FiLabel = iLabel
		return iRoot
	}
	if int32(**(**int8)(__ccgo_up(zPath))) == int32('.') {
		rawKey = int32(1)
		x = **(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(iRoot)))
		zPath = zPath + 1
		if int32(**(**int8)(__ccgo_up(zPath))) == int32('"') {
			zKey = zPath + uintptr(1)
			i = uint32(1)
			for {
				if !(**(**int8)(__ccgo_up(zPath + uintptr(i))) != 0 && int32(**(**int8)(__ccgo_up(zPath + uintptr(i)))) != int32('"')) {
					break
				}
				if int32(**(**int8)(__ccgo_up(zPath + uintptr(i)))) == int32('\\') && int32(**(**int8)(__ccgo_up(zPath + uintptr(i+uint32(1))))) != 0 {
					i = i + 1
				}
				goto _1
			_1:
				;
				i = i + 1
			}
			nKey = i - uint32(1)
			if **(**int8)(__ccgo_up(zPath + uintptr(i))) != 0 {
				i = i + 1
			} else {
				return uint32(JSON_LOOKUP_PATHERROR)
			}
			rawKey = libc.BoolInt32(libc.Xmemchr(tls, zKey, int32('\\'), nKey) == uintptr(0))
		} else {
			zKey = zPath
			i = uint32(0)
			for {
				if !(**(**int8)(__ccgo_up(zPath + uintptr(i))) != 0 && int32(**(**int8)(__ccgo_up(zPath + uintptr(i)))) != int32('.') && int32(**(**int8)(__ccgo_up(zPath + uintptr(i)))) != int32('[')) {
					break
				}
				goto _2
			_2:
				;
				i = i + 1
			}
			nKey = i
			if nKey == uint32(0) {
				return uint32(JSON_LOOKUP_PATHERROR)
			}
		}
		if int32(x)&int32(0x0f) != int32(JSONB_OBJECT) {
			return uint32(JSON_LOOKUP_NOTFOUND)
		}
		n = _jsonbPayloadSize(tls, pParse, iRoot, bp)
		j = iRoot + n /* j is the index of a label */
		iEnd = j + **(**Tu32)(__ccgo_up(bp))
		for j < iEnd {
			x = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(j)))) & int32(0x0f))
			if int32(x) < int32(JSONB_TEXT) || int32(x) > int32(JSONB_TEXTRAW) {
				return uint32(JSON_LOOKUP_ERROR)
			}
			n = _jsonbPayloadSize(tls, pParse, j, bp)
			if n == uint32(0) {
				return uint32(JSON_LOOKUP_ERROR)
			}
			k = j + n /* k is the index of the label text */
			if k+**(**Tu32)(__ccgo_up(bp)) >= iEnd {
				return uint32(JSON_LOOKUP_ERROR)
			}
			zLabel = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(k)
			rawLabel = libc.BoolInt32(int32(x) == int32(JSONB_TEXT) || int32(x) == int32(JSONB_TEXTRAW))
			if _jsonLabelCompare(tls, zKey, nKey, rawKey, zLabel, **(**Tu32)(__ccgo_up(bp)), rawLabel) != 0 {
				v = k + **(**Tu32)(__ccgo_up(bp)) /* v is the index of the value */
				if int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(v))))&int32(0x0f) > int32(JSONB_OBJECT) {
					return uint32(JSON_LOOKUP_ERROR)
				}
				n = _jsonbPayloadSize(tls, pParse, v, bp)
				if n == uint32(0) || v+n+**(**Tu32)(__ccgo_up(bp)) > iEnd {
					return uint32(JSON_LOOKUP_ERROR)
				}
				v4 = pParse + 32
				*(*Tu16)(unsafe.Pointer(v4)) = *(*Tu16)(unsafe.Pointer(v4)) + 1
				v3 = *(*Tu16)(unsafe.Pointer(v4))
				if int32(v3) >= int32(JSON_MAX_DEPTH) {
					return uint32(JSON_LOOKUP_TOODEEP)
				}
				rc = _jsonLookupStep(tls, pParse, v, zPath+uintptr(i), j)
				(*TJsonParse)(unsafe.Pointer(pParse)).FiDepth = (*TJsonParse)(unsafe.Pointer(pParse)).FiDepth - 1
				if (*TJsonParse)(unsafe.Pointer(pParse)).Fdelta != 0 {
					_jsonAfterEditSizeAdjust(tls, pParse, iRoot)
				}
				return rc
			}
			j = k + **(**Tu32)(__ccgo_up(bp))
			if int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(j))))&int32(0x0f) > int32(JSONB_OBJECT) {
				return uint32(JSON_LOOKUP_ERROR)
			}
			n = _jsonbPayloadSize(tls, pParse, j, bp)
			if n == uint32(0) {
				return uint32(JSON_LOOKUP_ERROR)
			}
			j = j + (n + **(**Tu32)(__ccgo_up(bp)))
		}
		if j > iEnd {
			return uint32(JSON_LOOKUP_ERROR)
		}
		if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) >= int32(JEDIT_INS) { /* Header of the label to be inserted */
			if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) == int32(JEDIT_AINS) && Xsqlite3_strglob(tls, __ccgo_ts+27844, zPath+uintptr(i)) != 0 {
				return uint32(JSON_LOOKUP_NOTARRAY)
			}
			libc.Xmemset(tls, bp+60, 0, uint32(56))
			(**(**TJsonParse)(__ccgo_up(bp + 60))).Fdb = (*TJsonParse)(unsafe.Pointer(pParse)).Fdb
			if rawKey != 0 {
				v5 = int32(JSONB_TEXTRAW)
			} else {
				v5 = int32(JSONB_TEXT5)
			}
			_jsonBlobAppendNode(tls, bp+60, uint8(v5), uint64(nKey), uintptr(0))
			v4 = pParse + 35
			*(*Tu8)(unsafe.Pointer(v4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v4))) | int32((**(**TJsonParse)(__ccgo_up(bp + 60))).Foom))
			rc = _jsonCreateEditSubstructure(tls, pParse, bp+4, zPath+uintptr(i))
			if !(rc >= libc.Uint32FromUint32(JSON_LOOKUP_PATHERROR)) && _jsonBlobMakeEditable(tls, pParse, (**(**TJsonParse)(__ccgo_up(bp + 60))).FnBlob+nKey+(**(**TJsonParse)(__ccgo_up(bp + 4))).FnBlob) != 0 {
				nIns = (**(**TJsonParse)(__ccgo_up(bp + 60))).FnBlob + nKey + (**(**TJsonParse)(__ccgo_up(bp + 4))).FnBlob
				_jsonBlobEdit(tls, pParse, j, uint32(0), uintptr(0), nIns)
				if !((*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0) {
					/* Because pParse->oom!=0 */
					/* Because pPasre->oom!=0 */
					libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(j), (**(**TJsonParse)(__ccgo_up(bp + 60))).FaBlob, (**(**TJsonParse)(__ccgo_up(bp + 60))).FnBlob)
					k = j + (**(**TJsonParse)(__ccgo_up(bp + 60))).FnBlob
					libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(k), zKey, nKey)
					k = k + nKey
					libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(k), (**(**TJsonParse)(__ccgo_up(bp + 4))).FaBlob, (**(**TJsonParse)(__ccgo_up(bp + 4))).FnBlob)
					if (*TJsonParse)(unsafe.Pointer(pParse)).Fdelta != 0 {
						_jsonAfterEditSizeAdjust(tls, pParse, iRoot)
					}
				}
			}
			_jsonParseReset(tls, bp+4)
			_jsonParseReset(tls, bp+60)
			return rc
		}
	} else {
		if int32(**(**int8)(__ccgo_up(zPath))) == int32('[') {
			kk = uint64(0)
			x = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(iRoot)))) & int32(0x0f))
			if int32(x) != int32(JSONB_ARRAY) {
				return uint32(JSON_LOOKUP_NOTFOUND)
			}
			n = _jsonbPayloadSize(tls, pParse, iRoot, bp)
			i = uint32(1)
			for int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zPath + uintptr(i))))])&int32(0x04) != 0 {
				if kk < uint64(0xffffffff) {
					kk = kk*uint64(10) + uint64(**(**int8)(__ccgo_up(zPath + uintptr(i)))) - uint64('0')
				}
				/*     ^^^^^^^^^^--- Allow kk to be bigger than any JSON array so that
				 ** we get NOTFOUND instead of PATHERROR, without overflowing kk. */
				i = i + 1
			}
			if i < uint32(2) || int32(**(**int8)(__ccgo_up(zPath + uintptr(i)))) != int32(']') {
				if int32(**(**int8)(__ccgo_up(zPath + 1))) == int32('#') {
					kk = uint64(_jsonbArrayCount(tls, pParse, iRoot))
					i = uint32(2)
					if int32(**(**int8)(__ccgo_up(zPath + 2))) == int32('-') && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zPath + 3)))])&int32(0x04) != 0 {
						nn = uint64(0)
						i = uint32(3)
						for cond := true; cond; cond = int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zPath + uintptr(i))))])&int32(0x04) != 0 {
							if nn < uint64(0xffffffff) {
								nn = nn*uint64(10) + uint64(**(**int8)(__ccgo_up(zPath + uintptr(i)))) - uint64('0')
							}
							/*     ^^^^^^^^^^--- Allow nn to be bigger than any JSON array to
							 ** get NOTFOUND instead of PATHERROR, without overflowing nn. */
							i = i + 1
						}
						if nn > kk {
							return uint32(JSON_LOOKUP_NOTFOUND)
						}
						kk = kk - nn
					}
					if int32(**(**int8)(__ccgo_up(zPath + uintptr(i)))) != int32(']') {
						return uint32(JSON_LOOKUP_PATHERROR)
					}
				} else {
					return uint32(JSON_LOOKUP_PATHERROR)
				}
			}
			j = iRoot + n
			iEnd = j + **(**Tu32)(__ccgo_up(bp))
			for j < iEnd {
				if kk == uint64(0) {
					v4 = pParse + 32
					*(*Tu16)(unsafe.Pointer(v4)) = *(*Tu16)(unsafe.Pointer(v4)) + 1
					v3 = *(*Tu16)(unsafe.Pointer(v4))
					if int32(v3) >= int32(JSON_MAX_DEPTH) {
						return uint32(JSON_LOOKUP_TOODEEP)
					}
					rc = _jsonLookupStep(tls, pParse, j, zPath+uintptr(i+uint32(1)), uint32(0))
					(*TJsonParse)(unsafe.Pointer(pParse)).FiDepth = (*TJsonParse)(unsafe.Pointer(pParse)).FiDepth - 1
					if (*TJsonParse)(unsafe.Pointer(pParse)).Fdelta != 0 {
						_jsonAfterEditSizeAdjust(tls, pParse, iRoot)
					}
					return rc
				}
				kk = kk - 1
				n = _jsonbPayloadSize(tls, pParse, j, bp)
				if n == uint32(0) {
					return uint32(JSON_LOOKUP_ERROR)
				}
				j = j + (n + **(**Tu32)(__ccgo_up(bp)))
			}
			if j > iEnd {
				return uint32(JSON_LOOKUP_ERROR)
			}
			if kk > uint64(0) {
				return uint32(JSON_LOOKUP_NOTFOUND)
			}
			if int32((*TJsonParse)(unsafe.Pointer(pParse)).FeEdit) >= int32(JEDIT_INS) {
				rc = _jsonCreateEditSubstructure(tls, pParse, bp+116, zPath+uintptr(i+uint32(1)))
				if !(rc >= libc.Uint32FromUint32(JSON_LOOKUP_PATHERROR)) && _jsonBlobMakeEditable(tls, pParse, (**(**TJsonParse)(__ccgo_up(bp + 116))).FnBlob) != 0 {
					_jsonBlobEdit(tls, pParse, j, uint32(0), (**(**TJsonParse)(__ccgo_up(bp + 116))).FaBlob, (**(**TJsonParse)(__ccgo_up(bp + 116))).FnBlob)
				}
				_jsonParseReset(tls, bp+116)
				if (*TJsonParse)(unsafe.Pointer(pParse)).Fdelta != 0 {
					_jsonAfterEditSizeAdjust(tls, pParse, iRoot)
				}
				return rc
			}
		} else {
			return uint32(JSON_LOOKUP_PATHERROR)
		}
	}
	return uint32(JSON_LOOKUP_NOTFOUND)
}

// C documentation
//
//	/*
//	** RFC-7396 MergePatch for two JSONB blobs.
//	**
//	** pTarget is the target. pPatch is the patch.  The target is updated
//	** in place.  The patch is read-only.
//	**
//	** The original RFC-7396 algorithm is this:
//	**
//	**   define MergePatch(Target, Patch):
//	**     if Patch is an Object:
//	**       if Target is not an Object:
//	**         Target = {} # Ignore the contents and set it to an empty Object
//	**     for each Name/Value pair in Patch:
//	**         if Value is null:
//	**           if Name exists in Target:
//	**             remove the Name/Value pair from Target
//	**         else:
//	**           Target[Name] = MergePatch(Target[Name], Value)
//	**       return Target
//	**     else:
//	**       return Patch
//	**
//	** Here is an equivalent algorithm restructured to show the actual
//	** implementation:
//	**
//	** 01   define MergePatch(Target, Patch):
//	** 02      if Patch is not an Object:
//	** 03         return Patch
//	** 04      else: // if Patch is an Object
//	** 05         if Target is not an Object:
//	** 06            Target = {}
//	** 07      for each Name/Value pair in Patch:
//	** 08         if Name exists in Target:
//	** 09            if Value is null:
//	** 10               remove the Name/Value pair from Target
//	** 11            else
//	** 12               Target[name] = MergePatch(Target[Name], Value)
//	** 13         else if Value is not NULL:
//	** 14            if Value is not an Object:
//	** 15               Target[name] = Value
//	** 16            else:
//	** 17               Target[name] = MergePatch('{}',value)
//	** 18      return Target
//	**  |
//	**  ^---- Line numbers referenced in comments in the implementation
//	*/
func _jsonMergePatch(tls *libc.TLS, pTarget uintptr, iTarget Tu32, pPatch uintptr, iPatch Tu32, iDepth Tu32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var ePLabel, eTLabel, x Tu8
	var iPCursor, iPEnd, iPLabel, iPValue, iTCursor, iTEnd, iTEndBE, iTLabel, iTStart, iTValue, n, nPLabel, nPValue, nTLabel, nTValue, szNew, szPatch, szTarget Tu32
	var isEqual, rc, rc1, savedDelta, savedDelta1, v1 int32
	var _ /* sz at bp+0 */ Tu32
	var _ /* szPLabel at bp+12 */ Tu32
	var _ /* szPValue at bp+16 */ Tu32
	var _ /* szTLabel at bp+4 */ Tu32
	var _ /* szTValue at bp+8 */ Tu32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = ePLabel, eTLabel, iPCursor, iPEnd, iPLabel, iPValue, iTCursor, iTEnd, iTEndBE, iTLabel, iTStart, iTValue, isEqual, n, nPLabel, nPValue, nTLabel, nTValue, rc, rc1, savedDelta, savedDelta1, szNew, szPatch, szTarget, x, v1
	**(**Tu32)(__ccgo_up(bp)) = uint32(0)     /* Node type of the target label */
	iTLabel = uint32(0)                       /* Index of the label */
	nTLabel = uint32(0)                       /* Header size in bytes for the target label */
	**(**Tu32)(__ccgo_up(bp + 4)) = uint32(0) /* Size of the target label payload */
	iTValue = uint32(0)                       /* Index of the target value */
	nTValue = uint32(0)                       /* Header size of the target value */
	**(**Tu32)(__ccgo_up(bp + 8)) = uint32(0) /* Payload size of the patch value */
	x = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob + uintptr(iPatch)))) & int32(0x0f))
	if int32(x) != int32(JSONB_OBJECT) { /* Total size of the target, header+payload */
		n = _jsonbPayloadSize(tls, pPatch, iPatch, bp)
		szPatch = n + **(**Tu32)(__ccgo_up(bp))
		**(**Tu32)(__ccgo_up(bp)) = uint32(0)
		n = _jsonbPayloadSize(tls, pTarget, iTarget, bp)
		szTarget = n + **(**Tu32)(__ccgo_up(bp))
		_jsonBlobEdit(tls, pTarget, iTarget, szTarget, (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPatch), szPatch)
		if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 {
			v1 = int32(JSON_MERGE_OOM)
		} else {
			v1 = JSON_MERGE_OK
		}
		return v1 /* Line 03 */
	}
	x = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTarget)))) & int32(0x0f))
	if int32(x) != int32(JSONB_OBJECT) { /* Algorithm line 05 */
		n = _jsonbPayloadSize(tls, pTarget, iTarget, bp)
		_jsonBlobEdit(tls, pTarget, iTarget+n, **(**Tu32)(__ccgo_up(bp)), uintptr(0), uint32(0))
		x = **(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTarget)))
		**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTarget))) = uint8(int32(x)&int32(0xf0) | int32(JSONB_OBJECT))
	}
	n = _jsonbPayloadSize(tls, pPatch, iPatch, bp)
	if n == uint32(0) {
		return int32(JSON_MERGE_BADPATCH)
	}
	iPCursor = iPatch + n
	iPEnd = iPCursor + **(**Tu32)(__ccgo_up(bp))
	n = _jsonbPayloadSize(tls, pTarget, iTarget, bp)
	if n == uint32(0) {
		return int32(JSON_MERGE_BADTARGET)
	}
	iTStart = iTarget + n
	iTEndBE = iTStart + **(**Tu32)(__ccgo_up(bp))
	for iPCursor < iPEnd { /* Algorithm line 07 */
		iPLabel = iPCursor
		ePLabel = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob + uintptr(iPCursor)))) & int32(0x0f))
		if int32(ePLabel) < int32(JSONB_TEXT) || int32(ePLabel) > int32(JSONB_TEXTRAW) {
			return int32(JSON_MERGE_BADPATCH)
		}
		nPLabel = _jsonbPayloadSize(tls, pPatch, iPCursor, bp+12)
		if nPLabel == uint32(0) {
			return int32(JSON_MERGE_BADPATCH)
		}
		iPValue = iPCursor + nPLabel + **(**Tu32)(__ccgo_up(bp + 12))
		if iPValue >= iPEnd {
			return int32(JSON_MERGE_BADPATCH)
		}
		nPValue = _jsonbPayloadSize(tls, pPatch, iPValue, bp+16)
		if nPValue == uint32(0) {
			return int32(JSON_MERGE_BADPATCH)
		}
		iPCursor = iPValue + nPValue + **(**Tu32)(__ccgo_up(bp + 16))
		if iPCursor > iPEnd {
			return int32(JSON_MERGE_BADPATCH)
		}
		iTCursor = iTStart
		iTEnd = iTEndBE + uint32((*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta)
		for iTCursor < iTEnd { /* true if the patch and target labels match */
			iTLabel = iTCursor
			eTLabel = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTCursor)))) & int32(0x0f))
			if int32(eTLabel) < int32(JSONB_TEXT) || int32(eTLabel) > int32(JSONB_TEXTRAW) {
				return int32(JSON_MERGE_BADTARGET)
			}
			nTLabel = _jsonbPayloadSize(tls, pTarget, iTCursor, bp+4)
			if nTLabel == uint32(0) {
				return int32(JSON_MERGE_BADTARGET)
			}
			iTValue = iTLabel + nTLabel + **(**Tu32)(__ccgo_up(bp + 4))
			if iTValue >= iTEnd {
				return int32(JSON_MERGE_BADTARGET)
			}
			nTValue = _jsonbPayloadSize(tls, pTarget, iTValue, bp+8)
			if nTValue == uint32(0) {
				return int32(JSON_MERGE_BADTARGET)
			}
			if iTValue+nTValue+**(**Tu32)(__ccgo_up(bp + 8)) > iTEnd {
				return int32(JSON_MERGE_BADTARGET)
			}
			isEqual = _jsonLabelCompare(tls, (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPLabel+nPLabel), **(**Tu32)(__ccgo_up(bp + 12)), libc.BoolInt32(int32(ePLabel) == int32(JSONB_TEXT) || int32(ePLabel) == int32(JSONB_TEXTRAW)), (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTLabel+nTLabel), **(**Tu32)(__ccgo_up(bp + 4)), libc.BoolInt32(int32(eTLabel) == int32(JSONB_TEXT) || int32(eTLabel) == int32(JSONB_TEXTRAW)))
			if isEqual != 0 {
				break
			}
			iTCursor = iTValue + nTValue + **(**Tu32)(__ccgo_up(bp + 8))
		}
		x = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob + uintptr(iPValue)))) & int32(0x0f))
		if iTCursor < iTEnd {
			/* A match was found.  Algorithm line 08 */
			if int32(x) == 0 {
				/* Patch value is NULL.  Algorithm line 09 */
				_jsonBlobEdit(tls, pTarget, iTLabel, nTLabel+**(**Tu32)(__ccgo_up(bp + 4))+nTValue+**(**Tu32)(__ccgo_up(bp + 8)), uintptr(0), uint32(0))
				/*  vvvvvv----- No OOM on a delete-only edit */
				if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 {
					return int32(JSON_MERGE_OOM)
				}
			} else {
				savedDelta = (*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta
				(*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta = 0
				if iDepth >= uint32(JSON_MAX_DEPTH) {
					return int32(JSON_MERGE_TOODEEP)
				}
				rc = _jsonMergePatch(tls, pTarget, iTValue, pPatch, iPValue, iDepth+uint32(1))
				if rc != 0 {
					return rc
				}
				**(**int32)(__ccgo_up(pTarget + 40)) += savedDelta
			}
		} else {
			if int32(x) > 0 { /* Algorithm line 13 */
				/* No match and patch value is not NULL */
				szNew = **(**Tu32)(__ccgo_up(bp + 12)) + nPLabel
				if int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob + uintptr(iPValue))))&int32(0x0f) != int32(JSONB_OBJECT) { /* Line 14 */
					_jsonBlobEdit(tls, pTarget, iTEnd, uint32(0), uintptr(0), **(**Tu32)(__ccgo_up(bp + 16))+nPValue+szNew)
					if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 {
						return int32(JSON_MERGE_OOM)
					}
					libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd), (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPLabel), szNew)
					libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd+szNew), (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPValue), **(**Tu32)(__ccgo_up(bp + 16))+nPValue)
				} else {
					_jsonBlobEdit(tls, pTarget, iTEnd, uint32(0), uintptr(0), szNew+uint32(1))
					if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 {
						return int32(JSON_MERGE_OOM)
					}
					libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd), (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPLabel), szNew)
					**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob + uintptr(iTEnd+szNew))) = uint8(0x00)
					savedDelta1 = (*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta
					(*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta = 0
					if iDepth >= uint32(JSON_MAX_DEPTH) {
						return int32(JSON_MERGE_TOODEEP)
					}
					rc1 = _jsonMergePatch(tls, pTarget, iTEnd+szNew, pPatch, iPValue, iDepth+uint32(1))
					if rc1 != 0 {
						return rc1
					}
					**(**int32)(__ccgo_up(pTarget + 40)) += savedDelta1
				}
			}
		}
	}
	if (*TJsonParse)(unsafe.Pointer(pTarget)).Fdelta != 0 {
		_jsonAfterEditSizeAdjust(tls, pTarget, iTarget)
	}
	if (*TJsonParse)(unsafe.Pointer(pTarget)).Foom != 0 {
		v1 = int32(JSON_MERGE_OOM)
	} else {
		v1 = JSON_MERGE_OK
	}
	return v1
}

func _jsonObjectCompute(tls *libc.TLS, ctx uintptr, isFinal int32) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var c int8
	var flags, inStr int32
	var i, j, v2, v3 Tu64
	var pOgStr, pStr uintptr
	var v8 Tsqlite3_destructor_type
	var _ /* tmpStr at bp+0 */ TJsonString
	_, _, _, _, _, _, _, _, _, _ = c, flags, i, inStr, j, pOgStr, pStr, v2, v3, v8
	flags = int32(Xsqlite3_user_data(tls, ctx))
	pStr = Xsqlite3_aggregate_context(tls, ctx, 0)
	if pStr != 0 {
		pOgStr = pStr
		_jsonAppendRawNZ(tls, pOgStr, __ccgo_ts+28094, uint32(2)) /* Ensure it is zero-terminated */
		_jsonStringTrimOneChar(tls, pOgStr)                       /* Remove the zero terminator */
		(*TJsonString)(unsafe.Pointer(pStr)).FpCtx = ctx
		if (*TJsonString)(unsafe.Pointer(pStr)).FeErr != 0 {
			_jsonReturnString(tls, pStr, uintptr(0), uintptr(0))
			return
		}
		if int32(**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf))) != int32('{') {
			inStr = 0
			if !(isFinal != 0) {
				/* Work with a temporary copy of the string if this is not the
				 ** final result */
				_jsonStringInit(tls, bp, ctx)
				_jsonAppendRawNZ(tls, bp, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf, uint32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed+uint64(1)))
				pStr = bp
				if (*TJsonString)(unsafe.Pointer(pStr)).FeErr != 0 {
					_jsonReturnString(tls, pStr, uintptr(0), uintptr(0))
					return
				}
				_jsonStringTrimOneChar(tls, pStr) /* Remove zero terminator */
			}
			/* Fix up the string by changing the initial "@" flag back to
			 ** to "{" and removing all subsequence "@" entries, with their
			 ** associated comma delimeters. */
			**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf)) = int8('{')
			v2 = libc.Uint64FromInt32(1)
			j = v2
			i = v2
			for {
				if !(i < (*TJsonString)(unsafe.Pointer(pStr)).FnUsed) {
					break
				}
				c = **(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf + uintptr(i)))
				if int32(c) == int32('"') {
					inStr = libc.BoolInt32(!(inStr != 0))
					v2 = j
					j = j + 1
					**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf + uintptr(v2))) = int8('"')
				} else {
					if int32(c) == int32('\\') {
						v2 = j
						j = j + 1
						**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf + uintptr(v2))) = int8('\\')
						v2 = j
						j = j + 1
						i = i + 1
						v3 = i
						**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf + uintptr(v2))) = **(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf + uintptr(v3)))
					} else {
						if int32(c) == int32('@') && !(inStr != 0) {
							if int32(**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf + uintptr(i+uint64(1))))) == int32(',') {
								i = i + 1
							} else {
								if int32(**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf + uintptr(j-uint64(1))))) == int32(',') {
									j = j - 1
								}
							}
						} else {
							v2 = j
							j = j + 1
							**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf + uintptr(v2))) = c
						}
					}
				}
				goto _1
			_1:
				;
				i = i + 1
			}
			**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf + uintptr(j))) = 0 /* Restore zero terminator */
			(*TJsonString)(unsafe.Pointer(pStr)).FnUsed = j                                    /* Truncate the string */
		}
		if flags&int32(JSON_BLOB) != 0 {
			_jsonReturnStringAsBlob(tls, pStr)
			if isFinal != 0 {
				if !((*TJsonString)(unsafe.Pointer(pStr)).FbStatic != 0) {
					_sqlite3RCStrUnref(tls, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf)
				}
			} else {
				_jsonStringTrimOneChar(tls, pOgStr)
			}
		} else {
			if isFinal != 0 {
				if (*TJsonString)(unsafe.Pointer(pStr)).FbStatic != 0 {
					v8 = uintptr(-libc.Int32FromInt32(1))
				} else {
					v8 = __ccgo_fp(_sqlite3RCStrUnref)
				}
				Xsqlite3_result_text(tls, ctx, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf, int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed), v8)
				(*TJsonString)(unsafe.Pointer(pStr)).FbStatic = uint8(1)
			} else {
				Xsqlite3_result_text(tls, ctx, (*TJsonString)(unsafe.Pointer(pStr)).FzBuf, int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed), uintptr(-libc.Int32FromInt32(1)))
				_jsonStringTrimOneChar(tls, pOgStr)
			}
		}
		if pStr != pOgStr {
			_jsonStringReset(tls, pStr)
		}
	} else {
		if flags&int32(JSON_BLOB) != 0 {
			Xsqlite3_result_blob(tls, ctx, uintptr(unsafe.Pointer(&_emptyObject1)), int32(1), libc.UintptrFromInt32(0))
		} else {
			Xsqlite3_result_text(tls, ctx, __ccgo_ts+28096, int32(2), libc.UintptrFromInt32(0))
		}
	}
	Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
}

// C documentation
//
//	/*
//	** Implementation of the json_object(NAME,VALUE,...) function.  Return a JSON
//	** object that contains all name/value given in arguments.  Or if any name
//	** is not a string or if any value is a BLOB, throw an error.
//	*/
func _jsonObjectFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var i int32
	var n Tu32
	var z uintptr
	var _ /* jx at bp+0 */ TJsonString
	_, _, _ = i, n, z
	if argc&int32(1) != 0 {
		Xsqlite3_result_error(tls, ctx, __ccgo_ts+27920, -int32(1))
		return
	}
	_jsonStringInit(tls, bp, ctx)
	_jsonAppendChar(tls, bp, int8('{'))
	i = 0
	for {
		if !(i < argc) {
			break
		}
		if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4))) != int32(SQLITE_TEXT) {
			Xsqlite3_result_error(tls, ctx, __ccgo_ts+27971, -int32(1))
			_jsonStringReset(tls, bp)
			return
		}
		_jsonAppendSeparator(tls, bp)
		z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
		n = uint32(Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4))))
		_jsonAppendString(tls, bp, z, n)
		_jsonAppendChar(tls, bp, int8(':'))
		_jsonAppendSqlValue(tls, bp, **(**uintptr)(__ccgo_up(argv + uintptr(i+int32(1))*4)))
		goto _1
	_1:
		;
		i = i + int32(2)
	}
	_jsonAppendChar(tls, bp, int8('}'))
	_jsonReturnString(tls, bp, uintptr(0), uintptr(0))
	Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
}

// C documentation
//
//	/*
//	** json_group_obj(NAME,VALUE)
//	**
//	** Return a JSON object composed of all names and values in the aggregate.
//	**
//	** Rows for which NAME is NULL do not result in a new entry.  However, we
//	** do initially insert a "@" entry into the growing string for each null entry
//	** and change the first character of the string to "@" to signal that the
//	** string contains null entries.  The "@" markers are needed in order to
//	** correctly process xInverse() requests.  The initial "@" is converted
//	** back into "{" and the "@" null values are removed by jsonObjectCompute().
//	*/
func _jsonObjectStep(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	var n Tu32
	var pStr, z uintptr
	_, _, _ = n, pStr, z
	_ = argc
	pStr = Xsqlite3_aggregate_context(tls, ctx, int32(128))
	if pStr != 0 {
		z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		n = uint32(_sqlite3Strlen30(tls, z))
		if (*TJsonString)(unsafe.Pointer(pStr)).FzBuf == uintptr(0) {
			_jsonStringInit(tls, pStr, ctx)
			_jsonAppendChar(tls, pStr, int8('{'))
		} else {
			if (*TJsonString)(unsafe.Pointer(pStr)).FnUsed > uint64(1) {
				_jsonAppendChar(tls, pStr, int8(','))
			}
		}
		(*TJsonString)(unsafe.Pointer(pStr)).FpCtx = ctx
		if z != uintptr(0) {
			_jsonAppendString(tls, pStr, z, n)
			_jsonAppendChar(tls, pStr, int8(':'))
			_jsonAppendSqlValue(tls, pStr, **(**uintptr)(__ccgo_up(argv + 1*4)))
		} else {
			**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pStr)).FzBuf)) = int8('@')
			_jsonAppendRawNZ(tls, pStr, __ccgo_ts+27909, uint32(1))
		}
	}
}

// C documentation
//
//	/*
//	** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
//	** from the SQL function argument pArg.  Return a pointer to the new
//	** JsonParse object.
//	**
//	** Ownership of the new JsonParse object is passed to the caller.  The
//	** caller should invoke jsonParseFree() on the return value when it
//	** has finished using it.
//	**
//	** If any errors are detected, an appropriate error messages is set
//	** using sqlite3_result_error() or the equivalent and this routine
//	** returns NULL.  This routine also returns NULL if the pArg argument
//	** is an SQL NULL value, but no error message is set in that case.  This
//	** is so that SQL functions that are given NULL arguments will return
//	** a NULL value.
//	*/
func _jsonParseFuncArg(tls *libc.TLS, ctx uintptr, pArg uintptr, flgs Tu32) (r uintptr) {
	var db, p, pFromCache, zNew, v2 uintptr
	var eType, isRCStr, rc int32
	var nBlob, v1 Tu32
	_, _, _, _, _, _, _, _, _, _ = db, eType, isRCStr, nBlob, p, pFromCache, rc, zNew, v1, v2 /* Datatype of pArg */
	p = uintptr(0)                                                                            /* Value to be returned */
	pFromCache = uintptr(0)                                                                   /* The database connection */
	eType = Xsqlite3_value_type(tls, pArg)
	if eType == int32(SQLITE_NULL) {
		return uintptr(0)
	}
	pFromCache = _jsonCacheSearch(tls, ctx, pArg)
	if pFromCache != 0 {
		(*TJsonParse)(unsafe.Pointer(pFromCache)).FnJPRef = (*TJsonParse)(unsafe.Pointer(pFromCache)).FnJPRef + 1
		if flgs&uint32(JSON_EDITABLE) == uint32(0) {
			return pFromCache
		}
	}
	db = Xsqlite3_context_db_handle(tls, ctx)
	goto rebuild_from_cache
rebuild_from_cache:
	;
	p = _sqlite3DbMallocZero(tls, db, uint64(56))
	if p == uintptr(0) {
		goto json_pfa_oom
	}
	libc.Xmemset(tls, p, 0, uint32(56))
	(*TJsonParse)(unsafe.Pointer(p)).Fdb = db
	(*TJsonParse)(unsafe.Pointer(p)).FnJPRef = uint32(1)
	if pFromCache != uintptr(0) {
		nBlob = (*TJsonParse)(unsafe.Pointer(pFromCache)).FnBlob
		(*TJsonParse)(unsafe.Pointer(p)).FaBlob = _sqlite3DbMallocRaw(tls, db, uint64(nBlob))
		if (*TJsonParse)(unsafe.Pointer(p)).FaBlob == uintptr(0) {
			goto json_pfa_oom
		}
		libc.Xmemcpy(tls, (*TJsonParse)(unsafe.Pointer(p)).FaBlob, (*TJsonParse)(unsafe.Pointer(pFromCache)).FaBlob, nBlob)
		v1 = nBlob
		(*TJsonParse)(unsafe.Pointer(p)).FnBlob = v1
		(*TJsonParse)(unsafe.Pointer(p)).FnBlobAlloc = v1
		(*TJsonParse)(unsafe.Pointer(p)).FhasNonstd = (*TJsonParse)(unsafe.Pointer(pFromCache)).FhasNonstd
		_jsonParseFree(tls, pFromCache)
		return p
	}
	if eType == int32(SQLITE_BLOB) {
		if _jsonArgIsJsonb(tls, pArg, p) != 0 {
			if flgs&uint32(JSON_EDITABLE) != uint32(0) && _jsonBlobMakeEditable(tls, p, uint32(0)) == 0 {
				goto json_pfa_oom
			}
			return p
		}
		/* If the blob is not valid JSONB, fall through into trying to cast
		 ** the blob into text which is then interpreted as JSON.  (tag-20240123-a)
		 **
		 ** This goes against all historical documentation about how the SQLite
		 ** JSON functions were suppose to work.  From the beginning, blob was
		 ** reserved for expansion and a blob value should have raised an error.
		 ** But it did not, due to a bug.  And many applications came to depend
		 ** upon this buggy behavior, especially when using the CLI and reading
		 ** JSON text using readfile(), which returns a blob.  For this reason
		 ** we will continue to support the bug moving forward.
		 ** See for example https://sqlite.org/forum/forumpost/012136abd5292b8d
		 */
	}
	(*TJsonParse)(unsafe.Pointer(p)).FzJson = Xsqlite3_value_text(tls, pArg)
	(*TJsonParse)(unsafe.Pointer(p)).FnJson = Xsqlite3_value_bytes(tls, pArg)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto json_pfa_oom
	}
	if (*TJsonParse)(unsafe.Pointer(p)).FnJson == 0 {
		goto json_pfa_malformed
	}
	if flgs&uint32(JSON_KEEPERROR) != 0 {
		v2 = uintptr(0)
	} else {
		v2 = ctx
	}
	if _jsonConvertTextToBlob(tls, p, v2) != 0 {
		if flgs&uint32(JSON_KEEPERROR) != 0 {
			(*TJsonParse)(unsafe.Pointer(p)).FnErr = uint8(1)
			return p
		} else {
			_jsonParseFree(tls, p)
			return uintptr(0)
		}
	} else {
		isRCStr = _sqlite3ValueIsOfClass(tls, pArg, __ccgo_fp(_sqlite3RCStrUnref))
		if !(isRCStr != 0) {
			zNew = _sqlite3RCStrNew(tls, uint64((*TJsonParse)(unsafe.Pointer(p)).FnJson))
			if zNew == uintptr(0) {
				goto json_pfa_oom
			}
			libc.Xmemcpy(tls, zNew, (*TJsonParse)(unsafe.Pointer(p)).FzJson, uint32((*TJsonParse)(unsafe.Pointer(p)).FnJson))
			(*TJsonParse)(unsafe.Pointer(p)).FzJson = zNew
			**(**int8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(p)).FzJson + uintptr((*TJsonParse)(unsafe.Pointer(p)).FnJson))) = 0
		} else {
			_sqlite3RCStrRef(tls, (*TJsonParse)(unsafe.Pointer(p)).FzJson)
		}
		(*TJsonParse)(unsafe.Pointer(p)).FbJsonIsRCStr = uint8(1)
		rc = _jsonCacheInsert(tls, ctx, p)
		if rc == int32(SQLITE_NOMEM) {
			goto json_pfa_oom
		}
		if flgs&uint32(JSON_EDITABLE) != 0 {
			pFromCache = p
			p = uintptr(0)
			goto rebuild_from_cache
		}
	}
	return p
	goto json_pfa_malformed
json_pfa_malformed:
	;
	if flgs&uint32(JSON_KEEPERROR) != 0 {
		(*TJsonParse)(unsafe.Pointer(p)).FnErr = uint8(1)
		return p
	} else {
		_jsonParseFree(tls, p)
		Xsqlite3_result_error(tls, ctx, __ccgo_ts+27699, -int32(1))
		return uintptr(0)
	}
	goto json_pfa_oom
json_pfa_oom:
	;
	_jsonParseFree(tls, pFromCache)
	_jsonParseFree(tls, p)
	Xsqlite3_result_error_nomem(tls, ctx)
	return uintptr(0)
}

// C documentation
//
//	/*
//	** Implementation of the json_mergepatch(JSON1,JSON2) function.  Return a JSON
//	** object that is the result of running the RFC 7396 MergePatch() algorithm
//	** on the two arguments.
//	*/
func _jsonPatchFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	var pPatch, pTarget uintptr
	var rc int32
	_, _, _ = pPatch, pTarget, rc /* Result code */
	_ = argc
	pTarget = _jsonParseFuncArg(tls, ctx, **(**uintptr)(__ccgo_up(argv)), uint32(JSON_EDITABLE))
	if pTarget == uintptr(0) {
		return
	}
	pPatch = _jsonParseFuncArg(tls, ctx, **(**uintptr)(__ccgo_up(argv + 1*4)), uint32(0))
	if pPatch != 0 {
		rc = _jsonMergePatch(tls, pTarget, uint32(0), pPatch, uint32(0), uint32(0))
		if rc == JSON_MERGE_OK {
			_jsonReturnParse(tls, ctx, pTarget)
		} else {
			if rc == int32(JSON_MERGE_OOM) {
				Xsqlite3_result_error_nomem(tls, ctx)
			} else {
				if rc == int32(JSON_MERGE_TOODEEP) {
					Xsqlite3_result_error(tls, ctx, __ccgo_ts+27649, -int32(1))
				} else {
					Xsqlite3_result_error(tls, ctx, __ccgo_ts+27699, -int32(1))
				}
			}
		}
		_jsonParseFree(tls, pPatch)
	}
	_jsonParseFree(tls, pTarget)
}

// C documentation
//
//	/*
//	** json_pretty(JSON)
//	** json_pretty(JSON, INDENT)
//	**
//	** Return text that is a pretty-printed rendering of the input JSON.
//	** If the argument is not valid JSON, return NULL.
//	**
//	** The INDENT argument is text that is used for indentation.  If omitted,
//	** it defaults to four spaces (the same as PostgreSQL).
//	*/
func _jsonPrettyFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(160)
	defer tls.Free(160)
	var v1 uintptr
	var v2 bool
	var _ /* s at bp+0 */ TJsonString
	var _ /* x at bp+128 */ TJsonPretty
	_, _ = v1, v2 /* Pretty printing context */
	libc.Xmemset(tls, bp+128, 0, uint32(20))
	(**(**TJsonPretty)(__ccgo_up(bp + 128))).FpParse = _jsonParseFuncArg(tls, ctx, **(**uintptr)(__ccgo_up(argv)), uint32(0))
	if (**(**TJsonPretty)(__ccgo_up(bp + 128))).FpParse == uintptr(0) {
		return
	}
	(**(**TJsonPretty)(__ccgo_up(bp + 128))).FpOut = bp
	_jsonStringInit(tls, bp, ctx)
	if v2 = argc == int32(1); !v2 {
		v1 = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
		(**(**TJsonPretty)(__ccgo_up(bp + 128))).FzIndent = v1
	}
	if v2 || v1 == uintptr(0) {
		(**(**TJsonPretty)(__ccgo_up(bp + 128))).FzIndent = __ccgo_ts + 28029
		(**(**TJsonPretty)(__ccgo_up(bp + 128))).FszIndent = uint32(4)
	} else {
		(**(**TJsonPretty)(__ccgo_up(bp + 128))).FszIndent = libc.Xstrlen(tls, (**(**TJsonPretty)(__ccgo_up(bp + 128))).FzIndent)
	}
	_jsonTranslateBlobToPrettyText(tls, bp+128, uint32(0))
	_jsonReturnString(tls, bp, uintptr(0), uintptr(0))
	_jsonParseFree(tls, (**(**TJsonPretty)(__ccgo_up(bp + 128))).FpParse)
}

// C documentation
//
//	/* Append formatted text (not to exceed N bytes) to the JsonString.
//	*/
func _jsonPrintf(tls *libc.TLS, N int32, p uintptr, zFormat uintptr, va uintptr) {
	var ap Tva_list
	_ = ap
	if (*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(N) >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, uint32(N)) != 0 {
		return
	}
	ap = va
	Xsqlite3_vsnprintf(tls, N, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zFormat, ap)
	_ = ap
	**(**Tu64)(__ccgo_up(p + 16)) += uint64(int32(libc.Xstrlen(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed))))
}

// C documentation
//
//	/*
//	** json_remove(JSON, PATH, ...)
//	**
//	** Remove the named elements from JSON and return the result.  malformed
//	** JSON or PATH arguments result in an error.
//	*/
func _jsonRemoveFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	var i, v1 int32
	var p, zPath uintptr
	var rc Tu32
	_, _, _, _, _ = i, p, rc, zPath, v1 /* The parse */
	zPath = uintptr(0)                  /* Subroutine return code */
	if argc < int32(1) {
		return
	}
	if argc > int32(1) {
		v1 = int32(JSON_EDITABLE)
	} else {
		v1 = 0
	}
	p = _jsonParseFuncArg(tls, ctx, **(**uintptr)(__ccgo_up(argv)), uint32(v1))
	if p == uintptr(0) {
		return
	}
	i = int32(1)
	for {
		if !(i < argc) {
			break
		}
		zPath = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
		if zPath == uintptr(0) {
			goto json_remove_done
		}
		if int32(**(**int8)(__ccgo_up(zPath))) != int32('$') {
			goto json_remove_patherror
		}
		if int32(**(**int8)(__ccgo_up(zPath + 1))) == 0 {
			/* json_remove(j,'$') returns NULL */
			goto json_remove_done
		}
		(*TJsonParse)(unsafe.Pointer(p)).FeEdit = uint8(JEDIT_DEL)
		(*TJsonParse)(unsafe.Pointer(p)).Fdelta = 0
		rc = _jsonLookupStep(tls, p, uint32(0), zPath+uintptr(1), uint32(0))
		if rc >= uint32(JSON_LOOKUP_PATHERROR) {
			if rc == uint32(JSON_LOOKUP_NOTFOUND) {
				goto _2 /* No-op */
			} else {
				_jsonBadPathError(tls, ctx, zPath, int32(rc))
			}
			goto json_remove_done
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	_jsonReturnParse(tls, ctx, p)
	_jsonParseFree(tls, p)
	return
	goto json_remove_patherror
json_remove_patherror:
	;
	_jsonBadPathError(tls, ctx, zPath, 0)
	goto json_remove_done
json_remove_done:
	;
	_jsonParseFree(tls, p)
	return
}

// C documentation
//
//	/*
//	** Return the value of the BLOB node at index i.
//	**
//	** If the value is a primitive, return it as an SQL value.
//	** If the value is an array or object, return it as either
//	** JSON text or the BLOB encoding, depending on the eMode flag
//	** as follows:
//	**
//	**     eMode==0     JSONB if the JSON_B flag is set in userdata or
//	**                  text if the JSON_B flag is omitted from userdata.
//	**
//	**     eMode==1     Text
//	**
//	**     eMode==2     JSONB
//	*/
func _jsonReturnFromBlob(tls *libc.TLS, pParse uintptr, i Tu32, pCtx uintptr, eMode int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bNeg, rc int32
	var c, x int8
	var db, z, z1, z2, zOut uintptr
	var iIn, iOut, n, nOut, szEscape, v19, v20 Tu32
	var r, v16 float64
	var v17 int64
	var _ /* iRes at bp+8 */ Tsqlite3_int64
	var _ /* r at bp+16 */ float64
	var _ /* sz at bp+0 */ Tu32
	var _ /* v at bp+24 */ Tu32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bNeg, c, db, iIn, iOut, n, nOut, r, rc, szEscape, x, z, z1, z2, zOut, v16, v17, v19, v20
	db = Xsqlite3_context_db_handle(tls, pCtx)
	n = _jsonbPayloadSize(tls, pParse, i, bp)
	if n == uint32(0) {
		Xsqlite3_result_error(tls, pCtx, __ccgo_ts+27699, -int32(1))
		return
	}
	switch int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i)))) & libc.Int32FromInt32(0x0f) {
	case JSONB_NULL:
		goto _1
	case int32(JSONB_TRUE):
		goto _2
	case int32(JSONB_FALSE):
		goto _3
	case int32(JSONB_INT):
		goto _4
	case int32(JSONB_INT5):
		goto _5
	case int32(JSONB_FLOAT):
		goto _6
	case int32(JSONB_FLOAT5):
		goto _7
	case int32(JSONB_TEXT):
		goto _8
	case int32(JSONB_TEXTRAW):
		goto _9
	case int32(JSONB_TEXTJ):
		goto _10
	case int32(JSONB_TEXT5):
		goto _11
	case int32(JSONB_OBJECT):
		goto _12
	case int32(JSONB_ARRAY):
		goto _13
	default:
		goto _14
	}
	goto _15
_1:
	;
	if **(**Tu32)(__ccgo_up(bp)) != 0 {
		goto returnfromblob_malformed
	}
	Xsqlite3_result_null(tls, pCtx)
	goto _15
_2:
	;
	if **(**Tu32)(__ccgo_up(bp)) != 0 {
		goto returnfromblob_malformed
	}
	Xsqlite3_result_int(tls, pCtx, int32(1))
	goto _15
_3:
	;
	if **(**Tu32)(__ccgo_up(bp)) != 0 {
		goto returnfromblob_malformed
	}
	Xsqlite3_result_int(tls, pCtx, 0)
	goto _15
_5:
	;
_4:
	;
	**(**Tsqlite3_int64)(__ccgo_up(bp + 8)) = 0
	bNeg = 0
	if **(**Tu32)(__ccgo_up(bp)) == uint32(0) {
		goto returnfromblob_malformed
	}
	x = int8(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n))))
	if int32(x) == int32('-') {
		if **(**Tu32)(__ccgo_up(bp)) < uint32(2) {
			goto returnfromblob_malformed
		}
		n = n + 1
		**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) - 1
		bNeg = int32(1)
	}
	z = _sqlite3DbStrNDup(tls, db, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), uint64(int32(**(**Tu32)(__ccgo_up(bp)))))
	if z == uintptr(0) {
		goto returnfromblob_oom
	}
	rc = _sqlite3DecOrHexToI64(tls, z, bp+8)
	_sqlite3DbFree(tls, db, z)
	if rc == 0 {
		if **(**Tsqlite3_int64)(__ccgo_up(bp + 8)) < 0 {
			r = float64(**(**Tsqlite3_uint64)(__ccgo_up(bp + 8)))
			if bNeg != 0 {
				v16 = -r
			} else {
				v16 = r
			}
			Xsqlite3_result_double(tls, pCtx, v16)
		} else {
			if bNeg != 0 {
				v17 = -**(**Tsqlite3_int64)(__ccgo_up(bp + 8))
			} else {
				v17 = **(**Tsqlite3_int64)(__ccgo_up(bp + 8))
			}
			Xsqlite3_result_int64(tls, pCtx, v17)
		}
	} else {
		if rc == int32(3) && bNeg != 0 {
			Xsqlite3_result_int64(tls, pCtx, int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)))
		} else {
			if rc == int32(1) {
				goto returnfromblob_malformed
			} else {
				if bNeg != 0 {
					n = n - 1
					**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + 1
				}
				goto to_double
			}
		}
	}
	goto _15
_7:
	;
_6:
	;
	if **(**Tu32)(__ccgo_up(bp)) == uint32(0) {
		goto returnfromblob_malformed
	}
	goto to_double
to_double:
	;
	z1 = _sqlite3DbStrNDup(tls, db, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), uint64(int32(**(**Tu32)(__ccgo_up(bp)))))
	if z1 == uintptr(0) {
		goto returnfromblob_oom
	}
	rc = _sqlite3AtoF(tls, z1, bp+16)
	_sqlite3DbFree(tls, db, z1)
	if rc <= 0 {
		goto returnfromblob_malformed
	}
	Xsqlite3_result_double(tls, pCtx, **(**float64)(__ccgo_up(bp + 16)))
	goto _15
_9:
	;
_8:
	;
	Xsqlite3_result_text(tls, pCtx, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), int32(**(**Tu32)(__ccgo_up(bp))), uintptr(-libc.Int32FromInt32(1)))
	goto _15
_11:
	;
_10:
	;
	nOut = **(**Tu32)(__ccgo_up(bp))
	z2 = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n)
	zOut = _sqlite3DbMallocRaw(tls, db, uint64(nOut)+uint64(1))
	if zOut == uintptr(0) {
		goto returnfromblob_oom
	}
	v19 = libc.Uint32FromInt32(0)
	iOut = v19
	iIn = v19
	for {
		if !(iIn < **(**Tu32)(__ccgo_up(bp))) {
			break
		}
		c = **(**int8)(__ccgo_up(z2 + uintptr(iIn)))
		if int32(c) == int32('\\') {
			szEscape = _jsonUnescapeOneChar(tls, z2+uintptr(iIn), **(**Tu32)(__ccgo_up(bp))-iIn, bp+24)
			if **(**Tu32)(__ccgo_up(bp + 24)) <= uint32(0x7f) {
				v20 = iOut
				iOut = iOut + 1
				**(**int8)(__ccgo_up(zOut + uintptr(v20))) = int8(**(**Tu32)(__ccgo_up(bp + 24)))
			} else {
				if **(**Tu32)(__ccgo_up(bp + 24)) <= uint32(0x7ff) {
					v19 = iOut
					iOut = iOut + 1
					**(**int8)(__ccgo_up(zOut + uintptr(v19))) = int8(libc.Uint32FromInt32(0xc0) | **(**Tu32)(__ccgo_up(bp + 24))>>libc.Int32FromInt32(6))
					v19 = iOut
					iOut = iOut + 1
					**(**int8)(__ccgo_up(zOut + uintptr(v19))) = int8(uint32(0x80) | **(**Tu32)(__ccgo_up(bp + 24))&uint32(0x3f))
				} else {
					if **(**Tu32)(__ccgo_up(bp + 24)) < uint32(0x10000) {
						v19 = iOut
						iOut = iOut + 1
						**(**int8)(__ccgo_up(zOut + uintptr(v19))) = int8(uint32(0xe0) | **(**Tu32)(__ccgo_up(bp + 24))>>libc.Int32FromInt32(12))
						v19 = iOut
						iOut = iOut + 1
						**(**int8)(__ccgo_up(zOut + uintptr(v19))) = int8(uint32(0x80) | **(**Tu32)(__ccgo_up(bp + 24))>>libc.Int32FromInt32(6)&uint32(0x3f))
						v19 = iOut
						iOut = iOut + 1
						**(**int8)(__ccgo_up(zOut + uintptr(v19))) = int8(uint32(0x80) | **(**Tu32)(__ccgo_up(bp + 24))&uint32(0x3f))
					} else {
						if **(**Tu32)(__ccgo_up(bp + 24)) == uint32(JSON_INVALID_CHAR) {
							/* Silently ignore illegal unicode */
						} else {
							v19 = iOut
							iOut = iOut + 1
							**(**int8)(__ccgo_up(zOut + uintptr(v19))) = int8(uint32(0xf0) | **(**Tu32)(__ccgo_up(bp + 24))>>libc.Int32FromInt32(18))
							v19 = iOut
							iOut = iOut + 1
							**(**int8)(__ccgo_up(zOut + uintptr(v19))) = int8(uint32(0x80) | **(**Tu32)(__ccgo_up(bp + 24))>>libc.Int32FromInt32(12)&uint32(0x3f))
							v19 = iOut
							iOut = iOut + 1
							**(**int8)(__ccgo_up(zOut + uintptr(v19))) = int8(uint32(0x80) | **(**Tu32)(__ccgo_up(bp + 24))>>libc.Int32FromInt32(6)&uint32(0x3f))
							v19 = iOut
							iOut = iOut + 1
							**(**int8)(__ccgo_up(zOut + uintptr(v19))) = int8(uint32(0x80) | **(**Tu32)(__ccgo_up(bp + 24))&uint32(0x3f))
						}
					}
				}
			}
			iIn = iIn + (szEscape - uint32(1))
		} else {
			v19 = iOut
			iOut = iOut + 1
			**(**int8)(__ccgo_up(zOut + uintptr(v19))) = c
		}
		goto _18
	_18:
		;
		iIn = iIn + 1
	} /* end for() */
	**(**int8)(__ccgo_up(zOut + uintptr(iOut))) = 0
	Xsqlite3_result_text(tls, pCtx, zOut, int32(iOut), __ccgo_fp(_sqlite3RowSetClear))
	goto _15
_13:
	;
_12:
	;
	if eMode == 0 {
		if int32(Xsqlite3_user_data(tls, pCtx))&int32(JSON_BLOB) != 0 {
			eMode = int32(2)
		} else {
			eMode = int32(1)
		}
	}
	if eMode == int32(2) {
		Xsqlite3_result_blob(tls, pCtx, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i), int32(**(**Tu32)(__ccgo_up(bp))+n), uintptr(-libc.Int32FromInt32(1)))
	} else {
		_jsonReturnTextJsonFromBlob(tls, pCtx, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i), **(**Tu32)(__ccgo_up(bp))+n)
	}
	goto _15
_14:
	;
	goto returnfromblob_malformed
_15:
	;
	return
	goto returnfromblob_oom
returnfromblob_oom:
	;
	Xsqlite3_result_error_nomem(tls, pCtx)
	return
	goto returnfromblob_malformed
returnfromblob_malformed:
	;
	Xsqlite3_result_error(tls, pCtx, __ccgo_ts+27699, -int32(1))
	return
}

// C documentation
//
//	/*
//	** Make the return value of a JSON function either the raw JSONB blob
//	** or make it JSON text, depending on whether the JSON_BLOB flag is
//	** set on the function.
//	*/
func _jsonReturnParse(tls *libc.TLS, ctx uintptr, p uintptr) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var flgs int32
	var _ /* s at bp+0 */ TJsonString
	_ = flgs
	if (*TJsonParse)(unsafe.Pointer(p)).Foom != 0 {
		Xsqlite3_result_error_nomem(tls, ctx)
		return
	}
	flgs = int32(Xsqlite3_user_data(tls, ctx))
	if flgs&int32(JSON_BLOB) != 0 {
		if (*TJsonParse)(unsafe.Pointer(p)).FnBlobAlloc > uint32(0) && !((*TJsonParse)(unsafe.Pointer(p)).FbReadOnly != 0) {
			Xsqlite3_result_blob(tls, ctx, (*TJsonParse)(unsafe.Pointer(p)).FaBlob, int32((*TJsonParse)(unsafe.Pointer(p)).FnBlob), __ccgo_fp(_sqlite3RowSetClear))
			(*TJsonParse)(unsafe.Pointer(p)).FnBlobAlloc = uint32(0)
		} else {
			Xsqlite3_result_blob(tls, ctx, (*TJsonParse)(unsafe.Pointer(p)).FaBlob, int32((*TJsonParse)(unsafe.Pointer(p)).FnBlob), uintptr(-libc.Int32FromInt32(1)))
		}
	} else {
		_jsonStringInit(tls, bp, ctx)
		(*TJsonParse)(unsafe.Pointer(p)).Fdelta = 0
		_jsonTranslateBlobToText(tls, p, uint32(0), bp)
		_jsonReturnString(tls, bp, p, ctx)
		Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
	}
}

/****************************************************************************
** SQL functions used for testing and debugging
****************************************************************************/

/****************************************************************************
** Scalar SQL function implementations
****************************************************************************/

// C documentation
//
//	/* Make the text in p (which is probably a generated JSON text string)
//	** the result of the SQL function.
//	**
//	** The JsonString is reset.
//	**
//	** If pParse and ctx are both non-NULL, then the SQL string in p is
//	** loaded into the zJson field of the pParse object as a RCStr and the
//	** pParse is added to the cache.
//	*/
func _jsonReturnString(tls *libc.TLS, p uintptr, pParse uintptr, ctx uintptr) {
	var flags, rc int32
	_, _ = flags, rc
	_jsonStringTerminate(tls, p)
	if int32((*TJsonString)(unsafe.Pointer(p)).FeErr) == 0 {
		flags = int32(Xsqlite3_user_data(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx))
		if flags&int32(JSON_BLOB) != 0 {
			_jsonReturnStringAsBlob(tls, p)
		} else {
			if (*TJsonString)(unsafe.Pointer(p)).FbStatic != 0 {
				Xsqlite3_result_text64(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx, (*TJsonString)(unsafe.Pointer(p)).FzBuf, (*TJsonString)(unsafe.Pointer(p)).FnUsed, uintptr(-libc.Int32FromInt32(1)), uint8(SQLITE_UTF8))
			} else {
				if pParse != 0 && int32((*TJsonParse)(unsafe.Pointer(pParse)).FbJsonIsRCStr) == 0 && (*TJsonParse)(unsafe.Pointer(pParse)).FnBlobAlloc > uint32(0) {
					(*TJsonParse)(unsafe.Pointer(pParse)).FzJson = _sqlite3RCStrRef(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf)
					(*TJsonParse)(unsafe.Pointer(pParse)).FnJson = int32((*TJsonString)(unsafe.Pointer(p)).FnUsed)
					(*TJsonParse)(unsafe.Pointer(pParse)).FbJsonIsRCStr = uint8(1)
					rc = _jsonCacheInsert(tls, ctx, pParse)
					if rc == int32(SQLITE_NOMEM) {
						Xsqlite3_result_error_nomem(tls, ctx)
						_jsonStringReset(tls, p)
						return
					}
				}
				Xsqlite3_result_text64(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx, _sqlite3RCStrRef(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf), (*TJsonString)(unsafe.Pointer(p)).FnUsed, __ccgo_fp(_sqlite3RCStrUnref), uint8(SQLITE_UTF8))
			}
		}
	} else {
		if int32((*TJsonString)(unsafe.Pointer(p)).FeErr)&int32(JSTRING_OOM) != 0 {
			Xsqlite3_result_error_nomem(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx)
		} else {
			if int32((*TJsonString)(unsafe.Pointer(p)).FeErr)&int32(JSTRING_TOODEEP) != 0 {
				/* error already in p->pCtx */
			} else {
				if int32((*TJsonString)(unsafe.Pointer(p)).FeErr)&int32(JSTRING_MALFORMED) != 0 {
					Xsqlite3_result_error(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx, __ccgo_ts+27699, -int32(1))
				}
			}
		}
	}
	_jsonStringReset(tls, p)
}

/**************************************************************************
** Utility routines for dealing with JsonParse objects
**************************************************************************/

// C documentation
//
//	/*
//	** The input string pStr is a well-formed JSON text string.  Convert
//	** this into the JSONB format and make it the return value of the
//	** SQL function.
//	*/
func _jsonReturnStringAsBlob(tls *libc.TLS, pStr uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var _ /* px at bp+0 */ TJsonParse
	libc.Xmemset(tls, bp, 0, uint32(56))
	(**(**TJsonParse)(__ccgo_up(bp))).FzJson = (*TJsonString)(unsafe.Pointer(pStr)).FzBuf
	(**(**TJsonParse)(__ccgo_up(bp))).FnJson = int32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed)
	(**(**TJsonParse)(__ccgo_up(bp))).Fdb = Xsqlite3_context_db_handle(tls, (*TJsonString)(unsafe.Pointer(pStr)).FpCtx)
	_jsonTranslateTextToBlob(tls, bp, uint32(0))
	if (**(**TJsonParse)(__ccgo_up(bp))).Foom != 0 {
		_sqlite3DbFree(tls, (**(**TJsonParse)(__ccgo_up(bp))).Fdb, (**(**TJsonParse)(__ccgo_up(bp))).FaBlob)
		Xsqlite3_result_error_nomem(tls, (*TJsonString)(unsafe.Pointer(pStr)).FpCtx)
	} else {
		Xsqlite3_result_blob(tls, (*TJsonString)(unsafe.Pointer(pStr)).FpCtx, (**(**TJsonParse)(__ccgo_up(bp))).FaBlob, int32((**(**TJsonParse)(__ccgo_up(bp))).FnBlob), __ccgo_fp(_sqlite3RowSetClear))
	}
}

// C documentation
//
//	/*
//	** json_set(JSON, PATH, VALUE, ...)
//	**
//	** Set the value at PATH to VALUE.  Create the PATH if it does not already
//	** exist.  Overwrite existing values that do exist.
//	** If JSON or PATH is malformed, throw an error.
//	**
//	** json_insert(JSON, PATH, VALUE, ...)
//	**
//	** Create PATH and initialize it to VALUE.  If PATH already exists, this
//	** routine is a no-op.  If JSON or PATH is malformed, throw an error.
//	*/
func _jsonSetFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	var eInsType, flags int32
	_, _ = eInsType, flags
	flags = int32(Xsqlite3_user_data(tls, ctx))
	eInsType = flags & int32(0xC) >> int32(2)
	if argc < int32(1) {
		return
	}
	if argc&int32(1) == 0 {
		_jsonWrongNumArgs(tls, ctx, _azInsType[eInsType])
		return
	}
	_jsonInsertIntoBlob(tls, ctx, argc, argv, int32(_aEditType[eInsType]))
}

// C documentation
//
//	/*
//	** If the cursor is currently pointing at the label of a object entry,
//	** then return the index of the value.  For all other cases, return the
//	** current pointer position, which is the value.
//	*/
func _jsonSkipLabel(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var n Tu32
	var _ /* sz at bp+0 */ Tu32
	_ = n
	if int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeType) == int32(JSONB_OBJECT) {
		**(**Tu32)(__ccgo_up(bp)) = uint32(0)
		n = _jsonbPayloadSize(tls, p+168, (*TJsonEachCursor)(unsafe.Pointer(p)).Fi, bp)
		return int32((*TJsonEachCursor)(unsafe.Pointer(p)).Fi + n + **(**Tu32)(__ccgo_up(bp)))
	} else {
		return int32((*TJsonEachCursor)(unsafe.Pointer(p)).Fi)
	}
	return r
}

// C documentation
//
//	/* Report JSON nested too deep
//	*/
func _jsonStringTooDeep(tls *libc.TLS, p uintptr) {
	var v1 uintptr
	_ = v1
	v1 = p + 25
	*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_TOODEEP))
	Xsqlite3_result_error(tls, (*TJsonString)(unsafe.Pointer(p)).FpCtx, __ccgo_ts+27649, -int32(1))
	_jsonStringReset(tls, p)
}

// C documentation
//
//	/*
//	** Translate the binary JSONB representation of JSON beginning at
//	** pParse->aBlob[i] into a JSON text string.  Append the JSON
//	** text onto the end of pOut.  Return the index in pParse->aBlob[]
//	** of the first byte past the end of the element that is translated.
//	**
//	** This is a variant of jsonTranslateBlobToText() that "pretty-prints"
//	** the output.  Extra whitespace is inserted to make the JSON easier
//	** for humans to read.
//	**
//	** If an error is detected in the BLOB input, the pOut->eErr flag
//	** might get set to JSTRING_MALFORMED.  But not all BLOB input errors
//	** are detected.  So a malformed JSONB input might either result
//	** in an error, or in incorrect JSON.
//	**
//	** The pOut->eErr JSTRING_OOM flag is set on a OOM.
//	*/
func _jsonTranslateBlobToPrettyText(tls *libc.TLS, pPretty uintptr, i Tu32) (r Tu32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iEnd, j, n Tu32
	var pOut, pParse, v1 uintptr
	var _ /* sz at bp+0 */ Tu32
	_, _, _, _, _, _ = iEnd, j, n, pOut, pParse, v1
	pParse = (*TJsonPretty)(unsafe.Pointer(pPretty)).FpParse
	pOut = (*TJsonPretty)(unsafe.Pointer(pPretty)).FpOut
	n = _jsonbPayloadSize(tls, pParse, i, bp)
	if n == uint32(0) {
		v1 = pOut + 25
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
		return (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob + uint32(1)
	}
	switch int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i)))) & libc.Int32FromInt32(0x0f) {
	case int32(JSONB_ARRAY):
		j = i + n
		iEnd = j + **(**Tu32)(__ccgo_up(bp))
		_jsonAppendChar(tls, pOut, int8('['))
		if j < iEnd {
			_jsonAppendChar(tls, pOut, int8('\n'))
			(*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent = (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent + 1
			if (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent >= uint32(JSON_MAX_DEPTH) {
				_jsonStringTooDeep(tls, pOut)
			}
			for int32((*TJsonString)(unsafe.Pointer(pOut)).FeErr) == 0 {
				_jsonPrettyIndent(tls, pPretty)
				j = _jsonTranslateBlobToPrettyText(tls, pPretty, j)
				if j >= iEnd {
					break
				}
				_jsonAppendRawNZ(tls, pOut, __ccgo_ts+27838, uint32(2))
			}
			_jsonAppendChar(tls, pOut, int8('\n'))
			(*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent = (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent - 1
			_jsonPrettyIndent(tls, pPretty)
		}
		_jsonAppendChar(tls, pOut, int8(']'))
		i = iEnd
	case int32(JSONB_OBJECT):
		j = i + n
		iEnd = j + **(**Tu32)(__ccgo_up(bp))
		_jsonAppendChar(tls, pOut, int8('{'))
		if j < iEnd {
			_jsonAppendChar(tls, pOut, int8('\n'))
			(*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent = (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent + 1
			if (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent >= uint32(JSON_MAX_DEPTH) {
				_jsonStringTooDeep(tls, pOut)
			}
			(*TJsonParse)(unsafe.Pointer(pParse)).FiDepth = uint16((*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent)
			for int32((*TJsonString)(unsafe.Pointer(pOut)).FeErr) == 0 {
				_jsonPrettyIndent(tls, pPretty)
				j = _jsonTranslateBlobToText(tls, pParse, j, pOut)
				if j > iEnd {
					v1 = pOut + 25
					*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
					break
				}
				_jsonAppendRawNZ(tls, pOut, __ccgo_ts+27841, uint32(2))
				j = _jsonTranslateBlobToPrettyText(tls, pPretty, j)
				if j >= iEnd {
					break
				}
				_jsonAppendRawNZ(tls, pOut, __ccgo_ts+27838, uint32(2))
			}
			_jsonAppendChar(tls, pOut, int8('\n'))
			(*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent = (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent - 1
			_jsonPrettyIndent(tls, pPretty)
		}
		_jsonAppendChar(tls, pOut, int8('}'))
		i = iEnd
	default:
		i = _jsonTranslateBlobToText(tls, pParse, i, pOut)
		break
	}
	return i
}

// C documentation
//
//	/*
//	** Translate the binary JSONB representation of JSON beginning at
//	** pParse->aBlob[i] into a JSON text string.  Append the JSON
//	** text onto the end of pOut.  Return the index in pParse->aBlob[]
//	** of the first byte past the end of the element that is translated.
//	**
//	** If an error is detected in the BLOB input, the pOut->eErr flag
//	** might get set to JSTRING_MALFORMED.  But not all BLOB input errors
//	** are detected.  So a malformed JSONB input might either result
//	** in an error, or in incorrect JSON.
//	**
//	** The pOut->eErr JSTRING_OOM flag is set on a OOM.
//	*/
func _jsonTranslateBlobToText(tls *libc.TLS, pParse uintptr, i Tu32, pOut uintptr) (r Tu32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bOverflow, x, v30, v31 int32
	var iEnd, j, k, k1, k2, n, sz2 Tu32
	var u Tsqlite3_uint64
	var zIn, zIn1, zIn2, v1 uintptr
	var v25 Tu16
	var _ /* sz at bp+0 */ Tu32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bOverflow, iEnd, j, k, k1, k2, n, sz2, u, x, zIn, zIn1, zIn2, v1, v25, v30, v31
	n = _jsonbPayloadSize(tls, pParse, i, bp)
	if n == uint32(0) {
		v1 = pOut + 25
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
		return (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob + uint32(1)
	}
	switch int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i)))) & libc.Int32FromInt32(0x0f) {
	case JSONB_NULL:
		goto _2
	case int32(JSONB_TRUE):
		goto _3
	case int32(JSONB_FALSE):
		goto _4
	case int32(JSONB_FLOAT):
		goto _5
	case int32(JSONB_INT):
		goto _6
	case int32(JSONB_INT5):
		goto _7
	case int32(JSONB_FLOAT5):
		goto _8
	case int32(JSONB_TEXTJ):
		goto _9
	case int32(JSONB_TEXT):
		goto _10
	case int32(JSONB_TEXT5):
		goto _11
	case int32(JSONB_TEXTRAW):
		goto _12
	case int32(JSONB_ARRAY):
		goto _13
	case int32(JSONB_OBJECT):
		goto _14
	default:
		goto _15
	}
	goto _16
_2:
	;
	_jsonAppendRawNZ(tls, pOut, __ccgo_ts+1697, uint32(4))
	return i + uint32(1)
_3:
	;
	_jsonAppendRawNZ(tls, pOut, __ccgo_ts+9395, uint32(4))
	return i + uint32(1)
_4:
	;
	_jsonAppendRawNZ(tls, pOut, __ccgo_ts+9400, uint32(5))
	return i + uint32(1)
_6:
	;
_5:
	;
	if **(**Tu32)(__ccgo_up(bp)) == uint32(0) {
		goto malformed_jsonb
	}
	_jsonAppendRaw(tls, pOut, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), **(**Tu32)(__ccgo_up(bp)))
	goto _16
_7:
	; /* Integer literal in hexadecimal notation */
	k = uint32(2)
	u = uint64(0)
	zIn = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n)
	bOverflow = 0
	if **(**Tu32)(__ccgo_up(bp)) == uint32(0) {
		goto malformed_jsonb
	}
	if int32(**(**int8)(__ccgo_up(zIn))) == int32('-') {
		_jsonAppendChar(tls, pOut, int8('-'))
		k = k + 1
	} else {
		if int32(**(**int8)(__ccgo_up(zIn))) == int32('+') {
			k = k + 1
		}
	}
	for {
		if !(k < **(**Tu32)(__ccgo_up(bp))) {
			break
		}
		if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zIn + uintptr(k))))])&libc.Int32FromInt32(0x08) != 0) {
			v1 = pOut + 25
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
			break
		} else {
			if u>>libc.Int32FromInt32(60) != uint64(0) {
				bOverflow = int32(1)
			} else {
				u = u*uint64(16) + uint64(_sqlite3HexToInt(tls, int32(**(**int8)(__ccgo_up(zIn + uintptr(k))))))
			}
		}
		goto _17
	_17:
		;
		k = k + 1
	}
	if bOverflow != 0 {
		v1 = __ccgo_ts + 27718
	} else {
		v1 = __ccgo_ts + 14221
	}
	_jsonPrintf(tls, int32(100), pOut, v1, libc.VaList(bp+16, u))
	goto _16
_8:
	; /* Float literal missing digits beside "." */
	k1 = uint32(0)
	zIn1 = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n)
	if **(**Tu32)(__ccgo_up(bp)) == uint32(0) {
		goto malformed_jsonb
	}
	if int32(**(**int8)(__ccgo_up(zIn1))) == int32('-') {
		_jsonAppendChar(tls, pOut, int8('-'))
		k1 = k1 + 1
	}
	if int32(**(**int8)(__ccgo_up(zIn1 + uintptr(k1)))) == int32('.') {
		_jsonAppendChar(tls, pOut, int8('0'))
	}
	for {
		if !(k1 < **(**Tu32)(__ccgo_up(bp))) {
			break
		}
		_jsonAppendChar(tls, pOut, **(**int8)(__ccgo_up(zIn1 + uintptr(k1))))
		if int32(**(**int8)(__ccgo_up(zIn1 + uintptr(k1)))) == int32('.') && (k1+uint32(1) == **(**Tu32)(__ccgo_up(bp)) || !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zIn1 + uintptr(k1+uint32(1)))))])&libc.Int32FromInt32(0x04) != 0)) {
			_jsonAppendChar(tls, pOut, int8('0'))
		}
		goto _20
	_20:
		;
		k1 = k1 + 1
	}
	goto _16
_10:
	;
_9:
	;
	if (*TJsonString)(unsafe.Pointer(pOut)).FnUsed+uint64(**(**Tu32)(__ccgo_up(bp)))+uint64(2) <= (*TJsonString)(unsafe.Pointer(pOut)).FnAlloc || _jsonStringGrow(tls, pOut, **(**Tu32)(__ccgo_up(bp))+uint32(2)) == 0 {
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pOut)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(pOut)).FnUsed))) = int8('"')
		libc.Xmemcpy(tls, (*TJsonString)(unsafe.Pointer(pOut)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(pOut)).FnUsed)+uintptr(1), (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), **(**Tu32)(__ccgo_up(bp)))
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(pOut)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(pOut)).FnUsed+uint64(**(**Tu32)(__ccgo_up(bp)))+uint64(1)))) = int8('"')
		**(**Tu64)(__ccgo_up(pOut + 16)) += uint64(**(**Tu32)(__ccgo_up(bp)) + uint32(2))
	}
	goto _16
_11:
	;
	sz2 = **(**Tu32)(__ccgo_up(bp))
	zIn2 = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n)
	_jsonAppendChar(tls, pOut, int8('"'))
	for sz2 > uint32(0) {
		k2 = uint32(0)
		for {
			if !(k2 < sz2 && (_jsonIsOk[uint8(**(**int8)(__ccgo_up(zIn2 + uintptr(k2))))] != 0 || int32(**(**int8)(__ccgo_up(zIn2 + uintptr(k2)))) == int32('\''))) {
				break
			}
			goto _21
		_21:
			;
			k2 = k2 + 1
		}
		if k2 > uint32(0) {
			_jsonAppendRawNZ(tls, pOut, zIn2, k2)
			if k2 >= sz2 {
				break
			}
			zIn2 = zIn2 + uintptr(k2)
			sz2 = sz2 - k2
		}
		if int32(**(**int8)(__ccgo_up(zIn2))) == int32('"') {
			_jsonAppendRawNZ(tls, pOut, __ccgo_ts+27816, uint32(2))
			zIn2 = zIn2 + 1
			sz2 = sz2 - 1
			continue
		}
		if int32(**(**int8)(__ccgo_up(zIn2))) <= int32(0x1f) {
			if (*TJsonString)(unsafe.Pointer(pOut)).FnUsed+uint64(7) > (*TJsonString)(unsafe.Pointer(pOut)).FnAlloc && _jsonStringGrow(tls, pOut, uint32(7)) != 0 {
				break
			}
			_jsonAppendControlChar(tls, pOut, uint8(**(**int8)(__ccgo_up(zIn2))))
			zIn2 = zIn2 + 1
			sz2 = sz2 - 1
			continue
		}
		if sz2 < uint32(2) {
			v1 = pOut + 25
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
			break
		}
		switch int32(uint8(**(**int8)(__ccgo_up(zIn2 + 1)))) {
		case int32('\''):
			_jsonAppendChar(tls, pOut, int8('\''))
		case int32('v'):
			_jsonAppendRawNZ(tls, pOut, __ccgo_ts+27819, uint32(6))
		case int32('x'):
			if sz2 < uint32(4) {
				v1 = pOut + 25
				*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
				sz2 = uint32(2)
				break
			}
			_jsonAppendRawNZ(tls, pOut, __ccgo_ts+27826, uint32(4))
			_jsonAppendRawNZ(tls, pOut, zIn2+2, uint32(2))
			zIn2 = zIn2 + uintptr(2)
			sz2 = sz2 - uint32(2)
		case int32('0'):
			_jsonAppendRawNZ(tls, pOut, __ccgo_ts+27831, uint32(6))
		case int32('\r'):
			if sz2 > uint32(2) && int32(**(**int8)(__ccgo_up(zIn2 + 2))) == int32('\n') {
				zIn2 = zIn2 + 1
				sz2 = sz2 - 1
			}
		case int32('\n'):
		case int32(0xe2):
			/* '\' followed by either U+2028 or U+2029 is ignored as
			 ** whitespace.  Not that in UTF8, U+2028 is 0xe2 0x80 0x29.
			 ** U+2029 is the same except for the last byte */
			if sz2 < uint32(4) || int32(0x80) != int32(uint8(**(**int8)(__ccgo_up(zIn2 + 2)))) || int32(0xa8) != int32(uint8(**(**int8)(__ccgo_up(zIn2 + 3)))) && int32(0xa9) != int32(uint8(**(**int8)(__ccgo_up(zIn2 + 3)))) {
				v1 = pOut + 25
				*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
				sz2 = uint32(2)
				break
			}
			zIn2 = zIn2 + uintptr(2)
			sz2 = sz2 - uint32(2)
		default:
			_jsonAppendRawNZ(tls, pOut, zIn2, uint32(2))
			break
		}
		zIn2 = zIn2 + uintptr(2)
		sz2 = sz2 - uint32(2)
	}
	_jsonAppendChar(tls, pOut, int8('"'))
	goto _16
_12:
	;
	_jsonAppendString(tls, pOut, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), **(**Tu32)(__ccgo_up(bp)))
	goto _16
_13:
	;
	_jsonAppendChar(tls, pOut, int8('['))
	j = i + n
	iEnd = j + **(**Tu32)(__ccgo_up(bp))
	v1 = pParse + 32
	*(*Tu16)(unsafe.Pointer(v1)) = *(*Tu16)(unsafe.Pointer(v1)) + 1
	v25 = *(*Tu16)(unsafe.Pointer(v1))
	if int32(v25) > int32(JSON_MAX_DEPTH) {
		_jsonStringTooDeep(tls, pOut)
	}
	for j < iEnd && int32((*TJsonString)(unsafe.Pointer(pOut)).FeErr) == 0 {
		j = _jsonTranslateBlobToText(tls, pParse, j, pOut)
		_jsonAppendChar(tls, pOut, int8(','))
	}
	(*TJsonParse)(unsafe.Pointer(pParse)).FiDepth = (*TJsonParse)(unsafe.Pointer(pParse)).FiDepth - 1
	if j > iEnd {
		v1 = pOut + 25
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
	}
	if **(**Tu32)(__ccgo_up(bp)) > uint32(0) {
		_jsonStringTrimOneChar(tls, pOut)
	}
	_jsonAppendChar(tls, pOut, int8(']'))
	goto _16
_14:
	;
	x = 0
	_jsonAppendChar(tls, pOut, int8('{'))
	j = i + n
	iEnd = j + **(**Tu32)(__ccgo_up(bp))
	v1 = pParse + 32
	*(*Tu16)(unsafe.Pointer(v1)) = *(*Tu16)(unsafe.Pointer(v1)) + 1
	v25 = *(*Tu16)(unsafe.Pointer(v1))
	if int32(v25) > int32(JSON_MAX_DEPTH) {
		_jsonStringTooDeep(tls, pOut)
	}
	for j < iEnd && int32((*TJsonString)(unsafe.Pointer(pOut)).FeErr) == 0 {
		j = _jsonTranslateBlobToText(tls, pParse, j, pOut)
		v31 = x
		x = x + 1
		if v31&int32(1) != 0 {
			v30 = int32(',')
		} else {
			v30 = int32(':')
		}
		_jsonAppendChar(tls, pOut, int8(v30))
	}
	(*TJsonParse)(unsafe.Pointer(pParse)).FiDepth = (*TJsonParse)(unsafe.Pointer(pParse)).FiDepth - 1
	if x&int32(1) != 0 || j > iEnd {
		v1 = pOut + 25
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
	}
	if **(**Tu32)(__ccgo_up(bp)) > uint32(0) {
		_jsonStringTrimOneChar(tls, pOut)
	}
	_jsonAppendChar(tls, pOut, int8('}'))
	goto _16
_15:
	;
	goto malformed_jsonb
malformed_jsonb:
	;
	v1 = pOut + 25
	*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JSTRING_MALFORMED))
	goto _16
_16:
	;
	return i + n + **(**Tu32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Translate a single element of JSON text at pParse->zJson[i] into
//	** its equivalent binary JSONB representation.  Append the translation into
//	** pParse->aBlob[] beginning at pParse->nBlob.  The size of
//	** pParse->aBlob[] is increased as necessary.
//	**
//	** Return the index of the first character past the end of the element parsed,
//	** or one of the following special result codes:
//	**
//	**      0    End of input
//	**     -1    Syntax error or OOM
//	**     -2    '}' seen   **     -3    ']' seen    \___  For these returns, pParse->iErr is set to
//	**     -4    ',' seen    /     the index in zJson[] of the seen character
//	**     -5    ':' seen   /
//	*/
func _jsonTranslateTextToBlob(tls *libc.TLS, pParse uintptr, i Tu32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var c, cDelim int8
	var iBlob, iStart, iThis, j, k1, v46 Tu32
	var k, nn, x, v48 int32
	var opcode, seenE, t Tu8
	var z, v41 uintptr
	var v40 Tu16
	var _ /* op at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, cDelim, iBlob, iStart, iThis, j, k, k1, nn, opcode, seenE, t, x, z, v40, v41, v46, v48
	z = (*TJsonParse)(unsafe.Pointer(pParse)).FzJson
	goto json_parse_restart
json_parse_restart:
	;
	switch int32(uint8(**(**int8)(__ccgo_up(z + uintptr(i))))) {
	case int32('{'):
		goto _1
	case int32('['):
		goto _2
	case int32('"'):
		goto _3
	case int32('\''):
		goto _4
	case int32('t'):
		goto _5
	case int32('f'):
		goto _6
	case int32('.'):
		goto _7
	case int32('9'):
		goto _8
	case int32('8'):
		goto _9
	case int32('7'):
		goto _10
	case int32('6'):
		goto _11
	case int32('5'):
		goto _12
	case int32('4'):
		goto _13
	case int32('3'):
		goto _14
	case int32('2'):
		goto _15
	case int32('1'):
		goto _16
	case int32('0'):
		goto _17
	case int32('-'):
		goto _18
	case int32('+'):
		goto _19
	case int32('}'):
		goto _20
	case int32(']'):
		goto _21
	case int32(','):
		goto _22
	case int32(':'):
		goto _23
	case 0:
		goto _24
	case int32(0x20):
		goto _25
	case int32(0x0d):
		goto _26
	case int32(0x0a):
		goto _27
	case int32(0x09):
		goto _28
	case int32(0xef):
		goto _29
	case int32(0xe3):
		goto _30
	case int32(0xe2):
		goto _31
	case int32(0xe1):
		goto _32
	case int32(0xc2):
		goto _33
	case int32('/'):
		goto _34
	case int32(0x0c):
		goto _35
	case int32(0x0b):
		goto _36
	case int32('n'):
		goto _37
	default:
		goto _38
	}
	goto _39
_1:
	;
	/* Parse object */
	iThis = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob
	_jsonBlobAppendNode(tls, pParse, uint8(JSONB_OBJECT), uint64(uint32((*TJsonParse)(unsafe.Pointer(pParse)).FnJson)-i), uintptr(0))
	v41 = pParse + 32
	*(*Tu16)(unsafe.Pointer(v41)) = *(*Tu16)(unsafe.Pointer(v41)) + 1
	v40 = *(*Tu16)(unsafe.Pointer(v41))
	if int32(v40) > int32(JSON_MAX_DEPTH) {
		(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
		return -int32(1)
	}
	iStart = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob
	j = i + uint32(1)
	for {
		iBlob = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob
		x = _jsonTranslateTextToBlob(tls, pParse, j)
		if x <= 0 {
			if x == -int32(2) {
				j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr
				if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob != iStart {
					(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
				}
				break
			}
			j = j + uint32(_json5Whitespace(tls, z+uintptr(j)))
			**(**int32)(__ccgo_up(bp)) = int32(JSONB_TEXT)
			if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(j))))])&int32(0x42) != 0 || int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32('\\') && _jsonIs4HexB(tls, z+uintptr(j+uint32(1)), bp) != 0 {
				k = int32(j + uint32(1))
				for int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(k))))])&int32(0x46) != 0 && _json5Whitespace(tls, z+uintptr(k)) == 0 || int32(**(**int8)(__ccgo_up(z + uintptr(k)))) == int32('\\') && _jsonIs4HexB(tls, z+uintptr(k+int32(1)), bp) != 0 {
					k = k + 1
				}
				_jsonBlobAppendNode(tls, pParse, uint8(**(**int32)(__ccgo_up(bp))), uint64(uint32(k)-j), z+uintptr(j))
				(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
				x = k
			} else {
				if x != -int32(1) {
					(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
				}
				return -int32(1)
			}
		}
		if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 {
			return -int32(1)
		}
		t = uint8(int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(iBlob)))) & int32(0x0f))
		if int32(t) < int32(JSONB_TEXT) || int32(t) > int32(JSONB_TEXTRAW) {
			(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
			return -int32(1)
		}
		j = uint32(x)
		if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32(':') {
			j = j + 1
		} else {
			if _jsonIsSpace[uint8(**(**int8)(__ccgo_up(z + uintptr(j))))] != 0 {
				/* strspn() is not helpful here */
				for cond := true; cond; cond = _jsonIsSpace[uint8(**(**int8)(__ccgo_up(z + uintptr(j))))] != 0 {
					j = j + 1
				}
				if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32(':') {
					j = j + 1
					goto parse_object_value
				}
			}
			x = _jsonTranslateTextToBlob(tls, pParse, j)
			if x != -int32(5) {
				if x != -int32(1) {
					(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
				}
				return -int32(1)
			}
			j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr + uint32(1)
		}
		goto parse_object_value
	parse_object_value:
		;
		x = _jsonTranslateTextToBlob(tls, pParse, j)
		if x <= 0 {
			if x != -int32(1) {
				(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
			}
			return -int32(1)
		}
		j = uint32(x)
		if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32(',') {
			goto _42
		} else {
			if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32('}') {
				break
			} else {
				if _jsonIsSpace[uint8(**(**int8)(__ccgo_up(z + uintptr(j))))] != 0 {
					j = j + (uint32(1) + libc.Xstrspn(tls, z+uintptr(j+uint32(1)), uintptr(unsafe.Pointer(&_jsonSpaces))))
					if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32(',') {
						goto _42
					} else {
						if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32('}') {
							break
						}
					}
				}
				x = _jsonTranslateTextToBlob(tls, pParse, j)
				if x == -int32(4) {
					j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr
					goto _42
				}
				if x == -int32(2) {
					j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr
					break
				}
			}
		}
		(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
		return -int32(1)
		goto _42
	_42:
		;
		j = j + 1
	}
	_jsonBlobChangePayloadSize(tls, pParse, iThis, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-iStart)
	(*TJsonParse)(unsafe.Pointer(pParse)).FiDepth = (*TJsonParse)(unsafe.Pointer(pParse)).FiDepth - 1
	return int32(j + uint32(1))
_2:
	;
	/* Parse array */
	iThis = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob
	_jsonBlobAppendNode(tls, pParse, uint8(JSONB_ARRAY), uint64(uint32((*TJsonParse)(unsafe.Pointer(pParse)).FnJson)-i), uintptr(0))
	iStart = (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob
	if (*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0 {
		return -int32(1)
	}
	v41 = pParse + 32
	*(*Tu16)(unsafe.Pointer(v41)) = *(*Tu16)(unsafe.Pointer(v41)) + 1
	v40 = *(*Tu16)(unsafe.Pointer(v41))
	if int32(v40) > int32(JSON_MAX_DEPTH) {
		(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
		return -int32(1)
	}
	j = i + uint32(1)
	for {
		x = _jsonTranslateTextToBlob(tls, pParse, j)
		if x <= 0 {
			if x == -int32(3) {
				j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr
				if (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob != iStart {
					(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
				}
				break
			}
			if x != -int32(1) {
				(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
			}
			return -int32(1)
		}
		j = uint32(x)
		if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32(',') {
			goto _45
		} else {
			if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32(']') {
				break
			} else {
				if _jsonIsSpace[uint8(**(**int8)(__ccgo_up(z + uintptr(j))))] != 0 {
					j = j + (uint32(1) + libc.Xstrspn(tls, z+uintptr(j+uint32(1)), uintptr(unsafe.Pointer(&_jsonSpaces))))
					if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32(',') {
						goto _45
					} else {
						if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) == int32(']') {
							break
						}
					}
				}
				x = _jsonTranslateTextToBlob(tls, pParse, j)
				if x == -int32(4) {
					j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr
					goto _45
				}
				if x == -int32(3) {
					j = (*TJsonParse)(unsafe.Pointer(pParse)).FiErr
					break
				}
			}
		}
		(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
		return -int32(1)
		goto _45
	_45:
		;
		j = j + 1
	}
	_jsonBlobChangePayloadSize(tls, pParse, iThis, (*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-iStart)
	(*TJsonParse)(unsafe.Pointer(pParse)).FiDepth = (*TJsonParse)(unsafe.Pointer(pParse)).FiDepth - 1
	return int32(j + uint32(1))
_4:
	;
	(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
	opcode = uint8(JSONB_TEXT)
	goto parse_string
_3:
	;
	/* Parse string */
	opcode = uint8(JSONB_TEXT)
	goto parse_string
parse_string:
	;
	cDelim = **(**int8)(__ccgo_up(z + uintptr(i)))
	j = i + uint32(1)
	for int32(1) != 0 {
		if _jsonIsOk[uint8(**(**int8)(__ccgo_up(z + uintptr(j))))] != 0 {
			if !(_jsonIsOk[uint8(**(**int8)(__ccgo_up(z + uintptr(j+uint32(1)))))] != 0) {
				j = j + uint32(1)
			} else {
				if !(_jsonIsOk[uint8(**(**int8)(__ccgo_up(z + uintptr(j+uint32(2)))))] != 0) {
					j = j + uint32(2)
				} else {
					j = j + uint32(3)
					continue
				}
			}
		}
		c = **(**int8)(__ccgo_up(z + uintptr(j)))
		if int32(c) == int32(cDelim) {
			break
		} else {
			if int32(c) == int32('\\') {
				j = j + 1
				v46 = j
				c = **(**int8)(__ccgo_up(z + uintptr(v46)))
				if int32(c) == int32('"') || int32(c) == int32('\\') || int32(c) == int32('/') || int32(c) == int32('b') || int32(c) == int32('f') || int32(c) == int32('n') || int32(c) == int32('r') || int32(c) == int32('t') || int32(c) == int32('u') && _jsonIs4Hex(tls, z+uintptr(j+uint32(1))) != 0 {
					if int32(opcode) == int32(JSONB_TEXT) {
						opcode = uint8(JSONB_TEXTJ)
					}
				} else {
					if int32(c) == int32('\'') || int32(c) == int32('v') || int32(c) == int32('\n') || int32(c) == int32('0') && !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(j+uint32(1)))))])&libc.Int32FromInt32(0x04) != 0) || int32(0xe2) == int32(uint8(c)) && int32(0x80) == int32(uint8(**(**int8)(__ccgo_up(z + uintptr(j+uint32(1)))))) && (int32(0xa8) == int32(uint8(**(**int8)(__ccgo_up(z + uintptr(j+uint32(2)))))) || int32(0xa9) == int32(uint8(**(**int8)(__ccgo_up(z + uintptr(j+uint32(2))))))) || int32(c) == int32('x') && _jsonIs2Hex(tls, z+uintptr(j+uint32(1))) != 0 {
						opcode = uint8(JSONB_TEXT5)
						(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
					} else {
						if int32(c) == int32('\r') {
							if int32(**(**int8)(__ccgo_up(z + uintptr(j+uint32(1))))) == int32('\n') {
								j = j + 1
							}
							opcode = uint8(JSONB_TEXT5)
							(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
						} else {
							(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
							return -int32(1)
						}
					}
				}
			} else {
				if int32(c) <= int32(0x1f) {
					if int32(c) == 0 {
						(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
						return -int32(1)
					}
					/* Control characters are not allowed in canonical JSON string
					 ** literals, but are allowed in JSON5 string literals. */
					opcode = uint8(JSONB_TEXT5)
					(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
				} else {
					if int32(c) == int32('"') {
						opcode = uint8(JSONB_TEXT5)
					}
				}
			}
		}
		j = j + 1
	}
	_jsonBlobAppendNode(tls, pParse, opcode, uint64(j-uint32(1)-i), z+uintptr(i+uint32(1)))
	return int32(j + uint32(1))
_5:
	;
	if libc.Xstrncmp(tls, z+uintptr(i), __ccgo_ts+9395, uint32(4)) == 0 && !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(4)))))])&libc.Int32FromInt32(0x06) != 0) {
		_jsonBlobAppendOneByte(tls, pParse, uint8(JSONB_TRUE))
		return int32(i + uint32(4))
	}
	(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
	return -int32(1)
_6:
	;
	if libc.Xstrncmp(tls, z+uintptr(i), __ccgo_ts+9400, uint32(5)) == 0 && !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(5)))))])&libc.Int32FromInt32(0x06) != 0) {
		_jsonBlobAppendOneByte(tls, pParse, uint8(JSONB_FALSE))
		return int32(i + uint32(5))
	}
	(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
	return -int32(1)
_19:
	;
	(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
	t = uint8(0x00) /* Bit 0x01:  JSON5.   Bit 0x02:  FLOAT */
	goto parse_number
_7:
	;
	if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(1)))))])&int32(0x04) != 0 {
		(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
		t = uint8(0x03) /* Bit 0x01:  JSON5.   Bit 0x02:  FLOAT */
		seenE = uint8(0)
		goto parse_number_2
	}
	(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
	return -int32(1)
_18:
	;
_17:
	;
_16:
	;
_15:
	;
_14:
	;
_13:
	;
_12:
	;
_11:
	;
_10:
	;
_9:
	;
_8:
	;
	/* Parse number */
	t = uint8(0x00) /* Bit 0x01:  JSON5.   Bit 0x02:  FLOAT */
	goto parse_number
parse_number:
	;
	seenE = uint8(0)
	c = **(**int8)(__ccgo_up(z + uintptr(i)))
	if int32(c) <= int32('0') {
		if int32(c) == int32('0') {
			if (int32(**(**int8)(__ccgo_up(z + uintptr(i+uint32(1))))) == int32('x') || int32(**(**int8)(__ccgo_up(z + uintptr(i+uint32(1))))) == int32('X')) && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(2)))))])&int32(0x08) != 0 {
				(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
				t = uint8(0x01)
				j = i + uint32(3)
				for {
					if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(j))))])&int32(0x08) != 0) {
						break
					}
					goto _47
				_47:
					;
					j = j + 1
				}
				goto parse_number_finish
			} else {
				if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(1)))))])&int32(0x04) != 0 {
					(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i + uint32(1)
					return -int32(1)
				}
			}
		} else {
			if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(1)))))])&libc.Int32FromInt32(0x04) != 0) {
				/* JSON5 allows for "+Infinity" and "-Infinity" using exactly
				 ** that case.  SQLite also allows these in any case and it allows
				 ** "+inf" and "-inf". */
				if (int32(**(**int8)(__ccgo_up(z + uintptr(i+uint32(1))))) == int32('I') || int32(**(**int8)(__ccgo_up(z + uintptr(i+uint32(1))))) == int32('i')) && Xsqlite3_strnicmp(tls, z+uintptr(i+uint32(1)), __ccgo_ts+27714, int32(3)) == 0 {
					(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
					if int32(**(**int8)(__ccgo_up(z + uintptr(i)))) == int32('-') {
						_jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint64(6), __ccgo_ts+27797)
					} else {
						_jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint64(5), __ccgo_ts+27804)
					}
					if Xsqlite3_strnicmp(tls, z+uintptr(i+uint32(4)), __ccgo_ts+27810, int32(5)) == 0 {
						v48 = int32(9)
					} else {
						v48 = int32(4)
					}
					return int32(i + uint32(v48))
				}
				if int32(**(**int8)(__ccgo_up(z + uintptr(i+uint32(1))))) == int32('.') {
					(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
					t = uint8(int32(t) | libc.Int32FromInt32(0x01))
					goto parse_number_2
				}
				(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
				return -int32(1)
			}
			if int32(**(**int8)(__ccgo_up(z + uintptr(i+uint32(1))))) == int32('0') {
				if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(2)))))])&int32(0x04) != 0 {
					(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i + uint32(1)
					return -int32(1)
				} else {
					if (int32(**(**int8)(__ccgo_up(z + uintptr(i+uint32(2))))) == int32('x') || int32(**(**int8)(__ccgo_up(z + uintptr(i+uint32(2))))) == int32('X')) && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(3)))))])&int32(0x08) != 0 {
						(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
						t = uint8(int32(t) | libc.Int32FromInt32(0x01))
						j = i + uint32(4)
						for {
							if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(j))))])&int32(0x08) != 0) {
								break
							}
							goto _49
						_49:
							;
							j = j + 1
						}
						goto parse_number_finish
					}
				}
			}
		}
	}
	goto parse_number_2
parse_number_2:
	;
	j = i + uint32(1)
	for {
		c = **(**int8)(__ccgo_up(z + uintptr(j)))
		if int32(_sqlite3CtypeMap[uint8(c)])&int32(0x04) != 0 {
			goto _50
		}
		if int32(c) == int32('.') {
			if int32(t)&int32(0x02) != 0 {
				(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
				return -int32(1)
			}
			t = uint8(int32(t) | libc.Int32FromInt32(0x02))
			goto _50
		}
		if int32(c) == int32('e') || int32(c) == int32('E') {
			if int32(**(**int8)(__ccgo_up(z + uintptr(j-uint32(1))))) < int32('0') {
				if int32(**(**int8)(__ccgo_up(z + uintptr(j-uint32(1))))) == int32('.') && j-uint32(2) >= i && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(j-uint32(2)))))])&int32(0x04) != 0 {
					(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
					t = uint8(int32(t) | libc.Int32FromInt32(0x01))
				} else {
					(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
					return -int32(1)
				}
			}
			if seenE != 0 {
				(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
				return -int32(1)
			}
			t = uint8(int32(t) | libc.Int32FromInt32(0x02))
			seenE = uint8(1)
			c = **(**int8)(__ccgo_up(z + uintptr(j+uint32(1))))
			if int32(c) == int32('+') || int32(c) == int32('-') {
				j = j + 1
				c = **(**int8)(__ccgo_up(z + uintptr(j+uint32(1))))
			}
			if int32(c) < int32('0') || int32(c) > int32('9') {
				(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
				return -int32(1)
			}
			goto _50
		}
		break
		goto _50
	_50:
		;
		j = j + 1
	}
	if int32(**(**int8)(__ccgo_up(z + uintptr(j-uint32(1))))) < int32('0') {
		if int32(**(**int8)(__ccgo_up(z + uintptr(j-uint32(1))))) == int32('.') && j-uint32(2) >= i && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(j-uint32(2)))))])&int32(0x04) != 0 {
			(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
			t = uint8(int32(t) | libc.Int32FromInt32(0x01))
		} else {
			(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j
			return -int32(1)
		}
	}
	goto parse_number_finish
parse_number_finish:
	;
	if int32(**(**int8)(__ccgo_up(z + uintptr(i)))) == int32('+') {
		i = i + 1
	}
	_jsonBlobAppendNode(tls, pParse, uint8(int32(JSONB_INT)+int32(t)), uint64(j-i), z+uintptr(i))
	return int32(j)
_20:
	;
	(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
	return -int32(2) /* End of {...} */
_21:
	;
	(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
	return -int32(3) /* End of [...] */
_22:
	;
	(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
	return -int32(4) /* List separator */
_23:
	;
	(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
	return -int32(5) /* Object label/value separator */
_24:
	;
	return 0 /* End of file */
_28:
	;
_27:
	;
_26:
	;
_25:
	;
	i = i + (uint32(1) + libc.Xstrspn(tls, z+uintptr(i+uint32(1)), uintptr(unsafe.Pointer(&_jsonSpaces))))
	goto json_parse_restart
_36:
	;
_35:
	;
_34:
	;
_33:
	;
_32:
	;
_31:
	;
_30:
	;
_29:
	;
	j = uint32(_json5Whitespace(tls, z+uintptr(i)))
	if j > uint32(0) {
		i = i + j
		(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
		goto json_parse_restart
	}
	(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
	return -int32(1)
_37:
	;
	if libc.Xstrncmp(tls, z+uintptr(i), __ccgo_ts+1697, uint32(4)) == 0 && !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(4)))))])&libc.Int32FromInt32(0x06) != 0) {
		_jsonBlobAppendOneByte(tls, pParse, uint8(JSONB_NULL))
		return int32(i + uint32(4))
	}
	/* fall-through into the default case that checks for NaN */
_38:
	;
	c = **(**int8)(__ccgo_up(z + uintptr(i)))
	k1 = uint32(0)
	for {
		if !(k1 < libc.Uint32FromInt64(80)/libc.Uint32FromInt64(16)) {
			break
		}
		if int32(c) != int32(_aNanInfName[k1].Fc1) && int32(c) != int32(_aNanInfName[k1].Fc2) {
			goto _51
		}
		nn = int32(_aNanInfName[k1].Fn)
		if Xsqlite3_strnicmp(tls, z+uintptr(i), _aNanInfName[k1].FzMatch, nn) != 0 {
			goto _51
		}
		if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(i+uint32(nn)))))])&int32(0x06) != 0 {
			goto _51
		}
		if int32(_aNanInfName[k1].FeType) == int32(JSONB_FLOAT) {
			_jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint64(5), __ccgo_ts+27804)
		} else {
			_jsonBlobAppendOneByte(tls, pParse, uint8(JSONB_NULL))
		}
		(*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1)
		return int32(i + uint32(nn))
		goto _51
	_51:
		;
		k1 = k1 + 1
	}
	(*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i
	return -int32(1) /* Syntax error */
_39:
	; /* End switch(z[i]) */
	return r
}

// C documentation
//
//	/*
//	** json_type(JSON)
//	** json_type(JSON, PATH)
//	**
//	** Return the top-level "type" of a JSON string.  json_type() raises an
//	** error if either the JSON or PATH inputs are not well-formed.
//	*/
func _jsonTypeFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	var i Tu32
	var p, zPath uintptr
	_, _, _ = i, p, zPath /* The parse */
	zPath = uintptr(0)
	p = _jsonParseFuncArg(tls, ctx, **(**uintptr)(__ccgo_up(argv)), uint32(0))
	if p == uintptr(0) {
		return
	}
	if argc == int32(2) {
		zPath = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
		if zPath == uintptr(0) {
			goto json_type_done
		}
		if int32(**(**int8)(__ccgo_up(zPath))) != int32('$') {
			_jsonBadPathError(tls, ctx, zPath, 0)
			goto json_type_done
		}
		i = _jsonLookupStep(tls, p, uint32(0), zPath+uintptr(1), uint32(0))
		if i >= uint32(JSON_LOOKUP_PATHERROR) {
			if i == uint32(JSON_LOOKUP_NOTFOUND) {
				/* no-op */
			} else {
				_jsonBadPathError(tls, ctx, zPath, int32(i))
			}
			goto json_type_done
		}
	} else {
		i = uint32(0)
	}
	Xsqlite3_result_text(tls, ctx, _jsonbType[int32(**(**Tu8)(__ccgo_up((*TJsonParse)(unsafe.Pointer(p)).FaBlob + uintptr(i))))&int32(0x0f)], -int32(1), libc.UintptrFromInt32(0))
	goto json_type_done
json_type_done:
	;
	_jsonParseFree(tls, p)
}

// C documentation
//
//	/*
//	** json_valid(JSON)
//	** json_valid(JSON, FLAGS)
//	**
//	** Check the JSON argument to see if it is well-formed.  The FLAGS argument
//	** encodes the various constraints on what is meant by "well-formed":
//	**
//	**     0x01      Canonical RFC-8259 JSON text
//	**     0x02      JSON text with optional JSON-5 extensions
//	**     0x04      Superficially appears to be JSONB
//	**     0x08      Strictly well-formed JSONB
//	**
//	** If the FLAGS argument is omitted, it defaults to 1.  Useful values for
//	** FLAGS include:
//	**
//	**    1          Strict canonical JSON text
//	**    2          JSON text perhaps with JSON-5 extensions
//	**    4          Superficially appears to be JSONB
//	**    5          Canonical JSON text or superficial JSONB
//	**    6          JSON-5 text or superficial JSONB
//	**    8          Strict JSONB
//	**    9          Canonical JSON text or strict JSONB
//	**    10         JSON-5 text or strict JSONB
//	**
//	** Other flag combinations are redundant.  For example, every canonical
//	** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3
//	** are the same.  Similarly, any input that passes a strict JSONB validation
//	** will also pass the superficial validation so 12 through 15 are the same
//	** as 8 through 11 respectively.
//	**
//	** This routine runs in linear time to validate text and when doing strict
//	** JSONB validation.  Superficial JSONB validation is constant time,
//	** assuming the BLOB is already in memory.  The performance advantage
//	** of superficial JSONB validation is why that option is provided.
//	** Application developers can choose to do fast superficial validation or
//	** slower strict validation, according to their specific needs.
//	**
//	** Only the lower four bits of the FLAGS argument are currently used.
//	** Higher bits are reserved for future expansion.   To facilitate
//	** compatibility, the current implementation raises an error if any bit
//	** in FLAGS is set other than the lower four bits.
//	**
//	** The original circa 2015 implementation of the JSON routines in
//	** SQLite only supported canonical RFC-8259 JSON text and the json_valid()
//	** function only accepted one argument.  That is why the default value
//	** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only
//	** recognize canonical RFC-8259 JSON text as valid.  The extra FLAGS
//	** argument was added when the JSON routines were extended to support
//	** JSON5-like extensions and binary JSONB stored in BLOBs.
//	**
//	** Return Values:
//	**
//	**   *   Raise an error if FLAGS is outside the range of 1 to 15.
//	**   *   Return NULL if the input is NULL
//	**   *   Return 1 if the input is well-formed.
//	**   *   Return 0 if the input is not well-formed.
//	*/
func _jsonValidFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var f Ti64
	var flags, res Tu8
	var p uintptr
	var _ /* px at bp+56 */ TJsonParse
	var _ /* py at bp+0 */ TJsonParse
	_, _, _, _ = f, flags, p, res /* The parse */
	flags = uint8(1)
	res = uint8(0)
	if argc == int32(2) {
		f = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
		if f < int64(1) || f > int64(15) {
			Xsqlite3_result_error(tls, ctx, __ccgo_ts+28034, -int32(1))
			return
		}
		flags = uint8(f & int64(0x0f))
	}
	switch Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv))) {
	case int32(SQLITE_NULL):
		return
	case int32(SQLITE_BLOB):
		libc.Xmemset(tls, bp, 0, uint32(56))
		if _jsonArgIsJsonb(tls, **(**uintptr)(__ccgo_up(argv)), bp) != 0 {
			if int32(flags)&int32(0x04) != 0 {
				/* Superficial checking only - accomplished by the
				 ** jsonArgIsJsonb() call above. */
				res = uint8(1)
			} else {
				if int32(flags)&int32(0x08) != 0 {
					/* Strict checking.  Check by translating BLOB->TEXT->BLOB.  If
					 ** no errors occur, call that a "strict check". */
					res = libc.BoolUint8(uint32(0) == _jsonbValidityCheck(tls, bp, uint32(0), (**(**TJsonParse)(__ccgo_up(bp))).FnBlob, uint32(1)))
				}
			}
			break
		}
		/* Fall through into interpreting the input as text.  See note
		 ** above at tag-20240123-a. */
		fallthrough
	default:
		if int32(flags)&int32(0x3) == 0 {
			break
		}
		libc.Xmemset(tls, bp+56, 0, uint32(56))
		p = _jsonParseFuncArg(tls, ctx, **(**uintptr)(__ccgo_up(argv)), uint32(JSON_KEEPERROR))
		if p != 0 {
			if (*TJsonParse)(unsafe.Pointer(p)).Foom != 0 {
				Xsqlite3_result_error_nomem(tls, ctx)
			} else {
				if (*TJsonParse)(unsafe.Pointer(p)).FnErr != 0 {
					/* no-op */
				} else {
					if int32(flags)&int32(0x02) != 0 || int32((*TJsonParse)(unsafe.Pointer(p)).FhasNonstd) == 0 {
						res = uint8(1)
					}
				}
			}
			_jsonParseFree(tls, p)
		} else {
			Xsqlite3_result_error_nomem(tls, ctx)
		}
		break
	}
	Xsqlite3_result_int(tls, ctx, int32(res))
}

// C documentation
//
//	/*
//	** Implementation of the length() function
//	*/
func _lengthFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var c, v1 uint8
	var z, z0 uintptr
	_, _, _, _ = c, z, z0, v1
	_ = argc
	switch Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv))) {
	case int32(SQLITE_BLOB):
		fallthrough
	case int32(SQLITE_INTEGER):
		fallthrough
	case int32(SQLITE_FLOAT):
		Xsqlite3_result_int(tls, context, Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv))))
	case int32(SQLITE_TEXT):
		z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		if z == uintptr(0) {
			return
		}
		z0 = z
		for {
			v1 = **(**uint8)(__ccgo_up(z))
			c = v1
			if !(int32(v1) != 0) {
				break
			}
			z = z + 1
			if int32(c) >= int32(0xc0) {
				for int32(**(**uint8)(__ccgo_up(z)))&int32(0xc0) == int32(0x80) {
					z = z + 1
					z0 = z0 + 1
				}
			}
		}
		Xsqlite3_result_int(tls, context, int32(z)-int32(z0))
	default:
		Xsqlite3_result_null(tls, context)
		break
	}
}

// C documentation
//
//	/*
//	** Implementation of the like() SQL function.  This function implements
//	** the built-in LIKE operator.  The first argument to the function is the
//	** pattern and the second argument is the string.  So, the SQL statements:
//	**
//	**       A LIKE B
//	**
//	** is implemented as like(B,A).
//	**
//	** This same function (with a different compareInfo structure) computes
//	** the GLOB operator.
//	*/
func _likeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pInfo, zA, zB uintptr
	var escape Tu32
	var nPat int32
	var _ /* backupInfo at bp+0 */ TcompareInfo
	var _ /* zEsc at bp+4 */ uintptr
	_, _, _, _, _, _ = db, escape, nPat, pInfo, zA, zB
	db = Xsqlite3_context_db_handle(tls, context)
	pInfo = Xsqlite3_user_data(tls, context)
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv))) == int32(SQLITE_BLOB) || Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 1*4))) == int32(SQLITE_BLOB) {
		Xsqlite3_result_int(tls, context, 0)
		return
	}
	/* Limit the length of the LIKE or GLOB pattern to avoid problems
	 ** of deep recursion and N*N behavior in patternCompare().
	 */
	nPat = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
	if nPat > **(**int32)(__ccgo_up(db + 120 + 8*4)) {
		Xsqlite3_result_error(tls, context, __ccgo_ts+17703, -int32(1))
		return
	}
	if argc == int32(3) {
		/* The escape character string must consist of a single UTF-8 character.
		 ** Otherwise, return an error.
		 */
		**(**uintptr)(__ccgo_up(bp + 4)) = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 2*4)))
		if **(**uintptr)(__ccgo_up(bp + 4)) == uintptr(0) {
			return
		}
		if _sqlite3Utf8CharLen(tls, **(**uintptr)(__ccgo_up(bp + 4)), -int32(1)) != int32(1) {
			Xsqlite3_result_error(tls, context, __ccgo_ts+17736, -int32(1))
			return
		}
		escape = _sqlite3Utf8Read(tls, bp+4)
		if escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchAll) || escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchOne) {
			libc.Xmemcpy(tls, bp, pInfo, uint32(4))
			pInfo = bp
			if escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchAll) {
				(*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchAll = uint8(0)
			}
			if escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchOne) {
				(*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchOne = uint8(0)
			}
		}
	} else {
		escape = uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchSet)
	}
	zB = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	zA = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	if zA != 0 && zB != 0 {
		Xsqlite3_result_int(tls, context, libc.BoolInt32(_patternCompare(tls, zB, zA, pInfo, escape) == SQLITE_MATCH))
	}
}

// C documentation
//
//	/*
//	** A function that loads a shared-library extension then returns NULL.
//	*/
func _loadExt(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, zFile, zProc uintptr
	var _ /* zErrMsg at bp+0 */ uintptr
	_, _, _ = db, zFile, zProc
	zFile = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	db = Xsqlite3_context_db_handle(tls, context)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	/* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
	 ** flag is set.  See the sqlite3_enable_load_extension() API.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_LoadExtFunc) == uint64(0) {
		Xsqlite3_result_error(tls, context, __ccgo_ts+14888, -int32(1))
		return
	}
	if argc == int32(2) {
		zProc = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	} else {
		zProc = uintptr(0)
	}
	if zFile != 0 && Xsqlite3_load_extension(tls, db, zFile, zProc, bp) != 0 {
		Xsqlite3_result_error(tls, context, **(**uintptr)(__ccgo_up(bp)), -int32(1))
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp)))
	}
}

// C documentation
//
//	/*
//	** Load the content from either the sqlite_stat4
//	** into the relevant Index.aSample[] arrays.
//	**
//	** Arguments zSql1 and zSql2 must point to SQL statements that return
//	** data equivalent to the following:
//	**
//	**    zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
//	**    zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
//	**
//	** where %Q is replaced with the database name before the SQL is executed.
//	*/
func _loadStatTbl(tls *libc.TLS, db uintptr, zSql1 uintptr, zSql2 uintptr, zDb uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, nByte Ti64
	var nCol, nIdxCol, nSample, rc int32
	var pIdx, pIdx1, pPrevIdx, pPtr, pSample, pSpace, zIndex, zIndex1, zSql uintptr
	var _ /* pStmt at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, nByte, nCol, nIdxCol, nSample, pIdx, pIdx1, pPrevIdx, pPtr, pSample, pSpace, rc, zIndex, zIndex1, zSql /* Result codes from subroutines */
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)                                                                                                               /* Text of the SQL statement */
	pPrevIdx = uintptr(0)                                                                                                                                   /* A slot in pIdx->aSample[] */
	zSql = _sqlite3MPrintf(tls, db, zSql1, libc.VaList(bp+16, zDb))
	if !(zSql != 0) {
		return int32(SQLITE_NOMEM)
	}
	rc = Xsqlite3_prepare(tls, db, zSql, -int32(1), bp, uintptr(0))
	_sqlite3DbFree(tls, db, zSql)
	if rc != 0 {
		return rc
	}
	for Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) == int32(SQLITE_ROW) {
		nIdxCol = int32(1) /* Available memory as a u8 for easier manipulation */
		zIndex = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), 0)
		if zIndex == uintptr(0) {
			continue
		}
		nSample = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		pIdx = _findIndexOrPrimaryKey(tls, db, zIndex, zDb)
		if pIdx == uintptr(0) {
			continue
		}
		if (*TIndex)(unsafe.Pointer(pIdx)).FaSample != uintptr(0) {
			/* The same index appears in sqlite_stat4 under multiple names */
			continue
		}
		if !((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
			nIdxCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
		} else {
			nIdxCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
		}
		(*TIndex)(unsafe.Pointer(pIdx)).FnSampleCol = nIdxCol
		(*TIndex)(unsafe.Pointer(pIdx)).FmxSample = nSample
		nByte = (libc.Int64FromInt64(20)*int64(nSample) + libc.Int64FromInt32(7)) & int64(^libc.Int32FromInt32(7))
		nByte = nByte + libc.Int64FromInt64(8)*int64(nIdxCol)*int64(3)*int64(nSample)
		nByte = nByte + int64(nIdxCol)*libc.Int64FromInt64(8) /* Space for Index.aAvgEq[] */
		(*TIndex)(unsafe.Pointer(pIdx)).FaSample = _sqlite3DbMallocZero(tls, db, uint64(nByte))
		if (*TIndex)(unsafe.Pointer(pIdx)).FaSample == uintptr(0) {
			Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
			return int32(SQLITE_NOMEM)
		}
		pPtr = (*TIndex)(unsafe.Pointer(pIdx)).FaSample
		pPtr = pPtr + uintptr((int64(nSample)*libc.Int64FromInt64(20)+libc.Int64FromInt32(7))&int64(^libc.Int32FromInt32(7)))
		pSpace = pPtr
		(*TIndex)(unsafe.Pointer(pIdx)).FaAvgEq = pSpace
		pSpace = pSpace + uintptr(nIdxCol)*8
		**(**Tu32)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FpTable + 28)) |= uint32(TF_HasStat4)
		i = 0
		for {
			if !(i < int64(nSample)) {
				break
			}
			(**(**TIndexSample)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(i)*20))).FanEq = pSpace
			pSpace = pSpace + uintptr(nIdxCol)*8
			(**(**TIndexSample)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(i)*20))).FanLt = pSpace
			pSpace = pSpace + uintptr(nIdxCol)*8
			(**(**TIndexSample)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(i)*20))).FanDLt = pSpace
			pSpace = pSpace + uintptr(nIdxCol)*8
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	rc = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	if rc != 0 {
		return rc
	}
	zSql = _sqlite3MPrintf(tls, db, zSql2, libc.VaList(bp+16, zDb))
	if !(zSql != 0) {
		return int32(SQLITE_NOMEM)
	}
	rc = Xsqlite3_prepare(tls, db, zSql, -int32(1), bp, uintptr(0))
	_sqlite3DbFree(tls, db, zSql)
	if rc != 0 {
		return rc
	}
	for Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) == int32(SQLITE_ROW) { /* Pointer to the index object */
		nCol = int32(1) /* Number of columns in index */
		zIndex1 = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), 0)
		if zIndex1 == uintptr(0) {
			continue
		}
		pIdx1 = _findIndexOrPrimaryKey(tls, db, zIndex1, zDb)
		if pIdx1 == uintptr(0) {
			continue
		}
		if (*TIndex)(unsafe.Pointer(pIdx1)).FnSample >= (*TIndex)(unsafe.Pointer(pIdx1)).FmxSample {
			/* Too many slots used because the same index appears in
			 ** sqlite_stat4 using multiple names */
			continue
		}
		/* This next condition is true if data has already been loaded from
		 ** the sqlite_stat4 table. */
		nCol = (*TIndex)(unsafe.Pointer(pIdx1)).FnSampleCol
		if pIdx1 != pPrevIdx {
			_initAvgEq(tls, pPrevIdx)
			pPrevIdx = pIdx1
		}
		pSample = (*TIndex)(unsafe.Pointer(pIdx1)).FaSample + uintptr((*TIndex)(unsafe.Pointer(pIdx1)).FnSample)*20
		_decodeIntArray(tls, Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(1)), nCol, (*TIndexSample)(unsafe.Pointer(pSample)).FanEq, uintptr(0), uintptr(0))
		_decodeIntArray(tls, Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(2)), nCol, (*TIndexSample)(unsafe.Pointer(pSample)).FanLt, uintptr(0), uintptr(0))
		_decodeIntArray(tls, Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(3)), nCol, (*TIndexSample)(unsafe.Pointer(pSample)).FanDLt, uintptr(0), uintptr(0))
		/* Take a copy of the sample. Add 8 extra 0x00 bytes the end of the buffer.
		 ** This is in case the sample record is corrupted. In that case, the
		 ** sqlite3VdbeRecordCompare() may read up to two varints past the
		 ** end of the allocated buffer before it realizes it is dealing with
		 ** a corrupt record.  Or it might try to read a large integer from the
		 ** buffer.  In any case, eight 0x00 bytes prevents this from causing
		 ** a buffer overread.  */
		(*TIndexSample)(unsafe.Pointer(pSample)).Fn = Xsqlite3_column_bytes(tls, **(**uintptr)(__ccgo_up(bp)), int32(4))
		(*TIndexSample)(unsafe.Pointer(pSample)).Fp = _sqlite3DbMallocZero(tls, db, uint64((*TIndexSample)(unsafe.Pointer(pSample)).Fn+int32(8)))
		if (*TIndexSample)(unsafe.Pointer(pSample)).Fp == uintptr(0) {
			Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
			return int32(SQLITE_NOMEM)
		}
		if (*TIndexSample)(unsafe.Pointer(pSample)).Fn != 0 {
			libc.Xmemcpy(tls, (*TIndexSample)(unsafe.Pointer(pSample)).Fp, Xsqlite3_column_blob(tls, **(**uintptr)(__ccgo_up(bp)), int32(4)), uint32((*TIndexSample)(unsafe.Pointer(pSample)).Fn))
		}
		(*TIndex)(unsafe.Pointer(pIdx1)).FnSample = (*TIndex)(unsafe.Pointer(pIdx1)).FnSample + 1
	}
	rc = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	if rc == SQLITE_OK {
		_initAvgEq(tls, pPrevIdx)
	}
	return rc
}

// C documentation
//
//	/*
//	** Get a reference to pPage1 of the database file.  This will
//	** also acquire a readlock on that file.
//	**
//	** SQLITE_OK is returned on success.  If the file is not a
//	** well-formed database file, then SQLITE_CORRUPT is returned.
//	** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
//	** is returned if we run out of memory.
//	*/
func _lockBtree(tls *libc.TLS, pBt uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nPage, pageSize, usableSize Tu32
	var page1, v1 uintptr
	var rc, v4 int32
	var _ /* isOpen at bp+8 */ int32
	var _ /* nPageFile at bp+4 */ Tu32
	var _ /* pPage1 at bp+0 */ uintptr
	_, _, _, _, _, _, _ = nPage, page1, pageSize, rc, usableSize, v1, v4 /* Number of pages in the database */
	**(**Tu32)(__ccgo_up(bp + 4)) = uint32(0)                            /* Number of pages in the database file */
	rc = _sqlite3PagerSharedLock(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager)
	if rc != SQLITE_OK {
		return rc
	}
	rc = _btreeGetPage(tls, pBt, uint32(1), bp, 0)
	if rc != SQLITE_OK {
		return rc
	}
	/* Do some checking to help insure the file we opened really is
	 ** a valid database file.
	 */
	nPage = _sqlite3Get4byte(tls, uintptr(28)+(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData)
	_sqlite3PagerPagecount(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, bp+4)
	if nPage == uint32(0) || libc.Xmemcmp(tls, uintptr(24)+(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData, uintptr(92)+(*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData, uint32(4)) != 0 {
		nPage = **(**Tu32)(__ccgo_up(bp + 4))
	}
	if (*Tsqlite3)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).Fdb)).Fflags&uint64(SQLITE_ResetDatabase) != uint64(0) {
		nPage = uint32(0)
	}
	if nPage > uint32(0) {
		page1 = (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaData
		rc = int32(SQLITE_NOTADB)
		/* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
		 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
		 ** 61 74 20 33 00. */
		if libc.Xmemcmp(tls, page1, uintptr(unsafe.Pointer(&_zMagicHeader)), uint32(16)) != 0 {
			goto page1_init_failed
		}
		if int32(**(**Tu8)(__ccgo_up(page1 + 18))) > int32(2) {
			v1 = pBt + 24
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTS_READ_ONLY))
		}
		if int32(**(**Tu8)(__ccgo_up(page1 + 19))) > int32(2) {
			goto page1_init_failed
		}
		/* If the read version is set to 2, this database should be accessed
		 ** in WAL mode. If the log is not already open, open it now. Then
		 ** return SQLITE_OK and return without populating BtShared.pPage1.
		 ** The caller detects this and calls this function again. This is
		 ** required as the version of page 1 currently in the page1 buffer
		 ** may not be the latest version - there may be a newer one in the log
		 ** file.
		 */
		if int32(**(**Tu8)(__ccgo_up(page1 + 19))) == int32(2) && int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_NO_WAL) == 0 {
			**(**int32)(__ccgo_up(bp + 8)) = 0
			rc = _sqlite3PagerOpenWal(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, bp+8)
			if rc != SQLITE_OK {
				goto page1_init_failed
			} else {
				if **(**int32)(__ccgo_up(bp + 8)) == 0 {
					_releasePageOne(tls, **(**uintptr)(__ccgo_up(bp)))
					return SQLITE_OK
				}
			}
			rc = int32(SQLITE_NOTADB)
		} else {
		}
		/* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
		 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
		 **
		 ** The original design allowed these amounts to vary, but as of
		 ** version 3.6.0, we require them to be fixed.
		 */
		if libc.Xmemcmp(tls, page1+21, __ccgo_ts+5565, uint32(3)) != 0 {
			goto page1_init_failed
		}
		/* EVIDENCE-OF: R-51873-39618 The page size for a database file is
		 ** determined by the 2-byte integer located at an offset of 16 bytes from
		 ** the beginning of the database file. */
		pageSize = uint32(int32(**(**Tu8)(__ccgo_up(page1 + 16)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(page1 + 17)))<<int32(16))
		/* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
		 ** between 512 and 65536 inclusive. */
		if (pageSize-uint32(1))&pageSize != uint32(0) || pageSize > uint32(SQLITE_MAX_PAGE_SIZE) || pageSize <= uint32(256) {
			goto page1_init_failed
		}
		/* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
		 ** integer at offset 20 is the number of bytes of space at the end of
		 ** each page to reserve for extensions.
		 **
		 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
		 ** determined by the one-byte unsigned integer found at an offset of 20
		 ** into the database file header. */
		usableSize = pageSize - uint32(**(**Tu8)(__ccgo_up(page1 + 20)))
		if pageSize != (*TBtShared)(unsafe.Pointer(pBt)).FpageSize {
			/* After reading the first page of the database assuming a page size
			 ** of BtShared.pageSize, we have discovered that the page-size is
			 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
			 ** zero and return SQLITE_OK. The caller will call this function
			 ** again with the correct page-size.
			 */
			_releasePageOne(tls, **(**uintptr)(__ccgo_up(bp)))
			(*TBtShared)(unsafe.Pointer(pBt)).FusableSize = usableSize
			(*TBtShared)(unsafe.Pointer(pBt)).FpageSize = pageSize
			v1 = pBt + 24
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED))
			_freeTempSpace(tls, pBt)
			rc = _sqlite3PagerSetPagesize(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pBt+36, int32(pageSize-usableSize))
			return rc
		}
		if nPage > **(**Tu32)(__ccgo_up(bp + 4)) {
			if _sqlite3WritableSchema(tls, (*TBtShared)(unsafe.Pointer(pBt)).Fdb) == 0 {
				rc = _sqlite3CorruptError(tls, int32(76633))
				goto page1_init_failed
			} else {
				nPage = **(**Tu32)(__ccgo_up(bp + 4))
			}
		}
		/* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
		 ** be less than 480. In other words, if the page size is 512, then the
		 ** reserved space size cannot exceed 32. */
		if usableSize < uint32(480) {
			goto page1_init_failed
		}
		v1 = pBt + 24
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED))
		(*TBtShared)(unsafe.Pointer(pBt)).FpageSize = pageSize
		(*TBtShared)(unsafe.Pointer(pBt)).FusableSize = usableSize
		if _sqlite3Get4byte(tls, page1+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(4)*libc.Int32FromInt32(4))) != 0 {
			v4 = int32(1)
		} else {
			v4 = 0
		}
		(*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum = uint8(v4)
		if _sqlite3Get4byte(tls, page1+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(7)*libc.Int32FromInt32(4))) != 0 {
			v4 = int32(1)
		} else {
			v4 = 0
		}
		(*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum = uint8(v4)
	}
	/* maxLocal is the maximum amount of payload to store locally for
	 ** a cell.  Make sure it is small enough so that at least minFanout
	 ** cells can will fit on one page.  We assume a 10-byte page header.
	 ** Besides the payload, the cell must store:
	 **     2-byte pointer to the cell
	 **     4-byte child pointer
	 **     9-byte nKey value
	 **     4-byte nData value
	 **     4-byte overflow page pointer
	 ** So a cell consists of a 2-byte pointer, a header which is as much as
	 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
	 ** page pointer.
	 */
	(*TBtShared)(unsafe.Pointer(pBt)).FmaxLocal = uint16(((*TBtShared)(unsafe.Pointer(pBt)).FusableSize-libc.Uint32FromInt32(12))*libc.Uint32FromInt32(64)/libc.Uint32FromInt32(255) - libc.Uint32FromInt32(23))
	(*TBtShared)(unsafe.Pointer(pBt)).FminLocal = uint16(((*TBtShared)(unsafe.Pointer(pBt)).FusableSize-libc.Uint32FromInt32(12))*libc.Uint32FromInt32(32)/libc.Uint32FromInt32(255) - libc.Uint32FromInt32(23))
	(*TBtShared)(unsafe.Pointer(pBt)).FmaxLeaf = uint16((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - libc.Uint32FromInt32(35))
	(*TBtShared)(unsafe.Pointer(pBt)).FminLeaf = uint16(((*TBtShared)(unsafe.Pointer(pBt)).FusableSize-libc.Uint32FromInt32(12))*libc.Uint32FromInt32(32)/libc.Uint32FromInt32(255) - libc.Uint32FromInt32(23))
	if int32((*TBtShared)(unsafe.Pointer(pBt)).FmaxLocal) > int32(127) {
		(*TBtShared)(unsafe.Pointer(pBt)).Fmax1bytePayload = uint8(127)
	} else {
		(*TBtShared)(unsafe.Pointer(pBt)).Fmax1bytePayload = uint8((*TBtShared)(unsafe.Pointer(pBt)).FmaxLocal)
	}
	(*TBtShared)(unsafe.Pointer(pBt)).FpPage1 = **(**uintptr)(__ccgo_up(bp))
	(*TBtShared)(unsafe.Pointer(pBt)).FnPage = nPage
	return SQLITE_OK
	goto page1_init_failed
page1_init_failed:
	;
	_releasePageOne(tls, **(**uintptr)(__ccgo_up(bp)))
	(*TBtShared)(unsafe.Pointer(pBt)).FpPage1 = uintptr(0)
	return rc
}

// C documentation
//
//	/*
//	** Record the fact that we want to lock a table at run-time.
//	**
//	** The table to be locked has root page iTab and is found in database iDb.
//	** A read or a write lock can be taken depending on isWritelock.
//	**
//	** This routine just records the fact that the lock is desired.  The
//	** code to make the lock occur is generated by a later call to
//	** codeTableLocks() which occurs during sqlite3FinishCoding().
//	*/
func _lockTable(tls *libc.TLS, pParse uintptr, iDb int32, iTab TPgno, isWriteLock Tu8, zName uintptr) {
	var i, nBytes, v3 int32
	var p, pToplevel, v1 uintptr
	_, _, _, _, _, _ = i, nBytes, p, pToplevel, v1, v3
	if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 {
		v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel
	} else {
		v1 = pParse
	}
	pToplevel = v1
	i = 0
	for {
		if !(i < (*TParse)(unsafe.Pointer(pToplevel)).FnTableLock) {
			break
		}
		p = (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock + uintptr(i)*16
		if (*TTableLock)(unsafe.Pointer(p)).FiDb == iDb && (*TTableLock)(unsafe.Pointer(p)).FiTab == iTab {
			(*TTableLock)(unsafe.Pointer(p)).FisWriteLock = libc.BoolUint8((*TTableLock)(unsafe.Pointer(p)).FisWriteLock != 0 || isWriteLock != 0)
			return
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	nBytes = int32(uint32(16) * uint32((*TParse)(unsafe.Pointer(pToplevel)).FnTableLock+libc.Int32FromInt32(1)))
	(*TParse)(unsafe.Pointer(pToplevel)).FaTableLock = _sqlite3DbReallocOrFree(tls, (*TParse)(unsafe.Pointer(pToplevel)).Fdb, (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock, uint64(nBytes))
	if (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock != 0 {
		v1 = pToplevel + 108
		v3 = *(*int32)(unsafe.Pointer(v1))
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		p = (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock + uintptr(v3)*16
		(*TTableLock)(unsafe.Pointer(p)).FiDb = iDb
		(*TTableLock)(unsafe.Pointer(p)).FiTab = iTab
		(*TTableLock)(unsafe.Pointer(p)).FisWriteLock = isWriteLock
		(*TTableLock)(unsafe.Pointer(p)).FzLockName = zName
	} else {
		(*TParse)(unsafe.Pointer(pToplevel)).FnTableLock = 0
		_sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(pToplevel)).Fdb)
	}
}

// C documentation
//
//	/*
//	** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
//	** that name in the set of source tables in pSrcList and make the pExpr
//	** expression node refer back to that source column.  The following changes
//	** are made to pExpr:
//	**
//	**    pExpr->iDb           Set the index in db->aDb[] of the database X
//	**                         (even if X is implied).
//	**    pExpr->iTable        Set to the cursor number for the table obtained
//	**                         from pSrcList.
//	**    pExpr->y.pTab        Points to the Table structure of X.Y (even if
//	**                         X and/or Y are implied.)
//	**    pExpr->iColumn       Set to the column number within the table.
//	**    pExpr->op            Set to TK_COLUMN.
//	**    pExpr->pLeft         Any expression this points to is deleted
//	**    pExpr->pRight        Any expression this points to is deleted.
//	**
//	** The zDb variable is the name of the database (the "X").  This value may be
//	** NULL meaning that name is of the form Y.Z or Z.  Any available database
//	** can be used.  The zTable variable is the name of the table (the "Y").  This
//	** value can be NULL if zDb is also NULL.  If zTable is NULL it
//	** means that the form of the name is Z and that columns from any table
//	** can be used.
//	**
//	** If the name cannot be resolved unambiguously, leave an error message
//	** in pParse and return WRC_Abort.  Return WRC_Prune on success.
//	*/
func _lookupName(tls *libc.TLS, pParse uintptr, zDb uintptr, zTab uintptr, pRight uintptr, pNC uintptr, pExpr uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var cnt, cntTab, eNewExprOp, hit, i, iCol, j, nSubquery, op, v4 int32
	var db, pEList, pItem, pMatch, pOrig, pSchema, pSel, pSrcList, pTab, pTopNC, pUpsert, zAs, zCol, zErr, v8 uintptr
	var v5 uint32
	var _ /* bRowid at bp+4 */ int32
	var _ /* pFJMatch at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = cnt, cntTab, db, eNewExprOp, hit, i, iCol, j, nSubquery, op, pEList, pItem, pMatch, pOrig, pSchema, pSel, pSrcList, pTab, pTopNC, pUpsert, zAs, zCol, zErr, v4, v5, v8 /* Loop counters */
	cnt = 0                                                                                                                                                                                                                                               /* Number of matching column names */
	cntTab = 0                                                                                                                                                                                                                                            /* Number of potential "rowid" matches */
	nSubquery = 0                                                                                                                                                                                                                                         /* How many levels of subquery */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb                                                                                                                                                                                                            /* Use for looping over pSrcList items */
	pMatch = uintptr(0)                                                                                                                                                                                                                                   /* The matching pSrcList item */
	pTopNC = pNC                                                                                                                                                                                                                                          /* First namecontext in the list */
	pSchema = uintptr(0)                                                                                                                                                                                                                                  /* Schema of the expression */
	eNewExprOp = int32(TK_COLUMN)                                                                                                                                                                                                                         /* New value for pExpr->op on success */
	pTab = uintptr(0)                                                                                                                                                                                                                                     /* Table holding the row */
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)                                                                                                                                                                                                             /* Matches for FULL JOIN .. USING */
	zCol = *(*uintptr)(unsafe.Pointer(pRight + 8))
	/* the name context cannot be NULL. */
	/* The Z in X.Y.Z cannot be NULL */
	/* Initialize the node to no-match */
	(*TExpr)(unsafe.Pointer(pExpr)).FiTable = -int32(1)
	/* Translate the schema name in zDb into a pointer to the corresponding
	 ** schema.  If not found, pSchema will remain NULL and nothing will match
	 ** resulting in an appropriate error message toward the end of this routine
	 */
	if zDb != 0 {
		if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_PartIdx)|libc.Int32FromInt32(NC_IsCheck)) != 0 {
			/* Silently ignore database qualifiers inside CHECK constraints and
			 ** partial indices.  Do not raise errors because that might break
			 ** legacy and because it does not hurt anything to just ignore the
			 ** database name. */
			zDb = uintptr(0)
		} else {
			i = 0
			for {
				if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
					break
				}
				if _sqlite3StrICmp(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FzDbSName, zDb) == 0 {
					pSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema
					break
				}
				goto _1
			_1:
				;
				i = i + 1
			}
			if i == (*Tsqlite3)(unsafe.Pointer(db)).FnDb && _sqlite3StrICmp(tls, __ccgo_ts+8033, zDb) == 0 {
				/* This branch is taken when the main database has been renamed
				 ** using SQLITE_DBCONFIG_MAINDBNAME. */
				pSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema
				zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FzDbSName
			}
		}
	}
	/* Start at the inner-most context and move outward until a match is found */
	for cond := true; cond; cond = pNC != 0 {
		pSrcList = (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList
		if pSrcList != 0 {
			i = 0
			pItem = pSrcList + 8
			for {
				if !(i < (*TSrcList)(unsafe.Pointer(pSrcList)).FnSrc) {
					break
				}
				pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
				if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4000>>14) != 0 {
					/* In this case, pItem is a subquery that has been formed from a
					 ** parenthesized subset of the FROM clause terms.  Example:
					 **   .... FROM t1 LEFT JOIN (t2 RIGHT JOIN t3 USING(x)) USING(y) ...
					 **                          \_________________________/
					 **             This pItem -------------^
					 */
					hit = 0
					pSel = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 44)))).FpSelect
					pEList = (*TSelect)(unsafe.Pointer(pSel)).FpEList
					j = 0
					for {
						if !(j < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
							break
						}
						**(**int32)(__ccgo_up(bp + 4)) = 0 /* True if possible rowid match */
						if !(_sqlite3MatchEName(tls, pEList+8+uintptr(j)*20, zCol, zTab, zDb, bp+4) != 0) {
							goto _3
						}
						if **(**int32)(__ccgo_up(bp + 4)) == 0 {
							if cnt > 0 {
								if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x800>>11) == 0 || _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pItem + 40)), zCol) < 0 || pMatch == pItem {
									/* Two or more tables have the same column name which is
									 ** not joined by USING. Or, a single table has two columns
									 ** that match a USING term (if pMatch==pItem). These are both
									 ** "ambiguous column name" errors. Signal as much by clearing
									 ** pFJMatch and letting cnt go above 1. */
									_sqlite3ExprListDelete(tls, db, **(**uintptr)(__ccgo_up(bp)))
									**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
								} else {
									if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) == 0 {
										/* An INNER or LEFT JOIN.  Use the left-most table */
										goto _3
									} else {
										if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LEFT) == 0 {
											/* A RIGHT JOIN.  Use the right-most table */
											cnt = 0
											_sqlite3ExprListDelete(tls, db, **(**uintptr)(__ccgo_up(bp)))
											**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
										} else {
											/* For a FULL JOIN, we must construct a coalesce() func */
											_extendFJMatch(tls, pParse, bp, pMatch, (*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
										}
									}
								}
							}
							cnt = cnt + 1
							hit = int32(1)
						} else {
							if cnt > 0 {
								/* This is a potential rowid match, but there has already been
								 ** a real match found. So this can be ignored.  */
								goto _3
							}
						}
						cntTab = cntTab + 1
						pMatch = pItem
						(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(j)
						libc.SetBitFieldPtr16Uint32(pEList+8+uintptr(j)*20+8+4, libc.Uint32FromInt32(1), 6, 0x40)
						/* rowid cannot be part of a USING clause - assert() this. */
						if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(j)*20 + 8 + 4))&0x80>>7)) != 0 {
							break
						}
						goto _3
					_3:
						;
						j = j + 1
					}
					if hit != 0 || zTab == uintptr(0) {
						goto _2
					}
				}
				if zTab != 0 {
					if zDb != 0 {
						if (*TTable)(unsafe.Pointer(pTab)).FpSchema != pSchema {
							goto _2
						}
						if pSchema == uintptr(0) && libc.Xstrcmp(tls, zDb, __ccgo_ts+8038) != 0 {
							goto _2
						}
					}
					if (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != uintptr(0) {
						if _sqlite3StrICmp(tls, zTab, (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias) != 0 {
							goto _2
						}
					} else {
						if _sqlite3StrICmp(tls, zTab, (*TTable)(unsafe.Pointer(pTab)).FzName) != 0 {
							if (*TTable)(unsafe.Pointer(pTab)).Ftnum != uint32(1) {
								goto _2
							}
							if !(_isValidSchemaTableName(tls, zTab, pTab, zDb) != 0) {
								goto _2
							}
						}
					}
					if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != 0 {
						_sqlite3RenameTokenRemap(tls, pParse, uintptr(0), pExpr+44)
					}
				}
				j = _sqlite3ColumnIndex(tls, pTab, zCol)
				if j >= 0 {
					if cnt > 0 {
						if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x800>>11) == 0 || _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pItem + 40)), zCol) < 0 {
							/* Two or more tables have the same column name which is
							 ** not joined by USING.  This is an error.  Signal as much
							 ** by clearing pFJMatch and letting cnt go above 1. */
							_sqlite3ExprListDelete(tls, db, **(**uintptr)(__ccgo_up(bp)))
							**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
						} else {
							if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) == 0 {
								/* An INNER or LEFT JOIN.  Use the left-most table */
								goto _2
							} else {
								if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LEFT) == 0 {
									/* A RIGHT JOIN.  Use the right-most table */
									cnt = 0
									_sqlite3ExprListDelete(tls, db, **(**uintptr)(__ccgo_up(bp)))
									**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
								} else {
									/* For a FULL JOIN, we must construct a coalesce() func */
									_extendFJMatch(tls, pParse, bp, pMatch, (*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
								}
							}
						}
					}
					cnt = cnt + 1
					pMatch = pItem
					/* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
					if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
						v4 = -int32(1)
					} else {
						v4 = int32(int16(j))
					}
					(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(v4)
					if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4000>>14) != 0 {
						_sqlite3SrcItemColumnUsed(tls, pItem, j)
					}
				}
				if 0 == cnt && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_NoVisibleRowid) == uint32(0) {
					/* pTab is a potential ROWID match.  Keep track of it and match
					 ** the ROWID later if that seems appropriate.  (Search for "cntTab"
					 ** to find related code.)  Only allow a ROWID match if there is
					 ** a single ROWID match candidate.
					 */
					/* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is
					 ** simpler since we require exactly one candidate, which will
					 ** always be a non-VIEW
					 */
					cntTab = cntTab + 1
					pMatch = pItem
				}
				goto _2
			_2:
				;
				i = i + 1
				pItem += 48
			}
			if pMatch != 0 {
				(*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TSrcItem)(unsafe.Pointer(pMatch)).FiCursor
				(*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = (*TSrcItem)(unsafe.Pointer(pMatch)).FpSTab
				if int32((*TSrcItem)(unsafe.Pointer(pMatch)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) != 0 {
					**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_CanBeNull))
				}
				pSchema = (*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab)).FpSchema
			}
		} /* if( pSrcList ) */
		/* If we have not already resolved the name, then maybe
		 ** it is a new.* or old.* trigger argument reference.  Or
		 ** maybe it is an excluded.* from an upsert.  Or maybe it is
		 ** a reference in the RETURNING clause to a table being modified.
		 */
		if cnt == 0 && zDb == uintptr(0) {
			pTab = uintptr(0)
			if (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != uintptr(0) {
				op = int32((*TParse)(unsafe.Pointer(pParse)).FeTriggerOp)
				if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x8>>3)) != 0 {
					if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_UBaseReg) != 0 && (zTab == uintptr(0) || _sqlite3StrICmp(tls, zTab, (*TTable)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).FpTriggerTab)).FzName) == 0 || _isValidSchemaTableName(tls, zTab, (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab, uintptr(0)) != 0) {
						(*TExpr)(unsafe.Pointer(pExpr)).FiTable = libc.BoolInt32(op != int32(TK_DELETE))
						pTab = (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab
					}
				} else {
					if op != int32(TK_DELETE) && zTab != 0 && _sqlite3StrICmp(tls, __ccgo_ts+8040, zTab) == 0 {
						(*TExpr)(unsafe.Pointer(pExpr)).FiTable = int32(1)
						pTab = (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab
					} else {
						if op != int32(TK_INSERT) && zTab != 0 && _sqlite3StrICmp(tls, __ccgo_ts+8044, zTab) == 0 {
							(*TExpr)(unsafe.Pointer(pExpr)).FiTable = 0
							pTab = (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab
						}
					}
				}
			}
			if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_UUpsert) != 0 && zTab != uintptr(0) {
				pUpsert = *(*uintptr)(unsafe.Pointer(pNC + 8))
				if pUpsert != 0 && _sqlite3StrICmp(tls, __ccgo_ts+8048, zTab) == 0 {
					pTab = (*(*TSrcItem)(unsafe.Pointer((*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertSrc + 8))).FpSTab
					(*TExpr)(unsafe.Pointer(pExpr)).FiTable = int32(EXCLUDED_TABLE_NUMBER)
				}
			}
			if pTab != 0 {
				pSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema
				cntTab = cntTab + 1
				iCol = _sqlite3ColumnIndex(tls, pTab, zCol)
				if iCol >= 0 {
					if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) == iCol {
						iCol = -int32(1)
					}
				} else {
					if _sqlite3IsRowid(tls, zCol) != 0 && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_NoVisibleRowid) == uint32(0) {
						iCol = -int32(1)
					} else {
						iCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
					}
				}
				if iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol) {
					cnt = cnt + 1
					pMatch = uintptr(0)
					if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == int32(EXCLUDED_TABLE_NUMBER) {
						if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
							(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(iCol)
							(*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = pTab
							eNewExprOp = int32(TK_COLUMN)
						} else {
							(*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TUpsert)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNC + 8)))).FregData + int32(_sqlite3TableColumnToStorage(tls, pTab, int16(iCol)))
							eNewExprOp = int32(TK_REGISTER)
						}
					} else {
						(*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = pTab
						if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x8>>3)) != 0 {
							eNewExprOp = int32(TK_REGISTER)
							(*TExpr)(unsafe.Pointer(pExpr)).Fop2 = uint8(TK_COLUMN)
							(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(iCol)
							(*TExpr)(unsafe.Pointer(pExpr)).FiTable = *(*int32)(unsafe.Pointer(pNC + 8)) + (int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(1))*(*TExpr)(unsafe.Pointer(pExpr)).FiTable + int32(_sqlite3TableColumnToStorage(tls, pTab, int16(iCol))) + int32(1)
						} else {
							(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(iCol)
							eNewExprOp = int32(TK_TRIGGER)
							if iCol < 0 {
								(*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = int8(SQLITE_AFF_INTEGER)
							} else {
								if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == 0 {
									if iCol >= int32(32) {
										v5 = uint32(0xffffffff)
									} else {
										v5 = libc.Uint32FromInt32(1) << iCol
									}
									**(**Tu32)(__ccgo_up(pParse + 180)) |= v5
								} else {
									if iCol >= int32(32) {
										v5 = uint32(0xffffffff)
									} else {
										v5 = libc.Uint32FromInt32(1) << iCol
									}
									**(**Tu32)(__ccgo_up(pParse + 184)) |= v5
								}
							}
						}
					}
				}
			}
		}
		/*
		 ** Perhaps the name is a reference to the ROWID
		 */
		if cnt == 0 && cntTab >= int32(1) && pMatch != 0 && (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_IdxExpr)|libc.Int32FromInt32(NC_GenCol)) == 0 && _sqlite3IsRowid(tls, zCol) != 0 && ((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pMatch)).FpSTab)).FtabFlags&uint32(TF_NoVisibleRowid) == uint32(0) || int32(*(*uint32)(unsafe.Pointer(pMatch + 12 + 4))&0x4000>>14) != 0) {
			cnt = cntTab
			if int32(*(*uint32)(unsafe.Pointer(pMatch + 12 + 4))&0x4000>>14) == 0 {
				(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(-int32(1))
			}
			(*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = int8(SQLITE_AFF_INTEGER)
		}
		/*
		 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
		 ** might refer to an result-set alias.  This happens, for example, when
		 ** we are resolving names in the WHERE clause of the following command:
		 **
		 **     SELECT a+b AS x FROM table WHERE x<10;
		 **
		 ** In cases like this, replace pExpr with a copy of the expression that
		 ** forms the result set entry ("a+b" in the example) and return immediately.
		 ** Note that the expression in the result set should have already been
		 ** resolved by the time the WHERE clause is resolved.
		 **
		 ** The ability to use an output result-set column in the WHERE, GROUP BY,
		 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
		 ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
		 ** is supported for backwards compatibility only. Hence, we issue a warning
		 ** on sqlite3_log() whenever the capability is used.
		 */
		if cnt == 0 && (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_UEList) != 0 && zTab == uintptr(0) {
			pEList = *(*uintptr)(unsafe.Pointer(pNC + 8))
			j = 0
			for {
				if !(j < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
					break
				}
				zAs = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(j)*20))).FzEName
				if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(j)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME && Xsqlite3_stricmp(tls, zAs, zCol) == 0 {
					pOrig = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(j)*20))).FpExpr
					if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_AllowAgg) == 0 && (*TExpr)(unsafe.Pointer(pOrig)).Fflags&uint32(libc.Int32FromInt32(EP_Agg)) != uint32(0) {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8057, libc.VaList(bp+16, zAs))
						return int32(WRC_Abort)
					}
					if (*TExpr)(unsafe.Pointer(pOrig)).Fflags&uint32(libc.Int32FromInt32(EP_Win)) != uint32(0) && ((*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_AllowWin) == 0 || pNC != pTopNC) {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8088, libc.VaList(bp+16, zAs))
						return int32(WRC_Abort)
					}
					if _sqlite3ExprVectorSize(tls, pOrig) != int32(1) {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8125, 0)
						return int32(WRC_Abort)
					}
					_resolveAlias(tls, pParse, pEList, j, pExpr, nSubquery)
					cnt = int32(1)
					pMatch = uintptr(0)
					if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
						_sqlite3RenameTokenRemap(tls, pParse, uintptr(0), pExpr)
					}
					goto lookupname_end
				}
				goto _7
			_7:
				;
				j = j + 1
			}
		}
		/* Advance to the next name context.  The loop will exit when either
		 ** we have a match (cnt>0) or when we run out of name contexts.
		 */
		if cnt != 0 {
			break
		}
		pNC = (*TNameContext)(unsafe.Pointer(pNC)).FpNext
		nSubquery = nSubquery + 1
	}
	/*
	 ** If X and Y are NULL (in other words if only the column name Z is
	 ** supplied) and the value of Z is enclosed in double-quotes, then
	 ** Z is a string literal if it doesn't match any column names.  In that
	 ** case, we need to return right away and not make any changes to
	 ** pExpr.
	 **
	 ** Because no reference was made to outer contexts, the pNC->nRef
	 ** fields are not changed in any context.
	 */
	if cnt == 0 && zTab == uintptr(0) {
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_DblQuoted)) != uint32(0) && _areDoubleQuotedStringsEnabled(tls, db, pTopNC) != 0 {
			/* If a double-quoted identifier does not match any known column name,
			 ** then treat it as a string.
			 **
			 ** This hack was added in the early days of SQLite in a misguided attempt
			 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
			 ** I now sorely regret putting in this hack. The effect of this hack is
			 ** that misspelled identifier names are silently converted into strings
			 ** rather than causing an error, to the frustration of countless
			 ** programmers. To all those frustrated programmers, my apologies.
			 **
			 ** Someday, I hope to get rid of this hack. Unfortunately there is
			 ** a huge amount of legacy SQL that uses it. So for now, we just
			 ** issue a warning.
			 */
			Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+8143, libc.VaList(bp+16, zCol))
			(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_STRING)
			libc.Xmemset(tls, pExpr+44, 0, uint32(8))
			return int32(WRC_Prune)
		}
		if _sqlite3ExprIdToTrueFalse(tls, pExpr) != 0 {
			return int32(WRC_Prune)
		}
	}
	/*
	 ** cnt==0 means there was not match.
	 ** cnt>1 means there were two or more matches.
	 **
	 ** cnt==0 is always an error.  cnt>1 is often an error, but might
	 ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING.
	 */
	if cnt != int32(1) {
		if **(**uintptr)(__ccgo_up(bp)) != 0 {
			if (*TExprList)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnExpr == cnt-int32(1) {
				if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Leaf)) != uint32(0) {
					**(**Tu32)(__ccgo_up(pExpr + 4)) &= ^uint32(libc.Int32FromInt32(EP_Leaf))
				} else {
					_sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
					(*TExpr)(unsafe.Pointer(pExpr)).FpLeft = uintptr(0)
					_sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpRight)
					(*TExpr)(unsafe.Pointer(pExpr)).FpRight = uintptr(0)
				}
				_extendFJMatch(tls, pParse, bp, pMatch, (*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
				(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_FUNCTION)
				*(*uintptr)(unsafe.Pointer(pExpr + 8)) = __ccgo_ts + 8178
				*(*uintptr)(unsafe.Pointer(pExpr + 20)) = **(**uintptr)(__ccgo_up(bp))
				(*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = int8(SQLITE_AFF_DEFER)
				cnt = int32(1)
				goto lookupname_end
			} else {
				_sqlite3ExprListDelete(tls, db, **(**uintptr)(__ccgo_up(bp)))
				**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			}
		}
		if cnt == 0 {
			v8 = __ccgo_ts + 8187
		} else {
			v8 = __ccgo_ts + 8202
		}
		zErr = v8
		if zDb != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8224, libc.VaList(bp+16, zErr, zDb, zTab, zCol))
		} else {
			if zTab != 0 {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8237, libc.VaList(bp+16, zErr, zTab, zCol))
			} else {
				if cnt == 0 && (*TExpr)(unsafe.Pointer(pRight)).Fflags&uint32(libc.Int32FromInt32(EP_DblQuoted)) != uint32(0) {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8247, libc.VaList(bp+16, zErr, zCol))
				} else {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8308, libc.VaList(bp+16, zErr, zCol))
				}
			}
		}
		_sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr)
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 8, 0x100)
		(*TNameContext)(unsafe.Pointer(pTopNC)).FnNcErr = (*TNameContext)(unsafe.Pointer(pTopNC)).FnNcErr + 1
		eNewExprOp = int32(TK_NULL)
	}
	/* Remove all substructure from pExpr */
	if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Leaf)) != libc.Uint32FromInt32(0)) {
		_sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		(*TExpr)(unsafe.Pointer(pExpr)).FpLeft = uintptr(0)
		_sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpRight)
		(*TExpr)(unsafe.Pointer(pExpr)).FpRight = uintptr(0)
		**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Leaf))
	}
	/* If a column from a table in pSrcList is referenced, then record
	 ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
	 ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  Bit 63 is
	 ** set if the 63rd or any subsequent column is used.
	 **
	 ** The colUsed mask is an optimization used to help determine if an
	 ** index is a covering index.  The correct answer is still obtained
	 ** if the mask contains extra set bits.  However, it is important to
	 ** avoid setting bits beyond the maximum column number of the table.
	 ** (See ticket [b92e5e8ec2cdbaa1]).
	 **
	 ** If a generated column is referenced, set bits for every column
	 ** of the table.
	 */
	if pMatch != 0 {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) >= 0 {
			**(**TBitmask)(__ccgo_up(pMatch + 24)) |= _sqlite3ExprColUsed(tls, pExpr)
		} else {
			libc.SetBitFieldPtr32Uint32(pMatch+12+4, libc.Uint32FromInt32(1), 15, 0x8000)
		}
	}
	(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(eNewExprOp)
	goto lookupname_end
lookupname_end:
	;
	if cnt == int32(1) {
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FxAuth != 0 && (int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_TRIGGER)) {
			_sqlite3AuthRead(tls, pParse, pExpr, pSchema, (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList)
		}
		/* Increment the nRef value on all name contexts from TopNC up to
		 ** the point where the name matched. */
		for {
			(*TNameContext)(unsafe.Pointer(pTopNC)).FnRef = (*TNameContext)(unsafe.Pointer(pTopNC)).FnRef + 1
			if pTopNC == pNC {
				break
			}
			pTopNC = (*TNameContext)(unsafe.Pointer(pTopNC)).FpNext
			goto _9
		_9:
		}
		return int32(WRC_Prune)
	} else {
		return int32(WRC_Abort)
	}
	return r
}

// C documentation
//
//	/*
//	** Tag the given column as being part of the PRIMARY KEY
//	*/
func _makeColumnPartOfPrimaryKey(tls *libc.TLS, pParse uintptr, pCol uintptr) {
	var v1 uintptr
	_ = v1
	v1 = pCol + 10
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(COLFLAG_PRIMKEY))
	if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15325, 0)
	}
}

// C documentation
//
//	/*
//	** Open an mem file handle.
//	*/
func _memdbOpen(tls *libc.TLS, pVfs uintptr, zName uintptr, pFd uintptr, flags int32, pOutFlags uintptr) (r int32) {
	var apNew, p, pFile, pVfsMutex, v3 uintptr
	var i, szName, v2 int32
	_, _, _, _, _, _, _, _ = apNew, i, p, pFile, pVfsMutex, szName, v2, v3
	pFile = pFd
	p = uintptr(0)
	_ = pVfs
	libc.Xmemset(tls, pFile, 0, uint32(12))
	szName = _sqlite3Strlen30(tls, zName)
	if szName > int32(1) && (int32(**(**int8)(__ccgo_up(zName))) == int32('/') || int32(**(**int8)(__ccgo_up(zName))) == int32('\\')) {
		pVfsMutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1))
		Xsqlite3_mutex_enter(tls, pVfsMutex)
		i = 0
		for {
			if !(i < _memdb_g.FnMemStore) {
				break
			}
			if libc.Xstrcmp(tls, (*TMemStore)(unsafe.Pointer(**(**uintptr)(__ccgo_up(_memdb_g.FapMemStore + uintptr(i)*4)))).FzFName, zName) == 0 {
				p = **(**uintptr)(__ccgo_up(_memdb_g.FapMemStore + uintptr(i)*4))
				break
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if p == uintptr(0) {
			p = _sqlite3Malloc(tls, uint64(int64(56)+int64(szName)+int64(3)))
			if p == uintptr(0) {
				Xsqlite3_mutex_leave(tls, pVfsMutex)
				return int32(SQLITE_NOMEM)
			}
			apNew = _sqlite3Realloc(tls, _memdb_g.FapMemStore, uint64(int64(4)*(int64(1)+int64(_memdb_g.FnMemStore))))
			if apNew == uintptr(0) {
				Xsqlite3_free(tls, p)
				Xsqlite3_mutex_leave(tls, pVfsMutex)
				return int32(SQLITE_NOMEM)
			}
			v3 = uintptr(unsafe.Pointer(&_memdb_g))
			v2 = *(*int32)(unsafe.Pointer(v3))
			*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
			**(**uintptr)(__ccgo_up(apNew + uintptr(v2)*4)) = p
			_memdb_g.FapMemStore = apNew
			libc.Xmemset(tls, p, 0, uint32(56))
			(*TMemStore)(unsafe.Pointer(p)).FmFlags = uint32(libc.Int32FromInt32(SQLITE_DESERIALIZE_RESIZEABLE) | libc.Int32FromInt32(SQLITE_DESERIALIZE_FREEONCLOSE))
			(*TMemStore)(unsafe.Pointer(p)).FszMax = _sqlite3Config.FmxMemdbSize
			(*TMemStore)(unsafe.Pointer(p)).FzFName = p + 1*56
			libc.Xmemcpy(tls, (*TMemStore)(unsafe.Pointer(p)).FzFName, zName, uint32(szName+int32(1)))
			(*TMemStore)(unsafe.Pointer(p)).FpMutex = Xsqlite3_mutex_alloc(tls, SQLITE_MUTEX_FAST)
			if (*TMemStore)(unsafe.Pointer(p)).FpMutex == uintptr(0) {
				_memdb_g.FnMemStore = _memdb_g.FnMemStore - 1
				Xsqlite3_free(tls, p)
				Xsqlite3_mutex_leave(tls, pVfsMutex)
				return int32(SQLITE_NOMEM)
			}
			(*TMemStore)(unsafe.Pointer(p)).FnRef = int32(1)
			_memdbEnter(tls, p)
		} else {
			_memdbEnter(tls, p)
			(*TMemStore)(unsafe.Pointer(p)).FnRef = (*TMemStore)(unsafe.Pointer(p)).FnRef + 1
		}
		Xsqlite3_mutex_leave(tls, pVfsMutex)
	} else {
		p = _sqlite3Malloc(tls, uint64(56))
		if p == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, p, 0, uint32(56))
		(*TMemStore)(unsafe.Pointer(p)).FmFlags = uint32(libc.Int32FromInt32(SQLITE_DESERIALIZE_RESIZEABLE) | libc.Int32FromInt32(SQLITE_DESERIALIZE_FREEONCLOSE))
		(*TMemStore)(unsafe.Pointer(p)).FszMax = _sqlite3Config.FmxMemdbSize
	}
	(*TMemFile)(unsafe.Pointer(pFile)).FpStore = p
	if pOutFlags != uintptr(0) {
		**(**int32)(__ccgo_up(pOutFlags)) = flags | int32(SQLITE_OPEN_MEMORY)
	}
	(*Tsqlite3_file)(unsafe.Pointer(pFd)).FpMethods = uintptr(unsafe.Pointer(&_memdb_io_methods))
	_memdbLeave(tls, p)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Read data from an memdb-file.
//	*/
func _memdbRead(tls *libc.TLS, pFile uintptr, zBuf uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) {
	var p uintptr
	_ = p
	p = (*TMemFile)(unsafe.Pointer(pFile)).FpStore
	_memdbEnter(tls, p)
	if iOfst+int64(iAmt) > (*TMemStore)(unsafe.Pointer(p)).Fsz {
		libc.Xmemset(tls, zBuf, 0, uint32(iAmt))
		if iOfst < (*TMemStore)(unsafe.Pointer(p)).Fsz {
			libc.Xmemcpy(tls, zBuf, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr(iOfst), uint32((*TMemStore)(unsafe.Pointer(p)).Fsz-iOfst))
		}
		_memdbLeave(tls, p)
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
	}
	libc.Xmemcpy(tls, zBuf, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr(iOfst), uint32(iAmt))
	_memdbLeave(tls, p)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Write data to an memdb-file.
//	*/
func _memdbWrite(tls *libc.TLS, pFile uintptr, z uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) {
	var p uintptr
	var rc, v1 int32
	var v2 bool
	_, _, _, _ = p, rc, v1, v2
	p = (*TMemFile)(unsafe.Pointer(pFile)).FpStore
	_memdbEnter(tls, p)
	if (*TMemStore)(unsafe.Pointer(p)).FmFlags&uint32(SQLITE_DESERIALIZE_READONLY) != 0 {
		/* Can't happen: memdbLock() will return SQLITE_READONLY before
		 ** reaching this point */
		_memdbLeave(tls, p)
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(3)<<libc.Int32FromInt32(8)
	}
	if iOfst+int64(iAmt) > (*TMemStore)(unsafe.Pointer(p)).Fsz {
		if v2 = iOfst+int64(iAmt) > (*TMemStore)(unsafe.Pointer(p)).FszAlloc; v2 {
			v1 = _memdbEnlarge(tls, p, iOfst+int64(iAmt))
			rc = v1
		}
		if v2 && v1 != SQLITE_OK {
			_memdbLeave(tls, p)
			return rc
		}
		if iOfst > (*TMemStore)(unsafe.Pointer(p)).Fsz {
			libc.Xmemset(tls, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr((*TMemStore)(unsafe.Pointer(p)).Fsz), 0, uint32(iOfst-(*TMemStore)(unsafe.Pointer(p)).Fsz))
		}
		(*TMemStore)(unsafe.Pointer(p)).Fsz = iOfst + int64(iAmt)
	}
	libc.Xmemcpy(tls, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr(iOfst), z, uint32(iAmt))
	_memdbLeave(tls, p)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Read data from the in-memory journal file.  This is the implementation
//	** of the sqlite3_vfs.xRead method.
//	*/
func _memjrnlRead(tls *libc.TLS, pJfd uintptr, zBuf uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) {
	var iChunkOffset, iSpace, nCopy, nRead, v5 int32
	var iOff Tsqlite3_int64
	var p, pChunk, zOut, v2 uintptr
	var v3 bool
	var v6 int64
	_, _, _, _, _, _, _, _, _, _, _, _ = iChunkOffset, iOff, iSpace, nCopy, nRead, p, pChunk, zOut, v2, v3, v5, v6
	p = pJfd
	zOut = zBuf
	nRead = iAmt
	if int64(iAmt)+iOfst > (*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FiOffset {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
	}
	if (*TMemJournal)(unsafe.Pointer(p)).Freadpoint.FiOffset != iOfst || iOfst == 0 {
		iOff = 0
		pChunk = (*TMemJournal)(unsafe.Pointer(p)).FpFirst
		for {
			if !(pChunk != 0 && iOff+int64((*TMemJournal)(unsafe.Pointer(p)).FnChunkSize) <= iOfst) {
				break
			}
			iOff = iOff + int64((*TMemJournal)(unsafe.Pointer(p)).FnChunkSize)
			goto _1
		_1:
			;
			pChunk = (*TFileChunk)(unsafe.Pointer(pChunk)).FpNext
		}
	} else {
		pChunk = (*TMemJournal)(unsafe.Pointer(p)).Freadpoint.FpChunk
	}
	iChunkOffset = int32(iOfst % int64((*TMemJournal)(unsafe.Pointer(p)).FnChunkSize))
	for {
		iSpace = (*TMemJournal)(unsafe.Pointer(p)).FnChunkSize - iChunkOffset
		if nRead < (*TMemJournal)(unsafe.Pointer(p)).FnChunkSize-iChunkOffset {
			v5 = nRead
		} else {
			v5 = (*TMemJournal)(unsafe.Pointer(p)).FnChunkSize - iChunkOffset
		}
		nCopy = v5
		libc.Xmemcpy(tls, zOut, pChunk+4+uintptr(iChunkOffset), uint32(nCopy))
		zOut = zOut + uintptr(nCopy)
		nRead = nRead - iSpace
		iChunkOffset = 0
		goto _4
	_4:
		;
		if v3 = nRead >= 0; v3 {
			v2 = (*TFileChunk)(unsafe.Pointer(pChunk)).FpNext
			pChunk = v2
		}
		if !(v3 && v2 != uintptr(0) && nRead > 0) {
			break
		}
	}
	if pChunk != 0 {
		v6 = iOfst + int64(iAmt)
	} else {
		v6 = 0
	}
	(*TMemJournal)(unsafe.Pointer(p)).Freadpoint.FiOffset = v6
	(*TMemJournal)(unsafe.Pointer(p)).Freadpoint.FpChunk = pChunk
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Write data to the file.
//	*/
func _memjrnlWrite(tls *libc.TLS, pJfd uintptr, zBuf uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) {
	var iChunkOffset, iSpace, nWrite, rc, v1 int32
	var p, pChunk, pNew, zWrite, v2 uintptr
	_, _, _, _, _, _, _, _, _, _ = iChunkOffset, iSpace, nWrite, p, pChunk, pNew, rc, zWrite, v1, v2
	p = pJfd
	nWrite = iAmt
	zWrite = zBuf
	/* If the file should be created now, create it and write the new data
	 ** into the file on disk. */
	if (*TMemJournal)(unsafe.Pointer(p)).FnSpill > 0 && int64(iAmt)+iOfst > int64((*TMemJournal)(unsafe.Pointer(p)).FnSpill) {
		rc = _memjrnlCreateFile(tls, p)
		if rc == SQLITE_OK {
			rc = _sqlite3OsWrite(tls, pJfd, zBuf, iAmt, iOfst)
		}
		return rc
	} else {
		/* An in-memory journal file should only ever be appended to. Random
		 ** access writes are not required. The only exception to this is when
		 ** the in-memory journal is being used by a connection using the
		 ** atomic-write optimization. In this case the first 28 bytes of the
		 ** journal file may be written as part of committing the transaction. */
		if iOfst > 0 && iOfst != (*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FiOffset {
			_memjrnlTruncate(tls, pJfd, iOfst)
		}
		if iOfst == 0 && (*TMemJournal)(unsafe.Pointer(p)).FpFirst != 0 {
			libc.Xmemcpy(tls, (*TMemJournal)(unsafe.Pointer(p)).FpFirst+4, zBuf, uint32(iAmt))
		} else {
			for nWrite > 0 {
				pChunk = (*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FpChunk
				iChunkOffset = int32((*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FiOffset % int64((*TMemJournal)(unsafe.Pointer(p)).FnChunkSize))
				if nWrite < (*TMemJournal)(unsafe.Pointer(p)).FnChunkSize-iChunkOffset {
					v1 = nWrite
				} else {
					v1 = (*TMemJournal)(unsafe.Pointer(p)).FnChunkSize - iChunkOffset
				}
				iSpace = v1
				if iChunkOffset == 0 {
					/* New chunk is required to extend the file. */
					pNew = Xsqlite3_malloc(tls, int32(libc.Uint32FromInt64(12)+uint32((*TMemJournal)(unsafe.Pointer(p)).FnChunkSize-libc.Int32FromInt32(8))))
					if !(pNew != 0) {
						return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
					}
					(*TFileChunk)(unsafe.Pointer(pNew)).FpNext = uintptr(0)
					if pChunk != 0 {
						(*TFileChunk)(unsafe.Pointer(pChunk)).FpNext = pNew
					} else {
						(*TMemJournal)(unsafe.Pointer(p)).FpFirst = pNew
					}
					v2 = pNew
					(*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FpChunk = v2
					pChunk = v2
				}
				libc.Xmemcpy(tls, pChunk+4+uintptr(iChunkOffset), zWrite, uint32(iSpace))
				zWrite = zWrite + uintptr(iSpace)
				nWrite = nWrite - iSpace
				(*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FiOffset += int64(iSpace)
			}
		}
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** The pFunc is the only aggregate function in the query.  Check to see
//	** if the query is a candidate for the min/max optimization.
//	**
//	** If the query is a candidate for the min/max optimization, then set
//	** *ppMinMax to be an ORDER BY clause to be used for the optimization
//	** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
//	** whether pFunc is a min() or max() function.
//	**
//	** If the query is not a candidate for the min/max optimization, return
//	** WHERE_ORDERBY_NORMAL (which must be zero).
//	**
//	** This routine must be called after aggregate functions have been
//	** located but before their arguments have been subjected to aggregate
//	** analysis.
//	*/
func _minMaxQuery(tls *libc.TLS, db uintptr, pFunc uintptr, ppMinMax uintptr) (r Tu8) {
	var eRet int32
	var pEList, pOrderBy, zFunc, v1 uintptr
	var sortFlags Tu8
	_, _, _, _, _, _ = eRet, pEList, pOrderBy, sortFlags, zFunc, v1
	eRet = WHERE_ORDERBY_NORMAL
	sortFlags = uint8(0)
	pEList = *(*uintptr)(unsafe.Pointer(pFunc + 20))
	if pEList == uintptr(0) || (*TExprList)(unsafe.Pointer(pEList)).FnExpr != int32(1) || (*TExpr)(unsafe.Pointer(pFunc)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) || (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_MinMaxOpt)) != uint32(0) {
		return uint8(eRet)
	}
	zFunc = *(*uintptr)(unsafe.Pointer(pFunc + 8))
	if _sqlite3StrICmp(tls, zFunc, __ccgo_ts+18024) == 0 {
		eRet = int32(WHERE_ORDERBY_MIN)
		if _sqlite3ExprCanBeNull(tls, (*(*TExprList_item)(unsafe.Pointer(pEList + 8))).FpExpr) != 0 {
			sortFlags = uint8(KEYINFO_ORDER_BIGNULL)
		}
	} else {
		if _sqlite3StrICmp(tls, zFunc, __ccgo_ts+18028) == 0 {
			eRet = int32(WHERE_ORDERBY_MAX)
			sortFlags = uint8(KEYINFO_ORDER_DESC)
		} else {
			return uint8(eRet)
		}
	}
	v1 = _sqlite3ExprListDup(tls, db, pEList, 0)
	pOrderBy = v1
	**(**uintptr)(__ccgo_up(ppMinMax)) = v1
	if pOrderBy != 0 {
		(*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8))).Ffg.FsortFlags = sortFlags
	}
	return uint8(eRet)
}

// C documentation
//
//	/*
//	** Move the cursor down to a new child page.  The newPgno argument is the
//	** page number of the child page to move to.
//	**
//	** This function returns SQLITE_CORRUPT if the page-header flags field of
//	** the new child page does not match the flags field of the parent (i.e.
//	** if an intkey page appears to be the parent of a non-intkey page, or
//	** vice-versa).
//	*/
func _moveToChild(tls *libc.TLS, pCur uintptr, newPgno Tu32) (r int32) {
	var rc int32
	var v1 uintptr
	var v2 Ti8
	_, _, _ = rc, v1, v2
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) >= libc.Int32FromInt32(BTCURSOR_MAX_DEPTH)-libc.Int32FromInt32(1) {
		return _sqlite3CorruptError(tls, int32(78687))
	}
	(*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0)
	v1 = pCur + 1
	*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl)))
	**(**Tu16)(__ccgo_up(pCur + 72 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*2)) = (*TBtCursor)(unsafe.Pointer(pCur)).Fix
	**(**uintptr)(__ccgo_up(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4)) = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
	(*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(0)
	(*TBtCursor)(unsafe.Pointer(pCur)).FiPage = (*TBtCursor)(unsafe.Pointer(pCur)).FiPage + 1
	rc = _getAndInitPage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBt, newPgno, pCur+116, int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurPagerFlags))
	if rc == SQLITE_OK && (int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell) < int32(1) || int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FintKey) != int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurIntKey)) {
		_releasePage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage)
		rc = _sqlite3CorruptError(tls, int32(78701))
	}
	if rc != 0 {
		v1 = pCur + 68
		*(*Ti8)(unsafe.Pointer(v1)) = *(*Ti8)(unsafe.Pointer(v1)) - 1
		v2 = *(*Ti8)(unsafe.Pointer(v1))
		(*TBtCursor)(unsafe.Pointer(pCur)).FpPage = **(**uintptr)(__ccgo_up(pCur + 120 + uintptr(v2)*4))
	}
	return rc
}

// C documentation
//
//	/*
//	** Move the cursor to point to the root page of its b-tree structure.
//	**
//	** If the table has a virtual root page, then the cursor is moved to point
//	** to the virtual root page instead of the actual root page. A table has a
//	** virtual root page when the actual root page contains no cells and a
//	** single child page. This can only happen with the table rooted at page 1.
//	**
//	** If the b-tree structure is empty, the cursor state is set to
//	** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
//	** the cursor is set to point to the first cell located on the root
//	** (or virtual root) page and the cursor state is set to CURSOR_VALID.
//	**
//	** If this function returns successfully, it may be assumed that the
//	** page-header flags indicate that the [virtual] root-page is the expected
//	** kind of b-tree page (i.e. if when opening the cursor the caller did not
//	** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
//	** indicating a table b-tree, or if the caller did specify a KeyInfo
//	** structure the flags byte is set to 0x02 or 0x0A, indicating an index
//	** b-tree).
//	*/
func _moveToRoot(tls *libc.TLS, pCur uintptr) (r int32) {
	var pRoot, v2 uintptr
	var rc int32
	var subpage TPgno
	var v1 Ti8
	_, _, _, _, _ = pRoot, rc, subpage, v1, v2
	rc = SQLITE_OK
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) >= 0 {
		if (*TBtCursor)(unsafe.Pointer(pCur)).FiPage != 0 {
			_releasePageNotNull(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage)
			for {
				v2 = pCur + 68
				*(*Ti8)(unsafe.Pointer(v2)) = *(*Ti8)(unsafe.Pointer(v2)) - 1
				v1 = *(*Ti8)(unsafe.Pointer(v2))
				if !(v1 != 0) {
					break
				}
				_releasePageNotNull(tls, **(**uintptr)(__ccgo_up(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4)))
			}
			v2 = **(**uintptr)(__ccgo_up(pCur + 120))
			(*TBtCursor)(unsafe.Pointer(pCur)).FpPage = v2
			pRoot = v2
			goto skip_init
		}
	} else {
		if (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot == uint32(0) {
			(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID)
			return int32(SQLITE_EMPTY)
		} else {
			if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) {
				if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == int32(CURSOR_FAULT) {
					return (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext
				}
				_sqlite3BtreeClearCursor(tls, pCur)
			}
			rc = _getAndInitPage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBt, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, pCur+116, int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurPagerFlags))
			if rc != SQLITE_OK {
				(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID)
				return rc
			}
			(*TBtCursor)(unsafe.Pointer(pCur)).FiPage = 0
			(*TBtCursor)(unsafe.Pointer(pCur)).FcurIntKey = (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FintKey
		}
	}
	pRoot = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
	/* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
	 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
	 ** NULL, the caller expects a table b-tree. If this is not the case,
	 ** return an SQLITE_CORRUPT error.
	 **
	 ** Earlier versions of SQLite assumed that this test could not fail
	 ** if the root page was already loaded when this function was called (i.e.
	 ** if pCur->iPage>=0). But this is not so if the database is corrupted
	 ** in such a way that page pRoot is linked into a second b-tree table
	 ** (or the freelist).  */
	if int32((*TMemPage)(unsafe.Pointer(pRoot)).FisInit) == 0 || libc.BoolInt32((*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo == uintptr(0)) != int32((*TMemPage)(unsafe.Pointer(pRoot)).FintKey) {
		return _sqlite3CorruptError(tls, int32(78836))
	}
	goto skip_init
skip_init:
	;
	(*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(0)
	(*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0)
	v2 = pCur + 1
	*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) & ^(libc.Int32FromInt32(BTCF_AtLast) | libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl)))
	if int32((*TMemPage)(unsafe.Pointer(pRoot)).FnCell) > 0 {
		(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_VALID)
	} else {
		if !((*TMemPage)(unsafe.Pointer(pRoot)).Fleaf != 0) {
			if (*TMemPage)(unsafe.Pointer(pRoot)).Fpgno != uint32(1) {
				return _sqlite3CorruptError(tls, int32(78848))
			}
			subpage = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pRoot)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pRoot)).FhdrOffset)+int32(8)))
			(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_VALID)
			rc = _moveToChild(tls, pCur, subpage)
		} else {
			(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID)
			rc = int32(SQLITE_EMPTY)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** This routine is called to process a compound query form from
//	** two or more separate queries using UNION, UNION ALL, EXCEPT, or
//	** INTERSECT
//	**
//	** "p" points to the right-most of the two queries.  the query on the
//	** left is p->pPrior.  The left query could also be a compound query
//	** in which case this routine will be called recursively.
//	**
//	** The results of the total query are to be written into a destination
//	** of type eDest with parameter iParm.
//	**
//	** Example 1:  Consider a three-way compound SQL statement.
//	**
//	**     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
//	**
//	** This statement is parsed up as follows:
//	**
//	**     SELECT c FROM t3
//	**      |
//	**      `----->  SELECT b FROM t2
//	**                |
//	**                `------>  SELECT a FROM t1
//	**
//	** The arrows in the diagram above represent the Select.pPrior pointer.
//	** So if this routine is called with p equal to the t3 query, then
//	** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
//	**
//	** Notice that because of the way SQLite parses compound SELECTs, the
//	** individual selects always group from left to right.
//	*/
func _multiSelect(tls *libc.TLS, pParse uintptr, p uintptr, pDest uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var addr, rc int32
	var db, pDelete, pOne, pPrior, v uintptr
	var _ /* dest at bp+0 */ TSelectDest
	var _ /* nLimit at bp+28 */ int32
	_, _, _, _, _, _, _ = addr, db, pDelete, pOne, pPrior, rc, v
	rc = SQLITE_OK       /* Alternative data destination */
	pDelete = uintptr(0) /* Database connection */
	/* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
	 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
	 */
	/* Calling function guarantees this much */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pPrior = (*TSelect)(unsafe.Pointer(p)).FpPrior
	**(**TSelectDest)(__ccgo_up(bp)) = **(**TSelectDest)(__ccgo_up(pDest))
	v = _sqlite3GetVdbe(tls, pParse)
	/* The VDBE already created by calling function */
	/* Create the destination temporary table if necessary
	 */
	if int32((**(**TSelectDest)(__ccgo_up(bp))).FeDest) == int32(SRT_EphemTab) {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (**(**TSelectDest)(__ccgo_up(bp))).FiSDParm, (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr)
		(**(**TSelectDest)(__ccgo_up(bp))).FeDest = uint8(SRT_Table)
	}
	/* Special handling for a compound-select that originates as a VALUES clause.
	 */
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_MultiValue) != 0 {
		rc = _multiSelectValues(tls, pParse, p, bp)
		if rc >= 0 {
			goto multi_select_end
		}
		rc = SQLITE_OK
	}
	/* Make sure all SELECTs in the statement have the same number of elements
	 ** in their result sets.
	 */
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Recursive) != uint32(0) && _hasAnchor(tls, p) != 0 {
		_generateWithRecursiveQuery(tls, pParse, p, bp)
	} else {
		if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 {
			/* If the compound has an ORDER BY clause, then always use the merge
			 ** algorithm. */
			return _multiSelectByMerge(tls, pParse, p, pDest)
		} else {
			if int32((*TSelect)(unsafe.Pointer(p)).Fop) != int32(TK_ALL) {
				/* If the compound is EXCEPT, INTERSECT, or UNION (anything other than
				 ** UNION ALL) then also always use the merge algorithm.  However, the
				 ** multiSelectByMerge() routine requires that the compound have an
				 ** ORDER BY clause, and it doesn't right now.  So invent one first. */
				pOne = _sqlite3ExprInt32(tls, db, int32(1))
				(*TSelect)(unsafe.Pointer(p)).FpOrderBy = _sqlite3ExprListAppend(tls, pParse, uintptr(0), pOne)
				if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
					goto multi_select_end
				}
				*(*Tu16)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy + 8 + 16)) = uint16(1)
				return _multiSelectByMerge(tls, pParse, p, pDest)
			} else {
				/* For a UNION ALL compound without ORDER BY, simply run the left
				 ** query, then run the right query */
				addr = 0
				**(**int32)(__ccgo_up(bp + 28)) = 0 /* Initialize to suppress harmless compiler warning */
				if (*TSelect)(unsafe.Pointer(pPrior)).FpPrior == uintptr(0) {
					_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22064, 0)
					_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22079, 0)
				}
				(*TSelect)(unsafe.Pointer(pPrior)).FiLimit = (*TSelect)(unsafe.Pointer(p)).FiLimit
				(*TSelect)(unsafe.Pointer(pPrior)).FiOffset = (*TSelect)(unsafe.Pointer(p)).FiOffset
				(*TSelect)(unsafe.Pointer(pPrior)).FpLimit = _sqlite3ExprDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpLimit, 0)
				rc = _sqlite3Select(tls, pParse, pPrior, bp)
				_sqlite3ExprDelete(tls, db, (*TSelect)(unsafe.Pointer(pPrior)).FpLimit)
				(*TSelect)(unsafe.Pointer(pPrior)).FpLimit = uintptr(0)
				if rc != 0 {
					goto multi_select_end
				}
				(*TSelect)(unsafe.Pointer(p)).FpPrior = uintptr(0)
				(*TSelect)(unsafe.Pointer(p)).FiLimit = (*TSelect)(unsafe.Pointer(pPrior)).FiLimit
				(*TSelect)(unsafe.Pointer(p)).FiOffset = (*TSelect)(unsafe.Pointer(pPrior)).FiOffset
				if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 {
					addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), (*TSelect)(unsafe.Pointer(p)).FiLimit)
					if (*TSelect)(unsafe.Pointer(p)).FiOffset != 0 {
						_sqlite3VdbeAddOp3(tls, v, int32(OP_OffsetLimit), (*TSelect)(unsafe.Pointer(p)).FiLimit, (*TSelect)(unsafe.Pointer(p)).FiOffset+int32(1), (*TSelect)(unsafe.Pointer(p)).FiOffset)
					}
				}
				_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+21719, 0)
				rc = _sqlite3Select(tls, pParse, p, bp)
				pDelete = (*TSelect)(unsafe.Pointer(p)).FpPrior
				(*TSelect)(unsafe.Pointer(p)).FpPrior = pPrior
				(*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEstAdd(tls, (*TSelect)(unsafe.Pointer(p)).FnSelectRow, (*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow)
				if (*TSelect)(unsafe.Pointer(p)).FpLimit != 0 && _sqlite3ExprIsInteger(tls, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpLimit)).FpLeft, bp+28, pParse) != 0 && **(**int32)(__ccgo_up(bp + 28)) > 0 && int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32(_sqlite3LogEst(tls, uint64(**(**int32)(__ccgo_up(bp + 28))))) {
					(*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEst(tls, uint64(**(**int32)(__ccgo_up(bp + 28))))
				}
				if addr != 0 {
					_sqlite3VdbeJumpHere(tls, v, addr)
				}
				if (*TSelect)(unsafe.Pointer(p)).FpNext == uintptr(0) {
					_sqlite3VdbeExplainPop(tls, pParse)
				}
			}
		}
	}
	goto multi_select_end
multi_select_end:
	;
	(*TSelectDest)(unsafe.Pointer(pDest)).FiSdst = (**(**TSelectDest)(__ccgo_up(bp))).FiSdst
	(*TSelectDest)(unsafe.Pointer(pDest)).FnSdst = (**(**TSelectDest)(__ccgo_up(bp))).FnSdst
	(*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2 = (**(**TSelectDest)(__ccgo_up(bp))).FiSDParm2
	if pDelete != 0 {
		_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3SelectDeleteGeneric), pDelete)
	}
	return rc
}

// C documentation
//
//	/*
//	** Generate code for a compound SELECT statement using a merge
//	** algorithm.  The compound must have an ORDER BY clause for this
//	** to work.
//	**
//	** We assume a query of the following form:
//	**
//	**      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
//	**
//	** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
//	** is to code both <selectA> and <selectB> with the ORDER BY clause as
//	** co-routines.  Then run the co-routines in parallel and merge the results
//	** into the output.  In addition to the two coroutines (called selectA and
//	** selectB) there are 7 subroutines:
//	**
//	**    outA:    Move the output of the selectA coroutine into the output
//	**             of the compound query.
//	**
//	**    outB:    Move the output of the selectB coroutine into the output
//	**             of the compound query.  (Only generated for UNION and
//	**             UNION ALL.  EXCEPT and INTERSECT never output a row that
//	**             appears only in B.)
//	**
//	**    AltB:    Called when there is data from both coroutines and A<B.
//	**
//	**    AeqB:    Called when there is data from both coroutines and A==B.
//	**
//	**    AgtB:    Called when there is data from both coroutines and A>B.
//	**
//	**    EofA:    Called when data is exhausted from selectA.
//	**
//	**    EofB:    Called when data is exhausted from selectB.
//	**
//	** The implementation of the latter five subroutines depend on which
//	** <operator> is used:
//	**
//	**
//	**             UNION ALL         UNION            EXCEPT          INTERSECT
//	**          -------------  -----------------  --------------  -----------------
//	**   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
//	**
//	**   AeqB:   outA, nextA         nextA             nextA         outA, nextA
//	**
//	**   AgtB:   outB, nextB      outB, nextB          nextB            nextB
//	**
//	**   EofA:   outB, nextB      outB, nextB          halt             halt
//	**
//	**   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
//	**
//	** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
//	** causes an immediate jump to EofA and an EOF on B following nextB causes
//	** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
//	** following nextX causes a jump to the end of the select processing.
//	**
//	** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
//	** within the output subroutine.  The regPrev register set holds the previously
//	** output value.  A comparison is made against this value and the output
//	** is skipped if the next results would be the same as the previous.
//	**
//	** The implementation plan is to implement the two coroutines and seven
//	** subroutines first, then put the control logic at the bottom.  Like this:
//	**
//	**          goto Init
//	**     coA: coroutine for left query (A)
//	**     coB: coroutine for right query (B)
//	**    outA: output one row of A
//	**    outB: output one row of B (UNION and UNION ALL only)
//	**    EofA: ...
//	**    EofB: ...
//	**    AltB: ...
//	**    AeqB: ...
//	**    AgtB: ...
//	**    Init: initialize coroutine registers
//	**          yield coA, on eof goto EofA
//	**          yield coB, on eof goto EofB
//	**    Cmpr: Compare A, B
//	**          Jump AltB, AeqB, AgtB
//	**     End: ...
//	**
//	** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
//	** actually called using Gosub and they do not Return.  EofA and EofB loop
//	** until all data is exhausted then jump to the "end" label.  AltB, AeqB,
//	** and AgtB jump to either Cmpr or to one of EofA or EofB.
//	*/
func _multiSelectByMerge(tls *libc.TLS, pParse uintptr, p uintptr, pDest uintptr) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var aPermute, db, pItem, pItem1, pKeyDup, pKeyMerge, pNew, pOrderBy, pPrior, pSplit, v, v3 uintptr
	var addr1, addrAeqB, addrAgtB, addrAltB, addrEofA, addrEofA_noB, addrEofB, addrOutA, addrOutB, addrSelectA, addrSelectB, bKeep, i, j, labelCmpr, labelEnd, nExpr, nOrderBy, nSelect, op, regAddrA, regAddrB, regLimitA, regLimitB, regOutA, regOutB, regPrev, savedLimit, savedOffset, v4 int32
	var _ /* destA at bp+0 */ TSelectDest
	var _ /* destB at bp+28 */ TSelectDest
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aPermute, addr1, addrAeqB, addrAgtB, addrAltB, addrEofA, addrEofA_noB, addrEofB, addrOutA, addrOutB, addrSelectA, addrSelectB, bKeep, db, i, j, labelCmpr, labelEnd, nExpr, nOrderBy, nSelect, op, pItem, pItem1, pKeyDup, pKeyMerge, pNew, pOrderBy, pPrior, pSplit, regAddrA, regAddrB, regLimitA, regLimitB, regOutA, regOutB, regPrev, savedLimit, savedOffset, v, v3, v4 /* Address of the output-A subroutine */
	addrOutB = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
	pKeyDup = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Mapping from ORDER BY terms to result set columns */
	/* "Managed" code needs this.  Ticket #3382. */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	/* Already thrown the error if VDBE alloc failed */
	labelEnd = _sqlite3VdbeMakeLabel(tls, pParse)
	labelCmpr = _sqlite3VdbeMakeLabel(tls, pParse)
	/* Patch up the ORDER BY clause
	 */
	op = int32((*TSelect)(unsafe.Pointer(p)).Fop)
	pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy
	nOrderBy = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr
	/* For operators other than UNION ALL we have to make sure that
	 ** the ORDER BY clause covers every term of the result set.  Add
	 ** terms to the ORDER BY clause as necessary.
	 */
	if op != int32(TK_ALL) {
		i = int32(1)
		for {
			if !(int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 && i <= (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr) {
				break
			}
			j = 0
			pItem = pOrderBy + 8
			for {
				if !(j < nOrderBy) {
					break
				}
				if int32((*(*struct {
					FiOrderByCol Tu16
					FiAlias      Tu16
				})(unsafe.Pointer(pItem + 16))).FiOrderByCol) == i {
					break
				}
				goto _2
			_2:
				;
				j = j + 1
				pItem += 20
			}
			if j == nOrderBy {
				pNew = _sqlite3ExprInt32(tls, db, i)
				if pNew == uintptr(0) {
					return int32(SQLITE_NOMEM)
				}
				v3 = _sqlite3ExprListAppend(tls, pParse, pOrderBy, pNew)
				pOrderBy = v3
				(*TSelect)(unsafe.Pointer(p)).FpOrderBy = v3
				if pOrderBy != 0 {
					v4 = nOrderBy
					nOrderBy = nOrderBy + 1
					*(*Tu16)(unsafe.Pointer(pOrderBy + 8 + uintptr(v4)*20 + 16)) = uint16(i)
				}
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	/* Compute the comparison permutation and keyinfo that is used with
	 ** the permutation to determine if the next row of results comes
	 ** from selectA or selectB.  Also add literal collations to the
	 ** ORDER BY clause terms so that when selectA and selectB are
	 ** evaluated, they use the correct collation.
	 */
	aPermute = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(nOrderBy+libc.Int32FromInt32(1))))
	if aPermute != 0 {
		bKeep = 0
		**(**Tu32)(__ccgo_up(aPermute)) = uint32(nOrderBy)
		i = int32(1)
		pItem1 = pOrderBy + 8
		for {
			if !(i <= nOrderBy) {
				break
			}
			**(**Tu32)(__ccgo_up(aPermute + uintptr(i)*4)) = uint32(int32((*(*struct {
				FiOrderByCol Tu16
				FiAlias      Tu16
			})(unsafe.Pointer(pItem1 + 16))).FiOrderByCol) - int32(1))
			if **(**Tu32)(__ccgo_up(aPermute + uintptr(i)*4)) != uint32(i)-uint32(1) {
				bKeep = int32(1)
			}
			goto _5
		_5:
			;
			i = i + 1
			pItem1 += 20
		}
		if bKeep == 0 {
			_sqlite3DbFreeNN(tls, db, aPermute)
			aPermute = uintptr(0)
		}
	}
	pKeyMerge = _multiSelectByMergeKeyInfo(tls, pParse, p, int32(1))
	/* Allocate a range of temporary registers and the KeyInfo needed
	 ** for the logic that removes duplicate result rows when the
	 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
	 */
	if op == int32(TK_ALL) {
		regPrev = 0
	} else {
		nExpr = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr
		regPrev = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += nExpr + int32(1)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regPrev)
		pKeyDup = _sqlite3KeyInfoAlloc(tls, db, nExpr, int32(1))
		if pKeyDup != 0 {
			i = 0
			for {
				if !(i < nExpr) {
					break
				}
				*(*uintptr)(unsafe.Pointer(pKeyDup + 20 + uintptr(i)*4)) = _multiSelectCollSeq(tls, pParse, p, i)
				**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pKeyDup)).FaSortFlags + uintptr(i))) = uint8(0)
				goto _6
			_6:
				;
				i = i + 1
			}
		}
	}
	/* Separate the left and the right query from one another
	 */
	nSelect = int32(1)
	if (op == int32(TK_ALL) || op == int32(TK_UNION)) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_BalancedMerge)) == uint32(0) {
		pSplit = p
		for {
			if !((*TSelect)(unsafe.Pointer(pSplit)).FpPrior != uintptr(0) && int32((*TSelect)(unsafe.Pointer(pSplit)).Fop) == op) {
				break
			}
			nSelect = nSelect + 1
			goto _7
		_7:
			;
			pSplit = (*TSelect)(unsafe.Pointer(pSplit)).FpPrior
		}
	}
	if nSelect <= int32(3) {
		pSplit = p
	} else {
		pSplit = p
		i = int32(2)
		for {
			if !(i < nSelect) {
				break
			}
			pSplit = (*TSelect)(unsafe.Pointer(pSplit)).FpPrior
			goto _8
		_8:
			;
			i = i + int32(2)
		}
	}
	pPrior = (*TSelect)(unsafe.Pointer(pSplit)).FpPrior
	(*TSelect)(unsafe.Pointer(pSplit)).FpPrior = uintptr(0)
	(*TSelect)(unsafe.Pointer(pPrior)).FpNext = uintptr(0)
	(*TSelect)(unsafe.Pointer(pPrior)).FpOrderBy = _sqlite3ExprListDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pOrderBy, 0)
	_sqlite3ResolveOrderGroupBy(tls, pParse, p, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, __ccgo_ts+8876)
	_sqlite3ResolveOrderGroupBy(tls, pParse, pPrior, (*TSelect)(unsafe.Pointer(pPrior)).FpOrderBy, __ccgo_ts+8876)
	/* Compute the limit registers */
	_computeLimitRegisters(tls, pParse, p, labelEnd)
	if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 && op == int32(TK_ALL) {
		v3 = pParse + 48
		*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
		v4 = *(*int32)(unsafe.Pointer(v3))
		regLimitA = v4
		v3 = pParse + 48
		*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
		v4 = *(*int32)(unsafe.Pointer(v3))
		regLimitB = v4
		if (*TSelect)(unsafe.Pointer(p)).FiOffset != 0 {
			v4 = (*TSelect)(unsafe.Pointer(p)).FiOffset + int32(1)
		} else {
			v4 = (*TSelect)(unsafe.Pointer(p)).FiLimit
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), v4, regLimitA)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), regLimitA, regLimitB)
	} else {
		v4 = libc.Int32FromInt32(0)
		regLimitB = v4
		regLimitA = v4
	}
	_sqlite3ExprDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpLimit)
	(*TSelect)(unsafe.Pointer(p)).FpLimit = uintptr(0)
	v3 = pParse + 48
	*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
	v4 = *(*int32)(unsafe.Pointer(v3))
	regAddrA = v4
	v3 = pParse + 48
	*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
	v4 = *(*int32)(unsafe.Pointer(v3))
	regAddrB = v4
	v3 = pParse + 48
	*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
	v4 = *(*int32)(unsafe.Pointer(v3))
	regOutA = v4
	v3 = pParse + 48
	*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
	v4 = *(*int32)(unsafe.Pointer(v3))
	regOutB = v4
	_sqlite3SelectDestInit(tls, bp, int32(SRT_Coroutine), regAddrA)
	_sqlite3SelectDestInit(tls, bp+28, int32(SRT_Coroutine), regAddrB)
	_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22226, libc.VaList(bp+64, _sqlite3SelectOpName(tls, int32((*TSelect)(unsafe.Pointer(p)).Fop))))
	/* Generate a coroutine to evaluate the SELECT statement to the
	 ** left of the compound operator - the "A" select.
	 */
	addrSelectA = _sqlite3VdbeCurrentAddr(tls, v) + int32(1)
	addr1 = _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regAddrA, 0, addrSelectA)
	(*TSelect)(unsafe.Pointer(pPrior)).FiLimit = regLimitA
	_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22237, 0)
	_sqlite3Select(tls, pParse, pPrior, bp)
	_sqlite3VdbeEndCoroutine(tls, v, regAddrA)
	_sqlite3VdbeJumpHere(tls, v, addr1)
	/* Generate a coroutine to evaluate the SELECT statement on
	 ** the right - the "B" select
	 */
	addrSelectB = _sqlite3VdbeCurrentAddr(tls, v) + int32(1)
	addr1 = _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regAddrB, 0, addrSelectB)
	savedLimit = (*TSelect)(unsafe.Pointer(p)).FiLimit
	savedOffset = (*TSelect)(unsafe.Pointer(p)).FiOffset
	(*TSelect)(unsafe.Pointer(p)).FiLimit = regLimitB
	(*TSelect)(unsafe.Pointer(p)).FiOffset = 0
	_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22242, 0)
	_sqlite3Select(tls, pParse, p, bp+28)
	(*TSelect)(unsafe.Pointer(p)).FiLimit = savedLimit
	(*TSelect)(unsafe.Pointer(p)).FiOffset = savedOffset
	_sqlite3VdbeEndCoroutine(tls, v, regAddrB)
	/* Generate a subroutine that outputs the current row of the A
	 ** select as the next output row of the compound select.
	 */
	addrOutA = _generateOutputSubroutine(tls, pParse, p, bp, pDest, regOutA, regPrev, pKeyDup, labelEnd)
	/* Generate a subroutine that outputs the current row of the B
	 ** select as the next output row of the compound select.
	 */
	if op == int32(TK_ALL) || op == int32(TK_UNION) {
		addrOutB = _generateOutputSubroutine(tls, pParse, p, bp+28, pDest, regOutB, regPrev, pKeyDup, labelEnd)
	}
	_sqlite3KeyInfoUnref(tls, pKeyDup)
	/* Generate a subroutine to run when the results from select A
	 ** are exhausted and only data in select B remains.
	 */
	if op == int32(TK_EXCEPT) || op == int32(TK_INTERSECT) {
		v4 = labelEnd
		addrEofA = v4
		addrEofA_noB = v4
	} else {
		addrEofA = _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutB, addrOutB)
		addrEofA_noB = _sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrB, labelEnd)
		_sqlite3VdbeGoto(tls, v, addrEofA)
		(*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEstAdd(tls, (*TSelect)(unsafe.Pointer(p)).FnSelectRow, (*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow)
	}
	/* Generate a subroutine to run when the results from select B
	 ** are exhausted and only data in select A remains.
	 */
	if op == int32(TK_INTERSECT) {
		addrEofB = addrEofA
		if int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32((*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow) {
			(*TSelect)(unsafe.Pointer(p)).FnSelectRow = (*TSelect)(unsafe.Pointer(pPrior)).FnSelectRow
		}
	} else {
		addrEofB = _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutA, addrOutA)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrA, labelEnd)
		_sqlite3VdbeGoto(tls, v, addrEofB)
	}
	/* Generate code to handle the case of A<B
	 */
	addrAltB = _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutA, addrOutA)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrA, addrEofA)
	_sqlite3VdbeGoto(tls, v, labelCmpr)
	/* Generate code to handle the case of A==B
	 */
	if op == int32(TK_ALL) {
		addrAeqB = addrAltB
	} else {
		if op == int32(TK_INTERSECT) {
			addrAeqB = addrAltB
			addrAltB = addrAltB + 1
		} else {
			addrAeqB = addrAltB + int32(1)
		}
	}
	/* Generate code to handle the case of A>B
	 */
	addrAgtB = _sqlite3VdbeCurrentAddr(tls, v)
	if op == int32(TK_ALL) || op == int32(TK_UNION) {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutB, addrOutB)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrB, addrEofB)
		_sqlite3VdbeGoto(tls, v, labelCmpr)
	} else {
		addrAgtB = addrAgtB + 1 /* Just do next-B.  Might as well use the next-B call
		 ** in the next code block */
	}
	/* This code runs once to initialize everything.
	 */
	_sqlite3VdbeJumpHere(tls, v, addr1)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrA, addrEofA_noB)
	/* v---  Also the A>B case for EXCEPT and INTERSECT */
	_sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regAddrB, addrEofB)
	/* Implement the main merge loop
	 */
	if aPermute != uintptr(0) {
		_sqlite3VdbeAddOp4(tls, v, int32(OP_Permutation), 0, 0, 0, aPermute, -int32(15))
	}
	_sqlite3VdbeResolveLabel(tls, v, labelCmpr)
	_sqlite3VdbeAddOp4(tls, v, int32(OP_Compare), (**(**TSelectDest)(__ccgo_up(bp))).FiSdst, (**(**TSelectDest)(__ccgo_up(bp + 28))).FiSdst, nOrderBy, pKeyMerge, -int32(9))
	if aPermute != uintptr(0) {
		_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_PERMUTE))
	}
	_sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addrAltB, addrAeqB, addrAgtB)
	/* Jump to the this point in order to terminate the query.
	 */
	_sqlite3VdbeResolveLabel(tls, v, labelEnd)
	/* Make arrangements to free the 2nd and subsequent arms of the compound
	 ** after the parse has finished */
	if (*TSelect)(unsafe.Pointer(pSplit)).FpPrior != 0 {
		_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3SelectDeleteGeneric), (*TSelect)(unsafe.Pointer(pSplit)).FpPrior)
	}
	(*TSelect)(unsafe.Pointer(pSplit)).FpPrior = pPrior
	(*TSelect)(unsafe.Pointer(pPrior)).FpNext = pSplit
	_sqlite3ExprListDelete(tls, db, (*TSelect)(unsafe.Pointer(pPrior)).FpOrderBy)
	(*TSelect)(unsafe.Pointer(pPrior)).FpOrderBy = uintptr(0)
	/*** TBD:  Insert subroutine calls to close cursors on incomplete
	 **** subqueries ****/
	_sqlite3VdbeExplainPop(tls, pParse)
	return libc.BoolInt32((*TParse)(unsafe.Pointer(pParse)).FnErr != 0)
}

// C documentation
//
//	/*
//	** The select statement passed as the second parameter is a compound SELECT
//	** with an ORDER BY clause. This function allocates and returns a KeyInfo
//	** structure suitable for implementing the ORDER BY.
//	**
//	** Space to hold the KeyInfo structure is obtained from malloc. The calling
//	** function is responsible for ensuring that this structure is eventually
//	** freed.
//	*/
func _multiSelectByMergeKeyInfo(tls *libc.TLS, pParse uintptr, p uintptr, nExtra int32) (r uintptr) {
	var db, pColl, pItem, pOrderBy, pRet, pTerm uintptr
	var i, nOrderBy, v1 int32
	_, _, _, _, _, _, _, _, _ = db, i, nOrderBy, pColl, pItem, pOrderBy, pRet, pTerm, v1
	pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy
	if pOrderBy != uintptr(0) {
		v1 = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr
	} else {
		v1 = 0
	}
	nOrderBy = v1
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pRet = _sqlite3KeyInfoAlloc(tls, db, nOrderBy+nExtra, int32(1))
	if pRet != 0 {
		i = 0
		for {
			if !(i < nOrderBy) {
				break
			}
			pItem = pOrderBy + 8 + uintptr(i)*20
			pTerm = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr
			if (*TExpr)(unsafe.Pointer(pTerm)).Fflags&uint32(EP_Collate) != 0 {
				pColl = _sqlite3ExprCollSeq(tls, pParse, pTerm)
			} else {
				pColl = _multiSelectCollSeq(tls, pParse, p, int32((*(*struct {
					FiOrderByCol Tu16
					FiAlias      Tu16
				})(unsafe.Pointer(pItem + 16))).FiOrderByCol)-int32(1))
				if pColl == uintptr(0) {
					pColl = (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl
				}
				(*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr = _sqlite3ExprAddCollateString(tls, pParse, pTerm, (*TCollSeq)(unsafe.Pointer(pColl)).FzName)
			}
			*(*uintptr)(unsafe.Pointer(pRet + 20 + uintptr(i)*4)) = pColl
			**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pRet)).FaSortFlags + uintptr(i))) = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	return pRet
}

// C documentation
//
//	/*
//	** If pBt points to an empty file then convert that empty file
//	** into a new empty database by initializing the first page of
//	** the database.
//	*/
func _newDatabase(tls *libc.TLS, pBt uintptr) (r int32) {
	var data, pP1, v1 uintptr
	var rc int32
	_, _, _, _ = data, pP1, rc, v1
	if (*TBtShared)(unsafe.Pointer(pBt)).FnPage > uint32(0) {
		return SQLITE_OK
	}
	pP1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1
	data = (*TMemPage)(unsafe.Pointer(pP1)).FaData
	rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pP1)).FpDbPage)
	if rc != 0 {
		return rc
	}
	libc.Xmemcpy(tls, data, uintptr(unsafe.Pointer(&_zMagicHeader)), uint32(16))
	**(**uint8)(__ccgo_up(data + 16)) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FpageSize >> libc.Int32FromInt32(8) & libc.Uint32FromInt32(0xff))
	**(**uint8)(__ccgo_up(data + 17)) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FpageSize >> libc.Int32FromInt32(16) & libc.Uint32FromInt32(0xff))
	**(**uint8)(__ccgo_up(data + 18)) = uint8(1)
	**(**uint8)(__ccgo_up(data + 19)) = uint8(1)
	**(**uint8)(__ccgo_up(data + 20)) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FpageSize - (*TBtShared)(unsafe.Pointer(pBt)).FusableSize)
	**(**uint8)(__ccgo_up(data + 21)) = uint8(64)
	**(**uint8)(__ccgo_up(data + 22)) = uint8(32)
	**(**uint8)(__ccgo_up(data + 23)) = uint8(32)
	libc.Xmemset(tls, data+24, 0, uint32(libc.Int32FromInt32(100)-libc.Int32FromInt32(24)))
	_zeroPage(tls, pP1, libc.Int32FromInt32(PTF_INTKEY)|libc.Int32FromInt32(PTF_LEAF)|libc.Int32FromInt32(PTF_LEAFDATA))
	v1 = pBt + 24
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED))
	_sqlite3Put4byte(tls, data+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(4)*libc.Int32FromInt32(4)), uint32((*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum))
	_sqlite3Put4byte(tls, data+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(7)*libc.Int32FromInt32(4)), uint32((*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum))
	(*TBtShared)(unsafe.Pointer(pBt)).FnPage = uint32(1)
	**(**uint8)(__ccgo_up(data + 31)) = uint8(1)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Obtain a reference to an r-tree node.
//	*/
func _nodeAcquire(tls *libc.TLS, pRtree uintptr, iNode Ti64, pParent uintptr, ppNode uintptr) (r int32) {
	var pBlob, pNode, v1 uintptr
	var rc int32
	_, _, _, _ = pBlob, pNode, rc, v1
	rc = SQLITE_OK
	pNode = uintptr(0)
	/* Check if the requested node is already in the hash table. If so,
	 ** increase its reference count and return it.
	 */
	v1 = _nodeHashLookup(tls, pRtree, iNode)
	pNode = v1
	if v1 != uintptr(0) {
		if pParent != 0 && pParent != (*TRtreeNode)(unsafe.Pointer(pNode)).FpParent {
			return libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		}
		(*TRtreeNode)(unsafe.Pointer(pNode)).FnRef = (*TRtreeNode)(unsafe.Pointer(pNode)).FnRef + 1
		**(**uintptr)(__ccgo_up(ppNode)) = pNode
		return SQLITE_OK
	}
	if (*TRtree)(unsafe.Pointer(pRtree)).FpNodeBlob != 0 {
		pBlob = (*TRtree)(unsafe.Pointer(pRtree)).FpNodeBlob
		(*TRtree)(unsafe.Pointer(pRtree)).FpNodeBlob = uintptr(0)
		rc = Xsqlite3_blob_reopen(tls, pBlob, iNode)
		(*TRtree)(unsafe.Pointer(pRtree)).FpNodeBlob = pBlob
		if rc != 0 {
			_nodeBlobReset(tls, pRtree)
			if rc == int32(SQLITE_NOMEM) {
				return int32(SQLITE_NOMEM)
			}
		}
	}
	if (*TRtree)(unsafe.Pointer(pRtree)).FpNodeBlob == uintptr(0) {
		rc = Xsqlite3_blob_open(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName, __ccgo_ts+28645, iNode, 0, pRtree+80)
	}
	if rc != 0 {
		**(**uintptr)(__ccgo_up(ppNode)) = uintptr(0)
		/* If unable to open an sqlite3_blob on the desired row, that can only
		 ** be because the shadow tables hold erroneous data. */
		if rc == int32(SQLITE_ERROR) {
			rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		}
	} else {
		if iNode <= 0 {
			rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		} else {
			if (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize == Xsqlite3_blob_bytes(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpNodeBlob) {
				pNode = Xsqlite3_malloc64(tls, uint64(uint32(32)+uint32((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize)))
				if !(pNode != 0) {
					rc = int32(SQLITE_NOMEM)
				} else {
					(*TRtreeNode)(unsafe.Pointer(pNode)).FpParent = pParent
					(*TRtreeNode)(unsafe.Pointer(pNode)).FzData = pNode + 1*32
					(*TRtreeNode)(unsafe.Pointer(pNode)).FnRef = int32(1)
					(*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef = (*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef + 1
					(*TRtreeNode)(unsafe.Pointer(pNode)).FiNode = iNode
					(*TRtreeNode)(unsafe.Pointer(pNode)).FisDirty = 0
					(*TRtreeNode)(unsafe.Pointer(pNode)).FpNext = uintptr(0)
					rc = Xsqlite3_blob_read(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpNodeBlob, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData, (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize, 0)
				}
			}
		}
	}
	/* If the root node was just loaded, set pRtree->iDepth to the height
	 ** of the r-tree structure. A height of zero means all data is stored on
	 ** the root node. A height of one means the children of the root node
	 ** are the leaves, and so on. If the depth as specified on the root node
	 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
	 */
	if rc == SQLITE_OK && pNode != 0 && iNode == int64(1) {
		(*TRtree)(unsafe.Pointer(pRtree)).FiDepth = _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData)
		if (*TRtree)(unsafe.Pointer(pRtree)).FiDepth >= int32(RTREE_MAX_DEPTH) {
			rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		}
	}
	/* If no error has occurred so far, check if the "number of entries"
	 ** field on the node is too large. If so, set the return code to
	 ** SQLITE_CORRUPT_VTAB.
	 */
	if pNode != 0 && rc == SQLITE_OK {
		if _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2) > ((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize-int32(4))/int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell) {
			rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		}
	}
	if rc == SQLITE_OK {
		if pNode != uintptr(0) {
			_nodeReference(tls, pParent)
			_nodeHashInsert(tls, pRtree, pNode)
		} else {
			rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		}
		**(**uintptr)(__ccgo_up(ppNode)) = pNode
	} else {
		_nodeBlobReset(tls, pRtree)
		if pNode != 0 {
			(*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef = (*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef - 1
			Xsqlite3_free(tls, pNode)
		}
		**(**uintptr)(__ccgo_up(ppNode)) = uintptr(0)
	}
	return rc
}

// C documentation
//
//	/*
//	** Remove the cell with index iCell from node pNode.
//	*/
func _nodeDeleteCell(tls *libc.TLS, pRtree uintptr, pNode uintptr, iCell int32) {
	var nByte int32
	var pDst, pSrc uintptr
	_, _, _ = nByte, pDst, pSrc
	pDst = (*TRtreeNode)(unsafe.Pointer(pNode)).FzData + uintptr(int32(4)+int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)*iCell)
	pSrc = pDst + uintptr((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)
	nByte = (_readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2) - iCell - int32(1)) * int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)
	libc.Xmemmove(tls, pDst, pSrc, uint32(nByte))
	_writeInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2, _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2)-int32(1))
	(*TRtreeNode)(unsafe.Pointer(pNode)).FisDirty = int32(1)
}

// C documentation
//
//	/*
//	** Add node pNode to the node hash table.
//	*/
func _nodeHashInsert(tls *libc.TLS, pRtree uintptr, pNode uintptr) {
	var iHash int32
	_ = iHash
	iHash = int32(_nodeHash(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode))
	(*TRtreeNode)(unsafe.Pointer(pNode)).FpNext = **(**uintptr)(__ccgo_up(pRtree + 120 + uintptr(iHash)*4))
	**(**uintptr)(__ccgo_up(pRtree + 120 + uintptr(iHash)*4)) = pNode
}

// C documentation
//
//	/*
//	** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
//	** indicating that node has not yet been assigned a node number. It is
//	** assigned a node number when nodeWrite() is called to write the
//	** node contents out to the database.
//	*/
func _nodeNew(tls *libc.TLS, pRtree uintptr, pParent uintptr) (r uintptr) {
	var pNode uintptr
	_ = pNode
	pNode = Xsqlite3_malloc64(tls, uint64(uint32(32)+uint32((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize)))
	if pNode != 0 {
		libc.Xmemset(tls, pNode, 0, uint32(32)+uint32((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize))
		(*TRtreeNode)(unsafe.Pointer(pNode)).FzData = pNode + 1*32
		(*TRtreeNode)(unsafe.Pointer(pNode)).FnRef = int32(1)
		(*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef = (*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef + 1
		(*TRtreeNode)(unsafe.Pointer(pNode)).FpParent = pParent
		(*TRtreeNode)(unsafe.Pointer(pNode)).FisDirty = int32(1)
		_nodeReference(tls, pParent)
	}
	return pNode
}

// C documentation
//
//	/*
//	** Clear the content of node p (set all bytes to 0x00).
//	*/
func _nodeZero(tls *libc.TLS, pRtree uintptr, p uintptr) {
	libc.Xmemset(tls, (*TRtreeNode)(unsafe.Pointer(p)).FzData+2, 0, uint32((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize-int32(2)))
	(*TRtreeNode)(unsafe.Pointer(p)).FisDirty = int32(1)
}

func _nth_valueStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) {
	var fVal float64
	var iVal Ti64
	var p uintptr
	_, _, _ = fVal, iVal, p
	p = Xsqlite3_aggregate_context(tls, pCtx, int32(16))
	if p != 0 {
		switch Xsqlite3_value_numeric_type(tls, **(**uintptr)(__ccgo_up(apArg + 1*4))) {
		case int32(SQLITE_INTEGER):
			iVal = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apArg + 1*4)))
		case int32(SQLITE_FLOAT):
			fVal = Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(apArg + 1*4)))
			if float64(int64(fVal)) != fVal {
				goto error_out
			}
			iVal = int64(fVal)
		default:
			goto error_out
		}
		if iVal <= 0 {
			goto error_out
		}
		(*TNthValueCtx)(unsafe.Pointer(p)).FnStep = (*TNthValueCtx)(unsafe.Pointer(p)).FnStep + 1
		if iVal == (*TNthValueCtx)(unsafe.Pointer(p)).FnStep {
			(*TNthValueCtx)(unsafe.Pointer(p)).FpValue = Xsqlite3_value_dup(tls, **(**uintptr)(__ccgo_up(apArg)))
			if !((*TNthValueCtx)(unsafe.Pointer(p)).FpValue != 0) {
				Xsqlite3_result_error_nomem(tls, pCtx)
			}
		}
	}
	_ = nArg
	_ = apArg
	return
	goto error_out
error_out:
	;
	Xsqlite3_result_error(tls, pCtx, __ccgo_ts+25409, -int32(1))
}

// C documentation
//
//	/*
//	** This routine does the work of opening a database on behalf of
//	** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
//	** is UTF-8 encoded.
//	*/
func _openDatabase(tls *libc.TLS, zFilename uintptr, ppDb uintptr, _flags uint32, zVfs uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	*(*uint32)(unsafe.Pointer(bp)) = _flags
	var db, v2 uintptr
	var i, isThreadsafe, rc int32
	var v1 uint32
	var _ /* zErrMsg at bp+8 */ uintptr
	var _ /* zOpen at bp+4 */ uintptr
	_, _, _, _, _, _ = db, i, isThreadsafe, rc, v1, v2 /* True for threadsafe connections */
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)      /* Filename argument to pass to BtreeOpen() */
	**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)      /* Loop counter */
	**(**uintptr)(__ccgo_up(ppDb)) = uintptr(0)
	rc = Xsqlite3_initialize(tls)
	if rc != 0 {
		return rc
	}
	if int32(_sqlite3Config.FbCoreMutex) == 0 {
		isThreadsafe = 0
	} else {
		if **(**uint32)(__ccgo_up(bp))&uint32(SQLITE_OPEN_NOMUTEX) != 0 {
			isThreadsafe = 0
		} else {
			if **(**uint32)(__ccgo_up(bp))&uint32(SQLITE_OPEN_FULLMUTEX) != 0 {
				isThreadsafe = int32(1)
			} else {
				isThreadsafe = int32(_sqlite3Config.FbFullMutex)
			}
		}
	}
	if **(**uint32)(__ccgo_up(bp))&uint32(SQLITE_OPEN_PRIVATECACHE) != 0 {
		**(**uint32)(__ccgo_up(bp)) = **(**uint32)(__ccgo_up(bp)) & uint32(^libc.Int32FromInt32(SQLITE_OPEN_SHAREDCACHE))
	} else {
		if _sqlite3Config.FsharedCacheEnabled != 0 {
			**(**uint32)(__ccgo_up(bp)) = **(**uint32)(__ccgo_up(bp)) | uint32(SQLITE_OPEN_SHAREDCACHE)
		}
	}
	/* Remove harmful bits from the flags parameter
	 **
	 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
	 ** dealt with in the previous code block.  Besides these, the only
	 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
	 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
	 ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_EXRESCODE, and some reserved
	 ** bits.  Silently mask off all other flags.
	 */
	**(**uint32)(__ccgo_up(bp)) = **(**uint32)(__ccgo_up(bp)) & uint32(^(libc.Int32FromInt32(SQLITE_OPEN_DELETEONCLOSE) | libc.Int32FromInt32(SQLITE_OPEN_EXCLUSIVE) | libc.Int32FromInt32(SQLITE_OPEN_MAIN_DB) | libc.Int32FromInt32(SQLITE_OPEN_TEMP_DB) | libc.Int32FromInt32(SQLITE_OPEN_TRANSIENT_DB) | libc.Int32FromInt32(SQLITE_OPEN_MAIN_JOURNAL) | libc.Int32FromInt32(SQLITE_OPEN_TEMP_JOURNAL) | libc.Int32FromInt32(SQLITE_OPEN_SUBJOURNAL) | libc.Int32FromInt32(SQLITE_OPEN_SUPER_JOURNAL) | libc.Int32FromInt32(SQLITE_OPEN_NOMUTEX) | libc.Int32FromInt32(SQLITE_OPEN_FULLMUTEX) | libc.Int32FromInt32(SQLITE_OPEN_WAL)))
	/* Allocate the sqlite data structure */
	db = _sqlite3MallocZero(tls, uint64(584))
	if db == uintptr(0) {
		goto opendb_out
	}
	if isThreadsafe != 0 {
		(*Tsqlite3)(unsafe.Pointer(db)).Fmutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_RECURSIVE))
		if (*Tsqlite3)(unsafe.Pointer(db)).Fmutex == uintptr(0) {
			Xsqlite3_free(tls, db)
			db = uintptr(0)
			goto opendb_out
		}
		if isThreadsafe == 0 {
		}
	}
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	if **(**uint32)(__ccgo_up(bp))&uint32(SQLITE_OPEN_EXRESCODE) != uint32(0) {
		v1 = uint32(0xffffffff)
	} else {
		v1 = uint32(0xff)
	}
	(*Tsqlite3)(unsafe.Pointer(db)).FerrMask = int32(v1)
	(*Tsqlite3)(unsafe.Pointer(db)).FnDb = int32(2)
	(*Tsqlite3)(unsafe.Pointer(db)).FeOpenState = uint8(SQLITE_STATE_BUSY)
	(*Tsqlite3)(unsafe.Pointer(db)).FaDb = db + 476
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = uint32(1)
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0)
	(*Tsqlite3)(unsafe.Pointer(db)).FnFpDigit = uint8(17)
	libc.Xmemcpy(tls, db+120, uintptr(unsafe.Pointer(&_aHardLimit)), uint32(52))
	**(**int32)(__ccgo_up(db + 120 + 11*4)) = SQLITE_DEFAULT_WORKER_THREADS
	(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1)
	(*Tsqlite3)(unsafe.Pointer(db)).FnextAutovac = int8(-int32(1))
	(*Tsqlite3)(unsafe.Pointer(db)).FszMmap = _sqlite3Config.FszMmap
	(*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize = 0
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit = uintptr(unsafe.Pointer(&_sqlite3StdType)) /* Any array of string ptrs will do */
	**(**Tu64)(__ccgo_up(db + 32)) |= uint64(uint32(libc.Int32FromInt32(SQLITE_ShortColNames)|libc.Int32FromInt32(SQLITE_EnableTrigger))|libc.Uint32FromUint32(SQLITE_EnableView)|libc.Uint32FromInt32(SQLITE_CacheSpill)) | uint64(libc.Int32FromInt32(0x00010))<<libc.Int32FromInt32(32) | uint64(libc.Int32FromInt32(0x00020))<<libc.Int32FromInt32(32) | uint64(libc.Int32FromInt32(0x00040))<<libc.Int32FromInt32(32) | libc.Uint64FromInt32(SQLITE_TrustedSchema) | libc.Uint64FromInt32(SQLITE_DqsDML) | libc.Uint64FromInt32(SQLITE_DqsDDL) | libc.Uint64FromInt32(SQLITE_AutoIndex)
	_sqlite3HashInit(tls, db+448)
	_sqlite3HashInit(tls, db+404)
	/* Add the default collation sequence BINARY. BINARY works for both UTF-8
	 ** and UTF-16, so add a version for each to avoid any unnecessary
	 ** conversions. The only error that can occur here is a malloc() failure.
	 **
	 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
	 ** functions:
	 */
	_createCollation(tls, db, uintptr(unsafe.Pointer(&_sqlite3StrBINARY)), uint8(SQLITE_UTF8), uintptr(0), __ccgo_fp(_binCollFunc), uintptr(0))
	_createCollation(tls, db, uintptr(unsafe.Pointer(&_sqlite3StrBINARY)), uint8(SQLITE_UTF16BE), uintptr(0), __ccgo_fp(_binCollFunc), uintptr(0))
	_createCollation(tls, db, uintptr(unsafe.Pointer(&_sqlite3StrBINARY)), uint8(SQLITE_UTF16LE), uintptr(0), __ccgo_fp(_binCollFunc), uintptr(0))
	_createCollation(tls, db, __ccgo_ts+25175, uint8(SQLITE_UTF8), uintptr(0), __ccgo_fp(_nocaseCollatingFunc), uintptr(0))
	_createCollation(tls, db, __ccgo_ts+27499, uint8(SQLITE_UTF8), uintptr(0), __ccgo_fp(_rtrimCollFunc), uintptr(0))
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto opendb_out
	}
	/* Parse the filename/URI argument
	 **
	 ** Only allow sensible combinations of bits in the flags argument.
	 ** Throw an error if any non-sense combination is used.  If we
	 ** do not block illegal combinations here, it could trigger
	 ** assert() statements in deeper layers.  Sensible combinations
	 ** are:
	 **
	 **  1:  SQLITE_OPEN_READONLY
	 **  2:  SQLITE_OPEN_READWRITE
	 **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
	 */
	(*Tsqlite3)(unsafe.Pointer(db)).FopenFlags = **(**uint32)(__ccgo_up(bp))
	/* READONLY */
	/* READWRITE */
	/* READWRITE | CREATE */
	if int32(1)<<(**(**uint32)(__ccgo_up(bp))&uint32(7))&int32(0x46) == 0 {
		rc = _sqlite3MisuseError(tls, int32(190956)) /* IMP: R-18321-05872 */
	} else {
		if zFilename == uintptr(0) {
			zFilename = __ccgo_ts + 5556
		}
		rc = _sqlite3ParseUri(tls, zVfs, zFilename, bp, db, bp+4, bp+8)
	}
	if rc != SQLITE_OK {
		if rc == int32(SQLITE_NOMEM) {
			_sqlite3OomFault(tls, db)
		}
		if **(**uintptr)(__ccgo_up(bp + 8)) != 0 {
			v2 = __ccgo_ts + 4729
		} else {
			v2 = uintptr(0)
		}
		_sqlite3ErrorWithMsg(tls, db, rc, v2, libc.VaList(bp+24, **(**uintptr)(__ccgo_up(bp + 8))))
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 8)))
		goto opendb_out
	}
	/* Open the backend database driver */
	rc = _sqlite3BtreeOpen(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, **(**uintptr)(__ccgo_up(bp + 4)), db, (*Tsqlite3)(unsafe.Pointer(db)).FaDb+4, 0, int32(**(**uint32)(__ccgo_up(bp))|uint32(SQLITE_OPEN_MAIN_DB)))
	if rc != SQLITE_OK {
		if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<<libc.Int32FromInt32(8) {
			rc = int32(SQLITE_NOMEM)
		}
		_sqlite3Error(tls, db, rc)
		goto opendb_out
	}
	_sqlite3BtreeEnter(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpBt)
	(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema = _sqlite3SchemaGet(tls, db, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpBt)
	if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
		_sqlite3SetTextEncoding(tls, db, (*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).Fenc)
	}
	_sqlite3BtreeLeave(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpBt)
	(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema = _sqlite3SchemaGet(tls, db, uintptr(0))
	/* The default safety_level for the main database is FULL; for the temp
	 ** database it is OFF. This matches the pager layer defaults.
	 */
	(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FzDbSName = __ccgo_ts + 8033
	(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).Fsafety_level = uint8(libc.Int32FromInt32(SQLITE_DEFAULT_SYNCHRONOUS) + libc.Int32FromInt32(1))
	(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FzDbSName = __ccgo_ts + 26494
	(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).Fsafety_level = uint8(PAGER_SYNCHRONOUS_OFF)
	(*Tsqlite3)(unsafe.Pointer(db)).FeOpenState = uint8(SQLITE_STATE_OPEN)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto opendb_out
	}
	/* Register all built-in functions, but do not attempt to read the
	 ** database schema yet. This is delayed until the first time the database
	 ** is accessed.
	 */
	_sqlite3Error(tls, db, SQLITE_OK)
	_sqlite3RegisterPerConnectionBuiltinFunctions(tls, db)
	rc = Xsqlite3_errcode(tls, db)
	/* Load compiled-in extensions */
	i = 0
	for {
		if !(rc == SQLITE_OK && i < int32(libc.Uint32FromInt64(20)/libc.Uint32FromInt64(4))) {
			break
		}
		rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3BuiltinExtensions[i]})))(tls, db)
		goto _3
	_3:
		;
		i = i + 1
	}
	/* Load automatic extensions - extensions that have been registered
	 ** using the sqlite3_automatic_extension() API.
	 */
	if rc == SQLITE_OK {
		_sqlite3AutoLoadExtensions(tls, db)
		rc = Xsqlite3_errcode(tls, db)
		if rc != SQLITE_OK {
			goto opendb_out
		}
	}
	/* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
	 ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
	 ** mode.  Doing nothing at all also makes NORMAL the default.
	 */
	if rc != 0 {
		_sqlite3Error(tls, db, rc)
	}
	/* Enable the lookaside-malloc subsystem */
	_setupLookaside(tls, db, uintptr(0), _sqlite3Config.FszLookaside, _sqlite3Config.FnLookaside)
	Xsqlite3_wal_autocheckpoint(tls, db, int32(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT))
	goto opendb_out
opendb_out:
	;
	if db != 0 {
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	}
	rc = Xsqlite3_errcode(tls, db)
	if rc&int32(0xff) == int32(SQLITE_NOMEM) {
		Xsqlite3_close(tls, db)
		db = uintptr(0)
	} else {
		if rc != SQLITE_OK {
			(*Tsqlite3)(unsafe.Pointer(db)).FeOpenState = uint8(SQLITE_STATE_SICK)
		}
	}
	**(**uintptr)(__ccgo_up(ppDb)) = db
	Xsqlite3_free_filename(tls, **(**uintptr)(__ccgo_up(bp + 4)))
	return rc
}

func _openRbuHandle(tls *libc.TLS, zTarget uintptr, zRbu uintptr, zState uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, p, pCsr, pFd, pState, v2 uintptr
	var frc int32
	var nByte, nRbu, nTarget Tsize_t
	var v1 uint32
	var _ /* bRetry at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _ = db, frc, nByte, nRbu, nTarget, p, pCsr, pFd, pState, v1, v2
	if zTarget != 0 {
		v1 = libc.Xstrlen(tls, zTarget)
	} else {
		v1 = uint32(0)
	}
	nTarget = v1
	nRbu = libc.Xstrlen(tls, zRbu)
	nByte = uint32(264) + nTarget + uint32(1) + nRbu + uint32(1)
	p = Xsqlite3_malloc64(tls, uint64(nByte))
	if p != 0 {
		pState = uintptr(0)
		/* Create the custom VFS. */
		libc.Xmemset(tls, p, 0, uint32(264))
		Xsqlite3rbu_rename_handler(tls, p, uintptr(0), uintptr(0))
		_rbuCreateVfs(tls, p)
		/* Open the target, RBU and state databases */
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			pCsr = p + 1*264
			**(**int32)(__ccgo_up(bp)) = 0
			if zTarget != 0 {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget = pCsr
				libc.Xmemcpy(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget, zTarget, nTarget+uint32(1))
				pCsr = pCsr + uintptr(nTarget+uint32(1))
			}
			(*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu = pCsr
			libc.Xmemcpy(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, zRbu, nRbu+uint32(1))
			pCsr = pCsr + uintptr(nRbu+uint32(1))
			if zState != 0 {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FzState = _rbuMPrintf(tls, p, __ccgo_ts+4729, libc.VaList(bp+16, zState))
			}
			/* If the first attempt to open the database file fails and the bRetry
			 ** flag it set, this means that the db was not opened because it seemed
			 ** to be a wal-mode db. But, this may have happened due to an earlier
			 ** RBU vacuum operation leaving an old wal file in the directory.
			 ** If this is the case, it will have been checkpointed and deleted
			 ** when the handle was closed and a second attempt to open the
			 ** database may succeed.  */
			_rbuOpenDatabase(tls, p, uintptr(0), bp)
			if **(**int32)(__ccgo_up(bp)) != 0 {
				_rbuOpenDatabase(tls, p, uintptr(0), uintptr(0))
			}
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			pState = _rbuLoadState(tls, p)
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				if (*TRbuState)(unsafe.Pointer(pState)).FeStage == 0 {
					_rbuDeleteOalFile(tls, p)
					_rbuInitPhaseOneSteps(tls, p)
					(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_OAL)
				} else {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = (*TRbuState)(unsafe.Pointer(pState)).FeStage
					(*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep = (*TRbuState)(unsafe.Pointer(pState)).FnPhaseOneStep
				}
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress = (*TRbuState)(unsafe.Pointer(pState)).FnProgress
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FiOalSz = (*TRbuState)(unsafe.Pointer(pState)).FiOalSz
			}
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd)).FpWalFd != 0 {
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR)
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+35795, 0)
			} else {
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_MOVE) {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_CKPT)
					(*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = 0
				}
			}
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && ((*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) || (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_MOVE)) && (*TRbuState)(unsafe.Pointer(pState)).FeStage != 0 {
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
				v2 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd
			} else {
				v2 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd
			}
			pFd = v2
			if (*Trbu_file)(unsafe.Pointer(pFd)).FiCookie != (*TRbuState)(unsafe.Pointer(pState)).FiCookie {
				/* At this point (pTargetFd->iCookie) contains the value of the
				 ** change-counter cookie (the thing that gets incremented when a
				 ** transaction is committed in rollback mode) currently stored on
				 ** page 1 of the database file. */
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_BUSY)
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
					v2 = __ccgo_ts + 35827
				} else {
					v2 = __ccgo_ts + 35834
				}
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+35841, libc.VaList(bp+16, v2))
			}
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) {
				db = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+17325, uintptr(0), uintptr(0), p+36)
				/* Point the object iterator at the first object */
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _rbuObjIterFirst(tls, p, p+56)
				}
				/* If the RBU database contains no data_xxx tables, declare the RBU
				 ** update finished.  */
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FzTbl == uintptr(0) {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_DONE)
					(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_DONE)
				} else {
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuState)(unsafe.Pointer(pState)).FeStage == 0 && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
						_rbuCopyPragma(tls, p, __ccgo_ts+19894)
						_rbuCopyPragma(tls, p, __ccgo_ts+19285)
					}
					/* Open transactions both databases. The *-oal file is opened or
					 ** created at this point. */
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
						(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, db, __ccgo_ts+35873, uintptr(0), uintptr(0), p+36)
					}
					/* Check if the main database is a zipvfs db. If it is, set the upper
					 ** level pager to use "journal_mode=off". This prevents it from
					 ** generating a large journal using a temp file.  */
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
						frc = Xsqlite3_file_control(tls, db, __ccgo_ts+8033, int32(SQLITE_FCNTL_ZIPVFS), uintptr(0))
						if frc == SQLITE_OK {
							(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, db, __ccgo_ts+35889, uintptr(0), uintptr(0), p+36)
						}
					}
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
						_rbuSetupOal(tls, p, pState)
					}
				}
			} else {
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_MOVE) {
					/* no-op */
				} else {
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_CKPT) {
						if !((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == libc.UintptrFromInt32(0)) && _rbuExclusiveCheckpoint(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain) != 0 {
							/* If the rbu_exclusive_checkpoint=1 URI parameter was specified
							 ** and an incremental checkpoint is being resumed, attempt an
							 ** exclusive lock on the db file. If this fails, so be it.  */
							(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_DONE)
							_rbuLockDatabase(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain)
							(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = int32(RBU_STAGE_CKPT)
						}
						_rbuSetupCheckpoint(tls, p, pState)
					} else {
						if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_DONE) {
							(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_DONE)
						} else {
							(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_CORRUPT)
						}
					}
				}
			}
		}
		_rbuFreeState(tls, pState)
	}
	return p
}

// C documentation
//
//	/*
//	** This routine generates code that opens the sqlite_statN tables.
//	** The sqlite_stat1 table is always relevant.  sqlite_stat2 is now
//	** obsolete.  sqlite_stat3 and sqlite_stat4 are only opened when
//	** appropriate compile-time options are provided.
//	**
//	** If the sqlite_statN tables do not previously exist, it is created.
//	**
//	** Argument zWhere may be a pointer to a buffer containing a table name,
//	** or it may be a NULL pointer. If it is not NULL, then all entries in
//	** the sqlite_statN tables associated with the named table are deleted.
//	** If zWhere==0, then code is generated to delete all stat table entries.
//	*/
func _openStatTable(tls *libc.TLS, pParse uintptr, iDb int32, iStatCur int32, zWhere uintptr, zWhereType uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var aCreateTbl [3]Tu8
	var aRoot [3]Tu32
	var db, pDb, pStat, v, zTab, v3 uintptr
	var i, nToOpen, v1 int32
	_, _, _, _, _, _, _, _, _, _, _ = aCreateTbl, aRoot, db, i, nToOpen, pDb, pStat, v, zTab, v1, v3
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	v = _sqlite3GetVdbe(tls, pParse)
	if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) {
		v1 = int32(2)
	} else {
		v1 = int32(1)
	}
	nToOpen = v1
	if v == uintptr(0) {
		return
	}
	pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16
	/* Create new statistic tables if they do not exist, or clear them
	 ** if they do already exist.
	 */
	i = 0
	for {
		if !(i < int32(libc.Uint32FromInt64(24)/libc.Uint32FromInt64(8))) {
			break
		}
		zTab = _aTable[i].FzName
		aCreateTbl[i] = uint8(0)
		v3 = _sqlite3FindTable(tls, db, zTab, (*TDb)(unsafe.Pointer(pDb)).FzDbSName)
		pStat = v3
		if v3 == uintptr(0) {
			if i < nToOpen {
				/* The sqlite_statN table does not exist. Create it. Note that a
				 ** side-effect of the CREATE TABLE statement is to leave the rootpage
				 ** of the new table in register pParse->regRoot. This is important
				 ** because the OpenWrite opcode below will be needing it. */
				_sqlite3NestedParse(tls, pParse, __ccgo_ts+14130, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, zTab, _aTable[i].FzCols))
				aRoot[i] = uint32((*(*struct {
					FaddrCrTab      int32
					FregRowid       int32
					FregRoot        int32
					FconstraintName TToken
				})(unsafe.Pointer(pParse + 188))).FregRoot)
				aCreateTbl[i] = uint8(OPFLAG_P2ISREG)
			}
		} else {
			/* The table already exists. If zWhere is not NULL, delete all entries
			 ** associated with the table zWhere. If zWhere is NULL, delete the
			 ** entire contents of the table. */
			aRoot[i] = (*TTable)(unsafe.Pointer(pStat)).Ftnum
			_sqlite3TableLock(tls, pParse, iDb, aRoot[i], uint8(1), zTab)
			if zWhere != 0 {
				_sqlite3NestedParse(tls, pParse, __ccgo_ts+14153, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, zTab, zWhereType, zWhere))
			} else {
				if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 {
					_sqlite3NestedParse(tls, pParse, __ccgo_ts+14183, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, zTab))
				} else {
					/* The sqlite_stat[134] table already exists.  Delete all rows. */
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Clear), int32(aRoot[i]), iDb)
				}
			}
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	/* Open the sqlite_stat[134] tables for writing. */
	i = 0
	for {
		if !(i < nToOpen) {
			break
		}
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_OpenWrite), iStatCur+i, int32(aRoot[i]), iDb, int32(3))
		_sqlite3VdbeChangeP5(tls, v, uint16(aCreateTbl[i]))
		goto _4
	_4:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** An index on expressions is being used in the inner loop of an
//	** aggregate query with a GROUP BY clause.  This routine attempts
//	** to adjust the AggInfo object to take advantage of index and to
//	** perhaps use the index as a covering index.
//	**
//	*/
func _optimizeAggregateUseOfIndexedExpr(tls *libc.TLS, pParse uintptr, pSelect uintptr, pAggInfo uintptr, pNC uintptr) {
	var j, k, mx int32
	_, _, _ = j, k, mx
	(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator
	if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnSortingColumn > uint32(0) {
		mx = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpGroupBy)).FnExpr - int32(1)
		j = 0
		for {
			if !(j < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn) {
				break
			}
			k = (**(**TAggInfo_col)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(j)*20))).FiSorterColumn
			if k > mx {
				mx = k
			}
			goto _1
		_1:
			;
			j = j + 1
		}
		(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnSortingColumn = uint32(mx + int32(1))
	}
	_analyzeAggFuncArgs(tls, pAggInfo, pNC)
	_ = pSelect
	_ = pParse
}

// C documentation
//
//	/*
//	** The following routine implements the rough equivalent of localtime_r()
//	** using whatever operating-system specific localtime facility that
//	** is available.  This routine returns 0 on success and
//	** non-zero on any kind of error.
//	**
//	** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
//	** routine will always fail.  If bLocaltimeFault is nonzero and
//	** sqlite3GlobalConfig.xAltLocaltime is not NULL, then xAltLocaltime() is
//	** invoked in place of the OS-defined localtime() function.
//	**
//	** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
//	** library function localtime_r() is used to assist in the calculation of
//	** local time.
//	*/
func _osLocaltime(tls *libc.TLS, t uintptr, pTm uintptr) (r int32) {
	var mutex, pX, v1 uintptr
	var rc int32
	_, _, _, _ = mutex, pX, rc, v1
	mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN))
	Xsqlite3_mutex_enter(tls, mutex)
	v1 = libc.X_localtime32(tls, t)
	goto _2
_2:
	pX = v1
	if _sqlite3Config.FbLocaltimeFault != 0 {
		if _sqlite3Config.FxAltLocaltime != uintptr(0) && 0 == (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.FxAltLocaltime})))(tls, t, pTm) {
			pX = pTm
		} else {
			pX = uintptr(0)
		}
	}
	if pX != 0 {
		**(**Ttm)(__ccgo_up(pTm)) = **(**Ttm)(__ccgo_up(pX))
	}
	Xsqlite3_mutex_leave(tls, mutex)
	rc = libc.BoolInt32(pX == uintptr(0))
	return rc
}

func _out2Prerelease(tls *libc.TLS, p uintptr, pOp uintptr) (r uintptr) {
	var pOut uintptr
	_ = pOut
	pOut = (*TVdbe)(unsafe.Pointer(p)).FaMem + uintptr((*TVdbeOp)(unsafe.Pointer(pOp)).Fp2)*40
	if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { /*OPTIMIZATION-IF-FALSE*/
		return _out2PrereleaseWithClear(tls, pOut)
	} else {
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Int)
		return pOut
	}
	return r
}

// C documentation
//
//	/*
//	** Search the free-list on page pPg for space to store a cell nByte bytes in
//	** size. If one can be found, return a pointer to the space and remove it
//	** from the free-list.
//	**
//	** If no suitable space can be found on the free-list, return NULL.
//	**
//	** This function may detect corruption within pPg.  If corruption is
//	** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
//	**
//	** Slots on the free list that are between 1 and 3 bytes larger than nByte
//	** will be ignored if adding the extra space to the fragmentation count
//	** causes the fragmentation count to exceed 60.
//	*/
func _pageFindSlot(tls *libc.TLS, pPg uintptr, nByte int32, pRc uintptr) (r uintptr) {
	var aData, pTmp, v2 uintptr
	var hdr, iAddr, maxPC, pc, size, x, v1 int32
	_, _, _, _, _, _, _, _, _, _ = aData, hdr, iAddr, maxPC, pTmp, pc, size, x, v1, v2
	hdr = int32((*TMemPage)(unsafe.Pointer(pPg)).FhdrOffset)                                                       /* Offset to page header */
	aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData                                                                /* Page data */
	iAddr = hdr + int32(1)                                                                                         /* Address of ptr to pc */
	pTmp = aData + uintptr(iAddr)                                                                                  /* Temporary ptr into aData[] */
	pc = int32(**(**Tu8)(__ccgo_up(pTmp)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(pTmp + 1)))                       /* Excess size of the slot */
	maxPC = int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPg)).FpBt)).FusableSize - uint32(nByte)) /* Size of the free slot */
	for pc <= maxPC {
		/* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
		 ** freeblock form a big-endian integer which is the size of the freeblock
		 ** in bytes, including the 4-byte header. */
		pTmp = aData + uintptr(pc+int32(2))
		size = int32(**(**Tu8)(__ccgo_up(pTmp)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(pTmp + 1)))
		v1 = size - nByte
		x = v1
		if v1 >= 0 {
			if x < int32(4) {
				/* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
				 ** number of bytes in fragments may not exceed 60. */
				if int32(**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(7))))) > int32(57) {
					return uintptr(0)
				}
				/* Remove the slot from the free-list. Update the number of
				 ** fragmented bytes within the page. */
				libc.Xmemcpy(tls, aData+uintptr(iAddr), aData+uintptr(pc), uint32(2))
				v2 = aData + uintptr(hdr+int32(7))
				*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) + int32(uint8(x)))
				return aData + uintptr(pc)
			} else {
				if x+pc > maxPC {
					/* This slot extends off the end of the usable part of the page */
					**(**int32)(__ccgo_up(pRc)) = _sqlite3CorruptError(tls, int32(75005))
					return uintptr(0)
				} else {
					/* The slot remains on the free-list. Reduce its size to account
					 ** for the portion used by the new allocation. */
					**(**Tu8)(__ccgo_up(aData + uintptr(pc+int32(2)))) = uint8(x >> libc.Int32FromInt32(8))
					**(**Tu8)(__ccgo_up(aData + uintptr(pc+int32(2)) + 1)) = uint8(x)
				}
			}
			return aData + uintptr(pc+x)
		}
		iAddr = pc
		pTmp = aData + uintptr(pc)
		pc = int32(**(**Tu8)(__ccgo_up(pTmp)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(pTmp + 1)))
		if pc <= iAddr {
			if pc != 0 {
				/* The next slot in the chain comes before the current slot */
				**(**int32)(__ccgo_up(pRc)) = _sqlite3CorruptError(tls, int32(75020))
			}
			return uintptr(0)
		}
	}
	if pc > maxPC+nByte-int32(4) {
		/* The free slot chain extends off the end of the page */
		**(**int32)(__ccgo_up(pRc)) = _sqlite3CorruptError(tls, int32(75027))
	}
	return uintptr(0)
}

// C documentation
//
//	/*
//	** The pCArray object contains pointers to b-tree cells and their sizes.
//	**
//	** This function adds the space associated with each cell in the array
//	** that is currently stored within the body of pPg to the pPg free-list.
//	** The cell-pointers and other fields of the page are not updated.
//	**
//	** This function returns the total number of cells added to the free-list.
//	*/
func _pageFreeArray(tls *libc.TLS, pPg uintptr, iFirst int32, nCell int32, pCArray uintptr) (r int32) {
	var aAfter, aOfst [10]int32
	var aData, pCell, pEnd, pStart uintptr
	var i, iAfter, iEnd, iOfst, j, nFree, nRet, sz int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = aAfter, aData, aOfst, i, iAfter, iEnd, iOfst, j, nFree, nRet, pCell, pEnd, pStart, sz
	aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData
	pEnd = aData + uintptr((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPg)).FpBt)).FusableSize)
	pStart = aData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPg)).FhdrOffset)+int32(8)+int32((*TMemPage)(unsafe.Pointer(pPg)).FchildPtrSize))
	nRet = 0
	iEnd = iFirst + nCell
	nFree = 0
	i = iFirst
	for {
		if !(i < iEnd) {
			break
		}
		pCell = **(**uintptr)(__ccgo_up((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4))
		if uint32(pCell) >= uint32(pStart) && uint32(pCell) < uint32(pEnd) {
			/* No need to use cachedCellSize() here.  The sizes of all cells that
			 ** are to be freed have already been computing while deciding which
			 ** cells need freeing */
			sz = int32(**(**Tu16)(__ccgo_up((*TCellArray)(unsafe.Pointer(pCArray)).FszCell + uintptr(i)*2)))
			iOfst = int32(uint16(int32(pCell) - int32(aData)))
			iAfter = iOfst + sz
			j = 0
			for {
				if !(j < nFree) {
					break
				}
				if aOfst[j] == iAfter {
					aOfst[j] = iOfst
					break
				} else {
					if aAfter[j] == iOfst {
						aAfter[j] = iAfter
						break
					}
				}
				goto _2
			_2:
				;
				j = j + 1
			}
			if j >= nFree {
				if nFree >= int32(libc.Uint32FromInt64(40)/libc.Uint32FromInt64(4)) {
					j = 0
					for {
						if !(j < nFree) {
							break
						}
						_freeSpace(tls, pPg, aOfst[j], aAfter[j]-aOfst[j])
						goto _3
					_3:
						;
						j = j + 1
					}
					nFree = 0
				}
				aOfst[nFree] = iOfst
				aAfter[nFree] = iAfter
				if aData+uintptr(iAfter) > pEnd {
					return 0
				}
				nFree = nFree + 1
			}
			nRet = nRet + 1
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	j = 0
	for {
		if !(j < nFree) {
			break
		}
		_freeSpace(tls, pPg, aOfst[j], aAfter[j]-aOfst[j])
		goto _4
	_4:
		;
		j = j + 1
	}
	return nRet
}

// C documentation
//
//	/*
//	** The pCArray objects contains pointers to b-tree cells and the cell sizes.
//	** This function attempts to add the cells stored in the array to page pPg.
//	** If it cannot (because the page needs to be defragmented before the cells
//	** will fit), non-zero is returned. Otherwise, if the cells are added
//	** successfully, zero is returned.
//	**
//	** Argument pCellptr points to the first entry in the cell-pointer array
//	** (part of page pPg) to populate. After cell apCell[0] is written to the
//	** page body, a 16-bit offset is written to pCellptr. And so on, for each
//	** cell in the array. It is the responsibility of the caller to ensure
//	** that it is safe to overwrite this part of the cell-pointer array.
//	**
//	** When this function is called, *ppData points to the start of the
//	** content area on page pPg. If the size of the content area is extended,
//	** *ppData is updated to point to the new start of the content area
//	** before returning.
//	**
//	** Finally, argument pBegin points to the byte immediately following the
//	** end of the space required by this page for the cell-pointer area (for
//	** all cells - not just those inserted by the current call). If the content
//	** area must be extended to before this point in order to accommodate all
//	** cells in apCell[], then the cells do not fit and non-zero is returned.
//	*/
func _pageInsertArray(tls *libc.TLS, pPg uintptr, pBegin uintptr, ppData uintptr, pCellptr uintptr, iFirst int32, nCell int32, pCArray uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aData, pData, pEnd, pSlot, v2 uintptr
	var i, iEnd, k, sz int32
	var v3 bool
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _ = aData, i, iEnd, k, pData, pEnd, pSlot, sz, v2, v3
	i = iFirst                                      /* Loop counter - cell index to insert */
	aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData /* Complete page */
	pData = **(**uintptr)(__ccgo_up(ppData))        /* Content area.  A subset of aData[] */
	iEnd = iFirst + nCell                           /* Maximum extent of cell data */
	/* Never called on page 1 */
	if iEnd <= iFirst {
		return 0
	}
	k = 0
	for {
		if !(**(**int32)(__ccgo_up(pCArray + 40 + uintptr(k)*4)) <= i) {
			break
		}
		goto _1
	_1:
		;
		k = k + 1
	}
	pEnd = **(**uintptr)(__ccgo_up(pCArray + 16 + uintptr(k)*4))
	for int32(1) != 0 {
		sz = int32(**(**Tu16)(__ccgo_up((*TCellArray)(unsafe.Pointer(pCArray)).FszCell + uintptr(i)*2)))
		if v3 = int32(**(**Tu8)(__ccgo_up(aData + 1))) == 0 && int32(**(**Tu8)(__ccgo_up(aData + 2))) == 0; !v3 {
			v2 = _pageFindSlot(tls, pPg, sz, bp)
			pSlot = v2
		}
		if v3 || v2 == uintptr(0) {
			if int32(pData)-int32(pBegin) < sz {
				return int32(1)
			}
			pData = pData - uintptr(sz)
			pSlot = pData
		}
		/* pSlot and pCArray->apCell[i] will never overlap on a well-formed
		 ** database.  But they might for a corrupt database.  Hence use memmove()
		 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
		if uint32(**(**uintptr)(__ccgo_up((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4))+uintptr(sz)) > uint32(pEnd) && uint32(**(**uintptr)(__ccgo_up((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4))) < uint32(pEnd) {
			_sqlite3CorruptError(tls, int32(81003))
			return int32(1)
		}
		libc.Xmemmove(tls, pSlot, **(**uintptr)(__ccgo_up((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4)), uint32(sz))
		**(**Tu8)(__ccgo_up(pCellptr)) = uint8((int32(pSlot) - int32(aData)) >> libc.Int32FromInt32(8))
		**(**Tu8)(__ccgo_up(pCellptr + 1)) = uint8(int32(pSlot) - int32(aData))
		pCellptr = pCellptr + uintptr(2)
		i = i + 1
		if i >= iEnd {
			break
		}
		if **(**int32)(__ccgo_up(pCArray + 40 + uintptr(k)*4)) <= i {
			k = k + 1
			pEnd = **(**uintptr)(__ccgo_up(pCArray + 16 + uintptr(k)*4))
		}
	}
	**(**uintptr)(__ccgo_up(ppData)) = pData
	return 0
}

// C documentation
//
//	/*
//	** Obtain a reference to a memory mapped page object for page number pgno.
//	** The new object will use the pointer pData, obtained from xFetch().
//	** If successful, set *ppPage to point to the new page reference
//	** and return SQLITE_OK. Otherwise, return an SQLite error code and set
//	** *ppPage to zero.
//	**
//	** Page references obtained by calling this function should be released
//	** by calling pagerReleaseMapPage().
//	*/
func _pagerAcquireMapPage(tls *libc.TLS, pPager uintptr, pgno TPgno, pData uintptr, ppPage uintptr) (r int32) {
	var p, v1 uintptr
	_, _ = p, v1 /* Memory mapped page to return */
	if (*TPager)(unsafe.Pointer(pPager)).FpMmapFreelist != 0 {
		v1 = (*TPager)(unsafe.Pointer(pPager)).FpMmapFreelist
		p = v1
		**(**uintptr)(__ccgo_up(ppPage)) = v1
		(*TPager)(unsafe.Pointer(pPager)).FpMmapFreelist = (*TPgHdr)(unsafe.Pointer(p)).FpDirty
		(*TPgHdr)(unsafe.Pointer(p)).FpDirty = uintptr(0)
		libc.Xmemset(tls, (*TPgHdr)(unsafe.Pointer(p)).FpExtra, 0, uint32(8))
	} else {
		v1 = _sqlite3MallocZero(tls, uint64(uint32(48)+uint32((*TPager)(unsafe.Pointer(pPager)).FnExtra)))
		p = v1
		**(**uintptr)(__ccgo_up(ppPage)) = v1
		if p == uintptr(0) {
			_sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int64(pgno-libc.Uint32FromInt32(1))*(*TPager)(unsafe.Pointer(pPager)).FpageSize, pData)
			return int32(SQLITE_NOMEM)
		}
		(*TPgHdr)(unsafe.Pointer(p)).FpExtra = p + 1*48
		(*TPgHdr)(unsafe.Pointer(p)).Fflags = uint16(PGHDR_MMAP)
		(*TPgHdr)(unsafe.Pointer(p)).FnRef = int64(1)
		(*TPgHdr)(unsafe.Pointer(p)).FpPager = pPager
	}
	(*TPgHdr)(unsafe.Pointer(p)).Fpgno = pgno
	(*TPgHdr)(unsafe.Pointer(p)).FpData = pData
	(*TPager)(unsafe.Pointer(pPager)).FnMmapOut = (*TPager)(unsafe.Pointer(pPager)).FnMmapOut + 1
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Check if zSuper is a valid super-journal name. There are two valid
//	** formats:
//	**
//	**   + The 3rd and 4th last bytes of the filename are ".9", and the
//	**     following 2 bytes are hex digits. This is a file created in 8.3
//	**     filenames mode.
//	**
//	**   + The 3rd last byte of the filename is "9" and the filename
//	**     contains the string "-mj" starting at the 12th last byte.
//	**     All bytes following the "-mj" are hex digits.
//	**
//	** If the filename matches either of these patterns, return non-zero.
//	** Otherwise, return zero.
//	*/
func _pagerIsSuperJrnlName(tls *libc.TLS, zSuper uintptr) (r int32) {
	var ii, nSuper int32
	_, _ = ii, nSuper
	nSuper = _sqlite3Strlen30(tls, zSuper)
	if nSuper < int32(4) {
		return 0
	}
	if int32(**(**int8)(__ccgo_up(zSuper + uintptr(nSuper-int32(3))))) != int32('9') {
		return 0
	}
	if nSuper < int32(12) {
		return 0
	}
	if libc.Xmemcmp(tls, zSuper+uintptr(nSuper-int32(12)), __ccgo_ts+5413, uint32(3)) != 0 {
		return 0
	}
	ii = nSuper - int32(9)
	for {
		if !(ii < nSuper) {
			break
		}
		if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zSuper + uintptr(ii))))])&int32(0x08) == 0 {
			return 0
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** Check that there are at least nSavepoint savepoints open. If there are
//	** currently less than nSavepoints open, then open one or more savepoints
//	** to make up the difference. If the number of savepoints is already
//	** equal to nSavepoint, then this function is a no-op.
//	**
//	** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
//	** occurs while opening the sub-journal file, then an IO error code is
//	** returned. Otherwise, SQLITE_OK.
//	*/
func _pagerOpenSavepoint(tls *libc.TLS, pPager uintptr, nSavepoint int32) (r int32) {
	var aNew uintptr
	var ii, nCurrent, rc int32
	_, _, _, _ = aNew, ii, nCurrent, rc
	rc = SQLITE_OK                                           /* Return code */
	nCurrent = (*TPager)(unsafe.Pointer(pPager)).FnSavepoint /* New Pager.aSavepoint array */
	/* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
	 ** if the allocation fails. Otherwise, zero the new portion in case a
	 ** malloc failure occurs while populating it in the for(...) loop below.
	 */
	aNew = _sqlite3Realloc(tls, (*TPager)(unsafe.Pointer(pPager)).FaSavepoint, uint64(uint32(48)*uint32(nSavepoint)))
	if !(aNew != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, aNew+uintptr(nCurrent)*48, 0, uint32(nSavepoint-nCurrent)*uint32(48))
	(*TPager)(unsafe.Pointer(pPager)).FaSavepoint = aNew
	/* Populate the PagerSavepoint structures just allocated. */
	ii = nCurrent
	for {
		if !(ii < nSavepoint) {
			break
		}
		(**(**TPagerSavepoint)(__ccgo_up(aNew + uintptr(ii)*48))).FnOrig = (*TPager)(unsafe.Pointer(pPager)).FdbSize
		if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) && (*TPager)(unsafe.Pointer(pPager)).FjournalOff > 0 {
			(**(**TPagerSavepoint)(__ccgo_up(aNew + uintptr(ii)*48))).FiOffset = (*TPager)(unsafe.Pointer(pPager)).FjournalOff
		} else {
			(**(**TPagerSavepoint)(__ccgo_up(aNew + uintptr(ii)*48))).FiOffset = int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize)
		}
		(**(**TPagerSavepoint)(__ccgo_up(aNew + uintptr(ii)*48))).FiSubRec = (*TPager)(unsafe.Pointer(pPager)).FnSubRec
		(**(**TPagerSavepoint)(__ccgo_up(aNew + uintptr(ii)*48))).FpInSavepoint = _sqlite3BitvecCreate(tls, (*TPager)(unsafe.Pointer(pPager)).FdbSize)
		(**(**TPagerSavepoint)(__ccgo_up(aNew + uintptr(ii)*48))).FbTruncateOnRelease = int32(1)
		if !((**(**TPagerSavepoint)(__ccgo_up(aNew + uintptr(ii)*48))).FpInSavepoint != 0) {
			return int32(SQLITE_NOMEM)
		}
		if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) {
			_sqlite3WalSavepoint(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, aNew+uintptr(ii)*48+32)
		}
		(*TPager)(unsafe.Pointer(pPager)).FnSavepoint = ii + int32(1)
		goto _1
	_1:
		;
		ii = ii + 1
	}
	return rc
}

// C documentation
//
//	/*
//	** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
//	** exclusive-locking mode when this function is called, take an EXCLUSIVE
//	** lock on the database file and use heap-memory to store the wal-index
//	** in. Otherwise, use the normal shared-memory.
//	*/
func _pagerOpenWal(tls *libc.TLS, pPager uintptr) (r int32) {
	var rc int32
	_ = rc
	rc = SQLITE_OK
	/* If the pager is already in exclusive-mode, the WAL module will use
	 ** heap-memory for the wal-index instead of the VFS shared-memory
	 ** implementation. Take the exclusive lock now, before opening the WAL
	 ** file, to make sure this is safe.
	 */
	if (*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0 {
		rc = _pagerExclusiveLock(tls, pPager)
	}
	/* Open the connection to the log file. If this operation fails,
	 ** (e.g. due to malloc() failure), return an error code.
	 */
	if rc == SQLITE_OK {
		rc = _sqlite3WalOpen(tls, (*TPager)(unsafe.Pointer(pPager)).FpVfs, (*TPager)(unsafe.Pointer(pPager)).Ffd, (*TPager)(unsafe.Pointer(pPager)).FzWal, int32((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode), (*TPager)(unsafe.Pointer(pPager)).FjournalSizeLimit, pPager+232)
	}
	_pagerFixMaplimit(tls, pPager)
	return rc
}

// C documentation
//
//	/*
//	** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
//	** the entire super-journal file. The case pSavepoint==NULL occurs when
//	** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
//	** savepoint.
//	**
//	** When pSavepoint is not NULL (meaning a non-transaction savepoint is
//	** being rolled back), then the rollback consists of up to three stages,
//	** performed in the order specified:
//	**
//	**   * Pages are played back from the main journal starting at byte
//	**     offset PagerSavepoint.iOffset and continuing to
//	**     PagerSavepoint.iHdrOffset, or to the end of the main journal
//	**     file if PagerSavepoint.iHdrOffset is zero.
//	**
//	**   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
//	**     back starting from the journal header immediately following
//	**     PagerSavepoint.iHdrOffset to the end of the main journal file.
//	**
//	**   * Pages are then played back from the sub-journal file, starting
//	**     with the PagerSavepoint.iSubRec and continuing to the end of
//	**     the journal file.
//	**
//	** Throughout the rollback process, each time a page is rolled back, the
//	** corresponding bit is set in a bitvec structure (variable pDone in the
//	** implementation below). This is used to ensure that a page is only
//	** rolled back the first time it is encountered in either journal.
//	**
//	** If pSavepoint is NULL, then pages are only played back from the main
//	** journal file. There is no need for a bitvec in this case.
//	**
//	** In either case, before playback commences the Pager.dbSize variable
//	** is reset to the value that it held at the start of the savepoint
//	** (or transaction). No page with a page-number greater than this value
//	** is played back. If one is encountered it is simply skipped.
//	*/
func _pagerPlaybackSavepoint(tls *libc.TLS, pPager uintptr, pSavepoint uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iHdrOff, szJ Ti64
	var ii, ii1 Tu32
	var pDone uintptr
	var rc int32
	var v1 uint32
	var v2 int64
	var _ /* dummy at bp+4 */ Tu32
	var _ /* nJRec at bp+0 */ Tu32
	var _ /* offset at bp+8 */ Ti64
	_, _, _, _, _, _, _, _ = iHdrOff, ii, ii1, pDone, rc, szJ, v1, v2 /* End of first segment of main-journal records */
	rc = SQLITE_OK                                                    /* Return code */
	pDone = uintptr(0)                                                /* Bitvec to ensure pages played back only once */
	/* Allocate a bitvec to use to store the set of pages rolled back */
	if pSavepoint != 0 {
		pDone = _sqlite3BitvecCreate(tls, (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FnOrig)
		if !(pDone != 0) {
			return int32(SQLITE_NOMEM)
		}
	}
	/* Set the database size back to the value it was before the savepoint
	 ** being reverted was opened.
	 */
	if pSavepoint != 0 {
		v1 = (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FnOrig
	} else {
		v1 = (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize
	}
	(*TPager)(unsafe.Pointer(pPager)).FdbSize = v1
	(*TPager)(unsafe.Pointer(pPager)).FchangeCountDone = (*TPager)(unsafe.Pointer(pPager)).FtempFile
	if !(pSavepoint != 0) && (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) {
		return _pagerRollbackWal(tls, pPager)
	}
	/* Use pPager->journalOff as the effective size of the main rollback
	 ** journal.  The actual file might be larger than this in
	 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
	 ** past pPager->journalOff is off-limits to us.
	 */
	szJ = (*TPager)(unsafe.Pointer(pPager)).FjournalOff
	/* Begin by rolling back records from the main journal starting at
	 ** PagerSavepoint.iOffset and continuing to the next journal header.
	 ** There might be records in the main journal that have a page number
	 ** greater than the current database size (pPager->dbSize) but those
	 ** will be skipped automatically.  Pages are added to pDone as they
	 ** are played back.
	 */
	if pSavepoint != 0 && !((*TPager)(unsafe.Pointer(pPager)).FpWal != libc.UintptrFromInt32(0)) {
		if (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiHdrOffset != 0 {
			v2 = (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiHdrOffset
		} else {
			v2 = szJ
		}
		iHdrOff = v2
		(*TPager)(unsafe.Pointer(pPager)).FjournalOff = (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiOffset
		for rc == SQLITE_OK && (*TPager)(unsafe.Pointer(pPager)).FjournalOff < iHdrOff {
			rc = _pager_playback_one_page(tls, pPager, pPager+80, pDone, int32(1), int32(1))
		}
	} else {
		(*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0
	}
	/* Continue rolling back records out of the main journal starting at
	 ** the first journal header seen and continuing until the effective end
	 ** of the main journal file.  Continue to skip out-of-range pages and
	 ** continue adding pages rolled back to pDone.
	 */
	for rc == SQLITE_OK && (*TPager)(unsafe.Pointer(pPager)).FjournalOff < szJ { /* Loop counter */
		**(**Tu32)(__ccgo_up(bp)) = uint32(0)
		rc = _readJournalHdr(tls, pPager, 0, szJ, bp, bp+4)
		/*
		 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
		 ** test is related to ticket #2565.  See the discussion in the
		 ** pager_playback() function for additional information.
		 */
		if **(**Tu32)(__ccgo_up(bp)) == uint32(0) && (*TPager)(unsafe.Pointer(pPager)).FjournalHdr+int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) == (*TPager)(unsafe.Pointer(pPager)).FjournalOff {
			**(**Tu32)(__ccgo_up(bp)) = uint32((szJ - (*TPager)(unsafe.Pointer(pPager)).FjournalOff) / ((*TPager)(unsafe.Pointer(pPager)).FpageSize + libc.Int64FromInt32(8)))
		}
		ii = uint32(0)
		for {
			if !(rc == SQLITE_OK && ii < **(**Tu32)(__ccgo_up(bp)) && (*TPager)(unsafe.Pointer(pPager)).FjournalOff < szJ) {
				break
			}
			rc = _pager_playback_one_page(tls, pPager, pPager+80, pDone, int32(1), int32(1))
			goto _3
		_3:
			;
			ii = ii + 1
		}
	}
	/* Finally,  rollback pages from the sub-journal.  Page that were
	 ** previously rolled back out of the main journal (and are hence in pDone)
	 ** will be skipped.  Out-of-range pages are also skipped.
	 */
	if pSavepoint != 0 { /* Loop counter */
		**(**Ti64)(__ccgo_up(bp + 8)) = int64((*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiSubRec) * (int64(4) + (*TPager)(unsafe.Pointer(pPager)).FpageSize)
		if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) {
			rc = _sqlite3WalSavepointUndo(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, pSavepoint+32)
		}
		ii1 = (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiSubRec
		for {
			if !(rc == SQLITE_OK && ii1 < (*TPager)(unsafe.Pointer(pPager)).FnSubRec) {
				break
			}
			rc = _pager_playback_one_page(tls, pPager, bp+8, pDone, 0, int32(1))
			goto _4
		_4:
			;
			ii1 = ii1 + 1
		}
	}
	_sqlite3BitvecDestroy(tls, pDone)
	if rc == SQLITE_OK {
		(*TPager)(unsafe.Pointer(pPager)).FjournalOff = szJ
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is called by the pcache layer when it has reached some
//	** soft memory limit. The first argument is a pointer to a Pager object
//	** (cast as a void*). The pager is always 'purgeable' (not an in-memory
//	** database). The second argument is a reference to a page that is
//	** currently dirty but has no outstanding references. The page
//	** is always associated with the Pager object passed as the first
//	** argument.
//	**
//	** The job of this function is to make pPg clean by writing its contents
//	** out to the database file, if possible. This may involve syncing the
//	** journal file.
//	**
//	** If successful, sqlite3PcacheMakeClean() is called on the page and
//	** SQLITE_OK returned. If an IO error occurs while trying to make the
//	** page clean, the IO error code is returned. If the page cannot be
//	** made clean for some other reason, but no error occurs, then SQLITE_OK
//	** is returned by sqlite3PcacheMakeClean() is not called.
//	*/
func _pagerStress(tls *libc.TLS, p uintptr, pPg uintptr) (r int32) {
	var pPager uintptr
	var rc int32
	_, _ = pPager, rc
	pPager = p
	rc = SQLITE_OK
	/* The doNotSpill NOSYNC bit is set during times when doing a sync of
	 ** journal (and adding a new header) is not allowed.  This occurs
	 ** during calls to sqlite3PagerWrite() while trying to journal multiple
	 ** pages belonging to the same sector.
	 **
	 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
	 ** regardless of whether or not a sync is required.  This is set during
	 ** a rollback or by user request, respectively.
	 **
	 ** Spilling is also prohibited when in an error state since that could
	 ** lead to database corruption.   In the current implementation it
	 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
	 ** while in the error state, hence it is impossible for this routine to
	 ** be called in the error state.  Nevertheless, we include a NEVER()
	 ** test for the error state as a safeguard against future changes.
	 */
	if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 {
		return SQLITE_OK
	}
	if (*TPager)(unsafe.Pointer(pPager)).FdoNotSpill != 0 && (int32((*TPager)(unsafe.Pointer(pPager)).FdoNotSpill)&(libc.Int32FromInt32(SPILLFLAG_ROLLBACK)|libc.Int32FromInt32(SPILLFLAG_OFF)) != 0 || int32((*TPgHdr)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_NEED_SYNC) != 0) {
		return SQLITE_OK
	}
	**(**Tu32)(__ccgo_up(pPager + 200 + 3*4)) = **(**Tu32)(__ccgo_up(pPager + 200 + 3*4)) + 1
	(*TPgHdr)(unsafe.Pointer(pPg)).FpDirty = uintptr(0)
	if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) {
		/* Write a single frame for this page to the log. */
		rc = _subjournalPageIfRequired(tls, pPg)
		if rc == SQLITE_OK {
			rc = _pagerWalFrames(tls, pPager, pPg, uint32(0), 0)
		}
	} else {
		/* Sync the journal file if required. */
		if int32((*TPgHdr)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_NEED_SYNC) != 0 || int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_WRITER_CACHEMOD) {
			rc = _syncJournal(tls, pPager, int32(1))
		}
		/* Write the contents of the page out to the database file. */
		if rc == SQLITE_OK {
			rc = _pager_write_pagelist(tls, pPager, pPg)
		}
	}
	/* Mark the page as clean. */
	if rc == SQLITE_OK {
		_sqlite3PcacheMakeClean(tls, pPg)
	}
	return _pager_error(tls, pPager, rc)
}

// C documentation
//
//	/*
//	** This function is a wrapper around sqlite3WalFrames(). As well as logging
//	** the contents of the list of pages headed by pList (connected by pDirty),
//	** this function notifies any active backup processes that the pages have
//	** changed.
//	**
//	** The list of pages passed into this routine is always sorted by page number.
//	** Hence, if page 1 appears anywhere on the list, it will be the first page.
//	*/
func _pagerWalFrames(tls *libc.TLS, pPager uintptr, _pList uintptr, nTruncate TPgno, isCommit int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*uintptr)(unsafe.Pointer(bp)) = _pList
	var nList, rc int32
	var p, ppNext, v2 uintptr
	_, _, _, _, _ = nList, p, ppNext, rc, v2 /* For looping over pages */
	if isCommit != 0 {
		/* If a WAL transaction is being committed, there is no point in writing
		 ** any pages with page numbers greater than nTruncate into the WAL file.
		 ** They will never be read by any client. So remove them from the pDirty
		 ** list here. */
		ppNext = bp
		nList = 0
		p = **(**uintptr)(__ccgo_up(bp))
		for {
			v2 = p
			**(**uintptr)(__ccgo_up(ppNext)) = v2
			if !(v2 != uintptr(0)) {
				break
			}
			if (*TPgHdr)(unsafe.Pointer(p)).Fpgno <= nTruncate {
				ppNext = p + 16
				nList = nList + 1
			}
			goto _1
		_1:
			;
			p = (*TPgHdr)(unsafe.Pointer(p)).FpDirty
		}
	} else {
		nList = int32(1)
	}
	**(**Tu32)(__ccgo_up(pPager + 200 + 2*4)) += uint32(nList)
	if (*TPgHdr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fpgno == uint32(1) {
		_pager_write_changecounter(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	rc = _sqlite3WalFrames(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), **(**uintptr)(__ccgo_up(bp)), nTruncate, isCommit, int32((*TPager)(unsafe.Pointer(pPager)).FwalSyncFlags))
	if rc == SQLITE_OK && (*TPager)(unsafe.Pointer(pPager)).FpBackup != 0 {
		p = **(**uintptr)(__ccgo_up(bp))
		for {
			if !(p != 0) {
				break
			}
			_sqlite3BackupUpdate(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup, (*TPgHdr)(unsafe.Pointer(p)).Fpgno, (*TPgHdr)(unsafe.Pointer(p)).FpData)
			goto _3
		_3:
			;
			p = (*TPgHdr)(unsafe.Pointer(p)).FpDirty
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** This is a variant of sqlite3PagerWrite() that runs when the sector size
//	** is larger than the page size.  SQLite makes the (reasonable) assumption that
//	** all bytes of a sector are written together by hardware.  Hence, all bytes of
//	** a sector need to be journalled in case of a power loss in the middle of
//	** a write.
//	**
//	** Usually, the sector size is less than or equal to the page size, in which
//	** case pages can be individually written.  This routine only runs in the
//	** exceptional case where the page size is smaller than the sector size.
//	*/
func _pagerWriteLargeSector(tls *libc.TLS, pPg uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var ii, nPage, needSync, rc int32
	var nPageCount, nPagePerSector, pg, pg1 TPgno
	var pPage1, pPager, v1 uintptr
	var _ /* pPage at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _ = ii, nPage, nPageCount, nPagePerSector, needSync, pPage1, pPager, pg, pg1, rc, v1
	rc = SQLITE_OK                                  /* First page of the sector pPg is located on. */
	nPage = 0                                       /* Loop counter */
	needSync = 0                                    /* True if any page has PGHDR_NEED_SYNC */
	pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager /* The pager that owns pPg */
	nPagePerSector = uint32(int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) / (*TPager)(unsafe.Pointer(pPager)).FpageSize)
	/* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
	 ** a journal header to be written between the pages journaled by
	 ** this function.
	 */
	v1 = pPager + 21
	*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(SPILLFLAG_NOSYNC))
	/* This trick assumes that both the page-size and sector-size are
	 ** an integer power of 2. It sets variable pg1 to the identifier
	 ** of the first page of the sector pPg is located on.
	 */
	pg1 = ((*TPgHdr)(unsafe.Pointer(pPg)).Fpgno-uint32(1)) & ^(nPagePerSector-libc.Uint32FromInt32(1)) + uint32(1)
	nPageCount = (*TPager)(unsafe.Pointer(pPager)).FdbSize
	if (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno > nPageCount {
		nPage = int32((*TPgHdr)(unsafe.Pointer(pPg)).Fpgno - pg1 + uint32(1))
	} else {
		if pg1+nPagePerSector-uint32(1) > nPageCount {
			nPage = int32(nPageCount + uint32(1) - pg1)
		} else {
			nPage = int32(nPagePerSector)
		}
	}
	ii = 0
	for {
		if !(ii < nPage && rc == SQLITE_OK) {
			break
		}
		pg = pg1 + uint32(ii)
		if pg == (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno || !(_sqlite3BitvecTest(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, pg) != 0) {
			if pg != (*TPager)(unsafe.Pointer(pPager)).FlckPgno {
				rc = _sqlite3PagerGet(tls, pPager, pg, bp, 0)
				if rc == SQLITE_OK {
					rc = _pager_write(tls, **(**uintptr)(__ccgo_up(bp)))
					if int32((*TPgHdr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fflags)&int32(PGHDR_NEED_SYNC) != 0 {
						needSync = int32(1)
					}
					_sqlite3PagerUnrefNotNull(tls, **(**uintptr)(__ccgo_up(bp)))
				}
			}
		} else {
			v1 = _sqlite3PagerLookup(tls, pPager, pg)
			**(**uintptr)(__ccgo_up(bp)) = v1
			if v1 != uintptr(0) {
				if int32((*TPgHdr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fflags)&int32(PGHDR_NEED_SYNC) != 0 {
					needSync = int32(1)
				}
				_sqlite3PagerUnrefNotNull(tls, **(**uintptr)(__ccgo_up(bp)))
			}
		}
		goto _2
	_2:
		;
		ii = ii + 1
	}
	/* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
	 ** starting at pg1, then it needs to be set for all of them. Because
	 ** writing to any of these nPage pages may damage the others, the
	 ** journal file must contain sync()ed copies of all of them
	 ** before any of them can be written out to the database file.
	 */
	if rc == SQLITE_OK && needSync != 0 {
		ii = 0
		for {
			if !(ii < nPage) {
				break
			}
			pPage1 = _sqlite3PagerLookup(tls, pPager, pg1+uint32(ii))
			if pPage1 != 0 {
				v1 = pPage1 + 28
				*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(PGHDR_NEED_SYNC))
				_sqlite3PagerUnrefNotNull(tls, pPage1)
			}
			goto _4
		_4:
			;
			ii = ii + 1
		}
	}
	v1 = pPager + 21
	*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(SPILLFLAG_NOSYNC))
	return rc
}

// C documentation
//
//	/*
//	** Parameter zSuper is the name of a super-journal file. A single journal
//	** file that referred to the super-journal file has just been rolled back.
//	** This routine checks if it is possible to delete the super-journal file,
//	** and does so if it is.
//	**
//	** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not
//	** available for use within this function.
//	**
//	** When a super-journal file is created, it is populated with the names
//	** of all of its child journals, one after another, formatted as utf-8
//	** encoded text. The end of each child journal file is marked with a
//	** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
//	** file for a transaction involving two databases might be:
//	**
//	**   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
//	**
//	** A super-journal file may only be deleted once all of its child
//	** journals have been rolled back.
//	**
//	** This function reads the contents of the super-journal file into
//	** memory and loops through each of the child journal names. For
//	** each child journal, it checks if:
//	**
//	**   * if the child journal exists, and if so
//	**   * if the child journal contains a reference to super-journal
//	**     file zSuper
//	**
//	** If a child journal can be found that matches both of the criteria
//	** above, this function returns without doing anything. Otherwise, if
//	** no such child journal can be found, file zSuper is deleted from
//	** the file-system using sqlite3OsDelete().
//	**
//	** If an IO error within this function, an error code is returned. This
//	** function allocates memory by calling sqlite3Malloc(). If an allocation
//	** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
//	** occur, SQLITE_OK is returned.
//	**
//	** TODO: This function allocates a single block of memory to load
//	** the entire contents of the super-journal file. This could be
//	** a couple of kilobytes or so - potentially larger than the page
//	** size.
//	*/
func _pager_delsuper(tls *libc.TLS, pPager uintptr, zSuper uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bSeen, c, flags, flags1, rc int32
	var pJournal, pSuper, pVfs, zFree, zJournal, zSuperJournal uintptr
	var v1, v2, v3 int8
	var _ /* exists at bp+8 */ int32
	var _ /* nSuperJournal at bp+0 */ Ti64
	var _ /* zSuperPtr at bp+12 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = bSeen, c, flags, flags1, pJournal, pSuper, pVfs, rc, zFree, zJournal, zSuperJournal, v1, v2, v3
	pVfs = (*TPager)(unsafe.Pointer(pPager)).FpVfs /* Malloc'd child-journal file descriptor */
	zSuperJournal = uintptr(0)                     /* Pointer to one journal within MJ file */
	zFree = uintptr(0)                             /* Free this buffer */
	bSeen = 0                                      /* If super-journal contains pPager->zJournal */
	/* Check if this looks like a real super-journal name. If it does not,
	 ** return SQLITE_OK without attempting to delete it. This is to limit
	 ** the degree to which a crafted journal file can be used to cause
	 ** SQLite to delete arbitrary files. */
	if _pagerIsSuperJrnlName(tls, zSuper) == 0 {
		return SQLITE_OK
	}
	/* Allocate space for both the pJournal and pSuper file descriptors.
	 ** If successful, open the super-journal file for reading.
	 */
	pSuper = _sqlite3MallocZero(tls, uint64(int64(2)*int64((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile)))
	if !(pSuper != 0) {
		rc = int32(SQLITE_NOMEM)
		pJournal = uintptr(0)
	} else {
		flags = libc.Int32FromInt32(SQLITE_OPEN_READONLY) | libc.Int32FromInt32(SQLITE_OPEN_SUPER_JOURNAL)
		rc = _sqlite3OsOpen(tls, pVfs, zSuper, pSuper, flags, uintptr(0))
		pJournal = pSuper + uintptr((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile)
	}
	if rc != SQLITE_OK {
		goto delsuper_out
	}
	/* Load the entire super-journal file into space obtained from
	 ** sqlite3_malloc() and pointed to by zSuperJournal.   Also obtain
	 ** sufficient space (in zSuperPtr) to hold the names of super-journal
	 ** files extracted from regular rollback-journals.
	 */
	rc = _sqlite3OsFileSize(tls, pSuper, bp)
	if rc != SQLITE_OK {
		goto delsuper_out
	}
	zFree = _sqlite3Malloc(tls, uint64(int64(4)+**(**Ti64)(__ccgo_up(bp))+int64(2)))
	if !(zFree != 0) {
		rc = int32(SQLITE_NOMEM)
		goto delsuper_out
	} else {
	}
	v3 = libc.Int8FromInt32(0)
	**(**int8)(__ccgo_up(zFree + 3)) = v3
	v2 = v3
	**(**int8)(__ccgo_up(zFree + 2)) = v2
	v1 = v2
	**(**int8)(__ccgo_up(zFree + 1)) = v1
	**(**int8)(__ccgo_up(zFree)) = v1
	zSuperJournal = zFree + 4
	rc = _sqlite3OsRead(tls, pSuper, zSuperJournal, int32(**(**Ti64)(__ccgo_up(bp))), 0)
	if rc != SQLITE_OK {
		goto delsuper_out
	}
	**(**int8)(__ccgo_up(zSuperJournal + uintptr(**(**Ti64)(__ccgo_up(bp))))) = 0
	**(**int8)(__ccgo_up(zSuperJournal + uintptr(**(**Ti64)(__ccgo_up(bp))+int64(1)))) = 0
	zJournal = zSuperJournal
	for int64(int32(zJournal)-int32(zSuperJournal)) < **(**Ti64)(__ccgo_up(bp)) {
		if libc.Xstrcmp(tls, zJournal, (*TPager)(unsafe.Pointer(pPager)).FzJournal) == 0 {
			bSeen = int32(1)
		} else {
			rc = _sqlite3OsAccess(tls, pVfs, zJournal, SQLITE_ACCESS_EXISTS, bp+8)
			if rc != SQLITE_OK {
				goto delsuper_out
			}
			if **(**int32)(__ccgo_up(bp + 8)) != 0 {
				**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
				flags1 = libc.Int32FromInt32(SQLITE_OPEN_READONLY) | libc.Int32FromInt32(SQLITE_OPEN_SUPER_JOURNAL)
				rc = _sqlite3OsOpen(tls, pVfs, zJournal, pJournal, flags1, uintptr(0))
				if rc != SQLITE_OK {
					goto delsuper_out
				}
				rc = _readSuperJournal(tls, pJournal, uint64(1)+uint64((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname), bp+12)
				_sqlite3OsClose(tls, pJournal)
				if rc != SQLITE_OK {
					goto delsuper_out
				}
				c = libc.BoolInt32(**(**uintptr)(__ccgo_up(bp + 12)) != uintptr(0) && libc.Xstrcmp(tls, **(**uintptr)(__ccgo_up(bp + 12)), zSuper) == 0)
				_freeSuperJournal(tls, **(**uintptr)(__ccgo_up(bp + 12)))
				if c != 0 {
					/* We have a match. Do not delete the super-journal file. */
					goto delsuper_out
				}
			}
		}
		zJournal = zJournal + uintptr(_sqlite3Strlen30(tls, zJournal)+libc.Int32FromInt32(1))
	}
	_sqlite3OsClose(tls, pSuper)
	if bSeen != 0 {
		/* Only delete the super-journal if bSeen is true - indicating that
		 ** the super-journal contained a pointer to this database's journal
		 ** file. */
		rc = _sqlite3OsDelete(tls, pVfs, zSuper, 0)
	}
	goto delsuper_out
delsuper_out:
	;
	Xsqlite3_free(tls, zFree)
	if pSuper != 0 {
		_sqlite3OsClose(tls, pSuper)
		Xsqlite3_free(tls, pSuper)
	}
	return rc
}

// C documentation
//
//	/*
//	** Playback the journal and thus restore the database file to
//	** the state it was in before we started making changes.
//	**
//	** The journal file format is as follows:
//	**
//	**  (1)  8 byte prefix.  A copy of aJournalMagic[].
//	**  (2)  4 byte big-endian integer which is the number of valid page records
//	**       in the journal.  If this value is 0xffffffff, then compute the
//	**       number of page records from the journal size.
//	**  (3)  4 byte big-endian integer which is the initial value for the
//	**       sanity checksum.
//	**  (4)  4 byte integer which is the number of pages to truncate the
//	**       database to during a rollback.
//	**  (5)  4 byte big-endian integer which is the sector size.  The header
//	**       is this many bytes in size.
//	**  (6)  4 byte big-endian integer which is the page size.
//	**  (7)  zero padding out to the next sector size.
//	**  (8)  Zero or more pages instances, each as follows:
//	**        +  4 byte page number.
//	**        +  pPager->pageSize bytes of data.
//	**        +  4 byte checksum
//	**
//	** When we speak of the journal header, we mean the first 7 items above.
//	** Each entry in the journal is an instance of the 8th item.
//	**
//	** Call the value from the second bullet "nRec".  nRec is the number of
//	** valid page entries in the journal.  In most cases, you can compute the
//	** value of nRec from the size of the journal file.  But if a power
//	** failure occurred while the journal was being written, it could be the
//	** case that the size of the journal file had already been increased but
//	** the extra entries had not yet made it safely to disk.  In such a case,
//	** the value of nRec computed from the file size would be too large.  For
//	** that reason, we always use the nRec value in the header.
//	**
//	** If the nRec value is 0xffffffff it means that nRec should be computed
//	** from the file size.  This value is used when the user selects the
//	** no-sync option for the journal.  A power failure could lead to corruption
//	** in this case.  But for things like temporary table (which will be
//	** deleted when the power is restored) we don't care.
//	**
//	** If the file opened as the journal file is not a well-formed
//	** journal file then all pages up to the first corrupted page are rolled
//	** back (or no pages if the journal header is corrupted). The journal file
//	** is then deleted and SQLITE_OK returned, just as if no corruption had
//	** been encountered.
//	**
//	** If an I/O or malloc() error occurs, the journal-file is not deleted
//	** and an error code is returned.
//	**
//	** The isHot parameter indicates that we are trying to rollback a journal
//	** that might be a hot journal.  Or, it could be that the journal is
//	** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
//	** If the journal really is hot, reset the pager cache prior rolling
//	** back any content.  If the journal is merely persistent, no reset is
//	** needed.
//	*/
func _pager_playback(tls *libc.TLS, pPager uintptr, isHot int32) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var nPlayback, needPagerReset, rc int32
	var pVfs uintptr
	var u Tu32
	var _ /* mxPg at bp+12 */ TPgno
	var _ /* nRec at bp+8 */ Tu32
	var _ /* res at bp+16 */ int32
	var _ /* savedPageSize at bp+24 */ Tu32
	var _ /* szJ at bp+0 */ Ti64
	var _ /* zSuper at bp+20 */ uintptr
	_, _, _, _, _ = nPlayback, needPagerReset, pVfs, rc, u
	pVfs = (*TPager)(unsafe.Pointer(pPager)).FpVfs /* Unsigned loop counter */
	**(**TPgno)(__ccgo_up(bp + 12)) = uint32(0)    /* Result code of a subroutine */
	**(**int32)(__ccgo_up(bp + 16)) = int32(1)     /* Value returned by sqlite3OsAccess() */
	**(**uintptr)(__ccgo_up(bp + 20)) = uintptr(0) /* True to reset page prior to first page rollback */
	nPlayback = 0                                  /* Total number of pages restored from journal */
	**(**Tu32)(__ccgo_up(bp + 24)) = uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize)
	/* Figure out how many records are in the journal.  Abort early if
	 ** the journal is empty.
	 */
	rc = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp)
	if rc != SQLITE_OK {
		goto end_playback
	}
	/* Read the super-journal name from the journal, if it is present.
	 ** If a super-journal file name is specified, but the file is not
	 ** present on disk, then the journal is not hot and does not need to be
	 ** played back.
	 */
	rc = _readSuperJournal(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, uint64(int64(1)+int64((*Tsqlite3_vfs)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).FpVfs)).FmxPathname)), bp+20)
	if rc == SQLITE_OK && **(**uintptr)(__ccgo_up(bp + 20)) != 0 {
		rc = _sqlite3OsAccess(tls, pVfs, **(**uintptr)(__ccgo_up(bp + 20)), SQLITE_ACCESS_EXISTS, bp+16)
	}
	if rc != SQLITE_OK || !(**(**int32)(__ccgo_up(bp + 16)) != 0) {
		goto end_playback
	}
	(*TPager)(unsafe.Pointer(pPager)).FjournalOff = 0
	needPagerReset = isHot
	/* This loop terminates either when a readJournalHdr() or
	 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
	 ** occurs.
	 */
	for int32(1) != 0 {
		/* Read the next journal header from the journal file.  If there are
		 ** not enough bytes left in the journal file for a complete header, or
		 ** it is corrupted, then a process must have failed while writing it.
		 ** This indicates nothing more needs to be rolled back.
		 */
		rc = _readJournalHdr(tls, pPager, isHot, **(**Ti64)(__ccgo_up(bp)), bp+8, bp+12)
		if rc != SQLITE_OK {
			if rc == int32(SQLITE_DONE) {
				rc = SQLITE_OK
			}
			goto end_playback
		}
		/* If nRec is 0xffffffff, then this journal was created by a process
		 ** working in no-sync mode. This means that the rest of the journal
		 ** file consists of pages, there are no more journal headers. Compute
		 ** the value of nRec based on this assumption.
		 */
		if **(**Tu32)(__ccgo_up(bp + 8)) == uint32(0xffffffff) {
			**(**Tu32)(__ccgo_up(bp + 8)) = uint32(int32((**(**Ti64)(__ccgo_up(bp)) - int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize)) / ((*TPager)(unsafe.Pointer(pPager)).FpageSize + libc.Int64FromInt32(8))))
		}
		/* If nRec is 0 and this rollback is of a transaction created by this
		 ** process and if this is the final header in the journal, then it means
		 ** that this part of the journal was being filled but has not yet been
		 ** synced to disk.  Compute the number of pages based on the remaining
		 ** size of the file.
		 **
		 ** The third term of the test was added to fix ticket #2565.
		 ** When rolling back a hot journal, nRec==0 always means that the next
		 ** chunk of the journal contains zero pages to be rolled back.  But
		 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
		 ** the journal, it means that the journal might contain additional
		 ** pages that need to be rolled back and that the number of pages
		 ** should be computed based on the journal file size.
		 */
		if **(**Tu32)(__ccgo_up(bp + 8)) == uint32(0) && !(isHot != 0) && (*TPager)(unsafe.Pointer(pPager)).FjournalHdr+int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) == (*TPager)(unsafe.Pointer(pPager)).FjournalOff {
			**(**Tu32)(__ccgo_up(bp + 8)) = uint32(int32((**(**Ti64)(__ccgo_up(bp)) - (*TPager)(unsafe.Pointer(pPager)).FjournalOff) / ((*TPager)(unsafe.Pointer(pPager)).FpageSize + libc.Int64FromInt32(8))))
		}
		/* If this is the first header read from the journal, truncate the
		 ** database file back to its original size.
		 */
		if (*TPager)(unsafe.Pointer(pPager)).FjournalOff == int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) {
			rc = _pager_truncate(tls, pPager, **(**TPgno)(__ccgo_up(bp + 12)))
			if rc != SQLITE_OK {
				goto end_playback
			}
			(*TPager)(unsafe.Pointer(pPager)).FdbSize = **(**TPgno)(__ccgo_up(bp + 12))
			if (*TPager)(unsafe.Pointer(pPager)).FmxPgno < **(**TPgno)(__ccgo_up(bp + 12)) {
				(*TPager)(unsafe.Pointer(pPager)).FmxPgno = **(**TPgno)(__ccgo_up(bp + 12))
			}
		}
		/* Copy original pages out of the journal and back into the
		 ** database file and/or page cache.
		 */
		u = uint32(0)
		for {
			if !(u < **(**Tu32)(__ccgo_up(bp + 8))) {
				break
			}
			if needPagerReset != 0 {
				_pager_reset(tls, pPager)
				needPagerReset = 0
			}
			rc = _pager_playback_one_page(tls, pPager, pPager+80, uintptr(0), int32(1), 0)
			if rc == SQLITE_OK {
				nPlayback = nPlayback + 1
			} else {
				if rc == int32(SQLITE_DONE) {
					(*TPager)(unsafe.Pointer(pPager)).FjournalOff = **(**Ti64)(__ccgo_up(bp))
					break
				} else {
					if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8) {
						/* If the journal has been truncated, simply stop reading and
						 ** processing the journal. This might happen if the journal was
						 ** not completely written and synced prior to a crash.  In that
						 ** case, the database should have never been written in the
						 ** first place so it is OK to simply abandon the rollback. */
						rc = SQLITE_OK
						goto end_playback
					} else {
						/* If we are unable to rollback, quit and return the error
						 ** code.  This will cause the pager to enter the error state
						 ** so that no further harm will be done.  Perhaps the next
						 ** process to come along will be able to rollback the database.
						 */
						goto end_playback
					}
				}
			}
			goto _1
		_1:
			;
			u = u + 1
		}
	}
	/*NOTREACHED*/
	goto end_playback
end_playback:
	;
	if rc == SQLITE_OK {
		rc = _sqlite3PagerSetPagesize(tls, pPager, bp+24, -int32(1))
	}
	/* Following a rollback, the database file should be back in its original
	 ** state prior to the start of the transaction, so invoke the
	 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
	 ** assertion that the transaction counter was modified.
	 */
	/* If this playback is happening automatically as a result of an IO or
	 ** malloc error that occurred after the change-counter was updated but
	 ** before the transaction was committed, then the change-counter
	 ** modification may just have been reverted. If this happens in exclusive
	 ** mode, then subsequent transactions performed by the connection will not
	 ** update the change-counter at all. This may lead to cache inconsistency
	 ** problems for other processes at some point in the future. So, just
	 ** in case this has happened, clear the changeCountDone flag now.
	 */
	(*TPager)(unsafe.Pointer(pPager)).FchangeCountDone = (*TPager)(unsafe.Pointer(pPager)).FtempFile
	if rc == SQLITE_OK && (int32((*TPager)(unsafe.Pointer(pPager)).FeState) >= int32(PAGER_WRITER_DBMOD) || int32((*TPager)(unsafe.Pointer(pPager)).FeState) == PAGER_OPEN) {
		rc = _sqlite3PagerSync(tls, pPager, uintptr(0))
	}
	if rc == SQLITE_OK {
		rc = _pager_end_transaction(tls, pPager, libc.BoolInt32(**(**uintptr)(__ccgo_up(bp + 20)) != uintptr(0)), 0)
	}
	if rc == SQLITE_OK && **(**uintptr)(__ccgo_up(bp + 20)) != 0 && **(**int32)(__ccgo_up(bp + 16)) != 0 {
		/* If there was a super-journal and this routine will return success,
		 ** see if it is possible to delete the super-journal.
		 */
		rc = _pager_delsuper(tls, pPager, **(**uintptr)(__ccgo_up(bp + 20)))
	}
	if isHot != 0 && nPlayback != 0 {
		Xsqlite3_log(tls, libc.Int32FromInt32(SQLITE_NOTICE)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8), __ccgo_ts+5417, libc.VaList(bp+40, nPlayback, (*TPager)(unsafe.Pointer(pPager)).FzJournal))
	}
	/* The Pager.sectorSize variable may have been updated while rolling
	 ** back a journal created by a process with a different sector size
	 ** value. Reset it to the correct value for this process.
	 */
	_freeSuperJournal(tls, **(**uintptr)(__ccgo_up(bp + 20)))
	_setSectorSize(tls, pPager)
	return rc
}

// C documentation
//
//	/*
//	** Read a single page from either the journal file (if isMainJrnl==1) or
//	** from the sub-journal (if isMainJrnl==0) and playback that page.
//	** The page begins at offset *pOffset into the file. The *pOffset
//	** value is increased to the start of the next page in the journal.
//	**
//	** The main rollback journal uses checksums - the statement journal does
//	** not.
//	**
//	** If the page number of the page record read from the (sub-)journal file
//	** is greater than the current value of Pager.dbSize, then playback is
//	** skipped and SQLITE_OK is returned.
//	**
//	** If pDone is not NULL, then it is a record of pages that have already
//	** been played back.  If the page at *pOffset has already been played back
//	** (if the corresponding pDone bit is set) then skip the playback.
//	** Make sure the pDone bit corresponding to the *pOffset page is set
//	** prior to returning.
//	**
//	** If the page record is successfully read from the (sub-)journal file
//	** and played back, then SQLITE_OK is returned. If an IO error occurs
//	** while reading the record from the (sub-)journal file or while writing
//	** to the database file, then the IO error code is returned. If data
//	** is successfully read from the (sub-)journal file but appears to be
//	** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
//	** two circumstances:
//	**
//	**   * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
//	**   * If the record is being rolled back from the main journal file
//	**     and the checksum field does not match the record content.
//	**
//	** Neither of these two scenarios are possible during a savepoint rollback.
//	**
//	** If this is a savepoint rollback, then memory may have to be dynamically
//	** allocated by this function. If this is the case and an allocation fails,
//	** SQLITE_NOMEM is returned.
//	*/
func _pager_playback_one_page(tls *libc.TLS, pPager uintptr, pOffset uintptr, pDone uintptr, isMainJrnl int32, isSavepnt int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aData, jfd, pData, v1 uintptr
	var isSynced, rc, v2 int32
	var ofst Ti64
	var v3 bool
	var _ /* cksum at bp+8 */ Tu32
	var _ /* pPg at bp+0 */ uintptr
	var _ /* pgno at bp+4 */ TPgno
	_, _, _, _, _, _, _, _, _ = aData, isSynced, jfd, ofst, pData, rc, v1, v2, v3 /* True if journal page is synced */
	/* isMainJrnl is 0 or 1 */
	/* isSavepnt is 0 or 1 */
	/* pDone always used on sub-journals */
	/* pDone never used on non-savepoint */
	aData = (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace
	/* Temp storage must have already been allocated */
	/* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
	 ** or savepoint rollback done at the request of the caller) or this is
	 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
	 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
	 ** only reads from the main journal, not the sub-journal.
	 */
	/* Read the page number and page data from the journal or sub-journal
	 ** file. Return an error code to the caller if an IO error occurs.
	 */
	if isMainJrnl != 0 {
		v1 = (*TPager)(unsafe.Pointer(pPager)).Fjfd
	} else {
		v1 = (*TPager)(unsafe.Pointer(pPager)).Fsjfd
	}
	jfd = v1
	rc = _read32bits(tls, jfd, **(**Ti64)(__ccgo_up(pOffset)), bp+4)
	if rc != SQLITE_OK {
		return rc
	}
	rc = _sqlite3OsRead(tls, jfd, aData, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), **(**Ti64)(__ccgo_up(pOffset))+int64(4))
	if rc != SQLITE_OK {
		return rc
	}
	**(**Ti64)(__ccgo_up(pOffset)) += (*TPager)(unsafe.Pointer(pPager)).FpageSize + int64(4) + int64(isMainJrnl*int32(4))
	/* Sanity checking on the page.  This is more important that I originally
	 ** thought.  If a power failure occurs while the journal is being written,
	 ** it could cause invalid data to be written into the journal.  We need to
	 ** detect this invalid data (with high probability) and ignore it.
	 */
	if **(**TPgno)(__ccgo_up(bp + 4)) == uint32(0) || **(**TPgno)(__ccgo_up(bp + 4)) == (*TPager)(unsafe.Pointer(pPager)).FlckPgno {
		return int32(SQLITE_DONE)
	}
	if **(**TPgno)(__ccgo_up(bp + 4)) > (*TPager)(unsafe.Pointer(pPager)).FdbSize || _sqlite3BitvecTest(tls, pDone, **(**TPgno)(__ccgo_up(bp + 4))) != 0 {
		return SQLITE_OK
	}
	if isMainJrnl != 0 {
		rc = _read32bits(tls, jfd, **(**Ti64)(__ccgo_up(pOffset))-int64(4), bp+8)
		if rc != 0 {
			return rc
		}
		if !(isSavepnt != 0) && _pager_cksum(tls, pPager, aData) != **(**Tu32)(__ccgo_up(bp + 8)) {
			return int32(SQLITE_DONE)
		}
	}
	/* If this page has already been played back before during the current
	 ** rollback, then don't bother to play it back again.
	 */
	if v3 = pDone != 0; v3 {
		v2 = _sqlite3BitvecSet(tls, pDone, **(**TPgno)(__ccgo_up(bp + 4)))
		rc = v2
	}
	if v3 && v2 != SQLITE_OK {
		return rc
	}
	/* When playing back page 1, restore the nReserve setting
	 */
	if **(**TPgno)(__ccgo_up(bp + 4)) == uint32(1) && int32((*TPager)(unsafe.Pointer(pPager)).FnReserve) != int32(**(**Tu8)(__ccgo_up(aData + 20))) {
		(*TPager)(unsafe.Pointer(pPager)).FnReserve = int16(**(**Tu8)(__ccgo_up(aData + 20)))
	}
	/* If the pager is in CACHEMOD state, then there must be a copy of this
	 ** page in the pager cache. In this case just update the pager cache,
	 ** not the database file. The page is left marked dirty in this case.
	 **
	 ** An exception to the above rule: If the database is in no-sync mode
	 ** and a page is moved during an incremental vacuum then the page may
	 ** not be in the pager cache. Later: if a malloc() or IO error occurs
	 ** during a Movepage() call, then the page may not be in the cache
	 ** either. So the condition described in the above paragraph is not
	 ** assert()able.
	 **
	 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
	 ** pager cache if it exists and the main file. The page is then marked
	 ** not dirty. Since this code is only executed in PAGER_OPEN state for
	 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
	 ** if the pager is in OPEN state.
	 **
	 ** Ticket #1171:  The statement journal might contain page content that is
	 ** different from the page content at the start of the transaction.
	 ** This occurs when a page is changed prior to the start of a statement
	 ** then changed again within the statement.  When rolling back such a
	 ** statement we must not write to the original database unless we know
	 ** for certain that original page contents are synced into the main rollback
	 ** journal.  Otherwise, a power loss might leave modified data in the
	 ** database file without an entry in the rollback journal that can
	 ** restore the database to its original form.  Two conditions must be
	 ** met before writing to the database files. (1) the database must be
	 ** locked.  (2) we know that the original page content is fully synced
	 ** in the main journal either because the page is not in cache or else
	 ** the page is marked as needSync==0.
	 **
	 ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
	 ** is possible to fail a statement on a database that does not yet exist.
	 ** Do not attempt to write if database file has never been opened.
	 */
	if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) {
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	} else {
		**(**uintptr)(__ccgo_up(bp)) = _sqlite3PagerLookup(tls, pPager, **(**TPgno)(__ccgo_up(bp + 4)))
	}
	if isMainJrnl != 0 {
		isSynced = libc.BoolInt32((*TPager)(unsafe.Pointer(pPager)).FnoSync != 0 || **(**Ti64)(__ccgo_up(pOffset)) <= (*TPager)(unsafe.Pointer(pPager)).FjournalHdr)
	} else {
		isSynced = libc.BoolInt32(**(**uintptr)(__ccgo_up(bp)) == uintptr(0) || 0 == int32((*TPgHdr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fflags)&int32(PGHDR_NEED_SYNC))
	}
	if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) && (int32((*TPager)(unsafe.Pointer(pPager)).FeState) >= int32(PAGER_WRITER_DBMOD) || int32((*TPager)(unsafe.Pointer(pPager)).FeState) == PAGER_OPEN) && isSynced != 0 {
		ofst = int64(**(**TPgno)(__ccgo_up(bp + 4))-libc.Uint32FromInt32(1)) * (*TPager)(unsafe.Pointer(pPager)).FpageSize
		/* Write the data read from the journal back into the database file.
		 ** This is usually safe even for an encrypted database - as the data
		 ** was encrypted before it was written to the journal file. The exception
		 ** is if the data was just read from an in-memory sub-journal. In that
		 ** case it must be encrypted here before it is copied into the database
		 ** file.  */
		rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, aData, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), ofst)
		if **(**TPgno)(__ccgo_up(bp + 4)) > (*TPager)(unsafe.Pointer(pPager)).FdbFileSize {
			(*TPager)(unsafe.Pointer(pPager)).FdbFileSize = **(**TPgno)(__ccgo_up(bp + 4))
		}
		if (*TPager)(unsafe.Pointer(pPager)).FpBackup != 0 {
			_sqlite3BackupUpdate(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup, **(**TPgno)(__ccgo_up(bp + 4)), aData)
		}
	} else {
		if !(isMainJrnl != 0) && **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
			/* If this is a rollback of a savepoint and data was not written to
			 ** the database and the page is not in-memory, there is a potential
			 ** problem. When the page is next fetched by the b-tree layer, it
			 ** will be read from the database file, which may or may not be
			 ** current.
			 **
			 ** There are a couple of different ways this can happen. All are quite
			 ** obscure. When running in synchronous mode, this can only happen
			 ** if the page is on the free-list at the start of the transaction, then
			 ** populated, then moved using sqlite3PagerMovepage().
			 **
			 ** The solution is to add an in-memory page to the cache containing
			 ** the data just read from the sub-journal. Mark the page as dirty
			 ** and if the pager requires a journal-sync, then mark the page as
			 ** requiring a journal-sync before it is written.
			 */
			v1 = pPager + 21
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(SPILLFLAG_ROLLBACK))
			rc = _sqlite3PagerGet(tls, pPager, **(**TPgno)(__ccgo_up(bp + 4)), bp, int32(1))
			v1 = pPager + 21
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(SPILLFLAG_ROLLBACK))
			if rc != SQLITE_OK {
				return rc
			}
			_sqlite3PcacheMakeDirty(tls, **(**uintptr)(__ccgo_up(bp)))
		}
	}
	if **(**uintptr)(__ccgo_up(bp)) != 0 {
		pData = (*TPgHdr)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpData
		libc.Xmemcpy(tls, pData, aData, uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize))
		(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TPager)(unsafe.Pointer(pPager)).FxReiniter})))(tls, **(**uintptr)(__ccgo_up(bp)))
		/* It used to be that sqlite3PcacheMakeClean(pPg) was called here.  But
		 ** that call was dangerous and had no detectable benefit since the cache
		 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
		 ** has been removed. */
		/* If this was page 1, then restore the value of Pager.dbFileVers.
		 ** Do this before any decoding. */
		if **(**TPgno)(__ccgo_up(bp + 4)) == uint32(1) {
			libc.Xmemcpy(tls, pPager+112, pData+24, uint32(16))
		}
		_sqlite3PcacheRelease(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is used to change the actual size of the database
//	** file in the file-system. This only happens when committing a transaction,
//	** or rolling back a transaction (including rolling back a hot-journal).
//	**
//	** If the main database file is not open, or the pager is not in either
//	** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
//	** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
//	** If the file on disk is currently larger than nPage pages, then use the VFS
//	** xTruncate() method to truncate it.
//	**
//	** Or, it might be the case that the file on disk is smaller than
//	** nPage pages. Some operating system implementations can get confused if
//	** you try to truncate a file to some size that is larger than it
//	** currently is, so detect this case and write a single zero byte to
//	** the end of the new file instead.
//	**
//	** If successful, return SQLITE_OK. If an IO error occurs while modifying
//	** the database file, return the error code to the caller.
//	*/
func _pager_truncate(tls *libc.TLS, pPager uintptr, nPage TPgno) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pTmp uintptr
	var rc, szPage int32
	var _ /* currentSize at bp+0 */ Ti64
	var _ /* newSize at bp+8 */ Ti64
	_, _, _ = pTmp, rc, szPage
	rc = SQLITE_OK
	if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) && (int32((*TPager)(unsafe.Pointer(pPager)).FeState) >= int32(PAGER_WRITER_DBMOD) || int32((*TPager)(unsafe.Pointer(pPager)).FeState) == PAGER_OPEN) {
		szPage = int32((*TPager)(unsafe.Pointer(pPager)).FpageSize)
		/* TODO: Is it safe to use Pager.dbFileSize here? */
		rc = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, bp)
		**(**Ti64)(__ccgo_up(bp + 8)) = int64(szPage) * int64(nPage)
		if rc == SQLITE_OK && **(**Ti64)(__ccgo_up(bp)) != **(**Ti64)(__ccgo_up(bp + 8)) {
			if **(**Ti64)(__ccgo_up(bp)) > **(**Ti64)(__ccgo_up(bp + 8)) {
				rc = _sqlite3OsTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, **(**Ti64)(__ccgo_up(bp + 8)))
			} else {
				if **(**Ti64)(__ccgo_up(bp))+int64(szPage) <= **(**Ti64)(__ccgo_up(bp + 8)) {
					pTmp = (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace
					libc.Xmemset(tls, pTmp, 0, uint32(szPage))
					_sqlite3OsFileControlHint(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_SIZE_HINT), bp+8)
					rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, pTmp, szPage, **(**Ti64)(__ccgo_up(bp + 8))-int64(szPage))
				}
			}
			if rc == SQLITE_OK {
				(*TPager)(unsafe.Pointer(pPager)).FdbFileSize = nPage
			}
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Mark a single data page as writeable. The page is written into the
//	** main journal or sub-journal as required. If the page is written into
//	** one of the journals, the corresponding bit is set in the
//	** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
//	** of any open savepoints as appropriate.
//	*/
func _pager_write(tls *libc.TLS, pPg uintptr) (r int32) {
	var pPager, v1 uintptr
	var rc int32
	_, _, _ = pPager, rc, v1
	pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager
	rc = SQLITE_OK
	/* This routine is not called unless a write-transaction has already
	 ** been started. The journal file may or may not be open at this point.
	 ** It is never called in the ERROR state.
	 */
	/* The journal file needs to be opened. Higher level routines have already
	 ** obtained the necessary locks to begin the write-transaction, but the
	 ** rollback journal might not yet be open. Open it now if this is the case.
	 **
	 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
	 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
	 ** an error might occur and the pager would end up in WRITER_LOCKED state
	 ** with pages marked as dirty in the cache.
	 */
	if int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_WRITER_LOCKED) {
		rc = _pager_open_journal(tls, pPager)
		if rc != SQLITE_OK {
			return rc
		}
	}
	/* Mark the page that is about to be modified as dirty. */
	_sqlite3PcacheMakeDirty(tls, pPg)
	/* If a rollback journal is in use, them make sure the page that is about
	 ** to change is in the rollback journal, or if the page is a new page off
	 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
	 */
	if (*TPager)(unsafe.Pointer(pPager)).FpInJournal != uintptr(0) && _sqlite3BitvecTestNotNull(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) == 0 {
		if (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno <= (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize {
			rc = _pagerAddPageToRollbackJournal(tls, pPg)
			if rc != SQLITE_OK {
				return rc
			}
		} else {
			if int32((*TPager)(unsafe.Pointer(pPager)).FeState) != int32(PAGER_WRITER_DBMOD) {
				v1 = pPg + 28
				*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(PGHDR_NEED_SYNC))
			}
		}
	}
	/* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
	 ** and before writing the page into the rollback journal.  Wait until now,
	 ** after the page has been successfully journalled, before setting the
	 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
	 */
	v1 = pPg + 28
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(PGHDR_WRITEABLE))
	/* If the statement journal is open and the page is not in it,
	 ** then write the page into the statement journal.
	 */
	if (*TPager)(unsafe.Pointer(pPager)).FnSavepoint > 0 {
		rc = _subjournalPageIfRequired(tls, pPg)
	}
	/* Update the database size and return. */
	if (*TPager)(unsafe.Pointer(pPager)).FdbSize < (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno {
		(*TPager)(unsafe.Pointer(pPager)).FdbSize = (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno
	}
	return rc
}

// C documentation
//
//	/*
//	** The argument is the first in a linked list of dirty pages connected
//	** by the PgHdr.pDirty pointer. This function writes each one of the
//	** in-memory pages in the list to the database file. The argument may
//	** be NULL, representing an empty list. In this case this function is
//	** a no-op.
//	**
//	** The pager must hold at least a RESERVED lock when this function
//	** is called. Before writing anything to the database file, this lock
//	** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
//	** SQLITE_BUSY is returned and no data is written to the database file.
//	**
//	** If the pager is a temp-file pager and the actual file-system file
//	** is not yet open, it is created and opened before any data is
//	** written out.
//	**
//	** Once the lock has been upgraded and, if necessary, the file opened,
//	** the pages are written out to the database file in list order. Writing
//	** a page is skipped if it meets either of the following criteria:
//	**
//	**   * The page number is greater than Pager.dbSize, or
//	**   * The PGHDR_DONT_WRITE flag is set on the page.
//	**
//	** If writing out a page causes the database file to grow, Pager.dbFileSize
//	** is updated accordingly. If page 1 is written out, then the value cached
//	** in Pager.dbFileVers[] is updated to match the new value stored in
//	** the database file.
//	**
//	** If everything is successful, SQLITE_OK is returned. If an IO error
//	** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
//	** be obtained, SQLITE_BUSY is returned.
//	*/
func _pager_write_pagelist(tls *libc.TLS, pPager uintptr, pList uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var offset Ti64
	var pData uintptr
	var pgno TPgno
	var rc int32
	var _ /* szFile at bp+0 */ Tsqlite3_int64
	_, _, _, _ = offset, pData, pgno, rc
	rc = SQLITE_OK /* Return code */
	/* This function is only called for rollback pagers in WRITER_DBMOD state. */
	/* If the file is a temp-file has not yet been opened, open it now. It
	 ** is not possible for rc to be other than SQLITE_OK if this branch
	 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
	 */
	if !((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != libc.UintptrFromInt32(0)) {
		rc = _pagerOpentemp(tls, pPager, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32((*TPager)(unsafe.Pointer(pPager)).FvfsFlags))
	}
	/* Before the first write, give the VFS a hint of what the final
	 ** file size will be.
	 */
	if rc == SQLITE_OK && (*TPager)(unsafe.Pointer(pPager)).FdbHintSize < (*TPager)(unsafe.Pointer(pPager)).FdbSize && ((*TPgHdr)(unsafe.Pointer(pList)).FpDirty != 0 || (*TPgHdr)(unsafe.Pointer(pList)).Fpgno > (*TPager)(unsafe.Pointer(pPager)).FdbHintSize) {
		**(**Tsqlite3_int64)(__ccgo_up(bp)) = (*TPager)(unsafe.Pointer(pPager)).FpageSize * int64((*TPager)(unsafe.Pointer(pPager)).FdbSize)
		_sqlite3OsFileControlHint(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_SIZE_HINT), bp)
		(*TPager)(unsafe.Pointer(pPager)).FdbHintSize = (*TPager)(unsafe.Pointer(pPager)).FdbSize
	}
	for rc == SQLITE_OK && pList != 0 {
		pgno = (*TPgHdr)(unsafe.Pointer(pList)).Fpgno
		/* If there are dirty pages in the page cache with page numbers greater
		 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
		 ** make the file smaller (presumably by auto-vacuum code). Do not write
		 ** any such pages to the file.
		 **
		 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
		 ** set (set by sqlite3PagerDontWrite()).
		 */
		if pgno <= (*TPager)(unsafe.Pointer(pPager)).FdbSize && 0 == int32((*TPgHdr)(unsafe.Pointer(pList)).Fflags)&int32(PGHDR_DONT_WRITE) {
			offset = int64(pgno-libc.Uint32FromInt32(1)) * (*TPager)(unsafe.Pointer(pPager)).FpageSize /* Data to write */
			if (*TPgHdr)(unsafe.Pointer(pList)).Fpgno == uint32(1) {
				_pager_write_changecounter(tls, pList)
			}
			pData = (*TPgHdr)(unsafe.Pointer(pList)).FpData
			/* Write out the page data. */
			rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, pData, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), offset)
			/* If page 1 was just written, update Pager.dbFileVers to match
			 ** the value now stored in the database file. If writing this
			 ** page caused the database file to grow, update dbFileSize.
			 */
			if pgno == uint32(1) {
				libc.Xmemcpy(tls, pPager+112, pData+24, uint32(16))
			}
			if pgno > (*TPager)(unsafe.Pointer(pPager)).FdbFileSize {
				(*TPager)(unsafe.Pointer(pPager)).FdbFileSize = pgno
			}
			**(**Tu32)(__ccgo_up(pPager + 200 + 2*4)) = **(**Tu32)(__ccgo_up(pPager + 200 + 2*4)) + 1
			/* Update any backup objects copying the contents of this pager. */
			_sqlite3BackupUpdate(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup, pgno, (*TPgHdr)(unsafe.Pointer(pList)).FpData)
		} else {
		}
		pList = (*TPgHdr)(unsafe.Pointer(pList)).FpDirty
	}
	return rc
}

func _parseGrowPhraseArray(tls *libc.TLS, pParse uintptr) (r int32) {
	var apNew uintptr
	var nByte Tsqlite3_int64
	_, _ = apNew, nByte
	if (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase%int32(8) == 0 {
		nByte = int64(uint32(4) * uint32((*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase+libc.Int32FromInt32(8)))
		apNew = Xsqlite3_realloc64(tls, (*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase, uint64(nByte))
		if apNew == uintptr(0) {
			(*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM)
			return int32(SQLITE_NOMEM)
		}
		(*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase = apNew
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Process a modifier to a date-time stamp.  The modifiers are
//	** as follows:
//	**
//	**     NNN days
//	**     NNN hours
//	**     NNN minutes
//	**     NNN.NNNN seconds
//	**     NNN months
//	**     NNN years
//	**     +/-YYYY-MM-DD HH:MM:SS.SSS
//	**     ceiling
//	**     floor
//	**     start of month
//	**     start of year
//	**     start of week
//	**     start of day
//	**     weekday N
//	**     unixepoch
//	**     auto
//	**     localtime
//	**     utc
//	**     subsec
//	**     subsecond
//	**
//	** Return 0 on success and 1 if there is any kind of error. If the error
//	** is in a system call (i.e. localtime()), then an error message is written
//	** to context pCtx. If the error is an unrecognized modifier, no error is
//	** written to pCtx.
//	*/
func _parseModifier(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, p uintptr, idx int32) (r int32) {
	bp := tls.Alloc(160)
	defer tls.Free(160)
	var Z, day Tsqlite3_int64
	var cnt, i, rc, rx, x, y, v1 int32
	var db, z2, zCopy uintptr
	var iErr, iGuess, iOrigJD, v2 Ti64
	var rRounder, v11 float64
	var z0 int8
	var v4 bool
	var _ /* D at bp+64 */ int32
	var _ /* M at bp+60 */ int32
	var _ /* Y at bp+56 */ int32
	var _ /* h at bp+68 */ int32
	var _ /* m at bp+72 */ int32
	var _ /* new at bp+8 */ TDateTime
	var _ /* r at bp+0 */ float64
	var _ /* tx at bp+80 */ TDateTime
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = Z, cnt, day, db, i, iErr, iGuess, iOrigJD, rRounder, rc, rx, x, y, z0, z2, zCopy, v1, v11, v2, v4
	rc = int32(1)
	switch int32(_sqlite3UpperToLower[uint8(**(**int8)(__ccgo_up(z)))]) {
	case int32('a'):
		/*
		 **    auto
		 **
		 ** If rawS is available, then interpret as a julian day number, or
		 ** a unix timestamp, depending on its magnitude.
		 */
		if Xsqlite3_stricmp(tls, z, __ccgo_ts+1279) == 0 {
			if idx > int32(1) {
				return int32(1)
			} /* IMP: R-33611-57934 */
			_autoAdjustDate(tls, p)
			rc = 0
		}
	case int32('c'):
		/*
		 **    ceiling
		 **
		 ** Resolve day-of-month overflow by rolling forward into the next
		 ** month.  As this is the default action, this modifier is really
		 ** a no-op that is only included for symmetry.  See "floor".
		 */
		if Xsqlite3_stricmp(tls, z, __ccgo_ts+1284) == 0 {
			_computeJD(tls, p)
			_clearYMD_HMS_TZ(tls, p)
			rc = 0
			(*TDateTime)(unsafe.Pointer(p)).FnFloor = 0
		}
	case int32('f'):
		/*
		 **    floor
		 **
		 ** Resolve day-of-month overflow by rolling back to the end of the
		 ** previous month.
		 */
		if Xsqlite3_stricmp(tls, z, __ccgo_ts+1292) == 0 {
			_computeJD(tls, p)
			**(**Tsqlite3_int64)(__ccgo_up(p)) -= int64(int32((*TDateTime)(unsafe.Pointer(p)).FnFloor) * int32(86400000))
			_clearYMD_HMS_TZ(tls, p)
			rc = 0
		}
	case int32('j'):
		/*
		 **    julianday
		 **
		 ** Always interpret the prior number as a julian-day value.  If this
		 ** is not the first modifier, or if the prior argument is not a numeric
		 ** value in the allowed range of julian day numbers understood by
		 ** SQLite (0..5373484.5) then the result will be NULL.
		 */
		if Xsqlite3_stricmp(tls, z, __ccgo_ts+1298) == 0 {
			if idx > int32(1) {
				return int32(1)
			} /* IMP: R-31176-64601 */
			if (*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0 && int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x1>>0)) != 0 {
				rc = 0
				libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1)
			}
		}
	case int32('l'):
		/*    localtime
		 **
		 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
		 ** show local time.
		 */
		if Xsqlite3_stricmp(tls, z, __ccgo_ts+1308) == 0 && _sqlite3NotPureFunc(tls, pCtx) != 0 {
			if int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x10>>4)) != 0 {
				v1 = SQLITE_OK
			} else {
				v1 = _toLocaltime(tls, p, pCtx)
			}
			rc = v1
			libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 3, 0x8)
			libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 4, 0x10)
		}
	case int32('u'):
		/*
		 **    unixepoch
		 **
		 ** Treat the current value of p->s as the number of
		 ** seconds since 1970.  Convert to a real julian day number.
		 */
		if Xsqlite3_stricmp(tls, z, __ccgo_ts+1318) == 0 && int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x1>>0)) != 0 {
			if idx > int32(1) {
				return int32(1)
			} /* IMP: R-49255-55373 */
			**(**float64)(__ccgo_up(bp)) = float64((*TDateTime)(unsafe.Pointer(p)).Fs*float64(1000)) + float64(2.1086676e+14)
			if **(**float64)(__ccgo_up(bp)) >= float64(0) && **(**float64)(__ccgo_up(bp)) < float64(4.642690608e+14) {
				_clearYMD_HMS_TZ(tls, p)
				(*TDateTime)(unsafe.Pointer(p)).FiJD = int64(**(**float64)(__ccgo_up(bp)) + libc.Float64FromFloat64(0.5))
				(*TDateTime)(unsafe.Pointer(p)).FvalidJD = int8(1)
				libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1)
				rc = 0
			}
		} else {
			if Xsqlite3_stricmp(tls, z, __ccgo_ts+1328) == 0 && _sqlite3NotPureFunc(tls, pCtx) != 0 {
				if int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x8>>3)) == 0 { /* Guess at the corresponding utc time */
					cnt = 0 /* Guess is off by this much */
					_computeJD(tls, p)
					v2 = (*TDateTime)(unsafe.Pointer(p)).FiJD
					iOrigJD = v2
					iGuess = v2
					iErr = 0
					for {
						libc.Xmemset(tls, bp+8, 0, uint32(48))
						iGuess = iGuess - iErr
						(**(**TDateTime)(__ccgo_up(bp + 8))).FiJD = iGuess
						(**(**TDateTime)(__ccgo_up(bp + 8))).FvalidJD = int8(1)
						rc = _toLocaltime(tls, bp+8, pCtx)
						if rc != 0 {
							return rc
						}
						_computeJD(tls, bp+8)
						iErr = (**(**TDateTime)(__ccgo_up(bp + 8))).FiJD - iOrigJD
						goto _5
					_5:
						;
						if v4 = iErr != 0; v4 {
							v1 = cnt
							cnt = cnt + 1
						}
						if !(v4 && v1 < int32(3)) {
							break
						}
					}
					libc.Xmemset(tls, p, 0, uint32(48))
					(*TDateTime)(unsafe.Pointer(p)).FiJD = iGuess
					(*TDateTime)(unsafe.Pointer(p)).FvalidJD = int8(1)
					libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 3, 0x8)
					libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 4, 0x10)
				}
				rc = SQLITE_OK
			}
		}
	case int32('w'):
		/*
		 **    weekday N
		 **
		 ** Move the date to the same time on the next occurrence of
		 ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
		 ** date is already on the appropriate weekday, this is a no-op.
		 */
		if v4 = Xsqlite3_strnicmp(tls, z, __ccgo_ts+1332, int32(8)) == 0 && _sqlite3AtoF(tls, z+8, bp) > 0 && **(**float64)(__ccgo_up(bp)) >= float64(0) && **(**float64)(__ccgo_up(bp)) < float64(7); v4 {
			v1 = int32(**(**float64)(__ccgo_up(bp)))
			n = v1
		}
		if v4 && float64(v1) == **(**float64)(__ccgo_up(bp)) {
			_computeYMD_HMS(tls, p)
			(*TDateTime)(unsafe.Pointer(p)).Ftz = 0
			(*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0
			_computeJD(tls, p)
			Z = ((*TDateTime)(unsafe.Pointer(p)).FiJD + int64(129600000)) / int64(86400000) % int64(7)
			if Z > int64(n) {
				Z = Z - int64(7)
			}
			**(**Tsqlite3_int64)(__ccgo_up(p)) += (int64(n) - Z) * int64(86400000)
			_clearYMD_HMS_TZ(tls, p)
			rc = 0
		}
	case int32('s'):
		/*
		 **    start of TTTTT
		 **
		 ** Move the date backwards to the beginning of the current day,
		 ** or month or year.
		 **
		 **    subsecond
		 **    subsec
		 **
		 ** Show subsecond precision in the output of datetime() and
		 ** unixepoch() and strftime('%s').
		 */
		if Xsqlite3_strnicmp(tls, z, __ccgo_ts+1341, int32(9)) != 0 {
			if Xsqlite3_stricmp(tls, z, __ccgo_ts+1239) == 0 || Xsqlite3_stricmp(tls, z, __ccgo_ts+1246) == 0 {
				libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 2, 0x4)
				rc = 0
			}
			break
		}
		if !((*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0) && !((*TDateTime)(unsafe.Pointer(p)).FvalidYMD != 0) && !((*TDateTime)(unsafe.Pointer(p)).FvalidHMS != 0) {
			break
		}
		z = z + uintptr(9)
		_computeYMD(tls, p)
		(*TDateTime)(unsafe.Pointer(p)).FvalidHMS = int8(1)
		v1 = libc.Int32FromInt32(0)
		(*TDateTime)(unsafe.Pointer(p)).Fm = v1
		(*TDateTime)(unsafe.Pointer(p)).Fh = v1
		(*TDateTime)(unsafe.Pointer(p)).Fs = float64(0)
		libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1)
		(*TDateTime)(unsafe.Pointer(p)).Ftz = 0
		(*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0
		if Xsqlite3_stricmp(tls, z, __ccgo_ts+1351) == 0 {
			(*TDateTime)(unsafe.Pointer(p)).FD = int32(1)
			rc = 0
		} else {
			if Xsqlite3_stricmp(tls, z, __ccgo_ts+1357) == 0 {
				(*TDateTime)(unsafe.Pointer(p)).FM = int32(1)
				(*TDateTime)(unsafe.Pointer(p)).FD = int32(1)
				rc = 0
			} else {
				if Xsqlite3_stricmp(tls, z, __ccgo_ts+1362) == 0 {
					rc = 0
				}
			}
		}
	case int32('+'):
		fallthrough
	case int32('-'):
		fallthrough
	case int32('0'):
		fallthrough
	case int32('1'):
		fallthrough
	case int32('2'):
		fallthrough
	case int32('3'):
		fallthrough
	case int32('4'):
		fallthrough
	case int32('5'):
		fallthrough
	case int32('6'):
		fallthrough
	case int32('7'):
		fallthrough
	case int32('8'):
		fallthrough
	case int32('9'):
		z2 = z
		db = Xsqlite3_context_db_handle(tls, pCtx)
		z0 = **(**int8)(__ccgo_up(z))
		n = int32(1)
		for {
			if !(**(**int8)(__ccgo_up(z + uintptr(n))) != 0) {
				break
			}
			if int32(**(**int8)(__ccgo_up(z + uintptr(n)))) == int32(':') {
				break
			}
			if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(n))))])&int32(0x01) != 0 {
				break
			}
			if int32(**(**int8)(__ccgo_up(z + uintptr(n)))) == int32('-') {
				if n == int32(5) && _getDigits(tls, z+1, __ccgo_ts+1366, libc.VaList(bp+136, bp+56)) == int32(1) {
					break
				}
				if n == int32(6) && _getDigits(tls, z+1, __ccgo_ts+1370, libc.VaList(bp+136, bp+56)) == int32(1) {
					break
				}
			}
			goto _9
		_9:
			;
			n = n + 1
		}
		zCopy = _sqlite3DbStrNDup(tls, db, z, uint64(n))
		if zCopy == uintptr(0) {
			break
		}
		rx = libc.BoolInt32(_sqlite3AtoF(tls, zCopy, bp) <= 0)
		_sqlite3DbFree(tls, db, zCopy)
		if rx != 0 {
			break
		}
		if int32(**(**int8)(__ccgo_up(z + uintptr(n)))) == int32('-') {
			/* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the
			 ** specified number of years, months, and days.  MM is limited to
			 ** the range 0-11 and DD is limited to 0-30.
			 */
			if int32(z0) != int32('+') && int32(z0) != int32('-') {
				break
			} /* Must start with +/- */
			if n == int32(5) {
				if _getDigits(tls, z+1, __ccgo_ts+1374, libc.VaList(bp+136, bp+56, bp+60, bp+64)) != int32(3) {
					break
				}
			} else {
				if _getDigits(tls, z+1, __ccgo_ts+1386, libc.VaList(bp+136, bp+56, bp+60, bp+64)) != int32(3) {
					break
				}
				z = z + 1
			}
			if **(**int32)(__ccgo_up(bp + 60)) >= int32(12) {
				break
			} /* M range 0..11 */
			if **(**int32)(__ccgo_up(bp + 64)) >= int32(31) {
				break
			} /* D range 0..30 */
			_computeYMD_HMS(tls, p)
			(*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0
			if int32(z0) == int32('-') {
				**(**int32)(__ccgo_up(p + 8)) -= **(**int32)(__ccgo_up(bp + 56))
				**(**int32)(__ccgo_up(p + 12)) -= **(**int32)(__ccgo_up(bp + 60))
				**(**int32)(__ccgo_up(bp + 64)) = -**(**int32)(__ccgo_up(bp + 64))
			} else {
				**(**int32)(__ccgo_up(p + 8)) += **(**int32)(__ccgo_up(bp + 56))
				**(**int32)(__ccgo_up(p + 12)) += **(**int32)(__ccgo_up(bp + 60))
			}
			if (*TDateTime)(unsafe.Pointer(p)).FM > 0 {
				v1 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(1)) / int32(12)
			} else {
				v1 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(12)) / int32(12)
			}
			x = v1
			**(**int32)(__ccgo_up(p + 8)) += x
			**(**int32)(__ccgo_up(p + 12)) -= x * int32(12)
			_computeFloor(tls, p)
			_computeJD(tls, p)
			(*TDateTime)(unsafe.Pointer(p)).FvalidHMS = 0
			(*TDateTime)(unsafe.Pointer(p)).FvalidYMD = 0
			**(**Tsqlite3_int64)(__ccgo_up(p)) += int64(**(**int32)(__ccgo_up(bp + 64))) * int64(86400000)
			if int32(**(**int8)(__ccgo_up(z + 11))) == 0 {
				rc = 0
				break
			}
			if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + 11)))])&int32(0x01) != 0 && _getDigits(tls, z+12, __ccgo_ts+1211, libc.VaList(bp+136, bp+68, bp+72)) == int32(2) {
				z2 = z + 12
				n = int32(2)
			} else {
				break
			}
		}
		if int32(**(**int8)(__ccgo_up(z2 + uintptr(n)))) == int32(':') {
			if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z2)))])&libc.Int32FromInt32(0x04) != 0) {
				z2 = z2 + 1
			}
			libc.Xmemset(tls, bp+80, 0, uint32(48))
			if _parseHhMmSs(tls, z2, bp+80) != 0 {
				break
			}
			_computeJD(tls, bp+80)
			(**(**TDateTime)(__ccgo_up(bp + 80))).FiJD -= int64(43200000)
			day = (**(**TDateTime)(__ccgo_up(bp + 80))).FiJD / int64(86400000)
			(**(**TDateTime)(__ccgo_up(bp + 80))).FiJD -= day * int64(86400000)
			if int32(z0) == int32('-') {
				(**(**TDateTime)(__ccgo_up(bp + 80))).FiJD = -(**(**TDateTime)(__ccgo_up(bp + 80))).FiJD
			}
			_computeJD(tls, p)
			_clearYMD_HMS_TZ(tls, p)
			**(**Tsqlite3_int64)(__ccgo_up(p)) += (**(**TDateTime)(__ccgo_up(bp + 80))).FiJD
			rc = 0
			break
		}
		/* If control reaches this point, it means the transformation is
		 ** one of the forms like "+NNN days".  */
		z = z + uintptr(n)
		for int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z)))])&int32(0x01) != 0 {
			z = z + 1
		}
		n = _sqlite3Strlen30(tls, z)
		if n < int32(3) || n > int32(10) {
			break
		}
		if int32(_sqlite3UpperToLower[uint8(**(**int8)(__ccgo_up(z + uintptr(n-int32(1)))))]) == int32('s') {
			n = n - 1
		}
		_computeJD(tls, p)
		if **(**float64)(__ccgo_up(bp)) < libc.Float64FromInt32(0) {
			v11 = -libc.Float64FromFloat64(0.5)
		} else {
			v11 = +libc.Float64FromFloat64(0.5)
		}
		rRounder = v11
		(*TDateTime)(unsafe.Pointer(p)).FnFloor = 0
		i = 0
		for {
			if !(i < int32(libc.Uint32FromInt64(96)/libc.Uint32FromInt64(16))) {
				break
			}
			if int32(_aXformType[i].FnName) == n && Xsqlite3_strnicmp(tls, uintptr(unsafe.Pointer(&_aXformType))+uintptr(i)*16+1, z, n) == 0 && **(**float64)(__ccgo_up(bp)) > float64(-_aXformType[i].FrLimit) && **(**float64)(__ccgo_up(bp)) < float64(_aXformType[i].FrLimit) {
				switch i {
				case int32(4): /* Special processing to add months */
					_computeYMD_HMS(tls, p)
					**(**int32)(__ccgo_up(p + 12)) += int32(**(**float64)(__ccgo_up(bp)))
					if (*TDateTime)(unsafe.Pointer(p)).FM > 0 {
						v1 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(1)) / int32(12)
					} else {
						v1 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(12)) / int32(12)
					}
					x = v1
					**(**int32)(__ccgo_up(p + 8)) += x
					**(**int32)(__ccgo_up(p + 12)) -= x * int32(12)
					_computeFloor(tls, p)
					(*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0
					**(**float64)(__ccgo_up(bp)) = **(**float64)(__ccgo_up(bp)) - float64(int32(**(**float64)(__ccgo_up(bp))))
				case int32(5): /* Special processing to add years */
					y = int32(**(**float64)(__ccgo_up(bp)))
					_computeYMD_HMS(tls, p)
					**(**int32)(__ccgo_up(p + 8)) += y
					_computeFloor(tls, p)
					(*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0
					**(**float64)(__ccgo_up(bp)) = **(**float64)(__ccgo_up(bp)) - float64(int32(**(**float64)(__ccgo_up(bp))))
					break
				}
				_computeJD(tls, p)
				**(**Tsqlite3_int64)(__ccgo_up(p)) += int64(float64(float64(**(**float64)(__ccgo_up(bp))*libc.Float64FromFloat64(1000))*float64(_aXformType[i].FrXform)) + rRounder)
				rc = 0
				break
			}
			goto _12
		_12:
			;
			i = i + 1
		}
		_clearYMD_HMS_TZ(tls, p)
	default:
		break
	}
	return rc
}

// C documentation
//
//	/*
//	  ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
//	  ** all elements in the list.  And make sure list length does not exceed
//	  ** SQLITE_LIMIT_COMPOUND_SELECT.
//	  */
func _parserDoubleLinkSelect(tls *libc.TLS, pParse uintptr, p uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var cnt, mxSelect, v2 int32
	var pLoop, pNext, v1 uintptr
	var v3 bool
	_, _, _, _, _, _, _ = cnt, mxSelect, pLoop, pNext, v1, v2, v3
	if (*TSelect)(unsafe.Pointer(p)).FpPrior != 0 {
		pNext = uintptr(0)
		pLoop = p
		cnt = int32(1)
		for int32(1) != 0 {
			(*TSelect)(unsafe.Pointer(pLoop)).FpNext = pNext
			**(**Tu32)(__ccgo_up(pLoop + 4)) |= uint32(SF_Compound)
			pNext = pLoop
			pLoop = (*TSelect)(unsafe.Pointer(pLoop)).FpPrior
			if pLoop == uintptr(0) {
				break
			}
			cnt = cnt + 1
			if (*TSelect)(unsafe.Pointer(pLoop)).FpOrderBy != 0 || (*TSelect)(unsafe.Pointer(pLoop)).FpLimit != 0 {
				if (*TSelect)(unsafe.Pointer(pLoop)).FpOrderBy != uintptr(0) {
					v1 = __ccgo_ts + 26084
				} else {
					v1 = __ccgo_ts + 26093
				}
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+26099, libc.VaList(bp+8, v1, _sqlite3SelectOpName(tls, int32((*TSelect)(unsafe.Pointer(pNext)).Fop))))
				break
			}
		}
		if v3 = (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_MultiValue)|libc.Int32FromInt32(SF_Values)) == uint32(0); v3 {
			v2 = **(**int32)(__ccgo_up((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 4*4))
			mxSelect = v2
		}
		if v3 && v2 > 0 && cnt > mxSelect {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+26141, 0)
		}
	}
}

// C documentation
//
//	/*
//	** Compare two UTF-8 strings for equality where the first string is
//	** a GLOB or LIKE expression.  Return values:
//	**
//	**    SQLITE_MATCH:            Match
//	**    SQLITE_NOMATCH:          No match
//	**    SQLITE_NOWILDCARDMATCH:  No match in spite of having * or % wildcards.
//	**
//	** Globbing rules:
//	**
//	**      '*'       Matches any sequence of zero or more characters.
//	**
//	**      '?'       Matches exactly one character.
//	**
//	**     [...]      Matches one character from the enclosed list of
//	**                characters.
//	**
//	**     [^...]     Matches one character not in the enclosed list.
//	**
//	** With the [...] and [^...] matching, a ']' character can be included
//	** in the list by making it the first character after '[' or '^'.  A
//	** range of characters can be specified using '-'.  Example:
//	** "[a-z]" matches any single lower-case letter.  To match a '-', make
//	** it the last character in the list.
//	**
//	** Like matching rules:
//	**
//	**      '%'       Matches any sequence of zero or more characters
//	**
//	***     '_'       Matches any one character
//	**
//	**      Ec        Where E is the "esc" character and c is any other
//	**                character, including '%', '_', and esc, match exactly c.
//	**
//	** The comments within this routine usually assume glob matching.
//	**
//	** This routine is usually quick, but can be N**2 in the worst case.
//	*/
func _patternCompare(tls *libc.TLS, _zPattern uintptr, _zString uintptr, pInfo uintptr, matchOther Tu32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*uintptr)(unsafe.Pointer(bp)) = _zPattern
	*(*uintptr)(unsafe.Pointer(bp + 4)) = _zString
	var bMatch, bMatch1, bMatch2, invert, seen, v13 int32
	var c, c2, matchAll, matchOne, prior_c, v1, v4 Tu32
	var noCase Tu8
	var zEscaped, v3, v6 uintptr
	var v2, v5 uint32
	var _ /* zStop at bp+9 */ [3]int8
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bMatch, bMatch1, bMatch2, c, c2, invert, matchAll, matchOne, noCase, prior_c, seen, zEscaped, v1, v13, v2, v3, v4, v5, v6 /* Next pattern and input string chars */
	matchOne = uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchOne)                                                                                                                 /* "?" or "_" */
	matchAll = uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchAll)                                                                                                                 /* "*" or "%" */
	noCase = (*TcompareInfo)(unsafe.Pointer(pInfo)).FnoCase                                                                                                                             /* True if uppercase==lowercase */
	zEscaped = uintptr(0)                                                                                                                                                               /* One past the last escaped input char */
	for {
		if int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))) < int32(0x80) {
			v3 = **(**uintptr)(__ccgo_up(bp))
			**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
			v2 = uint32(**(**Tu8)(__ccgo_up(v3)))
		} else {
			v2 = _sqlite3Utf8Read(tls, bp)
		}
		v1 = v2
		c = v1
		if !(v1 != uint32(0)) {
			break
		}
		if c == matchAll { /* Match "*" */
			/* Skip over multiple "*" characters in the pattern.  If there
			 ** are also "?" characters, skip those as well, but consume a
			 ** single character of the input string for each "?" skipped */
			for {
				if int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))) < int32(0x80) {
					v6 = **(**uintptr)(__ccgo_up(bp))
					**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
					v5 = uint32(**(**Tu8)(__ccgo_up(v6)))
				} else {
					v5 = _sqlite3Utf8Read(tls, bp)
				}
				v4 = v5
				c = v4
				if !(v4 == matchAll || c == matchOne && matchOne != uint32(0)) {
					break
				}
				if c == matchOne && _sqlite3Utf8Read(tls, bp+4) == uint32(0) {
					return int32(SQLITE_NOWILDCARDMATCH)
				}
			}
			if c == uint32(0) {
				return SQLITE_MATCH /* "*" at the end of the pattern matches */
			} else {
				if c == matchOther {
					if int32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchSet) == 0 {
						c = _sqlite3Utf8Read(tls, bp)
						if c == uint32(0) {
							return int32(SQLITE_NOWILDCARDMATCH)
						}
					} else {
						/* "[...]" immediately follows the "*".  We have to do a slow
						 ** recursive search in this case, but it is an unusual case. */
						/* '[' is a single-byte character */
						for **(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4)))) != 0 {
							bMatch = _patternCompare(tls, **(**uintptr)(__ccgo_up(bp))+uintptr(-libc.Int32FromInt32(1)), **(**uintptr)(__ccgo_up(bp + 4)), pInfo, matchOther)
							if bMatch != int32(SQLITE_NOMATCH) {
								return bMatch
							}
							v3 = **(**uintptr)(__ccgo_up(bp + 4))
							**(**uintptr)(__ccgo_up(bp + 4)) = **(**uintptr)(__ccgo_up(bp + 4)) + 1
							if int32(**(**Tu8)(__ccgo_up(v3))) >= int32(0xc0) {
								for int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4)))))&int32(0xc0) == int32(0x80) {
									**(**uintptr)(__ccgo_up(bp + 4)) = **(**uintptr)(__ccgo_up(bp + 4)) + 1
								}
							}
						}
						return int32(SQLITE_NOWILDCARDMATCH)
					}
				}
			}
			/* At this point variable c contains the first character of the
			 ** pattern string past the "*".  Search in the input string for the
			 ** first matching character and recursively continue the match from
			 ** that point.
			 **
			 ** For a case-insensitive search, set variable cx to be the same as
			 ** c but in the other case and search the input string for either
			 ** c or cx.
			 */
			if c < uint32(0x80) {
				if noCase != 0 {
					(**(**[3]int8)(__ccgo_up(bp + 9)))[0] = int8(c & uint32(^(int32(_sqlite3CtypeMap[uint8(c)]) & libc.Int32FromInt32(0x20))))
					(**(**[3]int8)(__ccgo_up(bp + 9)))[int32(1)] = int8(_sqlite3UpperToLower[uint8(c)])
					(**(**[3]int8)(__ccgo_up(bp + 9)))[int32(2)] = 0
				} else {
					(**(**[3]int8)(__ccgo_up(bp + 9)))[0] = int8(c)
					(**(**[3]int8)(__ccgo_up(bp + 9)))[int32(1)] = 0
				}
				for int32(1) != 0 {
					**(**uintptr)(__ccgo_up(bp + 4)) = **(**uintptr)(__ccgo_up(bp + 4)) + uintptr(libc.Xstrcspn(tls, **(**uintptr)(__ccgo_up(bp + 4)), bp+9))
					if int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4))))) == 0 {
						break
					}
					**(**uintptr)(__ccgo_up(bp + 4)) = **(**uintptr)(__ccgo_up(bp + 4)) + 1
					bMatch1 = _patternCompare(tls, **(**uintptr)(__ccgo_up(bp)), **(**uintptr)(__ccgo_up(bp + 4)), pInfo, matchOther)
					if bMatch1 != int32(SQLITE_NOMATCH) {
						return bMatch1
					}
				}
			} else {
				for {
					if int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4))))) < int32(0x80) {
						v3 = **(**uintptr)(__ccgo_up(bp + 4))
						**(**uintptr)(__ccgo_up(bp + 4)) = **(**uintptr)(__ccgo_up(bp + 4)) + 1
						v2 = uint32(**(**Tu8)(__ccgo_up(v3)))
					} else {
						v2 = _sqlite3Utf8Read(tls, bp+4)
					}
					v1 = v2
					c2 = v1
					if !(v1 != uint32(0)) {
						break
					}
					if c2 != c {
						continue
					}
					bMatch2 = _patternCompare(tls, **(**uintptr)(__ccgo_up(bp)), **(**uintptr)(__ccgo_up(bp + 4)), pInfo, matchOther)
					if bMatch2 != int32(SQLITE_NOMATCH) {
						return bMatch2
					}
				}
			}
			return int32(SQLITE_NOWILDCARDMATCH)
		}
		if c == matchOther {
			if int32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchSet) == 0 {
				c = _sqlite3Utf8Read(tls, bp)
				if c == uint32(0) {
					return int32(SQLITE_NOMATCH)
				}
				zEscaped = **(**uintptr)(__ccgo_up(bp))
			} else {
				prior_c = uint32(0)
				seen = 0
				invert = 0
				c = _sqlite3Utf8Read(tls, bp+4)
				if c == uint32(0) {
					return int32(SQLITE_NOMATCH)
				}
				c2 = _sqlite3Utf8Read(tls, bp)
				if c2 == uint32('^') {
					invert = int32(1)
					c2 = _sqlite3Utf8Read(tls, bp)
				}
				if c2 == uint32(']') {
					if c == uint32(']') {
						seen = int32(1)
					}
					c2 = _sqlite3Utf8Read(tls, bp)
				}
				for c2 != 0 && c2 != uint32(']') {
					if c2 == uint32('-') && int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))) != int32(']') && int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))) != 0 && prior_c > uint32(0) {
						c2 = _sqlite3Utf8Read(tls, bp)
						if c >= prior_c && c <= c2 {
							seen = int32(1)
						}
						prior_c = uint32(0)
					} else {
						if c == c2 {
							seen = int32(1)
						}
						prior_c = c2
					}
					c2 = _sqlite3Utf8Read(tls, bp)
				}
				if c2 == uint32(0) || seen^invert == 0 {
					return int32(SQLITE_NOMATCH)
				}
				continue
			}
		}
		if int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4))))) < int32(0x80) {
			v3 = **(**uintptr)(__ccgo_up(bp + 4))
			**(**uintptr)(__ccgo_up(bp + 4)) = **(**uintptr)(__ccgo_up(bp + 4)) + 1
			v2 = uint32(**(**Tu8)(__ccgo_up(v3)))
		} else {
			v2 = _sqlite3Utf8Read(tls, bp+4)
		}
		c2 = v2
		if c == c2 {
			continue
		}
		if noCase != 0 && int32(_sqlite3UpperToLower[uint8(c)]) == int32(_sqlite3UpperToLower[uint8(c2)]) && c < uint32(0x80) && c2 < uint32(0x80) {
			continue
		}
		if c == matchOne && **(**uintptr)(__ccgo_up(bp)) != zEscaped && c2 != uint32(0) {
			continue
		}
		return int32(SQLITE_NOMATCH)
	}
	if int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 4))))) == 0 {
		v13 = SQLITE_MATCH
	} else {
		v13 = int32(SQLITE_NOMATCH)
	}
	return v13
}

// C documentation
//
//	/*
//	** Malloc function used within this file to allocate space from the buffer
//	** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
//	** such buffer exists or there is no space left in it, this function falls
//	** back to sqlite3Malloc().
//	**
//	** Multiple threads can run this routine at the same time.  Global variables
//	** in pcache1 need to be protected via mutex.
//	*/
func _pcache1Alloc(tls *libc.TLS, nByte int32) (r uintptr) {
	var p uintptr
	var sz int32
	_, _ = p, sz
	p = uintptr(0)
	if nByte <= _pcache1_g.FszSlot {
		Xsqlite3_mutex_enter(tls, _pcache1_g.Fmutex)
		p = _pcache1_g.FpFree
		if p != 0 {
			_pcache1_g.FpFree = (*TPgFreeslot)(unsafe.Pointer(_pcache1_g.FpFree)).FpNext
			_pcache1_g.FnFreeSlot = _pcache1_g.FnFreeSlot - 1
			libc.AtomicStoreNInt32(uintptr(unsafe.Pointer(&_pcache1_g))+96, libc.BoolInt32(_pcache1_g.FnFreeSlot < _pcache1_g.FnReserve), libc.Int32FromInt32(__ATOMIC_RELAXED))
			_sqlite3StatusHighwater(tls, int32(SQLITE_STATUS_PAGECACHE_SIZE), nByte)
			_sqlite3StatusUp(tls, int32(SQLITE_STATUS_PAGECACHE_USED), int32(1))
		}
		Xsqlite3_mutex_leave(tls, _pcache1_g.Fmutex)
	}
	if p == uintptr(0) {
		/* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
		 ** it from sqlite3Malloc instead.
		 */
		p = _sqlite3Malloc(tls, uint64(nByte))
		if p != 0 {
			sz = _sqlite3MallocSize(tls, p)
			Xsqlite3_mutex_enter(tls, _pcache1_g.Fmutex)
			_sqlite3StatusHighwater(tls, int32(SQLITE_STATUS_PAGECACHE_SIZE), nByte)
			_sqlite3StatusUp(tls, int32(SQLITE_STATUS_PAGECACHE_OVERFLOW), sz)
			Xsqlite3_mutex_leave(tls, _pcache1_g.Fmutex)
		}
	}
	return p
}

// C documentation
//
//	/*
//	** Allocate a new page object initially associated with cache pCache.
//	*/
func _pcache1AllocPage(tls *libc.TLS, pCache uintptr, benignMalloc int32) (r uintptr) {
	var p, pPg uintptr
	_, _ = p, pPg
	p = uintptr(0)
	if (*TPCache1)(unsafe.Pointer(pCache)).FpFree != 0 || (*TPCache1)(unsafe.Pointer(pCache)).FnPage == uint32(0) && _pcache1InitBulk(tls, pCache) != 0 {
		p = (*TPCache1)(unsafe.Pointer(pCache)).FpFree
		(*TPCache1)(unsafe.Pointer(pCache)).FpFree = (*TPgHdr1)(unsafe.Pointer(p)).FpNext
		(*TPgHdr1)(unsafe.Pointer(p)).FpNext = uintptr(0)
	} else {
		/* The group mutex must be released before pcache1Alloc() is called. This
		 ** is because it might call sqlite3_release_memory(), which assumes that
		 ** this mutex is not held. */
		Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex)
		if benignMalloc != 0 {
			_sqlite3BeginBenignMalloc(tls)
		}
		pPg = _pcache1Alloc(tls, (*TPCache1)(unsafe.Pointer(pCache)).FszAlloc)
		if benignMalloc != 0 {
			_sqlite3EndBenignMalloc(tls)
		}
		Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex)
		if pPg == uintptr(0) {
			return uintptr(0)
		}
		p = pPg + uintptr((*TPCache1)(unsafe.Pointer(pCache)).FszPage)
		(*TPgHdr1)(unsafe.Pointer(p)).Fpage.FpBuf = pPg
		(*TPgHdr1)(unsafe.Pointer(p)).Fpage.FpExtra = p + uintptr((libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
		(*TPgHdr1)(unsafe.Pointer(p)).FisBulkLocal = uint16(0)
		(*TPgHdr1)(unsafe.Pointer(p)).FisAnchor = uint16(0)
		(*TPgHdr1)(unsafe.Pointer(p)).FpLruPrev = uintptr(0) /* Initializing this saves a valgrind error */
	}
	**(**uint32)(__ccgo_up((*TPCache1)(unsafe.Pointer(pCache)).FpnPurgeable)) = **(**uint32)(__ccgo_up((*TPCache1)(unsafe.Pointer(pCache)).FpnPurgeable)) + 1
	return p
}

// C documentation
//
//	/*
//	** Implementation of the sqlite3_pcache.xCachesize method.
//	**
//	** Configure the cache_size limit for a cache.
//	*/
func _pcache1Cachesize(tls *libc.TLS, p uintptr, nMax int32) {
	var n Tu32
	var pCache, pGroup uintptr
	_, _, _ = n, pCache, pGroup
	pCache = p
	if (*TPCache1)(unsafe.Pointer(pCache)).FbPurgeable != 0 {
		pGroup = (*TPCache1)(unsafe.Pointer(pCache)).FpGroup
		Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex)
		n = uint32(nMax)
		if n > uint32(0x7fff0000)-(*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage+(*TPCache1)(unsafe.Pointer(pCache)).FnMax {
			n = uint32(0x7fff0000) - (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage + (*TPCache1)(unsafe.Pointer(pCache)).FnMax
		}
		**(**uint32)(__ccgo_up(pGroup + 4)) += n - (*TPCache1)(unsafe.Pointer(pCache)).FnMax
		(*TPGroup)(unsafe.Pointer(pGroup)).FmxPinned = (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage + uint32(10) - (*TPGroup)(unsafe.Pointer(pGroup)).FnMinPage
		(*TPCache1)(unsafe.Pointer(pCache)).FnMax = n
		(*TPCache1)(unsafe.Pointer(pCache)).Fn90pct = (*TPCache1)(unsafe.Pointer(pCache)).FnMax * uint32(9) / uint32(10)
		_pcache1EnforceMaxPage(tls, pCache)
		Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex)
	}
}

// C documentation
//
//	/*
//	** Implementation of the sqlite3_pcache.xCreate method.
//	**
//	** Allocate a new cache.
//	*/
func _pcache1Create(tls *libc.TLS, szPage int32, szExtra int32, bPurgeable int32) (r uintptr) {
	var pCache, pGroup, v1 uintptr
	var sz Ti64
	var v2 int32
	_, _, _, _, _ = pCache, pGroup, sz, v1, v2 /* Bytes of memory required to allocate the new cache */
	sz = int64(uint32(68) + uint32(52)*uint32(_pcache1_g.FseparateCache))
	pCache = _sqlite3MallocZero(tls, uint64(sz))
	if pCache != 0 {
		if _pcache1_g.FseparateCache != 0 {
			pGroup = pCache + 1*68
			(*TPGroup)(unsafe.Pointer(pGroup)).FmxPinned = uint32(10)
		} else {
			pGroup = uintptr(unsafe.Pointer(&_pcache1_g))
		}
		Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex)
		if int32((*TPGroup)(unsafe.Pointer(pGroup)).Flru.FisAnchor) == 0 {
			(*TPGroup)(unsafe.Pointer(pGroup)).Flru.FisAnchor = uint16(1)
			v1 = pGroup + 20
			(*TPGroup)(unsafe.Pointer(pGroup)).Flru.FpLruNext = v1
			(*TPGroup)(unsafe.Pointer(pGroup)).Flru.FpLruPrev = v1
		}
		(*TPCache1)(unsafe.Pointer(pCache)).FpGroup = pGroup
		(*TPCache1)(unsafe.Pointer(pCache)).FszPage = szPage
		(*TPCache1)(unsafe.Pointer(pCache)).FszExtra = szExtra
		(*TPCache1)(unsafe.Pointer(pCache)).FszAlloc = int32(uint32(szPage+szExtra) + (libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
		if bPurgeable != 0 {
			v2 = int32(1)
		} else {
			v2 = 0
		}
		(*TPCache1)(unsafe.Pointer(pCache)).FbPurgeable = v2
		_pcache1ResizeHash(tls, pCache)
		if bPurgeable != 0 {
			(*TPCache1)(unsafe.Pointer(pCache)).FnMin = uint32(10)
			**(**uint32)(__ccgo_up(pGroup + 8)) += (*TPCache1)(unsafe.Pointer(pCache)).FnMin
			(*TPGroup)(unsafe.Pointer(pGroup)).FmxPinned = (*TPGroup)(unsafe.Pointer(pGroup)).FnMaxPage + uint32(10) - (*TPGroup)(unsafe.Pointer(pGroup)).FnMinPage
			(*TPCache1)(unsafe.Pointer(pCache)).FpnPurgeable = pGroup + 16
		} else {
			(*TPCache1)(unsafe.Pointer(pCache)).FpnPurgeable = pCache + 40
		}
		Xsqlite3_mutex_leave(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex)
		if (*TPCache1)(unsafe.Pointer(pCache)).FnHash == uint32(0) {
			_pcache1Destroy(tls, pCache)
			pCache = uintptr(0)
		}
	}
	return pCache
}

// C documentation
//
//	/*
//	** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
//	** in the header of the pcache1Fetch() procedure.
//	**
//	** This steps are broken out into a separate procedure because they are
//	** usually not needed, and by avoiding the stack initialization required
//	** for these steps, the main pcache1Fetch() procedure can run faster.
//	*/
func _pcache1FetchStage2(tls *libc.TLS, pCache uintptr, iKey uint32, createFlag int32) (r uintptr) {
	var h, nPinned uint32
	var pGroup, pOther, pPage uintptr
	_, _, _, _, _ = h, nPinned, pGroup, pOther, pPage
	pGroup = (*TPCache1)(unsafe.Pointer(pCache)).FpGroup
	pPage = uintptr(0)
	/* Step 3: Abort if createFlag is 1 but the cache is nearly full */
	nPinned = (*TPCache1)(unsafe.Pointer(pCache)).FnPage - (*TPCache1)(unsafe.Pointer(pCache)).FnRecyclable
	if createFlag == int32(1) && (nPinned >= (*TPGroup)(unsafe.Pointer(pGroup)).FmxPinned || nPinned >= (*TPCache1)(unsafe.Pointer(pCache)).Fn90pct || _pcache1UnderMemoryPressure(tls, pCache) != 0 && (*TPCache1)(unsafe.Pointer(pCache)).FnRecyclable < nPinned) {
		return uintptr(0)
	}
	if (*TPCache1)(unsafe.Pointer(pCache)).FnPage >= (*TPCache1)(unsafe.Pointer(pCache)).FnHash {
		_pcache1ResizeHash(tls, pCache)
	}
	/* Step 4. Try to recycle a page. */
	if (*TPCache1)(unsafe.Pointer(pCache)).FbPurgeable != 0 && !((*TPgHdr1)(unsafe.Pointer((*TPGroup)(unsafe.Pointer(pGroup)).Flru.FpLruPrev)).FisAnchor != 0) && ((*TPCache1)(unsafe.Pointer(pCache)).FnPage+uint32(1) >= (*TPCache1)(unsafe.Pointer(pCache)).FnMax || _pcache1UnderMemoryPressure(tls, pCache) != 0) {
		pPage = (*TPGroup)(unsafe.Pointer(pGroup)).Flru.FpLruPrev
		_pcache1RemoveFromHash(tls, pPage, 0)
		_pcache1PinPage(tls, pPage)
		pOther = (*TPgHdr1)(unsafe.Pointer(pPage)).FpCache
		if (*TPCache1)(unsafe.Pointer(pOther)).FszAlloc != (*TPCache1)(unsafe.Pointer(pCache)).FszAlloc {
			_pcache1FreePage(tls, pPage)
			pPage = uintptr(0)
		} else {
			**(**uint32)(__ccgo_up(pGroup + 16)) -= uint32((*TPCache1)(unsafe.Pointer(pOther)).FbPurgeable - (*TPCache1)(unsafe.Pointer(pCache)).FbPurgeable)
		}
	}
	/* Step 5. If a usable page buffer has still not been found,
	 ** attempt to allocate a new one.
	 */
	if !(pPage != 0) {
		pPage = _pcache1AllocPage(tls, pCache, libc.BoolInt32(createFlag == int32(1)))
	}
	if pPage != 0 {
		h = iKey % (*TPCache1)(unsafe.Pointer(pCache)).FnHash
		(*TPCache1)(unsafe.Pointer(pCache)).FnPage = (*TPCache1)(unsafe.Pointer(pCache)).FnPage + 1
		(*TPgHdr1)(unsafe.Pointer(pPage)).FiKey = iKey
		(*TPgHdr1)(unsafe.Pointer(pPage)).FpNext = **(**uintptr)(__ccgo_up((*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(h)*4))
		(*TPgHdr1)(unsafe.Pointer(pPage)).FpCache = pCache
		(*TPgHdr1)(unsafe.Pointer(pPage)).FpLruNext = uintptr(0)
		/* pPage->pLruPrev = 0;
		 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
		**(**uintptr)(__ccgo_up((*TPgHdr1)(unsafe.Pointer(pPage)).Fpage.FpExtra)) = uintptr(0)
		**(**uintptr)(__ccgo_up((*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(h)*4)) = pPage
		if iKey > (*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey {
			(*TPCache1)(unsafe.Pointer(pCache)).FiMaxKey = iKey
		}
	}
	return pPage
}

// C documentation
//
//	/*
//	** Try to initialize the pCache->pFree and pCache->pBulk fields.  Return
//	** true if pCache->pFree ends up containing one or more free pages.
//	*/
func _pcache1InitBulk(tls *libc.TLS, pCache uintptr) (r int32) {
	var nBulk, v2 int32
	var pX, zBulk, v1 uintptr
	var szBulk Ti64
	_, _, _, _, _, _ = nBulk, pX, szBulk, zBulk, v1, v2
	if _pcache1_g.FnInitPage == 0 {
		return 0
	}
	/* Do not bother with a bulk allocation if the cache size very small */
	if (*TPCache1)(unsafe.Pointer(pCache)).FnMax < uint32(3) {
		return 0
	}
	_sqlite3BeginBenignMalloc(tls)
	if _pcache1_g.FnInitPage > 0 {
		szBulk = int64((*TPCache1)(unsafe.Pointer(pCache)).FszAlloc) * int64(_pcache1_g.FnInitPage)
	} else {
		szBulk = int64(-int32(1024)) * int64(_pcache1_g.FnInitPage)
	}
	if szBulk > int64((*TPCache1)(unsafe.Pointer(pCache)).FszAlloc)*int64((*TPCache1)(unsafe.Pointer(pCache)).FnMax) {
		szBulk = int64((*TPCache1)(unsafe.Pointer(pCache)).FszAlloc) * int64((*TPCache1)(unsafe.Pointer(pCache)).FnMax)
	}
	if szBulk >= int64((*TPCache1)(unsafe.Pointer(pCache)).FszAlloc) {
		v1 = _sqlite3Malloc(tls, uint64(szBulk))
		(*TPCache1)(unsafe.Pointer(pCache)).FpBulk = v1
		zBulk = v1
		_sqlite3EndBenignMalloc(tls)
		if zBulk != 0 {
			nBulk = _sqlite3MallocSize(tls, zBulk) / (*TPCache1)(unsafe.Pointer(pCache)).FszAlloc
			for {
				pX = zBulk + uintptr((*TPCache1)(unsafe.Pointer(pCache)).FszPage)
				(*TPgHdr1)(unsafe.Pointer(pX)).Fpage.FpBuf = zBulk
				(*TPgHdr1)(unsafe.Pointer(pX)).Fpage.FpExtra = pX + uintptr((libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
				(*TPgHdr1)(unsafe.Pointer(pX)).FisBulkLocal = uint16(1)
				(*TPgHdr1)(unsafe.Pointer(pX)).FisAnchor = uint16(0)
				(*TPgHdr1)(unsafe.Pointer(pX)).FpNext = (*TPCache1)(unsafe.Pointer(pCache)).FpFree
				(*TPgHdr1)(unsafe.Pointer(pX)).FpLruPrev = uintptr(0) /* Initializing this saves a valgrind error */
				(*TPCache1)(unsafe.Pointer(pCache)).FpFree = pX
				zBulk = zBulk + uintptr((*TPCache1)(unsafe.Pointer(pCache)).FszAlloc)
				goto _3
			_3:
				;
				nBulk = nBulk - 1
				v2 = nBulk
				if !(v2 != 0) {
					break
				}
			}
		}
	}
	return libc.BoolInt32((*TPCache1)(unsafe.Pointer(pCache)).FpFree != uintptr(0))
}

// C documentation
//
//	/*
//	** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
//	** computed.
//	*/
func _populateCellCache(tls *libc.TLS, p uintptr, idx int32, N int32) {
	var pRef, szCell uintptr
	_, _ = pRef, szCell
	pRef = (*TCellArray)(unsafe.Pointer(p)).FpRef
	szCell = (*TCellArray)(unsafe.Pointer(p)).FszCell
	for N > 0 {
		if int32(**(**Tu16)(__ccgo_up(szCell + uintptr(idx)*2))) == 0 {
			**(**Tu16)(__ccgo_up(szCell + uintptr(idx)*2)) = (*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pRef)).FxCellSize})))(tls, pRef, **(**uintptr)(__ccgo_up((*TCellArray)(unsafe.Pointer(p)).FapCell + uintptr(idx)*4)))
		} else {
		}
		idx = idx + 1
		N = N - 1
	}
}

// C documentation
//
//	/*
//	** Locate a pragma in the aPragmaName[] array.
//	*/
func _pragmaLocate(tls *libc.TLS, zName uintptr) (r uintptr) {
	var lwr, mid, rc, upr int32
	var v1 uintptr
	_, _, _, _, _ = lwr, mid, rc, upr, v1
	mid = 0
	lwr = 0
	upr = int32(libc.Uint32FromInt64(1072)/libc.Uint32FromInt64(16)) - libc.Int32FromInt32(1)
	for lwr <= upr {
		mid = (lwr + upr) / int32(2)
		rc = Xsqlite3_stricmp(tls, zName, _aPragmaName[mid].FzName)
		if rc == 0 {
			break
		}
		if rc < 0 {
			upr = mid - int32(1)
		} else {
			lwr = mid + int32(1)
		}
	}
	if lwr > upr {
		v1 = uintptr(0)
	} else {
		v1 = uintptr(unsafe.Pointer(&_aPragmaName)) + uintptr(mid)*16
	}
	return v1
}

// C documentation
//
//	/* The xColumn method simply returns the corresponding column from
//	** the PRAGMA.
//	*/
func _pragmaVtabColumn(tls *libc.TLS, pVtabCursor uintptr, ctx uintptr, i int32) (r int32) {
	var pCsr, pTab uintptr
	_, _ = pCsr, pTab
	pCsr = pVtabCursor
	pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVtabCursor)).FpVtab
	if i < int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden) {
		Xsqlite3_result_value(tls, ctx, Xsqlite3_column_value(tls, (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma, i))
	} else {
		Xsqlite3_result_text(tls, ctx, **(**uintptr)(__ccgo_up(pCsr + 16 + uintptr(i-int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden))*4)), -int32(1), uintptr(-libc.Int32FromInt32(1)))
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Pragma virtual table module xConnect method.
//	*/
func _pragmaVtabConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(256)
	defer tls.Free(256)
	var cSep int8
	var i, j, rc int32
	var pPragma, pTab uintptr
	var _ /* acc at bp+0 */ TStrAccum
	var _ /* zBuf at bp+24 */ [200]int8
	_, _, _, _, _, _ = cSep, i, j, pPragma, pTab, rc
	pPragma = pAux
	pTab = uintptr(0)
	cSep = int8('(')
	_ = argc
	_ = argv
	_sqlite3StrAccumInit(tls, bp, uintptr(0), bp+24, int32(200), 0)
	Xsqlite3_str_appendall(tls, bp, __ccgo_ts+21140)
	i = 0
	j = int32((*TPragmaName)(unsafe.Pointer(pPragma)).FiPragCName)
	for {
		if !(i < int32((*TPragmaName)(unsafe.Pointer(pPragma)).FnPragCName)) {
			break
		}
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+21155, libc.VaList(bp+232, int32(cSep), _pragCName[j]))
		cSep = int8(',')
		goto _1
	_1:
		;
		i = i + 1
		j = j + 1
	}
	if i == 0 {
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+21162, libc.VaList(bp+232, (*TPragmaName)(unsafe.Pointer(pPragma)).FzName))
		i = i + 1
	}
	j = 0
	if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_Result1) != 0 {
		Xsqlite3_str_appendall(tls, bp, __ccgo_ts+21168)
		j = j + 1
	}
	if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&(libc.Int32FromInt32(PragFlg_SchemaOpt)|libc.Int32FromInt32(PragFlg_SchemaReq)) != 0 {
		Xsqlite3_str_appendall(tls, bp, __ccgo_ts+21180)
		j = j + 1
	}
	Xsqlite3_str_append(tls, bp, __ccgo_ts+6474, int32(1))
	_sqlite3StrAccumFinish(tls, bp)
	rc = Xsqlite3_declare_vtab(tls, db, bp+24)
	if rc == SQLITE_OK {
		pTab = Xsqlite3_malloc(tls, int32(24))
		if pTab == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		} else {
			libc.Xmemset(tls, pTab, 0, uint32(24))
			(*TPragmaVtab)(unsafe.Pointer(pTab)).FpName = pPragma
			(*TPragmaVtab)(unsafe.Pointer(pTab)).Fdb = db
			(*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden = uint8(i)
			(*TPragmaVtab)(unsafe.Pointer(pTab)).FnHidden = uint8(j)
		}
	} else {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+232, Xsqlite3_errmsg(tls, db)))
	}
	**(**uintptr)(__ccgo_up(ppVtab)) = pTab
	return rc
}

// C documentation
//
//	/* Clear all content from pragma virtual table cursor. */
func _pragmaVtabCursorClear(tls *libc.TLS, pCsr uintptr) {
	var i int32
	_ = i
	Xsqlite3_finalize(tls, (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma)
	(*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FpPragma = uintptr(0)
	(*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FiRowid = 0
	i = 0
	for {
		if !(i < int32(libc.Uint32FromInt64(8)/libc.Uint32FromInt64(4))) {
			break
		}
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(pCsr + 16 + uintptr(i)*4)))
		**(**uintptr)(__ccgo_up(pCsr + 16 + uintptr(i)*4)) = uintptr(0)
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Pragma virtual table module xFilter method.
//	*/
func _pragmaVtabFilter(tls *libc.TLS, pVtabCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i, j, rc, v1 int32
	var pCsr, pTab, zSql, zText uintptr
	var _ /* acc at bp+0 */ TStrAccum
	_, _, _, _, _, _, _, _ = i, j, pCsr, pTab, rc, zSql, zText, v1
	pCsr = pVtabCursor
	pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVtabCursor)).FpVtab
	_ = idxNum
	_ = idxStr
	_pragmaVtabCursorClear(tls, pCsr)
	if int32((*TPragmaName)(unsafe.Pointer((*TPragmaVtab)(unsafe.Pointer(pTab)).FpName)).FmPragFlg)&int32(PragFlg_Result1) != 0 {
		v1 = 0
	} else {
		v1 = int32(1)
	}
	j = v1
	i = 0
	for {
		if !(i < argc) {
			break
		}
		zText = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
		if zText != 0 {
			**(**uintptr)(__ccgo_up(pCsr + 16 + uintptr(j)*4)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+32, zText))
			if **(**uintptr)(__ccgo_up(pCsr + 16 + uintptr(j)*4)) == uintptr(0) {
				return int32(SQLITE_NOMEM)
			}
		}
		goto _2
	_2:
		;
		i = i + 1
		j = j + 1
	}
	_sqlite3StrAccumInit(tls, bp, uintptr(0), uintptr(0), 0, **(**int32)(__ccgo_up((*TPragmaVtab)(unsafe.Pointer(pTab)).Fdb + 120 + 1*4)))
	Xsqlite3_str_appendall(tls, bp, __ccgo_ts+21195)
	if **(**uintptr)(__ccgo_up(pCsr + 16 + 1*4)) != 0 {
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+21203, libc.VaList(bp+32, **(**uintptr)(__ccgo_up(pCsr + 16 + 1*4))))
	}
	Xsqlite3_str_appendall(tls, bp, (*TPragmaName)(unsafe.Pointer((*TPragmaVtab)(unsafe.Pointer(pTab)).FpName)).FzName)
	if **(**uintptr)(__ccgo_up(pCsr + 16)) != 0 {
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+21207, libc.VaList(bp+32, **(**uintptr)(__ccgo_up(pCsr + 16))))
	}
	zSql = _sqlite3StrAccumFinish(tls, bp)
	if zSql == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	rc = Xsqlite3_prepare_v2(tls, (*TPragmaVtab)(unsafe.Pointer(pTab)).Fdb, zSql, -int32(1), pCsr+4, uintptr(0))
	Xsqlite3_free(tls, zSql)
	if rc != SQLITE_OK {
		(*TPragmaVtab)(unsafe.Pointer(pTab)).Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+32, Xsqlite3_errmsg(tls, (*TPragmaVtab)(unsafe.Pointer(pTab)).Fdb)))
		return rc
	}
	return _pragmaVtabNext(tls, pVtabCursor)
}

// C documentation
//
//	/*
//	** fts5VisitEntries() callback used by fts5SetupPrefixIter()
//	*/
func _prefixIterSetupCb(tls *libc.TLS, p uintptr, pCtx uintptr, p1 uintptr, pNew uintptr, nNew int32) {
	var i, i1, iStore, nMerge int32
	var pSetup uintptr
	_, _, _, _, _ = i, i1, iStore, nMerge, pSetup
	pSetup = pCtx
	nMerge = (*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FnMerge
	if (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FnData > 0 {
		if (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FiRowid <= (*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FiLastRowid && (*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).Fdoclist.Fn > 0 {
			i = 0
			for {
				if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).Fdoclist.Fn != 0) {
					break
				}
				i1 = i * nMerge
				iStore = i1
				for {
					if !(iStore < i1+nMerge) {
						break
					}
					if (**(**TFts5Buffer)(__ccgo_up((*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FaBuf + uintptr(iStore)*12))).Fn == 0 {
						_fts5BufferSwap(tls, pSetup+28, (*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FaBuf+uintptr(iStore)*12)
						_sqlite3Fts5BufferZero(tls, pSetup+28)
						break
					}
					goto _2
				_2:
					;
					iStore = iStore + 1
				}
				if iStore == i1+nMerge {
					(*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FxMerge})))(tls, p, pSetup+28, nMerge, (*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FaBuf+uintptr(i1)*12)
					iStore = i1
					for {
						if !(iStore < i1+nMerge) {
							break
						}
						_sqlite3Fts5BufferZero(tls, (*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FaBuf+uintptr(iStore)*12)
						goto _3
					_3:
						;
						iStore = iStore + 1
					}
				}
				goto _1
			_1:
				;
				i = i + 1
			}
			(*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FiLastRowid = 0
		}
		(*(*func(*libc.TLS, uintptr, Tu64, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FxAppend})))(tls, p, uint64((*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FiRowid)-uint64((*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FiLastRowid), p1, pSetup+28)
		(*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FiLastRowid = (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FiRowid
	}
	if (*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FpTokendata != 0 {
		_prefixIterSetupTokendataCb(tls, p, (*TPrefixSetupCtx)(unsafe.Pointer(pSetup)).FpTokendata, p1, pNew, nNew)
	}
}

// C documentation
//
//	/*
//	** fts5VisitEntries() callback used by fts5SetupPrefixIterTokendata(). This
//	** callback adds an entry to the Fts5TokenDataIter.aMap[] array for each
//	** position in the current position-list. It doesn't matter that some of
//	** these may be out of order - they will be sorted later.
//	*/
func _prefixIterSetupTokendataCb(tls *libc.TLS, p uintptr, pCtx uintptr, p1 uintptr, pNew uintptr, nNew int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pSetup uintptr
	var _ /* iPos at bp+8 */ Ti64
	var _ /* iPosOff at bp+0 */ int32
	_ = pSetup
	pSetup = pCtx
	**(**int32)(__ccgo_up(bp)) = 0
	**(**Ti64)(__ccgo_up(bp + 8)) = 0
	if pNew != 0 {
		(*TTokendataSetupCtx)(unsafe.Pointer(pSetup)).FnTermByte = nNew - int32(1)
		(*TTokendataSetupCtx)(unsafe.Pointer(pSetup)).FiTermOff = (*TFts5TokenDataIter)(unsafe.Pointer((*TTokendataSetupCtx)(unsafe.Pointer(pSetup)).FpT)).Fterms.Fn
		_sqlite3Fts5BufferAppendBlob(tls, p+44, (*TTokendataSetupCtx)(unsafe.Pointer(pSetup)).FpT+20, uint32(nNew-int32(1)), pNew+uintptr(1))
	}
	for 0 == _sqlite3Fts5PoslistNext64(tls, (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FpData, (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FnData, bp, bp+8) {
		_fts5TokendataIterAppendMap(tls, p, (*TTokendataSetupCtx)(unsafe.Pointer(pSetup)).FpT, (*TTokendataSetupCtx)(unsafe.Pointer(pSetup)).FiTermOff, (*TTokendataSetupCtx)(unsafe.Pointer(pSetup)).FnTermByte, (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FiRowid, **(**Ti64)(__ccgo_up(bp + 8)))
	}
}

// C documentation
//
//	/*
//	** Implementation of the printf() (a.k.a. format()) SQL function.
//	*/
func _printfFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var db, zFormat, v1 uintptr
	var n int32
	var v2 bool
	var _ /* str at bp+12 */ TStrAccum
	var _ /* x at bp+0 */ TPrintfArguments
	_, _, _, _, _ = db, n, zFormat, v1, v2
	db = Xsqlite3_context_db_handle(tls, context)
	if v2 = argc >= int32(1); v2 {
		v1 = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		zFormat = v1
	}
	if v2 && v1 != uintptr(0) {
		(**(**TPrintfArguments)(__ccgo_up(bp))).FnArg = argc - int32(1)
		(**(**TPrintfArguments)(__ccgo_up(bp))).FnUsed = 0
		(**(**TPrintfArguments)(__ccgo_up(bp))).FapArg = argv + uintptr(1)*4
		_sqlite3StrAccumInit(tls, bp+12, db, uintptr(0), 0, **(**int32)(__ccgo_up(db + 120)))
		(**(**TStrAccum)(__ccgo_up(bp + 12))).FprintfFlags = uint8(SQLITE_PRINTF_SQLFUNC)
		Xsqlite3_str_appendf(tls, bp+12, zFormat, libc.VaList(bp+48, bp))
		if int32((**(**TStrAccum)(__ccgo_up(bp + 12))).FaccError) == SQLITE_OK {
			n = int32((**(**TStrAccum)(__ccgo_up(bp + 12))).FnChar)
			Xsqlite3_result_text(tls, context, _sqlite3StrAccumFinish(tls, bp+12), n, __ccgo_fp(_sqlite3RowSetClear))
		} else {
			if int32((**(**TStrAccum)(__ccgo_up(bp + 12))).FaccError) == int32(SQLITE_NOMEM) {
				Xsqlite3_result_error_nomem(tls, context)
			} else {
				Xsqlite3_result_error_toobig(tls, context)
			}
			Xsqlite3_str_reset(tls, bp+12)
		}
	}
}

// C documentation
//
//	/*
//	** This is a Walker expression callback. pExpr is a node from the WHERE
//	** clause of a SELECT statement. This function examines pExpr to see if
//	** any substitutions based on the contents of pWalker->u.pConst should
//	** be made to pExpr or its immediate children.
//	**
//	** A substitution is made if:
//	**
//	**   + pExpr is a column with an affinity other than BLOB that matches
//	**     one of the columns in pWalker->u.pConst, or
//	**
//	**   + pExpr is a binary comparison operator (=, <=, >=, <, >) that
//	**     uses an affinity other than TEXT and one of its immediate
//	**     children is a column that matches one of the columns in
//	**     pWalker->u.pConst.
//	*/
func _propagateConstantExprRewrite(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var pConst uintptr
	_ = pConst
	pConst = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	if (*TWhereConst)(unsafe.Pointer(pConst)).FbHasAffBlob != 0 {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) >= int32(TK_EQ) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) <= int32(TK_GE) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IS) {
			_propagateConstantExprRewriteOne(tls, pConst, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, 0)
			if **(**Tu8)(__ccgo_up((*TWhereConst)(unsafe.Pointer(pConst)).FpOomFault)) != 0 {
				return int32(WRC_Prune)
			}
			if int32(_sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)) != int32(SQLITE_AFF_TEXT) {
				_propagateConstantExprRewriteOne(tls, pConst, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, 0)
			}
		}
	}
	return _propagateConstantExprRewriteOne(tls, pConst, pExpr, (*TWhereConst)(unsafe.Pointer(pConst)).FbHasAffBlob)
}

// C documentation
//
//	/*
//	** This is a helper function for Walker callback propagateConstantExprRewrite().
//	**
//	** Argument pExpr is a candidate expression to be replaced by a value. If
//	** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
//	** then overwrite it with the corresponding value. Except, do not do so
//	** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
//	** is SQLITE_AFF_BLOB.
//	*/
func _propagateConstantExprRewriteOne(tls *libc.TLS, pConst uintptr, pExpr uintptr, bIgnoreAffBlob int32) (r int32) {
	var i int32
	var pColumn uintptr
	_, _ = i, pColumn
	if **(**Tu8)(__ccgo_up((*TWhereConst)(unsafe.Pointer(pConst)).FpOomFault)) != 0 {
		return int32(WRC_Prune)
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) {
		return WRC_Continue
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&(libc.Uint32FromInt32(EP_FixedCol)|(*TWhereConst)(unsafe.Pointer(pConst)).FmExcludeOn) != uint32(0) {
		return WRC_Continue
	}
	i = 0
	for {
		if !(i < (*TWhereConst)(unsafe.Pointer(pConst)).FnConst) {
			break
		}
		pColumn = **(**uintptr)(__ccgo_up((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr(i*int32(2))*4))
		if pColumn == pExpr {
			goto _1
		}
		if (*TExpr)(unsafe.Pointer(pColumn)).FiTable != (*TExpr)(unsafe.Pointer(pExpr)).FiTable {
			goto _1
		}
		if int32((*TExpr)(unsafe.Pointer(pColumn)).FiColumn) != int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) {
			goto _1
		}
		if bIgnoreAffBlob != 0 && int32(_sqlite3ExprAffinity(tls, pColumn)) <= int32(SQLITE_AFF_BLOB) {
			break
		}
		/* A match is found.  Add the EP_FixedCol property */
		(*TWhereConst)(unsafe.Pointer(pConst)).FnChng = (*TWhereConst)(unsafe.Pointer(pConst)).FnChng + 1
		**(**Tu32)(__ccgo_up(pExpr + 4)) &= ^uint32(libc.Int32FromInt32(EP_Leaf))
		**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_FixedCol))
		(*TExpr)(unsafe.Pointer(pExpr)).FpLeft = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FpParse)).Fdb, **(**uintptr)(__ccgo_up((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr(i*int32(2)+int32(1))*4)), 0)
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FpParse)).Fdb)).FmallocFailed != 0 {
			return int32(WRC_Prune)
		}
		break
		goto _1
	_1:
		;
		i = i + 1
	}
	return int32(WRC_Prune)
}

// C documentation
//
//	/*
//	** The WHERE-clause constant propagation optimization.
//	**
//	** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
//	** CONSTANT=COLUMN that are top-level AND-connected terms that are not
//	** part of a ON clause from a LEFT JOIN, then throughout the query
//	** replace all other occurrences of COLUMN with CONSTANT.
//	**
//	** For example, the query:
//	**
//	**      SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
//	**
//	** Is transformed into
//	**
//	**      SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
//	**
//	** Return true if any transformations where made and false if not.
//	**
//	** Implementation note:  Constant propagation is tricky due to affinity
//	** and collating sequence interactions.  Consider this example:
//	**
//	**    CREATE TABLE t1(a INT,b TEXT);
//	**    INSERT INTO t1 VALUES(123,'0123');
//	**    SELECT * FROM t1 WHERE a=123 AND b=a;
//	**    SELECT * FROM t1 WHERE a=123 AND b=123;
//	**
//	** The two SELECT statements above should return different answers.  b=a
//	** is always true because the comparison uses numeric affinity, but b=123
//	** is false because it uses text affinity and '0123' is not the same as '123'.
//	** To work around this, the expression tree is not actually changed from
//	** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
//	** and the "123" value is hung off of the pLeft pointer.  Code generator
//	** routines know to generate the constant "123" instead of looking up the
//	** column value.  Also, to avoid collation problems, this optimization is
//	** only attempted if the "a=123" term uses the default BINARY collation.
//	**
//	** 2021-05-25 forum post 6a06202608: Another troublesome case is...
//	**
//	**    CREATE TABLE t1(x);
//	**    INSERT INTO t1 VALUES(10.0);
//	**    SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10;
//	**
//	** The query should return no rows, because the t1.x value is '10.0' not '10'
//	** and '10.0' is not LIKE '10'.  But if we are not careful, the first WHERE
//	** term "x=10" will cause the second WHERE term to become "10 LIKE 10",
//	** resulting in a false positive.  To avoid this, constant propagation for
//	** columns with BLOB affinity is only allowed if the constant is used with
//	** operators ==, <=, <, >=, >, or IS in a way that will cause the correct
//	** type conversions to occur.  See logic associated with the bHasAffBlob flag
//	** for details.
//	*/
func _propagateConstants(tls *libc.TLS, pParse uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var nChng int32
	var _ /* w at bp+28 */ TWalker
	var _ /* x at bp+0 */ TWhereConst
	_ = nChng
	nChng = 0
	(**(**TWhereConst)(__ccgo_up(bp))).FpParse = pParse
	(**(**TWhereConst)(__ccgo_up(bp))).FpOomFault = (*TParse)(unsafe.Pointer(pParse)).Fdb + 87
	for cond := true; cond; cond = (**(**TWhereConst)(__ccgo_up(bp))).FnChng != 0 {
		(**(**TWhereConst)(__ccgo_up(bp))).FnConst = 0
		(**(**TWhereConst)(__ccgo_up(bp))).FnChng = 0
		(**(**TWhereConst)(__ccgo_up(bp))).FapExpr = uintptr(0)
		(**(**TWhereConst)(__ccgo_up(bp))).FbHasAffBlob = 0
		if (*TSelect)(unsafe.Pointer(p)).FpSrc != uintptr(0) && (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc > 0 && int32((*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
			/* Do not propagate constants on any ON clause if there is a
			 ** RIGHT JOIN anywhere in the query */
			(**(**TWhereConst)(__ccgo_up(bp))).FmExcludeOn = uint32(libc.Int32FromInt32(EP_InnerON) | libc.Int32FromInt32(EP_OuterON))
		} else {
			/* Do not propagate constants through the ON clause of a LEFT JOIN */
			(**(**TWhereConst)(__ccgo_up(bp))).FmExcludeOn = uint32(EP_OuterON)
		}
		_findConstInWhere(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpWhere)
		if (**(**TWhereConst)(__ccgo_up(bp))).FnConst != 0 {
			libc.Xmemset(tls, bp+28, 0, uint32(28))
			(**(**TWalker)(__ccgo_up(bp + 28))).FpParse = pParse
			(**(**TWalker)(__ccgo_up(bp + 28))).FxExprCallback = __ccgo_fp(_propagateConstantExprRewrite)
			(**(**TWalker)(__ccgo_up(bp + 28))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop)
			(**(**TWalker)(__ccgo_up(bp + 28))).FxSelectCallback2 = uintptr(0)
			(**(**TWalker)(__ccgo_up(bp + 28))).FwalkerDepth = 0
			*(*uintptr)(unsafe.Pointer(bp + 28 + 24)) = bp
			_sqlite3WalkExpr(tls, bp+28, (*TSelect)(unsafe.Pointer(p)).FpWhere)
			_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer((**(**TWhereConst)(__ccgo_up(bp))).FpParse)).Fdb, (**(**TWhereConst)(__ccgo_up(bp))).FapExpr)
			nChng = nChng + (**(**TWhereConst)(__ccgo_up(bp))).FnChng
		}
	}
	return nChng
}

// C documentation
//
//	/*
//	** The cell pCell is currently part of page pSrc but will ultimately be part
//	** of pPage.  (pSrc and pPage are often the same.)  If pCell contains a
//	** pointer to an overflow page, insert an entry into the pointer-map for
//	** the overflow page that will be valid after pCell has been moved to pPage.
//	*/
func _ptrmapPutOvflPtr(tls *libc.TLS, pPage uintptr, pSrc uintptr, pCell uintptr, pRC uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var ovfl TPgno
	var _ /* info at bp+0 */ TCellInfo
	_ = ovfl
	if **(**int32)(__ccgo_up(pRC)) != 0 {
		return
	}
	(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, pCell, bp)
	if uint32((**(**TCellInfo)(__ccgo_up(bp))).FnLocal) < (**(**TCellInfo)(__ccgo_up(bp))).FnPayload {
		if uint32(pCell) < uint32((*TMemPage)(unsafe.Pointer(pSrc)).FaDataEnd) && uint32(pCell+uintptr((**(**TCellInfo)(__ccgo_up(bp))).FnLocal)) > uint32((*TMemPage)(unsafe.Pointer(pSrc)).FaDataEnd) {
			**(**int32)(__ccgo_up(pRC)) = _sqlite3CorruptError(tls, int32(74817))
			return
		}
		ovfl = _sqlite3Get4byte(tls, pCell+uintptr(int32((**(**TCellInfo)(__ccgo_up(bp))).FnSize)-int32(4)))
		_ptrmapPut(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpBt, ovfl, uint8(PTRMAP_OVERFLOW1), (*TMemPage)(unsafe.Pointer(pPage)).Fpgno, pRC)
	}
}

// C documentation
//
//	/*
//	** Make copies of relevant WHERE clause terms of the outer query into
//	** the WHERE clause of subquery.  Example:
//	**
//	**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
//	**
//	** Transformed into:
//	**
//	**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
//	**     WHERE x=5 AND y=10;
//	**
//	** The hope is that the terms added to the inner query will make it more
//	** efficient.
//	**
//	** NAME AMBIGUITY
//	**
//	** This optimization is called the "WHERE-clause push-down optimization"
//	** or sometimes the "predicate push-down optimization".
//	**
//	** Do not confuse this optimization with another unrelated optimization
//	** with a similar name:  The "MySQL push-down optimization" causes WHERE
//	** clause terms that can be evaluated using only the index and without
//	** reference to the table are run first, so that if they are false,
//	** unnecessary table seeks are avoided.
//	**
//	** RULES
//	**
//	** Do not attempt this optimization if:
//	**
//	**   (1) (** This restriction was removed on 2017-09-29.  We used to
//	**           disallow this optimization for aggregate subqueries, but now
//	**           it is allowed by putting the extra terms on the HAVING clause.
//	**           The added HAVING clause is pointless if the subquery lacks
//	**           a GROUP BY clause.  But such a HAVING clause is also harmless
//	**           so there does not appear to be any reason to add extra logic
//	**           to suppress it. **)
//	**
//	**   (2) The inner query is the recursive part of a common table expression.
//	**
//	**   (3) The inner query has a LIMIT clause (since the changes to the WHERE
//	**       clause would change the meaning of the LIMIT).
//	**
//	**   (4) The inner query is the right operand of a LEFT JOIN and the
//	**       expression to be pushed down does not come from the ON clause
//	**       on that LEFT JOIN.
//	**
//	**   (5) The WHERE clause expression originates in the ON or USING clause
//	**       of a LEFT JOIN where iCursor is not the right-hand table of that
//	**       left join.  An example:
//	**
//	**           SELECT *
//	**           FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
//	**           JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
//	**           LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
//	**
//	**       The correct answer is three rows:  (1,1,NULL),(2,2,8),(2,2,9).
//	**       But if the (b2=2) term were to be pushed down into the bb subquery,
//	**       then the (1,1,NULL) row would be suppressed.
//	**
//	**   (6) Window functions make things tricky as changes to the WHERE clause
//	**       of the inner query could change the window over which window
//	**       functions are calculated. Therefore, do not attempt the optimization
//	**       if:
//	**
//	**     (6a) The inner query uses multiple incompatible window partitions.
//	**
//	**     (6b) The inner query is a compound and uses window-functions.
//	**
//	**     (6c) The WHERE clause does not consist entirely of constants and
//	**          copies of expressions found in the PARTITION BY clause of
//	**          all window-functions used by the sub-query. It is safe to
//	**          filter out entire partitions, as this does not change the
//	**          window over which any window-function is calculated.
//	**
//	**   (7) The inner query is a Common Table Expression (CTE) that should
//	**       be materialized.  (This restriction is implemented in the calling
//	**       routine.)
//	**
//	**   (8) If the subquery is a compound that uses UNION, INTERSECT,
//	**       or EXCEPT, then all of the result set columns for all arms of
//	**       the compound must use the BINARY collating sequence.
//	**
//	**   (9) All three of the following are true:
//	**
//	**       (9a) The WHERE clause expression originates in the ON or USING clause
//	**            of a join (either an INNER or an OUTER join), and
//	**
//	**       (9b) The subquery is to the right of the ON/USING clause
//	**
//	**       (9c) There is a RIGHT JOIN (or FULL JOIN) in between the ON/USING
//	**            clause and the subquery.
//	**
//	**       Without this restriction, the WHERE-clause push-down optimization
//	**       might move the ON/USING filter expression from the left side of a
//	**       RIGHT JOIN over to the right side, which leads to incorrect answers.
//	**       See also restriction (6) in sqlite3ExprIsSingleTableConstraint().
//	**
//	**  (10) The inner query is not the right-hand table of a RIGHT JOIN.
//	**
//	**  (11) The subquery is not a VALUES clause
//	**
//	**  (12) The WHERE clause is not "rowid ISNULL" or the equivalent.  This
//	**       case only comes up if SQLite is compiled using
//	**       SQLITE_ALLOW_ROWID_IN_VIEW.
//	**
//	** Return 0 if no changes are made and non-zero if one or more WHERE clause
//	** terms are duplicated into the subquery.
//	*/
func _pushDownWhereTerms(tls *libc.TLS, pParse uintptr, pSubq uintptr, pWhere uintptr, pSrcList uintptr, iSrc int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var ii, nChng, notUnionAll int32
	var op Tu8
	var pColl, pList, pNew, pSel, pSrc uintptr
	var _ /* x at bp+0 */ TSubstContext
	_, _, _, _, _, _, _, _, _ = ii, nChng, notUnionAll, op, pColl, pList, pNew, pSel, pSrc /* The subquery FROM term into which WHERE is pushed */
	nChng = 0
	pSrc = pSrcList + 8 + uintptr(iSrc)*48
	if pWhere == uintptr(0) {
		return 0
	}
	if (*TSelect)(unsafe.Pointer(pSubq)).FselFlags&uint32(libc.Int32FromInt32(SF_Recursive)|libc.Int32FromInt32(SF_MultiPart)) != 0 {
		return 0 /* restrictions (2) and (11) */
	}
	if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 {
		return 0 /* restrictions (10) */
	}
	if (*TSelect)(unsafe.Pointer(pSubq)).FpPrior != 0 {
		notUnionAll = 0
		pSel = pSubq
		for {
			if !(pSel != 0) {
				break
			}
			op = (*TSelect)(unsafe.Pointer(pSel)).Fop
			if int32(op) != int32(TK_ALL) && int32(op) != int32(TK_SELECT) {
				notUnionAll = int32(1)
			}
			if (*TSelect)(unsafe.Pointer(pSel)).FpWin != 0 {
				return 0
			} /* restriction (6b) */
			goto _1
		_1:
			;
			pSel = (*TSelect)(unsafe.Pointer(pSel)).FpPrior
		}
		if notUnionAll != 0 {
			/* If any of the compound arms are connected using UNION, INTERSECT,
			 ** or EXCEPT, then we must ensure that none of the columns use a
			 ** non-BINARY collating sequence. */
			pSel = pSubq
			for {
				if !(pSel != 0) {
					break
				}
				pList = (*TSelect)(unsafe.Pointer(pSel)).FpEList
				ii = 0
				for {
					if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
						break
					}
					pColl = _sqlite3ExprCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr)
					if !(_sqlite3IsBinary(tls, pColl) != 0) {
						return 0 /* Restriction (8) */
					}
					goto _3
				_3:
					;
					ii = ii + 1
				}
				goto _2
			_2:
				;
				pSel = (*TSelect)(unsafe.Pointer(pSel)).FpPrior
			}
		}
	} else {
		if (*TSelect)(unsafe.Pointer(pSubq)).FpWin != 0 && (*TWindow)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSubq)).FpWin)).FpPartition == uintptr(0) {
			return 0
		}
	}
	if (*TSelect)(unsafe.Pointer(pSubq)).FpLimit != uintptr(0) {
		return 0 /* restriction (3) */
	}
	for int32((*TExpr)(unsafe.Pointer(pWhere)).Fop) == int32(TK_AND) {
		nChng = nChng + _pushDownWhereTerms(tls, pParse, pSubq, (*TExpr)(unsafe.Pointer(pWhere)).FpRight, pSrcList, iSrc)
		pWhere = (*TExpr)(unsafe.Pointer(pWhere)).FpLeft
	}
	if _sqlite3ExprIsSingleTableConstraint(tls, pWhere, pSrcList, iSrc, int32(1)) != 0 {
		nChng = nChng + 1
		**(**Tu32)(__ccgo_up(pSubq + 4)) |= uint32(SF_PushDown)
		for pSubq != 0 {
			pNew = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pWhere, 0)
			_unsetJoinExpr(tls, pNew, -int32(1), int32(1))
			(**(**TSubstContext)(__ccgo_up(bp))).FpParse = pParse
			(**(**TSubstContext)(__ccgo_up(bp))).FiTable = (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor
			(**(**TSubstContext)(__ccgo_up(bp))).FiNewTable = (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor
			(**(**TSubstContext)(__ccgo_up(bp))).FisOuterJoin = 0
			(**(**TSubstContext)(__ccgo_up(bp))).FnSelDepth = 0
			(**(**TSubstContext)(__ccgo_up(bp))).FpEList = (*TSelect)(unsafe.Pointer(pSubq)).FpEList
			(**(**TSubstContext)(__ccgo_up(bp))).FpCList = _findLeftmostExprlist(tls, pSubq)
			pNew = _substExpr(tls, bp, pNew)
			if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 && int32((*TExpr)(unsafe.Pointer(pNew)).Fop) == int32(TK_IN) && (*TExpr)(unsafe.Pointer(pNew)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
				**(**Tu32)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pNew + 20)) + 4)) |= uint32(SF_ClonedRhsIn)
				**(**Tu32)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pWhere + 20)) + 4)) |= uint32(SF_ClonedRhsIn)
			}
			if (*TSelect)(unsafe.Pointer(pSubq)).FpWin != 0 && 0 == _pushDownWindowCheck(tls, pParse, pSubq, pNew) {
				/* Restriction 6c has prevented push-down in this case */
				_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pNew)
				nChng = nChng - 1
				break
			}
			if (*TSelect)(unsafe.Pointer(pSubq)).FselFlags&uint32(SF_Aggregate) != 0 {
				(*TSelect)(unsafe.Pointer(pSubq)).FpHaving = _sqlite3ExprAnd(tls, pParse, (*TSelect)(unsafe.Pointer(pSubq)).FpHaving, pNew)
			} else {
				(*TSelect)(unsafe.Pointer(pSubq)).FpWhere = _sqlite3ExprAnd(tls, pParse, (*TSelect)(unsafe.Pointer(pSubq)).FpWhere, pNew)
			}
			pSubq = (*TSelect)(unsafe.Pointer(pSubq)).FpPrior
		}
	}
	return nChng
}

// C documentation
//
//	/*
//	** Generate code that will push the record in registers regData
//	** through regData+nData-1 onto the sorter.
//	*/
func _pushOntoSorter(tls *libc.TLS, pParse uintptr, pSort uintptr, pSelect uintptr, regData int32, regOrigData int32, nData int32, nPrefixReg int32) {
	var addrFirst, addrJmp, bSeq, iCsr, iLimit, iSkip, nBase, nExpr, nKey, nOBSat, op, regBase, regPrevKey, regRecord, v1 int32
	var pKI, pOp, v, v4 uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrFirst, addrJmp, bSeq, iCsr, iLimit, iSkip, nBase, nExpr, nKey, nOBSat, op, pKI, pOp, regBase, regPrevKey, regRecord, v, v1, v4
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Stmt under construction */
	bSeq = libc.BoolInt32(int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&int32(SORTFLAG_UseSorter) == 0)
	nExpr = (*TExprList)(unsafe.Pointer((*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy)).FnExpr /* No. of ORDER BY terms */
	nBase = nExpr + bSeq + nData                                                              /* Regs for sorter record */
	regRecord = 0                                                                             /* Assembled sorter record */
	nOBSat = (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat                                       /* LIMIT counter */
	iSkip = 0                                                                                 /* End of the sorter insert loop */
	/* Three cases:
	 **   (1) The data to be sorted has already been packed into a Record
	 **       by a prior OP_MakeRecord.  In this case nData==1 and regData
	 **       will be completely unrelated to regOrigData.
	 **   (2) All output columns are included in the sort record.  In that
	 **       case regData==regOrigData.
	 **   (3) Some output columns are omitted from the sort record due to
	 **       the SQLITE_ENABLE_SORTER_REFERENCES optimization, or due to the
	 **       SQLITE_ECEL_OMITREF optimization, or due to the
	 **       SortCtx.pDeferredRowLoad optimization.  In any of these cases
	 **       regOrigData is 0 to prevent this routine from trying to copy
	 **       values that might not yet exist.
	 */
	if nPrefixReg != 0 {
		regBase = regData - nPrefixReg
	} else {
		regBase = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += nBase
	}
	if (*TSelect)(unsafe.Pointer(pSelect)).FiOffset != 0 {
		v1 = (*TSelect)(unsafe.Pointer(pSelect)).FiOffset + int32(1)
	} else {
		v1 = (*TSelect)(unsafe.Pointer(pSelect)).FiLimit
	}
	iLimit = v1
	(*TSortCtx)(unsafe.Pointer(pSort)).FlabelDone = _sqlite3VdbeMakeLabel(tls, pParse)
	if regOrigData != 0 {
		v1 = int32(SQLITE_ECEL_REF)
	} else {
		v1 = 0
	}
	_sqlite3ExprCodeExprList(tls, pParse, (*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy, regBase, regOrigData, uint8(int32(SQLITE_ECEL_DUP)|v1))
	if bSeq != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Sequence), (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor, regBase+nExpr)
	}
	if nPrefixReg == 0 && nData > 0 {
		_sqlite3ExprCodeMove(tls, pParse, regData, regBase+nExpr+bSeq, nData)
	}
	if nOBSat > 0 { /* Original KeyInfo on the sorter table */
		regRecord = _makeSorterRecord(tls, pParse, pSort, pSelect, regBase, nBase)
		regPrevKey = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat
		nKey = nExpr - (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat + bSeq
		if bSeq != 0 {
			addrFirst = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), regBase+nExpr)
		} else {
			addrFirst = _sqlite3VdbeAddOp1(tls, v, int32(OP_SequenceTest), (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor)
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Compare), regPrevKey, regBase, (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat)
		pOp = _sqlite3VdbeGetOp(tls, v, (*TSortCtx)(unsafe.Pointer(pSort)).FaddrSortIndex)
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
			return
		}
		(*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = nKey + nData
		pKI = *(*uintptr)(unsafe.Pointer(pOp + 16))
		libc.Xmemset(tls, (*TKeyInfo)(unsafe.Pointer(pKI)).FaSortFlags, 0, uint32((*TKeyInfo)(unsafe.Pointer(pKI)).FnKeyField)) /* Makes OP_Jump testable */
		_sqlite3VdbeChangeP4(tls, v, -int32(1), pKI, -int32(9))
		*(*uintptr)(unsafe.Pointer(pOp + 16)) = _sqlite3KeyInfoFromExprList(tls, pParse, (*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy, nOBSat, int32((*TKeyInfo)(unsafe.Pointer(pKI)).FnAllField)-int32((*TKeyInfo)(unsafe.Pointer(pKI)).FnKeyField)-int32(1))
		pOp = uintptr(0) /* Ensure pOp not used after sqlite3VdbeAddOp3() */
		addrJmp = _sqlite3VdbeCurrentAddr(tls, v)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addrJmp+int32(1), 0, addrJmp+int32(1))
		(*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut = _sqlite3VdbeMakeLabel(tls, pParse)
		v4 = pParse + 48
		*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
		v1 = *(*int32)(unsafe.Pointer(v4))
		(*TSortCtx)(unsafe.Pointer(pSort)).FregReturn = v1
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TSortCtx)(unsafe.Pointer(pSort)).FregReturn, (*TSortCtx)(unsafe.Pointer(pSort)).FlabelBkOut)
		_sqlite3VdbeAddOp1(tls, v, int32(OP_ResetSorter), (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor)
		if iLimit != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IfNot), iLimit, (*TSortCtx)(unsafe.Pointer(pSort)).FlabelDone)
		}
		_sqlite3VdbeJumpHere(tls, v, addrFirst)
		_sqlite3ExprCodeMove(tls, pParse, regBase, regPrevKey, (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat)
		_sqlite3VdbeJumpHere(tls, v, addrJmp)
	}
	if iLimit != 0 {
		/* At this point the values for the new sorter entry are stored
		 ** in an array of registers. They need to be composed into a record
		 ** and inserted into the sorter if either (a) there are currently
		 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
		 ** the largest record currently in the sorter. If (b) is true and there
		 ** are already LIMIT+OFFSET items in the sorter, delete the largest
		 ** entry before inserting the new one. This way there are never more
		 ** than LIMIT+OFFSET items in the sorter.
		 **
		 ** If the new record does not need to be inserted into the sorter,
		 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
		 ** value is not zero, then it is a label of where to jump.  Otherwise,
		 ** just bypass the row insert logic.  See the header comment on the
		 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
		 */
		iCsr = (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor
		_sqlite3VdbeAddOp2(tls, v, int32(OP_IfNotZero), iLimit, _sqlite3VdbeCurrentAddr(tls, v)+int32(4))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Last), iCsr, 0)
		iSkip = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxLE), iCsr, 0, regBase+nOBSat, nExpr-nOBSat)
		_sqlite3VdbeAddOp1(tls, v, int32(OP_Delete), iCsr)
	}
	if regRecord == 0 {
		regRecord = _makeSorterRecord(tls, pParse, pSort, pSelect, regBase, nBase)
	}
	if int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&int32(SORTFLAG_UseSorter) != 0 {
		op = int32(OP_SorterInsert)
	} else {
		op = int32(OP_IdxInsert)
	}
	_sqlite3VdbeAddOp4Int(tls, v, op, (*TSortCtx)(unsafe.Pointer(pSort)).FiECursor, regRecord, regBase+nOBSat, nBase-nOBSat)
	if iSkip != 0 {
		if (*TSortCtx)(unsafe.Pointer(pSort)).FlabelOBLopt != 0 {
			v1 = (*TSortCtx)(unsafe.Pointer(pSort)).FlabelOBLopt
		} else {
			v1 = _sqlite3VdbeCurrentAddr(tls, v)
		}
		_sqlite3VdbeChangeP2(tls, v, iSkip, v1)
	}
}

// C documentation
//
//	/*
//	** Query to see if Btree handle p may obtain a lock of type eLock
//	** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
//	** SQLITE_OK if the lock may be obtained (by calling
//	** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
//	*/
func _querySharedCacheTableLock(tls *libc.TLS, p uintptr, iTab TPgno, eLock Tu8) (r int32) {
	var pBt, pIter, v2 uintptr
	_, _, _ = pBt, pIter, v2
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt
	/* If requesting a write-lock, then the Btree must have an open write
	 ** transaction on this file. And, obviously, for this to be so there
	 ** must be an open write transaction on the file itself.
	 */
	/* This routine is a no-op if the shared-cache is not enabled */
	if !((*TBtree)(unsafe.Pointer(p)).Fsharable != 0) {
		return SQLITE_OK
	}
	/* If some other connection is holding an exclusive lock, the
	 ** requested lock may not be obtained.
	 */
	if (*TBtShared)(unsafe.Pointer(pBt)).FpWriter != p && int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_EXCLUSIVE) != 0 {
		_sqlite3ConnectionBlocked(tls, (*TBtree)(unsafe.Pointer(p)).Fdb, (*TBtree)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpWriter)).Fdb)
		return libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
	}
	pIter = (*TBtShared)(unsafe.Pointer(pBt)).FpLock
	for {
		if !(pIter != 0) {
			break
		}
		/* The condition (pIter->eLock!=eLock) in the following if(...)
		 ** statement is a simplification of:
		 **
		 **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
		 **
		 ** since we know that if eLock==WRITE_LOCK, then no other connection
		 ** may hold a WRITE_LOCK on any table in this file (since there can
		 ** only be a single writer).
		 */
		if (*TBtLock)(unsafe.Pointer(pIter)).FpBtree != p && (*TBtLock)(unsafe.Pointer(pIter)).FiTable == iTab && int32((*TBtLock)(unsafe.Pointer(pIter)).FeLock) != int32(eLock) {
			_sqlite3ConnectionBlocked(tls, (*TBtree)(unsafe.Pointer(p)).Fdb, (*TBtree)(unsafe.Pointer((*TBtLock)(unsafe.Pointer(pIter)).FpBtree)).Fdb)
			if int32(eLock) == int32(WRITE_LOCK) {
				v2 = pBt + 24
				*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(BTS_PENDING))
			}
			return libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		}
		goto _1
	_1:
		;
		pIter = (*TBtLock)(unsafe.Pointer(pIter)).FpNext
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Implementation of the QUOTE() function.
//	**
//	** The quote(X) function returns the text of an SQL literal which is the
//	** value of its argument suitable for inclusion into an SQL statement.
//	** Strings are surrounded by single-quotes with escapes on interior quotes
//	** as needed. BLOBs are encoded as hexadecimal literals. Strings with
//	** embedded NUL characters cannot be represented as string literals in SQL
//	** and hence the returned string literal is truncated prior to the first NUL.
//	**
//	** If sqlite3_user_data() is non-zero, then the UNISTR_QUOTE() function is
//	** implemented instead.  The difference is that UNISTR_QUOTE() uses the
//	** UNISTR() function to escape control characters.
//	*/
func _quoteFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db uintptr
	var _ /* str at bp+0 */ Tsqlite3_str
	_ = db
	db = Xsqlite3_context_db_handle(tls, context)
	_ = argc
	_sqlite3StrAccumInit(tls, bp, db, uintptr(0), 0, **(**int32)(__ccgo_up(db + 120)))
	_sqlite3QuoteValue(tls, bp, **(**uintptr)(__ccgo_up(argv)), int32(Xsqlite3_user_data(tls, context)))
	Xsqlite3_result_text(tls, context, _sqlite3StrAccumFinish(tls, bp), int32((**(**Tsqlite3_str)(__ccgo_up(bp))).FnChar), __ccgo_fp(_sqlite3RowSetClear))
	if int32((**(**Tsqlite3_str)(__ccgo_up(bp))).FaccError) != SQLITE_OK {
		Xsqlite3_result_null(tls, context)
		Xsqlite3_result_error_code(tls, context, int32((**(**Tsqlite3_str)(__ccgo_up(bp))).FaccError))
	}
}

// C documentation
//
//	/*
//	** Compare two constraint names.
//	**
//	** Summary:   *pRes := zQuote != zCmp
//	**
//	** Details:
//	** Compare the (possibly quoted) constraint name zQuote[0..nQuote-1]
//	** against zCmp[].  Write zero into *pRes if they are the same and
//	** non-zero if they differ.  Normally return SQLITE_OK, except if there
//	** is an OOM, set the OOM error condition on ctx and return SQLITE_NOMEM.
//	*/
func _quotedCompare(tls *libc.TLS, ctx uintptr, t int32, zQuote uintptr, nQuote int32, zCmp uintptr, pRes uintptr) (r int32) {
	var zCopy uintptr
	_ = zCopy
	zCopy = uintptr(0) /* De-quoted, zero-terminated copy of zQuote[] */
	if t == int32(TK_ILLEGAL) {
		**(**int32)(__ccgo_up(pRes)) = int32(1)
		return SQLITE_OK
	}
	zCopy = _sqlite3MallocZero(tls, uint64(nQuote+int32(1)))
	if zCopy == uintptr(0) {
		Xsqlite3_result_error_nomem(tls, ctx)
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemcpy(tls, zCopy, zQuote, uint32(nQuote))
	_sqlite3Dequote(tls, zCopy)
	**(**int32)(__ccgo_up(pRes)) = Xsqlite3_stricmp(tls, zCopy, zCmp)
	Xsqlite3_free(tls, zCopy)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
//	** there is room for at least nCol elements. If an OOM occurs, store an
//	** error code in the RBU handle passed as the first argument.
//	*/
func _rbuAllocateIterArrays(tls *libc.TLS, p uintptr, pIter uintptr, nCol int32) {
	var azNew uintptr
	var nByte Tsqlite3_int64
	_, _ = azNew, nByte
	nByte = int64((libc.Uint32FromInt32(2)*libc.Uint32FromInt64(4) + libc.Uint32FromInt64(4) + libc.Uint32FromInt32(3)*libc.Uint32FromInt64(1)) * uint32(nCol))
	azNew = _rbuMalloc(tls, p, nByte)
	if azNew != 0 {
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol = azNew
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType = azNew + uintptr(nCol)*4
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder = (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(nCol)*4
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk = (*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(nCol)*4
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull = (*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(nCol)
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed = (*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull + uintptr(nCol)
	}
}

// C documentation
//
//	/*
//	** Called when iAmt bytes are read from offset iOff of the wal file while
//	** the rbu object is in capture mode. Record the frame number of the frame
//	** being read in the aFrame[] array.
//	*/
func _rbuCaptureWalRead(tls *libc.TLS, pRbu uintptr, iOff Ti64, iAmt int32) (r int32) {
	var aNew uintptr
	var iFrame, mReq Tu32
	var nNew, v1 int32
	_, _, _, _, _ = aNew, iFrame, mReq, nNew, v1
	mReq = uint32(libc.Int32FromInt32(1)<<libc.Int32FromInt32(WAL_LOCK_WRITE) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(WAL_LOCK_CKPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(WAL_LOCK_READ0))
	if (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FmLock != mReq {
		(*Tsqlite3rbu)(unsafe.Pointer(pRbu)).Frc = int32(SQLITE_BUSY)
		return libc.Int32FromInt32(SQLITE_NOTICE) | libc.Int32FromInt32(3)<<libc.Int32FromInt32(8)
	}
	(*Tsqlite3rbu)(unsafe.Pointer(pRbu)).Fpgsz = iAmt
	if (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnFrame == (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnFrameAlloc {
		if (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnFrameAlloc != 0 {
			v1 = (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnFrameAlloc
		} else {
			v1 = int32(64)
		}
		nNew = v1 * int32(2)
		aNew = Xsqlite3_realloc64(tls, (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FaFrame, uint64(uint32(nNew)*uint32(8)))
		if aNew == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		(*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FaFrame = aNew
		(*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnFrameAlloc = nNew
	}
	iFrame = uint32((iOff-libc.Int64FromInt32(32))/int64(iAmt+libc.Int32FromInt32(24))) + uint32(1)
	if (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FiMaxFrame < iFrame {
		(*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FiMaxFrame = iFrame
	}
	(**(**TRbuFrame)(__ccgo_up((*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FaFrame + uintptr((*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnFrame)*8))).FiWalFrame = iFrame
	(**(**TRbuFrame)(__ccgo_up((*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FaFrame + uintptr((*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnFrame)*8))).FiDbPage = uint32(0)
	(*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnFrame = (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FnFrame + 1
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** The second argument passed to this function is the name of a PRAGMA
//	** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
//	** This function executes the following on sqlite3rbu.dbRbu:
//	**
//	**   "PRAGMA main.$zPragma"
//	**
//	** where $zPragma is the string passed as the second argument, then
//	** on sqlite3rbu.dbMain:
//	**
//	**   "PRAGMA main.$zPragma = $val"
//	**
//	** where $val is the value returned by the first PRAGMA invocation.
//	**
//	** In short, it copies the value  of the specified PRAGMA setting from
//	** dbRbu to dbMain.
//	*/
func _rbuCopyPragma(tls *libc.TLS, p uintptr, zPragma uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var _ /* pPragma at bp+0 */ uintptr
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+35212, libc.VaList(bp+16, zPragma)))
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+35227, libc.VaList(bp+16, zPragma, Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), 0)))
		}
		_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
	}
}

// C documentation
//
//	/*
//	** If an error has already occurred when this function is called, it
//	** immediately returns zero (without doing any work). Or, if an error
//	** occurs during the execution of this function, it sets the error code
//	** in the sqlite3rbu object indicated by the first argument and returns
//	** zero.
//	**
//	** The iterator passed as the second argument is guaranteed to point to
//	** a table (not an index) when this function is called. This function
//	** attempts to create any imposter table required to write to the main
//	** table b-tree of the table before returning. Non-zero is returned if
//	** an imposter table are created, or zero otherwise.
//	**
//	** An imposter table is required in all cases except RBU_PK_VTAB. Only
//	** virtual tables are written to directly. The imposter table has the
//	** same schema as the actual target table (less any UNIQUE constraints).
//	** More precisely, the "same schema" means the same columns, types,
//	** collation sequences. For tables that do not have an external PRIMARY
//	** KEY, it also means the same PRIMARY KEY declaration.
//	*/
func _rbuCreateImposterTable(tls *libc.TLS, p uintptr, pIter uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var iCol, tnum int32
	var zCol, zComma, zPk, zPk1, zSql, v2 uintptr
	var _ /* zColl at bp+0 */ uintptr
	_, _, _, _, _, _, _, _ = iCol, tnum, zCol, zComma, zPk, zPk1, zSql, v2
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType != int32(RBU_PK_VTAB) {
		tnum = (*TRbuObjIter)(unsafe.Pointer(pIter)).FiTnum
		zComma = __ccgo_ts + 1711
		zSql = uintptr(0)
		Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, 0, int32(1)))
		iCol = 0
		for {
			if !((*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && iCol < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) {
				break
			}
			zPk = __ccgo_ts + 1711
			zCol = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iCol)*4))
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_table_column_metadata(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zCol, uintptr(0), bp, uintptr(0), uintptr(0), uintptr(0))
			if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_IPK) && **(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(iCol))) != 0 {
				/* If the target table column is an "INTEGER PRIMARY KEY", add
				 ** "PRIMARY KEY" to the imposter table column declaration. */
				zPk = __ccgo_ts + 33263
			}
			if **(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull + uintptr(iCol))) != 0 {
				v2 = __ccgo_ts + 33276
			} else {
				v2 = __ccgo_ts + 1711
			}
			zSql = _rbuMPrintf(tls, p, __ccgo_ts+33286, libc.VaList(bp+16, zSql, zComma, zCol, **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iCol)*4)), zPk, **(**uintptr)(__ccgo_up(bp)), v2))
			zComma = __ccgo_ts + 17436
			goto _1
		_1:
			;
			iCol = iCol + 1
		}
		if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_WITHOUT_ROWID) {
			zPk1 = _rbuWithoutRowidPK(tls, p, pIter)
			if zPk1 != 0 {
				zSql = _rbuMPrintf(tls, p, __ccgo_ts+33313, libc.VaList(bp+16, zSql, zPk1))
			}
		}
		Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, int32(1), tnum))
		if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_WITHOUT_ROWID) {
			v2 = __ccgo_ts + 33320
		} else {
			v2 = __ccgo_ts + 1711
		}
		_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+33335, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zSql, v2))
		Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, 0, 0))
	}
}

// C documentation
//
//	/*
//	** This function creates the second imposter table used when writing to
//	** a table b-tree where the table has an external primary key. If the
//	** iterator passed as the second argument does not currently point to
//	** a table (not index) with an external primary key, this function is a
//	** no-op.
//	**
//	** Assuming the iterator does point to a table with an external PK, this
//	** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
//	** used to access that PK index. For example, if the target table is
//	** declared as follows:
//	**
//	**   CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
//	**
//	** then the imposter table schema is:
//	**
//	**   CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
//	**
//	*/
func _rbuCreateImposterTable2(tls *libc.TLS, p uintptr, pIter uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var bDesc, bKey, iCid, tnum int32
	var zCollate, zCols, zComma, zIdx, zPk, v1 uintptr
	var _ /* pQuery at bp+0 */ uintptr
	var _ /* pXInfo at bp+4 */ uintptr
	_, _, _, _, _, _, _, _, _, _ = bDesc, bKey, iCid, tnum, zCollate, zCols, zComma, zIdx, zPk, v1
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) {
		tnum = (*TRbuObjIter)(unsafe.Pointer(pIter)).FiPkTnum /* Root page of PK index */
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)             /* SELECT name ... WHERE rootpage = $tnum */
		zIdx = uintptr(0)                                     /* Name of PK index */
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)         /* PRAGMA main.index_xinfo = $zIdx */
		zComma = __ccgo_ts + 1711
		zCols = uintptr(0) /* Used to build up list of table cols */
		zPk = uintptr(0)   /* Used to build up table PK declaration */
		/* Figure out the name of the primary key index for the current table.
		 ** This is needed for the argument to "PRAGMA index_xinfo". Set
		 ** zIdx to point to a nul-terminated string containing this name. */
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, __ccgo_ts+33104)
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			Xsqlite3_bind_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(1), tnum)
			if int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
				zIdx = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), 0)
			}
		}
		if zIdx != 0 {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32256, libc.VaList(bp+16, zIdx)))
		}
		_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
		for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4))) {
			bKey = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp + 4)), int32(5))
			if bKey != 0 {
				iCid = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp + 4)), int32(1))
				bDesc = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp + 4)), int32(3))
				zCollate = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp + 4)), int32(4))
				zCols = _rbuMPrintf(tls, p, __ccgo_ts+33154, libc.VaList(bp+16, zCols, zComma, iCid, **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iCid)*4)), zCollate))
				if bDesc != 0 {
					v1 = __ccgo_ts + 32496
				} else {
					v1 = __ccgo_ts + 1711
				}
				zPk = _rbuMPrintf(tls, p, __ccgo_ts+33176, libc.VaList(bp+16, zPk, zComma, iCid, v1))
				zComma = __ccgo_ts + 17436
			}
		}
		zCols = _rbuMPrintf(tls, p, __ccgo_ts+33186, libc.VaList(bp+16, zCols))
		_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp + 4)))
		Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, int32(1), tnum))
		_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+33201, libc.VaList(bp+16, zCols, zPk))
		Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, 0, 0))
	}
}

// C documentation
//
//	/*
//	** The RBU handle passed as the only argument has just been opened and
//	** the state database is empty. If this RBU handle was opened for an
//	** RBU vacuum operation, create the schema in the target db.
//	*/
func _rbuCreateTargetSchema(tls *libc.TLS, p uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i int32
	var zSql uintptr
	var _ /* pInsert at bp+4 */ uintptr
	var _ /* pSql at bp+0 */ uintptr
	_, _ = i, zSql
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+35247, uintptr(0), uintptr(0), p+36)
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, __ccgo_ts+35272)
	}
	for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) == int32(SQLITE_ROW) {
		zSql = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), 0)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, zSql, uintptr(0), uintptr(0), p+36)
	}
	_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK {
		return
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, __ccgo_ts+35380)
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, __ccgo_ts+35445)
	}
	for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) == int32(SQLITE_ROW) {
		i = 0
		for {
			if !(i < int32(5)) {
				break
			}
			Xsqlite3_bind_value(tls, **(**uintptr)(__ccgo_up(bp + 4)), i+int32(1), Xsqlite3_column_value(tls, **(**uintptr)(__ccgo_up(bp)), i))
			goto _1
		_1:
			;
			i = i + 1
		}
		Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4)))
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp + 4)))
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+35489, uintptr(0), uintptr(0), p+36)
	}
	_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
	_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp + 4)))
}

// C documentation
//
//	/*
//	** Apply a delta.
//	**
//	** The output buffer should be big enough to hold the whole output
//	** file and a NUL terminator at the end.  The delta_output_size()
//	** routine will determine this size for you.
//	**
//	** The delta string should be null-terminated.  But the delta string
//	** may contain embedded NUL characters (if the input and output are
//	** binary files) so we also have to pass in the length of the delta in
//	** the lenDelta parameter.
//	**
//	** This function returns the size of the output file in bytes (excluding
//	** the final NUL terminator character).  Except, if the delta string is
//	** malformed or intended for use with a source file other than zSrc,
//	** then this routine returns -1.
//	**
//	** Refer to the delta_create() documentation above for a description
//	** of the delta file format.
//	*/
func _rbuDeltaApply(tls *libc.TLS, zSrc uintptr, lenSrc int32, _zDelta uintptr, _lenDelta int32, zOut uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*uintptr)(unsafe.Pointer(bp)) = _zDelta
	*(*int32)(unsafe.Pointer(bp + 4)) = _lenDelta
	var cnt, limit, ofst, total uint32
	_, _, _, _ = cnt, limit, ofst, total
	total = uint32(0)
	limit = _rbuDeltaGetInt(tls, bp, bp+4)
	if **(**int32)(__ccgo_up(bp + 4)) <= 0 || int32(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))) != int32('\n') {
		/* ERROR: size integer not terminated by "\n" */
		return -int32(1)
	}
	**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
	**(**int32)(__ccgo_up(bp + 4)) = **(**int32)(__ccgo_up(bp + 4)) - 1
	for **(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)))) != 0 && **(**int32)(__ccgo_up(bp + 4)) > 0 {
		cnt = _rbuDeltaGetInt(tls, bp, bp+4)
		if **(**int32)(__ccgo_up(bp + 4)) <= 0 {
			return -int32(1)
		}
		switch int32(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))) {
		case int32('@'):
			**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
			**(**int32)(__ccgo_up(bp + 4)) = **(**int32)(__ccgo_up(bp + 4)) - 1
			ofst = _rbuDeltaGetInt(tls, bp, bp+4)
			if **(**int32)(__ccgo_up(bp + 4)) > 0 || int32(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))) != int32(',') {
				/* ERROR: copy command not terminated by ',' */
				return -int32(1)
			}
			**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
			**(**int32)(__ccgo_up(bp + 4)) = **(**int32)(__ccgo_up(bp + 4)) - 1
			total = total + cnt
			if total > limit {
				/* ERROR: copy exceeds output file size */
				return -int32(1)
			}
			if uint64(ofst)+uint64(cnt) > uint64(lenSrc) {
				/* ERROR: copy extends past end of input */
				return -int32(1)
			}
			libc.Xmemcpy(tls, zOut, zSrc+uintptr(ofst), cnt)
			zOut = zOut + uintptr(cnt)
		case int32(':'):
			**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
			**(**int32)(__ccgo_up(bp + 4)) = **(**int32)(__ccgo_up(bp + 4)) - 1
			total = total + cnt
			if total > limit {
				/* ERROR:  insert command gives an output larger than predicted */
				return -int32(1)
			}
			if int64(cnt) > int64(**(**int32)(__ccgo_up(bp + 4))) {
				/* ERROR: insert count exceeds size of delta */
				return -int32(1)
			}
			libc.Xmemcpy(tls, zOut, **(**uintptr)(__ccgo_up(bp)), cnt)
			zOut = zOut + uintptr(cnt)
			**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + uintptr(cnt)
			**(**int32)(__ccgo_up(bp + 4)) = int32(uint32(**(**int32)(__ccgo_up(bp + 4))) - cnt)
		case int32(';'):
			**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
			**(**int32)(__ccgo_up(bp + 4)) = **(**int32)(__ccgo_up(bp + 4)) - 1
			**(**int8)(__ccgo_up(zOut)) = 0
			if total != limit {
				/* ERROR: generated size does not match predicted size */
				return -int32(1)
			}
			return int32(total)
		default:
			/* ERROR: unknown delta operator */
			return -int32(1)
		}
	}
	/* ERROR: unterminated delta */
	return -int32(1)
}

// C documentation
//
//	/*
//	** Read bytes from *pz and convert them into a positive integer.  When
//	** finished, leave *pz pointing to the first character past the end of
//	** the integer.  The *pLen parameter holds the length of the string
//	** in *pz and is decremented once for each character in the integer.
//	*/
func _rbuDeltaGetInt(tls *libc.TLS, pz uintptr, pLen uintptr) (r uint32) {
	var c, v1 int32
	var v uint32
	var z, zEnd uintptr
	var v2 bool
	_, _, _, _, _, _ = c, v, z, zEnd, v1, v2
	v = uint32(0)
	z = **(**uintptr)(__ccgo_up(pz))
	zEnd = z + uintptr(**(**int32)(__ccgo_up(pLen)))
	for {
		if v2 = z < zEnd; v2 {
			v1 = int32(_zValue[**(**uint8)(__ccgo_up(z))])
			c = v1
		}
		if !(v2 && v1 >= 0) {
			break
		}
		v = v<<int32(6) + uint32(c)
		z = z + 1
	}
	**(**int32)(__ccgo_up(pLen)) -= int32(z) - int32(**(**uintptr)(__ccgo_up(pz)))
	**(**uintptr)(__ccgo_up(pz)) = z
	return v
}

func _rbuDeltaOutputSize(tls *libc.TLS, _zDelta uintptr, _lenDelta int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*uintptr)(unsafe.Pointer(bp)) = _zDelta
	*(*int32)(unsafe.Pointer(bp + 4)) = _lenDelta
	var size int32
	_ = size
	size = int32(_rbuDeltaGetInt(tls, bp, bp+4))
	if **(**int32)(__ccgo_up(bp + 4)) <= 0 || int32(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))) != int32('\n') {
		/* ERROR: size integer not terminated by "\n" */
		return -int32(1)
	}
	return size
}

/*
** End of code taken from fossil.
*************************************************************************/

// C documentation
//
//	/*
//	** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
//	** then edit any error message string so as to remove all occurrences of
//	** the pattern "rbu_imp_[0-9]*".
//	*/
func _rbuEditErrmsg(tls *libc.TLS, p uintptr) {
	var i uint32
	var nDel int32
	var nErrmsg Tsize_t
	_, _, _ = i, nDel, nErrmsg
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == int32(SQLITE_CONSTRAINT) && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg != 0 {
		nErrmsg = libc.Xstrlen(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg)
		i = uint32(0)
		for {
			if !(i < nErrmsg-uint32(8)) {
				break
			}
			if libc.Xmemcmp(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i), __ccgo_ts+33936, uint32(8)) == 0 {
				nDel = int32(8)
				for int32(**(**int8)(__ccgo_up((*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg + uintptr(i+uint32(nDel))))) >= int32('0') && int32(**(**int8)(__ccgo_up((*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg + uintptr(i+uint32(nDel))))) <= int32('9') {
					nDel = nDel + 1
				}
				libc.Xmemmove(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i), (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i+uint32(nDel)), nErrmsg+uint32(1)-i-uint32(nDel))
				nErrmsg = nErrmsg - uint32(nDel)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
}

// C documentation
//
//	/*
//	** Implementation of SQL scalar function rbu_fossil_delta().
//	**
//	** This function applies a fossil delta patch to a blob. Exactly two
//	** arguments must be passed to this function. The first is the blob to
//	** patch and the second the patch to apply. If no error occurs, this
//	** function returns the patched blob.
//	*/
func _rbuFossilDeltaFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var aDelta, aOrig, aOut uintptr
	var nDelta, nOrig, nOut, nOut2 int32
	_, _, _, _, _, _, _ = aDelta, aOrig, aOut, nDelta, nOrig, nOut, nOut2
	_ = argc
	nOrig = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
	aOrig = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv)))
	nDelta = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	aDelta = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	/* Figure out the size of the output */
	nOut = _rbuDeltaOutputSize(tls, aDelta, nDelta)
	if nOut < 0 {
		Xsqlite3_result_error(tls, context, __ccgo_ts+31508, -int32(1))
		return
	}
	aOut = Xsqlite3_malloc64(tls, uint64(int64(nOut)+int64(1)))
	if aOut == uintptr(0) {
		Xsqlite3_result_error_nomem(tls, context)
	} else {
		nOut2 = _rbuDeltaApply(tls, aOrig, nOrig, aDelta, nDelta, aOut)
		if nOut2 != nOut {
			Xsqlite3_free(tls, aOut)
			Xsqlite3_result_error(tls, context, __ccgo_ts+31508, -int32(1))
		} else {
			Xsqlite3_result_blob(tls, context, aOut, nOut, __ccgo_fp(Xsqlite3_free))
		}
	}
}

// C documentation
//
//	/*
//	** Set output variable *ppStmt to point to an UPDATE statement that may
//	** be used to update the imposter table for the main table b-tree of the
//	** table object that pIter currently points to, assuming that the
//	** rbu_control column of the data_xyz table contains zMask.
//	**
//	** If the zMask string does not specify any columns to update, then this
//	** is not an error. Output variable *ppStmt is set to NULL in this case.
//	*/
func _rbuGetUpdateStmt(tls *libc.TLS, p uintptr, pIter uintptr, zMask uintptr, ppStmt uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var nUp int32
	var pUp, pp, zPrefix, zSet, zUpdate, zWhere uintptr
	_, _, _, _, _, _, _ = nUp, pUp, pp, zPrefix, zSet, zUpdate, zWhere
	pUp = uintptr(0)
	nUp = 0
	/* In case an error occurs */
	**(**uintptr)(__ccgo_up(ppStmt)) = uintptr(0)
	/* Search for an existing statement. If one is found, shift it to the front
	 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
	 ** to the number of statements currently in the cache and pUp to the
	 ** last object in the list.  */
	pp = pIter + 104
	for {
		if !(**(**uintptr)(__ccgo_up(pp)) != 0) {
			break
		}
		pUp = **(**uintptr)(__ccgo_up(pp))
		if libc.Xstrcmp(tls, (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FzMask, zMask) == 0 {
			**(**uintptr)(__ccgo_up(pp)) = (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpNext
			(*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpNext = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate
			(*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate = pUp
			**(**uintptr)(__ccgo_up(ppStmt)) = (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpUpdate
			return SQLITE_OK
		}
		nUp = nUp + 1
		goto _1
	_1:
		;
		pp = **(**uintptr)(__ccgo_up(pp)) + 8
	}
	if nUp >= int32(SQLITE_RBU_UPDATE_CACHESIZE) {
		pp = pIter + 104
		for {
			if !(**(**uintptr)(__ccgo_up(pp)) != pUp) {
				break
			}
			goto _2
		_2:
			;
			pp = **(**uintptr)(__ccgo_up(pp)) + 8
		}
		**(**uintptr)(__ccgo_up(pp)) = uintptr(0)
		Xsqlite3_finalize(tls, (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpUpdate)
		(*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpUpdate = uintptr(0)
	} else {
		pUp = _rbuMalloc(tls, p, int64(uint32(12)+uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)+uint32(1)))
	}
	if pUp != 0 {
		zWhere = _rbuObjIterGetWhere(tls, p, pIter)
		zSet = _rbuObjIterGetSetlist(tls, p, pIter, zMask)
		zUpdate = uintptr(0)
		(*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FzMask = pUp + 1*12
		libc.Xmemcpy(tls, (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FzMask, zMask, uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol))
		(*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpNext = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate = pUp
		if zSet != 0 {
			zPrefix = __ccgo_ts + 1711
			if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType != int32(RBU_PK_VTAB) {
				zPrefix = __ccgo_ts + 33936
			}
			zUpdate = Xsqlite3_mprintf(tls, __ccgo_ts+34597, libc.VaList(bp+8, zPrefix, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zSet, zWhere))
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pUp+4, p+36, zUpdate)
			**(**uintptr)(__ccgo_up(ppStmt)) = (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpUpdate
		}
		Xsqlite3_free(tls, zWhere)
		Xsqlite3_free(tls, zSet)
	}
	return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
}

// C documentation
//
//	/*
//	** Increment the schema cookie of the main database opened by p->dbMain.
//	**
//	** Or, if this is an RBU vacuum, set the schema cookie of the main db
//	** opened by p->dbMain to one more than the schema cookie of the main
//	** db opened by p->dbRbu.
//	*/
func _rbuIncrSchemaCookie(tls *libc.TLS, p uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var dbread, v1 uintptr
	var iCookie int32
	var _ /* pStmt at bp+0 */ uintptr
	_, _, _ = dbread, iCookie, v1
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
			v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu
		} else {
			v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain
		}
		dbread = v1
		iCookie = int32(1000000)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, dbread, bp, p+36, __ccgo_ts+35003)
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			/* Coverage: it may be that this sqlite3_step() cannot fail. There
			 ** is already a transaction open, so the prepared statement cannot
			 ** throw an SQLITE_SCHEMA exception. The only database page the
			 ** statement reads is page 1, which is guaranteed to be in the cache.
			 ** And no memory allocations are required.  */
			if int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
				iCookie = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), 0)
			}
			_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+35025, libc.VaList(bp+16, iCookie+int32(1)))
		}
	}
}

// C documentation
//
//	/*
//	** This user-defined SQL function is invoked with a single argument - the
//	** name of a table expected to appear in the target database. It returns
//	** the number of auxilliary indexes on the table.
//	*/
func _rbuIndexCntFunc(tls *libc.TLS, pCtx uintptr, nVal int32, apVal uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, p, v1 uintptr
	var nIndex, rc int32
	var _ /* pStmt at bp+0 */ uintptr
	var _ /* zErrmsg at bp+4 */ uintptr
	_, _, _, _, _ = db, nIndex, p, rc, v1
	p = Xsqlite3_user_data(tls, pCtx)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
		v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu
	} else {
		v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain
	}
	db = v1
	_ = nVal
	rc = _prepareFreeAndCollectError(tls, db, bp, bp+4, Xsqlite3_mprintf(tls, __ccgo_ts+35578, libc.VaList(bp+16, Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(apVal))))))
	if rc != SQLITE_OK {
		Xsqlite3_result_error(tls, pCtx, **(**uintptr)(__ccgo_up(bp + 4)), -int32(1))
	} else {
		nIndex = 0
		if int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
			nIndex = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), 0)
		}
		rc = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		if rc == SQLITE_OK {
			Xsqlite3_result_int(tls, pCtx, nIndex)
		} else {
			Xsqlite3_result_error(tls, pCtx, Xsqlite3_errmsg(tls, db), -int32(1))
		}
	}
	Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 4)))
}

// C documentation
//
//	/*
//	** If the RBU database contains the rbu_count table, use it to initialize
//	** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
//	** is assumed to contain the same columns as:
//	**
//	**   CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
//	**
//	** There should be one row in the table for each data_xxx table in the
//	** database. The 'tbl' column should contain the name of a data_xxx table,
//	** and the cnt column the number of rows it contains.
//	**
//	** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
//	** for all rows in the rbu_count table, where nIndex is the number of
//	** indexes on the corresponding target database table.
//	*/
func _rbuInitPhaseOneSteps(tls *libc.TLS, p uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bExists int32
	var _ /* pStmt at bp+0 */ uintptr
	_ = bExists
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		bExists = 0 /* True if rbu_count exists */
		(*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep = int64(-int32(1))
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_create_function(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+35650, int32(1), int32(SQLITE_UTF8), p, __ccgo_fp(_rbuIndexCntFunc), uintptr(0), uintptr(0))
		/* Check for the rbu_count table. If it does not exist, or if an error
		 ** occurs, nPhaseOneStep will be left set to -1. */
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, __ccgo_ts+35664)
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			if int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
				bExists = int32(1)
			}
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && bExists != 0 {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, __ccgo_ts+35721)
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				if int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp)), 0)
				}
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
			}
		}
	}
}

// C documentation
//
//	/*
//	** Allocate an RbuState object and load the contents of the rbu_state
//	** table into it. Return a pointer to the new object. It is the
//	** responsibility of the caller to eventually free the object using
//	** sqlite3_free().
//	**
//	** If an error occurs, leave an error code and message in the rbu handle
//	** and return NULL.
//	*/
func _rbuLoadState(tls *libc.TLS, p uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pRet uintptr
	var rc2 int32
	var _ /* pStmt at bp+0 */ uintptr
	var _ /* rc at bp+4 */ int32
	_, _ = pRet, rc2
	pRet = uintptr(0)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	pRet = _rbuMalloc(tls, p, int64(64))
	if pRet == uintptr(0) {
		return uintptr(0)
	}
	**(**int32)(__ccgo_up(bp + 4)) = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+34627, libc.VaList(bp+16, p+24)))
	for **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
		switch Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), 0) {
		case int32(RBU_STATE_STAGE):
			(*TRbuState)(unsafe.Pointer(pRet)).FeStage = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
			if (*TRbuState)(unsafe.Pointer(pRet)).FeStage != int32(RBU_STAGE_OAL) && (*TRbuState)(unsafe.Pointer(pRet)).FeStage != int32(RBU_STAGE_MOVE) && (*TRbuState)(unsafe.Pointer(pRet)).FeStage != int32(RBU_STAGE_CKPT) {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_CORRUPT)
			}
		case int32(RBU_STATE_TBL):
			(*TRbuState)(unsafe.Pointer(pRet)).FzTbl = _rbuStrndup(tls, Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(1)), bp+4)
		case int32(RBU_STATE_IDX):
			(*TRbuState)(unsafe.Pointer(pRet)).FzIdx = _rbuStrndup(tls, Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(1)), bp+4)
		case int32(RBU_STATE_ROW):
			(*TRbuState)(unsafe.Pointer(pRet)).FnRow = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		case int32(RBU_STATE_PROGRESS):
			(*TRbuState)(unsafe.Pointer(pRet)).FnProgress = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		case int32(RBU_STATE_CKPT):
			(*TRbuState)(unsafe.Pointer(pRet)).FiWalCksum = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		case int32(RBU_STATE_COOKIE):
			(*TRbuState)(unsafe.Pointer(pRet)).FiCookie = uint32(Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(1)))
		case int32(RBU_STATE_OALSZ):
			(*TRbuState)(unsafe.Pointer(pRet)).FiOalSz = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		case int32(RBU_STATE_PHASEONESTEP):
			(*TRbuState)(unsafe.Pointer(pRet)).FnPhaseOneStep = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		case int32(RBU_STATE_DATATBL):
			(*TRbuState)(unsafe.Pointer(pRet)).FzDataTbl = _rbuStrndup(tls, Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(1)), bp+4)
		default:
			**(**int32)(__ccgo_up(bp + 4)) = int32(SQLITE_CORRUPT)
			break
		}
	}
	rc2 = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 4)) = rc2
	}
	(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = **(**int32)(__ccgo_up(bp + 4))
	return pRet
}

// C documentation
//
//	/*
//	** Attempt to allocate and return a pointer to a zeroed block of nByte
//	** bytes.
//	**
//	** If an error (i.e. an OOM condition) occurs, return NULL and leave an
//	** error code in the rbu handle passed as the first argument. Or, if an
//	** error has already occurred when this function is called, return NULL
//	** immediately without attempting the allocation or modifying the stored
//	** error code.
//	*/
func _rbuMalloc(tls *libc.TLS, p uintptr, nByte Tsqlite3_int64) (r uintptr) {
	var pRet uintptr
	_ = pRet
	pRet = uintptr(0)
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		pRet = Xsqlite3_malloc64(tls, uint64(nByte))
		if pRet == uintptr(0) {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
		} else {
			libc.Xmemset(tls, pRet, 0, uint32(nByte))
		}
	}
	return pRet
}

// C documentation
//
//	/*
//	** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
//	** on the database file. This proc moves the *-oal file to the *-wal path,
//	** then reopens the database file (this time in vanilla, non-oal, WAL mode).
//	** If an error occurs, leave an error code and error message in the rbu
//	** handle.
//	*/
func _rbuMoveOalFile(tls *libc.TLS, p uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var dbMain, zBase, zMove, zOal, zWal uintptr
	_, _, _, _, _ = dbMain, zBase, zMove, zOal, zWal
	zBase = Xsqlite3_db_filename(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033)
	zMove = zBase
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
		zMove = Xsqlite3_db_filename(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+8033)
	}
	zOal = Xsqlite3_mprintf(tls, __ccgo_ts+34989, libc.VaList(bp+8, zMove))
	zWal = Xsqlite3_mprintf(tls, __ccgo_ts+34996, libc.VaList(bp+8, zMove))
	if zWal == uintptr(0) || zOal == uintptr(0) {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
	} else {
		/* Move the *-oal file to *-wal. At this point connection p->db is
		 ** holding a SHARED lock on the target database file (because it is
		 ** in WAL mode). So no other connection may be writing the db.
		 **
		 ** In order to ensure that there are no database readers, an EXCLUSIVE
		 ** lock is obtained here before the *-oal is moved to *-wal.
		 */
		dbMain = uintptr(0)
		_rbuFileSuffix3(tls, zBase, zWal)
		_rbuFileSuffix3(tls, zBase, zOal)
		/* Re-open the databases. */
		_rbuObjIterFinalize(tls, p+56)
		Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu)
		Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = uintptr(0)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu = uintptr(0)
		dbMain = _rbuOpenDbhandle(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget, int32(1))
		if dbMain != 0 {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _rbuLockDatabase(tls, dbMain)
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3rbu)(unsafe.Pointer(p)).FxRename})))(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FpRenameArg, zOal, zWal)
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK || (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) || _rbuExclusiveCheckpoint(tls, dbMain) == 0 {
			Xsqlite3_close(tls, dbMain)
			dbMain = uintptr(0)
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			_rbuOpenDatabase(tls, p, dbMain, uintptr(0))
			_rbuSetupCheckpoint(tls, p, uintptr(0))
		}
	}
	Xsqlite3_free(tls, zWal)
	Xsqlite3_free(tls, zOal)
}

// C documentation
//
//	/*
//	** This is a helper function for rbuObjIterCacheTableInfo(). It populates
//	** the pIter->abIndexed[] array.
//	*/
func _rbuObjIterCacheIndexedCols(tls *libc.TLS, p uintptr, pIter uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bIndex, bPartial, iCid int32
	var zIdx uintptr
	var _ /* pList at bp+0 */ uintptr
	var _ /* pXInfo at bp+4 */ uintptr
	_, _, _, _ = bIndex, bPartial, iCid, zIdx
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	bIndex = 0
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		libc.Xmemcpy(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk, uint32(1)*uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol))
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32228, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl)))
	}
	(*TRbuObjIter)(unsafe.Pointer(pIter)).FnIndex = 0
	for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
		zIdx = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		bPartial = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(4))
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
		if zIdx == uintptr(0) {
			break
		}
		if bPartial != 0 {
			libc.Xmemset(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, int32(0x01), uint32(1)*uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol))
		}
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32256, libc.VaList(bp+16, zIdx)))
		for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4))) {
			iCid = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp + 4)), int32(1))
			if iCid >= 0 {
				**(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed + uintptr(iCid))) = uint8(1)
			}
			if iCid == -int32(2) {
				libc.Xmemset(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, int32(0x01), uint32(1)*uint32((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol))
			}
		}
		_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp + 4)))
		bIndex = int32(1)
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FnIndex = (*TRbuObjIter)(unsafe.Pointer(pIter)).FnIndex + 1
	}
	if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_WITHOUT_ROWID) {
		/* "PRAGMA index_list" includes the main PK b-tree */
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FnIndex = (*TRbuObjIter)(unsafe.Pointer(pIter)).FnIndex - 1
	}
	_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
	if bIndex == 0 {
		(*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed = uintptr(0)
	}
}

// C documentation
//
//	/*
//	** If they are not already populated, populate the pIter->azTblCol[],
//	** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
//	** the table (not index) that the iterator currently points to.
//	**
//	** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
//	** an error does occur, an error code and error message are also left in
//	** the RBU handle.
//	*/
func _rbuObjIterCacheTableInfo(tls *libc.TLS, p uintptr, pIter uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bNotNull, bRbuRowid, i, iOrder, iPk, nCol, t, v2 int32
	var t1, zCopy, zName, zName1, zType, v3 uintptr
	var _ /* iTnum at bp+4 */ int32
	var _ /* pStmt at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = bNotNull, bRbuRowid, i, iOrder, iPk, nCol, t, t1, zCopy, zName, zName1, zType, v2, v3
	if (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol == uintptr(0) {
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		nCol = 0      /* for() loop iterator variable */
		bRbuRowid = 0 /* If input table has column "rbu_rowid" */
		iOrder = 0
		**(**int32)(__ccgo_up(bp + 4)) = 0
		/* Figure out the type of table this step will deal with. */
		_rbuTableType(tls, p, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, pIter+36, bp+4, pIter+60)
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == RBU_PK_NOTABLE {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR)
			(*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+22573, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl))
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != 0 {
			return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
		}
		if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) {
			(*TRbuObjIter)(unsafe.Pointer(pIter)).FiTnum = **(**int32)(__ccgo_up(bp + 4))
		}
		/* Populate the azTblCol[] and nTblCol variables based on the columns
		 ** of the input table. Ignore any input table columns that begin with
		 ** "rbu_".  */
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32285, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl)))
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			nCol = Xsqlite3_column_count(tls, **(**uintptr)(__ccgo_up(bp)))
			_rbuAllocateIterArrays(tls, p, pIter, nCol)
		}
		i = 0
		for {
			if !((*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && i < nCol) {
				break
			}
			zName = Xsqlite3_column_name(tls, **(**uintptr)(__ccgo_up(bp)), i)
			if Xsqlite3_strnicmp(tls, __ccgo_ts+32304, zName, int32(4)) != 0 {
				zCopy = _rbuStrndup(tls, zName, p+32)
				**(**int32)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)*4)) = (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol
				v3 = pIter + 8
				v2 = *(*int32)(unsafe.Pointer(v3))
				*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
				**(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(v2)*4)) = zCopy
			} else {
				if 0 == Xsqlite3_stricmp(tls, __ccgo_ts+32309, zName) {
					bRbuRowid = int32(1)
				}
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && bRbuRowid != libc.BoolInt32((*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE)) {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR)
			if bRbuRowid != 0 {
				v3 = __ccgo_ts + 32319
			} else {
				v3 = __ccgo_ts + 32332
			}
			(*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+32341, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, v3))
		}
		/* Check that all non-HIDDEN columns in the destination table are also
		 ** present in the input table. Populate the abTblPk[], azTblType[] and
		 ** aiTblOrder[] arrays at the same time.  */
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32370, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl)))
		}
		for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
			zName1 = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
			if zName1 == uintptr(0) {
				break
			} /* An OOM - finalize() below returns S_NOMEM */
			i = iOrder
			for {
				if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) {
					break
				}
				if 0 == libc.Xstrcmp(tls, zName1, **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4))) {
					break
				}
				goto _5
			_5:
				;
				i = i + 1
			}
			if i == (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR)
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+32392, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zName1))
			} else {
				iPk = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(5))
				bNotNull = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(3))
				zType = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(2))
				if i != iOrder {
					t = **(**int32)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(i)*4))
					**(**int32)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(i)*4)) = **(**int32)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(iOrder)*4))
					**(**int32)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(iOrder)*4)) = t
					t1 = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4))
					**(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)) = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iOrder)*4))
					**(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iOrder)*4)) = t1
				}
				**(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iOrder)*4)) = _rbuStrndup(tls, zType, p+32)
				**(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(iOrder))) = uint8(iPk)
				**(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull + uintptr(iOrder))) = libc.BoolUint8(uint8(bNotNull) != 0 || iPk != 0)
				iOrder = iOrder + 1
			}
		}
		_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
		_rbuObjIterCacheIndexedCols(tls, p, pIter)
	}
	return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
}

// C documentation
//
//	/*
//	** Initialize the iterator structure passed as the second argument.
//	**
//	** If no error occurs, SQLITE_OK is returned and the iterator is left
//	** pointing to the first entry. Otherwise, an error code and message is
//	** left in the RBU handle passed as the first argument. A copy of the
//	** error code is returned.
//	*/
func _rbuObjIterFirst(tls *libc.TLS, p uintptr, pIter uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var rc int32
	var v1 uintptr
	_, _ = rc, v1
	libc.Xmemset(tls, pIter, 0, uint32(108))
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
		v1 = __ccgo_ts + 31700
	} else {
		v1 = __ccgo_ts + 1711
	}
	rc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+31741, libc.VaList(bp+8, v1)))
	if rc == SQLITE_OK {
		rc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+4, p+36, __ccgo_ts+31891)
	}
	(*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup = int32(1)
	(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc
	return _rbuObjIterNext(tls, p, pIter)
}

// C documentation
//
//	/*
//	** This function constructs and returns a pointer to a nul-terminated
//	** string containing some SQL clause or list based on one or more of the
//	** column names currently stored in the pIter->azTblCol[] array.
//	*/
func _rbuObjIterGetCollist(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i int32
	var z, zList, zSep uintptr
	_, _, _, _ = i, z, zList, zSep
	zList = uintptr(0)
	zSep = __ccgo_ts + 1711
	i = 0
	for {
		if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) {
			break
		}
		z = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4))
		zList = _rbuMPrintf(tls, p, __ccgo_ts+32419, libc.VaList(bp+8, zList, zSep, z))
		zSep = __ccgo_ts + 17436
		goto _1
	_1:
		;
		i = i + 1
	}
	return zList
}

// C documentation
//
//	/*
//	** This function is used to create a SELECT list (the list of SQL
//	** expressions that follows a SELECT keyword) for a SELECT statement
//	** used to read from an data_xxx or rbu_tmp_xxx table while updating the
//	** index object currently indicated by the iterator object passed as the
//	** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
//	** to obtain the required information.
//	**
//	** If the index is of the following form:
//	**
//	**   CREATE INDEX i1 ON t1(c, b COLLATE nocase);
//	**
//	** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
//	** "ipk", the returned string is:
//	**
//	**   "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
//	**
//	** As well as the returned string, three other malloc'd strings are
//	** returned via output parameters. As follows:
//	**
//	**   pzImposterCols: ...
//	**   pzImposterPk: ...
//	**   pzWhere: ...
//	*/
func _rbuObjIterGetIndexCols(tls *libc.TLS, p uintptr, pIter uintptr, pzImposterCols uintptr, pzImposterPk uintptr, pzWhere uintptr, pnBind uintptr) (r uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var bDesc, i, iCid, iSeq, nBind, rc, rc2 int32
	var zAnd, zCol, zCollate, zCom, zImpCols, zImpPK, zOrder, zRet, zType, zWhere, v2 uintptr
	var _ /* pXInfo at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bDesc, i, iCid, iSeq, nBind, rc, rc2, zAnd, zCol, zCollate, zCom, zImpCols, zImpPK, zOrder, zRet, zType, zWhere, v2
	rc = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc /* sqlite3_finalize() return code */
	zRet = uintptr(0)                          /* String to return */
	zImpCols = uintptr(0)                      /* String to return via *pzImposterCols */
	zImpPK = uintptr(0)                        /* String to return via *pzImposterPK */
	zWhere = uintptr(0)                        /* String to return via *pzWhere */
	nBind = 0                                  /* Value to return via *pnBind */
	zCom = __ccgo_ts + 1711                    /* Set to ", " later on */
	zAnd = __ccgo_ts + 1711                    /* Set to " AND " later on */
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)  /* PRAGMA index_xinfo = ? */
	if rc == SQLITE_OK {
		rc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32256, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx)))
	}
	for rc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
		iCid = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		bDesc = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(3))
		zCollate = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(4))
		zCol = uintptr(0)
		if iCid == -int32(2) {
			iSeq = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), 0)
			zRet = Xsqlite3_mprintf(tls, __ccgo_ts+32738, libc.VaList(bp+16, zRet, zCom, (**(**TRbuSpan)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iSeq)*8))).FnSpan, (**(**TRbuSpan)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iSeq)*8))).FzSpan, zCollate))
			zType = __ccgo_ts + 1711
		} else {
			if iCid < 0 {
				/* An integer primary key. If the table has an explicit IPK, use
				 ** its name. Otherwise, use "rbu_rowid".  */
				if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_IPK) {
					i = 0
					for {
						if !(int32(**(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i)))) == 0) {
							break
						}
						goto _1
					_1:
						;
						i = i + 1
					}
					zCol = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4))
				} else {
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
						zCol = __ccgo_ts + 32579
					} else {
						zCol = __ccgo_ts + 32309
					}
				}
				zType = __ccgo_ts + 1185
			} else {
				zCol = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iCid)*4))
				zType = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iCid)*4))
			}
			zRet = Xsqlite3_mprintf(tls, __ccgo_ts+32760, libc.VaList(bp+16, zRet, zCom, zCol, zCollate))
		}
		if (*TRbuObjIter)(unsafe.Pointer(pIter)).FbUnique == 0 || Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(5)) != 0 {
			if bDesc != 0 {
				v2 = __ccgo_ts + 32496
			} else {
				v2 = __ccgo_ts + 1711
			}
			zOrder = v2
			zImpPK = Xsqlite3_mprintf(tls, __ccgo_ts+32780, libc.VaList(bp+16, zImpPK, zCom, nBind, zCol, zOrder))
		}
		zImpCols = Xsqlite3_mprintf(tls, __ccgo_ts+32801, libc.VaList(bp+16, zImpCols, zCom, nBind, zCol, zType, zCollate))
		zWhere = Xsqlite3_mprintf(tls, __ccgo_ts+32834, libc.VaList(bp+16, zWhere, zAnd, nBind, zCol))
		if zRet == uintptr(0) || zImpPK == uintptr(0) || zImpCols == uintptr(0) || zWhere == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		}
		zCom = __ccgo_ts + 17436
		zAnd = __ccgo_ts + 24859
		nBind = nBind + 1
	}
	rc2 = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	if rc == SQLITE_OK {
		rc = rc2
	}
	if rc != SQLITE_OK {
		Xsqlite3_free(tls, zRet)
		Xsqlite3_free(tls, zImpCols)
		Xsqlite3_free(tls, zImpPK)
		Xsqlite3_free(tls, zWhere)
		zRet = uintptr(0)
		zImpCols = uintptr(0)
		zImpPK = uintptr(0)
		zWhere = uintptr(0)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc
	}
	**(**uintptr)(__ccgo_up(pzImposterCols)) = zImpCols
	**(**uintptr)(__ccgo_up(pzImposterPk)) = zImpPK
	**(**uintptr)(__ccgo_up(pzWhere)) = zWhere
	**(**int32)(__ccgo_up(pnBind)) = nBind
	return zRet
}

func _rbuObjIterGetIndexWhere(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aIdxCol, zRet, zSql, v1 uintptr
	var c int8
	var i, iIdxCol, nIdxAlloc, nParen, nSpan, nSpan1, rc2, v3 int32
	var _ /* pStmt at bp+0 */ uintptr
	var _ /* rc at bp+4 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _ = aIdxCol, c, i, iIdxCol, nIdxAlloc, nParen, nSpan, nSpan1, rc2, zRet, zSql, v1, v3
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 4)) = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
	zRet = uintptr(0)
	if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 4)) = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, __ccgo_ts+33424)
	}
	if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 4)) = Xsqlite3_bind_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(1), (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx, -int32(1), libc.UintptrFromInt32(0))
		if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
			zSql = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), 0)
			if zSql != 0 {
				v1 = _rbuStrndup(tls, zSql, bp+4)
				zSql = v1
				(*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdxSql = v1
			}
			if zSql != 0 {
				nParen = 0
				iIdxCol = 0
				nIdxAlloc = 0
				i = 0
				for {
					if !(**(**int8)(__ccgo_up(zSql + uintptr(i))) != 0) {
						break
					}
					c = **(**int8)(__ccgo_up(zSql + uintptr(i)))
					/* If necessary, grow the pIter->aIdxCol[] array */
					if iIdxCol == nIdxAlloc {
						aIdxCol = Xsqlite3_realloc64(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol, uint64(uint32(nIdxAlloc)*uint32(8)+libc.Uint32FromInt32(16)*libc.Uint32FromInt64(8)))
						if aIdxCol == uintptr(0) {
							**(**int32)(__ccgo_up(bp + 4)) = int32(SQLITE_NOMEM)
							break
						}
						(*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol = aIdxCol
						nIdxAlloc = nIdxAlloc + int32(16)
					}
					if int32(c) == int32('(') {
						if nParen == 0 {
							(**(**TRbuSpan)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol))).FzSpan = zSql + uintptr(i+int32(1))
						}
						nParen = nParen + 1
					} else {
						if int32(c) == int32(')') {
							nParen = nParen - 1
							if nParen == 0 {
								nSpan = t__predefined_ptrdiff_t(zSql+uintptr(i)) - int32((**(**TRbuSpan)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iIdxCol)*8))).FzSpan)
								v3 = iIdxCol
								iIdxCol = iIdxCol + 1
								(**(**TRbuSpan)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(v3)*8))).FnSpan = nSpan
								i = i + 1
								break
							}
						} else {
							if int32(c) == int32(',') && nParen == int32(1) {
								nSpan1 = t__predefined_ptrdiff_t(zSql+uintptr(i)) - int32((**(**TRbuSpan)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iIdxCol)*8))).FzSpan)
								v3 = iIdxCol
								iIdxCol = iIdxCol + 1
								(**(**TRbuSpan)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(v3)*8))).FnSpan = nSpan1
								(**(**TRbuSpan)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iIdxCol)*8))).FzSpan = zSql + uintptr(i+int32(1))
							} else {
								if int32(c) == int32('"') || int32(c) == int32('\'') || int32(c) == int32('`') {
									i = i + 1
									for {
										if !(int32(1) != 0) {
											break
										}
										if int32(**(**int8)(__ccgo_up(zSql + uintptr(i)))) == int32(c) {
											if int32(**(**int8)(__ccgo_up(zSql + uintptr(i+int32(1))))) != int32(c) {
												break
											}
											i = i + 1
										}
										goto _5
									_5:
										;
										i = i + 1
									}
								} else {
									if int32(c) == int32('[') {
										i = i + 1
										for {
											if !(int32(1) != 0) {
												break
											}
											if int32(**(**int8)(__ccgo_up(zSql + uintptr(i)))) == int32(']') {
												break
											}
											goto _6
										_6:
											;
											i = i + 1
										}
									} else {
										if int32(c) == int32('-') && int32(**(**int8)(__ccgo_up(zSql + uintptr(i+int32(1))))) == int32('-') {
											i = i + int32(2)
											for {
												if !(**(**int8)(__ccgo_up(zSql + uintptr(i))) != 0 && int32(**(**int8)(__ccgo_up(zSql + uintptr(i)))) != int32('\n')) {
													break
												}
												goto _7
											_7:
												;
												i = i + 1
											}
											if int32(**(**int8)(__ccgo_up(zSql + uintptr(i)))) == int32('\000') {
												break
											}
										} else {
											if int32(c) == int32('/') && int32(**(**int8)(__ccgo_up(zSql + uintptr(i+int32(1))))) == int32('*') {
												i = i + int32(2)
												for {
													if !(**(**int8)(__ccgo_up(zSql + uintptr(i))) != 0 && (int32(**(**int8)(__ccgo_up(zSql + uintptr(i)))) != int32('*') || int32(**(**int8)(__ccgo_up(zSql + uintptr(i+int32(1))))) != int32('/'))) {
														break
													}
													goto _8
												_8:
													;
													i = i + 1
												}
												if int32(**(**int8)(__ccgo_up(zSql + uintptr(i)))) == int32('\000') {
													break
												}
												i = i + 1
											}
										}
									}
								}
							}
						}
					}
					goto _2
				_2:
					;
					i = i + 1
				}
				if **(**int8)(__ccgo_up(zSql + uintptr(i))) != 0 {
					zRet = _rbuStrndup(tls, zSql+uintptr(i), bp+4)
				}
				(*TRbuObjIter)(unsafe.Pointer(pIter)).FnIdxCol = iIdxCol
			}
		}
		rc2 = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp + 4)) = rc2
		}
	}
	(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = **(**int32)(__ccgo_up(bp + 4))
	return zRet
}

// C documentation
//
//	/*
//	** Assuming the current table columns are "a", "b" and "c", and the zObj
//	** paramter is passed "old", return a string of the form:
//	**
//	**     "old.a, old.b, old.b"
//	**
//	** With the column names escaped.
//	**
//	** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
//	** the text ", old._rowid_" to the returned value.
//	*/
func _rbuObjIterGetOldlist(tls *libc.TLS, p uintptr, pIter uintptr, zObj uintptr) (r uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i int32
	var zCol, zList, zS uintptr
	_, _, _, _ = i, zCol, zList, zS
	zList = uintptr(0)
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed != 0 {
		zS = __ccgo_ts + 1711
		i = 0
		for {
			if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) {
				break
			}
			if **(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed + uintptr(i))) != 0 {
				zCol = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4))
				zList = Xsqlite3_mprintf(tls, __ccgo_ts+32858, libc.VaList(bp+8, zList, zS, zObj, zCol))
			} else {
				zList = Xsqlite3_mprintf(tls, __ccgo_ts+32870, libc.VaList(bp+8, zList, zS))
			}
			zS = __ccgo_ts + 17436
			if zList == uintptr(0) {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
				break
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		/* For a table with implicit rowids, append "old._rowid_" to the list. */
		if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) {
			zList = _rbuMPrintf(tls, p, __ccgo_ts+32879, libc.VaList(bp+8, zList, zObj))
		}
	}
	return zList
}

// C documentation
//
//	/*
//	** Return a comma separated list of the quoted PRIMARY KEY column names,
//	** in order, for the current table. Before each column name, add the text
//	** zPre. After each column name, add the zPost text. Use zSeparator as
//	** the separator text (usually ", ").
//	*/
func _rbuObjIterGetPkList(tls *libc.TLS, p uintptr, pIter uintptr, zPre uintptr, zSeparator uintptr, zPost uintptr) (r uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i, iPk int32
	var zCol, zRet, zSep uintptr
	_, _, _, _, _ = i, iPk, zCol, zRet, zSep
	iPk = int32(1)
	zRet = uintptr(0)
	zSep = __ccgo_ts + 1711
	for int32(1) != 0 {
		i = 0
		for {
			if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) {
				break
			}
			if int32(**(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i)))) == iPk {
				zCol = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4))
				zRet = _rbuMPrintf(tls, p, __ccgo_ts+32428, libc.VaList(bp+8, zRet, zSep, zPre, zCol, zPost))
				zSep = zSeparator
				break
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if i == (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol {
			break
		}
		iPk = iPk + 1
	}
	return zRet
}

// C documentation
//
//	/*
//	** Return a nul-terminated string containing the comma separated list of
//	** assignments that should be included following the "SET" keyword of
//	** an UPDATE statement used to update the table object that the iterator
//	** passed as the second argument currently points to if the rbu_control
//	** column of the data_xxx table entry is set to zMask.
//	**
//	** The memory for the returned string is obtained from sqlite3_malloc().
//	** It is the responsibility of the caller to eventually free it using
//	** sqlite3_free().
//	**
//	** If an OOM error is encountered when allocating space for the new
//	** string, an error code is left in the rbu handle passed as the first
//	** argument and NULL is returned. Or, if an error has already occurred
//	** when this function is called, NULL is returned immediately, without
//	** attempting the allocation or modifying the stored error code.
//	*/
func _rbuObjIterGetSetlist(tls *libc.TLS, p uintptr, pIter uintptr, zMask uintptr) (r uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var c int8
	var i int32
	var zList, zSep uintptr
	_, _, _, _ = c, i, zList, zSep
	zList = uintptr(0)
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		if int32(libc.Xstrlen(tls, zMask)) != (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol {
			_rbuBadControlError(tls, p)
		} else {
			zSep = __ccgo_ts + 1711
			i = 0
			for {
				if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) {
					break
				}
				c = **(**int8)(__ccgo_up(zMask + uintptr(**(**int32)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(i)*4)))))
				if int32(c) == int32('x') {
					zList = _rbuMPrintf(tls, p, __ccgo_ts+32970, libc.VaList(bp+8, zList, zSep, **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), i+int32(1)))
					zSep = __ccgo_ts + 17436
				} else {
					if int32(c) == int32('d') {
						zList = _rbuMPrintf(tls, p, __ccgo_ts+33009, libc.VaList(bp+8, zList, zSep, **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), i+int32(1)))
						zSep = __ccgo_ts + 17436
					} else {
						if int32(c) == int32('f') {
							zList = _rbuMPrintf(tls, p, __ccgo_ts+33039, libc.VaList(bp+8, zList, zSep, **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4)), i+int32(1)))
							zSep = __ccgo_ts + 17436
						}
					}
				}
				goto _1
			_1:
				;
				i = i + 1
			}
		}
	}
	return zList
}

// C documentation
//
//	/*
//	** Return an expression that can be used in a WHERE clause to match the
//	** primary key of the current table. For example, if the table is:
//	**
//	**   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
//	**
//	** Return the string:
//	**
//	**   "b = ?1 AND c = ?2"
//	*/
func _rbuObjIterGetWhere(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i, i1 int32
	var zCol, zList, zSep, zSep1 uintptr
	_, _, _, _, _, _ = i, i1, zCol, zList, zSep, zSep1
	zList = uintptr(0)
	if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) {
		zList = _rbuMPrintf(tls, p, __ccgo_ts+32894, libc.VaList(bp+8, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol+int32(1)))
	} else {
		if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) {
			zSep = __ccgo_ts + 1711
			i = 0
			for {
				if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) {
					break
				}
				if **(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i))) != 0 {
					zList = _rbuMPrintf(tls, p, __ccgo_ts+32908, libc.VaList(bp+8, zList, zSep, i, i+int32(1)))
					zSep = __ccgo_ts + 24859
				}
				goto _1
			_1:
				;
				i = i + 1
			}
			zList = _rbuMPrintf(tls, p, __ccgo_ts+32920, libc.VaList(bp+8, zList))
		} else {
			zSep1 = __ccgo_ts + 1711
			i1 = 0
			for {
				if !(i1 < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) {
					break
				}
				if **(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i1))) != 0 {
					zCol = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i1)*4))
					zList = _rbuMPrintf(tls, p, __ccgo_ts+32970, libc.VaList(bp+8, zList, zSep1, zCol, i1+int32(1)))
					zSep1 = __ccgo_ts + 24859
				}
				goto _2
			_2:
				;
				i1 = i1 + 1
			}
		}
	}
	return zList
}

// C documentation
//
//	/*
//	** Advance the iterator to the next position.
//	**
//	** If no error occurs, SQLITE_OK is returned and the iterator is left
//	** pointing to the next entry. Otherwise, an error code and message is
//	** left in the RBU handle passed as the first argument. A copy of the
//	** error code is returned.
//	*/
func _rbuObjIterNext(tls *libc.TLS, p uintptr, pIter uintptr) (r int32) {
	var pIdx uintptr
	var rc, v1 int32
	_, _, _ = pIdx, rc, v1
	rc = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
	if rc == SQLITE_OK {
		/* Free any SQLite statements used while processing the previous object */
		_rbuObjIterClearStatements(tls, pIter)
		if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) {
			rc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31529, uintptr(0), uintptr(0), p+36)
		}
		if rc == SQLITE_OK {
			if (*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup != 0 {
				_rbuObjIterFreeCols(tls, pIter)
				(*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup = 0
				rc = Xsqlite3_step(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter)
				if rc != int32(SQLITE_ROW) {
					rc = _resetAndCollectError(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter, p+36)
					(*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl = uintptr(0)
					(*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl = uintptr(0)
				} else {
					(*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl = Xsqlite3_column_text(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter, 0)
					(*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl = Xsqlite3_column_text(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpTblIter, int32(1))
					if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl != 0 && (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl != 0 {
						v1 = SQLITE_OK
					} else {
						v1 = int32(SQLITE_NOMEM)
					}
					rc = v1
				}
			} else {
				if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) {
					pIdx = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter
					rc = Xsqlite3_bind_text(tls, pIdx, int32(1), (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, -int32(1), libc.UintptrFromInt32(0))
				}
				if rc == SQLITE_OK {
					rc = Xsqlite3_step(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter)
					if rc != int32(SQLITE_ROW) {
						rc = _resetAndCollectError(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter, p+36)
						(*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup = int32(1)
						(*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx = uintptr(0)
					} else {
						(*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx = Xsqlite3_column_text(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter, 0)
						(*TRbuObjIter)(unsafe.Pointer(pIter)).FiTnum = Xsqlite3_column_int(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter, int32(1))
						(*TRbuObjIter)(unsafe.Pointer(pIter)).FbUnique = Xsqlite3_column_int(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpIdxIter, int32(2))
						if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx != 0 {
							v1 = SQLITE_OK
						} else {
							v1 = int32(SQLITE_NOMEM)
						}
						rc = v1
					}
				}
			}
		}
	}
	if rc != SQLITE_OK {
		_rbuObjIterFinalize(tls, pIter)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc
	}
	return rc
}

// C documentation
//
//	/*
//	** Ensure that the SQLite statement handles required to update the
//	** target database object currently indicated by the iterator passed
//	** as the second argument are available.
//	*/
func _rbuObjIterPrepareAll(tls *libc.TLS, p uintptr, pIter uintptr, nOffset int32) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var bRbuRowid, tnum int32
	var pz, zBind, zBindings, zCollist, zIdx, zLimit, zNewlist, zOldlist, zOrder, zPart, zRbuRowid, zRbuRowid1, zSql, zStart, zStart1, zTbl, zTbl1, zWhere1, zWrite, v1, v2, v3 uintptr
	var _ /* nBind at bp+12 */ int32
	var _ /* zImposterCols at bp+0 */ uintptr
	var _ /* zImposterPK at bp+4 */ uintptr
	var _ /* zWhere at bp+8 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bRbuRowid, pz, tnum, zBind, zBindings, zCollist, zIdx, zLimit, zNewlist, zOldlist, zOrder, zPart, zRbuRowid, zRbuRowid1, zSql, zStart, zStart1, zTbl, zTbl1, zWhere1, zWrite, v1, v2, v3
	if (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect == uintptr(0) && _rbuObjIterCacheTableInfo(tls, p, pIter) == SQLITE_OK {
		tnum = (*TRbuObjIter)(unsafe.Pointer(pIter)).FiTnum
		zCollist = uintptr(0) /* List of indexed columns */
		pz = p + 36
		zIdx = (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx
		zLimit = uintptr(0)
		if nOffset != 0 {
			zLimit = Xsqlite3_mprintf(tls, __ccgo_ts+33490, libc.VaList(bp+24, nOffset))
			if !(zLimit != 0) {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
			}
		}
		if zIdx != 0 {
			zTbl = (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)     /* Columns for imposter table */
			**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* Primary key declaration for imposter */
			**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0) /* WHERE clause on PK columns */
			zBind = uintptr(0)
			zPart = uintptr(0)
			**(**int32)(__ccgo_up(bp + 12)) = 0
			zPart = _rbuObjIterGetIndexWhere(tls, p, pIter)
			zCollist = _rbuObjIterGetIndexCols(tls, p, pIter, bp, bp+4, bp+8, bp+12)
			zBind = _rbuObjIterGetBindlist(tls, p, **(**int32)(__ccgo_up(bp + 12)))
			/* Create the imposter table used to write to this index. */
			Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+24, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, 0, int32(1)))
			Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+24, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, int32(1), tnum))
			_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+33510, libc.VaList(bp+24, zTbl, **(**uintptr)(__ccgo_up(bp)), **(**uintptr)(__ccgo_up(bp + 4))))
			Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+24, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, 0, 0))
			/* Create the statement to insert index entries */
			(*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol = **(**int32)(__ccgo_up(bp + 12))
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+80, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+33575, libc.VaList(bp+24, zTbl, zBind)))
			}
			/* And to delete index entries */
			if libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+84, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+33611, libc.VaList(bp+24, zTbl, **(**uintptr)(__ccgo_up(bp + 8)))))
			}
			/* Create the SELECT statement to read keys in sorted order */
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
					zStart = uintptr(0)
					if nOffset != 0 {
						zStart = _rbuVacuumIndexStart(tls, p, pIter)
						if zStart != 0 {
							Xsqlite3_free(tls, zLimit)
							zLimit = uintptr(0)
						}
					}
					if zStart != 0 {
						if zPart != 0 {
							v2 = __ccgo_ts + 33645
						} else {
							v2 = __ccgo_ts + 33649
						}
						v1 = v2
					} else {
						v1 = __ccgo_ts + 1711
					}
					zSql = Xsqlite3_mprintf(tls, __ccgo_ts+33655, libc.VaList(bp+24, zCollist, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zPart, v1, zStart, zCollist, zLimit))
					Xsqlite3_free(tls, zStart)
				} else {
					if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) {
						zSql = Xsqlite3_mprintf(tls, __ccgo_ts+33716, libc.VaList(bp+24, zCollist, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zPart, zCollist, zLimit))
					} else {
						if zPart != 0 {
							v1 = __ccgo_ts + 33645
						} else {
							v1 = __ccgo_ts + 33649
						}
						zSql = Xsqlite3_mprintf(tls, __ccgo_ts+33777, libc.VaList(bp+24, zCollist, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zPart, zCollist, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zPart, v1, zCollist, zLimit))
					}
				}
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter+76, pz, zSql)
				} else {
					Xsqlite3_free(tls, zSql)
				}
			}
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp)))
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 4)))
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 8)))
			Xsqlite3_free(tls, zBind)
			Xsqlite3_free(tls, zPart)
		} else {
			bRbuRowid = libc.BoolInt32((*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0))
			zTbl1 = (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl /* Imposter table name */
			zBindings = _rbuObjIterGetBindlist(tls, p, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol+bRbuRowid)
			zWhere1 = _rbuObjIterGetWhere(tls, p, pIter)
			zOldlist = _rbuObjIterGetOldlist(tls, p, pIter, __ccgo_ts+8044)
			zNewlist = _rbuObjIterGetOldlist(tls, p, pIter, __ccgo_ts+8040)
			zCollist = _rbuObjIterGetCollist(tls, p, pIter)
			(*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol = (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol
			/* Create the imposter table or tables (if required). */
			_rbuCreateImposterTable(tls, p, pIter)
			_rbuCreateImposterTable2(tls, p, pIter)
			if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) {
				v1 = __ccgo_ts + 1711
			} else {
				v1 = __ccgo_ts + 33936
			}
			zWrite = v1
			/* Create the INSERT statement to write to the target PK b-tree */
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				if bRbuRowid != 0 {
					v1 = __ccgo_ts + 33945
				} else {
					v1 = __ccgo_ts + 1711
				}
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+80, pz, Xsqlite3_mprintf(tls, __ccgo_ts+33955, libc.VaList(bp+24, zWrite, zTbl1, zCollist, v1, zBindings)))
			}
			/* Create the DELETE statement to write to the target PK b-tree.
			 ** Because it only performs INSERT operations, this is not required for
			 ** an rbu vacuum handle.  */
			if libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+84, pz, Xsqlite3_mprintf(tls, __ccgo_ts+33991, libc.VaList(bp+24, zWrite, zTbl1, zWhere1)))
			}
			if libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed != 0 {
				zRbuRowid = __ccgo_ts + 1711
				if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) {
					zRbuRowid = __ccgo_ts + 34019
				}
				/* Create the rbu_tmp_xxx table and the triggers to populate it. */
				if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) {
					v1 = __ccgo_ts + 34031
				} else {
					v1 = __ccgo_ts + 1711
				}
				_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+34048, libc.VaList(bp+24, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, v1, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl))
				_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+34124, libc.VaList(bp+24, zWrite, zTbl1, zOldlist, zWrite, zTbl1, zOldlist, zWrite, zTbl1, zNewlist))
				if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) {
					_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+34423, libc.VaList(bp+24, zWrite, zTbl1, zNewlist))
				}
				_rbuObjIterPrepareTmpInsert(tls, p, pIter, zCollist, zRbuRowid)
			}
			/* Create the SELECT statement to read keys from data_xxx */
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				zRbuRowid1 = __ccgo_ts + 1711
				zStart1 = uintptr(0)
				zOrder = uintptr(0)
				if bRbuRowid != 0 {
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
						v1 = __ccgo_ts + 34522
					} else {
						v1 = __ccgo_ts + 34532
					}
					zRbuRowid1 = v1
				}
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
					if nOffset != 0 {
						zStart1 = _rbuVacuumTableStart(tls, p, pIter, bRbuRowid, zWrite)
						if zStart1 != 0 {
							Xsqlite3_free(tls, zLimit)
							zLimit = uintptr(0)
						}
					}
					if bRbuRowid != 0 {
						zOrder = _rbuMPrintf(tls, p, __ccgo_ts+32579, 0)
					} else {
						zOrder = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+1711, __ccgo_ts+17436, __ccgo_ts+1711)
					}
				}
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
						v1 = __ccgo_ts + 34543
					} else {
						v1 = __ccgo_ts + 1711
					}
					if zStart1 != 0 {
						v2 = zStart1
					} else {
						v2 = __ccgo_ts + 1711
					}
					if zOrder != 0 {
						v3 = __ccgo_ts + 26084
					} else {
						v3 = __ccgo_ts + 1711
					}
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter+76, pz, Xsqlite3_mprintf(tls, __ccgo_ts+34549, libc.VaList(bp+24, zCollist, v1, zRbuRowid1, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, v2, v3, zOrder, zLimit)))
				}
				Xsqlite3_free(tls, zStart1)
				Xsqlite3_free(tls, zOrder)
			}
			Xsqlite3_free(tls, zWhere1)
			Xsqlite3_free(tls, zOldlist)
			Xsqlite3_free(tls, zNewlist)
			Xsqlite3_free(tls, zBindings)
		}
		Xsqlite3_free(tls, zCollist)
		Xsqlite3_free(tls, zLimit)
	}
	return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
}

// C documentation
//
//	/*
//	** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
//	** Specifically a statement of the form:
//	**
//	**     INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
//	**
//	** The number of bound variables is equal to the number of columns in
//	** the target table, plus one (for the rbu_control column), plus one more
//	** (for the rbu_rowid column) if the target table is an implicit IPK or
//	** virtual table.
//	*/
func _rbuObjIterPrepareTmpInsert(tls *libc.TLS, p uintptr, pIter uintptr, zCollist uintptr, zRbuRowid uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var bRbuRowid int32
	var zBind uintptr
	_, _ = bRbuRowid, zBind
	bRbuRowid = libc.BoolInt32((*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE))
	zBind = _rbuObjIterGetBindlist(tls, p, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol+int32(1)+bRbuRowid)
	if zBind != 0 {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter+88, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+33367, libc.VaList(bp+8, p+24, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zCollist, zRbuRowid, zBind)))
	}
}

// C documentation
//
//	/*
//	** Open the database handle and attach the RBU database as "rbu". If an
//	** error occurs, leave an error code and message in the RBU handle.
//	**
//	** If argument dbMain is not NULL, then it is a database handle already
//	** open on the target database. Use this handle instead of opening a new
//	** one.
//	*/
func _rbuOpenDatabase(tls *libc.TLS, p uintptr, dbMain uintptr, pbRetry uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bOpen, rc int32
	var pState, zExtra, zFile, zTarget, v1, v2 uintptr
	_, _, _, _, _, _, _, _ = bOpen, pState, rc, zExtra, zFile, zTarget, v1, v2
	/* Open the RBU database */
	(*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu = _rbuOpenDbhandle(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, int32(1))
	(*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = dbMain
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
		Xsqlite3_file_control(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+8033, int32(SQLITE_FCNTL_RBUCNT), p)
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState == uintptr(0) {
			zFile = Xsqlite3_db_filename(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+8033)
			(*Tsqlite3rbu)(unsafe.Pointer(p)).FzState = _rbuMPrintf(tls, p, __ccgo_ts+34657, libc.VaList(bp+8, zFile, zFile))
		}
	}
	/* If using separate RBU and state databases, attach the state database to
	 ** the RBU db handle now.  */
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState != 0 {
		_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+34685, libc.VaList(bp+8, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState))
		libc.Xmemcpy(tls, p+24, __ccgo_ts+16294, uint32(4))
	} else {
		libc.Xmemcpy(tls, p+24, __ccgo_ts+8033, uint32(4))
	}
	/* If it has not already been created, create the rbu_state table */
	_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+34703, libc.VaList(bp+8, p+24))
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
		bOpen = 0
		(*Tsqlite3rbu)(unsafe.Pointer(p)).FnRbu = 0
		(*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd = uintptr(0)
		rc = Xsqlite3_file_control(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+8033, int32(SQLITE_FCNTL_RBUCNT), p)
		if rc != int32(SQLITE_NOTFOUND) {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc
		}
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage >= int32(RBU_STAGE_MOVE) {
			bOpen = int32(1)
		} else {
			pState = _rbuLoadState(tls, p)
			if pState != 0 {
				bOpen = libc.BoolInt32((*TRbuState)(unsafe.Pointer(pState)).FeStage >= int32(RBU_STAGE_MOVE))
				_rbuFreeState(tls, pState)
			}
		}
		if bOpen != 0 {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = _rbuOpenDbhandle(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FnRbu <= int32(1)))
		}
	}
	(*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage = 0
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain == uintptr(0) {
		if !((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == libc.UintptrFromInt32(0)) {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = _rbuOpenDbhandle(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget, int32(1))
		} else {
			if (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd)).FpWalFd != 0 {
				if pbRetry != 0 {
					(*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd)).FbNolock = uint8(0)
					Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu)
					Xsqlite3_close(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain)
					(*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = uintptr(0)
					(*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu = uintptr(0)
					**(**int32)(__ccgo_up(pbRetry)) = int32(1)
					return
				}
				(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR)
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+34769, 0)
			} else {
				zExtra = uintptr(0)
				if libc.Xstrlen(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu) >= uint32(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+27348, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, uint32(5)) {
					zExtra = (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu + 5
					for **(**int8)(__ccgo_up(zExtra)) != 0 {
						v1 = zExtra
						zExtra = zExtra + 1
						if int32(**(**int8)(__ccgo_up(v1))) == int32('?') {
							break
						}
					}
					if int32(**(**int8)(__ccgo_up(zExtra))) == int32('\000') {
						zExtra = uintptr(0)
					}
				}
				if zExtra == uintptr(0) {
					v1 = __ccgo_ts + 1711
				} else {
					v1 = __ccgo_ts + 34801
				}
				if zExtra == uintptr(0) {
					v2 = __ccgo_ts + 1711
				} else {
					v2 = zExtra
				}
				zTarget = Xsqlite3_mprintf(tls, __ccgo_ts+34803, libc.VaList(bp+8, Xsqlite3_db_filename(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+8033), v1, v2))
				if zTarget == uintptr(0) {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
					return
				}
				(*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain = _rbuOpenDbhandle(tls, p, zTarget, libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FnRbu <= int32(1)))
				Xsqlite3_free(tls, zTarget)
			}
		}
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_create_function(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+34835, -int32(1), int32(SQLITE_UTF8), p, __ccgo_fp(_rbuTmpInsertFunc), uintptr(0), uintptr(0))
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_create_function(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+34850, int32(2), int32(SQLITE_UTF8), uintptr(0), __ccgo_fp(_rbuFossilDeltaFunc), uintptr(0), uintptr(0))
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_create_function(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+34867, -int32(1), int32(SQLITE_UTF8), p, __ccgo_fp(_rbuTargetNameFunc), uintptr(0), uintptr(0))
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_file_control(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, int32(SQLITE_FCNTL_RBU), p)
	}
	_rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+34883, 0)
	/* Mark the database file just opened as an RBU target database. If
	 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
	 ** This is an error.  */
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_file_control(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+8033, int32(SQLITE_FCNTL_RBU), p)
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == int32(SQLITE_NOTFOUND) {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+34911, 0)
	}
}

// C documentation
//
//	/*
//	** Update the contents of the rbu_state table within the rbu database. The
//	** value stored in the RBU_STATE_STAGE column is eStage. All other values
//	** are determined by inspecting the rbu handle passed as the first argument.
//	*/
func _rbuSaveState(tls *libc.TLS, p uintptr, eStage int32) {
	bp := tls.Alloc(192)
	defer tls.Free(192)
	var pFd, v1 uintptr
	var rc int32
	var _ /* pInsert at bp+0 */ uintptr
	_, _, _ = pFd, rc, v1
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK || (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == int32(SQLITE_DONE) {
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
			v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FpRbuFd
		} else {
			v1 = (*Tsqlite3rbu)(unsafe.Pointer(p)).FpTargetFd
		}
		pFd = v1
		rc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+35052, libc.VaList(bp+16, p+24, int32(RBU_STATE_STAGE), eStage, int32(RBU_STATE_TBL), (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FzTbl, int32(RBU_STATE_IDX), (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FzIdx, int32(RBU_STATE_ROW), (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep, int32(RBU_STATE_PROGRESS), (*Tsqlite3rbu)(unsafe.Pointer(p)).FnProgress, int32(RBU_STATE_CKPT), (*Tsqlite3rbu)(unsafe.Pointer(p)).FiWalCksum, int32(RBU_STATE_COOKIE), int64((*Trbu_file)(unsafe.Pointer(pFd)).FiCookie), int32(RBU_STATE_OALSZ), (*Tsqlite3rbu)(unsafe.Pointer(p)).FiOalSz, int32(RBU_STATE_PHASEONESTEP), (*Tsqlite3rbu)(unsafe.Pointer(p)).FnPhaseOneStep, int32(RBU_STATE_DATATBL), (*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FzDataTbl)))
		if rc == SQLITE_OK {
			Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp)))
			rc = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		}
		if rc != SQLITE_OK {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc
		}
	}
}

// C documentation
//
//	/*
//	** This function is called as part of sqlite3rbu_open() when initializing
//	** an rbu handle in OAL stage. If the rbu update has not started (i.e.
//	** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
//	** things so that the next call to sqlite3rbu_step() continues on from
//	** where the previous rbu handle left off.
//	**
//	** If an error occurs, an error code and error message are left in the
//	** rbu handle passed as the first argument.
//	*/
func _rbuSetupOal(tls *libc.TLS, p uintptr, pState uintptr) {
	var pIter uintptr
	var rc int32
	_, _ = pIter, rc
	if (*TRbuState)(unsafe.Pointer(pState)).FzTbl != 0 {
		pIter = p + 56
		rc = SQLITE_OK
		for rc == SQLITE_OK && (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl != 0 && ((*TRbuObjIter)(unsafe.Pointer(pIter)).FbCleanup != 0 || _rbuStrCompare(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx, (*TRbuState)(unsafe.Pointer(pState)).FzIdx) != 0 || (*TRbuState)(unsafe.Pointer(pState)).FzDataTbl == uintptr(0) && _rbuStrCompare(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, (*TRbuState)(unsafe.Pointer(pState)).FzTbl) != 0 || (*TRbuState)(unsafe.Pointer(pState)).FzDataTbl != 0 && _rbuStrCompare(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, (*TRbuState)(unsafe.Pointer(pState)).FzDataTbl) != 0) {
			rc = _rbuObjIterNext(tls, p, pIter)
		}
		if rc == SQLITE_OK && !((*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl != 0) {
			rc = int32(SQLITE_ERROR)
			(*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+35542, 0)
		}
		if rc == SQLITE_OK {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = (*TRbuState)(unsafe.Pointer(pState)).FnRow
			rc = _rbuObjIterPrepareAll(tls, p, p+56, (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep)
		}
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = rc
	}
}

// C documentation
//
//	/*
//	** This function does the work for an sqlite3rbu_step() call.
//	**
//	** The object-iterator (p->objiter) currently points to a valid object,
//	** and the input cursor (p->objiter.pSelect) currently points to a valid
//	** input row. Perform whatever processing is required and return.
//	**
//	** If no  error occurs, SQLITE_OK is returned. Otherwise, an error code
//	** and message is left in the RBU handle and a copy of the error code
//	** returned.
//	*/
func _rbuStep(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var c int8
	var eType, i int32
	var pIter, pVal uintptr
	var _ /* pUpdate at bp+4 */ uintptr
	var _ /* zMask at bp+0 */ uintptr
	_, _, _, _, _ = c, eType, i, pIter, pVal
	pIter = p + 56
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	eType = _rbuStepType(tls, p, bp)
	if eType != 0 {
		if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) && (eType == int32(RBU_IDX_DELETE) || eType == int32(RBU_IDX_INSERT)) {
			_rbuBadControlError(tls, p)
		} else {
			if eType == int32(RBU_REPLACE) {
				if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) {
					**(**Ti64)(__ccgo_up(p + 184)) += int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnIndex)
					_rbuStepOneOp(tls, p, int32(RBU_DELETE))
				}
				if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
					_rbuStepOneOp(tls, p, int32(RBU_INSERT))
				}
			} else {
				if eType != int32(RBU_UPDATE) {
					_rbuStepOneOp(tls, p, eType)
				} else {
					**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
					**(**Ti64)(__ccgo_up(p + 184)) -= int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnIndex)
					_rbuGetUpdateStmt(tls, p, pIter, **(**uintptr)(__ccgo_up(bp)), bp+4)
					if **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
						i = 0
						for {
							if !((*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol) {
								break
							}
							c = **(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(**(**int32)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(i)*4)))))
							pVal = Xsqlite3_column_value(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, i)
							if **(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i))) != 0 || int32(c) != int32('.') {
								(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_bind_value(tls, **(**uintptr)(__ccgo_up(bp + 4)), i+int32(1), pVal)
							}
							goto _1
						_1:
							;
							i = i + 1
						}
						if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && ((*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE)) {
							/* Bind the rbu_rowid value to column _rowid_ */
							pVal = Xsqlite3_column_value(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1))
							(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_bind_value(tls, **(**uintptr)(__ccgo_up(bp + 4)), (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1), pVal)
						}
						if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
							Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4)))
							(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _resetAndCollectError(tls, **(**uintptr)(__ccgo_up(bp + 4)), p+36)
						}
					}
				}
			}
		}
	}
	return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
}

// C documentation
//
//	/*
//	** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
//	** RBU_IDX_DELETE. This function performs the work of a single
//	** sqlite3rbu_step() call for the type of operation specified by eType.
//	*/
func _rbuStepOneOp(tls *libc.TLS, p uintptr, eType int32) {
	var i int32
	var pIter, pVal, pWriter uintptr
	_, _, _, _ = i, pIter, pVal, pWriter
	pIter = p + 56
	/* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
	 ** statement below does actually delete a row, nPhaseOneStep will be
	 ** incremented by the same amount when SQL function rbu_tmp_insert()
	 ** is invoked by the trigger.  */
	if eType == int32(RBU_DELETE) {
		**(**Ti64)(__ccgo_up(p + 184)) -= int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnIndex)
	}
	if eType == int32(RBU_IDX_DELETE) || eType == int32(RBU_DELETE) {
		pWriter = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpDelete
	} else {
		pWriter = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpInsert
	}
	i = 0
	for {
		if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol) {
			break
		}
		/* If this is an INSERT into a table b-tree and the table has an
		 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
		 ** to write a NULL into the IPK column. That is not permitted.  */
		if eType == int32(RBU_INSERT) && (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) && (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_IPK) && **(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i))) != 0 && Xsqlite3_column_type(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, i) == int32(SQLITE_NULL) {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_MISMATCH)
			(*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+26905, 0)
			return
		}
		if eType == int32(RBU_DELETE) && int32(**(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i)))) == 0 {
			goto _1
		}
		pVal = Xsqlite3_column_value(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, i)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_bind_value(tls, pWriter, i+int32(1), pVal)
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != 0 {
			return
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx == uintptr(0) {
		if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_VTAB) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) && (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
			/* For a virtual table, or a table with no primary key, the
			 ** SELECT statement is:
			 **
			 **   SELECT <cols>, rbu_control, rbu_rowid FROM ....
			 **
			 ** Hence column_value(pIter->nCol+1).
			 */
			pVal = Xsqlite3_column_value(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1))
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_bind_value(tls, pWriter, (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1), pVal)
		}
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		Xsqlite3_step(tls, pWriter)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _resetAndCollectError(tls, pWriter, p+36)
	}
}

// C documentation
//
//	/* Determine the type of a table.
//	**
//	**   peType is of type (int*), a pointer to an output parameter of type
//	**   (int). This call sets the output parameter as follows, depending
//	**   on the type of the table specified by parameters dbName and zTbl.
//	**
//	**     RBU_PK_NOTABLE:       No such table.
//	**     RBU_PK_NONE:          Table has an implicit rowid.
//	**     RBU_PK_IPK:           Table has an explicit IPK column.
//	**     RBU_PK_EXTERNAL:      Table has an external PK index.
//	**     RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
//	**     RBU_PK_VTAB:          Table is a virtual table.
//	**
//	**   Argument *piPk is also of type (int*), and also points to an output
//	**   parameter. Unless the table has an external primary key index
//	**   (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
//	**   if the table does have an external primary key index, then *piPk
//	**   is set to the root page number of the primary key index before
//	**   returning.
//	**
//	** ALGORITHM:
//	**
//	**   if( no entry exists in sqlite_schema ){
//	**     return RBU_PK_NOTABLE
//	**   }else if( sql for the entry starts with "CREATE VIRTUAL" ){
//	**     return RBU_PK_VTAB
//	**   }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
//	**     if( the index that is the pk exists in sqlite_schema ){
//	**       *piPK = rootpage of that index.
//	**       return RBU_PK_EXTERNAL
//	**     }else{
//	**       return RBU_PK_WITHOUT_ROWID
//	**     }
//	**   }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
//	**     return RBU_PK_IPK
//	**   }else{
//	**     return RBU_PK_NONE
//	**   }
//	*/
func _rbuTableType(tls *libc.TLS, p uintptr, zTab uintptr, peType uintptr, piTnum uintptr, piPk uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i uint32
	var zIdx, zOrig uintptr
	var _ /* aStmt at bp+0 */ [4]uintptr
	_, _, _ = i, zIdx, zOrig
	/*
	 ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q)
	 ** 1) PRAGMA index_list = ?
	 ** 2) SELECT count(*) FROM sqlite_schema where name=%Q
	 ** 3) PRAGMA table_info = ?
	 */
	**(**[4]uintptr)(__ccgo_up(bp)) = [4]uintptr{}
	**(**int32)(__ccgo_up(peType)) = RBU_PK_NOTABLE
	**(**int32)(__ccgo_up(piPk)) = 0
	(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32016, libc.VaList(bp+24, zTab)))
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK || Xsqlite3_step(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[0]) != int32(SQLITE_ROW) {
		/* Either an error, or no such table. */
		goto rbuTableType_end
	}
	if Xsqlite3_column_int(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[0], 0) != 0 {
		**(**int32)(__ccgo_up(peType)) = int32(RBU_PK_VTAB) /* virtual table */
		goto rbuTableType_end
	}
	**(**int32)(__ccgo_up(piTnum)) = Xsqlite3_column_int(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[0], int32(1))
	(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+1*4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32135, libc.VaList(bp+24, zTab)))
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != 0 {
		goto rbuTableType_end
	}
	for Xsqlite3_step(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[int32(1)]) == int32(SQLITE_ROW) {
		zOrig = Xsqlite3_column_text(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[int32(1)], int32(3))
		zIdx = Xsqlite3_column_text(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[int32(1)], int32(1))
		if zOrig != 0 && zIdx != 0 && int32(**(**Tu8)(__ccgo_up(zOrig))) == int32('p') {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+2*4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32156, libc.VaList(bp+24, zIdx)))
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				if Xsqlite3_step(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[int32(2)]) == int32(SQLITE_ROW) {
					**(**int32)(__ccgo_up(piPk)) = Xsqlite3_column_int(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[int32(2)], 0)
					**(**int32)(__ccgo_up(peType)) = int32(RBU_PK_EXTERNAL)
				} else {
					**(**int32)(__ccgo_up(peType)) = int32(RBU_PK_WITHOUT_ROWID)
				}
			}
			goto rbuTableType_end
		}
	}
	(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+3*4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32207, libc.VaList(bp+24, zTab)))
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		for Xsqlite3_step(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[int32(3)]) == int32(SQLITE_ROW) {
			if Xsqlite3_column_int(tls, (**(**[4]uintptr)(__ccgo_up(bp)))[int32(3)], int32(5)) > 0 {
				**(**int32)(__ccgo_up(peType)) = int32(RBU_PK_IPK) /* explicit IPK column */
				goto rbuTableType_end
			}
		}
		**(**int32)(__ccgo_up(peType)) = int32(RBU_PK_NONE)
	}
	goto rbuTableType_end
rbuTableType_end:
	;
	i = uint32(0)
	for {
		if !(i < libc.Uint32FromInt64(16)/libc.Uint32FromInt64(4)) {
			break
		}
		_rbuFinalize(tls, p, (**(**[4]uintptr)(__ccgo_up(bp)))[i])
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** The implementation of the rbu_target_name() SQL function. This function
//	** accepts one or two arguments. The first argument is the name of a table -
//	** the name of a table in the RBU database.  The second, if it is present, is 1
//	** for a view or 0 for a table.
//	**
//	** For a non-vacuum RBU handle, if the table name matches the pattern:
//	**
//	**     data[0-9]_<name>
//	**
//	** where <name> is any sequence of 1 or more characters, <name> is returned.
//	** Otherwise, if the only argument does not match the above pattern, an SQL
//	** NULL is returned.
//	**
//	**     "data_t1"     -> "t1"
//	**     "data0123_t2" -> "t2"
//	**     "dataAB_t3"   -> NULL
//	**
//	** For an rbu vacuum handle, a copy of the first argument is returned if
//	** the second argument is either missing or 0 (not a view).
//	*/
func _rbuTargetNameFunc(tls *libc.TLS, pCtx uintptr, argc int32, argv uintptr) {
	var i int32
	var p, zIn uintptr
	_, _, _ = i, p, zIn
	p = Xsqlite3_user_data(tls, pCtx)
	zIn = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	if zIn != 0 {
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) {
			if argc == int32(1) || 0 == Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 1*4))) {
				Xsqlite3_result_text(tls, pCtx, zIn, -int32(1), libc.UintptrFromInt32(0))
			}
		} else {
			if libc.Xstrlen(tls, zIn) > uint32(4) && libc.Xmemcmp(tls, __ccgo_ts+28645, zIn, uint32(4)) == 0 {
				i = int32(4)
				for {
					if !(int32(**(**int8)(__ccgo_up(zIn + uintptr(i)))) >= int32('0') && int32(**(**int8)(__ccgo_up(zIn + uintptr(i)))) <= int32('9')) {
						break
					}
					goto _1
				_1:
					;
					i = i + 1
				}
				if int32(**(**int8)(__ccgo_up(zIn + uintptr(i)))) == int32('_') && **(**int8)(__ccgo_up(zIn + uintptr(i+int32(1)))) != 0 {
					Xsqlite3_result_text(tls, pCtx, zIn+uintptr(i+int32(1)), -int32(1), libc.UintptrFromInt32(0))
				}
			}
		}
	}
}

// C documentation
//
//	/*
//	** This function is called as part of restating an RBU vacuum when the
//	** current operation is writing content to an index. If possible, it
//	** queries the target index b-tree for the largest key already written to
//	** it, then composes and returns an expression that can be used in a WHERE
//	** clause to select the remaining required rows from the source table.
//	** It is only possible to return such an expression if:
//	**
//	**   * The index contains no DESC columns, and
//	**   * The last key written to the index before the operation was
//	**     suspended does not contain any NULL values.
//	**
//	** The expression is of the form:
//	**
//	**   (index-field1, index-field2, ...) > (?, ?, ...)
//	**
//	** except that the "?" placeholders are replaced with literal values.
//	**
//	** If the expression cannot be created, NULL is returned. In this case,
//	** the caller has to use an OFFSET clause to extract only the required
//	** rows from the sourct table, just as it does for an RBU update operation.
//	*/
func _rbuVacuumIndexStart(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var bFailed, i, iCid, iCol int32
	var zCol, zCollate, zLhs, zOrder, zQuoted, zRet, zSelect, zSep, zVector uintptr
	var _ /* pSel at bp+4 */ uintptr
	var _ /* pXInfo at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _ = bFailed, i, iCid, iCol, zCol, zCollate, zLhs, zOrder, zQuoted, zRet, zSelect, zSep, zVector
	zOrder = uintptr(0)
	zLhs = uintptr(0)
	zSelect = uintptr(0)
	zVector = uintptr(0)
	zRet = uintptr(0)
	bFailed = 0
	zSep = __ccgo_ts + 1711
	iCol = 0
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32256, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx)))
	for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
		iCid = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
		zCollate = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(4))
		if Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(3)) != 0 {
			bFailed = int32(1)
			break
		}
		if iCid < 0 {
			if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_IPK) {
				i = 0
				for {
					if !(int32(**(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i)))) == 0) {
						break
					}
					goto _1
				_1:
					;
					i = i + 1
				}
				zCol = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*4))
			} else {
				zCol = __ccgo_ts + 32579
			}
		} else {
			zCol = **(**uintptr)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iCid)*4))
		}
		zLhs = _rbuMPrintf(tls, p, __ccgo_ts+32587, libc.VaList(bp+16, zLhs, zSep, zCol, zCollate))
		zOrder = _rbuMPrintf(tls, p, __ccgo_ts+32608, libc.VaList(bp+16, zOrder, zSep, iCol, zCol, zCollate))
		zSelect = _rbuMPrintf(tls, p, __ccgo_ts+32644, libc.VaList(bp+16, zSelect, zSep, iCol, zCol))
		zSep = __ccgo_ts + 17436
		iCol = iCol + 1
	}
	_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
	if bFailed != 0 {
		goto index_start_out
	}
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32671, libc.VaList(bp+16, zSelect, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zOrder)))
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4))) {
			zSep = __ccgo_ts + 1711
			iCol = 0
			for {
				if !(iCol < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol) {
					break
				}
				zQuoted = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp + 4)), iCol)
				if zQuoted == uintptr(0) {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
				} else {
					if int32(**(**int8)(__ccgo_up(zQuoted))) == int32('N') {
						bFailed = int32(1)
						break
					}
				}
				zVector = _rbuMPrintf(tls, p, __ccgo_ts+32719, libc.VaList(bp+16, zVector, zSep, zQuoted))
				zSep = __ccgo_ts + 17436
				goto _2
			_2:
				;
				iCol = iCol + 1
			}
			if !(bFailed != 0) {
				zRet = _rbuMPrintf(tls, p, __ccgo_ts+32726, libc.VaList(bp+16, zLhs, zVector))
			}
		}
		_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp + 4)))
	}
	goto index_start_out
index_start_out:
	;
	Xsqlite3_free(tls, zOrder)
	Xsqlite3_free(tls, zSelect)
	Xsqlite3_free(tls, zVector)
	Xsqlite3_free(tls, zLhs)
	return zRet
}

// C documentation
//
//	/*
//	** This function is called as part of restarting an RBU vacuum within
//	** stage 1 of the process (while the *-oal file is being built) while
//	** updating a table (not an index). The table may be a rowid table or
//	** a WITHOUT ROWID table. It queries the target database to find the
//	** largest key that has already been written to the target table and
//	** constructs a WHERE clause that can be used to extract the remaining
//	** rows from the source table. For a rowid table, the WHERE clause
//	** is of the form:
//	**
//	**     "WHERE _rowid_ > ?"
//	**
//	** and for WITHOUT ROWID tables:
//	**
//	**     "WHERE (key1, key2) > (?, ?)"
//	**
//	** Instead of "?" placeholders, the actual WHERE clauses created by
//	** this function contain literal SQL values.
//	*/
func _rbuVacuumTableStart(tls *libc.TLS, p uintptr, pIter uintptr, bRowid int32, zWrite uintptr) (r uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var iMax Tsqlite3_int64
	var zList, zOrder, zRet, zSelect, zVal uintptr
	var _ /* pMax at bp+0 */ uintptr
	_, _, _, _, _, _ = iMax, zList, zOrder, zRet, zSelect, zVal
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	zRet = uintptr(0)
	if bRowid != 0 {
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32441, libc.VaList(bp+16, zWrite, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl)))
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
			iMax = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp)), 0)
			zRet = _rbuMPrintf(tls, p, __ccgo_ts+32473, libc.VaList(bp+16, iMax))
		}
		_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
	} else {
		zOrder = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+1711, __ccgo_ts+17436, __ccgo_ts+32496)
		zSelect = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+32502, __ccgo_ts+32509, __ccgo_ts+6474)
		zList = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+1711, __ccgo_ts+17436, __ccgo_ts+1711)
		if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
			(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32517, libc.VaList(bp+16, zSelect, zWrite, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, zOrder)))
			if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
				zVal = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), 0)
				zRet = _rbuMPrintf(tls, p, __ccgo_ts+32559, libc.VaList(bp+16, zList, zVal))
			}
			_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
		}
		Xsqlite3_free(tls, zOrder)
		Xsqlite3_free(tls, zSelect)
		Xsqlite3_free(tls, zList)
	}
	return zRet
}

// C documentation
//
//	/*
//	** Open an rbu file handle.
//	*/
func _rbuVfsOpen(tls *libc.TLS, pVfs uintptr, zName uintptr, pFile uintptr, flags int32, pOutFlags uintptr) (r int32) {
	var nOpen Tsize_t
	var oflags, rc int32
	var pDb, pFd, pMeth, pRbuVfs, pRealVfs, zOpen uintptr
	_, _, _, _, _, _, _, _, _ = nOpen, oflags, pDb, pFd, pMeth, pRbuVfs, pRealVfs, rc, zOpen
	pRbuVfs = pVfs
	pRealVfs = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpRealVfs
	pFd = pFile
	rc = SQLITE_OK
	zOpen = zName
	oflags = flags
	libc.Xmemset(tls, pFd, 0, uint32(64))
	(*Trbu_file)(unsafe.Pointer(pFd)).FpReal = pFd + 1*64
	(*Trbu_file)(unsafe.Pointer(pFd)).FpRbuVfs = pRbuVfs
	(*Trbu_file)(unsafe.Pointer(pFd)).FopenFlags = flags
	if zName != 0 {
		if flags&int32(SQLITE_OPEN_MAIN_DB) != 0 {
			/* A main database has just been opened. The following block sets
			 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
			 ** the name of the *-wal file this db connection will use. SQLite
			 ** happens to pass a pointer to this buffer when using xAccess()
			 ** or xOpen() to operate on the *-wal file.  */
			(*Trbu_file)(unsafe.Pointer(pFd)).FzWal = Xsqlite3_filename_wal(tls, zName)
		} else {
			if flags&int32(SQLITE_OPEN_WAL) != 0 {
				pDb = _rbuFindMaindb(tls, pRbuVfs, zName, 0)
				if pDb != 0 {
					if (*Trbu_file)(unsafe.Pointer(pDb)).FpRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pDb)).FpRbu)).FeStage == int32(RBU_STAGE_OAL) {
						if (*Tsqlite3rbu)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pDb)).FpRbu)).FzTarget == uintptr(0) {
							zOpen = Xsqlite3_db_filename(tls, (*Tsqlite3rbu)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pDb)).FpRbu)).FdbRbu, __ccgo_ts+8033)
							zOpen = Xsqlite3_filename_wal(tls, zOpen)
						}
						nOpen = libc.Xstrlen(tls, zOpen)
						**(**int8)(__ccgo_up(zOpen + uintptr(nOpen-uint32(3)))) = int8('o')
						(*Trbu_file)(unsafe.Pointer(pFd)).FpRbu = (*Trbu_file)(unsafe.Pointer(pDb)).FpRbu
					}
					(*Trbu_file)(unsafe.Pointer(pDb)).FpWalFd = pFd
				}
			}
		}
	} else {
		(*Trbu_file)(unsafe.Pointer(pFd)).FpRbu = (*Trbu_vfs)(unsafe.Pointer(pRbuVfs)).FpRbu
	}
	if oflags&int32(SQLITE_OPEN_MAIN_DB) != 0 && Xsqlite3_uri_boolean(tls, zName, __ccgo_ts+35982, 0) != 0 {
		oflags = libc.Int32FromInt32(SQLITE_OPEN_TEMP_DB) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_EXCLUSIVE) | libc.Int32FromInt32(SQLITE_OPEN_DELETEONCLOSE)
		zOpen = uintptr(0)
	}
	if rc == SQLITE_OK {
		rc = (*(*func(*libc.TLS, uintptr, Tsqlite3_filename, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pRealVfs)).FxOpen})))(tls, pRealVfs, zOpen, (*Trbu_file)(unsafe.Pointer(pFd)).FpReal, oflags, pOutFlags)
	}
	if (*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pFd)).FpReal)).FpMethods != 0 {
		pMeth = (*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(pFd)).FpReal)).FpMethods
		/* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
		 ** pointer and, if the file is a main database file, link it into the
		 ** mutex protected linked list of all such files.  */
		if (*Tsqlite3_io_methods)(unsafe.Pointer(pMeth)).FiVersion < int32(2) || (*Tsqlite3_io_methods)(unsafe.Pointer(pMeth)).FxShmLock == uintptr(0) {
			(*Tsqlite3_file)(unsafe.Pointer(pFile)).FpMethods = uintptr(unsafe.Pointer(&_rbuvfs_io_methods1))
		} else {
			(*Tsqlite3_file)(unsafe.Pointer(pFile)).FpMethods = uintptr(unsafe.Pointer(&_rbuvfs_io_methods))
		}
		if flags&int32(SQLITE_OPEN_MAIN_DB) != 0 {
			_rbuMainlistAdd(tls, pFd)
		}
	} else {
		Xsqlite3_free(tls, (*Trbu_file)(unsafe.Pointer(pFd)).FzDel)
	}
	return rc
}

// C documentation
//
//	/*
//	** Read data from an rbuVfs-file.
//	*/
func _rbuVfsRead(tls *libc.TLS, pFile uintptr, zBuf uintptr, iAmt int32, iOfst Tsqlite_int64) (r int32) {
	var aBuf, p, pBuf, pFd, pRbu uintptr
	var iRoot Tu32
	var rc, v1 int32
	_, _, _, _, _, _, _, _ = aBuf, iRoot, p, pBuf, pFd, pRbu, rc, v1
	p = pFile
	pRbu = (*Trbu_file)(unsafe.Pointer(p)).FpRbu
	if pRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_CAPTURE) {
		rc = _rbuCaptureWalRead(tls, (*Trbu_file)(unsafe.Pointer(p)).FpRbu, iOfst, iAmt)
	} else {
		if pRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_OAL) && (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_WAL) != 0 && iOfst >= (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FiOalSz {
			rc = SQLITE_OK
			libc.Xmemset(tls, zBuf, 0, uint32(iAmt))
		} else {
			rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Tsqlite3_int64) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpReal)).FpMethods)).FxRead})))(tls, (*Trbu_file)(unsafe.Pointer(p)).FpReal, zBuf, iAmt, iOfst)
			/* If this is being called to read the first page of the target
			 ** database as part of an rbu vacuum operation, synthesize the
			 ** contents of the first page if it does not yet exist. Otherwise,
			 ** SQLite will not check for a *-wal file.  */
			if pRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FzTarget == uintptr(0) && rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8) && iOfst == 0 && (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_MAIN_DB) != 0 && (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).Frc == SQLITE_OK {
				pFd = (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FpRbuFd
				rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, Tsqlite3_int64) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(pFd)).FpMethods)).FxRead})))(tls, pFd, zBuf, iAmt, iOfst)
				if rc == SQLITE_OK {
					aBuf = zBuf
					if _rbuGetU32(tls, aBuf+52) != 0 {
						v1 = int32(1)
					} else {
						v1 = 0
					}
					iRoot = uint32(v1)
					_rbuPutU32(tls, aBuf+52, iRoot)                                                                                         /* largest root page number */
					_rbuPutU32(tls, aBuf+36, uint32(0))                                                                                     /* number of free pages */
					_rbuPutU32(tls, aBuf+32, uint32(0))                                                                                     /* first page on free list trunk */
					_rbuPutU32(tls, aBuf+28, uint32(1))                                                                                     /* size of db file in pages */
					_rbuPutU32(tls, aBuf+24, (*Trbu_file)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FpRbuFd)).FiCookie+uint32(1)) /* Change counter */
					if iAmt > int32(100) {
						libc.Xmemset(tls, aBuf+100, 0, uint32(iAmt-int32(100)))
						_rbuPutU16(tls, aBuf+105, uint16(iAmt&int32(0xFFFF)))
						**(**Tu8)(__ccgo_up(aBuf + 100)) = uint8(0x0D)
					}
				}
			}
		}
		if rc == SQLITE_OK && iOfst == 0 && (*Trbu_file)(unsafe.Pointer(p)).FopenFlags&int32(SQLITE_OPEN_MAIN_DB) != 0 {
			/* These look like magic numbers. But they are stable, as they are part
			 ** of the definition of the SQLite file format, which may not change. */
			pBuf = zBuf
			(*Trbu_file)(unsafe.Pointer(p)).FiCookie = _rbuGetU32(tls, pBuf+24)
			(*Trbu_file)(unsafe.Pointer(p)).FiWriteVer = **(**Tu8)(__ccgo_up(pBuf + 19))
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Take or release a shared-memory lock.
//	*/
func _rbuVfsShmLock(tls *libc.TLS, pFile uintptr, ofst int32, n int32, flags int32) (r int32) {
	var bCapture, rc int32
	var p, pRbu uintptr
	_, _, _, _ = bCapture, p, pRbu, rc
	p = pFile
	pRbu = (*Trbu_file)(unsafe.Pointer(p)).FpRbu
	rc = SQLITE_OK
	if pRbu != 0 && ((*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_OAL) || (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_MOVE) || (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_DONE)) {
		/* Prevent SQLite from taking a shm-lock on the target file when it
		 ** is supplying heap memory to the upper layer in place of *-shm
		 ** segments. */
		if ofst == int32(WAL_LOCK_CKPT) && n == int32(1) {
			rc = int32(SQLITE_BUSY)
		}
	} else {
		bCapture = 0
		if pRbu != 0 && (*Tsqlite3rbu)(unsafe.Pointer(pRbu)).FeStage == int32(RBU_STAGE_CAPTURE) {
			bCapture = int32(1)
		}
		if bCapture == 0 || 0 == flags&int32(SQLITE_SHM_UNLOCK) {
			rc = (*(*func(*libc.TLS, uintptr, int32, int32, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpReal)).FpMethods)).FxShmLock})))(tls, (*Trbu_file)(unsafe.Pointer(p)).FpReal, ofst, n, flags)
			if bCapture != 0 && rc == SQLITE_OK {
				**(**Tu32)(__ccgo_up(pRbu + 204)) |= uint32((int32(1)<<n - int32(1)) << ofst)
			}
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
//	*/
func _rbuVfsShmMap(tls *libc.TLS, pFile uintptr, iRegion int32, szRegion int32, isWrite int32, pp uintptr) (r int32) {
	var apNew, p, pNew uintptr
	var eStage, rc, v1 int32
	var nByte Tsqlite3_int64
	_, _, _, _, _, _, _ = apNew, eStage, nByte, p, pNew, rc, v1
	p = pFile
	rc = SQLITE_OK
	if (*Trbu_file)(unsafe.Pointer(p)).FpRbu != 0 {
		v1 = (*Tsqlite3rbu)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpRbu)).FeStage
	} else {
		v1 = 0
	}
	eStage = v1
	/* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
	 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
	 ** instead of a file on disk.  */
	if eStage == int32(RBU_STAGE_OAL) {
		nByte = int64(uint32(iRegion+libc.Int32FromInt32(1)) * uint32(4))
		apNew = Xsqlite3_realloc64(tls, (*Trbu_file)(unsafe.Pointer(p)).FapShm, uint64(nByte))
		/* This is an RBU connection that uses its own heap memory for the
		 ** pages of the *-shm file. Since no other process can have run
		 ** recovery, the connection must request *-shm pages in order
		 ** from start to finish.  */
		if apNew == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		} else {
			libc.Xmemset(tls, apNew+uintptr((*Trbu_file)(unsafe.Pointer(p)).FnShm)*4, 0, uint32(4)*uint32(libc.Int32FromInt32(1)+iRegion-(*Trbu_file)(unsafe.Pointer(p)).FnShm))
			(*Trbu_file)(unsafe.Pointer(p)).FapShm = apNew
			(*Trbu_file)(unsafe.Pointer(p)).FnShm = iRegion + int32(1)
		}
		if rc == SQLITE_OK {
			pNew = Xsqlite3_malloc64(tls, uint64(szRegion))
			if pNew == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				libc.Xmemset(tls, pNew, 0, uint32(szRegion))
				**(**uintptr)(__ccgo_up((*Trbu_file)(unsafe.Pointer(p)).FapShm + uintptr(iRegion)*4)) = pNew
			}
		}
		if rc == SQLITE_OK {
			**(**uintptr)(__ccgo_up(pp)) = **(**uintptr)(__ccgo_up((*Trbu_file)(unsafe.Pointer(p)).FapShm + uintptr(iRegion)*4))
		} else {
			**(**uintptr)(__ccgo_up(pp)) = uintptr(0)
		}
	} else {
		rc = (*(*func(*libc.TLS, uintptr, int32, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*Trbu_file)(unsafe.Pointer(p)).FpReal)).FpMethods)).FxShmMap})))(tls, (*Trbu_file)(unsafe.Pointer(p)).FpReal, iRegion, szRegion, isWrite, pp)
	}
	return rc
}

// C documentation
//
//	/*
//	** The iterator currently points to a table (not index) of type
//	** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
//	** declaration for the corresponding imposter table. For example,
//	** if the iterator points to a table created as:
//	**
//	**   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
//	**
//	** this function returns:
//	**
//	**   PRIMARY KEY("b", "a" DESC)
//	*/
func _rbuWithoutRowidPK(tls *libc.TLS, p uintptr, pIter uintptr) (r uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var z, zCol, zDesc, zIdx, zOrig, zSep, v1 uintptr
	var _ /* pXInfo at bp+4 */ uintptr
	var _ /* pXList at bp+0 */ uintptr
	_, _, _, _, _, _, _ = z, zCol, zDesc, zIdx, zOrig, zSep, v1
	z = uintptr(0)
	if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		zSep = __ccgo_ts + 33076
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)     /* PRAGMA index_list = (pIter->zTbl) */
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* PRAGMA index_xinfo = <pk-index> */
		(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32228, libc.VaList(bp+16, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl)))
		for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
			zOrig = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(3))
			if zOrig != 0 && libc.Xstrcmp(tls, zOrig, __ccgo_ts+19074) == 0 {
				zIdx = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
				if zIdx != 0 {
					(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+4, p+36, Xsqlite3_mprintf(tls, __ccgo_ts+32256, libc.VaList(bp+16, zIdx)))
				}
				break
			}
		}
		_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp)))
		for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4))) {
			if Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp + 4)), int32(5)) != 0 {
				/* int iCid = sqlite3_column_int(pXInfo, 0); */
				zCol = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp + 4)), int32(2))
				if Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp + 4)), int32(3)) != 0 {
					v1 = __ccgo_ts + 32496
				} else {
					v1 = __ccgo_ts + 1711
				}
				zDesc = v1
				z = _rbuMPrintf(tls, p, __ccgo_ts+33089, libc.VaList(bp+16, z, zSep, zCol, zDesc))
				zSep = __ccgo_ts + 17436
			}
		}
		z = _rbuMPrintf(tls, p, __ccgo_ts+33100, libc.VaList(bp+16, z))
		_rbuFinalize(tls, p, **(**uintptr)(__ccgo_up(bp + 4)))
	}
	return z
}

// C documentation
//
//	/*
//	** Read the content for page pPg out of the database file (or out of
//	** the WAL if that is where the most recent copy if found) into
//	** pPg->pData. A shared lock or greater must be held on the database
//	** file before this function is called.
//	**
//	** If page 1 is read, then the value of Pager.dbFileVers[] is set to
//	** the value read from the database file.
//	**
//	** If an IO error occurs, then the IO error is returned to the caller.
//	** Otherwise, SQLITE_OK is returned.
//	*/
func _readDbPage(tls *libc.TLS, pPg uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var dbFileVers, pPager uintptr
	var iOffset Ti64
	var rc int32
	var _ /* iFrame at bp+0 */ Tu32
	_, _, _, _ = dbFileVers, iOffset, pPager, rc
	pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager /* Pager object associated with page pPg */
	rc = SQLITE_OK                                  /* Return code */
	**(**Tu32)(__ccgo_up(bp)) = uint32(0)           /* Frame of WAL containing pgno */
	if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) {
		rc = _sqlite3WalFindFrame(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno, bp)
		if rc != 0 {
			return rc
		}
	}
	if **(**Tu32)(__ccgo_up(bp)) != 0 {
		rc = _sqlite3WalReadFrame(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, **(**Tu32)(__ccgo_up(bp)), int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), (*TPgHdr)(unsafe.Pointer(pPg)).FpData)
	} else {
		iOffset = int64((*TPgHdr)(unsafe.Pointer(pPg)).Fpgno-libc.Uint32FromInt32(1)) * (*TPager)(unsafe.Pointer(pPager)).FpageSize
		rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, (*TPgHdr)(unsafe.Pointer(pPg)).FpData, int32((*TPager)(unsafe.Pointer(pPager)).FpageSize), iOffset)
		if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8) {
			rc = SQLITE_OK
		}
	}
	if (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno == uint32(1) {
		if rc != 0 {
			/* If the read is unsuccessful, set the dbFileVers[] to something
			 ** that will never be a valid file version.  dbFileVers[] is a copy
			 ** of bytes 24..39 of the database.  Bytes 28..31 should always be
			 ** zero or the size of the database in page. Bytes 32..35 and 35..39
			 ** should be page numbers which are never 0xffffffff.  So filling
			 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
			 **
			 ** For an encrypted database, the situation is more complex:  bytes
			 ** 24..39 of the database are white noise.  But the probability of
			 ** white noise equaling 16 bytes of 0xff is vanishingly small so
			 ** we should still be ok.
			 */
			libc.Xmemset(tls, pPager+112, int32(0xff), uint32(16))
		} else {
			dbFileVers = (*TPgHdr)(unsafe.Pointer(pPg)).FpData + 24
			libc.Xmemcpy(tls, pPager+112, dbFileVers, uint32(16))
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** The journal file must be open when this is called. A journal header file
//	** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
//	** file. The current location in the journal file is given by
//	** pPager->journalOff. See comments above function writeJournalHdr() for
//	** a description of the journal header format.
//	**
//	** If the header is read successfully, *pNRec is set to the number of
//	** page records following this header and *pDbSize is set to the size of the
//	** database before the transaction began, in pages. Also, pPager->cksumInit
//	** is set to the value read from the journal header. SQLITE_OK is returned
//	** in this case.
//	**
//	** If the journal header file appears to be corrupted, SQLITE_DONE is
//	** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
//	** cannot be read from the journal file an error code is returned.
//	*/
func _readJournalHdr(tls *libc.TLS, pPager uintptr, isHot int32, journalSize Ti64, pNRec uintptr, pDbSize uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iHdrOff Ti64
	var rc, v1, v2, v4 int32
	var v3, v5 bool
	var _ /* aMagic at bp+0 */ [8]uint8
	var _ /* iPageSize at bp+8 */ Tu32
	var _ /* iSectorSize at bp+12 */ Tu32
	_, _, _, _, _, _, _ = iHdrOff, rc, v1, v2, v3, v4, v5 /* Offset of journal header being read */
	/* Journal file must be open. */
	/* Advance Pager.journalOff to the start of the next sector. If the
	 ** journal file is too small for there to be a header stored at this
	 ** point, return SQLITE_DONE.
	 */
	(*TPager)(unsafe.Pointer(pPager)).FjournalOff = _journalHdrOffset(tls, pPager)
	if (*TPager)(unsafe.Pointer(pPager)).FjournalOff+int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) > journalSize {
		return int32(SQLITE_DONE)
	}
	iHdrOff = (*TPager)(unsafe.Pointer(pPager)).FjournalOff
	/* Read in the first 8 bytes of the journal header. If they do not match
	 ** the  magic string found at the start of each journal header, return
	 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
	 ** proceed.
	 */
	if isHot != 0 || iHdrOff != (*TPager)(unsafe.Pointer(pPager)).FjournalHdr {
		rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp, int32(8), iHdrOff)
		if rc != 0 {
			return rc
		}
		if libc.Xmemcmp(tls, bp, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8)) != 0 {
			return int32(SQLITE_DONE)
		}
	}
	/* Read the first three 32-bit fields of the journal header: The nRec
	 ** field, the checksum-initializer and the database size at the start
	 ** of the transaction. Return an error code if anything goes wrong.
	 */
	v1 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(8), pNRec)
	rc = v1
	if v3 = SQLITE_OK != v1; !v3 {
		v2 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(12), pPager+52)
		rc = v2
	}
	if v5 = v3 || SQLITE_OK != v2; !v5 {
		v4 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(16), pDbSize)
		rc = v4
	}
	if v5 || SQLITE_OK != v4 {
		return rc
	}
	if (*TPager)(unsafe.Pointer(pPager)).FjournalOff == 0 { /* Sector-size field of journal header */
		/* Read the page-size and sector-size journal header fields. */
		v1 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(20), bp+12)
		rc = v1
		if v3 = SQLITE_OK != v1; !v3 {
			v2 = _read32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(24), bp+8)
			rc = v2
		}
		if v3 || SQLITE_OK != v2 {
			return rc
		}
		/* Versions of SQLite prior to 3.5.8 set the page-size field of the
		 ** journal header to zero. In this case, assume that the Pager.pageSize
		 ** variable is already set to the correct page size.
		 */
		if **(**Tu32)(__ccgo_up(bp + 8)) == uint32(0) {
			**(**Tu32)(__ccgo_up(bp + 8)) = uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize)
		}
		/* Check that the values read from the page-size and sector-size fields
		 ** are within range. To be 'in range', both values need to be a power
		 ** of two greater than or equal to 512 or 32, and not greater than their
		 ** respective compile time maximum limits.
		 */
		if **(**Tu32)(__ccgo_up(bp + 8)) < uint32(512) || **(**Tu32)(__ccgo_up(bp + 12)) < uint32(32) || **(**Tu32)(__ccgo_up(bp + 8)) > uint32(SQLITE_MAX_PAGE_SIZE) || **(**Tu32)(__ccgo_up(bp + 12)) > uint32(MAX_SECTOR_SIZE) || (**(**Tu32)(__ccgo_up(bp + 8))-uint32(1))&**(**Tu32)(__ccgo_up(bp + 8)) != uint32(0) || (**(**Tu32)(__ccgo_up(bp + 12))-uint32(1))&**(**Tu32)(__ccgo_up(bp + 12)) != uint32(0) {
			/* If the either the page-size or sector-size in the journal-header is
			 ** invalid, then the process that wrote the journal-header must have
			 ** crashed before the header was synced. In this case stop reading
			 ** the journal file here.
			 */
			return int32(SQLITE_DONE)
		}
		/* Update the page-size to match the value read from the journal.
		 ** Use a testcase() macro to make sure that malloc failure within
		 ** PagerSetPagesize() is tested.
		 */
		rc = _sqlite3PagerSetPagesize(tls, pPager, bp+8, -int32(1))
		/* Update the assumed sector-size to match the value used by
		 ** the process that created this journal. If this journal was
		 ** created by a process other than this one, then this routine
		 ** is being called from within pager_playback(). The local value
		 ** of Pager.sectorSize is restored at the end of that routine.
		 */
		(*TPager)(unsafe.Pointer(pPager)).FsectorSize = **(**Tu32)(__ccgo_up(bp + 12))
	}
	**(**Ti64)(__ccgo_up(pPager + 80)) += int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize)
	return rc
}

// C documentation
//
//	/*
//	** Parameter pJrnl is a file-handle open on a journal file. This function
//	** attempts to read a super-journal file name from the end of the journal
//	** file. If successful, it sets output parameter (*pzSuper) to point to a
//	** buffer containing the super-journal name as a nul-terminated string.
//	** The caller is responsible for freeing the buffer using freeSuperJournal().
//	**
//	** Refer to comments above writeSuperJournal() for the format used to store
//	** a super-journal file name at the end of a journal file.
//	**
//	** Parameter nSuper is passed the maximum allowable size of the super journal
//	** name in bytes. If the super-journal name in the journal is longer than
//	** nSuper bytes (including a nul-terminator), then this is handled as if no
//	** super-journal name were present in the journal.
//	**
//	** If there is no super-journal name at the end of pJrnl, (*pzSuper) is
//	** set to 0 and SQLITE_OK is returned. Or, if an error occurs while reading
//	** the super-journal name, an SQLite error code is returned and (*pzSuper)
//	** is set to 0.
//	*/
func _readSuperJournal(tls *libc.TLS, pJrnl uintptr, nSuper Tu64, pzSuper uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var rc, v1, v2, v4, v6 int32
	var u Tu32
	var zOut uintptr
	var v3, v5, v7 bool
	var _ /* aMagic at bp+24 */ [8]uint8
	var _ /* cksum at bp+16 */ Tu32
	var _ /* len at bp+0 */ Tu32
	var _ /* szJ at bp+8 */ Ti64
	_, _, _, _, _, _, _, _, _, _ = rc, u, zOut, v1, v2, v3, v4, v5, v6, v7 /* A buffer to hold the magic header */
	zOut = uintptr(0)
	**(**uintptr)(__ccgo_up(pzSuper)) = uintptr(0)
	v1 = _sqlite3OsFileSize(tls, pJrnl, bp+8)
	rc = v1
	if v3 = SQLITE_OK != v1 || **(**Ti64)(__ccgo_up(bp + 8)) < int64(16); !v3 {
		v2 = _read32bits(tls, pJrnl, **(**Ti64)(__ccgo_up(bp + 8))-int64(16), bp)
		rc = v2
	}
	if v5 = v3 || SQLITE_OK != v2 || uint64(**(**Tu32)(__ccgo_up(bp))) >= nSuper || int64(**(**Tu32)(__ccgo_up(bp))) > **(**Ti64)(__ccgo_up(bp + 8))-int64(16) || **(**Tu32)(__ccgo_up(bp)) == uint32(0); !v5 {
		v4 = _read32bits(tls, pJrnl, **(**Ti64)(__ccgo_up(bp + 8))-int64(12), bp+16)
		rc = v4
	}
	if v7 = v5 || SQLITE_OK != v4; !v7 {
		v6 = _sqlite3OsRead(tls, pJrnl, bp+24, int32(8), **(**Ti64)(__ccgo_up(bp + 8))-int64(8))
		rc = v6
	}
	if v7 || SQLITE_OK != v6 || libc.Xmemcmp(tls, bp+24, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8)) != 0 {
		return rc
	}
	zOut = _sqlite3MallocZero(tls, uint64(uint32(4)+**(**Tu32)(__ccgo_up(bp))+uint32(2)))
	if !(zOut != 0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		zOut = zOut + 4
		v1 = _sqlite3OsRead(tls, pJrnl, zOut, int32(**(**Tu32)(__ccgo_up(bp))), **(**Ti64)(__ccgo_up(bp + 8))-int64(16)-int64(**(**Tu32)(__ccgo_up(bp))))
		rc = v1
		if SQLITE_OK == v1 { /* Unsigned loop counter */
			/* See if the checksum matches the super-journal name */
			u = uint32(0)
			for {
				if !(u < **(**Tu32)(__ccgo_up(bp))) {
					break
				}
				**(**Tu32)(__ccgo_up(bp + 16)) = **(**Tu32)(__ccgo_up(bp + 16)) - uint32(**(**int8)(__ccgo_up(zOut + uintptr(u))))
				goto _9
			_9:
				;
				u = u + 1
			}
		}
		if rc != SQLITE_OK || **(**Tu32)(__ccgo_up(bp + 16)) != 0 {
			/* If the checksum doesn't add up, then one or more of the disk sectors
			 ** containing the super-journal filename is corrupted. This means
			 ** definitely roll back, so just return SQLITE_OK and report a (nul)
			 ** super-journal filename.  */
			_freeSuperJournal(tls, zOut)
			zOut = uintptr(0)
		}
	}
	**(**uintptr)(__ccgo_up(pzSuper)) = zOut
	return rc
}

// C documentation
//
//	/*
//	** Array apCell[] contains pointers to nCell b-tree page cells. The
//	** szCell[] array contains the size in bytes of each cell. This function
//	** replaces the current contents of page pPg with the contents of the cell
//	** array.
//	**
//	** Some of the cells in apCell[] may currently be stored in pPg. This
//	** function works around problems caused by this by making a copy of any
//	** such cells before overwriting the page data.
//	**
//	** The MemPage.nFree field is invalidated by this function. It is the
//	** responsibility of the caller to set it correctly.
//	*/
func _rebuildPage(tls *libc.TLS, pCArray uintptr, iFirst int32, nCell int32, pPg uintptr) (r int32) {
	var aData, pCell, pCellptr, pData, pEnd, pSrcEnd, pTmp uintptr
	var hdr, i, iEnd, k, usableSize int32
	var j Tu32
	var sz Tu16
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, hdr, i, iEnd, j, k, pCell, pCellptr, pData, pEnd, pSrcEnd, pTmp, sz, usableSize
	hdr = int32((*TMemPage)(unsafe.Pointer(pPg)).FhdrOffset) /* Offset of header on pPg */
	aData = (*TMemPage)(unsafe.Pointer(pPg)).FaData          /* Pointer to data for pPg */
	usableSize = int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPg)).FpBt)).FusableSize)
	pEnd = aData + uintptr(usableSize)
	i = iFirst       /* Start of cell content area */
	iEnd = i + nCell /* Loop terminator */
	pCellptr = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx
	pTmp = _sqlite3PagerTempSpace(tls, (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPg)).FpBt)).FpPager) /* Current pCArray->apEnd[k] value */
	j = uint32(int32(**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(5)))))<<libc.Int32FromInt32(8) | int32(**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(5)) + 1))))
	if j > uint32(usableSize) {
		j = uint32(0)
	}
	libc.Xmemcpy(tls, pTmp+uintptr(j), aData+uintptr(j), uint32(usableSize)-j)
	k = 0
	for {
		if !(**(**int32)(__ccgo_up(pCArray + 40 + uintptr(k)*4)) <= i) {
			break
		}
		goto _1
	_1:
		;
		k = k + 1
	}
	pSrcEnd = **(**uintptr)(__ccgo_up(pCArray + 16 + uintptr(k)*4))
	pData = pEnd
	for int32(1) != 0 {
		pCell = **(**uintptr)(__ccgo_up((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*4))
		sz = **(**Tu16)(__ccgo_up((*TCellArray)(unsafe.Pointer(pCArray)).FszCell + uintptr(i)*2))
		if uint32(pCell) >= uint32(aData+uintptr(j)) && uint32(pCell) < uint32(pEnd) {
			if uint32(pCell+uintptr(sz)) > uint32(pEnd) {
				return _sqlite3CorruptError(tls, int32(80905))
			}
			pCell = pTmp + uintptr(int32(pCell)-int32(aData))
		} else {
			if uint32(pCell+uintptr(sz)) > uint32(pSrcEnd) && uint32(pCell) < uint32(pSrcEnd) {
				return _sqlite3CorruptError(tls, int32(80910))
			}
		}
		pData = pData - uintptr(sz)
		**(**Tu8)(__ccgo_up(pCellptr)) = uint8((int32(pData) - int32(aData)) >> libc.Int32FromInt32(8))
		**(**Tu8)(__ccgo_up(pCellptr + 1)) = uint8(int32(pData) - int32(aData))
		pCellptr = pCellptr + uintptr(2)
		if pData < pCellptr {
			return _sqlite3CorruptError(tls, int32(80916))
		}
		libc.Xmemmove(tls, pData, pCell, uint32(sz))
		i = i + 1
		if i >= iEnd {
			break
		}
		if **(**int32)(__ccgo_up(pCArray + 40 + uintptr(k)*4)) <= i {
			k = k + 1
			pSrcEnd = **(**uintptr)(__ccgo_up(pCArray + 16 + uintptr(k)*4))
		}
	}
	/* The pPg->nFree field is now set incorrectly. The caller will fix it. */
	(*TMemPage)(unsafe.Pointer(pPg)).FnCell = uint16(nCell)
	(*TMemPage)(unsafe.Pointer(pPg)).FnOverflow = uint8(0)
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(1)))) = uint8(libc.Int32FromInt32(0) >> libc.Int32FromInt32(8))
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(1)) + 1)) = uint8(libc.Int32FromInt32(0))
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(3)))) = uint8(int32((*TMemPage)(unsafe.Pointer(pPg)).FnCell) >> libc.Int32FromInt32(8))
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(3)) + 1)) = uint8((*TMemPage)(unsafe.Pointer(pPg)).FnCell)
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(5)))) = uint8((int32(pData) - int32(aData)) >> libc.Int32FromInt32(8))
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(5)) + 1)) = uint8(int32(pData) - int32(aData))
	**(**Tu8)(__ccgo_up(aData + uintptr(hdr+int32(7)))) = uint8(0x00)
	return SQLITE_OK
}

// C documentation
//
//	/* Recompute the colNotIdxed field of the Index.
//	**
//	** colNotIdxed is a bitmask that has a 0 bit representing each indexed
//	** columns that are within the first 63 columns of the table and a 1 for
//	** all other bits (all columns that are not in the index).  The
//	** high-order bit of colNotIdxed is always 1.  All unindexed columns
//	** of the table have a 1.
//	**
//	** 2019-10-24:  For the purpose of this computation, virtual columns are
//	** not considered to be covered by the index, even if they are in the
//	** index, because we do not trust the logic in whereIndexExprTrans() to be
//	** able to find all instances of a reference to the indexed table column
//	** and convert them into references to the index.  Hence we always want
//	** the actual table at hand in order to recompute the virtual column, if
//	** necessary.
//	**
//	** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
//	** to determine if the index is covering index.
//	*/
func _recomputeColumnsNotIndexed(tls *libc.TLS, pIdx uintptr) {
	var j, x int32
	var m TBitmask
	var pTab uintptr
	_, _, _, _ = j, m, pTab, x
	m = uint64(0)
	pTab = (*TIndex)(unsafe.Pointer(pIdx)).FpTable
	j = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) - int32(1)
	for {
		if !(j >= 0) {
			break
		}
		x = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2)))
		if x >= 0 && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(x)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 {
			if x < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) {
				m = m | libc.Uint64FromInt32(1)<<x
			}
		}
		goto _1
	_1:
		;
		j = j - 1
	}
	(*TIndex)(unsafe.Pointer(pIdx)).FcolNotIdxed = ^m
	/* See note-20221022-a */
}

// C documentation
//
//	/*
//	** pSelect is a SELECT statement and pSrcItem is one item in the FROM
//	** clause of that SELECT.
//	**
//	** This routine scans the entire SELECT statement and recomputes the
//	** pSrcItem->colUsed mask.
//	*/
func _recomputeColumnsUsedExpr(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var pItem uintptr
	_ = pItem
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) {
		return WRC_Continue
	}
	pItem = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	if (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor != (*TExpr)(unsafe.Pointer(pExpr)).FiTable {
		return WRC_Continue
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) < 0 {
		return WRC_Continue
	}
	**(**TBitmask)(__ccgo_up(pItem + 24)) |= _sqlite3ExprColUsed(tls, pExpr)
	return WRC_Continue
}

// C documentation
//
//	/* Resize the hash table so that it contains "new_size" buckets.
//	**
//	** The hash table might fail to resize if sqlite3_malloc() fails or
//	** if the new size is the same as the prior size.
//	** Return TRUE if the resize occurs and false if not.
//	*/
func _rehash(tls *libc.TLS, pH uintptr, new_size uint32) (r int32) {
	var elem, new_ht, next_elem uintptr
	var v1 uint32
	_, _, _, _ = elem, new_ht, next_elem, v1 /* For looping over existing elements */
	if new_size*uint32(8) > uint32(SQLITE_MALLOC_SOFT_LIMIT) {
		new_size = libc.Uint32FromInt32(SQLITE_MALLOC_SOFT_LIMIT) / libc.Uint32FromInt64(8)
	}
	if new_size == (*THash)(unsafe.Pointer(pH)).Fhtsize {
		return 0
	}
	/* The inability to allocates space for a larger hash table is
	 ** a performance hit but it is not a fatal error.  So mark the
	 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
	 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
	 ** only zeroes the requested number of bytes whereas this module will
	 ** use the actual amount of space allocated for the hash table (which
	 ** may be larger than the requested amount).
	 */
	_sqlite3BeginBenignMalloc(tls)
	new_ht = _sqlite3Malloc(tls, uint64(new_size*uint32(8)))
	_sqlite3EndBenignMalloc(tls)
	if new_ht == uintptr(0) {
		return 0
	}
	Xsqlite3_free(tls, (*THash)(unsafe.Pointer(pH)).Fht)
	(*THash)(unsafe.Pointer(pH)).Fht = new_ht
	v1 = uint32(_sqlite3MallocSize(tls, new_ht)) / libc.Uint32FromInt64(8)
	new_size = v1
	(*THash)(unsafe.Pointer(pH)).Fhtsize = v1
	libc.Xmemset(tls, new_ht, 0, new_size*uint32(8))
	elem = (*THash)(unsafe.Pointer(pH)).Ffirst
	(*THash)(unsafe.Pointer(pH)).Ffirst = libc.UintptrFromInt32(0)
	for {
		if !(elem != 0) {
			break
		}
		next_elem = (*THashElem)(unsafe.Pointer(elem)).Fnext
		_insertElement(tls, pH, new_ht+uintptr((*THashElem)(unsafe.Pointer(elem)).Fh%new_size)*8, elem)
		goto _2
	_2:
		;
		elem = next_elem
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** Release auxiliary memory held in an array of N Mem elements.
//	**
//	** After this routine returns, all Mem elements in the array will still
//	** be valid.  Those Mem elements that were not holding auxiliary resources
//	** will be unchanged.  Mem elements which had something freed will be
//	** set to MEM_Undefined.
//	*/
func _releaseMemArray(tls *libc.TLS, p uintptr, N int32) {
	var db, pEnd, v1 uintptr
	_, _, _ = db, pEnd, v1
	if p != 0 && N != 0 {
		pEnd = p + uintptr(N)*40
		db = (*TMem)(unsafe.Pointer(p)).Fdb
		if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed != 0 {
			for {
				if (*TMem)(unsafe.Pointer(p)).FszMalloc != 0 {
					_sqlite3DbFree(tls, db, (*TMem)(unsafe.Pointer(p)).FzMalloc)
				}
				goto _2
			_2:
				;
				p += 40
				v1 = p
				if !(v1 < pEnd) {
					break
				}
			}
			return
		}
		for {
			/* This block is really an inlined version of sqlite3VdbeMemRelease()
			 ** that takes advantage of the fact that the memory cell value is
			 ** being set to NULL after releasing any dynamic resources.
			 **
			 ** The justification for duplicating code is that according to
			 ** callgrind, this causes a certain test case to hit the CPU 4.7
			 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
			 ** sqlite3MemRelease() were called from here. With -O2, this jumps
			 ** to 6.6 percent. The test case is inserting 1000 rows into a table
			 ** with no indexes using a single prepared INSERT statement, bind()
			 ** and reset(). Inserts are grouped into a transaction.
			 */
			if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 {
				_sqlite3VdbeMemRelease(tls, p)
				(*TMem)(unsafe.Pointer(p)).Fflags = uint16(MEM_Undefined)
			} else {
				if (*TMem)(unsafe.Pointer(p)).FszMalloc != 0 {
					_sqlite3DbNNFreeNN(tls, db, (*TMem)(unsafe.Pointer(p)).FzMalloc)
					(*TMem)(unsafe.Pointer(p)).FszMalloc = 0
					(*TMem)(unsafe.Pointer(p)).Fflags = uint16(MEM_Undefined)
				}
			}
			goto _4
		_4:
			;
			p += 40
			v1 = p
			if !(v1 < pEnd) {
				break
			}
		}
	}
}

// C documentation
//
//	/*
//	** Move the open database page pDbPage to location iFreePage in the
//	** database. The pDbPage reference remains valid.
//	**
//	** The isCommit flag indicates that there is no need to remember that
//	** the journal needs to be sync()ed before database page pDbPage->pgno
//	** can be written to. The caller has already promised not to write to that
//	** page.
//	*/
func _relocatePage(tls *libc.TLS, pBt uintptr, pDbPage uintptr, eType Tu8, iPtrPage TPgno, iFreePage TPgno, isCommit int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iDbPage, nextOvfl TPgno
	var pPager uintptr
	var _ /* pPtrPage at bp+0 */ uintptr
	var _ /* rc at bp+4 */ int32
	_, _, _ = iDbPage, nextOvfl, pPager /* The page that contains a pointer to pDbPage */
	iDbPage = (*TMemPage)(unsafe.Pointer(pDbPage)).Fpgno
	pPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager
	if iDbPage < uint32(3) {
		return _sqlite3CorruptError(tls, int32(77187))
	}
	/* Move page iDbPage from its current location to page number iFreePage */
	**(**int32)(__ccgo_up(bp + 4)) = _sqlite3PagerMovepage(tls, pPager, (*TMemPage)(unsafe.Pointer(pDbPage)).FpDbPage, iFreePage, isCommit)
	if **(**int32)(__ccgo_up(bp + 4)) != SQLITE_OK {
		return **(**int32)(__ccgo_up(bp + 4))
	}
	(*TMemPage)(unsafe.Pointer(pDbPage)).Fpgno = iFreePage
	/* If pDbPage was a btree-page, then it may have child pages and/or cells
	 ** that point to overflow pages. The pointer map entries for all these
	 ** pages need to be changed.
	 **
	 ** If pDbPage is an overflow page, then the first 4 bytes may store a
	 ** pointer to a subsequent overflow page. If this is the case, then
	 ** the pointer map needs to be updated for the subsequent overflow page.
	 */
	if int32(eType) == int32(PTRMAP_BTREE) || int32(eType) == int32(PTRMAP_ROOTPAGE) {
		**(**int32)(__ccgo_up(bp + 4)) = _setChildPtrmaps(tls, pDbPage)
		if **(**int32)(__ccgo_up(bp + 4)) != SQLITE_OK {
			return **(**int32)(__ccgo_up(bp + 4))
		}
	} else {
		nextOvfl = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pDbPage)).FaData)
		if nextOvfl != uint32(0) {
			_ptrmapPut(tls, pBt, nextOvfl, uint8(PTRMAP_OVERFLOW2), iFreePage, bp+4)
			if **(**int32)(__ccgo_up(bp + 4)) != SQLITE_OK {
				return **(**int32)(__ccgo_up(bp + 4))
			}
		}
	}
	/* Fix the database pointer on page iPtrPage that pointed at iDbPage so
	 ** that it points at iFreePage. Also fix the pointer map entry for
	 ** iPtrPage.
	 */
	if int32(eType) != int32(PTRMAP_ROOTPAGE) {
		**(**int32)(__ccgo_up(bp + 4)) = _btreeGetPage(tls, pBt, iPtrPage, bp, 0)
		if **(**int32)(__ccgo_up(bp + 4)) != SQLITE_OK {
			return **(**int32)(__ccgo_up(bp + 4))
		}
		**(**int32)(__ccgo_up(bp + 4)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpDbPage)
		if **(**int32)(__ccgo_up(bp + 4)) != SQLITE_OK {
			_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
			return **(**int32)(__ccgo_up(bp + 4))
		}
		**(**int32)(__ccgo_up(bp + 4)) = _modifyPagePointer(tls, **(**uintptr)(__ccgo_up(bp)), iDbPage, iFreePage, eType)
		_releasePage(tls, **(**uintptr)(__ccgo_up(bp)))
		if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
			_ptrmapPut(tls, pBt, iFreePage, eType, iPtrPage, bp+4)
		}
	}
	return **(**int32)(__ccgo_up(bp + 4))
}

// C documentation
//
//	/*
//	** pX is an expression of the form:  (vector) IN (SELECT ...)
//	** In other words, it is a vector IN operator with a SELECT clause on the
//	** RHS.  But not all terms in the vector are indexable and the terms might
//	** not be in the correct order for indexing.
//	**
//	** This routine makes a copy of the input pX expression and then adjusts
//	** the vector on the LHS with corresponding changes to the SELECT so that
//	** the vector contains only index terms and those terms are in the correct
//	** order.  The modified IN expression is returned.  The caller is responsible
//	** for deleting the returned expression.
//	**
//	** Example:
//	**
//	**    CREATE TABLE t1(a,b,c,d,e,f);
//	**    CREATE INDEX t1x1 ON t1(e,c);
//	**    SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
//	**                           \_______________________________________/
//	**                                     The pX expression
//	**
//	** Since only columns e and c can be used with the index, in that order,
//	** the modified IN expression that is returned will be:
//	**
//	**        (e,c) IN (SELECT z,x FROM t2)
//	**
//	** The reduced pX is different from the original (obviously) and thus is
//	** only used for indexing, to improve performance.  The original unaltered
//	** IN expression must also be run on each output row for correctness.
//	*/
func _removeUnindexableInClauseTerms(tls *libc.TLS, pParse uintptr, iEq int32, pLoop uintptr, pX uintptr) (r uintptr) {
	var db, p, pLhs, pNew, pOrigLhs, pOrigRhs, pRhs, pSelect, v4 uintptr
	var i, iField, v3 int32
	_, _, _, _, _, _, _, _, _, _, _, _ = db, i, iField, p, pLhs, pNew, pOrigLhs, pOrigRhs, pRhs, pSelect, v3, v4
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pNew = _sqlite3ExprDup(tls, db, pX, 0)
	if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
		pSelect = *(*uintptr)(unsafe.Pointer(pNew + 20))
		for {
			if !(pSelect != 0) {
				break
			} /* Original unmodified RHS */
			pOrigLhs = uintptr(0) /* Original unmodified LHS */
			pRhs = uintptr(0)     /* New RHS after modifications */
			pLhs = uintptr(0)     /* Loop counter */
			pOrigRhs = (*TSelect)(unsafe.Pointer(pSelect)).FpEList
			if pSelect == *(*uintptr)(unsafe.Pointer(pNew + 20)) {
				pOrigLhs = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pNew)).FpLeft + 20))
			}
			i = iEq
			for {
				if !(i < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm)) {
					break
				}
				if (*TWhereTerm)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX {
					iField = (*(*struct {
						FleftColumn int32
						FiField     int32
					})(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)) + 24))).FiField - int32(1)
					if (*(*TExprList_item)(unsafe.Pointer(pOrigRhs + 8 + uintptr(iField)*20))).FpExpr == uintptr(0) {
						goto _2 /* Duplicate PK column */
					}
					pRhs = _sqlite3ExprListAppend(tls, pParse, pRhs, (*(*TExprList_item)(unsafe.Pointer(pOrigRhs + 8 + uintptr(iField)*20))).FpExpr)
					(*(*TExprList_item)(unsafe.Pointer(pOrigRhs + 8 + uintptr(iField)*20))).FpExpr = uintptr(0)
					if pRhs != 0 {
						*(*Tu16)(unsafe.Pointer(pRhs + 8 + uintptr((*TExprList)(unsafe.Pointer(pRhs)).FnExpr-int32(1))*20 + 16)) = uint16(iField + int32(1))
					}
					if pOrigLhs != 0 {
						pLhs = _sqlite3ExprListAppend(tls, pParse, pLhs, (*(*TExprList_item)(unsafe.Pointer(pOrigLhs + 8 + uintptr(iField)*20))).FpExpr)
						(*(*TExprList_item)(unsafe.Pointer(pOrigLhs + 8 + uintptr(iField)*20))).FpExpr = uintptr(0)
					}
				}
				goto _2
			_2:
				;
				i = i + 1
			}
			_sqlite3ExprListDelete(tls, db, pOrigRhs)
			if pOrigLhs != 0 {
				_sqlite3ExprListDelete(tls, db, pOrigLhs)
				*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pNew)).FpLeft + 20)) = pLhs
			}
			(*TSelect)(unsafe.Pointer(pSelect)).FpEList = pRhs
			v4 = pParse + 100
			*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
			v3 = *(*int32)(unsafe.Pointer(v4))
			(*TSelect)(unsafe.Pointer(pSelect)).FselId = uint32(v3) /* Req'd for SubrtnSig validity */
			if pLhs != 0 && (*TExprList)(unsafe.Pointer(pLhs)).FnExpr == int32(1) {
				/* Take care here not to generate a TK_VECTOR containing only a
				 ** single value. Since the parser never creates such a vector, some
				 ** of the subroutines do not handle this case.  */
				p = (*(*TExprList_item)(unsafe.Pointer(pLhs + 8))).FpExpr
				(*(*TExprList_item)(unsafe.Pointer(pLhs + 8))).FpExpr = uintptr(0)
				_sqlite3ExprDelete(tls, db, (*TExpr)(unsafe.Pointer(pNew)).FpLeft)
				(*TExpr)(unsafe.Pointer(pNew)).FpLeft = p
			}
			/* If either the ORDER BY clause or the GROUP BY clause contains
			 ** references to result-set columns, those references might now be
			 ** obsolete.  So fix them up.
			 */
			if pRhs != 0 {
				_adjustOrderByCol(tls, (*TSelect)(unsafe.Pointer(pSelect)).FpOrderBy, pRhs)
				_adjustOrderByCol(tls, (*TSelect)(unsafe.Pointer(pSelect)).FpGroupBy, pRhs)
				i = 0
				for {
					if !(i < (*TExprList)(unsafe.Pointer(pRhs)).FnExpr) {
						break
					}
					*(*Tu16)(unsafe.Pointer(pRhs + 8 + uintptr(i)*20 + 16)) = uint16(0)
					goto _5
				_5:
					;
					i = i + 1
				}
			}
			goto _1
		_1:
			;
			pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior
		}
	}
	return pNew
}

// C documentation
//
//	/*
//	** This is a Walker expression callback.
//	**
//	** For every TK_COLUMN node in the expression tree, search to see
//	** if the column being references is the column being renamed by an
//	** ALTER TABLE statement.  If it is, then attach its associated
//	** RenameToken object to the list of RenameToken objects being
//	** constructed in RenameCtx object at pWalker->u.pRename.
//	*/
func _renameColumnExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var p uintptr
	_ = p
	p = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_TRIGGER) && int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) == (*TRenameCtx)(unsafe.Pointer(p)).FiCol && (*TParse)(unsafe.Pointer((*TWalker)(unsafe.Pointer(pWalker)).FpParse)).FpTriggerTab == (*TRenameCtx)(unsafe.Pointer(p)).FpTab {
		_renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p, pExpr)
	} else {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) == (*TRenameCtx)(unsafe.Pointer(p)).FiCol && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) && (*TRenameCtx)(unsafe.Pointer(p)).FpTab == (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab {
			_renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p, pExpr)
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** SQL function:
//	**
//	**     sqlite_rename_column(SQL,TYPE,OBJ,DB,TABLE,COL,NEWNAME,QUOTE,TEMP)
//	**
//	**   0. zSql:     SQL statement to rewrite
//	**   1. type:     Type of object ("table", "view" etc.)
//	**   2. object:   Name of object
//	**   3. Database: Database name (e.g. "main")
//	**   4. Table:    Table name
//	**   5. iCol:     Index of column to rename
//	**   6. zNew:     New column name
//	**   7. bQuote:   Non-zero if the new column name should be quoted.
//	**   8. bTemp:    True if zSql comes from temp schema
//	**
//	** Do a column rename operation on the CREATE statement given in zSql.
//	** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
//	** into zNew.  The name should be quoted if bQuote is true.
//	**
//	** This function is used internally by the ALTER TABLE RENAME COLUMN command.
//	** It is only accessible to SQL created using sqlite3NestedParse().  It is
//	** not reachable from ordinary SQL passed into sqlite3_prepare() unless the
//	** SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test setting is enabled.
//	*/
func _renameColumnFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) {
	bp := tls.Alloc(336)
	defer tls.Free(336)
	var bFKOnly, bQuote, bTemp, i, iCol, rc, v1 int32
	var db, pExpr, pFKey, pIdx, pSelect, pStep, pTab, pTarget, pUpsertSet, zDb, zNew, zOld, zSql, zTable uintptr
	var xAuth Tsqlite3_xauth
	var _ /* sCtx at bp+0 */ TRenameCtx
	var _ /* sParse at bp+20 */ TParse
	var _ /* sWalker at bp+304 */ TWalker
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bFKOnly, bQuote, bTemp, db, i, iCol, pExpr, pFKey, pIdx, pSelect, pStep, pTab, pTarget, pUpsertSet, rc, xAuth, zDb, zNew, zOld, zSql, zTable, v1
	db = Xsqlite3_context_db_handle(tls, context)
	zSql = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	zDb = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 3*4)))
	zTable = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 4*4)))
	iCol = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 5*4)))
	zNew = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 6*4)))
	bQuote = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 7*4)))
	bTemp = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 8*4)))
	xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth
	_ = NotUsed
	if zSql == uintptr(0) {
		return
	}
	if zTable == uintptr(0) {
		return
	}
	if zNew == uintptr(0) {
		return
	}
	if iCol < 0 {
		return
	}
	_sqlite3BtreeEnterAll(tls, db)
	pTab = _sqlite3FindTable(tls, db, zTable, zDb)
	if pTab == uintptr(0) || iCol >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) {
		_sqlite3BtreeLeaveAll(tls, db)
		return
	}
	zOld = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName
	libc.Xmemset(tls, bp, 0, uint32(20))
	if iCol == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
		v1 = -int32(1)
	} else {
		v1 = iCol
	}
	(**(**TRenameCtx)(__ccgo_up(bp))).FiCol = v1
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0)
	rc = _renameParseSql(tls, bp+20, zDb, db, zSql, bTemp)
	/* Find tokens that need to be replaced. */
	libc.Xmemset(tls, bp+304, 0, uint32(28))
	(**(**TWalker)(__ccgo_up(bp + 304))).FpParse = bp + 20
	(**(**TWalker)(__ccgo_up(bp + 304))).FxExprCallback = __ccgo_fp(_renameColumnExprCb)
	(**(**TWalker)(__ccgo_up(bp + 304))).FxSelectCallback = __ccgo_fp(_renameColumnSelectCb)
	*(*uintptr)(unsafe.Pointer(bp + 304 + 24)) = bp
	(**(**TRenameCtx)(__ccgo_up(bp))).FpTab = pTab
	if rc != SQLITE_OK {
		goto renameColumnFunc_done
	}
	if (**(**TParse)(__ccgo_up(bp + 20))).FpNewTable != 0 {
		if int32((*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).FeTabType) == int32(TABTYP_VIEW) {
			pSelect = (*(*struct {
				FpSelect uintptr
			})(unsafe.Pointer(&(*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).Fu))).FpSelect
			**(**Tu32)(__ccgo_up(pSelect + 4)) &= ^libc.Uint32FromInt32(SF_View)
			(**(**TParse)(__ccgo_up(bp + 20))).Frc = SQLITE_OK
			_sqlite3SelectPrep(tls, bp+20, pSelect, uintptr(0))
			if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				v1 = int32(SQLITE_NOMEM)
			} else {
				v1 = (**(**TParse)(__ccgo_up(bp + 20))).Frc
			}
			rc = v1
			if rc == SQLITE_OK {
				_sqlite3WalkSelect(tls, bp+304, pSelect)
			}
			if rc != SQLITE_OK {
				goto renameColumnFunc_done
			}
		} else {
			if int32((*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).FeTabType) == TABTYP_NORM {
				/* A regular table */
				bFKOnly = Xsqlite3_stricmp(tls, zTable, (*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).FzName)
				(**(**TRenameCtx)(__ccgo_up(bp))).FpTab = (**(**TParse)(__ccgo_up(bp + 20))).FpNewTable
				if bFKOnly == 0 {
					if iCol < int32((*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).FnCol) {
						_renameTokenFind(tls, bp+20, bp, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).FaCol + uintptr(iCol)*12))).FzCnName)
					}
					if (**(**TRenameCtx)(__ccgo_up(bp))).FiCol < 0 {
						_renameTokenFind(tls, bp+20, bp, (**(**TParse)(__ccgo_up(bp + 20))).FpNewTable+32)
					}
					_sqlite3WalkExprList(tls, bp+304, (*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).FpCheck)
					pIdx = (*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).FpIndex
					for {
						if !(pIdx != 0) {
							break
						}
						_sqlite3WalkExprList(tls, bp+304, (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr)
						goto _3
					_3:
						;
						pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
					}
					pIdx = (**(**TParse)(__ccgo_up(bp + 20))).FpNewIndex
					for {
						if !(pIdx != 0) {
							break
						}
						_sqlite3WalkExprList(tls, bp+304, (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr)
						goto _4
					_4:
						;
						pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
					}
					i = 0
					for {
						if !(i < int32((*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).FnCol)) {
							break
						}
						pExpr = _sqlite3ColumnExpr(tls, (**(**TParse)(__ccgo_up(bp + 20))).FpNewTable, (*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable)).FaCol+uintptr(i)*12)
						_sqlite3WalkExpr(tls, bp+304, pExpr)
						goto _5
					_5:
						;
						i = i + 1
					}
				}
				pFKey = (*(*struct {
					FaddColOffset int32
					FpFKey        uintptr
					FpDfltList    uintptr
				})(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTable + 44))).FpFKey
				for {
					if !(pFKey != 0) {
						break
					}
					i = 0
					for {
						if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) {
							break
						}
						if bFKOnly == 0 && (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FiFrom == iCol {
							_renameTokenFind(tls, bp+20, bp, pFKey+36+uintptr(i)*8)
						}
						if 0 == Xsqlite3_stricmp(tls, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zTable) && 0 == Xsqlite3_stricmp(tls, (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FzCol, zOld) {
							_renameTokenFind(tls, bp+20, bp, (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FzCol)
						}
						goto _7
					_7:
						;
						i = i + 1
					}
					goto _6
				_6:
					;
					pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom
				}
			}
		}
	} else {
		if (**(**TParse)(__ccgo_up(bp + 20))).FpNewIndex != 0 {
			_sqlite3WalkExprList(tls, bp+304, (*TIndex)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewIndex)).FaColExpr)
			_sqlite3WalkExpr(tls, bp+304, (*TIndex)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewIndex)).FpPartIdxWhere)
		} else {
			rc = _renameResolveTrigger(tls, bp+20)
			if rc != SQLITE_OK {
				goto renameColumnFunc_done
			}
			pStep = (*TTrigger)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTrigger)).Fstep_list
			for {
				if !(pStep != 0) {
					break
				}
				if (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc != 0 {
					pTarget = _sqlite3LocateTableItem(tls, bp+20, uint32(0), (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc+8)
					if pTarget == pTab {
						if (*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert != 0 {
							pUpsertSet = (*TUpsert)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpUpsert)).FpUpsertSet
							_renameColumnElistNames(tls, bp+20, bp, pUpsertSet, zOld)
						}
						_renameColumnIdlistNames(tls, bp+20, bp, (*TTriggerStep)(unsafe.Pointer(pStep)).FpIdList, zOld)
						_renameColumnElistNames(tls, bp+20, bp, (*TTriggerStep)(unsafe.Pointer(pStep)).FpExprList, zOld)
					}
				}
				goto _8
			_8:
				;
				pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext
			}
			/* Find tokens to edit in UPDATE OF clause */
			if (**(**TParse)(__ccgo_up(bp + 20))).FpTriggerTab == pTab {
				_renameColumnIdlistNames(tls, bp+20, bp, (*TTrigger)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp + 20))).FpNewTrigger)).FpColumns, zOld)
			}
			/* Find tokens to edit in various expressions and selects */
			_renameWalkTrigger(tls, bp+304, (**(**TParse)(__ccgo_up(bp + 20))).FpNewTrigger)
		}
	}
	rc = _renameEditSql(tls, context, bp, zSql, zNew, bQuote)
	goto renameColumnFunc_done
renameColumnFunc_done:
	;
	if rc != SQLITE_OK {
		if rc == int32(SQLITE_ERROR) && _sqlite3WritableSchema(tls, db) != 0 {
			Xsqlite3_result_value(tls, context, **(**uintptr)(__ccgo_up(argv)))
		} else {
			if (**(**TParse)(__ccgo_up(bp + 20))).FzErrMsg != 0 {
				_renameColumnParseError(tls, context, __ccgo_ts+1711, **(**uintptr)(__ccgo_up(argv + 1*4)), **(**uintptr)(__ccgo_up(argv + 2*4)), bp+20)
			} else {
				Xsqlite3_result_error_code(tls, context, rc)
			}
		}
	}
	_renameParseCleanup(tls, bp+20)
	_renameTokenFree(tls, db, (**(**TRenameCtx)(__ccgo_up(bp))).FpList)
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth
	_sqlite3BtreeLeaveAll(tls, db)
}

// C documentation
//
//	/*
//	** This function edits SQL statement zSql, replacing each token identified
//	** by the linked list pRename with the text of zNew. If argument bQuote is
//	** true, then zNew is always quoted first. If no error occurs, the result
//	** is loaded into context object pCtx as the result.
//	**
//	** Or, if an error occurs (i.e. an OOM condition), an error is left in
//	** pCtx and an SQLite error code returned.
//	*/
func _renameEditSql(tls *libc.TLS, pCtx uintptr, pRename uintptr, zSql uintptr, zNew uintptr, bQuote int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pBest, zBuf1, zBuf2, zOut, zQuot, zReplace, v1 uintptr
	var iOff, rc int32
	var nNew, nOut, nQuot, nReplace, nSql Ti64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, iOff, nNew, nOut, nQuot, nReplace, nSql, pBest, rc, zBuf1, zBuf2, zOut, zQuot, zReplace, v1
	nNew = int64(_sqlite3Strlen30(tls, zNew))
	nSql = int64(_sqlite3Strlen30(tls, zSql))
	db = Xsqlite3_context_db_handle(tls, pCtx)
	rc = SQLITE_OK
	zQuot = uintptr(0)
	nQuot = 0
	zBuf1 = uintptr(0)
	zBuf2 = uintptr(0)
	if zNew != 0 {
		/* Set zQuot to point to a buffer containing a quoted copy of the
		 ** identifier zNew. If the corresponding identifier in the original
		 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
		 ** point to zQuot so that all substitutions are made using the
		 ** quoted version of the new column name.  */
		zQuot = _sqlite3MPrintf(tls, db, __ccgo_ts+12791, libc.VaList(bp+8, zNew))
		if zQuot == uintptr(0) {
			return int32(SQLITE_NOMEM)
		} else {
			nQuot = int64(_sqlite3Strlen30(tls, zQuot) - int32(1))
		}
		zOut = _sqlite3DbMallocZero(tls, db, uint64(nSql)+uint64((*TRenameCtx)(unsafe.Pointer(pRename)).FnList)*uint64(nQuot)+uint64(1))
	} else {
		zOut = _sqlite3DbMallocZero(tls, db, (uint64(2)*uint64(nSql)+uint64(1))*uint64(3))
		if zOut != 0 {
			zBuf1 = zOut + uintptr(nSql*int64(2)+int64(1))
			zBuf2 = zOut + uintptr(nSql*int64(4)+int64(2))
		}
	}
	/* At this point pRename->pList contains a list of RenameToken objects
	 ** corresponding to all tokens in the input SQL that must be replaced
	 ** with the new column name, or with single-quoted versions of themselves.
	 ** All that remains is to construct and return the edited SQL string. */
	if zOut != 0 {
		nOut = nSql
		libc.Xmemcpy(tls, zOut, zSql, uint32(nSql))
		for (*TRenameCtx)(unsafe.Pointer(pRename)).FpList != 0 {
			pBest = _renameColumnTokenNext(tls, pRename)
			if zNew != 0 {
				if bQuote == 0 && _sqlite3IsIdChar(tls, **(**Tu8)(__ccgo_up((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz))) != 0 {
					nReplace = nNew
					zReplace = zNew
				} else {
					nReplace = nQuot
					zReplace = zQuot
					if int32(**(**int8)(__ccgo_up((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz + uintptr((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)))) == int32('"') {
						nReplace = nReplace + 1
					}
				}
			} else {
				/* Dequote the double-quoted token. Then requote it again, this time
				 ** using single quotes. If the character immediately following the
				 ** original token within the input SQL was a single quote ('), then
				 ** add another space after the new, single-quoted version of the
				 ** token. This is so that (SELECT "string"'alias') maps to
				 ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias').  */
				libc.Xmemcpy(tls, zBuf1, (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz, (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)
				**(**int8)(__ccgo_up(zBuf1 + uintptr((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn))) = 0
				_sqlite3Dequote(tls, zBuf1)
				if int32(**(**int8)(__ccgo_up((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz + uintptr((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)))) == int32('\'') {
					v1 = __ccgo_ts + 12758
				} else {
					v1 = __ccgo_ts + 1711
				}
				Xsqlite3_snprintf(tls, int32(nSql*libc.Int64FromInt32(2)), zBuf2, __ccgo_ts+12797, libc.VaList(bp+8, zBuf1, v1))
				zReplace = zBuf2
				nReplace = int64(_sqlite3Strlen30(tls, zReplace))
			}
			iOff = int32((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz) - int32(zSql)
			if int64((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn) != nReplace {
				libc.Xmemmove(tls, zOut+uintptr(int64(iOff)+nReplace), zOut+uintptr(uint32(iOff)+(*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn), uint32(nOut-int64(uint32(iOff)+(*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)))
				nOut = nOut + (nReplace - int64((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn))
				**(**int8)(__ccgo_up(zOut + uintptr(nOut))) = int8('\000')
			}
			libc.Xmemcpy(tls, zOut+uintptr(iOff), zReplace, uint32(nReplace))
			_sqlite3DbFree(tls, db, pBest)
		}
		Xsqlite3_result_text(tls, pCtx, zOut, -int32(1), uintptr(-libc.Int32FromInt32(1)))
		_sqlite3DbFree(tls, db, zOut)
	} else {
		rc = int32(SQLITE_NOMEM)
	}
	Xsqlite3_free(tls, zQuot)
	return rc
}

// C documentation
//
//	/*
//	** Parse the SQL statement zSql using Parse object (*p). The Parse object
//	** is initialized by this function before it is used.
//	*/
func _renameParseSql(tls *libc.TLS, p uintptr, zDb uintptr, db uintptr, zSql uintptr, bTemp int32) (r int32) {
	var flags Tu64
	var iDb, rc int32
	_, _, _ = flags, iDb, rc
	_sqlite3ParseObjectInit(tls, p, db)
	if zSql == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	if Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+12783, int32(7)) != 0 {
		return _sqlite3CorruptError(tls, int32(121717))
	}
	if bTemp != 0 {
		(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(1)
	} else {
		iDb = _sqlite3FindDbName(tls, db, zDb)
		(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(iDb)
	}
	(*TParse)(unsafe.Pointer(p)).FeParseMode = uint8(PARSE_MODE_RENAME)
	(*TParse)(unsafe.Pointer(p)).Fdb = db
	(*TParse)(unsafe.Pointer(p)).FnQueryLoop = int16(1)
	flags = (*Tsqlite3)(unsafe.Pointer(db)).Fflags
	**(**Tu64)(__ccgo_up(db + 32)) |= uint64(libc.Int32FromInt32(0x00040)) << libc.Int32FromInt32(32)
	rc = _sqlite3RunParser(tls, p, zSql)
	(*Tsqlite3)(unsafe.Pointer(db)).Fflags = flags
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		rc = int32(SQLITE_NOMEM)
	}
	if rc == SQLITE_OK && ((*TParse)(unsafe.Pointer(p)).FpNewTable == uintptr(0) && (*TParse)(unsafe.Pointer(p)).FpNewIndex == uintptr(0) && (*TParse)(unsafe.Pointer(p)).FpNewTrigger == uintptr(0)) {
		rc = _sqlite3CorruptError(tls, int32(121738))
	}
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(0)
	return rc
}

func _renameQuotefixExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_STRING) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_DblQuoted) != 0 {
		_renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, *(*uintptr)(unsafe.Pointer(pWalker + 24)), pExpr)
	}
	return WRC_Continue
}

// C documentation
//
//	/* SQL function: sqlite_rename_quotefix(DB,SQL)
//	**
//	** Rewrite the DDL statement "SQL" so that any string literals that use
//	** double-quotes use single quotes instead.
//	**
//	** Two arguments must be passed:
//	**
//	**   0: Database name ("main", "temp" etc.).
//	**   1: SQL statement to edit.
//	**
//	** The returned value is the modified SQL statement. For example, given
//	** the database schema:
//	**
//	**   CREATE TABLE t1(a, b, c);
//	**
//	**   SELECT sqlite_rename_quotefix('main',
//	**       'CREATE VIEW v1 AS SELECT "a", "string" FROM t1'
//	**   );
//	**
//	** returns the string:
//	**
//	**   CREATE VIEW v1 AS SELECT "a", 'string' FROM t1
//	**
//	** If there is a error in the input SQL, then raise an error, except
//	** if PRAGMA writable_schema=ON, then just return the input string
//	** unmodified following an error.
//	*/
func _renameQuotefixFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) {
	bp := tls.Alloc(336)
	defer tls.Free(336)
	var db, pSelect, zDb, zInput uintptr
	var i, rc, v1 int32
	var xAuth Tsqlite3_xauth
	var _ /* sCtx at bp+284 */ TRenameCtx
	var _ /* sParse at bp+0 */ TParse
	var _ /* sWalker at bp+304 */ TWalker
	_, _, _, _, _, _, _, _ = db, i, pSelect, rc, xAuth, zDb, zInput, v1
	db = Xsqlite3_context_db_handle(tls, context)
	zDb = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	zInput = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0)
	_sqlite3BtreeEnterAll(tls, db)
	_ = NotUsed
	if zDb != 0 && zInput != 0 {
		rc = _renameParseSql(tls, bp, zDb, db, zInput, 0)
		if rc == SQLITE_OK {
			/* Walker to find tokens that need to be replaced. */
			libc.Xmemset(tls, bp+284, 0, uint32(20))
			libc.Xmemset(tls, bp+304, 0, uint32(28))
			(**(**TWalker)(__ccgo_up(bp + 304))).FpParse = bp
			(**(**TWalker)(__ccgo_up(bp + 304))).FxExprCallback = __ccgo_fp(_renameQuotefixExprCb)
			(**(**TWalker)(__ccgo_up(bp + 304))).FxSelectCallback = __ccgo_fp(_renameColumnSelectCb)
			*(*uintptr)(unsafe.Pointer(bp + 304 + 24)) = bp + 284
			if (**(**TParse)(__ccgo_up(bp))).FpNewTable != 0 {
				if int32((*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTable)).FeTabType) == int32(TABTYP_VIEW) {
					pSelect = (*(*struct {
						FpSelect uintptr
					})(unsafe.Pointer(&(*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTable)).Fu))).FpSelect
					**(**Tu32)(__ccgo_up(pSelect + 4)) &= ^libc.Uint32FromInt32(SF_View)
					(**(**TParse)(__ccgo_up(bp))).Frc = SQLITE_OK
					_sqlite3SelectPrep(tls, bp, pSelect, uintptr(0))
					if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
						v1 = int32(SQLITE_NOMEM)
					} else {
						v1 = (**(**TParse)(__ccgo_up(bp))).Frc
					}
					rc = v1
					if rc == SQLITE_OK {
						_sqlite3WalkSelect(tls, bp+304, pSelect)
					}
				} else {
					_sqlite3WalkExprList(tls, bp+304, (*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTable)).FpCheck)
					i = 0
					for {
						if !(i < int32((*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTable)).FnCol)) {
							break
						}
						_sqlite3WalkExpr(tls, bp+304, _sqlite3ColumnExpr(tls, (**(**TParse)(__ccgo_up(bp))).FpNewTable, (*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTable)).FaCol+uintptr(i)*12))
						goto _2
					_2:
						;
						i = i + 1
					}
				}
			} else {
				if (**(**TParse)(__ccgo_up(bp))).FpNewIndex != 0 {
					_sqlite3WalkExprList(tls, bp+304, (*TIndex)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewIndex)).FaColExpr)
					_sqlite3WalkExpr(tls, bp+304, (*TIndex)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewIndex)).FpPartIdxWhere)
				} else {
					rc = _renameResolveTrigger(tls, bp)
					if rc == SQLITE_OK {
						_renameWalkTrigger(tls, bp+304, (**(**TParse)(__ccgo_up(bp))).FpNewTrigger)
					}
				}
			}
			if rc == SQLITE_OK {
				rc = _renameEditSql(tls, context, bp+284, zInput, uintptr(0), 0)
			}
			_renameTokenFree(tls, db, (**(**TRenameCtx)(__ccgo_up(bp + 284))).FpList)
		}
		if rc != SQLITE_OK {
			if _sqlite3WritableSchema(tls, db) != 0 && rc == int32(SQLITE_ERROR) {
				Xsqlite3_result_value(tls, context, **(**uintptr)(__ccgo_up(argv + 1*4)))
			} else {
				Xsqlite3_result_error_code(tls, context, rc)
			}
		}
		_renameParseCleanup(tls, bp)
	}
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth
	_sqlite3BtreeLeaveAll(tls, db)
}

// C documentation
//
//	/*
//	** Set all pEList->a[].fg.eEName fields in the expression-list to val.
//	*/
func _renameSetENames(tls *libc.TLS, pEList uintptr, val int32) {
	var i int32
	_ = i
	if pEList != 0 {
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
				break
			}
			libc.SetBitFieldPtr16Uint32(pEList+8+uintptr(i)*20+8+4, uint32(val&libc.Int32FromInt32(0x3)), 0, 0x3)
			goto _1
		_1:
			;
			i = i + 1
		}
	}
}

// C documentation
//
//	/*
//	** Walker expression callback used by "RENAME TABLE".
//	*/
func _renameTableExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var p uintptr
	_ = p
	p = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) && (*TRenameCtx)(unsafe.Pointer(p)).FpTab == (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab {
		_renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p, pExpr+44)
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** This C function implements an SQL user function that is used by SQL code
//	** generated by the ALTER TABLE ... RENAME command to modify the definition
//	** of any foreign key constraints that use the table being renamed as the
//	** parent table. It is passed three arguments:
//	**
//	**   0: The database containing the table being renamed.
//	**   1. type:     Type of object ("table", "view" etc.)
//	**   2. object:   Name of object
//	**   3: The complete text of the schema statement being modified,
//	**   4: The old name of the table being renamed, and
//	**   5: The new name of the table being renamed.
//	**   6: True if the schema statement comes from the temp db.
//	**
//	** It returns the new schema statement. For example:
//	**
//	** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
//	**       -> 'CREATE TABLE t1(a REFERENCES t3)'
//	*/
func _renameTableFunc(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) {
	bp := tls.Alloc(368)
	defer tls.Free(368)
	var bQuote, bTemp, i, isLegacy, rc int32
	var db, pFKey, pItem, pSelect, pStep, pTab, pTrigger, zDb, zInput, zNew, zOld uintptr
	var xAuth Tsqlite3_xauth
	var _ /* sCtx at bp+284 */ TRenameCtx
	var _ /* sNC at bp+332 */ TNameContext
	var _ /* sParse at bp+0 */ TParse
	var _ /* sWalker at bp+304 */ TWalker
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bQuote, bTemp, db, i, isLegacy, pFKey, pItem, pSelect, pStep, pTab, pTrigger, rc, xAuth, zDb, zInput, zNew, zOld
	db = Xsqlite3_context_db_handle(tls, context)
	zDb = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	zInput = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 3*4)))
	zOld = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 4*4)))
	zNew = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 5*4)))
	bTemp = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 6*4)))
	_ = NotUsed
	if zInput != 0 && zOld != 0 && zNew != 0 {
		bQuote = int32(1)
		xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth
		(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0)
		_sqlite3BtreeEnterAll(tls, db)
		libc.Xmemset(tls, bp+284, 0, uint32(20))
		(**(**TRenameCtx)(__ccgo_up(bp + 284))).FpTab = _sqlite3FindTable(tls, db, zOld, zDb)
		libc.Xmemset(tls, bp+304, 0, uint32(28))
		(**(**TWalker)(__ccgo_up(bp + 304))).FpParse = bp
		(**(**TWalker)(__ccgo_up(bp + 304))).FxExprCallback = __ccgo_fp(_renameTableExprCb)
		(**(**TWalker)(__ccgo_up(bp + 304))).FxSelectCallback = __ccgo_fp(_renameTableSelectCb)
		*(*uintptr)(unsafe.Pointer(bp + 304 + 24)) = bp + 284
		rc = _renameParseSql(tls, bp, zDb, db, zInput, bTemp)
		if rc == SQLITE_OK {
			isLegacy = int32((*Tsqlite3)(unsafe.Pointer(db)).Fflags & libc.Uint64FromInt32(SQLITE_LegacyAlter))
			if (**(**TParse)(__ccgo_up(bp))).FpNewTable != 0 {
				pTab = (**(**TParse)(__ccgo_up(bp))).FpNewTable
				if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
					if isLegacy == 0 {
						pSelect = (*(*struct {
							FpSelect uintptr
						})(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTab)).Fu))).FpSelect
						libc.Xmemset(tls, bp+332, 0, uint32(36))
						(**(**TNameContext)(__ccgo_up(bp + 332))).FpParse = bp
						**(**Tu32)(__ccgo_up(pSelect + 4)) &= ^libc.Uint32FromInt32(SF_View)
						_sqlite3SelectPrep(tls, bp, (*(*struct {
							FpSelect uintptr
						})(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTab)).Fu))).FpSelect, bp+332)
						if (**(**TParse)(__ccgo_up(bp))).FnErr != 0 {
							rc = (**(**TParse)(__ccgo_up(bp))).Frc
						} else {
							_sqlite3WalkSelect(tls, bp+304, (*(*struct {
								FpSelect uintptr
							})(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTab)).Fu))).FpSelect)
						}
					}
				} else {
					/* Modify any FK definitions to point to the new table. */
					if (isLegacy == 0 || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0) && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
						pFKey = (*(*struct {
							FaddColOffset int32
							FpFKey        uintptr
							FpDfltList    uintptr
						})(unsafe.Pointer(pTab + 44))).FpFKey
						for {
							if !(pFKey != 0) {
								break
							}
							if Xsqlite3_stricmp(tls, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zOld) == 0 {
								_renameTokenFind(tls, bp, bp+284, (*TFKey)(unsafe.Pointer(pFKey)).FzTo)
							}
							goto _1
						_1:
							;
							pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom
						}
					}
					/* If this is the table being altered, fix any table refs in CHECK
					 ** expressions. Also update the name that appears right after the
					 ** "CREATE [VIRTUAL] TABLE" bit. */
					if Xsqlite3_stricmp(tls, zOld, (*TTable)(unsafe.Pointer(pTab)).FzName) == 0 {
						(**(**TRenameCtx)(__ccgo_up(bp + 284))).FpTab = pTab
						if isLegacy == 0 {
							_sqlite3WalkExprList(tls, bp+304, (*TTable)(unsafe.Pointer(pTab)).FpCheck)
						}
						_renameTokenFind(tls, bp, bp+284, (*TTable)(unsafe.Pointer(pTab)).FzName)
					}
				}
			} else {
				if (**(**TParse)(__ccgo_up(bp))).FpNewIndex != 0 {
					_renameTokenFind(tls, bp, bp+284, (*TIndex)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewIndex)).FzName)
					if isLegacy == 0 {
						_sqlite3WalkExpr(tls, bp+304, (*TIndex)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewIndex)).FpPartIdxWhere)
					}
				} else {
					pTrigger = (**(**TParse)(__ccgo_up(bp))).FpNewTrigger
					if 0 == Xsqlite3_stricmp(tls, (*TTrigger)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTrigger)).Ftable, zOld) && (*TTable)(unsafe.Pointer((**(**TRenameCtx)(__ccgo_up(bp + 284))).FpTab)).FpSchema == (*TTrigger)(unsafe.Pointer(pTrigger)).FpTabSchema {
						_renameTokenFind(tls, bp, bp+284, (*TTrigger)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTrigger)).Ftable)
					}
					if isLegacy == 0 {
						rc = _renameResolveTrigger(tls, bp)
						if rc == SQLITE_OK {
							_renameWalkTrigger(tls, bp+304, pTrigger)
							pStep = (*TTrigger)(unsafe.Pointer(pTrigger)).Fstep_list
							for {
								if !(pStep != 0) {
									break
								}
								if (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc != 0 {
									i = 0
									for {
										if !(i < (*TSrcList)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc)).FnSrc) {
											break
										}
										pItem = (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc + 8 + uintptr(i)*48
										if 0 == Xsqlite3_stricmp(tls, (*TSrcItem)(unsafe.Pointer(pItem)).FzName, zOld) {
											_renameTokenFind(tls, bp, bp+284, (*TSrcItem)(unsafe.Pointer(pItem)).FzName)
										}
										goto _3
									_3:
										;
										i = i + 1
									}
								}
								goto _2
							_2:
								;
								pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext
							}
						}
					}
				}
			}
		}
		if rc == SQLITE_OK {
			rc = _renameEditSql(tls, context, bp+284, zInput, zNew, bQuote)
		}
		if rc != SQLITE_OK {
			if rc == int32(SQLITE_ERROR) && _sqlite3WritableSchema(tls, db) != 0 {
				Xsqlite3_result_value(tls, context, **(**uintptr)(__ccgo_up(argv + 3*4)))
			} else {
				if (**(**TParse)(__ccgo_up(bp))).FzErrMsg != 0 {
					_renameColumnParseError(tls, context, __ccgo_ts+1711, **(**uintptr)(__ccgo_up(argv + 1*4)), **(**uintptr)(__ccgo_up(argv + 2*4)), bp)
				} else {
					Xsqlite3_result_error_code(tls, context, rc)
				}
			}
		}
		_renameParseCleanup(tls, bp)
		_renameTokenFree(tls, db, (**(**TRenameCtx)(__ccgo_up(bp + 284))).FpList)
		_sqlite3BtreeLeaveAll(tls, db)
		(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth
	}
	return
}

// C documentation
//
//	/*
//	** Walker select callback used by "RENAME TABLE".
//	*/
func _renameTableSelectCb(tls *libc.TLS, pWalker uintptr, pSelect uintptr) (r int32) {
	var i int32
	var p, pItem, pSrc uintptr
	_, _, _, _ = i, p, pItem, pSrc
	p = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	pSrc = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc
	if (*TSelect)(unsafe.Pointer(pSelect)).FselFlags&uint32(libc.Int32FromInt32(SF_View)|libc.Int32FromInt32(SF_CopyCte)) != 0 {
		return int32(WRC_Prune)
	}
	if pSrc == uintptr(0) {
		return int32(WRC_Abort)
	}
	i = 0
	for {
		if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) {
			break
		}
		pItem = pSrc + 8 + uintptr(i)*48
		if (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab == (*TRenameCtx)(unsafe.Pointer(p)).FpTab {
			_renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p, (*TSrcItem)(unsafe.Pointer(pItem)).FzName)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	_renameWalkWith(tls, pWalker, pSelect)
	return WRC_Continue
}

// C documentation
//
//	/* Function:  sqlite_rename_test(DB,SQL,TYPE,NAME,ISTEMP,WHEN,DQS)
//	**
//	** An SQL user function that checks that there are no parse or symbol
//	** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
//	** After an ALTER TABLE .. RENAME operation is performed and the schema
//	** reloaded, this function is called on each SQL statement in the schema
//	** to ensure that it is still usable.
//	**
//	**   0: Database name ("main", "temp" etc.).
//	**   1: SQL statement.
//	**   2: Object type ("view", "table", "trigger" or "index").
//	**   3: Object name.
//	**   4: True if object is from temp schema.
//	**   5: "when" part of error message.
//	**   6: True to disable the DQS quirk when parsing SQL.
//	**
//	** The return value is computed as follows:
//	**
//	**   A. If an error is seen and not in PRAGMA writable_schema=ON mode,
//	**      then raise the error.
//	**   B. Else if a trigger is created and the the table that the trigger is
//	**      attached to is in database zDb, then return 1.
//	**   C. Otherwise return NULL.
//	*/
func _renameTableTest(tls *libc.TLS, context uintptr, NotUsed int32, argv uintptr) {
	bp := tls.Alloc(320)
	defer tls.Free(320)
	var bNoDQS, bTemp, i1, i2, isLegacy, rc int32
	var db, zDb, zInput, zWhen uintptr
	var flags Tu64
	var xAuth Tsqlite3_xauth
	var _ /* sNC at bp+284 */ TNameContext
	var _ /* sParse at bp+0 */ TParse
	_, _, _, _, _, _, _, _, _, _, _, _ = bNoDQS, bTemp, db, flags, i1, i2, isLegacy, rc, xAuth, zDb, zInput, zWhen
	db = Xsqlite3_context_db_handle(tls, context)
	zDb = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	zInput = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	bTemp = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 4*4)))
	isLegacy = int32((*Tsqlite3)(unsafe.Pointer(db)).Fflags & libc.Uint64FromInt32(SQLITE_LegacyAlter))
	zWhen = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 5*4)))
	bNoDQS = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 6*4)))
	xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0)
	_ = NotUsed
	if zDb != 0 && zInput != 0 {
		flags = (*Tsqlite3)(unsafe.Pointer(db)).Fflags
		if bNoDQS != 0 {
			**(**Tu64)(__ccgo_up(db + 32)) &= uint64(^(libc.Int32FromInt32(SQLITE_DqsDML) | libc.Int32FromInt32(SQLITE_DqsDDL)))
		}
		rc = _renameParseSql(tls, bp, zDb, db, zInput, bTemp)
		(*Tsqlite3)(unsafe.Pointer(db)).Fflags = flags
		if rc == SQLITE_OK {
			if isLegacy == 0 && (**(**TParse)(__ccgo_up(bp))).FpNewTable != 0 && int32((*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTable)).FeTabType) == int32(TABTYP_VIEW) {
				libc.Xmemset(tls, bp+284, 0, uint32(36))
				(**(**TNameContext)(__ccgo_up(bp + 284))).FpParse = bp
				_sqlite3SelectPrep(tls, bp, (*(*struct {
					FpSelect uintptr
				})(unsafe.Pointer(&(*TTable)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTable)).Fu))).FpSelect, bp+284)
				if (**(**TParse)(__ccgo_up(bp))).FnErr != 0 {
					rc = (**(**TParse)(__ccgo_up(bp))).Frc
				}
			} else {
				if (**(**TParse)(__ccgo_up(bp))).FpNewTrigger != 0 {
					if isLegacy == 0 {
						rc = _renameResolveTrigger(tls, bp)
					}
					if rc == SQLITE_OK {
						i1 = _sqlite3SchemaToIndex(tls, db, (*TTrigger)(unsafe.Pointer((**(**TParse)(__ccgo_up(bp))).FpNewTrigger)).FpTabSchema)
						i2 = _sqlite3FindDbName(tls, db, zDb)
						if i1 == i2 {
							/* Handle output case B */
							Xsqlite3_result_int(tls, context, int32(1))
						}
					}
				}
			}
		}
		if rc != SQLITE_OK && zWhen != 0 && !(_sqlite3WritableSchema(tls, db) != 0) {
			/* Output case A */
			_renameColumnParseError(tls, context, zWhen, **(**uintptr)(__ccgo_up(argv + 2*4)), **(**uintptr)(__ccgo_up(argv + 3*4)), bp)
		}
		_renameParseCleanup(tls, bp)
	}
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth
}

// C documentation
//
//	/*
//	** Generate code to verify that the schemas of database zDb and, if
//	** bTemp is not true, database "temp", can still be parsed. This is
//	** called at the end of the generation of an ALTER TABLE ... RENAME ...
//	** statement to ensure that the operation has not rendered any schema
//	** objects unusable.
//	*/
func _renameTestSchema(tls *libc.TLS, pParse uintptr, zDb uintptr, bTemp int32, zWhen uintptr, bNoDQS int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 5, 0x20)
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+9888, libc.VaList(bp+8, zDb, zDb, bTemp, zWhen, bNoDQS))
	if bTemp == 0 {
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+10063, libc.VaList(bp+8, zDb, zWhen, bNoDQS))
	}
}

// C documentation
//
//	/*
//	** Walker callback used by sqlite3RenameExprUnmap().
//	*/
func _renameUnmapExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var pParse uintptr
	_ = pParse
	pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse
	_sqlite3RenameTokenRemap(tls, pParse, uintptr(0), pExpr)
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) {
		_sqlite3RenameTokenRemap(tls, pParse, uintptr(0), pExpr+44)
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Walker callback used by sqlite3RenameExprUnmap().
//	*/
func _renameUnmapSelectCb(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) {
	var i int32
	var pList, pParse, pSrc uintptr
	_, _, _, _ = i, pList, pParse, pSrc
	pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return int32(WRC_Abort)
	}
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_View)|libc.Int32FromInt32(SF_CopyCte)) != 0 {
		return int32(WRC_Prune)
	}
	if (*TSelect)(unsafe.Pointer(p)).FpEList != 0 {
		pList = (*TSelect)(unsafe.Pointer(p)).FpEList
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
				break
			}
			if (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FzEName != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME {
				_sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FzEName)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	if (*TSelect)(unsafe.Pointer(p)).FpSrc != 0 { /* Every Select as a SrcList, even if it is empty */
		pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc
		i = 0
		for {
			if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) {
				break
			}
			_sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48))).FzName)
			if int32(*(*uint32)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48 + 12 + 4))&0x800>>11) == 0 {
				_sqlite3WalkExpr(tls, pWalker, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48 + 40)))
			} else {
				_unmapColumnIdlistNames(tls, pParse, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48 + 40)))
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	_renameWalkWith(tls, pWalker, p)
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Expression walker callback used by renumberCursors() to update
//	** Expr objects to match newly assigned cursor numbers.
//	*/
func _renumberCursorsCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var op int32
	_ = op
	op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop)
	if op == int32(TK_COLUMN) || op == int32(TK_IF_NULL_ROW) {
		_renumberCursorDoMapping(tls, pWalker, pExpr+28)
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) {
		_renumberCursorDoMapping(tls, pWalker, pExpr+36)
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** The replace() function.  Three arguments are all strings: call
//	** them A, B, and C. The result is also a string which is derived
//	** from A by replacing every occurrence of B with C.  The match
//	** must be exact.  Collating sequences are not used.
//	*/
func _replaceFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var cntExpand uint32
	var db, zOld, zOut, zPattern, zRep, zStr uintptr
	var i, j, loopLimit, nPattern, nRep, nStr, v2, v3 int32
	var nOut Ti64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = cntExpand, db, i, j, loopLimit, nOut, nPattern, nRep, nStr, zOld, zOut, zPattern, zRep, zStr, v2, v3 /* Number zOut expansions */
	db = Xsqlite3_context_db_handle(tls, context)
	_ = argc
	zStr = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	if zStr == uintptr(0) {
		return
	}
	nStr = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
	/* No encoding change */
	zPattern = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	if zPattern == uintptr(0) {
		return
	}
	if int32(**(**uint8)(__ccgo_up(zPattern))) == 0 {
		Xsqlite3_result_text(tls, context, zStr, nStr, uintptr(-libc.Int32FromInt32(1)))
		return
	}
	nPattern = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	/* No encoding change */
	zRep = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 2*4)))
	if zRep == uintptr(0) {
		return
	}
	nRep = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 2*4)))
	nOut = int64(nStr + int32(1))
	zOut = _contextMalloc(tls, context, nOut)
	if zOut == uintptr(0) {
		return
	}
	loopLimit = nStr - nPattern
	cntExpand = uint32(0)
	v2 = libc.Int32FromInt32(0)
	j = v2
	i = v2
	for {
		if !(i <= loopLimit) {
			break
		}
		if int32(**(**uint8)(__ccgo_up(zStr + uintptr(i)))) != int32(**(**uint8)(__ccgo_up(zPattern))) || libc.Xmemcmp(tls, zStr+uintptr(i), zPattern, uint32(nPattern)) != 0 {
			v3 = j
			j = j + 1
			**(**uint8)(__ccgo_up(zOut + uintptr(v3))) = **(**uint8)(__ccgo_up(zStr + uintptr(i)))
		} else {
			if nRep > nPattern {
				nOut = nOut + int64(nRep-nPattern)
				if nOut-int64(1) > int64(**(**int32)(__ccgo_up(db + 120))) {
					Xsqlite3_result_error_toobig(tls, context)
					Xsqlite3_free(tls, zOut)
					return
				}
				cntExpand = cntExpand + 1
				if cntExpand&(cntExpand-uint32(1)) == uint32(0) {
					zOld = zOut
					zOut = _sqlite3Realloc(tls, zOut, uint64(int64(int32(nOut))+(nOut-int64(nStr)-int64(1))))
					if zOut == uintptr(0) {
						Xsqlite3_result_error_nomem(tls, context)
						Xsqlite3_free(tls, zOld)
						return
					}
				}
			}
			libc.Xmemcpy(tls, zOut+uintptr(j), zRep, uint32(nRep))
			j = j + nRep
			i = i + (nPattern - int32(1))
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	libc.Xmemcpy(tls, zOut+uintptr(j), zStr+uintptr(i), uint32(nStr-i))
	j = j + (nStr - i)
	**(**uint8)(__ccgo_up(zOut + uintptr(j))) = uint8(0)
	Xsqlite3_result_text(tls, context, zOut, j, __ccgo_fp(Xsqlite3_free))
}

// C documentation
//
//	/*
//	** Reset the aggregate accumulator.
//	**
//	** The aggregate accumulator is a set of memory cells that hold
//	** intermediate results while calculating an aggregate.  This
//	** routine generates code that stores NULLs in all of those memory
//	** cells.
//	*/
func _resetAccumulator(tls *libc.TLS, pParse uintptr, pAggInfo uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, nExtra, nReg int32
	var pE, pFunc, pKeyInfo, pKeyInfo1, pOBList, v uintptr
	_, _, _, _, _, _, _, _, _ = i, nExtra, nReg, pE, pFunc, pKeyInfo, pKeyInfo1, pOBList, v
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	nReg = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc + (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn
	if nReg == 0 {
		return
	}
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return
	}
	_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+nReg-int32(1))
	pFunc = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc
	i = libc.Int32FromInt32(0)
	for {
		if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) {
			break
		}
		if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiDistinct >= 0 {
			pE = (*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr
			if *(*uintptr)(unsafe.Pointer(pE + 20)) == uintptr(0) || (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pE + 20)))).FnExpr != int32(1) {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22642, 0)
				(*TAggInfo_func)(unsafe.Pointer(pFunc)).FiDistinct = -int32(1)
			} else {
				pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, *(*uintptr)(unsafe.Pointer(pE + 20)), 0, 0)
				(*TAggInfo_func)(unsafe.Pointer(pFunc)).FiDistAddr = _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiDistinct, 0, 0, pKeyInfo, -int32(9))
				_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+22693, libc.VaList(bp+8, (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFunc)).FzName))
			}
		}
		if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiOBTab >= 0 {
			nExtra = 0
			pOBList = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr)).FpLeft + 20))
			if !((*TAggInfo_func)(unsafe.Pointer(pFunc)).FbOBUnique != 0) {
				nExtra = nExtra + 1 /* One extra column for the OP_Sequence */
			}
			if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FbOBPayload != 0 {
				/* extra columns for the function arguments */
				nExtra = nExtra + (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr + 20)))).FnExpr
			}
			if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FbUseSubtype != 0 {
				nExtra = nExtra + (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr + 20)))).FnExpr
			}
			pKeyInfo1 = _sqlite3KeyInfoFromExprList(tls, pParse, pOBList, 0, nExtra)
			if !((*TAggInfo_func)(unsafe.Pointer(pFunc)).FbOBUnique != 0) && (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
				(*TKeyInfo)(unsafe.Pointer(pKeyInfo1)).FnKeyField = (*TKeyInfo)(unsafe.Pointer(pKeyInfo1)).FnKeyField + 1
			}
			_sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiOBTab, (*TExprList)(unsafe.Pointer(pOBList)).FnExpr+nExtra, 0, pKeyInfo1, -int32(9))
			_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+22726, libc.VaList(bp+8, (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFunc)).FzName))
		}
		goto _1
	_1:
		;
		i = i + 1
		pFunc += 24
	}
}

// C documentation
//
//	/*
//	** Resize an Index object to hold N columns total.  Return SQLITE_OK
//	** on success and SQLITE_NOMEM on an OOM error.
//	*/
func _resizeIndexObject(tls *libc.TLS, pParse uintptr, pIdx uintptr, N int32) (r int32) {
	var db, zExtra uintptr
	var nByte Tu64
	_, _, _ = db, nByte, zExtra
	if int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) >= N {
		return SQLITE_OK
	}
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	nByte = uint64(libc.Uint32FromInt64(4)+libc.Uint32FromInt64(2)+libc.Uint32FromInt64(2)+libc.Uint32FromInt32(1)) * uint64(N)
	zExtra = _sqlite3DbMallocZero(tls, db, nByte)
	if zExtra == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemcpy(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FazColl, uint32(4)*uint32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn))
	(*TIndex)(unsafe.Pointer(pIdx)).FazColl = zExtra
	zExtra = zExtra + uintptr(uint32(4)*uint32(N))
	libc.Xmemcpy(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst, uint32(2)*uint32(int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)+libc.Int32FromInt32(1)))
	(*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst = zExtra
	zExtra = zExtra + uintptr(uint32(2)*uint32(N))
	libc.Xmemcpy(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FaiColumn, uint32(2)*uint32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn))
	(*TIndex)(unsafe.Pointer(pIdx)).FaiColumn = zExtra
	zExtra = zExtra + uintptr(uint32(2)*uint32(N))
	libc.Xmemcpy(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder, uint32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn))
	(*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder = zExtra
	(*TIndex)(unsafe.Pointer(pIdx)).FnColumn = uint16(N) /* See tag-20250221-1 above for proof of safety */
	libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(1), 4, 0x10)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Resolve label "x" to be the address of the next instruction to
//	** be inserted.  The parameter "x" must have been obtained from
//	** a prior call to sqlite3VdbeMakeLabel().
//	*/
func _resizeResolveLabel(tls *libc.TLS, p uintptr, v uintptr, j int32) {
	var nNewSize int32
	_ = nNewSize
	nNewSize = int32(10) - (*TParse)(unsafe.Pointer(p)).FnLabel
	(*TParse)(unsafe.Pointer(p)).FaLabel = _sqlite3DbReallocOrFree(tls, (*TParse)(unsafe.Pointer(p)).Fdb, (*TParse)(unsafe.Pointer(p)).FaLabel, uint64(uint32(nNewSize)*uint32(4)))
	if (*TParse)(unsafe.Pointer(p)).FaLabel == uintptr(0) {
		(*TParse)(unsafe.Pointer(p)).FnLabelAlloc = 0
	} else {
		if nNewSize >= int32(100) && nNewSize/int32(100) > (*TParse)(unsafe.Pointer(p)).FnLabelAlloc/int32(100) {
			_sqlite3ProgressCheck(tls, p)
		}
		(*TParse)(unsafe.Pointer(p)).FnLabelAlloc = nNewSize
		**(**int32)(__ccgo_up((*TParse)(unsafe.Pointer(p)).FaLabel + uintptr(j)*4)) = (*TVdbe)(unsafe.Pointer(v)).FnOp
	}
}

// C documentation
//
//	/*
//	** Turn the pExpr expression into an alias for the iCol-th column of the
//	** result set in pEList.
//	**
//	** If the reference is followed by a COLLATE operator, then make sure
//	** the COLLATE operator is preserved.  For example:
//	**
//	**     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
//	**
//	** Should be transformed into:
//	**
//	**     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
//	**
//	** The nSubquery parameter specifies how many levels of subquery the
//	** alias is removed from the original expression.  The usual value is
//	** zero but it might be more if the alias is contained within a subquery
//	** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
//	** structures must be increased by the nSubquery amount.
//	*/
func _resolveAlias(tls *libc.TLS, pParse uintptr, pEList uintptr, iCol int32, pExpr uintptr, nSubquery int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var db, pDup, pOrig uintptr
	var _ /* temp at bp+0 */ TExpr
	_, _, _ = db, pDup, pOrig /* The database connection */
	pOrig = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(iCol)*20))).FpExpr
	if (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo != 0 {
		return
	}
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pDup = _sqlite3ExprDup(tls, db, pOrig, 0)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_sqlite3ExprDelete(tls, db, pDup)
		pDup = uintptr(0)
	} else {
		_incrAggFunctionDepth(tls, pDup, nSubquery)
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE) {
			pDup = _sqlite3ExprAddCollateString(tls, pParse, pDup, *(*uintptr)(unsafe.Pointer(pExpr + 8)))
		}
		libc.Xmemcpy(tls, bp, pDup, uint32(52))
		libc.Xmemcpy(tls, pDup, pExpr, uint32(52))
		libc.Xmemcpy(tls, pExpr, bp, uint32(52))
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
			if *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)) != uintptr(0) {
				(*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FpOwner = pExpr
			}
		}
		_sqlite3ExprDeferredDelete(tls, pParse, pDup)
	}
}

// C documentation
//
//	/*
//	** pEList is a list of expressions which are really the result set of the
//	** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
//	** This routine checks to see if pE is a simple identifier which corresponds
//	** to the AS-name of one of the terms of the expression list.  If it is,
//	** this routine return an integer between 1 and N where N is the number of
//	** elements in pEList, corresponding to the matching entry.  If there is
//	** no match, or if pE is not a simple identifier, then this routine
//	** return 0.
//	**
//	** pEList has been resolved.  pE has not.
//	*/
func _resolveAsName(tls *libc.TLS, pParse uintptr, pEList uintptr, pE uintptr) (r int32) {
	var i int32
	var zCol uintptr
	_, _ = i, zCol /* Loop counter */
	_ = pParse
	if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_ID) {
		zCol = *(*uintptr)(unsafe.Pointer(pE + 8))
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
				break
			}
			if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME && Xsqlite3_stricmp(tls, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName, zCol) == 0 {
				return i + int32(1)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	return 0
}

// C documentation
//
//	/*
//	** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
//	** each term of the ORDER BY clause is a constant integer between 1
//	** and N where N is the number of columns in the compound SELECT.
//	**
//	** ORDER BY terms that are already an integer between 1 and N are
//	** unmodified.  ORDER BY terms that are integers outside the range of
//	** 1 through N generate an error.  ORDER BY terms that are expressions
//	** are matched against result set expressions of compound SELECT
//	** beginning with the left-most SELECT and working toward the right.
//	** At the first match, the ORDER BY expression is transformed into
//	** the integer column number.
//	**
//	** Return the number of errors seen.
//	*/
func _resolveCompoundOrderBy(tls *libc.TLS, pParse uintptr, pSelect uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pDup, pE, pEList, pItem, pNew, pOrderBy, pParent uintptr
	var i, moreToDo int32
	var _ /* iCol at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _ = db, i, moreToDo, pDup, pE, pEList, pItem, pNew, pOrderBy, pParent
	moreToDo = int32(1)
	pOrderBy = (*TSelect)(unsafe.Pointer(pSelect)).FpOrderBy
	if pOrderBy == uintptr(0) {
		return 0
	}
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr > **(**int32)(__ccgo_up(db + 120 + 2*4)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8842, 0)
		return int32(1)
	}
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) {
			break
		}
		libc.SetBitFieldPtr16Uint32(pOrderBy+8+uintptr(i)*20+8+4, libc.Uint32FromInt32(0), 2, 0x4)
		goto _1
	_1:
		;
		i = i + 1
	}
	(*TSelect)(unsafe.Pointer(pSelect)).FpNext = uintptr(0)
	for (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 {
		(*TSelect)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpPrior)).FpNext = pSelect
		pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior
	}
	for pSelect != 0 && moreToDo != 0 {
		moreToDo = 0
		pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList
		i = 0
		pItem = pOrderBy + 8
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) {
				break
			}
			**(**int32)(__ccgo_up(bp)) = -int32(1)
			if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 8 + 4))&0x4>>2)) != 0 {
				goto _2
			}
			pE = _sqlite3ExprSkipCollateAndLikely(tls, (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr)
			if pE == uintptr(0) {
				goto _2
			}
			if _sqlite3ExprIsInteger(tls, pE, bp, uintptr(0)) != 0 {
				if **(**int32)(__ccgo_up(bp)) <= 0 || **(**int32)(__ccgo_up(bp)) > (*TExprList)(unsafe.Pointer(pEList)).FnExpr {
					_resolveOutOfRangeError(tls, pParse, __ccgo_ts+8876, i+int32(1), (*TExprList)(unsafe.Pointer(pEList)).FnExpr, pE)
					return int32(1)
				}
			} else {
				**(**int32)(__ccgo_up(bp)) = _resolveAsName(tls, pParse, pEList, pE)
				if **(**int32)(__ccgo_up(bp)) == 0 {
					/* Now test if expression pE matches one of the values returned
					 ** by pSelect. In the usual case this is done by duplicating the
					 ** expression, resolving any symbols in it, and then comparing
					 ** it against each expression returned by the SELECT statement.
					 ** Once the comparisons are finished, the duplicate expression
					 ** is deleted.
					 **
					 ** If this is running as part of an ALTER TABLE operation and
					 ** the symbols resolve successfully, also resolve the symbols in the
					 ** actual expression. This allows the code in alter.c to modify
					 ** column references within the ORDER BY expression as required.  */
					pDup = _sqlite3ExprDup(tls, db, pE, 0)
					if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
						**(**int32)(__ccgo_up(bp)) = _resolveOrderByTermToExprList(tls, pParse, pSelect, pDup)
						if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && **(**int32)(__ccgo_up(bp)) > 0 {
							_resolveOrderByTermToExprList(tls, pParse, pSelect, pE)
						}
					}
					_sqlite3ExprDelete(tls, db, pDup)
				}
			}
			if **(**int32)(__ccgo_up(bp)) > 0 {
				/* Convert the ORDER BY term into an integer column number iCol,
				 ** taking care to preserve the COLLATE clause if it exists. */
				if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
					pNew = _sqlite3ExprInt32(tls, db, **(**int32)(__ccgo_up(bp)))
					if pNew == uintptr(0) {
						return int32(1)
					}
					if (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr == pE {
						(*TExprList_item)(unsafe.Pointer(pItem)).FpExpr = pNew
					} else {
						pParent = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr
						for int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pParent)).FpLeft)).Fop) == int32(TK_COLLATE) {
							pParent = (*TExpr)(unsafe.Pointer(pParent)).FpLeft
						}
						(*TExpr)(unsafe.Pointer(pParent)).FpLeft = pNew
					}
					_sqlite3ExprDelete(tls, db, pE)
					(*(*struct {
						FiOrderByCol Tu16
						FiAlias      Tu16
					})(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(**(**int32)(__ccgo_up(bp)))
				}
				libc.SetBitFieldPtr16Uint32(pItem+8+4, libc.Uint32FromInt32(1), 2, 0x4)
			} else {
				moreToDo = int32(1)
			}
			goto _2
		_2:
			;
			i = i + 1
			pItem += 20
		}
		pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpNext
	}
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) {
			break
		}
		if int32(uint32(*(*uint16)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20 + 8 + 4))&0x4>>2)) == 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8882, libc.VaList(bp+16, i+int32(1)))
			return int32(1)
		}
		goto _3
	_3:
		;
		i = i + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** This routine is callback for sqlite3WalkExpr().
//	**
//	** Resolve symbolic names into TK_COLUMN operators for the current
//	** node in the expression tree.  Return 0 to continue the search down
//	** the tree or 2 to abort the tree walk.
//	**
//	** This routine also does error checking and name resolution for
//	** function names.  The operator for aggregate functions is changed
//	** to TK_AGG_FUNCTION.
//	*/
func _resolveExprStep(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var anRef [8]int32
	var auth, i, is_agg, n, nLeft, nRef, nRight, no_such_func, rc, savedAllowFlags, wrong_num_args, v5 int32
	var enc Tu8
	var p, pDef, pItem, pLeft, pList, pNC, pNC2, pParse, pRight, pRight1, pSel, pSrcList, pWin, zDb, zId, zTable, zType, v4 uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = anRef, auth, enc, i, is_agg, n, nLeft, nRef, nRight, no_such_func, p, pDef, pItem, pLeft, pList, pNC, pNC2, pParse, pRight, pRight1, pSel, pSrcList, pWin, rc, savedAllowFlags, wrong_num_args, zDb, zId, zTable, zType, v4, v5
	pNC = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	pParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse
	switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) {
	/* The special operator TK_ROW means use the rowid for the first
	 ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
	 ** clause processing on UPDATE and DELETE statements, and by
	 ** UPDATE ... FROM statement processing.
	 */
	case int32(TK_ROW):
		pSrcList = (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList
		pItem = pSrcList + 8
		(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_COLUMN)
		(*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
		(*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor
		(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = (*TExpr)(unsafe.Pointer(pExpr)).FiColumn - 1
		(*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = int8(SQLITE_AFF_INTEGER)
		break
		/* An optimization:  Attempt to convert
		 **
		 **      "expr IS NOT NULL"  -->  "TRUE"
		 **      "expr IS NULL"      -->  "FALSE"
		 **
		 ** if we can prove that "expr" is never NULL.  Call this the
		 ** "NOT NULL strength reduction optimization".
		 **
		 ** If this optimization occurs, also restore the NameContext ref-counts
		 ** to the state they where in before the "column" LHS expression was
		 ** resolved.  This prevents "column" from being counted as having been
		 ** referenced, which might prevent a SELECT from being erroneously
		 ** marked as correlated.
		 **
		 ** 2024-03-28: Beware of aggregates.  A bare column of aggregated table
		 ** can still evaluate to NULL even though it is marked as NOT NULL.
		 ** Example:
		 **
		 **       CREATE TABLE t1(a INT NOT NULL);
		 **       SELECT a, a IS NULL, a IS NOT NULL, count(*) FROM t1;
		 **
		 ** The "a IS NULL" and "a IS NOT NULL" expressions cannot be optimized
		 ** here because at the time this case is hit, we do not yet know whether
		 ** or not t1 is being aggregated.  We have to assume the worst and omit
		 ** the optimization.  The only time it is safe to apply this optimization
		 ** is within the WHERE clause.
		 */
		fallthrough
	case int32(TK_NOTNULL):
		fallthrough
	case int32(TK_ISNULL):
		i = 0
		p = pNC
		for {
			if !(p != 0 && i < int32(libc.Uint32FromInt64(32)/libc.Uint32FromInt64(4))) {
				break
			}
			anRef[i] = (*TNameContext)(unsafe.Pointer(p)).FnRef
			goto _1
		_1:
			;
			p = (*TNameContext)(unsafe.Pointer(p)).FpNext
			i = i + 1
		}
		_sqlite3WalkExpr(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
			return int32(WRC_Prune)
		}
		if _sqlite3ExprCanBeNull(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) != 0 {
			/* The expression can be NULL.  So the optimization does not apply */
			return int32(WRC_Prune)
		}
		i = 0
		p = pNC
		for {
			if !(p != 0) {
				break
			}
			if (*TNameContext)(unsafe.Pointer(p)).FncFlags&int32(NC_Where) == 0 {
				return int32(WRC_Prune) /* Not in a WHERE clause.  Unsafe to optimize. */
			}
			goto _2
		_2:
			;
			p = (*TNameContext)(unsafe.Pointer(p)).FpNext
			i = i + 1
		}
		*(*int32)(unsafe.Pointer(pExpr + 8)) = libc.BoolInt32(int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_NOTNULL))
		**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(EP_IntValue)
		(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_INTEGER)
		i = 0
		p = pNC
		for {
			if !(p != 0 && i < int32(libc.Uint32FromInt64(32)/libc.Uint32FromInt64(4))) {
				break
			}
			(*TNameContext)(unsafe.Pointer(p)).FnRef = anRef[i]
			goto _3
		_3:
			;
			p = (*TNameContext)(unsafe.Pointer(p)).FpNext
			i = i + 1
		}
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		(*TExpr)(unsafe.Pointer(pExpr)).FpLeft = uintptr(0)
		return int32(WRC_Prune)
		/* A column name:                    ID
		 ** Or table name and column name:    ID.ID
		 ** Or a database, table and column:  ID.ID.ID
		 **
		 ** The TK_ID and TK_OUT cases are combined so that there will only
		 ** be one call to lookupName().  Then the compiler will in-line
		 ** lookupName() for a size reduction and performance increase.
		 */
		fallthrough
	case int32(TK_ID):
		fallthrough
	case int32(TK_DOT):
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_ID) {
			zDb = uintptr(0)
			zTable = uintptr(0)
			pRight = pExpr
		} else {
			pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
			if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_IdxExpr)|libc.Int32FromInt32(NC_GenCol)) != 0 {
				_notValidImpl(tls, pParse, pNC, __ccgo_ts+8417, uintptr(0), pExpr)
			}
			pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight
			if int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_ID) {
				zDb = uintptr(0)
			} else {
				zDb = *(*uintptr)(unsafe.Pointer(pLeft + 8))
				pLeft = (*TExpr)(unsafe.Pointer(pRight)).FpLeft
				pRight = (*TExpr)(unsafe.Pointer(pRight)).FpRight
			}
			zTable = *(*uintptr)(unsafe.Pointer(pLeft + 8))
			if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
				_sqlite3RenameTokenRemap(tls, pParse, pExpr, pRight)
				_sqlite3RenameTokenRemap(tls, pParse, pExpr+44, pLeft)
			}
		}
		return _lookupName(tls, pParse, zDb, zTable, pRight, pNC, pExpr)
		/* Resolve function names
		 */
		fallthrough
	case int32(TK_FUNCTION): /* Number of arguments */
		no_such_func = 0                                                              /* True if no such function exists */
		wrong_num_args = 0                                                            /* True if wrong number of arguments */
		is_agg = 0                                                                    /* Information about the function */
		enc = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fenc /* The database encoding */
		savedAllowFlags = (*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_AllowAgg) | libc.Int32FromInt32(NC_AllowWin))
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) && int32((*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FeFrmType) != int32(TK_FILTER) {
			v4 = *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy))
		} else {
			v4 = uintptr(0)
		}
		pWin = v4
		pList = *(*uintptr)(unsafe.Pointer(pExpr + 20))
		if pList != 0 {
			v5 = (*TExprList)(unsafe.Pointer(pList)).FnExpr
		} else {
			v5 = 0
		}
		n = v5
		zId = *(*uintptr)(unsafe.Pointer(pExpr + 8))
		pDef = _sqlite3FindFunction(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zId, n, enc, uint8(0))
		if pDef == uintptr(0) {
			pDef = _sqlite3FindFunction(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zId, -int32(2), enc, uint8(0))
			if pDef == uintptr(0) {
				no_such_func = int32(1)
			} else {
				wrong_num_args = int32(1)
			}
		} else {
			is_agg = libc.BoolInt32((*TFuncDef)(unsafe.Pointer(pDef)).FxFinalize != uintptr(0))
			if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_UNLIKELY) != 0 {
				**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Unlikely))
				if n == int32(2) {
					(*TExpr)(unsafe.Pointer(pExpr)).FiTable = _exprProbability(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*20))).FpExpr)
					if (*TExpr)(unsafe.Pointer(pExpr)).FiTable < 0 {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8434, libc.VaList(bp+8, pExpr))
						(*TNameContext)(unsafe.Pointer(pNC)).FnNcErr = (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr + 1
					}
				} else {
					/* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
					 ** equivalent to likelihood(X, 0.0625).
					 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
					 ** short-hand for likelihood(X,0.0625).
					 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
					 ** for likelihood(X,0.9375).
					 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
					 ** to likelihood(X,0.9375). */
					/* TUNING: unlikely() probability is 0.0625.  likely() is 0.9375 */
					if int32(**(**int8)(__ccgo_up((*TFuncDef)(unsafe.Pointer(pDef)).FzName))) == int32('u') {
						v5 = int32(8388608)
					} else {
						v5 = int32(125829120)
					}
					(*TExpr)(unsafe.Pointer(pExpr)).FiTable = v5
				}
			}
			auth = _sqlite3AuthCheck(tls, pParse, int32(SQLITE_FUNCTION), uintptr(0), (*TFuncDef)(unsafe.Pointer(pDef)).FzName, uintptr(0))
			if auth != SQLITE_OK {
				if auth == int32(SQLITE_DENY) {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8498, libc.VaList(bp+8, pExpr))
					(*TNameContext)(unsafe.Pointer(pNC)).FnNcErr = (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr + 1
				}
				(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_NULL)
				return int32(WRC_Prune)
			}
			/* If the function may call sqlite3_value_subtype(), then set the
			 ** EP_SubtArg flag on all of its argument expressions. This prevents
			 ** where.c from replacing the expression with a value read from an
			 ** index on the same expression, which will not have the correct
			 ** subtype. Also set the flag if the function expression itself is
			 ** an EP_SubtArg expression. In this case subtypes are required as
			 ** the function may return a value with a subtype back to its
			 ** caller using sqlite3_result_value().  */
			if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_SUBTYPE) != 0 || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&libc.Uint32FromUint32(EP_SubtArg) != uint32(0) {
				_resolveSetExprSubtypeArg(tls, pList)
			}
			if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)|libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG)) != 0 {
				/* For the purposes of the EP_ConstFunc flag, date and time
				 ** functions and other functions that change slowly are considered
				 ** constant because they are constant for the duration of one query.
				 ** This allows them to be factored out of inner loops. */
				**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_ConstFunc))
			}
			if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_CONSTANT) == uint32(0) {
				/* Clearly non-deterministic functions like random(), but also
				 ** date/time functions that use 'now', and other functions like
				 ** sqlite_version() that might change over time cannot be used
				 ** in an index or generated column.  Curiously, they can be used
				 ** in a CHECK constraint.  SQLServer, MySQL, and PostgreSQL all
				 ** allow this. */
				if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_IdxExpr)|libc.Int32FromInt32(NC_PartIdx)|libc.Int32FromInt32(NC_GenCol)) != 0 {
					_notValidImpl(tls, pParse, pNC, __ccgo_ts+8534, uintptr(0), pExpr)
				}
			} else {
				/* Must fit in 8 bits */
				(*TExpr)(unsafe.Pointer(pExpr)).Fop2 = uint8((*TNameContext)(unsafe.Pointer(pNC)).FncFlags & int32(NC_SelfRef))
			}
			if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_INTERNAL) != uint32(0) && int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmDbFlags&uint32(DBFLAG_InternalFunc) == uint32(0) {
				/* Internal-use-only functions are disallowed unless the
				 ** SQL is being compiled using sqlite3NestedParse() or
				 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
				 ** used to activate internal functions for testing purposes */
				no_such_func = int32(1)
				pDef = uintptr(0)
			} else {
				if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_DIRECT)|libc.Int32FromInt32(SQLITE_FUNC_UNSAFE)) != uint32(0) && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
					if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_FromDDL) != 0 {
						**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_FromDDL))
					}
					_sqlite3ExprFunctionUsable(tls, pParse, pExpr, pDef)
				}
			}
		}
		if 0 == libc.BoolInt32(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME)) {
			if pDef != 0 && (*TFuncDef)(unsafe.Pointer(pDef)).FxValue == uintptr(0) && pWin != 0 {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8562, libc.VaList(bp+8, pExpr))
				(*TNameContext)(unsafe.Pointer(pNC)).FnNcErr = (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr + 1
			} else {
				if is_agg != 0 && (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_AllowAgg) == 0 || is_agg != 0 && (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_WINDOW) != 0 && !(pWin != 0) || is_agg != 0 && pWin != 0 && (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_AllowWin) == 0 {
					if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_WINDOW) != 0 || pWin != 0 {
						zType = __ccgo_ts + 8605
					} else {
						zType = __ccgo_ts + 8612
					}
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8622, libc.VaList(bp+8, zType, pExpr))
					(*TNameContext)(unsafe.Pointer(pNC)).FnNcErr = (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr + 1
					is_agg = 0
				} else {
					if no_such_func != 0 && int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Finit1.Fbusy) == 0 {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8650, libc.VaList(bp+8, pExpr))
						(*TNameContext)(unsafe.Pointer(pNC)).FnNcErr = (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr + 1
					} else {
						if wrong_num_args != 0 {
							_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8672, libc.VaList(bp+8, pExpr))
							(*TNameContext)(unsafe.Pointer(pNC)).FnNcErr = (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr + 1
						} else {
							if is_agg == 0 && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
								_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8716, libc.VaList(bp+8, pExpr))
								(*TNameContext)(unsafe.Pointer(pNC)).FnNcErr = (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr + 1
							} else {
								if is_agg == 0 && (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 {
									_sqlite3ExprOrderByAggregateError(tls, pParse, pExpr)
									(*TNameContext)(unsafe.Pointer(pNC)).FnNcErr = (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr + 1
								}
							}
						}
					}
				}
			}
			if is_agg != 0 {
				/* Window functions may not be arguments of aggregate functions.
				 ** Or arguments of other window functions. But aggregate functions
				 ** may be arguments for window functions.  */
				if !(pWin != 0) {
					v5 = int32(NC_AllowAgg)
				} else {
					v5 = 0
				}
				**(**int32)(__ccgo_up(pNC + 24)) &= ^(libc.Int32FromInt32(NC_AllowWin) | v5)
			}
		} else {
			if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) || (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 {
				is_agg = int32(1)
			}
		}
		_sqlite3WalkExprList(tls, pWalker, pList)
		if is_agg != 0 {
			if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 {
				_sqlite3WalkExprList(tls, pWalker, *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20)))
			}
			if pWin != 0 && (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
				pSel = (*TNameContext)(unsafe.Pointer(pNC)).FpWinSelect
				if libc.BoolInt32(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME)) == 0 {
					if pSel != 0 {
						v4 = (*TSelect)(unsafe.Pointer(pSel)).FpWinDefn
					} else {
						v4 = uintptr(0)
					}
					_sqlite3WindowUpdate(tls, pParse, v4, pWin, pDef)
					if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
						break
					}
				}
				_sqlite3WalkExprList(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpPartition)
				_sqlite3WalkExprList(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy)
				_sqlite3WalkExpr(tls, pWalker, (*TWindow)(unsafe.Pointer(pWin)).FpFilter)
				_sqlite3WindowLink(tls, pSel, pWin)
				**(**int32)(__ccgo_up(pNC + 24)) |= int32(NC_HasWin)
			} else {
				/* For looping up thru outer contexts */
				(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_AGG_FUNCTION)
				(*TExpr)(unsafe.Pointer(pExpr)).Fop2 = uint8(0)
				if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
					_sqlite3WalkExpr(tls, pWalker, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FpFilter)
				}
				pNC2 = pNC
				for pNC2 != 0 && _sqlite3ReferencesSrcList(tls, pParse, pExpr, (*TNameContext)(unsafe.Pointer(pNC2)).FpSrcList) == 0 {
					v4 = pExpr + 2
					*(*Tu8)(unsafe.Pointer(v4)) = Tu8(uint32(*(*Tu8)(unsafe.Pointer(v4))) + (libc.Uint32FromInt32(1) + (*TNameContext)(unsafe.Pointer(pNC2)).FnNestedSelect))
					pNC2 = (*TNameContext)(unsafe.Pointer(pNC2)).FpNext
				}
				if pNC2 != 0 && pDef != 0 {
					v4 = pExpr + 2
					*(*Tu8)(unsafe.Pointer(v4)) = Tu8(uint32(*(*Tu8)(unsafe.Pointer(v4))) + (*TNameContext)(unsafe.Pointer(pNC2)).FnNestedSelect)
					v4 = pNC2 + 24
					*(*int32)(unsafe.Pointer(v4)) = int32(uint32(*(*int32)(unsafe.Pointer(v4))) | (libc.Uint32FromInt32(NC_HasAgg) | ((*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags^libc.Uint32FromInt32(SQLITE_FUNC_ANYORDER))&uint32(libc.Int32FromInt32(SQLITE_FUNC_MINMAX)|libc.Int32FromInt32(SQLITE_FUNC_ANYORDER))))
				}
			}
			**(**int32)(__ccgo_up(pNC + 24)) |= savedAllowFlags
		}
		/* FIX ME:  Compute pExpr->affinity based on the expected return
		 ** type of the function
		 */
		return int32(WRC_Prune)
	case int32(TK_EXISTS):
		fallthrough
	case int32(TK_SELECT):
		fallthrough
	case int32(TK_IN):
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
			nRef = (*TNameContext)(unsafe.Pointer(pNC)).FnRef
			if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_EXISTS) {
				libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 4, 0x10)
			}
			if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_SelfRef) != 0 {
				_notValidImpl(tls, pParse, pNC, __ccgo_ts+8764, pExpr, pExpr)
			} else {
				_sqlite3WalkSelect(tls, pWalker, *(*uintptr)(unsafe.Pointer(pExpr + 20)))
			}
			if nRef != (*TNameContext)(unsafe.Pointer(pNC)).FnRef {
				**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_VarSelect))
				**(**Tu32)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 4)) |= uint32(SF_Correlated)
			}
			**(**int32)(__ccgo_up(pNC + 24)) |= int32(NC_Subquery)
		}
	case int32(TK_VARIABLE):
		if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_IsCheck)|libc.Int32FromInt32(NC_PartIdx)|libc.Int32FromInt32(NC_IdxExpr)|libc.Int32FromInt32(NC_GenCol)) != 0 {
			_notValidImpl(tls, pParse, pNC, __ccgo_ts+8775, pExpr, pExpr)
		}
	case int32(TK_IS):
		fallthrough
	case int32(TK_ISNOT):
		pRight1 = _sqlite3ExprSkipCollateAndLikely(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight)
		/* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
		 ** and "x IS NOT FALSE". */
		if pRight1 != 0 && (int32((*TExpr)(unsafe.Pointer(pRight1)).Fop) == int32(TK_ID) || int32((*TExpr)(unsafe.Pointer(pRight1)).Fop) == int32(TK_TRUEFALSE)) {
			rc = _resolveExprStep(tls, pWalker, pRight1)
			if rc == int32(WRC_Abort) {
				return int32(WRC_Abort)
			}
			if int32((*TExpr)(unsafe.Pointer(pRight1)).Fop) == int32(TK_TRUEFALSE) {
				(*TExpr)(unsafe.Pointer(pExpr)).Fop2 = (*TExpr)(unsafe.Pointer(pExpr)).Fop
				(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_TRUTH)
				return WRC_Continue
			}
		}
		fallthrough
	case int32(TK_BETWEEN):
		fallthrough
	case int32(TK_EQ):
		fallthrough
	case int32(TK_NE):
		fallthrough
	case int32(TK_LT):
		fallthrough
	case int32(TK_LE):
		fallthrough
	case int32(TK_GT):
		fallthrough
	case int32(TK_GE):
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
			break
		}
		nLeft = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_BETWEEN) {
			nRight = _sqlite3ExprVectorSize(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr)
			if nRight == nLeft {
				nRight = _sqlite3ExprVectorSize(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 1*20))).FpExpr)
			}
		} else {
			nRight = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight)
		}
		if nLeft != nRight {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8125, 0)
			_sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr)
		}
		break
	}
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		v5 = int32(WRC_Abort)
	} else {
		v5 = WRC_Continue
	}
	return v5
}

// C documentation
//
//	/*
//	** This function checks if argument pFrom refers to a CTE declared by
//	** a WITH clause on the stack currently maintained by the parser (on the
//	** pParse->pWith linked list).  And if currently processing a CTE
//	** CTE expression, through routine checks to see if the reference is
//	** a recursive reference to the CTE.
//	**
//	** If pFrom matches a CTE according to either of these two above, pFrom->pSTab
//	** and other fields are populated accordingly.
//	**
//	** Return 0 if no match is found.
//	** Return 1 if a match is found.
//	** Return 2 if an error condition is detected.
//	*/
func _resolveFromTermToCte(tls *libc.TLS, pParse uintptr, pWalker uintptr, pFrom uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var bMayRecursive, i, iRecTab, rc, v3 int32
	var db, pCte, pCteUse, pEList, pItem, pLeft, pRecTerm, pSavedWith, pSel, pSrc, pTab, v1 uintptr
	var _ /* pWith at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bMayRecursive, db, i, iRecTab, pCte, pCteUse, pEList, pItem, pLeft, pRecTerm, pSavedWith, pSel, pSrc, pTab, rc, v1, v3 /* The matching WITH */
	if (*TParse)(unsafe.Pointer(pParse)).FpWith == uintptr(0) {
		/* There are no WITH clauses in the stack.  No match is possible */
		return 0
	}
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		/* Prior errors might have left pParse->pWith in a goofy state, so
		 ** go no further. */
		return 0
	}
	if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x10000>>16) == 0 && *(*uintptr)(unsafe.Pointer(pFrom + 44)) != uintptr(0) {
		/* The FROM term contains a schema qualifier (ex: main.t1) and so
		 ** it cannot possibly be a CTE reference. */
		return 0
	}
	if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x400>>10) != 0 {
		/* The FROM term is specifically excluded from matching a CTE.
		 **   (1)  It is part of a trigger that used to have zDatabase but had
		 **        zDatabase removed by sqlite3FixTriggerStep().
		 **   (2)  This is the first term in the FROM clause of an UPDATE.
		 */
		return 0
	}
	pCte = _searchWith(tls, (*TParse)(unsafe.Pointer(pParse)).FpWith, pFrom, bp)
	if pCte != 0 {
		db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Initial value of pParse->pWith */
		iRecTab = -int32(1)
		/* If pCte->zCteErr is non-NULL at this point, then this is an illegal
		 ** recursive reference to CTE pCte. Leave an error in pParse and return
		 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
		 ** In this case, proceed.  */
		if (*TCte)(unsafe.Pointer(pCte)).FzCteErr != 0 {
			_sqlite3ErrorMsg(tls, pParse, (*TCte)(unsafe.Pointer(pCte)).FzCteErr, libc.VaList(bp+16, (*TCte)(unsafe.Pointer(pCte)).FzName))
			return int32(2)
		}
		if _cannotBeFunction(tls, pParse, pFrom) != 0 {
			return int32(2)
		}
		pTab = _sqlite3DbMallocZero(tls, db, uint64(80))
		if pTab == uintptr(0) {
			return int32(2)
		}
		pCteUse = (*TCte)(unsafe.Pointer(pCte)).FpUse
		if pCteUse == uintptr(0) {
			v1 = _sqlite3DbMallocZero(tls, db, uint64(20))
			pCteUse = v1
			(*TCte)(unsafe.Pointer(pCte)).FpUse = v1
			if pCteUse == uintptr(0) || _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3DbFree), pCteUse) == uintptr(0) {
				_sqlite3DbFree(tls, db, pTab)
				return int32(2)
			}
			(*TCteUse)(unsafe.Pointer(pCteUse)).FeM10d = (*TCte)(unsafe.Pointer(pCte)).FeM10d
		}
		(*TSrcItem)(unsafe.Pointer(pFrom)).FpSTab = pTab
		(*TTable)(unsafe.Pointer(pTab)).FnTabRef = uint32(1)
		(*TTable)(unsafe.Pointer(pTab)).FzName = _sqlite3DbStrDup(tls, db, (*TCte)(unsafe.Pointer(pCte)).FzName)
		(*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1))
		(*TTable)(unsafe.Pointer(pTab)).FnRowLogEst = int16(200)
		**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(libc.Int32FromInt32(TF_Ephemeral) | libc.Int32FromInt32(TF_NoVisibleRowid))
		_sqlite3SrcItemAttachSubquery(tls, pParse, pFrom, (*TCte)(unsafe.Pointer(pCte)).FpSelect, int32(1))
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			return int32(2)
		}
		pSel = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pFrom + 44)))).FpSelect
		**(**Tu32)(__ccgo_up(pSel + 4)) |= uint32(SF_CopyCte)
		if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x2>>1) != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22289, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(pFrom + 32))))
			return int32(2)
		}
		libc.SetBitFieldPtr32Uint32(pFrom+12+4, libc.Uint32FromInt32(1), 9, 0x200)
		*(*uintptr)(unsafe.Pointer(pFrom + 36)) = pCteUse
		(*TCteUse)(unsafe.Pointer(pCteUse)).FnUse = (*TCteUse)(unsafe.Pointer(pCteUse)).FnUse + 1
		/* Check if this is a recursive CTE. */
		pRecTerm = pSel
		bMayRecursive = libc.BoolInt32(int32((*TSelect)(unsafe.Pointer(pSel)).Fop) == int32(TK_ALL) || int32((*TSelect)(unsafe.Pointer(pSel)).Fop) == int32(TK_UNION))
		for bMayRecursive != 0 && int32((*TSelect)(unsafe.Pointer(pRecTerm)).Fop) == int32((*TSelect)(unsafe.Pointer(pSel)).Fop) {
			pSrc = (*TSelect)(unsafe.Pointer(pRecTerm)).FpSrc
			i = 0
			for {
				if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) {
					break
				}
				pItem = pSrc + 8 + uintptr(i)*48
				if (*TSrcItem)(unsafe.Pointer(pItem)).FzName != uintptr(0) && !(int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x20000>>17) != 0) && !(int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4>>2) != 0) && (int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x10000>>16) != 0 || *(*uintptr)(unsafe.Pointer(pItem + 44)) == uintptr(0)) && 0 == _sqlite3StrICmp(tls, (*TSrcItem)(unsafe.Pointer(pItem)).FzName, (*TCte)(unsafe.Pointer(pCte)).FzName) {
					(*TSrcItem)(unsafe.Pointer(pItem)).FpSTab = pTab
					(*TTable)(unsafe.Pointer(pTab)).FnTabRef = (*TTable)(unsafe.Pointer(pTab)).FnTabRef + 1
					libc.SetBitFieldPtr32Uint32(pItem+12+4, libc.Uint32FromInt32(1), 7, 0x80)
					if (*TSelect)(unsafe.Pointer(pRecTerm)).FselFlags&uint32(SF_Recursive) != 0 {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22309, libc.VaList(bp+16, (*TCte)(unsafe.Pointer(pCte)).FzName))
						return int32(2)
					}
					**(**Tu32)(__ccgo_up(pRecTerm + 4)) |= uint32(SF_Recursive)
					if iRecTab < 0 {
						v1 = pParse + 44
						v3 = *(*int32)(unsafe.Pointer(v1))
						*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
						iRecTab = v3
					}
					(*TSrcItem)(unsafe.Pointer(pItem)).FiCursor = iRecTab
				}
				goto _2
			_2:
				;
				i = i + 1
			}
			if (*TSelect)(unsafe.Pointer(pRecTerm)).FselFlags&uint32(SF_Recursive) == uint32(0) {
				break
			}
			pRecTerm = (*TSelect)(unsafe.Pointer(pRecTerm)).FpPrior
		}
		(*TCte)(unsafe.Pointer(pCte)).FzCteErr = __ccgo_ts + 22352
		pSavedWith = (*TParse)(unsafe.Pointer(pParse)).FpWith
		(*TParse)(unsafe.Pointer(pParse)).FpWith = **(**uintptr)(__ccgo_up(bp))
		if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_Recursive) != 0 {
			(*TSelect)(unsafe.Pointer(pRecTerm)).FpWith = (*TSelect)(unsafe.Pointer(pSel)).FpWith
			rc = _sqlite3WalkSelect(tls, pWalker, pRecTerm)
			(*TSelect)(unsafe.Pointer(pRecTerm)).FpWith = uintptr(0)
			if rc != 0 {
				(*TParse)(unsafe.Pointer(pParse)).FpWith = pSavedWith
				return int32(2)
			}
		} else {
			if _sqlite3WalkSelect(tls, pWalker, pSel) != 0 {
				(*TParse)(unsafe.Pointer(pParse)).FpWith = pSavedWith
				return int32(2)
			}
		}
		(*TParse)(unsafe.Pointer(pParse)).FpWith = **(**uintptr)(__ccgo_up(bp))
		pLeft = pSel
		for {
			if !((*TSelect)(unsafe.Pointer(pLeft)).FpPrior != 0) {
				break
			}
			goto _5
		_5:
			;
			pLeft = (*TSelect)(unsafe.Pointer(pLeft)).FpPrior
		}
		pEList = (*TSelect)(unsafe.Pointer(pLeft)).FpEList
		if (*TCte)(unsafe.Pointer(pCte)).FpCols != 0 {
			if pEList != 0 && (*TExprList)(unsafe.Pointer(pEList)).FnExpr != (*TExprList)(unsafe.Pointer((*TCte)(unsafe.Pointer(pCte)).FpCols)).FnExpr {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22375, libc.VaList(bp+16, (*TCte)(unsafe.Pointer(pCte)).FzName, (*TExprList)(unsafe.Pointer(pEList)).FnExpr, (*TExprList)(unsafe.Pointer((*TCte)(unsafe.Pointer(pCte)).FpCols)).FnExpr))
				(*TParse)(unsafe.Pointer(pParse)).FpWith = pSavedWith
				return int32(2)
			}
			pEList = (*TCte)(unsafe.Pointer(pCte)).FpCols
		}
		_sqlite3ColumnsFromExprList(tls, pParse, pEList, pTab+34, pTab+4)
		if bMayRecursive != 0 {
			if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_Recursive) != 0 {
				(*TCte)(unsafe.Pointer(pCte)).FzCteErr = __ccgo_ts + 22413
			} else {
				(*TCte)(unsafe.Pointer(pCte)).FzCteErr = __ccgo_ts + 22447
			}
			_sqlite3WalkSelect(tls, pWalker, pSel)
		}
		(*TCte)(unsafe.Pointer(pCte)).FzCteErr = uintptr(0)
		(*TParse)(unsafe.Pointer(pParse)).FpWith = pSavedWith
		return int32(1) /* Success */
	}
	return 0 /* No match */
}

// C documentation
//
//	/*
//	** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
//	** The Name context of the SELECT statement is pNC.  zType is either
//	** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
//	**
//	** This routine resolves each term of the clause into an expression.
//	** If the order-by term is an integer I between 1 and N (where N is the
//	** number of columns in the result set of the SELECT) then the expression
//	** in the resolution is a copy of the I-th result-set expression.  If
//	** the order-by term is an identifier that corresponds to the AS-name of
//	** a result-set expression, then the term resolves to a copy of the
//	** result-set expression.  Otherwise, the expression is resolved in
//	** the usual way - using sqlite3ResolveExprNames().
//	**
//	** This routine returns the number of errors.  If errors occur, then
//	** an appropriate error message might be left in pParse.  (OOM errors
//	** excepted.)
//	*/
func _resolveOrderGroupBy(tls *libc.TLS, pNC uintptr, pSelect uintptr, pOrderBy uintptr, zType uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, j, nResult int32
	var pE, pE2, pItem, pParse uintptr
	var _ /* iCol at bp+0 */ int32
	_, _, _, _, _, _, _ = i, j, nResult, pE, pE2, pItem, pParse /* Number of terms in the result set */
	nResult = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpEList)).FnExpr
	pParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse
	i = 0
	pItem = pOrderBy + 8
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) {
			break
		}
		pE = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr
		pE2 = _sqlite3ExprSkipCollateAndLikely(tls, pE)
		if pE2 == uintptr(0) {
			goto _1
		}
		if int32(**(**int8)(__ccgo_up(zType))) != int32('G') {
			**(**int32)(__ccgo_up(bp)) = _resolveAsName(tls, pParse, (*TSelect)(unsafe.Pointer(pSelect)).FpEList, pE2)
			if **(**int32)(__ccgo_up(bp)) > 0 {
				/* If an AS-name match is found, mark this ORDER BY column as being
				 ** a copy of the iCol-th result-set column.  The subsequent call to
				 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
				 ** copy of the iCol-th result-set expression. */
				(*(*struct {
					FiOrderByCol Tu16
					FiAlias      Tu16
				})(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(**(**int32)(__ccgo_up(bp)))
				goto _1
			}
		}
		if _sqlite3ExprIsInteger(tls, pE2, bp, uintptr(0)) != 0 {
			/* The ORDER BY term is an integer constant.  Again, set the column
			 ** number so that sqlite3ResolveOrderGroupBy() will convert the
			 ** order-by term to a copy of the result-set expression */
			if **(**int32)(__ccgo_up(bp)) < int32(1) || **(**int32)(__ccgo_up(bp)) > int32(0xffff) {
				_resolveOutOfRangeError(tls, pParse, zType, i+int32(1), nResult, pE2)
				return int32(1)
			}
			(*(*struct {
				FiOrderByCol Tu16
				FiAlias      Tu16
			})(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(**(**int32)(__ccgo_up(bp)))
			goto _1
		}
		/* Otherwise, treat the ORDER BY term as an ordinary expression */
		(*(*struct {
			FiOrderByCol Tu16
			FiAlias      Tu16
		})(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(0)
		if _sqlite3ResolveExprNames(tls, pNC, pE) != 0 {
			return int32(1)
		}
		j = 0
		for {
			if !(j < (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpEList)).FnExpr) {
				break
			}
			if _sqlite3ExprCompare(tls, uintptr(0), pE, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpEList + 8 + uintptr(j)*20))).FpExpr, -int32(1)) == 0 {
				/* Since this expression is being changed into a reference
				 ** to an identical expression in the result set, remove all Window
				 ** objects belonging to the expression from the Select.pWin list. */
				_windowRemoveExprFromSelect(tls, pSelect, pE)
				(*(*struct {
					FiOrderByCol Tu16
					FiAlias      Tu16
				})(unsafe.Pointer(pItem + 16))).FiOrderByCol = uint16(j + int32(1))
			}
			goto _2
		_2:
			;
			j = j + 1
		}
		goto _1
	_1:
		;
		i = i + 1
		pItem += 20
	}
	return _sqlite3ResolveOrderGroupBy(tls, pParse, pSelect, pOrderBy, zType)
}

// C documentation
//
//	/*
//	** This routine is called after all opcodes have been inserted.  It loops
//	** through all the opcodes and fixes up some details.
//	**
//	** (1) For each jump instruction with a negative P2 value (a label)
//	**     resolve the P2 value to an actual address.
//	**
//	** (2) Compute the maximum number of arguments used by the xUpdate/xFilter
//	**     methods of any virtual table and store that value in *pMaxVtabArgs.
//	**
//	** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
//	**     indicate what the prepared statement actually does.
//	**
//	** (4) (discontinued)
//	**
//	** (5) Reclaim the memory allocated for storing labels.
//	**
//	** This routine will only function correctly if the mkopcodeh.tcl generator
//	** script numbers the opcodes correctly.  Changes to this routine must be
//	** coordinated with changes to mkopcodeh.tcl.
//	*/
func _resolveP2Values(tls *libc.TLS, p uintptr, pMaxVtabArgs uintptr) {
	var aLabel, pOp, pParse uintptr
	var n, nMaxVtabArgs int32
	_, _, _, _, _ = aLabel, n, nMaxVtabArgs, pOp, pParse
	nMaxVtabArgs = **(**int32)(__ccgo_up(pMaxVtabArgs))
	pParse = (*TVdbe)(unsafe.Pointer(p)).FpParse
	aLabel = (*TParse)(unsafe.Pointer(pParse)).FaLabel
	/* tag-20230419-1 */
	libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 6, 0x40)
	libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 7, 0x80)
	pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))*20
	for int32(1) != 0 {
		/* Only JUMP opcodes and the short list of special opcodes in the switch
		 ** below need to be considered.  The mkopcodeh.tcl generator script groups
		 ** all these opcodes together near the front of the opcode list.  Skip
		 ** any opcode that does not need processing by virtual of the fact that
		 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
		 */
		if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) <= int32(SQLITE_MX_JUMP_OPCODE) {
			/* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
			 ** cases from this switch! */
			switch int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) {
			case int32(OP_Transaction):
				if (*TOp)(unsafe.Pointer(pOp)).Fp2 != 0 {
					libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 6, 0x40)
				}
				fallthrough
			case int32(OP_AutoCommit):
				fallthrough
			case OP_Savepoint:
				libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 7, 0x80)
			case int32(OP_Checkpoint):
				fallthrough
			case int32(OP_Vacuum):
				fallthrough
			case int32(OP_JournalMode):
				libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 6, 0x40)
				libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 7, 0x80)
			case int32(OP_Init):
				goto resolve_p2_values_loop_exit
			case int32(OP_VUpdate):
				if (*TOp)(unsafe.Pointer(pOp)).Fp2 > nMaxVtabArgs {
					nMaxVtabArgs = (*TOp)(unsafe.Pointer(pOp)).Fp2
				}
			case int32(OP_VFilter):
				/* The instruction immediately prior to VFilter will be an
				 ** OP_Integer that sets the "argc" value for the VFilter.  See
				 ** the code where OP_VFilter is generated at tag-20250207a. */
				n = (**(**TOp)(__ccgo_up(pOp + uintptr(-libc.Int32FromInt32(1))*20))).Fp1
				if n > nMaxVtabArgs {
					nMaxVtabArgs = n
				}
				/* Fall through into the default case */
				fallthrough
			default:
				if (*TOp)(unsafe.Pointer(pOp)).Fp2 < 0 {
					/* The mkopcodeh.tcl script has so arranged things that the only
					 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
					 ** have non-negative values for P2. */
					/* True because of tag-20230419-1 */
					(*TOp)(unsafe.Pointer(pOp)).Fp2 = **(**int32)(__ccgo_up(aLabel + uintptr(^(*TOp)(unsafe.Pointer(pOp)).Fp2)*4))
				}
				/* OPFLG_JUMP opcodes never have P2==0, though OPFLG_JUMP0 opcodes
				 ** might */
				/* Jumps never go off the end of the bytecode array */
				break
			}
			/* The mkopcodeh.tcl script has so arranged things that the only
			 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
			 ** have non-negative values for P2. */
		}
		pOp -= 20
	}
	goto resolve_p2_values_loop_exit
resolve_p2_values_loop_exit:
	;
	if aLabel != 0 {
		_sqlite3DbNNFreeNN(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, (*TParse)(unsafe.Pointer(pParse)).FaLabel)
		(*TParse)(unsafe.Pointer(pParse)).FaLabel = uintptr(0)
	}
	(*TParse)(unsafe.Pointer(pParse)).FnLabel = 0
	**(**int32)(__ccgo_up(pMaxVtabArgs)) = nMaxVtabArgs
}

// C documentation
//
//	/*
//	** Walker callback for windowRemoveExprFromSelect().
//	*/
func _resolveRemoveWindowsCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var pWin uintptr
	_ = pWin
	_ = pWalker
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
		pWin = *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy))
		_sqlite3WindowUnlinkFromSelect(tls, pWin)
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Resolve names in the SELECT statement p and all of its descendants.
//	*/
func _resolveSelectStep(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var db, pGroupBy, pItem, pItem1, pItem2, pLeftmost, pOuterNC, pParse, pSub, pSub1, pWin, zSavedContext uintptr
	var i, isCompound, nCompound, nRef, v1 int32
	var _ /* sNC at bp+0 */ TNameContext
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, isCompound, nCompound, nRef, pGroupBy, pItem, pItem1, pItem2, pLeftmost, pOuterNC, pParse, pSub, pSub1, pWin, zSavedContext, v1 /* Database connection */
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Resolved) != 0 {
		return int32(WRC_Prune)
	}
	pOuterNC = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	/* Normally sqlite3SelectExpand() will be called first and will have
	 ** already expanded this SELECT.  However, if this is a subquery within
	 ** an expression, sqlite3ResolveExprNames() will be called without a
	 ** prior call to sqlite3SelectExpand().  When that happens, let
	 ** sqlite3SelectPrep() do all of the processing for this SELECT.
	 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
	 ** this routine in the correct order.
	 */
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Expanded) == uint32(0) {
		_sqlite3SelectPrep(tls, pParse, p, pOuterNC)
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			v1 = int32(WRC_Abort)
		} else {
			v1 = int32(WRC_Prune)
		}
		return v1
	}
	isCompound = libc.BoolInt32((*TSelect)(unsafe.Pointer(p)).FpPrior != uintptr(0))
	nCompound = 0
	pLeftmost = p
	for p != 0 {
		**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_Resolved)
		/* Resolve the expressions in the LIMIT and OFFSET clauses. These
		 ** are not allowed to refer to any names, so pass an empty NameContext.
		 */
		libc.Xmemset(tls, bp, 0, uint32(36))
		(**(**TNameContext)(__ccgo_up(bp))).FpParse = pParse
		(**(**TNameContext)(__ccgo_up(bp))).FpWinSelect = p
		if _sqlite3ResolveExprNames(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpLimit) != 0 {
			return int32(WRC_Abort)
		}
		/* If the SF_Converted flags is set, then this Select object was
		 ** was created by the convertCompoundSelectToSubquery() function.
		 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
		 ** as if it were part of the sub-query, not the parent. This block
		 ** moves the pOrderBy down to the sub-query. It will be moved back
		 ** after the names have been resolved.  */
		if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Converted) != 0 {
			pSub = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8 + 44)))).FpSelect
			(*TSelect)(unsafe.Pointer(pSub)).FpOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy
			(*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0)
		}
		/* Recursively resolve names in all subqueries in the FROM clause
		 */
		if pOuterNC != 0 {
			(*TNameContext)(unsafe.Pointer(pOuterNC)).FnNestedSelect = (*TNameContext)(unsafe.Pointer(pOuterNC)).FnNestedSelect + 1
		}
		i = 0
		for {
			if !(i < (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc) {
				break
			}
			pItem = (*TSelect)(unsafe.Pointer(p)).FpSrc + 8 + uintptr(i)*48
			/* Test of tag-20240424-1*/
			if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4>>2) != 0 && (*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 44)))).FpSelect)).FselFlags&uint32(SF_Resolved) == uint32(0) {
				if pOuterNC != 0 {
					v1 = (*TNameContext)(unsafe.Pointer(pOuterNC)).FnRef
				} else {
					v1 = 0
				}
				nRef = v1
				zSavedContext = (*TParse)(unsafe.Pointer(pParse)).FzAuthContext
				if (*TSrcItem)(unsafe.Pointer(pItem)).FzName != 0 {
					(*TParse)(unsafe.Pointer(pParse)).FzAuthContext = (*TSrcItem)(unsafe.Pointer(pItem)).FzName
				}
				_sqlite3ResolveSelectNames(tls, pParse, (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 44)))).FpSelect, pOuterNC)
				(*TParse)(unsafe.Pointer(pParse)).FzAuthContext = zSavedContext
				if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
					return int32(WRC_Abort)
				}
				/* If the number of references to the outer context changed when
				 ** expressions in the sub-select were resolved, the sub-select
				 ** is correlated. It is not required to check the refcount on any
				 ** but the innermost outer context object, as lookupName() increments
				 ** the refcount on all contexts between the current one and the
				 ** context containing the column when it resolves a name. */
				if pOuterNC != 0 {
					libc.SetBitFieldPtr32Uint32(pItem+12+4, libc.BoolUint32((*TNameContext)(unsafe.Pointer(pOuterNC)).FnRef > nRef), 4, 0x10)
				}
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		if pOuterNC != 0 && (*TNameContext)(unsafe.Pointer(pOuterNC)).FnNestedSelect > uint32(0) {
			(*TNameContext)(unsafe.Pointer(pOuterNC)).FnNestedSelect = (*TNameContext)(unsafe.Pointer(pOuterNC)).FnNestedSelect - 1
		}
		/* Set up the local name-context to pass to sqlite3ResolveExprNames() to
		 ** resolve the result-set expression list.
		 */
		(**(**TNameContext)(__ccgo_up(bp))).FncFlags = libc.Int32FromInt32(NC_AllowAgg) | libc.Int32FromInt32(NC_AllowWin)
		(**(**TNameContext)(__ccgo_up(bp))).FpSrcList = (*TSelect)(unsafe.Pointer(p)).FpSrc
		(**(**TNameContext)(__ccgo_up(bp))).FpNext = pOuterNC
		/* Resolve names in the result set. */
		if _sqlite3ResolveExprListNames(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpEList) != 0 {
			return int32(WRC_Abort)
		}
		(**(**TNameContext)(__ccgo_up(bp))).FncFlags &= ^libc.Int32FromInt32(NC_AllowWin)
		/* If there are no aggregate functions in the result-set, and no GROUP BY
		 ** expression, do not allow aggregates in any of the other expressions.
		 */
		pGroupBy = (*TSelect)(unsafe.Pointer(p)).FpGroupBy
		if pGroupBy != 0 || (**(**TNameContext)(__ccgo_up(bp))).FncFlags&int32(NC_HasAgg) != 0 {
			**(**Tu32)(__ccgo_up(p + 4)) |= uint32(int32(SF_Aggregate) | (**(**TNameContext)(__ccgo_up(bp))).FncFlags&(libc.Int32FromInt32(NC_MinMaxAgg)|libc.Int32FromInt32(NC_OrderAgg)))
		} else {
			(**(**TNameContext)(__ccgo_up(bp))).FncFlags &= ^libc.Int32FromInt32(NC_AllowAgg)
		}
		/* Add the output column list to the name-context before parsing the
		 ** other expressions in the SELECT statement. This is so that
		 ** expressions in the WHERE clause (etc.) can refer to expressions by
		 ** aliases in the result set.
		 **
		 ** Minor point: If this is the case, then the expression will be
		 ** re-evaluated for each reference to it.
		 */
		*(*uintptr)(unsafe.Pointer(bp + 8)) = (*TSelect)(unsafe.Pointer(p)).FpEList
		(**(**TNameContext)(__ccgo_up(bp))).FncFlags |= int32(NC_UEList)
		if (*TSelect)(unsafe.Pointer(p)).FpHaving != 0 {
			if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) == uint32(0) {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8974, 0)
				return int32(WRC_Abort)
			}
			if _sqlite3ResolveExprNames(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpHaving) != 0 {
				return int32(WRC_Abort)
			}
		}
		(**(**TNameContext)(__ccgo_up(bp))).FncFlags |= int32(NC_Where)
		if _sqlite3ResolveExprNames(tls, bp, (*TSelect)(unsafe.Pointer(p)).FpWhere) != 0 {
			return int32(WRC_Abort)
		}
		(**(**TNameContext)(__ccgo_up(bp))).FncFlags &= ^libc.Int32FromInt32(NC_Where)
		/* Resolve names in table-valued-function arguments */
		i = 0
		for {
			if !(i < (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc) {
				break
			}
			pItem1 = (*TSelect)(unsafe.Pointer(p)).FpSrc + 8 + uintptr(i)*48
			if int32(*(*uint32)(unsafe.Pointer(pItem1 + 12 + 4))&0x8>>3) != 0 && _sqlite3ResolveExprListNames(tls, bp, *(*uintptr)(unsafe.Pointer(pItem1 + 32))) != 0 {
				return int32(WRC_Abort)
			}
			goto _4
		_4:
			;
			i = i + 1
		}
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
			pWin = (*TSelect)(unsafe.Pointer(p)).FpWinDefn
			for {
				if !(pWin != 0) {
					break
				}
				if _sqlite3ResolveExprListNames(tls, bp, (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy) != 0 || _sqlite3ResolveExprListNames(tls, bp, (*TWindow)(unsafe.Pointer(pWin)).FpPartition) != 0 {
					return int32(WRC_Abort)
				}
				goto _5
			_5:
				;
				pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin
			}
		}
		(**(**TNameContext)(__ccgo_up(bp))).FncFlags |= libc.Int32FromInt32(NC_AllowAgg) | libc.Int32FromInt32(NC_AllowWin)
		/* If this is a converted compound query, move the ORDER BY clause from
		 ** the sub-query back to the parent query. At this point each term
		 ** within the ORDER BY clause has been transformed to an integer value.
		 ** These integers will be replaced by copies of the corresponding result
		 ** set expressions by the call to resolveOrderGroupBy() below.  */
		if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Converted) != 0 {
			pSub1 = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8 + 44)))).FpSelect
			(*TSelect)(unsafe.Pointer(p)).FpOrderBy = (*TSelect)(unsafe.Pointer(pSub1)).FpOrderBy
			(*TSelect)(unsafe.Pointer(pSub1)).FpOrderBy = uintptr(0)
		}
		/* Process the ORDER BY clause for singleton SELECT statements.
		 ** The ORDER BY clause for compounds SELECT statements is handled
		 ** below, after all of the result-sets for all of the elements of
		 ** the compound have been resolved.
		 **
		 ** If there is an ORDER BY clause on a term of a compound-select other
		 ** than the right-most term, then that is a syntax error.  But the error
		 ** is not detected until much later, and so we need to go ahead and
		 ** resolve those symbols on the incorrect ORDER BY for consistency.
		 */
		if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != uintptr(0) && isCompound <= nCompound && _resolveOrderGroupBy(tls, bp, p, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, __ccgo_ts+8876) != 0 {
			return int32(WRC_Abort)
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			return int32(WRC_Abort)
		}
		(**(**TNameContext)(__ccgo_up(bp))).FncFlags &= ^libc.Int32FromInt32(NC_AllowWin)
		/* Resolve the GROUP BY clause.  At the same time, make sure
		 ** the GROUP BY clause does not contain aggregate functions.
		 */
		if pGroupBy != 0 {
			if _resolveOrderGroupBy(tls, bp, p, pGroupBy, __ccgo_ts+9013) != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				return int32(WRC_Abort)
			}
			i = 0
			pItem2 = pGroupBy + 8
			for {
				if !(i < (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr) {
					break
				}
				if (*TExpr)(unsafe.Pointer((*TExprList_item)(unsafe.Pointer(pItem2)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Agg)) != uint32(0) {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9019, 0)
					return int32(WRC_Abort)
				}
				goto _6
			_6:
				;
				i = i + 1
				pItem2 += 20
			}
		}
		/* If this is part of a compound SELECT, check that it has the right
		 ** number of expressions in the select list. */
		if (*TSelect)(unsafe.Pointer(p)).FpNext != 0 && (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr != (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpNext)).FpEList)).FnExpr {
			_sqlite3SelectWrongNumTermsError(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpNext)
			return int32(WRC_Abort)
		}
		/* If the SELECT statement contains ON clauses that were moved into
		 ** the WHERE clause, go through and verify that none of the terms
		 ** in the ON clauses reference tables to the right of the ON clause. */
		if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_OnToWhere) != 0 {
			_sqlite3SelectCheckOnClauses(tls, pParse, p)
			if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
				return int32(WRC_Abort)
			}
		}
		/* Advance to the next term of the compound
		 */
		p = (*TSelect)(unsafe.Pointer(p)).FpPrior
		nCompound = nCompound + 1
	}
	/* Resolve the ORDER BY on a compound SELECT after all terms of
	 ** the compound have been resolved.
	 */
	if isCompound != 0 && _resolveCompoundOrderBy(tls, pParse, pLeftmost) != 0 {
		return int32(WRC_Abort)
	}
	return int32(WRC_Prune)
}

// C documentation
//
//	/*
//	** Set the EP_SubtArg property on every expression inside of
//	** pList.  If any subexpression is actually a subquery, then
//	** also set the EP_SubtArg property on the first result-set
//	** column of that subquery.
//	*/
func _resolveSetExprSubtypeArg(tls *libc.TLS, pList uintptr) {
	var ii, nn, v1 int32
	var pExpr uintptr
	_, _, _, _ = ii, nn, pExpr, v1
	if pList != 0 {
		v1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr
	} else {
		v1 = 0
	}
	nn = v1
	ii = 0
	for {
		if !(ii < nn) {
			break
		}
		pExpr = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr
		**(**Tu32)(__ccgo_up(pExpr + 4)) |= libc.Uint32FromUint32(EP_SubtArg)
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_SELECT) {
			_resolveSetExprSubtypeArg(tls, (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList)
		}
		goto _2
	_2:
		;
		ii = ii + 1
	}
}

// C documentation
//
//	/*
//	** Implementation of the round() function
//	*/
func _roundFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var n Ti64
	var zBuf uintptr
	var v1 float64
	var _ /* r at bp+0 */ float64
	_, _, _ = n, zBuf, v1
	n = 0
	if argc == int32(2) {
		if int32(SQLITE_NULL) == Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 1*4))) {
			return
		}
		n = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
		if n > int64(30) {
			n = int64(30)
		}
		if n < 0 {
			n = 0
		}
	}
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv))) == int32(SQLITE_NULL) {
		return
	}
	**(**float64)(__ccgo_up(bp)) = Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv)))
	/* If Y==0 and X will fit in a 64-bit int,
	 ** handle the rounding directly,
	 ** otherwise use printf.
	 */
	if **(**float64)(__ccgo_up(bp)) < -libc.Float64FromFloat64(4.503599627370496e+15) || **(**float64)(__ccgo_up(bp)) > +libc.Float64FromFloat64(4.503599627370496e+15) {
		/* The value has no fractional part so there is nothing to round */
	} else {
		if n == 0 {
			if **(**float64)(__ccgo_up(bp)) < libc.Float64FromInt32(0) {
				v1 = -libc.Float64FromFloat64(0.5)
			} else {
				v1 = +libc.Float64FromFloat64(0.5)
			}
			**(**float64)(__ccgo_up(bp)) = float64(int64(**(**float64)(__ccgo_up(bp)) + v1))
		} else {
			zBuf = Xsqlite3_mprintf(tls, __ccgo_ts+17697, libc.VaList(bp+16, int32(n), **(**float64)(__ccgo_up(bp))))
			if zBuf == uintptr(0) {
				Xsqlite3_result_error_nomem(tls, context)
				return
			}
			_sqlite3AtoF(tls, zBuf, bp)
			Xsqlite3_free(tls, zBuf)
		}
	}
	Xsqlite3_result_double(tls, context, **(**float64)(__ccgo_up(bp)))
}

// C documentation
//
//	/*
//	** Allocate a new RowSetEntry object that is associated with the
//	** given RowSet.  Return a pointer to the new and completely uninitialized
//	** object.
//	**
//	** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
//	** routine returns NULL.
//	*/
func _rowSetEntryAlloc(tls *libc.TLS, p uintptr) (r uintptr) {
	var pNew, v1, v2 uintptr
	_, _, _ = pNew, v1, v2
	if int32((*TRowSet)(unsafe.Pointer(p)).FnFresh) == 0 {
		pNew = _sqlite3DbMallocRawNN(tls, (*TRowSet)(unsafe.Pointer(p)).Fdb, uint64(1016))
		if pNew == uintptr(0) {
			return uintptr(0)
		}
		(*TRowSetChunk)(unsafe.Pointer(pNew)).FpNextChunk = (*TRowSet)(unsafe.Pointer(p)).FpChunk
		(*TRowSet)(unsafe.Pointer(p)).FpChunk = pNew
		(*TRowSet)(unsafe.Pointer(p)).FpFresh = pNew + 8
		(*TRowSet)(unsafe.Pointer(p)).FnFresh = uint16(uint32(libc.Int32FromInt32(ROWSET_ALLOCATION_SIZE)-libc.Int32FromInt32(8)) / libc.Uint32FromInt64(16))
	}
	(*TRowSet)(unsafe.Pointer(p)).FnFresh = (*TRowSet)(unsafe.Pointer(p)).FnFresh - 1
	v2 = p + 16
	v1 = *(*uintptr)(unsafe.Pointer(v2))
	*(*uintptr)(unsafe.Pointer(v2)) += 16
	return v1
}

// C documentation
//
//	/*
//	** Rtree virtual table module xBestIndex method. There are three
//	** table scan strategies to choose from (in order from most to
//	** least desirable):
//	**
//	**   idxNum     idxStr        Strategy
//	**   ------------------------------------------------
//	**     1        Unused        Direct lookup by rowid.
//	**     2        See below     R-tree query or full-table scan.
//	**   ------------------------------------------------
//	**
//	** If strategy 1 is used, then idxStr is not meaningful. If strategy
//	** 2 is used, idxStr is formatted to contain 2 bytes for each
//	** constraint used. The first two bytes of idxStr correspond to
//	** the constraint in sqlite3_index_info.aConstraintUsage[] with
//	** (argvIndex==1) etc.
//	**
//	** The first of each pair of bytes in idxStr identifies the constraint
//	** operator as follows:
//	**
//	**   Operator    Byte Value
//	**   ----------------------
//	**      =        0x41 ('A')
//	**     <=        0x42 ('B')
//	**      <        0x43 ('C')
//	**     >=        0x44 ('D')
//	**      >        0x45 ('E')
//	**   MATCH       0x46 ('F')
//	**   ----------------------
//	**
//	** The second of each pair of bytes identifies the coordinate column
//	** to which the constraint applies. The leftmost coordinate column
//	** is 'a', the second from the left 'b' etc.
//	*/
func _rtreeBestIndex(tls *libc.TLS, tab uintptr, pIdxInfo uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var bMatch, iIdx, ii, jj, rc, v4 int32
	var doOmit, op Tu8
	var nRow Ti64
	var p, pRtree uintptr
	var _ /* zIdxStr at bp+0 */ [41]int8
	_, _, _, _, _, _, _, _, _, _, _ = bMatch, doOmit, iIdx, ii, jj, nRow, op, p, pRtree, rc, v4
	pRtree = tab
	rc = SQLITE_OK
	bMatch = 0 /* Estimated rows returned by this scan */
	iIdx = 0
	libc.Xmemset(tls, bp, 0, uint32(41))
	/* Check if there exists a MATCH constraint - even an unusable one. If there
	 ** is, do not consider the lookup-by-rowid plan as using such a plan would
	 ** require the VDBE to evaluate the MATCH constraint, which is not currently
	 ** possible. */
	ii = 0
	for {
		if !(ii < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint) {
			break
		}
		if int32((**(**Tsqlite3_index_constraint)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(ii)*12))).Fop) == int32(SQLITE_INDEX_CONSTRAINT_MATCH) {
			bMatch = int32(1)
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	ii = 0
	for {
		if !(ii < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint && iIdx < int32(libc.Uint32FromInt64(41)-libc.Uint32FromInt32(1))) {
			break
		}
		p = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(ii)*12
		if bMatch == 0 && (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fusable != 0 && (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn <= 0 && int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_EQ) {
			jj = 0
			for {
				if !(jj < ii) {
					break
				}
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(jj)*8))).FargvIndex = 0
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(jj)*8))).Fomit = uint8(0)
				goto _3
			_3:
				;
				jj = jj + 1
			}
			(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = int32(1)
			(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(ii)*8))).FargvIndex = int32(1)
			(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(jj)*8))).Fomit = uint8(1)
			/* This strategy involves a two rowid lookups on an B-Tree structures
			 ** and then a linear search of an R-Tree node. This should be
			 ** considered almost as quick as a direct rowid lookup (for which
			 ** sqlite uses an internal cost of 0.0). It is expected to return
			 ** a single row.
			 */
			(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(30)
			(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(1)
			(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxFlags = int32(SQLITE_INDEX_SCAN_UNIQUE)
			return SQLITE_OK
		}
		if (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fusable != 0 && ((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn > 0 && (*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn <= int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) || int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_MATCH)) {
			doOmit = uint8(1)
			switch int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) {
			case int32(SQLITE_INDEX_CONSTRAINT_EQ):
				op = uint8(RTREE_EQ)
				doOmit = uint8(0)
			case int32(SQLITE_INDEX_CONSTRAINT_GT):
				op = uint8(RTREE_GT)
				doOmit = uint8(0)
			case int32(SQLITE_INDEX_CONSTRAINT_LE):
				op = uint8(RTREE_LE)
			case int32(SQLITE_INDEX_CONSTRAINT_LT):
				op = uint8(RTREE_LT)
				doOmit = uint8(0)
			case int32(SQLITE_INDEX_CONSTRAINT_GE):
				op = uint8(RTREE_GE)
			case int32(SQLITE_INDEX_CONSTRAINT_MATCH):
				op = uint8(RTREE_MATCH)
			default:
				op = uint8(0)
				break
			}
			if op != 0 {
				v4 = iIdx
				iIdx = iIdx + 1
				(**(**[41]int8)(__ccgo_up(bp)))[v4] = int8(op)
				v4 = iIdx
				iIdx = iIdx + 1
				(**(**[41]int8)(__ccgo_up(bp)))[v4] = int8((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).FiColumn - libc.Int32FromInt32(1) + libc.Int32FromUint8('0'))
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(ii)*8))).FargvIndex = iIdx / int32(2)
				(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(ii)*8))).Fomit = doOmit
			}
		}
		goto _2
	_2:
		;
		ii = ii + 1
	}
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = int32(2)
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FneedToFreeIdxStr = int32(1)
	if iIdx > 0 {
		(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr = Xsqlite3_malloc(tls, iIdx+int32(1))
		if (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemcpy(tls, (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr, bp, uint32(iIdx+int32(1)))
	}
	nRow = (*TRtree)(unsafe.Pointer(pRtree)).FnRowEst >> (iIdx / int32(2))
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(libc.Float64FromFloat64(6) * float64(nRow))
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = nRow
	return rc
}

// C documentation
//
//	/*
//	** This function is a no-op if there is already an error code stored
//	** in the RtreeCheck object indicated by the first argument. NULL is
//	** returned in this case.
//	**
//	** Otherwise, the contents of rtree table node iNode are loaded from
//	** the database and copied into a buffer obtained from sqlite3_malloc().
//	** If no error occurs, a pointer to the buffer is returned and (*pnNode)
//	** is set to the size of the buffer in bytes.
//	**
//	** Or, if an error does occur, NULL is returned and an error code left
//	** in the RtreeCheck object. The final value of *pnNode is undefined in
//	** this case.
//	*/
func _rtreeCheckGetNode(tls *libc.TLS, pCheck uintptr, iNode Ti64, pnNode uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var nNode int32
	var pNode, pRet uintptr
	_, _, _ = nNode, pNode, pRet
	pRet = uintptr(0) /* Return value */
	if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK && (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode == uintptr(0) {
		(*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode = _rtreeCheckPrepare(tls, pCheck, __ccgo_ts+30299, libc.VaList(bp+8, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzDb, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzTab))
	}
	if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK {
		Xsqlite3_bind_int64(tls, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode, int32(1), iNode)
		if Xsqlite3_step(tls, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode) == int32(SQLITE_ROW) {
			nNode = Xsqlite3_column_bytes(tls, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode, 0)
			pNode = Xsqlite3_column_blob(tls, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode, 0)
			pRet = Xsqlite3_malloc64(tls, uint64(nNode))
			if pRet == uintptr(0) {
				(*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc = int32(SQLITE_NOMEM)
			} else {
				libc.Xmemcpy(tls, pRet, pNode, uint32(nNode))
				**(**int32)(__ccgo_up(pnNode)) = nNode
			}
		}
		_rtreeCheckReset(tls, pCheck, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FpGetNode)
		if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc == SQLITE_OK && pRet == uintptr(0) {
			_rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+30344, libc.VaList(bp+8, iNode))
		}
	}
	return pRet
}

// C documentation
//
//	/*
//	** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
//	** (if bLeaf==1) table contains a specified entry. The schemas of the
//	** two tables are:
//	**
//	**   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
//	**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
//	**
//	** In both cases, this function checks that there exists an entry with
//	** IPK value iKey and the second column set to iVal.
//	**
//	*/
func _rtreeCheckMapping(tls *libc.TLS, pCheck uintptr, bLeaf int32, iKey Ti64, iVal Ti64) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var azSql [2]uintptr
	var ii Ti64
	var pStmt, v1 uintptr
	var rc int32
	_, _, _, _, _ = azSql, ii, pStmt, rc, v1
	azSql = [2]uintptr{
		0: __ccgo_ts + 30376,
		1: __ccgo_ts + 30430,
	}
	if **(**uintptr)(__ccgo_up(pCheck + 24 + uintptr(bLeaf)*4)) == uintptr(0) {
		**(**uintptr)(__ccgo_up(pCheck + 24 + uintptr(bLeaf)*4)) = _rtreeCheckPrepare(tls, pCheck, azSql[bLeaf], libc.VaList(bp+8, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzDb, (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzTab))
	}
	if (*TRtreeCheck)(unsafe.Pointer(pCheck)).Frc != SQLITE_OK {
		return
	}
	pStmt = **(**uintptr)(__ccgo_up(pCheck + 24 + uintptr(bLeaf)*4))
	Xsqlite3_bind_int64(tls, pStmt, int32(1), iKey)
	rc = Xsqlite3_step(tls, pStmt)
	if rc == int32(SQLITE_DONE) {
		if bLeaf != 0 {
			v1 = __ccgo_ts + 30478
		} else {
			v1 = __ccgo_ts + 30486
		}
		_rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+30495, libc.VaList(bp+8, iKey, iVal, v1))
	} else {
		if rc == int32(SQLITE_ROW) {
			ii = Xsqlite3_column_int64(tls, pStmt, 0)
			if ii != iVal {
				if bLeaf != 0 {
					v1 = __ccgo_ts + 30478
				} else {
					v1 = __ccgo_ts + 30486
				}
				_rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+30540, libc.VaList(bp+8, iKey, ii, v1, iKey, iVal))
			}
		}
	}
	_rtreeCheckReset(tls, pCheck, pStmt)
}

// C documentation
//
//	/*
//	** This function does the bulk of the work for the rtree integrity-check.
//	** It is called by rtreecheck(), which is the SQL function implementation.
//	*/
func _rtreeCheckTable(tls *libc.TLS, db uintptr, zDb uintptr, zTab uintptr, pzReport uintptr) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var nAux, rc int32
	var pStmt uintptr
	var _ /* check at bp+0 */ TRtreeCheck
	_, _, _ = nAux, pStmt, rc /* Common context for various routines */
	pStmt = uintptr(0)        /* Used to find column count of rtree table */
	nAux = 0                  /* Number of extra columns. */
	/* Initialize the context object */
	libc.Xmemset(tls, bp, 0, uint32(52))
	(**(**TRtreeCheck)(__ccgo_up(bp))).Fdb = db
	(**(**TRtreeCheck)(__ccgo_up(bp))).FzDb = zDb
	(**(**TRtreeCheck)(__ccgo_up(bp))).FzTab = zTab
	/* Find the number of auxiliary columns */
	pStmt = _rtreeCheckPrepare(tls, bp, __ccgo_ts+30930, libc.VaList(bp+64, zDb, zTab))
	if pStmt != 0 {
		nAux = Xsqlite3_column_count(tls, pStmt) - int32(2)
		Xsqlite3_finalize(tls, pStmt)
	} else {
		if (**(**TRtreeCheck)(__ccgo_up(bp))).Frc != int32(SQLITE_NOMEM) {
			(**(**TRtreeCheck)(__ccgo_up(bp))).Frc = SQLITE_OK
		}
	}
	/* Find number of dimensions in the rtree table. */
	pStmt = _rtreeCheckPrepare(tls, bp, __ccgo_ts+28746, libc.VaList(bp+64, zDb, zTab))
	if pStmt != 0 {
		(**(**TRtreeCheck)(__ccgo_up(bp))).FnDim = (Xsqlite3_column_count(tls, pStmt) - int32(1) - nAux) / int32(2)
		if (**(**TRtreeCheck)(__ccgo_up(bp))).FnDim < int32(1) {
			_rtreeCheckAppendMsg(tls, bp, __ccgo_ts+30958, 0)
		} else {
			if int32(SQLITE_ROW) == Xsqlite3_step(tls, pStmt) {
				(**(**TRtreeCheck)(__ccgo_up(bp))).FbInt = libc.BoolInt32(Xsqlite3_column_type(tls, pStmt, int32(1)) == int32(SQLITE_INTEGER))
			}
		}
		rc = Xsqlite3_finalize(tls, pStmt)
		if rc != int32(SQLITE_CORRUPT) {
			(**(**TRtreeCheck)(__ccgo_up(bp))).Frc = rc
		}
	}
	/* Do the actual integrity-check */
	if (**(**TRtreeCheck)(__ccgo_up(bp))).FnDim >= int32(1) {
		if (**(**TRtreeCheck)(__ccgo_up(bp))).Frc == SQLITE_OK {
			_rtreeCheckNode(tls, bp, 0, uintptr(0), int64(1))
		}
		_rtreeCheckCount(tls, bp, __ccgo_ts+30989, int64((**(**TRtreeCheck)(__ccgo_up(bp))).FnLeaf))
		_rtreeCheckCount(tls, bp, __ccgo_ts+30996, int64((**(**TRtreeCheck)(__ccgo_up(bp))).FnNonLeaf))
	}
	/* Finalize SQL statements used by the integrity-check */
	Xsqlite3_finalize(tls, (**(**TRtreeCheck)(__ccgo_up(bp))).FpGetNode)
	Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 24)))
	Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 24 + 1*4)))
	**(**uintptr)(__ccgo_up(pzReport)) = (**(**TRtreeCheck)(__ccgo_up(bp))).FzReport
	return (**(**TRtreeCheck)(__ccgo_up(bp))).Frc
}

// C documentation
//
//	/*
//	** Rtree virtual table module xColumn method.
//	*/
func _rtreeColumn(tls *libc.TLS, cur uintptr, ctx uintptr, i int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var p, pCsr, pNode, pRtree uintptr
	var _ /* c at bp+0 */ TRtreeCoord
	var _ /* rc at bp+4 */ int32
	_, _, _, _ = p, pCsr, pNode, pRtree
	pRtree = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(cur)).FpVtab
	pCsr = cur
	p = _rtreeSearchPointFirst(tls, pCsr)
	**(**int32)(__ccgo_up(bp + 4)) = SQLITE_OK
	pNode = _rtreeNodeOfFirstSearchPoint(tls, pCsr, bp+4)
	if **(**int32)(__ccgo_up(bp + 4)) != 0 {
		return **(**int32)(__ccgo_up(bp + 4))
	}
	if p == uintptr(0) {
		return SQLITE_OK
	}
	if int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell) >= _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2) {
		return int32(SQLITE_ABORT)
	}
	if i == 0 {
		Xsqlite3_result_int64(tls, ctx, _nodeGetRowid(tls, pRtree, pNode, int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell)))
	} else {
		if i <= int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) {
			_nodeGetCoord(tls, pRtree, pNode, int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell), i-int32(1), bp)
			if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
				Xsqlite3_result_double(tls, ctx, float64(*(*TRtreeValue)(unsafe.Pointer(bp))))
			} else {
				Xsqlite3_result_int(tls, ctx, *(*int32)(unsafe.Pointer(bp)))
			}
		} else {
			if !((*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid != 0) {
				if (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux == uintptr(0) {
					**(**int32)(__ccgo_up(bp + 4)) = Xsqlite3_prepare_v3(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb, (*TRtree)(unsafe.Pointer(pRtree)).FzReadAuxSql, -int32(1), uint32(0), pCsr+36, uintptr(0))
					if **(**int32)(__ccgo_up(bp + 4)) != 0 {
						return **(**int32)(__ccgo_up(bp + 4))
					}
				}
				Xsqlite3_bind_int64(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux, int32(1), _nodeGetRowid(tls, pRtree, pNode, int32((*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell)))
				**(**int32)(__ccgo_up(bp + 4)) = Xsqlite3_step(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux)
				if **(**int32)(__ccgo_up(bp + 4)) == int32(SQLITE_ROW) {
					(*TRtreeCursor)(unsafe.Pointer(pCsr)).FbAuxValid = uint8(1)
				} else {
					Xsqlite3_reset(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux)
					if **(**int32)(__ccgo_up(bp + 4)) == int32(SQLITE_DONE) {
						**(**int32)(__ccgo_up(bp + 4)) = SQLITE_OK
					}
					return **(**int32)(__ccgo_up(bp + 4))
				}
			}
			Xsqlite3_result_value(tls, ctx, Xsqlite3_column_value(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FpReadAux, i-int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)+int32(1)))
		}
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Push a new element onto the priority queue
//	*/
func _rtreeEnqueue(tls *libc.TLS, pCur uintptr, rScore TRtreeDValue, iLevel Tu8) (r uintptr) {
	var i, j, nNew, v1 int32
	var pNew, pParent, v2 uintptr
	_, _, _, _, _, _, _ = i, j, nNew, pNew, pParent, v1, v2
	if (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPoint >= (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPointAlloc {
		nNew = (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPointAlloc*int32(2) + int32(8)
		pNew = Xsqlite3_realloc64(tls, (*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint, uint64(uint32(nNew)*uint32(24)))
		if pNew == uintptr(0) {
			return uintptr(0)
		}
		(*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint = pNew
		(*TRtreeCursor)(unsafe.Pointer(pCur)).FnPointAlloc = nNew
	}
	v2 = pCur + 24
	v1 = *(*int32)(unsafe.Pointer(v2))
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	i = v1
	pNew = (*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint + uintptr(i)*24
	(*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FrScore = rScore
	(*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FiLevel = iLevel
	for i > 0 {
		j = (i - int32(1)) / int32(2)
		pParent = (*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint + uintptr(j)*24
		if _rtreeSearchPointCompare(tls, pNew, pParent) >= 0 {
			break
		}
		_rtreeSearchPointSwap(tls, pCur, j, i)
		i = j
		pNew = pParent
	}
	return pNew
}

// C documentation
//
//	/*
//	** Rtree virtual table module xFilter method.
//	*/
func _rtreeFilter(tls *libc.TLS, pVtabCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var eType, eType1, ii, rc int32
	var iRowid Ti64
	var iVal Tsqlite3_int64
	var p, p1, pCsr, pNew, pRtree uintptr
	var _ /* iCell at bp+4 */ int32
	var _ /* iNode at bp+16 */ Ti64
	var _ /* pLeaf at bp+8 */ uintptr
	var _ /* pRoot at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _ = eType, eType1, iRowid, iVal, ii, p, p1, pCsr, pNew, pRtree, rc
	pRtree = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVtabCursor)).FpVtab
	pCsr = pVtabCursor
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	rc = SQLITE_OK
	**(**int32)(__ccgo_up(bp + 4)) = 0
	_rtreeReference(tls, pRtree)
	/* Reset the cursor to the same state as rtreeOpen() leaves it in. */
	_resetCursor(tls, pCsr)
	(*TRtreeCursor)(unsafe.Pointer(pCsr)).FiStrategy = idxNum
	if idxNum == int32(1) { /* Search point for the leaf */
		iRowid = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv)))
		**(**Ti64)(__ccgo_up(bp + 16)) = 0
		eType = Xsqlite3_value_numeric_type(tls, **(**uintptr)(__ccgo_up(argv)))
		if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) && 0 == _sqlite3IntFloatCompare(tls, iRowid, Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv)))) {
			rc = _findLeafNode(tls, pRtree, iRowid, bp+8, bp+16)
		} else {
			rc = SQLITE_OK
			**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
		}
		if rc == SQLITE_OK && **(**uintptr)(__ccgo_up(bp + 8)) != uintptr(0) {
			p = _rtreeSearchPointNew(tls, pCsr, float64(0), uint8(0))
			/* Always returns pCsr->sPoint */
			**(**uintptr)(__ccgo_up(pCsr + 64)) = **(**uintptr)(__ccgo_up(bp + 8))
			(*TRtreeSearchPoint)(unsafe.Pointer(p)).Fid = **(**Ti64)(__ccgo_up(bp + 16))
			(*TRtreeSearchPoint)(unsafe.Pointer(p)).FeWithin = uint8(PARTLY_WITHIN)
			rc = _nodeRowidIndex(tls, pRtree, **(**uintptr)(__ccgo_up(bp + 8)), iRowid, bp+4)
			(*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell = uint8(**(**int32)(__ccgo_up(bp + 4)))
		} else {
			(*TRtreeCursor)(unsafe.Pointer(pCsr)).FatEOF = uint8(1)
		}
	} else {
		/* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
		 ** with the configured constraints.
		 */
		rc = _nodeAcquire(tls, pRtree, int64(1), uintptr(0), bp)
		if rc == SQLITE_OK && argc > 0 {
			(*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint = Xsqlite3_malloc64(tls, uint64(uint32(24)*uint32(argc)))
			(*TRtreeCursor)(unsafe.Pointer(pCsr)).FnConstraint = argc
			if !((*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint != 0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				libc.Xmemset(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint, 0, uint32(24)*uint32(argc))
				libc.Xmemset(tls, pCsr+84, 0, uint32(4)*uint32((*TRtree)(unsafe.Pointer(pRtree)).FiDepth+libc.Int32FromInt32(1)))
				ii = 0
				for {
					if !(ii < argc) {
						break
					}
					p1 = (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint + uintptr(ii)*24
					eType1 = Xsqlite3_value_numeric_type(tls, **(**uintptr)(__ccgo_up(argv + uintptr(ii)*4)))
					(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32(**(**int8)(__ccgo_up(idxStr + uintptr(ii*int32(2)))))
					(*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(**(**int8)(__ccgo_up(idxStr + uintptr(ii*int32(2)+int32(1))))) - int32('0')
					if (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop >= int32(RTREE_MATCH) {
						/* A MATCH operator. The right-hand-side must be a blob that
						 ** can be cast into an RtreeMatchArg object. One created using
						 ** an sqlite3_rtree_geometry_callback() SQL user function.
						 */
						rc = _deserializeGeometry(tls, **(**uintptr)(__ccgo_up(argv + uintptr(ii)*4)), p1)
						if rc != SQLITE_OK {
							break
						}
						(*Tsqlite3_rtree_query_info)(unsafe.Pointer((*TRtreeConstraint)(unsafe.Pointer(p1)).FpInfo)).FnCoord = int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)
						(*Tsqlite3_rtree_query_info)(unsafe.Pointer((*TRtreeConstraint)(unsafe.Pointer(p1)).FpInfo)).FanQueue = pCsr + 84
						(*Tsqlite3_rtree_query_info)(unsafe.Pointer((*TRtreeConstraint)(unsafe.Pointer(p1)).FpInfo)).FmxLevel = (*TRtree)(unsafe.Pointer(pRtree)).FiDepth + int32(1)
					} else {
						if eType1 == int32(SQLITE_INTEGER) {
							iVal = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + uintptr(ii)*4)))
							*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(iVal)
							if iVal >= libc.Int64FromInt32(1)<<libc.Int32FromInt32(48) || iVal <= -(libc.Int64FromInt32(1)<<libc.Int32FromInt32(48)) {
								if (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop == int32(RTREE_LT) {
									(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32(RTREE_LE)
								}
								if (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop == int32(RTREE_GT) {
									(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32(RTREE_GE)
								}
							}
						} else {
							if eType1 == int32(SQLITE_FLOAT) {
								*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv + uintptr(ii)*4)))
							} else {
								*(*TRtreeDValue)(unsafe.Pointer(p1 + 8)) = float64(0)
								if eType1 == int32(SQLITE_NULL) {
									(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32(RTREE_FALSE)
								} else {
									if (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop == int32(RTREE_LT) || (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop == int32(RTREE_LE) {
										(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32(RTREE_TRUE)
									} else {
										(*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32(RTREE_FALSE)
									}
								}
							}
						}
					}
					goto _1
				_1:
					;
					ii = ii + 1
				}
			}
		}
		if rc == SQLITE_OK {
			/* Due to the resetCursor() call above */
			pNew = _rtreeSearchPointNew(tls, pCsr, float64(0), uint8((*TRtree)(unsafe.Pointer(pRtree)).FiDepth+libc.Int32FromInt32(1)))
			if pNew == uintptr(0) { /* Because pCsr->bPoint was FALSE */
				return int32(SQLITE_NOMEM)
			}
			(*TRtreeSearchPoint)(unsafe.Pointer(pNew)).Fid = int64(1)
			(*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FiCell = uint8(0)
			(*TRtreeSearchPoint)(unsafe.Pointer(pNew)).FeWithin = uint8(PARTLY_WITHIN)
			**(**uintptr)(__ccgo_up(pCsr + 64)) = **(**uintptr)(__ccgo_up(bp))
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			rc = _rtreeStepToLeaf(tls, pCsr)
		}
	}
	_nodeRelease(tls, pRtree, **(**uintptr)(__ccgo_up(bp)))
	_rtreeRelease(tls, pRtree)
	return rc
}

// C documentation
//
//	/*
//	** This function is the implementation of both the xConnect and xCreate
//	** methods of the r-tree virtual table.
//	**
//	**   argv[0]   -> module name
//	**   argv[1]   -> database name
//	**   argv[2]   -> table name
//	**   argv[...] -> column names...
//	*/
func _rtreeInit(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr, isCreate int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aErrMsg [5]uintptr
	var eCoordType, iErr, ii, nDb, nName, rc, v1 int32
	var pRtree, pSql, zArg, zSql uintptr
	_, _, _, _, _, _, _, _, _, _, _, _ = aErrMsg, eCoordType, iErr, ii, nDb, nName, pRtree, pSql, rc, zArg, zSql, v1
	rc = SQLITE_OK
	if pAux != 0 {
		v1 = int32(RTREE_COORD_INT32)
	} else {
		v1 = RTREE_COORD_REAL32
	} /* Length of string argv[2] */
	eCoordType = v1
	ii = int32(4)
	aErrMsg = [5]uintptr{
		1: __ccgo_ts + 30038,
		2: __ccgo_ts + 30081,
		3: __ccgo_ts + 30116,
		4: __ccgo_ts + 30152,
	}
	if argc < int32(6) || argc > libc.Int32FromInt32(RTREE_MAX_AUX_COLUMN)+libc.Int32FromInt32(3) {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+8, aErrMsg[int32(2)+libc.BoolInt32(argc >= int32(6))]))
		return int32(SQLITE_ERROR)
	}
	Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_CONSTRAINT_SUPPORT), libc.VaList(bp+8, int32(1)))
	Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_INNOCUOUS), 0)
	/* Allocate the sqlite3_vtab structure */
	nDb = int32(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + 1*4))))
	nName = int32(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(argv + 2*4))))
	pRtree = Xsqlite3_malloc64(tls, uint64(uint32(512)+uint32(nDb)+uint32(nName*int32(2))+uint32(8)))
	if !(pRtree != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pRtree, 0, uint32(512)+uint32(nDb)+uint32(nName*int32(2))+uint32(8))
	(*TRtree)(unsafe.Pointer(pRtree)).FnBusy = uint32(1)
	(*TRtree)(unsafe.Pointer(pRtree)).Fbase.FpModule = uintptr(unsafe.Pointer(&_rtreeModule))
	(*TRtree)(unsafe.Pointer(pRtree)).FzDb = pRtree + 1*512
	(*TRtree)(unsafe.Pointer(pRtree)).FzName = (*TRtree)(unsafe.Pointer(pRtree)).FzDb + uintptr(nDb+int32(1))
	(*TRtree)(unsafe.Pointer(pRtree)).FzNodeName = (*TRtree)(unsafe.Pointer(pRtree)).FzName + uintptr(nName+int32(1))
	(*TRtree)(unsafe.Pointer(pRtree)).FeCoordType = uint8(eCoordType)
	libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, **(**uintptr)(__ccgo_up(argv + 1*4)), uint32(nDb))
	libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzName, **(**uintptr)(__ccgo_up(argv + 2*4)), uint32(nName))
	libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName, **(**uintptr)(__ccgo_up(argv + 2*4)), uint32(nName))
	libc.Xmemcpy(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName+uintptr(nName), __ccgo_ts+30189, uint32(6))
	/* Create/Connect to the underlying relational database schema. If
	 ** that is successful, call sqlite3_declare_vtab() to configure
	 ** the r-tree table schema.
	 */
	pSql = Xsqlite3_str_new(tls, db)
	Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+30195, libc.VaList(bp+8, _rtreeTokenLength(tls, **(**uintptr)(__ccgo_up(argv + 3*4))), **(**uintptr)(__ccgo_up(argv + 3*4))))
	ii = int32(4)
	for {
		if !(ii < argc) {
			break
		}
		zArg = **(**uintptr)(__ccgo_up(argv + uintptr(ii)*4))
		if int32(**(**int8)(__ccgo_up(zArg))) == int32('+') {
			(*TRtree)(unsafe.Pointer(pRtree)).FnAux = (*TRtree)(unsafe.Pointer(pRtree)).FnAux + 1
			Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+30219, libc.VaList(bp+8, _rtreeTokenLength(tls, zArg+uintptr(1)), zArg+uintptr(1)))
		} else {
			if int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux) > 0 {
				break
			} else {
				(*TRtree)(unsafe.Pointer(pRtree)).FnDim2 = (*TRtree)(unsafe.Pointer(pRtree)).FnDim2 + 1
				Xsqlite3_str_appendf(tls, pSql, _azFormat[eCoordType], libc.VaList(bp+8, _rtreeTokenLength(tls, zArg), zArg))
			}
		}
		goto _2
	_2:
		;
		ii = ii + 1
	}
	Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+30246, 0)
	zSql = Xsqlite3_str_finish(tls, pSql)
	if !(zSql != 0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		if ii < argc {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+8, aErrMsg[int32(4)]))
			rc = int32(SQLITE_ERROR)
		} else {
			v1 = Xsqlite3_declare_vtab(tls, db, zSql)
			rc = v1
			if SQLITE_OK != v1 {
				**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db)))
			}
		}
	}
	Xsqlite3_free(tls, zSql)
	if rc != 0 {
		goto rtreeInit_fail
	}
	(*TRtree)(unsafe.Pointer(pRtree)).FnDim = uint8(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) / int32(2))
	if int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim) < int32(1) {
		iErr = int32(2)
	} else {
		if int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) > libc.Int32FromInt32(RTREE_MAX_DIMENSIONS)*libc.Int32FromInt32(2) {
			iErr = int32(3)
		} else {
			if int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)%int32(2) != 0 {
				iErr = int32(1)
			} else {
				iErr = 0
			}
		}
	}
	if iErr != 0 {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+8, aErrMsg[iErr]))
		goto rtreeInit_fail
	}
	(*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell = uint8(int32(8) + int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)*int32(4))
	/* Figure out the node size to use. */
	rc = _getNodeSize(tls, db, pRtree, isCreate, pzErr)
	if rc != 0 {
		goto rtreeInit_fail
	}
	rc = _rtreeSqlInit(tls, pRtree, db, **(**uintptr)(__ccgo_up(argv + 1*4)), **(**uintptr)(__ccgo_up(argv + 2*4)), isCreate)
	if rc != 0 {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+8, Xsqlite3_errmsg(tls, db)))
		goto rtreeInit_fail
	}
	**(**uintptr)(__ccgo_up(ppVtab)) = pRtree
	return SQLITE_OK
	goto rtreeInit_fail
rtreeInit_fail:
	;
	if rc == SQLITE_OK {
		rc = int32(SQLITE_ERROR)
	}
	_rtreeRelease(tls, pRtree)
	return rc
}

// C documentation
//
//	/*
//	** Get the RtreeNode for the search point with the lowest score.
//	*/
func _rtreeNodeOfFirstSearchPoint(tls *libc.TLS, pCur uintptr, pRC uintptr) (r uintptr) {
	var id Tsqlite3_int64
	var ii int32
	var v1 int64
	_, _, _ = id, ii, v1
	ii = int32(1) - int32((*TRtreeCursor)(unsafe.Pointer(pCur)).FbPoint)
	if **(**uintptr)(__ccgo_up(pCur + 64 + uintptr(ii)*4)) == uintptr(0) {
		if ii != 0 {
			v1 = (**(**TRtreeSearchPoint)(__ccgo_up((*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint))).Fid
		} else {
			v1 = (*TRtreeCursor)(unsafe.Pointer(pCur)).FsPoint.Fid
		}
		id = v1
		**(**int32)(__ccgo_up(pRC)) = _nodeAcquire(tls, (*TRtreeCursor)(unsafe.Pointer(pCur)).Fbase.FpVtab, id, uintptr(0), pCur+64+uintptr(ii)*4)
	}
	return **(**uintptr)(__ccgo_up(pCur + 64 + uintptr(ii)*4))
}

// C documentation
//
//	/*
//	** Allocate a new RtreeSearchPoint and return a pointer to it.  Return
//	** NULL if malloc fails.
//	*/
func _rtreeSearchPointNew(tls *libc.TLS, pCur uintptr, rScore TRtreeDValue, iLevel Tu8) (r uintptr) {
	var ii int32
	var pFirst, pNew uintptr
	_, _, _ = ii, pFirst, pNew
	pFirst = _rtreeSearchPointFirst(tls, pCur)
	**(**Tu32)(__ccgo_up(pCur + 84 + uintptr(iLevel)*4)) = **(**Tu32)(__ccgo_up(pCur + 84 + uintptr(iLevel)*4)) + 1
	if pFirst == uintptr(0) || (*TRtreeSearchPoint)(unsafe.Pointer(pFirst)).FrScore > rScore || (*TRtreeSearchPoint)(unsafe.Pointer(pFirst)).FrScore == rScore && int32((*TRtreeSearchPoint)(unsafe.Pointer(pFirst)).FiLevel) > int32(iLevel) {
		if (*TRtreeCursor)(unsafe.Pointer(pCur)).FbPoint != 0 {
			pNew = _rtreeEnqueue(tls, pCur, rScore, iLevel)
			if pNew == uintptr(0) {
				return uintptr(0)
			}
			ii = (int32(pNew)-int32((*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint))/24 + int32(1)
			if ii < int32(RTREE_CACHE_SZ) {
				**(**uintptr)(__ccgo_up(pCur + 64 + uintptr(ii)*4)) = **(**uintptr)(__ccgo_up(pCur + 64))
			} else {
				_nodeRelease(tls, (*TRtreeCursor)(unsafe.Pointer(pCur)).Fbase.FpVtab, **(**uintptr)(__ccgo_up(pCur + 64)))
			}
			**(**uintptr)(__ccgo_up(pCur + 64)) = uintptr(0)
			**(**TRtreeSearchPoint)(__ccgo_up(pNew)) = (*TRtreeCursor)(unsafe.Pointer(pCur)).FsPoint
		}
		(*TRtreeCursor)(unsafe.Pointer(pCur)).FsPoint.FrScore = rScore
		(*TRtreeCursor)(unsafe.Pointer(pCur)).FsPoint.FiLevel = iLevel
		(*TRtreeCursor)(unsafe.Pointer(pCur)).FbPoint = uint8(1)
		return pCur + 40
	} else {
		return _rtreeEnqueue(tls, pCur, rScore, iLevel)
	}
	return r
}

// C documentation
//
//	/* Remove the search point with the lowest current score.
//	*/
func _rtreeSearchPointPop(tls *libc.TLS, p uintptr) {
	var i, j, k, n, v1 int32
	var v2 uintptr
	_, _, _, _, _, _ = i, j, k, n, v1, v2
	i = int32(1) - int32((*TRtreeCursor)(unsafe.Pointer(p)).FbPoint)
	if **(**uintptr)(__ccgo_up(p + 64 + uintptr(i)*4)) != 0 {
		_nodeRelease(tls, (*TRtreeCursor)(unsafe.Pointer(p)).Fbase.FpVtab, **(**uintptr)(__ccgo_up(p + 64 + uintptr(i)*4)))
		**(**uintptr)(__ccgo_up(p + 64 + uintptr(i)*4)) = uintptr(0)
	}
	if (*TRtreeCursor)(unsafe.Pointer(p)).FbPoint != 0 {
		**(**Tu32)(__ccgo_up(p + 84 + uintptr((*TRtreeCursor)(unsafe.Pointer(p)).FsPoint.FiLevel)*4)) = **(**Tu32)(__ccgo_up(p + 84 + uintptr((*TRtreeCursor)(unsafe.Pointer(p)).FsPoint.FiLevel)*4)) - 1
		(*TRtreeCursor)(unsafe.Pointer(p)).FbPoint = uint8(0)
	} else {
		if (*TRtreeCursor)(unsafe.Pointer(p)).FnPoint != 0 {
			**(**Tu32)(__ccgo_up(p + 84 + uintptr((**(**TRtreeSearchPoint)(__ccgo_up((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint))).FiLevel)*4)) = **(**Tu32)(__ccgo_up(p + 84 + uintptr((**(**TRtreeSearchPoint)(__ccgo_up((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint))).FiLevel)*4)) - 1
			v2 = p + 24
			*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) - 1
			v1 = *(*int32)(unsafe.Pointer(v2))
			n = v1
			**(**TRtreeSearchPoint)(__ccgo_up((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint)) = **(**TRtreeSearchPoint)(__ccgo_up((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint + uintptr(n)*24))
			if n < libc.Int32FromInt32(RTREE_CACHE_SZ)-libc.Int32FromInt32(1) {
				**(**uintptr)(__ccgo_up(p + 64 + 1*4)) = **(**uintptr)(__ccgo_up(p + 64 + uintptr(n+int32(1))*4))
				**(**uintptr)(__ccgo_up(p + 64 + uintptr(n+int32(1))*4)) = uintptr(0)
			}
			i = 0
			for {
				v1 = i*libc.Int32FromInt32(2) + libc.Int32FromInt32(1)
				j = v1
				if !(v1 < n) {
					break
				}
				k = j + int32(1)
				if k < n && _rtreeSearchPointCompare(tls, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(k)*24, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(j)*24) < 0 {
					if _rtreeSearchPointCompare(tls, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(k)*24, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(i)*24) < 0 {
						_rtreeSearchPointSwap(tls, p, i, k)
						i = k
					} else {
						break
					}
				} else {
					if _rtreeSearchPointCompare(tls, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(j)*24, (*TRtreeCursor)(unsafe.Pointer(p)).FaPoint+uintptr(i)*24) < 0 {
						_rtreeSearchPointSwap(tls, p, i, j)
						i = j
					} else {
						break
					}
				}
			}
		}
	}
}

func _rtreeSqlInit(tls *libc.TLS, pRtree uintptr, db uintptr, zDb uintptr, zPrefix uintptr, isCreate int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var appStmt [8]uintptr
	var f, i, ii, ii1, rc int32
	var p, p1, zCreate, zFormat, zSql, zSql1 uintptr
	_, _, _, _, _, _, _, _, _, _, _, _ = appStmt, f, i, ii, ii1, p, p1, rc, zCreate, zFormat, zSql, zSql1
	rc = SQLITE_OK
	f = libc.Int32FromInt32(SQLITE_PREPARE_PERSISTENT) | libc.Int32FromInt32(SQLITE_PREPARE_NO_VTAB)
	(*TRtree)(unsafe.Pointer(pRtree)).Fdb = db
	if isCreate != 0 {
		p = Xsqlite3_str_new(tls, db)
		Xsqlite3_str_appendf(tls, p, __ccgo_ts+29450, libc.VaList(bp+8, zDb, zPrefix))
		ii = 0
		for {
			if !(ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux)) {
				break
			}
			Xsqlite3_str_appendf(tls, p, __ccgo_ts+29512, libc.VaList(bp+8, ii))
			goto _1
		_1:
			;
			ii = ii + 1
		}
		Xsqlite3_str_appendf(tls, p, __ccgo_ts+29517, libc.VaList(bp+8, zDb, zPrefix))
		Xsqlite3_str_appendf(tls, p, __ccgo_ts+29581, libc.VaList(bp+8, zDb, zPrefix))
		Xsqlite3_str_appendf(tls, p, __ccgo_ts+29651, libc.VaList(bp+8, zDb, zPrefix, (*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize))
		zCreate = Xsqlite3_str_finish(tls, p)
		if !(zCreate != 0) {
			return int32(SQLITE_NOMEM)
		}
		rc = Xsqlite3_exec(tls, db, zCreate, uintptr(0), uintptr(0), uintptr(0))
		Xsqlite3_free(tls, zCreate)
		if rc != SQLITE_OK {
			return rc
		}
	}
	appStmt[0] = pRtree + 84
	appStmt[int32(1)] = pRtree + 88
	appStmt[int32(2)] = pRtree + 92
	appStmt[int32(3)] = pRtree + 96
	appStmt[int32(4)] = pRtree + 100
	appStmt[int32(5)] = pRtree + 104
	appStmt[int32(6)] = pRtree + 108
	appStmt[int32(7)] = pRtree + 112
	rc = _rtreeQueryStat1(tls, db, pRtree)
	i = 0
	for {
		if !(i < int32(N_STATEMENT) && rc == SQLITE_OK) {
			break
		}
		if i != int32(3) || int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux) == 0 {
			zFormat = _azSql[i]
		} else {
			/* An UPSERT is very slightly slower than REPLACE, but it is needed
			 ** if there are auxiliary columns */
			zFormat = __ccgo_ts + 29700
		}
		zSql = Xsqlite3_mprintf(tls, zFormat, libc.VaList(bp+8, zDb, zPrefix))
		if zSql != 0 {
			rc = Xsqlite3_prepare_v3(tls, db, zSql, -int32(1), uint32(f), appStmt[i], uintptr(0))
		} else {
			rc = int32(SQLITE_NOMEM)
		}
		Xsqlite3_free(tls, zSql)
		goto _2
	_2:
		;
		i = i + 1
	}
	if (*TRtree)(unsafe.Pointer(pRtree)).FnAux != 0 && rc != int32(SQLITE_NOMEM) {
		(*TRtree)(unsafe.Pointer(pRtree)).FzReadAuxSql = Xsqlite3_mprintf(tls, __ccgo_ts+29808, libc.VaList(bp+8, zDb, zPrefix))
		if (*TRtree)(unsafe.Pointer(pRtree)).FzReadAuxSql == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		} else {
			p1 = Xsqlite3_str_new(tls, db)
			Xsqlite3_str_appendf(tls, p1, __ccgo_ts+29853, libc.VaList(bp+8, zDb, zPrefix))
			ii1 = 0
			for {
				if !(ii1 < int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux)) {
					break
				}
				if ii1 != 0 {
					Xsqlite3_str_append(tls, p1, __ccgo_ts+15563, int32(1))
				}
				if ii1 < int32((*TRtree)(unsafe.Pointer(pRtree)).FnAuxNotNull) {
					Xsqlite3_str_appendf(tls, p1, __ccgo_ts+29880, libc.VaList(bp+8, ii1, ii1+int32(2), ii1))
				} else {
					Xsqlite3_str_appendf(tls, p1, __ccgo_ts+29902, libc.VaList(bp+8, ii1, ii1+int32(2)))
				}
				goto _3
			_3:
				;
				ii1 = ii1 + 1
			}
			Xsqlite3_str_appendf(tls, p1, __ccgo_ts+29910, 0)
			zSql1 = Xsqlite3_str_finish(tls, p1)
			if zSql1 == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				rc = Xsqlite3_prepare_v3(tls, db, zSql1, -int32(1), uint32(f), pRtree+116, uintptr(0))
				Xsqlite3_free(tls, zSql1)
			}
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** The xUpdate method for rtree module virtual tables.
//	*/
func _rtreeUpdate(tls *libc.TLS, pVtab uintptr, nData int32, aData uintptr, pRowid uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var bHaveRowid, ii, jj, nn, rc, rc2, steprc int32
	var pRtree, pUp uintptr
	var _ /* cell at bp+0 */ TRtreeCell
	var _ /* pLeaf at bp+48 */ uintptr
	_, _, _, _, _, _, _, _, _ = bHaveRowid, ii, jj, nn, pRtree, pUp, rc, rc2, steprc
	pRtree = pVtab
	rc = SQLITE_OK /* New cell to insert if nData>1 */
	bHaveRowid = 0 /* Set to 1 after new rowid is determined */
	if (*TRtree)(unsafe.Pointer(pRtree)).FnNodeRef != 0 {
		/* Unable to write to the btree while another cursor is reading from it,
		 ** since the write might do a rebalance which would disrupt the read
		 ** cursor. */
		return libc.Int32FromInt32(SQLITE_LOCKED) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
	}
	_rtreeReference(tls, pRtree)
	libc.Xmemset(tls, bp, 0, uint32(48))
	/* Constraint handling. A write operation on an r-tree table may return
	 ** SQLITE_CONSTRAINT for two reasons:
	 **
	 **   1. A duplicate rowid value, or
	 **   2. The supplied data violates the "x2>=x1" constraint.
	 **
	 ** In the first case, if the conflict-handling mode is REPLACE, then
	 ** the conflicting row can be removed before proceeding. In the second
	 ** case, SQLITE_CONSTRAINT must be returned regardless of the
	 ** conflict-handling mode specified by the user.
	 */
	if nData > int32(1) {
		nn = nData - int32(4)
		if nn > int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2) {
			nn = int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)
		}
		/* Populate the cell.aCoord[] array. The first coordinate is aData[3].
		 **
		 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
		 ** with "column" that are interpreted as table constraints.
		 ** Example:  CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
		 ** This problem was discovered after years of use, so we silently ignore
		 ** these kinds of misdeclared tables to avoid breaking any legacy.
		 */
		if int32((*TRtree)(unsafe.Pointer(pRtree)).FeCoordType) == RTREE_COORD_REAL32 {
			ii = 0
			for {
				if !(ii < nn) {
					break
				}
				*(*TRtreeValue)(unsafe.Pointer(bp + 8 + uintptr(ii)*4)) = _rtreeValueDown(tls, **(**uintptr)(__ccgo_up(aData + uintptr(ii+int32(3))*4)))
				*(*TRtreeValue)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) = _rtreeValueUp(tls, **(**uintptr)(__ccgo_up(aData + uintptr(ii+int32(4))*4)))
				if *(*TRtreeValue)(unsafe.Pointer(bp + 8 + uintptr(ii)*4)) > *(*TRtreeValue)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) {
					rc = _rtreeConstraintError(tls, pRtree, ii+int32(1))
					goto constraint
				}
				goto _1
			_1:
				;
				ii = ii + int32(2)
			}
		} else {
			ii = 0
			for {
				if !(ii < nn) {
					break
				}
				*(*int32)(unsafe.Pointer(bp + 8 + uintptr(ii)*4)) = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(aData + uintptr(ii+int32(3))*4)))
				*(*int32)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(aData + uintptr(ii+int32(4))*4)))
				if *(*int32)(unsafe.Pointer(bp + 8 + uintptr(ii)*4)) > *(*int32)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) {
					rc = _rtreeConstraintError(tls, pRtree, ii+int32(1))
					goto constraint
				}
				goto _2
			_2:
				;
				ii = ii + int32(2)
			}
		}
		/* If a rowid value was supplied, check if it is already present in
		 ** the table. If so, the constraint has failed. */
		if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(aData + 2*4))) != int32(SQLITE_NULL) {
			(**(**TRtreeCell)(__ccgo_up(bp))).FiRowid = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(aData + 2*4)))
			if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(aData))) == int32(SQLITE_NULL) || Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(aData))) != (**(**TRtreeCell)(__ccgo_up(bp))).FiRowid {
				Xsqlite3_bind_int64(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid, int32(1), (**(**TRtreeCell)(__ccgo_up(bp))).FiRowid)
				steprc = Xsqlite3_step(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid)
				rc = Xsqlite3_reset(tls, (*TRtree)(unsafe.Pointer(pRtree)).FpReadRowid)
				if int32(SQLITE_ROW) == steprc {
					if Xsqlite3_vtab_on_conflict(tls, (*TRtree)(unsafe.Pointer(pRtree)).Fdb) == int32(SQLITE_REPLACE) {
						rc = _rtreeDeleteRowid(tls, pRtree, (**(**TRtreeCell)(__ccgo_up(bp))).FiRowid)
					} else {
						rc = _rtreeConstraintError(tls, pRtree, 0)
						goto constraint
					}
				}
			}
			bHaveRowid = int32(1)
		}
	}
	/* If aData[0] is not an SQL NULL value, it is the rowid of a
	 ** record to delete from the r-tree table. The following block does
	 ** just that.
	 */
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(aData))) != int32(SQLITE_NULL) {
		rc = _rtreeDeleteRowid(tls, pRtree, Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(aData))))
	}
	/* If the aData[] array contains more than one element, elements
	 ** (aData[2]..aData[argc-1]) contain a new record to insert into
	 ** the r-tree structure.
	 */
	if rc == SQLITE_OK && nData > int32(1) {
		/* Insert the new record into the r-tree */
		**(**uintptr)(__ccgo_up(bp + 48)) = uintptr(0)
		/* Figure out the rowid of the new row. */
		if bHaveRowid == 0 {
			rc = _rtreeNewRowid(tls, pRtree, bp)
		}
		**(**Tsqlite_int64)(__ccgo_up(pRowid)) = (**(**TRtreeCell)(__ccgo_up(bp))).FiRowid
		if rc == SQLITE_OK {
			rc = _ChooseLeaf(tls, pRtree, bp, 0, bp+48)
		}
		if rc == SQLITE_OK {
			rc = _rtreeInsertCell(tls, pRtree, **(**uintptr)(__ccgo_up(bp + 48)), bp, 0)
			rc2 = _nodeRelease(tls, pRtree, **(**uintptr)(__ccgo_up(bp + 48)))
			if rc == SQLITE_OK {
				rc = rc2
			}
		}
		if rc == SQLITE_OK && (*TRtree)(unsafe.Pointer(pRtree)).FnAux != 0 {
			pUp = (*TRtree)(unsafe.Pointer(pRtree)).FpWriteAux
			Xsqlite3_bind_int64(tls, pUp, int32(1), **(**Tsqlite_int64)(__ccgo_up(pRowid)))
			jj = 0
			for {
				if !(jj < int32((*TRtree)(unsafe.Pointer(pRtree)).FnAux)) {
					break
				}
				Xsqlite3_bind_value(tls, pUp, jj+int32(2), **(**uintptr)(__ccgo_up(aData + uintptr(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim2)+int32(3)+jj)*4)))
				goto _3
			_3:
				;
				jj = jj + 1
			}
			Xsqlite3_step(tls, pUp)
			rc = Xsqlite3_reset(tls, pUp)
		}
	}
	goto constraint
constraint:
	;
	_rtreeRelease(tls, pRtree)
	return rc
}

// C documentation
//
//	/*
//	** Usage:
//	**
//	**   rtreecheck(<rtree-table>);
//	**   rtreecheck(<database>, <rtree-table>);
//	**
//	** Invoking this SQL function runs an integrity-check on the named rtree
//	** table. The integrity-check verifies the following:
//	**
//	**   1. For each cell in the r-tree structure (%_node table), that:
//	**
//	**       a) for each dimension, (coord1 <= coord2).
//	**
//	**       b) unless the cell is on the root node, that the cell is bounded
//	**          by the parent cell on the parent node.
//	**
//	**       c) for leaf nodes, that there is an entry in the %_rowid
//	**          table corresponding to the cell's rowid value that
//	**          points to the correct node.
//	**
//	**       d) for cells on non-leaf nodes, that there is an entry in the
//	**          %_parent table mapping from the cell's child node to the
//	**          node that it resides on.
//	**
//	**   2. That there are the same number of entries in the %_rowid table
//	**      as there are leaf cells in the r-tree structure, and that there
//	**      is a leaf cell that corresponds to each entry in the %_rowid table.
//	**
//	**   3. That there are the same number of entries in the %_parent table
//	**      as there are non-leaf cells in the r-tree structure, and that
//	**      there is a non-leaf cell that corresponds to each entry in the
//	**      %_parent table.
//	*/
func _rtreecheck(tls *libc.TLS, ctx uintptr, nArg int32, apArg uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var rc int32
	var zDb, zTab, v1 uintptr
	var _ /* zReport at bp+0 */ uintptr
	_, _, _, _ = rc, zDb, zTab, v1
	if nArg != int32(1) && nArg != int32(2) {
		Xsqlite3_result_error(tls, ctx, __ccgo_ts+31023, -int32(1))
	} else {
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		zDb = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(apArg)))
		if nArg == int32(1) {
			zTab = zDb
			zDb = __ccgo_ts + 8033
		} else {
			zTab = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(apArg + 1*4)))
		}
		rc = _rtreeCheckTable(tls, Xsqlite3_context_db_handle(tls, ctx), zDb, zTab, bp)
		if rc == SQLITE_OK {
			if **(**uintptr)(__ccgo_up(bp)) != 0 {
				v1 = **(**uintptr)(__ccgo_up(bp))
			} else {
				v1 = __ccgo_ts + 21023
			}
			Xsqlite3_result_text(tls, ctx, v1, -int32(1), uintptr(-libc.Int32FromInt32(1)))
		} else {
			Xsqlite3_result_error_code(tls, ctx, rc)
		}
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp)))
	}
}

/* Conditionally include the geopoly code */
/************** Include geopoly.c in the middle of rtree.c *******************/
/************** Begin file geopoly.c *****************************************/
/*
** 2018-05-25
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
**
** This file implements an alternative R-Tree virtual table that
** uses polygons to express the boundaries of 2-dimensional objects.
**
** This file is #include-ed onto the end of "rtree.c" so that it has
** access to all of the R-Tree internals.
 */
/* #include <stdlib.h> */

/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */

/* Character class routines */
/* Use the SQLite core versions if this routine is part of the
 ** SQLite amalgamation */

// C documentation
//
//	/*
//	** Implementation of a scalar function that decodes r-tree nodes to
//	** human readable strings. This can be used for debugging and analysis.
//	**
//	** The scalar function takes two arguments: (1) the number of dimensions
//	** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
//	** an r-tree node.  For a two-dimensional r-tree structure called "rt", to
//	** deserialize all nodes, a statement like:
//	**
//	**   SELECT rtreenode(2, data) FROM rt_node;
//	**
//	** The human readable string takes the form of a Tcl list with one
//	** entry for each cell in the r-tree node. Each entry is itself a
//	** list, containing the 8-byte rowid/pageno followed by the
//	** <num-dimension>*2 coordinates.
//	*/
func _rtreenode(tls *libc.TLS, ctx uintptr, nArg int32, apArg uintptr) {
	bp := tls.Alloc(608)
	defer tls.Free(608)
	var errCode, ii, jj, nData int32
	var pOut uintptr
	var _ /* cell at bp+544 */ TRtreeCell
	var _ /* node at bp+0 */ TRtreeNode
	var _ /* tree at bp+32 */ TRtree
	_, _, _, _, _ = errCode, ii, jj, nData, pOut
	_ = nArg
	libc.Xmemset(tls, bp, 0, uint32(32))
	libc.Xmemset(tls, bp+32, 0, uint32(512))
	(**(**TRtree)(__ccgo_up(bp + 32))).FnDim = uint8(Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(apArg))))
	if int32((**(**TRtree)(__ccgo_up(bp + 32))).FnDim) < int32(1) || int32((**(**TRtree)(__ccgo_up(bp + 32))).FnDim) > int32(5) {
		return
	}
	(**(**TRtree)(__ccgo_up(bp + 32))).FnDim2 = uint8(int32((**(**TRtree)(__ccgo_up(bp + 32))).FnDim) * int32(2))
	(**(**TRtree)(__ccgo_up(bp + 32))).FnBytesPerCell = uint8(int32(8) + int32(8)*int32((**(**TRtree)(__ccgo_up(bp + 32))).FnDim))
	(**(**TRtreeNode)(__ccgo_up(bp))).FzData = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(apArg + 1*4)))
	if (**(**TRtreeNode)(__ccgo_up(bp))).FzData == uintptr(0) {
		return
	}
	nData = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(apArg + 1*4)))
	if nData < int32(4) {
		return
	}
	if nData < int32(4)+_readInt16(tls, (*TRtreeNode)(unsafe.Pointer(bp)).FzData+2)*int32((**(**TRtree)(__ccgo_up(bp + 32))).FnBytesPerCell) {
		return
	}
	pOut = Xsqlite3_str_new(tls, uintptr(0))
	ii = 0
	for {
		if !(ii < _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(bp)).FzData+2)) {
			break
		}
		_nodeGetCell(tls, bp+32, bp, ii, bp+544)
		if ii > 0 {
			Xsqlite3_str_append(tls, pOut, __ccgo_ts+12758, int32(1))
		}
		Xsqlite3_str_appendf(tls, pOut, __ccgo_ts+30249, libc.VaList(bp+600, (**(**TRtreeCell)(__ccgo_up(bp + 544))).FiRowid))
		jj = 0
		for {
			if !(jj < int32((**(**TRtree)(__ccgo_up(bp + 32))).FnDim2)) {
				break
			}
			Xsqlite3_str_appendf(tls, pOut, __ccgo_ts+30255, libc.VaList(bp+600, float64(*(*TRtreeValue)(unsafe.Pointer(bp + 544 + 8 + uintptr(jj)*4)))))
			goto _2
		_2:
			;
			jj = jj + 1
		}
		Xsqlite3_str_append(tls, pOut, __ccgo_ts+28094, int32(1))
		goto _1
	_1:
		;
		ii = ii + 1
	}
	errCode = Xsqlite3_str_errcode(tls, pOut)
	Xsqlite3_result_error_code(tls, ctx, errCode)
	Xsqlite3_result_text(tls, ctx, Xsqlite3_str_finish(tls, pOut), -int32(1), __ccgo_fp(Xsqlite3_free))
}

// C documentation
//
//	/*
//	** Copy the contents of object (*pFrom) into (*pTo).
//	*/
func _sampleCopy(tls *libc.TLS, p uintptr, pTo uintptr, pFrom uintptr) {
	(*TStatSample)(unsafe.Pointer(pTo)).FisPSample = (*TStatSample)(unsafe.Pointer(pFrom)).FisPSample
	(*TStatSample)(unsafe.Pointer(pTo)).FiCol = (*TStatSample)(unsafe.Pointer(pFrom)).FiCol
	(*TStatSample)(unsafe.Pointer(pTo)).FiHash = (*TStatSample)(unsafe.Pointer(pFrom)).FiHash
	libc.Xmemcpy(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanEq, (*TStatSample)(unsafe.Pointer(pFrom)).FanEq, uint32(8)*uint32((*TStatAccum)(unsafe.Pointer(p)).FnCol))
	libc.Xmemcpy(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanLt, (*TStatSample)(unsafe.Pointer(pFrom)).FanLt, uint32(8)*uint32((*TStatAccum)(unsafe.Pointer(p)).FnCol))
	libc.Xmemcpy(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanDLt, (*TStatSample)(unsafe.Pointer(pFrom)).FanDLt, uint32(8)*uint32((*TStatAccum)(unsafe.Pointer(p)).FnCol))
	if (*TStatSample)(unsafe.Pointer(pFrom)).FnRowid != 0 {
		_sampleSetRowid(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, pTo, int32((*TStatSample)(unsafe.Pointer(pFrom)).FnRowid), *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(pFrom)).Fu)))
	} else {
		_sampleSetRowidInt64(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, pTo, *(*Ti64)(unsafe.Pointer(pFrom + 16)))
	}
}

// C documentation
//
//	/*
//	** Copy the contents of sample *pNew into the p->a[] array. If necessary,
//	** remove the least desirable sample from p->a[] to make room.
//	*/
func _sampleInsert(tls *libc.TLS, p uintptr, pNew uintptr, nEqZero int32) {
	var anDLt, anEq, anLt, pMin, pOld, pSample, pUpgrade uintptr
	var i, iMin int32
	_, _, _, _, _, _, _, _, _ = anDLt, anEq, anLt, i, iMin, pMin, pOld, pSample, pUpgrade
	pSample = uintptr(0)
	/* StatAccum.nMaxEqZero is set to the maximum number of leading 0
	 ** values in the anEq[] array of any sample in StatAccum.a[]. In
	 ** other words, if nMaxEqZero is n, then it is guaranteed that there
	 ** are no samples with StatSample.anEq[m]==0 for (m>=n). */
	if nEqZero > (*TStatAccum)(unsafe.Pointer(p)).FnMaxEqZero {
		(*TStatAccum)(unsafe.Pointer(p)).FnMaxEqZero = nEqZero
	}
	if int32((*TStatSample)(unsafe.Pointer(pNew)).FisPSample) == 0 {
		pUpgrade = uintptr(0)
		/* This sample is being added because the prefix that ends in column
		 ** iCol occurs many times in the table. However, if we have already
		 ** added a sample that shares this prefix, there is no need to add
		 ** this one. Instead, upgrade the priority of the highest priority
		 ** existing sample that shares this prefix.  */
		i = (*TStatAccum)(unsafe.Pointer(p)).FnSample - int32(1)
		for {
			if !(i >= 0) {
				break
			}
			pOld = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40
			if **(**TtRowcnt)(__ccgo_up((*TStatSample)(unsafe.Pointer(pOld)).FanEq + uintptr((*TStatSample)(unsafe.Pointer(pNew)).FiCol)*8)) == uint64(0) {
				if (*TStatSample)(unsafe.Pointer(pOld)).FisPSample != 0 {
					return
				}
				if pUpgrade == uintptr(0) || _sampleIsBetter(tls, p, pOld, pUpgrade) != 0 {
					pUpgrade = pOld
				}
			}
			goto _1
		_1:
			;
			i = i - 1
		}
		if pUpgrade != 0 {
			(*TStatSample)(unsafe.Pointer(pUpgrade)).FiCol = (*TStatSample)(unsafe.Pointer(pNew)).FiCol
			**(**TtRowcnt)(__ccgo_up((*TStatSample)(unsafe.Pointer(pUpgrade)).FanEq + uintptr((*TStatSample)(unsafe.Pointer(pUpgrade)).FiCol)*8)) = **(**TtRowcnt)(__ccgo_up((*TStatSample)(unsafe.Pointer(pNew)).FanEq + uintptr((*TStatSample)(unsafe.Pointer(pUpgrade)).FiCol)*8))
			goto find_new_min
		}
	}
	/* If necessary, remove sample iMin to make room for the new sample. */
	if (*TStatAccum)(unsafe.Pointer(p)).FnSample >= (*TStatAccum)(unsafe.Pointer(p)).FmxSample {
		pMin = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiMin)*40
		anEq = (*TStatSample)(unsafe.Pointer(pMin)).FanEq
		anLt = (*TStatSample)(unsafe.Pointer(pMin)).FanLt
		anDLt = (*TStatSample)(unsafe.Pointer(pMin)).FanDLt
		_sampleClear(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, pMin)
		libc.Xmemmove(tls, pMin, pMin+1*40, uint32(40)*uint32((*TStatAccum)(unsafe.Pointer(p)).FnSample-(*TStatAccum)(unsafe.Pointer(p)).FiMin-libc.Int32FromInt32(1)))
		pSample = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FnSample-int32(1))*40
		(*TStatSample)(unsafe.Pointer(pSample)).FnRowid = uint32(0)
		(*TStatSample)(unsafe.Pointer(pSample)).FanEq = anEq
		(*TStatSample)(unsafe.Pointer(pSample)).FanDLt = anDLt
		(*TStatSample)(unsafe.Pointer(pSample)).FanLt = anLt
		(*TStatAccum)(unsafe.Pointer(p)).FnSample = (*TStatAccum)(unsafe.Pointer(p)).FmxSample - int32(1)
	}
	/* The "rows less-than" for the rowid column must be greater than that
	 ** for the last sample in the p->a[] array. Otherwise, the samples would
	 ** be out of order. */
	/* Insert the new sample */
	pSample = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FnSample)*40
	_sampleCopy(tls, p, pSample, pNew)
	(*TStatAccum)(unsafe.Pointer(p)).FnSample = (*TStatAccum)(unsafe.Pointer(p)).FnSample + 1
	/* Zero the first nEqZero entries in the anEq[] array. */
	libc.Xmemset(tls, (*TStatSample)(unsafe.Pointer(pSample)).FanEq, 0, uint32(8)*uint32(nEqZero))
	goto find_new_min
find_new_min:
	;
	if (*TStatAccum)(unsafe.Pointer(p)).FnSample >= (*TStatAccum)(unsafe.Pointer(p)).FmxSample {
		iMin = -int32(1)
		i = 0
		for {
			if !(i < (*TStatAccum)(unsafe.Pointer(p)).FmxSample) {
				break
			}
			if (**(**TStatSample)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FisPSample != 0 {
				goto _2
			}
			if iMin < 0 || _sampleIsBetter(tls, p, (*TStatAccum)(unsafe.Pointer(p)).Fa+uintptr(iMin)*40, (*TStatAccum)(unsafe.Pointer(p)).Fa+uintptr(i)*40) != 0 {
				iMin = i
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		(*TStatAccum)(unsafe.Pointer(p)).FiMin = iMin
	}
}

// C documentation
//
//	/* Initialize the BLOB value of a ROWID
//	*/
func _sampleSetRowid(tls *libc.TLS, db uintptr, p uintptr, n int32, pData uintptr) {
	if (*TStatSample)(unsafe.Pointer(p)).FnRowid != 0 {
		_sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu)))
	}
	*(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu)) = _sqlite3DbMallocRawNN(tls, db, uint64(n))
	if *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu)) != 0 {
		(*TStatSample)(unsafe.Pointer(p)).FnRowid = uint32(n)
		libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(p)).Fu)), pData, uint32(n))
	} else {
		(*TStatSample)(unsafe.Pointer(p)).FnRowid = uint32(0)
	}
}

// C documentation
//
//	/*
//	** The cursor passed as the only argument must point to a valid entry
//	** when this function is called (i.e. have eState==CURSOR_VALID). This
//	** function saves the current cursor key in variables pCur->nKey and
//	** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
//	** code otherwise.
//	**
//	** If the cursor is open on an intkey table, then the integer key
//	** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
//	** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
//	** set to point to a malloced buffer pCur->nKey bytes in size containing
//	** the key.
//	*/
func _saveCursorKey(tls *libc.TLS, pCur uintptr) (r int32) {
	var pKey uintptr
	var rc int32
	_, _ = pKey, rc
	rc = SQLITE_OK
	if (*TBtCursor)(unsafe.Pointer(pCur)).FcurIntKey != 0 {
		/* Only the rowid is required for a table btree */
		(*TBtCursor)(unsafe.Pointer(pCur)).FnKey = _sqlite3BtreeIntegerKey(tls, pCur)
	} else {
		(*TBtCursor)(unsafe.Pointer(pCur)).FnKey = int64(_sqlite3BtreePayloadSize(tls, pCur))
		pKey = _sqlite3Malloc(tls, uint64((*TBtCursor)(unsafe.Pointer(pCur)).FnKey+int64(9)+int64(8)))
		if pKey != 0 {
			rc = _sqlite3BtreePayload(tls, pCur, uint32(0), uint32(int32((*TBtCursor)(unsafe.Pointer(pCur)).FnKey)), pKey)
			if rc == SQLITE_OK {
				libc.Xmemset(tls, pKey+uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FnKey), 0, uint32(libc.Int32FromInt32(9)+libc.Int32FromInt32(8)))
				(*TBtCursor)(unsafe.Pointer(pCur)).FpKey = pKey
			} else {
				Xsqlite3_free(tls, pKey)
			}
		} else {
			rc = int32(SQLITE_NOMEM)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Check schema cookies in all databases.  If any cookie is out
//	** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
//	** make no changes to pParse->rc.
//	*/
func _schemaIsValid(tls *libc.TLS, pParse uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pBt uintptr
	var iDb, openedTransaction, rc int32
	var _ /* cookie at bp+0 */ int32
	_, _, _, _, _ = db, iDb, openedTransaction, pBt, rc
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	iDb = 0
	for {
		if !(iDb < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
			break
		}
		openedTransaction = 0                                                                     /* True if a transaction is opened */
		pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt /* Btree database to read cookie from */
		if pBt == uintptr(0) {
			goto _1
		}
		/* If there is not already a read-only (or read-write) transaction opened
		 ** on the b-tree database, open one now. If a transaction is opened, it
		 ** will be closed immediately after reading the meta-value. */
		if _sqlite3BtreeTxnState(tls, pBt) == SQLITE_TXN_NONE {
			rc = _sqlite3BtreeBeginTrans(tls, pBt, 0, uintptr(0))
			if rc == int32(SQLITE_NOMEM) || rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<<libc.Int32FromInt32(8) {
				_sqlite3OomFault(tls, db)
				(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM)
			}
			if rc != SQLITE_OK {
				return
			}
			openedTransaction = int32(1)
		}
		/* Read the schema cookie from the database. If it does not match the
		 ** value stored as part of the in-memory schema representation,
		 ** set Parse.rc to SQLITE_SCHEMA. */
		_sqlite3BtreeGetMeta(tls, pBt, int32(BTREE_SCHEMA_VERSION), bp)
		if **(**int32)(__ccgo_up(bp)) != (*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema)).Fschema_cookie {
			if int32((*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema)).FschemaFlags)&int32(DB_SchemaLoaded) == int32(DB_SchemaLoaded) {
				(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_SCHEMA)
			}
			_sqlite3ResetOneSchema(tls, db, iDb)
		}
		/* Close the transaction, if one was opened. */
		if openedTransaction != 0 {
			_sqlite3BtreeCommit(tls, pBt)
		}
		goto _1
	_1:
		;
		iDb = iDb + 1
	}
}

// C documentation
//
//	/*
//	** The xExpr callback for the search of invalid ON clause terms.
//	*/
func _selectCheckOnClausesExpr(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iTab, ii, nSrc int32
	var pCtx, pSrc, v2 uintptr
	_, _, _, _, _, _ = iTab, ii, nSrc, pCtx, pSrc, v2
	pCtx = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	/* Check if pExpr is root or near-root of an ON clause constraint that needs
	 ** to be checked to ensure that it does not refer to tables in its FROM
	 ** clause to the right of itself. i.e. it is either:
	 **
	 **   + an ON clause on an OUTER join, or
	 **   + an ON clause on an INNER join within a FROM that features at
	 **     least one RIGHT or FULL join.
	 */
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)) != uint32(0) && int32((*(*TSrcItem)(unsafe.Pointer((*TCheckOnCtx)(unsafe.Pointer(pCtx)).FpSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
		/* If CheckOnCtx.iJoin is already set, then fall through and process
		 ** this expression node as normal. Or, if CheckOnCtx.iJoin is still 0,
		 ** set it to the cursor number of the RHS of the join to which this
		 ** ON expression was attached and then iterate through the entire
		 ** expression.  */
		if (*TCheckOnCtx)(unsafe.Pointer(pCtx)).FiJoin == 0 {
			(*TCheckOnCtx)(unsafe.Pointer(pCtx)).FiJoin = *(*int32)(unsafe.Pointer(pExpr + 36))
			_sqlite3WalkExprNN(tls, pWalker, pExpr)
			(*TCheckOnCtx)(unsafe.Pointer(pCtx)).FiJoin = 0
			return int32(WRC_Prune)
		}
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) {
		/* A column expression. Find the SrcList (if any) to which it refers.
		 ** Then, if CheckOnCtx.iJoin indicates that this expression is part of an
		 ** ON clause from that SrcList (i.e. if iJoin is non-zero), check that it
		 ** does not refer to a table to the right of CheckOnCtx.iJoin. */
		for cond := true; cond; cond = pCtx != 0 {
			pSrc = (*TCheckOnCtx)(unsafe.Pointer(pCtx)).FpSrc
			nSrc = (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc
			iTab = (*TExpr)(unsafe.Pointer(pExpr)).FiTable
			ii = 0
			for {
				if !(ii < nSrc && (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(ii)*48))).FiCursor != iTab) {
					break
				}
				goto _1
			_1:
				;
				ii = ii + 1
			}
			if ii < nSrc {
				if (*TCheckOnCtx)(unsafe.Pointer(pCtx)).FiJoin != 0 && iTab > (*TCheckOnCtx)(unsafe.Pointer(pCtx)).FiJoin {
					if (*TCheckOnCtx)(unsafe.Pointer(pCtx)).FbFuncArg != 0 {
						v2 = __ccgo_ts + 22794
					} else {
						v2 = __ccgo_ts + 22818
					}
					_sqlite3ErrorMsg(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, __ccgo_ts+22828, libc.VaList(bp+8, v2))
					return int32(WRC_Abort)
				}
				break
			}
			pCtx = (*TCheckOnCtx)(unsafe.Pointer(pCtx)).FpParent
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** The xSelect callback for the search of invalid ON clause terms.
//	*/
func _selectCheckOnClausesSelect(tls *libc.TLS, pWalker uintptr, pSelect uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pCtx uintptr
	var _ /* sCtx at bp+0 */ TCheckOnCtx
	_ = pCtx
	pCtx = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	if (*TSelect)(unsafe.Pointer(pSelect)).FpSrc == (*TCheckOnCtx)(unsafe.Pointer(pCtx)).FpSrc || (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc)).FnSrc == 0 {
		return WRC_Continue
	} else {
		libc.Xmemset(tls, bp, 0, uint32(16))
		(**(**TCheckOnCtx)(__ccgo_up(bp))).FpSrc = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc
		(**(**TCheckOnCtx)(__ccgo_up(bp))).FpParent = pCtx
		*(*uintptr)(unsafe.Pointer(pWalker + 24)) = bp
		_sqlite3WalkSelect(tls, pWalker, pSelect)
		*(*uintptr)(unsafe.Pointer(pWalker + 24)) = pCtx
		**(**Tu32)(__ccgo_up(pSelect + 4)) &= uint32(^libc.Int32FromInt32(SF_OnToWhere))
		return int32(WRC_Prune)
	}
	return r
}

// C documentation
//
//	/*
//	** This routine is a Walker callback for "expanding" a SELECT statement.
//	** "Expanding" means to do the following:
//	**
//	**    (1)  Make sure VDBE cursor numbers have been assigned to every
//	**         element of the FROM clause.
//	**
//	**    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that
//	**         defines FROM clause.  When views appear in the FROM clause,
//	**         fill pTabList->a[].pSelect with a copy of the SELECT statement
//	**         that implements the view.  A copy is made of the view's SELECT
//	**         statement so that we can freely modify or delete that statement
//	**         without worrying about messing up the persistent representation
//	**         of the view.
//	**
//	**    (3)  Add terms to the WHERE clause to accommodate the NATURAL keyword
//	**         on joins and the ON and USING clause of joins.
//	**
//	**    (4)  Scan the list of columns in the result set (pEList) looking
//	**         for instances of the "*" operator or the TABLE.* operator.
//	**         If found, expand each "*" to be every column in every table
//	**         and TABLE.* to be every column in TABLE.
//	**
//	*/
func _selectExpander(tls *libc.TLS, pWalker uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var a, db, pE, pEList, pExpr, pFrom, pLeft, pNestedFrom, pNew, pParse, pRight, pSel, pTab, pTab1, pTabList, pUsing, pX, pX1, zName, zSchemaName, zTName, zTabName, zUName, v2 uintptr
	var eCodeOrig Tu8
	var elistFlags Tu32
	var flags, i, iDb, iErrOfst, ii, j, k, longNames, nAdd, rc, tableSeen, v1 int32
	var nCol Ti16
	var selFlags Tu16
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a, db, eCodeOrig, elistFlags, flags, i, iDb, iErrOfst, ii, j, k, longNames, nAdd, nCol, pE, pEList, pExpr, pFrom, pLeft, pNestedFrom, pNew, pParse, pRight, pSel, pTab, pTab1, pTabList, pUsing, pX, pX1, rc, selFlags, tableSeen, zName, zSchemaName, zTName, zTabName, zUName, v1, v2
	pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	selFlags = uint16((*TSelect)(unsafe.Pointer(p)).FselFlags)
	elistFlags = uint32(0)
	**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_Expanded)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		return int32(WRC_Abort)
	}
	if int32(selFlags)&int32(SF_Expanded) != 0 {
		return int32(WRC_Prune)
	}
	if (*TWalker)(unsafe.Pointer(pWalker)).FeCode != 0 {
		/* Renumber selId because it has been copied from a view */
		v2 = pParse + 100
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		(*TSelect)(unsafe.Pointer(p)).FselId = uint32(v1)
	}
	pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc
	pEList = (*TSelect)(unsafe.Pointer(p)).FpEList
	if (*TParse)(unsafe.Pointer(pParse)).FpWith != 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_View) != 0 {
		if (*TSelect)(unsafe.Pointer(p)).FpWith == uintptr(0) {
			(*TSelect)(unsafe.Pointer(p)).FpWith = _sqlite3DbMallocZero(tls, db, uint64(uint32(libc.UintptrFromInt32(0)+12)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(24)))
			if (*TSelect)(unsafe.Pointer(p)).FpWith == uintptr(0) {
				return int32(WRC_Abort)
			}
		}
		(*TWith)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpWith)).FbView = int32(1)
	}
	_sqlite3WithPush(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpWith, uint8(0))
	/* Make sure cursor numbers have been assigned to all entries in
	 ** the FROM clause of the SELECT statement.
	 */
	_sqlite3SrcListAssignCursors(tls, pParse, pTabList)
	/* Look up every table named in the FROM clause of the select.  If
	 ** an entry of the FROM clause is a subquery instead of a table or view,
	 ** then create a transient table structure to describe the subquery.
	 */
	i = 0
	pFrom = pTabList + 8
	for {
		if !(i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) {
			break
		}
		if (*TSrcItem)(unsafe.Pointer(pFrom)).FpSTab != 0 {
			goto _3
		}
		if (*TSrcItem)(unsafe.Pointer(pFrom)).FzName == uintptr(0) {
			pSel = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pFrom + 44)))).FpSelect
			/* A sub-query in the FROM clause of a SELECT */
			if _sqlite3WalkSelect(tls, pWalker, pSel) != 0 {
				return int32(WRC_Abort)
			}
			if _sqlite3ExpandSubquery(tls, pParse, pFrom) != 0 {
				return int32(WRC_Abort)
			}
		} else {
			v1 = _resolveFromTermToCte(tls, pParse, pWalker, pFrom)
			rc = v1
			if v1 != 0 {
				if rc > int32(1) {
					return int32(WRC_Abort)
				}
				pTab = (*TSrcItem)(unsafe.Pointer(pFrom)).FpSTab
			} else {
				/* An ordinary table or view name in the FROM clause */
				v2 = _sqlite3LocateTableItem(tls, pParse, uint32(0), pFrom)
				pTab = v2
				(*TSrcItem)(unsafe.Pointer(pFrom)).FpSTab = v2
				if pTab == uintptr(0) {
					return int32(WRC_Abort)
				}
				if (*TTable)(unsafe.Pointer(pTab)).FnTabRef >= uint32(0xffff) {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22489, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
					(*TSrcItem)(unsafe.Pointer(pFrom)).FpSTab = uintptr(0)
					return int32(WRC_Abort)
				}
				(*TTable)(unsafe.Pointer(pTab)).FnTabRef = (*TTable)(unsafe.Pointer(pTab)).FnTabRef + 1
				if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) && _cannotBeFunction(tls, pParse, pFrom) != 0 {
					return int32(WRC_Abort)
				}
				if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
					eCodeOrig = uint8((*TWalker)(unsafe.Pointer(pWalker)).FeCode)
					if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 {
						return int32(WRC_Abort)
					}
					if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
						if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_EnableView) == uint64(0) && (*TTable)(unsafe.Pointer(pTab)).FpSchema != (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema {
							_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22528, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
						}
						_sqlite3SrcItemAttachSubquery(tls, pParse, pFrom, (*(*struct {
							FpSelect uintptr
						})(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTab)).Fu))).FpSelect, int32(1))
					} else {
						if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) && (int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x100>>8) != 0 || int32((*TParse)(unsafe.Pointer(pParse)).FprepFlags)&int32(SQLITE_PREPARE_FROM_DDL) != 0) && (*(*struct {
							FnArg  int32
							FazArg uintptr
							Fp     uintptr
						})(unsafe.Pointer(pTab + 44))).Fp != uintptr(0) && int32((*TVTable)(unsafe.Pointer((*(*struct {
							FnArg  int32
							FazArg uintptr
							Fp     uintptr
						})(unsafe.Pointer(pTab + 44))).Fp)).FeVtabRisk) > libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_TrustedSchema) != uint64(0)) {
							_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17567, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
						}
					}
					nCol = (*TTable)(unsafe.Pointer(pTab)).FnCol
					(*TTable)(unsafe.Pointer(pTab)).FnCol = int16(-int32(1))
					(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1) /* Turn on Select.selId renumbering */
					if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x4>>2) != 0 {
						_sqlite3WalkSelect(tls, pWalker, (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pFrom + 44)))).FpSelect)
					}
					(*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(eCodeOrig)
					(*TTable)(unsafe.Pointer(pTab)).FnCol = nCol
				}
			}
		}
		/* Locate the index named by the INDEXED BY clause, if any. */
		if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x2>>1) != 0 && _sqlite3IndexedByLookup(tls, pParse, pFrom) != 0 {
			return int32(WRC_Abort)
		}
		goto _3
	_3:
		;
		i = i + 1
		pFrom += 48
	}
	/* Process NATURAL keywords, and ON and USING clauses of joins.
	 */
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 || _sqlite3ProcessJoin(tls, pParse, p) != 0 {
		return int32(WRC_Abort)
	}
	/* For every "*" that occurs in the column list, insert the names of
	 ** all columns in all tables.  And for every TABLE.* insert the names
	 ** of all columns in TABLE.  The parser inserted a special expression
	 ** with the TK_ASTERISK operator for each "*" that it found in the column
	 ** list.  The following code just has to locate the TK_ASTERISK
	 ** expressions and expand each one to the list of all columns in
	 ** all tables.
	 **
	 ** The first loop just checks to see if there are any "*" operators
	 ** that need expanding.
	 */
	k = 0
	for {
		if !(k < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
			break
		}
		pE = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(k)*20))).FpExpr
		if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_ASTERISK) {
			break
		}
		if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_DOT) && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pE)).FpRight)).Fop) == int32(TK_ASTERISK) {
			break
		}
		elistFlags = elistFlags | (*TExpr)(unsafe.Pointer(pE)).Fflags
		goto _6
	_6:
		;
		k = k + 1
	}
	if k < (*TExprList)(unsafe.Pointer(pEList)).FnExpr {
		/*
		 ** If we get here it means the result set contains one or more "*"
		 ** operators that need to be expanded.  Loop through each expression
		 ** in the result set and expand them one by one.
		 */
		a = pEList + 8
		pNew = uintptr(0)
		flags = int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags)
		longNames = libc.BoolInt32(flags&int32(SQLITE_FullColNames) != 0 && flags&int32(SQLITE_ShortColNames) == 0)
		k = 0
		for {
			if !(k < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
				break
			}
			pE = (**(**TExprList_item)(__ccgo_up(a + uintptr(k)*20))).FpExpr
			elistFlags = elistFlags | (*TExpr)(unsafe.Pointer(pE)).Fflags
			pRight = (*TExpr)(unsafe.Pointer(pE)).FpRight
			if int32((*TExpr)(unsafe.Pointer(pE)).Fop) != int32(TK_ASTERISK) && (int32((*TExpr)(unsafe.Pointer(pE)).Fop) != int32(TK_DOT) || int32((*TExpr)(unsafe.Pointer(pRight)).Fop) != int32(TK_ASTERISK)) {
				/* This particular expression does not need to be expanded.
				 */
				pNew = _sqlite3ExprListAppend(tls, pParse, pNew, (**(**TExprList_item)(__ccgo_up(a + uintptr(k)*20))).FpExpr)
				if pNew != 0 {
					(*(*TExprList_item)(unsafe.Pointer(pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20))).FzEName = (**(**TExprList_item)(__ccgo_up(a + uintptr(k)*20))).FzEName
					libc.SetBitFieldPtr16Uint32(pNew+8+uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20+8+4, uint32(int32(uint32(*(*uint16)(unsafe.Pointer(a + uintptr(k)*20 + 8 + 4))&0x3>>0))), 0, 0x3)
					(**(**TExprList_item)(__ccgo_up(a + uintptr(k)*20))).FzEName = uintptr(0)
				}
				(**(**TExprList_item)(__ccgo_up(a + uintptr(k)*20))).FpExpr = uintptr(0)
			} else {
				/* This expression is a "*" or a "TABLE.*" and needs to be
				 ** expanded. */
				tableSeen = 0 /* Set to 1 when TABLE matches */
				zTName = uintptr(0)
				if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_DOT) {
					zTName = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pE)).FpLeft + 8))
					iErrOfst = *(*int32)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pE)).FpRight + 36))
				} else {
					iErrOfst = *(*int32)(unsafe.Pointer(pE + 36))
				}
				i = 0
				pFrom = pTabList + 8
				for {
					if !(i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) {
						break
					} /* Number of cols including rowid */
					pTab1 = (*TSrcItem)(unsafe.Pointer(pFrom)).FpSTab /* AS name for this data source */
					zSchemaName = uintptr(0)                          /* USING clause for pFrom[1] */
					v2 = (*TSrcItem)(unsafe.Pointer(pFrom)).FzAlias
					zTabName = v2
					if v2 == uintptr(0) {
						zTabName = (*TTable)(unsafe.Pointer(pTab1)).FzName
					}
					if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
						break
					}
					if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x4000>>14) != 0 {
						pNestedFrom = (*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pFrom + 44)))).FpSelect)).FpEList
					} else {
						if zTName != 0 && _sqlite3StrICmp(tls, zTName, zTabName) != 0 {
							goto _8
						}
						pNestedFrom = uintptr(0)
						iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab1)).FpSchema)
						if iDb >= 0 {
							v2 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
						} else {
							v2 = __ccgo_ts + 8038
						}
						zSchemaName = v2
					}
					if i+int32(1) < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc && int32(*(*uint32)(unsafe.Pointer(pFrom + 1*48 + 12 + 4))&0x800>>11) != 0 && int32(selFlags)&int32(SF_NestedFrom) != 0 {
						pUsing = *(*uintptr)(unsafe.Pointer(pFrom + 1*48 + 40))
						ii = 0
						for {
							if !(ii < (*TIdList)(unsafe.Pointer(pUsing)).FnId) {
								break
							}
							zUName = (*(*TIdList_item)(unsafe.Pointer(pUsing + 4 + uintptr(ii)*4))).FzName
							pRight = _sqlite3Expr(tls, db, int32(TK_ID), zUName)
							_sqlite3ExprSetErrorOffset(tls, pRight, iErrOfst)
							pNew = _sqlite3ExprListAppend(tls, pParse, pNew, pRight)
							if pNew != 0 {
								pX = pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20
								(*TExprList_item)(unsafe.Pointer(pX)).FzEName = _sqlite3MPrintf(tls, db, __ccgo_ts+22559, libc.VaList(bp+8, zUName))
								libc.SetBitFieldPtr16Uint32(pX+8+4, libc.Uint32FromInt32(ENAME_TAB), 0, 0x3)
								libc.SetBitFieldPtr16Uint32(pX+8+4, libc.Uint32FromInt32(1), 7, 0x80)
							}
							goto _11
						_11:
							;
							ii = ii + 1
						}
					} else {
						pUsing = uintptr(0)
					}
					nAdd = int32((*TTable)(unsafe.Pointer(pTab1)).FnCol)
					if (*TTable)(unsafe.Pointer(pTab1)).FtabFlags&uint32(TF_NoVisibleRowid) == uint32(0) && int32(selFlags)&int32(SF_NestedFrom) != 0 {
						nAdd = nAdd + 1
					}
					j = 0
					for {
						if !(j < nAdd) {
							break
						} /* Newly added ExprList term */
						if j == int32((*TTable)(unsafe.Pointer(pTab1)).FnCol) {
							zName = _sqlite3RowidAlias(tls, pTab1)
							if zName == uintptr(0) {
								goto _12
							}
						} else {
							zName = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(j)*12))).FzCnName
							/* If pTab is actually an SF_NestedFrom sub-select, do not
							 ** expand any ENAME_ROWID columns.  */
							if pNestedFrom != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pNestedFrom + 8 + uintptr(j)*20 + 8 + 4))&0x3>>0)) == int32(ENAME_ROWID) {
								goto _12
							}
							if zTName != 0 && pNestedFrom != 0 && _sqlite3MatchEName(tls, pNestedFrom+8+uintptr(j)*20, uintptr(0), zTName, uintptr(0), uintptr(0)) == 0 {
								goto _12
							}
							/* If a column is marked as 'hidden', omit it from the expanded
							 ** result-set list unless the SELECT has the SF_IncludeHidden
							 ** bit set.
							 */
							if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_IncludeHidden) == uint32(0) && int32((*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab1)).FaCol+uintptr(j)*12)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0 {
								goto _12
							}
							if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_NOEXPAND) != 0 && zTName == uintptr(0) && int32(selFlags)&int32(SF_NestedFrom) == 0 {
								goto _12
							}
						}
						tableSeen = int32(1)
						if i > 0 && zTName == uintptr(0) && int32(selFlags)&int32(SF_NestedFrom) == 0 {
							if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x800>>11) != 0 && _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pFrom + 40)), zName) >= 0 {
								/* In a join with a USING clause, omit columns in the
								 ** using clause from the table on the right. */
								goto _12
							}
						}
						pRight = _sqlite3Expr(tls, db, int32(TK_ID), zName)
						if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc > int32(1) && (int32((*TSrcItem)(unsafe.Pointer(pFrom)).Ffg.Fjointype)&int32(JT_LTORJ) == 0 || int32(selFlags)&int32(SF_NestedFrom) != 0 || !(_inAnyUsingClause(tls, zName, pFrom, (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc-i-int32(1)) != 0)) || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
							pLeft = _sqlite3Expr(tls, db, int32(TK_ID), zTabName)
							pExpr = _sqlite3PExpr(tls, pParse, int32(TK_DOT), pLeft, pRight)
							if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*TExpr)(unsafe.Pointer(pE)).FpLeft != 0 {
								_sqlite3RenameTokenRemap(tls, pParse, pLeft, (*TExpr)(unsafe.Pointer(pE)).FpLeft)
							}
							if zSchemaName != 0 {
								pLeft = _sqlite3Expr(tls, db, int32(TK_ID), zSchemaName)
								pExpr = _sqlite3PExpr(tls, pParse, int32(TK_DOT), pLeft, pExpr)
							}
						} else {
							pExpr = pRight
						}
						_sqlite3ExprSetErrorOffset(tls, pExpr, iErrOfst)
						pNew = _sqlite3ExprListAppend(tls, pParse, pNew, pExpr)
						if pNew == uintptr(0) {
							break /* OOM */
						}
						pX1 = pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20
						if int32(selFlags)&int32(SF_NestedFrom) != 0 && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
							if pNestedFrom != 0 && libc.Bool(libc.Bool(!(libc.Int32FromInt32(ViewCanHaveRowid) != 0)) || j < (*TExprList)(unsafe.Pointer(pNestedFrom)).FnExpr) {
								(*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3DbStrDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pNestedFrom + 8 + uintptr(j)*20))).FzEName)
							} else {
								(*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3MPrintf(tls, db, __ccgo_ts+22564, libc.VaList(bp+8, zSchemaName, zTabName, zName))
							}
							if j == int32((*TTable)(unsafe.Pointer(pTab1)).FnCol) {
								v1 = int32(ENAME_ROWID)
							} else {
								v1 = int32(ENAME_TAB)
							}
							libc.SetBitFieldPtr16Uint32(pX1+8+4, uint32(v1), 0, 0x3)
							if int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x800>>11) != 0 && _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pFrom + 40)), zName) >= 0 || pUsing != 0 && _sqlite3IdListIndex(tls, pUsing, zName) >= 0 || j < int32((*TTable)(unsafe.Pointer(pTab1)).FnCol) && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_NOEXPAND) != 0 {
								libc.SetBitFieldPtr16Uint32(pX1+8+4, libc.Uint32FromInt32(1), 8, 0x100)
							}
						} else {
							if longNames != 0 {
								(*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3MPrintf(tls, db, __ccgo_ts+14849, libc.VaList(bp+8, zTabName, zName))
								libc.SetBitFieldPtr16Uint32(pX1+8+4, libc.Uint32FromInt32(ENAME_NAME), 0, 0x3)
							} else {
								(*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3DbStrDup(tls, db, zName)
								libc.SetBitFieldPtr16Uint32(pX1+8+4, libc.Uint32FromInt32(ENAME_NAME), 0, 0x3)
							}
						}
						goto _12
					_12:
						;
						j = j + 1
					}
					goto _8
				_8:
					;
					i = i + 1
					pFrom += 48
				}
				if !(tableSeen != 0) {
					if zTName != 0 {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22573, libc.VaList(bp+8, zTName))
					} else {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22591, 0)
					}
				}
			}
			goto _7
		_7:
			;
			k = k + 1
		}
		_sqlite3ExprListDelete(tls, db, pEList)
		(*TSelect)(unsafe.Pointer(p)).FpEList = pNew
	}
	if (*TSelect)(unsafe.Pointer(p)).FpEList != 0 {
		if (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr > **(**int32)(__ccgo_up(db + 120 + 2*4)) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22611, 0)
			return int32(WRC_Abort)
		}
		if elistFlags&uint32(libc.Int32FromInt32(EP_HasFunc)|libc.Int32FromInt32(EP_Subquery)) != uint32(0) {
			**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_ComplexResult)
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** This routine generates the code for the inside of the inner loop
//	** of a SELECT.
//	**
//	** If srcTab is negative, then the p->pEList expressions
//	** are evaluated in order to get the data for this row.  If srcTab is
//	** zero or more, then data is pulled from srcTab and p->pEList is used only
//	** to get the number of columns and the collation sequence for each column.
//	*/
func _selectInnerLoop(tls *libc.TLS, pParse uintptr, p uintptr, srcTab int32, pSort uintptr, pDistinct uintptr, pDest uintptr, iContinue int32, iBreak int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var addr, addrTest, eDest, eType, hasDistinct, i, i2, iParm, iTab, j, nKey, nPrefixReg, nResultCol, r1, r11, r12, r13, r2, r21, r3, regOrig, regResult, v1 int32
	var ecelFlags Tu8
	var pEList, pSO, v uintptr
	var _ /* sRowLoadInfo at bp+0 */ TRowLoadInfo
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrTest, eDest, eType, ecelFlags, hasDistinct, i, i2, iParm, iTab, j, nKey, nPrefixReg, nResultCol, pEList, pSO, r1, r11, r12, r13, r2, r21, r3, regOrig, regResult, v, v1
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe                /* True if the DISTINCT keyword is present */
	eDest = int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) /* How to dispose of results */
	iParm = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm      /* Number of result columns */
	nPrefixReg = 0                                              /* Start of memory holding full result (or 0) */
	if pDistinct != 0 {
		v1 = int32((*TDistinctCtx)(unsafe.Pointer(pDistinct)).FeTnctType)
	} else {
		v1 = WHERE_DISTINCT_NOOP
	}
	hasDistinct = v1
	if pSort != 0 && (*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy == uintptr(0) {
		pSort = uintptr(0)
	}
	if pSort == uintptr(0) && !(hasDistinct != 0) {
		_codeOffset(tls, v, (*TSelect)(unsafe.Pointer(p)).FiOffset, iContinue)
	}
	/* Pull the requested columns.
	 */
	nResultCol = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr
	if (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst == 0 {
		if pSort != 0 {
			nPrefixReg = (*TExprList)(unsafe.Pointer((*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy)).FnExpr
			if !(int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&libc.Int32FromInt32(SORTFLAG_UseSorter) != 0) {
				nPrefixReg = nPrefixReg + 1
			}
			**(**int32)(__ccgo_up(pParse + 48)) += nPrefixReg
		}
		(*TSelectDest)(unsafe.Pointer(pDest)).FiSdst = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += nResultCol
	} else {
		if (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst+nResultCol > (*TParse)(unsafe.Pointer(pParse)).FnMem {
			/* This is an error condition that can result, for example, when a SELECT
			 ** on the right-hand side of an INSERT contains more result columns than
			 ** there are columns in the table on the left.  The error will be caught
			 ** and reported later.  But we need to make sure enough memory is allocated
			 ** to avoid other spurious errors in the meantime. */
			**(**int32)(__ccgo_up(pParse + 48)) += nResultCol
		}
	}
	(*TSelectDest)(unsafe.Pointer(pDest)).FnSdst = nResultCol
	v1 = (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst
	regResult = v1
	regOrig = v1
	if srcTab >= 0 {
		i = 0
		for {
			if !(i < nResultCol) {
				break
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), srcTab, i, regResult+i)
			goto _3
		_3:
			;
			i = i + 1
		}
	} else {
		if eDest != int32(SRT_Exists) {
			if eDest == int32(SRT_Mem) || eDest == int32(SRT_Output) || eDest == int32(SRT_Coroutine) {
				ecelFlags = uint8(SQLITE_ECEL_DUP)
			} else {
				ecelFlags = uint8(0)
			}
			if pSort != 0 && hasDistinct == 0 && eDest != int32(SRT_EphemTab) && eDest != int32(SRT_Table) {
				/* For each expression in p->pEList that is a copy of an expression in
				 ** the ORDER BY clause (pSort->pOrderBy), set the associated
				 ** iOrderByCol value to one more than the index of the ORDER BY
				 ** expression within the sort-key that pushOntoSorter() will generate.
				 ** This allows the p->pEList field to be omitted from the sorted record,
				 ** saving space and CPU cycles.  */
				ecelFlags = uint8(int32(ecelFlags) | (libc.Int32FromInt32(SQLITE_ECEL_OMITREF) | libc.Int32FromInt32(SQLITE_ECEL_REF)))
				i = (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat
				for {
					if !(i < (*TExprList)(unsafe.Pointer((*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy)).FnExpr) {
						break
					}
					v1 = int32(*(*Tu16)(unsafe.Pointer((*TSortCtx)(unsafe.Pointer(pSort)).FpOrderBy + 8 + uintptr(i)*20 + 16)))
					j = v1
					if v1 > 0 {
						*(*Tu16)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8 + uintptr(j-int32(1))*20 + 16)) = uint16(i + int32(1) - (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat)
					}
					goto _4
				_4:
					;
					i = i + 1
				}
				/* Adjust nResultCol to account for columns that are omitted
				 ** from the sorter by the optimizations in this branch */
				pEList = (*TSelect)(unsafe.Pointer(p)).FpEList
				i = 0
				for {
					if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
						break
					}
					if int32(*(*Tu16)(unsafe.Pointer(pEList + 8 + uintptr(i)*20 + 16))) > 0 {
						nResultCol = nResultCol - 1
						regOrig = 0
					}
					goto _6
				_6:
					;
					i = i + 1
				}
			}
			(**(**TRowLoadInfo)(__ccgo_up(bp))).FregResult = regResult
			(**(**TRowLoadInfo)(__ccgo_up(bp))).FecelFlags = ecelFlags
			if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 && int32(ecelFlags)&int32(SQLITE_ECEL_OMITREF) != 0 && nPrefixReg > 0 {
				(*TSortCtx)(unsafe.Pointer(pSort)).FpDeferredRowLoad = bp
				regOrig = 0
			} else {
				_innerLoopLoadRow(tls, pParse, p, bp)
			}
		}
	}
	/* If the DISTINCT keyword was present on the SELECT statement
	 ** and this row has been seen before, then do not make this row
	 ** part of the result.
	 */
	if hasDistinct != 0 {
		eType = int32((*TDistinctCtx)(unsafe.Pointer(pDistinct)).FeTnctType)
		iTab = (*TDistinctCtx)(unsafe.Pointer(pDistinct)).FtabTnct
		iTab = _codeDistinct(tls, pParse, eType, iTab, iContinue, (*TSelect)(unsafe.Pointer(p)).FpEList, regResult)
		_fixDistinctOpenEph(tls, pParse, eType, iTab, (*TDistinctCtx)(unsafe.Pointer(pDistinct)).FaddrTnct)
		if pSort == uintptr(0) {
			_codeOffset(tls, v, (*TSelect)(unsafe.Pointer(p)).FiOffset, iContinue)
		}
	}
	switch eDest {
	/* Store the result as data using a unique key.
	 */
	case int32(SRT_Fifo):
		fallthrough
	case int32(SRT_DistFifo):
		fallthrough
	case int32(SRT_Table):
		fallthrough
	case int32(SRT_EphemTab):
		r1 = _sqlite3GetTempRange(tls, pParse, nPrefixReg+int32(1))
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regResult, nResultCol, r1+nPrefixReg)
		if eDest == int32(SRT_DistFifo) {
			/* If the destination is DistFifo, then cursor (iParm+1) is open
			 ** on an ephemeral index. If the current row is already present
			 ** in the index, do not write it to the output. If not, add the
			 ** current row to the index and proceed with writing it to the
			 ** output table as well.  */
			addr = _sqlite3VdbeCurrentAddr(tls, v) + int32(4)
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), iParm+int32(1), addr, r1, 0)
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm+int32(1), r1, regResult, nResultCol)
		}
		if pSort != 0 {
			_pushOntoSorter(tls, pParse, pSort, p, r1+nPrefixReg, regOrig, int32(1), nPrefixReg)
		} else {
			r2 = _sqlite3GetTempReg(tls, pParse)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iParm, r2)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iParm, r1, r2)
			_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND))
			_sqlite3ReleaseTempReg(tls, pParse, r2)
		}
		_sqlite3ReleaseTempRange(tls, pParse, r1, nPrefixReg+int32(1))
	case int32(SRT_Upfrom):
		if pSort != 0 {
			_pushOntoSorter(tls, pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg)
		} else {
			i2 = (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2
			r11 = _sqlite3GetTempReg(tls, pParse)
			/* If the UPDATE FROM join is an aggregate that matches no rows, it
			 ** might still be trying to return one row, because that is what
			 ** aggregates do.  Don't record that empty row in the output table. */
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regResult, iBreak)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regResult+libc.BoolInt32(i2 < 0), nResultCol-libc.BoolInt32(i2 < 0), r11)
			if i2 < 0 {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iParm, r11, regResult)
			} else {
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, r11, regResult, i2)
			}
		}
		break
		/* If we are creating a set for an "expr IN (SELECT ...)" construct,
		 ** then there should be a single item on the stack.  Write this
		 ** item into the set table with bogus data.
		 */
		fallthrough
	case int32(SRT_Set):
		if pSort != 0 {
			/* At first glance you would think we could optimize out the
			 ** ORDER BY in this case since the order of entries in the set
			 ** does not matter.  But there might be a LIMIT clause, in which
			 ** case the order does matter */
			_pushOntoSorter(tls, pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg)
			(*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2 = 0 /* Signal that any Bloom filter is unpopulated */
		} else {
			r12 = _sqlite3GetTempReg(tls, pParse)
			_sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regResult, nResultCol, r12, (*TSelectDest)(unsafe.Pointer(pDest)).FzAffSdst, nResultCol)
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, r12, regResult, nResultCol)
			if (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2 != 0 {
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm2, 0, regResult, nResultCol)
				_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+21699, 0)
			}
			_sqlite3ReleaseTempReg(tls, pParse, r12)
		}
		break
		/* If any row exist in the result set, record that fact and abort.
		 */
		fallthrough
	case int32(SRT_Exists):
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), iParm)
		/* The LIMIT clause will terminate the loop for us */
		break
		/* If this is a scalar select that is part of an expression, then
		 ** store the results in the appropriate memory cell or array of
		 ** memory cells and break out of the scan loop.
		 */
		fallthrough
	case int32(SRT_Mem):
		if pSort != 0 {
			_pushOntoSorter(tls, pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg)
			(*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm = regResult
		} else {
			if regResult != iParm {
				/* This occurs in cases where the SELECT had both a DISTINCT and
				 ** an OFFSET clause.  */
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regResult, iParm, nResultCol-int32(1))
			}
			/* The LIMIT clause will jump out of the loop for us */
		}
	case int32(SRT_Coroutine): /* Send data to a co-routine */
		fallthrough
	case int32(SRT_Output): /* Return the results */
		if pSort != 0 {
			_pushOntoSorter(tls, pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg)
		} else {
			if eDest == int32(SRT_Coroutine) {
				_sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm)
			} else {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), regResult, nResultCol)
			}
		}
		break
		/* Write the results into a priority queue that is order according to
		 ** pDest->pOrderBy (in pSO).  pDest->iSDParm (in iParm) is the cursor for an
		 ** index with pSO->nExpr+2 columns.  Build a key using pSO for the first
		 ** pSO->nExpr columns, then make sure all keys are unique by adding a
		 ** final OP_Sequence column.  The last column is the record as a blob.
		 */
		fallthrough
	case int32(SRT_DistQueue):
		fallthrough
	case int32(SRT_Queue):
		addrTest = 0
		pSO = (*TSelectDest)(unsafe.Pointer(pDest)).FpOrderBy
		nKey = (*TExprList)(unsafe.Pointer(pSO)).FnExpr
		r13 = _sqlite3GetTempReg(tls, pParse)
		r21 = _sqlite3GetTempRange(tls, pParse, nKey+int32(2))
		r3 = r21 + nKey + int32(1)
		if eDest == int32(SRT_DistQueue) {
			/* If the destination is DistQueue, then cursor (iParm+1) is open
			 ** on a second ephemeral index that holds all values every previously
			 ** added to the queue. */
			addrTest = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), iParm+int32(1), 0, regResult, nResultCol)
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regResult, nResultCol, r3)
		if eDest == int32(SRT_DistQueue) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), iParm+int32(1), r3)
			_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT))
		}
		i = 0
		for {
			if !(i < nKey) {
				break
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), regResult+int32(*(*Tu16)(unsafe.Pointer(pSO + 8 + uintptr(i)*20 + 16)))-int32(1), r21+i)
			goto _7
		_7:
			;
			i = i + 1
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Sequence), iParm, r21+nKey)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), r21, nKey+int32(2), r13)
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iParm, r13, r21, nKey+int32(2))
		if addrTest != 0 {
			_sqlite3VdbeJumpHere(tls, v, addrTest)
		}
		_sqlite3ReleaseTempReg(tls, pParse, r13)
		_sqlite3ReleaseTempRange(tls, pParse, r21, nKey+int32(2))
		break
		/* Discard the results.  This is used for SELECT statements inside
		 ** the body of a TRIGGER.  The purpose of such selects is to call
		 ** user-defined functions that have side effects.  We do not care
		 ** about the actual results of the select.
		 */
		fallthrough
	default:
		break
	}
	/* Jump to the end of the loop if the LIMIT is reached.  Except, if
	 ** there is a sorter, in which case the sorter has already limited
	 ** the output for us.
	 */
	if pSort == uintptr(0) && (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_DecrJumpZero), (*TSelect)(unsafe.Pointer(p)).FiLimit, iBreak)
	}
}

// C documentation
//
//	/*
//	** Walker SELECT callbacks for sqlite3ReferencesSrcList().
//	**
//	** When entering a new subquery on the pExpr argument, add all FROM clause
//	** entries for that subquery to the exclude list.
//	**
//	** When leaving the subquery, remove those entries from the exclude list.
//	*/
func _selectRefEnter(tls *libc.TLS, pWalker uintptr, pSelect uintptr) (r int32) {
	var i, j Ti64
	var p, pSrc, piNew uintptr
	_, _, _, _, _ = i, j, p, pSrc, piNew
	p = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	pSrc = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc
	if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc == 0 {
		return WRC_Continue
	}
	j = (*TRefSrcList)(unsafe.Pointer(p)).FnExclude
	**(**Ti64)(__ccgo_up(p + 8)) += int64((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc)
	piNew = _sqlite3DbRealloc(tls, (*TRefSrcList)(unsafe.Pointer(p)).Fdb, (*TRefSrcList)(unsafe.Pointer(p)).FaiExclude, uint64((*TRefSrcList)(unsafe.Pointer(p)).FnExclude*int64(4)))
	if piNew == uintptr(0) {
		(*TRefSrcList)(unsafe.Pointer(p)).FnExclude = 0
		return int32(WRC_Abort)
	} else {
		(*TRefSrcList)(unsafe.Pointer(p)).FaiExclude = piNew
	}
	i = 0
	for {
		if !(i < int64((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc)) {
			break
		}
		**(**int32)(__ccgo_up((*TRefSrcList)(unsafe.Pointer(p)).FaiExclude + uintptr(j)*4)) = (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48))).FiCursor
		goto _1
	_1:
		;
		i = i + 1
		j = j + 1
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Callback function used by selectWindowRewriteEList(). If necessary,
//	** this function appends to the output expression-list and updates
//	** expression (*ppExpr) in place.
//	*/
func _selectWindowRewriteExprCb(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var f, i, i1, iCol, nSrc, v4 int32
	var p, pDup, pParse, pWin uintptr
	_, _, _, _, _, _, _, _, _, _ = f, i, i1, iCol, nSrc, p, pDup, pParse, pWin, v4
	p = *(*uintptr)(unsafe.Pointer(pWalker + 24))
	pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse
	/* If this function is being called from within a scalar sub-select
	 ** that used by the SELECT statement being processed, only process
	 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
	 ** not process aggregates or window functions at all, as they belong
	 ** to the scalar sub-select.  */
	if (*TWindowRewrite)(unsafe.Pointer(p)).FpSubSelect != 0 {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) {
			return WRC_Continue
		} else {
			nSrc = (*TSrcList)(unsafe.Pointer((*TWindowRewrite)(unsafe.Pointer(p)).FpSrc)).FnSrc
			i = 0
			for {
				if !(i < nSrc) {
					break
				}
				if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*(*TSrcItem)(unsafe.Pointer((*TWindowRewrite)(unsafe.Pointer(p)).FpSrc + 8 + uintptr(i)*48))).FiCursor {
					break
				}
				goto _1
			_1:
				;
				i = i + 1
			}
			if i == nSrc {
				return WRC_Continue
			}
		}
	}
	switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) {
	case int32(TK_FUNCTION):
		if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != libc.Uint32FromInt32(0)) {
			break
		} else {
			pWin = (*TWindowRewrite)(unsafe.Pointer(p)).FpWin
			for {
				if !(pWin != 0) {
					break
				}
				if *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)) == pWin {
					return int32(WRC_Prune)
				}
				goto _2
			_2:
				;
				pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin
			}
		}
		fallthrough
	case int32(TK_IF_NULL_ROW):
		fallthrough
	case int32(TK_AGG_FUNCTION):
		fallthrough
	case int32(TK_COLUMN):
		iCol = -int32(1)
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
			return int32(WRC_Abort)
		}
		if (*TWindowRewrite)(unsafe.Pointer(p)).FpSub != 0 {
			i1 = 0
			for {
				if !(i1 < (*TExprList)(unsafe.Pointer((*TWindowRewrite)(unsafe.Pointer(p)).FpSub)).FnExpr) {
					break
				}
				if 0 == _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer((*TWindowRewrite)(unsafe.Pointer(p)).FpSub + 8 + uintptr(i1)*20))).FpExpr, pExpr, -int32(1)) {
					iCol = i1
					break
				}
				goto _3
			_3:
				;
				i1 = i1 + 1
			}
		}
		if iCol < 0 {
			pDup = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, 0)
			if pDup != 0 && int32((*TExpr)(unsafe.Pointer(pDup)).Fop) == int32(TK_AGG_FUNCTION) {
				(*TExpr)(unsafe.Pointer(pDup)).Fop = uint8(TK_FUNCTION)
			}
			(*TWindowRewrite)(unsafe.Pointer(p)).FpSub = _sqlite3ExprListAppend(tls, pParse, (*TWindowRewrite)(unsafe.Pointer(p)).FpSub, pDup)
		}
		if (*TWindowRewrite)(unsafe.Pointer(p)).FpSub != 0 {
			f = int32((*TExpr)(unsafe.Pointer(pExpr)).Fflags & uint32(EP_Collate))
			**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Static))
			_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr)
			**(**Tu32)(__ccgo_up(pExpr + 4)) &= ^uint32(libc.Int32FromInt32(EP_Static))
			libc.Xmemset(tls, pExpr, 0, uint32(52))
			(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_COLUMN)
			if iCol < 0 {
				v4 = (*TExprList)(unsafe.Pointer((*TWindowRewrite)(unsafe.Pointer(p)).FpSub)).FnExpr - int32(1)
			} else {
				v4 = iCol
			}
			(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(v4)
			(*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TWindow)(unsafe.Pointer((*TWindowRewrite)(unsafe.Pointer(p)).FpWin)).FiEphCsr
			(*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab = (*TWindowRewrite)(unsafe.Pointer(p)).FpTab
			(*TExpr)(unsafe.Pointer(pExpr)).Fflags = uint32(f)
		}
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
			return int32(WRC_Abort)
		}
	default: /* no-op */
		break
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Deserialize the data blob pointed to by buf as serial type serial_type
//	** and store the result in pMem.
//	**
//	** This function is implemented as two separate routines for performance.
//	** The few cases that require local variables are broken out into a separate
//	** routine so that in most cases the overhead of moving the stack pointer
//	** is avoided.
//	*/
func _serialGet(tls *libc.TLS, buf uintptr, serial_type Tu32, pMem uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var y Tu32
	var v1 int32
	var _ /* x at bp+0 */ Tu64
	_, _ = y, v1
	**(**Tu64)(__ccgo_up(bp)) = uint64(uint32(**(**uint8)(__ccgo_up(buf)))<<libc.Int32FromInt32(24) | uint32(int32(**(**uint8)(__ccgo_up(buf + 1)))<<libc.Int32FromInt32(16)) | uint32(int32(**(**uint8)(__ccgo_up(buf + 2)))<<libc.Int32FromInt32(8)) | uint32(**(**uint8)(__ccgo_up(buf + 3))))
	y = uint32(**(**uint8)(__ccgo_up(buf + libc.UintptrFromInt32(4))))<<libc.Int32FromInt32(24) | uint32(int32(**(**uint8)(__ccgo_up(buf + libc.UintptrFromInt32(4) + 1)))<<libc.Int32FromInt32(16)) | uint32(int32(**(**uint8)(__ccgo_up(buf + libc.UintptrFromInt32(4) + 2)))<<libc.Int32FromInt32(8)) | uint32(**(**uint8)(__ccgo_up(buf + libc.UintptrFromInt32(4) + 3)))
	**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(32) + uint64(y)
	if serial_type == uint32(6) {
		/* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
		 ** twos-complement integer. */
		*(*Ti64)(unsafe.Pointer(pMem)) = **(**Ti64)(__ccgo_up(bp))
		(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Int)
	} else {
		/* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
		 ** floating point number. */
		libc.Xmemcpy(tls, pMem, bp, uint32(8))
		if **(**Tu64)(__ccgo_up(bp))&(libc.Uint64FromInt32(0x7ff)<<libc.Int32FromInt32(52)) == libc.Uint64FromInt32(0x7ff)<<libc.Int32FromInt32(52) && **(**Tu64)(__ccgo_up(bp))&(libc.Uint64FromInt32(1)<<libc.Int32FromInt32(52)-libc.Uint64FromInt32(1)) != uint64(0) {
			v1 = int32(MEM_Null)
		} else {
			v1 = int32(MEM_Real)
		}
		(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(v1)
	}
}

func _serialGet7(tls *libc.TLS, buf uintptr, pMem uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var y Tu32
	var _ /* x at bp+0 */ Tu64
	_ = y
	**(**Tu64)(__ccgo_up(bp)) = uint64(uint32(**(**uint8)(__ccgo_up(buf)))<<libc.Int32FromInt32(24) | uint32(int32(**(**uint8)(__ccgo_up(buf + 1)))<<libc.Int32FromInt32(16)) | uint32(int32(**(**uint8)(__ccgo_up(buf + 2)))<<libc.Int32FromInt32(8)) | uint32(**(**uint8)(__ccgo_up(buf + 3))))
	y = uint32(**(**uint8)(__ccgo_up(buf + libc.UintptrFromInt32(4))))<<libc.Int32FromInt32(24) | uint32(int32(**(**uint8)(__ccgo_up(buf + libc.UintptrFromInt32(4) + 1)))<<libc.Int32FromInt32(16)) | uint32(int32(**(**uint8)(__ccgo_up(buf + libc.UintptrFromInt32(4) + 2)))<<libc.Int32FromInt32(8)) | uint32(**(**uint8)(__ccgo_up(buf + libc.UintptrFromInt32(4) + 3)))
	**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))<<libc.Int32FromInt32(32) + uint64(y)
	libc.Xmemcpy(tls, pMem, bp, uint32(8))
	if **(**Tu64)(__ccgo_up(bp))&(libc.Uint64FromInt32(0x7ff)<<libc.Int32FromInt32(52)) == libc.Uint64FromInt32(0x7ff)<<libc.Int32FromInt32(52) && **(**Tu64)(__ccgo_up(bp))&(libc.Uint64FromInt32(1)<<libc.Int32FromInt32(52)-libc.Uint64FromInt32(1)) != uint64(0) {
		(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Null)
		return int32(1)
	}
	(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(MEM_Real)
	return 0
}

// C documentation
//
//	/*
//	** Return a comma-separated list of the fully-qualified (with both database
//	** and table name) column names from table pTab. e.g.
//	**
//	**    "main"."t1"."a", "main"."t1"."b", "main"."t1"."c"
//	*/
func _sessionAllCols(tls *libc.TLS, zDb uintptr, pTab uintptr) (r uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var ii int32
	var zRet, v2 uintptr
	_, _, _ = ii, zRet, v2
	zRet = uintptr(0)
	ii = 0
	for {
		if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
			break
		}
		if zRet != 0 {
			v2 = __ccgo_ts + 17436
		} else {
			v2 = __ccgo_ts + 1711
		}
		zRet = Xsqlite3_mprintf(tls, __ccgo_ts+36768, libc.VaList(bp+8, zRet, v2, zDb, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FazCol + uintptr(ii)*4))))
		if !(zRet != 0) {
			break
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	return zRet
}

// C documentation
//
//	/*
//	** This function is a no-op if *pRc is other than SQLITE_OK when it is
//	** called. Otherwise, append a blob of data to the buffer.
//	**
//	** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
//	** returning.
//	*/
func _sessionAppendBlob(tls *libc.TLS, p uintptr, aBlob uintptr, nBlob int32, pRc uintptr) {
	if nBlob > 0 && 0 == _sessionBufferGrow(tls, p, int64(nBlob), pRc) {
		libc.Xmemcpy(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), aBlob, uint32(nBlob))
		**(**int32)(__ccgo_up(p + 4)) += nBlob
	}
}

// C documentation
//
//	/*
//	** Append a DELETE change to the buffer passed as the first argument. Use
//	** the changeset format if argument bPatchset is zero, or the patchset
//	** format otherwise.
//	*/
func _sessionAppendDelete(tls *libc.TLS, pBuf uintptr, bPatchset int32, p uintptr, nCol int32, abPK uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a, pStart, v2 uintptr
	var eType, i int32
	var _ /* n at bp+4 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _ = a, eType, i, pStart, v2
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	_sessionAppendByte(tls, pBuf, uint8(SQLITE_DELETE), bp)
	_sessionAppendByte(tls, pBuf, (*TSessionChange)(unsafe.Pointer(p)).FbIndirect, bp)
	if bPatchset == 0 {
		_sessionAppendBlob(tls, pBuf, (*TSessionChange)(unsafe.Pointer(p)).FaRecord, (*TSessionChange)(unsafe.Pointer(p)).FnRecord, bp)
	} else {
		a = (*TSessionChange)(unsafe.Pointer(p)).FaRecord
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			pStart = a
			v2 = a
			a = a + 1
			eType = int32(**(**Tu8)(__ccgo_up(v2)))
			switch eType {
			case 0:
				fallthrough
			case int32(SQLITE_NULL):
			case int32(SQLITE_FLOAT):
				fallthrough
			case int32(SQLITE_INTEGER):
				a = a + uintptr(8)
			default:
				a = a + uintptr(_sessionVarintGet(tls, a, bp+4))
				a = a + uintptr(**(**int32)(__ccgo_up(bp + 4)))
				break
			}
			if **(**Tu8)(__ccgo_up(abPK + uintptr(i))) != 0 {
				_sessionAppendBlob(tls, pBuf, pStart, int32(a)-int32(pStart), bp)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** This function is a no-op if *pRc is other than SQLITE_OK when it is
//	** called. Otherwise, append the string representation of integer iVal
//	** to the buffer. No nul-terminator is written.
//	**
//	** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
//	** returning.
//	*/
func _sessionAppendInteger(tls *libc.TLS, p uintptr, iVal int32, pRc uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var _ /* aBuf at bp+0 */ [24]int8
	Xsqlite3_snprintf(tls, int32(libc.Uint32FromInt64(24)-libc.Uint32FromInt32(1)), bp, __ccgo_ts+6506, libc.VaList(bp+32, iVal))
	_sessionAppendStr(tls, p, bp, pRc)
}

// C documentation
//
//	/*
//	** This function is called when rebasing a local UPDATE change against one
//	** or more remote UPDATE changes. The aRec/nRec buffer contains the current
//	** old.* and new.* records for the change. The rebase buffer (a single
//	** record) is in aChange/nChange. The rebased change is appended to buffer
//	** pBuf.
//	**
//	** Rebasing the UPDATE involves:
//	**
//	**   * Removing any changes to fields for which the corresponding field
//	**     in the rebase buffer is set to "replaced" (type 0xFF). If this
//	**     means the UPDATE change updates no fields, nothing is appended
//	**     to the output buffer.
//	**
//	**   * For each field modified by the local change for which the
//	**     corresponding field in the rebase buffer is not "undefined" (0x00)
//	**     or "replaced" (0xFF), the old.* value is replaced by the value
//	**     in the rebase buffer.
//	*/
func _sessionAppendPartialUpdate(tls *libc.TLS, pBuf uintptr, pIter uintptr, aRec uintptr, nRec int32, aChange uintptr, nChange int32, pRc uintptr) {
	var a1, a2, pOut, v1 uintptr
	var bData, i, n1, n11, n2, n21 int32
	_, _, _, _, _, _, _, _, _, _ = a1, a2, bData, i, n1, n11, n2, n21, pOut, v1
	_sessionBufferGrow(tls, pBuf, libc.Int64FromInt32(2)+int64(nRec)+int64(nChange), pRc)
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK {
		bData = 0
		pOut = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf)
		a1 = aRec
		a2 = aChange
		v1 = pOut
		pOut = pOut + 1
		**(**Tu8)(__ccgo_up(v1)) = uint8(SQLITE_UPDATE)
		v1 = pOut
		pOut = pOut + 1
		**(**Tu8)(__ccgo_up(v1)) = uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect)
		i = 0
		for {
			if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) {
				break
			}
			n1 = _sessionSerialLen(tls, a1)
			n2 = _sessionSerialLen(tls, a2)
			if **(**Tu8)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK + uintptr(i))) != 0 || int32(**(**Tu8)(__ccgo_up(a2))) == 0 {
				if !(**(**Tu8)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK + uintptr(i))) != 0) && **(**Tu8)(__ccgo_up(a1)) != 0 {
					bData = int32(1)
				}
				libc.Xmemcpy(tls, pOut, a1, uint32(n1))
				pOut = pOut + uintptr(n1)
			} else {
				if int32(**(**Tu8)(__ccgo_up(a2))) != int32(0xFF) && **(**Tu8)(__ccgo_up(a1)) != 0 {
					bData = int32(1)
					libc.Xmemcpy(tls, pOut, a2, uint32(n2))
					pOut = pOut + uintptr(n2)
				} else {
					v1 = pOut
					pOut = pOut + 1
					**(**Tu8)(__ccgo_up(v1)) = uint8('\000')
				}
			}
			a1 = a1 + uintptr(n1)
			a2 = a2 + uintptr(n2)
			goto _3
		_3:
			;
			i = i + 1
		}
		if bData != 0 {
			a2 = aChange
			i = 0
			for {
				if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) {
					break
				}
				n11 = _sessionSerialLen(tls, a1)
				n21 = _sessionSerialLen(tls, a2)
				if **(**Tu8)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK + uintptr(i))) != 0 || int32(**(**Tu8)(__ccgo_up(a2))) != int32(0xFF) {
					libc.Xmemcpy(tls, pOut, a1, uint32(n11))
					pOut = pOut + uintptr(n11)
				} else {
					v1 = pOut
					pOut = pOut + 1
					**(**Tu8)(__ccgo_up(v1)) = uint8('\000')
				}
				a1 = a1 + uintptr(n11)
				a2 = a2 + uintptr(n21)
				goto _5
			_5:
				;
				i = i + 1
			}
			(*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf = int32(pOut) - int32((*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf)
		}
	}
}

// C documentation
//
//	/*
//	** Buffers a1 and a2 must both contain a sessions module record nCol
//	** fields in size. This function appends an nCol sessions module
//	** record to buffer pBuf that is a copy of a1, except that for
//	** each field that is undefined in a1[], swap in the field from a2[].
//	*/
func _sessionAppendRecordMerge(tls *libc.TLS, pBuf uintptr, nCol int32, a1 uintptr, n1 int32, a2 uintptr, n2 int32, pRc uintptr) {
	var a1Eof, a2Eof, pOut uintptr
	var i, nn1, nn2, v2, v3 int32
	_, _, _, _, _, _, _, _ = a1Eof, a2Eof, i, nn1, nn2, pOut, v2, v3
	a1Eof = a1 + uintptr(n1)
	a2Eof = a2 + uintptr(n2)
	_sessionBufferGrow(tls, pBuf, int64(n1)+int64(n2), pRc)
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK {
		pOut = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf)
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			if a1 < a1Eof {
				v2 = _sessionSerialLen(tls, a1)
			} else {
				v2 = 0
			}
			nn1 = v2
			if a2 < a2Eof {
				v3 = _sessionSerialLen(tls, a2)
			} else {
				v3 = 0
			}
			nn2 = v3
			if nn1 == 0 || nn2 > 0 && (int32(**(**Tu8)(__ccgo_up(a1))) == 0 || int32(**(**Tu8)(__ccgo_up(a1))) == int32(0xFF)) {
				libc.Xmemcpy(tls, pOut, a2, uint32(nn2))
				pOut = pOut + uintptr(nn2)
			} else {
				libc.Xmemcpy(tls, pOut, a1, uint32(nn1))
				pOut = pOut + uintptr(nn1)
			}
			a1 = a1 + uintptr(nn1)
			a2 = a2 + uintptr(nn2)
			goto _1
		_1:
			;
			i = i + 1
		}
		(*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf = int32(pOut) - int32((*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf)
	}
}

// C documentation
//
//	/*
//	** This function is a no-op if *pRc is other than SQLITE_OK when it is
//	** called. Otherwise, append a string to the buffer. All bytes in the string
//	** up to (but not including) the nul-terminator are written to the buffer.
//	**
//	** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
//	** returning.
//	*/
func _sessionAppendStr(tls *libc.TLS, p uintptr, zStr uintptr, pRc uintptr) {
	var nStr int32
	_ = nStr
	nStr = _sqlite3Strlen30(tls, zStr)
	if 0 == _sessionBufferGrow(tls, p, int64(nStr)+int64(1), pRc) {
		libc.Xmemcpy(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), zStr, uint32(nStr))
		**(**int32)(__ccgo_up(p + 4)) += nStr
		**(**Tu8)(__ccgo_up((*TSessionBuffer)(unsafe.Pointer(p)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf))) = uint8(0x00)
	}
}

// C documentation
//
//	/*
//	**
//	** This function appends an update change to the buffer (see the comments
//	** under "CHANGESET FORMAT" at the top of the file). An update change
//	** consists of:
//	**
//	**   1 byte:  SQLITE_UPDATE (0x17)
//	**   n bytes: old.* record (see RECORD FORMAT)
//	**   m bytes: new.* record (see RECORD FORMAT)
//	**
//	** The SessionChange object passed as the third argument contains the
//	** values that were stored in the row when the session began (the old.*
//	** values). The statement handle passed as the second argument points
//	** at the current version of the row (the new.* values).
//	**
//	** If all of the old.* values are equal to their corresponding new.* value
//	** (i.e. nothing has changed), then no data at all is appended to the buffer.
//	**
//	** Otherwise, the old.* record contains all primary key values and the
//	** original values of any fields that have been modified. The new.* record
//	** contains the new values of only those fields that have been modified.
//	*/
func _sessionAppendUpdate(tls *libc.TLS, pBuf uintptr, bPatchset int32, pStmt uintptr, p uintptr, abPK uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var bChanged, bNoop, eType, i, nAdvance, nHdr, nRewind int32
	var pCsr uintptr
	var _ /* buf2 at bp+4 */ TSessionBuffer
	var _ /* dVal at bp+24 */ float64
	var _ /* iVal at bp+16 */ Tsqlite3_int64
	var _ /* n at bp+32 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _ = bChanged, bNoop, eType, i, nAdvance, nHdr, nRewind, pCsr
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	**(**TSessionBuffer)(__ccgo_up(bp + 4)) = TSessionBuffer{} /* Buffer to accumulate new.* record in */
	bNoop = int32(1)                                           /* Set to zero if any values are modified */
	nRewind = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf    /* Used to iterate through columns */
	pCsr = (*TSessionChange)(unsafe.Pointer(p)).FaRecord       /* Used to iterate through old.* values */
	_sessionAppendByte(tls, pBuf, uint8(SQLITE_UPDATE), bp)
	_sessionAppendByte(tls, pBuf, (*TSessionChange)(unsafe.Pointer(p)).FbIndirect, bp)
	i = 0
	for {
		if !(i < Xsqlite3_column_count(tls, pStmt)) {
			break
		}
		bChanged = 0
		eType = int32(**(**Tu8)(__ccgo_up(pCsr)))
		switch eType {
		case int32(SQLITE_NULL):
			nAdvance = int32(1)
			if Xsqlite3_column_type(tls, pStmt, i) != int32(SQLITE_NULL) {
				bChanged = int32(1)
			}
		case int32(SQLITE_FLOAT):
			fallthrough
		case int32(SQLITE_INTEGER):
			nAdvance = int32(9)
			if eType == Xsqlite3_column_type(tls, pStmt, i) {
				**(**Tsqlite3_int64)(__ccgo_up(bp + 16)) = _sessionGetI64(tls, pCsr+1)
				if eType == int32(SQLITE_INTEGER) {
					if **(**Tsqlite3_int64)(__ccgo_up(bp + 16)) == Xsqlite3_column_int64(tls, pStmt, i) {
						break
					}
				} else {
					libc.Xmemcpy(tls, bp+24, bp+16, uint32(8))
					if **(**float64)(__ccgo_up(bp + 24)) == Xsqlite3_column_double(tls, pStmt, i) {
						break
					}
				}
			}
			bChanged = int32(1)
		default:
			nHdr = int32(1) + _sessionVarintGet(tls, pCsr+1, bp+32)
			nAdvance = nHdr + **(**int32)(__ccgo_up(bp + 32))
			if eType == Xsqlite3_column_type(tls, pStmt, i) && **(**int32)(__ccgo_up(bp + 32)) == Xsqlite3_column_bytes(tls, pStmt, i) && (**(**int32)(__ccgo_up(bp + 32)) == 0 || 0 == libc.Xmemcmp(tls, pCsr+uintptr(nHdr), Xsqlite3_column_blob(tls, pStmt, i), uint32(**(**int32)(__ccgo_up(bp + 32))))) {
				break
			}
			bChanged = int32(1)
		}
		/* If at least one field has been modified, this is not a no-op. */
		if bChanged != 0 {
			bNoop = 0
		}
		/* Add a field to the old.* record. This is omitted if this module is
		 ** currently generating a patchset. */
		if bPatchset == 0 {
			if bChanged != 0 || **(**Tu8)(__ccgo_up(abPK + uintptr(i))) != 0 {
				_sessionAppendBlob(tls, pBuf, pCsr, nAdvance, bp)
			} else {
				_sessionAppendByte(tls, pBuf, uint8(0), bp)
			}
		}
		/* Add a field to the new.* record. Or the only record if currently
		 ** generating a patchset.  */
		if bChanged != 0 || bPatchset != 0 && **(**Tu8)(__ccgo_up(abPK + uintptr(i))) != 0 {
			_sessionAppendCol(tls, bp+4, pStmt, i, bp)
		} else {
			_sessionAppendByte(tls, bp+4, uint8(0), bp)
		}
		pCsr = pCsr + uintptr(nAdvance)
		goto _1
	_1:
		;
		i = i + 1
	}
	if bNoop != 0 {
		(*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf = nRewind
	} else {
		_sessionAppendBlob(tls, pBuf, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FnBuf, bp)
	}
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf)
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Arguments aLeft and aRight are pointers to change records for table pTab.
//	** This function returns true if the two records apply to the same row (i.e.
//	** have the same values stored in the primary key columns), or false
//	** otherwise.
//	*/
func _sessionChangeEqual(tls *libc.TLS, pTab uintptr, bLeftPkOnly int32, aLeft uintptr, bRightPkOnly int32, aRight uintptr) (r int32) {
	var a1, a2 uintptr
	var iCol, n1, n2 int32
	_, _, _, _, _ = a1, a2, iCol, n1, n2
	a1 = aLeft  /* Cursor to iterate through aLeft */
	a2 = aRight /* Used to iterate through table columns */
	iCol = 0
	for {
		if !(iCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
			break
		}
		if **(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(iCol))) != 0 {
			n1 = _sessionSerialLen(tls, a1)
			n2 = _sessionSerialLen(tls, a2)
			if n1 != n2 || libc.Xmemcmp(tls, a1, a2, uint32(n1)) != 0 {
				return 0
			}
			a1 = a1 + uintptr(n1)
			a2 = a2 + uintptr(n2)
		} else {
			if bLeftPkOnly == 0 {
				a1 = a1 + uintptr(_sessionSerialLen(tls, a1))
			}
			if bRightPkOnly == 0 {
				a2 = a2 + uintptr(_sessionSerialLen(tls, a2))
			}
		}
		goto _1
	_1:
		;
		iCol = iCol + 1
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** This function is called to merge two changes to the same row together as
//	** part of an sqlite3changeset_concat() operation. A new change object is
//	** allocated and a pointer to it stored in *ppNew.
//	**
//	** Because they have been vetted by sqlite3changegroup_add() or similar,
//	** both the aRec[] change and the pExist change are safe to use without
//	** checking for buffer overflows.
//	*/
func _sessionChangeMerge(tls *libc.TLS, pTab uintptr, bRebase int32, bPatchset int32, pExist uintptr, op2 int32, bIndirect int32, aRec uintptr, nRec int32, ppNew uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a1, a2, aExist, pIn, pNew, pOut, pOut1, v2 uintptr
	var i, i1, n1, n2, nIn, op1, rc int32
	var nByte, nByte1 Tsqlite3_int64
	var _ /* a1 at bp+4 */ uintptr
	var _ /* a1 at bp+8 */ uintptr
	var _ /* a2 at bp+12 */ uintptr
	var _ /* aCsr at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a1, a2, aExist, i, i1, n1, n2, nByte, nByte1, nIn, op1, pIn, pNew, pOut, pOut1, rc, v2
	pNew = uintptr(0)
	rc = SQLITE_OK
	if !(pExist != 0) {
		pNew = Xsqlite3_malloc64(tls, uint64(uint32(20)+uint32(nRec)))
		if !(pNew != 0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, pNew, 0, uint32(20))
		(*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(op2)
		(*TSessionChange)(unsafe.Pointer(pNew)).FbIndirect = uint8(bIndirect)
		(*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = pNew + 1*20
		if bIndirect == 0 || bRebase == 0 {
			(*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = nRec
			libc.Xmemcpy(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord, aRec, uint32(nRec))
		} else {
			pIn = aRec
			pOut = (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord
			i = 0
			for {
				if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
					break
				}
				nIn = _sessionSerialLen(tls, pIn)
				if int32(**(**Tu8)(__ccgo_up(pIn))) == 0 {
					v2 = pOut
					pOut = pOut + 1
					**(**Tu8)(__ccgo_up(v2)) = uint8(0)
				} else {
					if int32(**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i)))) == 0 {
						v2 = pOut
						pOut = pOut + 1
						**(**Tu8)(__ccgo_up(v2)) = uint8(0xFF)
					} else {
						libc.Xmemcpy(tls, pOut, pIn, uint32(nIn))
						pOut = pOut + uintptr(nIn)
					}
				}
				pIn = pIn + uintptr(nIn)
				goto _1
			_1:
				;
				i = i + 1
			}
			(*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = int32(pOut) - int32((*TSessionChange)(unsafe.Pointer(pNew)).FaRecord)
		}
	} else {
		if bRebase != 0 {
			if int32((*TSessionChange)(unsafe.Pointer(pExist)).Fop) == int32(SQLITE_DELETE) && (*TSessionChange)(unsafe.Pointer(pExist)).FbIndirect != 0 {
				**(**uintptr)(__ccgo_up(ppNew)) = pExist
			} else {
				nByte = int64(uint32(nRec+(*TSessionChange)(unsafe.Pointer(pExist)).FnRecord) + uint32(20))
				pNew = Xsqlite3_malloc64(tls, uint64(nByte))
				if pNew == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
				} else {
					a1 = (*TSessionChange)(unsafe.Pointer(pExist)).FaRecord
					a2 = aRec
					libc.Xmemset(tls, pNew, 0, uint32(nByte))
					(*TSessionChange)(unsafe.Pointer(pNew)).FbIndirect = libc.BoolUint8(bIndirect != 0 || (*TSessionChange)(unsafe.Pointer(pExist)).FbIndirect != 0)
					(*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(op2)
					v2 = pNew + 1*20
					(*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = v2
					pOut1 = v2
					i1 = 0
					for {
						if !(i1 < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
							break
						}
						n1 = _sessionSerialLen(tls, a1)
						n2 = _sessionSerialLen(tls, a2)
						if int32(**(**Tu8)(__ccgo_up(a1))) == int32(0xFF) || int32(**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i1)))) == 0 && bIndirect != 0 {
							v2 = pOut1
							pOut1 = pOut1 + 1
							**(**Tu8)(__ccgo_up(v2)) = uint8(0xFF)
						} else {
							if int32(**(**Tu8)(__ccgo_up(a2))) == 0 {
								libc.Xmemcpy(tls, pOut1, a1, uint32(n1))
								pOut1 = pOut1 + uintptr(n1)
							} else {
								libc.Xmemcpy(tls, pOut1, a2, uint32(n2))
								pOut1 = pOut1 + uintptr(n2)
							}
						}
						a1 = a1 + uintptr(n1)
						a2 = a2 + uintptr(n2)
						goto _5
					_5:
						;
						i1 = i1 + 1
					}
					(*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = int32(pOut1) - int32((*TSessionChange)(unsafe.Pointer(pNew)).FaRecord)
				}
				Xsqlite3_free(tls, pExist)
			}
		} else {
			op1 = int32((*TSessionChange)(unsafe.Pointer(pExist)).Fop)
			/*
			 **   op1=INSERT, op2=INSERT      ->      Unsupported. Discard op2.
			 **   op1=INSERT, op2=UPDATE      ->      INSERT.
			 **   op1=INSERT, op2=DELETE      ->      (none)
			 **
			 **   op1=UPDATE, op2=INSERT      ->      Unsupported. Discard op2.
			 **   op1=UPDATE, op2=UPDATE      ->      UPDATE.
			 **   op1=UPDATE, op2=DELETE      ->      DELETE.
			 **
			 **   op1=DELETE, op2=INSERT      ->      UPDATE.
			 **   op1=DELETE, op2=UPDATE      ->      Unsupported. Discard op2.
			 **   op1=DELETE, op2=DELETE      ->      Unsupported. Discard op2.
			 */
			if op1 == int32(SQLITE_INSERT) && op2 == int32(SQLITE_INSERT) || op1 == int32(SQLITE_UPDATE) && op2 == int32(SQLITE_INSERT) || op1 == int32(SQLITE_DELETE) && op2 == int32(SQLITE_UPDATE) || op1 == int32(SQLITE_DELETE) && op2 == int32(SQLITE_DELETE) {
				pNew = pExist
			} else {
				if op1 == int32(SQLITE_INSERT) && op2 == int32(SQLITE_DELETE) {
					Xsqlite3_free(tls, pExist)
				} else {
					aExist = (*TSessionChange)(unsafe.Pointer(pExist)).FaRecord
					/* Allocate a new SessionChange object. Ensure that the aRecord[]
					 ** buffer of the new object is large enough to hold any record that
					 ** may be generated by combining the input records.  */
					nByte1 = int64(uint32(20) + uint32((*TSessionChange)(unsafe.Pointer(pExist)).FnRecord) + uint32(nRec))
					pNew = Xsqlite3_malloc64(tls, uint64(nByte1))
					if !(pNew != 0) {
						Xsqlite3_free(tls, pExist)
						return int32(SQLITE_NOMEM)
					}
					libc.Xmemset(tls, pNew, 0, uint32(20))
					(*TSessionChange)(unsafe.Pointer(pNew)).FbIndirect = libc.BoolUint8(bIndirect != 0 && (*TSessionChange)(unsafe.Pointer(pExist)).FbIndirect != 0)
					v2 = pNew + 1*20
					(*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = v2
					**(**uintptr)(__ccgo_up(bp)) = v2
					if op1 == int32(SQLITE_INSERT) { /* INSERT + UPDATE */
						**(**uintptr)(__ccgo_up(bp + 4)) = aRec
						(*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_INSERT)
						if bPatchset == 0 {
							_sessionSkipRecord(tls, bp+4, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol)
						}
						_sessionMergeRecord(tls, bp, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, aExist, **(**uintptr)(__ccgo_up(bp + 4)))
					} else {
						if op1 == int32(SQLITE_DELETE) { /* DELETE + INSERT */
							(*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_UPDATE)
							if bPatchset != 0 {
								libc.Xmemcpy(tls, **(**uintptr)(__ccgo_up(bp)), aRec, uint32(nRec))
								**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + uintptr(nRec)
							} else {
								if 0 == _sessionMergeUpdate(tls, bp, pTab, bPatchset, aExist, uintptr(0), aRec, uintptr(0)) {
									Xsqlite3_free(tls, pNew)
									pNew = uintptr(0)
								}
							}
						} else {
							if op2 == int32(SQLITE_UPDATE) { /* UPDATE + UPDATE */
								**(**uintptr)(__ccgo_up(bp + 8)) = aExist
								**(**uintptr)(__ccgo_up(bp + 12)) = aRec
								if bPatchset == 0 {
									_sessionSkipRecord(tls, bp+8, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol)
									_sessionSkipRecord(tls, bp+12, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol)
								}
								(*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_UPDATE)
								if 0 == _sessionMergeUpdate(tls, bp, pTab, bPatchset, aRec, aExist, **(**uintptr)(__ccgo_up(bp + 8)), **(**uintptr)(__ccgo_up(bp + 12))) {
									Xsqlite3_free(tls, pNew)
									pNew = uintptr(0)
								}
							} else { /* UPDATE + DELETE */
								(*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_DELETE)
								if bPatchset != 0 {
									libc.Xmemcpy(tls, **(**uintptr)(__ccgo_up(bp)), aRec, uint32(nRec))
									**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + uintptr(nRec)
								} else {
									_sessionMergeRecord(tls, bp, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, aRec, aExist)
								}
							}
						}
					}
					if pNew != 0 {
						(*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = int32(**(**uintptr)(__ccgo_up(bp))) - int32((*TSessionChange)(unsafe.Pointer(pNew)).FaRecord)
					}
					Xsqlite3_free(tls, pExist)
				}
			}
		}
	}
	**(**uintptr)(__ccgo_up(ppNew)) = pNew
	return rc
}

// C documentation
//
//	/*
//	** Argument pIter is a changeset iterator that has been initialized, but
//	** not yet passed to sqlite3changeset_next(). This function applies the
//	** changeset to the main database attached to handle "db". The supplied
//	** conflict handler callback is invoked to resolve any conflicts encountered
//	** while applying the change.
//	*/
func _sessionChangesetApply(tls *libc.TLS, db uintptr, pIter uintptr, __ccgo_fp_xFilter uintptr, __ccgo_fp_xFilterIter uintptr, __ccgo_fp_xConflict uintptr, pCtx uintptr, ppRebase uintptr, pnRebase uintptr, flags int32) (r int32) {
	bp := tls.Alloc(256)
	defer tls.Free(256)
	var bPatchset, i, nMinCol, nTab, rc, rc2, res, schemaMismatch, v2, v3, v4 int32
	var savedFlag Tu64
	var v5, v7 bool
	var _ /* abPK at bp+100 */ uintptr
	var _ /* nCol at bp+88 */ int32
	var _ /* nFk at bp+104 */ int32
	var _ /* notUsed at bp+108 */ int32
	var _ /* op at bp+92 */ int32
	var _ /* sApply at bp+4 */ TSessionApplyCtx
	var _ /* sIter at bp+112 */ Tsqlite3_changeset_iter
	var _ /* zNew at bp+96 */ uintptr
	var _ /* zTab at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = bPatchset, i, nMinCol, nTab, rc, rc2, res, savedFlag, schemaMismatch, v2, v3, v4, v5, v7
	schemaMismatch = 0
	rc = SQLITE_OK                            /* Return code */
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* Name of current table */
	nTab = 0
	savedFlag = (*Tsqlite3)(unsafe.Pointer(db)).Fflags & (uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32))
	Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, db))
	if flags&int32(SQLITE_CHANGESETAPPLY_FKNOACTION) != 0 {
		**(**Tu64)(__ccgo_up(db + 32)) |= uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32)
		**(**int32)(__ccgo_up((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)) -= int32(32)
	}
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FbNoDiscard = int32(1)
	libc.Xmemset(tls, bp+4, 0, uint32(84))
	(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbRebase = libc.BoolUint8(ppRebase != 0 && pnRebase != 0)
	(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbInvertConstraints = libc.BoolInt32(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETAPPLY_INVERT) != 0))
	(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbIgnoreNoop = libc.BoolUint8(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETAPPLY_IGNORENOOP) != 0))
	(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbNoUpdateLoop = libc.BoolUint8(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETAPPLY_NOUPDATELOOP) != 0))
	if flags&int32(SQLITE_CHANGESETAPPLY_NOSAVEPOINT) == 0 {
		rc = Xsqlite3_exec(tls, db, __ccgo_ts+37754, uintptr(0), uintptr(0), uintptr(0))
	}
	if rc == SQLITE_OK {
		rc = Xsqlite3_exec(tls, db, __ccgo_ts+37780, uintptr(0), uintptr(0), uintptr(0))
	}
	for rc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3changeset_next(tls, pIter) {
		Xsqlite3changeset_op(tls, pIter, bp+96, bp+88, bp+92, uintptr(0))
		if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) || Xsqlite3_strnicmp(tls, **(**uintptr)(__ccgo_up(bp + 96)), **(**uintptr)(__ccgo_up(bp)), nTab+int32(1)) != 0 {
			rc = _sessionRetryConstraints(tls, db, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset, **(**uintptr)(__ccgo_up(bp)), bp+4, __ccgo_fp_xConflict, pCtx)
			if rc != SQLITE_OK {
				break
			}
			_sessionUpdateFree(tls, bp+4)
			Xsqlite3_free(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FazCol) /* cast works around VC++ bug */
			Xsqlite3_finalize(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FpDelete)
			Xsqlite3_finalize(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FpInsert)
			Xsqlite3_finalize(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FpSelect)
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).Fdb = db
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FpDelete = uintptr(0)
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FpInsert = uintptr(0)
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FpSelect = uintptr(0)
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FnCol = 0
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FazCol = uintptr(0)
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FabPK = uintptr(0)
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbStat1 = 0
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbDeferConstraints = int32(1)
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbRebaseStarted = uint8(0)
			(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbRowid = 0
			libc.Xmemset(tls, bp+4+48, 0, uint32(12))
			/* If an xFilter() callback was specified, invoke it now. If the
			 ** xFilter callback returns zero, skip this table. If it returns
			 ** non-zero, proceed. */
			schemaMismatch = libc.BoolInt32(__ccgo_fp_xFilter != 0 && 0 == (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xFilter})))(tls, pCtx, **(**uintptr)(__ccgo_up(bp + 96))))
			if schemaMismatch != 0 {
				**(**uintptr)(__ccgo_up(bp)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+224, **(**uintptr)(__ccgo_up(bp + 96))))
				if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
					break
				}
				nTab = int32(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(bp))))
				(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FazCol = **(**uintptr)(__ccgo_up(bp))
			} else {
				nMinCol = 0
				Xsqlite3changeset_pk(tls, pIter, bp+100, uintptr(0))
				rc = _sessionTableInfo(tls, uintptr(0), db, __ccgo_ts+8033, **(**uintptr)(__ccgo_up(bp + 96)), bp+4+16, uintptr(0), bp, bp+4+20, uintptr(0), uintptr(0), bp+4+24, bp+4+76)
				if rc != SQLITE_OK {
					break
				}
				i = 0
				for {
					if !(i < (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FnCol) {
						break
					}
					if **(**Tu8)(__ccgo_up((**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FabPK + uintptr(i))) != 0 {
						nMinCol = i + int32(1)
					}
					goto _1
				_1:
					;
					i = i + 1
				}
				if (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FnCol == 0 {
					schemaMismatch = int32(1)
					Xsqlite3_log(tls, int32(SQLITE_SCHEMA), __ccgo_ts+37810, libc.VaList(bp+224, **(**uintptr)(__ccgo_up(bp))))
				} else {
					if (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FnCol < **(**int32)(__ccgo_up(bp + 88)) {
						schemaMismatch = int32(1)
						Xsqlite3_log(tls, int32(SQLITE_SCHEMA), __ccgo_ts+37854, libc.VaList(bp+224, **(**uintptr)(__ccgo_up(bp)), (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FnCol, **(**int32)(__ccgo_up(bp + 88))))
					} else {
						if **(**int32)(__ccgo_up(bp + 88)) < nMinCol || libc.Xmemcmp(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FabPK, **(**uintptr)(__ccgo_up(bp + 100)), uint32(**(**int32)(__ccgo_up(bp + 88)))) != 0 {
							schemaMismatch = int32(1)
							Xsqlite3_log(tls, int32(SQLITE_SCHEMA), __ccgo_ts+37925, libc.VaList(bp+224, **(**uintptr)(__ccgo_up(bp))))
						} else {
							(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FnCol = **(**int32)(__ccgo_up(bp + 88))
							if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up(bp)), __ccgo_ts+14050) {
								v2 = _sessionStat1Sql(tls, db, bp+4)
								rc = v2
								if v2 != 0 {
									break
								}
								(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbStat1 = int32(1)
							} else {
								v2 = _sessionSelectRow(tls, db, **(**uintptr)(__ccgo_up(bp)), bp+4)
								rc = v2
								if v5 = v2 != 0; !v5 {
									v3 = _sessionDeleteRow(tls, db, **(**uintptr)(__ccgo_up(bp)), bp+4)
									rc = v3
								}
								if v7 = v5 || v3 != 0; !v7 {
									v4 = _sessionInsertRow(tls, db, **(**uintptr)(__ccgo_up(bp)), bp+4)
									rc = v4
								}
								if v7 || v4 != 0 {
									break
								}
								(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbStat1 = 0
							}
						}
					}
				}
				nTab = _sqlite3Strlen30(tls, **(**uintptr)(__ccgo_up(bp)))
			}
		}
		/* If there is a schema mismatch on the current table, proceed to the
		 ** next change. A log message has already been issued. */
		if schemaMismatch != 0 {
			continue
		}
		/* If this is a call to apply_v3(), invoke xFilterIter here. */
		if __ccgo_fp_xFilterIter != 0 && 0 == (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xFilterIter})))(tls, pCtx, pIter) {
			continue
		}
		rc = _sessionApplyOneWithRetry(tls, db, pIter, bp+4, __ccgo_fp_xConflict, pCtx)
	}
	bPatchset = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset
	if rc == SQLITE_OK {
		rc = Xsqlite3changeset_finalize(tls, pIter)
	} else {
		Xsqlite3changeset_finalize(tls, pIter)
	}
	if rc == SQLITE_OK {
		rc = _sessionRetryConstraints(tls, db, bPatchset, **(**uintptr)(__ccgo_up(bp)), bp+4, __ccgo_fp_xConflict, pCtx)
	}
	if rc == SQLITE_OK {
		Xsqlite3_db_status(tls, db, int32(SQLITE_DBSTATUS_DEFERRED_FKS), bp+104, bp+108, 0)
		if **(**int32)(__ccgo_up(bp + 104)) != 0 {
			res = int32(SQLITE_CHANGESET_ABORT)
			libc.Xmemset(tls, bp+112, 0, uint32(100))
			(**(**Tsqlite3_changeset_iter)(__ccgo_up(bp + 112))).FnCol = **(**int32)(__ccgo_up(bp + 104))
			res = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xConflict})))(tls, pCtx, int32(SQLITE_CHANGESET_FOREIGN_KEY), bp+112)
			if res != SQLITE_CHANGESET_OMIT {
				rc = int32(SQLITE_CONSTRAINT)
			}
		}
	}
	rc2 = Xsqlite3_exec(tls, db, __ccgo_ts+37985, uintptr(0), uintptr(0), uintptr(0))
	if rc == SQLITE_OK {
		rc = rc2
	}
	if flags&int32(SQLITE_CHANGESETAPPLY_NOSAVEPOINT) == 0 {
		if rc == SQLITE_OK {
			rc = Xsqlite3_exec(tls, db, __ccgo_ts+38015, uintptr(0), uintptr(0), uintptr(0))
		}
		if rc != SQLITE_OK {
			Xsqlite3_exec(tls, db, __ccgo_ts+38039, uintptr(0), uintptr(0), uintptr(0))
			Xsqlite3_exec(tls, db, __ccgo_ts+38015, uintptr(0), uintptr(0), uintptr(0))
		}
	}
	if rc == SQLITE_OK && bPatchset == 0 && (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FbRebase != 0 {
		**(**uintptr)(__ccgo_up(ppRebase)) = (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).Frebase.FaBuf
		**(**int32)(__ccgo_up(pnRebase)) = (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).Frebase.FnBuf
		(**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).Frebase.FaBuf = uintptr(0)
	}
	_sessionUpdateFree(tls, bp+4)
	Xsqlite3_finalize(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FpInsert)
	Xsqlite3_finalize(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FpDelete)
	Xsqlite3_finalize(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FpSelect)
	Xsqlite3_free(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FazCol) /* cast works around VC++ bug */
	Xsqlite3_free(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).Fconstraints.FaBuf)
	Xsqlite3_free(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).Frebase.FaBuf)
	if flags&int32(SQLITE_CHANGESETAPPLY_FKNOACTION) != 0 && savedFlag == uint64(0) {
		**(**Tu64)(__ccgo_up(db + 32)) &= ^(uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32))
		**(**int32)(__ccgo_up((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)) -= int32(32)
	}
	Xsqlite3_set_errmsg(tls, db, rc, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FzErr)
	Xsqlite3_free(tls, (**(**TSessionApplyCtx)(__ccgo_up(bp + 4))).FzErr)
	Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, db))
	return rc
}

// C documentation
//
//	/*
//	** Check if a changeset entry with nCol columns and the PK array passed
//	** as the final argument to this function is compatible with SessionTable
//	** pTab. If so, return 1. Otherwise, if they are incompatible in some way,
//	** return 0.
//	*/
func _sessionChangesetCheckCompat(tls *libc.TLS, pTab uintptr, nCol int32, abPK uintptr) (r int32) {
	var bPK Tu8
	var ii, v2 int32
	_, _, _ = bPK, ii, v2
	if (*TSessionTable)(unsafe.Pointer(pTab)).FazCol != 0 && nCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol {
		ii = 0
		for {
			if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
				break
			}
			if ii < nCol {
				v2 = int32(**(**Tu8)(__ccgo_up(abPK + uintptr(ii))))
			} else {
				v2 = 0
			}
			bPK = uint8(v2)
			if int32(**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(ii)))) != int32(bPK) {
				return 0
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
		return int32(1)
	}
	return libc.BoolInt32((*TSessionTable)(unsafe.Pointer(pTab)).FnCol == nCol && 0 == libc.Xmemcmp(tls, abPK, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, uint32(nCol)))
}

func _sessionChangesetExtendRecord(tls *libc.TLS, pGrp uintptr, pTab uintptr, nCol int32, op int32, aRec uintptr, nRec int32, pOut uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var eType, iOff, ii, n int32
	var iVal Tsqlite3_int64
	var rVal float64
	var z, z1 uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _ = eType, iOff, iVal, ii, n, rVal, z, z1
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	ii = 0
	(*TSessionBuffer)(unsafe.Pointer(pOut)).FnBuf = 0
	if op == int32(SQLITE_INSERT) || op == int32(SQLITE_DELETE) && (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch == 0 {
		/* Append the missing default column values to the record. */
		_sessionAppendBlob(tls, pOut, aRec, nRec, bp)
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt == uintptr(0) {
			**(**int32)(__ccgo_up(bp)) = _sessionPrepareDfltStmt(tls, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb, pTab, pTab+52)
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && int32(SQLITE_ROW) != Xsqlite3_step(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt) {
				**(**int32)(__ccgo_up(bp)) = Xsqlite3_errcode(tls, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb)
			}
		}
		ii = nCol
		for {
			if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
				break
			}
			eType = Xsqlite3_column_type(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii)
			_sessionAppendByte(tls, pOut, uint8(eType), bp)
			switch eType {
			case int32(SQLITE_FLOAT):
				fallthrough
			case int32(SQLITE_INTEGER):
				if SQLITE_OK == _sessionBufferGrow(tls, pOut, int64(8), bp) {
					if eType == int32(SQLITE_INTEGER) {
						iVal = Xsqlite3_column_int64(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii)
						_sessionPutI64(tls, (*TSessionBuffer)(unsafe.Pointer(pOut)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(pOut)).FnBuf), iVal)
					} else {
						rVal = Xsqlite3_column_double(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii)
						_sessionPutDouble(tls, (*TSessionBuffer)(unsafe.Pointer(pOut)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(pOut)).FnBuf), rVal)
					}
					**(**int32)(__ccgo_up(pOut + 4)) += int32(8)
				}
			case int32(SQLITE_BLOB):
				fallthrough
			case int32(SQLITE_TEXT):
				n = Xsqlite3_column_bytes(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii)
				_sessionAppendVarint(tls, pOut, n, bp)
				if eType == int32(SQLITE_TEXT) {
					z = Xsqlite3_column_text(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii)
					_sessionAppendBlob(tls, pOut, z, n, bp)
				} else {
					z1 = Xsqlite3_column_blob(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FpDfltStmt, ii)
					_sessionAppendBlob(tls, pOut, z1, n, bp)
				}
			default:
				break
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
	} else {
		if op == int32(SQLITE_UPDATE) {
			/* Append missing "undefined" entries to the old.* record. And, if this
			 ** is an UPDATE, to the new.* record as well.  */
			iOff = 0
			if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch == 0 {
				ii = 0
				for {
					if !(ii < nCol) {
						break
					}
					iOff = iOff + _sessionSerialLen(tls, aRec+uintptr(iOff))
					goto _2
				_2:
					;
					ii = ii + 1
				}
				_sessionAppendBlob(tls, pOut, aRec, iOff, bp)
				ii = 0
				for {
					if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-nCol) {
						break
					}
					_sessionAppendByte(tls, pOut, uint8(0x00), bp)
					goto _3
				_3:
					;
					ii = ii + 1
				}
			}
			_sessionAppendBlob(tls, pOut, aRec+uintptr(iOff), nRec-iOff, bp)
			ii = 0
			for {
				if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-nCol) {
					break
				}
				_sessionAppendByte(tls, pOut, uint8(0x00), bp)
				goto _4
			_4:
				;
				ii = ii + 1
			}
		} else {
			_sessionAppendBlob(tls, pOut, aRec, nRec, bp)
		}
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Locate or create a SessionTable object that may be used to add the
//	** change currently pointed to by iterator pIter to changegroup pGrp.
//	** If successful, set output variable (*ppTab) to point to the table
//	** object and return SQLITE_OK. Otherwise, if some error occurs, return
//	** an SQLite error code and leave (*ppTab) set to NULL.
//	*/
func _sessionChangesetFindTable(tls *libc.TLS, pGrp uintptr, zTab uintptr, pIter uintptr, ppTab uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nTab, rc int32
	var pTab, ppNew uintptr
	var _ /* abPK at bp+0 */ uintptr
	var _ /* nCol at bp+4 */ int32
	_, _, _, _ = nTab, pTab, ppNew, rc
	rc = SQLITE_OK
	pTab = uintptr(0)
	nTab = int32(libc.Xstrlen(tls, zTab))
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 4)) = 0
	**(**uintptr)(__ccgo_up(ppTab)) = uintptr(0)
	/* Search the list for an existing table */
	pTab = (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FpList
	for {
		if !(pTab != 0) {
			break
		}
		if 0 == Xsqlite3_strnicmp(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zTab, nTab+int32(1)) {
			break
		}
		goto _1
	_1:
		;
		pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext
	}
	if pIter != 0 {
		Xsqlite3changeset_pk(tls, pIter, bp, bp+4)
	} else {
		if !(pTab != 0) && !((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb != 0) {
			return SQLITE_OK
		}
	}
	/* If one was not found above, create a new table now */
	if !(pTab != 0) {
		pTab = Xsqlite3_malloc64(tls, uint64(uint32(56)+uint32(**(**int32)(__ccgo_up(bp + 4)))+uint32(nTab)+uint32(1)))
		if !(pTab != 0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, pTab, 0, uint32(56))
		(*TSessionTable)(unsafe.Pointer(pTab)).FnCol = **(**int32)(__ccgo_up(bp + 4))
		(*TSessionTable)(unsafe.Pointer(pTab)).FabPK = pTab + 1*56
		if **(**int32)(__ccgo_up(bp + 4)) > 0 {
			libc.Xmemcpy(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, **(**uintptr)(__ccgo_up(bp)), uint32(**(**int32)(__ccgo_up(bp + 4))))
		}
		(*TSessionTable)(unsafe.Pointer(pTab)).FzName = (*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(**(**int32)(__ccgo_up(bp + 4)))
		libc.Xmemcpy(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zTab, uint32(nTab+int32(1)))
		if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb != 0 {
			(*TSessionTable)(unsafe.Pointer(pTab)).FnCol = 0
			rc = _sessionInitTable(tls, uintptr(0), pTab, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Fdb, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FzDb)
			if rc != 0 || (*TSessionTable)(unsafe.Pointer(pTab)).FnCol == 0 {
				Xsqlite3_free(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FazCol)
				Xsqlite3_free(tls, pTab)
				return rc
			}
		}
		/* The new object must be linked on to the end of the list, not
		 ** simply added to the start of it. This is to ensure that the
		 ** tables within the output of sqlite3changegroup_output() are in
		 ** the right order.  */
		ppNew = pGrp + 8
		for {
			if !(**(**uintptr)(__ccgo_up(ppNew)) != 0) {
				break
			}
			goto _2
		_2:
			;
			ppNew = **(**uintptr)(__ccgo_up(ppNew))
		}
		**(**uintptr)(__ccgo_up(ppNew)) = pTab
	}
	/* Check that the table is compatible. */
	if pIter != 0 && !(_sessionChangesetCheckCompat(tls, pTab, **(**int32)(__ccgo_up(bp + 4)), **(**uintptr)(__ccgo_up(bp))) != 0) {
		rc = int32(SQLITE_SCHEMA)
	}
	**(**uintptr)(__ccgo_up(ppTab)) = pTab
	return rc
}

func _sessionChangesetInvert(tls *libc.TLS, pInput uintptr, __ccgo_fp_xOutput uintptr, pOut uintptr, pnInverted uintptr, ppInverted uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var abPK, apVal, pVal, pVal1, v7 uintptr
	var bIndirect, eType2, iCol, nVar, v1 int32
	var eType Tu8
	var _ /* nByte at bp+32 */ int32
	var _ /* nByte at bp+36 */ int32
	var _ /* nCol at bp+16 */ int32
	var _ /* rc at bp+0 */ int32
	var _ /* sOut at bp+4 */ TSessionBuffer
	var _ /* sPK at bp+20 */ TSessionBuffer
	_, _, _, _, _, _, _, _, _, _, _ = abPK, apVal, bIndirect, eType, eType2, iCol, nVar, pVal, pVal1, v1, v7
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK                      /* Output buffer */
	**(**int32)(__ccgo_up(bp + 16)) = 0                         /* Number of cols in current table */
	abPK = uintptr(0)                                           /* PK array for current table */
	apVal = uintptr(0)                                          /* Space for values for UPDATE inversion */
	**(**TSessionBuffer)(__ccgo_up(bp + 20)) = TSessionBuffer{} /* PK array for current table */
	/* Initialize the output buffer */
	libc.Xmemset(tls, bp+4, 0, uint32(12))
	/* Zero the output variables in case an error occurs. */
	if ppInverted != 0 {
		**(**uintptr)(__ccgo_up(ppInverted)) = uintptr(0)
		**(**int32)(__ccgo_up(pnInverted)) = 0
	}
	for int32(1) != 0 {
		/* Test for EOF. */
		v1 = _sessionInputBuffer(tls, pInput, int32(2))
		**(**int32)(__ccgo_up(bp)) = v1
		if v1 != 0 {
			goto finished_invert
		}
		if (*TSessionInput)(unsafe.Pointer(pInput)).FiNext+int32(1) >= (*TSessionInput)(unsafe.Pointer(pInput)).FnData {
			if (*TSessionInput)(unsafe.Pointer(pInput)).FiNext != (*TSessionInput)(unsafe.Pointer(pInput)).FnData {
				**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(237907))
				goto finished_invert
			}
			break
		}
		eType = **(**Tu8)(__ccgo_up((*TSessionInput)(unsafe.Pointer(pInput)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext)))
		switch int32(eType) {
		case int32('T'):
			(*TSessionInput)(unsafe.Pointer(pInput)).FiNext = (*TSessionInput)(unsafe.Pointer(pInput)).FiNext + 1
			v1 = _sessionChangesetBufferTblhdr(tls, pInput, bp+32)
			**(**int32)(__ccgo_up(bp)) = v1
			if v1 != 0 {
				goto finished_invert
			}
			nVar = _sessionVarintGet(tls, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext), bp+16)
			(**(**TSessionBuffer)(__ccgo_up(bp + 20))).FnBuf = 0
			_sessionAppendBlob(tls, bp+20, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext+nVar), **(**int32)(__ccgo_up(bp + 16)), bp)
			_sessionAppendByte(tls, bp+4, eType, bp)
			_sessionAppendBlob(tls, bp+4, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext), **(**int32)(__ccgo_up(bp + 32)), bp)
			if **(**int32)(__ccgo_up(bp)) != 0 {
				goto finished_invert
			}
			**(**int32)(__ccgo_up(pInput + 8)) += **(**int32)(__ccgo_up(bp + 32))
			Xsqlite3_free(tls, apVal)
			apVal = uintptr(0)
			abPK = (**(**TSessionBuffer)(__ccgo_up(bp + 20))).FaBuf
		case int32(SQLITE_INSERT):
			fallthrough
		case int32(SQLITE_DELETE):
			bIndirect = int32(**(**Tu8)(__ccgo_up((*TSessionInput)(unsafe.Pointer(pInput)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext+int32(1)))))
			if int32(eType) == int32(SQLITE_DELETE) {
				v1 = int32(SQLITE_INSERT)
			} else {
				v1 = int32(SQLITE_DELETE)
			}
			eType2 = v1
			**(**int32)(__ccgo_up(pInput + 8)) += int32(2)
			**(**int32)(__ccgo_up(bp)) = _sessionChangesetBufferRecord(tls, pInput, **(**int32)(__ccgo_up(bp + 16)), bp+36)
			_sessionAppendByte(tls, bp+4, uint8(eType2), bp)
			_sessionAppendByte(tls, bp+4, uint8(bIndirect), bp)
			_sessionAppendBlob(tls, bp+4, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext), **(**int32)(__ccgo_up(bp + 36)), bp)
			**(**int32)(__ccgo_up(pInput + 8)) += **(**int32)(__ccgo_up(bp + 36))
			if **(**int32)(__ccgo_up(bp)) != 0 {
				goto finished_invert
			}
		case int32(SQLITE_UPDATE):
			if uintptr(0) == apVal {
				apVal = Xsqlite3_malloc64(tls, uint64(uint32(4)*uint32(**(**int32)(__ccgo_up(bp + 16)))*uint32(2)))
				if uintptr(0) == apVal {
					**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
					goto finished_invert
				}
				libc.Xmemset(tls, apVal, 0, uint32(4)*uint32(**(**int32)(__ccgo_up(bp + 16)))*uint32(2))
			}
			/* Write the header for the new UPDATE change. Same as the original. */
			_sessionAppendByte(tls, bp+4, eType, bp)
			_sessionAppendByte(tls, bp+4, **(**Tu8)(__ccgo_up((*TSessionInput)(unsafe.Pointer(pInput)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext+int32(1)))), bp)
			/* Read the old.* and new.* records for the update change. */
			**(**int32)(__ccgo_up(pInput + 8)) += int32(2)
			**(**int32)(__ccgo_up(bp)) = _sessionReadRecord(tls, pInput, **(**int32)(__ccgo_up(bp + 16)), uintptr(0), apVal, uintptr(0))
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp)) = _sessionReadRecord(tls, pInput, **(**int32)(__ccgo_up(bp + 16)), uintptr(0), apVal+uintptr(**(**int32)(__ccgo_up(bp + 16)))*4, uintptr(0))
			}
			/* Write the new old.* record. Consists of the PK columns from the
			 ** original old.* record, and the other values from the original
			 ** new.* record. */
			iCol = 0
			for {
				if !(iCol < **(**int32)(__ccgo_up(bp + 16))) {
					break
				}
				if **(**Tu8)(__ccgo_up(abPK + uintptr(iCol))) != 0 {
					v1 = 0
				} else {
					v1 = **(**int32)(__ccgo_up(bp + 16))
				}
				pVal = **(**uintptr)(__ccgo_up(apVal + uintptr(iCol+v1)*4))
				_sessionAppendValue(tls, bp+4, pVal, bp)
				goto _4
			_4:
				;
				iCol = iCol + 1
			}
			/* Write the new new.* record. Consists of a copy of all values
			 ** from the original old.* record, except for the PK columns, which
			 ** are set to "undefined". */
			iCol = 0
			for {
				if !(iCol < **(**int32)(__ccgo_up(bp + 16))) {
					break
				}
				if **(**Tu8)(__ccgo_up(abPK + uintptr(iCol))) != 0 {
					v7 = uintptr(0)
				} else {
					v7 = **(**uintptr)(__ccgo_up(apVal + uintptr(iCol)*4))
				}
				pVal1 = v7
				_sessionAppendValue(tls, bp+4, pVal1, bp)
				goto _6
			_6:
				;
				iCol = iCol + 1
			}
			iCol = 0
			for {
				if !(iCol < **(**int32)(__ccgo_up(bp + 16))*int32(2)) {
					break
				}
				_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up(apVal + uintptr(iCol)*4)))
				goto _8
			_8:
				;
				iCol = iCol + 1
			}
			libc.Xmemset(tls, apVal, 0, uint32(4)*uint32(**(**int32)(__ccgo_up(bp + 16)))*uint32(2))
			if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
				goto finished_invert
			}
		default:
			**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(238010))
			goto finished_invert
		}
		if __ccgo_fp_xOutput != 0 && (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FnBuf >= _sessions_strm_chunk_size {
			**(**int32)(__ccgo_up(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xOutput})))(tls, pOut, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FnBuf)
			(**(**TSessionBuffer)(__ccgo_up(bp + 4))).FnBuf = 0
			if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
				goto finished_invert
			}
		}
	}
	if pnInverted != 0 && ppInverted != 0 {
		**(**int32)(__ccgo_up(pnInverted)) = (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FnBuf
		**(**uintptr)(__ccgo_up(ppInverted)) = (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf
		(**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf = uintptr(0)
	} else {
		if (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FnBuf > 0 && __ccgo_fp_xOutput != uintptr(0) {
			**(**int32)(__ccgo_up(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xOutput})))(tls, pOut, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FnBuf)
		}
	}
	goto finished_invert
finished_invert:
	;
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf)
	Xsqlite3_free(tls, apVal)
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 20))).FaBuf)
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Advance the changeset iterator to the next change. The differences between
//	** this function and sessionChangesetNext() are that
//	**
//	**   * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE
//	**     that modifies no columns), this function sets (*pbEmpty) to 1.
//	**
//	**   * If the iterator is configured to skip no-op UPDATEs,
//	**     sessionChangesetNext() does that. This function does not.
//	*/
func _sessionChangesetNextOne(tls *libc.TLS, p uintptr, paRec uintptr, pnRec uintptr, pbNew uintptr, pbEmpty uintptr) (r int32) {
	var abPK, apNew, apOld, v10, v3, v6 uintptr
	var i, nVal, v2 int32
	var op Tu8
	_, _, _, _, _, _, _, _, _, _ = abPK, apNew, apOld, i, nVal, op, v10, v2, v3, v6
	/* If the iterator is in the error-state, return immediately. */
	if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK {
		return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc
	}
	/* Free the current contents of p->apValue[], if any. */
	if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue != 0 {
		i = 0
		for {
			if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol*int32(2)) {
				break
			}
			_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4)))
			goto _1
		_1:
			;
			i = i + 1
		}
		libc.Xmemset(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue, 0, uint32(4)*uint32((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*uint32(2))
	}
	/* Make sure the buffer contains at least 2 bytes of input data, or all
	 ** remaining data if there are less than 2 bytes available. This is
	 ** sufficient either for the 'T' or 'P' byte that begins a new table,
	 ** or for the "op" and "bIndirect" single bytes otherwise. */
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = _sessionInputBuffer(tls, p, int32(2))
	if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK {
		return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc
	}
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiCurrent = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext
	_sessionDiscardData(tls, p)
	/* If the iterator is already at the end of the changeset, return DONE. */
	if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext >= (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FnData {
		return int32(SQLITE_DONE)
	}
	v3 = p + 8
	v2 = *(*int32)(unsafe.Pointer(v3))
	*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
	op = **(**Tu8)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData + uintptr(v2)))
	for int32(op) == int32('T') || int32(op) == int32('P') {
		if pbNew != 0 {
			**(**int32)(__ccgo_up(pbNew)) = int32(1)
		}
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset = libc.BoolInt32(int32(op) == int32('P'))
		if _sessionChangesetReadTblhdr(tls, p) != 0 {
			return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc
		}
		v2 = _sessionInputBuffer(tls, p, int32(2))
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v2
		if v2 != 0 {
			return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc
		}
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiCurrent = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext >= (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FnData {
			return int32(SQLITE_DONE)
		}
		v3 = p + 8
		v2 = *(*int32)(unsafe.Pointer(v3))
		*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
		op = **(**Tu8)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData + uintptr(v2)))
	}
	if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FzTab == uintptr(0) || (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset != 0 && (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0 {
		/* The first record in the changeset is not a table header. Must be a
		 ** corrupt changeset. */
		v2 = _sqlite3CorruptError(tls, int32(237587))
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v2
		return v2
	}
	if int32(op) != int32(SQLITE_UPDATE) && int32(op) != int32(SQLITE_DELETE) && int32(op) != int32(SQLITE_INSERT) || (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext >= (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FnData {
		v2 = _sqlite3CorruptError(tls, int32(237593))
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v2
		return v2
	}
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop = int32(op)
	v3 = p + 8
	v2 = *(*int32)(unsafe.Pointer(v3))
	*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbIndirect = int32(**(**Tu8)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData + uintptr(v2))))
	if paRec != 0 { /* Number of values to buffer */
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset == 0 && int32(op) == int32(SQLITE_UPDATE) {
			nVal = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol * int32(2)
		} else {
			if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset != 0 && int32(op) == int32(SQLITE_DELETE) {
				nVal = 0
				i = 0
				for {
					if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) {
						break
					}
					if **(**Tu8)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 {
						nVal = nVal + 1
					}
					goto _11
				_11:
					;
					i = i + 1
				}
			} else {
				nVal = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol
			}
		}
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = _sessionChangesetBufferRecord(tls, p, nVal, pnRec)
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK {
			return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc
		}
		**(**uintptr)(__ccgo_up(paRec)) = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext)
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext += **(**int32)(__ccgo_up(pnRec))
	} else {
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0 {
			v3 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4
		} else {
			v3 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue
		}
		apOld = v3
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0 {
			v6 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue
		} else {
			v6 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4
		}
		apNew = v6
		/* If this is an UPDATE or DELETE, read the old.* record. */
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop != int32(SQLITE_INSERT) && ((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset == 0 || (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_DELETE)) {
			if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset != 0 {
				v10 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK
			} else {
				v10 = uintptr(0)
			}
			abPK = v10
			(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = _sessionReadRecord(tls, p, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol, abPK, apOld, uintptr(0))
			if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK {
				return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc
			}
		}
		/* If this is an INSERT or UPDATE, read the new.* record. */
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop != int32(SQLITE_DELETE) {
			(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = _sessionReadRecord(tls, p, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol, uintptr(0), apNew, pbEmpty)
			if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc != SQLITE_OK {
				return (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc
			}
		}
		if ((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset != 0 || (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0) && (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_UPDATE) {
			/* If this is an UPDATE that is part of a patchset, then all PK and
			 ** modified fields are present in the new.* record. The old.* record
			 ** is currently completely empty. This block shifts the PK fields from
			 ** new.* to old.*, to accommodate the code that reads these arrays.  */
			i = 0
			for {
				if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) {
					break
				}
				if **(**Tu8)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 {
					**(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4)) = **(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i+(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4))
					if **(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4)) == uintptr(0) {
						v2 = _sqlite3CorruptError(tls, int32(237639))
						(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v2
						return v2
					}
					**(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i+(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4)) = uintptr(0)
				}
				goto _15
			_15:
				;
				i = i + 1
			}
		} else {
			if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbInvert != 0 {
				if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_INSERT) {
					(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop = int32(SQLITE_DELETE)
				} else {
					if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_DELETE) {
						(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop = int32(SQLITE_INSERT)
					}
				}
			}
		}
		/* If this is an UPDATE that is part of a changeset, then check that
		 ** there are no fields in the old.* record that are not (a) PK fields,
		 ** or (b) also present in the new.* record.
		 **
		 ** Such records are technically corrupt, but the rebaser was at one
		 ** point generating them. Under most circumstances this is benign, but
		 ** can cause spurious SQLITE_RANGE errors when applying the changeset. */
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FbPatchset == 0 && (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fop == int32(SQLITE_UPDATE) {
			i = 0
			for {
				if !(i < (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) {
					break
				}
				if int32(**(**Tu8)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK + uintptr(i)))) == 0 && **(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i+(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*4)) == uintptr(0) {
					_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4)))
					**(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*4)) = uintptr(0)
				}
				goto _17
			_17:
				;
				i = i + 1
			}
		}
	}
	return int32(SQLITE_ROW)
}

// C documentation
//
//	/*
//	** The input pointer currently points to the second byte of a table-header.
//	** Specifically, to the following:
//	**
//	**   + number of columns in table (varint)
//	**   + array of PK flags (1 byte per column),
//	**   + table name (nul terminated).
//	**
//	** This function decodes the table-header and populates the p->nCol,
//	** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
//	** also allocated or resized according to the new value of p->nCol. The
//	** input pointer is left pointing to the byte following the table header.
//	**
//	** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
//	** is returned and the final values of the various fields enumerated above
//	** are undefined.
//	*/
func _sessionChangesetReadTblhdr(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iPK Tsize_t
	var nByte, nVarint, v2 int32
	var v1 uintptr
	var _ /* nCopy at bp+4 */ int32
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _ = iPK, nByte, nVarint, v1, v2
	**(**int32)(__ccgo_up(bp)) = _sessionChangesetBufferTblhdr(tls, p, bp+4)
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		nVarint = _sessionVarintGet(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData+uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext), p+80)
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol > 0 {
			**(**int32)(__ccgo_up(bp + 4)) = **(**int32)(__ccgo_up(bp + 4)) - nVarint
			(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext += nVarint
			nByte = int32(uint32((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*uint32(4)*uint32(2) + uint32(**(**int32)(__ccgo_up(bp + 4))))
			(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FnBuf = 0
			_sessionBufferGrow(tls, p+44, int64(nByte), bp)
		} else {
			**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(237501))
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		iPK = uint32(4) * uint32((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) * uint32(2)
		libc.Xmemset(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf, 0, iPK)
		libc.Xmemcpy(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf+uintptr(iPK), (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FaData+uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext), uint32(**(**int32)(__ccgo_up(bp + 4))))
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext += **(**int32)(__ccgo_up(bp + 4))
	}
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf
	if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue == uintptr(0) {
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK = uintptr(0)
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FzTab = uintptr(0)
	} else {
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol*int32(2))*4
		if (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK != 0 {
			v1 = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FabPK + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)
		} else {
			v1 = uintptr(0)
		}
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FzTab = v1
	}
	v2 = **(**int32)(__ccgo_up(bp))
	(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Frc = v2
	return v2
}

// C documentation
//
//	/*
//	** Invoke the conflict handler for the change that the changeset iterator
//	** currently points to.
//	**
//	** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
//	** If argument pbReplace is NULL, then the type of conflict handler invoked
//	** depends solely on eType, as follows:
//	**
//	**    eType value                 Value passed to xConflict
//	**    -------------------------------------------------
//	**    CHANGESET_DATA              CHANGESET_NOTFOUND
//	**    CHANGESET_CONFLICT          CHANGESET_CONSTRAINT
//	**
//	** Or, if pbReplace is not NULL, then an attempt is made to find an existing
//	** record with the same primary key as the record about to be deleted, updated
//	** or inserted. If such a record can be found, it is available to the conflict
//	** handler as the "conflicting" record. In this case the type of conflict
//	** handler invoked is as follows:
//	**
//	**    eType value         PK Record found?   Value passed to xConflict
//	**    ----------------------------------------------------------------
//	**    CHANGESET_DATA      Yes                CHANGESET_DATA
//	**    CHANGESET_DATA      No                 CHANGESET_NOTFOUND
//	**    CHANGESET_CONFLICT  Yes                CHANGESET_CONFLICT
//	**    CHANGESET_CONFLICT  No                 CHANGESET_CONSTRAINT
//	**
//	** If pbReplace is not NULL, and a record with a matching PK is found, and
//	** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
//	** is set to non-zero before returning SQLITE_OK.
//	**
//	** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
//	** returned. Or, if the conflict handler returns an invalid value,
//	** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
//	** this function returns SQLITE_OK.
//	*/
func _sessionConflictHandler(tls *libc.TLS, eType int32, p uintptr, pIter uintptr, __ccgo_fp_xConflict uintptr, pCtx uintptr, pbReplace uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aBlob uintptr
	var nBlob, res int32
	var _ /* nCol at bp+4 */ int32
	var _ /* op at bp+8 */ int32
	var _ /* rc at bp+0 */ int32
	var _ /* zDummy at bp+12 */ uintptr
	_, _, _ = aBlob, nBlob, res
	res = SQLITE_CHANGESET_OMIT
	Xsqlite3changeset_op(tls, pIter, bp+12, bp+4, bp+8, uintptr(0))
	/* Bind the new.* PRIMARY KEY values to the SELECT statement. */
	if pbReplace != 0 {
		**(**int32)(__ccgo_up(bp)) = _sessionSeekToRow(tls, pIter, p)
	} else {
		**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	}
	if **(**int32)(__ccgo_up(bp)) == int32(SQLITE_ROW) {
		/* There exists another row with the new.* primary key. */
		if 0 == int32((*TSessionApplyCtx)(unsafe.Pointer(p)).FbIgnoreNoop) || 0 == Xsqlite3_column_int(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect, Xsqlite3_column_count(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect)-int32(1)) {
			(*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FpConflict = (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect
			res = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xConflict})))(tls, pCtx, eType, pIter)
			(*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FpConflict = uintptr(0)
		}
		**(**int32)(__ccgo_up(bp)) = Xsqlite3_reset(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect)
	} else {
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			if (*TSessionApplyCtx)(unsafe.Pointer(p)).FbDeferConstraints != 0 && eType == int32(SQLITE_CHANGESET_CONFLICT) {
				/* Instead of invoking the conflict handler, append the change blob
				 ** to the SessionApplyCtx.constraints buffer. */
				aBlob = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FaData + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiCurrent)
				nBlob = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiNext - (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiCurrent
				_sessionAppendBlob(tls, p+48, aBlob, nBlob, bp)
				return **(**int32)(__ccgo_up(bp))
			} else {
				if int32((*TSessionApplyCtx)(unsafe.Pointer(p)).FbIgnoreNoop) == 0 || **(**int32)(__ccgo_up(bp + 8)) != int32(SQLITE_DELETE) || eType == int32(SQLITE_CHANGESET_CONFLICT) {
					/* No other row with the new.* primary key. */
					res = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xConflict})))(tls, pCtx, eType+int32(1), pIter)
					if res == int32(SQLITE_CHANGESET_REPLACE) {
						**(**int32)(__ccgo_up(bp)) = int32(SQLITE_MISUSE)
					}
				}
			}
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		switch res {
		case int32(SQLITE_CHANGESET_REPLACE):
			**(**int32)(__ccgo_up(pbReplace)) = int32(1)
		case SQLITE_CHANGESET_OMIT:
		case int32(SQLITE_CHANGESET_ABORT):
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ABORT)
		default:
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_MISUSE)
			break
		}
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp)) = _sessionRebaseAdd(tls, p, res, pIter)
		}
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Formulate a statement to DELETE a row from database db. Assuming a table
//	** structure like this:
//	**
//	**     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
//	**
//	** The DELETE statement looks like this:
//	**
//	**     DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
//	**
//	** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
//	** matching b and d values, or 1 otherwise. The second case comes up if the
//	** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
//	**
//	** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
//	** pointing to the prepared version of the SQL statement.
//	*/
func _sessionDeleteRow(tls *libc.TLS, db uintptr, zTab uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, nPk int32
	var zSep uintptr
	var _ /* buf at bp+4 */ TSessionBuffer
	var _ /* rc at bp+0 */ int32
	_, _, _ = i, nPk, zSep
	zSep = __ccgo_ts + 1711
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	**(**TSessionBuffer)(__ccgo_up(bp + 4)) = TSessionBuffer{}
	nPk = 0
	_sessionAppendStr(tls, bp+4, __ccgo_ts+37243, bp)
	_sessionAppendIdent(tls, bp+4, zTab, bp)
	_sessionAppendStr(tls, bp+4, __ccgo_ts+37154, bp)
	i = 0
	for {
		if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) {
			break
		}
		if **(**Tu8)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 {
			nPk = nPk + 1
			_sessionAppendStr(tls, bp+4, zSep, bp)
			_sessionAppendIdent(tls, bp+4, **(**uintptr)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(i)*4)), bp)
			_sessionAppendStr(tls, bp+4, __ccgo_ts+37149, bp)
			_sessionAppendInteger(tls, bp+4, i+int32(1), bp)
			zSep = __ccgo_ts + 24859
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if nPk < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol {
		_sessionAppendStr(tls, bp+4, __ccgo_ts+37261, bp)
		_sessionAppendInteger(tls, bp+4, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol+int32(1), bp)
		_sessionAppendStr(tls, bp+4, __ccgo_ts+36673, bp)
		zSep = __ccgo_ts + 1711
		i = 0
		for {
			if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) {
				break
			}
			if !(**(**Tu8)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0) {
				_sessionAppendStr(tls, bp+4, zSep, bp)
				_sessionAppendIdent(tls, bp+4, **(**uintptr)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(i)*4)), bp)
				_sessionAppendStr(tls, bp+4, __ccgo_ts+37237, bp)
				_sessionAppendInteger(tls, bp+4, i+int32(1), bp)
				zSep = __ccgo_ts + 37269
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		_sessionAppendStr(tls, bp+4, __ccgo_ts+6474, bp)
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp)) = _sessionPrepare(tls, db, p+4, p+80, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf)
	}
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf)
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** If the SessionInput object passed as the only argument is a streaming
//	** object and the buffer is full, discard some data to free up space.
//	*/
func _sessionDiscardData(tls *libc.TLS, pIn uintptr) {
	var nMove int32
	_ = nMove
	if (*TSessionInput)(unsafe.Pointer(pIn)).FxInput != 0 && (*TSessionInput)(unsafe.Pointer(pIn)).FiCurrent >= _sessions_strm_chunk_size {
		nMove = (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FnBuf - (*TSessionInput)(unsafe.Pointer(pIn)).FiCurrent
		if nMove > 0 {
			libc.Xmemmove(tls, (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FaBuf, (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FaBuf+uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiCurrent), uint32(nMove))
		}
		(*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FnBuf -= (*TSessionInput)(unsafe.Pointer(pIn)).FiCurrent
		**(**int32)(__ccgo_up(pIn + 8)) -= (*TSessionInput)(unsafe.Pointer(pIn)).FiCurrent
		(*TSessionInput)(unsafe.Pointer(pIn)).FiCurrent = 0
		(*TSessionInput)(unsafe.Pointer(pIn)).FnData = (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FnBuf
	}
}

func _sessionExprCompareOther(tls *libc.TLS, nCol int32, zDb1 uintptr, zDb2 uintptr, zTab uintptr, azCol uintptr, abPK uintptr) (r uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var bHave, i int32
	var zRet, zSep uintptr
	_, _, _, _ = bHave, i, zRet, zSep
	zSep = __ccgo_ts + 1711
	zRet = uintptr(0)
	bHave = 0
	i = 0
	for {
		if !(i < nCol) {
			break
		}
		if int32(**(**Tu8)(__ccgo_up(abPK + uintptr(i)))) == 0 {
			bHave = int32(1)
			zRet = Xsqlite3_mprintf(tls, __ccgo_ts+36632, libc.VaList(bp+8, zRet, zSep, zDb1, zTab, **(**uintptr)(__ccgo_up(azCol + uintptr(i)*4)), zDb2, zTab, **(**uintptr)(__ccgo_up(azCol + uintptr(i)*4))))
			zSep = __ccgo_ts + 36673
			if zRet == uintptr(0) {
				break
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if bHave == 0 {
		zRet = Xsqlite3_mprintf(tls, __ccgo_ts+1857, 0)
	}
	return zRet
}

func _sessionExprComparePK(tls *libc.TLS, nCol int32, zDb1 uintptr, zDb2 uintptr, zTab uintptr, azCol uintptr, abPK uintptr) (r uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var i int32
	var zRet, zSep uintptr
	_, _, _ = i, zRet, zSep
	zSep = __ccgo_ts + 1711
	zRet = uintptr(0)
	i = 0
	for {
		if !(i < nCol) {
			break
		}
		if **(**Tu8)(__ccgo_up(abPK + uintptr(i))) != 0 {
			zRet = Xsqlite3_mprintf(tls, __ccgo_ts+36598, libc.VaList(bp+8, zRet, zSep, zDb1, zTab, **(**uintptr)(__ccgo_up(azCol + uintptr(i)*4)), zDb2, zTab, **(**uintptr)(__ccgo_up(azCol + uintptr(i)*4))))
			zSep = __ccgo_ts + 24859
			if zRet == uintptr(0) {
				break
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return zRet
}

// C documentation
//
//	/*
//	** Generate either a changeset (if argument bPatchset is zero) or a patchset
//	** (if it is non-zero) based on the current contents of the session object
//	** passed as the first argument.
//	**
//	** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
//	** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
//	** occurs, an SQLite error code is returned and both output variables set
//	** to 0.
//	*/
func _sessionGenerateChangeset(tls *libc.TLS, pSession uintptr, bPatchset int32, __ccgo_fp_xOutput uintptr, pOut uintptr, pnChangeset uintptr, ppChangeset uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, p, pTab, zName uintptr
	var i, iCol, nNoop, nOldCol, nRewind int32
	var _ /* buf at bp+0 */ TSessionBuffer
	var _ /* pSel at bp+16 */ uintptr
	var _ /* rc at bp+12 */ int32
	_, _, _, _, _, _, _, _, _ = db, i, iCol, nNoop, nOldCol, nRewind, p, pTab, zName
	db = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb /* Used to iterate through attached tables */
	**(**TSessionBuffer)(__ccgo_up(bp)) = TSessionBuffer{} /* Return code */
	/* Zero the output variables in case an error occurs. If this session
	 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
	 ** this call will be a no-op.  */
	if __ccgo_fp_xOutput == uintptr(0) {
		**(**int32)(__ccgo_up(pnChangeset)) = 0
		**(**uintptr)(__ccgo_up(ppChangeset)) = uintptr(0)
	}
	if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc != 0 {
		return (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc
	}
	Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, db))
	**(**int32)(__ccgo_up(bp + 12)) = Xsqlite3_exec(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, __ccgo_ts+37092, uintptr(0), uintptr(0), uintptr(0))
	if **(**int32)(__ccgo_up(bp + 12)) != SQLITE_OK {
		Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, db))
		return **(**int32)(__ccgo_up(bp + 12))
	}
	pTab = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable
	for {
		if !(**(**int32)(__ccgo_up(bp + 12)) == SQLITE_OK && pTab != 0) {
			break
		}
		if (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry != 0 {
			zName = (*TSessionTable)(unsafe.Pointer(pTab)).FzName /* Used to iterate through hash buckets */
			**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)        /* SELECT statement to query table pTab */
			nRewind = (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf /* Size of buffer after writing tbl header */
			nOldCol = (*TSessionTable)(unsafe.Pointer(pTab)).FnCol
			/* Check the table schema is still Ok. */
			**(**int32)(__ccgo_up(bp + 12)) = _sessionReinitTable(tls, pSession, pTab)
			if **(**int32)(__ccgo_up(bp + 12)) == SQLITE_OK && (*TSessionTable)(unsafe.Pointer(pTab)).FnCol != nOldCol {
				**(**int32)(__ccgo_up(bp + 12)) = _sessionUpdateChanges(tls, pSession, pTab)
			}
			/* Write a table header */
			_sessionAppendTableHdr(tls, bp, bPatchset, pTab, bp+12)
			/* Build and compile a statement to execute: */
			if **(**int32)(__ccgo_up(bp + 12)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp + 12)) = _sessionSelectStmt(tls, db, 0, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb, zName, (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, (*TSessionTable)(unsafe.Pointer(pTab)).FazCol, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, bp+16, uintptr(0))
			}
			nNoop = (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf
			i = 0
			for {
				if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange && **(**int32)(__ccgo_up(bp + 12)) == SQLITE_OK) {
					break
				} /* Used to iterate through changes */
				p = **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(i)*4))
				for {
					if !(**(**int32)(__ccgo_up(bp + 12)) == SQLITE_OK && p != 0) {
						break
					}
					**(**int32)(__ccgo_up(bp + 12)) = _sessionSelectBind(tls, **(**uintptr)(__ccgo_up(bp + 16)), (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, p)
					if **(**int32)(__ccgo_up(bp + 12)) != SQLITE_OK {
						goto _3
					}
					if Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 16))) == int32(SQLITE_ROW) {
						if int32((*TSessionChange)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INSERT) {
							_sessionAppendByte(tls, bp, uint8(SQLITE_INSERT), bp+12)
							_sessionAppendByte(tls, bp, (*TSessionChange)(unsafe.Pointer(p)).FbIndirect, bp+12)
							iCol = 0
							for {
								if !(iCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
									break
								}
								_sessionAppendCol(tls, bp, **(**uintptr)(__ccgo_up(bp + 16)), iCol, bp+12)
								goto _4
							_4:
								;
								iCol = iCol + 1
							}
						} else {
							**(**int32)(__ccgo_up(bp + 12)) = _sessionAppendUpdate(tls, bp, bPatchset, **(**uintptr)(__ccgo_up(bp + 16)), p, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK)
						}
					} else {
						if int32((*TSessionChange)(unsafe.Pointer(p)).Fop) != int32(SQLITE_INSERT) {
							**(**int32)(__ccgo_up(bp + 12)) = _sessionAppendDelete(tls, bp, bPatchset, p, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK)
						}
					}
					if **(**int32)(__ccgo_up(bp + 12)) == SQLITE_OK {
						**(**int32)(__ccgo_up(bp + 12)) = Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp + 16)))
					}
					/* If the buffer is now larger than sessions_strm_chunk_size, pass
					 ** its contents to the xOutput() callback. */
					if __ccgo_fp_xOutput != 0 && **(**int32)(__ccgo_up(bp + 12)) == SQLITE_OK && (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf > nNoop && (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf > _sessions_strm_chunk_size {
						**(**int32)(__ccgo_up(bp + 12)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xOutput})))(tls, pOut, (**(**TSessionBuffer)(__ccgo_up(bp))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf)
						nNoop = -int32(1)
						(**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf = 0
					}
					goto _3
				_3:
					;
					p = (*TSessionChange)(unsafe.Pointer(p)).FpNext
				}
				goto _2
			_2:
				;
				i = i + 1
			}
			Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 16)))
			if (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf == nNoop {
				(**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf = nRewind
			}
		}
		goto _1
	_1:
		;
		pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext
	}
	if **(**int32)(__ccgo_up(bp + 12)) == SQLITE_OK {
		if __ccgo_fp_xOutput == uintptr(0) {
			**(**int32)(__ccgo_up(pnChangeset)) = (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf
			**(**uintptr)(__ccgo_up(ppChangeset)) = (**(**TSessionBuffer)(__ccgo_up(bp))).FaBuf
			(**(**TSessionBuffer)(__ccgo_up(bp))).FaBuf = uintptr(0)
		} else {
			if (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf > 0 {
				**(**int32)(__ccgo_up(bp + 12)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xOutput})))(tls, pOut, (**(**TSessionBuffer)(__ccgo_up(bp))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf)
			}
		}
	}
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp))).FaBuf)
	Xsqlite3_exec(tls, db, __ccgo_ts+37112, uintptr(0), uintptr(0), uintptr(0))
	Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, db))
	return **(**int32)(__ccgo_up(bp + 12))
}

// C documentation
//
//	/*
//	** If required, grow the hash table used to store changes on table pTab
//	** (part of the session pSession). If a fatal OOM error occurs, set the
//	** session object to failed and return SQLITE_ERROR. Otherwise, return
//	** SQLITE_OK.
//	**
//	** It is possible that a non-fatal OOM error occurs in this function. In
//	** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
//	** Growing the hash table in this case is a performance optimization only,
//	** it is not required for correct operation.
//	*/
func _sessionGrowHash(tls *libc.TLS, pSession uintptr, bPatchset int32, pTab uintptr) (r int32) {
	var apNew, p, pNext uintptr
	var bPkOnly, i, iHash, v1 int32
	var nNew Tsqlite3_int64
	_, _, _, _, _, _, _, _ = apNew, bPkOnly, i, iHash, nNew, p, pNext, v1
	if (*TSessionTable)(unsafe.Pointer(pTab)).FnChange == 0 || (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry >= (*TSessionTable)(unsafe.Pointer(pTab)).FnChange/int32(2) {
		if (*TSessionTable)(unsafe.Pointer(pTab)).FnChange != 0 {
			v1 = (*TSessionTable)(unsafe.Pointer(pTab)).FnChange
		} else {
			v1 = int32(128)
		}
		nNew = int64(2) * int64(v1)
		apNew = _sessionMalloc64(tls, pSession, int64(4)*nNew)
		if apNew == uintptr(0) {
			if (*TSessionTable)(unsafe.Pointer(pTab)).FnChange == 0 {
				return int32(SQLITE_ERROR)
			}
			return SQLITE_OK
		}
		libc.Xmemset(tls, apNew, 0, uint32(int64(4)*nNew))
		i = 0
		for {
			if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange) {
				break
			}
			p = **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(i)*4))
			for {
				if !(p != 0) {
					break
				}
				bPkOnly = libc.BoolInt32(int32((*TSessionChange)(unsafe.Pointer(p)).Fop) == int32(SQLITE_DELETE) && bPatchset != 0)
				iHash = int32(_sessionChangeHash(tls, pTab, bPkOnly, (*TSessionChange)(unsafe.Pointer(p)).FaRecord, int32(nNew)))
				pNext = (*TSessionChange)(unsafe.Pointer(p)).FpNext
				(*TSessionChange)(unsafe.Pointer(p)).FpNext = **(**uintptr)(__ccgo_up(apNew + uintptr(iHash)*4))
				**(**uintptr)(__ccgo_up(apNew + uintptr(iHash)*4)) = p
				goto _3
			_3:
				;
				p = pNext
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		_sessionFree(tls, pSession, (*TSessionTable)(unsafe.Pointer(pTab)).FapChange)
		(*TSessionTable)(unsafe.Pointer(pTab)).FnChange = int32(nNew)
		(*TSessionTable)(unsafe.Pointer(pTab)).FapChange = apNew
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This function is called to initialize the SessionTable.nCol, azCol[]
//	** abPK[] and azDflt[] members of SessionTable object pTab. If these
//	** fields are already initialized, this function is a no-op.
//	**
//	** If an error occurs, an error code is stored in sqlite3_session.rc and
//	** non-zero returned. Or, if no error occurs but the table has no primary
//	** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
//	** indicate that updates on this table should be ignored. SessionTable.abPK
//	** is set to NULL in this case.
//	*/
func _sessionInitTable(tls *libc.TLS, pSession uintptr, pTab uintptr, db uintptr, zDb uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, rc int32
	var v1 uintptr
	var _ /* abPK at bp+0 */ uintptr
	_, _, _ = i, rc, v1
	rc = SQLITE_OK
	if (*TSessionTable)(unsafe.Pointer(pTab)).FnCol == 0 {
		Xsqlite3_free(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FazCol)
		(*TSessionTable)(unsafe.Pointer(pTab)).FabPK = uintptr(0)
		if pSession == uintptr(0) || (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK != 0 {
			v1 = pTab + 20
		} else {
			v1 = uintptr(0)
		}
		rc = _sessionTableInfo(tls, pSession, db, zDb, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, pTab+8, pTab+12, uintptr(0), pTab+24, pTab+28, pTab+32, bp, v1)
		if rc == SQLITE_OK {
			i = 0
			for {
				if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
					break
				}
				if **(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(i))) != 0 {
					(*TSessionTable)(unsafe.Pointer(pTab)).FabPK = **(**uintptr)(__ccgo_up(bp))
					break
				}
				goto _2
			_2:
				;
				i = i + 1
			}
			if 0 == Xsqlite3_stricmp(tls, __ccgo_ts+14050, (*TSessionTable)(unsafe.Pointer(pTab)).FzName) {
				(*TSessionTable)(unsafe.Pointer(pTab)).FbStat1 = int32(1)
			}
			if pSession != 0 && (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize != 0 {
				**(**Ti64)(__ccgo_up(pSession + 48)) += int64(uint32(libc.Int32FromInt32(1)+_sessionVarintLen(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol)+(*TSessionTable)(unsafe.Pointer(pTab)).FnCol) + libc.Xstrlen(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName) + libc.Uint32FromInt32(1))
			}
		}
	}
	if pSession != 0 {
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = rc
		return libc.BoolInt32(rc != 0 || (*TSessionTable)(unsafe.Pointer(pTab)).FabPK == uintptr(0))
	}
	return rc
}

// C documentation
//
//	/*
//	** Formulate and prepare an INSERT statement to add a record to table zTab.
//	** For example:
//	**
//	**     INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
//	**
//	** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
//	** pointing to the prepared version of the SQL statement.
//	*/
func _sessionInsertRow(tls *libc.TLS, db uintptr, zTab uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i int32
	var _ /* buf at bp+4 */ TSessionBuffer
	var _ /* rc at bp+0 */ int32
	_ = i
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	**(**TSessionBuffer)(__ccgo_up(bp + 4)) = TSessionBuffer{}
	_sessionAppendStr(tls, bp+4, __ccgo_ts+37274, bp)
	_sessionAppendIdent(tls, bp+4, zTab, bp)
	_sessionAppendStr(tls, bp+4, __ccgo_ts+24865, bp)
	i = 0
	for {
		if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) {
			break
		}
		if i != 0 {
			_sessionAppendStr(tls, bp+4, __ccgo_ts+17436, bp)
		}
		_sessionAppendIdent(tls, bp+4, **(**uintptr)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(i)*4)), bp)
		goto _1
	_1:
		;
		i = i + 1
	}
	_sessionAppendStr(tls, bp+4, __ccgo_ts+37292, bp)
	i = int32(1)
	for {
		if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) {
			break
		}
		_sessionAppendStr(tls, bp+4, __ccgo_ts+37303, bp)
		goto _2
	_2:
		;
		i = i + 1
	}
	_sessionAppendStr(tls, bp+4, __ccgo_ts+6474, bp)
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp)) = _sessionPrepare(tls, db, p+8, p+80, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf)
	}
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf)
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Allocate and return a pointer to a buffer nByte bytes in size. If
//	** pSession is not NULL, increase the sqlite3_session.nMalloc variable
//	** by the number of bytes allocated.
//	*/
func _sessionMalloc64(tls *libc.TLS, pSession uintptr, nByte Ti64) (r uintptr) {
	var pRet, v1 uintptr
	_, _ = pRet, v1
	pRet = Xsqlite3_malloc64(tls, uint64(nByte))
	if pSession != 0 {
		v1 = pSession + 40
		*(*Ti64)(unsafe.Pointer(v1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(v1))) + Xsqlite3_msize(tls, pRet))
	}
	return pRet
}

// C documentation
//
//	/*
//	** Arguments aLeft and aRight both point to buffers containing change
//	** records with nCol columns. This function "merges" the two records into
//	** a single records which is written to the buffer at *paOut. *paOut is
//	** then set to point to one byte after the last byte written before
//	** returning.
//	**
//	** The merging of records is done as follows: For each column, if the
//	** aRight record contains a value for the column, copy the value from
//	** their. Otherwise, if aLeft contains a value, copy it. If neither
//	** record contains a value for a given column, then neither does the
//	** output record.
//	*/
func _sessionMergeRecord(tls *libc.TLS, paOut uintptr, nCol int32, aLeft uintptr, aRight uintptr) {
	var a1, a2, aOut uintptr
	var iCol, n1, n2 int32
	_, _, _, _, _, _ = a1, a2, aOut, iCol, n1, n2
	a1 = aLeft                             /* Cursor used to iterate through aLeft */
	a2 = aRight                            /* Cursor used to iterate through aRight */
	aOut = **(**uintptr)(__ccgo_up(paOut)) /* Used to iterate from 0 to nCol */
	iCol = 0
	for {
		if !(iCol < nCol) {
			break
		}
		n1 = _sessionSerialLen(tls, a1)
		n2 = _sessionSerialLen(tls, a2)
		if **(**Tu8)(__ccgo_up(a2)) != 0 {
			libc.Xmemcpy(tls, aOut, a2, uint32(n2))
			aOut = aOut + uintptr(n2)
		} else {
			libc.Xmemcpy(tls, aOut, a1, uint32(n1))
			aOut = aOut + uintptr(n1)
		}
		a1 = a1 + uintptr(n1)
		a2 = a2 + uintptr(n2)
		goto _1
	_1:
		;
		iCol = iCol + 1
	}
	**(**uintptr)(__ccgo_up(paOut)) = aOut
}

// C documentation
//
//	/*
//	** This function is used by changeset_concat() to merge two UPDATE changes
//	** on the same row.
//	*/
func _sessionMergeUpdate(tls *libc.TLS, paOut uintptr, pTab uintptr, bPatchset int32, aOldRecord1 uintptr, aOldRecord2 uintptr, aNewRecord1 uintptr, aNewRecord2 uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aNew, aNew1, aOld, aOld1, aOut, v2 uintptr
	var bRequired, i int32
	var _ /* aNew1 at bp+8 */ uintptr
	var _ /* aNew2 at bp+12 */ uintptr
	var _ /* aOld1 at bp+0 */ uintptr
	var _ /* aOld2 at bp+4 */ uintptr
	var _ /* nNew at bp+20 */ int32
	var _ /* nNew at bp+28 */ int32
	var _ /* nOld at bp+16 */ int32
	var _ /* nOld at bp+24 */ int32
	_, _, _, _, _, _, _, _ = aNew, aNew1, aOld, aOld1, aOut, bRequired, i, v2
	**(**uintptr)(__ccgo_up(bp)) = aOldRecord1
	**(**uintptr)(__ccgo_up(bp + 4)) = aOldRecord2
	**(**uintptr)(__ccgo_up(bp + 8)) = aNewRecord1
	**(**uintptr)(__ccgo_up(bp + 12)) = aNewRecord2
	aOut = **(**uintptr)(__ccgo_up(paOut))
	if bPatchset == 0 {
		bRequired = 0
		/* Write the old.* vector first. */
		i = 0
		for {
			if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
				break
			}
			aOld = _sessionMergeValue(tls, bp, bp+4, bp+16)
			aNew = _sessionMergeValue(tls, bp+8, bp+12, bp+20)
			if **(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 || **(**int32)(__ccgo_up(bp + 16)) != **(**int32)(__ccgo_up(bp + 20)) || libc.Xmemcmp(tls, aOld, aNew, uint32(**(**int32)(__ccgo_up(bp + 20)))) != 0 {
				if int32(**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i)))) == 0 {
					bRequired = int32(1)
				}
				libc.Xmemcpy(tls, aOut, aOld, uint32(**(**int32)(__ccgo_up(bp + 16))))
				aOut = aOut + uintptr(**(**int32)(__ccgo_up(bp + 16)))
			} else {
				v2 = aOut
				aOut = aOut + 1
				**(**Tu8)(__ccgo_up(v2)) = uint8('\000')
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if !(bRequired != 0) {
			return 0
		}
	}
	/* Write the new.* vector */
	**(**uintptr)(__ccgo_up(bp)) = aOldRecord1
	**(**uintptr)(__ccgo_up(bp + 4)) = aOldRecord2
	**(**uintptr)(__ccgo_up(bp + 8)) = aNewRecord1
	**(**uintptr)(__ccgo_up(bp + 12)) = aNewRecord2
	i = 0
	for {
		if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
			break
		}
		aOld1 = _sessionMergeValue(tls, bp, bp+4, bp+24)
		aNew1 = _sessionMergeValue(tls, bp+8, bp+12, bp+28)
		if bPatchset == 0 && (**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 || **(**int32)(__ccgo_up(bp + 24)) == **(**int32)(__ccgo_up(bp + 28)) && 0 == libc.Xmemcmp(tls, aOld1, aNew1, uint32(**(**int32)(__ccgo_up(bp + 28))))) {
			v2 = aOut
			aOut = aOut + 1
			**(**Tu8)(__ccgo_up(v2)) = uint8('\000')
		} else {
			libc.Xmemcpy(tls, aOut, aNew1, uint32(**(**int32)(__ccgo_up(bp + 28))))
			aOut = aOut + uintptr(**(**int32)(__ccgo_up(bp + 28)))
		}
		goto _3
	_3:
		;
		i = i + 1
	}
	**(**uintptr)(__ccgo_up(paOut)) = aOut
	return int32(1)
}

// C documentation
//
//	/*
//	** Add a single change to the changegroup pGrp.
//	*/
func _sessionOneChangeToHash(tls *libc.TLS, pGrp uintptr, pTab uintptr, op int32, bIndirect int32, nCol int32, aRec uintptr, nRec int32, bRebase int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bPkOnly1, bPkOnly2, iHash, rc int32
	var pBuf, pExist, pp uintptr
	var _ /* pChange at bp+0 */ uintptr
	_, _, _, _, _, _, _ = bPkOnly1, bPkOnly2, iHash, pBuf, pExist, pp, rc
	rc = SQLITE_OK
	iHash = 0
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	pExist = uintptr(0)
	pp = uintptr(0)
	if nCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol {
		pBuf = pGrp + 12
		rc = _sessionChangesetExtendRecord(tls, pGrp, pTab, nCol, op, aRec, nRec, pBuf)
		aRec = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf
		nRec = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf
	}
	if rc == SQLITE_OK && _sessionGrowHash(tls, uintptr(0), (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch, pTab) != 0 {
		rc = int32(SQLITE_NOMEM)
	}
	if rc == SQLITE_OK {
		/* Search for existing entry. If found, remove it from the hash table.
		 ** Code below may link it back in.  */
		iHash = int32(_sessionChangeHash(tls, pTab, libc.BoolInt32((*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch != 0 && op == int32(SQLITE_DELETE)), aRec, (*TSessionTable)(unsafe.Pointer(pTab)).FnChange))
		pp = (*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(iHash)*4
		for {
			if !(**(**uintptr)(__ccgo_up(pp)) != 0) {
				break
			}
			bPkOnly1 = 0
			bPkOnly2 = 0
			if (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch != 0 {
				bPkOnly1 = libc.BoolInt32(int32((*TSessionChange)(unsafe.Pointer(**(**uintptr)(__ccgo_up(pp)))).Fop) == int32(SQLITE_DELETE))
				bPkOnly2 = libc.BoolInt32(op == int32(SQLITE_DELETE))
			}
			if _sessionChangeEqual(tls, pTab, bPkOnly1, (*TSessionChange)(unsafe.Pointer(**(**uintptr)(__ccgo_up(pp)))).FaRecord, bPkOnly2, aRec) != 0 {
				pExist = **(**uintptr)(__ccgo_up(pp))
				**(**uintptr)(__ccgo_up(pp)) = (*TSessionChange)(unsafe.Pointer(**(**uintptr)(__ccgo_up(pp)))).FpNext
				(*TSessionTable)(unsafe.Pointer(pTab)).FnEntry = (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry - 1
				break
			}
			goto _1
		_1:
			;
			pp = **(**uintptr)(__ccgo_up(pp)) + 16
		}
	}
	if rc == SQLITE_OK {
		rc = _sessionChangeMerge(tls, pTab, bRebase, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FbPatch, pExist, op, bIndirect, aRec, nRec, bp)
	}
	if rc == SQLITE_OK && **(**uintptr)(__ccgo_up(bp)) != 0 {
		(*TSessionChange)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpNext = **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(iHash)*4))
		**(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(iHash)*4)) = **(**uintptr)(__ccgo_up(bp))
		(*TSessionTable)(unsafe.Pointer(pTab)).FnEntry = (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry + 1
	}
	return rc
}

// C documentation
//
//	/*
//	** Prepare a statement against database handle db that SELECTs a single
//	** row containing the default values for each column in table pTab. For
//	** example, if pTab is declared as:
//	**
//	**   CREATE TABLE pTab(a PRIMARY KEY, b DEFAULT 123, c DEFAULT 'abcd');
//	**
//	** Then this function prepares and returns the SQL statement:
//	**
//	**   SELECT NULL, 123, 'abcd';
//	*/
func _sessionPrepareDfltStmt(tls *libc.TLS, db uintptr, pTab uintptr, ppStmt uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var ii int32
	var zDflt, zSep, v2 uintptr
	var _ /* rc at bp+12 */ int32
	var _ /* sql at bp+0 */ TSessionBuffer
	_, _, _, _ = ii, zDflt, zSep, v2
	**(**TSessionBuffer)(__ccgo_up(bp)) = TSessionBuffer{}
	**(**int32)(__ccgo_up(bp + 12)) = SQLITE_OK
	zSep = __ccgo_ts + 12758
	ii = 0
	**(**uintptr)(__ccgo_up(ppStmt)) = uintptr(0)
	_sessionAppendPrintf(tls, bp, bp+12, __ccgo_ts+36591, 0)
	ii = 0
	for {
		if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
			break
		}
		if **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FazDflt + uintptr(ii)*4)) != 0 {
			v2 = **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FazDflt + uintptr(ii)*4))
		} else {
			v2 = __ccgo_ts + 1712
		}
		zDflt = v2
		_sessionAppendPrintf(tls, bp, bp+12, __ccgo_ts+6444, libc.VaList(bp+24, zSep, zDflt))
		zSep = __ccgo_ts + 17436
		goto _1
	_1:
		;
		ii = ii + 1
	}
	if **(**int32)(__ccgo_up(bp + 12)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 12)) = Xsqlite3_prepare_v2(tls, db, (**(**TSessionBuffer)(__ccgo_up(bp))).FaBuf, -int32(1), ppStmt, uintptr(0))
	}
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp))).FaBuf)
	return **(**int32)(__ccgo_up(bp + 12))
}

// C documentation
//
//	/*
//	** This function is only called from within a pre-update-hook callback.
//	** It determines if the current pre-update-hook change affects the same row
//	** as the change stored in argument pChange. If so, it returns true. Otherwise
//	** if the pre-update-hook does not affect the same row as pChange, it returns
//	** false.
//	*/
func _sessionPreupdateEqual(tls *libc.TLS, pSession uintptr, iRowid Ti64, pTab uintptr, pChange uintptr, op int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var a, z, v2 uintptr
	var eType, iCol, iIdx, rc int32
	var _ /* iVal at bp+8 */ Ti64
	var _ /* n at bp+24 */ int32
	var _ /* pVal at bp+0 */ uintptr
	var _ /* rVal at bp+16 */ float64
	_, _, _, _, _, _, _ = a, eType, iCol, iIdx, rc, z, v2   /* Used to iterate through columns */
	a = (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord /* Cursor used to scan change record */
	if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 {
		if int32(**(**Tu8)(__ccgo_up(a))) != int32(SQLITE_INTEGER) {
			return 0
		}
		return libc.BoolInt32(_sessionGetI64(tls, a+1) == iRowid)
	}
	iCol = 0
	for {
		if !(iCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
			break
		}
		if !(**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(iCol))) != 0) {
			a = a + uintptr(_sessionSerialLen(tls, a))
		} else {
			v2 = a
			a = a + 1                               /* Error code from preupdate_new/old */
			eType = int32(**(**Tu8)(__ccgo_up(v2))) /* Type of value from change record */
			iIdx = **(**int32)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FaiIdx + uintptr(iCol)*4))
			/* The following calls to preupdate_new() and preupdate_old() can not
			 ** fail. This is because they cache their return values, and by the
			 ** time control flows to here they have already been called once from
			 ** within sessionPreupdateHash(). The first two asserts below verify
			 ** this (that the method has already been called). */
			if op == int32(SQLITE_INSERT) {
				/* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
				rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iIdx, bp)
			} else {
				/* assert( db->pPreUpdate->pUnpacked ); */
				rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iIdx, bp)
			}
			_ = rc /* Suppress warning about unused variable */
			if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(bp))) != eType {
				return 0
			}
			/* A SessionChange object never has a NULL value in a PK column */
			if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) {
				**(**Ti64)(__ccgo_up(bp + 8)) = _sessionGetI64(tls, a)
				a = a + uintptr(8)
				if eType == int32(SQLITE_INTEGER) {
					if Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(bp))) != **(**Ti64)(__ccgo_up(bp + 8)) {
						return 0
					}
				} else {
					libc.Xmemcpy(tls, bp+16, bp+8, uint32(8))
					if Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(bp))) != **(**float64)(__ccgo_up(bp + 16)) {
						return 0
					}
				}
			} else {
				a = a + uintptr(_sessionVarintGet(tls, a, bp+24))
				if Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(bp))) != **(**int32)(__ccgo_up(bp + 24)) {
					return 0
				}
				if eType == int32(SQLITE_TEXT) {
					z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(bp)))
				} else {
					z = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(bp)))
				}
				if **(**int32)(__ccgo_up(bp + 24)) > 0 && libc.Xmemcmp(tls, a, z, uint32(**(**int32)(__ccgo_up(bp + 24)))) != 0 {
					return 0
				}
				a = a + uintptr(**(**int32)(__ccgo_up(bp + 24)))
			}
		}
		goto _1
	_1:
		;
		iCol = iCol + 1
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** This function may only be called from within a pre-update callback.
//	** It calculates a hash based on the primary key values of the old.* or
//	** new.* row currently available and, assuming no error occurs, writes it to
//	** *piHash before returning. If the primary key contains one or more NULL
//	** values, *pbNullPK is set to true before returning.
//	**
//	** If an error occurs, an SQLite error code is returned and the final values
//	** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
//	** and the output variables are set as described above.
//	*/
func _sessionPreupdateHash(tls *libc.TLS, pSession uintptr, iRowid Ti64, pTab uintptr, bNew int32, piHash uintptr, pbNullPK uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var eType, i, iIdx, n, rc int32
	var h uint32
	var z uintptr
	var _ /* iVal at bp+8 */ Ti64
	var _ /* pVal at bp+0 */ uintptr
	var _ /* rVal at bp+16 */ float64
	_, _, _, _, _, _, _ = eType, h, i, iIdx, n, rc, z
	h = uint32(0) /* Used to iterate through columns */
	if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 {
		h = _sessionHashAppendI64(tls, h, iRowid)
	} else {
		i = 0
		for {
			if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
				break
			}
			if **(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 {
				iIdx = **(**int32)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FaiIdx + uintptr(i)*4))
				if bNew != 0 {
					rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iIdx, bp)
				} else {
					rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iIdx, bp)
				}
				if rc != SQLITE_OK {
					return rc
				}
				eType = Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(bp)))
				h = _sessionHashAppendType(tls, h, eType)
				if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) {
					if eType == int32(SQLITE_INTEGER) {
						**(**Ti64)(__ccgo_up(bp + 8)) = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(bp)))
					} else {
						**(**float64)(__ccgo_up(bp + 16)) = Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(bp)))
						libc.Xmemcpy(tls, bp+8, bp+16, uint32(8))
					}
					h = _sessionHashAppendI64(tls, h, **(**Ti64)(__ccgo_up(bp + 8)))
				} else {
					if eType == int32(SQLITE_TEXT) || eType == int32(SQLITE_BLOB) {
						if eType == int32(SQLITE_TEXT) {
							z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(bp)))
						} else {
							z = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(bp)))
						}
						n = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(bp)))
						if !(z != 0) && (eType != int32(SQLITE_BLOB) || n > 0) {
							return int32(SQLITE_NOMEM)
						}
						h = _sessionHashAppendBlob(tls, h, n, z)
					} else {
						**(**int32)(__ccgo_up(pbNullPK)) = int32(1)
					}
				}
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	**(**int32)(__ccgo_up(piHash)) = int32(h % uint32((*TSessionTable)(unsafe.Pointer(pTab)).FnChange))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This function is only called from with a pre-update-hook reporting a
//	** change on table pTab (attached to session pSession). The type of change
//	** (UPDATE, INSERT, DELETE) is specified by the first argument.
//	**
//	** Unless one is already present or an error occurs, an entry is added
//	** to the changed-rows hash table associated with table pTab.
//	*/
func _sessionPreupdateOneChange(tls *libc.TLS, op int32, iRowid Ti64, pSession uintptr, pTab uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i, iIdx, iIdx1, nExpect, rc int32
	var p, pC uintptr
	var _ /* bNull at bp+4 */ int32
	var _ /* iHash at bp+0 */ int32
	var _ /* nByte at bp+32 */ Tsqlite3_int64
	var _ /* p at bp+40 */ uintptr
	var _ /* p at bp+44 */ uintptr
	var _ /* stat1 at bp+8 */ TSessionStat1Ctx
	_, _, _, _, _, _, _ = i, iIdx, iIdx1, nExpect, p, pC, rc
	**(**int32)(__ccgo_up(bp + 4)) = 0
	rc = SQLITE_OK
	nExpect = 0
	**(**TSessionStat1Ctx)(__ccgo_up(bp + 8)) = TSessionStat1Ctx{}
	if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc != 0 {
		return
	}
	/* Load table details if required */
	if _sessionInitTable(tls, pSession, pTab, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb) != 0 {
		return
	}
	/* Check the number of columns in this xPreUpdate call matches the
	 ** number of columns in the table.  */
	nExpect = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxCount})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx)
	if (*TSessionTable)(unsafe.Pointer(pTab)).FnTotalCol < nExpect {
		if _sessionReinitTable(tls, pSession, pTab) != 0 {
			return
		}
		if _sessionUpdateChanges(tls, pSession, pTab) != 0 {
			return
		}
	}
	if (*TSessionTable)(unsafe.Pointer(pTab)).FnTotalCol != nExpect {
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_SCHEMA)
		return
	}
	/* Grow the hash table if required */
	if _sessionGrowHash(tls, pSession, 0, pTab) != 0 {
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_NOMEM)
		return
	}
	if (*TSessionTable)(unsafe.Pointer(pTab)).FbStat1 != 0 {
		(**(**TSessionStat1Ctx)(__ccgo_up(bp + 8))).Fhook = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook
		(**(**TSessionStat1Ctx)(__ccgo_up(bp + 8))).FpSession = pSession
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx = bp + 8
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew = __ccgo_fp(_sessionStat1New)
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld = __ccgo_fp(_sessionStat1Old)
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxCount = __ccgo_fp(_sessionStat1Count)
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxDepth = __ccgo_fp(_sessionStat1Depth)
		if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpZeroBlob == uintptr(0) {
			p = _sqlite3ValueNew(tls, uintptr(0))
			if p == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
				goto error_out
			}
			_sqlite3ValueSetStr(tls, p, 0, __ccgo_ts+1711, uint8(0), libc.UintptrFromInt32(0))
			(*Tsqlite3_session)(unsafe.Pointer(pSession)).FpZeroBlob = p
		}
	}
	/* Calculate the hash-key for this change. If the primary key of the row
	 ** includes a NULL value, exit early. Such changes are ignored by the
	 ** session module. */
	rc = _sessionPreupdateHash(tls, pSession, iRowid, pTab, libc.BoolInt32(op == int32(SQLITE_INSERT)), bp, bp+4)
	if rc != SQLITE_OK {
		goto error_out
	}
	if **(**int32)(__ccgo_up(bp + 4)) == 0 {
		pC = **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(**(**int32)(__ccgo_up(bp)))*4))
		for {
			if !(pC != 0) {
				break
			}
			if _sessionPreupdateEqual(tls, pSession, iRowid, pTab, pC, op) != 0 {
				break
			}
			goto _1
		_1:
			;
			pC = (*TSessionChange)(unsafe.Pointer(pC)).FpNext
		}
		if pC == uintptr(0) { /* Used to iterate through columns */
			(*TSessionTable)(unsafe.Pointer(pTab)).FnEntry = (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry + 1
			/* Figure out how large an allocation is required */
			**(**Tsqlite3_int64)(__ccgo_up(bp + 32)) = int64(20)
			i = (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid
			for {
				if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
					break
				}
				iIdx = **(**int32)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FaiIdx + uintptr(i)*4))
				**(**uintptr)(__ccgo_up(bp + 40)) = uintptr(0)
				if op != int32(SQLITE_INSERT) {
					/* This may fail if the column has a non-NULL default and was added
					 ** using ALTER TABLE ADD COLUMN after this record was created. */
					rc = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iIdx, bp+40)
				} else {
					if **(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 {
						(*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iIdx, bp+40)
					}
				}
				if rc == SQLITE_OK {
					/* This may fail if SQLite value p contains a utf-16 string that must
					 ** be converted to utf-8 and an OOM error occurs while doing so. */
					rc = _sessionSerializeValue(tls, uintptr(0), **(**uintptr)(__ccgo_up(bp + 40)), bp+32)
				}
				if rc != SQLITE_OK {
					goto error_out
				}
				goto _2
			_2:
				;
				i = i + 1
			}
			if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 {
				**(**Tsqlite3_int64)(__ccgo_up(bp + 32)) = **(**Tsqlite3_int64)(__ccgo_up(bp + 32)) + int64(9) /* Size of rowid field - an integer */
			}
			/* Allocate the change object */
			pC = _sessionMalloc64(tls, pSession, **(**Tsqlite3_int64)(__ccgo_up(bp + 32)))
			if !(pC != 0) {
				rc = int32(SQLITE_NOMEM)
				goto error_out
			} else {
				libc.Xmemset(tls, pC, 0, uint32(20))
				(*TSessionChange)(unsafe.Pointer(pC)).FaRecord = pC + 1*20
			}
			/* Populate the change object. None of the preupdate_old(),
			 ** preupdate_new() or SerializeValue() calls below may fail as all
			 ** required values and encodings have already been cached in memory.
			 ** It is not possible for an OOM to occur in this block. */
			**(**Tsqlite3_int64)(__ccgo_up(bp + 32)) = 0
			if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 {
				**(**Tu8)(__ccgo_up((*TSessionChange)(unsafe.Pointer(pC)).FaRecord)) = uint8(SQLITE_INTEGER)
				_sessionPutI64(tls, (*TSessionChange)(unsafe.Pointer(pC)).FaRecord+1, iRowid)
				**(**Tsqlite3_int64)(__ccgo_up(bp + 32)) = int64(9)
			}
			i = (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid
			for {
				if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
					break
				}
				**(**uintptr)(__ccgo_up(bp + 44)) = uintptr(0)
				iIdx1 = **(**int32)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FaiIdx + uintptr(i)*4))
				if op != int32(SQLITE_INSERT) {
					(*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxOld})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iIdx1, bp+44)
				} else {
					if **(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 {
						(*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iIdx1, bp+44)
					}
				}
				_sessionSerializeValue(tls, (*TSessionChange)(unsafe.Pointer(pC)).FaRecord+uintptr(**(**Tsqlite3_int64)(__ccgo_up(bp + 32))), **(**uintptr)(__ccgo_up(bp + 44)), bp+32)
				goto _3
			_3:
				;
				i = i + 1
			}
			/* Add the change to the hash-table */
			if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbIndirect != 0 || (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxDepth})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx) != 0 {
				(*TSessionChange)(unsafe.Pointer(pC)).FbIndirect = uint8(1)
			}
			(*TSessionChange)(unsafe.Pointer(pC)).FnRecordField = uint16((*TSessionTable)(unsafe.Pointer(pTab)).FnCol)
			(*TSessionChange)(unsafe.Pointer(pC)).FnRecord = int32(**(**Tsqlite3_int64)(__ccgo_up(bp + 32)))
			(*TSessionChange)(unsafe.Pointer(pC)).Fop = uint8(op)
			(*TSessionChange)(unsafe.Pointer(pC)).FpNext = **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(**(**int32)(__ccgo_up(bp)))*4))
			**(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(**(**int32)(__ccgo_up(bp)))*4)) = pC
		} else {
			if (*TSessionChange)(unsafe.Pointer(pC)).FbIndirect != 0 {
				/* If the existing change is considered "indirect", but this current
				 ** change is "direct", mark the change object as direct. */
				if (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxDepth})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx) == 0 && (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbIndirect == 0 {
					(*TSessionChange)(unsafe.Pointer(pC)).FbIndirect = uint8(0)
				}
			}
		}
		if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize != 0 {
			rc = _sessionUpdateMaxSize(tls, op, pSession, pTab, pC)
		}
	}
	/* If an error has occurred, mark the session object as failed. */
	goto error_out
error_out:
	;
	if (*TSessionTable)(unsafe.Pointer(pTab)).FbStat1 != 0 {
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook = (**(**TSessionStat1Ctx)(__ccgo_up(bp + 8))).Fhook
	}
	if rc != SQLITE_OK {
		(*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = rc
	}
}

// C documentation
//
//	/*
//	** Write a double value to the buffer aBuf[].
//	*/
func _sessionPutDouble(tls *libc.TLS, aBuf uintptr, _r float64) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*float64)(unsafe.Pointer(bp)) = _r
	var _ /* i at bp+8 */ Tu64
	libc.Xmemcpy(tls, bp+8, bp, uint32(8))
	_sessionPutI64(tls, aBuf, int64(**(**Tu64)(__ccgo_up(bp + 8))))
}

// C documentation
//
//	/*
//	** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
//	** for details.
//	**
//	** When this function is called, *paChange points to the start of the record
//	** to deserialize. Assuming no error occurs, *paChange is set to point to
//	** one byte after the end of the same record before this function returns.
//	** If the argument abPK is NULL, then the record contains nCol values. Or,
//	** if abPK is other than NULL, then the record contains only the PK fields
//	** (in other words, it is a patchset DELETE record).
//	**
//	** If successful, each element of the apOut[] array (allocated by the caller)
//	** is set to point to an sqlite3_value object containing the value read
//	** from the corresponding position in the record. If that value is not
//	** included in the record (i.e. because the record is part of an UPDATE change
//	** and the field was not modified), the corresponding element of apOut[] is
//	** set to NULL.
//	**
//	** It is the responsibility of the caller to free all sqlite_value structures
//	** using sqlite3_free().
//	**
//	** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
//	** The apOut[] array may have been partially populated in this case.
//	*/
func _sessionReadRecord(tls *libc.TLS, pIn uintptr, nCol int32, abPK uintptr, apOut uintptr, pbEmpty uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aVal, v3 uintptr
	var eType, i, nRem, rc, v2 int32
	var enc Tu8
	var _ /* d at bp+16 */ float64
	var _ /* nByte at bp+0 */ int32
	var _ /* v at bp+8 */ Tsqlite3_int64
	_, _, _, _, _, _, _, _ = aVal, eType, enc, i, nRem, rc, v2, v3 /* Used to iterate through columns */
	rc = SQLITE_OK
	if pbEmpty != 0 {
		**(**int32)(__ccgo_up(pbEmpty)) = int32(1)
	}
	i = 0
	for {
		if !(i < nCol && rc == SQLITE_OK) {
			break
		}
		eType = 0 /* Type of value (SQLITE_NULL, TEXT etc.) */
		if abPK != 0 && int32(**(**Tu8)(__ccgo_up(abPK + uintptr(i)))) == 0 {
			goto _1
		}
		rc = _sessionInputBuffer(tls, pIn, int32(9))
		if rc == SQLITE_OK {
			if (*TSessionInput)(unsafe.Pointer(pIn)).FiNext >= (*TSessionInput)(unsafe.Pointer(pIn)).FnData {
				rc = _sqlite3CorruptError(tls, int32(237320))
			} else {
				v3 = pIn + 8
				v2 = *(*int32)(unsafe.Pointer(v3))
				*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
				eType = int32(**(**Tu8)(__ccgo_up((*TSessionInput)(unsafe.Pointer(pIn)).FaData + uintptr(v2))))
				if eType != 0 {
					if pbEmpty != 0 {
						**(**int32)(__ccgo_up(pbEmpty)) = 0
					}
					**(**uintptr)(__ccgo_up(apOut + uintptr(i)*4)) = _sqlite3ValueNew(tls, uintptr(0))
					if !(**(**uintptr)(__ccgo_up(apOut + uintptr(i)*4)) != 0) {
						rc = int32(SQLITE_NOMEM)
					}
				}
			}
		}
		if rc == SQLITE_OK {
			aVal = (*TSessionInput)(unsafe.Pointer(pIn)).FaData + uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext)
			if eType == int32(SQLITE_TEXT) || eType == int32(SQLITE_BLOB) {
				nRem = (*TSessionInput)(unsafe.Pointer(pIn)).FnData - (*TSessionInput)(unsafe.Pointer(pIn)).FiNext
				**(**int32)(__ccgo_up(pIn + 8)) += _sessionVarintGetSafe(tls, aVal, nRem, bp)
				rc = _sessionInputBuffer(tls, pIn, **(**int32)(__ccgo_up(bp)))
				if rc == SQLITE_OK {
					if **(**int32)(__ccgo_up(bp)) < 0 || **(**int32)(__ccgo_up(bp)) > (*TSessionInput)(unsafe.Pointer(pIn)).FnData-(*TSessionInput)(unsafe.Pointer(pIn)).FiNext {
						rc = _sqlite3CorruptError(tls, int32(237341))
					} else {
						if eType == int32(SQLITE_TEXT) {
							v2 = int32(SQLITE_UTF8)
						} else {
							v2 = 0
						}
						enc = uint8(v2)
						rc = _sessionValueSetStr(tls, **(**uintptr)(__ccgo_up(apOut + uintptr(i)*4)), (*TSessionInput)(unsafe.Pointer(pIn)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext), **(**int32)(__ccgo_up(bp)), enc)
						**(**int32)(__ccgo_up(pIn + 8)) += **(**int32)(__ccgo_up(bp))
					}
				}
			}
			if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) {
				if (*TSessionInput)(unsafe.Pointer(pIn)).FnData-(*TSessionInput)(unsafe.Pointer(pIn)).FiNext < int32(8) {
					rc = _sqlite3CorruptError(tls, int32(237351))
				} else {
					**(**Tsqlite3_int64)(__ccgo_up(bp + 8)) = _sessionGetI64(tls, aVal)
					if eType == int32(SQLITE_INTEGER) {
						_sqlite3VdbeMemSetInt64(tls, **(**uintptr)(__ccgo_up(apOut + uintptr(i)*4)), **(**Tsqlite3_int64)(__ccgo_up(bp + 8)))
					} else {
						libc.Xmemcpy(tls, bp+16, bp+8, uint32(8))
						_sqlite3VdbeMemSetDouble(tls, **(**uintptr)(__ccgo_up(apOut + uintptr(i)*4)), **(**float64)(__ccgo_up(bp + 16)))
					}
					**(**int32)(__ccgo_up(pIn + 8)) += int32(8)
				}
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return rc
}

// C documentation
//
//	/*
//	** pIter is configured to iterate through a changeset. This function rebases
//	** that changeset according to the current configuration of the rebaser
//	** object passed as the first argument. If no error occurs and argument xOutput
//	** is not NULL, then the changeset is returned to the caller by invoking
//	** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
//	** then (*ppOut) is set to point to a buffer containing the rebased changeset
//	** before this function returns. In this case (*pnOut) is set to the size of
//	** the buffer in bytes.  It is the responsibility of the caller to eventually
//	** free the (*ppOut) buffer using sqlite3_free().
//	**
//	** If an error occurs, an SQLite error code is returned. If ppOut and
//	** pnOut are not NULL, then the two output parameters are set to 0 before
//	** returning.
//	*/
func _sessionRebase(tls *libc.TLS, p uintptr, pIter uintptr, __ccgo_fp_xOutput uintptr, pOut uintptr, pnOut uintptr, ppOut uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bDone, iHash, v2 int32
	var pChange, pTab, zTab uintptr
	var _ /* aRec at bp+4 */ uintptr
	var _ /* bNew at bp+12 */ int32
	var _ /* nRec at bp+8 */ int32
	var _ /* pCsr at bp+28 */ uintptr
	var _ /* rc at bp+0 */ int32
	var _ /* sOut at bp+16 */ TSessionBuffer
	_, _, _, _, _, _ = bDone, iHash, pChange, pTab, zTab, v2
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 8)) = 0
	**(**int32)(__ccgo_up(bp + 12)) = 0
	pTab = uintptr(0)
	**(**TSessionBuffer)(__ccgo_up(bp + 16)) = TSessionBuffer{}
	for int32(SQLITE_ROW) == _sessionChangesetNext(tls, pIter, bp+4, bp+8, bp+12) {
		pChange = uintptr(0)
		bDone = 0
		if **(**int32)(__ccgo_up(bp + 12)) != 0 {
			zTab = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab
			pTab = (*Tsqlite3_rebaser)(unsafe.Pointer(p)).Fgrp.FpList
			for {
				if !(pTab != 0) {
					break
				}
				if 0 == Xsqlite3_stricmp(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zTab) {
					break
				}
				goto _1
			_1:
				;
				pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext
			}
			**(**int32)(__ccgo_up(bp + 12)) = 0
			/* A patchset may not be rebased */
			if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0 {
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
			}
			/* Append a table header to the output for this new table */
			if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0 {
				v2 = int32('P')
			} else {
				v2 = int32('T')
			}
			_sessionAppendByte(tls, bp+16, uint8(v2), bp)
			_sessionAppendVarint(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, bp)
			_sessionAppendBlob(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, bp)
			_sessionAppendBlob(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab, int32(libc.Xstrlen(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab))+int32(1), bp)
		}
		if pTab != 0 && **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			iHash = int32(_sessionChangeHash(tls, pTab, 0, **(**uintptr)(__ccgo_up(bp + 4)), (*TSessionTable)(unsafe.Pointer(pTab)).FnChange))
			pChange = **(**uintptr)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(iHash)*4))
			for {
				if !(pChange != 0) {
					break
				}
				if _sessionChangeEqual(tls, pTab, 0, **(**uintptr)(__ccgo_up(bp + 4)), 0, (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord) != 0 {
					break
				}
				goto _3
			_3:
				;
				pChange = (*TSessionChange)(unsafe.Pointer(pChange)).FpNext
			}
		}
		if pChange != 0 {
			switch (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop {
			case int32(SQLITE_INSERT):
				if int32((*TSessionChange)(unsafe.Pointer(pChange)).Fop) == int32(SQLITE_INSERT) {
					bDone = int32(1)
					if int32((*TSessionChange)(unsafe.Pointer(pChange)).FbIndirect) == 0 {
						_sessionAppendByte(tls, bp+16, uint8(SQLITE_UPDATE), bp)
						_sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp)
						_sessionAppendBlob(tls, bp+16, (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, bp)
						_sessionAppendBlob(tls, bp+16, **(**uintptr)(__ccgo_up(bp + 4)), **(**int32)(__ccgo_up(bp + 8)), bp)
					}
				}
			case int32(SQLITE_UPDATE):
				bDone = int32(1)
				if int32((*TSessionChange)(unsafe.Pointer(pChange)).Fop) == int32(SQLITE_DELETE) {
					if int32((*TSessionChange)(unsafe.Pointer(pChange)).FbIndirect) == 0 {
						**(**uintptr)(__ccgo_up(bp + 28)) = **(**uintptr)(__ccgo_up(bp + 4))
						_sessionSkipRecord(tls, bp+28, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol)
						_sessionAppendByte(tls, bp+16, uint8(SQLITE_INSERT), bp)
						_sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp)
						_sessionAppendRecordMerge(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, **(**uintptr)(__ccgo_up(bp + 28)), **(**int32)(__ccgo_up(bp + 8))-(int32(**(**uintptr)(__ccgo_up(bp + 28)))-int32(**(**uintptr)(__ccgo_up(bp + 4)))), (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, bp)
					}
				} else {
					_sessionAppendPartialUpdate(tls, bp+16, pIter, **(**uintptr)(__ccgo_up(bp + 4)), **(**int32)(__ccgo_up(bp + 8)), (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, bp)
				}
			default:
				bDone = int32(1)
				if int32((*TSessionChange)(unsafe.Pointer(pChange)).Fop) == int32(SQLITE_INSERT) {
					_sessionAppendByte(tls, bp+16, uint8(SQLITE_DELETE), bp)
					_sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp)
					_sessionAppendRecordMerge(tls, bp+16, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, **(**uintptr)(__ccgo_up(bp + 4)), **(**int32)(__ccgo_up(bp + 8)), bp)
				}
				break
			}
		}
		if bDone == 0 {
			_sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop), bp)
			_sessionAppendByte(tls, bp+16, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp)
			_sessionAppendBlob(tls, bp+16, **(**uintptr)(__ccgo_up(bp + 4)), **(**int32)(__ccgo_up(bp + 8)), bp)
		}
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && __ccgo_fp_xOutput != 0 && (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FnBuf > _sessions_strm_chunk_size {
			**(**int32)(__ccgo_up(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xOutput})))(tls, pOut, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FnBuf)
			(**(**TSessionBuffer)(__ccgo_up(bp + 16))).FnBuf = 0
		}
		if **(**int32)(__ccgo_up(bp)) != 0 {
			break
		}
	}
	if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
		Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf)
		libc.Xmemset(tls, bp+16, 0, uint32(12))
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		if __ccgo_fp_xOutput != 0 {
			if (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FnBuf > 0 {
				**(**int32)(__ccgo_up(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xOutput})))(tls, pOut, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FnBuf)
			}
		} else {
			if ppOut != 0 {
				**(**uintptr)(__ccgo_up(ppOut)) = (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf
				**(**int32)(__ccgo_up(pnOut)) = (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FnBuf
				(**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf = uintptr(0)
			}
		}
	}
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf)
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** This function is called from within sqlite3changeset_apply_v2() when
//	** a conflict is encountered and resolved using conflict resolution
//	** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
//	** It adds a conflict resolution record to the buffer in
//	** SessionApplyCtx.rebase, which will eventually be returned to the caller
//	** of apply_v2() as the "rebase" buffer.
//	**
//	** Return SQLITE_OK if successful, or an SQLite error code otherwise.
//	*/
func _sessionRebaseAdd(tls *libc.TLS, p uintptr, eType int32, pIter uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var eOp, i, v1 int32
	var zTab uintptr
	var _ /* pVal at bp+4 */ uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _ = eOp, i, zTab, v1
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	if (*TSessionApplyCtx)(unsafe.Pointer(p)).FbRebase != 0 {
		eOp = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop
		if int32((*TSessionApplyCtx)(unsafe.Pointer(p)).FbRebaseStarted) == 0 {
			/* Append a table-header to the rebase buffer */
			zTab = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab
			_sessionAppendByte(tls, p+60, uint8('T'), bp)
			_sessionAppendVarint(tls, p+60, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol, bp)
			_sessionAppendBlob(tls, p+60, (*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol, bp)
			_sessionAppendBlob(tls, p+60, zTab, int32(libc.Xstrlen(tls, zTab))+int32(1), bp)
			(*TSessionApplyCtx)(unsafe.Pointer(p)).FbRebaseStarted = uint8(1)
		}
		if eOp == int32(SQLITE_DELETE) {
			v1 = int32(SQLITE_DELETE)
		} else {
			v1 = int32(SQLITE_INSERT)
		}
		_sessionAppendByte(tls, p+60, uint8(v1), bp)
		_sessionAppendByte(tls, p+60, libc.BoolUint8(eType == libc.Int32FromInt32(SQLITE_CHANGESET_REPLACE)), bp)
		i = 0
		for {
			if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) {
				break
			}
			**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
			if eOp == int32(SQLITE_DELETE) || eOp == int32(SQLITE_UPDATE) && **(**Tu8)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 {
				Xsqlite3changeset_old(tls, pIter, i, bp+4)
			} else {
				Xsqlite3changeset_new(tls, pIter, i, bp+4)
			}
			_sessionAppendValue(tls, p+60, **(**uintptr)(__ccgo_up(bp + 4)), bp)
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Re-initialize table object pTab.
//	*/
func _sessionReinitTable(tls *libc.TLS, pSession uintptr, pTab uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var a, v1 uintptr
	var ii, nOldCol int32
	var _ /* abPK at bp+20 */ uintptr
	var _ /* aiIdx at bp+16 */ uintptr
	var _ /* azCol at bp+8 */ uintptr
	var _ /* azDflt at bp+12 */ uintptr
	var _ /* bRowid at bp+24 */ int32
	var _ /* nCol at bp+0 */ int32
	var _ /* nTotalCol at bp+4 */ int32
	_, _, _, _ = a, ii, nOldCol, v1
	**(**int32)(__ccgo_up(bp)) = 0
	**(**int32)(__ccgo_up(bp + 4)) = 0
	**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
	**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
	**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)
	**(**uintptr)(__ccgo_up(bp + 20)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 24)) = 0
	if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK != 0 {
		v1 = bp + 24
	} else {
		v1 = uintptr(0)
	}
	(*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = _sessionTableInfo(tls, pSession, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, bp, bp+4, uintptr(0), bp+8, bp+12, bp+16, bp+20, v1)
	if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc == SQLITE_OK {
		if (*TSessionTable)(unsafe.Pointer(pTab)).FnCol > **(**int32)(__ccgo_up(bp)) || (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != **(**int32)(__ccgo_up(bp + 24)) {
			(*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_SCHEMA)
		} else {
			nOldCol = (*TSessionTable)(unsafe.Pointer(pTab)).FnCol
			ii = 0
			for {
				if !(ii < **(**int32)(__ccgo_up(bp))) {
					break
				}
				if ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol {
					if int32(**(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(ii)))) != int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 20)) + uintptr(ii)))) {
						(*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_SCHEMA)
					}
				} else {
					if **(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 20)) + uintptr(ii))) != 0 {
						(*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_SCHEMA)
					}
				}
				goto _2
			_2:
				;
				ii = ii + 1
			}
			if (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc == SQLITE_OK {
				a = (*TSessionTable)(unsafe.Pointer(pTab)).FazCol
				(*TSessionTable)(unsafe.Pointer(pTab)).FazCol = **(**uintptr)(__ccgo_up(bp + 8))
				(*TSessionTable)(unsafe.Pointer(pTab)).FnCol = **(**int32)(__ccgo_up(bp))
				(*TSessionTable)(unsafe.Pointer(pTab)).FnTotalCol = **(**int32)(__ccgo_up(bp + 4))
				(*TSessionTable)(unsafe.Pointer(pTab)).FazDflt = **(**uintptr)(__ccgo_up(bp + 12))
				(*TSessionTable)(unsafe.Pointer(pTab)).FabPK = **(**uintptr)(__ccgo_up(bp + 20))
				(*TSessionTable)(unsafe.Pointer(pTab)).FaiIdx = **(**uintptr)(__ccgo_up(bp + 16))
				**(**uintptr)(__ccgo_up(bp + 8)) = a
			}
			if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize != 0 {
				**(**Ti64)(__ccgo_up(pSession + 48)) += int64(**(**int32)(__ccgo_up(bp)) - nOldCol)
				**(**Ti64)(__ccgo_up(pSession + 48)) += int64(_sessionVarintLen(tls, **(**int32)(__ccgo_up(bp))))
				**(**Ti64)(__ccgo_up(pSession + 48)) -= int64(_sessionVarintLen(tls, nOldCol))
			}
		}
	}
	Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 8)))
	return (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc
}

// C documentation
//
//	/*
//	** Retry the changes accumulated in the pApply->constraints buffer. The
//	** pApply->constraints buffer contains all changes to table zTab that
//	** could not be applied due to SQLITE_CONSTRAINT errors. This function
//	** attempts to apply them as follows:
//	**
//	**   1) It runs through the buffer and attempts to retry each change,
//	**      removing any that are successfully applied from the buffer. This
//	**      is repeated until no further progress can be made.
//	**
//	**   2) For each UPDATE change in the buffer, try the following in a
//	**      savepoint transaction:
//	**
//	**      a) DELETE the affected row,
//	**      b) Attempt step (1) with remaining changes,
//	**      c) Attempt to INSERT a row equivalent to the one that would be
//	**         created by applying this UPDATE change.
//	**
//	**      If the INSERT in (c) succeeds, the savepoint is committed and all
//	**      successfully applied changes are removed from the buffer. Step (2)
//	**      is then repeated.
//	**
//	**   3) Once step (2) has been attempted for each UPDATE in the change,
//	**      a final attempt is made to apply each remaining change. This time,
//	**      if an SQLITE_CONSTRAINT error is encountered, the conflict handler
//	**      is invoked and the user has to decide whether to omit the change
//	**      or rollback the entire _apply() operation.
//	*/
func _sessionRetryConstraints(tls *libc.TLS, db uintptr, bPatchset int32, zTab uintptr, pApply uintptr, __ccgo_fp_xConflict uintptr, pCtx uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var iSkip, iThis, iUpdate, rc int32
	var _ /* app at bp+32 */ TSessionBuffer
	var _ /* cons at bp+0 */ TSessionBuffer
	var _ /* cons at bp+12 */ TSessionBuffer
	var _ /* cons at bp+44 */ TSessionBuffer
	var _ /* pInsert at bp+28 */ uintptr
	var _ /* pUp at bp+24 */ uintptr
	_, _, _, _ = iSkip, iThis, iUpdate, rc
	rc = SQLITE_OK
	iUpdate = 0
	/* Step (1) */
	for (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints.FnBuf != 0 {
		**(**TSessionBuffer)(__ccgo_up(bp)) = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints
		libc.Xmemset(tls, pApply+48, 0, uint32(12))
		rc = _sessionApplyRetryBuffer(tls, bp, -int32(1), db, bPatchset, zTab, pApply, __ccgo_fp_xConflict, pCtx)
		Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp))).FaBuf)
		if rc != SQLITE_OK {
			break
		}
		/* If no progress has been made this round, break out of the loop. */
		if (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints.FnBuf >= (**(**TSessionBuffer)(__ccgo_up(bp))).FnBuf {
			break
		}
	}
	/* Step (2) */
	for rc == SQLITE_OK && (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints.FnBuf != 0 && !((*TSessionApplyCtx)(unsafe.Pointer(pApply)).FbNoUpdateLoop != 0) {
		**(**TSessionBuffer)(__ccgo_up(bp + 12)) = TSessionBuffer{}
		**(**uintptr)(__ccgo_up(bp + 24)) = uintptr(0)
		**(**uintptr)(__ccgo_up(bp + 28)) = uintptr(0)
		iSkip = 0
		rc = _sessionRetryIterInit(tls, pApply+48, bPatchset, zTab, pApply, bp+24)
		if rc == SQLITE_OK {
			iThis = -int32(1)
			for int32(SQLITE_ROW) == Xsqlite3changeset_next(tls, **(**uintptr)(__ccgo_up(bp + 24))) {
				if (*Tsqlite3_changeset_iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 24)))).Fop == int32(SQLITE_UPDATE) {
					iThis = iThis + 1
				}
				if iThis == iUpdate {
					break
				}
				iSkip = iSkip + 1
			}
			if iThis == iUpdate {
				rc = Xsqlite3_exec(tls, db, __ccgo_ts+37694, uintptr(0), uintptr(0), uintptr(0))
				if rc == SQLITE_OK {
					rc = _sessionUpdateToDeleteInsert(tls, db, zTab, pApply, **(**uintptr)(__ccgo_up(bp + 24)), bp+28)
				}
			}
			Xsqlite3changeset_finalize(tls, **(**uintptr)(__ccgo_up(bp + 24)))
			if iThis != iUpdate {
				break
			}
		}
		if rc == SQLITE_OK {
			**(**TSessionBuffer)(__ccgo_up(bp + 12)) = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints
			for rc == SQLITE_OK && (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints.FnBuf > 0 {
				**(**TSessionBuffer)(__ccgo_up(bp + 32)) = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints
				libc.Xmemset(tls, pApply+48, 0, uint32(12))
				rc = _sessionApplyRetryBuffer(tls, bp+32, iSkip, db, bPatchset, zTab, pApply, __ccgo_fp_xConflict, pCtx)
				if (**(**TSessionBuffer)(__ccgo_up(bp + 32))).FaBuf != (**(**TSessionBuffer)(__ccgo_up(bp + 12))).FaBuf {
					Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 32))).FaBuf)
				}
				if (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints.FnBuf >= (**(**TSessionBuffer)(__ccgo_up(bp + 32))).FnBuf {
					break
				}
				iSkip = -int32(1)
			}
		}
		iUpdate = iUpdate + 1
		if rc == SQLITE_OK {
			Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 28)))
			rc = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 28)))
			if rc == int32(SQLITE_CONSTRAINT) {
				rc = Xsqlite3_exec(tls, db, __ccgo_ts+37714, uintptr(0), uintptr(0), uintptr(0))
				Xsqlite3_free(tls, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints.FaBuf)
				(*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints = **(**TSessionBuffer)(__ccgo_up(bp + 12))
				libc.Xmemset(tls, bp+12, 0, uint32(12))
			} else {
				if rc == SQLITE_OK {
					iUpdate = 0
				}
			}
			if rc == SQLITE_OK {
				rc = Xsqlite3_exec(tls, db, __ccgo_ts+37736, uintptr(0), uintptr(0), uintptr(0))
			}
		} else {
			Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 28)))
		}
		Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 12))).FaBuf)
	}
	/* Step (3) */
	if rc == SQLITE_OK && (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints.FnBuf != 0 {
		**(**TSessionBuffer)(__ccgo_up(bp + 44)) = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints
		libc.Xmemset(tls, pApply+48, 0, uint32(12))
		(*TSessionApplyCtx)(unsafe.Pointer(pApply)).FbDeferConstraints = 0
		rc = _sessionApplyRetryBuffer(tls, bp+44, -int32(1), db, bPatchset, zTab, pApply, __ccgo_fp_xConflict, pCtx)
		Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 44))).FaBuf)
	}
	return rc
}

// C documentation
//
//	/*
//	** Create an iterator to iterate through the retry buffer pRetry.
//	*/
func _sessionRetryIterInit(tls *libc.TLS, pRetry uintptr, bPatchset int32, zTab uintptr, pApply uintptr, ppIter uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var nByte Tsize_t
	var _ /* pRet at bp+0 */ uintptr
	var _ /* rc at bp+4 */ int32
	_ = nByte
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 4)) = SQLITE_OK
	**(**int32)(__ccgo_up(bp + 4)) = _sessionChangesetStart(tls, bp, uintptr(0), uintptr(0), (*TSessionBuffer)(unsafe.Pointer(pRetry)).FnBuf, (*TSessionBuffer)(unsafe.Pointer(pRetry)).FaBuf, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FbInvertConstraints, int32(1))
	if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
		nByte = uint32(int32(2)*(*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol) * uint32(4)
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FbPatchset = bPatchset
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FzTab = zTab
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnCol = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FabPK = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FabPK
		_sessionBufferGrow(tls, **(**uintptr)(__ccgo_up(bp))+44, int64(nByte), bp+4)
		(*Tsqlite3_changeset_iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FapValue = (*Tsqlite3_changeset_iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Ftblhdr.FaBuf
		if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
			libc.Xmemset(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FapValue, 0, nByte)
		} else {
			Xsqlite3changeset_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		}
	}
	**(**uintptr)(__ccgo_up(ppIter)) = **(**uintptr)(__ccgo_up(bp))
	return **(**int32)(__ccgo_up(bp + 4))
}

// C documentation
//
//	/*
//	** SQL statement pSelect is as generated by the sessionSelectRow() function.
//	** This function binds the primary key values from the change that changeset
//	** iterator pIter points to to the SELECT and attempts to seek to the table
//	** entry. If a row is found, the SELECT statement left pointing at the row
//	** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
//	** has occured, the statement is reset and SQLITE_OK is returned. If an
//	** error occurs, the statement is reset and an SQLite error code is returned.
//	**
//	** If this function returns SQLITE_ROW, the caller must eventually reset()
//	** statement pSelect. If any other value is returned, the statement does
//	** not require a reset().
//	**
//	** If the iterator currently points to an INSERT record, bind values from the
//	** new.* record to the SELECT statement. Or, if it points to a DELETE or
//	** UPDATE, bind values from the old.* record.
//	*/
func _sessionSeekToRow(tls *libc.TLS, pIter uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var ii, rc int32
	var pSelect, v1 uintptr
	var _ /* nCol at bp+0 */ int32
	var _ /* op at bp+4 */ int32
	var _ /* pVal at bp+12 */ uintptr
	var _ /* zDummy at bp+8 */ uintptr
	_, _, _, _ = ii, pSelect, rc, v1
	pSelect = (*TSessionApplyCtx)(unsafe.Pointer(p)).FpSelect /* Unused */
	Xsqlite3_clear_bindings(tls, pSelect)
	Xsqlite3changeset_op(tls, pIter, bp+8, bp, bp+4, uintptr(0))
	if **(**int32)(__ccgo_up(bp + 4)) == int32(SQLITE_INSERT) {
		v1 = __ccgo_fp(Xsqlite3changeset_new)
	} else {
		v1 = __ccgo_fp(Xsqlite3changeset_old)
	}
	rc = _sessionBindRow(tls, pIter, v1, **(**int32)(__ccgo_up(bp)), (*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK, pSelect)
	if **(**int32)(__ccgo_up(bp + 4)) != int32(SQLITE_DELETE) && (*TSessionApplyCtx)(unsafe.Pointer(p)).FbIgnoreNoop != 0 {
		ii = 0
		for {
			if !(rc == SQLITE_OK && ii < **(**int32)(__ccgo_up(bp))) {
				break
			}
			if int32(**(**Tu8)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(ii)))) == 0 {
				**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
				Xsqlite3changeset_new(tls, pIter, ii, bp+12)
				Xsqlite3_bind_int(tls, pSelect, ii+int32(1)+**(**int32)(__ccgo_up(bp)), libc.BoolInt32(**(**uintptr)(__ccgo_up(bp + 12)) == uintptr(0)))
				if **(**uintptr)(__ccgo_up(bp + 12)) != 0 {
					rc = _sessionBindValue(tls, pSelect, ii+int32(1), **(**uintptr)(__ccgo_up(bp + 12)))
				}
			}
			goto _2
		_2:
			;
			ii = ii + 1
		}
	}
	if rc == SQLITE_OK {
		rc = Xsqlite3_step(tls, pSelect)
		if rc != int32(SQLITE_ROW) {
			rc = Xsqlite3_reset(tls, pSelect)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Bind the PRIMARY KEY values from the change passed in argument pChange
//	** to the SELECT statement passed as the first argument. The SELECT statement
//	** is as prepared by function sessionSelectStmt().
//	**
//	** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
//	** error code (e.g. SQLITE_NOMEM) otherwise.
//	*/
func _sessionSelectBind(tls *libc.TLS, pSelect uintptr, nCol int32, abPK uintptr, pChange uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var a, v2 uintptr
	var eType, i, rc int32
	var iVal Ti64
	var _ /* iVal at bp+8 */ Ti64
	var _ /* n at bp+16 */ int32
	var _ /* n at bp+20 */ int32
	var _ /* rVal at bp+0 */ float64
	_, _, _, _, _, _ = a, eType, i, iVal, rc, v2
	rc = SQLITE_OK
	a = (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord
	i = 0
	for {
		if !(i < nCol && rc == SQLITE_OK) {
			break
		}
		v2 = a
		a = a + 1
		eType = int32(**(**Tu8)(__ccgo_up(v2)))
		switch eType {
		case 0:
			fallthrough
		case int32(SQLITE_NULL):
		case int32(SQLITE_INTEGER):
			if **(**Tu8)(__ccgo_up(abPK + uintptr(i))) != 0 {
				iVal = _sessionGetI64(tls, a)
				rc = Xsqlite3_bind_int64(tls, pSelect, i+int32(1), iVal)
			}
			a = a + uintptr(8)
		case int32(SQLITE_FLOAT):
			if **(**Tu8)(__ccgo_up(abPK + uintptr(i))) != 0 {
				**(**Ti64)(__ccgo_up(bp + 8)) = _sessionGetI64(tls, a)
				libc.Xmemcpy(tls, bp, bp+8, uint32(8))
				rc = Xsqlite3_bind_double(tls, pSelect, i+int32(1), **(**float64)(__ccgo_up(bp)))
			}
			a = a + uintptr(8)
		case int32(SQLITE_TEXT):
			a = a + uintptr(_sessionVarintGet(tls, a, bp+16))
			if **(**Tu8)(__ccgo_up(abPK + uintptr(i))) != 0 {
				rc = Xsqlite3_bind_text(tls, pSelect, i+int32(1), a, **(**int32)(__ccgo_up(bp + 16)), uintptr(-libc.Int32FromInt32(1)))
			}
			a = a + uintptr(**(**int32)(__ccgo_up(bp + 16)))
		default:
			a = a + uintptr(_sessionVarintGet(tls, a, bp+20))
			if **(**Tu8)(__ccgo_up(abPK + uintptr(i))) != 0 {
				rc = Xsqlite3_bind_blob(tls, pSelect, i+int32(1), a, **(**int32)(__ccgo_up(bp + 20)), uintptr(-libc.Int32FromInt32(1)))
			}
			a = a + uintptr(**(**int32)(__ccgo_up(bp + 20)))
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return rc
}

// C documentation
//
//	/*
//	** Formulate and prepare an SQL statement to query table zTab by primary
//	** key. Assuming the following table structure:
//	**
//	**     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
//	**
//	** The SELECT statement looks like this:
//	**
//	**     SELECT * FROM x WHERE a = ?1 AND c = ?3
//	**
//	** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
//	** pointing to the prepared version of the SQL statement.
//	*/
func _sessionSelectRow(tls *libc.TLS, db uintptr, zTab uintptr, p uintptr) (r int32) {
	/* TODO */
	return _sessionSelectStmt(tls, db, int32((*TSessionApplyCtx)(unsafe.Pointer(p)).FbIgnoreNoop), __ccgo_ts+8033, zTab, (*TSessionApplyCtx)(unsafe.Pointer(p)).FbRowid, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol, (*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol, (*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK, p+12, p+80)
}

// C documentation
//
//	/*
//	** Formulate and prepare a SELECT statement to retrieve a row from table
//	** zTab in database zDb based on its primary key. i.e.
//	**
//	**   SELECT *, <noop-test> FROM zDb.zTab WHERE (pk1, pk2,...) IS (?1, ?2,...)
//	**
//	** where <noop-test> is:
//	**
//	**   1 AND (?A OR ?1 IS <column>) AND ...
//	**
//	** for each non-pk <column>.
//	*/
func _sessionSelectStmt(tls *libc.TLS, db uintptr, bIgnoreNoop int32, zDb uintptr, zTab uintptr, bRowid int32, nCol int32, azCol uintptr, abPK uintptr, ppStmt uintptr, pzErrmsg uintptr) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var i int32
	var zSep, zSql, v2 uintptr
	var _ /* cols at bp+4 */ TSessionBuffer
	var _ /* nooptest at bp+16 */ TSessionBuffer
	var _ /* pkfield at bp+28 */ TSessionBuffer
	var _ /* pkvar at bp+40 */ TSessionBuffer
	var _ /* rc at bp+0 */ int32
	_, _, _, _ = i, zSep, zSql, v2
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	zSql = uintptr(0)
	zSep = __ccgo_ts + 1711
	**(**TSessionBuffer)(__ccgo_up(bp + 4)) = TSessionBuffer{}
	**(**TSessionBuffer)(__ccgo_up(bp + 16)) = TSessionBuffer{}
	**(**TSessionBuffer)(__ccgo_up(bp + 28)) = TSessionBuffer{}
	**(**TSessionBuffer)(__ccgo_up(bp + 40)) = TSessionBuffer{}
	_sessionAppendStr(tls, bp+16, __ccgo_ts+36923, bp)
	if 0 == Xsqlite3_stricmp(tls, __ccgo_ts+14050, zTab) {
		_sessionAppendStr(tls, bp+16, __ccgo_ts+36927, bp)
		_sessionAppendStr(tls, bp+28, __ccgo_ts+36951, bp)
		_sessionAppendStr(tls, bp+40, __ccgo_ts+36960, bp)
		_sessionAppendStr(tls, bp+4, __ccgo_ts+37005, bp)
	} else {
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			if (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FnBuf != 0 {
				_sessionAppendStr(tls, bp+4, __ccgo_ts+17436, bp)
			}
			_sessionAppendIdent(tls, bp+4, **(**uintptr)(__ccgo_up(azCol + uintptr(i)*4)), bp)
			if **(**Tu8)(__ccgo_up(abPK + uintptr(i))) != 0 {
				_sessionAppendStr(tls, bp+28, zSep, bp)
				_sessionAppendStr(tls, bp+40, zSep, bp)
				zSep = __ccgo_ts + 17436
				_sessionAppendIdent(tls, bp+28, **(**uintptr)(__ccgo_up(azCol + uintptr(i)*4)), bp)
				_sessionAppendPrintf(tls, bp+40, bp, __ccgo_ts+37019, libc.VaList(bp+64, i+int32(1)))
			} else {
				_sessionAppendPrintf(tls, bp+16, bp, __ccgo_ts+37023, libc.VaList(bp+64, i+int32(1)+nCol, i+int32(1), zTab, **(**uintptr)(__ccgo_up(azCol + uintptr(i)*4))))
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		if bIgnoreNoop != 0 {
			v2 = (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf
		} else {
			v2 = __ccgo_ts + 1711
		}
		zSql = Xsqlite3_mprintf(tls, __ccgo_ts+37050, libc.VaList(bp+64, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf, v2, zDb, zTab, (**(**TSessionBuffer)(__ccgo_up(bp + 28))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp + 40))).FaBuf))
		if zSql == uintptr(0) {
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp)) = _sessionPrepare(tls, db, ppStmt, pzErrmsg, zSql)
	}
	Xsqlite3_free(tls, zSql)
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf)
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 28))).FaBuf)
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 40))).FaBuf)
	Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf)
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** This function is used to serialize the contents of value pValue (see
//	** comment titled "RECORD FORMAT" above).
//	**
//	** If it is non-NULL, the serialized form of the value is written to
//	** buffer aBuf. *pnWrite is set to the number of bytes written before
//	** returning. Or, if aBuf is NULL, the only thing this function does is
//	** set *pnWrite.
//	**
//	** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
//	** within a call to sqlite3_value_text() (may fail if the db is utf-16))
//	** SQLITE_NOMEM is returned.
//	*/
func _sessionSerializeValue(tls *libc.TLS, aBuf uintptr, pValue uintptr, pnWrite uintptr) (r1 int32) {
	var eType, n, nByte, nVarint int32
	var i Tu64
	var r float64
	var z uintptr
	_, _, _, _, _, _, _ = eType, i, n, nByte, nVarint, r, z /* Size of serialized value in bytes */
	if pValue != 0 {                                        /* Value type (SQLITE_NULL, TEXT etc.) */
		eType = Xsqlite3_value_type(tls, pValue)
		if aBuf != 0 {
			**(**Tu8)(__ccgo_up(aBuf)) = uint8(eType)
		}
		switch eType {
		case int32(SQLITE_NULL):
			nByte = int32(1)
		case int32(SQLITE_INTEGER):
			fallthrough
		case int32(SQLITE_FLOAT):
			if aBuf != 0 {
				/* TODO: SQLite does something special to deal with mixed-endian
				 ** floating point values (e.g. ARM7). This code probably should
				 ** too.  */
				if eType == int32(SQLITE_INTEGER) {
					i = uint64(Xsqlite3_value_int64(tls, pValue))
					_sessionPutI64(tls, aBuf+1, int64(i))
				} else {
					r = Xsqlite3_value_double(tls, pValue)
					_sessionPutDouble(tls, aBuf+1, r)
				}
			}
			nByte = int32(9)
		default:
			if eType == int32(SQLITE_TEXT) {
				z = Xsqlite3_value_text(tls, pValue)
			} else {
				z = Xsqlite3_value_blob(tls, pValue)
			}
			n = Xsqlite3_value_bytes(tls, pValue)
			if z == uintptr(0) && (eType != int32(SQLITE_BLOB) || n > 0) {
				return int32(SQLITE_NOMEM)
			}
			nVarint = _sessionVarintLen(tls, n)
			if aBuf != 0 {
				_sessionVarintPut(tls, aBuf+1, n)
				if n > 0 {
					libc.Xmemcpy(tls, aBuf+uintptr(nVarint+int32(1)), z, uint32(n))
				}
			}
			nByte = int32(1) + nVarint + n
			break
		}
	} else {
		nByte = int32(1)
		if aBuf != 0 {
			**(**Tu8)(__ccgo_up(aBuf)) = uint8('\000')
		}
	}
	if pnWrite != 0 {
		**(**Tsqlite3_int64)(__ccgo_up(pnWrite)) += int64(nByte)
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Prepare statements for applying changes to the sqlite_stat1 table.
//	** These are similar to those created by sessionSelectRow(),
//	** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
//	** other tables.
//	*/
func _sessionStat1Sql(tls *libc.TLS, db uintptr, p uintptr) (r int32) {
	var rc int32
	_ = rc
	rc = _sessionSelectRow(tls, db, __ccgo_ts+14050, p)
	if rc == SQLITE_OK {
		rc = _sessionPrepare(tls, db, p+8, uintptr(0), __ccgo_ts+37307)
	}
	if rc == SQLITE_OK {
		rc = _sessionPrepare(tls, db, p+4, uintptr(0), __ccgo_ts+37420)
	}
	return rc
}

// C documentation
//
//	/*
//	** This function queries the database for the names of the columns of table
//	** zThis, in schema zDb.
//	**
//	** Otherwise, if they are not NULL, variable *pnCol is set to the number
//	** of columns in the database table and variable *pzTab is set to point to a
//	** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
//	** point to an array of pointers to column names. And *pabPK (again, if not
//	** NULL) is set to point to an array of booleans - true if the corresponding
//	** column is part of the primary key.
//	**
//	** For example, if the table is declared as:
//	**
//	**     CREATE TABLE tbl1(w, x DEFAULT 'abc', y, z, PRIMARY KEY(w, z));
//	**
//	** Then the five output variables are populated as follows:
//	**
//	**     *pnCol  = 4
//	**     *pzTab  = "tbl1"
//	**     *pazCol = {"w", "x", "y", "z"}
//	**     *pazDflt = {NULL, 'abc', NULL, NULL}
//	**     *pabPK  = {1, 0, 0, 1}
//	**
//	** All returned buffers are part of the same single allocation, which must
//	** be freed using sqlite3_free() by the caller
//	*/
func _sessionTableInfo(tls *libc.TLS, pSession uintptr, db uintptr, zDb uintptr, zThis uintptr, pnCol uintptr, pnTotalCol uintptr, pzTab uintptr, pazCol uintptr, pazDflt uintptr, paiIdx uintptr, pabPK uintptr, pbRowid uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var abPK, aiIdx, azCol, azDflt, pAlloc, zDflt, zName, zPragma uintptr
	var bRowid, i, nDbCol, nDflt, nName1, nThis, rc int32
	var nByte Tsqlite3_int64
	var nName Tsize_t
	var _ /* pStmt at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = abPK, aiIdx, azCol, azDflt, bRowid, i, nByte, nDbCol, nDflt, nName, nName1, nThis, pAlloc, rc, zDflt, zName, zPragma
	nDbCol = 0
	pAlloc = uintptr(0)
	azCol = uintptr(0)
	azDflt = uintptr(0)
	abPK = uintptr(0)
	aiIdx = uintptr(0)
	bRowid = 0 /* Set to true to use rowid as PK */
	**(**uintptr)(__ccgo_up(pazCol)) = uintptr(0)
	**(**uintptr)(__ccgo_up(pabPK)) = uintptr(0)
	**(**int32)(__ccgo_up(pnCol)) = 0
	if pnTotalCol != 0 {
		**(**int32)(__ccgo_up(pnTotalCol)) = 0
	}
	if paiIdx != 0 {
		**(**uintptr)(__ccgo_up(paiIdx)) = uintptr(0)
	}
	if pzTab != 0 {
		**(**uintptr)(__ccgo_up(pzTab)) = uintptr(0)
	}
	if pazDflt != 0 {
		**(**uintptr)(__ccgo_up(pazDflt)) = uintptr(0)
	}
	nThis = _sqlite3Strlen30(tls, zThis)
	if nThis == int32(12) && 0 == Xsqlite3_stricmp(tls, __ccgo_ts+14050, zThis) {
		rc = Xsqlite3_table_column_metadata(tls, db, zDb, zThis, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0))
		if rc == SQLITE_OK {
			/* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
			zPragma = Xsqlite3_mprintf(tls, __ccgo_ts+36431, 0)
		} else {
			if rc == int32(SQLITE_ERROR) {
				zPragma = Xsqlite3_mprintf(tls, __ccgo_ts+1711, 0)
			} else {
				return rc
			}
		}
	} else {
		zPragma = Xsqlite3_mprintf(tls, __ccgo_ts+36561, libc.VaList(bp+16, zDb, zThis))
	}
	if !(zPragma != 0) {
		return int32(SQLITE_NOMEM)
	}
	rc = Xsqlite3_prepare_v2(tls, db, zPragma, -int32(1), bp, uintptr(0))
	Xsqlite3_free(tls, zPragma)
	if rc != SQLITE_OK {
		return rc
	}
	nByte = int64(nThis + int32(1))
	bRowid = libc.BoolInt32(pbRowid != uintptr(0))
	for int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
		nByte = nByte + int64(Xsqlite3_column_bytes(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))) /* name */
		nByte = nByte + int64(Xsqlite3_column_bytes(tls, **(**uintptr)(__ccgo_up(bp)), int32(4))) /* dflt_value */
		if Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(6)) == 0 {                /* !hidden */
			nDbCol = nDbCol + 1
		}
		if Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(5)) != 0 {
			bRowid = 0
		} /* pk */
	}
	if nDbCol == 0 {
		bRowid = 0
	}
	nDbCol = nDbCol + bRowid
	nByte = nByte + int64(libc.Xstrlen(tls, __ccgo_ts+32579))
	rc = Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp)))
	if rc == SQLITE_OK {
		nByte = nByte + int64(uint32(nDbCol)*(libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(4)+libc.Uint32FromInt64(1)+libc.Uint32FromInt32(1)+libc.Uint32FromInt32(1)))
		pAlloc = _sessionMalloc64(tls, pSession, nByte)
		if pAlloc == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		} else {
			libc.Xmemset(tls, pAlloc, 0, uint32(nByte))
		}
	}
	if rc == SQLITE_OK {
		azCol = pAlloc
		azDflt = azCol + uintptr(nDbCol)*4
		aiIdx = azDflt + uintptr(nDbCol)*4
		abPK = aiIdx + uintptr(nDbCol)*4
		pAlloc = abPK + uintptr(nDbCol)
		if pzTab != 0 {
			libc.Xmemcpy(tls, pAlloc, zThis, uint32(nThis+int32(1)))
			**(**uintptr)(__ccgo_up(pzTab)) = pAlloc
			pAlloc = pAlloc + uintptr(nThis+int32(1))
		}
		i = 0
		if bRowid != 0 {
			nName = libc.Xstrlen(tls, __ccgo_ts+32579)
			libc.Xmemcpy(tls, pAlloc, __ccgo_ts+32579, nName+uint32(1))
			**(**uintptr)(__ccgo_up(azCol + uintptr(i)*4)) = pAlloc
			pAlloc = pAlloc + uintptr(nName+uint32(1))
			**(**Tu8)(__ccgo_up(abPK + uintptr(i))) = uint8(1)
			**(**int32)(__ccgo_up(aiIdx + uintptr(i)*4)) = -int32(1)
			i = i + 1
		}
		for int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
			if Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(6)) == 0 { /* !hidden */
				nName1 = Xsqlite3_column_bytes(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
				nDflt = Xsqlite3_column_bytes(tls, **(**uintptr)(__ccgo_up(bp)), int32(4))
				zName = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
				zDflt = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), int32(4))
				if zName == uintptr(0) {
					break
				}
				libc.Xmemcpy(tls, pAlloc, zName, uint32(nName1+int32(1)))
				**(**uintptr)(__ccgo_up(azCol + uintptr(i)*4)) = pAlloc
				pAlloc = pAlloc + uintptr(nName1+int32(1))
				if zDflt != 0 {
					libc.Xmemcpy(tls, pAlloc, zDflt, uint32(nDflt+int32(1)))
					**(**uintptr)(__ccgo_up(azDflt + uintptr(i)*4)) = pAlloc
					pAlloc = pAlloc + uintptr(nDflt+int32(1))
				} else {
					**(**uintptr)(__ccgo_up(azDflt + uintptr(i)*4)) = uintptr(0)
				}
				**(**Tu8)(__ccgo_up(abPK + uintptr(i))) = uint8(Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), int32(5)))
				**(**int32)(__ccgo_up(aiIdx + uintptr(i)*4)) = Xsqlite3_column_int(tls, **(**uintptr)(__ccgo_up(bp)), 0)
				i = i + 1
			}
			if pnTotalCol != 0 {
				**(**int32)(__ccgo_up(pnTotalCol)) = **(**int32)(__ccgo_up(pnTotalCol)) + 1
			}
		}
		rc = Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	/* If successful, populate the output variables. Otherwise, zero them and
	 ** free any allocation made. An error code will be returned in this case.
	 */
	if rc == SQLITE_OK {
		**(**uintptr)(__ccgo_up(pazCol)) = azCol
		if pazDflt != 0 {
			**(**uintptr)(__ccgo_up(pazDflt)) = azDflt
		}
		**(**uintptr)(__ccgo_up(pabPK)) = abPK
		**(**int32)(__ccgo_up(pnCol)) = nDbCol
		if paiIdx != 0 {
			**(**uintptr)(__ccgo_up(paiIdx)) = aiIdx
		}
	} else {
		_sessionFree(tls, pSession, azCol)
	}
	if pbRowid != 0 {
		**(**int32)(__ccgo_up(pbRowid)) = bRowid
	}
	Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	return rc
}

// C documentation
//
//	/*
//	** Table pTab has one or more existing change-records with old.* records
//	** with fewer than pTab->nCol columns. This function updates all such
//	** change-records with the default values for the missing columns.
//	*/
func _sessionUpdateChanges(tls *libc.TLS, pSession uintptr, pTab uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var ii int32
	var pp uintptr
	var _ /* pStmt at bp+0 */ uintptr
	var _ /* rc at bp+4 */ int32
	_, _ = ii, pp
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 4)) = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc
	**(**int32)(__ccgo_up(bp + 4)) = _sessionPrepareDfltStmt(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, pTab, bp)
	if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
		ii = 0
		pp = uintptr(0)
		ii = 0
		for {
			if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange) {
				break
			}
			pp = (*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(ii)*4
			for {
				if !(**(**uintptr)(__ccgo_up(pp)) != 0) {
					break
				}
				if int32((*TSessionChange)(unsafe.Pointer(**(**uintptr)(__ccgo_up(pp)))).FnRecordField) != (*TSessionTable)(unsafe.Pointer(pTab)).FnCol {
					_sessionUpdateOneChange(tls, pSession, bp+4, pp, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, **(**uintptr)(__ccgo_up(bp)))
				}
				goto _2
			_2:
				;
				pp = **(**uintptr)(__ccgo_up(pp)) + 16
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
	}
	_sessionFinalizeStmt(tls, **(**uintptr)(__ccgo_up(bp)), bp+4)
	(*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = **(**int32)(__ccgo_up(bp + 4))
	return (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc
}

// C documentation
//
//	/*
//	** Find a prepared UPDATE statement suitable for the UPDATE step currently
//	** being visited by the iterator. The UPDATE is of the form:
//	**
//	**   UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ?
//	*/
func _sessionUpdateFind(tls *libc.TLS, pIter uintptr, p uintptr, bPatchset int32, ppStmt uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bStat1, ii, nByte, nCol, nU32, nUp int32
	var pUp, pp, zSep, zSql uintptr
	var _ /* buf at bp+4 */ TSessionBuffer
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _ = bStat1, ii, nByte, nCol, nU32, nUp, pUp, pp, zSep, zSql
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	pUp = uintptr(0)
	nCol = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol
	nU32 = ((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol + int32(33)) / int32(32)
	if (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask == uintptr(0) {
		(*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask = Xsqlite3_malloc(tls, int32(uint32(nU32)*uint32(4)))
		if (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask == uintptr(0) {
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		libc.Xmemset(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, 0, uint32(nU32)*uint32(4))
		**(**int32)(__ccgo_up(bp)) = int32(SQLITE_CORRUPT)
		ii = 0
		for {
			if !(ii < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) {
				break
			}
			if **(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol+ii)*4)) != 0 {
				**(**Tu32)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask + uintptr(ii/int32(32))*4)) |= uint32(libc.Int32FromInt32(1) << (ii % libc.Int32FromInt32(32)))
				**(**int32)(__ccgo_up(bp)) = SQLITE_OK
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		if bPatchset != 0 {
			**(**Tu32)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask + uintptr(nCol/int32(32))*4)) |= uint32(libc.Int32FromInt32(1) << (nCol % libc.Int32FromInt32(32)))
		}
		if (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp != 0 {
			nUp = 0
			pp = p + 32
			for int32(1) != 0 {
				nUp = nUp + 1
				if 0 == libc.Xmemcmp(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, (*TSessionUpdate)(unsafe.Pointer(**(**uintptr)(__ccgo_up(pp)))).FaMask, uint32(nU32)*uint32(4)) {
					pUp = **(**uintptr)(__ccgo_up(pp))
					**(**uintptr)(__ccgo_up(pp)) = (*TSessionUpdate)(unsafe.Pointer(pUp)).FpNext
					(*TSessionUpdate)(unsafe.Pointer(pUp)).FpNext = (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp
					(*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp = pUp
					break
				}
				if (*TSessionUpdate)(unsafe.Pointer(**(**uintptr)(__ccgo_up(pp)))).FpNext != 0 {
					pp = **(**uintptr)(__ccgo_up(pp)) + 8
				} else {
					if nUp >= int32(SESSION_UPDATE_CACHE_SZ) {
						Xsqlite3_finalize(tls, (*TSessionUpdate)(unsafe.Pointer(**(**uintptr)(__ccgo_up(pp)))).FpStmt)
						Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(pp)))
						**(**uintptr)(__ccgo_up(pp)) = uintptr(0)
					}
					break
				}
			}
		}
		if pUp == uintptr(0) {
			nByte = int32(uint32(12) * uint32(nU32) * uint32(4))
			bStat1 = libc.BoolInt32(Xsqlite3_stricmp(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab, __ccgo_ts+14050) == 0)
			pUp = Xsqlite3_malloc(tls, nByte)
			if pUp == uintptr(0) {
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
			} else {
				zSep = __ccgo_ts + 1711
				libc.Xmemset(tls, bp+4, 0, uint32(12))
				(*TSessionUpdate)(unsafe.Pointer(pUp)).FaMask = pUp + 1*12
				libc.Xmemcpy(tls, (*TSessionUpdate)(unsafe.Pointer(pUp)).FaMask, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, uint32(nU32)*uint32(4))
				_sessionAppendStr(tls, bp+4, __ccgo_ts+37130, bp)
				_sessionAppendIdent(tls, bp+4, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab, bp)
				_sessionAppendStr(tls, bp+4, __ccgo_ts+37143, bp)
				/* Create the assignments part of the UPDATE */
				ii = 0
				for {
					if !(ii < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) {
						break
					}
					if int32(**(**Tu8)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(ii)))) == 0 && **(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol+ii)*4)) != 0 {
						_sessionAppendStr(tls, bp+4, zSep, bp)
						_sessionAppendIdent(tls, bp+4, **(**uintptr)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(ii)*4)), bp)
						_sessionAppendStr(tls, bp+4, __ccgo_ts+37149, bp)
						_sessionAppendInteger(tls, bp+4, ii*int32(2)+int32(1), bp)
						zSep = __ccgo_ts + 17436
					}
					goto _2
				_2:
					;
					ii = ii + 1
				}
				/* Create the WHERE clause part of the UPDATE */
				zSep = __ccgo_ts + 1711
				_sessionAppendStr(tls, bp+4, __ccgo_ts+37154, bp)
				ii = 0
				for {
					if !(ii < (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) {
						break
					}
					if **(**Tu8)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(ii))) != 0 || bPatchset == 0 && **(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr(ii)*4)) != 0 {
						_sessionAppendStr(tls, bp+4, zSep, bp)
						if bStat1 != 0 && ii == int32(1) {
							_sessionAppendStr(tls, bp+4, __ccgo_ts+37162, bp)
						} else {
							_sessionAppendIdent(tls, bp+4, **(**uintptr)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(ii)*4)), bp)
							_sessionAppendStr(tls, bp+4, __ccgo_ts+37237, bp)
							_sessionAppendInteger(tls, bp+4, ii*int32(2)+int32(2), bp)
						}
						zSep = __ccgo_ts + 24859
					}
					goto _3
				_3:
					;
					ii = ii + 1
				}
				if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
					zSql = (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf
					**(**int32)(__ccgo_up(bp)) = Xsqlite3_prepare_v2(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).Fdb, zSql, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FnBuf, pUp, uintptr(0))
				}
				if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
					Xsqlite3_free(tls, pUp)
					pUp = uintptr(0)
				} else {
					(*TSessionUpdate)(unsafe.Pointer(pUp)).FpNext = (*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp
					(*TSessionApplyCtx)(unsafe.Pointer(p)).FpUp = pUp
				}
				Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 4))).FaBuf)
			}
		}
	}
	if pUp != 0 {
		**(**uintptr)(__ccgo_up(ppStmt)) = (*TSessionUpdate)(unsafe.Pointer(pUp)).FpStmt
	} else {
		**(**uintptr)(__ccgo_up(ppStmt)) = uintptr(0)
	}
	return **(**int32)(__ccgo_up(bp))
}

func _sessionUpdateMaxSize(tls *libc.TLS, op int32, pSession uintptr, pTab uintptr, pC uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var bChanged, eType, iIdx, ii, ii1, nIncr, nOld int32
	var pCsr, v3 uintptr
	var _ /* dVal at bp+24 */ float64
	var _ /* iVal at bp+16 */ Tsqlite3_int64
	var _ /* nByte at bp+32 */ int32
	var _ /* nNew at bp+0 */ Ti64
	var _ /* p at bp+12 */ uintptr
	var _ /* p at bp+8 */ uintptr
	_, _, _, _, _, _, _, _, _ = bChanged, eType, iIdx, ii, ii1, nIncr, nOld, pCsr, v3
	**(**Ti64)(__ccgo_up(bp)) = int64(2)
	if int32((*TSessionChange)(unsafe.Pointer(pC)).Fop) == int32(SQLITE_INSERT) {
		if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 {
			**(**Ti64)(__ccgo_up(bp)) = **(**Ti64)(__ccgo_up(bp)) + int64(9)
		}
		if op != int32(SQLITE_DELETE) {
			ii = 0
			for {
				if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
					break
				}
				**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
				(*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, **(**int32)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FaiIdx + uintptr(ii)*4)), bp+8)
				_sessionSerializeValue(tls, uintptr(0), **(**uintptr)(__ccgo_up(bp + 8)), bp)
				goto _1
			_1:
				;
				ii = ii + 1
			}
		}
	} else {
		if op == int32(SQLITE_DELETE) {
			**(**Ti64)(__ccgo_up(bp)) = **(**Ti64)(__ccgo_up(bp)) + int64((*TSessionChange)(unsafe.Pointer(pC)).FnRecord)
			if Xsqlite3_preupdate_blobwrite(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb) >= 0 {
				**(**Ti64)(__ccgo_up(bp)) = **(**Ti64)(__ccgo_up(bp)) + int64((*TSessionChange)(unsafe.Pointer(pC)).FnRecord)
			}
		} else {
			pCsr = (*TSessionChange)(unsafe.Pointer(pC)).FaRecord
			if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 {
				**(**Ti64)(__ccgo_up(bp)) = **(**Ti64)(__ccgo_up(bp)) + int64(libc.Int32FromInt32(9)+libc.Int32FromInt32(1))
				pCsr = pCsr + uintptr(9)
			}
			ii1 = (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid
			for {
				if !(ii1 < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) {
					break
				}
				bChanged = int32(1)
				nOld = 0
				iIdx = **(**int32)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FaiIdx + uintptr(ii1)*4))
				**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
				(*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FxNew})))(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fhook.FpCtx, iIdx, bp+12)
				if **(**uintptr)(__ccgo_up(bp + 12)) == uintptr(0) {
					return int32(SQLITE_NOMEM)
				}
				v3 = pCsr
				pCsr = pCsr + 1
				eType = int32(**(**Tu8)(__ccgo_up(v3)))
				switch eType {
				case int32(SQLITE_NULL):
					bChanged = libc.BoolInt32(Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(bp + 12))) != int32(SQLITE_NULL))
				case int32(SQLITE_FLOAT):
					fallthrough
				case int32(SQLITE_INTEGER):
					if eType == Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(bp + 12))) {
						**(**Tsqlite3_int64)(__ccgo_up(bp + 16)) = _sessionGetI64(tls, pCsr)
						if eType == int32(SQLITE_INTEGER) {
							bChanged = libc.BoolInt32(**(**Tsqlite3_int64)(__ccgo_up(bp + 16)) != Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(bp + 12))))
						} else {
							libc.Xmemcpy(tls, bp+24, bp+16, uint32(8))
							bChanged = libc.BoolInt32(**(**float64)(__ccgo_up(bp + 24)) != Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(bp + 12))))
						}
					}
					nOld = int32(8)
					pCsr = pCsr + uintptr(8)
				default:
					nOld = _sessionVarintGet(tls, pCsr, bp+32)
					pCsr = pCsr + uintptr(nOld)
					nOld = nOld + **(**int32)(__ccgo_up(bp + 32))
					if eType == Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(bp + 12))) && **(**int32)(__ccgo_up(bp + 32)) == Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(bp + 12))) && (**(**int32)(__ccgo_up(bp + 32)) == 0 || 0 == libc.Xmemcmp(tls, pCsr, Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(bp + 12))), uint32(**(**int32)(__ccgo_up(bp + 32))))) {
						bChanged = 0
					}
					pCsr = pCsr + uintptr(**(**int32)(__ccgo_up(bp + 32)))
					break
				}
				if bChanged != 0 && **(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(ii1))) != 0 {
					**(**Ti64)(__ccgo_up(bp)) = int64((*TSessionChange)(unsafe.Pointer(pC)).FnRecord + int32(2))
					break
				}
				if bChanged != 0 {
					**(**Ti64)(__ccgo_up(bp)) = **(**Ti64)(__ccgo_up(bp)) + int64(int32(1)+nOld)
					_sessionSerializeValue(tls, uintptr(0), **(**uintptr)(__ccgo_up(bp + 12)), bp)
				} else {
					if **(**Tu8)(__ccgo_up((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(ii1))) != 0 {
						**(**Ti64)(__ccgo_up(bp)) = **(**Ti64)(__ccgo_up(bp)) + int64(int32(2)+nOld)
					} else {
						**(**Ti64)(__ccgo_up(bp)) = **(**Ti64)(__ccgo_up(bp)) + int64(2)
					}
				}
				goto _2
			_2:
				;
				ii1 = ii1 + 1
			}
		}
	}
	if **(**Ti64)(__ccgo_up(bp)) > int64((*TSessionChange)(unsafe.Pointer(pC)).FnMaxSize) {
		nIncr = int32(**(**Ti64)(__ccgo_up(bp)) - int64((*TSessionChange)(unsafe.Pointer(pC)).FnMaxSize))
		(*TSessionChange)(unsafe.Pointer(pC)).FnMaxSize = int32(**(**Ti64)(__ccgo_up(bp)))
		**(**Ti64)(__ccgo_up(pSession + 48)) += int64(nIncr)
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Session-change object (*pp) contains an old.* record with fewer than
//	** nCol fields. This function updates it with the default values for
//	** the missing fields.
//	*/
func _sessionUpdateOneChange(tls *libc.TLS, pSession uintptr, pRc uintptr, pp uintptr, nCol int32, pDflt uintptr) {
	var eType, iField, n, n1, n2, nByte, nIncr, v1 int32
	var iVal Ti64
	var pNew, pOld, z, z1, v2 uintptr
	var rVal float64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = eType, iField, iVal, n, n1, n2, nByte, nIncr, pNew, pOld, rVal, z, z1, v1, v2
	pOld = **(**uintptr)(__ccgo_up(pp))
	for int32((*TSessionChange)(unsafe.Pointer(pOld)).FnRecordField) < nCol {
		pNew = uintptr(0)
		nByte = 0
		nIncr = 0
		iField = int32((*TSessionChange)(unsafe.Pointer(pOld)).FnRecordField)
		eType = Xsqlite3_column_type(tls, pDflt, iField)
		switch eType {
		case int32(SQLITE_NULL):
			nIncr = int32(1)
		case int32(SQLITE_INTEGER):
			fallthrough
		case int32(SQLITE_FLOAT):
			nIncr = int32(9)
		default:
			n = Xsqlite3_column_bytes(tls, pDflt, iField)
			nIncr = int32(1) + _sessionVarintLen(tls, n) + n
			break
		}
		nByte = int32(uint32(nIncr) + (uint32(20) + uint32((*TSessionChange)(unsafe.Pointer(pOld)).FnRecord)))
		pNew = _sessionMalloc64(tls, pSession, int64(nByte))
		if pNew == uintptr(0) {
			**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
			return
		} else {
			libc.Xmemcpy(tls, pNew, pOld, uint32(20))
			(*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = pNew + 1*20
			libc.Xmemcpy(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord, (*TSessionChange)(unsafe.Pointer(pOld)).FaRecord, uint32((*TSessionChange)(unsafe.Pointer(pOld)).FnRecord))
			v2 = pNew + 8
			v1 = *(*int32)(unsafe.Pointer(v2))
			*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
			**(**Tu8)(__ccgo_up((*TSessionChange)(unsafe.Pointer(pNew)).FaRecord + uintptr(v1))) = uint8(eType)
			switch eType {
			case int32(SQLITE_INTEGER):
				iVal = Xsqlite3_column_int64(tls, pDflt, iField)
				_sessionPutI64(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), iVal)
				**(**int32)(__ccgo_up(pNew + 8)) += int32(8)
			case int32(SQLITE_FLOAT):
				rVal = Xsqlite3_column_double(tls, pDflt, iField)
				_sessionPutDouble(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), rVal)
				**(**int32)(__ccgo_up(pNew + 8)) += int32(8)
			case int32(SQLITE_TEXT):
				n1 = Xsqlite3_column_bytes(tls, pDflt, iField)
				z = Xsqlite3_column_text(tls, pDflt, iField)
				**(**int32)(__ccgo_up(pNew + 8)) += _sessionVarintPut(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), n1)
				libc.Xmemcpy(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), z, uint32(n1))
				**(**int32)(__ccgo_up(pNew + 8)) += n1
			case int32(SQLITE_BLOB):
				n2 = Xsqlite3_column_bytes(tls, pDflt, iField)
				z1 = Xsqlite3_column_blob(tls, pDflt, iField)
				**(**int32)(__ccgo_up(pNew + 8)) += _sessionVarintPut(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), n2)
				libc.Xmemcpy(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), z1, uint32(n2))
				**(**int32)(__ccgo_up(pNew + 8)) += n2
			default:
				break
			}
			_sessionFree(tls, pSession, pOld)
			v2 = pNew
			pOld = v2
			**(**uintptr)(__ccgo_up(pp)) = v2
			(*TSessionChange)(unsafe.Pointer(pNew)).FnRecordField = (*TSessionChange)(unsafe.Pointer(pNew)).FnRecordField + 1
			**(**int32)(__ccgo_up(pNew + 4)) += nIncr
			if pSession != 0 {
				**(**Ti64)(__ccgo_up(pSession + 48)) += int64(nIncr)
			}
		}
	}
}

// C documentation
//
//	/*
//	** Iterator pUp points to an UPDATE change. This function deletes the
//	** affected row from the database and creates an INSERT statement that
//	** may be used to reinsert the row as it is after the UPDATE change
//	** has been applied.
//	**
//	** If successful, SQLITE_OK is returned and output variable (*ppInsert)
//	** is left pointing to a prepared INSERT statement. It is the responsibility
//	** of the caller to eventually free this statement using sqlite3_finalize().
//	** Or, if an error occurs, an SQLite error code is returned and (*ppInsert)
//	** set to NULL. pApply->zErr may be set to an error message in this case.
//	*/
func _sessionUpdateToDeleteInsert(tls *libc.TLS, db uintptr, zTab uintptr, pApply uintptr, pUp uintptr, ppInsert uintptr) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var iCol, ii int32
	var pVal, zComma, zComma2, zInsert, zSelect uintptr
	var _ /* bWR at bp+12 */ int32
	var _ /* cols at bp+16 */ TSessionBuffer
	var _ /* insbind at bp+28 */ TSessionBuffer
	var _ /* pRet at bp+0 */ uintptr
	var _ /* pSelect at bp+4 */ uintptr
	var _ /* pkcols at bp+40 */ TSessionBuffer
	var _ /* rc at bp+8 */ int32
	var _ /* selbind at bp+52 */ TSessionBuffer
	_, _, _, _, _, _, _ = iCol, ii, pVal, zComma, zComma2, zInsert, zSelect
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)     /* The INSERT statement */
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* SELECT to read current values of row */
	**(**int32)(__ccgo_up(bp + 8)) = SQLITE_OK
	**(**int32)(__ccgo_up(bp + 12)) = 0
	**(**int32)(__ccgo_up(bp + 8)) = _sessionTableIsWithoutRowid(tls, db, zTab, bp+12)
	if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK {
		zSelect = uintptr(0)
		zInsert = uintptr(0)
		**(**TSessionBuffer)(__ccgo_up(bp + 16)) = TSessionBuffer{}
		**(**TSessionBuffer)(__ccgo_up(bp + 28)) = TSessionBuffer{}
		**(**TSessionBuffer)(__ccgo_up(bp + 40)) = TSessionBuffer{}
		**(**TSessionBuffer)(__ccgo_up(bp + 52)) = TSessionBuffer{}
		zComma = __ccgo_ts + 1711
		zComma2 = __ccgo_ts + 1711
		ii = 0
		for {
			if !(ii < (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol) {
				break
			}
			_sessionAppendStr(tls, bp+16, zComma, bp+8)
			_sessionAppendIdent(tls, bp+16, **(**uintptr)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(pApply)).FazCol + uintptr(ii)*4)), bp+8)
			_sessionAppendStr(tls, bp+28, zComma, bp+8)
			_sessionAppendStr(tls, bp+28, __ccgo_ts+6476, bp+8)
			zComma = __ccgo_ts + 17436
			if **(**Tu8)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(pApply)).FabPK + uintptr(ii))) != 0 {
				_sessionAppendStr(tls, bp+40, zComma2, bp+8)
				_sessionAppendIdent(tls, bp+40, **(**uintptr)(__ccgo_up((*TSessionApplyCtx)(unsafe.Pointer(pApply)).FazCol + uintptr(ii)*4)), bp+8)
				_sessionAppendStr(tls, bp+52, zComma2, bp+8)
				_sessionAppendPrintf(tls, bp+52, bp+8, __ccgo_ts+37019, libc.VaList(bp+72, ii+int32(1)))
				zComma2 = __ccgo_ts + 17436
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
		if **(**int32)(__ccgo_up(bp + 12)) == 0 {
			_sessionAppendStr(tls, bp+16, zComma, bp+8)
			_sessionAppendStr(tls, bp+16, __ccgo_ts+32579, bp+8)
			_sessionAppendStr(tls, bp+28, zComma, bp+8)
			_sessionAppendStr(tls, bp+28, __ccgo_ts+6476, bp+8)
		}
		if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK {
			zSelect = Xsqlite3_mprintf(tls, __ccgo_ts+37627, libc.VaList(bp+72, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf, zTab, (**(**TSessionBuffer)(__ccgo_up(bp + 40))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp + 52))).FaBuf))
			if zSelect == uintptr(0) {
				**(**int32)(__ccgo_up(bp + 8)) = int32(SQLITE_NOMEM)
			}
		}
		if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK {
			zInsert = Xsqlite3_mprintf(tls, __ccgo_ts+37664, libc.VaList(bp+72, zTab, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf, (**(**TSessionBuffer)(__ccgo_up(bp + 28))).FaBuf))
			if zInsert == uintptr(0) {
				**(**int32)(__ccgo_up(bp + 8)) = int32(SQLITE_NOMEM)
			}
		}
		if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp + 8)) = _sessionPrepare(tls, db, bp+4, pApply+80, zSelect)
		}
		if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp + 8)) = _sessionPrepare(tls, db, bp, pApply+80, zInsert)
		}
		Xsqlite3_free(tls, zSelect)
		Xsqlite3_free(tls, zInsert)
		Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 16))).FaBuf)
		Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 28))).FaBuf)
		Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 40))).FaBuf)
		Xsqlite3_free(tls, (**(**TSessionBuffer)(__ccgo_up(bp + 52))).FaBuf)
	}
	if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 8)) = _sessionBindRow(tls, pUp, __ccgo_fp(Xsqlite3changeset_old), (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FabPK, **(**uintptr)(__ccgo_up(bp + 4)))
	}
	if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK && Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 4))) == int32(SQLITE_ROW) {
		iCol = 0
		for {
			if !(iCol < (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol) {
				break
			}
			pVal = **(**uintptr)(__ccgo_up((*Tsqlite3_changeset_iter)(unsafe.Pointer(pUp)).FapValue + uintptr(iCol+(*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol)*4))
			if pVal == uintptr(0) {
				pVal = Xsqlite3_column_value(tls, **(**uintptr)(__ccgo_up(bp + 4)), iCol)
			}
			**(**int32)(__ccgo_up(bp + 8)) = Xsqlite3_bind_value(tls, **(**uintptr)(__ccgo_up(bp)), iCol+int32(1), pVal)
			goto _2
		_2:
			;
			iCol = iCol + 1
		}
		if **(**int32)(__ccgo_up(bp + 12)) == 0 {
			Xsqlite3_bind_int64(tls, **(**uintptr)(__ccgo_up(bp)), iCol+int32(1), Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp + 4)), iCol))
		}
	}
	_sessionFinalizeStmt(tls, **(**uintptr)(__ccgo_up(bp + 4)), bp+8)
	/* Delete the row from the database. */
	if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 8)) = _sessionBindRow(tls, pUp, __ccgo_fp(Xsqlite3changeset_old), (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FabPK, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FpDelete)
		Xsqlite3_bind_int(tls, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FpDelete, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol+int32(1), int32(1))
	}
	if **(**int32)(__ccgo_up(bp + 8)) == SQLITE_OK {
		Xsqlite3_step(tls, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FpDelete)
		**(**int32)(__ccgo_up(bp + 8)) = Xsqlite3_reset(tls, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FpDelete)
	}
	if **(**int32)(__ccgo_up(bp + 8)) != SQLITE_OK {
		Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	}
	**(**uintptr)(__ccgo_up(ppInsert)) = **(**uintptr)(__ccgo_up(bp))
	return **(**int32)(__ccgo_up(bp + 8))
}

// C documentation
//
//	/*
//	** This function sets the value of the sqlite3_value object passed as the
//	** first argument to a copy of the string or blob held in the aData[]
//	** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
//	** error occurs.
//	*/
func _sessionValueSetStr(tls *libc.TLS, pVal uintptr, aData uintptr, nData int32, enc Tu8) (r int32) {
	var aCopy uintptr
	_ = aCopy
	/* In theory this code could just pass SQLITE_TRANSIENT as the final
	 ** argument to sqlite3ValueSetStr() and have the copy created
	 ** automatically. But doing so makes it difficult to detect any OOM
	 ** error. Hence the code to create the copy externally. */
	aCopy = Xsqlite3_malloc64(tls, uint64(int64(nData)+int64(1)))
	if aCopy == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemcpy(tls, aCopy, aData, uint32(nData))
	_sqlite3ValueSetStr(tls, pVal, nData, aCopy, enc, __ccgo_fp(Xsqlite3_free))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Read a varint value from buffer aBuf[], size nBuf bytes, into *piVal.
//	** Return the number of bytes read.
//	*/
func _sessionVarintGetSafe(tls *libc.TLS, aBuf uintptr, nBuf int32, piVal uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aRead uintptr
	var v1 int32
	var _ /* aCopy at bp+0 */ [9]Tu8
	_, _ = aRead, v1
	aRead = aBuf
	libc.Xmemset(tls, bp, 0, uint32(9))
	if uint32(nBuf) < uint32(9) {
		libc.Xmemcpy(tls, bp, aBuf, uint32(nBuf))
		aRead = bp
	}
	if int32(**(**Tu8)(__ccgo_up(aRead))) < int32(libc.Uint8FromInt32(0x80)) {
		**(**int32)(__ccgo_up(piVal)) = int32(uint32(**(**Tu8)(__ccgo_up(aRead))))
		v1 = libc.Int32FromInt32(1)
	} else {
		v1 = int32(_sqlite3GetVarint32(tls, aRead, piVal))
	}
	return int32(uint8(v1))
}

/* Load an unaligned and unsigned 32-bit integer */

// C documentation
//
//	/**************************** sqlite3_result_  *******************************
//	** The following routines are used by user-defined functions to specify
//	** the function result.
//	**
//	** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
//	** result as a string or blob.  Appropriate errors are set if the string/blob
//	** is too big or if an OOM occurs.
//	**
//	** The invokeValueDestructor(P,X) routine invokes destructor function X()
//	** on value P if P is not going to be used and need to be destroyed.
//	*/
func _setResultStrOrError(tls *libc.TLS, pCtx uintptr, z uintptr, n int32, enc Tu8, __ccgo_fp_xDel uintptr) {
	var pOut, v1 uintptr
	var rc int32
	_, _, _ = pOut, rc, v1
	pOut = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut
	if int32(enc) == int32(SQLITE_UTF8) {
		rc = _sqlite3VdbeMemSetText(tls, pOut, z, int64(n), __ccgo_fp_xDel)
	} else {
		if int32(enc) == int32(SQLITE_UTF8_ZT) {
			/* It is usually considered improper to assert() on an input. However,
			 ** the following assert() is checking for inputs that are documented
			 ** to result in undefined behavior. */
			rc = _sqlite3VdbeMemSetText(tls, pOut, z, int64(n), __ccgo_fp_xDel)
			v1 = pOut + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
		} else {
			rc = _sqlite3VdbeMemSetStr(tls, pOut, z, int64(n), enc, __ccgo_fp_xDel)
		}
	}
	if rc != 0 {
		if rc == int32(SQLITE_TOOBIG) {
			Xsqlite3_result_error_toobig(tls, pCtx)
		} else {
			/* The only errors possible from sqlite3VdbeMemSetStr are
			 ** SQLITE_TOOBIG and SQLITE_NOMEM */
			Xsqlite3_result_error_nomem(tls, pCtx)
		}
		return
	}
	_sqlite3VdbeChangeEncoding(tls, pOut, int32((*Tsqlite3_context)(unsafe.Pointer(pCtx)).Fenc))
	if _sqlite3VdbeMemTooBig(tls, pOut) != 0 {
		Xsqlite3_result_error_toobig(tls, pCtx)
	}
}

// C documentation
//
//	/*
//	** Add a lock on the table with root-page iTable to the shared-btree used
//	** by Btree handle p. Parameter eLock must be either READ_LOCK or
//	** WRITE_LOCK.
//	**
//	** This function assumes the following:
//	**
//	**   (a) The specified Btree object p is connected to a sharable
//	**       database (one with the BtShared.sharable flag set), and
//	**
//	**   (b) No other Btree objects hold a lock that conflicts
//	**       with the requested lock (i.e. querySharedCacheTableLock() has
//	**       already been called and returned SQLITE_OK).
//	**
//	** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
//	** is returned if a malloc attempt fails.
//	*/
func _setSharedCacheTableLock(tls *libc.TLS, p uintptr, iTable TPgno, eLock Tu8) (r int32) {
	var pBt, pIter, pLock uintptr
	_, _, _ = pBt, pIter, pLock
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt
	pLock = uintptr(0)
	/* A connection with the read-uncommitted flag set will never try to
	 ** obtain a read-lock using this function. The only read-lock obtained
	 ** by a connection in read-uncommitted mode is on the sqlite_schema
	 ** table, and that lock is obtained in BtreeBeginTrans().  */
	/* This function should only be called on a sharable b-tree after it
	 ** has been determined that no other b-tree holds a conflicting lock.  */
	/* First search the list for an existing lock on this table. */
	pIter = (*TBtShared)(unsafe.Pointer(pBt)).FpLock
	for {
		if !(pIter != 0) {
			break
		}
		if (*TBtLock)(unsafe.Pointer(pIter)).FiTable == iTable && (*TBtLock)(unsafe.Pointer(pIter)).FpBtree == p {
			pLock = pIter
			break
		}
		goto _1
	_1:
		;
		pIter = (*TBtLock)(unsafe.Pointer(pIter)).FpNext
	}
	/* If the above search did not find a BtLock struct associating Btree p
	 ** with table iTable, allocate one and link it into the list.
	 */
	if !(pLock != 0) {
		pLock = _sqlite3MallocZero(tls, uint64(16))
		if !(pLock != 0) {
			return int32(SQLITE_NOMEM)
		}
		(*TBtLock)(unsafe.Pointer(pLock)).FiTable = iTable
		(*TBtLock)(unsafe.Pointer(pLock)).FpBtree = p
		(*TBtLock)(unsafe.Pointer(pLock)).FpNext = (*TBtShared)(unsafe.Pointer(pBt)).FpLock
		(*TBtShared)(unsafe.Pointer(pBt)).FpLock = pLock
	}
	/* Set the BtLock.eLock variable to the maximum of the current lock
	 ** and the requested lock. This means if a write-lock was already held
	 ** and a read-lock requested, we don't incorrectly downgrade the lock.
	 */
	if int32(eLock) > int32((*TBtLock)(unsafe.Pointer(pLock)).FeLock) {
		(*TBtLock)(unsafe.Pointer(pLock)).FeLock = eLock
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Set up the lookaside buffers for a database connection.
//	** Return SQLITE_OK on success.
//	** If lookaside is already active, return SQLITE_BUSY.
//	**
//	** The sz parameter is the number of bytes in each lookaside slot.
//	** The cnt parameter is the number of slots.  If pBuf is NULL the
//	** space for the lookaside memory is obtained from sqlite3_malloc()
//	** or similar.  If pBuf is not NULL then it is sz*cnt bytes of memory
//	** to use for the lookaside memory.
//	*/
func _setupLookaside(tls *libc.TLS, db uintptr, pBuf uintptr, sz int32, cnt int32) (r int32) {
	var i, nBig, nSm, v1 int32
	var p, pStart uintptr
	var szAlloc Tsqlite3_int64
	_, _, _, _, _, _, _ = i, nBig, nSm, p, pStart, szAlloc, v1 /* Number smaller LOOKASIDE_SMALL-byte slots */
	if _sqlite3LookasideUsed(tls, db, uintptr(0)) > 0 {
		return int32(SQLITE_BUSY)
	}
	/* Free any existing lookaside buffer for this handle before
	 ** allocating a new one so we don't have to have space for
	 ** both at the same time.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbMalloced != 0 {
		Xsqlite3_free(tls, (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart)
	}
	/* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
	 ** than a pointer and small enough to fit in a u16.
	 */
	sz = sz & ^libc.Int32FromInt32(7)
	if sz <= libc.Int32FromInt64(4) {
		sz = 0
	}
	if sz > int32(65528) {
		sz = int32(65528)
	}
	/* Count must be at least 1 to be useful, but not so large as to use
	 ** more than 0x7fff0000 total bytes for lookaside. */
	if cnt < int32(1) {
		cnt = 0
	}
	if sz > 0 && cnt > int32(0x7fff0000)/sz {
		cnt = int32(0x7fff0000) / sz
	}
	szAlloc = int64(sz) * int64(cnt)
	if szAlloc == 0 {
		sz = 0
		pStart = uintptr(0)
	} else {
		if pBuf == uintptr(0) {
			_sqlite3BeginBenignMalloc(tls)
			pStart = _sqlite3Malloc(tls, uint64(szAlloc))
			_sqlite3EndBenignMalloc(tls)
			if pStart != 0 {
				szAlloc = int64(_sqlite3MallocSize(tls, pStart))
			}
		} else {
			pStart = pBuf
		}
	}
	if sz >= libc.Int32FromInt32(LOOKASIDE_SMALL)*libc.Int32FromInt32(3) {
		nBig = int32(szAlloc / int64(libc.Int32FromInt32(3)*libc.Int32FromInt32(LOOKASIDE_SMALL)+sz))
		nSm = int32((szAlloc - int64(sz)*int64(nBig)) / int64(LOOKASIDE_SMALL))
	} else {
		if sz >= libc.Int32FromInt32(LOOKASIDE_SMALL)*libc.Int32FromInt32(2) {
			nBig = int32(szAlloc / int64(libc.Int32FromInt32(LOOKASIDE_SMALL)+sz))
			nSm = int32((szAlloc - int64(sz)*int64(nBig)) / int64(LOOKASIDE_SMALL))
		} else {
			if sz > 0 {
				nBig = int32(szAlloc / int64(sz))
				nSm = 0
			} else {
				v1 = libc.Int32FromInt32(0)
				nSm = v1
				nBig = v1
			}
		}
	}
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart = pStart
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit = uintptr(0)
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpFree = uintptr(0)
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(sz)
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue = uint16(sz)
	if pStart != 0 {
		p = pStart
		i = 0
		for {
			if !(i < nBig) {
				break
			}
			(*TLookasideSlot)(unsafe.Pointer(p)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit
			(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpInit = p
			p = p + uintptr(sz)
			goto _2
		_2:
			;
			i = i + 1
		}
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit = uintptr(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = uintptr(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle = p
		i = 0
		for {
			if !(i < nSm) {
				break
			}
			(*TLookasideSlot)(unsafe.Pointer(p)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit
			(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit = p
			p = p + 128
			goto _3
		_3:
			;
			i = i + 1
		}
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = p
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = uint32(0)
		if pBuf == uintptr(0) {
			v1 = int32(1)
		} else {
			v1 = 0
		}
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbMalloced = uint8(v1)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FnSlot = uint32(nBig + nSm)
	} else {
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart = uintptr(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallInit = uintptr(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = uintptr(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle = uintptr(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd = uintptr(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = uint32(1)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbMalloced = uint8(0)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FnSlot = uint32(0)
	}
	(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
//	*/
func _splitNodeStartree(tls *libc.TLS, pRtree uintptr, aCell uintptr, nCell int32, pLeft uintptr, pRight uintptr, pBboxLeft uintptr, pBboxRight uintptr) (r int32) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var aSpare, aaSorted, pBbox, pCell, pTarget, v7, v8 uintptr
	var area, fBestArea, fBestMargin, fBestOverlap, margin, overlap TRtreeDValue
	var iBestDim, iBestLeft, iBestSplit, ii, jj, kk, nLeft int32
	var nByte Tsqlite3_int64
	var _ /* left at bp+0 */ TRtreeCell
	var _ /* right at bp+48 */ TRtreeCell
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aSpare, aaSorted, area, fBestArea, fBestMargin, fBestOverlap, iBestDim, iBestLeft, iBestSplit, ii, jj, kk, margin, nByte, nLeft, overlap, pBbox, pCell, pTarget, v7, v8
	iBestDim = 0
	iBestSplit = 0
	fBestMargin = float64(0)
	nByte = int64(uint32(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)+libc.Int32FromInt32(1)) * (uint32(4) + uint32(nCell)*uint32(4)))
	aaSorted = Xsqlite3_malloc64(tls, uint64(nByte))
	if !(aaSorted != 0) {
		return int32(SQLITE_NOMEM)
	}
	aSpare = aaSorted + uintptr((*TRtree)(unsafe.Pointer(pRtree)).FnDim)*4 + uintptr(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)*nCell)*4
	libc.Xmemset(tls, aaSorted, 0, uint32(nByte))
	ii = 0
	for {
		if !(ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)) {
			break
		}
		**(**uintptr)(__ccgo_up(aaSorted + uintptr(ii)*4)) = aaSorted + uintptr((*TRtree)(unsafe.Pointer(pRtree)).FnDim)*4 + uintptr(ii*nCell)*4
		jj = 0
		for {
			if !(jj < nCell) {
				break
			}
			**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(aaSorted + uintptr(ii)*4)) + uintptr(jj)*4)) = jj
			goto _2
		_2:
			;
			jj = jj + 1
		}
		_SortByDimension(tls, pRtree, **(**uintptr)(__ccgo_up(aaSorted + uintptr(ii)*4)), nCell, ii, aCell, aSpare)
		goto _1
	_1:
		;
		ii = ii + 1
	}
	ii = 0
	for {
		if !(ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)) {
			break
		}
		margin = float64(0)
		fBestOverlap = float64(0)
		fBestArea = float64(0)
		iBestLeft = 0
		nLeft = ((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize - int32(4)) / int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell) / int32(3)
		for {
			if !(nLeft <= nCell-((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize-int32(4))/int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)/int32(3)) {
				break
			}
			libc.Xmemcpy(tls, bp, aCell+uintptr(**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(aaSorted + uintptr(ii)*4)))))*48, uint32(48))
			libc.Xmemcpy(tls, bp+48, aCell+uintptr(**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(aaSorted + uintptr(ii)*4)) + uintptr(nCell-int32(1))*4)))*48, uint32(48))
			kk = int32(1)
			for {
				if !(kk < nCell-int32(1)) {
					break
				}
				if kk < nLeft {
					_cellUnion(tls, pRtree, bp, aCell+uintptr(**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(aaSorted + uintptr(ii)*4)) + uintptr(kk)*4)))*48)
				} else {
					_cellUnion(tls, pRtree, bp+48, aCell+uintptr(**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(aaSorted + uintptr(ii)*4)) + uintptr(kk)*4)))*48)
				}
				goto _5
			_5:
				;
				kk = kk + 1
			}
			margin = margin + _cellMargin(tls, pRtree, bp)
			margin = margin + _cellMargin(tls, pRtree, bp+48)
			overlap = _cellOverlap(tls, pRtree, bp, bp+48, int32(1))
			area = _cellArea(tls, pRtree, bp) + _cellArea(tls, pRtree, bp+48)
			if nLeft == ((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize-int32(4))/int32((*TRtree)(unsafe.Pointer(pRtree)).FnBytesPerCell)/int32(3) || overlap < fBestOverlap || overlap == fBestOverlap && area < fBestArea {
				iBestLeft = nLeft
				fBestOverlap = overlap
				fBestArea = area
			}
			goto _4
		_4:
			;
			nLeft = nLeft + 1
		}
		if ii == 0 || margin < fBestMargin {
			iBestDim = ii
			fBestMargin = margin
			iBestSplit = iBestLeft
		}
		goto _3
	_3:
		;
		ii = ii + 1
	}
	libc.Xmemcpy(tls, pBboxLeft, aCell+uintptr(**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(aaSorted + uintptr(iBestDim)*4)))))*48, uint32(48))
	libc.Xmemcpy(tls, pBboxRight, aCell+uintptr(**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(aaSorted + uintptr(iBestDim)*4)) + uintptr(iBestSplit)*4)))*48, uint32(48))
	ii = 0
	for {
		if !(ii < nCell) {
			break
		}
		if ii < iBestSplit {
			v7 = pLeft
		} else {
			v7 = pRight
		}
		pTarget = v7
		if ii < iBestSplit {
			v8 = pBboxLeft
		} else {
			v8 = pBboxRight
		}
		pBbox = v8
		pCell = aCell + uintptr(**(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(aaSorted + uintptr(iBestDim)*4)) + uintptr(ii)*4)))*48
		_nodeInsertCell(tls, pRtree, pTarget, pCell)
		_cellUnion(tls, pRtree, pBbox, pCell)
		goto _6
	_6:
		;
		ii = ii + 1
	}
	Xsqlite3_free(tls, aaSorted)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Add a new CHECK constraint to the table currently under construction.
//	*/
func _sqlite3AddCheckConstraint(tls *libc.TLS, pParse uintptr, pCheckExpr uintptr, zStart uintptr, zEnd uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pTab uintptr
	var _ /* t at bp+0 */ TToken
	_, _ = db, pTab
	pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if pTab != 0 && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) == libc.Int32FromInt32(PARSE_MODE_DECLARE_VTAB)) && !(_sqlite3BtreeIsReadonly(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb)*16))).FpBt) != 0) {
		(*TTable)(unsafe.Pointer(pTab)).FpCheck = _sqlite3ExprListAppend(tls, pParse, (*TTable)(unsafe.Pointer(pTab)).FpCheck, pCheckExpr)
		if (*(*struct {
			FaddrCrTab      int32
			FregRowid       int32
			FregRoot        int32
			FconstraintName TToken
		})(unsafe.Pointer(pParse + 188))).FconstraintName.Fn != 0 {
			_sqlite3ExprListSetName(tls, pParse, (*TTable)(unsafe.Pointer(pTab)).FpCheck, pParse+188+12, int32(1))
		} else {
			zStart = zStart + 1
			for {
				if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zStart)))])&int32(0x01) != 0) {
					break
				}
				goto _1
			_1:
				;
				zStart = zStart + 1
			}
			for int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zEnd + uintptr(-libc.Int32FromInt32(1)))))])&int32(0x01) != 0 {
				zEnd = zEnd - 1
			}
			(**(**TToken)(__ccgo_up(bp))).Fz = zStart
			(**(**TToken)(__ccgo_up(bp))).Fn = uint32(int32(zEnd) - int32((**(**TToken)(__ccgo_up(bp))).Fz))
			_sqlite3ExprListSetName(tls, pParse, (*TTable)(unsafe.Pointer(pTab)).FpCheck, bp, int32(1))
		}
	} else {
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pCheckExpr)
	}
}

// C documentation
//
//	/*
//	** Set the collation function of the most recently parsed table column
//	** to the CollSeq given.
//	*/
func _sqlite3AddCollateType(tls *libc.TLS, pParse uintptr, pToken uintptr) {
	var db, p, pIdx, zColl, v1 uintptr
	var i int32
	_, _, _, _, _, _ = db, i, p, pIdx, zColl, v1
	v1 = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	p = v1
	if v1 == uintptr(0) || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		return
	}
	i = int32((*TTable)(unsafe.Pointer(p)).FnCol) - int32(1)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	zColl = _sqlite3NameFromToken(tls, db, pToken)
	if !(zColl != 0) {
		return
	}
	if _sqlite3LocateCollSeq(tls, pParse, zColl) != 0 {
		_sqlite3ColumnSetColl(tls, db, (*TTable)(unsafe.Pointer(p)).FaCol+uintptr(i)*12, zColl)
		/* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
		 ** then an index may have been created on this column before the
		 ** collation type was added. Correct this if it is the case.
		 */
		pIdx = (*TTable)(unsafe.Pointer(p)).FpIndex
		for {
			if !(pIdx != 0) {
				break
			}
			if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn))) == i {
				**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl)) = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(p)).FaCol+uintptr(i)*12)
			}
			goto _2
		_2:
			;
			pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		}
	}
	_sqlite3DbFree(tls, db, zColl)
}

// C documentation
//
//	/*
//	** Add a new column to the table currently being constructed.
//	**
//	** The parser calls this routine once for each column declaration
//	** in a CREATE TABLE statement.  sqlite3StartTable() gets called
//	** first to get things going.  Then this routine is called for each
//	** column.
//	*/
func _sqlite3AddColumn(tls *libc.TLS, pParse uintptr, _sName TToken, _sType TToken) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	*(*TToken)(unsafe.Pointer(bp)) = _sName
	*(*TToken)(unsafe.Pointer(bp + 8)) = _sType
	var aNew, db, p, pCol, z, zType, v1 uintptr
	var affinity int8
	var eType, h, szEst Tu8
	var i int32
	_, _, _, _, _, _, _, _, _, _, _, _ = aNew, affinity, db, eType, h, i, p, pCol, szEst, z, zType, v1
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	eType = uint8(COLTYPE_CUSTOM)
	szEst = uint8(1)
	affinity = int8(SQLITE_AFF_BLOB)
	v1 = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	p = v1
	if v1 == uintptr(0) {
		return
	}
	if int32((*TTable)(unsafe.Pointer(p)).FnCol)+int32(1) > **(**int32)(__ccgo_up(db + 120 + 2*4)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15173, libc.VaList(bp+24, (*TTable)(unsafe.Pointer(p)).FzName))
		return
	}
	if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
		_sqlite3DequoteToken(tls, bp)
	}
	/* Because keywords GENERATE ALWAYS can be converted into identifiers
	 ** by the parser, we can sometimes end up with a typename that ends
	 ** with "generated always".  Check for this case and omit the surplus
	 ** text. */
	if (**(**TToken)(__ccgo_up(bp + 8))).Fn >= uint32(16) && Xsqlite3_strnicmp(tls, (**(**TToken)(__ccgo_up(bp + 8))).Fz+uintptr((**(**TToken)(__ccgo_up(bp + 8))).Fn-libc.Uint32FromInt32(6)), __ccgo_ts+15196, int32(6)) == 0 {
		(**(**TToken)(__ccgo_up(bp + 8))).Fn -= uint32(6)
		for (**(**TToken)(__ccgo_up(bp + 8))).Fn > uint32(0) && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up((**(**TToken)(__ccgo_up(bp + 8))).Fz + uintptr((**(**TToken)(__ccgo_up(bp + 8))).Fn-uint32(1)))))])&int32(0x01) != 0 {
			(**(**TToken)(__ccgo_up(bp + 8))).Fn = (**(**TToken)(__ccgo_up(bp + 8))).Fn - 1
		}
		if (**(**TToken)(__ccgo_up(bp + 8))).Fn >= uint32(9) && Xsqlite3_strnicmp(tls, (**(**TToken)(__ccgo_up(bp + 8))).Fz+uintptr((**(**TToken)(__ccgo_up(bp + 8))).Fn-libc.Uint32FromInt32(9)), __ccgo_ts+15203, int32(9)) == 0 {
			(**(**TToken)(__ccgo_up(bp + 8))).Fn -= uint32(9)
			for (**(**TToken)(__ccgo_up(bp + 8))).Fn > uint32(0) && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up((**(**TToken)(__ccgo_up(bp + 8))).Fz + uintptr((**(**TToken)(__ccgo_up(bp + 8))).Fn-uint32(1)))))])&int32(0x01) != 0 {
				(**(**TToken)(__ccgo_up(bp + 8))).Fn = (**(**TToken)(__ccgo_up(bp + 8))).Fn - 1
			}
		}
	}
	/* Check for standard typenames.  For standard typenames we will
	 ** set the Column.eType field rather than storing the typename after
	 ** the column name, in order to save space. */
	if (**(**TToken)(__ccgo_up(bp + 8))).Fn >= uint32(3) {
		_sqlite3DequoteToken(tls, bp+8)
		i = 0
		for {
			if !(i < int32(SQLITE_N_STDTYPE)) {
				break
			}
			if (**(**TToken)(__ccgo_up(bp + 8))).Fn == uint32(_sqlite3StdTypeLen[i]) && Xsqlite3_strnicmp(tls, (**(**TToken)(__ccgo_up(bp + 8))).Fz, _sqlite3StdType[i], int32((**(**TToken)(__ccgo_up(bp + 8))).Fn)) == 0 {
				(**(**TToken)(__ccgo_up(bp + 8))).Fn = uint32(0)
				eType = uint8(i + int32(1))
				affinity = _sqlite3StdTypeAffinity[i]
				if int32(affinity) <= int32(SQLITE_AFF_TEXT) {
					szEst = uint8(5)
				}
				break
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	z = _sqlite3DbMallocRaw(tls, db, uint64(int64((**(**TToken)(__ccgo_up(bp))).Fn)+int64(1)+int64((**(**TToken)(__ccgo_up(bp + 8))).Fn)+libc.BoolInt64((**(**TToken)(__ccgo_up(bp + 8))).Fn > libc.Uint32FromInt32(0))))
	if z == uintptr(0) {
		return
	}
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		_sqlite3RenameTokenMap(tls, pParse, z, bp)
	}
	libc.Xmemcpy(tls, z, (**(**TToken)(__ccgo_up(bp))).Fz, (**(**TToken)(__ccgo_up(bp))).Fn)
	**(**int8)(__ccgo_up(z + uintptr((**(**TToken)(__ccgo_up(bp))).Fn))) = 0
	_sqlite3Dequote(tls, z)
	if (*TTable)(unsafe.Pointer(p)).FnCol != 0 && _sqlite3ColumnIndex(tls, p, z) >= 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15213, libc.VaList(bp+24, z))
		_sqlite3DbFree(tls, db, z)
		return
	}
	aNew = _sqlite3DbRealloc(tls, db, (*TTable)(unsafe.Pointer(p)).FaCol, uint64((int64((*TTable)(unsafe.Pointer(p)).FnCol)+int64(1))*int64(12)))
	if aNew == uintptr(0) {
		_sqlite3DbFree(tls, db, z)
		return
	}
	(*TTable)(unsafe.Pointer(p)).FaCol = aNew
	pCol = (*TTable)(unsafe.Pointer(p)).FaCol + uintptr((*TTable)(unsafe.Pointer(p)).FnCol)*12
	libc.Xmemset(tls, pCol, 0, uint32(12))
	(*TColumn)(unsafe.Pointer(pCol)).FzCnName = z
	(*TColumn)(unsafe.Pointer(pCol)).FhName = _sqlite3StrIHash(tls, z)
	if (**(**TToken)(__ccgo_up(bp + 8))).Fn == uint32(0) {
		/* If there is no type specified, columns have the default affinity
		 ** 'BLOB' with a default size of 4 bytes. */
		(*TColumn)(unsafe.Pointer(pCol)).Faffinity = affinity
		libc.SetBitFieldPtr8Uint32(pCol+4, uint32(eType), 4, 0xf0)
		(*TColumn)(unsafe.Pointer(pCol)).FszEst = szEst
	} else {
		zType = z + uintptr(_sqlite3Strlen30(tls, z)) + uintptr(1)
		libc.Xmemcpy(tls, zType, (**(**TToken)(__ccgo_up(bp + 8))).Fz, (**(**TToken)(__ccgo_up(bp + 8))).Fn)
		**(**int8)(__ccgo_up(zType + uintptr((**(**TToken)(__ccgo_up(bp + 8))).Fn))) = 0
		_sqlite3Dequote(tls, zType)
		(*TColumn)(unsafe.Pointer(pCol)).Faffinity = _sqlite3AffinityType(tls, zType, pCol)
		v1 = pCol + 10
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(COLFLAG_HASTYPE))
	}
	if int32((*TTable)(unsafe.Pointer(p)).FnCol) <= int32(0xff) {
		h = uint8(uint32((*TColumn)(unsafe.Pointer(pCol)).FhName) % uint32(16))
		**(**Tu8)(__ccgo_up(p + 64 + uintptr(h))) = uint8((*TTable)(unsafe.Pointer(p)).FnCol)
	}
	(*TTable)(unsafe.Pointer(p)).FnCol = (*TTable)(unsafe.Pointer(p)).FnCol + 1
	(*TTable)(unsafe.Pointer(p)).FnNVCol = (*TTable)(unsafe.Pointer(p)).FnNVCol + 1
	(*(*struct {
		FaddrCrTab      int32
		FregRowid       int32
		FregRoot        int32
		FconstraintName TToken
	})(unsafe.Pointer(pParse + 188))).FconstraintName.Fn = uint32(0)
}

// C documentation
//
//	/*
//	** The expression is the default value for the most recently added column
//	** of the table currently under construction.
//	**
//	** Default value expressions must be constant.  Raise an exception if this
//	** is not the case.
//	**
//	** This routine is called by the parser while in the middle of
//	** parsing a CREATE TABLE statement.
//	*/
func _sqlite3AddDefaultValue(tls *libc.TLS, pParse uintptr, pExpr uintptr, zStart uintptr, zEnd uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var db, p, pCol, pDfltExpr uintptr
	var isInit int32
	var _ /* x at bp+0 */ TExpr
	_, _, _, _, _ = db, isInit, p, pCol, pDfltExpr
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	if p != uintptr(0) {
		isInit = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb) != int32(1))
		pCol = (*TTable)(unsafe.Pointer(p)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(p)).FnCol)-int32(1))*12
		if !(_sqlite3ExprIsConstantOrFunction(tls, pExpr, uint8(isInit)) != 0) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15239, libc.VaList(bp+64, (*TColumn)(unsafe.Pointer(pCol)).FzCnName))
		} else {
			if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15284, 0)
			} else {
				libc.Xmemset(tls, bp, 0, uint32(52))
				(**(**TExpr)(__ccgo_up(bp))).Fop = uint8(TK_SPAN)
				*(*uintptr)(unsafe.Pointer(bp + 8)) = _sqlite3DbSpanDup(tls, db, zStart, zEnd)
				(**(**TExpr)(__ccgo_up(bp))).FpLeft = pExpr
				(**(**TExpr)(__ccgo_up(bp))).Fflags = uint32(EP_Skip)
				pDfltExpr = _sqlite3ExprDup(tls, db, bp, int32(EXPRDUP_REDUCE))
				_sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp + 8)))
				_sqlite3ColumnSetExpr(tls, pParse, p, pCol, pDfltExpr)
			}
		}
	}
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		_sqlite3RenameExprUnmap(tls, pParse, pExpr)
	}
	_sqlite3ExprDelete(tls, db, pExpr)
}

// C documentation
//
//	/* Change the most recently parsed column to be a GENERATED ALWAYS AS
//	** column.
//	*/
func _sqlite3AddGenerated(tls *libc.TLS, pParse uintptr, pExpr uintptr, pType uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var eType Tu8
	var pCol, pTab, v1 uintptr
	_, _, _, _ = eType, pCol, pTab, v1
	eType = uint8(COLFLAG_VIRTUAL)
	pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	if pTab == uintptr(0) {
		/* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
		goto generated_done
	}
	pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-int32(1))*12
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) == int32(PARSE_MODE_DECLARE_VTAB) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15474, 0)
		goto generated_done
	}
	if int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) > 0 {
		goto generated_error
	}
	if pType != 0 {
		if (*TToken)(unsafe.Pointer(pType)).Fn == uint32(7) && Xsqlite3_strnicmp(tls, __ccgo_ts+15517, (*TToken)(unsafe.Pointer(pType)).Fz, int32(7)) == 0 {
			/* no-op */
		} else {
			if (*TToken)(unsafe.Pointer(pType)).Fn == uint32(6) && Xsqlite3_strnicmp(tls, __ccgo_ts+15525, (*TToken)(unsafe.Pointer(pType)).Fz, int32(6)) == 0 {
				eType = uint8(COLFLAG_STORED)
			} else {
				goto generated_error
			}
		}
	}
	if int32(eType) == int32(COLFLAG_VIRTUAL) {
		(*TTable)(unsafe.Pointer(pTab)).FnNVCol = (*TTable)(unsafe.Pointer(pTab)).FnNVCol - 1
	}
	v1 = pCol + 10
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | int32(eType))
	**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(eType)
	if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 {
		_makeColumnPartOfPrimaryKey(tls, pParse, pCol) /* For the error message */
	}
	if pExpr != 0 && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_ID) {
		/* The value of a generated column needs to be a real expression, not
		 ** just a reference to another column, in order for covering index
		 ** optimizations to work correctly.  So if the value is not an expression,
		 ** turn it into one by adding a unary "+" operator. */
		pExpr = _sqlite3PExpr(tls, pParse, int32(TK_UPLUS), pExpr, uintptr(0))
	}
	if pExpr != 0 && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_RAISE) {
		(*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = (*TColumn)(unsafe.Pointer(pCol)).Faffinity
	}
	_sqlite3ColumnSetExpr(tls, pParse, pTab, pCol, pExpr)
	pExpr = uintptr(0)
	goto generated_done
	goto generated_error
generated_error:
	;
	_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15532, libc.VaList(bp+8, (*TColumn)(unsafe.Pointer(pCol)).FzCnName))
	goto generated_done
generated_done:
	;
	_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr)
}

// C documentation
//
//	/*
//	** This routine is called by the parser while in the middle of
//	** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
//	** been seen on a column.  This routine sets the notNull flag on
//	** the column currently under construction.
//	*/
func _sqlite3AddNotNull(tls *libc.TLS, pParse uintptr, onError int32) {
	var p, pCol, pIdx uintptr
	_, _, _ = p, pCol, pIdx
	p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	if p == uintptr(0) || int32((*TTable)(unsafe.Pointer(p)).FnCol) < int32(1) {
		return
	}
	pCol = (*TTable)(unsafe.Pointer(p)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(p)).FnCol)-int32(1))*12
	libc.SetBitFieldPtr8Uint32(pCol+4, uint32(uint8(onError)), 0, 0xf)
	**(**Tu32)(__ccgo_up(p + 28)) |= uint32(TF_HasNotNull)
	/* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
	 ** on this column.  */
	if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_UNIQUE) != 0 {
		pIdx = (*TTable)(unsafe.Pointer(p)).FpIndex
		for {
			if !(pIdx != 0) {
				break
			}
			if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn))) == int32((*TTable)(unsafe.Pointer(p)).FnCol)-int32(1) {
				libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(1), 3, 0x8)
			}
			goto _1
		_1:
			;
			pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		}
	}
}

// C documentation
//
//	/*
//	** Designate the PRIMARY KEY for the table.  pList is a list of names
//	** of columns that form the primary key.  If pList is NULL, then the
//	** most recently added column of the table is the primary key.
//	**
//	** A table can have at most one primary key.  If the table already has
//	** a primary key (and this is the second primary key) then create an
//	** error.
//	**
//	** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
//	** then we will try to use that column as the rowid.  Set the Table.iPKey
//	** field of the table under construction to be the index of the
//	** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
//	** no INTEGER PRIMARY KEY.
//	**
//	** If the key is not an INTEGER PRIMARY KEY, then create a unique
//	** index for the key.  No index is created for INTEGER PRIMARY KEYs.
//	*/
func _sqlite3AddPrimaryKey(tls *libc.TLS, pParse uintptr, pList uintptr, onError int32, autoInc int32, sortOrder int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, iCol, nTerm int32
	var pCExpr, pCExpr1, pCol, pTab uintptr
	_, _, _, _, _, _, _ = i, iCol, nTerm, pCExpr, pCExpr1, pCol, pTab
	pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	pCol = uintptr(0)
	iCol = -int32(1)
	if pTab == uintptr(0) {
		goto primary_key_exit
	}
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasPrimaryKey) != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15377, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
		goto primary_key_exit
	}
	**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(TF_HasPrimaryKey)
	if pList == uintptr(0) {
		iCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) - int32(1)
		pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12
		_makeColumnPartOfPrimaryKey(tls, pParse, pCol)
		nTerm = int32(1)
	} else {
		nTerm = (*TExprList)(unsafe.Pointer(pList)).FnExpr
		i = 0
		for {
			if !(i < nTerm) {
				break
			}
			pCExpr = _sqlite3ExprSkipCollate(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr)
			_sqlite3StringToId(tls, pCExpr)
			if int32((*TExpr)(unsafe.Pointer(pCExpr)).Fop) == int32(TK_ID) {
				iCol = _sqlite3ColumnIndex(tls, pTab, *(*uintptr)(unsafe.Pointer(pCExpr + 8)))
				if iCol >= 0 {
					pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12
					_makeColumnPartOfPrimaryKey(tls, pParse, pCol)
				}
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	if nTerm == int32(1) && pCol != 0 && int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf0>>4)) == int32(COLTYPE_INTEGER) && sortOrder != int32(SQLITE_SO_DESC) {
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && pList != 0 {
			pCExpr1 = _sqlite3ExprSkipCollate(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8))).FpExpr)
			_sqlite3RenameTokenRemap(tls, pParse, pTab+32, pCExpr1)
		}
		(*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(iCol)
		(*TTable)(unsafe.Pointer(pTab)).FkeyConf = uint8(onError)
		**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(autoInc * int32(TF_Autoincrement))
		if pList != 0 {
			(*TParse)(unsafe.Pointer(pParse)).FiPkSortOrder = (*(*TExprList_item)(unsafe.Pointer(pList + 8))).Ffg.FsortFlags
		}
		_sqlite3HasExplicitNulls(tls, pParse, pList)
	} else {
		if autoInc != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15418, 0)
		} else {
			_sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), pList, onError, uintptr(0), uintptr(0), sortOrder, 0, uint8(SQLITE_IDXTYPE_PRIMARYKEY))
			pList = uintptr(0)
		}
	}
	goto primary_key_exit
primary_key_exit:
	;
	_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pList)
	return
}

// C documentation
//
//	/*
//	** Add the RETURNING clause to the parse currently underway.
//	**
//	** This routine creates a special TEMP trigger that will fire for each row
//	** of the DML statement.  That TEMP trigger contains a single SELECT
//	** statement with a result set that is the argument of the RETURNING clause.
//	** The trigger has the Trigger.bReturning flag and an opcode of
//	** TK_RETURNING instead of TK_SELECT, so that the trigger code generator
//	** knows to handle it specially.  The TEMP trigger is automatically
//	** removed at the end of the parse.
//	**
//	** When this routine is called, we do not yet know if the RETURNING clause
//	** is attached to a DELETE, INSERT, or UPDATE, so construct it as a
//	** RETURNING trigger instead.  It will then be converted into the appropriate
//	** type on the first call to sqlite3TriggersExist().
//	*/
func _sqlite3AddReturning(tls *libc.TLS, pParse uintptr, pList uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pHash, pRet uintptr
	_, _, _ = db, pHash, pRet
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15119, 0)
	} else {
	}
	libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 3, 0x8)
	pRet = _sqlite3DbMallocZero(tls, db, uint64(140))
	if pRet == uintptr(0) {
		_sqlite3ExprListDelete(tls, db, pList)
		return
	}
	(*(*struct {
		FpReturning uintptr
	})(unsafe.Pointer(&(*TParse)(unsafe.Pointer(pParse)).Fu1))).FpReturning = pRet
	(*TReturning)(unsafe.Pointer(pRet)).FpParse = pParse
	(*TReturning)(unsafe.Pointer(pRet)).FpReturnEL = pList
	_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3DeleteReturning), pRet)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		return
	}
	Xsqlite3_snprintf(tls, int32(40), pRet+100, __ccgo_ts+15153, libc.VaList(bp+8, pParse))
	(*TReturning)(unsafe.Pointer(pRet)).FretTrig.FzName = pRet + 100
	(*TReturning)(unsafe.Pointer(pRet)).FretTrig.Fop = uint8(TK_RETURNING)
	(*TReturning)(unsafe.Pointer(pRet)).FretTrig.Ftr_tm = uint8(TRIGGER_AFTER)
	(*TReturning)(unsafe.Pointer(pRet)).FretTrig.FbReturning = uint8(1)
	(*TReturning)(unsafe.Pointer(pRet)).FretTrig.FpSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema
	(*TReturning)(unsafe.Pointer(pRet)).FretTrig.FpTabSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema
	(*TReturning)(unsafe.Pointer(pRet)).FretTrig.Fstep_list = pRet + 44
	(*TReturning)(unsafe.Pointer(pRet)).FretTStep.Fop = uint8(TK_RETURNING)
	(*TReturning)(unsafe.Pointer(pRet)).FretTStep.FpTrig = pRet + 8
	(*TReturning)(unsafe.Pointer(pRet)).FretTStep.FpExprList = pList
	pHash = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema + 40
	if _sqlite3HashInsert(tls, pHash, pRet+100, pRet+8) == pRet+8 {
		_sqlite3OomFault(tls, db)
	}
}

// C documentation
//
//	/*
//	** Allocate heap space to hold an Index object with nCol columns.
//	**
//	** Increase the allocation size to provide an extra nExtra bytes
//	** of 8-byte aligned space after the Index object and return a
//	** pointer to this extra space in *ppExtra.
//	*/
func _sqlite3AllocateIndexObject(tls *libc.TLS, db uintptr, nCol int32, nExtra int32, ppExtra uintptr) (r uintptr) {
	var nByte Ti64
	var p, pExtra uintptr
	_, _, _ = nByte, p, pExtra                                                                                                                                                                                                                                                                                        /* Bytes of space for Index object + arrays */
	nByte = int64((libc.Uint32FromInt64(104)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)) + (uint32(4)*uint32(nCol)+uint32(7))&uint32(^libc.Int32FromInt32(7)) + (uint32(2)*uint32(nCol+libc.Int32FromInt32(1))+uint32(2)*uint32(nCol)+uint32(1)*uint32(nCol)+uint32(7))&uint32(^libc.Int32FromInt32(7))) /* Index.aSortOrder */
	p = _sqlite3DbMallocZero(tls, db, uint64(nByte+int64(nExtra)))
	if p != 0 {
		pExtra = p + uintptr((libc.Uint32FromInt64(104)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
		(*TIndex)(unsafe.Pointer(p)).FazColl = pExtra
		pExtra = pExtra + uintptr((libc.Uint32FromInt64(4)*uint32(nCol)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
		(*TIndex)(unsafe.Pointer(p)).FaiRowLogEst = pExtra
		pExtra = pExtra + uintptr(uint32(2)*uint32(nCol+libc.Int32FromInt32(1)))
		(*TIndex)(unsafe.Pointer(p)).FaiColumn = pExtra
		pExtra = pExtra + uintptr(uint32(2)*uint32(nCol))
		(*TIndex)(unsafe.Pointer(p)).FaSortOrder = pExtra
		(*TIndex)(unsafe.Pointer(p)).FnColumn = uint16(nCol)
		(*TIndex)(unsafe.Pointer(p)).FnKeyCol = uint16(nCol - libc.Int32FromInt32(1))
		**(**uintptr)(__ccgo_up(ppExtra)) = p + uintptr(nByte)
	}
	return p
}

// C documentation
//
//	/*
//	** Generate bytecode to implement:
//	**
//	**    ALTER TABLE pSrc ADD [CONSTRAINT pName] CHECK(pExpr)
//	**
//	** Any "ON CONFLICT" text that occurs after the "CHECK(...)", up
//	** until pParse->sLastToken, is included as part of the new constraint.
//	*/
func _sqlite3AlterAddConstraint(tls *libc.TLS, pParse uintptr, pSrc uintptr, pFirst uintptr, pName uintptr, zExpr uintptr, nExpr int32, pExpr uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var nCons, rc int32
	var pCons, pTab, zName uintptr
	var _ /* iDb at bp+0 */ int32
	var _ /* zDb at bp+4 */ uintptr
	_, _, _, _, _ = nCons, pCons, pTab, rc, zName
	pTab = uintptr(0)                             /* Table identified by pSrc */
	**(**int32)(__ccgo_up(bp)) = 0                /* Which schema does pTab live in */
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* Name of the schema in which pTab lives */
	pCons = uintptr(0)                            /* Result from error checking pExpr */
	/* Look up the table being altered. */
	pTab = _alterFindTable(tls, pParse, pSrc, bp, bp+4, int32(1))
	if !(pTab != 0) {
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr)
		return
	}
	/* Verify that the new CHECK constraint does not contain any
	 ** internal-use-only function.  Forum post 2026-05-10T01:11:28Z
	 */
	rc = _sqlite3ResolveSelfReference(tls, pParse, pTab, int32(NC_IsCheck), pExpr, uintptr(0))
	_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr)
	if rc != 0 {
		return
	}
	/* If this new constraint has a name, check that it is not a duplicate of
	 ** an existing constraint. It is an error if it is.  */
	if pName != 0 {
		zName = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pName)
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+13500, libc.VaList(bp+16, zName, int32(SQLITE_ERROR), **(**uintptr)(__ccgo_up(bp + 4)), (*TTable)(unsafe.Pointer(pTab)).FzName, zName))
		_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zName)
	}
	/* Search for a constraint violation. Throw an exception if one is found. */
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+13665, libc.VaList(bp+16, int32(SQLITE_CONSTRAINT), **(**uintptr)(__ccgo_up(bp + 4)), (*TTable)(unsafe.Pointer(pTab)).FzName, nExpr, zExpr))
	/* Edit the SQL for the named table. */
	pCons = (*TToken)(unsafe.Pointer(pFirst)).Fz
	nCons = _alterRtrimConstraint(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pCons, int32((*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz)-int32(pCons))
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+13745, libc.VaList(bp+16, **(**uintptr)(__ccgo_up(bp + 4)), nCons, pCons, (*TTable)(unsafe.Pointer(pTab)).FzName))
	/* Finally, reload the database schema. */
	_renameReloadSchema(tls, pParse, **(**int32)(__ccgo_up(bp)), uint16(INITFLAG_AlterDropCons))
}

// C documentation
//
//	/*
//	** This function is called by the parser after the table-name in
//	** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
//	** pSrc is the full-name of the table being altered.
//	**
//	** This routine makes a (partial) copy of the Table structure
//	** for the table being altered and sets Parse.pNewTable to point
//	** to it. Routines called by the parser as the column definition
//	** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
//	** the copy. The copy of the Table structure is deleted by tokenize.c
//	** after parsing is finished.
//	**
//	** Routine sqlite3AlterFinishAddColumn() will be called to complete
//	** coding the "ALTER TABLE ... ADD" statement.
//	*/
func _sqlite3AlterBeginAddColumn(tls *libc.TLS, pParse uintptr, pSrc uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pCol, pNew, pTab uintptr
	var i, iDb, nAlloc int32
	_, _, _, _, _, _, _ = db, i, iDb, nAlloc, pCol, pNew, pTab
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	/* Look up the table being altered. */
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto exit_begin_add_column
	}
	pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pSrc+8)
	if !(pTab != 0) {
		goto exit_begin_add_column
	}
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12249, 0)
		goto exit_begin_add_column
	}
	/* Make sure this is not an attempt to ALTER a view. */
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12283, 0)
		goto exit_begin_add_column
	}
	if SQLITE_OK != _isAlterableTable(tls, pParse, pTab) {
		goto exit_begin_add_column
	}
	_sqlite3MayAbort(tls, pParse)
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	/* Put a copy of the Table struct in Parse.pNewTable for the
	 ** sqlite3AddColumn() function and friends to modify.  But modify
	 ** the name by adding an "sqlite_altertab_" prefix.  By adding this
	 ** prefix, we insure that the name will not collide with an existing
	 ** table because user table are not allowed to have the "sqlite_"
	 ** prefix on their name.
	 */
	pNew = _sqlite3DbMallocZero(tls, db, uint64(80))
	if !(pNew != 0) {
		goto exit_begin_add_column
	}
	(*TParse)(unsafe.Pointer(pParse)).FpNewTable = pNew
	(*TTable)(unsafe.Pointer(pNew)).FnTabRef = uint32(1)
	(*TTable)(unsafe.Pointer(pNew)).FnCol = (*TTable)(unsafe.Pointer(pTab)).FnCol
	nAlloc = (int32((*TTable)(unsafe.Pointer(pNew)).FnCol)-int32(1))/int32(8)*int32(8) + int32(8)
	(*TTable)(unsafe.Pointer(pNew)).FaCol = _sqlite3DbMallocZero(tls, db, uint64(uint32(12)*uint32(nAlloc)))
	(*TTable)(unsafe.Pointer(pNew)).FzName = _sqlite3MPrintf(tls, db, __ccgo_ts+12313, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
	if !((*TTable)(unsafe.Pointer(pNew)).FaCol != 0) || !((*TTable)(unsafe.Pointer(pNew)).FzName != 0) {
		goto exit_begin_add_column
	}
	libc.Xmemcpy(tls, (*TTable)(unsafe.Pointer(pNew)).FaCol, (*TTable)(unsafe.Pointer(pTab)).FaCol, uint32(12)*uint32((*TTable)(unsafe.Pointer(pNew)).FnCol))
	i = 0
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pNew)).FnCol)) {
			break
		}
		pCol = (*TTable)(unsafe.Pointer(pNew)).FaCol + uintptr(i)*12
		(*TColumn)(unsafe.Pointer(pCol)).FzCnName = _sqlite3DbStrDup(tls, db, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)
		(*TColumn)(unsafe.Pointer(pCol)).FhName = _sqlite3StrIHash(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)
		goto _1
	_1:
		;
		i = i + 1
	}
	(*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pNew + 44))).FpDfltList = _sqlite3ExprListDup(tls, db, (*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pTab + 44))).FpDfltList, 0)
	(*TTable)(unsafe.Pointer(pNew)).FpSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema
	(*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pNew + 44))).FaddColOffset = (*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pTab + 44))).FaddColOffset
	goto exit_begin_add_column
exit_begin_add_column:
	;
	_sqlite3SrcListDelete(tls, db, pSrc)
	return
}

// C documentation
//
//	/*
//	** This function is called by the parser upon parsing an
//	**
//	**     ALTER TABLE pSrc DROP COLUMN pName
//	**
//	** statement. Argument pSrc contains the possibly qualified name of the
//	** table being edited, and token pName the name of the column to drop.
//	*/
func _sqlite3AlterDropColumn(tls *libc.TLS, pParse uintptr, pSrc uintptr, pName uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var addr, i, iCol, iColPos, iCur, iDb, iPos, nField, reg, regOut, regRec, v2 int32
	var aff int8
	var db, pPk, pTab, v, zCol, zDb, v1 uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, aff, db, i, iCol, iColPos, iCur, iDb, iPos, nField, pPk, pTab, reg, regOut, regRec, v, zCol, zDb, v1, v2
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database containing pTab ("main" etc.) */
	zCol = uintptr(0)                          /* Index of column zCol in pTab->aCol[] */
	/* Look up the table being altered. */
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto exit_drop_column
	}
	pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pSrc+8)
	if !(pTab != 0) {
		goto exit_drop_column
	}
	/* Make sure this is not an attempt to ALTER a view, virtual table or
	 ** system table. */
	if SQLITE_OK != _isAlterableTable(tls, pParse, pTab) {
		goto exit_drop_column
	}
	if SQLITE_OK != _isRealTable(tls, pParse, pTab, int32(1)) {
		goto exit_drop_column
	}
	/* Find the index of the column being dropped. */
	zCol = _sqlite3NameFromToken(tls, db, pName)
	if zCol == uintptr(0) {
		goto exit_drop_column
	}
	iCol = _sqlite3ColumnIndex(tls, pTab, zCol)
	if iCol < 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12424, libc.VaList(bp+8, pName))
		goto exit_drop_column
	}
	/* Do not allow the user to drop a PRIMARY KEY column or a column
	 ** constrained by a UNIQUE constraint.  */
	if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FcolFlags)&(libc.Int32FromInt32(COLFLAG_PRIMKEY)|libc.Int32FromInt32(COLFLAG_UNIQUE)) != 0 {
		if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 {
			v1 = __ccgo_ts + 12809
		} else {
			v1 = __ccgo_ts + 7048
		}
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12821, libc.VaList(bp+8, v1, zCol))
		goto exit_drop_column
	}
	/* Do not allow the number of columns to go to zero */
	if int32((*TTable)(unsafe.Pointer(pTab)).FnCol) <= int32(1) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12849, libc.VaList(bp+8, zCol))
		goto exit_drop_column
	}
	/* Edit the sqlite_schema table */
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	/* Invoke the authorization callback. */
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ALTER_TABLE), zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, zCol) != 0 {
		goto exit_drop_column
	}
	_renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iDb == int32(1)), __ccgo_ts+1711, 0)
	_renameFixQuotes(tls, pParse, zDb, libc.BoolInt32(iDb == int32(1)))
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+12897, libc.VaList(bp+8, zDb, iDb, iCol, (*TTable)(unsafe.Pointer(pTab)).FzName))
	/* Drop and reload the database schema. */
	_renameReloadSchema(tls, pParse, iDb, uint16(INITFLAG_AlterDrop))
	_renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iDb == int32(1)), __ccgo_ts+13018, int32(1))
	/* Edit rows of table on disk */
	if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 {
		pPk = uintptr(0)
		nField = 0
		v = _sqlite3GetVdbe(tls, pParse)
		v1 = pParse + 44
		v2 = *(*int32)(unsafe.Pointer(v1))
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		iCur = v2
		_sqlite3OpenTable(tls, pParse, iCur, iDb, pTab, int32(OP_OpenWrite))
		addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iCur)
		v1 = pParse + 48
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		v2 = *(*int32)(unsafe.Pointer(v1))
		reg = v2
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCur, reg)
			**(**int32)(__ccgo_up(pParse + 48)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
		} else {
			pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
			**(**int32)(__ccgo_up(pParse + 48)) += int32((*TIndex)(unsafe.Pointer(pPk)).FnColumn)
			i = 0
			for {
				if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
					break
				}
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iCur, i, reg+i+int32(1))
				goto _6
			_6:
				;
				i = i + 1
			}
			nField = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)
		}
		v1 = pParse + 48
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		v2 = *(*int32)(unsafe.Pointer(v1))
		regRec = v2
		i = 0
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			if i != iCol && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 {
				if pPk != 0 {
					iPos = _sqlite3TableColumnToIndex(tls, pPk, i)
					iColPos = _sqlite3TableColumnToIndex(tls, pPk, iCol)
					if iPos < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) {
						goto _9
					}
					regOut = reg + int32(1) + iPos - libc.BoolInt32(iPos > iColPos)
				} else {
					regOut = reg + int32(1) + nField
				}
				if i == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regOut)
				} else {
					aff = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).Faffinity
					if int32(aff) == int32(SQLITE_AFF_REAL) {
						(**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).Faffinity = int8(SQLITE_AFF_NUMERIC)
					}
					_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, i, regOut)
					(**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).Faffinity = aff
				}
				nField = nField + 1
			}
			goto _9
		_9:
			;
			i = i + 1
		}
		if nField == 0 {
			/* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */
			(*TParse)(unsafe.Pointer(pParse)).FnMem = (*TParse)(unsafe.Pointer(pParse)).FnMem + 1
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, reg+int32(1))
			nField = int32(1)
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), reg+int32(1), nField, regRec)
		if pPk != 0 {
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iCur, regRec, reg+int32(1), int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol))
		} else {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iCur, regRec, reg)
		}
		_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_SAVEPOSITION))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iCur, addr+int32(1))
		_sqlite3VdbeJumpHere(tls, v, addr)
	}
	goto exit_drop_column
exit_drop_column:
	;
	_sqlite3DbFree(tls, db, zCol)
	_sqlite3SrcListDelete(tls, db, pSrc)
}

// C documentation
//
//	/*
//	** Generate bytecode for one of:
//	**
//	**  (1)   ALTER TABLE pSrc DROP CONSTRAINT pCons
//	**  (2)   ALTER TABLE pSrc ALTER pCol DROP NOT NULL
//	**
//	** One of pCons and pCol must be NULL and the other non-null.
//	*/
func _sqlite3AlterDropConstraint(tls *libc.TLS, pParse uintptr, pSrc uintptr, pCons uintptr, pCol uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var db, pTab, z, zArg uintptr
	var _ /* iCol at bp+8 */ int32
	var _ /* iDb at bp+0 */ int32
	var _ /* zDb at bp+4 */ uintptr
	_, _, _, _ = db, pTab, z, zArg
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pTab = uintptr(0)
	**(**int32)(__ccgo_up(bp)) = 0
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	zArg = uintptr(0)
	pTab = _alterFindTable(tls, pParse, pSrc, bp, bp+4, libc.BoolInt32(pCons != uintptr(0)))
	if !(pTab != 0) {
		return
	}
	if pCons != 0 {
		z = _sqlite3NameFromToken(tls, db, pCons)
		zArg = _sqlite3MPrintf(tls, db, __ccgo_ts+13142, libc.VaList(bp+24, z))
		_sqlite3DbFree(tls, db, z)
	} else {
		if _alterFindCol(tls, pParse, pTab, pCol, bp+8) != 0 {
			return
		}
		zArg = _sqlite3MPrintf(tls, db, __ccgo_ts+6506, libc.VaList(bp+24, **(**int32)(__ccgo_up(bp + 8))))
	}
	/* Edit the SQL for the named table. */
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+13145, libc.VaList(bp+24, **(**uintptr)(__ccgo_up(bp + 4)), zArg, (*TTable)(unsafe.Pointer(pTab)).FzName))
	_sqlite3DbFree(tls, db, zArg)
	/* Finally, reload the database schema. */
	_renameReloadSchema(tls, pParse, **(**int32)(__ccgo_up(bp)), uint16(INITFLAG_AlterDropCons))
}

// C documentation
//
//	/*
//	** This function is called after an "ALTER TABLE ... ADD" statement
//	** has been parsed. Argument pColDef contains the text of the new
//	** column definition.
//	**
//	** The Table structure pParse->pNewTable was extended to include
//	** the new column during parsing.
//	*/
func _sqlite3AlterFinishAddColumn(tls *libc.TLS, pParse uintptr, pColDef uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var db, pCol, pDflt, pNew, pTab, v, zCol, zDb, zEnd, zTab, v1 uintptr
	var iDb, r1, rc int32
	var _ /* pVal at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, iDb, pCol, pDflt, pNew, pTab, r1, rc, v, zCol, zDb, zEnd, zTab, v1 /* Temporary registers */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return
	}
	pNew = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pNew)).FpSchema)
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	zTab = (*TTable)(unsafe.Pointer(pNew)).FzName + 16 /* Skip the "sqlite_altertab_" prefix on the name */
	pCol = (*TTable)(unsafe.Pointer(pNew)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(pNew)).FnCol)-int32(1))*12
	pDflt = _sqlite3ColumnExpr(tls, pNew, pCol)
	pTab = _sqlite3FindTable(tls, db, zTab, zDb)
	/* Invoke the authorization callback. */
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ALTER_TABLE), zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0)) != 0 {
		return
	}
	/* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
	 ** If there is a NOT NULL constraint, then the default value for the
	 ** column must not be NULL.
	 */
	if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+11506, 0)
		return
	}
	if (*TTable)(unsafe.Pointer(pNew)).FpIndex != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+11538, 0)
		return
	}
	if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) == 0 {
		/* If the default value for the new column was specified with a
		 ** literal NULL, then set pDflt to 0. This simplifies checking
		 ** for an SQL NULL default below.
		 */
		if pDflt != 0 && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pDflt)).FpLeft)).Fop) == int32(TK_NULL) {
			pDflt = uintptr(0)
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0 && (*(*struct {
			FaddColOffset int32
			FpFKey        uintptr
			FpDfltList    uintptr
		})(unsafe.Pointer(pNew + 44))).FpFKey != 0 && pDflt != 0 {
			_sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+11565)
		}
		if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) != 0 && !(pDflt != 0) {
			_sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+11624)
		}
		/* Ensure the default expression is something that sqlite3ValueFromExpr()
		 ** can handle (i.e. not CURRENT_TIME etc.)
		 */
		if pDflt != 0 {
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			rc = _sqlite3ValueFromExpr(tls, db, pDflt, uint8(SQLITE_UTF8), uint8(SQLITE_AFF_BLOB), bp)
			if rc != SQLITE_OK {
				return
			}
			if !(**(**uintptr)(__ccgo_up(bp)) != 0) {
				_sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+11677)
			}
			_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up(bp)))
		}
	} else {
		if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_STORED) != 0 {
			_sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+11723)
		}
	}
	/* Modify the CREATE TABLE statement. */
	zCol = _sqlite3DbStrNDup(tls, db, (*TToken)(unsafe.Pointer(pColDef)).Fz, uint64((*TToken)(unsafe.Pointer(pColDef)).Fn))
	if zCol != 0 {
		zEnd = zCol + uintptr((*TToken)(unsafe.Pointer(pColDef)).Fn-uint32(1))
		for zEnd > zCol && (int32(**(**int8)(__ccgo_up(zEnd))) == int32(';') || int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zEnd)))])&int32(0x01) != 0) {
			v1 = zEnd
			zEnd = zEnd - 1
			**(**int8)(__ccgo_up(v1)) = int8('\000')
		}
		/* substr() operations on characters, but addColOffset is in bytes. So we
		 ** have to use printf() to translate between these units: */
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+11750, libc.VaList(bp+16, zDb, (*(*struct {
			FaddColOffset int32
			FpFKey        uintptr
			FpDfltList    uintptr
		})(unsafe.Pointer(pNew + 44))).FaddColOffset, zCol, (*(*struct {
			FaddColOffset int32
			FpFKey        uintptr
			FpDfltList    uintptr
		})(unsafe.Pointer(pNew + 44))).FaddColOffset, zTab))
		_sqlite3DbFree(tls, db, zCol)
	}
	v = _sqlite3GetVdbe(tls, pParse)
	if v != 0 {
		/* Make sure the schema version is at least 3.  But do not upgrade
		 ** from less than 3 to 4, as that will corrupt any preexisting DESC
		 ** index.
		 */
		r1 = _sqlite3GetTempReg(tls, pParse)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_ReadCookie), iDb, r1, int32(BTREE_FILE_FORMAT))
		_sqlite3VdbeUsesBtree(tls, v, iDb)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), r1, -int32(2))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_IfPos), r1, _sqlite3VdbeCurrentAddr(tls, v)+int32(2))
		_sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_FILE_FORMAT), int32(3))
		_sqlite3ReleaseTempReg(tls, pParse, r1)
		/* Reload the table definition */
		_renameReloadSchema(tls, pParse, iDb, uint16(INITFLAG_AlterAdd))
		/* Verify that constraints are still satisfied */
		if (*TTable)(unsafe.Pointer(pNew)).FpCheck != uintptr(0) || int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) != 0 && int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 || (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Strict) != uint32(0) {
			_sqlite3NestedParse(tls, pParse, __ccgo_ts+11896, libc.VaList(bp+16, zTab, zDb))
		}
	}
}

// C documentation
//
//	/*
//	** Register built-in functions used to help implement ALTER TABLE
//	*/
func _sqlite3AlterFunctions(tls *libc.TLS) {
	_sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aAlterTableFuncs)), int32(libc.Uint32FromInt64(360)/libc.Uint32FromInt64(40)))
}

// C documentation
//
//	/*
//	** Handles the following parser reduction:
//	**
//	**  cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
//	*/
func _sqlite3AlterRenameColumn(tls *libc.TLS, pParse uintptr, pSrc uintptr, pOld uintptr, pNew uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var bQuote, iCol, iSchema int32
	var db, pTab, zDb, zNew, zOld uintptr
	_, _, _, _, _, _, _, _ = bQuote, db, iCol, iSchema, pTab, zDb, zNew, zOld
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Index of column being renamed */
	zOld = uintptr(0)                          /* Old column name */
	zNew = uintptr(0)                          /* True to quote the new name */
	/* Locate the table to be altered */
	pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pSrc+8)
	if !(pTab != 0) {
		goto exit_rename_column
	}
	/* Cannot alter a system table */
	if SQLITE_OK != _isAlterableTable(tls, pParse, pTab) {
		goto exit_rename_column
	}
	if SQLITE_OK != _isRealTable(tls, pParse, pTab, 0) {
		goto exit_rename_column
	}
	/* Which schema holds the table to be altered */
	iSchema = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iSchema)*16))).FzDbSName
	/* Invoke the authorization callback. */
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ALTER_TABLE), zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0)) != 0 {
		goto exit_rename_column
	}
	/* Make sure the old name really is a column name in the table to be
	 ** altered.  Set iCol to be the index of the column being renamed */
	zOld = _sqlite3NameFromToken(tls, db, pOld)
	if !(zOld != 0) {
		goto exit_rename_column
	}
	iCol = _sqlite3ColumnIndex(tls, pTab, zOld)
	if iCol < 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12424, libc.VaList(bp+8, pOld))
		goto exit_rename_column
	}
	/* Ensure the schema contains no double-quoted strings */
	_renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iSchema == int32(1)), __ccgo_ts+1711, 0)
	_renameFixQuotes(tls, pParse, zDb, libc.BoolInt32(iSchema == int32(1)))
	/* Do the rename operation using a recursive UPDATE statement that
	 ** uses the sqlite_rename_column() SQL function to compute the new
	 ** CREATE statement text for the sqlite_schema table.
	 */
	_sqlite3MayAbort(tls, pParse)
	zNew = _sqlite3NameFromToken(tls, db, pNew)
	if !(zNew != 0) {
		goto exit_rename_column
	}
	bQuote = int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up((*TToken)(unsafe.Pointer(pNew)).Fz)))]) & int32(0x80)
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+12445, libc.VaList(bp+8, zDb, zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, iCol, zNew, bQuote, libc.BoolInt32(iSchema == int32(1)), (*TTable)(unsafe.Pointer(pTab)).FzName))
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+12627, libc.VaList(bp+8, zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, iCol, zNew, bQuote))
	/* Drop and reload the database schema. */
	_renameReloadSchema(tls, pParse, iSchema, uint16(INITFLAG_AlterRename))
	_renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iSchema == int32(1)), __ccgo_ts+11455, int32(1))
	goto exit_rename_column
exit_rename_column:
	;
	_sqlite3SrcListDelete(tls, db, pSrc)
	_sqlite3DbFree(tls, db, zOld)
	_sqlite3DbFree(tls, db, zNew)
	return
}

// C documentation
//
//	/*
//	** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
//	** command.
//	*/
func _sqlite3AlterRenameTable(tls *libc.TLS, pParse uintptr, pSrc uintptr, pName uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var db, pTab, pVTab, v, zDb, zName, zTabName, v2 uintptr
	var i, iDb, nTabName, v1 int32
	_, _, _, _, _, _, _, _, _, _, _, _ = db, i, iDb, nTabName, pTab, pVTab, v, zDb, zName, zTabName, v1, v2 /* Table being renamed */
	zName = uintptr(0)                                                                                      /* NULL-terminated version of pName */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pVTab = uintptr(0) /* Non-zero if this is a v-tab with an xRename() */
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto exit_rename_table
	}
	pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pSrc+8)
	if !(pTab != 0) {
		goto exit_rename_table
	}
	iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	/* Get a NULL terminated version of the new table name. */
	zName = _sqlite3NameFromToken(tls, db, pName)
	if !(zName != 0) {
		goto exit_rename_table
	}
	/* Check that a table or index named 'zName' does not already exist
	 ** in database iDb. If so, this is an error.
	 */
	if _sqlite3FindTable(tls, db, zName, zDb) != 0 || _sqlite3FindIndex(tls, db, zName, zDb) != 0 || _sqlite3IsShadowTableOf(tls, db, pTab, zName) != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+10535, libc.VaList(bp+8, zName))
		goto exit_rename_table
	}
	/* Make sure it is not a system table being altered, or a reserved name
	 ** that the table is being renamed to.
	 */
	if SQLITE_OK != _isAlterableTable(tls, pParse, pTab) {
		goto exit_rename_table
	}
	if SQLITE_OK != _sqlite3CheckObjectName(tls, pParse, zName, __ccgo_ts+10594, zName) {
		goto exit_rename_table
	}
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+10600, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
		goto exit_rename_table
	}
	/* Invoke the authorization callback. */
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ALTER_TABLE), zDb, (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0)) != 0 {
		goto exit_rename_table
	}
	if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 {
		goto exit_rename_table
	}
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		pVTab = _sqlite3GetVTable(tls, db, pTab)
		if (*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer((*TVTable)(unsafe.Pointer(pVTab)).FpVtab)).FpModule)).FxRename == uintptr(0) {
			pVTab = uintptr(0)
		}
	}
	/* Begin a transaction for database iDb. Then modify the schema cookie
	 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
	 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
	 ** nested SQL may raise an exception.  */
	v = _sqlite3GetVdbe(tls, pParse)
	if v == uintptr(0) {
		goto exit_rename_table
	}
	_sqlite3MayAbort(tls, pParse)
	/* figure out how many UTF-8 characters are in zName */
	zTabName = (*TTable)(unsafe.Pointer(pTab)).FzName
	nTabName = _sqlite3Utf8CharLen(tls, zTabName, -int32(1))
	/* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
	 ** the schema to use the new table name.  */
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+10627, libc.VaList(bp+8, zDb, zDb, zTabName, zName, libc.BoolInt32(iDb == int32(1)), zTabName))
	/* Update the tbl_name and name columns of the sqlite_schema table
	 ** as required.  */
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+10811, libc.VaList(bp+8, zDb, zName, zName, zName, nTabName, zTabName))
	/* If the sqlite_sequence table exists in this database, then update
	 ** it with the new table name.
	 */
	if _sqlite3FindTable(tls, db, __ccgo_ts+11116, zDb) != 0 {
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+11132, libc.VaList(bp+8, zDb, zName, (*TTable)(unsafe.Pointer(pTab)).FzName))
	}
	/* If the table being renamed is not itself part of the temp database,
	 ** edit view and trigger definitions within the temp database
	 ** as required.  */
	if iDb != int32(1) {
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+11190, libc.VaList(bp+8, zDb, zTabName, zName, zTabName, zDb, zName))
	}
	/* If this is a virtual table, invoke the xRename() function if
	 ** one is defined. The xRename() callback will modify the names
	 ** of any resources used by the v-table implementation (including other
	 ** SQLite tables) that are identified by the name of the virtual table.
	 */
	if pVTab != 0 {
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		i = v1
		_sqlite3VdbeLoadString(tls, v, i, zName)
		_sqlite3VdbeAddOp4(tls, v, int32(OP_VRename), i, 0, 0, pVTab, -int32(12))
	}
	_renameReloadSchema(tls, pParse, iDb, uint16(INITFLAG_AlterRename))
	_renameTestSchema(tls, pParse, zDb, libc.BoolInt32(iDb == int32(1)), __ccgo_ts+11455, 0)
	goto exit_rename_table
exit_rename_table:
	;
	_sqlite3SrcListDelete(tls, db, pSrc)
	_sqlite3DbFree(tls, db, zName)
}

// C documentation
//
//	/*
//	** Prepare a statement of the form:
//	**
//	**   ALTER TABLE pSrc ALTER pCol SET NOT NULL
//	*/
func _sqlite3AlterSetNotNull(tls *libc.TLS, pParse uintptr, pSrc uintptr, pCol uintptr, pFirst uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var nCons int32
	var pCons, pTab uintptr
	var _ /* iCol at bp+0 */ int32
	var _ /* iDb at bp+4 */ int32
	var _ /* zDb at bp+8 */ uintptr
	_, _, _ = nCons, pCons, pTab
	pTab = uintptr(0)
	**(**int32)(__ccgo_up(bp)) = 0
	**(**int32)(__ccgo_up(bp + 4)) = 0
	**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
	pCons = uintptr(0)
	nCons = 0
	/* Look up the table being altered. */
	pTab = _alterFindTable(tls, pParse, pSrc, bp+4, bp+8, 0)
	if !(pTab != 0) {
		return
	}
	/* Find the column being altered. */
	if _alterFindCol(tls, pParse, pTab, pCol, bp) != 0 {
		return
	}
	/* Find the length in bytes of the constraint definition */
	pCons = (*TToken)(unsafe.Pointer(pFirst)).Fz
	nCons = _alterRtrimConstraint(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pCons, int32((*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz)-int32(pCons))
	/* Search for a constraint violation. Throw an exception if one is found. */
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+13268, libc.VaList(bp+24, int32(SQLITE_CONSTRAINT), **(**uintptr)(__ccgo_up(bp + 8)), (*TTable)(unsafe.Pointer(pTab)).FzName, int32((*TToken)(unsafe.Pointer(pCol)).Fn), (*TToken)(unsafe.Pointer(pCol)).Fz))
	/* Edit the SQL for the named table. */
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+13349, libc.VaList(bp+24, **(**uintptr)(__ccgo_up(bp + 8)), **(**int32)(__ccgo_up(bp)), nCons, pCons, **(**int32)(__ccgo_up(bp)), (*TTable)(unsafe.Pointer(pTab)).FzName))
	/* Finally, reload the database schema. */
	_renameReloadSchema(tls, pParse, **(**int32)(__ccgo_up(bp + 4)), uint16(INITFLAG_AlterDropCons))
}

// C documentation
//
//	/*
//	** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
//	** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
//	** arrays. The contents of sqlite_stat4 are used to populate the
//	** Index.aSample[] arrays.
//	**
//	** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
//	** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined
//	** during compilation and the sqlite_stat4 table is present, no data is
//	** read from it.
//	**
//	** If SQLITE_ENABLE_STAT4 was defined during compilation and the
//	** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
//	** returned. However, in this case, data is read from the sqlite_stat1
//	** table (if it is present) before returning.
//	**
//	** If an OOM error occurs, this function always sets db->mallocFailed.
//	** This means if the caller does not care about other errors, the return
//	** code may be ignored.
//	*/
func _sqlite3AnalysisLoad(tls *libc.TLS, db uintptr, iDb int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, pIdx, pIdx1, pIdx2, pSchema, pStat1, pTab, zSql, v3 uintptr
	var rc, v5 int32
	var _ /* sInfo at bp+0 */ TanalysisInfo
	_, _, _, _, _, _, _, _, _, _, _ = i, pIdx, pIdx1, pIdx2, pSchema, pStat1, pTab, rc, zSql, v3, v5
	rc = SQLITE_OK
	pSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema
	/* Clear any prior statistics */
	i = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst
	for {
		if !(i != 0) {
			break
		}
		pTab = (*THashElem)(unsafe.Pointer(i)).Fdata
		**(**Tu32)(__ccgo_up(pTab + 28)) &= uint32(^libc.Int32FromInt32(TF_HasStat1))
		goto _1
	_1:
		;
		i = (*THashElem)(unsafe.Pointer(i)).Fnext
	}
	i = (*THash)(unsafe.Pointer(pSchema + 24)).Ffirst
	for {
		if !(i != 0) {
			break
		}
		pIdx = (*THashElem)(unsafe.Pointer(i)).Fdata
		libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(0), 7, 0x80)
		_sqlite3DeleteIndexSamples(tls, db, pIdx)
		(*TIndex)(unsafe.Pointer(pIdx)).FaSample = uintptr(0)
		goto _2
	_2:
		;
		i = (*THashElem)(unsafe.Pointer(i)).Fnext
	}
	/* Load new statistics out of the sqlite_stat1 table */
	(**(**TanalysisInfo)(__ccgo_up(bp))).Fdb = db
	(**(**TanalysisInfo)(__ccgo_up(bp))).FzDatabase = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	v3 = _sqlite3FindTable(tls, db, __ccgo_ts+14050, (**(**TanalysisInfo)(__ccgo_up(bp))).FzDatabase)
	pStat1 = v3
	if v3 != 0 && int32((*TTable)(unsafe.Pointer(pStat1)).FeTabType) == TABTYP_NORM {
		zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+14423, libc.VaList(bp+16, (**(**TanalysisInfo)(__ccgo_up(bp))).FzDatabase))
		if zSql == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		} else {
			rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_analysisLoader), bp, uintptr(0))
			_sqlite3DbFree(tls, db, zSql)
		}
	}
	/* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
	i = (*THash)(unsafe.Pointer(pSchema + 24)).Ffirst
	for {
		if !(i != 0) {
			break
		}
		pIdx1 = (*THashElem)(unsafe.Pointer(i)).Fdata
		if !(int32(uint32(*(*uint16)(unsafe.Pointer(pIdx1 + 56))&0x80>>7)) != 0) {
			_sqlite3DefaultRowEst(tls, pIdx1)
		}
		goto _4
	_4:
		;
		i = (*THashElem)(unsafe.Pointer(i)).Fnext
	}
	/* Load the statistics from the sqlite_stat4 table. */
	if rc == SQLITE_OK {
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable + 1
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0)
		rc = _loadStat4(tls, db, (**(**TanalysisInfo)(__ccgo_up(bp))).FzDatabase)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable - 1
		if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0 {
			v5 = 0
		} else {
			v5 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue)
		}
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(v5)
	}
	i = (*THash)(unsafe.Pointer(pSchema + 24)).Ffirst
	for {
		if !(i != 0) {
			break
		}
		pIdx2 = (*THashElem)(unsafe.Pointer(i)).Fdata
		Xsqlite3_free(tls, (*TIndex)(unsafe.Pointer(pIdx2)).FaiRowEst)
		(*TIndex)(unsafe.Pointer(pIdx2)).FaiRowEst = uintptr(0)
		goto _6
	_6:
		;
		i = (*THashElem)(unsafe.Pointer(i)).Fnext
	}
	if rc == int32(SQLITE_NOMEM) {
		_sqlite3OomFault(tls, db)
	}
	return rc
}

/************** End of analyze.c *********************************************/
/************** Begin file attach.c ******************************************/
/*
** 2003 April 6
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to implement the ATTACH and DETACH commands.
 */
/* #include "sqliteInt.h" */

// C documentation
//
//	/*
//	** Generate code for the ANALYZE command.  The parser calls this routine
//	** when it recognizes an ANALYZE command.
//	**
//	**        ANALYZE                            -- 1
//	**        ANALYZE  <database>                -- 2
//	**        ANALYZE  ?<database>.?<tablename>  -- 3
//	**
//	** Form 1 causes all indices in all attached databases to be analyzed.
//	** Form 2 analyzes all indices the single database named.
//	** Form 3 analyzes all indices associated with the named table.
//	*/
func _sqlite3Analyze(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pIdx, pTab, v, z, zDb, v4 uintptr
	var i, iDb, v2 int32
	var v3 bool
	var _ /* pTableName at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _ = db, i, iDb, pIdx, pTab, v, z, zDb, v2, v3, v4
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	/* Read the database schema. If an error occurs, leave an error message
	 ** and code in pParse and return NULL. */
	if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) {
		return
	}
	if pName1 == uintptr(0) {
		/* Form 1:  Analyze everything */
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			if i == int32(1) {
				goto _1
			} /* Do not analyze the TEMP database */
			_analyzeDatabase(tls, pParse, i)
			goto _1
		_1:
			;
			i = i + 1
		}
	} else {
		if v3 = (*TToken)(unsafe.Pointer(pName2)).Fn == uint32(0); v3 {
			v2 = _sqlite3FindDb(tls, db, pName1)
			iDb = v2
		}
		if v3 && v2 >= 0 {
			/* Analyze the schema named as the argument */
			_analyzeDatabase(tls, pParse, iDb)
		} else {
			/* Form 3: Analyze the table or index named as an argument */
			iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp)
			if iDb >= 0 {
				if (*TToken)(unsafe.Pointer(pName2)).Fn != 0 {
					v4 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
				} else {
					v4 = uintptr(0)
				}
				zDb = v4
				z = _sqlite3NameFromToken(tls, db, **(**uintptr)(__ccgo_up(bp)))
				if z != 0 {
					v4 = _sqlite3FindIndex(tls, db, z, zDb)
					pIdx = v4
					if v4 != uintptr(0) {
						_analyzeTable(tls, pParse, (*TIndex)(unsafe.Pointer(pIdx)).FpTable, pIdx)
					} else {
						v4 = _sqlite3LocateTable(tls, pParse, uint32(0), z, zDb)
						pTab = v4
						if v4 != uintptr(0) {
							_analyzeTable(tls, pParse, pTab, uintptr(0))
						}
					}
					_sqlite3DbFree(tls, db, z)
				}
			}
		}
	}
	if v3 = int32((*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec) == 0; v3 {
		v4 = _sqlite3GetVdbe(tls, pParse)
		v = v4
	}
	if v3 && v4 != uintptr(0) {
		_sqlite3VdbeAddOp0(tls, v, int32(OP_Expire))
	}
}

// C documentation
//
//	/*
//	** pArray is a pointer to an array of objects. Each object in the
//	** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
//	** to extend the array so that there is space for a new object at the end.
//	**
//	** When this function is called, *pnEntry contains the current size of
//	** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
//	** in total).
//	**
//	** If the realloc() is successful (i.e. if no OOM condition occurs), the
//	** space allocated for the new object is zeroed, *pnEntry updated to
//	** reflect the new size of the array and a pointer to the new allocation
//	** returned. *pIdx is set to the index of the new array entry in this case.
//	**
//	** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
//	** unchanged and a copy of pArray returned.
//	*/
func _sqlite3ArrayAllocate(tls *libc.TLS, db uintptr, pArray uintptr, szEntry int32, pnEntry uintptr, pIdx uintptr) (r uintptr) {
	var n, sz Tsqlite3_int64
	var pNew, z uintptr
	var v1 int32
	var v2 int64
	_, _, _, _, _, _ = n, pNew, sz, z, v1, v2
	v1 = **(**int32)(__ccgo_up(pnEntry))
	**(**int32)(__ccgo_up(pIdx)) = v1
	n = int64(v1)
	if n&(n-int64(1)) == 0 {
		if n == 0 {
			v2 = int64(1)
		} else {
			v2 = int64(2) * n
		}
		sz = v2
		pNew = _sqlite3DbRealloc(tls, db, pArray, uint64(sz*int64(szEntry)))
		if pNew == uintptr(0) {
			**(**int32)(__ccgo_up(pIdx)) = -int32(1)
			return pArray
		}
		pArray = pNew
	}
	z = pArray
	libc.Xmemset(tls, z+uintptr(n*int64(szEntry)), 0, uint32(szEntry))
	**(**int32)(__ccgo_up(pnEntry)) = **(**int32)(__ccgo_up(pnEntry)) + 1
	return pArray
}

// C documentation
//
//	/*
//	** The pExpr should be a TK_COLUMN expression.  The table referred to
//	** is in pTabList or else it is the NEW or OLD table of a trigger.
//	** Check to see if it is OK to read this particular column.
//	**
//	** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
//	** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
//	** then generate an error.
//	*/
func _sqlite3AuthRead(tls *libc.TLS, pParse uintptr, pExpr uintptr, pSchema uintptr, pTabList uintptr) {
	var iCol, iDb, iSrc int32
	var pTab, zCol uintptr
	_, _, _, _, _ = iCol, iDb, iSrc, pTab, zCol
	pTab = uintptr(0) /* Index of column in table */
	iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSchema)
	if iDb < 0 {
		/* An attempt to read a column out of a subquery or other
		 ** temporary table. */
		return
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_TRIGGER) {
		pTab = (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab
	} else {
		iSrc = 0
		for {
			if !(iSrc < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) {
				break
			}
			if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(iSrc)*48))).FiCursor {
				pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(iSrc)*48))).FpSTab
				break
			}
			goto _1
		_1:
			;
			iSrc = iSrc + 1
		}
	}
	iCol = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
	if pTab == uintptr(0) {
		return
	}
	if iCol >= 0 {
		zCol = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName
	} else {
		if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) >= 0 {
			zCol = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*12))).FzCnName
		} else {
			zCol = __ccgo_ts + 9414
		}
	}
	if int32(SQLITE_IGNORE) == _sqlite3AuthReadCol(tls, pParse, (*TTable)(unsafe.Pointer(pTab)).FzName, zCol, iDb) {
		(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_NULL)
	}
}

// C documentation
//
//	/*
//	** Invoke the authorization callback for permission to read column zCol from
//	** table zTab in database zDb. This function assumes that an authorization
//	** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
//	**
//	** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
//	** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
//	** is treated as SQLITE_DENY. In this case an error is left in pParse.
//	*/
func _sqlite3AuthReadCol(tls *libc.TLS, pParse uintptr, zTab uintptr, zCol uintptr, iDb int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, z, zDb uintptr
	var rc int32
	_, _, _, _ = db, rc, z, zDb
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb                                                     /* Database handle */
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName /* Auth callback return code */
	if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 {
		return SQLITE_OK
	}
	rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxAuth})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpAuthArg, int32(SQLITE_READ), zTab, zCol, zDb, (*TParse)(unsafe.Pointer(pParse)).FzAuthContext)
	if rc == int32(SQLITE_DENY) {
		z = Xsqlite3_mprintf(tls, __ccgo_ts+14849, libc.VaList(bp+8, zTab, zCol))
		if (*Tsqlite3)(unsafe.Pointer(db)).FnDb > int32(2) || iDb != 0 {
			z = Xsqlite3_mprintf(tls, __ccgo_ts+14855, libc.VaList(bp+8, zDb, z))
		}
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14861, libc.VaList(bp+8, z))
		(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_AUTH)
	} else {
		if rc != int32(SQLITE_IGNORE) && rc != SQLITE_OK {
			_sqliteAuthBadReturnCode(tls, pParse)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Load all automatic extensions.
//	**
//	** If anything goes wrong, set an error in the database connection.
//	*/
func _sqlite3AutoLoadExtensions(tls *libc.TLS, db uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var go1, rc, v2 int32
	var i Tu32
	var mutex, pThunk uintptr
	var xInit Tsqlite3_loadext_entry
	var v3 bool
	var _ /* zErrmsg at bp+0 */ uintptr
	_, _, _, _, _, _, _, _ = go1, i, mutex, pThunk, rc, xInit, v2, v3
	go1 = int32(1)
	if _sqlite3Autoext.FnExt == uint32(0) {
		/* Common case: early out without every having to acquire a mutex */
		return
	}
	i = uint32(0)
	for {
		if !(go1 != 0) {
			break
		}
		mutex = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN))
		pThunk = uintptr(unsafe.Pointer(&_sqlite3Apis))
		Xsqlite3_mutex_enter(tls, mutex)
		if i >= _sqlite3Autoext.FnExt {
			xInit = uintptr(0)
			go1 = 0
		} else {
			xInit = **(**uintptr)(__ccgo_up(_sqlite3Autoext.FaExt + uintptr(i)*4))
		}
		Xsqlite3_mutex_leave(tls, mutex)
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		if v3 = xInit != 0; v3 {
			v2 = (*(*func(*libc.TLS, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xInit})))(tls, db, bp, pThunk)
			rc = v2
		}
		if v3 && v2 != 0 {
			_sqlite3ErrorWithMsg(tls, db, rc, __ccgo_ts+18964, libc.VaList(bp+16, **(**uintptr)(__ccgo_up(bp))))
			go1 = 0
		}
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp)))
		goto _1
	_1:
		;
		i = i + 1
	}
}

/************** End of loadext.c *********************************************/
/************** Begin file pragma.c ******************************************/
/*
** 2003 April 6
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to implement the PRAGMA command.
 */
/* #include "sqliteInt.h" */

/***************************************************************************
** The "pragma.h" include file is an automatically generated file that
** that includes the PragType_XXXX macro definitions and the aPragmaName[]
** object.  This ensures that the aPragmaName[] table is arranged in
** lexicographical order to facility a binary search of the pragma name.
** Do not edit pragma.h directly.  Edit and rerun the script in at
** ../tool/mkpragmatab.tcl. */
/************** Include pragma.h in the middle of pragma.c *******************/
/************** Begin file pragma.h ******************************************/
/* DO NOT EDIT!
** This file is automatically generated by the script at
** ../tool/mkpragmatab.tcl.  To update the set of pragmas, edit
** that script and rerun it.
 */

/* The various pragma types */

/* Property flags associated with various pragma. */

// C documentation
//
//	/*
//	** This routine generates code that will initialize all of the
//	** register used by the autoincrement tracker.
//	*/
func _sqlite3AutoincrementBegin(tls *libc.TLS, pParse uintptr) {
	var aOp, db, p, pDb, v uintptr
	var memId int32
	_, _, _, _, _, _ = aOp, db, memId, p, pDb, v /* Information about an AUTOINCREMENT */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb   /* Register holding max rowid */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* VDBE under construction */
	/* This routine is never called during trigger-generation.  It is
	 ** only called from the top-level */
	/* We failed long ago if this is not so */
	p = (*TParse)(unsafe.Pointer(pParse)).FpAinc
	for {
		if !(p != 0) {
			break
		}
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TAutoincInfo)(unsafe.Pointer(p)).FiDb)*16
		memId = (*TAutoincInfo)(unsafe.Pointer(p)).FregCtr
		_sqlite3OpenTable(tls, pParse, 0, (*TAutoincInfo)(unsafe.Pointer(p)).FiDb, (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).FpSeqTab, int32(OP_OpenRead))
		_sqlite3VdbeLoadString(tls, v, memId-int32(1), (*TTable)(unsafe.Pointer((*TAutoincInfo)(unsafe.Pointer(p)).FpTab)).FzName)
		aOp = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(48)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_autoInc)), _iLn1)
		if aOp == uintptr(0) {
			break
		}
		(**(**TVdbeOp)(__ccgo_up(aOp))).Fp2 = memId
		(**(**TVdbeOp)(__ccgo_up(aOp))).Fp3 = memId + int32(2)
		(**(**TVdbeOp)(__ccgo_up(aOp + 2*20))).Fp3 = memId
		(**(**TVdbeOp)(__ccgo_up(aOp + 3*20))).Fp1 = memId - int32(1)
		(**(**TVdbeOp)(__ccgo_up(aOp + 3*20))).Fp3 = memId
		(**(**TVdbeOp)(__ccgo_up(aOp + 3*20))).Fp5 = uint16(SQLITE_JUMPIFNULL)
		(**(**TVdbeOp)(__ccgo_up(aOp + 4*20))).Fp2 = memId + int32(1)
		(**(**TVdbeOp)(__ccgo_up(aOp + 5*20))).Fp3 = memId
		(**(**TVdbeOp)(__ccgo_up(aOp + 6*20))).Fp1 = memId
		(**(**TVdbeOp)(__ccgo_up(aOp + 7*20))).Fp2 = memId + int32(2)
		(**(**TVdbeOp)(__ccgo_up(aOp + 7*20))).Fp1 = memId
		(**(**TVdbeOp)(__ccgo_up(aOp + 10*20))).Fp2 = memId
		if (*TParse)(unsafe.Pointer(pParse)).FnTab == 0 {
			(*TParse)(unsafe.Pointer(pParse)).FnTab = int32(1)
		}
		goto _1
	_1:
		;
		p = (*TAutoincInfo)(unsafe.Pointer(p)).FpNext
	}
}

// C documentation
//
//	/*
//	** Generate VDBE code for a BEGIN statement.
//	*/
func _sqlite3BeginTransaction(tls *libc.TLS, pParse uintptr, type1 int32) {
	var db, pBt, v uintptr
	var eTxnType, i int32
	_, _, _, _, _ = db, eTxnType, i, pBt, v
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_TRANSACTION), __ccgo_ts+17325, uintptr(0), uintptr(0)) != 0 {
		return
	}
	v = _sqlite3GetVdbe(tls, pParse)
	if !(v != 0) {
		return
	}
	if type1 != int32(TK_DEFERRED) {
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
			if pBt != 0 && _sqlite3BtreeIsReadonly(tls, pBt) != 0 {
				eTxnType = 0 /* Read txn */
			} else {
				if type1 == int32(TK_EXCLUSIVE) {
					eTxnType = int32(2) /* Exclusive txn */
				} else {
					eTxnType = int32(1) /* Write txn */
				}
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Transaction), i, eTxnType)
			_sqlite3VdbeUsesBtree(tls, v, i)
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	_sqlite3VdbeAddOp0(tls, v, int32(OP_AutoCommit))
}

// C documentation
//
//	/*
//	** This is called by the parser when it sees a CREATE TRIGGER statement
//	** up to the point of the BEGIN before the trigger actions.  A Trigger
//	** structure is generated based on the information available and stored
//	** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
//	** sqlite3FinishTrigger() function is called to complete the trigger
//	** construction process.
//	*/
func _sqlite3BeginTrigger(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr, tr_tm int32, op int32, pColumns uintptr, pTableName uintptr, pWhen uintptr, isTemp int32, noErr int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var code, iDb, iTabDb, v4 int32
	var db, pTab, pTrigger, zDb, zDbTrig, zName, v1 uintptr
	var _ /* pName at bp+0 */ uintptr
	var _ /* sFix at bp+4 */ TDbFixer
	_, _, _, _, _, _, _, _, _, _, _ = code, db, iDb, iTabDb, pTab, pTrigger, zDb, zDbTrig, zName, v1, v4
	pTrigger = uintptr(0)                      /* Table that the trigger fires off of */
	zName = uintptr(0)                         /* Name of the trigger */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* State vector for the DB fixer */
	/* pName1->z might be NULL, but not pName1 itself */
	if isTemp != 0 {
		/* If TEMP was specified, then the trigger name may not be qualified. */
		if (*TToken)(unsafe.Pointer(pName2)).Fn > uint32(0) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23070, 0)
			goto trigger_cleanup
		}
		iDb = int32(1)
		**(**uintptr)(__ccgo_up(bp)) = pName1
	} else {
		/* Figure out the db that the trigger will be created in */
		iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp)
		if iDb < 0 {
			goto trigger_cleanup
		}
	}
	if !(pTableName != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto trigger_cleanup
	}
	/* A long-standing parser bug is that this syntax was allowed:
	 **
	 **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
	 **                                                 ^^^^^^^^
	 **
	 ** To maintain backwards compatibility, ignore the database
	 ** name on pTableName if we are reparsing out of the schema table
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 && iDb != int32(1) {
		_sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(pTableName + 8 + 44)))
		*(*uintptr)(unsafe.Pointer(pTableName + 8 + 44)) = uintptr(0)
	}
	/* If the trigger name was unqualified, and the table is a temp table,
	 ** then set iDb to 1 to create the trigger in the temporary database.
	 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
	 ** exist, the error is caught by the block below.
	 */
	pTab = _sqlite3SrcListLookup(tls, pParse, pTableName)
	if int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 && (*TToken)(unsafe.Pointer(pName2)).Fn == uint32(0) && pTab != 0 && (*TTable)(unsafe.Pointer(pTab)).FpSchema == (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema {
		iDb = int32(1)
	}
	/* Ensure the table name matches database name and that the table exists */
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto trigger_cleanup
	}
	_sqlite3FixInit(tls, bp+4, pParse, iDb, __ccgo_ts+23116, **(**uintptr)(__ccgo_up(bp)))
	if _sqlite3FixSrcList(tls, bp+4, pTableName) != 0 {
		goto trigger_cleanup
	}
	pTab = _sqlite3SrcListLookup(tls, pParse, pTableName)
	if !(pTab != 0) {
		/* The table does not exist. */
		goto trigger_orphan_error
	}
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23124, 0)
		goto trigger_orphan_error
	}
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Shadow) != uint32(0) && _sqlite3ReadOnlyShadowTables(tls, db) != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23165, 0)
		goto trigger_orphan_error
	}
	/* Check that the trigger name is not reserved and that no trigger of the
	 ** specified name exists */
	zName = _sqlite3NameFromToken(tls, db, **(**uintptr)(__ccgo_up(bp)))
	if zName == uintptr(0) {
		goto trigger_cleanup
	}
	if _sqlite3CheckObjectName(tls, pParse, zName, __ccgo_ts+23116, (*TTable)(unsafe.Pointer(pTab)).FzName) != 0 {
		goto trigger_cleanup
	}
	if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
		if _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema+40, zName) != 0 {
			if !(noErr != 0) {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23205, libc.VaList(bp+64, **(**uintptr)(__ccgo_up(bp))))
			} else {
				_sqlite3CodeVerifySchema(tls, pParse, iDb)
			}
			goto trigger_cleanup
		}
	}
	/* NB: The SQLITE_ALLOW_TRIGGERS_ON_SYSTEM_TABLES compile-time option is
	 ** experimental and unsupported. Do not use it unless understand the
	 ** implications and you cannot get by without this capability. */
	/* Do not create a trigger on a system table */
	if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+7973, int32(7)) == 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23231, 0)
		goto trigger_cleanup
	}
	/* INSTEAD of triggers are only for views and views only support INSTEAD
	 ** of triggers.
	 */
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) && tr_tm != int32(TK_INSTEAD) {
		if tr_tm == int32(TK_BEFORE) {
			v1 = __ccgo_ts + 23269
		} else {
			v1 = __ccgo_ts + 23276
		}
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23282, libc.VaList(bp+64, v1, pTableName+8))
		goto trigger_orphan_error
	}
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VIEW)) && tr_tm == int32(TK_INSTEAD) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23319, libc.VaList(bp+64, pTableName+8))
		goto trigger_orphan_error
	}
	if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
		iTabDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
		code = int32(SQLITE_CREATE_TRIGGER)
		zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iTabDb)*16))).FzDbSName
		if isTemp != 0 {
			v1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FzDbSName
		} else {
			v1 = zDb
		}
		zDbTrig = v1
		if iTabDb == int32(1) || isTemp != 0 {
			code = int32(SQLITE_CREATE_TEMP_TRIGGER)
		}
		if _sqlite3AuthCheck(tls, pParse, code, zName, (*TTable)(unsafe.Pointer(pTab)).FzName, zDbTrig) != 0 {
			goto trigger_cleanup
		}
		if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iTabDb == int32(1) {
			v1 = __ccgo_ts + 7981
		} else {
			v1 = __ccgo_ts + 7501
		}
		if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_INSERT), v1, uintptr(0), zDb) != 0 {
			goto trigger_cleanup
		}
	}
	/* INSTEAD OF triggers can only appear on views and BEFORE triggers
	 ** cannot appear on views.  So we might as well translate every
	 ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
	 ** elsewhere.
	 */
	if tr_tm == int32(TK_INSTEAD) {
		tr_tm = int32(TK_BEFORE)
	}
	/* Build the Trigger object */
	pTrigger = _sqlite3DbMallocZero(tls, db, uint64(36))
	if pTrigger == uintptr(0) {
		goto trigger_cleanup
	}
	(*TTrigger)(unsafe.Pointer(pTrigger)).FzName = zName
	zName = uintptr(0)
	(*TTrigger)(unsafe.Pointer(pTrigger)).Ftable = _sqlite3DbStrDup(tls, db, (*(*TSrcItem)(unsafe.Pointer(pTableName + 8))).FzName)
	(*TTrigger)(unsafe.Pointer(pTrigger)).FpSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema
	(*TTrigger)(unsafe.Pointer(pTrigger)).FpTabSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema
	(*TTrigger)(unsafe.Pointer(pTrigger)).Fop = uint8(op)
	if tr_tm == int32(TK_BEFORE) {
		v4 = int32(TRIGGER_BEFORE)
	} else {
		v4 = int32(TRIGGER_AFTER)
	}
	(*TTrigger)(unsafe.Pointer(pTrigger)).Ftr_tm = uint8(v4)
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		_sqlite3RenameTokenRemap(tls, pParse, (*TTrigger)(unsafe.Pointer(pTrigger)).Ftable, (*(*TSrcItem)(unsafe.Pointer(pTableName + 8))).FzName)
		(*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen = pWhen
		pWhen = uintptr(0)
	} else {
		(*TTrigger)(unsafe.Pointer(pTrigger)).FpWhen = _sqlite3ExprDup(tls, db, pWhen, int32(EXPRDUP_REDUCE))
	}
	(*TTrigger)(unsafe.Pointer(pTrigger)).FpColumns = pColumns
	pColumns = uintptr(0)
	(*TParse)(unsafe.Pointer(pParse)).FpNewTrigger = pTrigger
	goto trigger_cleanup
trigger_cleanup:
	;
	_sqlite3DbFree(tls, db, zName)
	_sqlite3SrcListDelete(tls, db, pTableName)
	_sqlite3IdListDelete(tls, db, pColumns)
	_sqlite3ExprDelete(tls, db, pWhen)
	if !((*TParse)(unsafe.Pointer(pParse)).FpNewTrigger != 0) {
		_sqlite3DeleteTrigger(tls, db, pTrigger)
	} else {
	}
	return
	goto trigger_orphan_error
trigger_orphan_error:
	;
	if int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb) == int32(1) {
		/* Ticket #3810.
		 ** Normally, whenever a table is dropped, all associated triggers are
		 ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
		 ** and the table is dropped by a different database connection, the
		 ** trigger is not visible to the database connection that does the
		 ** drop so the trigger cannot be dropped.  This results in an
		 ** "orphaned trigger" - a trigger whose associated table is missing.
		 **
		 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
		 */
		libc.SetBitFieldPtr8Uint32(db+176+8, libc.Uint32FromInt32(1), 0, 0x1)
	}
	goto trigger_cleanup
}

// C documentation
//
//	/*
//	** Clear the i-th bit.
//	**
//	** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
//	** that BitvecClear can use to rebuilt its hash table.
//	*/
func _sqlite3BitvecClear(tls *libc.TLS, p uintptr, i Tu32, pBuf uintptr) {
	var aiValues, v1 uintptr
	var bin, h Tu32
	var j uint32
	_, _, _, _, _ = aiValues, bin, h, j, v1
	if p == uintptr(0) {
		return
	}
	i = i - 1
	for (*TBitvec)(unsafe.Pointer(p)).FiDivisor != 0 {
		bin = i / (*TBitvec)(unsafe.Pointer(p)).FiDivisor
		i = i % (*TBitvec)(unsafe.Pointer(p)).FiDivisor
		p = **(**uintptr)(__ccgo_up(p + 12 + uintptr(bin)*4))
		if !(p != 0) {
			return
		}
	}
	if (*TBitvec)(unsafe.Pointer(p)).FiSize <= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(1)*libc.Uint32FromInt32(BITVEC_SZELEM) {
		v1 = p + 12 + uintptr(i/uint32(BITVEC_SZELEM))
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^int32(uint8(libc.Int32FromInt32(1)<<(i&uint32(libc.Int32FromInt32(BITVEC_SZELEM)-libc.Int32FromInt32(1))))))
	} else {
		aiValues = pBuf
		libc.Xmemcpy(tls, aiValues, p+12, uint32(500))
		libc.Xmemset(tls, p+12, 0, uint32(500))
		(*TBitvec)(unsafe.Pointer(p)).FnSet = uint32(0)
		j = uint32(0)
		for {
			if !(j < (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)) {
				break
			}
			if **(**Tu32)(__ccgo_up(aiValues + uintptr(j)*4)) != 0 && **(**Tu32)(__ccgo_up(aiValues + uintptr(j)*4)) != i+uint32(1) {
				h = (**(**Tu32)(__ccgo_up(aiValues + uintptr(j)*4)) - libc.Uint32FromInt32(1)) * libc.Uint32FromInt32(1) % ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4))
				(*TBitvec)(unsafe.Pointer(p)).FnSet = (*TBitvec)(unsafe.Pointer(p)).FnSet + 1
				for **(**Tu32)(__ccgo_up(p + 12 + uintptr(h)*4)) != 0 {
					h = h + 1
					if h >= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4) {
						h = uint32(0)
					}
				}
				**(**Tu32)(__ccgo_up(p + 12 + uintptr(h)*4)) = **(**Tu32)(__ccgo_up(aiValues + uintptr(j)*4))
			}
			goto _2
		_2:
			;
			j = j + 1
		}
	}
}

// C documentation
//
//	/*
//	** Set the i-th bit.  Return 0 on success and an error code if
//	** anything goes wrong.
//	**
//	** This routine might cause sub-bitmaps to be allocated.  Failing
//	** to get the memory needed to hold the sub-bitmap is the only
//	** that can go wrong with an insert, assuming p and i are valid.
//	**
//	** The calling function must ensure that p is a valid Bitvec object
//	** and that the value for "i" is within range of the Bitvec object.
//	** Otherwise the behavior is undefined.
//	*/
func _sqlite3BitvecSet(tls *libc.TLS, p uintptr, i Tu32) (r int32) {
	var aiValues, v1 uintptr
	var bin, h, v2 Tu32
	var j uint32
	var rc int32
	_, _, _, _, _, _, _ = aiValues, bin, h, j, rc, v1, v2
	if p == uintptr(0) {
		return SQLITE_OK
	}
	i = i - 1
	for (*TBitvec)(unsafe.Pointer(p)).FiSize > (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(1)*libc.Uint32FromInt32(BITVEC_SZELEM) && (*TBitvec)(unsafe.Pointer(p)).FiDivisor != 0 {
		bin = i / (*TBitvec)(unsafe.Pointer(p)).FiDivisor
		i = i % (*TBitvec)(unsafe.Pointer(p)).FiDivisor
		if **(**uintptr)(__ccgo_up(p + 12 + uintptr(bin)*4)) == uintptr(0) {
			**(**uintptr)(__ccgo_up(p + 12 + uintptr(bin)*4)) = _sqlite3BitvecCreate(tls, (*TBitvec)(unsafe.Pointer(p)).FiDivisor)
			if **(**uintptr)(__ccgo_up(p + 12 + uintptr(bin)*4)) == uintptr(0) {
				return int32(SQLITE_NOMEM)
			}
		}
		p = **(**uintptr)(__ccgo_up(p + 12 + uintptr(bin)*4))
	}
	if (*TBitvec)(unsafe.Pointer(p)).FiSize <= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(1)*libc.Uint32FromInt32(BITVEC_SZELEM) {
		v1 = p + 12 + uintptr(i/uint32(BITVEC_SZELEM))
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(1)<<(i&uint32(libc.Int32FromInt32(BITVEC_SZELEM)-libc.Int32FromInt32(1))))
		return SQLITE_OK
	}
	v2 = i
	i = i + 1
	h = v2 * libc.Uint32FromInt32(1) % ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4))
	/* if there wasn't a hash collision, and this doesn't */
	/* completely fill the hash, then just add it without */
	/* worrying about sub-dividing and re-hashing. */
	if !(**(**Tu32)(__ccgo_up(p + 12 + uintptr(h)*4)) != 0) {
		if (*TBitvec)(unsafe.Pointer(p)).FnSet < (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)-libc.Uint32FromInt32(1) {
			goto bitvec_set_end
		} else {
			goto bitvec_set_rehash
		}
	}
	/* there was a collision, check to see if it's already */
	/* in hash, if not, try to find a spot for it */
	for cond := true; cond; cond = **(**Tu32)(__ccgo_up(p + 12 + uintptr(h)*4)) != 0 {
		if **(**Tu32)(__ccgo_up(p + 12 + uintptr(h)*4)) == i {
			return SQLITE_OK
		}
		h = h + 1
		if h >= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4) {
			h = uint32(0)
		}
	}
	/* we didn't find it in the hash.  h points to the first */
	/* available free spot. check to see if this is going to */
	/* make our hash too "full".  */
	goto bitvec_set_rehash
bitvec_set_rehash:
	;
	if (*TBitvec)(unsafe.Pointer(p)).FnSet >= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)/libc.Uint32FromInt32(2) {
		aiValues = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(500))
		if aiValues == uintptr(0) {
			return int32(SQLITE_NOMEM)
		} else {
			libc.Xmemcpy(tls, aiValues, p+12, uint32(500))
			libc.Xmemset(tls, p+12, 0, uint32(500))
			(*TBitvec)(unsafe.Pointer(p)).FiDivisor = (*TBitvec)(unsafe.Pointer(p)).FiSize / ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4))
			if (*TBitvec)(unsafe.Pointer(p)).FiSize%((libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)) != uint32(0) {
				(*TBitvec)(unsafe.Pointer(p)).FiDivisor = (*TBitvec)(unsafe.Pointer(p)).FiDivisor + 1
			}
			if (*TBitvec)(unsafe.Pointer(p)).FiDivisor < (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(1)*libc.Uint32FromInt32(BITVEC_SZELEM) {
				(*TBitvec)(unsafe.Pointer(p)).FiDivisor = (libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(1) * libc.Uint32FromInt32(BITVEC_SZELEM)
			}
			rc = _sqlite3BitvecSet(tls, p, i)
			j = uint32(0)
			for {
				if !(j < (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)) {
					break
				}
				if **(**Tu32)(__ccgo_up(aiValues + uintptr(j)*4)) != 0 {
					rc = rc | _sqlite3BitvecSet(tls, p, **(**Tu32)(__ccgo_up(aiValues + uintptr(j)*4)))
				}
				goto _3
			_3:
				;
				j = j + 1
			}
			_sqlite3DbFree(tls, uintptr(0), aiValues)
			return rc
		}
	}
	goto bitvec_set_end
bitvec_set_end:
	;
	(*TBitvec)(unsafe.Pointer(p)).FnSet = (*TBitvec)(unsafe.Pointer(p)).FnSet + 1
	**(**Tu32)(__ccgo_up(p + 12 + uintptr(h)*4)) = i
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Check to see if the i-th bit is set.  Return true or false.
//	** If p is NULL (if the bitmap has not been created) or if
//	** i is out of range, then return false.
//	*/
func _sqlite3BitvecTestNotNull(tls *libc.TLS, p uintptr, i Tu32) (r int32) {
	var bin, h, v1 Tu32
	_, _, _ = bin, h, v1
	i = i - 1
	if i >= (*TBitvec)(unsafe.Pointer(p)).FiSize {
		return 0
	}
	for (*TBitvec)(unsafe.Pointer(p)).FiDivisor != 0 {
		bin = i / (*TBitvec)(unsafe.Pointer(p)).FiDivisor
		i = i % (*TBitvec)(unsafe.Pointer(p)).FiDivisor
		p = **(**uintptr)(__ccgo_up(p + 12 + uintptr(bin)*4))
		if !(p != 0) {
			return 0
		}
	}
	if (*TBitvec)(unsafe.Pointer(p)).FiSize <= (libc.Uint32FromInt32(BITVEC_SZ)-libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4))/libc.Uint32FromInt64(4)*libc.Uint32FromInt64(4)/libc.Uint32FromInt64(1)*libc.Uint32FromInt32(BITVEC_SZELEM) {
		return libc.BoolInt32(int32(**(**Tu8)(__ccgo_up(p + 12 + uintptr(i/uint32(BITVEC_SZELEM)))))&(int32(1)<<(i&uint32(libc.Int32FromInt32(BITVEC_SZELEM)-libc.Int32FromInt32(1)))) != 0)
	} else {
		v1 = i
		i = i + 1
		h = v1 * libc.Uint32FromInt32(1) % ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4))
		for **(**Tu32)(__ccgo_up(p + 12 + uintptr(h)*4)) != 0 {
			if **(**Tu32)(__ccgo_up(p + 12 + uintptr(h)*4)) == i {
				return int32(1)
			}
			h = (h + uint32(1)) % ((libc.Uint32FromInt32(BITVEC_SZ) - libc.Uint32FromInt32(3)*libc.Uint32FromInt64(4)) / libc.Uint32FromInt64(4) * libc.Uint32FromInt64(4) / libc.Uint32FromInt64(4))
		}
		return 0
	}
	return r
}

// C documentation
//
//	/*
//	** Compare two blobs.  Return negative, zero, or positive if the first
//	** is less than, equal to, or greater than the second, respectively.
//	** If one blob is a prefix of the other, then the shorter is the lessor.
//	*/
func _sqlite3BlobCompare(tls *libc.TLS, pB1 uintptr, pB2 uintptr) (r int32) {
	var c, n1, n2, v1 int32
	_, _, _, _ = c, n1, n2, v1
	n1 = (*TMem)(unsafe.Pointer(pB1)).Fn
	n2 = (*TMem)(unsafe.Pointer(pB2)).Fn
	/* It is possible to have a Blob value that has some non-zero content
	 ** followed by zero content.  But that only comes up for Blobs formed
	 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
	 ** sqlite3MemCompare(). */
	if (int32((*TMem)(unsafe.Pointer(pB1)).Fflags)|int32((*TMem)(unsafe.Pointer(pB2)).Fflags))&int32(MEM_Zero) != 0 {
		if int32((*TMem)(unsafe.Pointer(pB1)).Fflags)&int32((*TMem)(unsafe.Pointer(pB2)).Fflags)&int32(MEM_Zero) != 0 {
			return *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pB1)).Fu)) - *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pB2)).Fu))
		} else {
			if int32((*TMem)(unsafe.Pointer(pB1)).Fflags)&int32(MEM_Zero) != 0 {
				if !(_isAllZero(tls, (*TMem)(unsafe.Pointer(pB2)).Fz, (*TMem)(unsafe.Pointer(pB2)).Fn) != 0) {
					return -int32(1)
				}
				return *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pB1)).Fu)) - n2
			} else {
				if !(_isAllZero(tls, (*TMem)(unsafe.Pointer(pB1)).Fz, (*TMem)(unsafe.Pointer(pB1)).Fn) != 0) {
					return +libc.Int32FromInt32(1)
				}
				return n1 - *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pB2)).Fu))
			}
		}
	}
	if n1 > n2 {
		v1 = n2
	} else {
		v1 = n1
	}
	c = libc.Xmemcmp(tls, (*TMem)(unsafe.Pointer(pB1)).Fz, (*TMem)(unsafe.Pointer(pB2)).Fz, uint32(v1))
	if c != 0 {
		return c
	}
	return n1 - n2
}

/* The following two functions are used only within testcase() to prove
** test coverage.  These functions do no exist for production builds.
** We must use separate SQLITE_NOINLINE functions here, since otherwise
** optimizer code movement causes gcov to become very confused.
 */

// C documentation
//
//	/*
//	** Copy the complete content of pBtFrom into pBtTo.  A transaction
//	** must be active for both files.
//	**
//	** The size of file pTo may be reduced by this operation. If anything
//	** goes wrong, the transaction on pTo is rolled back. If successful, the
//	** transaction is committed before returning.
//	*/
func _sqlite3BtreeCopyFile(tls *libc.TLS, pTo uintptr, pFrom uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var pFd, v1 uintptr
	var rc int32
	var _ /* b at bp+0 */ Tsqlite3_backup
	var _ /* nByte at bp+56 */ Ti64
	_, _, _ = pFd, rc, v1
	_sqlite3BtreeEnter(tls, pTo)
	_sqlite3BtreeEnter(tls, pFrom)
	pFd = _sqlite3PagerFile(tls, _sqlite3BtreePager(tls, pTo))
	if (*Tsqlite3_file)(unsafe.Pointer(pFd)).FpMethods != 0 {
		**(**Ti64)(__ccgo_up(bp + 56)) = int64(_sqlite3BtreeGetPageSize(tls, pFrom)) * int64(_sqlite3BtreeLastPage(tls, pFrom))
		rc = _sqlite3OsFileControl(tls, pFd, int32(SQLITE_FCNTL_OVERWRITE), bp+56)
		if rc == int32(SQLITE_NOTFOUND) {
			rc = SQLITE_OK
		}
		if rc != 0 {
			goto copy_finished
		}
	}
	/* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
	 ** to 0. This is used by the implementations of sqlite3_backup_step()
	 ** and sqlite3_backup_finish() to detect that they are being called
	 ** from this function, not directly by the user.
	 */
	libc.Xmemset(tls, bp, 0, uint32(52))
	(**(**Tsqlite3_backup)(__ccgo_up(bp))).FpSrcDb = (*TBtree)(unsafe.Pointer(pFrom)).Fdb
	(**(**Tsqlite3_backup)(__ccgo_up(bp))).FpSrc = pFrom
	(**(**Tsqlite3_backup)(__ccgo_up(bp))).FpDest = pTo
	(**(**Tsqlite3_backup)(__ccgo_up(bp))).FiNext = uint32(1)
	/* 0x7FFFFFFF is the hard limit for the number of pages in a database
	 ** file. By passing this as the number of pages to copy to
	 ** sqlite3_backup_step(), we can guarantee that the copy finishes
	 ** within a single call (unless an error occurs). The assert() statement
	 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
	 ** or an error code.  */
	Xsqlite3_backup_step(tls, bp, int32(0x7FFFFFFF))
	rc = Xsqlite3_backup_finish(tls, bp)
	if rc == SQLITE_OK {
		v1 = (*TBtree)(unsafe.Pointer(pTo)).FpBt + 24
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(BTS_PAGESIZE_FIXED))
	} else {
		_sqlite3PagerClearCache(tls, _sqlite3BtreePager(tls, (**(**Tsqlite3_backup)(__ccgo_up(bp))).FpDest))
	}
	goto copy_finished
copy_finished:
	;
	_sqlite3BtreeLeave(tls, pFrom)
	_sqlite3BtreeLeave(tls, pTo)
	return rc
}

/************** End of backup.c **********************************************/
/************** Begin file vdbemem.c *****************************************/
/*
** 2004 May 26
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains code use to manipulate "Mem" structure.  A "Mem"
** stores a single value in the VDBE.  Mem is an opaque structure visible
** only within the VDBE.  Interface routines refer to a Mem using the
** name sqlite_value
 */
/* #include "sqliteInt.h" */
/* #include "vdbeInt.h" */

/* True if X is a power of two.  0 is considered a power of two here.
** In other words, return true if X has at most one bit set.
 */

// C documentation
//
//	/*
//	** The first argument, pCur, is a cursor opened on some b-tree. Count the
//	** number of entries in the b-tree and write the result to *pnEntry.
//	**
//	** SQLITE_OK is returned if the operation is successfully executed.
//	** Otherwise, if an error is encountered (i.e. an IO error or database
//	** corruption) an SQLite error code is returned.
//	*/
func _sqlite3BtreeCount(tls *libc.TLS, db uintptr, pCur uintptr, pnEntry uintptr) (r int32) {
	var iIdx, rc int32
	var nEntry Ti64
	var pPage uintptr
	_, _, _, _ = iIdx, nEntry, pPage, rc
	nEntry = 0 /* Return code */
	rc = _moveToRoot(tls, pCur)
	if rc == int32(SQLITE_EMPTY) {
		**(**Ti64)(__ccgo_up(pnEntry)) = 0
		return SQLITE_OK
	}
	/* Unless an error occurs, the following loop runs one iteration for each
	 ** page in the B-Tree structure (not including overflow pages).
	 */
	for rc == SQLITE_OK && !(libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0) { /* Current page of the b-tree */
		/* If this is a leaf page or the tree is not an int-key tree, then
		 ** this page contains countable entries. Increment the entry counter
		 ** accordingly.
		 */
		pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
		if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 || !((*TMemPage)(unsafe.Pointer(pPage)).FintKey != 0) {
			nEntry = nEntry + int64((*TMemPage)(unsafe.Pointer(pPage)).FnCell)
		}
		/* pPage is a leaf node. This loop navigates the cursor so that it
		 ** points to the first interior cell that it points to the parent of
		 ** the next page in the tree that has not yet been visited. The
		 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
		 ** of the page, or to the number of cells in the page if the next page
		 ** to visit is the right-child of its parent.
		 **
		 ** If all pages in the tree have been visited, return SQLITE_OK to the
		 ** caller.
		 */
		if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 {
			for cond := true; cond; cond = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) >= int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell) {
				if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) == 0 {
					/* All pages of the b-tree have been visited. Return successfully. */
					**(**Ti64)(__ccgo_up(pnEntry)) = nEntry
					return _moveToRoot(tls, pCur)
				}
				_moveToParent(tls, pCur)
			}
			(*TBtCursor)(unsafe.Pointer(pCur)).Fix = (*TBtCursor)(unsafe.Pointer(pCur)).Fix + 1
			pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
		}
		/* Descend to the child node of the cell that the cursor currently
		 ** points at. This is the right-child if (iIdx==pPage->nCell).
		 */
		iIdx = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix)
		if iIdx == int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
			rc = _moveToChild(tls, pCur, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8))))
		} else {
			rc = _moveToChild(tls, pCur, _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iIdx))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iIdx) + 1)))))))
		}
	}
	/* An error has occurred. Return an error code. */
	return rc
}

// C documentation
//
//	/*
//	** Return the size of a BtCursor object in bytes.
//	**
//	** This interfaces is needed so that users of cursors can preallocate
//	** sufficient storage to hold a cursor.  The BtCursor object is opaque
//	** to users so they cannot do the sizeof() themselves - they must call
//	** this routine.
//	*/
func _sqlite3BtreeCursorSize(tls *libc.TLS) (r int32) {
	return int32((libc.Uint32FromInt64(200) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7)))
}

// C documentation
//
//	/*
//	** Delete the entry that the cursor is pointing to.
//	**
//	** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
//	** the cursor is left pointing at an arbitrary location after the delete.
//	** But if that bit is set, then the cursor is left in a state such that
//	** the next call to BtreeNext() or BtreePrev() moves it to the same row
//	** as it would have been on if the call to BtreeDelete() had been omitted.
//	**
//	** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
//	** associated with a single table entry and its indexes.  Only one of those
//	** deletes is considered the "primary" delete.  The primary delete occurs
//	** on a cursor that is not a BTREE_FORDELETE cursor.  All but one delete
//	** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
//	** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
//	** but which might be used by alternative storage engines.
//	*/
func _sqlite3BtreeDelete(tls *libc.TLS, pCur uintptr, flags Tu8) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bPreserve Tu8
	var iCellDepth, iCellIdx, nCell int32
	var n TPgno
	var p, pBt, pCell, pLeaf, pPage, pTmp, v2 uintptr
	var v1 Ti8
	var _ /* info at bp+8 */ TCellInfo
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _ = bPreserve, iCellDepth, iCellIdx, n, nCell, p, pBt, pCell, pLeaf, pPage, pTmp, v1, v2
	p = (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt /* Keep cursor valid.  2 for CURSOR_SKIPNEXT */
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID {
		if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) {
			**(**int32)(__ccgo_up(bp)) = _btreeRestoreCursorPosition(tls, pCur)
			if **(**int32)(__ccgo_up(bp)) != 0 || int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID {
				return **(**int32)(__ccgo_up(bp))
			}
		} else {
			return _sqlite3CorruptError(tls, int32(83091))
		}
	}
	iCellDepth = int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)
	iCellIdx = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix)
	pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
	if int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) <= iCellIdx {
		return _sqlite3CorruptError(tls, int32(83100))
	}
	pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iCellIdx))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iCellIdx) + 1)))))
	if (*TMemPage)(unsafe.Pointer(pPage)).FnFree < 0 && _btreeComputeFreeSpace(tls, pPage) != 0 {
		return _sqlite3CorruptError(tls, int32(83104))
	}
	if pCell < (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx+uintptr((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
		return _sqlite3CorruptError(tls, int32(83107))
	}
	/* If the BTREE_SAVEPOSITION bit is on, then the cursor position must
	 ** be preserved following this delete operation. If the current delete
	 ** will cause a b-tree rebalance, then this is done by saving the cursor
	 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
	 ** returning.
	 **
	 ** If the current delete will not cause a rebalance, then the cursor
	 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
	 ** before or after the deleted entry.
	 **
	 ** The bPreserve value records which path is required:
	 **
	 **    bPreserve==0         Not necessary to save the cursor position
	 **    bPreserve==1         Use CURSOR_REQUIRESEEK to save the cursor position
	 **    bPreserve==2         Cursor won't move.  Set CURSOR_SKIPNEXT.
	 */
	bPreserve = libc.BoolUint8(int32(flags)&int32(BTREE_SAVEPOSITION) != 0)
	if bPreserve != 0 {
		if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) || (*TMemPage)(unsafe.Pointer(pPage)).FnFree+int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxCellSize})))(tls, pPage, pCell))+int32(2) > int32((*TBtShared)(unsafe.Pointer(pBt)).FusableSize*libc.Uint32FromInt32(2)/libc.Uint32FromInt32(3)) || int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) == int32(1) {
			/* A b-tree rebalance will be required after deleting this entry.
			 ** Save the cursor key.  */
			**(**int32)(__ccgo_up(bp)) = _saveCursorKey(tls, pCur)
			if **(**int32)(__ccgo_up(bp)) != 0 {
				return **(**int32)(__ccgo_up(bp))
			}
		} else {
			bPreserve = uint8(2)
		}
	}
	/* If the page containing the entry to delete is not a leaf page, move
	 ** the cursor to the largest entry in the tree that is smaller than
	 ** the entry being deleted. This cell will replace the cell being deleted
	 ** from the internal node. The 'previous' entry is used for this instead
	 ** of the 'next' entry, as the previous entry is always a part of the
	 ** sub-tree headed by the child page of the cell being deleted. This makes
	 ** balancing the tree following the delete operation easier.  */
	if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
		**(**int32)(__ccgo_up(bp)) = _sqlite3BtreePrevious(tls, pCur, 0)
		if **(**int32)(__ccgo_up(bp)) != 0 {
			return **(**int32)(__ccgo_up(bp))
		}
	}
	/* Save the positions of any other cursors open on this table before
	 ** making any modifications.  */
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_Multiple) != 0 {
		**(**int32)(__ccgo_up(bp)) = _saveAllCursors(tls, pBt, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, pCur)
		if **(**int32)(__ccgo_up(bp)) != 0 {
			return **(**int32)(__ccgo_up(bp))
		}
	}
	/* If this is a delete operation to remove a row from a table b-tree,
	 ** invalidate any incrblob cursors open on the row being deleted.  */
	if (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo == uintptr(0) && (*TBtree)(unsafe.Pointer(p)).FhasIncrblobCur != 0 {
		_invalidateIncrblobCursors(tls, p, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey, 0)
	}
	/* Make the page containing the entry to be deleted writable. Then free any
	 ** overflow pages associated with the entry and finally remove the cell
	 ** itself from within the page.  */
	**(**int32)(__ccgo_up(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage)
	if **(**int32)(__ccgo_up(bp)) != 0 {
		return **(**int32)(__ccgo_up(bp))
	}
	(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, pCell, bp+8)
	if uint32((**(**TCellInfo)(__ccgo_up(bp + 8))).FnLocal) != (**(**TCellInfo)(__ccgo_up(bp + 8))).FnPayload {
		**(**int32)(__ccgo_up(bp)) = _clearCellOverflow(tls, pPage, pCell, bp+8)
	} else {
		**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	}
	_dropCell(tls, pPage, iCellIdx, int32((**(**TCellInfo)(__ccgo_up(bp + 8))).FnSize), bp)
	if **(**int32)(__ccgo_up(bp)) != 0 {
		return **(**int32)(__ccgo_up(bp))
	}
	/* If the cell deleted was not located on a leaf page, then the cursor
	 ** is currently pointing to the largest entry in the sub-tree headed
	 ** by the child-page of the cell that was just deleted from an internal
	 ** node. The cell from the leaf node needs to be moved to the internal
	 ** node to replace the deleted cell.  */
	if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
		pLeaf = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
		if (*TMemPage)(unsafe.Pointer(pLeaf)).FnFree < 0 {
			**(**int32)(__ccgo_up(bp)) = _btreeComputeFreeSpace(tls, pLeaf)
			if **(**int32)(__ccgo_up(bp)) != 0 {
				return **(**int32)(__ccgo_up(bp))
			}
		}
		if iCellDepth < int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)-int32(1) {
			n = (*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(pCur + 120 + uintptr(iCellDepth+int32(1))*4)))).Fpgno
		} else {
			n = (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).Fpgno
		}
		pCell = (*TMemPage)(unsafe.Pointer(pLeaf)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pLeaf)).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pLeaf)).FaCellIdx + uintptr(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pLeaf)).FnCell)-int32(1))))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pLeaf)).FaCellIdx + uintptr(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pLeaf)).FnCell)-int32(1))) + 1)))))
		if pCell < (*TMemPage)(unsafe.Pointer(pLeaf)).FaData+4 {
			return _sqlite3CorruptError(tls, int32(83198))
		}
		nCell = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pLeaf)).FxCellSize})))(tls, pLeaf, pCell))
		pTmp = (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace
		**(**int32)(__ccgo_up(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pLeaf)).FpDbPage)
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp)) = _insertCell(tls, pPage, iCellIdx, pCell-uintptr(4), nCell+int32(4), pTmp, n)
		}
		_dropCell(tls, pLeaf, int32((*TMemPage)(unsafe.Pointer(pLeaf)).FnCell)-int32(1), nCell, bp)
		if **(**int32)(__ccgo_up(bp)) != 0 {
			return **(**int32)(__ccgo_up(bp))
		}
	}
	/* Balance the tree. If the entry deleted was located on a leaf page,
	 ** then the cursor still points to that page. In this case the first
	 ** call to balance() repairs the tree, and the if(...) condition is
	 ** never true.
	 **
	 ** Otherwise, if the entry deleted was on an internal node page, then
	 ** pCur is pointing to the leaf page from which a cell was removed to
	 ** replace the cell deleted from the internal node. This is slightly
	 ** tricky as the leaf node may be underfull, and the internal node may
	 ** be either under or overfull. In this case run the balancing algorithm
	 ** on the leaf node first. If the balance proceeds far enough up the
	 ** tree that we can be sure that any problem in the internal node has
	 ** been corrected, so be it. Otherwise, after balancing the leaf node,
	 ** walk the cursor up the tree to the internal node and balance it as
	 ** well.  */
	if (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnFree*int32(3) <= int32((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FusableSize)*int32(2) {
		/* Optimization: If the free space is less than 2/3rds of the page,
		 ** then balance() will always be a no-op.  No need to invoke it. */
		**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	} else {
		**(**int32)(__ccgo_up(bp)) = _balance(tls, pCur)
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) > iCellDepth {
		_releasePageNotNull(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage)
		(*TBtCursor)(unsafe.Pointer(pCur)).FiPage = (*TBtCursor)(unsafe.Pointer(pCur)).FiPage - 1
		for int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) > iCellDepth {
			v2 = pCur + 68
			v1 = *(*Ti8)(unsafe.Pointer(v2))
			*(*Ti8)(unsafe.Pointer(v2)) = *(*Ti8)(unsafe.Pointer(v2)) - 1
			_releasePage(tls, **(**uintptr)(__ccgo_up(pCur + 120 + uintptr(v1)*4)))
		}
		(*TBtCursor)(unsafe.Pointer(pCur)).FpPage = **(**uintptr)(__ccgo_up(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4))
		**(**int32)(__ccgo_up(bp)) = _balance(tls, pCur)
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		if int32(bPreserve) > int32(1) {
			(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_SKIPNEXT)
			if iCellIdx >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
				(*TBtCursor)(unsafe.Pointer(pCur)).FskipNext = -int32(1)
				(*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) - int32(1))
			} else {
				(*TBtCursor)(unsafe.Pointer(pCur)).FskipNext = int32(1)
			}
		} else {
			**(**int32)(__ccgo_up(bp)) = _moveToRoot(tls, pCur)
			if bPreserve != 0 {
				_btreeReleaseAllCursorPages(tls, pCur)
				(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_REQUIRESEEK)
			}
			if **(**int32)(__ccgo_up(bp)) == int32(SQLITE_EMPTY) {
				**(**int32)(__ccgo_up(bp)) = SQLITE_OK
			}
		}
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/* Move the cursor so that it points to an entry in an index table
//	** near the key pIdxKey.   Return a success code.
//	**
//	** If an exact match is not found, then the cursor is always
//	** left pointing at a leaf page which would hold the entry if it
//	** were present.  The cursor might point to an entry that comes
//	** before or after the key.
//	**
//	** An integer is written into *pRes which is the result of
//	** comparing the key with the entry to which the cursor is
//	** pointing.  The meaning of the integer written into
//	** *pRes is as follows:
//	**
//	**     *pRes<0      The cursor is left pointing at an entry that
//	**                  is smaller than pIdxKey or if the table is empty
//	**                  and the cursor is therefore left point to nothing.
//	**
//	**     *pRes==0     The cursor is left pointing at an entry that
//	**                  exactly matches pIdxKey.
//	**
//	**     *pRes>0      The cursor is left pointing at an entry that
//	**                  is larger than pIdxKey.
//	**
//	** The pIdxKey->eqSeen field is set to 1 if there
//	** exists an entry in the table that exactly matches pIdxKey.
//	*/
func _sqlite3BtreeIndexMoveto(tls *libc.TLS, pCur uintptr, pIdxKey uintptr, pRes uintptr) (r int32) {
	var c, c1, idx, lwr, nCell, nOverrun, rc, upr, v1 int32
	var chldPg TPgno
	var pCell, pCellBody, pCellKey, pPage, v3 uintptr
	var xRecordCompare TRecordCompare
	var v10 Ti8
	var v2 bool
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, c1, chldPg, idx, lwr, nCell, nOverrun, pCell, pCellBody, pCellKey, pPage, rc, upr, xRecordCompare, v1, v10, v2, v3
	xRecordCompare = _sqlite3VdbeFindCompare(tls, pIdxKey)
	(*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode = uint8(0)
	/* Check to see if we can skip a lot of work.  Two cases:
	 **
	 **    (1) If the cursor is already pointing to the very last cell
	 **        in the table and the pIdxKey search key is greater than or
	 **        equal to that last cell, then no movement is required.
	 **
	 **    (2) If the cursor is on the last page of the table and the first
	 **        cell on that last page is less than or equal to the pIdxKey
	 **        search key, then we can start the search on the current page
	 **        without needing to go back to root.
	 */
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == CURSOR_VALID && (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).Fleaf != 0 && _cursorOnLastPage(tls, pCur) != 0 {
		if v2 = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) == int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell)-int32(1); v2 {
			v1 = _indexCellCompare(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage, int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix), pIdxKey, xRecordCompare)
			c = v1
		}
		if v2 && v1 <= 0 && int32((*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode) == SQLITE_OK {
			**(**int32)(__ccgo_up(pRes)) = c
			return SQLITE_OK /* Cursor already pointing at the correct spot */
		}
		if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) > 0 && _indexCellCompare(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage, 0, pIdxKey, xRecordCompare) <= 0 && int32((*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode) == SQLITE_OK {
			v3 = pCur + 1
			*(*Tu8)(unsafe.Pointer(v3)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v3))) & ^(libc.Int32FromInt32(BTCF_ValidOvfl) | libc.Int32FromInt32(BTCF_AtLast)))
			if !((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FisInit != 0) {
				return _sqlite3CorruptError(tls, int32(79316))
			}
			goto bypass_moveto_root /* Start search on the current page */
		}
		(*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode = uint8(SQLITE_OK)
	}
	rc = _moveToRoot(tls, pCur)
	if rc != 0 {
		if rc == int32(SQLITE_EMPTY) {
			**(**int32)(__ccgo_up(pRes)) = -int32(1)
			return SQLITE_OK
		}
		return rc
	}
	goto bypass_moveto_root
bypass_moveto_root:
	;
	for {
		pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage /* Pointer to current cell in pPage */
		/* pPage->nCell must be greater than zero. If this is the root-page
		 ** the cursor would have been INVALID above and this for(;;) loop
		 ** not run. If this is not the root-page, then the moveToChild() routine
		 ** would have already detected db corruption. Similarly, pPage must
		 ** be the right kind (index or table) of b-tree page. Otherwise
		 ** a moveToChild() or moveToRoot() call would have detected corruption.  */
		lwr = 0
		upr = int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) - int32(1)
		idx = upr >> int32(1) /* idx = (lwr+upr)/2; */
		for {                 /* Size of the pCell cell in bytes */
			pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaDataOfst + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx) + 1)))))
			/* The maximum supported page-size is 65536 bytes. This means that
			 ** the maximum number of record bytes stored on an index B-Tree
			 ** page is less than 16384 bytes and may be stored as a 2-byte
			 ** varint. This information is used to attempt to avoid parsing
			 ** the entire cell by checking for the cases where the record is
			 ** stored entirely within the b-tree page by inspecting the first
			 ** 2 bytes of the cell.
			 */
			nCell = int32(**(**Tu8)(__ccgo_up(pCell)))
			if nCell <= int32((*TMemPage)(unsafe.Pointer(pPage)).Fmax1bytePayload) {
				/* This branch runs if the record-size field of the cell is a
				 ** single byte varint and the record fits entirely on the main
				 ** b-tree page.  */
				if pCell+uintptr(nCell) >= (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd {
					rc = _sqlite3CorruptError(tls, int32(79375))
					goto moveto_index_finish
				}
				c1 = (*(*func(*libc.TLS, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xRecordCompare})))(tls, nCell, pCell+1, pIdxKey)
			} else {
				if v2 = !(int32(**(**Tu8)(__ccgo_up(pCell + 1)))&libc.Int32FromInt32(0x80) != 0); v2 {
					v1 = nCell&libc.Int32FromInt32(0x7f)<<libc.Int32FromInt32(7) + int32(**(**Tu8)(__ccgo_up(pCell + 1)))
					nCell = v1
				}
				if v2 && v1 <= int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) && pCell+uintptr(nCell) < (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd {
					/* The record-size field is a 2 byte varint and the record
					 ** fits entirely on the main b-tree page.  */
					c1 = (*(*func(*libc.TLS, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xRecordCompare})))(tls, nCell, pCell+2, pIdxKey)
				} else {
					pCellBody = pCell - uintptr((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize)
					nOverrun = int32(18) /* Size of the overrun padding */
					(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, pCellBody, pCur+32)
					nCell = int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey)
					/* True if key size is 2^32 or more */
					/* Invalid key size:  0x80 0x80 0x00 */
					/* Invalid key size:  0x80 0x80 0x01 */
					/* Minimum legal index key size */
					if nCell < int32(2) || uint32(nCell)/(*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FusableSize > (*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FnPage {
						rc = _sqlite3CorruptError(tls, int32(79406))
						goto moveto_index_finish
					}
					pCellKey = _sqlite3Malloc(tls, uint64(nCell)+uint64(nOverrun))
					if pCellKey == uintptr(0) {
						rc = int32(SQLITE_NOMEM)
						goto moveto_index_finish
					}
					(*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(idx)
					rc = _accessPayload(tls, pCur, uint32(0), uint32(nCell), pCellKey, 0)
					libc.Xmemset(tls, pCellKey+uintptr(nCell), 0, uint32(nOverrun)) /* Fix uninit warnings */
					v3 = pCur + 1
					*(*Tu8)(unsafe.Pointer(v3)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v3))) & ^libc.Int32FromInt32(BTCF_ValidOvfl))
					if rc != 0 {
						Xsqlite3_free(tls, pCellKey)
						goto moveto_index_finish
					}
					c1 = _sqlite3VdbeRecordCompare(tls, nCell, pCellKey, pIdxKey)
					Xsqlite3_free(tls, pCellKey)
				}
			}
			if c1 < 0 {
				lwr = idx + int32(1)
			} else {
				if c1 > 0 {
					upr = idx - int32(1)
				} else {
					**(**int32)(__ccgo_up(pRes)) = 0
					rc = SQLITE_OK
					(*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(idx)
					if (*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode != 0 {
						rc = _sqlite3CorruptError(tls, int32(79438))
					}
					goto moveto_index_finish
				}
			}
			if lwr > upr {
				break
			}
			idx = (lwr + upr) >> int32(1) /* idx = (lwr+upr)/2 */
			goto _5
		_5:
		}
		if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 {
			(*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(idx)
			**(**int32)(__ccgo_up(pRes)) = c1
			rc = SQLITE_OK
			goto moveto_index_finish
		}
		if lwr >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
			chldPg = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+int32(8)))
		} else {
			chldPg = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*lwr))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*lwr) + 1))))))
		}
		/* This block is similar to an in-lined version of:
		 **
		 **    pCur->ix = (u16)lwr;
		 **    rc = moveToChild(pCur, chldPg);
		 **    if( rc ) break;
		 */
		(*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0)
		v3 = pCur + 1
		*(*Tu8)(unsafe.Pointer(v3)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v3))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl)))
		if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) >= libc.Int32FromInt32(BTCURSOR_MAX_DEPTH)-libc.Int32FromInt32(1) {
			return _sqlite3CorruptError(tls, int32(79469))
		}
		**(**Tu16)(__ccgo_up(pCur + 72 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*2)) = uint16(lwr)
		**(**uintptr)(__ccgo_up(pCur + 120 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*4)) = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
		(*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(0)
		(*TBtCursor)(unsafe.Pointer(pCur)).FiPage = (*TBtCursor)(unsafe.Pointer(pCur)).FiPage + 1
		rc = _getAndInitPage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBt, chldPg, pCur+116, int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurPagerFlags))
		if rc == SQLITE_OK && (int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell) < int32(1) || int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FintKey) != int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurIntKey)) {
			_releasePage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage)
			rc = _sqlite3CorruptError(tls, int32(79480))
		}
		if rc != 0 {
			v3 = pCur + 68
			*(*Ti8)(unsafe.Pointer(v3)) = *(*Ti8)(unsafe.Pointer(v3)) - 1
			v10 = *(*Ti8)(unsafe.Pointer(v3))
			(*TBtCursor)(unsafe.Pointer(pCur)).FpPage = **(**uintptr)(__ccgo_up(pCur + 120 + uintptr(v10)*4))
			break
		}
		/*
		 ***** End of in-lined moveToChild() call */
		goto _4
	_4:
	}
	goto moveto_index_finish
moveto_index_finish:
	;
	(*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0)
	return rc
}

// C documentation
//
//	/*
//	** Insert a new record into the BTree.  The content of the new record
//	** is described by the pX object.  The pCur cursor is used only to
//	** define what table the record should be inserted into, and is left
//	** pointing at a random location.
//	**
//	** For a table btree (used for rowid tables), only the pX.nKey value of
//	** the key is used. The pX.pKey value must be NULL.  The pX.nKey is the
//	** rowid or INTEGER PRIMARY KEY of the row.  The pX.nData,pData,nZero fields
//	** hold the content of the row.
//	**
//	** For an index btree (used for indexes and WITHOUT ROWID tables), the
//	** key is an arbitrary byte sequence stored in pX.pKey,nKey.  The
//	** pX.pData,nData,nZero fields must be zero.
//	**
//	** If the seekResult parameter is non-zero, then a successful call to
//	** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already
//	** been performed.  In other words, if seekResult!=0 then the cursor
//	** is currently pointing to a cell that will be adjacent to the cell
//	** to be inserted.  If seekResult<0 then pCur points to a cell that is
//	** smaller then (pKey,nKey).  If seekResult>0 then pCur points to a cell
//	** that is larger than (pKey,nKey).
//	**
//	** If seekResult==0, that means pCur is pointing at some unknown location.
//	** In that case, this routine must seek the cursor to the correct insertion
//	** point for (pKey,nKey) before doing the insertion.  For index btrees,
//	** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
//	** key values and pX->aMem can be used instead of pX->pKey to avoid having
//	** to decode the key.
//	*/
func _sqlite3BtreeInsert(tls *libc.TLS, pCur uintptr, pX uintptr, flags int32, seekResult int32) (r int32) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var idx int32
	var newCell, oldCell, p, pPage, v1 uintptr
	var ovfl TPgno
	var v2 Tu16
	var _ /* info at bp+112 */ TCellInfo
	var _ /* info at bp+88 */ TCellInfo
	var _ /* loc at bp+4 */ int32
	var _ /* r at bp+16 */ TUnpackedRecord
	var _ /* rc at bp+0 */ int32
	var _ /* szNew at bp+8 */ int32
	var _ /* x2 at bp+48 */ TBtreePayload
	_, _, _, _, _, _, _, _ = idx, newCell, oldCell, ovfl, p, pPage, v1, v2
	**(**int32)(__ccgo_up(bp + 4)) = seekResult /* -1: before desired location  +1: after */
	**(**int32)(__ccgo_up(bp + 8)) = 0
	p = (*TBtCursor)(unsafe.Pointer(pCur)).FpBtree
	newCell = uintptr(0)
	/* Save the positions of any other cursors open on this table.
	 **
	 ** In some cases, the call to btreeMoveto() below is a no-op. For
	 ** example, when inserting data into a table with auto-generated integer
	 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
	 ** integer key to use. It then calls this function to actually insert the
	 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
	 ** that the cursor is already where it needs to be and returns without
	 ** doing any work. To avoid thwarting these optimizations, it is important
	 ** not to clear the cursor here.
	 */
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_Multiple) != 0 {
		**(**int32)(__ccgo_up(bp)) = _saveAllCursors(tls, (*TBtree)(unsafe.Pointer(p)).FpBt, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, pCur)
		if **(**int32)(__ccgo_up(bp)) != 0 {
			return **(**int32)(__ccgo_up(bp))
		}
		if **(**int32)(__ccgo_up(bp + 4)) != 0 && int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) < 0 {
			/* This can only happen if the schema is corrupt such that there is more
			 ** than one table or index with the same root page as used by the cursor.
			 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
			 ** the schema was loaded. This cannot be asserted though, as a user might
			 ** set the flag, load the schema, and then unset the flag.  */
			return _sqlite3CorruptError(tls, int32(82673))
		}
	}
	/* Ensure that the cursor is not in the CURSOR_FAULT state and that it
	 ** points to a valid cell.
	 */
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) {
		**(**int32)(__ccgo_up(bp)) = _moveToRoot(tls, pCur)
		if **(**int32)(__ccgo_up(bp)) != 0 && **(**int32)(__ccgo_up(bp)) != int32(SQLITE_EMPTY) {
			return **(**int32)(__ccgo_up(bp))
		}
	}
	/* Assert that the caller has been consistent. If this cursor was opened
	 ** expecting an index b-tree, then the caller should be inserting blob
	 ** keys with no associated data. If the cursor was opened expecting an
	 ** intkey table, the caller should be inserting integer keys with a
	 ** blob of associated data.  */
	if (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo == uintptr(0) {
		/* If this is an insert into a table b-tree, invalidate any incrblob
		 ** cursors open on the row being replaced */
		if (*TBtree)(unsafe.Pointer(p)).FhasIncrblobCur != 0 {
			_invalidateIncrblobCursors(tls, p, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, (*TBtreePayload)(unsafe.Pointer(pX)).FnKey, 0)
		}
		/* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
		 ** to a row with the same key as the new entry being inserted.
		 */
		/* On the other hand, BTREE_SAVEPOSITION==0 does not imply
		 ** that the cursor is not pointing to a row to be overwritten.
		 ** So do a complete check.
		 */
		if int32((*TBtCursor)(unsafe.Pointer(pCur)).FcurFlags)&int32(BTCF_ValidNKey) != 0 && (*TBtreePayload)(unsafe.Pointer(pX)).FnKey == (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey {
			/* The cursor is pointing to the entry that is to be
			 ** overwritten */
			if int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize) != 0 && (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnPayload == uint32((*TBtreePayload)(unsafe.Pointer(pX)).FnData)+uint32((*TBtreePayload)(unsafe.Pointer(pX)).FnZero) {
				/* New entry is the same size as the old.  Do an overwrite */
				return _btreeOverwriteCell(tls, pCur, pX)
			}
		} else {
			if **(**int32)(__ccgo_up(bp + 4)) == 0 {
				/* The cursor is *not* pointing to the cell to be overwritten, nor
				 ** to an adjacent cell.  Move the cursor so that it is pointing either
				 ** to the cell to be overwritten or an adjacent cell.
				 */
				**(**int32)(__ccgo_up(bp)) = _sqlite3BtreeTableMoveto(tls, pCur, (*TBtreePayload)(unsafe.Pointer(pX)).FnKey, libc.BoolInt32(flags&int32(BTREE_APPEND) != 0), bp+4)
				if **(**int32)(__ccgo_up(bp)) != 0 {
					return **(**int32)(__ccgo_up(bp))
				}
			}
		}
	} else {
		/* This is an index or a WITHOUT ROWID table */
		/* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
		 ** to a row with the same key as the new entry being inserted.
		 */
		/* If the cursor is not already pointing either to the cell to be
		 ** overwritten, or if a new cell is being inserted, if the cursor is
		 ** not pointing to an immediately adjacent cell, then move the cursor
		 ** so that it does.
		 */
		if **(**int32)(__ccgo_up(bp + 4)) == 0 && flags&int32(BTREE_SAVEPOSITION) == 0 {
			if (*TBtreePayload)(unsafe.Pointer(pX)).FnMem != 0 {
				(**(**TUnpackedRecord)(__ccgo_up(bp + 16))).FpKeyInfo = (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo
				(**(**TUnpackedRecord)(__ccgo_up(bp + 16))).FaMem = (*TBtreePayload)(unsafe.Pointer(pX)).FaMem
				(**(**TUnpackedRecord)(__ccgo_up(bp + 16))).FnField = (*TBtreePayload)(unsafe.Pointer(pX)).FnMem
				(**(**TUnpackedRecord)(__ccgo_up(bp + 16))).Fdefault_rc = 0
				(**(**TUnpackedRecord)(__ccgo_up(bp + 16))).FeqSeen = uint8(0)
				**(**int32)(__ccgo_up(bp)) = _sqlite3BtreeIndexMoveto(tls, pCur, bp+16, bp+4)
			} else {
				**(**int32)(__ccgo_up(bp)) = _btreeMoveto(tls, pCur, (*TBtreePayload)(unsafe.Pointer(pX)).FpKey, (*TBtreePayload)(unsafe.Pointer(pX)).FnKey, libc.BoolInt32(flags&int32(BTREE_APPEND) != 0), bp+4)
			}
			if **(**int32)(__ccgo_up(bp)) != 0 {
				return **(**int32)(__ccgo_up(bp))
			}
		}
		/* If the cursor is currently pointing to an entry to be overwritten
		 ** and the new content is the same as as the old, then use the
		 ** overwrite optimization.
		 */
		if **(**int32)(__ccgo_up(bp + 4)) == 0 {
			_getCellInfo(tls, pCur)
			if (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnKey == (*TBtreePayload)(unsafe.Pointer(pX)).FnKey {
				(**(**TBtreePayload)(__ccgo_up(bp + 48))).FpData = (*TBtreePayload)(unsafe.Pointer(pX)).FpKey
				(**(**TBtreePayload)(__ccgo_up(bp + 48))).FnData = int32((*TBtreePayload)(unsafe.Pointer(pX)).FnKey)
				(**(**TBtreePayload)(__ccgo_up(bp + 48))).FnZero = 0
				return _btreeOverwriteCell(tls, pCur, bp+48)
			}
		}
	}
	pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
	if (*TMemPage)(unsafe.Pointer(pPage)).FnFree < 0 {
		if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) > int32(CURSOR_INVALID) {
			/* ^^^^^--- due to the moveToRoot() call above */
			**(**int32)(__ccgo_up(bp)) = _sqlite3CorruptError(tls, int32(82796))
		} else {
			**(**int32)(__ccgo_up(bp)) = _btreeComputeFreeSpace(tls, pPage)
		}
		if **(**int32)(__ccgo_up(bp)) != 0 {
			return **(**int32)(__ccgo_up(bp))
		}
	}
	newCell = (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FpTmpSpace
	if flags&int32(BTREE_PREFORMAT) != 0 {
		**(**int32)(__ccgo_up(bp)) = SQLITE_OK
		**(**int32)(__ccgo_up(bp + 8)) = (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FnPreformatSize
		if **(**int32)(__ccgo_up(bp + 8)) < int32(4) {
			**(**int32)(__ccgo_up(bp + 8)) = int32(4)
			**(**uint8)(__ccgo_up(newCell + 3)) = uint8(0)
		}
		if (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FautoVacuum != 0 && **(**int32)(__ccgo_up(bp + 8)) > int32((*TMemPage)(unsafe.Pointer(pPage)).FmaxLocal) {
			(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, newCell, bp+88)
			if (**(**TCellInfo)(__ccgo_up(bp + 88))).FnPayload != uint32((**(**TCellInfo)(__ccgo_up(bp + 88))).FnLocal) {
				ovfl = _sqlite3Get4byte(tls, newCell+uintptr(**(**int32)(__ccgo_up(bp + 8))-int32(4)))
				_ptrmapPut(tls, (*TBtree)(unsafe.Pointer(p)).FpBt, ovfl, uint8(PTRMAP_OVERFLOW1), (*TMemPage)(unsafe.Pointer(pPage)).Fpgno, bp)
				if **(**int32)(__ccgo_up(bp)) != 0 {
					goto end_insert
				}
			}
		}
	} else {
		**(**int32)(__ccgo_up(bp)) = _fillInCell(tls, pPage, newCell, pX, bp+8)
		if **(**int32)(__ccgo_up(bp)) != 0 {
			goto end_insert
		}
	}
	idx = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix)
	(*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0)
	if **(**int32)(__ccgo_up(bp + 4)) == 0 {
		if idx >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
			return _sqlite3CorruptError(tls, int32(82838))
		}
		**(**int32)(__ccgo_up(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage)
		if **(**int32)(__ccgo_up(bp)) != 0 {
			goto end_insert
		}
		oldCell = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx))))<<libc.Int32FromInt32(8)|int32(**(**Tu8)(__ccgo_up((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx) + 1)))))
		if !((*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0) {
			libc.Xmemcpy(tls, newCell, oldCell, uint32(4))
		}
		(*(*func(*libc.TLS, uintptr, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxParseCell})))(tls, pPage, oldCell, bp+112)
		if uint32((**(**TCellInfo)(__ccgo_up(bp + 112))).FnLocal) != (**(**TCellInfo)(__ccgo_up(bp + 112))).FnPayload {
			**(**int32)(__ccgo_up(bp)) = _clearCellOverflow(tls, pPage, oldCell, bp+112)
		} else {
			**(**int32)(__ccgo_up(bp)) = SQLITE_OK
		}
		v1 = pCur + 1
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(BTCF_ValidOvfl))
		if int32((**(**TCellInfo)(__ccgo_up(bp + 112))).FnSize) == **(**int32)(__ccgo_up(bp + 8)) && uint32((**(**TCellInfo)(__ccgo_up(bp + 112))).FnLocal) == (**(**TCellInfo)(__ccgo_up(bp + 112))).FnPayload && (!((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FautoVacuum != 0) || **(**int32)(__ccgo_up(bp + 8)) < int32((*TMemPage)(unsafe.Pointer(pPage)).FminLocal)) {
			/* Overwrite the old cell with the new if they are the same size.
			 ** We could also try to do this if the old cell is smaller, then add
			 ** the leftover space to the free list.  But experiments show that
			 ** doing that is no faster then skipping this optimization and just
			 ** calling dropCell() and insertCell().
			 **
			 ** This optimization cannot be used on an autovacuum database if the
			 ** new entry uses overflow pages, as the insertCell() call below is
			 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry.  */
			/* clearCell never fails when nLocal==nPayload */
			if oldCell < (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)+uintptr(10) {
				return _sqlite3CorruptError(tls, int32(82865))
			}
			if oldCell+uintptr(**(**int32)(__ccgo_up(bp + 8))) > (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd {
				return _sqlite3CorruptError(tls, int32(82868))
			}
			libc.Xmemcpy(tls, oldCell, newCell, uint32(**(**int32)(__ccgo_up(bp + 8))))
			return SQLITE_OK
		}
		_dropCell(tls, pPage, idx, int32((**(**TCellInfo)(__ccgo_up(bp + 112))).FnSize), bp)
		if **(**int32)(__ccgo_up(bp)) != 0 {
			goto end_insert
		}
	} else {
		if **(**int32)(__ccgo_up(bp + 4)) < 0 && int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) > 0 {
			v1 = pCur + 70
			*(*Tu16)(unsafe.Pointer(v1)) = *(*Tu16)(unsafe.Pointer(v1)) + 1
			v2 = *(*Tu16)(unsafe.Pointer(v1))
			idx = int32(v2)
			v1 = pCur + 1
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl)))
		} else {
		}
	}
	**(**int32)(__ccgo_up(bp)) = _insertCellFast(tls, pPage, idx, newCell, **(**int32)(__ccgo_up(bp + 8)))
	/* If no error has occurred and pPage has an overflow cell, call balance()
	 ** to redistribute the cells within the tree. Since balance() may move
	 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
	 ** variables.
	 **
	 ** Previous versions of SQLite called moveToRoot() to move the cursor
	 ** back to the root page as balance() used to invalidate the contents
	 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
	 ** set the cursor state to "invalid". This makes common insert operations
	 ** slightly faster.
	 **
	 ** There is a subtle but important optimization here too. When inserting
	 ** multiple records into an intkey b-tree using a single cursor (as can
	 ** happen while processing an "INSERT INTO ... SELECT" statement), it
	 ** is advantageous to leave the cursor pointing to the last entry in
	 ** the b-tree if possible. If the cursor is left pointing to the last
	 ** entry in the table, and the next row inserted has an integer key
	 ** larger than the largest existing key, it is possible to insert the
	 ** row without seeking the cursor. This can be a big performance boost.
	 */
	if (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow != 0 {
		v1 = pCur + 1
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl)))
		**(**int32)(__ccgo_up(bp)) = _balance(tls, pCur)
		/* Must make sure nOverflow is reset to zero even if the balance()
		 ** fails. Internal data structure corruption will result otherwise.
		 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
		 ** from trying to save the current position of the cursor.  */
		(*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnOverflow = uint8(0)
		(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_INVALID)
		if flags&int32(BTREE_SAVEPOSITION) != 0 && **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			_btreeReleaseAllCursorPages(tls, pCur)
			if (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo != 0 {
				(*TBtCursor)(unsafe.Pointer(pCur)).FpKey = _sqlite3Malloc(tls, uint64((*TBtreePayload)(unsafe.Pointer(pX)).FnKey))
				if (*TBtCursor)(unsafe.Pointer(pCur)).FpKey == uintptr(0) {
					**(**int32)(__ccgo_up(bp)) = int32(SQLITE_NOMEM)
				} else {
					libc.Xmemcpy(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpKey, (*TBtreePayload)(unsafe.Pointer(pX)).FpKey, uint32((*TBtreePayload)(unsafe.Pointer(pX)).FnKey))
				}
			}
			(*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_REQUIRESEEK)
			(*TBtCursor)(unsafe.Pointer(pCur)).FnKey = (*TBtreePayload)(unsafe.Pointer(pX)).FnKey
		}
	}
	goto end_insert
end_insert:
	;
	return **(**int32)(__ccgo_up(bp))
	return r
}

// C documentation
//
//	/*
//	** This routine does a complete check of the given BTree file.  aRoot[] is
//	** an array of pages numbers were each page number is the root page of
//	** a table.  nRoot is the number of entries in aRoot.
//	**
//	** A read-only or read-write transaction must be opened before calling
//	** this function.
//	**
//	** Write the number of error seen in *pnErr.  Except for some memory
//	** allocation errors,  an error message held in memory obtained from
//	** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
//	** returned.  If a memory allocation error occurs, NULL is returned.
//	**
//	** If the first entry in aRoot[] is 0, that indicates that the list of
//	** root pages is incomplete.  This is a "partial integrity-check".  This
//	** happens when performing an integrity check on a single table.  The
//	** zero is skipped, of course.  But in addition, the freelist checks
//	** and the checks to make sure every page is referenced are also skipped,
//	** since obviously it is not possible to know which pages are covered by
//	** the unverified btrees.  Except, if aRoot[1] is 1, then the freelist
//	** checks are still performed.
//	*/
func _sqlite3BtreeIntegrityCheck(tls *libc.TLS, db uintptr, p uintptr, aRoot uintptr, aCnt uintptr, nRoot int32, mxErr int32, pnErr uintptr, pzOut uintptr) (r int32) {
	bp := tls.Alloc(224)
	defer tls.Free(224)
	var bCkFreelist, bPartial int32
	var i, mx, mxInHdr TPgno
	var pBt uintptr
	var savedDbFlags Tu64
	var _ /* notUsed at bp+192 */ Ti64
	var _ /* sCheck at bp+0 */ TIntegrityCk
	var _ /* zErr at bp+88 */ [100]int8
	_, _, _, _, _, _, _ = bCkFreelist, bPartial, i, mx, mxInHdr, pBt, savedDbFlags
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt
	savedDbFlags = (*Tsqlite3)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).Fdb)).Fflags
	bPartial = 0           /* True if not checking all btrees */
	bCkFreelist = int32(1) /* True to scan the freelist */
	/* aRoot[0]==0 means this is a partial check */
	if **(**TPgno)(__ccgo_up(aRoot)) == uint32(0) {
		bPartial = int32(1)
		if **(**TPgno)(__ccgo_up(aRoot + 1*4)) != uint32(1) {
			bCkFreelist = 0
		}
	}
	_sqlite3BtreeEnter(tls, p)
	libc.Xmemset(tls, bp, 0, uint32(88))
	(**(**TIntegrityCk)(__ccgo_up(bp))).Fdb = db
	(**(**TIntegrityCk)(__ccgo_up(bp))).FpBt = pBt
	(**(**TIntegrityCk)(__ccgo_up(bp))).FpPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager
	(**(**TIntegrityCk)(__ccgo_up(bp))).FnCkPage = _btreePagecount(tls, (**(**TIntegrityCk)(__ccgo_up(bp))).FpBt)
	(**(**TIntegrityCk)(__ccgo_up(bp))).FmxErr = mxErr
	_sqlite3StrAccumInit(tls, bp+48, uintptr(0), bp+88, int32(100), int32(SQLITE_MAX_LENGTH))
	(**(**TIntegrityCk)(__ccgo_up(bp))).FerrMsg.FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL)
	if (**(**TIntegrityCk)(__ccgo_up(bp))).FnCkPage == uint32(0) {
		goto integrity_ck_cleanup
	}
	(**(**TIntegrityCk)(__ccgo_up(bp))).FaPgRef = _sqlite3MallocZero(tls, uint64((**(**TIntegrityCk)(__ccgo_up(bp))).FnCkPage/uint32(8)+uint32(1)))
	if !((**(**TIntegrityCk)(__ccgo_up(bp))).FaPgRef != 0) {
		_checkOom(tls, bp)
		goto integrity_ck_cleanup
	}
	(**(**TIntegrityCk)(__ccgo_up(bp))).Fheap = _sqlite3PageMalloc(tls, int32((*TBtShared)(unsafe.Pointer(pBt)).FpageSize))
	if (**(**TIntegrityCk)(__ccgo_up(bp))).Fheap == uintptr(0) {
		_checkOom(tls, bp)
		goto integrity_ck_cleanup
	}
	i = uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize + libc.Uint32FromInt32(1)
	if i <= (**(**TIntegrityCk)(__ccgo_up(bp))).FnCkPage {
		_setPageReferenced(tls, bp, i)
	}
	/* Check the integrity of the freelist
	 */
	if bCkFreelist != 0 {
		(**(**TIntegrityCk)(__ccgo_up(bp))).FzPfx = __ccgo_ts + 6179
		_checkList(tls, bp, int32(1), _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+32), _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+36))
		(**(**TIntegrityCk)(__ccgo_up(bp))).FzPfx = uintptr(0)
	}
	/* Check all the tables.
	 */
	if !(bPartial != 0) {
		if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 {
			mx = uint32(0)
			i = uint32(0)
			for {
				if !(int32(i) < nRoot) {
					break
				}
				if mx < **(**TPgno)(__ccgo_up(aRoot + uintptr(i)*4)) {
					mx = **(**TPgno)(__ccgo_up(aRoot + uintptr(i)*4))
				}
				goto _1
			_1:
				;
				i = i + 1
			}
			mxInHdr = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+52)
			if mx != mxInHdr {
				_checkAppendMsg(tls, bp, __ccgo_ts+6190, libc.VaList(bp+208, mx, mxInHdr))
			}
		} else {
			if _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+64) != uint32(0) {
				_checkAppendMsg(tls, bp, __ccgo_ts+6235, 0)
			}
		}
	}
	**(**Tu64)(__ccgo_up((*TBtShared)(unsafe.Pointer(pBt)).Fdb + 32)) &= ^libc.Uint64FromInt32(SQLITE_CellSizeCk)
	i = uint32(0)
	for {
		if !(int32(i) < nRoot && (**(**TIntegrityCk)(__ccgo_up(bp))).FmxErr != 0) {
			break
		}
		(**(**TIntegrityCk)(__ccgo_up(bp))).FnRow = 0
		if **(**TPgno)(__ccgo_up(aRoot + uintptr(i)*4)) != 0 {
			if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && **(**TPgno)(__ccgo_up(aRoot + uintptr(i)*4)) > uint32(1) && !(bPartial != 0) {
				_checkPtrmap(tls, bp, **(**TPgno)(__ccgo_up(aRoot + uintptr(i)*4)), uint8(PTRMAP_ROOTPAGE), uint32(0))
			}
			(**(**TIntegrityCk)(__ccgo_up(bp))).Fv0 = **(**TPgno)(__ccgo_up(aRoot + uintptr(i)*4))
			_checkTreePage(tls, bp, **(**TPgno)(__ccgo_up(aRoot + uintptr(i)*4)), bp+192, libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32))
		}
		_sqlite3MemSetArrayInt64(tls, aCnt, int32(i), (**(**TIntegrityCk)(__ccgo_up(bp))).FnRow)
		goto _2
	_2:
		;
		i = i + 1
	}
	(*Tsqlite3)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).Fdb)).Fflags = savedDbFlags
	/* Make sure every page in the file is referenced
	 */
	if !(bPartial != 0) {
		i = uint32(1)
		for {
			if !(i <= (**(**TIntegrityCk)(__ccgo_up(bp))).FnCkPage && (**(**TIntegrityCk)(__ccgo_up(bp))).FmxErr != 0) {
				break
			}
			/* If the database supports auto-vacuum, make sure no tables contain
			 ** references to pointer-map pages.
			 */
			if _getPageReferenced(tls, bp, i) == 0 && (_ptrmapPageno(tls, pBt, i) != i || !((*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0)) {
				_checkAppendMsg(tls, bp, __ccgo_ts+6290, libc.VaList(bp+208, i))
			}
			if _getPageReferenced(tls, bp, i) != 0 && (_ptrmapPageno(tls, pBt, i) == i && (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0) {
				_checkAppendMsg(tls, bp, __ccgo_ts+6310, libc.VaList(bp+208, i))
			}
			goto _3
		_3:
			;
			i = i + 1
		}
	}
	/* Clean  up and report errors.
	 */
	goto integrity_ck_cleanup
integrity_ck_cleanup:
	;
	_sqlite3PageFree(tls, (**(**TIntegrityCk)(__ccgo_up(bp))).Fheap)
	Xsqlite3_free(tls, (**(**TIntegrityCk)(__ccgo_up(bp))).FaPgRef)
	**(**int32)(__ccgo_up(pnErr)) = (**(**TIntegrityCk)(__ccgo_up(bp))).FnErr
	if (**(**TIntegrityCk)(__ccgo_up(bp))).FnErr == 0 {
		Xsqlite3_str_reset(tls, bp+48)
		**(**uintptr)(__ccgo_up(pzOut)) = uintptr(0)
	} else {
		**(**uintptr)(__ccgo_up(pzOut)) = _sqlite3StrAccumFinish(tls, bp+48)
	}
	/* Make sure this analysis did not leave any unref() pages. */
	_sqlite3BtreeLeave(tls, p)
	return (**(**TIntegrityCk)(__ccgo_up(bp))).Frc
}

func _sqlite3BtreeNext(tls *libc.TLS, pCur uintptr, flags int32) (r int32) {
	var pPage, v1 uintptr
	var v2 Tu16
	_, _, _ = pPage, v1, v2
	_ = flags /* Used in COMDB2 but not native SQLite */
	(*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0)
	v1 = pCur + 1
	*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(BTCF_ValidNKey) | libc.Int32FromInt32(BTCF_ValidOvfl)))
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID {
		return _btreeNext(tls, pCur)
	}
	pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage
	v1 = pCur + 70
	*(*Tu16)(unsafe.Pointer(v1)) = *(*Tu16)(unsafe.Pointer(v1)) + 1
	v2 = *(*Tu16)(unsafe.Pointer(v1))
	if int32(v2) >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) {
		(*TBtCursor)(unsafe.Pointer(pCur)).Fix = (*TBtCursor)(unsafe.Pointer(pCur)).Fix - 1
		return _btreeNext(tls, pCur)
	}
	if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 0 {
		return SQLITE_OK
	} else {
		return _moveToLeftmost(tls, pCur)
	}
	return r
}

// C documentation
//
//	/*
//	** Return the offset into the database file for the start of the
//	** payload to which the cursor is pointing.
//	*/
func _sqlite3BtreeOffset(tls *libc.TLS, pCur uintptr) (r Ti64) {
	_getCellInfo(tls, pCur)
	return int64((*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FpageSize)*(int64((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).Fpgno)-int64(1)) + int64(int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload)-int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaData))
}

// C documentation
//
//	/*
//	** Open a database file.
//	**
//	** zFilename is the name of the database file.  If zFilename is NULL
//	** then an ephemeral database is created.  The ephemeral database might
//	** be exclusively in memory, or it might use a disk-based memory cache.
//	** Either way, the ephemeral database will be automatically deleted
//	** when sqlite3BtreeClose() is called.
//	**
//	** If zFilename is ":memory:" then an in-memory database is created
//	** that is automatically destroyed when it is closed.
//	**
//	** The "flags" parameter is a bitmask that might contain bits like
//	** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
//	**
//	** If the database is already opened in the same database connection
//	** and we are in shared cache mode, then the open will fail with an
//	** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
//	** objects in the same database connection since doing so will lead
//	** to problems with locking.
//	*/
func _sqlite3BtreeOpen(tls *libc.TLS, pVfs uintptr, zFilename uintptr, db uintptr, ppBtree uintptr, flags int32, vfsFlags int32) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var i, iDb, isMemdb, isTempDb, nFilename, nFullPathname, rc, v1 int32
	var mutexOpen, mutexShared, mutexShared1, p, pBt, pExisting, pFile, pSib, zFullPathname, v4 uintptr
	var nReserve Tu8
	var _ /* zDbHeader at bp+0 */ [100]uint8
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iDb, isMemdb, isTempDb, mutexOpen, mutexShared, mutexShared1, nFilename, nFullPathname, nReserve, p, pBt, pExisting, pFile, pSib, rc, zFullPathname, v1, v4
	pBt = uintptr(0)       /* Handle to return */
	mutexOpen = uintptr(0) /* Prevents a race condition. Ticket #3537 */
	rc = SQLITE_OK         /* Database header content */
	/* True if opening an ephemeral, temporary database */
	isTempDb = libc.BoolInt32(zFilename == uintptr(0) || int32(**(**int8)(__ccgo_up(zFilename))) == 0)
	/* Set the variable isMemdb to true for an in-memory database, or
	 ** false for a file-based database.
	 */
	isMemdb = libc.BoolInt32(zFilename != 0 && libc.Xstrcmp(tls, zFilename, __ccgo_ts+5556) == 0 || isTempDb != 0 && _sqlite3TempInMemory(tls, db) != 0 || vfsFlags&int32(SQLITE_OPEN_MEMORY) != 0)
	/* flags fit in 8 bits */
	/* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
	/* A BTREE_SINGLE database is always a temporary and/or ephemeral */
	if isMemdb != 0 {
		flags = flags | int32(BTREE_MEMORY)
	}
	if vfsFlags&int32(SQLITE_OPEN_MAIN_DB) != 0 && (isMemdb != 0 || isTempDb != 0) {
		vfsFlags = vfsFlags & ^libc.Int32FromInt32(SQLITE_OPEN_MAIN_DB) | int32(SQLITE_OPEN_TEMP_DB)
	}
	p = _sqlite3MallocZero(tls, uint64(48))
	if !(p != 0) {
		return int32(SQLITE_NOMEM)
	}
	(*TBtree)(unsafe.Pointer(p)).FinTrans = uint8(TRANS_NONE)
	(*TBtree)(unsafe.Pointer(p)).Fdb = db
	(*TBtree)(unsafe.Pointer(p)).Flock.FpBtree = p
	(*TBtree)(unsafe.Pointer(p)).Flock.FiTable = uint32(1)
	/*
	 ** If this Btree is a candidate for shared cache, try to find an
	 ** existing BtShared object that we can share with
	 */
	if isTempDb == 0 && (isMemdb == 0 || vfsFlags&int32(SQLITE_OPEN_URI) != 0) {
		if vfsFlags&int32(SQLITE_OPEN_SHAREDCACHE) != 0 {
			nFilename = _sqlite3Strlen30(tls, zFilename) + int32(1)
			nFullPathname = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname + int32(1)
			if nFullPathname > nFilename {
				v1 = nFullPathname
			} else {
				v1 = nFilename
			}
			zFullPathname = _sqlite3Malloc(tls, uint64(v1))
			(*TBtree)(unsafe.Pointer(p)).Fsharable = uint8(1)
			if !(zFullPathname != 0) {
				Xsqlite3_free(tls, p)
				return int32(SQLITE_NOMEM)
			}
			if isMemdb != 0 {
				libc.Xmemcpy(tls, zFullPathname, zFilename, uint32(nFilename))
			} else {
				rc = _sqlite3OsFullPathname(tls, pVfs, zFilename, nFullPathname, zFullPathname)
				if rc != 0 {
					if rc == libc.Int32FromInt32(SQLITE_OK)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8) {
						rc = SQLITE_OK
					} else {
						Xsqlite3_free(tls, zFullPathname)
						Xsqlite3_free(tls, p)
						return rc
					}
				}
			}
			mutexOpen = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_OPEN))
			Xsqlite3_mutex_enter(tls, mutexOpen)
			mutexShared = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN))
			Xsqlite3_mutex_enter(tls, mutexShared)
			pBt = _sqlite3SharedCacheList
			for {
				if !(pBt != 0) {
					break
				}
				if 0 == libc.Xstrcmp(tls, zFullPathname, _sqlite3PagerFilename(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, 0)) && _sqlite3PagerVfs(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) == pVfs {
					iDb = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1)
					for {
						if !(iDb >= 0) {
							break
						}
						pExisting = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt
						if pExisting != 0 && (*TBtree)(unsafe.Pointer(pExisting)).FpBt == pBt {
							Xsqlite3_mutex_leave(tls, mutexShared)
							Xsqlite3_mutex_leave(tls, mutexOpen)
							Xsqlite3_free(tls, zFullPathname)
							Xsqlite3_free(tls, p)
							return int32(SQLITE_CONSTRAINT)
						}
						goto _3
					_3:
						;
						iDb = iDb - 1
					}
					(*TBtree)(unsafe.Pointer(p)).FpBt = pBt
					(*TBtShared)(unsafe.Pointer(pBt)).FnRef = (*TBtShared)(unsafe.Pointer(pBt)).FnRef + 1
					break
				}
				goto _2
			_2:
				;
				pBt = (*TBtShared)(unsafe.Pointer(pBt)).FpNext
			}
			Xsqlite3_mutex_leave(tls, mutexShared)
			Xsqlite3_free(tls, zFullPathname)
		}
	}
	if pBt == uintptr(0) {
		/*
		 ** The following asserts make sure that structures used by the btree are
		 ** the right size.  This is to guard against size changes that result
		 ** when compiling on a different architecture.
		 */
		/* Suppress false-positive compiler warning from PVS-Studio */
		libc.Xmemset(tls, bp+16, 0, uint32(8))
		pBt = _sqlite3MallocZero(tls, uint64(92))
		if pBt == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
			goto btree_open_out
		}
		rc = _sqlite3PagerOpen(tls, pVfs, pBt, zFilename, int32(84), flags, vfsFlags, __ccgo_fp(_pageReinit))
		if rc == SQLITE_OK {
			_sqlite3PagerSetMmapLimit(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, (*Tsqlite3)(unsafe.Pointer(db)).FszMmap)
			rc = _sqlite3PagerReadFileheader(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, int32(100), bp)
		}
		if rc != SQLITE_OK {
			goto btree_open_out
		}
		(*TBtShared)(unsafe.Pointer(pBt)).FopenFlags = uint8(flags)
		(*TBtShared)(unsafe.Pointer(pBt)).Fdb = db
		_sqlite3PagerSetBusyHandler(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, __ccgo_fp(_btreeInvokeBusyHandler), pBt)
		(*TBtree)(unsafe.Pointer(p)).FpBt = pBt
		(*TBtShared)(unsafe.Pointer(pBt)).FpCursor = uintptr(0)
		(*TBtShared)(unsafe.Pointer(pBt)).FpPage1 = uintptr(0)
		if _sqlite3PagerIsreadonly(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager) != 0 {
			v4 = pBt + 24
			*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(BTS_READ_ONLY))
		}
		/* EVIDENCE-OF: R-51873-39618 The page size for a database file is
		 ** determined by the 2-byte integer located at an offset of 16 bytes from
		 ** the beginning of the database file. */
		(*TBtShared)(unsafe.Pointer(pBt)).FpageSize = uint32(int32((**(**[100]uint8)(__ccgo_up(bp)))[int32(16)])<<int32(8) | int32((**(**[100]uint8)(__ccgo_up(bp)))[int32(17)])<<int32(16))
		if (*TBtShared)(unsafe.Pointer(pBt)).FpageSize < uint32(512) || (*TBtShared)(unsafe.Pointer(pBt)).FpageSize > uint32(SQLITE_MAX_PAGE_SIZE) || ((*TBtShared)(unsafe.Pointer(pBt)).FpageSize-uint32(1))&(*TBtShared)(unsafe.Pointer(pBt)).FpageSize != uint32(0) {
			(*TBtShared)(unsafe.Pointer(pBt)).FpageSize = uint32(0)
			/* If the magic name ":memory:" will create an in-memory database, then
			 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
			 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
			 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
			 ** regular file-name. In this case the auto-vacuum applies as per normal.
			 */
			if zFilename != 0 && !(isMemdb != 0) {
				(*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum = uint8(libc.Int32FromInt32(0))
				(*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum = uint8(libc.Int32FromInt32(0))
			}
			nReserve = uint8(0)
		} else {
			/* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
			 ** determined by the one-byte unsigned integer found at an offset of 20
			 ** into the database file header. */
			nReserve = (**(**[100]uint8)(__ccgo_up(bp)))[int32(20)]
			v4 = pBt + 24
			*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED))
			if _sqlite3Get4byte(tls, bp+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(4)*libc.Int32FromInt32(4))) != 0 {
				v1 = int32(1)
			} else {
				v1 = 0
			}
			(*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum = uint8(v1)
			if _sqlite3Get4byte(tls, bp+uintptr(libc.Int32FromInt32(36)+libc.Int32FromInt32(7)*libc.Int32FromInt32(4))) != 0 {
				v1 = int32(1)
			} else {
				v1 = 0
			}
			(*TBtShared)(unsafe.Pointer(pBt)).FincrVacuum = uint8(v1)
		}
		rc = _sqlite3PagerSetPagesize(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pBt+36, int32(nReserve))
		if rc != 0 {
			goto btree_open_out
		}
		(*TBtShared)(unsafe.Pointer(pBt)).FusableSize = (*TBtShared)(unsafe.Pointer(pBt)).FpageSize - uint32(nReserve)
		/* 8-byte alignment of pageSize */
		/* Add the new BtShared object to the linked list sharable BtShareds.
		 */
		(*TBtShared)(unsafe.Pointer(pBt)).FnRef = int32(1)
		if (*TBtree)(unsafe.Pointer(p)).Fsharable != 0 {
			mutexShared1 = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_MAIN))
			if libc.Bool(int32(SQLITE_THREADSAFE) != 0) && _sqlite3Config.FbCoreMutex != 0 {
				(*TBtShared)(unsafe.Pointer(pBt)).Fmutex = _sqlite3MutexAlloc(tls, SQLITE_MUTEX_FAST)
				if (*TBtShared)(unsafe.Pointer(pBt)).Fmutex == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
					goto btree_open_out
				}
			}
			Xsqlite3_mutex_enter(tls, mutexShared1)
			(*TBtShared)(unsafe.Pointer(pBt)).FpNext = _sqlite3SharedCacheList
			_sqlite3SharedCacheList = pBt
			Xsqlite3_mutex_leave(tls, mutexShared1)
		}
	}
	/* If the new Btree uses a sharable pBtShared, then link the new
	 ** Btree into the list of all sharable Btrees for the same connection.
	 ** The list is kept in ascending order by pBt address.
	 */
	if (*TBtree)(unsafe.Pointer(p)).Fsharable != 0 {
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			v4 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
			pSib = v4
			if v4 != uintptr(0) && (*TBtree)(unsafe.Pointer(pSib)).Fsharable != 0 {
				for (*TBtree)(unsafe.Pointer(pSib)).FpPrev != 0 {
					pSib = (*TBtree)(unsafe.Pointer(pSib)).FpPrev
				}
				if uint32((*TBtree)(unsafe.Pointer(p)).FpBt) < uint32((*TBtree)(unsafe.Pointer(pSib)).FpBt) {
					(*TBtree)(unsafe.Pointer(p)).FpNext = pSib
					(*TBtree)(unsafe.Pointer(p)).FpPrev = uintptr(0)
					(*TBtree)(unsafe.Pointer(pSib)).FpPrev = p
				} else {
					for (*TBtree)(unsafe.Pointer(pSib)).FpNext != 0 && uint32((*TBtree)(unsafe.Pointer((*TBtree)(unsafe.Pointer(pSib)).FpNext)).FpBt) < uint32((*TBtree)(unsafe.Pointer(p)).FpBt) {
						pSib = (*TBtree)(unsafe.Pointer(pSib)).FpNext
					}
					(*TBtree)(unsafe.Pointer(p)).FpNext = (*TBtree)(unsafe.Pointer(pSib)).FpNext
					(*TBtree)(unsafe.Pointer(p)).FpPrev = pSib
					if (*TBtree)(unsafe.Pointer(p)).FpNext != 0 {
						(*TBtree)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpNext)).FpPrev = p
					}
					(*TBtree)(unsafe.Pointer(pSib)).FpNext = p
				}
				break
			}
			goto _8
		_8:
			;
			i = i + 1
		}
	}
	**(**uintptr)(__ccgo_up(ppBtree)) = p
	goto btree_open_out
btree_open_out:
	;
	if rc != SQLITE_OK {
		if pBt != 0 && (*TBtShared)(unsafe.Pointer(pBt)).FpPager != 0 {
			_sqlite3PagerClose(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, uintptr(0))
		}
		Xsqlite3_free(tls, pBt)
		Xsqlite3_free(tls, p)
		**(**uintptr)(__ccgo_up(ppBtree)) = uintptr(0)
	} else {
		/* If the B-Tree was successfully opened, set the pager-cache size to the
		 ** default value. Except, when opening on an existing shared pager-cache,
		 ** do not change the pager-cache size.
		 */
		if _sqlite3BtreeSchema(tls, p, 0, uintptr(0)) == uintptr(0) {
			_sqlite3BtreeSetCacheSize(tls, p, -int32(2000))
		}
		pFile = _sqlite3PagerFile(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager)
		if (*Tsqlite3_file)(unsafe.Pointer(pFile)).FpMethods != 0 {
			_sqlite3OsFileControlHint(tls, pFile, int32(SQLITE_FCNTL_PDB), pBt+4)
		}
	}
	if mutexOpen != 0 {
		Xsqlite3_mutex_leave(tls, mutexOpen)
	}
	return rc
}

// C documentation
//
//	/*
//	** Return an estimate for the number of rows in the table that pCur is
//	** pointing to.  Return a negative number if no estimate is currently
//	** available.
//	*/
func _sqlite3BtreeRowCountEst(tls *libc.TLS, pCur uintptr) (r Ti64) {
	var i Tu8
	var n Ti64
	_, _ = i, n
	/* Currently this interface is only called by the OP_IfSizeBetween
	 ** opcode and the OP_Count opcode with P3=1.  In either case,
	 ** the cursor will always be valid unless the btree is empty. */
	if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID {
		return 0
	}
	if int32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).Fleaf) == 0 {
		return int64(-int32(1))
	}
	n = int64((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FnCell)
	i = uint8(0)
	for {
		if !(int32(i) < int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)) {
			break
		}
		n = n * int64(int32((*TMemPage)(unsafe.Pointer(**(**uintptr)(__ccgo_up(pCur + 120 + uintptr(i)*4)))).FnCell)+int32(1))
		goto _1
	_1:
		;
		i = i + 1
	}
	return n
}

// C documentation
//
//	/*
//	** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
//	**
//	**    newFlag==0       Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
//	**    newFlag==1       BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
//	**    newFlag==2       BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
//	**    newFlag==(-1)    No changes
//	**
//	** This routine acts as a query if newFlag is less than zero
//	**
//	** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
//	** freelist leaf pages are not written back to the database.  Thus in-page
//	** deleted content is cleared, but freelist deleted content is not.
//	**
//	** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
//	** that freelist leaf pages are written back into the database, increasing
//	** the amount of disk I/O.
//	*/
func _sqlite3BtreeSecureDelete(tls *libc.TLS, p uintptr, newFlag int32) (r int32) {
	var b int32
	var v1 uintptr
	_, _ = b, v1
	if p == uintptr(0) {
		return 0
	}
	_sqlite3BtreeEnter(tls, p)
	if newFlag >= 0 {
		v1 = (*TBtree)(unsafe.Pointer(p)).FpBt + 24
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(BTS_FAST_SECURE))
		v1 = (*TBtree)(unsafe.Pointer(p)).FpBt + 24
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | int32(uint16(libc.Int32FromInt32(BTS_SECURE_DELETE)*newFlag)))
	}
	b = int32((*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FbtsFlags) & int32(BTS_FAST_SECURE) / int32(BTS_SECURE_DELETE)
	_sqlite3BtreeLeave(tls, p)
	return b
}

// C documentation
//
//	/*
//	** Change the default pages size and the number of reserved bytes per page.
//	** Or, if the page size has already been fixed, return SQLITE_READONLY
//	** without changing anything.
//	**
//	** The page size must be a power of 2 between 512 and 65536.  If the page
//	** size supplied does not meet this constraint then the page size is not
//	** changed.
//	**
//	** Page sizes are constrained to be a power of two so that the region
//	** of the database file used for locking (beginning at PENDING_BYTE,
//	** the first byte past the 1GB boundary, 0x40000000) needs to occur
//	** at the beginning of a page.
//	**
//	** If parameter nReserve is less than zero, then the number of reserved
//	** bytes per page is left unchanged.
//	**
//	** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
//	** and autovacuum mode can no longer be changed.
//	*/
func _sqlite3BtreeSetPageSize(tls *libc.TLS, p uintptr, pageSize int32, nReserve int32, iFix int32) (r int32) {
	var pBt, v1 uintptr
	var rc, x int32
	_, _, _, _ = pBt, rc, x, v1
	rc = SQLITE_OK
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt
	_sqlite3BtreeEnter(tls, p)
	(*TBtShared)(unsafe.Pointer(pBt)).FnReserveWanted = uint8(nReserve)
	x = int32((*TBtShared)(unsafe.Pointer(pBt)).FpageSize - (*TBtShared)(unsafe.Pointer(pBt)).FusableSize)
	if x == nReserve && (pageSize == 0 || uint32(pageSize) == (*TBtShared)(unsafe.Pointer(pBt)).FpageSize) {
		_sqlite3BtreeLeave(tls, p)
		return SQLITE_OK
	}
	if nReserve < x {
		nReserve = x
	}
	if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_PAGESIZE_FIXED) != 0 {
		_sqlite3BtreeLeave(tls, p)
		return int32(SQLITE_READONLY)
	}
	if pageSize >= int32(512) && pageSize <= int32(SQLITE_MAX_PAGE_SIZE) && (pageSize-int32(1))&pageSize == 0 {
		if nReserve > int32(32) && pageSize == int32(512) {
			pageSize = int32(1024)
		}
		(*TBtShared)(unsafe.Pointer(pBt)).FpageSize = uint32(pageSize)
		_freeTempSpace(tls, pBt)
	}
	rc = _sqlite3PagerSetPagesize(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, pBt+36, nReserve)
	(*TBtShared)(unsafe.Pointer(pBt)).FusableSize = (*TBtShared)(unsafe.Pointer(pBt)).FpageSize - uint32(uint16(nReserve))
	if iFix != 0 {
		v1 = pBt + 24
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED))
	}
	_sqlite3BtreeLeave(tls, p)
	return rc
}

// C documentation
//
//	/*
//	** Set both the "read version" (single byte at byte offset 18) and
//	** "write version" (single byte at byte offset 19) fields in the database
//	** header to iVersion.
//	*/
func _sqlite3BtreeSetVersion(tls *libc.TLS, pBtree uintptr, iVersion int32) (r int32) {
	var aData, pBt, v1 uintptr
	var rc int32
	_, _, _, _ = aData, pBt, rc, v1
	pBt = (*TBtree)(unsafe.Pointer(pBtree)).FpBt /* Return code */
	/* If setting the version fields to 1, do not automatically open the
	 ** WAL connection, even if the version fields are currently set to 2.
	 */
	v1 = pBt + 24
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(BTS_NO_WAL))
	if iVersion == int32(1) {
		v1 = pBt + 24
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(BTS_NO_WAL))
	}
	rc = _sqlite3BtreeBeginTrans(tls, pBtree, 0, uintptr(0))
	if rc == SQLITE_OK {
		aData = (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData
		if int32(**(**Tu8)(__ccgo_up(aData + 18))) != int32(uint8(iVersion)) || int32(**(**Tu8)(__ccgo_up(aData + 19))) != int32(uint8(iVersion)) {
			rc = _sqlite3BtreeBeginTrans(tls, pBtree, int32(2), uintptr(0))
			if rc == SQLITE_OK {
				rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FpDbPage)
				if rc == SQLITE_OK {
					**(**Tu8)(__ccgo_up(aData + 18)) = uint8(iVersion)
					**(**Tu8)(__ccgo_up(aData + 19)) = uint8(iVersion)
				}
			}
		}
	}
	v1 = pBt + 24
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(BTS_NO_WAL))
	return rc
}

// C documentation
//
//	/*
//	** This function is used as part of copying the current row from cursor
//	** pSrc into cursor pDest. If the cursors are open on intkey tables, then
//	** parameter iKey is used as the rowid value when the record is copied
//	** into pDest. Otherwise, the record is copied verbatim.
//	**
//	** This function does not actually write the new value to cursor pDest.
//	** Instead, it creates and populates any required overflow pages and
//	** writes the data for the new cell into the BtShared.pTmpSpace buffer
//	** for the destination database. The size of the cell, in bytes, is left
//	** in BtShared.nPreformatSize. The caller completes the insertion by
//	** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
//	**
//	** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
//	*/
func _sqlite3BtreeTransferRow(tls *libc.TLS, pDest uintptr, pSrc uintptr, iKey Ti64) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aIn, aOut, pBt, pPageOut, pPgnoOut, pSrcPager, v1 uintptr
	var nCopy int32
	var nIn, nOut, nRem Tu32
	var ovflIn TPgno
	var v2 uint32
	var _ /* pNew at bp+12 */ uintptr
	var _ /* pPageIn at bp+4 */ uintptr
	var _ /* pgnoNew at bp+8 */ TPgno
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _ = aIn, aOut, nCopy, nIn, nOut, nRem, ovflIn, pBt, pPageOut, pPgnoOut, pSrcPager, v1, v2
	pBt = (*TBtCursor)(unsafe.Pointer(pDest)).FpBt
	aOut = (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace /* Bytes of data still to copy */
	_getCellInfo(tls, pSrc)
	if (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload < uint32(0x80) {
		v1 = aOut
		aOut = aOut + 1
		**(**Tu8)(__ccgo_up(v1)) = uint8((*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload)
	} else {
		aOut = aOut + uintptr(_sqlite3PutVarint(tls, aOut, uint64((*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload)))
	}
	if (*TBtCursor)(unsafe.Pointer(pDest)).FpKeyInfo == uintptr(0) {
		aOut = aOut + uintptr(_sqlite3PutVarint(tls, aOut, uint64(iKey)))
	}
	nIn = uint32((*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnLocal)
	aIn = (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FpPayload
	if aIn+uintptr(nIn) > (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pSrc)).FpPage)).FaDataEnd {
		return _sqlite3CorruptError(tls, int32(82970))
	}
	nRem = (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload
	if nIn == nRem && nIn < uint32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pDest)).FpPage)).FmaxLocal) {
		libc.Xmemcpy(tls, aOut, aIn, nIn)
		(*TBtShared)(unsafe.Pointer(pBt)).FnPreformatSize = int32(nIn + uint32(int32(aOut)-int32((*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace)))
		return SQLITE_OK
	} else {
		**(**int32)(__ccgo_up(bp)) = SQLITE_OK
		pSrcPager = (*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pSrc)).FpBt)).FpPager
		pPgnoOut = uintptr(0)
		ovflIn = uint32(0)
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
		pPageOut = uintptr(0) /* Size of output buffer aOut[] */
		nOut = uint32(_btreePayloadToLocal(tls, (*TBtCursor)(unsafe.Pointer(pDest)).FpPage, int64((*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload)))
		(*TBtShared)(unsafe.Pointer(pBt)).FnPreformatSize = int32(nOut) + (int32(aOut) - int32((*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace))
		if nOut < (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload {
			pPgnoOut = aOut + uintptr(nOut)
			**(**int32)(__ccgo_up(pBt + 88)) += int32(4)
		}
		if nRem > nIn {
			if aIn+uintptr(nIn)+uintptr(4) > (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pSrc)).FpPage)).FaDataEnd {
				return _sqlite3CorruptError(tls, int32(82995))
			}
			ovflIn = _sqlite3Get4byte(tls, (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FpPayload+uintptr(nIn))
		}
		for cond := true; cond; cond = nRem > uint32(0) && **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			nRem = nRem - nOut
			for cond := true; cond; cond = **(**int32)(__ccgo_up(bp)) == SQLITE_OK && nOut > uint32(0) {
				if nIn > uint32(0) {
					if nOut < nIn {
						v2 = nOut
					} else {
						v2 = nIn
					}
					nCopy = int32(v2)
					libc.Xmemcpy(tls, aOut, aIn, uint32(nCopy))
					nOut = nOut - uint32(nCopy)
					nIn = nIn - uint32(nCopy)
					aOut = aOut + uintptr(nCopy)
					aIn = aIn + uintptr(nCopy)
				}
				if nOut > uint32(0) {
					_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp + 4)))
					**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
					**(**int32)(__ccgo_up(bp)) = _sqlite3PagerGet(tls, pSrcPager, ovflIn, bp+4, int32(PAGER_GET_READONLY))
					if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
						aIn = _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp + 4)))
						ovflIn = _sqlite3Get4byte(tls, aIn)
						aIn = aIn + uintptr(4)
						nIn = (*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pSrc)).FpBt)).FusableSize - uint32(4)
					}
				}
			}
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && nRem > uint32(0) && pPgnoOut != 0 {
				**(**TPgno)(__ccgo_up(bp + 8)) = uint32(0) /* Prevent harmless static-analyzer warning */
				**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
				**(**int32)(__ccgo_up(bp)) = _allocateBtreePage(tls, pBt, bp+12, bp+8, uint32(0), uint8(0))
				_sqlite3Put4byte(tls, pPgnoOut, **(**TPgno)(__ccgo_up(bp + 8)))
				if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && pPageOut != 0 {
					_ptrmapPut(tls, pBt, **(**TPgno)(__ccgo_up(bp + 8)), uint8(PTRMAP_OVERFLOW2), (*TMemPage)(unsafe.Pointer(pPageOut)).Fpgno, bp)
				}
				_releasePage(tls, pPageOut)
				pPageOut = **(**uintptr)(__ccgo_up(bp + 12))
				if pPageOut != 0 {
					pPgnoOut = (*TMemPage)(unsafe.Pointer(pPageOut)).FaData
					_sqlite3Put4byte(tls, pPgnoOut, uint32(0))
					aOut = pPgnoOut + 4
					if (*TBtShared)(unsafe.Pointer(pBt)).FusableSize-uint32(4) < nRem {
						v2 = (*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(4)
					} else {
						v2 = nRem
					}
					nOut = v2
				}
			}
		}
		_releasePage(tls, pPageOut)
		_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp + 4)))
		return **(**int32)(__ccgo_up(bp))
	}
	return r
}

// C documentation
//
//	/*
//	** Generate code that will increment the schema cookie.
//	**
//	** The schema cookie is used to determine when the schema for the
//	** database changes.  After each schema change, the cookie value
//	** changes.  When a process first reads the schema it records the
//	** cookie.  Thereafter, whenever it goes to access the database,
//	** it checks the cookie to make sure the schema has not changed
//	** since it was last read.
//	**
//	** This plan is not completely bullet-proof.  It is possible for
//	** the schema to change multiple times and for the cookie to be
//	** set back to prior value.  But schema changes are infrequent
//	** and the probability of hitting the same cookie value is only
//	** 1 chance in 2^32.  So we're safe enough.
//	**
//	** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
//	** the schema-version whenever the schema changes.
//	*/
func _sqlite3ChangeCookie(tls *libc.TLS, pParse uintptr, iDb int32) {
	var db, v uintptr
	_, _ = db, v
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	_sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_SCHEMA_VERSION), int32(libc.Uint32FromInt32(1)+uint32((*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema)).Fschema_cookie)))
}

// C documentation
//
//	/*
//	** This routine is used to check if the UTF-8 string zName is a legal
//	** unqualified name for a new schema object (table, index, view or
//	** trigger). All names are legal except those that begin with the string
//	** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
//	** is reserved for internal use.
//	**
//	** When parsing the sqlite_schema table, this routine also checks to
//	** make sure the "type", "name", and "tbl_name" columns are consistent
//	** with the SQL.
//	*/
func _sqlite3CheckObjectName(tls *libc.TLS, pParse uintptr, zName uintptr, zType uintptr, zTblName uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db uintptr
	_ = db
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if _sqlite3WritableSchema(tls, db) != 0 || int32(uint32(*(*uint8)(unsafe.Pointer(db + 176 + 8))&0x6>>1)) != 0 || !(_sqlite3Config.FbExtraSchemaChecks != 0) {
		/* Skip these error checks for writable_schema=ON */
		return SQLITE_OK
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 {
		if Xsqlite3_stricmp(tls, zType, **(**uintptr)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit))) != 0 || Xsqlite3_stricmp(tls, zName, **(**uintptr)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit + 1*4))) != 0 || Xsqlite3_stricmp(tls, zTblName, **(**uintptr)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit + 2*4))) != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1711, 0) /* corruptSchema() will supply the error */
			return int32(SQLITE_ERROR)
		}
	} else {
		if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && 0 == Xsqlite3_strnicmp(tls, zName, __ccgo_ts+7973, int32(7)) || _sqlite3ReadOnlyShadowTables(tls, db) != 0 && _sqlite3ShadowTableName(tls, db, zName) != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14980, libc.VaList(bp+8, zName))
			return int32(SQLITE_ERROR)
		}
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Remove entries from the sqlite_statN tables (for N in (1,2,3))
//	** after a DROP INDEX or DROP TABLE command.
//	*/
func _sqlite3ClearStatTables(tls *libc.TLS, pParse uintptr, iDb int32, zType uintptr, zName uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var i int32
	var zDbName uintptr
	var _ /* zTab at bp+0 */ [24]int8
	_, _ = i, zDbName
	zDbName = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FzDbSName
	i = int32(1)
	for {
		if !(i <= int32(4)) {
			break
		}
		Xsqlite3_snprintf(tls, int32(24), bp, __ccgo_ts+16168, libc.VaList(bp+32, i))
		if _sqlite3FindTable(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, bp, zDbName) != 0 {
			_sqlite3NestedParse(tls, pParse, __ccgo_ts+14153, libc.VaList(bp+32, zDbName, bp, zType, zName))
		}
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Generate code to drop a table.
//	*/
func _sqlite3CodeDropTable(tls *libc.TLS, pParse uintptr, pTab uintptr, iDb int32, isView int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pDb, pTrigger, v uintptr
	_, _, _, _ = db, pDb, pTrigger, v
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16
	v = _sqlite3GetVdbe(tls, pParse)
	_sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb)
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		_sqlite3VdbeAddOp0(tls, v, int32(OP_VBegin))
	}
	/* Drop all triggers associated with the table being dropped. Code
	 ** is generated to remove entries from sqlite_schema and/or
	 ** sqlite_temp_schema if required.
	 */
	pTrigger = _sqlite3TriggerList(tls, pParse, pTab)
	for pTrigger != 0 {
		_sqlite3DropTriggerPtr(tls, pParse, pTrigger)
		pTrigger = (*TTrigger)(unsafe.Pointer(pTrigger)).FpNext
	}
	/* Remove any entries of the sqlite_sequence table associated with
	 ** the table being dropped. This is done before the table is dropped
	 ** at the btree level, in case the sqlite_sequence table needs to
	 ** move as a result of the drop (can happen in auto-vacuum mode).
	 */
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Autoincrement) != 0 {
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+16182, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, (*TTable)(unsafe.Pointer(pTab)).FzName))
	}
	/* Drop all entries in the schema table that refer to the
	 ** table. The program name loops through the schema table and deletes
	 ** every row that refers to a table of the same name as the one being
	 ** dropped. Triggers are handled separately because a trigger can be
	 ** created in the temp database that refers to a table in another
	 ** database.
	 */
	_sqlite3NestedParse(tls, pParse, __ccgo_ts+16227, libc.VaList(bp+8, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, (*TTable)(unsafe.Pointer(pTab)).FzName))
	if !(isView != 0) && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
		_destroyTable(tls, pParse, pTab)
	}
	/* Remove the table entry from SQLite's internal schema and modify
	 ** the schema cookie.
	 */
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		_sqlite3VdbeAddOp4(tls, v, int32(OP_VDestroy), iDb, 0, 0, (*TTable)(unsafe.Pointer(pTab)).FzName, 0)
		_sqlite3MayAbort(tls, pParse)
	}
	_sqlite3VdbeAddOp4(tls, v, int32(OP_DropTable), iDb, 0, 0, (*TTable)(unsafe.Pointer(pTab)).FzName, 0)
	_sqlite3ChangeCookie(tls, pParse, iDb)
	_sqliteViewResetAll(tls, db, iDb)
}

// C documentation
//
//	/*
//	** Generate code that will construct an ephemeral table containing all terms
//	** in the RHS of an IN operator.  The IN operator can be in either of two
//	** forms:
//	**
//	**     x IN (4,5,11)              -- IN operator with list on right-hand side
//	**     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
//	**
//	** The pExpr parameter is the IN operator.  The cursor number for the
//	** constructed ephemeral table is returned.  The first time the ephemeral
//	** table is computed, the cursor number is also stored in pExpr->iTable,
//	** however the cursor number returned might not be the same, as it might
//	** have been duplicated using OP_OpenDup.
//	**
//	** If the LHS expression ("x" in the examples) is a column value, or
//	** the SELECT statement returns a column value, then the affinity of that
//	** column is used to build the index keys. If both 'x' and the
//	** SELECT... statement are columns, then numeric affinity is used
//	** if either column has NUMERIC or INTEGER affinity. If neither
//	** 'x' nor the SELECT... statement are columns, then numeric affinity
//	** is used.
//	*/
func _sqlite3CodeRhsOfIN(tls *libc.TLS, pParse uintptr, pExpr uintptr, iTab int32, allowBloom int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var addr, addrBloom, addrOnce, i, i1, nVal, r1, r2, rc, regBloom, v1 int32
	var p, pCopy, pE2, pEList, pItem, pKeyInfo, pLeft, pList, pSelect, pSig, v, v2 uintptr
	var _ /* affinity at bp+28 */ int8
	var _ /* dest at bp+0 */ TSelectDest
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrBloom, addrOnce, i, i1, nVal, p, pCopy, pE2, pEList, pItem, pKeyInfo, pLeft, pList, pSelect, pSig, r1, r2, rc, regBloom, v, v1, v2
	addrOnce = 0          /* the LHS of the IN operator */
	pKeyInfo = uintptr(0) /* The prepared statement under construction */
	pSig = uintptr(0)     /* Signature for this subroutine */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	/* The evaluation of the IN must be repeated every time it
	 ** is encountered if any of the following is true:
	 **
	 **    *  The right-hand side is a correlated subquery
	 **    *  The right-hand side is an expression list containing variables
	 **    *  We are inside a trigger
	 **
	 ** If all of the above are false, then we can compute the RHS just once
	 ** and reuse it many names.
	 */
	if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_VarSelect)) != libc.Uint32FromInt32(0)) && (*TParse)(unsafe.Pointer(pParse)).FiSelfTab == 0 {
		/* Reuse of the RHS is allowed
		 **
		 ** Compute a signature for the RHS of the IN operator to facility
		 ** finding and reusing prior instances of the same IN operator.
		 */
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) && (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FselFlags&uint32(SF_All) == uint32(0) {
			pSig = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(24))
			if pSig != 0 {
				(*TSubrtnSig)(unsafe.Pointer(pSig)).FselId = int32((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FselId)
				(*TSubrtnSig)(unsafe.Pointer(pSig)).FzAff = _exprINAffinity(tls, pParse, pExpr)
			}
		}
		/* Check to see if there is a prior materialization of the RHS of
		 ** this IN operator.  If there is, then make use of that prior
		 ** materialization rather than recomputing it.
		 */
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subrtn)) != uint32(0) || _findCompatibleInRhsSubrtn(tls, pParse, pExpr, pSig) != 0 {
			addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
			if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
				_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+9546, libc.VaList(bp+40, (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FselId))
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*(*struct {
				FiAddr     int32
				FregReturn int32
			})(unsafe.Pointer(pExpr + 44))).FregReturn, (*(*struct {
				FiAddr     int32
				FregReturn int32
			})(unsafe.Pointer(pExpr + 44))).FiAddr)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), iTab, (*TExpr)(unsafe.Pointer(pExpr)).FiTable)
			_sqlite3VdbeJumpHere(tls, v, addrOnce)
			if pSig != 0 {
				_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TSubrtnSig)(unsafe.Pointer(pSig)).FzAff)
				_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSig)
			}
			return
		}
		/* Begin coding the subroutine */
		**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Subrtn))
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		(*(*struct {
			FiAddr     int32
			FregReturn int32
		})(unsafe.Pointer(pExpr + 44))).FregReturn = v1
		(*(*struct {
			FiAddr     int32
			FregReturn int32
		})(unsafe.Pointer(pExpr + 44))).FiAddr = _sqlite3VdbeAddOp2(tls, v, int32(OP_BeginSubrtn), 0, (*(*struct {
			FiAddr     int32
			FregReturn int32
		})(unsafe.Pointer(pExpr + 44))).FregReturn) + int32(1)
		if pSig != 0 {
			(*TSubrtnSig)(unsafe.Pointer(pSig)).FbComplete = uint8(0)
			(*TSubrtnSig)(unsafe.Pointer(pSig)).FiAddr = (*(*struct {
				FiAddr     int32
				FregReturn int32
			})(unsafe.Pointer(pExpr + 44))).FiAddr
			(*TSubrtnSig)(unsafe.Pointer(pSig)).FregReturn = (*(*struct {
				FiAddr     int32
				FregReturn int32
			})(unsafe.Pointer(pExpr + 44))).FregReturn
			(*TSubrtnSig)(unsafe.Pointer(pSig)).FiTable = iTab
			(*TParse)(unsafe.Pointer(pParse)).FmSubrtnSig = uint8(int32(1) << ((*TSubrtnSig)(unsafe.Pointer(pSig)).FselId & int32(7)))
			_sqlite3VdbeChangeP4(tls, v, -int32(1), pSig, -int32(18))
		}
		addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
	}
	/* Check to see if this is a vector IN operator */
	pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
	nVal = _sqlite3ExprVectorSize(tls, pLeft)
	/* Construct the ephemeral table that will contain the content of
	 ** RHS of the IN operator.
	 */
	(*TExpr)(unsafe.Pointer(pExpr)).FiTable = iTab
	addr = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TExpr)(unsafe.Pointer(pExpr)).FiTable, nVal)
	pKeyInfo = _sqlite3KeyInfoAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, nVal, int32(1))
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
		/* Case 1:     expr IN (SELECT ...)
		 **
		 ** Generate code to write the results of the select into the temporary
		 ** table allocated and opened above.
		 */
		pSelect = *(*uintptr)(unsafe.Pointer(pExpr + 20))
		pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList
		if addrOnce != 0 {
			v2 = __ccgo_ts + 1711
		} else {
			v2 = __ccgo_ts + 9569
		}
		_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+9581, libc.VaList(bp+40, v2, (*TSelect)(unsafe.Pointer(pSelect)).FselId))
		/* If the LHS and RHS of the IN operator do not match, that
		 ** error will have been caught long before we reach this point. */
		if (*TExprList)(unsafe.Pointer(pEList)).FnExpr == nVal {
			addrBloom = 0
			_sqlite3SelectDestInit(tls, bp, int32(SRT_Set), iTab)
			(**(**TSelectDest)(__ccgo_up(bp))).FzAffSdst = _exprINAffinity(tls, pParse, pExpr)
			(*TSelect)(unsafe.Pointer(pSelect)).FiLimit = 0
			if addrOnce != 0 && allowBloom != 0 && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_BloomFilter)) == uint32(0) {
				v2 = pParse + 48
				*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
				v1 = *(*int32)(unsafe.Pointer(v2))
				regBloom = v1
				addrBloom = _sqlite3VdbeAddOp2(tls, v, int32(OP_Blob), int32(10000), regBloom)
				(**(**TSelectDest)(__ccgo_up(bp))).FiSDParm2 = regBloom
			}
			/* Caused by OOM in sqlite3KeyInfoAlloc() */
			pCopy = _sqlite3SelectDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSelect, 0)
			if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
				v1 = int32(1)
			} else {
				v1 = _sqlite3Select(tls, pParse, pCopy, bp)
			}
			rc = v1
			_sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pCopy)
			_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (**(**TSelectDest)(__ccgo_up(bp))).FzAffSdst)
			if addrBloom != 0 {
				/* Remember that location of the Bloom filter in the P3 operand
				 ** of the OP_Once that began this subroutine. tag-202407032019 */
				(*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, v, addrOnce))).Fp3 = (**(**TSelectDest)(__ccgo_up(bp))).FiSDParm2
				if (**(**TSelectDest)(__ccgo_up(bp))).FiSDParm2 == 0 {
					/* If the Bloom filter won't actually be used, keep it small */
					(*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, v, addrBloom))).Fp1 = int32(10)
				}
			}
			if rc != 0 {
				_sqlite3KeyInfoUnref(tls, pKeyInfo)
				return
			}
			/* OOM will cause exit after sqlite3Select() */
			i = 0
			for {
				if !(i < nVal) {
					break
				}
				p = _sqlite3VectorFieldSubexpr(tls, pLeft, i)
				*(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(i)*4)) = _sqlite3BinaryCompareCollSeq(tls, pParse, p, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr)
				goto _7
			_7:
				;
				i = i + 1
			}
		}
	} else {
		if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != uintptr(0) {
			pList = *(*uintptr)(unsafe.Pointer(pExpr + 20))
			**(**int8)(__ccgo_up(bp + 28)) = _sqlite3ExprAffinity(tls, pLeft)
			if int32(**(**int8)(__ccgo_up(bp + 28))) <= int32(SQLITE_AFF_NONE) {
				**(**int8)(__ccgo_up(bp + 28)) = int8(SQLITE_AFF_BLOB)
			} else {
				if int32(**(**int8)(__ccgo_up(bp + 28))) == int32(SQLITE_AFF_REAL) {
					**(**int8)(__ccgo_up(bp + 28)) = int8(SQLITE_AFF_NUMERIC)
				}
			}
			if pKeyInfo != 0 {
				*(*uintptr)(unsafe.Pointer(pKeyInfo + 20)) = _sqlite3ExprCollSeq(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
			}
			/* Loop through each expression in <exprlist>. */
			r1 = _sqlite3GetTempReg(tls, pParse)
			r2 = _sqlite3GetTempReg(tls, pParse)
			i1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr
			pItem = pList + 8
			for {
				if !(i1 > 0) {
					break
				}
				pE2 = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr
				/* If the expression is not constant then we will need to
				 ** disable the test that was generated above that makes sure
				 ** this code only executes once.  Because for a non-constant
				 ** expression we need to rerun this code each time.
				 */
				if addrOnce != 0 && !(_sqlite3ExprIsConstant(tls, pParse, pE2) != 0) {
					_sqlite3VdbeChangeToNoop(tls, v, addrOnce-int32(1))
					_sqlite3VdbeChangeToNoop(tls, v, addrOnce)
					**(**Tu32)(__ccgo_up(pExpr + 4)) &= ^uint32(libc.Int32FromInt32(EP_Subrtn))
					addrOnce = 0
				}
				/* Evaluate the expression and insert it into the temp table */
				_sqlite3ExprCode(tls, pParse, pE2, r1)
				_sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), r1, int32(1), r2, bp+28, int32(1))
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iTab, r2, r1, int32(1))
				goto _8
			_8:
				;
				i1 = i1 - 1
				pItem += 20
			}
			_sqlite3ReleaseTempReg(tls, pParse, r1)
			_sqlite3ReleaseTempReg(tls, pParse, r2)
		}
	}
	if pSig != 0 {
		(*TSubrtnSig)(unsafe.Pointer(pSig)).FbComplete = uint8(1)
	}
	if pKeyInfo != 0 {
		_sqlite3VdbeChangeP4(tls, v, addr, pKeyInfo, -int32(9))
	}
	if addrOnce != 0 {
		_sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), iTab)
		_sqlite3VdbeJumpHere(tls, v, addrOnce)
		/* Subroutine return */
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*(*struct {
			FiAddr     int32
			FregReturn int32
		})(unsafe.Pointer(pExpr + 44))).FregReturn, (*(*struct {
			FiAddr     int32
			FregReturn int32
		})(unsafe.Pointer(pExpr + 44))).FiAddr, int32(1))
		_sqlite3ClearTempRegCache(tls, pParse)
	}
}

// C documentation
//
//	/*
//	** Generate code for the trigger program associated with trigger p on
//	** table pTab. The reg, orconf and ignoreJump parameters passed to this
//	** function are the same as those described in the header function for
//	** sqlite3CodeRowTrigger()
//	*/
func _sqlite3CodeRowTriggerDirect(tls *libc.TLS, pParse uintptr, p uintptr, pTab uintptr, reg int32, orconf int32, ignoreJump int32) {
	var bRecursive, v1 int32
	var pPrg, v, v2 uintptr
	_, _, _, _, _ = bRecursive, pPrg, v, v1, v2
	v = _sqlite3GetVdbe(tls, pParse)
	pPrg = _getRowTrigger(tls, pParse, p, pTab, orconf)
	/* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
	 ** is a pointer to the sub-vdbe containing the trigger program.  */
	if pPrg != 0 {
		bRecursive = libc.BoolInt32((*TTrigger)(unsafe.Pointer(p)).FzName != 0 && uint64(0) == (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_RecTriggers))
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		_sqlite3VdbeAddOp4(tls, v, int32(OP_Program), reg, ignoreJump, v1, (*TTriggerPrg)(unsafe.Pointer(pPrg)).FpProgram, -int32(4))
		/* Set the P5 operand of the OP_Program instruction to non-zero if
		 ** recursive invocation of this trigger program is disallowed. Recursive
		 ** invocation is disallowed if (a) the sub-program is really a trigger,
		 ** not a foreign key action, and (b) the flag to enable recursive triggers
		 ** is clear.  */
		_sqlite3VdbeChangeP5(tls, v, uint16(bRecursive))
	}
}

// C documentation
//
//	/*
//	** Generate code for scalar subqueries used as a subquery expression
//	** or EXISTS operator:
//	**
//	**     (SELECT a FROM b)          -- subquery
//	**     EXISTS (SELECT a FROM b)   -- EXISTS subquery
//	**
//	** The pExpr parameter is the SELECT or EXISTS operator to be coded.
//	**
//	** Return the register that holds the result.  For a multi-column SELECT,
//	** the result is stored in a contiguous array of registers and the
//	** return value is the register of the left-most result column.
//	** Return 0 if an error occurs.
//	*/
func _sqlite3CodeSubselect(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var addrOnce, nReg, rReg, v1 int32
	var db, pLeft, pLimit, pSel, v, v2 uintptr
	var _ /* dest at bp+0 */ TSelectDest
	_, _, _, _, _, _, _, _, _, _ = addrOnce, db, nReg, pLeft, pLimit, pSel, rReg, v, v1, v2
	addrOnce = 0 /* Address of OP_Once at top of subroutine */
	rReg = 0     /* New limit expression */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return 0
	}
	pSel = *(*uintptr)(unsafe.Pointer(pExpr + 20))
	/* If this routine has already been coded, then invoke it as a
	 ** subroutine. */
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subrtn)) != uint32(0) {
		_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+9600, libc.VaList(bp+40, (*TSelect)(unsafe.Pointer(pSel)).FselId))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*(*struct {
			FiAddr     int32
			FregReturn int32
		})(unsafe.Pointer(pExpr + 44))).FregReturn, (*(*struct {
			FiAddr     int32
			FregReturn int32
		})(unsafe.Pointer(pExpr + 44))).FiAddr)
		return (*TExpr)(unsafe.Pointer(pExpr)).FiTable
	}
	/* Begin coding the subroutine */
	**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Subrtn))
	v2 = pParse + 48
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	v1 = *(*int32)(unsafe.Pointer(v2))
	(*(*struct {
		FiAddr     int32
		FregReturn int32
	})(unsafe.Pointer(pExpr + 44))).FregReturn = v1
	(*(*struct {
		FiAddr     int32
		FregReturn int32
	})(unsafe.Pointer(pExpr + 44))).FiAddr = _sqlite3VdbeAddOp2(tls, v, int32(OP_BeginSubrtn), 0, (*(*struct {
		FiAddr     int32
		FregReturn int32
	})(unsafe.Pointer(pExpr + 44))).FregReturn) + int32(1)
	/* The evaluation of the EXISTS/SELECT must be repeated every time it
	 ** is encountered if any of the following is true:
	 **
	 **    *  The right-hand side is a correlated subquery
	 **    *  The right-hand side is an expression list containing variables
	 **    *  We are inside a trigger
	 **
	 ** If all of the above are false, then we can run this code just once
	 ** save the results, and reuse the same result on subsequent invocations.
	 */
	if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_VarSelect)) != libc.Uint32FromInt32(0)) {
		addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
	}
	/* For a SELECT, generate code to put the values for all columns of
	 ** the first row into an array of registers and return the index of
	 ** the first register.
	 **
	 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
	 ** into a register and return that register number.
	 **
	 ** In both cases, the query is augmented with "LIMIT 1".  Any
	 ** preexisting limit is discarded in place of the new LIMIT 1.
	 */
	if addrOnce != 0 {
		v2 = __ccgo_ts + 1711
	} else {
		v2 = __ccgo_ts + 9569
	}
	_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+9618, libc.VaList(bp+40, v2, (*TSelect)(unsafe.Pointer(pSel)).FselId))
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_SELECT) {
		v1 = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpEList)).FnExpr
	} else {
		v1 = int32(1)
	}
	nReg = v1
	_sqlite3SelectDestInit(tls, bp, 0, (*TParse)(unsafe.Pointer(pParse)).FnMem+int32(1))
	**(**int32)(__ccgo_up(pParse + 48)) += nReg
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_SELECT) {
		(**(**TSelectDest)(__ccgo_up(bp))).FeDest = uint8(SRT_Mem)
		if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_Distinct) != 0 && (*TSelect)(unsafe.Pointer(pSel)).FpLimit != 0 && (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpLimit)).FpRight != 0 {
			/* If there is both a DISTINCT and an OFFSET clause, then allocate
			 ** a separate dest.iSdst array for sqlite3Select() and other
			 ** routines to populate. In this case results will be copied over
			 ** into the dest.iSDParm array only after OFFSET processing. This
			 ** ensures that in the case where OFFSET excludes all rows, the
			 ** dest.iSDParm array is not left populated with the contents of the
			 ** last row visited - it should be all NULLs if all rows were
			 ** excluded by OFFSET.  */
			(**(**TSelectDest)(__ccgo_up(bp))).FiSdst = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
			**(**int32)(__ccgo_up(pParse + 48)) += nReg
		} else {
			(**(**TSelectDest)(__ccgo_up(bp))).FiSdst = (**(**TSelectDest)(__ccgo_up(bp))).FiSDParm
		}
		(**(**TSelectDest)(__ccgo_up(bp))).FnSdst = nReg
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, (**(**TSelectDest)(__ccgo_up(bp))).FiSDParm, (*TParse)(unsafe.Pointer(pParse)).FnMem)
	} else {
		(**(**TSelectDest)(__ccgo_up(bp))).FeDest = uint8(SRT_Exists)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (**(**TSelectDest)(__ccgo_up(bp))).FiSDParm)
	}
	if (*TSelect)(unsafe.Pointer(pSel)).FpLimit != 0 {
		/* The subquery already has a limit.  If the pre-existing limit X is
		 ** not already integer value 1 or 0, then make the new limit X<>0 so that
		 ** the new limit is either 1 or 0 */
		pLeft = (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpLimit)).FpLeft
		if libc.BoolInt32((*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != uint32(0)) == 0 || *(*int32)(unsafe.Pointer(pLeft + 8)) != int32(1) && *(*int32)(unsafe.Pointer(pLeft + 8)) != 0 {
			db = (*TParse)(unsafe.Pointer(pParse)).Fdb
			pLimit = _sqlite3ExprInt32(tls, db, 0)
			if pLimit != 0 {
				(*TExpr)(unsafe.Pointer(pLimit)).FaffExpr = int8(SQLITE_AFF_NUMERIC)
				pLimit = _sqlite3PExpr(tls, pParse, int32(TK_NE), _sqlite3ExprDup(tls, db, pLeft, 0), pLimit)
			}
			_sqlite3ExprDeferredDelete(tls, pParse, pLeft)
			(*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpLimit)).FpLeft = pLimit
		}
	} else {
		/* If there is no pre-existing limit add a limit of 1 */
		pLimit = _sqlite3ExprInt32(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(1))
		(*TSelect)(unsafe.Pointer(pSel)).FpLimit = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), pLimit, uintptr(0))
	}
	(*TSelect)(unsafe.Pointer(pSel)).FiLimit = 0
	if _sqlite3Select(tls, pParse, pSel, bp) != 0 {
		(*TExpr)(unsafe.Pointer(pExpr)).Fop2 = (*TExpr)(unsafe.Pointer(pExpr)).Fop
		(*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_ERROR)
		return 0
	}
	v1 = (**(**TSelectDest)(__ccgo_up(bp))).FiSDParm
	rReg = v1
	(*TExpr)(unsafe.Pointer(pExpr)).FiTable = v1
	if addrOnce != 0 {
		_sqlite3VdbeJumpHere(tls, v, addrOnce)
	}
	/* Subroutine return */
	_sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*(*struct {
		FiAddr     int32
		FregReturn int32
	})(unsafe.Pointer(pExpr + 44))).FregReturn, (*(*struct {
		FiAddr     int32
		FregReturn int32
	})(unsafe.Pointer(pExpr + 44))).FiAddr, int32(1))
	_sqlite3ClearTempRegCache(tls, pParse)
	return rReg
}

// C documentation
//
//	/*
//	** The most recently coded instruction was an OP_Column to retrieve the
//	** i-th column of table pTab. This routine sets the P4 parameter of the
//	** OP_Column to the default value, if any.
//	**
//	** The default value of a column is specified by a DEFAULT clause in the
//	** column definition. This was either supplied by the user when the table
//	** was created, or added later to the table definition by an ALTER TABLE
//	** command. If the latter, then the row-records in the table btree on disk
//	** may not contain a value for the column and the default value, taken
//	** from the P4 parameter of the OP_Column instruction, is returned instead.
//	** If the former, then all row-records are guaranteed to include a value
//	** for the column and the P4 value is not required.
//	**
//	** Column definitions created by an ALTER TABLE command may only have
//	** literal default values specified: a number, null or a string. (If a more
//	** complicated default expression value was provided, it is evaluated
//	** when the ALTER TABLE is executed and one of the literal values written
//	** into the sqlite_schema table.)
//	**
//	** Therefore, the P4 parameter is only required if the default value for
//	** the column is a literal number, string or null. The sqlite3ValueFromExpr()
//	** function is capable of transforming these types of expressions into
//	** sqlite3_value objects.
//	**
//	** If column as REAL affinity and the table is an ordinary b-tree table
//	** (not a virtual table) then the value might have been stored as an
//	** integer.  In that case, add an OP_RealAffinity opcode to make sure
//	** it has been converted into REAL.
//	*/
func _sqlite3ColumnDefault(tls *libc.TLS, v uintptr, pTab uintptr, i int32, iReg int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var enc Tu8
	var pCol uintptr
	var _ /* pValue at bp+0 */ uintptr
	_, _ = enc, pCol
	pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12
	if (*TColumn)(unsafe.Pointer(pCol)).FiDflt != 0 {
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		enc = (*Tsqlite3)(unsafe.Pointer(_sqlite3VdbeDb(tls, v))).Fenc
		_sqlite3ValueFromExpr(tls, _sqlite3VdbeDb(tls, v), _sqlite3ColumnExpr(tls, pTab, pCol), enc, uint8((*TColumn)(unsafe.Pointer(pCol)).Faffinity), bp)
		if **(**uintptr)(__ccgo_up(bp)) != 0 {
			_sqlite3VdbeAppendP4(tls, v, **(**uintptr)(__ccgo_up(bp)), -int32(11))
		}
	}
	if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) == int32(SQLITE_AFF_REAL) && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
		_sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), iReg)
	}
}

// C documentation
//
//	/*
//	** Return the expression associated with a column.  The expression might be
//	** the DEFAULT clause or the AS clause of a generated column.
//	** Return NULL if the column has no associated expression.
//	*/
func _sqlite3ColumnExpr(tls *libc.TLS, pTab uintptr, pCol uintptr) (r uintptr) {
	if int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) == 0 {
		return uintptr(0)
	}
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
		return uintptr(0)
	}
	if (*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pTab + 44))).FpDfltList == uintptr(0) {
		return uintptr(0)
	}
	if (*TExprList)(unsafe.Pointer((*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pTab + 44))).FpDfltList)).FnExpr < int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) {
		return uintptr(0)
	}
	return (*(*TExprList_item)(unsafe.Pointer((*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pTab + 44))).FpDfltList + 8 + uintptr(int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt)-int32(1))*20))).FpExpr
}

// C documentation
//
//	/*
//	** Return the index of a column in a table.  Return -1 if the column
//	** is not contained in the table.
//	*/
func _sqlite3ColumnIndex(tls *libc.TLS, pTab uintptr, zCol uintptr) (r int32) {
	var aCol uintptr
	var h Tu8
	var i, nCol int32
	_, _, _, _ = aCol, h, i, nCol
	h = _sqlite3StrIHash(tls, zCol)
	aCol = (*TTable)(unsafe.Pointer(pTab)).FaCol
	nCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
	/* See if the aHx gives us a lucky match */
	i = int32(**(**Tu8)(__ccgo_up(pTab + 64 + uintptr(uint32(h)%uint32(16)))))
	if int32((**(**TColumn)(__ccgo_up(aCol + uintptr(i)*12))).FhName) == int32(h) && _sqlite3StrICmp(tls, (**(**TColumn)(__ccgo_up(aCol + uintptr(i)*12))).FzCnName, zCol) == 0 {
		return i
	}
	/* No lucky match from the hash table.  Do a full search. */
	i = 0
	for int32(1) != 0 {
		if int32((**(**TColumn)(__ccgo_up(aCol + uintptr(i)*12))).FhName) == int32(h) && _sqlite3StrICmp(tls, (**(**TColumn)(__ccgo_up(aCol + uintptr(i)*12))).FzCnName, zCol) == 0 {
			return i
		}
		i = i + 1
		if i >= nCol {
			break
		}
	}
	return -int32(1)
}

// C documentation
//
//	/*
//	** Set the collating sequence name for a column.
//	*/
func _sqlite3ColumnSetColl(tls *libc.TLS, db uintptr, pCol uintptr, zColl uintptr) {
	var n, nColl Ti64
	var zNew, v1 uintptr
	_, _, _, _ = n, nColl, zNew, v1
	n = int64(_sqlite3Strlen30(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) + int32(1))
	if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_HASTYPE) != 0 {
		n = n + int64(_sqlite3Strlen30(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n))+int32(1))
	}
	nColl = int64(_sqlite3Strlen30(tls, zColl) + int32(1))
	zNew = _sqlite3DbRealloc(tls, db, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, uint64(nColl+n))
	if zNew != 0 {
		(*TColumn)(unsafe.Pointer(pCol)).FzCnName = zNew
		libc.Xmemcpy(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n), zColl, uint32(nColl))
		v1 = pCol + 10
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(COLFLAG_HASCOLL))
	}
}

// C documentation
//
//	/*
//	** Set the expression associated with a column.  This is usually
//	** the DEFAULT value, but might also be the expression that computes
//	** the value for a generated column.
//	*/
func _sqlite3ColumnSetExpr(tls *libc.TLS, pParse uintptr, pTab uintptr, pCol uintptr, pExpr uintptr) {
	var pList uintptr
	var v1 int32
	_, _ = pList, v1
	pList = (*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pTab + 44))).FpDfltList
	if int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) == 0 || pList == uintptr(0) || (*TExprList)(unsafe.Pointer(pList)).FnExpr < int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) {
		if pList == uintptr(0) {
			v1 = int32(1)
		} else {
			v1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr + int32(1)
		}
		(*TColumn)(unsafe.Pointer(pCol)).FiDflt = uint16(v1)
		(*(*struct {
			FaddColOffset int32
			FpFKey        uintptr
			FpDfltList    uintptr
		})(unsafe.Pointer(pTab + 44))).FpDfltList = _sqlite3ExprListAppend(tls, pParse, pList, pExpr)
	} else {
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt)-int32(1))*20))).FpExpr)
		(*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt)-int32(1))*20))).FpExpr = pExpr
	}
}

// C documentation
//
//	/*
//	** Return the declared type of a column.  Or return zDflt if the column
//	** has no declared type.
//	**
//	** The column type is an extra string stored after the zero-terminator on
//	** the column name if and only if the COLFLAG_HASTYPE flag is set.
//	*/
func _sqlite3ColumnType(tls *libc.TLS, pCol uintptr, zDflt uintptr) (r uintptr) {
	if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_HASTYPE) != 0 {
		return (*TColumn)(unsafe.Pointer(pCol)).FzCnName + uintptr(libc.Xstrlen(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) + uintptr(1)
	} else {
		if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf0>>4)) != 0 {
			return _sqlite3StdType[int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf0>>4))-int32(1)]
		} else {
			return zDflt
		}
	}
	return r
}

// C documentation
//
//	/*
//	** Given an expression list (which is really the list of expressions
//	** that form the result set of a SELECT statement) compute appropriate
//	** column names for a table that would hold the expression list.
//	**
//	** All column names will be unique.
//	**
//	** Only the column names are computed.  Column.zType, Column.zColl,
//	** and other fields of Column are zeroed.
//	**
//	** Return SQLITE_OK on success.  If a memory allocation error occurs,
//	** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
//	**
//	** The only guarantee that SQLite makes about column names is that if the
//	** column has an AS clause assigning it a name, that will be the name used.
//	** That is the only documented guarantee.  However, countless applications
//	** developed over the years have made baseless assumptions about column names
//	** and will break if those assumptions changes.  Hence, use extreme caution
//	** when modifying this routine to avoid breaking legacy.
//	**
//	** See Also: sqlite3GenerateColumnNames()
//	*/
func _sqlite3ColumnsFromExprList(tls *libc.TLS, pParse uintptr, pEList uintptr, pnCol uintptr, paCol uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var aCol, db, pCol, pColExpr, pCollide, pTab, pX, zName, v2, v3 uintptr
	var i, iCol, j, nCol, nName int32
	var v5 bool
	var v8 Tu32
	var _ /* cnt at bp+0 */ Tu32
	var _ /* ht at bp+4 */ THash
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aCol, db, i, iCol, j, nCol, nName, pCol, pColExpr, pCollide, pTab, pX, zName, v2, v3, v5, v8
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	_sqlite3HashInit(tls, bp+4)
	if pEList != 0 {
		nCol = (*TExprList)(unsafe.Pointer(pEList)).FnExpr
		aCol = _sqlite3DbMallocZero(tls, db, uint64(uint32(12)*uint32(nCol)))
		if nCol > int32(32767) {
			nCol = int32(32767)
		}
	} else {
		nCol = 0
		aCol = uintptr(0)
	}
	**(**Ti16)(__ccgo_up(pnCol)) = int16(nCol)
	**(**uintptr)(__ccgo_up(paCol)) = aCol
	i = 0
	pCol = aCol
	for {
		if !(i < nCol && !((*TParse)(unsafe.Pointer(pParse)).FnErr != 0)) {
			break
		}
		pX = pEList + 8 + uintptr(i)*20
		/* Get an appropriate name for the column
		 */
		v2 = (*TExprList_item)(unsafe.Pointer(pX)).FzEName
		zName = v2
		if v2 != uintptr(0) && int32(uint32(*(*uint16)(unsafe.Pointer(pX + 8 + 4))&0x3>>0)) == ENAME_NAME {
			/* If the column contains an "AS <name>" phrase, use <name> as the name */
		} else {
			pColExpr = _sqlite3ExprSkipCollateAndLikely(tls, (*TExprList_item)(unsafe.Pointer(pX)).FpExpr)
			for pColExpr != uintptr(0) && int32((*TExpr)(unsafe.Pointer(pColExpr)).Fop) == int32(TK_DOT) {
				pColExpr = (*TExpr)(unsafe.Pointer(pColExpr)).FpRight
			}
			if int32((*TExpr)(unsafe.Pointer(pColExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pColExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)|libc.Int32FromInt32(EP_Subrtn)) == uint32(0) && (*TExpr)(unsafe.Pointer(pColExpr)).Fy.FpTab != uintptr(0) {
				/* For columns use the column name name */
				iCol = int32((*TExpr)(unsafe.Pointer(pColExpr)).FiColumn)
				pTab = (*TExpr)(unsafe.Pointer(pColExpr)).Fy.FpTab
				if iCol < 0 {
					iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey)
				}
				if iCol >= 0 {
					v2 = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName
				} else {
					v2 = __ccgo_ts + 19186
				}
				zName = v2
			} else {
				if int32((*TExpr)(unsafe.Pointer(pColExpr)).Fop) == int32(TK_ID) {
					zName = *(*uintptr)(unsafe.Pointer(pColExpr + 8))
				} else {
					/* Use the original text of the column expression as its name */
					/* pointer comparison intended */
				}
			}
		}
		if zName != 0 && !(_sqlite3IsTrueOrFalse(tls, zName) != 0) {
			zName = _sqlite3DbStrDup(tls, db, zName)
		} else {
			zName = _sqlite3MPrintf(tls, db, __ccgo_ts+21866, libc.VaList(bp+32, i+int32(1)))
		}
		/* Make sure the column name is unique.  If the name is not unique,
		 ** append an integer to the name so that it becomes unique.
		 */
		**(**Tu32)(__ccgo_up(bp)) = uint32(0)
		for {
			if v5 = zName != 0; v5 {
				v2 = _sqlite3HashFind(tls, bp+4, zName)
				pCollide = v2
			}
			if !(v5 && v2 != uintptr(0)) {
				break
			}
			if int32(uint32(*(*uint16)(unsafe.Pointer(pCollide + 8 + 4))&0x80>>7)) != 0 {
				v3 = pCol + 10
				*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | libc.Int32FromInt32(COLFLAG_NOEXPAND))
			}
			nName = _sqlite3Strlen30(tls, zName)
			if nName > 0 {
				j = nName - int32(1)
				for {
					if !(j > 0 && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zName + uintptr(j))))])&int32(0x04) != 0) {
						break
					}
					goto _7
				_7:
					;
					j = j - 1
				}
				if int32(**(**int8)(__ccgo_up(zName + uintptr(j)))) == int32(':') {
					nName = j
				}
			}
			**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp)) + 1
			v8 = **(**Tu32)(__ccgo_up(bp))
			zName = _sqlite3MPrintf(tls, db, __ccgo_ts+21875, libc.VaList(bp+32, nName, zName, v8))
			_sqlite3ProgressCheck(tls, pParse)
			if **(**Tu32)(__ccgo_up(bp)) > uint32(3) {
				Xsqlite3_randomness(tls, int32(4), bp)
			}
		}
		(*TColumn)(unsafe.Pointer(pCol)).FzCnName = zName
		(*TColumn)(unsafe.Pointer(pCol)).FhName = _sqlite3StrIHash(tls, zName)
		if int32(uint32(*(*uint16)(unsafe.Pointer(pX + 8 + 4))&0x100>>8)) != 0 {
			v2 = pCol + 10
			*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(COLFLAG_NOEXPAND))
		}
		if zName != 0 && _sqlite3HashInsert(tls, bp+4, zName, pX) == pX {
			_sqlite3OomFault(tls, db)
		}
		goto _1
	_1:
		;
		i = i + 1
		pCol += 12
	}
	_sqlite3HashClear(tls, bp+4)
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		j = 0
		for {
			if !(j < i) {
				break
			}
			_sqlite3DbFree(tls, db, (**(**TColumn)(__ccgo_up(aCol + uintptr(j)*12))).FzCnName)
			goto _10
		_10:
			;
			j = j + 1
		}
		_sqlite3DbFree(tls, db, aCol)
		**(**uintptr)(__ccgo_up(paCol)) = uintptr(0)
		**(**Ti16)(__ccgo_up(pnCol)) = 0
		return (*TParse)(unsafe.Pointer(pParse)).Frc
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This routine is called when a commit occurs.
//	*/
func _sqlite3CommitInternalChanges(tls *libc.TLS, db uintptr) {
	**(**Tu32)(__ccgo_up(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaChange))
}

func _sqlite3CompileOptions(tls *libc.TLS, pnOpt uintptr) (r uintptr) {
	**(**int32)(__ccgo_up(pnOpt)) = int32(libc.Uint32FromInt64(228) / libc.Uint32FromInt64(4))
	return uintptr(unsafe.Pointer(&_sqlite3azCompileOpt))
}

/************** End of ctime.c ***********************************************/
/************** Begin file global.c ******************************************/
/*
** 2008 June 13
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains definitions of global variables and constants.
 */
/* #include "sqliteInt.h" */

// C documentation
//
//	/*
//	** This routine generates code to finish the INSERT or UPDATE operation
//	** that was started by a prior call to sqlite3GenerateConstraintChecks.
//	** A consecutive range of registers starting at regNewData contains the
//	** rowid and the content to be inserted.
//	**
//	** The arguments to this routine should be the same as the first six
//	** arguments to sqlite3GenerateConstraintChecks.
//	*/
func _sqlite3CompleteInsertion(tls *libc.TLS, pParse uintptr, pTab uintptr, iDataCur int32, iIdxCur int32, regNewData int32, aRegIdx uintptr, update_flags int32, appendBias int32, useSeekResult int32) {
	var i, v2 int32
	var pIdx, v uintptr
	var pik_flags Tu8
	_, _, _, _, _ = i, pIdx, pik_flags, v, v2 /* Loop counter */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	/* This table is not a VIEW */
	i = 0
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		/* All REPLACE indexes are at the end of the list */
		if **(**int32)(__ccgo_up(aRegIdx + uintptr(i)*4)) == 0 {
			goto _1
		}
		if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), **(**int32)(__ccgo_up(aRegIdx + uintptr(i)*4)), _sqlite3VdbeCurrentAddr(tls, v)+int32(2))
		}
		if useSeekResult != 0 {
			v2 = int32(OPFLAG_USESEEKRESULT)
		} else {
			v2 = 0
		}
		pik_flags = uint8(v2)
		if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
			pik_flags = uint8(int32(pik_flags) | libc.Int32FromInt32(OPFLAG_NCHANGE))
			pik_flags = uint8(int32(pik_flags) | update_flags&libc.Int32FromInt32(OPFLAG_SAVEPOSITION))
			if update_flags == 0 {
				_codeWithoutRowidPreupdate(tls, pParse, pTab, iIdxCur+i, **(**int32)(__ccgo_up(aRegIdx + uintptr(i)*4)))
			}
		}
		if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 {
			v2 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
		} else {
			v2 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
		}
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iIdxCur+i, **(**int32)(__ccgo_up(aRegIdx + uintptr(i)*4)), **(**int32)(__ccgo_up(aRegIdx + uintptr(i)*4))+int32(1), v2)
		_sqlite3VdbeChangeP5(tls, v, uint16(pik_flags))
		goto _1
	_1:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		i = i + 1
	}
	if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
		return
	}
	if (*TParse)(unsafe.Pointer(pParse)).Fnested != 0 {
		pik_flags = uint8(0)
	} else {
		pik_flags = uint8(OPFLAG_NCHANGE)
		if update_flags != 0 {
			v2 = update_flags
		} else {
			v2 = int32(OPFLAG_LASTROWID)
		}
		pik_flags = uint8(int32(pik_flags) | v2)
	}
	if appendBias != 0 {
		pik_flags = uint8(int32(pik_flags) | libc.Int32FromInt32(OPFLAG_APPEND))
	}
	if useSeekResult != 0 {
		pik_flags = uint8(int32(pik_flags) | libc.Int32FromInt32(OPFLAG_USESEEKRESULT))
	}
	_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iDataCur, **(**int32)(__ccgo_up(aRegIdx + uintptr(i)*4)), regNewData)
	if !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) {
		_sqlite3VdbeAppendP4(tls, v, pTab, -int32(5))
	}
	_sqlite3VdbeChangeP5(tls, v, uint16(pik_flags))
}

// C documentation
//
//	/*
//	** All regular columns for table pTab have been puts into registers
//	** starting with iRegStore.  The registers that correspond to STORED
//	** or VIRTUAL columns have not yet been initialized.  This routine goes
//	** back and computes the values for those columns based on the previously
//	** computed normal columns.
//	*/
func _sqlite3ComputeGeneratedColumns(tls *libc.TLS, pParse uintptr, iRegStore int32, pTab uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var eProgress, i, ii, jj, x, v2 int32
	var pCol, pOp, pRedo, zP4, v4 uintptr
	var _ /* w at bp+0 */ TWalker
	_, _, _, _, _, _, _, _, _, _, _ = eProgress, i, ii, jj, pCol, pOp, pRedo, x, zP4, v2, v4
	/* Before computing generated columns, first go through and make sure
	 ** that appropriate affinity has been applied to the regular columns
	 */
	_sqlite3TableAffinity(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, pTab, iRegStore)
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasStored) != uint32(0) {
		pOp = _sqlite3VdbeGetLastOp(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe)
		if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Affinity) {
			zP4 = *(*uintptr)(unsafe.Pointer(pOp + 16))
			v2 = libc.Int32FromInt32(0)
			jj = v2
			ii = v2
			for {
				if !(**(**int8)(__ccgo_up(zP4 + uintptr(jj))) != 0) {
					break
				}
				if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(ii)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
					goto _1
				}
				if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(ii)*12))).FcolFlags)&int32(COLFLAG_STORED) != 0 {
					**(**int8)(__ccgo_up(zP4 + uintptr(jj))) = int8(SQLITE_AFF_NONE)
				}
				jj = jj + 1
				goto _1
			_1:
				;
				ii = ii + 1
			}
		} else {
			if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_TypeCheck) {
				/* If an OP_TypeCheck was generated because the table is STRICT,
				 ** then set the P3 operand to indicate that generated columns should
				 ** not be checked */
				(*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 = int32(1)
			}
		}
	}
	/* Because there can be multiple generated columns that refer to one another,
	 ** this is a two-pass algorithm.  On the first pass, mark all generated
	 ** columns as "not available".
	 */
	i = 0
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
			break
		}
		if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
			v4 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12 + 10
			*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(COLFLAG_NOTAVAIL))
		}
		goto _3
	_3:
		;
		i = i + 1
	}
	*(*uintptr)(unsafe.Pointer(bp + 24)) = pTab
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_exprColumnFlagUnion)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = uintptr(0)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback2 = uintptr(0)
	/* On the second pass, compute the value of each NOT-AVAILABLE column.
	 ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will
	 ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as
	 ** they are needed.
	 */
	(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = -iRegStore
	for cond := true; cond; cond = pRedo != 0 && eProgress != 0 {
		eProgress = 0
		pRedo = uintptr(0)
		i = 0
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12
			if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_NOTAVAIL) != 0 {
				v4 = pCol + 10
				*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(COLFLAG_BUSY))
				(**(**TWalker)(__ccgo_up(bp))).FeCode = uint16(0)
				_sqlite3WalkExpr(tls, bp, _sqlite3ColumnExpr(tls, pTab, pCol))
				v4 = pCol + 10
				*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) & ^libc.Int32FromInt32(COLFLAG_BUSY))
				if int32((**(**TWalker)(__ccgo_up(bp))).FeCode)&int32(COLFLAG_NOTAVAIL) != 0 {
					pRedo = pCol
					goto _5
				}
				eProgress = int32(1)
				x = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(i))) + iRegStore
				_sqlite3ExprCodeGeneratedColumn(tls, pParse, pTab, pCol, x)
				v4 = pCol + 10
				*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) & ^libc.Int32FromInt32(COLFLAG_NOTAVAIL))
			}
			goto _5
		_5:
			;
			i = i + 1
		}
	}
	if pRedo != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9669, libc.VaList(bp+40, (*TColumn)(unsafe.Pointer(pRedo)).FzCnName))
	}
	(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = 0
}

// C documentation
//
//	/*
//	** This function is called when
//	** the transaction opened by database db has just finished. Locks held
//	** by database connection db have been released.
//	**
//	** This function loops through each entry in the blocked connections
//	** list and does the following:
//	**
//	**   1) If the sqlite3.pBlockingConnection member of a list entry is
//	**      set to db, then set pBlockingConnection=0.
//	**
//	**   2) If the sqlite3.pUnlockConnection member of a list entry is
//	**      set to db, then invoke the configured unlock-notify callback and
//	**      set pUnlockConnection=0.
//	**
//	**   3) If the two steps above mean that pBlockingConnection==0 and
//	**      pUnlockConnection==0, remove the entry from the blocked connections
//	**      list.
//	*/
func _sqlite3ConnectionUnlocked(tls *libc.TLS, db uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var aArg, aDyn, p, pNew, pp, xUnlockNotify, v2 uintptr
	var nArg, v3 int32
	var _ /* aStatic at bp+0 */ [16]uintptr
	_, _, _, _, _, _, _, _, _ = aArg, aDyn, nArg, p, pNew, pp, xUnlockNotify, v2, v3
	xUnlockNotify = uintptr(0) /* Unlock-notify cb to invoke */
	nArg = 0                   /* Arguments to the unlock callback */
	aDyn = uintptr(0)          /* Starter space for aArg[].  No malloc required */
	aArg = bp
	_enterMutex(tls) /* Enter STATIC_MAIN mutex */
	/* This loop runs once for each entry in the blocked-connections list. */
	pp = uintptr(unsafe.Pointer(&_sqlite3BlockedList))
	for {
		if !(**(**uintptr)(__ccgo_up(pp)) != 0) {
			break
		}
		p = **(**uintptr)(__ccgo_up(pp))
		/* Step 1. */
		if (*Tsqlite3)(unsafe.Pointer(p)).FpBlockingConnection == db {
			(*Tsqlite3)(unsafe.Pointer(p)).FpBlockingConnection = uintptr(0)
		}
		/* Step 2. */
		if (*Tsqlite3)(unsafe.Pointer(p)).FpUnlockConnection == db {
			if (*Tsqlite3)(unsafe.Pointer(p)).FxUnlockNotify != xUnlockNotify && nArg != 0 {
				(*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{xUnlockNotify})))(tls, aArg, nArg)
				nArg = 0
			}
			_sqlite3BeginBenignMalloc(tls)
			if !(aDyn != 0) && nArg == int32(libc.Uint32FromInt64(64)/libc.Uint32FromInt64(4)) || aDyn != 0 && nArg == int32(uint32(_sqlite3MallocSize(tls, aDyn))/libc.Uint32FromInt64(4)) {
				/* The aArg[] array needs to grow. */
				pNew = _sqlite3Malloc(tls, uint64(uint32(nArg)*uint32(4)*uint32(2)))
				if pNew != 0 {
					libc.Xmemcpy(tls, pNew, aArg, uint32(nArg)*uint32(4))
					Xsqlite3_free(tls, aDyn)
					v2 = pNew
					aArg = v2
					aDyn = v2
				} else {
					/* This occurs when the array of context pointers that need to
					 ** be passed to the unlock-notify callback is larger than the
					 ** aStatic[] array allocated on the stack and the attempt to
					 ** allocate a larger array from the heap has failed.
					 **
					 ** This is a difficult situation to handle. Returning an error
					 ** code to the caller is insufficient, as even if an error code
					 ** is returned the transaction on connection db will still be
					 ** closed and the unlock-notify callbacks on blocked connections
					 ** will go unissued. This might cause the application to wait
					 ** indefinitely for an unlock-notify callback that will never
					 ** arrive.
					 **
					 ** Instead, invoke the unlock-notify callback with the context
					 ** array already accumulated. We can then clear the array and
					 ** begin accumulating any further context pointers without
					 ** requiring any dynamic allocation. This is sub-optimal because
					 ** it means that instead of one callback with a large array of
					 ** context pointers the application will receive two or more
					 ** callbacks with smaller arrays of context pointers, which will
					 ** reduce the applications ability to prioritize multiple
					 ** connections. But it is the best that can be done under the
					 ** circumstances.
					 */
					(*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{xUnlockNotify})))(tls, aArg, nArg)
					nArg = 0
				}
			}
			_sqlite3EndBenignMalloc(tls)
			v3 = nArg
			nArg = nArg + 1
			**(**uintptr)(__ccgo_up(aArg + uintptr(v3)*4)) = (*Tsqlite3)(unsafe.Pointer(p)).FpUnlockArg
			xUnlockNotify = (*Tsqlite3)(unsafe.Pointer(p)).FxUnlockNotify
			(*Tsqlite3)(unsafe.Pointer(p)).FpUnlockConnection = uintptr(0)
			(*Tsqlite3)(unsafe.Pointer(p)).FxUnlockNotify = uintptr(0)
			(*Tsqlite3)(unsafe.Pointer(p)).FpUnlockArg = uintptr(0)
		}
		/* Step 3. */
		if (*Tsqlite3)(unsafe.Pointer(p)).FpBlockingConnection == uintptr(0) && (*Tsqlite3)(unsafe.Pointer(p)).FpUnlockConnection == uintptr(0) {
			/* Remove connection p from the blocked connections list. */
			**(**uintptr)(__ccgo_up(pp)) = (*Tsqlite3)(unsafe.Pointer(p)).FpNextBlocked
			(*Tsqlite3)(unsafe.Pointer(p)).FpNextBlocked = uintptr(0)
		} else {
			pp = p + 576
		}
		goto _1
	_1:
	}
	if nArg != 0 {
		(*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{xUnlockNotify})))(tls, aArg, nArg)
	}
	Xsqlite3_free(tls, aDyn)
	_leaveMutex(tls) /* Leave STATIC_MAIN mutex */
}

// C documentation
//
//	/*
//	** Generate bytecode that will initialize a Bloom filter that is appropriate
//	** for pLevel.
//	**
//	** If there are inner loops within pLevel that have the WHERE_BLOOMFILTER
//	** flag set, initialize a Bloomfilter for them as well.  Except don't do
//	** this recursive initialization if the SQLITE_BloomPulldown optimization has
//	** been turned off.
//	**
//	** When the Bloom filter is initialized, the WHERE_BLOOMFILTER flag is cleared
//	** from the loop, but the regFilter value is set to a register that implements
//	** the Bloom filter.  When regFilter is positive, the
//	** sqlite3WhereCodeOneLoopStart() will generate code to test the Bloom filter
//	** and skip the subsequence B-Tree seek if the Bloom filter indicates that
//	** no matching rows exist.
//	**
//	** This routine may only be called if it has previously been determined that
//	** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit
//	** is set.
//	*/
func _sqlite3ConstructBloomFilter(tls *libc.TLS, pWInfo uintptr, iLevel int32, pLevel uintptr, notReady TBitmask) {
	var addrCont, addrOnce, addrTop, iCur, iSrc, jj, n, r1, r11, v1 int32
	var pExpr, pIdx, pItem, pLoop, pParse, pTab, pTabItem, pTabList, pTerm, pWCEnd, saved_pIdxEpr, saved_pIdxPartExpr, v, v2 uintptr
	var sz Tu64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrCont, addrOnce, addrTop, iCur, iSrc, jj, n, pExpr, pIdx, pItem, pLoop, pParse, pTab, pTabItem, pTabList, pTerm, pWCEnd, r1, r11, saved_pIdxEpr, saved_pIdxPartExpr, sz, v, v1, v2 /* Last WHERE clause term */
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse                                                                                                                                                                                                            /* Parsing context */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe                                                                                                                                                                                                                      /* VDBE under construction */
	pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop                                                                                                                                                                                                            /* saved copy of Parse.pIdxPartExpr */
	saved_pIdxEpr = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr
	saved_pIdxPartExpr = (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr
	(*TParse)(unsafe.Pointer(pParse)).FpIdxEpr = uintptr(0)
	(*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr = uintptr(0)
	addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
	for cond := true; cond; cond = iLevel < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) {
		_sqlite3WhereExplainBloomFilter(tls, pParse, pWInfo, pLevel)
		addrCont = _sqlite3VdbeMakeLabel(tls, pParse)
		iCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter = v1
		/* The Bloom filter is a Blob held in a register.  Initialize it
		 ** to zero-filled blob of at least 80K bits, but maybe more if the
		 ** estimated size of the table is larger.  We could actually
		 ** measure the size of the table at run-time using OP_Count with
		 ** P3==1 and use that value to initialize the blob.  But that makes
		 ** testing complicated.  By basing the blob size on the value in the
		 ** sqlite_stat1 table, testing is much easier.
		 */
		pTabList = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList
		iSrc = int32((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)
		pItem = pTabList + 8 + uintptr(iSrc)*48
		pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
		sz = _sqlite3LogEstToInt(tls, (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst)
		if sz < uint64(10000) {
			sz = uint64(10000)
		} else {
			if sz > uint64(10000000) {
				sz = uint64(10000000)
			}
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Blob), int32(sz), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter)
		addrTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iCur)
		pWCEnd = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa + uintptr((*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.FnTerm)*48
		pTerm = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa
		for {
			if !(pTerm < pWCEnd) {
				break
			}
			pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VIRTUAL) == 0 && _sqlite3ExprIsSingleTableConstraint(tls, pExpr, pTabList, iSrc, 0) != 0 {
				_sqlite3ExprIfFalse(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, addrCont, int32(SQLITE_JUMPIFNULL))
			}
			goto _3
		_3:
			;
			pTerm += 48
		}
		if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != 0 {
			r1 = _sqlite3GetTempReg(tls, pParse)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCur, r1)
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, 0, r1, int32(1))
			_sqlite3ReleaseTempReg(tls, pParse, r1)
		} else {
			pIdx = (*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FpIndex
			n = int32((*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FnEq)
			r11 = _sqlite3GetTempRange(tls, pParse, n)
			jj = 0
			for {
				if !(jj < n) {
					break
				}
				_sqlite3ExprCodeLoadIndexColumn(tls, pParse, pIdx, iCur, jj, r11+jj)
				goto _4
			_4:
				;
				jj = jj + 1
			}
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, 0, r11, n)
			_sqlite3ReleaseTempRange(tls, pParse, r11, n)
		}
		_sqlite3VdbeResolveLabel(tls, v, addrCont)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, addrTop+int32(1))
		_sqlite3VdbeJumpHere(tls, v, addrTop)
		**(**Tu32)(__ccgo_up(pLoop + 40)) &= uint32(^libc.Int32FromInt32(WHERE_BLOOMFILTER))
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_BloomPulldown)) != uint32(0) {
			break
		}
		for {
			iLevel = iLevel + 1
			v1 = iLevel
			if !(v1 < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) {
				break
			}
			pLevel = pWInfo + 760 + uintptr(iLevel)*88
			pTabItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
			if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) != 0 {
				continue
			}
			pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
			if pLoop == uintptr(0) {
				continue
			}
			if (*TWhereLoop)(unsafe.Pointer(pLoop)).Fprereq&notReady != 0 {
				continue
			}
			if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_BLOOMFILTER)|libc.Int32FromInt32(WHERE_COLUMN_IN)) == uint32(WHERE_BLOOMFILTER) {
				/* This is a candidate for bloom-filter pull-down (early evaluation).
				 ** The test that WHERE_COLUMN_IN is omitted is important, as we are
				 ** not able to do early evaluation of bloom filters that make use of
				 ** the IN operator */
				break
			}
		}
	}
	_sqlite3VdbeJumpHere(tls, v, addrOnce)
	(*TParse)(unsafe.Pointer(pParse)).FpIdxEpr = saved_pIdxEpr
	(*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr = saved_pIdxPartExpr
}

// C documentation
//
//	/*
//	** If p2 exists and p1 and p2 have the same number of terms, then change
//	** every term of p1 to have the same sort order as p2 and return true.
//	**
//	** If p2 is NULL or p1 and p2 are different lengths, then make no changes
//	** and return false.
//	**
//	** p1 must be non-NULL.
//	*/
func _sqlite3CopySortOrder(tls *libc.TLS, p1 uintptr, p2 uintptr) (r int32) {
	var ii int32
	var sortFlags Tu8
	_, _ = ii, sortFlags
	if p2 != 0 && (*TExprList)(unsafe.Pointer(p1)).FnExpr == (*TExprList)(unsafe.Pointer(p2)).FnExpr {
		ii = 0
		for {
			if !(ii < (*TExprList)(unsafe.Pointer(p1)).FnExpr) {
				break
			}
			sortFlags = uint8(int32((*(*TExprList_item)(unsafe.Pointer(p2 + 8 + uintptr(ii)*20))).Ffg.FsortFlags) & int32(KEYINFO_ORDER_DESC))
			(*(*TExprList_item)(unsafe.Pointer(p1 + 8 + uintptr(ii)*20))).Ffg.FsortFlags = sortFlags
			goto _1
		_1:
			;
			ii = ii + 1
		}
		return int32(1)
	} else {
		return 0
	}
	return r
}

// C documentation
//
//	/*
//	** Allocate and return a pointer to an expression to load the column iCol
//	** from datasource iSrc in SrcList pSrc.
//	*/
func _sqlite3CreateColumnExpr(tls *libc.TLS, db uintptr, pSrc uintptr, iSrc int32, iCol int32) (r uintptr) {
	var p, pItem, pTab, v1 uintptr
	var v2 uint64
	var v3 int32
	_, _, _, _, _, _ = p, pItem, pTab, v1, v2, v3
	p = _sqlite3ExprAlloc(tls, db, int32(TK_COLUMN), uintptr(0), 0)
	if p != 0 {
		pItem = pSrc + 8 + uintptr(iSrc)*48
		v1 = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
		(*TExpr)(unsafe.Pointer(p)).Fy.FpTab = v1
		pTab = v1
		(*TExpr)(unsafe.Pointer(p)).FiTable = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor
		if int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FiPKey) == iCol {
			(*TExpr)(unsafe.Pointer(p)).FiColumn = int16(-int32(1))
		} else {
			(*TExpr)(unsafe.Pointer(p)).FiColumn = int16(iCol)
			if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
				if int32((*TTable)(unsafe.Pointer(pTab)).FnCol) >= int32(64) {
					v2 = uint64(-libc.Int32FromInt32(1))
				} else {
					v2 = libc.Uint64FromInt32(1)<<(*TTable)(unsafe.Pointer(pTab)).FnCol - uint64(1)
				}
				(*TSrcItem)(unsafe.Pointer(pItem)).FcolUsed = v2
			} else {
				if iCol >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
					v3 = int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1)
				} else {
					v3 = iCol
				}
				**(**TBitmask)(__ccgo_up(pItem + 24)) |= libc.Uint64FromInt32(1) << v3
			}
		}
	}
	return p
}

// C documentation
//
//	/*
//	** This routine is called to create a new foreign key on the table
//	** currently under construction.  pFromCol determines which columns
//	** in the current table point to the foreign key.  If pFromCol==0 then
//	** connect the key to the last column inserted.  pTo is the name of
//	** the table referred to (a.k.a the "parent" table).  pToCol is a list
//	** of tables in the parent pTo table.  flags contains all
//	** information about the conflict resolution algorithms specified
//	** in the ON DELETE, ON UPDATE and ON INSERT clauses.
//	**
//	** An FKey structure is created and added to the table currently
//	** under construction in the pParse->pNewTable field.
//	**
//	** The foreign key is set for IMMEDIATE processing.  A subsequent call
//	** to sqlite3DeferForeignKey() might change this to DEFERRED.
//	*/
func _sqlite3CreateForeignKey(tls *libc.TLS, pParse uintptr, pFromCol uintptr, pTo uintptr, pToCol uintptr, flags int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, p, pFKey, pNextTo, z uintptr
	var i, iCol, j, n, nCol int32
	var nByte Ti64
	_, _, _, _, _, _, _, _, _, _, _ = db, i, iCol, j, n, nByte, nCol, p, pFKey, pNextTo, z
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pFKey = uintptr(0)
	p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	if p == uintptr(0) || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) == int32(PARSE_MODE_DECLARE_VTAB) {
		goto fk_end
	}
	if pFromCol == uintptr(0) {
		iCol = int32((*TTable)(unsafe.Pointer(p)).FnCol) - int32(1)
		if iCol < 0 {
			goto fk_end
		}
		if pToCol != 0 && (*TExprList)(unsafe.Pointer(pToCol)).FnExpr != int32(1) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16393, libc.VaList(bp+8, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(iCol)*12))).FzCnName, pTo))
			goto fk_end
		}
		nCol = int32(1)
	} else {
		if pToCol != 0 && (*TExprList)(unsafe.Pointer(pToCol)).FnExpr != (*TExprList)(unsafe.Pointer(pFromCol)).FnExpr {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16456, 0)
			goto fk_end
		} else {
			nCol = (*TExprList)(unsafe.Pointer(pFromCol)).FnExpr
		}
	}
	nByte = int64(uint32(libc.UintptrFromInt32(0)+36) + uint32(nCol)*uint32(8) + (*TToken)(unsafe.Pointer(pTo)).Fn + uint32(1))
	if pToCol != 0 {
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pToCol)).FnExpr) {
				break
			}
			nByte = nByte + int64(_sqlite3Strlen30(tls, (*(*TExprList_item)(unsafe.Pointer(pToCol + 8 + uintptr(i)*20))).FzEName)+int32(1))
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	pFKey = _sqlite3DbMallocZero(tls, db, uint64(nByte))
	if pFKey == uintptr(0) {
		goto fk_end
	}
	(*TFKey)(unsafe.Pointer(pFKey)).FpFrom = p
	(*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom = (*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(p + 44))).FpFKey
	z = pFKey + 36 + uintptr(nCol)*8
	(*TFKey)(unsafe.Pointer(pFKey)).FzTo = z
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		_sqlite3RenameTokenMap(tls, pParse, z, pTo)
	}
	libc.Xmemcpy(tls, z, (*TToken)(unsafe.Pointer(pTo)).Fz, (*TToken)(unsafe.Pointer(pTo)).Fn)
	**(**int8)(__ccgo_up(z + uintptr((*TToken)(unsafe.Pointer(pTo)).Fn))) = 0
	_sqlite3Dequote(tls, z)
	z = z + uintptr((*TToken)(unsafe.Pointer(pTo)).Fn+uint32(1))
	(*TFKey)(unsafe.Pointer(pFKey)).FnCol = nCol
	if pFromCol == uintptr(0) {
		(*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FiFrom = int32((*TTable)(unsafe.Pointer(p)).FnCol) - int32(1)
	} else {
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			j = 0
			for {
				if !(j < int32((*TTable)(unsafe.Pointer(p)).FnCol)) {
					break
				}
				if _sqlite3StrICmp(tls, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(j)*12))).FzCnName, (*(*TExprList_item)(unsafe.Pointer(pFromCol + 8 + uintptr(i)*20))).FzEName) == 0 {
					(*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FiFrom = j
					break
				}
				goto _3
			_3:
				;
				j = j + 1
			}
			if j >= int32((*TTable)(unsafe.Pointer(p)).FnCol) {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16550, libc.VaList(bp+8, (*(*TExprList_item)(unsafe.Pointer(pFromCol + 8 + uintptr(i)*20))).FzEName))
				goto fk_end
			}
			if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
				_sqlite3RenameTokenRemap(tls, pParse, pFKey+36+uintptr(i)*8, (*(*TExprList_item)(unsafe.Pointer(pFromCol + 8 + uintptr(i)*20))).FzEName)
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	if pToCol != 0 {
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			n = _sqlite3Strlen30(tls, (*(*TExprList_item)(unsafe.Pointer(pToCol + 8 + uintptr(i)*20))).FzEName)
			(*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FzCol = z
			if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
				_sqlite3RenameTokenRemap(tls, pParse, z, (*(*TExprList_item)(unsafe.Pointer(pToCol + 8 + uintptr(i)*20))).FzEName)
			}
			libc.Xmemcpy(tls, z, (*(*TExprList_item)(unsafe.Pointer(pToCol + 8 + uintptr(i)*20))).FzEName, uint32(n))
			**(**int8)(__ccgo_up(z + uintptr(n))) = 0
			z = z + uintptr(n+int32(1))
			goto _4
		_4:
			;
			i = i + 1
		}
	}
	(*TFKey)(unsafe.Pointer(pFKey)).FisDeferred = uint8(0)
	**(**Tu8)(__ccgo_up(pFKey + 25)) = uint8(flags & libc.Int32FromInt32(0xff))                               /* ON DELETE action */
	**(**Tu8)(__ccgo_up(pFKey + 25 + 1)) = uint8(flags >> libc.Int32FromInt32(8) & libc.Int32FromInt32(0xff)) /* ON UPDATE action */
	pNextTo = _sqlite3HashInsert(tls, (*TTable)(unsafe.Pointer(p)).FpSchema+56, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, pFKey)
	if pNextTo == pFKey {
		_sqlite3OomFault(tls, db)
		goto fk_end
	}
	if pNextTo != 0 {
		(*TFKey)(unsafe.Pointer(pFKey)).FpNextTo = pNextTo
		(*TFKey)(unsafe.Pointer(pNextTo)).FpPrevTo = pFKey
	}
	/* Link the foreign key to the table as the last step.
	 */
	(*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(p + 44))).FpFKey = pFKey
	pFKey = uintptr(0)
	goto fk_end
fk_end:
	;
	_sqlite3DbFree(tls, db, pFKey)
	_sqlite3ExprListDelete(tls, db, pFromCol)
	_sqlite3ExprListDelete(tls, db, pToCol)
}

// C documentation
//
//	/*
//	** This function is exactly the same as sqlite3_create_function(), except
//	** that it is designed to be called by internal code. The difference is
//	** that if a malloc() fails in sqlite3_create_function(), an error code
//	** is returned and the mallocFailed flag cleared.
//	*/
func _sqlite3CreateFunc(tls *libc.TLS, db uintptr, zFunctionName uintptr, nArg int32, enc int32, pUserData uintptr, __ccgo_fp_xSFunc uintptr, __ccgo_fp_xStep uintptr, __ccgo_fp_xFinal uintptr, __ccgo_fp_xValue uintptr, __ccgo_fp_xInverse uintptr, pDestructor uintptr) (r int32) {
	var extraFlags, rc int32
	var p, v1 uintptr
	_, _, _, _ = extraFlags, p, rc, v1
	if zFunctionName == uintptr(0) || __ccgo_fp_xSFunc != uintptr(0) && __ccgo_fp_xFinal != uintptr(0) || libc.BoolInt32(__ccgo_fp_xFinal == uintptr(0)) != libc.BoolInt32(__ccgo_fp_xStep == uintptr(0)) || libc.BoolInt32(__ccgo_fp_xValue == uintptr(0)) != libc.BoolInt32(__ccgo_fp_xInverse == uintptr(0)) || (nArg < -int32(1) || nArg > int32(SQLITE_MAX_FUNCTION_ARG)) || int32(255) < _sqlite3Strlen30(tls, zFunctionName) {
		return _sqlite3MisuseError(tls, int32(189333))
	}
	extraFlags = enc & (libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_DIRECTONLY) | libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(SQLITE_INNOCUOUS) | libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE) | libc.Int32FromInt32(SQLITE_SELFORDER1))
	enc = enc & (libc.Int32FromInt32(SQLITE_FUNC_ENCMASK) | libc.Int32FromInt32(SQLITE_ANY))
	/* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE.  But
	 ** the meaning is inverted.  So flip the bit. */
	extraFlags = extraFlags ^ int32(SQLITE_FUNC_UNSAFE) /* tag-20230109-1 */
	/* If SQLITE_UTF16 is specified as the encoding type, transform this
	 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
	 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
	 **
	 ** If SQLITE_ANY is specified, add three versions of the function
	 ** to the hash table.
	 */
	switch enc {
	case int32(SQLITE_UTF16):
		enc = int32(SQLITE_UTF16LE)
	case int32(SQLITE_ANY):
		rc = _sqlite3CreateFunc(tls, db, zFunctionName, nArg, int32(SQLITE_UTF8)|extraFlags^int32(SQLITE_FUNC_UNSAFE), pUserData, __ccgo_fp_xSFunc, __ccgo_fp_xStep, __ccgo_fp_xFinal, __ccgo_fp_xValue, __ccgo_fp_xInverse, pDestructor)
		if rc == SQLITE_OK {
			rc = _sqlite3CreateFunc(tls, db, zFunctionName, nArg, int32(SQLITE_UTF16LE)|extraFlags^int32(SQLITE_FUNC_UNSAFE), pUserData, __ccgo_fp_xSFunc, __ccgo_fp_xStep, __ccgo_fp_xFinal, __ccgo_fp_xValue, __ccgo_fp_xInverse, pDestructor)
		}
		if rc != SQLITE_OK {
			return rc
		}
		enc = int32(SQLITE_UTF16BE)
	case int32(SQLITE_UTF8):
		fallthrough
	case int32(SQLITE_UTF16LE):
		fallthrough
	case int32(SQLITE_UTF16BE):
	default:
		enc = int32(SQLITE_UTF8)
		break
	}
	/* Check if an existing function is being overridden or deleted. If so,
	 ** and there are active VMs, then return SQLITE_BUSY. If a function
	 ** is being overridden/deleted but there are no active VMs, allow the
	 ** operation to continue but invalidate all precompiled statements.
	 */
	p = _sqlite3FindFunction(tls, db, zFunctionName, nArg, uint8(enc), uint8(0))
	if p != 0 && (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(SQLITE_FUNC_ENCMASK) == uint32(enc) && int32((*TFuncDef)(unsafe.Pointer(p)).FnArg) == nArg {
		if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive != 0 {
			_sqlite3ErrorWithMsg(tls, db, int32(SQLITE_BUSY), __ccgo_ts+27145, 0)
			return int32(SQLITE_BUSY)
		} else {
			_sqlite3ExpirePreparedStatements(tls, db, 0)
		}
	} else {
		if __ccgo_fp_xSFunc == uintptr(0) && __ccgo_fp_xFinal == uintptr(0) {
			/* Trying to delete a function that does not exist.  This is a no-op.
			 ** https://sqlite.org/forum/forumpost/726219164b */
			return SQLITE_OK
		}
	}
	p = _sqlite3FindFunction(tls, db, zFunctionName, nArg, uint8(enc), uint8(1))
	if !(p != 0) {
		return int32(SQLITE_NOMEM)
	}
	/* If an older version of the function with a configured destructor is
	 ** being replaced invoke the destructor function here. */
	_functionDestroy(tls, db, p)
	if pDestructor != 0 {
		(*TFuncDestructor)(unsafe.Pointer(pDestructor)).FnRef = (*TFuncDestructor)(unsafe.Pointer(pDestructor)).FnRef + 1
	}
	*(*uintptr)(unsafe.Pointer(p + 36)) = pDestructor
	(*TFuncDef)(unsafe.Pointer(p)).FfuncFlags = (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(SQLITE_FUNC_ENCMASK) | uint32(extraFlags)
	if __ccgo_fp_xSFunc != 0 {
		v1 = __ccgo_fp_xSFunc
	} else {
		v1 = __ccgo_fp_xStep
	}
	(*TFuncDef)(unsafe.Pointer(p)).FxSFunc = v1
	(*TFuncDef)(unsafe.Pointer(p)).FxFinalize = __ccgo_fp_xFinal
	(*TFuncDef)(unsafe.Pointer(p)).FxValue = __ccgo_fp_xValue
	(*TFuncDef)(unsafe.Pointer(p)).FxInverse = __ccgo_fp_xInverse
	(*TFuncDef)(unsafe.Pointer(p)).FpUserData = pUserData
	(*TFuncDef)(unsafe.Pointer(p)).FnArg = int16(uint16(nArg))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Create a new index for an SQL table.  pName1.pName2 is the name of the index
//	** and pTblList is the name of the table that is to be indexed.  Both will
//	** be NULL for a primary key or an index that is created to satisfy a
//	** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
//	** as the table to be indexed.  pParse->pNewTable is a table that is
//	** currently being constructed by a CREATE TABLE statement.
//	**
//	** pList is a list of columns to be indexed.  pList will be NULL if this
//	** is a primary key or unique-constraint on the most recent column added
//	** to the table currently under construction.
//	*/
func _sqlite3CreateIndex(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr, pTblName uintptr, pList uintptr, onError int32, pStart uintptr, pPIWhere uintptr, sortOrder int32, ifNotExist int32, idxType Tu8) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var db, p, pCExpr, pCol, pDb, pExpr, pIdx, pIndex, pListItem, pLoop, pNext, pPk, pTab, pThis, ppFrom, v, z1, z2, zColl, zDb, zName, zStmt, v2 uintptr
	var i, iDb, iMem, j, k, n, n1, nColl, nExtra, nExtraCol, nName, requestedSortOrder, sortOrderMask, x, v5 int32
	var _ /* pName at bp+52 */ uintptr
	var _ /* prevCol at bp+64 */ TToken
	var _ /* sFix at bp+0 */ TDbFixer
	var _ /* zExtra at bp+56 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, iDb, iMem, j, k, n, n1, nColl, nExtra, nExtraCol, nName, p, pCExpr, pCol, pDb, pExpr, pIdx, pIndex, pListItem, pLoop, pNext, pPk, pTab, pThis, ppFrom, requestedSortOrder, sortOrderMask, v, x, z1, z2, zColl, zDb, zName, zStmt, v2, v5
	pTab = uintptr(0)                              /* Table to be indexed */
	pIndex = uintptr(0)                            /* The index to be created */
	zName = uintptr(0)                             /* 1 to honor DESC in index.  0 to ignore. */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb     /* Index of the database that is being written */
	**(**uintptr)(__ccgo_up(bp + 52)) = uintptr(0) /* For looping over pList */
	nExtra = 0                                     /* Number of extra columns needed */
	**(**uintptr)(__ccgo_up(bp + 56)) = uintptr(0) /* Extra space after the Index object */
	pPk = uintptr(0)                               /* PRIMARY KEY index for WITHOUT ROWID tables */
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		goto exit_create_index
	}
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) == int32(PARSE_MODE_DECLARE_VTAB) && int32(idxType) != int32(SQLITE_IDXTYPE_PRIMARYKEY) {
		goto exit_create_index
	}
	if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) {
		goto exit_create_index
	}
	if _sqlite3HasExplicitNulls(tls, pParse, pList) != 0 {
		goto exit_create_index
	}
	/*
	 ** Find the table that is to be indexed.  Return early if not found.
	 */
	if pTblName != uintptr(0) {
		/* Use the two-part index name to determine the database
		 ** to search for the table. 'Fix' the table name to this db
		 ** before looking up the table.
		 */
		iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp+52)
		if iDb < 0 {
			goto exit_create_index
		}
		/* If the index name was unqualified, check if the table
		 ** is a temp table. If so, set the database to 1. Do not do this
		 ** if initializing a database schema.
		 */
		if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) {
			pTab = _sqlite3SrcListLookup(tls, pParse, pTblName)
			if (*TToken)(unsafe.Pointer(pName2)).Fn == uint32(0) && pTab != 0 && (*TTable)(unsafe.Pointer(pTab)).FpSchema == (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema {
				iDb = int32(1)
			}
		}
		_sqlite3FixInit(tls, bp, pParse, iDb, __ccgo_ts+16635, **(**uintptr)(__ccgo_up(bp + 52)))
		if _sqlite3FixSrcList(tls, bp, pTblName) != 0 {
			/* Because the parser constructs pTblName from a single identifier,
			 ** sqlite3FixSrcList can never fail. */
		}
		pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pTblName+8)
		if pTab == uintptr(0) {
			goto exit_create_index
		}
		if iDb == int32(1) && (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema != (*TTable)(unsafe.Pointer(pTab)).FpSchema {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16641, libc.VaList(bp+80, (*TTable)(unsafe.Pointer(pTab)).FzName))
			goto exit_create_index
		}
		if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
			pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
		}
	} else {
		pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
		if !(pTab != 0) {
			goto exit_create_index
		}
		iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	}
	pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16
	if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+7973, int32(7)) == 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 && pTblName != uintptr(0) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16691, libc.VaList(bp+80, (*TTable)(unsafe.Pointer(pTab)).FzName))
		goto exit_create_index
	}
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16719, 0)
		goto exit_create_index
	}
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16744, 0)
		goto exit_create_index
	}
	/*
	 ** Find the name of the index.  Make sure there is not already another
	 ** index or table with the same name.
	 **
	 ** Exception:  If we are reading the names of permanent indices from the
	 ** sqlite_schema table (because some other process changed the schema) and
	 ** one of the index names collides with the name of a temporary table or
	 ** index, then we will continue to process this index.
	 **
	 ** If pName==0 it means that we are
	 ** dealing with a primary key or UNIQUE constraint.  We have to invent our
	 ** own name.
	 */
	if **(**uintptr)(__ccgo_up(bp + 52)) != 0 {
		zName = _sqlite3NameFromToken(tls, db, **(**uintptr)(__ccgo_up(bp + 52)))
		if zName == uintptr(0) {
			goto exit_create_index
		}
		if SQLITE_OK != _sqlite3CheckObjectName(tls, pParse, zName, __ccgo_ts+16635, (*TTable)(unsafe.Pointer(pTab)).FzName) {
			goto exit_create_index
		}
		if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
			if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) {
				if _sqlite3FindTable(tls, db, zName, (*TDb)(unsafe.Pointer(pDb)).FzDbSName) != uintptr(0) {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16778, libc.VaList(bp+80, zName))
					goto exit_create_index
				}
			}
			if _sqlite3FindIndex(tls, db, zName, (*TDb)(unsafe.Pointer(pDb)).FzDbSName) != uintptr(0) {
				if !(ifNotExist != 0) {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16812, libc.VaList(bp+80, zName))
				} else {
					_sqlite3CodeVerifySchema(tls, pParse, iDb)
					_sqlite3ForceNotReadOnly(tls, pParse)
				}
				goto exit_create_index
			}
		}
	} else {
		pLoop = (*TTable)(unsafe.Pointer(pTab)).FpIndex
		n = libc.Int32FromInt32(1)
		for {
			if !(pLoop != 0) {
				break
			}
			goto _1
		_1:
			;
			pLoop = (*TIndex)(unsafe.Pointer(pLoop)).FpNext
			n = n + 1
		}
		zName = _sqlite3MPrintf(tls, db, __ccgo_ts+16836, libc.VaList(bp+80, (*TTable)(unsafe.Pointer(pTab)).FzName, n))
		if zName == uintptr(0) {
			goto exit_create_index
		}
		/* Automatic index names generated from within sqlite3_declare_vtab()
		 ** must have names that are distinct from normal automatic index names.
		 ** The following statement converts "sqlite3_autoindex..." into
		 ** "sqlite3_butoindex..." in order to make the names distinct.
		 ** The "vtab_err.test" test demonstrates the need of this statement. */
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != PARSE_MODE_NORMAL {
			**(**int8)(__ccgo_up(zName + 7)) = **(**int8)(__ccgo_up(zName + 7)) + 1
		}
	}
	/* Check for authorization to create an index.
	 */
	if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
		zDb = (*TDb)(unsafe.Pointer(pDb)).FzDbSName
		if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
			v2 = __ccgo_ts + 7981
		} else {
			v2 = __ccgo_ts + 7501
		}
		if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_INSERT), v2, uintptr(0), zDb) != 0 {
			goto exit_create_index
		}
		i = int32(SQLITE_CREATE_INDEX)
		if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
			i = int32(SQLITE_CREATE_TEMP_INDEX)
		}
		if _sqlite3AuthCheck(tls, pParse, i, zName, (*TTable)(unsafe.Pointer(pTab)).FzName, zDb) != 0 {
			goto exit_create_index
		}
	}
	/* If pList==0, it means this routine was called to make a primary
	 ** key out of the last column added to the table under construction.
	 ** So create a fake list to simulate this.
	 */
	if pList == uintptr(0) {
		pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-int32(1))*12
		v2 = pCol + 10
		*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(COLFLAG_UNIQUE))
		_sqlite3TokenInit(tls, bp+64, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)
		pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+64, 0))
		if pList == uintptr(0) {
			goto exit_create_index
		}
		_sqlite3ExprListSetSortOrder(tls, pList, sortOrder, -int32(1))
	} else {
		_sqlite3ExprListCheckLength(tls, pParse, pList, __ccgo_ts+16635)
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			goto exit_create_index
		}
	}
	/* Figure out how many bytes of space are required to store explicitly
	 ** specified collation sequence names.
	 */
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
			break
		}
		pExpr = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE) {
			nExtra = nExtra + (int32(1) + _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8))))
		}
		goto _4
	_4:
		;
		i = i + 1
	}
	/*
	 ** Allocate the index structure.
	 */
	nName = _sqlite3Strlen30(tls, zName)
	if pPk != 0 {
		v5 = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)
	} else {
		v5 = int32(1)
	}
	nExtraCol = v5
	pIndex = _sqlite3AllocateIndexObject(tls, db, (*TExprList)(unsafe.Pointer(pList)).FnExpr+nExtraCol, nName+nExtra+int32(1), bp+56)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto exit_create_index
	}
	(*TIndex)(unsafe.Pointer(pIndex)).FzName = **(**uintptr)(__ccgo_up(bp + 56))
	**(**uintptr)(__ccgo_up(bp + 56)) = **(**uintptr)(__ccgo_up(bp + 56)) + uintptr(nName+int32(1))
	libc.Xmemcpy(tls, (*TIndex)(unsafe.Pointer(pIndex)).FzName, zName, uint32(nName+int32(1)))
	(*TIndex)(unsafe.Pointer(pIndex)).FpTable = pTab
	(*TIndex)(unsafe.Pointer(pIndex)).FonError = uint8(onError)
	libc.SetBitFieldPtr16Uint32(pIndex+56, libc.BoolUint32(onError != libc.Int32FromInt32(OE_None)), 3, 0x8)
	libc.SetBitFieldPtr16Uint32(pIndex+56, uint32(idxType), 0, 0x3)
	(*TIndex)(unsafe.Pointer(pIndex)).FpSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema
	(*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol = uint16((*TExprList)(unsafe.Pointer(pList)).FnExpr)
	if pPIWhere != 0 {
		_sqlite3ResolveSelfReference(tls, pParse, pTab, int32(NC_PartIdx), pPIWhere, uintptr(0))
		(*TIndex)(unsafe.Pointer(pIndex)).FpPartIdxWhere = pPIWhere
		pPIWhere = uintptr(0)
	}
	/* Check to see if we should honor DESC requests on index columns
	 */
	if int32((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format) >= int32(4) {
		sortOrderMask = -int32(1) /* Honor DESC */
	} else {
		sortOrderMask = 0 /* Ignore DESC */
	}
	/* Analyze the list of expressions that form the terms of the index and
	 ** report any errors.  In the common case where the expression is exactly
	 ** a table column, store that column in aiColumn[].  For general expressions,
	 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
	 **
	 ** TODO: Issue a warning if two or more columns of the index are identical.
	 ** TODO: Issue a warning if the table primary key is used as part of the
	 ** index key.
	 */
	pListItem = pList + 8
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		(*TIndex)(unsafe.Pointer(pIndex)).FaColExpr = pList
		pList = uintptr(0)
	}
	i = 0
	for {
		if !(i < int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol)) {
			break
		} /* Collation sequence name */
		_sqlite3StringToId(tls, (*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr)
		_sqlite3ResolveSelfReference(tls, pParse, pTab, int32(NC_IdxExpr), (*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr, uintptr(0))
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			goto exit_create_index
		}
		pCExpr = _sqlite3ExprSkipCollate(tls, (*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr)
		if int32((*TExpr)(unsafe.Pointer(pCExpr)).Fop) != int32(TK_COLUMN) {
			if pTab == (*TParse)(unsafe.Pointer(pParse)).FpNewTable {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16859, 0)
				goto exit_create_index
			}
			if (*TIndex)(unsafe.Pointer(pIndex)).FaColExpr == uintptr(0) {
				(*TIndex)(unsafe.Pointer(pIndex)).FaColExpr = pList
				pList = uintptr(0)
			}
			j = -int32(2)
			**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(i)*2)) = int16(-libc.Int32FromInt32(2))
			libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 3, 0x8)
			libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 11, 0x800)
		} else {
			j = int32((*TExpr)(unsafe.Pointer(pCExpr)).FiColumn)
			if j < 0 {
				j = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey)
			} else {
				if int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12 + 4))&0xf>>0)) == 0 {
					libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 3, 0x8)
				}
				if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
					libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 10, 0x400)
					libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 11, 0x800)
				}
			}
			**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(i)*2)) = int16(j)
		}
		zColl = uintptr(0)
		if int32((*TExpr)(unsafe.Pointer((*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr)).Fop) == int32(TK_COLLATE) {
			zColl = *(*uintptr)(unsafe.Pointer((*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr + 8))
			nColl = _sqlite3Strlen30(tls, zColl) + int32(1)
			libc.Xmemcpy(tls, **(**uintptr)(__ccgo_up(bp + 56)), zColl, uint32(nColl))
			zColl = **(**uintptr)(__ccgo_up(bp + 56))
			**(**uintptr)(__ccgo_up(bp + 56)) = **(**uintptr)(__ccgo_up(bp + 56)) + uintptr(nColl)
			nExtra = nExtra - nColl
		} else {
			if j >= 0 {
				zColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(j)*12)
			}
		}
		if !(zColl != 0) {
			zColl = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
		}
		if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) && !(_sqlite3LocateCollSeq(tls, pParse, zColl) != 0) {
			goto exit_create_index
		}
		**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(i)*4)) = zColl
		requestedSortOrder = int32((*TExprList_item)(unsafe.Pointer(pListItem)).Ffg.FsortFlags) & sortOrderMask
		**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaSortOrder + uintptr(i))) = uint8(requestedSortOrder)
		goto _6
	_6:
		;
		i = i + 1
		pListItem += 20
	}
	/* Append the table key to the end of the index.  For WITHOUT ROWID
	 ** tables (when pPk!=0) this will be the declared PRIMARY KEY.  For
	 ** normal tables (when pPk==0) this will be the rowid.
	 */
	if pPk != 0 {
		j = 0
		for {
			if !(j < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
				break
			}
			x = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(j)*2)))
			if _isDupColumn(tls, pIndex, int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol), pPk, j) != 0 {
				(*TIndex)(unsafe.Pointer(pIndex)).FnColumn = (*TIndex)(unsafe.Pointer(pIndex)).FnColumn - 1
			} else {
				**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(i)*2)) = int16(x)
				**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(i)*4)) = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(j)*4))
				**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaSortOrder + uintptr(i))) = **(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaSortOrder + uintptr(j)))
				i = i + 1
			}
			goto _7
		_7:
			;
			j = j + 1
		}
	} else {
		**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(i)*2)) = int16(-libc.Int32FromInt32(1))
		**(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(i)*4)) = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
	}
	_sqlite3DefaultRowEst(tls, pIndex)
	if (*TParse)(unsafe.Pointer(pParse)).FpNewTable == uintptr(0) {
		_estimateIndexWidth(tls, pIndex)
	}
	/* If this index contains every column of its table, then mark
	 ** it as a covering index */
	_recomputeColumnsNotIndexed(tls, pIndex)
	if pTblName != uintptr(0) && int32((*TIndex)(unsafe.Pointer(pIndex)).FnColumn) >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) {
		libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(1), 5, 0x20)
		j = 0
		for {
			if !(j < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
				goto _8
			}
			if _sqlite3TableColumnToIndex(tls, pIndex, j) >= 0 {
				goto _8
			}
			libc.SetBitFieldPtr16Uint32(pIndex+56, libc.Uint32FromInt32(0), 5, 0x20)
			break
			goto _8
		_8:
			;
			j = j + 1
		}
	}
	if pTab == (*TParse)(unsafe.Pointer(pParse)).FpNewTable {
		pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
		for {
			if !(pIdx != 0) {
				break
			}
			if int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) != int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol) {
				goto _9
			}
			k = 0
			for {
				if !(k < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
					break
				}
				if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(k)*2))) != int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(k)*2))) {
					break
				}
				z1 = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(k)*4))
				z2 = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(k)*4))
				if _sqlite3StrICmp(tls, z1, z2) != 0 {
					break
				}
				goto _10
			_10:
				;
				k = k + 1
			}
			if k == int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) {
				if int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != int32((*TIndex)(unsafe.Pointer(pIndex)).FonError) {
					/* This constraint creates the same index as a previous
					 ** constraint specified somewhere in the CREATE TABLE statement.
					 ** However the ON CONFLICT clauses are different. If both this
					 ** constraint and the previous equivalent constraint have explicit
					 ** ON CONFLICT clauses this is an error. Otherwise, use the
					 ** explicitly specified behavior for the index.
					 */
					if !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) == int32(OE_Default) || int32((*TIndex)(unsafe.Pointer(pIndex)).FonError) == int32(OE_Default)) {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16920, libc.VaList(bp+80, 0))
					}
					if int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) == int32(OE_Default) {
						(*TIndex)(unsafe.Pointer(pIdx)).FonError = (*TIndex)(unsafe.Pointer(pIndex)).FonError
					}
				}
				if int32(idxType) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
					libc.SetBitFieldPtr16Uint32(pIdx+56, uint32(idxType), 0, 0x3)
				}
				if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
					(*TIndex)(unsafe.Pointer(pIndex)).FpNext = (*TParse)(unsafe.Pointer(pParse)).FpNewIndex
					(*TParse)(unsafe.Pointer(pParse)).FpNewIndex = pIndex
					pIndex = uintptr(0)
				}
				goto exit_create_index
			}
			goto _9
		_9:
			;
			pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		}
	}
	if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
		/* Link the new Index structure to its table and to the other
		 ** in-memory database structures.
		 */
		if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 {
			if pTblName != uintptr(0) {
				(*TIndex)(unsafe.Pointer(pIndex)).Ftnum = (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum
				if _sqlite3IndexHasDuplicateRootPage(tls, pIndex) != 0 {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16962, 0)
					(*TParse)(unsafe.Pointer(pParse)).Frc = _sqlite3CorruptError(tls, int32(130930))
					goto exit_create_index
				}
			}
			p = _sqlite3HashInsert(tls, (*TIndex)(unsafe.Pointer(pIndex)).FpSchema+24, (*TIndex)(unsafe.Pointer(pIndex)).FzName, pIndex)
			if p != 0 {
				/* Malloc must have failed */
				_sqlite3OomFault(tls, db)
				goto exit_create_index
			}
			**(**Tu32)(__ccgo_up(db + 24)) |= uint32(DBFLAG_SchemaChange)
		} else {
			if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) || pTblName != uintptr(0) {
				v2 = pParse + 48
				*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
				v5 = *(*int32)(unsafe.Pointer(v2))
				iMem = v5
				v = _sqlite3GetVdbe(tls, pParse)
				if v == uintptr(0) {
					goto exit_create_index
				}
				_sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb)
				/* Create the rootpage for the index using CreateIndex. But before
				 ** doing so, code a Noop instruction and store its address in
				 ** Index.tnum. This is required in case this index is actually a
				 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
				 ** that case the convertToWithoutRowidTable() routine will replace
				 ** the Noop with a Goto to jump over the VDBE code generated below. */
				(*TIndex)(unsafe.Pointer(pIndex)).Ftnum = uint32(_sqlite3VdbeAddOp0(tls, v, int32(OP_Noop)))
				_sqlite3VdbeAddOp3(tls, v, int32(OP_CreateBtree), iDb, iMem, int32(BTREE_BLOBKEY))
				/* Gather the complete text of the CREATE INDEX statement into
				 ** the zStmt variable
				 */
				if pStart != 0 {
					n1 = int32(uint32(int32((*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz)-int32((*TToken)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 52)))).Fz)) + (*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fn)
					if int32(**(**int8)(__ccgo_up((*TToken)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 52)))).Fz + uintptr(n1-int32(1))))) == int32(';') {
						n1 = n1 - 1
					}
					/* A named index with an explicit CREATE INDEX statement */
					if onError == OE_None {
						v2 = __ccgo_ts + 1711
					} else {
						v2 = __ccgo_ts + 16979
					}
					zStmt = _sqlite3MPrintf(tls, db, __ccgo_ts+16987, libc.VaList(bp+80, v2, n1, (*TToken)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 52)))).Fz))
				} else {
					/* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
					/* zStmt = sqlite3MPrintf(""); */
					zStmt = uintptr(0)
				}
				/* Add an entry in sqlite_schema for this index
				 */
				_sqlite3NestedParse(tls, pParse, __ccgo_ts+17007, libc.VaList(bp+80, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TIndex)(unsafe.Pointer(pIndex)).FzName, (*TTable)(unsafe.Pointer(pTab)).FzName, iMem, zStmt))
				_sqlite3DbFree(tls, db, zStmt)
				/* Fill the index with data and reparse the schema. Code an OP_Expire
				 ** to invalidate all pre-compiled statements.
				 */
				if pTblName != 0 {
					_sqlite3RefillIndex(tls, pParse, pIndex, iMem)
					_sqlite3ChangeCookie(tls, pParse, iDb)
					_sqlite3VdbeAddParseSchemaOp(tls, v, iDb, _sqlite3MPrintf(tls, db, __ccgo_ts+17066, libc.VaList(bp+80, (*TIndex)(unsafe.Pointer(pIndex)).FzName)), uint16(0))
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Expire), 0, int32(1))
				}
				_sqlite3VdbeJumpHere(tls, v, int32((*TIndex)(unsafe.Pointer(pIndex)).Ftnum))
			}
		}
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 || pTblName == uintptr(0) {
		(*TIndex)(unsafe.Pointer(pIndex)).FpNext = (*TTable)(unsafe.Pointer(pTab)).FpIndex
		(*TTable)(unsafe.Pointer(pTab)).FpIndex = pIndex
		pIndex = uintptr(0)
	} else {
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
			(*TParse)(unsafe.Pointer(pParse)).FpNewIndex = pIndex
			pIndex = uintptr(0)
		}
	}
	/* Clean up before exiting */
	goto exit_create_index
exit_create_index:
	;
	if pIndex != 0 {
		_sqlite3FreeIndex(tls, db, pIndex)
	}
	if pTab != 0 {
		ppFrom = pTab + 8
		for {
			v2 = **(**uintptr)(__ccgo_up(ppFrom))
			pThis = v2
			if !(v2 != uintptr(0)) {
				break
			}
			if int32((*TIndex)(unsafe.Pointer(pThis)).FonError) != int32(OE_Replace) {
				goto _14
			}
			for {
				v2 = (*TIndex)(unsafe.Pointer(pThis)).FpNext
				pNext = v2
				if !(v2 != uintptr(0) && int32((*TIndex)(unsafe.Pointer(pNext)).FonError) != int32(OE_Replace)) {
					break
				}
				**(**uintptr)(__ccgo_up(ppFrom)) = pNext
				(*TIndex)(unsafe.Pointer(pThis)).FpNext = (*TIndex)(unsafe.Pointer(pNext)).FpNext
				(*TIndex)(unsafe.Pointer(pNext)).FpNext = pThis
				ppFrom = pNext + 20
			}
			break
			goto _14
		_14:
			;
			ppFrom = pThis + 20
		}
	}
	_sqlite3ExprDelete(tls, db, pPIWhere)
	_sqlite3ExprListDelete(tls, db, pList)
	_sqlite3SrcListDelete(tls, db, pTblName)
	_sqlite3DbFree(tls, db, zName)
}

// C documentation
//
//	/*
//	** The parser calls this routine in order to create a new VIEW
//	*/
func _sqlite3CreateView(tls *libc.TLS, pParse uintptr, pBegin uintptr, pName1 uintptr, pName2 uintptr, pCNames uintptr, pSelect uintptr, isTemp int32, noErr int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var db, p, z uintptr
	var iDb, n int32
	var _ /* pName at bp+60 */ uintptr
	var _ /* sEnd at bp+0 */ TToken
	var _ /* sFix at bp+8 */ TDbFixer
	_, _, _, _, _ = db, iDb, n, p, z
	**(**uintptr)(__ccgo_up(bp + 60)) = uintptr(0)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if int32((*TParse)(unsafe.Pointer(pParse)).FnVar) > 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16020, 0)
		goto create_view_fail
	}
	_sqlite3StartTable(tls, pParse, pName1, pName2, isTemp, int32(1), 0, noErr)
	p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	if p == uintptr(0) || (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		goto create_view_fail
	}
	/* Legacy versions of SQLite allowed the use of the magic "rowid" column
	 ** on a view, even though views do not have rowids.  The following flag
	 ** setting fixes this problem.  But the fix can be disabled by compiling
	 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
	 ** depend upon the old buggy behavior.  The ability can also be toggled
	 ** using sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW,...) */
	**(**Tu32)(__ccgo_up(p + 28)) |= uint32(TF_NoVisibleRowid) /* Never allow rowid in view */
	_sqlite3TwoPartName(tls, pParse, pName1, pName2, bp+60)
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(p)).FpSchema)
	_sqlite3FixInit(tls, bp+8, pParse, iDb, __ccgo_ts+12332, **(**uintptr)(__ccgo_up(bp + 60)))
	if _sqlite3FixSelect(tls, bp+8, pSelect) != 0 {
		goto create_view_fail
	}
	/* Make a copy of the entire SELECT statement that defines the view.
	 ** This will force all the Expr.token.z values to be dynamically
	 ** allocated rather than point to the input string - which means that
	 ** they will persist after the current sqlite3_exec() call returns.
	 */
	**(**Tu32)(__ccgo_up(pSelect + 4)) |= uint32(SF_View)
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		(*(*struct {
			FpSelect uintptr
		})(unsafe.Pointer(&(*TTable)(unsafe.Pointer(p)).Fu))).FpSelect = pSelect
		pSelect = uintptr(0)
	} else {
		(*(*struct {
			FpSelect uintptr
		})(unsafe.Pointer(&(*TTable)(unsafe.Pointer(p)).Fu))).FpSelect = _sqlite3SelectDup(tls, db, pSelect, int32(EXPRDUP_REDUCE))
	}
	(*TTable)(unsafe.Pointer(p)).FpCheck = _sqlite3ExprListDup(tls, db, pCNames, int32(EXPRDUP_REDUCE))
	(*TTable)(unsafe.Pointer(p)).FeTabType = uint8(TABTYP_VIEW)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto create_view_fail
	}
	/* Locate the end of the CREATE VIEW statement.  Make sEnd point to
	 ** the end.
	 */
	**(**TToken)(__ccgo_up(bp)) = (*TParse)(unsafe.Pointer(pParse)).FsLastToken
	if int32(**(**int8)(__ccgo_up((**(**TToken)(__ccgo_up(bp))).Fz))) != int32(';') {
		(**(**TToken)(__ccgo_up(bp))).Fz += uintptr((**(**TToken)(__ccgo_up(bp))).Fn)
	}
	(**(**TToken)(__ccgo_up(bp))).Fn = uint32(0)
	n = int32((**(**TToken)(__ccgo_up(bp))).Fz) - int32((*TToken)(unsafe.Pointer(pBegin)).Fz)
	z = (*TToken)(unsafe.Pointer(pBegin)).Fz
	for int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(n-int32(1)))))])&int32(0x01) != 0 {
		n = n - 1
	}
	(**(**TToken)(__ccgo_up(bp))).Fz = z + uintptr(n-int32(1))
	(**(**TToken)(__ccgo_up(bp))).Fn = uint32(1)
	/* Use sqlite3EndTable() to add the view to the schema table */
	_sqlite3EndTable(tls, pParse, uintptr(0), bp, uint32(0), uintptr(0))
	goto create_view_fail
create_view_fail:
	;
	_sqlite3SelectDelete(tls, db, pSelect)
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		_sqlite3RenameExprlistUnmap(tls, pParse, pCNames)
	}
	_sqlite3ExprListDelete(tls, db, pCNames)
	return
}

// C documentation
//
//	/*
//	** Return true if zName points to a name that may be used to refer to
//	** database iDb attached to handle db.
//	*/
func _sqlite3DbIsNamed(tls *libc.TLS, db uintptr, iDb int32, zName uintptr) (r int32) {
	return libc.BoolInt32(_sqlite3StrICmp(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, zName) == 0 || iDb == 0 && _sqlite3StrICmp(tls, __ccgo_ts+8033, zName) == 0)
}

func _sqlite3DbMallocSize(tls *libc.TLS, db uintptr, p uintptr) (r int32) {
	if db != 0 {
		if uint32(p) < uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd) {
			if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle) {
				return int32(LOOKASIDE_SMALL)
			}
			if uint32(p) >= uint32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) {
				return int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue)
			}
		}
	}
	return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxSize})))(tls, p)
}

// C documentation
//
//	/*
//	** The text between zStart and zEnd represents a phrase within a larger
//	** SQL statement.  Make a copy of this phrase in space obtained form
//	** sqlite3DbMalloc().  Omit leading and trailing whitespace.
//	*/
func _sqlite3DbSpanDup(tls *libc.TLS, db uintptr, zStart uintptr, zEnd uintptr) (r uintptr) {
	var n int32
	_ = n
	for int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zStart)))])&int32(0x01) != 0 {
		zStart = zStart + 1
	}
	n = int32(zEnd) - int32(zStart)
	for int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zStart + uintptr(n-int32(1)))))])&int32(0x01) != 0 {
		n = n - 1
	}
	return _sqlite3DbStrNDup(tls, db, zStart, uint64(n))
}

// C documentation
//
//	/*
//	** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
//	** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
//	** whereas sqlite3Atoi64() does not.
//	**
//	** Returns:
//	**
//	**     0    Successful transformation.  Fits in a 64-bit signed integer.
//	**     1    Excess text after the integer value
//	**     2    Integer too large for a 64-bit signed integer or is malformed
//	**     3    Special case of 9223372036854775808
//	*/
func _sqlite3DecOrHexToI64(tls *libc.TLS, z uintptr, pOut uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, k, n int32
	var _ /* u at bp+0 */ Tu64
	_, _, _ = i, k, n
	if int32(**(**int8)(__ccgo_up(z))) == int32('0') && (int32(**(**int8)(__ccgo_up(z + 1))) == int32('x') || int32(**(**int8)(__ccgo_up(z + 1))) == int32('X')) {
		**(**Tu64)(__ccgo_up(bp)) = uint64(0)
		i = int32(2)
		for {
			if !(int32(**(**int8)(__ccgo_up(z + uintptr(i)))) == int32('0')) {
				break
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		k = i
		for {
			if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(z + uintptr(k))))])&int32(0x08) != 0) {
				break
			}
			**(**Tu64)(__ccgo_up(bp)) = **(**Tu64)(__ccgo_up(bp))*uint64(16) + uint64(_sqlite3HexToInt(tls, int32(**(**int8)(__ccgo_up(z + uintptr(k))))))
			goto _2
		_2:
			;
			k = k + 1
		}
		libc.Xmemcpy(tls, pOut, bp, uint32(8))
		if k-i > int32(16) {
			return int32(2)
		}
		if int32(**(**int8)(__ccgo_up(z + uintptr(k)))) != 0 {
			return int32(1)
		}
		return 0
	} else {
		n = int32(libc.Uint32FromInt32(0x3fffffff) & libc.Xstrspn(tls, z, __ccgo_ts+1841))
		if **(**int8)(__ccgo_up(z + uintptr(n))) != 0 {
			n = n + 1
		}
		return _sqlite3Atoi64(tls, z, pOut, n, uint8(SQLITE_UTF8))
	}
	return r
}

// C documentation
//
//	/*
//	** Fill the Index.aiRowEst[] array with default information - information
//	** to be used when we have not run the ANALYZE command.
//	**
//	** aiRowEst[0] is supposed to contain the number of elements in the index.
//	** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
//	** number of rows in the table that match any particular value of the
//	** first column of the index.  aiRowEst[2] is an estimate of the number
//	** of rows that match any particular combination of the first 2 columns
//	** of the index.  And so forth.  It must always be the case that
//	*
//	**           aiRowEst[N]<=aiRowEst[N-1]
//	**           aiRowEst[N]>=1
//	**
//	** Apart from that, we have little to go on besides intuition as to
//	** how aiRowEst[] should be initialized.  The numbers generated here
//	** are based on typical values found in actual indices.
//	*/
func _sqlite3DefaultRowEst(tls *libc.TLS, pIdx uintptr) {
	var a uintptr
	var i, nCopy, v1 int32
	var x, v2 TLogEst
	_, _, _, _, _, _ = a, i, nCopy, x, v1, v2
	a = (*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst
	if int32(libc.Uint32FromInt64(10)/libc.Uint32FromInt64(2)) < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) {
		v1 = int32(libc.Uint32FromInt64(10) / libc.Uint32FromInt64(2))
	} else {
		v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
	}
	nCopy = v1
	/* Indexes with default row estimates should not have stat1 data */
	/* Set the first entry (number of rows in the index) to the estimated
	 ** number of rows in the table, or half the number of rows in the table
	 ** for a partial index.
	 **
	 ** 2020-05-27:  If some of the stat data is coming from the sqlite_stat1
	 ** table but other parts we are having to guess at, then do not let the
	 ** estimated number of rows in the table be less than 1000 (LogEst 99).
	 ** Failure to do this can cause the indexes for which we do not have
	 ** stat1 data to be ignored by the query planner.
	 */
	x = (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FnRowLogEst
	if int32(x) < int32(99) {
		v2 = libc.Int16FromInt32(99)
		x = v2
		(*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FnRowLogEst = v2
	}
	if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != uintptr(0) {
		x = int16(int32(x) - libc.Int32FromInt32(10))
	}
	**(**TLogEst)(__ccgo_up(a)) = x
	/* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
	 ** 6 and each subsequent value (if any) is 5.  */
	libc.Xmemcpy(tls, a+1*2, uintptr(unsafe.Pointer(&_aVal)), uint32(nCopy)*uint32(2))
	i = nCopy + int32(1)
	for {
		if !(i <= int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
			break
		}
		**(**TLogEst)(__ccgo_up(a + uintptr(i)*2)) = int16(23)
		goto _3
	_3:
		;
		i = i + 1
	}
	if int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != OE_None {
		**(**TLogEst)(__ccgo_up(a + uintptr((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)*2)) = 0
	}
}

// C documentation
//
//	/*
//	** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
//	** clause is seen as part of a foreign key definition.  The isDeferred
//	** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
//	** The behavior of the most recently created foreign key is adjusted
//	** accordingly.
//	*/
func _sqlite3DeferForeignKey(tls *libc.TLS, pParse uintptr, isDeferred int32) {
	var pFKey, pTab, v1 uintptr
	_, _, _ = pFKey, pTab, v1
	v1 = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	pTab = v1
	if v1 == uintptr(0) {
		return
	}
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
		return
	}
	v1 = (*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pTab + 44))).FpFKey
	pFKey = v1
	if v1 == uintptr(0) {
		return
	}
	/* EV: R-30323-21917 */
	(*TFKey)(unsafe.Pointer(pFKey)).FisDeferred = uint8(isDeferred)
}

// C documentation
//
//	/*
//	** Delete memory allocated for the column names of a table or view (the
//	** Table.aCol[] array).
//	*/
func _sqlite3DeleteColumnNames(tls *libc.TLS, db uintptr, pTable uintptr) {
	var i int32
	var pCol, v1 uintptr
	_, _, _ = i, pCol, v1
	v1 = (*TTable)(unsafe.Pointer(pTable)).FaCol
	pCol = v1
	if v1 != uintptr(0) {
		i = 0
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTable)).FnCol)) {
				break
			}
			_sqlite3DbFree(tls, db, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)
			goto _2
		_2:
			;
			i = i + 1
			pCol += 12
		}
		_sqlite3DbNNFreeNN(tls, db, (*TTable)(unsafe.Pointer(pTable)).FaCol)
		if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == TABTYP_NORM {
			_sqlite3ExprListDelete(tls, db, (*(*struct {
				FaddColOffset int32
				FpFKey        uintptr
				FpDfltList    uintptr
			})(unsafe.Pointer(pTable + 44))).FpDfltList)
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) {
			(*TTable)(unsafe.Pointer(pTable)).FaCol = uintptr(0)
			(*TTable)(unsafe.Pointer(pTable)).FnCol = 0
			if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == TABTYP_NORM {
				(*(*struct {
					FaddColOffset int32
					FpFKey        uintptr
					FpDfltList    uintptr
				})(unsafe.Pointer(pTable + 44))).FpDfltList = uintptr(0)
			}
		}
	}
}

// C documentation
//
//	/*
//	** Generate code for a DELETE FROM statement.
//	**
//	**     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
//	**                 \________/       \________________/
//	**                  pTabList              pWhere
//	*/
func _sqlite3DeleteFrom(tls *libc.TLS, pParse uintptr, pTabList uintptr, pWhere uintptr, pOrderBy uintptr, pLimit uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var aToOpen, db, pIdx, pPk, pTab, pTrigger, pVTab, pWInfo, v, v3 uintptr
	var addrBypass, addrEphOpen, addrLoop, bComplex, count, eOnePass, i, iAddrOnce, iDb, iEphCur, iKey, iPk, iRowSet, iTabCur, isView, memCnt, nIdx, rcauth, v1, v2 int32
	var nKey, nPk Ti16
	var wcf Tu16
	var _ /* aiCurOnePass at bp+56 */ [2]int32
	var _ /* iDataCur at bp+0 */ int32
	var _ /* iIdxCur at bp+4 */ int32
	var _ /* sContext at bp+8 */ TAuthContext
	var _ /* sNC at bp+16 */ TNameContext
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aToOpen, addrBypass, addrEphOpen, addrLoop, bComplex, count, db, eOnePass, i, iAddrOnce, iDb, iEphCur, iKey, iPk, iRowSet, iTabCur, isView, memCnt, nIdx, nKey, nPk, pIdx, pPk, pTab, pTrigger, pVTab, pWInfo, rcauth, v, wcf, v1, v2, v3 /* Cursor number for the table */
	**(**int32)(__ccgo_up(bp)) = 0                                                                                                                                                                                                                                                                                                                /* VDBE cursor for the canonical data source */
	**(**int32)(__ccgo_up(bp + 4)) = 0                                                                                                                                                                                                                                                                                                            /* Database number */
	memCnt = 0                                                                                                                                                                                                                                                                                                                                    /* The write cursors opened by WHERE_ONEPASS */
	aToOpen = uintptr(0)                                                                                                                                                                                                                                                                                                                          /* The PRIMARY KEY index on the table */
	iPk = 0                                                                                                                                                                                                                                                                                                                                       /* First of nPk registers holding PRIMARY KEY value */
	nPk = int16(1)                                                                                                                                                                                                                                                                                                                                /* Number of memory cells in the row key */
	iEphCur = 0                                                                                                                                                                                                                                                                                                                                   /* Ephemeral table holding all primary key values */
	iRowSet = 0                                                                                                                                                                                                                                                                                                                                   /* Register for rowset of rows to delete */
	addrBypass = 0                                                                                                                                                                                                                                                                                                                                /* Address of jump over the delete logic */
	addrLoop = 0                                                                                                                                                                                                                                                                                                                                  /* Top of the delete loop */
	addrEphOpen = 0                                                                                                                                                                                                                                                                                                                               /* List of table triggers, if required */
	libc.Xmemset(tls, bp+8, 0, uint32(8))
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		goto delete_from_cleanup
	}
	/* Locate the table which we want to delete.  This table has to be
	 ** put in an SrcList structure because some of the subroutines we
	 ** will be calling are designed to work with multiple tables and expect
	 ** an SrcList* parameter instead of just a Table* parameter.
	 */
	pTab = _sqlite3SrcListLookup(tls, pParse, pTabList)
	if pTab == uintptr(0) {
		goto delete_from_cleanup
	}
	/* Figure out if we have any triggers and if the table being
	 ** deleted from is a view
	 */
	pTrigger = _sqlite3TriggersExist(tls, pParse, pTab, int32(TK_DELETE), uintptr(0), uintptr(0))
	isView = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW))
	bComplex = libc.BoolInt32(pTrigger != 0 || _sqlite3FkRequired(tls, pParse, pTab, uintptr(0), 0) != 0)
	/* If pTab is really a view, make sure it has been initialized.
	 */
	if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 {
		goto delete_from_cleanup
	}
	if _sqlite3IsReadOnly(tls, pParse, pTab, pTrigger) != 0 {
		goto delete_from_cleanup
	}
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	rcauth = _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName)
	if rcauth == int32(SQLITE_DENY) {
		goto delete_from_cleanup
	}
	/* Assign cursor numbers to the table and all its indices.
	 */
	v3 = pParse + 44
	v2 = *(*int32)(unsafe.Pointer(v3))
	*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
	v1 = v2
	(*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor = v1
	iTabCur = v1
	nIdx = 0
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		(*TParse)(unsafe.Pointer(pParse)).FnTab = (*TParse)(unsafe.Pointer(pParse)).FnTab + 1
		goto _4
	_4:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		nIdx = nIdx + 1
	}
	/* Start the view context
	 */
	if isView != 0 {
		_sqlite3AuthContextPush(tls, pParse, bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)
	}
	/* Begin generating code.
	 */
	v = _sqlite3GetVdbe(tls, pParse)
	if v == uintptr(0) {
		goto delete_from_cleanup
	}
	if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 {
		_sqlite3VdbeCountChanges(tls, v)
	}
	_sqlite3BeginWriteOperation(tls, pParse, bComplex, iDb)
	/* If we are trying to delete from a view, realize that view into
	 ** an ephemeral table.
	 */
	if isView != 0 {
		_sqlite3MaterializeView(tls, pParse, pTab, pWhere, pOrderBy, pLimit, iTabCur)
		v1 = iTabCur
		**(**int32)(__ccgo_up(bp + 4)) = v1
		**(**int32)(__ccgo_up(bp)) = v1
		pOrderBy = uintptr(0)
		pLimit = uintptr(0)
	}
	/* Resolve the column names in the WHERE clause.
	 */
	libc.Xmemset(tls, bp+16, 0, uint32(36))
	(**(**TNameContext)(__ccgo_up(bp + 16))).FpParse = pParse
	(**(**TNameContext)(__ccgo_up(bp + 16))).FpSrcList = pTabList
	if _sqlite3ResolveExprNames(tls, bp+16, pWhere) != 0 {
		goto delete_from_cleanup
	}
	/* Initialize the counter of the number of rows deleted, if
	 ** we are counting rows.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00001))<<libc.Int32FromInt32(32)) != uint64(0) && !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) && !((*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0) && !(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x8>>3)) != 0) {
		v3 = pParse + 48
		*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
		v1 = *(*int32)(unsafe.Pointer(v3))
		memCnt = v1
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, memCnt)
	}
	/* Special case: A DELETE without a WHERE clause deletes everything.
	 ** It is easier just to erase the whole table. Prior to version 3.6.5,
	 ** this optimization caused the row change count (the value returned by
	 ** API function sqlite3_count_changes) to be set incorrectly.
	 **
	 ** The "rcauth==SQLITE_OK" terms is the
	 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
	 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
	 ** the truncate optimization is disabled and all rows are deleted
	 ** individually.
	 */
	if rcauth == SQLITE_OK && pWhere == uintptr(0) && !(bComplex != 0) && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) && (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback == uintptr(0) {
		_sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(1), (*TTable)(unsafe.Pointer(pTab)).FzName)
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			if memCnt != 0 {
				v1 = memCnt
			} else {
				v1 = -int32(1)
			}
			_sqlite3VdbeAddOp4(tls, v, int32(OP_Clear), int32((*TTable)(unsafe.Pointer(pTab)).Ftnum), iDb, v1, (*TTable)(unsafe.Pointer(pTab)).FzName, -int32(1))
		}
		pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
		for {
			if !(pIdx != 0) {
				break
			}
			if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
				if memCnt != 0 {
					v1 = memCnt
				} else {
					v1 = -int32(1)
				}
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Clear), int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb, v1)
			} else {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Clear), int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb)
			}
			goto _9
		_9:
			;
			pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		}
	} else {
		wcf = uint16(libc.Int32FromInt32(WHERE_ONEPASS_DESIRED) | libc.Int32FromInt32(WHERE_DUPLICATES_OK))
		if (**(**TNameContext)(__ccgo_up(bp + 16))).FncFlags&int32(NC_Subquery) != 0 {
			bComplex = int32(1)
		}
		if bComplex != 0 {
			v1 = 0
		} else {
			v1 = int32(WHERE_ONEPASS_MULTIROW)
		}
		wcf = uint16(int32(wcf) | v1)
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			/* For a rowid table, initialize the RowSet to an empty set */
			pPk = uintptr(0)
			v3 = pParse + 48
			*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
			v1 = *(*int32)(unsafe.Pointer(v3))
			iRowSet = v1
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, iRowSet)
		} else {
			/* For a WITHOUT ROWID table, create an ephemeral table used to
			 ** hold all primary keys for rows to be deleted. */
			pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
			nPk = int16((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)
			iPk = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
			**(**int32)(__ccgo_up(pParse + 48)) += int32(nPk)
			v3 = pParse + 44
			v1 = *(*int32)(unsafe.Pointer(v3))
			*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
			iEphCur = v1
			addrEphOpen = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), iEphCur, int32(nPk))
			_sqlite3VdbeSetP4KeyInfo(tls, pParse, pPk)
		}
		/* Construct a query to find the rowid or primary key for every row
		 ** to be deleted, based on the WHERE clause. Set variable eOnePass
		 ** to indicate the strategy used to implement this delete:
		 **
		 **  ONEPASS_OFF:    Two-pass approach - use a FIFO for rowids/PK values.
		 **  ONEPASS_SINGLE: One-pass approach - at most one row deleted.
		 **  ONEPASS_MULTI:  One-pass approach - any number of rows may be deleted.
		 */
		pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, uintptr(0), uintptr(0), uintptr(0), wcf, iTabCur+int32(1))
		if pWInfo == uintptr(0) {
			goto delete_from_cleanup
		}
		eOnePass = _sqlite3WhereOkOnePass(tls, pWInfo, bp+56)
		if eOnePass != int32(ONEPASS_SINGLE) {
			_sqlite3MultiWrite(tls, pParse)
		}
		if _sqlite3WhereUsesDeferredSeek(tls, pWInfo) != 0 {
			_sqlite3VdbeAddOp1(tls, v, int32(OP_FinishSeek), iTabCur)
		}
		/* Keep track of the number of rows to be deleted */
		if memCnt != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), memCnt, int32(1))
		}
		/* Extract the rowid or primary key for the current row */
		if pPk != 0 {
			i = 0
			for {
				if !(i < int32(nPk)) {
					break
				}
				_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iTabCur, int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))), iPk+i)
				goto _16
			_16:
				;
				i = i + 1
			}
			iKey = iPk
		} else {
			v3 = pParse + 48
			*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
			v1 = *(*int32)(unsafe.Pointer(v3))
			iKey = v1
			_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iTabCur, -int32(1), iKey)
		}
		if eOnePass != ONEPASS_OFF {
			/* For ONEPASS, no need to store the rowid/primary-key. There is only
			 ** one, so just keep it in its register(s) and fall through to the
			 ** delete code.  */
			nKey = nPk /* OP_Found will use an unpacked key */
			aToOpen = _sqlite3DbMallocRawNN(tls, db, uint64(nIdx+int32(2)))
			if aToOpen == uintptr(0) {
				_sqlite3WhereEnd(tls, pWInfo)
				goto delete_from_cleanup
			}
			libc.Xmemset(tls, aToOpen, int32(1), uint32(nIdx+int32(1)))
			**(**Tu8)(__ccgo_up(aToOpen + uintptr(nIdx+int32(1)))) = uint8(0)
			if (**(**[2]int32)(__ccgo_up(bp + 56)))[0] >= 0 {
				**(**Tu8)(__ccgo_up(aToOpen + uintptr((**(**[2]int32)(__ccgo_up(bp + 56)))[0]-iTabCur))) = uint8(0)
			}
			if (**(**[2]int32)(__ccgo_up(bp + 56)))[int32(1)] >= 0 {
				**(**Tu8)(__ccgo_up(aToOpen + uintptr((**(**[2]int32)(__ccgo_up(bp + 56)))[int32(1)]-iTabCur))) = uint8(0)
			}
			if addrEphOpen != 0 {
				_sqlite3VdbeChangeToNoop(tls, v, addrEphOpen)
			}
			addrBypass = _sqlite3VdbeMakeLabel(tls, pParse)
		} else {
			if pPk != 0 {
				/* Add the PK key for this row to the temporary table */
				v3 = pParse + 48
				*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
				v1 = *(*int32)(unsafe.Pointer(v3))
				iKey = v1
				nKey = 0 /* Zero tells OP_Found to use a composite key */
				_sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), iPk, int32(nPk), iKey, _sqlite3IndexAffinityStr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pPk), int32(nPk))
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iEphCur, iKey, iPk, int32(nPk))
			} else {
				/* Add the rowid of the row to be deleted to the RowSet */
				nKey = int16(1) /* OP_DeferredSeek always uses a single rowid */
				_sqlite3VdbeAddOp2(tls, v, int32(OP_RowSetAdd), iRowSet, iKey)
			}
			_sqlite3WhereEnd(tls, pWInfo)
		}
		/* Unless this is a view, open cursors for the table we are
		 ** deleting from and all its indices. If this is a view, then the
		 ** only effect this statement has is to fire the INSTEAD OF
		 ** triggers.
		 */
		if !(isView != 0) {
			iAddrOnce = 0
			if eOnePass == int32(ONEPASS_MULTI) {
				iAddrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
			}
			_sqlite3OpenTableAndIndices(tls, pParse, pTab, int32(OP_OpenWrite), uint8(OPFLAG_FORDELETE), iTabCur, aToOpen, bp, bp+4)
			if eOnePass == int32(ONEPASS_MULTI) {
				_sqlite3VdbeJumpHereOrPopInst(tls, v, iAddrOnce)
			}
		}
		/* Set up a loop over the rowids/primary-keys that were found in the
		 ** where-clause loop above.
		 */
		if eOnePass != ONEPASS_OFF {
			/* OP_Found will use an unpacked key */
			if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) && **(**Tu8)(__ccgo_up(aToOpen + uintptr(**(**int32)(__ccgo_up(bp))-iTabCur))) != 0 {
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), **(**int32)(__ccgo_up(bp)), addrBypass, iKey, int32(nKey))
			}
		} else {
			if pPk != 0 {
				addrLoop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iEphCur)
				if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEphCur, 0, iKey)
				} else {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_RowData), iEphCur, iKey)
				}
				/* OP_Found will use a composite key */
			} else {
				addrLoop = _sqlite3VdbeAddOp3(tls, v, int32(OP_RowSetRead), iRowSet, 0, iKey)
			}
		}
		/* Delete the row */
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
			pVTab = _sqlite3GetVTable(tls, db, pTab)
			_sqlite3VtabMakeWritable(tls, pParse, pTab)
			_sqlite3MayAbort(tls, pParse)
			if eOnePass == int32(ONEPASS_SINGLE) {
				_sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iTabCur)
				if (*TParse)(unsafe.Pointer(pParse)).FpToplevel == uintptr(0) {
					(*TParse)(unsafe.Pointer(pParse)).FisMultiWrite = uint8(0)
				}
			}
			_sqlite3VdbeAddOp4(tls, v, int32(OP_VUpdate), 0, int32(1), iKey, pVTab, -int32(12))
			_sqlite3VdbeChangeP5(tls, v, uint16(OE_Abort))
		} else {
			count = libc.BoolInt32(int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0) /* True to count changes */
			_sqlite3GenerateRowDelete(tls, pParse, pTab, pTrigger, **(**int32)(__ccgo_up(bp)), **(**int32)(__ccgo_up(bp + 4)), iKey, nKey, uint8(count), uint8(OE_Default), uint8(eOnePass), (**(**[2]int32)(__ccgo_up(bp + 56)))[int32(1)])
		}
		/* End of the loop over all rowids/primary-keys. */
		if eOnePass != ONEPASS_OFF {
			_sqlite3VdbeResolveLabel(tls, v, addrBypass)
			_sqlite3WhereEnd(tls, pWInfo)
		} else {
			if pPk != 0 {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iEphCur, addrLoop+int32(1))
				_sqlite3VdbeJumpHere(tls, v, addrLoop)
			} else {
				_sqlite3VdbeGoto(tls, v, addrLoop)
				_sqlite3VdbeJumpHere(tls, v, addrLoop)
			}
		}
	} /* End non-truncate path */
	/* Update the sqlite_sequence table by storing the content of the
	 ** maximum rowid counter values recorded while inserting into
	 ** autoincrement tables.
	 */
	if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab == uintptr(0) {
		_sqlite3AutoincrementEnd(tls, pParse)
	}
	/* Return the number of rows that were deleted. If this routine is
	 ** generating code because of a call to sqlite3NestedParse(), do not
	 ** invoke the callback function.
	 */
	if memCnt != 0 {
		_sqlite3CodeChangeCount(tls, v, memCnt, __ccgo_ts+17667)
	}
	goto delete_from_cleanup
delete_from_cleanup:
	;
	_sqlite3AuthContextPop(tls, bp+8)
	_sqlite3SrcListDelete(tls, db, pTabList)
	_sqlite3ExprDelete(tls, db, pWhere)
	if aToOpen != 0 {
		_sqlite3DbNNFreeNN(tls, db, aToOpen)
	}
	return
}

/* Make sure "isView" and other macros defined above are undefined. Otherwise
** they may interfere with compilation of other functions in this file
** (or in another file, if this file becomes part of the amalgamation).  */

// C documentation
//
//	/*
//	** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken
//	** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those
//	** that contain '_' characters that must be removed before further processing.
//	*/
func _sqlite3DequoteNumber(tls *libc.TLS, pParse uintptr, p uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bHex int32
	var pIn, pOut, v1, v3 uintptr
	var _ /* iValue at bp+0 */ int32
	_, _, _, _, _ = bHex, pIn, pOut, v1, v3
	if p != 0 {
		pIn = *(*uintptr)(unsafe.Pointer(p + 8))
		pOut = *(*uintptr)(unsafe.Pointer(p + 8))
		bHex = libc.BoolInt32(int32(**(**int8)(__ccgo_up(pIn))) == int32('0') && (int32(**(**int8)(__ccgo_up(pIn + 1))) == int32('x') || int32(**(**int8)(__ccgo_up(pIn + 1))) == int32('X')))
		(*TExpr)(unsafe.Pointer(p)).Fop = uint8(TK_INTEGER)
		for {
			if int32(**(**int8)(__ccgo_up(pIn))) != int32('_') {
				v3 = pOut
				pOut = pOut + 1
				**(**int8)(__ccgo_up(v3)) = **(**int8)(__ccgo_up(pIn))
				if int32(**(**int8)(__ccgo_up(pIn))) == int32('e') || int32(**(**int8)(__ccgo_up(pIn))) == int32('E') || int32(**(**int8)(__ccgo_up(pIn))) == int32('.') {
					(*TExpr)(unsafe.Pointer(p)).Fop = uint8(TK_FLOAT)
				}
			} else {
				if bHex == 0 && (!(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(pIn + uintptr(-libc.Int32FromInt32(1)))))])&libc.Int32FromInt32(0x04) != 0) || !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(pIn + 1)))])&libc.Int32FromInt32(0x04) != 0)) || bHex == int32(1) && (!(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(pIn + uintptr(-libc.Int32FromInt32(1)))))])&libc.Int32FromInt32(0x08) != 0) || !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(pIn + 1)))])&libc.Int32FromInt32(0x08) != 0)) {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1797, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(p + 8))))
				}
			}
			goto _2
		_2:
			;
			v1 = pIn
			pIn = pIn + 1
			if !(**(**int8)(__ccgo_up(v1)) != 0) {
				break
			}
		}
		if bHex != 0 {
			(*TExpr)(unsafe.Pointer(p)).Fop = uint8(TK_INTEGER)
		}
		/* tag-20240227-a: If after dequoting, the number is an integer that
		 ** fits in 32 bits, then it must be converted into EP_IntValue.  Other
		 ** parts of the code expect this.  See also tag-20240227-b. */
		if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_INTEGER) && _sqlite3GetInt32(tls, *(*uintptr)(unsafe.Pointer(p + 8)), bp) != 0 {
			*(*int32)(unsafe.Pointer(p + 8)) = **(**int32)(__ccgo_up(bp))
			**(**Tu32)(__ccgo_up(p + 4)) |= uint32(EP_IntValue)
		}
	}
}

// C documentation
//
//	/*
//	** If the input token p is quoted, try to adjust the token to remove
//	** the quotes.  This is not always possible:
//	**
//	**     "abc"     ->   abc
//	**     "ab""cd"  ->   (not possible because of the interior "")
//	**
//	** Remove the quotes if possible.  This is a optimization.  The overall
//	** system should still return the correct answer even if this routine
//	** is always a no-op.
//	*/
func _sqlite3DequoteToken(tls *libc.TLS, p uintptr) {
	var i uint32
	_ = i
	if (*TToken)(unsafe.Pointer(p)).Fn < uint32(2) {
		return
	}
	if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up((*TToken)(unsafe.Pointer(p)).Fz)))])&libc.Int32FromInt32(0x80) != 0) {
		return
	}
	i = uint32(1)
	for {
		if !(i < (*TToken)(unsafe.Pointer(p)).Fn-uint32(1)) {
			break
		}
		if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up((*TToken)(unsafe.Pointer(p)).Fz + uintptr(i))))])&int32(0x80) != 0 {
			return
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	**(**uint32)(__ccgo_up(p + 4)) -= uint32(2)
	(*TToken)(unsafe.Pointer(p)).Fz = (*TToken)(unsafe.Pointer(p)).Fz + 1
}

// C documentation
//
//	/*
//	** This routine will drop an existing named index.  This routine
//	** implements the DROP INDEX statement.
//	*/
func _sqlite3DropIndex(tls *libc.TLS, pParse uintptr, pName uintptr, ifExists int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var code, iDb int32
	var db, pIndex, pTab, v, zDb, zTab, v1 uintptr
	_, _, _, _, _, _, _, _, _ = code, db, iDb, pIndex, pTab, v, zDb, zTab, v1
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto exit_drop_index
	}
	/* Never called with prior non-OOM errors */
	if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) {
		goto exit_drop_index
	}
	pIndex = _sqlite3FindIndex(tls, db, (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzName, *(*uintptr)(unsafe.Pointer(pName + 8 + 44)))
	if pIndex == uintptr(0) {
		if !(ifExists != 0) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17093, libc.VaList(bp+8, pName+8))
		} else {
			_sqlite3CodeVerifyNamedSchema(tls, pParse, *(*uintptr)(unsafe.Pointer(pName + 8 + 44)))
			_sqlite3ForceNotReadOnly(tls, pParse)
		}
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 8, 0x100)
		goto exit_drop_index
	}
	if int32(uint32(*(*uint16)(unsafe.Pointer(pIndex + 56))&0x3>>0)) != SQLITE_IDXTYPE_APPDEF {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17111, libc.VaList(bp+8, 0))
		goto exit_drop_index
	}
	iDb = _sqlite3SchemaToIndex(tls, db, (*TIndex)(unsafe.Pointer(pIndex)).FpSchema)
	code = int32(SQLITE_DROP_INDEX)
	pTab = (*TIndex)(unsafe.Pointer(pIndex)).FpTable
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
		v1 = __ccgo_ts + 7981
	} else {
		v1 = __ccgo_ts + 7501
	}
	zTab = v1
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), zTab, uintptr(0), zDb) != 0 {
		goto exit_drop_index
	}
	if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
		code = int32(SQLITE_DROP_TEMP_INDEX)
	}
	if _sqlite3AuthCheck(tls, pParse, code, (*TIndex)(unsafe.Pointer(pIndex)).FzName, (*TTable)(unsafe.Pointer(pTab)).FzName, zDb) != 0 {
		goto exit_drop_index
	}
	/* Generate code to remove the index and from the schema table */
	v = _sqlite3GetVdbe(tls, pParse)
	if v != 0 {
		_sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb)
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+17184, libc.VaList(bp+8, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TIndex)(unsafe.Pointer(pIndex)).FzName))
		_sqlite3ClearStatTables(tls, pParse, iDb, __ccgo_ts+14261, (*TIndex)(unsafe.Pointer(pIndex)).FzName)
		_sqlite3ChangeCookie(tls, pParse, iDb)
		_destroyRootPage(tls, pParse, int32((*TIndex)(unsafe.Pointer(pIndex)).Ftnum), iDb)
		_sqlite3VdbeAddOp4(tls, v, int32(OP_DropIndex), iDb, 0, 0, (*TIndex)(unsafe.Pointer(pIndex)).FzName, 0)
	}
	goto exit_drop_index
exit_drop_index:
	;
	_sqlite3SrcListDelete(tls, db, pName)
}

// C documentation
//
//	/*
//	** This routine is called to do the work of a DROP TABLE statement.
//	** pName is the name of the table to be dropped.
//	*/
func _sqlite3DropTable(tls *libc.TLS, pParse uintptr, pName uintptr, isView int32, noErr int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var code, iDb int32
	var db, pTab, v, zArg2, zDb, zTab, v1 uintptr
	_, _, _, _, _, _, _, _, _ = code, db, iDb, pTab, v, zArg2, zDb, zTab, v1
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto exit_drop_table
	}
	if _sqlite3ReadSchema(tls, pParse) != 0 {
		goto exit_drop_table
	}
	if noErr != 0 {
		(*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr = (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr + 1
	}
	pTab = _sqlite3LocateTableItem(tls, pParse, uint32(isView), pName+8)
	if noErr != 0 {
		(*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr = (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr - 1
	}
	if pTab == uintptr(0) {
		if noErr != 0 {
			_sqlite3CodeVerifyNamedSchema(tls, pParse, *(*uintptr)(unsafe.Pointer(pName + 8 + 44)))
			_sqlite3ForceNotReadOnly(tls, pParse)
		}
		goto exit_drop_table
	}
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	/* If pTab is a virtual table, call ViewGetColumnNames() to ensure
	 ** it is initialized.
	 */
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) && _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 {
		goto exit_drop_table
	}
	if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
		v1 = __ccgo_ts + 7981
	} else {
		v1 = __ccgo_ts + 7501
	}
	zTab = v1
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	zArg2 = uintptr(0)
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), zTab, uintptr(0), zDb) != 0 {
		goto exit_drop_table
	}
	if isView != 0 {
		if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
			code = int32(SQLITE_DROP_TEMP_VIEW)
		} else {
			code = int32(SQLITE_DROP_VIEW)
		}
	} else {
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
			code = int32(SQLITE_DROP_VTABLE)
			zArg2 = (*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, db, pTab))).FpMod)).FzName
		} else {
			if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
				code = int32(SQLITE_DROP_TEMP_TABLE)
			} else {
				code = int32(SQLITE_DROP_TABLE)
			}
		}
	}
	if _sqlite3AuthCheck(tls, pParse, code, (*TTable)(unsafe.Pointer(pTab)).FzName, zArg2, zDb) != 0 {
		goto exit_drop_table
	}
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), zDb) != 0 {
		goto exit_drop_table
	}
	if _tableMayNotBeDropped(tls, db, pTab) != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16299, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
		goto exit_drop_table
	}
	/* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
	 ** on a table.
	 */
	if isView != 0 && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VIEW)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16327, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
		goto exit_drop_table
	}
	if !(isView != 0) && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16361, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
		goto exit_drop_table
	}
	/* Generate code to remove the table from the schema table
	 ** on disk.
	 */
	v = _sqlite3GetVdbe(tls, pParse)
	if v != 0 {
		_sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb)
		if !(isView != 0) {
			_sqlite3ClearStatTables(tls, pParse, iDb, __ccgo_ts+14265, (*TTable)(unsafe.Pointer(pTab)).FzName)
			_sqlite3FkDropTable(tls, pParse, pName, pTab)
		}
		_sqlite3CodeDropTable(tls, pParse, pTab, iDb, isView)
	}
	goto exit_drop_table
exit_drop_table:
	;
	_sqlite3SrcListDelete(tls, db, pName)
}

// C documentation
//
//	/*
//	** This function is called to drop a trigger from the database schema.
//	**
//	** This may be called directly from the parser and therefore identifies
//	** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
//	** same job as this routine except it takes a pointer to the trigger
//	** instead of the trigger name.
//	**/
func _sqlite3DropTrigger(tls *libc.TLS, pParse uintptr, pName uintptr, noErr int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pTrigger, zDb, zName uintptr
	var i, j, v2 int32
	_, _, _, _, _, _, _ = db, i, j, pTrigger, zDb, zName, v2
	pTrigger = uintptr(0)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto drop_trigger_cleanup
	}
	if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) {
		goto drop_trigger_cleanup
	}
	zDb = *(*uintptr)(unsafe.Pointer(pName + 8 + 44))
	zName = (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzName
	i = OMIT_TEMPDB
	for {
		if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
			break
		}
		if i < int32(2) {
			v2 = i ^ int32(1)
		} else {
			v2 = i
		}
		j = v2 /* Search TEMP before MAIN */
		if zDb != 0 && _sqlite3DbIsNamed(tls, db, j, zDb) == 0 {
			goto _1
		}
		pTrigger = _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(j)*16))).FpSchema+40, zName)
		if pTrigger != 0 {
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if !(pTrigger != 0) {
		if !(noErr != 0) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23612, libc.VaList(bp+8, pName+8))
		} else {
			_sqlite3CodeVerifyNamedSchema(tls, pParse, zDb)
		}
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 8, 0x100)
		goto drop_trigger_cleanup
	}
	_sqlite3DropTriggerPtr(tls, pParse, pTrigger)
	goto drop_trigger_cleanup
drop_trigger_cleanup:
	;
	_sqlite3SrcListDelete(tls, db, pName)
}

// C documentation
//
//	/*
//	** Drop a trigger given a pointer to that trigger.
//	*/
func _sqlite3DropTriggerPtr(tls *libc.TLS, pParse uintptr, pTrigger uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var code, iDb int32
	var db, pTable, v, zDb, zTab, v1 uintptr
	_, _, _, _, _, _, _, _ = code, db, iDb, pTable, v, zDb, zTab, v1
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTrigger)(unsafe.Pointer(pTrigger)).FpSchema)
	pTable = _tableOfTrigger(tls, pTrigger)
	if pTable != 0 {
		code = int32(SQLITE_DROP_TRIGGER)
		zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
		if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
			v1 = __ccgo_ts + 7981
		} else {
			v1 = __ccgo_ts + 7501
		}
		zTab = v1
		if iDb == int32(1) {
			code = int32(SQLITE_DROP_TEMP_TRIGGER)
		}
		if _sqlite3AuthCheck(tls, pParse, code, (*TTrigger)(unsafe.Pointer(pTrigger)).FzName, (*TTable)(unsafe.Pointer(pTable)).FzName, zDb) != 0 || _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), zTab, uintptr(0), zDb) != 0 {
			return
		}
	}
	/* Generate code to destroy the database record of the trigger.
	 */
	v1 = _sqlite3GetVdbe(tls, pParse)
	v = v1
	if v1 != uintptr(0) {
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+23632, libc.VaList(bp+8, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TTrigger)(unsafe.Pointer(pTrigger)).FzName))
		_sqlite3ChangeCookie(tls, pParse, iDb)
		_sqlite3VdbeAddOp4(tls, v, int32(OP_DropTrigger), iDb, 0, 0, (*TTrigger)(unsafe.Pointer(pTrigger)).FzName, 0)
	}
}

// C documentation
//
//	/*
//	** This routine is called to report the final ")" that terminates
//	** a CREATE TABLE statement.
//	**
//	** The table structure that other action routines have been building
//	** is added to the internal hash tables, assuming no errors have
//	** occurred.
//	**
//	** An entry for the table is made in the schema table on disk, unless
//	** this is a temporary table or db->init.busy==1.  When db->init.busy==1
//	** it means we are reading the sqlite_schema table because we just
//	** connected to the database or because the sqlite_schema table has
//	** recently changed, so the entry for this table already exists in
//	** the sqlite_schema table.  We do not want to create it again.
//	**
//	** If the pSelect argument is not NULL, it means that this routine
//	** was called to create a table generated from a
//	** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
//	** the new table will match the result set of the SELECT.
//	*/
func _sqlite3EndTable(tls *libc.TLS, pParse uintptr, pCons uintptr, pEnd uintptr, tabOpts Tu32, pSelect uintptr) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var addrInsLoop, addrTop, iCsr, iDb, ii, ii1, n, nNG, regRec, regRowid, regYield, v4 int32
	var colFlags Tu32
	var db, p, pCol, pDb, pEnd2, pIdx, pOld, pSchema, pSelTab, pX, v, zStmt, zType, zType2, v5 uintptr
	var v12 Ti16
	var _ /* dest at bp+0 */ TSelectDest
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrInsLoop, addrTop, colFlags, db, iCsr, iDb, ii, ii1, n, nNG, p, pCol, pDb, pEnd2, pIdx, pOld, pSchema, pSelTab, pX, regRec, regRowid, regYield, v, zStmt, zType, zType2, v12, v4, v5 /* The new table */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb                                                                                                                                                                                                                                      /* An implied index of the table */
	if pEnd == uintptr(0) && pSelect == uintptr(0) {
		return
	}
	p = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	if p == uintptr(0) {
		return
	}
	if pSelect == uintptr(0) && _sqlite3ShadowTableName(tls, db, (*TTable)(unsafe.Pointer(p)).FzName) != 0 {
		**(**Tu32)(__ccgo_up(p + 28)) |= uint32(TF_Shadow)
	}
	/* If the db->init.busy is 1 it means we are reading the SQL off the
	 ** "sqlite_schema" or "sqlite_temp_schema" table on the disk.
	 ** So do not write to the disk again.  Extract the root page number
	 ** for the table from the db->init.newTnum field.  (The page number
	 ** should have been put there by the sqliteOpenCb routine.)
	 **
	 ** If the root page number is 1, that means this is the sqlite_schema
	 ** table itself.  So mark it read-only.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 {
		if pSelect != 0 || !(int32((*TTable)(unsafe.Pointer(p)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) && (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1711, 0)
			return
		}
		(*TTable)(unsafe.Pointer(p)).Ftnum = (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum
		if (*TTable)(unsafe.Pointer(p)).Ftnum == uint32(1) {
			**(**Tu32)(__ccgo_up(p + 28)) |= uint32(TF_Readonly)
		}
	}
	/* Special processing for tables that include the STRICT keyword:
	 **
	 **   *  Do not allow custom column datatypes.  Every column must have
	 **      a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB.
	 **
	 **   *  If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY,
	 **      then all columns of the PRIMARY KEY must have a NOT NULL
	 **      constraint.
	 */
	if tabOpts&uint32(TF_Strict) != 0 {
		**(**Tu32)(__ccgo_up(p + 28)) |= uint32(TF_Strict)
		ii = 0
		for {
			if !(ii < int32((*TTable)(unsafe.Pointer(p)).FnCol)) {
				break
			}
			pCol = (*TTable)(unsafe.Pointer(p)).FaCol + uintptr(ii)*12
			if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf0>>4)) == COLTYPE_CUSTOM {
				if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_HASTYPE) != 0 {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15613, libc.VaList(bp+40, (*TTable)(unsafe.Pointer(p)).FzName, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, _sqlite3ColumnType(tls, pCol, __ccgo_ts+1711)))
				} else {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15646, libc.VaList(bp+40, (*TTable)(unsafe.Pointer(p)).FzName, (*TColumn)(unsafe.Pointer(pCol)).FzCnName))
				}
				return
			} else {
				if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf0>>4)) == int32(COLTYPE_ANY) {
					(*TColumn)(unsafe.Pointer(pCol)).Faffinity = int8(SQLITE_AFF_BLOB)
				}
			}
			if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 && int32((*TTable)(unsafe.Pointer(p)).FiPKey) != ii && int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) == OE_None {
				libc.SetBitFieldPtr8Uint32(pCol+4, libc.Uint32FromInt32(OE_Abort), 0, 0xf)
				**(**Tu32)(__ccgo_up(p + 28)) |= uint32(TF_HasNotNull)
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
	}
	/* Special processing for WITHOUT ROWID Tables */
	if tabOpts&uint32(TF_WithoutRowid) != 0 {
		if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_Autoincrement) != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15673, 0)
			return
		}
		if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_HasPrimaryKey) == uint32(0) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15723, libc.VaList(bp+40, (*TTable)(unsafe.Pointer(p)).FzName))
			return
		}
		**(**Tu32)(__ccgo_up(p + 28)) |= uint32(libc.Int32FromInt32(TF_WithoutRowid) | libc.Int32FromInt32(TF_NoVisibleRowid))
		_convertToWithoutRowidTable(tls, pParse, p)
	}
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(p)).FpSchema)
	/* Resolve names in all CHECK constraint expressions.
	 */
	if (*TTable)(unsafe.Pointer(p)).FpCheck != 0 {
		_sqlite3ResolveSelfReference(tls, pParse, p, int32(NC_IsCheck), uintptr(0), (*TTable)(unsafe.Pointer(p)).FpCheck)
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			/* If errors are seen, delete the CHECK constraints now, else they might
			 ** actually be used if PRAGMA writable_schema=ON is set. */
			_sqlite3ExprListDelete(tls, db, (*TTable)(unsafe.Pointer(p)).FpCheck)
			(*TTable)(unsafe.Pointer(p)).FpCheck = uintptr(0)
		} else {
		}
	}
	if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_HasGenerated) != 0 {
		nNG = 0
		ii1 = 0
		for {
			if !(ii1 < int32((*TTable)(unsafe.Pointer(p)).FnCol)) {
				break
			}
			colFlags = uint32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(ii1)*12))).FcolFlags)
			if colFlags&uint32(COLFLAG_GENERATED) != uint32(0) {
				pX = _sqlite3ColumnExpr(tls, p, (*TTable)(unsafe.Pointer(p)).FaCol+uintptr(ii1)*12)
				if _sqlite3ResolveSelfReference(tls, pParse, p, int32(NC_GenCol), pX, uintptr(0)) != 0 {
					/* If there are errors in resolving the expression, change the
					 ** expression to a NULL.  This prevents code generators that operate
					 ** on the expression from inserting extra parts into the expression
					 ** tree that have been allocated from lookaside memory, which is
					 ** illegal in a schema and will lead to errors or heap corruption
					 ** when the database connection closes. */
					_sqlite3ColumnSetExpr(tls, pParse, p, (*TTable)(unsafe.Pointer(p)).FaCol+uintptr(ii1)*12, _sqlite3ExprAlloc(tls, db, int32(TK_NULL), uintptr(0), 0))
				}
			} else {
				nNG = nNG + 1
			}
			goto _2
		_2:
			;
			ii1 = ii1 + 1
		}
		if nNG == 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15755, 0)
			return
		}
	}
	/* Estimate the average row size for the table and for all implied indices */
	_estimateTableWidth(tls, p)
	pIdx = (*TTable)(unsafe.Pointer(p)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		_estimateIndexWidth(tls, pIdx)
		goto _3
	_3:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
	}
	/* If not initializing, then create a record for the new table
	 ** in the schema table of the database.
	 **
	 ** If this is a TEMPORARY table, write the entry into the auxiliary
	 ** file instead of into the main database file.
	 */
	if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) { /* Text of the CREATE TABLE or CREATE VIEW statement */
		v = _sqlite3GetVdbe(tls, pParse)
		if v == uintptr(0) {
			return
		}
		_sqlite3VdbeAddOp1(tls, v, int32(OP_Close), 0)
		/*
		 ** Initialize zType for the new view or table.
		 */
		if int32((*TTable)(unsafe.Pointer(p)).FeTabType) == TABTYP_NORM {
			/* A regular table */
			zType = __ccgo_ts + 10594
			zType2 = __ccgo_ts + 15799
		} else {
			/* A view */
			zType = __ccgo_ts + 12332
			zType2 = __ccgo_ts + 15805
		}
		/* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
		 ** statement to populate the new table. The root-page number for the
		 ** new table is in register pParse->u1.cr.regRoot.
		 **
		 ** Once the SELECT has been coded by sqlite3Select(), it is in a
		 ** suitable state to query for the column names and types to be used
		 ** by the new table.
		 **
		 ** A shared-cache write-lock is not required to write to the new table,
		 ** as a schema-lock must have already been obtained to create it. Since
		 ** a schema-lock excludes all other database users, the write-lock would
		 ** be redundant.
		 */
		if pSelect != 0 { /* Write cursor on the new table */
			if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != PARSE_MODE_NORMAL {
				(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_ERROR)
				(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
				return
			}
			v5 = pParse + 44
			v4 = *(*int32)(unsafe.Pointer(v5))
			*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
			iCsr = v4
			v5 = pParse + 48
			*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
			v4 = *(*int32)(unsafe.Pointer(v5))
			regYield = v4
			v5 = pParse + 48
			*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
			v4 = *(*int32)(unsafe.Pointer(v5))
			regRec = v4
			v5 = pParse + 48
			*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
			v4 = *(*int32)(unsafe.Pointer(v5))
			regRowid = v4
			_sqlite3MayAbort(tls, pParse)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_OpenWrite), iCsr, (*(*struct {
				FaddrCrTab      int32
				FregRowid       int32
				FregRoot        int32
				FconstraintName TToken
			})(unsafe.Pointer(pParse + 188))).FregRoot, iDb)
			_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_P2ISREG))
			addrTop = _sqlite3VdbeCurrentAddr(tls, v) + int32(1)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regYield, 0, addrTop)
			if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
				return
			}
			pSelTab = _sqlite3ResultSetOfSelect(tls, pParse, pSelect, int8(SQLITE_AFF_BLOB))
			if pSelTab == uintptr(0) {
				return
			}
			v12 = (*TTable)(unsafe.Pointer(pSelTab)).FnCol
			(*TTable)(unsafe.Pointer(p)).FnNVCol = v12
			(*TTable)(unsafe.Pointer(p)).FnCol = v12
			(*TTable)(unsafe.Pointer(p)).FaCol = (*TTable)(unsafe.Pointer(pSelTab)).FaCol
			(*TTable)(unsafe.Pointer(pSelTab)).FnCol = 0
			(*TTable)(unsafe.Pointer(pSelTab)).FaCol = uintptr(0)
			_sqlite3DeleteTable(tls, db, pSelTab)
			_sqlite3SelectDestInit(tls, bp, int32(SRT_Coroutine), regYield)
			_sqlite3Select(tls, pParse, pSelect, bp)
			if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
				return
			}
			_sqlite3VdbeEndCoroutine(tls, v, regYield)
			_sqlite3VdbeJumpHere(tls, v, addrTop-int32(1))
			addrInsLoop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (**(**TSelectDest)(__ccgo_up(bp))).FiSDParm)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), (**(**TSelectDest)(__ccgo_up(bp))).FiSdst, (**(**TSelectDest)(__ccgo_up(bp))).FnSdst, regRec)
			_sqlite3TableAffinity(tls, v, p, 0)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iCsr, regRowid)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iCsr, regRec, regRowid)
			_sqlite3VdbeGoto(tls, v, addrInsLoop)
			_sqlite3VdbeJumpHere(tls, v, addrInsLoop)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iCsr)
		}
		/* Compute the complete text of the CREATE statement */
		if pSelect != 0 {
			zStmt = _createTableStmt(tls, db, p)
		} else {
			if tabOpts != 0 {
				v5 = pParse + 208
			} else {
				v5 = pEnd
			}
			pEnd2 = v5
			n = int32((*TToken)(unsafe.Pointer(pEnd2)).Fz) - int32((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz)
			if int32(**(**int8)(__ccgo_up((*TToken)(unsafe.Pointer(pEnd2)).Fz))) != int32(';') {
				n = int32(uint32(n) + (*TToken)(unsafe.Pointer(pEnd2)).Fn)
			}
			zStmt = _sqlite3MPrintf(tls, db, __ccgo_ts+15810, libc.VaList(bp+40, zType2, n, (*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz))
		}
		/* A slot for the record has already been allocated in the
		 ** schema table.  We just need to update that slot with all
		 ** the information we've collected.
		 */
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+15825, libc.VaList(bp+40, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, zType, (*TTable)(unsafe.Pointer(p)).FzName, (*TTable)(unsafe.Pointer(p)).FzName, (*(*struct {
			FaddrCrTab      int32
			FregRowid       int32
			FregRoot        int32
			FconstraintName TToken
		})(unsafe.Pointer(pParse + 188))).FregRoot, zStmt, (*(*struct {
			FaddrCrTab      int32
			FregRowid       int32
			FregRoot        int32
			FconstraintName TToken
		})(unsafe.Pointer(pParse + 188))).FregRowid))
		_sqlite3DbFree(tls, db, zStmt)
		_sqlite3ChangeCookie(tls, pParse, iDb)
		/* Check to see if we need to create an sqlite_sequence table for
		 ** keeping track of autoincrement keys.
		 */
		if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_Autoincrement) != uint32(0) && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != libc.Int32FromInt32(PARSE_MODE_NORMAL)) {
			pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16
			if (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).FpSeqTab == uintptr(0) {
				_sqlite3NestedParse(tls, pParse, __ccgo_ts+15923, libc.VaList(bp+40, (*TDb)(unsafe.Pointer(pDb)).FzDbSName))
			}
		}
		/* Reparse everything to update our internal data structures */
		_sqlite3VdbeAddParseSchemaOp(tls, v, iDb, _sqlite3MPrintf(tls, db, __ccgo_ts+15965, libc.VaList(bp+40, (*TTable)(unsafe.Pointer(p)).FzName)), uint16(0))
		/* Test for cycles in generated columns and illegal expressions
		 ** in CHECK constraints and in DEFAULT clauses. */
		if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_HasGenerated) != 0 {
			_sqlite3VdbeAddOp4(tls, v, int32(OP_SqlExec), int32(0x0001), 0, 0, _sqlite3MPrintf(tls, db, __ccgo_ts+15999, libc.VaList(bp+40, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TTable)(unsafe.Pointer(p)).FzName)), -int32(7))
		}
	}
	/* Add the table to the in-memory representation of the database.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 {
		pSchema = (*TTable)(unsafe.Pointer(p)).FpSchema
		pOld = _sqlite3HashInsert(tls, pSchema+8, (*TTable)(unsafe.Pointer(p)).FzName, p)
		if pOld != 0 {
			/* Malloc must have failed inside HashInsert() */
			_sqlite3OomFault(tls, db)
			return
		}
		(*TParse)(unsafe.Pointer(pParse)).FpNewTable = uintptr(0)
		**(**Tu32)(__ccgo_up(db + 24)) |= uint32(DBFLAG_SchemaChange)
		/* If this is the magic sqlite_sequence table used by autoincrement,
		 ** then record a pointer to this table in the main database structure
		 ** so that INSERT can find the table easily.  */
		if libc.Xstrcmp(tls, (*TTable)(unsafe.Pointer(p)).FzName, __ccgo_ts+11116) == 0 {
			(*TSchema)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FpSchema)).FpSeqTab = p
		}
	}
	if !(pSelect != 0) && int32((*TTable)(unsafe.Pointer(p)).FeTabType) == TABTYP_NORM {
		if (*TToken)(unsafe.Pointer(pCons)).Fz == uintptr(0) {
			pCons = pEnd
		}
		(*(*struct {
			FaddColOffset int32
			FpFKey        uintptr
			FpDfltList    uintptr
		})(unsafe.Pointer(p + 44))).FaddColOffset = int32(13) + (int32((*TToken)(unsafe.Pointer(pCons)).Fz) - int32((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz))
	}
}

// C documentation
//
//	/*
//	** Return a static string that describes the kind of error specified in the
//	** argument.
//	*/
func _sqlite3ErrStr(tls *libc.TLS, rc int32) (r uintptr) {
	var zErr uintptr
	_ = zErr
	zErr = __ccgo_ts + 27064
	switch rc {
	case libc.Int32FromInt32(SQLITE_ABORT) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8):
		zErr = __ccgo_ts + 27078
	case int32(SQLITE_ROW):
		zErr = __ccgo_ts + 27100
	case int32(SQLITE_DONE):
		zErr = __ccgo_ts + 27122
	default:
		rc = rc & int32(0xff)
		if rc >= 0 && rc < int32(libc.Uint32FromInt64(116)/libc.Uint32FromInt64(4)) && _aMsg[rc] != uintptr(0) {
			zErr = _aMsg[rc]
		}
		break
	}
	return zErr
}

// C documentation
//
//	/* The input list pList is the list of result set terms from a RETURNING
//	** clause.  The table that we are returning from is pTab.
//	**
//	** This routine makes a copy of the pList, and at the same time expands
//	** any "*" wildcards to be the complete set of columns from pTab.
//	*/
func _sqlite3ExpandReturning(tls *libc.TLS, pParse uintptr, pList uintptr, pTab uintptr) (r uintptr) {
	var db, pItem, pItem1, pNew, pNewExpr, pNewExpr1, pOldExpr uintptr
	var i, jj int32
	_, _, _, _, _, _, _, _, _ = db, i, jj, pItem, pItem1, pNew, pNewExpr, pNewExpr1, pOldExpr
	pNew = uintptr(0)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
			break
		}
		pOldExpr = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr
		if pOldExpr == uintptr(0) {
			goto _1
		}
		if _isAsteriskTerm(tls, pParse, pOldExpr) != 0 {
			jj = 0
			for {
				if !(jj < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
					break
				}
				if int32((*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(jj)*12)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0 {
					goto _2
				}
				pNewExpr = _sqlite3Expr(tls, db, int32(TK_ID), (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(jj)*12))).FzCnName)
				pNew = _sqlite3ExprListAppend(tls, pParse, pNew, pNewExpr)
				if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
					pItem = pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20
					(*TExprList_item)(unsafe.Pointer(pItem)).FzEName = _sqlite3DbStrDup(tls, db, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(jj)*12))).FzCnName)
					libc.SetBitFieldPtr16Uint32(pItem+8+4, libc.Uint32FromInt32(ENAME_NAME), 0, 0x3)
				}
				goto _2
			_2:
				;
				jj = jj + 1
			}
		} else {
			pNewExpr1 = _sqlite3ExprDup(tls, db, pOldExpr, 0)
			pNew = _sqlite3ExprListAppend(tls, pParse, pNew, pNewExpr1)
			if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) && (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FzEName != uintptr(0) {
				pItem1 = pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*20
				(*TExprList_item)(unsafe.Pointer(pItem1)).FzEName = _sqlite3DbStrDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FzEName)
				libc.SetBitFieldPtr16Uint32(pItem1+8+4, uint32(int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0))), 0, 0x3)
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return pNew
}

// C documentation
//
//	/*
//	** The SrcItem structure passed as the second argument represents a
//	** sub-query in the FROM clause of a SELECT statement. This function
//	** allocates and populates the SrcItem.pTab object. If successful,
//	** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
//	** SQLITE_NOMEM.
//	*/
func _sqlite3ExpandSubquery(tls *libc.TLS, pParse uintptr, pFrom uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pSel, pTab, v1 uintptr
	var v2 int32
	_, _, _, _ = pSel, pTab, v1, v2
	pSel = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pFrom + 44)))).FpSelect
	v1 = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(80))
	pTab = v1
	(*TSrcItem)(unsafe.Pointer(pFrom)).FpSTab = v1
	if pTab == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	(*TTable)(unsafe.Pointer(pTab)).FnTabRef = uint32(1)
	if (*TSrcItem)(unsafe.Pointer(pFrom)).FzAlias != 0 {
		(*TTable)(unsafe.Pointer(pTab)).FzName = _sqlite3DbStrDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TSrcItem)(unsafe.Pointer(pFrom)).FzAlias)
	} else {
		(*TTable)(unsafe.Pointer(pTab)).FzName = _sqlite3MPrintf(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, __ccgo_ts+22485, libc.VaList(bp+8, pFrom))
	}
	for (*TSelect)(unsafe.Pointer(pSel)).FpPrior != 0 {
		pSel = (*TSelect)(unsafe.Pointer(pSel)).FpPrior
	}
	_sqlite3ColumnsFromExprList(tls, pParse, (*TSelect)(unsafe.Pointer(pSel)).FpEList, pTab+34, pTab+4)
	(*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1))
	(*TTable)(unsafe.Pointer(pTab)).FeTabType = uint8(TABTYP_VIEW)
	(*TTable)(unsafe.Pointer(pTab)).FnRowLogEst = int16(200)
	/* The usual case - do not allow ROWID on a subquery */
	**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(libc.Int32FromInt32(TF_Ephemeral) | libc.Int32FromInt32(TF_NoVisibleRowid))
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		v2 = int32(SQLITE_ERROR)
	} else {
		v2 = SQLITE_OK
	}
	return v2
}

// C documentation
//
//	/*
//	** Mark every prepared statement associated with a database connection
//	** as expired.
//	**
//	** An expired statement means that recompilation of the statement is
//	** recommend.  Statements expire when things happen that make their
//	** programs obsolete.  Removing user-defined functions or collating
//	** sequences, or changing an authorization function are the types of
//	** things that make prepared statements obsolete.
//	**
//	** If iCode is 1, then expiration is advisory.  The statement should
//	** be reprepared before being restarted, but if it is already running
//	** it is allowed to run to completion.
//	**
//	** Internally, this function just sets the Vdbe.expired flag on all
//	** prepared statements.  The flag is set to 1 for an immediate expiration
//	** and set to 2 for an advisory expiration.
//	*/
func _sqlite3ExpirePreparedStatements(tls *libc.TLS, db uintptr, iCode int32) {
	var p uintptr
	_ = p
	p = (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe
	for {
		if !(p != 0) {
			break
		}
		libc.SetBitFieldPtr16Uint32(p+152, uint32(iCode+libc.Int32FromInt32(1)), 0, 0x3)
		goto _1
	_1:
		;
		p = (*TVdbe)(unsafe.Pointer(p)).FpVNext
	}
}

// C documentation
//
//	/*
//	** Attach an ORDER BY clause to a function call.
//	**
//	**     functionname( arguments ORDER BY sortlist )
//	**     \_____________________/          \______/
//	**             pExpr                    pOrderBy
//	**
//	** The ORDER BY clause is inserted into a new Expr node of type TK_ORDER
//	** and added to the Expr.pLeft field of the parent TK_FUNCTION node.
//	*/
func _sqlite3ExprAddFunctionOrderBy(tls *libc.TLS, pParse uintptr, pExpr uintptr, pOrderBy uintptr) {
	var db, pOB uintptr
	_, _ = db, pOB
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if pOrderBy == uintptr(0) {
		return
	}
	if pExpr == uintptr(0) {
		_sqlite3ExprListDelete(tls, db, pOrderBy)
		return
	}
	if *(*uintptr)(unsafe.Pointer(pExpr + 20)) == uintptr(0) || (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr == 0 {
		/* Ignore ORDER BY on zero-argument aggregates */
		_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3ExprListDeleteGeneric), pOrderBy)
		return
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) && int32((*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FeFrmType) != int32(TK_FILTER) {
		_sqlite3ExprOrderByAggregateError(tls, pParse, pExpr)
		_sqlite3ExprListDelete(tls, db, pOrderBy)
		return
	}
	if (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr > **(**int32)(__ccgo_up(db + 120 + 2*4)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8842, 0)
		_sqlite3ExprListDelete(tls, db, pOrderBy)
		return
	}
	pOB = _sqlite3ExprAlloc(tls, db, int32(TK_ORDER), uintptr(0), 0)
	if pOB == uintptr(0) {
		_sqlite3ExprListDelete(tls, db, pOrderBy)
		return
	}
	*(*uintptr)(unsafe.Pointer(pOB + 20)) = pOrderBy
	(*TExpr)(unsafe.Pointer(pExpr)).FpLeft = pOB
	**(**Tu32)(__ccgo_up(pOB + 4)) |= uint32(libc.Int32FromInt32(EP_FullSize))
}

// C documentation
//
//	/*
//	** Return the 'affinity' of the expression pExpr if any.
//	**
//	** If pExpr is a column, a reference to a column via an 'AS' alias,
//	** or a sub-select with a column as the return value, then the
//	** affinity of that column is returned. Otherwise, 0x00 is returned,
//	** indicating no affinity for the expression.
//	**
//	** i.e. the WHERE clause expressions in the following statements all
//	** have an affinity:
//	**
//	** CREATE TABLE t1(a);
//	** SELECT * FROM t1 WHERE a;
//	** SELECT a AS b FROM t1 WHERE b;
//	** SELECT * FROM t1 WHERE (select a from t1);
//	*/
func _sqlite3ExprAffinity(tls *libc.TLS, pExpr uintptr) (r int8) {
	var op int32
	_ = op
	op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop)
	for int32(1) != 0 {
		if op == int32(TK_COLUMN) || op == int32(TK_AGG_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab != uintptr(0) {
			return _sqlite3TableColumnAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn))
		}
		if op == int32(TK_SELECT) {
			return _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList + 8))).FpExpr)
		}
		if op == int32(TK_CAST) {
			return _sqlite3AffinityType(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), uintptr(0))
		}
		if op == int32(TK_SELECT_COLUMN) {
			return _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20)))).FpEList + 8 + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)*20))).FpExpr)
		}
		if op == int32(TK_VECTOR) || op == int32(TK_FUNCTION) && int32((*TExpr)(unsafe.Pointer(pExpr)).FaffExpr) == int32(SQLITE_AFF_DEFER) {
			return _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr)
		}
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Skip)|libc.Int32FromInt32(EP_IfNullRow)) != uint32(0) {
			pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
			op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop)
			continue
		}
		if op != int32(TK_REGISTER) {
			break
		}
		op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2)
		if op == int32(TK_REGISTER) {
			break
		}
	}
	return (*TExpr)(unsafe.Pointer(pExpr)).FaffExpr
}

// C documentation
//
//	/*
//	** This routine is the core allocator for Expr nodes.
//	**
//	** Construct a new expression node and return a pointer to it.  Memory
//	** for this node and for the pToken argument is a single allocation
//	** obtained from sqlite3DbMalloc().  The calling function
//	** is responsible for making sure the node eventually gets freed.
//	**
//	** If dequote is true, then the token (if it exists) is dequoted.
//	** If dequote is false, no dequoting is performed.  The deQuote
//	** parameter is ignored if pToken is NULL or if the token does not
//	** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
//	** then the EP_DblQuoted flag is set on the expression node.
//	**
//	** Special case (tag-20240227-a):  If op==TK_INTEGER and pToken points to
//	** a string that can be translated into a 32-bit integer, then the token is
//	** not stored in u.zToken.  Instead, the integer values is written
//	** into u.iValue and the EP_IntValue flag is set. No extra storage
//	** is allocated to hold the integer text and the dequote flag is ignored.
//	** See also tag-20240227-b.
//	*/
func _sqlite3ExprAlloc(tls *libc.TLS, db uintptr, op int32, pToken uintptr, dequote int32) (r uintptr) {
	var nExtra int32
	var pNew uintptr
	var v1 uint32
	_, _, _ = nExtra, pNew, v1
	if pToken != 0 {
		v1 = (*TToken)(unsafe.Pointer(pToken)).Fn + uint32(1)
	} else {
		v1 = uint32(0)
	}
	nExtra = int32(v1)
	pNew = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(52)+uint32(nExtra)))
	if pNew != 0 {
		libc.Xmemset(tls, pNew, 0, uint32(52))
		(*TExpr)(unsafe.Pointer(pNew)).Fop = uint8(op)
		(*TExpr)(unsafe.Pointer(pNew)).FiAgg = int16(-int32(1))
		if nExtra != 0 {
			*(*uintptr)(unsafe.Pointer(pNew + 8)) = pNew + 1*52
			if (*TToken)(unsafe.Pointer(pToken)).Fn != 0 {
				libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(pNew + 8)), (*TToken)(unsafe.Pointer(pToken)).Fz, (*TToken)(unsafe.Pointer(pToken)).Fn)
			}
			**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pNew + 8)) + uintptr((*TToken)(unsafe.Pointer(pToken)).Fn))) = 0
			if dequote != 0 && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pNew + 8)))))])&int32(0x80) != 0 {
				_sqlite3DequoteExpr(tls, pNew)
			}
		}
		(*TExpr)(unsafe.Pointer(pNew)).FnHeight = int32(1)
	}
	return pNew
}

// C documentation
//
//	/*
//	** Assign a variable number to an expression that encodes a wildcard
//	** in the original SQL statement.
//	**
//	** Wildcards consisting of a single "?" are assigned the next sequential
//	** variable number.
//	**
//	** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
//	** sure "nnn" is not too big to avoid a denial of service attack when
//	** the SQL statement comes from an external source.
//	**
//	** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
//	** as the previous instance of the same wildcard.  Or if this is the first
//	** instance of the wildcard, the next sequential variable number is
//	** assigned.
//	*/
func _sqlite3ExprAssignVarNumber(tls *libc.TLS, pParse uintptr, pExpr uintptr, n Tu32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bOk, doAdd int32
	var db, z, v2 uintptr
	var x, v1 TynVar
	var _ /* i at bp+0 */ Ti64
	_, _, _, _, _, _, _ = bOk, db, doAdd, x, z, v1, v2
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if pExpr == uintptr(0) {
		return
	}
	z = *(*uintptr)(unsafe.Pointer(pExpr + 8))
	if int32(**(**int8)(__ccgo_up(z + 1))) == 0 {
		/* Wildcard of the form "?".  Assign the next variable number */
		v2 = pParse + 216
		*(*TynVar)(unsafe.Pointer(v2)) = *(*TynVar)(unsafe.Pointer(v2)) + 1
		v1 = *(*TynVar)(unsafe.Pointer(v2))
		x = v1
	} else {
		doAdd = 0
		if int32(**(**int8)(__ccgo_up(z))) == int32('?') {
			if n == uint32(2) { /*OPTIMIZATION-IF-TRUE*/
				**(**Ti64)(__ccgo_up(bp)) = int64(int32(**(**int8)(__ccgo_up(z + 1))) - int32('0')) /* The common case of ?N for a single digit N */
				bOk = int32(1)
			} else {
				bOk = libc.BoolInt32(0 == _sqlite3Atoi64(tls, z+1, bp, int32(n-uint32(1)), uint8(SQLITE_UTF8)))
			}
			if bOk == 0 || **(**Ti64)(__ccgo_up(bp)) < int64(1) || **(**Ti64)(__ccgo_up(bp)) > int64(**(**int32)(__ccgo_up(db + 120 + 9*4))) {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9276, libc.VaList(bp+16, **(**int32)(__ccgo_up(db + 120 + 9*4))))
				_sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr)
				return
			}
			x = int16(**(**Ti64)(__ccgo_up(bp)))
			if int32(x) > int32((*TParse)(unsafe.Pointer(pParse)).FnVar) {
				(*TParse)(unsafe.Pointer(pParse)).FnVar = int16(int32(x))
				doAdd = int32(1)
			} else {
				if _sqlite3VListNumToName(tls, (*TParse)(unsafe.Pointer(pParse)).FpVList, int32(x)) == uintptr(0) {
					doAdd = int32(1)
				}
			}
		} else {
			/* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
			 ** number as the prior appearance of the same name, or if the name
			 ** has never appeared before, reuse the same variable number
			 */
			x = int16(_sqlite3VListNameToNum(tls, (*TParse)(unsafe.Pointer(pParse)).FpVList, z, int32(n)))
			if int32(x) == 0 {
				v2 = pParse + 216
				*(*TynVar)(unsafe.Pointer(v2)) = *(*TynVar)(unsafe.Pointer(v2)) + 1
				v1 = *(*TynVar)(unsafe.Pointer(v2))
				x = v1
				doAdd = int32(1)
			}
		}
		if doAdd != 0 {
			(*TParse)(unsafe.Pointer(pParse)).FpVList = _sqlite3VListAdd(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpVList, z, int32(n), int32(x))
		}
	}
	(*TExpr)(unsafe.Pointer(pExpr)).FiColumn = x
	if int32(x) > **(**int32)(__ccgo_up(db + 120 + 9*4)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9319, 0)
		_sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr)
	}
}

// C documentation
//
//	/*
//	** Return FALSE if there is no chance that the expression can be NULL.
//	**
//	** If the expression might be NULL or if the expression is too complex
//	** to tell return TRUE.
//	**
//	** This routine is used as an optimization, to skip OP_IsNull opcodes
//	** when we know that a value cannot be NULL.  Hence, a false positive
//	** (returning TRUE when in fact the expression can never be NULL) might
//	** be a small performance hit but is otherwise harmless.  On the other
//	** hand, a false negative (returning FALSE when the result could be NULL)
//	** will likely result in an incorrect answer.  So when in doubt, return
//	** TRUE.
//	*/
func _sqlite3ExprCanBeNull(tls *libc.TLS, p uintptr) (r int32) {
	var op Tu8
	_ = op
	for int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_UPLUS) || int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_UMINUS) {
		p = (*TExpr)(unsafe.Pointer(p)).FpLeft
	}
	op = (*TExpr)(unsafe.Pointer(p)).Fop
	if int32(op) == int32(TK_REGISTER) {
		op = (*TExpr)(unsafe.Pointer(p)).Fop2
	}
	switch int32(op) {
	case int32(TK_INTEGER):
		fallthrough
	case int32(TK_STRING):
		fallthrough
	case int32(TK_FLOAT):
		fallthrough
	case int32(TK_BLOB):
		return 0
	case int32(TK_COLUMN):
		return libc.BoolInt32((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_CanBeNull)) != uint32(0) || (*TExpr)(unsafe.Pointer(p)).Fy.FpTab == uintptr(0) || int32((*TExpr)(unsafe.Pointer(p)).FiColumn) >= 0 && (*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FaCol != uintptr(0) && int32((*TExpr)(unsafe.Pointer(p)).FiColumn) < int32((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FnCol) && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FaCol + uintptr((*TExpr)(unsafe.Pointer(p)).FiColumn)*12 + 4))&0xf>>0)) == 0)
	default:
		return int32(1)
	}
	return r
}

// C documentation
//
//	/*
//	** Return TRUE if expression pExpr is able to return a subtype.
//	**
//	** A TRUE return does not guarantee that a subtype will be returned.
//	** It only indicates that a subtype return is possible.  False positives
//	** are acceptable as they only disable an optimization.  False negatives,
//	** on the other hand, can lead to incorrect answers.
//	*/
func _sqlite3ExprCanReturnSubtype(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var _ /* w at bp+0 */ TWalker
	libc.Xmemset(tls, bp, 0, uint32(28))
	(**(**TWalker)(__ccgo_up(bp))).FpParse = pParse
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_exprNodeCanReturnSubtype)
	_sqlite3WalkExpr(tls, bp, pExpr)
	return int32((**(**TWalker)(__ccgo_up(bp))).FeCode)
}

// C documentation
//
//	/*
//	** Check that argument nHeight is less than or equal to the maximum
//	** expression depth allowed. If it is not, leave an error message in
//	** pParse.
//	*/
func _sqlite3ExprCheckHeight(tls *libc.TLS, pParse uintptr, nHeight int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var mxHeight, rc int32
	_, _ = mxHeight, rc
	rc = SQLITE_OK
	mxHeight = **(**int32)(__ccgo_up((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 3*4))
	if nHeight > mxHeight {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9078, libc.VaList(bp+8, mxHeight))
		rc = int32(SQLITE_ERROR)
	}
	return rc
}

// C documentation
//
//	/*
//	** Generate code that pushes the value of every element of the given
//	** expression list into a sequence of registers beginning at target.
//	**
//	** Return the number of elements evaluated.  The number returned will
//	** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
//	** is defined.
//	**
//	** The SQLITE_ECEL_DUP flag prevents the arguments from being
//	** filled using OP_SCopy.  OP_Copy must be used instead.
//	**
//	** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
//	** factored out into initialization code.
//	**
//	** The SQLITE_ECEL_REF flag means that expressions in the list with
//	** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
//	** in registers at srcReg, and so the value can be copied from there.
//	** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
//	** are simply omitted rather than being copied from srcReg.
//	*/
func _sqlite3ExprCodeExprList(tls *libc.TLS, pParse uintptr, pList uintptr, target int32, srcReg int32, flags Tu8) (r int32) {
	var copyOp Tu8
	var i, inReg, j, n, v1 int32
	var pExpr, pItem, pOp, v, v5 uintptr
	var v4 bool
	_, _, _, _, _, _, _, _, _, _, _, _ = copyOp, i, inReg, j, n, pExpr, pItem, pOp, v, v1, v4, v5
	if int32(flags)&int32(SQLITE_ECEL_DUP) != 0 {
		v1 = int32(OP_Copy)
	} else {
		v1 = int32(OP_SCopy)
	}
	copyOp = uint8(v1)
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	/* Never gets this far otherwise */
	n = (*TExprList)(unsafe.Pointer(pList)).FnExpr
	if !(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x80>>7)) != 0) {
		flags = uint8(int32(flags) & ^libc.Int32FromInt32(SQLITE_ECEL_FACTOR))
	}
	pItem = pList + 8
	i = libc.Int32FromInt32(0)
	for {
		if !(i < n) {
			break
		}
		pExpr = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr
		if v4 = int32(flags)&int32(SQLITE_ECEL_REF) != 0; v4 {
			v1 = int32((*(*struct {
				FiOrderByCol Tu16
				FiAlias      Tu16
			})(unsafe.Pointer(pItem + 16))).FiOrderByCol)
			j = v1
		}
		if v4 && v1 > 0 {
			if int32(flags)&int32(SQLITE_ECEL_OMITREF) != 0 {
				i = i - 1
				n = n - 1
			} else {
				_sqlite3VdbeAddOp2(tls, v, int32(copyOp), j+srcReg-int32(1), target+i)
			}
		} else {
			if int32(flags)&int32(SQLITE_ECEL_FACTOR) != 0 && _sqlite3ExprIsConstantNotJoin(tls, pParse, pExpr) != 0 {
				_sqlite3ExprCodeRunJustOnce(tls, pParse, pExpr, target+i)
			} else {
				inReg = _sqlite3ExprCodeTarget(tls, pParse, pExpr, target+i)
				if inReg != target+i {
					if v4 = int32(copyOp) == int32(OP_Copy); v4 {
						v5 = _sqlite3VdbeGetLastOp(tls, v)
						pOp = v5
					}
					if v4 && int32((*TVdbeOp)(unsafe.Pointer(v5)).Fopcode) == int32(OP_Copy) && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1+(*TVdbeOp)(unsafe.Pointer(pOp)).Fp3+int32(1) == inReg && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2+(*TVdbeOp)(unsafe.Pointer(pOp)).Fp3+int32(1) == target+i && int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp5) == 0 {
						(*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 = (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 + 1
					} else {
						_sqlite3VdbeAddOp2(tls, v, int32(copyOp), inReg, target+i)
					}
				}
			}
		}
		goto _2
	_2:
		;
		i = i + 1
		pItem += 20
	}
	return n
}

// C documentation
//
//	/*
//	** Generate code that will compute the value of generated column pCol
//	** and store the result in register regOut
//	*/
func _sqlite3ExprCodeGeneratedColumn(tls *libc.TLS, pParse uintptr, pTab uintptr, pCol uintptr, regOut int32) {
	var iAddr, nErr, p3 int32
	var v uintptr
	_, _, _, _ = iAddr, nErr, p3, v
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	nErr = (*TParse)(unsafe.Pointer(pParse)).FnErr
	if (*TParse)(unsafe.Pointer(pParse)).FiSelfTab > 0 {
		iAddr = _sqlite3VdbeAddOp3(tls, v, int32(OP_IfNullRow), (*TParse)(unsafe.Pointer(pParse)).FiSelfTab-int32(1), 0, regOut)
	} else {
		iAddr = 0
	}
	_sqlite3ExprCodeCopy(tls, pParse, _sqlite3ColumnExpr(tls, pTab, pCol), regOut)
	if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Strict) != uint32(0) {
		p3 = int32(2) + (int32(pCol)-int32((*TTable)(unsafe.Pointer(pTab)).FaCol))/12
		_sqlite3VdbeAddOp4(tls, v, int32(OP_TypeCheck), regOut, int32(1), p3, pTab, -int32(5))
	} else {
		if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) >= int32(SQLITE_AFF_TEXT) {
			_sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), regOut, int32(1), 0, pCol+5, int32(1))
		}
	}
	if iAddr != 0 {
		_sqlite3VdbeJumpHere(tls, v, iAddr)
	}
	if (*TParse)(unsafe.Pointer(pParse)).FnErr > nErr {
		(*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FerrByteOffset = -int32(1)
	}
}

// C documentation
//
//	/*
//	** Generate code to extract the value of the iCol-th column of a table.
//	*/
func _sqlite3ExprCodeGetColumnOfTable(tls *libc.TLS, v uintptr, pTab uintptr, iTabCur int32, iCol int32, regOut int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var op, savedSelfTab, x int32
	var pCol, pParse, v1 uintptr
	_, _, _, _, _, _ = op, pCol, pParse, savedSelfTab, x, v1
	if iCol < 0 || iCol == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iTabCur, regOut)
	} else {
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
			op = int32(OP_VColumn)
			x = iCol
		} else {
			v1 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12
			pCol = v1
			if int32((*TColumn)(unsafe.Pointer(v1)).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
				pParse = _sqlite3VdbeParser(tls, v)
				if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_BUSY) != 0 {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9669, libc.VaList(bp+8, (*TColumn)(unsafe.Pointer(pCol)).FzCnName))
				} else {
					savedSelfTab = (*TParse)(unsafe.Pointer(pParse)).FiSelfTab
					v1 = pCol + 10
					*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(COLFLAG_BUSY))
					(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = iTabCur + int32(1)
					_sqlite3ExprCodeGeneratedColumn(tls, pParse, pTab, pCol, regOut)
					(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = savedSelfTab
					v1 = pCol + 10
					*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(COLFLAG_BUSY))
				}
				return
			} else {
				if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
					x = _sqlite3TableColumnToIndex(tls, _sqlite3PrimaryKeyIndex(tls, pTab), iCol)
					op = int32(OP_Column)
				} else {
					x = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(iCol)))
					op = int32(OP_Column)
				}
			}
		}
		_sqlite3VdbeAddOp3(tls, v, op, iTabCur, x, regOut)
		_sqlite3ColumnDefault(tls, v, pTab, iCol, regOut)
	}
}

// C documentation
//
//	/*
//	** Generate code for an IN expression.
//	**
//	**      x IN (SELECT ...)
//	**      x IN (value, value, ...)
//	**
//	** The left-hand side (LHS) is a scalar or vector expression.  The
//	** right-hand side (RHS) is an array of zero or more scalar values, or a
//	** subquery.  If the RHS is a subquery, the number of result columns must
//	** match the number of columns in the vector on the LHS.  If the RHS is
//	** a list of values, the LHS must be a scalar.
//	**
//	** The IN operator is true if the LHS value is contained within the RHS.
//	** The result is false if the LHS is definitely not in the RHS.  The
//	** result is NULL if the presence of the LHS in the RHS cannot be
//	** determined due to NULLs.
//	**
//	** This routine generates code that jumps to destIfFalse if the LHS is not
//	** contained within the RHS.  If due to NULLs we cannot determine if the LHS
//	** is contained in the RHS then jump to destIfNull.  If the LHS is contained
//	** within the RHS then fall through.
//	**
//	** See the separate in-operator.md documentation file in the canonical
//	** SQLite source tree for additional information.
//	*/
func _sqlite3ExprCodeIN(tls *libc.TLS, pParse uintptr, pExpr uintptr, destIfFalse int32, destIfNull int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var addrTop, addrTruthOp, destNotNull, destStep2, destStep6, eType, i, ii, labelOk, nVector, op, op1, r2, r3, rLhs, rLhsOrig, regCkNull, v3 int32
	var aiMap, p, p1, pColl, pColl1, pLeft, pList, pOp, pRhs, v, zAff, v1 uintptr
	var okConstFactor Tu8
	var _ /* iDummy at bp+4 */ int32
	var _ /* iTab at bp+8 */ int32
	var _ /* rRhsHasNull at bp+0 */ int32
	var _ /* regToFree at bp+12 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrTop, addrTruthOp, aiMap, destNotNull, destStep2, destStep6, eType, i, ii, labelOk, nVector, okConstFactor, op, op1, p, p1, pColl, pColl1, pLeft, pList, pOp, pRhs, r2, r3, rLhs, rLhsOrig, regCkNull, v, zAff, v1, v3
	**(**int32)(__ccgo_up(bp)) = 0     /* Statement under construction */
	aiMap = uintptr(0)                 /* Map from vector field to index column */
	zAff = uintptr(0)                  /* Where to jump when NULLs seen in step 2 */
	destStep6 = 0                      /* Top of the step-6 loop */
	**(**int32)(__ccgo_up(bp + 8)) = 0 /* Index to use */
	okConstFactor = uint8(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28)) & 0x80 >> 7)))
	pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
	if _sqlite3ExprCheckIN(tls, pParse, pExpr) != 0 {
		return
	}
	zAff = _exprINAffinity(tls, pParse, pExpr)
	nVector = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
	aiMap = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(nVector)*uint32(4)))
	if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
		goto sqlite3ExprCodeIN_oom_error
	}
	/* Attempt to compute the RHS. After this step, if anything other than
	 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
	 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
	 ** the RHS has not yet been coded.  */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	/* OOM detected prior to this routine */
	if destIfFalse == destIfNull {
		v1 = uintptr(0)
	} else {
		v1 = bp
	}
	eType = _sqlite3FindInIndex(tls, pParse, pExpr, uint32(libc.Int32FromInt32(IN_INDEX_MEMBERSHIP)|libc.Int32FromInt32(IN_INDEX_NOOP_OK)), v1, aiMap, bp+8)
	/* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
	 ** vector, then it is stored in an array of nVector registers starting
	 ** at r1.
	 **
	 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
	 ** so that the fields are in the same order as an existing index.   The
	 ** aiMap[] array contains a mapping from the original LHS field order to
	 ** the field order that matches the RHS index.
	 **
	 ** Avoid factoring the LHS of the IN(...) expression out of the loop,
	 ** even if it is constant, as OP_Affinity may be used on the register
	 ** by code generated below.  */
	libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(0), 7, 0x80)
	rLhs = _exprCodeVector(tls, pParse, pLeft, bp+4)
	libc.SetBitFieldPtr16Uint32(pParse+28, uint32(okConstFactor), 7, 0x80)
	/* If sqlite3FindInIndex() did not find or create an index that is
	 ** suitable for evaluating the IN operator, then evaluate using a
	 ** sequence of comparisons.
	 **
	 ** This is step (1) in the in-operator.md optimized algorithm.
	 */
	if eType == int32(IN_INDEX_NOOP) {
		labelOk = _sqlite3VdbeMakeLabel(tls, pParse)
		regCkNull = 0
		pList = *(*uintptr)(unsafe.Pointer(pExpr + 20))
		pColl = _sqlite3ExprCollSeq(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		if destIfNull != destIfFalse {
			regCkNull = _sqlite3GetTempReg(tls, pParse)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_BitAnd), rLhs, rLhs, regCkNull)
		}
		ii = 0
		for {
			if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
				break
			}
			r2 = _sqlite3ExprCodeTemp(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr, bp+12)
			if regCkNull != 0 && _sqlite3ExprCanBeNull(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr) != 0 {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_BitAnd), regCkNull, r2, regCkNull)
			}
			_sqlite3ReleaseTempReg(tls, pParse, **(**int32)(__ccgo_up(bp + 12)))
			if ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1) || destIfNull != destIfFalse {
				if rLhs != r2 {
					v3 = int32(OP_Eq)
				} else {
					v3 = int32(OP_NotNull)
				}
				op = v3
				_sqlite3VdbeAddOp4(tls, v, op, rLhs, labelOk, r2, pColl, -int32(2))
				_sqlite3VdbeChangeP5(tls, v, uint16(**(**int8)(__ccgo_up(zAff))))
			} else {
				if rLhs != r2 {
					v3 = int32(OP_Ne)
				} else {
					v3 = int32(OP_IsNull)
				}
				op1 = v3
				_sqlite3VdbeAddOp4(tls, v, op1, rLhs, destIfFalse, r2, pColl, -int32(2))
				_sqlite3VdbeChangeP5(tls, v, uint16(int32(**(**int8)(__ccgo_up(zAff)))|int32(SQLITE_JUMPIFNULL)))
			}
			goto _2
		_2:
			;
			ii = ii + 1
		}
		if regCkNull != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regCkNull, destIfNull)
			_sqlite3VdbeGoto(tls, v, destIfFalse)
		}
		_sqlite3VdbeResolveLabel(tls, v, labelOk)
		_sqlite3ReleaseTempReg(tls, pParse, regCkNull)
		goto sqlite3ExprCodeIN_finished
	}
	if eType != int32(IN_INDEX_ROWID) {
		/* If this IN operator will use an index, then the order of columns in the
		 ** vector might be different from the order in the index.  In that case,
		 ** we need to reorder the LHS values to be in index order.  Run Affinity
		 ** before reordering the columns, so that the affinity is correct.
		 */
		_sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), rLhs, nVector, 0, zAff, nVector)
		i = 0
		for {
			if !(i < nVector && **(**int32)(__ccgo_up(aiMap + uintptr(i)*4)) == i) {
				break
			}
			goto _5
		_5:
			;
			i = i + 1
		} /* Are LHS fields reordered? */
		if i != nVector {
			/* Need to reorder the LHS fields according to aiMap */
			rLhsOrig = rLhs
			rLhs = _sqlite3GetTempRange(tls, pParse, nVector)
			i = 0
			for {
				if !(i < nVector) {
					break
				}
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), rLhsOrig+i, rLhs+**(**int32)(__ccgo_up(aiMap + uintptr(i)*4)), 0)
				goto _6
			_6:
				;
				i = i + 1
			}
			_sqlite3ReleaseTempReg(tls, pParse, rLhsOrig)
		}
	}
	/* Step 2: Check to see if the LHS contains any NULL columns.  If the
	 ** LHS does contain NULLs then the result must be either FALSE or NULL.
	 ** We will then skip the binary search of the RHS.
	 */
	if destIfNull == destIfFalse {
		destStep2 = destIfFalse
	} else {
		v3 = _sqlite3VdbeMakeLabel(tls, pParse)
		destStep6 = v3
		destStep2 = v3
	}
	i = 0
	for {
		if !(i < nVector) {
			break
		}
		p = _sqlite3VectorFieldSubexpr(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, i)
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			goto sqlite3ExprCodeIN_oom_error
		}
		if _sqlite3ExprCanBeNull(tls, p) != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), rLhs+**(**int32)(__ccgo_up(aiMap + uintptr(i)*4)), destStep2)
		}
		goto _8
	_8:
		;
		i = i + 1
	}
	/* Step 3.  The LHS is now known to be non-NULL.  Do the binary search
	 ** of the RHS using the LHS as a probe.  If found, the result is
	 ** true.
	 */
	if eType == int32(IN_INDEX_ROWID) {
		/* In this case, the RHS is the ROWID of table b-tree and so we also
		 ** know that the RHS is non-NULL.  Hence, we combine steps 3 and 4
		 ** into a single opcode. */
		_sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), **(**int32)(__ccgo_up(bp + 8)), destIfFalse, rLhs)
		addrTruthOp = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) /* Return True */
	} else {
		if destIfFalse == destIfNull {
			/* Combine Step 3 and Step 5 into a single opcode */
			if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subrtn)) != uint32(0) {
				pOp = _sqlite3VdbeGetOp(tls, v, (*(*struct {
					FiAddr     int32
					FregReturn int32
				})(unsafe.Pointer(pExpr + 44))).FiAddr)
				if (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 > 0 { /* tag-202407032019 */
					_sqlite3VdbeAddOp4Int(tls, v, int32(OP_Filter), (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3, destIfFalse, rLhs, nVector)
				}
			}
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), **(**int32)(__ccgo_up(bp + 8)), destIfFalse, rLhs, nVector)
			goto sqlite3ExprCodeIN_finished
		}
		/* Ordinary Step 3, for the case where FALSE and NULL are distinct */
		addrTruthOp = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), **(**int32)(__ccgo_up(bp + 8)), 0, rLhs, nVector)
	}
	/* Step 4.  If the RHS is known to be non-NULL and we did not find
	 ** an match on the search above, then the result must be FALSE.
	 */
	if **(**int32)(__ccgo_up(bp)) != 0 && nVector == int32(1) {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), **(**int32)(__ccgo_up(bp)), destIfFalse)
	}
	/* Step 5.  If we do not care about the difference between NULL and
	 ** FALSE, then just return false.
	 */
	if destIfFalse == destIfNull {
		_sqlite3VdbeGoto(tls, v, destIfFalse)
	}
	/* Step 6: Loop through rows of the RHS.  Compare each row to the LHS.
	 ** If any comparison is NULL, then the result is NULL.  If all
	 ** comparisons are FALSE then the final result is FALSE.
	 **
	 ** For a scalar LHS, it is sufficient to check just the first row
	 ** of the RHS.
	 */
	if destStep6 != 0 {
		_sqlite3VdbeResolveLabel(tls, v, destStep6)
	}
	addrTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), **(**int32)(__ccgo_up(bp + 8)), destIfFalse)
	if nVector > int32(1) {
		destNotNull = _sqlite3VdbeMakeLabel(tls, pParse)
	} else {
		/* For nVector==1, combine steps 6 and 7 by immediately returning
		 ** FALSE if the first comparison is not NULL */
		destNotNull = destIfFalse
	}
	i = 0
	for {
		if !(i < nVector) {
			break
		}
		r3 = _sqlite3GetTempReg(tls, pParse)
		p1 = _sqlite3VectorFieldSubexpr(tls, pLeft, i)
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
			pRhs = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList + 8 + uintptr(i)*20))).FpExpr
			pColl1 = _sqlite3BinaryCompareCollSeq(tls, pParse, p1, pRhs)
		} else {
			/* If the RHS of the IN(...) expression are scalar expressions, do
			 ** not consider their collation sequences. The documentation says
			 ** "The collating sequence used for expressions of the form "x IN (y, z,
			 ** ...)" is the collating sequence of x.".  */
			pColl1 = _sqlite3ExprCollSeq(tls, pParse, p1)
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), **(**int32)(__ccgo_up(bp + 8)), **(**int32)(__ccgo_up(aiMap + uintptr(i)*4)), r3)
		_sqlite3VdbeAddOp4(tls, v, int32(OP_Ne), rLhs+**(**int32)(__ccgo_up(aiMap + uintptr(i)*4)), destNotNull, r3, pColl1, -int32(2))
		_sqlite3ReleaseTempReg(tls, pParse, r3)
		goto _9
	_9:
		;
		i = i + 1
	}
	_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, destIfNull)
	if nVector > int32(1) {
		_sqlite3VdbeResolveLabel(tls, v, destNotNull)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), **(**int32)(__ccgo_up(bp + 8)), addrTop+int32(1))
		/* Step 7:  If we reach this point, we know that the result must
		 ** be false. */
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, destIfFalse)
	}
	/* Jumps here in order to return true. */
	_sqlite3VdbeJumpHere(tls, v, addrTruthOp)
	goto sqlite3ExprCodeIN_finished
sqlite3ExprCodeIN_finished:
	;
	goto sqlite3ExprCodeIN_oom_error
sqlite3ExprCodeIN_oom_error:
	;
	_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, aiMap)
	_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zAff)
}

// C documentation
//
//	/*
//	** Generate code that will evaluate expression pExpr just one time
//	** per prepared statement execution.
//	**
//	** If the expression uses functions (that might throw an exception) then
//	** guard them with an OP_Once opcode to ensure that the code is only executed
//	** once. If no functions are involved, then factor the code out and put it at
//	** the end of the prepared statement in the initialization section.
//	**
//	** If regDest>0 then the result is always stored in that register and the
//	** result is not reusable.  If regDest<0 then this routine is free to
//	** store the value wherever it wants.  The register where the expression
//	** is stored is returned.  When regDest<0, two identical expressions might
//	** code to the same register, if they do not contain function calls and hence
//	** are factored out into the initialization section at the end of the
//	** prepared statement.
//	*/
func _sqlite3ExprCodeRunJustOnce(tls *libc.TLS, pParse uintptr, pExpr uintptr, regDest int32) (r int32) {
	var addr, i, v2 int32
	var p, pItem, pItem1, v, v3 uintptr
	_, _, _, _, _, _, _, _ = addr, i, p, pItem, pItem1, v, v2, v3
	p = (*TParse)(unsafe.Pointer(pParse)).FpConstExpr
	if regDest < 0 && p != 0 {
		pItem = p + 8
		i = (*TExprList)(unsafe.Pointer(p)).FnExpr
		for {
			if !(i > 0) {
				break
			}
			if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 8 + 4))&0x8>>3)) != 0 && _sqlite3ExprCompare(tls, uintptr(0), (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr, pExpr, -int32(1)) == 0 {
				return *(*int32)(unsafe.Pointer(pItem + 16))
			}
			goto _1
		_1:
			;
			pItem += 20
			i = i - 1
		}
	}
	pExpr = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, 0)
	if pExpr != uintptr(0) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_HasFunc)) != uint32(0) {
		v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
		addr = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(0), 7, 0x80)
		if !((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0) {
			if regDest < 0 {
				v3 = pParse + 48
				*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
				v2 = *(*int32)(unsafe.Pointer(v3))
				regDest = v2
			}
			_sqlite3ExprCode(tls, pParse, pExpr, regDest)
		}
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 7, 0x80)
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr)
		_sqlite3VdbeJumpHere(tls, v, addr)
	} else {
		p = _sqlite3ExprListAppend(tls, pParse, p, pExpr)
		if p != 0 {
			pItem1 = p + 8 + uintptr((*TExprList)(unsafe.Pointer(p)).FnExpr-int32(1))*20
			libc.SetBitFieldPtr16Uint32(pItem1+8+4, libc.BoolUint32(regDest < libc.Int32FromInt32(0)), 3, 0x8)
			if regDest < 0 {
				v3 = pParse + 48
				*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
				v2 = *(*int32)(unsafe.Pointer(v3))
				regDest = v2
			}
			*(*int32)(unsafe.Pointer(pItem1 + 16)) = regDest
		}
		(*TParse)(unsafe.Pointer(pParse)).FpConstExpr = p
	}
	return regDest
}

// C documentation
//
//	/*
//	** Generate code into the current Vdbe to evaluate the given
//	** expression.  Attempt to store the results in register "target".
//	** Return the register where results are stored.
//	**
//	** With this routine, there is no guarantee that results will
//	** be stored in target.  The result might be stored in some other
//	** register if it is convenient to do so.  The calling function
//	** must check the return code and move the results to the desired
//	** register.
//	*/
func _sqlite3ExprCodeTarget(tls *libc.TLS, pParse uintptr, pExpr uintptr, target int32) (r int32) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var aListelem, db, db1, pAggInfo, pAggInfo1, pCol, pCol1, pColl, pDef, pDel, pEList, pFarg, pInfo, pLeft, pLeft1, pLeft2, pTab, pTab1, pTab2, pTest, pX, v, z, zBlob, zId, v3 uintptr
	var addr, addrINR, addrIsNull, addrIsNull1, aff, bNormal, destIfFalse, destIfNull, endLabel, i, i1, iCol, iCol1, iReg, iSrc, iTab, inReg, isTrue, n, n1, nCol, nExpr, nFarg, nextCase, op, p1, p5, v1 int32
	var constMask Tu32
	var enc, exprOp, okConstFactor Tu8
	var v2 bool
	var _ /* opCompare at bp+68 */ TExpr
	var _ /* r1 at bp+8 */ int32
	var _ /* r2 at bp+12 */ int32
	var _ /* regFree1 at bp+0 */ int32
	var _ /* regFree2 at bp+4 */ int32
	var _ /* tempX at bp+16 */ TExpr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aListelem, addr, addrINR, addrIsNull, addrIsNull1, aff, bNormal, constMask, db, db1, destIfFalse, destIfNull, enc, endLabel, exprOp, i, i1, iCol, iCol1, iReg, iSrc, iTab, inReg, isTrue, n, n1, nCol, nExpr, nFarg, nextCase, okConstFactor, op, p1, p5, pAggInfo, pAggInfo1, pCol, pCol1, pColl, pDef, pDel, pEList, pFarg, pInfo, pLeft, pLeft1, pLeft2, pTab, pTab1, pTab2, pTest, pX, v, z, zBlob, zId, v1, v2, v3
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* The opcode being coded */
	inReg = target                               /* Results stored in register inReg */
	**(**int32)(__ccgo_up(bp)) = 0               /* If non-zero free this temporary register */
	**(**int32)(__ccgo_up(bp + 4)) = 0           /* Temporary expression node */
	p5 = 0
	goto expr_code_doover
expr_code_doover:
	;
	if pExpr == uintptr(0) {
		op = int32(TK_NULL)
	} else {
		if v2 = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr != uintptr(0) && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Leaf)) != libc.Uint32FromInt32(0)); v2 {
			v1 = _sqlite3IndexedExprLookup(tls, pParse, pExpr, target)
			**(**int32)(__ccgo_up(bp + 8)) = v1
		}
		if v2 && v1 >= 0 {
			return **(**int32)(__ccgo_up(bp + 8))
		} else {
			op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop)
		}
	}
	switch op {
	case int32(TK_AGG_COLUMN):
		pAggInfo = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo
		if int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) >= (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn {
			/* Happens when the left table of a RIGHT JOIN is null and
			 ** is using an expression index */
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target)
			break
		}
		pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)*20
		if !((*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode != 0) {
			return (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg + int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)
		} else {
			if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FuseSortingIdx != 0 {
				pTab = (*TAggInfo_col)(unsafe.Pointer(pCol)).FpTab
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdxPTab, (*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn, target)
				if pTab == uintptr(0) {
					/* No comment added */
				} else {
					if (*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn < 0 {
					} else {
						if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TAggInfo_col)(unsafe.Pointer(pCol)).FiColumn)*12))).Faffinity) == int32(SQLITE_AFF_REAL) {
							_sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), target)
						}
					}
				}
				return target
			} else {
				if (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab == uintptr(0) {
					/* This case happens when the argument to an aggregate function
					 ** is rewritten by aggregateConvertIndexedExprRefToColumn() */
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TExpr)(unsafe.Pointer(pExpr)).FiTable, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn), target)
					return target
				}
			}
		}
		/* Otherwise, fall thru into the TK_COLUMN case */
		fallthrough
	case int32(TK_COLUMN):
		iTab = (*TExpr)(unsafe.Pointer(pExpr)).FiTable
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != uint32(0) {
			iReg = _sqlite3ExprCodeTarget(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, target)
			aff = int32(_sqlite3TableColumnAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)))
			if aff > int32(SQLITE_AFF_BLOB) {
				_sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), iReg, int32(1), 0, uintptr(unsafe.Pointer(&_zAff))+uintptr((aff-int32('B'))*int32(2)), -int32(1))
			}
			return iReg
		}
		if iTab < 0 {
			if (*TParse)(unsafe.Pointer(pParse)).FiSelfTab < 0 {
				iCol = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
				pTab1 = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab
				if iCol < 0 {
					return -int32(1) - (*TParse)(unsafe.Pointer(pParse)).FiSelfTab
				}
				pCol1 = (*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(iCol)*12
				iSrc = int32(_sqlite3TableColumnToStorage(tls, pTab1, int16(iCol))) - (*TParse)(unsafe.Pointer(pParse)).FiSelfTab
				if int32((*TColumn)(unsafe.Pointer(pCol1)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
					if int32((*TColumn)(unsafe.Pointer(pCol1)).FcolFlags)&int32(COLFLAG_BUSY) != 0 {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9669, libc.VaList(bp+128, (*TColumn)(unsafe.Pointer(pCol1)).FzCnName))
						return 0
					}
					v3 = pCol1 + 10
					*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | libc.Int32FromInt32(COLFLAG_BUSY))
					if int32((*TColumn)(unsafe.Pointer(pCol1)).FcolFlags)&int32(COLFLAG_NOTAVAIL) != 0 {
						_sqlite3ExprCodeGeneratedColumn(tls, pParse, pTab1, pCol1, iSrc)
					}
					v3 = pCol1 + 10
					*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) & ^(libc.Int32FromInt32(COLFLAG_BUSY) | libc.Int32FromInt32(COLFLAG_NOTAVAIL)))
					return iSrc
				} else {
					if int32((*TColumn)(unsafe.Pointer(pCol1)).Faffinity) == int32(SQLITE_AFF_REAL) {
						_sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), iSrc, target)
						_sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), target)
						return target
					} else {
						return iSrc
					}
				}
			} else {
				/* Coding an expression that is part of an index where column names
				 ** in the index refer to the table to which the index belongs */
				iTab = (*TParse)(unsafe.Pointer(pParse)).FiSelfTab - int32(1)
			}
		} else {
			if v2 = (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr != 0; v2 {
				v1 = _exprPartidxExprLookup(tls, pParse, pExpr, target)
				**(**int32)(__ccgo_up(bp + 8)) = v1
			}
			if v2 && 0 != v1 {
				return **(**int32)(__ccgo_up(bp + 8))
			}
		}
		iReg = _sqlite3ExprCodeGetColumn(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab, int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn), iTab, target, (*TExpr)(unsafe.Pointer(pExpr)).Fop2)
		return iReg
	case int32(TK_INTEGER):
		_codeInteger(tls, pParse, pExpr, 0, target)
		return target
	case int32(TK_TRUEFALSE):
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprTruthValue(tls, pExpr), target)
		return target
	case int32(TK_FLOAT):
		_codeReal(tls, v, *(*uintptr)(unsafe.Pointer(pExpr + 8)), 0, target)
		return target
	case int32(TK_STRING):
		_sqlite3VdbeLoadString(tls, v, target, *(*uintptr)(unsafe.Pointer(pExpr + 8)))
		return target
	case int32(TK_NULLS):
		/* Set a range of registers to NULL.  pExpr->y.nReg registers starting
		 ** with target */
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, target, target+*(*int32)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy))-int32(1))
		return target
	default:
		/* Make NULL the default case so that if a bug causes an illegal
		 ** Expr node to be passed into this function, it will be handled
		 ** sanely and not crash.  But keep the assert() to bring the problem
		 ** to the attention of the developers. */
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target)
		return target
	case int32(TK_BLOB):
		z = *(*uintptr)(unsafe.Pointer(pExpr + 8)) + 2
		n = _sqlite3Strlen30(tls, z) - int32(1)
		zBlob = _sqlite3HexToBlob(tls, _sqlite3VdbeDb(tls, v), z, n)
		_sqlite3VdbeAddOp4(tls, v, int32(OP_Blob), n/int32(2), target, 0, zBlob, -int32(7))
		return target
	case int32(TK_VARIABLE):
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Variable), int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn), target)
		return target
	case int32(TK_REGISTER):
		return (*TExpr)(unsafe.Pointer(pExpr)).FiTable
	case int32(TK_CAST):
		/* Expressions of the form:   CAST(pLeft AS token) */
		_sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, target)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Cast), target, int32(_sqlite3AffinityType(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), uintptr(0))))
		return inReg
	case int32(TK_IS):
		fallthrough
	case int32(TK_ISNOT):
		if op == int32(TK_IS) {
			v1 = int32(TK_EQ)
		} else {
			v1 = int32(TK_NE)
		}
		op = v1
		p5 = int32(SQLITE_NULLEQ)
		fallthrough
	case int32(TK_LT):
		fallthrough
	case int32(TK_LE):
		fallthrough
	case int32(TK_GT):
		fallthrough
	case int32(TK_GE):
		fallthrough
	case int32(TK_NE):
		fallthrough
	case int32(TK_EQ):
		pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
		addrIsNull = 0
		if _sqlite3ExprIsVector(tls, pLeft) != 0 {
			_codeVectorCompare(tls, pParse, pExpr, target, uint8(op), uint8(p5))
		} else {
			if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subquery)) != uint32(0) && p5 != int32(SQLITE_NULLEQ) {
				addrIsNull = _exprComputeOperands(tls, pParse, pExpr, bp+8, bp+12, bp, bp+4)
			} else {
				**(**int32)(__ccgo_up(bp + 8)) = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp)
				**(**int32)(__ccgo_up(bp + 12)) = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, bp+4)
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), inReg)
			_codeCompare(tls, pParse, pLeft, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, op, **(**int32)(__ccgo_up(bp + 8)), **(**int32)(__ccgo_up(bp + 12)), _sqlite3VdbeCurrentAddr(tls, v)+int32(2), p5, libc.BoolInt32((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Commuted)) != uint32(0)))
			if p5 == int32(SQLITE_NULLEQ) {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, inReg)
			} else {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_ZeroOrNull), **(**int32)(__ccgo_up(bp + 8)), inReg, **(**int32)(__ccgo_up(bp + 12)))
				if addrIsNull != 0 {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, _sqlite3VdbeCurrentAddr(tls, v)+int32(2))
					_sqlite3VdbeJumpHere(tls, v, addrIsNull)
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, inReg)
				}
			}
		}
	case int32(TK_AND):
		fallthrough
	case int32(TK_OR):
		inReg = _exprCodeTargetAndOr(tls, pParse, pExpr, target, bp)
	case int32(TK_PLUS):
		fallthrough
	case int32(TK_STAR):
		fallthrough
	case int32(TK_MINUS):
		fallthrough
	case int32(TK_REM):
		fallthrough
	case int32(TK_BITAND):
		fallthrough
	case int32(TK_BITOR):
		fallthrough
	case int32(TK_SLASH):
		fallthrough
	case int32(TK_LSHIFT):
		fallthrough
	case int32(TK_RSHIFT):
		fallthrough
	case int32(TK_CONCAT):
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subquery)) != uint32(0) {
			addrIsNull1 = _exprComputeOperands(tls, pParse, pExpr, bp+8, bp+12, bp, bp+4)
		} else {
			**(**int32)(__ccgo_up(bp + 8)) = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp)
			**(**int32)(__ccgo_up(bp + 12)) = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, bp+4)
			addrIsNull1 = 0
		}
		_sqlite3VdbeAddOp3(tls, v, op, **(**int32)(__ccgo_up(bp + 12)), **(**int32)(__ccgo_up(bp + 8)), target)
		if addrIsNull1 != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, _sqlite3VdbeCurrentAddr(tls, v)+int32(2))
			_sqlite3VdbeJumpHere(tls, v, addrIsNull1)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target)
		}
	case int32(TK_UMINUS):
		pLeft1 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
		if int32((*TExpr)(unsafe.Pointer(pLeft1)).Fop) == int32(TK_INTEGER) {
			_codeInteger(tls, pParse, pLeft1, int32(1), target)
			return target
		} else {
			if int32((*TExpr)(unsafe.Pointer(pLeft1)).Fop) == int32(TK_FLOAT) {
				_codeReal(tls, v, *(*uintptr)(unsafe.Pointer(pLeft1 + 8)), int32(1), target)
				return target
			} else {
				(**(**TExpr)(__ccgo_up(bp + 16))).Fop = uint8(TK_INTEGER)
				(**(**TExpr)(__ccgo_up(bp + 16))).Fflags = uint32(libc.Int32FromInt32(EP_IntValue) | libc.Int32FromInt32(EP_TokenOnly))
				*(*int32)(unsafe.Pointer(bp + 16 + 8)) = 0
				**(**int32)(__ccgo_up(bp + 8)) = _sqlite3ExprCodeTemp(tls, pParse, bp+16, bp)
				**(**int32)(__ccgo_up(bp + 12)) = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp+4)
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Subtract), **(**int32)(__ccgo_up(bp + 12)), **(**int32)(__ccgo_up(bp + 8)), target)
			}
		}
	case int32(TK_BITNOT):
		fallthrough
	case int32(TK_NOT):
		**(**int32)(__ccgo_up(bp + 8)) = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp)
		_sqlite3VdbeAddOp2(tls, v, op, **(**int32)(__ccgo_up(bp + 8)), inReg)
	case int32(TK_TRUTH): /* IS TRUE or IS FALSE */
		**(**int32)(__ccgo_up(bp + 8)) = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp)
		isTrue = _sqlite3ExprTruthValue(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight)
		bNormal = libc.BoolInt32(int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) == int32(TK_IS))
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsTrue), **(**int32)(__ccgo_up(bp + 8)), inReg, libc.BoolInt32(!(isTrue != 0)), isTrue^bNormal)
	case int32(TK_ISNULL):
		fallthrough
	case int32(TK_NOTNULL):
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), target)
		**(**int32)(__ccgo_up(bp + 8)) = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp)
		addr = _sqlite3VdbeAddOp1(tls, v, op, **(**int32)(__ccgo_up(bp + 8)))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, target)
		_sqlite3VdbeJumpHere(tls, v, addr)
	case int32(TK_AGG_FUNCTION):
		pInfo = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo
		if pInfo == uintptr(0) || int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) < 0 || int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg) >= (*TAggInfo)(unsafe.Pointer(pInfo)).FnFunc {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9730, libc.VaList(bp+128, pExpr))
		} else {
			return (*TAggInfo)(unsafe.Pointer(pInfo)).FiFirstReg + (*TAggInfo)(unsafe.Pointer(pInfo)).FnColumn + int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)
		}
	case int32(TK_FUNCTION): /* The function name */
		constMask = uint32(0)                      /* Loop counter */
		db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* The database connection */
		enc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc /* The text encoding used by this database */
		pColl = uintptr(0)                         /* A collating sequence */
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
			return (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FregResult
		}
		if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x80>>7)) != 0 && _sqlite3ExprIsConstantNotJoin(tls, pParse, pExpr) != 0 {
			/* SQL functions can be expensive. So try to avoid running them
			 ** multiple times if we know they always give the same result */
			return _sqlite3ExprCodeRunJustOnce(tls, pParse, pExpr, -int32(1))
		}
		pFarg = *(*uintptr)(unsafe.Pointer(pExpr + 20))
		if pFarg != 0 {
			v1 = (*TExprList)(unsafe.Pointer(pFarg)).FnExpr
		} else {
			v1 = 0
		}
		nFarg = v1
		zId = *(*uintptr)(unsafe.Pointer(pExpr + 8))
		pDef = _sqlite3FindFunction(tls, db, zId, nFarg, enc, uint8(0))
		if pDef == uintptr(0) || (*TFuncDef)(unsafe.Pointer(pDef)).FxFinalize != uintptr(0) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9757, libc.VaList(bp+128, pExpr))
			break
		}
		if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_INLINE) != uint32(0) && pFarg != uintptr(0) {
			return _exprCodeInlineFunction(tls, pParse, pFarg, int32((*TFuncDef)(unsafe.Pointer(pDef)).FpUserData), target)
		} else {
			if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_DIRECT)|libc.Int32FromInt32(SQLITE_FUNC_UNSAFE)) != 0 {
				_sqlite3ExprFunctionUsable(tls, pParse, pExpr, pDef)
			}
		}
		i = 0
		for {
			if !(i < nFarg) {
				break
			}
			if i < int32(32) && _sqlite3ExprIsConstant(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + uintptr(i)*20))).FpExpr) != 0 {
				constMask = constMask | libc.Uint32FromInt32(1)<<i
			}
			if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != uint32(0) && !(pColl != 0) {
				pColl = _sqlite3ExprCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + uintptr(i)*20))).FpExpr)
			}
			goto _9
		_9:
			;
			i = i + 1
		}
		if pFarg != 0 {
			if constMask != 0 {
				**(**int32)(__ccgo_up(bp + 8)) = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
				**(**int32)(__ccgo_up(pParse + 48)) += nFarg
			} else {
				**(**int32)(__ccgo_up(bp + 8)) = _sqlite3GetTempRange(tls, pParse, nFarg)
			}
			/* For length() and typeof() and octet_length() functions,
			 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
			 ** or OPFLAG_TYPEOFARG or OPFLAG_BYTELENARG respectively, to avoid
			 ** unnecessary data loading.
			 */
			if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_LENGTH)|libc.Int32FromInt32(SQLITE_FUNC_TYPEOF)) != uint32(0) {
				exprOp = (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr)).Fop
				if int32(exprOp) == int32(TK_COLUMN) || int32(exprOp) == int32(TK_AGG_COLUMN) {
					(*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr)).Fop2 = uint8((*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags & uint32(OPFLAG_BYTELENARG))
				}
			}
			_sqlite3ExprCodeExprList(tls, pParse, pFarg, **(**int32)(__ccgo_up(bp + 8)), 0, uint8(SQLITE_ECEL_FACTOR))
		} else {
			**(**int32)(__ccgo_up(bp + 8)) = 0
		}
		/* Possibly overload the function if the first argument is
		 ** a virtual table column.
		 **
		 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
		 ** second argument, not the first, as the argument to test to
		 ** see if it is a column in a virtual table.  This is done because
		 ** the left operand of infix functions (the operand we want to
		 ** control overloading) ends up as the second argument to the
		 ** function.  The expression "A glob B" is equivalent to
		 ** "glob(B,A).  We want to use the A in "A glob B" to test
		 ** for function overloading.  But we use the B term in "glob(B,A)".
		 */
		if nFarg >= int32(2) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InfixFunc)) != uint32(0) {
			pDef = _sqlite3VtabOverloadFunction(tls, db, pDef, nFarg, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*20))).FpExpr)
		} else {
			if nFarg > 0 {
				pDef = _sqlite3VtabOverloadFunction(tls, db, pDef, nFarg, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr)
			}
		}
		if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 {
			if !(pColl != 0) {
				pColl = (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl
			}
			_sqlite3VdbeAddOp4(tls, v, int32(OP_CollSeq), 0, 0, 0, pColl, -int32(2))
		}
		_sqlite3VdbeAddFunctionCall(tls, pParse, int32(constMask), **(**int32)(__ccgo_up(bp + 8)), target, nFarg, pDef, int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2))
		if nFarg != 0 {
			if constMask == uint32(0) {
				_sqlite3ReleaseTempRange(tls, pParse, **(**int32)(__ccgo_up(bp + 8)), nFarg)
			} else {
			}
		}
		return target
	case int32(TK_EXISTS):
		fallthrough
	case int32(TK_SELECT):
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
			return 0
		} else {
			if v2 = op == int32(TK_SELECT) && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0); v2 {
				v1 = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList)).FnExpr
				nCol = v1
			}
			if v2 && v1 != int32(1) {
				_sqlite3SubselectError(tls, pParse, nCol, int32(1))
			} else {
				return _sqlite3CodeSubselect(tls, pParse, pExpr)
			}
		}
	case int32(TK_SELECT_COLUMN):
		pLeft2 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
		if (*TExpr)(unsafe.Pointer(pLeft2)).FiTable == 0 || int32((*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn) > int32((*TExpr)(unsafe.Pointer(pLeft2)).Fop2) {
			(*TExpr)(unsafe.Pointer(pLeft2)).FiTable = _sqlite3CodeSubselect(tls, pParse, pLeft2)
			(*TExpr)(unsafe.Pointer(pLeft2)).Fop2 = (*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn
		}
		n1 = _sqlite3ExprVectorSize(tls, pLeft2)
		if (*TExpr)(unsafe.Pointer(pExpr)).FiTable != n1 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9342, libc.VaList(bp+128, (*TExpr)(unsafe.Pointer(pExpr)).FiTable, n1))
		}
		return (*TExpr)(unsafe.Pointer(pLeft2)).FiTable + int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
	case int32(TK_IN):
		destIfFalse = _sqlite3VdbeMakeLabel(tls, pParse)
		destIfNull = _sqlite3VdbeMakeLabel(tls, pParse)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target)
		_sqlite3ExprCodeIN(tls, pParse, pExpr, destIfFalse, destIfNull)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), target)
		_sqlite3VdbeResolveLabel(tls, v, destIfFalse)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), target, 0)
		_sqlite3VdbeResolveLabel(tls, v, destIfNull)
		return target
		/*
		 **    x BETWEEN y AND z
		 **
		 ** This is equivalent to
		 **
		 **    x>=y AND x<=z
		 **
		 ** X is stored in pExpr->pLeft.
		 ** Y is stored in pExpr->pList->a[0].pExpr.
		 ** Z is stored in pExpr->pList->a[1].pExpr.
		 */
		fallthrough
	case int32(TK_BETWEEN):
		_exprCodeBetween(tls, pParse, pExpr, target, uintptr(0), 0)
		return target
	case int32(TK_COLLATE):
		if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Collate)) != libc.Uint32FromInt32(0)) {
			/* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
			 ** "SOFT-COLLATE" that is added to constraints that are pushed down
			 ** from outer queries into sub-queries by the WHERE-clause push-down
			 ** optimization. Clear subtypes as subtypes may not cross a subquery
			 ** boundary.
			 */
			_sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, target)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_ClrSubtype), target)
			return target
		} else {
			pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
			goto expr_code_doover /* 2018-04-28: Prevent deep recursion. */
		}
		fallthrough
	case int32(TK_SPAN):
		fallthrough
	case int32(TK_UPLUS):
		pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
		goto expr_code_doover /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
	case int32(TK_TRIGGER):
		pTab2 = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab
		iCol1 = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
		p1 = (*TExpr)(unsafe.Pointer(pExpr)).FiTable*(int32((*TTable)(unsafe.Pointer(pTab2)).FnCol)+int32(1)) + int32(1) + int32(_sqlite3TableColumnToStorage(tls, pTab2, int16(iCol1)))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Param), p1, target)
		/* If the column has REAL affinity, it may currently be stored as an
		 ** integer. Use OP_RealAffinity to make sure it is really real.
		 **
		 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
		 ** floating point when extracting it from the record.  */
		if iCol1 >= 0 && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab2)).FaCol + uintptr(iCol1)*12))).Faffinity) == int32(SQLITE_AFF_REAL) {
			_sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), target)
		}
	case int32(TK_VECTOR):
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8125, 0)
		break
		/* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
		 ** that derive from the right-hand table of a LEFT JOIN.  The
		 ** Expr.iTable value is the table number for the right-hand table.
		 ** The expression is only evaluated if that table is not currently
		 ** on a LEFT JOIN NULL row.
		 */
		fallthrough
	case int32(TK_IF_NULL_ROW):
		okConstFactor = uint8(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28)) & 0x80 >> 7)))
		pAggInfo1 = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo
		if pAggInfo1 != 0 {
			if !((*TAggInfo)(unsafe.Pointer(pAggInfo1)).FdirectMode != 0) {
				inReg = (*TAggInfo)(unsafe.Pointer(pAggInfo1)).FiFirstReg + int32((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)
				break
			}
			if (*TAggInfo)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo)).FuseSortingIdx != 0 {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TAggInfo)(unsafe.Pointer(pAggInfo1)).FsortingIdxPTab, (**(**TAggInfo_col)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo1)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)*20))).FiSorterColumn, target)
				inReg = target
				break
			}
		}
		addrINR = _sqlite3VdbeAddOp3(tls, v, int32(OP_IfNullRow), (*TExpr)(unsafe.Pointer(pExpr)).FiTable, 0, target)
		/* The OP_IfNullRow opcode above can overwrite the result register with
		 ** NULL.  So we have to ensure that the result register is not a value
		 ** that is suppose to be a constant.  Two defenses are needed:
		 **   (1)  Temporarily disable factoring of constant expressions
		 **   (2)  Make sure the computed value really is stored in register
		 **        "target" and not someplace else.
		 */
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(0), 7, 0x80) /* note (1) above */
		_sqlite3ExprCode(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, target)
		libc.SetBitFieldPtr16Uint32(pParse+28, uint32(okConstFactor), 7, 0x80)
		_sqlite3VdbeJumpHere(tls, v, addrINR)
		break
		/*
		 ** Form A:
		 **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
		 **
		 ** Form B:
		 **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
		 **
		 ** Form A is can be transformed into the equivalent form B as follows:
		 **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
		 **        WHEN x=eN THEN rN ELSE y END
		 **
		 ** X (if it exists) is in pExpr->pLeft.
		 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
		 ** odd.  The Y is also optional.  If the number of elements in x.pList
		 ** is even, then Y is omitted and the "otherwise" result is NULL.
		 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
		 **
		 ** The result of the expression is the Ri for the first matching Ei,
		 ** or if there is no matching Ei, the ELSE term Y, or if there is
		 ** no ELSE term, NULL.
		 */
		fallthrough
	case int32(TK_CASE): /* The X expression */
		pTest = uintptr(0) /* X==Ei (form A) or just Ei (form B) */
		pDel = uintptr(0)
		db1 = (*TParse)(unsafe.Pointer(pParse)).Fdb
		pEList = *(*uintptr)(unsafe.Pointer(pExpr + 20))
		aListelem = pEList + 8
		nExpr = (*TExprList)(unsafe.Pointer(pEList)).FnExpr
		endLabel = _sqlite3VdbeMakeLabel(tls, pParse)
		v3 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
		pX = v3
		if v3 != uintptr(0) {
			pDel = _sqlite3ExprDup(tls, db1, pX, 0)
			if (*Tsqlite3)(unsafe.Pointer(db1)).FmallocFailed != 0 {
				_sqlite3ExprDelete(tls, db1, pDel)
				break
			}
			_sqlite3ExprToRegister(tls, pDel, _exprCodeVector(tls, pParse, pDel, bp))
			libc.Xmemset(tls, bp+68, 0, uint32(52))
			(**(**TExpr)(__ccgo_up(bp + 68))).Fop = uint8(TK_EQ)
			(**(**TExpr)(__ccgo_up(bp + 68))).FpLeft = pDel
			pTest = bp + 68
			/* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
			 ** The value in regFree1 might get SCopy-ed into the file result.
			 ** So make sure that the regFree1 register is not reused for other
			 ** purposes and possibly overwritten.  */
			**(**int32)(__ccgo_up(bp)) = 0
		}
		i1 = 0
		for {
			if !(i1 < nExpr-int32(1)) {
				break
			}
			if pX != 0 {
				(**(**TExpr)(__ccgo_up(bp + 68))).FpRight = (**(**TExprList_item)(__ccgo_up(aListelem + uintptr(i1)*20))).FpExpr
			} else {
				pTest = (**(**TExprList_item)(__ccgo_up(aListelem + uintptr(i1)*20))).FpExpr
			}
			nextCase = _sqlite3VdbeMakeLabel(tls, pParse)
			_sqlite3ExprIfFalse(tls, pParse, pTest, nextCase, int32(SQLITE_JUMPIFNULL))
			_sqlite3ExprCode(tls, pParse, (**(**TExprList_item)(__ccgo_up(aListelem + uintptr(i1+int32(1))*20))).FpExpr, target)
			_sqlite3VdbeGoto(tls, v, endLabel)
			_sqlite3VdbeResolveLabel(tls, v, nextCase)
			goto _13
		_13:
			;
			i1 = i1 + int32(2)
		}
		if nExpr&int32(1) != 0 {
			_sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(nExpr-int32(1))*20))).FpExpr, target)
		} else {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target)
		}
		_sqlite3ExprDelete(tls, db1, pDel)
		_setDoNotMergeFlagOnCopy(tls, v)
		_sqlite3VdbeResolveLabel(tls, v, endLabel)
	case int32(TK_RAISE):
		if !((*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0) && !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9781, 0)
			return 0
		}
		if int32((*TExpr)(unsafe.Pointer(pExpr)).FaffExpr) == int32(OE_Abort) {
			_sqlite3MayAbort(tls, pParse)
		}
		if int32((*TExpr)(unsafe.Pointer(pExpr)).FaffExpr) == int32(OE_Ignore) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Halt), SQLITE_OK, int32(OE_Ignore))
		} else {
			**(**int32)(__ccgo_up(bp + 8)) = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, bp)
			if (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0 {
				v1 = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(7)<<libc.Int32FromInt32(8)
			} else {
				v1 = int32(SQLITE_ERROR)
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Halt), v1, int32((*TExpr)(unsafe.Pointer(pExpr)).FaffExpr), **(**int32)(__ccgo_up(bp + 8)))
		}
		break
	}
	_sqlite3ReleaseTempReg(tls, pParse, **(**int32)(__ccgo_up(bp)))
	_sqlite3ReleaseTempReg(tls, pParse, **(**int32)(__ccgo_up(bp + 4)))
	return inReg
}

// C documentation
//
//	/*
//	** Generate code to evaluate an expression and store the results
//	** into a register.  Return the register number where the results
//	** are stored.
//	**
//	** If the register is a temporary register that can be deallocated,
//	** then write its number into *pReg.  If the result register is not
//	** a temporary, then set *pReg to zero.
//	**
//	** If pExpr is a constant, then this routine might generate this
//	** code to fill the register in the initialization section of the
//	** VDBE program, in order to factor it out of the evaluation loop.
//	*/
func _sqlite3ExprCodeTemp(tls *libc.TLS, pParse uintptr, pExpr uintptr, pReg uintptr) (r int32) {
	var r1, r2 int32
	_, _ = r1, r2
	pExpr = _sqlite3ExprSkipCollateAndLikely(tls, pExpr)
	if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x80>>7)) != 0 && pExpr != uintptr(0) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_REGISTER) && _sqlite3ExprIsConstantNotJoin(tls, pParse, pExpr) != 0 {
		**(**int32)(__ccgo_up(pReg)) = 0
		r2 = _sqlite3ExprCodeRunJustOnce(tls, pParse, pExpr, -int32(1))
	} else {
		r1 = _sqlite3GetTempReg(tls, pParse)
		r2 = _sqlite3ExprCodeTarget(tls, pParse, pExpr, r1)
		if r2 == r1 {
			**(**int32)(__ccgo_up(pReg)) = r1
		} else {
			_sqlite3ReleaseTempReg(tls, pParse, r1)
			**(**int32)(__ccgo_up(pReg)) = 0
		}
	}
	return r2
}

// C documentation
//
//	/*
//	** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
//	** return the appropriate colUsed mask.
//	*/
func _sqlite3ExprColUsed(tls *libc.TLS, pExpr uintptr) (r TBitmask) {
	var n int32
	var pExTab uintptr
	var v1 uint64
	_, _, _ = n, pExTab, v1
	n = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
	pExTab = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab
	if (*TTable)(unsafe.Pointer(pExTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pExTab)).FaCol + uintptr(n)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
		if int32((*TTable)(unsafe.Pointer(pExTab)).FnCol) >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
			v1 = uint64(-libc.Int32FromInt32(1))
		} else {
			v1 = libc.Uint64FromInt32(1)<<(*TTable)(unsafe.Pointer(pExTab)).FnCol - uint64(1)
		}
		return v1
	} else {
		if n >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
			n = int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) - libc.Int32FromInt32(1)
		}
		return libc.Uint64FromInt32(1) << n
	}
	return r
}

// C documentation
//
//	/*
//	** Return the collation sequence for the expression pExpr. If
//	** there is no defined collating sequence, return NULL.
//	**
//	** See also: sqlite3ExprNNCollSeq()
//	**
//	** The sqlite3ExprNNCollSeq() works the same exact that it returns the
//	** default collation if pExpr has no defined collation.
//	**
//	** The collating sequence might be determined by a COLLATE operator
//	** or by the presence of a column with a defined collating sequence.
//	** COLLATE operators take first precedence.  Left operands take
//	** precedence over right operands.
//	*/
func _sqlite3ExprCollSeq(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r uintptr) {
	var db, p, pColl, pNext, zColl uintptr
	var i, j, op, v1 int32
	_, _, _, _, _, _, _, _, _ = db, i, j, op, p, pColl, pNext, zColl, v1
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pColl = uintptr(0)
	p = pExpr
	for p != 0 {
		op = int32((*TExpr)(unsafe.Pointer(p)).Fop)
		if op == int32(TK_REGISTER) {
			op = int32((*TExpr)(unsafe.Pointer(p)).Fop2)
		}
		if op == int32(TK_AGG_COLUMN) && (*TExpr)(unsafe.Pointer(p)).Fy.FpTab != uintptr(0) || op == int32(TK_COLUMN) || op == int32(TK_TRIGGER) {
			v1 = int32((*TExpr)(unsafe.Pointer(p)).FiColumn)
			j = v1
			if v1 >= 0 {
				zColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).Fy.FpTab)).FaCol+uintptr(j)*12)
				pColl = _sqlite3FindCollSeq(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, zColl, 0)
			}
			break
		}
		if op == int32(TK_CAST) || op == int32(TK_UPLUS) {
			p = (*TExpr)(unsafe.Pointer(p)).FpLeft
			continue
		}
		if op == int32(TK_VECTOR) || op == int32(TK_FUNCTION) && int32((*TExpr)(unsafe.Pointer(p)).FaffExpr) == int32(SQLITE_AFF_DEFER) {
			p = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8))).FpExpr
			continue
		}
		if op == int32(TK_COLLATE) {
			pColl = _sqlite3GetCollSeq(tls, pParse, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uintptr(0), *(*uintptr)(unsafe.Pointer(p + 8)))
			break
		}
		if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_Collate) != 0 {
			if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 && (*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(p)).FpLeft)).Fflags&uint32(EP_Collate) != uint32(0) {
				p = (*TExpr)(unsafe.Pointer(p)).FpLeft
			} else {
				pNext = (*TExpr)(unsafe.Pointer(p)).FpRight
				/* The Expr.x union is never used at the same time as Expr.pRight */
				if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) == uint32(0) && *(*uintptr)(unsafe.Pointer(p + 20)) != uintptr(0) && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
					i = 0
					for {
						if !(i < (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)))).FnExpr) {
							break
						}
						if (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8 + uintptr(i)*20))).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Collate)) != uint32(0) {
							pNext = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8 + uintptr(i)*20))).FpExpr
							break
						}
						goto _2
					_2:
						;
						i = i + 1
					}
				}
				p = pNext
			}
		} else {
			break
		}
	}
	if _sqlite3CheckCollSeq(tls, pParse, pColl) != 0 {
		pColl = uintptr(0)
	}
	return pColl
}

// C documentation
//
//	/*
//	** Do a deep comparison of two expression trees.  Return 0 if the two
//	** expressions are completely identical.  Return 1 if they differ only
//	** by a COLLATE operator at the top level.  Return 2 if there are differences
//	** other than the top-level COLLATE operator.
//	**
//	** If any subelement of pB has Expr.iTable==(-1) then it is allowed
//	** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
//	**
//	** The pA side might be using TK_REGISTER.  If that is the case and pB is
//	** not using TK_REGISTER but is otherwise equivalent, then still return 0.
//	**
//	** Sometimes this routine will return 2 even if the two expressions
//	** really are equivalent.  If we cannot prove that the expressions are
//	** identical, we return 2 just to be safe.  So if this routine
//	** returns 2, then you do not really know for certain if the two
//	** expressions are the same.  But if you get a 0 or 1 return, then you
//	** can be sure the expressions are the same.  In the places where
//	** this routine is used, it does not hurt to get an extra 2 - that
//	** just might result in some slightly slower code.  But returning
//	** an incorrect 0 or 1 could lead to a malfunction.
//	**
//	** If pParse is not NULL and SQLITE_EnableQPSG is off then TK_VARIABLE
//	** terms in pA with bindings in pParse->pReprepare can be matched against
//	** literals in pB.  The pParse->pVdbe->expmask bitmask is updated for
//	** each variable referenced.
//	*/
func _sqlite3ExprCompare(tls *libc.TLS, pParse uintptr, pA uintptr, pB uintptr, iTab int32) (r int32) {
	var combinedFlags Tu32
	var v1 int32
	_, _ = combinedFlags, v1
	if pA == uintptr(0) || pB == uintptr(0) {
		if pB == pA {
			v1 = 0
		} else {
			v1 = int32(2)
		}
		return v1
	}
	if pParse != 0 && int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_VARIABLE) {
		return _exprCompareVariable(tls, pParse, pA, pB)
	}
	combinedFlags = (*TExpr)(unsafe.Pointer(pA)).Fflags | (*TExpr)(unsafe.Pointer(pB)).Fflags
	if combinedFlags&uint32(EP_IntValue) != 0 {
		if (*TExpr)(unsafe.Pointer(pA)).Fflags&(*TExpr)(unsafe.Pointer(pB)).Fflags&uint32(EP_IntValue) != uint32(0) && *(*int32)(unsafe.Pointer(pA + 8)) == *(*int32)(unsafe.Pointer(pB + 8)) {
			return 0
		}
		return int32(2)
	}
	if int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32((*TExpr)(unsafe.Pointer(pB)).Fop) || int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_RAISE) {
		if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_COLLATE) && _sqlite3ExprCompare(tls, pParse, (*TExpr)(unsafe.Pointer(pA)).FpLeft, pB, iTab) < int32(2) {
			return int32(1)
		}
		if int32((*TExpr)(unsafe.Pointer(pB)).Fop) == int32(TK_COLLATE) && _sqlite3ExprCompare(tls, pParse, pA, (*TExpr)(unsafe.Pointer(pB)).FpLeft, iTab) < int32(2) {
			return int32(1)
		}
		if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_AGG_COLUMN) && int32((*TExpr)(unsafe.Pointer(pB)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pB)).FiTable < 0 && (*TExpr)(unsafe.Pointer(pA)).FiTable == iTab {
			/* fall through */
		} else {
			return int32(2)
		}
	}
	if *(*uintptr)(unsafe.Pointer(pA + 8)) != 0 {
		if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_FUNCTION) || int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_AGG_FUNCTION) {
			if _sqlite3StrICmp(tls, *(*uintptr)(unsafe.Pointer(pA + 8)), *(*uintptr)(unsafe.Pointer(pB + 8))) != 0 {
				return int32(2)
			}
			if libc.BoolInt32((*TExpr)(unsafe.Pointer(pA)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0)) != libc.BoolInt32((*TExpr)(unsafe.Pointer(pB)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0)) {
				return int32(2)
			}
			if (*TExpr)(unsafe.Pointer(pA)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
				if _sqlite3WindowCompare(tls, pParse, *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pA)).Fy)), *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pB)).Fy)), int32(1)) != 0 {
					return int32(2)
				}
			}
		} else {
			if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_NULL) {
				return 0
			} else {
				if int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_COLLATE) {
					if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(pA + 8)), *(*uintptr)(unsafe.Pointer(pB + 8))) != 0 {
						return int32(2)
					}
				} else {
					if *(*uintptr)(unsafe.Pointer(pB + 8)) != uintptr(0) && int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_AGG_COLUMN) && libc.Xstrcmp(tls, *(*uintptr)(unsafe.Pointer(pA + 8)), *(*uintptr)(unsafe.Pointer(pB + 8))) != 0 {
						return int32(2)
					}
				}
			}
		}
	}
	if (*TExpr)(unsafe.Pointer(pA)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)|libc.Int32FromInt32(EP_Commuted)) != (*TExpr)(unsafe.Pointer(pB)).Fflags&uint32(libc.Int32FromInt32(EP_Distinct)|libc.Int32FromInt32(EP_Commuted)) {
		return int32(2)
	}
	if combinedFlags&uint32(EP_TokenOnly) == uint32(0) {
		if combinedFlags&uint32(EP_xIsSelect) != 0 {
			return int32(2)
		}
		if combinedFlags&uint32(EP_FixedCol) == uint32(0) && _sqlite3ExprCompare(tls, pParse, (*TExpr)(unsafe.Pointer(pA)).FpLeft, (*TExpr)(unsafe.Pointer(pB)).FpLeft, iTab) != 0 {
			return int32(2)
		}
		if _sqlite3ExprCompare(tls, pParse, (*TExpr)(unsafe.Pointer(pA)).FpRight, (*TExpr)(unsafe.Pointer(pB)).FpRight, iTab) != 0 {
			return int32(2)
		}
		if _sqlite3ExprListCompare(tls, *(*uintptr)(unsafe.Pointer(pA + 20)), *(*uintptr)(unsafe.Pointer(pB + 20)), iTab) != 0 {
			return int32(2)
		}
		if int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_STRING) && int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_TRUEFALSE) && combinedFlags&uint32(EP_Reduced) == uint32(0) {
			if int32((*TExpr)(unsafe.Pointer(pA)).FiColumn) != int32((*TExpr)(unsafe.Pointer(pB)).FiColumn) {
				return int32(2)
			}
			if int32((*TExpr)(unsafe.Pointer(pA)).Fop2) != int32((*TExpr)(unsafe.Pointer(pB)).Fop2) && int32((*TExpr)(unsafe.Pointer(pA)).Fop) == int32(TK_TRUTH) {
				return int32(2)
			}
			if int32((*TExpr)(unsafe.Pointer(pA)).Fop) != int32(TK_IN) && (*TExpr)(unsafe.Pointer(pA)).FiTable != (*TExpr)(unsafe.Pointer(pB)).FiTable && (*TExpr)(unsafe.Pointer(pA)).FiTable != iTab {
				return int32(2)
			}
		}
	}
	return 0
}

// C documentation
//
//	/*
//	** Make a guess at all the possible datatypes of the result that could
//	** be returned by an expression.  Return a bitmask indicating the answer:
//	**
//	**     0x01         Numeric
//	**     0x02         Text
//	**     0x04         Blob
//	**
//	** If the expression must return NULL, then 0x00 is returned.
//	*/
func _sqlite3ExprDataType(tls *libc.TLS, pExpr uintptr) (r int32) {
	var aff, ii, res int32
	var pList uintptr
	_, _, _, _ = aff, ii, pList, res
	for pExpr != 0 {
		switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) {
		case int32(TK_COLLATE):
			fallthrough
		case int32(TK_IF_NULL_ROW):
			fallthrough
		case int32(TK_UPLUS):
			pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
		case int32(TK_NULL):
			pExpr = uintptr(0)
		case int32(TK_STRING):
			return int32(0x02)
		case int32(TK_BLOB):
			return int32(0x04)
		case int32(TK_CONCAT):
			return int32(0x06)
		case int32(TK_VARIABLE):
			fallthrough
		case int32(TK_AGG_FUNCTION):
			fallthrough
		case int32(TK_FUNCTION):
			return int32(0x07)
		case int32(TK_COLUMN):
			fallthrough
		case int32(TK_AGG_COLUMN):
			fallthrough
		case int32(TK_SELECT):
			fallthrough
		case int32(TK_CAST):
			fallthrough
		case int32(TK_SELECT_COLUMN):
			fallthrough
		case int32(TK_VECTOR):
			aff = int32(_sqlite3ExprAffinity(tls, pExpr))
			if aff >= int32(SQLITE_AFF_NUMERIC) {
				return int32(0x05)
			}
			if aff == int32(SQLITE_AFF_TEXT) {
				return int32(0x06)
			}
			return int32(0x07)
		case int32(TK_CASE):
			res = 0
			pList = *(*uintptr)(unsafe.Pointer(pExpr + 20))
			ii = int32(1)
			for {
				if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
					break
				}
				res = res | _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr)
				goto _1
			_1:
				;
				ii = ii + int32(2)
			}
			if (*TExprList)(unsafe.Pointer(pList)).FnExpr%int32(2) != 0 {
				res = res | _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*20))).FpExpr)
			}
			return res
		default:
			return int32(0x01)
		} /* End of switch(op) */
	} /* End of while(pExpr) */
	return 0x00
}

// C documentation
//
//	/*
//	** Recursively delete an expression tree.
//	*/
func _sqlite3ExprDeleteNN(tls *libc.TLS, db uintptr, p uintptr) {
	var pLeft uintptr
	_ = pLeft
	goto exprDeleteRestart
exprDeleteRestart:
	;
	if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Leaf)) != libc.Uint32FromInt32(0)) {
		/* The Expr.x union is never used at the same time as Expr.pRight */
		if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 {
			_sqlite3ExprDeleteNN(tls, db, (*TExpr)(unsafe.Pointer(p)).FpRight)
		} else {
			if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
				_sqlite3SelectDelete(tls, db, *(*uintptr)(unsafe.Pointer(p + 20)))
			} else {
				_sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(p + 20)))
				if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
					_sqlite3WindowDelete(tls, db, *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)))
				}
			}
		}
		if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 && int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_SELECT_COLUMN) {
			pLeft = (*TExpr)(unsafe.Pointer(p)).FpLeft
			if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Static)) != libc.Uint32FromInt32(0)) && !((*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(libc.Int32FromInt32(EP_Static)) != libc.Uint32FromInt32(0)) {
				/* Avoid unnecessary recursion on unary operators */
				_sqlite3DbNNFreeNN(tls, db, p)
				p = pLeft
				goto exprDeleteRestart
			} else {
				_sqlite3ExprDeleteNN(tls, db, pLeft)
			}
		}
	}
	if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Static)) != libc.Uint32FromInt32(0)) {
		_sqlite3DbNNFreeNN(tls, db, p)
	}
}

// C documentation
//
//	/*
//	** Compute and return a new Expr object which when passed to
//	** sqlite3ExprCode() will generate all necessary code to compute
//	** the iField-th column of the vector expression pVector.
//	**
//	** It is ok for pVector to be a scalar (as long as iField==0).
//	** In that case, this routine works like sqlite3ExprDup().
//	**
//	** The caller owns the returned Expr object and is responsible for
//	** ensuring that the returned value eventually gets freed.
//	**
//	** The caller retains ownership of pVector.  If pVector is a TK_SELECT,
//	** then the returned object will reference pVector and so pVector must remain
//	** valid for the life of the returned object.  If pVector is a TK_VECTOR
//	** or a scalar expression, then it can be deleted as soon as this routine
//	** returns.
//	**
//	** A trick to cause a TK_SELECT pVector to be deleted together with
//	** the returned Expr object is to attach the pVector to the pRight field
//	** of the returned TK_SELECT_COLUMN Expr object.
//	*/
func _sqlite3ExprForVectorField(tls *libc.TLS, pParse uintptr, pVector uintptr, iField int32, nField int32) (r uintptr) {
	var pRet, ppVector uintptr
	_, _ = pRet, ppVector
	if int32((*TExpr)(unsafe.Pointer(pVector)).Fop) == int32(TK_SELECT) {
		/* The TK_SELECT_COLUMN Expr node:
		 **
		 ** pLeft:           pVector containing TK_SELECT.  Not deleted.
		 ** pRight:          not used.  But recursively deleted.
		 ** iColumn:         Index of a column in pVector
		 ** iTable:          0 or the number of columns on the LHS of an assignment
		 ** pLeft->iTable:   First in an array of register holding result, or 0
		 **                  if the result is not yet computed.
		 **
		 ** sqlite3ExprDelete() specifically skips the recursive delete of
		 ** pLeft on TK_SELECT_COLUMN nodes.  But pRight is followed, so pVector
		 ** can be attached to pRight to cause this node to take ownership of
		 ** pVector.  Typically there will be multiple TK_SELECT_COLUMN nodes
		 ** with the same pLeft pointer to the pVector, but only one of them
		 ** will own the pVector.
		 */
		pRet = _sqlite3PExpr(tls, pParse, int32(TK_SELECT_COLUMN), uintptr(0), uintptr(0))
		if pRet != 0 {
			**(**Tu32)(__ccgo_up(pRet + 4)) |= uint32(libc.Int32FromInt32(EP_FullSize))
			(*TExpr)(unsafe.Pointer(pRet)).FiTable = nField
			(*TExpr)(unsafe.Pointer(pRet)).FiColumn = int16(iField)
			(*TExpr)(unsafe.Pointer(pRet)).FpLeft = pVector
		}
	} else {
		if int32((*TExpr)(unsafe.Pointer(pVector)).Fop) == int32(TK_VECTOR) {
			ppVector = *(*uintptr)(unsafe.Pointer(pVector + 20)) + 8 + uintptr(iField)*20
			pVector = **(**uintptr)(__ccgo_up(ppVector))
			if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
				/* This must be a vector UPDATE inside a trigger */
				**(**uintptr)(__ccgo_up(ppVector)) = uintptr(0)
				return pVector
			}
		}
		pRet = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pVector, 0)
	}
	return pRet
}

// C documentation
//
//	/*
//	** Construct a new expression node for a function with multiple
//	** arguments.
//	*/
func _sqlite3ExprFunction(tls *libc.TLS, pParse uintptr, pList uintptr, pToken uintptr, eDistinct int32) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pNew uintptr
	_, _ = db, pNew
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pNew = _sqlite3ExprAlloc(tls, db, int32(TK_FUNCTION), pToken, int32(1))
	if pNew == uintptr(0) {
		_sqlite3ExprListDelete(tls, db, pList) /* Avoid memory leak when malloc fails */
		return uintptr(0)
	}
	*(*int32)(unsafe.Pointer(pNew + 36)) = int32((*TToken)(unsafe.Pointer(pToken)).Fz) - int32((*TParse)(unsafe.Pointer(pParse)).FzTail)
	if pList != 0 && (*TExprList)(unsafe.Pointer(pList)).FnExpr > **(**int32)(__ccgo_up((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 6*4)) && !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9172, libc.VaList(bp+8, pToken))
	}
	*(*uintptr)(unsafe.Pointer(pNew + 20)) = pList
	**(**Tu32)(__ccgo_up(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_HasFunc))
	_sqlite3ExprSetHeightAndFlags(tls, pParse, pNew)
	if eDistinct == int32(SF_Distinct) {
		**(**Tu32)(__ccgo_up(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_Distinct))
	}
	return pNew
}

// C documentation
//
//	/*
//	** Return true if we can prove the pE2 will always be true if pE1 is
//	** true.  Return false if we cannot complete the proof or if pE2 might
//	** be false.  Examples:
//	**
//	**     pE1: x==5        pE2: x==5             Result: true
//	**     pE1: x>0         pE2: x==5             Result: false
//	**     pE1: x=21        pE2: x=21 OR y=43     Result: true
//	**     pE1: x!=123      pE2: x IS NOT NULL    Result: true
//	**     pE1: x!=?1       pE2: x IS NOT NULL    Result: true
//	**     pE1: x IS NULL   pE2: x IS NOT NULL    Result: false
//	**     pE1: x IS ?2     pE2: x IS NOT NULL    Result: false
//	**     pE1: iif(x,y)    pE2: x                Result: true
//	**     PE1: iif(x,y,0)  pE2: x                Result: true
//	**
//	** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
//	** Expr.iTable<0 then assume a table number given by iTab.
//	**
//	** If pParse is not NULL, then the values of bound variables in pE1 are
//	** compared against literal values in pE2 and pParse->pVdbe->expmask is
//	** modified to record which bound variables are referenced.  If pParse
//	** is NULL, then false will be returned if pE1 contains any bound variables.
//	**
//	** When in doubt, return false.  Returning true might give a performance
//	** improvement.  Returning false might cause a performance reduction, but
//	** it will always give the correct answer and is hence always safe.
//	*/
func _sqlite3ExprImpliesExpr(tls *libc.TLS, pParse uintptr, pE1 uintptr, pE2 uintptr, iTab int32) (r int32) {
	if _sqlite3ExprCompare(tls, pParse, pE1, pE2, iTab) == 0 {
		return int32(1)
	}
	if int32((*TExpr)(unsafe.Pointer(pE2)).Fop) == int32(TK_OR) && (_sqlite3ExprImpliesExpr(tls, pParse, pE1, (*TExpr)(unsafe.Pointer(pE2)).FpLeft, iTab) != 0 || _sqlite3ExprImpliesExpr(tls, pParse, pE1, (*TExpr)(unsafe.Pointer(pE2)).FpRight, iTab) != 0) {
		return int32(1)
	}
	if int32((*TExpr)(unsafe.Pointer(pE2)).Fop) == int32(TK_NOTNULL) && _exprImpliesNotNull(tls, pParse, pE1, (*TExpr)(unsafe.Pointer(pE2)).FpLeft, iTab, 0) != 0 {
		return int32(1)
	}
	if _sqlite3ExprIsIIF(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pE1) != 0 {
		return _sqlite3ExprImpliesExpr(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pE1 + 20)) + 8))).FpExpr, pE2, iTab)
	}
	return 0
}

// C documentation
//
//	/*
//	** Return true (non-zero) if expression p can only be true if at least
//	** one column of table iTab is non-null.  In other words, return true
//	** if expression p will always be NULL or false if every column of iTab
//	** is NULL.
//	**
//	** False negatives are acceptable.  In other words, it is ok to return
//	** zero even if expression p will never be true of every column of iTab
//	** is NULL.  A false negative is merely a missed optimization opportunity.
//	**
//	** False positives are not allowed, however.  A false positive may result
//	** in an incorrect answer.
//	**
//	** Terms of p that are marked with EP_OuterON (and hence that come from
//	** the ON or USING clauses of OUTER JOINS) are excluded from the analysis.
//	**
//	** This routine is used to check if a LEFT JOIN can be converted into
//	** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
//	** clause requires that some column of the right table of the LEFT JOIN
//	** be non-NULL, then the LEFT JOIN can be safely converted into an
//	** ordinary join.
//	*/
func _sqlite3ExprImpliesNonNullRow(tls *libc.TLS, p uintptr, iTab int32, isRJ int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var _ /* w at bp+0 */ TWalker
	p = _sqlite3ExprSkipCollateAndLikely(tls, p)
	if p == uintptr(0) {
		return 0
	}
	if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_NOTNULL) {
		p = (*TExpr)(unsafe.Pointer(p)).FpLeft
	} else {
		for int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_AND) {
			if _sqlite3ExprImpliesNonNullRow(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft, iTab, isRJ) != 0 {
				return int32(1)
			}
			p = (*TExpr)(unsafe.Pointer(p)).FpRight
		}
	}
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_impliesNotNullRow)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = uintptr(0)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback2 = uintptr(0)
	(**(**TWalker)(__ccgo_up(bp))).FeCode = uint16(0)
	(**(**TWalker)(__ccgo_up(bp))).FmWFlags = libc.BoolUint16(isRJ != 0)
	*(*int32)(unsafe.Pointer(bp + 24)) = iTab
	_sqlite3WalkExpr(tls, bp, p)
	return int32((**(**TWalker)(__ccgo_up(bp))).FeCode)
}

// C documentation
//
//	/*
//	** Allocate an expression for a 32-bit signed integer literal.
//	*/
func _sqlite3ExprInt32(tls *libc.TLS, db uintptr, iVal int32) (r uintptr) {
	var pNew uintptr
	var v1 int32
	_, _ = pNew, v1
	pNew = _sqlite3DbMallocRawNN(tls, db, uint64(52))
	if pNew != 0 {
		libc.Xmemset(tls, pNew, 0, uint32(52))
		(*TExpr)(unsafe.Pointer(pNew)).Fop = uint8(TK_INTEGER)
		(*TExpr)(unsafe.Pointer(pNew)).FiAgg = int16(-int32(1))
		if iVal != 0 {
			v1 = int32(EP_IsTrue)
		} else {
			v1 = int32(EP_IsFalse)
		}
		(*TExpr)(unsafe.Pointer(pNew)).Fflags = uint32(libc.Int32FromInt32(EP_IntValue) | libc.Int32FromInt32(EP_Leaf) | v1)
		*(*int32)(unsafe.Pointer(pNew + 8)) = iVal
		(*TExpr)(unsafe.Pointer(pNew)).FnHeight = int32(1)
	}
	return pNew
}

// C documentation
//
//	/*
//	** Walk the expression tree passed as the first argument. Return non-zero
//	** if the expression consists entirely of constants or copies of terms
//	** in pGroupBy that sort with the BINARY collation sequence.
//	**
//	** This routine is used to determine if a term of the HAVING clause can
//	** be promoted into the WHERE clause.  In order for such a promotion to work,
//	** the value of the HAVING clause term must be the same for all members of
//	** a "group".  The requirement that the GROUP BY term must be BINARY
//	** assumes that no other collating sequence will have a finer-grained
//	** grouping than binary.  In other words (A=B COLLATE binary) implies
//	** A=B in every other collating sequence.  The requirement that the
//	** GROUP BY be BINARY is stricter than necessary.  It would also work
//	** to promote HAVING clauses that use the same alternative collating
//	** sequence as the GROUP BY term, but that is much harder to check,
//	** alternative collating sequences are uncommon, and this is only an
//	** optimization, so we take the easy way out and simply require the
//	** GROUP BY to use the BINARY collating sequence.
//	*/
func _sqlite3ExprIsConstantOrGroupBy(tls *libc.TLS, pParse uintptr, p uintptr, pGroupBy uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var _ /* w at bp+0 */ TWalker
	(**(**TWalker)(__ccgo_up(bp))).FeCode = uint16(1)
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_exprNodeIsConstantOrGroupBy)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = uintptr(0)
	*(*uintptr)(unsafe.Pointer(bp + 24)) = pGroupBy
	(**(**TWalker)(__ccgo_up(bp))).FpParse = pParse
	_sqlite3WalkExpr(tls, bp, p)
	return int32((**(**TWalker)(__ccgo_up(bp))).FeCode)
}

// C documentation
//
//	/*
//	** Return true if the expression is one of the following:
//	**
//	**    CASE WHEN x THEN y END
//	**    CASE WHEN x THEN y ELSE NULL END
//	**    CASE WHEN x THEN y ELSE false END
//	**    iif(x,y)
//	**    iif(x,y,NULL)
//	**    iif(x,y,false)
//	*/
func _sqlite3ExprIsIIF(tls *libc.TLS, db uintptr, pExpr uintptr) (r int32) {
	var pDef, pList, z uintptr
	_, _, _ = pDef, pList, z
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) {
		z = *(*uintptr)(unsafe.Pointer(pExpr + 8))
		if int32(**(**int8)(__ccgo_up(z))) != int32('i') && int32(**(**int8)(__ccgo_up(z))) != int32('I') {
			return 0
		}
		if *(*uintptr)(unsafe.Pointer(pExpr + 20)) == uintptr(0) {
			return 0
		}
		pDef = _sqlite3FindFunction(tls, db, z, (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uint8(0))
		if pDef == uintptr(0) {
			return 0
		}
		if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_INLINE) == uint32(0) {
			return 0
		}
		if int32((*TFuncDef)(unsafe.Pointer(pDef)).FpUserData) != int32(INLINEFUNC_iif) {
			return 0
		}
	} else {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_CASE) {
			if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != uintptr(0) {
				return 0
			}
		} else {
			return 0
		}
	}
	pList = *(*uintptr)(unsafe.Pointer(pExpr + 20))
	if (*TExprList)(unsafe.Pointer(pList)).FnExpr == int32(2) {
		return int32(1)
	}
	if (*TExprList)(unsafe.Pointer(pList)).FnExpr == int32(3) && _sqlite3ExprIsNotTrue(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 2*20))).FpExpr) != 0 {
		return int32(1)
	}
	return 0
}

// C documentation
//
//	/*
//	** If the expression p codes a constant integer that is small enough
//	** to fit in a 32-bit integer, return 1 and put the value of the integer
//	** in *pValue.  If the expression is not an integer or if it is too big
//	** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
//	**
//	** If the pParse pointer is provided, then allow the expression p to be
//	** a parameter (TK_VARIABLE) that is bound to an integer.
//	** But if pParse is NULL, then p must be a pure integer literal.
//	*/
func _sqlite3ExprIsInteger(tls *libc.TLS, p uintptr, pValue uintptr, pParse uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pVal uintptr
	var rc int32
	var vv Tsqlite3_int64
	var _ /* v at bp+0 */ int32
	_, _, _ = pVal, rc, vv
	rc = 0
	if p == uintptr(0) {
		return 0
	} /* Used to only happen following on OOM */
	/* If an expression is an integer literal that fits in a signed 32-bit
	 ** integer, then the EP_IntValue flag will have already been set */
	if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_IntValue) != 0 {
		**(**int32)(__ccgo_up(pValue)) = *(*int32)(unsafe.Pointer(p + 8))
		return int32(1)
	}
	switch int32((*TExpr)(unsafe.Pointer(p)).Fop) {
	case int32(TK_UPLUS):
		rc = _sqlite3ExprIsInteger(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft, pValue, uintptr(0))
	case int32(TK_UMINUS):
		**(**int32)(__ccgo_up(bp)) = 0
		if _sqlite3ExprIsInteger(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft, bp, uintptr(0)) != 0 {
			**(**int32)(__ccgo_up(pValue)) = -**(**int32)(__ccgo_up(bp))
			rc = int32(1)
		}
	case int32(TK_VARIABLE):
		if pParse == uintptr(0) {
			break
		}
		if (*TParse)(unsafe.Pointer(pParse)).FpVdbe == uintptr(0) {
			break
		}
		if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_EnableQPSG) != uint64(0) {
			break
		}
		_sqlite3VdbeSetVarmask(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32((*TExpr)(unsafe.Pointer(p)).FiColumn))
		pVal = _sqlite3VdbeGetBoundValue(tls, (*TParse)(unsafe.Pointer(pParse)).FpReprepare, int32((*TExpr)(unsafe.Pointer(p)).FiColumn), uint8(SQLITE_AFF_BLOB))
		if pVal != 0 {
			if Xsqlite3_value_type(tls, pVal) == int32(SQLITE_INTEGER) {
				vv = Xsqlite3_value_int64(tls, pVal)
				if vv == vv&int64(0x7fffffff) { /* non-negative numbers only */
					**(**int32)(__ccgo_up(pValue)) = int32(vv)
					rc = int32(1)
				}
			}
			_sqlite3ValueFree(tls, pVal)
		}
	default:
		break
	}
	return rc
}

// C documentation
//
//	/*
//	** If pExpr is one of "like", "glob", "match", or "regexp", then
//	** return the corresponding SQLITE_INDEX_CONSTRAINT_xxxx value.
//	** If not, return 0.
//	**
//	** pExpr is guaranteed to be a TK_FUNCTION.
//	*/
func _sqlite3ExprIsLikeOperator(tls *libc.TLS, pExpr uintptr) (r int32) {
	var i int32
	_ = i
	i = 0
	for {
		if !(i < int32(libc.Uint32FromInt64(32)/libc.Uint32FromInt64(8))) {
			break
		}
		if _sqlite3StrICmp(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), _aOp[i].FzOp) == 0 {
			return int32(_aOp[i].FeOp)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** Check pExpr to see if it is an constraint on the single data source
//	** pSrc = &pSrcList->a[iSrc].  In other words, check to see if pExpr
//	** constrains pSrc but does not depend on any other tables or data
//	** sources anywhere else in the query.  Return true (non-zero) if pExpr
//	** is a constraint on pSrc only.
//	**
//	** This is an optimization.  False negatives will perhaps cause slower
//	** queries, but false positives will yield incorrect answers.  So when in
//	** doubt, return 0.
//	**
//	** To be an single-source constraint, the following must be true:
//	**
//	**   (1)  pExpr cannot refer to any table other than pSrc->iCursor.
//	**
//	**   (2a) pExpr cannot use subqueries unless the bAllowSubq parameter is
//	**        true and the subquery is non-correlated
//	**
//	**   (2b) pExpr cannot use non-deterministic functions.
//	**
//	**   (3)  pSrc cannot be part of the left operand for a RIGHT JOIN.
//	**        (Is there some way to relax this constraint?)
//	**
//	**   (4)  If pSrc is the right operand of a LEFT JOIN, then...
//	**         (4a)  pExpr must come from an ON clause..
//	**         (4b)  and specifically the ON clause associated with the LEFT JOIN.
//	**
//	**   (5)  If pSrc is the right operand of a LEFT JOIN or the left
//	**        operand of a RIGHT JOIN, then pExpr must be from the WHERE
//	**        clause, not an ON clause.
//	**
//	**   (6) Either:
//	**
//	**       (6a) pExpr does not originate in an ON or USING clause, or
//	**
//	**       (6b) The ON or USING clause from which pExpr is derived is
//	**            not to the left of a RIGHT JOIN (or FULL JOIN).
//	**
//	**       Without this restriction, accepting pExpr as a single-table
//	**       constraint might move the the ON/USING filter expression
//	**       from the left side of a RIGHT JOIN over to the right side,
//	**       which leads to incorrect answers.  See also restriction (9)
//	**       on push-down.
//	*/
func _sqlite3ExprIsSingleTableConstraint(tls *libc.TLS, pExpr uintptr, pSrcList uintptr, iSrc int32, bAllowSubq int32) (r int32) {
	var jj int32
	var pSrc uintptr
	_, _ = jj, pSrc
	pSrc = pSrcList + 8 + uintptr(iSrc)*48
	if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
		return 0 /* rule (3) */
	}
	if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&int32(JT_LEFT) != 0 {
		if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) {
			return 0
		} /* rule (4a) */
		if *(*int32)(unsafe.Pointer(pExpr + 36)) != (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor {
			return 0
		} /* rule (4b) */
	} else {
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) {
			return 0
		} /* rule (5) */
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) && int32((*(*TSrcItem)(unsafe.Pointer(pSrcList + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
		jj = 0
		for {
			if !(jj < iSrc) {
				break
			}
			if *(*int32)(unsafe.Pointer(pExpr + 36)) == (*(*TSrcItem)(unsafe.Pointer(pSrcList + 8 + uintptr(jj)*48))).FiCursor {
				if int32((*(*TSrcItem)(unsafe.Pointer(pSrcList + 8 + uintptr(jj)*48))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
					return 0 /* restriction (6) */
				}
				break
			}
			goto _1
		_1:
			;
			jj = jj + 1
		}
	}
	/* Rules (1), (2a), and (2b) handled by the following: */
	return _sqlite3ExprIsTableConstant(tls, pExpr, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor, bAllowSubq)
}

// C documentation
//
//	/*
//	** Walk an expression tree.  Return non-zero if the expression is constant
//	** for any single row of the table with cursor iCur.  In other words, the
//	** expression must not refer to any non-deterministic function nor any
//	** table other than iCur.
//	**
//	** Consider uncorrelated subqueries to be constants if the bAllowSubq
//	** parameter is true.
//	*/
func _sqlite3ExprIsTableConstant(tls *libc.TLS, p uintptr, iCur int32, bAllowSubq int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var _ /* w at bp+0 */ TWalker
	(**(**TWalker)(__ccgo_up(bp))).FeCode = uint16(3)
	(**(**TWalker)(__ccgo_up(bp))).FpParse = uintptr(0)
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_exprNodeIsConstant)
	if bAllowSubq != 0 {
		(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_exprSelectWalkTableConstant)
	} else {
		(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkFail)
	}
	*(*int32)(unsafe.Pointer(bp + 24)) = iCur
	_sqlite3WalkExpr(tls, bp, p)
	return int32((**(**TWalker)(__ccgo_up(bp))).FeCode)
}

func _sqlite3ExprListAppendGrow(tls *libc.TLS, db uintptr, pList uintptr, pExpr uintptr) (r uintptr) {
	var pItem, pNew, v2 uintptr
	var v1 int32
	_, _, _, _ = pItem, pNew, v1, v2
	**(**int32)(__ccgo_up(pList + 4)) *= int32(2)
	pNew = _sqlite3DbRealloc(tls, db, pList, uint64(uint32(libc.UintptrFromInt32(0)+8)+uint32((*TExprList)(unsafe.Pointer(pList)).FnAlloc)*libc.Uint32FromInt64(20)))
	if pNew == uintptr(0) {
		_sqlite3ExprListDelete(tls, db, pList)
		_sqlite3ExprDelete(tls, db, pExpr)
		return uintptr(0)
	} else {
		pList = pNew
	}
	v2 = pList
	v1 = *(*int32)(unsafe.Pointer(v2))
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	pItem = pList + 8 + uintptr(v1)*20
	**(**TExprList_item)(__ccgo_up(pItem)) = _zeroItem
	(*TExprList_item)(unsafe.Pointer(pItem)).FpExpr = pExpr
	return pList
}

func _sqlite3ExprListAppendNew(tls *libc.TLS, db uintptr, pExpr uintptr) (r uintptr) {
	var pItem, pList uintptr
	_, _ = pItem, pList
	pList = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(libc.UintptrFromInt32(0)+8)+uint32(libc.Int32FromInt32(4))*libc.Uint32FromInt64(20)))
	if pList == uintptr(0) {
		_sqlite3ExprDelete(tls, db, pExpr)
		return uintptr(0)
	}
	(*TExprList)(unsafe.Pointer(pList)).FnAlloc = int32(4)
	(*TExprList)(unsafe.Pointer(pList)).FnExpr = int32(1)
	pItem = pList + 8
	**(**TExprList_item)(__ccgo_up(pItem)) = _zeroItem
	(*TExprList_item)(unsafe.Pointer(pItem)).FpExpr = pExpr
	return pList
}

// C documentation
//
//	/*
//	** pColumns and pExpr form a vector assignment which is part of the SET
//	** clause of an UPDATE statement.  Like this:
//	**
//	**        (a,b,c) = (expr1,expr2,expr3)
//	** Or:    (a,b,c) = (SELECT x,y,z FROM ....)
//	**
//	** For each term of the vector assignment, append new entries to the
//	** expression list pList.  In the case of a subquery on the RHS, append
//	** TK_SELECT_COLUMN expressions.
//	*/
func _sqlite3ExprListAppendVector(tls *libc.TLS, pParse uintptr, pList uintptr, pColumns uintptr, pExpr uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pFirst, pSubExpr uintptr
	var i, iFirst, n, v1 int32
	var v3 bool
	_, _, _, _, _, _, _, _ = db, i, iFirst, n, pFirst, pSubExpr, v1, v3
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if pList != 0 {
		v1 = (*TExprList)(unsafe.Pointer(pList)).FnExpr
	} else {
		v1 = 0
	}
	iFirst = v1
	/* pColumns can only be NULL due to an OOM but an OOM will cause an
	 ** exit prior to this routine being invoked */
	if pColumns == uintptr(0) {
		goto vector_append_error
	}
	if pExpr == uintptr(0) {
		goto vector_append_error
	}
	/* If the RHS is a vector, then we can immediately check to see that
	 ** the size of the RHS and LHS match.  But if the RHS is a SELECT,
	 ** wildcards ("*") in the result set of the SELECT must be expanded before
	 ** we can do the size check, so defer the size check until code generation.
	 */
	if v3 = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_SELECT); v3 {
		v1 = _sqlite3ExprVectorSize(tls, pExpr)
		n = v1
	}
	if v3 && (*TIdList)(unsafe.Pointer(pColumns)).FnId != v1 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9342, libc.VaList(bp+8, (*TIdList)(unsafe.Pointer(pColumns)).FnId, n))
		goto vector_append_error
	}
	i = 0
	for {
		if !(i < (*TIdList)(unsafe.Pointer(pColumns)).FnId) {
			break
		}
		pSubExpr = _sqlite3ExprForVectorField(tls, pParse, pExpr, i, (*TIdList)(unsafe.Pointer(pColumns)).FnId)
		if pSubExpr == uintptr(0) {
			goto _4
		}
		pList = _sqlite3ExprListAppend(tls, pParse, pList, pSubExpr)
		if pList != 0 {
			(*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*20))).FzEName = (*(*TIdList_item)(unsafe.Pointer(pColumns + 4 + uintptr(i)*4))).FzName
			(*(*TIdList_item)(unsafe.Pointer(pColumns + 4 + uintptr(i)*4))).FzName = uintptr(0)
		}
		goto _4
	_4:
		;
		i = i + 1
	}
	if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_SELECT) && pList != uintptr(0) {
		pFirst = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(iFirst)*20))).FpExpr
		/* Store the SELECT statement in pRight so it will be deleted when
		 ** sqlite3ExprListDelete() is called */
		(*TExpr)(unsafe.Pointer(pFirst)).FpRight = pExpr
		pExpr = uintptr(0)
		/* Remember the size of the LHS in iTable so that we can check that
		 ** the RHS and LHS sizes match during code generation. */
		(*TExpr)(unsafe.Pointer(pFirst)).FiTable = (*TIdList)(unsafe.Pointer(pColumns)).FnId
	}
	goto vector_append_error
vector_append_error:
	;
	_sqlite3ExprUnmapAndDelete(tls, pParse, pExpr)
	_sqlite3IdListDelete(tls, db, pColumns)
	return pList
}

// C documentation
//
//	/*
//	** If the expression list pEList contains more than iLimit elements,
//	** leave an error message in pParse.
//	*/
func _sqlite3ExprListCheckLength(tls *libc.TLS, pParse uintptr, pEList uintptr, zObject uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var mx int32
	_ = mx
	mx = **(**int32)(__ccgo_up((*TParse)(unsafe.Pointer(pParse)).Fdb + 120 + 2*4))
	if pEList != 0 && (*TExprList)(unsafe.Pointer(pEList)).FnExpr > mx {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9372, libc.VaList(bp+8, zObject))
	}
}

// C documentation
//
//	/*
//	** Compare two ExprList objects.  Return 0 if they are identical, 1
//	** if they are certainly different, or 2 if it is not possible to
//	** determine if they are identical or not.
//	**
//	** If any subelement of pB has Expr.iTable==(-1) then it is allowed
//	** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
//	**
//	** This routine might return non-zero for equivalent ExprLists.  The
//	** only consequence will be disabled optimizations.  But this routine
//	** must never return 0 if the two ExprList objects are different, or
//	** a malfunction will result.
//	**
//	** Two NULL pointers are considered to be the same.  But a NULL pointer
//	** always differs from a non-NULL pointer.
//	*/
func _sqlite3ExprListCompare(tls *libc.TLS, pA uintptr, pB uintptr, iTab int32) (r int32) {
	var i, res, v2 int32
	var pExprA, pExprB uintptr
	_, _, _, _, _ = i, pExprA, pExprB, res, v2
	if pA == uintptr(0) && pB == uintptr(0) {
		return 0
	}
	if pA == uintptr(0) || pB == uintptr(0) {
		return int32(1)
	}
	if (*TExprList)(unsafe.Pointer(pA)).FnExpr != (*TExprList)(unsafe.Pointer(pB)).FnExpr {
		return int32(1)
	}
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pA)).FnExpr) {
			break
		}
		pExprA = (*(*TExprList_item)(unsafe.Pointer(pA + 8 + uintptr(i)*20))).FpExpr
		pExprB = (*(*TExprList_item)(unsafe.Pointer(pB + 8 + uintptr(i)*20))).FpExpr
		if int32((*(*TExprList_item)(unsafe.Pointer(pA + 8 + uintptr(i)*20))).Ffg.FsortFlags) != int32((*(*TExprList_item)(unsafe.Pointer(pB + 8 + uintptr(i)*20))).Ffg.FsortFlags) {
			return int32(1)
		}
		v2 = _sqlite3ExprCompare(tls, uintptr(0), pExprA, pExprB, iTab)
		res = v2
		if v2 != 0 {
			return res
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return 0
}

func _sqlite3ExprListDup(tls *libc.TLS, db uintptr, p uintptr, flags int32) (r uintptr) {
	var i int32
	var pItem, pNew, pNewExpr, pOldExpr, pOldItem, pPriorSelectColNew, pPriorSelectColOld, v2 uintptr
	var v3 bool
	_, _, _, _, _, _, _, _, _, _ = i, pItem, pNew, pNewExpr, pOldExpr, pOldItem, pPriorSelectColNew, pPriorSelectColOld, v2, v3
	pPriorSelectColOld = uintptr(0)
	pPriorSelectColNew = uintptr(0)
	if p == uintptr(0) {
		return uintptr(0)
	}
	pNew = _sqlite3DbMallocRawNN(tls, db, uint64(_sqlite3DbMallocSize(tls, db, p)))
	if pNew == uintptr(0) {
		return uintptr(0)
	}
	(*TExprList)(unsafe.Pointer(pNew)).FnExpr = (*TExprList)(unsafe.Pointer(p)).FnExpr
	(*TExprList)(unsafe.Pointer(pNew)).FnAlloc = (*TExprList)(unsafe.Pointer(p)).FnAlloc
	pItem = pNew + 8
	pOldItem = p + 8
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(p)).FnExpr) {
			break
		}
		pOldExpr = (*TExprList_item)(unsafe.Pointer(pOldItem)).FpExpr
		(*TExprList_item)(unsafe.Pointer(pItem)).FpExpr = _sqlite3ExprDup(tls, db, pOldExpr, flags)
		if v3 = pOldExpr != 0 && int32((*TExpr)(unsafe.Pointer(pOldExpr)).Fop) == int32(TK_SELECT_COLUMN); v3 {
			v2 = (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr
			pNewExpr = v2
		}
		if v3 && v2 != uintptr(0) {
			if (*TExpr)(unsafe.Pointer(pNewExpr)).FpRight != 0 {
				pPriorSelectColOld = (*TExpr)(unsafe.Pointer(pOldExpr)).FpRight
				pPriorSelectColNew = (*TExpr)(unsafe.Pointer(pNewExpr)).FpRight
				(*TExpr)(unsafe.Pointer(pNewExpr)).FpLeft = (*TExpr)(unsafe.Pointer(pNewExpr)).FpRight
			} else {
				if (*TExpr)(unsafe.Pointer(pOldExpr)).FpLeft != pPriorSelectColOld {
					pPriorSelectColOld = (*TExpr)(unsafe.Pointer(pOldExpr)).FpLeft
					pPriorSelectColNew = _sqlite3ExprDup(tls, db, pPriorSelectColOld, flags)
					(*TExpr)(unsafe.Pointer(pNewExpr)).FpRight = pPriorSelectColNew
				}
				(*TExpr)(unsafe.Pointer(pNewExpr)).FpLeft = pPriorSelectColNew
			}
		}
		(*TExprList_item)(unsafe.Pointer(pItem)).FzEName = _sqlite3DbStrDup(tls, db, (*TExprList_item)(unsafe.Pointer(pOldItem)).FzEName)
		(*TExprList_item)(unsafe.Pointer(pItem)).Ffg = (*TExprList_item)(unsafe.Pointer(pOldItem)).Ffg
		(*TExprList_item)(unsafe.Pointer(pItem)).Fu = (*TExprList_item)(unsafe.Pointer(pOldItem)).Fu
		goto _1
	_1:
		;
		i = i + 1
		pItem += 20
		pOldItem += 20
	}
	return pNew
}

// C documentation
//
//	/*
//	** Set the ExprList.a[].zEName element of the most recently added item
//	** on the expression list.
//	**
//	** pList might be NULL following an OOM error.  But pName should never be
//	** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
//	** is set.
//	*/
func _sqlite3ExprListSetName(tls *libc.TLS, pParse uintptr, pList uintptr, pName uintptr, dequote int32) {
	var pItem uintptr
	_ = pItem
	if pList != 0 {
		pItem = pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*20
		(*TExprList_item)(unsafe.Pointer(pItem)).FzEName = _sqlite3DbStrNDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TToken)(unsafe.Pointer(pName)).Fz, uint64((*TToken)(unsafe.Pointer(pName)).Fn))
		if dequote != 0 {
			/* If dequote==0, then pName->z does not point to part of a DDL
			 ** statement handled by the parser. And so no token need be added
			 ** to the token-map.  */
			_sqlite3Dequote(tls, (*TExprList_item)(unsafe.Pointer(pItem)).FzEName)
			if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
				_sqlite3RenameTokenMap(tls, pParse, (*TExprList_item)(unsafe.Pointer(pItem)).FzEName, pName)
			}
		}
	}
}

// C documentation
//
//	/*
//	** Set the sort order for the last element on the given ExprList.
//	*/
func _sqlite3ExprListSetSortOrder(tls *libc.TLS, p uintptr, iSortOrder int32, eNulls int32) {
	var pItem, v1 uintptr
	_, _ = pItem, v1
	if p == uintptr(0) {
		return
	}
	pItem = p + 8 + uintptr((*TExprList)(unsafe.Pointer(p)).FnExpr-int32(1))*20
	if iSortOrder == -int32(1) {
		iSortOrder = SQLITE_SO_ASC
	}
	(*TExprList_item)(unsafe.Pointer(pItem)).Ffg.FsortFlags = uint8(iSortOrder)
	if eNulls != -int32(1) {
		libc.SetBitFieldPtr16Uint32(pItem+8+4, libc.Uint32FromInt32(1), 5, 0x20)
		if iSortOrder != eNulls {
			v1 = pItem + 8
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(KEYINFO_ORDER_BIGNULL))
		}
	}
}

// C documentation
//
//	/*
//	** Expression list pEList is a list of vector values. This function
//	** converts the contents of pEList to a VALUES(...) Select statement
//	** returning 1 row for each element of the list. For example, the
//	** expression list:
//	**
//	**   ( (1,2), (3,4) (5,6) )
//	**
//	** is translated to the equivalent of:
//	**
//	**   VALUES(1,2), (3,4), (5,6)
//	**
//	** Each of the vector values in pEList must contain exactly nElem terms.
//	** If a list element that is not a vector or does not contain nElem terms,
//	** an error message is left in pParse.
//	**
//	** This is used as part of processing IN(...) expressions with a list
//	** of vectors on the RHS. e.g. "... IN ((1,2), (3,4), (5,6))".
//	*/
func _sqlite3ExprListToValues(tls *libc.TLS, pParse uintptr, nElem int32, pEList uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var ii, nExprElem int32
	var pExpr, pRet, pSel, v2 uintptr
	_, _, _, _, _, _ = ii, nExprElem, pExpr, pRet, pSel, v2
	pRet = uintptr(0)
	ii = 0
	for {
		if !(ii < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
			break
		}
		pExpr = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20))).FpExpr
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_VECTOR) {
			nExprElem = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr
		} else {
			nExprElem = int32(1)
		}
		if nExprElem != nElem {
			if nExprElem > int32(1) {
				v2 = __ccgo_ts + 9126
			} else {
				v2 = __ccgo_ts + 1711
			}
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9128, libc.VaList(bp+8, nExprElem, v2, nElem))
			break
		}
		pSel = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(pExpr + 20)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_Values), uintptr(0))
		*(*uintptr)(unsafe.Pointer(pExpr + 20)) = uintptr(0)
		if pSel != 0 {
			if pRet != 0 {
				(*TSelect)(unsafe.Pointer(pSel)).Fop = uint8(TK_ALL)
				(*TSelect)(unsafe.Pointer(pSel)).FpPrior = pRet
			}
			pRet = pSel
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	if pRet != 0 && (*TSelect)(unsafe.Pointer(pRet)).FpPrior != 0 {
		**(**Tu32)(__ccgo_up(pRet + 4)) |= uint32(SF_MultiValue)
	}
	_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pEList)
	return pRet
}

// C documentation
//
//	/*
//	** Make arrangements to invoke OP_Null on a range of registers
//	** during initialization.
//	*/
func _sqlite3ExprNullRegisterRange(tls *libc.TLS, pParse uintptr, iReg int32, nReg int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var okConstFactor Tu8
	var _ /* t at bp+0 */ TExpr
	_ = okConstFactor
	okConstFactor = uint8(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28)) & 0x80 >> 7)))
	libc.Xmemset(tls, bp, 0, uint32(52))
	(**(**TExpr)(__ccgo_up(bp))).Fop = uint8(TK_NULLS)
	*(*int32)(unsafe.Pointer(bp + 44)) = nReg
	libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 7, 0x80)
	_sqlite3ExprCodeRunJustOnce(tls, pParse, bp, iReg)
	libc.SetBitFieldPtr16Uint32(pParse+28, uint32(okConstFactor), 7, 0x80)
}

// C documentation
//
//	/*
//	** pExpr is a CHECK constraint on a row that is being UPDATE-ed.  The
//	** only columns that are modified by the UPDATE are those for which
//	** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
//	**
//	** Return true if CHECK constraint pExpr uses any of the
//	** changing columns (or the rowid if it is changing).  In other words,
//	** return true if this CHECK constraint must be validated for
//	** the new row in the UPDATE statement.
//	**
//	** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
//	** The operation of this routine is the same - return true if an only if
//	** the expression uses one or more of columns identified by the second and
//	** third arguments.
//	*/
func _sqlite3ExprReferencesUpdatedColumn(tls *libc.TLS, pExpr uintptr, aiChng uintptr, chngRowid int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var v1 uintptr
	var _ /* w at bp+0 */ TWalker
	_ = v1
	libc.Xmemset(tls, bp, 0, uint32(28))
	(**(**TWalker)(__ccgo_up(bp))).FeCode = uint16(0)
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_checkConstraintExprNode)
	*(*uintptr)(unsafe.Pointer(bp + 24)) = aiChng
	_sqlite3WalkExpr(tls, bp, pExpr)
	if !(chngRowid != 0) {
		v1 = bp + 20
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(CKCNSTRNT_ROWID))
	}
	return libc.BoolInt32(int32((**(**TWalker)(__ccgo_up(bp))).FeCode) != 0)
}

// C documentation
//
//	/*
//	** Set the error offset for an Expr node, if possible.
//	*/
func _sqlite3ExprSetErrorOffset(tls *libc.TLS, pExpr uintptr, iOfst int32) {
	if pExpr == uintptr(0) {
		return
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)|libc.Int32FromInt32(EP_OuterON)) != uint32(0) {
		return
	}
	*(*int32)(unsafe.Pointer(pExpr + 36)) = iOfst
}

// C documentation
//
//	/*
//	** Skip over any TK_COLLATE operators and/or any unlikely()
//	** or likelihood() or likely() functions at the root of an
//	** expression.
//	*/
func _sqlite3ExprSkipCollateAndLikely(tls *libc.TLS, pExpr uintptr) (r uintptr) {
	for pExpr != 0 && (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Skip)|libc.Int32FromInt32(EP_Unlikely)) != uint32(0) {
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Unlikely)) != uint32(0) {
			pExpr = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8))).FpExpr
		} else {
			if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE) {
				pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
			} else {
				break
			}
		}
	}
	return pExpr
}

// C documentation
//
//	/*
//	** If the expression passed as the only argument is of type TK_VECTOR
//	** return the number of expressions in the vector. Or, if the expression
//	** is a sub-select, return the number of columns in the sub-select. For
//	** any other type of expression, return 1.
//	*/
func _sqlite3ExprVectorSize(tls *libc.TLS, pExpr uintptr) (r int32) {
	var op Tu8
	_ = op
	op = (*TExpr)(unsafe.Pointer(pExpr)).Fop
	if int32(op) == int32(TK_REGISTER) {
		op = (*TExpr)(unsafe.Pointer(pExpr)).Fop2
	}
	if int32(op) == int32(TK_VECTOR) {
		return (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr
	} else {
		if int32(op) == int32(TK_SELECT) {
			return (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList)).FnExpr
		} else {
			return int32(1)
		}
	}
	return r
}

// C documentation
//
//	/*
//	** Parameter zName points to a UTF-8 encoded string nName bytes long.
//	** Return the CollSeq* pointer for the collation sequence named zName
//	** for the encoding 'enc' from the database 'db'.
//	**
//	** If the entry specified is not found and 'create' is true, then create a
//	** new entry.  Otherwise return NULL.
//	**
//	** A separate function sqlite3LocateCollSeq() is a wrapper around
//	** this routine.  sqlite3LocateCollSeq() invokes the collation factory
//	** if necessary and generates an error message if the collating sequence
//	** cannot be found.
//	**
//	** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
//	*/
func _sqlite3FindCollSeq(tls *libc.TLS, db uintptr, enc Tu8, zName uintptr, create int32) (r uintptr) {
	var pColl uintptr
	_ = pColl
	if zName != 0 {
		pColl = _findCollSeqEntry(tls, db, zName, create)
		if pColl != 0 {
			pColl = pColl + uintptr(int32(enc)-int32(1))*20
		}
	} else {
		pColl = (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl
	}
	return pColl
}

// C documentation
//
//	/*
//	** Parameter zName points to a nul-terminated buffer containing the name
//	** of a database ("main", "temp" or the name of an attached db). This
//	** function returns the index of the named database in db->aDb[], or
//	** -1 if the named db cannot be found.
//	*/
func _sqlite3FindDbName(tls *libc.TLS, db uintptr, zName uintptr) (r int32) {
	var i int32
	var pDb uintptr
	_, _ = i, pDb
	i = -int32(1) /* Database number */
	if zName != 0 {
		i = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1)
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16
		for {
			if !(i >= 0) {
				break
			}
			if 0 == Xsqlite3_stricmp(tls, (*TDb)(unsafe.Pointer(pDb)).FzDbSName, zName) {
				break
			}
			/* "main" is always an acceptable alias for the primary database
			 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
			if i == 0 && 0 == Xsqlite3_stricmp(tls, __ccgo_ts+8033, zName) {
				break
			}
			goto _1
		_1:
			;
			i = i - 1
			pDb -= 16
		}
	}
	return i
}

// C documentation
//
//	/*
//	** Locate a user function given a name, a number of arguments and a flag
//	** indicating whether the function prefers UTF-16 over UTF-8.  Return a
//	** pointer to the FuncDef structure that defines that function, or return
//	** NULL if the function does not exist.
//	**
//	** If the createFlag argument is true, then a new (blank) FuncDef
//	** structure is created and liked into the "db" structure if a
//	** no matching function previously existed.
//	**
//	** If nArg is -2, then the first valid function found is returned.  A
//	** function is valid if xSFunc is non-zero.  The nArg==(-2)
//	** case is used to see if zName is a valid function name for some number
//	** of arguments.  If nArg is -2, then createFlag must be 0.
//	**
//	** If createFlag is false, then a function with the required name and
//	** number of arguments may be returned even if the eTextRep flag does not
//	** match that requested.
//	*/
func _sqlite3FindFunction(tls *libc.TLS, db uintptr, zName uintptr, nArg int32, enc Tu8, createFlag Tu8) (r uintptr) {
	var bestScore, h, nName, score, score1 int32
	var p, pBest, pOther, z, v1 uintptr
	var v2 bool
	_, _, _, _, _, _, _, _, _, _, _ = bestScore, h, nName, p, pBest, pOther, score, score1, z, v1, v2 /* Iterator variable */
	pBest = uintptr(0)                                                                                /* Best match found so far */
	bestScore = 0                                                                                     /* Length of the name */
	nName = _sqlite3Strlen30(tls, zName)
	/* First search for a match amongst the application-defined functions.
	 */
	p = _sqlite3HashFind(tls, db+432, zName)
	for p != 0 {
		score = _matchQuality(tls, p, nArg, enc)
		if score > bestScore {
			pBest = p
			bestScore = score
		}
		p = (*TFuncDef)(unsafe.Pointer(p)).FpNext
	}
	/* If no match is found, search the built-in functions.
	 **
	 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
	 ** functions even if a prior app-defined function was found.  And give
	 ** priority to built-in functions.
	 **
	 ** Except, if createFlag is true, that means that we are trying to
	 ** install a new function.  Whatever FuncDef structure is returned it will
	 ** have fields overwritten with new information appropriate for the
	 ** new function.  But the FuncDefs for built-in functions are read-only.
	 ** So we must not search for built-ins when creating a new function.
	 */
	if !(createFlag != 0) && (pBest == uintptr(0) || (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_PreferBuiltin) != uint32(0)) {
		bestScore = 0
		h = (int32(_sqlite3UpperToLower[uint8(**(**int8)(__ccgo_up(zName)))]) + nName) % int32(SQLITE_FUNC_HASH_SZ)
		p = _sqlite3FunctionSearch(tls, h, zName)
		for p != 0 {
			score1 = _matchQuality(tls, p, nArg, enc)
			if score1 > bestScore {
				pBest = p
				bestScore = score1
			}
			p = (*TFuncDef)(unsafe.Pointer(p)).FpNext
		}
	}
	/* If the createFlag parameter is true and the search did not reveal an
	 ** exact match for the name, number of arguments and encoding, then add a
	 ** new entry to the hash table and return it.
	 */
	if v2 = createFlag != 0 && bestScore < int32(FUNC_PERFECT_MATCH); v2 {
		v1 = _sqlite3DbMallocZero(tls, db, uint64(uint32(40)+uint32(nName)+uint32(1)))
		pBest = v1
	}
	if v2 && v1 != uintptr(0) {
		(*TFuncDef)(unsafe.Pointer(pBest)).FzName = pBest + 1*40
		(*TFuncDef)(unsafe.Pointer(pBest)).FnArg = int16(uint16(nArg))
		(*TFuncDef)(unsafe.Pointer(pBest)).FfuncFlags = uint32(enc)
		libc.Xmemcpy(tls, pBest+1*40, zName, uint32(nName+int32(1)))
		z = (*TFuncDef)(unsafe.Pointer(pBest)).FzName
		for {
			if !(**(**Tu8)(__ccgo_up(z)) != 0) {
				break
			}
			**(**Tu8)(__ccgo_up(z)) = _sqlite3UpperToLower[**(**Tu8)(__ccgo_up(z))]
			goto _3
		_3:
			;
			z = z + 1
		}
		pOther = _sqlite3HashInsert(tls, db+432, (*TFuncDef)(unsafe.Pointer(pBest)).FzName, pBest)
		if pOther == pBest {
			_sqlite3DbFree(tls, db, pBest)
			_sqlite3OomFault(tls, db)
			return uintptr(0)
		} else {
			(*TFuncDef)(unsafe.Pointer(pBest)).FpNext = pOther
		}
	}
	if pBest != 0 && ((*TFuncDef)(unsafe.Pointer(pBest)).FxSFunc != 0 || createFlag != 0) {
		return pBest
	}
	return uintptr(0)
}

// C documentation
//
//	/*
//	** This function is used by the implementation of the IN (...) operator.
//	** The pX parameter is the expression on the RHS of the IN operator, which
//	** might be either a list of expressions or a subquery.
//	**
//	** The job of this routine is to find or create a b-tree object that can
//	** be used either to test for membership in the RHS set or to iterate through
//	** all members of the RHS set, skipping duplicates.
//	**
//	** A cursor is opened on the b-tree object that is the RHS of the IN operator
//	** and the *piTab parameter is set to the index of that cursor.
//	**
//	** The returned value of this function indicates the b-tree type, as follows:
//	**
//	**   IN_INDEX_ROWID      - The cursor was opened on a database table.
//	**   IN_INDEX_INDEX_ASC  - The cursor was opened on an ascending index.
//	**   IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
//	**   IN_INDEX_EPH        - The cursor was opened on a specially created and
//	**                         populated ephemeral table.
//	**   IN_INDEX_NOOP       - No cursor was allocated.  The IN operator must be
//	**                         implemented as a sequence of comparisons.
//	**
//	** An existing b-tree might be used if the RHS expression pX is a simple
//	** subquery such as:
//	**
//	**     SELECT <column1>, <column2>... FROM <table>
//	**
//	** If the RHS of the IN operator is a list or a more complex subquery, then
//	** an ephemeral table might need to be generated from the RHS and then
//	** pX->iTable made to point to the ephemeral table instead of an
//	** existing table.  In this case, the creation and initialization of the
//	** ephemeral table might be put inside of a subroutine, the EP_Subrtn flag
//	** will be set on pX and the pX->y.sub fields will be set to show where
//	** the subroutine is coded.
//	**
//	** The inFlags parameter must contain, at a minimum, one of the bits
//	** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both.  If inFlags contains
//	** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
//	** membership test.  When the IN_INDEX_LOOP bit is set, the IN index will
//	** be used to loop over all values of the RHS of the IN operator.
//	**
//	** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
//	** through the set members) then the b-tree must not contain duplicates.
//	** An ephemeral table will be created unless the selected columns are guaranteed
//	** to be unique - either because it is an INTEGER PRIMARY KEY or due to
//	** a UNIQUE constraint or index.
//	**
//	** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
//	** for fast set membership tests) then an ephemeral table must
//	** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
//	** index can be found with the specified <columns> as its left-most.
//	**
//	** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
//	** if the RHS of the IN operator is a list (not a subquery) then this
//	** routine might decide that creating an ephemeral b-tree for membership
//	** testing is too expensive and return IN_INDEX_NOOP.  In that case, the
//	** calling routine should implement the IN operator using a sequence
//	** of Eq or Ne comparison operations.
//	**
//	** When the b-tree is being used for membership tests, the calling function
//	** might need to know whether or not the RHS side of the IN operator
//	** contains a NULL.  If prRhsHasNull is not a NULL pointer and
//	** if there is any chance that the (...) might contain a NULL value at
//	** runtime, then a register is allocated and the register number written
//	** to *prRhsHasNull. If there is no chance that the (...) contains a
//	** NULL value, then *prRhsHasNull is left unchanged.
//	**
//	** If a register is allocated and its location stored in *prRhsHasNull, then
//	** the value in that register will be NULL if the b-tree contains one or more
//	** NULL values, and it will be some non-NULL value if the b-tree contains no
//	** NULL values.
//	**
//	** If the aiMap parameter is not NULL, it must point to an array containing
//	** one element for each column returned by the SELECT statement on the RHS
//	** of the IN(...) operator. The i'th entry of the array is populated with the
//	** offset of the index column that matches the i'th column returned by the
//	** SELECT. For example, if the expression and selected index are:
//	**
//	**   (?,?,?) IN (SELECT a, b, c FROM t1)
//	**   CREATE INDEX i1 ON t1(b, c, a);
//	**
//	** then aiMap[] is populated with {2, 0, 1}.
//	*/
func _sqlite3FindInIndex(tls *libc.TLS, pParse uintptr, pX uintptr, inFlags Tu32, prRhsHasNull uintptr, aiMap uintptr, piTab uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var affinity_ok, bloomOk, eType, i, i1, i2, iAddr, iAddr1, iCol, iDb, iTab, j, mustBeUnique, n, nExpr, rMayHaveNull, v1, v10 int32
	var cmpaff, idxaff int8
	var colUsed, mCol TBitmask
	var db, p, pEList, pEList1, pIdx, pLhs, pLhs1, pReq, pRhs, pTab, v, v2 uintptr
	var savedNQueryLoop Tu32
	var v5 bool
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = affinity_ok, bloomOk, cmpaff, colUsed, db, eType, i, i1, i2, iAddr, iAddr1, iCol, iDb, iTab, idxaff, j, mCol, mustBeUnique, n, nExpr, p, pEList, pEList1, pIdx, pLhs, pLhs1, pReq, pRhs, pTab, rMayHaveNull, savedNQueryLoop, v, v1, v10, v2, v5 /* SELECT to the right of IN operator */
	eType = 0                                                                                                                                                                                                                                                                                                                                                     /* True if RHS must be unique */
	v = _sqlite3GetVdbe(tls, pParse)                                                                                                                                                                                                                                                                                                                              /* Virtual machine being coded */
	mustBeUnique = libc.BoolInt32(inFlags&uint32(IN_INDEX_LOOP) != uint32(0))
	v2 = pParse + 44
	v1 = *(*int32)(unsafe.Pointer(v2))
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	iTab = v1
	/* If the RHS of this IN(...) operator is a SELECT, and if it matters
	 ** whether or not the SELECT result contains NULL values, check whether
	 ** or not NULL is actually possible (it may not be, for example, due
	 ** to NOT NULL constraints in the schema). If no NULL values are possible,
	 ** set prRhsHasNull to 0 before continuing.  */
	if prRhsHasNull != 0 && (*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
		pEList = (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pX + 20)))).FpEList
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
				break
			}
			if _sqlite3ExprCanBeNull(tls, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr) != 0 {
				break
			}
			goto _3
		_3:
			;
			i = i + 1
		}
		if i == (*TExprList)(unsafe.Pointer(pEList)).FnExpr {
			prRhsHasNull = uintptr(0)
		}
	}
	/* Check to see if an existing table or index can be used to
	 ** satisfy the query.  This is preferable to generating a new
	 ** ephemeral table.  */
	if v5 = (*TParse)(unsafe.Pointer(pParse)).FnErr == 0; v5 {
		v2 = _isCandidateForInOpt(tls, pX)
		p = v2
	}
	if v5 && v2 != uintptr(0) {
		db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Database idx for pTab */
		pEList1 = (*TSelect)(unsafe.Pointer(p)).FpEList
		nExpr = (*TExprList)(unsafe.Pointer(pEList1)).FnExpr
		/* Because of isCandidateForInOpt(p) */
		/* Because of isCandidateForInOpt(p) */
		/* Because of isCandidateForInOpt(p) */
		pTab = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSTab
		/* Code an OP_Transaction and OP_TableLock for <table>. */
		iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
		_sqlite3CodeVerifySchema(tls, pParse, iDb)
		_sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab)).FzName)
		/* sqlite3GetVdbe() has always been previously called */
		if nExpr == int32(1) && int32((*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pEList1 + 8))).FpExpr)).FiColumn) < 0 {
			/* The "x IN (SELECT rowid FROM table)" case */
			iAddr = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
			_sqlite3OpenTable(tls, pParse, iTab, iDb, pTab, int32(OP_OpenRead))
			eType = int32(IN_INDEX_ROWID)
			_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+9424, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
			_sqlite3VdbeJumpHere(tls, v, iAddr)
		} else { /* Iterator variable */
			affinity_ok = int32(1)
			/* Check that the affinity that will be used to perform each
			 ** comparison is the same as the affinity of each column in table
			 ** on the RHS of the IN operator.  If it not, it is not possible to
			 ** use any index of the RHS table.  */
			i1 = 0
			for {
				if !(i1 < nExpr && affinity_ok != 0) {
					break
				}
				pLhs = _sqlite3VectorFieldSubexpr(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft, i1)
				iCol = int32((*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pEList1 + 8 + uintptr(i1)*20))).FpExpr)).FiColumn)
				idxaff = _sqlite3TableColumnAffinity(tls, pTab, iCol) /* RHS table */
				cmpaff = _sqlite3CompareAffinity(tls, pLhs, idxaff)
				switch int32(cmpaff) {
				case int32(SQLITE_AFF_BLOB):
				case int32(SQLITE_AFF_TEXT):
					/* sqlite3CompareAffinity() only returns TEXT if one side or the
					 ** other has no affinity and the other side is TEXT.  Hence,
					 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
					 ** and for the term on the LHS of the IN to have no affinity. */
				default:
					affinity_ok = libc.BoolInt32(int32(idxaff) >= int32(SQLITE_AFF_NUMERIC))
				}
				goto _6
			_6:
				;
				i1 = i1 + 1
			}
			if affinity_ok != 0 {
				/* Search for an existing index that will work for this IN operator */
				pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
				for {
					if !(pIdx != 0 && eType == 0) {
						break
					} /* Mask for the current column */
					if int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) < nExpr {
						goto _7
					}
					if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != uintptr(0) {
						goto _7
					}
					/* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
					 ** BITMASK(nExpr) without overflowing */
					if int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) {
						goto _7
					}
					if mustBeUnique != 0 {
						if int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) > nExpr || int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) > nExpr && !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != libc.Int32FromInt32(OE_None)) {
							goto _7 /* This index is not unique over the IN RHS columns */
						}
					}
					colUsed = uint64(0) /* Columns of index used so far */
					i1 = 0
					for {
						if !(i1 < nExpr) {
							break
						}
						pLhs1 = _sqlite3VectorFieldSubexpr(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft, i1)
						pRhs = (*(*TExprList_item)(unsafe.Pointer(pEList1 + 8 + uintptr(i1)*20))).FpExpr
						pReq = _sqlite3BinaryCompareCollSeq(tls, pParse, pLhs1, pRhs)
						j = 0
						for {
							if !(j < nExpr) {
								break
							}
							if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2))) != int32((*TExpr)(unsafe.Pointer(pRhs)).FiColumn) {
								goto _9
							}
							if pReq != uintptr(0) && _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pReq)).FzName, **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*4))) != 0 {
								goto _9
							}
							break
							goto _9
						_9:
							;
							j = j + 1
						}
						if j == nExpr {
							break
						}
						mCol = libc.Uint64FromInt32(1) << j
						if mCol&colUsed != 0 {
							break
						} /* Each column used only once */
						colUsed = colUsed | mCol
						if aiMap != 0 {
							**(**int32)(__ccgo_up(aiMap + uintptr(i1)*4)) = j
						}
						goto _8
					_8:
						;
						i1 = i1 + 1
					}
					if colUsed == libc.Uint64FromInt32(1)<<nExpr-uint64(1) {
						/* If we reach this point, that means the index pIdx is usable */
						iAddr1 = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
						_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+9471, libc.VaList(bp+8, (*TIndex)(unsafe.Pointer(pIdx)).FzName))
						_sqlite3VdbeAddOp3(tls, v, int32(OP_OpenRead), iTab, int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb)
						_sqlite3VdbeSetP4KeyInfo(tls, pParse, pIdx)
						eType = int32(IN_INDEX_INDEX_ASC) + int32(**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder)))
						if prRhsHasNull != 0 {
							v2 = pParse + 48
							*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
							v1 = *(*int32)(unsafe.Pointer(v2))
							**(**int32)(__ccgo_up(prRhsHasNull)) = v1
							if nExpr == int32(1) {
								_sqlite3SetHasNullFlag(tls, v, iTab, **(**int32)(__ccgo_up(prRhsHasNull)))
							}
						}
						_sqlite3VdbeJumpHere(tls, v, iAddr1)
					}
					goto _7
				_7:
					;
					pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
				} /* End loop over indexes */
			} /* End if( affinity_ok ) */
		} /* End if not an rowid index */
	} /* End attempt to optimize using an index */
	/* If no preexisting index is available for the IN clause
	 ** and IN_INDEX_NOOP is an allowed reply
	 ** and the RHS of the IN operator is a list, not a subquery
	 ** and the RHS is not constant or has two or fewer terms,
	 ** then it is not worth creating an ephemeral table to evaluate
	 ** the IN operator so return IN_INDEX_NOOP.
	 */
	if eType == 0 && inFlags&uint32(IN_INDEX_NOOP_OK) != 0 && (*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(EP_xIsSelect) == uint32(0) && (!(_sqlite3InRhsIsConstant(tls, pParse, pX) != 0) || (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pX + 20)))).FnExpr <= int32(2)) {
		(*TParse)(unsafe.Pointer(pParse)).FnTab = (*TParse)(unsafe.Pointer(pParse)).FnTab - 1 /* Back out the allocation of the unused cursor */
		iTab = -int32(1)                                                                      /* Cursor is not allocated */
		eType = int32(IN_INDEX_NOOP)
	}
	if eType == 0 {
		/* Could not find an existing table or index to use as the RHS b-tree.
		 ** We will have to generate an ephemeral table to do the job.
		 */
		savedNQueryLoop = uint32((*TParse)(unsafe.Pointer(pParse)).FnQueryLoop)
		rMayHaveNull = 0
		bloomOk = libc.BoolInt32(inFlags&uint32(IN_INDEX_MEMBERSHIP) != uint32(0))
		eType = int32(IN_INDEX_EPH)
		if inFlags&uint32(IN_INDEX_LOOP) != 0 {
			(*TParse)(unsafe.Pointer(pParse)).FnQueryLoop = 0
		} else {
			if prRhsHasNull != 0 {
				v2 = pParse + 48
				*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
				v10 = *(*int32)(unsafe.Pointer(v2))
				v1 = v10
				rMayHaveNull = v1
				**(**int32)(__ccgo_up(prRhsHasNull)) = v1
			}
		}
		if !(bloomOk != 0) && (*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(EP_xIsSelect) != uint32(0) && (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pX + 20)))).FselFlags&uint32(SF_ClonedRhsIn) != uint32(0) {
			bloomOk = int32(1)
		}
		_sqlite3CodeRhsOfIN(tls, pParse, pX, iTab, bloomOk)
		if rMayHaveNull != 0 {
			_sqlite3SetHasNullFlag(tls, v, iTab, rMayHaveNull)
		}
		(*TParse)(unsafe.Pointer(pParse)).FnQueryLoop = int16(savedNQueryLoop)
	}
	if aiMap != 0 && eType != int32(IN_INDEX_INDEX_ASC) && eType != int32(IN_INDEX_INDEX_DESC) {
		n = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft)
		i2 = 0
		for {
			if !(i2 < n) {
				break
			}
			**(**int32)(__ccgo_up(aiMap + uintptr(i2)*4)) = i2
			goto _15
		_15:
			;
			i2 = i2 + 1
		}
	}
	**(**int32)(__ccgo_up(piTab)) = iTab
	return eType
}

// C documentation
//
//	/*
//	** Locate the in-memory structure that describes a particular database
//	** table given the name of that table and (optionally) the name of the
//	** database containing the table.  Return NULL if not found.
//	**
//	** If zDatabase is 0, all databases are searched for the table and the
//	** first matching table is returned.  (No checking for duplicate table
//	** names is done.)  The search order is TEMP first, then MAIN, then any
//	** auxiliary databases added using the ATTACH command.
//	**
//	** See also sqlite3LocateTable().
//	*/
func _sqlite3FindTable(tls *libc.TLS, db uintptr, zName uintptr, zDatabase uintptr) (r uintptr) {
	var i int32
	var p uintptr
	_, _ = i, p
	p = uintptr(0)
	/* All mutexes are required for schema access.  Make sure we hold them. */
	if zDatabase != 0 {
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			if _sqlite3StrICmp(tls, zDatabase, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FzDbSName) == 0 {
				break
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if i >= (*Tsqlite3)(unsafe.Pointer(db)).FnDb {
			/* No match against the official names.  But always match "main"
			 ** to schema 0 as a legacy fallback. */
			if _sqlite3StrICmp(tls, zDatabase, __ccgo_ts+8033) == 0 {
				i = 0
			} else {
				return uintptr(0)
			}
		}
		p = _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema+8, zName)
		if p == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+7973, int32(7)) == 0 {
			if i == int32(1) {
				if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+8000+7) == 0 || _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+8019+7) == 0 || _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+7501+7) == 0 {
					p = _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema+8, __ccgo_ts+7981)
				}
			} else {
				if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+8019+7) == 0 {
					p = _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema+8, __ccgo_ts+7501)
				}
			}
		}
	} else {
		/* Match against TEMP first */
		p = _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema+8, zName)
		if p != 0 {
			return p
		}
		/* The main database is second */
		p = _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema+8, zName)
		if p != 0 {
			return p
		}
		/* Attached databases are in order of attachment */
		i = int32(2)
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			p = _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema+8, zName)
			if p != 0 {
				break
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		if p == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+7973, int32(7)) == 0 {
			if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+8019+7) == 0 {
				p = _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema+8, __ccgo_ts+7501)
			} else {
				if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+8000+7) == 0 {
					p = _sqlite3HashFind(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema+8, __ccgo_ts+7981)
				}
			}
		}
	}
	return p
}

// C documentation
//
//	/*
//	** This routine is called after a single SQL statement has been
//	** parsed and a VDBE program to execute that statement has been
//	** prepared.  This routine puts the finishing touches on the
//	** VDBE program and resets the pParse structure for the next
//	** parse.
//	**
//	** Note that if an error occurred, it might be the case that
//	** no VDBE code was generated.
//	*/
func _sqlite3FinishCoding(tls *libc.TLS, pParse uintptr) {
	var addrRewind, i, iDb, reg, v2 int32
	var db, pEL, pRet, pReturning, pSchema, v, vtab uintptr
	_, _, _, _, _, _, _, _, _, _, _, _ = addrRewind, db, i, iDb, pEL, pRet, pReturning, pSchema, reg, v, vtab, v2
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*TParse)(unsafe.Pointer(pParse)).Fnested != 0 {
		return
	}
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM)
		}
		return
	}
	/* Begin by generating some termination code at the end of the
	 ** vdbe program
	 */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	if v == uintptr(0) {
		if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 {
			(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_DONE)
			return
		}
		v = _sqlite3GetVdbe(tls, pParse)
		if v == uintptr(0) {
			(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_ERROR)
		}
	}
	if v != 0 {
		if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x8>>3)) != 0 {
			pReturning = (*(*struct {
				FpReturning uintptr
			})(unsafe.Pointer(&(*TParse)(unsafe.Pointer(pParse)).Fu1))).FpReturning
			if (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol != 0 {
				_sqlite3VdbeAddOp0(tls, v, int32(OP_FkCheck))
				addrRewind = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur)
				reg = (*TReturning)(unsafe.Pointer(pReturning)).FiRetReg
				i = 0
				for {
					if !(i < (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol) {
						break
					}
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur, i, reg+i)
					goto _1
				_1:
					;
					i = i + 1
				}
				_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), reg, i)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur, addrRewind+int32(1))
				_sqlite3VdbeJumpHere(tls, v, addrRewind)
			}
		}
		_sqlite3VdbeAddOp0(tls, v, int32(OP_Halt))
		/* The cookie mask contains one bit for each database file open.
		 ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
		 ** set for each database that is used.  Generate code to start a
		 ** transaction on each used database and to verify the schema cookie
		 ** on each used database.
		 */
		_sqlite3VdbeJumpHere(tls, v, 0)
		iDb = 0
		for {
			if libc.BoolInt32((*TParse)(unsafe.Pointer(pParse)).FcookieMask&(libc.Uint32FromInt32(1)<<iDb) != uint32(0)) == 0 {
				goto _3
			}
			_sqlite3VdbeUsesBtree(tls, v, iDb)
			pSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_Transaction), iDb, libc.BoolInt32((*TParse)(unsafe.Pointer(pParse)).FwriteMask&(libc.Uint32FromInt32(1)<<iDb) != uint32(0)), (*TSchema)(unsafe.Pointer(pSchema)).Fschema_cookie, (*TSchema)(unsafe.Pointer(pSchema)).FiGeneration)
			if int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 {
				_sqlite3VdbeChangeP5(tls, v, uint16(1))
			}
			goto _3
		_3:
			;
			iDb = iDb + 1
			v2 = iDb
			if !(v2 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
		}
		i = 0
		for {
			if !(i < (*TParse)(unsafe.Pointer(pParse)).FnVtabLock) {
				break
			}
			vtab = _sqlite3GetVTable(tls, db, **(**uintptr)(__ccgo_up((*TParse)(unsafe.Pointer(pParse)).FapVtabLock + uintptr(i)*4)))
			_sqlite3VdbeAddOp4(tls, v, int32(OP_VBegin), 0, 0, 0, vtab, -int32(12))
			goto _4
		_4:
			;
			i = i + 1
		}
		(*TParse)(unsafe.Pointer(pParse)).FnVtabLock = 0
		/* Once all the cookies have been verified and transactions opened,
		 ** obtain the required table-locks. This is a no-op unless the
		 ** shared-cache feature is enabled.
		 */
		if (*TParse)(unsafe.Pointer(pParse)).FnTableLock != 0 {
			_codeTableLocks(tls, pParse)
		}
		/* Initialize any AUTOINCREMENT data structures required.
		 */
		if (*TParse)(unsafe.Pointer(pParse)).FpAinc != 0 {
			_sqlite3AutoincrementBegin(tls, pParse)
		}
		/* Code constant expressions that were factored out of inner loops.
		 */
		if (*TParse)(unsafe.Pointer(pParse)).FpConstExpr != 0 {
			pEL = (*TParse)(unsafe.Pointer(pParse)).FpConstExpr
			libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(0), 7, 0x80)
			i = 0
			for {
				if !(i < (*TExprList)(unsafe.Pointer(pEL)).FnExpr) {
					break
				}
				_sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pEL + 8 + uintptr(i)*20))).FpExpr, *(*int32)(unsafe.Pointer(pEL + 8 + uintptr(i)*20 + 16)))
				goto _5
			_5:
				;
				i = i + 1
			}
		}
		if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x8>>3)) != 0 {
			pRet = (*(*struct {
				FpReturning uintptr
			})(unsafe.Pointer(&(*TParse)(unsafe.Pointer(pParse)).Fu1))).FpReturning
			if (*TReturning)(unsafe.Pointer(pRet)).FnRetCol != 0 {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TReturning)(unsafe.Pointer(pRet)).FiRetCur, (*TReturning)(unsafe.Pointer(pRet)).FnRetCol)
			}
		}
		/* Finally, jump back to the beginning of the executable code. */
		_sqlite3VdbeGoto(tls, v, int32(1))
	}
	/* Get the VDBE program ready for execution
	 */
	if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
		/* A minimum of one cursor is required if autoincrement is used
		 *  See ticket [a696379c1f08866] */
		_sqlite3VdbeMakeReady(tls, v, pParse)
		(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_DONE)
	} else {
		(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_ERROR)
	}
}

// C documentation
//
//	/*
//	** This routine is called after all of the trigger actions have been parsed
//	** in order to complete the process of building the trigger.
//	*/
func _sqlite3FinishTrigger(tls *libc.TLS, pParse uintptr, pStepList uintptr, pAll uintptr) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var db, pHash, pLink, pStep, pTab, pTrig, v, z, zName uintptr
	var iDb int32
	var _ /* nameToken at bp+56 */ TToken
	var _ /* sFix at bp+0 */ TDbFixer
	_, _, _, _, _, _, _, _, _, _ = db, iDb, pHash, pLink, pStep, pTab, pTrig, v, z, zName
	pTrig = (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger /* Name of trigger */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb             /* Trigger name for error reporting */
	(*TParse)(unsafe.Pointer(pParse)).FpNewTrigger = uintptr(0)
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 || !(pTrig != 0) {
		goto triggerfinish_cleanup
	}
	zName = (*TTrigger)(unsafe.Pointer(pTrig)).FzName
	iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTrigger)(unsafe.Pointer(pTrig)).FpSchema)
	(*TTrigger)(unsafe.Pointer(pTrig)).Fstep_list = pStepList
	for pStepList != 0 {
		(*TTriggerStep)(unsafe.Pointer(pStepList)).FpTrig = pTrig
		pStepList = (*TTriggerStep)(unsafe.Pointer(pStepList)).FpNext
	}
	_sqlite3TokenInit(tls, bp+56, (*TTrigger)(unsafe.Pointer(pTrig)).FzName)
	_sqlite3FixInit(tls, bp, pParse, iDb, __ccgo_ts+23116, bp+56)
	if _sqlite3FixTriggerStep(tls, bp, (*TTrigger)(unsafe.Pointer(pTrig)).Fstep_list) != 0 || _sqlite3FixExpr(tls, bp, (*TTrigger)(unsafe.Pointer(pTrig)).FpWhen) != 0 {
		goto triggerfinish_cleanup
	}
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		(*TParse)(unsafe.Pointer(pParse)).FpNewTrigger = pTrig
		pTrig = uintptr(0)
	} else {
		/* if we are not initializing,
		 ** build the sqlite_schema entry
		 */
		if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) {
			/* If this is a new CREATE TABLE statement, and if shadow tables
			 ** are read-only, and the trigger makes a change to a shadow table,
			 ** then raise an error - do not allow the trigger to be created. */
			if _sqlite3ReadOnlyShadowTables(tls, db) != 0 {
				pStep = (*TTrigger)(unsafe.Pointer(pTrig)).Fstep_list
				for {
					if !(pStep != 0) {
						break
					}
					if (*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc != uintptr(0) && _sqlite3ShadowTableName(tls, db, (*(*TSrcItem)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc + 8))).FzName) != 0 {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23365, libc.VaList(bp+72, (*TTrigger)(unsafe.Pointer(pTrig)).FzName, (*(*TSrcItem)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pStep)).FpSrc + 8))).FzName))
						goto triggerfinish_cleanup
					}
					goto _1
				_1:
					;
					pStep = (*TTriggerStep)(unsafe.Pointer(pStep)).FpNext
				}
			}
			/* Make an entry in the sqlite_schema table */
			v = _sqlite3GetVdbe(tls, pParse)
			if v == uintptr(0) {
				goto triggerfinish_cleanup
			}
			_sqlite3BeginWriteOperation(tls, pParse, 0, iDb)
			z = _sqlite3DbStrNDup(tls, db, (*TToken)(unsafe.Pointer(pAll)).Fz, uint64((*TToken)(unsafe.Pointer(pAll)).Fn))
			_sqlite3NestedParse(tls, pParse, __ccgo_ts+23413, libc.VaList(bp+72, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, zName, (*TTrigger)(unsafe.Pointer(pTrig)).Ftable, z))
			_sqlite3DbFree(tls, db, z)
			_sqlite3ChangeCookie(tls, pParse, iDb)
			_sqlite3VdbeAddParseSchemaOp(tls, v, iDb, _sqlite3MPrintf(tls, db, __ccgo_ts+23488, libc.VaList(bp+72, zName)), uint16(0))
		}
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 {
		pLink = pTrig
		pHash = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 40
		pTrig = _sqlite3HashInsert(tls, pHash, zName, pTrig)
		if pTrig != 0 {
			_sqlite3OomFault(tls, db)
		} else {
			if (*TTrigger)(unsafe.Pointer(pLink)).FpSchema == (*TTrigger)(unsafe.Pointer(pLink)).FpTabSchema {
				pTab = _sqlite3HashFind(tls, (*TTrigger)(unsafe.Pointer(pLink)).FpTabSchema+8, (*TTrigger)(unsafe.Pointer(pLink)).Ftable)
				(*TTrigger)(unsafe.Pointer(pLink)).FpNext = (*TTable)(unsafe.Pointer(pTab)).FpTrigger
				(*TTable)(unsafe.Pointer(pTab)).FpTrigger = pLink
			}
		}
	}
	goto triggerfinish_cleanup
triggerfinish_cleanup:
	;
	_sqlite3DeleteTrigger(tls, db, pTrig)
	_sqlite3DeleteTriggerStep(tls, db, pStepList)
}

// C documentation
//
//	/*
//	** This function is called when inserting, deleting or updating a row of
//	** table pTab to generate VDBE code to perform foreign key constraint
//	** processing for the operation.
//	**
//	** For a DELETE operation, parameter regOld is passed the index of the
//	** first register in an array of (pTab->nCol+1) registers containing the
//	** rowid of the row being deleted, followed by each of the column values
//	** of the row being deleted, from left to right. Parameter regNew is passed
//	** zero in this case.
//	**
//	** For an INSERT operation, regOld is passed zero and regNew is passed the
//	** first register of an array of (pTab->nCol+1) registers containing the new
//	** row data.
//	**
//	** For an UPDATE operation, this function is called twice. Once before
//	** the original record is deleted from the table using the calling convention
//	** described for DELETE. Then again after the original record is deleted
//	** but before the new record is inserted using the INSERT convention.
//	*/
func _sqlite3FkCheck(tls *libc.TLS, pParse uintptr, pTab uintptr, regOld int32, regNew int32, aChange uintptr, bChngRowid int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aiCol, db, pFKey, pItem, pSrc, pTo, v, zCol, zDb, v7 uintptr
	var bIgnore, eAction, i, iDb, iFromCol, iJump, iReg, isIgnoreErrors, rcauth, v4 int32
	var _ /* aiCol at bp+16 */ uintptr
	var _ /* aiFree at bp+4 */ uintptr
	var _ /* iCol at bp+8 */ int32
	var _ /* pIdx at bp+0 */ uintptr
	var _ /* pIdx at bp+12 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aiCol, bIgnore, db, eAction, i, iDb, iFromCol, iJump, iReg, isIgnoreErrors, pFKey, pItem, pSrc, pTo, rcauth, v, zCol, zDb, v4, v7
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Name of database containing pTab */
	isIgnoreErrors = int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28)) & 0x1 >> 0))
	/* Exactly one of regOld and regNew should be non-zero. */
	/* If foreign-keys are disabled, this function is a no-op. */
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) == uint64(0) {
		return
	}
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
		return
	}
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	/* Loop through all the foreign key constraints for which pTab is the
	 ** child table (the table that the foreign key definition is part of).  */
	pFKey = (*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pTab + 44))).FpFKey
	for {
		if !(pFKey != 0) {
			break
		} /* Parent table of foreign key pFKey */
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* Index on key columns in pTo */
		**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
		bIgnore = 0
		if aChange != 0 && Xsqlite3_stricmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, (*TFKey)(unsafe.Pointer(pFKey)).FzTo) != 0 && _fkChildIsModified(tls, pTab, pFKey, aChange, bChngRowid) == 0 {
			goto _1
		}
		/* Find the parent table of this foreign key. Also find a unique index
		 ** on the parent key columns in the parent table. If either of these
		 ** schema items cannot be located, set an error in pParse and return
		 ** early.  */
		if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x1>>0)) != 0 {
			pTo = _sqlite3FindTable(tls, db, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zDb)
		} else {
			pTo = _sqlite3LocateTable(tls, pParse, uint32(0), (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zDb)
		}
		if !(pTo != 0) || _sqlite3FkLocateIndex(tls, pParse, pTo, pFKey, bp, bp+4) != 0 {
			if !(isIgnoreErrors != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				return
			}
			if pTo == uintptr(0) {
				/* If isIgnoreErrors is true, then a table is being dropped. In this
				 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
				 ** before actually dropping it in order to check FK constraints.
				 ** If the parent table of an FK constraint on the current table is
				 ** missing, behave as if it is empty. i.e. decrement the relevant
				 ** FK counter for each row of the current table with non-NULL keys.
				 */
				v = _sqlite3GetVdbe(tls, pParse)
				iJump = _sqlite3VdbeCurrentAddr(tls, v) + (*TFKey)(unsafe.Pointer(pFKey)).FnCol + int32(1)
				i = 0
				for {
					if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) {
						break
					}
					iFromCol = (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FiFrom
					iReg = int32(_sqlite3TableColumnToStorage(tls, (*TFKey)(unsafe.Pointer(pFKey)).FpFrom, int16(iFromCol))) + regOld + int32(1)
					_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), iReg, iJump)
					goto _2
				_2:
					;
					i = i + 1
				}
				_sqlite3VdbeAddOp2(tls, v, int32(OP_FkCounter), int32((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred), -int32(1))
			}
			goto _1
		}
		if **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
			aiCol = **(**uintptr)(__ccgo_up(bp + 4))
		} else {
			**(**int32)(__ccgo_up(bp + 8)) = (*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FiFrom
			aiCol = bp + 8
		}
		i = 0
		for {
			if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) {
				break
			}
			if **(**int32)(__ccgo_up(aiCol + uintptr(i)*4)) == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
				**(**int32)(__ccgo_up(aiCol + uintptr(i)*4)) = -int32(1)
			}
			/* Request permission to read the parent key columns. If the
			 ** authorization callback returns SQLITE_IGNORE, behave as if any
			 ** values read from the parent table are NULL. */
			if (*Tsqlite3)(unsafe.Pointer(db)).FxAuth != 0 {
				if **(**uintptr)(__ccgo_up(bp)) != 0 {
					v4 = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaiColumn + uintptr(i)*2)))
				} else {
					v4 = int32((*TTable)(unsafe.Pointer(pTo)).FiPKey)
				}
				zCol = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTo)).FaCol + uintptr(v4)*12))).FzCnName
				rcauth = _sqlite3AuthReadCol(tls, pParse, (*TTable)(unsafe.Pointer(pTo)).FzName, zCol, iDb)
				bIgnore = libc.BoolInt32(rcauth == int32(SQLITE_IGNORE))
			}
			goto _3
		_3:
			;
			i = i + 1
		}
		/* Take a shared-cache advisory read-lock on the parent table. Allocate
		 ** a cursor to use to search the unique index on the parent key columns
		 ** in the parent table.  */
		_sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTo)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTo)).FzName)
		(*TParse)(unsafe.Pointer(pParse)).FnTab = (*TParse)(unsafe.Pointer(pParse)).FnTab + 1
		if regOld != 0 {
			/* A row is being removed from the child table. Search for the parent.
			 ** If the parent does not exist, removing the child row resolves an
			 ** outstanding foreign key constraint violation. */
			_fkLookupParent(tls, pParse, iDb, pTo, **(**uintptr)(__ccgo_up(bp)), pFKey, aiCol, regOld, -int32(1), bIgnore)
		}
		if regNew != 0 && !(_isSetNullAction(tls, pParse, pFKey) != 0) {
			/* A row is being added to the child table. If a parent row cannot
			 ** be found, adding the child row has violated the FK constraint.
			 **
			 ** If this operation is being performed as part of a trigger program
			 ** that is actually a "SET NULL" action belonging to this very
			 ** foreign key, then omit this scan altogether. As all child key
			 ** values are guaranteed to be NULL, it is not possible for adding
			 ** this row to cause an FK violation.  */
			_fkLookupParent(tls, pParse, iDb, pTo, **(**uintptr)(__ccgo_up(bp)), pFKey, aiCol, regNew, +libc.Int32FromInt32(1), bIgnore)
		}
		_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp + 4)))
		goto _1
	_1:
		;
		pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom
	}
	/* Loop through all the foreign key constraints that refer to this table.
	 ** (the "child" constraints) */
	pFKey = _sqlite3FkReferences(tls, pTab)
	for {
		if !(pFKey != 0) {
			break
		}
		**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
		**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)
		if aChange != 0 && _fkParentIsModified(tls, pTab, pFKey, aChange, bChngRowid) == 0 {
			goto _5
		}
		if !((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred != 0) && !((*Tsqlite3)(unsafe.Pointer(db)).Fflags&libc.Uint64FromInt32(SQLITE_DeferFKs) != 0) && !((*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0) && !((*TParse)(unsafe.Pointer(pParse)).FisMultiWrite != 0) {
			/* Inserting a single row into a parent table cannot cause (or fix)
			 ** an immediate foreign key violation. So do nothing in this case.  */
			goto _5
		}
		if _sqlite3FkLocateIndex(tls, pParse, pTab, pFKey, bp+12, bp+16) != 0 {
			if !(isIgnoreErrors != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				return
			}
			goto _5
		}
		/* Create a SrcList structure containing the child table.  We need the
		 ** child table as a SrcList for sqlite3WhereBegin() */
		pSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0))
		if pSrc != 0 {
			pItem = pSrc + 8
			(*TSrcItem)(unsafe.Pointer(pItem)).FpSTab = (*TFKey)(unsafe.Pointer(pFKey)).FpFrom
			(*TSrcItem)(unsafe.Pointer(pItem)).FzName = (*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FzName
			(*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSTab)).FnTabRef = (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSTab)).FnTabRef + 1
			v7 = pParse + 44
			v4 = *(*int32)(unsafe.Pointer(v7))
			*(*int32)(unsafe.Pointer(v7)) = *(*int32)(unsafe.Pointer(v7)) + 1
			(*TSrcItem)(unsafe.Pointer(pItem)).FiCursor = v4
			if regNew != 0 {
				_fkScanChildren(tls, pParse, pSrc, pTab, **(**uintptr)(__ccgo_up(bp + 12)), pFKey, **(**uintptr)(__ccgo_up(bp + 16)), regNew, -int32(1))
			}
			if regOld != 0 {
				eAction = int32(**(**Tu8)(__ccgo_up(pFKey + 25 + libc.BoolUintptr(aChange != uintptr(0)))))
				if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00008))<<libc.Int32FromInt32(32)) != 0 {
					eAction = OE_None
				}
				_fkScanChildren(tls, pParse, pSrc, pTab, **(**uintptr)(__ccgo_up(bp + 12)), pFKey, **(**uintptr)(__ccgo_up(bp + 16)), regOld, int32(1))
				/* If this is a deferred FK constraint, or a CASCADE or SET NULL
				 ** action applies, then any foreign key violations caused by
				 ** removing the parent key will be rectified by the action trigger.
				 ** So do not set the "may-abort" flag in this case.
				 **
				 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
				 ** may-abort flag will eventually be set on this statement anyway
				 ** (when this function is called as part of processing the UPDATE
				 ** within the action trigger).
				 **
				 ** Note 2: At first glance it may seem like SQLite could simply omit
				 ** all OP_FkCounter related scans when either CASCADE or SET NULL
				 ** applies. The trouble starts if the CASCADE or SET NULL action
				 ** trigger causes other triggers or action rules attached to the
				 ** child table to fire. In these cases the fk constraint counters
				 ** might be set incorrectly if any OP_FkCounter related scans are
				 ** omitted.  */
				if !((*TFKey)(unsafe.Pointer(pFKey)).FisDeferred != 0) && eAction != int32(OE_Cascade) && eAction != int32(OE_SetNull) {
					_sqlite3MayAbort(tls, pParse)
				}
			}
			(*TSrcItem)(unsafe.Pointer(pItem)).FzName = uintptr(0)
			_sqlite3SrcListDelete(tls, db, pSrc)
		}
		_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp + 16)))
		goto _5
	_5:
		;
		pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextTo
	}
}

// C documentation
//
//	/*
//	** Clear the apTrigger[] cache of CASCADE triggers for all foreign keys
//	** in a particular database.  This needs to happen when the schema
//	** changes.
//	*/
func _sqlite3FkClearTriggerCache(tls *libc.TLS, db uintptr, iDb int32) {
	var k, pFKey, pHash, pTab uintptr
	_, _, _, _ = k, pFKey, pHash, pTab
	pHash = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 8
	k = (*THash)(unsafe.Pointer(pHash)).Ffirst
	for {
		if !(k != 0) {
			break
		}
		pTab = (*THashElem)(unsafe.Pointer(k)).Fdata
		if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
			goto _1
		}
		pFKey = (*(*struct {
			FaddColOffset int32
			FpFKey        uintptr
			FpDfltList    uintptr
		})(unsafe.Pointer(pTab + 44))).FpFKey
		for {
			if !(pFKey != 0) {
				break
			}
			_fkTriggerDelete(tls, db, **(**uintptr)(__ccgo_up(pFKey + 28)))
			**(**uintptr)(__ccgo_up(pFKey + 28)) = uintptr(0)
			_fkTriggerDelete(tls, db, **(**uintptr)(__ccgo_up(pFKey + 28 + 1*4)))
			**(**uintptr)(__ccgo_up(pFKey + 28 + 1*4)) = uintptr(0)
			goto _2
		_2:
			;
			pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom
		}
		goto _1
	_1:
		;
		k = (*THashElem)(unsafe.Pointer(k)).Fnext
	}
}

// C documentation
//
//	/*
//	** This function is called to generate code that runs when table pTab is
//	** being dropped from the database. The SrcList passed as the second argument
//	** to this function contains a single entry guaranteed to resolve to
//	** table pTab.
//	**
//	** Normally, no code is required. However, if either
//	**
//	**   (a) The table is the parent table of a FK constraint, or
//	**   (b) The table is the child table of a deferred FK constraint and it is
//	**       determined at runtime that there are outstanding deferred FK
//	**       constraint violations in the database,
//	**
//	** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
//	** the table from the database. Triggers are disabled while running this
//	** DELETE, but foreign key actions are not.
//	*/
func _sqlite3FkDropTable(tls *libc.TLS, pParse uintptr, pName uintptr, pTab uintptr) {
	var db, p, v uintptr
	var iSkip int32
	_, _, _, _ = db, iSkip, p, v
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == TABTYP_NORM {
		iSkip = 0
		v = _sqlite3GetVdbe(tls, pParse)
		/* VDBE has already been allocated */
		if _sqlite3FkReferences(tls, pTab) == uintptr(0) {
			p = (*(*struct {
				FaddColOffset int32
				FpFKey        uintptr
				FpDfltList    uintptr
			})(unsafe.Pointer(pTab + 44))).FpFKey
			for {
				if !(p != 0) {
					break
				}
				if (*TFKey)(unsafe.Pointer(p)).FisDeferred != 0 || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DeferFKs) != 0 {
					break
				}
				goto _1
			_1:
				;
				p = (*TFKey)(unsafe.Pointer(p)).FpNextFrom
			}
			if !(p != 0) {
				return
			}
			iSkip = _sqlite3VdbeMakeLabel(tls, pParse)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_FkIfZero), int32(1), iSkip)
		}
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 0, 0x1)
		_sqlite3DeleteFrom(tls, pParse, _sqlite3SrcListDup(tls, db, pName, 0), uintptr(0), uintptr(0), uintptr(0))
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(0), 0, 0x1)
		/* If the DELETE has generated immediate foreign key constraint
		 ** violations, halt the VDBE and return an error at this point, before
		 ** any modifications to the schema are made. This is because statement
		 ** transactions are not able to rollback schema changes.
		 **
		 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
		 ** the statement transaction will not be rolled back even if FK
		 ** constraints are violated.
		 */
		if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DeferFKs) == uint64(0) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_FkIfZero), 0, _sqlite3VdbeCurrentAddr(tls, v)+int32(2))
			_sqlite3HaltConstraint(tls, pParse, libc.Int32FromInt32(SQLITE_CONSTRAINT)|libc.Int32FromInt32(3)<<libc.Int32FromInt32(8), int32(OE_Abort), uintptr(0), int8(-libc.Int32FromInt32(1)), uint8(P5_ConstraintFK))
		}
		if iSkip != 0 {
			_sqlite3VdbeResolveLabel(tls, v, iSkip)
		}
	}
}

// C documentation
//
//	/*
//	** A foreign key constraint requires that the key columns in the parent
//	** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
//	** Given that pParent is the parent table for foreign key constraint pFKey,
//	** search the schema for a unique index on the parent key columns.
//	**
//	** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
//	** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
//	** is set to point to the unique index.
//	**
//	** If the parent key consists of a single column (the foreign key constraint
//	** is not a composite foreign key), output variable *paiCol is set to NULL.
//	** Otherwise, it is set to point to an allocated array of size N, where
//	** N is the number of columns in the parent key. The first element of the
//	** array is the index of the child table column that is mapped by the FK
//	** constraint to the parent table column stored in the left-most column
//	** of index *ppIdx. The second element of the array is the index of the
//	** child table column that corresponds to the second left-most column of
//	** *ppIdx, and so on.
//	**
//	** If the required index cannot be found, either because:
//	**
//	**   1) The named parent key columns do not exist, or
//	**
//	**   2) The named parent key columns do exist, but are not subject to a
//	**      UNIQUE or PRIMARY KEY constraint, or
//	**
//	**   3) No parent key columns were provided explicitly as part of the
//	**      foreign key definition, and the parent table does not have a
//	**      PRIMARY KEY, or
//	**
//	**   4) No parent key columns were provided explicitly as part of the
//	**      foreign key definition, and the PRIMARY KEY of the parent table
//	**      consists of a different number of columns to the child key in
//	**      the child table.
//	**
//	** then non-zero is returned, and a "foreign key mismatch" error loaded
//	** into pParse. If an OOM error occurs, non-zero is returned and the
//	** pParse->db->mallocFailed flag is set.
//	*/
func _sqlite3FkLocateIndex(tls *libc.TLS, pParse uintptr, pParent uintptr, pFKey uintptr, ppIdx uintptr, paiCol uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aiCol, pIdx, zDfltColl, zIdxCol, zKey uintptr
	var i, i1, j, nCol int32
	var iCol Ti16
	_, _, _, _, _, _, _, _, _, _ = aiCol, i, i1, iCol, j, nCol, pIdx, zDfltColl, zIdxCol, zKey
	pIdx = uintptr(0)                                       /* Value to return via *ppIdx */
	aiCol = uintptr(0)                                      /* Value to return via *paiCol */
	nCol = (*TFKey)(unsafe.Pointer(pFKey)).FnCol            /* Number of columns in parent key */
	zKey = (*(*TsColMap)(unsafe.Pointer(pFKey + 36))).FzCol /* Name of left-most parent key column */
	/* The caller is responsible for zeroing output parameters. */
	/* If this is a non-composite (single column) foreign key, check if it
	 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
	 ** and *paiCol set to zero and return early.
	 **
	 ** Otherwise, for a composite foreign key (more than one column), allocate
	 ** space for the aiCol array (returned via output parameter *paiCol).
	 ** Non-composite foreign keys do not require the aiCol array.
	 */
	if nCol == int32(1) {
		/* The FK maps to the IPK if any of the following are true:
		 **
		 **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
		 **      mapped to the primary key of table pParent, or
		 **   2) The FK is explicitly mapped to a column declared as INTEGER
		 **      PRIMARY KEY.
		 */
		if int32((*TTable)(unsafe.Pointer(pParent)).FiPKey) >= 0 {
			if !(zKey != 0) {
				return 0
			}
			if !(_sqlite3StrICmp(tls, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pParent)).FaCol + uintptr((*TTable)(unsafe.Pointer(pParent)).FiPKey)*12))).FzCnName, zKey) != 0) {
				return 0
			}
		}
	} else {
		if paiCol != 0 {
			aiCol = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(nCol)*uint32(4)))
			if !(aiCol != 0) {
				return int32(1)
			}
			**(**uintptr)(__ccgo_up(paiCol)) = aiCol
		}
	}
	pIdx = (*TTable)(unsafe.Pointer(pParent)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		if int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) == nCol && int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != OE_None && (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere == uintptr(0) {
			/* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
			 ** of columns. If each indexed column corresponds to a foreign key
			 ** column of pFKey, then this index is a winner.  */
			if zKey == uintptr(0) {
				/* If zKey is NULL, then this foreign key is implicitly mapped to
				 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
				 ** identified by the test.  */
				if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
					if aiCol != 0 {
						i = 0
						for {
							if !(i < nCol) {
								break
							}
							**(**int32)(__ccgo_up(aiCol + uintptr(i)*4)) = (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(i)*8))).FiFrom
							goto _2
						_2:
							;
							i = i + 1
						}
					}
					break
				}
			} else {
				i1 = 0
				for {
					if !(i1 < nCol) {
						break
					}
					iCol = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i1)*2)) /* Name of indexed column */
					if int32(iCol) < 0 {
						break
					} /* No foreign keys against expression indexes */
					/* If the index uses a collation sequence that is different from
					 ** the default collation sequence for the column, this index is
					 ** unusable. Bail out early in this case.  */
					zDfltColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(pParent)).FaCol+uintptr(iCol)*12)
					if !(zDfltColl != 0) {
						zDfltColl = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
					}
					if _sqlite3StrICmp(tls, **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i1)*4)), zDfltColl) != 0 {
						break
					}
					zIdxCol = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pParent)).FaCol + uintptr(iCol)*12))).FzCnName
					j = 0
					for {
						if !(j < nCol) {
							break
						}
						if _sqlite3StrICmp(tls, (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(j)*8))).FzCol, zIdxCol) == 0 {
							if aiCol != 0 {
								**(**int32)(__ccgo_up(aiCol + uintptr(i1)*4)) = (*(*TsColMap)(unsafe.Pointer(pFKey + 36 + uintptr(j)*8))).FiFrom
							}
							break
						}
						goto _4
					_4:
						;
						j = j + 1
					}
					if j == nCol {
						break
					}
					goto _3
				_3:
					;
					i1 = i1 + 1
				}
				if i1 == nCol {
					break
				} /* pIdx is usable */
			}
		}
		goto _1
	_1:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
	}
	if !(pIdx != 0) {
		if !(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x1>>0)) != 0) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18522, libc.VaList(bp+8, (*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FzName, (*TFKey)(unsafe.Pointer(pFKey)).FzTo))
		}
		_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, aiCol)
		return int32(1)
	}
	**(**uintptr)(__ccgo_up(ppIdx)) = pIdx
	return 0
}

// C documentation
//
//	/*
//	** This function is called before generating code to update or delete a
//	** row contained in table pTab.
//	*/
func _sqlite3FkOldmask(tls *libc.TLS, pParse uintptr, pTab uintptr) (r Tu32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i int32
	var mask Tu32
	var p uintptr
	var v3 uint32
	var _ /* pIdx at bp+0 */ uintptr
	_, _, _, _ = i, mask, p, v3
	mask = uint32(0)
	if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_ForeignKeys) != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == TABTYP_NORM {
		p = (*(*struct {
			FaddColOffset int32
			FpFKey        uintptr
			FpDfltList    uintptr
		})(unsafe.Pointer(pTab + 44))).FpFKey
		for {
			if !(p != 0) {
				break
			}
			i = 0
			for {
				if !(i < (*TFKey)(unsafe.Pointer(p)).FnCol) {
					break
				}
				if (*(*TsColMap)(unsafe.Pointer(p + 36 + uintptr(i)*8))).FiFrom > int32(31) {
					v3 = uint32(0xffffffff)
				} else {
					v3 = libc.Uint32FromInt32(1) << (*(*TsColMap)(unsafe.Pointer(p + 36 + uintptr(i)*8))).FiFrom
				}
				mask = mask | v3
				goto _2
			_2:
				;
				i = i + 1
			}
			goto _1
		_1:
			;
			p = (*TFKey)(unsafe.Pointer(p)).FpNextFrom
		}
		p = _sqlite3FkReferences(tls, pTab)
		for {
			if !(p != 0) {
				break
			}
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			_sqlite3FkLocateIndex(tls, pParse, pTab, p, bp, uintptr(0))
			if **(**uintptr)(__ccgo_up(bp)) != 0 {
				i = 0
				for {
					if !(i < int32((*TIndex)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnKeyCol)) {
						break
					}
					if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaiColumn + uintptr(i)*2))) > int32(31) {
						v3 = uint32(0xffffffff)
					} else {
						v3 = libc.Uint32FromInt32(1) << **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaiColumn + uintptr(i)*2))
					}
					mask = mask | v3
					goto _5
				_5:
					;
					i = i + 1
				}
			}
			goto _4
		_4:
			;
			p = (*TFKey)(unsafe.Pointer(p)).FpNextTo
		}
	}
	return mask
}

// C documentation
//
//	/*
//	** This function is called before generating code to update or delete a
//	** row contained in table pTab. If the operation is a DELETE, then
//	** parameter aChange is passed a NULL value. For an UPDATE, aChange points
//	** to an array of size N, where N is the number of columns in table pTab.
//	** If the i'th column is not modified by the UPDATE, then the corresponding
//	** entry in the aChange[] array is set to -1. If the column is modified,
//	** the value is 0 or greater. Parameter chngRowid is set to true if the
//	** UPDATE statement modifies the rowid fields of the table.
//	**
//	** If any foreign key processing will be required, this function returns
//	** non-zero. If there is no foreign key related processing, this function
//	** returns zero.
//	**
//	** For an UPDATE, this function returns 2 if:
//	**
//	**   * There are any FKs for which pTab is the child and the parent table
//	**     and any FK processing at all is required (even of a different FK), or
//	**
//	**   * the UPDATE modifies one or more parent keys for which the action is
//	**     not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
//	**
//	** Or, assuming some other foreign key processing is required, 1.
//	*/
func _sqlite3FkRequired(tls *libc.TLS, pParse uintptr, pTab uintptr, aChange uintptr, chngRowid int32) (r int32) {
	var bHaveFK, eRet, v3 int32
	var p uintptr
	_, _, _, _ = bHaveFK, eRet, p, v3
	eRet = int32(1) /* Value to return if bHaveFK is true */
	bHaveFK = 0     /* If FK processing is required */
	if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_ForeignKeys) != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == TABTYP_NORM {
		if !(aChange != 0) {
			/* A DELETE operation. Foreign key processing is required if the
			 ** table in question is either the child or parent table for any
			 ** foreign key constraint.  */
			bHaveFK = libc.BoolInt32(_sqlite3FkReferences(tls, pTab) != 0 || (*(*struct {
				FaddColOffset int32
				FpFKey        uintptr
				FpDfltList    uintptr
			})(unsafe.Pointer(pTab + 44))).FpFKey != 0)
		} else {
			/* Check if any child key columns are being modified. */
			p = (*(*struct {
				FaddColOffset int32
				FpFKey        uintptr
				FpDfltList    uintptr
			})(unsafe.Pointer(pTab + 44))).FpFKey
			for {
				if !(p != 0) {
					break
				}
				if _fkChildIsModified(tls, pTab, p, aChange, chngRowid) != 0 {
					if 0 == Xsqlite3_stricmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, (*TFKey)(unsafe.Pointer(p)).FzTo) {
						eRet = int32(2)
					}
					bHaveFK = int32(1)
				}
				goto _1
			_1:
				;
				p = (*TFKey)(unsafe.Pointer(p)).FpNextFrom
			}
			/* Check if any parent key columns are being modified. */
			p = _sqlite3FkReferences(tls, pTab)
			for {
				if !(p != 0) {
					break
				}
				if _fkParentIsModified(tls, pTab, p, aChange, chngRowid) != 0 {
					if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&(uint64(libc.Int32FromInt32(0x00008))<<libc.Int32FromInt32(32)) == uint64(0) && int32(**(**Tu8)(__ccgo_up(p + 25 + 1))) != OE_None {
						return int32(2)
					}
					bHaveFK = int32(1)
				}
				goto _2
			_2:
				;
				p = (*TFKey)(unsafe.Pointer(p)).FpNextTo
			}
		}
	}
	if bHaveFK != 0 {
		v3 = eRet
	} else {
		v3 = 0
	}
	return v3
}

// C documentation
//
//	/*
//	** Return an IEEE754 floating point value that approximates d*pow(10,p).
//	**
//	** The (current) algorithm is adapted from the work of Ross Cox at
//	** https://github.com/rsc/fpfmt
//	*/
func _sqlite3Fp10Convert2(tls *libc.TLS, d Tu64, p int32) (r float64) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var adj, b, e, lp, s int32
	var hi, pwr10h, sticky, u, x Tu64
	var mid1, mid2 Tu32
	var _ /* lo at bp+8 */ Tu64
	var _ /* m at bp+16 */ Tu64
	var _ /* pwr10l at bp+0 */ Tu32
	var _ /* r at bp+24 */ float64
	_, _, _, _, _, _, _, _, _, _, _, _ = adj, b, e, hi, lp, mid1, mid2, pwr10h, s, sticky, u, x
	if p < -int32(348) {
		return float64(0)
	}
	if p > +libc.Int32FromInt32(347) {
		return float64(libc.X__builtin_inff(tls))
	}
	b = int32(64) - _countLeadingZeros(tls, d)
	lp = _pwr10to2(tls, p)
	e = int32(53) - b - lp
	if e > int32(1074) {
		if e >= int32(1130) {
			return float64(0)
		}
		e = int32(1074)
	}
	s = -(e - (int32(64) - b) + lp + int32(3))
	pwr10h = _powerOfTen(tls, p, bp)
	if **(**Tu32)(__ccgo_up(bp)) != uint32(0) {
		pwr10h = pwr10h + 1
		**(**Tu32)(__ccgo_up(bp)) = ^**(**Tu32)(__ccgo_up(bp))
	}
	x = d << (int32(64) - b)
	hi = _sqlite3Multiply128(tls, x, pwr10h, bp+8)
	mid1 = uint32(**(**Tu64)(__ccgo_up(bp + 8)) >> int32(32))
	sticky = uint64(1)
	if hi&(libc.Uint64FromInt32(1)<<s-uint64(1)) == uint64(0) {
		mid2 = uint32(_sqlite3Multiply128(tls, x, uint64(**(**Tu32)(__ccgo_up(bp)))<<int32(32), bp+8) >> int32(32))
		sticky = libc.BoolUint64(mid1-mid2 > libc.Uint32FromInt32(1))
		hi = hi - libc.BoolUint64(mid1 < mid2)
	}
	u = hi>>s | sticky
	adj = libc.BoolInt32(u >= libc.Uint64FromInt32(1)<<libc.Int32FromInt32(55)-libc.Uint64FromInt32(2))
	if adj != 0 {
		u = u>>adj | u&uint64(1)
		e = e - adj
	}
	**(**Tu64)(__ccgo_up(bp + 16)) = (u + uint64(1) + u>>libc.Int32FromInt32(2)&uint64(1)) >> int32(2)
	if e <= -int32(972) {
		return float64(libc.X__builtin_inff(tls))
	}
	if **(**Tu64)(__ccgo_up(bp + 16))&(libc.Uint64FromInt32(1)<<libc.Int32FromInt32(52)) != uint64(0) {
		**(**Tu64)(__ccgo_up(bp + 16)) = **(**Tu64)(__ccgo_up(bp + 16)) & ^(libc.Uint64FromInt32(1)<<libc.Int32FromInt32(52)) | uint64(libc.Int32FromInt32(1075)-e)<<libc.Int32FromInt32(52)
	}
	libc.Xmemcpy(tls, bp+24, bp+16, uint32(8))
	return **(**float64)(__ccgo_up(bp + 24))
}

// C documentation
//
//	/*
//	** Decode a floating-point value into an approximate decimal
//	** representation.
//	**
//	** If iRound<=0 then round to -iRound significant digits to the
//	** the right of the decimal point, or to a maximum of mxRound total
//	** significant digits.
//	**
//	** If iRound>0 round to min(iRound,mxRound) significant digits total.
//	**
//	** mxRound must be positive.
//	**
//	** The significant digits of the decimal representation are
//	** stored in p->z[] which is a often (but not always) a pointer
//	** into the middle of p->zBuf[].  There are p->n significant digits.
//	** The p->z[] array is *not* zero-terminated.
//	*/
func _sqlite3FpDecode(tls *libc.TLS, p uintptr, _r float64, iRound int32, mxRound int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	*(*float64)(unsafe.Pointer(bp)) = _r
	var e, i, j, jj, jj1, kk, kk1, kk2, n, nn, v1 int32
	var v2, v21 Tu64
	var z, zBuf uintptr
	var _ /* exp at bp+16 */ int32
	var _ /* v at bp+8 */ Tu64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = e, i, j, jj, jj1, kk, kk1, kk2, n, nn, v2, v21, z, zBuf, v1
	**(**int32)(__ccgo_up(bp + 16)) = 0 /* Local alias for p->z */
	(*TFpDecode)(unsafe.Pointer(p)).FisSpecial = 0
	/* Convert negative numbers to positive.  Deal with Infinity, 0.0, and
	 ** NaN. */
	if **(**float64)(__ccgo_up(bp)) < float64(0) {
		(*TFpDecode)(unsafe.Pointer(p)).Fsign = int8('-')
		**(**float64)(__ccgo_up(bp)) = -**(**float64)(__ccgo_up(bp))
	} else {
		if **(**float64)(__ccgo_up(bp)) == float64(0) {
			(*TFpDecode)(unsafe.Pointer(p)).Fsign = int8('+')
			(*TFpDecode)(unsafe.Pointer(p)).Fn = int32(1)
			(*TFpDecode)(unsafe.Pointer(p)).FiDP = int32(1)
			(*TFpDecode)(unsafe.Pointer(p)).Fz = __ccgo_ts + 1857
			return
		} else {
			(*TFpDecode)(unsafe.Pointer(p)).Fsign = int8('+')
		}
	}
	libc.Xmemcpy(tls, bp+8, bp, uint32(8))
	e = int32(**(**Tu64)(__ccgo_up(bp + 8)) >> libc.Int32FromInt32(52) & uint64(0x7ff))
	if e == int32(0x7ff) {
		(*TFpDecode)(unsafe.Pointer(p)).FisSpecial = int8(int32(1) + libc.BoolInt32(**(**Tu64)(__ccgo_up(bp + 8)) != uint64(0x7ff0000000000000)))
		(*TFpDecode)(unsafe.Pointer(p)).Fn = 0
		(*TFpDecode)(unsafe.Pointer(p)).FiDP = 0
		(*TFpDecode)(unsafe.Pointer(p)).Fz = p + 12
		return
	}
	**(**Tu64)(__ccgo_up(bp + 8)) = **(**Tu64)(__ccgo_up(bp + 8)) & uint64(0x000fffffffffffff)
	if e == 0 {
		nn = _countLeadingZeros(tls, **(**Tu64)(__ccgo_up(bp + 8)))
		**(**Tu64)(__ccgo_up(bp + 8)) = **(**Tu64)(__ccgo_up(bp + 8)) << uint64(nn)
		e = -int32(1074) - nn
	} else {
		**(**Tu64)(__ccgo_up(bp + 8)) = **(**Tu64)(__ccgo_up(bp + 8))<<libc.Int32FromInt32(11) | libc.Uint64FromInt32(1)<<libc.Int32FromInt32(63)
		e = e - int32(1086)
	}
	if iRound <= 0 || iRound >= int32(18) {
		v1 = int32(18)
	} else {
		v1 = iRound + int32(1)
	}
	_sqlite3Fp2Convert10(tls, **(**Tu64)(__ccgo_up(bp + 8)), e, v1, bp+8, bp+16)
	/* Extract significant digits, start at the right-most slot in p->zBuf
	 ** and working back to the right.  "i" keeps track of the next slot in
	 ** which to store a digit. */
	zBuf = p + 12
	i = int32(SQLITE_U64_DIGITS)
	for **(**Tu64)(__ccgo_up(bp + 8)) >= uint64(10) {
		kk = int32(**(**Tu64)(__ccgo_up(bp + 8)) % uint64(100) * uint64(2))
		**(**Tu16)(__ccgo_up(zBuf + uintptr(i-int32(2)))) = **(**Tu16)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3DigitPairs)) + uintptr(kk)))
		i = i - int32(2)
		**(**Tu64)(__ccgo_up(bp + 8)) = **(**Tu64)(__ccgo_up(bp + 8)) / uint64(100)
	}
	if **(**Tu64)(__ccgo_up(bp + 8)) != 0 {
		i = i - 1
		v1 = i
		**(**int8)(__ccgo_up(zBuf + uintptr(v1))) = int8(**(**Tu64)(__ccgo_up(bp + 8)) + uint64('0'))
	}
	n = int32(SQLITE_U64_DIGITS) - i /* Total number of digits extracted */
	(*TFpDecode)(unsafe.Pointer(p)).FiDP = n + **(**int32)(__ccgo_up(bp + 16))
	if iRound <= 0 {
		iRound = (*TFpDecode)(unsafe.Pointer(p)).FiDP - iRound
		if iRound == 0 && int32(**(**int8)(__ccgo_up(zBuf + uintptr(i)))) >= int32('5') {
			iRound = int32(1)
			i = i - 1
			v1 = i
			**(**int8)(__ccgo_up(zBuf + uintptr(v1))) = int8('0')
			n = n + 1
			(*TFpDecode)(unsafe.Pointer(p)).FiDP = (*TFpDecode)(unsafe.Pointer(p)).FiDP + 1
		}
	}
	z = zBuf + uintptr(i) /* z points to the first digit */
	if iRound > 0 && (iRound < n || n > mxRound) {
		if iRound > mxRound {
			iRound = mxRound
		}
		if iRound == int32(17) {
			/* If the precision is exactly 17, which only happens with the "!"
			 ** flag (ex: "%!.17g") then try to reduce the precision if that
			 ** yields text that will round-trip to the original floating-point.
			 ** value.  Thus, for exaple, 49.47 will render as 49.47, rather than
			 ** as 49.469999999999999. */
			if int32(**(**int8)(__ccgo_up(z + 15))) == int32('9') && int32(**(**int8)(__ccgo_up(z + 14))) == int32('9') {
				jj = int32(14)
				for {
					if !(jj > 0 && int32(**(**int8)(__ccgo_up(z + uintptr(jj-int32(1))))) == int32('9')) {
						break
					}
					goto _4
				_4:
					;
					jj = jj - 1
				}
				if jj == 0 {
					v2 = uint64(1)
				} else {
					v2 = uint64(int32(**(**int8)(__ccgo_up(z))) - int32('0'))
					kk1 = int32(1)
					for {
						if !(kk1 < jj) {
							break
						}
						v2 = v2*uint64(10) + uint64(**(**int8)(__ccgo_up(z + uintptr(kk1)))) - uint64('0')
						goto _5
					_5:
						;
						kk1 = kk1 + 1
					}
					v2 = v2 + 1
				}
				if **(**float64)(__ccgo_up(bp)) == _sqlite3Fp10Convert2(tls, v2, **(**int32)(__ccgo_up(bp + 16))+n-jj) {
					iRound = jj + int32(1)
				}
			} else {
				if (*TFpDecode)(unsafe.Pointer(p)).FiDP >= n || int32(**(**int8)(__ccgo_up(z + 15))) == int32('0') && int32(**(**int8)(__ccgo_up(z + 14))) == int32('0') && int32(**(**int8)(__ccgo_up(z + 13))) == int32('0') {
					jj1 = int32(13)
					for {
						if !(int32(**(**int8)(__ccgo_up(z + uintptr(jj1-int32(1))))) == int32('0')) {
							break
						}
						goto _6
					_6:
						;
						jj1 = jj1 - 1
					}
					v21 = uint64(int32(**(**int8)(__ccgo_up(z))) - int32('0'))
					kk2 = int32(1)
					for {
						if !(kk2 < jj1) {
							break
						}
						v21 = v21*uint64(10) + uint64(**(**int8)(__ccgo_up(z + uintptr(kk2)))) - uint64('0')
						goto _7
					_7:
						;
						kk2 = kk2 + 1
					}
					if **(**float64)(__ccgo_up(bp)) == _sqlite3Fp10Convert2(tls, v21, **(**int32)(__ccgo_up(bp + 16))+n-jj1) {
						iRound = jj1 + int32(1)
					}
				}
			}
		}
		n = iRound
		if int32(**(**int8)(__ccgo_up(z + uintptr(iRound)))) >= int32('5') {
			j = iRound - int32(1)
			for int32(1) != 0 {
				**(**int8)(__ccgo_up(z + uintptr(j))) = **(**int8)(__ccgo_up(z + uintptr(j))) + 1
				if int32(**(**int8)(__ccgo_up(z + uintptr(j)))) <= int32('9') {
					break
				}
				**(**int8)(__ccgo_up(z + uintptr(j))) = int8('0')
				if j == 0 {
					z = z - 1
					**(**int8)(__ccgo_up(z)) = int8('1')
					n = n + 1
					(*TFpDecode)(unsafe.Pointer(p)).FiDP = (*TFpDecode)(unsafe.Pointer(p)).FiDP + 1
					break
				} else {
					j = j - 1
				}
			}
		}
	}
	for int32(**(**int8)(__ccgo_up(z + uintptr(n-int32(1))))) == int32('0') {
		n = n - 1
	}
	(*TFpDecode)(unsafe.Pointer(p)).Fn = n
	(*TFpDecode)(unsafe.Pointer(p)).Fz = z
}

func _sqlite3Fts5AuxInit(tls *libc.TLS, pApi uintptr) (r int32) {
	var aBuiltin [4]struct {
		FzFunc     uintptr
		FpUserData uintptr
		FxFunc     Tfts5_extension_function
		FxDestroy  uintptr
	}
	var i, rc int32
	_, _, _ = aBuiltin, i, rc
	aBuiltin = [4]struct {
		FzFunc     uintptr
		FpUserData uintptr
		FxFunc     Tfts5_extension_function
		FxDestroy  uintptr
	}{
		0: {
			FzFunc: __ccgo_ts + 38605,
			FxFunc: __ccgo_fp(_fts5SnippetFunction),
		},
		1: {
			FzFunc: __ccgo_ts + 38613,
			FxFunc: __ccgo_fp(_fts5HighlightFunction),
		},
		2: {
			FzFunc: __ccgo_ts + 38623,
			FxFunc: __ccgo_fp(_fts5Bm25Function),
		},
		3: {
			FzFunc: __ccgo_ts + 38628,
			FxFunc: __ccgo_fp(_fts5GetLocaleFunction),
		},
	}
	rc = SQLITE_OK /* To iterate through builtin functions */
	i = 0
	for {
		if !(rc == SQLITE_OK && i < int32(libc.Uint32FromInt64(64)/libc.Uint32FromInt64(16))) {
			break
		}
		rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, Tfts5_extension_function, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tfts5_api)(unsafe.Pointer(pApi)).FxCreateFunction})))(tls, pApi, aBuiltin[i].FzFunc, aBuiltin[i].FpUserData, aBuiltin[i].FxFunc, aBuiltin[i].FxDestroy)
		goto _1
	_1:
		;
		i = i + 1
	}
	return rc
}

/*
** 2014 May 31
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
 */

/* #include "fts5Int.h" */

// C documentation
//
//	/*
//	** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
//	** the error code in p. If an error has already occurred when this function
//	** is called, it is a no-op.
//	*/
func _sqlite3Fts5BufferAppendBlob(tls *libc.TLS, pRc uintptr, pBuf uintptr, nData Tu32, pData uintptr) {
	var v1 int32
	var v2 uintptr
	_, _ = v1, v2
	if nData != 0 {
		if uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)+nData <= uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace) {
			v1 = 0
		} else {
			v1 = _sqlite3Fts5BufferSize(tls, pRc, pBuf, nData+uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn))
		}
		if v1 != 0 {
			return
		}
		libc.Xmemcpy(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), pData, nData)
		v2 = pBuf + 4
		*(*int32)(unsafe.Pointer(v2)) = int32(uint32(*(*int32)(unsafe.Pointer(v2))) + nData)
	}
}

// C documentation
//
//	/*
//	** Encode value iVal as an SQLite varint and append it to the buffer object
//	** pBuf. If an OOM error occurs, set the error code in p.
//	*/
func _sqlite3Fts5BufferAppendVarint(tls *libc.TLS, pRc uintptr, pBuf uintptr, iVal Ti64) {
	var v1 int32
	_ = v1
	if uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)+uint32(libc.Int32FromInt32(9)) <= uint32((*TFts5Buffer)(unsafe.Pointer(pBuf)).FnSpace) {
		v1 = 0
	} else {
		v1 = _sqlite3Fts5BufferSize(tls, pRc, pBuf, uint32(int32(9)+(*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn))
	}
	if v1 != 0 {
		return
	}
	**(**int32)(__ccgo_up(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(iVal))
}

// C documentation
//
//	/*
//	** Call sqlite3_declare_vtab() based on the contents of the configuration
//	** object passed as the only argument. Return SQLITE_OK if successful, or
//	** an SQLite error code if an error occurs.
//	*/
func _sqlite3Fts5ConfigDeclareVtab(tls *libc.TLS, pConfig uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i int32
	var zSep, zSql, v2 uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _ = i, zSep, zSql, v2
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+39542, 0)
	i = 0
	for {
		if !(zSql != 0 && i < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
			break
		}
		if i == 0 {
			v2 = __ccgo_ts + 1711
		} else {
			v2 = __ccgo_ts + 17436
		}
		zSep = v2
		zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+39558, libc.VaList(bp+16, zSql, zSep, **(**uintptr)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FazCol + uintptr(i)*4))))
		goto _1
	_1:
		;
		i = i + 1
	}
	zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+39565, libc.VaList(bp+16, zSql, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName, __ccgo_ts+39205))
	if zSql != 0 {
		**(**int32)(__ccgo_up(bp)) = Xsqlite3_declare_vtab(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, zSql)
		Xsqlite3_free(tls, zSql)
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Load the contents of the %_config table into memory.
//	*/
func _sqlite3Fts5ConfigLoad(tls *libc.TLS, pConfig uintptr, iCookie int32) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var iVersion int32
	var pVal, zK, zSelect, zSql uintptr
	var _ /* bDummy at bp+8 */ int32
	var _ /* p at bp+0 */ uintptr
	var _ /* rc at bp+4 */ int32
	_, _, _, _, _ = iVersion, pVal, zK, zSelect, zSql
	zSelect = __ccgo_ts + 39673
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**int32)(__ccgo_up(bp + 4)) = SQLITE_OK
	iVersion = 0
	/* Set default values */
	(*TFts5Config)(unsafe.Pointer(pConfig)).Fpgsz = int32(FTS5_DEFAULT_PAGE_SIZE)
	(*TFts5Config)(unsafe.Pointer(pConfig)).FnAutomerge = int32(FTS5_DEFAULT_AUTOMERGE)
	(*TFts5Config)(unsafe.Pointer(pConfig)).FnUsermerge = int32(FTS5_DEFAULT_USERMERGE)
	(*TFts5Config)(unsafe.Pointer(pConfig)).FnCrisisMerge = int32(FTS5_DEFAULT_CRISISMERGE)
	(*TFts5Config)(unsafe.Pointer(pConfig)).FnHashSize = libc.Int32FromInt32(1024) * libc.Int32FromInt32(1024)
	(*TFts5Config)(unsafe.Pointer(pConfig)).FnDeleteMerge = int32(FTS5_DEFAULT_DELETE_AUTOMERGE)
	zSql = _sqlite3Fts5Mprintf(tls, bp+4, zSelect, libc.VaList(bp+24, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName))
	if zSql != 0 {
		**(**int32)(__ccgo_up(bp + 4)) = Xsqlite3_prepare_v2(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, zSql, -int32(1), bp, uintptr(0))
		Xsqlite3_free(tls, zSql)
	}
	if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
		for int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp))) {
			zK = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp)), 0)
			pVal = Xsqlite3_column_value(tls, **(**uintptr)(__ccgo_up(bp)), int32(1))
			if 0 == Xsqlite3_stricmp(tls, zK, __ccgo_ts+39705) {
				iVersion = Xsqlite3_value_int(tls, pVal)
			} else {
				**(**int32)(__ccgo_up(bp + 8)) = 0
				_sqlite3Fts5ConfigSetValue(tls, pConfig, zK, pVal, bp+8)
			}
		}
		**(**int32)(__ccgo_up(bp + 4)) = Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK && iVersion != int32(FTS5_CURRENT_VERSION) && iVersion != int32(FTS5_CURRENT_VERSION_SECUREDELETE) {
		**(**int32)(__ccgo_up(bp + 4)) = int32(SQLITE_ERROR)
		_sqlite3Fts5ConfigErrmsg(tls, pConfig, __ccgo_ts+39713, libc.VaList(bp+24, iVersion, int32(FTS5_CURRENT_VERSION), int32(FTS5_CURRENT_VERSION_SECUREDELETE)))
	} else {
		(*TFts5Config)(unsafe.Pointer(pConfig)).FiVersion = iVersion
	}
	if **(**int32)(__ccgo_up(bp + 4)) == SQLITE_OK {
		(*TFts5Config)(unsafe.Pointer(pConfig)).FiCookie = iCookie
	}
	return **(**int32)(__ccgo_up(bp + 4))
}

// C documentation
//
//	/*
//	** Arguments nArg/azArg contain the string arguments passed to the xCreate
//	** or xConnect method of the virtual table. This function attempts to
//	** allocate an instance of Fts5Config containing the results of parsing
//	** those arguments.
//	**
//	** If successful, SQLITE_OK is returned and *ppOut is set to point to the
//	** new Fts5Config object. If an error occurs, an SQLite error code is
//	** returned, *ppOut is set to NULL and an error message may be left in
//	** *pzErr. It is the responsibility of the caller to eventually free any
//	** such error message using sqlite3_free().
//	*/
func _sqlite3Fts5ConfigParse(tls *libc.TLS, pGlobal uintptr, db uintptr, nArg int32, azArg uintptr, ppOut uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var bOption, i int32
	var nByte Tsqlite3_int64
	var pRet, z, zOrig, zTail, v1, v2 uintptr
	var _ /* bDummy at bp+20 */ int32
	var _ /* bMustBeCol at bp+16 */ int32
	var _ /* bUnindexed at bp+4 */ int32
	var _ /* rc at bp+0 */ int32
	var _ /* zOne at bp+8 */ uintptr
	var _ /* zTwo at bp+12 */ uintptr
	_, _, _, _, _, _, _, _, _ = bOption, i, nByte, pRet, z, zOrig, zTail, v1, v2
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	**(**int32)(__ccgo_up(bp + 4)) = 0 /* True if there are one or more UNINDEXED */
	v1 = Xsqlite3_malloc64(tls, uint64(164))
	pRet = v1
	**(**uintptr)(__ccgo_up(ppOut)) = v1
	if pRet == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, pRet, 0, uint32(164))
	(*TFts5Config)(unsafe.Pointer(pRet)).FpGlobal = pGlobal
	(*TFts5Config)(unsafe.Pointer(pRet)).Fdb = db
	(*TFts5Config)(unsafe.Pointer(pRet)).FiCookie = -int32(1)
	nByte = int64(uint32(nArg) * (libc.Uint32FromInt64(4) + libc.Uint32FromInt64(1)))
	(*TFts5Config)(unsafe.Pointer(pRet)).FazCol = _sqlite3Fts5MallocZero(tls, bp, nByte)
	if (*TFts5Config)(unsafe.Pointer(pRet)).FazCol != 0 {
		v1 = (*TFts5Config)(unsafe.Pointer(pRet)).FazCol + uintptr(nArg)*4
	} else {
		v1 = uintptr(0)
	}
	(*TFts5Config)(unsafe.Pointer(pRet)).FabUnindexed = v1
	(*TFts5Config)(unsafe.Pointer(pRet)).FzDb = _sqlite3Fts5Strndup(tls, bp, **(**uintptr)(__ccgo_up(azArg + 1*4)), -int32(1))
	(*TFts5Config)(unsafe.Pointer(pRet)).FzName = _sqlite3Fts5Strndup(tls, bp, **(**uintptr)(__ccgo_up(azArg + 2*4)), -int32(1))
	(*TFts5Config)(unsafe.Pointer(pRet)).FbColumnsize = int32(1)
	(*TFts5Config)(unsafe.Pointer(pRet)).FeDetail = FTS5_DETAIL_FULL
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && Xsqlite3_stricmp(tls, (*TFts5Config)(unsafe.Pointer(pRet)).FzName, __ccgo_ts+39205) == 0 {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39316, libc.VaList(bp+32, (*TFts5Config)(unsafe.Pointer(pRet)).FzName))
		**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
	}
	i = int32(3)
	for {
		if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && i < nArg) {
			break
		}
		zOrig = **(**uintptr)(__ccgo_up(azArg + uintptr(i)*4))
		**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
		**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
		bOption = 0
		**(**int32)(__ccgo_up(bp + 16)) = 0
		z = _fts5ConfigGobbleWord(tls, bp, zOrig, bp+8, bp+16)
		z = _fts5ConfigSkipWhitespace(tls, z)
		if z != 0 && int32(**(**int8)(__ccgo_up(z))) == int32('=') {
			bOption = int32(1)
			z = z + 1
			if **(**int32)(__ccgo_up(bp + 16)) != 0 {
				z = uintptr(0)
			}
		}
		z = _fts5ConfigSkipWhitespace(tls, z)
		if z != 0 && **(**int8)(__ccgo_up(z)) != 0 {
			z = _fts5ConfigGobbleWord(tls, bp, z, bp+12, bp+20)
			if z != 0 && **(**int8)(__ccgo_up(z)) != 0 {
				z = uintptr(0)
			}
		}
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			if z == uintptr(0) {
				**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39345, libc.VaList(bp+32, zOrig))
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
			} else {
				if bOption != 0 {
					if **(**uintptr)(__ccgo_up(bp + 8)) != 0 {
						v1 = **(**uintptr)(__ccgo_up(bp + 8))
					} else {
						v1 = __ccgo_ts + 1711
					}
					if **(**uintptr)(__ccgo_up(bp + 12)) != 0 {
						v2 = **(**uintptr)(__ccgo_up(bp + 12))
					} else {
						v2 = __ccgo_ts + 1711
					}
					**(**int32)(__ccgo_up(bp)) = _fts5ConfigParseSpecial(tls, pRet, v1, v2, pzErr)
				} else {
					**(**int32)(__ccgo_up(bp)) = _fts5ConfigParseColumn(tls, pRet, **(**uintptr)(__ccgo_up(bp + 8)), **(**uintptr)(__ccgo_up(bp + 12)), pzErr, bp+4)
					**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
				}
			}
		}
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 8)))
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 12)))
		goto _3
	_3:
		;
		i = i + 1
	}
	/* We only allow contentless_delete=1 if the table is indeed contentless. */
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FbContentlessDelete != 0 && (*TFts5Config)(unsafe.Pointer(pRet)).FeContent != int32(FTS5_CONTENT_NONE) {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39365, 0)
		**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
	}
	/* We only allow contentless_delete=1 if columnsize=0 is not present.
	 **
	 ** This restriction may be removed at some point.
	 */
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FbContentlessDelete != 0 && (*TFts5Config)(unsafe.Pointer(pRet)).FbColumnsize == 0 {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39415, 0)
		**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
	}
	/* We only allow contentless_unindexed=1 if the table is actually a
	 ** contentless one.
	 */
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FbContentlessUnindexed != 0 && (*TFts5Config)(unsafe.Pointer(pRet)).FeContent != int32(FTS5_CONTENT_NONE) {
		**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+39470, 0)
		**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
	}
	/* If no zContent option was specified, fill in the default values. */
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FzContent == uintptr(0) {
		zTail = uintptr(0)
		if (*TFts5Config)(unsafe.Pointer(pRet)).FeContent == FTS5_CONTENT_NORMAL {
			zTail = __ccgo_ts + 38828
		} else {
			if **(**int32)(__ccgo_up(bp + 4)) != 0 && (*TFts5Config)(unsafe.Pointer(pRet)).FbContentlessUnindexed != 0 {
				(*TFts5Config)(unsafe.Pointer(pRet)).FeContent = int32(FTS5_CONTENT_UNINDEXED)
				zTail = __ccgo_ts + 38828
			} else {
				if (*TFts5Config)(unsafe.Pointer(pRet)).FbColumnsize != 0 {
					zTail = __ccgo_ts + 39523
				}
			}
		}
		if zTail != 0 {
			(*TFts5Config)(unsafe.Pointer(pRet)).FzContent = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+39531, libc.VaList(bp+32, (*TFts5Config)(unsafe.Pointer(pRet)).FzDb, (*TFts5Config)(unsafe.Pointer(pRet)).FzName, zTail))
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pRet)).FzContentRowid == uintptr(0) {
		(*TFts5Config)(unsafe.Pointer(pRet)).FzContentRowid = _sqlite3Fts5Strndup(tls, bp, __ccgo_ts+19186, -int32(1))
	}
	/* Formulate the zContentExprlist text */
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp)) = _fts5ConfigMakeExprlist(tls, pRet)
	}
	if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
		_sqlite3Fts5ConfigFree(tls, pRet)
		**(**uintptr)(__ccgo_up(ppOut)) = uintptr(0)
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Parameter zIn contains a rank() function specification. The format of
//	** this is:
//	**
//	**   + Bareword (function name)
//	**   + Open parenthesis - "("
//	**   + Zero or more SQL literals in a comma separated list
//	**   + Close parenthesis - ")"
//	*/
func _sqlite3Fts5ConfigParseRank(tls *libc.TLS, zIn uintptr, pzRank uintptr, pzRankArgs uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var p, pArgs, pRank, zRank, zRankArgs uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _, _, _ = p, pArgs, pRank, zRank, zRankArgs
	p = zIn
	zRank = uintptr(0)
	zRankArgs = uintptr(0)
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	**(**uintptr)(__ccgo_up(pzRank)) = uintptr(0)
	**(**uintptr)(__ccgo_up(pzRankArgs)) = uintptr(0)
	if p == uintptr(0) {
		**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
	} else {
		p = _fts5ConfigSkipWhitespace(tls, p)
		pRank = p
		p = _fts5ConfigSkipBareword(tls, p)
		if p != 0 {
			zRank = _sqlite3Fts5MallocZero(tls, bp, int64(int32(uintptr(1)+p)-int32(pRank)))
			if zRank != 0 {
				libc.Xmemcpy(tls, zRank, pRank, uint32(int32(p)-int32(pRank)))
			}
		} else {
			**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
		}
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			p = _fts5ConfigSkipWhitespace(tls, p)
			if int32(**(**int8)(__ccgo_up(p))) != int32('(') {
				**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
			}
			p = p + 1
		}
		if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
			p = _fts5ConfigSkipWhitespace(tls, p)
			pArgs = p
			if int32(**(**int8)(__ccgo_up(p))) != int32(')') {
				p = _fts5ConfigSkipArgs(tls, p)
				if p == uintptr(0) {
					**(**int32)(__ccgo_up(bp)) = int32(SQLITE_ERROR)
				} else {
					zRankArgs = _sqlite3Fts5MallocZero(tls, bp, int64(int32(uintptr(1)+p)-int32(pArgs)))
					if zRankArgs != 0 {
						libc.Xmemcpy(tls, zRankArgs, pArgs, uint32(int32(p)-int32(pArgs)))
					}
				}
			}
		}
	}
	if **(**int32)(__ccgo_up(bp)) != SQLITE_OK {
		Xsqlite3_free(tls, zRank)
	} else {
		**(**uintptr)(__ccgo_up(pzRank)) = zRank
		**(**uintptr)(__ccgo_up(pzRankArgs)) = zRankArgs
	}
	return **(**int32)(__ccgo_up(bp))
}

func _sqlite3Fts5ConfigSetValue(tls *libc.TLS, pConfig uintptr, zKey uintptr, pVal uintptr, pbBadkey uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bVal, bVal1, nAutomerge, nCrisisMerge, nHashSize, nUsermerge, nVal, pgsz, rc, v1 int32
	var zIn uintptr
	var _ /* zRank at bp+0 */ uintptr
	var _ /* zRankArgs at bp+4 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _ = bVal, bVal1, nAutomerge, nCrisisMerge, nHashSize, nUsermerge, nVal, pgsz, rc, zIn, v1
	rc = SQLITE_OK
	if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+39591) {
		pgsz = 0
		if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) {
			pgsz = Xsqlite3_value_int(tls, pVal)
		}
		if pgsz < int32(32) || pgsz > libc.Int32FromInt32(64)*libc.Int32FromInt32(1024) {
			**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
		} else {
			(*TFts5Config)(unsafe.Pointer(pConfig)).Fpgsz = pgsz
		}
	} else {
		if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+39596) {
			nHashSize = -int32(1)
			if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) {
				nHashSize = Xsqlite3_value_int(tls, pVal)
			}
			if nHashSize <= 0 {
				**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
			} else {
				(*TFts5Config)(unsafe.Pointer(pConfig)).FnHashSize = nHashSize
			}
		} else {
			if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+39605) {
				nAutomerge = -int32(1)
				if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) {
					nAutomerge = Xsqlite3_value_int(tls, pVal)
				}
				if nAutomerge < 0 || nAutomerge > int32(64) {
					**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
				} else {
					if nAutomerge == int32(1) {
						nAutomerge = int32(FTS5_DEFAULT_AUTOMERGE)
					}
					(*TFts5Config)(unsafe.Pointer(pConfig)).FnAutomerge = nAutomerge
				}
			} else {
				if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+39615) {
					nUsermerge = -int32(1)
					if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) {
						nUsermerge = Xsqlite3_value_int(tls, pVal)
					}
					if nUsermerge < int32(2) || nUsermerge > int32(16) {
						**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
					} else {
						(*TFts5Config)(unsafe.Pointer(pConfig)).FnUsermerge = nUsermerge
					}
				} else {
					if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+39625) {
						nCrisisMerge = -int32(1)
						if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) {
							nCrisisMerge = Xsqlite3_value_int(tls, pVal)
						}
						if nCrisisMerge < 0 {
							**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
						} else {
							if nCrisisMerge <= int32(1) {
								nCrisisMerge = int32(FTS5_DEFAULT_CRISISMERGE)
							}
							if nCrisisMerge >= int32(FTS5_MAX_SEGMENT) {
								nCrisisMerge = libc.Int32FromInt32(FTS5_MAX_SEGMENT) - libc.Int32FromInt32(1)
							}
							(*TFts5Config)(unsafe.Pointer(pConfig)).FnCrisisMerge = nCrisisMerge
						}
					} else {
						if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+39637) {
							nVal = -int32(1)
							if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) {
								nVal = Xsqlite3_value_int(tls, pVal)
							} else {
								**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
							}
							if nVal < 0 {
								nVal = int32(FTS5_DEFAULT_DELETE_AUTOMERGE)
							}
							if nVal > int32(100) {
								nVal = 0
							}
							(*TFts5Config)(unsafe.Pointer(pConfig)).FnDeleteMerge = nVal
						} else {
							if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+39205) {
								zIn = Xsqlite3_value_text(tls, pVal)
								rc = _sqlite3Fts5ConfigParseRank(tls, zIn, bp, bp+4)
								if rc == SQLITE_OK {
									Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzRank)
									Xsqlite3_free(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FzRankArgs)
									(*TFts5Config)(unsafe.Pointer(pConfig)).FzRank = **(**uintptr)(__ccgo_up(bp))
									(*TFts5Config)(unsafe.Pointer(pConfig)).FzRankArgs = **(**uintptr)(__ccgo_up(bp + 4))
								} else {
									if rc == int32(SQLITE_ERROR) {
										rc = SQLITE_OK
										**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
									}
								}
							} else {
								if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+39649) {
									bVal = -int32(1)
									if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) {
										bVal = Xsqlite3_value_int(tls, pVal)
									}
									if bVal < 0 {
										**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
									} else {
										if bVal != 0 {
											v1 = int32(1)
										} else {
											v1 = 0
										}
										(*TFts5Config)(unsafe.Pointer(pConfig)).FbSecureDelete = v1
									}
								} else {
									if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+39663) {
										bVal1 = -int32(1)
										if int32(SQLITE_INTEGER) == Xsqlite3_value_numeric_type(tls, pVal) {
											bVal1 = Xsqlite3_value_int(tls, pVal)
										}
										if bVal1 < 0 {
											**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
										} else {
											if bVal1 != 0 {
												v1 = int32(1)
											} else {
												v1 = 0
											}
											(*TFts5Config)(unsafe.Pointer(pConfig)).FbPrefixInsttoken = v1
										}
									} else {
										**(**int32)(__ccgo_up(pbBadkey)) = int32(1)
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return rc
}

func _sqlite3Fts5ExprAnd(tls *libc.TLS, pp1 uintptr, p2 uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var ap, p1 uintptr
	var i, nPhrase int32
	var _ /* sParse at bp+0 */ TFts5Parse
	_, _, _, _ = ap, i, nPhrase, p1
	libc.Xmemset(tls, bp, 0, uint32(28))
	if **(**uintptr)(__ccgo_up(pp1)) != 0 && p2 != 0 {
		p1 = **(**uintptr)(__ccgo_up(pp1))
		nPhrase = (*TFts5Expr)(unsafe.Pointer(p1)).FnPhrase + (*TFts5Expr)(unsafe.Pointer(p2)).FnPhrase
		(*TFts5Expr)(unsafe.Pointer(p1)).FpRoot = _sqlite3Fts5ParseNode(tls, bp, int32(FTS5_AND), (*TFts5Expr)(unsafe.Pointer(p1)).FpRoot, (*TFts5Expr)(unsafe.Pointer(p2)).FpRoot, uintptr(0))
		(*TFts5Expr)(unsafe.Pointer(p2)).FpRoot = uintptr(0)
		if (**(**TFts5Parse)(__ccgo_up(bp))).Frc == SQLITE_OK {
			ap = Xsqlite3_realloc64(tls, (*TFts5Expr)(unsafe.Pointer(p1)).FapExprPhrase, uint64(uint32(nPhrase)*uint32(4)))
			if ap == uintptr(0) {
				(**(**TFts5Parse)(__ccgo_up(bp))).Frc = int32(SQLITE_NOMEM)
			} else {
				libc.Xmemmove(tls, ap+uintptr((*TFts5Expr)(unsafe.Pointer(p2)).FnPhrase)*4, ap, uint32((*TFts5Expr)(unsafe.Pointer(p1)).FnPhrase)*uint32(4))
				i = 0
				for {
					if !(i < (*TFts5Expr)(unsafe.Pointer(p2)).FnPhrase) {
						break
					}
					**(**uintptr)(__ccgo_up(ap + uintptr(i)*4)) = **(**uintptr)(__ccgo_up((*TFts5Expr)(unsafe.Pointer(p2)).FapExprPhrase + uintptr(i)*4))
					goto _1
				_1:
					;
					i = i + 1
				}
				(*TFts5Expr)(unsafe.Pointer(p1)).FnPhrase = nPhrase
				(*TFts5Expr)(unsafe.Pointer(p1)).FapExprPhrase = ap
			}
		}
		Xsqlite3_free(tls, (*TFts5Expr)(unsafe.Pointer(p2)).FapExprPhrase)
		Xsqlite3_free(tls, p2)
	} else {
		if p2 != 0 {
			**(**uintptr)(__ccgo_up(pp1)) = p2
		}
	}
	return (**(**TFts5Parse)(__ccgo_up(bp))).Frc
}

// C documentation
//
//	/*
//	** Clear the position lists associated with all phrases in the expression
//	** passed as the first argument. Argument bLive is true if the expression
//	** might be pointing to a real entry, otherwise it has just been reset.
//	**
//	** At present this function is only used for detail=col and detail=none
//	** fts5 tables. This implies that all phrases must be at most 1 token
//	** in size, as phrase matches are not supported without detail=full.
//	*/
func _sqlite3Fts5ExprClearPoslists(tls *libc.TLS, pExpr uintptr, bLive int32) (r uintptr) {
	var i int32
	var pBuf, pNode, pRet uintptr
	_, _, _, _ = i, pBuf, pNode, pRet
	pRet = Xsqlite3_malloc64(tls, uint64(uint32(16)*uint32((*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase)))
	if pRet != 0 {
		libc.Xmemset(tls, pRet, 0, uint32(16)*uint32((*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase))
		i = 0
		for {
			if !(i < (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase) {
				break
			}
			pBuf = **(**uintptr)(__ccgo_up((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*4)) + 4
			pNode = (*TFts5ExprPhrase)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*4)))).FpNode
			if bLive != 0 && ((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn == 0 || (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid != (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FpRoot)).FiRowid || (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof != 0) {
				(**(**TFts5PoslistPopulator)(__ccgo_up(pRet + uintptr(i)*16))).FbMiss = int32(1)
			} else {
				(*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn = 0
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	return pRet
}

// C documentation
//
//	/*
//	** Create a new FTS5 expression by cloning phrase iPhrase of the
//	** expression passed as the second argument.
//	*/
func _sqlite3Fts5ExprClonePhrase(tls *libc.TLS, pExpr uintptr, iPhrase int32, ppNew uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, tflags int32
	var nByte Tsqlite3_int64
	var p, pColset, pColsetOrig, pNew, pOrig uintptr
	var _ /* rc at bp+0 */ int32
	var _ /* sCtx at bp+4 */ TTokenCtx
	_, _, _, _, _, _, _, _ = i, nByte, p, pColset, pColsetOrig, pNew, pOrig, tflags
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK           /* Return code */
	pOrig = uintptr(0)                               /* The phrase extracted from pExpr */
	pNew = uintptr(0)                                /* Expression to return via *ppNew */
	**(**TTokenCtx)(__ccgo_up(bp + 4)) = TTokenCtx{} /* Context object for fts5ParseTokenize */
	if !(pExpr != 0) || iPhrase < 0 || iPhrase >= (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase {
		**(**int32)(__ccgo_up(bp)) = int32(SQLITE_RANGE)
	} else {
		pOrig = **(**uintptr)(__ccgo_up((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(iPhrase)*4))
		pNew = _sqlite3Fts5MallocZero(tls, bp, int64(24))
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		(*TFts5Expr)(unsafe.Pointer(pNew)).FapExprPhrase = _sqlite3Fts5MallocZero(tls, bp, int64(4))
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		(*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot = _sqlite3Fts5MallocZero(tls, bp, int64(uint32(libc.UintptrFromInt32(0)+40)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(4)))
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		(*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear = _sqlite3Fts5MallocZero(tls, bp, int64(uint32(libc.UintptrFromInt32(0)+12)+uint32(libc.Int32FromInt32(2))*libc.Uint32FromInt64(4)))
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && pOrig != uintptr(0) {
		pColsetOrig = (*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5ExprPhrase)(unsafe.Pointer(pOrig)).FpNode)).FpNear)).FpColset
		if pColsetOrig != 0 {
			nByte = int64(libc.Uint32FromInt64(8) * uint32(((*TFts5Colset)(unsafe.Pointer(pColsetOrig)).FnCol+libc.Int32FromInt32(2))/libc.Int32FromInt32(2)))
			pColset = _sqlite3Fts5MallocZero(tls, bp, nByte)
			if pColset != 0 {
				libc.Xmemcpy(tls, pColset, pColsetOrig, uint32(nByte))
			}
			(*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear)).FpColset = pColset
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		if (*TFts5ExprPhrase)(unsafe.Pointer(pOrig)).FnTerm != 0 { /* Used to iterate through phrase terms */
			(**(**TTokenCtx)(__ccgo_up(bp + 4))).FpConfig = (*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig
			i = 0
			for {
				if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && i < (*TFts5ExprPhrase)(unsafe.Pointer(pOrig)).FnTerm) {
					break
				}
				tflags = 0
				p = pOrig + 20 + uintptr(i)*24
				for {
					if !(p != 0 && **(**int32)(__ccgo_up(bp)) == SQLITE_OK) {
						break
					}
					**(**int32)(__ccgo_up(bp)) = _fts5ParseTokenize(tls, bp+4, tflags, (*TFts5ExprTerm)(unsafe.Pointer(p)).FpTerm, (*TFts5ExprTerm)(unsafe.Pointer(p)).FnFullTerm, 0, 0)
					tflags = int32(FTS5_TOKEN_COLOCATED)
					goto _2
				_2:
					;
					p = (*TFts5ExprTerm)(unsafe.Pointer(p)).FpSynonym
				}
				if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
					(*(*TFts5ExprTerm)(unsafe.Pointer((**(**TTokenCtx)(__ccgo_up(bp + 4))).FpPhrase + 20 + uintptr(i)*24))).FbPrefix = (*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 20 + uintptr(i)*24))).FbPrefix
					(*(*TFts5ExprTerm)(unsafe.Pointer((**(**TTokenCtx)(__ccgo_up(bp + 4))).FpPhrase + 20 + uintptr(i)*24))).FbFirst = (*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 20 + uintptr(i)*24))).FbFirst
				}
				goto _1
			_1:
				;
				i = i + 1
			}
		} else {
			/* This happens when parsing a token or quoted phrase that contains
			 ** no token characters at all. (e.g ... MATCH '""'). */
			(**(**TTokenCtx)(__ccgo_up(bp + 4))).FpPhrase = _sqlite3Fts5MallocZero(tls, bp, int64(uint32(libc.UintptrFromInt32(0)+20)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(24)))
		}
	}
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK && (**(**TTokenCtx)(__ccgo_up(bp + 4))).FpPhrase != 0 {
		/* All the allocations succeeded. Put the expression object together. */
		(*TFts5Expr)(unsafe.Pointer(pNew)).FpIndex = (*TFts5Expr)(unsafe.Pointer(pExpr)).FpIndex
		(*TFts5Expr)(unsafe.Pointer(pNew)).FpConfig = (*TFts5Expr)(unsafe.Pointer(pExpr)).FpConfig
		(*TFts5Expr)(unsafe.Pointer(pNew)).FnPhrase = int32(1)
		**(**uintptr)(__ccgo_up((*TFts5Expr)(unsafe.Pointer(pNew)).FapExprPhrase)) = (**(**TTokenCtx)(__ccgo_up(bp + 4))).FpPhrase
		*(*uintptr)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear + 12)) = (**(**TTokenCtx)(__ccgo_up(bp + 4))).FpPhrase
		(*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear)).FnPhrase = int32(1)
		(*TFts5ExprPhrase)(unsafe.Pointer((**(**TTokenCtx)(__ccgo_up(bp + 4))).FpPhrase)).FpNode = (*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot
		if (*TFts5ExprPhrase)(unsafe.Pointer(pOrig)).FnTerm == int32(1) && (*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 20))).FpSynonym == uintptr(0) && int32((*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 20))).FbFirst) == 0 {
			(*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FeType = int32(FTS5_TERM)
			(*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FxNext = __ccgo_fp(_fts5ExprNodeNext_TERM)
		} else {
			(*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FeType = int32(FTS5_STRING)
			(*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FxNext = __ccgo_fp(_fts5ExprNodeNext_STRING)
		}
	} else {
		_sqlite3Fts5ExprFree(tls, pNew)
		_fts5ExprPhraseFree(tls, (**(**TTokenCtx)(__ccgo_up(bp + 4))).FpPhrase)
		pNew = uintptr(0)
	}
	**(**uintptr)(__ccgo_up(ppNew)) = pNew
	return **(**int32)(__ccgo_up(bp))
}

func _sqlite3Fts5ExprNew(tls *libc.TLS, pConfig uintptr, bPhraseToAnd int32, iCol int32, zExpr uintptr, ppNew uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var n, t int32
	var pColset, pEngine, pNew, v1 uintptr
	var _ /* sParse at bp+0 */ TFts5Parse
	var _ /* token at bp+32 */ TFts5Token
	var _ /* z at bp+40 */ uintptr
	_, _, _, _, _, _ = n, pColset, pEngine, pNew, t, v1
	**(**uintptr)(__ccgo_up(bp + 40)) = zExpr
	**(**uintptr)(__ccgo_up(ppNew)) = uintptr(0)
	**(**uintptr)(__ccgo_up(pzErr)) = uintptr(0)
	libc.Xmemset(tls, bp, 0, uint32(28))
	(**(**TFts5Parse)(__ccgo_up(bp))).FbPhraseToAnd = bPhraseToAnd
	pEngine = _sqlite3Fts5ParserAlloc(tls, __ccgo_fp(_fts5ParseAlloc))
	if pEngine == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	(**(**TFts5Parse)(__ccgo_up(bp))).FpConfig = pConfig
	for cond := true; cond; cond = (**(**TFts5Parse)(__ccgo_up(bp))).Frc == SQLITE_OK && t != FTS5_EOF {
		t = _fts5ExprGetToken(tls, bp, bp+40, bp+32)
		_sqlite3Fts5Parser(tls, pEngine, t, **(**TFts5Token)(__ccgo_up(bp + 32)), bp)
	}
	_sqlite3Fts5ParserFree(tls, pEngine, __ccgo_fp(_fts5ParseFree))
	/* If the LHS of the MATCH expression was a user column, apply the
	 ** implicit column-filter.  */
	if (**(**TFts5Parse)(__ccgo_up(bp))).Frc == SQLITE_OK && iCol < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol {
		n = int32(libc.Uint32FromInt64(8) * uint32((libc.Int32FromInt32(1)+libc.Int32FromInt32(2))/libc.Int32FromInt32(2)))
		pColset = _sqlite3Fts5MallocZero(tls, bp+8, int64(n))
		if pColset != 0 {
			(*TFts5Colset)(unsafe.Pointer(pColset)).FnCol = int32(1)
			*(*int32)(unsafe.Pointer(pColset + 4)) = iCol
			_sqlite3Fts5ParseSetColset(tls, bp, (**(**TFts5Parse)(__ccgo_up(bp))).FpExpr, pColset)
		}
	}
	if (**(**TFts5Parse)(__ccgo_up(bp))).Frc == SQLITE_OK {
		v1 = Xsqlite3_malloc64(tls, uint64(24))
		pNew = v1
		**(**uintptr)(__ccgo_up(ppNew)) = v1
		if pNew == uintptr(0) {
			(**(**TFts5Parse)(__ccgo_up(bp))).Frc = int32(SQLITE_NOMEM)
			_sqlite3Fts5ParseNodeFree(tls, (**(**TFts5Parse)(__ccgo_up(bp))).FpExpr)
		} else {
			(*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot = (**(**TFts5Parse)(__ccgo_up(bp))).FpExpr
			(*TFts5Expr)(unsafe.Pointer(pNew)).FpIndex = uintptr(0)
			(*TFts5Expr)(unsafe.Pointer(pNew)).FpConfig = pConfig
			(*TFts5Expr)(unsafe.Pointer(pNew)).FapExprPhrase = (**(**TFts5Parse)(__ccgo_up(bp))).FapPhrase
			(*TFts5Expr)(unsafe.Pointer(pNew)).FnPhrase = (**(**TFts5Parse)(__ccgo_up(bp))).FnPhrase
			(*TFts5Expr)(unsafe.Pointer(pNew)).FbDesc = 0
			(**(**TFts5Parse)(__ccgo_up(bp))).FapPhrase = uintptr(0)
		}
	} else {
		_sqlite3Fts5ParseNodeFree(tls, (**(**TFts5Parse)(__ccgo_up(bp))).FpExpr)
	}
	Xsqlite3_free(tls, (**(**TFts5Parse)(__ccgo_up(bp))).FapPhrase)
	if uintptr(0) == **(**uintptr)(__ccgo_up(pzErr)) {
		**(**uintptr)(__ccgo_up(pzErr)) = (**(**TFts5Parse)(__ccgo_up(bp))).FzErr
	} else {
		Xsqlite3_free(tls, (**(**TFts5Parse)(__ccgo_up(bp))).FzErr)
	}
	return (**(**TFts5Parse)(__ccgo_up(bp))).Frc
}

// C documentation
//
//	/*
//	** Empty (but do not delete) a hash table.
//	*/
func _sqlite3Fts5HashClear(tls *libc.TLS, pHash uintptr) {
	var i int32
	var pNext, pSlot uintptr
	_, _, _ = i, pNext, pSlot
	i = 0
	for {
		if !(i < (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot) {
			break
		}
		pSlot = **(**uintptr)(__ccgo_up((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(i)*4))
		for {
			if !(pSlot != 0) {
				break
			}
			pNext = (*TFts5HashEntry)(unsafe.Pointer(pSlot)).FpHashNext
			Xsqlite3_free(tls, pSlot)
			goto _2
		_2:
			;
			pSlot = pNext
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	libc.Xmemset(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot, 0, uint32((*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot)*uint32(4))
	(*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry = 0
}

// C documentation
//
//	/*
//	** Allocate a new hash table.
//	*/
func _sqlite3Fts5HashNew(tls *libc.TLS, pConfig uintptr, ppNew uintptr, pnByte uintptr) (r int32) {
	var nByte Tsqlite3_int64
	var pNew, v1 uintptr
	var rc int32
	_, _, _, _ = nByte, pNew, rc, v1
	rc = SQLITE_OK
	v1 = Xsqlite3_malloc64(tls, uint64(24))
	pNew = v1
	**(**uintptr)(__ccgo_up(ppNew)) = v1
	if pNew == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		libc.Xmemset(tls, pNew, 0, uint32(24))
		(*TFts5Hash)(unsafe.Pointer(pNew)).FpnByte = pnByte
		(*TFts5Hash)(unsafe.Pointer(pNew)).FeDetail = (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail
		(*TFts5Hash)(unsafe.Pointer(pNew)).FnSlot = int32(1024)
		nByte = int64(uint32(4) * uint32((*TFts5Hash)(unsafe.Pointer(pNew)).FnSlot))
		(*TFts5Hash)(unsafe.Pointer(pNew)).FaSlot = Xsqlite3_malloc64(tls, uint64(nByte))
		if (*TFts5Hash)(unsafe.Pointer(pNew)).FaSlot == uintptr(0) {
			Xsqlite3_free(tls, pNew)
			**(**uintptr)(__ccgo_up(ppNew)) = uintptr(0)
			rc = int32(SQLITE_NOMEM)
		} else {
			libc.Xmemset(tls, (*TFts5Hash)(unsafe.Pointer(pNew)).FaSlot, 0, uint32(nByte))
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Query the hash table for a doclist associated with term pTerm/nTerm.
//	*/
func _sqlite3Fts5HashQuery(tls *libc.TLS, pHash uintptr, nPre int32, pTerm uintptr, nTerm int32, ppOut uintptr, pnDoclist uintptr) (r int32) {
	var iHash uint32
	var nHashPre, nList int32
	var p, pFaux, pRet, zKey, v2 uintptr
	_, _, _, _, _, _, _, _ = iHash, nHashPre, nList, p, pFaux, pRet, zKey, v2
	iHash = _fts5HashKey(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot, pTerm, nTerm)
	zKey = uintptr(0)
	p = **(**uintptr)(__ccgo_up((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4))
	for {
		if !(p != 0) {
			break
		}
		zKey = p + 1*40
		if nTerm == (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey && libc.Xmemcmp(tls, zKey, pTerm, uint32(nTerm)) == 0 {
			break
		}
		goto _1
	_1:
		;
		p = (*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext
	}
	if p != 0 {
		nHashPre = int32(uint32(40) + uint32(nTerm))
		nList = (*TFts5HashEntry)(unsafe.Pointer(p)).FnData - nHashPre
		v2 = Xsqlite3_malloc64(tls, uint64(nPre+nList+int32(10)))
		**(**uintptr)(__ccgo_up(ppOut)) = v2
		pRet = v2
		if pRet != 0 {
			pFaux = pRet + uintptr(nPre-nHashPre)
			libc.Xmemcpy(tls, pRet+uintptr(nPre), p+uintptr(nHashPre), uint32(nList))
			nList = nList + _fts5HashAddPoslistSize(tls, pHash, p, pFaux)
			**(**int32)(__ccgo_up(pnDoclist)) = nList
		} else {
			**(**int32)(__ccgo_up(pnDoclist)) = 0
			return int32(SQLITE_NOMEM)
		}
	} else {
		**(**uintptr)(__ccgo_up(ppOut)) = uintptr(0)
		**(**int32)(__ccgo_up(pnDoclist)) = 0
	}
	return SQLITE_OK
}

func _sqlite3Fts5HashScanEntry(tls *libc.TLS, pHash uintptr, pzTerm uintptr, pnTerm uintptr, ppDoclist uintptr, pnDoclist uintptr) {
	var nTerm int32
	var p, zKey, v1 uintptr
	_, _, _, _ = nTerm, p, zKey, v1
	v1 = (*TFts5Hash)(unsafe.Pointer(pHash)).FpScan
	p = v1
	if v1 != 0 {
		zKey = p + 1*40
		nTerm = (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey
		_fts5HashAddPoslistSize(tls, pHash, p, uintptr(0))
		**(**uintptr)(__ccgo_up(pzTerm)) = zKey
		**(**int32)(__ccgo_up(pnTerm)) = nTerm
		**(**uintptr)(__ccgo_up(ppDoclist)) = zKey + uintptr(nTerm)
		**(**int32)(__ccgo_up(pnDoclist)) = int32(uint32((*TFts5HashEntry)(unsafe.Pointer(p)).FnData) - (uint32(40) + uint32(nTerm)))
	} else {
		**(**uintptr)(__ccgo_up(pzTerm)) = uintptr(0)
		**(**int32)(__ccgo_up(pnTerm)) = 0
		**(**uintptr)(__ccgo_up(ppDoclist)) = uintptr(0)
		**(**int32)(__ccgo_up(pnDoclist)) = 0
	}
}

/*
** 2014 May 31
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
**
** Low level access to the FTS index stored in the database file. The
** routines in this file file implement all read and write access to the
** %_data table. Other parts of the system access this functionality via
** the interface defined in fts5Int.h.
 */

/* #include "fts5Int.h" */

/*
** Overview:
**
** The %_data table contains all the FTS indexes for an FTS5 virtual table.
** As well as the main term index, there may be up to 31 prefix indexes.
** The format is similar to FTS3/4, except that:
**
**   * all segment b-tree leaf data is stored in fixed size page records
**     (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
**     taken to ensure it is possible to iterate in either direction through
**     the entries in a doclist, or to seek to a specific entry within a
**     doclist, without loading it into memory.
**
**   * large doclists that span many pages have associated "doclist index"
**     records that contain a copy of the first rowid on each page spanned by
**     the doclist. This is used to speed up seek operations, and merges of
**     large doclists with very small doclists.
**
**   * extra fields in the "structure record" record the state of ongoing
**     incremental merge operations.
**
 */

/*
** There are two versions of the format used for the structure record:
**
**   1. the legacy format, that may be read by all fts5 versions, and
**
**   2. the V2 format, which is used by contentless_delete=1 databases.
**
** Both begin with a 4-byte "configuration cookie" value. Then, a legacy
** format structure record contains a varint - the number of levels in
** the structure. Whereas a V2 structure record contains the constant
** 4 bytes [0xff 0x00 0x00 0x01]. This is unambiguous as the value of a
** varint has to be at least 16256 to begin with "0xFF". And the default
** maximum number of levels is 64.
**
** See below for more on structure record formats.
 */

/*
** Details:
**
** The %_data table managed by this module,
**
**     CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
**
** , contains the following 6 types of records. See the comments surrounding
** the FTS5_*_ROWID macros below for a description of how %_data rowids are
** assigned to each fo them.
**
** 1. Structure Records:
**
**   The set of segments that make up an index - the index structure - are
**   recorded in a single record within the %_data table. The record consists
**   of a single 32-bit configuration cookie value followed by a list of
**   SQLite varints.
**
**   If the structure record is a V2 record, the configuration cookie is
**   followed by the following 4 bytes: [0xFF 0x00 0x00 0x01].
**
**   Next, the record continues with three varints:
**
**     + number of levels,
**     + total number of segments on all levels,
**     + value of write counter.
**
**   Then, for each level from 0 to nMax:
**
**     + number of input segments in ongoing merge.
**     + total number of segments in level.
**     + for each segment from oldest to newest:
**         + segment id (always > 0)
**         + first leaf page number (often 1, always greater than 0)
**         + final leaf page number
**
**      Then, for V2 structures only:
**
**         + lower origin counter value,
**         + upper origin counter value,
**         + the number of tombstone hash pages.
**
** 2. The Averages Record:
**
**   A single record within the %_data table. The data is a list of varints.
**   The first value is the number of rows in the index. Then, for each column
**   from left to right, the total number of tokens in the column for all
**   rows of the table.
**
** 3. Segment leaves:
**
**   TERM/DOCLIST FORMAT:
**
**     Most of each segment leaf is taken up by term/doclist data. The
**     general format of term/doclist, starting with the first term
**     on the leaf page, is:
**
**         varint : size of first term
**         blob:    first term data
**         doclist: first doclist
**         zero-or-more {
**           varint:  number of bytes in common with previous term
**           varint:  number of bytes of new term data (nNew)
**           blob:    nNew bytes of new term data
**           doclist: next doclist
**         }
**
**     doclist format:
**
**         varint:  first rowid
**         poslist: first poslist
**         zero-or-more {
**           varint:  rowid delta (always > 0)
**           poslist: next poslist
**         }
**
**     poslist format:
**
**         varint: size of poslist in bytes multiplied by 2, not including
**                 this field. Plus 1 if this entry carries the "delete" flag.
**         collist: collist for column 0
**         zero-or-more {
**           0x01 byte
**           varint: column number (I)
**           collist: collist for column I
**         }
**
**     collist format:
**
**         varint: first offset + 2
**         zero-or-more {
**           varint: offset delta + 2
**         }
**
**   PAGE FORMAT
**
**     Each leaf page begins with a 4-byte header containing 2 16-bit
**     unsigned integer fields in big-endian format. They are:
**
**       * The byte offset of the first rowid on the page, if it exists
**         and occurs before the first term (otherwise 0).
**
**       * The byte offset of the start of the page footer. If the page
**         footer is 0 bytes in size, then this field is the same as the
**         size of the leaf page in bytes.
**
**     The page footer consists of a single varint for each term located
**     on the page. Each varint is the byte offset of the current term
**     within the page, delta-compressed against the previous value. In
**     other words, the first varint in the footer is the byte offset of
**     the first term, the second is the byte offset of the second less that
**     of the first, and so on.
**
**     The term/doclist format described above is accurate if the entire
**     term/doclist data fits on a single leaf page. If this is not the case,
**     the format is changed in two ways:
**
**       + if the first rowid on a page occurs before the first term, it
**         is stored as a literal value:
**
**             varint:  first rowid
**
**       + the first term on each page is stored in the same way as the
**         very first term of the segment:
**
**             varint : size of first term
**             blob:    first term data
**
** 5. Segment doclist indexes:
**
**   Doclist indexes are themselves b-trees, however they usually consist of
**   a single leaf record only. The format of each doclist index leaf page
**   is:
**
**     * Flags byte. Bits are:
**         0x01: Clear if leaf is also the root page, otherwise set.
**
**     * Page number of fts index leaf page. As a varint.
**
**     * First rowid on page indicated by previous field. As a varint.
**
**     * A list of varints, one for each subsequent termless page. A
**       positive delta if the termless page contains at least one rowid,
**       or an 0x00 byte otherwise.
**
**   Internal doclist index nodes are:
**
**     * Flags byte. Bits are:
**         0x01: Clear for root page, otherwise set.
**
**     * Page number of first child page. As a varint.
**
**     * Copy of first rowid on page indicated by previous field. As a varint.
**
**     * A list of delta-encoded varints - the first rowid on each subsequent
**       child page.
**
** 6. Tombstone Hash Page
**
**   These records are only ever present in contentless_delete=1 tables.
**   There are zero or more of these associated with each segment. They
**   are used to store the tombstone rowids for rows contained in the
**   associated segments.
**
**   The set of nHashPg tombstone hash pages associated with a single
**   segment together form a single hash table containing tombstone rowids.
**   To find the page of the hash on which a key might be stored:
**
**       iPg = (rowid % nHashPg)
**
**   Then, within page iPg, which has nSlot slots:
**
**       iSlot = (rowid / nHashPg) % nSlot
**
**   Each tombstone hash page begins with an 8 byte header:
**
**     1-byte:  Key-size (the size in bytes of each slot). Either 4 or 8.
**     1-byte:  rowid-0-tombstone flag. This flag is only valid on the
**              first tombstone hash page for each segment (iPg=0). If set,
**              the hash table contains rowid 0. If clear, it does not.
**              Rowid 0 is handled specially.
**     2-bytes: unused.
**     4-bytes: Big-endian integer containing number of entries on page.
**
**   Following this are nSlot 4 or 8 byte slots (depending on the key-size
**   in the first byte of the page header). The number of slots may be
**   determined based on the size of the page record and the key-size:
**
**     nSlot = (nByte - 8) / key-size
 */

/*
** Rowids for the averages and structure records in the %_data table.
 */

/*
** Macros determining the rowids used by segment leaves and dlidx leaves
** and nodes. All nodes and leaves are stored in the %_data table with large
** positive rowids.
**
** Each segment has a unique non-zero 16-bit id.
**
** The rowid for each segment leaf is found by passing the segment id and
** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
** sequentially starting from 1.
 */

/*
** Each time a blob is read from the %_data table, it is padded with this
** many zero bytes. This makes it easier to decode the various record formats
** without overreading if the records are corrupt.
 */

// C documentation
//
//	/*
//	** Add an entry to the in-memory hash table. The key is the concatenation
//	** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
//	**
//	**     (bByte || pToken) -> (iRowid,iCol,iPos)
//	**
//	** Or, if iCol is negative, then the value is a delete marker.
//	*/
func _sqlite3Fts5HashWrite(tls *libc.TLS, pHash uintptr, iRowid Ti64, iCol int32, iPos int32, bByte int8, pToken uintptr, nToken int32) (r int32) {
	var bNew, nIncr, rc, v2 int32
	var iDiff Tu64
	var iHash uint32
	var nByte, nNew Tsqlite3_int64
	var p, pNew, pPtr, pp, zKey, zKey1, v6 uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bNew, iDiff, iHash, nByte, nIncr, nNew, p, pNew, pPtr, pp, rc, zKey, zKey1, v2, v6
	nIncr = 0 /* If non-delete entry should be written */
	bNew = libc.BoolInt32((*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == FTS5_DETAIL_FULL)
	/* Attempt to locate an existing hash entry */
	iHash = _fts5HashKey2(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot, uint8(bByte), pToken, nToken)
	p = **(**uintptr)(__ccgo_up((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4))
	for {
		if !(p != 0) {
			break
		}
		zKey = p + 1*40
		if int32(**(**int8)(__ccgo_up(zKey))) == int32(bByte) && (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey == nToken+int32(1) && libc.Xmemcmp(tls, zKey+1, pToken, uint32(nToken)) == 0 {
			break
		}
		goto _1
	_1:
		;
		p = (*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext
	}
	/* If an existing hash entry cannot be found, create a new one. */
	if p == uintptr(0) {
		nByte = int64(uint32(40) + uint32(nToken+libc.Int32FromInt32(1)) + uint32(1) + uint32(64))
		if nByte < int64(128) {
			nByte = int64(128)
		}
		/* Grow the Fts5Hash.aSlot[] array if necessary. */
		if (*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry*int32(2) >= (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot {
			rc = _fts5HashResize(tls, pHash)
			if rc != SQLITE_OK {
				return rc
			}
			iHash = _fts5HashKey2(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot, uint8(bByte), pToken, nToken)
		}
		/* Allocate new Fts5HashEntry and add it to the hash table. */
		p = Xsqlite3_malloc64(tls, uint64(nByte))
		if !(p != 0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, p, 0, uint32(40))
		(*TFts5HashEntry)(unsafe.Pointer(p)).FnAlloc = int32(nByte)
		zKey1 = p + 1*40
		**(**int8)(__ccgo_up(zKey1)) = bByte
		libc.Xmemcpy(tls, zKey1+1, pToken, uint32(nToken))
		(*TFts5HashEntry)(unsafe.Pointer(p)).FnKey = nToken + int32(1)
		**(**int8)(__ccgo_up(zKey1 + uintptr(nToken+int32(1)))) = int8('\000')
		(*TFts5HashEntry)(unsafe.Pointer(p)).FnData = int32(uint32(nToken+int32(1)) + uint32(40))
		(*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext = **(**uintptr)(__ccgo_up((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4))
		**(**uintptr)(__ccgo_up((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4)) = p
		(*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry = (*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry + 1
		/* Add the first rowid field to the hash-entry */
		**(**int32)(__ccgo_up(p + 16)) += _sqlite3Fts5PutVarint(tls, p+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FnData), uint64(iRowid))
		(*TFts5HashEntry)(unsafe.Pointer(p)).FiRowid = iRowid
		(*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist = (*TFts5HashEntry)(unsafe.Pointer(p)).FnData
		if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail != int32(FTS5_DETAIL_NONE) {
			**(**int32)(__ccgo_up(p + 16)) += int32(1)
			if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == FTS5_DETAIL_FULL {
				v2 = 0
			} else {
				v2 = -int32(1)
			}
			(*TFts5HashEntry)(unsafe.Pointer(p)).FiCol = int16(v2)
		}
	} else {
		/* Appending to an existing hash-entry. Check that there is enough
		 ** space to append the largest possible new entry. Worst case scenario
		 ** is:
		 **
		 **     + 9 bytes for a new rowid,
		 **     + 4 byte reserved for the "poslist size" varint.
		 **     + 1 byte for a "new column" byte,
		 **     + 3 bytes for a new column number (16-bit max) as a varint,
		 **     + 5 bytes for the new position offset (32-bit max).
		 */
		if (*TFts5HashEntry)(unsafe.Pointer(p)).FnAlloc-(*TFts5HashEntry)(unsafe.Pointer(p)).FnData < libc.Int32FromInt32(9)+libc.Int32FromInt32(4)+libc.Int32FromInt32(1)+libc.Int32FromInt32(3)+libc.Int32FromInt32(5) {
			nNew = int64((*TFts5HashEntry)(unsafe.Pointer(p)).FnAlloc * int32(2))
			pNew = Xsqlite3_realloc64(tls, p, uint64(nNew))
			if pNew == uintptr(0) {
				return int32(SQLITE_NOMEM)
			}
			(*TFts5HashEntry)(unsafe.Pointer(pNew)).FnAlloc = int32(nNew)
			pp = (*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*4
			for {
				if !(**(**uintptr)(__ccgo_up(pp)) != p) {
					break
				}
				goto _3
			_3:
				;
				pp = **(**uintptr)(__ccgo_up(pp))
			}
			**(**uintptr)(__ccgo_up(pp)) = pNew
			p = pNew
		}
		nIncr = nIncr - (*TFts5HashEntry)(unsafe.Pointer(p)).FnData
	}
	pPtr = p
	/* If this is a new rowid, append the 4-byte size field for the previous
	 ** entry, and the new rowid for this entry.  */
	if iRowid != (*TFts5HashEntry)(unsafe.Pointer(p)).FiRowid {
		iDiff = uint64(iRowid) - uint64((*TFts5HashEntry)(unsafe.Pointer(p)).FiRowid)
		_fts5HashAddPoslistSize(tls, pHash, p, uintptr(0))
		**(**int32)(__ccgo_up(p + 16)) += _sqlite3Fts5PutVarint(tls, pPtr+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FnData), iDiff)
		(*TFts5HashEntry)(unsafe.Pointer(p)).FiRowid = iRowid
		bNew = int32(1)
		(*TFts5HashEntry)(unsafe.Pointer(p)).FiSzPoslist = (*TFts5HashEntry)(unsafe.Pointer(p)).FnData
		if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail != int32(FTS5_DETAIL_NONE) {
			**(**int32)(__ccgo_up(p + 16)) += int32(1)
			if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == FTS5_DETAIL_FULL {
				v2 = 0
			} else {
				v2 = -int32(1)
			}
			(*TFts5HashEntry)(unsafe.Pointer(p)).FiCol = int16(v2)
			(*TFts5HashEntry)(unsafe.Pointer(p)).FiPos = 0
		}
	}
	if iCol >= 0 {
		if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == int32(FTS5_DETAIL_NONE) {
			(*TFts5HashEntry)(unsafe.Pointer(p)).FbContent = uint8(1)
		} else {
			/* Append a new column value, if necessary */
			if iCol != int32((*TFts5HashEntry)(unsafe.Pointer(p)).FiCol) {
				if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == FTS5_DETAIL_FULL {
					v6 = p + 16
					v2 = *(*int32)(unsafe.Pointer(v6))
					*(*int32)(unsafe.Pointer(v6)) = *(*int32)(unsafe.Pointer(v6)) + 1
					**(**Tu8)(__ccgo_up(pPtr + uintptr(v2))) = uint8(0x01)
					**(**int32)(__ccgo_up(p + 16)) += _sqlite3Fts5PutVarint(tls, pPtr+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FnData), uint64(iCol))
					(*TFts5HashEntry)(unsafe.Pointer(p)).FiCol = int16(iCol)
					(*TFts5HashEntry)(unsafe.Pointer(p)).FiPos = 0
				} else {
					bNew = int32(1)
					v2 = iCol
					iPos = v2
					(*TFts5HashEntry)(unsafe.Pointer(p)).FiCol = int16(v2)
				}
			}
			/* Append the new position offset, if necessary */
			if bNew != 0 {
				**(**int32)(__ccgo_up(p + 16)) += _sqlite3Fts5PutVarint(tls, pPtr+uintptr((*TFts5HashEntry)(unsafe.Pointer(p)).FnData), uint64(iPos-(*TFts5HashEntry)(unsafe.Pointer(p)).FiPos+int32(2)))
				(*TFts5HashEntry)(unsafe.Pointer(p)).FiPos = iPos
			}
		}
	} else {
		/* This is a delete. Set the delete flag. */
		(*TFts5HashEntry)(unsafe.Pointer(p)).FbDel = uint8(1)
	}
	nIncr = nIncr + (*TFts5HashEntry)(unsafe.Pointer(p)).FnData
	**(**int32)(__ccgo_up((*TFts5Hash)(unsafe.Pointer(pHash)).FpnByte)) += nIncr
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Add iRowid to the tombstone list of the segment or segments that contain
//	** rows from origin iOrigin. Return SQLITE_OK if successful, or an SQLite
//	** error code otherwise.
//	*/
func _sqlite3Fts5IndexContentlessDelete(tls *libc.TLS, p uintptr, iOrigin Ti64, iRowid Ti64) (r int32) {
	var bFound, iLvl, iSeg int32
	var pSeg, pStruct uintptr
	_, _, _, _, _ = bFound, iLvl, iSeg, pSeg, pStruct
	pStruct = _fts5StructureRead(tls, p)
	if pStruct != 0 {
		bFound = 0
		iLvl = (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel - int32(1)
		for {
			if !(iLvl >= 0) {
				break
			}
			iSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg - int32(1)
			for {
				if !(iSeg >= 0) {
					break
				}
				pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FaSeg + uintptr(iSeg)*56
				if (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin1 <= uint64(iOrigin) && (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2 >= uint64(iOrigin) {
					if bFound == 0 {
						(*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntryTombstone = (*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntryTombstone + 1
						bFound = int32(1)
					}
					_fts5IndexTombstoneAdd(tls, p, pSeg, uint64(iRowid))
				}
				goto _2
			_2:
				;
				iSeg = iSeg - 1
			}
			goto _1
		_1:
			;
			iLvl = iLvl - 1
		}
		_fts5StructureRelease(tls, pStruct)
	}
	return _fts5IndexReturn(tls, p)
}

/*************************************************************************
**************************************************************************
** Below this point is the implementation of the integrity-check
** functionality.
 */

// C documentation
//
//	/*
//	** Read and decode the "averages" record from the database.
//	**
//	** Parameter anSize must point to an array of size nCol, where nCol is
//	** the number of user defined columns in the FTS table.
//	*/
func _sqlite3Fts5IndexGetAverages(tls *libc.TLS, p uintptr, pnRow uintptr, anSize uintptr) (r int32) {
	var i, iCol, nCol int32
	var pData uintptr
	_, _, _, _ = i, iCol, nCol, pData
	nCol = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FnCol
	**(**Ti64)(__ccgo_up(pnRow)) = 0
	libc.Xmemset(tls, anSize, 0, uint32(8)*uint32(nCol))
	pData = _fts5DataRead(tls, p, int64(FTS5_AVERAGES_ROWID))
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*TFts5Data)(unsafe.Pointer(pData)).Fnn != 0 {
		i = 0
		i = i + int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+uintptr(i), pnRow))
		iCol = 0
		for {
			if !(i < (*TFts5Data)(unsafe.Pointer(pData)).Fnn && iCol < nCol) {
				break
			}
			i = i + int32(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+uintptr(i), anSize+uintptr(iCol)*8))
			goto _1
		_1:
			;
			iCol = iCol + 1
		}
	}
	_fts5DataRelease(tls, pData)
	return _fts5IndexReturn(tls, p)
}

// C documentation
//
//	/*
//	** Run internal checks to ensure that the FTS index (a) is internally
//	** consistent and (b) contains entries for which the XOR of the checksums
//	** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
//	**
//	** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
//	** checksum does not match. Return SQLITE_OK if all checks pass without
//	** error, or some other SQLite error code if another error (e.g. OOM)
//	** occurs.
//	*/
func _sqlite3Fts5IndexIntegrityCheck(tls *libc.TLS, p uintptr, cksum Tu64, bUseCksum int32) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var cksum2 Tu64
	var eDetail, flags, iCol, iLvl, iSeg, iTokOff int32
	var iRowid Ti64
	var pSeg, pStruct, z uintptr
	var _ /* iOff at bp+32 */ int32
	var _ /* iPos at bp+24 */ Ti64
	var _ /* n at bp+16 */ int32
	var _ /* pIter at bp+12 */ uintptr
	var _ /* poslist at bp+0 */ TFts5Buffer
	_, _, _, _, _, _, _, _, _, _, _ = cksum2, eDetail, flags, iCol, iLvl, iRowid, iSeg, iTokOff, pSeg, pStruct, z
	eDetail = (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FeDetail
	cksum2 = uint64(0) /* Checksum based on contents of indexes */
	**(**TFts5Buffer)(__ccgo_up(bp)) = TFts5Buffer{}
	flags = int32(FTS5INDEX_QUERY_NOOUTPUT)
	/* Load the FTS index structure */
	pStruct = _fts5StructureRead(tls, p)
	if pStruct == uintptr(0) {
		return _fts5IndexReturn(tls, p)
	}
	/* Check that the internal nodes of each segment match the leaves */
	iLvl = 0
	for {
		if !(iLvl < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) {
			break
		}
		iSeg = 0
		for {
			if !(iSeg < (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FnSeg) {
				break
			}
			pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*12))).FaSeg + uintptr(iSeg)*56
			_fts5IndexIntegrityCheckSegment(tls, p, pSeg)
			goto _2
		_2:
			;
			iSeg = iSeg + 1
		}
		goto _1
	_1:
		;
		iLvl = iLvl + 1
	}
	/* The cksum argument passed to this function is a checksum calculated
	 ** based on all expected entries in the FTS index (including prefix index
	 ** entries). This block checks that a checksum calculated based on the
	 ** actual contents of FTS index is identical.
	 **
	 ** Two versions of the same checksum are calculated. The first (stack
	 ** variable cksum2) based on entries extracted from the full-text index
	 ** while doing a linear scan of each individual index in turn.
	 **
	 ** As each term visited by the linear scans, a separate query for the
	 ** same term is performed. cksum3 is calculated based on the entries
	 ** extracted by these queries.
	 */
	_fts5MultiIterNew(tls, p, pStruct, flags, uintptr(0), uintptr(0), 0, -int32(1), 0, bp+12)
	for {
		if !(_fts5MultiIterEof(tls, p, **(**uintptr)(__ccgo_up(bp + 12))) == 0) {
			break
		} /* Size of term in bytes */
		**(**Ti64)(__ccgo_up(bp + 24)) = 0  /* Position read from poslist */
		**(**int32)(__ccgo_up(bp + 32)) = 0 /* Offset within poslist */
		iRowid = _fts5MultiIterRowid(tls, **(**uintptr)(__ccgo_up(bp + 12)))
		z = _fts5MultiIterTerm(tls, **(**uintptr)(__ccgo_up(bp + 12)), bp+16)
		/* If this is a new term, query for it. Update cksum3 with the results. */
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
			break
		}
		if eDetail == int32(FTS5_DETAIL_NONE) {
			if 0 == _fts5MultiIterIsEmpty(tls, p, **(**uintptr)(__ccgo_up(bp + 12))) {
				cksum2 = cksum2 ^ _sqlite3Fts5IndexEntryCksum(tls, iRowid, 0, 0, -int32(1), z, **(**int32)(__ccgo_up(bp + 16)))
			}
		} else {
			(**(**TFts5Buffer)(__ccgo_up(bp))).Fn = 0
			_fts5SegiterPoslist(tls, p, **(**uintptr)(__ccgo_up(bp + 12))+80+uintptr((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 12)))).FaFirst + 1*4))).FiFirst)*104, uintptr(0), bp)
			_sqlite3Fts5BufferAppendBlob(tls, p+44, bp, uint32(4), __ccgo_ts+40909)
			for 0 == _sqlite3Fts5PoslistNext64(tls, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp, (**(**TFts5Buffer)(__ccgo_up(bp))).Fn, bp+32, bp+24) {
				iCol = int32(**(**Ti64)(__ccgo_up(bp + 24)) >> libc.Int32FromInt32(32) & libc.Int64FromInt32(0x7FFFFFFF))
				iTokOff = int32(**(**Ti64)(__ccgo_up(bp + 24)) & libc.Int64FromInt32(0x7FFFFFFF))
				cksum2 = cksum2 ^ _sqlite3Fts5IndexEntryCksum(tls, iRowid, iCol, iTokOff, -int32(1), z, **(**int32)(__ccgo_up(bp + 16)))
			}
		}
		goto _3
	_3:
		;
		_fts5MultiIterNext(tls, p, **(**uintptr)(__ccgo_up(bp + 12)), 0, 0)
	}
	_fts5MultiIterFree(tls, **(**uintptr)(__ccgo_up(bp + 12)))
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bUseCksum != 0 && cksum != cksum2 {
		(*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
		_sqlite3Fts5ConfigErrmsg(tls, (*TFts5Index)(unsafe.Pointer(p)).FpConfig, __ccgo_ts+40914, libc.VaList(bp+48, (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FzName))
	}
	_fts5StructureRelease(tls, pStruct)
	_sqlite3Fts5BufferFree(tls, bp)
	return _fts5IndexReturn(tls, p)
}

/*************************************************************************
**************************************************************************
** Below this point is the implementation of the fts5_decode() scalar
** function only.
 */

// C documentation
//
//	/*
//	** Set a token-mapping for the iterator passed as the first argument. This
//	** is used in detail=column or detail=none mode when a token is requested
//	** using the xInstToken() API. In this case the caller tokenizers the
//	** current row and configures the token-mapping via multiple calls to this
//	** function.
//	*/
func _sqlite3Fts5IndexIterWriteTokendata(tls *libc.TLS, pIndexIter uintptr, pToken uintptr, nToken int32, iRowid Ti64, iCol int32, iOff int32) (r int32) {
	var iPos Ti64
	var ii int32
	var p, pIter, pT, pTerm uintptr
	_, _, _, _, _, _ = iPos, ii, p, pIter, pT, pTerm
	pIter = pIndexIter
	pT = (*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter
	p = (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex
	iPos = int64(iCol)<<libc.Int32FromInt32(32) + int64(iOff)
	if (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg > 0 {
		/* This is a prefix term iterator. */
		if pT == uintptr(0) {
			pT = _sqlite3Fts5MallocZero(tls, p+44, int64(uint32(libc.UintptrFromInt32(0)+56)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(80)))
			(*TFts5Iter)(unsafe.Pointer(pIter)).FpTokenDataIter = pT
		}
		if pT != 0 {
			_fts5TokendataIterAppendMap(tls, p, pT, (*TFts5TokenDataIter)(unsafe.Pointer(pT)).Fterms.Fn, nToken, iRowid, iPos)
			_sqlite3Fts5BufferAppendBlob(tls, p+44, pT+20, uint32(nToken), pToken)
		}
	} else {
		ii = 0
		for {
			if !(int64(ii) < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter) {
				break
			}
			pTerm = *(*uintptr)(unsafe.Pointer(pT + 56 + uintptr(ii)*4)) + 80 + 72
			if nToken == (*TFts5Buffer)(unsafe.Pointer(pTerm)).Fn-int32(1) && libc.Xmemcmp(tls, pToken, (*TFts5Buffer)(unsafe.Pointer(pTerm)).Fp+uintptr(1), uint32(nToken)) == 0 {
				break
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
		if int64(ii) < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter {
			_fts5TokendataIterAppendMap(tls, p, pT, ii, 0, iRowid, iPos)
		}
	}
	return _fts5IndexReturn(tls, p)
}

// C documentation
//
//	/*
//	** Open a new Fts5Index handle. If the bCreate argument is true, create
//	** and initialize the underlying %_data table.
//	**
//	** If successful, set *pp to point to the new object and return SQLITE_OK.
//	** Otherwise, set *pp to NULL and return an SQLite error code.
//	*/
func _sqlite3Fts5IndexOpen(tls *libc.TLS, pConfig uintptr, bCreate int32, pp uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var p, v1 uintptr
	var _ /* rc at bp+0 */ int32
	_, _ = p, v1
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK /* New object */
	v1 = _sqlite3Fts5MallocZero(tls, bp, int64(112))
	p = v1
	**(**uintptr)(__ccgo_up(pp)) = v1
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		(*TFts5Index)(unsafe.Pointer(p)).FpConfig = pConfig
		(*TFts5Index)(unsafe.Pointer(p)).FnWorkUnit = int32(FTS5_WORK_UNIT)
		(*TFts5Index)(unsafe.Pointer(p)).FzDataTbl = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+40734, libc.VaList(bp+16, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName))
		if (*TFts5Index)(unsafe.Pointer(p)).FzDataTbl != 0 && bCreate != 0 {
			**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5CreateTable(tls, pConfig, __ccgo_ts+28645, __ccgo_ts+40742, 0, pzErr)
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5CreateTable(tls, pConfig, __ccgo_ts+14261, __ccgo_ts+40777, int32(1), pzErr)
			}
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5IndexReinit(tls, p)
			}
		}
	}
	if **(**int32)(__ccgo_up(bp)) != 0 {
		_sqlite3Fts5IndexClose(tls, p)
		**(**uintptr)(__ccgo_up(pp)) = uintptr(0)
	}
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Open a new iterator to iterate though all rowid that match the
//	** specified token or token prefix.
//	*/
func _sqlite3Fts5IndexQuery(tls *libc.TLS, p uintptr, pToken uintptr, nToken int32, flags int32, pColset uintptr, ppIter uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bDesc, bTokendata, iIdx, iPrefixIdx, nChar, nIdxChar int32
	var pConfig, pSeg, pStruct uintptr
	var _ /* buf at bp+4 */ TFts5Buffer
	var _ /* pRet at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _ = bDesc, bTokendata, iIdx, iPrefixIdx, nChar, nIdxChar, pConfig, pSeg, pStruct
	pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**TFts5Buffer)(__ccgo_up(bp + 4)) = TFts5Buffer{}
	/* If the QUERY_SCAN flag is set, all other flags must be clear. */
	if _sqlite3Fts5BufferSize(tls, p+44, bp+4, uint32(nToken+int32(1))) == 0 {
		iIdx = 0       /* Index to search */
		iPrefixIdx = 0 /* +1 prefix index */
		bTokendata = (*TFts5Config)(unsafe.Pointer(pConfig)).FbTokendata
		if nToken > 0 {
			libc.Xmemcpy(tls, (**(**TFts5Buffer)(__ccgo_up(bp + 4))).Fp+1, pToken, uint32(nToken))
		}
		/* The NOTOKENDATA flag is set when each token in a tokendata=1 table
		 ** should be treated individually, instead of merging all those with
		 ** a common prefix into a single entry. This is used, for example, by
		 ** queries performed as part of an integrity-check, or by the fts5vocab
		 ** module.  */
		if flags&(libc.Int32FromInt32(FTS5INDEX_QUERY_NOTOKENDATA)|libc.Int32FromInt32(FTS5INDEX_QUERY_SCAN)) != 0 {
			bTokendata = 0
		}
		/* Figure out which index to search and set iIdx accordingly. If this
		 ** is a prefix query for which there is no prefix index, set iIdx to
		 ** greater than pConfig->nPrefix to indicate that the query will be
		 ** satisfied by scanning multiple terms in the main index.
		 **
		 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
		 ** prefix-query. Instead of using a prefix-index (if one exists),
		 ** evaluate the prefix query using the main FTS index. This is used
		 ** for internal sanity checking by the integrity-check in debug
		 ** mode only.  */
		if flags&int32(FTS5INDEX_QUERY_PREFIX) != 0 {
			nChar = _fts5IndexCharlen(tls, pToken, nToken)
			iIdx = int32(1)
			for {
				if !(iIdx <= (*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix) {
					break
				}
				nIdxChar = **(**int32)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FaPrefix + uintptr(iIdx-int32(1))*4))
				if nIdxChar == nChar {
					break
				}
				if nIdxChar == nChar+int32(1) {
					iPrefixIdx = iIdx
				}
				goto _1
			_1:
				;
				iIdx = iIdx + 1
			}
		}
		if bTokendata != 0 && iIdx == 0 {
			**(**Tu8)(__ccgo_up((**(**TFts5Buffer)(__ccgo_up(bp + 4))).Fp)) = uint8('0')
			**(**uintptr)(__ccgo_up(bp)) = _fts5SetupTokendataIter(tls, p, (**(**TFts5Buffer)(__ccgo_up(bp + 4))).Fp, nToken+int32(1), pColset)
		} else {
			if iIdx <= (*TFts5Config)(unsafe.Pointer(pConfig)).FnPrefix {
				/* Straight index lookup */
				pStruct = _fts5StructureRead(tls, p)
				**(**Tu8)(__ccgo_up((**(**TFts5Buffer)(__ccgo_up(bp + 4))).Fp)) = uint8(libc.Int32FromUint8('0') + iIdx)
				if pStruct != 0 {
					_fts5MultiIterNew(tls, p, pStruct, flags|int32(FTS5INDEX_QUERY_SKIPEMPTY), pColset, (**(**TFts5Buffer)(__ccgo_up(bp + 4))).Fp, nToken+int32(1), -int32(1), 0, bp)
					_fts5StructureRelease(tls, pStruct)
				}
			} else {
				/* Scan multiple terms in the main index for a prefix query. */
				bDesc = libc.BoolInt32(flags&int32(FTS5INDEX_QUERY_DESC) != 0)
				_fts5SetupPrefixIter(tls, p, bDesc, iPrefixIdx, (**(**TFts5Buffer)(__ccgo_up(bp + 4))).Fp, nToken+int32(1), pColset, bp)
				if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
				} else {
					_fts5IterSetOutputCb(tls, p+44, **(**uintptr)(__ccgo_up(bp)))
					if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
						pSeg = **(**uintptr)(__ccgo_up(bp)) + 80 + uintptr((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaFirst + 1*4))).FiFirst)*104
						if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf != 0 {
							(*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFts5Iter)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FxSetOutputs})))(tls, **(**uintptr)(__ccgo_up(bp)), pSeg)
						}
					}
				}
			}
		}
		if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 {
			_fts5IterClose(tls, **(**uintptr)(__ccgo_up(bp)))
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			_fts5IndexCloseReader(tls, p)
		}
		**(**uintptr)(__ccgo_up(ppIter)) = **(**uintptr)(__ccgo_up(bp))
		_sqlite3Fts5BufferFree(tls, bp+4)
	}
	return _fts5IndexReturn(tls, p)
}

// C documentation
//
//	/*
//	** The %_data table is completely empty when this function is called. This
//	** function populates it with the initial structure objects for each index,
//	** and the initial version of the "averages" record (a zero-byte blob).
//	*/
func _sqlite3Fts5IndexReinit(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var pTmp uintptr
	var _ /* uFts at bp+0 */ struct {
		F__ccgo_align [0]uint32
		FtmpSpace     [0][44]Tu8
		FsFts         TFts5Structure
		F__ccgo_pad2  [16]byte
	}
	_ = pTmp
	_fts5StructureInvalidate(tls, p)
	_fts5IndexDiscardData(tls, p)
	pTmp = bp
	libc.Xmemset(tls, bp, 0, uint32(44))
	if (*TFts5Config)(unsafe.Pointer((*TFts5Index)(unsafe.Pointer(p)).FpConfig)).FbContentlessDelete != 0 {
		(*TFts5Structure)(unsafe.Pointer(pTmp)).FnOriginCntr = uint64(1)
	}
	_fts5DataWrite(tls, p, int64(FTS5_AVERAGES_ROWID), __ccgo_ts+1711, 0)
	_fts5StructureWrite(tls, p, pTmp)
	return _fts5IndexReturn(tls, p)
}

// C documentation
//
//	/*
//	** Set the 32-bit cookie value stored at the start of all structure
//	** records to the value passed as the second argument.
//	**
//	** Return SQLITE_OK if successful, or an SQLite error code if an error
//	** occurs.
//	*/
func _sqlite3Fts5IndexSetCookie(tls *libc.TLS, p uintptr, iNew int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pConfig uintptr
	var rc int32
	var _ /* aCookie at bp+0 */ [4]Tu8
	var _ /* pBlob at bp+4 */ uintptr
	_, _ = pConfig, rc                                  /* Return code */
	pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig /* Binary representation of iNew */
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	_sqlite3Fts5Put32(tls, bp, iNew)
	rc = Xsqlite3_blob_open(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Index)(unsafe.Pointer(p)).FzDataTbl, __ccgo_ts+40179, int64(FTS5_STRUCTURE_ROWID), int32(1), bp+4)
	if rc == SQLITE_OK {
		Xsqlite3_blob_write(tls, **(**uintptr)(__ccgo_up(bp + 4)), bp, int32(4), 0)
		rc = Xsqlite3_blob_close(tls, **(**uintptr)(__ccgo_up(bp + 4)))
	}
	return rc
}

// C documentation
//
//	/*
//	** Return true if the value passed as the only argument is an
//	** fts5_locale() value.
//	*/
func _sqlite3Fts5IsLocaleValue(tls *libc.TLS, pConfig uintptr, pVal uintptr) (r int32) {
	var nBlob, ret int32
	var pBlob uintptr
	_, _, _ = nBlob, pBlob, ret
	ret = 0
	if Xsqlite3_value_type(tls, pVal) == int32(SQLITE_BLOB) {
		/* Call sqlite3_value_bytes() after sqlite3_value_blob() in this case.
		 ** If the blob was created using zeroblob(), then sqlite3_value_blob()
		 ** may call malloc(). If this malloc() fails, then the values returned
		 ** by both value_blob() and value_bytes() will be 0. If value_bytes() were
		 ** called first, then the NULL pointer returned by value_blob() might
		 ** be dereferenced.  */
		pBlob = Xsqlite3_value_blob(tls, pVal)
		nBlob = Xsqlite3_value_bytes(tls, pVal)
		if nBlob > libc.Int32FromInt64(16) && 0 == libc.Xmemcmp(tls, pBlob, (*TFts5Config)(unsafe.Pointer(pConfig)).FpGlobal+56, uint32(libc.Int32FromInt64(16))) {
			ret = int32(1)
		}
	}
	return ret
}

// C documentation
//
//	/*
//	** Move to the next matching term/rowid. Used by the fts5vocab module.
//	*/
func _sqlite3Fts5IterNextScan(tls *libc.TLS, pIndexIter uintptr) (r int32) {
	var p, pIter, pSeg uintptr
	_, _, _ = p, pIter, pSeg
	pIter = pIndexIter
	p = (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex
	_fts5MultiIterNext(tls, p, pIter, 0, 0)
	if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK {
		pSeg = pIter + 80 + uintptr((**(**TFts5CResult)(__ccgo_up((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*104
		if (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf != 0 && int32(**(**Tu8)(__ccgo_up((*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fp))) != int32('0') {
			_fts5DataRelease(tls, (*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf)
			(*TFts5SegIter)(unsafe.Pointer(pSeg)).FpLeaf = uintptr(0)
			(*TFts5Iter)(unsafe.Pointer(pIter)).Fbase.FbEof = uint8(1)
		}
	}
	return _fts5IndexReturn(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex)
}

// C documentation
//
//	/*
//	** Attempt to instantiate the tokenizer.
//	*/
func _sqlite3Fts5LoadTokenizer(tls *libc.TLS, pConfig uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var azArg, pMod, xCreate, v1 uintptr
	var nArg, rc, v3 int32
	_, _, _, _, _, _, _ = azArg, nArg, pMod, rc, xCreate, v1, v3
	azArg = (*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FazArg
	nArg = (*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FnArg
	pMod = uintptr(0)
	rc = SQLITE_OK
	if nArg == 0 {
		v1 = uintptr(0)
	} else {
		v1 = **(**uintptr)(__ccgo_up(azArg))
	}
	pMod = _fts5LocateTokenizer(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).FpGlobal, v1)
	if pMod == uintptr(0) {
		rc = int32(SQLITE_ERROR)
		_sqlite3Fts5ConfigErrmsg(tls, pConfig, __ccgo_ts+41680, libc.VaList(bp+8, **(**uintptr)(__ccgo_up(azArg))))
	} else {
		xCreate = uintptr(0)
		if (*TFts5TokenizerModule)(unsafe.Pointer(pMod)).FbV2Native != 0 {
			xCreate = (*TFts5TokenizerModule)(unsafe.Pointer(pMod)).Fx2.FxCreate
			(*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FpApi2 = pMod + 24
		} else {
			(*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FpApi1 = pMod + 12
			xCreate = (*TFts5TokenizerModule)(unsafe.Pointer(pMod)).Fx1.FxCreate
		}
		if azArg != 0 {
			v1 = azArg + 1*4
		} else {
			v1 = uintptr(0)
		}
		if nArg != 0 {
			v3 = nArg - int32(1)
		} else {
			v3 = 0
		}
		rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xCreate})))(tls, (*TFts5TokenizerModule)(unsafe.Pointer(pMod)).FpUserData, v1, v3, pConfig+76)
		if rc != SQLITE_OK {
			if rc != int32(SQLITE_NOMEM) {
				_sqlite3Fts5ConfigErrmsg(tls, pConfig, __ccgo_ts+41702, 0)
			}
		} else {
			if (*TFts5TokenizerModule)(unsafe.Pointer(pMod)).FbV2Native == 0 {
				(*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FePattern = _sqlite3Fts5TokenizerPattern(tls, (*TFts5TokenizerModule)(unsafe.Pointer(pMod)).Fx1.FxCreate, (*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FpTok)
			}
		}
	}
	if rc != SQLITE_OK {
		(*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FpApi1 = uintptr(0)
		(*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FpApi2 = uintptr(0)
		(*TFts5Config)(unsafe.Pointer(pConfig)).Ft.FpTok = uintptr(0)
	}
	return rc
}

func _sqlite3Fts5MallocZero(tls *libc.TLS, pRc uintptr, nByte Tsqlite3_int64) (r uintptr) {
	var pRet uintptr
	_ = pRet
	pRet = uintptr(0)
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK {
		pRet = Xsqlite3_malloc64(tls, uint64(nByte))
		if pRet == uintptr(0) {
			if nByte > 0 {
				**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
			}
		} else {
			libc.Xmemset(tls, pRet, 0, uint32(nByte))
		}
	}
	return pRet
}

func _sqlite3Fts5ParseColset(tls *libc.TLS, pParse uintptr, pColset uintptr, p uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iCol int32
	var pConfig, pRet, z uintptr
	_, _, _, _ = iCol, pConfig, pRet, z
	pRet = uintptr(0) /* Dequoted copy of token p */
	z = _sqlite3Fts5Strndup(tls, pParse+8, (*TFts5Token)(unsafe.Pointer(p)).Fp, (*TFts5Token)(unsafe.Pointer(p)).Fn)
	if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc == SQLITE_OK {
		pConfig = (*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig
		_sqlite3Fts5Dequote(tls, z)
		iCol = 0
		for {
			if !(iCol < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
				break
			}
			if 0 == Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FazCol + uintptr(iCol)*4)), z) {
				break
			}
			goto _1
		_1:
			;
			iCol = iCol + 1
		}
		if iCol == (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol {
			_sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+13123, libc.VaList(bp+8, z))
		} else {
			pRet = _fts5ParseColset(tls, pParse, pColset, iCol)
		}
		Xsqlite3_free(tls, z)
	}
	if pRet == uintptr(0) {
		Xsqlite3_free(tls, pColset)
	}
	return pRet
}

// C documentation
//
//	/*
//	** Allocate and return an Fts5Colset object specifying the inverse of
//	** the colset passed as the second argument. Free the colset passed
//	** as the second argument before returning.
//	*/
func _sqlite3Fts5ParseColsetInvert(tls *libc.TLS, pParse uintptr, p uintptr) (r uintptr) {
	var i, iOld, nCol, v2 int32
	var pRet, v3 uintptr
	_, _, _, _, _, _ = i, iOld, nCol, pRet, v2, v3
	nCol = (*TFts5Config)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig)).FnCol
	pRet = _sqlite3Fts5MallocZero(tls, pParse+8, int64(libc.Uint32FromInt64(8)*uint32((nCol+libc.Int32FromInt32(1)+libc.Int32FromInt32(2))/libc.Int32FromInt32(2))))
	if pRet != 0 {
		iOld = 0
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			if iOld >= (*TFts5Colset)(unsafe.Pointer(p)).FnCol || *(*int32)(unsafe.Pointer(p + 4 + uintptr(iOld)*4)) != i {
				v3 = pRet
				v2 = *(*int32)(unsafe.Pointer(v3))
				*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
				*(*int32)(unsafe.Pointer(pRet + 4 + uintptr(v2)*4)) = i
			} else {
				iOld = iOld + 1
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	Xsqlite3_free(tls, p)
	return pRet
}

func _sqlite3Fts5ParseImplicitAnd(tls *libc.TLS, pParse uintptr, pLeft uintptr, pRight uintptr) (r uintptr) {
	var ap, pPrev, pRet uintptr
	_, _, _ = ap, pPrev, pRet
	pRet = uintptr(0)
	if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc != 0 {
		_sqlite3Fts5ParseNodeFree(tls, pLeft)
		_sqlite3Fts5ParseNodeFree(tls, pRight)
	} else {
		if (*TFts5ExprNode)(unsafe.Pointer(pLeft)).FeType == int32(FTS5_AND) {
			pPrev = *(*uintptr)(unsafe.Pointer(pLeft + 40 + uintptr((*TFts5ExprNode)(unsafe.Pointer(pLeft)).FnChild-int32(1))*4))
		} else {
			pPrev = pLeft
		}
		if (*TFts5ExprNode)(unsafe.Pointer(pRight)).FeType == FTS5_EOF {
			_sqlite3Fts5ParseNodeFree(tls, pRight)
			pRet = pLeft
			(*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase = (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase - 1
		} else {
			if (*TFts5ExprNode)(unsafe.Pointer(pPrev)).FeType == FTS5_EOF {
				if pPrev == pLeft {
					pRet = pRight
				} else {
					*(*uintptr)(unsafe.Pointer(pLeft + 40 + uintptr((*TFts5ExprNode)(unsafe.Pointer(pLeft)).FnChild-int32(1))*4)) = pRight
					pRet = pLeft
				}
				ap = (*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr((*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-int32(1)-(*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pRight)).FpNear)).FnPhrase)*4
				libc.Xmemmove(tls, ap, ap+1*4, uint32(4)*uint32((*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pRight)).FpNear)).FnPhrase))
				(*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase = (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase - 1
				_sqlite3Fts5ParseNodeFree(tls, pPrev)
			} else {
				pRet = _sqlite3Fts5ParseNode(tls, pParse, int32(FTS5_AND), pLeft, pRight, uintptr(0))
			}
		}
	}
	return pRet
}

// C documentation
//
//	/*
//	** Token pTok has appeared in a MATCH expression where the NEAR operator
//	** is expected. If token pTok does not contain "NEAR", store an error
//	** in the pParse object.
//	*/
func _sqlite3Fts5ParseNear(tls *libc.TLS, pParse uintptr, pTok uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	if (*TFts5Token)(unsafe.Pointer(pTok)).Fn != int32(4) || libc.Xmemcmp(tls, __ccgo_ts+39842, (*TFts5Token)(unsafe.Pointer(pTok)).Fp, uint32(4)) != 0 {
		_sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+38355, libc.VaList(bp+8, (*TFts5Token)(unsafe.Pointer(pTok)).Fn, (*TFts5Token)(unsafe.Pointer(pTok)).Fp))
	}
}

// C documentation
//
//	/*
//	** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
//	** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
//	** appended to it and the results returned.
//	**
//	** If an OOM error occurs, both the pNear and pPhrase objects are freed and
//	** NULL returned.
//	*/
func _sqlite3Fts5ParseNearset(tls *libc.TLS, pParse uintptr, pNear uintptr, pPhrase uintptr) (r uintptr) {
	var SZALLOC, nNew, v1 int32
	var nByte, nByte1 Tsqlite3_int64
	var pLast, pRet, v2 uintptr
	_, _, _, _, _, _, _, _ = SZALLOC, nByte, nByte1, nNew, pLast, pRet, v1, v2
	SZALLOC = int32(8)
	pRet = uintptr(0)
	if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc == SQLITE_OK {
		if pNear == uintptr(0) {
			nByte = int64(uint32(libc.UintptrFromInt32(0)+12) + uint32(SZALLOC+libc.Int32FromInt32(1))*libc.Uint32FromInt64(4))
			pRet = Xsqlite3_malloc64(tls, uint64(nByte))
			if pRet == uintptr(0) {
				(*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM)
			} else {
				libc.Xmemset(tls, pRet, 0, uint32(nByte))
			}
		} else {
			if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase%SZALLOC == 0 {
				nNew = (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase + SZALLOC
				nByte1 = int64(uint32(libc.UintptrFromInt32(0)+12) + uint32(nNew+libc.Int32FromInt32(1))*libc.Uint32FromInt64(4))
				pRet = Xsqlite3_realloc64(tls, pNear, uint64(nByte1))
				if pRet == uintptr(0) {
					(*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM)
				}
			} else {
				pRet = pNear
			}
		}
	}
	if pRet == uintptr(0) {
		_sqlite3Fts5ParseNearsetFree(tls, pNear)
		_sqlite3Fts5ParsePhraseFree(tls, pPhrase)
	} else {
		if (*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase > 0 {
			pLast = *(*uintptr)(unsafe.Pointer(pRet + 12 + uintptr((*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase-int32(1))*4))
			if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm == 0 {
				_fts5ExprPhraseFree(tls, pPhrase)
				(*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase = (*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase - 1
				(*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase = (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase - 1
				pPhrase = pLast
			} else {
				if (*TFts5ExprPhrase)(unsafe.Pointer(pLast)).FnTerm == 0 {
					_fts5ExprPhraseFree(tls, pLast)
					**(**uintptr)(__ccgo_up((*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr((*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-int32(2))*4)) = pPhrase
					(*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase = (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase - 1
					(*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase = (*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase - 1
				}
			}
		}
		v2 = pRet + 8
		v1 = *(*int32)(unsafe.Pointer(v2))
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		*(*uintptr)(unsafe.Pointer(pRet + 12 + uintptr(v1)*4)) = pPhrase
	}
	return pRet
}

// C documentation
//
//	/*
//	** Allocate and return a new expression object. If anything goes wrong (i.e.
//	** OOM error), leave an error code in pParse and return NULL.
//	*/
func _sqlite3Fts5ParseNode(tls *libc.TLS, pParse uintptr, eType int32, pLeft uintptr, pRight uintptr, pNear uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iPhrase, nChild int32
	var nByte Tsqlite3_int64
	var pPhrase, pRet, v2 uintptr
	_, _, _, _, _, _ = iPhrase, nByte, nChild, pPhrase, pRet, v2
	pRet = uintptr(0)
	if (*TFts5Parse)(unsafe.Pointer(pParse)).Frc == SQLITE_OK {
		nChild = 0 /* Bytes of space to allocate for this node */
		if eType == int32(FTS5_STRING) && pNear == uintptr(0) {
			return uintptr(0)
		}
		if eType != int32(FTS5_STRING) && pLeft == uintptr(0) {
			return pRight
		}
		if eType != int32(FTS5_STRING) && pRight == uintptr(0) {
			return pLeft
		}
		if eType == int32(FTS5_STRING) && (*TFts5Parse)(unsafe.Pointer(pParse)).FbPhraseToAnd != 0 && (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12)))).FnTerm > int32(1) {
			pRet = _fts5ParsePhraseToAnd(tls, pParse, pNear)
		} else {
			if eType == int32(FTS5_NOT) {
				nChild = int32(2)
			} else {
				if eType == int32(FTS5_AND) || eType == int32(FTS5_OR) {
					nChild = int32(2)
					if (*TFts5ExprNode)(unsafe.Pointer(pLeft)).FeType == eType {
						nChild = nChild + ((*TFts5ExprNode)(unsafe.Pointer(pLeft)).FnChild - int32(1))
					}
					if (*TFts5ExprNode)(unsafe.Pointer(pRight)).FeType == eType {
						nChild = nChild + ((*TFts5ExprNode)(unsafe.Pointer(pRight)).FnChild - int32(1))
					}
				}
			}
			nByte = int64(uint32(libc.UintptrFromInt32(0)+40) + uint32(nChild)*libc.Uint32FromInt64(4))
			pRet = _sqlite3Fts5MallocZero(tls, pParse+8, nByte)
			if pRet != 0 {
				(*TFts5ExprNode)(unsafe.Pointer(pRet)).FeType = eType
				(*TFts5ExprNode)(unsafe.Pointer(pRet)).FpNear = pNear
				_fts5ExprAssignXNext(tls, pRet)
				if eType == int32(FTS5_STRING) {
					iPhrase = 0
					for {
						if !(iPhrase < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) {
							break
						}
						(*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(iPhrase)*4)))).FpNode = pRet
						if (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 12 + uintptr(iPhrase)*4)))).FnTerm == 0 {
							(*TFts5ExprNode)(unsafe.Pointer(pRet)).FxNext = uintptr(0)
							(*TFts5ExprNode)(unsafe.Pointer(pRet)).FeType = FTS5_EOF
						}
						goto _1
					_1:
						;
						iPhrase = iPhrase + 1
					}
					if (*TFts5Config)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig)).FeDetail != FTS5_DETAIL_FULL {
						pPhrase = *(*uintptr)(unsafe.Pointer(pNear + 12))
						if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase != int32(1) || (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm > int32(1) || (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm > 0 && (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 20))).FbFirst != 0 {
							if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase == int32(1) {
								v2 = __ccgo_ts + 39929
							} else {
								v2 = __ccgo_ts + 39842
							}
							_sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+39936, libc.VaList(bp+8, v2))
							_sqlite3Fts5ParseNodeFree(tls, pRet)
							pRet = uintptr(0)
							pNear = uintptr(0)
						}
					}
				} else {
					_fts5ExprAddChildren(tls, pRet, pLeft)
					_fts5ExprAddChildren(tls, pRet, pRight)
					v2 = libc.UintptrFromInt32(0)
					pRight = v2
					pLeft = v2
					if (*TFts5ExprNode)(unsafe.Pointer(pRet)).FiHeight > int32(SQLITE_FTS5_MAX_EXPR_DEPTH) {
						_sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+39986, libc.VaList(bp+8, int32(SQLITE_FTS5_MAX_EXPR_DEPTH)))
						_sqlite3Fts5ParseNodeFree(tls, pRet)
						pRet = uintptr(0)
					}
				}
			}
		}
	}
	if pRet == uintptr(0) {
		_sqlite3Fts5ParseNodeFree(tls, pLeft)
		_sqlite3Fts5ParseNodeFree(tls, pRight)
		_sqlite3Fts5ParseNearsetFree(tls, pNear)
	}
	return pRet
}

// C documentation
//
//	/*
//	** This function is called by the parser to process a string token. The
//	** string may or may not be quoted. In any case it is tokenized and a
//	** phrase object consisting of all tokens returned.
//	*/
func _sqlite3Fts5ParseTerm(tls *libc.TLS, pParse uintptr, pAppend uintptr, pToken uintptr, bPrefix int32) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var flags, n, rc, v1 int32
	var pConfig uintptr
	var v3 bool
	var _ /* sCtx at bp+0 */ TTokenCtx
	var _ /* z at bp+12 */ uintptr
	_, _, _, _, _, _ = flags, n, pConfig, rc, v1, v3
	pConfig = (*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig /* Tokenize return code */
	**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
	libc.Xmemset(tls, bp, 0, uint32(12))
	(**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase = pAppend
	(**(**TTokenCtx)(__ccgo_up(bp))).FpConfig = pConfig
	rc = _fts5ParseStringFromToken(tls, pToken, bp+12)
	if rc == SQLITE_OK {
		if bPrefix != 0 {
			v1 = int32(FTS5_TOKENIZE_PREFIX)
		} else {
			v1 = 0
		}
		flags = int32(FTS5_TOKENIZE_QUERY) | v1
		_sqlite3Fts5Dequote(tls, **(**uintptr)(__ccgo_up(bp + 12)))
		n = int32(libc.Xstrlen(tls, **(**uintptr)(__ccgo_up(bp + 12))))
		rc = _sqlite3Fts5Tokenize(tls, pConfig, flags, **(**uintptr)(__ccgo_up(bp + 12)), n, bp, __ccgo_fp(_fts5ParseTokenize))
	}
	Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 12)))
	if v3 = rc != 0; !v3 {
		v1 = (**(**TTokenCtx)(__ccgo_up(bp))).Frc
		rc = v1
	}
	if v3 || v1 != 0 {
		(*TFts5Parse)(unsafe.Pointer(pParse)).Frc = rc
		_fts5ExprPhraseFree(tls, (**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase)
		(**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase = uintptr(0)
	} else {
		if pAppend == uintptr(0) {
			if _parseGrowPhraseArray(tls, pParse) != 0 {
				_fts5ExprPhraseFree(tls, (**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase)
				return uintptr(0)
			}
			(*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase = (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase + 1
		}
		if (**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase == uintptr(0) {
			/* This happens when parsing a token or quoted phrase that contains
			 ** no token characters at all. (e.g ... MATCH '""'). */
			(**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase = _sqlite3Fts5MallocZero(tls, pParse+8, int64(uint32(libc.UintptrFromInt32(0)+20)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(24)))
		} else {
			if (*TFts5ExprPhrase)(unsafe.Pointer((**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase)).FnTerm != 0 {
				(*(*TFts5ExprTerm)(unsafe.Pointer((**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase + 20 + uintptr((*TFts5ExprPhrase)(unsafe.Pointer((**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase)).FnTerm-int32(1))*24))).FbPrefix = uint8(bPrefix)
			}
		}
		**(**uintptr)(__ccgo_up((*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr((*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-int32(1))*4)) = (**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase
	}
	return (**(**TTokenCtx)(__ccgo_up(bp))).FpPhrase
}

// C documentation
//
//	/* The main parser program.
//	** The first argument is a pointer to a structure obtained from
//	** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
//	** The second argument is the major token number.  The third is
//	** the minor token.  The fourth optional argument is whatever the
//	** user wants (and specified in the grammar) and is available for
//	** use by the action routines.
//	**
//	** Inputs:
//	** <ul>
//	** <li> A pointer to the parser (an opaque structure.)
//	** <li> The major token number.
//	** <li> The minor token number.
//	** <li> An option argument of a grammar-specified type.
//	** </ul>
//	**
//	** Outputs:
//	** None.
//	*/
func _sqlite3Fts5Parser(tls *libc.TLS, fts5yyp uintptr, fts5yymajor int32, fts5yyminor TFts5Token, pParse uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var fts5yyact uint8
	var fts5yypParser uintptr
	var fts5yyruleno uint32
	var _ /* fts5yyminorunion at bp+0 */ Tfts5YYMINORTYPE
	_, _, _ = fts5yyact, fts5yypParser, fts5yyruleno /* The parser action. */
	fts5yypParser = fts5yyp                          /* The parser */
	(*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).FpParse = pParse
	fts5yyact = (*Tfts5yyStackEntry)(unsafe.Pointer((*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos)).Fstateno
	for int32(1) != 0 { /* Exit by "break" */
		fts5yyact = _fts5yy_find_shift_action(tls, uint8(fts5yymajor), fts5yyact)
		if int32(fts5yyact) >= int32(fts5YY_MIN_REDUCE) {
			fts5yyruleno = uint32(int32(fts5yyact) - int32(fts5YY_MIN_REDUCE)) /* Reduce by this rule */
			/* Check that the stack is large enough to grow by a single entry
			 ** if the RHS of the rule is empty.  This ensures that there is room
			 ** enough on the stack to push the LHS value */
			if int32(_fts5yyRuleInfoNRhs[fts5yyruleno]) == 0 {
				if (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos >= (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystackEnd {
					if int32(1) != 0 {
						_fts5yyStackOverflow(tls, fts5yypParser)
						break
					}
				}
			}
			fts5yyact = _fts5yy_reduce(tls, fts5yypParser, fts5yyruleno, fts5yymajor, fts5yyminor)
		} else {
			if int32(fts5yyact) <= int32(fts5YY_MAX_SHIFTREDUCE) {
				_fts5yy_shift(tls, fts5yypParser, fts5yyact, uint8(fts5yymajor), fts5yyminor)
				break
			} else {
				if int32(fts5yyact) == int32(fts5YY_ACCEPT_ACTION) {
					(*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos -= 12
					_fts5yy_accept(tls, fts5yypParser)
					return
				} else {
					*(*TFts5Token)(unsafe.Pointer(bp)) = fts5yyminor
					/* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
					 ** do any kind of error recovery.  Instead, simply invoke the syntax
					 ** error routine and continue going as if nothing had happened.
					 **
					 ** Applications can set this macro (for example inside %include) if
					 ** they intend to abandon the parse upon the first syntax error seen.
					 */
					_fts5yy_syntax_error(tls, fts5yypParser, fts5yymajor, fts5yyminor)
					_fts5yy_destructor(tls, fts5yypParser, uint8(fts5yymajor), bp)
					break
				}
			}
		}
	}
	return
}

// C documentation
//
//	/*
//	** Clear all secondary memory allocations from the parser
//	*/
func _sqlite3Fts5ParserFinalize(tls *libc.TLS, p uintptr) {
	var fts5yytos, pParser uintptr
	_, _ = fts5yytos, pParser
	pParser = p
	/* In-lined version of calling fts5yy_pop_parser_stack() for each
	 ** element left in the stack */
	fts5yytos = (*Tfts5yyParser)(unsafe.Pointer(pParser)).Ffts5yytos
	for fts5yytos > (*Tfts5yyParser)(unsafe.Pointer(pParser)).Ffts5yystack {
		if int32((*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fmajor) >= int32(fts5YY_MIN_DSTRCTR) {
			_fts5yy_destructor(tls, pParser, (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fmajor, fts5yytos+4)
		}
		fts5yytos -= 12
	}
}

func _sqlite3Fts5PoslistReaderInit(tls *libc.TLS, a uintptr, n int32, pIter uintptr) (r int32) {
	libc.Xmemset(tls, pIter, 0, uint32(24))
	(*TFts5PoslistReader)(unsafe.Pointer(pIter)).Fa = a
	(*TFts5PoslistReader)(unsafe.Pointer(pIter)).Fn = n
	_sqlite3Fts5PoslistReaderNext(tls, pIter)
	return int32((*TFts5PoslistReader)(unsafe.Pointer(pIter)).FbEof)
}

// C documentation
//
//	/*
//	** Advance the iterator object passed as the only argument. Return true
//	** if the iterator reaches EOF, or false otherwise.
//	*/
func _sqlite3Fts5PoslistReaderNext(tls *libc.TLS, pIter uintptr) (r int32) {
	if _sqlite3Fts5PoslistNext64(tls, (*TFts5PoslistReader)(unsafe.Pointer(pIter)).Fa, (*TFts5PoslistReader)(unsafe.Pointer(pIter)).Fn, pIter+8, pIter+16) != 0 {
		(*TFts5PoslistReader)(unsafe.Pointer(pIter)).FbEof = uint8(1)
	}
	return int32((*TFts5PoslistReader)(unsafe.Pointer(pIter)).FbEof)
}

// C documentation
//
//	/*
//	** Append position iPos to the position list being accumulated in buffer
//	** pBuf, which must be already be large enough to hold the new data.
//	** The previous position written to this list is *piPrev. *piPrev is set
//	** to iPos before returning.
//	*/
func _sqlite3Fts5PoslistSafeAppend(tls *libc.TLS, pBuf uintptr, piPrev uintptr, iPos Ti64) {
	var v1 int32
	var v2 uintptr
	_, _ = v1, v2
	if iPos >= **(**Ti64)(__ccgo_up(piPrev)) {
		if iPos&_colmask != **(**Ti64)(__ccgo_up(piPrev))&_colmask {
			v2 = pBuf + 4
			v1 = *(*int32)(unsafe.Pointer(v2))
			*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
			**(**Tu8)(__ccgo_up((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp + uintptr(v1))) = uint8(1)
			**(**int32)(__ccgo_up(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(iPos>>libc.Int32FromInt32(32)))
			**(**Ti64)(__ccgo_up(piPrev)) = iPos & _colmask
		}
		**(**int32)(__ccgo_up(pBuf + 4)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(iPos-**(**Ti64)(__ccgo_up(piPrev))+int64(2)))
		**(**Ti64)(__ccgo_up(piPrev)) = iPos
	}
}

// C documentation
//
//	/*
//	** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
//	*/
func _sqlite3Fts5StorageClose(tls *libc.TLS, p uintptr) (r int32) {
	var i, rc int32
	_, _ = i, rc
	rc = SQLITE_OK
	if p != 0 {
		/* Finalize all SQL statements */
		i = 0
		for {
			if !(i < int32(libc.Uint32FromInt64(48)/libc.Uint32FromInt64(4))) {
				break
			}
			Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(p + 32 + uintptr(i)*4)))
			goto _1
		_1:
			;
			i = i + 1
		}
		Xsqlite3_free(tls, p)
	}
	return rc
}

// C documentation
//
//	/*
//	** Insert a new row into the FTS content table.
//	*/
func _sqlite3Fts5StorageContentInsert(tls *libc.TLS, p uintptr, bReplace int32, apVal uintptr, piRowid uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var bUnindexed, i, iLoc, rc int32
	var pConfig, pVal uintptr
	var _ /* nLoc at bp+16 */ int32
	var _ /* nText at bp+12 */ int32
	var _ /* pInsert at bp+0 */ uintptr
	var _ /* pLoc at bp+8 */ uintptr
	var _ /* pText at bp+4 */ uintptr
	_, _, _, _, _, _ = bUnindexed, i, iLoc, pConfig, pVal, rc
	pConfig = (*TFts5Storage)(unsafe.Pointer(p)).FpConfig
	rc = SQLITE_OK
	/* Insert the new row into the %_content table. */
	if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent != FTS5_CONTENT_NORMAL && (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent != int32(FTS5_CONTENT_UNINDEXED) {
		if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(apVal + 1*4))) == int32(SQLITE_INTEGER) {
			**(**Ti64)(__ccgo_up(piRowid)) = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(apVal + 1*4)))
		} else {
			rc = _fts5StorageNewRowid(tls, p, piRowid)
		}
	} else {
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* Counter variable */
		rc = _fts5StorageGetStmt(tls, p, int32(FTS5_STMT_INSERT_CONTENT)+bReplace, bp, uintptr(0))
		if **(**uintptr)(__ccgo_up(bp)) != 0 {
			Xsqlite3_clear_bindings(tls, **(**uintptr)(__ccgo_up(bp)))
		}
		/* Bind the rowid value */
		Xsqlite3_bind_value(tls, **(**uintptr)(__ccgo_up(bp)), int32(1), **(**uintptr)(__ccgo_up(apVal + 1*4)))
		/* Loop through values for user-defined columns. i=2 is the leftmost
		 ** user-defined column. As is column 1 of pSavedRow.  */
		i = int32(2)
		for {
			if !(rc == SQLITE_OK && i <= (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol+int32(1)) {
				break
			}
			bUnindexed = int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr(i-int32(2)))))
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == FTS5_CONTENT_NORMAL || bUnindexed != 0 {
				pVal = **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4))
				if Xsqlite3_value_nochange(tls, pVal) != 0 && (*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow != 0 {
					/* This is an UPDATE statement, and user-defined column (i-2) was not
					 ** modified.  Retrieve the value from Fts5Storage.pSavedRow.  */
					pVal = Xsqlite3_column_value(tls, (*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow, i-int32(1))
					if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale != 0 && bUnindexed == 0 {
						Xsqlite3_bind_value(tls, **(**uintptr)(__ccgo_up(bp)), (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol+i, Xsqlite3_column_value(tls, (*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow, (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol+i-int32(1)))
					}
				} else {
					if _sqlite3Fts5IsLocaleValue(tls, pConfig, pVal) != 0 {
						**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
						**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
						**(**int32)(__ccgo_up(bp + 12)) = 0
						**(**int32)(__ccgo_up(bp + 16)) = 0
						rc = _sqlite3Fts5DecodeLocaleValue(tls, pVal, bp+4, bp+12, bp+8, bp+16)
						if rc == SQLITE_OK {
							Xsqlite3_bind_text(tls, **(**uintptr)(__ccgo_up(bp)), i, **(**uintptr)(__ccgo_up(bp + 4)), **(**int32)(__ccgo_up(bp + 12)), uintptr(-libc.Int32FromInt32(1)))
							if bUnindexed == 0 {
								iLoc = (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol + i
								Xsqlite3_bind_text(tls, **(**uintptr)(__ccgo_up(bp)), iLoc, **(**uintptr)(__ccgo_up(bp + 8)), **(**int32)(__ccgo_up(bp + 16)), uintptr(-libc.Int32FromInt32(1)))
							}
						}
						goto _1
					}
				}
				rc = Xsqlite3_bind_value(tls, **(**uintptr)(__ccgo_up(bp)), i, pVal)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if rc == SQLITE_OK {
			Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp)))
			rc = Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp)))
		}
		**(**Ti64)(__ccgo_up(piRowid)) = Xsqlite3_last_insert_rowid(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb)
	}
	return rc
}

// C documentation
//
//	/*
//	** Insert new entries into the FTS index and %_docsize table.
//	*/
func _sqlite3Fts5StorageIndexInsert(tls *libc.TLS, p uintptr, apVal uintptr, iRowid Ti64) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var iCol int32
	var pConfig, pVal uintptr
	var _ /* buf at bp+16 */ TFts5Buffer
	var _ /* ctx at bp+4 */ TFts5InsertCtx
	var _ /* nLoc at bp+36 */ int32
	var _ /* nText at bp+28 */ int32
	var _ /* pLoc at bp+40 */ uintptr
	var _ /* pText at bp+32 */ uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _ = iCol, pConfig, pVal
	pConfig = (*TFts5Storage)(unsafe.Pointer(p)).FpConfig
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK /* Buffer used to build up %_docsize blob */
	libc.Xmemset(tls, bp+16, 0, uint32(12))
	(**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FpStorage = p
	**(**int32)(__ccgo_up(bp)) = _fts5StorageLoadTotals(tls, p, int32(1))
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5IndexBeginWrite(tls, (*TFts5Storage)(unsafe.Pointer(p)).FpIndex, 0, iRowid)
	}
	(**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol = 0
	for {
		if !(**(**int32)(__ccgo_up(bp)) == SQLITE_OK && (**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
			break
		}
		(**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FszCol = 0
		if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr((**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol)))) == 0 {
			**(**int32)(__ccgo_up(bp + 28)) = 0            /* Size of pText in bytes */
			**(**uintptr)(__ccgo_up(bp + 32)) = uintptr(0) /* Pointer to buffer containing text value */
			**(**int32)(__ccgo_up(bp + 36)) = 0            /* Size of pText in bytes */
			**(**uintptr)(__ccgo_up(bp + 40)) = uintptr(0) /* Pointer to buffer containing text value */
			pVal = **(**uintptr)(__ccgo_up(apVal + uintptr((**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol+int32(2))*4))
			if (*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow != 0 && Xsqlite3_value_nochange(tls, pVal) != 0 {
				pVal = Xsqlite3_column_value(tls, (*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow, (**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol+int32(1))
				if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == FTS5_CONTENT_NORMAL && (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale != 0 {
					iCol = (**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol + int32(1) + (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol
					**(**uintptr)(__ccgo_up(bp + 40)) = Xsqlite3_column_text(tls, (*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow, iCol)
					**(**int32)(__ccgo_up(bp + 36)) = Xsqlite3_column_bytes(tls, (*TFts5Storage)(unsafe.Pointer(p)).FpSavedRow, iCol)
				}
			} else {
				pVal = **(**uintptr)(__ccgo_up(apVal + uintptr((**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol+int32(2))*4))
			}
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale != 0 && _sqlite3Fts5IsLocaleValue(tls, pConfig, pVal) != 0 {
				**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5DecodeLocaleValue(tls, pVal, bp+32, bp+28, bp+40, bp+36)
			} else {
				**(**uintptr)(__ccgo_up(bp + 32)) = Xsqlite3_value_text(tls, pVal)
				**(**int32)(__ccgo_up(bp + 28)) = Xsqlite3_value_bytes(tls, pVal)
			}
			if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
				_sqlite3Fts5SetLocale(tls, pConfig, **(**uintptr)(__ccgo_up(bp + 40)), **(**int32)(__ccgo_up(bp + 36)))
				**(**int32)(__ccgo_up(bp)) = _sqlite3Fts5Tokenize(tls, pConfig, int32(FTS5_TOKENIZE_DOCUMENT), **(**uintptr)(__ccgo_up(bp + 32)), **(**int32)(__ccgo_up(bp + 28)), bp+4, __ccgo_fp(_fts5StorageInsertCallback))
				_sqlite3Fts5ClearLocale(tls, pConfig)
			}
		}
		_sqlite3Fts5BufferAppendVarint(tls, bp, bp+16, int64((**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FszCol))
		**(**Ti64)(__ccgo_up((*TFts5Storage)(unsafe.Pointer(p)).FaTotalSize + uintptr((**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol)*8)) += int64((**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FszCol)
		goto _1
	_1:
		;
		(**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol = (**(**TFts5InsertCtx)(__ccgo_up(bp + 4))).FiCol + 1
	}
	(*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow = (*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow + 1
	/* Write the %_docsize record */
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp)) = _fts5StorageInsertDocsize(tls, p, iRowid, bp+16)
	}
	Xsqlite3_free(tls, (**(**TFts5Buffer)(__ccgo_up(bp + 16))).Fp)
	return **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Check that the contents of the FTS index match that of the %_content
//	** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
//	** some other SQLite error code if an error occurs while attempting to
//	** determine this.
//	*/
func _sqlite3Fts5StorageIntegrity(tls *libc.TLS, p uintptr, iArg int32) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var aColSize, aTotalSize, pConfig, pVal uintptr
	var bUseCksum, i, i1, iCol, rc, rc2 int32
	var _ /* ctx at bp+0 */ TFts5IntegrityCtx
	var _ /* nLoc at bp+48 */ int32
	var _ /* nRow at bp+56 */ Ti64
	var _ /* nRow at bp+64 */ Ti64
	var _ /* nText at bp+40 */ int32
	var _ /* pLoc at bp+44 */ uintptr
	var _ /* pScan at bp+32 */ uintptr
	var _ /* pText at bp+36 */ uintptr
	_, _, _, _, _, _, _, _, _, _ = aColSize, aTotalSize, bUseCksum, i, i1, iCol, pConfig, pVal, rc, rc2
	pConfig = (*TFts5Storage)(unsafe.Pointer(p)).FpConfig
	rc = SQLITE_OK
	libc.Xmemset(tls, bp, 0, uint32(32))
	(**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FpConfig = (*TFts5Storage)(unsafe.Pointer(p)).FpConfig
	aTotalSize = Xsqlite3_malloc64(tls, uint64(uint32((*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)*(libc.Uint32FromInt64(4)+libc.Uint32FromInt64(8))))
	if !(aTotalSize != 0) {
		return int32(SQLITE_NOMEM)
	}
	aColSize = aTotalSize + uintptr((*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)*8
	libc.Xmemset(tls, aTotalSize, 0, uint32(8)*uint32((*TFts5Config)(unsafe.Pointer(pConfig)).FnCol))
	bUseCksum = libc.BoolInt32((*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == FTS5_CONTENT_NORMAL || (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == int32(FTS5_CONTENT_EXTERNAL) && iArg != 0)
	if bUseCksum != 0 {
		/* Generate the expected index checksum based on the contents of the
		 ** %_content table. This block stores the checksum in ctx.cksum. */
		rc = _fts5StorageGetStmt(tls, p, int32(FTS5_STMT_SCAN), bp+32, uintptr(0))
		if rc == SQLITE_OK {
			for int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 32))) {
				(**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FiRowid = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp + 32)), 0)
				(**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FszCol = 0
				if (*TFts5Config)(unsafe.Pointer(pConfig)).FbColumnsize != 0 {
					rc = _sqlite3Fts5StorageDocsize(tls, p, (**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FiRowid, aColSize)
				}
				if rc == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail == int32(FTS5_DETAIL_NONE) {
					rc = _sqlite3Fts5TermsetNew(tls, bp+24)
				}
				i = 0
				for {
					if !(rc == SQLITE_OK && i < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
						break
					}
					if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr(i)))) == 0 {
						**(**uintptr)(__ccgo_up(bp + 36)) = uintptr(0)
						**(**int32)(__ccgo_up(bp + 40)) = 0
						**(**uintptr)(__ccgo_up(bp + 44)) = uintptr(0)
						**(**int32)(__ccgo_up(bp + 48)) = 0
						pVal = Xsqlite3_column_value(tls, **(**uintptr)(__ccgo_up(bp + 32)), i+int32(1))
						if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == int32(FTS5_CONTENT_EXTERNAL) && _sqlite3Fts5IsLocaleValue(tls, pConfig, pVal) != 0 {
							rc = _sqlite3Fts5DecodeLocaleValue(tls, pVal, bp+36, bp+40, bp+44, bp+48)
						} else {
							if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == FTS5_CONTENT_NORMAL && (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale != 0 {
								iCol = i + int32(1) + (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol
								**(**uintptr)(__ccgo_up(bp + 44)) = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp + 32)), iCol)
								**(**int32)(__ccgo_up(bp + 48)) = Xsqlite3_column_bytes(tls, **(**uintptr)(__ccgo_up(bp + 32)), iCol)
							}
							**(**uintptr)(__ccgo_up(bp + 36)) = Xsqlite3_value_text(tls, pVal)
							**(**int32)(__ccgo_up(bp + 40)) = Xsqlite3_value_bytes(tls, pVal)
						}
						(**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FiCol = i
						(**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FszCol = 0
						if rc == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail == int32(FTS5_DETAIL_COLUMNS) {
							rc = _sqlite3Fts5TermsetNew(tls, bp+24)
						}
						if rc == SQLITE_OK {
							_sqlite3Fts5SetLocale(tls, pConfig, **(**uintptr)(__ccgo_up(bp + 44)), **(**int32)(__ccgo_up(bp + 48)))
							rc = _sqlite3Fts5Tokenize(tls, pConfig, int32(FTS5_TOKENIZE_DOCUMENT), **(**uintptr)(__ccgo_up(bp + 36)), **(**int32)(__ccgo_up(bp + 40)), bp, __ccgo_fp(_fts5StorageIntegrityCallback))
							_sqlite3Fts5ClearLocale(tls, pConfig)
						}
						/* If this is not a columnsize=0 database, check that the number
						 ** of tokens in the value matches the aColSize[] value read from
						 ** the %_docsize table.  */
						if rc == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pConfig)).FbColumnsize != 0 && (**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FszCol != **(**int32)(__ccgo_up(aColSize + uintptr(i)*4)) {
							rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
						}
						**(**Ti64)(__ccgo_up(aTotalSize + uintptr(i)*8)) += int64((**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FszCol)
						if (*TFts5Config)(unsafe.Pointer(pConfig)).FeDetail == int32(FTS5_DETAIL_COLUMNS) {
							_sqlite3Fts5TermsetFree(tls, (**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FpTermset)
							(**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FpTermset = uintptr(0)
						}
					}
					goto _1
				_1:
					;
					i = i + 1
				}
				_sqlite3Fts5TermsetFree(tls, (**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FpTermset)
				(**(**TFts5IntegrityCtx)(__ccgo_up(bp))).FpTermset = uintptr(0)
				if rc != SQLITE_OK {
					break
				}
			}
			rc2 = Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp + 32)))
			if rc == SQLITE_OK {
				rc = rc2
			}
		}
		/* Test that the "totals" (sometimes called "averages") record looks Ok */
		if rc == SQLITE_OK {
			rc = _fts5StorageLoadTotals(tls, p, 0)
			i1 = 0
			for {
				if !(rc == SQLITE_OK && i1 < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
					break
				}
				if **(**Ti64)(__ccgo_up((*TFts5Storage)(unsafe.Pointer(p)).FaTotalSize + uintptr(i1)*8)) != **(**Ti64)(__ccgo_up(aTotalSize + uintptr(i1)*8)) {
					rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
				}
				goto _2
			_2:
				;
				i1 = i1 + 1
			}
		}
		/* Check that the %_docsize and %_content tables contain the expected
		 ** number of rows.  */
		if rc == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == FTS5_CONTENT_NORMAL {
			**(**Ti64)(__ccgo_up(bp + 56)) = 0
			rc = _fts5StorageCount(tls, p, __ccgo_ts+38828, bp+56)
			if rc == SQLITE_OK && **(**Ti64)(__ccgo_up(bp + 56)) != (*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow {
				rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
			}
		}
		if rc == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pConfig)).FbColumnsize != 0 {
			**(**Ti64)(__ccgo_up(bp + 64)) = 0
			rc = _fts5StorageCount(tls, p, __ccgo_ts+39523, bp+64)
			if rc == SQLITE_OK && **(**Ti64)(__ccgo_up(bp + 64)) != (*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow {
				rc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
			}
		}
	}
	/* Pass the expected checksum down to the FTS index module. It will
	 ** verify, amongst other things, that it matches the checksum generated by
	 ** inspecting the index itself.  */
	if rc == SQLITE_OK {
		rc = _sqlite3Fts5IndexIntegrityCheck(tls, (*TFts5Storage)(unsafe.Pointer(p)).FpIndex, (**(**TFts5IntegrityCtx)(__ccgo_up(bp))).Fcksum, bUseCksum)
	}
	Xsqlite3_free(tls, aTotalSize)
	return rc
}

// C documentation
//
//	/*
//	** Open a new Fts5Index handle. If the bCreate argument is true, create
//	** and initialize the underlying tables
//	**
//	** If successful, set *pp to point to the new object and return SQLITE_OK.
//	** Otherwise, set *pp to NULL and return an SQLite error code.
//	*/
func _sqlite3Fts5StorageOpen(tls *libc.TLS, pConfig uintptr, pIndex uintptr, bCreate int32, pp uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, rc int32
	var nByte Tsqlite3_int64
	var p, pDefn, zCols, zDefn, v1 uintptr
	_, _, _, _, _, _, _, _ = i, nByte, p, pDefn, rc, zCols, zDefn, v1
	rc = SQLITE_OK                                                                              /* Bytes of space to allocate */
	nByte = int64(uint32(80) + uint32((*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)*uint32(8)) /* Fts5Storage.aTotalSize[] */
	v1 = Xsqlite3_malloc64(tls, uint64(nByte))
	p = v1
	**(**uintptr)(__ccgo_up(pp)) = v1
	if !(p != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, p, 0, uint32(nByte))
	(*TFts5Storage)(unsafe.Pointer(p)).FaTotalSize = p + 1*80
	(*TFts5Storage)(unsafe.Pointer(p)).FpConfig = pConfig
	(*TFts5Storage)(unsafe.Pointer(p)).FpIndex = pIndex
	if bCreate != 0 {
		if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == FTS5_CONTENT_NORMAL || (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == int32(FTS5_CONTENT_UNINDEXED) {
			i = 0
			zDefn = uintptr(0)
			pDefn = Xsqlite3_str_new(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb)
			Xsqlite3_str_appendf(tls, pDefn, __ccgo_ts+42799, 0)
			i = 0
			for {
				if !(i < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
					break
				}
				if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == FTS5_CONTENT_NORMAL || **(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr(i))) != 0 {
					Xsqlite3_str_appendf(tls, pDefn, __ccgo_ts+42822, libc.VaList(bp+8, i))
				}
				goto _2
			_2:
				;
				i = i + 1
			}
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale != 0 {
				i = 0
				for {
					if !(i < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
						break
					}
					if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr(i)))) == 0 {
						Xsqlite3_str_appendf(tls, pDefn, __ccgo_ts+42828, libc.VaList(bp+8, i))
					}
					goto _3
				_3:
					;
					i = i + 1
				}
			}
			zDefn = Xsqlite3_str_finish(tls, pDefn)
			if zDefn != 0 {
				rc = _sqlite3Fts5CreateTable(tls, pConfig, __ccgo_ts+38828, zDefn, 0, pzErr)
				Xsqlite3_free(tls, zDefn)
			} else {
				rc = int32(SQLITE_NOMEM)
			}
		}
		if rc == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(pConfig)).FbColumnsize != 0 {
			zCols = __ccgo_ts + 42834
			if (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete != 0 {
				zCols = __ccgo_ts + 42866
			}
			rc = _sqlite3Fts5CreateTable(tls, pConfig, __ccgo_ts+39523, zCols, 0, pzErr)
		}
		if rc == SQLITE_OK {
			rc = _sqlite3Fts5CreateTable(tls, pConfig, __ccgo_ts+41837, __ccgo_ts+42914, int32(1), pzErr)
		}
		if rc == SQLITE_OK {
			rc = _sqlite3Fts5StorageConfigValue(tls, p, __ccgo_ts+39705, uintptr(0), int32(FTS5_CURRENT_VERSION))
		}
	}
	if rc != 0 {
		_sqlite3Fts5StorageClose(tls, p)
		**(**uintptr)(__ccgo_up(pp)) = uintptr(0)
	}
	return rc
}

func _sqlite3Fts5StorageRebuild(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var iCol, rc2 int32
	var iRowid Ti64
	var pConfig, pVal uintptr
	var _ /* buf at bp+0 */ TFts5Buffer
	var _ /* ctx at bp+16 */ TFts5InsertCtx
	var _ /* nLoc at bp+40 */ int32
	var _ /* nText at bp+32 */ int32
	var _ /* pLoc at bp+44 */ uintptr
	var _ /* pScan at bp+12 */ uintptr
	var _ /* pText at bp+36 */ uintptr
	var _ /* rc at bp+28 */ int32
	_, _, _, _, _ = iCol, iRowid, pConfig, pVal, rc2
	**(**TFts5Buffer)(__ccgo_up(bp)) = TFts5Buffer{}
	pConfig = (*TFts5Storage)(unsafe.Pointer(p)).FpConfig
	**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0)
	libc.Xmemset(tls, bp+16, 0, uint32(12))
	(**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FpStorage = p
	**(**int32)(__ccgo_up(bp + 28)) = _sqlite3Fts5StorageDeleteAll(tls, p)
	if **(**int32)(__ccgo_up(bp + 28)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 28)) = _fts5StorageLoadTotals(tls, p, int32(1))
	}
	if **(**int32)(__ccgo_up(bp + 28)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 28)) = _fts5StorageGetStmt(tls, p, int32(FTS5_STMT_SCAN), bp+12, (*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg)
	}
	for **(**int32)(__ccgo_up(bp + 28)) == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 12))) {
		iRowid = Xsqlite3_column_int64(tls, **(**uintptr)(__ccgo_up(bp + 12)), 0)
		_sqlite3Fts5BufferZero(tls, bp)
		**(**int32)(__ccgo_up(bp + 28)) = _sqlite3Fts5IndexBeginWrite(tls, (*TFts5Storage)(unsafe.Pointer(p)).FpIndex, 0, iRowid)
		(**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FiCol = 0
		for {
			if !(**(**int32)(__ccgo_up(bp + 28)) == SQLITE_OK && (**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FiCol < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) {
				break
			}
			(**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FszCol = 0
			if int32(**(**Tu8)(__ccgo_up((*TFts5Config)(unsafe.Pointer(pConfig)).FabUnindexed + uintptr((**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FiCol)))) == 0 {
				**(**int32)(__ccgo_up(bp + 32)) = 0            /* Size of pText in bytes */
				**(**uintptr)(__ccgo_up(bp + 36)) = uintptr(0) /* Pointer to buffer containing text value */
				**(**int32)(__ccgo_up(bp + 40)) = 0            /* Size of pLoc in bytes */
				**(**uintptr)(__ccgo_up(bp + 44)) = uintptr(0) /* Pointer to buffer containing text value */
				pVal = Xsqlite3_column_value(tls, **(**uintptr)(__ccgo_up(bp + 12)), (**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FiCol+int32(1))
				if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent == int32(FTS5_CONTENT_EXTERNAL) && _sqlite3Fts5IsLocaleValue(tls, pConfig, pVal) != 0 {
					**(**int32)(__ccgo_up(bp + 28)) = _sqlite3Fts5DecodeLocaleValue(tls, pVal, bp+36, bp+32, bp+44, bp+40)
				} else {
					**(**uintptr)(__ccgo_up(bp + 36)) = Xsqlite3_value_text(tls, pVal)
					**(**int32)(__ccgo_up(bp + 32)) = Xsqlite3_value_bytes(tls, pVal)
					if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLocale != 0 {
						iCol = (**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FiCol + int32(1) + (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol
						**(**uintptr)(__ccgo_up(bp + 44)) = Xsqlite3_column_text(tls, **(**uintptr)(__ccgo_up(bp + 12)), iCol)
						**(**int32)(__ccgo_up(bp + 40)) = Xsqlite3_column_bytes(tls, **(**uintptr)(__ccgo_up(bp + 12)), iCol)
					}
				}
				if **(**int32)(__ccgo_up(bp + 28)) == SQLITE_OK {
					_sqlite3Fts5SetLocale(tls, pConfig, **(**uintptr)(__ccgo_up(bp + 44)), **(**int32)(__ccgo_up(bp + 40)))
					**(**int32)(__ccgo_up(bp + 28)) = _sqlite3Fts5Tokenize(tls, pConfig, int32(FTS5_TOKENIZE_DOCUMENT), **(**uintptr)(__ccgo_up(bp + 36)), **(**int32)(__ccgo_up(bp + 32)), bp+16, __ccgo_fp(_fts5StorageInsertCallback))
					_sqlite3Fts5ClearLocale(tls, pConfig)
				}
			}
			_sqlite3Fts5BufferAppendVarint(tls, bp+28, bp, int64((**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FszCol))
			**(**Ti64)(__ccgo_up((*TFts5Storage)(unsafe.Pointer(p)).FaTotalSize + uintptr((**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FiCol)*8)) += int64((**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FszCol)
			goto _1
		_1:
			;
			(**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FiCol = (**(**TFts5InsertCtx)(__ccgo_up(bp + 16))).FiCol + 1
		}
		(*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow = (*TFts5Storage)(unsafe.Pointer(p)).FnTotalRow + 1
		if **(**int32)(__ccgo_up(bp + 28)) == SQLITE_OK {
			**(**int32)(__ccgo_up(bp + 28)) = _fts5StorageInsertDocsize(tls, p, iRowid, bp)
		}
	}
	Xsqlite3_free(tls, (**(**TFts5Buffer)(__ccgo_up(bp))).Fp)
	rc2 = Xsqlite3_reset(tls, **(**uintptr)(__ccgo_up(bp + 12)))
	if **(**int32)(__ccgo_up(bp + 28)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 28)) = rc2
	}
	/* Write the averages record */
	if **(**int32)(__ccgo_up(bp + 28)) == SQLITE_OK {
		**(**int32)(__ccgo_up(bp + 28)) = _fts5StorageSaveTotals(tls, p)
	}
	return **(**int32)(__ccgo_up(bp + 28))
}

// C documentation
//
//	/*
//	** Return a nul-terminated copy of the string indicated by pIn. If nIn
//	** is non-negative, then it is the length of the string in bytes. Otherwise,
//	** the length of the string is determined using strlen().
//	**
//	** It is the responsibility of the caller to eventually free the returned
//	** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
//	*/
func _sqlite3Fts5Strndup(tls *libc.TLS, pRc uintptr, pIn uintptr, nIn int32) (r uintptr) {
	var zRet uintptr
	_ = zRet
	zRet = uintptr(0)
	if **(**int32)(__ccgo_up(pRc)) == SQLITE_OK {
		if nIn < 0 {
			nIn = int32(libc.Xstrlen(tls, pIn))
		}
		zRet = Xsqlite3_malloc64(tls, uint64(int64(nIn)+int64(1)))
		if zRet != 0 {
			libc.Xmemcpy(tls, zRet, pIn, uint32(nIn))
			**(**int8)(__ccgo_up(zRet + uintptr(nIn))) = int8('\000')
		} else {
			**(**int32)(__ccgo_up(pRc)) = int32(SQLITE_NOMEM)
		}
	}
	return zRet
}

func _sqlite3Fts5TermsetAdd(tls *libc.TLS, p uintptr, iIdx int32, pTerm uintptr, nTerm int32, pbPresent uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var hash Tu32
	var i int32
	var pEntry uintptr
	var _ /* rc at bp+0 */ int32
	_, _, _ = hash, i, pEntry
	**(**int32)(__ccgo_up(bp)) = SQLITE_OK
	**(**int32)(__ccgo_up(pbPresent)) = 0
	if p != 0 {
		hash = uint32(13)
		/* Calculate a hash value for this term. This is the same hash checksum
		 ** used by the fts5_hash.c module. This is not important for correct
		 ** operation of the module, but is necessary to ensure that some tests
		 ** designed to produce hash table collisions really do work.  */
		i = nTerm - int32(1)
		for {
			if !(i >= 0) {
				break
			}
			hash = hash<<libc.Int32FromInt32(3) ^ hash ^ uint32(**(**int8)(__ccgo_up(pTerm + uintptr(i))))
			goto _1
		_1:
			;
			i = i - 1
		}
		hash = hash<<libc.Int32FromInt32(3) ^ hash ^ uint32(iIdx)
		hash = hash % uint32(int32(libc.Uint32FromInt64(2048)/libc.Uint32FromInt64(4)))
		pEntry = **(**uintptr)(__ccgo_up(p + uintptr(hash)*4))
		for {
			if !(pEntry != 0) {
				break
			}
			if (*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FiIdx == iIdx && (*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FnTerm == nTerm && libc.Xmemcmp(tls, (*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FpTerm, pTerm, uint32(nTerm)) == 0 {
				**(**int32)(__ccgo_up(pbPresent)) = int32(1)
				break
			}
			goto _2
		_2:
			;
			pEntry = (*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FpNext
		}
		if pEntry == uintptr(0) {
			pEntry = _sqlite3Fts5MallocZero(tls, bp, int64(uint32(16)+uint32(nTerm)))
			if pEntry != 0 {
				(*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FpTerm = pEntry + 1*16
				(*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FnTerm = nTerm
				(*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FiIdx = iIdx
				libc.Xmemcpy(tls, (*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FpTerm, pTerm, uint32(nTerm))
				(*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FpNext = **(**uintptr)(__ccgo_up(p + uintptr(hash)*4))
				**(**uintptr)(__ccgo_up(p + uintptr(hash)*4)) = pEntry
			}
		}
	}
	return **(**int32)(__ccgo_up(bp))
}

func _sqlite3Fts5TermsetFree(tls *libc.TLS, p uintptr) {
	var i Tu32
	var pDel, pEntry uintptr
	_, _, _ = i, pDel, pEntry
	if p != 0 {
		i = uint32(0)
		for {
			if !(i < uint32(int32(libc.Uint32FromInt64(2048)/libc.Uint32FromInt64(4)))) {
				break
			}
			pEntry = **(**uintptr)(__ccgo_up(p + uintptr(i)*4))
			for pEntry != 0 {
				pDel = pEntry
				pEntry = (*TFts5TermsetEntry)(unsafe.Pointer(pEntry)).FpNext
				Xsqlite3_free(tls, pDel)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		Xsqlite3_free(tls, p)
	}
}

/*
** 2014 Jun 09
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
**
** This is an SQLite module implementing full-text search.
 */

/* #include "fts5Int.h" */

/* Maximum allowed page size */

// C documentation
//
//	/*
//	** Register all built-in tokenizers with FTS5.
//	*/
func _sqlite3Fts5TokenizerInit(tls *libc.TLS, pApi uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var i, rc int32
	var _ /* aBuiltin at bp+0 */ [3]struct {
		FzName uintptr
		Fx     Tfts5_tokenizer
	}
	var _ /* sPorter at bp+48 */ Tfts5_tokenizer_v2
	_, _ = i, rc
	**(**[3]struct {
		FzName uintptr
		Fx     Tfts5_tokenizer
	})(__ccgo_up(bp)) = [3]struct {
		FzName uintptr
		Fx     Tfts5_tokenizer
	}{
		0: {
			FzName: __ccgo_ts + 43131,
			Fx: Tfts5_tokenizer{
				FxCreate:   __ccgo_fp(_fts5UnicodeCreate),
				FxDelete:   __ccgo_fp(_fts5UnicodeDelete),
				FxTokenize: __ccgo_fp(_fts5UnicodeTokenize),
			},
		},
		1: {
			FzName: __ccgo_ts + 43450,
			Fx: Tfts5_tokenizer{
				FxCreate:   __ccgo_fp(_fts5AsciiCreate),
				FxDelete:   __ccgo_fp(_fts5AsciiDelete),
				FxTokenize: __ccgo_fp(_fts5AsciiTokenize),
			},
		},
		2: {
			FzName: __ccgo_ts + 43442,
			Fx: Tfts5_tokenizer{
				FxCreate:   __ccgo_fp(_fts5TriCreate),
				FxDelete:   __ccgo_fp(_fts5TriDelete),
				FxTokenize: __ccgo_fp(_fts5TriTokenize),
			},
		},
	}
	rc = SQLITE_OK /* To iterate through builtin functions */
	i = 0
	for {
		if !(rc == SQLITE_OK && i < int32(libc.Uint32FromInt64(48)/libc.Uint32FromInt64(16))) {
			break
		}
		rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tfts5_api)(unsafe.Pointer(pApi)).FxCreateTokenizer})))(tls, pApi, (**(**[3]struct {
			FzName uintptr
			Fx     Tfts5_tokenizer
		})(__ccgo_up(bp)))[i].FzName, pApi, bp+uintptr(i)*16+4, uintptr(0))
		goto _1
	_1:
		;
		i = i + 1
	}
	if rc == SQLITE_OK {
		**(**Tfts5_tokenizer_v2)(__ccgo_up(bp + 48)) = Tfts5_tokenizer_v2{
			FiVersion:  int32(2),
			FxCreate:   __ccgo_fp(_fts5PorterCreate),
			FxDelete:   __ccgo_fp(_fts5PorterDelete),
			FxTokenize: __ccgo_fp(_fts5PorterTokenize),
		}
		rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tfts5_api)(unsafe.Pointer(pApi)).FxCreateTokenizer_v2})))(tls, pApi, __ccgo_ts+43141, pApi, bp+48, uintptr(0))
	}
	return rc
}

/*
** 2012-05-25
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
 */

/*
** DO NOT EDIT THIS MACHINE GENERATED FILE.
 */

/* #include <assert.h> */

// C documentation
//
//	/*
//	** Interpret the argument as a unicode codepoint. If the codepoint
//	** is an upper case character that has a lower case equivalent,
//	** return the codepoint corresponding to the lower case version.
//	** Otherwise, return a copy of the argument.
//	**
//	** The results are undefined if the value passed to this function
//	** is less than zero.
//	*/
func _sqlite3Fts5UnicodeFold(tls *libc.TLS, c int32, eRemoveDiacritic int32) (r int32) {
	var cmp, iHi, iLo, iRes, iTest, ret int32
	var p uintptr
	_, _, _, _, _, _, _ = cmp, iHi, iLo, iRes, iTest, p, ret
	ret = c
	if c < int32(128) {
		if c >= int32('A') && c <= int32('Z') {
			ret = c + (libc.Int32FromUint8('a') - libc.Int32FromUint8('A'))
		}
	} else {
		if c < int32(65536) {
			iHi = int32(libc.Uint32FromInt64(652)/libc.Uint32FromInt64(4) - libc.Uint32FromInt32(1))
			iLo = 0
			iRes = -int32(1)
			for iHi >= iLo {
				iTest = (iHi + iLo) / int32(2)
				cmp = c - int32(_aEntry[iTest].FiCode)
				if cmp >= 0 {
					iRes = iTest
					iLo = iTest + int32(1)
				} else {
					iHi = iTest - int32(1)
				}
			}
			p = uintptr(unsafe.Pointer(&_aEntry)) + uintptr(iRes)*4
			if c < int32((*struct {
				FiCode  uint16
				Fflags  uint8
				FnRange uint8
			})(unsafe.Pointer(p)).FiCode)+int32((*struct {
				FiCode  uint16
				Fflags  uint8
				FnRange uint8
			})(unsafe.Pointer(p)).FnRange) && 0 == int32(0x01)&int32((*struct {
				FiCode  uint16
				Fflags  uint8
				FnRange uint8
			})(unsafe.Pointer(p)).Fflags)&(int32((*struct {
				FiCode  uint16
				Fflags  uint8
				FnRange uint8
			})(unsafe.Pointer(p)).FiCode)^c) {
				ret = (c + int32(_aiOff[int32((*struct {
					FiCode  uint16
					Fflags  uint8
					FnRange uint8
				})(unsafe.Pointer(p)).Fflags)>>int32(1)])) & int32(0x0000FFFF)
			}
			if eRemoveDiacritic != 0 {
				ret = _fts5_remove_diacritic(tls, ret, libc.BoolInt32(eRemoveDiacritic == int32(2)))
			}
		} else {
			if c >= int32(66560) && c < int32(66600) {
				ret = c + int32(40)
			}
		}
	}
	return ret
}

// C documentation
//
//	/*
//	** Compute the column names for a SELECT statement.
//	**
//	** The only guarantee that SQLite makes about column names is that if the
//	** column has an AS clause assigning it a name, that will be the name used.
//	** That is the only documented guarantee.  However, countless applications
//	** developed over the years have made baseless assumptions about column names
//	** and will break if those assumptions changes.  Hence, use extreme caution
//	** when modifying this routine to avoid breaking legacy.
//	**
//	** See Also: sqlite3ColumnsFromExprList()
//	**
//	** The PRAGMA short_column_names and PRAGMA full_column_names settings are
//	** deprecated.  The default setting is short=ON, full=OFF.  99.9% of all
//	** applications should operate this way.  Nevertheless, we need to support the
//	** other modes for legacy:
//	**
//	**    short=OFF, full=OFF:      Column name is the text of the expression has it
//	**                              originally appears in the SELECT statement.  In
//	**                              other words, the zSpan of the result expression.
//	**
//	**    short=ON, full=OFF:       (This is the default setting).  If the result
//	**                              refers directly to a table column, then the
//	**                              result column name is just the table column
//	**                              name: COLUMN.  Otherwise use zSpan.
//	**
//	**    full=ON, short=ANY:       If the result refers directly to a table column,
//	**                              then the result column name with the table name
//	**                              prefix, ex: TABLE.COLUMN.  Otherwise use zSpan.
//	*/
func _sqlite3GenerateColumnNames(tls *libc.TLS, pParse uintptr, pSelect uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, p, pEList, pTab, pTabList, v, z, zCol, zName, zName1, v2 uintptr
	var fullName, i, iCol, srcName int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, fullName, i, iCol, p, pEList, pTab, pTabList, srcName, v, z, zCol, zName, zName1, v2
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
	if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x20>>5)) != 0 {
		return
	}
	/* Column names are determined by the left-most term of a compound select */
	for (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 {
		pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior
	}
	pTabList = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc
	pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList
	libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 5, 0x20)
	fullName = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_FullColNames) != uint64(0))
	srcName = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ShortColNames) != uint64(0) || fullName != 0)
	_sqlite3VdbeSetNumCols(tls, v, (*TExprList)(unsafe.Pointer(pEList)).FnExpr)
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
			break
		}
		p = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FpExpr
		/* Agg processing has not run yet */
		/* Covering idx not yet coded */
		if (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(i)*20 + 8 + 4))&0x3>>0)) == ENAME_NAME {
			/* An AS clause always takes first priority */
			zName = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName
			_sqlite3VdbeSetColName(tls, v, i, COLNAME_NAME, zName, uintptr(-libc.Int32FromInt32(1)))
		} else {
			if srcName != 0 && int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_COLUMN) {
				iCol = int32((*TExpr)(unsafe.Pointer(p)).FiColumn)
				pTab = (*TExpr)(unsafe.Pointer(p)).Fy.FpTab
				if iCol < 0 {
					iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey)
				}
				if iCol < 0 {
					zCol = __ccgo_ts + 19186
				} else {
					zCol = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).FzCnName
				}
				if fullName != 0 {
					zName1 = uintptr(0)
					zName1 = _sqlite3MPrintf(tls, db, __ccgo_ts+14849, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, zCol))
					_sqlite3VdbeSetColName(tls, v, i, COLNAME_NAME, zName1, __ccgo_fp(_sqlite3RowSetClear))
				} else {
					_sqlite3VdbeSetColName(tls, v, i, COLNAME_NAME, zCol, uintptr(-libc.Int32FromInt32(1)))
				}
			} else {
				z = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*20))).FzEName
				if z == uintptr(0) {
					v2 = _sqlite3MPrintf(tls, db, __ccgo_ts+21866, libc.VaList(bp+8, i+int32(1)))
				} else {
					v2 = _sqlite3DbStrDup(tls, db, z)
				}
				z = v2
				_sqlite3VdbeSetColName(tls, v, i, COLNAME_NAME, z, __ccgo_fp(_sqlite3RowSetClear))
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	_generateColumnTypes(tls, pParse, pTabList, pEList)
}

// C documentation
//
//	/*
//	** Generate code to do constraint checks prior to an INSERT or an UPDATE
//	** on table pTab.
//	**
//	** The regNewData parameter is the first register in a range that contains
//	** the data to be inserted or the data after the update.  There will be
//	** pTab->nCol+1 registers in this range.  The first register (the one
//	** that regNewData points to) will contain the new rowid, or NULL in the
//	** case of a WITHOUT ROWID table.  The second register in the range will
//	** contain the content of the first table column.  The third register will
//	** contain the content of the second table column.  And so forth.
//	**
//	** The regOldData parameter is similar to regNewData except that it contains
//	** the data prior to an UPDATE rather than afterwards.  regOldData is zero
//	** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
//	** checking regOldData for zero.
//	**
//	** For an UPDATE, the pkChng boolean is true if the true primary key (the
//	** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
//	** might be modified by the UPDATE.  If pkChng is false, then the key of
//	** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
//	**
//	** For an INSERT, the pkChng boolean indicates whether or not the rowid
//	** was explicitly specified as part of the INSERT statement.  If pkChng
//	** is zero, it means that the either rowid is computed automatically or
//	** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
//	** pkChng will only be true if the INSERT statement provides an integer
//	** value for either the rowid column or its INTEGER PRIMARY KEY alias.
//	**
//	** The code generated by this routine will store new index entries into
//	** registers identified by aRegIdx[].  No index entry is created for
//	** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
//	** the same as the order of indices on the linked list of indices
//	** at pTab->pIndex.
//	**
//	** (2019-05-07) The generated code also creates a new record for the
//	** main table, if pTab is a rowid table, and stores that record in the
//	** register identified by aRegIdx[nIdx] - in other words in the first
//	** entry of aRegIdx[] past the last index.  It is important that the
//	** record be generated during constraint checks to avoid affinity changes
//	** to the register content that occur after constraint checks but before
//	** the new record is inserted.
//	**
//	** The caller must have already opened writeable cursors on the main
//	** table and all applicable indices (that is to say, all indices for which
//	** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
//	** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
//	** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
//	** for the first index in the pTab->pIndex list.  Cursors for other indices
//	** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
//	**
//	** This routine also generates code to check constraints.  NOT NULL,
//	** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
//	** then the appropriate action is performed.  There are five possible
//	** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
//	**
//	**  Constraint type  Action       What Happens
//	**  ---------------  ----------   ----------------------------------------
//	**  any              ROLLBACK     The current transaction is rolled back and
//	**                                sqlite3_step() returns immediately with a
//	**                                return code of SQLITE_CONSTRAINT.
//	**
//	**  any              ABORT        Back out changes from the current command
//	**                                only (do not do a complete rollback) then
//	**                                cause sqlite3_step() to return immediately
//	**                                with SQLITE_CONSTRAINT.
//	**
//	**  any              FAIL         Sqlite3_step() returns immediately with a
//	**                                return code of SQLITE_CONSTRAINT.  The
//	**                                transaction is not rolled back and any
//	**                                changes to prior rows are retained.
//	**
//	**  any              IGNORE       The attempt in insert or update the current
//	**                                row is skipped, without throwing an error.
//	**                                Processing continues with the next row.
//	**                                (There is an immediate jump to ignoreDest.)
//	**
//	**  NOT NULL         REPLACE      The NULL value is replace by the default
//	**                                value for that column.  If the default value
//	**                                is NULL, the action is the same as ABORT.
//	**
//	**  UNIQUE           REPLACE      The other row that conflicts with the row
//	**                                being inserted is removed.
//	**
//	**  CHECK            REPLACE      Illegal.  The results in an exception.
//	**
//	** Which action to take is determined by the overrideError parameter.
//	** Or if overrideError==OE_Default, then the pParse->onError parameter
//	** is used.  Or if pParse->onError==OE_Default then the onError value
//	** for the constraint is used.
//	*/
func _sqlite3GenerateConstraintChecks(tls *libc.TLS, pParse uintptr, pTab uintptr, aRegIdx uintptr, iDataCur int32, iIdxCur int32, regNewData int32, regOldData int32, pkChng Tu8, overrideError Tu8, ignoreDest int32, pbMayReplace uintptr, aiChng uintptr, pUpsert uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var addr1, addrBypass, addrConflictCk, addrJump, addrRecheck, addrRowidOk, addrUniqueOk, allOk, b2ndPass, i, iField, iReg, iThisCur, ipkBottom, ipkTop, isGenerated, jj, lblRecheckOk, nCol, nConflictCk, nGenerated, nIdx, nPkField, nReplaceTrig, nSeenReplace, onError, op, p2, regCmp, regIdx, regR, regRec, regTrigCnt, seenReplace, upsertIpkDelay, upsertIpkReturn, x, x1, v2 int32
	var bAffinityDone, isUpdate Tu8
	var bUsed, db, p4, pCheck, pCol, pCopy, pExpr, pIdx, pPk, pTerm, pTrigger, pUpsertClause, v, zMsg, zName, zP4, v8 uintptr
	var nByte Tu64
	var _ /* ix at bp+0 */ int32
	var _ /* sIdxIter at bp+4 */ TIndexIterator
	var _ /* x at bp+20 */ TVdbeOp
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, addrBypass, addrConflictCk, addrJump, addrRecheck, addrRowidOk, addrUniqueOk, allOk, b2ndPass, bAffinityDone, bUsed, db, i, iField, iReg, iThisCur, ipkBottom, ipkTop, isGenerated, isUpdate, jj, lblRecheckOk, nByte, nCol, nConflictCk, nGenerated, nIdx, nPkField, nReplaceTrig, nSeenReplace, onError, op, p2, p4, pCheck, pCol, pCopy, pExpr, pIdx, pPk, pTerm, pTrigger, pUpsertClause, regCmp, regIdx, regR, regRec, regTrigCnt, seenReplace, upsertIpkDelay, upsertIpkReturn, v, x, x1, zMsg, zName, zP4, v2, v8 /* Pointer to one of the indices */
	pPk = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  /* Conflict resolution strategy */
	seenReplace = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
	pUpsertClause = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        /* True if this is an UPDATE operation */
	bAffinityDone = uint8(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          /* True if the OP_Affinity operation has been run */
	upsertIpkReturn = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               /* Address of Goto at end of IPK uniqueness check */
	upsertIpkDelay = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                /* Address of Goto to bypass initial IPK check */
	ipkTop = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        /* Top of the IPK uniqueness check */
	ipkBottom = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     /* Register used to count replace trigger invocations */
	addrRecheck = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   /* Jump here to recheck all uniqueness constraints */
	lblRecheckOk = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  /* List of DELETE triggers on the table pTab */
	nReplaceTrig = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  /* Index iterator */
	isUpdate = libc.BoolUint8(regOldData != 0)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	/* This table is not a VIEW */
	nCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
	/* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
	 ** normal rowid tables.  nPkField is the number of key fields in the
	 ** pPk index or 1 for a rowid table.  In other words, nPkField is the
	 ** number of fields in the true primary key of the table. */
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
		pPk = uintptr(0)
		nPkField = int32(1)
	} else {
		pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
		nPkField = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)
	}
	/* Record that this module has started */
	/* Test all NOT NULL constraints.
	 */
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasNotNull) != 0 {
		b2ndPass = 0        /* True if currently running 2nd pass */
		nSeenReplace = 0    /* Number of ON CONFLICT REPLACE operations */
		nGenerated = 0      /* Number of generated columns with NOT NULL */
		for int32(1) != 0 { /* Make 2 passes over columns. Exit loop via "break" */
			i = 0
			for {
				if !(i < nCol) {
					break
				} /* Register holding column value */
				pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12 /* non-zero if column is generated */
				onError = int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4)) & 0xf >> 0))
				if onError == OE_None {
					goto _1
				} /* No NOT NULL on this column */
				if i == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
					goto _1 /* ROWID is never NULL */
				}
				isGenerated = int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags) & int32(COLFLAG_GENERATED)
				if isGenerated != 0 && !(b2ndPass != 0) {
					nGenerated = nGenerated + 1
					goto _1 /* Generated columns processed on 2nd pass */
				}
				if aiChng != 0 && **(**int32)(__ccgo_up(aiChng + uintptr(i)*4)) < 0 && !(isGenerated != 0) {
					/* Do not check NOT NULL on columns that do not change */
					goto _1
				}
				if int32(overrideError) != int32(OE_Default) {
					onError = int32(overrideError)
				} else {
					if onError == int32(OE_Default) {
						onError = int32(OE_Abort)
					}
				}
				if onError == int32(OE_Replace) {
					if b2ndPass != 0 || int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt) == 0 {
						onError = int32(OE_Abort)
					} else {
					}
				} else {
					if b2ndPass != 0 && !(isGenerated != 0) {
						goto _1
					}
				}
				iReg = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(i))) + regNewData + int32(1)
				switch onError {
				case int32(OE_Replace):
					addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_NotNull), iReg)
					nSeenReplace = nSeenReplace + 1
					_sqlite3ExprCodeCopy(tls, pParse, _sqlite3ColumnExpr(tls, pTab, pCol), iReg)
					_sqlite3VdbeJumpHere(tls, v, addr1)
				case int32(OE_Abort):
					_sqlite3MayAbort(tls, pParse)
					fallthrough
				case int32(OE_Rollback):
					fallthrough
				case int32(OE_Fail):
					zMsg = _sqlite3MPrintf(tls, db, __ccgo_ts+14849, libc.VaList(bp+48, (*TTable)(unsafe.Pointer(pTab)).FzName, (*TColumn)(unsafe.Pointer(pCol)).FzCnName))
					_sqlite3VdbeAddOp3(tls, v, int32(OP_HaltIfNull), libc.Int32FromInt32(SQLITE_CONSTRAINT)|libc.Int32FromInt32(5)<<libc.Int32FromInt32(8), onError, iReg)
					_sqlite3VdbeAppendP4(tls, v, zMsg, -int32(7))
					_sqlite3VdbeChangeP5(tls, v, uint16(P5_ConstraintNotNull))
				default:
					_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), iReg, ignoreDest)
					break
				} /* end switch(onError) */
				goto _1
			_1:
				;
				i = i + 1
			} /* end loop i over columns */
			if nGenerated == 0 && nSeenReplace == 0 {
				/* If there are no generated columns with NOT NULL constraints
				 ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single
				 ** pass is sufficient */
				break
			}
			if b2ndPass != 0 {
				break
			} /* Never need more than 2 passes */
			b2ndPass = int32(1)
			if nSeenReplace > 0 && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) {
				/* If any NOT NULL ON CONFLICT REPLACE constraints fired on the
				 ** first pass, recomputed values for all generated columns, as
				 ** those values might depend on columns affected by the REPLACE.
				 */
				_sqlite3ComputeGeneratedColumns(tls, pParse, regNewData+int32(1), pTab)
			}
		} /* end of 2-pass loop */
	} /* end if( has-not-null-constraints ) */
	/* Test all CHECK constraints
	 */
	if (*TTable)(unsafe.Pointer(pTab)).FpCheck != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_IgnoreChecks) == uint64(0) {
		pCheck = (*TTable)(unsafe.Pointer(pTab)).FpCheck
		(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = -(regNewData + int32(1))
		if int32(overrideError) != int32(OE_Default) {
			v2 = int32(overrideError)
		} else {
			v2 = int32(OE_Abort)
		}
		onError = v2
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pCheck)).FnExpr) {
				break
			}
			pExpr = (*(*TExprList_item)(unsafe.Pointer(pCheck + 8 + uintptr(i)*20))).FpExpr
			if aiChng != 0 && !(_sqlite3ExprReferencesUpdatedColumn(tls, pExpr, aiChng, int32(pkChng)) != 0) {
				/* The check constraints do not reference any of the columns being
				 ** updated so there is no point it verifying the check constraint */
				goto _3
			}
			if int32(bAffinityDone) == 0 {
				_sqlite3TableAffinity(tls, v, pTab, regNewData+int32(1))
				bAffinityDone = uint8(1)
			}
			allOk = _sqlite3VdbeMakeLabel(tls, pParse)
			pCopy = _sqlite3ExprDup(tls, db, pExpr, 0)
			if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
				_sqlite3ExprIfTrue(tls, pParse, pCopy, allOk, int32(SQLITE_JUMPIFNULL))
			}
			_sqlite3ExprDelete(tls, db, pCopy)
			if onError == int32(OE_Ignore) {
				_sqlite3VdbeGoto(tls, v, ignoreDest)
			} else {
				zName = (*(*TExprList_item)(unsafe.Pointer(pCheck + 8 + uintptr(i)*20))).FzEName
				if onError == int32(OE_Replace) {
					onError = int32(OE_Abort)
				} /* IMP: R-26383-51744 */
				_sqlite3HaltConstraint(tls, pParse, libc.Int32FromInt32(SQLITE_CONSTRAINT)|libc.Int32FromInt32(1)<<libc.Int32FromInt32(8), onError, zName, P4_TRANSIENT, uint8(P5_ConstraintCheck))
			}
			_sqlite3VdbeResolveLabel(tls, v, allOk)
			goto _3
		_3:
			;
			i = i + 1
		}
		(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = 0
	}
	/* UNIQUE and PRIMARY KEY constraints should be handled in the following
	 ** order:
	 **
	 **   (1)  OE_Update
	 **   (2)  OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
	 **   (3)  OE_Replace
	 **
	 ** OE_Fail and OE_Ignore must happen before any changes are made.
	 ** OE_Update guarantees that only a single row will change, so it
	 ** must happen before OE_Replace.  Technically, OE_Abort and OE_Rollback
	 ** could happen in any order, but they are grouped up front for
	 ** convenience.
	 **
	 ** 2018-08-14: Ticket https://sqlite.org/src/info/908f001483982c43
	 ** The order of constraints used to have OE_Update as (2) and OE_Abort
	 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
	 ** constraint before any others, so it had to be moved.
	 **
	 ** Constraint checking code is generated in this order:
	 **   (A)  The rowid constraint
	 **   (B)  Unique index constraints that do not have OE_Replace as their
	 **        default conflict resolution strategy
	 **   (C)  Unique index that do use OE_Replace by default.
	 **
	 ** The ordering of (2) and (3) is accomplished by making sure the linked
	 ** list of indexes attached to a table puts all OE_Replace indexes last
	 ** in the list.  See sqlite3CreateIndex() for where that happens.
	 */
	(**(**TIndexIterator)(__ccgo_up(bp + 4))).FeType = 0
	(**(**TIndexIterator)(__ccgo_up(bp + 4))).Fi = 0
	*(*uintptr)(unsafe.Pointer(bp + 4 + 8 + 4)) = uintptr(0) /* Silence harmless compiler warning */
	*(*uintptr)(unsafe.Pointer(bp + 4 + 8)) = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	if pUpsert != 0 {
		if (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget == uintptr(0) {
			/* There is just on ON CONFLICT clause and it has no constraint-target */
			if int32((*TUpsert)(unsafe.Pointer(pUpsert)).FisDoUpdate) == 0 {
				/* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
				 ** Make all unique constraint resolution be OE_Ignore */
				overrideError = uint8(OE_Ignore)
				pUpsert = uintptr(0)
			} else {
				/* A single ON CONFLICT DO UPDATE.  Make all resolutions OE_Update */
				overrideError = uint8(OE_Update)
			}
		} else {
			if (*TTable)(unsafe.Pointer(pTab)).FpIndex != uintptr(0) {
				nIdx = 0
				pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
				for {
					if !(pIdx != 0) {
						break
					}
					goto _4
				_4:
					;
					pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
					nIdx = nIdx + 1
				}
				(**(**TIndexIterator)(__ccgo_up(bp + 4))).FeType = int32(1)
				*(*int32)(unsafe.Pointer(bp + 4 + 8)) = nIdx
				nByte = uint64((libc.Uint32FromInt64(8)+libc.Uint32FromInt32(1))*uint32(nIdx) + uint32(nIdx))
				*(*uintptr)(unsafe.Pointer(bp + 4 + 8 + 4)) = _sqlite3DbMallocZero(tls, db, nByte)
				if *(*uintptr)(unsafe.Pointer(bp + 4 + 8 + 4)) == uintptr(0) {
					return
				} /* OOM */
				bUsed = *(*uintptr)(unsafe.Pointer(bp + 4 + 8 + 4)) + uintptr(nIdx)*8
				(*TUpsert)(unsafe.Pointer(pUpsert)).FpToFree = *(*uintptr)(unsafe.Pointer(bp + 4 + 8 + 4))
				i = 0
				pTerm = pUpsert
				for {
					if !(pTerm != 0) {
						break
					}
					if (*TUpsert)(unsafe.Pointer(pTerm)).FpUpsertTarget == uintptr(0) {
						break
					}
					if (*TUpsert)(unsafe.Pointer(pTerm)).FpUpsertIdx == uintptr(0) {
						goto _5
					} /* Skip ON CONFLICT for the IPK */
					jj = 0
					pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
					for pIdx != uintptr(0) && pIdx != (*TUpsert)(unsafe.Pointer(pTerm)).FpUpsertIdx {
						pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
						jj = jj + 1
					}
					if **(**Tu8)(__ccgo_up(bUsed + uintptr(jj))) != 0 {
						goto _5
					} /* Duplicate ON CONFLICT clause ignored */
					**(**Tu8)(__ccgo_up(bUsed + uintptr(jj))) = uint8(1)
					(**(**TIndexListTerm)(__ccgo_up(*(*uintptr)(unsafe.Pointer(bp + 4 + 8 + 4)) + uintptr(i)*8))).Fp = pIdx
					(**(**TIndexListTerm)(__ccgo_up(*(*uintptr)(unsafe.Pointer(bp + 4 + 8 + 4)) + uintptr(i)*8))).Fix = jj
					i = i + 1
					goto _5
				_5:
					;
					pTerm = (*TUpsert)(unsafe.Pointer(pTerm)).FpNextUpsert
				}
				jj = 0
				pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
				for {
					if !(pIdx != 0) {
						break
					}
					if **(**Tu8)(__ccgo_up(bUsed + uintptr(jj))) != 0 {
						goto _6
					}
					(**(**TIndexListTerm)(__ccgo_up(*(*uintptr)(unsafe.Pointer(bp + 4 + 8 + 4)) + uintptr(i)*8))).Fp = pIdx
					(**(**TIndexListTerm)(__ccgo_up(*(*uintptr)(unsafe.Pointer(bp + 4 + 8 + 4)) + uintptr(i)*8))).Fix = jj
					i = i + 1
					goto _6
				_6:
					;
					pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
					jj = jj + 1
				}
			}
		}
	}
	/* Determine if it is possible that triggers (either explicitly coded
	 ** triggers or FK resolution actions) might run as a result of deletes
	 ** that happen when OE_Replace conflict resolution occurs. (Call these
	 ** "replace triggers".)  If any replace triggers run, we will need to
	 ** recheck all of the uniqueness constraints after they have all run.
	 ** But on the recheck, the resolution is OE_Abort instead of OE_Replace.
	 **
	 ** If replace triggers are a possibility, then
	 **
	 **   (1) Allocate register regTrigCnt and initialize it to zero.
	 **       That register will count the number of replace triggers that
	 **       fire.  Constraint recheck only occurs if the number is positive.
	 **   (2) Initialize pTrigger to the list of all DELETE triggers on pTab.
	 **   (3) Initialize addrRecheck and lblRecheckOk
	 **
	 ** The uniqueness rechecking code will create a series of tests to run
	 ** in a second pass.  The addrRecheck and lblRecheckOk variables are
	 ** used to link together these tests which are separated from each other
	 ** in the generate bytecode.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(libc.Int32FromInt32(SQLITE_RecTriggers)|libc.Int32FromInt32(SQLITE_ForeignKeys)) == uint64(0) {
		/* There are not DELETE triggers nor FK constraints.  No constraint
		 ** rechecks are needed. */
		pTrigger = uintptr(0)
		regTrigCnt = 0
	} else {
		if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_RecTriggers) != 0 {
			pTrigger = _sqlite3TriggersExist(tls, pParse, pTab, int32(TK_DELETE), uintptr(0), uintptr(0))
			regTrigCnt = libc.BoolInt32(pTrigger != uintptr(0) || _sqlite3FkRequired(tls, pParse, pTab, uintptr(0), 0) != 0)
		} else {
			pTrigger = uintptr(0)
			regTrigCnt = _sqlite3FkRequired(tls, pParse, pTab, uintptr(0), 0)
		}
		if regTrigCnt != 0 {
			/* Replace triggers might exist.  Allocate the counter and
			 ** initialize it to zero. */
			v8 = pParse + 48
			*(*int32)(unsafe.Pointer(v8)) = *(*int32)(unsafe.Pointer(v8)) + 1
			v2 = *(*int32)(unsafe.Pointer(v8))
			regTrigCnt = v2
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regTrigCnt)
			lblRecheckOk = _sqlite3VdbeMakeLabel(tls, pParse)
			addrRecheck = lblRecheckOk
		}
	}
	/* If rowid is changing, make sure the new rowid does not previously
	 ** exist in the table.
	 */
	if pkChng != 0 && pPk == uintptr(0) {
		addrRowidOk = _sqlite3VdbeMakeLabel(tls, pParse)
		/* Figure out what action to take in case of a rowid collision */
		onError = int32((*TTable)(unsafe.Pointer(pTab)).FkeyConf)
		if int32(overrideError) != int32(OE_Default) {
			onError = int32(overrideError)
		} else {
			if onError == int32(OE_Default) {
				onError = int32(OE_Abort)
			}
		}
		/* figure out whether or not upsert applies in this case */
		if pUpsert != 0 {
			pUpsertClause = _sqlite3UpsertOfIndex(tls, pUpsert, uintptr(0))
			if pUpsertClause != uintptr(0) {
				if int32((*TUpsert)(unsafe.Pointer(pUpsertClause)).FisDoUpdate) == 0 {
					onError = int32(OE_Ignore) /* DO NOTHING is the same as INSERT OR IGNORE */
				} else {
					onError = int32(OE_Update) /* DO UPDATE */
				}
			}
			if pUpsertClause != pUpsert {
				/* The first ON CONFLICT clause has a conflict target other than
				 ** the IPK.  We have to jump ahead to that first ON CONFLICT clause
				 ** and then come back here and deal with the IPK afterwards */
				upsertIpkDelay = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto))
			}
		}
		/* If the response to a rowid conflict is REPLACE but the response
		 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
		 ** to defer the running of the rowid conflict checking until after
		 ** the UNIQUE constraints have run.
		 */
		if onError == int32(OE_Replace) && onError != int32(overrideError) && (*TTable)(unsafe.Pointer(pTab)).FpIndex != 0 && !(upsertIpkDelay != 0) {
			ipkTop = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) + int32(1)
		}
		if isUpdate != 0 {
			/* pkChng!=0 does not mean that the rowid has changed, only that
			 ** it might have changed.  Skip the conflict logic below if the rowid
			 ** is unchanged. */
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), regNewData, addrRowidOk, regOldData)
			_sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NOTNULL))
		}
		/* Check to see if the new rowid already exists in the table.  Skip
		 ** the following conflict logic if it does not. */
		_sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDataCur, addrRowidOk, regNewData)
		switch onError {
		default:
			onError = int32(OE_Abort)
			fallthrough
		case int32(OE_Rollback):
			fallthrough
		case int32(OE_Abort):
			fallthrough
		case int32(OE_Fail):
			_sqlite3RowidConstraint(tls, pParse, onError, pTab)
		case int32(OE_Replace):
			/* If there are DELETE triggers on this table and the
			 ** recursive-triggers flag is set, call GenerateRowDelete() to
			 ** remove the conflicting row from the table. This will fire
			 ** the triggers and remove both the table and index b-tree entries.
			 **
			 ** Otherwise, if there are no triggers or the recursive-triggers
			 ** flag is not set, but the table has one or more indexes, call
			 ** GenerateRowIndexDelete(). This removes the index b-tree entries
			 ** only. The table b-tree entry will be replaced by the new entry
			 ** when it is inserted.
			 **
			 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
			 ** also invoke MultiWrite() to indicate that this VDBE may require
			 ** statement rollback (if the statement is aborted after the delete
			 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
			 ** but being more selective here allows statements like:
			 **
			 **   REPLACE INTO t(rowid) VALUES($newrowid)
			 **
			 ** to run without a statement journal if there are no indexes on the
			 ** table.
			 */
			if regTrigCnt != 0 {
				_sqlite3MultiWrite(tls, pParse)
				_sqlite3GenerateRowDelete(tls, pParse, pTab, pTrigger, iDataCur, iIdxCur, regNewData, int16(1), uint8(0), uint8(OE_Replace), uint8(1), -int32(1))
				_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), regTrigCnt, int32(1)) /* incr trigger cnt */
				nReplaceTrig = nReplaceTrig + 1
			} else {
				/* This OP_Delete opcode fires the pre-update-hook only. It does
				 ** not modify the b-tree. It is more efficient to let the coming
				 ** OP_Insert replace the existing entry than it is to delete the
				 ** existing entry and then insert a new one. */
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Delete), iDataCur, int32(OPFLAG_ISNOOP))
				_sqlite3VdbeAppendP4(tls, v, pTab, -int32(5))
				if (*TTable)(unsafe.Pointer(pTab)).FpIndex != 0 {
					_sqlite3MultiWrite(tls, pParse)
					_sqlite3GenerateRowIndexDelete(tls, pParse, pTab, iDataCur, iIdxCur, uintptr(0), -int32(1))
				}
			}
			seenReplace = int32(1)
		case int32(OE_Update):
			_sqlite3UpsertDoUpdate(tls, pParse, pUpsert, pTab, uintptr(0), iDataCur)
			fallthrough
		case int32(OE_Ignore):
			_sqlite3VdbeGoto(tls, v, ignoreDest)
			break
		}
		_sqlite3VdbeResolveLabel(tls, v, addrRowidOk)
		if pUpsert != 0 && pUpsertClause != pUpsert {
			upsertIpkReturn = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto))
		} else {
			if ipkTop != 0 {
				ipkBottom = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto))
				_sqlite3VdbeJumpHere(tls, v, ipkTop-int32(1))
			}
		}
	}
	/* Test all UNIQUE constraints by creating entries for each UNIQUE
	 ** index and making sure that duplicate entries do not already exist.
	 ** Compute the revised record entries for indices as we go.
	 **
	 ** This loop also handles the case of the PRIMARY KEY index for a
	 ** WITHOUT ROWID table.
	 */
	pIdx = _indexIteratorFirst(tls, bp+4, bp)
	for {
		if !(pIdx != 0) {
			break
		} /* First opcode in the conflict check logic */
		if **(**int32)(__ccgo_up(aRegIdx + uintptr(**(**int32)(__ccgo_up(bp)))*4)) == 0 {
			goto _9
		} /* Skip indices that do not change */
		if pUpsert != 0 {
			pUpsertClause = _sqlite3UpsertOfIndex(tls, pUpsert, pIdx)
			if upsertIpkDelay != 0 && pUpsertClause == pUpsert {
				_sqlite3VdbeJumpHere(tls, v, upsertIpkDelay)
			}
		}
		addrUniqueOk = _sqlite3VdbeMakeLabel(tls, pParse)
		if int32(bAffinityDone) == 0 {
			_sqlite3TableAffinity(tls, v, pTab, regNewData+int32(1))
			bAffinityDone = uint8(1)
		}
		iThisCur = iIdxCur + **(**int32)(__ccgo_up(bp))
		/* Skip partial indices for which the WHERE clause is not true */
		if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, **(**int32)(__ccgo_up(aRegIdx + uintptr(**(**int32)(__ccgo_up(bp)))*4)))
			(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = -(regNewData + int32(1))
			_sqlite3ExprIfFalseDup(tls, pParse, (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere, addrUniqueOk, int32(SQLITE_JUMPIFNULL))
			(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = 0
		}
		/* Create a record for this index entry as it should appear after
		 ** the insert or update.  Store that record in the aRegIdx[ix] register
		 */
		regIdx = **(**int32)(__ccgo_up(aRegIdx + uintptr(**(**int32)(__ccgo_up(bp)))*4)) + int32(1)
		i = 0
		for {
			if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) {
				break
			}
			iField = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2)))
			if iField == -int32(2) {
				(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = -(regNewData + int32(1))
				_sqlite3ExprCodeCopy(tls, pParse, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*20))).FpExpr, regIdx+i)
				(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = 0
			} else {
				if iField == -int32(1) || iField == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
					x = regNewData
					_sqlite3VdbeAddOp2(tls, v, int32(OP_IntCopy), x, regIdx+i)
				} else {
					x = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(iField))) + regNewData + int32(1)
					_sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), x, regIdx+i)
				}
			}
			goto _10
		_10:
			;
			i = i + 1
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regIdx, int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn), **(**int32)(__ccgo_up(aRegIdx + uintptr(**(**int32)(__ccgo_up(bp)))*4)))
		/* In an UPDATE operation, if this index is the PRIMARY KEY index
		 ** of a WITHOUT ROWID table and there has been no change the
		 ** primary key, then no collision is possible.  The collision detection
		 ** logic below can all be skipped. */
		if isUpdate != 0 && pPk == pIdx && int32(pkChng) == 0 {
			_sqlite3VdbeResolveLabel(tls, v, addrUniqueOk)
			goto _9
		}
		/* Find out what action to take in case there is a uniqueness conflict */
		onError = int32((*TIndex)(unsafe.Pointer(pIdx)).FonError)
		if onError == OE_None {
			_sqlite3VdbeResolveLabel(tls, v, addrUniqueOk)
			goto _9 /* pIdx is not a UNIQUE index */
		}
		if int32(overrideError) != int32(OE_Default) {
			onError = int32(overrideError)
		} else {
			if onError == int32(OE_Default) {
				onError = int32(OE_Abort)
			}
		}
		/* Figure out if the upsert clause applies to this index */
		if pUpsertClause != 0 {
			if int32((*TUpsert)(unsafe.Pointer(pUpsertClause)).FisDoUpdate) == 0 {
				onError = int32(OE_Ignore) /* DO NOTHING is the same as INSERT OR IGNORE */
			} else {
				onError = int32(OE_Update) /* DO UPDATE */
			}
		}
		/* Collision detection may be omitted if all of the following are true:
		 **   (1) The conflict resolution algorithm is REPLACE
		 **   (2) The table is a WITHOUT ROWID table
		 **   (3) There are no secondary indexes on the table
		 **   (4) No delete triggers need to be fired if there is a conflict
		 **   (5) No FK constraint counters need to be updated if a conflict occurs.
		 **
		 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
		 ** must be explicitly deleted in order to ensure any pre-update hook
		 ** is invoked.  */
		/* Check to see if the new index entry will be unique */
		addrConflictCk = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_NoConflict), iThisCur, addrUniqueOk, regIdx, int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol))
		/* Generate code to handle collisions */
		if pIdx == pPk {
			v2 = regIdx
		} else {
			v2 = _sqlite3GetTempRange(tls, pParse, nPkField)
		}
		regR = v2
		if isUpdate != 0 || onError == int32(OE_Replace) {
			if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxRowid), iThisCur, regR)
				/* Conflict only if the rowid of the existing index entry
				 ** is different from old-rowid */
				if isUpdate != 0 {
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), regR, addrUniqueOk, regOldData)
					_sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NOTNULL))
				}
			} else {
				/* Extract the PRIMARY KEY from the end of the index entry and
				 ** store it in registers regR..regR+nPk-1 */
				if pIdx != pPk {
					i = 0
					for {
						if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
							break
						}
						x1 = _sqlite3TableColumnToIndex(tls, pIdx, int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))))
						_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iThisCur, x1, regR+i)
						goto _12
					_12:
						;
						i = i + 1
					}
				}
				if isUpdate != 0 {
					/* If currently processing the PRIMARY KEY of a WITHOUT ROWID
					 ** table, only conflict if the new PRIMARY KEY values are actually
					 ** different from the old.  See TH3 withoutrowid04.test.
					 **
					 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
					 ** of the matched index row are different from the original PRIMARY
					 ** KEY values of this row before the update.  */
					addrJump = _sqlite3VdbeCurrentAddr(tls, v) + int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)
					op = int32(OP_Ne)
					if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
						v2 = regIdx
					} else {
						v2 = regR
					}
					regCmp = v2
					i = 0
					for {
						if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
							break
						}
						p4 = _sqlite3LocateCollSeq(tls, pParse, **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(i)*4)))
						x1 = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2)))
						if i == int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)-int32(1) {
							addrJump = addrUniqueOk
							op = int32(OP_Eq)
						}
						x1 = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(x1)))
						_sqlite3VdbeAddOp4(tls, v, op, regOldData+int32(1)+x1, addrJump, regCmp+i, p4, -int32(2))
						_sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NOTNULL))
						goto _14
					_14:
						;
						i = i + 1
					}
				}
			}
		}
		/* Generate code that executes if the new index entry is not unique */
		switch onError {
		case int32(OE_Rollback):
			fallthrough
		case int32(OE_Abort):
			fallthrough
		case int32(OE_Fail):
			_sqlite3UniqueConstraint(tls, pParse, onError, pIdx)
		case int32(OE_Update):
			_sqlite3UpsertDoUpdate(tls, pParse, pUpsert, pTab, pIdx, iIdxCur+**(**int32)(__ccgo_up(bp)))
			fallthrough
		case int32(OE_Ignore):
			_sqlite3VdbeGoto(tls, v, ignoreDest)
		default: /* Number of opcodes in conflict check logic */
			nConflictCk = _sqlite3VdbeCurrentAddr(tls, v) - addrConflictCk
			if regTrigCnt != 0 {
				_sqlite3MultiWrite(tls, pParse)
				nReplaceTrig = nReplaceTrig + 1
			}
			if pTrigger != 0 && isUpdate != 0 {
				_sqlite3VdbeAddOp1(tls, v, int32(OP_CursorLock), iDataCur)
			}
			if pIdx == pPk {
				v2 = int32(ONEPASS_SINGLE)
			} else {
				v2 = ONEPASS_OFF
			}
			_sqlite3GenerateRowDelete(tls, pParse, pTab, pTrigger, iDataCur, iIdxCur, regR, int16(nPkField), uint8(0), uint8(OE_Replace), uint8(v2), iThisCur)
			if pTrigger != 0 && isUpdate != 0 {
				_sqlite3VdbeAddOp1(tls, v, int32(OP_CursorUnlock), iDataCur)
			}
			if regTrigCnt != 0 { /* Jump destination to bypass recheck logic */
				_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), regTrigCnt, int32(1)) /* incr trigger cnt */
				addrBypass = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto))            /* Bypass recheck */
				/* Here we insert code that will be invoked after all constraint
				 ** checks have run, if and only if one or more replace triggers
				 ** fired. */
				_sqlite3VdbeResolveLabel(tls, v, lblRecheckOk)
				lblRecheckOk = _sqlite3VdbeMakeLabel(tls, pParse)
				if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 {
					/* Bypass the recheck if this partial index is not defined
					 ** for the current row */
					_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regIdx-int32(1), lblRecheckOk)
				}
				/* Copy the constraint check code from above, except change
				 ** the constraint-ok jump destination to be the address of
				 ** the next retest block */
				for nConflictCk > 0 { /* Conflict check opcode to copy */
					/* The sqlite3VdbeAddOp4() call might reallocate the opcode array.
					 ** Hence, make a complete copy of the opcode, rather than using
					 ** a pointer to the opcode. */
					**(**TVdbeOp)(__ccgo_up(bp + 20)) = **(**TVdbeOp)(__ccgo_up(_sqlite3VdbeGetOp(tls, v, addrConflictCk)))
					if int32((**(**TVdbeOp)(__ccgo_up(bp + 20))).Fopcode) != int32(OP_IdxRowid) {
						if int32(_sqlite3OpcodeProperty[(**(**TVdbeOp)(__ccgo_up(bp + 20))).Fopcode])&int32(OPFLG_JUMP) != 0 {
							p2 = lblRecheckOk
						} else {
							p2 = (**(**TVdbeOp)(__ccgo_up(bp + 20))).Fp2
						}
						if int32((**(**TVdbeOp)(__ccgo_up(bp + 20))).Fp4type) == -int32(3) {
							v8 = uintptr(*(*int32)(unsafe.Pointer(bp + 20 + 16)))
						} else {
							v8 = *(*uintptr)(unsafe.Pointer(bp + 20 + 16))
						}
						zP4 = v8
						_sqlite3VdbeAddOp4(tls, v, int32((**(**TVdbeOp)(__ccgo_up(bp + 20))).Fopcode), (**(**TVdbeOp)(__ccgo_up(bp + 20))).Fp1, p2, (**(**TVdbeOp)(__ccgo_up(bp + 20))).Fp3, zP4, int32((**(**TVdbeOp)(__ccgo_up(bp + 20))).Fp4type))
						_sqlite3VdbeChangeP5(tls, v, (**(**TVdbeOp)(__ccgo_up(bp + 20))).Fp5)
					}
					nConflictCk = nConflictCk - 1
					addrConflictCk = addrConflictCk + 1
				}
				/* If the retest fails, issue an abort */
				_sqlite3UniqueConstraint(tls, pParse, int32(OE_Abort), pIdx)
				_sqlite3VdbeJumpHere(tls, v, addrBypass) /* Terminate the recheck bypass */
			}
			seenReplace = int32(1)
			break
		}
		_sqlite3VdbeResolveLabel(tls, v, addrUniqueOk)
		if regR != regIdx {
			_sqlite3ReleaseTempRange(tls, pParse, regR, nPkField)
		}
		if pUpsertClause != 0 && upsertIpkReturn != 0 && _sqlite3UpsertNextIsIPK(tls, pUpsertClause) != 0 {
			_sqlite3VdbeGoto(tls, v, upsertIpkDelay+int32(1))
			_sqlite3VdbeJumpHere(tls, v, upsertIpkReturn)
			upsertIpkReturn = 0
		}
		goto _9
	_9:
		;
		pIdx = _indexIteratorNext(tls, bp+4, bp)
	}
	/* If the IPK constraint is a REPLACE, run it last */
	if ipkTop != 0 {
		_sqlite3VdbeGoto(tls, v, ipkTop)
		_sqlite3VdbeJumpHere(tls, v, ipkBottom)
	}
	/* Recheck all uniqueness constraints after replace triggers have run */
	if nReplaceTrig != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_IfNot), regTrigCnt, lblRecheckOk)
		if !(pPk != 0) {
			if isUpdate != 0 {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), regNewData, addrRecheck, regOldData)
				_sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NOTNULL))
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDataCur, addrRecheck, regNewData)
			_sqlite3RowidConstraint(tls, pParse, int32(OE_Abort), pTab)
		} else {
			_sqlite3VdbeGoto(tls, v, addrRecheck)
		}
		_sqlite3VdbeResolveLabel(tls, v, lblRecheckOk)
	}
	/* Generate the table record */
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
		regRec = **(**int32)(__ccgo_up(aRegIdx + uintptr(**(**int32)(__ccgo_up(bp)))*4))
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regNewData+int32(1), int32((*TTable)(unsafe.Pointer(pTab)).FnNVCol), regRec)
		if !(bAffinityDone != 0) {
			_sqlite3TableAffinity(tls, v, pTab, 0)
		}
	}
	**(**int32)(__ccgo_up(pbMayReplace)) = seenReplace
}

// C documentation
//
//	/*
//	** Generate code that will assemble an index key and stores it in register
//	** regOut.  The key with be for index pIdx which is an index on pTab.
//	** iCur is the index of a cursor open on the pTab table and pointing to
//	** the entry that needs indexing.  If pTab is a WITHOUT ROWID table, then
//	** iCur must be the cursor of the PRIMARY KEY index.
//	**
//	** Return a register number which is the first in a block of
//	** registers that holds the elements of the index key.  The
//	** block of registers has already been deallocated by the time
//	** this routine returns.
//	**
//	** If *piPartIdxLabel is not NULL, fill it in with a label and jump
//	** to that label if pIdx is a partial index that should be skipped.
//	** The label should be resolved using sqlite3ResolvePartIdxLabel().
//	** A partial index should be skipped if its WHERE clause evaluates
//	** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
//	** will be set to zero which is an empty label that is ignored by
//	** sqlite3ResolvePartIdxLabel().
//	**
//	** The pPrior and regPrior parameters are used to implement a cache to
//	** avoid unnecessary register loads.  If pPrior is not NULL, then it is
//	** a pointer to a different index for which an index key has just been
//	** computed into register regPrior.  If the current pIdx index is generating
//	** its key into the same sequence of registers and if pPrior and pIdx share
//	** a column in common, then the register corresponding to that column already
//	** holds the correct value and the loading of that register is skipped.
//	** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
//	** on a table with multiple indices, and especially with the ROWID or
//	** PRIMARY KEY columns of the index.
//	*/
func _sqlite3GenerateIndexKey(tls *libc.TLS, pParse uintptr, pIdx uintptr, iDataCur int32, regOut int32, prefixOnly int32, piPartIdxLabel uintptr, pPrior uintptr, regPrior int32) (r int32) {
	var j, nCol, regBase, v1 int32
	var v uintptr
	_, _, _, _, _ = j, nCol, regBase, v, v1
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	if piPartIdxLabel != 0 {
		if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 {
			**(**int32)(__ccgo_up(piPartIdxLabel)) = _sqlite3VdbeMakeLabel(tls, pParse)
			(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = iDataCur + int32(1)
			_sqlite3ExprIfFalseDup(tls, pParse, (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere, **(**int32)(__ccgo_up(piPartIdxLabel)), int32(SQLITE_JUMPIFNULL))
			(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = 0
			pPrior = uintptr(0) /* Ticket a9efb42811fa41ee 2019-11-02;
			 ** pPartIdxWhere may have corrupted regPrior registers */
		} else {
			**(**int32)(__ccgo_up(piPartIdxLabel)) = 0
		}
	}
	if prefixOnly != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 {
		v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
	} else {
		v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
	}
	nCol = v1
	regBase = _sqlite3GetTempRange(tls, pParse, nCol)
	if pPrior != 0 && (regBase != regPrior || (*TIndex)(unsafe.Pointer(pPrior)).FpPartIdxWhere != 0) {
		pPrior = uintptr(0)
	}
	j = 0
	for {
		if !(j < nCol) {
			break
		}
		if pPrior != 0 && int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPrior)).FaiColumn + uintptr(j)*2))) == int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2))) && int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPrior)).FaiColumn + uintptr(j)*2))) != -int32(2) {
			/* This column was already computed by the previous index */
			goto _2
		}
		_sqlite3ExprCodeLoadIndexColumn(tls, pParse, pIdx, iDataCur, j, regBase+j)
		if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2))) >= 0 {
			/* If the column affinity is REAL but the number is an integer, then it
			 ** might be stored in the table as an integer (using a compact
			 ** representation) then converted to REAL by an OP_RealAffinity opcode.
			 ** But we are getting ready to store this value back into an index, where
			 ** it should be converted by to INTEGER again.  So omit the
			 ** OP_RealAffinity opcode if it is present */
			_sqlite3VdbeDeletePriorOpcode(tls, v, uint8(OP_RealAffinity))
		}
		goto _2
	_2:
		;
		j = j + 1
	}
	if regOut != 0 {
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regBase, nCol, regOut)
	}
	_sqlite3ReleaseTempRange(tls, pParse, regBase, nCol)
	return regBase
}

// C documentation
//
//	/*
//	** This routine generates VDBE code that causes a single row of a
//	** single table to be deleted.  Both the original table entry and
//	** all indices are removed.
//	**
//	** Preconditions:
//	**
//	**   1.  iDataCur is an open cursor on the btree that is the canonical data
//	**       store for the table.  (This will be either the table itself,
//	**       in the case of a rowid table, or the PRIMARY KEY index in the case
//	**       of a WITHOUT ROWID table.)
//	**
//	**   2.  Read/write cursors for all indices of pTab must be open as
//	**       cursor number iIdxCur+i for the i-th index.
//	**
//	**   3.  The primary key for the row to be deleted must be stored in a
//	**       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
//	**       that a search record formed from OP_MakeRecord is contained in the
//	**       single memory location iPk.
//	**
//	** eMode:
//	**   Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
//	**   ONEPASS_MULTI.  If eMode is not ONEPASS_OFF, then the cursor
//	**   iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
//	**   then this function must seek iDataCur to the entry identified by iPk
//	**   and nPk before reading from it.
//	**
//	**   If eMode is ONEPASS_MULTI, then this call is being made as part
//	**   of a ONEPASS delete that affects multiple rows. In this case, if
//	**   iIdxNoSeek is a valid cursor number (>=0) and is not the same as
//	**   iDataCur, then its position should be preserved following the delete
//	**   operation. Or, if iIdxNoSeek is not a valid cursor number, the
//	**   position of iDataCur should be preserved instead.
//	**
//	** iIdxNoSeek:
//	**   If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
//	**   then it identifies an index cursor (from within array of cursors
//	**   starting at iIdxCur) that already points to the index entry to be deleted.
//	**   Except, this optimization is disabled if there are BEFORE triggers since
//	**   the trigger body might have moved the cursor.
//	*/
func _sqlite3GenerateRowDelete(tls *libc.TLS, pParse uintptr, pTab uintptr, pTrigger uintptr, iDataCur int32, iIdxCur int32, iPk int32, nPk Ti16, count Tu8, onconf Tu8, eMode Tu8, iIdxNoSeek int32) {
	var addrStart, iCol, iLabel, iOld, kk, v1 int32
	var mask Tu32
	var opSeek, p5 Tu8
	var v uintptr
	_, _, _, _, _, _, _, _, _, _ = addrStart, iCol, iLabel, iOld, kk, mask, opSeek, p5, v, v1
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Vdbe */
	iOld = 0                                     /* Seek opcode */
	/* Vdbe is guaranteed to have been allocated by this stage. */
	/* Seek cursor iCur to the row to delete. If this row no longer exists
	 ** (this can happen if a trigger program has already deleted it), do
	 ** not attempt to delete it or fire any DELETE triggers.  */
	iLabel = _sqlite3VdbeMakeLabel(tls, pParse)
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
		v1 = int32(OP_NotExists)
	} else {
		v1 = int32(OP_NotFound)
	}
	opSeek = uint8(v1)
	if int32(eMode) == ONEPASS_OFF {
		_sqlite3VdbeAddOp4Int(tls, v, int32(opSeek), iDataCur, iLabel, iPk, int32(nPk))
	}
	/* If there are any triggers to fire, allocate a range of registers to
	 ** use for the old.* references in the triggers.  */
	if _sqlite3FkRequired(tls, pParse, pTab, uintptr(0), 0) != 0 || pTrigger != 0 { /* Start of BEFORE trigger programs */
		/* TODO: Could use temporary registers here. Also could attempt to
		 ** avoid copying the contents of the rowid register.  */
		mask = _sqlite3TriggerColmask(tls, pParse, pTrigger, uintptr(0), 0, libc.Int32FromInt32(TRIGGER_BEFORE)|libc.Int32FromInt32(TRIGGER_AFTER), pTab, int32(onconf))
		mask = mask | _sqlite3FkOldmask(tls, pParse, pTab)
		iOld = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += int32(1) + int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
		/* Populate the OLD.* pseudo-table register array. These values will be
		 ** used by any BEFORE and AFTER triggers that exist.  */
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), iPk, iOld)
		iCol = 0
		for {
			if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			if mask == uint32(0xffffffff) || iCol <= int32(31) && mask&(libc.Uint32FromInt32(1)<<iCol) != uint32(0) {
				kk = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(iCol)))
				_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iDataCur, iCol, iOld+kk+int32(1))
			}
			goto _2
		_2:
			;
			iCol = iCol + 1
		}
		/* Invoke BEFORE DELETE trigger programs. */
		addrStart = _sqlite3VdbeCurrentAddr(tls, v)
		_sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_DELETE), uintptr(0), int32(TRIGGER_BEFORE), pTab, iOld, int32(onconf), iLabel)
		/* If any BEFORE triggers were coded, then seek the cursor to the
		 ** row to be deleted again. It may be that the BEFORE triggers moved
		 ** the cursor or already deleted the row that the cursor was
		 ** pointing to.
		 **
		 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
		 ** may have moved that cursor.
		 */
		if addrStart < _sqlite3VdbeCurrentAddr(tls, v) {
			_sqlite3VdbeAddOp4Int(tls, v, int32(opSeek), iDataCur, iLabel, iPk, int32(nPk))
			iIdxNoSeek = -int32(1)
		}
		/* Do FK processing. This call checks that any FK constraints that
		 ** refer to this table (i.e. constraints attached to other tables)
		 ** are not violated by deleting this row.  */
		_sqlite3FkCheck(tls, pParse, pTab, iOld, 0, uintptr(0), 0)
	}
	/* Delete the index and table entries. Skip this step if pTab is really
	 ** a view (in which case the only effect of the DELETE statement is to
	 ** fire the INSTEAD OF triggers).
	 **
	 ** If variable 'count' is non-zero, then this OP_Delete instruction should
	 ** invoke the update-hook. The pre-update-hook, on the other hand should
	 ** be invoked unless table pTab is a system table. The difference is that
	 ** the update-hook is not invoked for rows removed by REPLACE, but the
	 ** pre-update-hook is.
	 */
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VIEW)) {
		p5 = uint8(0)
		_sqlite3GenerateRowIndexDelete(tls, pParse, pTab, iDataCur, iIdxCur, uintptr(0), iIdxNoSeek)
		if count != 0 {
			v1 = int32(OPFLAG_NCHANGE)
		} else {
			v1 = 0
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Delete), iDataCur, v1)
		if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 || 0 == Xsqlite3_stricmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+14050) {
			_sqlite3VdbeAppendP4(tls, v, pTab, -int32(5))
		}
		if int32(eMode) != ONEPASS_OFF {
			_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_AUXDELETE))
		}
		if iIdxNoSeek >= 0 && iIdxNoSeek != iDataCur {
			_sqlite3VdbeAddOp1(tls, v, int32(OP_Delete), iIdxNoSeek)
		}
		if int32(eMode) == int32(ONEPASS_MULTI) {
			p5 = uint8(int32(p5) | libc.Int32FromInt32(OPFLAG_SAVEPOSITION))
		}
		_sqlite3VdbeChangeP5(tls, v, uint16(p5))
	}
	/* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
	 ** handle rows (possibly in other tables) that refer via a foreign key
	 ** to the row just deleted. */
	_sqlite3FkActions(tls, pParse, pTab, uintptr(0), iOld, uintptr(0), 0)
	/* Invoke AFTER DELETE trigger programs. */
	if pTrigger != 0 {
		_sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_DELETE), uintptr(0), int32(TRIGGER_AFTER), pTab, iOld, int32(onconf), iLabel)
	}
	/* Jump here if the row had already been deleted before any BEFORE
	 ** trigger programs were invoked. Or if a trigger program throws a
	 ** RAISE(IGNORE) exception.  */
	_sqlite3VdbeResolveLabel(tls, v, iLabel)
}

// C documentation
//
//	/*
//	** This routine generates VDBE code that causes the deletion of all
//	** index entries associated with a single row of a single table, pTab
//	**
//	** Preconditions:
//	**
//	**   1.  A read/write cursor "iDataCur" must be open on the canonical storage
//	**       btree for the table pTab.  (This will be either the table itself
//	**       for rowid tables or to the primary key index for WITHOUT ROWID
//	**       tables.)
//	**
//	**   2.  Read/write cursors for all indices of pTab must be open as
//	**       cursor number iIdxCur+i for the i-th index.  (The pTab->pIndex
//	**       index is the 0-th index.)
//	**
//	**   3.  The "iDataCur" cursor must be already be positioned on the row
//	**       that is to be deleted.
//	*/
func _sqlite3GenerateRowIndexDelete(tls *libc.TLS, pParse uintptr, pTab uintptr, iDataCur int32, iIdxCur int32, aRegIdx uintptr, iIdxNoSeek int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, r1, v3 int32
	var pIdx, pPk, pPrior, v, v1 uintptr
	var _ /* iPartIdxLabel at bp+0 */ int32
	_, _, _, _, _, _, _, _ = i, pIdx, pPk, pPrior, r1, v, v1, v3 /* Index loop counter */
	r1 = -int32(1)                                               /* Current index */
	pPrior = uintptr(0)                                          /* PRIMARY KEY index, or NULL for rowid tables */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
		v1 = uintptr(0)
	} else {
		v1 = _sqlite3PrimaryKeyIndex(tls, pTab)
	}
	pPk = v1
	i = 0
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		if aRegIdx != uintptr(0) && **(**int32)(__ccgo_up(aRegIdx + uintptr(i)*4)) == 0 {
			goto _2
		}
		if pIdx == pPk {
			goto _2
		}
		if iIdxCur+i == iIdxNoSeek {
			goto _2
		}
		r1 = _sqlite3GenerateIndexKey(tls, pParse, pIdx, iDataCur, 0, int32(1), bp, pPrior, r1)
		if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 {
			v3 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
		} else {
			v3 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_IdxDelete), iIdxCur+i, r1, v3)
		_sqlite3VdbeChangeP4(tls, v, -int32(1), pIdx, -int32(6))
		_sqlite3ResolvePartIdxLabel(tls, pParse, **(**int32)(__ccgo_up(bp)))
		pPrior = pIdx
		goto _2
	_2:
		;
		i = i + 1
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
	}
}

// C documentation
//
//	/*
//	** If zNum represents an integer that will fit in 32-bits, then set
//	** *pValue to that integer and return true.  Otherwise return false.
//	**
//	** This routine accepts both decimal and hexadecimal notation for integers.
//	**
//	** Any non-numeric characters that following zNum are ignored.
//	** This is different from sqlite3Atoi64() which requires the
//	** input number to be zero-terminated.
//	*/
func _sqlite3GetInt32(tls *libc.TLS, zNum uintptr, pValue uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var c, i, neg, v3 int32
	var v Tsqlite_int64
	var v4 bool
	var _ /* u at bp+0 */ Tu32
	_, _, _, _, _, _ = c, i, neg, v, v3, v4
	v = 0
	neg = 0
	if int32(**(**int8)(__ccgo_up(zNum))) == int32('-') {
		neg = int32(1)
		zNum = zNum + 1
	} else {
		if int32(**(**int8)(__ccgo_up(zNum))) == int32('+') {
			zNum = zNum + 1
		} else {
			if int32(**(**int8)(__ccgo_up(zNum))) == int32('0') && (int32(**(**int8)(__ccgo_up(zNum + 1))) == int32('x') || int32(**(**int8)(__ccgo_up(zNum + 1))) == int32('X')) && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zNum + 2)))])&int32(0x08) != 0 {
				**(**Tu32)(__ccgo_up(bp)) = uint32(0)
				zNum = zNum + uintptr(2)
				for int32(**(**int8)(__ccgo_up(zNum))) == int32('0') {
					zNum = zNum + 1
				}
				i = 0
				for {
					if !(i < int32(8) && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zNum + uintptr(i))))])&int32(0x08) != 0) {
						break
					}
					**(**Tu32)(__ccgo_up(bp)) = **(**Tu32)(__ccgo_up(bp))*uint32(16) + uint32(_sqlite3HexToInt(tls, int32(**(**int8)(__ccgo_up(zNum + uintptr(i))))))
					goto _1
				_1:
					;
					i = i + 1
				}
				if **(**Tu32)(__ccgo_up(bp))&uint32(0x80000000) == uint32(0) && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zNum + uintptr(i))))])&int32(0x08) == 0 {
					libc.Xmemcpy(tls, pValue, bp, uint32(4))
					return int32(1)
				} else {
					return 0
				}
			}
		}
	}
	if !(int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zNum)))])&libc.Int32FromInt32(0x04) != 0) {
		return 0
	}
	for int32(**(**int8)(__ccgo_up(zNum))) == int32('0') {
		zNum = zNum + 1
	}
	i = 0
	for {
		if v4 = i < int32(11); v4 {
			v3 = int32(**(**int8)(__ccgo_up(zNum + uintptr(i)))) - libc.Int32FromUint8('0')
			c = v3
		}
		if !(v4 && v3 >= 0 && c <= int32(9)) {
			break
		}
		v = v*int64(10) + int64(c)
		goto _2
	_2:
		;
		i = i + 1
	}
	/* The longest decimal representation of a 32 bit integer is 10 digits:
	 **
	 **             1234567890
	 **     2^31 -> 2147483648
	 */
	if i > int32(10) {
		return 0
	}
	if v-int64(neg) > int64(2147483647) {
		return 0
	}
	if neg != 0 {
		v = -v
	}
	**(**int32)(__ccgo_up(pValue)) = int32(v)
	return int32(1)
}

// C documentation
//
//	/*
//	** Allocate a single new register for use to hold some intermediate result.
//	*/
func _sqlite3GetTempReg(tls *libc.TLS, pParse uintptr) (r int32) {
	var v1 int32
	var v2 uintptr
	var v3 Tu8
	_, _, _ = v1, v2, v3
	if int32((*TParse)(unsafe.Pointer(pParse)).FnTempReg) == 0 {
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		return v1
	}
	v2 = pParse + 19
	*(*Tu8)(unsafe.Pointer(v2)) = *(*Tu8)(unsafe.Pointer(v2)) - 1
	v3 = *(*Tu8)(unsafe.Pointer(v2))
	return **(**int32)(__ccgo_up(pParse + 136 + uintptr(v3)*4))
}

// C documentation
//
//	/*
//	** Get a VDBE for the given parser context.  Create a new one if necessary.
//	** If an error occurs, return NULL and leave a message in pParse.
//	*/
func _sqlite3GetVdbe(tls *libc.TLS, pParse uintptr) (r uintptr) {
	if (*TParse)(unsafe.Pointer(pParse)).FpVdbe != 0 {
		return (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	}
	if (*TParse)(unsafe.Pointer(pParse)).FpToplevel == uintptr(0) && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_FactorOutConst)) == uint32(0) {
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 7, 0x80)
	}
	return _sqlite3VdbeCreate(tls, pParse)
}

// C documentation
//
//	/*
//	** If expression list pList contains an expression that was parsed with
//	** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
//	** pParse and return non-zero. Otherwise, return zero.
//	*/
func _sqlite3HasExplicitNulls(tls *libc.TLS, pParse uintptr, pList uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i int32
	var sf Tu8
	var v2 uintptr
	_, _, _ = i, sf, v2
	if pList != 0 {
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
				break
			}
			if int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*20 + 8 + 4))&0x20>>5)) != 0 {
				sf = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).Ffg.FsortFlags
				if int32(sf) == 0 || int32(sf) == int32(3) {
					v2 = __ccgo_ts + 16596
				} else {
					v2 = __ccgo_ts + 16602
				}
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16607, libc.VaList(bp+8, v2))
				return int32(1)
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	return 0
}

// C documentation
//
//	/*
//	** Return the size of the header added to each page by this module.
//	*/
func _sqlite3HeaderSizeBtree(tls *libc.TLS) (r int32) {
	return int32((libc.Uint32FromInt64(84) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7)))
}

// C documentation
//
//	/*
//	** Return the size of the header added by this middleware layer
//	** in the page-cache hierarchy.
//	*/
func _sqlite3HeaderSizePcache(tls *libc.TLS) (r int32) {
	return int32((libc.Uint32FromInt64(48) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7)))
}

// C documentation
//
//	/*
//	** Return the size of the header on each page of this PCACHE implementation.
//	*/
func _sqlite3HeaderSizePcache1(tls *libc.TLS) (r int32) {
	return int32((libc.Uint32FromInt64(32) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7)))
}

// C documentation
//
//	/*
//	** Append a new element to the given IdList.  Create a new IdList if
//	** need be.
//	**
//	** A new IdList is returned, or NULL if malloc() fails.
//	*/
func _sqlite3IdListAppend(tls *libc.TLS, pParse uintptr, pList uintptr, pToken uintptr) (r uintptr) {
	var db, pNew, v2 uintptr
	var i, v1 int32
	_, _, _, _, _ = db, i, pNew, v1, v2
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if pList == uintptr(0) {
		pList = _sqlite3DbMallocZero(tls, db, uint64(uint32(libc.UintptrFromInt32(0)+4)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(4)))
		if pList == uintptr(0) {
			return uintptr(0)
		}
	} else {
		pNew = _sqlite3DbRealloc(tls, db, pList, uint64(uint32(libc.UintptrFromInt32(0)+4)+uint32((*TIdList)(unsafe.Pointer(pList)).FnId+libc.Int32FromInt32(1))*libc.Uint32FromInt64(4)))
		if pNew == uintptr(0) {
			_sqlite3IdListDelete(tls, db, pList)
			return uintptr(0)
		}
		pList = pNew
	}
	v2 = pList
	v1 = *(*int32)(unsafe.Pointer(v2))
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	i = v1
	(*(*TIdList_item)(unsafe.Pointer(pList + 4 + uintptr(i)*4))).FzName = _sqlite3NameFromToken(tls, db, pToken)
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*(*TIdList_item)(unsafe.Pointer(pList + 4 + uintptr(i)*4))).FzName != 0 {
		_sqlite3RenameTokenMap(tls, pParse, (*(*TIdList_item)(unsafe.Pointer(pList + 4 + uintptr(i)*4))).FzName, pToken)
	}
	return pList
}

func _sqlite3IdListDup(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) {
	var i int32
	var pNew, pNewItem, pOldItem uintptr
	_, _, _, _ = i, pNew, pNewItem, pOldItem
	if p == uintptr(0) {
		return uintptr(0)
	}
	pNew = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(libc.UintptrFromInt32(0)+4)+uint32((*TIdList)(unsafe.Pointer(p)).FnId)*libc.Uint32FromInt64(4)))
	if pNew == uintptr(0) {
		return uintptr(0)
	}
	(*TIdList)(unsafe.Pointer(pNew)).FnId = (*TIdList)(unsafe.Pointer(p)).FnId
	i = 0
	for {
		if !(i < (*TIdList)(unsafe.Pointer(p)).FnId) {
			break
		}
		pNewItem = pNew + 4 + uintptr(i)*4
		pOldItem = p + 4 + uintptr(i)*4
		(*TIdList_item)(unsafe.Pointer(pNewItem)).FzName = _sqlite3DbStrDup(tls, db, (*TIdList_item)(unsafe.Pointer(pOldItem)).FzName)
		goto _1
	_1:
		;
		i = i + 1
	}
	return pNew
}

// C documentation
//
//	/*
//	** If the source-list item passed as an argument was augmented with an
//	** INDEXED BY clause, then try to locate the specified index. If there
//	** was such a clause and the named index cannot be found, return
//	** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
//	** pFrom->pIndex and return SQLITE_OK.
//	*/
func _sqlite3IndexedByLookup(tls *libc.TLS, pParse uintptr, pFrom uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pIdx, pTab, zIndexedBy uintptr
	_, _, _ = pIdx, pTab, zIndexedBy
	pTab = (*TSrcItem)(unsafe.Pointer(pFrom)).FpSTab
	zIndexedBy = *(*uintptr)(unsafe.Pointer(pFrom + 32))
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0 && _sqlite3StrICmp(tls, (*TIndex)(unsafe.Pointer(pIdx)).FzName, zIndexedBy) != 0) {
			break
		}
		goto _1
	_1:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
	}
	if !(pIdx != 0) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22248, libc.VaList(bp+8, zIndexedBy, 0))
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 8, 0x100)
		return int32(SQLITE_ERROR)
	}
	*(*uintptr)(unsafe.Pointer(pFrom + 36)) = pIdx
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Initialize all database files - the main database file, the file
//	** used to store temporary tables, and any additional database files
//	** created using ATTACH statements.  Return a success code.  If an
//	** error occurs, write an error message into *pzErrMsg.
//	**
//	** After a database is initialized, the DB_SchemaLoaded bit is set
//	** bit is set in the flags field of the Db structure.
//	*/
func _sqlite3Init(tls *libc.TLS, db uintptr, pzErrMsg uintptr) (r int32) {
	var commit_internal, i, rc int32
	_, _, _ = commit_internal, i, rc
	commit_internal = libc.BoolInt32(!((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&libc.Uint32FromInt32(DBFLAG_SchemaChange) != 0))
	(*Tsqlite3)(unsafe.Pointer(db)).Fenc = (*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).Fenc
	/* Do the main schema first */
	if !(int32((*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).FschemaFlags)&libc.Int32FromInt32(DB_SchemaLoaded) == libc.Int32FromInt32(DB_SchemaLoaded)) {
		rc = _sqlite3InitOne(tls, db, 0, pzErrMsg, uint32(0))
		if rc != 0 {
			return rc
		}
	}
	/* All other schemas after the main schema. The "temp" schema must be last */
	i = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1)
	for {
		if !(i > 0) {
			break
		}
		if !(int32((*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema)).FschemaFlags)&libc.Int32FromInt32(DB_SchemaLoaded) == libc.Int32FromInt32(DB_SchemaLoaded)) {
			rc = _sqlite3InitOne(tls, db, i, pzErrMsg, uint32(0))
			if rc != 0 {
				return rc
			}
		}
		goto _1
	_1:
		;
		i = i - 1
	}
	if commit_internal != 0 {
		_sqlite3CommitInternalChanges(tls, db)
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This is the callback routine for the code that initializes the
//	** database.  See sqlite3Init() below for additional information.
//	** This routine is also called from the OP_ParseSchema opcode of the VDBE.
//	**
//	** Each callback contains the following information:
//	**
//	**     argv[0] = type of object: "table", "index", "trigger", or "view".
//	**     argv[1] = name of thing being created
//	**     argv[2] = associated table if an index or trigger
//	**     argv[3] = root page number for table or index. 0 for trigger or view.
//	**     argv[4] = SQL text for the CREATE statement.
//	**
//	*/
func _sqlite3InitCallback(tls *libc.TLS, pInit uintptr, argc int32, argv uintptr, NotUsed uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pData, pIndex uintptr
	var iDb, rc int32
	var saved_iDb Tu8
	var _ /* pStmt at bp+0 */ uintptr
	_, _, _, _, _, _ = db, iDb, pData, pIndex, rc, saved_iDb
	pData = pInit
	db = (*TInitData)(unsafe.Pointer(pData)).Fdb
	iDb = (*TInitData)(unsafe.Pointer(pData)).FiDb
	_ = NotUsed
	_ = argc
	**(**Tu32)(__ccgo_up(db + 24)) |= uint32(DBFLAG_EncodingFixed)
	if argv == uintptr(0) {
		return 0
	} /* Might happen if EMPTY_RESULT_CALLBACKS are on */
	(*TInitData)(unsafe.Pointer(pData)).FnInitRow = (*TInitData)(unsafe.Pointer(pData)).FnInitRow + 1
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_corruptSchema(tls, pData, argv, uintptr(0))
		return int32(1)
	}
	if **(**uintptr)(__ccgo_up(argv + 3*4)) == uintptr(0) {
		_corruptSchema(tls, pData, argv, uintptr(0))
	} else {
		if **(**uintptr)(__ccgo_up(argv + 4*4)) != 0 && int32('c') == int32(_sqlite3UpperToLower[uint8(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(argv + 4*4)))))]) && int32('r') == int32(_sqlite3UpperToLower[uint8(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(argv + 4*4)) + 1)))]) {
			saved_iDb = (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb
			/* Return code from sqlite3_prepare() */
			(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(iDb)
			if _sqlite3GetUInt32(tls, **(**uintptr)(__ccgo_up(argv + 3*4)), db+176) == 0 || (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum > (*TInitData)(unsafe.Pointer(pData)).FmxPage && (*TInitData)(unsafe.Pointer(pData)).FmxPage > uint32(0) {
				if _sqlite3Config.FbExtraSchemaChecks != 0 {
					_corruptSchema(tls, pData, argv, __ccgo_ts+16962)
				}
			}
			libc.SetBitFieldPtr8Uint32(db+176+8, libc.Uint32FromInt32(0), 0, 0x1)
			(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit = argv
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			_sqlite3Prepare(tls, db, **(**uintptr)(__ccgo_up(argv + 4*4)), -int32(1), uint32(0), uintptr(0), bp, uintptr(0))
			rc = (*Tsqlite3)(unsafe.Pointer(db)).FerrCode
			(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = saved_iDb
			/* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
			if SQLITE_OK != rc {
				if int32(uint32(*(*uint8)(unsafe.Pointer(db + 176 + 8))&0x1>>0)) != 0 {
				} else {
					if rc > (*TInitData)(unsafe.Pointer(pData)).Frc {
						(*TInitData)(unsafe.Pointer(pData)).Frc = rc
					}
					if rc == int32(SQLITE_NOMEM) {
						_sqlite3OomFault(tls, db)
					} else {
						if rc != int32(SQLITE_INTERRUPT) && rc&int32(0xFF) != int32(SQLITE_LOCKED) {
							_corruptSchema(tls, pData, argv, Xsqlite3_errmsg(tls, db))
						}
					}
				}
			}
			(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit = uintptr(unsafe.Pointer(&_sqlite3StdType)) /* Any array of string ptrs will do */
			Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp)))
		} else {
			if **(**uintptr)(__ccgo_up(argv + 1*4)) == uintptr(0) || **(**uintptr)(__ccgo_up(argv + 4*4)) != uintptr(0) && int32(**(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(argv + 4*4))))) != 0 {
				_corruptSchema(tls, pData, argv, uintptr(0))
			} else {
				pIndex = _sqlite3FindIndex(tls, db, **(**uintptr)(__ccgo_up(argv + 1*4)), (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName)
				if pIndex == uintptr(0) {
					_corruptSchema(tls, pData, argv, __ccgo_ts+21324)
				} else {
					if _sqlite3GetUInt32(tls, **(**uintptr)(__ccgo_up(argv + 3*4)), pIndex+44) == 0 || (*TIndex)(unsafe.Pointer(pIndex)).Ftnum < uint32(2) || (*TIndex)(unsafe.Pointer(pIndex)).Ftnum > (*TInitData)(unsafe.Pointer(pData)).FmxPage || _sqlite3IndexHasDuplicateRootPage(tls, pIndex) != 0 {
						if _sqlite3Config.FbExtraSchemaChecks != 0 {
							_corruptSchema(tls, pData, argv, __ccgo_ts+16962)
						}
					}
				}
			}
		}
	}
	return 0
}

// C documentation
//
//	/*
//	** Attempt to read the database schema and initialize internal
//	** data structures for a single database file.  The index of the
//	** database file is given by iDb.  iDb==0 is used for the main
//	** database.  iDb==1 should never be used.  iDb>=2 is used for
//	** auxiliary databases.  Return one of the SQLITE_ error codes to
//	** indicate success or failure.
//	*/
func _sqlite3InitOne(tls *libc.TLS, db uintptr, iDb int32, pzErrMsg uintptr, mFlags Tu32) (r int32) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var encoding Tu8
	var i, mask, openedTransaction, rc, size int32
	var pDb, zSchemaTabName, zSql, v1, v2 uintptr
	var xAuth Tsqlite3_xauth
	var _ /* azArg at bp+0 */ [6]uintptr
	var _ /* initData at bp+44 */ TInitData
	var _ /* meta at bp+24 */ [5]int32
	_, _, _, _, _, _, _, _, _, _, _, _ = encoding, i, mask, openedTransaction, pDb, rc, size, xAuth, zSchemaTabName, zSql, v1, v2
	openedTransaction = 0
	mask = int32((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&libc.Uint32FromInt32(DBFLAG_EncodingFixed) | uint32(^libc.Int32FromInt32(DBFLAG_EncodingFixed)))
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(1)
	/* Construct the in-memory representation schema tables (sqlite_schema or
	 ** sqlite_temp_schema) by invoking the parser directly.  The appropriate
	 ** table name will be inserted automatically by the parser so we can just
	 ** use the abbreviation "x" here.  The parser will also automatically tag
	 ** the schema table as read-only. */
	(**(**[6]uintptr)(__ccgo_up(bp)))[0] = __ccgo_ts + 10594
	if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
		v2 = __ccgo_ts + 7981
	} else {
		v2 = __ccgo_ts + 7501
	}
	v1 = v2
	zSchemaTabName = v1
	(**(**[6]uintptr)(__ccgo_up(bp)))[int32(1)] = v1
	(**(**[6]uintptr)(__ccgo_up(bp)))[int32(2)] = (**(**[6]uintptr)(__ccgo_up(bp)))[int32(1)]
	(**(**[6]uintptr)(__ccgo_up(bp)))[int32(3)] = __ccgo_ts + 21337
	(**(**[6]uintptr)(__ccgo_up(bp)))[int32(4)] = __ccgo_ts + 21339
	(**(**[6]uintptr)(__ccgo_up(bp)))[int32(5)] = uintptr(0)
	(**(**TInitData)(__ccgo_up(bp + 44))).Fdb = db
	(**(**TInitData)(__ccgo_up(bp + 44))).FiDb = iDb
	(**(**TInitData)(__ccgo_up(bp + 44))).Frc = SQLITE_OK
	(**(**TInitData)(__ccgo_up(bp + 44))).FpzErrMsg = pzErrMsg
	(**(**TInitData)(__ccgo_up(bp + 44))).FmInitFlags = mFlags
	(**(**TInitData)(__ccgo_up(bp + 44))).FnInitRow = uint32(0)
	(**(**TInitData)(__ccgo_up(bp + 44))).FmxPage = uint32(0)
	_sqlite3InitCallback(tls, bp+44, int32(5), bp, uintptr(0))
	**(**Tu32)(__ccgo_up(db + 24)) &= uint32(mask)
	if (**(**TInitData)(__ccgo_up(bp + 44))).Frc != 0 {
		rc = (**(**TInitData)(__ccgo_up(bp + 44))).Frc
		goto error_out
	}
	/* Create a cursor to hold the database open
	 */
	pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16
	if (*TDb)(unsafe.Pointer(pDb)).FpBt == uintptr(0) {
		v1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema + 78
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(DB_SchemaLoaded))
		rc = SQLITE_OK
		goto error_out
	}
	/* If there is not already a read-only (or read-write) transaction opened
	 ** on the b-tree database, open one now. If a transaction is opened, it
	 ** will be closed before this function returns.  */
	_sqlite3BtreeEnter(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt)
	if _sqlite3BtreeTxnState(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) == SQLITE_TXN_NONE {
		rc = _sqlite3BtreeBeginTrans(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, 0, uintptr(0))
		if rc != SQLITE_OK {
			_sqlite3SetString(tls, pzErrMsg, db, _sqlite3ErrStr(tls, rc))
			goto initone_error_out
		}
		openedTransaction = int32(1)
	}
	/* Get the database meta information.
	 **
	 ** Meta values are as follows:
	 **    meta[0]   Schema cookie.  Changes with each schema change.
	 **    meta[1]   File format of schema layer.
	 **    meta[2]   Size of the page cache.
	 **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
	 **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
	 **    meta[5]   User version
	 **    meta[6]   Incremental vacuum mode
	 **    meta[7]   unused
	 **    meta[8]   unused
	 **    meta[9]   unused
	 **
	 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
	 ** the possible values of meta[4].
	 */
	i = 0
	for {
		if !(i < int32(libc.Uint32FromInt64(20)/libc.Uint32FromInt64(4))) {
			break
		}
		_sqlite3BtreeGetMeta(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, i+int32(1), bp+24+uintptr(i)*4)
		goto _4
	_4:
		;
		i = i + 1
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ResetDatabase) != uint64(0) {
		libc.Xmemset(tls, bp+24, 0, uint32(20))
	}
	(*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fschema_cookie = (**(**[5]int32)(__ccgo_up(bp + 24)))[libc.Int32FromInt32(BTREE_SCHEMA_VERSION)-libc.Int32FromInt32(1)]
	/* If opening a non-empty database, check the text encoding. For the
	 ** main database, set sqlite3.enc to the encoding of the main database.
	 ** For an attached db, it is an error if the encoding is not the same
	 ** as sqlite3.enc.
	 */
	if (**(**[5]int32)(__ccgo_up(bp + 24)))[libc.Int32FromInt32(BTREE_TEXT_ENCODING)-libc.Int32FromInt32(1)] != 0 { /* text encoding */
		if iDb == 0 && (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_EncodingFixed) == uint32(0) {
			/* If opening the main database, set ENC(db). */
			encoding = uint8(int32(uint8((**(**[5]int32)(__ccgo_up(bp + 24)))[libc.Int32FromInt32(BTREE_TEXT_ENCODING)-libc.Int32FromInt32(1)])) & int32(3))
			if int32(encoding) == 0 {
				encoding = uint8(SQLITE_UTF8)
			}
			_sqlite3SetTextEncoding(tls, db, encoding)
		} else {
			/* If opening an attached database, the encoding much match ENC(db) */
			if (**(**[5]int32)(__ccgo_up(bp + 24)))[libc.Int32FromInt32(BTREE_TEXT_ENCODING)-libc.Int32FromInt32(1)]&int32(3) != int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc) {
				_sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+14563)
				rc = int32(SQLITE_ERROR)
				goto initone_error_out
			}
		}
	}
	(*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fenc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc
	if (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size == 0 {
		size = _sqlite3AbsInt32(tls, (**(**[5]int32)(__ccgo_up(bp + 24)))[libc.Int32FromInt32(BTREE_DEFAULT_CACHE_SIZE)-libc.Int32FromInt32(1)])
		if size == 0 {
			size = -int32(2000)
		}
		(*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size = size
		_sqlite3BtreeSetCacheSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size)
	}
	/*
	 ** file_format==1    Version 3.0.0.
	 ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
	 ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
	 ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
	 */
	(*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format = uint8((**(**[5]int32)(__ccgo_up(bp + 24)))[libc.Int32FromInt32(BTREE_FILE_FORMAT)-libc.Int32FromInt32(1)])
	if int32((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format) == 0 {
		(*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format = uint8(1)
	}
	if int32((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Ffile_format) > int32(SQLITE_MAX_FILE_FORMAT) {
		_sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+21411)
		rc = int32(SQLITE_ERROR)
		goto initone_error_out
	}
	/* Ticket #2804:  When we open a database in the newer file format,
	 ** clear the legacy_file_format pragma flag so that a VACUUM will
	 ** not downgrade the database and thus invalidate any descending
	 ** indices that the user might have created.
	 */
	if iDb == 0 && (**(**[5]int32)(__ccgo_up(bp + 24)))[libc.Int32FromInt32(BTREE_FILE_FORMAT)-libc.Int32FromInt32(1)] >= int32(4) {
		**(**Tu64)(__ccgo_up(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_LegacyFileFmt)
	}
	/* Read the schema information out of the schema tables
	 */
	(**(**TInitData)(__ccgo_up(bp + 44))).FmxPage = _sqlite3BtreeLastPage(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt)
	zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+21435, libc.VaList(bp+80, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, zSchemaTabName))
	xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0)
	rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_sqlite3InitCallback), bp+44, uintptr(0))
	(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth
	if rc == SQLITE_OK {
		rc = (**(**TInitData)(__ccgo_up(bp + 44))).Frc
	}
	_sqlite3DbFree(tls, db, zSql)
	if rc == SQLITE_OK {
		_sqlite3AnalysisLoad(tls, db, iDb)
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		rc = int32(SQLITE_NOMEM)
		_sqlite3ResetAllSchemasOfConnection(tls, db)
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16
	} else {
		if rc == SQLITE_OK || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_NoSchemaError) != 0 && rc != int32(SQLITE_NOMEM) {
			/* Hack: If the SQLITE_NoSchemaError flag is set, then consider
			 ** the schema loaded, even if errors (other than OOM) occurred. In
			 ** this situation the current sqlite3_prepare() operation will fail,
			 ** but the following one will attempt to compile the supplied statement
			 ** against whatever subset of the schema was loaded before the error
			 ** occurred.
			 **
			 ** The primary purpose of this is to allow access to the sqlite_schema
			 ** table even when its contents have been corrupted.
			 */
			v1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 78
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(DB_SchemaLoaded))
			rc = SQLITE_OK
		}
	}
	/* Jump here for an error that occurs after successfully allocating
	 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
	 ** before that point, jump to error_out.
	 */
	goto initone_error_out
initone_error_out:
	;
	if openedTransaction != 0 {
		_sqlite3BtreeCommit(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt)
	}
	_sqlite3BtreeLeave(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt)
	goto error_out
error_out:
	;
	if rc != 0 {
		if rc == int32(SQLITE_NOMEM) || rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<<libc.Int32FromInt32(8) {
			_sqlite3OomFault(tls, db)
		}
		_sqlite3ResetOneSchema(tls, db, iDb)
	}
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(0)
	return rc
}

// C documentation
//
//	/*
//	** This routine is called to handle SQL of the following forms:
//	**
//	**    insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
//	**    insert into TABLE (IDLIST) select
//	**    insert into TABLE (IDLIST) default values
//	**
//	** The IDLIST following the table name is always optional.  If omitted,
//	** then a list of all (non-hidden) columns for the table is substituted.
//	** The IDLIST appears in the pColumn parameter.  pColumn is NULL if IDLIST
//	** is omitted.
//	**
//	** For the pSelect parameter holds the values to be inserted for the
//	** first two forms shown above.  A VALUES clause is really just short-hand
//	** for a SELECT statement that omits the FROM clause and everything else
//	** that follows.  If the pSelect parameter is NULL, that means that the
//	** DEFAULT VALUES form of the INSERT statement is intended.
//	**
//	** The code generated follows one of four templates.  For a simple
//	** insert with data coming from a single-row VALUES clause, the code executes
//	** once straight down through.  Pseudo-code follows (we call this
//	** the "1st template"):
//	**
//	**         open write cursor to <table> and its indices
//	**         put VALUES clause expressions into registers
//	**         write the resulting record into <table>
//	**         cleanup
//	**
//	** The three remaining templates assume the statement is of the form
//	**
//	**   INSERT INTO <table> SELECT ...
//	**
//	** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
//	** in other words if the SELECT pulls all columns from a single table
//	** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
//	** if <table2> and <table1> are distinct tables but have identical
//	** schemas, including all the same indices, then a special optimization
//	** is invoked that copies raw records from <table2> over to <table1>.
//	** See the xferOptimization() function for the implementation of this
//	** template.  This is the 2nd template.
//	**
//	**         open a write cursor to <table>
//	**         open read cursor on <table2>
//	**         transfer all records in <table2> over to <table>
//	**         close cursors
//	**         foreach index on <table>
//	**           open a write cursor on the <table> index
//	**           open a read cursor on the corresponding <table2> index
//	**           transfer all records from the read to the write cursors
//	**           close cursors
//	**         end foreach
//	**
//	** The 3rd template is for when the second template does not apply
//	** and the SELECT clause does not read from <table> at any time.
//	** The generated code follows this template:
//	**
//	**         X <- A
//	**         goto B
//	**      A: setup for the SELECT
//	**         loop over the rows in the SELECT
//	**           load values into registers R..R+n
//	**           yield X
//	**         end loop
//	**         cleanup after the SELECT
//	**         end-coroutine X
//	**      B: open write cursor to <table> and its indices
//	**      C: yield X, at EOF goto D
//	**         insert the select result into <table> from R..R+n
//	**         goto C
//	**      D: cleanup
//	**
//	** The 4th template is used if the insert statement takes its
//	** values from a SELECT but the data is being inserted into a table
//	** that is also read as part of the SELECT.  In the third form,
//	** we have to use an intermediate table to store the results of
//	** the select.  The template is like this:
//	**
//	**         X <- A
//	**         goto B
//	**      A: setup for the SELECT
//	**         loop over the tables in the SELECT
//	**           load value into register R..R+n
//	**           yield X
//	**         end loop
//	**         cleanup after the SELECT
//	**         end co-routine R
//	**      B: open temp table
//	**      L: yield X, at EOF goto M
//	**         insert row from R..R+n into temp table
//	**         goto L
//	**      M: open write cursor to <table> and its indices
//	**         rewind temp table
//	**      C: loop over rows of intermediate table
//	**           transfer values form intermediate table into <table>
//	**         end loop
//	**      D: cleanup
//	*/
func _sqlite3Insert(tls *libc.TLS, pParse uintptr, pTabList uintptr, pSelect uintptr, pColumn uintptr, onError int32, pUpsert uintptr) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var aRegIdx, aTabColMap, db, pIdx, pIpk, pItem, pList, pNx, pSubq, pTab, pTrigger, pVTab, pX, v, v5 uintptr
	var addr1, addr11, addrCont, addrInsTop, addrL, addrTop, bUseSeek, endOfLoop, i, iDb, iRegStore, ipkColumn, isView, j, k, nColumn, nHidden, nIdx, rc, regAutoinc, regCols, regData, regFromSelect, regIns, regRec, regRowCount, regRowid, regTempRowid, regYield, srcTab, y, v1 int32
	var appendFlag, bIdListInOrder, useTempTable, withoutRowid Tu8
	var colFlags, v20 Tu32
	var _ /* dest at bp+8 */ TSelectDest
	var _ /* iDataCur at bp+0 */ int32
	var _ /* iIdxCur at bp+4 */ int32
	var _ /* isReplace at bp+76 */ int32
	var _ /* sNC at bp+40 */ TNameContext
	var _ /* tmask at bp+36 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aRegIdx, aTabColMap, addr1, addr11, addrCont, addrInsTop, addrL, addrTop, appendFlag, bIdListInOrder, bUseSeek, colFlags, db, endOfLoop, i, iDb, iRegStore, ipkColumn, isView, j, k, nColumn, nHidden, nIdx, pIdx, pIpk, pItem, pList, pNx, pSubq, pTab, pTrigger, pVTab, pX, rc, regAutoinc, regCols, regData, regFromSelect, regIns, regRec, regRowCount, regRowid, regTempRowid, regYield, srcTab, useTempTable, v, withoutRowid, y, v1, v20, v5 /* Number of columns in the data */
	nHidden = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Number of hidden columns if TABLE is virtual */
	**(**int32)(__ccgo_up(bp)) = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      /* VDBE cursor that is the main data repository */
	**(**int32)(__ccgo_up(bp + 4)) = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  /* First index cursor */
	ipkColumn = -int32(1)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               /* Label for the end of the insertion loop */
	srcTab = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          /* Data comes from this temporary cursor if >=0 */
	addrInsTop = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      /* Jump to label "D" */
	addrCont = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        /* Index of database holding TABLE */
	useTempTable = uint8(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             /* Store SELECT results in intermediate table */
	appendFlag = uint8(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               /* True if IDLIST is in table order */
	pList = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  /* Register in which to store next column */
	/* Register allocations */
	regFromSelect = 0       /* Base register for data coming from SELECT */
	regAutoinc = 0          /* Register holding the AUTOINCREMENT counter */
	regRowCount = 0         /* register holding first column to insert */
	aRegIdx = uintptr(0)    /* One register allocated to each index */
	aTabColMap = uintptr(0) /* Mask of trigger times */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		goto insert_cleanup
	}
	(**(**TSelectDest)(__ccgo_up(bp + 8))).FiSDParm = 0 /* Suppress a harmless compiler warning */
	/* If the Select object is really just a simple VALUES() list with a
	 ** single row (the common case) then keep that one row of values
	 ** and discard the other (unused) parts of the pSelect object
	 */
	if pSelect != 0 && (*TSelect)(unsafe.Pointer(pSelect)).FselFlags&uint32(SF_Values) != uint32(0) && (*TSelect)(unsafe.Pointer(pSelect)).FpPrior == uintptr(0) {
		pList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList
		(*TSelect)(unsafe.Pointer(pSelect)).FpEList = uintptr(0)
		_sqlite3SelectDelete(tls, db, pSelect)
		pSelect = uintptr(0)
	}
	/* Locate the table into which we will be inserting new information.
	 */
	pTab = _sqlite3SrcListLookup(tls, pParse, pTabList)
	if pTab == uintptr(0) {
		goto insert_cleanup
	}
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_INSERT), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) != 0 {
		goto insert_cleanup
	}
	withoutRowid = libc.BoolUint8(!((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)))
	/* Figure out if we have any triggers and if the table being
	 ** inserted into is a view
	 */
	pTrigger = _sqlite3TriggersExist(tls, pParse, pTab, int32(TK_INSERT), uintptr(0), bp+36)
	isView = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW))
	/* If pTab is really a view, make sure it has been initialized.
	 ** ViewGetColumnNames() is a no-op if pTab is not a view.
	 */
	if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 {
		goto insert_cleanup
	}
	/* Cannot insert into a read-only table.
	 */
	if _sqlite3IsReadOnly(tls, pParse, pTab, pTrigger) != 0 {
		goto insert_cleanup
	}
	/* Allocate a VDBE
	 */
	v = _sqlite3GetVdbe(tls, pParse)
	if v == uintptr(0) {
		goto insert_cleanup
	}
	if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 {
		_sqlite3VdbeCountChanges(tls, v)
	}
	_sqlite3BeginWriteOperation(tls, pParse, libc.BoolInt32(pSelect != 0 || pTrigger != 0), iDb)
	/* If the statement is of the form
	 **
	 **       INSERT INTO <table1> SELECT * FROM <table2>;
	 **
	 ** Then special optimizations can be applied that make the transfer
	 ** very fast and which reduce fragmentation of indices.
	 **
	 ** This is the 2nd template.
	 */
	if pColumn == uintptr(0) && pSelect != uintptr(0) && pTrigger == uintptr(0) && _xferOptimization(tls, pParse, pTab, pSelect, onError, iDb) != 0 {
		goto insert_end
	}
	/* If this is an AUTOINCREMENT table, look up the sequence number in the
	 ** sqlite_sequence table and store it in memory cell regAutoinc.
	 */
	regAutoinc = _autoIncBegin(tls, pParse, iDb, pTab)
	/* Allocate a block registers to hold the rowid and the values
	 ** for all columns of the new row.
	 */
	v1 = (*TParse)(unsafe.Pointer(pParse)).FnMem + libc.Int32FromInt32(1)
	regIns = v1
	regRowid = v1
	**(**int32)(__ccgo_up(pParse + 48)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol) + int32(1)
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		regRowid = regRowid + 1
		(*TParse)(unsafe.Pointer(pParse)).FnMem = (*TParse)(unsafe.Pointer(pParse)).FnMem + 1
	}
	regData = regRowid + int32(1)
	/* If the INSERT statement included an IDLIST term, then make sure
	 ** all elements of the IDLIST really are columns of the table and
	 ** remember the column indices.
	 **
	 ** If the table has an INTEGER PRIMARY KEY column and that column
	 ** is named in the IDLIST, then record in the ipkColumn variable
	 ** the index into IDLIST of the primary key column.  ipkColumn is
	 ** the index of the primary key as it appears in IDLIST, not as
	 ** is appears in the original table.  (The index of the INTEGER
	 ** PRIMARY KEY in the original table is pTab->iPKey.)  After this
	 ** loop, if ipkColumn==(-1), that means that integer primary key
	 ** is unspecified, and hence the table is either WITHOUT ROWID or
	 ** it will automatically generated an integer primary key.
	 **
	 ** bIdListInOrder is true if the columns in IDLIST are in storage
	 ** order.  This enables an optimization that avoids shuffling the
	 ** columns into storage order.  False negatives are harmless,
	 ** but false positives will cause database corruption.
	 */
	bIdListInOrder = libc.BoolUint8((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(libc.Int32FromInt32(TF_OOOHidden)|libc.Int32FromInt32(TF_HasStored)) == uint32(0))
	if pColumn != 0 {
		aTabColMap = _sqlite3DbMallocZero(tls, db, uint64(uint32((*TTable)(unsafe.Pointer(pTab)).FnCol)*uint32(4)))
		if aTabColMap == uintptr(0) {
			goto insert_cleanup
		}
		i = 0
		for {
			if !(i < (*TIdList)(unsafe.Pointer(pColumn)).FnId) {
				break
			}
			j = _sqlite3ColumnIndex(tls, pTab, (*(*TIdList_item)(unsafe.Pointer(pColumn + 4 + uintptr(i)*4))).FzName)
			if j >= 0 {
				if **(**int32)(__ccgo_up(aTabColMap + uintptr(j)*4)) == 0 {
					**(**int32)(__ccgo_up(aTabColMap + uintptr(j)*4)) = i + int32(1)
				}
				if i != j {
					bIdListInOrder = uint8(0)
				}
				if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
					ipkColumn = i
				}
				if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&(libc.Int32FromInt32(COLFLAG_STORED)|libc.Int32FromInt32(COLFLAG_VIRTUAL)) != 0 {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18567, libc.VaList(bp+88, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FzCnName))
					goto insert_cleanup
				}
			} else {
				if _sqlite3IsRowid(tls, (*(*TIdList_item)(unsafe.Pointer(pColumn + 4 + uintptr(i)*4))).FzName) != 0 && !(withoutRowid != 0) {
					ipkColumn = i
					bIdListInOrder = uint8(0)
				} else {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18608, libc.VaList(bp+88, pTabList+8, (*(*TIdList_item)(unsafe.Pointer(pColumn + 4 + uintptr(i)*4))).FzName))
					libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 8, 0x100)
					goto insert_cleanup
				}
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	/* Figure out how many columns of data are supplied.  If the data
	 ** is coming from a SELECT statement, then generate a co-routine that
	 ** produces a single row of the SELECT on each invocation.  The
	 ** co-routine is the common header to the 3rd and 4th templates.
	 */
	if pSelect != 0 { /* Result code */
		if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc)).FnSrc == int32(1) && int32(*(*uint32)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8 + 12 + 4))&0x40>>6) != 0 && (*TSelect)(unsafe.Pointer(pSelect)).FpPrior == uintptr(0) {
			pItem = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8
			pSubq = *(*uintptr)(unsafe.Pointer(pItem + 44))
			(**(**TSelectDest)(__ccgo_up(bp + 8))).FiSDParm = (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn
			regFromSelect = (*TSubquery)(unsafe.Pointer(pSubq)).FregResult
			nColumn = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(pSubq)).FpSelect)).FpEList)).FnExpr
			_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+18640, libc.VaList(bp+88, pItem))
			if bIdListInOrder != 0 && nColumn == int32((*TTable)(unsafe.Pointer(pTab)).FnCol) {
				regData = regFromSelect
				regRowid = regData - int32(1)
				if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
					v1 = int32(1)
				} else {
					v1 = 0
				}
				regIns = regRowid - v1
			}
		} else {
			v5 = pParse + 48
			*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
			v1 = *(*int32)(unsafe.Pointer(v5)) /* Top of the co-routine */
			regYield = v1
			addrTop = _sqlite3VdbeCurrentAddr(tls, v) + int32(1)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regYield, 0, addrTop)
			_sqlite3SelectDestInit(tls, bp+8, int32(SRT_Coroutine), regYield)
			if bIdListInOrder != 0 {
				v1 = regData
			} else {
				v1 = 0
			}
			(**(**TSelectDest)(__ccgo_up(bp + 8))).FiSdst = v1
			(**(**TSelectDest)(__ccgo_up(bp + 8))).FnSdst = int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
			rc = _sqlite3Select(tls, pParse, pSelect, bp+8)
			regFromSelect = (**(**TSelectDest)(__ccgo_up(bp + 8))).FiSdst
			if rc != 0 || (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
				goto insert_cleanup
			}
			_sqlite3VdbeEndCoroutine(tls, v, regYield)
			_sqlite3VdbeJumpHere(tls, v, addrTop-int32(1)) /* label B: */
			nColumn = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpEList)).FnExpr
		}
		/* Set useTempTable to TRUE if the result of the SELECT statement
		 ** should be written into a temporary table (template 4).  Set to
		 ** FALSE if each output row of the SELECT can be written directly into
		 ** the destination table (template 3).
		 **
		 ** A temp table must be used if the table being updated is also one
		 ** of the tables being read by the SELECT statement.  Also use a
		 ** temp table in the case of row triggers.
		 */
		if pTrigger != 0 || _readsTable(tls, pParse, iDb, pTab) != 0 {
			useTempTable = uint8(1)
		}
		if useTempTable != 0 { /* Label "L" */
			v5 = pParse + 44
			v1 = *(*int32)(unsafe.Pointer(v5))
			*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
			srcTab = v1
			regRec = _sqlite3GetTempReg(tls, pParse)
			regTempRowid = _sqlite3GetTempReg(tls, pParse)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), srcTab, nColumn)
			addrL = _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (**(**TSelectDest)(__ccgo_up(bp + 8))).FiSDParm)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regFromSelect, nColumn, regRec)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), srcTab, regTempRowid)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), srcTab, regRec, regTempRowid)
			_sqlite3VdbeGoto(tls, v, addrL)
			_sqlite3VdbeJumpHere(tls, v, addrL)
			_sqlite3ReleaseTempReg(tls, pParse, regRec)
			_sqlite3ReleaseTempReg(tls, pParse, regTempRowid)
		}
	} else {
		libc.Xmemset(tls, bp+40, 0, uint32(36))
		(**(**TNameContext)(__ccgo_up(bp + 40))).FpParse = pParse
		srcTab = -int32(1)
		if pList != 0 {
			nColumn = (*TExprList)(unsafe.Pointer(pList)).FnExpr
			if _sqlite3ResolveExprListNames(tls, bp+40, pList) != 0 {
				goto insert_cleanup
			}
		} else {
			nColumn = 0
		}
	}
	/* If there is no IDLIST term but the table has an integer primary
	 ** key, the set the ipkColumn variable to the integer primary key
	 ** column index in the original table definition.
	 */
	if pColumn == uintptr(0) && nColumn > 0 {
		ipkColumn = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey)
		if ipkColumn >= 0 && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) {
			i = ipkColumn - int32(1)
			for {
				if !(i >= 0) {
					break
				}
				if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
					ipkColumn = ipkColumn - 1
				}
				goto _9
			_9:
				;
				i = i - 1
			}
		}
		/* Make sure the number of columns in the source data matches the number
		 ** of columns to be inserted into the table.
		 */
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(libc.Int32FromInt32(TF_HasGenerated)|libc.Int32FromInt32(TF_HasHidden)) != uint32(0) {
			i = 0
			for {
				if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
					break
				}
				if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_NOINSERT) != 0 {
					nHidden = nHidden + 1
				}
				goto _10
			_10:
				;
				i = i + 1
			}
		}
		if nColumn != int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-nHidden {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18648, libc.VaList(bp+88, pTabList+8, int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-nHidden, nColumn))
			goto insert_cleanup
		}
	}
	if pColumn != uintptr(0) && nColumn != (*TIdList)(unsafe.Pointer(pColumn)).FnId {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18700, libc.VaList(bp+88, nColumn, (*TIdList)(unsafe.Pointer(pColumn)).FnId))
		goto insert_cleanup
	}
	/* Initialize the count of rows to be inserted
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00001))<<libc.Int32FromInt32(32)) != uint64(0) && !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) && !((*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0) && !(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x8>>3)) != 0) {
		v5 = pParse + 48
		*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
		v1 = *(*int32)(unsafe.Pointer(v5))
		regRowCount = v1
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regRowCount)
	}
	/* If this is not a view, open the table and and all indices */
	if !(isView != 0) {
		nIdx = _sqlite3OpenTableAndIndices(tls, pParse, pTab, int32(OP_OpenWrite), uint8(0), -int32(1), uintptr(0), bp, bp+4)
		aRegIdx = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(nIdx+libc.Int32FromInt32(2))))
		if aRegIdx == uintptr(0) {
			goto insert_cleanup
		}
		i = 0
		pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
		for {
			if !(i < nIdx) {
				break
			}
			v5 = pParse + 48
			*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
			v1 = *(*int32)(unsafe.Pointer(v5))
			**(**int32)(__ccgo_up(aRegIdx + uintptr(i)*4)) = v1
			**(**int32)(__ccgo_up(pParse + 48)) += int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
			goto _13
		_13:
			;
			pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
			i = i + 1
		}
		v5 = pParse + 48
		*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
		v1 = *(*int32)(unsafe.Pointer(v5))
		**(**int32)(__ccgo_up(aRegIdx + uintptr(i)*4)) = v1 /* Register to store the table record */
	}
	if pUpsert != 0 {
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18725, libc.VaList(bp+88, (*TTable)(unsafe.Pointer(pTab)).FzName))
			goto insert_cleanup
		}
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18771, 0)
			goto insert_cleanup
		}
		if _sqlite3HasExplicitNulls(tls, pParse, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget) != 0 {
			goto insert_cleanup
		}
		(*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor = **(**int32)(__ccgo_up(bp))
		pNx = pUpsert
		for cond := true; cond; cond = pNx != uintptr(0) {
			(*TUpsert)(unsafe.Pointer(pNx)).FpUpsertSrc = pTabList
			(*TUpsert)(unsafe.Pointer(pNx)).FregData = regData
			(*TUpsert)(unsafe.Pointer(pNx)).FiDataCur = **(**int32)(__ccgo_up(bp))
			(*TUpsert)(unsafe.Pointer(pNx)).FiIdxCur = **(**int32)(__ccgo_up(bp + 4))
			if (*TUpsert)(unsafe.Pointer(pNx)).FpUpsertTarget != 0 {
				if _sqlite3UpsertAnalyzeTarget(tls, pParse, pTabList, pNx, pUpsert) != 0 {
					goto insert_cleanup
				}
			}
			pNx = (*TUpsert)(unsafe.Pointer(pNx)).FpNextUpsert
		}
	}
	/* This is the top of the main insertion loop */
	if useTempTable != 0 {
		/* This block codes the top of loop only.  The complete loop is the
		 ** following pseudocode (template 4):
		 **
		 **         rewind temp table, if empty goto D
		 **      C: loop over rows of intermediate table
		 **           transfer values form intermediate table into <table>
		 **         end loop
		 **      D: ...
		 */
		addrInsTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), srcTab)
		addrCont = _sqlite3VdbeCurrentAddr(tls, v)
	} else {
		if pSelect != 0 {
			/* This block codes the top of loop only.  The complete loop is the
			 ** following pseudocode (template 3):
			 **
			 **      C: yield X, at EOF goto D
			 **         insert the select result into <table> from R..R+n
			 **         goto C
			 **      D: ...
			 */
			v1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (**(**TSelectDest)(__ccgo_up(bp + 8))).FiSDParm)
			addrCont = v1
			addrInsTop = v1
			if ipkColumn >= 0 {
				/* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
				 ** SELECT, go ahead and copy the value into the rowid slot now, so that
				 ** the value does not get overwritten by a NULL at tag-20191021-002. */
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), regFromSelect+ipkColumn, regRowid)
			}
		}
	}
	/* Compute data for ordinary columns of the new entry.  Values
	 ** are written in storage order into registers starting with regData.
	 ** Only ordinary columns are computed in this loop. The rowid
	 ** (if there is one) is computed later and generated columns are
	 ** computed after the rowid since they might depend on the value
	 ** of the rowid.
	 */
	nHidden = 0
	iRegStore = regData
	i = 0
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
			break
		}
		if i == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
			/* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
			 ** using the rowid. So put a NULL in the IPK slot of the record to avoid
			 ** using excess space.  The file format definition requires this extra
			 ** NULL - we cannot optimize further by skipping the column completely */
			_sqlite3VdbeAddOp1(tls, v, int32(OP_SoftNull), iRegStore)
			goto _19
		}
		v20 = uint32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)
		colFlags = v20
		if v20&uint32(COLFLAG_NOINSERT) != uint32(0) {
			nHidden = nHidden + 1
			if colFlags&uint32(COLFLAG_VIRTUAL) != uint32(0) {
				/* Virtual columns do not participate in OP_MakeRecord.  So back up
				 ** iRegStore by one slot to compensate for the iRegStore++ in the
				 ** outer for() loop */
				iRegStore = iRegStore - 1
				goto _19
			} else {
				if colFlags&uint32(COLFLAG_STORED) != uint32(0) {
					/* Stored columns are computed later.  But if there are BEFORE
					 ** triggers, the slots used for stored columns will be OP_Copy-ed
					 ** to a second block of registers, so the register needs to be
					 ** initialized to NULL to avoid an uninitialized register read */
					if **(**int32)(__ccgo_up(bp + 36))&int32(TRIGGER_BEFORE) != 0 {
						_sqlite3VdbeAddOp1(tls, v, int32(OP_SoftNull), iRegStore)
					}
					goto _19
				} else {
					if pColumn == uintptr(0) {
						/* Hidden columns that are not explicitly named in the INSERT
						 ** get their default value */
						_sqlite3ExprCodeFactorable(tls, pParse, _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12), iRegStore)
						goto _19
					}
				}
			}
		}
		if pColumn != 0 {
			j = **(**int32)(__ccgo_up(aTabColMap + uintptr(i)*4))
			if j == 0 {
				/* A column not named in the insert column list gets its
				 ** default value */
				_sqlite3ExprCodeFactorable(tls, pParse, _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12), iRegStore)
				goto _19
			}
			k = j - int32(1)
		} else {
			if nColumn == 0 {
				/* This is INSERT INTO ... DEFAULT VALUES.  Load the default value. */
				_sqlite3ExprCodeFactorable(tls, pParse, _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12), iRegStore)
				goto _19
			} else {
				k = i - nHidden
			}
		}
		if useTempTable != 0 {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), srcTab, k, iRegStore)
		} else {
			if pSelect != 0 {
				if regFromSelect != regData {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), regFromSelect+k, iRegStore)
				}
			} else {
				pX = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(k)*20))).FpExpr
				y = _sqlite3ExprCodeTarget(tls, pParse, pX, iRegStore)
				if y != iRegStore {
					if (*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(libc.Int32FromInt32(EP_Subquery)) != uint32(0) {
						v1 = int32(OP_Copy)
					} else {
						v1 = int32(OP_SCopy)
					}
					_sqlite3VdbeAddOp2(tls, v, v1, y, iRegStore)
				}
			}
		}
		goto _19
	_19:
		;
		i = i + 1
		iRegStore = iRegStore + 1
	}
	/* Run the BEFORE and INSTEAD OF triggers, if there are any
	 */
	endOfLoop = _sqlite3VdbeMakeLabel(tls, pParse)
	if **(**int32)(__ccgo_up(bp + 36))&int32(TRIGGER_BEFORE) != 0 {
		regCols = _sqlite3GetTempRange(tls, pParse, int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(1))
		/* build the NEW.* reference row.  Note that if there is an INTEGER
		 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
		 ** translated into a unique ID for the row.  But on a BEFORE trigger,
		 ** we do not know what the unique ID will be (because the insert has
		 ** not happened yet) so we substitute a rowid of -1
		 */
		if ipkColumn < 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), -int32(1), regCols)
		} else {
			if useTempTable != 0 {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), srcTab, ipkColumn, regCols)
			} else {
				/* Otherwise useTempTable is true */
				_sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ipkColumn)*20))).FpExpr, regCols)
			}
			addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_NotNull), regCols)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), -int32(1), regCols)
			_sqlite3VdbeJumpHere(tls, v, addr1)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), regCols)
		}
		/* Copy the new data already generated. */
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regRowid+int32(1), regCols+int32(1), int32((*TTable)(unsafe.Pointer(pTab)).FnNVCol)-int32(1))
		/* Compute the new value for generated columns after all other
		 ** columns have already been computed.  This must be done after
		 ** computing the ROWID in case one of the generated columns
		 ** refers to the ROWID. */
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != 0 {
			_sqlite3ComputeGeneratedColumns(tls, pParse, regCols+int32(1), pTab)
		}
		/* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
		 ** do not attempt any conversions before assembling the record.
		 ** If this is a real table, attempt conversions as required by the
		 ** table column affinities.
		 */
		if !(isView != 0) {
			_sqlite3TableAffinity(tls, v, pTab, regCols+int32(1))
		}
		/* Fire BEFORE or INSTEAD OF triggers */
		_sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_INSERT), uintptr(0), int32(TRIGGER_BEFORE), pTab, regCols-int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-int32(1), onError, endOfLoop)
		_sqlite3ReleaseTempRange(tls, pParse, regCols, int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(1))
	}
	if !(isView != 0) {
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
			/* The row that the VUpdate opcode will delete: none */
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regIns)
		}
		if ipkColumn >= 0 {
			/* Compute the new rowid */
			if useTempTable != 0 {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), srcTab, ipkColumn, regRowid)
			} else {
				if pSelect != 0 {
					/* Rowid already initialized at tag-20191021-001 */
				} else {
					pIpk = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ipkColumn)*20))).FpExpr
					if int32((*TExpr)(unsafe.Pointer(pIpk)).Fop) == int32(TK_NULL) && !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
						_sqlite3VdbeAddOp3(tls, v, int32(OP_NewRowid), **(**int32)(__ccgo_up(bp)), regRowid, regAutoinc)
						appendFlag = uint8(1)
					} else {
						_sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ipkColumn)*20))).FpExpr, regRowid)
					}
				}
			}
			/* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
			 ** to generate a unique primary key value.
			 */
			if !(appendFlag != 0) {
				if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
					addr11 = _sqlite3VdbeAddOp1(tls, v, int32(OP_NotNull), regRowid)
					_sqlite3VdbeAddOp3(tls, v, int32(OP_NewRowid), **(**int32)(__ccgo_up(bp)), regRowid, regAutoinc)
					_sqlite3VdbeJumpHere(tls, v, addr11)
				} else {
					addr11 = _sqlite3VdbeCurrentAddr(tls, v)
					_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regRowid, addr11+int32(2))
				}
				_sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), regRowid)
			}
		} else {
			if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) || withoutRowid != 0 {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regRowid)
			} else {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_NewRowid), **(**int32)(__ccgo_up(bp)), regRowid, regAutoinc)
				appendFlag = uint8(1)
			}
		}
		_autoIncStep(tls, pParse, regAutoinc, regRowid)
		/* Compute the new value for generated columns after all other
		 ** columns have already been computed.  This must be done after
		 ** computing the ROWID in case one of the generated columns
		 ** is derived from the INTEGER PRIMARY KEY. */
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != 0 {
			_sqlite3ComputeGeneratedColumns(tls, pParse, regRowid+int32(1), pTab)
		}
		/* Generate code to check constraints and generate index keys and
		 ** do the insertion.
		 */
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
			pVTab = _sqlite3GetVTable(tls, db, pTab)
			_sqlite3VtabMakeWritable(tls, pParse, pTab)
			_sqlite3VdbeAddOp4(tls, v, int32(OP_VUpdate), int32(1), int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(2), regIns, pVTab, -int32(12))
			if onError == int32(OE_Default) {
				v1 = int32(OE_Abort)
			} else {
				v1 = onError
			}
			_sqlite3VdbeChangeP5(tls, v, uint16(v1))
			_sqlite3MayAbort(tls, pParse)
		} else {
			**(**int32)(__ccgo_up(bp + 76)) = 0 /* True to use OPFLAG_SEEKRESULT */
			_sqlite3GenerateConstraintChecks(tls, pParse, pTab, aRegIdx, **(**int32)(__ccgo_up(bp)), **(**int32)(__ccgo_up(bp + 4)), regIns, 0, libc.BoolUint8(ipkColumn >= 0), uint8(onError), endOfLoop, bp+76, uintptr(0), pUpsert)
			if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0 {
				_sqlite3FkCheck(tls, pParse, pTab, 0, regIns, uintptr(0), 0)
			}
			/* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
			 ** constraints or (b) there are no triggers and this table is not a
			 ** parent table in a foreign key constraint. It is safe to set the
			 ** flag in the second case as if any REPLACE constraint is hit, an
			 ** OP_Delete or OP_IdxDelete instruction will be executed on each
			 ** cursor that is disturbed. And these instructions both clear the
			 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
			 ** functionality.  */
			bUseSeek = libc.BoolInt32(**(**int32)(__ccgo_up(bp + 76)) == 0 || !(_sqlite3VdbeHasSubProgram(tls, v) != 0))
			_sqlite3CompleteInsertion(tls, pParse, pTab, **(**int32)(__ccgo_up(bp)), **(**int32)(__ccgo_up(bp + 4)), regIns, aRegIdx, 0, int32(appendFlag), bUseSeek)
		}
	}
	/* Update the count of rows that are inserted
	 */
	if regRowCount != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), regRowCount, int32(1))
	}
	if pTrigger != 0 {
		/* Code AFTER triggers */
		_sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_INSERT), uintptr(0), int32(TRIGGER_AFTER), pTab, regData-int32(2)-int32((*TTable)(unsafe.Pointer(pTab)).FnCol), onError, endOfLoop)
	}
	/* The bottom of the main insertion loop, if the data source
	 ** is a SELECT statement.
	 */
	_sqlite3VdbeResolveLabel(tls, v, endOfLoop)
	if useTempTable != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), srcTab, addrCont)
		_sqlite3VdbeJumpHere(tls, v, addrInsTop)
		_sqlite3VdbeAddOp1(tls, v, int32(OP_Close), srcTab)
	} else {
		if pSelect != 0 {
			_sqlite3VdbeGoto(tls, v, addrCont)
			_sqlite3VdbeJumpHere(tls, v, addrInsTop)
		}
	}
	goto insert_end
insert_end:
	;
	/* Update the sqlite_sequence table by storing the content of the
	 ** maximum rowid counter values recorded while inserting into
	 ** autoincrement tables.
	 */
	if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab == uintptr(0) {
		_sqlite3AutoincrementEnd(tls, pParse)
	}
	/*
	 ** Return the number of rows inserted. If this routine is
	 ** generating code because of a call to sqlite3NestedParse(), do not
	 ** invoke the callback function.
	 */
	if regRowCount != 0 {
		_sqlite3CodeChangeCount(tls, v, regRowCount, __ccgo_ts+18792)
	}
	goto insert_cleanup
insert_cleanup:
	;
	_sqlite3SrcListDelete(tls, db, pTabList)
	_sqlite3ExprListDelete(tls, db, pList)
	_sqlite3UpsertDelete(tls, db, pUpsert)
	_sqlite3SelectDelete(tls, db, pSelect)
	if pColumn != 0 {
		_sqlite3IdListDelete(tls, db, pColumn)
		_sqlite3DbFree(tls, db, aTabColMap)
	}
	if aRegIdx != 0 {
		_sqlite3DbNNFreeNN(tls, db, aRegIdx)
	}
}

/* Make sure "isView" and other macros defined above are undefined. Otherwise
** they may interfere with compilation of other functions in this file
** (or in another file, if this file becomes part of the amalgamation).  */

/*
** Meanings of bits in of pWalker->eCode for
** sqlite3ExprReferencesUpdatedColumn()
 */

// C documentation
//
//	/*
//	** Render an signed 64-bit integer as text.  Store the result in zOut[] and
//	** return the length of the string that was stored, in bytes.  The value
//	** returned does not include the zero terminator at the end of the output
//	** string.
//	**
//	** The caller must ensure that zOut[] is at least 21 bytes in size.
//	*/
func _sqlite3Int64ToText(tls *libc.TLS, v Ti64, zOut uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, kk, v2 int32
	var x Tu64
	var v1 uint64
	var _ /* u at bp+0 */ struct {
		FforceAlignment [0]Tu16
		Fa              [21]int8
		F__ccgo_pad2    [1]byte
	}
	_, _, _, _, _ = i, kk, x, v1, v2
	if v > 0 {
		x = uint64(v)
	} else {
		if v == 0 {
			**(**int8)(__ccgo_up(zOut)) = int8('0')
			**(**int8)(__ccgo_up(zOut + 1)) = 0
			return int32(1)
		} else {
			if v == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)) {
				v1 = libc.Uint64FromInt32(1) << libc.Int32FromInt32(63)
			} else {
				v1 = uint64(-v)
			}
			x = v1
		}
	}
	i = int32(libc.Uint32FromInt64(21) - libc.Uint32FromInt32(1))
	**(**int8)(__ccgo_up(bp + uintptr(i))) = 0
	for x >= uint64(10) {
		kk = int32(x % uint64(100) * uint64(2))
		**(**Tu16)(__ccgo_up(bp + uintptr(i-int32(2)))) = **(**Tu16)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3DigitPairs)) + uintptr(kk)))
		i = i - int32(2)
		x = x / uint64(100)
	}
	if x != 0 {
		i = i - 1
		v2 = i
		**(**int8)(__ccgo_up(bp + uintptr(v2))) = int8(x + uint64('0'))
	}
	if v < 0 {
		i = i - 1
		v2 = i
		**(**int8)(__ccgo_up(bp + uintptr(v2))) = int8('-')
	}
	libc.Xmemcpy(tls, zOut, bp+uintptr(i), uint32(21)-uint32(i))
	return int32(libc.Uint32FromInt64(21) - libc.Uint32FromInt32(1) - uint32(i))
}

// C documentation
//
//	/*
//	** pExpr points to an expression which implements a function.  If
//	** it is appropriate to apply the LIKE optimization to that function
//	** then set aWc[0] through aWc[2] to the wildcard characters and the
//	** escape character and then return TRUE.  If the function is not a
//	** LIKE-style function then return FALSE.
//	**
//	** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
//	** operator if c is a string literal that is exactly one byte in length.
//	** That one byte is stored in aWc[3].  aWc[3] is set to zero if there is
//	** no ESCAPE clause.
//	**
//	** *pIsNocase is set to true if uppercase and lowercase are equivalent for
//	** the function (default for LIKE).  If the function makes the distinction
//	** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
//	** false.
//	*/
func _sqlite3IsLikeFunction(tls *libc.TLS, db uintptr, pExpr uintptr, pIsNocase uintptr, aWc uintptr) (r int32) {
	var nExpr int32
	var pDef, pEscape, zEscape uintptr
	_, _, _, _ = nExpr, pDef, pEscape, zEscape
	if !(*(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0) {
		return 0
	}
	nExpr = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr
	pDef = _sqlite3FindFunction(tls, db, *(*uintptr)(unsafe.Pointer(pExpr + 8)), nExpr, uint8(SQLITE_UTF8), uint8(0))
	if pDef == uintptr(0) || (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_LIKE) == uint32(0) {
		return 0
	}
	/* The memcpy() statement assumes that the wildcard characters are
	 ** the first three statements in the compareInfo structure.  The
	 ** asserts() that follow verify that assumption
	 */
	libc.Xmemcpy(tls, aWc, (*TFuncDef)(unsafe.Pointer(pDef)).FpUserData, uint32(3))
	if nExpr < int32(3) {
		**(**int8)(__ccgo_up(aWc + 3)) = 0
	} else {
		pEscape = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)) + 8 + 2*20))).FpExpr
		if int32((*TExpr)(unsafe.Pointer(pEscape)).Fop) != int32(TK_STRING) {
			return 0
		}
		zEscape = *(*uintptr)(unsafe.Pointer(pEscape + 8))
		if int32(**(**int8)(__ccgo_up(zEscape))) == 0 || int32(**(**int8)(__ccgo_up(zEscape + 1))) != 0 {
			return 0
		}
		if int32(**(**int8)(__ccgo_up(zEscape))) == int32(**(**int8)(__ccgo_up(aWc))) {
			return 0
		}
		if int32(**(**int8)(__ccgo_up(zEscape))) == int32(**(**int8)(__ccgo_up(aWc + 1))) {
			return 0
		}
		**(**int8)(__ccgo_up(aWc + 3)) = **(**int8)(__ccgo_up(zEscape))
	}
	**(**int32)(__ccgo_up(pIsNocase)) = libc.BoolInt32((*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_CASE) == uint32(0))
	return int32(1)
}

/* Mathematical Constants */

// C documentation
//
//	/*
//	** Return true if pTab is a virtual table and zName is a shadow table name
//	** for that virtual table.
//	*/
func _sqlite3IsShadowTableOf(tls *libc.TLS, db uintptr, pTab uintptr, zName uintptr) (r int32) {
	var nName int32
	var pMod uintptr
	_, _ = nName, pMod /* Module for the virtual table */
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
		return 0
	}
	nName = _sqlite3Strlen30(tls, (*TTable)(unsafe.Pointer(pTab)).FzName)
	if Xsqlite3_strnicmp(tls, zName, (*TTable)(unsafe.Pointer(pTab)).FzName, nName) != 0 {
		return 0
	}
	if int32(**(**int8)(__ccgo_up(zName + uintptr(nName)))) != int32('_') {
		return 0
	}
	pMod = _sqlite3HashFind(tls, db+404, **(**uintptr)(__ccgo_up((*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).FazArg)))
	if pMod == uintptr(0) {
		return 0
	}
	if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FiVersion < int32(3) {
		return 0
	}
	if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxShadowName == uintptr(0) {
		return 0
	}
	return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxShadowName})))(tls, zName+uintptr(nName)+uintptr(1))
}

// C documentation
//
//	/*
//	** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
//	** type of join.  Return an integer constant that expresses that type
//	** in terms of the following bit values:
//	**
//	**     JT_INNER
//	**     JT_CROSS
//	**     JT_OUTER
//	**     JT_NATURAL
//	**     JT_LEFT
//	**     JT_RIGHT
//	**
//	** A full outer join is the combination of JT_LEFT and JT_RIGHT.
//	**
//	** If an illegal or unsupported join type is seen, then still return
//	** a join type, but put an error in the pParse structure.
//	**
//	** These are the valid join types:
//	**
//	**
//	**      pA       pB       pC               Return Value
//	**     -------  -----    -----             ------------
//	**     CROSS      -        -                 JT_CROSS
//	**     INNER      -        -                 JT_INNER
//	**     LEFT       -        -                 JT_LEFT|JT_OUTER
//	**     LEFT     OUTER      -                 JT_LEFT|JT_OUTER
//	**     RIGHT      -        -                 JT_RIGHT|JT_OUTER
//	**     RIGHT    OUTER      -                 JT_RIGHT|JT_OUTER
//	**     FULL       -        -                 JT_LEFT|JT_RIGHT|JT_OUTER
//	**     FULL     OUTER      -                 JT_LEFT|JT_RIGHT|JT_OUTER
//	**     NATURAL  INNER      -                 JT_NATURAL|JT_INNER
//	**     NATURAL  LEFT       -                 JT_NATURAL|JT_LEFT|JT_OUTER
//	**     NATURAL  LEFT     OUTER               JT_NATURAL|JT_LEFT|JT_OUTER
//	**     NATURAL  RIGHT      -                 JT_NATURAL|JT_RIGHT|JT_OUTER
//	**     NATURAL  RIGHT    OUTER               JT_NATURAL|JT_RIGHT|JT_OUTER
//	**     NATURAL  FULL       -                 JT_NATURAL|JT_LEFT|JT_RIGHT
//	**     NATURAL  FULL     OUTER               JT_NATRUAL|JT_LEFT|JT_RIGHT
//	**
//	** To preserve historical compatibly, SQLite also accepts a variety
//	** of other non-standard and in many cases nonsensical join types.
//	** This routine makes as much sense at it can from the nonsense join
//	** type and returns a result.  Examples of accepted nonsense join types
//	** include but are not limited to:
//	**
//	**          INNER CROSS JOIN        ->   same as JOIN
//	**          NATURAL CROSS JOIN      ->   same as NATURAL JOIN
//	**          OUTER LEFT JOIN         ->   same as LEFT JOIN
//	**          LEFT NATURAL JOIN       ->   same as NATURAL LEFT JOIN
//	**          LEFT RIGHT JOIN         ->   same as FULL JOIN
//	**          RIGHT OUTER FULL JOIN   ->   same as FULL JOIN
//	**          CROSS CROSS CROSS JOIN  ->   same as JOIN
//	**
//	** The only restrictions on the join type name are:
//	**
//	**    *   "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT",
//	**        or "FULL".
//	**
//	**    *   "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT,
//	**        or "FULL".
//	**
//	**    *   If "OUTER" is present then there must also be one of
//	**        "LEFT", "RIGHT", or "FULL"
//	*/
func _sqlite3JoinType(tls *libc.TLS, pParse uintptr, pA uintptr, pB uintptr, pC uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var apAll [3]uintptr
	var i, j, jointype int32
	var p, zSp1, zSp2 uintptr
	_, _, _, _, _, _, _ = apAll, i, j, jointype, p, zSp1, zSp2
	jointype = 0
	apAll[0] = pA
	apAll[int32(1)] = pB
	apAll[int32(2)] = pC
	i = 0
	for {
		if !(i < int32(3) && apAll[i] != 0) {
			break
		}
		p = apAll[i]
		j = 0
		for {
			if !(j < int32(libc.Uint32FromInt64(21)/libc.Uint32FromInt64(3))) {
				break
			}
			if (*TToken)(unsafe.Pointer(p)).Fn == uint32(_aKeyword[j].FnChar) && Xsqlite3_strnicmp(tls, (*TToken)(unsafe.Pointer(p)).Fz, uintptr(unsafe.Pointer(&_zKeyText))+uintptr(_aKeyword[j].Fi), int32((*TToken)(unsafe.Pointer(p)).Fn)) == 0 {
				jointype = jointype | int32(_aKeyword[j].Fcode)
				break
			}
			goto _2
		_2:
			;
			j = j + 1
		}
		if j >= int32(libc.Uint32FromInt64(21)/libc.Uint32FromInt64(3)) {
			jointype = jointype | int32(JT_ERROR)
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if jointype&(libc.Int32FromInt32(JT_INNER)|libc.Int32FromInt32(JT_OUTER)) == libc.Int32FromInt32(JT_INNER)|libc.Int32FromInt32(JT_OUTER) || jointype&int32(JT_ERROR) != 0 || jointype&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_RIGHT)) == int32(JT_OUTER) {
		zSp1 = __ccgo_ts + 12758
		zSp2 = __ccgo_ts + 12758
		if pB == uintptr(0) {
			zSp1 = zSp1 + 1
		}
		if pC == uintptr(0) {
			zSp2 = zSp2 + 1
		}
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21518, libc.VaList(bp+8, pA, zSp1, pB, zSp2, pC))
		jointype = int32(JT_INNER)
	}
	return jointype
}

// C documentation
//
//	/*
//	** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
//	** defined in pager.h. This function returns the associated lowercase
//	** journal-mode name.
//	*/
func _sqlite3JournalModename(tls *libc.TLS, eMode int32) (r uintptr) {
	if eMode == int32(libc.Uint32FromInt64(24)/libc.Uint32FromInt64(4)) {
		return uintptr(0)
	}
	return _azModeName[eMode]
}

// C documentation
//
//	/*
//	** Open a journal file.
//	**
//	** The behaviour of the journal file depends on the value of parameter
//	** nSpill. If nSpill is 0, then the journal file is always create and
//	** accessed using the underlying VFS. If nSpill is less than zero, then
//	** all content is always stored in main-memory. Finally, if nSpill is a
//	** positive value, then the journal file is initially created in-memory
//	** but may be flushed to disk later on. In this case the journal file is
//	** flushed to disk either when it grows larger than nSpill bytes in size,
//	** or when sqlite3JournalCreate() is called.
//	*/
func _sqlite3JournalOpen(tls *libc.TLS, pVfs uintptr, zName uintptr, pJfd uintptr, flags int32, nSpill int32) (r int32) {
	var p uintptr
	_ = p
	p = pJfd
	/* Zero the file-handle object. If nSpill was passed zero, initialize
	 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
	 ** case none of the code in this module is executed as a result of calls
	 ** made on the journal file-handle.  */
	libc.Xmemset(tls, p, 0, uint32(64))
	if nSpill == 0 {
		return _sqlite3OsOpen(tls, pVfs, zName, pJfd, flags, uintptr(0))
	}
	if nSpill > 0 {
		(*TMemJournal)(unsafe.Pointer(p)).FnChunkSize = nSpill
	} else {
		(*TMemJournal)(unsafe.Pointer(p)).FnChunkSize = int32(uint32(libc.Int32FromInt32(8)+libc.Int32FromInt32(MEMJOURNAL_DFLT_FILECHUNKSIZE)) - libc.Uint32FromInt64(12))
	}
	(*Tsqlite3_file)(unsafe.Pointer(pJfd)).FpMethods = uintptr(unsafe.Pointer(&_MemJournalMethods))
	(*TMemJournal)(unsafe.Pointer(p)).FnSpill = nSpill
	(*TMemJournal)(unsafe.Pointer(p)).Fflags = flags
	(*TMemJournal)(unsafe.Pointer(p)).FzJournal = zName
	(*TMemJournal)(unsafe.Pointer(p)).FpVfs = pVfs
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Allocate a KeyInfo object sufficient for an index of N key columns and
//	** X extra columns.
//	*/
func _sqlite3KeyInfoAlloc(tls *libc.TLS, db uintptr, N int32, X int32) (r uintptr) {
	var nExtra int32
	var p uintptr
	_, _ = nExtra, p
	nExtra = int32(uint32(N+X) * (libc.Uint32FromInt64(4) + libc.Uint32FromInt32(1)))
	if N+X > int32(0xffff) {
		return _sqlite3OomFault(tls, db)
	}
	p = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(libc.UintptrFromInt32(0)+20)+uint32(libc.Int32FromInt32(0))*libc.Uint32FromInt64(4)+uint32(nExtra)))
	if p != 0 {
		(*TKeyInfo)(unsafe.Pointer(p)).FaSortFlags = p + 20 + uintptr(N+X)*4
		(*TKeyInfo)(unsafe.Pointer(p)).FnKeyField = uint16(N)
		(*TKeyInfo)(unsafe.Pointer(p)).FnAllField = uint16(N + X)
		(*TKeyInfo)(unsafe.Pointer(p)).Fenc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc
		(*TKeyInfo)(unsafe.Pointer(p)).Fdb = db
		(*TKeyInfo)(unsafe.Pointer(p)).FnRef = uint32(1)
		libc.Xmemset(tls, p+20, 0, uint32(nExtra))
	} else {
		return _sqlite3OomFault(tls, db)
	}
	return p
}

// C documentation
//
//	/*
//	** Return a KeyInfo structure that is appropriate for the given Index.
//	**
//	** The caller should invoke sqlite3KeyInfoUnref() on the returned object
//	** when it has finished using it.
//	*/
func _sqlite3KeyInfoOfIndex(tls *libc.TLS, pParse uintptr, pIdx uintptr) (r uintptr) {
	var i, nCol, nKey int32
	var pKey, zColl, v2 uintptr
	_, _, _, _, _, _ = i, nCol, nKey, pKey, zColl, v2
	nCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
	nKey = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return uintptr(0)
	}
	if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 {
		pKey = _sqlite3KeyInfoAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, nKey, nCol-nKey)
	} else {
		pKey = _sqlite3KeyInfoAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, nCol, 0)
	}
	if pKey != 0 {
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			zColl = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i)*4))
			if zColl == uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) {
				v2 = uintptr(0)
			} else {
				v2 = _sqlite3LocateCollSeq(tls, pParse, zColl)
			}
			*(*uintptr)(unsafe.Pointer(pKey + 20 + uintptr(i)*4)) = v2
			**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pKey)).FaSortFlags + uintptr(i))) = **(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(i)))
			goto _1
		_1:
			;
			i = i + 1
		}
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x100>>8)) == 0 && _sqlite3HashFind(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpSchema+24, (*TIndex)(unsafe.Pointer(pIdx)).FzName) != 0 {
				/* Deactivate the index because it contains an unknown collating
				 ** sequence.  The only way to reactive the index is to reload the
				 ** schema.  Adding the missing collating sequence later does not
				 ** reactive the index.  The application had the chance to register
				 ** the missing index using the collation-needed callback.  For
				 ** simplicity, SQLite will not give the application a second chance.
				 **
				 ** Except, do not do this if the index is not in the schema hash
				 ** table. In this case the index is currently being constructed
				 ** by a CREATE INDEX statement, and retrying will not help.  */
				libc.SetBitFieldPtr16Uint32(pIdx+56, libc.Uint32FromInt32(1), 8, 0x100)
				(*TParse)(unsafe.Pointer(pParse)).Frc = libc.Int32FromInt32(SQLITE_ERROR) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
			}
			_sqlite3KeyInfoUnref(tls, pKey)
			pKey = uintptr(0)
		}
	}
	return pKey
}

// C documentation
//
//	/*
//	** Close the mutex on database connection db.
//	**
//	** Furthermore, if database connection db is a zombie (meaning that there
//	** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
//	** every sqlite3_stmt has now been finalized and every sqlite3_backup has
//	** finished, then free all resources.
//	*/
func _sqlite3LeaveMutexAndCloseZombie(tls *libc.TLS, db uintptr) {
	var i, p, pColl, pDb, pMod, pNext uintptr
	var j int32
	_, _, _, _, _, _, _ = i, j, p, pColl, pDb, pMod, pNext
	/* If there are outstanding sqlite3_stmt or sqlite3_backup objects
	 ** or if the connection has not yet been closed by sqlite3_close_v2(),
	 ** then just leave the mutex and return.
	 */
	if int32((*Tsqlite3)(unsafe.Pointer(db)).FeOpenState) != int32(SQLITE_STATE_ZOMBIE) || _connectionIsBusy(tls, db) != 0 {
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
		return
	}
	/* If we reach this point, it means that the database connection has
	 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
	 ** passed to sqlite3_close (meaning that it is a zombie).  Therefore,
	 ** go ahead and free all resources.
	 */
	/* If a transaction is open, roll it back. This also ensures that if
	 ** any database schemas have been modified by an uncommitted transaction
	 ** they are reset. And that the required b-tree mutex is held to make
	 ** the pager rollback and schema reset an atomic operation. */
	_sqlite3RollbackAll(tls, db, SQLITE_OK)
	/* Free any outstanding Savepoint structures. */
	_sqlite3CloseSavepoints(tls, db)
	/* Close all database connections */
	j = 0
	for {
		if !(j < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
			break
		}
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(j)*16
		if (*TDb)(unsafe.Pointer(pDb)).FpBt != 0 {
			_sqlite3BtreeClose(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt)
			(*TDb)(unsafe.Pointer(pDb)).FpBt = uintptr(0)
			if j != int32(1) {
				(*TDb)(unsafe.Pointer(pDb)).FpSchema = uintptr(0)
			}
		}
		goto _1
	_1:
		;
		j = j + 1
	}
	/* Clear the TEMP schema separately and last */
	if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema != 0 {
		_sqlite3SchemaClear(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema)
	}
	_sqlite3VtabUnlockList(tls, db)
	/* Free up the array of auxiliary databases */
	_sqlite3CollapseDatabaseArray(tls, db)
	/* Tell the code in notify.c that the connection no longer holds any
	 ** locks and does not require any further unlock-notify callbacks.
	 */
	_sqlite3ConnectionClosed(tls, db)
	i = (*THash)(unsafe.Pointer(db + 432)).Ffirst
	for {
		if !(i != 0) {
			break
		}
		p = (*THashElem)(unsafe.Pointer(i)).Fdata
		for cond := true; cond; cond = p != 0 {
			_functionDestroy(tls, db, p)
			pNext = (*TFuncDef)(unsafe.Pointer(p)).FpNext
			_sqlite3DbFree(tls, db, p)
			p = pNext
		}
		goto _2
	_2:
		;
		i = (*THashElem)(unsafe.Pointer(i)).Fnext
	}
	_sqlite3HashClear(tls, db+432)
	i = (*THash)(unsafe.Pointer(db + 448)).Ffirst
	for {
		if !(i != 0) {
			break
		}
		pColl = (*THashElem)(unsafe.Pointer(i)).Fdata
		/* Invoke any destructors registered for collation sequence user data. */
		j = 0
		for {
			if !(j < int32(3)) {
				break
			}
			if (**(**TCollSeq)(__ccgo_up(pColl + uintptr(j)*20))).FxDel != 0 {
				(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(**(**TCollSeq)(__ccgo_up(pColl + uintptr(j)*20))).FxDel})))(tls, (**(**TCollSeq)(__ccgo_up(pColl + uintptr(j)*20))).FpUser)
			}
			goto _4
		_4:
			;
			j = j + 1
		}
		_sqlite3DbFree(tls, db, pColl)
		goto _3
	_3:
		;
		i = (*THashElem)(unsafe.Pointer(i)).Fnext
	}
	_sqlite3HashClear(tls, db+448)
	i = (*THash)(unsafe.Pointer(db + 404)).Ffirst
	for {
		if !(i != 0) {
			break
		}
		pMod = (*THashElem)(unsafe.Pointer(i)).Fdata
		_sqlite3VtabEponymousTableClear(tls, db, pMod)
		_sqlite3VtabModuleUnref(tls, db, pMod)
		goto _5
	_5:
		;
		i = (*THashElem)(unsafe.Pointer(i)).Fnext
	}
	_sqlite3HashClear(tls, db+404)
	_sqlite3Error(tls, db, SQLITE_OK) /* Deallocates any cached error strings. */
	_sqlite3ValueFree(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpErr)
	_sqlite3CloseExtensions(tls, db)
	(*Tsqlite3)(unsafe.Pointer(db)).FeOpenState = uint8(SQLITE_STATE_ERROR)
	/* The temp-database schema is allocated differently from the other schema
	 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
	 ** So it needs to be freed here. Todo: Why not roll the temp schema into
	 ** the same sqliteMalloc() as the one that allocates the database
	 ** structure?
	 */
	_sqlite3DbFree(tls, db, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema)
	if (*Tsqlite3)(unsafe.Pointer(db)).FxAutovacDestr != 0 {
		(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxAutovacDestr})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpAutovacPagesArg)
	}
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	(*Tsqlite3)(unsafe.Pointer(db)).FeOpenState = uint8(SQLITE_STATE_CLOSED)
	Xsqlite3_mutex_free(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbMalloced != 0 {
		Xsqlite3_free(tls, (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart)
	}
	Xsqlite3_free(tls, db)
}

// C documentation
//
//	/*
//	** Attempt to load an SQLite extension library contained in the file
//	** zFile.  The entry point is zProc.  zProc may be 0 in which case a
//	** default entry point name (sqlite3_extension_init) is used.  Use
//	** of the default name is recommended.
//	**
//	** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
//	**
//	** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
//	** error message text.  The calling function should free this memory
//	** by calling sqlite3DbFree(db, ).
//	*/
func _sqlite3LoadExtension(tls *libc.TLS, db uintptr, zFile uintptr, zProc uintptr, pzErrMsg uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aHandle, handle, pVfs, zAltEntry, zAltFile, zEntry, v1 uintptr
	var c, cnt, iEntry, iFile, ii, ncFile, rc, v3, v8 int32
	var nMsg Tu64
	var xInit Tsqlite3_loadext_entry
	var v4 bool
	var _ /* zErrmsg at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aHandle, c, cnt, handle, iEntry, iFile, ii, nMsg, ncFile, pVfs, rc, xInit, zAltEntry, zAltFile, zEntry, v1, v3, v4, v8
	pVfs = (*Tsqlite3)(unsafe.Pointer(db)).FpVfs
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	zAltEntry = uintptr(0)
	nMsg = uint64(libc.Xstrlen(tls, zFile))
	if pzErrMsg != 0 {
		**(**uintptr)(__ccgo_up(pzErrMsg)) = uintptr(0)
	}
	/* Ticket #1863.  To avoid a creating security problems for older
	 ** applications that relink against newer versions of SQLite, the
	 ** ability to run load_extension is turned off by default.  One
	 ** must call either sqlite3_enable_load_extension(db) or
	 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
	 ** to turn on extension loading.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_LoadExtension) == uint64(0) {
		if pzErrMsg != 0 {
			**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+14888, 0)
		}
		return int32(SQLITE_ERROR)
	}
	if zProc != 0 {
		v1 = zProc
	} else {
		v1 = __ccgo_ts + 18810
	}
	zEntry = v1
	/* tag-20210611-1.  Some dlopen() implementations will segfault if given
	 ** an oversize filename.  Most filesystems have a pathname limit of 4K,
	 ** so limit the extension filename length to about twice that.
	 ** https://sqlite.org/forum/forumpost/08a0d6d9bf
	 **
	 ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
	 ** See https://sqlite.org/forum/forumpost/24083b579d.
	 */
	if nMsg > uint64(FILENAME_MAX) {
		goto extension_not_found
	}
	/* Do not allow sqlite3_load_extension() to link to a copy of the
	 ** running application, by passing in an empty filename. */
	if nMsg == uint64(0) {
		goto extension_not_found
	}
	handle = _sqlite3OsDlOpen(tls, pVfs, zFile)
	ii = 0
	for {
		if !(ii < int32(libc.Uint32FromInt64(4)/libc.Uint32FromInt64(4)) && handle == uintptr(0)) {
			break
		}
		zAltFile = Xsqlite3_mprintf(tls, __ccgo_ts+14849, libc.VaList(bp+16, zFile, _azEndings[ii]))
		if zAltFile == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		if nMsg+uint64(libc.Xstrlen(tls, _azEndings[ii]))+uint64(1) <= uint64(FILENAME_MAX) {
			handle = _sqlite3OsDlOpen(tls, pVfs, zAltFile)
		}
		Xsqlite3_free(tls, zAltFile)
		goto _2
	_2:
		;
		ii = ii + 1
	}
	if handle == uintptr(0) {
		goto extension_not_found
	}
	xInit = _sqlite3OsDlSym(tls, pVfs, handle, zEntry)
	/* If no entry point was specified and the default legacy
	 ** entry point name "sqlite3_extension_init" was not found, then
	 ** construct an entry point name "sqlite3_X_init" where the X is
	 ** replaced by the lowercase value of every ASCII alphabetic
	 ** character in the filename after the last "/" up to the first ".",
	 ** and skipping the first three characters if they are "lib".
	 ** Examples:
	 **
	 **    /usr/local/lib/libExample5.4.3.so ==>  sqlite3_example_init
	 **    C:/lib/mathfuncs.dll              ==>  sqlite3_mathfuncs_init
	 **
	 ** If that still finds no entry point, repeat a second time but this
	 ** time include both alphabetic and numeric characters up to the first
	 ** ".".  Example:
	 **
	 **    /usr/local/lib/libExample5.4.3.so ==>  sqlite3_example5_init
	 */
	if xInit == uintptr(0) && zProc == uintptr(0) {
		ncFile = _sqlite3Strlen30(tls, zFile)
		cnt = 0
		zAltEntry = Xsqlite3_malloc64(tls, uint64(ncFile+int32(30)))
		if zAltEntry == uintptr(0) {
			_sqlite3OsDlClose(tls, pVfs, handle)
			return int32(SQLITE_NOMEM)
		}
		for {
			libc.Xmemcpy(tls, zAltEntry, __ccgo_ts+18833, uint32(8))
			iFile = ncFile - int32(1)
			for {
				if !(iFile >= 0 && !(int32(**(**int8)(__ccgo_up(zFile + uintptr(iFile)))) == int32('/') || int32(**(**int8)(__ccgo_up(zFile + uintptr(iFile)))) == int32('\\'))) {
					break
				}
				goto _6
			_6:
				;
				iFile = iFile - 1
			}
			iFile = iFile + 1
			if Xsqlite3_strnicmp(tls, zFile+uintptr(iFile), __ccgo_ts+18842, int32(3)) == 0 {
				iFile = iFile + int32(3)
			}
			iEntry = int32(8)
			for {
				v3 = int32(**(**int8)(__ccgo_up(zFile + uintptr(iFile))))
				c = v3
				if !(v3 != 0 && c != int32('.')) {
					break
				}
				if int32(_sqlite3CtypeMap[uint8(c)])&int32(0x02) != 0 || cnt != 0 && int32(_sqlite3CtypeMap[uint8(c)])&int32(0x04) != 0 {
					v8 = iEntry
					iEntry = iEntry + 1
					**(**int8)(__ccgo_up(zAltEntry + uintptr(v8))) = int8(_sqlite3UpperToLower[uint32(c)])
				}
				goto _7
			_7:
				;
				iFile = iFile + 1
			}
			libc.Xmemcpy(tls, zAltEntry+uintptr(iEntry), __ccgo_ts+18846, uint32(6))
			zEntry = zAltEntry
			xInit = _sqlite3OsDlSym(tls, pVfs, handle, zEntry)
			goto _5
		_5:
			;
			if v4 = xInit == uintptr(0); v4 {
				cnt = cnt + 1
				v3 = cnt
			}
			if !(v4 && v3 < int32(2)) {
				break
			}
		}
	}
	if xInit == uintptr(0) {
		if pzErrMsg != 0 {
			nMsg = nMsg + uint64(libc.Xstrlen(tls, zEntry)+uint32(300))
			v1 = Xsqlite3_malloc64(tls, nMsg)
			**(**uintptr)(__ccgo_up(bp)) = v1
			**(**uintptr)(__ccgo_up(pzErrMsg)) = v1
			if **(**uintptr)(__ccgo_up(bp)) != 0 {
				/* zErrmsg would be NULL if not so */
				Xsqlite3_snprintf(tls, int32(nMsg), **(**uintptr)(__ccgo_up(bp)), __ccgo_ts+18852, libc.VaList(bp+16, zEntry, zFile))
				_sqlite3OsDlError(tls, pVfs, int32(nMsg-uint64(1)), **(**uintptr)(__ccgo_up(bp)))
			}
		}
		_sqlite3OsDlClose(tls, pVfs, handle)
		Xsqlite3_free(tls, zAltEntry)
		return int32(SQLITE_ERROR)
	}
	Xsqlite3_free(tls, zAltEntry)
	rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xInit})))(tls, db, bp, uintptr(unsafe.Pointer(&_sqlite3Apis)))
	if rc != 0 {
		if rc == libc.Int32FromInt32(SQLITE_OK)|libc.Int32FromInt32(1)<<libc.Int32FromInt32(8) {
			return SQLITE_OK
		}
		if pzErrMsg != 0 {
			**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+18895, libc.VaList(bp+16, **(**uintptr)(__ccgo_up(bp))))
		}
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp)))
		_sqlite3OsDlClose(tls, pVfs, handle)
		return int32(SQLITE_ERROR)
	}
	/* Append the new shared library handle to the db->aExtension array. */
	aHandle = _sqlite3DbMallocZero(tls, db, uint64(uint32(4)*uint32((*Tsqlite3)(unsafe.Pointer(db)).FnExtension+libc.Int32FromInt32(1))))
	if aHandle == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FnExtension > 0 {
		libc.Xmemcpy(tls, aHandle, (*Tsqlite3)(unsafe.Pointer(db)).FaExtension, uint32(4)*uint32((*Tsqlite3)(unsafe.Pointer(db)).FnExtension))
	}
	_sqlite3DbFree(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaExtension)
	(*Tsqlite3)(unsafe.Pointer(db)).FaExtension = aHandle
	v1 = db + 212
	v3 = *(*int32)(unsafe.Pointer(v1))
	*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
	**(**uintptr)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaExtension + uintptr(v3)*4)) = handle
	return SQLITE_OK
	goto extension_not_found
extension_not_found:
	;
	if pzErrMsg != 0 {
		nMsg = nMsg + uint64(300)
		v1 = Xsqlite3_malloc64(tls, nMsg)
		**(**uintptr)(__ccgo_up(bp)) = v1
		**(**uintptr)(__ccgo_up(pzErrMsg)) = v1
		if **(**uintptr)(__ccgo_up(bp)) != 0 {
			/* zErrmsg would be NULL if not so */
			Xsqlite3_snprintf(tls, int32(nMsg), **(**uintptr)(__ccgo_up(bp)), __ccgo_ts+18927, libc.VaList(bp+16, int32(FILENAME_MAX), zFile))
			_sqlite3OsDlError(tls, pVfs, int32(nMsg-uint64(1)), **(**uintptr)(__ccgo_up(bp)))
		}
	}
	return int32(SQLITE_ERROR)
}

// C documentation
//
//	/*
//	** Locate the in-memory structure that describes a particular database
//	** table given the name of that table and (optionally) the name of the
//	** database containing the table.  Return NULL if not found.  Also leave an
//	** error message in pParse->zErrMsg.
//	**
//	** The difference between this routine and sqlite3FindTable() is that this
//	** routine leaves an error message in pParse->zErrMsg where
//	** sqlite3FindTable() does not.
//	*/
func _sqlite3LocateTable(tls *libc.TLS, pParse uintptr, flags Tu32, zName uintptr, zDbase uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, p, pMod, zMsg, v1 uintptr
	_, _, _, _, _ = db, p, pMod, zMsg, v1
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	/* Read the database schema. If an error occurs, leave an error message
	 ** and code in pParse and return NULL. */
	if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_SchemaKnownOk) == uint32(0) && SQLITE_OK != _sqlite3ReadSchema(tls, pParse) {
		return uintptr(0)
	}
	p = _sqlite3FindTable(tls, db, zName, zDbase)
	if p == uintptr(0) {
		/* If zName is the not the name of a table in the schema created using
		 ** CREATE, then check to see if it is the name of an virtual table that
		 ** can be an eponymous virtual table. */
		if int32((*TParse)(unsafe.Pointer(pParse)).FprepFlags)&int32(SQLITE_PREPARE_NO_VTAB) == 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 {
			pMod = _sqlite3HashFind(tls, db+404, zName)
			if pMod == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+14903, int32(7)) == 0 {
				pMod = _sqlite3PragmaVtabRegister(tls, db, zName)
			}
			if pMod == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+14911, int32(4)) == 0 {
				pMod = _sqlite3JsonVtabRegister(tls, db, zName)
			}
			if pMod != 0 && _sqlite3VtabEponymousTableInit(tls, pParse, pMod) != 0 {
				return (*TModule)(unsafe.Pointer(pMod)).FpEpoTab
			}
		}
		if flags&uint32(LOCATE_NOERR) != 0 {
			return uintptr(0)
		}
		libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 8, 0x100)
	} else {
		if int32((*TTable)(unsafe.Pointer(p)).FeTabType) == int32(TABTYP_VTAB) && int32((*TParse)(unsafe.Pointer(pParse)).FprepFlags)&int32(SQLITE_PREPARE_NO_VTAB) != 0 {
			p = uintptr(0)
		}
	}
	if p == uintptr(0) {
		if flags&uint32(LOCATE_VIEW) != 0 {
			v1 = __ccgo_ts + 14916
		} else {
			v1 = __ccgo_ts + 14929
		}
		zMsg = v1
		if zDbase != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8237, libc.VaList(bp+8, zMsg, zDbase, zName))
		} else {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8308, libc.VaList(bp+8, zMsg, zName))
		}
	} else {
	}
	return p
}

// C documentation
//
//	/*
//	** Convert a double into a LogEst
//	** In other words, compute an approximation for 10*log2(x).
//	*/
func _sqlite3LogEstFromDouble(tls *libc.TLS, _x float64) (r TLogEst) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*float64)(unsafe.Pointer(bp)) = _x
	var e TLogEst
	var _ /* a at bp+8 */ Tu64
	_ = e
	if **(**float64)(__ccgo_up(bp)) <= libc.Float64FromInt32(1) {
		return 0
	}
	if **(**float64)(__ccgo_up(bp)) <= libc.Float64FromInt32(2000000000) {
		return _sqlite3LogEst(tls, uint64(**(**float64)(__ccgo_up(bp))))
	}
	libc.Xmemcpy(tls, bp+8, bp, uint32(8))
	e = int16(**(**Tu64)(__ccgo_up(bp + 8))>>libc.Int32FromInt32(52) - uint64(1022))
	return int16(int32(e) * int32(10))
}

// C documentation
//
//	/*
//	** Table pTab is a virtual table.  If it the virtual table implementation
//	** exists and has an xShadowName method, then loop over all other ordinary
//	** tables within the same schema looking for shadow tables of pTab, and mark
//	** any shadow tables seen using the TF_Shadow flag.
//	*/
func _sqlite3MarkAllShadowTablesOf(tls *libc.TLS, db uintptr, pTab uintptr) {
	var k, pMod, pOther uintptr
	var nName int32
	_, _, _, _ = k, nName, pMod, pOther /* For looping through the symbol table */
	pMod = _sqlite3HashFind(tls, db+404, **(**uintptr)(__ccgo_up((*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).FazArg)))
	if pMod == uintptr(0) {
		return
	}
	if (*TModule)(unsafe.Pointer(pMod)).FpModule == uintptr(0) {
		return
	}
	if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FiVersion < int32(3) {
		return
	}
	if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxShadowName == uintptr(0) {
		return
	}
	nName = _sqlite3Strlen30(tls, (*TTable)(unsafe.Pointer(pTab)).FzName)
	k = (*THash)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FpSchema + 8)).Ffirst
	for {
		if !(k != 0) {
			break
		}
		pOther = (*THashElem)(unsafe.Pointer(k)).Fdata
		if !(int32((*TTable)(unsafe.Pointer(pOther)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
			goto _1
		}
		if (*TTable)(unsafe.Pointer(pOther)).FtabFlags&uint32(TF_Shadow) != 0 {
			goto _1
		}
		if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pOther)).FzName, (*TTable)(unsafe.Pointer(pTab)).FzName, nName) == 0 && int32(**(**int8)(__ccgo_up((*TTable)(unsafe.Pointer(pOther)).FzName + uintptr(nName)))) == int32('_') && (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxShadowName})))(tls, (*TTable)(unsafe.Pointer(pOther)).FzName+uintptr(nName)+uintptr(1)) != 0 {
			**(**Tu32)(__ccgo_up(pOther + 28)) |= uint32(TF_Shadow)
		}
		goto _1
	_1:
		;
		k = (*THashElem)(unsafe.Pointer(k)).Fnext
	}
}

// C documentation
//
//	/*
//	** Like malloc(), but remember the size of the allocation
//	** so that we can find it later using sqlite3MemSize().
//	**
//	** For this low-level routine, we are guaranteed that nByte>0 because
//	** cases of nByte<=0 will be intercepted and dealt with by higher level
//	** routines.
//	*/
func _sqlite3MemMalloc(tls *libc.TLS, nByte int32) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var p uintptr
	_ = p
	p = libc.Xmalloc(tls, uint32(nByte+int32(8)))
	if p != 0 {
		**(**Tsqlite3_int64)(__ccgo_up(p)) = int64(nByte)
		p += 8
	} else {
		Xsqlite3_log(tls, int32(SQLITE_NOMEM), __ccgo_ts+1607, libc.VaList(bp+8, nByte))
	}
	return p
}

// C documentation
//
//	/*
//	** Like realloc().  Resize an allocation previously obtained from
//	** sqlite3MemMalloc().
//	**
//	** For this low-level interface, we know that pPrior!=0.  Cases where
//	** pPrior==0 while have been intercepted by higher-level routine and
//	** redirected to xMalloc.  Similarly, we know that nByte>0 because
//	** cases where nByte<=0 will have been intercepted by higher-level
//	** routines and redirected to xFree.
//	*/
func _sqlite3MemRealloc(tls *libc.TLS, pPrior uintptr, nByte int32) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var p uintptr
	_ = p
	p = pPrior
	/* EV: R-46199-30249 */
	p -= 8
	p = libc.Xrealloc(tls, p, uint32(nByte+libc.Int32FromInt32(8)))
	if p != 0 {
		**(**Tsqlite3_int64)(__ccgo_up(p)) = int64(nByte)
		p += 8
	} else {
		Xsqlite3_log(tls, int32(SQLITE_NOMEM), __ccgo_ts+1645, libc.VaList(bp+8, _sqlite3MemSize(tls, pPrior), nByte))
	}
	return p
}

// C documentation
//
//	/*
//	** This routine is called when the extension is loaded.
//	** Register the new VFS.
//	*/
func _sqlite3MemdbInit(tls *libc.TLS) (r int32) {
	var pLower uintptr
	var sz uint32
	_, _ = pLower, sz
	pLower = Xsqlite3_vfs_find(tls, uintptr(0))
	if pLower == uintptr(0) {
		return int32(SQLITE_ERROR)
	}
	sz = uint32((*Tsqlite3_vfs)(unsafe.Pointer(pLower)).FszOsFile)
	_memdb_vfs.FpAppData = pLower
	/* The following conditional can only be true when compiled for
	 ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0.  We always leave
	 ** it in, to be safe, but it is marked as NO_TEST since there
	 ** is no way to reach it under most builds. */
	if sz < uint32(12) {
		sz = uint32(12)
	} /*NO_TEST*/
	_memdb_vfs.FszOsFile = int32(sz)
	return Xsqlite3_vfs_register(tls, uintptr(unsafe.Pointer(&_memdb_vfs)), 0)
}

// C documentation
//
//	/*
//	** This function is called by the parser for the second and subsequent
//	** rows of a multi-row VALUES clause. Argument pLeft is the part of
//	** the VALUES clause already parsed, argument pRow is the vector of values
//	** for the new row. The Select object returned represents the complete
//	** VALUES clause, including the new row.
//	**
//	** There are two ways in which this may be achieved - by incremental
//	** coding of a co-routine (the "co-routine" method) or by returning a
//	** Select object equivalent to the following (the "UNION ALL" method):
//	**
//	**        "pLeft UNION ALL SELECT pRow"
//	**
//	** If the VALUES clause contains a lot of rows, this compound Select
//	** object may consume a lot of memory.
//	**
//	** When the co-routine method is used, each row that will be returned
//	** by the VALUES clause is coded into part of a co-routine as it is
//	** passed to this function. The returned Select object is equivalent to:
//	**
//	**     SELECT * FROM (
//	**       Select object to read co-routine
//	**     )
//	**
//	** The co-routine method is used in most cases. Exceptions are:
//	**
//	**    a) If the current statement has a WITH clause. This is to avoid
//	**       statements like:
//	**
//	**            WITH cte AS ( VALUES('x'), ('y') ... )
//	**            SELECT * FROM cte AS a, cte AS b;
//	**
//	**       This will not work, as the co-routine uses a hard-coded register
//	**       for its OP_Yield instructions, and so it is not possible for two
//	**       cursors to iterate through it concurrently.
//	**
//	**    b) The schema is currently being parsed (i.e. the VALUES clause is part
//	**       of a schema item like a VIEW or TRIGGER). In this case there is no VM
//	**       being generated when parsing is taking place, and so generating
//	**       a co-routine is not possible.
//	**
//	**    c) There are non-constant expressions in the VALUES clause (e.g.
//	**       the VALUES clause is part of a correlated sub-query).
//	**
//	**    d) One or more of the values in the first row of the VALUES clause
//	**       has an affinity (i.e. is a CAST expression). This causes problems
//	**       because the complex rules SQLite uses (see function
//	**       sqlite3SubqueryColumnTypes() in select.c) to determine the effective
//	**       affinity of such a column for all rows require access to all values in
//	**       the column simultaneously.
//	*/
func _sqlite3MultiValues(tls *libc.TLS, pParse uintptr, pLeft uintptr, pRow uintptr) (r uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var f, v1 int32
	var p, pRet, pSelect, pSubq, pSubq1, v, v2 uintptr
	var _ /* dest at bp+0 */ TSelectDest
	_, _, _, _, _, _, _, _, _ = f, p, pRet, pSelect, pSubq, pSubq1, v, v1, v2
	if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x40>>6)) != 0 || (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Finit1.Fbusy != 0 || _exprListIsConstant(tls, pParse, pRow) == 0 || (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pLeft)).FpSrc)).FnSrc == 0 && _exprListIsNoAffinity(tls, pParse, (*TSelect)(unsafe.Pointer(pLeft)).FpEList) == 0 || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != PARSE_MODE_NORMAL {
		/* The co-routine method cannot be used. Fall back to UNION ALL. */
		pSelect = uintptr(0)
		f = libc.Int32FromInt32(SF_Values) | libc.Int32FromInt32(SF_MultiValue)
		if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pLeft)).FpSrc)).FnSrc != 0 {
			_sqlite3MultiValuesEnd(tls, pParse, pLeft)
			f = int32(SF_Values)
		} else {
			if (*TSelect)(unsafe.Pointer(pLeft)).FpPrior != 0 {
				/* In this case set the SF_MultiValue flag only if it was set on pLeft */
				f = int32(uint32(f) & (*TSelect)(unsafe.Pointer(pLeft)).FselFlags)
			}
		}
		pSelect = _sqlite3SelectNew(tls, pParse, pRow, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(f), uintptr(0))
		**(**Tu32)(__ccgo_up(pLeft + 4)) &= ^libc.Uint32FromInt32(SF_MultiValue)
		if pSelect != 0 {
			(*TSelect)(unsafe.Pointer(pSelect)).Fop = uint8(TK_ALL)
			(*TSelect)(unsafe.Pointer(pSelect)).FpPrior = pLeft
			pLeft = pSelect
		}
	} else {
		p = uintptr(0) /* SrcItem that reads from co-routine */
		if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pLeft)).FpSrc)).FnSrc == 0 {
			/* Co-routine has not yet been started and the special Select object
			 ** that accesses the co-routine has not yet been created. This block
			 ** does both those things. */
			v = _sqlite3GetVdbe(tls, pParse)
			pRet = _sqlite3SelectNew(tls, pParse, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0))
			/* Ensure the database schema has been read. This is to ensure we have
			 ** the correct text encoding.  */
			if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmDbFlags&uint32(DBFLAG_SchemaKnownOk) == uint32(0) {
				_sqlite3ReadSchema(tls, pParse)
			}
			if pRet != 0 {
				(*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pRet)).FpSrc)).FnSrc = int32(1)
				(*TSelect)(unsafe.Pointer(pRet)).FpPrior = (*TSelect)(unsafe.Pointer(pLeft)).FpPrior
				(*TSelect)(unsafe.Pointer(pRet)).Fop = (*TSelect)(unsafe.Pointer(pLeft)).Fop
				if (*TSelect)(unsafe.Pointer(pRet)).FpPrior != 0 {
					**(**Tu32)(__ccgo_up(pRet + 4)) |= uint32(SF_Values)
				}
				(*TSelect)(unsafe.Pointer(pLeft)).FpPrior = uintptr(0)
				(*TSelect)(unsafe.Pointer(pLeft)).Fop = uint8(TK_SELECT)
				p = (*TSelect)(unsafe.Pointer(pRet)).FpSrc + 8
				libc.SetBitFieldPtr32Uint32(p+12+4, libc.Uint32FromInt32(1), 6, 0x40)
				(*TSrcItem)(unsafe.Pointer(p)).FiCursor = -int32(1)
				*(*Tu32)(unsafe.Pointer(p + 32)) = uint32(2)
				if _sqlite3SrcItemAttachSubquery(tls, pParse, p, pLeft, 0) != 0 {
					pSubq = *(*uintptr)(unsafe.Pointer(p + 44))
					(*TSubquery)(unsafe.Pointer(pSubq)).FaddrFillSub = _sqlite3VdbeCurrentAddr(tls, v) + int32(1)
					v2 = pParse + 48
					*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
					v1 = *(*int32)(unsafe.Pointer(v2))
					(*TSubquery)(unsafe.Pointer(pSubq)).FregReturn = v1
					_sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn, 0, (*TSubquery)(unsafe.Pointer(pSubq)).FaddrFillSub)
					_sqlite3SelectDestInit(tls, bp, int32(SRT_Coroutine), (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn)
					/* Allocate registers for the output of the co-routine. Do so so
					 ** that there are two unused registers immediately before those
					 ** used by the co-routine. This allows the code in sqlite3Insert()
					 ** to use these registers directly, instead of copying the output
					 ** of the co-routine to a separate array for processing.  */
					(**(**TSelectDest)(__ccgo_up(bp))).FiSdst = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(3)
					(**(**TSelectDest)(__ccgo_up(bp))).FnSdst = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pLeft)).FpEList)).FnExpr
					**(**int32)(__ccgo_up(pParse + 48)) += int32(2) + (**(**TSelectDest)(__ccgo_up(bp))).FnSdst
					**(**Tu32)(__ccgo_up(pLeft + 4)) |= uint32(SF_MultiValue)
					_sqlite3Select(tls, pParse, pLeft, bp)
					(*TSubquery)(unsafe.Pointer(pSubq)).FregResult = (**(**TSelectDest)(__ccgo_up(bp))).FiSdst
				}
				pLeft = pRet
			}
		} else {
			p = (*TSelect)(unsafe.Pointer(pLeft)).FpSrc + 8
			*(*Tu32)(unsafe.Pointer(p + 32)) = *(*Tu32)(unsafe.Pointer(p + 32)) + 1
		}
		if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
			pSubq1 = *(*uintptr)(unsafe.Pointer(p + 44))
			if (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(pSubq1)).FpSelect)).FpEList)).FnExpr != (*TExprList)(unsafe.Pointer(pRow)).FnExpr {
				_sqlite3SelectWrongNumTermsError(tls, pParse, (*TSubquery)(unsafe.Pointer(pSubq1)).FpSelect)
			} else {
				_sqlite3ExprCodeExprList(tls, pParse, pRow, (*TSubquery)(unsafe.Pointer(pSubq1)).FregResult, 0, uint8(0))
				_sqlite3VdbeAddOp1(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Yield), (*TSubquery)(unsafe.Pointer(pSubq1)).FregReturn)
			}
		}
		_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pRow)
	}
	return pLeft
}

// C documentation
//
//	/*
//	** Cause a function to throw an error if it was call from OP_PureFunc
//	** rather than OP_Function.
//	**
//	** OP_PureFunc means that the function must be deterministic, and should
//	** throw an error if it is given inputs that would make it non-deterministic.
//	** This routine is invoked by date/time functions that use non-deterministic
//	** features such as 'now'.
//	*/
func _sqlite3NotPureFunc(tls *libc.TLS, pCtx uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pOp, zContext, zMsg uintptr
	_, _, _ = pOp, zContext, zMsg
	if (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe == uintptr(0) {
		return int32(1)
	}
	pOp = (*TVdbe)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe)).FaOp + uintptr((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FiOp)*20
	if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_PureFunc) {
		if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp5)&int32(NC_IsCheck) != 0 {
			zContext = __ccgo_ts + 6639
		} else {
			if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp5)&int32(NC_GenCol) != 0 {
				zContext = __ccgo_ts + 6658
			} else {
				zContext = __ccgo_ts + 6677
			}
		}
		zMsg = Xsqlite3_mprintf(tls, __ccgo_ts+6686, libc.VaList(bp+8, (*TFuncDef)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpFunc)).FzName, zContext))
		Xsqlite3_result_error(tls, pCtx, zMsg, -int32(1))
		Xsqlite3_free(tls, zMsg)
		return 0
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** This routine reactivates the memory allocator and clears the
//	** db->mallocFailed flag as necessary.
//	**
//	** The memory allocator is not restarted if there are running
//	** VDBEs.
//	*/
func _sqlite3OomClear(tls *libc.TLS, db uintptr) {
	var v1 int32
	_ = v1
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec == 0 {
		(*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed = uint8(0)
		libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED))
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable - 1
		if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0 {
			v1 = 0
		} else {
			v1 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue)
		}
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(v1)
	}
}

// C documentation
//
//	/*
//	** Call this routine to record the fact that an OOM (out-of-memory) error
//	** has happened.  This routine will set db->mallocFailed, and also
//	** temporarily disable the lookaside memory allocator and interrupt
//	** any running VDBEs.
//	**
//	** Always return a NULL pointer so that this routine can be invoked using
//	**
//	**      return sqlite3OomFault(db);
//	**
//	** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
//	** common case where no OOM occurs.
//	*/
func _sqlite3OomFault(tls *libc.TLS, db uintptr) (r uintptr) {
	var pParse uintptr
	_ = pParse
	if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).FbBenignMalloc) == 0 {
		(*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed = uint8(1)
		if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec > 0 {
			libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(1), libc.Int32FromInt32(__ATOMIC_RELAXED))
		}
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable + 1
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0)
		if (*Tsqlite3)(unsafe.Pointer(db)).FpParse != 0 {
			_sqlite3ErrorMsg(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpParse, __ccgo_ts+1681, 0)
			(*TParse)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpParse)).Frc = int32(SQLITE_NOMEM)
			pParse = (*TParse)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpParse)).FpOuterParse
			for {
				if !(pParse != 0) {
					break
				}
				(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
				(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM)
				goto _1
			_1:
				;
				pParse = (*TParse)(unsafe.Pointer(pParse)).FpOuterParse
			}
		}
	}
	return uintptr(0)
}

// C documentation
//
//	/*
//	** Allocate cursors for the pTab table and all its indices and generate
//	** code to open and initialized those cursors.
//	**
//	** The cursor for the object that contains the complete data (normally
//	** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
//	** ROWID table) is returned in *piDataCur.  The first index cursor is
//	** returned in *piIdxCur.  The number of indices is returned.
//	**
//	** Use iBase as the first cursor (either the *piDataCur for rowid tables
//	** or the first index for WITHOUT ROWID tables) if it is non-negative.
//	** If iBase is negative, then allocate the next available cursor.
//	**
//	** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
//	** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
//	** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
//	** pTab->pIndex list.
//	**
//	** If pTab is a virtual table, then this routine is a no-op and the
//	** *piDataCur and *piIdxCur values are left uninitialized.
//	*/
func _sqlite3OpenTableAndIndices(tls *libc.TLS, pParse uintptr, pTab uintptr, op int32, p5 Tu8, iBase int32, aToOpen uintptr, piDataCur uintptr, piIdxCur uintptr) (r int32) {
	var i, iDataCur, iDb, iIdxCur, v1 int32
	var pIdx, v uintptr
	_, _, _, _, _, _, _ = i, iDataCur, iDb, iIdxCur, pIdx, v, v1
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		/* This routine is a no-op for virtual tables. Leave the output
		 ** variables *piDataCur and *piIdxCur set to illegal cursor numbers
		 ** for improved error detection. */
		v1 = -libc.Int32FromInt32(999)
		**(**int32)(__ccgo_up(piIdxCur)) = v1
		**(**int32)(__ccgo_up(piDataCur)) = v1
		return 0
	}
	iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	if iBase < 0 {
		iBase = (*TParse)(unsafe.Pointer(pParse)).FnTab
	}
	v1 = iBase
	iBase = iBase + 1
	iDataCur = v1
	**(**int32)(__ccgo_up(piDataCur)) = iDataCur
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) && (aToOpen == uintptr(0) || **(**Tu8)(__ccgo_up(aToOpen)) != 0) {
		_sqlite3OpenTable(tls, pParse, iDataCur, iDb, pTab, op)
	} else {
		if int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnoSharedCache) == 0 {
			_sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, libc.BoolUint8(op == int32(OP_OpenWrite)), (*TTable)(unsafe.Pointer(pTab)).FzName)
		}
	}
	**(**int32)(__ccgo_up(piIdxCur)) = iBase
	i = 0
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		v1 = iBase
		iBase = iBase + 1
		iIdxCur = v1
		if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
			**(**int32)(__ccgo_up(piDataCur)) = iIdxCur
			p5 = uint8(0)
		}
		if aToOpen == uintptr(0) || **(**Tu8)(__ccgo_up(aToOpen + uintptr(i+int32(1)))) != 0 {
			_sqlite3VdbeAddOp3(tls, v, op, iIdxCur, int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb)
			_sqlite3VdbeSetP4KeyInfo(tls, pParse, pIdx)
			_sqlite3VdbeChangeP5(tls, v, uint16(p5))
		}
		goto _3
	_3:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		i = i + 1
	}
	if iBase > (*TParse)(unsafe.Pointer(pParse)).FnTab {
		(*TParse)(unsafe.Pointer(pParse)).FnTab = iBase
	}
	return i
}

// C documentation
//
//	/*
//	** Make sure the TEMP database is open and available for use.  Return
//	** the number of errors.  Leave any error messages in the pParse structure.
//	*/
func _sqlite3OpenTempDatabase(tls *libc.TLS, pParse uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db uintptr
	var rc int32
	var _ /* pBt at bp+0 */ uintptr
	_, _ = db, rc
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt == uintptr(0) && !((*TParse)(unsafe.Pointer(pParse)).Fexplain != 0) {
		rc = _sqlite3BtreeOpen(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, uintptr(0), db, bp, 0, _flags)
		if rc != SQLITE_OK {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17355, 0)
			(*TParse)(unsafe.Pointer(pParse)).Frc = rc
			return int32(1)
		}
		(**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpBt = **(**uintptr)(__ccgo_up(bp))
		if int32(SQLITE_NOMEM) == _sqlite3BtreeSetPageSize(tls, **(**uintptr)(__ccgo_up(bp)), (*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize, 0, 0) {
			_sqlite3OomFault(tls, db)
			return int32(1)
		}
	}
	return 0
}

func _sqlite3OsRandomness(tls *libc.TLS, pVfs uintptr, nByte int32, zBufOut uintptr) (r int32) {
	if _sqlite3Config.FiPrngSeed != 0 {
		libc.Xmemset(tls, zBufOut, 0, uint32(nByte))
		if nByte > libc.Int32FromInt64(4) {
			nByte = int32(4)
		}
		libc.Xmemcpy(tls, zBufOut, uintptr(unsafe.Pointer(&_sqlite3Config))+276, uint32(nByte))
		return SQLITE_OK
	} else {
		return (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FxRandomness})))(tls, pVfs, nByte, zBufOut)
	}
	return r
}

// C documentation
//
//	/*
//	** Allocate an Expr node which joins as many as two subtrees.
//	**
//	** One or both of the subtrees can be NULL.  Return a pointer to the new
//	** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
//	** free the subtrees and return NULL.
//	*/
func _sqlite3PExpr(tls *libc.TLS, pParse uintptr, op int32, pLeft uintptr, pRight uintptr) (r uintptr) {
	var p uintptr
	_ = p
	p = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(52))
	if p != 0 {
		libc.Xmemset(tls, p, 0, uint32(52))
		(*TExpr)(unsafe.Pointer(p)).Fop = uint8(op & int32(0xff))
		(*TExpr)(unsafe.Pointer(p)).FiAgg = int16(-int32(1))
		_sqlite3ExprAttachSubtrees(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p, pLeft, pRight)
		_sqlite3ExprCheckHeight(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FnHeight)
	} else {
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pLeft)
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pRight)
	}
	return p
}

// C documentation
//
//	/*
//	** Add pSelect to the Expr.x.pSelect field.  Or, if pExpr is NULL (due
//	** do a memory allocation failure) then delete the pSelect object.
//	*/
func _sqlite3PExprAddSelect(tls *libc.TLS, pParse uintptr, pExpr uintptr, pSelect uintptr) {
	if pExpr != 0 {
		*(*uintptr)(unsafe.Pointer(pExpr + 20)) = pSelect
		**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_xIsSelect) | libc.Int32FromInt32(EP_Subquery))
		_sqlite3ExprSetHeightAndFlags(tls, pParse, pExpr)
	} else {
		_sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSelect)
	}
}

// C documentation
//
//	/*
//	** A call to this routine tells the pager that it is not necessary to
//	** write the information on page pPg back to the disk, even though
//	** that page might be marked as dirty.  This happens, for example, when
//	** the page has been added as a leaf of the freelist and so its
//	** content no longer matters.
//	**
//	** The overlying software layer calls this routine when all of the data
//	** on the given page is unused. The pager marks the page as clean so
//	** that it does not get written to disk.
//	**
//	** Tests show that this optimization can quadruple the speed of large
//	** DELETE operations.
//	**
//	** This optimization cannot be used with a temp-file, as the page may
//	** have been dirty at the start of the transaction. In that case, if
//	** memory pressure forces page pPg out of the cache, the data does need
//	** to be written out to disk so that it may be read back in if the
//	** current transaction is rolled back.
//	*/
func _sqlite3PagerDontWrite(tls *libc.TLS, pPg uintptr) {
	var pPager, v1 uintptr
	_, _ = pPager, v1
	pPager = (*TPgHdr)(unsafe.Pointer(pPg)).FpPager
	if !((*TPager)(unsafe.Pointer(pPager)).FtempFile != 0) && int32((*TPgHdr)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_DIRTY) != 0 && (*TPager)(unsafe.Pointer(pPager)).FnSavepoint == 0 {
		v1 = pPg + 28
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(PGHDR_DONT_WRITE))
		v1 = pPg + 28
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(PGHDR_WRITEABLE))
	}
}

// C documentation
//
//	/*
//	** Return the approximate number of bytes of memory currently
//	** used by the pager and its associated cache.
//	*/
func _sqlite3PagerMemUsed(tls *libc.TLS, pPager uintptr) (r int32) {
	var perPageSize int32
	_ = perPageSize
	perPageSize = int32((*TPager)(unsafe.Pointer(pPager)).FpageSize + int64((*TPager)(unsafe.Pointer(pPager)).FnExtra) + int64(int32(libc.Uint32FromInt64(48)+libc.Uint32FromInt32(5)*libc.Uint32FromInt64(4))))
	return int32(int64(perPageSize*_sqlite3PcachePagecount(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache)+_sqlite3MallocSize(tls, pPager)) + (*TPager)(unsafe.Pointer(pPager)).FpageSize)
}

// C documentation
//
//	/*
//	** Move the page pPg to location pgno in the file.
//	**
//	** There must be no references to the page previously located at
//	** pgno (which we call pPgOld) though that page is allowed to be
//	** in cache.  If the page previously located at pgno is not already
//	** in the rollback journal, it is not put there by by this routine.
//	**
//	** References to the page pPg remain valid. Updating any
//	** meta-data associated with pPg (i.e. data stored in the nExtra bytes
//	** allocated along with the page) is the responsibility of the caller.
//	**
//	** A transaction must be active when this routine is called. It used to be
//	** required that a statement transaction was not active, but this restriction
//	** has been removed (CREATE INDEX needs to move a page when a statement
//	** transaction is active).
//	**
//	** If the fourth argument, isCommit, is non-zero, then this page is being
//	** moved as part of a database reorganization just before the transaction
//	** is being committed. In this case, it is guaranteed that the database page
//	** pPg refers to will not be written to again within this transaction.
//	**
//	** This function may return SQLITE_NOMEM or an IO error code if an error
//	** occurs. Otherwise, it returns SQLITE_OK.
//	*/
func _sqlite3PagerMovepage(tls *libc.TLS, pPager uintptr, pPg uintptr, pgno TPgno, isCommit int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var needSyncPgno, origPgno TPgno
	var pPgOld, v3 uintptr
	var rc, v1 int32
	var v2 bool
	var _ /* pPgHdr at bp+0 */ uintptr
	_, _, _, _, _, _, _ = needSyncPgno, origPgno, pPgOld, rc, v1, v2, v3 /* The page being overwritten. */
	needSyncPgno = uint32(0)                                             /* The original page number */
	/* In order to be able to rollback, an in-memory database must journal
	 ** the page we are moving from.
	 */
	if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 {
		rc = _sqlite3PagerWrite(tls, pPg)
		if rc != 0 {
			return rc
		}
	}
	/* If the page being moved is dirty and has not been saved by the latest
	 ** savepoint, then save the current contents of the page into the
	 ** sub-journal now. This is required to handle the following scenario:
	 **
	 **   BEGIN;
	 **     <journal page X, then modify it in memory>
	 **     SAVEPOINT one;
	 **       <Move page X to location Y>
	 **     ROLLBACK TO one;
	 **
	 ** If page X were not written to the sub-journal here, it would not
	 ** be possible to restore its contents when the "ROLLBACK TO one"
	 ** statement were is processed.
	 **
	 ** subjournalPage() may need to allocate space to store pPg->pgno into
	 ** one or more savepoint bitvecs. This is the reason this function
	 ** may return SQLITE_NOMEM.
	 */
	if v2 = int32((*TDbPage)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_DIRTY) != 0; v2 {
		v1 = _subjournalPageIfRequired(tls, pPg)
		rc = v1
	}
	if v2 && SQLITE_OK != v1 {
		return rc
	}
	/* If the journal needs to be sync()ed before page pPg->pgno can
	 ** be written to, store pPg->pgno in local variable needSyncPgno.
	 **
	 ** If the isCommit flag is set, there is no need to remember that
	 ** the journal needs to be sync()ed before database page pPg->pgno
	 ** can be written to. The caller has already promised not to write to it.
	 */
	if int32((*TDbPage)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_NEED_SYNC) != 0 && !(isCommit != 0) {
		needSyncPgno = (*TDbPage)(unsafe.Pointer(pPg)).Fpgno
	}
	/* If the cache contains a page with page-number pgno, remove it
	 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
	 ** page pgno before the 'move' operation, it needs to be retained
	 ** for the page moved there.
	 */
	v3 = pPg + 28
	*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) & ^libc.Int32FromInt32(PGHDR_NEED_SYNC))
	pPgOld = _sqlite3PagerLookup(tls, pPager, pgno)
	if pPgOld != 0 {
		if (*TPgHdr)(unsafe.Pointer(pPgOld)).FnRef > int64(1) {
			_sqlite3PagerUnrefNotNull(tls, pPgOld)
			return _sqlite3CorruptError(tls, int32(66914))
		}
		v3 = pPg + 28
		*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | int32((*TPgHdr)(unsafe.Pointer(pPgOld)).Fflags)&libc.Int32FromInt32(PGHDR_NEED_SYNC))
		if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 {
			/* Do not discard pages from an in-memory database since we might
			 ** need to rollback later.  Just move the page out of the way. */
			_sqlite3PcacheMove(tls, pPgOld, (*TPager)(unsafe.Pointer(pPager)).FdbSize+uint32(1))
		} else {
			_sqlite3PcacheDrop(tls, pPgOld)
		}
	}
	origPgno = (*TDbPage)(unsafe.Pointer(pPg)).Fpgno
	_sqlite3PcacheMove(tls, pPg, pgno)
	_sqlite3PcacheMakeDirty(tls, pPg)
	/* For an in-memory database, make sure the original page continues
	 ** to exist, in case the transaction needs to roll back.  Use pPgOld
	 ** as the original page since it has already been allocated.
	 */
	if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 && pPgOld != 0 {
		_sqlite3PcacheMove(tls, pPgOld, origPgno)
		_sqlite3PagerUnrefNotNull(tls, pPgOld)
	}
	if needSyncPgno != 0 {
		rc = _sqlite3PagerGet(tls, pPager, needSyncPgno, bp, 0)
		if rc != SQLITE_OK {
			if needSyncPgno <= (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize {
				_sqlite3BitvecClear(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, needSyncPgno, (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace)
			}
			return rc
		}
		v3 = **(**uintptr)(__ccgo_up(bp)) + 28
		*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | libc.Int32FromInt32(PGHDR_NEED_SYNC))
		_sqlite3PcacheMakeDirty(tls, **(**uintptr)(__ccgo_up(bp)))
		_sqlite3PagerUnrefNotNull(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Allocate and initialize a new Pager object and put a pointer to it
//	** in *ppPager. The pager should eventually be freed by passing it
//	** to sqlite3PagerClose().
//	**
//	** The zFilename argument is the path to the database file to open.
//	** If zFilename is NULL then a randomly-named temporary file is created
//	** and used as the file to be cached. Temporary files are be deleted
//	** automatically when they are closed. If zFilename is ":memory:" then
//	** all information is held in cache. It is never written to disk.
//	** This can be used to implement an in-memory database.
//	**
//	** The nExtra parameter specifies the number of bytes of space allocated
//	** along with each page reference. This space is available to the user
//	** via the sqlite3PagerGetExtra() API.  When a new page is allocated, the
//	** first 8 bytes of this space are zeroed but the remainder is uninitialized.
//	** (The extra space is used by btree as the MemPage object.)
//	**
//	** The flags argument is used to specify properties that affect the
//	** operation of the pager. It should be passed some bitwise combination
//	** of the PAGER_* flags.
//	**
//	** The vfsFlags parameter is a bitmask to pass to the flags parameter
//	** of the xOpen() method of the supplied VFS when opening files.
//	**
//	** If the pager object is allocated and the specified file opened
//	** successfully, SQLITE_OK is returned and *ppPager set to point to
//	** the new pager object. If an error occurs, *ppPager is set to NULL
//	** and error code returned. This function may return SQLITE_NOMEM
//	** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
//	** various SQLITE_IO_XXX errors.
//	*/
func _sqlite3PagerOpen(tls *libc.TLS, pVfs uintptr, ppPager uintptr, zFilename uintptr, nExtra int32, flags int32, vfsFlags int32, __ccgo_fp_xReinit uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iDc, journalFileSize, memDb, memJM, nPathname, nUriByte, pcacheSize, rc, readOnly, tempFile, useJournal, v4 int32
	var pPtr, z, zPathname, zUri, v1 uintptr
	var _ /* fout at bp+8 */ int32
	var _ /* pPager at bp+0 */ uintptr
	var _ /* szPageDflt at bp+4 */ Tu32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = iDc, journalFileSize, memDb, memJM, nPathname, nUriByte, pPtr, pcacheSize, rc, readOnly, tempFile, useJournal, z, zPathname, zUri, v1, v4
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)                         /* Pager object to allocate and return */
	rc = SQLITE_OK                                                    /* Return code */
	tempFile = 0                                                      /* True for temp files (incl. in-memory files) */
	memDb = 0                                                         /* True if this is an in-memory file */
	memJM = 0                                                         /* Memory journal mode */
	readOnly = 0                                                      /* Bytes to allocate for each journal fd */
	zPathname = uintptr(0)                                            /* Full path to database file */
	nPathname = 0                                                     /* Number of bytes in zPathname */
	useJournal = libc.BoolInt32(flags&int32(PAGER_OMIT_JOURNAL) == 0) /* False to omit journal */
	pcacheSize = _sqlite3PcacheSize(tls)                              /* Bytes to allocate for PCache */
	**(**Tu32)(__ccgo_up(bp + 4)) = uint32(SQLITE_DEFAULT_PAGE_SIZE)  /* Default page size */
	zUri = uintptr(0)                                                 /* URI args to copy */
	nUriByte = int32(1)                                               /* Number of bytes of URI args at *zUri */
	/* Figure out how much space is required for each journal file-handle
	 ** (there are two of them, the main journal and the sub-journal).  */
	journalFileSize = (_sqlite3JournalSize(tls, pVfs) + int32(7)) & ^libc.Int32FromInt32(7)
	/* Set the output variable to NULL in case an error occurs. */
	**(**uintptr)(__ccgo_up(ppPager)) = uintptr(0)
	if flags&int32(PAGER_MEMORY) != 0 {
		memDb = int32(1)
		if zFilename != 0 && **(**int8)(__ccgo_up(zFilename)) != 0 {
			zPathname = _sqlite3DbStrDup(tls, uintptr(0), zFilename)
			if zPathname == uintptr(0) {
				return int32(SQLITE_NOMEM)
			}
			nPathname = _sqlite3Strlen30(tls, zPathname)
			zFilename = uintptr(0)
		}
	}
	/* Compute and store the full pathname in an allocated buffer pointed
	 ** to by zPathname, length nPathname. Or, if this is a temporary file,
	 ** leave both nPathname and zPathname set to 0.
	 */
	if zFilename != 0 && **(**int8)(__ccgo_up(zFilename)) != 0 {
		nPathname = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname + int32(1)
		zPathname = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(int64(2)*int64(nPathname)))
		if zPathname == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		**(**int8)(__ccgo_up(zPathname)) = 0 /* Make sure initialized even if FullPathname() fails */
		rc = _sqlite3OsFullPathname(tls, pVfs, zFilename, nPathname, zPathname)
		if rc != SQLITE_OK {
			if rc == libc.Int32FromInt32(SQLITE_OK)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8) {
				if vfsFlags&int32(SQLITE_OPEN_NOFOLLOW) != 0 {
					rc = libc.Int32FromInt32(SQLITE_CANTOPEN) | libc.Int32FromInt32(6)<<libc.Int32FromInt32(8)
				} else {
					rc = SQLITE_OK
				}
			}
		}
		nPathname = _sqlite3Strlen30(tls, zPathname)
		v1 = zFilename + uintptr(_sqlite3Strlen30(tls, zFilename)+int32(1))
		zUri = v1
		z = v1
		for **(**int8)(__ccgo_up(z)) != 0 {
			z = z + uintptr(libc.Xstrlen(tls, z)+uint32(1))
			z = z + uintptr(libc.Xstrlen(tls, z)+uint32(1))
		}
		nUriByte = t__predefined_ptrdiff_t(z+1) - int32(zUri)
		if rc == SQLITE_OK && nPathname+int32(8) > (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname {
			/* This branch is taken when the journal path required by
			 ** the database being opened will be more than pVfs->mxPathname
			 ** bytes in length. This means the database cannot be opened,
			 ** as it will not be possible to open the journal file or even
			 ** check for a hot-journal before reading.
			 */
			rc = _sqlite3CantopenError(tls, int32(64499))
		}
		if rc != SQLITE_OK {
			_sqlite3DbFree(tls, uintptr(0), zPathname)
			return rc
		}
	}
	/* Allocate memory for the Pager structure, PCache object, the
	 ** three file descriptors, the database file name and the journal
	 ** file name. The layout in memory is as follows:
	 **
	 **     Pager object                    (sizeof(Pager) bytes)
	 **     PCache object                   (sqlite3PcacheSize() bytes)
	 **     Database file handle            (pVfs->szOsFile bytes)
	 **     Sub-journal file handle         (journalFileSize bytes)
	 **     Main journal file handle        (journalFileSize bytes)
	 **     Ptr back to the Pager           (sizeof(Pager*) bytes)
	 **     \0\0\0\0 database prefix        (4 bytes)
	 **     Database file name              (nPathname+1 bytes)
	 **     URI query parameters            (nUriByte bytes)
	 **     Journal filename                (nPathname+8+1 bytes)
	 **     WAL filename                    (nPathname+4+1 bytes)
	 **     \0\0\0 terminator               (3 bytes)
	 **
	 ** Some 3rd-party software, over which we have no control, depends on
	 ** the specific order of the filenames and the \0 separators between them
	 ** so that it can (for example) find the database filename given the WAL
	 ** filename without using the sqlite3_filename_database() API.  This is a
	 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
	 ** software is in widespread use, so we try to avoid changing the filename
	 ** order and formatting if possible.  In particular, the details of the
	 ** filename format expected by 3rd-party software should be as follows:
	 **
	 **   - Main Database Path
	 **   - \0
	 **   - Multiple URI components consisting of:
	 **     - Key
	 **     - \0
	 **     - Value
	 **     - \0
	 **   - \0
	 **   - Journal Path
	 **   - \0
	 **   - WAL Path (zWALName)
	 **   - \0
	 **
	 ** The sqlite3_create_filename() interface and the databaseFilename() utility
	 ** that is used by sqlite3_filename_database() and kin also depend on the
	 ** specific formatting and order of the various filenames, so if the format
	 ** changes here, be sure to change it there as well.
	 */
	pPtr = _sqlite3MallocZero(tls, uint64((libc.Uint32FromInt64(240)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))+uint32((pcacheSize+libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7))+uint32(((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile+libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7)))+uint64(journalFileSize)*uint64(2)+uint64(__SIZEOF_POINTER__)+uint64(4)+uint64(nPathname)+uint64(1)+uint64(nUriByte)+uint64(nPathname)+uint64(8)+uint64(1)+uint64(nPathname)+uint64(4)+uint64(1)+uint64(3))
	if !(pPtr != 0) {
		_sqlite3DbFree(tls, uintptr(0), zPathname)
		return int32(SQLITE_NOMEM)
	}
	**(**uintptr)(__ccgo_up(bp)) = pPtr
	pPtr = pPtr + uintptr((libc.Uint32FromInt64(240)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpPCache = pPtr
	pPtr = pPtr + uintptr((pcacheSize+libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7))
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Ffd = pPtr
	pPtr = pPtr + uintptr(((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile+libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7))
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fsjfd = pPtr
	pPtr = pPtr + uintptr(journalFileSize)
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fjfd = pPtr
	pPtr = pPtr + uintptr(journalFileSize)
	libc.Xmemcpy(tls, pPtr, bp, uint32(__SIZEOF_POINTER__))
	pPtr = pPtr + uintptr(__SIZEOF_POINTER__)
	/* Fill in the Pager.zFilename and pPager.zQueryParam fields */
	pPtr = pPtr + uintptr(4) /* Skip zero prefix */
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FzFilename = pPtr
	if nPathname > 0 {
		libc.Xmemcpy(tls, pPtr, zPathname, uint32(nPathname))
		pPtr = pPtr + uintptr(nPathname+int32(1))
		if zUri != 0 {
			libc.Xmemcpy(tls, pPtr, zUri, uint32(nUriByte))
			pPtr = pPtr + uintptr(nUriByte)
		} else {
			pPtr = pPtr + 1
		}
	}
	/* Fill in Pager.zJournal */
	if nPathname > 0 {
		(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FzJournal = pPtr
		libc.Xmemcpy(tls, pPtr, zPathname, uint32(nPathname))
		pPtr = pPtr + uintptr(nPathname)
		libc.Xmemcpy(tls, pPtr, __ccgo_ts+5444, uint32(8))
		pPtr = pPtr + uintptr(libc.Int32FromInt32(8)+libc.Int32FromInt32(1))
	} else {
		(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FzJournal = uintptr(0)
	}
	/* Fill in Pager.zWal */
	if nPathname > 0 {
		(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FzWal = pPtr
		libc.Xmemcpy(tls, pPtr, zPathname, uint32(nPathname))
		pPtr = pPtr + uintptr(nPathname)
		libc.Xmemcpy(tls, pPtr, __ccgo_ts+5453, uint32(4))
		pPtr = pPtr + uintptr(libc.Int32FromInt32(4)+libc.Int32FromInt32(1))
	} else {
		(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FzWal = uintptr(0)
	}
	_ = pPtr /* Suppress warning about unused pPtr value */
	if nPathname != 0 {
		_sqlite3DbFree(tls, uintptr(0), zPathname)
	}
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpVfs = pVfs
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FvfsFlags = uint32(vfsFlags)
	/* Open the pager file.
	 */
	if !(zFilename != 0 && **(**int8)(__ccgo_up(zFilename)) != 0) {
		goto _2
	}
	**(**int32)(__ccgo_up(bp + 8)) = 0 /* VFS flags returned by xOpen() */
	rc = _sqlite3OsOpen(tls, pVfs, (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FzFilename, (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Ffd, vfsFlags, bp+8)
	v4 = libc.BoolInt32(**(**int32)(__ccgo_up(bp + 8))&libc.Int32FromInt32(SQLITE_OPEN_MEMORY) != libc.Int32FromInt32(0))
	memJM = v4
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FmemVfs = uint8(v4)
	readOnly = libc.BoolInt32(**(**int32)(__ccgo_up(bp + 8))&int32(SQLITE_OPEN_READONLY) != 0)
	/* If the file was successfully opened for read/write access,
	 ** choose a default page size in case we have to create the
	 ** database file. The default page size is the maximum of:
	 **
	 **    + SQLITE_DEFAULT_PAGE_SIZE,
	 **    + The value returned by sqlite3OsSectorSize()
	 **    + The largest page size that can be written atomically.
	 */
	if rc == SQLITE_OK {
		iDc = _sqlite3OsDeviceCharacteristics(tls, (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Ffd)
		if !(readOnly != 0) {
			_setSectorSize(tls, **(**uintptr)(__ccgo_up(bp)))
			if **(**Tu32)(__ccgo_up(bp + 4)) < (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FsectorSize {
				if (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FsectorSize > uint32(SQLITE_MAX_DEFAULT_PAGE_SIZE) {
					**(**Tu32)(__ccgo_up(bp + 4)) = uint32(SQLITE_MAX_DEFAULT_PAGE_SIZE)
				} else {
					**(**Tu32)(__ccgo_up(bp + 4)) = (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FsectorSize
				}
			}
		}
		(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnoLock = uint8(Xsqlite3_uri_boolean(tls, (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FzFilename, __ccgo_ts+5458, 0))
		if iDc&int32(SQLITE_IOCAP_IMMUTABLE) != 0 || Xsqlite3_uri_boolean(tls, (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FzFilename, __ccgo_ts+5465, 0) != 0 {
			vfsFlags = vfsFlags | int32(SQLITE_OPEN_READONLY)
			goto act_like_temp_file
		}
	}
	goto _3
_2:
	;
	/* If a temporary file is requested, it is not opened immediately.
	 ** In this case we accept the default page size and delay actually
	 ** opening the file until the first call to OsWrite().
	 **
	 ** This branch is also run for an in-memory database. An in-memory
	 ** database is the same as a temp-file that is never written out to
	 ** disk and uses an in-memory rollback journal.
	 **
	 ** This branch also runs for files marked as immutable.
	 */
	goto act_like_temp_file
act_like_temp_file:
	;
	tempFile = int32(1)
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FeState = uint8(PAGER_READER)  /* Pretend we already have a lock */
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FeLock = uint8(EXCLUSIVE_LOCK) /* Pretend we are in EXCLUSIVE mode */
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnoLock = uint8(1)             /* Do no locking */
	readOnly = vfsFlags & int32(SQLITE_OPEN_READONLY)
_3:
	;
	/* The following call to PagerSetPagesize() serves to set the value of
	 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
	 */
	if rc == SQLITE_OK {
		rc = _sqlite3PagerSetPagesize(tls, **(**uintptr)(__ccgo_up(bp)), bp+4, -int32(1))
	}
	/* Initialize the PCache object. */
	if rc == SQLITE_OK {
		nExtra = (nExtra + int32(7)) & ^libc.Int32FromInt32(7)
		if !(memDb != 0) {
			v1 = __ccgo_fp(_pagerStress)
		} else {
			v1 = uintptr(0)
		}
		rc = _sqlite3PcacheOpen(tls, int32(**(**Tu32)(__ccgo_up(bp + 4))), nExtra, libc.BoolInt32(!(memDb != 0)), v1, **(**uintptr)(__ccgo_up(bp)), (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpPCache)
	}
	/* If an error occurred above, free the  Pager structure and close the file.
	 */
	if rc != SQLITE_OK {
		_sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Ffd)
		_sqlite3PageFree(tls, (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FpTmpSpace)
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp)))
		return rc
	}
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FuseJournal = uint8(useJournal)
	/* pPager->stmtOpen = 0; */
	/* pPager->stmtInUse = 0; */
	/* pPager->nRef = 0; */
	/* pPager->stmtSize = 0; */
	/* pPager->stmtJSize = 0; */
	/* pPager->nPage = 0; */
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FmxPgno = uint32(SQLITE_MAX_PAGE_COUNT)
	/* pPager->state = PAGER_UNLOCK; */
	/* pPager->errMask = 0; */
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FtempFile = uint8(tempFile)
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FexclusiveMode = uint8(tempFile)
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FchangeCountDone = (*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FtempFile
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FmemDb = uint8(memDb)
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FreadOnly = uint8(readOnly)
	_sqlite3PagerSetFlags(tls, **(**uintptr)(__ccgo_up(bp)), uint32(libc.Int32FromInt32(SQLITE_DEFAULT_SYNCHRONOUS)+libc.Int32FromInt32(1)|libc.Int32FromInt32(PAGER_CACHESPILL)))
	/* pPager->pFirst = 0; */
	/* pPager->pFirstSynced = 0; */
	/* pPager->pLast = 0; */
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnExtra = uint16(nExtra)
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FjournalSizeLimit = int64(-int32(1))
	_setSectorSize(tls, **(**uintptr)(__ccgo_up(bp)))
	if !(useJournal != 0) {
		(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FjournalMode = uint8(PAGER_JOURNALMODE_OFF)
	} else {
		if memDb != 0 || memJM != 0 {
			(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FjournalMode = uint8(PAGER_JOURNALMODE_MEMORY)
		}
	}
	/* pPager->xBusyHandler = 0; */
	/* pPager->pBusyHandlerArg = 0; */
	(*TPager)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FxReiniter = __ccgo_fp_xReinit
	_setGetterMethod(tls, **(**uintptr)(__ccgo_up(bp)))
	/* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
	/* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
	**(**uintptr)(__ccgo_up(ppPager)) = **(**uintptr)(__ccgo_up(bp))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Read the first N bytes from the beginning of the file into memory
//	** that pDest points to.
//	**
//	** If the pager was opened on a transient file (zFilename==""), or
//	** opened on a file less than N bytes in size, the output buffer is
//	** zeroed and SQLITE_OK returned. The rationale for this is that this
//	** function is used to read database headers, and a new transient or
//	** zero sized database has a header than consists entirely of zeroes.
//	**
//	** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
//	** the error code is returned to the caller and the contents of the
//	** output buffer undefined.
//	*/
func _sqlite3PagerReadFileheader(tls *libc.TLS, pPager uintptr, N int32, pDest uintptr) (r int32) {
	var rc int32
	_ = rc
	rc = SQLITE_OK
	libc.Xmemset(tls, pDest, 0, uint32(N))
	/* This routine is only called by btree immediately after creating
	 ** the Pager object.  There has not been an opportunity to transition
	 ** to WAL mode yet.
	 */
	if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) {
		rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, pDest, N, 0)
		if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8) {
			rc = SQLITE_OK
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is called to rollback or release (commit) a savepoint.
//	** The savepoint to release or rollback need not be the most recently
//	** created savepoint.
//	**
//	** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
//	** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
//	** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
//	** that have occurred since the specified savepoint was created.
//	**
//	** The savepoint to rollback or release is identified by parameter
//	** iSavepoint. A value of 0 means to operate on the outermost savepoint
//	** (the first created). A value of (Pager.nSavepoint-1) means operate
//	** on the most recently created savepoint. If iSavepoint is greater than
//	** (Pager.nSavepoint-1), then this function is a no-op.
//	**
//	** If a negative value is passed to this function, then the current
//	** transaction is rolled back. This is different to calling
//	** sqlite3PagerRollback() because this function does not terminate
//	** the transaction or unlock the database, it just restores the
//	** contents of the database to its original state.
//	**
//	** In any case, all savepoints with an index greater than iSavepoint
//	** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
//	** then savepoint iSavepoint is also destroyed.
//	**
//	** This function may return SQLITE_NOMEM if a memory allocation fails,
//	** or an IO error code if an IO error occurs while rolling back a
//	** savepoint. If no errors occur, SQLITE_OK is returned.
//	*/
func _sqlite3PagerSavepoint(tls *libc.TLS, pPager uintptr, op int32, iSavepoint int32) (r int32) {
	var ii, nNew, rc, v1 int32
	var pRel, pSavepoint, v3 uintptr
	var sz Ti64
	_, _, _, _, _, _, _, _ = ii, nNew, pRel, pSavepoint, rc, sz, v1, v3
	rc = (*TPager)(unsafe.Pointer(pPager)).FerrCode
	if rc == SQLITE_OK && iSavepoint < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint { /* Number of remaining savepoints after this op. */
		/* Figure out how many savepoints will still be active after this
		 ** operation. Store this value in nNew. Then free resources associated
		 ** with any savepoints that are destroyed by this operation.
		 */
		if op == int32(SAVEPOINT_RELEASE) {
			v1 = 0
		} else {
			v1 = int32(1)
		}
		nNew = iSavepoint + v1
		ii = nNew
		for {
			if !(ii < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint) {
				break
			}
			_sqlite3BitvecDestroy(tls, (**(**TPagerSavepoint)(__ccgo_up((*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(ii)*48))).FpInSavepoint)
			goto _2
		_2:
			;
			ii = ii + 1
		}
		(*TPager)(unsafe.Pointer(pPager)).FnSavepoint = nNew
		/* Truncate the sub-journal so that it only includes the parts
		 ** that are still in use. */
		if op == int32(SAVEPOINT_RELEASE) {
			pRel = (*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(nNew)*48
			if (*TPagerSavepoint)(unsafe.Pointer(pRel)).FbTruncateOnRelease != 0 && (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fsjfd)).FpMethods != uintptr(0) {
				/* Only truncate if it is an in-memory sub-journal. */
				if _sqlite3JournalIsInMemory(tls, (*TPager)(unsafe.Pointer(pPager)).Fsjfd) != 0 {
					sz = ((*TPager)(unsafe.Pointer(pPager)).FpageSize + int64(4)) * int64((*TPagerSavepoint)(unsafe.Pointer(pRel)).FiSubRec)
					rc = _sqlite3OsTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).Fsjfd, sz)
				}
				(*TPager)(unsafe.Pointer(pPager)).FnSubRec = (*TPagerSavepoint)(unsafe.Pointer(pRel)).FiSubRec
			}
		} else {
			if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) || (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) {
				if nNew == 0 {
					v3 = uintptr(0)
				} else {
					v3 = (*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(nNew-int32(1))*48
				}
				pSavepoint = v3
				rc = _pagerPlaybackSavepoint(tls, pPager, pSavepoint)
			}
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Adjust settings of the pager to those specified in the pgFlags parameter.
//	**
//	** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
//	** of the database to damage due to OS crashes or power failures by
//	** changing the number of syncs()s when writing the journals.
//	** There are four levels:
//	**
//	**    OFF       sqlite3OsSync() is never called.  This is the default
//	**              for temporary and transient files.
//	**
//	**    NORMAL    The journal is synced once before writes begin on the
//	**              database.  This is normally adequate protection, but
//	**              it is theoretically possible, though very unlikely,
//	**              that an inopertune power failure could leave the journal
//	**              in a state which would cause damage to the database
//	**              when it is rolled back.
//	**
//	**    FULL      The journal is synced twice before writes begin on the
//	**              database (with some additional information - the nRec field
//	**              of the journal header - being written in between the two
//	**              syncs).  If we assume that writing a
//	**              single disk sector is atomic, then this mode provides
//	**              assurance that the journal will not be corrupted to the
//	**              point of causing damage to the database during rollback.
//	**
//	**    EXTRA     This is like FULL except that is also syncs the directory
//	**              that contains the rollback journal after the rollback
//	**              journal is unlinked.
//	**
//	** The above is for a rollback-journal mode.  For WAL mode, OFF continues
//	** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
//	** prior to the start of checkpoint and that the database file is synced
//	** at the conclusion of the checkpoint if the entire content of the WAL
//	** was written back into the database.  But no sync operations occur for
//	** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
//	** file is synced following each commit operation, in addition to the
//	** syncs associated with NORMAL.  There is no difference between FULL
//	** and EXTRA for WAL mode.
//	**
//	** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
//	** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
//	** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
//	** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
//	** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
//	** synchronous=FULL versus synchronous=NORMAL setting determines when
//	** the xSync primitive is called and is relevant to all platforms.
//	**
//	** Numeric values associated with these states are OFF==1, NORMAL=2,
//	** and FULL=3.
//	*/
func _sqlite3PagerSetFlags(tls *libc.TLS, pPager uintptr, pgFlags uint32) {
	var level uint32
	var v1 int32
	var v2 uintptr
	_, _, _ = level, v1, v2
	level = pgFlags & uint32(PAGER_SYNCHRONOUS_MASK)
	if (*TPager)(unsafe.Pointer(pPager)).FtempFile != 0 || level == uint32(PAGER_SYNCHRONOUS_OFF) {
		(*TPager)(unsafe.Pointer(pPager)).FnoSync = uint8(1)
		(*TPager)(unsafe.Pointer(pPager)).FfullSync = uint8(0)
		(*TPager)(unsafe.Pointer(pPager)).FextraSync = uint8(0)
	} else {
		(*TPager)(unsafe.Pointer(pPager)).FnoSync = uint8(0)
		if level >= uint32(PAGER_SYNCHRONOUS_FULL) {
			v1 = int32(1)
		} else {
			v1 = 0
		}
		(*TPager)(unsafe.Pointer(pPager)).FfullSync = uint8(v1)
		/* Set Pager.extraSync if "PRAGMA synchronous=EXTRA" is requested, or
		 ** if the file-system supports F2FS style atomic writes. If this flag
		 ** is set, SQLite syncs the directory to disk immediately after deleting
		 ** a journal file in "PRAGMA journal_mode=DELETE" mode.  */
		if level == uint32(PAGER_SYNCHRONOUS_EXTRA) {
			(*TPager)(unsafe.Pointer(pPager)).FextraSync = uint8(1)
		} else {
			(*TPager)(unsafe.Pointer(pPager)).FextraSync = uint8(0)
		}
	}
	if (*TPager)(unsafe.Pointer(pPager)).FnoSync != 0 {
		(*TPager)(unsafe.Pointer(pPager)).FsyncFlags = uint8(0)
	} else {
		if pgFlags&uint32(PAGER_FULLFSYNC) != 0 {
			(*TPager)(unsafe.Pointer(pPager)).FsyncFlags = uint8(SQLITE_SYNC_FULL)
		} else {
			(*TPager)(unsafe.Pointer(pPager)).FsyncFlags = uint8(SQLITE_SYNC_NORMAL)
		}
	}
	(*TPager)(unsafe.Pointer(pPager)).FwalSyncFlags = uint8(int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags) << libc.Int32FromInt32(2))
	if (*TPager)(unsafe.Pointer(pPager)).FfullSync != 0 {
		v2 = pPager + 11
		*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) | int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags))
	}
	if pgFlags&uint32(PAGER_CKPT_FULLFSYNC) != 0 && !((*TPager)(unsafe.Pointer(pPager)).FnoSync != 0) {
		v2 = pPager + 11
		*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) | libc.Int32FromInt32(SQLITE_SYNC_FULL)<<libc.Int32FromInt32(2))
	}
	if pgFlags&uint32(PAGER_CACHESPILL) != 0 {
		v2 = pPager + 21
		*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) & ^libc.Int32FromInt32(SPILLFLAG_OFF))
	} else {
		v2 = pPager + 21
		*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) | libc.Int32FromInt32(SPILLFLAG_OFF))
	}
}

/*
** The following global variable is incremented whenever the library
** attempts to open a temporary file.  This information is used for
** testing and analysis only.
 */

// C documentation
//
//	/*
//	** Change the page size used by the Pager object. The new page size
//	** is passed in *pPageSize.
//	**
//	** If the pager is in the error state when this function is called, it
//	** is a no-op. The value returned is the error state error code (i.e.
//	** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
//	**
//	** Otherwise, if all of the following are true:
//	**
//	**   * the new page size (value of *pPageSize) is valid (a power
//	**     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
//	**
//	**   * there are no outstanding page references, and
//	**
//	**   * the database is either not an in-memory database or it is
//	**     an in-memory database that currently consists of zero pages.
//	**
//	** then the pager object page size is set to *pPageSize.
//	**
//	** If the page size is changed, then this function uses sqlite3PagerMalloc()
//	** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
//	** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
//	** In all other cases, SQLITE_OK is returned.
//	**
//	** If the page size is not changed, either because one of the enumerated
//	** conditions above is not true, the pager was in error state when this
//	** function was called, or because the memory allocation attempt failed,
//	** then *pPageSize is set to the old, retained page size before returning.
//	*/
func _sqlite3PagerSetPagesize(tls *libc.TLS, pPager uintptr, pPageSize uintptr, nReserve int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pNew uintptr
	var pageSize Tu32
	var rc int32
	var _ /* nByte at bp+0 */ Ti64
	_, _, _ = pNew, pageSize, rc
	rc = SQLITE_OK
	/* It is not possible to do a full assert_pager_state() here, as this
	 ** function may be called from within PagerOpen(), before the state
	 ** of the Pager object is internally consistent.
	 **
	 ** At one point this function returned an error if the pager was in
	 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
	 ** there is at least one outstanding page reference, this function
	 ** is a no-op for that case anyhow.
	 */
	pageSize = **(**Tu32)(__ccgo_up(pPageSize))
	if (int32((*TPager)(unsafe.Pointer(pPager)).FmemDb) == 0 || (*TPager)(unsafe.Pointer(pPager)).FdbSize == uint32(0)) && _sqlite3PcacheRefCount(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache) == 0 && pageSize != 0 && pageSize != uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize) {
		pNew = libc.UintptrFromInt32(0) /* New temp space */
		**(**Ti64)(__ccgo_up(bp)) = 0
		if int32((*TPager)(unsafe.Pointer(pPager)).FeState) > PAGER_OPEN && (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(0) {
			rc = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, bp)
		}
		if rc == SQLITE_OK {
			/* 8 bytes of zeroed overrun space is sufficient so that the b-tree
			 * cell header parser will never run off the end of the allocation */
			pNew = _sqlite3PageMalloc(tls, int32(pageSize+uint32(8)))
			if !(pNew != 0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				libc.Xmemset(tls, pNew+uintptr(pageSize), 0, uint32(8))
			}
		}
		if rc == SQLITE_OK {
			_pager_reset(tls, pPager)
			rc = _sqlite3PcacheSetPageSize(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, int32(pageSize))
		}
		if rc == SQLITE_OK {
			_sqlite3PageFree(tls, (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace)
			(*TPager)(unsafe.Pointer(pPager)).FpTmpSpace = pNew
			(*TPager)(unsafe.Pointer(pPager)).FdbSize = uint32((**(**Ti64)(__ccgo_up(bp)) + int64(pageSize) - libc.Int64FromInt32(1)) / int64(pageSize))
			(*TPager)(unsafe.Pointer(pPager)).FpageSize = int64(pageSize)
			(*TPager)(unsafe.Pointer(pPager)).FlckPgno = uint32(_sqlite3PendingByte)/pageSize + uint32(1)
		} else {
			_sqlite3PageFree(tls, pNew)
		}
	}
	**(**Tu32)(__ccgo_up(pPageSize)) = uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize)
	if rc == SQLITE_OK {
		if nReserve < 0 {
			nReserve = int32((*TPager)(unsafe.Pointer(pPager)).FnReserve)
		}
		(*TPager)(unsafe.Pointer(pPager)).FnReserve = int16(nReserve)
		_pagerFixMaplimit(tls, pPager)
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is called to obtain a shared lock on the database file.
//	** It is illegal to call sqlite3PagerGet() until after this function
//	** has been successfully called. If a shared-lock is already held when
//	** this function is called, it is a no-op.
//	**
//	** The following operations are also performed by this function.
//	**
//	**   1) If the pager is currently in PAGER_OPEN state (no lock held
//	**      on the database file), then an attempt is made to obtain a
//	**      SHARED lock on the database file. Immediately after obtaining
//	**      the SHARED lock, the file-system is checked for a hot-journal,
//	**      which is played back if present. Following any hot-journal
//	**      rollback, the contents of the cache are validated by checking
//	**      the 'change-counter' field of the database file header and
//	**      discarded if they are found to be invalid.
//	**
//	**   2) If the pager is running in exclusive-mode, and there are currently
//	**      no outstanding references to any pages, and is in the error state,
//	**      then an attempt is made to clear the error state by discarding
//	**      the contents of the page cache and rolling back any open journal
//	**      file.
//	**
//	** If everything is successful, SQLITE_OK is returned. If an IO error
//	** occurs while locking the database, checking for a hot-journal file or
//	** rolling back a journal file, the IO error code is returned.
//	*/
func _sqlite3PagerSharedLock(tls *libc.TLS, pPager uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var f, rc int32
	var pVfs uintptr
	var _ /* bExists at bp+4 */ int32
	var _ /* bHotJournal at bp+0 */ int32
	var _ /* dbFileVers at bp+12 */ [16]int8
	var _ /* fout at bp+8 */ int32
	_, _, _ = f, pVfs, rc
	rc = SQLITE_OK /* Return code */
	/* This routine is only called from b-tree and only when there are no
	 ** outstanding pages. This implies that the pager state should either
	 ** be OPEN or READER. READER is only possible if the pager is or was in
	 ** exclusive access mode.  */
	if !((*TPager)(unsafe.Pointer(pPager)).FpWal != libc.UintptrFromInt32(0)) && int32((*TPager)(unsafe.Pointer(pPager)).FeState) == PAGER_OPEN {
		**(**int32)(__ccgo_up(bp)) = int32(1) /* True if there exists a hot journal-file */
		rc = _pager_wait_on_lock(tls, pPager, int32(SHARED_LOCK))
		if rc != SQLITE_OK {
			goto failed
		}
		/* If a journal file exists, and there is no RESERVED lock on the
		 ** database file, then it either needs to be played back or deleted.
		 */
		if int32((*TPager)(unsafe.Pointer(pPager)).FeLock) <= int32(SHARED_LOCK) {
			rc = _hasHotJournal(tls, pPager, bp)
		}
		if rc != SQLITE_OK {
			goto failed
		}
		if **(**int32)(__ccgo_up(bp)) != 0 {
			if (*TPager)(unsafe.Pointer(pPager)).FreadOnly != 0 {
				rc = libc.Int32FromInt32(SQLITE_READONLY) | libc.Int32FromInt32(3)<<libc.Int32FromInt32(8)
				goto failed
			}
			/* Get an EXCLUSIVE lock on the database file. At this point it is
			 ** important that a RESERVED lock is not obtained on the way to the
			 ** EXCLUSIVE lock. If it were, another process might open the
			 ** database file, detect the RESERVED lock, and conclude that the
			 ** database is safe to read while this process is still rolling the
			 ** hot-journal back.
			 **
			 ** Because the intermediate RESERVED lock is not requested, any
			 ** other process attempting to access the database file will get to
			 ** this point in the code and fail to obtain its own EXCLUSIVE lock
			 ** on the database file.
			 **
			 ** Unless the pager is in locking_mode=exclusive mode, the lock is
			 ** downgraded to SHARED_LOCK before this function returns.
			 */
			rc = _pagerLockDb(tls, pPager, int32(EXCLUSIVE_LOCK))
			if rc != SQLITE_OK {
				goto failed
			}
			/* If it is not already open and the file exists on disk, open the
			 ** journal for read/write access. Write access is required because
			 ** in exclusive-access mode the file descriptor will be kept open
			 ** and possibly used for a transaction later on. Also, write-access
			 ** is usually required to finalize the journal in journal_mode=persist
			 ** mode (and also for journal_mode=truncate on some systems).
			 **
			 ** If the journal does not exist, it usually means that some
			 ** other connection managed to get in and roll it back before
			 ** this connection obtained the exclusive lock above. Or, it
			 ** may mean that the pager was in the error-state when this
			 ** function was called and the journal file does not exist.
			 */
			if !((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != libc.UintptrFromInt32(0)) && int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) != int32(PAGER_JOURNALMODE_OFF) {
				pVfs = (*TPager)(unsafe.Pointer(pPager)).FpVfs /* True if journal file exists */
				rc = _sqlite3OsAccess(tls, pVfs, (*TPager)(unsafe.Pointer(pPager)).FzJournal, SQLITE_ACCESS_EXISTS, bp+4)
				if rc == SQLITE_OK && **(**int32)(__ccgo_up(bp + 4)) != 0 {
					**(**int32)(__ccgo_up(bp + 8)) = 0
					f = libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_MAIN_JOURNAL)
					rc = _sqlite3OsOpen(tls, pVfs, (*TPager)(unsafe.Pointer(pPager)).FzJournal, (*TPager)(unsafe.Pointer(pPager)).Fjfd, f, bp+8)
					if rc == SQLITE_OK && **(**int32)(__ccgo_up(bp + 8))&int32(SQLITE_OPEN_READONLY) != 0 {
						rc = _sqlite3CantopenError(tls, int32(65020))
						_sqlite3OsClose(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd)
					}
				}
			}
			/* Playback and delete the journal.  Drop the database write
			 ** lock and reacquire the read lock. Purge the cache before
			 ** playing back the hot-journal so that we don't end up with
			 ** an inconsistent cache.  Sync the hot journal before playing
			 ** it back since the process that crashed and left the hot journal
			 ** probably did not sync it and we are required to always sync
			 ** the journal before playing it back.
			 */
			if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) {
				rc = _pagerSyncHotJournal(tls, pPager)
				if rc == SQLITE_OK {
					rc = _pager_playback(tls, pPager, libc.BoolInt32(!((*TPager)(unsafe.Pointer(pPager)).FtempFile != 0)))
					(*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_OPEN)
				}
			} else {
				if !((*TPager)(unsafe.Pointer(pPager)).FexclusiveMode != 0) {
					_pagerUnlockDb(tls, pPager, int32(SHARED_LOCK))
				}
			}
			if rc != SQLITE_OK {
				/* This branch is taken if an error occurs while trying to open
				 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
				 ** pager_unlock() routine will be called before returning to unlock
				 ** the file. If the unlock attempt fails, then Pager.eLock must be
				 ** set to UNKNOWN_LOCK (see the comment above the #define for
				 ** UNKNOWN_LOCK above for an explanation).
				 **
				 ** In order to get pager_unlock() to do this, set Pager.eState to
				 ** PAGER_ERROR now. This is not actually counted as a transition
				 ** to ERROR state in the state diagram at the top of this file,
				 ** since we know that the same call to pager_unlock() will very
				 ** shortly transition the pager object to the OPEN state. Calling
				 ** assert_pager_state() would fail now, as it should not be possible
				 ** to be in ERROR state when there are zero outstanding page
				 ** references.
				 */
				_pager_error(tls, pPager, rc)
				goto failed
			}
		}
		if !((*TPager)(unsafe.Pointer(pPager)).FtempFile != 0) && (*TPager)(unsafe.Pointer(pPager)).FhasHeldSharedLock != 0 {
			rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, bp+12, int32(16), int64(24))
			if rc != SQLITE_OK {
				if rc != libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8) {
					goto failed
				}
				libc.Xmemset(tls, bp+12, 0, uint32(16))
			}
			if libc.Xmemcmp(tls, pPager+112, bp+12, uint32(16)) != 0 {
				_pager_reset(tls, pPager)
				/* Unmap the database file. It is possible that external processes
				 ** may have truncated the database file and then extended it back
				 ** to its original size while this process was not holding a lock.
				 ** In this case there may exist a Pager.pMap mapping that appears
				 ** to be the right size but is not actually valid. Avoid this
				 ** possibility by unmapping the db here. */
				if (*TPager)(unsafe.Pointer(pPager)).FbUseFetch != 0 {
					_sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, 0, uintptr(0))
				}
			}
		}
		/* If there is a WAL file in the file-system, open this database in WAL
		 ** mode. Otherwise, the following function call is a no-op.
		 */
		rc = _pagerOpenWalIfPresent(tls, pPager)
	}
	if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) {
		rc = _pagerBeginReadTransaction(tls, pPager)
	}
	if int32((*TPager)(unsafe.Pointer(pPager)).FtempFile) == 0 && int32((*TPager)(unsafe.Pointer(pPager)).FeState) == PAGER_OPEN && rc == SQLITE_OK {
		rc = _pagerPagecount(tls, pPager, pPager+28)
	}
	goto failed
failed:
	;
	if rc != SQLITE_OK {
		_pager_unlock(tls, pPager)
	} else {
		(*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_READER)
		(*TPager)(unsafe.Pointer(pPager)).FhasHeldSharedLock = uint8(1)
	}
	return rc
}

// C documentation
//
//	/*
//	** Turn bulk memory into a valid Parse object and link that Parse object
//	** into database connection db.
//	**
//	** Call sqlite3ParseObjectReset() to undo this operation.
//	**
//	** Caution:  Do not confuse this routine with sqlite3ParseObjectInit() which
//	** is generated by Lemon.
//	*/
func _sqlite3ParseObjectInit(tls *libc.TLS, pParse uintptr, db uintptr) {
	libc.Xmemset(tls, pParse+uintptr(uint32(libc.UintptrFromInt32(0)+4)), 0, uint32(libc.UintptrFromInt32(0)+136)-uint32(libc.UintptrFromInt32(0)+4))
	libc.Xmemset(tls, pParse+uintptr(uint32(libc.UintptrFromInt32(0)+208)), 0, libc.Uint32FromInt64(284)-uint32(libc.UintptrFromInt32(0)+208))
	(*TParse)(unsafe.Pointer(pParse)).FpOuterParse = (*Tsqlite3)(unsafe.Pointer(db)).FpParse
	(*Tsqlite3)(unsafe.Pointer(db)).FpParse = pParse
	(*TParse)(unsafe.Pointer(pParse)).Fdb = db
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1681, 0)
	}
}

/*
** Maximum number of times that we will try again to prepare a statement
** that returns SQLITE_ERROR_RETRY.
 */

// C documentation
//
//	/*
//	** This function is used to parse both URIs and non-URI filenames passed by the
//	** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
//	** URIs specified as part of ATTACH statements.
//	**
//	** The first argument to this function is the name of the VFS to use (or
//	** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
//	** query parameter. The second argument contains the URI (or non-URI filename)
//	** itself. When this function is called the *pFlags variable should contain
//	** the default flags to open the database handle with. The value stored in
//	** *pFlags may be updated before returning if the URI filename contains
//	** "cache=xxx" or "mode=xxx" query parameters.
//	**
//	** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
//	** the VFS that should be used to open the database file. *pzFile is set to
//	** point to a buffer containing the name of the file to open.  The value
//	** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter()
//	** and is in the same format as names created using sqlite3_create_filename().
//	** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on
//	** the value returned in *pzFile to avoid a memory leak.
//	**
//	** If an error occurs, then an SQLite error code is returned and *pzErrMsg
//	** may be set to point to a buffer containing an English language error
//	** message. It is the responsibility of the caller to eventually release
//	** this buffer by calling sqlite3_free().
//	*/
func _sqlite3ParseUri(tls *libc.TLS, zDefaultVfs uintptr, zUri uintptr, pFlags uintptr, ppVfs uintptr, pzFile uintptr, pzErrMsg uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aMode, z, zFile, zModeType, zOpt, zVal, zVfs uintptr
	var c, v2 int8
	var eState, i, limit, mask, mode, octet, rc int32
	var flags uint32
	var iIn, iOut, nOpt, nUri, nVal, v3, v4 Ti64
	var nByte Tu64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aMode, c, eState, flags, i, iIn, iOut, limit, mask, mode, nByte, nOpt, nUri, nVal, octet, rc, z, zFile, zModeType, zOpt, zVal, zVfs, v2, v3, v4
	rc = SQLITE_OK
	flags = **(**uint32)(__ccgo_up(pFlags))
	zVfs = zDefaultVfs
	nUri = int64(libc.Xstrlen(tls, zUri))
	if (flags&uint32(SQLITE_OPEN_URI) != 0 || libc.AtomicLoadNUint8(uintptr(unsafe.Pointer(&_sqlite3Config))+6, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0) && nUri >= int64(5) && libc.Xmemcmp(tls, zUri, __ccgo_ts+27348, uint32(5)) == 0 { /* Input character index */
		iOut = 0                        /* Output character index */
		nByte = uint64(nUri + int64(8)) /* Bytes of space to allocate */
		/* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
		 ** method that there may be extra parameters following the file-name.  */
		flags = flags | uint32(SQLITE_OPEN_URI)
		iIn = 0
		for {
			if !(iIn < nUri) {
				break
			}
			nByte = nByte + libc.BoolUint64(int32(**(**int8)(__ccgo_up(zUri + uintptr(iIn)))) == libc.Int32FromUint8('&'))
			goto _1
		_1:
			;
			iIn = iIn + 1
		}
		zFile = Xsqlite3_malloc64(tls, nByte)
		if !(zFile != 0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, zFile, 0, uint32(4)) /* 4-byte of 0x00 is the start of DB name marker */
		zFile = zFile + uintptr(4)
		iIn = int64(5)
		/* Discard the scheme and authority segments of the URI. */
		if int32(**(**int8)(__ccgo_up(zUri + 5))) == int32('/') && int32(**(**int8)(__ccgo_up(zUri + 6))) == int32('/') {
			iIn = int64(7)
			for **(**int8)(__ccgo_up(zUri + uintptr(iIn))) != 0 && int32(**(**int8)(__ccgo_up(zUri + uintptr(iIn)))) != int32('/') {
				iIn = iIn + 1
			}
			if iIn != int64(7) && (iIn != int64(16) || libc.Xmemcmp(tls, __ccgo_ts+27354, zUri+7, uint32(9)) != 0) {
				**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+27364, libc.VaList(bp+8, int32(iIn-libc.Int64FromInt32(7)), zUri+7))
				rc = int32(SQLITE_ERROR)
				goto parse_uri_out
			}
		}
		/* Copy the filename and any query parameters into the zFile buffer.
		 ** Decode %HH escape codes along the way.
		 **
		 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
		 ** on the parsing context. As follows:
		 **
		 **   0: Parsing file-name.
		 **   1: Parsing name section of a name=value query parameter.
		 **   2: Parsing value section of a name=value query parameter.
		 */
		eState = 0
		for {
			v2 = **(**int8)(__ccgo_up(zUri + uintptr(iIn)))
			c = v2
			if !(int32(v2) != 0 && int32(c) != int32('#')) {
				break
			}
			iIn = iIn + 1
			if int32(c) == int32('%') && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zUri + uintptr(iIn))))])&int32(0x08) != 0 && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zUri + uintptr(iIn+int64(1)))))])&int32(0x08) != 0 {
				v3 = iIn
				iIn = iIn + 1
				octet = int32(_sqlite3HexToInt(tls, int32(**(**int8)(__ccgo_up(zUri + uintptr(v3)))))) << int32(4)
				v4 = iIn
				iIn = iIn + 1
				octet = octet + int32(_sqlite3HexToInt(tls, int32(**(**int8)(__ccgo_up(zUri + uintptr(v4))))))
				if octet == 0 {
					/* This branch is taken when "%00" appears within the URI. In this
					 ** case we ignore all text in the remainder of the path, name or
					 ** value currently being parsed. So ignore the current character
					 ** and skip to the next "?", "=" or "&", as appropriate. */
					for {
						v2 = **(**int8)(__ccgo_up(zUri + uintptr(iIn)))
						c = v2
						if !(int32(v2) != 0 && int32(c) != int32('#') && (eState != 0 || int32(c) != int32('?')) && (eState != int32(1) || int32(c) != int32('=') && int32(c) != int32('&')) && (eState != int32(2) || int32(c) != int32('&'))) {
							break
						}
						iIn = iIn + 1
					}
					continue
				}
				c = int8(octet)
			} else {
				if eState == int32(1) && (int32(c) == int32('&') || int32(c) == int32('=')) {
					if int32(**(**int8)(__ccgo_up(zFile + uintptr(iOut-int64(1))))) == 0 {
						/* An empty option name. Ignore this option altogether. */
						for **(**int8)(__ccgo_up(zUri + uintptr(iIn))) != 0 && int32(**(**int8)(__ccgo_up(zUri + uintptr(iIn)))) != int32('#') && int32(**(**int8)(__ccgo_up(zUri + uintptr(iIn-int64(1))))) != int32('&') {
							iIn = iIn + 1
						}
						continue
					}
					if int32(c) == int32('&') {
						v3 = iOut
						iOut = iOut + 1
						**(**int8)(__ccgo_up(zFile + uintptr(v3))) = int8('\000')
					} else {
						eState = int32(2)
					}
					c = 0
				} else {
					if eState == 0 && int32(c) == int32('?') || eState == int32(2) && int32(c) == int32('&') {
						c = 0
						eState = int32(1)
					}
				}
			}
			v3 = iOut
			iOut = iOut + 1
			**(**int8)(__ccgo_up(zFile + uintptr(v3))) = c
		}
		if eState == int32(1) {
			v3 = iOut
			iOut = iOut + 1
			**(**int8)(__ccgo_up(zFile + uintptr(v3))) = int8('\000')
		}
		libc.Xmemset(tls, zFile+uintptr(iOut), 0, uint32(4)) /* end-of-options + empty journal filenames */
		/* Check if there were any options specified that should be interpreted
		 ** here. Options that are interpreted here include "vfs" and those that
		 ** correspond to flags that may be passed to the sqlite3_open_v2()
		 ** method. */
		zOpt = zFile + uintptr(libc.Xstrlen(tls, zFile)+uint32(1))
		for **(**int8)(__ccgo_up(zOpt)) != 0 {
			nOpt = int64(libc.Xstrlen(tls, zOpt))
			zVal = zOpt + uintptr(nOpt+int64(1))
			nVal = int64(libc.Xstrlen(tls, zVal))
			if nOpt == int64(3) && libc.Xmemcmp(tls, __ccgo_ts+27392, zOpt, uint32(3)) == 0 {
				zVfs = zVal
			} else {
				aMode = uintptr(0)
				zModeType = uintptr(0)
				mask = 0
				limit = 0
				if nOpt == int64(5) && libc.Xmemcmp(tls, __ccgo_ts+27396, zOpt, uint32(5)) == 0 {
					mask = libc.Int32FromInt32(SQLITE_OPEN_SHAREDCACHE) | libc.Int32FromInt32(SQLITE_OPEN_PRIVATECACHE)
					aMode = uintptr(unsafe.Pointer(&_aCacheMode))
					limit = mask
					zModeType = __ccgo_ts + 27396
				}
				if nOpt == int64(4) && libc.Xmemcmp(tls, __ccgo_ts+27417, zOpt, uint32(4)) == 0 {
					mask = libc.Int32FromInt32(SQLITE_OPEN_READONLY) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_MEMORY)
					aMode = uintptr(unsafe.Pointer(&_aOpenMode))
					limit = int32(uint32(mask) & flags)
					zModeType = __ccgo_ts + 27432
				}
				if aMode != 0 {
					mode = 0
					i = 0
					for {
						if !((**(**struct {
							Fz    uintptr
							Fmode int32
						})(__ccgo_up(aMode + uintptr(i)*8))).Fz != 0) {
							break
						}
						z = (**(**struct {
							Fz    uintptr
							Fmode int32
						})(__ccgo_up(aMode + uintptr(i)*8))).Fz
						if nVal == int64(libc.Xstrlen(tls, z)) && 0 == libc.Xmemcmp(tls, zVal, z, uint32(nVal)) {
							mode = (**(**struct {
								Fz    uintptr
								Fmode int32
							})(__ccgo_up(aMode + uintptr(i)*8))).Fmode
							break
						}
						goto _9
					_9:
						;
						i = i + 1
					}
					if mode == 0 {
						**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+27439, libc.VaList(bp+8, zModeType, zVal))
						rc = int32(SQLITE_ERROR)
						goto parse_uri_out
					}
					if mode & ^libc.Int32FromInt32(SQLITE_OPEN_MEMORY) > limit {
						**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+27459, libc.VaList(bp+8, zModeType, zVal))
						rc = int32(SQLITE_PERM)
						goto parse_uri_out
					}
					flags = flags&uint32(^mask) | uint32(mode)
				}
			}
			zOpt = zVal + uintptr(nVal+int64(1))
		}
	} else {
		zFile = Xsqlite3_malloc64(tls, uint64(nUri+int64(8)))
		if !(zFile != 0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, zFile, 0, uint32(4))
		zFile = zFile + uintptr(4)
		if nUri != 0 {
			libc.Xmemcpy(tls, zFile, zUri, uint32(nUri))
		}
		libc.Xmemset(tls, zFile+uintptr(nUri), 0, uint32(4))
		flags = flags & uint32(^libc.Int32FromInt32(SQLITE_OPEN_URI))
	}
	**(**uintptr)(__ccgo_up(ppVfs)) = Xsqlite3_vfs_find(tls, zVfs)
	if **(**uintptr)(__ccgo_up(ppVfs)) == uintptr(0) {
		**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+27483, libc.VaList(bp+8, zVfs))
		rc = int32(SQLITE_ERROR)
	}
	goto parse_uri_out
parse_uri_out:
	;
	if rc != SQLITE_OK {
		Xsqlite3_free_filename(tls, zFile)
		zFile = uintptr(0)
	}
	**(**uint32)(__ccgo_up(pFlags)) = flags
	**(**uintptr)(__ccgo_up(pzFile)) = zFile
	return rc
}

// C documentation
//
//	/* The main parser program.
//	** The first argument is a pointer to a structure obtained from
//	** "sqlite3ParserAlloc" which describes the current state of the parser.
//	** The second argument is the major token number.  The third is
//	** the minor token.  The fourth optional argument is whatever the
//	** user wants (and specified in the grammar) and is available for
//	** use by the action routines.
//	**
//	** Inputs:
//	** <ul>
//	** <li> A pointer to the parser (an opaque structure.)
//	** <li> The major token number.
//	** <li> The minor token number.
//	** <li> An option argument of a grammar-specified type.
//	** </ul>
//	**
//	** Outputs:
//	** None.
//	*/
func _sqlite3Parser(tls *libc.TLS, yyp uintptr, yymajor int32, yyminor TToken) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pParse, yypParser uintptr
	var yyact uint16
	var yyruleno uint32
	var _ /* yyminorunion at bp+0 */ TYYMINORTYPE
	_, _, _, _ = pParse, yyact, yypParser, yyruleno /* The parser action. */
	yypParser = yyp                                 /* The parser */
	pParse = (*TyyParser)(unsafe.Pointer(yypParser)).FpParse
	yyact = (*TyyStackEntry)(unsafe.Pointer((*TyyParser)(unsafe.Pointer(yypParser)).Fyytos)).Fstateno
	for int32(1) != 0 { /* Exit by "break" */
		yyact = _yy_find_shift_action(tls, uint16(yymajor), yyact)
		if int32(yyact) >= int32(YY_MIN_REDUCE) {
			yyruleno = uint32(int32(yyact) - int32(YY_MIN_REDUCE)) /* Reduce by this rule */
			/* Check that the stack is large enough to grow by a single entry
			 ** if the RHS of the rule is empty.  This ensures that there is room
			 ** enough on the stack to push the LHS value */
			if int32(_yyRuleInfoNRhs[yyruleno]) == 0 {
				if (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos >= (*TyyParser)(unsafe.Pointer(yypParser)).FyystackEnd {
					if _yyGrowStack(tls, yypParser) != 0 {
						_yyStackOverflow(tls, yypParser)
						break
					}
				}
			}
			yyact = _yy_reduce(tls, yypParser, yyruleno, yymajor, yyminor, pParse)
		} else {
			if int32(yyact) <= int32(YY_MAX_SHIFTREDUCE) {
				_yy_shift(tls, yypParser, yyact, uint16(yymajor), yyminor)
				break
			} else {
				if int32(yyact) == int32(YY_ACCEPT_ACTION) {
					(*TyyParser)(unsafe.Pointer(yypParser)).Fyytos -= 12
					_yy_accept(tls, yypParser)
					return
				} else {
					*(*TToken)(unsafe.Pointer(bp)) = yyminor
					/* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
					 ** do any kind of error recovery.  Instead, simply invoke the syntax
					 ** error routine and continue going as if nothing had happened.
					 **
					 ** Applications can set this macro (for example inside %include) if
					 ** they intend to abandon the parse upon the first syntax error seen.
					 */
					_yy_syntax_error(tls, yypParser, yymajor, yyminor)
					_yy_destructor(tls, yypParser, uint16(yymajor), bp)
					break
				}
			}
		}
	}
	return
}

// C documentation
//
//	/*
//	** Clear all secondary memory allocations from the parser
//	*/
func _sqlite3ParserFinalize(tls *libc.TLS, p uintptr) {
	var pParser, yytos uintptr
	_, _ = pParser, yytos
	pParser = p
	/* In-lined version of calling yy_pop_parser_stack() for each
	 ** element left in the stack */
	yytos = (*TyyParser)(unsafe.Pointer(pParser)).Fyytos
	for yytos > (*TyyParser)(unsafe.Pointer(pParser)).Fyystack {
		if int32((*TyyStackEntry)(unsafe.Pointer(yytos)).Fmajor) >= int32(YY_MIN_DSTRCTR) {
			_yy_destructor(tls, pParser, (*TyyStackEntry)(unsafe.Pointer(yytos)).Fmajor, yytos+4)
		}
		yytos -= 12
	}
	if (*TyyParser)(unsafe.Pointer(pParser)).Fyystack != pParser+16 {
		_parserStackFree(tls, (*TyyParser)(unsafe.Pointer(pParser)).Fyystack, (*TyyParser)(unsafe.Pointer(pParser)).FpParse)
	}
}

/*
** Return the peak depth of the stack for a parser.
 */

/* This array of booleans keeps track of the parser statement
** coverage.  The element yycoverage[X][Y] is set when the parser
** is in state X and has a lookahead token Y.  In a well-tested
** systems, every element of this matrix should end up being set.
 */

/*
** Write into out a description of every state/lookahead combination that
**
**   (1)  has not been used by the parser, and
**   (2)  is not a syntax error.
**
** Return the number of missed state/lookahead combinations.
 */

// C documentation
//
//	/*
//	** Make sure the page is marked as dirty. If it isn't dirty already,
//	** make it so.
//	*/
func _sqlite3PcacheMakeDirty(tls *libc.TLS, p uintptr) {
	var v1 uintptr
	_ = v1
	if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(PGHDR_CLEAN)|libc.Int32FromInt32(PGHDR_DONT_WRITE)) != 0 { /*OPTIMIZATION-IF-FALSE*/
		v1 = p + 28
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(PGHDR_DONT_WRITE))
		if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&int32(PGHDR_CLEAN) != 0 {
			v1 = p + 28
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) ^ (libc.Int32FromInt32(PGHDR_DIRTY) | libc.Int32FromInt32(PGHDR_CLEAN)))
			_pcacheManageDirtyList(tls, p, uint8(PCACHE_DIRTYLIST_ADD))
		}
	}
}

// C documentation
//
//	/*
//	** Create a new PCache object. Storage space to hold the object
//	** has already been allocated and is passed in as the p pointer.
//	** The caller discovers how much space needs to be allocated by
//	** calling sqlite3PcacheSize().
//	**
//	** szExtra is some extra space allocated for each page.  The first
//	** 8 bytes of the extra space will be zeroed as the page is allocated,
//	** but remaining content will be uninitialized.  Though it is opaque
//	** to this module, the extra space really ends up being the MemPage
//	** structure in the pager.
//	*/
func _sqlite3PcacheOpen(tls *libc.TLS, szPage int32, szExtra int32, bPurgeable int32, __ccgo_fp_xStress uintptr, pStress uintptr, p uintptr) (r int32) {
	libc.Xmemset(tls, p, 0, uint32(56))
	(*TPCache)(unsafe.Pointer(p)).FszPage = int32(1)
	(*TPCache)(unsafe.Pointer(p)).FszExtra = szExtra
	/* First 8 bytes will be zeroed */
	(*TPCache)(unsafe.Pointer(p)).FbPurgeable = uint8(bPurgeable)
	(*TPCache)(unsafe.Pointer(p)).FeCreate = uint8(2)
	(*TPCache)(unsafe.Pointer(p)).FxStress = __ccgo_fp_xStress
	(*TPCache)(unsafe.Pointer(p)).FpStress = pStress
	(*TPCache)(unsafe.Pointer(p)).FszCache = int32(100)
	(*TPCache)(unsafe.Pointer(p)).FszSpill = int32(1)
	return _sqlite3PcacheSetPageSize(tls, p, szPage)
}

// C documentation
//
//	/*
//	** Decrement the reference count on a page. If the page is clean and the
//	** reference count drops to 0, then it is made eligible for recycling.
//	*/
func _sqlite3PcacheRelease(tls *libc.TLS, p uintptr) {
	var v1 Ti64
	var v2 uintptr
	_, _ = v1, v2
	(*TPCache)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(p)).FpCache)).FnRefSum = (*TPCache)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(p)).FpCache)).FnRefSum - 1
	v2 = p + 32
	*(*Ti64)(unsafe.Pointer(v2)) = *(*Ti64)(unsafe.Pointer(v2)) - 1
	v1 = *(*Ti64)(unsafe.Pointer(v2))
	if v1 == 0 {
		if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&int32(PGHDR_CLEAN) != 0 {
			_pcacheUnpin(tls, p)
		} else {
			_pcacheManageDirtyList(tls, p, uint8(PCACHE_DIRTYLIST_FRONT))
		}
	}
}

// C documentation
//
//	/*
//	** Change the page size for PCache object. The caller must ensure that there
//	** are no outstanding page references when this function is called.
//	*/
func _sqlite3PcacheSetPageSize(tls *libc.TLS, pCache uintptr, szPage int32) (r int32) {
	var pNew uintptr
	_ = pNew
	if (*TPCache)(unsafe.Pointer(pCache)).FszPage != 0 {
		pNew = (*(*func(*libc.TLS, int32, int32, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxCreate})))(tls, szPage, int32(uint32((*TPCache)(unsafe.Pointer(pCache)).FszExtra)+(libc.Uint32FromInt64(48)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))), int32((*TPCache)(unsafe.Pointer(pCache)).FbPurgeable))
		if pNew == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		(*(*func(*libc.TLS, uintptr, int32))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxCachesize})))(tls, pNew, _numberOfCachePages(tls, pCache))
		if (*TPCache)(unsafe.Pointer(pCache)).FpCache != 0 {
			(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxDestroy})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache)
		}
		(*TPCache)(unsafe.Pointer(pCache)).FpCache = pNew
		(*TPCache)(unsafe.Pointer(pCache)).FszPage = szPage
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Drop every cache entry whose page number is greater than "pgno". The
//	** caller must ensure that there are no outstanding references to any pages
//	** other than page 1 with a page number greater than pgno.
//	**
//	** If there is a reference to page 1 and the pgno parameter passed to this
//	** function is 0, then the data area associated with page 1 is zeroed, but
//	** the page object is not dropped.
//	*/
func _sqlite3PcacheTruncate(tls *libc.TLS, pCache uintptr, pgno TPgno) {
	var p, pNext, pPage1 uintptr
	_, _, _ = p, pNext, pPage1
	if (*TPCache)(unsafe.Pointer(pCache)).FpCache != 0 {
		p = (*TPCache)(unsafe.Pointer(pCache)).FpDirty
		for {
			if !(p != 0) {
				break
			}
			pNext = (*TPgHdr)(unsafe.Pointer(p)).FpDirtyNext
			/* This routine never gets call with a positive pgno except right
			 ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
			 ** it must be that pgno==0.
			 */
			if (*TPgHdr)(unsafe.Pointer(p)).Fpgno > pgno {
				_sqlite3PcacheMakeClean(tls, p)
			}
			goto _1
		_1:
			;
			p = pNext
		}
		if pgno == uint32(0) && (*TPCache)(unsafe.Pointer(pCache)).FnRefSum != 0 {
			pPage1 = (*(*func(*libc.TLS, uintptr, uint32, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxFetch})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache, uint32(1), 0)
			if pPage1 != 0 { /* Page 1 is always available in cache, because
				 ** pCache->nRefSum>0 */
				libc.Xmemset(tls, (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage1)).FpBuf, 0, uint32((*TPCache)(unsafe.Pointer(pCache)).FszPage))
				pgno = uint32(1)
			}
		}
		(*(*func(*libc.TLS, uintptr, uint32))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fpcache2.FxTruncate})))(tls, (*TPCache)(unsafe.Pointer(pCache)).FpCache, pgno+uint32(1))
	}
}

// C documentation
//
//	/*
//	** Process a pragma statement.
//	**
//	** Pragmas are of this form:
//	**
//	**      PRAGMA [schema.]id [= value]
//	**
//	** The identifier might also be a string.  The value is a string, and
//	** identifier, or a number.  If minusFlag is true, then the value is
//	** a number that was preceded by a minus sign.
//	**
//	** If the left side is "database.id" then pId1 is the database name
//	** and pId2 is the id.  If the left side is just "id" then pId1 is the
//	** id and pId2 is any empty string.
//	*/
func _sqlite3Pragma(tls *libc.TLS, pParse uintptr, pId1 uintptr, pId2 uintptr, pValue uintptr, minusFlag int32) {
	bp := tls.Alloc(208)
	defer tls.Free(208)
	var a1, a11, addr, addr1, addrCkFault, addrCkOk, addrOk, addrTop, b, bStrict, ckUniq, cnt, doTypeCheck, eAuto, eMode, eMode1, eMode2, i, i1, i10, i2, i3, i4, i5, i6, i7, i8, i9, iAddr, iAddr1, iBt, iCol, iCol1, iCookie, iDb, iDbLast, iEnd, iIdxDb, iLevel, iReg, iTab, iTabCur, iTabDb, iTabDb1, ii, ii1, ii2, ii3, ii4, initNCol, isHidden, isQuick, j2, j3, j4, jmp, jmp2, jmp21, jmp3, jmp4, jmp5, jmp6, jmp61, jmp7, k, k3, kk, label6, labelError, labelOk, loopTop, mx, mxCol, n, nBtree, nCheck, nHidden, nIdx, nIndex, nLimit, p11, p3, p4, r1, r11, r2, rc, regResult, regRow, showInternFunc, size, size1, size2, uniqOk, x1, v2 int32
	var aOp, aOp1, aOp2, aOp3, aOp4, aOp5, aRoot, db, j, j1, k1, k2, k4, p, p1, pBt, pBt1, pBt2, pCheck, pCol, pCol1, pColExpr, pColl, pDb, pEnc, pFK, pFK1, pHash, pIdx, pIdx1, pIdx3, pIdx4, pIdx5, pIdx6, pIdx7, pMod, pObjTab, pPager, pPager1, pParent, pPk, pPk1, pPragma, pPrior, pSchema, pTab, pTab1, pTab10, pTab11, pTab12, pTab2, pTab3, pTab4, pTab5, pTab6, pTab7, pTab8, pTab9, pTbls, pVTab, v, x2, zDb, zErr, zErr1, zErr2, zLeft, zMod, zMode, zOpt, zRet, zRight, zSql, zSubSql, zType, v1, v5 uintptr
	var azOrigin [3]uintptr
	var cnum Ti16
	var enc Tu8
	var iPrior Tsqlite3_int64
	var iRange, szThreshold TLogEst
	var mask Tu64
	var opMask Tu32
	var _ /* N at bp+104 */ Tsqlite3_int64
	var _ /* N at bp+112 */ Tsqlite3_int64
	var _ /* N at bp+120 */ Tsqlite3_int64
	var _ /* N at bp+96 */ Tsqlite3_int64
	var _ /* aFcntl at bp+4 */ [4]uintptr
	var _ /* aiCols at bp+72 */ uintptr
	var _ /* iDataCur at bp+80 */ int32
	var _ /* iIdxCur at bp+84 */ int32
	var _ /* iLimit at bp+32 */ Ti64
	var _ /* iLimit at bp+40 */ int32
	var _ /* jmp3 at bp+92 */ int32
	var _ /* mxErr at bp+76 */ int32
	var _ /* pDfltValue at bp+88 */ uintptr
	var _ /* pDummy at bp+64 */ uintptr
	var _ /* pId at bp+0 */ uintptr
	var _ /* pIdx at bp+68 */ uintptr
	var _ /* res at bp+56 */ int32
	var _ /* res at bp+60 */ int32
	var _ /* size at bp+44 */ int32
	var _ /* sz at bp+48 */ Tsqlite3_int64
	var _ /* x at bp+24 */ Ti64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a1, a11, aOp, aOp1, aOp2, aOp3, aOp4, aOp5, aRoot, addr, addr1, addrCkFault, addrCkOk, addrOk, addrTop, azOrigin, b, bStrict, ckUniq, cnt, cnum, db, doTypeCheck, eAuto, eMode, eMode1, eMode2, enc, i, i1, i10, i2, i3, i4, i5, i6, i7, i8, i9, iAddr, iAddr1, iBt, iCol, iCol1, iCookie, iDb, iDbLast, iEnd, iIdxDb, iLevel, iPrior, iRange, iReg, iTab, iTabCur, iTabDb, iTabDb1, ii, ii1, ii2, ii3, ii4, initNCol, isHidden, isQuick, j, j1, j2, j3, j4, jmp, jmp2, jmp21, jmp3, jmp4, jmp5, jmp6, jmp61, jmp7, k, k1, k2, k3, k4, kk, label6, labelError, labelOk, loopTop, mask, mx, mxCol, n, nBtree, nCheck, nHidden, nIdx, nIndex, nLimit, opMask, p, p1, p11, p3, p4, pBt, pBt1, pBt2, pCheck, pCol, pCol1, pColExpr, pColl, pDb, pEnc, pFK, pFK1, pHash, pIdx, pIdx1, pIdx3, pIdx4, pIdx5, pIdx6, pIdx7, pMod, pObjTab, pPager, pPager1, pParent, pPk, pPk1, pPragma, pPrior, pSchema, pTab, pTab1, pTab10, pTab11, pTab12, pTab2, pTab3, pTab4, pTab5, pTab6, pTab7, pTab8, pTab9, pTbls, pVTab, r1, r11, r2, rc, regResult, regRow, showInternFunc, size, size1, size2, szThreshold, uniqOk, v, x1, x2, zDb, zErr, zErr1, zErr2, zLeft, zMod, zMode, zOpt, zRet, zRight, zSql, zSubSql, zType, v1, v2, v5
	zLeft = uintptr(0)                         /* Nul-terminated UTF-8 string <id> */
	zRight = uintptr(0)                        /* Nul-terminated UTF-8 string <value>, or NULL */
	zDb = uintptr(0)                           /* return value form SQLITE_FCNTL_PRAGMA */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* The specific database being pragmaed */
	v = _sqlite3GetVdbe(tls, pParse)           /* The pragma */
	if v == uintptr(0) {
		return
	}
	_sqlite3VdbeRunOnlyOnce(tls, v)
	(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(2)
	/* Interpret the [schema.] part of the pragma statement. iDb is the
	 ** index of the database this pragma is being applied to in db.aDb[]. */
	iDb = _sqlite3TwoPartName(tls, pParse, pId1, pId2, bp)
	if iDb < 0 {
		return
	}
	pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16
	/* If the temp database has been explicitly named as part of the
	 ** pragma, make sure it is open.
	 */
	if iDb == int32(1) && _sqlite3OpenTempDatabase(tls, pParse) != 0 {
		return
	}
	zLeft = _sqlite3NameFromToken(tls, db, **(**uintptr)(__ccgo_up(bp)))
	if !(zLeft != 0) {
		return
	}
	if minusFlag != 0 {
		zRight = _sqlite3MPrintf(tls, db, __ccgo_ts+20448, libc.VaList(bp+136, pValue))
	} else {
		zRight = _sqlite3NameFromToken(tls, db, pValue)
	}
	if (*TToken)(unsafe.Pointer(pId2)).Fn > uint32(0) {
		v1 = (*TDb)(unsafe.Pointer(pDb)).FzDbSName
	} else {
		v1 = uintptr(0)
	}
	zDb = v1
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_PRAGMA), zLeft, zRight, zDb) != 0 {
		goto pragma_out
	}
	/* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
	 ** connection.  If it returns SQLITE_OK, then assume that the VFS
	 ** handled the pragma and generate a no-op prepared statement.
	 **
	 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
	 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
	 ** object corresponding to the database file to which the pragma
	 ** statement refers.
	 **
	 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
	 ** file control is an array of pointers to strings (char**) in which the
	 ** second element of the array is the name of the pragma and the third
	 ** element is the argument to the pragma or NULL if the pragma has no
	 ** argument.
	 */
	(**(**[4]uintptr)(__ccgo_up(bp + 4)))[0] = uintptr(0)
	(**(**[4]uintptr)(__ccgo_up(bp + 4)))[int32(1)] = zLeft
	(**(**[4]uintptr)(__ccgo_up(bp + 4)))[int32(2)] = zRight
	(**(**[4]uintptr)(__ccgo_up(bp + 4)))[int32(3)] = uintptr(0)
	(*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FnBusy = 0
	rc = Xsqlite3_file_control(tls, db, zDb, int32(SQLITE_FCNTL_PRAGMA), bp+4)
	if rc == SQLITE_OK {
		_sqlite3VdbeSetNumCols(tls, v, int32(1))
		_sqlite3VdbeSetColName(tls, v, 0, COLNAME_NAME, (**(**[4]uintptr)(__ccgo_up(bp + 4)))[0], uintptr(-libc.Int32FromInt32(1)))
		_returnSingleText(tls, v, (**(**[4]uintptr)(__ccgo_up(bp + 4)))[0])
		Xsqlite3_free(tls, (**(**[4]uintptr)(__ccgo_up(bp + 4)))[0])
		goto pragma_out
	}
	if rc != int32(SQLITE_NOTFOUND) {
		if (**(**[4]uintptr)(__ccgo_up(bp + 4)))[0] != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+4729, libc.VaList(bp+136, (**(**[4]uintptr)(__ccgo_up(bp + 4)))[0]))
			Xsqlite3_free(tls, (**(**[4]uintptr)(__ccgo_up(bp + 4)))[0])
		}
		(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
		(*TParse)(unsafe.Pointer(pParse)).Frc = rc
		goto pragma_out
	}
	/* Locate the pragma in the lookup table */
	pPragma = _pragmaLocate(tls, zLeft)
	if pPragma == uintptr(0) {
		/* IMP: R-43042-22504 No error messages are generated if an
		 ** unknown pragma is issued. */
		goto pragma_out
	}
	/* Make sure the database schema is loaded if the pragma requires that */
	if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_NeedSchema) != 0 {
		if _sqlite3ReadSchema(tls, pParse) != 0 {
			goto pragma_out
		}
	}
	/* Register the result column names for pragmas that return results */
	if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_NoColumns) == 0 && (int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_NoColumns1) == 0 || zRight == uintptr(0)) {
		_setPragmaResultColumnNames(tls, v, pPragma)
	}
	/* Jump to the appropriate pragma handler */
	switch int32((*TPragmaName)(unsafe.Pointer(pPragma)).FePragTyp) {
	/*
	 **  PRAGMA [schema.]default_cache_size
	 **  PRAGMA [schema.]default_cache_size=N
	 **
	 ** The first form reports the current persistent setting for the
	 ** page cache size.  The value returned is the maximum number of
	 ** pages in the page cache.  The second form sets both the current
	 ** page cache size value and the persistent page cache size value
	 ** stored in the database file.
	 **
	 ** Older versions of SQLite would set the default cache size to a
	 ** negative number to indicate synchronous=OFF.  These days, synchronous
	 ** is always on by default regardless of the sign of the default cache
	 ** size.  But continue to take the absolute value of the default cache
	 ** size of historical compatibility.
	 */
	case int32(PragTyp_DEFAULT_CACHE_SIZE):
		_sqlite3VdbeUsesBtree(tls, v, iDb)
		if !(zRight != 0) {
			**(**int32)(__ccgo_up(pParse + 48)) += int32(2)
			aOp = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(36)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_getCacheSize)), _iLn3)
			if 0 != 0 {
				break
			}
			(**(**TVdbeOp)(__ccgo_up(aOp))).Fp1 = iDb
			(**(**TVdbeOp)(__ccgo_up(aOp + 1*20))).Fp1 = iDb
			(**(**TVdbeOp)(__ccgo_up(aOp + 6*20))).Fp1 = -int32(2000)
		} else {
			size = _sqlite3AbsInt32(tls, _sqlite3Atoi(tls, zRight))
			_sqlite3BeginWriteOperation(tls, pParse, 0, iDb)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_DEFAULT_CACHE_SIZE), size)
			(*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size = size
			_sqlite3BtreeSetCacheSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size)
		}
		break
		/*
		 **  PRAGMA [schema.]page_size
		 **  PRAGMA [schema.]page_size=N
		 **
		 ** The first form reports the current setting for the
		 ** database page size in bytes.  The second form sets the
		 ** database page size value.  The value can only be set if
		 ** the database has not yet been created.
		 */
		fallthrough
	case int32(PragTyp_PAGE_SIZE):
		pBt = (*TDb)(unsafe.Pointer(pDb)).FpBt
		if !(zRight != 0) {
			if pBt != 0 {
				v2 = _sqlite3BtreeGetPageSize(tls, pBt)
			} else {
				v2 = 0
			}
			size1 = v2
			_returnSingleInt(tls, v, int64(size1))
		} else {
			/* Malloc may fail when setting the page-size, as there is an internal
			 ** buffer that the pager module resizes using sqlite3_realloc().
			 */
			(*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize = _sqlite3Atoi(tls, zRight)
			if int32(SQLITE_NOMEM) == _sqlite3BtreeSetPageSize(tls, pBt, (*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize, 0, 0) {
				_sqlite3OomFault(tls, db)
			}
		}
		break
		/*
		 **  PRAGMA [schema.]secure_delete
		 **  PRAGMA [schema.]secure_delete=ON/OFF/FAST
		 **
		 ** The first form reports the current setting for the
		 ** secure_delete flag.  The second form changes the secure_delete
		 ** flag setting and reports the new value.
		 */
		fallthrough
	case int32(PragTyp_SECURE_DELETE):
		pBt1 = (*TDb)(unsafe.Pointer(pDb)).FpBt
		b = -int32(1)
		if zRight != 0 {
			if Xsqlite3_stricmp(tls, zRight, __ccgo_ts+20452) == 0 {
				b = int32(2)
			} else {
				b = int32(_sqlite3GetBoolean(tls, zRight, uint8(0)))
			}
		}
		if (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) && b >= 0 {
			ii = 0
			for {
				if !(ii < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
					break
				}
				_sqlite3BtreeSecureDelete(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii)*16))).FpBt, b)
				goto _3
			_3:
				;
				ii = ii + 1
			}
		}
		b = _sqlite3BtreeSecureDelete(tls, pBt1, b)
		_returnSingleInt(tls, v, int64(b))
		break
		/*
		 **  PRAGMA [schema.]max_page_count
		 **  PRAGMA [schema.]max_page_count=N
		 **
		 ** The first form reports the current setting for the
		 ** maximum number of pages in the database file.  The
		 ** second form attempts to change this setting.  Both
		 ** forms return the current setting.
		 **
		 ** The absolute value of N is used.  This is undocumented and might
		 ** change.  The only purpose is to provide an easy way to test
		 ** the sqlite3AbsInt32() function.
		 **
		 **  PRAGMA [schema.]page_count
		 **
		 ** Return the number of pages in the specified database.
		 */
		fallthrough
	case int32(PragTyp_PAGE_COUNT):
		**(**Ti64)(__ccgo_up(bp + 24)) = 0
		_sqlite3CodeVerifySchema(tls, pParse, iDb)
		v1 = pParse + 48
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		v2 = *(*int32)(unsafe.Pointer(v1))
		iReg = v2
		if int32(_sqlite3UpperToLower[uint8(**(**int8)(__ccgo_up(zLeft)))]) == int32('p') {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Pagecount), iDb, iReg)
		} else {
			if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+24) == 0 {
				if **(**Ti64)(__ccgo_up(bp + 24)) < 0 {
					**(**Ti64)(__ccgo_up(bp + 24)) = 0
				} else {
					if **(**Ti64)(__ccgo_up(bp + 24)) > libc.Int64FromUint32(0xfffffffe) {
						**(**Ti64)(__ccgo_up(bp + 24)) = libc.Int64FromUint32(0xfffffffe)
					}
				}
			} else {
				**(**Ti64)(__ccgo_up(bp + 24)) = 0
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MaxPgcnt), iDb, iReg, int32(**(**Ti64)(__ccgo_up(bp + 24))))
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), iReg, int32(1))
		break
		/*
		 **  PRAGMA [schema.]locking_mode
		 **  PRAGMA [schema.]locking_mode = (normal|exclusive)
		 */
		fallthrough
	case int32(PragTyp_LOCKING_MODE):
		zRet = __ccgo_ts + 20243
		eMode = _getLockingMode(tls, zRight)
		if (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) && eMode == -int32(1) {
			/* Simple "PRAGMA locking_mode;" statement. This is a query for
			 ** the current default locking mode (which may be different to
			 ** the locking-mode of the main database).
			 */
			eMode = int32((*Tsqlite3)(unsafe.Pointer(db)).FdfltLockMode)
		} else {
			if (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) {
				ii1 = int32(2)
				for {
					if !(ii1 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
						break
					}
					pPager = _sqlite3BtreePager(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii1)*16))).FpBt)
					_sqlite3PagerLockingMode(tls, pPager, eMode)
					goto _6
				_6:
					;
					ii1 = ii1 + 1
				}
				(*Tsqlite3)(unsafe.Pointer(db)).FdfltLockMode = uint8(eMode)
			}
			pPager = _sqlite3BtreePager(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt)
			eMode = _sqlite3PagerLockingMode(tls, pPager, eMode)
		}
		if eMode == int32(PAGER_LOCKINGMODE_EXCLUSIVE) {
			zRet = __ccgo_ts + 5159
		}
		_returnSingleText(tls, v, zRet)
		break
		/*
		 **  PRAGMA [schema.]journal_mode
		 **  PRAGMA [schema.]journal_mode =
		 **                      (delete|persist|off|truncate|memory|wal|off)
		 */
		fallthrough
	case int32(PragTyp_JOURNAL_MODE): /* Loop counter */
		if zRight == uintptr(0) {
			/* If there is no "=MODE" part of the pragma, do a query for the
			 ** current mode */
			eMode1 = -int32(1)
		} else {
			n = _sqlite3Strlen30(tls, zRight)
			eMode1 = 0
			for {
				v1 = _sqlite3JournalModename(tls, eMode1)
				zMode = v1
				if !(v1 != uintptr(0)) {
					break
				}
				if Xsqlite3_strnicmp(tls, zRight, zMode, n) == 0 {
					break
				}
				goto _7
			_7:
				;
				eMode1 = eMode1 + 1
			}
			if !(zMode != 0) {
				/* If the "=MODE" part does not match any known journal mode,
				 ** then do a query */
				eMode1 = -int32(1)
			}
			if eMode1 == int32(PAGER_JOURNALMODE_OFF) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_Defensive) != uint64(0) {
				/* Do not allow journal-mode "OFF" in defensive since the database
				 ** can become corrupted using ordinary SQL when the journal is off */
				eMode1 = -int32(1)
			}
		}
		if eMode1 == -int32(1) && (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) {
			/* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
			iDb = 0
			(*TToken)(unsafe.Pointer(pId2)).Fn = uint32(1)
		}
		ii2 = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1)
		for {
			if !(ii2 >= 0) {
				break
			}
			if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii2)*16))).FpBt != 0 && (ii2 == iDb || (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0)) {
				_sqlite3VdbeUsesBtree(tls, v, ii2)
				_sqlite3VdbeAddOp3(tls, v, int32(OP_JournalMode), ii2, int32(1), eMode1)
			}
			goto _9
		_9:
			;
			ii2 = ii2 - 1
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), int32(1))
		break
		/*
		 **  PRAGMA [schema.]journal_size_limit
		 **  PRAGMA [schema.]journal_size_limit=N
		 **
		 ** Get or set the size limit on rollback journal files.
		 */
		fallthrough
	case int32(PragTyp_JOURNAL_SIZE_LIMIT):
		pPager1 = _sqlite3BtreePager(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt)
		**(**Ti64)(__ccgo_up(bp + 32)) = int64(-int32(2))
		if zRight != 0 {
			_sqlite3DecOrHexToI64(tls, zRight, bp+32)
			if **(**Ti64)(__ccgo_up(bp + 32)) < int64(-int32(1)) {
				**(**Ti64)(__ccgo_up(bp + 32)) = int64(-int32(1))
			}
		}
		**(**Ti64)(__ccgo_up(bp + 32)) = _sqlite3PagerJournalSizeLimit(tls, pPager1, **(**Ti64)(__ccgo_up(bp + 32)))
		_returnSingleInt(tls, v, **(**Ti64)(__ccgo_up(bp + 32)))
		break
		/*
		 **  PRAGMA [schema.]auto_vacuum
		 **  PRAGMA [schema.]auto_vacuum=N
		 **
		 ** Get or set the value of the database 'auto-vacuum' parameter.
		 ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
		 */
		fallthrough
	case int32(PragTyp_AUTO_VACUUM):
		pBt2 = (*TDb)(unsafe.Pointer(pDb)).FpBt
		if !(zRight != 0) {
			_returnSingleInt(tls, v, int64(_sqlite3BtreeGetAutoVacuum(tls, pBt2)))
		} else {
			eAuto = _getAutoVacuum(tls, zRight)
			(*Tsqlite3)(unsafe.Pointer(db)).FnextAutovac = int8(uint8(eAuto))
			/* Call SetAutoVacuum() to set initialize the internal auto and
			 ** incr-vacuum flags. This is required in case this connection
			 ** creates the database file. It is important that it is created
			 ** as an auto-vacuum capable db.
			 */
			rc = _sqlite3BtreeSetAutoVacuum(tls, pBt2, eAuto)
			if rc == SQLITE_OK && (eAuto == int32(1) || eAuto == int32(2)) {
				iAddr = _sqlite3VdbeCurrentAddr(tls, v)
				aOp1 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(20)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_setMeta6)), _iLn11)
				if 0 != 0 {
					break
				}
				(**(**TVdbeOp)(__ccgo_up(aOp1))).Fp1 = iDb
				(**(**TVdbeOp)(__ccgo_up(aOp1 + 1*20))).Fp1 = iDb
				(**(**TVdbeOp)(__ccgo_up(aOp1 + 2*20))).Fp2 = iAddr + int32(4)
				(**(**TVdbeOp)(__ccgo_up(aOp1 + 4*20))).Fp1 = iDb
				(**(**TVdbeOp)(__ccgo_up(aOp1 + 4*20))).Fp3 = eAuto - int32(1)
				_sqlite3VdbeUsesBtree(tls, v, iDb)
			}
		}
		break
		/*
		 **  PRAGMA [schema.]incremental_vacuum(N)
		 **
		 ** Do N steps of incremental vacuuming on a database.
		 */
		fallthrough
	case int32(PragTyp_INCREMENTAL_VACUUM):
		**(**int32)(__ccgo_up(bp + 40)) = 0
		if zRight == uintptr(0) || !(_sqlite3GetInt32(tls, zRight, bp+40) != 0) || **(**int32)(__ccgo_up(bp + 40)) <= 0 {
			**(**int32)(__ccgo_up(bp + 40)) = int32(0x7fffffff)
		}
		_sqlite3BeginWriteOperation(tls, pParse, 0, iDb)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), **(**int32)(__ccgo_up(bp + 40)), int32(1))
		addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_IncrVacuum), iDb)
		_sqlite3VdbeAddOp1(tls, v, int32(OP_ResultRow), int32(1))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), int32(1), -int32(1))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_IfPos), int32(1), addr)
		_sqlite3VdbeJumpHere(tls, v, addr)
		break
		/*
		 **  PRAGMA [schema.]cache_size
		 **  PRAGMA [schema.]cache_size=N
		 **
		 ** The first form reports the current local setting for the
		 ** page cache size. The second form sets the local
		 ** page cache size value.  If N is positive then that is the
		 ** number of pages in the cache.  If N is negative, then the
		 ** number of pages is adjusted so that the cache uses -N kibibytes
		 ** of memory.
		 */
		fallthrough
	case int32(PragTyp_CACHE_SIZE):
		if !(zRight != 0) {
			_returnSingleInt(tls, v, int64((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size))
		} else {
			size2 = _sqlite3Atoi(tls, zRight)
			(*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size = size2
			_sqlite3BtreeSetCacheSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fcache_size)
		}
		break
		/*
		 **  PRAGMA [schema.]cache_spill
		 **  PRAGMA cache_spill=BOOLEAN
		 **  PRAGMA [schema.]cache_spill=N
		 **
		 ** The first form reports the current local setting for the
		 ** page cache spill size. The second form turns cache spill on
		 ** or off.  When turning cache spill on, the size is set to the
		 ** current cache_size.  The third form sets a spill size that
		 ** may be different form the cache size.
		 ** If N is positive then that is the
		 ** number of pages in the cache.  If N is negative, then the
		 ** number of pages is adjusted so that the cache uses -N kibibytes
		 ** of memory.
		 **
		 ** If the number of cache_spill pages is less then the number of
		 ** cache_size pages, no spilling occurs until the page count exceeds
		 ** the number of cache_size pages.
		 **
		 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
		 ** not just the schema specified.
		 */
		fallthrough
	case int32(PragTyp_CACHE_SPILL):
		if !(zRight != 0) {
			if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_CacheSpill) == uint64(0) {
				v2 = 0
			} else {
				v2 = _sqlite3BtreeSetSpillSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, 0)
			}
			_returnSingleInt(tls, v, int64(v2))
		} else {
			**(**int32)(__ccgo_up(bp + 44)) = int32(1)
			if _sqlite3GetInt32(tls, zRight, bp+44) != 0 {
				_sqlite3BtreeSetSpillSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, **(**int32)(__ccgo_up(bp + 44)))
			}
			if _sqlite3GetBoolean(tls, zRight, libc.BoolUint8(**(**int32)(__ccgo_up(bp + 44)) != 0)) != 0 {
				**(**Tu64)(__ccgo_up(db + 32)) |= uint64(SQLITE_CacheSpill)
			} else {
				**(**Tu64)(__ccgo_up(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_CacheSpill)
			}
			_setAllPagerFlags(tls, db)
		}
		break
		/*
		 **  PRAGMA [schema.]mmap_size(N)
		 **
		 ** Used to set mapping size limit. The mapping size limit is
		 ** used to limit the aggregate size of all memory mapped regions of the
		 ** database file. If this parameter is set to zero, then memory mapping
		 ** is not used at all.  If N is negative, then the default memory map
		 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
		 ** The parameter N is measured in bytes.
		 **
		 ** This value is advisory.  The underlying VFS is free to memory map
		 ** as little or as much as it wants.  Except, if N is set to 0 then the
		 ** upper layers will never invoke the xFetch interfaces to the VFS.
		 */
		fallthrough
	case int32(PragTyp_MMAP_SIZE):
		if zRight != 0 {
			_sqlite3DecOrHexToI64(tls, zRight, bp+48)
			if **(**Tsqlite3_int64)(__ccgo_up(bp + 48)) < 0 {
				**(**Tsqlite3_int64)(__ccgo_up(bp + 48)) = _sqlite3Config.FszMmap
			}
			if (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) {
				(*Tsqlite3)(unsafe.Pointer(db)).FszMmap = **(**Tsqlite3_int64)(__ccgo_up(bp + 48))
			}
			ii3 = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1)
			for {
				if !(ii3 >= 0) {
					break
				}
				if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii3)*16))).FpBt != 0 && (ii3 == iDb || (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0)) {
					_sqlite3BtreeSetMmapLimit(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii3)*16))).FpBt, **(**Tsqlite3_int64)(__ccgo_up(bp + 48)))
				}
				goto _11
			_11:
				;
				ii3 = ii3 - 1
			}
		}
		**(**Tsqlite3_int64)(__ccgo_up(bp + 48)) = int64(-int32(1))
		rc = Xsqlite3_file_control(tls, db, zDb, int32(SQLITE_FCNTL_MMAP_SIZE), bp+48)
		if rc == SQLITE_OK {
			_returnSingleInt(tls, v, **(**Tsqlite3_int64)(__ccgo_up(bp + 48)))
		} else {
			if rc != int32(SQLITE_NOTFOUND) {
				(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
				(*TParse)(unsafe.Pointer(pParse)).Frc = rc
			}
		}
		break
		/*
		 **   PRAGMA temp_store
		 **   PRAGMA temp_store = "default"|"memory"|"file"
		 **
		 ** Return or set the local value of the temp_store flag.  Changing
		 ** the local value does not make changes to the disk file and the default
		 ** value will be restored the next time the database is opened.
		 **
		 ** Note that it is possible for the library compile-time options to
		 ** override this setting
		 */
		fallthrough
	case int32(PragTyp_TEMP_STORE):
		if !(zRight != 0) {
			_returnSingleInt(tls, v, int64((*Tsqlite3)(unsafe.Pointer(db)).Ftemp_store))
		} else {
			_changeTempStorage(tls, pParse, zRight)
		}
		break
		/*
		 **   PRAGMA temp_store_directory
		 **   PRAGMA temp_store_directory = ""|"directory_name"
		 **
		 ** Return or set the local value of the temp_store_directory flag.  Changing
		 ** the value sets a specific directory to be used for temporary files.
		 ** Setting to a null string reverts to the default temporary directory search.
		 ** If temporary directory is changed, then invalidateTempStorage.
		 **
		 */
		fallthrough
	case int32(PragTyp_TEMP_STORE_DIRECTORY):
		Xsqlite3_mutex_enter(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)))
		if !(zRight != 0) {
			_returnSingleText(tls, v, Xsqlite3_temp_directory)
		} else {
			if **(**int8)(__ccgo_up(zRight)) != 0 {
				rc = _sqlite3OsAccess(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, zRight, int32(SQLITE_ACCESS_READWRITE), bp+56)
				if rc != SQLITE_OK || **(**int32)(__ccgo_up(bp + 56)) == 0 {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20457, 0)
					Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)))
					goto pragma_out
				}
			}
			if libc.Bool(false) || libc.Bool(true) && int32((*Tsqlite3)(unsafe.Pointer(db)).Ftemp_store) <= int32(1) || libc.Bool(libc.Bool(false) && int32((*Tsqlite3)(unsafe.Pointer(db)).Ftemp_store) == int32(1)) {
				_invalidateTempStorage(tls, pParse)
			}
			Xsqlite3_free(tls, Xsqlite3_temp_directory)
			if **(**int8)(__ccgo_up(zRight)) != 0 {
				Xsqlite3_temp_directory = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+136, zRight))
			} else {
				Xsqlite3_temp_directory = uintptr(0)
			}
		}
		Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)))
		break
		/*
		 **   PRAGMA data_store_directory
		 **   PRAGMA data_store_directory = ""|"directory_name"
		 **
		 ** Return or set the local value of the data_store_directory flag.  Changing
		 ** the value sets a specific directory to be used for database files that
		 ** were specified with a relative pathname.  Setting to a null string reverts
		 ** to the default database directory, which for database files specified with
		 ** a relative path will probably be based on the current directory for the
		 ** process.  Database file specified with an absolute path are not impacted
		 ** by this setting, regardless of its value.
		 **
		 */
		fallthrough
	case int32(PragTyp_DATA_STORE_DIRECTORY):
		Xsqlite3_mutex_enter(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)))
		if !(zRight != 0) {
			_returnSingleText(tls, v, Xsqlite3_data_directory)
		} else {
			if **(**int8)(__ccgo_up(zRight)) != 0 {
				rc = _sqlite3OsAccess(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, zRight, int32(SQLITE_ACCESS_READWRITE), bp+60)
				if rc != SQLITE_OK || **(**int32)(__ccgo_up(bp + 60)) == 0 {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20457, 0)
					Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)))
					goto pragma_out
				}
			}
			Xsqlite3_free(tls, Xsqlite3_data_directory)
			if **(**int8)(__ccgo_up(zRight)) != 0 {
				Xsqlite3_data_directory = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+136, zRight))
			} else {
				Xsqlite3_data_directory = uintptr(0)
			}
		}
		Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)))
		break
		/*
		 **   PRAGMA [schema.]synchronous
		 **   PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
		 **
		 ** Return or set the local value of the synchronous flag.  Changing
		 ** the local value does not make changes to the disk file and the
		 ** default value will be restored the next time the database is
		 ** opened.
		 */
		fallthrough
	case int32(PragTyp_SYNCHRONOUS):
		if !(zRight != 0) {
			_returnSingleInt(tls, v, int64(int32((*TDb)(unsafe.Pointer(pDb)).Fsafety_level)-int32(1)))
		} else {
			if !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0) {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20482, 0)
			} else {
				if iDb != int32(1) {
					iLevel = (int32(_getSafetyLevel(tls, zRight, 0, uint8(1))) + int32(1)) & int32(PAGER_SYNCHRONOUS_MASK)
					if iLevel == 0 {
						iLevel = int32(1)
					}
					(*TDb)(unsafe.Pointer(pDb)).Fsafety_level = uint8(iLevel)
					(*TDb)(unsafe.Pointer(pDb)).FbSyncSet = uint8(1)
					_setAllPagerFlags(tls, db)
				}
			}
		}
	case int32(PragTyp_FLAG):
		if zRight == uintptr(0) {
			_setPragmaResultColumnNames(tls, v, pPragma)
			_returnSingleInt(tls, v, libc.BoolInt64((*Tsqlite3)(unsafe.Pointer(db)).Fflags&(*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != uint64(0)))
		} else {
			mask = (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg /* Mask of bits to set or clear. */
			if int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) == 0 {
				/* Foreign key support may not be enabled or disabled while not
				 ** in auto-commit mode.  */
				mask = mask & uint64(^libc.Int32FromInt32(SQLITE_ForeignKeys))
			}
			if _sqlite3GetBoolean(tls, zRight, uint8(0)) != 0 {
				if mask&uint64(SQLITE_WriteSchema) == uint64(0) || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_Defensive) == uint64(0) {
					**(**Tu64)(__ccgo_up(db + 32)) |= mask
				}
			} else {
				**(**Tu64)(__ccgo_up(db + 32)) &= ^mask
				if mask == uint64(SQLITE_DeferFKs) {
					(*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons = 0
					(*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons = 0
				}
				if mask&uint64(SQLITE_WriteSchema) != uint64(0) && Xsqlite3_stricmp(tls, zRight, __ccgo_ts+20535) == 0 {
					/* IMP: R-60817-01178 If the argument is "RESET" then schema
					 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
					 ** in addition, the schema is reloaded. */
					_sqlite3ResetAllSchemasOfConnection(tls, db)
				}
			}
			/* Many of the flag-pragmas modify the code generated by the SQL
			 ** compiler (eg. count_changes). So add an opcode to expire all
			 ** compiled SQL statements after modifying a pragma value.
			 */
			_sqlite3VdbeAddOp0(tls, v, int32(OP_Expire))
			_setAllPagerFlags(tls, db)
		}
		break
		/*
		 **   PRAGMA table_info(<table>)
		 **
		 ** Return a single row for each column of the named table. The columns of
		 ** the returned data set are:
		 **
		 ** cid:        Column id (numbered from left to right, starting at 0)
		 ** name:       Column name
		 ** type:       Column declaration type.
		 ** notnull:    True if 'NOT NULL' is part of column declaration
		 ** dflt_value: The default value for the column, if any.
		 ** pk:         Non-zero for PK fields.
		 */
		fallthrough
	case int32(PragTyp_TABLE_INFO):
		if zRight != 0 {
			_sqlite3CodeVerifyNamedSchema(tls, pParse, zDb)
			pTab = _sqlite3LocateTable(tls, pParse, uint32(LOCATE_NOERR), zRight, zDb)
			if pTab != 0 {
				nHidden = 0
				pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
				(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(7)
				_sqlite3ViewGetColumnNames(tls, pParse, pTab)
				i = 0
				pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol
				for {
					if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
						break
					}
					isHidden = 0
					if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_NOINSERT) != 0 {
						if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg == uint64(0) {
							nHidden = nHidden + 1
							goto _12
						}
						if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
							isHidden = int32(2) /* GENERATED ALWAYS AS ... VIRTUAL */
						} else {
							if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_STORED) != 0 {
								isHidden = int32(3) /* GENERATED ALWAYS AS ... STORED */
							} else {
								isHidden = int32(1) /* HIDDEN */
							}
						}
					}
					if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_PRIMKEY) == 0 {
						k = 0
					} else {
						if pPk == uintptr(0) {
							k = int32(1)
						} else {
							k = int32(1)
							for {
								if !(k <= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) && int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(k-int32(1))*2))) != i) {
									break
								}
								goto _13
							_13:
								;
								k = k + 1
							}
						}
					}
					pColExpr = _sqlite3ColumnExpr(tls, pTab, pCol)
					if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != 0 {
						v1 = __ccgo_ts + 20541
					} else {
						v1 = __ccgo_ts + 20549
					}
					if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 4))&0xf>>0)) != 0 {
						v2 = int32(1)
					} else {
						v2 = 0
					}
					if isHidden >= int32(2) || pColExpr == uintptr(0) {
						v5 = uintptr(0)
					} else {
						v5 = *(*uintptr)(unsafe.Pointer(pColExpr + 8))
					}
					_sqlite3VdbeMultiLoad(tls, v, int32(1), v1, libc.VaList(bp+136, i-nHidden, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, _sqlite3ColumnType(tls, pCol, __ccgo_ts+1711), v2, v5, k, isHidden))
					goto _12
				_12:
					;
					i = i + 1
					pCol += 12
				}
			}
		}
		break
		/*
		 **   PRAGMA table_list
		 **
		 ** Return a single row for each table, virtual table, or view in the
		 ** entire schema.
		 **
		 ** schema:     Name of attached database hold this table
		 ** name:       Name of the table itself
		 ** type:       "table", "view", "virtual", "shadow"
		 ** ncol:       Number of columns
		 ** wr:         True for a WITHOUT ROWID table
		 ** strict:     True for a STRICT table
		 */
		fallthrough
	case int32(PragTyp_TABLE_LIST):
		(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(6)
		_sqlite3CodeVerifyNamedSchema(tls, pParse, zDb)
		ii4 = 0
		for {
			if !(ii4 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			if zDb != 0 && Xsqlite3_stricmp(tls, zDb, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*16))).FzDbSName) != 0 {
				goto _17
			}
			/* Ensure that the Table.nCol field is initialized for all views
			 ** and virtual tables.  Each time we initialize a Table.nCol value
			 ** for a table, that can potentially disrupt the hash table, so restart
			 ** the initialization scan.
			 */
			pHash = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*16))).FpSchema + 8
			initNCol = int32((*THash)(unsafe.Pointer(pHash)).Fcount)
			for {
				v2 = initNCol
				initNCol = initNCol - 1
				if !(v2 != 0) {
					break
				}
				k1 = (*THash)(unsafe.Pointer(pHash)).Ffirst
				for {
					if !(int32(1) != 0) {
						break
					}
					if k1 == uintptr(0) {
						initNCol = 0
						break
					}
					pTab1 = (*THashElem)(unsafe.Pointer(k1)).Fdata
					if int32((*TTable)(unsafe.Pointer(pTab1)).FnCol) == 0 {
						zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+20556, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab1)).FzName))
						if zSql != 0 {
							**(**uintptr)(__ccgo_up(bp + 64)) = uintptr(0)
							Xsqlite3_prepare_v3(tls, db, zSql, -int32(1), uint32(SQLITE_PREPARE_DONT_LOG), bp+64, uintptr(0))
							Xsqlite3_finalize(tls, **(**uintptr)(__ccgo_up(bp + 64)))
							_sqlite3DbFree(tls, db, zSql)
						}
						if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
							_sqlite3ErrorMsg(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpParse, __ccgo_ts+1681, 0)
							(*TParse)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpParse)).Frc = int32(SQLITE_NOMEM)
						}
						pHash = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*16))).FpSchema + 8
						break
					}
					goto _19
				_19:
					;
					k1 = (*THashElem)(unsafe.Pointer(k1)).Fnext
				}
			}
			k1 = (*THash)(unsafe.Pointer(pHash)).Ffirst
			for {
				if !(k1 != 0) {
					break
				}
				pTab2 = (*THashElem)(unsafe.Pointer(k1)).Fdata
				if zRight != 0 && Xsqlite3_stricmp(tls, zRight, (*TTable)(unsafe.Pointer(pTab2)).FzName) != 0 {
					goto _20
				}
				if int32((*TTable)(unsafe.Pointer(pTab2)).FeTabType) == int32(TABTYP_VIEW) {
					zType = __ccgo_ts + 12332
				} else {
					if int32((*TTable)(unsafe.Pointer(pTab2)).FeTabType) == int32(TABTYP_VTAB) {
						zType = __ccgo_ts + 15517
					} else {
						if (*TTable)(unsafe.Pointer(pTab2)).FtabFlags&uint32(TF_Shadow) != 0 {
							zType = __ccgo_ts + 20572
						} else {
							zType = __ccgo_ts + 10594
						}
					}
				}
				_sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+20579, libc.VaList(bp+136, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*16))).FzDbSName, _sqlite3PreferredTableName(tls, (*TTable)(unsafe.Pointer(pTab2)).FzName), zType, int32((*TTable)(unsafe.Pointer(pTab2)).FnCol), libc.BoolInt32((*TTable)(unsafe.Pointer(pTab2)).FtabFlags&uint32(TF_WithoutRowid) != uint32(0)), libc.BoolInt32((*TTable)(unsafe.Pointer(pTab2)).FtabFlags&uint32(TF_Strict) != uint32(0))))
				goto _20
			_20:
				;
				k1 = (*THashElem)(unsafe.Pointer(k1)).Fnext
			}
			goto _17
		_17:
			;
			ii4 = ii4 + 1
		}
	case int32(PragTyp_INDEX_INFO):
		if zRight != 0 {
			pIdx = _sqlite3FindIndex(tls, db, zRight, zDb)
			if pIdx == uintptr(0) {
				/* If there is no index named zRight, check to see if there is a
				 ** WITHOUT ROWID table named zRight, and if there is, show the
				 ** structure of the PRIMARY KEY index for that table. */
				pTab3 = _sqlite3LocateTable(tls, pParse, uint32(LOCATE_NOERR), zRight, zDb)
				if pTab3 != 0 && !((*TTable)(unsafe.Pointer(pTab3)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
					pIdx = _sqlite3PrimaryKeyIndex(tls, pTab3)
				}
			}
			if pIdx != 0 {
				iIdxDb = _sqlite3SchemaToIndex(tls, db, (*TIndex)(unsafe.Pointer(pIdx)).FpSchema)
				if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != 0 {
					/* PRAGMA index_xinfo (newer version with more rows and columns) */
					mx = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
					(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(6)
				} else {
					/* PRAGMA index_info (legacy version) */
					mx = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
					(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(3)
				}
				pTab3 = (*TIndex)(unsafe.Pointer(pIdx)).FpTable
				_sqlite3CodeVerifySchema(tls, pParse, iIdxDb)
				i1 = 0
				for {
					if !(i1 < mx) {
						break
					}
					cnum = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i1)*2))
					if int32(cnum) < 0 {
						v1 = uintptr(0)
					} else {
						v1 = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab3)).FaCol + uintptr(cnum)*12))).FzCnName
					}
					_sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+20586, libc.VaList(bp+136, i1, int32(cnum), v1))
					if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != 0 {
						_sqlite3VdbeMultiLoad(tls, v, int32(4), __ccgo_ts+20591, libc.VaList(bp+136, int32(**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(i1)))), **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i1)*4)), libc.BoolInt32(i1 < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol))))
					}
					_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), (*TParse)(unsafe.Pointer(pParse)).FnMem)
					goto _21
				_21:
					;
					i1 = i1 + 1
				}
			}
		}
	case int32(PragTyp_INDEX_LIST):
		if zRight != 0 {
			pTab4 = _sqlite3FindTable(tls, db, zRight, zDb)
			if pTab4 != 0 {
				iTabDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab4)).FpSchema)
				(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(5)
				_sqlite3CodeVerifySchema(tls, pParse, iTabDb)
				pIdx1 = (*TTable)(unsafe.Pointer(pTab4)).FpIndex
				i2 = libc.Int32FromInt32(0)
				for {
					if !(pIdx1 != 0) {
						break
					}
					azOrigin = [3]uintptr{
						0: __ccgo_ts + 20596,
						1: __ccgo_ts + 20598,
						2: __ccgo_ts + 19074,
					}
					_sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+20600, libc.VaList(bp+136, i2, (*TIndex)(unsafe.Pointer(pIdx1)).FzName, libc.BoolInt32(int32((*TIndex)(unsafe.Pointer(pIdx1)).FonError) != OE_None), azOrigin[int32(uint32(*(*uint16)(unsafe.Pointer(pIdx1 + 56))&0x3>>0))], libc.BoolInt32((*TIndex)(unsafe.Pointer(pIdx1)).FpPartIdxWhere != uintptr(0))))
					goto _23
				_23:
					;
					pIdx1 = (*TIndex)(unsafe.Pointer(pIdx1)).FpNext
					i2 = i2 + 1
				}
			}
		}
	case int32(PragTyp_DATABASE_LIST):
		(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(3)
		i3 = 0
		for {
			if !(i3 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			if (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i3)*16))).FpBt == uintptr(0) {
				goto _24
			}
			_sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+20606, libc.VaList(bp+136, i3, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i3)*16))).FzDbSName, _sqlite3BtreeGetFilename(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i3)*16))).FpBt)))
			goto _24
		_24:
			;
			i3 = i3 + 1
		}
	case int32(PragTyp_COLLATION_LIST):
		i4 = 0
		(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(2)
		p = (*THash)(unsafe.Pointer(db + 448)).Ffirst
		for {
			if !(p != 0) {
				break
			}
			pColl = (*THashElem)(unsafe.Pointer(p)).Fdata
			v2 = i4
			i4 = i4 + 1
			_sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+20610, libc.VaList(bp+136, v2, (*TCollSeq)(unsafe.Pointer(pColl)).FzName))
			goto _25
		_25:
			;
			p = (*THashElem)(unsafe.Pointer(p)).Fnext
		}
	case int32(PragTyp_FUNCTION_LIST):
		showInternFunc = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_InternalFunc) != uint32(0))
		(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(6)
		i5 = 0
		for {
			if !(i5 < int32(SQLITE_FUNC_HASH_SZ)) {
				break
			}
			p1 = **(**uintptr)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(i5)*4))
			for {
				if !(p1 != 0) {
					break
				}
				_pragmaFunclistLine(tls, v, p1, int32(1), showInternFunc)
				goto _28
			_28:
				;
				p1 = *(*uintptr)(unsafe.Pointer(p1 + 36))
			}
			goto _27
		_27:
			;
			i5 = i5 + 1
		}
		j = (*THash)(unsafe.Pointer(db + 432)).Ffirst
		for {
			if !(j != 0) {
				break
			}
			p1 = (*THashElem)(unsafe.Pointer(j)).Fdata
			_pragmaFunclistLine(tls, v, p1, 0, showInternFunc)
			goto _29
		_29:
			;
			j = (*THashElem)(unsafe.Pointer(j)).Fnext
		}
	case int32(PragTyp_MODULE_LIST):
		(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(1)
		j1 = (*THash)(unsafe.Pointer(db + 404)).Ffirst
		for {
			if !(j1 != 0) {
				break
			}
			pMod = (*THashElem)(unsafe.Pointer(j1)).Fdata
			_sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+9126, libc.VaList(bp+136, (*TModule)(unsafe.Pointer(pMod)).FzName))
			goto _30
		_30:
			;
			j1 = (*THashElem)(unsafe.Pointer(j1)).Fnext
		}
	case int32(PragTyp_PRAGMA_LIST):
		i6 = 0
		for {
			if !(i6 < int32(libc.Uint32FromInt64(1072)/libc.Uint32FromInt64(16))) {
				break
			}
			_sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+9126, libc.VaList(bp+136, _aPragmaName[i6].FzName))
			goto _31
		_31:
			;
			i6 = i6 + 1
		}
	case int32(PragTyp_FOREIGN_KEY_LIST):
		if zRight != 0 {
			pTab5 = _sqlite3FindTable(tls, db, zRight, zDb)
			if pTab5 != 0 && int32((*TTable)(unsafe.Pointer(pTab5)).FeTabType) == TABTYP_NORM {
				pFK = (*(*struct {
					FaddColOffset int32
					FpFKey        uintptr
					FpDfltList    uintptr
				})(unsafe.Pointer(pTab5 + 44))).FpFKey
				if pFK != 0 {
					iTabDb1 = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab5)).FpSchema)
					i7 = 0
					(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(8)
					_sqlite3CodeVerifySchema(tls, pParse, iTabDb1)
					for pFK != 0 {
						j2 = 0
						for {
							if !(j2 < (*TFKey)(unsafe.Pointer(pFK)).FnCol) {
								break
							}
							_sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+20613, libc.VaList(bp+136, i7, j2, (*TFKey)(unsafe.Pointer(pFK)).FzTo, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab5)).FaCol + uintptr((*(*TsColMap)(unsafe.Pointer(pFK + 36 + uintptr(j2)*8))).FiFrom)*12))).FzCnName, (*(*TsColMap)(unsafe.Pointer(pFK + 36 + uintptr(j2)*8))).FzCol, _actionName(tls, **(**Tu8)(__ccgo_up(pFK + 25 + 1))), _actionName(tls, **(**Tu8)(__ccgo_up(pFK + 25))), __ccgo_ts+20622))
							goto _32
						_32:
							;
							j2 = j2 + 1
						}
						i7 = i7 + 1
						pFK = (*TFKey)(unsafe.Pointer(pFK)).FpNextFrom
					}
				}
			}
		}
	case int32(PragTyp_FOREIGN_KEY_CHECK): /* child to parent column mapping */
		regResult = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += int32(4)
		v1 = pParse + 48
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		v2 = *(*int32)(unsafe.Pointer(v1))
		regRow = v2
		k2 = (*THash)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 8)).Ffirst
		for k2 != 0 {
			if zRight != 0 {
				pTab6 = _sqlite3LocateTable(tls, pParse, uint32(0), zRight, zDb)
				k2 = uintptr(0)
			} else {
				pTab6 = (*THashElem)(unsafe.Pointer(k2)).Fdata
				k2 = (*THashElem)(unsafe.Pointer(k2)).Fnext
			}
			if pTab6 == uintptr(0) || !(int32((*TTable)(unsafe.Pointer(pTab6)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) || (*(*struct {
				FaddColOffset int32
				FpFKey        uintptr
				FpDfltList    uintptr
			})(unsafe.Pointer(pTab6 + 44))).FpFKey == uintptr(0) {
				continue
			}
			iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab6)).FpSchema)
			zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
			_sqlite3CodeVerifySchema(tls, pParse, iDb)
			_sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab6)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab6)).FzName)
			_sqlite3TouchRegister(tls, pParse, int32((*TTable)(unsafe.Pointer(pTab6)).FnCol)+regRow)
			_sqlite3OpenTable(tls, pParse, 0, iDb, pTab6, int32(OP_OpenRead))
			_sqlite3VdbeLoadString(tls, v, regResult, (*TTable)(unsafe.Pointer(pTab6)).FzName)
			i8 = int32(1)
			pFK1 = (*(*struct {
				FaddColOffset int32
				FpFKey        uintptr
				FpDfltList    uintptr
			})(unsafe.Pointer(pTab6 + 44))).FpFKey
			for {
				if !(pFK1 != 0) {
					break
				}
				pParent = _sqlite3FindTable(tls, db, (*TFKey)(unsafe.Pointer(pFK1)).FzTo, zDb)
				if pParent == uintptr(0) {
					goto _35
				}
				**(**uintptr)(__ccgo_up(bp + 68)) = uintptr(0)
				_sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pParent)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pParent)).FzName)
				x1 = _sqlite3FkLocateIndex(tls, pParse, pParent, pFK1, bp+68, uintptr(0))
				if x1 == 0 {
					if **(**uintptr)(__ccgo_up(bp + 68)) == uintptr(0) {
						_sqlite3OpenTable(tls, pParse, i8, iDb, pParent, int32(OP_OpenRead))
					} else {
						_sqlite3VdbeAddOp3(tls, v, int32(OP_OpenRead), i8, int32((*TIndex)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 68)))).Ftnum), iDb)
						_sqlite3VdbeSetP4KeyInfo(tls, pParse, **(**uintptr)(__ccgo_up(bp + 68)))
					}
				} else {
					k2 = uintptr(0)
					break
				}
				goto _35
			_35:
				;
				i8 = i8 + 1
				pFK1 = (*TFKey)(unsafe.Pointer(pFK1)).FpNextFrom
			}
			if pFK1 != 0 {
				break
			}
			if (*TParse)(unsafe.Pointer(pParse)).FnTab < i8 {
				(*TParse)(unsafe.Pointer(pParse)).FnTab = i8
			}
			addrTop = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), 0)
			i8 = int32(1)
			pFK1 = (*(*struct {
				FaddColOffset int32
				FpFKey        uintptr
				FpDfltList    uintptr
			})(unsafe.Pointer(pTab6 + 44))).FpFKey
			for {
				if !(pFK1 != 0) {
					break
				}
				pParent = _sqlite3FindTable(tls, db, (*TFKey)(unsafe.Pointer(pFK1)).FzTo, zDb)
				**(**uintptr)(__ccgo_up(bp + 68)) = uintptr(0)
				**(**uintptr)(__ccgo_up(bp + 72)) = uintptr(0)
				if pParent != 0 {
					x1 = _sqlite3FkLocateIndex(tls, pParse, pParent, pFK1, bp+68, bp+72)
				}
				addrOk = _sqlite3VdbeMakeLabel(tls, pParse)
				/* Generate code to read the child key values into registers
				 ** regRow..regRow+n. If any of the child key values are NULL, this
				 ** row cannot cause an FK violation. Jump directly to addrOk in
				 ** this case. */
				_sqlite3TouchRegister(tls, pParse, regRow+(*TFKey)(unsafe.Pointer(pFK1)).FnCol)
				j3 = 0
				for {
					if !(j3 < (*TFKey)(unsafe.Pointer(pFK1)).FnCol) {
						break
					}
					if **(**uintptr)(__ccgo_up(bp + 72)) != 0 {
						v2 = **(**int32)(__ccgo_up(**(**uintptr)(__ccgo_up(bp + 72)) + uintptr(j3)*4))
					} else {
						v2 = (*(*TsColMap)(unsafe.Pointer(pFK1 + 36 + uintptr(j3)*8))).FiFrom
					}
					iCol = v2
					_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab6, 0, iCol, regRow+j3)
					_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regRow+j3, addrOk)
					goto _37
				_37:
					;
					j3 = j3 + 1
				}
				/* Generate code to query the parent index for a matching parent
				 ** key. If a match is found, jump to addrOk. */
				if **(**uintptr)(__ccgo_up(bp + 68)) != 0 {
					_sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), regRow, (*TFKey)(unsafe.Pointer(pFK1)).FnCol, 0, _sqlite3IndexAffinityStr(tls, db, **(**uintptr)(__ccgo_up(bp + 68))), (*TFKey)(unsafe.Pointer(pFK1)).FnCol)
					_sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), i8, addrOk, regRow, (*TFKey)(unsafe.Pointer(pFK1)).FnCol)
				} else {
					if pParent != 0 {
						jmp = _sqlite3VdbeCurrentAddr(tls, v) + int32(2)
						_sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), i8, jmp, regRow)
						_sqlite3VdbeGoto(tls, v, addrOk)
					}
				}
				/* Generate code to report an FK violation to the caller. */
				if (*TTable)(unsafe.Pointer(pTab6)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), 0, regResult+int32(1))
				} else {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regResult+int32(1))
				}
				_sqlite3VdbeMultiLoad(tls, v, regResult+int32(2), __ccgo_ts+20627, libc.VaList(bp+136, (*TFKey)(unsafe.Pointer(pFK1)).FzTo, i8-int32(1)))
				_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), regResult, int32(4))
				_sqlite3VdbeResolveLabel(tls, v, addrOk)
				_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp + 72)))
				goto _36
			_36:
				;
				i8 = i8 + 1
				pFK1 = (*TFKey)(unsafe.Pointer(pFK1)).FpNextFrom
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), 0, addrTop+int32(1))
			_sqlite3VdbeJumpHere(tls, v, addrTop)
		}
		break
		/* Reinstall the LIKE and GLOB functions.  The variant of LIKE
		 ** used will be case sensitive or not depending on the RHS.
		 */
		fallthrough
	case int32(PragTyp_CASE_SENSITIVE_LIKE):
		if zRight != 0 {
			_sqlite3RegisterLikeFunctions(tls, db, int32(_sqlite3GetBoolean(tls, zRight, uint8(0))))
		}
		break
		/*    PRAGMA integrity_check
		 **    PRAGMA integrity_check(N)
		 **    PRAGMA quick_check
		 **    PRAGMA quick_check(N)
		 **
		 ** Verify the integrity of the database.
		 **
		 ** The "quick_check" is reduced version of
		 ** integrity_check designed to detect most database corruption
		 ** without the overhead of cross-checking indexes.  Quick_check
		 ** is linear time whereas integrity_check is O(NlogN).
		 **
		 ** The maximum number of errors is 100 by default.  A different default
		 ** can be specified using a numeric parameter N.
		 **
		 ** Or, the parameter N can be the name of a table.  In that case, only
		 ** the one table named is verified.  The freelist is only verified if
		 ** the named table is "sqlite_schema" (or one of its aliases).
		 **
		 ** All schemas are checked by default.  To check just a single
		 ** schema, use the form:
		 **
		 **      PRAGMA schema.integrity_check;
		 */
		fallthrough
	case int32(PragTyp_INTEGRITY_CHECK):
		pObjTab = uintptr(0) /* Check only this one table, if not NULL */
		isQuick = libc.BoolInt32(int32(_sqlite3UpperToLower[uint8(**(**int8)(__ccgo_up(zLeft)))]) == int32('q'))
		/* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
		 ** then iDb is set to the index of the database identified by <db>.
		 ** In this case, the integrity of database iDb only is verified by
		 ** the VDBE created below.
		 **
		 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
		 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
		 ** to -1 here, to indicate that the VDBE should verify the integrity
		 ** of all attached databases.  */
		if (*TToken)(unsafe.Pointer(pId2)).Fz == uintptr(0) {
			iDb = -int32(1)
		}
		/* Initialize the VDBE program */
		(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(6)
		/* Set the maximum error count */
		**(**int32)(__ccgo_up(bp + 76)) = int32(SQLITE_INTEGRITY_CHECK_ERROR_MAX)
		if zRight != 0 {
			if _sqlite3GetInt32(tls, (*TToken)(unsafe.Pointer(pValue)).Fz, bp+76) != 0 {
				if **(**int32)(__ccgo_up(bp + 76)) <= 0 {
					**(**int32)(__ccgo_up(bp + 76)) = int32(SQLITE_INTEGRITY_CHECK_ERROR_MAX)
				}
			} else {
				if iDb >= 0 {
					v1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
				} else {
					v1 = uintptr(0)
				}
				pObjTab = _sqlite3LocateTable(tls, pParse, uint32(0), zRight, v1)
			}
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), **(**int32)(__ccgo_up(bp + 76))-int32(1), int32(1)) /* reg[1] holds errors left */
		/* Do an integrity check on each database file */
		i9 = 0
		for {
			if !(i9 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			} /* Array of root page numbers of all btrees */
			cnt = 0 /* Number of entries in aRoot[] */
			if libc.Bool(OMIT_TEMPDB != 0) && i9 == int32(1) {
				goto _40
			}
			if iDb >= 0 && i9 != iDb {
				goto _40
			}
			_sqlite3CodeVerifySchema(tls, pParse, i9)
			libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(0), 7, 0x80) /* tag-20230327-1 */
			/* Do an integrity check of the B-Tree
			 **
			 ** Begin by finding the root pages numbers
			 ** for all tables and indices in the database.
			 */
			pTbls = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i9)*16))).FpSchema + 8
			cnt = 0
			x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst
			for {
				if !(x2 != 0) {
					break
				}
				pTab7 = (*THashElem)(unsafe.Pointer(x2)).Fdata /* Number of indexes on pTab */
				if _tableSkipIntegrityCheck(tls, pTab7, pObjTab) != 0 {
					goto _41
				}
				if (*TTable)(unsafe.Pointer(pTab7)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
					cnt = cnt + 1
				}
				nIdx = 0
				pIdx3 = (*TTable)(unsafe.Pointer(pTab7)).FpIndex
				for {
					if !(pIdx3 != 0) {
						break
					}
					cnt = cnt + 1
					goto _42
				_42:
					;
					pIdx3 = (*TIndex)(unsafe.Pointer(pIdx3)).FpNext
					nIdx = nIdx + 1
				}
				goto _41
			_41:
				;
				x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext
			}
			if cnt == 0 {
				goto _40
			}
			if pObjTab != 0 {
				cnt = cnt + 1
			}
			aRoot = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(cnt+libc.Int32FromInt32(1))))
			if aRoot == uintptr(0) {
				break
			}
			cnt = 0
			if pObjTab != 0 {
				cnt = cnt + 1
				v2 = cnt
				**(**int32)(__ccgo_up(aRoot + uintptr(v2)*4)) = 0
			}
			x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst
			for {
				if !(x2 != 0) {
					break
				}
				pTab8 = (*THashElem)(unsafe.Pointer(x2)).Fdata
				if _tableSkipIntegrityCheck(tls, pTab8, pObjTab) != 0 {
					goto _44
				}
				if (*TTable)(unsafe.Pointer(pTab8)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
					cnt = cnt + 1
					v2 = cnt
					**(**int32)(__ccgo_up(aRoot + uintptr(v2)*4)) = int32((*TTable)(unsafe.Pointer(pTab8)).Ftnum)
				}
				pIdx4 = (*TTable)(unsafe.Pointer(pTab8)).FpIndex
				for {
					if !(pIdx4 != 0) {
						break
					}
					cnt = cnt + 1
					v2 = cnt
					**(**int32)(__ccgo_up(aRoot + uintptr(v2)*4)) = int32((*TIndex)(unsafe.Pointer(pIdx4)).Ftnum)
					goto _46
				_46:
					;
					pIdx4 = (*TIndex)(unsafe.Pointer(pIdx4)).FpNext
				}
				goto _44
			_44:
				;
				x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext
			}
			**(**int32)(__ccgo_up(aRoot)) = cnt
			/* Make sure sufficient number of registers have been allocated */
			_sqlite3TouchRegister(tls, pParse, int32(8)+cnt)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, int32(8), int32(8)+cnt)
			_sqlite3ClearTempRegCache(tls, pParse)
			/* Do the b-tree integrity checks */
			_sqlite3VdbeAddOp4(tls, v, int32(OP_IntegrityCk), int32(1), cnt, int32(8), aRoot, -int32(15))
			_sqlite3VdbeChangeP5(tls, v, uint16(i9))
			addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), int32(2))
			_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, _sqlite3MPrintf(tls, db, __ccgo_ts+20631, libc.VaList(bp+136, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i9)*16))).FzDbSName)), -int32(7))
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(2), int32(3), int32(3))
			_integrityCheckResultRow(tls, v)
			_sqlite3VdbeJumpHere(tls, v, addr1)
			/* Check that the indexes all have the right number of rows */
			if pObjTab != 0 {
				v2 = int32(1)
			} else {
				v2 = 0
			}
			cnt = v2
			_sqlite3VdbeLoadString(tls, v, int32(2), __ccgo_ts+20655)
			x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst
			for {
				if !(x2 != 0) {
					break
				}
				iTab = 0
				pTab9 = (*THashElem)(unsafe.Pointer(x2)).Fdata
				if _tableSkipIntegrityCheck(tls, pTab9, pObjTab) != 0 {
					goto _49
				}
				if (*TTable)(unsafe.Pointer(pTab9)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
					v2 = cnt
					cnt = cnt + 1
					iTab = v2
				} else {
					iTab = cnt
					pIdx5 = (*TTable)(unsafe.Pointer(pTab9)).FpIndex
					for {
						if !(pIdx5 != 0) {
							break
						}
						if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx5 + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
							break
						}
						iTab = iTab + 1
						goto _51
					_51:
						;
						pIdx5 = (*TIndex)(unsafe.Pointer(pIdx5)).FpNext
					}
				}
				pIdx5 = (*TTable)(unsafe.Pointer(pTab9)).FpIndex
				for {
					if !(pIdx5 != 0) {
						break
					}
					if (*TIndex)(unsafe.Pointer(pIdx5)).FpPartIdxWhere == uintptr(0) {
						addr1 = _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), int32(8)+cnt, 0, int32(8)+iTab)
						_sqlite3VdbeLoadString(tls, v, int32(4), (*TIndex)(unsafe.Pointer(pIdx5)).FzName)
						_sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(4), int32(2), int32(3))
						_integrityCheckResultRow(tls, v)
						_sqlite3VdbeJumpHere(tls, v, addr1)
					}
					cnt = cnt + 1
					goto _52
				_52:
					;
					pIdx5 = (*TIndex)(unsafe.Pointer(pIdx5)).FpNext
				}
				goto _49
			_49:
				;
				x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext
			}
			/* Make sure all the indices are constructed correctly.
			 */
			x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst
			for {
				if !(x2 != 0) {
					break
				}
				pTab10 = (*THashElem)(unsafe.Pointer(x2)).Fdata
				pPrior = uintptr(0)
				r1 = -int32(1) /* Maximum non-virtual column number */
				if _tableSkipIntegrityCheck(tls, pTab10, pObjTab) != 0 {
					goto _53
				}
				if !(int32((*TTable)(unsafe.Pointer(pTab10)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
					goto _53
				}
				if isQuick != 0 || (*TTable)(unsafe.Pointer(pTab10)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
					pPk1 = uintptr(0)
					r2 = 0
				} else {
					pPk1 = _sqlite3PrimaryKeyIndex(tls, pTab10)
					r2 = _sqlite3GetTempRange(tls, pParse, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol))
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), int32(1), r2, r2+int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)-int32(1))
				}
				_sqlite3OpenTableAndIndices(tls, pParse, pTab10, int32(OP_OpenRead), uint8(0), int32(1), uintptr(0), bp+80, bp+84)
				/* reg[7] counts the number of entries in the table.
				 ** reg[8+i] counts the number of entries in the i-th index
				 */
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, int32(7))
				j4 = 0
				pIdx6 = (*TTable)(unsafe.Pointer(pTab10)).FpIndex
				for {
					if !(pIdx6 != 0) {
						break
					}
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, int32(8)+j4) /* index entries counter */
					goto _54
				_54:
					;
					pIdx6 = (*TIndex)(unsafe.Pointer(pIdx6)).FpNext
					j4 = j4 + 1
				}
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), **(**int32)(__ccgo_up(bp + 80)), 0)
				loopTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), int32(7), int32(1))
				/* Fetch the right-most column from the table.  This will cause
				 ** the entire record header to be parsed and sanity checked.  It
				 ** will also prepopulate the cursor column cache that is used
				 ** by the OP_IsType code, so it is a required step.
				 */
				if (*TTable)(unsafe.Pointer(pTab10)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
					mxCol = -int32(1)
					j4 = 0
					for {
						if !(j4 < int32((*TTable)(unsafe.Pointer(pTab10)).FnCol)) {
							break
						}
						if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 {
							mxCol = mxCol + 1
						}
						goto _55
					_55:
						;
						j4 = j4 + 1
					}
					if mxCol == int32((*TTable)(unsafe.Pointer(pTab10)).FiPKey) {
						mxCol = mxCol - 1
					}
				} else {
					/* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID
					 ** PK index column-count, so there is no need to account for them
					 ** in this case. */
					mxCol = int32((*TIndex)(unsafe.Pointer(_sqlite3PrimaryKeyIndex(tls, pTab10))).FnColumn) - int32(1)
				}
				if mxCol >= 0 {
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), **(**int32)(__ccgo_up(bp + 80)), mxCol, int32(3))
					_sqlite3VdbeTypeofColumn(tls, v, int32(3))
				}
				if !(isQuick != 0) {
					if pPk1 != 0 {
						a1 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxGT), **(**int32)(__ccgo_up(bp + 80)), 0, r2, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol))
						_sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), r2)
						zErr = _sqlite3MPrintf(tls, db, __ccgo_ts+20684, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab10)).FzName))
						_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr, -int32(7))
						_integrityCheckResultRow(tls, v)
						_sqlite3VdbeJumpHere(tls, v, a1)
						_sqlite3VdbeJumpHere(tls, v, a1+int32(1))
						j4 = 0
						for {
							if !(j4 < int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)) {
								break
							}
							_sqlite3ExprCodeLoadIndexColumn(tls, pParse, pPk1, **(**int32)(__ccgo_up(bp + 80)), j4, r2+j4)
							goto _56
						_56:
							;
							j4 = j4 + 1
						}
					}
				}
				/* Verify datatypes for all columns:
				 **
				 **   (1) NOT NULL columns may not contain a NULL
				 **   (2) Datatype must be exact for non-ANY columns in STRICT tables
				 **   (3) Datatype for TEXT columns in non-STRICT tables must be
				 **       NULL, TEXT, or BLOB.
				 **   (4) Datatype for numeric columns in non-STRICT tables must not
				 **       be a TEXT value that can be losslessly converted to numeric.
				 */
				bStrict = libc.BoolInt32((*TTable)(unsafe.Pointer(pTab10)).FtabFlags&uint32(TF_Strict) != uint32(0))
				j4 = 0
				for {
					if !(j4 < int32((*TTable)(unsafe.Pointer(pTab10)).FnCol)) {
						break
					}
					pCol1 = (*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*12 /* Check datatypes (besides NOT NULL) */
					if j4 == int32((*TTable)(unsafe.Pointer(pTab10)).FiPKey) {
						goto _57
					}
					if bStrict != 0 {
						doTypeCheck = libc.BoolInt32(int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf0>>4)) > int32(COLTYPE_ANY))
					} else {
						doTypeCheck = libc.BoolInt32(int32((*TColumn)(unsafe.Pointer(pCol1)).Faffinity) > int32(SQLITE_AFF_BLOB))
					}
					if int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf>>0)) == 0 && !(doTypeCheck != 0) {
						goto _57
					}
					/* Compute the operands that will be needed for OP_IsType */
					p4 = int32(SQLITE_NULL)
					if int32((*TColumn)(unsafe.Pointer(pCol1)).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
						_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab10, **(**int32)(__ccgo_up(bp + 80)), j4, int32(3))
						p11 = -int32(1)
						p3 = int32(3)
					} else {
						if (*TColumn)(unsafe.Pointer(pCol1)).FiDflt != 0 {
							**(**uintptr)(__ccgo_up(bp + 88)) = uintptr(0)
							_sqlite3ValueFromExpr(tls, db, _sqlite3ColumnExpr(tls, pTab10, pCol1), (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uint8((*TColumn)(unsafe.Pointer(pCol1)).Faffinity), bp+88)
							if **(**uintptr)(__ccgo_up(bp + 88)) != 0 {
								p4 = Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(bp + 88)))
								_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up(bp + 88)))
							}
						}
						p11 = **(**int32)(__ccgo_up(bp + 80))
						if !((*TTable)(unsafe.Pointer(pTab10)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
							p3 = _sqlite3TableColumnToIndex(tls, _sqlite3PrimaryKeyIndex(tls, pTab10), j4)
						} else {
							p3 = int32(_sqlite3TableColumnToStorage(tls, pTab10, int16(j4)))
						}
					}
					labelError = _sqlite3VdbeMakeLabel(tls, pParse)
					labelOk = _sqlite3VdbeMakeLabel(tls, pParse)
					if int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf>>0)) != 0 {
						jmp2 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), p11, labelOk, p3, p4)
						if p11 < 0 {
							_sqlite3VdbeChangeP5(tls, v, uint16(0x0f)) /* INT, REAL, TEXT, or BLOB */
							jmp3 = jmp2
						} else {
							_sqlite3VdbeChangeP5(tls, v, uint16(0x0d)) /* INT, TEXT, or BLOB */
							/* OP_IsType does not detect NaN values in the database file
							 ** which should be treated as a NULL.  So if the header type
							 ** is REAL, we have to load the actual data using OP_Column
							 ** to reliably determine if the value is a NULL. */
							_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), p11, p3, int32(3))
							_sqlite3ColumnDefault(tls, v, pTab10, j4, int32(3))
							jmp3 = _sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), int32(3), labelOk)
						}
						zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+20720, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab10)).FzName, (*TColumn)(unsafe.Pointer(pCol1)).FzCnName))
						_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr1, -int32(7))
						if doTypeCheck != 0 {
							_sqlite3VdbeGoto(tls, v, labelError)
							_sqlite3VdbeJumpHere(tls, v, jmp2)
							_sqlite3VdbeJumpHere(tls, v, jmp3)
						} else {
							/* VDBE byte code will fall thru */
						}
					}
					if bStrict != 0 && doTypeCheck != 0 {
						_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), p11, labelOk, p3, p4)
						_sqlite3VdbeChangeP5(tls, v, uint16(_aStdTypeMask[int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf0>>4))-int32(1)]))
						zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+20740, libc.VaList(bp+136, _sqlite3StdType[int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 4))&0xf0>>4))-int32(1)], (*TTable)(unsafe.Pointer(pTab10)).FzName, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*12))).FzCnName))
						_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr1, -int32(7))
					} else {
						if !(bStrict != 0) && int32((*TColumn)(unsafe.Pointer(pCol1)).Faffinity) == int32(SQLITE_AFF_TEXT) {
							/* (3) Datatype for TEXT columns in non-STRICT tables must be
							 **     NULL, TEXT, or BLOB. */
							_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), p11, labelOk, p3, p4)
							_sqlite3VdbeChangeP5(tls, v, uint16(0x1c)) /* NULL, TEXT, or BLOB */
							zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+20762, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab10)).FzName, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*12))).FzCnName))
							_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr1, -int32(7))
						} else {
							if !(bStrict != 0) && int32((*TColumn)(unsafe.Pointer(pCol1)).Faffinity) >= int32(SQLITE_AFF_NUMERIC) {
								/* (4) Datatype for numeric columns in non-STRICT tables must not
								 **     be a TEXT value that can be converted to numeric. */
								_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), p11, labelOk, p3, p4)
								_sqlite3VdbeChangeP5(tls, v, uint16(0x1b)) /* NULL, INT, FLOAT, or BLOB */
								if p11 >= 0 {
									_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab10, **(**int32)(__ccgo_up(bp + 80)), j4, int32(3))
								}
								_sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), int32(3), int32(1), 0, __ccgo_ts+20785, -int32(1))
								_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IsType), -int32(1), labelOk, int32(3), p4)
								_sqlite3VdbeChangeP5(tls, v, uint16(0x1c)) /* NULL, TEXT, or BLOB */
								zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+20787, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab10)).FzName, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*12))).FzCnName))
								_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr1, -int32(7))
							}
						}
					}
					_sqlite3VdbeResolveLabel(tls, v, labelError)
					_integrityCheckResultRow(tls, v)
					_sqlite3VdbeResolveLabel(tls, v, labelOk)
					goto _57
				_57:
					;
					j4 = j4 + 1
				}
				/* Verify CHECK constraints */
				if (*TTable)(unsafe.Pointer(pTab10)).FpCheck != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_IgnoreChecks) == uint64(0) {
					pCheck = _sqlite3ExprListDup(tls, db, (*TTable)(unsafe.Pointer(pTab10)).FpCheck, 0)
					if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
						addrCkFault = _sqlite3VdbeMakeLabel(tls, pParse)
						addrCkOk = _sqlite3VdbeMakeLabel(tls, pParse)
						(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = **(**int32)(__ccgo_up(bp + 80)) + int32(1)
						k3 = (*TExprList)(unsafe.Pointer(pCheck)).FnExpr - int32(1)
						for {
							if !(k3 > 0) {
								break
							}
							_sqlite3ExprIfFalse(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pCheck + 8 + uintptr(k3)*20))).FpExpr, addrCkFault, 0)
							goto _58
						_58:
							;
							k3 = k3 - 1
						}
						_sqlite3ExprIfTrue(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pCheck + 8))).FpExpr, addrCkOk, int32(SQLITE_JUMPIFNULL))
						_sqlite3VdbeResolveLabel(tls, v, addrCkFault)
						(*TParse)(unsafe.Pointer(pParse)).FiSelfTab = 0
						zErr2 = _sqlite3MPrintf(tls, db, __ccgo_ts+20807, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab10)).FzName))
						_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, zErr2, -int32(7))
						_integrityCheckResultRow(tls, v)
						_sqlite3VdbeResolveLabel(tls, v, addrCkOk)
					}
					_sqlite3ExprListDelete(tls, db, pCheck)
				}
				if !(isQuick != 0) { /* Omit the remaining tests for quick_check */
					/* Validate index entries for the current row */
					j4 = 0
					pIdx6 = (*TTable)(unsafe.Pointer(pTab10)).FpIndex
					for {
						if !(pIdx6 != 0) {
							break
						}
						ckUniq = _sqlite3VdbeMakeLabel(tls, pParse)
						if pPk1 == pIdx6 {
							goto _59
						}
						r1 = _sqlite3GenerateIndexKey(tls, pParse, pIdx6, **(**int32)(__ccgo_up(bp + 80)), 0, 0, bp+92, pPrior, r1)
						pPrior = pIdx6
						_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), int32(8)+j4, int32(1)) /* increment entry count */
						/* Verify that an index entry exists for the current table row */
						_sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), **(**int32)(__ccgo_up(bp + 84))+j4, ckUniq, r1, int32((*TIndex)(unsafe.Pointer(pIdx6)).FnColumn))
						jmp21 = _sqlite3VdbeAddOp3(tls, v, int32(OP_IFindKey), **(**int32)(__ccgo_up(bp + 84))+j4, ckUniq, r1)
						_sqlite3VdbeChangeP4(tls, v, -int32(1), pIdx6, -int32(6))
						_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, int32(3), 0, _sqlite3MPrintf(tls, db, __ccgo_ts+20837, libc.VaList(bp+136, (*TIndex)(unsafe.Pointer(pIdx6)).FzName)), -int32(7))
						_sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3))
						_integrityCheckResultRow(tls, v)
						_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, ckUniq)
						_sqlite3VdbeJumpHere(tls, v, jmp21)
						_sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+20896)
						_sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3))
						_sqlite3VdbeLoadString(tls, v, int32(4), __ccgo_ts+20901)
						_sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(4), int32(3), int32(3))
						jmp5 = _sqlite3VdbeLoadString(tls, v, int32(4), (*TIndex)(unsafe.Pointer(pIdx6)).FzName)
						_sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(4), int32(3), int32(3))
						jmp4 = _integrityCheckResultRow(tls, v)
						_sqlite3VdbeResolveLabel(tls, v, ckUniq)
						/* The OP_IdxRowid opcode is an optimized version of OP_Column
						 ** that extracts the rowid off the end of the index record.
						 ** But it only works correctly if index record does not have
						 ** any extra bytes at the end.  Verify that this is the case. */
						if (*TTable)(unsafe.Pointer(pTab10)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
							_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxRowid), **(**int32)(__ccgo_up(bp + 84))+j4, int32(3))
							jmp7 = _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), int32(3), 0, r1+int32((*TIndex)(unsafe.Pointer(pIdx6)).FnColumn)-int32(1))
							_sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+20922)
							_sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3))
							_sqlite3VdbeLoadString(tls, v, int32(4), __ccgo_ts+20958)
							_sqlite3VdbeGoto(tls, v, jmp5-int32(1))
							_sqlite3VdbeJumpHere(tls, v, jmp7)
						}
						/* Any indexed columns with non-BINARY collations must still hold
						 ** the exact same text value as the table. */
						label6 = 0
						kk = 0
						for {
							if !(kk < int32((*TIndex)(unsafe.Pointer(pIdx6)).FnKeyCol)) {
								break
							}
							if **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx6)).FazColl + uintptr(kk)*4)) == uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) {
								goto _60
							}
							if label6 == 0 {
								label6 = _sqlite3VdbeMakeLabel(tls, pParse)
							}
							_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), **(**int32)(__ccgo_up(bp + 84))+j4, kk, int32(3))
							_sqlite3VdbeAddOp3(tls, v, int32(OP_Ne), int32(3), label6, r1+kk)
							goto _60
						_60:
							;
							kk = kk + 1
						}
						if label6 != 0 {
							jmp6 = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto))
							_sqlite3VdbeResolveLabel(tls, v, label6)
							_sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+20896)
							_sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3))
							_sqlite3VdbeLoadString(tls, v, int32(4), __ccgo_ts+20969)
							_sqlite3VdbeGoto(tls, v, jmp5-int32(1))
							_sqlite3VdbeJumpHere(tls, v, jmp6)
						}
						/* For UNIQUE indexes, verify that only one entry exists with the
						 ** current key.  The entry is unique if (1) any column is NULL
						 ** or (2) the next entry has a different key */
						if int32((*TIndex)(unsafe.Pointer(pIdx6)).FonError) != OE_None {
							uniqOk = _sqlite3VdbeMakeLabel(tls, pParse)
							kk = 0
							for {
								if !(kk < int32((*TIndex)(unsafe.Pointer(pIdx6)).FnKeyCol)) {
									break
								}
								iCol1 = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx6)).FaiColumn + uintptr(kk)*2)))
								if iCol1 >= 0 && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(iCol1)*12 + 4))&0xf>>0)) != 0 {
									goto _61
								}
								_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), r1+kk, uniqOk)
								goto _61
							_61:
								;
								kk = kk + 1
							}
							jmp61 = _sqlite3VdbeAddOp1(tls, v, int32(OP_Next), **(**int32)(__ccgo_up(bp + 84))+j4)
							_sqlite3VdbeGoto(tls, v, uniqOk)
							_sqlite3VdbeJumpHere(tls, v, jmp61)
							_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxGT), **(**int32)(__ccgo_up(bp + 84))+j4, uniqOk, r1, int32((*TIndex)(unsafe.Pointer(pIdx6)).FnKeyCol))
							_sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+20996)
							_sqlite3VdbeGoto(tls, v, jmp5)
							_sqlite3VdbeResolveLabel(tls, v, uniqOk)
						}
						_sqlite3VdbeJumpHere(tls, v, jmp4)
						_sqlite3ResolvePartIdxLabel(tls, pParse, **(**int32)(__ccgo_up(bp + 92)))
						goto _59
					_59:
						;
						pIdx6 = (*TIndex)(unsafe.Pointer(pIdx6)).FpNext
						j4 = j4 + 1
					}
				}
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), **(**int32)(__ccgo_up(bp + 80)), loopTop)
				_sqlite3VdbeJumpHere(tls, v, loopTop-int32(1))
				if pPk1 != 0 {
					_sqlite3ReleaseTempRange(tls, pParse, r2, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol))
				}
				goto _53
			_53:
				;
				x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext
			}
			/* Second pass to invoke the xIntegrity method on all virtual
			 ** tables.
			 */
			x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst
			for {
				if !(x2 != 0) {
					break
				}
				pTab11 = (*THashElem)(unsafe.Pointer(x2)).Fdata
				if _tableSkipIntegrityCheck(tls, pTab11, pObjTab) != 0 {
					goto _62
				}
				if int32((*TTable)(unsafe.Pointer(pTab11)).FeTabType) == TABTYP_NORM {
					goto _62
				}
				if !(int32((*TTable)(unsafe.Pointer(pTab11)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
					goto _62
				}
				if int32((*TTable)(unsafe.Pointer(pTab11)).FnCol) <= 0 {
					zMod = **(**uintptr)(__ccgo_up((*(*struct {
						FnArg  int32
						FazArg uintptr
						Fp     uintptr
					})(unsafe.Pointer(pTab11 + 44))).FazArg))
					if _sqlite3HashFind(tls, db+404, zMod) == uintptr(0) {
						goto _62
					}
				}
				_sqlite3ViewGetColumnNames(tls, pParse, pTab11)
				if (*(*struct {
					FnArg  int32
					FazArg uintptr
					Fp     uintptr
				})(unsafe.Pointer(pTab11 + 44))).Fp == uintptr(0) {
					goto _62
				}
				pVTab = (*TVTable)(unsafe.Pointer((*(*struct {
					FnArg  int32
					FazArg uintptr
					Fp     uintptr
				})(unsafe.Pointer(pTab11 + 44))).Fp)).FpVtab
				if pVTab == uintptr(0) {
					goto _62
				}
				if (*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FpModule == uintptr(0) {
					goto _62
				}
				if (*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FpModule)).FiVersion < int32(4) {
					goto _62
				}
				if (*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FpModule)).FxIntegrity == uintptr(0) {
					goto _62
				}
				_sqlite3VdbeAddOp3(tls, v, int32(OP_VCheck), i9, int32(3), isQuick)
				(*TTable)(unsafe.Pointer(pTab11)).FnTabRef = (*TTable)(unsafe.Pointer(pTab11)).FnTabRef + 1
				_sqlite3VdbeAppendP4(tls, v, pTab11, -int32(17))
				a11 = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), int32(3))
				_integrityCheckResultRow(tls, v)
				_sqlite3VdbeJumpHere(tls, v, a11)
				goto _62
				goto _62
			_62:
				;
				x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext
			}
			goto _40
		_40:
			;
			i9 = i9 + 1
		}
		aOp2 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(28)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_endCode)), _iLn21)
		if aOp2 != 0 {
			(**(**TVdbeOp)(__ccgo_up(aOp2))).Fp2 = int32(1) - **(**int32)(__ccgo_up(bp + 76))
			(**(**TVdbeOp)(__ccgo_up(aOp2 + 2*20))).Fp4type = int8(-libc.Int32FromInt32(1))
			*(*uintptr)(unsafe.Pointer(aOp2 + 2*20 + 16)) = __ccgo_ts + 21023
			(**(**TVdbeOp)(__ccgo_up(aOp2 + 5*20))).Fp4type = int8(-libc.Int32FromInt32(1))
			*(*uintptr)(unsafe.Pointer(aOp2 + 5*20 + 16)) = _sqlite3ErrStr(tls, int32(SQLITE_CORRUPT))
		}
		_sqlite3VdbeChangeP3(tls, v, 0, _sqlite3VdbeCurrentAddr(tls, v)-int32(2))
		break
		/*
		 **   PRAGMA encoding
		 **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
		 **
		 ** In its first form, this pragma returns the encoding of the main
		 ** database. If the database is not initialized, it is initialized now.
		 **
		 ** The second form of this pragma is a no-op if the main database file
		 ** has not already been initialized. In this case it sets the default
		 ** encoding that will be used for the main database file if a new file
		 ** is created. If an existing main database file is opened, then the
		 ** default text encoding for the existing database is used.
		 **
		 ** In all cases new databases created using the ATTACH command are
		 ** created to use the same default text encoding as the main database. If
		 ** the main database has not been initialized and/or created when ATTACH
		 ** is executed, this is done before the ATTACH operation.
		 **
		 ** In the second form this pragma sets the text encoding to be used in
		 ** new database files created using this database handle. It is only
		 ** useful if invoked immediately after the main database i
		 */
		fallthrough
	case int32(PragTyp_ENCODING):
		if !(zRight != 0) { /* "PRAGMA encoding" */
			if _sqlite3ReadSchema(tls, pParse) != 0 {
				goto pragma_out
			}
			_returnSingleText(tls, v, _encnames1[(*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fenc].FzName)
		} else { /* "PRAGMA encoding = XXX" */
			/* Only change the value of sqlite.enc if the database handle is not
			 ** initialized. If the main database exists, the new sqlite.enc value
			 ** will be overwritten when the schema is next loaded. If it does not
			 ** already exists, it will be created to use the new encoding value.
			 */
			if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_EncodingFixed) == uint32(0) {
				pEnc = uintptr(unsafe.Pointer(&_encnames1))
				for {
					if !((*struct {
						FzName uintptr
						Fenc   Tu8
					})(unsafe.Pointer(pEnc)).FzName != 0) {
						break
					}
					if 0 == _sqlite3StrICmp(tls, zRight, (*struct {
						FzName uintptr
						Fenc   Tu8
					})(unsafe.Pointer(pEnc)).FzName) {
						if (*struct {
							FzName uintptr
							Fenc   Tu8
						})(unsafe.Pointer(pEnc)).Fenc != 0 {
							v2 = int32((*struct {
								FzName uintptr
								Fenc   Tu8
							})(unsafe.Pointer(pEnc)).Fenc)
						} else {
							v2 = int32(SQLITE_UTF16LE)
						}
						enc = uint8(v2)
						(*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).Fenc = enc
						_sqlite3SetTextEncoding(tls, db, enc)
						break
					}
					goto _63
				_63:
					;
					pEnc += 8
				}
				if !((*struct {
					FzName uintptr
					Fenc   Tu8
				})(unsafe.Pointer(pEnc)).FzName != 0) {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21084, libc.VaList(bp+136, zRight))
				}
			}
		}
		break
		/*
		 **   PRAGMA [schema.]schema_version
		 **   PRAGMA [schema.]schema_version = <integer>
		 **
		 **   PRAGMA [schema.]user_version
		 **   PRAGMA [schema.]user_version = <integer>
		 **
		 **   PRAGMA [schema.]freelist_count
		 **
		 **   PRAGMA [schema.]data_version
		 **
		 **   PRAGMA [schema.]application_id
		 **   PRAGMA [schema.]application_id = <integer>
		 **
		 ** The pragma's schema_version and user_version are used to set or get
		 ** the value of the schema-version and user-version, respectively. Both
		 ** the schema-version and the user-version are 32-bit signed integers
		 ** stored in the database header.
		 **
		 ** The schema-cookie is usually only manipulated internally by SQLite. It
		 ** is incremented by SQLite whenever the database schema is modified (by
		 ** creating or dropping a table or index). The schema version is used by
		 ** SQLite each time a query is executed to ensure that the internal cache
		 ** of the schema used when compiling the SQL query matches the schema of
		 ** the database against which the compiled query is actually executed.
		 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
		 ** the schema-version is potentially dangerous and may lead to program
		 ** crashes or database corruption. Use with caution!
		 **
		 ** The user-version is not used internally by SQLite. It may be used by
		 ** applications for any purpose.
		 */
		fallthrough
	case int32(PragTyp_HEADER_VALUE):
		iCookie = int32((*TPragmaName)(unsafe.Pointer(pPragma)).FiArg) /* Which cookie to read or write */
		_sqlite3VdbeUsesBtree(tls, v, iDb)
		if zRight != 0 && int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_ReadOnly) == 0 {
			aOp3 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(8)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_setCookie)), 0)
			if 0 != 0 {
				break
			}
			(**(**TVdbeOp)(__ccgo_up(aOp3))).Fp1 = iDb
			(**(**TVdbeOp)(__ccgo_up(aOp3 + 1*20))).Fp1 = iDb
			(**(**TVdbeOp)(__ccgo_up(aOp3 + 1*20))).Fp2 = iCookie
			(**(**TVdbeOp)(__ccgo_up(aOp3 + 1*20))).Fp3 = _sqlite3Atoi(tls, zRight)
			(**(**TVdbeOp)(__ccgo_up(aOp3 + 1*20))).Fp5 = uint16(1)
			if iCookie == int32(BTREE_SCHEMA_VERSION) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_Defensive) != uint64(0) {
				/* Do not allow the use of PRAGMA schema_version=VALUE in defensive
				 ** mode.  Change the OP_SetCookie opcode into a no-op.  */
				(**(**TVdbeOp)(__ccgo_up(aOp3 + 1*20))).Fopcode = uint8(OP_Noop)
			}
		} else {
			aOp4 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(4)), uintptr(unsafe.Pointer(&_readCookie)), 0)
			if 0 != 0 {
				break
			}
			(**(**TVdbeOp)(__ccgo_up(aOp4))).Fp1 = iDb
			(**(**TVdbeOp)(__ccgo_up(aOp4 + 1*20))).Fp1 = iDb
			(**(**TVdbeOp)(__ccgo_up(aOp4 + 1*20))).Fp3 = iCookie
			_sqlite3VdbeReusable(tls, v)
		}
		break
		/*
		 **   PRAGMA compile_options
		 **
		 ** Return the names of all compile-time options used in this build,
		 ** one option per row.
		 */
		fallthrough
	case int32(PragTyp_COMPILE_OPTIONS):
		i10 = 0
		(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(1)
		for {
			v2 = i10
			i10 = i10 + 1
			v1 = Xsqlite3_compileoption_get(tls, v2)
			zOpt = v1
			if !(v1 != uintptr(0)) {
				break
			}
			_sqlite3VdbeLoadString(tls, v, int32(1), zOpt)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), int32(1))
		}
		_sqlite3VdbeReusable(tls, v)
		break
		/*
		 **   PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
		 **
		 ** Checkpoint the database.
		 */
		fallthrough
	case int32(PragTyp_WAL_CHECKPOINT):
		if (*TToken)(unsafe.Pointer(pId2)).Fz != 0 {
			v2 = iDb
		} else {
			v2 = libc.Int32FromInt32(SQLITE_MAX_ATTACHED) + libc.Int32FromInt32(2)
		}
		iBt = v2
		eMode2 = SQLITE_CHECKPOINT_PASSIVE
		if zRight != 0 {
			if _sqlite3StrICmp(tls, zRight, __ccgo_ts+20250) == 0 {
				eMode2 = int32(SQLITE_CHECKPOINT_FULL)
			} else {
				if _sqlite3StrICmp(tls, zRight, __ccgo_ts+21109) == 0 {
					eMode2 = int32(SQLITE_CHECKPOINT_RESTART)
				} else {
					if _sqlite3StrICmp(tls, zRight, __ccgo_ts+20403) == 0 {
						eMode2 = int32(SQLITE_CHECKPOINT_TRUNCATE)
					} else {
						if _sqlite3StrICmp(tls, zRight, __ccgo_ts+21117) == 0 {
							eMode2 = -int32(1)
						}
					}
				}
			}
		}
		(*TParse)(unsafe.Pointer(pParse)).FnMem = int32(3)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Checkpoint), iBt, eMode2, int32(1))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), int32(1), int32(3))
		break
		/*
		 **   PRAGMA wal_autocheckpoint
		 **   PRAGMA wal_autocheckpoint = N
		 **
		 ** Configure a database connection to automatically checkpoint a database
		 ** after accumulating N frames in the log. Or query for the current value
		 ** of N.
		 */
		fallthrough
	case int32(PragTyp_WAL_AUTOCHECKPOINT):
		if zRight != 0 {
			Xsqlite3_wal_autocheckpoint(tls, db, _sqlite3Atoi(tls, zRight))
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).FxWalCallback == __ccgo_fp(_sqlite3WalDefaultHook) {
			v2 = int32((*Tsqlite3)(unsafe.Pointer(db)).FpWalArg)
		} else {
			v2 = 0
		}
		_returnSingleInt(tls, v, int64(v2))
		break
		/*
		 **  PRAGMA shrink_memory
		 **
		 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
		 ** connection on which it is invoked to free up as much memory as it
		 ** can, by calling sqlite3_db_release_memory().
		 */
		fallthrough
	case int32(PragTyp_SHRINK_MEMORY):
		Xsqlite3_db_release_memory(tls, db)
		break
		/*
		 **  PRAGMA optimize
		 **  PRAGMA optimize(MASK)
		 **  PRAGMA schema.optimize
		 **  PRAGMA schema.optimize(MASK)
		 **
		 ** Attempt to optimize the database.  All schemas are optimized in the first
		 ** two forms, and only the specified schema is optimized in the latter two.
		 **
		 ** The details of optimizations performed by this pragma are expected
		 ** to change and improve over time.  Applications should anticipate that
		 ** this pragma will perform new optimizations in future releases.
		 **
		 ** The optional argument is a bitmask of optimizations to perform:
		 **
		 **    0x00001    Debugging mode.  Do not actually perform any optimizations
		 **               but instead return one line of text for each optimization
		 **               that would have been done.  Off by default.
		 **
		 **    0x00002    Run ANALYZE on tables that might benefit.  On by default.
		 **               See below for additional information.
		 **
		 **    0x00010    Run all ANALYZE operations using an analysis_limit that
		 **               is the lessor of the current analysis_limit and the
		 **               SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option.
		 **               The default value of SQLITE_DEFAULT_OPTIMIZE_LIMIT is
		 **               currently (2024-02-19) set to 2000, which is such that
		 **               the worst case run-time for PRAGMA optimize on a 100MB
		 **               database will usually be less than 100 milliseconds on
		 **               a RaspberryPI-4 class machine.  On by default.
		 **
		 **    0x10000    Look at tables to see if they need to be reanalyzed
		 **               due to growth or shrinkage even if they have not been
		 **               queried during the current connection.  Off by default.
		 **
		 ** The default MASK is and always shall be 0x0fffe.  In the current
		 ** implementation, the default mask only covers the 0x00002 optimization,
		 ** though additional optimizations that are covered by 0x0fffe might be
		 ** added in the future.  Optimizations that are off by default and must
		 ** be explicitly requested have masks of 0x10000 or greater.
		 **
		 ** DETERMINATION OF WHEN TO RUN ANALYZE
		 **
		 ** In the current implementation, a table is analyzed if only if all of
		 ** the following are true:
		 **
		 ** (1) MASK bit 0x00002 is set.
		 **
		 ** (2) The table is an ordinary table, not a virtual table or view.
		 **
		 ** (3) The table name does not begin with "sqlite_".
		 **
		 ** (4) One or more of the following is true:
		 **      (4a) The 0x10000 MASK bit is set.
		 **      (4b) One or more indexes on the table lacks an entry
		 **           in the sqlite_stat1 table.
		 **      (4c) The query planner used sqlite_stat1-style statistics for one
		 **           or more indexes of the table at some point during the lifetime
		 **           of the current connection.
		 **
		 ** (5) One or more of the following is true:
		 **      (5a) One or more indexes on the table lacks an entry
		 **           in the sqlite_stat1 table.  (Same as 4a)
		 **      (5b) The number of rows in the table has increased or decreased by
		 **           10-fold.  In other words, the current size of the table is
		 **           10 times larger than the size in sqlite_stat1 or else the
		 **           current size is less than 1/10th the size in sqlite_stat1.
		 **
		 ** The rules for when tables are analyzed are likely to change in
		 ** future releases.  Future versions of SQLite might accept a string
		 ** literal argument to this pragma that contains a mnemonic description
		 ** of the options rather than a bitmap.
		 */
		fallthrough
	case int32(PragTyp_OPTIMIZE): /* Analysis limit to use */
		nCheck = 0 /* Number of tables to be optimized */
		nBtree = 0 /* Number of indexes on the current table */
		if zRight != 0 {
			opMask = uint32(_sqlite3Atoi(tls, zRight))
			if opMask&uint32(0x02) == uint32(0) {
				break
			}
		} else {
			opMask = uint32(0xfffe)
		}
		if opMask&uint32(0x10) == uint32(0) {
			nLimit = 0
		} else {
			if (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit > 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit < int32(SQLITE_DEFAULT_OPTIMIZE_LIMIT) {
				nLimit = 0
			} else {
				nLimit = int32(SQLITE_DEFAULT_OPTIMIZE_LIMIT)
			}
		}
		v1 = pParse + 44
		v2 = *(*int32)(unsafe.Pointer(v1))
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		iTabCur = v2
		if zDb != 0 {
			v2 = iDb
		} else {
			v2 = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1)
		}
		iDbLast = v2
		for {
			if !(iDb <= iDbLast) {
				break
			}
			if iDb == int32(1) {
				goto _71
			}
			_sqlite3CodeVerifySchema(tls, pParse, iDb)
			pSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema
			k4 = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst
			for {
				if !(k4 != 0) {
					break
				}
				pTab12 = (*THashElem)(unsafe.Pointer(k4)).Fdata
				/* This only works for ordinary tables */
				if !(int32((*TTable)(unsafe.Pointer(pTab12)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
					goto _73
				}
				/* Do not scan system tables */
				if 0 == Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab12)).FzName, __ccgo_ts+7973, int32(7)) {
					goto _73
				}
				/* Find the size of the table as last recorded in sqlite_stat1.
				 ** If any index is unanalyzed, then the threshold is -1 to
				 ** indicate a new, unanalyzed index
				 */
				szThreshold = (*TTable)(unsafe.Pointer(pTab12)).FnRowLogEst
				nIndex = 0
				pIdx7 = (*TTable)(unsafe.Pointer(pTab12)).FpIndex
				for {
					if !(pIdx7 != 0) {
						break
					}
					nIndex = nIndex + 1
					if !(int32(uint32(*(*uint16)(unsafe.Pointer(pIdx7 + 56))&0x80>>7)) != 0) {
						szThreshold = int16(-int32(1)) /* Always analyze if any index lacks statistics */
					}
					goto _74
				_74:
					;
					pIdx7 = (*TIndex)(unsafe.Pointer(pIdx7)).FpNext
				}
				/* If table pTab has not been used in a way that would benefit from
				 ** having analysis statistics during the current session, then skip it,
				 ** unless the 0x10000 MASK bit is set. */
				if (*TTable)(unsafe.Pointer(pTab12)).FtabFlags&uint32(TF_MaybeReanalyze) != uint32(0) {
					/* Check for size change if stat1 has been used for a query */
				} else {
					if opMask&uint32(0x10000) != 0 {
						/* Check for size change if 0x10000 is set */
					} else {
						if (*TTable)(unsafe.Pointer(pTab12)).FpIndex != uintptr(0) && int32(szThreshold) < 0 {
							/* Do analysis if unanalyzed indexes exists */
						} else {
							/* Otherwise, we can skip this table */
							goto _73
						}
					}
				}
				nCheck = nCheck + 1
				if nCheck == int32(2) {
					/* If ANALYZE might be invoked two or more times, hold a write
					 ** transaction for efficiency */
					_sqlite3BeginWriteOperation(tls, pParse, 0, iDb)
				}
				nBtree = nBtree + (nIndex + int32(1))
				/* Reanalyze if the table is 10 times larger or smaller than
				 ** the last analysis.  Unconditional reanalysis if there are
				 ** unanalyzed indexes. */
				_sqlite3OpenTable(tls, pParse, iTabCur, iDb, pTab12, int32(OP_OpenRead))
				if int32(szThreshold) >= 0 {
					iRange = int16(33) /* 10x size change */
					if int32(szThreshold) >= int32(iRange) {
						v2 = int32(szThreshold) - int32(iRange)
					} else {
						v2 = -int32(1)
					}
					_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IfSizeBetween), iTabCur, int32(uint32(_sqlite3VdbeCurrentAddr(tls, v)+int32(2))+opMask&uint32(1)), v2, int32(szThreshold)+int32(iRange))
				} else {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iTabCur, int32(uint32(_sqlite3VdbeCurrentAddr(tls, v)+int32(2))+opMask&uint32(1)))
				}
				zSubSql = _sqlite3MPrintf(tls, db, __ccgo_ts+21122, libc.VaList(bp+136, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TTable)(unsafe.Pointer(pTab12)).FzName))
				if opMask&uint32(0x01) != 0 {
					r11 = _sqlite3GetTempReg(tls, pParse)
					_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, r11, 0, zSubSql, -int32(7))
					_sqlite3VdbeAddOp2(tls, v, int32(OP_ResultRow), r11, int32(1))
				} else {
					if nLimit != 0 {
						v2 = int32(0x02)
					} else {
						v2 = 00
					}
					_sqlite3VdbeAddOp4(tls, v, int32(OP_SqlExec), v2, nLimit, 0, zSubSql, -int32(7))
				}
				goto _73
			_73:
				;
				k4 = (*THashElem)(unsafe.Pointer(k4)).Fnext
			}
			goto _71
		_71:
			;
			iDb = iDb + 1
		}
		_sqlite3VdbeAddOp0(tls, v, int32(OP_Expire))
		/* In a schema with a large number of tables and indexes, scale back
		 ** the analysis_limit to avoid excess run-time in the worst case.
		 */
		if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) && nLimit > 0 && nBtree > int32(100) {
			nLimit = int32(100) * nLimit / nBtree
			if nLimit < int32(100) {
				nLimit = int32(100)
			}
			aOp5 = _sqlite3VdbeGetOp(tls, v, 0)
			iEnd = _sqlite3VdbeCurrentAddr(tls, v)
			iAddr1 = 0
			for {
				if !(iAddr1 < iEnd) {
					break
				}
				if int32((**(**TVdbeOp)(__ccgo_up(aOp5 + uintptr(iAddr1)*20))).Fopcode) == int32(OP_SqlExec) {
					(**(**TVdbeOp)(__ccgo_up(aOp5 + uintptr(iAddr1)*20))).Fp2 = nLimit
				}
				goto _77
			_77:
				;
				iAddr1 = iAddr1 + 1
			}
		}
		break
		/*
		 **   PRAGMA busy_timeout
		 **   PRAGMA busy_timeout = N
		 **
		 ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
		 ** if one is set.  If no busy handler or a different busy handler is set
		 ** then 0 is returned.  Setting the busy_timeout to 0 or negative
		 ** disables the timeout.
		 */
		/*case PragTyp_BUSY_TIMEOUT*/
		fallthrough
	default:
		if zRight != 0 {
			Xsqlite3_busy_timeout(tls, db, _sqlite3Atoi(tls, zRight))
		}
		_returnSingleInt(tls, v, int64((*Tsqlite3)(unsafe.Pointer(db)).FbusyTimeout))
		break
		/*
		 **   PRAGMA soft_heap_limit
		 **   PRAGMA soft_heap_limit = N
		 **
		 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
		 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
		 ** specified and is a non-negative integer.
		 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
		 ** returns the same integer that would be returned by the
		 ** sqlite3_soft_heap_limit64(-1) C-language function.
		 */
		fallthrough
	case int32(PragTyp_SOFT_HEAP_LIMIT):
		if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+96) == SQLITE_OK {
			Xsqlite3_soft_heap_limit64(tls, **(**Tsqlite3_int64)(__ccgo_up(bp + 96)))
		}
		_returnSingleInt(tls, v, Xsqlite3_soft_heap_limit64(tls, int64(-int32(1))))
		break
		/*
		 **   PRAGMA hard_heap_limit
		 **   PRAGMA hard_heap_limit = N
		 **
		 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
		 ** limit.  The hard heap limit can be activated or lowered by this
		 ** pragma, but not raised or deactivated.  Only the
		 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
		 ** the hard heap limit.  This allows an application to set a heap limit
		 ** constraint that cannot be relaxed by an untrusted SQL script.
		 */
		fallthrough
	case int32(PragTyp_HARD_HEAP_LIMIT):
		if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+104) == SQLITE_OK {
			iPrior = Xsqlite3_hard_heap_limit64(tls, int64(-int32(1)))
			if **(**Tsqlite3_int64)(__ccgo_up(bp + 104)) > 0 && (iPrior == 0 || iPrior > **(**Tsqlite3_int64)(__ccgo_up(bp + 104))) {
				Xsqlite3_hard_heap_limit64(tls, **(**Tsqlite3_int64)(__ccgo_up(bp + 104)))
			}
		}
		_returnSingleInt(tls, v, Xsqlite3_hard_heap_limit64(tls, int64(-int32(1))))
		break
		/*
		 **   PRAGMA threads
		 **   PRAGMA threads = N
		 **
		 ** Configure the maximum number of worker threads.  Return the new
		 ** maximum, which might be less than requested.
		 */
		fallthrough
	case int32(PragTyp_THREADS):
		if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+112) == SQLITE_OK && **(**Tsqlite3_int64)(__ccgo_up(bp + 112)) >= 0 {
			Xsqlite3_limit(tls, db, int32(SQLITE_LIMIT_WORKER_THREADS), int32(**(**Tsqlite3_int64)(__ccgo_up(bp + 112))&libc.Int64FromInt32(0x7fffffff)))
		}
		_returnSingleInt(tls, v, int64(Xsqlite3_limit(tls, db, int32(SQLITE_LIMIT_WORKER_THREADS), -int32(1))))
		break
		/*
		 **   PRAGMA analysis_limit
		 **   PRAGMA analysis_limit = N
		 **
		 ** Configure the maximum number of rows that ANALYZE will examine
		 ** in each index that it looks at.  Return the new limit.
		 */
		fallthrough
	case int32(PragTyp_ANALYSIS_LIMIT):
		if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+120) == SQLITE_OK && **(**Tsqlite3_int64)(__ccgo_up(bp + 120)) >= 0 {
			(*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit = int32(**(**Tsqlite3_int64)(__ccgo_up(bp + 120)) & libc.Int64FromInt32(0x7fffffff))
		}
		_returnSingleInt(tls, v, int64((*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit)) /* IMP: R-57594-65522 */
		break
	} /* End of the PRAGMA switch */
	/* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
	 ** purpose is to execute assert() statements to verify that if the
	 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
	 ** to the PRAGMA, the implementation has not added any OP_ResultRow
	 ** instructions to the VM.  */
	if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&int32(PragFlg_NoColumns1) != 0 && zRight != 0 {
	}
	goto pragma_out
pragma_out:
	;
	_sqlite3DbFree(tls, db, zLeft)
	_sqlite3DbFree(tls, db, zRight)
}

// C documentation
//
//	/*
//	** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
//	*/
func _sqlite3Prepare(tls *libc.TLS, db uintptr, zSql uintptr, nBytes int32, prepFlags Tu32, pReprepare uintptr, ppStmt uintptr, pzTail uintptr) (r int32) {
	bp := tls.Alloc(304)
	defer tls.Free(304)
	var i, mxLen, rc, v1 int32
	var pBt, pT, zDb, zSqlCopy uintptr
	var _ /* sParse at bp+0 */ TParse
	_, _, _, _, _, _, _, _ = i, mxLen, pBt, pT, rc, zDb, zSqlCopy, v1
	rc = SQLITE_OK /* Parsing context */
	/* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */
	libc.Xmemset(tls, bp+uintptr(uint32(libc.UintptrFromInt32(0)+4)), 0, uint32(libc.UintptrFromInt32(0)+136)-uint32(libc.UintptrFromInt32(0)+4))
	libc.Xmemset(tls, bp+uintptr(uint32(libc.UintptrFromInt32(0)+208)), 0, libc.Uint32FromInt64(284)-uint32(libc.UintptrFromInt32(0)+208))
	(**(**TParse)(__ccgo_up(bp))).FpOuterParse = (*Tsqlite3)(unsafe.Pointer(db)).FpParse
	(*Tsqlite3)(unsafe.Pointer(db)).FpParse = bp
	(**(**TParse)(__ccgo_up(bp))).Fdb = db
	if pReprepare != 0 {
		(**(**TParse)(__ccgo_up(bp))).FpReprepare = pReprepare
		(**(**TParse)(__ccgo_up(bp))).Fexplain = uint8(Xsqlite3_stmt_isexplain(tls, pReprepare))
	} else {
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_sqlite3ErrorMsg(tls, bp, __ccgo_ts+1681, 0)
		v1 = libc.Int32FromInt32(SQLITE_NOMEM)
		rc = v1
		(*Tsqlite3)(unsafe.Pointer(db)).FerrCode = v1
		goto end_prepare
	}
	/* For a long-term use prepared statement avoid the use of
	 ** lookaside memory.
	 */
	if prepFlags&uint32(SQLITE_PREPARE_PERSISTENT) != 0 {
		(**(**TParse)(__ccgo_up(bp))).FdisableLookaside = (**(**TParse)(__ccgo_up(bp))).FdisableLookaside + 1
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable + 1
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0)
	}
	(**(**TParse)(__ccgo_up(bp))).FprepFlags = uint8(prepFlags & uint32(0xff))
	/* Check to verify that it is possible to get a read lock on all
	 ** database schemas.  The inability to get a read lock indicates that
	 ** some other database connection is holding a write-lock, which in
	 ** turn means that the other connection has made uncommitted changes
	 ** to the schema.
	 **
	 ** Were we to proceed and prepare the statement against the uncommitted
	 ** schema changes and if those schema changes are subsequently rolled
	 ** back and different changes are made in their place, then when this
	 ** prepared statement goes to run the schema cookie would fail to detect
	 ** the schema change.  Disaster would follow.
	 **
	 ** This thread is currently holding mutexes on all Btrees (because
	 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
	 ** is not possible for another thread to start a new schema change
	 ** while this routine is running.  Hence, we do not need to hold
	 ** locks on the schema, we just need to make sure nobody else is
	 ** holding them.
	 **
	 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
	 ** but it does *not* override schema lock detection, so this all still
	 ** works even if READ_UNCOMMITTED is set.
	 */
	if !((*Tsqlite3)(unsafe.Pointer(db)).FnoSharedCache != 0) {
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
			if pBt != 0 {
				rc = _sqlite3BtreeSchemaLocked(tls, pBt)
				if rc != 0 {
					zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FzDbSName
					_sqlite3ErrorWithMsg(tls, db, rc, __ccgo_ts+21469, libc.VaList(bp+296, zDb))
					goto end_prepare
				}
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FpDisconnect != 0 {
		_sqlite3VtabUnlockList(tls, db)
	}
	if nBytes >= 0 && (nBytes == 0 || int32(**(**int8)(__ccgo_up(zSql + uintptr(nBytes-int32(1))))) != 0) {
		mxLen = **(**int32)(__ccgo_up(db + 120 + 1*4))
		if nBytes > mxLen {
			_sqlite3ErrorWithMsg(tls, db, int32(SQLITE_TOOBIG), __ccgo_ts+21499, 0)
			rc = _sqlite3ApiExit(tls, db, int32(SQLITE_TOOBIG))
			goto end_prepare
		}
		zSqlCopy = _sqlite3DbStrNDup(tls, db, zSql, uint64(nBytes))
		if zSqlCopy != 0 {
			_sqlite3RunParser(tls, bp, zSqlCopy)
			(**(**TParse)(__ccgo_up(bp))).FzTail = zSql + uintptr(int32((**(**TParse)(__ccgo_up(bp))).FzTail)-int32(zSqlCopy))
			_sqlite3DbFree(tls, db, zSqlCopy)
		} else {
			(**(**TParse)(__ccgo_up(bp))).FzTail = zSql + uintptr(nBytes)
		}
	} else {
		_sqlite3RunParser(tls, bp, zSql)
	}
	if pzTail != 0 {
		**(**uintptr)(__ccgo_up(pzTail)) = (**(**TParse)(__ccgo_up(bp))).FzTail
	}
	if int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 {
		_sqlite3VdbeSetSql(tls, (**(**TParse)(__ccgo_up(bp))).FpVdbe, zSql, int32((**(**TParse)(__ccgo_up(bp))).FzTail)-int32(zSql), uint8(prepFlags))
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		(**(**TParse)(__ccgo_up(bp))).Frc = int32(SQLITE_NOMEM)
		libc.SetBitFieldPtr16Uint32(bp+28, libc.Uint32FromInt32(0), 8, 0x100)
	}
	if (**(**TParse)(__ccgo_up(bp))).Frc != SQLITE_OK && (**(**TParse)(__ccgo_up(bp))).Frc != int32(SQLITE_DONE) {
		if int32(Tbft(*(*uint16)(unsafe.Pointer(bp + 28))&0x100>>8)) != 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 {
			_schemaIsValid(tls, bp)
		}
		if (**(**TParse)(__ccgo_up(bp))).FpVdbe != 0 {
			_sqlite3VdbeFinalize(tls, (**(**TParse)(__ccgo_up(bp))).FpVdbe)
		}
		rc = (**(**TParse)(__ccgo_up(bp))).Frc
		if (**(**TParse)(__ccgo_up(bp))).FzErrMsg != 0 {
			_sqlite3ErrorWithMsg(tls, db, rc, __ccgo_ts+4729, libc.VaList(bp+296, (**(**TParse)(__ccgo_up(bp))).FzErrMsg))
			_sqlite3DbFree(tls, db, (**(**TParse)(__ccgo_up(bp))).FzErrMsg)
		} else {
			_sqlite3Error(tls, db, rc)
		}
	} else {
		**(**uintptr)(__ccgo_up(ppStmt)) = (**(**TParse)(__ccgo_up(bp))).FpVdbe
		rc = SQLITE_OK
		_sqlite3ErrorClear(tls, db)
	}
	/* Delete any TriggerPrg structures allocated while parsing this statement. */
	for (**(**TParse)(__ccgo_up(bp))).FpTriggerPrg != 0 {
		pT = (**(**TParse)(__ccgo_up(bp))).FpTriggerPrg
		(**(**TParse)(__ccgo_up(bp))).FpTriggerPrg = (*TTriggerPrg)(unsafe.Pointer(pT)).FpNext
		_sqlite3DbFree(tls, db, pT)
	}
	goto end_prepare
end_prepare:
	;
	_sqlite3ParseObjectReset(tls, bp)
	return rc
}

// C documentation
//
//	/*
//	** This routine processes the join information for a SELECT statement.
//	**
//	**   *  A NATURAL join is converted into a USING join.  After that, we
//	**      do not need to be concerned with NATURAL joins and we only have
//	**      think about USING joins.
//	**
//	**   *  ON and USING clauses result in extra terms being added to the
//	**      WHERE clause to enforce the specified constraints.  The extra
//	**      WHERE clause terms will be tagged with EP_OuterON or
//	**      EP_InnerON so that we know that they originated in ON/USING.
//	**
//	** The terms of a FROM clause are contained in the Select.pSrc structure.
//	** The left most table is the first entry in Select.pSrc.  The right-most
//	** table is the last entry.  The join operator is held in the entry to
//	** the right.  Thus entry 1 contains the join operator for the join between
//	** entries 0 and 1.  Any ON or USING clauses associated with the join are
//	** also attached to the right entry.
//	**
//	** This routine returns the number of errors encountered.
//	*/
func _sqlite3ProcessJoin(tls *libc.TLS, pParse uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pE1, pE2, pEq, pFuncArgs, pLeft, pList, pRight, pRightTab, pSrc, pUsing, zName, zName1 uintptr
	var i, iRightCol, j, v2 int32
	var joinType Tu32
	var _ /* iLeft at bp+0 */ int32
	var _ /* iLeftCol at bp+4 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, i, iRightCol, j, joinType, pE1, pE2, pEq, pFuncArgs, pLeft, pList, pRight, pRightTab, pSrc, pUsing, zName, zName1, v2 /* Right table being joined */
	pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc
	pLeft = pSrc + 8
	pRight = pLeft + 1*48
	i = 0
	for {
		if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc-int32(1)) {
			break
		}
		pRightTab = (*TSrcItem)(unsafe.Pointer(pRight)).FpSTab
		if (*TSrcItem)(unsafe.Pointer(pLeft)).FpSTab == uintptr(0) || pRightTab == uintptr(0) {
			goto _1
		}
		if int32((*TSrcItem)(unsafe.Pointer(pRight)).Ffg.Fjointype)&int32(JT_OUTER) != 0 {
			v2 = int32(EP_OuterON)
		} else {
			v2 = int32(EP_InnerON)
		}
		joinType = uint32(v2)
		/* If this is a NATURAL join, synthesize an appropriate USING clause
		 ** to specify which columns should be joined.
		 */
		if int32((*TSrcItem)(unsafe.Pointer(pRight)).Ffg.Fjointype)&int32(JT_NATURAL) != 0 {
			pUsing = uintptr(0)
			if int32(*(*uint32)(unsafe.Pointer(pRight + 12 + 4))&0x800>>11) != 0 || *(*uintptr)(unsafe.Pointer(pRight + 40)) != 0 {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21548, libc.VaList(bp+16, 0))
				return int32(1)
			}
			j = 0
			for {
				if !(j < int32((*TTable)(unsafe.Pointer(pRightTab)).FnCol)) {
					break
				} /* Name of column in the right table */
				if int32((*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pRightTab)).FaCol+uintptr(j)*12)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0 {
					goto _3
				}
				zName = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pRightTab)).FaCol + uintptr(j)*12))).FzCnName
				if _tableAndColumnIndex(tls, pSrc, 0, i, zName, uintptr(0), uintptr(0), int32(1)) != 0 {
					pUsing = _sqlite3IdListAppend(tls, pParse, pUsing, uintptr(0))
					if pUsing != 0 {
						(*(*TIdList_item)(unsafe.Pointer(pUsing + 4 + uintptr((*TIdList)(unsafe.Pointer(pUsing)).FnId-int32(1))*4))).FzName = _sqlite3DbStrDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zName)
					}
				}
				goto _3
			_3:
				;
				j = j + 1
			}
			if pUsing != 0 {
				libc.SetBitFieldPtr32Uint32(pRight+12+4, libc.Uint32FromInt32(1), 11, 0x800)
				libc.SetBitFieldPtr32Uint32(pRight+12+4, libc.Uint32FromInt32(1), 13, 0x2000)
				*(*uintptr)(unsafe.Pointer(pRight + 40)) = pUsing
			}
			if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
				return int32(1)
			}
		}
		/* Create extra terms on the WHERE clause for each column named
		 ** in the USING clause.  Example: If the two tables to be joined are
		 ** A and B and the USING clause names X, Y, and Z, then add this
		 ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
		 ** Report an error if any column mentioned in the USING clause is
		 ** not contained in both tables to be joined.
		 */
		if int32(*(*uint32)(unsafe.Pointer(pRight + 12 + 4))&0x800>>11) != 0 {
			pList = *(*uintptr)(unsafe.Pointer(pRight + 40))
			db = (*TParse)(unsafe.Pointer(pParse)).Fdb
			j = 0
			for {
				if !(j < (*TIdList)(unsafe.Pointer(pList)).FnId) {
					break
				} /* Equality constraint.  pE1 == pE2 */
				zName1 = (*(*TIdList_item)(unsafe.Pointer(pList + 4 + uintptr(j)*4))).FzName
				iRightCol = _sqlite3ColumnIndex(tls, pRightTab, zName1)
				if iRightCol < 0 || _tableAndColumnIndex(tls, pSrc, 0, i, zName1, bp, bp+4, int32(*(*uint32)(unsafe.Pointer(pRight + 12 + 4))&0x2000>>13)) == 0 {
					_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21598, libc.VaList(bp+16, zName1))
					return int32(1)
				}
				pE1 = _sqlite3CreateColumnExpr(tls, db, pSrc, **(**int32)(__ccgo_up(bp)), **(**int32)(__ccgo_up(bp + 4)))
				_sqlite3SrcItemColumnUsed(tls, pSrc+8+uintptr(**(**int32)(__ccgo_up(bp)))*48, **(**int32)(__ccgo_up(bp + 4)))
				if int32((*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 && (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
					/* This branch runs if the query contains one or more RIGHT or FULL
					 ** JOINs.  If only a single table on the left side of this join
					 ** contains the zName column, then this branch is a no-op.
					 ** But if there are two or more tables on the left side
					 ** of the join, construct a coalesce() function that gathers all
					 ** such tables.  Raise an error if more than one of those references
					 ** to zName is not also within a prior USING clause.
					 **
					 ** We really ought to raise an error if there are two or more
					 ** non-USING references to zName on the left of an INNER or LEFT
					 ** JOIN.  But older versions of SQLite do not do that, so we avoid
					 ** adding a new error so as to not break legacy applications.
					 */
					pFuncArgs = uintptr(0) /* Arguments to the coalesce() */
					**(**Tu32)(__ccgo_up(pE1 + 4)) |= uint32(libc.Int32FromInt32(EP_CanBeNull))
					for _tableAndColumnIndex(tls, pSrc, **(**int32)(__ccgo_up(bp))+int32(1), i, zName1, bp, bp+4, int32(*(*uint32)(unsafe.Pointer(pRight + 12 + 4))&0x2000>>13)) != 0 {
						if int32(*(*uint32)(unsafe.Pointer(pSrc + 8 + uintptr(**(**int32)(__ccgo_up(bp)))*48 + 12 + 4))&0x800>>11) == 0 || _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(**(**int32)(__ccgo_up(bp)))*48 + 40)), zName1) < 0 {
							_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21662, libc.VaList(bp+16, zName1))
							break
						}
						pFuncArgs = _sqlite3ExprListAppend(tls, pParse, pFuncArgs, pE1)
						pE1 = _sqlite3CreateColumnExpr(tls, db, pSrc, **(**int32)(__ccgo_up(bp)), **(**int32)(__ccgo_up(bp + 4)))
						_sqlite3SrcItemColumnUsed(tls, pSrc+8+uintptr(**(**int32)(__ccgo_up(bp)))*48, **(**int32)(__ccgo_up(bp + 4)))
					}
					if pFuncArgs != 0 {
						pFuncArgs = _sqlite3ExprListAppend(tls, pParse, pFuncArgs, pE1)
						pE1 = _sqlite3ExprFunction(tls, pParse, pFuncArgs, uintptr(unsafe.Pointer(&_tkCoalesce)), 0)
						if pE1 != 0 {
							(*TExpr)(unsafe.Pointer(pE1)).FaffExpr = int8(SQLITE_AFF_DEFER)
						}
					}
				} else {
					if int32((*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i+int32(1))*48))).Ffg.Fjointype)&int32(JT_LEFT) != 0 && (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
						**(**Tu32)(__ccgo_up(pE1 + 4)) |= uint32(libc.Int32FromInt32(EP_CanBeNull))
					}
				}
				pE2 = _sqlite3CreateColumnExpr(tls, db, pSrc, i+int32(1), iRightCol)
				_sqlite3SrcItemColumnUsed(tls, pRight, iRightCol)
				pEq = _sqlite3PExpr(tls, pParse, int32(TK_EQ), pE1, pE2)
				if pEq != 0 {
					**(**Tu32)(__ccgo_up(pEq + 4)) |= joinType
					*(*int32)(unsafe.Pointer(pEq + 36)) = (*TExpr)(unsafe.Pointer(pE2)).FiTable
				}
				(*TSelect)(unsafe.Pointer(p)).FpWhere = _sqlite3ExprAnd(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpWhere, pEq)
				goto _4
			_4:
				;
				j = j + 1
			}
		} else {
			if *(*uintptr)(unsafe.Pointer(pRight + 40)) != 0 {
				_sqlite3SetJoinExpr(tls, *(*uintptr)(unsafe.Pointer(pRight + 40)), (*TSrcItem)(unsafe.Pointer(pRight)).FiCursor, joinType)
				(*TSelect)(unsafe.Pointer(p)).FpWhere = _sqlite3ExprAnd(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpWhere, *(*uintptr)(unsafe.Pointer(pRight + 40)))
				*(*uintptr)(unsafe.Pointer(pRight + 40)) = uintptr(0)
				libc.SetBitFieldPtr32Uint32(pRight+12+4, libc.Uint32FromInt32(1), 12, 0x1000)
				**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_OnToWhere)
			}
		}
		if int32((*TTable)(unsafe.Pointer(pRightTab)).FeTabType) == int32(TABTYP_VTAB) && joinType == uint32(EP_OuterON) && *(*uintptr)(unsafe.Pointer(pRight + 32)) != 0 {
			**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_OnToWhere)
		}
		goto _1
	_1:
		;
		i = i + 1
		pRight += 48
		pLeft += 48
	}
	return 0
}

// C documentation
//
//	/*
//	** If pExpr has a byte offset for the start of a token, record that as
//	** as the error offset.
//	*/
func _sqlite3RecordErrorOffsetOfExpr(tls *libc.TLS, db uintptr, pExpr uintptr) {
	for pExpr != 0 && ((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) || *(*int32)(unsafe.Pointer(pExpr + 36)) <= 0) {
		pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
	}
	if pExpr == uintptr(0) {
		return
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_FromDDL)) != uint32(0) {
		return
	}
	(*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = *(*int32)(unsafe.Pointer(pExpr + 36))
}

// C documentation
//
//	/*
//	** Check to see if pExpr references any tables in pSrcList.
//	** Possible return values:
//	**
//	**    1         pExpr does references a table in pSrcList.
//	**
//	**    0         pExpr references some table that is not defined in either
//	**              pSrcList or in subqueries of pExpr itself.
//	**
//	**   -1         pExpr only references no tables at all, or it only
//	**              references tables defined in subqueries of pExpr itself.
//	**
//	** As currently used, pExpr is always an aggregate function call.  That
//	** fact is exploited for efficiency.
//	*/
func _sqlite3ReferencesSrcList(tls *libc.TLS, pParse uintptr, pExpr uintptr, pSrcList uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var _ /* w at bp+0 */ TWalker
	var _ /* x at bp+32 */ TRefSrcList
	libc.Xmemset(tls, bp, 0, uint32(28))
	libc.Xmemset(tls, bp+32, 0, uint32(24))
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_exprRefToSrcList)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_selectRefEnter)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback2 = __ccgo_fp(_selectRefLeave)
	*(*uintptr)(unsafe.Pointer(bp + 24)) = bp + 32
	(**(**TRefSrcList)(__ccgo_up(bp + 32))).Fdb = (*TParse)(unsafe.Pointer(pParse)).Fdb
	(**(**TRefSrcList)(__ccgo_up(bp + 32))).FpRef = pSrcList
	_sqlite3WalkExprList(tls, bp, *(*uintptr)(unsafe.Pointer(pExpr + 20)))
	if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 {
		_sqlite3WalkExprList(tls, bp, *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 20)))
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
		_sqlite3WalkExpr(tls, bp, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)))).FpFilter)
	}
	if (**(**TRefSrcList)(__ccgo_up(bp + 32))).FaiExclude != 0 {
		_sqlite3DbNNFreeNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (**(**TRefSrcList)(__ccgo_up(bp + 32))).FaiExclude)
	}
	if int32((**(**TWalker)(__ccgo_up(bp))).FeCode)&int32(0x01) != 0 {
		return int32(1)
	} else {
		if (**(**TWalker)(__ccgo_up(bp))).FeCode != 0 {
			return 0
		} else {
			return -int32(1)
		}
	}
	return r
}

// C documentation
//
//	/*
//	** Generate code that will erase and refill index *pIdx.  This is
//	** used to initialize a newly created index or to recompute the
//	** content of an index in response to a REINDEX command.
//	**
//	** if memRootPage is not negative, it means that the index is newly
//	** created.  The register specified by memRootPage contains the
//	** root page number of the index.  If memRootPage is negative, then
//	** the index already exists and must be cleared before being refilled and
//	** the root page number of the index is taken from pIndex->tnum.
//	*/
func _sqlite3RefillIndex(tls *libc.TLS, pParse uintptr, pIndex uintptr, memRootPage int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var addr1, addr2, iDb, iIdx, iSorter, iTab, j2, regRecord, v1, v3 int32
	var db, pKey, pTab, v, v2, v4 uintptr
	var tnum TPgno
	var _ /* iPartIdxLabel at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, addr2, db, iDb, iIdx, iSorter, iTab, j2, pKey, pTab, regRecord, tnum, v, v1, v2, v3, v4
	pTab = (*TIndex)(unsafe.Pointer(pIndex)).FpTable
	v2 = pParse + 44
	v1 = *(*int32)(unsafe.Pointer(v2))
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1 /* The table that is indexed */
	iTab = v1
	v4 = pParse + 44
	v3 = *(*int32)(unsafe.Pointer(v4))
	*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1 /* Btree cursor used for pTab */
	iIdx = v3                                                         /* Register holding assembled index record */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb                        /* The database connection */
	iDb = _sqlite3SchemaToIndex(tls, db, (*TIndex)(unsafe.Pointer(pIndex)).FpSchema)
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_REINDEX), (*TIndex)(unsafe.Pointer(pIndex)).FzName, uintptr(0), (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName) != 0 {
		return
	}
	/* Require a write-lock on the table to perform this operation */
	_sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(1), (*TTable)(unsafe.Pointer(pTab)).FzName)
	v = _sqlite3GetVdbe(tls, pParse)
	if v == uintptr(0) {
		return
	}
	if memRootPage >= 0 {
		tnum = uint32(memRootPage)
	} else {
		tnum = (*TIndex)(unsafe.Pointer(pIndex)).Ftnum
	}
	pKey = _sqlite3KeyInfoOfIndex(tls, pParse, pIndex)
	/* Open the sorter cursor if we are to use one. */
	v2 = pParse + 44
	v1 = *(*int32)(unsafe.Pointer(v2))
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	iSorter = v1
	_sqlite3VdbeAddOp4(tls, v, int32(OP_SorterOpen), iSorter, 0, int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol), _sqlite3KeyInfoRef(tls, pKey), -int32(9))
	/* Open the table. Loop through all rows of the table, inserting index
	 ** records into the sorter. */
	_sqlite3OpenTable(tls, pParse, iTab, iDb, pTab, int32(OP_OpenRead))
	addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iTab, 0)
	regRecord = _sqlite3GetTempReg(tls, pParse)
	_sqlite3MultiWrite(tls, pParse)
	_sqlite3GenerateIndexKey(tls, pParse, pIndex, iTab, regRecord, 0, bp, uintptr(0), 0)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_SorterInsert), iSorter, regRecord)
	_sqlite3ResolvePartIdxLabel(tls, pParse, **(**int32)(__ccgo_up(bp)))
	_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iTab, addr1+int32(1))
	_sqlite3VdbeJumpHere(tls, v, addr1)
	if memRootPage < 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Clear), int32(tnum), iDb)
	}
	_sqlite3VdbeAddOp4(tls, v, int32(OP_OpenWrite), iIdx, int32(tnum), iDb, pKey, -int32(9))
	if memRootPage >= 0 {
		v1 = int32(OPFLAG_P2ISREG)
	} else {
		v1 = 0
	}
	_sqlite3VdbeChangeP5(tls, v, uint16(int32(OPFLAG_BULKCSR)|v1))
	addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_SorterSort), iSorter, 0)
	if int32((*TIndex)(unsafe.Pointer(pIndex)).FonError) != OE_None {
		j2 = _sqlite3VdbeGoto(tls, v, int32(1))
		addr2 = _sqlite3VdbeCurrentAddr(tls, v)
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_SorterCompare), iSorter, j2, regRecord, int32((*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol))
		_sqlite3UniqueConstraint(tls, pParse, int32(OE_Abort), pIndex)
		_sqlite3VdbeJumpHere(tls, v, j2)
	} else {
		/* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
		 ** abort. The exception is if one of the indexed expressions contains a
		 ** user function that throws an exception when it is evaluated. But the
		 ** overhead of adding a statement journal to a CREATE INDEX statement is
		 ** very small (since most of the pages written do not contain content that
		 ** needs to be restored if the statement aborts), so we call
		 ** sqlite3MayAbort() for all CREATE INDEX statements.  */
		_sqlite3MayAbort(tls, pParse)
		addr2 = _sqlite3VdbeCurrentAddr(tls, v)
	}
	_sqlite3VdbeAddOp3(tls, v, int32(OP_SorterData), iSorter, regRecord, iIdx)
	if !(int32(uint32(*(*uint16)(unsafe.Pointer(pIndex + 56))&0x200>>9)) != 0) {
		/* This OP_SeekEnd opcode makes index insert for a REINDEX go much
		 ** faster by avoiding unnecessary seeks.  But the optimization does
		 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
		 ** with DESC primary keys, since those indexes have there keys in
		 ** a different order from the main table.
		 ** See ticket: https://sqlite.org/src/info/bba7b69f9849b5bf
		 */
		_sqlite3VdbeAddOp1(tls, v, int32(OP_SeekEnd), iIdx)
	}
	_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), iIdx, regRecord)
	_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT))
	_sqlite3ReleaseTempReg(tls, pParse, regRecord)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_SorterNext), iSorter, addr2)
	_sqlite3VdbeJumpHere(tls, v, addr1)
	_sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iTab)
	_sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iIdx)
	_sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iSorter)
}

// C documentation
//
//	/*
//	** All of the FuncDef structures in the aBuiltinFunc[] array above
//	** to the global function hash table.  This occurs at start-time (as
//	** a consequence of calling sqlite3_initialize()).
//	**
//	** After this routine runs
//	*/
func _sqlite3RegisterBuiltinFunctions(tls *libc.TLS) {
	_sqlite3AlterFunctions(tls)
	_sqlite3WindowFunctions(tls)
	_sqlite3RegisterDateTimeFunctions(tls)
	_sqlite3RegisterJsonFunctions(tls)
	_sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aBuiltinFunc)), int32(libc.Uint32FromInt64(4240)/libc.Uint32FromInt64(40)))
}

// C documentation
//
//	/*
//	** This function registered all of the above C functions as SQL
//	** functions.  This should be the only routine in this file with
//	** external linkage.
//	*/
func _sqlite3RegisterDateTimeFunctions(tls *libc.TLS) {
	_sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aDateTimeFuncs)), int32(libc.Uint32FromInt64(400)/libc.Uint32FromInt64(40)))
}

// C documentation
//
//	/*
//	** Register JSON functions.
//	*/
func _sqlite3RegisterJsonFunctions(tls *libc.TLS) {
	_sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aJsonFunc)), int32(libc.Uint32FromInt64(1440)/libc.Uint32FromInt64(40)))
}

// C documentation
//
//	/*
//	** Generate code for the REINDEX command.
//	**
//	**        REINDEX                            -- 1
//	**        REINDEX  <collation>               -- 2
//	**        REINDEX  ?<database>.?<indexname>  -- 3
//	**        REINDEX  ?<database>.?<tablename>  -- 4
//	**        REINDEX  EXPRESSIONS               -- 5
//	**
//	** Form 1 causes all indexes in all attached databases to be rebuilt.
//	** Form 2 rebuilds all indexes in all databases that use the named
//	** collating function.  Forms 3 and 4 rebuild the named index or all
//	** indexes associated with the named table, respectively.  Form 5
//	** rebuilds all expression indexes in addition to all collations,
//	** indexes, or tables named "EXPRESSIONS".
//	**
//	** If the name is ambiguous such that it matches two or more of
//	** forms 2 through 5, then rebuild the union of all matching indexes,
//	** taken care to avoid rebuilding the same index more than once.
//	*/
func _sqlite3Reindex(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bAll, bMatch, iDb, iReDb, isExprIdx int32
	var db, k, pDb, pIdx, pReIndex, pReTab, pTab, z, zColl, zDb, v1 uintptr
	var v2 bool
	var _ /* pObjName at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bAll, bMatch, db, iDb, iReDb, isExprIdx, k, pDb, pIdx, pReIndex, pReTab, pTab, z, zColl, zDb, v1, v2
	z = uintptr(0)                             /* Name of a table or index or collation */
	zDb = uintptr(0)                           /* Name of the database */
	iReDb = -int32(1)                          /* The database index number */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Name of the table or index to be reindexed */
	bMatch = 0                                 /* At least one name match */
	zColl = uintptr(0)                         /* Rebuild indexes using this collation */
	pReTab = uintptr(0)                        /* Rebuild all indexes of this table */
	pReIndex = uintptr(0)                      /* Rebuild this index */
	isExprIdx = 0                              /* Rebuild all expression indexes */
	bAll = 0                                   /* Rebuild all indexes */
	/* Read the database schema. If an error occurs, leave an error message
	 ** and code in pParse and return NULL. */
	if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) {
		return
	}
	if pName1 == uintptr(0) {
		/* rebuild all indexes */
		bMatch = int32(1)
		bAll = int32(1)
	} else {
		if pName2 == uintptr(0) || (*TToken)(unsafe.Pointer(pName2)).Fz == uintptr(0) {
			z = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pName1)
			if z == uintptr(0) {
				return
			}
		} else {
			iReDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp)
			if iReDb < 0 {
				return
			}
			z = _sqlite3NameFromToken(tls, db, **(**uintptr)(__ccgo_up(bp)))
			if z == uintptr(0) {
				return
			}
			zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iReDb)*16))).FzDbSName
		}
	}
	if !(bAll != 0) {
		if zDb == uintptr(0) && _sqlite3StrICmp(tls, z, __ccgo_ts+17448) == 0 {
			isExprIdx = int32(1)
			bMatch = int32(1)
		}
		if zDb == uintptr(0) && _sqlite3FindCollSeq(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, z, 0) != uintptr(0) {
			zColl = z
			bMatch = int32(1)
		}
		if v2 = zColl == uintptr(0); v2 {
			v1 = _sqlite3FindTable(tls, db, z, zDb)
			pReTab = v1
		}
		if v2 && v1 != uintptr(0) {
			bMatch = int32(1)
		}
		if v2 = zColl == uintptr(0); v2 {
			v1 = _sqlite3FindIndex(tls, db, z, zDb)
			pReIndex = v1
		}
		if v2 && v1 != uintptr(0) {
			bMatch = int32(1)
		}
	}
	if bMatch != 0 {
		iDb = 0
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb
		for {
			if !(iDb < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			if iReDb >= 0 && iReDb != iDb {
				goto _5
			}
			k = (*THash)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema + 8)).Ffirst
			for {
				if !(k != 0) {
					break
				}
				pTab = (*THashElem)(unsafe.Pointer(k)).Fdata
				if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
					goto _6
				}
				pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
				for {
					if !(pIdx != 0) {
						break
					}
					if bAll != 0 || pTab == pReTab || pIdx == pReIndex || isExprIdx != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x800>>11)) != 0 || zColl != uintptr(0) && _collationMatch(tls, zColl, pIdx) != 0 {
						_sqlite3BeginWriteOperation(tls, pParse, 0, iDb)
						_sqlite3RefillIndex(tls, pParse, pIdx, -int32(1))
					}
					goto _7
				_7:
					;
					pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
				} /* End loop over indexes of pTab */
				goto _6
			_6:
				;
				k = (*THashElem)(unsafe.Pointer(k)).Fnext
			} /* End loop over tables of iDb */
			goto _5
		_5:
			;
			iDb = iDb + 1
			pDb += 16
		} /* End loop over databases */
	} else {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17460, 0)
	}
	_sqlite3DbFree(tls, db, z)
	return
}

// C documentation
//
//	/*
//	** Deallocate a register, making available for reuse for some other
//	** purpose.
//	*/
func _sqlite3ReleaseTempReg(tls *libc.TLS, pParse uintptr, iReg int32) {
	var v1 Tu8
	var v2 uintptr
	_, _ = v1, v2
	if iReg != 0 {
		if int32((*TParse)(unsafe.Pointer(pParse)).FnTempReg) < int32(libc.Uint32FromInt64(32)/libc.Uint32FromInt64(4)) {
			v2 = pParse + 19
			v1 = *(*Tu8)(unsafe.Pointer(v2))
			*(*Tu8)(unsafe.Pointer(v2)) = *(*Tu8)(unsafe.Pointer(v2)) + 1
			**(**int32)(__ccgo_up(pParse + 136 + uintptr(v1)*4)) = iReg
		}
	}
}

// C documentation
//
//	/*
//	** Remember that the parser tree element pPtr was created using
//	** the token pToken.
//	**
//	** In other words, construct a new RenameToken object and add it
//	** to the list of RenameToken objects currently being built up
//	** in pParse->pRename.
//	**
//	** The pPtr argument is returned so that this routine can be used
//	** with tail recursion in tokenExpr() routine, for a small performance
//	** improvement.
//	*/
func _sqlite3RenameTokenMap(tls *libc.TLS, pParse uintptr, pPtr uintptr, pToken uintptr) (r uintptr) {
	var pNew uintptr
	_ = pNew
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != int32(PARSE_MODE_UNMAP) {
		pNew = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(16))
		if pNew != 0 {
			(*TRenameToken)(unsafe.Pointer(pNew)).Fp = pPtr
			(*TRenameToken)(unsafe.Pointer(pNew)).Ft = **(**TToken)(__ccgo_up(pToken))
			(*TRenameToken)(unsafe.Pointer(pNew)).FpNext = (*TParse)(unsafe.Pointer(pParse)).FpRename
			(*TParse)(unsafe.Pointer(pParse)).FpRename = pNew
		}
	}
	return pPtr
}

// C documentation
//
//	/*
//	** Erase all schema information from all attached databases (including
//	** "main" and "temp") for a single database connection.
//	*/
func _sqlite3ResetAllSchemasOfConnection(tls *libc.TLS, db uintptr) {
	var i int32
	var pDb, v2 uintptr
	_, _, _ = i, pDb, v2
	_sqlite3BtreeEnterAll(tls, db)
	i = 0
	for {
		if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
			break
		}
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16
		if (*TDb)(unsafe.Pointer(pDb)).FpSchema != 0 {
			if (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock == uint32(0) {
				_sqlite3SchemaClear(tls, (*TDb)(unsafe.Pointer(pDb)).FpSchema)
			} else {
				v2 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema + 78
				*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(DB_ResetWanted))
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	**(**Tu32)(__ccgo_up(db + 24)) &= uint32(^(libc.Int32FromInt32(DBFLAG_SchemaChange) | libc.Int32FromInt32(DBFLAG_SchemaKnownOk)))
	_sqlite3VtabUnlockList(tls, db)
	_sqlite3BtreeLeaveAll(tls, db)
	if (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock == uint32(0) {
		_sqlite3CollapseDatabaseArray(tls, db)
	}
}

// C documentation
//
//	/*
//	** Reset the schema for the database at index iDb.  Also reset the
//	** TEMP schema.  The reset is deferred if db->nSchemaLock is not zero.
//	** Deferred resets may be run by calling with iDb<0.
//	*/
func _sqlite3ResetOneSchema(tls *libc.TLS, db uintptr, iDb int32) {
	var i int32
	var v1 uintptr
	_, _ = i, v1
	if iDb >= 0 {
		v1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema + 78
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(DB_ResetWanted))
		v1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema + 78
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(DB_ResetWanted))
		**(**Tu32)(__ccgo_up(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaKnownOk))
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock == uint32(0) {
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			if int32((*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema)).FschemaFlags)&int32(DB_ResetWanted) == int32(DB_ResetWanted) {
				_sqlite3SchemaClear(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpSchema)
			}
			goto _3
		_3:
			;
			i = i + 1
		}
	}
}

// C documentation
//
//	/*
//	** Resolve all names for all expression in an expression list.  This is
//	** just like sqlite3ResolveExprNames() except that it works for an expression
//	** list rather than a single expression.
//	**
//	** The return value is SQLITE_OK (0) for success or SQLITE_ERROR (1) for a
//	** failure.
//	*/
func _sqlite3ResolveExprListNames(tls *libc.TLS, pNC uintptr, pList uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, savedHasAgg int32
	var pExpr uintptr
	var _ /* w at bp+0 */ TWalker
	_, _, _ = i, pExpr, savedHasAgg
	savedHasAgg = 0
	if pList == uintptr(0) {
		return SQLITE_OK
	}
	(**(**TWalker)(__ccgo_up(bp))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_resolveExprStep)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_resolveSelectStep)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback2 = uintptr(0)
	*(*uintptr)(unsafe.Pointer(bp + 24)) = pNC
	savedHasAgg = (*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg))
	**(**int32)(__ccgo_up(pNC + 24)) &= ^(libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg))
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
			break
		}
		pExpr = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr
		if pExpr == uintptr(0) {
			goto _1
		}
		**(**int32)(__ccgo_up((**(**TWalker)(__ccgo_up(bp))).FpParse + 228)) += (*TExpr)(unsafe.Pointer(pExpr)).FnHeight
		if _sqlite3ExprCheckHeight(tls, (**(**TWalker)(__ccgo_up(bp))).FpParse, (*TParse)(unsafe.Pointer((**(**TWalker)(__ccgo_up(bp))).FpParse)).FnHeight) != 0 {
			return int32(SQLITE_ERROR)
		}
		_sqlite3WalkExprNN(tls, bp, pExpr)
		**(**int32)(__ccgo_up((**(**TWalker)(__ccgo_up(bp))).FpParse + 228)) -= (*TExpr)(unsafe.Pointer(pExpr)).FnHeight
		if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_HasAgg)|libc.Int32FromInt32(NC_MinMaxAgg)|libc.Int32FromInt32(NC_HasWin)|libc.Int32FromInt32(NC_OrderAgg)) != 0 {
			**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32((*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_HasWin)))
			savedHasAgg = savedHasAgg | (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_HasAgg)|libc.Int32FromInt32(NC_MinMaxAgg)|libc.Int32FromInt32(NC_HasWin)|libc.Int32FromInt32(NC_OrderAgg))
			**(**int32)(__ccgo_up(pNC + 24)) &= ^(libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg))
		}
		if (*TParse)(unsafe.Pointer((**(**TWalker)(__ccgo_up(bp))).FpParse)).FnErr > 0 {
			return int32(SQLITE_ERROR)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	**(**int32)(__ccgo_up(pNC + 24)) |= savedHasAgg
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This routine walks an expression tree and resolves references to
//	** table columns and result-set columns.  At the same time, do error
//	** checking on function usage and set a flag if any aggregate functions
//	** are seen.
//	**
//	** To resolve table columns references we look for nodes (or subtrees) of the
//	** form X.Y.Z or Y.Z or just Z where
//	**
//	**      X:   The name of a database.  Ex:  "main" or "temp" or
//	**           the symbolic name assigned to an ATTACH-ed database.
//	**
//	**      Y:   The name of a table in a FROM clause.  Or in a trigger
//	**           one of the special names "old" or "new".
//	**
//	**      Z:   The name of a column in table Y.
//	**
//	** The node at the root of the subtree is modified as follows:
//	**
//	**    Expr.op        Changed to TK_COLUMN
//	**    Expr.pTab      Points to the Table object for X.Y
//	**    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
//	**    Expr.iTable    The VDBE cursor number for X.Y
//	**
//	**
//	** To resolve result-set references, look for expression nodes of the
//	** form Z (with no X and Y prefix) where the Z matches the right-hand
//	** size of an AS clause in the result-set of a SELECT.  The Z expression
//	** is replaced by a copy of the left-hand side of the result-set expression.
//	** Table-name and function resolution occurs on the substituted expression
//	** tree.  For example, in:
//	**
//	**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
//	**
//	** The "x" term of the order by is replaced by "a+b" to render:
//	**
//	**      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
//	**
//	** Function calls are checked to make sure that the function is
//	** defined and that the correct number of arguments are specified.
//	** If the function is an aggregate function, then the NC_HasAgg flag is
//	** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
//	** If an expression contains aggregate functions then the EP_Agg
//	** property on the expression is set.
//	**
//	** An error message is left in pParse if anything is amiss.  The number
//	** if errors is returned.
//	*/
func _sqlite3ResolveExprNames(tls *libc.TLS, pNC uintptr, pExpr uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var savedHasAgg int32
	var v1 uintptr
	var _ /* w at bp+0 */ TWalker
	_, _ = savedHasAgg, v1
	if pExpr == uintptr(0) {
		return SQLITE_OK
	}
	savedHasAgg = (*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg))
	**(**int32)(__ccgo_up(pNC + 24)) &= ^(libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_MinMaxAgg) | libc.Int32FromInt32(NC_HasWin) | libc.Int32FromInt32(NC_OrderAgg))
	(**(**TWalker)(__ccgo_up(bp))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_resolveExprStep)
	if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_NoSelect) != 0 {
		v1 = uintptr(0)
	} else {
		v1 = __ccgo_fp(_resolveSelectStep)
	}
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = v1
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback2 = uintptr(0)
	*(*uintptr)(unsafe.Pointer(bp + 24)) = pNC
	**(**int32)(__ccgo_up((**(**TWalker)(__ccgo_up(bp))).FpParse + 228)) += (*TExpr)(unsafe.Pointer(pExpr)).FnHeight
	if _sqlite3ExprCheckHeight(tls, (**(**TWalker)(__ccgo_up(bp))).FpParse, (*TParse)(unsafe.Pointer((**(**TWalker)(__ccgo_up(bp))).FpParse)).FnHeight) != 0 {
		return int32(SQLITE_ERROR)
	}
	_sqlite3WalkExprNN(tls, bp, pExpr)
	**(**int32)(__ccgo_up((**(**TWalker)(__ccgo_up(bp))).FpParse + 228)) -= (*TExpr)(unsafe.Pointer(pExpr)).FnHeight
	**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32((*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_HasWin)))
	**(**int32)(__ccgo_up(pNC + 24)) |= savedHasAgg
	return libc.BoolInt32((*TNameContext)(unsafe.Pointer(pNC)).FnNcErr > 0 || (*TParse)(unsafe.Pointer((**(**TWalker)(__ccgo_up(bp))).FpParse)).FnErr > 0)
}

// C documentation
//
//	/*
//	** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
//	** the SELECT statement pSelect.  If any term is reference to a
//	** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
//	** field) then convert that term into a copy of the corresponding result set
//	** column.
//	**
//	** If any errors are detected, add an error message to pParse and
//	** return non-zero.  Return zero if no errors are seen.
//	*/
func _sqlite3ResolveOrderGroupBy(tls *libc.TLS, pParse uintptr, pSelect uintptr, pOrderBy uintptr, zType uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pEList, pItem uintptr
	var i int32
	_, _, _, _ = db, i, pEList, pItem
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if pOrderBy == uintptr(0) || (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		return 0
	}
	if (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr > **(**int32)(__ccgo_up(db + 120 + 2*4)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8943, libc.VaList(bp+8, zType))
		return int32(1)
	}
	pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList
	/* sqlite3SelectNew() guarantees this */
	i = 0
	pItem = pOrderBy + 8
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) {
			break
		}
		if (*(*struct {
			FiOrderByCol Tu16
			FiAlias      Tu16
		})(unsafe.Pointer(pItem + 16))).FiOrderByCol != 0 {
			if int32((*(*struct {
				FiOrderByCol Tu16
				FiAlias      Tu16
			})(unsafe.Pointer(pItem + 16))).FiOrderByCol) > (*TExprList)(unsafe.Pointer(pEList)).FnExpr {
				_resolveOutOfRangeError(tls, pParse, zType, i+int32(1), (*TExprList)(unsafe.Pointer(pEList)).FnExpr, uintptr(0))
				return int32(1)
			}
			_resolveAlias(tls, pParse, pEList, int32((*(*struct {
				FiOrderByCol Tu16
				FiAlias      Tu16
			})(unsafe.Pointer(pItem + 16))).FiOrderByCol)-int32(1), (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr, 0)
		}
		goto _1
	_1:
		;
		i = i + 1
		pItem += 20
	}
	return 0
}

// C documentation
//
//	/* Force the INT64 value currently stored as the result to be
//	** a MEM_IntReal value.  See the SQLITE_TESTCTRL_RESULT_INTREAL
//	** test-control.
//	*/
func _sqlite3ResultIntReal(tls *libc.TLS, pCtx uintptr) {
	var v1 uintptr
	_ = v1
	if int32((*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut)).Fflags)&int32(MEM_Int) != 0 {
		v1 = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(MEM_Int))
		v1 = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_IntReal))
	}
}

// C documentation
//
//	/*
//	** Given a SELECT statement, generate a Table structure that describes
//	** the result set of that SELECT.
//	*/
func _sqlite3ResultSetOfSelect(tls *libc.TLS, pParse uintptr, pSelect uintptr, aff int8) (r uintptr) {
	var db, pTab uintptr
	var savedFlags Tu64
	_, _, _ = db, pTab, savedFlags
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	(*TParse)(unsafe.Pointer(pParse)).FnNestSel = (*TParse)(unsafe.Pointer(pParse)).FnNestSel + 1
	if (*TParse)(unsafe.Pointer(pParse)).FnNestSel >= **(**int32)(__ccgo_up(db + 120 + 3*4)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21887, 0)
		return uintptr(0)
	}
	savedFlags = (*Tsqlite3)(unsafe.Pointer(db)).Fflags
	**(**Tu64)(__ccgo_up(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_FullColNames)
	**(**Tu64)(__ccgo_up(db + 32)) |= uint64(SQLITE_ShortColNames)
	_sqlite3SelectPrep(tls, pParse, pSelect, uintptr(0))
	(*Tsqlite3)(unsafe.Pointer(db)).Fflags = savedFlags
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return uintptr(0)
	}
	for (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 {
		pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior
	}
	pTab = _sqlite3DbMallocZero(tls, db, uint64(80))
	if pTab == uintptr(0) {
		return uintptr(0)
	}
	(*TTable)(unsafe.Pointer(pTab)).FnTabRef = uint32(1)
	(*TTable)(unsafe.Pointer(pTab)).FzName = uintptr(0)
	(*TTable)(unsafe.Pointer(pTab)).FnRowLogEst = int16(200)
	_sqlite3ColumnsFromExprList(tls, pParse, (*TSelect)(unsafe.Pointer(pSelect)).FpEList, pTab+34, pTab+4)
	_sqlite3SubqueryColumnTypes(tls, pParse, pTab, pSelect, aff)
	(*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1))
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_sqlite3DeleteTable(tls, db, pTab)
		return uintptr(0)
	}
	(*TParse)(unsafe.Pointer(pParse)).FnNestSel = (*TParse)(unsafe.Pointer(pParse)).FnNestSel - 1
	return pTab
}

// C documentation
//
//	/* If the Expr node is a subquery or an EXISTS operator or an IN operator that
//	** uses a subquery, and if the subquery is SF_Correlated, then mark the
//	** expression as EP_VarSelect.
//	*/
func _sqlite3ReturningSubqueryVarSelect(tls *libc.TLS, NotUsed uintptr, pExpr uintptr) (r int32) {
	_ = NotUsed
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) && (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FselFlags&uint32(SF_Correlated) != uint32(0) {
		**(**Tu32)(__ccgo_up(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_VarSelect))
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Rollback all database files.  If tripCode is not SQLITE_OK, then
//	** any write cursors are invalidated ("tripped" - as in "tripping a circuit
//	** breaker") and made to return tripCode if there are any further
//	** attempts to use that cursor.  Read cursors remain open and valid
//	** but are "saved" in case the table pages are moved around.
//	*/
func _sqlite3RollbackAll(tls *libc.TLS, db uintptr, tripCode int32) {
	var i, inTrans, schemaChange int32
	var p uintptr
	_, _, _, _ = i, inTrans, p, schemaChange
	inTrans = 0
	_sqlite3BeginBenignMalloc(tls)
	/* Obtain all b-tree mutexes before making any calls to BtreeRollback().
	 ** This is important in case the transaction being rolled back has
	 ** modified the database schema. If the b-tree mutexes are not taken
	 ** here, then another shared-cache connection might sneak in between
	 ** the database rollback and schema reset, which can cause false
	 ** corruption reports in some cases.  */
	_sqlite3BtreeEnterAll(tls, db)
	schemaChange = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_SchemaChange) != uint32(0) && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0)
	i = 0
	for {
		if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
			break
		}
		p = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
		if p != 0 {
			if _sqlite3BtreeTxnState(tls, p) == int32(SQLITE_TXN_WRITE) {
				inTrans = int32(1)
			}
			_sqlite3BtreeRollback(tls, p, tripCode, libc.BoolInt32(!(schemaChange != 0)))
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	_sqlite3VtabRollback(tls, db)
	_sqlite3EndBenignMalloc(tls)
	if schemaChange != 0 {
		_sqlite3ExpirePreparedStatements(tls, db, 0)
		_sqlite3ResetAllSchemasOfConnection(tls, db)
	}
	_sqlite3BtreeLeaveAll(tls, db)
	/* Any deferred constraint violations have now been resolved. */
	(*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons = 0
	(*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons = 0
	**(**Tu64)(__ccgo_up(db + 32)) &= ^(libc.Uint64FromInt32(SQLITE_DeferFKs) | uint64(libc.Int32FromInt32(0x00002))<<libc.Int32FromInt32(32))
	/* If one has been configured, invoke the rollback-hook callback */
	if (*Tsqlite3)(unsafe.Pointer(db)).FxRollbackCallback != 0 && (inTrans != 0 || !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0)) {
		(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxRollbackCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpRollbackArg)
	}
}

/*
** Return a static string containing the name corresponding to the error code
** specified in the argument.
 */

// C documentation
//
//	/*
//	** Allocate a RowSet object.  Return NULL if a memory allocation
//	** error occurs.
//	*/
func _sqlite3RowSetInit(tls *libc.TLS, db uintptr) (r uintptr) {
	var N int32
	var p uintptr
	_, _ = N, p
	p = _sqlite3DbMallocRawNN(tls, db, uint64(32))
	if p != 0 {
		N = _sqlite3DbMallocSize(tls, db, p)
		(*TRowSet)(unsafe.Pointer(p)).FpChunk = uintptr(0)
		(*TRowSet)(unsafe.Pointer(p)).Fdb = db
		(*TRowSet)(unsafe.Pointer(p)).FpEntry = uintptr(0)
		(*TRowSet)(unsafe.Pointer(p)).FpLast = uintptr(0)
		(*TRowSet)(unsafe.Pointer(p)).FpForest = uintptr(0)
		(*TRowSet)(unsafe.Pointer(p)).FpFresh = uintptr((libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) + p
		(*TRowSet)(unsafe.Pointer(p)).FnFresh = uint16((uint32(N) - (libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) / libc.Uint32FromInt64(16))
		(*TRowSet)(unsafe.Pointer(p)).FrsFlags = uint16(ROWSET_SORTED)
		(*TRowSet)(unsafe.Pointer(p)).FiBatch = 0
	}
	return p
}

// C documentation
//
//	/*
//	** Extract the smallest element from the RowSet.
//	** Write the element into *pRowid.  Return 1 on success.  Return
//	** 0 if the RowSet is already empty.
//	**
//	** After this routine has been called, the sqlite3RowSetInsert()
//	** routine may not be called again.
//	**
//	** This routine may not be called after sqlite3RowSetTest() has
//	** been used.  Older versions of RowSet allowed that, but as the
//	** capability was not used by the code generator, it was removed
//	** for code economy.
//	*/
func _sqlite3RowSetNext(tls *libc.TLS, p uintptr, pRowid uintptr) (r int32) {
	var v1 uintptr
	_ = v1
	/* Cannot be used with sqlite3RowSetText() */
	/* Merge the forest into a single sorted list on first call */
	if int32((*TRowSet)(unsafe.Pointer(p)).FrsFlags)&int32(ROWSET_NEXT) == 0 { /*OPTIMIZATION-IF-FALSE*/
		if int32((*TRowSet)(unsafe.Pointer(p)).FrsFlags)&int32(ROWSET_SORTED) == 0 { /*OPTIMIZATION-IF-FALSE*/
			(*TRowSet)(unsafe.Pointer(p)).FpEntry = _rowSetEntrySort(tls, (*TRowSet)(unsafe.Pointer(p)).FpEntry)
		}
		v1 = p + 26
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | (libc.Int32FromInt32(ROWSET_SORTED) | libc.Int32FromInt32(ROWSET_NEXT)))
	}
	/* Return the next entry on the list */
	if (*TRowSet)(unsafe.Pointer(p)).FpEntry != 0 {
		**(**Ti64)(__ccgo_up(pRowid)) = (*TRowSetEntry)(unsafe.Pointer((*TRowSet)(unsafe.Pointer(p)).FpEntry)).Fv
		(*TRowSet)(unsafe.Pointer(p)).FpEntry = (*TRowSetEntry)(unsafe.Pointer((*TRowSet)(unsafe.Pointer(p)).FpEntry)).FpRight
		if (*TRowSet)(unsafe.Pointer(p)).FpEntry == uintptr(0) { /*OPTIMIZATION-IF-TRUE*/
			/* Free memory immediately, rather than waiting on sqlite3_finalize() */
			_sqlite3RowSetClear(tls, p)
		}
		return int32(1)
	} else {
		return 0
	}
	return r
}

// C documentation
//
//	/*
//	** Check to see if element iRowid was inserted into the rowset as
//	** part of any insert batch prior to iBatch.  Return 1 or 0.
//	**
//	** If this is the first test of a new batch and if there exist entries
//	** on pRowSet->pEntry, then sort those entries into the forest at
//	** pRowSet->pForest so that they can be tested.
//	*/
func _sqlite3RowSetTest(tls *libc.TLS, pRowSet uintptr, iBatch int32, iRowid Tsqlite3_int64) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var p, pTree, ppPrevTree, v2 uintptr
	var _ /* pAux at bp+0 */ uintptr
	var _ /* pTail at bp+4 */ uintptr
	_, _, _, _ = p, pTree, ppPrevTree, v2
	/* This routine is never called after sqlite3RowSetNext() */
	/* Sort entries into the forest on the first test of a new batch.
	 ** To save unnecessary work, only do this when the batch number changes.
	 */
	if iBatch != (*TRowSet)(unsafe.Pointer(pRowSet)).FiBatch { /*OPTIMIZATION-IF-FALSE*/
		p = (*TRowSet)(unsafe.Pointer(pRowSet)).FpEntry
		if p != 0 {
			ppPrevTree = pRowSet + 20
			if int32((*TRowSet)(unsafe.Pointer(pRowSet)).FrsFlags)&int32(ROWSET_SORTED) == 0 { /*OPTIMIZATION-IF-FALSE*/
				/* Only sort the current set of entries if they need it */
				p = _rowSetEntrySort(tls, p)
			}
			pTree = (*TRowSet)(unsafe.Pointer(pRowSet)).FpForest
			for {
				if !(pTree != 0) {
					break
				}
				ppPrevTree = pTree + 8
				if (*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft == uintptr(0) {
					(*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft = _rowSetListToTree(tls, p)
					break
				} else {
					_rowSetTreeToList(tls, (*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft, bp, bp+4)
					(*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft = uintptr(0)
					p = _rowSetEntryMerge(tls, **(**uintptr)(__ccgo_up(bp)), p)
				}
				goto _1
			_1:
				;
				pTree = (*TRowSetEntry)(unsafe.Pointer(pTree)).FpRight
			}
			if pTree == uintptr(0) {
				v2 = _rowSetEntryAlloc(tls, pRowSet)
				pTree = v2
				**(**uintptr)(__ccgo_up(ppPrevTree)) = v2
				if pTree != 0 {
					(*TRowSetEntry)(unsafe.Pointer(pTree)).Fv = 0
					(*TRowSetEntry)(unsafe.Pointer(pTree)).FpRight = uintptr(0)
					(*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft = _rowSetListToTree(tls, p)
				}
			}
			(*TRowSet)(unsafe.Pointer(pRowSet)).FpEntry = uintptr(0)
			(*TRowSet)(unsafe.Pointer(pRowSet)).FpLast = uintptr(0)
			v2 = pRowSet + 26
			*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(ROWSET_SORTED))
		}
		(*TRowSet)(unsafe.Pointer(pRowSet)).FiBatch = iBatch
	}
	/* Test to see if the iRowid value appears anywhere in the forest.
	 ** Return 1 if it does and 0 if not.
	 */
	pTree = (*TRowSet)(unsafe.Pointer(pRowSet)).FpForest
	for {
		if !(pTree != 0) {
			break
		}
		p = (*TRowSetEntry)(unsafe.Pointer(pTree)).FpLeft
		for p != 0 {
			if (*TRowSetEntry)(unsafe.Pointer(p)).Fv < iRowid {
				p = (*TRowSetEntry)(unsafe.Pointer(p)).FpRight
			} else {
				if (*TRowSetEntry)(unsafe.Pointer(p)).Fv > iRowid {
					p = (*TRowSetEntry)(unsafe.Pointer(p)).FpLeft
				} else {
					return int32(1)
				}
			}
		}
		goto _4
	_4:
		;
		pTree = (*TRowSetEntry)(unsafe.Pointer(pTree)).FpRight
	}
	return 0
}

/************** End of rowset.c **********************************************/
/************** Begin file pager.c *******************************************/
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This is the implementation of the page cache subsystem or "pager".
**
** The pager is used to access a database disk file.  It implements
** atomic commit and rollback through the use of a journal file that
** is separate from the database file.  The pager also implements file
** locking to prevent two processes from writing the same database
** file simultaneously, or one process from reading the database while
** another is writing.
 */
/* #include "sqliteInt.h" */
/************** Include wal.h in the middle of pager.c ***********************/
/************** Begin file wal.h *********************************************/
/*
** 2010 February 1
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This header file defines the interface to the write-ahead logging
** system. Refer to the comments below and the header comment attached to
** the implementation of each function in log.c for further details.
 */

/* #include "sqliteInt.h" */

/* Macros for extracting appropriate sync flags for either transaction
** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
 */

// C documentation
//
//	/*
//	** Return a pointer to a buffer containing a usable rowid alias for table
//	** pTab. An alias is usable if there is not an explicit user-defined column
//	** of the same name.
//	*/
func _sqlite3RowidAlias(tls *libc.TLS, pTab uintptr) (r uintptr) {
	var azOpt [3]uintptr
	var ii int32
	_, _ = azOpt, ii
	azOpt = [3]uintptr{
		0: __ccgo_ts + 9406,
		1: __ccgo_ts + 9414,
		2: __ccgo_ts + 9420,
	}
	ii = 0
	for {
		if !(ii < int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(4))) {
			break
		}
		if _sqlite3ColumnIndex(tls, pTab, azOpt[ii]) < 0 {
			return azOpt[ii]
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	return uintptr(0)
}

// C documentation
//
//	/*
//	** Code an OP_Halt due to non-unique rowid.
//	*/
func _sqlite3RowidConstraint(tls *libc.TLS, pParse uintptr, onError int32, pTab uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var rc int32
	var zMsg uintptr
	_, _ = rc, zMsg
	if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) >= 0 {
		zMsg = _sqlite3MPrintf(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, __ccgo_ts+14849, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*12))).FzCnName))
		rc = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(6)<<libc.Int32FromInt32(8)
	} else {
		zMsg = _sqlite3MPrintf(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, __ccgo_ts+17439, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
		rc = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(10)<<libc.Int32FromInt32(8)
	}
	_sqlite3HaltConstraint(tls, pParse, rc, onError, zMsg, int8(-libc.Int32FromInt32(7)), uint8(P5_ConstraintUnique))
}

// C documentation
//
//	/*
//	** Run the parser on the given SQL string.
//	*/
func _sqlite3RunParser(tls *libc.TLS, pParse uintptr, zSql uintptr) (r int32) {
	bp := tls.Alloc(656)
	defer tls.Free(656)
	var db, pEngine, pParentParse uintptr
	var lastTokenParsed, nErr int32
	var mxSqlLen, n Ti64
	var _ /* sEngine at bp+4 */ TyyParser
	var _ /* tokenType at bp+0 */ int32
	var _ /* x at bp+624 */ TToken
	_, _, _, _, _, _, _ = db, lastTokenParsed, mxSqlLen, n, nErr, pEngine, pParentParse
	nErr = 0                                   /* The LEMON-generated LALR(1) parser */
	n = 0                                      /* type of the next token */
	lastTokenParsed = -int32(1)                /* type of the previous token */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Max length of an SQL string */
	pParentParse = uintptr(0)                  /* Space to hold the Lemon-generated Parser object */
	mxSqlLen = int64(**(**int32)(__ccgo_up(db + 120 + 1*4)))
	if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive == 0 {
		libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED))
	}
	(*TParse)(unsafe.Pointer(pParse)).Frc = SQLITE_OK
	(*TParse)(unsafe.Pointer(pParse)).FzTail = zSql
	pEngine = bp + 4
	_sqlite3ParserInit(tls, pEngine, pParse)
	pParentParse = (*Tsqlite3)(unsafe.Pointer(db)).FpParse
	(*Tsqlite3)(unsafe.Pointer(db)).FpParse = pParse
	for int32(1) != 0 {
		n = _sqlite3GetToken(tls, zSql, bp)
		mxSqlLen = mxSqlLen - n
		if mxSqlLen < 0 {
			(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_TOOBIG)
			(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
			break
		}
		if **(**int32)(__ccgo_up(bp)) >= int32(TK_WINDOW) {
			if libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0 {
				(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_INTERRUPT)
				(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
				break
			}
			if **(**int32)(__ccgo_up(bp)) == int32(TK_SPACE) {
				zSql = zSql + uintptr(n)
				continue
			}
			if int32(**(**int8)(__ccgo_up(zSql))) == 0 {
				/* Upon reaching the end of input, call the parser two more times
				 ** with tokens TK_SEMI and 0, in that order. */
				if lastTokenParsed == int32(TK_SEMI) {
					**(**int32)(__ccgo_up(bp)) = 0
				} else {
					if lastTokenParsed == 0 {
						break
					} else {
						**(**int32)(__ccgo_up(bp)) = int32(TK_SEMI)
					}
				}
				n = 0
			} else {
				if **(**int32)(__ccgo_up(bp)) == int32(TK_WINDOW) {
					**(**int32)(__ccgo_up(bp)) = _analyzeWindowKeyword(tls, zSql+6)
				} else {
					if **(**int32)(__ccgo_up(bp)) == int32(TK_OVER) {
						**(**int32)(__ccgo_up(bp)) = _analyzeOverKeyword(tls, zSql+4, lastTokenParsed)
					} else {
						if **(**int32)(__ccgo_up(bp)) == int32(TK_FILTER) {
							**(**int32)(__ccgo_up(bp)) = _analyzeFilterKeyword(tls, zSql+6, lastTokenParsed)
						} else {
							if **(**int32)(__ccgo_up(bp)) == int32(TK_COMMENT) && ((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00040))<<libc.Int32FromInt32(32)) != uint64(0)) {
								/* Ignore SQL comments if either (1) we are reparsing the schema or
								 ** (2) SQLITE_DBCONFIG_ENABLE_COMMENTS is turned on (the default). */
								zSql = zSql + uintptr(n)
								continue
							} else {
								if **(**int32)(__ccgo_up(bp)) != int32(TK_QNUMBER) {
									(**(**TToken)(__ccgo_up(bp + 624))).Fz = zSql
									(**(**TToken)(__ccgo_up(bp + 624))).Fn = uint32(n)
									_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+26451, libc.VaList(bp+640, bp+624))
									break
								}
							}
						}
					}
				}
			}
		}
		(*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz = zSql
		(*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fn = uint32(n)
		_sqlite3Parser(tls, pEngine, **(**int32)(__ccgo_up(bp)), (*TParse)(unsafe.Pointer(pParse)).FsLastToken)
		lastTokenParsed = **(**int32)(__ccgo_up(bp))
		zSql = zSql + uintptr(n)
		if (*TParse)(unsafe.Pointer(pParse)).Frc != SQLITE_OK {
			break
		}
	}
	_sqlite3ParserFinalize(tls, pEngine)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM)
	}
	if (*TParse)(unsafe.Pointer(pParse)).FzErrMsg != 0 || (*TParse)(unsafe.Pointer(pParse)).Frc != SQLITE_OK && (*TParse)(unsafe.Pointer(pParse)).Frc != int32(SQLITE_DONE) {
		if (*TParse)(unsafe.Pointer(pParse)).FzErrMsg == uintptr(0) {
			(*TParse)(unsafe.Pointer(pParse)).FzErrMsg = _sqlite3DbStrDup(tls, db, _sqlite3ErrStr(tls, (*TParse)(unsafe.Pointer(pParse)).Frc))
		}
		if int32((*TParse)(unsafe.Pointer(pParse)).FprepFlags)&int32(SQLITE_PREPARE_DONT_LOG) == 0 {
			Xsqlite3_log(tls, (*TParse)(unsafe.Pointer(pParse)).Frc, __ccgo_ts+26476, libc.VaList(bp+640, (*TParse)(unsafe.Pointer(pParse)).FzErrMsg, (*TParse)(unsafe.Pointer(pParse)).FzTail))
		}
		nErr = nErr + 1
	}
	(*TParse)(unsafe.Pointer(pParse)).FzTail = zSql
	Xsqlite3_free(tls, (*TParse)(unsafe.Pointer(pParse)).FapVtabLock)
	if (*TParse)(unsafe.Pointer(pParse)).FpNewTable != 0 && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != libc.Int32FromInt32(PARSE_MODE_NORMAL)) {
		/* If the pParse->declareVtab flag is set, do not delete any table
		 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
		 ** will take responsibility for freeing the Table structure.
		 */
		_sqlite3DeleteTable(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpNewTable)
	}
	if (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger != 0 && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
		_sqlite3DeleteTrigger(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger)
	}
	if (*TParse)(unsafe.Pointer(pParse)).FpVList != 0 {
		_sqlite3DbNNFreeNN(tls, db, (*TParse)(unsafe.Pointer(pParse)).FpVList)
	}
	(*Tsqlite3)(unsafe.Pointer(db)).FpParse = pParentParse
	return nErr
}

/************** End of tokenize.c ********************************************/
/************** Begin file complete.c ****************************************/
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** An tokenizer for SQL
**
** This file contains C code that implements the sqlite3_complete() API.
** This code used to be part of the tokenizer.c source file.  But by
** separating it out, the code will be automatically omitted from
** static links that do not use it.
 */
/* #include "sqliteInt.h" */

/*
** This is defined in tokenize.c.  We just have to import the definition.
 */

/*
** Token types used by the sqlite3_complete() routine.  See the header
** comments on that procedure for additional information.
 */

// C documentation
//
//	/*
//	** This routine implements the OP_Vacuum opcode of the VDBE.
//	*/
func _sqlite3RunVacuum(tls *libc.TLS, pzErrMsg uintptr, db uintptr, iDb int32, pOut uintptr) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var i, isMemDb, nDb, nNew, nRes, rc, v1 int32
	var id, pDb, pMain, pTemp, zDbMain, zFilename, zOut uintptr
	var pgflags, saved_mDbFlags, saved_openFlags Tu32
	var saved_flags Tu64
	var saved_mTrace Tu8
	var saved_nChange, saved_nTotalChange Ti64
	var _ /* iRandom at bp+0 */ Tu64
	var _ /* meta at bp+64 */ Tu32
	var _ /* sz at bp+56 */ Ti64
	var _ /* zDbVacuum at bp+8 */ [42]int8
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, id, isMemDb, nDb, nNew, nRes, pDb, pMain, pTemp, pgflags, rc, saved_flags, saved_mDbFlags, saved_mTrace, saved_nChange, saved_nTotalChange, saved_openFlags, zDbMain, zFilename, zOut, v1
	rc = SQLITE_OK                          /* Saved trace settings */
	pDb = uintptr(0)                        /* Name of output file */
	pgflags = uint32(PAGER_SYNCHRONOUS_OFF) /* Name of the ATTACH-ed database used for vacuum */
	if !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0) {
		_sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+23971)
		return int32(SQLITE_ERROR) /* IMP: R-12218-18073 */
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive > int32(1) {
		_sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+24011)
		return int32(SQLITE_ERROR) /* IMP: R-15610-35227 */
	}
	saved_openFlags = (*Tsqlite3)(unsafe.Pointer(db)).FopenFlags
	if pOut != 0 {
		if Xsqlite3_value_type(tls, pOut) != int32(SQLITE_TEXT) {
			_sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+24054)
			return int32(SQLITE_ERROR)
		}
		zOut = Xsqlite3_value_text(tls, pOut)
		**(**uint32)(__ccgo_up(db + 60)) &= uint32(^libc.Int32FromInt32(SQLITE_OPEN_READONLY))
		**(**uint32)(__ccgo_up(db + 60)) |= uint32(libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE))
	} else {
		zOut = __ccgo_ts + 1711
	}
	/* Save the current value of the database flags so that it can be
	 ** restored before returning. Then set the writable-schema flag, and
	 ** disable CHECK and foreign key constraints.  */
	saved_flags = (*Tsqlite3)(unsafe.Pointer(db)).Fflags
	saved_mDbFlags = (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags
	saved_nChange = (*Tsqlite3)(unsafe.Pointer(db)).FnChange
	saved_nTotalChange = (*Tsqlite3)(unsafe.Pointer(db)).FnTotalChange
	saved_mTrace = (*Tsqlite3)(unsafe.Pointer(db)).FmTrace
	**(**Tu64)(__ccgo_up(db + 32)) |= uint64(libc.Int32FromInt32(SQLITE_WriteSchema)|libc.Int32FromInt32(SQLITE_IgnoreChecks)) | uint64(libc.Int32FromInt32(0x00040))<<libc.Int32FromInt32(32) | uint64(libc.Int32FromInt32(0x00010))<<libc.Int32FromInt32(32) | uint64(libc.Int32FromInt32(0x00020))<<libc.Int32FromInt32(32)
	**(**Tu32)(__ccgo_up(db + 24)) |= uint32(libc.Int32FromInt32(DBFLAG_PreferBuiltin) | libc.Int32FromInt32(DBFLAG_Vacuum))
	**(**Tu64)(__ccgo_up(db + 32)) &= ^(uint64(libc.Int32FromInt32(SQLITE_ForeignKeys)|libc.Int32FromInt32(SQLITE_ReverseOrder)|libc.Int32FromInt32(SQLITE_Defensive)) | uint64(libc.Int32FromInt32(0x00001))<<libc.Int32FromInt32(32))
	(*Tsqlite3)(unsafe.Pointer(db)).FmTrace = uint8(0)
	zDbMain = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	pMain = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt
	isMemDb = _sqlite3PagerIsMemdb(tls, _sqlite3BtreePager(tls, pMain))
	/* Attach the temporary database as 'vacuum_XXXXXX'. The synchronous pragma
	 ** can be set to 'off' for this file, as it is not recovered if a crash
	 ** occurs anyway. The integrity of the database is maintained by a
	 ** (possibly synchronous) transaction opened on the main database before
	 ** sqlite3BtreeCopyFile() is called.
	 **
	 ** An optimization would be to use a non-journaled pager.
	 ** (Later:) I tried setting "PRAGMA vacuum_XXXXXX.journal_mode=OFF" but
	 ** that actually made the VACUUM run slower.  Very little journalling
	 ** actually occurs when doing a vacuum since the vacuum_db is initially
	 ** empty.  Only the journal header is written.  Apparently it takes more
	 ** time to parse and run the PRAGMA to turn journalling off than it does
	 ** to write the journal header file.
	 */
	Xsqlite3_randomness(tls, int32(8), bp)
	Xsqlite3_snprintf(tls, int32(42), bp+8, __ccgo_ts+24072, libc.VaList(bp+80, **(**Tu64)(__ccgo_up(bp))))
	nDb = (*Tsqlite3)(unsafe.Pointer(db)).FnDb
	rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+24087, libc.VaList(bp+80, zOut, bp+8))
	(*Tsqlite3)(unsafe.Pointer(db)).FopenFlags = saved_openFlags
	if rc != SQLITE_OK {
		goto end_of_vacuum
	}
	pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(nDb)*16
	pTemp = (*TDb)(unsafe.Pointer(pDb)).FpBt
	nRes = _sqlite3BtreeGetRequestedReserve(tls, pMain)
	if pOut != 0 {
		id = _sqlite3PagerFile(tls, _sqlite3BtreePager(tls, pTemp))
		**(**Ti64)(__ccgo_up(bp + 56)) = 0
		if (*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods != uintptr(0) && (_sqlite3OsFileSize(tls, id, bp+56) != SQLITE_OK || **(**Ti64)(__ccgo_up(bp + 56)) > 0) {
			rc = int32(SQLITE_ERROR)
			_sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+24103)
			goto end_of_vacuum
		}
		**(**Tu32)(__ccgo_up(db + 24)) |= uint32(DBFLAG_VacuumInto)
		/* For a VACUUM INTO, the pager-flags are set to the same values as
		 ** they are for the database being vacuumed, except that PAGER_CACHESPILL
		 ** is always set. */
		pgflags = uint32(uint64((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).Fsafety_level) | (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(PAGER_FLAGS_MASK))
		/* If the VACUUM INTO target file is a URI filename and if the
		 ** "reserve=N" query parameter is present, reset the reserve to the
		 ** amount specified, if the amount is within range */
		zFilename = _sqlite3BtreeGetFilename(tls, pTemp)
		if zFilename != 0 {
			nNew = int32(Xsqlite3_uri_int64(tls, zFilename, __ccgo_ts+24130, int64(nRes)))
			if nNew >= 0 && nNew <= int32(255) {
				nRes = nNew
			}
		}
	}
	_sqlite3BtreeSetCacheSize(tls, pTemp, (*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema)).Fcache_size)
	_sqlite3BtreeSetSpillSize(tls, pTemp, _sqlite3BtreeSetSpillSize(tls, pMain, 0))
	_sqlite3BtreeSetPagerFlags(tls, pTemp, pgflags|uint32(PAGER_CACHESPILL))
	/* Begin a transaction and take an exclusive lock on the main database
	 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
	 ** to ensure that we do not try to change the page-size on a WAL database.
	 */
	rc = _execSql(tls, db, pzErrMsg, __ccgo_ts+17325)
	if rc != SQLITE_OK {
		goto end_of_vacuum
	}
	if pOut == uintptr(0) {
		v1 = int32(2)
	} else {
		v1 = 0
	}
	rc = _sqlite3BtreeBeginTrans(tls, pMain, v1, uintptr(0))
	if rc != SQLITE_OK {
		goto end_of_vacuum
	}
	/* Do not attempt to change the page size for a WAL database */
	if _sqlite3PagerGetJournalMode(tls, _sqlite3BtreePager(tls, pMain)) == int32(PAGER_JOURNALMODE_WAL) && pOut == uintptr(0) {
		(*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize = 0
	}
	if _sqlite3BtreeSetPageSize(tls, pTemp, _sqlite3BtreeGetPageSize(tls, pMain), nRes, 0) != 0 || !(isMemDb != 0) && _sqlite3BtreeSetPageSize(tls, pTemp, (*Tsqlite3)(unsafe.Pointer(db)).FnextPagesize, nRes, 0) != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		rc = int32(SQLITE_NOMEM)
		goto end_of_vacuum
	}
	if int32((*Tsqlite3)(unsafe.Pointer(db)).FnextAutovac) >= 0 {
		v1 = int32((*Tsqlite3)(unsafe.Pointer(db)).FnextAutovac)
	} else {
		v1 = _sqlite3BtreeGetAutoVacuum(tls, pMain)
	}
	_sqlite3BtreeSetAutoVacuum(tls, pTemp, v1)
	/* Query the schema of the main database. Create a mirror schema
	 ** in the temporary database.
	 */
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(nDb) /* force new CREATE statements into vacuum_db */
	rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+24138, libc.VaList(bp+80, zDbMain))
	if rc != SQLITE_OK {
		goto end_of_vacuum
	}
	rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+24246, libc.VaList(bp+80, zDbMain))
	if rc != SQLITE_OK {
		goto end_of_vacuum
	}
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(0)
	/* Loop through the tables in the main database. For each, do
	 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
	 ** the contents to the temporary database.
	 */
	rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+24300, libc.VaList(bp+80, bp+8, zDbMain, bp+8))
	**(**Tu32)(__ccgo_up(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_Vacuum))
	if rc != SQLITE_OK {
		goto end_of_vacuum
	}
	/* Copy the triggers, views, and virtual tables from the main database
	 ** over to the temporary database.  None of these objects has any
	 ** associated storage, so all we have to do is copy their entries
	 ** from the schema table.
	 */
	rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+24437, libc.VaList(bp+80, bp+8, zDbMain))
	if rc != 0 {
		goto end_of_vacuum
	}
	/* Copy Btree meta values */
	i = 0
	for {
		if !(i < int32(libc.Uint32FromInt64(10)/libc.Uint32FromInt64(1))) {
			break
		}
		/* GetMeta() and UpdateMeta() cannot fail in this context because
		 ** we already have page 1 loaded into cache and marked dirty. */
		_sqlite3BtreeGetMeta(tls, pMain, int32(_aCopy[i]), bp+64)
		rc = _sqlite3BtreeUpdateMeta(tls, pTemp, int32(_aCopy[i]), **(**Tu32)(__ccgo_up(bp + 64))+uint32(_aCopy[i+int32(1)]))
		if rc != SQLITE_OK {
			goto end_of_vacuum
		}
		goto _3
	_3:
		;
		i = i + int32(2)
	}
	if pOut == uintptr(0) {
		rc = _sqlite3BtreeCopyFile(tls, pMain, pTemp)
	}
	if rc != SQLITE_OK {
		goto end_of_vacuum
	}
	rc = _sqlite3BtreeCommit(tls, pTemp)
	if rc != SQLITE_OK {
		goto end_of_vacuum
	}
	if pOut == uintptr(0) {
		_sqlite3BtreeSetAutoVacuum(tls, pMain, _sqlite3BtreeGetAutoVacuum(tls, pTemp))
	}
	if pOut == uintptr(0) {
		nRes = _sqlite3BtreeGetRequestedReserve(tls, pTemp)
		rc = _sqlite3BtreeSetPageSize(tls, pMain, _sqlite3BtreeGetPageSize(tls, pTemp), nRes, int32(1))
	}
	goto end_of_vacuum
end_of_vacuum:
	;
	/* Restore the original value of db->flags */
	(*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(0)
	(*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags = saved_mDbFlags
	(*Tsqlite3)(unsafe.Pointer(db)).Fflags = saved_flags
	(*Tsqlite3)(unsafe.Pointer(db)).FnChange = saved_nChange
	(*Tsqlite3)(unsafe.Pointer(db)).FnTotalChange = saved_nTotalChange
	(*Tsqlite3)(unsafe.Pointer(db)).FmTrace = saved_mTrace
	_sqlite3BtreeSetPageSize(tls, pMain, -int32(1), 0, int32(1))
	/* Currently there is an SQL level transaction open on the vacuum
	 ** database. No locks are held on any other files (since the main file
	 ** was committed at the btree level). So it safe to end the transaction
	 ** by manually setting the autoCommit flag to true and detaching the
	 ** vacuum database. The vacuum_db journal file is deleted when the pager
	 ** is closed by the DETACH.
	 */
	(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1)
	if pDb != 0 {
		_sqlite3BtreeClose(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt)
		(*TDb)(unsafe.Pointer(pDb)).FpBt = uintptr(0)
		(*TDb)(unsafe.Pointer(pDb)).FpSchema = uintptr(0)
	}
	/* This both clears the schemas and reduces the size of the db->aDb[]
	 ** array. */
	_sqlite3ResetAllSchemasOfConnection(tls, db)
	return rc
}

/* At this point, there is a write transaction open on both the
 ** vacuum database and the main database. Assuming no error occurs,
 ** both transactions are closed by this block - the main database
 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
 ** call to sqlite3BtreeCommit().
 */

// C documentation
//
//	/*
//	** Free all resources held by the schema structure. The void* argument points
//	** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
//	** pointer itself, it just cleans up subsidiary resources (i.e. the contents
//	** of the schema hash tables).
//	**
//	** The Schema.cache_size variable is not cleared.
//	*/
func _sqlite3SchemaClear(tls *libc.TLS, p uintptr) {
	bp := tls.Alloc(624)
	defer tls.Free(624)
	var pElem, pSchema, pTab, v3 uintptr
	var _ /* temp1 at bp+0 */ THash
	var _ /* temp2 at bp+16 */ THash
	var _ /* xdb at bp+32 */ Tsqlite3
	_, _, _, _ = pElem, pSchema, pTab, v3
	pSchema = p
	libc.Xmemset(tls, bp+32, 0, uint32(584))
	**(**THash)(__ccgo_up(bp)) = (*TSchema)(unsafe.Pointer(pSchema)).FtblHash
	**(**THash)(__ccgo_up(bp + 16)) = (*TSchema)(unsafe.Pointer(pSchema)).FtrigHash
	_sqlite3HashInit(tls, pSchema+40)
	_sqlite3HashClear(tls, pSchema+24)
	pElem = (*THash)(unsafe.Pointer(bp + 16)).Ffirst
	for {
		if !(pElem != 0) {
			break
		}
		_sqlite3DeleteTrigger(tls, bp+32, (*THashElem)(unsafe.Pointer(pElem)).Fdata)
		goto _1
	_1:
		;
		pElem = (*THashElem)(unsafe.Pointer(pElem)).Fnext
	}
	_sqlite3HashClear(tls, bp+16)
	_sqlite3HashInit(tls, pSchema+8)
	pElem = (*THash)(unsafe.Pointer(bp)).Ffirst
	for {
		if !(pElem != 0) {
			break
		}
		pTab = (*THashElem)(unsafe.Pointer(pElem)).Fdata
		_sqlite3DeleteTable(tls, bp+32, pTab)
		goto _2
	_2:
		;
		pElem = (*THashElem)(unsafe.Pointer(pElem)).Fnext
	}
	_sqlite3HashClear(tls, bp)
	_sqlite3HashClear(tls, pSchema+56)
	(*TSchema)(unsafe.Pointer(pSchema)).FpSeqTab = uintptr(0)
	if int32((*TSchema)(unsafe.Pointer(pSchema)).FschemaFlags)&int32(DB_SchemaLoaded) != 0 {
		(*TSchema)(unsafe.Pointer(pSchema)).FiGeneration = (*TSchema)(unsafe.Pointer(pSchema)).FiGeneration + 1
	}
	v3 = pSchema + 78
	*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) & ^(libc.Int32FromInt32(DB_SchemaLoaded) | libc.Int32FromInt32(DB_ResetWanted)))
}

// C documentation
//
//	/*
//	** Find and return the schema associated with a BTree.  Create
//	** a new one if necessary.
//	*/
func _sqlite3SchemaGet(tls *libc.TLS, db uintptr, pBt uintptr) (r uintptr) {
	var p uintptr
	_ = p
	if pBt != 0 {
		p = _sqlite3BtreeSchema(tls, pBt, int32(84), __ccgo_fp(_sqlite3SchemaClear))
	} else {
		p = _sqlite3DbMallocZero(tls, uintptr(0), uint64(84))
	}
	if !(p != 0) {
		_sqlite3OomFault(tls, db)
	} else {
		if 0 == int32((*TSchema)(unsafe.Pointer(p)).Ffile_format) {
			_sqlite3HashInit(tls, p+8)
			_sqlite3HashInit(tls, p+24)
			_sqlite3HashInit(tls, p+40)
			_sqlite3HashInit(tls, p+56)
			(*TSchema)(unsafe.Pointer(p)).Fenc = uint8(SQLITE_UTF8)
		}
	}
	return p
}

/************** End of callback.c ********************************************/
/************** Begin file delete.c ******************************************/
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains C code routines that are called by the parser
** in order to generate code for DELETE FROM statements.
 */
/* #include "sqliteInt.h" */

// C documentation
//
//	/*
//	** Generate byte-code for the SELECT statement given in the p argument.
//	**
//	** The results are returned according to the SelectDest structure.
//	** See comments in sqliteInt.h for further information.
//	**
//	** This routine returns the number of errors.  If any errors are
//	** encountered, then an appropriate error message is left in
//	** pParse->zErrMsg.
//	**
//	** This routine does NOT free the Select structure passed in.  The
//	** calling function needs to do that.
//	**
//	** This is a long function.  The following is an outline of the processing
//	** steps, with tags referencing various milestones:
//	**
//	**  *  Resolve names and similar preparation                tag-select-0100
//	**  *  Scan of the FROM clause                              tag-select-0200
//	**      +  OUTER JOIN strength reduction                      tag-select-0220
//	**      +  Sub-query ORDER BY removal                         tag-select-0230
//	**      +  Query flattening                                   tag-select-0240
//	**  *  Separate subroutine for compound-SELECT              tag-select-0300
//	**  *  WHERE-clause constant propagation                    tag-select-0330
//	**  *  Count()-of-VIEW optimization                         tag-select-0350
//	**  *  Scan of the FROM clause again                        tag-select-0400
//	**      +  Authorize unreferenced tables                      tag-select-0410
//	**      +  Predicate push-down optimization                   tag-select-0420
//	**      +  Omit unused subquery columns optimization          tag-select-0440
//	**      +  Generate code to implement subqueries              tag-select-0480
//	**         -  Co-routines                                       tag-select-0482
//	**         -  Reuse previously computed CTE                     tag-select-0484
//	**         -  REuse previously computed VIEW                    tag-select-0486
//	**         -  Materialize a VIEW or CTE                         tag-select-0488
//	**  *  DISTINCT ORDER BY -> GROUP BY optimization           tag-select-0500
//	**  *  Set up for ORDER BY                                  tag-select-0600
//	**  *  Create output table                                  tag-select-0630
//	**  *  Prepare registers for LIMIT                          tag-select-0650
//	**  *  Setup for DISTINCT                                   tag-select-0680
//	**  *  Generate code for non-aggregate and non-GROUP BY     tag-select-0700
//	**  *  Generate code for aggregate and/or GROUP BY          tag-select-0800
//	**      +  GROUP BY queries                                   tag-select-0810
//	**      +  non-GROUP BY queries                               tag-select-0820
//	**         -  Special case of count() w/o GROUP BY              tag-select-0821
//	**         -  General case of non-GROUP BY aggregates           tag-select-0822
//	**  *  Sort results, as needed                              tag-select-0900
//	**  *  Internal self-checks                                 tag-select-1000
//	*/
func _sqlite3Select(tls *libc.TLS, pParse uintptr, p uintptr, pDest uintptr) (r int32) {
	bp := tls.Alloc(160)
	defer tls.Free(160)
	var addr1, addrEnd, addrGosub, addrOutputRow, addrReset, addrSetAbort, addrSortingIdx, addrTop, addrTopOfLoop, eDist, eDist1, groupBySort, i, iAMem, iAbortFlag, iBMem, iBreak, iCont, iCsr, iDb, iDb1, iEnd, iOrderByCol, iUseFlag, ii, isAgg, j, k, nCol, nGroupBy, onceAddr, orderByGrp, rc, regAcc, regBase, regGosub, regOutputRow, regRecord, regReset, sortOut, sortPTab, topAddr, v12, v15 int32
	var db, p0, pAggInfo, pBase, pBest, pCol, pCteUse, pCteUse1, pDistinct, pDistinct1, pEList, pExpr, pF, pF1, pGroupBy, pHaving, pI2, pIdx, pItem, pItem1, pItem2, pKeyInfo, pKeyInfo1, pKeyInfo2, pPrior, pPriorSubq, pSub, pSub1, pSubq, pTab, pTab1, pTabList, pWInfo, pWhere, pWin, pX, v, zDb, zSavedAuthContext, v1, v3 uintptr
	var distFlag, distFlag1, wctrlFlags Tu16
	var iRoot TPgno
	var minMaxFlag Tu8
	var _ /* dest at bp+56 */ TSelectDest
	var _ /* pMinMaxOrderBy at bp+52 */ uintptr
	var _ /* sDistinct at bp+0 */ TDistinctCtx
	var _ /* sNC at bp+84 */ TNameContext
	var _ /* sSort at bp+12 */ TSortCtx
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, addrEnd, addrGosub, addrOutputRow, addrReset, addrSetAbort, addrSortingIdx, addrTop, addrTopOfLoop, db, distFlag, distFlag1, eDist, eDist1, groupBySort, i, iAMem, iAbortFlag, iBMem, iBreak, iCont, iCsr, iDb, iDb1, iEnd, iOrderByCol, iRoot, iUseFlag, ii, isAgg, j, k, minMaxFlag, nCol, nGroupBy, onceAddr, orderByGrp, p0, pAggInfo, pBase, pBest, pCol, pCteUse, pCteUse1, pDistinct, pDistinct1, pEList, pExpr, pF, pF1, pGroupBy, pHaving, pI2, pIdx, pItem, pItem1, pItem2, pKeyInfo, pKeyInfo1, pKeyInfo2, pPrior, pPriorSubq, pSub, pSub1, pSubq, pTab, pTab1, pTabList, pWInfo, pWhere, pWin, pX, rc, regAcc, regBase, regGosub, regOutputRow, regRecord, regReset, sortOut, sortPTab, topAddr, v, wctrlFlags, zDb, zSavedAuthContext, v1, v12, v15, v3 /* True for select lists like "count(*)" */
	pEList = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        /* The HAVING clause.  May be NULL */
	pAggInfo = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      /* Aggregate information */
	rc = int32(1)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              /* The database connection */
	**(**uintptr)(__ccgo_up(bp + 52)) = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             /* Flag for min/max queries */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	v = _sqlite3GetVdbe(tls, pParse)
	if p == uintptr(0) || (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return int32(1)
	}
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_SELECT), uintptr(0), uintptr(0), uintptr(0)) != 0 {
		return int32(1)
	}
	/* tag-select-0100 */
	if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) <= int32(SRT_DistQueue) {
		/* All of these destinations are also able to ignore the ORDER BY clause */
		if (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 {
			_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3ExprListDeleteGeneric), (*TSelect)(unsafe.Pointer(p)).FpOrderBy)
			(*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0)
		}
		**(**Tu32)(__ccgo_up(p + 4)) &= ^libc.Uint32FromInt32(SF_Distinct)
	}
	_sqlite3SelectPrep(tls, pParse, p, uintptr(0))
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		goto select_end
	}
	/* If the SF_UFSrcCheck flag is set, then this function is being called
	 ** as part of populating the temp table for an UPDATE...FROM statement.
	 ** In this case, it is an error if the target object (pSrc->a[0]) name
	 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
	 **
	 ** Postgres disallows this case too. The reason is that some other
	 ** systems handle this case differently, and not all the same way,
	 ** which is just confusing. To avoid this, we follow PG's lead and
	 ** disallow it altogether.  */
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_UFSrcCheck) != 0 {
		p0 = (*TSelect)(unsafe.Pointer(p)).FpSrc + 8
		if _sameSrcAlias(tls, p0, (*TSelect)(unsafe.Pointer(p)).FpSrc) != 0 {
			if (*TSrcItem)(unsafe.Pointer(p0)).FzAlias != 0 {
				v1 = (*TSrcItem)(unsafe.Pointer(p0)).FzAlias
			} else {
				v1 = (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(p0)).FpSTab)).FzName
			}
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22862, libc.VaList(bp+128, v1))
			goto select_end
		}
		/* Clear the SF_UFSrcCheck flag. The check has already been performed,
		 ** and leaving this flag set can cause errors if a compound sub-query
		 ** in p->pSrc is flattened into this query and this function called
		 ** again as part of compound SELECT processing.  */
		**(**Tu32)(__ccgo_up(p + 4)) &= ^libc.Uint32FromInt32(SF_UFSrcCheck)
	}
	if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) == int32(SRT_Output) {
		_sqlite3GenerateColumnNames(tls, pParse, p)
	}
	if _sqlite3WindowRewrite(tls, pParse, p) != 0 {
		goto select_end
	}
	pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc
	isAgg = libc.BoolInt32((*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) != uint32(0))
	libc.Xmemset(tls, bp+12, 0, uint32(40))
	(**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy
	/* Try to do various optimizations (flattening subqueries, and strength
	 ** reduction of join operators) in the FROM clause up into the main query
	 ** tag-select-0200
	 */
	i = 0
	for {
		if !(!((*TSelect)(unsafe.Pointer(p)).FpPrior != 0) && i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) {
			break
		}
		pItem = pTabList + 8 + uintptr(i)*48
		if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4>>2) != 0 {
			v1 = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 44)))).FpSelect
		} else {
			v1 = uintptr(0)
		}
		pSub = v1
		pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
		/* The expander should have already created transient Table objects
		 ** even for FROM clause elements such as subqueries that do not correspond
		 ** to a real table */
		/* Try to simplify joins:
		 **
		 **      LEFT JOIN  ->  JOIN
		 **     RIGHT JOIN  ->  JOIN
		 **      FULL JOIN  ->  RIGHT JOIN
		 **
		 ** If terms of the i-th table are used in the WHERE clause in such a
		 ** way that the i-th table cannot be the NULL row of a join, then
		 ** perform the appropriate simplification. This is called
		 ** "OUTER JOIN strength reduction" in the SQLite documentation.
		 ** tag-select-0220
		 */
		if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) != 0 && _sqlite3ExprImpliesNonNullRow(tls, (*TSelect)(unsafe.Pointer(p)).FpWhere, (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor, int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LTORJ)) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_SimplifyJoin)) == uint32(0) {
			if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LEFT) != 0 {
				if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 {
					v1 = pItem + 12
					*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(JT_LEFT))
				} else {
					v1 = pItem + 12
					*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(JT_LEFT) | libc.Int32FromInt32(JT_OUTER)))
					_unsetJoinExpr(tls, (*TSelect)(unsafe.Pointer(p)).FpWhere, (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor, 0)
				}
			}
			if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
				j = i + int32(1)
				for {
					if !(j < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) {
						break
					}
					pI2 = pTabList + 8 + uintptr(j)*48
					if int32((*TSrcItem)(unsafe.Pointer(pI2)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 {
						if int32((*TSrcItem)(unsafe.Pointer(pI2)).Ffg.Fjointype)&int32(JT_LEFT) != 0 {
							v1 = pI2 + 12
							*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(JT_RIGHT))
						} else {
							v1 = pI2 + 12
							*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(JT_RIGHT) | libc.Int32FromInt32(JT_OUTER)))
							_unsetJoinExpr(tls, (*TSelect)(unsafe.Pointer(p)).FpWhere, (*TSrcItem)(unsafe.Pointer(pI2)).FiCursor, int32(1))
						}
					}
					goto _6
				_6:
					;
					j = j + 1
				}
				j = (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc - int32(1)
				for {
					if !(j >= 0) {
						break
					}
					v1 = pTabList + 8 + uintptr(j)*48 + 12
					*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(JT_LTORJ))
					if int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(j)*48))).Ffg.Fjointype)&int32(JT_RIGHT) != 0 {
						break
					}
					goto _9
				_9:
					;
					j = j - 1
				}
			}
		}
		/* No further action if this term of the FROM clause is not a subquery */
		if pSub == uintptr(0) {
			goto _2
		}
		/* Catch mismatch in the declared columns of a view and the number of
		 ** columns in the SELECT on the RHS */
		if int32((*TTable)(unsafe.Pointer(pTab)).FnCol) != (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub)).FpEList)).FnExpr {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22916, libc.VaList(bp+128, int32((*TTable)(unsafe.Pointer(pTab)).FnCol), (*TTable)(unsafe.Pointer(pTab)).FzName, (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub)).FpEList)).FnExpr))
			goto select_end
		}
		/* Do not attempt the usual optimizations (flattening and ORDER BY
		 ** elimination) on a MATERIALIZED common table expression because
		 ** a MATERIALIZED common table expression is an optimization fence.
		 */
		if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x200>>9) != 0 && int32((*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 36)))).FeM10d) == M10d_Yes {
			goto _2
		}
		/* Do not try to flatten an aggregate subquery.
		 **
		 ** Flattening an aggregate subquery is only possible if the outer query
		 ** is not a join.  But if the outer query is not a join, then the subquery
		 ** will be implemented as a co-routine and there is no advantage to
		 ** flattening in that case.
		 */
		if (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(SF_Aggregate) != uint32(0) {
			goto _2
		}
		/* tag-select-0230:
		 ** If a FROM-clause subquery has an ORDER BY clause that is not
		 ** really doing anything, then delete it now so that it does not
		 ** interfere with query flattening.  See the discussion at
		 ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a
		 **
		 ** Beware of these cases where the ORDER BY clause may not be safely
		 ** omitted:
		 **
		 **    (1)   There is also a LIMIT clause
		 **    (2)   The subquery was added to help with window-function
		 **          processing
		 **    (3)   The subquery is in the FROM clause of an UPDATE
		 **    (4)   The outer query uses an aggregate function other than
		 **          the built-in count(), min(), or max().
		 **    (5)   The ORDER BY isn't going to accomplish anything because
		 **          one of:
		 **            (a)  The outer query has a different ORDER BY clause
		 **            (b)  The subquery is part of a join
		 **          See forum post 062d576715d277c8
		 **    (6)   The subquery is not a recursive CTE.  ORDER BY has a different
		 **          meaning for recursive CTEs and this optimization does not
		 **          apply.
		 **
		 ** Also retain the ORDER BY if the OmitOrderBy optimization is disabled.
		 */
		if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != uintptr(0) && ((*TSelect)(unsafe.Pointer(p)).FpOrderBy != uintptr(0) || (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc > int32(1)) && (*TSelect)(unsafe.Pointer(pSub)).FpLimit == uintptr(0) && (*TSelect)(unsafe.Pointer(pSub)).FselFlags&uint32(libc.Int32FromInt32(SF_OrderByReqd)|libc.Int32FromInt32(SF_Recursive)) == uint32(0) && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_OrderByReqd) == uint32(0) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_OmitOrderBy)) == uint32(0) {
			_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3ExprListDeleteGeneric), (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy)
			(*TSelect)(unsafe.Pointer(pSub)).FpOrderBy = uintptr(0)
		}
		/* If the outer query contains a "complex" result set (that is,
		 ** if the result set of the outer query uses functions or subqueries)
		 ** and if the subquery contains an ORDER BY clause and if
		 ** it will be implemented as a co-routine, then do not flatten.  This
		 ** restriction allows SQL constructs like this:
		 **
		 **  SELECT expensive_function(x)
		 **    FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
		 **
		 ** The expensive_function() is only computed on the 10 rows that
		 ** are output, rather than every row of the table.
		 **
		 ** The requirement that the outer query have a complex result set
		 ** means that flattening does occur on simpler SQL constraints without
		 ** the expensive_function() like:
		 **
		 **  SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
		 */
		if (*TSelect)(unsafe.Pointer(pSub)).FpOrderBy != uintptr(0) && i == 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_ComplexResult) != uint32(0) && ((*TSrcList)(unsafe.Pointer(pTabList)).FnSrc == int32(1) || int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + 1*48))).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)) != 0) {
			goto _2
		}
		/* tag-select-0240 */
		if _flattenSubquery(tls, pParse, p, i, isAgg) != 0 {
			if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
				goto select_end
			}
			/* This subquery can be absorbed into its parent. */
			i = -int32(1)
		}
		pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			goto select_end
		}
		if !(int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) <= libc.Int32FromInt32(SRT_Fifo)) {
			(**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	/* Handle compound SELECT statements using the separate multiSelect()
	 ** procedure.  tag-select-0300
	 */
	if (*TSelect)(unsafe.Pointer(p)).FpPrior != 0 {
		rc = _multiSelect(tls, pParse, p, pDest)
		if (*TSelect)(unsafe.Pointer(p)).FpNext == uintptr(0) {
			_sqlite3VdbeExplainPop(tls, pParse)
		}
		return rc
	}
	/* If there may be an "EXISTS (SELECT ...)" in the WHERE clause, attempt
	 ** to change it into a join.  */
	if int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x10>>4)) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_ExistsToJoin)) == uint32(0) {
		_existsToJoin(tls, pParse, p, (*TSelect)(unsafe.Pointer(p)).FpWhere)
		pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc
	}
	/* Do the WHERE-clause constant propagation optimization if this is
	 ** a join.  No need to spend time on this operation for non-join queries
	 ** as the equivalent optimization will be handled by query planner in
	 ** sqlite3WhereBegin().  tag-select-0330
	 */
	if (*TSelect)(unsafe.Pointer(p)).FpWhere != uintptr(0) && int32((*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpWhere)).Fop) == int32(TK_AND) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_PropagateConst)) == uint32(0) && _propagateConstants(tls, pParse, p) != 0 {
	} else {
	}
	/* tag-select-0350 */
	if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_QueryFlattener)|libc.Int32FromInt32(SQLITE_CountOfView)) == uint32(0) && _countOfViewOptimization(tls, pParse, p) != 0 {
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			goto select_end
		}
		pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc
	}
	/* Loop over all terms in the FROM clause and do two things for each term:
	 **
	 **   (1) Authorize unreferenced tables
	 **   (2) Generate code for all sub-queries
	 **
	 ** tag-select-0400
	 */
	i = 0
	for {
		if !(i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) {
			break
		}
		pItem1 = pTabList + 8 + uintptr(i)*48
		/* Authorized unreferenced tables.  tag-select-0410
		 **
		 ** Issue SQLITE_READ authorizations with a fake column name for any
		 ** tables that are referenced but from which no values are extracted.
		 ** Examples of where these kinds of null SQLITE_READ authorizations
		 ** would occur:
		 **
		 **     SELECT count(*) FROM t1;   -- SQLITE_READ t1.""
		 **     SELECT t1.* FROM t1, t2;   -- SQLITE_READ t2.""
		 **
		 ** The fake column name is an empty string.  It is possible for a table to
		 ** have a column named by the empty string, in which case there is no way to
		 ** distinguish between an unreferenced table and an actual reference to the
		 ** "" column. The original design was for the fake column name to be a NULL,
		 ** which would be unambiguous.  But legacy authorization callbacks might
		 ** assume the column name is non-NULL and segfault.  The use of an empty
		 ** string for the fake column name seems safer.
		 */
		if (*TSrcItem)(unsafe.Pointer(pItem1)).FcolUsed == uint64(0) && (*TSrcItem)(unsafe.Pointer(pItem1)).FzName != uintptr(0) {
			if int32(*(*uint32)(unsafe.Pointer(pItem1 + 12 + 4))&0x10000>>16) != 0 {
				iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(pItem1 + 44)))
				zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
			} else {
				if int32(*(*uint32)(unsafe.Pointer(pItem1 + 12 + 4))&0x4>>2) != 0 {
					zDb = uintptr(0)
				} else {
					zDb = *(*uintptr)(unsafe.Pointer(pItem1 + 44))
				}
			}
			_sqlite3AuthCheck(tls, pParse, int32(SQLITE_READ), (*TSrcItem)(unsafe.Pointer(pItem1)).FzName, __ccgo_ts+1711, zDb)
		}
		/* Generate code for all sub-queries in the FROM clause
		 */
		if int32(*(*uint32)(unsafe.Pointer(pItem1 + 12 + 4))&0x4>>2) == 0 {
			goto _11
		}
		pSubq = *(*uintptr)(unsafe.Pointer(pItem1 + 44))
		pSub1 = (*TSubquery)(unsafe.Pointer(pSubq)).FpSelect
		/* The code for a subquery should only be generated once. */
		if (*TSubquery)(unsafe.Pointer(pSubq)).FaddrFillSub != 0 {
			goto _11
		}
		/* Increment Parse.nHeight by the height of the largest expression
		 ** tree referred to by this, the parent select. The child select
		 ** may contain expression trees of at most
		 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
		 ** more conservative than necessary, but much easier than enforcing
		 ** an exact limit.
		 */
		**(**int32)(__ccgo_up(pParse + 228)) += _sqlite3SelectExprHeight(tls, p)
		/* Make copies of constant WHERE-clause terms in the outer query down
		 ** inside the subquery.  This can help the subquery to run more efficiently.
		 ** This is the "predicate push-down optimization".  tag-select-0420
		 */
		if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_PushDown)) == uint32(0) && (int32(*(*uint32)(unsafe.Pointer(pItem1 + 12 + 4))&0x200>>9) == 0 || int32((*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem1 + 36)))).FeM10d) != M10d_Yes && (*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem1 + 36)))).FnUse < int32(2)) && _pushDownWhereTerms(tls, pParse, pSub1, (*TSelect)(unsafe.Pointer(p)).FpWhere, pTabList, i) != 0 {
		} else {
		}
		/* Convert unused result columns of the subquery into simple NULL
		 ** expressions, to avoid unneeded searching and computation.
		 ** tag-select-0440
		 */
		if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_NullUnusedCols)) == uint32(0) && _disableUnusedSubqueryResultColumns(tls, pItem1) != 0 {
		}
		zSavedAuthContext = (*TParse)(unsafe.Pointer(pParse)).FzAuthContext
		(*TParse)(unsafe.Pointer(pParse)).FzAuthContext = (*TSrcItem)(unsafe.Pointer(pItem1)).FzName
		/* Generate byte-code to implement the subquery  tag-select-0480
		 */
		if _fromClauseTermCanBeCoroutine(tls, pParse, pTabList, i, int32((*TSelect)(unsafe.Pointer(p)).FselFlags)) != 0 {
			/* Implement a co-routine that will return a single row of the result
			 ** set on each invocation.  tag-select-0482
			 */
			addrTop = _sqlite3VdbeCurrentAddr(tls, v) + int32(1)
			v1 = pParse + 48
			*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
			v12 = *(*int32)(unsafe.Pointer(v1))
			(*TSubquery)(unsafe.Pointer(pSubq)).FregReturn = v12
			_sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn, 0, addrTop)
			(*TSubquery)(unsafe.Pointer(pSubq)).FaddrFillSub = addrTop
			_sqlite3SelectDestInit(tls, bp+56, int32(SRT_Coroutine), (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn)
			_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22956, libc.VaList(bp+128, pItem1))
			_sqlite3Select(tls, pParse, pSub1, bp+56)
			(*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem1)).FpSTab)).FnRowLogEst = (*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow
			libc.SetBitFieldPtr32Uint32(pItem1+12+4, libc.Uint32FromInt32(1), 6, 0x40)
			(*TSubquery)(unsafe.Pointer(pSubq)).FregResult = (**(**TSelectDest)(__ccgo_up(bp + 56))).FiSdst
			_sqlite3VdbeEndCoroutine(tls, v, (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn)
			_sqlite3VdbeJumpHere(tls, v, addrTop-int32(1))
			_sqlite3ClearTempRegCache(tls, pParse)
		} else {
			if int32(*(*uint32)(unsafe.Pointer(pItem1 + 12 + 4))&0x200>>9) != 0 && (*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem1 + 36)))).FaddrM9e > 0 {
				/* This is a CTE for which materialization code has already been
				 ** generated.  Invoke the subroutine to compute the materialization,
				 ** then make the pItem->iCursor be a copy of the ephemeral table that
				 ** holds the result of the materialization. tag-select-0484 */
				pCteUse = *(*uintptr)(unsafe.Pointer(pItem1 + 36))
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TCteUse)(unsafe.Pointer(pCteUse)).FregRtn, (*TCteUse)(unsafe.Pointer(pCteUse)).FaddrM9e)
				if (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor != (*TCteUse)(unsafe.Pointer(pCteUse)).FiCur {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor, (*TCteUse)(unsafe.Pointer(pCteUse)).FiCur)
				}
				(*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow = (*TCteUse)(unsafe.Pointer(pCteUse)).FnRowEst
			} else {
				v1 = _isSelfJoinView(tls, pTabList, pItem1, 0, i)
				pPrior = v1
				if v1 != uintptr(0) {
					pPriorSubq = *(*uintptr)(unsafe.Pointer(pPrior + 44))
					if (*TSubquery)(unsafe.Pointer(pPriorSubq)).FaddrFillSub != 0 {
						_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TSubquery)(unsafe.Pointer(pPriorSubq)).FregReturn, (*TSubquery)(unsafe.Pointer(pPriorSubq)).FaddrFillSub)
					}
					_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor, (*TSrcItem)(unsafe.Pointer(pPrior)).FiCursor)
					(*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow = (*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(pPriorSubq)).FpSelect)).FnSelectRow
				} else {
					onceAddr = 0
					v1 = pParse + 48
					*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
					v12 = *(*int32)(unsafe.Pointer(v1))
					(*TSubquery)(unsafe.Pointer(pSubq)).FregReturn = v12
					topAddr = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto))
					(*TSubquery)(unsafe.Pointer(pSubq)).FaddrFillSub = topAddr + int32(1)
					libc.SetBitFieldPtr32Uint32(pItem1+12+4, libc.Uint32FromInt32(1), 5, 0x20)
					if int32(*(*uint32)(unsafe.Pointer(pItem1 + 12 + 4))&0x10>>4) == 0 {
						/* If the subquery is not correlated and if we are not inside of
						 ** a trigger, then we only need to compute the value of the subquery
						 ** once. */
						onceAddr = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
					} else {
					}
					_sqlite3SelectDestInit(tls, bp+56, int32(SRT_EphemTab), (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor)
					_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22971, libc.VaList(bp+128, pItem1))
					_sqlite3Select(tls, pParse, pSub1, bp+56)
					(*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem1)).FpSTab)).FnRowLogEst = (*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow
					if onceAddr != 0 {
						_sqlite3VdbeJumpHere(tls, v, onceAddr)
					}
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Return), (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn, topAddr+int32(1))
					_sqlite3VdbeJumpHere(tls, v, topAddr)
					_sqlite3ClearTempRegCache(tls, pParse)
					if int32(*(*uint32)(unsafe.Pointer(pItem1 + 12 + 4))&0x200>>9) != 0 && int32(*(*uint32)(unsafe.Pointer(pItem1 + 12 + 4))&0x10>>4) == 0 {
						pCteUse1 = *(*uintptr)(unsafe.Pointer(pItem1 + 36))
						(*TCteUse)(unsafe.Pointer(pCteUse1)).FaddrM9e = (*TSubquery)(unsafe.Pointer(pSubq)).FaddrFillSub
						(*TCteUse)(unsafe.Pointer(pCteUse1)).FregRtn = (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn
						(*TCteUse)(unsafe.Pointer(pCteUse1)).FiCur = (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor
						(*TCteUse)(unsafe.Pointer(pCteUse1)).FnRowEst = (*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow
					}
				}
			}
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			goto select_end
		}
		**(**int32)(__ccgo_up(pParse + 228)) -= _sqlite3SelectExprHeight(tls, p)
		(*TParse)(unsafe.Pointer(pParse)).FzAuthContext = zSavedAuthContext
		goto _11
	_11:
		;
		i = i + 1
	}
	/* Various elements of the SELECT copied into local variables for
	 ** convenience */
	pEList = (*TSelect)(unsafe.Pointer(p)).FpEList
	pWhere = (*TSelect)(unsafe.Pointer(p)).FpWhere
	pGroupBy = (*TSelect)(unsafe.Pointer(p)).FpGroupBy
	pHaving = (*TSelect)(unsafe.Pointer(p)).FpHaving
	(**(**TDistinctCtx)(__ccgo_up(bp))).FisTnct = libc.BoolUint8((*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != uint32(0))
	/* tag-select-0500
	 **
	 ** If the query is DISTINCT with an ORDER BY but is not an aggregate, and
	 ** if the select-list is the same as the ORDER BY list, then this query
	 ** can be rewritten as a GROUP BY. In other words, this:
	 **
	 **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
	 **
	 ** is transformed to:
	 **
	 **     SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
	 **
	 ** The second form is preferred as a single index (or temp-table) may be
	 ** used for both the ORDER BY and DISTINCT processing. As originally
	 ** written the query must use a temp-table for at least one of the ORDER
	 ** BY and DISTINCT, and an index or separate temp-table for the other.
	 */
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) == uint32(SF_Distinct) && _sqlite3CopySortOrder(tls, pEList, (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy) != 0 && _sqlite3ExprListCompare(tls, pEList, (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy, -int32(1)) == 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_GroupByOrder)) == uint32(0) && (*TSelect)(unsafe.Pointer(p)).FpWin == uintptr(0) {
		**(**Tu32)(__ccgo_up(p + 4)) &= ^libc.Uint32FromInt32(SF_Distinct)
		v1 = _sqlite3ExprListDup(tls, db, pEList, 0)
		(*TSelect)(unsafe.Pointer(p)).FpGroupBy = v1
		pGroupBy = v1
		if pGroupBy != 0 {
			i = 0
			for {
				if !(i < (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr) {
					break
				}
				*(*Tu16)(unsafe.Pointer(pGroupBy + 8 + uintptr(i)*20 + 16)) = uint16(i + int32(1))
				goto _18
			_18:
				;
				i = i + 1
			}
		}
		**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_Aggregate)
		/* Notice that even thought SF_Distinct has been cleared from p->selFlags,
		 ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
		 ** original setting of the SF_Distinct flag, not the current setting */
		(**(**TDistinctCtx)(__ccgo_up(bp))).FisTnct = uint8(2)
	}
	/* If there is an ORDER BY clause, then create an ephemeral index to
	 ** do the sorting.  But this sorting ephemeral index might end up
	 ** being unused if the data can be extracted in pre-sorted order.
	 ** If that is the case, then the OP_OpenEphemeral instruction will be
	 ** changed to an OP_Noop once we figure out that the sorting index is
	 ** not needed.  The sSort.addrSortIndex variable is used to facilitate
	 ** that change.  tag-select-0600
	 */
	if (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy != 0 {
		pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy, 0, (*TExprList)(unsafe.Pointer(pEList)).FnExpr)
		v1 = pParse + 44
		v12 = *(*int32)(unsafe.Pointer(v1))
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		(**(**TSortCtx)(__ccgo_up(bp + 12))).FiECursor = v12
		(**(**TSortCtx)(__ccgo_up(bp + 12))).FaddrSortIndex = _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), (**(**TSortCtx)(__ccgo_up(bp + 12))).FiECursor, (*TExprList)(unsafe.Pointer((**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy)).FnExpr+int32(1)+(*TExprList)(unsafe.Pointer(pEList)).FnExpr, 0, pKeyInfo, -int32(9))
	} else {
		(**(**TSortCtx)(__ccgo_up(bp + 12))).FaddrSortIndex = -int32(1)
	}
	/* If the output is destined for a temporary table, open that table.
	 ** tag-select-0630
	 */
	if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) == int32(SRT_EphemTab) {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TSelectDest)(unsafe.Pointer(pDest)).FiSDParm, (*TExprList)(unsafe.Pointer(pEList)).FnExpr)
		if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_NestedFrom) != 0 {
			ii = (*TExprList)(unsafe.Pointer(pEList)).FnExpr - int32(1)
			for {
				if !(ii > 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20 + 8 + 4))&0x40>>6)) == 0) {
					break
				}
				_sqlite3ExprDelete(tls, db, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20))).FpExpr)
				_sqlite3DbFree(tls, db, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20))).FzEName)
				(*TExprList)(unsafe.Pointer(pEList)).FnExpr = (*TExprList)(unsafe.Pointer(pEList)).FnExpr - 1
				goto _21
			_21:
				;
				ii = ii - 1
			}
			ii = 0
			for {
				if !(ii < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) {
					break
				}
				if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20 + 8 + 4))&0x40>>6)) == 0 {
					(*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*20))).FpExpr)).Fop = uint8(TK_NULL)
				}
				goto _22
			_22:
				;
				ii = ii + 1
			}
		}
	}
	/* Set the limiter.  tag-select-0650
	 */
	iEnd = _sqlite3VdbeMakeLabel(tls, pParse)
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_FixedLimit) == uint32(0) {
		(*TSelect)(unsafe.Pointer(p)).FnSelectRow = int16(320) /* 4 billion rows */
	}
	if (*TSelect)(unsafe.Pointer(p)).FpLimit != 0 {
		_computeLimitRegisters(tls, pParse, p, iEnd)
	}
	if (*TSelect)(unsafe.Pointer(p)).FiLimit == 0 && (**(**TSortCtx)(__ccgo_up(bp + 12))).FaddrSortIndex >= 0 {
		_sqlite3VdbeChangeOpcode(tls, v, (**(**TSortCtx)(__ccgo_up(bp + 12))).FaddrSortIndex, uint8(OP_SorterOpen))
		v1 = bp + 12 + 32
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(SORTFLAG_UseSorter))
	}
	/* Open an ephemeral index to use for the distinct set. tag-select-0680
	 */
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) != 0 {
		v1 = pParse + 44
		v12 = *(*int32)(unsafe.Pointer(v1))
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		(**(**TDistinctCtx)(__ccgo_up(bp))).FtabTnct = v12
		(**(**TDistinctCtx)(__ccgo_up(bp))).FaddrTnct = _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), (**(**TDistinctCtx)(__ccgo_up(bp))).FtabTnct, 0, 0, _sqlite3KeyInfoFromExprList(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpEList, 0, 0), -int32(9))
		_sqlite3VdbeChangeP5(tls, v, uint16(BTREE_UNORDERED))
		(**(**TDistinctCtx)(__ccgo_up(bp))).FeTnctType = uint8(WHERE_DISTINCT_UNORDERED)
	} else {
		(**(**TDistinctCtx)(__ccgo_up(bp))).FeTnctType = uint8(WHERE_DISTINCT_NOOP)
	}
	if !(isAgg != 0) && pGroupBy == uintptr(0) {
		if (**(**TDistinctCtx)(__ccgo_up(bp))).FisTnct != 0 {
			v12 = int32(WHERE_WANT_DISTINCT)
		} else {
			v12 = 0
		}
		/* No aggregate functions and no GROUP BY clause.  tag-select-0700 */
		wctrlFlags = uint16(uint32(v12) | (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_FixedLimit))
		pWin = (*TSelect)(unsafe.Pointer(p)).FpWin /* Main window object (or NULL) */
		if pWin != 0 {
			_sqlite3WindowCodeInit(tls, pParse, p)
		}
		/* Begin the database scan. */
		pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy, (*TSelect)(unsafe.Pointer(p)).FpEList, p, wctrlFlags, int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow))
		if pWInfo == uintptr(0) {
			goto select_end
		}
		if int32(_sqlite3WhereOutputRowCount(tls, pWInfo)) < int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) {
			(*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3WhereOutputRowCount(tls, pWInfo)
			if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) <= int32(SRT_DistQueue) && int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) >= int32(SRT_DistFifo) {
				/* TUNING: For a UNION CTE, because UNION is implies DISTINCT,
				 ** reduce the estimated output row count by 8 (LogEst 30).
				 ** Search for tag-20250414a to see other cases */
				v1 = p + 2
				*(*TLogEst)(unsafe.Pointer(v1)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v1))) - libc.Int32FromInt32(30))
			}
		}
		if (**(**TDistinctCtx)(__ccgo_up(bp))).FisTnct != 0 && _sqlite3WhereIsDistinct(tls, pWInfo) != 0 {
			(**(**TDistinctCtx)(__ccgo_up(bp))).FeTnctType = uint8(_sqlite3WhereIsDistinct(tls, pWInfo))
		}
		if (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy != 0 {
			(**(**TSortCtx)(__ccgo_up(bp + 12))).FnOBSat = _sqlite3WhereIsOrdered(tls, pWInfo)
			(**(**TSortCtx)(__ccgo_up(bp + 12))).FlabelOBLopt = _sqlite3WhereOrderByLimitOptLabel(tls, pWInfo)
			if (**(**TSortCtx)(__ccgo_up(bp + 12))).FnOBSat == (*TExprList)(unsafe.Pointer((**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy)).FnExpr {
				(**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy = uintptr(0)
			}
		}
		/* If sorting index that was created by a prior OP_OpenEphemeral
		 ** instruction ended up not being needed, then change the OP_OpenEphemeral
		 ** into an OP_Noop.
		 */
		if (**(**TSortCtx)(__ccgo_up(bp + 12))).FaddrSortIndex >= 0 && (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy == uintptr(0) {
			_sqlite3VdbeChangeToNoop(tls, v, (**(**TSortCtx)(__ccgo_up(bp + 12))).FaddrSortIndex)
		}
		if pWin != 0 {
			addrGosub = _sqlite3VdbeMakeLabel(tls, pParse)
			iCont = _sqlite3VdbeMakeLabel(tls, pParse)
			iBreak = _sqlite3VdbeMakeLabel(tls, pParse)
			v1 = pParse + 48
			*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
			v12 = *(*int32)(unsafe.Pointer(v1))
			regGosub = v12
			_sqlite3WindowCodeStep(tls, pParse, p, pWInfo, regGosub, addrGosub)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, iBreak)
			_sqlite3VdbeResolveLabel(tls, v, addrGosub)
			(**(**TSortCtx)(__ccgo_up(bp + 12))).FlabelOBLopt = 0
			_selectInnerLoop(tls, pParse, p, -int32(1), bp+12, bp, pDest, iCont, iBreak)
			_sqlite3VdbeResolveLabel(tls, v, iCont)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regGosub)
			_sqlite3VdbeResolveLabel(tls, v, iBreak)
		} else {
			/* Use the standard inner loop. */
			_selectInnerLoop(tls, pParse, p, -int32(1), bp+12, bp, pDest, _sqlite3WhereContinueLabel(tls, pWInfo), _sqlite3WhereBreakLabel(tls, pWInfo))
			/* End the database scan loop.
			 */
			_sqlite3WhereEnd(tls, pWInfo)
		}
	} else { /* End of processing for this SELECT */
		sortPTab = 0   /* Pseudotable used to decode sorting results */
		sortOut = 0    /* Output register from the sorter */
		orderByGrp = 0 /* True if the GROUP BY and ORDER BY are the same */
		/* Remove any and all aliases between the result set and the
		 ** GROUP BY clause.
		 */
		if pGroupBy != 0 { /* For looping over expression in a list */
			k = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList)).FnExpr
			pItem2 = (*TSelect)(unsafe.Pointer(p)).FpEList + 8
			for {
				if !(k > 0) {
					break
				}
				(*(*struct {
					FiOrderByCol Tu16
					FiAlias      Tu16
				})(unsafe.Pointer(pItem2 + 16))).FiAlias = uint16(0)
				goto _30
			_30:
				;
				k = k - 1
				pItem2 += 20
			}
			k = (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr
			pItem2 = pGroupBy + 8
			for {
				if !(k > 0) {
					break
				}
				(*(*struct {
					FiOrderByCol Tu16
					FiAlias      Tu16
				})(unsafe.Pointer(pItem2 + 16))).FiAlias = uint16(0)
				goto _31
			_31:
				;
				k = k - 1
				pItem2 += 20
			}
			if int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32(66) {
				(*TSelect)(unsafe.Pointer(p)).FnSelectRow = int16(66)
			}
			/* If there is both a GROUP BY and an ORDER BY clause and they are
			 ** identical, then it may be possible to disable the ORDER BY clause
			 ** on the grounds that the GROUP BY will cause elements to come out
			 ** in the correct order. It also may not - the GROUP BY might use a
			 ** database index that causes rows to be grouped together as required
			 ** but not actually sorted. Either way, record the fact that the
			 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
			 ** variable.  */
			if _sqlite3CopySortOrder(tls, pGroupBy, (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy) != 0 && _sqlite3ExprListCompare(tls, pGroupBy, (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy, -int32(1)) == 0 {
				orderByGrp = int32(1)
			}
		} else {
			(*TSelect)(unsafe.Pointer(p)).FnSelectRow = 0
		}
		/* Create a label to jump to when we want to abort the query */
		addrEnd = _sqlite3VdbeMakeLabel(tls, pParse)
		/* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
		 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
		 ** SELECT statement.
		 */
		pAggInfo = _sqlite3DbMallocZero(tls, db, uint64(48))
		if pAggInfo != 0 {
			_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_agginfoFree), pAggInfo)
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			goto select_end
		}
		(*TAggInfo)(unsafe.Pointer(pAggInfo)).FselId = (*TSelect)(unsafe.Pointer(p)).FselId
		libc.Xmemset(tls, bp+84, 0, uint32(36))
		(**(**TNameContext)(__ccgo_up(bp + 84))).FpParse = pParse
		(**(**TNameContext)(__ccgo_up(bp + 84))).FpSrcList = pTabList
		*(*uintptr)(unsafe.Pointer(bp + 84 + 8)) = pAggInfo
		if pGroupBy != 0 {
			v12 = (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr
		} else {
			v12 = 0
		}
		(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnSortingColumn = uint32(v12)
		(*TAggInfo)(unsafe.Pointer(pAggInfo)).FpGroupBy = pGroupBy
		_sqlite3ExprAnalyzeAggList(tls, bp+84, pEList)
		_sqlite3ExprAnalyzeAggList(tls, bp+84, (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy)
		if pHaving != 0 {
			if pGroupBy != 0 {
				_havingToWhere(tls, pParse, p)
				pWhere = (*TSelect)(unsafe.Pointer(p)).FpWhere
			}
			_sqlite3ExprAnalyzeAggregates(tls, bp+84, pHaving)
		}
		(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn
		if (*TSelect)(unsafe.Pointer(p)).FpGroupBy == uintptr(0) && (*TSelect)(unsafe.Pointer(p)).FpHaving == uintptr(0) && (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc == int32(1) {
			minMaxFlag = _minMaxQuery(tls, db, (**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr, bp+52)
		} else {
			minMaxFlag = uint8(WHERE_ORDERBY_NORMAL)
		}
		_analyzeAggFuncArgs(tls, pAggInfo, bp+84)
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			goto select_end
		}
		/* Processing for aggregates with GROUP BY is very different and
		 ** much more complex than aggregates without a GROUP BY.  tag-select-0810
		 */
		if pGroupBy != 0 { /* Return address register for reset subroutine */
			pDistinct = uintptr(0)
			distFlag = uint16(0)
			eDist = WHERE_DISTINCT_NOOP
			if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc == int32(1) && (**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FiDistinct >= 0 && (**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr != uintptr(0) && (*TExpr)(unsafe.Pointer((**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr)).Fflags&uint32(EP_xIsSelect) == uint32(0) && *(*uintptr)(unsafe.Pointer((**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr + 20)) != uintptr(0) {
				pExpr = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr + 20)) + 8))).FpExpr
				pExpr = _sqlite3ExprDup(tls, db, pExpr, 0)
				pDistinct = _sqlite3ExprListDup(tls, db, pGroupBy, 0)
				pDistinct = _sqlite3ExprListAppend(tls, pParse, pDistinct, pExpr)
				if pDistinct != 0 {
					v12 = libc.Int32FromInt32(WHERE_WANT_DISTINCT) | libc.Int32FromInt32(WHERE_AGG_DISTINCT)
				} else {
					v12 = 0
				}
				distFlag = uint16(v12)
			}
			/* If there is a GROUP BY clause we might need a sorting index to
			 ** implement it.  Allocate that sorting index now.  If it turns out
			 ** that we do not need it after all, the OP_SorterOpen instruction
			 ** will be converted into a Noop.
			 */
			v1 = pParse + 44
			v12 = *(*int32)(unsafe.Pointer(v1))
			*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
			(*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx = v12
			pKeyInfo1 = _sqlite3KeyInfoFromExprList(tls, pParse, pGroupBy, 0, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn)
			addrSortingIdx = _sqlite3VdbeAddOp4(tls, v, int32(OP_SorterOpen), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, int32((*TAggInfo)(unsafe.Pointer(pAggInfo)).FnSortingColumn), 0, pKeyInfo1, -int32(9))
			/* Initialize memory locations used by GROUP BY aggregate processing
			 */
			v1 = pParse + 48
			*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
			v12 = *(*int32)(unsafe.Pointer(v1))
			iUseFlag = v12
			v1 = pParse + 48
			*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
			v12 = *(*int32)(unsafe.Pointer(v1))
			iAbortFlag = v12
			v1 = pParse + 48
			*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
			v12 = *(*int32)(unsafe.Pointer(v1))
			regOutputRow = v12
			addrOutputRow = _sqlite3VdbeMakeLabel(tls, pParse)
			v1 = pParse + 48
			*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
			v12 = *(*int32)(unsafe.Pointer(v1))
			regReset = v12
			addrReset = _sqlite3VdbeMakeLabel(tls, pParse)
			iAMem = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
			**(**int32)(__ccgo_up(pParse + 48)) += (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr
			iBMem = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
			**(**int32)(__ccgo_up(pParse + 48)) += (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, iAbortFlag)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, iAMem, iAMem+(*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr-int32(1))
			_sqlite3ExprNullRegisterRange(tls, pParse, iAMem, (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr)
			/* Begin a loop that will extract all source rows in GROUP BY order.
			 ** This might involve two separate loops with an OP_Sort in between, or
			 ** it might be a single loop that uses an index to extract information
			 ** in the right order to begin with.
			 */
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regReset, addrReset)
			if int32((**(**TDistinctCtx)(__ccgo_up(bp))).FisTnct) == int32(2) {
				v12 = int32(WHERE_DISTINCTBY)
			} else {
				v12 = int32(WHERE_GROUPBY)
			}
			if orderByGrp != 0 {
				v15 = int32(WHERE_SORTBYGROUP)
			} else {
				v15 = 0
			}
			pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, pGroupBy, pDistinct, p, uint16(v12|v15|int32(distFlag)), 0)
			if pWInfo == uintptr(0) {
				_sqlite3ExprListDelete(tls, db, pDistinct)
				goto select_end
			}
			if (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr != 0 {
				_optimizeAggregateUseOfIndexedExpr(tls, pParse, p, pAggInfo, bp+84)
			}
			_assignAggregateRegisters(tls, pParse, pAggInfo)
			eDist = _sqlite3WhereIsDistinct(tls, pWInfo)
			if _sqlite3WhereIsOrdered(tls, pWInfo) == (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr {
				/* The optimizer is able to deliver rows in group by order so
				 ** we do not have to sort.  The OP_OpenEphemeral table will be
				 ** cancelled later because we still need to use the pKeyInfo
				 */
				groupBySort = 0
			} else {
				if (**(**TDistinctCtx)(__ccgo_up(bp))).FisTnct != 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Distinct) == uint32(0) {
					v1 = __ccgo_ts + 22987
				} else {
					v1 = __ccgo_ts + 22996
				}
				_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+21752, libc.VaList(bp+128, v1))
				groupBySort = int32(1)
				nGroupBy = (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr
				nCol = nGroupBy
				j = nGroupBy
				i = 0
				for {
					if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn) {
						break
					}
					if (**(**TAggInfo_col)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(i)*20))).FiSorterColumn >= j {
						nCol = nCol + 1
						j = j + 1
					}
					goto _47
				_47:
					;
					i = i + 1
				}
				regBase = _sqlite3GetTempRange(tls, pParse, nCol)
				_sqlite3ExprCodeExprList(tls, pParse, pGroupBy, regBase, 0, uint8(0))
				j = nGroupBy
				(*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(1)
				i = 0
				for {
					if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn) {
						break
					}
					pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(i)*20
					if (*TAggInfo_col)(unsafe.Pointer(pCol)).FiSorterColumn >= j {
						_sqlite3ExprCode(tls, pParse, (*TAggInfo_col)(unsafe.Pointer(pCol)).FpCExpr, j+regBase)
						j = j + 1
					}
					goto _48
				_48:
					;
					i = i + 1
				}
				(*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(0)
				regRecord = _sqlite3GetTempReg(tls, pParse)
				_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regBase, nCol, regRecord)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_SorterInsert), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, regRecord)
				_sqlite3ReleaseTempReg(tls, pParse, regRecord)
				_sqlite3ReleaseTempRange(tls, pParse, regBase, nCol)
				_sqlite3WhereEnd(tls, pWInfo)
				v1 = pParse + 44
				v15 = *(*int32)(unsafe.Pointer(v1))
				*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
				v12 = v15
				sortPTab = v12
				(*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdxPTab = v12
				sortOut = _sqlite3GetTempReg(tls, pParse)
				_sqlite3VdbeAddOp3(tls, v, int32(OP_OpenPseudo), sortPTab, sortOut, nCol)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_SorterSort), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, addrEnd)
				(*TAggInfo)(unsafe.Pointer(pAggInfo)).FuseSortingIdx = uint8(1)
			}
			/* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
			 ** that are indexed (and that were previously identified and tagged
			 ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
			 ** must now be converted into a TK_AGG_COLUMN node so that the value
			 ** is correctly pulled from the index rather than being recomputed. */
			if (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr != 0 {
				_aggregateConvertIndexedExprRefToColumn(tls, pAggInfo)
			}
			/* If the index or temporary table used by the GROUP BY sort
			 ** will naturally deliver rows in the order required by the ORDER BY
			 ** clause, cancel the ephemeral table open coded earlier.
			 **
			 ** This is an optimization - the correct answer should result regardless.
			 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
			 ** disable this optimization for testing purposes.  */
			if orderByGrp != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_GroupByOrder)) == uint32(0) && (groupBySort != 0 || _sqlite3WhereIsSorted(tls, pWInfo) != 0) {
				(**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy = uintptr(0)
				_sqlite3VdbeChangeToNoop(tls, v, (**(**TSortCtx)(__ccgo_up(bp + 12))).FaddrSortIndex)
			}
			/* Evaluate the current GROUP BY terms and store in b0, b1, b2...
			 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
			 ** Then compare the current GROUP BY terms against the GROUP BY terms
			 ** from the previous row currently stored in a0, a1, a2...
			 */
			addrTopOfLoop = _sqlite3VdbeCurrentAddr(tls, v)
			if groupBySort != 0 {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_SorterData), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, sortOut, sortPTab)
			}
			j = 0
			for {
				if !(j < (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr) {
					break
				}
				iOrderByCol = int32(*(*Tu16)(unsafe.Pointer(pGroupBy + 8 + uintptr(j)*20 + 16)))
				if groupBySort != 0 {
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), sortPTab, j, iBMem+j)
				} else {
					(*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(1)
					_sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pGroupBy + 8 + uintptr(j)*20))).FpExpr, iBMem+j)
				}
				if iOrderByCol != 0 {
					pX = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8 + uintptr(iOrderByCol-int32(1))*20))).FpExpr
					pBase = _sqlite3ExprSkipCollateAndLikely(tls, pX)
					for pBase != uintptr(0) && int32((*TExpr)(unsafe.Pointer(pBase)).Fop) == int32(TK_IF_NULL_ROW) {
						pX = (*TExpr)(unsafe.Pointer(pBase)).FpLeft
						pBase = _sqlite3ExprSkipCollateAndLikely(tls, pX)
					}
					if pBase != uintptr(0) && int32((*TExpr)(unsafe.Pointer(pBase)).Fop) != int32(TK_AGG_COLUMN) && int32((*TExpr)(unsafe.Pointer(pBase)).Fop) != int32(TK_REGISTER) {
						_sqlite3ExprToRegister(tls, pX, iAMem+j)
					}
				}
				goto _52
			_52:
				;
				j = j + 1
			}
			_sqlite3VdbeAddOp4(tls, v, int32(OP_Compare), iAMem, iBMem, (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr, _sqlite3KeyInfoRef(tls, pKeyInfo1), -int32(9))
			addr1 = _sqlite3VdbeCurrentAddr(tls, v)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addr1+int32(1), 0, addr1+int32(1))
			/* Generate code that runs whenever the GROUP BY changes.
			 ** Changes in the GROUP BY are detected by the previous code
			 ** block.  If there were no changes, this block is skipped.
			 **
			 ** This code copies current group by terms in b0,b1,b2,...
			 ** over to a0,a1,a2.  It then calls the output subroutine
			 ** and resets the aggregate accumulator registers in preparation
			 ** for the next GROUP BY batch.
			 */
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutputRow, addrOutputRow)
			_sqlite3ExprCodeMove(tls, pParse, iBMem, iAMem, (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IfPos), iAbortFlag, addrEnd)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regReset, addrReset)
			/* Update the aggregate accumulators based on the content of
			 ** the current row
			 */
			_sqlite3VdbeJumpHere(tls, v, addr1)
			_updateAccumulator(tls, pParse, iUseFlag, pAggInfo, eDist)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), iUseFlag)
			/* End of the loop
			 */
			if groupBySort != 0 {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_SorterNext), (*TAggInfo)(unsafe.Pointer(pAggInfo)).FsortingIdx, addrTopOfLoop)
			} else {
				_sqlite3WhereEnd(tls, pWInfo)
				_sqlite3VdbeChangeToNoop(tls, v, addrSortingIdx)
			}
			_sqlite3ExprListDelete(tls, db, pDistinct)
			/* Output the final row of result
			 */
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regOutputRow, addrOutputRow)
			/* Jump over the subroutines
			 */
			_sqlite3VdbeGoto(tls, v, addrEnd)
			/* Generate a subroutine that outputs a single row of the result
			 ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
			 ** is less than or equal to zero, the subroutine is a no-op.  If
			 ** the processing calls for the query to abort, this subroutine
			 ** increments the iAbortFlag memory location before returning in
			 ** order to signal the caller to abort.
			 */
			addrSetAbort = _sqlite3VdbeCurrentAddr(tls, v)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), iAbortFlag)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regOutputRow)
			_sqlite3VdbeResolveLabel(tls, v, addrOutputRow)
			addrOutputRow = _sqlite3VdbeCurrentAddr(tls, v)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IfPos), iUseFlag, addrOutputRow+int32(2))
			_sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regOutputRow)
			_finalizeAggFunctions(tls, pParse, pAggInfo)
			_sqlite3ExprIfFalse(tls, pParse, pHaving, addrOutputRow+int32(1), int32(SQLITE_JUMPIFNULL))
			_selectInnerLoop(tls, pParse, p, -int32(1), bp+12, bp, pDest, addrOutputRow+int32(1), addrSetAbort)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regOutputRow)
			/* Generate a subroutine that will reset the group-by accumulator
			 */
			_sqlite3VdbeResolveLabel(tls, v, addrReset)
			_resetAccumulator(tls, pParse, pAggInfo)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, iUseFlag)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regReset)
			if int32(distFlag) != 0 && eDist != WHERE_DISTINCT_NOOP {
				pF = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc
				_fixDistinctOpenEph(tls, pParse, eDist, (*TAggInfo_func)(unsafe.Pointer(pF)).FiDistinct, (*TAggInfo_func)(unsafe.Pointer(pF)).FiDistAddr)
			}
		} else {
			v1 = _isSimpleCount(tls, p, pAggInfo)
			pTab1 = v1
			if v1 != uintptr(0) {
				/* tag-select-0821
				 **
				 ** If isSimpleCount() returns a pointer to a Table structure, then
				 ** the SQL statement is of the form:
				 **
				 **   SELECT count(*) FROM <tbl>
				 **
				 ** where the Table structure returned represents table <tbl>.
				 **
				 ** This statement is so common that it is optimized specially. The
				 ** OP_Count instruction is executed either on the intkey table that
				 ** contains the data for table <tbl> or on one of its indexes. It
				 ** is better to execute the op on an index, as indexes are almost
				 ** always spread across less pages than their corresponding tables.
				 */
				iDb1 = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab1)).FpSchema)
				v3 = pParse + 44
				v12 = *(*int32)(unsafe.Pointer(v3))
				*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
				iCsr = v12                                     /* Iterator variable */
				pKeyInfo2 = uintptr(0)                         /* Keyinfo for scanned index */
				pBest = uintptr(0)                             /* Best index found so far */
				iRoot = (*TTable)(unsafe.Pointer(pTab1)).Ftnum /* Root page of scanned b-tree */
				_sqlite3CodeVerifySchema(tls, pParse, iDb1)
				_sqlite3TableLock(tls, pParse, iDb1, (*TTable)(unsafe.Pointer(pTab1)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab1)).FzName)
				/* Search for the index that has the lowest scan cost.
				 **
				 ** (2011-04-15) Do not do a full scan of an unordered index.
				 **
				 ** (2013-10-03) Do not count the entries in a partial index.
				 **
				 ** In practice the KeyInfo structure will not be used. It is only
				 ** passed to keep OP_OpenRead happy.
				 */
				if !((*TTable)(unsafe.Pointer(pTab1)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
					pBest = _sqlite3PrimaryKeyIndex(tls, pTab1)
				}
				if !(int32(*(*uint32)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8 + 12 + 4))&0x1>>0) != 0) {
					pIdx = (*TTable)(unsafe.Pointer(pTab1)).FpIndex
					for {
						if !(pIdx != 0) {
							break
						}
						if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x4>>2)) == 0 && int32((*TIndex)(unsafe.Pointer(pIdx)).FszIdxRow) < int32((*TTable)(unsafe.Pointer(pTab1)).FszTabRow) && (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere == uintptr(0) && (!(pBest != 0) || int32((*TIndex)(unsafe.Pointer(pIdx)).FszIdxRow) < int32((*TIndex)(unsafe.Pointer(pBest)).FszIdxRow)) {
							pBest = pIdx
						}
						goto _56
					_56:
						;
						pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
					}
				}
				if pBest != 0 {
					iRoot = (*TIndex)(unsafe.Pointer(pBest)).Ftnum
					pKeyInfo2 = _sqlite3KeyInfoOfIndex(tls, pParse, pBest)
				}
				/* Open a read-only cursor, execute the OP_Count, close the cursor. */
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_OpenRead), iCsr, int32(iRoot), iDb1, int32(1))
				if pKeyInfo2 != 0 {
					_sqlite3VdbeChangeP4(tls, v, -int32(1), pKeyInfo2, -int32(9))
				}
				_assignAggregateRegisters(tls, pParse, pAggInfo)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Count), iCsr, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn+0)
				_sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iCsr)
				_explainSimpleCount(tls, pParse, pTab1, pBest)
			} else {
				/* The general case of an aggregate query without GROUP BY
				 ** tag-select-0822 */
				regAcc = 0 /* "populate accumulators" flag */
				pDistinct1 = uintptr(0)
				distFlag1 = uint16(0)
				/* If there are accumulator registers but no min() or max() functions
				 ** without FILTER clauses, allocate register regAcc. Register regAcc
				 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
				 ** The code generated by updateAccumulator() uses this to ensure
				 ** that the accumulator registers are (a) updated only once if
				 ** there are no min() or max functions or (b) always updated for the
				 ** first row visited by the aggregate, so that they are updated at
				 ** least once even if the FILTER clause means the min() or max()
				 ** function visits zero rows.  */
				if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator != 0 {
					i = 0
					for {
						if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) {
							break
						}
						if (*TExpr)(unsafe.Pointer((**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*24))).FpFExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
							goto _57
						}
						if (*TFuncDef)(unsafe.Pointer((**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*24))).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 {
							break
						}
						goto _57
					_57:
						;
						i = i + 1
					}
					if i == (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc {
						v1 = pParse + 48
						*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
						v12 = *(*int32)(unsafe.Pointer(v1))
						regAcc = v12
						_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regAcc)
					}
				} else {
					if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc == int32(1) && (**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FiDistinct >= 0 {
						pDistinct1 = *(*uintptr)(unsafe.Pointer((**(**TAggInfo_func)(__ccgo_up((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr + 20))
						if pDistinct1 != 0 {
							v12 = libc.Int32FromInt32(WHERE_WANT_DISTINCT) | libc.Int32FromInt32(WHERE_AGG_DISTINCT)
						} else {
							v12 = 0
						}
						distFlag1 = uint16(v12)
					}
				}
				_assignAggregateRegisters(tls, pParse, pAggInfo)
				/* This case runs if the aggregate has no GROUP BY clause.  The
				 ** processing is much simpler since there is only a single row
				 ** of output.
				 */
				_resetAccumulator(tls, pParse, pAggInfo)
				/* If this query is a candidate for the min/max optimization, then
				 ** minMaxFlag will have been previously set to either
				 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
				 ** be an appropriate ORDER BY expression for the optimization.
				 */
				pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, **(**uintptr)(__ccgo_up(bp + 52)), pDistinct1, p, uint16(int32(minMaxFlag)|int32(distFlag1)), 0)
				if pWInfo == uintptr(0) {
					goto select_end
				}
				eDist1 = _sqlite3WhereIsDistinct(tls, pWInfo)
				_updateAccumulator(tls, pParse, regAcc, pAggInfo, eDist1)
				if eDist1 != WHERE_DISTINCT_NOOP {
					pF1 = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc
					if pF1 != 0 {
						_fixDistinctOpenEph(tls, pParse, eDist1, (*TAggInfo_func)(unsafe.Pointer(pF1)).FiDistinct, (*TAggInfo_func)(unsafe.Pointer(pF1)).FiDistAddr)
					}
				}
				if regAcc != 0 {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), regAcc)
				}
				if minMaxFlag != 0 {
					_sqlite3WhereMinMaxOptEarlyOut(tls, v, pWInfo)
				}
				_sqlite3WhereEnd(tls, pWInfo)
				_finalizeAggFunctions(tls, pParse, pAggInfo)
			}
			(**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy = uintptr(0)
			_sqlite3ExprIfFalse(tls, pParse, pHaving, addrEnd, int32(SQLITE_JUMPIFNULL))
			_selectInnerLoop(tls, pParse, p, -int32(1), uintptr(0), uintptr(0), pDest, addrEnd, addrEnd)
		}
		_sqlite3VdbeResolveLabel(tls, v, addrEnd)
	} /* endif aggregate query */
	if int32((**(**TDistinctCtx)(__ccgo_up(bp))).FeTnctType) == int32(WHERE_DISTINCT_UNORDERED) {
		_explainTempTable(tls, pParse, __ccgo_ts+22987)
	}
	/* If there is an ORDER BY clause, then we need to sort the results
	 ** and send them to the callback one by one.  tag-select-0900
	 */
	if (**(**TSortCtx)(__ccgo_up(bp + 12))).FpOrderBy != 0 {
		_generateSortTail(tls, pParse, p, bp+12, (*TExprList)(unsafe.Pointer(pEList)).FnExpr, pDest)
	}
	/* Jump here to skip this query
	 */
	_sqlite3VdbeResolveLabel(tls, v, iEnd)
	/* The SELECT has been coded. If there is an error in the Parse structure,
	 ** set the return code to 1. Otherwise 0. */
	rc = libc.BoolInt32((*TParse)(unsafe.Pointer(pParse)).FnErr > 0)
	/* Control jumps to here if an error is encountered above, or upon
	 ** successful coding of the SELECT.
	 */
	goto select_end
select_end:
	;
	_sqlite3ExprListDelete(tls, db, **(**uintptr)(__ccgo_up(bp + 52)))
	_sqlite3VdbeExplainPop(tls, pParse)
	return rc
}

/************** End of select.c **********************************************/
/************** Begin file table.c *******************************************/
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains the sqlite3_get_table() and sqlite3_free_table()
** interface routines.  These are just wrappers around the main
** interface routine of sqlite3_exec().
**
** These routines are in a separate files so that they will not be linked
** if they are not used.
 */
/* #include "sqliteInt.h" */

// C documentation
//
//	/*
//	** Check all ON clauses in pSelect to verify that they do not reference
//	** columns to the right.
//	*/
func _sqlite3SelectCheckOnClauses(tls *libc.TLS, pParse uintptr, pSelect uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var ii int32
	var pItem uintptr
	var _ /* sCtx at bp+28 */ TCheckOnCtx
	var _ /* w at bp+0 */ TWalker
	_, _ = ii, pItem
	libc.Xmemset(tls, bp, 0, uint32(28))
	(**(**TWalker)(__ccgo_up(bp))).FpParse = pParse
	(**(**TWalker)(__ccgo_up(bp))).FxExprCallback = __ccgo_fp(_selectCheckOnClausesExpr)
	(**(**TWalker)(__ccgo_up(bp))).FxSelectCallback = __ccgo_fp(_selectCheckOnClausesSelect)
	*(*uintptr)(unsafe.Pointer(bp + 24)) = bp + 28
	libc.Xmemset(tls, bp+28, 0, uint32(16))
	(**(**TCheckOnCtx)(__ccgo_up(bp + 28))).FpSrc = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc
	_sqlite3WalkExpr(tls, bp, (*TSelect)(unsafe.Pointer(pSelect)).FpWhere)
	**(**Tu32)(__ccgo_up(pSelect + 4)) &= uint32(^libc.Int32FromInt32(SF_OnToWhere))
	/* Check for any table-function args that are attached to virtual tables
	 ** on the RHS of an outer join. They are subject to the same constraints
	 ** as ON clauses. */
	(**(**TCheckOnCtx)(__ccgo_up(bp + 28))).FbFuncArg = int32(1)
	ii = 0
	for {
		if !(ii < (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc)).FnSrc) {
			break
		}
		pItem = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8 + uintptr(ii)*48
		if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x8>>3) != 0 && int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_OUTER) != 0 {
			(**(**TCheckOnCtx)(__ccgo_up(bp + 28))).FiJoin = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor
			_sqlite3WalkExprList(tls, bp, *(*uintptr)(unsafe.Pointer(pItem + 32)))
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
}

func _sqlite3SelectDup(tls *libc.TLS, db uintptr, pDup uintptr, flags int32) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var p, pNew, pNext, pp uintptr
	var _ /* pRet at bp+0 */ uintptr
	_, _, _, _ = p, pNew, pNext, pp
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	pNext = uintptr(0)
	pp = bp
	p = pDup
	for {
		if !(p != 0) {
			break
		}
		pNew = _sqlite3DbMallocRawNN(tls, db, uint64(68))
		if pNew == uintptr(0) {
			break
		}
		(*TSelect)(unsafe.Pointer(pNew)).FpEList = _sqlite3ExprListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpEList, flags)
		(*TSelect)(unsafe.Pointer(pNew)).FpSrc = _sqlite3SrcListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpSrc, flags)
		(*TSelect)(unsafe.Pointer(pNew)).FpWhere = _sqlite3ExprDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpWhere, flags)
		(*TSelect)(unsafe.Pointer(pNew)).FpGroupBy = _sqlite3ExprListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpGroupBy, flags)
		(*TSelect)(unsafe.Pointer(pNew)).FpHaving = _sqlite3ExprDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpHaving, flags)
		(*TSelect)(unsafe.Pointer(pNew)).FpOrderBy = _sqlite3ExprListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, flags)
		(*TSelect)(unsafe.Pointer(pNew)).Fop = (*TSelect)(unsafe.Pointer(p)).Fop
		(*TSelect)(unsafe.Pointer(pNew)).FpNext = pNext
		(*TSelect)(unsafe.Pointer(pNew)).FpPrior = uintptr(0)
		(*TSelect)(unsafe.Pointer(pNew)).FpLimit = _sqlite3ExprDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpLimit, flags)
		(*TSelect)(unsafe.Pointer(pNew)).FiLimit = 0
		(*TSelect)(unsafe.Pointer(pNew)).FiOffset = 0
		(*TSelect)(unsafe.Pointer(pNew)).FselFlags = (*TSelect)(unsafe.Pointer(p)).FselFlags
		(*TSelect)(unsafe.Pointer(pNew)).FnSelectRow = (*TSelect)(unsafe.Pointer(p)).FnSelectRow
		(*TSelect)(unsafe.Pointer(pNew)).FpWith = _sqlite3WithDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpWith)
		(*TSelect)(unsafe.Pointer(pNew)).FpWin = uintptr(0)
		(*TSelect)(unsafe.Pointer(pNew)).FpWinDefn = _sqlite3WindowListDup(tls, db, (*TSelect)(unsafe.Pointer(p)).FpWinDefn)
		if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 {
			_gatherSelectWindows(tls, pNew)
		}
		(*TSelect)(unsafe.Pointer(pNew)).FselId = (*TSelect)(unsafe.Pointer(p)).FselId
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			/* Any prior OOM might have left the Select object incomplete.
			 ** Delete the whole thing rather than allow an incomplete Select
			 ** to be used by the code generator. */
			(*TSelect)(unsafe.Pointer(pNew)).FpNext = uintptr(0)
			_sqlite3SelectDelete(tls, db, pNew)
			break
		}
		**(**uintptr)(__ccgo_up(pp)) = pNew
		pp = pNew + 44
		pNext = pNew
		goto _1
	_1:
		;
		p = (*TSelect)(unsafe.Pointer(p)).FpPrior
	}
	return **(**uintptr)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** Allocate a new Select structure and return a pointer to that
//	** structure.
//	*/
func _sqlite3SelectNew(tls *libc.TLS, pParse uintptr, pEList uintptr, pSrc uintptr, pWhere uintptr, pGroupBy uintptr, pHaving uintptr, pOrderBy uintptr, selFlags Tu32, pLimit uintptr) (r uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var pAllocated, pNew, v1 uintptr
	var v2 int32
	var _ /* standin at bp+0 */ TSelect
	_, _, _, _ = pAllocated, pNew, v1, v2
	v1 = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(68))
	pNew = v1
	pAllocated = v1
	if pNew == uintptr(0) {
		pNew = bp
	}
	if pEList == uintptr(0) {
		pEList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3Expr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_ASTERISK), uintptr(0)))
	}
	(*TSelect)(unsafe.Pointer(pNew)).FpEList = pEList
	(*TSelect)(unsafe.Pointer(pNew)).Fop = uint8(TK_SELECT)
	(*TSelect)(unsafe.Pointer(pNew)).FselFlags = selFlags
	(*TSelect)(unsafe.Pointer(pNew)).FiLimit = 0
	(*TSelect)(unsafe.Pointer(pNew)).FiOffset = 0
	v1 = pParse + 100
	*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
	v2 = *(*int32)(unsafe.Pointer(v1))
	(*TSelect)(unsafe.Pointer(pNew)).FselId = uint32(v2)
	(*TSelect)(unsafe.Pointer(pNew)).FnSelectRow = 0
	if pSrc == uintptr(0) {
		pSrc = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(libc.UintptrFromInt32(0)+8)+libc.Uint32FromInt64(48)))
	}
	(*TSelect)(unsafe.Pointer(pNew)).FpSrc = pSrc
	(*TSelect)(unsafe.Pointer(pNew)).FpWhere = pWhere
	(*TSelect)(unsafe.Pointer(pNew)).FpGroupBy = pGroupBy
	(*TSelect)(unsafe.Pointer(pNew)).FpHaving = pHaving
	(*TSelect)(unsafe.Pointer(pNew)).FpOrderBy = pOrderBy
	(*TSelect)(unsafe.Pointer(pNew)).FpPrior = uintptr(0)
	(*TSelect)(unsafe.Pointer(pNew)).FpNext = uintptr(0)
	(*TSelect)(unsafe.Pointer(pNew)).FpLimit = pLimit
	(*TSelect)(unsafe.Pointer(pNew)).FpWith = uintptr(0)
	(*TSelect)(unsafe.Pointer(pNew)).FpWin = uintptr(0)
	(*TSelect)(unsafe.Pointer(pNew)).FpWinDefn = uintptr(0)
	if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
		_clearSelect(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pNew, libc.BoolInt32(pNew != bp))
		pAllocated = uintptr(0)
	} else {
	}
	return pAllocated
}

// C documentation
//
//	/*
//	** Return true if zName is a shadow table name in the current database
//	** connection.
//	**
//	** zName is temporarily modified while this routine is running, but is
//	** restored to its original value prior to this routine returning.
//	*/
func _sqlite3ShadowTableName(tls *libc.TLS, db uintptr, zName uintptr) (r int32) {
	var pTab, zCopy, zTail, v1 uintptr
	_, _, _, _ = pTab, zCopy, zTail, v1
	zTail = libc.Xstrrchr(tls, zName, int32('_'))
	if zTail == uintptr(0) {
		return 0
	}
	zCopy = _sqlite3DbStrNDup(tls, db, zName, uint64(int32(zTail)-int32(zName)))
	if zCopy != 0 {
		v1 = _sqlite3FindTable(tls, db, zCopy, uintptr(0))
	} else {
		v1 = uintptr(0)
	}
	pTab = v1
	_sqlite3DbFree(tls, db, zCopy)
	if pTab == uintptr(0) {
		return 0
	}
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
		return 0
	}
	return _sqlite3IsShadowTableOf(tls, db, pTab, zName)
}

// C documentation
//
//	/*
//	** Append a new table name to the given SrcList.  Create a new SrcList if
//	** need be.  A new entry is created in the SrcList even if pTable is NULL.
//	**
//	** A SrcList is returned, or NULL if there is an OOM error or if the
//	** SrcList grows to large.  The returned
//	** SrcList might be the same as the SrcList that was input or it might be
//	** a new one.  If an OOM error does occurs, then the prior value of pList
//	** that is input to this routine is automatically freed.
//	**
//	** If pDatabase is not null, it means that the table has an optional
//	** database name prefix.  Like this:  "database.table".  The pDatabase
//	** points to the table name and the pTable points to the database name.
//	** The SrcList.a[].zName field is filled with the table name which might
//	** come from pTable (if pDatabase is NULL) or from pDatabase.
//	** SrcList.a[].zDatabase is filled with the database name from pTable,
//	** or with NULL if no database is specified.
//	**
//	** In other words, if call like this:
//	**
//	**         sqlite3SrcListAppend(D,A,B,0);
//	**
//	** Then B is a table name and the database name is unspecified.  If called
//	** like this:
//	**
//	**         sqlite3SrcListAppend(D,A,B,C);
//	**
//	** Then C is the table name and B is the database name.  If C is defined
//	** then so is B.  In other words, we never have a case where:
//	**
//	**         sqlite3SrcListAppend(D,A,0,C);
//	**
//	** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
//	** before being added to the SrcList.
//	*/
func _sqlite3SrcListAppend(tls *libc.TLS, pParse uintptr, pList uintptr, pTable uintptr, pDatabase uintptr) (r uintptr) {
	var db, pItem, pNew uintptr
	_, _, _ = db, pItem, pNew
	/* Cannot have C without B */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if pList == uintptr(0) {
		pList = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(libc.UintptrFromInt32(0)+8)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(48)))
		if pList == uintptr(0) {
			return uintptr(0)
		}
		(*TSrcList)(unsafe.Pointer(pList)).FnAlloc = uint32(1)
		(*TSrcList)(unsafe.Pointer(pList)).FnSrc = int32(1)
		libc.Xmemset(tls, pList+8, 0, uint32(48))
		(*(*TSrcItem)(unsafe.Pointer(pList + 8))).FiCursor = -int32(1)
	} else {
		pNew = _sqlite3SrcListEnlarge(tls, pParse, pList, int32(1), (*TSrcList)(unsafe.Pointer(pList)).FnSrc)
		if pNew == uintptr(0) {
			_sqlite3SrcListDelete(tls, db, pList)
			return uintptr(0)
		} else {
			pList = pNew
		}
	}
	pItem = pList + 8 + uintptr((*TSrcList)(unsafe.Pointer(pList)).FnSrc-int32(1))*48
	if pDatabase != 0 && (*TToken)(unsafe.Pointer(pDatabase)).Fz == uintptr(0) {
		pDatabase = uintptr(0)
	}
	if pDatabase != 0 {
		(*TSrcItem)(unsafe.Pointer(pItem)).FzName = _sqlite3NameFromToken(tls, db, pDatabase)
		*(*uintptr)(unsafe.Pointer(pItem + 44)) = _sqlite3NameFromToken(tls, db, pTable)
	} else {
		(*TSrcItem)(unsafe.Pointer(pItem)).FzName = _sqlite3NameFromToken(tls, db, pTable)
		*(*uintptr)(unsafe.Pointer(pItem + 44)) = uintptr(0)
	}
	return pList
}

// C documentation
//
//	/*
//	** This routine is called by the parser to add a new term to the
//	** end of a growing FROM clause.  The "p" parameter is the part of
//	** the FROM clause that has already been constructed.  "p" is NULL
//	** if this is the first term of the FROM clause.  pTable and pDatabase
//	** are the name of the table and database named in the FROM clause term.
//	** pDatabase is NULL if the database name qualifier is missing - the
//	** usual case.  If the term has an alias, then pAlias points to the
//	** alias token.  If the term is a subquery, then pSubquery is the
//	** SELECT statement that the subquery encodes.  The pTable and
//	** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
//	** parameters are the content of the ON and USING clauses.
//	**
//	** Return a new SrcList which encodes is the FROM with the new
//	** term added.
//	*/
func _sqlite3SrcListAppendFromTerm(tls *libc.TLS, pParse uintptr, p uintptr, pTable uintptr, pDatabase uintptr, pAlias uintptr, pSubquery uintptr, pOnUsing uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pItem, pToken, v1 uintptr
	_, _, _, _ = db, pItem, pToken, v1
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if !(p != 0) && pOnUsing != uintptr(0) && ((*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpOn != 0 || (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpUsing != 0) {
		if (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpOn != 0 {
			v1 = __ccgo_ts + 17280
		} else {
			v1 = __ccgo_ts + 17283
		}
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17289, libc.VaList(bp+8, v1))
		goto append_from_error
	}
	p = _sqlite3SrcListAppend(tls, pParse, p, pTable, pDatabase)
	if p == uintptr(0) {
		goto append_from_error
	}
	pItem = p + 8 + uintptr((*TSrcList)(unsafe.Pointer(p)).FnSrc-int32(1))*48
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*TSrcItem)(unsafe.Pointer(pItem)).FzName != 0 {
		if pDatabase != 0 && (*TToken)(unsafe.Pointer(pDatabase)).Fz != 0 {
			v1 = pDatabase
		} else {
			v1 = pTable
		}
		pToken = v1
		_sqlite3RenameTokenMap(tls, pParse, (*TSrcItem)(unsafe.Pointer(pItem)).FzName, pToken)
	}
	if (*TToken)(unsafe.Pointer(pAlias)).Fn != 0 {
		(*TSrcItem)(unsafe.Pointer(pItem)).FzAlias = _sqlite3NameFromToken(tls, db, pAlias)
	}
	if pSubquery != 0 {
		if _sqlite3SrcItemAttachSubquery(tls, pParse, pItem, pSubquery, 0) != 0 {
			if (*TSelect)(unsafe.Pointer(pSubquery)).FselFlags&uint32(SF_NestedFrom) != 0 {
				libc.SetBitFieldPtr32Uint32(pItem+12+4, libc.Uint32FromInt32(1), 14, 0x4000)
			}
		}
	}
	if pOnUsing == uintptr(0) {
		*(*uintptr)(unsafe.Pointer(pItem + 40)) = uintptr(0)
	} else {
		if (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpUsing != 0 {
			libc.SetBitFieldPtr32Uint32(pItem+12+4, libc.Uint32FromInt32(1), 11, 0x800)
			*(*uintptr)(unsafe.Pointer(pItem + 40)) = (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpUsing
		} else {
			*(*uintptr)(unsafe.Pointer(pItem + 40)) = (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpOn
		}
	}
	return p
	goto append_from_error
append_from_error:
	;
	_sqlite3ClearOnOrUsing(tls, db, pOnUsing)
	_sqlite3SelectDelete(tls, db, pSubquery)
	return uintptr(0)
}

// C documentation
//
//	/*
//	** Append the contents of SrcList p2 to SrcList p1 and return the resulting
//	** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2
//	** are deleted by this function.
//	*/
func _sqlite3SrcListAppendList(tls *libc.TLS, pParse uintptr, p1 uintptr, p2 uintptr) (r uintptr) {
	var nOld int32
	var pNew, v1 uintptr
	_, _, _ = nOld, pNew, v1
	if p2 != 0 {
		nOld = (*TSrcList)(unsafe.Pointer(p1)).FnSrc
		pNew = _sqlite3SrcListEnlarge(tls, pParse, p1, (*TSrcList)(unsafe.Pointer(p2)).FnSrc, nOld)
		if pNew == uintptr(0) {
			_sqlite3SrcListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p2)
		} else {
			p1 = pNew
			libc.Xmemcpy(tls, p1+8+uintptr(nOld)*48, p2+8, uint32((*TSrcList)(unsafe.Pointer(p2)).FnSrc)*uint32(48))
			v1 = p1 + 8 + 12
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(JT_LTORJ)&int32((*(*TSrcItem)(unsafe.Pointer(p2 + 8))).Ffg.Fjointype))
			_sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p2)
		}
	}
	return p1
}

// C documentation
//
//	/*
//	** If cursors, triggers, views and subqueries are all omitted from
//	** the build, then none of the following routines, except for
//	** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
//	** called with a NULL argument.
//	*/
func _sqlite3SrcListDup(tls *libc.TLS, db uintptr, p uintptr, flags int32) (r uintptr) {
	var i int32
	var pNew, pNewItem, pNewSubq, pOldItem, pTab, v3 uintptr
	var v1 Tu32
	_, _, _, _, _, _, _, _ = i, pNew, pNewItem, pNewSubq, pOldItem, pTab, v1, v3
	if p == uintptr(0) {
		return uintptr(0)
	}
	pNew = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(libc.UintptrFromInt32(0)+8)+uint32((*TSrcList)(unsafe.Pointer(p)).FnSrc)*libc.Uint32FromInt64(48)))
	if pNew == uintptr(0) {
		return uintptr(0)
	}
	v1 = uint32((*TSrcList)(unsafe.Pointer(p)).FnSrc)
	(*TSrcList)(unsafe.Pointer(pNew)).FnAlloc = v1
	(*TSrcList)(unsafe.Pointer(pNew)).FnSrc = int32(v1)
	i = 0
	for {
		if !(i < (*TSrcList)(unsafe.Pointer(p)).FnSrc) {
			break
		}
		pNewItem = pNew + 8 + uintptr(i)*48
		pOldItem = p + 8 + uintptr(i)*48
		(*TSrcItem)(unsafe.Pointer(pNewItem)).Ffg = (*TSrcItem)(unsafe.Pointer(pOldItem)).Ffg
		if int32(*(*uint32)(unsafe.Pointer(pOldItem + 12 + 4))&0x4>>2) != 0 {
			pNewSubq = _sqlite3DbMallocRaw(tls, db, uint64(16))
			if pNewSubq == uintptr(0) {
				libc.SetBitFieldPtr32Uint32(pNewItem+12+4, libc.Uint32FromInt32(0), 2, 0x4)
			} else {
				libc.Xmemcpy(tls, pNewSubq, *(*uintptr)(unsafe.Pointer(pOldItem + 44)), uint32(16))
				(*TSubquery)(unsafe.Pointer(pNewSubq)).FpSelect = _sqlite3SelectDup(tls, db, (*TSubquery)(unsafe.Pointer(pNewSubq)).FpSelect, flags)
				if (*TSubquery)(unsafe.Pointer(pNewSubq)).FpSelect == uintptr(0) {
					_sqlite3DbFree(tls, db, pNewSubq)
					pNewSubq = uintptr(0)
					libc.SetBitFieldPtr32Uint32(pNewItem+12+4, libc.Uint32FromInt32(0), 2, 0x4)
				}
			}
			*(*uintptr)(unsafe.Pointer(pNewItem + 44)) = pNewSubq
		} else {
			if int32(*(*uint32)(unsafe.Pointer(pOldItem + 12 + 4))&0x10000>>16) != 0 {
				*(*uintptr)(unsafe.Pointer(pNewItem + 44)) = *(*uintptr)(unsafe.Pointer(pOldItem + 44))
			} else {
				*(*uintptr)(unsafe.Pointer(pNewItem + 44)) = _sqlite3DbStrDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 44)))
			}
		}
		(*TSrcItem)(unsafe.Pointer(pNewItem)).FzName = _sqlite3DbStrDup(tls, db, (*TSrcItem)(unsafe.Pointer(pOldItem)).FzName)
		(*TSrcItem)(unsafe.Pointer(pNewItem)).FzAlias = _sqlite3DbStrDup(tls, db, (*TSrcItem)(unsafe.Pointer(pOldItem)).FzAlias)
		(*TSrcItem)(unsafe.Pointer(pNewItem)).FiCursor = (*TSrcItem)(unsafe.Pointer(pOldItem)).FiCursor
		if int32(*(*uint32)(unsafe.Pointer(pNewItem + 12 + 4))&0x2>>1) != 0 {
			*(*uintptr)(unsafe.Pointer(pNewItem + 32)) = _sqlite3DbStrDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 32)))
		} else {
			if int32(*(*uint32)(unsafe.Pointer(pNewItem + 12 + 4))&0x8>>3) != 0 {
				*(*uintptr)(unsafe.Pointer(pNewItem + 32)) = _sqlite3ExprListDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 32)), flags)
			} else {
				*(*Tu32)(unsafe.Pointer(pNewItem + 32)) = *(*Tu32)(unsafe.Pointer(pOldItem + 32))
			}
		}
		(*TSrcItem)(unsafe.Pointer(pNewItem)).Fu2 = (*TSrcItem)(unsafe.Pointer(pOldItem)).Fu2
		if int32(*(*uint32)(unsafe.Pointer(pNewItem + 12 + 4))&0x200>>9) != 0 {
			(*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNewItem + 36)))).FnUse = (*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNewItem + 36)))).FnUse + 1
		}
		v3 = (*TSrcItem)(unsafe.Pointer(pOldItem)).FpSTab
		(*TSrcItem)(unsafe.Pointer(pNewItem)).FpSTab = v3
		pTab = v3
		if pTab != 0 {
			(*TTable)(unsafe.Pointer(pTab)).FnTabRef = (*TTable)(unsafe.Pointer(pTab)).FnTabRef + 1
		}
		if int32(*(*uint32)(unsafe.Pointer(pOldItem + 12 + 4))&0x800>>11) != 0 {
			*(*uintptr)(unsafe.Pointer(pNewItem + 40)) = _sqlite3IdListDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 40)))
		} else {
			*(*uintptr)(unsafe.Pointer(pNewItem + 40)) = _sqlite3ExprDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 40)), flags)
		}
		(*TSrcItem)(unsafe.Pointer(pNewItem)).FcolUsed = (*TSrcItem)(unsafe.Pointer(pOldItem)).FcolUsed
		goto _2
	_2:
		;
		i = i + 1
	}
	return pNew
}

// C documentation
//
//	/*
//	** Expand the space allocated for the given SrcList object by
//	** creating nExtra new slots beginning at iStart.  iStart is zero based.
//	** New slots are zeroed.
//	**
//	** For example, suppose a SrcList initially contains two entries: A,B.
//	** To append 3 new entries onto the end, do this:
//	**
//	**    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
//	**
//	** After the call above it would contain:  A, B, nil, nil, nil.
//	** If the iStart argument had been 1 instead of 2, then the result
//	** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
//	** the iStart value would be 0.  The result then would
//	** be: nil, nil, nil, A, B.
//	**
//	** If a memory allocation fails or the SrcList becomes too large, leave
//	** the original SrcList unchanged, return NULL, and leave an error message
//	** in pParse.
//	*/
func _sqlite3SrcListEnlarge(tls *libc.TLS, pParse uintptr, pSrc uintptr, nExtra int32, iStart int32) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pNew uintptr
	var i int32
	var nAlloc Tsqlite3_int64
	_, _, _, _ = db, i, nAlloc, pNew
	/* Sanity checking on calling parameters */
	/* Allocate additional space if needed */
	if uint32((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc)+uint32(nExtra) > (*TSrcList)(unsafe.Pointer(pSrc)).FnAlloc {
		nAlloc = int64(2)*int64((*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) + int64(nExtra)
		db = (*TParse)(unsafe.Pointer(pParse)).Fdb
		if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc+nExtra >= int32(SQLITE_MAX_SRCLIST) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17244, libc.VaList(bp+8, int32(SQLITE_MAX_SRCLIST)))
			return uintptr(0)
		}
		if nAlloc > int64(SQLITE_MAX_SRCLIST) {
			nAlloc = int64(SQLITE_MAX_SRCLIST)
		}
		pNew = _sqlite3DbRealloc(tls, db, pSrc, uint64(int64(uint32(libc.UintptrFromInt32(0)+8))+nAlloc*libc.Int64FromInt64(48)))
		if pNew == uintptr(0) {
			return uintptr(0)
		}
		pSrc = pNew
		(*TSrcList)(unsafe.Pointer(pSrc)).FnAlloc = uint32(nAlloc)
	}
	/* Move existing slots that come after the newly inserted slots
	 ** out of the way */
	i = (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc - int32(1)
	for {
		if !(i >= iStart) {
			break
		}
		*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i+nExtra)*48)) = *(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48))
		goto _1
	_1:
		;
		i = i - 1
	}
	**(**int32)(__ccgo_up(pSrc)) += nExtra
	/* Zero the newly allocated slots */
	libc.Xmemset(tls, pSrc+8+uintptr(iStart)*48, 0, uint32(48)*uint32(nExtra))
	i = iStart
	for {
		if !(i < iStart+nExtra) {
			break
		}
		(*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48))).FiCursor = -int32(1)
		goto _2
	_2:
		;
		i = i + 1
	}
	/* Return a pointer to the enlarged SrcList */
	return pSrc
}

// C documentation
//
//	/*
//	** When building up a FROM clause in the parser, the join operator
//	** is initially attached to the left operand.  But the code generator
//	** expects the join operator to be on the right operand.  This routine
//	** Shifts all join operators from left to right for an entire FROM
//	** clause.
//	**
//	** Example: Suppose the join is like this:
//	**
//	**           A natural cross join B
//	**
//	** The operator is "natural cross join".  The A and B operands are stored
//	** in p->a[0] and p->a[1], respectively.  The parser initially stores the
//	** operator with A.  This routine shifts that operator over to B.
//	**
//	** Additional changes:
//	**
//	**   *   All tables to the left of the right-most RIGHT JOIN are tagged with
//	**       JT_LTORJ (mnemonic: Left Table Of Right Join) so that the
//	**       code generator can easily tell that the table is part of
//	**       the left operand of at least one RIGHT JOIN.
//	*/
func _sqlite3SrcListShiftJoinType(tls *libc.TLS, pParse uintptr, p uintptr) {
	var allFlags, v3 Tu8
	var i, v1 int32
	var v7 uintptr
	_, _, _, _, _ = allFlags, i, v1, v3, v7
	_ = pParse
	if p != 0 && (*TSrcList)(unsafe.Pointer(p)).FnSrc > int32(1) {
		i = (*TSrcList)(unsafe.Pointer(p)).FnSrc - int32(1)
		allFlags = uint8(0)
		for {
			v3 = (*(*TSrcItem)(unsafe.Pointer(p + 8 + uintptr(i-int32(1))*48))).Ffg.Fjointype
			(*(*TSrcItem)(unsafe.Pointer(p + 8 + uintptr(i)*48))).Ffg.Fjointype = v3
			allFlags = uint8(int32(allFlags) | int32(v3))
			goto _2
		_2:
			;
			i = i - 1
			v1 = i
			if !(v1 > 0) {
				break
			}
		}
		(*(*TSrcItem)(unsafe.Pointer(p + 8))).Ffg.Fjointype = uint8(0)
		/* All terms to the left of a RIGHT JOIN should be tagged with the
		 ** JT_LTORJ flags */
		if int32(allFlags)&int32(JT_RIGHT) != 0 {
			i = (*TSrcList)(unsafe.Pointer(p)).FnSrc - int32(1)
			for {
				if !(i > 0 && int32((*(*TSrcItem)(unsafe.Pointer(p + 8 + uintptr(i)*48))).Ffg.Fjointype)&int32(JT_RIGHT) == 0) {
					break
				}
				goto _4
			_4:
				;
				i = i - 1
			}
			i = i - 1
			for {
				v7 = p + 8 + uintptr(i)*48 + 12
				*(*Tu8)(unsafe.Pointer(v7)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v7))) | libc.Int32FromInt32(JT_LTORJ))
				goto _6
			_6:
				;
				i = i - 1
				v1 = i
				if !(v1 >= 0) {
					break
				}
			}
		}
	}
}

// C documentation
//
//	/*
//	** Begin constructing a new table representation in memory.  This is
//	** the first of several action routines that get called in response
//	** to a CREATE TABLE statement.  In particular, this routine is called
//	** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
//	** flag is true if the table should be stored in the auxiliary database
//	** file instead of in the main database file.  This is normally the case
//	** when the "TEMP" or "TEMPORARY" keyword occurs in between
//	** CREATE and TABLE.
//	**
//	** The new table record is initialized and put in pParse->pNewTable.
//	** As more of the CREATE TABLE statement is parsed, additional action
//	** routines will be called to add more information to this record.
//	** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
//	** is called to complete the construction of the new table record.
//	*/
func _sqlite3StartTable(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr, isTemp int32, isView int32, isVirtual int32, noErr int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var addr1, fileFormat, iDb, reg1, reg2, reg3, v7, v8 int32
	var db, pTable, v, zDb, zDb1, zName, v1 uintptr
	var v6 bool
	var _ /* pName at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, db, fileFormat, iDb, pTable, reg1, reg2, reg3, v, zDb, zDb1, zName, v1, v6, v7, v8
	zName = uintptr(0)                         /* The name of the new table */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Unqualified name of the table to create */
	if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FnewTnum == uint32(1) {
		/* Special case:  Parsing the sqlite_schema or sqlite_temp_schema schema */
		iDb = int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb)
		if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) {
			v1 = __ccgo_ts + 7981
		} else {
			v1 = __ccgo_ts + 7501
		}
		zName = _sqlite3DbStrDup(tls, db, v1)
		**(**uintptr)(__ccgo_up(bp)) = pName1
	} else {
		/* The common case */
		iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp)
		if iDb < 0 {
			return
		}
		if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && isTemp != 0 && (*TToken)(unsafe.Pointer(pName2)).Fn > uint32(0) && iDb != int32(1) {
			/* If creating a temp table, the name may not be qualified. Unless
			 ** the database name is "temp" anyway.  */
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15022, 0)
			return
		}
		if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && isTemp != 0 {
			iDb = int32(1)
		}
		zName = _sqlite3NameFromToken(tls, db, **(**uintptr)(__ccgo_up(bp)))
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
			_sqlite3RenameTokenMap(tls, pParse, zName, **(**uintptr)(__ccgo_up(bp)))
		}
	}
	(*TParse)(unsafe.Pointer(pParse)).FsNameToken = **(**TToken)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))
	if zName == uintptr(0) {
		return
	}
	if isView != 0 {
		v1 = __ccgo_ts + 12332
	} else {
		v1 = __ccgo_ts + 10594
	}
	if _sqlite3CheckObjectName(tls, pParse, zName, v1, zName) != 0 {
		goto begin_table_error
	}
	if int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb) == int32(1) {
		isTemp = int32(1)
	}
	zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && isTemp == int32(1) {
		v1 = __ccgo_ts + 7981
	} else {
		v1 = __ccgo_ts + 7501
	}
	if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_INSERT), v1, uintptr(0), zDb) != 0 {
		goto begin_table_error
	}
	if !(isVirtual != 0) && _sqlite3AuthCheck(tls, pParse, int32(_aCode[isTemp+int32(2)*isView]), zName, uintptr(0), zDb) != 0 {
		goto begin_table_error
	}
	/* Make sure the new table name does not collide with an existing
	 ** index or table name in the same database.  Issue an error message if
	 ** it does. The exception is if the statement being parsed was passed
	 ** to an sqlite3_declare_vtab() call. In that case only the column names
	 ** and types will be used, so there is no need to test for namespace
	 ** collisions.
	 */
	if !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != libc.Int32FromInt32(PARSE_MODE_NORMAL)) {
		zDb1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
		if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) {
			goto begin_table_error
		}
		pTable = _sqlite3FindTable(tls, db, zName, zDb1)
		if pTable != 0 {
			if !(noErr != 0) {
				if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == int32(TABTYP_VIEW) {
					v1 = __ccgo_ts + 12332
				} else {
					v1 = __ccgo_ts + 10594
				}
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15063, libc.VaList(bp+16, v1, **(**uintptr)(__ccgo_up(bp))))
			} else {
				_sqlite3CodeVerifySchema(tls, pParse, iDb)
				_sqlite3ForceNotReadOnly(tls, pParse)
			}
			goto begin_table_error
		}
		if _sqlite3FindIndex(tls, db, zName, zDb1) != uintptr(0) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15084, libc.VaList(bp+16, zName))
			goto begin_table_error
		}
	}
	pTable = _sqlite3DbMallocZero(tls, db, uint64(80))
	if pTable == uintptr(0) {
		(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM)
		(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
		goto begin_table_error
	}
	(*TTable)(unsafe.Pointer(pTable)).FzName = zName
	(*TTable)(unsafe.Pointer(pTable)).FiPKey = int16(-int32(1))
	(*TTable)(unsafe.Pointer(pTable)).FpSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpSchema
	(*TTable)(unsafe.Pointer(pTable)).FnTabRef = uint32(1)
	(*TTable)(unsafe.Pointer(pTable)).FnRowLogEst = int16(200)
	(*TParse)(unsafe.Pointer(pParse)).FpNewTable = pTable
	/* Begin generating the code that will insert the table record into
	 ** the schema table.  Note in particular that we must go ahead
	 ** and allocate the record number for the table entry now.  Before any
	 ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
	 ** indices to be created and the table record must come before the
	 ** indices.  Hence, the record number for the table must be allocated
	 ** now.
	 */
	if v6 = !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0); v6 {
		v1 = _sqlite3GetVdbe(tls, pParse)
		v = v1
	}
	if v6 && v1 != uintptr(0) {
		_sqlite3BeginWriteOperation(tls, pParse, int32(1), iDb)
		if isVirtual != 0 {
			_sqlite3VdbeAddOp0(tls, v, int32(OP_VBegin))
		}
		/* If the file format and encoding in the database have not been set,
		 ** set them now.
		 */
		v1 = pParse + 48
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		v8 = *(*int32)(unsafe.Pointer(v1))
		v7 = v8
		(*(*struct {
			FaddrCrTab      int32
			FregRowid       int32
			FregRoot        int32
			FconstraintName TToken
		})(unsafe.Pointer(pParse + 188))).FregRowid = v7
		reg1 = v7
		v1 = pParse + 48
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		v8 = *(*int32)(unsafe.Pointer(v1))
		v7 = v8
		(*(*struct {
			FaddrCrTab      int32
			FregRowid       int32
			FregRoot        int32
			FconstraintName TToken
		})(unsafe.Pointer(pParse + 188))).FregRoot = v7
		reg2 = v7
		v1 = pParse + 48
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		v7 = *(*int32)(unsafe.Pointer(v1))
		reg3 = v7
		_sqlite3VdbeAddOp3(tls, v, int32(OP_ReadCookie), iDb, reg3, int32(BTREE_FILE_FORMAT))
		_sqlite3VdbeUsesBtree(tls, v, iDb)
		addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_If), reg3)
		if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_LegacyFileFmt) != uint64(0) {
			v7 = int32(1)
		} else {
			v7 = int32(SQLITE_MAX_FILE_FORMAT)
		}
		fileFormat = v7
		_sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_FILE_FORMAT), fileFormat)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_SetCookie), iDb, int32(BTREE_TEXT_ENCODING), int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc))
		_sqlite3VdbeJumpHere(tls, v, addr1)
		/* This just creates a place-holder record in the sqlite_schema table.
		 ** The record created does not contain anything yet.  It will be replaced
		 ** by the real entry in code generated at sqlite3EndTable().
		 **
		 ** The rowid for the new entry is left in register pParse->u1.cr.regRowid.
		 ** The root page of the new table is left in reg pParse->u1.cr.regRoot.
		 ** The rowid and root page number values are needed by the code that
		 ** sqlite3EndTable will generate.
		 */
		if isView != 0 || isVirtual != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, reg2)
		} else {
			(*(*struct {
				FaddrCrTab      int32
				FregRowid       int32
				FregRoot        int32
				FconstraintName TToken
			})(unsafe.Pointer(pParse + 188))).FaddrCrTab = _sqlite3VdbeAddOp3(tls, v, int32(OP_CreateBtree), iDb, reg2, int32(BTREE_INTKEY))
		}
		_sqlite3OpenSchemaTable(tls, pParse, iDb)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), 0, reg1)
		_sqlite3VdbeAddOp4(tls, v, int32(OP_Blob), int32(6), reg3, 0, uintptr(unsafe.Pointer(&_nullRow)), -int32(1))
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), 0, reg3, reg1)
		_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_APPEND))
		_sqlite3VdbeAddOp0(tls, v, int32(OP_Close))
	} else {
		if int32(uint32(*(*uint8)(unsafe.Pointer(db + 176 + 8))&0x6>>1)) != 0 {
			**(**Tu32)(__ccgo_up(pTable + 28)) |= uint32(TF_Imposter)
			if int32(uint32(*(*uint8)(unsafe.Pointer(db + 176 + 8))&0x6>>1)) >= int32(2) {
				**(**Tu32)(__ccgo_up(pTable + 28)) |= uint32(TF_Readonly)
			}
		}
	}
	/* Normal (non-error) return. */
	return
	/* If an error occurs, we jump here */
	goto begin_table_error
begin_table_error:
	;
	libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 8, 0x100)
	_sqlite3DbFree(tls, db, zName)
	return
}

// C documentation
//
//	/*
//	** Unless it is NULL, the argument must be an UnpackedRecord object returned
//	** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
//	** the object.
//	*/
func _sqlite3Stat4ProbeFree(tls *libc.TLS, pRec uintptr) {
	var aMem, db uintptr
	var i, nCol int32
	_, _, _, _ = aMem, db, i, nCol
	if pRec != 0 {
		nCol = int32((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(pRec)).FpKeyInfo)).FnAllField)
		aMem = (*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem
		db = (**(**TMem)(__ccgo_up(aMem))).Fdb
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			_sqlite3VdbeMemRelease(tls, aMem+uintptr(i)*40)
			goto _1
		_1:
			;
			i = i + 1
		}
		_sqlite3KeyInfoUnref(tls, (*TUnpackedRecord)(unsafe.Pointer(pRec)).FpKeyInfo)
		_sqlite3DbFreeNN(tls, db, pRec)
	}
}

// C documentation
//
//	/*
//	** This function is used to allocate and populate UnpackedRecord
//	** structures intended to be compared against sample index keys stored
//	** in the sqlite_stat4 table.
//	**
//	** A single call to this function populates zero or more fields of the
//	** record starting with field iVal (fields are numbered from left to
//	** right starting with 0). A single field is populated if:
//	**
//	**  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
//	**
//	**  * The expression is a bound variable, and this is a reprepare, or
//	**
//	**  * The sqlite3ValueFromExpr() function is able to extract a value
//	**    from the expression (i.e. the expression is a literal value).
//	**
//	** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
//	** vector components that match either of the two latter criteria listed
//	** above.
//	**
//	** Before any value is appended to the record, the affinity of the
//	** corresponding column within index pIdx is applied to it. Before
//	** this function returns, output parameter *pnExtract is set to the
//	** number of values appended to the record.
//	**
//	** When this function is called, *ppRec must either point to an object
//	** allocated by an earlier call to this function, or must be NULL. If it
//	** is NULL and a value can be successfully extracted, a new UnpackedRecord
//	** is allocated (and *ppRec set to point to it) before returning.
//	**
//	** Unless an error is encountered, SQLITE_OK is returned. It is not an
//	** error if a value cannot be extracted from pExpr. If an error does
//	** occur, an SQLite error code is returned.
//	*/
func _sqlite3Stat4ProbeSetValue(tls *libc.TLS, pParse uintptr, pIdx uintptr, ppRec uintptr, pExpr uintptr, nElem int32, iVal int32, pnExtract uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aff Tu8
	var i, nExtract, rc int32
	var pElem, v2 uintptr
	var _ /* alloc at bp+0 */ TValueNewStat4Ctx
	var _ /* pVal at bp+16 */ uintptr
	_, _, _, _, _, _ = aff, i, nExtract, pElem, rc, v2
	rc = SQLITE_OK
	nExtract = 0
	if pExpr == uintptr(0) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_SELECT) {
		(**(**TValueNewStat4Ctx)(__ccgo_up(bp))).FpParse = pParse
		(**(**TValueNewStat4Ctx)(__ccgo_up(bp))).FpIdx = pIdx
		(**(**TValueNewStat4Ctx)(__ccgo_up(bp))).FppRec = ppRec
		i = 0
		for {
			if !(i < nElem) {
				break
			}
			**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)
			if pExpr != 0 {
				v2 = _sqlite3VectorFieldSubexpr(tls, pExpr, i)
			} else {
				v2 = uintptr(0)
			}
			pElem = v2
			aff = uint8(_sqlite3IndexColumnAffinity(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pIdx, iVal+i))
			(**(**TValueNewStat4Ctx)(__ccgo_up(bp))).FiVal = iVal + i
			rc = _stat4ValueFromExpr(tls, pParse, pElem, aff, bp, bp+16)
			if !(**(**uintptr)(__ccgo_up(bp + 16)) != 0) {
				break
			}
			nExtract = nExtract + 1
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	**(**int32)(__ccgo_up(pnExtract)) = nExtract
	return rc
}

func _sqlite3StatusDown(tls *libc.TLS, op int32, N int32) {
	**(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)) -= uint32(N)
}

// C documentation
//
//	/*
//	** Adjust the highwater mark if necessary.
//	** The caller must hold the appropriate mutex.
//	*/
func _sqlite3StatusHighwater(tls *libc.TLS, op int32, X int32) {
	var newValue Tsqlite3StatValueType
	_ = newValue
	newValue = uint32(X)
	if newValue > **(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) {
		**(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) = newValue
	}
}

// C documentation
//
//	/*
//	** Add N to the value of a status record.  The caller must hold the
//	** appropriate mutex.  (Locking is checked by assert()).
//	**
//	** The StatusUp() routine can accept positive or negative values for N.
//	** The value of N is added to the current status value and the high-water
//	** mark is adjusted if necessary.
//	**
//	** The StatusDown() routine lowers the current value by N.  The highwater
//	** mark is unchanged.  N must be non-negative for StatusDown().
//	*/
func _sqlite3StatusUp(tls *libc.TLS, op int32, N int32) {
	**(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)) += uint32(N)
	if **(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)) > **(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) {
		**(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 40 + uintptr(op)*4)) = **(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4))
	}
}

// C documentation
//
//	/*
//	** Return the current value of a status parameter.  The caller must
//	** be holding the appropriate mutex.
//	*/
func _sqlite3StatusValue(tls *libc.TLS, op int32) (r Tsqlite3_int64) {
	return int64(**(**Tsqlite3StatValueType)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*4)))
}

// C documentation
//
//	/*
//	** Execute the statement pStmt, either until a row of data is ready, the
//	** statement is completely executed or an error occurs.
//	**
//	** This routine implements the bulk of the logic behind the sqlite_step()
//	** API.  The only thing omitted is the automatic recompile if a
//	** schema change has occurred.  That detail is handled by the
//	** outer sqlite3_step() wrapper procedure.
//	*/
func _sqlite3Step(tls *libc.TLS, p uintptr) (r int32) {
	var db uintptr
	var rc int32
	_, _ = db, rc
	db = (*TVdbe)(unsafe.Pointer(p)).Fdb
	if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) != int32(VDBE_RUN_STATE) {
		goto restart_step
	restart_step:
		;
		if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) == int32(VDBE_READY_STATE) {
			if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x3>>0)) != 0 {
				(*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_SCHEMA)
				rc = int32(SQLITE_ERROR)
				if int32((*TVdbe)(unsafe.Pointer(p)).FprepFlags)&int32(SQLITE_PREPARE_SAVESQL) != 0 {
					/* If this statement was prepared using saved SQL and an
					 ** error has occurred, then return the error code in p->rc to the
					 ** caller. Set the error code in the database handle to the same
					 ** value.
					 */
					rc = _sqlite3VdbeTransferError(tls, p)
				}
				goto end_of_step
			}
			/* If there are no other statements currently running, then
			 ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
			 ** from interrupting a statement that has not yet started.
			 */
			if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive == 0 {
				libc.AtomicStoreNInt32(db+312, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED))
			}
			if int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&(libc.Int32FromInt32(SQLITE_TRACE_PROFILE)|libc.Int32FromInt32(SQLITE_TRACE_XPROFILE)) != 0 && !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) && (*TVdbe)(unsafe.Pointer(p)).FzSql != 0 {
				_sqlite3OsCurrentTimeInt64(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, p+136)
			} else {
			}
			(*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive = (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive + 1
			if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) == 0 {
				(*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite = (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite + 1
			}
			if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x80>>7)) != 0 {
				(*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead = (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead + 1
			}
			(*TVdbe)(unsafe.Pointer(p)).Fpc = 0
			(*TVdbe)(unsafe.Pointer(p)).FeVdbeState = uint8(VDBE_RUN_STATE)
		} else {
			if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) == int32(VDBE_HALT_STATE) {
				/* We used to require that sqlite3_reset() be called before retrying
				 ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
				 ** with version 3.7.0, we changed this so that sqlite3_reset() would
				 ** be called automatically instead of throwing the SQLITE_MISUSE error.
				 ** This "automatic-reset" change is not technically an incompatibility,
				 ** since any application that receives an SQLITE_MISUSE is broken by
				 ** definition.
				 **
				 ** Nevertheless, some published applications that were originally written
				 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
				 ** returns, and those were broken by the automatic-reset change.  As a
				 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
				 ** legacy behavior of returning SQLITE_MISUSE for cases where the
				 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
				 ** or SQLITE_BUSY error.
				 */
				Xsqlite3_reset(tls, p)
				goto restart_step
			}
		}
	}
	if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) != 0 {
		rc = _sqlite3VdbeList(tls, p)
	} else {
		(*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec = (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec + 1
		rc = _sqlite3VdbeExec(tls, p)
		(*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec = (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec - 1
	}
	if rc == int32(SQLITE_ROW) {
		(*Tsqlite3)(unsafe.Pointer(db)).FerrCode = int32(SQLITE_ROW)
		return int32(SQLITE_ROW)
	} else {
		/* If the statement completed successfully, invoke the profile callback */
		if (*TVdbe)(unsafe.Pointer(p)).FstartTime > 0 {
			_invokeProfileCallback(tls, db, p)
		}
		(*TVdbe)(unsafe.Pointer(p)).FpResultRow = uintptr(0)
		if rc == int32(SQLITE_DONE) && (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0 {
			(*TVdbe)(unsafe.Pointer(p)).Frc = _doWalCallbacks(tls, db)
			if (*TVdbe)(unsafe.Pointer(p)).Frc != SQLITE_OK {
				rc = int32(SQLITE_ERROR)
			}
		} else {
			if rc != int32(SQLITE_DONE) && int32((*TVdbe)(unsafe.Pointer(p)).FprepFlags)&int32(SQLITE_PREPARE_SAVESQL) != 0 {
				/* If this statement was prepared using saved SQL and an
				 ** error has occurred, then return the error code in p->rc to the
				 ** caller. Set the error code in the database handle to the same value.
				 */
				rc = _sqlite3VdbeTransferError(tls, p)
			}
		}
	}
	(*Tsqlite3)(unsafe.Pointer(db)).FerrCode = rc
	if int32(SQLITE_NOMEM) == _sqlite3ApiExit(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, (*TVdbe)(unsafe.Pointer(p)).Frc) {
		(*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
		if int32((*TVdbe)(unsafe.Pointer(p)).FprepFlags)&int32(SQLITE_PREPARE_SAVESQL) != 0 {
			rc = (*TVdbe)(unsafe.Pointer(p)).Frc
		}
	}
	goto end_of_step
end_of_step:
	;
	/* There are only a limited number of result codes allowed from the
	 ** statements prepared using the legacy sqlite3_prepare() interface */
	return rc & (*Tsqlite3)(unsafe.Pointer(db)).FerrMask
}

// C documentation
//
//	/* Convert a storage column number into a table column number.
//	**
//	** The storage column number (0,1,2,....) is the index of the value
//	** as it appears in the record on disk.  The true column number
//	** is the index (0,1,2,...) of the column in the CREATE TABLE statement.
//	**
//	** The storage column number is less than the table column number if
//	** and only there are VIRTUAL columns to the left.
//	**
//	** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
//	*/
func _sqlite3StorageColumnToTable(tls *libc.TLS, pTab uintptr, iCol Ti16) (r Ti16) {
	var i int32
	_ = i
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasVirtual) != 0 {
		i = 0
		for {
			if !(i <= int32(iCol)) {
				break
			}
			if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
				iCol = iCol + 1
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	return iCol
}

// C documentation
//
//	/*
//	** Enlarge the memory allocation on a StrAccum object so that it is
//	** able to accept at least N more bytes of text.
//	**
//	** Return the number of bytes of text that StrAccum is able to accept
//	** after the attempted enlargement.  The value returned might be zero.
//	*/
func _sqlite3StrAccumEnlarge(tls *libc.TLS, p uintptr, N Ti64) (r int32) {
	var szNew Ti64
	var zNew, zOld, v1 uintptr
	_, _, _, _ = szNew, zNew, zOld, v1
	/* Only called if really needed */
	if (*TStrAccum)(unsafe.Pointer(p)).FaccError != 0 {
		return 0
	}
	if (*TStrAccum)(unsafe.Pointer(p)).FmxAlloc == uint32(0) {
		_sqlite3StrAccumSetError(tls, p, uint8(SQLITE_TOOBIG))
		return int32((*TStrAccum)(unsafe.Pointer(p)).FnAlloc - (*TStrAccum)(unsafe.Pointer(p)).FnChar - uint32(1))
	} else {
		if int32((*TStrAccum)(unsafe.Pointer(p)).FprintfFlags)&int32(SQLITE_PRINTF_MALLOCED) != 0 {
			v1 = (*TStrAccum)(unsafe.Pointer(p)).FzText
		} else {
			v1 = uintptr(0)
		}
		zOld = v1
		szNew = int64((*TStrAccum)(unsafe.Pointer(p)).FnChar) + N + int64(1)
		if szNew+int64((*TStrAccum)(unsafe.Pointer(p)).FnChar) <= int64((*TStrAccum)(unsafe.Pointer(p)).FmxAlloc) {
			/* Force exponential buffer size growth as long as it does not overflow,
			 ** to avoid having to call this routine too often */
			szNew = szNew + int64((*TStrAccum)(unsafe.Pointer(p)).FnChar)
		}
		if szNew > int64((*TStrAccum)(unsafe.Pointer(p)).FmxAlloc) {
			Xsqlite3_str_reset(tls, p)
			_sqlite3StrAccumSetError(tls, p, uint8(SQLITE_TOOBIG))
			return 0
		} else {
			(*TStrAccum)(unsafe.Pointer(p)).FnAlloc = uint32(int32(szNew))
		}
		if (*TStrAccum)(unsafe.Pointer(p)).Fdb != 0 {
			zNew = _sqlite3DbRealloc(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, zOld, uint64((*TStrAccum)(unsafe.Pointer(p)).FnAlloc))
		} else {
			zNew = _sqlite3Realloc(tls, zOld, uint64((*TStrAccum)(unsafe.Pointer(p)).FnAlloc))
		}
		if zNew != 0 {
			if !(int32((*TStrAccum)(unsafe.Pointer(p)).FprintfFlags)&libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED) != libc.Int32FromInt32(0)) && (*TStrAccum)(unsafe.Pointer(p)).FnChar > uint32(0) {
				libc.Xmemcpy(tls, zNew, (*TStrAccum)(unsafe.Pointer(p)).FzText, (*TStrAccum)(unsafe.Pointer(p)).FnChar)
			}
			(*TStrAccum)(unsafe.Pointer(p)).FzText = zNew
			(*TStrAccum)(unsafe.Pointer(p)).FnAlloc = uint32(_sqlite3DbMallocSize(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, zNew))
			v1 = p + 21
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED))
		} else {
			Xsqlite3_str_reset(tls, p)
			_sqlite3StrAccumSetError(tls, p, uint8(SQLITE_NOMEM))
			return 0
		}
	}
	return int32(N)
}

// C documentation
//
//	/*
//	** pTab is a transient Table object that represents a subquery of some
//	** kind (maybe a parenthesized subquery in the FROM clause of a larger
//	** query, or a VIEW, or a CTE).  This routine computes type information
//	** for that Table object based on the Select object that implements the
//	** subquery.  For the purposes of this routine, "type information" means:
//	**
//	**    *   The datatype name, as it might appear in a CREATE TABLE statement
//	**    *   Which collating sequence to use for the column
//	**    *   The affinity of the column
//	*/
func _sqlite3SubqueryColumnTypes(tls *libc.TLS, pParse uintptr, pTab uintptr, pSelect uintptr, aff int8) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var a, db, p, pCol, pColl, pS2, zType, v4 uintptr
	var i, j, m int32
	var k, n Ti64
	var _ /* sNC at bp+0 */ TNameContext
	_, _, _, _, _, _, _, _, _, _, _, _, _ = a, db, i, j, k, m, n, p, pCol, pColl, pS2, zType, v4
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		return
	}
	for (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 {
		pSelect = (*TSelect)(unsafe.Pointer(pSelect)).FpPrior
	}
	a = (*TSelect)(unsafe.Pointer(pSelect)).FpEList + 8
	libc.Xmemset(tls, bp, 0, uint32(36))
	(**(**TNameContext)(__ccgo_up(bp))).FpSrcList = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc
	i = 0
	pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
			break
		}
		m = 0
		pS2 = pSelect
		**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags) & libc.Int32FromInt32(COLFLAG_NOINSERT))
		p = (**(**TExprList_item)(__ccgo_up(a + uintptr(i)*20))).FpExpr
		/* pCol->szEst = ... // Column size est for SELECT tables never used */
		(*TColumn)(unsafe.Pointer(pCol)).Faffinity = _sqlite3ExprAffinity(tls, p)
		for int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) <= int32(SQLITE_AFF_NONE) && (*TSelect)(unsafe.Pointer(pS2)).FpNext != uintptr(0) {
			m = m | _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS2)).FpEList + 8 + uintptr(i)*20))).FpExpr)
			pS2 = (*TSelect)(unsafe.Pointer(pS2)).FpNext
			(*TColumn)(unsafe.Pointer(pCol)).Faffinity = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS2)).FpEList + 8 + uintptr(i)*20))).FpExpr)
		}
		if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) <= int32(SQLITE_AFF_NONE) {
			(*TColumn)(unsafe.Pointer(pCol)).Faffinity = aff
		}
		if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) >= int32(SQLITE_AFF_TEXT) && ((*TSelect)(unsafe.Pointer(pS2)).FpNext != 0 || pS2 != pSelect) {
			pS2 = (*TSelect)(unsafe.Pointer(pS2)).FpNext
			for {
				if !(pS2 != 0) {
					break
				}
				m = m | _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS2)).FpEList + 8 + uintptr(i)*20))).FpExpr)
				goto _2
			_2:
				;
				pS2 = (*TSelect)(unsafe.Pointer(pS2)).FpNext
			}
			if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) == int32(SQLITE_AFF_TEXT) && m&int32(0x01) != 0 {
				(*TColumn)(unsafe.Pointer(pCol)).Faffinity = int8(SQLITE_AFF_BLOB)
			} else {
				if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) >= int32(SQLITE_AFF_NUMERIC) && m&int32(0x02) != 0 {
					(*TColumn)(unsafe.Pointer(pCol)).Faffinity = int8(SQLITE_AFF_BLOB)
				}
			}
			if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) >= int32(SQLITE_AFF_NUMERIC) && int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_CAST) {
				(*TColumn)(unsafe.Pointer(pCol)).Faffinity = int8(SQLITE_AFF_FLEXNUM)
			}
		}
		zType = _columnTypeImpl(tls, bp, p, uintptr(0), uintptr(0), uintptr(0))
		if zType == uintptr(0) || int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) != int32(_sqlite3AffinityType(tls, zType, uintptr(0))) {
			if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) == int32(SQLITE_AFF_NUMERIC) || int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) == int32(SQLITE_AFF_FLEXNUM) {
				zType = __ccgo_ts + 21883
			} else {
				zType = uintptr(0)
				j = int32(1)
				for {
					if !(j < int32(SQLITE_N_STDTYPE)) {
						break
					}
					if int32(_sqlite3StdTypeAffinity[j]) == int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) {
						zType = _sqlite3StdType[j]
						break
					}
					goto _3
				_3:
					;
					j = j + 1
				}
			}
		}
		if zType != 0 {
			k = int64(libc.Xstrlen(tls, zType))
			n = int64(libc.Xstrlen(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName))
			(*TColumn)(unsafe.Pointer(pCol)).FzCnName = _sqlite3DbReallocOrFree(tls, db, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, uint64(n+k+int64(2)))
			v4 = pCol + 10
			*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) & ^(libc.Int32FromInt32(COLFLAG_HASTYPE) | libc.Int32FromInt32(COLFLAG_HASCOLL)))
			if (*TColumn)(unsafe.Pointer(pCol)).FzCnName != 0 {
				libc.Xmemcpy(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n+int64(1)), zType, uint32(k+int64(1)))
				v4 = pCol + 10
				*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(COLFLAG_HASTYPE))
			}
		}
		pColl = _sqlite3ExprCollSeq(tls, pParse, p)
		if pColl != 0 {
			_sqlite3ColumnSetColl(tls, db, pCol, (*TCollSeq)(unsafe.Pointer(pColl)).FzName)
		}
		goto _1
	_1:
		;
		i = i + 1
		pCol += 12
	}
	(*TTable)(unsafe.Pointer(pTab)).FszTabRow = int16(1) /* Any non-zero value works */
}

// C documentation
//
//	/*
//	** Make changes to the evolving bytecode to do affinity transformations
//	** of values that are about to be gathered into a row for table pTab.
//	**
//	** For ordinary (legacy, non-strict) tables:
//	** -----------------------------------------
//	**
//	** Compute the affinity string for table pTab, if it has not already been
//	** computed.  As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
//	**
//	** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries
//	** which were then optimized out) then this routine becomes a no-op.
//	**
//	** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the
//	** affinities for register iReg and following.  Or if iReg==0,
//	** then just set the P4 operand of the previous opcode (which should  be
//	** an OP_MakeRecord) to the affinity string.
//	**
//	** A column affinity string has one character per column:
//	**
//	**    Character      Column affinity
//	**    ---------      ---------------
//	**    'A'            BLOB
//	**    'B'            TEXT
//	**    'C'            NUMERIC
//	**    'D'            INTEGER
//	**    'E'            REAL
//	**
//	** For STRICT tables:
//	** ------------------
//	**
//	** Generate an appropriate OP_TypeCheck opcode that will verify the
//	** datatypes against the column definitions in pTab.  If iReg==0, that
//	** means an OP_MakeRecord opcode has already been generated and should be
//	** the last opcode generated.  The new OP_TypeCheck needs to be inserted
//	** before the OP_MakeRecord.  The new OP_TypeCheck should use the same
//	** register set as the OP_MakeRecord.  If iReg>0 then register iReg is
//	** the first of a series of registers that will form the new record.
//	** Apply the type checking to that array of registers.
//	*/
func _sqlite3TableAffinity(tls *libc.TLS, v uintptr, pTab uintptr, iReg int32) {
	var i, p3 int32
	var pPrev, zColAff uintptr
	_, _, _, _ = i, p3, pPrev, zColAff
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Strict) != 0 {
		if iReg == 0 {
			_sqlite3VdbeAppendP4(tls, v, pTab, -int32(5))
			pPrev = _sqlite3VdbeGetLastOp(tls, v)
			(*TVdbeOp)(unsafe.Pointer(pPrev)).Fopcode = uint8(OP_TypeCheck)
			p3 = (*TVdbeOp)(unsafe.Pointer(pPrev)).Fp3
			(*TVdbeOp)(unsafe.Pointer(pPrev)).Fp3 = 0
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), (*TVdbeOp)(unsafe.Pointer(pPrev)).Fp1, (*TVdbeOp)(unsafe.Pointer(pPrev)).Fp2, p3)
		} else {
			/* Insert an isolated OP_Typecheck */
			_sqlite3VdbeAddOp2(tls, v, int32(OP_TypeCheck), iReg, int32((*TTable)(unsafe.Pointer(pTab)).FnNVCol))
			_sqlite3VdbeAppendP4(tls, v, pTab, -int32(5))
		}
		return
	}
	zColAff = (*TTable)(unsafe.Pointer(pTab)).FzColAff
	if zColAff == uintptr(0) {
		zColAff = _sqlite3TableAffinityStr(tls, uintptr(0), pTab)
		if !(zColAff != 0) {
			_sqlite3OomFault(tls, _sqlite3VdbeDb(tls, v))
			return
		}
		(*TTable)(unsafe.Pointer(pTab)).FzColAff = zColAff
	}
	i = int32(libc.Xstrlen(tls, zColAff) & libc.Uint32FromInt32(0x3fffffff))
	if i != 0 {
		if iReg != 0 {
			_sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), iReg, i, 0, zColAff, i)
		} else {
			_sqlite3VdbeChangeP4(tls, v, -int32(1), zColAff, i)
		}
	}
}

// C documentation
//
//	/*
//	** Compute an affinity string for a table.   Space is obtained
//	** from sqlite3DbMalloc().  The caller is responsible for freeing
//	** the space when done.
//	*/
func _sqlite3TableAffinityStr(tls *libc.TLS, db uintptr, pTab uintptr) (r uintptr) {
	var i, j, v2, v3 int32
	var zColAff uintptr
	_, _, _, _, _ = i, j, zColAff, v2, v3
	zColAff = _sqlite3DbMallocRaw(tls, db, uint64(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+int32(1)))
	if zColAff != 0 {
		v2 = libc.Int32FromInt32(0)
		j = v2
		i = v2
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 {
				v3 = j
				j = j + 1
				**(**int8)(__ccgo_up(zColAff + uintptr(v3))) = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).Faffinity
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		for cond := true; cond; cond = j >= 0 && int32(**(**int8)(__ccgo_up(zColAff + uintptr(j)))) <= int32(SQLITE_AFF_BLOB) {
			v2 = j
			j = j - 1
			**(**int8)(__ccgo_up(zColAff + uintptr(v2))) = 0
		}
	}
	return zColAff
}

// C documentation
//
//	/* Convert a table column number into a storage column number.
//	**
//	** The storage column number (0,1,2,....) is the index of the value
//	** as it appears in the record on disk.  Or, if the input column is
//	** the N-th virtual column (zero-based) then the storage number is
//	** the number of non-virtual columns in the table plus N.
//	**
//	** The true column number is the index (0,1,2,...) of the column in
//	** the CREATE TABLE statement.
//	**
//	** If the input column is a VIRTUAL column, then it should not appear
//	** in storage.  But the value sometimes is cached in registers that
//	** follow the range of registers used to construct storage.  This
//	** avoids computing the same VIRTUAL column multiple times, and provides
//	** values for use by OP_Param opcodes in triggers.  Hence, if the
//	** input column is a VIRTUAL table, put it after all the other columns.
//	**
//	** In the following, N means "normal column", S means STORED, and
//	** V means VIRTUAL.  Suppose the CREATE TABLE has columns like this:
//	**
//	**        CREATE TABLE ex(N,S,V,N,S,V,N,S,V);
//	**                     -- 0 1 2 3 4 5 6 7 8
//	**
//	** Then the mapping from this function is as follows:
//	**
//	**    INPUTS:     0 1 2 3 4 5 6 7 8
//	**    OUTPUTS:    0 1 6 2 3 7 4 5 8
//	**
//	** So, in other words, this routine shifts all the virtual columns to
//	** the end.
//	**
//	** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and
//	** this routine is a no-op macro.  If the pTab does not have any virtual
//	** columns, then this routine is no-op that always return iCol.  If iCol
//	** is negative (indicating the ROWID column) then this routine return iCol.
//	*/
func _sqlite3TableColumnToStorage(tls *libc.TLS, pTab uintptr, iCol Ti16) (r Ti16) {
	var i int32
	var n Ti16
	_, _ = i, n
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasVirtual) == uint32(0) || int32(iCol) < 0 {
		return iCol
	}
	i = 0
	n = libc.Int16FromInt32(0)
	for {
		if !(i < int32(iCol)) {
			break
		}
		if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 {
			n = n + 1
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
		/* iCol is a virtual column itself */
		return int16(int32((*TTable)(unsafe.Pointer(pTab)).FnNVCol) + i - int32(n))
	} else {
		/* iCol is a normal or stored column */
		return n
	}
	return r
}

// C documentation
//
//	/* Create a new thread */
func _sqlite3ThreadCreate(tls *libc.TLS, ppThread uintptr, __ccgo_fp_xTask uintptr, pIn uintptr) (r int32) {
	var p uintptr
	_ = p
	**(**uintptr)(__ccgo_up(ppThread)) = uintptr(0)
	p = _sqlite3Malloc(tls, uint64(20))
	if p == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	/* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
	 ** function that returns SQLITE_ERROR when passed the argument 200, that
	 ** forces worker threads to run sequentially and deterministically
	 ** (via the sqlite3FaultSim() term of the conditional) for testing
	 ** purposes. */
	if int32(_sqlite3Config.FbCoreMutex) == 0 || _sqlite3FaultSim(tls, int32(200)) != 0 {
		libc.Xmemset(tls, p, 0, uint32(20))
	} else {
		(*TSQLiteThread)(unsafe.Pointer(p)).FxTask = __ccgo_fp_xTask
		(*TSQLiteThread)(unsafe.Pointer(p)).FpIn = pIn
		(*TSQLiteThread)(unsafe.Pointer(p)).Ftid = uintptr(libc.X_beginthreadex(tls, uintptr(0), uint32(0), __ccgo_fp(_sqlite3ThreadProc), p, uint32(0), p+4))
		if (*TSQLiteThread)(unsafe.Pointer(p)).Ftid == uintptr(0) {
			libc.Xmemset(tls, p, 0, uint32(20))
		}
	}
	if (*TSQLiteThread)(unsafe.Pointer(p)).FxTask == uintptr(0) {
		(*TSQLiteThread)(unsafe.Pointer(p)).Fid = uint32(libc.XGetCurrentThreadId(tls))
		(*TSQLiteThread)(unsafe.Pointer(p)).FpResult = (*(*func(*libc.TLS, uintptr) uintptr)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xTask})))(tls, pIn)
	}
	**(**uintptr)(__ccgo_up(ppThread)) = p
	return SQLITE_OK
}

/* os_win.c */

// C documentation
//
//	/*
//	** Triggers may access values stored in the old.* or new.* pseudo-table.
//	** This function returns a 32-bit bitmask indicating which columns of the
//	** old.* or new.* tables actually are used by triggers. This information
//	** may be used by the caller, for example, to avoid having to load the entire
//	** old.* record into memory when executing an UPDATE or DELETE command.
//	**
//	** Bit 0 of the returned mask is set if the left-most column of the
//	** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
//	** the second leftmost column value is required, and so on. If there
//	** are more than 32 columns in the table, and at least one of the columns
//	** with an index greater than 32 may be accessed, 0xffffffff is returned.
//	**
//	** It is not possible to determine if the old.rowid or new.rowid column is
//	** accessed by triggers. The caller must always assume that it is.
//	**
//	** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
//	** applies to the old.* table. If 1, the new.* table.
//	**
//	** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
//	** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
//	** included in the returned mask if the TRIGGER_BEFORE bit is set in the
//	** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
//	** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
//	*/
func _sqlite3TriggerColmask(tls *libc.TLS, pParse uintptr, pTrigger uintptr, pChanges uintptr, isNew int32, tr_tm int32, pTab uintptr, orconf int32) (r Tu32) {
	var mask Tu32
	var op, v1 int32
	var p, pPrg uintptr
	_, _, _, _, _ = mask, op, p, pPrg, v1
	if pChanges != 0 {
		v1 = int32(TK_UPDATE)
	} else {
		v1 = int32(TK_DELETE)
	}
	op = v1
	mask = uint32(0)
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
		return uint32(0xffffffff)
	}
	p = pTrigger
	for {
		if !(p != 0) {
			break
		}
		if int32((*TTrigger)(unsafe.Pointer(p)).Fop) == op && tr_tm&int32((*TTrigger)(unsafe.Pointer(p)).Ftr_tm) != 0 && _checkColumnOverlap(tls, (*TTrigger)(unsafe.Pointer(p)).FpColumns, pChanges) != 0 {
			if (*TTrigger)(unsafe.Pointer(p)).FbReturning != 0 {
				mask = uint32(0xffffffff)
			} else {
				pPrg = _getRowTrigger(tls, pParse, p, pTab, orconf)
				if pPrg != 0 {
					mask = mask | **(**Tu32)(__ccgo_up(pPrg + 16 + uintptr(isNew)*4))
				}
			}
		}
		goto _2
	_2:
		;
		p = (*TTrigger)(unsafe.Pointer(p)).FpNext
	}
	return mask
}

// C documentation
//
//	/*
//	** Given table pTab, return a list of all the triggers attached to
//	** the table. The list is connected by Trigger.pNext pointers.
//	**
//	** All of the triggers on pTab that are in the same database as pTab
//	** are already attached to pTab->pTrigger.  But there might be additional
//	** triggers on pTab in the TEMP schema.  This routine prepends all
//	** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
//	** and returns the combined list.
//	**
//	** To state it another way:  This routine returns a list of all triggers
//	** that fire off of pTab.  The list will include any TEMP triggers on
//	** pTab as well as the triggers lised in pTab->pTrigger.
//	*/
func _sqlite3TriggerList(tls *libc.TLS, pParse uintptr, pTab uintptr) (r uintptr) {
	var p, pList, pTmpSchema, pTrig uintptr
	_, _, _, _ = p, pList, pTmpSchema, pTrig /* Loop variable for TEMP triggers */
	pTmpSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + 1*16))).FpSchema
	p = (*THash)(unsafe.Pointer(pTmpSchema + 40)).Ffirst
	pList = (*TTable)(unsafe.Pointer(pTab)).FpTrigger
	for p != 0 {
		pTrig = (*THashElem)(unsafe.Pointer(p)).Fdata
		if (*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema == (*TTable)(unsafe.Pointer(pTab)).FpSchema && (*TTrigger)(unsafe.Pointer(pTrig)).Ftable != 0 && 0 == _sqlite3StrICmp(tls, (*TTrigger)(unsafe.Pointer(pTrig)).Ftable, (*TTable)(unsafe.Pointer(pTab)).FzName) && ((*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema != pTmpSchema || (*TTrigger)(unsafe.Pointer(pTrig)).FbReturning != 0) {
			(*TTrigger)(unsafe.Pointer(pTrig)).FpNext = pList
			pList = pTrig
		} else {
			if int32((*TTrigger)(unsafe.Pointer(pTrig)).Fop) == int32(TK_RETURNING) {
				(*TTrigger)(unsafe.Pointer(pTrig)).Ftable = (*TTable)(unsafe.Pointer(pTab)).FzName
				(*TTrigger)(unsafe.Pointer(pTrig)).FpTabSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema
				(*TTrigger)(unsafe.Pointer(pTrig)).FpNext = pList
				pList = pTrig
			}
		}
		p = (*THashElem)(unsafe.Pointer(p)).Fnext
	}
	return pList
}

// C documentation
//
//	/*
//	** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
//	*/
func _sqlite3UniqueConstraint(tls *libc.TLS, pParse uintptr, onError int32, pIdx uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var j, v2 int32
	var pTab, zCol, zErr uintptr
	var _ /* errMsg at bp+0 */ TStrAccum
	_, _, _, _, _ = j, pTab, zCol, zErr, v2
	pTab = (*TIndex)(unsafe.Pointer(pIdx)).FpTable
	_sqlite3StrAccumInit(tls, bp, (*TParse)(unsafe.Pointer(pParse)).Fdb, uintptr(0), 0, **(**int32)(__ccgo_up((*TParse)(unsafe.Pointer(pParse)).Fdb + 120)))
	if (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr != 0 {
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+17425, libc.VaList(bp+32, (*TIndex)(unsafe.Pointer(pIdx)).FzName))
	} else {
		j = 0
		for {
			if !(j < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
				break
			}
			zCol = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2)))*12))).FzCnName
			if j != 0 {
				Xsqlite3_str_append(tls, bp, __ccgo_ts+17436, int32(2))
			}
			Xsqlite3_str_appendall(tls, bp, (*TTable)(unsafe.Pointer(pTab)).FzName)
			Xsqlite3_str_append(tls, bp, __ccgo_ts+1750, int32(1))
			Xsqlite3_str_appendall(tls, bp, zCol)
			goto _1
		_1:
			;
			j = j + 1
		}
	}
	zErr = _sqlite3StrAccumFinish(tls, bp)
	if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
		v2 = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(6)<<libc.Int32FromInt32(8)
	} else {
		v2 = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(8)<<libc.Int32FromInt32(8)
	}
	_sqlite3HaltConstraint(tls, pParse, v2, onError, zErr, int8(-libc.Int32FromInt32(7)), uint8(P5_ConstraintUnique))
}

// C documentation
//
//	/*
//	** Process an UPDATE statement.
//	**
//	**   UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL;
//	**          \_______/ \_/     \______/      \_____/       \________________/
//	**           onError   |      pChanges         |                pWhere
//	**                     \_______________________/
//	**                               pTabList
//	*/
func _sqlite3Update(tls *libc.TLS, pParse uintptr, pTabList uintptr, pChanges uintptr, pWhere uintptr, onError int32, pOrderBy uintptr, pLimit uintptr, pUpsert uintptr) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var aRegIdx, aToOpen, aXRef, db, pIdx, pKeyInfo, pPk, pRowidExpr, pTab, pTrigger, pWInfo, v, v4 uintptr
	var addrOnce, addrOpen, addrTop, bFinishSeek, bProgress, eOnePass, flags, hasFK, i, iBaseCur, iCur, iDataCur, iDb, iEph, iIdxCur, iPk, iRowidExpr, isView, j, k, labelBreak, labelContinue, nAllIdx, nChangeFrom, nEphCol, nIdx, nKey, nOff, newmask, rc, reg, regKey, regNew, regNewRowid, regOld, regOldRowid, regRowCount, regRowSet, v1, v2 int32
	var chngKey, chngPk, chngRowid, v8 Tu8
	var colFlags, oldmask Tu32
	var nPk Ti16
	var v12 uint64
	var v42 uint32
	var _ /* aiCurOnePass at bp+48 */ [2]int32
	var _ /* bReplace at bp+56 */ int32
	var _ /* iNotUsed1 at bp+60 */ int32
	var _ /* iNotUsed2 at bp+64 */ int32
	var _ /* sContext at bp+0 */ TAuthContext
	var _ /* sNC at bp+8 */ TNameContext
	var _ /* tmask at bp+44 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aRegIdx, aToOpen, aXRef, addrOnce, addrOpen, addrTop, bFinishSeek, bProgress, chngKey, chngPk, chngRowid, colFlags, db, eOnePass, flags, hasFK, i, iBaseCur, iCur, iDataCur, iDb, iEph, iIdxCur, iPk, iRowidExpr, isView, j, k, labelBreak, labelContinue, nAllIdx, nChangeFrom, nEphCol, nIdx, nKey, nOff, nPk, newmask, oldmask, pIdx, pKeyInfo, pPk, pRowidExpr, pTab, pTrigger, pWInfo, rc, reg, regKey, regNew, regNewRowid, regOld, regOldRowid, regRowCount, regRowSet, v, v1, v12, v2, v4, v42, v8 /* The table to be updated */
	addrTop = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           /* VDBE instruction address of the start of the loop */
	pWInfo = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   /* The database structure */
	aRegIdx = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  /* Registers for to each index and the main table */
	aXRef = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    /* Either chngPk or chngRowid */
	pRowidExpr = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               /* Expression defining the new record number */
	iRowidExpr = -int32(1)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                /* Mask of NEW.* columns accessed by BEFORE triggers */
	iEph = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              /* Ephemeral table holding all primary key values */
	nKey = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              /* The write cursors opened by WHERE_ONEPASS */
	addrOpen = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          /* Address of OP_OpenEphemeral */
	iPk = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               /* First of nPk cells holding PRIMARY KEY value */
	nPk = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               /* Number of components of the PRIMARY KEY */
	**(**int32)(__ccgo_up(bp + 56)) = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   /* True if REPLACE conflict resolution might happen */
	bFinishSeek = int32(1)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                /* The OP_FinishSeek opcode is needed */
	nChangeFrom = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       /* If there is a FROM, pChanges->nExpr, else 0 */
	/* Register Allocations */
	regRowCount = 0 /* A count of rows changed */
	regOldRowid = 0 /* The old rowid */
	regNewRowid = 0 /* The new rowid */
	regNew = 0      /* Content of the NEW.* table in triggers */
	regOld = 0      /* Content of OLD.* table in triggers */
	regRowSet = 0   /* Rowset of rows to be updated */
	regKey = 0      /* composite PRIMARY KEY value */
	libc.Xmemset(tls, bp, 0, uint32(8))
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		goto update_cleanup
	}
	/* Locate the table which we want to update.
	 */
	pTab = _sqlite3SrcListLookup(tls, pParse, pTabList)
	if pTab == uintptr(0) {
		goto update_cleanup
	}
	iDb = _sqlite3SchemaToIndex(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	/* Figure out if we have any triggers and if the table being
	 ** updated is a view.
	 */
	pTrigger = _sqlite3TriggersExist(tls, pParse, pTab, int32(TK_UPDATE), pChanges, bp+44)
	isView = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW))
	/* If there was a FROM clause, set nChangeFrom to the number of expressions
	 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
	 ** clause if this function is being called to generate code for part of
	 ** an UPSERT statement.  */
	if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc > int32(1) {
		v1 = (*TExprList)(unsafe.Pointer(pChanges)).FnExpr
	} else {
		v1 = 0
	}
	nChangeFrom = v1
	if _sqlite3ViewGetColumnNames(tls, pParse, pTab) != 0 {
		goto update_cleanup
	}
	if _sqlite3IsReadOnly(tls, pParse, pTab, pTrigger) != 0 {
		goto update_cleanup
	}
	/* Allocate a cursors for the main database table and for all indices.
	 ** The index cursors might not be used, but if they are used they
	 ** need to occur right after the database cursor.  So go ahead and
	 ** allocate enough space, just in case.
	 */
	v4 = pParse + 44
	v2 = *(*int32)(unsafe.Pointer(v4))
	*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
	v1 = v2
	iDataCur = v1
	iBaseCur = v1
	iIdxCur = iDataCur + int32(1)
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
		v4 = uintptr(0)
	} else {
		v4 = _sqlite3PrimaryKeyIndex(tls, pTab)
	}
	pPk = v4
	nIdx = 0
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		if pPk == pIdx {
			iDataCur = (*TParse)(unsafe.Pointer(pParse)).FnTab
		}
		(*TParse)(unsafe.Pointer(pParse)).FnTab = (*TParse)(unsafe.Pointer(pParse)).FnTab + 1
		goto _6
	_6:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		nIdx = nIdx + 1
	}
	if pUpsert != 0 {
		/* On an UPSERT, reuse the same cursors already opened by INSERT */
		iDataCur = (*TUpsert)(unsafe.Pointer(pUpsert)).FiDataCur
		iIdxCur = (*TUpsert)(unsafe.Pointer(pUpsert)).FiIdxCur
		(*TParse)(unsafe.Pointer(pParse)).FnTab = iBaseCur
	}
	(*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor = iDataCur
	/* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
	 ** Initialize aXRef[] and aToOpen[] to their default values.
	 */
	aXRef = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+nIdx+libc.Int32FromInt32(1))+uint32(nIdx)+uint32(2)))
	if aXRef == uintptr(0) {
		goto update_cleanup
	}
	aRegIdx = aXRef + uintptr((*TTable)(unsafe.Pointer(pTab)).FnCol)*4
	aToOpen = aRegIdx + uintptr(nIdx)*4 + libc.UintptrFromInt32(1)*4
	libc.Xmemset(tls, aToOpen, int32(1), uint32(nIdx+int32(1)))
	**(**Tu8)(__ccgo_up(aToOpen + uintptr(nIdx+int32(1)))) = uint8(0)
	i = 0
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
			break
		}
		**(**int32)(__ccgo_up(aXRef + uintptr(i)*4)) = -int32(1)
		goto _7
	_7:
		;
		i = i + 1
	}
	/* Initialize the name-context */
	libc.Xmemset(tls, bp+8, 0, uint32(36))
	(**(**TNameContext)(__ccgo_up(bp + 8))).FpParse = pParse
	(**(**TNameContext)(__ccgo_up(bp + 8))).FpSrcList = pTabList
	*(*uintptr)(unsafe.Pointer(bp + 8 + 8)) = pUpsert
	(**(**TNameContext)(__ccgo_up(bp + 8))).FncFlags = int32(NC_UUpsert)
	/* Begin generating code. */
	v = _sqlite3GetVdbe(tls, pParse)
	if v == uintptr(0) {
		goto update_cleanup
	}
	/* Resolve the column names in all the expressions of the
	 ** of the UPDATE statement.  Also find the column index
	 ** for each column to be updated in the pChanges array.  For each
	 ** column to be updated, make sure we have authorization to change
	 ** that column.
	 */
	v8 = libc.Uint8FromInt32(0)
	chngPk = v8
	chngRowid = v8
	i = 0
	for {
		if !(i < (*TExprList)(unsafe.Pointer(pChanges)).FnExpr) {
			break
		}
		/* If this is an UPDATE with a FROM clause, do not resolve expressions
		 ** here. The call to sqlite3Select() below will do that. */
		if nChangeFrom == 0 && _sqlite3ResolveExprNames(tls, bp+8, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FpExpr) != 0 {
			goto update_cleanup
		}
		j = _sqlite3ColumnIndex(tls, pTab, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FzEName)
		if j >= 0 {
			if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
				chngRowid = uint8(1)
				pRowidExpr = (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FpExpr
				iRowidExpr = i
			} else {
				if pPk != 0 && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 {
					chngPk = uint8(1)
				} else {
					if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
						_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23837, libc.VaList(bp+80, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FzCnName))
						goto update_cleanup
					}
				}
			}
			**(**int32)(__ccgo_up(aXRef + uintptr(j)*4)) = i
		} else {
			if pPk == uintptr(0) && _sqlite3IsRowid(tls, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FzEName) != 0 {
				j = -int32(1)
				chngRowid = uint8(1)
				pRowidExpr = (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FpExpr
				iRowidExpr = i
			} else {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13123, libc.VaList(bp+80, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FzEName))
				libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 8, 0x100)
				goto update_cleanup
			}
		}
		if j < 0 {
			v4 = __ccgo_ts + 9414
		} else {
			v4 = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FzCnName
		}
		rc = _sqlite3AuthCheck(tls, pParse, int32(SQLITE_UPDATE), (*TTable)(unsafe.Pointer(pTab)).FzName, v4, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName)
		if rc == int32(SQLITE_DENY) {
			goto update_cleanup
		} else {
			if rc == int32(SQLITE_IGNORE) {
				**(**int32)(__ccgo_up(aXRef + uintptr(j)*4)) = -int32(1)
			}
		}
		goto _9
	_9:
		;
		i = i + 1
	}
	chngKey = uint8(int32(chngRowid) + int32(chngPk))
	/* Mark generated columns as changing if their generator expressions
	 ** reference any changing column.  The actual aXRef[] value for
	 ** generated expressions is not used, other than to check to see that it
	 ** is non-negative, so the value of aXRef[] for generated columns can be
	 ** set to any non-negative number.  We use 99999 so that the value is
	 ** obvious when looking at aXRef[] in a symbolic debugger.
	 */
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != 0 {
		for cond := true; cond; cond = bProgress != 0 {
			bProgress = 0
			i = 0
			for {
				if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
					break
				}
				if **(**int32)(__ccgo_up(aXRef + uintptr(i)*4)) >= 0 {
					goto _11
				}
				if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_GENERATED) == 0 {
					goto _11
				}
				if _sqlite3ExprReferencesUpdatedColumn(tls, _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*12), aXRef, int32(chngRowid)) != 0 {
					**(**int32)(__ccgo_up(aXRef + uintptr(i)*4)) = int32(99999)
					bProgress = int32(1)
				}
				goto _11
			_11:
				;
				i = i + 1
			}
		}
	}
	/* The SET expressions are not actually used inside the WHERE loop.
	 ** So reset the colUsed mask. Unless this is a virtual table. In that
	 ** case, set all bits of the colUsed mask (to ensure that the virtual
	 ** table implementation makes all columns available).
	 */
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		v12 = uint64(-libc.Int32FromInt32(1))
	} else {
		v12 = uint64(0)
	}
	(*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FcolUsed = v12
	hasFK = _sqlite3FkRequired(tls, pParse, pTab, aXRef, int32(chngKey))
	/* There is one entry in the aRegIdx[] array for each index on the table
	 ** being updated.  Fill in aRegIdx[] with a register number that will hold
	 ** the key for accessing each index.
	 */
	if onError == int32(OE_Replace) {
		**(**int32)(__ccgo_up(bp + 56)) = int32(1)
	}
	nAllIdx = 0
	pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
	for {
		if !(pIdx != 0) {
			break
		}
		if chngKey != 0 || hasFK > int32(1) || pIdx == pPk || _indexWhereClauseMightChange(tls, pIdx, aXRef, int32(chngRowid)) != 0 {
			v4 = pParse + 48
			*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
			v1 = *(*int32)(unsafe.Pointer(v4))
			reg = v1
			**(**int32)(__ccgo_up(pParse + 48)) += int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
		} else {
			reg = 0
			i = 0
			for {
				if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
					break
				}
				if _indexColumnIsBeingUpdated(tls, pIdx, i, aXRef, int32(chngRowid)) != 0 {
					v4 = pParse + 48
					*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
					v1 = *(*int32)(unsafe.Pointer(v4))
					reg = v1
					**(**int32)(__ccgo_up(pParse + 48)) += int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
					if onError == int32(OE_Default) && int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) == int32(OE_Replace) {
						**(**int32)(__ccgo_up(bp + 56)) = int32(1)
					}
					break
				}
				goto _16
			_16:
				;
				i = i + 1
			}
		}
		if reg == 0 {
			**(**Tu8)(__ccgo_up(aToOpen + uintptr(nAllIdx+int32(1)))) = uint8(0)
		}
		**(**int32)(__ccgo_up(aRegIdx + uintptr(nAllIdx)*4)) = reg
		goto _13
	_13:
		;
		pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		nAllIdx = nAllIdx + 1
	}
	v4 = pParse + 48
	*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
	v1 = *(*int32)(unsafe.Pointer(v4))
	**(**int32)(__ccgo_up(aRegIdx + uintptr(nAllIdx)*4)) = v1 /* Register storing the table record */
	if **(**int32)(__ccgo_up(bp + 56)) != 0 {
		/* If REPLACE conflict resolution might be invoked, open cursors on all
		 ** indexes in case they are needed to delete records.  */
		libc.Xmemset(tls, aToOpen, int32(1), uint32(nIdx+int32(1)))
	}
	if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 {
		_sqlite3VdbeCountChanges(tls, v)
	}
	_sqlite3BeginWriteOperation(tls, pParse, libc.BoolInt32(pTrigger != 0 || hasFK != 0), iDb)
	/* Allocate required registers. */
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
		/* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
		 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
		 ** reallocated.  aRegIdx[nAllIdx] is the register in which the main
		 ** table record is written.  regRowSet holds the RowSet for the
		 ** two-pass update algorithm. */
		regRowSet = **(**int32)(__ccgo_up(aRegIdx + uintptr(nAllIdx)*4))
		v4 = pParse + 48
		*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
		v2 = *(*int32)(unsafe.Pointer(v4))
		v1 = v2
		regNewRowid = v1
		regOldRowid = v1
		if chngPk != 0 || pTrigger != 0 || hasFK != 0 {
			regOld = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
			**(**int32)(__ccgo_up(pParse + 48)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
		}
		if chngKey != 0 || pTrigger != 0 || hasFK != 0 {
			v4 = pParse + 48
			*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
			v1 = *(*int32)(unsafe.Pointer(v4))
			regNewRowid = v1
		}
		regNew = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
	}
	/* Start the view context. */
	if isView != 0 {
		_sqlite3AuthContextPush(tls, pParse, bp, (*TTable)(unsafe.Pointer(pTab)).FzName)
	}
	/* If we are trying to update a view, realize that view into
	 ** an ephemeral table.
	 */
	if nChangeFrom == 0 && isView != 0 {
		_sqlite3MaterializeView(tls, pParse, pTab, pWhere, pOrderBy, pLimit, iDataCur)
		pOrderBy = uintptr(0)
		pLimit = uintptr(0)
	}
	/* Resolve the column names in all the expressions in the
	 ** WHERE clause.
	 */
	if nChangeFrom == 0 && _sqlite3ResolveExprNames(tls, bp+8, pWhere) != 0 {
		goto update_cleanup
	}
	/* Virtual tables must be handled separately */
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		_updateVirtualTable(tls, pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, pWhere, onError)
		goto update_cleanup
	}
	/* Jump to labelBreak to abandon further processing of this UPDATE */
	v1 = _sqlite3VdbeMakeLabel(tls, pParse)
	labelBreak = v1
	labelContinue = v1
	/* Not an UPSERT.  Normal processing.  Begin by
	 ** initialize the count of updated rows */
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00001))<<libc.Int32FromInt32(32)) != uint64(0) && !((*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0) && !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) && !(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x8>>3)) != 0) && pUpsert == uintptr(0) {
		v4 = pParse + 48
		*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
		v1 = *(*int32)(unsafe.Pointer(v4))
		regRowCount = v1
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regRowCount)
	}
	if nChangeFrom == 0 && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, regRowSet, regOldRowid)
		v4 = pParse + 44
		v1 = *(*int32)(unsafe.Pointer(v4))
		*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
		iEph = v1
		addrOpen = _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenEphemeral), iEph, 0, regRowSet)
	} else {
		if pPk != 0 {
			v1 = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)
		} else {
			v1 = 0
		}
		nPk = int16(v1)
		iPk = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += int32(nPk)
		**(**int32)(__ccgo_up(pParse + 48)) += nChangeFrom
		v4 = pParse + 48
		*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
		v1 = *(*int32)(unsafe.Pointer(v4))
		regKey = v1
		if pUpsert == uintptr(0) {
			if isView != 0 {
				v1 = int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
			} else {
				v1 = 0
			}
			nEphCol = int32(nPk) + nChangeFrom + v1
			v4 = pParse + 44
			v2 = *(*int32)(unsafe.Pointer(v4))
			*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
			iEph = v2
			if pPk != 0 {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, iPk, iPk+int32(nPk)-int32(1))
			}
			addrOpen = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), iEph, nEphCol)
			if pPk != 0 {
				pKeyInfo = _sqlite3KeyInfoOfIndex(tls, pParse, pPk)
				if pKeyInfo != 0 {
					(*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnAllField = uint16(nEphCol)
					_sqlite3VdbeAppendP4(tls, v, pKeyInfo, -int32(9))
				}
			}
			if nChangeFrom != 0 {
				_updateFromSelect(tls, pParse, iEph, pPk, pChanges, pTabList, pWhere, pOrderBy, pLimit)
				if isView != 0 {
					iDataCur = iEph
				}
			}
		}
	}
	if nChangeFrom != 0 {
		_sqlite3MultiWrite(tls, pParse)
		eOnePass = ONEPASS_OFF
		nKey = int32(nPk)
		regKey = iPk
	} else {
		if pUpsert != 0 {
			/* If this is an UPSERT, then all cursors have already been opened by
			 ** the outer INSERT and the data cursor should be pointing at the row
			 ** that is to be updated.  So bypass the code that searches for the
			 ** row(s) to be updated.
			 */
			pWInfo = uintptr(0)
			eOnePass = int32(ONEPASS_SINGLE)
			_sqlite3ExprIfFalse(tls, pParse, pWhere, labelBreak, int32(SQLITE_JUMPIFNULL))
			bFinishSeek = 0
		} else {
			/* Begin the database scan.
			 **
			 ** Do not consider a single-pass strategy for a multi-row update if
			 ** there is anything that might disrupt the cursor being used to do
			 ** the UPDATE:
			 **   (1) This is a nested UPDATE
			 **   (2) There are triggers
			 **   (3) There are FOREIGN KEY constraints
			 **   (4) There are REPLACE conflict handlers
			 **   (5) There are subqueries in the WHERE clause
			 */
			flags = int32(WHERE_ONEPASS_DESIRED)
			if !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) && !(pTrigger != 0) && !(hasFK != 0) && !(chngKey != 0) && !(**(**int32)(__ccgo_up(bp + 56)) != 0) && (pWhere == uintptr(0) || !((*TExpr)(unsafe.Pointer(pWhere)).Fflags&uint32(libc.Int32FromInt32(EP_Subquery)) != libc.Uint32FromInt32(0))) {
				flags = flags | int32(WHERE_ONEPASS_MULTIROW)
			}
			pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, uintptr(0), uintptr(0), uintptr(0), uint16(flags), iIdxCur)
			if pWInfo == uintptr(0) {
				goto update_cleanup
			}
			/* A one-pass strategy that might update more than one row may not
			 ** be used if any column of the index used for the scan is being
			 ** updated. Otherwise, if there is an index on "b", statements like
			 ** the following could create an infinite loop:
			 **
			 **   UPDATE t1 SET b=b+1 WHERE b>?
			 **
			 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
			 ** strategy that uses an index for which one or more columns are being
			 ** updated.  */
			eOnePass = _sqlite3WhereOkOnePass(tls, pWInfo, bp+48)
			bFinishSeek = _sqlite3WhereUsesDeferredSeek(tls, pWInfo)
			if eOnePass != int32(ONEPASS_SINGLE) {
				_sqlite3MultiWrite(tls, pParse)
				if eOnePass == int32(ONEPASS_MULTI) {
					iCur = (**(**[2]int32)(__ccgo_up(bp + 48)))[int32(1)]
					if iCur >= 0 && iCur != iDataCur && **(**Tu8)(__ccgo_up(aToOpen + uintptr(iCur-iBaseCur))) != 0 {
						eOnePass = ONEPASS_OFF
					}
				}
			}
		}
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			/* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
			 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
			 ** leave it in register regOldRowid.  */
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iDataCur, regOldRowid)
			if eOnePass == ONEPASS_OFF {
				v4 = pParse + 48
				*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
				v1 = *(*int32)(unsafe.Pointer(v4))
				**(**int32)(__ccgo_up(aRegIdx + uintptr(nAllIdx)*4)) = v1
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iEph, regRowSet, regOldRowid)
			} else {
				if addrOpen != 0 {
					_sqlite3VdbeChangeToNoop(tls, v, addrOpen)
				}
			}
		} else {
			/* Read the PK of the current row into an array of registers. In
			 ** ONEPASS_OFF mode, serialize the array into a record and store it in
			 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
			 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
			 ** is not required) and leave the PK fields in the array of registers.  */
			i = 0
			for {
				if !(i < int32(nPk)) {
					break
				}
				_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iDataCur, int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))), iPk+i)
				goto _39
			_39:
				;
				i = i + 1
			}
			if eOnePass != 0 {
				if addrOpen != 0 {
					_sqlite3VdbeChangeToNoop(tls, v, addrOpen)
				}
				nKey = int32(nPk)
				regKey = iPk
			} else {
				_sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), iPk, int32(nPk), regKey, _sqlite3IndexAffinityStr(tls, db, pPk), int32(nPk))
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iEph, regKey, iPk, int32(nPk))
			}
		}
	}
	if pUpsert == uintptr(0) {
		if nChangeFrom == 0 && eOnePass != int32(ONEPASS_MULTI) {
			_sqlite3WhereEnd(tls, pWInfo)
		}
		if !(isView != 0) {
			addrOnce = 0
			**(**int32)(__ccgo_up(bp + 60)) = 0
			**(**int32)(__ccgo_up(bp + 64)) = 0
			/* Open every index that needs updating. */
			if eOnePass != ONEPASS_OFF {
				if (**(**[2]int32)(__ccgo_up(bp + 48)))[0] >= 0 {
					**(**Tu8)(__ccgo_up(aToOpen + uintptr((**(**[2]int32)(__ccgo_up(bp + 48)))[0]-iBaseCur))) = uint8(0)
				}
				if (**(**[2]int32)(__ccgo_up(bp + 48)))[int32(1)] >= 0 {
					**(**Tu8)(__ccgo_up(aToOpen + uintptr((**(**[2]int32)(__ccgo_up(bp + 48)))[int32(1)]-iBaseCur))) = uint8(0)
				}
			}
			if eOnePass == int32(ONEPASS_MULTI) && nIdx-libc.BoolInt32((**(**[2]int32)(__ccgo_up(bp + 48)))[int32(1)] >= 0) > 0 {
				addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
			}
			_sqlite3OpenTableAndIndices(tls, pParse, pTab, int32(OP_OpenWrite), uint8(0), iBaseCur, aToOpen, bp+60, bp+64)
			if addrOnce != 0 {
				_sqlite3VdbeJumpHereOrPopInst(tls, v, addrOnce)
			}
		}
		/* Top of the update loop */
		if eOnePass != ONEPASS_OFF {
			if (**(**[2]int32)(__ccgo_up(bp + 48)))[0] != iDataCur && (**(**[2]int32)(__ccgo_up(bp + 48)))[int32(1)] != iDataCur {
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iDataCur, labelBreak, regKey, nKey)
			}
			if eOnePass != int32(ONEPASS_SINGLE) {
				labelContinue = _sqlite3VdbeMakeLabel(tls, pParse)
			}
			if pPk != 0 {
				v1 = regKey
			} else {
				v1 = regOldRowid
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), v1, labelBreak)
		} else {
			if pPk != 0 || nChangeFrom != 0 {
				labelContinue = _sqlite3VdbeMakeLabel(tls, pParse)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iEph, labelBreak)
				addrTop = _sqlite3VdbeCurrentAddr(tls, v)
				if nChangeFrom != 0 {
					if !(isView != 0) {
						if pPk != 0 {
							i = 0
							for {
								if !(i < int32(nPk)) {
									break
								}
								_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEph, i, iPk+i)
								goto _41
							_41:
								;
								i = i + 1
							}
							_sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iDataCur, labelContinue, iPk, int32(nPk))
						} else {
							_sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iEph, regOldRowid)
							_sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDataCur, labelContinue, regOldRowid)
						}
					}
				} else {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_RowData), iEph, regKey)
					_sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iDataCur, labelContinue, regKey, 0)
				}
			} else {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iEph, labelBreak)
				labelContinue = _sqlite3VdbeMakeLabel(tls, pParse)
				addrTop = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iEph, regOldRowid)
				_sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDataCur, labelContinue, regOldRowid)
			}
		}
	}
	/* If the rowid value will change, set register regNewRowid to
	 ** contain the new value. If the rowid is not being modified,
	 ** then regNewRowid is the same register as regOldRowid, which is
	 ** already populated.  */
	if chngRowid != 0 {
		if nChangeFrom == 0 {
			_sqlite3ExprCode(tls, pParse, pRowidExpr, regNewRowid)
		} else {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEph, iRowidExpr, regNewRowid)
		}
		_sqlite3VdbeAddOp1(tls, v, int32(OP_MustBeInt), regNewRowid)
	}
	/* Compute the old pre-UPDATE content of the row being changed, if that
	 ** information is needed */
	if chngPk != 0 || hasFK != 0 || pTrigger != 0 {
		if hasFK != 0 {
			v42 = _sqlite3FkOldmask(tls, pParse, pTab)
		} else {
			v42 = uint32(0)
		}
		oldmask = v42
		oldmask = oldmask | _sqlite3TriggerColmask(tls, pParse, pTrigger, pChanges, 0, libc.Int32FromInt32(TRIGGER_BEFORE)|libc.Int32FromInt32(TRIGGER_AFTER), pTab, onError)
		i = 0
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			colFlags = uint32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)
			k = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(i))) + regOld
			if oldmask == uint32(0xffffffff) || i < int32(32) && oldmask&(libc.Uint32FromInt32(1)<<i) != uint32(0) || colFlags&uint32(COLFLAG_PRIMKEY) != uint32(0) {
				_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iDataCur, i, k)
			} else {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, k)
			}
			goto _43
		_43:
			;
			i = i + 1
		}
		if int32(chngRowid) == 0 && pPk == uintptr(0) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), regOldRowid, regNewRowid)
		}
	}
	/* Populate the array of registers beginning at regNew with the new
	 ** row data. This array is used to check constants, create the new
	 ** table and index records, and as the values for any new.* references
	 ** made by triggers.
	 **
	 ** If there are one or more BEFORE triggers, then do not populate the
	 ** registers associated with columns that are (a) not modified by
	 ** this UPDATE statement and (b) not accessed by new.* references. The
	 ** values for registers not modified by the UPDATE must be reloaded from
	 ** the database after the BEFORE triggers are fired anyway (as the trigger
	 ** may have modified them). So not loading those that are not going to
	 ** be used eliminates some redundant opcodes.
	 */
	newmask = int32(_sqlite3TriggerColmask(tls, pParse, pTrigger, pChanges, int32(1), int32(TRIGGER_BEFORE), pTab, onError))
	i = 0
	k = regNew
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
			break
		}
		if i == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, k)
		} else {
			if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
				if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
					k = k - 1
				}
			} else {
				j = **(**int32)(__ccgo_up(aXRef + uintptr(i)*4))
				if j >= 0 {
					if nChangeFrom != 0 {
						if isView != 0 {
							v1 = int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
						} else {
							v1 = int32(nPk)
						}
						nOff = v1
						_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEph, nOff+j, k)
					} else {
						_sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(j)*20))).FpExpr, k)
					}
				} else {
					if 0 == **(**int32)(__ccgo_up(bp + 44))&int32(TRIGGER_BEFORE) || i > int32(31) || uint32(newmask)&(libc.Uint32FromInt32(1)<<i) != 0 {
						/* This branch loads the value of a column that will not be changed
						 ** into a register. This is done if there are no BEFORE triggers, or
						 ** if there are one or more BEFORE triggers that use this value via
						 ** a new.* reference in a trigger program.
						 */
						_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iDataCur, i, k)
						bFinishSeek = 0
					} else {
						_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, k)
					}
				}
			}
		}
		goto _44
	_44:
		;
		i = i + 1
		k = k + 1
	}
	if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != 0 {
		_sqlite3ComputeGeneratedColumns(tls, pParse, regNew, pTab)
	}
	/* Fire any BEFORE UPDATE triggers. This happens before constraints are
	 ** verified. One could argue that this is wrong.
	 */
	if **(**int32)(__ccgo_up(bp + 44))&int32(TRIGGER_BEFORE) != 0 {
		_sqlite3TableAffinity(tls, v, pTab, regNew)
		_sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_UPDATE), pChanges, int32(TRIGGER_BEFORE), pTab, regOldRowid, onError, labelContinue)
		if !(isView != 0) {
			/* The row-trigger may have deleted the row being updated. In this
			 ** case, jump to the next row. No updates or AFTER triggers are
			 ** required. This behavior - what happens when the row being updated
			 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
			 ** documentation.
			 */
			if pPk != 0 {
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iDataCur, labelContinue, regKey, nKey)
			} else {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDataCur, labelContinue, regOldRowid)
			}
			/* After-BEFORE-trigger-reload-loop:
			 ** If it did not delete it, the BEFORE trigger may still have modified
			 ** some of the columns of the row being updated. Load the values for
			 ** all columns not modified by the update statement into their registers
			 ** in case this has happened. Only unmodified columns are reloaded.
			 ** The values computed for modified columns use the values before the
			 ** BEFORE trigger runs.  See test case trigger1-18.0 (added 2018-04-26)
			 ** for an example.
			 */
			i = 0
			k = regNew
			for {
				if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
					break
				}
				if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
					if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
						k = k - 1
					}
				} else {
					if **(**int32)(__ccgo_up(aXRef + uintptr(i)*4)) < 0 && i != int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
						_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iDataCur, i, k)
					}
				}
				goto _46
			_46:
				;
				i = i + 1
				k = k + 1
			}
			if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasGenerated) != 0 {
				_sqlite3ComputeGeneratedColumns(tls, pParse, regNew, pTab)
			}
		}
	}
	if !(isView != 0) {
		/* Do constraint checks. */
		_sqlite3GenerateConstraintChecks(tls, pParse, pTab, aRegIdx, iDataCur, iIdxCur, regNewRowid, regOldRowid, chngKey, uint8(onError), labelContinue, bp+56, aXRef, uintptr(0))
		/* If REPLACE conflict handling may have been used, or if the PK of the
		 ** row is changing, then the GenerateConstraintChecks() above may have
		 ** moved cursor iDataCur. Reseek it. */
		if **(**int32)(__ccgo_up(bp + 56)) != 0 || chngKey != 0 {
			if pPk != 0 {
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iDataCur, labelContinue, regKey, nKey)
			} else {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDataCur, labelContinue, regOldRowid)
			}
		}
		/* Do FK constraint checks. */
		if hasFK != 0 {
			_sqlite3FkCheck(tls, pParse, pTab, regOldRowid, 0, aXRef, int32(chngKey))
		}
		/* Delete the index entries associated with the current record.  */
		_sqlite3GenerateRowIndexDelete(tls, pParse, pTab, iDataCur, iIdxCur, aRegIdx, -int32(1))
		/* We must run the OP_FinishSeek opcode to resolve a prior
		 ** OP_DeferredSeek if there is any possibility that there have been
		 ** no OP_Column opcodes since the OP_DeferredSeek was issued.  But
		 ** we want to avoid the OP_FinishSeek if possible, as running it
		 ** costs CPU cycles. */
		if bFinishSeek != 0 {
			_sqlite3VdbeAddOp1(tls, v, int32(OP_FinishSeek), iDataCur)
		}
		/* If changing the rowid value, or if there are foreign key constraints
		 ** to process, delete the old record. Otherwise, add a noop OP_Delete
		 ** to invoke the pre-update hook.
		 **
		 ** That (regNew==regnewRowid+1) is true is also important for the
		 ** pre-update hook. If the caller invokes preupdate_new(), the returned
		 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
		 ** is the column index supplied by the user.
		 */
		if hasFK > int32(1) || chngKey != 0 {
			v1 = 0
		} else {
			v1 = int32(OPFLAG_ISNOOP)
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Delete), iDataCur, int32(OPFLAG_ISUPDATE)|v1, regNewRowid)
		if eOnePass == int32(ONEPASS_MULTI) {
			_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_SAVEPOSITION))
		}
		if !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) {
			_sqlite3VdbeAppendP4(tls, v, pTab, -int32(5))
		}
		if hasFK != 0 {
			_sqlite3FkCheck(tls, pParse, pTab, 0, regNewRowid, aXRef, int32(chngKey))
		}
		/* Insert the new index entries and the new record. */
		if eOnePass == int32(ONEPASS_MULTI) {
			v1 = int32(OPFLAG_SAVEPOSITION)
		} else {
			v1 = 0
		}
		_sqlite3CompleteInsertion(tls, pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx, int32(OPFLAG_ISUPDATE)|v1, 0, 0)
		/* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
		 ** handle rows (possibly in other tables) that refer via a foreign key
		 ** to the row just updated. */
		if hasFK != 0 {
			_sqlite3FkActions(tls, pParse, pTab, pChanges, regOldRowid, aXRef, int32(chngKey))
		}
	}
	/* Increment the row counter
	 */
	if regRowCount != 0 {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), regRowCount, int32(1))
	}
	if pTrigger != 0 {
		_sqlite3CodeRowTrigger(tls, pParse, pTrigger, int32(TK_UPDATE), pChanges, int32(TRIGGER_AFTER), pTab, regOldRowid, onError, labelContinue)
	}
	/* Repeat the above with the next record to be updated, until
	 ** all record selected by the WHERE clause have been updated.
	 */
	if eOnePass == int32(ONEPASS_SINGLE) {
		/* Nothing to do at end-of-loop for a single-pass */
	} else {
		if eOnePass == int32(ONEPASS_MULTI) {
			_sqlite3VdbeResolveLabel(tls, v, labelContinue)
			_sqlite3WhereEnd(tls, pWInfo)
		} else {
			_sqlite3VdbeResolveLabel(tls, v, labelContinue)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iEph, addrTop)
		}
	}
	_sqlite3VdbeResolveLabel(tls, v, labelBreak)
	/* Update the sqlite_sequence table by storing the content of the
	 ** maximum rowid counter values recorded while inserting into
	 ** autoincrement tables.
	 */
	if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 && (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab == uintptr(0) && pUpsert == uintptr(0) {
		_sqlite3AutoincrementEnd(tls, pParse)
	}
	/*
	 ** Return the number of rows that were changed, if we are tracking
	 ** that information.
	 */
	if regRowCount != 0 {
		_sqlite3CodeChangeCount(tls, v, regRowCount, __ccgo_ts+23873)
	}
	goto update_cleanup
update_cleanup:
	;
	_sqlite3AuthContextPop(tls, bp)
	_sqlite3DbFree(tls, db, aXRef) /* Also frees aRegIdx[] and aToOpen[] */
	_sqlite3SrcListDelete(tls, db, pTabList)
	_sqlite3ExprListDelete(tls, db, pChanges)
	_sqlite3ExprDelete(tls, db, pWhere)
	return
}

/* Make sure "isView" and other macros defined above are undefined. Otherwise
** they may interfere with compilation of other functions in this file
** (or in another file, if this file becomes part of the amalgamation).  */

// C documentation
//
//	/*
//	** Analyze the ON CONFLICT clause described by pUpsert.  Resolve all
//	** symbols in the conflict-target.
//	**
//	** Return SQLITE_OK if everything works, or an error code is something
//	** is wrong.
//	*/
func _sqlite3UpsertAnalyzeTarget(tls *libc.TLS, pParse uintptr, pTabList uintptr, pUpsert uintptr, pAll uintptr) (r int32) {
	bp := tls.Alloc(176)
	defer tls.Free(176)
	var iCursor, ii, jj, nClause, nn, rc int32
	var pExpr, pIdx, pTab, pTarget, pTerm, v2 uintptr
	var v3 bool
	var _ /* sCol at bp+36 */ [2]TExpr
	var _ /* sNC at bp+0 */ TNameContext
	var _ /* zWhich at bp+140 */ [16]int8
	_, _, _, _, _, _, _, _, _, _, _, _, _ = iCursor, ii, jj, nClause, nn, pExpr, pIdx, pTab, pTarget, pTerm, rc, v2, v3 /* Index column converted into an Expr */
	nClause = 0                                                                                                         /* Counter of ON CONFLICT clauses */
	/* Resolve all symbolic names in the conflict-target clause, which
	 ** includes both the list of columns and the optional partial-index
	 ** WHERE clause.
	 */
	libc.Xmemset(tls, bp, 0, uint32(36))
	(**(**TNameContext)(__ccgo_up(bp))).FpParse = pParse
	(**(**TNameContext)(__ccgo_up(bp))).FpSrcList = pTabList
	for {
		if !(pUpsert != 0 && (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget != 0) {
			break
		}
		rc = _sqlite3ResolveExprListNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget)
		if rc != 0 {
			return rc
		}
		rc = _sqlite3ResolveExprNames(tls, bp, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTargetWhere)
		if rc != 0 {
			return rc
		}
		/* Check to see if the conflict target matches the rowid. */
		pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpSTab
		pTarget = (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTarget
		iCursor = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor
		if v3 = (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) && (*TExprList)(unsafe.Pointer(pTarget)).FnExpr == int32(1); v3 {
			v2 = (*(*TExprList_item)(unsafe.Pointer(pTarget + 8))).FpExpr
			pTerm = v2
		}
		if v3 && int32((*TExpr)(unsafe.Pointer(v2)).Fop) == int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pTerm)).FiColumn) == -int32(1) {
			/* The conflict-target is the rowid of the primary table */
			goto _1
		}
		/* Initialize sCol[0..1] to be an expression parse tree for a
		 ** single column of an index.  The sCol[0] node will be the TK_COLLATE
		 ** operator and sCol[1] will be the TK_COLUMN operator.  Code below
		 ** will populate the specific collation and column number values
		 ** prior to comparing against the conflict-target expression.
		 */
		libc.Xmemset(tls, bp+36, 0, uint32(104))
		(**(**[2]TExpr)(__ccgo_up(bp + 36)))[0].Fop = uint8(TK_COLLATE)
		(**(**[2]TExpr)(__ccgo_up(bp + 36)))[0].FpLeft = bp + 36 + 1*52
		(**(**[2]TExpr)(__ccgo_up(bp + 36)))[int32(1)].Fop = uint8(TK_COLUMN)
		(**(**[2]TExpr)(__ccgo_up(bp + 36)))[int32(1)].FiTable = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FiCursor
		/* Check for matches against other indexes */
		pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
		for {
			if !(pIdx != 0) {
				break
			}
			if !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != libc.Int32FromInt32(OE_None)) {
				goto _4
			}
			if (*TExprList)(unsafe.Pointer(pTarget)).FnExpr != int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) {
				goto _4
			}
			if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 {
				if (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTargetWhere == uintptr(0) {
					goto _4
				}
				if _sqlite3ExprCompare(tls, pParse, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertTargetWhere, (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere, iCursor) != 0 {
					goto _4
				}
			}
			nn = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
			ii = 0
			for {
				if !(ii < nn) {
					break
				}
				*(*uintptr)(unsafe.Pointer(bp + 36 + 8)) = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(ii)*4))
				if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(ii)*2))) == -int32(2) {
					pExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(ii)*20))).FpExpr
					if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLLATE) {
						(**(**[2]TExpr)(__ccgo_up(bp + 36)))[0].FpLeft = pExpr
						pExpr = bp + 36
					}
				} else {
					(**(**[2]TExpr)(__ccgo_up(bp + 36)))[0].FpLeft = bp + 36 + 1*52
					(**(**[2]TExpr)(__ccgo_up(bp + 36)))[int32(1)].FiColumn = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(ii)*2))
					pExpr = bp + 36
				}
				jj = 0
				for {
					if !(jj < nn) {
						break
					}
					if _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pTarget + 8 + uintptr(jj)*20))).FpExpr, pExpr, iCursor) < int32(2) {
						break /* Column ii of the index matches column jj of target */
					}
					goto _6
				_6:
					;
					jj = jj + 1
				}
				if jj >= nn {
					/* The target contains no match for column jj of the index */
					break
				}
				goto _5
			_5:
				;
				ii = ii + 1
			}
			if ii < nn {
				/* Column ii of the index did not match any term of the conflict target.
				 ** Continue the search with the next index. */
				goto _4
			}
			(*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertIdx = pIdx
			if _sqlite3UpsertOfIndex(tls, pAll, pIdx) != pUpsert {
				/* Really this should be an error.  The isDup ON CONFLICT clause will
				 ** never fire.  But this problem was not discovered until three years
				 ** after multi-CONFLICT upsert was added, and so we silently ignore
				 ** the problem to prevent breaking applications that might actually
				 ** have redundant ON CONFLICT clauses. */
				(*TUpsert)(unsafe.Pointer(pUpsert)).FisDup = uint8(1)
			}
			break
			goto _4
		_4:
			;
			pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		}
		if (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertIdx == uintptr(0) {
			if nClause == 0 && (*TUpsert)(unsafe.Pointer(pUpsert)).FpNextUpsert == uintptr(0) {
				(**(**[16]int8)(__ccgo_up(bp + 140)))[0] = 0
			} else {
				Xsqlite3_snprintf(tls, int32(16), bp+140, __ccgo_ts+23886, libc.VaList(bp+168, nClause+int32(1)))
			}
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23890, libc.VaList(bp+168, bp+140))
			return int32(SQLITE_ERROR)
		}
		goto _1
	_1:
		;
		pUpsert = (*TUpsert)(unsafe.Pointer(pUpsert)).FpNextUpsert
		nClause = nClause + 1
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Generate bytecode that does an UPDATE as part of an upsert.
//	**
//	** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
//	** In this case parameter iCur is a cursor open on the table b-tree that
//	** currently points to the conflicting table row. Otherwise, if pIdx
//	** is not NULL, then pIdx is the constraint that failed and iCur is a
//	** cursor points to the conflicting row.
//	*/
func _sqlite3UpsertDoUpdate(tls *libc.TLS, pParse uintptr, pUpsert uintptr, pTab uintptr, pIdx uintptr, iCur int32) {
	var db, pPk, pSrc, pTop, v uintptr
	var i, iDataCur, iPk, iStorage, k, nPk, regRowid int32
	_, _, _, _, _, _, _, _, _, _, _, _ = db, i, iDataCur, iPk, iStorage, k, nPk, pPk, pSrc, pTop, regRowid, v
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pTop = pUpsert
	iDataCur = (*TUpsert)(unsafe.Pointer(pUpsert)).FiDataCur
	pUpsert = _sqlite3UpsertOfIndex(tls, pTop, pIdx)
	if pIdx != 0 && iCur != iDataCur {
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			regRowid = _sqlite3GetTempReg(tls, pParse)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxRowid), iCur, regRowid)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), iDataCur, 0, regRowid)
			_sqlite3ReleaseTempReg(tls, pParse, regRowid)
		} else {
			pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
			nPk = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)
			iPk = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
			**(**int32)(__ccgo_up(pParse + 48)) += nPk
			i = 0
			for {
				if !(i < nPk) {
					break
				}
				k = _sqlite3TableColumnToIndex(tls, pIdx, int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))))
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iCur, k, iPk+i)
				goto _1
			_1:
				;
				i = i + 1
			}
			i = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), iDataCur, 0, iPk, nPk)
			_sqlite3VdbeAddOp4(tls, v, int32(OP_Halt), int32(SQLITE_CORRUPT), int32(OE_Abort), 0, __ccgo_ts+14943, -int32(1))
			_sqlite3MayAbort(tls, pParse)
			_sqlite3VdbeJumpHere(tls, v, i)
		}
	}
	/* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
	 ** So we have to make a copy before passing it down into sqlite3Update() */
	pSrc = _sqlite3SrcListDup(tls, db, (*TUpsert)(unsafe.Pointer(pTop)).FpUpsertSrc, 0)
	/* excluded.* columns of type REAL need to be converted to a hard real */
	i = 0
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
			break
		}
		if int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*12))).Faffinity) == int32(SQLITE_AFF_REAL) {
			iStorage = (*TUpsert)(unsafe.Pointer(pTop)).FregData + int32(_sqlite3TableColumnToStorage(tls, pTab, int16(i)))
			_sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), iStorage)
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	_sqlite3Update(tls, pParse, pSrc, _sqlite3ExprListDup(tls, db, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertSet, 0), _sqlite3ExprDup(tls, db, (*TUpsert)(unsafe.Pointer(pUpsert)).FpUpsertWhere, 0), int32(OE_Abort), uintptr(0), uintptr(0), pUpsert)
}

/************** End of upsert.c **********************************************/
/************** Begin file vacuum.c ******************************************/
/*
** 2003 April 6
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to implement the VACUUM command.
**
** Most of the code in this file may be omitted by defining the
** SQLITE_OMIT_VACUUM macro.
 */
/* #include "sqliteInt.h" */
/* #include "vdbeInt.h" */

// C documentation
//
//	/*
//	** zIn is a UTF-16 encoded unicode string at least nByte bytes long.
//	** Return the number of bytes in the first nChar unicode characters
//	** in pZ.  nChar must be non-negative.  Surrogate pairs count as a single
//	** character.
//	*/
func _sqlite3Utf16ByteLen(tls *libc.TLS, zIn uintptr, nByte int32, nChar int32) (r int32) {
	var c, n int32
	var z, zEnd uintptr
	_, _, _, _ = c, n, z, zEnd
	z = zIn
	zEnd = z + uintptr(nByte-int32(1))
	n = 0
	if true {
		z = z + 1
	}
	for n < nChar && z <= zEnd {
		c = int32(**(**uint8)(__ccgo_up(z)))
		z = z + uintptr(2)
		if c >= int32(0xd8) && c < int32(0xdc) && z <= zEnd && int32(**(**uint8)(__ccgo_up(z))) >= int32(0xdc) && int32(**(**uint8)(__ccgo_up(z))) < int32(0xe0) {
			z = z + uintptr(2)
		}
		n = n + 1
	}
	return int32(z) - int32(zIn) - libc.BoolInt32(true)
}

// C documentation
//
//	/*
//	** Add a new name/number pair to a VList.  This might require that the
//	** VList object be reallocated, so return the new VList.  If an OOM
//	** error occurs, the original VList returned and the
//	** db->mallocFailed flag is set.
//	**
//	** A VList is really just an array of integers.  To destroy a VList,
//	** simply pass it to sqlite3DbFree().
//	**
//	** The first integer is the number of integers allocated for the whole
//	** VList.  The second integer is the number of integers actually used.
//	** Each name/number pair is encoded by subsequent groups of 3 or more
//	** integers.
//	**
//	** Each name/number pair starts with two integers which are the numeric
//	** value for the pair and the size of the name/number pair, respectively.
//	** The text name overlays one or more following integers.  The text name
//	** is always zero-terminated.
//	**
//	** Conceptually:
//	**
//	**    struct VList {
//	**      int nAlloc;   // Number of allocated slots
//	**      int nUsed;    // Number of used slots
//	**      struct VListEntry {
//	**        int iValue;    // Value for this entry
//	**        int nSlot;     // Slots used by this entry
//	**        // ... variable name goes here
//	**      } a[0];
//	**    }
//	**
//	** During code generation, pointers to the variable names within the
//	** VList are taken.  When that happens, nAlloc is set to zero as an
//	** indication that the VList may never again be enlarged, since the
//	** accompanying realloc() would invalidate the pointers.
//	*/
func _sqlite3VListAdd(tls *libc.TLS, db uintptr, pIn uintptr, zName uintptr, nName int32, iVal int32) (r uintptr) {
	var i, nInt int32
	var nAlloc Tsqlite3_int64
	var pOut, z uintptr
	var v1 int64
	_, _, _, _, _, _ = i, nAlloc, nInt, pOut, z, v1 /* Index in pIn[] where zName is stored */
	nInt = nName/int32(4) + int32(3)
	/* Verify ok to add new elements */
	if pIn == uintptr(0) || **(**TVList)(__ccgo_up(pIn + 1*4))+nInt > **(**TVList)(__ccgo_up(pIn)) {
		if pIn != 0 {
			v1 = int64(2) * int64(**(**TVList)(__ccgo_up(pIn)))
		} else {
			v1 = int64(10)
		}
		/* Enlarge the allocation */
		nAlloc = v1 + int64(nInt)
		pOut = _sqlite3DbRealloc(tls, db, pIn, uint64(nAlloc*int64(4)))
		if pOut == uintptr(0) {
			return pIn
		}
		if pIn == uintptr(0) {
			**(**TVList)(__ccgo_up(pOut + 1*4)) = int32(2)
		}
		pIn = pOut
		**(**TVList)(__ccgo_up(pIn)) = int32(nAlloc)
	}
	i = **(**TVList)(__ccgo_up(pIn + 1*4))
	**(**TVList)(__ccgo_up(pIn + uintptr(i)*4)) = iVal
	**(**TVList)(__ccgo_up(pIn + uintptr(i+int32(1))*4)) = nInt
	z = pIn + uintptr(i+int32(2))*4
	**(**TVList)(__ccgo_up(pIn + 1*4)) = i + nInt
	libc.Xmemcpy(tls, z, zName, uint32(nName))
	**(**int8)(__ccgo_up(z + uintptr(nName))) = 0
	return pIn
}

// C documentation
//
//	/*
//	** Return the number of the variable named zName, if it is in VList.
//	** or return 0 if there is no such variable.
//	*/
func _sqlite3VListNameToNum(tls *libc.TLS, pIn uintptr, zName uintptr, nName int32) (r int32) {
	var i, mx int32
	var z uintptr
	_, _, _ = i, mx, z
	if pIn == uintptr(0) {
		return 0
	}
	mx = **(**TVList)(__ccgo_up(pIn + 1*4))
	i = int32(2)
	for cond := true; cond; cond = i < mx {
		z = pIn + uintptr(i+int32(2))*4
		if libc.Xstrncmp(tls, z, zName, uint32(nName)) == 0 && int32(**(**int8)(__ccgo_up(z + uintptr(nName)))) == 0 {
			return **(**TVList)(__ccgo_up(pIn + uintptr(i)*4))
		}
		i = i + **(**TVList)(__ccgo_up(pIn + uintptr(i+int32(1))*4))
	}
	return 0
}

/************** End of util.c ************************************************/
/************** Begin file hash.c ********************************************/
/*
** 2001 September 22
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This is the implementation of generic hash-tables
** used in SQLite.
 */
/* #include "sqliteInt.h" */
/* #include <assert.h> */

// C documentation
//
//	/*
//	** Print into memory obtained from sqliteMalloc().  Use the internal
//	** %-conversion extensions.
//	*/
func _sqlite3VMPrintf(tls *libc.TLS, db uintptr, zFormat uintptr, ap Tva_list) (r uintptr) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var z uintptr
	var _ /* acc at bp+72 */ TStrAccum
	var _ /* zBase at bp+0 */ [70]int8
	_ = z
	_sqlite3StrAccumInit(tls, bp+72, db, bp, int32(70), **(**int32)(__ccgo_up(db + 120)))
	(**(**TStrAccum)(__ccgo_up(bp + 72))).FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL)
	Xsqlite3_str_vappendf(tls, bp+72, zFormat, ap)
	z = _sqlite3StrAccumFinish(tls, bp+72)
	if int32((**(**TStrAccum)(__ccgo_up(bp + 72))).FaccError) == int32(SQLITE_NOMEM) {
		_sqlite3OomFault(tls, db)
	}
	return z
}

// C documentation
//
//	/*
//	** Add an OP_Function or OP_PureFunc opcode.
//	**
//	** The eCallCtx argument is information (typically taken from Expr.op2)
//	** that describes the calling context of the function.  0 means a general
//	** function call.  NC_IsCheck means called by a check constraint,
//	** NC_IdxExpr means called as part of an index expression.  NC_PartIdx
//	** means in the WHERE clause of a partial index.  NC_GenCol means called
//	** while computing a generated column value.  0 is the usual case.
//	*/
func _sqlite3VdbeAddFunctionCall(tls *libc.TLS, pParse uintptr, p1 int32, p2 int32, p3 int32, nArg int32, pFunc uintptr, eCallCtx int32) (r int32) {
	var addr, v1 int32
	var pCtx, v uintptr
	_, _, _, _ = addr, pCtx, v, v1
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	pCtx = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(libc.UintptrFromInt32(0)+28)+uint32(nArg)*libc.Uint32FromInt64(4)))
	if pCtx == uintptr(0) {
		_freeEphemeralFunction(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pFunc)
		return 0
	}
	(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut = uintptr(0)
	(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpFunc = pFunc
	(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe = uintptr(0)
	(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = 0
	(*Tsqlite3_context)(unsafe.Pointer(pCtx)).Fargc = uint16(nArg)
	(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FiOp = _sqlite3VdbeCurrentAddr(tls, v)
	if eCallCtx != 0 {
		v1 = int32(OP_PureFunc)
	} else {
		v1 = int32(OP_Function)
	}
	addr = _sqlite3VdbeAddOp4(tls, v, v1, p1, p2, p3, pCtx, -int32(16))
	_sqlite3VdbeChangeP5(tls, v, uint16(eCallCtx&int32(NC_SelfRef)))
	_sqlite3MayAbort(tls, pParse)
	return addr
}

func _sqlite3VdbeAddOp3(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32) (r int32) {
	var i int32
	var pOp uintptr
	_, _ = i, pOp
	i = (*TVdbe)(unsafe.Pointer(p)).FnOp
	if (*TVdbe)(unsafe.Pointer(p)).FnOpAlloc <= i {
		return _growOp3(tls, p, op, p1, p2, p3)
	}
	(*TVdbe)(unsafe.Pointer(p)).FnOp = (*TVdbe)(unsafe.Pointer(p)).FnOp + 1
	pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i)*20
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(op)
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp5 = uint16(0)
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = p1
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = p2
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 = p3
	*(*uintptr)(unsafe.Pointer(pOp + 16)) = uintptr(0)
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = P4_NOTUSED
	/* Replicate this logic in sqlite3VdbeAddOp4Int()
	 ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv   */
	/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	 ** Replicate in sqlite3VdbeAddOp4Int() */
	return i
}

func _sqlite3VdbeAddOp4Int(tls *libc.TLS, p uintptr, op int32, p1 int32, p2 int32, p3 int32, p4 int32) (r int32) {
	var i int32
	var pOp uintptr
	_, _ = i, pOp
	i = (*TVdbe)(unsafe.Pointer(p)).FnOp
	if (*TVdbe)(unsafe.Pointer(p)).FnOpAlloc <= i {
		return _addOp4IntSlow(tls, p, op, p1, p2, p3, p4)
	}
	(*TVdbe)(unsafe.Pointer(p)).FnOp = (*TVdbe)(unsafe.Pointer(p)).FnOp + 1
	pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i)*20
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(op)
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp5 = uint16(0)
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = p1
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = p2
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 = p3
	*(*int32)(unsafe.Pointer(pOp + 16)) = p4
	(*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(3))
	/* Replicate this logic in sqlite3VdbeAddOp3()
	 ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv   */
	/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	 ** Replicate in sqlite3VdbeAddOp3() */
	return i
}

// C documentation
//
//	/*
//	** Add a whole list of operations to the operation stack.  Return a
//	** pointer to the first operation inserted.
//	**
//	** Non-zero P2 arguments to jump instructions are automatically adjusted
//	** so that the jump target is relative to the first operation inserted.
//	*/
func _sqlite3VdbeAddOpList(tls *libc.TLS, p uintptr, nOp int32, aOp uintptr, iLineno int32) (r uintptr) {
	var i int32
	var pFirst, pOut, v1 uintptr
	_, _, _, _ = i, pFirst, pOut, v1
	if (*TVdbe)(unsafe.Pointer(p)).FnOp+nOp > (*TVdbe)(unsafe.Pointer(p)).FnOpAlloc && _growOpArray(tls, p, nOp) != 0 {
		return uintptr(0)
	}
	v1 = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp)*20
	pOut = v1
	pFirst = v1
	i = 0
	for {
		if !(i < nOp) {
			break
		}
		(*TVdbeOp)(unsafe.Pointer(pOut)).Fopcode = (*TVdbeOpList)(unsafe.Pointer(aOp)).Fopcode
		(*TVdbeOp)(unsafe.Pointer(pOut)).Fp1 = int32((*TVdbeOpList)(unsafe.Pointer(aOp)).Fp1)
		(*TVdbeOp)(unsafe.Pointer(pOut)).Fp2 = int32((*TVdbeOpList)(unsafe.Pointer(aOp)).Fp2)
		if int32(_sqlite3OpcodeProperty[(*TVdbeOpList)(unsafe.Pointer(aOp)).Fopcode])&int32(OPFLG_JUMP) != 0 && int32((*TVdbeOpList)(unsafe.Pointer(aOp)).Fp2) > 0 {
			**(**int32)(__ccgo_up(pOut + 8)) += (*TVdbe)(unsafe.Pointer(p)).FnOp
		}
		(*TVdbeOp)(unsafe.Pointer(pOut)).Fp3 = int32((*TVdbeOpList)(unsafe.Pointer(aOp)).Fp3)
		(*TVdbeOp)(unsafe.Pointer(pOut)).Fp4type = P4_NOTUSED
		*(*uintptr)(unsafe.Pointer(pOut + 16)) = uintptr(0)
		(*TVdbeOp)(unsafe.Pointer(pOut)).Fp5 = uint16(0)
		_ = iLineno
		goto _2
	_2:
		;
		i = i + 1
		aOp += 4
		pOut += 20
	}
	**(**int32)(__ccgo_up(p + 108)) += nOp
	return pFirst
}

// C documentation
//
//	/*
//	** Allocate sufficient space for an UnpackedRecord structure large enough
//	** to hold a decoded index record for pKeyInfo.
//	**
//	** The space is allocated using sqlite3DbMallocRaw().  If an OOM error
//	** occurs, NULL is returned.
//	*/
func _sqlite3VdbeAllocUnpackedRecord(tls *libc.TLS, pKeyInfo uintptr) (r uintptr) {
	var nByte Tu64
	var p uintptr
	_, _ = nByte, p /* Number of bytes required for *p */
	nByte = uint64((libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)) + uint32(40)*uint32(int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField)+libc.Int32FromInt32(1)))
	p = _sqlite3DbMallocRaw(tls, (*TKeyInfo)(unsafe.Pointer(pKeyInfo)).Fdb, nByte)
	if !(p != 0) {
		return uintptr(0)
	}
	(*TUnpackedRecord)(unsafe.Pointer(p)).FaMem = p + uintptr((libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
	(*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo = pKeyInfo
	(*TUnpackedRecord)(unsafe.Pointer(p)).FnField = uint16(int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField) + int32(1))
	return p
}

// C documentation
//
//	/*
//	** If parameter iOp is less than zero, then invoke the destructor for
//	** all auxiliary data pointers currently cached by the VM passed as
//	** the first argument.
//	**
//	** Or, if iOp is greater than or equal to zero, then the destructor is
//	** only invoked for those auxiliary data pointers created by the user
//	** function invoked by the OP_Function opcode at instruction iOp of
//	** VM pVdbe, and only then if:
//	**
//	**    * the associated function parameter is the 32nd or later (counting
//	**      from left to right), or
//	**
//	**    * the corresponding bit in argument mask is clear (where the first
//	**      function parameter corresponds to bit 0 etc.).
//	*/
func _sqlite3VdbeDeleteAuxData(tls *libc.TLS, db uintptr, pp uintptr, iOp int32, mask int32) {
	var pAux uintptr
	_ = pAux
	for **(**uintptr)(__ccgo_up(pp)) != 0 {
		pAux = **(**uintptr)(__ccgo_up(pp))
		if iOp < 0 || (*TAuxData)(unsafe.Pointer(pAux)).FiAuxOp == iOp && (*TAuxData)(unsafe.Pointer(pAux)).FiAuxArg >= 0 && ((*TAuxData)(unsafe.Pointer(pAux)).FiAuxArg > int32(31) || !(uint32(mask)&(libc.Uint32FromInt32(1)<<(*TAuxData)(unsafe.Pointer(pAux)).FiAuxArg) != 0)) {
			if (*TAuxData)(unsafe.Pointer(pAux)).FxDeleteAux != 0 {
				(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TAuxData)(unsafe.Pointer(pAux)).FxDeleteAux})))(tls, (*TAuxData)(unsafe.Pointer(pAux)).FpAux)
			}
			**(**uintptr)(__ccgo_up(pp)) = (*TAuxData)(unsafe.Pointer(pAux)).FpNextAux
			_sqlite3DbFree(tls, db, pAux)
		} else {
			pp = pAux + 16
		}
	}
}

// C documentation
//
//	/*
//	** If the last opcode is "op" and it is not a jump destination,
//	** then remove it.  Return true if and only if an opcode was removed.
//	*/
func _sqlite3VdbeDeletePriorOpcode(tls *libc.TLS, p uintptr, op Tu8) (r int32) {
	if (*TVdbe)(unsafe.Pointer(p)).FnOp > 0 && int32((**(**TOp)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))*20))).Fopcode) == int32(op) {
		return _sqlite3VdbeChangeToNoop(tls, p, (*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))
	} else {
		return 0
	}
	return r
}

// C documentation
//
//	/*
//	** Compute a string that describes the P4 parameter for an opcode.
//	** Use zTemp for any required temporary buffer space.
//	*/
func _sqlite3VdbeDisplayP4(tls *libc.TLS, db uintptr, pOp uintptr) (r uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var ai, pColl, pColl1, pDef, pDef1, pKeyInfo, pMem, pSig, pVtab, zColl, zP4, v2, v3 uintptr
	var i, n Tu32
	var j, v6 int32
	var _ /* x at bp+0 */ TStrAccum
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = ai, i, j, n, pColl, pColl1, pDef, pDef1, pKeyInfo, pMem, pSig, pVtab, zColl, zP4, v2, v3, v6
	zP4 = uintptr(0)
	_sqlite3StrAccumInit(tls, bp, uintptr(0), uintptr(0), 0, int32(SQLITE_MAX_LENGTH))
	switch int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) {
	case -int32(9):
		pKeyInfo = *(*uintptr)(unsafe.Pointer(pOp + 16))
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6449, libc.VaList(bp+32, int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField)))
		j = 0
		for {
			if !(j < int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField)) {
				break
			}
			pColl = *(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(j)*4))
			if pColl != 0 {
				v2 = (*TCollSeq)(unsafe.Pointer(pColl)).FzName
			} else {
				v2 = __ccgo_ts + 1711
			}
			zColl = v2
			if libc.Xstrcmp(tls, zColl, __ccgo_ts+6454) == 0 {
				zColl = __ccgo_ts + 6461
			}
			if int32(**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(j))))&int32(KEYINFO_ORDER_DESC) != 0 {
				v2 = __ccgo_ts + 6442
			} else {
				v2 = __ccgo_ts + 1711
			}
			if int32(**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(j))))&int32(KEYINFO_ORDER_BIGNULL) != 0 {
				v3 = __ccgo_ts + 6463
			} else {
				v3 = __ccgo_ts + 1711
			}
			Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6466, libc.VaList(bp+32, v2, v3, zColl))
			goto _1
		_1:
			;
			j = j + 1
		}
		Xsqlite3_str_append(tls, bp, __ccgo_ts+6474, int32(1))
	case -int32(2):
		pColl1 = *(*uintptr)(unsafe.Pointer(pOp + 16))
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6490, libc.VaList(bp+32, (*TCollSeq)(unsafe.Pointer(pColl1)).FzName, _encnames[(*TCollSeq)(unsafe.Pointer(pColl1)).Fenc]))
	case -int32(8):
		pDef = *(*uintptr)(unsafe.Pointer(pOp + 16))
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6499, libc.VaList(bp+32, (*TFuncDef)(unsafe.Pointer(pDef)).FzName, int32((*TFuncDef)(unsafe.Pointer(pDef)).FnArg)))
	case -int32(16):
		pDef1 = (*Tsqlite3_context)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpFunc
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6499, libc.VaList(bp+32, (*TFuncDef)(unsafe.Pointer(pDef1)).FzName, int32((*TFuncDef)(unsafe.Pointer(pDef1)).FnArg)))
	case -int32(14):
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1472, libc.VaList(bp+32, **(**Ti64)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pOp + 16))))))
	case -int32(3):
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6506, libc.VaList(bp+32, *(*int32)(unsafe.Pointer(pOp + 16))))
	case -int32(13):
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1439, libc.VaList(bp+32, **(**float64)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pOp + 16))))))
	case -int32(11):
		pMem = *(*uintptr)(unsafe.Pointer(pOp + 16))
		if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Str) != 0 {
			zP4 = (*TMem)(unsafe.Pointer(pMem)).Fz
		} else {
			if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
				Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1472, libc.VaList(bp+32, *(*Ti64)(unsafe.Pointer(pMem))))
			} else {
				if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Real) != 0 {
					Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1439, libc.VaList(bp+32, *(*float64)(unsafe.Pointer(pMem))))
				} else {
					if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Null) != 0 {
						zP4 = __ccgo_ts + 1712
					} else {
						zP4 = __ccgo_ts + 6509
					}
				}
			}
		}
	case -int32(12):
		pVtab = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6516, libc.VaList(bp+32, pVtab))
	case -int32(15):
		ai = *(*uintptr)(unsafe.Pointer(pOp + 16))
		n = **(**Tu32)(__ccgo_up(ai)) /* The first element of an INTARRAY is always the
		 ** count of the number of elements to follow */
		i = uint32(1)
		for {
			if !(i <= n) {
				break
			}
			if i == uint32(1) {
				v6 = int32('[')
			} else {
				v6 = int32(',')
			}
			Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6524, libc.VaList(bp+32, v6, **(**Tu32)(__ccgo_up(ai + uintptr(i)*4))))
			goto _5
		_5:
			;
			i = i + 1
		}
		Xsqlite3_str_append(tls, bp, __ccgo_ts+6529, int32(1))
	case -int32(4):
		zP4 = __ccgo_ts + 6531
	case -int32(5):
		zP4 = (*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FzName
	case -int32(6):
		zP4 = (*TIndex)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FzName
	case -int32(18):
		pSig = *(*uintptr)(unsafe.Pointer(pOp + 16))
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6539, libc.VaList(bp+32, (*TSubrtnSig)(unsafe.Pointer(pSig)).FselId, (*TSubrtnSig)(unsafe.Pointer(pSig)).FzAff))
	default:
		zP4 = *(*uintptr)(unsafe.Pointer(pOp + 16))
	}
	if zP4 != 0 {
		Xsqlite3_str_appendall(tls, bp, zP4)
	}
	if int32((**(**TStrAccum)(__ccgo_up(bp))).FaccError)&int32(SQLITE_NOMEM) != 0 {
		_sqlite3OomFault(tls, db)
	}
	return _sqlite3StrAccumFinish(tls, bp)
}

// C documentation
//
//	/*
//	** Execute as much of a VDBE program as we can.
//	** This is the core of sqlite3_step().
//	*/
func _sqlite3VdbeExec(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(800)
	defer tls.Free(800)
	var aCol, aMem, aOffset, aOp, aPermute, aRoot, apArg, apArg1, db, pArgc, pBt, pBt1, pBt2, pBt3, pC, pC1, pC10, pC11, pC12, pC13, pC14, pC15, pC16, pC17, pC18, pC19, pC2, pC20, pC21, pC22, pC23, pC24, pC25, pC26, pC27, pC28, pC29, pC3, pC30, pC31, pC32, pC33, pC34, pC4, pC5, pC6, pC7, pC8, pC9, pCaller, pColl, pCrsr, pCrsr1, pCrsr2, pCrsr3, pCrsr4, pCrsr5, pCrsr6, pCrsr7, pCrsr8, pCtx, pCtx1, pCtx2, pCur, pCur1, pCur2, pCur3, pCur4, pCur5, pCur6, pCx, pCx1, pCx2, pCx3, pData, pData0, pDb, pDb1, pDb2, pDb3, pDest, pDest1, pDest2, pEnd, pFrame, pFrame1, pFrame2, pFrame3, pFrame4, pIdxKey, pIn, pIn1, pIn2, pIn3, pKey, pKeyInfo, pKeyInfo1, pKeyInfo2, pLast, pMem, pMem1, pMem2, pMem3, pMem4, pModule, pModule1, pModule2, pModule3, pModule4, pModule5, pModule6, pName, pNew, pOp, pOrig, pOut, pPager, pProgram, pQuery, pRec, pReg, pRhs, pRt, pSavepoint, pSrc, pTab, pTab1, pTab2, pTab3, pTabCur, pTmp, pVCur1, pVTab, pVar, pVtab, pVtab1, pVtab2, pVtab3, pVtab4, pVtab5, pVtab6, pVtab7, pX, pX1, pnErr, t1, z1, z2, z3, zAffinity, zAffinity1, zData, zDb, zDb1, zEndHdr, zErr, zFilename, zHdr, zHdr1, zName, zPayload, zSchema, zSql, zTab, zTrace, v191, v194 uintptr
	var affinity int8
	var alreadyExists, bRev, c, c1, c2, cnt, cnt1, desiredAutoCommit, eNew, eOld, eqOnly, exists, i, i1, i2, i4, i5, i6, i7, i8, i9, iCompare, iCookie, iDb, iDb1, iDb2, iDb3, iQuery, iRollback, iSavepoint, iSet, ii, ii1, isLegacy, isSchemaChange, isTransaction, len1, n, n1, n2, n4, nArg, nArg1, nCol, nField, nField1, nField2, nHdr, nKeyCol, nMem, nName, nRoot, nStep, nVarint, oc, opflags, p1, p11, p12, p13, p2, p21, pcDest, pcx, rc, res, res11, res13, res14, res21, savedAnalysisLimit, seekResult, v11, v21, wrFlag, v190, v193 int32
	var colCacheCtr, iAddr, iMap, iPrior, idx, len11, n3, p22, p23, serialType, serial_type, v215, v216 Tu32
	var encoding, isWriteLock, mTrace, op, p5, resetSchemaOnFault, vtabOnConflict, v227 Tu8
	var flags1, flags11, flags2, flags3, flags31, newType, nullFlag, type1, type2, typeMask, v192 Tu16
	var h, h1, iKey1, nAlloc, nData, nProgressLimit, nVmStep, offset64, uu Tu64
	var i3, iA, iB1, iKey, iKey2, nByte, nByte1, nByte2, nCellKey, nZero, sz, v256 Ti64
	var newMax, v213 uint32
	var rA, rB float64
	var xAuth Tsqlite3_xauth
	var v206 int64
	var v217 bool
	var _ /* aRes at bp+616 */ [3]int32
	var _ /* iA at bp+8 */ Ti64
	var _ /* iB at bp+0 */ Ti64
	var _ /* iMeta at bp+88 */ int32
	var _ /* iMeta at bp+92 */ int32
	var _ /* iMoved at bp+504 */ int32
	var _ /* initData at bp+528 */ TInitData
	var _ /* m at bp+464 */ TMem
	var _ /* nChange at bp+512 */ Ti64
	var _ /* nEntry at bp+80 */ Ti64
	var _ /* nErr at bp+556 */ int32
	var _ /* nullFunc at bp+708 */ TFuncDef
	var _ /* pVCur at bp+672 */ uintptr
	var _ /* pgno at bp+520 */ TPgno
	var _ /* r at bp+104 */ TUnpackedRecord
	var _ /* r at bp+144 */ TUnpackedRecord
	var _ /* r at bp+176 */ TUnpackedRecord
	var _ /* r at bp+392 */ TUnpackedRecord
	var _ /* r at bp+432 */ TUnpackedRecord
	var _ /* r at bp+568 */ TUnpackedRecord
	var _ /* res at bp+136 */ int32
	var _ /* res at bp+208 */ int32
	var _ /* res at bp+264 */ int32
	var _ /* res at bp+312 */ int32
	var _ /* res at bp+328 */ int32
	var _ /* res at bp+332 */ int32
	var _ /* res at bp+336 */ int32
	var _ /* res at bp+340 */ int32
	var _ /* res at bp+384 */ int32
	var _ /* res at bp+564 */ int32
	var _ /* res at bp+96 */ int32
	var _ /* rowid at bp+424 */ Ti64
	var _ /* rowid at bp+752 */ Tsqlite_int64
	var _ /* sContext at bp+680 */ Tsqlite3_context
	var _ /* sMem at bp+24 */ TMem
	var _ /* sMem at bp+632 */ TMem
	var _ /* t at bp+64 */ Tu32
	var _ /* uA at bp+16 */ Tu64
	var _ /* v at bp+256 */ Ti64
	var _ /* v at bp+320 */ Ti64
	var _ /* v at bp+72 */ Tu64
	var _ /* val at bp+600 */ Ti64
	var _ /* x at bp+216 */ TMem
	var _ /* x at bp+272 */ TBtreePayload
	var _ /* x at bp+344 */ TBtreePayload
	var _ /* x at bp+608 */ Ti64
	var _ /* z at bp+560 */ uintptr
	var _ /* zErr at bp+524 */ uintptr
	var _ /* zErr at bp+676 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aCol, aMem, aOffset, aOp, aPermute, aRoot, affinity, alreadyExists, apArg, apArg1, bRev, c, c1, c2, cnt, cnt1, colCacheCtr, db, desiredAutoCommit, eNew, eOld, encoding, eqOnly, exists, flags1, flags11, flags2, flags3, flags31, h, h1, i, i1, i2, i3, i4, i5, i6, i7, i8, i9, iA, iAddr, iB1, iCompare, iCookie, iDb, iDb1, iDb2, iDb3, iKey, iKey1, iKey2, iMap, iPrior, iQuery, iRollback, iSavepoint, iSet, idx, ii, ii1, isLegacy, isSchemaChange, isTransaction, isWriteLock, len1, len11, mTrace, n, n1, n2, n3, n4, nAlloc, nArg, nArg1, nByte, nByte1, nByte2, nCellKey, nCol, nData, nField, nField1, nField2, nHdr, nKeyCol, nMem, nName, nProgressLimit, nRoot, nStep, nVarint, nVmStep, nZero, newMax, newType, nullFlag, oc, offset64, op, opflags, p1, p11, p12, p13, p2, p21, p22, p23, p5, pArgc, pBt, pBt1, pBt2, pBt3, pC, pC1, pC10, pC11, pC12, pC13, pC14, pC15, pC16, pC17, pC18, pC19, pC2, pC20, pC21, pC22, pC23, pC24, pC25, pC26, pC27, pC28, pC29, pC3, pC30, pC31, pC32, pC33, pC34, pC4, pC5, pC6, pC7, pC8, pC9, pCaller, pColl, pCrsr, pCrsr1, pCrsr2, pCrsr3, pCrsr4, pCrsr5, pCrsr6, pCrsr7, pCrsr8, pCtx, pCtx1, pCtx2, pCur, pCur1, pCur2, pCur3, pCur4, pCur5, pCur6, pCx, pCx1, pCx2, pCx3, pData, pData0, pDb, pDb1, pDb2, pDb3, pDest, pDest1, pDest2, pEnd, pFrame, pFrame1, pFrame2, pFrame3, pFrame4, pIdxKey, pIn, pIn1, pIn2, pIn3, pKey, pKeyInfo, pKeyInfo1, pKeyInfo2, pLast, pMem, pMem1, pMem2, pMem3, pMem4, pModule, pModule1, pModule2, pModule3, pModule4, pModule5, pModule6, pName, pNew, pOp, pOrig, pOut, pPager, pProgram, pQuery, pRec, pReg, pRhs, pRt, pSavepoint, pSrc, pTab, pTab1, pTab2, pTab3, pTabCur, pTmp, pVCur1, pVTab, pVar, pVtab, pVtab1, pVtab2, pVtab3, pVtab4, pVtab5, pVtab6, pVtab7, pX, pX1, pcDest, pcx, pnErr, rA, rB, rc, res, res11, res13, res14, res21, resetSchemaOnFault, savedAnalysisLimit, seekResult, serialType, serial_type, sz, t1, type1, type2, typeMask, uu, v11, v21, vtabOnConflict, wrFlag, xAuth, z1, z2, z3, zAffinity, zAffinity1, zData, zDb, zDb1, zEndHdr, zErr, zFilename, zHdr, zHdr1, zName, zPayload, zSchema, zSql, zTab, zTrace, v190, v191, v192, v193, v194, v206, v213, v215, v216, v217, v227, v256
	aOp = (*TVdbe)(unsafe.Pointer(p)).FaOp          /* Copy of p->aOp */
	pOp = aOp                                       /* Current operation */
	rc = SQLITE_OK                                  /* Value to return */
	db = (*TVdbe)(unsafe.Pointer(p)).Fdb            /* The database */
	resetSchemaOnFault = uint8(0)                   /* Reset schema after an error if positive */
	encoding = (*Tsqlite3)(unsafe.Pointer(db)).Fenc /* The database encoding */
	iCompare = 0                                    /* Result of last comparison */
	nVmStep = uint64(0)                             /* Invoke xProgress() when nVmStep reaches this */
	aMem = (*TVdbe)(unsafe.Pointer(p)).FaMem        /* Copy of p->aMem */
	pIn1 = uintptr(0)                               /* 1st input operand */
	pIn2 = uintptr(0)                               /* 2nd input operand */
	pIn3 = uintptr(0)                               /* 3rd input operand */
	pOut = uintptr(0)                               /* Output operand */
	colCacheCtr = uint32(0)                         /* Column cache counter */
	/*** INSERT STACK UNION HERE ***/
	/* sqlite3_step() verifies this */
	if (*TVdbe)(unsafe.Pointer(p)).FlockMask != uint32(0) {
		_sqlite3VdbeEnter(tls, p)
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FxProgress != 0 {
		iPrior = **(**Tu32)(__ccgo_up(p + 164 + 4*4))
		nProgressLimit = uint64((*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps - iPrior%(*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps)
	} else {
		nProgressLimit = libc.Uint64FromUint32(0xffffffff) | libc.Uint64FromUint32(0xffffffff)<<libc.Int32FromInt32(32)
	}
	if (*TVdbe)(unsafe.Pointer(p)).Frc == int32(SQLITE_NOMEM) {
		/* This happens if a malloc() inside a call to sqlite3_column_text() or
		 ** sqlite3_column_text16() failed.  */
		goto no_mem
	}
	(*TVdbe)(unsafe.Pointer(p)).Frc = SQLITE_OK
	(*TVdbe)(unsafe.Pointer(p)).FiCurrentTime = 0
	(*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FnBusy = 0
	if libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0 {
		goto abort_due_to_interrupt
	}
	pOp = aOp + uintptr((*TVdbe)(unsafe.Pointer(p)).Fpc)*20
	for {
		if !(int32(1) != 0) {
			break
		}
		/* Errors are detected by individual opcodes, with an immediate
		 ** jumps to abort_due_to_error. */
		nVmStep = nVmStep + 1
		/* Only allow tracing if SQLITE_DEBUG is defined.
		 */
		/* Check to see if we need to simulate an interrupt.  This only happens
		 ** if we have a special test build.
		 */
		/* Sanity checking on other operands */
		switch int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) {
		case int32(OP_Goto):
			goto _2
		case int32(OP_Gosub):
			goto _3
		case int32(OP_Return):
			goto _4
		case int32(OP_InitCoroutine):
			goto _5
		case int32(OP_EndCoroutine):
			goto _6
		case int32(OP_Yield):
			goto _7
		case int32(OP_HaltIfNull):
			goto _8
		case int32(OP_Halt):
			goto _9
		case int32(OP_Integer):
			goto _10
		case int32(OP_Int64):
			goto _11
		case int32(OP_Real):
			goto _12
		case int32(OP_String8):
			goto _13
		case int32(OP_String):
			goto _14
		case int32(OP_Null):
			goto _15
		case int32(OP_BeginSubrtn):
			goto _16
		case int32(OP_SoftNull):
			goto _17
		case int32(OP_Blob):
			goto _18
		case int32(OP_Variable):
			goto _19
		case int32(OP_Move):
			goto _20
		case int32(OP_Copy):
			goto _21
		case int32(OP_SCopy):
			goto _22
		case int32(OP_IntCopy):
			goto _23
		case int32(OP_FkCheck):
			goto _24
		case int32(OP_ResultRow):
			goto _25
		case int32(OP_Concat):
			goto _26
		case int32(OP_Remainder):
			goto _27
		case int32(OP_Divide):
			goto _28
		case int32(OP_Multiply):
			goto _29
		case int32(OP_Subtract):
			goto _30
		case int32(OP_Add):
			goto _31
		case int32(OP_CollSeq):
			goto _32
		case int32(OP_ShiftRight):
			goto _33
		case int32(OP_ShiftLeft):
			goto _34
		case int32(OP_BitOr):
			goto _35
		case int32(OP_BitAnd):
			goto _36
		case int32(OP_AddImm):
			goto _37
		case int32(OP_MustBeInt):
			goto _38
		case int32(OP_RealAffinity):
			goto _39
		case int32(OP_Cast):
			goto _40
		case int32(OP_Ge):
			goto _41
		case int32(OP_Gt):
			goto _42
		case int32(OP_Le):
			goto _43
		case int32(OP_Lt):
			goto _44
		case int32(OP_Ne):
			goto _45
		case int32(OP_Eq):
			goto _46
		case int32(OP_ElseEq):
			goto _47
		case int32(OP_Permutation):
			goto _48
		case int32(OP_Compare):
			goto _49
		case int32(OP_Jump):
			goto _50
		case int32(OP_Or):
			goto _51
		case int32(OP_And):
			goto _52
		case int32(OP_IsTrue):
			goto _53
		case int32(OP_Not):
			goto _54
		case int32(OP_BitNot):
			goto _55
		case int32(OP_Once):
			goto _56
		case int32(OP_If):
			goto _57
		case int32(OP_IfNot):
			goto _58
		case int32(OP_IsNull):
			goto _59
		case int32(OP_IsType):
			goto _60
		case int32(OP_ZeroOrNull):
			goto _61
		case int32(OP_NotNull):
			goto _62
		case int32(OP_IfNullRow):
			goto _63
		case int32(OP_Offset):
			goto _64
		case int32(OP_Column):
			goto _65
		case int32(OP_TypeCheck):
			goto _66
		case int32(OP_Affinity):
			goto _67
		case int32(OP_MakeRecord):
			goto _68
		case int32(OP_Count):
			goto _69
		case OP_Savepoint:
			goto _70
		case int32(OP_AutoCommit):
			goto _71
		case int32(OP_Transaction):
			goto _72
		case int32(OP_ReadCookie):
			goto _73
		case int32(OP_SetCookie):
			goto _74
		case int32(OP_OpenWrite):
			goto _75
		case int32(OP_OpenRead):
			goto _76
		case int32(OP_ReopenIdx):
			goto _77
		case int32(OP_OpenDup):
			goto _78
		case int32(OP_OpenEphemeral):
			goto _79
		case int32(OP_OpenAutoindex):
			goto _80
		case int32(OP_SorterOpen):
			goto _81
		case int32(OP_SequenceTest):
			goto _82
		case int32(OP_OpenPseudo):
			goto _83
		case int32(OP_Close):
			goto _84
		case int32(OP_SeekGT):
			goto _85
		case int32(OP_SeekGE):
			goto _86
		case int32(OP_SeekLE):
			goto _87
		case int32(OP_SeekLT):
			goto _88
		case int32(OP_SeekScan):
			goto _89
		case int32(OP_SeekHit):
			goto _90
		case int32(OP_IfNotOpen):
			goto _91
		case int32(OP_IfNoHope):
			goto _92
		case int32(OP_Found):
			goto _93
		case int32(OP_NotFound):
			goto _94
		case int32(OP_NoConflict):
			goto _95
		case int32(OP_NotExists):
			goto _96
		case int32(OP_SeekRowid):
			goto _97
		case int32(OP_Sequence):
			goto _98
		case int32(OP_NewRowid):
			goto _99
		case int32(OP_Insert):
			goto _100
		case int32(OP_RowCell):
			goto _101
		case int32(OP_Delete):
			goto _102
		case int32(OP_ResetCount):
			goto _103
		case int32(OP_SorterCompare):
			goto _104
		case int32(OP_SorterData):
			goto _105
		case int32(OP_RowData):
			goto _106
		case int32(OP_Rowid):
			goto _107
		case int32(OP_NullRow):
			goto _108
		case int32(OP_Last):
			goto _109
		case int32(OP_SeekEnd):
			goto _110
		case int32(OP_IfSizeBetween):
			goto _111
		case int32(OP_Sort):
			goto _112
		case int32(OP_SorterSort):
			goto _113
		case int32(OP_Rewind):
			goto _114
		case int32(OP_IfEmpty):
			goto _115
		case int32(OP_Prev):
			goto _116
		case int32(OP_Next):
			goto _117
		case int32(OP_SorterNext):
			goto _118
		case int32(OP_IdxInsert):
			goto _119
		case int32(OP_SorterInsert):
			goto _120
		case int32(OP_IdxDelete):
			goto _121
		case int32(OP_IdxRowid):
			goto _122
		case int32(OP_DeferredSeek):
			goto _123
		case int32(OP_FinishSeek):
			goto _124
		case int32(OP_IdxGE):
			goto _125
		case int32(OP_IdxLT):
			goto _126
		case int32(OP_IdxGT):
			goto _127
		case int32(OP_IdxLE):
			goto _128
		case int32(OP_Destroy):
			goto _129
		case int32(OP_Clear):
			goto _130
		case int32(OP_ResetSorter):
			goto _131
		case int32(OP_CreateBtree):
			goto _132
		case int32(OP_SqlExec):
			goto _133
		case int32(OP_ParseSchema):
			goto _134
		case int32(OP_LoadAnalysis):
			goto _135
		case int32(OP_DropTable):
			goto _136
		case int32(OP_DropIndex):
			goto _137
		case int32(OP_DropTrigger):
			goto _138
		case int32(OP_IntegrityCk):
			goto _139
		case int32(OP_IFindKey):
			goto _140
		case int32(OP_RowSetAdd):
			goto _141
		case int32(OP_RowSetRead):
			goto _142
		case int32(OP_RowSetTest):
			goto _143
		case int32(OP_Program):
			goto _144
		case int32(OP_Param):
			goto _145
		case int32(OP_FkCounter):
			goto _146
		case int32(OP_FkIfZero):
			goto _147
		case int32(OP_MemMax):
			goto _148
		case int32(OP_IfPos):
			goto _149
		case int32(OP_OffsetLimit):
			goto _150
		case int32(OP_IfNotZero):
			goto _151
		case int32(OP_DecrJumpZero):
			goto _152
		case int32(OP_AggStep):
			goto _153
		case int32(OP_AggInverse):
			goto _154
		case int32(OP_AggStep1):
			goto _155
		case int32(OP_AggFinal):
			goto _156
		case int32(OP_AggValue):
			goto _157
		case int32(OP_Checkpoint):
			goto _158
		case int32(OP_JournalMode):
			goto _159
		case int32(OP_Vacuum):
			goto _160
		case int32(OP_IncrVacuum):
			goto _161
		case int32(OP_Expire):
			goto _162
		case int32(OP_CursorLock):
			goto _163
		case int32(OP_CursorUnlock):
			goto _164
		case int32(OP_TableLock):
			goto _165
		case int32(OP_VBegin):
			goto _166
		case int32(OP_VCreate):
			goto _167
		case int32(OP_VDestroy):
			goto _168
		case int32(OP_VOpen):
			goto _169
		case int32(OP_VCheck):
			goto _170
		case int32(OP_VInitIn):
			goto _171
		case int32(OP_VFilter):
			goto _172
		case int32(OP_VColumn):
			goto _173
		case int32(OP_VNext):
			goto _174
		case int32(OP_VRename):
			goto _175
		case int32(OP_VUpdate):
			goto _176
		case int32(OP_Pagecount):
			goto _177
		case int32(OP_MaxPgcnt):
			goto _178
		case int32(OP_Function):
			goto _179
		case int32(OP_PureFunc):
			goto _180
		case int32(OP_ClrSubtype):
			goto _181
		case int32(OP_GetSubtype):
			goto _182
		case int32(OP_SetSubtype):
			goto _183
		case int32(OP_FilterAdd):
			goto _184
		case int32(OP_Filter):
			goto _185
		case int32(OP_Init):
			goto _186
		case int32(OP_Trace):
			goto _187
		default:
			goto _188
		}
		goto _189
		/*****************************************************************************
		 ** What follows is a massive switch statement where each case implements a
		 ** separate instruction in the virtual machine.  If we follow the usual
		 ** indentation conventions, each case should be indented by 6 spaces.  But
		 ** that is a lot of wasted space on the left margin.  So the code within
		 ** the switch statement will break with convention and be flush-left. Another
		 ** big comment (similar to this one) will mark the point in the code where
		 ** we transition back to normal indentation.
		 **
		 ** The formatting of each case is important.  The makefile for SQLite
		 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
		 ** file looking for lines that begin with "case OP_".  The opcodes.h files
		 ** will be filled with #defines that give unique integer values to each
		 ** opcode and the opcodes.c file is filled with an array of strings where
		 ** each string is the symbolic name for the corresponding opcode.  If the
		 ** case statement is followed by a comment of the form "/# same as ... #/"
		 ** that comment is used to determine the particular value of the opcode.
		 **
		 ** Other keywords in the comment that follows each case are used to
		 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
		 ** Keywords include: in1, in2, in3, out2, out3.  See
		 ** the mkopcodeh.awk script for additional information.
		 **
		 ** Documentation about VDBE opcodes is generated by scanning this file
		 ** for lines of that contain "Opcode:".  That line and all subsequent
		 ** comment lines are used in the generation of the opcode.html documentation
		 ** file.
		 **
		 ** SUMMARY:
		 **
		 **     Formatting is important to scripts that scan this file.
		 **     Do not deviate from the formatting style currently in use.
		 **
		 *****************************************************************************/
		/* Opcode:  Goto * P2 * * *
		 **
		 ** An unconditional jump to address P2.
		 ** The next instruction executed will be
		 ** the one at index P2 from the beginning of
		 ** the program.
		 **
		 ** The P1 parameter is not actually used by this opcode.  However, it
		 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
		 ** that this Goto is the bottom of a loop and that the lines from P2 down
		 ** to the current line should be indented for EXPLAIN output.
		 */
	_2:
		; /* jump */
		goto jump_to_p2_and_check_for_interrupt
	jump_to_p2_and_check_for_interrupt:
		;
		pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2-int32(1))*20
		/* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
		 ** OP_VNext, or OP_SorterNext) all jump here upon
		 ** completion.  Check to see if sqlite3_interrupt() has been called
		 ** or if the progress callback needs to be invoked.
		 **
		 ** This code uses unstructured "goto" statements and does not look clean.
		 ** But that is not due to sloppy coding habits. The code is written this
		 ** way for performance, to avoid having to run the interrupt and progress
		 ** checks on every opcode.  This helps sqlite3_step() to run about 1.5%
		 ** faster according to "valgrind --tool=cachegrind" */
		goto check_for_interrupt
	check_for_interrupt:
		;
		if libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0 {
			goto abort_due_to_interrupt
		}
		/* Call the progress callback if it is configured and the required number
		 ** of VDBE ops have been executed (either since this invocation of
		 ** sqlite3VdbeExec() or since last time the progress callback was called).
		 ** If the progress callback returns non-zero, exit the virtual machine with
		 ** a return code SQLITE_ABORT.
		 */
		for nVmStep >= nProgressLimit && (*Tsqlite3)(unsafe.Pointer(db)).FxProgress != uintptr(0) {
			nProgressLimit = nProgressLimit + uint64((*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps)
			if (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxProgress})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpProgressArg) != 0 {
				nProgressLimit = libc.Uint64FromUint32(0xffffffff) | libc.Uint64FromUint32(0xffffffff)<<libc.Int32FromInt32(32)
				rc = int32(SQLITE_INTERRUPT)
				goto abort_due_to_error
			}
		}
		goto _189
		/* Opcode:  Gosub P1 P2 * * *
		 **
		 ** Write the current address onto register P1
		 ** and then jump to address P2.
		 */
	_3:
		; /* jump */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		(*TMem)(unsafe.Pointer(pIn1)).Fflags = uint16(MEM_Int)
		*(*Ti64)(unsafe.Pointer(pIn1)) = int64((int32(pOp) - int32(aOp)) / 20)
		goto jump_to_p2_and_check_for_interrupt
		/* Opcode:  Return P1 P2 P3 * *
		 **
		 ** Jump to the address stored in register P1.  If P1 is a return address
		 ** register, then this accomplishes a return from a subroutine.
		 **
		 ** If P3 is 1, then the jump is only taken if register P1 holds an integer
		 ** values, otherwise execution falls through to the next opcode, and the
		 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
		 ** integer or else an assert() is raised.  P3 should be set to 1 when
		 ** this opcode is used in combination with OP_BeginSubrtn, and set to 0
		 ** otherwise.
		 **
		 ** The value in register P1 is unchanged by this opcode.
		 **
		 ** P2 is not used by the byte-code engine.  However, if P2 is positive
		 ** and also less than the current address, then the "EXPLAIN" output
		 ** formatter in the CLI will indent all opcodes from the P2 opcode up
		 ** to be not including the current Return.   P2 should be the first opcode
		 ** in the subroutine from which this opcode is returning.  Thus the P2
		 ** value is a byte-code indentation hint.  See tag-20220407a in
		 ** wherecode.c and shell.c.
		 */
	_4:
		; /* in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Int) != 0 {
			if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 {
			}
			pOp = aOp + uintptr(*(*Ti64)(unsafe.Pointer(pIn1)))*20
		} else {
			if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 {
			}
		}
		goto _189
		/* Opcode: InitCoroutine P1 P2 P3 * *
		 **
		 ** Set up register P1 so that it will Yield to the coroutine
		 ** located at address P3.
		 **
		 ** If P2!=0 then the coroutine implementation immediately follows
		 ** this opcode.  So jump over the coroutine implementation to
		 ** address P2.
		 **
		 ** See also: EndCoroutine
		 */
	_5:
		; /* jump0 */
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		*(*Ti64)(unsafe.Pointer(pOut)) = int64((*TOp)(unsafe.Pointer(pOp)).Fp3 - int32(1))
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Int)
		if (*TOp)(unsafe.Pointer(pOp)).Fp2 == 0 {
			goto _189
		}
		/* Most jump operations do a goto to this spot in order to update
		 ** the pOp pointer. */
		goto jump_to_p2
	jump_to_p2:
		;
		/* There are never any jumps to instruction 0 */
		/* Jumps must be in range */
		pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2-int32(1))*20
		goto _189
		/* Opcode:  EndCoroutine P1 * * * *
		 **
		 ** The instruction at the address in register P1 is a Yield.
		 ** Jump to the P2 parameter of that Yield.
		 ** After the jump, the value register P1 is left with a value
		 ** such that subsequent OP_Yields go back to the this same
		 ** OP_EndCoroutine instruction.
		 **
		 ** See also: InitCoroutine
		 */
	_6:
		;
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pCaller = aOp + uintptr(*(*Ti64)(unsafe.Pointer(pIn1)))*20
		*(*Ti64)(unsafe.Pointer(pIn1)) = int64((int32(pOp)-int32((*TVdbe)(unsafe.Pointer(p)).FaOp))/20 - int32(1))
		pOp = aOp + uintptr((*TVdbeOp)(unsafe.Pointer(pCaller)).Fp2-int32(1))*20
		goto _189
		/* Opcode:  Yield P1 P2 * * *
		 **
		 ** Swap the program counter with the value in register P1.  This
		 ** has the effect of yielding to a coroutine.
		 **
		 ** If the coroutine that is launched by this instruction ends with
		 ** Yield or Return then continue to the next instruction.  But if
		 ** the coroutine launched by this instruction ends with
		 ** EndCoroutine, then jump to P2 rather than continuing with the
		 ** next instruction.
		 **
		 ** See also: InitCoroutine
		 */
	_7:
		;
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		(*TMem)(unsafe.Pointer(pIn1)).Fflags = uint16(MEM_Int)
		pcDest = int32(*(*Ti64)(unsafe.Pointer(pIn1)))
		*(*Ti64)(unsafe.Pointer(pIn1)) = int64((int32(pOp) - int32(aOp)) / 20)
		pOp = aOp + uintptr(pcDest)*20
		goto _189
		/* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
		 ** Synopsis: if r[P3]=null halt
		 **
		 ** Check the value in register P3.  If it is NULL then Halt using
		 ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
		 ** value in register P3 is not NULL, then this routine is a no-op.
		 ** The P5 parameter should be 1.
		 */
	_8:
		; /* in3 */
		pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		if int32((*TMem)(unsafe.Pointer(pIn3)).Fflags)&int32(MEM_Null) == 0 {
			goto _189
		}
		/* Fall through into OP_Halt */
		/* Opcode:  Halt P1 P2 P3 P4 P5
		 **
		 ** Exit immediately.  All open cursors, etc are closed
		 ** automatically.
		 **
		 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
		 ** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
		 ** For errors, it can be some other value.  If P1!=0 then P2 will determine
		 ** whether or not to rollback the current transaction.  Do not rollback
		 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
		 ** then back out all changes that have occurred during this execution of the
		 ** VDBE, but do not rollback the transaction.
		 **
		 ** If P3 is not zero and P4 is NULL, then P3 is a register that holds the
		 ** text of an error message.
		 **
		 ** If P3 is zero and P4 is not null then the error message string is held
		 ** in P4.
		 **
		 ** P5 is a value between 1 and 4, inclusive, then the P4 error message
		 ** string is modified as follows:
		 **
		 **    1:  NOT NULL constraint failed: P4
		 **    2:  UNIQUE constraint failed: P4
		 **    3:  CHECK constraint failed: P4
		 **    4:  FOREIGN KEY constraint failed: P4
		 **
		 ** If P3 is zero and P5 is not zero and P4 is NULL, then everything after
		 ** the ":" is omitted.
		 **
		 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
		 ** every program.  So a jump past the last instruction of the program
		 ** is the same as executing Halt.
		 */
	_9:
		;
		/* A deliberately coded "OP_Halt SQLITE_INTERNAL * * * *" opcode indicates
		 ** something is wrong with the code generator.  Raise an assertion in order
		 ** to bring this to the attention of fuzzers and other testing tools. */
		if (*TVdbe)(unsafe.Pointer(p)).FpFrame != 0 && (*TOp)(unsafe.Pointer(pOp)).Fp1 == SQLITE_OK {
			/* Halt the sub-program. Return control to the parent frame. */
			pFrame = (*TVdbe)(unsafe.Pointer(p)).FpFrame
			(*TVdbe)(unsafe.Pointer(p)).FpFrame = (*TVdbeFrame)(unsafe.Pointer(pFrame)).FpParent
			(*TVdbe)(unsafe.Pointer(p)).FnFrame = (*TVdbe)(unsafe.Pointer(p)).FnFrame - 1
			_sqlite3VdbeSetChanges(tls, db, (*TVdbe)(unsafe.Pointer(p)).FnChange)
			pcx = _sqlite3VdbeFrameRestore(tls, pFrame)
			if (*TOp)(unsafe.Pointer(pOp)).Fp2 == int32(OE_Ignore) {
				/* Instruction pcx is the OP_Program that invoked the sub-program
				 ** currently being halted. If the p2 instruction of this OP_Halt
				 ** instruction is set to OE_Ignore, then the sub-program is throwing
				 ** an IGNORE exception. In this case jump to the address specified
				 ** as the p2 of the calling OP_Program.  */
				pcx = (**(**TOp)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(pcx)*20))).Fp2 - int32(1)
			}
			aOp = (*TVdbe)(unsafe.Pointer(p)).FaOp
			aMem = (*TVdbe)(unsafe.Pointer(p)).FaMem
			pOp = aOp + uintptr(pcx)*20
			goto _189
		}
		(*TVdbe)(unsafe.Pointer(p)).Frc = (*TOp)(unsafe.Pointer(pOp)).Fp1
		(*TVdbe)(unsafe.Pointer(p)).FerrorAction = uint8((*TOp)(unsafe.Pointer(pOp)).Fp2)
		if (*TVdbe)(unsafe.Pointer(p)).Frc != 0 {
			if (*TOp)(unsafe.Pointer(pOp)).Fp3 > 0 && int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == P4_NOTUSED {
				zErr = _sqlite3ValueText(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40, uint8(SQLITE_UTF8))
				_sqlite3VdbeError(tls, p, __ccgo_ts+4729, libc.VaList(bp+768, zErr))
			} else {
				if (*TOp)(unsafe.Pointer(pOp)).Fp5 != 0 {
					_sqlite3VdbeError(tls, p, __ccgo_ts+7073, libc.VaList(bp+768, _azType[int32((*TOp)(unsafe.Pointer(pOp)).Fp5)-int32(1)]))
					if *(*uintptr)(unsafe.Pointer(pOp + 16)) != 0 {
						(*TVdbe)(unsafe.Pointer(p)).FzErrMsg = _sqlite3MPrintf(tls, db, __ccgo_ts+7094, libc.VaList(bp+768, (*TVdbe)(unsafe.Pointer(p)).FzErrMsg, *(*uintptr)(unsafe.Pointer(pOp + 16))))
					}
				} else {
					_sqlite3VdbeError(tls, p, __ccgo_ts+4729, libc.VaList(bp+768, *(*uintptr)(unsafe.Pointer(pOp + 16))))
				}
			}
			_sqlite3VdbeLogAbort(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, pOp, aOp)
		}
		rc = _sqlite3VdbeHalt(tls, p)
		if rc == int32(SQLITE_BUSY) {
			(*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_BUSY)
		} else {
			if (*TVdbe)(unsafe.Pointer(p)).Frc != 0 {
				v190 = int32(SQLITE_ERROR)
			} else {
				v190 = int32(SQLITE_DONE)
			}
			rc = v190
		}
		goto vdbe_return
		/* Opcode: Integer P1 P2 * * *
		 ** Synopsis: r[P2]=P1
		 **
		 ** The 32-bit integer value P1 is written into register P2.
		 */
	_10:
		; /* out2 */
		pOut = _out2Prerelease(tls, p, pOp)
		*(*Ti64)(unsafe.Pointer(pOut)) = int64((*TOp)(unsafe.Pointer(pOp)).Fp1)
		goto _189
		/* Opcode: Int64 * P2 * P4 *
		 ** Synopsis: r[P2]=P4
		 **
		 ** P4 is a pointer to a 64-bit integer value.
		 ** Write that value into register P2.
		 */
	_11:
		; /* out2 */
		pOut = _out2Prerelease(tls, p, pOp)
		*(*Ti64)(unsafe.Pointer(pOut)) = **(**Ti64)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pOp + 16))))
		goto _189
		/* Opcode: Real * P2 * P4 *
		 ** Synopsis: r[P2]=P4
		 **
		 ** P4 is a pointer to a 64-bit floating point value.
		 ** Write that value into register P2.
		 */
	_12:
		; /* same as TK_FLOAT, out2 */
		pOut = _out2Prerelease(tls, p, pOp)
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Real)
		*(*float64)(unsafe.Pointer(pOut)) = **(**float64)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pOp + 16))))
		goto _189
		/* Opcode: String8 * P2 * P4 *
		 ** Synopsis: r[P2]='P4'
		 **
		 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
		 ** into a String opcode before it is executed for the first time.  During
		 ** this transformation, the length of string P4 is computed and stored
		 ** as the P1 parameter.
		 */
	_13:
		; /* same as TK_STRING, out2 */
		pOut = _out2Prerelease(tls, p, pOp)
		(*TOp)(unsafe.Pointer(pOp)).Fp1 = _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(pOp + 16)))
		if int32(encoding) != int32(SQLITE_UTF8) {
			rc = _sqlite3VdbeMemSetStr(tls, pOut, *(*uintptr)(unsafe.Pointer(pOp + 16)), int64(-int32(1)), uint8(SQLITE_UTF8), libc.UintptrFromInt32(0))
			if rc != 0 {
				goto too_big
			}
			if SQLITE_OK != _sqlite3VdbeChangeEncoding(tls, pOut, int32(encoding)) {
				goto no_mem
			}
			(*TMem)(unsafe.Pointer(pOut)).FszMalloc = 0
			v191 = pOut + 16
			*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_Static))
			if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == -int32(7) {
				_sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(pOp + 16)))
			}
			(*TOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(7))
			*(*uintptr)(unsafe.Pointer(pOp + 16)) = (*TMem)(unsafe.Pointer(pOut)).Fz
			(*TOp)(unsafe.Pointer(pOp)).Fp1 = (*TMem)(unsafe.Pointer(pOut)).Fn
		}
		if (*TOp)(unsafe.Pointer(pOp)).Fp1 > **(**int32)(__ccgo_up(db + 120)) {
			goto too_big
		}
		(*TOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_String)
		/* Fall through to the next case, OP_String */
		/* Opcode: String P1 P2 P3 P4 P5
		 ** Synopsis: r[P2]='P4' (len=P1)
		 **
		 ** The string value P4 of length P1 (bytes) is stored in register P2.
		 **
		 ** If P3 is not zero and the content of register P3 is equal to P5, then
		 ** the datatype of the register P2 is converted to BLOB.  The content is
		 ** the same sequence of bytes, it is merely interpreted as a BLOB instead
		 ** of a string, as if it had been CAST.  In other words:
		 **
		 ** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
		 */
	_14:
		; /* out2 */
		pOut = _out2Prerelease(tls, p, pOp)
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Term))
		(*TMem)(unsafe.Pointer(pOut)).Fz = *(*uintptr)(unsafe.Pointer(pOp + 16))
		(*TMem)(unsafe.Pointer(pOut)).Fn = (*TOp)(unsafe.Pointer(pOp)).Fp1
		(*TMem)(unsafe.Pointer(pOut)).Fenc = encoding
		goto _189
		/* Opcode: BeginSubrtn * P2 * * *
		 ** Synopsis: r[P2]=NULL
		 **
		 ** Mark the beginning of a subroutine that can be entered in-line
		 ** or that can be called using OP_Gosub.  The subroutine should
		 ** be terminated by an OP_Return instruction that has a P1 operand that
		 ** is the same as the P2 operand to this opcode and that has P3 set to 1.
		 ** If the subroutine is entered in-line, then the OP_Return will simply
		 ** fall through.  But if the subroutine is entered using OP_Gosub, then
		 ** the OP_Return will jump back to the first instruction after the OP_Gosub.
		 **
		 ** This routine works by loading a NULL into the P2 register.  When the
		 ** return address register contains a NULL, the OP_Return instruction is
		 ** a no-op that simply falls through to the next instruction (assuming that
		 ** the OP_Return opcode has a P3 value of 1).  Thus if the subroutine is
		 ** entered in-line, then the OP_Return will cause in-line execution to
		 ** continue.  But if the subroutine is entered via OP_Gosub, then the
		 ** OP_Return will cause a return to the address following the OP_Gosub.
		 **
		 ** This opcode is identical to OP_Null.  It has a different name
		 ** only to make the byte code easier to read and verify.
		 */
		/* Opcode: Null P1 P2 P3 * *
		 ** Synopsis: r[P2..P3]=NULL
		 **
		 ** Write a NULL into registers P2.  If P3 greater than P2, then also write
		 ** NULL into register P3 and every register in between P2 and P3.  If P3
		 ** is less than P2 (typically P3 is zero) then only register P2 is
		 ** set to NULL.
		 **
		 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
		 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
		 ** OP_Ne or OP_Eq.
		 */
	_16:
		;
	_15:
		;
		pOut = _out2Prerelease(tls, p, pOp)
		cnt = (*TOp)(unsafe.Pointer(pOp)).Fp3 - (*TOp)(unsafe.Pointer(pOp)).Fp2
		if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 {
			v190 = libc.Int32FromInt32(MEM_Null) | libc.Int32FromInt32(MEM_Cleared)
		} else {
			v190 = int32(MEM_Null)
		}
		v192 = uint16(v190)
		nullFlag = v192
		(*TMem)(unsafe.Pointer(pOut)).Fflags = v192
		(*TMem)(unsafe.Pointer(pOut)).Fn = 0
		for cnt > 0 {
			pOut += 40
			_sqlite3VdbeMemSetNull(tls, pOut)
			(*TMem)(unsafe.Pointer(pOut)).Fflags = nullFlag
			(*TMem)(unsafe.Pointer(pOut)).Fn = 0
			cnt = cnt - 1
		}
		goto _189
		/* Opcode: SoftNull P1 * * * *
		 ** Synopsis: r[P1]=NULL
		 **
		 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
		 ** instruction, but do not free any string or blob memory associated with
		 ** the register, so that if the value was a string or blob that was
		 ** previously copied using OP_SCopy, the copies will continue to be valid.
		 */
	_17:
		;
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_Undefined)|libc.Int32FromInt32(MEM_AffMask)) | int32(MEM_Null))
		goto _189
		/* Opcode: Blob P1 P2 * P4 *
		 ** Synopsis: r[P2]=P4 (len=P1)
		 **
		 ** P4 points to a blob of data P1 bytes long.  Store this
		 ** blob in register P2.  If P4 is a NULL pointer, then construct
		 ** a zero-filled blob that is P1 bytes long in P2.
		 */
	_18:
		; /* out2 */
		pOut = _out2Prerelease(tls, p, pOp)
		if *(*uintptr)(unsafe.Pointer(pOp + 16)) == uintptr(0) {
			_sqlite3VdbeMemSetZeroBlob(tls, pOut, (*TOp)(unsafe.Pointer(pOp)).Fp1)
			if _sqlite3VdbeMemExpandBlob(tls, pOut) != 0 {
				goto no_mem
			}
		} else {
			_sqlite3VdbeMemSetStr(tls, pOut, *(*uintptr)(unsafe.Pointer(pOp + 16)), int64((*TOp)(unsafe.Pointer(pOp)).Fp1), uint8(0), uintptr(0))
		}
		(*TMem)(unsafe.Pointer(pOut)).Fenc = encoding
		goto _189
		/* Opcode: Variable P1 P2 * * *
		 ** Synopsis: r[P2]=parameter(P1)
		 **
		 ** Transfer the values of bound parameter P1 into register P2
		 */
	_19:
		; /* Value being transferred */
		pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1-int32(1))*40
		if _sqlite3VdbeMemTooBig(tls, pVar) != 0 {
			goto too_big
		}
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 {
			_sqlite3VdbeMemSetNull(tls, pOut)
		}
		libc.Xmemcpy(tls, pOut, pVar, uint32(libc.UintptrFromInt32(0)+20))
		v191 = pOut + 16
		*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^(libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Ephem)))
		v191 = pOut + 16
		*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | (libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_FromBind)))
		goto _189
		/* Opcode: Move P1 P2 P3 * *
		 ** Synopsis: r[P2@P3]=r[P1@P3]
		 **
		 ** Move the P3 values in register P1..P1+P3-1 over into
		 ** registers P2..P2+P3-1.  Registers P1..P1+P3-1 are
		 ** left holding a NULL.  It is an error for register ranges
		 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.  It is an error
		 ** for P3 to be less than 1.
		 */
	_20:
		; /* Register to copy to */
		n = (*TOp)(unsafe.Pointer(pOp)).Fp3
		p1 = (*TOp)(unsafe.Pointer(pOp)).Fp1
		p2 = (*TOp)(unsafe.Pointer(pOp)).Fp2
		pIn1 = aMem + uintptr(p1)*40
		pOut = aMem + uintptr(p2)*40
		for {
			_sqlite3VdbeMemMove(tls, pOut, pIn1)
			if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Ephem) != 0 && _sqlite3VdbeMemMakeWriteable(tls, pOut) != 0 {
				goto no_mem
			}
			pIn1 += 40
			pOut += 40
			goto _197
		_197:
			;
			n = n - 1
			v190 = n
			if !(v190 != 0) {
				break
			}
		}
		goto _189
		/* Opcode: Copy P1 P2 P3 * P5
		 ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
		 **
		 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
		 **
		 ** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the
		 ** destination.  The 0x0001 bit of P5 indicates that this Copy opcode cannot
		 ** be merged.  The 0x0001 bit is used by the query planner and does not
		 ** come into play during query execution.
		 **
		 ** This instruction makes a deep copy of the value.  A duplicate
		 ** is made of any string or blob constant.  See also OP_SCopy.
		 */
	_21:
		;
		n1 = (*TOp)(unsafe.Pointer(pOp)).Fp3
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		for int32(1) != 0 {
			_sqlite3VdbeMemShallowCopy(tls, pOut, pIn1, int32(MEM_Ephem))
			if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Ephem) != 0 && _sqlite3VdbeMemMakeWriteable(tls, pOut) != 0 {
				goto no_mem
			}
			if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Subtype) != 0 && int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(0x0002) != 0 {
				v191 = pOut + 16
				*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^libc.Int32FromInt32(MEM_Subtype))
			}
			v190 = n1
			n1 = n1 - 1
			if v190 == 0 {
				break
			}
			pOut += 40
			pIn1 += 40
		}
		goto _189
		/* Opcode: SCopy P1 P2 * * *
		 ** Synopsis: r[P2]=r[P1]
		 **
		 ** Make a shallow copy of register P1 into register P2.
		 **
		 ** This instruction makes a shallow copy of the value.  If the value
		 ** is a string or blob, then the copy is only a pointer to the
		 ** original and hence if the original changes so will the copy.
		 ** Worse, if the original is deallocated, the copy becomes invalid.
		 ** Thus the program must guarantee that the original will not change
		 ** during the lifetime of the copy.  Use OP_Copy to make a complete
		 ** copy.
		 */
	_22:
		; /* out2 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		_sqlite3VdbeMemShallowCopy(tls, pOut, pIn1, int32(MEM_Ephem))
		goto _189
		/* Opcode: IntCopy P1 P2 * * *
		 ** Synopsis: r[P2]=r[P1]
		 **
		 ** Transfer the integer value held in register P1 into register P2.
		 **
		 ** This is an optimized version of SCopy that works only for integer
		 ** values.
		 */
	_23:
		; /* out2 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		_sqlite3VdbeMemSetInt64(tls, pOut, *(*Ti64)(unsafe.Pointer(pIn1)))
		goto _189
		/* Opcode: FkCheck * * * * *
		 **
		 ** Halt with an SQLITE_CONSTRAINT error if there are any unresolved
		 ** foreign key constraint violations.  If there are no foreign key
		 ** constraint violations, this is a no-op.
		 **
		 ** FK constraint violations are also checked when the prepared statement
		 ** exits.  This opcode is used to raise foreign key constraint errors prior
		 ** to returning results such as a row change count or the result of a
		 ** RETURNING clause.
		 */
	_24:
		;
		v190 = _sqlite3VdbeCheckFkImmediate(tls, p)
		rc = v190
		if v190 != SQLITE_OK {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: ResultRow P1 P2 * * *
		 ** Synopsis: output=r[P1@P2]
		 **
		 ** The registers P1 through P1+P2-1 contain a single row of
		 ** results. This opcode causes the sqlite3_step() call to terminate
		 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
		 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
		 ** the result row.
		 */
	_25:
		;
		(*TVdbe)(unsafe.Pointer(p)).FcacheCtr = (*TVdbe)(unsafe.Pointer(p)).FcacheCtr + uint32(2) | uint32(1)
		(*TVdbe)(unsafe.Pointer(p)).FpResultRow = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			goto no_mem
		}
		if int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&int32(SQLITE_TRACE_ROW) != 0 {
			(*(*func(*libc.TLS, Tu32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{*(*uintptr)(unsafe.Pointer(&(*Tsqlite3)(unsafe.Pointer(db)).Ftrace))})))(tls, uint32(SQLITE_TRACE_ROW), (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, p, uintptr(0))
		}
		(*TVdbe)(unsafe.Pointer(p)).Fpc = (int32(pOp)-int32(aOp))/20 + int32(1)
		rc = int32(SQLITE_ROW)
		goto vdbe_return
		/* Opcode: Concat P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P2]+r[P1]
		 **
		 ** Add the text in register P1 onto the end of the text in
		 ** register P2 and store the result in register P3.
		 ** If either the P1 or P2 text are NULL then store NULL in P3.
		 **
		 **   P3 = P2 || P1
		 **
		 ** It is illegal for P1 and P3 to be the same register. Sometimes,
		 ** if P3 is the same register as P2, the implementation is able
		 ** to avoid a memcpy().
		 */
	_26:
		; /* Initial flags for P2 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		flags1 = (*TMem)(unsafe.Pointer(pIn1)).Fflags
		if (int32(flags1)|int32((*TMem)(unsafe.Pointer(pIn2)).Fflags))&int32(MEM_Null) != 0 {
			_sqlite3VdbeMemSetNull(tls, pOut)
			goto _189
		}
		if int32(flags1)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) == 0 {
			if _sqlite3VdbeMemStringify(tls, pIn1, encoding, uint8(0)) != 0 {
				goto no_mem
			}
			flags1 = uint16(int32((*TMem)(unsafe.Pointer(pIn1)).Fflags) & ^libc.Int32FromInt32(MEM_Str))
		} else {
			if int32(flags1)&int32(MEM_Zero) != 0 {
				if _sqlite3VdbeMemExpandBlob(tls, pIn1) != 0 {
					goto no_mem
				}
				flags1 = uint16(int32((*TMem)(unsafe.Pointer(pIn1)).Fflags) & ^libc.Int32FromInt32(MEM_Str))
			}
		}
		flags2 = (*TMem)(unsafe.Pointer(pIn2)).Fflags
		if int32(flags2)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) == 0 {
			if _sqlite3VdbeMemStringify(tls, pIn2, encoding, uint8(0)) != 0 {
				goto no_mem
			}
			flags2 = uint16(int32((*TMem)(unsafe.Pointer(pIn2)).Fflags) & ^libc.Int32FromInt32(MEM_Str))
		} else {
			if int32(flags2)&int32(MEM_Zero) != 0 {
				if _sqlite3VdbeMemExpandBlob(tls, pIn2) != 0 {
					goto no_mem
				}
				flags2 = uint16(int32((*TMem)(unsafe.Pointer(pIn2)).Fflags) & ^libc.Int32FromInt32(MEM_Str))
			}
		}
		nByte = int64((*TMem)(unsafe.Pointer(pIn1)).Fn)
		nByte = nByte + int64((*TMem)(unsafe.Pointer(pIn2)).Fn)
		if nByte > int64(**(**int32)(__ccgo_up(db + 120))) {
			goto too_big
		}
		if _sqlite3VdbeMemGrow(tls, pOut, int32(nByte)+int32(2), libc.BoolInt32(pOut == pIn2)) != 0 {
			goto no_mem
		}
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Str))
		if pOut != pIn2 {
			libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pOut)).Fz, (*TMem)(unsafe.Pointer(pIn2)).Fz, uint32((*TMem)(unsafe.Pointer(pIn2)).Fn))
			(*TMem)(unsafe.Pointer(pIn2)).Fflags = flags2
		}
		libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pOut)).Fz+uintptr((*TMem)(unsafe.Pointer(pIn2)).Fn), (*TMem)(unsafe.Pointer(pIn1)).Fz, uint32((*TMem)(unsafe.Pointer(pIn1)).Fn))
		(*TMem)(unsafe.Pointer(pIn1)).Fflags = flags1
		if int32(encoding) > int32(SQLITE_UTF8) {
			nByte = nByte & int64(^libc.Int32FromInt32(1))
		}
		**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pOut)).Fz + uintptr(nByte))) = 0
		**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pOut)).Fz + uintptr(nByte+int64(1)))) = 0
		v191 = pOut + 16
		*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_Term))
		(*TMem)(unsafe.Pointer(pOut)).Fn = int32(nByte)
		(*TMem)(unsafe.Pointer(pOut)).Fenc = encoding
		goto _189
		/* Opcode: Add P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P1]+r[P2]
		 **
		 ** Add the value in register P1 to the value in register P2
		 ** and store the result in register P3.
		 ** If either input is NULL, the result is NULL.
		 */
		/* Opcode: Multiply P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P1]*r[P2]
		 **
		 **
		 ** Multiply the value in register P1 by the value in register P2
		 ** and store the result in register P3.
		 ** If either input is NULL, the result is NULL.
		 */
		/* Opcode: Subtract P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P2]-r[P1]
		 **
		 ** Subtract the value in register P1 from the value in register P2
		 ** and store the result in register P3.
		 ** If either input is NULL, the result is NULL.
		 */
		/* Opcode: Divide P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P2]/r[P1]
		 **
		 ** Divide the value in register P1 by the value in register P2
		 ** and store the result in register P3 (P3=P2/P1). If the value in
		 ** register P1 is zero, then the result is NULL. If either input is
		 ** NULL, the result is NULL.
		 */
		/* Opcode: Remainder P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P2]%r[P1]
		 **
		 ** Compute the remainder after integer register P2 is divided by
		 ** register P1 and store the result in register P3.
		 ** If the value in register P1 is zero the result is NULL.
		 ** If either operand is NULL, the result is NULL.
		 */
	_31:
		; /* same as TK_PLUS, in1, in2, out3 */
	_30:
		; /* same as TK_MINUS, in1, in2, out3 */
	_29:
		; /* same as TK_STAR, in1, in2, out3 */
	_28:
		; /* same as TK_SLASH, in1, in2, out3 */
	_27:
		; /* Real value of right operand */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		type1 = (*TMem)(unsafe.Pointer(pIn1)).Fflags
		pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		type2 = (*TMem)(unsafe.Pointer(pIn2)).Fflags
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		if !(int32(type1)&int32(type2)&int32(MEM_Int) != 0) {
			goto _202
		}
		goto int_math
	int_math:
		;
		iA = *(*Ti64)(unsafe.Pointer(pIn1))
		**(**Ti64)(__ccgo_up(bp)) = *(*Ti64)(unsafe.Pointer(pIn2))
		switch int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) {
		case int32(OP_Add):
			if _sqlite3AddInt64(tls, bp, iA) != 0 {
				goto fp_math
			}
		case int32(OP_Subtract):
			if _sqlite3SubInt64(tls, bp, iA) != 0 {
				goto fp_math
			}
		case int32(OP_Multiply):
			if _sqlite3MulInt64(tls, bp, iA) != 0 {
				goto fp_math
			}
		case int32(OP_Divide):
			if iA == 0 {
				goto arithmetic_result_is_null
			}
			if iA == int64(-int32(1)) && **(**Ti64)(__ccgo_up(bp)) == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)) {
				goto fp_math
			}
			**(**Ti64)(__ccgo_up(bp)) = **(**Ti64)(__ccgo_up(bp)) / iA
		default:
			if iA == 0 {
				goto arithmetic_result_is_null
			}
			if iA == int64(-int32(1)) {
				iA = int64(1)
			}
			**(**Ti64)(__ccgo_up(bp)) = **(**Ti64)(__ccgo_up(bp)) % iA
			break
		}
		*(*Ti64)(unsafe.Pointer(pOut)) = **(**Ti64)(__ccgo_up(bp))
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Int))
		goto _203
	_202:
		;
		if !((int32(type1)|int32(type2))&int32(MEM_Null) != 0) {
			goto _204
		}
		goto arithmetic_result_is_null
		goto _205
	_204:
		;
		type1 = _numericType(tls, pIn1)
		type2 = _numericType(tls, pIn2)
		if int32(type1)&int32(type2)&int32(MEM_Int) != 0 {
			goto int_math
		}
		goto fp_math
	fp_math:
		;
		rA = _sqlite3VdbeRealValue(tls, pIn1)
		rB = _sqlite3VdbeRealValue(tls, pIn2)
		switch int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) {
		case int32(OP_Add):
			rB = rB + rA
		case int32(OP_Subtract):
			rB = rB - rA
		case int32(OP_Multiply):
			rB = rB * rA
		case int32(OP_Divide):
			/* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
			if rA == libc.Float64FromInt32(0) {
				goto arithmetic_result_is_null
			}
			rB = rB / rA
		default:
			iA = _sqlite3VdbeIntValue(tls, pIn1)
			**(**Ti64)(__ccgo_up(bp)) = _sqlite3VdbeIntValue(tls, pIn2)
			if iA == 0 {
				goto arithmetic_result_is_null
			}
			if iA == int64(-int32(1)) {
				iA = int64(1)
			}
			rB = float64(**(**Ti64)(__ccgo_up(bp)) % iA)
			break
		}
		if _sqlite3IsNaN(tls, rB) != 0 {
			goto arithmetic_result_is_null
		}
		*(*float64)(unsafe.Pointer(pOut)) = rB
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Real))
	_205:
		;
	_203:
		;
		goto _189
		goto arithmetic_result_is_null
	arithmetic_result_is_null:
		;
		_sqlite3VdbeMemSetNull(tls, pOut)
		goto _189
		/* Opcode: CollSeq P1 * * P4
		 **
		 ** P4 is a pointer to a CollSeq object. If the next call to a user function
		 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
		 ** be returned. This is used by the built-in min(), max() and nullif()
		 ** functions.
		 **
		 ** If P1 is not zero, then it is a register that a subsequent min() or
		 ** max() aggregate will set to 1 if the current row is not the minimum or
		 ** maximum.  The P1 register is initialized to 0 by this instruction.
		 **
		 ** The interface used by the implementation of the aforementioned functions
		 ** to retrieve the collation sequence set by this opcode is not available
		 ** publicly.  Only built-in functions have access to this feature.
		 */
	_32:
		;
		if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 {
			_sqlite3VdbeMemSetInt64(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40, 0)
		}
		goto _189
		/* Opcode: BitAnd P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P1]&r[P2]
		 **
		 ** Take the bit-wise AND of the values in register P1 and P2 and
		 ** store the result in register P3.
		 ** If either input is NULL, the result is NULL.
		 */
		/* Opcode: BitOr P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P1]|r[P2]
		 **
		 ** Take the bit-wise OR of the values in register P1 and P2 and
		 ** store the result in register P3.
		 ** If either input is NULL, the result is NULL.
		 */
		/* Opcode: ShiftLeft P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P2]<<r[P1]
		 **
		 ** Shift the integer value in register P2 to the left by the
		 ** number of bits specified by the integer in register P1.
		 ** Store the result in register P3.
		 ** If either input is NULL, the result is NULL.
		 */
		/* Opcode: ShiftRight P1 P2 P3 * *
		 ** Synopsis: r[P3]=r[P2]>>r[P1]
		 **
		 ** Shift the integer value in register P2 to the right by the
		 ** number of bits specified by the integer in register P1.
		 ** Store the result in register P3.
		 ** If either input is NULL, the result is NULL.
		 */
	_36:
		; /* same as TK_BITAND, in1, in2, out3 */
	_35:
		; /* same as TK_BITOR, in1, in2, out3 */
	_34:
		; /* same as TK_LSHIFT, in1, in2, out3 */
	_33:
		;
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		if (int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)|int32((*TMem)(unsafe.Pointer(pIn2)).Fflags))&int32(MEM_Null) != 0 {
			_sqlite3VdbeMemSetNull(tls, pOut)
			goto _189
		}
		**(**Ti64)(__ccgo_up(bp + 8)) = _sqlite3VdbeIntValue(tls, pIn2)
		iB1 = _sqlite3VdbeIntValue(tls, pIn1)
		op = (*TOp)(unsafe.Pointer(pOp)).Fopcode
		if int32(op) == int32(OP_BitAnd) {
			**(**Ti64)(__ccgo_up(bp + 8)) = **(**Ti64)(__ccgo_up(bp + 8)) & iB1
		} else {
			if int32(op) == int32(OP_BitOr) {
				**(**Ti64)(__ccgo_up(bp + 8)) = **(**Ti64)(__ccgo_up(bp + 8)) | iB1
			} else {
				if iB1 != 0 {
					/* If shifting by a negative amount, shift in the other direction */
					if iB1 < 0 {
						op = uint8(libc.Int32FromInt32(2)*libc.Int32FromInt32(OP_ShiftLeft) + libc.Int32FromInt32(1) - int32(op))
						if iB1 > int64(-libc.Int32FromInt32(64)) {
							v206 = -iB1
						} else {
							v206 = int64(64)
						}
						iB1 = v206
					}
					if iB1 >= int64(64) {
						if **(**Ti64)(__ccgo_up(bp + 8)) >= 0 || int32(op) == int32(OP_ShiftLeft) {
							v190 = 0
						} else {
							v190 = -int32(1)
						}
						**(**Ti64)(__ccgo_up(bp + 8)) = int64(v190)
					} else {
						libc.Xmemcpy(tls, bp+16, bp+8, uint32(8))
						if int32(op) == int32(OP_ShiftLeft) {
							**(**Tu64)(__ccgo_up(bp + 16)) = **(**Tu64)(__ccgo_up(bp + 16)) << uint64(iB1)
						} else {
							**(**Tu64)(__ccgo_up(bp + 16)) = **(**Tu64)(__ccgo_up(bp + 16)) >> uint64(iB1)
							/* Sign-extend on a right shift of a negative number */
							if **(**Ti64)(__ccgo_up(bp + 8)) < 0 {
								**(**Tu64)(__ccgo_up(bp + 16)) = **(**Tu64)(__ccgo_up(bp + 16)) | (libc.Uint64FromUint32(0xffffffff)<<libc.Int32FromInt32(32)|libc.Uint64FromUint32(0xffffffff))<<(int64(64)-iB1)
							}
						}
						libc.Xmemcpy(tls, bp+8, bp+16, uint32(8))
					}
				}
			}
		}
		*(*Ti64)(unsafe.Pointer(pOut)) = **(**Ti64)(__ccgo_up(bp + 8))
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Int))
		goto _189
		/* Opcode: AddImm  P1 P2 * * *
		 ** Synopsis: r[P1]=r[P1]+P2
		 **
		 ** Add the constant P2 to the value in register P1.
		 ** The result is always an integer.
		 **
		 ** To force any register to be an integer, just add 0.
		 */
	_37:
		; /* in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		_sqlite3VdbeMemIntegerify(tls, pIn1)
		**(**Tu64)(__ccgo_up(pIn1)) += uint64((*TOp)(unsafe.Pointer(pOp)).Fp2)
		goto _189
		/* Opcode: MustBeInt P1 P2 * * *
		 **
		 ** Force the value in register P1 to be an integer.  If the value
		 ** in P1 is not an integer and cannot be converted into an integer
		 ** without data loss, then jump immediately to P2, or if P2==0
		 ** raise an SQLITE_MISMATCH exception.
		 */
	_38:
		; /* jump0, in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Int) == 0 {
			_applyAffinity(tls, pIn1, int8(SQLITE_AFF_NUMERIC), encoding)
			if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Int) == 0 {
				if (*TOp)(unsafe.Pointer(pOp)).Fp2 == 0 {
					rc = int32(SQLITE_MISMATCH)
					goto abort_due_to_error
				} else {
					goto jump_to_p2
				}
			}
		}
		(*TMem)(unsafe.Pointer(pIn1)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pIn1)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Int))
		goto _189
		/* Opcode: RealAffinity P1 * * * *
		 **
		 ** If register P1 holds an integer convert it to a real value.
		 **
		 ** This opcode is used when extracting information from a column that
		 ** has REAL affinity.  Such column values may still be stored as
		 ** integers, for space efficiency, but after extraction we want them
		 ** to have only a real value.
		 */
	_39:
		; /* in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
			_sqlite3VdbeMemRealify(tls, pIn1)
		}
		goto _189
		/* Opcode: Cast P1 P2 * * *
		 ** Synopsis: affinity(r[P1])
		 **
		 ** Force the value in register P1 to be the type defined by P2.
		 **
		 ** <ul>
		 ** <li> P2=='A' &rarr; BLOB
		 ** <li> P2=='B' &rarr; TEXT
		 ** <li> P2=='C' &rarr; NUMERIC
		 ** <li> P2=='D' &rarr; INTEGER
		 ** <li> P2=='E' &rarr; REAL
		 ** </ul>
		 **
		 ** A NULL value is not changed by this routine.  It remains NULL.
		 */
	_40:
		; /* in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Zero) != 0 {
			v190 = _sqlite3VdbeMemExpandBlob(tls, pIn1)
		} else {
			v190 = 0
		}
		rc = v190
		if rc != 0 {
			goto abort_due_to_error
		}
		rc = _sqlite3VdbeMemCast(tls, pIn1, uint8((*TOp)(unsafe.Pointer(pOp)).Fp2), encoding)
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: Eq P1 P2 P3 P4 P5
		 ** Synopsis: IF r[P3]==r[P1]
		 **
		 ** Compare the values in register P1 and P3.  If reg(P3)==reg(P1) then
		 ** jump to address P2.
		 **
		 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
		 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
		 ** to coerce both inputs according to this affinity before the
		 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
		 ** affinity is used. Note that the affinity conversions are stored
		 ** back into the input registers P1 and P3.  So this opcode can cause
		 ** persistent changes to registers P1 and P3.
		 **
		 ** Once any conversions have taken place, and neither value is NULL,
		 ** the values are compared. If both values are blobs then memcmp() is
		 ** used to determine the results of the comparison.  If both values
		 ** are text, then the appropriate collating function specified in
		 ** P4 is used to do the comparison.  If P4 is not specified then
		 ** memcmp() is used to compare text string.  If both values are
		 ** numeric, then a numeric comparison is used. If the two values
		 ** are of different types, then numbers are considered less than
		 ** strings and strings are considered less than blobs.
		 **
		 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
		 ** true or false and is never NULL.  If both operands are NULL then the result
		 ** of comparison is true.  If either operand is NULL then the result is false.
		 ** If neither operand is NULL the result is the same as it would be if
		 ** the SQLITE_NULLEQ flag were omitted from P5.
		 **
		 ** This opcode saves the result of comparison for use by the new
		 ** OP_Jump opcode.
		 */
		/* Opcode: Ne P1 P2 P3 P4 P5
		 ** Synopsis: IF r[P3]!=r[P1]
		 **
		 ** This works just like the Eq opcode except that the jump is taken if
		 ** the operands in registers P1 and P3 are not equal.  See the Eq opcode for
		 ** additional information.
		 */
		/* Opcode: Lt P1 P2 P3 P4 P5
		 ** Synopsis: IF r[P3]<r[P1]
		 **
		 ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
		 ** jump to address P2.
		 **
		 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
		 ** reg(P3) is NULL then the take the jump.  If the SQLITE_JUMPIFNULL
		 ** bit is clear then fall through if either operand is NULL.
		 **
		 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
		 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
		 ** to coerce both inputs according to this affinity before the
		 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
		 ** affinity is used. Note that the affinity conversions are stored
		 ** back into the input registers P1 and P3.  So this opcode can cause
		 ** persistent changes to registers P1 and P3.
		 **
		 ** Once any conversions have taken place, and neither value is NULL,
		 ** the values are compared. If both values are blobs then memcmp() is
		 ** used to determine the results of the comparison.  If both values
		 ** are text, then the appropriate collating function specified in
		 ** P4 is  used to do the comparison.  If P4 is not specified then
		 ** memcmp() is used to compare text string.  If both values are
		 ** numeric, then a numeric comparison is used. If the two values
		 ** are of different types, then numbers are considered less than
		 ** strings and strings are considered less than blobs.
		 **
		 ** This opcode saves the result of comparison for use by the new
		 ** OP_Jump opcode.
		 */
		/* Opcode: Le P1 P2 P3 P4 P5
		 ** Synopsis: IF r[P3]<=r[P1]
		 **
		 ** This works just like the Lt opcode except that the jump is taken if
		 ** the content of register P3 is less than or equal to the content of
		 ** register P1.  See the Lt opcode for additional information.
		 */
		/* Opcode: Gt P1 P2 P3 P4 P5
		 ** Synopsis: IF r[P3]>r[P1]
		 **
		 ** This works just like the Lt opcode except that the jump is taken if
		 ** the content of register P3 is greater than the content of
		 ** register P1.  See the Lt opcode for additional information.
		 */
		/* Opcode: Ge P1 P2 P3 P4 P5
		 ** Synopsis: IF r[P3]>=r[P1]
		 **
		 ** This works just like the Lt opcode except that the jump is taken if
		 ** the content of register P3 is greater than or equal to the content of
		 ** register P1.  See the Lt opcode for additional information.
		 */
	_46:
		; /* same as TK_EQ, jump, in1, in3 */
	_45:
		; /* same as TK_NE, jump, in1, in3 */
	_44:
		; /* same as TK_LT, jump, in1, in3 */
	_43:
		; /* same as TK_LE, jump, in1, in3 */
	_42:
		; /* same as TK_GT, jump, in1, in3 */
	_41:
		; /* Copy of initial value of pIn3->flags */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		flags11 = (*TMem)(unsafe.Pointer(pIn1)).Fflags
		flags3 = (*TMem)(unsafe.Pointer(pIn3)).Fflags
		if int32(flags11)&int32(flags3)&int32(MEM_Int) != 0 {
			/* Common case of comparison of two integers */
			if *(*Ti64)(unsafe.Pointer(pIn3)) > *(*Ti64)(unsafe.Pointer(pIn1)) {
				if **(**uint8)(__ccgo_up(_sqlite3aGTb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode))) != 0 {
					goto jump_to_p2
				}
				iCompare = +libc.Int32FromInt32(1)
			} else {
				if *(*Ti64)(unsafe.Pointer(pIn3)) < *(*Ti64)(unsafe.Pointer(pIn1)) {
					if **(**uint8)(__ccgo_up(_sqlite3aLTb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode))) != 0 {
						goto jump_to_p2
					}
					iCompare = -int32(1)
				} else {
					if **(**uint8)(__ccgo_up(_sqlite3aEQb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode))) != 0 {
						goto jump_to_p2
					}
					iCompare = 0
				}
			}
			goto _189
		}
		if (int32(flags11)|int32(flags3))&int32(MEM_Null) != 0 {
			/* One or both operands are NULL */
			if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(SQLITE_NULLEQ) != 0 {
				/* If SQLITE_NULLEQ is set (which will only happen if the operator is
				 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
				 ** or not both operands are null.
				 */
				if int32(flags11)&int32(flags3)&int32(MEM_Null) != 0 && int32(flags3)&int32(MEM_Cleared) == 0 {
					res = 0 /* Operands are equal */
				} else {
					if int32(flags3)&int32(MEM_Null) != 0 {
						v190 = -int32(1)
					} else {
						v190 = +libc.Int32FromInt32(1)
					}
					res = v190 /* Operands are not equal */
				}
			} else {
				/* SQLITE_NULLEQ is clear and at least one operand is NULL,
				 ** then the result is always NULL.
				 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
				 */
				if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(SQLITE_JUMPIFNULL) != 0 {
					goto jump_to_p2
				}
				iCompare = int32(1) /* Operands are not equal */
				goto _189
			}
		} else {
			/* Neither operand is NULL and we couldn't do the special high-speed
			 ** integer comparison case.  So do a general-case comparison. */
			affinity = int8(int32((*TOp)(unsafe.Pointer(pOp)).Fp5) & int32(SQLITE_AFF_MASK))
			if int32(affinity) >= int32(SQLITE_AFF_NUMERIC) {
				if (int32(flags11)|int32(flags3))&int32(MEM_Str) != 0 {
					if int32(flags11)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_Str)) == int32(MEM_Str) {
						_applyNumericAffinity(tls, pIn1, 0)
						flags3 = (*TMem)(unsafe.Pointer(pIn3)).Fflags
					}
					if int32(flags3)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_Str)) == int32(MEM_Str) {
						_applyNumericAffinity(tls, pIn3, 0)
					}
				}
			} else {
				if int32(affinity) == int32(SQLITE_AFF_TEXT) && (int32(flags11)|int32(flags3))&int32(MEM_Str) != 0 {
					if int32(flags11)&int32(MEM_Str) != 0 {
						v191 = pIn1 + 16
						*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal)))
					} else {
						if int32(flags11)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
							_sqlite3VdbeMemStringify(tls, pIn1, encoding, uint8(1))
							flags11 = uint16(int32((*TMem)(unsafe.Pointer(pIn1)).Fflags) & ^libc.Int32FromInt32(MEM_TypeMask) | int32(flags11)&int32(MEM_TypeMask))
							if pIn1 == pIn3 {
								flags3 = uint16(int32(flags11) | int32(MEM_Str))
							}
						}
					}
					if int32(flags3)&int32(MEM_Str) != 0 {
						v191 = pIn3 + 16
						*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal)))
					} else {
						if int32(flags3)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
							_sqlite3VdbeMemStringify(tls, pIn3, encoding, uint8(1))
							flags3 = uint16(int32((*TMem)(unsafe.Pointer(pIn3)).Fflags) & ^libc.Int32FromInt32(MEM_TypeMask) | int32(flags3)&int32(MEM_TypeMask))
						}
					}
				}
			}
			res = _sqlite3MemCompare(tls, pIn3, pIn1, *(*uintptr)(unsafe.Pointer(pOp + 16)))
		}
		/* At this point, res is negative, zero, or positive if reg[P1] is
		 ** less than, equal to, or greater than reg[P3], respectively.  Compute
		 ** the answer to this operator in res2, depending on what the comparison
		 ** operator actually is.  The next block of code depends on the fact
		 ** that the 6 comparison operators are consecutive integers in this
		 ** order:  NE, EQ, GT, LE, LT, GE */
		if res < 0 {
			res21 = int32(**(**uint8)(__ccgo_up(_sqlite3aLTb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode))))
		} else {
			if res == 0 {
				res21 = int32(**(**uint8)(__ccgo_up(_sqlite3aEQb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode))))
			} else {
				res21 = int32(**(**uint8)(__ccgo_up(_sqlite3aGTb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fopcode))))
			}
		}
		iCompare = res
		/* Undo any changes made by applyAffinity() to the input registers. */
		(*TMem)(unsafe.Pointer(pIn3)).Fflags = flags3
		(*TMem)(unsafe.Pointer(pIn1)).Fflags = flags11
		if res21 != 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: ElseEq * P2 * * *
		 **
		 ** This opcode must follow an OP_Lt or OP_Gt comparison operator.  There
		 ** can be zero or more OP_ReleaseReg opcodes intervening, but no other
		 ** opcodes are allowed to occur between this instruction and the previous
		 ** OP_Lt or OP_Gt.
		 **
		 ** If the result of an OP_Eq comparison on the same two operands as
		 ** the prior OP_Lt or OP_Gt would have been true, then jump to P2.  If
		 ** the result of an OP_Eq comparison on the two previous operands
		 ** would have been false or NULL, then fall through.
		 */
	_47:
		; /* same as TK_ESCAPE, jump */
		if iCompare == 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: Permutation * * * P4 *
		 **
		 ** Set the permutation used by the OP_Compare operator in the next
		 ** instruction.  The permutation is stored in the P4 operand.
		 **
		 ** The permutation is only valid for the next opcode which must be
		 ** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5.
		 **
		 ** The first integer in the P4 integer array is the length of the array
		 ** and does not become part of the permutation.
		 */
	_48:
		;
		goto _189
		/* Opcode: Compare P1 P2 P3 P4 P5
		 ** Synopsis: r[P1@P3] <-> r[P2@P3]
		 **
		 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
		 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
		 ** the comparison for use by the next OP_Jump instruct.
		 **
		 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
		 ** determined by the most recent OP_Permutation operator.  If the
		 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
		 ** order.
		 **
		 ** P4 is a KeyInfo structure that defines collating sequences and sort
		 ** orders for the comparison.  The permutation applies to registers
		 ** only.  The KeyInfo elements are used sequentially.
		 **
		 ** The comparison is a sort comparison, so NULLs compare equal,
		 ** NULLs are less than numbers, numbers are less than strings,
		 ** and strings are less than blobs.
		 **
		 ** This opcode must be immediately followed by an OP_Jump opcode.
		 */
	_49:
		; /* The permutation */
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_PERMUTE) == 0 {
			aPermute = uintptr(0)
		} else {
			aPermute = *(*uintptr)(unsafe.Pointer(pOp + uintptr(-libc.Int32FromInt32(1))*20 + 16)) + uintptr(1)*4
		}
		n2 = (*TOp)(unsafe.Pointer(pOp)).Fp3
		pKeyInfo = *(*uintptr)(unsafe.Pointer(pOp + 16))
		p11 = (*TOp)(unsafe.Pointer(pOp)).Fp1
		p21 = (*TOp)(unsafe.Pointer(pOp)).Fp2
		i = 0
		for {
			if !(i < n2) {
				break
			}
			if aPermute != 0 {
				v213 = **(**Tu32)(__ccgo_up(aPermute + uintptr(i)*4))
			} else {
				v213 = uint32(i)
			}
			idx = v213
			pColl = *(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(i)*4))
			bRev = int32(**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(i)))) & int32(KEYINFO_ORDER_DESC)
			iCompare = _sqlite3MemCompare(tls, aMem+uintptr(uint32(p11)+idx)*40, aMem+uintptr(uint32(p21)+idx)*40, pColl)
			if iCompare != 0 {
				if int32(**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(i))))&int32(KEYINFO_ORDER_BIGNULL) != 0 && (int32((**(**TMem)(__ccgo_up(aMem + uintptr(uint32(p11)+idx)*40))).Fflags)&int32(MEM_Null) != 0 || int32((**(**TMem)(__ccgo_up(aMem + uintptr(uint32(p21)+idx)*40))).Fflags)&int32(MEM_Null) != 0) {
					iCompare = -iCompare
				}
				if bRev != 0 {
					iCompare = -iCompare
				}
				break
			}
			goto _212
		_212:
			;
			i = i + 1
		}
		goto _189
		/* Opcode: Jump P1 P2 P3 * *
		 **
		 ** Jump to the instruction at address P1, P2, or P3 depending on whether
		 ** in the most recent OP_Compare instruction the P1 vector was less than,
		 ** equal to, or greater than the P2 vector, respectively.
		 **
		 ** This opcode must immediately follow an OP_Compare opcode.
		 */
	_50:
		; /* jump */
		if iCompare < 0 {
			pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1-int32(1))*20
		} else {
			if iCompare == 0 {
				pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2-int32(1))*20
			} else {
				pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3-int32(1))*20
			}
		}
		goto _189
		/* Opcode: And P1 P2 P3 * *
		 ** Synopsis: r[P3]=(r[P1] && r[P2])
		 **
		 ** Take the logical AND of the values in registers P1 and P2 and
		 ** write the result into register P3.
		 **
		 ** If either P1 or P2 is 0 (false) then the result is 0 even if
		 ** the other input is NULL.  A NULL and true or two NULLs give
		 ** a NULL output.
		 */
		/* Opcode: Or P1 P2 P3 * *
		 ** Synopsis: r[P3]=(r[P1] || r[P2])
		 **
		 ** Take the logical OR of the values in register P1 and P2 and
		 ** store the answer in register P3.
		 **
		 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
		 ** even if the other input is NULL.  A NULL and false or two NULLs
		 ** give a NULL output.
		 */
	_52:
		; /* same as TK_AND, in1, in2, out3 */
	_51:
		; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
		v11 = _sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40, int32(2))
		v21 = _sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40, int32(2))
		if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_And) {
			v11 = int32(_and_logic[v11*int32(3)+v21])
		} else {
			v11 = int32(_or_logic[v11*int32(3)+v21])
		}
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		if v11 == int32(2) {
			(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Null))
		} else {
			*(*Ti64)(unsafe.Pointer(pOut)) = int64(v11)
			(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Int))
		}
		goto _189
		/* Opcode: IsTrue P1 P2 P3 P4 *
		 ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
		 **
		 ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
		 ** IS NOT FALSE operators.
		 **
		 ** Interpret the value in register P1 as a boolean value.  Store that
		 ** boolean (a 0 or 1) in register P2.  Or if the value in register P1 is
		 ** NULL, then the P3 is stored in register P2.  Invert the answer if P4
		 ** is 1.
		 **
		 ** The logic is summarized like this:
		 **
		 ** <ul>
		 ** <li> If P3==0 and P4==0  then  r[P2] := r[P1] IS TRUE
		 ** <li> If P3==1 and P4==1  then  r[P2] := r[P1] IS FALSE
		 ** <li> If P3==0 and P4==1  then  r[P2] := r[P1] IS NOT TRUE
		 ** <li> If P3==1 and P4==0  then  r[P2] := r[P1] IS NOT FALSE
		 ** </ul>
		 */
	_53:
		; /* in1, out2 */
		_sqlite3VdbeMemSetInt64(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40, int64(_sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40, (*TOp)(unsafe.Pointer(pOp)).Fp3)^*(*int32)(unsafe.Pointer(pOp + 16))))
		goto _189
		/* Opcode: Not P1 P2 * * *
		 ** Synopsis: r[P2]= !r[P1]
		 **
		 ** Interpret the value in register P1 as a boolean value.  Store the
		 ** boolean complement in register P2.  If the value in register P1 is
		 ** NULL, then a NULL is stored in P2.
		 */
	_54:
		; /* same as TK_NOT, in1, out2 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) == 0 {
			_sqlite3VdbeMemSetInt64(tls, pOut, libc.BoolInt64(!(_sqlite3VdbeBooleanValue(tls, pIn1, 0) != 0)))
		} else {
			_sqlite3VdbeMemSetNull(tls, pOut)
		}
		goto _189
		/* Opcode: BitNot P1 P2 * * *
		 ** Synopsis: r[P2]= ~r[P1]
		 **
		 ** Interpret the content of register P1 as an integer.  Store the
		 ** ones-complement of the P1 value into register P2.  If P1 holds
		 ** a NULL then store a NULL in P2.
		 */
	_55:
		; /* same as TK_BITNOT, in1, out2 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		_sqlite3VdbeMemSetNull(tls, pOut)
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) == 0 {
			(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Int)
			*(*Ti64)(unsafe.Pointer(pOut)) = ^_sqlite3VdbeIntValue(tls, pIn1)
		}
		goto _189
		/* Opcode: Once P1 P2 P3 * *
		 **
		 ** Fall through to the next instruction the first time this opcode is
		 ** encountered on each invocation of the byte-code program.  Jump to P2
		 ** on the second and all subsequent encounters during the same invocation.
		 **
		 ** Top-level programs determine first invocation by comparing the P1
		 ** operand against the P1 operand on the OP_Init opcode at the beginning
		 ** of the program.  If the P1 values differ, then fall through and make
		 ** the P1 of this opcode equal to the P1 of OP_Init.  If P1 values are
		 ** the same then take the jump.
		 **
		 ** For subprograms, there is a bitmask in the VdbeFrame that determines
		 ** whether or not the jump should be taken.  The bitmask is necessary
		 ** because the self-altering code trick does not work for recursive
		 ** triggers.
		 **
		 ** The P3 operand is not used directly by this opcode.  However P3 is
		 ** used by the code generator as follows:  If this opcode is the start
		 ** of a subroutine and that subroutine uses a Bloom filter, then P3 will
		 ** be the register that holds that Bloom filter.  See tag-202407032019
		 ** in the source code for implementation details.
		 */
	_56:
		; /* Address of this instruction */
		if (*TVdbe)(unsafe.Pointer(p)).FpFrame != 0 {
			iAddr = uint32((int32(pOp) - int32((*TVdbe)(unsafe.Pointer(p)).FaOp)) / 20)
			if int32(**(**Tu8)(__ccgo_up((*TVdbeFrame)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FpFrame)).FaOnce + uintptr(iAddr/uint32(8)))))&(int32(1)<<(iAddr&uint32(7))) != 0 {
				goto jump_to_p2
			}
			v191 = (*TVdbeFrame)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FpFrame)).FaOnce + uintptr(iAddr/uint32(8))
			*(*Tu8)(unsafe.Pointer(v191)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v191))) | libc.Int32FromInt32(1)<<(iAddr&libc.Uint32FromInt32(7)))
		} else {
			if (**(**TOp)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FaOp))).Fp1 == (*TOp)(unsafe.Pointer(pOp)).Fp1 {
				goto jump_to_p2
			}
		}
		(*TOp)(unsafe.Pointer(pOp)).Fp1 = (**(**TOp)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FaOp))).Fp1
		goto _189
		/* Opcode: If P1 P2 P3 * *
		 **
		 ** Jump to P2 if the value in register P1 is true.  The value
		 ** is considered true if it is numeric and non-zero.  If the value
		 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
		 */
	_57:
		;
		c = _sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40, (*TOp)(unsafe.Pointer(pOp)).Fp3)
		if c != 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: IfNot P1 P2 P3 * *
		 **
		 ** Jump to P2 if the value in register P1 is False.  The value
		 ** is considered false if it has a numeric value of zero.  If the value
		 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
		 */
	_58:
		;
		c1 = libc.BoolInt32(!(_sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40, libc.BoolInt32(!((*TOp)(unsafe.Pointer(pOp)).Fp3 != 0))) != 0))
		if c1 != 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: IsNull P1 P2 * * *
		 ** Synopsis: if r[P1]==NULL goto P2
		 **
		 ** Jump to P2 if the value in register P1 is NULL.
		 */
	_59:
		; /* same as TK_ISNULL, jump, in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) != 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: IsType P1 P2 P3 P4 P5
		 ** Synopsis: if typeof(P1.P3) in P5 goto P2
		 **
		 ** Jump to P2 if the type of a column in a btree is one of the types specified
		 ** by the P5 bitmask.
		 **
		 ** P1 is normally a cursor on a btree for which the row decode cache is
		 ** valid through at least column P3.  In other words, there should have been
		 ** a prior OP_Column for column P3 or greater.  If the cursor is not valid,
		 ** then this opcode might give spurious results.
		 ** The the btree row has fewer than P3 columns, then use P4 as the
		 ** datatype.
		 **
		 ** If P1 is -1, then P3 is a register number and the datatype is taken
		 ** from the value in that register.
		 **
		 ** P5 is a bitmask of data types.  SQLITE_INTEGER is the least significant
		 ** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04.
		 ** SQLITE_BLOB is 0x08.  SQLITE_NULL is 0x10.
		 **
		 ** WARNING: This opcode does not reliably distinguish between NULL and REAL
		 ** when P1>=0.  If the database contains a NaN value, this opcode will think
		 ** that the datatype is REAL when it should be NULL.  When P1<0 and the value
		 ** is already stored in register P3, then this opcode does reliably
		 ** distinguish between NULL and REAL.  The problem only arises then P1>=0.
		 **
		 ** Take the jump to address P2 if and only if the datatype of the
		 ** value determined by P1 and P3 corresponds to one of the bits in the
		 ** P5 bitmask.
		 **
		 */
	_60:
		;
		if (*TOp)(unsafe.Pointer(pOp)).Fp1 >= 0 {
			pC = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
			if (*TOp)(unsafe.Pointer(pOp)).Fp3 < int32((*TVdbeCursor)(unsafe.Pointer(pC)).FnHdrParsed) {
				serialType = *(*Tu32)(unsafe.Pointer(pC + 84 + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*4))
				if serialType >= uint32(12) {
					if serialType&uint32(1) != 0 {
						typeMask = uint16(0x04) /* SQLITE_TEXT */
					} else {
						typeMask = uint16(0x08) /* SQLITE_BLOB */
					}
				} else {
					typeMask = uint16(_aMask[serialType])
				}
			} else {
				typeMask = uint16(int32(1) << (*(*int32)(unsafe.Pointer(pOp + 16)) - int32(1)))
			}
		} else {
			typeMask = uint16(int32(1) << (Xsqlite3_value_type(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40) - int32(1)))
		}
		if int32(typeMask)&int32((*TOp)(unsafe.Pointer(pOp)).Fp5) != 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: ZeroOrNull P1 P2 P3 * *
		 ** Synopsis: r[P2] = 0 OR NULL
		 **
		 ** If both registers P1 and P3 are NOT NULL, then store a zero in
		 ** register P2.  If either registers P1 or P3 are NULL then put
		 ** a NULL in register P2.
		 */
	_61:
		; /* in1, in2, out2, in3 */
		if int32((**(**TMem)(__ccgo_up(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40))).Fflags)&int32(MEM_Null) != 0 || int32((**(**TMem)(__ccgo_up(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40))).Fflags)&int32(MEM_Null) != 0 {
			_sqlite3VdbeMemSetNull(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40)
		} else {
			_sqlite3VdbeMemSetInt64(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40, 0)
		}
		goto _189
		/* Opcode: NotNull P1 P2 * * *
		 ** Synopsis: if r[P1]!=NULL goto P2
		 **
		 ** Jump to P2 if the value in register P1 is not NULL.
		 */
	_62:
		; /* same as TK_NOTNULL, jump, in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) == 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: IfNullRow P1 P2 P3 * *
		 ** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
		 **
		 ** Check the cursor P1 to see if it is currently pointing at a NULL row.
		 ** If it is, then set register P3 to NULL and jump immediately to P2.
		 ** If P1 is not on a NULL row, then fall through without making any
		 ** changes.
		 **
		 ** If P1 is not an open cursor, then this opcode is a no-op.
		 */
	_63:
		;
		pC1 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if pC1 != 0 && (*TVdbeCursor)(unsafe.Pointer(pC1)).FnullRow != 0 {
			_sqlite3VdbeMemSetNull(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40)
			goto jump_to_p2
		}
		goto _189
		/* Opcode: Offset P1 P2 P3 * *
		 ** Synopsis: r[P3] = sqlite_offset(P1)
		 **
		 ** Store in register r[P3] the byte offset into the database file that is the
		 ** start of the payload for the record at which that cursor P1 is currently
		 ** pointing.
		 **
		 ** P2 is the column number for the argument to the sqlite_offset() function.
		 ** This opcode does not use P2 itself, but the P2 value is used by the
		 ** code generator.  The P1, P2, and P3 operands to this opcode are the
		 ** same as for OP_Column.
		 **
		 ** This opcode is only available if SQLite is compiled with the
		 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
		 */
	_64:
		; /* The VDBE cursor */
		pC2 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pOut = (*TVdbe)(unsafe.Pointer(p)).FaMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		if pC2 == uintptr(0) || int32((*TVdbeCursor)(unsafe.Pointer(pC2)).FeCurType) != CURTYPE_BTREE {
			_sqlite3VdbeMemSetNull(tls, pOut)
		} else {
			if (*TVdbeCursor)(unsafe.Pointer(pC2)).FdeferredMoveto != 0 {
				rc = _sqlite3VdbeFinishMoveto(tls, pC2)
				if rc != 0 {
					goto abort_due_to_error
				}
			}
			if _sqlite3BtreeEof(tls, *(*uintptr)(unsafe.Pointer(pC2 + 36))) != 0 {
				_sqlite3VdbeMemSetNull(tls, pOut)
			} else {
				_sqlite3VdbeMemSetInt64(tls, pOut, _sqlite3BtreeOffset(tls, *(*uintptr)(unsafe.Pointer(pC2 + 36))))
			}
		}
		goto _189
		/* Opcode: Column P1 P2 P3 P4 P5
		 ** Synopsis: r[P3]=PX cursor P1 column P2
		 **
		 ** Interpret the data that cursor P1 points to as a structure built using
		 ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
		 ** information about the format of the data.)  Extract the P2-th column
		 ** from this record.  If there are less than (P2+1)
		 ** values in the record, extract a NULL.
		 **
		 ** The value extracted is stored in register P3.
		 **
		 ** If the record contains fewer than P2 fields, then extract a NULL.  Or,
		 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
		 ** the result.
		 **
		 ** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed
		 ** to only be used by the length() function or the equivalent.  The content
		 ** of large blobs is not loaded, thus saving CPU cycles.  If the
		 ** OPFLAG_TYPEOFARG bit is set then the result will only be used by the
		 ** typeof() function or the IS NULL or IS NOT NULL operators or the
		 ** equivalent.  In this case, all content loading can be omitted.
		 */
	_65:
		; /* PseudoTable input register */
		pC3 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		p22 = uint32((*TOp)(unsafe.Pointer(pOp)).Fp2)
		goto op_column_restart
	op_column_restart:
		;
		aOffset = (*TVdbeCursor)(unsafe.Pointer(pC3)).FaOffset
		if (*TVdbeCursor)(unsafe.Pointer(pC3)).FcacheStatus != (*TVdbe)(unsafe.Pointer(p)).FcacheCtr { /*OPTIMIZATION-IF-FALSE*/
			if (*TVdbeCursor)(unsafe.Pointer(pC3)).FnullRow != 0 {
				if int32((*TVdbeCursor)(unsafe.Pointer(pC3)).FeCurType) == int32(CURTYPE_PSEUDO) && (*TVdbeCursor)(unsafe.Pointer(pC3)).FseekResult > 0 {
					/* For the special case of as pseudo-cursor, the seekResult field
					 ** identifies the register that holds the record */
					pReg = aMem + uintptr((*TVdbeCursor)(unsafe.Pointer(pC3)).FseekResult)*40
					v215 = uint32((*TMem)(unsafe.Pointer(pReg)).Fn)
					(*TVdbeCursor)(unsafe.Pointer(pC3)).FszRow = v215
					(*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize = v215
					(*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow = (*TMem)(unsafe.Pointer(pReg)).Fz
				} else {
					pDest = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
					_sqlite3VdbeMemSetNull(tls, pDest)
					goto op_column_out
				}
			} else {
				pCrsr = *(*uintptr)(unsafe.Pointer(pC3 + 36))
				if (*TVdbeCursor)(unsafe.Pointer(pC3)).FdeferredMoveto != 0 {
					if v217 = *(*uintptr)(unsafe.Pointer(pC3 + 12)) != 0; v217 {
						v215 = **(**Tu32)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pC3 + 12)) + uintptr(uint32(1)+p22)*4))
						iMap = v215
					}
					if v217 && v215 > uint32(0) {
						pC3 = (*TVdbeCursor)(unsafe.Pointer(pC3)).FpAltCursor
						p22 = iMap - uint32(1)
						goto op_column_restart
					}
					rc = _sqlite3VdbeFinishMoveto(tls, pC3)
					if rc != 0 {
						goto abort_due_to_error
					}
				} else {
					if _sqlite3BtreeCursorHasMoved(tls, pCrsr) != 0 {
						rc = _sqlite3VdbeHandleMovedCursor(tls, pC3)
						if rc != 0 {
							goto abort_due_to_error
						}
						goto op_column_restart
					}
				}
				(*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize = _sqlite3BtreePayloadSize(tls, pCrsr)
				(*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow = _sqlite3BtreePayloadFetch(tls, pCrsr, pC3+76)
				/* Maximum page size is 64KiB */
			}
			(*TVdbeCursor)(unsafe.Pointer(pC3)).FcacheStatus = (*TVdbe)(unsafe.Pointer(p)).FcacheCtr
			v215 = uint32(**(**Tu8)(__ccgo_up((*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow)))
			**(**Tu32)(__ccgo_up(aOffset)) = v215
			if v215 < uint32(0x80) {
				(*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset = uint32(1)
			} else {
				(*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset = uint32(_sqlite3GetVarint32(tls, (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow, aOffset))
			}
			(*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed = uint16(0)
			if (*TVdbeCursor)(unsafe.Pointer(pC3)).FszRow < **(**Tu32)(__ccgo_up(aOffset)) { /*OPTIMIZATION-IF-FALSE*/
				/* pC->aRow does not have to hold the entire row, but it does at least
				 ** need to cover the header of the record.  If pC->aRow does not contain
				 ** the complete header, then set it to zero, forcing the header to be
				 ** dynamically allocated. */
				(*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow = uintptr(0)
				(*TVdbeCursor)(unsafe.Pointer(pC3)).FszRow = uint32(0)
				/* Make sure a corrupt database has not given us an oversize header.
				 ** Do this now to avoid an oversize memory allocation.
				 **
				 ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
				 ** types use so much data space that there can only be 4096 and 32 of
				 ** them, respectively.  So the maximum header length results from a
				 ** 3-byte type for each of the maximum of 32768 columns plus three
				 ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
				 */
				if **(**Tu32)(__ccgo_up(aOffset)) > uint32(98307) || **(**Tu32)(__ccgo_up(aOffset)) > (*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize {
					goto op_column_corrupt
				}
			} else {
				/* This is an optimization.  By skipping over the first few tests
				 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
				 ** measurable performance gain.
				 **
				 ** This branch is taken even if aOffset[0]==0.  Such a record is never
				 ** generated by SQLite, and could be considered corruption, but we
				 ** accept it for historical reasons.  When aOffset[0]==0, the code this
				 ** branch jumps to reads past the end of the record, but never more
				 ** than a few bytes.  Even if the record occurs at the end of the page
				 ** content area, the "page header" comes after the page content and so
				 ** this overread is harmless.  Similar overreads can occur for a corrupt
				 ** database file.
				 */
				zData = (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow
				/* Conditional skipped */
				goto op_column_read_header
			}
		} else {
			if _sqlite3BtreeCursorHasMoved(tls, *(*uintptr)(unsafe.Pointer(pC3 + 36))) != 0 {
				rc = _sqlite3VdbeHandleMovedCursor(tls, pC3)
				if rc != 0 {
					goto abort_due_to_error
				}
				goto op_column_restart
			}
		}
		/* Make sure at least the first p2+1 entries of the header have been
		 ** parsed and valid information is in aOffset[] and pC->aType[].
		 */
		if !(uint32((*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed) <= p22) {
			goto _219
		}
		/* If there is more header available for parsing in the record, try
		 ** to extract additional fields up through the p2+1-th field
		 */
		if !((*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset < **(**Tu32)(__ccgo_up(aOffset))) {
			goto _221
		}
		/* Make sure zData points to enough of the record to cover the header. */
		if (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow == uintptr(0) {
			libc.Xmemset(tls, bp+24, 0, uint32(40))
			rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, *(*uintptr)(unsafe.Pointer(pC3 + 36)), **(**Tu32)(__ccgo_up(aOffset)), bp+24)
			if rc != SQLITE_OK {
				goto abort_due_to_error
			}
			zData = (**(**TMem)(__ccgo_up(bp + 24))).Fz
		} else {
			zData = (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow
		}
		/* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
		goto op_column_read_header
	op_column_read_header:
		;
		i1 = int32((*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed)
		offset64 = uint64(**(**Tu32)(__ccgo_up(aOffset + uintptr(i1)*4)))
		zHdr = zData + uintptr((*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset)
		zEndHdr = zData + uintptr(**(**Tu32)(__ccgo_up(aOffset)))
		for cond := true; cond; cond = uint32(i1) <= p22 && zHdr < zEndHdr {
			v216 = uint32(**(**Tu8)(__ccgo_up(zHdr)))
			**(**Tu32)(__ccgo_up(bp + 64)) = v216
			v215 = v216
			*(*Tu32)(unsafe.Pointer(pC3 + 84 + uintptr(i1)*4)) = v215
			if v215 < uint32(0x80) {
				zHdr = zHdr + 1
				offset64 = offset64 + uint64(_sqlite3VdbeOneByteSerialTypeLen(tls, uint8(**(**Tu32)(__ccgo_up(bp + 64)))))
			} else {
				zHdr = zHdr + uintptr(_sqlite3GetVarint32(tls, zHdr, bp+64))
				*(*Tu32)(unsafe.Pointer(pC3 + 84 + uintptr(i1)*4)) = **(**Tu32)(__ccgo_up(bp + 64))
				offset64 = offset64 + uint64(_sqlite3VdbeSerialTypeLen(tls, **(**Tu32)(__ccgo_up(bp + 64))))
			}
			i1 = i1 + 1
			v190 = i1
			**(**Tu32)(__ccgo_up(aOffset + uintptr(v190)*4)) = uint32(offset64 & libc.Uint64FromUint32(0xffffffff))
		}
		/* The record is corrupt if any of the following are true:
		 ** (1) the bytes of the header extend past the declared header size
		 ** (2) the entire header was used but not all data was used
		 ** (3) the end of the data extends beyond the end of the record.
		 */
		if zHdr >= zEndHdr && (zHdr > zEndHdr || offset64 != uint64((*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize)) || offset64 > uint64((*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize) {
			if **(**Tu32)(__ccgo_up(aOffset)) == uint32(0) {
				i1 = 0
				zHdr = zEndHdr
			} else {
				if (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow == uintptr(0) {
					_sqlite3VdbeMemRelease(tls, bp+24)
				}
				goto op_column_corrupt
			}
		}
		(*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed = uint16(i1)
		(*TVdbeCursor)(unsafe.Pointer(pC3)).FiHdrOffset = uint32(int32(zHdr) - int32(zData))
		if (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow == uintptr(0) {
			_sqlite3VdbeMemRelease(tls, bp+24)
		}
		goto _222
	_221:
		;
		**(**Tu32)(__ccgo_up(bp + 64)) = uint32(0)
	_222:
		;
		/* If after trying to extract new entries from the header, nHdrParsed is
		 ** still not up to p2, that means that the record has fewer than p2
		 ** columns.  So the result will be either the default value or a NULL.
		 */
		if uint32((*TVdbeCursor)(unsafe.Pointer(pC3)).FnHdrParsed) <= p22 {
			pDest = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
			if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == -int32(11) {
				_sqlite3VdbeMemShallowCopy(tls, pDest, *(*uintptr)(unsafe.Pointer(pOp + 16)), int32(MEM_Static))
			} else {
				_sqlite3VdbeMemSetNull(tls, pDest)
			}
			goto op_column_out
		}
		goto _220
	_219:
		;
		**(**Tu32)(__ccgo_up(bp + 64)) = *(*Tu32)(unsafe.Pointer(pC3 + 84 + uintptr(p22)*4))
	_220:
		;
		/* Extract the content for the p2+1-th column.  Control can only
		 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
		 ** all valid.
		 */
		pDest = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		if int32((*TMem)(unsafe.Pointer(pDest)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 {
			_sqlite3VdbeMemSetNull(tls, pDest)
		}
		if (*TVdbeCursor)(unsafe.Pointer(pC3)).FszRow >= **(**Tu32)(__ccgo_up(aOffset + uintptr(p22+uint32(1))*4)) {
			/* This is the common case where the desired content fits on the original
			 ** page - where the content is not on an overflow page */
			zData = (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow + uintptr(**(**Tu32)(__ccgo_up(aOffset + uintptr(p22)*4)))
			if **(**Tu32)(__ccgo_up(bp + 64)) < uint32(12) {
				_sqlite3VdbeSerialGet(tls, zData, **(**Tu32)(__ccgo_up(bp + 64)), pDest)
			} else {
				v190 = int32((**(**Tu32)(__ccgo_up(bp + 64)) - libc.Uint32FromInt32(12)) / libc.Uint32FromInt32(2))
				len1 = v190
				(*TMem)(unsafe.Pointer(pDest)).Fn = v190
				(*TMem)(unsafe.Pointer(pDest)).Fenc = encoding
				if (*TMem)(unsafe.Pointer(pDest)).FszMalloc < len1+int32(2) {
					if len1 > **(**int32)(__ccgo_up(db + 120)) {
						goto too_big
					}
					(*TMem)(unsafe.Pointer(pDest)).Fflags = uint16(MEM_Null)
					if _sqlite3VdbeMemGrow(tls, pDest, len1+int32(2), 0) != 0 {
						goto no_mem
					}
				} else {
					(*TMem)(unsafe.Pointer(pDest)).Fz = (*TMem)(unsafe.Pointer(pDest)).FzMalloc
				}
				libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pDest)).Fz, zData, uint32(len1))
				**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pDest)).Fz + uintptr(len1))) = 0
				**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pDest)).Fz + uintptr(len1+int32(1)))) = 0
				(*TMem)(unsafe.Pointer(pDest)).Fflags = _aFlag1[**(**Tu32)(__ccgo_up(bp + 64))&uint32(1)]
			}
		} else {
			(*TMem)(unsafe.Pointer(pDest)).Fenc = encoding
			/* This branch happens only when content is on overflow pages */
			v227 = uint8(int32((*TOp)(unsafe.Pointer(pOp)).Fp5) & libc.Int32FromInt32(OPFLAG_BYTELENARG))
			p5 = v227
			if int32(v227) != 0 && (int32(p5) == int32(OPFLAG_TYPEOFARG) || **(**Tu32)(__ccgo_up(bp + 64)) >= uint32(12) && (**(**Tu32)(__ccgo_up(bp + 64))&uint32(1) == uint32(0) || int32(p5) == int32(OPFLAG_BYTELENARG))) || _sqlite3VdbeSerialTypeLen(tls, **(**Tu32)(__ccgo_up(bp + 64))) == uint32(0) {
				/* Content is irrelevant for
				 **    1. the typeof() function,
				 **    2. the length(X) function if X is a blob, and
				 **    3. if the content length is zero.
				 ** So we might as well use bogus content rather than reading
				 ** content from disk.
				 **
				 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
				 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
				 ** read more.  Use the global constant sqlite3CtypeMap[] as the array,
				 ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint())
				 ** and it begins with a bunch of zeros.
				 */
				_sqlite3VdbeSerialGet(tls, uintptr(unsafe.Pointer(&_sqlite3CtypeMap)), **(**Tu32)(__ccgo_up(bp + 64)), pDest)
			} else {
				rc = _vdbeColumnFromOverflow(tls, pC3, int32(p22), **(**Tu32)(__ccgo_up(bp + 64)), int64(**(**Tu32)(__ccgo_up(aOffset + uintptr(p22)*4))), (*TVdbe)(unsafe.Pointer(p)).FcacheCtr, colCacheCtr, pDest)
				if rc != 0 {
					if rc == int32(SQLITE_NOMEM) {
						goto no_mem
					}
					if rc == int32(SQLITE_TOOBIG) {
						goto too_big
					}
					goto abort_due_to_error
				}
			}
		}
		goto op_column_out
	op_column_out:
		;
		goto _189
		goto op_column_corrupt
	op_column_corrupt:
		;
		if (**(**TOp)(__ccgo_up(aOp))).Fp3 > 0 {
			pOp = aOp + uintptr((**(**TOp)(__ccgo_up(aOp))).Fp3-int32(1))*20
			goto _189
		} else {
			rc = _sqlite3CorruptError(tls, int32(99872))
			goto abort_due_to_error
		}
		/* Opcode: TypeCheck P1 P2 P3 P4 *
		 ** Synopsis: typecheck(r[P1@P2])
		 **
		 ** Apply affinities to the range of P2 registers beginning with P1.
		 ** Take the affinities from the Table object in P4.  If any value
		 ** cannot be coerced into the correct type, then raise an error.
		 **
		 ** If P3==0, then omit checking of VIRTUAL columns.
		 **
		 ** If P3==1, then omit checking of all generated column, both VIRTUAL
		 ** and STORED.
		 **
		 ** If P3>=2, then only check column number P3-2 in the table (which will
		 ** be a VIRTUAL column) against the value in reg[P1].  In this case,
		 ** P2 will be 1.
		 **
		 ** This opcode is similar to OP_Affinity except that this opcode
		 ** forces the register type to the Table column type.  This is used
		 ** to implement "strict affinity".
		 **
		 ** GENERATED ALWAYS AS ... STATIC columns are only checked if P3
		 ** is zero.  When P3 is non-zero, no type checking occurs for
		 ** static generated columns.  Virtual columns are computed at query time
		 ** and so they are never checked.
		 **
		 ** Preconditions:
		 **
		 ** <ul>
		 ** <li> P2 should be the number of non-virtual columns in the
		 **      table of P4 unless P3>1, in which case P2 will be 1.
		 ** <li> Table P4 is a STRICT table.
		 ** </ul>
		 **
		 ** If any precondition is false, an assertion fault occurs.
		 */
	_66:
		;
		pTab = *(*uintptr)(unsafe.Pointer(pOp + 16))
		aCol = (*TTable)(unsafe.Pointer(pTab)).FaCol
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if (*TOp)(unsafe.Pointer(pOp)).Fp3 < int32(2) {
			i2 = 0
			nCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol)
		} else {
			i2 = (*TOp)(unsafe.Pointer(pOp)).Fp3 - int32(2)
			nCol = i2 + int32(1)
		}
		for {
			if !(i2 < nCol) {
				break
			}
			if int32((**(**TColumn)(__ccgo_up(aCol + uintptr(i2)*12))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 && (*TOp)(unsafe.Pointer(pOp)).Fp3 < int32(2) {
				if int32((**(**TColumn)(__ccgo_up(aCol + uintptr(i2)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
					goto _228
				}
				if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 {
					pIn1 += 40
					goto _228
				}
			}
			_applyAffinity(tls, pIn1, (**(**TColumn)(__ccgo_up(aCol + uintptr(i2)*12))).Faffinity, encoding)
			if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) == 0 {
				switch int32(uint32(*(*uint8)(unsafe.Pointer(aCol + uintptr(i2)*12 + 4)) & 0xf0 >> 4)) {
				case int32(COLTYPE_BLOB):
					if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 {
						goto vdbe_type_error
					}
				case int32(COLTYPE_INTEGER):
					fallthrough
				case int32(COLTYPE_INT):
					if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Int) == 0 {
						goto vdbe_type_error
					}
				case int32(COLTYPE_TEXT):
					if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Str) == 0 {
						goto vdbe_type_error
					}
				case int32(COLTYPE_REAL):
					if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Int) != 0 {
						/* When applying REAL affinity, if the result is still an MEM_Int
						 ** that will fit in 6 bytes, then change the type to MEM_IntReal
						 ** so that we keep the high-resolution integer value but know that
						 ** the type really wants to be REAL. */
						if *(*Ti64)(unsafe.Pointer(pIn1)) <= int64(140737488355327) && *(*Ti64)(unsafe.Pointer(pIn1)) >= -int64(140737488355328) {
							v191 = pIn1 + 16
							*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_IntReal))
							v191 = pIn1 + 16
							*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^libc.Int32FromInt32(MEM_Int))
						} else {
							*(*float64)(unsafe.Pointer(pIn1)) = float64(*(*Ti64)(unsafe.Pointer(pIn1)))
							v191 = pIn1 + 16
							*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_Real))
							v191 = pIn1 + 16
							*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^libc.Int32FromInt32(MEM_Int))
						}
					} else {
						if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&(libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)) == 0 {
							goto vdbe_type_error
						}
					}
				default:
					/* COLTYPE_ANY.  Accept anything. */
					break
				}
			}
			pIn1 += 40
			goto _228
		_228:
			;
			i2 = i2 + 1
		}
		goto _189
		goto vdbe_type_error
	vdbe_type_error:
		;
		_sqlite3VdbeError(tls, p, __ccgo_ts+7101, libc.VaList(bp+768, _vdbeMemTypeName(tls, pIn1), _sqlite3StdType[int32(uint32(*(*uint8)(unsafe.Pointer(aCol + uintptr(i2)*12 + 4))&0xf0>>4))-int32(1)], (*TTable)(unsafe.Pointer(pTab)).FzName, (**(**TColumn)(__ccgo_up(aCol + uintptr(i2)*12))).FzCnName))
		rc = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
		goto abort_due_to_error
		/* Opcode: Affinity P1 P2 * P4 *
		 ** Synopsis: affinity(r[P1@P2])
		 **
		 ** Apply affinities to a range of P2 registers starting with P1.
		 **
		 ** P4 is a string that is P2 characters long. The N-th character of the
		 ** string indicates the column affinity that should be used for the N-th
		 ** memory cell in the range.
		 */
	_67:
		; /* The affinity to be applied */
		zAffinity = *(*uintptr)(unsafe.Pointer(pOp + 16))
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		for int32(1) != 0 {
			_applyAffinity(tls, pIn1, **(**int8)(__ccgo_up(zAffinity)), encoding)
			if int32(**(**int8)(__ccgo_up(zAffinity))) == int32(SQLITE_AFF_REAL) && int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Int) != 0 {
				/* When applying REAL affinity, if the result is still an MEM_Int
				 ** that will fit in 6 bytes, then change the type to MEM_IntReal
				 ** so that we keep the high-resolution integer value but know that
				 ** the type really wants to be REAL. */
				if *(*Ti64)(unsafe.Pointer(pIn1)) <= int64(140737488355327) && *(*Ti64)(unsafe.Pointer(pIn1)) >= -int64(140737488355328) {
					v191 = pIn1 + 16
					*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_IntReal))
					v191 = pIn1 + 16
					*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^libc.Int32FromInt32(MEM_Int))
				} else {
					*(*float64)(unsafe.Pointer(pIn1)) = float64(*(*Ti64)(unsafe.Pointer(pIn1)))
					v191 = pIn1 + 16
					*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_Real))
					v191 = pIn1 + 16
					*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Str)))
				}
			}
			zAffinity = zAffinity + 1
			if int32(**(**int8)(__ccgo_up(zAffinity))) == 0 {
				break
			}
			pIn1 += 40
		}
		goto _189
		/* Opcode: MakeRecord P1 P2 P3 P4 *
		 ** Synopsis: r[P3]=mkrec(r[P1@P2])
		 **
		 ** Convert P2 registers beginning with P1 into the [record format]
		 ** use as a data record in a database table or as a key
		 ** in an index.  The OP_Column opcode can decode the record later.
		 **
		 ** P4 may be a string that is P2 characters long.  The N-th character of the
		 ** string indicates the column affinity that should be used for the N-th
		 ** field of the index key.
		 **
		 ** The mapping from character to affinity is given by the SQLITE_AFF_
		 ** macros defined in sqliteInt.h.
		 **
		 ** If P4 is NULL then all index fields have the affinity BLOB.
		 **
		 ** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM
		 ** compile-time option is enabled:
		 **
		 **   * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index
		 **     of the right-most table that can be null-trimmed.
		 **
		 **   * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value
		 **     OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to
		 **     accept no-change records with serial_type 10.  This value is
		 **     only used inside an assert() and does not affect the end result.
		 */
	_68:
		; /* Where to write next byte of the payload */
		/* Assuming the record contains N fields, the record format looks
		 ** like this:
		 **
		 ** ------------------------------------------------------------------------
		 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
		 ** ------------------------------------------------------------------------
		 **
		 ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
		 ** and so forth.
		 **
		 ** Each type field is a varint representing the serial type of the
		 ** corresponding data element (see sqlite3VdbeSerialType()). The
		 ** hdr-size field is also a varint which is the offset from the beginning
		 ** of the record to data0.
		 */
		nData = uint64(0) /* Number of bytes of data space */
		nHdr = 0          /* Number of bytes of header space */
		nZero = 0         /* Number of zero bytes at the end of the record */
		nField = (*TOp)(unsafe.Pointer(pOp)).Fp1
		zAffinity1 = *(*uintptr)(unsafe.Pointer(pOp + 16))
		pData0 = aMem + uintptr(nField)*40
		nField = (*TOp)(unsafe.Pointer(pOp)).Fp2
		pLast = pData0 + uintptr(nField-int32(1))*40
		/* Identify the output register */
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		/* Apply the requested affinity to all inputs
		 */
		if zAffinity1 != 0 {
			pRec = pData0
			for cond := true; cond; cond = **(**int8)(__ccgo_up(zAffinity1)) != 0 {
				_applyAffinity(tls, pRec, **(**int8)(__ccgo_up(zAffinity1)), encoding)
				if int32(**(**int8)(__ccgo_up(zAffinity1))) == int32(SQLITE_AFF_REAL) && int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Int) != 0 {
					v191 = pRec + 16
					*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_IntReal))
					v191 = pRec + 16
					*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^libc.Int32FromInt32(MEM_Int))
				}
				zAffinity1 = zAffinity1 + 1
				pRec += 40
			}
		}
		/* Loop through the elements that will make up the record to figure
		 ** out how much space is required for the new record.  After this loop,
		 ** the Mem.uTemp field of each term should hold the serial-type that will
		 ** be used for that term in the generated record:
		 **
		 **   Mem.uTemp value    type
		 **   ---------------    ---------------
		 **      0               NULL
		 **      1               1-byte signed integer
		 **      2               2-byte signed integer
		 **      3               3-byte signed integer
		 **      4               4-byte signed integer
		 **      5               6-byte signed integer
		 **      6               8-byte signed integer
		 **      7               IEEE float
		 **      8               Integer constant 0
		 **      9               Integer constant 1
		 **     10,11            reserved for expansion
		 **    N>=12 and even    BLOB
		 **    N>=13 and odd     text
		 **
		 ** The following additional values are computed:
		 **     nHdr        Number of bytes needed for the record header
		 **     nData       Number of bytes of data space needed for the record
		 **     nZero       Zero bytes at the end of the record
		 */
		pRec = pLast
		for cond := true; cond; cond = int32(1) != 0 {
			if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Null) != 0 {
				if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Zero) != 0 {
					/* Values with MEM_Null and MEM_Zero are created by xColumn virtual
					 ** table methods that never invoke sqlite3_result_xxxxx() while
					 ** computing an unchanging column value in an UPDATE statement.
					 ** Give such values a special internal-use-only serial-type of 10
					 ** so that they can be passed through to xUpdate and have
					 ** a true sqlite3_value_nochange(). */
					(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(10)
				} else {
					(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(0)
				}
				nHdr = nHdr + 1
			} else {
				if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
					/* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
					i3 = *(*Ti64)(unsafe.Pointer(pRec))
					if i3 < 0 {
						uu = uint64(^i3)
					} else {
						uu = uint64(i3)
					}
					nHdr = nHdr + 1
					if uu <= uint64(127) {
						if i3&int64(1) == i3 && int32((*TVdbe)(unsafe.Pointer(p)).FminWriteFileFormat) >= int32(4) {
							(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(8) + uint32(uu)
						} else {
							nData = nData + 1
							(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(1)
						}
					} else {
						if uu <= uint64(32767) {
							nData = nData + uint64(2)
							(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(2)
						} else {
							if uu <= uint64(8388607) {
								nData = nData + uint64(3)
								(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(3)
							} else {
								if uu <= uint64(2147483647) {
									nData = nData + uint64(4)
									(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(4)
								} else {
									if uu <= uint64(140737488355327) {
										nData = nData + uint64(6)
										(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(5)
									} else {
										nData = nData + uint64(8)
										if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_IntReal) != 0 {
											/* If the value is IntReal and is going to take up 8 bytes to store
											 ** as an integer, then we might as well make it an 8-byte floating
											 ** point value */
											*(*float64)(unsafe.Pointer(pRec)) = float64(*(*Ti64)(unsafe.Pointer(pRec)))
											v191 = pRec + 16
											*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^libc.Int32FromInt32(MEM_IntReal))
											v191 = pRec + 16
											*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_Real))
											(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(7)
										} else {
											(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(6)
										}
									}
								}
							}
						}
					}
				} else {
					if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Real) != 0 {
						nHdr = nHdr + 1
						nData = nData + uint64(8)
						(*TMem)(unsafe.Pointer(pRec)).FuTemp = uint32(7)
					} else {
						len11 = uint32((*TMem)(unsafe.Pointer(pRec)).Fn)
						serial_type = len11*uint32(2) + uint32(12) + libc.BoolUint32(int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&libc.Int32FromInt32(MEM_Str) != libc.Int32FromInt32(0))
						if int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Zero) != 0 {
							serial_type = serial_type + uint32(*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pRec)).Fu)))*uint32(2)
							if nData != 0 {
								if _sqlite3VdbeMemExpandBlob(tls, pRec) != 0 {
									goto no_mem
								}
								len11 = len11 + uint32(*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pRec)).Fu)))
							} else {
								nZero = nZero + int64(*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pRec)).Fu)))
							}
						}
						nData = nData + uint64(len11)
						nHdr = nHdr + _sqlite3VarintLen(tls, uint64(serial_type))
						(*TMem)(unsafe.Pointer(pRec)).FuTemp = serial_type
					}
				}
			}
			if pRec == pData0 {
				break
			}
			pRec -= 40
		}
		/* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
		 ** which determines the total number of bytes in the header. The varint
		 ** value is the size of the header in bytes including the size varint
		 ** itself. */
		if nHdr <= int32(126) {
			/* The common case */
			nHdr = nHdr + int32(1)
		} else {
			/* Rare case of a really large header */
			nVarint = _sqlite3VarintLen(tls, uint64(nHdr))
			nHdr = nHdr + nVarint
			if nVarint < _sqlite3VarintLen(tls, uint64(nHdr)) {
				nHdr = nHdr + 1
			}
		}
		nByte1 = int64(uint64(nHdr) + nData)
		/* Make sure the output register has a buffer large enough to store
		 ** the new record. The output register (pOp->p3) is not allowed to
		 ** be one of the input registers (because the following call to
		 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
		 */
		if nByte1+nZero <= int64((*TMem)(unsafe.Pointer(pOut)).FszMalloc) {
			/* The output register is already large enough to hold the record.
			 ** No error checks or buffer enlargement is required */
			(*TMem)(unsafe.Pointer(pOut)).Fz = (*TMem)(unsafe.Pointer(pOut)).FzMalloc
		} else {
			/* Need to make sure that the output is not too big and then enlarge
			 ** the output register to hold the full result */
			if nByte1+nZero > int64(**(**int32)(__ccgo_up(db + 120))) {
				goto too_big
			}
			if _sqlite3VdbeMemClearAndResize(tls, pOut, int32(nByte1)) != 0 {
				goto no_mem
			}
		}
		(*TMem)(unsafe.Pointer(pOut)).Fn = int32(nByte1)
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Blob)
		if nZero != 0 {
			*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pOut)).Fu)) = int32(nZero)
			v191 = pOut + 16
			*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_Zero))
		}
		zHdr1 = (*TMem)(unsafe.Pointer(pOut)).Fz
		zPayload = zHdr1 + uintptr(nHdr)
		/* Write the record */
		if nHdr < int32(0x80) {
			v191 = zHdr1
			zHdr1 = zHdr1 + 1
			**(**Tu8)(__ccgo_up(v191)) = uint8(nHdr)
		} else {
			zHdr1 = zHdr1 + uintptr(_sqlite3PutVarint(tls, zHdr1, uint64(nHdr)))
		}
		pRec = pData0
		for int32(1) != 0 {
			serial_type = (*TMem)(unsafe.Pointer(pRec)).FuTemp
			/* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
			 ** additional varints, one per column.
			 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
			 ** immediately follow the header. */
			if serial_type <= uint32(7) {
				v191 = zHdr1
				zHdr1 = zHdr1 + 1
				**(**Tu8)(__ccgo_up(v191)) = uint8(serial_type)
				if serial_type == uint32(0) {
					/* NULL value.  No change in zPayload */
				} else {
					if serial_type == uint32(7) {
						libc.Xmemcpy(tls, bp+72, pRec, uint32(8))
					} else {
						**(**Tu64)(__ccgo_up(bp + 72)) = uint64(*(*Ti64)(unsafe.Pointer(pRec)))
					}
					len11 = uint32(_sqlite3SmallTypeSizes[serial_type])
					switch len11 {
					default:
						**(**Tu8)(__ccgo_up(zPayload + 7)) = uint8(**(**Tu64)(__ccgo_up(bp + 72)) & libc.Uint64FromInt32(0xff))
						**(**Tu64)(__ccgo_up(bp + 72)) = **(**Tu64)(__ccgo_up(bp + 72)) >> uint64(8)
						**(**Tu8)(__ccgo_up(zPayload + 6)) = uint8(**(**Tu64)(__ccgo_up(bp + 72)) & libc.Uint64FromInt32(0xff))
						**(**Tu64)(__ccgo_up(bp + 72)) = **(**Tu64)(__ccgo_up(bp + 72)) >> uint64(8)
						fallthrough
					case uint32(6):
						**(**Tu8)(__ccgo_up(zPayload + 5)) = uint8(**(**Tu64)(__ccgo_up(bp + 72)) & libc.Uint64FromInt32(0xff))
						**(**Tu64)(__ccgo_up(bp + 72)) = **(**Tu64)(__ccgo_up(bp + 72)) >> uint64(8)
						**(**Tu8)(__ccgo_up(zPayload + 4)) = uint8(**(**Tu64)(__ccgo_up(bp + 72)) & libc.Uint64FromInt32(0xff))
						**(**Tu64)(__ccgo_up(bp + 72)) = **(**Tu64)(__ccgo_up(bp + 72)) >> uint64(8)
						fallthrough
					case uint32(4):
						**(**Tu8)(__ccgo_up(zPayload + 3)) = uint8(**(**Tu64)(__ccgo_up(bp + 72)) & libc.Uint64FromInt32(0xff))
						**(**Tu64)(__ccgo_up(bp + 72)) = **(**Tu64)(__ccgo_up(bp + 72)) >> uint64(8)
						fallthrough
					case uint32(3):
						**(**Tu8)(__ccgo_up(zPayload + 2)) = uint8(**(**Tu64)(__ccgo_up(bp + 72)) & libc.Uint64FromInt32(0xff))
						**(**Tu64)(__ccgo_up(bp + 72)) = **(**Tu64)(__ccgo_up(bp + 72)) >> uint64(8)
						fallthrough
					case uint32(2):
						**(**Tu8)(__ccgo_up(zPayload + 1)) = uint8(**(**Tu64)(__ccgo_up(bp + 72)) & libc.Uint64FromInt32(0xff))
						**(**Tu64)(__ccgo_up(bp + 72)) = **(**Tu64)(__ccgo_up(bp + 72)) >> uint64(8)
						fallthrough
					case uint32(1):
						**(**Tu8)(__ccgo_up(zPayload)) = uint8(**(**Tu64)(__ccgo_up(bp + 72)) & libc.Uint64FromInt32(0xff))
					}
					zPayload = zPayload + uintptr(len11)
				}
			} else {
				if serial_type < uint32(0x80) {
					v191 = zHdr1
					zHdr1 = zHdr1 + 1
					**(**Tu8)(__ccgo_up(v191)) = uint8(serial_type)
					if serial_type >= uint32(14) && (*TMem)(unsafe.Pointer(pRec)).Fn > 0 {
						libc.Xmemcpy(tls, zPayload, (*TMem)(unsafe.Pointer(pRec)).Fz, uint32((*TMem)(unsafe.Pointer(pRec)).Fn))
						zPayload = zPayload + uintptr((*TMem)(unsafe.Pointer(pRec)).Fn)
					}
				} else {
					zHdr1 = zHdr1 + uintptr(_sqlite3PutVarint(tls, zHdr1, uint64(serial_type)))
					if (*TMem)(unsafe.Pointer(pRec)).Fn != 0 {
						libc.Xmemcpy(tls, zPayload, (*TMem)(unsafe.Pointer(pRec)).Fz, uint32((*TMem)(unsafe.Pointer(pRec)).Fn))
						zPayload = zPayload + uintptr((*TMem)(unsafe.Pointer(pRec)).Fn)
					}
				}
			}
			if pRec == pLast {
				break
			}
			pRec += 40
		}
		goto _189
		/* Opcode: Count P1 P2 P3 * *
		 ** Synopsis: r[P2]=count()
		 **
		 ** Store the number of entries (an integer value) in the table or index
		 ** opened by cursor P1 in register P2.
		 **
		 ** If P3==0, then an exact count is obtained, which involves visiting
		 ** every btree page of the table.  But if P3 is non-zero, an estimate
		 ** is returned based on the current cursor position.
		 */
	_69:
		;
		pCrsr1 = *(*uintptr)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) + 36))
		if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 {
			**(**Ti64)(__ccgo_up(bp + 80)) = _sqlite3BtreeRowCountEst(tls, pCrsr1)
		} else {
			**(**Ti64)(__ccgo_up(bp + 80)) = 0 /* Not needed.  Only used to silence a warning. */
			rc = _sqlite3BtreeCount(tls, db, pCrsr1, bp+80)
			if rc != 0 {
				goto abort_due_to_error
			}
		}
		pOut = _out2Prerelease(tls, p, pOp)
		*(*Ti64)(unsafe.Pointer(pOut)) = **(**Ti64)(__ccgo_up(bp + 80))
		goto check_for_interrupt
		/* Opcode: Savepoint P1 * * P4 *
		 **
		 ** Open, release or rollback the savepoint named by parameter P4, depending
		 ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
		 ** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
		 ** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
		 */
	_70:
		;
		p12 = (*TOp)(unsafe.Pointer(pOp)).Fp1
		zName = *(*uintptr)(unsafe.Pointer(pOp + 16))
		/* Assert that the p1 parameter is valid. Also that if there is no open
		 ** transaction, then there cannot be any savepoints.
		 */
		if p12 == SAVEPOINT_BEGIN {
			if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite > 0 {
				/* A new savepoint cannot be created if there are active write
				 ** statements (i.e. open read/write incremental blob handles).
				 */
				_sqlite3VdbeError(tls, p, __ccgo_ts+7142, 0)
				rc = int32(SQLITE_BUSY)
			} else {
				nName = _sqlite3Strlen30(tls, zName)
				/* This call is Ok even if this savepoint is actually a transaction
				 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
				 ** If this is a transaction savepoint being opened, it is guaranteed
				 ** that the db->aVTrans[] array is empty.  */
				rc = _sqlite3VtabSavepoint(tls, db, SAVEPOINT_BEGIN, (*Tsqlite3)(unsafe.Pointer(db)).FnStatement+(*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint)
				if rc != SQLITE_OK {
					goto abort_due_to_error
				}
				/* Create a new savepoint structure. */
				pNew = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(32)+uint32(nName)+uint32(1)))
				if pNew != 0 {
					(*TSavepoint)(unsafe.Pointer(pNew)).FzName = pNew + 1*32
					libc.Xmemcpy(tls, (*TSavepoint)(unsafe.Pointer(pNew)).FzName, zName, uint32(nName+int32(1)))
					/* If there is no open transaction, then mark this as a special
					 ** "transaction savepoint". */
					if (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0 {
						(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(0)
						(*Tsqlite3)(unsafe.Pointer(db)).FisTransactionSavepoint = uint8(1)
					} else {
						(*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint = (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint + 1
					}
					/* Link the new savepoint into the database handle's list. */
					(*TSavepoint)(unsafe.Pointer(pNew)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint
					(*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint = pNew
					(*TSavepoint)(unsafe.Pointer(pNew)).FnDeferredCons = (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons
					(*TSavepoint)(unsafe.Pointer(pNew)).FnDeferredImmCons = (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons
				}
			}
		} else {
			iSavepoint = 0
			/* Find the named savepoint. If there is no such savepoint, then an
			 ** an error is returned to the user.  */
			pSavepoint = (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint
			for {
				if !(pSavepoint != 0 && _sqlite3StrICmp(tls, (*TSavepoint)(unsafe.Pointer(pSavepoint)).FzName, zName) != 0) {
					break
				}
				iSavepoint = iSavepoint + 1
				goto _245
			_245:
				;
				pSavepoint = (*TSavepoint)(unsafe.Pointer(pSavepoint)).FpNext
			}
			if !(pSavepoint != 0) {
				_sqlite3VdbeError(tls, p, __ccgo_ts+7193, libc.VaList(bp+768, zName))
				rc = int32(SQLITE_ERROR)
			} else {
				if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite > 0 && p12 == int32(SAVEPOINT_RELEASE) {
					/* It is not possible to release (commit) a savepoint if there are
					 ** active write statements.
					 */
					_sqlite3VdbeError(tls, p, __ccgo_ts+7215, 0)
					rc = int32(SQLITE_BUSY)
				} else {
					/* Determine whether or not this is a transaction savepoint. If so,
					 ** and this is a RELEASE command, then the current transaction
					 ** is committed.
					 */
					isTransaction = libc.BoolInt32((*TSavepoint)(unsafe.Pointer(pSavepoint)).FpNext == uintptr(0) && (*Tsqlite3)(unsafe.Pointer(db)).FisTransactionSavepoint != 0)
					if isTransaction != 0 && p12 == int32(SAVEPOINT_RELEASE) {
						v190 = _sqlite3VdbeCheckFkDeferred(tls, p)
						rc = v190
						if v190 != SQLITE_OK {
							goto vdbe_return
						}
						(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1)
						if _sqlite3VdbeHalt(tls, p) == int32(SQLITE_BUSY) {
							(*TVdbe)(unsafe.Pointer(p)).Fpc = (int32(pOp) - int32(aOp)) / 20
							(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(0)
							v190 = libc.Int32FromInt32(SQLITE_BUSY)
							rc = v190
							(*TVdbe)(unsafe.Pointer(p)).Frc = v190
							goto vdbe_return
						}
						rc = (*TVdbe)(unsafe.Pointer(p)).Frc
						if rc != 0 {
							(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(0)
						} else {
							(*Tsqlite3)(unsafe.Pointer(db)).FisTransactionSavepoint = uint8(0)
						}
					} else {
						iSavepoint = (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint - iSavepoint - int32(1)
						if p12 == int32(SAVEPOINT_ROLLBACK) {
							isSchemaChange = libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_SchemaChange) != uint32(0))
							ii = 0
							for {
								if !(ii < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
									break
								}
								rc = _sqlite3BtreeTripAllCursors(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii)*16))).FpBt, libc.Int32FromInt32(SQLITE_ABORT)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8), libc.BoolInt32(isSchemaChange == 0))
								if rc != SQLITE_OK {
									goto abort_due_to_error
								}
								goto _248
							_248:
								;
								ii = ii + 1
							}
						} else {
							isSchemaChange = 0
						}
						ii = 0
						for {
							if !(ii < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
								break
							}
							rc = _sqlite3BtreeSavepoint(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii)*16))).FpBt, p12, iSavepoint)
							if rc != SQLITE_OK {
								goto abort_due_to_error
							}
							goto _249
						_249:
							;
							ii = ii + 1
						}
						if isSchemaChange != 0 {
							_sqlite3ExpirePreparedStatements(tls, db, 0)
							_sqlite3ResetAllSchemasOfConnection(tls, db)
							**(**Tu32)(__ccgo_up(db + 24)) |= uint32(DBFLAG_SchemaChange)
						}
					}
					if rc != 0 {
						goto abort_due_to_error
					}
					/* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
					 ** savepoints nested inside of the savepoint being operated on. */
					for (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint != pSavepoint {
						pTmp = (*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint
						(*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint = (*TSavepoint)(unsafe.Pointer(pTmp)).FpNext
						_sqlite3DbFree(tls, db, pTmp)
						(*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint = (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint - 1
					}
					/* If it is a RELEASE, then destroy the savepoint being operated on
					 ** too. If it is a ROLLBACK TO, then set the number of deferred
					 ** constraint violations present in the database to the value stored
					 ** when the savepoint was created.  */
					if p12 == int32(SAVEPOINT_RELEASE) {
						(*Tsqlite3)(unsafe.Pointer(db)).FpSavepoint = (*TSavepoint)(unsafe.Pointer(pSavepoint)).FpNext
						_sqlite3DbFree(tls, db, pSavepoint)
						if !(isTransaction != 0) {
							(*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint = (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint - 1
						}
					} else {
						(*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons = (*TSavepoint)(unsafe.Pointer(pSavepoint)).FnDeferredCons
						(*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons = (*TSavepoint)(unsafe.Pointer(pSavepoint)).FnDeferredImmCons
					}
					if !(isTransaction != 0) || p12 == int32(SAVEPOINT_ROLLBACK) {
						rc = _sqlite3VtabSavepoint(tls, db, p12, iSavepoint)
						if rc != SQLITE_OK {
							goto abort_due_to_error
						}
					}
				}
			}
		}
		if rc != 0 {
			goto abort_due_to_error
		}
		if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) == int32(VDBE_HALT_STATE) {
			rc = int32(SQLITE_DONE)
			goto vdbe_return
		}
		goto _189
		/* Opcode: AutoCommit P1 P2 * * *
		 **
		 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
		 ** back any currently active btree transactions. If there are any active
		 ** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
		 ** there are active writing VMs or active VMs that use shared cache.
		 **
		 ** This instruction causes the VM to halt.
		 */
	_71:
		;
		desiredAutoCommit = (*TOp)(unsafe.Pointer(pOp)).Fp1
		iRollback = (*TOp)(unsafe.Pointer(pOp)).Fp2
		/* At least this one VM is active */
		if desiredAutoCommit != int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) {
			if iRollback != 0 {
				_sqlite3RollbackAll(tls, db, libc.Int32FromInt32(SQLITE_ABORT)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8))
				(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1)
			} else {
				if desiredAutoCommit != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite > 0 {
					/* If this instruction implements a COMMIT and other VMs are writing
					 ** return an error indicating that the other VMs must complete first.
					 */
					_sqlite3VdbeError(tls, p, __ccgo_ts+7269, 0)
					rc = int32(SQLITE_BUSY)
					goto abort_due_to_error
				} else {
					v190 = _sqlite3VdbeCheckFkDeferred(tls, p)
					rc = v190
					if v190 != SQLITE_OK {
						goto vdbe_return
					} else {
						(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(desiredAutoCommit)
					}
				}
			}
			if _sqlite3VdbeHalt(tls, p) == int32(SQLITE_BUSY) {
				(*TVdbe)(unsafe.Pointer(p)).Fpc = (int32(pOp) - int32(aOp)) / 20
				(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(libc.Int32FromInt32(1) - desiredAutoCommit)
				v190 = libc.Int32FromInt32(SQLITE_BUSY)
				rc = v190
				(*TVdbe)(unsafe.Pointer(p)).Frc = v190
				goto vdbe_return
			}
			_sqlite3CloseSavepoints(tls, db)
			if (*TVdbe)(unsafe.Pointer(p)).Frc == SQLITE_OK {
				rc = int32(SQLITE_DONE)
			} else {
				rc = int32(SQLITE_ERROR)
			}
			goto vdbe_return
		} else {
			if !(desiredAutoCommit != 0) {
				v191 = __ccgo_ts + 7324
			} else {
				if iRollback != 0 {
					v194 = __ccgo_ts + 7372
				} else {
					v194 = __ccgo_ts + 7415
				}
				v191 = v194
			}
			_sqlite3VdbeError(tls, p, v191, 0)
			rc = int32(SQLITE_ERROR)
			goto abort_due_to_error
		}
		/* Opcode: Transaction P1 P2 P3 P4 P5
		 **
		 ** Begin a transaction on database P1 if a transaction is not already
		 ** active.
		 ** If P2 is non-zero, then a write-transaction is started, or if a
		 ** read-transaction is already active, it is upgraded to a write-transaction.
		 ** If P2 is zero, then a read-transaction is started.  If P2 is 2 or more
		 ** then an exclusive transaction is started.
		 **
		 ** P1 is the index of the database file on which the transaction is
		 ** started.  Index 0 is the main database file and index 1 is the
		 ** file used for temporary tables.  Indices of 2 or more are used for
		 ** attached databases.
		 **
		 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
		 ** true (this flag is set if the Vdbe may modify more than one row and may
		 ** throw an ABORT exception), a statement transaction may also be opened.
		 ** More specifically, a statement transaction is opened iff the database
		 ** connection is currently not in autocommit mode, or if there are other
		 ** active statements. A statement transaction allows the changes made by this
		 ** VDBE to be rolled back after an error without having to roll back the
		 ** entire transaction. If no error is encountered, the statement transaction
		 ** will automatically commit when the VDBE halts.
		 **
		 ** If P5!=0 then this opcode also checks the schema cookie against P3
		 ** and the schema generation counter against P4.
		 ** The cookie changes its value whenever the database schema changes.
		 ** This operation is used to detect when that the cookie has changed
		 ** and that the current process needs to reread the schema.  If the schema
		 ** cookie in P3 differs from the schema cookie in the database header or
		 ** if the schema generation counter in P4 differs from the current
		 ** generation counter, then an SQLITE_SCHEMA error is raised and execution
		 ** halts.  The sqlite3_step() wrapper function might then reprepare the
		 ** statement and rerun it from the beginning.
		 */
	_72:
		;
		**(**int32)(__ccgo_up(bp + 88)) = 0
		if (*TOp)(unsafe.Pointer(pOp)).Fp2 != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(libc.Uint64FromInt32(SQLITE_QueryOnly)|uint64(libc.Int32FromInt32(0x00002))<<libc.Int32FromInt32(32)) != uint64(0) {
			if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_QueryOnly) != 0 {
				/* Writes prohibited by the "PRAGMA query_only=TRUE" statement */
				rc = int32(SQLITE_READONLY)
			} else {
				/* Writes prohibited due to a prior SQLITE_CORRUPT in the current
				 ** transaction */
				rc = int32(SQLITE_CORRUPT)
			}
			goto abort_due_to_error
		}
		pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16
		pBt = (*TDb)(unsafe.Pointer(pDb)).FpBt
		if pBt != 0 {
			rc = _sqlite3BtreeBeginTrans(tls, pBt, (*TOp)(unsafe.Pointer(pOp)).Fp2, bp+88)
			if rc != SQLITE_OK {
				if rc&int32(0xff) == int32(SQLITE_BUSY) {
					(*TVdbe)(unsafe.Pointer(p)).Fpc = (int32(pOp) - int32(aOp)) / 20
					(*TVdbe)(unsafe.Pointer(p)).Frc = rc
					goto vdbe_return
				}
				goto abort_due_to_error
			}
			if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x20>>5)) != 0 && (*TOp)(unsafe.Pointer(pOp)).Fp2 != 0 && (int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) == 0 || (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead > int32(1)) {
				if (*TVdbe)(unsafe.Pointer(p)).FiStatement == 0 {
					(*Tsqlite3)(unsafe.Pointer(db)).FnStatement = (*Tsqlite3)(unsafe.Pointer(db)).FnStatement + 1
					(*TVdbe)(unsafe.Pointer(p)).FiStatement = (*Tsqlite3)(unsafe.Pointer(db)).FnSavepoint + (*Tsqlite3)(unsafe.Pointer(db)).FnStatement
				}
				rc = _sqlite3VtabSavepoint(tls, db, SAVEPOINT_BEGIN, (*TVdbe)(unsafe.Pointer(p)).FiStatement-int32(1))
				if rc == SQLITE_OK {
					rc = _sqlite3BtreeBeginStmt(tls, pBt, (*TVdbe)(unsafe.Pointer(p)).FiStatement)
				}
				/* Store the current value of the database handles deferred constraint
				 ** counter. If the statement transaction needs to be rolled back,
				 ** the value of this counter needs to be restored too.  */
				(*TVdbe)(unsafe.Pointer(p)).FnStmtDefCons = (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons
				(*TVdbe)(unsafe.Pointer(p)).FnStmtDefImmCons = (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons
			}
		}
		if rc == SQLITE_OK && (*TOp)(unsafe.Pointer(pOp)).Fp5 != 0 && (**(**int32)(__ccgo_up(bp + 88)) != (*TOp)(unsafe.Pointer(pOp)).Fp3 || (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).FiGeneration != *(*int32)(unsafe.Pointer(pOp + 16))) {
			/*
			 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
			 ** version is checked to ensure that the schema has not changed since the
			 ** SQL statement was prepared.
			 */
			_sqlite3DbFree(tls, db, (*TVdbe)(unsafe.Pointer(p)).FzErrMsg)
			(*TVdbe)(unsafe.Pointer(p)).FzErrMsg = _sqlite3DbStrDup(tls, db, __ccgo_ts+7456)
			/* If the schema-cookie from the database file matches the cookie
			 ** stored with the in-memory representation of the schema, do
			 ** not reload the schema from the database file.
			 **
			 ** If virtual-tables are in use, this is not just an optimization.
			 ** Often, v-tables store their data in other SQLite tables, which
			 ** are queried from within xNext() and other v-table methods using
			 ** prepared queries. If such a query is out-of-date, we do not want to
			 ** discard the database schema, as the user code implementing the
			 ** v-table would have to be ready for the sqlite3_vtab structure itself
			 ** to be invalidated whenever sqlite3_step() is called from within
			 ** a v-table method.
			 */
			if (*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpSchema)).Fschema_cookie != **(**int32)(__ccgo_up(bp + 88)) {
				_sqlite3ResetOneSchema(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1)
			}
			libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 0, 0x3)
			rc = int32(SQLITE_SCHEMA)
			/* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes()
			 ** from being modified in sqlite3VdbeHalt(). If this statement is
			 ** reprepared, changeCntOn will be set again. */
			libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 4, 0x10)
		}
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: ReadCookie P1 P2 P3 * *
		 **
		 ** Read cookie number P3 from database P1 and write it into register P2.
		 ** P3==1 is the schema version.  P3==2 is the database format.
		 ** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
		 ** the main database file and P1==1 is the database file used to store
		 ** temporary tables.
		 **
		 ** There must be a read-lock on the database (either a transaction
		 ** must be started or there must be an open cursor) before
		 ** executing this instruction.
		 */
	_73:
		;
		iDb = (*TOp)(unsafe.Pointer(pOp)).Fp1
		iCookie = (*TOp)(unsafe.Pointer(pOp)).Fp3
		_sqlite3BtreeGetMeta(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FpBt, iCookie, bp+92)
		pOut = _out2Prerelease(tls, p, pOp)
		*(*Ti64)(unsafe.Pointer(pOut)) = int64(**(**int32)(__ccgo_up(bp + 92)))
		goto _189
		/* Opcode: SetCookie P1 P2 P3 * P5
		 **
		 ** Write the integer value P3 into cookie number P2 of database P1.
		 ** P2==1 is the schema version.  P2==2 is the database format.
		 ** P2==3 is the recommended pager cache
		 ** size, and so forth.  P1==0 is the main database file and P1==1 is the
		 ** database file used to store temporary tables.
		 **
		 ** A transaction must be started before executing this opcode.
		 **
		 ** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal
		 ** schema version is set to P3-P5.  The "PRAGMA schema_version=N" statement
		 ** has P5 set to 1, so that the internal schema version will be different
		 ** from the database schema version, resulting in a schema reset.
		 */
	_74:
		;
		pDb1 = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16
		/* See note about index shifting on OP_ReadCookie */
		rc = _sqlite3BtreeUpdateMeta(tls, (*TDb)(unsafe.Pointer(pDb1)).FpBt, (*TOp)(unsafe.Pointer(pOp)).Fp2, uint32((*TOp)(unsafe.Pointer(pOp)).Fp3))
		if (*TOp)(unsafe.Pointer(pOp)).Fp2 == int32(BTREE_SCHEMA_VERSION) {
			/* When the schema cookie changes, record the new cookie internally */
			**(**Tu32)(__ccgo_up((*TDb)(unsafe.Pointer(pDb1)).FpSchema)) = **(**Tu32)(__ccgo_up(pOp + 12)) - uint32((*TOp)(unsafe.Pointer(pOp)).Fp5)
			**(**Tu32)(__ccgo_up(db + 24)) |= uint32(DBFLAG_SchemaChange)
			_sqlite3FkClearTriggerCache(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1)
		} else {
			if (*TOp)(unsafe.Pointer(pOp)).Fp2 == int32(BTREE_FILE_FORMAT) {
				/* Record changes in the file format */
				(*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb1)).FpSchema)).Ffile_format = uint8((*TOp)(unsafe.Pointer(pOp)).Fp3)
			}
		}
		if (*TOp)(unsafe.Pointer(pOp)).Fp1 == int32(1) {
			/* Invalidate all prepared statements whenever the TEMP database
			 ** schema is changed.  Ticket #1644 */
			_sqlite3ExpirePreparedStatements(tls, db, 0)
			libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 0, 0x3)
		}
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: OpenRead P1 P2 P3 P4 P5
		 ** Synopsis: root=P2 iDb=P3
		 **
		 ** Open a read-only cursor for the database table whose root page is
		 ** P2 in a database file.  The database file is determined by P3.
		 ** P3==0 means the main database, P3==1 means the database used for
		 ** temporary tables, and P3>1 means used the corresponding attached
		 ** database.  Give the new cursor an identifier of P1.  The P1
		 ** values need not be contiguous but all P1 values should be small integers.
		 ** It is an error for P1 to be negative.
		 **
		 ** Allowed P5 bits:
		 ** <ul>
		 ** <li>  <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
		 **       equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
		 **       of OP_SeekLE/OP_IdxLT)
		 ** </ul>
		 **
		 ** The P4 value may be either an integer (P4_INT32) or a pointer to
		 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
		 ** object, then table being opened must be an [index b-tree] where the
		 ** KeyInfo object defines the content and collating
		 ** sequence of that index b-tree. Otherwise, if P4 is an integer
		 ** value, then the table being opened must be a [table b-tree] with a
		 ** number of columns no less than the value of P4.
		 **
		 ** See also: OpenWrite, ReopenIdx
		 */
		/* Opcode: ReopenIdx P1 P2 P3 P4 P5
		 ** Synopsis: root=P2 iDb=P3
		 **
		 ** The ReopenIdx opcode works like OP_OpenRead except that it first
		 ** checks to see if the cursor on P1 is already open on the same
		 ** b-tree and if it is this opcode becomes a no-op.  In other words,
		 ** if the cursor is already open, do not reopen it.
		 **
		 ** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
		 ** and with P4 being a P4_KEYINFO object.  Furthermore, the P3 value must
		 ** be the same as every other ReopenIdx or OpenRead for the same cursor
		 ** number.
		 **
		 ** Allowed P5 bits:
		 ** <ul>
		 ** <li>  <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
		 **       equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
		 **       of OP_SeekLE/OP_IdxLT)
		 ** </ul>
		 **
		 ** See also: OP_OpenRead, OP_OpenWrite
		 */
		/* Opcode: OpenWrite P1 P2 P3 P4 P5
		 ** Synopsis: root=P2 iDb=P3
		 **
		 ** Open a read/write cursor named P1 on the table or index whose root
		 ** page is P2 (or whose root page is held in register P2 if the
		 ** OPFLAG_P2ISREG bit is set in P5 - see below).
		 **
		 ** The P4 value may be either an integer (P4_INT32) or a pointer to
		 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
		 ** object, then table being opened must be an [index b-tree] where the
		 ** KeyInfo object defines the content and collating
		 ** sequence of that index b-tree. Otherwise, if P4 is an integer
		 ** value, then the table being opened must be a [table b-tree] with a
		 ** number of columns no less than the value of P4.
		 **
		 ** Allowed P5 bits:
		 ** <ul>
		 ** <li>  <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
		 **       equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
		 **       of OP_SeekLE/OP_IdxLT)
		 ** <li>  <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
		 **       and subsequently delete entries in an index btree.  This is a
		 **       hint to the storage engine that the storage engine is allowed to
		 **       ignore.  The hint is not used by the official SQLite b*tree storage
		 **       engine, but is used by COMDB2.
		 ** <li>  <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
		 **       as the root page, not the value of P2 itself.
		 ** </ul>
		 **
		 ** This instruction works like OpenRead except that it opens the cursor
		 ** in read/write mode.
		 **
		 ** See also: OP_OpenRead, OP_ReopenIdx
		 */
	_77:
		;
		pCur = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if pCur != 0 && (*TVdbeCursor)(unsafe.Pointer(pCur)).FpgnoRoot == uint32((*TOp)(unsafe.Pointer(pOp)).Fp2) {
			/* Guaranteed by the code generator */
			_sqlite3BtreeClearCursor(tls, *(*uintptr)(unsafe.Pointer(pCur + 36)))
			goto open_cursor_set_hints
		}
		/* If the cursor is not currently open or is open on a different
		 ** index, then fall through into OP_OpenRead to force a reopen */
	_76:
		; /* ncycle */
	_75:
		;
		if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x3>>0)) == int32(1) {
			rc = libc.Int32FromInt32(SQLITE_ABORT) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
			goto abort_due_to_error
		}
		nField1 = 0
		pKeyInfo1 = uintptr(0)
		p23 = uint32((*TOp)(unsafe.Pointer(pOp)).Fp2)
		iDb1 = (*TOp)(unsafe.Pointer(pOp)).Fp3
		pDb2 = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb1)*16
		pX = (*TDb)(unsafe.Pointer(pDb2)).FpBt
		if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_OpenWrite) {
			wrFlag = int32(BTREE_WRCSR) | int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_FORDELETE)
			if int32((*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb2)).FpSchema)).Ffile_format) < int32((*TVdbe)(unsafe.Pointer(p)).FminWriteFileFormat) {
				(*TVdbe)(unsafe.Pointer(p)).FminWriteFileFormat = (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb2)).FpSchema)).Ffile_format
			}
			if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_P2ISREG) != 0 {
				pIn2 = aMem + uintptr(p23)*40
				_sqlite3VdbeMemIntegerify(tls, pIn2)
				p23 = uint32(int32(*(*Ti64)(unsafe.Pointer(pIn2))))
				/* The p2 value always comes from a prior OP_CreateBtree opcode and
				 ** that opcode will always set the p2 value to 2 or more or else fail.
				 ** If there were a failure, the prepared statement would have halted
				 ** before reaching this instruction. */
			}
		} else {
			wrFlag = 0
		}
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == -int32(9) {
			pKeyInfo1 = *(*uintptr)(unsafe.Pointer(pOp + 16))
			nField1 = int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo1)).FnAllField)
		} else {
			if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == -int32(3) {
				nField1 = *(*int32)(unsafe.Pointer(pOp + 16))
			}
		}
		/* Table with INTEGER PRIMARY KEY and nothing else */
		pCur = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, nField1, uint8(CURTYPE_BTREE))
		if pCur == uintptr(0) {
			goto no_mem
		}
		(*TVdbeCursor)(unsafe.Pointer(pCur)).FiDb = int8(iDb1)
		(*TVdbeCursor)(unsafe.Pointer(pCur)).FnullRow = uint8(1)
		libc.SetBitFieldPtr8Uint32(pCur+8, libc.Uint32FromInt32(1), 2, 0x4)
		(*TVdbeCursor)(unsafe.Pointer(pCur)).FpgnoRoot = p23
		rc = _sqlite3BtreeCursor(tls, pX, p23, wrFlag, pKeyInfo1, *(*uintptr)(unsafe.Pointer(pCur + 36)))
		(*TVdbeCursor)(unsafe.Pointer(pCur)).FpKeyInfo = pKeyInfo1
		/* Set the VdbeCursor.isTable variable. Previous versions of
		 ** SQLite used to check if the root-page flags were sane at this point
		 ** and report database corruption if they were not, but this check has
		 ** since moved into the btree layer.  */
		(*TVdbeCursor)(unsafe.Pointer(pCur)).FisTable = libc.BoolUint8(int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) != -int32(9))
		goto open_cursor_set_hints
	open_cursor_set_hints:
		;
		_sqlite3BtreeCursorHintFlags(tls, *(*uintptr)(unsafe.Pointer(pCur + 36)), uint32(int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&(libc.Int32FromInt32(OPFLAG_BULKCSR)|libc.Int32FromInt32(OPFLAG_SEEKEQ))))
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: OpenDup P1 P2 * * *
		 **
		 ** Open a new cursor P1 that points to the same ephemeral table as
		 ** cursor P2.  The P2 cursor must have been opened by a prior OP_OpenEphemeral
		 ** opcode.  Only ephemeral cursors may be duplicated.
		 **
		 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
		 */
	_78:
		; /* The new cursor */
		pOrig = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*4))
		/* Only ephemeral cursors can be duplicated */
		pCx = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, int32((*TVdbeCursor)(unsafe.Pointer(pOrig)).FnField), uint8(CURTYPE_BTREE))
		if pCx == uintptr(0) {
			goto no_mem
		}
		(*TVdbeCursor)(unsafe.Pointer(pCx)).FnullRow = uint8(1)
		libc.SetBitFieldPtr8Uint32(pCx+8, libc.Uint32FromInt32(1), 0, 0x1)
		(*TVdbeCursor)(unsafe.Pointer(pCx)).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pOrig)).FpKeyInfo
		(*TVdbeCursor)(unsafe.Pointer(pCx)).FisTable = (*TVdbeCursor)(unsafe.Pointer(pOrig)).FisTable
		(*TVdbeCursor)(unsafe.Pointer(pCx)).FpgnoRoot = (*TVdbeCursor)(unsafe.Pointer(pOrig)).FpgnoRoot
		libc.SetBitFieldPtr8Uint32(pCx+8, uint32(int32(TBool(*(*uint8)(unsafe.Pointer(pOrig + 8))&0x4>>2))), 2, 0x4)
		*(*uintptr)(unsafe.Pointer(pCx + 12)) = *(*uintptr)(unsafe.Pointer(pOrig + 12))
		libc.SetBitFieldPtr8Uint32(pCx+8, libc.Uint32FromInt32(1), 3, 0x8)
		libc.SetBitFieldPtr8Uint32(pOrig+8, libc.Uint32FromInt32(1), 3, 0x8)
		rc = _sqlite3BtreeCursor(tls, *(*uintptr)(unsafe.Pointer(pCx + 12)), (*TVdbeCursor)(unsafe.Pointer(pCx)).FpgnoRoot, int32(BTREE_WRCSR), (*TVdbeCursor)(unsafe.Pointer(pCx)).FpKeyInfo, *(*uintptr)(unsafe.Pointer(pCx + 36)))
		/* The sqlite3BtreeCursor() routine can only fail for the first cursor
		 ** opened for a database.  Since there is already an open cursor when this
		 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
		goto _189
		/* Opcode: OpenEphemeral P1 P2 P3 P4 P5
		 ** Synopsis: nColumn=P2
		 **
		 ** Open a new cursor P1 to a transient table.
		 ** The cursor is always opened read/write even if
		 ** the main database is read-only.  The ephemeral
		 ** table is deleted automatically when the cursor is closed.
		 **
		 ** If the cursor P1 is already opened on an ephemeral table, the table
		 ** is cleared (all content is erased).
		 **
		 ** P2 is the number of columns in the ephemeral table.
		 ** The cursor points to a BTree table if P4==0 and to a BTree index
		 ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
		 ** that defines the format of keys in the index.
		 **
		 ** The P5 parameter can be a mask of the BTREE_* flags defined
		 ** in btree.h.  These flags control aspects of the operation of
		 ** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
		 ** added automatically.
		 **
		 ** If P3 is positive, then reg[P3] is modified slightly so that it
		 ** can be used as zero-length data for OP_Insert.  This is an optimization
		 ** that avoids an extra OP_Blob opcode to initialize that register.
		 */
		/* Opcode: OpenAutoindex P1 P2 * P4 *
		 ** Synopsis: nColumn=P2
		 **
		 ** This opcode works the same as OP_OpenEphemeral.  It has a
		 ** different name to distinguish its use.  Tables created using
		 ** by this opcode will be used for automatically created transient
		 ** indices in joins.
		 */
	_80:
		; /* ncycle */
	_79:
		;
		if (*TOp)(unsafe.Pointer(pOp)).Fp3 > 0 {
			/* Make register reg[P3] into a value that can be used as the data
			 ** form sqlite3BtreeInsert() where the length of the data is zero. */
			/* Only used when number of columns is zero */
			(**(**TMem)(__ccgo_up(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40))).Fn = 0
			(**(**TMem)(__ccgo_up(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40))).Fz = __ccgo_ts + 1711
		}
		pCx1 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if pCx1 != 0 && !(int32(TBool(*(*uint8)(unsafe.Pointer(pCx1 + 8))&0x8>>3)) != 0) && (*TOp)(unsafe.Pointer(pOp)).Fp2 <= int32((*TVdbeCursor)(unsafe.Pointer(pCx1)).FnField) {
			/* If the ephemeral table is already open and has no duplicates from
			 ** OP_OpenDup, then erase all existing content so that the table is
			 ** empty again, rather than creating a new table. */
			(*TVdbeCursor)(unsafe.Pointer(pCx1)).FseqCount = 0
			(*TVdbeCursor)(unsafe.Pointer(pCx1)).FcacheStatus = uint32(CACHE_STALE)
			rc = _sqlite3BtreeClearTable(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), int32((*TVdbeCursor)(unsafe.Pointer(pCx1)).FpgnoRoot), uintptr(0))
		} else {
			pCx1 = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, (*TOp)(unsafe.Pointer(pOp)).Fp2, uint8(CURTYPE_BTREE))
			if pCx1 == uintptr(0) {
				goto no_mem
			}
			libc.SetBitFieldPtr8Uint32(pCx1+8, libc.Uint32FromInt32(1), 0, 0x1)
			rc = _sqlite3BtreeOpen(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, uintptr(0), db, pCx1+12, libc.Int32FromInt32(BTREE_OMIT_JOURNAL)|libc.Int32FromInt32(BTREE_SINGLE)|int32((*TOp)(unsafe.Pointer(pOp)).Fp5), _vfsFlags)
			if rc == SQLITE_OK {
				rc = _sqlite3BtreeBeginTrans(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), int32(1), uintptr(0))
				if rc == SQLITE_OK {
					/* If a transient index is required, create it by calling
					 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
					 ** opening it. If a transient table is required, just use the
					 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
					 */
					v194 = *(*uintptr)(unsafe.Pointer(pOp + 16))
					pKeyInfo2 = v194
					v191 = v194
					(*TVdbeCursor)(unsafe.Pointer(pCx1)).FpKeyInfo = v191
					if v191 != uintptr(0) {
						rc = _sqlite3BtreeCreateTable(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), pCx1+48, int32(BTREE_BLOBKEY)|int32((*TOp)(unsafe.Pointer(pOp)).Fp5))
						if rc == SQLITE_OK {
							rc = _sqlite3BtreeCursor(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), (*TVdbeCursor)(unsafe.Pointer(pCx1)).FpgnoRoot, int32(BTREE_WRCSR), pKeyInfo2, *(*uintptr)(unsafe.Pointer(pCx1 + 36)))
						}
						(*TVdbeCursor)(unsafe.Pointer(pCx1)).FisTable = uint8(0)
					} else {
						(*TVdbeCursor)(unsafe.Pointer(pCx1)).FpgnoRoot = uint32(SCHEMA_ROOT)
						rc = _sqlite3BtreeCursor(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)), uint32(SCHEMA_ROOT), int32(BTREE_WRCSR), uintptr(0), *(*uintptr)(unsafe.Pointer(pCx1 + 36)))
						(*TVdbeCursor)(unsafe.Pointer(pCx1)).FisTable = uint8(1)
					}
				}
				libc.SetBitFieldPtr8Uint32(pCx1+8, libc.BoolUint32(int32((*TOp)(unsafe.Pointer(pOp)).Fp5) != libc.Int32FromInt32(BTREE_UNORDERED)), 2, 0x4)
				if rc != 0 {
					_sqlite3BtreeClose(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 12)))
					**(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) = uintptr(0) /* Not required; helps with static analysis */
				} else {
				}
			}
		}
		if rc != 0 {
			goto abort_due_to_error
		}
		(*TVdbeCursor)(unsafe.Pointer(pCx1)).FnullRow = uint8(1)
		goto _189
		/* Opcode: SorterOpen P1 P2 P3 P4 *
		 **
		 ** This opcode works like OP_OpenEphemeral except that it opens
		 ** a transient index that is specifically designed to sort large
		 ** tables using an external merge-sort algorithm.
		 **
		 ** If argument P3 is non-zero, then it indicates that the sorter may
		 ** assume that a stable sort considering the first P3 fields of each
		 ** key is sufficient to produce the required results.
		 */
	_81:
		;
		pCx2 = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, (*TOp)(unsafe.Pointer(pOp)).Fp2, uint8(CURTYPE_SORTER))
		if pCx2 == uintptr(0) {
			goto no_mem
		}
		(*TVdbeCursor)(unsafe.Pointer(pCx2)).FpKeyInfo = *(*uintptr)(unsafe.Pointer(pOp + 16))
		rc = _sqlite3VdbeSorterInit(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp3, pCx2)
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: SequenceTest P1 P2 * * *
		 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2
		 **
		 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump
		 ** to P2. Regardless of whether or not the jump is taken, increment the
		 ** the sequence value.
		 */
	_82:
		;
		pC4 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		v191 = pC4 + 16
		v256 = *(*Ti64)(unsafe.Pointer(v191))
		*(*Ti64)(unsafe.Pointer(v191)) = *(*Ti64)(unsafe.Pointer(v191)) + 1
		if v256 == 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: OpenPseudo P1 P2 P3 * *
		 ** Synopsis: P3 columns in r[P2]
		 **
		 ** Open a new cursor that points to a fake table that contains a single
		 ** row of data.  The content of that one row is the content of memory
		 ** register P2.  In other words, cursor P1 becomes an alias for the
		 ** MEM_Blob content contained in register P2.
		 **
		 ** A pseudo-table created by this opcode is used to hold a single
		 ** row output from the sorter so that the row can be decomposed into
		 ** individual columns using the OP_Column opcode.  The OP_Column opcode
		 ** is the only cursor opcode that works with a pseudo-table.
		 **
		 ** P3 is the number of fields in the records that will be stored by
		 ** the pseudo-table.  If P2 is 0 or negative then the pseudo-cursor
		 ** will return NULL for every column.
		 */
	_83:
		;
		pCx3 = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, (*TOp)(unsafe.Pointer(pOp)).Fp3, uint8(CURTYPE_PSEUDO))
		if pCx3 == uintptr(0) {
			goto no_mem
		}
		(*TVdbeCursor)(unsafe.Pointer(pCx3)).FnullRow = uint8(1)
		(*TVdbeCursor)(unsafe.Pointer(pCx3)).FseekResult = (*TOp)(unsafe.Pointer(pOp)).Fp2
		(*TVdbeCursor)(unsafe.Pointer(pCx3)).FisTable = uint8(1)
		/* Give this pseudo-cursor a fake BtCursor pointer so that pCx
		 ** can be safely passed to sqlite3VdbeCursorMoveto().  This avoids a test
		 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
		 ** which is a performance optimization */
		*(*uintptr)(unsafe.Pointer(pCx3 + 36)) = _sqlite3BtreeFakeValidCursor(tls)
		goto _189
		/* Opcode: Close P1 * * * *
		 **
		 ** Close a cursor previously opened as P1.  If P1 is not
		 ** currently open, this instruction is a no-op.
		 */
	_84:
		; /* ncycle */
		_sqlite3VdbeFreeCursor(tls, p, **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)))
		**(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) = uintptr(0)
		goto _189
		/* Opcode: SeekGE P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
		 ** use the value in register P3 as the key.  If cursor P1 refers
		 ** to an SQL index, then P3 is the first in an array of P4 registers
		 ** that are used as an unpacked index key.
		 **
		 ** Reposition cursor P1 so that  it points to the smallest entry that
		 ** is greater than or equal to the key value. If there are no records
		 ** greater than or equal to the key and P2 is not zero, then jump to P2.
		 **
		 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
		 ** opcode will either land on a record that exactly matches the key, or
		 ** else it will cause a jump to P2.  When the cursor is OPFLAG_SEEKEQ,
		 ** this opcode must be followed by an IdxLE opcode with the same arguments.
		 ** The IdxGT opcode will be skipped if this opcode succeeds, but the
		 ** IdxGT opcode will be used on subsequent loop iterations.  The
		 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
		 ** is an equality search.
		 **
		 ** This opcode leaves the cursor configured to move in forward order,
		 ** from the beginning toward the end.  In other words, the cursor is
		 ** configured to use Next, not Prev.
		 **
		 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
		 */
		/* Opcode: SeekGT P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
		 ** use the value in register P3 as a key. If cursor P1 refers
		 ** to an SQL index, then P3 is the first in an array of P4 registers
		 ** that are used as an unpacked index key.
		 **
		 ** Reposition cursor P1 so that it points to the smallest entry that
		 ** is greater than the key value. If there are no records greater than
		 ** the key and P2 is not zero, then jump to P2.
		 **
		 ** This opcode leaves the cursor configured to move in forward order,
		 ** from the beginning toward the end.  In other words, the cursor is
		 ** configured to use Next, not Prev.
		 **
		 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
		 */
		/* Opcode: SeekLT P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
		 ** use the value in register P3 as a key. If cursor P1 refers
		 ** to an SQL index, then P3 is the first in an array of P4 registers
		 ** that are used as an unpacked index key.
		 **
		 ** Reposition cursor P1 so that  it points to the largest entry that
		 ** is less than the key value. If there are no records less than
		 ** the key and P2 is not zero, then jump to P2.
		 **
		 ** This opcode leaves the cursor configured to move in reverse order,
		 ** from the end toward the beginning.  In other words, the cursor is
		 ** configured to use Prev, not Next.
		 **
		 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
		 */
		/* Opcode: SeekLE P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
		 ** use the value in register P3 as a key. If cursor P1 refers
		 ** to an SQL index, then P3 is the first in an array of P4 registers
		 ** that are used as an unpacked index key.
		 **
		 ** Reposition cursor P1 so that it points to the largest entry that
		 ** is less than or equal to the key value. If there are no records
		 ** less than or equal to the key and P2 is not zero, then jump to P2.
		 **
		 ** This opcode leaves the cursor configured to move in reverse order,
		 ** from the end toward the beginning.  In other words, the cursor is
		 ** configured to use Prev, not Next.
		 **
		 ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
		 ** opcode will either land on a record that exactly matches the key, or
		 ** else it will cause a jump to P2.  When the cursor is OPFLAG_SEEKEQ,
		 ** this opcode must be followed by an IdxLE opcode with the same arguments.
		 ** The IdxGE opcode will be skipped if this opcode succeeds, but the
		 ** IdxGE opcode will be used on subsequent loop iterations.  The
		 ** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this
		 ** is an equality search.
		 **
		 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
		 */
	_88:
		; /* jump0, in3, group, ncycle */
	_87:
		; /* jump0, in3, group, ncycle */
	_86:
		; /* jump0, in3, group, ncycle */
	_85:
		; /* Only interested in == results */
		pC5 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		oc = int32((*TOp)(unsafe.Pointer(pOp)).Fopcode)
		eqOnly = 0
		(*TVdbeCursor)(unsafe.Pointer(pC5)).FnullRow = uint8(0)
		(*TVdbeCursor)(unsafe.Pointer(pC5)).FdeferredMoveto = uint8(0)
		(*TVdbeCursor)(unsafe.Pointer(pC5)).FcacheStatus = uint32(CACHE_STALE)
		if (*TVdbeCursor)(unsafe.Pointer(pC5)).FisTable != 0 {
			/* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */
			/* The input value in P3 might be of any type: integer, real, string,
			 ** blob, or NULL.  But it needs to be an integer before we can do
			 ** the seek, so convert it. */
			pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
			flags31 = (*TMem)(unsafe.Pointer(pIn3)).Fflags
			if int32(flags31)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Str)) == int32(MEM_Str) {
				_applyNumericAffinity(tls, pIn3, 0)
			}
			iKey = _sqlite3VdbeIntValue(tls, pIn3)         /* Get the integer key value */
			newType = (*TMem)(unsafe.Pointer(pIn3)).Fflags /* Record the type after applying numeric affinity */
			(*TMem)(unsafe.Pointer(pIn3)).Fflags = flags31 /* But convert the type back to its original */
			/* If the P3 value could not be converted into an integer without
			 ** loss of information, then special processing is required... */
			if int32(newType)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) == 0 {
				if int32(newType)&int32(MEM_Real) == 0 {
					if int32(newType)&int32(MEM_Null) != 0 || oc >= int32(OP_SeekGE) {
						goto jump_to_p2
					} else {
						rc = _sqlite3BtreeLast(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), bp+96)
						if rc != SQLITE_OK {
							goto abort_due_to_error
						}
						goto seek_not_found
					}
				}
				c2 = _sqlite3IntFloatCompare(tls, iKey, *(*float64)(unsafe.Pointer(pIn3)))
				/* If the approximation iKey is larger than the actual real search
				 ** term, substitute >= for > and < for <=. e.g. if the search term
				 ** is 4.9 and the integer approximation 5:
				 **
				 **        (x >  4.9)    ->     (x >= 5)
				 **        (x <= 4.9)    ->     (x <  5)
				 */
				if c2 > 0 {
					if oc&int32(0x0001) == libc.Int32FromInt32(OP_SeekGT)&libc.Int32FromInt32(0x0001) {
						oc = oc - 1
					}
				} else {
					if c2 < 0 {
						if oc&int32(0x0001) == libc.Int32FromInt32(OP_SeekLT)&libc.Int32FromInt32(0x0001) {
							oc = oc + 1
						}
					}
				}
			}
			rc = _sqlite3BtreeTableMoveto(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), int64(uint64(iKey)), 0, bp+96)
			(*TVdbeCursor)(unsafe.Pointer(pC5)).FmovetoTarget = iKey /* Used by OP_Delete */
			if rc != SQLITE_OK {
				goto abort_due_to_error
			}
		} else {
			/* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the
			 ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be
			 ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively,
			 ** with the same key.
			 */
			if _sqlite3BtreeCursorHasHint(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), uint32(BTREE_SEEK_EQ)) != 0 {
				eqOnly = int32(1)
			}
			nField2 = *(*int32)(unsafe.Pointer(pOp + 16))
			(**(**TUnpackedRecord)(__ccgo_up(bp + 104))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC5)).FpKeyInfo
			(**(**TUnpackedRecord)(__ccgo_up(bp + 104))).FnField = uint16(nField2)
			/* The next line of code computes as follows, only faster:
			 **   if( oc==OP_SeekGT || oc==OP_SeekLE ){
			 **     r.default_rc = -1;
			 **   }else{
			 **     r.default_rc = +1;
			 **   }
			 */
			if int32(1)&(oc-int32(OP_SeekLT)) != 0 {
				v190 = -int32(1)
			} else {
				v190 = +libc.Int32FromInt32(1)
			}
			(**(**TUnpackedRecord)(__ccgo_up(bp + 104))).Fdefault_rc = int8(v190)
			(**(**TUnpackedRecord)(__ccgo_up(bp + 104))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
			(**(**TUnpackedRecord)(__ccgo_up(bp + 104))).FeqSeen = uint8(0)
			rc = _sqlite3BtreeIndexMoveto(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), bp+104, bp+96)
			if rc != SQLITE_OK {
				goto abort_due_to_error
			}
			if eqOnly != 0 && int32((**(**TUnpackedRecord)(__ccgo_up(bp + 104))).FeqSeen) == 0 {
				goto seek_not_found
			}
		}
		if oc >= int32(OP_SeekGE) {
			if **(**int32)(__ccgo_up(bp + 96)) < 0 || **(**int32)(__ccgo_up(bp + 96)) == 0 && oc == int32(OP_SeekGT) {
				**(**int32)(__ccgo_up(bp + 96)) = 0
				rc = _sqlite3BtreeNext(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), 0)
				if rc != SQLITE_OK {
					if rc == int32(SQLITE_DONE) {
						rc = SQLITE_OK
						**(**int32)(__ccgo_up(bp + 96)) = int32(1)
					} else {
						goto abort_due_to_error
					}
				}
			} else {
				**(**int32)(__ccgo_up(bp + 96)) = 0
			}
		} else {
			if **(**int32)(__ccgo_up(bp + 96)) > 0 || **(**int32)(__ccgo_up(bp + 96)) == 0 && oc == int32(OP_SeekLT) {
				**(**int32)(__ccgo_up(bp + 96)) = 0
				rc = _sqlite3BtreePrevious(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)), 0)
				if rc != SQLITE_OK {
					if rc == int32(SQLITE_DONE) {
						rc = SQLITE_OK
						**(**int32)(__ccgo_up(bp + 96)) = int32(1)
					} else {
						goto abort_due_to_error
					}
				}
			} else {
				/* res might be negative because the table is empty.  Check to
				 ** see if this is the case.
				 */
				**(**int32)(__ccgo_up(bp + 96)) = _sqlite3BtreeEof(tls, *(*uintptr)(unsafe.Pointer(pC5 + 36)))
			}
		}
		goto seek_not_found
	seek_not_found:
		;
		if **(**int32)(__ccgo_up(bp + 96)) != 0 {
			goto jump_to_p2
		} else {
			if eqOnly != 0 {
				pOp += 20 /* Skip the OP_IdxLt or OP_IdxGT that follows */
			}
		}
		goto _189
		/* Opcode: SeekScan  P1 P2 * * P5
		 ** Synopsis: Scan-ahead up to P1 rows
		 **
		 ** This opcode is a prefix opcode to OP_SeekGE.  In other words, this
		 ** opcode must be immediately followed by OP_SeekGE. This constraint is
		 ** checked by assert() statements.
		 **
		 ** This opcode uses the P1 through P4 operands of the subsequent
		 ** OP_SeekGE.  In the text that follows, the operands of the subsequent
		 ** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4.   Only
		 ** the P1, P2 and P5 operands of this opcode are also used, and  are called
		 ** This.P1, This.P2 and This.P5.
		 **
		 ** This opcode helps to optimize IN operators on a multi-column index
		 ** where the IN operator is on the later terms of the index by avoiding
		 ** unnecessary seeks on the btree, substituting steps to the next row
		 ** of the b-tree instead.  A correct answer is obtained if this opcode
		 ** is omitted or is a no-op.
		 **
		 ** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which
		 ** is the desired entry that we want the cursor SeekGE.P1 to be pointing
		 ** to.  Call this SeekGE.P3/P4 row the "target".
		 **
		 ** If the SeekGE.P1 cursor is not currently pointing to a valid row,
		 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
		 **
		 ** If the SeekGE.P1 cursor is pointing to a valid row, then that row
		 ** might be the target row, or it might be near and slightly before the
		 ** target row, or it might be after the target row.  If the cursor is
		 ** currently before the target row, then this opcode attempts to position
		 ** the cursor on or after the target row by invoking sqlite3BtreeStep()
		 ** on the cursor between 1 and This.P1 times.
		 **
		 ** The This.P5 parameter is a flag that indicates what to do if the
		 ** cursor ends up pointing at a valid row that is past the target
		 ** row.  If This.P5 is false (0) then a jump is made to SeekGE.P2.  If
		 ** This.P5 is true (non-zero) then a jump is made to This.P2.  The P5==0
		 ** case occurs when there are no inequality constraints to the right of
		 ** the IN constraint.  The jump to SeekGE.P2 ends the loop.  The P5!=0 case
		 ** occurs when there are inequality constraints to the right of the IN
		 ** operator.  In that case, the This.P2 will point either directly to or
		 ** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for
		 ** loop terminate.
		 **
		 ** Possible outcomes from this opcode:<ol>
		 **
		 ** <li> If the cursor is initially not pointed to any valid row, then
		 **      fall through into the subsequent OP_SeekGE opcode.
		 **
		 ** <li> If the cursor is left pointing to a row that is before the target
		 **      row, even after making as many as This.P1 calls to
		 **      sqlite3BtreeNext(), then also fall through into OP_SeekGE.
		 **
		 ** <li> If the cursor is left pointing at the target row, either because it
		 **      was at the target row to begin with or because one or more
		 **      sqlite3BtreeNext() calls moved the cursor to the target row,
		 **      then jump to This.P2..,
		 **
		 ** <li> If the cursor started out before the target row and a call to
		 **      to sqlite3BtreeNext() moved the cursor off the end of the index
		 **      (indicating that the target row definitely does not exist in the
		 **      btree) then jump to SeekGE.P2, ending the loop.
		 **
		 ** <li> If the cursor ends up on a valid row that is past the target row
		 **      (indicating that the target row does not exist in the btree) then
		 **      jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0.
		 ** </ol>
		 */
	_89:
		;
		/* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
		 ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first
		 ** opcode past the OP_SeekGE itself.  */
		pC6 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((**(**TOp)(__ccgo_up(pOp + 1*20))).Fp1)*4))
		if !(_sqlite3BtreeCursorIsValidNN(tls, *(*uintptr)(unsafe.Pointer(pC6 + 36))) != 0) {
			goto _189
		}
		nStep = (*TOp)(unsafe.Pointer(pOp)).Fp1
		(**(**TUnpackedRecord)(__ccgo_up(bp + 144))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC6)).FpKeyInfo
		(**(**TUnpackedRecord)(__ccgo_up(bp + 144))).FnField = uint16(*(*int32)(unsafe.Pointer(pOp + 1*20 + 16)))
		(**(**TUnpackedRecord)(__ccgo_up(bp + 144))).Fdefault_rc = 0
		(**(**TUnpackedRecord)(__ccgo_up(bp + 144))).FaMem = aMem + uintptr((**(**TOp)(__ccgo_up(pOp + 1*20))).Fp3)*40
		**(**int32)(__ccgo_up(bp + 136)) = 0 /* Not needed.  Only used to silence a warning. */
	_260:
		;
		if !(int32(1) != 0) {
			goto _259
		}
		rc = _sqlite3VdbeIdxKeyCompare(tls, db, pC6, bp+144, bp+136)
		if rc != 0 {
			goto abort_due_to_error
		}
		if !(**(**int32)(__ccgo_up(bp + 136)) > 0 && int32((*TOp)(unsafe.Pointer(pOp)).Fp5) == 0) {
			goto _261
		}
		goto seekscan_search_fail
	seekscan_search_fail:
		;
		/* Jump to SeekGE.P2, ending the loop */
		pOp += 20
		goto jump_to_p2
	_261:
		;
		if **(**int32)(__ccgo_up(bp + 136)) >= 0 {
			/* Jump to This.P2, bypassing the OP_SeekGE opcode */
			goto jump_to_p2
			goto _259
		}
		if nStep <= 0 {
			goto _259
		}
		nStep = nStep - 1
		(*TVdbeCursor)(unsafe.Pointer(pC6)).FcacheStatus = uint32(CACHE_STALE)
		rc = _sqlite3BtreeNext(tls, *(*uintptr)(unsafe.Pointer(pC6 + 36)), 0)
		if rc != 0 {
			if rc == int32(SQLITE_DONE) {
				rc = SQLITE_OK
				goto seekscan_search_fail
			} else {
				goto abort_due_to_error
			}
		}
		goto _260
	_259:
		;
		goto _189
		/* Opcode: SeekHit P1 P2 P3 * *
		 ** Synopsis: set P2<=seekHit<=P3
		 **
		 ** Increase or decrease the seekHit value for cursor P1, if necessary,
		 ** so that it is no less than P2 and no greater than P3.
		 **
		 ** The seekHit integer represents the maximum of terms in an index for which
		 ** there is known to be at least one match.  If the seekHit value is smaller
		 ** than the total number of equality terms in an index lookup, then the
		 ** OP_IfNoHope opcode might run to see if the IN loop can be abandoned
		 ** early, thus saving work.  This is part of the IN-early-out optimization.
		 **
		 ** P1 must be a valid b-tree cursor.
		 */
	_90:
		;
		pC7 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if int32((*TVdbeCursor)(unsafe.Pointer(pC7)).FseekHit) < (*TOp)(unsafe.Pointer(pOp)).Fp2 {
			(*TVdbeCursor)(unsafe.Pointer(pC7)).FseekHit = uint16((*TOp)(unsafe.Pointer(pOp)).Fp2)
		} else {
			if int32((*TVdbeCursor)(unsafe.Pointer(pC7)).FseekHit) > (*TOp)(unsafe.Pointer(pOp)).Fp3 {
				(*TVdbeCursor)(unsafe.Pointer(pC7)).FseekHit = uint16((*TOp)(unsafe.Pointer(pOp)).Fp3)
			}
		}
		goto _189
		/* Opcode: IfNotOpen P1 P2 * * *
		 ** Synopsis: if( !csr[P1] ) goto P2
		 **
		 ** If cursor P1 is not open or if P1 is set to a NULL row using the
		 ** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through.
		 */
	_91:
		;
		pCur1 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if pCur1 == uintptr(0) || (*TVdbeCursor)(unsafe.Pointer(pCur1)).FnullRow != 0 {
			goto jump_to_p2_and_check_for_interrupt
		}
		goto _189
		/* Opcode: Found P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
		 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
		 ** record.
		 **
		 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
		 ** is a prefix of any entry in P1 then a jump is made to P2 and
		 ** P1 is left pointing at the matching entry.
		 **
		 ** This operation leaves the cursor in a state where it can be
		 ** advanced in the forward direction.  The Next instruction will work,
		 ** but not the Prev instruction.
		 **
		 ** See also: NotFound, NoConflict, NotExists. SeekGe
		 */
		/* Opcode: NotFound P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
		 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
		 ** record.
		 **
		 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
		 ** is not the prefix of any entry in P1 then a jump is made to P2.  If P1
		 ** does contain an entry whose prefix matches the P3/P4 record then control
		 ** falls through to the next instruction and P1 is left pointing at the
		 ** matching entry.
		 **
		 ** This operation leaves the cursor in a state where it cannot be
		 ** advanced in either direction.  In other words, the Next and Prev
		 ** opcodes do not work after this operation.
		 **
		 ** See also: Found, NotExists, NoConflict, IfNoHope
		 */
		/* Opcode: IfNoHope P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** Register P3 is the first of P4 registers that form an unpacked
		 ** record.  Cursor P1 is an index btree.  P2 is a jump destination.
		 ** In other words, the operands to this opcode are the same as the
		 ** operands to OP_NotFound and OP_IdxGT.
		 **
		 ** This opcode is an optimization attempt only.  If this opcode always
		 ** falls through, the correct answer is still obtained, but extra work
		 ** is performed.
		 **
		 ** A value of N in the seekHit flag of cursor P1 means that there exists
		 ** a key P3:N that will match some record in the index.  We want to know
		 ** if it is possible for a record P3:P4 to match some record in the
		 ** index.  If it is not possible, we can skip some work.  So if seekHit
		 ** is less than P4, attempt to find out if a match is possible by running
		 ** OP_NotFound.
		 **
		 ** This opcode is used in IN clause processing for a multi-column key.
		 ** If an IN clause is attached to an element of the key other than the
		 ** left-most element, and if there are no matches on the most recent
		 ** seek over the whole key, then it might be that one of the key element
		 ** to the left is prohibiting a match, and hence there is "no hope" of
		 ** any match regardless of how many IN clause elements are checked.
		 ** In such a case, we abandon the IN clause search early, using this
		 ** opcode.  The opcode name comes from the fact that the
		 ** jump is taken if there is "no hope" of achieving a match.
		 **
		 ** See also: NotFound, SeekHit
		 */
		/* Opcode: NoConflict P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
		 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
		 ** record.
		 **
		 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
		 ** contains any NULL value, jump immediately to P2.  If all terms of the
		 ** record are not-NULL then a check is done to determine if any row in the
		 ** P1 index btree has a matching key prefix.  If there are no matches, jump
		 ** immediately to P2.  If there is a match, fall through and leave the P1
		 ** cursor pointing to the matching row.
		 **
		 ** This opcode is similar to OP_NotFound with the exceptions that the
		 ** branch is always taken if any part of the search key input is NULL.
		 **
		 ** This operation leaves the cursor in a state where it cannot be
		 ** advanced in either direction.  In other words, the Next and Prev
		 ** opcodes do not work after this operation.
		 **
		 ** See also: NotFound, Found, NotExists
		 */
	_92:
		;
		pC8 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if int32((*TVdbeCursor)(unsafe.Pointer(pC8)).FseekHit) >= *(*int32)(unsafe.Pointer(pOp + 16)) {
			goto _189
		}
		/* Fall through into OP_NotFound */
	_95:
		; /* jump, in3, ncycle */
	_94:
		; /* jump, in3, ncycle */
	_93:
		;
		pC9 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		(**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		(**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FnField = uint16(*(*int32)(unsafe.Pointer(pOp + 16)))
		if int32((**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FnField) > 0 {
			/* Key values in an array of registers */
			(**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC9)).FpKeyInfo
			(**(**TUnpackedRecord)(__ccgo_up(bp + 176))).Fdefault_rc = 0
			rc = _sqlite3BtreeIndexMoveto(tls, *(*uintptr)(unsafe.Pointer(pC9 + 36)), bp+176, pC9+28)
		} else {
			/* Composite key generated by OP_MakeRecord */
			if int32((*TMem)(unsafe.Pointer((**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FaMem)).Fflags)&int32(MEM_Zero) != 0 {
				v190 = _sqlite3VdbeMemExpandBlob(tls, (**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FaMem)
			} else {
				v190 = 0
			}
			rc = v190
			if rc != 0 {
				goto no_mem
			}
			pIdxKey = _sqlite3VdbeAllocUnpackedRecord(tls, (*TVdbeCursor)(unsafe.Pointer(pC9)).FpKeyInfo)
			if pIdxKey == uintptr(0) {
				goto no_mem
			}
			_sqlite3VdbeRecordUnpack(tls, (*TMem)(unsafe.Pointer((**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FaMem)).Fn, (*TMem)(unsafe.Pointer((**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FaMem)).Fz, pIdxKey)
			(*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).Fdefault_rc = 0
			rc = _sqlite3BtreeIndexMoveto(tls, *(*uintptr)(unsafe.Pointer(pC9 + 36)), pIdxKey, pC9+28)
			_sqlite3DbFreeNN(tls, db, pIdxKey)
		}
		if rc != SQLITE_OK {
			goto abort_due_to_error
		}
		alreadyExists = libc.BoolInt32((*TVdbeCursor)(unsafe.Pointer(pC9)).FseekResult == 0)
		(*TVdbeCursor)(unsafe.Pointer(pC9)).FnullRow = uint8(int32(1) - alreadyExists)
		(*TVdbeCursor)(unsafe.Pointer(pC9)).FdeferredMoveto = uint8(0)
		(*TVdbeCursor)(unsafe.Pointer(pC9)).FcacheStatus = uint32(CACHE_STALE)
		if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Found) {
			if alreadyExists != 0 {
				goto jump_to_p2
			}
		} else {
			if !(alreadyExists != 0) {
				goto jump_to_p2
			}
			if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_NoConflict) {
				/* For the OP_NoConflict opcode, take the jump if any of the
				 ** input fields are NULL, since any key with a NULL will not
				 ** conflict */
				ii1 = 0
				for {
					if !(ii1 < int32((**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FnField)) {
						break
					}
					if int32((**(**TMem)(__ccgo_up((**(**TUnpackedRecord)(__ccgo_up(bp + 176))).FaMem + uintptr(ii1)*40))).Fflags)&int32(MEM_Null) != 0 {
						goto jump_to_p2
					}
					goto _263
				_263:
					;
					ii1 = ii1 + 1
				}
			}
			if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_IfNoHope) {
				(*TVdbeCursor)(unsafe.Pointer(pC9)).FseekHit = uint16(*(*int32)(unsafe.Pointer(pOp + 16)))
			}
		}
		goto _189
		/* Opcode: SeekRowid P1 P2 P3 * *
		 ** Synopsis: intkey=r[P3]
		 **
		 ** P1 is the index of a cursor open on an SQL table btree (with integer
		 ** keys).  If register P3 does not contain an integer or if P1 does not
		 ** contain a record with rowid P3 then jump immediately to P2.
		 ** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
		 ** a record with rowid P3 then
		 ** leave the cursor pointing at that record and fall through to the next
		 ** instruction.
		 **
		 ** The OP_NotExists opcode performs the same operation, but with OP_NotExists
		 ** the P3 register must be guaranteed to contain an integer value.  With this
		 ** opcode, register P3 might not contain an integer.
		 **
		 ** The OP_NotFound opcode performs the same operation on index btrees
		 ** (with arbitrary multi-value keys).
		 **
		 ** This opcode leaves the cursor in a state where it cannot be advanced
		 ** in either direction.  In other words, the Next and Prev opcodes will
		 ** not work following this opcode.
		 **
		 ** See also: Found, NotFound, NoConflict, SeekRowid
		 */
		/* Opcode: NotExists P1 P2 P3 * *
		 ** Synopsis: intkey=r[P3]
		 **
		 ** P1 is the index of a cursor open on an SQL table btree (with integer
		 ** keys).  P3 is an integer rowid.  If P1 does not contain a record with
		 ** rowid P3 then jump immediately to P2.  Or, if P2 is 0, raise an
		 ** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
		 ** leave the cursor pointing at that record and fall through to the next
		 ** instruction.
		 **
		 ** The OP_SeekRowid opcode performs the same operation but also allows the
		 ** P3 register to contain a non-integer value, in which case the jump is
		 ** always taken.  This opcode requires that P3 always contain an integer.
		 **
		 ** The OP_NotFound opcode performs the same operation on index btrees
		 ** (with arbitrary multi-value keys).
		 **
		 ** This opcode leaves the cursor in a state where it cannot be advanced
		 ** in either direction.  In other words, the Next and Prev opcodes will
		 ** not work following this opcode.
		 **
		 ** See also: Found, NotFound, NoConflict, SeekRowid
		 */
	_97:
		;
		pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		if int32((*TMem)(unsafe.Pointer(pIn3)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) == 0 {
			/* If pIn3->u.i does not contain an integer, compute iKey as the
			 ** integer value of pIn3.  Jump to P2 if pIn3 cannot be converted
			 ** into an integer without loss of information.  Take care to avoid
			 ** changing the datatype of pIn3, however, as it is used by other
			 ** parts of the prepared statement. */
			*(*TMem)(unsafe.Pointer(bp + 216)) = TMem{}
			/* If pIn3->u.i does not contain an integer, compute iKey as the
			 ** integer value of pIn3.  Jump to P2 if pIn3 cannot be converted
			 ** into an integer without loss of information.  Take care to avoid
			 ** changing the datatype of pIn3, however, as it is used by other
			 ** parts of the prepared statement. */
			*(*Tsqlite3_value)(unsafe.Pointer(bp + 216)) = **(**TMem)(__ccgo_up(pIn3))
			_applyAffinity(tls, bp+216, int8(SQLITE_AFF_NUMERIC), encoding)
			if int32((**(**TMem)(__ccgo_up(bp + 216))).Fflags)&int32(MEM_Int) == 0 {
				goto jump_to_p2
			}
			iKey1 = uint64(*(*Ti64)(unsafe.Pointer(bp + 216)))
			goto notExistsWithKey
		}
		/* Fall through into OP_NotExists */
	_96:
		; /* jump, in3, ncycle */
		pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		iKey1 = uint64(*(*Ti64)(unsafe.Pointer(pIn3)))
		goto notExistsWithKey
	notExistsWithKey:
		;
		pC10 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pCrsr2 = *(*uintptr)(unsafe.Pointer(pC10 + 36))
		**(**int32)(__ccgo_up(bp + 208)) = 0
		rc = _sqlite3BtreeTableMoveto(tls, pCrsr2, int64(iKey1), 0, bp+208)
		(*TVdbeCursor)(unsafe.Pointer(pC10)).FmovetoTarget = int64(iKey1) /* Used by OP_Delete */
		(*TVdbeCursor)(unsafe.Pointer(pC10)).FnullRow = uint8(0)
		(*TVdbeCursor)(unsafe.Pointer(pC10)).FcacheStatus = uint32(CACHE_STALE)
		(*TVdbeCursor)(unsafe.Pointer(pC10)).FdeferredMoveto = uint8(0)
		(*TVdbeCursor)(unsafe.Pointer(pC10)).FseekResult = **(**int32)(__ccgo_up(bp + 208))
		if **(**int32)(__ccgo_up(bp + 208)) != 0 {
			if (*TOp)(unsafe.Pointer(pOp)).Fp2 == 0 {
				rc = _sqlite3CorruptError(tls, int32(102154))
			} else {
				goto jump_to_p2
			}
		}
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: Sequence P1 P2 * * *
		 ** Synopsis: r[P2]=cursor[P1].ctr++
		 **
		 ** Find the next available sequence number for cursor P1.
		 ** Write the sequence number into register P2.
		 ** The sequence number on the cursor is incremented after this
		 ** instruction.
		 */
	_98:
		; /* out2 */
		pOut = _out2Prerelease(tls, p, pOp)
		v191 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4)) + 16
		v256 = *(*Ti64)(unsafe.Pointer(v191))
		*(*Ti64)(unsafe.Pointer(v191)) = *(*Ti64)(unsafe.Pointer(v191)) + 1
		*(*Ti64)(unsafe.Pointer(pOut)) = v256
		goto _189
		/* Opcode: NewRowid P1 P2 P3 * *
		 ** Synopsis: r[P2]=rowid
		 **
		 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
		 ** The record number is not previously used as a key in the database
		 ** table that cursor P1 points to.  The new record number is written
		 ** written to register P2.
		 **
		 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
		 ** the largest previously generated record number. No new record numbers are
		 ** allowed to be less than this value. When this value reaches its maximum,
		 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
		 ** generated record number. This P3 mechanism is used to help implement the
		 ** AUTOINCREMENT feature.
		 */
	_99:
		; /* Root frame of VDBE */
		**(**Ti64)(__ccgo_up(bp + 256)) = 0
		**(**int32)(__ccgo_up(bp + 264)) = 0
		pOut = _out2Prerelease(tls, p, pOp)
		pC11 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		/* The next rowid or record number (different terms for the same
		 ** thing) is obtained in a two-step algorithm.
		 **
		 ** First we attempt to find the largest existing rowid and add one
		 ** to that.  But if the largest existing rowid is already the maximum
		 ** positive integer, we have to fall through to the second
		 ** probabilistic algorithm
		 **
		 ** The second algorithm is to select a rowid at random and see if
		 ** it already exists in the table.  If it does not exist, we have
		 ** succeeded.  If the random rowid does exist, we select a new one
		 ** and try again, up to 100 times.
		 */
		/* Some compilers complain about constants of the form 0x7fffffffffffffff.
		 ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
		 ** to provide the constant while making all compilers happy.
		 */
		if !(int32(TBool(*(*uint8)(unsafe.Pointer(pC11 + 8))&0x2>>1)) != 0) {
			rc = _sqlite3BtreeLast(tls, *(*uintptr)(unsafe.Pointer(pC11 + 36)), bp+264)
			if rc != SQLITE_OK {
				goto abort_due_to_error
			}
			if **(**int32)(__ccgo_up(bp + 264)) != 0 {
				**(**Ti64)(__ccgo_up(bp + 256)) = int64(1) /* IMP: R-61914-48074 */
			} else {
				**(**Ti64)(__ccgo_up(bp + 256)) = _sqlite3BtreeIntegerKey(tls, *(*uintptr)(unsafe.Pointer(pC11 + 36)))
				if **(**Ti64)(__ccgo_up(bp + 256)) >= int64(libc.Uint64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)|libc.Uint64FromUint32(0xffffffff)) {
					libc.SetBitFieldPtr8Uint32(pC11+8, libc.Uint32FromInt32(1), 1, 0x2)
				} else {
					**(**Ti64)(__ccgo_up(bp + 256)) = **(**Ti64)(__ccgo_up(bp + 256)) + 1 /* IMP: R-29538-34987 */
				}
			}
		}
		if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 {
			/* Assert that P3 is a valid memory cell. */
			if (*TVdbe)(unsafe.Pointer(p)).FpFrame != 0 {
				pFrame1 = (*TVdbe)(unsafe.Pointer(p)).FpFrame
				for {
					if !((*TVdbeFrame)(unsafe.Pointer(pFrame1)).FpParent != 0) {
						break
					}
					goto _266
				_266:
					;
					pFrame1 = (*TVdbeFrame)(unsafe.Pointer(pFrame1)).FpParent
				}
				/* Assert that P3 is a valid memory cell. */
				pMem = (*TVdbeFrame)(unsafe.Pointer(pFrame1)).FaMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
			} else {
				/* Assert that P3 is a valid memory cell. */
				pMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
			}
			_sqlite3VdbeMemIntegerify(tls, pMem)
			/* mem(P3) holds an integer */
			if *(*Ti64)(unsafe.Pointer(pMem)) == int64(libc.Uint64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)|libc.Uint64FromUint32(0xffffffff)) || int32(TBool(*(*uint8)(unsafe.Pointer(pC11 + 8))&0x2>>1)) != 0 {
				rc = int32(SQLITE_FULL) /* IMP: R-17817-00630 */
				goto abort_due_to_error
			}
			if **(**Ti64)(__ccgo_up(bp + 256)) < *(*Ti64)(unsafe.Pointer(pMem))+int64(1) {
				**(**Ti64)(__ccgo_up(bp + 256)) = *(*Ti64)(unsafe.Pointer(pMem)) + int64(1)
			}
			*(*Ti64)(unsafe.Pointer(pMem)) = **(**Ti64)(__ccgo_up(bp + 256))
		}
		if int32(TBool(*(*uint8)(unsafe.Pointer(pC11 + 8))&0x2>>1)) != 0 {
			/* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
			 ** largest possible integer (9223372036854775807) then the database
			 ** engine starts picking positive candidate ROWIDs at random until
			 ** it finds one that is not previously used. */
			/* We cannot be in random rowid mode if this is
			 ** an AUTOINCREMENT table. */
			cnt1 = 0
			for {
				Xsqlite3_randomness(tls, int32(8), bp+256)
				**(**Ti64)(__ccgo_up(bp + 256)) = **(**Ti64)(__ccgo_up(bp + 256)) & (int64(libc.Uint64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)|libc.Uint64FromUint32(0xffffffff)) >> libc.Int32FromInt32(1))
				**(**Ti64)(__ccgo_up(bp + 256)) = **(**Ti64)(__ccgo_up(bp + 256)) + 1 /* Ensure that v is greater than zero */
				goto _270
			_270:
				;
				v190 = _sqlite3BtreeTableMoveto(tls, *(*uintptr)(unsafe.Pointer(pC11 + 36)), int64(uint64(**(**Ti64)(__ccgo_up(bp + 256)))), 0, bp+264)
				rc = v190
				if v217 = v190 == SQLITE_OK && **(**int32)(__ccgo_up(bp + 264)) == 0; v217 {
					cnt1 = cnt1 + 1
					v193 = cnt1
				}
				if !(v217 && v193 < int32(100)) {
					break
				}
			}
			if rc != 0 {
				goto abort_due_to_error
			}
			if **(**int32)(__ccgo_up(bp + 264)) == 0 {
				rc = int32(SQLITE_FULL) /* IMP: R-38219-53002 */
				goto abort_due_to_error
			}
			/* EV: R-40812-03570 */
		}
		(*TVdbeCursor)(unsafe.Pointer(pC11)).FdeferredMoveto = uint8(0)
		(*TVdbeCursor)(unsafe.Pointer(pC11)).FcacheStatus = uint32(CACHE_STALE)
		*(*Ti64)(unsafe.Pointer(pOut)) = **(**Ti64)(__ccgo_up(bp + 256))
		goto _189
		/* Opcode: Insert P1 P2 P3 P4 P5
		 ** Synopsis: intkey=r[P3] data=r[P2]
		 **
		 ** Write an entry into the table of cursor P1.  A new entry is
		 ** created if it doesn't already exist or the data for an existing
		 ** entry is overwritten.  The data is the value MEM_Blob stored in register
		 ** number P2. The key is stored in register P3. The key must
		 ** be a MEM_Int.
		 **
		 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
		 ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
		 ** then rowid is stored for subsequent return by the
		 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
		 **
		 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
		 ** run faster by avoiding an unnecessary seek on cursor P1.  However,
		 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
		 ** seeks on the cursor or if the most recent seek used a key equal to P3.
		 **
		 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
		 ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
		 ** is part of an INSERT operation.  The difference is only important to
		 ** the update hook.
		 **
		 ** Parameter P4 may point to a Table structure, or may be NULL. If it is
		 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
		 ** following a successful insert.
		 **
		 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
		 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
		 ** and register P2 becomes ephemeral.  If the cursor is changed, the
		 ** value of register P2 will then change.  Make sure this does not
		 ** cause any problems.)
		 **
		 ** This instruction only works on tables.  The equivalent instruction
		 ** for indices is OP_IdxInsert.
		 */
	_100:
		; /* Payload to be inserted */
		pData = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		pC12 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pKey = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		(**(**TBtreePayload)(__ccgo_up(bp + 272))).FnKey = *(*Ti64)(unsafe.Pointer(pKey))
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == -int32(5) && ((*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback != 0) {
			zDb = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TVdbeCursor)(unsafe.Pointer(pC12)).FiDb)*16))).FzDbSName
			pTab1 = *(*uintptr)(unsafe.Pointer(pOp + 16))
		} else {
			pTab1 = uintptr(0)
			zDb = uintptr(0)
		}
		/* Invoke the pre-update hook, if any */
		if pTab1 != 0 {
			if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 && !(int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&libc.Int32FromInt32(OPFLAG_ISUPDATE) != 0) {
				_sqlite3VdbePreUpdateHook(tls, p, pC12, int32(SQLITE_INSERT), zDb, pTab1, (**(**TBtreePayload)(__ccgo_up(bp + 272))).FnKey, (*TOp)(unsafe.Pointer(pOp)).Fp2, -int32(1))
			}
			if (*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback == uintptr(0) || (*TTable)(unsafe.Pointer(pTab1)).FaCol == uintptr(0) {
				/* Prevent post-update hook from running in cases when it should not */
				pTab1 = uintptr(0)
			}
		}
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_ISNOOP) != 0 {
			goto _189
		}
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_NCHANGE) != 0 {
			(*TVdbe)(unsafe.Pointer(p)).FnChange = (*TVdbe)(unsafe.Pointer(p)).FnChange + 1
			if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_LASTROWID) != 0 {
				(*Tsqlite3)(unsafe.Pointer(db)).FlastRowid = (**(**TBtreePayload)(__ccgo_up(bp + 272))).FnKey
			}
		}
		(**(**TBtreePayload)(__ccgo_up(bp + 272))).FpData = (*TMem)(unsafe.Pointer(pData)).Fz
		(**(**TBtreePayload)(__ccgo_up(bp + 272))).FnData = (*TMem)(unsafe.Pointer(pData)).Fn
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_USESEEKRESULT) != 0 {
			v190 = (*TVdbeCursor)(unsafe.Pointer(pC12)).FseekResult
		} else {
			v190 = 0
		}
		seekResult = v190
		if int32((*TMem)(unsafe.Pointer(pData)).Fflags)&int32(MEM_Zero) != 0 {
			(**(**TBtreePayload)(__ccgo_up(bp + 272))).FnZero = *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pData)).Fu))
		} else {
			(**(**TBtreePayload)(__ccgo_up(bp + 272))).FnZero = 0
		}
		(**(**TBtreePayload)(__ccgo_up(bp + 272))).FpKey = uintptr(0)
		rc = _sqlite3BtreeInsert(tls, *(*uintptr)(unsafe.Pointer(pC12 + 36)), bp+272, int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&(libc.Int32FromInt32(OPFLAG_APPEND)|libc.Int32FromInt32(OPFLAG_SAVEPOSITION)|libc.Int32FromInt32(OPFLAG_PREFORMAT)), seekResult)
		(*TVdbeCursor)(unsafe.Pointer(pC12)).FdeferredMoveto = uint8(0)
		(*TVdbeCursor)(unsafe.Pointer(pC12)).FcacheStatus = uint32(CACHE_STALE)
		colCacheCtr = colCacheCtr + 1
		/* Invoke the update-hook if required. */
		if rc != 0 {
			goto abort_due_to_error
		}
		if pTab1 != 0 {
			if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_ISUPDATE) != 0 {
				v190 = int32(SQLITE_UPDATE)
			} else {
				v190 = int32(SQLITE_INSERT)
			}
			(*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, Tsqlite_int64))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpUpdateArg, v190, zDb, (*TTable)(unsafe.Pointer(pTab1)).FzName, (**(**TBtreePayload)(__ccgo_up(bp + 272))).FnKey)
		}
		goto _189
		/* Opcode: RowCell P1 P2 P3 * *
		 **
		 ** P1 and P2 are both open cursors. Both must be opened on the same type
		 ** of table - intkey or index. This opcode is used as part of copying
		 ** the current row from P2 into P1. If the cursors are opened on intkey
		 ** tables, register P3 contains the rowid to use with the new record in
		 ** P1. If they are opened on index tables, P3 is not used.
		 **
		 ** This opcode must be followed by either an Insert or InsertIdx opcode
		 ** with the OPFLAG_PREFORMAT flag set to complete the insert operation.
		 */
	_101:
		; /* Rowid value to insert with */
		pDest1 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pSrc = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*4))
		if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 {
			v206 = *(*Ti64)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40))
		} else {
			v206 = 0
		}
		iKey2 = v206
		rc = _sqlite3BtreeTransferRow(tls, *(*uintptr)(unsafe.Pointer(pDest1 + 36)), *(*uintptr)(unsafe.Pointer(pSrc + 36)), iKey2)
		if rc != SQLITE_OK {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: Delete P1 P2 P3 P4 P5
		 **
		 ** Delete the record at which the P1 cursor is currently pointing.
		 **
		 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
		 ** the cursor will be left pointing at  either the next or the previous
		 ** record in the table. If it is left pointing at the next record, then
		 ** the next Next instruction will be a no-op. As a result, in this case
		 ** it is ok to delete a record from within a Next loop. If
		 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
		 ** left in an undefined state.
		 **
		 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
		 ** delete is one of several associated with deleting a table row and
		 ** all its associated index entries.  Exactly one of those deletes is
		 ** the "primary" delete.  The others are all on OPFLAG_FORDELETE
		 ** cursors or else are marked with the AUXDELETE flag.
		 **
		 ** If the OPFLAG_NCHANGE (0x01) flag of P2 (NB: P2 not P5) is set, then
		 ** the row change count is incremented (otherwise not).
		 **
		 ** If the OPFLAG_ISNOOP (0x40) flag of P2 (not P5!) is set, then the
		 ** pre-update-hook for deletes is run, but the btree is otherwise unchanged.
		 ** This happens when the OP_Delete is to be shortly followed by an OP_Insert
		 ** with the same key, causing the btree entry to be overwritten.
		 **
		 ** P1 must not be pseudo-table.  It has to be a real table with
		 ** multiple rows.
		 **
		 ** If P4 is not NULL then it points to a Table object. In this case either
		 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
		 ** have been positioned using OP_NotFound prior to invoking this opcode in
		 ** this case. Specifically, if one is configured, the pre-update hook is
		 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
		 ** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
		 **
		 ** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
		 ** of the memory cell that contains the value that the rowid of the row will
		 ** be set to by the update.
		 */
	_102:
		;
		opflags = (*TOp)(unsafe.Pointer(pOp)).Fp2
		pC13 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		/* If the update-hook or pre-update-hook will be invoked, set zDb to
		 ** the name of the db to pass as to it. Also set local pTab to a copy
		 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
		 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
		 ** VdbeCursor.movetoTarget to the current rowid.  */
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp4type) == -int32(5) && ((*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback != 0) {
			zDb1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TVdbeCursor)(unsafe.Pointer(pC13)).FiDb)*16))).FzDbSName
			pTab2 = *(*uintptr)(unsafe.Pointer(pOp + 16))
			if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_SAVEPOSITION) != 0 && (*TVdbeCursor)(unsafe.Pointer(pC13)).FisTable != 0 {
				(*TVdbeCursor)(unsafe.Pointer(pC13)).FmovetoTarget = _sqlite3BtreeIntegerKey(tls, *(*uintptr)(unsafe.Pointer(pC13 + 36)))
			}
		} else {
			zDb1 = uintptr(0)
			pTab2 = uintptr(0)
		}
		/* Invoke the pre-update-hook if required. */
		if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 && pTab2 != 0 {
			if opflags&int32(OPFLAG_ISUPDATE) != 0 {
				v190 = int32(SQLITE_UPDATE)
			} else {
				v190 = int32(SQLITE_DELETE)
			}
			_sqlite3VdbePreUpdateHook(tls, p, pC13, v190, zDb1, pTab2, (*TVdbeCursor)(unsafe.Pointer(pC13)).FmovetoTarget, (*TOp)(unsafe.Pointer(pOp)).Fp3, -int32(1))
		}
		if opflags&int32(OPFLAG_ISNOOP) != 0 {
			goto _189
		}
		/* Only flags that can be set are SAVEPOISTION and AUXDELETE */
		rc = _sqlite3BtreeDelete(tls, *(*uintptr)(unsafe.Pointer(pC13 + 36)), uint8((*TOp)(unsafe.Pointer(pOp)).Fp5))
		(*TVdbeCursor)(unsafe.Pointer(pC13)).FcacheStatus = uint32(CACHE_STALE)
		colCacheCtr = colCacheCtr + 1
		(*TVdbeCursor)(unsafe.Pointer(pC13)).FseekResult = 0
		if rc != 0 {
			goto abort_due_to_error
		}
		/* Invoke the update-hook if required. */
		if opflags&int32(OPFLAG_NCHANGE) != 0 {
			(*TVdbe)(unsafe.Pointer(p)).FnChange = (*TVdbe)(unsafe.Pointer(p)).FnChange + 1
			if (*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback != 0 && pTab2 != uintptr(0) && (*TTable)(unsafe.Pointer(pTab2)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
				(*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, Tsqlite_int64))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxUpdateCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpUpdateArg, int32(SQLITE_DELETE), zDb1, (*TTable)(unsafe.Pointer(pTab2)).FzName, (*TVdbeCursor)(unsafe.Pointer(pC13)).FmovetoTarget)
			}
		}
		goto _189
		/* Opcode: ResetCount * * * * *
		 **
		 ** The value of the change counter is copied to the database handle
		 ** change counter (returned by subsequent calls to sqlite3_changes()).
		 ** Then the VMs internal change counter resets to 0.
		 ** This is used by trigger programs.
		 */
	_103:
		;
		_sqlite3VdbeSetChanges(tls, db, (*TVdbe)(unsafe.Pointer(p)).FnChange)
		(*TVdbe)(unsafe.Pointer(p)).FnChange = 0
		goto _189
		/* Opcode: SorterCompare P1 P2 P3 P4
		 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
		 **
		 ** P1 is a sorter cursor. This instruction compares a prefix of the
		 ** record blob in register P3 against a prefix of the entry that
		 ** the sorter cursor currently points to.  Only the first P4 fields
		 ** of r[P3] and the sorter record are compared.
		 **
		 ** If either P3 or the sorter contains a NULL in one of their significant
		 ** fields (not counting the P4 fields at the end which are ignored) then
		 ** the comparison is assumed to be equal.
		 **
		 ** Fall through to next instruction if the two records compare equal to
		 ** each other.  Jump to P2 if they are different.
		 */
	_104:
		;
		pC14 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		nKeyCol = *(*int32)(unsafe.Pointer(pOp + 16))
		**(**int32)(__ccgo_up(bp + 312)) = 0
		rc = _sqlite3VdbeSorterCompare(tls, pC14, pIn3, nKeyCol, bp+312)
		if rc != 0 {
			goto abort_due_to_error
		}
		if **(**int32)(__ccgo_up(bp + 312)) != 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: SorterData P1 P2 P3 * *
		 ** Synopsis: r[P2]=data
		 **
		 ** Write into register P2 the current sorter data for sorter cursor P1.
		 ** Then clear the column header cache on cursor P3.
		 **
		 ** This opcode is normally used to move a record out of the sorter and into
		 ** a register that is the source for a pseudo-table cursor created using
		 ** OpenPseudo.  That pseudo-table cursor is the one that is identified by
		 ** parameter P3.  Clearing the P3 column cache as part of this opcode saves
		 ** us from having to issue a separate NullRow instruction to clear that cache.
		 */
	_105:
		;
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		pC15 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		rc = _sqlite3VdbeSorterRowkey(tls, pC15, pOut)
		if rc != 0 {
			goto abort_due_to_error
		}
		(*TVdbeCursor)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*4)))).FcacheStatus = uint32(CACHE_STALE)
		goto _189
		/* Opcode: RowData P1 P2 P3 * *
		 ** Synopsis: r[P2]=data
		 **
		 ** Write into register P2 the complete row content for the row at
		 ** which cursor P1 is currently pointing.
		 ** There is no interpretation of the data.
		 ** It is just copied onto the P2 register exactly as
		 ** it is found in the database file.
		 **
		 ** If cursor P1 is an index, then the content is the key of the row.
		 ** If cursor P2 is a table, then the content extracted is the data.
		 **
		 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
		 ** of a real table, not a pseudo-table.
		 **
		 ** If P3!=0 then this opcode is allowed to make an ephemeral pointer
		 ** into the database page.  That means that the content of the output
		 ** register will be invalidated as soon as the cursor moves - including
		 ** moves caused by other cursors that "save" the current cursors
		 ** position in order that they can write to the same table.  If P3==0
		 ** then a copy of the data is made into memory.  P3!=0 is faster, but
		 ** P3==0 is safer.
		 **
		 ** If P3!=0 then the content of the P2 register is unsuitable for use
		 ** in OP_Result and any OP_Result will invalidate the P2 register content.
		 ** The P2 register content is invalidated by opcodes like OP_Function or
		 ** by any use of another cursor pointing to the same table.
		 */
	_106:
		;
		pOut = _out2Prerelease(tls, p, pOp)
		pC16 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pCrsr3 = *(*uintptr)(unsafe.Pointer(pC16 + 36))
		/* The OP_RowData opcodes always follow OP_NotExists or
		 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
		 ** that might invalidate the cursor.
		 ** If this were not the case, one of the following assert()s
		 ** would fail.  Should this ever change (because of changes in the code
		 ** generator) then the fix would be to insert a call to
		 ** sqlite3VdbeCursorMoveto().
		 */
		n3 = _sqlite3BtreePayloadSize(tls, pCrsr3)
		if n3 > uint32(**(**int32)(__ccgo_up(db + 120))) {
			goto too_big
		}
		rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, pCrsr3, n3, pOut)
		if rc != 0 {
			goto abort_due_to_error
		}
		if !((*TOp)(unsafe.Pointer(pOp)).Fp3 != 0) {
			if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Ephem) != 0 && _sqlite3VdbeMemMakeWriteable(tls, pOut) != 0 {
				goto no_mem
			}
		}
		goto _189
		/* Opcode: Rowid P1 P2 * * *
		 ** Synopsis: r[P2]=PX rowid of P1
		 **
		 ** Store in register P2 an integer which is the key of the table entry that
		 ** P1 is currently point to.
		 **
		 ** P1 can be either an ordinary table or a virtual table.  There used to
		 ** be a separate OP_VRowid opcode for use with virtual tables, but this
		 ** one opcode now works for both table types.
		 */
	_107:
		;
		pOut = _out2Prerelease(tls, p, pOp)
		pC17 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if (*TVdbeCursor)(unsafe.Pointer(pC17)).FnullRow != 0 {
			(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Null)
			goto _189
		} else {
			if (*TVdbeCursor)(unsafe.Pointer(pC17)).FdeferredMoveto != 0 {
				**(**Ti64)(__ccgo_up(bp + 320)) = (*TVdbeCursor)(unsafe.Pointer(pC17)).FmovetoTarget
			} else {
				if int32((*TVdbeCursor)(unsafe.Pointer(pC17)).FeCurType) == int32(CURTYPE_VTAB) {
					pVtab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pC17 + 36)))).FpVtab
					pModule = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule
					rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule)).FxRowid})))(tls, *(*uintptr)(unsafe.Pointer(pC17 + 36)), bp+320)
					_sqlite3VtabImportErrmsg(tls, p, pVtab)
					if rc != 0 {
						goto abort_due_to_error
					}
				} else {
					rc = _sqlite3VdbeCursorRestore(tls, pC17)
					if rc != 0 {
						goto abort_due_to_error
					}
					if (*TVdbeCursor)(unsafe.Pointer(pC17)).FnullRow != 0 {
						(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Null)
						goto _189
					}
					**(**Ti64)(__ccgo_up(bp + 320)) = _sqlite3BtreeIntegerKey(tls, *(*uintptr)(unsafe.Pointer(pC17 + 36)))
				}
			}
		}
		*(*Ti64)(unsafe.Pointer(pOut)) = **(**Ti64)(__ccgo_up(bp + 320))
		goto _189
		/* Opcode: NullRow P1 * * * *
		 **
		 ** Move the cursor P1 to a null row.  Any OP_Column operations
		 ** that occur while the cursor is on the null row will always
		 ** write a NULL.
		 **
		 ** If cursor P1 is not previously opened, open it now to a special
		 ** pseudo-cursor that always returns NULL for every column.
		 */
	_108:
		;
		pC18 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if pC18 == uintptr(0) {
			/* If the cursor is not already open, create a special kind of
			 ** pseudo-cursor that always gives null rows. */
			pC18 = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, int32(1), uint8(CURTYPE_PSEUDO))
			if pC18 == uintptr(0) {
				goto no_mem
			}
			(*TVdbeCursor)(unsafe.Pointer(pC18)).FseekResult = 0
			(*TVdbeCursor)(unsafe.Pointer(pC18)).FisTable = uint8(1)
			libc.SetBitFieldPtr8Uint32(pC18+8, libc.Uint32FromInt32(1), 3, 0x8)
			*(*uintptr)(unsafe.Pointer(pC18 + 36)) = _sqlite3BtreeFakeValidCursor(tls)
		}
		(*TVdbeCursor)(unsafe.Pointer(pC18)).FnullRow = uint8(1)
		(*TVdbeCursor)(unsafe.Pointer(pC18)).FcacheStatus = uint32(CACHE_STALE)
		if int32((*TVdbeCursor)(unsafe.Pointer(pC18)).FeCurType) == CURTYPE_BTREE {
			_sqlite3BtreeClearCursor(tls, *(*uintptr)(unsafe.Pointer(pC18 + 36)))
		}
		goto _189
		/* Opcode: SeekEnd P1 * * * *
		 **
		 ** Position cursor P1 at the end of the btree for the purpose of
		 ** appending a new entry onto the btree.
		 **
		 ** It is assumed that the cursor is used only for appending and so
		 ** if the cursor is valid, then the cursor must already be pointing
		 ** at the end of the btree and so no changes are made to
		 ** the cursor.
		 */
		/* Opcode: Last P1 P2 * * *
		 **
		 ** The next use of the Rowid or Column or Prev instruction for P1
		 ** will refer to the last entry in the database table or index.
		 ** If the table or index is empty and P2>0, then jump immediately to P2.
		 ** If P2 is 0 or if the table or index is not empty, fall through
		 ** to the following instruction.
		 **
		 ** This opcode leaves the cursor configured to move in reverse order,
		 ** from the end toward the beginning.  In other words, the cursor is
		 ** configured to use Prev, not Next.
		 */
	_110:
		; /* ncycle */
	_109:
		;
		pC19 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pCrsr4 = *(*uintptr)(unsafe.Pointer(pC19 + 36))
		**(**int32)(__ccgo_up(bp + 328)) = 0
		if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_SeekEnd) {
			(*TVdbeCursor)(unsafe.Pointer(pC19)).FseekResult = -int32(1)
			if _sqlite3BtreeCursorIsValidNN(tls, pCrsr4) != 0 {
				goto _189
			}
		}
		rc = _sqlite3BtreeLast(tls, pCrsr4, bp+328)
		(*TVdbeCursor)(unsafe.Pointer(pC19)).FnullRow = uint8(**(**int32)(__ccgo_up(bp + 328)))
		(*TVdbeCursor)(unsafe.Pointer(pC19)).FdeferredMoveto = uint8(0)
		(*TVdbeCursor)(unsafe.Pointer(pC19)).FcacheStatus = uint32(CACHE_STALE)
		if rc != 0 {
			goto abort_due_to_error
		}
		if (*TOp)(unsafe.Pointer(pOp)).Fp2 > 0 {
			if **(**int32)(__ccgo_up(bp + 328)) != 0 {
				goto jump_to_p2
			}
		}
		goto _189
		/* Opcode: IfSizeBetween P1 P2 P3 P4 *
		 **
		 ** Let N be the approximate number of rows in the table or index
		 ** with cursor P1 and let X be 10*log2(N) if N is positive or -1
		 ** if N is zero.
		 **
		 ** Jump to P2 if X is in between P3 and P4, inclusive.
		 */
	_111:
		;
		pC20 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pCrsr5 = *(*uintptr)(unsafe.Pointer(pC20 + 36))
		rc = _sqlite3BtreeFirst(tls, pCrsr5, bp+332)
		if rc != 0 {
			goto abort_due_to_error
		}
		if **(**int32)(__ccgo_up(bp + 332)) != 0 {
			sz = int64(-int32(1)) /* -Infinity encoding */
		} else {
			sz = _sqlite3BtreeRowCountEst(tls, pCrsr5)
			sz = int64(_sqlite3LogEst(tls, uint64(sz)))
		}
		**(**int32)(__ccgo_up(bp + 332)) = libc.BoolInt32(sz >= int64((*TOp)(unsafe.Pointer(pOp)).Fp3) && sz <= int64(*(*int32)(unsafe.Pointer(pOp + 16))))
		if **(**int32)(__ccgo_up(bp + 332)) != 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: SorterSort P1 P2 * * *
		 **
		 ** After all records have been inserted into the Sorter object
		 ** identified by P1, invoke this opcode to actually do the sorting.
		 ** Jump to P2 if there are no records to be sorted.
		 **
		 ** This opcode is an alias for OP_Sort and OP_Rewind that is used
		 ** for Sorter objects.
		 */
		/* Opcode: Sort P1 P2 * * *
		 **
		 ** This opcode does exactly the same thing as OP_Rewind except that
		 ** it increments an undocumented global variable used for testing.
		 **
		 ** Sorting is accomplished by writing records into a sorting index,
		 ** then rewinding that index and playing it back from beginning to
		 ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
		 ** rewinding so that the global variable will be incremented and
		 ** regression tests can determine whether or not the optimizer is
		 ** correctly optimizing out sorts.
		 */
	_113:
		; /* jump ncycle */
	_112:
		; /* jump ncycle */
		**(**Tu32)(__ccgo_up(p + 164 + 2*4)) = **(**Tu32)(__ccgo_up(p + 164 + 2*4)) + 1
		/* Fall through into OP_Rewind */
		/* Opcode: Rewind P1 P2 * * *
		 **
		 ** The next use of the Rowid or Column or Next instruction for P1
		 ** will refer to the first entry in the database table or index.
		 ** If the table or index is empty, jump immediately to P2.
		 ** If the table or index is not empty, fall through to the following
		 ** instruction.
		 **
		 ** If P2 is zero, that is an assertion that the P1 table is never
		 ** empty and hence the jump will never be taken.
		 **
		 ** This opcode leaves the cursor configured to move in forward order,
		 ** from the beginning toward the end.  In other words, the cursor is
		 ** configured to use Next, not Prev.
		 */
	_114:
		;
		pC21 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		**(**int32)(__ccgo_up(bp + 336)) = int32(1)
		if int32((*TVdbeCursor)(unsafe.Pointer(pC21)).FeCurType) == int32(CURTYPE_SORTER) {
			rc = _sqlite3VdbeSorterRewind(tls, pC21, bp+336)
		} else {
			pCrsr6 = *(*uintptr)(unsafe.Pointer(pC21 + 36))
			rc = _sqlite3BtreeFirst(tls, pCrsr6, bp+336)
			(*TVdbeCursor)(unsafe.Pointer(pC21)).FdeferredMoveto = uint8(0)
			(*TVdbeCursor)(unsafe.Pointer(pC21)).FcacheStatus = uint32(CACHE_STALE)
		}
		if rc != 0 {
			goto abort_due_to_error
		}
		(*TVdbeCursor)(unsafe.Pointer(pC21)).FnullRow = uint8(**(**int32)(__ccgo_up(bp + 336)))
		if (*TOp)(unsafe.Pointer(pOp)).Fp2 > 0 {
			if **(**int32)(__ccgo_up(bp + 336)) != 0 {
				goto jump_to_p2
			}
		}
		goto _189
		/* Opcode: IfEmpty P1 P2 * * *
		 ** Synopsis: if( empty(P1) ) goto P2
		 **
		 ** Check to see if the b-tree table that cursor P1 references is empty
		 ** and jump to P2 if it is.
		 */
	_115:
		;
		pC22 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pCrsr7 = *(*uintptr)(unsafe.Pointer(pC22 + 36))
		rc = _sqlite3BtreeIsEmpty(tls, pCrsr7, bp+340)
		if rc != 0 {
			goto abort_due_to_error
		}
		if **(**int32)(__ccgo_up(bp + 340)) != 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: Next P1 P2 P3 * P5
		 **
		 ** Advance cursor P1 so that it points to the next key/data pair in its
		 ** table or index.  If there are no more key/value pairs then fall through
		 ** to the following instruction.  But if the cursor advance was successful,
		 ** jump immediately to P2.
		 **
		 ** The Next opcode is only valid following an SeekGT, SeekGE, or
		 ** OP_Rewind opcode used to position the cursor.  Next is not allowed
		 ** to follow SeekLT, SeekLE, or OP_Last.
		 **
		 ** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
		 ** been opened prior to this opcode or the program will segfault.
		 **
		 ** The P3 value is a hint to the btree implementation. If P3==1, that
		 ** means P1 is an SQL index and that this instruction could have been
		 ** omitted if that index had been unique.  P3 is usually 0.  P3 is
		 ** always either 0 or 1.
		 **
		 ** If P5 is positive and the jump is taken, then event counter
		 ** number P5-1 in the prepared statement is incremented.
		 **
		 ** See also: Prev
		 */
		/* Opcode: Prev P1 P2 P3 * P5
		 **
		 ** Back up cursor P1 so that it points to the previous key/data pair in its
		 ** table or index.  If there is no previous key/value pairs then fall through
		 ** to the following instruction.  But if the cursor backup was successful,
		 ** jump immediately to P2.
		 **
		 **
		 ** The Prev opcode is only valid following an SeekLT, SeekLE, or
		 ** OP_Last opcode used to position the cursor.  Prev is not allowed
		 ** to follow SeekGT, SeekGE, or OP_Rewind.
		 **
		 ** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
		 ** not open then the behavior is undefined.
		 **
		 ** The P3 value is a hint to the btree implementation. If P3==1, that
		 ** means P1 is an SQL index and that this instruction could have been
		 ** omitted if that index had been unique.  P3 is usually 0.  P3 is
		 ** always either 0 or 1.
		 **
		 ** If P5 is positive and the jump is taken, then event counter
		 ** number P5-1 in the prepared statement is incremented.
		 */
		/* Opcode: SorterNext P1 P2 * * P5
		 **
		 ** This opcode works just like OP_Next except that P1 must be a
		 ** sorter object for which the OP_SorterSort opcode has been
		 ** invoked.  This opcode advances the cursor to the next sorted
		 ** record, or jumps to P2 if there are no more sorted records.
		 */
	_118:
		;
		pC23 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		rc = _sqlite3VdbeSorterNext(tls, db, pC23)
		goto next_tail
	_116:
		; /* jump, ncycle */
		pC23 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		rc = _sqlite3BtreePrevious(tls, *(*uintptr)(unsafe.Pointer(pC23 + 36)), (*TOp)(unsafe.Pointer(pOp)).Fp3)
		goto next_tail
	_117:
		; /* jump, ncycle */
		pC23 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		rc = _sqlite3BtreeNext(tls, *(*uintptr)(unsafe.Pointer(pC23 + 36)), (*TOp)(unsafe.Pointer(pOp)).Fp3)
		goto next_tail
	next_tail:
		;
		(*TVdbeCursor)(unsafe.Pointer(pC23)).FcacheStatus = uint32(CACHE_STALE)
		if rc == SQLITE_OK {
			(*TVdbeCursor)(unsafe.Pointer(pC23)).FnullRow = uint8(0)
			**(**Tu32)(__ccgo_up(p + 164 + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp5)*4)) = **(**Tu32)(__ccgo_up(p + 164 + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp5)*4)) + 1
			goto jump_to_p2_and_check_for_interrupt
		}
		if rc != int32(SQLITE_DONE) {
			goto abort_due_to_error
		}
		rc = SQLITE_OK
		(*TVdbeCursor)(unsafe.Pointer(pC23)).FnullRow = uint8(1)
		goto check_for_interrupt
		/* Opcode: IdxInsert P1 P2 P3 P4 P5
		 ** Synopsis: key=r[P2]
		 **
		 ** Register P2 holds an SQL index key made using the
		 ** MakeRecord instructions.  This opcode writes that key
		 ** into the index P1.  Data for the entry is nil.
		 **
		 ** If P4 is not zero, then it is the number of values in the unpacked
		 ** key of reg(P2).  In that case, P3 is the index of the first register
		 ** for the unpacked key.  The availability of the unpacked key can sometimes
		 ** be an optimization.
		 **
		 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
		 ** that this insert is likely to be an append.
		 **
		 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
		 ** incremented by this instruction.  If the OPFLAG_NCHANGE bit is clear,
		 ** then the change counter is unchanged.
		 **
		 ** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
		 ** run faster by avoiding an unnecessary seek on cursor P1.  However,
		 ** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
		 ** seeks on the cursor or if the most recent seek used a key equivalent
		 ** to P2.
		 **
		 ** This instruction only works for indices.  The equivalent instruction
		 ** for tables is OP_Insert.
		 */
	_119:
		;
		pC24 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_NCHANGE) != 0 {
			(*TVdbe)(unsafe.Pointer(p)).FnChange = (*TVdbe)(unsafe.Pointer(p)).FnChange + 1
		}
		if int32((*TMem)(unsafe.Pointer(pIn2)).Fflags)&int32(MEM_Zero) != 0 {
			v190 = _sqlite3VdbeMemExpandBlob(tls, pIn2)
		} else {
			v190 = 0
		}
		rc = v190
		if rc != 0 {
			goto abort_due_to_error
		}
		(**(**TBtreePayload)(__ccgo_up(bp + 344))).FnKey = int64((*TMem)(unsafe.Pointer(pIn2)).Fn)
		(**(**TBtreePayload)(__ccgo_up(bp + 344))).FpKey = (*TMem)(unsafe.Pointer(pIn2)).Fz
		(**(**TBtreePayload)(__ccgo_up(bp + 344))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		(**(**TBtreePayload)(__ccgo_up(bp + 344))).FnMem = uint16(*(*int32)(unsafe.Pointer(pOp + 16)))
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_USESEEKRESULT) != 0 {
			v190 = (*TVdbeCursor)(unsafe.Pointer(pC24)).FseekResult
		} else {
			v190 = 0
		}
		rc = _sqlite3BtreeInsert(tls, *(*uintptr)(unsafe.Pointer(pC24 + 36)), bp+344, int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&(libc.Int32FromInt32(OPFLAG_APPEND)|libc.Int32FromInt32(OPFLAG_SAVEPOSITION)|libc.Int32FromInt32(OPFLAG_PREFORMAT)), v190)
		(*TVdbeCursor)(unsafe.Pointer(pC24)).FcacheStatus = uint32(CACHE_STALE)
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: SorterInsert P1 P2 * * *
		 ** Synopsis: key=r[P2]
		 **
		 ** Register P2 holds an SQL index key made using the
		 ** MakeRecord instructions.  This opcode writes that key
		 ** into the sorter P1.  Data for the entry is nil.
		 */
	_120:
		;
		pC25 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		if int32((*TMem)(unsafe.Pointer(pIn2)).Fflags)&int32(MEM_Zero) != 0 {
			v190 = _sqlite3VdbeMemExpandBlob(tls, pIn2)
		} else {
			v190 = 0
		}
		rc = v190
		if rc != 0 {
			goto abort_due_to_error
		}
		rc = _sqlite3VdbeSorterWrite(tls, pC25, pIn2)
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: IdxDelete P1 P2 P3 P4 *
		 ** Synopsis: key=r[P2@P3]
		 **
		 ** The content of P3 registers starting at register P2 form
		 ** an unpacked index key. This opcode removes that entry from the
		 ** index opened by cursor P1.
		 **
		 ** P4 is a pointer to an Index structure.
		 **
		 ** Raise an SQLITE_CORRUPT_INDEX error if no matching index entry is found
		 ** and not in writable_schema mode.
		 */
	_121:
		;
		pC26 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pCrsr8 = *(*uintptr)(unsafe.Pointer(pC26 + 36))
		(**(**TUnpackedRecord)(__ccgo_up(bp + 392))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC26)).FpKeyInfo
		(**(**TUnpackedRecord)(__ccgo_up(bp + 392))).FnField = uint16((*TOp)(unsafe.Pointer(pOp)).Fp3)
		(**(**TUnpackedRecord)(__ccgo_up(bp + 392))).Fdefault_rc = 0
		(**(**TUnpackedRecord)(__ccgo_up(bp + 392))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		rc = _sqlite3BtreeIndexMoveto(tls, pCrsr8, bp+392, bp+384)
		if rc != 0 {
			goto abort_due_to_error
		}
		if **(**int32)(__ccgo_up(bp + 384)) != 0 {
			rc = _sqlite3VdbeFindIndexKey(tls, pCrsr8, *(*uintptr)(unsafe.Pointer(pOp + 16)), bp+392, bp+384, 0)
			if rc != SQLITE_OK {
				goto abort_due_to_error
			}
			if **(**int32)(__ccgo_up(bp + 384)) != 0 {
				if !(_sqlite3WritableSchema(tls, db) != 0) {
					rc = _sqlite3ReportError(tls, libc.Int32FromInt32(SQLITE_CORRUPT)|libc.Int32FromInt32(3)<<libc.Int32FromInt32(8), int32(103271), __ccgo_ts+7484)
					goto abort_due_to_error
				}
				(*TVdbeCursor)(unsafe.Pointer(pC26)).FcacheStatus = uint32(CACHE_STALE)
				(*TVdbeCursor)(unsafe.Pointer(pC26)).FseekResult = 0
				goto _189
			}
		}
		rc = _sqlite3BtreeDelete(tls, pCrsr8, uint8(BTREE_AUXDELETE))
		if rc != 0 {
			goto abort_due_to_error
		}
		(*TVdbeCursor)(unsafe.Pointer(pC26)).FcacheStatus = uint32(CACHE_STALE)
		(*TVdbeCursor)(unsafe.Pointer(pC26)).FseekResult = 0
		goto _189
		/* Opcode: DeferredSeek P1 * P3 P4 *
		 ** Synopsis: Move P3 to P1.rowid if needed
		 **
		 ** P1 is an open index cursor and P3 is a cursor on the corresponding
		 ** table.  This opcode does a deferred seek of the P3 table cursor
		 ** to the row that corresponds to the current row of P1.
		 **
		 ** This is a deferred seek.  Nothing actually happens until
		 ** the cursor is used to read a record.  That way, if no reads
		 ** occur, no unnecessary I/O happens.
		 **
		 ** P4 may be an array of integers (type P4_INTARRAY) containing
		 ** one entry for each column in the P3 table.  If array entry a(i)
		 ** is non-zero, then reading column a(i)-1 from cursor P3 is
		 ** equivalent to performing the deferred seek and then reading column i
		 ** from P1.  This information is stored in P3 and used to redirect
		 ** reads against P3 over to P1, thus possibly avoiding the need to
		 ** seek and read cursor P3.
		 */
		/* Opcode: IdxRowid P1 P2 * * *
		 ** Synopsis: r[P2]=rowid
		 **
		 ** Write into register P2 an integer which is the last entry in the record at
		 ** the end of the index key pointed to by cursor P1.  This integer should be
		 ** the rowid of the table entry to which this index entry points.
		 **
		 ** See also: Rowid, MakeRecord.
		 */
	_123:
		; /* ncycle */
	_122:
		; /* Rowid that P1 current points to */
		pC27 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		/* The IdxRowid and Seek opcodes are combined because of the commonality
		 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
		rc = _sqlite3VdbeCursorRestore(tls, pC27)
		/* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed
		 ** since it was last positioned and an error (e.g. OOM or an IO error)
		 ** occurs while trying to reposition it. */
		if rc != SQLITE_OK {
			goto abort_due_to_error
		}
		if !((*TVdbeCursor)(unsafe.Pointer(pC27)).FnullRow != 0) {
			**(**Ti64)(__ccgo_up(bp + 424)) = 0 /* Not needed.  Only used to silence a warning. */
			rc = _sqlite3VdbeIdxRowid(tls, db, *(*uintptr)(unsafe.Pointer(pC27 + 36)), bp+424)
			if rc != SQLITE_OK {
				goto abort_due_to_error
			}
			if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_DeferredSeek) {
				pTabCur = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*4))
				(*TVdbeCursor)(unsafe.Pointer(pTabCur)).FnullRow = uint8(0)
				(*TVdbeCursor)(unsafe.Pointer(pTabCur)).FmovetoTarget = **(**Ti64)(__ccgo_up(bp + 424))
				(*TVdbeCursor)(unsafe.Pointer(pTabCur)).FdeferredMoveto = uint8(1)
				(*TVdbeCursor)(unsafe.Pointer(pTabCur)).FcacheStatus = uint32(CACHE_STALE)
				*(*uintptr)(unsafe.Pointer(pTabCur + 12)) = *(*uintptr)(unsafe.Pointer(pOp + 16))
				(*TVdbeCursor)(unsafe.Pointer(pTabCur)).FpAltCursor = pC27
			} else {
				pOut = _out2Prerelease(tls, p, pOp)
				*(*Ti64)(unsafe.Pointer(pOut)) = **(**Ti64)(__ccgo_up(bp + 424))
			}
		} else {
			_sqlite3VdbeMemSetNull(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40)
		}
		goto _189
		/* Opcode: FinishSeek P1 * * * *
		 **
		 ** If cursor P1 was previously moved via OP_DeferredSeek, complete that
		 ** seek operation now, without further delay.  If the cursor seek has
		 ** already occurred, this instruction is a no-op.
		 */
	_124:
		; /* The P1 index cursor */
		pC28 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if (*TVdbeCursor)(unsafe.Pointer(pC28)).FdeferredMoveto != 0 {
			rc = _sqlite3VdbeFinishMoveto(tls, pC28)
			if rc != 0 {
				goto abort_due_to_error
			}
		}
		goto _189
		/* Opcode: IdxGE P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** The P4 register values beginning with P3 form an unpacked index
		 ** key that omits the PRIMARY KEY.  Compare this key value against the index
		 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
		 ** fields at the end.
		 **
		 ** If the P1 index entry is greater than or equal to the key value
		 ** then jump to P2.  Otherwise fall through to the next instruction.
		 */
		/* Opcode: IdxGT P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** The P4 register values beginning with P3 form an unpacked index
		 ** key that omits the PRIMARY KEY.  Compare this key value against the index
		 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
		 ** fields at the end.
		 **
		 ** If the P1 index entry is greater than the key value
		 ** then jump to P2.  Otherwise fall through to the next instruction.
		 */
		/* Opcode: IdxLT P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** The P4 register values beginning with P3 form an unpacked index
		 ** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
		 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
		 ** ROWID on the P1 index.
		 **
		 ** If the P1 index entry is less than the key value then jump to P2.
		 ** Otherwise fall through to the next instruction.
		 */
		/* Opcode: IdxLE P1 P2 P3 P4 *
		 ** Synopsis: key=r[P3@P4]
		 **
		 ** The P4 register values beginning with P3 form an unpacked index
		 ** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
		 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
		 ** ROWID on the P1 index.
		 **
		 ** If the P1 index entry is less than or equal to the key value then jump
		 ** to P2. Otherwise fall through to the next instruction.
		 */
	_128:
		; /* jump, ncycle */
	_127:
		; /* jump, ncycle */
	_126:
		; /* jump, ncycle */
	_125:
		;
		pC29 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		(**(**TUnpackedRecord)(__ccgo_up(bp + 432))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC29)).FpKeyInfo
		(**(**TUnpackedRecord)(__ccgo_up(bp + 432))).FnField = uint16(*(*int32)(unsafe.Pointer(pOp + 16)))
		if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) < int32(OP_IdxLT) {
			(**(**TUnpackedRecord)(__ccgo_up(bp + 432))).Fdefault_rc = int8(-int32(1))
		} else {
			(**(**TUnpackedRecord)(__ccgo_up(bp + 432))).Fdefault_rc = 0
		}
		(**(**TUnpackedRecord)(__ccgo_up(bp + 432))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		/* Inlined version of sqlite3VdbeIdxKeyCompare() */
		nCellKey = 0
		pCur2 = *(*uintptr)(unsafe.Pointer(pC29 + 36))
		nCellKey = int64(_sqlite3BtreePayloadSize(tls, pCur2))
		/* nCellKey will always be between 0 and 0xffffffff because of the way
		 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
		if nCellKey <= 0 || nCellKey > int64(0x7fffffff) {
			rc = _sqlite3CorruptError(tls, int32(103483))
			goto abort_due_to_error
		}
		_sqlite3VdbeMemInit(tls, bp+464, db, uint16(0))
		rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, pCur2, uint32(nCellKey), bp+464)
		if rc != 0 {
			goto abort_due_to_error
		}
		res11 = _sqlite3VdbeRecordCompareWithSkip(tls, (**(**TMem)(__ccgo_up(bp + 464))).Fn, (**(**TMem)(__ccgo_up(bp + 464))).Fz, bp+432, 0)
		_sqlite3VdbeMemReleaseMalloc(tls, bp+464)
		/* End of inlined sqlite3VdbeIdxKeyCompare() */
		if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode)&int32(1) == libc.Int32FromInt32(OP_IdxLT)&libc.Int32FromInt32(1) {
			res11 = -res11
		} else {
			res11 = res11 + 1
		}
		if res11 > 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: Destroy P1 P2 P3 * *
		 **
		 ** Delete an entire database table or index whose root page in the database
		 ** file is given by P1.
		 **
		 ** The table being destroyed is in the main database file if P3==0.  If
		 ** P3==1 then the table to be destroyed is in the auxiliary database file
		 ** that is used to store tables create using CREATE TEMPORARY TABLE.
		 **
		 ** If AUTOVACUUM is enabled then it is possible that another root page
		 ** might be moved into the newly deleted root page in order to keep all
		 ** root pages contiguous at the beginning of the database.  The former
		 ** value of the root page that moved - its value before the move occurred -
		 ** is stored in register P2. If no page movement was required (because the
		 ** table being dropped was already the last one in the database) then a
		 ** zero is stored in register P2.  If AUTOVACUUM is disabled then a zero
		 ** is stored in register P2.
		 **
		 ** This opcode throws an error if there are any active reader VMs when
		 ** it is invoked. This is done to avoid the difficulty associated with
		 ** updating existing cursors when a root page is moved in an AUTOVACUUM
		 ** database. This error is thrown even if the database is not an AUTOVACUUM
		 ** db in order to avoid introducing an incompatibility between autovacuum
		 ** and non-autovacuum modes.
		 **
		 ** See also: Clear
		 */
	_129:
		;
		pOut = _out2Prerelease(tls, p, pOp)
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Null)
		if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead > (*Tsqlite3)(unsafe.Pointer(db)).FnVDestroy+int32(1) {
			rc = int32(SQLITE_LOCKED)
			(*TVdbe)(unsafe.Pointer(p)).FerrorAction = uint8(OE_Abort)
			goto abort_due_to_error
		} else {
			iDb2 = (*TOp)(unsafe.Pointer(pOp)).Fp3
			**(**int32)(__ccgo_up(bp + 504)) = 0 /* Not needed.  Only to silence a warning. */
			rc = _sqlite3BtreeDropTable(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb2)*16))).FpBt, (*TOp)(unsafe.Pointer(pOp)).Fp1, bp+504)
			(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Int)
			*(*Ti64)(unsafe.Pointer(pOut)) = int64(**(**int32)(__ccgo_up(bp + 504)))
			if rc != 0 {
				goto abort_due_to_error
			}
			if **(**int32)(__ccgo_up(bp + 504)) != 0 {
				_sqlite3RootPageMoved(tls, db, iDb2, uint32(**(**int32)(__ccgo_up(bp + 504))), uint32((*TOp)(unsafe.Pointer(pOp)).Fp1))
				/* All OP_Destroy operations occur on the same btree */
				resetSchemaOnFault = uint8(iDb2 + int32(1))
			}
		}
		goto _189
		/* Opcode: Clear P1 P2 P3
		 **
		 ** Delete all contents of the database table or index whose root page
		 ** in the database file is given by P1.  But, unlike Destroy, do not
		 ** remove the table or index from the database file.
		 **
		 ** The table being cleared is in the main database file if P2==0.  If
		 ** P2==1 then the table to be cleared is in the auxiliary database file
		 ** that is used to store tables create using CREATE TEMPORARY TABLE.
		 **
		 ** If the P3 value is non-zero, then the row change count is incremented
		 ** by the number of rows in the table being cleared. If P3 is greater
		 ** than zero, then the value stored in register P3 is also incremented
		 ** by the number of rows in the table being cleared.
		 **
		 ** See also: Destroy
		 */
	_130:
		;
		**(**Ti64)(__ccgo_up(bp + 512)) = 0
		rc = _sqlite3BtreeClearTable(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*16))).FpBt, int32(uint32((*TOp)(unsafe.Pointer(pOp)).Fp1)), bp+512)
		if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 {
			**(**Ti64)(__ccgo_up(p + 40)) += **(**Ti64)(__ccgo_up(bp + 512))
			if (*TOp)(unsafe.Pointer(pOp)).Fp3 > 0 {
				*(*Ti64)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40)) += **(**Ti64)(__ccgo_up(bp + 512))
			}
		}
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: ResetSorter P1 * * * *
		 **
		 ** Delete all contents from the ephemeral table or sorter
		 ** that is open on cursor P1.
		 **
		 ** This opcode only works for cursors used for sorting and
		 ** opened with OP_OpenEphemeral or OP_SorterOpen.
		 */
	_131:
		;
		pC30 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if int32((*TVdbeCursor)(unsafe.Pointer(pC30)).FeCurType) == int32(CURTYPE_SORTER) {
			_sqlite3VdbeSorterReset(tls, db, *(*uintptr)(unsafe.Pointer(pC30 + 36)))
		} else {
			rc = _sqlite3BtreeClearTableOfCursor(tls, *(*uintptr)(unsafe.Pointer(pC30 + 36)))
			if rc != 0 {
				goto abort_due_to_error
			}
		}
		goto _189
		/* Opcode: CreateBtree P1 P2 P3 * *
		 ** Synopsis: r[P2]=root iDb=P1 flags=P3
		 **
		 ** Allocate a new b-tree in the main database file if P1==0 or in the
		 ** TEMP database file if P1==1 or in an attached database if
		 ** P1>1.  The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
		 ** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
		 ** The root page number of the new b-tree is stored in register P2.
		 */
	_132:
		;
		pOut = _out2Prerelease(tls, p, pOp)
		**(**TPgno)(__ccgo_up(bp + 520)) = uint32(0)
		pDb3 = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16
		rc = _sqlite3BtreeCreateTable(tls, (*TDb)(unsafe.Pointer(pDb3)).FpBt, bp+520, (*TOp)(unsafe.Pointer(pOp)).Fp3)
		if rc != 0 {
			goto abort_due_to_error
		}
		*(*Ti64)(unsafe.Pointer(pOut)) = int64(**(**TPgno)(__ccgo_up(bp + 520)))
		goto _189
		/* Opcode: SqlExec P1 P2 * P4 *
		 **
		 ** Run the SQL statement or statements specified in the P4 string.
		 **
		 ** The P1 parameter is a bitmask of options:
		 **
		 **    0x0001     Disable Auth and Trace callbacks while the statements
		 **               in P4 are running.
		 **
		 **    0x0002     Set db->nAnalysisLimit to P2 while the statements in
		 **               P4 are running.
		 **
		 */
	_133:
		;
		(*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec = (*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec + 1
		**(**uintptr)(__ccgo_up(bp + 524)) = uintptr(0)
		xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth
		mTrace = (*Tsqlite3)(unsafe.Pointer(db)).FmTrace
		savedAnalysisLimit = (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit
		if (*TOp)(unsafe.Pointer(pOp)).Fp1&int32(0x0001) != 0 {
			(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0)
			(*Tsqlite3)(unsafe.Pointer(db)).FmTrace = uint8(0)
		}
		if (*TOp)(unsafe.Pointer(pOp)).Fp1&int32(0x0002) != 0 {
			(*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit = (*TOp)(unsafe.Pointer(pOp)).Fp2
		}
		rc = Xsqlite3_exec(tls, db, *(*uintptr)(unsafe.Pointer(pOp + 16)), uintptr(0), uintptr(0), bp+524)
		(*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec = (*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec - 1
		(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth
		(*Tsqlite3)(unsafe.Pointer(db)).FmTrace = mTrace
		(*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit = savedAnalysisLimit
		if **(**uintptr)(__ccgo_up(bp + 524)) != 0 || rc != 0 {
			_sqlite3VdbeError(tls, p, __ccgo_ts+4729, libc.VaList(bp+768, **(**uintptr)(__ccgo_up(bp + 524))))
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 524)))
			if rc == int32(SQLITE_NOMEM) {
				goto no_mem
			}
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: ParseSchema P1 * * P4 *
		 **
		 ** Read and parse all entries from the schema table of database P1
		 ** that match the WHERE clause P4.  If P4 is a NULL pointer, then the
		 ** entire schema for P1 is reparsed.
		 **
		 ** This opcode invokes the parser to create a new virtual machine,
		 ** then runs the new virtual machine.  It is thus a re-entrant opcode.
		 */
	_134:
		;
		/* Any prepared statement that invokes this opcode will hold mutexes
		 ** on every btree.  This is a prerequisite for invoking
		 ** sqlite3InitCallback().
		 */
		iDb3 = (*TOp)(unsafe.Pointer(pOp)).Fp1
		if *(*uintptr)(unsafe.Pointer(pOp + 16)) == uintptr(0) {
			_sqlite3SchemaClear(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb3)*16))).FpSchema)
			**(**Tu32)(__ccgo_up(db + 24)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaKnownOk))
			rc = _sqlite3InitOne(tls, db, iDb3, p+124, uint32((*TOp)(unsafe.Pointer(pOp)).Fp5))
			**(**Tu32)(__ccgo_up(db + 24)) |= uint32(DBFLAG_SchemaChange)
			libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 0, 0x3)
		} else {
			zSchema = __ccgo_ts + 7501
			(**(**TInitData)(__ccgo_up(bp + 528))).Fdb = db
			(**(**TInitData)(__ccgo_up(bp + 528))).FiDb = iDb3
			(**(**TInitData)(__ccgo_up(bp + 528))).FpzErrMsg = p + 124
			(**(**TInitData)(__ccgo_up(bp + 528))).FmInitFlags = uint32(0)
			(**(**TInitData)(__ccgo_up(bp + 528))).FmxPage = _sqlite3BtreeLastPage(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb3)*16))).FpBt)
			zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+7515, libc.VaList(bp+768, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb3)*16))).FzDbSName, zSchema, *(*uintptr)(unsafe.Pointer(pOp + 16))))
			if zSql == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
			} else {
				(*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(1)
				(**(**TInitData)(__ccgo_up(bp + 528))).Frc = SQLITE_OK
				(**(**TInitData)(__ccgo_up(bp + 528))).FnInitRow = uint32(0)
				rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_sqlite3InitCallback), bp+528, uintptr(0))
				if rc == SQLITE_OK {
					rc = (**(**TInitData)(__ccgo_up(bp + 528))).Frc
				}
				if rc == SQLITE_OK && (**(**TInitData)(__ccgo_up(bp + 528))).FnInitRow == uint32(0) {
					/* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
					 ** at least one SQL statement. Any less than that indicates that
					 ** the sqlite_schema table is corrupt. */
					rc = _sqlite3CorruptError(tls, int32(103776))
				}
				_sqlite3DbFreeNN(tls, db, zSql)
				(*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(0)
			}
		}
		if rc != 0 {
			_sqlite3ResetAllSchemasOfConnection(tls, db)
			if rc == int32(SQLITE_NOMEM) {
				goto no_mem
			}
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: LoadAnalysis P1 * * * *
		 **
		 ** Read the sqlite_stat1 table for database P1 and load the content
		 ** of that table into the internal index hash table.  This will cause
		 ** the analysis to be used when preparing all subsequent queries.
		 */
	_135:
		;
		rc = _sqlite3AnalysisLoad(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1)
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: DropTable P1 * * P4 *
		 **
		 ** Remove the internal (in-memory) data structures that describe
		 ** the table named P4 in database P1.  This is called after a table
		 ** is dropped from disk (using the Destroy opcode) in order to keep
		 ** the internal representation of the
		 ** schema consistent with what is on disk.
		 */
	_136:
		;
		_sqlite3UnlinkAndDeleteTable(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, *(*uintptr)(unsafe.Pointer(pOp + 16)))
		goto _189
		/* Opcode: DropIndex P1 * * P4 *
		 **
		 ** Remove the internal (in-memory) data structures that describe
		 ** the index named P4 in database P1.  This is called after an index
		 ** is dropped from disk (using the Destroy opcode)
		 ** in order to keep the internal representation of the
		 ** schema consistent with what is on disk.
		 */
	_137:
		;
		_sqlite3UnlinkAndDeleteIndex(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, *(*uintptr)(unsafe.Pointer(pOp + 16)))
		goto _189
		/* Opcode: DropTrigger P1 * * P4 *
		 **
		 ** Remove the internal (in-memory) data structures that describe
		 ** the trigger named P4 in database P1.  This is called after a trigger
		 ** is dropped from disk (using the Destroy opcode) in order to keep
		 ** the internal representation of the
		 ** schema consistent with what is on disk.
		 */
	_138:
		;
		_sqlite3UnlinkAndDeleteTrigger(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, *(*uintptr)(unsafe.Pointer(pOp + 16)))
		goto _189
		/* Opcode: IntegrityCk P1 P2 P3 P4 P5
		 **
		 ** Do an analysis of the currently open database.  Store in
		 ** register (P1+1) the text of an error message describing any problems.
		 ** If no problems are found, store a NULL in register (P1+1).
		 **
		 ** The register (P1) contains one less than the maximum number of allowed
		 ** errors.  At most reg(P1) errors will be reported.
		 ** In other words, the analysis stops as soon as reg(P1) errors are
		 ** seen.  Reg(P1) is updated with the number of errors remaining.
		 **
		 ** The root page numbers of all tables in the database are integers
		 ** stored in P4_INTARRAY argument.
		 **
		 ** If P5 is not zero, the check is done on the auxiliary database
		 ** file, not the main database file.
		 **
		 ** This opcode is used to implement the integrity_check pragma.
		 */
	_139:
		; /* Register keeping track of errors remaining */
		nRoot = (*TOp)(unsafe.Pointer(pOp)).Fp2
		aRoot = *(*uintptr)(unsafe.Pointer(pOp + 16))
		pnErr = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1+int32(1))*40
		rc = _sqlite3BtreeIntegrityCheck(tls, db, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp5)*16))).FpBt, aRoot+1*4, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40, nRoot, int32(*(*Ti64)(unsafe.Pointer(pnErr)))+int32(1), bp+556, bp+560)
		_sqlite3VdbeMemSetNull(tls, pIn1)
		if **(**int32)(__ccgo_up(bp + 556)) == 0 {
		} else {
			if rc != 0 {
				Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 560)))
				goto abort_due_to_error
			} else {
				*(*Ti64)(unsafe.Pointer(pnErr)) -= int64(**(**int32)(__ccgo_up(bp + 556)) - int32(1))
				_sqlite3VdbeMemSetStr(tls, pIn1, **(**uintptr)(__ccgo_up(bp + 560)), int64(-int32(1)), uint8(SQLITE_UTF8), __ccgo_fp(Xsqlite3_free))
			}
		}
		_sqlite3VdbeChangeEncoding(tls, pIn1, int32(encoding))
		goto check_for_interrupt
		/* Opcode: IFindKey P1 P2 P3 P4 *
		 **
		 ** This instruction always follows an OP_Found with the same P1, P2 and P3
		 ** values as this instruction and a non-zero P4 value. The P4 value to
		 ** this opcode is of type P4_INDEX and contains a pointer to the Index
		 ** object of for the index being searched.
		 **
		 ** This opcode uses sqlite3VdbeFindIndexKey() to search around the current
		 ** cursor location for an index key that exactly matches all fields that
		 ** are not indexed expressions or references to VIRTUAL generated columns,
		 ** and either exactly match or are real numbers that are within 2 ULPs of
		 ** each other if the don't match.
		 **
		 ** To put it another way, this opcode looks for nearby index entries that
		 ** are very close to the search key, but which might have small differences
		 ** in floating-point values that come via an expression.
		 **
		 ** If no nearby alternative entry is found in cursor P1, then jump to P2.
		 ** But if a close match is found, fall through.
		 **
		 ** This opcode is used by PRAGMA integrity_check to help distinguish
		 ** between truely corrupt indexes and expression indexes that are holding
		 ** floating-point values that are off by one or two ULPs.
		 */
	_140:
		;
		pC31 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		libc.Xmemset(tls, bp+568, 0, uint32(32))
		(**(**TUnpackedRecord)(__ccgo_up(bp + 568))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		(**(**TUnpackedRecord)(__ccgo_up(bp + 568))).FnField = (*TIndex)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FnColumn
		(**(**TUnpackedRecord)(__ccgo_up(bp + 568))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC31)).FpKeyInfo
		rc = _sqlite3VdbeFindIndexKey(tls, *(*uintptr)(unsafe.Pointer(pC31 + 36)), *(*uintptr)(unsafe.Pointer(pOp + 16)), bp+568, bp+564, int32(1))
		if rc != 0 || **(**int32)(__ccgo_up(bp + 564)) != 0 {
			rc = SQLITE_OK
			goto jump_to_p2
		}
		(*TVdbeCursor)(unsafe.Pointer(pC31)).FnullRow = uint8(0)
		goto _189
		/* Opcode: RowSetAdd P1 P2 * * *
		 ** Synopsis: rowset(P1)=r[P2]
		 **
		 ** Insert the integer value held by register P2 into a RowSet object
		 ** held in register P1.
		 **
		 ** An assertion fails if P2 is not an integer.
		 */
	_141:
		; /* in1, in2 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 {
			if _sqlite3VdbeMemSetRowSet(tls, pIn1) != 0 {
				goto no_mem
			}
		}
		_sqlite3RowSetInsert(tls, (*TMem)(unsafe.Pointer(pIn1)).Fz, *(*Ti64)(unsafe.Pointer(pIn2)))
		goto _189
		/* Opcode: RowSetRead P1 P2 P3 * *
		 ** Synopsis: r[P3]=rowset(P1)
		 **
		 ** Extract the smallest value from the RowSet object in P1
		 ** and put that value into register P3.
		 ** Or, if RowSet object P1 is initially empty, leave P3
		 ** unchanged and jump to instruction P2.
		 */
	_142:
		;
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 || _sqlite3RowSetNext(tls, (*TMem)(unsafe.Pointer(pIn1)).Fz, bp+600) == 0 {
			/* The boolean index is empty */
			_sqlite3VdbeMemSetNull(tls, pIn1)
			goto jump_to_p2_and_check_for_interrupt
		} else {
			/* A value was pulled from the index */
			_sqlite3VdbeMemSetInt64(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40, **(**Ti64)(__ccgo_up(bp + 600)))
		}
		goto check_for_interrupt
		/* Opcode: RowSetTest P1 P2 P3 P4
		 ** Synopsis: if r[P3] in rowset(P1) goto P2
		 **
		 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
		 ** contains a RowSet object and that RowSet object contains
		 ** the value held in P3, jump to register P2. Otherwise, insert the
		 ** integer in P3 into the RowSet and continue on to the
		 ** next opcode.
		 **
		 ** The RowSet object is optimized for the case where sets of integers
		 ** are inserted in distinct phases, which each set contains no duplicates.
		 ** Each set is identified by a unique P4 value. The first set
		 ** must have P4==0, the final set must have P4==-1, and for all other sets
		 ** must have P4>0.
		 **
		 ** This allows optimizations: (a) when P4==0 there is no need to test
		 ** the RowSet object for P3, as it is guaranteed not to contain it,
		 ** (b) when P4==-1 there is no need to insert the value, as it will
		 ** never be tested for, and (c) when a value that is part of set X is
		 ** inserted, there is no need to search to see if the same value was
		 ** previously inserted as part of set X (only if it was previously
		 ** inserted as part of some other set).
		 */
	_143:
		;
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		iSet = *(*int32)(unsafe.Pointer(pOp + 16))
		/* If there is anything other than a rowset object in memory cell P1,
		 ** delete it now and initialize P1 with an empty rowset
		 */
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 {
			if _sqlite3VdbeMemSetRowSet(tls, pIn1) != 0 {
				goto no_mem
			}
		}
		if iSet != 0 {
			exists = _sqlite3RowSetTest(tls, (*TMem)(unsafe.Pointer(pIn1)).Fz, iSet, *(*Ti64)(unsafe.Pointer(pIn3)))
			if exists != 0 {
				goto jump_to_p2
			}
		}
		if iSet >= 0 {
			_sqlite3RowSetInsert(tls, (*TMem)(unsafe.Pointer(pIn1)).Fz, *(*Ti64)(unsafe.Pointer(pIn3)))
		}
		goto _189
		/* Opcode: Program P1 P2 P3 P4 P5
		 **
		 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
		 **
		 ** P1 contains the address of the memory cell that contains the first memory
		 ** cell in an array of values used as arguments to the sub-program. P2
		 ** contains the address to jump to if the sub-program throws an IGNORE
		 ** exception using the RAISE() function. P2 might be zero, if there is
		 ** no possibility that an IGNORE exception will be raised.
		 ** Register P3 contains the address
		 ** of a memory cell in this (the parent) VM that is used to allocate the
		 ** memory required by the sub-vdbe at runtime.
		 **
		 ** P4 is a pointer to the VM containing the trigger program.
		 **
		 ** If P5 is non-zero, then recursive program invocation is enabled.
		 */
	_144:
		; /* Token identifying trigger */
		pProgram = *(*uintptr)(unsafe.Pointer(pOp + 16))
		pRt = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		/* If the p5 flag is clear, then recursive invocation of triggers is
		 ** disabled for backwards compatibility (p5 is set if this sub-program
		 ** is really a trigger, not a foreign key action, and the flag set
		 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
		 **
		 ** It is recursive invocation of triggers, at the SQL level, that is
		 ** disabled. In some cases a single trigger may generate more than one
		 ** SubProgram (if the trigger may be executed with more than one different
		 ** ON CONFLICT algorithm). SubProgram structures associated with a
		 ** single trigger all have the same value for the SubProgram.token
		 ** variable.  */
		if (*TOp)(unsafe.Pointer(pOp)).Fp5 != 0 {
			t1 = (*TSubProgram)(unsafe.Pointer(pProgram)).Ftoken
			pFrame2 = (*TVdbe)(unsafe.Pointer(p)).FpFrame
			for {
				if !(pFrame2 != 0 && (*TVdbeFrame)(unsafe.Pointer(pFrame2)).Ftoken != t1) {
					break
				}
				goto _278
			_278:
				;
				pFrame2 = (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FpParent
			}
			if pFrame2 != 0 {
				goto _189
			}
		}
		if (*TVdbe)(unsafe.Pointer(p)).FnFrame >= **(**int32)(__ccgo_up(db + 120 + 10*4)) {
			rc = int32(SQLITE_ERROR)
			_sqlite3VdbeError(tls, p, __ccgo_ts+7558, 0)
			goto abort_due_to_error
		}
		/* Register pRt is used to store the memory required to save the state
		 ** of the current program, and the memory required at runtime to execute
		 ** the trigger program. If this trigger has been fired before, then pRt
		 ** is already allocated. Otherwise, it must be initialized.  */
		if int32((*TMem)(unsafe.Pointer(pRt)).Fflags)&int32(MEM_Blob) == 0 {
			/* SubProgram.nMem is set to the number of memory cells used by the
			 ** program stored in SubProgram.aOp. As well as these, one memory
			 ** cell is required for each cursor used by the program. Set local
			 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
			 */
			nMem = (*TSubProgram)(unsafe.Pointer(pProgram)).FnMem + (*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr
			if (*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr == 0 {
				nMem = nMem + 1
			}
			nByte2 = int64((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))+uint32(nMem)*uint32(40)+uint32((*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr)*uint32(4)) + (int64(7)+int64((*TSubProgram)(unsafe.Pointer(pProgram)).FnOp))/int64(8)
			pFrame2 = _sqlite3DbMallocZero(tls, db, uint64(nByte2))
			if !(pFrame2 != 0) {
				goto no_mem
			}
			_sqlite3VdbeMemRelease(tls, pRt)
			(*TMem)(unsafe.Pointer(pRt)).Fflags = uint16(libc.Int32FromInt32(MEM_Blob) | libc.Int32FromInt32(MEM_Dyn))
			(*TMem)(unsafe.Pointer(pRt)).Fz = pFrame2
			(*TMem)(unsafe.Pointer(pRt)).Fn = int32(nByte2)
			(*TMem)(unsafe.Pointer(pRt)).FxDel = __ccgo_fp(_sqlite3VdbeFrameMemDel)
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).Fv = p
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildMem = nMem
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildCsr = (*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).Fpc = (int32(pOp) - int32(aOp)) / 20
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaMem = (*TVdbe)(unsafe.Pointer(p)).FaMem
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnMem = (*TVdbe)(unsafe.Pointer(p)).FnMem
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FapCsr = (*TVdbe)(unsafe.Pointer(p)).FapCsr
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnCursor = (*TVdbe)(unsafe.Pointer(p)).FnCursor
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaOp = (*TVdbe)(unsafe.Pointer(p)).FaOp
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnOp = (*TVdbe)(unsafe.Pointer(p)).FnOp
			(*TVdbeFrame)(unsafe.Pointer(pFrame2)).Ftoken = (*TSubProgram)(unsafe.Pointer(pProgram)).Ftoken
			pEnd = pFrame2 + uintptr((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7))) + uintptr((*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildMem)*40
			pMem1 = pFrame2 + uintptr((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
			for {
				if !(pMem1 != pEnd) {
					break
				}
				(*TMem)(unsafe.Pointer(pMem1)).Fflags = uint16(MEM_Undefined)
				(*TMem)(unsafe.Pointer(pMem1)).Fdb = db
				goto _279
			_279:
				;
				pMem1 += 40
			}
		} else {
			pFrame2 = (*TMem)(unsafe.Pointer(pRt)).Fz
		}
		(*TVdbe)(unsafe.Pointer(p)).FnFrame = (*TVdbe)(unsafe.Pointer(p)).FnFrame + 1
		(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FpParent = (*TVdbe)(unsafe.Pointer(p)).FpFrame
		(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FlastRowid = (*Tsqlite3)(unsafe.Pointer(db)).FlastRowid
		(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChange = (*TVdbe)(unsafe.Pointer(p)).FnChange
		(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnDbChange = (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FnChange
		(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FpAuxData = (*TVdbe)(unsafe.Pointer(p)).FpAuxData
		(*TVdbe)(unsafe.Pointer(p)).FpAuxData = uintptr(0)
		(*TVdbe)(unsafe.Pointer(p)).FnChange = 0
		(*TVdbe)(unsafe.Pointer(p)).FpFrame = pFrame2
		v191 = pFrame2 + uintptr((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
		aMem = v191
		(*TVdbe)(unsafe.Pointer(p)).FaMem = v191
		(*TVdbe)(unsafe.Pointer(p)).FnMem = (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildMem
		(*TVdbe)(unsafe.Pointer(p)).FnCursor = int32(uint16((*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildCsr))
		(*TVdbe)(unsafe.Pointer(p)).FapCsr = aMem + uintptr((*TVdbe)(unsafe.Pointer(p)).FnMem)*40
		(*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaOnce = (*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr)*4
		libc.Xmemset(tls, (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaOnce, 0, uint32(((*TSubProgram)(unsafe.Pointer(pProgram)).FnOp+int32(7))/int32(8)))
		v191 = (*TSubProgram)(unsafe.Pointer(pProgram)).FaOp
		aOp = v191
		(*TVdbe)(unsafe.Pointer(p)).FaOp = v191
		(*TVdbe)(unsafe.Pointer(p)).FnOp = (*TSubProgram)(unsafe.Pointer(pProgram)).FnOp
		pOp = aOp + uintptr(-libc.Int32FromInt32(1))*20
		goto check_for_interrupt
		/* Opcode: Param P1 P2 * * *
		 **
		 ** This opcode is only ever present in sub-programs called via the
		 ** OP_Program instruction. Copy a value currently stored in a memory
		 ** cell of the calling (parent) frame to cell P2 in the current frames
		 ** address space. This is used by trigger programs to access the new.*
		 ** and old.* values.
		 **
		 ** The address of the cell in the parent frame is determined by adding
		 ** the value of the P1 argument to the value of the P1 argument to the
		 ** calling OP_Program instruction.
		 */
	_145:
		;
		pOut = _out2Prerelease(tls, p, pOp)
		pFrame3 = (*TVdbe)(unsafe.Pointer(p)).FpFrame
		pIn = (*TVdbeFrame)(unsafe.Pointer(pFrame3)).FaMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1+(**(**TOp)(__ccgo_up((*TVdbeFrame)(unsafe.Pointer(pFrame3)).FaOp + uintptr((*TVdbeFrame)(unsafe.Pointer(pFrame3)).Fpc)*20))).Fp1)*40
		_sqlite3VdbeMemShallowCopy(tls, pOut, pIn, int32(MEM_Ephem))
		goto _189
		/* Opcode: FkCounter P1 P2 * * *
		 ** Synopsis: fkctr[P1]+=P2
		 **
		 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
		 ** If P1 is non-zero, the database constraint counter is incremented
		 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
		 ** statement counter is incremented (immediate foreign key constraints).
		 */
	_146:
		;
		if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 {
			**(**Ti64)(__ccgo_up(db + 528)) += int64((*TOp)(unsafe.Pointer(pOp)).Fp2)
		} else {
			if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_DeferFKs) != 0 {
				**(**Ti64)(__ccgo_up(db + 536)) += int64((*TOp)(unsafe.Pointer(pOp)).Fp2)
			} else {
				**(**Ti64)(__ccgo_up(p + 64)) += int64((*TOp)(unsafe.Pointer(pOp)).Fp2)
			}
		}
		goto _189
		/* Opcode: FkIfZero P1 P2 * * *
		 ** Synopsis: if fkctr[P1]==0 goto P2
		 **
		 ** This opcode tests if a foreign key constraint-counter is currently zero.
		 ** If so, jump to instruction P2. Otherwise, fall through to the next
		 ** instruction.
		 **
		 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
		 ** is zero (the one that counts deferred constraint violations). If P1 is
		 ** zero, the jump is taken if the statement constraint-counter is zero
		 ** (immediate foreign key constraint violations).
		 */
	_147:
		; /* jump */
		if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 {
			if (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons == 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons == 0 {
				goto jump_to_p2
			}
		} else {
			if (*TVdbe)(unsafe.Pointer(p)).FnFkConstraint == 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons == 0 {
				goto jump_to_p2
			}
		}
		goto _189
		/* Opcode: MemMax P1 P2 * * *
		 ** Synopsis: r[P1]=max(r[P1],r[P2])
		 **
		 ** P1 is a register in the root frame of this VM (the root frame is
		 ** different from the current frame if this instruction is being executed
		 ** within a sub-program). Set the value of register P1 to the maximum of
		 ** its current value and the value in register P2.
		 **
		 ** This instruction throws an error if the memory cell is not initially
		 ** an integer.
		 */
	_148:
		;
		if (*TVdbe)(unsafe.Pointer(p)).FpFrame != 0 {
			pFrame4 = (*TVdbe)(unsafe.Pointer(p)).FpFrame
			for {
				if !((*TVdbeFrame)(unsafe.Pointer(pFrame4)).FpParent != 0) {
					break
				}
				goto _282
			_282:
				;
				pFrame4 = (*TVdbeFrame)(unsafe.Pointer(pFrame4)).FpParent
			}
			pIn1 = (*TVdbeFrame)(unsafe.Pointer(pFrame4)).FaMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		} else {
			pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		}
		_sqlite3VdbeMemIntegerify(tls, pIn1)
		pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		_sqlite3VdbeMemIntegerify(tls, pIn2)
		if *(*Ti64)(unsafe.Pointer(pIn1)) < *(*Ti64)(unsafe.Pointer(pIn2)) {
			*(*Ti64)(unsafe.Pointer(pIn1)) = *(*Ti64)(unsafe.Pointer(pIn2))
		}
		goto _189
		/* Opcode: IfPos P1 P2 P3 * *
		 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
		 **
		 ** Register P1 must contain an integer.
		 ** If the value of register P1 is 1 or greater, subtract P3 from the
		 ** value in P1 and jump to P2.
		 **
		 ** If the initial value of register P1 is less than 1, then the
		 ** value is unchanged and control passes through to the next instruction.
		 */
	_149:
		; /* jump, in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if *(*Ti64)(unsafe.Pointer(pIn1)) > 0 {
			*(*Ti64)(unsafe.Pointer(pIn1)) -= int64((*TOp)(unsafe.Pointer(pOp)).Fp3)
			goto jump_to_p2
		}
		goto _189
		/* Opcode: OffsetLimit P1 P2 P3 * *
		 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
		 **
		 ** This opcode performs a commonly used computation associated with
		 ** LIMIT and OFFSET processing.  r[P1] holds the limit counter.  r[P3]
		 ** holds the offset counter.  The opcode computes the combined value
		 ** of the LIMIT and OFFSET and stores that value in r[P2].  The r[P2]
		 ** value computed is the total number of rows that will need to be
		 ** visited in order to complete the query.
		 **
		 ** If r[P3] is zero or negative, that means there is no OFFSET
		 ** and r[P2] is set to be the value of the LIMIT, r[P1].
		 **
		 ** if r[P1] is zero or negative, that means there is no LIMIT
		 ** and r[P2] is set to -1.
		 **
		 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
		 */
	_150:
		;
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		pOut = _out2Prerelease(tls, p, pOp)
		**(**Ti64)(__ccgo_up(bp + 608)) = *(*Ti64)(unsafe.Pointer(pIn1))
		if v217 = **(**Ti64)(__ccgo_up(bp + 608)) <= 0; !v217 {
			if *(*Ti64)(unsafe.Pointer(pIn3)) > 0 {
				v206 = *(*Ti64)(unsafe.Pointer(pIn3))
			} else {
				v206 = 0
			}
		}
		if v217 || _sqlite3AddInt64(tls, bp+608, v206) != 0 {
			/* If the LIMIT is less than or equal to zero, loop forever.  This
			 ** is documented.  But also, if the LIMIT+OFFSET exceeds 2^63 then
			 ** also loop forever.  This is undocumented.  In fact, one could argue
			 ** that the loop should terminate.  But assuming 1 billion iterations
			 ** per second (far exceeding the capabilities of any current hardware)
			 ** it would take nearly 300 years to actually reach the limit.  So
			 ** looping forever is a reasonable approximation. */
			*(*Ti64)(unsafe.Pointer(pOut)) = int64(-int32(1))
		} else {
			*(*Ti64)(unsafe.Pointer(pOut)) = **(**Ti64)(__ccgo_up(bp + 608))
		}
		goto _189
		/* Opcode: IfNotZero P1 P2 * * *
		 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
		 **
		 ** Register P1 must contain an integer.  If the content of register P1 is
		 ** initially greater than zero, then decrement the value in register P1.
		 ** If it is non-zero (negative or positive) and then also jump to P2.
		 ** If register P1 is initially zero, leave it unchanged and fall through.
		 */
	_151:
		; /* jump, in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if *(*Ti64)(unsafe.Pointer(pIn1)) != 0 {
			if *(*Ti64)(unsafe.Pointer(pIn1)) > 0 {
				*(*Ti64)(unsafe.Pointer(pIn1)) = *(*Ti64)(unsafe.Pointer(pIn1)) - 1
			}
			goto jump_to_p2
		}
		goto _189
		/* Opcode: DecrJumpZero P1 P2 * * *
		 ** Synopsis: if (--r[P1])==0 goto P2
		 **
		 ** Register P1 must hold an integer.  Decrement the value in P1
		 ** and jump to P2 if the new value is exactly zero.
		 */
	_152:
		; /* jump, in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if *(*Ti64)(unsafe.Pointer(pIn1)) > int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)) {
			*(*Ti64)(unsafe.Pointer(pIn1)) = *(*Ti64)(unsafe.Pointer(pIn1)) - 1
		}
		if *(*Ti64)(unsafe.Pointer(pIn1)) == 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: AggStep * P2 P3 P4 P5
		 ** Synopsis: accum=r[P3] step(r[P2@P5])
		 **
		 ** Execute the xStep function for an aggregate.
		 ** The function has P5 arguments.  P4 is a pointer to the
		 ** FuncDef structure that specifies the function.  Register P3 is the
		 ** accumulator.
		 **
		 ** The P5 arguments are taken from register P2 and its
		 ** successors.
		 */
		/* Opcode: AggInverse * P2 P3 P4 P5
		 ** Synopsis: accum=r[P3] inverse(r[P2@P5])
		 **
		 ** Execute the xInverse function for an aggregate.
		 ** The function has P5 arguments.  P4 is a pointer to the
		 ** FuncDef structure that specifies the function.  Register P3 is the
		 ** accumulator.
		 **
		 ** The P5 arguments are taken from register P2 and its
		 ** successors.
		 */
		/* Opcode: AggStep1 P1 P2 P3 P4 P5
		 ** Synopsis: accum=r[P3] step(r[P2@P5])
		 **
		 ** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
		 ** aggregate.  The function has P5 arguments.  P4 is a pointer to the
		 ** FuncDef structure that specifies the function.  Register P3 is the
		 ** accumulator.
		 **
		 ** The P5 arguments are taken from register P2 and its
		 ** successors.
		 **
		 ** This opcode is initially coded as OP_AggStep0.  On first evaluation,
		 ** the FuncDef stored in P4 is converted into an sqlite3_context and
		 ** the opcode is changed.  In this way, the initialization of the
		 ** sqlite3_context only happens once, instead of on each call to the
		 ** step function.
		 */
	_154:
		;
	_153:
		;
		n4 = int32((*TOp)(unsafe.Pointer(pOp)).Fp5)
		/* Allocate space for (a) the context object and (n-1) extra pointers
		 ** to append to the sqlite3_context.argv[1] array, and (b) a memory
		 ** cell in which to store the accumulation. Be careful that the memory
		 ** cell is 8-byte aligned, even on platforms where a pointer is 32-bits.
		 **
		 ** Note: We could avoid this by using a regular memory cell from aMem[] for
		 ** the accumulator, instead of allocating one here. */
		nAlloc = uint64((uint32(libc.UintptrFromInt32(0)+28) + uint32(n4)*libc.Uint32FromInt64(4) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7)))
		pCtx = _sqlite3DbMallocRawNN(tls, db, nAlloc+uint64(40))
		if pCtx == uintptr(0) {
			goto no_mem
		}
		(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut = pCtx + uintptr(nAlloc)
		_sqlite3VdbeMemInit(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, db, uint16(MEM_Null))
		(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpMem = uintptr(0)
		(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpFunc = *(*uintptr)(unsafe.Pointer(pOp + 16))
		(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FiOp = (int32(pOp) - int32(aOp)) / 20
		(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpVdbe = p
		(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FskipFlag = uint8(0)
		(*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = 0
		(*Tsqlite3_context)(unsafe.Pointer(pCtx)).Fenc = encoding
		(*Tsqlite3_context)(unsafe.Pointer(pCtx)).Fargc = uint16(n4)
		(*TOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(16))
		*(*uintptr)(unsafe.Pointer(pOp + 16)) = pCtx
		/* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
		(*TOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_AggStep1)
		/* Fall through into OP_AggStep */
	_155:
		;
		pCtx1 = *(*uintptr)(unsafe.Pointer(pOp + 16))
		pMem2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		/* If this function is inside of a trigger, the register array in aMem[]
		 ** might change from one evaluation to the next.  The next block of code
		 ** checks to see if the register array has changed, and if so it
		 ** reinitializes the relevant parts of the sqlite3_context object */
		if (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpMem != pMem2 {
			(*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpMem = pMem2
			i4 = int32((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).Fargc) - int32(1)
			for {
				if !(i4 >= 0) {
					break
				}
				*(*uintptr)(unsafe.Pointer(pCtx1 + 28 + uintptr(i4)*4)) = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2+i4)*40
				goto _285
			_285:
				;
				i4 = i4 - 1
			}
		}
		(*TMem)(unsafe.Pointer(pMem2)).Fn = (*TMem)(unsafe.Pointer(pMem2)).Fn + 1
		if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 {
			(*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpFunc)).FxInverse})))(tls, pCtx1, int32((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).Fargc), pCtx1+28)
		} else {
			(*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpFunc)).FxSFunc})))(tls, pCtx1, int32((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).Fargc), pCtx1+28)
		} /* IMP: R-24505-23230 */
		if (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FisError != 0 {
			if (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FisError > 0 {
				_sqlite3VdbeError(tls, p, __ccgo_ts+4729, libc.VaList(bp+768, Xsqlite3_value_text(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpOut)))
				rc = (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FisError
			}
			if (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FskipFlag != 0 {
				i4 = (**(**TOp)(__ccgo_up(pOp + uintptr(-libc.Int32FromInt32(1))*20))).Fp1
				if i4 != 0 {
					_sqlite3VdbeMemSetInt64(tls, aMem+uintptr(i4)*40, int64(1))
				}
				(*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FskipFlag = uint8(0)
			}
			_sqlite3VdbeMemRelease(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpOut)
			(*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FpOut)).Fflags = uint16(MEM_Null)
			(*Tsqlite3_context)(unsafe.Pointer(pCtx1)).FisError = 0
			if rc != 0 {
				goto abort_due_to_error
			}
		}
		goto _189
		/* Opcode: AggFinal P1 P2 * P4 *
		 ** Synopsis: accum=r[P1] N=P2
		 **
		 ** P1 is the memory location that is the accumulator for an aggregate
		 ** or window function.  Execute the finalizer function
		 ** for an aggregate and store the result in P1.
		 **
		 ** P2 is the number of arguments that the step function takes and
		 ** P4 is a pointer to the FuncDef for this function.  The P2
		 ** argument is not used by this opcode.  It is only there to disambiguate
		 ** functions that can take varying numbers of arguments.  The
		 ** P4 argument is only needed for the case where
		 ** the step function was not previously called.
		 */
		/* Opcode: AggValue * P2 P3 P4 *
		 ** Synopsis: r[P3]=value N=P2
		 **
		 ** Invoke the xValue() function and store the result in register P3.
		 **
		 ** P2 is the number of arguments that the step function takes and
		 ** P4 is a pointer to the FuncDef for this function.  The P2
		 ** argument is not used by this opcode.  It is only there to disambiguate
		 ** functions that can take varying numbers of arguments.  The
		 ** P4 argument is only needed for the case where
		 ** the step function was not previously called.
		 */
	_157:
		;
	_156:
		;
		pMem3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 {
			rc = _sqlite3VdbeMemAggValue(tls, pMem3, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40, *(*uintptr)(unsafe.Pointer(pOp + 16)))
			pMem3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		} else {
			rc = _sqlite3VdbeMemFinalize(tls, pMem3, *(*uintptr)(unsafe.Pointer(pOp + 16)))
		}
		if rc != 0 {
			_sqlite3VdbeError(tls, p, __ccgo_ts+4729, libc.VaList(bp+768, Xsqlite3_value_text(tls, pMem3)))
			goto abort_due_to_error
		}
		_sqlite3VdbeChangeEncoding(tls, pMem3, int32(encoding))
		goto _189
		/* Opcode: Checkpoint P1 P2 P3 * *
		 **
		 ** Checkpoint database P1. This is a no-op if P1 is not currently in
		 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
		 ** RESTART, or TRUNCATE.  Write 1 or 0 into mem[P3] if the checkpoint returns
		 ** SQLITE_BUSY or not, respectively.  Write the number of pages in the
		 ** WAL after the checkpoint into mem[P3+1] and the number of pages
		 ** in the WAL that have been checkpointed after the checkpoint
		 ** completes into mem[P3+2].  However on an error, mem[P3+1] and
		 ** mem[P3+2] are initialized to -1.
		 */
	_158:
		; /* Write results here */
		(**(**[3]int32)(__ccgo_up(bp + 616)))[0] = 0
		v190 = -libc.Int32FromInt32(1)
		(**(**[3]int32)(__ccgo_up(bp + 616)))[int32(2)] = v190
		(**(**[3]int32)(__ccgo_up(bp + 616)))[int32(1)] = v190
		rc = _sqlite3Checkpoint(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, (*TOp)(unsafe.Pointer(pOp)).Fp2, bp+616+1*4, bp+616+2*4)
		if rc != 0 {
			if rc != int32(SQLITE_BUSY) {
				goto abort_due_to_error
			}
			rc = SQLITE_OK
			(**(**[3]int32)(__ccgo_up(bp + 616)))[0] = int32(1)
		}
		i5 = 0
		pMem4 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		for {
			if !(i5 < int32(3)) {
				break
			}
			_sqlite3VdbeMemSetInt64(tls, pMem4, int64((**(**[3]int32)(__ccgo_up(bp + 616)))[i5]))
			goto _287
		_287:
			;
			i5 = i5 + 1
			pMem4 += 40
		}
		goto _189
		/* Opcode: JournalMode P1 P2 P3 * *
		 **
		 ** Change the journal mode of database P1 to P3. P3 must be one of the
		 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
		 ** modes (delete, truncate, persist, off and memory), this is a simple
		 ** operation. No IO is required.
		 **
		 ** If changing into or out of WAL mode the procedure is more complicated.
		 **
		 ** Write a string containing the final journal-mode to register P2.
		 */
	_159:
		; /* Name of database file for pPager */
		pOut = _out2Prerelease(tls, p, pOp)
		eNew = (*TOp)(unsafe.Pointer(pOp)).Fp3
		pBt1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpBt
		pPager = _sqlite3BtreePager(tls, pBt1)
		eOld = _sqlite3PagerGetJournalMode(tls, pPager)
		if eNew == -int32(1) {
			eNew = eOld
		}
		if !(_sqlite3PagerOkToChangeJournalMode(tls, pPager) != 0) {
			eNew = eOld
		}
		zFilename = _sqlite3PagerFilename(tls, pPager, int32(1))
		/* Do not allow a transition to journal_mode=WAL for a database
		 ** in temporary storage or if the VFS does not support shared memory
		 */
		if eNew == int32(PAGER_JOURNALMODE_WAL) && (_sqlite3Strlen30(tls, zFilename) == 0 || !(_sqlite3PagerWalSupported(tls, pPager) != 0)) {
			eNew = eOld
		}
		if eNew != eOld && (eOld == int32(PAGER_JOURNALMODE_WAL) || eNew == int32(PAGER_JOURNALMODE_WAL)) {
			if !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead > int32(1) {
				rc = int32(SQLITE_ERROR)
				if eNew == int32(PAGER_JOURNALMODE_WAL) {
					v191 = __ccgo_ts + 7595
				} else {
					v191 = __ccgo_ts + 7600
				}
				_sqlite3VdbeError(tls, p, __ccgo_ts+7607, libc.VaList(bp+768, v191))
				goto abort_due_to_error
			} else {
				if eOld == int32(PAGER_JOURNALMODE_WAL) {
					/* If leaving WAL mode, close the log file. If successful, the call
					 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
					 ** file. An EXCLUSIVE lock may still be held on the database file
					 ** after a successful return.
					 */
					rc = _sqlite3PagerCloseWal(tls, pPager, db)
					if rc == SQLITE_OK {
						_sqlite3PagerSetJournalMode(tls, pPager, eNew)
					}
				} else {
					if eOld == int32(PAGER_JOURNALMODE_MEMORY) {
						/* Cannot transition directly from MEMORY to WAL.  Use mode OFF
						 ** as an intermediate */
						_sqlite3PagerSetJournalMode(tls, pPager, int32(PAGER_JOURNALMODE_OFF))
					}
				}
				/* Open a transaction on the database file. Regardless of the journal
				 ** mode, this transaction always uses a rollback journal.
				 */
				if rc == SQLITE_OK {
					if eNew == int32(PAGER_JOURNALMODE_WAL) {
						v190 = int32(2)
					} else {
						v190 = int32(1)
					}
					rc = _sqlite3BtreeSetVersion(tls, pBt1, v190)
				}
			}
		}
		if rc != 0 {
			eNew = eOld
		}
		eNew = _sqlite3PagerSetJournalMode(tls, pPager, eNew)
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Term))
		(*TMem)(unsafe.Pointer(pOut)).Fz = _sqlite3JournalModename(tls, eNew)
		(*TMem)(unsafe.Pointer(pOut)).Fn = _sqlite3Strlen30(tls, (*TMem)(unsafe.Pointer(pOut)).Fz)
		(*TMem)(unsafe.Pointer(pOut)).Fenc = uint8(SQLITE_UTF8)
		_sqlite3VdbeChangeEncoding(tls, pOut, int32(encoding))
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: Vacuum P1 P2 * * *
		 **
		 ** Vacuum the entire database P1.  P1 is 0 for "main", and 2 or more
		 ** for an attached database.  The "temp" database may not be vacuumed.
		 **
		 ** If P2 is not zero, then it is a register holding a string which is
		 ** the file into which the result of vacuum should be written.  When
		 ** P2 is zero, the vacuum overwrites the original database.
		 */
	_160:
		;
		if (*TOp)(unsafe.Pointer(pOp)).Fp2 != 0 {
			v191 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		} else {
			v191 = uintptr(0)
		}
		rc = _sqlite3RunVacuum(tls, p+124, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, v191)
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: IncrVacuum P1 P2 * * *
		 **
		 ** Perform a single step of the incremental vacuum procedure on
		 ** the P1 database. If the vacuum has finished, jump to instruction
		 ** P2. Otherwise, fall through to the next instruction.
		 */
	_161:
		;
		pBt2 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpBt
		rc = _sqlite3BtreeIncrVacuum(tls, pBt2)
		if rc != 0 {
			if rc != int32(SQLITE_DONE) {
				goto abort_due_to_error
			}
			rc = SQLITE_OK
			goto jump_to_p2
		}
		goto _189
		/* Opcode: Expire P1 P2 * * *
		 **
		 ** Cause precompiled statements to expire.  When an expired statement
		 ** is executed using sqlite3_step() it will either automatically
		 ** reprepare itself (if it was originally created using sqlite3_prepare_v2())
		 ** or it will fail with SQLITE_SCHEMA.
		 **
		 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
		 ** then only the currently executing statement is expired.
		 **
		 ** If P2 is 0, then SQL statements are expired immediately.  If P2 is 1,
		 ** then running SQL statements are allowed to continue to run to completion.
		 ** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
		 ** that might help the statement run faster but which does not affect the
		 ** correctness of operation.
		 */
	_162:
		;
		if !((*TOp)(unsafe.Pointer(pOp)).Fp1 != 0) {
			_sqlite3ExpirePreparedStatements(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp2)
		} else {
			libc.SetBitFieldPtr16Uint32(p+152, uint32((*TOp)(unsafe.Pointer(pOp)).Fp2+libc.Int32FromInt32(1)), 0, 0x3)
		}
		goto _189
		/* Opcode: CursorLock P1 * * * *
		 **
		 ** Lock the btree to which cursor P1 is pointing so that the btree cannot be
		 ** written by an other cursor.
		 */
	_163:
		;
		pC32 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		_sqlite3BtreeCursorPin(tls, *(*uintptr)(unsafe.Pointer(pC32 + 36)))
		goto _189
		/* Opcode: CursorUnlock P1 * * * *
		 **
		 ** Unlock the btree to which cursor P1 is pointing so that it can be
		 ** written by other cursors.
		 */
	_164:
		;
		pC33 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		_sqlite3BtreeCursorUnpin(tls, *(*uintptr)(unsafe.Pointer(pC33 + 36)))
		goto _189
		/* Opcode: TableLock P1 P2 P3 P4 *
		 ** Synopsis: iDb=P1 root=P2 write=P3
		 **
		 ** Obtain a lock on a particular table. This instruction is only used when
		 ** the shared-cache feature is enabled.
		 **
		 ** P1 is the index of the database in sqlite3.aDb[] of the database
		 ** on which the lock is acquired.  A readlock is obtained if P3==0 or
		 ** a write lock if P3==1.
		 **
		 ** P2 contains the root-page of the table to lock.
		 **
		 ** P4 contains a pointer to the name of the table being locked. This is only
		 ** used to generate an error message if the lock cannot be obtained.
		 */
	_165:
		;
		isWriteLock = uint8((*TOp)(unsafe.Pointer(pOp)).Fp3)
		if isWriteLock != 0 || uint64(0) == (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00004))<<libc.Int32FromInt32(32)) {
			p13 = (*TOp)(unsafe.Pointer(pOp)).Fp1
			rc = _sqlite3BtreeLockTable(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(p13)*16))).FpBt, (*TOp)(unsafe.Pointer(pOp)).Fp2, isWriteLock)
			if rc != 0 {
				if rc&int32(0xFF) == int32(SQLITE_LOCKED) {
					z1 = *(*uintptr)(unsafe.Pointer(pOp + 16))
					_sqlite3VdbeError(tls, p, __ccgo_ts+7659, libc.VaList(bp+768, z1))
				}
				goto abort_due_to_error
			}
		}
		goto _189
		/* Opcode: VBegin * * * P4 *
		 **
		 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
		 ** xBegin method for that table.
		 **
		 ** Also, whether or not P4 is set, check that this is not being called from
		 ** within a callback to a virtual table xSync() method. If it is, the error
		 ** code will be set to SQLITE_LOCKED.
		 */
	_166:
		;
		pVTab = *(*uintptr)(unsafe.Pointer(pOp + 16))
		rc = _sqlite3VtabBegin(tls, db, pVTab)
		if pVTab != 0 {
			_sqlite3VtabImportErrmsg(tls, p, (*TVTable)(unsafe.Pointer(pVTab)).FpVtab)
		}
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: VCreate P1 P2 * * *
		 **
		 ** P2 is a register that holds the name of a virtual table in database
		 ** P1. Call the xCreate method for that table.
		 */
	_167:
		; /* Name of the virtual table */
		libc.Xmemset(tls, bp+632, 0, uint32(40))
		(**(**TMem)(__ccgo_up(bp + 632))).Fdb = db
		/* Because P2 is always a static string, it is impossible for the
		 ** sqlite3VdbeMemCopy() to fail */
		rc = _sqlite3VdbeMemCopy(tls, bp+632, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40)
		zTab = Xsqlite3_value_text(tls, bp+632)
		if zTab != 0 {
			rc = _sqlite3VtabCallCreate(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, zTab, p+124)
		}
		_sqlite3VdbeMemRelease(tls, bp+632)
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: VDestroy P1 * * P4 *
		 **
		 ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
		 ** of that table.
		 */
	_168:
		;
		(*Tsqlite3)(unsafe.Pointer(db)).FnVDestroy = (*Tsqlite3)(unsafe.Pointer(db)).FnVDestroy + 1
		rc = _sqlite3VtabCallDestroy(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, *(*uintptr)(unsafe.Pointer(pOp + 16)))
		(*Tsqlite3)(unsafe.Pointer(db)).FnVDestroy = (*Tsqlite3)(unsafe.Pointer(db)).FnVDestroy - 1
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: VOpen P1 * * P4 *
		 **
		 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
		 ** P1 is a cursor number.  This opcode opens a cursor to the virtual
		 ** table and stores that cursor in P1.
		 */
	_169:
		;
		pCur3 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if pCur3 != uintptr(0) && int32((*TVdbeCursor)(unsafe.Pointer(pCur3)).FeCurType) == int32(CURTYPE_VTAB) && (*Tsqlite3_vtab_cursor)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pCur3 + 36)))).FpVtab == (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab {
			/* This opcode is a no-op if the cursor is already open */
			goto _189
		}
		**(**uintptr)(__ccgo_up(bp + 672)) = uintptr(0)
		pVtab1 = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab
		if pVtab1 == uintptr(0) || (*Tsqlite3_vtab)(unsafe.Pointer(pVtab1)).FpModule == uintptr(0) {
			rc = int32(SQLITE_LOCKED)
			goto abort_due_to_error
		}
		pModule1 = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab1)).FpModule
		rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule1)).FxOpen})))(tls, pVtab1, bp+672)
		_sqlite3VtabImportErrmsg(tls, p, pVtab1)
		if rc != 0 {
			goto abort_due_to_error
		}
		/* Initialize sqlite3_vtab_cursor base class */
		(*Tsqlite3_vtab_cursor)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp + 672)))).FpVtab = pVtab1
		/* Initialize vdbe cursor object */
		pCur3 = _allocateCursor(tls, p, (*TOp)(unsafe.Pointer(pOp)).Fp1, 0, uint8(CURTYPE_VTAB))
		if pCur3 != 0 {
			*(*uintptr)(unsafe.Pointer(pCur3 + 36)) = **(**uintptr)(__ccgo_up(bp + 672))
			(*Tsqlite3_vtab)(unsafe.Pointer(pVtab1)).FnRef = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab1)).FnRef + 1
		} else {
			(*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule1)).FxClose})))(tls, **(**uintptr)(__ccgo_up(bp + 672)))
			goto no_mem
		}
		goto _189
		/* Opcode: VCheck P1 P2 P3 P4 *
		 **
		 ** P4 is a pointer to a Table object that is a virtual table in schema P1
		 ** that supports the xIntegrity() method.  This opcode runs the xIntegrity()
		 ** method for that virtual table, using P3 as the integer argument.  If
		 ** an error is reported back, the table name is prepended to the error
		 ** message and that message is stored in P2.  If no errors are seen,
		 ** register P2 is set to NULL.
		 */
	_170:
		;
		**(**uintptr)(__ccgo_up(bp + 676)) = uintptr(0)
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		_sqlite3VdbeMemSetNull(tls, pOut) /* Innocent until proven guilty */
		pTab3 = *(*uintptr)(unsafe.Pointer(pOp + 16))
		if (*(*struct {
			FnArg  int32
			FazArg uintptr
			Fp     uintptr
		})(unsafe.Pointer(pTab3 + 44))).Fp == uintptr(0) {
			goto _189
		}
		pVtab2 = (*TVTable)(unsafe.Pointer((*(*struct {
			FnArg  int32
			FazArg uintptr
			Fp     uintptr
		})(unsafe.Pointer(pTab3 + 44))).Fp)).FpVtab
		pModule2 = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab2)).FpModule
		_sqlite3VtabLock(tls, (*(*struct {
			FnArg  int32
			FazArg uintptr
			Fp     uintptr
		})(unsafe.Pointer(pTab3 + 44))).Fp)
		rc = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule2)).FxIntegrity})))(tls, pVtab2, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FzDbSName, (*TTable)(unsafe.Pointer(pTab3)).FzName, (*TOp)(unsafe.Pointer(pOp)).Fp3, bp+676)
		_sqlite3VtabUnlock(tls, (*(*struct {
			FnArg  int32
			FazArg uintptr
			Fp     uintptr
		})(unsafe.Pointer(pTab3 + 44))).Fp)
		if rc != 0 {
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 676)))
			goto abort_due_to_error
		}
		if **(**uintptr)(__ccgo_up(bp + 676)) != 0 {
			_sqlite3VdbeMemSetStr(tls, pOut, **(**uintptr)(__ccgo_up(bp + 676)), int64(-int32(1)), uint8(SQLITE_UTF8), __ccgo_fp(Xsqlite3_free))
		}
		goto _189
		/* Opcode: VInitIn P1 P2 P3 * *
		 ** Synopsis: r[P2]=ValueList(P1,P3)
		 **
		 ** Set register P2 to be a pointer to a ValueList object for cursor P1
		 ** with cache register P3 and output register P3+1.  This ValueList object
		 ** can be used as the first argument to sqlite3_vtab_in_first() and
		 ** sqlite3_vtab_in_next() to extract all of the values stored in the P1
		 ** cursor.  Register P3 is used to hold the values returned by
		 ** sqlite3_vtab_in_first() and sqlite3_vtab_in_next().
		 */
	_171:
		; /* New ValueList object to put in reg[P2] */
		pC34 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pRhs = Xsqlite3_malloc64(tls, uint64(8))
		if pRhs == uintptr(0) {
			goto no_mem
		}
		(*TValueList)(unsafe.Pointer(pRhs)).FpCsr = *(*uintptr)(unsafe.Pointer(pC34 + 36))
		(*TValueList)(unsafe.Pointer(pRhs)).FpOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		pOut = _out2Prerelease(tls, p, pOp)
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Null)
		_sqlite3VdbeMemSetPointer(tls, pOut, pRhs, __ccgo_ts+7688, __ccgo_fp(_sqlite3VdbeValueListFree))
		goto _189
		/* Opcode: VFilter P1 P2 P3 P4 *
		 ** Synopsis: iplan=r[P3] zplan='P4'
		 **
		 ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
		 ** the filtered result set is empty.
		 **
		 ** P4 is either NULL or a string that was generated by the xBestIndex
		 ** method of the module.  The interpretation of the P4 string is left
		 ** to the module implementation.
		 **
		 ** This opcode invokes the xFilter method on the virtual table specified
		 ** by P1.  The integer query plan parameter to xFilter is stored in register
		 ** P3. Register P3+1 stores the argc parameter to be passed to the
		 ** xFilter method. Registers P3+2..P3+1+argc are the argc
		 ** additional parameters which are passed to
		 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
		 **
		 ** A jump is made to P2 if the result set after filtering would be empty.
		 */
	_172:
		;
		pQuery = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		pArgc = pQuery + 1*40
		pCur4 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pVCur1 = *(*uintptr)(unsafe.Pointer(pCur4 + 36))
		pVtab3 = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVCur1)).FpVtab
		pModule3 = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab3)).FpModule
		/* Grab the index number and argc parameters */
		nArg = int32(*(*Ti64)(unsafe.Pointer(pArgc)))
		iQuery = int32(*(*Ti64)(unsafe.Pointer(pQuery)))
		/* Invoke the xFilter method */
		apArg = (*TVdbe)(unsafe.Pointer(p)).FapArg
		i6 = 0
		for {
			if !(i6 < nArg) {
				break
			}
			**(**uintptr)(__ccgo_up(apArg + uintptr(i6)*4)) = pArgc + uintptr(i6+int32(1))*40
			goto _291
		_291:
			;
			i6 = i6 + 1
		}
		rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule3)).FxFilter})))(tls, pVCur1, iQuery, *(*uintptr)(unsafe.Pointer(pOp + 16)), nArg, apArg)
		_sqlite3VtabImportErrmsg(tls, p, pVtab3)
		if rc != 0 {
			goto abort_due_to_error
		}
		res13 = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule3)).FxEof})))(tls, pVCur1)
		(*TVdbeCursor)(unsafe.Pointer(pCur4)).FnullRow = uint8(0)
		if res13 != 0 {
			goto jump_to_p2
		}
		goto _189
		/* Opcode: VColumn P1 P2 P3 * P5
		 ** Synopsis: r[P3]=vcolumn(P2)
		 **
		 ** Store in register P3 the value of the P2-th column of
		 ** the current row of the virtual-table of cursor P1.
		 **
		 ** If the VColumn opcode is being used to fetch the value of
		 ** an unchanging column during an UPDATE operation, then the P5
		 ** value is OPFLAG_NOCHNG.  This will cause the sqlite3_vtab_nochange()
		 ** function to return true inside the xColumn method of the virtual
		 ** table implementation.  The P5 column might also contain other
		 ** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
		 ** unused by OP_VColumn.
		 */
	_173:
		;
		pCur5 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		pDest2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		if (*TVdbeCursor)(unsafe.Pointer(pCur5)).FnullRow != 0 {
			_sqlite3VdbeMemSetNull(tls, pDest2)
			goto _189
		}
		pVtab4 = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pCur5 + 36)))).FpVtab
		pModule4 = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab4)).FpModule
		libc.Xmemset(tls, bp+680, 0, uint32(28))
		(**(**Tsqlite3_context)(__ccgo_up(bp + 680))).FpOut = pDest2
		(**(**Tsqlite3_context)(__ccgo_up(bp + 680))).Fenc = encoding
		(**(**TFuncDef)(__ccgo_up(bp + 708))).FpUserData = uintptr(0)
		(**(**TFuncDef)(__ccgo_up(bp + 708))).FfuncFlags = uint32(SQLITE_RESULT_SUBTYPE)
		(**(**Tsqlite3_context)(__ccgo_up(bp + 680))).FpFunc = bp + 708
		if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_NOCHNG) != 0 {
			_sqlite3VdbeMemSetNull(tls, pDest2)
			(*TMem)(unsafe.Pointer(pDest2)).Fflags = uint16(libc.Int32FromInt32(MEM_Null) | libc.Int32FromInt32(MEM_Zero))
			*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pDest2)).Fu)) = 0
		} else {
			(*TMem)(unsafe.Pointer(pDest2)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pDest2)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Null))
		}
		rc = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule4)).FxColumn})))(tls, *(*uintptr)(unsafe.Pointer(pCur5 + 36)), bp+680, (*TOp)(unsafe.Pointer(pOp)).Fp2)
		_sqlite3VtabImportErrmsg(tls, p, pVtab4)
		if (**(**Tsqlite3_context)(__ccgo_up(bp + 680))).FisError > 0 {
			_sqlite3VdbeError(tls, p, __ccgo_ts+4729, libc.VaList(bp+768, Xsqlite3_value_text(tls, pDest2)))
			rc = (**(**Tsqlite3_context)(__ccgo_up(bp + 680))).FisError
		}
		_sqlite3VdbeChangeEncoding(tls, pDest2, int32(encoding))
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: VNext P1 P2 * * *
		 **
		 ** Advance virtual table P1 to the next row in its result set and
		 ** jump to instruction P2.  Or, if the virtual table has reached
		 ** the end of its result set, then fall through to the next instruction.
		 */
	_174:
		;
		pCur6 = **(**uintptr)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*4))
		if (*TVdbeCursor)(unsafe.Pointer(pCur6)).FnullRow != 0 {
			goto _189
		}
		pVtab5 = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pCur6 + 36)))).FpVtab
		pModule5 = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab5)).FpModule
		/* Invoke the xNext() method of the module. There is no way for the
		 ** underlying implementation to return an error if one occurs during
		 ** xNext(). Instead, if an error occurs, true is returned (indicating that
		 ** data is available) and the error code returned when xColumn or
		 ** some other method is next invoked on the save virtual table cursor.
		 */
		rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule5)).FxNext})))(tls, *(*uintptr)(unsafe.Pointer(pCur6 + 36)))
		_sqlite3VtabImportErrmsg(tls, p, pVtab5)
		if rc != 0 {
			goto abort_due_to_error
		}
		res14 = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule5)).FxEof})))(tls, *(*uintptr)(unsafe.Pointer(pCur6 + 36)))
		if !(res14 != 0) {
			/* If there is data, jump to P2 */
			goto jump_to_p2_and_check_for_interrupt
		}
		goto check_for_interrupt
		/* Opcode: VRename P1 * * P4 *
		 **
		 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
		 ** This opcode invokes the corresponding xRename method. The value
		 ** in register P1 is passed as the zName argument to the xRename method.
		 */
	_175:
		;
		isLegacy = int32((*Tsqlite3)(unsafe.Pointer(db)).Fflags & libc.Uint64FromInt32(SQLITE_LegacyAlter))
		**(**Tu64)(__ccgo_up(db + 32)) |= uint64(SQLITE_LegacyAlter)
		pVtab6 = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab
		pName = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		rc = _sqlite3VdbeChangeEncoding(tls, pName, int32(SQLITE_UTF8))
		if rc != 0 {
			goto abort_due_to_error
		}
		rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVtab6)).FpModule)).FxRename})))(tls, pVtab6, (*TMem)(unsafe.Pointer(pName)).Fz)
		if isLegacy == 0 {
			**(**Tu64)(__ccgo_up(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_LegacyAlter)
		}
		_sqlite3VtabImportErrmsg(tls, p, pVtab6)
		libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 0, 0x3)
		if rc != 0 {
			goto abort_due_to_error
		}
		goto _189
		/* Opcode: VUpdate P1 P2 P3 P4 P5
		 ** Synopsis: data=r[P3@P2]
		 **
		 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
		 ** This opcode invokes the corresponding xUpdate method. P2 values
		 ** are contiguous memory cells starting at P3 to pass to the xUpdate
		 ** invocation. The value in register (P3+P2-1) corresponds to the
		 ** p2th element of the argv array passed to xUpdate.
		 **
		 ** The xUpdate method will do a DELETE or an INSERT or both.
		 ** The argv[0] element (which corresponds to memory cell P3)
		 ** is the rowid of a row to delete.  If argv[0] is NULL then no
		 ** deletion occurs.  The argv[1] element is the rowid of the new
		 ** row.  This can be NULL to have the virtual table select the new
		 ** rowid for itself.  The subsequent elements in the array are
		 ** the values of columns in the new row.
		 **
		 ** If P2==1 then no insert is performed.  argv[0] is the rowid of
		 ** a row to delete.
		 **
		 ** P1 is a boolean flag. If it is set to true and the xUpdate call
		 ** is successful, then the value returned by sqlite3_last_insert_rowid()
		 ** is set to the value of the rowid for the row just inserted.
		 **
		 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
		 ** apply in the case of a constraint failure on an insert or update.
		 */
	_176:
		;
		**(**Tsqlite_int64)(__ccgo_up(bp + 752)) = 0
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			goto no_mem
		}
		pVtab7 = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab
		if pVtab7 == uintptr(0) || (*Tsqlite3_vtab)(unsafe.Pointer(pVtab7)).FpModule == uintptr(0) {
			rc = int32(SQLITE_LOCKED)
			goto abort_due_to_error
		}
		pModule6 = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab7)).FpModule
		nArg1 = (*TOp)(unsafe.Pointer(pOp)).Fp2
		if (*Tsqlite3_module)(unsafe.Pointer(pModule6)).FxUpdate != 0 {
			vtabOnConflict = (*Tsqlite3)(unsafe.Pointer(db)).FvtabOnConflict
			apArg1 = (*TVdbe)(unsafe.Pointer(p)).FapArg
			pX1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
			i7 = 0
			for {
				if !(i7 < nArg1) {
					break
				}
				**(**uintptr)(__ccgo_up(apArg1 + uintptr(i7)*4)) = pX1
				pX1 += 40
				goto _292
			_292:
				;
				i7 = i7 + 1
			}
			(*Tsqlite3)(unsafe.Pointer(db)).FvtabOnConflict = uint8((*TOp)(unsafe.Pointer(pOp)).Fp5)
			rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule6)).FxUpdate})))(tls, pVtab7, nArg1, apArg1, bp+752)
			(*Tsqlite3)(unsafe.Pointer(db)).FvtabOnConflict = vtabOnConflict
			_sqlite3VtabImportErrmsg(tls, p, pVtab7)
			if rc == SQLITE_OK && (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 {
				(*Tsqlite3)(unsafe.Pointer(db)).FlastRowid = **(**Tsqlite_int64)(__ccgo_up(bp + 752))
			}
			if rc&int32(0xff) == int32(SQLITE_CONSTRAINT) && (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FbConstraint != 0 {
				if int32((*TOp)(unsafe.Pointer(pOp)).Fp5) == int32(OE_Ignore) {
					rc = SQLITE_OK
				} else {
					if int32((*TOp)(unsafe.Pointer(pOp)).Fp5) == int32(OE_Replace) {
						v190 = int32(OE_Abort)
					} else {
						v190 = int32((*TOp)(unsafe.Pointer(pOp)).Fp5)
					}
					(*TVdbe)(unsafe.Pointer(p)).FerrorAction = uint8(v190)
				}
			} else {
				(*TVdbe)(unsafe.Pointer(p)).FnChange = (*TVdbe)(unsafe.Pointer(p)).FnChange + 1
			}
			if rc != 0 {
				goto abort_due_to_error
			}
		}
		goto _189
		/* Opcode: Pagecount P1 P2 * * *
		 **
		 ** Write the current number of pages in database P1 to memory cell P2.
		 */
	_177:
		; /* out2 */
		pOut = _out2Prerelease(tls, p, pOp)
		*(*Ti64)(unsafe.Pointer(pOut)) = int64(_sqlite3BtreeLastPage(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpBt))
		goto _189
		/* Opcode: MaxPgcnt P1 P2 P3 * *
		 **
		 ** Try to set the maximum page count for database P1 to the value in P3.
		 ** Do not let the maximum page count fall below the current page count and
		 ** do not change the maximum page count value if P3==0.
		 **
		 ** Store the maximum page count after the change in register P2.
		 */
	_178:
		;
		pOut = _out2Prerelease(tls, p, pOp)
		pBt3 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*16))).FpBt
		newMax = uint32(0)
		if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 {
			newMax = _sqlite3BtreeLastPage(tls, pBt3)
			if newMax < uint32((*TOp)(unsafe.Pointer(pOp)).Fp3) {
				newMax = uint32((*TOp)(unsafe.Pointer(pOp)).Fp3)
			}
		}
		*(*Ti64)(unsafe.Pointer(pOut)) = int64(_sqlite3BtreeMaxPageCount(tls, pBt3, newMax))
		goto _189
		/* Opcode: Function P1 P2 P3 P4 *
		 ** Synopsis: r[P3]=func(r[P2@NP])
		 **
		 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
		 ** contains a pointer to the function to be run) with arguments taken
		 ** from register P2 and successors.  The number of arguments is in
		 ** the sqlite3_context object that P4 points to.
		 ** The result of the function is stored
		 ** in register P3.  Register P3 must not be one of the function inputs.
		 **
		 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
		 ** function was determined to be constant at compile time. If the first
		 ** argument was constant then bit 0 of P1 is set. This is used to determine
		 ** whether meta data associated with a user function argument using the
		 ** sqlite3_set_auxdata() API may be safely retained until the next
		 ** invocation of this opcode.
		 **
		 ** See also: AggStep, AggFinal, PureFunc
		 */
		/* Opcode: PureFunc P1 P2 P3 P4 *
		 ** Synopsis: r[P3]=func(r[P2@NP])
		 **
		 ** Invoke a user function (P4 is a pointer to an sqlite3_context object that
		 ** contains a pointer to the function to be run) with arguments taken
		 ** from register P2 and successors.  The number of arguments is in
		 ** the sqlite3_context object that P4 points to.
		 ** The result of the function is stored
		 ** in register P3.  Register P3 must not be one of the function inputs.
		 **
		 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
		 ** function was determined to be constant at compile time. If the first
		 ** argument was constant then bit 0 of P1 is set. This is used to determine
		 ** whether meta data associated with a user function argument using the
		 ** sqlite3_set_auxdata() API may be safely retained until the next
		 ** invocation of this opcode.
		 **
		 ** This opcode works exactly like OP_Function.  The only difference is in
		 ** its name.  This opcode is used in places where the function must be
		 ** purely non-deterministic.  Some built-in date/time functions can be
		 ** either deterministic of non-deterministic, depending on their arguments.
		 ** When those function are used in a non-deterministic way, they will check
		 ** to see if they were called using OP_PureFunc instead of OP_Function, and
		 ** if they were, they throw an error.
		 **
		 ** See also: AggStep, AggFinal, Function
		 */
	_180:
		; /* group */
	_179:
		;
		pCtx2 = *(*uintptr)(unsafe.Pointer(pOp + 16))
		/* If this function is inside of a trigger, the register array in aMem[]
		 ** might change from one evaluation to the next.  The next block of code
		 ** checks to see if the register array has changed, and if so it
		 ** reinitializes the relevant parts of the sqlite3_context object */
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*40
		if (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FpOut != pOut {
			(*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FpVdbe = p
			(*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FpOut = pOut
			(*Tsqlite3_context)(unsafe.Pointer(pCtx2)).Fenc = encoding
			i8 = int32((*Tsqlite3_context)(unsafe.Pointer(pCtx2)).Fargc) - int32(1)
			for {
				if !(i8 >= 0) {
					break
				}
				*(*uintptr)(unsafe.Pointer(pCtx2 + 28 + uintptr(i8)*4)) = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2+i8)*40
				goto _294
			_294:
				;
				i8 = i8 - 1
			}
		}
		(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Null))
		(*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FpFunc)).FxSFunc})))(tls, pCtx2, int32((*Tsqlite3_context)(unsafe.Pointer(pCtx2)).Fargc), pCtx2+28) /* IMP: R-24505-23230 */
		/* If the function returned an error, throw an exception */
		if (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FisError != 0 {
			if (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FisError > 0 {
				_sqlite3VdbeError(tls, p, __ccgo_ts+4729, libc.VaList(bp+768, Xsqlite3_value_text(tls, pOut)))
				rc = (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FisError
			}
			_sqlite3VdbeDeleteAuxData(tls, db, p+228, (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FiOp, (*TOp)(unsafe.Pointer(pOp)).Fp1)
			(*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FisError = 0
			if rc != 0 {
				goto abort_due_to_error
			}
		}
		goto _189
		/* Opcode: ClrSubtype P1 * * * *
		 ** Synopsis:  r[P1].subtype = 0
		 **
		 ** Clear the subtype from register P1.
		 */
	_181:
		; /* in1 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		v191 = pIn1 + 16
		*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^libc.Int32FromInt32(MEM_Subtype))
		goto _189
		/* Opcode: GetSubtype P1 P2 * * *
		 ** Synopsis:  r[P2] = r[P1].subtype
		 **
		 ** Extract the subtype value from register P1 and write that subtype
		 ** into register P2.  If P1 has no subtype, then P1 gets a NULL.
		 */
	_182:
		; /* in1 out2 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Subtype) != 0 {
			_sqlite3VdbeMemSetInt64(tls, pOut, int64((*TMem)(unsafe.Pointer(pIn1)).FeSubtype))
		} else {
			_sqlite3VdbeMemSetNull(tls, pOut)
		}
		goto _189
		/* Opcode: SetSubtype P1 P2 * * *
		 ** Synopsis:  r[P2].subtype = r[P1]
		 **
		 ** Set the subtype value of register P2 to the integer from register P1.
		 ** If P1 is NULL, clear the subtype from p2.
		 */
	_183:
		; /* in1 out2 */
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*40
		if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) != 0 {
			v191 = pOut + 16
			*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) & ^libc.Int32FromInt32(MEM_Subtype))
		} else {
			v191 = pOut + 16
			*(*Tu16)(unsafe.Pointer(v191)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v191))) | libc.Int32FromInt32(MEM_Subtype))
			(*TMem)(unsafe.Pointer(pOut)).FeSubtype = uint8(*(*Ti64)(unsafe.Pointer(pIn1)) & libc.Int64FromInt32(0xff))
		}
		goto _189
		/* Opcode: FilterAdd P1 * P3 P4 *
		 ** Synopsis: filter(P1) += key(P3@P4)
		 **
		 ** Compute a hash on the P4 registers starting with r[P3] and
		 ** add that hash to the bloom filter contained in r[P1].
		 */
	_184:
		;
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		h = _filterHash(tls, aMem, pOp)
		h = h % uint64((*TMem)(unsafe.Pointer(pIn1)).Fn*libc.Int32FromInt32(8))
		v191 = (*TMem)(unsafe.Pointer(pIn1)).Fz + uintptr(h/uint64(8))
		*(*int8)(unsafe.Pointer(v191)) = int8(int32(*(*int8)(unsafe.Pointer(v191))) | libc.Int32FromInt32(1)<<(h&libc.Uint64FromInt32(7)))
		goto _189
		/* Opcode: Filter P1 P2 P3 P4 *
		 ** Synopsis: if key(P3@P4) not in filter(P1) goto P2
		 **
		 ** Compute a hash on the key contained in the P4 registers starting
		 ** with r[P3].  Check to see if that hash is found in the
		 ** bloom filter hosted by register P1.  If it is not present then
		 ** maybe jump to P2.  Otherwise fall through.
		 **
		 ** False negatives are harmless.  It is always safe to fall through,
		 ** even if the value is in the bloom filter.  A false negative causes
		 ** more CPU cycles to be used, but it should still yield the correct
		 ** answer.  However, an incorrect answer may well arise from a
		 ** false positive - if the jump is taken when it should fall through.
		 */
	_185:
		;
		pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*40
		h1 = _filterHash(tls, aMem, pOp)
		h1 = h1 % uint64((*TMem)(unsafe.Pointer(pIn1)).Fn*libc.Int32FromInt32(8))
		if int32(**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pIn1)).Fz + uintptr(h1/uint64(8)))))&(int32(1)<<(h1&uint64(7))) == 0 {
			**(**Tu32)(__ccgo_up(p + 164 + 8*4)) = **(**Tu32)(__ccgo_up(p + 164 + 8*4)) + 1
			goto jump_to_p2
		} else {
			**(**Tu32)(__ccgo_up(p + 164 + 7*4)) = **(**Tu32)(__ccgo_up(p + 164 + 7*4)) + 1
		}
		goto _189
		/* Opcode: Trace P1 P2 * P4 *
		 **
		 ** Write P4 on the statement trace output if statement tracing is
		 ** enabled.
		 **
		 ** Operand P1 must be 0x7fffffff and P2 must positive.
		 */
		/* Opcode: Init P1 P2 P3 P4 *
		 ** Synopsis: Start at P2
		 **
		 ** Programs contain a single instance of this opcode as the very first
		 ** opcode.
		 **
		 ** If tracing is enabled (by the sqlite3_trace()) interface, then
		 ** the UTF-8 string contained in P4 is emitted on the trace callback.
		 ** Or if P4 is blank, use the string returned by sqlite3_sql().
		 **
		 ** If P2 is not zero, jump to instruction P2.
		 **
		 ** Increment the value of P1 so that OP_Once opcodes will jump the
		 ** first time they are evaluated for this run.
		 **
		 ** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
		 ** error is encountered.
		 */
	_187:
		;
	_186:
		;
		/* If the P4 argument is not NULL, then it must be an SQL comment string.
		 ** The "--" string is broken up to prevent false-positives with srcck1.c.
		 **
		 ** This assert() provides evidence for:
		 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
		 ** would have been returned by the legacy sqlite3_trace() interface by
		 ** using the X argument when X begins with "--" and invoking
		 ** sqlite3_expanded_sql(P) otherwise.
		 */
		/* OP_Init is always instruction 0 */
		if v217 = int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&(libc.Int32FromInt32(SQLITE_TRACE_STMT)|libc.Int32FromInt32(SQLITE_TRACE_LEGACY)) != 0 && int32((*TVdbe)(unsafe.Pointer(p)).FminWriteFileFormat) != int32(254); v217 {
			if *(*uintptr)(unsafe.Pointer(pOp + 16)) != 0 {
				v194 = *(*uintptr)(unsafe.Pointer(pOp + 16))
			} else {
				v194 = (*TVdbe)(unsafe.Pointer(p)).FzSql
			}
			v191 = v194
			zTrace = v191
		}
		if v217 && v191 != uintptr(0) {
			if int32((*Tsqlite3)(unsafe.Pointer(db)).FmTrace)&int32(SQLITE_TRACE_LEGACY) != 0 {
				z2 = _sqlite3VdbeExpandSql(tls, p, zTrace)
				(*(*func(*libc.TLS, uintptr, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).Ftrace.FxLegacy})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, z2)
				Xsqlite3_free(tls, z2)
			} else {
				if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec > int32(1) {
					z3 = _sqlite3MPrintf(tls, db, __ccgo_ts+7698, libc.VaList(bp+768, zTrace))
					(*(*func(*libc.TLS, Tu32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{*(*uintptr)(unsafe.Pointer(&(*Tsqlite3)(unsafe.Pointer(db)).Ftrace))})))(tls, uint32(SQLITE_TRACE_STMT), (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, p, z3)
					_sqlite3DbFree(tls, db, z3)
				} else {
					(*(*func(*libc.TLS, Tu32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{*(*uintptr)(unsafe.Pointer(&(*Tsqlite3)(unsafe.Pointer(db)).Ftrace))})))(tls, uint32(SQLITE_TRACE_STMT), (*Tsqlite3)(unsafe.Pointer(db)).FpTraceArg, p, zTrace)
				}
			}
		}
		if (*TOp)(unsafe.Pointer(pOp)).Fp1 >= _sqlite3Config.FiOnceResetThreshold {
			if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Trace) {
				goto _189
			}
			i9 = int32(1)
			for {
				if !(i9 < (*TVdbe)(unsafe.Pointer(p)).FnOp) {
					break
				}
				if int32((**(**TOp)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i9)*20))).Fopcode) == int32(OP_Once) {
					(**(**TOp)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i9)*20))).Fp1 = 0
				}
				goto _302
			_302:
				;
				i9 = i9 + 1
			}
			(*TOp)(unsafe.Pointer(pOp)).Fp1 = 0
		}
		(*TOp)(unsafe.Pointer(pOp)).Fp1 = (*TOp)(unsafe.Pointer(pOp)).Fp1 + 1
		**(**Tu32)(__ccgo_up(p + 164 + 6*4)) = **(**Tu32)(__ccgo_up(p + 164 + 6*4)) + 1
		goto jump_to_p2
		/* Opcode: Noop * * * * *
		 **
		 ** Do nothing.  Continue downward to the next opcode.
		 */
		/* Opcode: Explain P1 P2 P3 P4 *
		 **
		 ** This is the same as OP_Noop during normal query execution.  The
		 ** purpose of this opcode is to hold information about the query
		 ** plan for the purpose of EXPLAIN QUERY PLAN output.
		 **
		 ** The P4 value is human-readable text that describes the query plan
		 ** element.  Something like "SCAN t1" or "SEARCH t2 USING INDEX t2x1".
		 **
		 ** The P1 value is the ID of the current element and P2 is the parent
		 ** element for the case of nested query plan elements.  If P2 is zero
		 ** then this element is a top-level element.
		 **
		 ** For loop elements, P3 is the estimated code of each invocation of this
		 ** element.
		 **
		 ** As with all opcodes, the meanings of the parameters for OP_Explain
		 ** are subject to change from one release to the next.  Applications
		 ** should not attempt to interpret or use any of the information
		 ** contained in the OP_Explain opcode.  The information provided by this
		 ** opcode is intended for testing and debugging use only.
		 */
	_188:
		; /* This is really OP_Noop, OP_Explain */
		goto _189
		/*****************************************************************************
		 ** The cases of the switch statement above this line should all be indented
		 ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
		 ** readability.  From this point on down, the normal indentation rules are
		 ** restored.
		 *****************************************************************************/
	_189:
		;
		/* The following code adds nothing to the actual functionality
		 ** of the program.  It is only here for testing and debugging.
		 ** On the other hand, it does burn CPU cycles every time through
		 ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
		 */
		goto _1
	_1:
		;
		pOp += 20
	} /* The end of the for(;;) loop the loops through opcodes */
	/* If we reach this point, it means that execution is finished with
	 ** an error of some kind.
	 */
	goto abort_due_to_error
abort_due_to_error:
	;
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		rc = int32(SQLITE_NOMEM)
	} else {
		if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(33)<<libc.Int32FromInt32(8) {
			rc = _sqlite3CorruptError(tls, int32(105898))
		}
	}
	if (*TVdbe)(unsafe.Pointer(p)).FzErrMsg == uintptr(0) && rc != libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<<libc.Int32FromInt32(8) {
		_sqlite3VdbeError(tls, p, __ccgo_ts+4729, libc.VaList(bp+768, _sqlite3ErrStr(tls, rc)))
	}
	(*TVdbe)(unsafe.Pointer(p)).Frc = rc
	_sqlite3SystemError(tls, db, rc)
	_sqlite3VdbeLogAbort(tls, p, rc, pOp, aOp)
	if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) == int32(VDBE_RUN_STATE) {
		_sqlite3VdbeHalt(tls, p)
	}
	if rc == libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(12)<<libc.Int32FromInt32(8) {
		_sqlite3OomFault(tls, db)
	}
	if rc == int32(SQLITE_CORRUPT) && int32((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit) == 0 {
		**(**Tu64)(__ccgo_up(db + 32)) |= uint64(libc.Int32FromInt32(0x00002)) << libc.Int32FromInt32(32)
	}
	rc = int32(SQLITE_ERROR)
	if int32(resetSchemaOnFault) > 0 {
		_sqlite3ResetOneSchema(tls, db, int32(resetSchemaOnFault)-int32(1))
	}
	/* This is the only way out of this procedure.  We have to
	 ** release the mutexes on btrees that were acquired at the
	 ** top. */
	goto vdbe_return
vdbe_return:
	;
	for nVmStep >= nProgressLimit && (*Tsqlite3)(unsafe.Pointer(db)).FxProgress != uintptr(0) {
		nProgressLimit = nProgressLimit + uint64((*Tsqlite3)(unsafe.Pointer(db)).FnProgressOps)
		if (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxProgress})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpProgressArg) != 0 {
			nProgressLimit = libc.Uint64FromUint32(0xffffffff) | libc.Uint64FromUint32(0xffffffff)<<libc.Int32FromInt32(32)
			rc = int32(SQLITE_INTERRUPT)
			goto abort_due_to_error
		}
	}
	**(**Tu32)(__ccgo_up(p + 164 + 4*4)) += uint32(int32(nVmStep))
	if (*TVdbe)(unsafe.Pointer(p)).FlockMask != uint32(0) {
		_sqlite3VdbeLeave(tls, p)
	}
	return rc
	/* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
	 ** is encountered.
	 */
	goto too_big
too_big:
	;
	_sqlite3VdbeError(tls, p, __ccgo_ts+6807, 0)
	rc = int32(SQLITE_TOOBIG)
	goto abort_due_to_error
	/* Jump to here if a malloc() fails.
	 */
	goto no_mem
no_mem:
	;
	_sqlite3OomFault(tls, db)
	_sqlite3VdbeError(tls, p, __ccgo_ts+1681, 0)
	rc = int32(SQLITE_NOMEM)
	goto abort_due_to_error
	/* Jump to here if the sqlite3_interrupt() API sets the interrupt
	 ** flag.
	 */
	goto abort_due_to_interrupt
abort_due_to_interrupt:
	;
	rc = int32(SQLITE_INTERRUPT)
	goto abort_due_to_error
	return r
}

// C documentation
//
//	/*
//	** This function returns a pointer to a nul-terminated string in memory
//	** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
//	** string contains a copy of zRawSql but with host parameters expanded to
//	** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
//	** then the returned string holds a copy of zRawSql with "-- " prepended
//	** to each line of text.
//	**
//	** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
//	** then long strings and blobs are truncated to that many bytes.  This
//	** can be used to prevent unreasonably large trace strings when dealing
//	** with large (multi-megabyte) strings and blobs.
//	**
//	** The calling function is responsible for making sure the memory returned
//	** is eventually freed.
//	**
//	** ALGORITHM:  Scan the input string looking for host parameters in any of
//	** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
//	** string literals, quoted identifier names, and comments.  For text forms,
//	** the host parameter index is found by scanning the prepared
//	** statement for the corresponding OP_Variable opcode.  Once the host
//	** parameter index is known, locate the value in p->aVar[].  Then render
//	** the value as a literal in place of the host parameter name.
//	*/
func _sqlite3VdbeExpandSql(tls *libc.TLS, p uintptr, zRawSql uintptr) (r uintptr) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var db, pVar, zStart, v1 uintptr
	var enc Tu8
	var i, nOut, nOut1, nextIndex, v2 int32
	var n Ti64
	var _ /* idx at bp+0 */ int32
	var _ /* nToken at bp+8 */ Ti64
	var _ /* out at bp+16 */ TStrAccum
	var _ /* utf8 at bp+40 */ TMem
	_, _, _, _, _, _, _, _, _, _, _ = db, enc, i, n, nOut, nOut1, nextIndex, pVar, zStart, v1, v2 /* The database connection */
	**(**int32)(__ccgo_up(bp)) = 0                                                                /* Index of a host parameter */
	nextIndex = int32(1)                                                                          /* Used to convert UTF16 into UTF8 for display */
	db = (*TVdbe)(unsafe.Pointer(p)).Fdb
	_sqlite3StrAccumInit(tls, bp+16, uintptr(0), uintptr(0), 0, **(**int32)(__ccgo_up(db + 120)))
	if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec > int32(1) {
		for **(**int8)(__ccgo_up(zRawSql)) != 0 {
			zStart = zRawSql
			for {
				v1 = zRawSql
				zRawSql = zRawSql + 1
				if !(int32(**(**int8)(__ccgo_up(v1))) != int32('\n') && **(**int8)(__ccgo_up(zRawSql)) != 0) {
					break
				}
			}
			Xsqlite3_str_append(tls, bp+16, __ccgo_ts+6930, int32(3))
			Xsqlite3_str_append(tls, bp+16, zStart, int32(zRawSql)-int32(zStart))
		}
	} else {
		if int32((*TVdbe)(unsafe.Pointer(p)).FnVar) == 0 {
			Xsqlite3_str_append(tls, bp+16, zRawSql, _sqlite3Strlen30(tls, zRawSql))
		} else {
			for **(**int8)(__ccgo_up(zRawSql)) != 0 {
				n = _findNextHostParameter(tls, zRawSql, bp+8)
				Xsqlite3_str_append(tls, bp+16, zRawSql, int32(n))
				zRawSql = zRawSql + uintptr(n)
				if **(**Ti64)(__ccgo_up(bp + 8)) == 0 {
					break
				}
				if int32(**(**int8)(__ccgo_up(zRawSql))) == int32('?') {
					if **(**Ti64)(__ccgo_up(bp + 8)) > int64(1) {
						_sqlite3GetInt32(tls, zRawSql+1, bp)
					} else {
						**(**int32)(__ccgo_up(bp)) = nextIndex
					}
				} else {
					**(**int32)(__ccgo_up(bp)) = _sqlite3VdbeParameterIndex(tls, p, zRawSql, int32(**(**Ti64)(__ccgo_up(bp + 8))))
				}
				zRawSql = zRawSql + uintptr(**(**Ti64)(__ccgo_up(bp + 8)))
				if **(**int32)(__ccgo_up(bp))+int32(1) > nextIndex {
					v2 = **(**int32)(__ccgo_up(bp)) + int32(1)
				} else {
					v2 = nextIndex
				}
				nextIndex = v2
				pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr(**(**int32)(__ccgo_up(bp))-int32(1))*40
				if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Null) != 0 {
					Xsqlite3_str_append(tls, bp+16, __ccgo_ts+1712, int32(4))
				} else {
					if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
						Xsqlite3_str_appendf(tls, bp+16, __ccgo_ts+1472, libc.VaList(bp+88, *(*Ti64)(unsafe.Pointer(pVar))))
					} else {
						if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Real) != 0 {
							Xsqlite3_str_appendf(tls, bp+16, __ccgo_ts+6934, libc.VaList(bp+88, *(*float64)(unsafe.Pointer(pVar))))
						} else {
							if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Str) != 0 { /* Number of bytes of the string text to include in output */
								enc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc
								if int32(enc) != int32(SQLITE_UTF8) {
									libc.Xmemset(tls, bp+40, 0, uint32(40))
									(**(**TMem)(__ccgo_up(bp + 40))).Fdb = db
									_sqlite3VdbeMemSetStr(tls, bp+40, (*TMem)(unsafe.Pointer(pVar)).Fz, int64((*TMem)(unsafe.Pointer(pVar)).Fn), enc, libc.UintptrFromInt32(0))
									if int32(SQLITE_NOMEM) == _sqlite3VdbeChangeEncoding(tls, bp+40, int32(SQLITE_UTF8)) {
										(**(**TStrAccum)(__ccgo_up(bp + 16))).FaccError = uint8(SQLITE_NOMEM)
										(**(**TStrAccum)(__ccgo_up(bp + 16))).FnAlloc = uint32(0)
									}
									pVar = bp + 40
								}
								nOut = (*TMem)(unsafe.Pointer(pVar)).Fn
								Xsqlite3_str_appendf(tls, bp+16, __ccgo_ts+6941, libc.VaList(bp+88, nOut, (*TMem)(unsafe.Pointer(pVar)).Fz))
								if int32(enc) != int32(SQLITE_UTF8) {
									_sqlite3VdbeMemRelease(tls, bp+40)
								}
							} else {
								if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Zero) != 0 {
									Xsqlite3_str_appendf(tls, bp+16, __ccgo_ts+6948, libc.VaList(bp+88, *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pVar)).Fu))))
								} else { /* Number of bytes of the blob to include in output */
									Xsqlite3_str_append(tls, bp+16, __ccgo_ts+6961, int32(2))
									nOut1 = (*TMem)(unsafe.Pointer(pVar)).Fn
									i = 0
									for {
										if !(i < nOut1) {
											break
										}
										Xsqlite3_str_appendf(tls, bp+16, __ccgo_ts+6964, libc.VaList(bp+88, int32(**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pVar)).Fz + uintptr(i))))&int32(0xff)))
										goto _3
									_3:
										;
										i = i + 1
									}
									Xsqlite3_str_append(tls, bp+16, __ccgo_ts+6969, int32(1))
								}
							}
						}
					}
				}
			}
		}
	}
	if (**(**TStrAccum)(__ccgo_up(bp + 16))).FaccError != 0 {
		Xsqlite3_str_reset(tls, bp+16)
	}
	return _sqlite3StrAccumFinish(tls, bp+16)
}

/************** End of vdbetrace.c *******************************************/
/************** Begin file vdbe.c ********************************************/
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** The code in this file implements the function that runs the
** bytecode of a prepared statement.
**
** Various scripts scan this source file in order to generate HTML
** documentation, headers files, or other derived files.  The formatting
** of the code in this file is, therefore, important.  See other comments
** in this file for details.  If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code.
 */
/* #include "sqliteInt.h" */
/* #include "vdbeInt.h" */

/*
** High-resolution hardware timer used for debugging and testing only.
 */

/*
** Invoke this macro on memory cells just prior to changing the
** value of the cell.  This macro verifies that shallow copies are
** not misused.  A shallow copy of a string or blob just copies a
** pointer to the string or blob, not the content.  If the original
** is changed while the copy is still in use, the string or blob might
** be changed out from under the copy.  This macro verifies that nothing
** like that ever happens.
 */

/*
** The following global variable is incremented every time a cursor
** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
** procedures use this information to make sure that indices are
** working correctly.  This variable has no function other than to
** help verify the correct operation of the library.
 */

/*
** When this global variable is positive, it gets decremented once before
** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
** field of the sqlite3 structure is set in order to simulate an interrupt.
**
** This facility is used for testing purposes only.  It does not function
** in an ordinary build.
 */

/*
** The next global variable is incremented each type the OP_Sort opcode
** is executed.  The test procedures use this information to make sure that
** sorting is occurring or not occurring at appropriate times.   This variable
** has no function other than to help verify the correct operation of the
** library.
 */

/*
** The next global variable records the size of the largest MEM_Blob
** or MEM_Str that has been used by a VDBE opcode.  The test procedures
** use this information to make sure that the zero-blob functionality
** is working correctly.   This variable has no function other than to
** help verify the correct operation of the library.
 */

/*
** This macro evaluates to true if either the update hook or the preupdate
** hook are enabled for database connect DB.
 */

/*
** The next global variable is incremented each time the OP_Found opcode
** is executed. This is used to test whether or not the foreign key
** operation implemented using OP_FkIsZero is working. This variable
** has no function other than to help verify the correct operation of the
** library.
 */

/*
** Test a register to see if it exceeds the current maximum blob size.
** If it does, record the new maximum blob size.
 */

/*
** Invoke the VDBE coverage callback, if that callback is defined.  This
** feature is used for test suite validation only and does not appear an
** production builds.
**
** M is the type of branch.  I is the direction taken for this instance of
** the branch.
**
**   M: 2 - two-way branch (I=0: fall-thru   1: jump                )
**      3 - two-way + NULL (I=0: fall-thru   1: jump      2: NULL   )
**      4 - OP_Jump        (I=0: jump p1     1: jump p2   2: jump p3)
**
** In other words, if M is 2, then I is either 0 (for fall-through) or
** 1 (for when the branch is taken).  If M is 3, the I is 0 for an
** ordinary fall-through, I is 1 if the branch was taken, and I is 2
** if the result of comparison is NULL.  For M=3, I=2 the jump may or
** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
** When M is 4, that means that an OP_Jump is being run.  I is 0, 1, or 2
** depending on if the operands are less than, equal, or greater than.
**
** iSrcLine is the source code line (from the __LINE__ macro) that
** generated the VDBE instruction combined with flag bits.  The source
** code line number is in the lower 24 bits of iSrcLine and the upper
** 8 bytes are flags.  The lower three bits of the flags indicate
** values for I that should never occur.  For example, if the branch is
** always taken, the flags should be 0x05 since the fall-through and
** alternate branch are never taken.  If a branch is never taken then
** flags should be 0x06 since only the fall-through approach is allowed.
**
** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
** interested in equal or not-equal.  In other words, I==0 and I==2
** should be treated as equivalent
**
** Since only a line number is retained, not the filename, this macro
** only works for amalgamation builds.  But that is ok, since these macros
** should be no-ops except for special builds used to measure test coverage.
 */

/*
** An ephemeral string value (signified by the MEM_Ephem flag) contains
** a pointer to a dynamically allocated string where some other entity
** is responsible for deallocating that string.  Because the register
** does not control the string, it might be deleted without the register
** knowing it.
**
** This routine converts an ephemeral string into a dynamically allocated
** string that the register itself controls.  In other words, it
** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
 */

/* Return true if the cursor was opened using the OP_OpenSorter opcode. */

// C documentation
//
//	/*
//	** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
//	** suitable for comparing serialized records to the unpacked record passed
//	** as the only argument.
//	*/
func _sqlite3VdbeFindCompare(tls *libc.TLS, p uintptr) (r TRecordCompare) {
	var flags int32
	_ = flags
	/* varintRecordCompareInt() and varintRecordCompareString() both assume
	 ** that the size-of-header varint that occurs at the start of each record
	 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
	 ** also assumes that it is safe to overread a buffer by at least the
	 ** maximum possible legal header size plus 8 bytes. Because there is
	 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
	 ** buffer passed to varintRecordCompareInt() this makes it convenient to
	 ** limit the size of the header to 64 bytes in cases where the first field
	 ** is an integer.
	 **
	 ** The easiest way to enforce this limit is to consider only records with
	 ** 13 fields or less. If the first field is an integer, the maximum legal
	 ** header size is (12*5 + 1 + 1) bytes.  */
	if int32((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo)).FnAllField) <= int32(13) {
		flags = int32((**(**TMem)(__ccgo_up((*TUnpackedRecord)(unsafe.Pointer(p)).FaMem))).Fflags)
		if **(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo)).FaSortFlags)) != 0 {
			if int32(**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo)).FaSortFlags)))&int32(KEYINFO_ORDER_BIGNULL) != 0 {
				return __ccgo_fp(_sqlite3VdbeRecordCompare)
			}
			(*TUnpackedRecord)(unsafe.Pointer(p)).Fr1 = int8(1)
			(*TUnpackedRecord)(unsafe.Pointer(p)).Fr2 = int8(-int32(1))
		} else {
			(*TUnpackedRecord)(unsafe.Pointer(p)).Fr1 = int8(-int32(1))
			(*TUnpackedRecord)(unsafe.Pointer(p)).Fr2 = int8(1)
		}
		if flags&int32(MEM_Int) != 0 {
			*(*Ti64)(unsafe.Pointer(p + 8)) = *(*Ti64)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FaMem))
			return __ccgo_fp(_vdbeRecordCompareInt)
		}
		if flags&(libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Null)|libc.Int32FromInt32(MEM_Blob)) == 0 && *(*uintptr)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo + 20)) == uintptr(0) {
			(*TUnpackedRecord)(unsafe.Pointer(p)).Fu.Fz = (**(**TMem)(__ccgo_up((*TUnpackedRecord)(unsafe.Pointer(p)).FaMem))).Fz
			(*TUnpackedRecord)(unsafe.Pointer(p)).Fn = (**(**TMem)(__ccgo_up((*TUnpackedRecord)(unsafe.Pointer(p)).FaMem))).Fn
			return __ccgo_fp(_vdbeRecordCompareString)
		}
	}
	return __ccgo_fp(_sqlite3VdbeRecordCompare)
}

// C documentation
//
//	/*
//	** This is called when the record in (*p) should be found in the index
//	** opened by cursor pCur, but was not. This may happen as part of a DELETE
//	** operation or an integrity check.
//	**
//	** One reason that an exact match was not found may be the EIIB bug - that
//	** a text-to-float conversion may have caused a real value in record (*p)
//	** to be slightly different from its counterpart on disk. This function
//	** attempts to find the right index record. If it does find the right
//	** record, it leaves *pCur pointing to it and sets (*pRes) to 0 before
//	** returning. Otherwise, (*pRes) is set to non-zero and an SQLite error
//	** code returned.
//	**
//	** The algorithm used to find the correct record is:
//	**
//	**   * Scan up to BTREE_FDK_RANGE entries either side of the current entry.
//	**     If parameter bIntegrity is false, then all fields that are indexed
//	**     expressions or virtual table columns are omitted from the comparison.
//	**     If bIntegrity is true, then small differences in real values in
//	**     such fields are overlooked, but they are not omitted from the comparison
//	**     altogether.
//	**
//	**   * If the above fails to find an entry and bIntegrity is false, search
//	**     the entire index.
//	*/
func _sqlite3VdbeFindIndexKey(tls *libc.TLS, pCur uintptr, pIdx uintptr, p uintptr, pRes uintptr, bIntegrity int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iCol, ii, nStep, rc, v2 int32
	var mask TBitmask
	var _ /* res at bp+0 */ int32
	_, _, _, _, _, _ = iCol, ii, mask, nStep, rc, v2
	nStep = 0
	**(**int32)(__ccgo_up(bp)) = int32(1)
	rc = SQLITE_OK
	ii = 0
	/* Calculate a mask based on the first 64 columns of the index. The mask
	 ** bit is set if the corresponding index field is either an expression
	 ** or a virtual column of the table.  */
	mask = uint64(0)
	ii = 0
	for {
		if int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
			v2 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
		} else {
			v2 = int32(libc.Uint32FromInt64(8) * libc.Uint32FromInt32(8))
		}
		if !(ii < v2) {
			break
		}
		iCol = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(ii)*2)))
		if iCol == -int32(2) || iCol >= 0 && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(iCol)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
			mask = mask | libc.Uint64FromInt32(1)<<ii
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	/* If the mask is 0 at this point, then the index contains no expressions
	 ** or virtual columns. So do not search for a match - return so that the
	 ** caller may declare the db corrupt immediately. Or, if mask is non-zero,
	 ** proceed.  */
	if mask != uint64(0) {
		/* Move the cursor back BTREE_FDK_RANGE entries. If this hits an EOF,
		 ** position the cursor at the first entry in the index and set nStep
		 ** to -1 so that the first loop below scans the entire index. Otherwise,
		 ** set nStep to BTREE_FDK_RANGE*2 so that the first loop below scans
		 ** just that many entries.  */
		ii = 0
		for {
			if !(_sqlite3BtreeEof(tls, pCur) == 0 && ii < int32(BTREE_FDK_RANGE)) {
				break
			}
			rc = _sqlite3BtreePrevious(tls, pCur, 0)
			goto _3
		_3:
			;
			ii = ii + 1
		}
		if rc == int32(SQLITE_DONE) {
			rc = _sqlite3BtreeFirst(tls, pCur, bp)
			nStep = -int32(1)
		} else {
			nStep = libc.Int32FromInt32(BTREE_FDK_RANGE) * libc.Int32FromInt32(2)
		}
		/* This loop runs at most twice to search for a key with matching PK
		 ** fields in the index. The second iteration always searches the entire
		 ** index. The first iteration searches nStep entries starting with the
		 ** current cursor entry if (nStep>=0), or the entire index if (nStep<0).  */
		for _sqlite3BtreeCursorIsValidNN(tls, pCur) != 0 {
			ii = 0
			for {
				if !(rc == SQLITE_OK && (ii < nStep || nStep < 0)) {
					break
				}
				rc = _vdbeIsMatchingIndexKey(tls, pCur, bIntegrity, mask, p, bp)
				if **(**int32)(__ccgo_up(bp)) == 0 || rc != SQLITE_OK {
					break
				}
				rc = _sqlite3BtreeNext(tls, pCur, 0)
				goto _4
			_4:
				;
				ii = ii + 1
			}
			if rc == int32(SQLITE_DONE) {
				rc = SQLITE_OK
			}
			if nStep < 0 || rc != SQLITE_OK || **(**int32)(__ccgo_up(bp)) == 0 || bIntegrity != 0 {
				break
			}
			/* The first, non-exhaustive, search failed to find an entry with
			 ** matching PK fields. So restart for an exhaustive search of the
			 ** entire index.  */
			nStep = -int32(1)
			rc = _sqlite3BtreeFirst(tls, pCur, bp)
		}
	}
	**(**int32)(__ccgo_up(pRes)) = **(**int32)(__ccgo_up(bp))
	return rc
}

// C documentation
//
//	/*
//	** Delete a VdbeFrame object and its contents. VdbeFrame objects are
//	** allocated by the OP_Program opcode in sqlite3VdbeExec().
//	*/
func _sqlite3VdbeFrameDelete(tls *libc.TLS, p uintptr) {
	var aMem, apCsr uintptr
	var i int32
	_, _, _ = aMem, apCsr, i
	aMem = p + uintptr((libc.Uint32FromInt64(88)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
	apCsr = aMem + uintptr((*TVdbeFrame)(unsafe.Pointer(p)).FnChildMem)*40
	i = 0
	for {
		if !(i < (*TVdbeFrame)(unsafe.Pointer(p)).FnChildCsr) {
			break
		}
		if **(**uintptr)(__ccgo_up(apCsr + uintptr(i)*4)) != 0 {
			_sqlite3VdbeFreeCursorNN(tls, (*TVdbeFrame)(unsafe.Pointer(p)).Fv, **(**uintptr)(__ccgo_up(apCsr + uintptr(i)*4)))
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	_releaseMemArray(tls, aMem, (*TVdbeFrame)(unsafe.Pointer(p)).FnChildMem)
	_sqlite3VdbeDeleteAuxData(tls, (*TVdbe)(unsafe.Pointer((*TVdbeFrame)(unsafe.Pointer(p)).Fv)).Fdb, p+40, -int32(1), 0)
	_sqlite3DbFree(tls, (*TVdbe)(unsafe.Pointer((*TVdbeFrame)(unsafe.Pointer(p)).Fv)).Fdb, p)
}

func _sqlite3VdbeFreeCursorNN(tls *libc.TLS, p uintptr, pCx uintptr) {
	var pModule, pVCur uintptr
	_, _ = pModule, pVCur
	if int32(TBool(*(*uint8)(unsafe.Pointer(pCx + 8))&0x10>>4)) != 0 {
		_freeCursorWithCache(tls, p, pCx)
		return
	}
	switch int32((*TVdbeCursor)(unsafe.Pointer(pCx)).FeCurType) {
	case int32(CURTYPE_SORTER):
		_sqlite3VdbeSorterClose(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, pCx)
	case CURTYPE_BTREE:
		_sqlite3BtreeCloseCursor(tls, *(*uintptr)(unsafe.Pointer(pCx + 36)))
	case int32(CURTYPE_VTAB):
		pVCur = *(*uintptr)(unsafe.Pointer(pCx + 36))
		pModule = (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVCur)).FpVtab)).FpModule
		(*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVCur)).FpVtab)).FnRef = (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVCur)).FpVtab)).FnRef - 1
		(*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule)).FxClose})))(tls, pVCur)
		break
	}
}

// C documentation
//
//	/*
//	** Return a pointer to an sqlite3_value structure containing the value bound
//	** parameter iVar of VM v. Except, if the value is an SQL NULL, return
//	** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
//	** constants) to the value before returning it.
//	**
//	** The returned value must be freed by the caller using sqlite3ValueFree().
//	*/
func _sqlite3VdbeGetBoundValue(tls *libc.TLS, v uintptr, iVar int32, aff Tu8) (r uintptr) {
	var pMem, pRet uintptr
	_, _ = pMem, pRet
	if v != 0 {
		pMem = (*TVdbe)(unsafe.Pointer(v)).FaVar + uintptr(iVar-int32(1))*40
		if 0 == int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Null) {
			pRet = _sqlite3ValueNew(tls, (*TVdbe)(unsafe.Pointer(v)).Fdb)
			if pRet != 0 {
				_sqlite3VdbeMemCopy(tls, pRet, pMem)
				_sqlite3ValueApplyAffinity(tls, pRet, aff, uint8(SQLITE_UTF8))
			}
			return pRet
		}
	}
	return uintptr(0)
}

// C documentation
//
//	/*
//	** This routine is called the when a VDBE tries to halt.  If the VDBE
//	** has made changes and is in autocommit mode, then commit those
//	** changes.  If a rollback is needed, then do the rollback.
//	**
//	** This routine is the only way to move the sqlite3eOpenState of a VM from
//	** SQLITE_STATE_RUN to SQLITE_STATE_HALT.  It is harmless to
//	** call this on a VM that is in the SQLITE_STATE_HALT state.
//	**
//	** Return an error code.  If the commit could not complete because of
//	** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
//	** means the close did not happen and needs to be repeated.
//	*/
func _sqlite3VdbeHalt(tls *libc.TLS, p uintptr) (r int32) {
	var db uintptr
	var eStatementOp, isSpecialError, mrc, rc, v1 int32
	_, _, _, _, _, _ = db, eStatementOp, isSpecialError, mrc, rc, v1 /* Used to store transient return codes */
	db = (*TVdbe)(unsafe.Pointer(p)).Fdb
	/* This function contains the logic that determines if a statement or
	 ** transaction will be committed or rolled back as a result of the
	 ** execution of this virtual machine.
	 **
	 ** If any of the following errors occur:
	 **
	 **     SQLITE_NOMEM
	 **     SQLITE_IOERR
	 **     SQLITE_FULL
	 **     SQLITE_INTERRUPT
	 **
	 ** Then the internal cache might have been left in an inconsistent
	 ** state.  We need to rollback the statement transaction, if there is
	 ** one, or the complete transaction if there is no statement transaction.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		(*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
	}
	_closeAllCursors(tls, p)
	/* No commit or rollback needed if the program never started or if the
	 ** SQL statement does not read or write a database file.  */
	if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x80>>7)) != 0 { /* Primary error code from p->rc */
		eStatementOp = 0 /* Set to true if a 'special' error */
		/* Lock all btrees used by the statement */
		_sqlite3VdbeEnter(tls, p)
		/* Check for one of the special errors */
		if (*TVdbe)(unsafe.Pointer(p)).Frc != 0 {
			mrc = (*TVdbe)(unsafe.Pointer(p)).Frc & int32(0xff)
			isSpecialError = libc.BoolInt32(mrc == int32(SQLITE_NOMEM) || mrc == int32(SQLITE_IOERR) || mrc == int32(SQLITE_INTERRUPT) || mrc == int32(SQLITE_FULL))
		} else {
			v1 = libc.Int32FromInt32(0)
			isSpecialError = v1
			mrc = v1
		}
		if isSpecialError != 0 {
			/* If the query was read-only and the error code is SQLITE_INTERRUPT,
			 ** no rollback is necessary. Otherwise, at least a savepoint
			 ** transaction must be rolled back to restore the database to a
			 ** consistent state.
			 **
			 ** Even if the statement is read-only, it is important to perform
			 ** a statement or transaction rollback operation. If the error
			 ** occurred while writing to the journal, sub-journal or database
			 ** file as part of an effort to free up cache space (see function
			 ** pagerStress() in pager.c), the rollback is required to restore
			 ** the pager to a consistent state.
			 */
			if !(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) != 0) || mrc != int32(SQLITE_INTERRUPT) {
				if (mrc == int32(SQLITE_NOMEM) || mrc == int32(SQLITE_FULL)) && int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x20>>5)) != 0 {
					eStatementOp = int32(SAVEPOINT_ROLLBACK)
				} else {
					/* We are forced to roll back the active transaction. Before doing
					 ** so, abort any other statements this handle currently has active.
					 */
					_sqlite3RollbackAll(tls, db, libc.Int32FromInt32(SQLITE_ABORT)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8))
					_sqlite3CloseSavepoints(tls, db)
					(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1)
					(*TVdbe)(unsafe.Pointer(p)).FnChange = 0
				}
			}
		}
		/* Check for immediate foreign key violations. */
		if (*TVdbe)(unsafe.Pointer(p)).Frc == SQLITE_OK || int32((*TVdbe)(unsafe.Pointer(p)).FerrorAction) == int32(OE_Fail) && !(isSpecialError != 0) {
			_sqlite3VdbeCheckFkImmediate(tls, p)
		}
		/* If the auto-commit flag is set and this is the only active writer
		 ** VM, then we do either a commit or rollback of the current transaction.
		 **
		 ** Note: This block also runs if one of the special errors handled
		 ** above has occurred.
		 */
		if !((*Tsqlite3)(unsafe.Pointer(db)).FnVTrans > 0 && (*Tsqlite3)(unsafe.Pointer(db)).FaVTrans == uintptr(0)) && (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite == libc.BoolInt32(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) == 0) {
			if (*TVdbe)(unsafe.Pointer(p)).Frc == SQLITE_OK || int32((*TVdbe)(unsafe.Pointer(p)).FerrorAction) == int32(OE_Fail) && !(isSpecialError != 0) {
				rc = _sqlite3VdbeCheckFkDeferred(tls, p)
				if rc != SQLITE_OK {
					if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) != 0 {
						_sqlite3VdbeLeave(tls, p)
						return int32(SQLITE_ERROR)
					}
					rc = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(3)<<libc.Int32FromInt32(8)
				} else {
					if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00002))<<libc.Int32FromInt32(32)) != 0 {
						rc = int32(SQLITE_CORRUPT)
						**(**Tu64)(__ccgo_up(db + 32)) &= ^(uint64(libc.Int32FromInt32(0x00002)) << libc.Int32FromInt32(32))
					} else {
						/* The auto-commit flag is true, the vdbe program was successful
						 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
						 ** key constraints to hold up the transaction. This means a commit
						 ** is required. */
						rc = _vdbeCommit(tls, db, p)
					}
				}
				if rc == int32(SQLITE_BUSY) && int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) != 0 {
					_sqlite3VdbeLeave(tls, p)
					return int32(SQLITE_BUSY)
				} else {
					if rc != SQLITE_OK {
						_sqlite3SystemError(tls, db, rc)
						(*TVdbe)(unsafe.Pointer(p)).Frc = rc
						_sqlite3RollbackAll(tls, db, SQLITE_OK)
						(*TVdbe)(unsafe.Pointer(p)).FnChange = 0
					} else {
						(*Tsqlite3)(unsafe.Pointer(db)).FnDeferredCons = 0
						(*Tsqlite3)(unsafe.Pointer(db)).FnDeferredImmCons = 0
						**(**Tu64)(__ccgo_up(db + 32)) &= ^libc.Uint64FromInt32(SQLITE_DeferFKs)
						_sqlite3CommitInternalChanges(tls, db)
					}
				}
			} else {
				if (*TVdbe)(unsafe.Pointer(p)).Frc == int32(SQLITE_SCHEMA) && (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive > int32(1) {
					(*TVdbe)(unsafe.Pointer(p)).FnChange = 0
				} else {
					_sqlite3RollbackAll(tls, db, SQLITE_OK)
					(*TVdbe)(unsafe.Pointer(p)).FnChange = 0
				}
			}
			(*Tsqlite3)(unsafe.Pointer(db)).FnStatement = 0
		} else {
			if eStatementOp == 0 {
				if (*TVdbe)(unsafe.Pointer(p)).Frc == SQLITE_OK || int32((*TVdbe)(unsafe.Pointer(p)).FerrorAction) == int32(OE_Fail) {
					eStatementOp = int32(SAVEPOINT_RELEASE)
				} else {
					if int32((*TVdbe)(unsafe.Pointer(p)).FerrorAction) == int32(OE_Abort) {
						eStatementOp = int32(SAVEPOINT_ROLLBACK)
					} else {
						_sqlite3RollbackAll(tls, db, libc.Int32FromInt32(SQLITE_ABORT)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8))
						_sqlite3CloseSavepoints(tls, db)
						(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1)
						(*TVdbe)(unsafe.Pointer(p)).FnChange = 0
					}
				}
			}
		}
		/* If eStatementOp is non-zero, then a statement transaction needs to
		 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
		 ** do so. If this operation returns an error, and the current statement
		 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
		 ** current statement error code.
		 */
		if eStatementOp != 0 {
			rc = _sqlite3VdbeCloseStatement(tls, p, eStatementOp)
			if rc != 0 {
				if (*TVdbe)(unsafe.Pointer(p)).Frc == SQLITE_OK || (*TVdbe)(unsafe.Pointer(p)).Frc&int32(0xff) == int32(SQLITE_CONSTRAINT) {
					(*TVdbe)(unsafe.Pointer(p)).Frc = rc
					_sqlite3DbFree(tls, db, (*TVdbe)(unsafe.Pointer(p)).FzErrMsg)
					(*TVdbe)(unsafe.Pointer(p)).FzErrMsg = uintptr(0)
				}
				_sqlite3RollbackAll(tls, db, libc.Int32FromInt32(SQLITE_ABORT)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8))
				_sqlite3CloseSavepoints(tls, db)
				(*Tsqlite3)(unsafe.Pointer(db)).FautoCommit = uint8(1)
				(*TVdbe)(unsafe.Pointer(p)).FnChange = 0
			}
		}
		/* If this was an INSERT, UPDATE or DELETE and no statement transaction
		 ** has been rolled back, update the database connection change-counter.
		 */
		if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x10>>4)) != 0 {
			if eStatementOp != int32(SAVEPOINT_ROLLBACK) {
				_sqlite3VdbeSetChanges(tls, db, (*TVdbe)(unsafe.Pointer(p)).FnChange)
			} else {
				_sqlite3VdbeSetChanges(tls, db, 0)
			}
			(*TVdbe)(unsafe.Pointer(p)).FnChange = 0
		}
		/* Release the locks */
		_sqlite3VdbeLeave(tls, p)
	}
	/* We have successfully halted and closed the VM.  Record this fact. */
	(*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive = (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive - 1
	if !(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x40>>6)) != 0) {
		(*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite = (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite - 1
	}
	if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0x80>>7)) != 0 {
		(*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead = (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead - 1
	}
	(*TVdbe)(unsafe.Pointer(p)).FeVdbeState = uint8(VDBE_HALT_STATE)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		(*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
	}
	/* If the auto-commit flag is set to true, then any locks that were held
	 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
	 ** to invoke any required unlock-notify callbacks.
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0 {
		_sqlite3ConnectionUnlocked(tls, db)
	}
	if (*TVdbe)(unsafe.Pointer(p)).Frc == int32(SQLITE_BUSY) {
		v1 = int32(SQLITE_BUSY)
	} else {
		v1 = SQLITE_OK
	}
	return v1
}

// C documentation
//
//	/*
//	** Compare the key of the index entry that cursor pC is pointing to against
//	** the key string in pUnpacked.  Write into *pRes a number
//	** that is negative, zero, or positive if pC is less than, equal to,
//	** or greater than pUnpacked.  Return SQLITE_OK on success.
//	**
//	** pUnpacked is either created without a rowid or is truncated so that it
//	** omits the rowid at the end.  The rowid at the end of the index entry
//	** is ignored as well.  Hence, this routine only compares the prefixes
//	** of the keys prior to the final rowid, not the entire key.
//	*/
func _sqlite3VdbeIdxKeyCompare(tls *libc.TLS, db uintptr, pC uintptr, pUnpacked uintptr, res uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var nCellKey Ti64
	var pCur uintptr
	var rc int32
	var _ /* m at bp+0 */ TMem
	_, _, _ = nCellKey, pCur, rc
	nCellKey = 0
	pCur = *(*uintptr)(unsafe.Pointer(pC + 36))
	nCellKey = int64(_sqlite3BtreePayloadSize(tls, pCur))
	/* nCellKey will always be between 0 and 0xffffffff because of the way
	 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
	if nCellKey <= 0 || nCellKey > int64(0x7fffffff) {
		**(**int32)(__ccgo_up(res)) = 0
		return _sqlite3CorruptError(tls, int32(93164))
	}
	_sqlite3VdbeMemInit(tls, bp, db, uint16(0))
	rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, pCur, uint32(nCellKey), bp)
	if rc != 0 {
		return rc
	}
	**(**int32)(__ccgo_up(res)) = _sqlite3VdbeRecordCompareWithSkip(tls, (**(**TMem)(__ccgo_up(bp))).Fn, (**(**TMem)(__ccgo_up(bp))).Fz, pUnpacked, 0)
	_sqlite3VdbeMemReleaseMalloc(tls, bp)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** pCur points at an index entry created using the OP_MakeRecord opcode.
//	** Read the rowid (the last field in the record) and store it in *rowid.
//	** Return SQLITE_OK if everything works, or an error code otherwise.
//	**
//	** pCur might be pointing to text obtained from a corrupt database file.
//	** So the content cannot be trusted.  Do appropriate checks on the content.
//	*/
func _sqlite3VdbeIdxRowid(tls *libc.TLS, db uintptr, pCur uintptr, rowid uintptr) (r int32) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var lenRowid Tu32
	var nCellKey Ti64
	var rc int32
	var _ /* m at bp+8 */ TMem
	var _ /* szHdr at bp+0 */ Tu32
	var _ /* typeRowid at bp+4 */ Tu32
	var _ /* v at bp+48 */ TMem
	_, _, _ = lenRowid, nCellKey, rc
	nCellKey = 0
	/* Get the size of the index entry.  Only indices entries of less
	 ** than 2GiB are support - anything large must be database corruption.
	 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
	 ** this code can safely assume that nCellKey is 32-bits
	 */
	nCellKey = int64(_sqlite3BtreePayloadSize(tls, pCur))
	/* Read in the complete content of the index entry */
	_sqlite3VdbeMemInit(tls, bp+8, db, uint16(0))
	rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, pCur, uint32(nCellKey), bp+8)
	if rc != 0 {
		return rc
	}
	/* The index entry must begin with a header size */
	**(**Tu32)(__ccgo_up(bp)) = uint32(**(**Tu8)(__ccgo_up((**(**TMem)(__ccgo_up(bp + 8))).Fz)))
	if **(**Tu32)(__ccgo_up(bp)) >= uint32(0x80) {
		_sqlite3GetVarint32(tls, (**(**TMem)(__ccgo_up(bp + 8))).Fz, bp)
	}
	if **(**Tu32)(__ccgo_up(bp)) < uint32(3) || **(**Tu32)(__ccgo_up(bp)) > uint32((**(**TMem)(__ccgo_up(bp + 8))).Fn) {
		goto idx_rowid_corruption
	}
	/* The last field of the index should be an integer - the ROWID.
	 ** Verify that the last entry really is an integer. */
	**(**Tu32)(__ccgo_up(bp + 4)) = uint32(**(**Tu8)(__ccgo_up((**(**TMem)(__ccgo_up(bp + 8))).Fz + uintptr(**(**Tu32)(__ccgo_up(bp))-uint32(1)))))
	if **(**Tu32)(__ccgo_up(bp + 4)) >= uint32(0x80) {
		_sqlite3GetVarint32(tls, (**(**TMem)(__ccgo_up(bp + 8))).Fz+uintptr(**(**Tu32)(__ccgo_up(bp))-uint32(1)), bp+4)
	}
	if **(**Tu32)(__ccgo_up(bp + 4)) < uint32(1) || **(**Tu32)(__ccgo_up(bp + 4)) > uint32(9) || **(**Tu32)(__ccgo_up(bp + 4)) == uint32(7) {
		goto idx_rowid_corruption
	}
	lenRowid = uint32(_sqlite3SmallTypeSizes[**(**Tu32)(__ccgo_up(bp + 4))])
	if uint32((**(**TMem)(__ccgo_up(bp + 8))).Fn) < **(**Tu32)(__ccgo_up(bp))+lenRowid {
		goto idx_rowid_corruption
	}
	/* Fetch the integer off the end of the index record */
	_sqlite3VdbeSerialGet(tls, (**(**TMem)(__ccgo_up(bp + 8))).Fz+uintptr(uint32((**(**TMem)(__ccgo_up(bp + 8))).Fn)-lenRowid), **(**Tu32)(__ccgo_up(bp + 4)), bp+48)
	**(**Ti64)(__ccgo_up(rowid)) = *(*Ti64)(unsafe.Pointer(bp + 48))
	_sqlite3VdbeMemReleaseMalloc(tls, bp+8)
	return SQLITE_OK
	/* Jump here if database corruption is detected after m has been
	 ** allocated.  Free the m object and return SQLITE_CORRUPT. */
	goto idx_rowid_corruption
idx_rowid_corruption:
	;
	_sqlite3VdbeMemReleaseMalloc(tls, bp+8)
	return _sqlite3CorruptError(tls, int32(93131))
}

// C documentation
//
//	/*
//	** Give a listing of the program in the virtual machine.
//	**
//	** The interface is the same as sqlite3VdbeExec().  But instead of
//	** running the code, it invokes the callback once for each instruction.
//	** This feature is used to implement "EXPLAIN".
//	**
//	** When p->explain==1, each instruction is listed.  When
//	** p->explain==2, only OP_Explain instructions are listed and these
//	** are shown in a different format.  p->explain==2 is used to implement
//	** EXPLAIN QUERY PLAN.
//	** 2018-04-24:  In p->explain==2 mode, the OP_Init opcodes of triggers
//	** are also shown, so that the boundaries between the main program and
//	** each trigger are clear.
//	**
//	** When p->explain==1, first the main program is listed, then each of
//	** the trigger subprograms are listed one by one.
//	*/
func _sqlite3VdbeList(tls *libc.TLS, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bListSubprogs, rc int32
	var db, pMem, pOp, pSub, zP4 uintptr
	var _ /* aOp at bp+4 */ uintptr
	var _ /* i at bp+0 */ int32
	_, _, _, _, _, _, _ = bListSubprogs, db, pMem, pOp, pSub, rc, zP4
	pSub = uintptr(0)                                                                                                                                                                    /* Memory cell hold array of subprogs */
	db = (*TVdbe)(unsafe.Pointer(p)).Fdb                                                                                                                                                 /* Loop counter */
	rc = SQLITE_OK                                                                                                                                                                       /* Return code */
	pMem = (*TVdbe)(unsafe.Pointer(p)).FaMem + 1*40                                                                                                                                      /* First Mem of result set */
	bListSubprogs = libc.BoolInt32(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) == int32(1) || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_TriggerEQP) != uint64(0)) /* Current opcode */
	/* Even though this opcode does not use dynamic strings for
	 ** the result, result columns may become dynamic if the user calls
	 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
	 */
	_releaseMemArray(tls, pMem, int32(8))
	if (*TVdbe)(unsafe.Pointer(p)).Frc == int32(SQLITE_NOMEM) {
		/* This happens if a malloc() inside a call to sqlite3_column_text() or
		 ** sqlite3_column_text16() failed.  */
		_sqlite3OomFault(tls, db)
		return int32(SQLITE_ERROR)
	}
	if bListSubprogs != 0 {
		/* The first 8 memory cells are used for the result set.  So we will
		 ** commandeer the 9th cell to use as storage for an array of pointers
		 ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
		 ** cells.  */
		pSub = (*TVdbe)(unsafe.Pointer(p)).FaMem + 9*40
	} else {
		pSub = uintptr(0)
	}
	/* Figure out which opcode is next to display */
	rc = _sqlite3VdbeNextOpcode(tls, p, pSub, libc.BoolInt32(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) == int32(2)), p+32, bp, bp+4)
	if rc == SQLITE_OK {
		pOp = **(**uintptr)(__ccgo_up(bp + 4)) + uintptr(**(**int32)(__ccgo_up(bp)))*20
		if libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0 {
			(*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_INTERRUPT)
			rc = int32(SQLITE_ERROR)
			_sqlite3VdbeError(tls, p, _sqlite3ErrStr(tls, (*TVdbe)(unsafe.Pointer(p)).Frc), 0)
		} else {
			zP4 = _sqlite3VdbeDisplayP4(tls, db, pOp)
			if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)) == int32(2) {
				_sqlite3VdbeMemSetInt64(tls, pMem, int64((*TOp)(unsafe.Pointer(pOp)).Fp1))
				_sqlite3VdbeMemSetInt64(tls, pMem+uintptr(1)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp2))
				_sqlite3VdbeMemSetInt64(tls, pMem+uintptr(2)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp3))
				_sqlite3VdbeMemSetStr(tls, pMem+uintptr(3)*40, zP4, int64(-int32(1)), uint8(SQLITE_UTF8), __ccgo_fp(Xsqlite3_free))
			} else {
				_sqlite3VdbeMemSetInt64(tls, pMem+uintptr(0)*40, int64(**(**int32)(__ccgo_up(bp))))
				_sqlite3VdbeMemSetStr(tls, pMem+uintptr(1)*40, _sqlite3OpcodeName(tls, int32((*TOp)(unsafe.Pointer(pOp)).Fopcode)), int64(-int32(1)), uint8(SQLITE_UTF8), libc.UintptrFromInt32(0))
				_sqlite3VdbeMemSetInt64(tls, pMem+uintptr(2)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp1))
				_sqlite3VdbeMemSetInt64(tls, pMem+uintptr(3)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp2))
				_sqlite3VdbeMemSetInt64(tls, pMem+uintptr(4)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp3))
				/* pMem+5 for p4 is done last */
				_sqlite3VdbeMemSetInt64(tls, pMem+uintptr(6)*40, int64((*TOp)(unsafe.Pointer(pOp)).Fp5))
				_sqlite3VdbeMemSetNull(tls, pMem+uintptr(7)*40)
				_sqlite3VdbeMemSetStr(tls, pMem+uintptr(5)*40, zP4, int64(-int32(1)), uint8(SQLITE_UTF8), __ccgo_fp(Xsqlite3_free))
			}
			(*TVdbe)(unsafe.Pointer(p)).FpResultRow = pMem
			if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				(*TVdbe)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
				rc = int32(SQLITE_ERROR)
			} else {
				(*TVdbe)(unsafe.Pointer(p)).Frc = SQLITE_OK
				rc = int32(SQLITE_ROW)
			}
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Send a "statement aborts" message to the error log.
//	*/
func _sqlite3VdbeLogAbort(tls *libc.TLS, p uintptr, rc int32, pOp uintptr, aOp uintptr) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var pc int32
	var zPrefix, zSql uintptr
	var _ /* zXtra at bp+0 */ [100]int8
	_, _, _ = pc, zPrefix, zSql
	zSql = (*TVdbe)(unsafe.Pointer(p)).FzSql /* Original SQL text */
	zPrefix = __ccgo_ts + 1711               /* Buffer space to store zPrefix */
	if (*TVdbe)(unsafe.Pointer(p)).FpFrame != 0 {
		if *(*uintptr)(unsafe.Pointer(aOp + 16)) != uintptr(0) {
			Xsqlite3_snprintf(tls, int32(100), bp, __ccgo_ts+6971, libc.VaList(bp+112, *(*uintptr)(unsafe.Pointer(aOp + 16))+uintptr(3)))
			zPrefix = bp
		} else {
			zPrefix = __ccgo_ts + 6981
		}
	}
	pc = (int32(pOp) - int32(aOp)) / 20
	Xsqlite3_log(tls, rc, __ccgo_ts+7004, libc.VaList(bp+112, pc, (*TVdbe)(unsafe.Pointer(p)).FzErrMsg, zPrefix, zSql))
}

// C documentation
//
//	/*
//	** Prepare a virtual machine for execution for the first time after
//	** creating the virtual machine.  This involves things such
//	** as allocating registers and initializing the program counter.
//	** After the VDBE has be prepped, it can be executed by one or more
//	** calls to sqlite3VdbeExec().
//	**
//	** This function may be called exactly once on each virtual machine.
//	** After this routine is called the VM has been "packaged" and is ready
//	** to run.  After this routine is called, further calls to
//	** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
//	** the Vdbe from the Parse object that helped generate it so that the
//	** the Vdbe becomes an independent entity and the Parse object can be
//	** destroyed.
//	**
//	** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
//	** to its initial state after it has been run.
//	*/
func _sqlite3VdbeMakeReady(tls *libc.TLS, p uintptr, pParse uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, v1 uintptr
	var n, nCursor, nMem, nVar int32
	var _ /* nArg at bp+0 */ int32
	var _ /* x at bp+8 */ TReusableSpace
	_, _, _, _, _, _ = db, n, nCursor, nMem, nVar, v1 /* Reusable bulk memory */
	(*TVdbe)(unsafe.Pointer(p)).FpVList = (*TParse)(unsafe.Pointer(pParse)).FpVList
	(*TParse)(unsafe.Pointer(pParse)).FpVList = uintptr(0)
	db = (*TVdbe)(unsafe.Pointer(p)).Fdb
	nVar = int32((*TParse)(unsafe.Pointer(pParse)).FnVar)
	nMem = (*TParse)(unsafe.Pointer(pParse)).FnMem
	nCursor = (*TParse)(unsafe.Pointer(pParse)).FnTab
	**(**int32)(__ccgo_up(bp)) = (*TParse)(unsafe.Pointer(pParse)).FnMaxArg
	/* Each cursor uses a memory cell.  The first cursor (cursor 0) can
	 ** use aMem[0] which is not otherwise used by the VDBE program.  Allocate
	 ** space at the end of aMem[] for cursors 1 and greater.
	 ** See also: allocateCursor().
	 */
	nMem = nMem + nCursor
	if nCursor == 0 && nMem > 0 {
		nMem = nMem + 1
	} /* Space for aMem[0] even if not used */
	/* Figure out how much reusable memory is available at the end of the
	 ** opcode array.  This extra memory will be reallocated for other elements
	 ** of the prepared statement.
	 */
	n = int32((libc.Uint32FromInt64(20)*uint32((*TVdbe)(unsafe.Pointer(p)).FnOp) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7))) /* Bytes of opcode memory used */
	(**(**TReusableSpace)(__ccgo_up(bp + 8))).FpSpace = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(n)                                          /* Unused opcode memory */
	(**(**TReusableSpace)(__ccgo_up(bp + 8))).FnFree = int64(((*TParse)(unsafe.Pointer(pParse)).FszOpAlloc - n) & ^libc.Int32FromInt32(7))     /* Bytes of unused memory */
	_resolveP2Values(tls, p, bp)
	libc.SetBitFieldPtr16Uint32(p+152, uint32(libc.BoolUint8((*TParse)(unsafe.Pointer(pParse)).FisMultiWrite != 0 && int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 28))&0x2>>1)) != 0)), 5, 0x20)
	if (*TParse)(unsafe.Pointer(pParse)).Fexplain != 0 {
		if nMem < int32(10) {
			nMem = int32(10)
		}
		libc.SetBitFieldPtr16Uint32(p+152, uint32((*TParse)(unsafe.Pointer(pParse)).Fexplain), 2, 0xc)
		(*TVdbe)(unsafe.Pointer(p)).FnResColumn = uint16(int32(12) - int32(4)*int32(Tbft(*(*uint16)(unsafe.Pointer(p + 152))&0xc>>2)))
	}
	libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(0), 0, 0x3)
	/* Memory for registers, parameters, cursor, etc, is allocated in one or two
	 ** passes.  On the first pass, we try to reuse unused memory at the
	 ** end of the opcode array.  If we are unable to satisfy all memory
	 ** requirements by reusing the opcode array tail, then the second
	 ** pass will fill in the remainder using a fresh memory allocation.
	 **
	 ** This two-pass approach that reuses as much memory as possible from
	 ** the leftover memory at the end of the opcode array.  This can significantly
	 ** reduce the amount of memory held by a prepared statement.
	 */
	(**(**TReusableSpace)(__ccgo_up(bp + 8))).FnNeeded = 0
	(*TVdbe)(unsafe.Pointer(p)).FaMem = _allocSpace(tls, bp+8, uintptr(0), int64(uint32(nMem)*uint32(40)))
	(*TVdbe)(unsafe.Pointer(p)).FaVar = _allocSpace(tls, bp+8, uintptr(0), int64(uint32(nVar)*uint32(40)))
	(*TVdbe)(unsafe.Pointer(p)).FapArg = _allocSpace(tls, bp+8, uintptr(0), int64(uint32(**(**int32)(__ccgo_up(bp)))*uint32(4)))
	(*TVdbe)(unsafe.Pointer(p)).FapCsr = _allocSpace(tls, bp+8, uintptr(0), int64(uint32(nCursor)*uint32(4)))
	if (**(**TReusableSpace)(__ccgo_up(bp + 8))).FnNeeded != 0 {
		v1 = _sqlite3DbMallocRawNN(tls, db, uint64((**(**TReusableSpace)(__ccgo_up(bp + 8))).FnNeeded))
		(*TVdbe)(unsafe.Pointer(p)).FpFree = v1
		(**(**TReusableSpace)(__ccgo_up(bp + 8))).FpSpace = v1
		(**(**TReusableSpace)(__ccgo_up(bp + 8))).FnFree = (**(**TReusableSpace)(__ccgo_up(bp + 8))).FnNeeded
		if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
			(*TVdbe)(unsafe.Pointer(p)).FaMem = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FaMem, int64(uint32(nMem)*uint32(40)))
			(*TVdbe)(unsafe.Pointer(p)).FaVar = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FaVar, int64(uint32(nVar)*uint32(40)))
			(*TVdbe)(unsafe.Pointer(p)).FapArg = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FapArg, int64(uint32(**(**int32)(__ccgo_up(bp)))*uint32(4)))
			(*TVdbe)(unsafe.Pointer(p)).FapCsr = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FapCsr, int64(uint32(nCursor)*uint32(4)))
		}
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		(*TVdbe)(unsafe.Pointer(p)).FnVar = 0
		(*TVdbe)(unsafe.Pointer(p)).FnCursor = 0
		(*TVdbe)(unsafe.Pointer(p)).FnMem = 0
	} else {
		(*TVdbe)(unsafe.Pointer(p)).FnCursor = nCursor
		(*TVdbe)(unsafe.Pointer(p)).FnVar = int16(nVar)
		_initMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaVar, nVar, db, uint16(MEM_Null))
		(*TVdbe)(unsafe.Pointer(p)).FnMem = nMem
		_initMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaMem, nMem, db, uint16(MEM_Undefined))
		libc.Xmemset(tls, (*TVdbe)(unsafe.Pointer(p)).FapCsr, 0, uint32(nCursor)*uint32(4))
	}
	_sqlite3VdbeRewind(tls, p)
}

// C documentation
//
//	/*
//	** Cast the datatype of the value in pMem according to the affinity
//	** "aff".  Casting is different from applying affinity in that a cast
//	** is forced.  In other words, the value is converted into the desired
//	** affinity even if that results in loss of data.  This routine is
//	** used (for example) to implement the SQL "cast()" operator.
//	*/
func _sqlite3VdbeMemCast(tls *libc.TLS, pMem uintptr, aff Tu8, encoding Tu8) (r int32) {
	var rc int32
	var v1 uintptr
	_, _ = rc, v1
	if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Null) != 0 {
		return SQLITE_OK
	}
	switch int32(aff) {
	case int32(SQLITE_AFF_BLOB): /* Really a cast to BLOB */
		if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Blob) == 0 {
			_sqlite3ValueApplyAffinity(tls, pMem, uint8(SQLITE_AFF_TEXT), encoding)
			if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Str) != 0 {
				(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pMem)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Blob))
			}
		} else {
			v1 = pMem + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(MEM_TypeMask) & ^libc.Int32FromInt32(MEM_Blob)))
		}
	case int32(SQLITE_AFF_NUMERIC):
		_sqlite3VdbeMemNumerify(tls, pMem)
	case int32(SQLITE_AFF_INTEGER):
		_sqlite3VdbeMemIntegerify(tls, pMem)
	case int32(SQLITE_AFF_REAL):
		_sqlite3VdbeMemRealify(tls, pMem)
	default:
		v1 = pMem + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&libc.Int32FromInt32(MEM_Blob)>>libc.Int32FromInt32(3))
		_sqlite3ValueApplyAffinity(tls, pMem, uint8(SQLITE_AFF_TEXT), encoding)
		v1 = pMem + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal) | libc.Int32FromInt32(MEM_Blob) | libc.Int32FromInt32(MEM_Zero)))
		if int32(encoding) != int32(SQLITE_UTF8) {
			**(**int32)(__ccgo_up(pMem + 12)) &= ^libc.Int32FromInt32(1)
		}
		rc = _sqlite3VdbeChangeEncoding(tls, pMem, int32(encoding))
		if rc != 0 {
			return rc
		}
		_sqlite3VdbeMemZeroTerminateIfAble(tls, pMem)
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Make a full copy of pFrom into pTo.  Prior contents of pTo are
//	** freed before the copy is made.
//	*/
func _sqlite3VdbeMemCopy(tls *libc.TLS, pTo uintptr, pFrom uintptr) (r int32) {
	var rc int32
	var v1 uintptr
	_, _ = rc, v1
	rc = SQLITE_OK
	if int32((*TMem)(unsafe.Pointer(pTo)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 {
		_vdbeMemClearExternAndSetNull(tls, pTo)
	}
	libc.Xmemcpy(tls, pTo, pFrom, uint32(libc.UintptrFromInt32(0)+20))
	v1 = pTo + 16
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(MEM_Dyn))
	if int32((*TMem)(unsafe.Pointer(pTo)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 {
		if 0 == int32((*TMem)(unsafe.Pointer(pFrom)).Fflags)&int32(MEM_Static) {
			v1 = pTo + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Ephem))
			rc = _sqlite3VdbeMemMakeWriteable(tls, pTo)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** If the given Mem* has a zero-filled tail, turn it into an ordinary
//	** blob stored in dynamically allocated space.
//	*/
func _sqlite3VdbeMemExpandBlob(tls *libc.TLS, pMem uintptr) (r int32) {
	var nByte int32
	var v1 uintptr
	_, _ = nByte, v1
	/* Set nByte to the number of bytes required to store the expanded blob. */
	nByte = (*TMem)(unsafe.Pointer(pMem)).Fn + *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu))
	if nByte <= 0 {
		if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Blob) == 0 {
			return SQLITE_OK
		}
		nByte = int32(1)
	}
	if _sqlite3VdbeMemGrow(tls, pMem, nByte, int32(1)) != 0 {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, (*TMem)(unsafe.Pointer(pMem)).Fz+uintptr((*TMem)(unsafe.Pointer(pMem)).Fn), 0, uint32(*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu))))
	**(**int32)(__ccgo_up(pMem + 12)) += *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu))
	v1 = pMem + 16
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(MEM_Zero) | libc.Int32FromInt32(MEM_Term)))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Make sure pMem->z points to a writable allocation of at least n bytes.
//	**
//	** If the bPreserve argument is true, then copy of the content of
//	** pMem->z into the new allocation.  pMem must be either a string or
//	** blob if bPreserve is true.  If bPreserve is false, any prior content
//	** in pMem->z is discarded.
//	*/
func _sqlite3VdbeMemGrow(tls *libc.TLS, pMem uintptr, n int32, bPreserve int32) (r int32) {
	var v1 uintptr
	_ = v1
	/* If the bPreserve flag is set to true, then the memory cell must already
	 ** contain a valid string or blob value.  */
	if (*TMem)(unsafe.Pointer(pMem)).FszMalloc > 0 && bPreserve != 0 && (*TMem)(unsafe.Pointer(pMem)).Fz == (*TMem)(unsafe.Pointer(pMem)).FzMalloc {
		if (*TMem)(unsafe.Pointer(pMem)).Fdb != 0 {
			v1 = _sqlite3DbReallocOrFree(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).Fz, uint64(n))
			(*TMem)(unsafe.Pointer(pMem)).FzMalloc = v1
			(*TMem)(unsafe.Pointer(pMem)).Fz = v1
		} else {
			(*TMem)(unsafe.Pointer(pMem)).FzMalloc = _sqlite3Realloc(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, uint64(n))
			if (*TMem)(unsafe.Pointer(pMem)).FzMalloc == uintptr(0) {
				Xsqlite3_free(tls, (*TMem)(unsafe.Pointer(pMem)).Fz)
			}
			(*TMem)(unsafe.Pointer(pMem)).Fz = (*TMem)(unsafe.Pointer(pMem)).FzMalloc
		}
		bPreserve = 0
	} else {
		if (*TMem)(unsafe.Pointer(pMem)).FszMalloc > 0 {
			_sqlite3DbFreeNN(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc)
		}
		(*TMem)(unsafe.Pointer(pMem)).FzMalloc = _sqlite3DbMallocRaw(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, uint64(n))
	}
	if (*TMem)(unsafe.Pointer(pMem)).FzMalloc == uintptr(0) {
		_sqlite3VdbeMemSetNull(tls, pMem)
		(*TMem)(unsafe.Pointer(pMem)).Fz = uintptr(0)
		(*TMem)(unsafe.Pointer(pMem)).FszMalloc = 0
		return int32(SQLITE_NOMEM)
	} else {
		(*TMem)(unsafe.Pointer(pMem)).FszMalloc = _sqlite3DbMallocSize(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc)
	}
	if bPreserve != 0 && (*TMem)(unsafe.Pointer(pMem)).Fz != 0 {
		libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pMem)).FzMalloc, (*TMem)(unsafe.Pointer(pMem)).Fz, uint32((*TMem)(unsafe.Pointer(pMem)).Fn))
	}
	if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Dyn) != 0 {
		(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMem)(unsafe.Pointer(pMem)).FxDel})))(tls, (*TMem)(unsafe.Pointer(pMem)).Fz)
	}
	(*TMem)(unsafe.Pointer(pMem)).Fz = (*TMem)(unsafe.Pointer(pMem)).FzMalloc
	v1 = pMem + 16
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Ephem) | libc.Int32FromInt32(MEM_Static)))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This routine checks for a byte-order mark at the beginning of the
//	** UTF-16 string stored in *pMem. If one is present, it is removed and
//	** the encoding of the Mem adjusted. This routine does not do any
//	** byte-swapping, it just sets Mem.enc appropriately.
//	**
//	** The allocation (static, dynamic etc.) and encoding of the Mem may be
//	** changed by this function.
//	*/
func _sqlite3VdbeMemHandleBom(tls *libc.TLS, pMem uintptr) (r int32) {
	var b1, b2, bom Tu8
	var rc int32
	var v1 uintptr
	_, _, _, _, _ = b1, b2, bom, rc, v1
	rc = SQLITE_OK
	bom = uint8(0)
	if (*TMem)(unsafe.Pointer(pMem)).Fn > int32(1) {
		b1 = **(**Tu8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz))
		b2 = **(**Tu8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + libc.UintptrFromInt32(1)))
		if int32(b1) == int32(0xFE) && int32(b2) == int32(0xFF) {
			bom = uint8(SQLITE_UTF16BE)
		}
		if int32(b1) == int32(0xFF) && int32(b2) == int32(0xFE) {
			bom = uint8(SQLITE_UTF16LE)
		}
	}
	if bom != 0 {
		rc = _sqlite3VdbeMemMakeWriteable(tls, pMem)
		if rc == SQLITE_OK {
			**(**int32)(__ccgo_up(pMem + 12)) -= int32(2)
			libc.Xmemmove(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, (*TMem)(unsafe.Pointer(pMem)).Fz+2, uint32((*TMem)(unsafe.Pointer(pMem)).Fn))
			**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = int8('\000')
			**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(1)))) = int8('\000')
			v1 = pMem + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
			(*TMem)(unsafe.Pointer(pMem)).Fenc = bom
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Change pMem so that its MEM_Str or MEM_Blob value is stored in
//	** MEM.zMalloc, where it can be safely written.
//	**
//	** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
//	*/
func _sqlite3VdbeMemMakeWriteable(tls *libc.TLS, pMem uintptr) (r int32) {
	var rc, v1 int32
	var v2 uintptr
	_, _, _ = rc, v1, v2
	if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 {
		if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Zero) != 0 {
			v1 = _sqlite3VdbeMemExpandBlob(tls, pMem)
		} else {
			v1 = 0
		}
		if v1 != 0 {
			return int32(SQLITE_NOMEM)
		}
		if (*TMem)(unsafe.Pointer(pMem)).FszMalloc == 0 || (*TMem)(unsafe.Pointer(pMem)).Fz != (*TMem)(unsafe.Pointer(pMem)).FzMalloc {
			rc = _vdbeMemAddTerminator(tls, pMem)
			if rc != 0 {
				return rc
			}
		}
	}
	v2 = pMem + 16
	*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) & ^libc.Int32FromInt32(MEM_Ephem))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Convert pMem so that it has type MEM_Real or MEM_Int.
//	** Invalidate any prior representations.
//	**
//	** Every effort is made to force the conversion, even if the input
//	** is a string that does not look completely like a number.  Convert
//	** as much of the string as we can and ignore the rest.
//	*/
func _sqlite3VdbeMemNumerify(tls *libc.TLS, pMem uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var rc int32
	var v1 Tsqlite3_int64
	var v2 bool
	var v3 uintptr
	var _ /* ix at bp+0 */ Tsqlite3_int64
	_, _, _, _ = rc, v1, v2, v3
	if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_Real)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Null)) == 0 {
		rc = _sqlite3MemRealValueRC(tls, pMem, pMem)
		if v2 = rc&int32(2) == 0 && _sqlite3Atoi64(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, bp, (*TMem)(unsafe.Pointer(pMem)).Fn, (*TMem)(unsafe.Pointer(pMem)).Fenc) < int32(2); !v2 {
			v1 = _sqlite3RealToI64(tls, *(*float64)(unsafe.Pointer(pMem)))
			**(**Tsqlite3_int64)(__ccgo_up(bp)) = v1
		}
		if v2 || _sqlite3RealSameAsInt(tls, *(*float64)(unsafe.Pointer(pMem)), v1) != 0 {
			*(*Ti64)(unsafe.Pointer(pMem)) = **(**Tsqlite3_int64)(__ccgo_up(bp))
			(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pMem)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Int))
		} else {
			(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pMem)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Real))
		}
	}
	v3 = pMem + 16
	*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) & ^(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Blob) | libc.Int32FromInt32(MEM_Zero)))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Set the value stored in *pMem should already be a NULL.
//	** Also store a pointer to go with it.
//	*/
func _sqlite3VdbeMemSetPointer(tls *libc.TLS, pMem uintptr, pPtr uintptr, zPType uintptr, __ccgo_fp_xDestructor uintptr) {
	var v1 uintptr
	_ = v1
	_vdbeMemClear(tls, pMem)
	if zPType != 0 {
		v1 = zPType
	} else {
		v1 = __ccgo_ts + 1711
	}
	*(*uintptr)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu)) = v1
	(*TMem)(unsafe.Pointer(pMem)).Fz = pPtr
	(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(libc.Int32FromInt32(MEM_Null) | libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Subtype) | libc.Int32FromInt32(MEM_Term))
	(*TMem)(unsafe.Pointer(pMem)).FeSubtype = uint8('p')
	if __ccgo_fp_xDestructor != 0 {
		v1 = __ccgo_fp_xDestructor
	} else {
		v1 = __ccgo_fp(_sqlite3NoopDestructor)
	}
	(*TMem)(unsafe.Pointer(pMem)).FxDel = v1
}

// C documentation
//
//	/*
//	** Change the value of a Mem to be a string or a BLOB.
//	**
//	** The memory management strategy depends on the value of the xDel
//	** parameter. If the value passed is SQLITE_TRANSIENT, then the
//	** string is copied into a (possibly existing) buffer managed by the
//	** Mem structure. Otherwise, any existing buffer is freed and the
//	** pointer copied.
//	**
//	** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
//	** size limit) then no memory allocation occurs.  If the string can be
//	** stored without allocating memory, then it is.  If a memory allocation
//	** is required to store the string, then value of pMem is unchanged.  In
//	** either case, SQLITE_TOOBIG is returned.
//	**
//	** The "enc" parameter is the text encoding for the string, or zero
//	** to store a blob.
//	**
//	** If n is negative, then the string consists of all bytes up to but
//	** excluding the first zero character.  The n parameter must be
//	** non-negative for blobs.
//	*/
func _sqlite3VdbeMemSetStr(tls *libc.TLS, pMem uintptr, z uintptr, n Ti64, enc Tu8, __ccgo_fp_xDel uintptr) (r int32) {
	var flags Tu16
	var iLimit, v2 int32
	var nAlloc, nByte Ti64
	var v3 int64
	_, _, _, _, _, _ = flags, iLimit, nAlloc, nByte, v2, v3
	nByte = n /* New value for pMem->flags */
	/* If z is a NULL pointer, set pMem to contain an SQL NULL. */
	if !(z != 0) {
		_sqlite3VdbeMemSetNull(tls, pMem)
		return SQLITE_OK
	}
	if (*TMem)(unsafe.Pointer(pMem)).Fdb != 0 {
		iLimit = **(**int32)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fdb + 120))
	} else {
		iLimit = int32(SQLITE_MAX_LENGTH)
	}
	if nByte < 0 {
		if int32(enc) == int32(SQLITE_UTF8) {
			nByte = int64(libc.Xstrlen(tls, z))
		} else {
			nByte = 0
			for {
				if !(nByte <= int64(iLimit) && int32(**(**int8)(__ccgo_up(z + uintptr(nByte))))|int32(**(**int8)(__ccgo_up(z + uintptr(nByte+int64(1))))) != 0) {
					break
				}
				goto _1
			_1:
				;
				nByte = nByte + int64(2)
			}
		}
		flags = uint16(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Term))
	} else {
		if int32(enc) == 0 {
			flags = uint16(MEM_Blob)
			enc = uint8(SQLITE_UTF8)
		} else {
			flags = uint16(MEM_Str)
		}
	}
	if nByte > int64(iLimit) {
		if __ccgo_fp_xDel != 0 && __ccgo_fp_xDel != uintptr(-libc.Int32FromInt32(1)) {
			if __ccgo_fp_xDel == __ccgo_fp(_sqlite3RowSetClear) {
				_sqlite3DbFree(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, z)
			} else {
				(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xDel})))(tls, z)
			}
		}
		_sqlite3VdbeMemSetNull(tls, pMem)
		return _sqlite3ErrorToParser(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, int32(SQLITE_TOOBIG))
	}
	/* The following block sets the new values of Mem.z and Mem.xDel. It
	 ** also sets a flag in local variable "flags" to indicate the memory
	 ** management (one of MEM_Dyn or MEM_Static).
	 */
	if __ccgo_fp_xDel == uintptr(-libc.Int32FromInt32(1)) {
		nAlloc = nByte
		if int32(flags)&int32(MEM_Term) != 0 {
			if int32(enc) == int32(SQLITE_UTF8) {
				v2 = int32(1)
			} else {
				v2 = int32(2)
			}
			nAlloc = nAlloc + int64(v2)
		}
		if nAlloc > int64(libc.Int32FromInt32(32)) {
			v3 = nAlloc
		} else {
			v3 = int64(libc.Int32FromInt32(32))
		}
		if _sqlite3VdbeMemClearAndResize(tls, pMem, int32(v3)) != 0 {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, z, uint32(nAlloc))
	} else {
		_sqlite3VdbeMemRelease(tls, pMem)
		(*TMem)(unsafe.Pointer(pMem)).Fz = z
		if __ccgo_fp_xDel == __ccgo_fp(_sqlite3RowSetClear) {
			(*TMem)(unsafe.Pointer(pMem)).FzMalloc = (*TMem)(unsafe.Pointer(pMem)).Fz
			(*TMem)(unsafe.Pointer(pMem)).FszMalloc = _sqlite3DbMallocSize(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc)
		} else {
			(*TMem)(unsafe.Pointer(pMem)).FxDel = __ccgo_fp_xDel
			if __ccgo_fp_xDel == libc.UintptrFromInt32(0) {
				v2 = int32(MEM_Static)
			} else {
				v2 = int32(MEM_Dyn)
			}
			flags = uint16(int32(flags) | v2)
		}
	}
	(*TMem)(unsafe.Pointer(pMem)).Fn = int32(nByte & libc.Int64FromInt32(0x7fffffff))
	(*TMem)(unsafe.Pointer(pMem)).Fflags = flags
	(*TMem)(unsafe.Pointer(pMem)).Fenc = enc
	if int32(enc) > int32(SQLITE_UTF8) && _sqlite3VdbeMemHandleBom(tls, pMem) != 0 {
		return int32(SQLITE_NOMEM)
	}
	return SQLITE_OK
}

// C documentation
//
//	/* Like sqlite3VdbeMemSetStr() except:
//	**
//	**   enc is always SQLITE_UTF8
//	**   pMem->db is always non-NULL
//	*/
func _sqlite3VdbeMemSetText(tls *libc.TLS, pMem uintptr, z uintptr, n Ti64, __ccgo_fp_xDel uintptr) (r int32) {
	var flags Tu16
	var nAlloc, nByte Ti64
	var v1 int64
	_, _, _, _ = flags, nAlloc, nByte, v1
	nByte = n
	/* If z is a NULL pointer, set pMem to contain an SQL NULL. */
	if !(z != 0) {
		_sqlite3VdbeMemSetNull(tls, pMem)
		return SQLITE_OK
	}
	if nByte < 0 {
		nByte = int64(libc.Xstrlen(tls, z))
		flags = uint16(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Term))
	} else {
		flags = uint16(MEM_Str)
	}
	if nByte > int64(**(**int32)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fdb + 120))) {
		if __ccgo_fp_xDel != 0 && __ccgo_fp_xDel != uintptr(-libc.Int32FromInt32(1)) {
			if __ccgo_fp_xDel == __ccgo_fp(_sqlite3RowSetClear) {
				_sqlite3DbFree(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, z)
			} else {
				(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xDel})))(tls, z)
			}
		}
		_sqlite3VdbeMemSetNull(tls, pMem)
		return _sqlite3ErrorToParser(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, int32(SQLITE_TOOBIG))
	}
	/* The following block sets the new values of Mem.z and Mem.xDel. It
	 ** also sets a flag in local variable "flags" to indicate the memory
	 ** management (one of MEM_Dyn or MEM_Static).
	 */
	if __ccgo_fp_xDel == uintptr(-libc.Int32FromInt32(1)) {
		nAlloc = nByte + int64(1)
		if nAlloc > int64(libc.Int32FromInt32(32)) {
			v1 = nAlloc
		} else {
			v1 = int64(libc.Int32FromInt32(32))
		}
		if _sqlite3VdbeMemClearAndResize(tls, pMem, int32(v1)) != 0 {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, z, uint32(nByte))
		**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr(nByte))) = 0
	} else {
		_sqlite3VdbeMemRelease(tls, pMem)
		(*TMem)(unsafe.Pointer(pMem)).Fz = z
		if __ccgo_fp_xDel == __ccgo_fp(_sqlite3RowSetClear) {
			(*TMem)(unsafe.Pointer(pMem)).FzMalloc = (*TMem)(unsafe.Pointer(pMem)).Fz
			(*TMem)(unsafe.Pointer(pMem)).FszMalloc = _sqlite3DbMallocSize(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc)
			(*TMem)(unsafe.Pointer(pMem)).FxDel = uintptr(0)
		} else {
			if __ccgo_fp_xDel == libc.UintptrFromInt32(0) {
				(*TMem)(unsafe.Pointer(pMem)).FxDel = __ccgo_fp_xDel
				flags = uint16(int32(flags) | libc.Int32FromInt32(MEM_Static))
			} else {
				(*TMem)(unsafe.Pointer(pMem)).FxDel = __ccgo_fp_xDel
				flags = uint16(int32(flags) | libc.Int32FromInt32(MEM_Dyn))
			}
		}
	}
	(*TMem)(unsafe.Pointer(pMem)).Fflags = flags
	(*TMem)(unsafe.Pointer(pMem)).Fn = int32(nByte & libc.Int64FromInt32(0x7fffffff))
	(*TMem)(unsafe.Pointer(pMem)).Fenc = uint8(SQLITE_UTF8)
	return SQLITE_OK
}

func _sqlite3VdbeMemShallowCopy(tls *libc.TLS, pTo uintptr, pFrom uintptr, srcType int32) {
	var v1 uintptr
	_ = v1
	if int32((*TMem)(unsafe.Pointer(pTo)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 {
		_vdbeClrCopy(tls, pTo, pFrom, srcType)
		return
	}
	libc.Xmemcpy(tls, pTo, pFrom, uint32(libc.UintptrFromInt32(0)+20))
	if int32((*TMem)(unsafe.Pointer(pFrom)).Fflags)&int32(MEM_Static) == 0 {
		v1 = pTo + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Ephem)))
		v1 = pTo + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | srcType)
	}
}

// C documentation
//
//	/*
//	** Add MEM_Str to the set of representations for the given Mem.  This
//	** routine is only called if pMem is a number of some kind, not a NULL
//	** or a BLOB.
//	**
//	** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
//	** if bForce is true but are retained if bForce is false.
//	**
//	** A MEM_Null value will never be passed to this function. This function is
//	** used for converting values to text for returning to the user (i.e. via
//	** sqlite3_value_text()), or for ensuring that values to be used as btree
//	** keys are strings. In the former case a NULL pointer is returned the
//	** user and the latter is an internal programming error.
//	*/
func _sqlite3VdbeMemStringify(tls *libc.TLS, pMem uintptr, enc Tu8, bForce Tu8) (r int32) {
	var nByte int32
	var v1 uintptr
	_, _ = nByte, v1
	nByte = int32(32)
	if _sqlite3VdbeMemClearAndResize(tls, pMem, nByte) != 0 {
		(*TMem)(unsafe.Pointer(pMem)).Fenc = uint8(0)
		return int32(SQLITE_NOMEM)
	}
	_vdbeMemRenderNum(tls, nByte, (*TMem)(unsafe.Pointer(pMem)).Fz, pMem)
	(*TMem)(unsafe.Pointer(pMem)).Fenc = uint8(SQLITE_UTF8)
	v1 = pMem + 16
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | (libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Term)))
	if bForce != 0 {
		v1 = pMem + 16
		*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Real) | libc.Int32FromInt32(MEM_IntReal)))
	}
	_sqlite3VdbeChangeEncoding(tls, pMem, int32(enc))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Return true if the Mem object contains a TEXT or BLOB that is
//	** too large - whose size exceeds SQLITE_MAX_LENGTH.
//	*/
func _sqlite3VdbeMemTooBig(tls *libc.TLS, p uintptr) (r int32) {
	var n int32
	_ = n
	if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Blob)) != 0 {
		n = (*TMem)(unsafe.Pointer(p)).Fn
		if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Zero) != 0 {
			n = n + *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(p)).Fu))
		}
		return libc.BoolInt32(n > **(**int32)(__ccgo_up((*TMem)(unsafe.Pointer(p)).Fdb + 120)))
	}
	return 0
}

// C documentation
//
//	/*
//	** This routine transforms the internal text encoding used by pMem to
//	** desiredEnc. It is an error if the string is already of the desired
//	** encoding, or if *pMem does not contain a string value.
//	*/
func _sqlite3VdbeMemTranslate(tls *libc.TLS, pMem uintptr, desiredEnc Tu8) (r int32) {
	var c uint32
	var c2, c21, rc int32
	var len1 Tsqlite3_int64
	var temp Tu8
	var z, zIn, zOut, zTerm, v1, v2 uintptr
	_, _, _, _, _, _, _, _, _, _, _, _ = c, c2, c21, len1, rc, temp, z, zIn, zOut, zTerm, v1, v2
	/* If the translation is between UTF-16 little and big endian, then
	 ** all that is required is to swap the byte order. This case is handled
	 ** differently from the others.
	 */
	if int32((*TMem)(unsafe.Pointer(pMem)).Fenc) != int32(SQLITE_UTF8) && int32(desiredEnc) != int32(SQLITE_UTF8) {
		rc = _sqlite3VdbeMemMakeWriteable(tls, pMem)
		if rc != SQLITE_OK {
			return int32(SQLITE_NOMEM)
		}
		zIn = (*TMem)(unsafe.Pointer(pMem)).Fz
		zTerm = zIn + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn & ^libc.Int32FromInt32(1))
		for zIn < zTerm {
			temp = **(**uint8)(__ccgo_up(zIn))
			**(**uint8)(__ccgo_up(zIn)) = **(**uint8)(__ccgo_up(zIn + libc.UintptrFromInt32(1)))
			zIn = zIn + 1
			v1 = zIn
			zIn = zIn + 1
			**(**uint8)(__ccgo_up(v1)) = temp
		}
		(*TMem)(unsafe.Pointer(pMem)).Fenc = desiredEnc
		goto translate_out
	}
	/* Set len to the maximum number of bytes required in the output buffer. */
	if int32(desiredEnc) == int32(SQLITE_UTF8) {
		/* When converting from UTF-16, the maximum growth results from
		 ** translating a 2-byte character to a 4-byte UTF-8 character.
		 ** A single byte is required for the output string
		 ** nul-terminator.
		 */
		**(**int32)(__ccgo_up(pMem + 12)) &= ^libc.Int32FromInt32(1)
		len1 = int64(2)*int64((*TMem)(unsafe.Pointer(pMem)).Fn) + int64(1)
	} else {
		/* When converting from UTF-8 to UTF-16 the maximum growth is caused
		 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
		 ** character. Two bytes are required in the output buffer for the
		 ** nul-terminator.
		 */
		len1 = int64(2)*int64((*TMem)(unsafe.Pointer(pMem)).Fn) + int64(2)
	}
	/* Set zIn to point at the start of the input buffer and zTerm to point 1
	 ** byte past the end.
	 **
	 ** Variable zOut is set to point at the output buffer, space obtained
	 ** from sqlite3_malloc().
	 */
	zIn = (*TMem)(unsafe.Pointer(pMem)).Fz
	zTerm = zIn + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn)
	zOut = _sqlite3DbMallocRaw(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, uint64(len1))
	if !(zOut != 0) {
		return int32(SQLITE_NOMEM)
	}
	z = zOut
	if int32((*TMem)(unsafe.Pointer(pMem)).Fenc) == int32(SQLITE_UTF8) {
		if int32(desiredEnc) == int32(SQLITE_UTF16LE) {
			/* UTF-8 -> UTF-16 Little-endian */
			for zIn < zTerm {
				v1 = zIn
				zIn = zIn + 1
				c = uint32(**(**uint8)(__ccgo_up(v1)))
				if c >= uint32(0xc0) {
					c = uint32(_sqlite3Utf8Trans1[c-uint32(0xc0)])
					for zIn < zTerm && int32(**(**uint8)(__ccgo_up(zIn)))&int32(0xc0) == int32(0x80) {
						v1 = zIn
						zIn = zIn + 1
						c = c<<int32(6) + uint32(libc.Int32FromInt32(0x3f)&int32(**(**uint8)(__ccgo_up(v1))))
					}
					if c < uint32(0x80) || c&uint32(0xFFFFF800) == uint32(0xD800) || c&uint32(0xFFFFFFFE) == uint32(0xFFFE) {
						c = uint32(0xFFFD)
					}
				}
				if c <= uint32(0xFFFF) {
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c & libc.Uint32FromInt32(0x00FF))
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c >> libc.Int32FromInt32(8) & libc.Uint32FromInt32(0x00FF))
				} else {
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c>>libc.Int32FromInt32(10)&libc.Uint32FromInt32(0x003F) + (c-libc.Uint32FromInt32(0x10000))>>libc.Int32FromInt32(10)&libc.Uint32FromInt32(0x00C0))
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(libc.Uint32FromInt32(0x00D8) + (c-libc.Uint32FromInt32(0x10000))>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x03))
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c & libc.Uint32FromInt32(0x00FF))
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(libc.Uint32FromInt32(0x00DC) + c>>libc.Int32FromInt32(8)&libc.Uint32FromInt32(0x03))
				}
			}
		} else {
			/* UTF-8 -> UTF-16 Big-endian */
			for zIn < zTerm {
				v1 = zIn
				zIn = zIn + 1
				c = uint32(**(**uint8)(__ccgo_up(v1)))
				if c >= uint32(0xc0) {
					c = uint32(_sqlite3Utf8Trans1[c-uint32(0xc0)])
					for zIn < zTerm && int32(**(**uint8)(__ccgo_up(zIn)))&int32(0xc0) == int32(0x80) {
						v1 = zIn
						zIn = zIn + 1
						c = c<<int32(6) + uint32(libc.Int32FromInt32(0x3f)&int32(**(**uint8)(__ccgo_up(v1))))
					}
					if c < uint32(0x80) || c&uint32(0xFFFFF800) == uint32(0xD800) || c&uint32(0xFFFFFFFE) == uint32(0xFFFE) {
						c = uint32(0xFFFD)
					}
				}
				if c <= uint32(0xFFFF) {
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c >> libc.Int32FromInt32(8) & libc.Uint32FromInt32(0x00FF))
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c & libc.Uint32FromInt32(0x00FF))
				} else {
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(libc.Uint32FromInt32(0x00D8) + (c-libc.Uint32FromInt32(0x10000))>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x03))
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c>>libc.Int32FromInt32(10)&libc.Uint32FromInt32(0x003F) + (c-libc.Uint32FromInt32(0x10000))>>libc.Int32FromInt32(10)&libc.Uint32FromInt32(0x00C0))
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(libc.Uint32FromInt32(0x00DC) + c>>libc.Int32FromInt32(8)&libc.Uint32FromInt32(0x03))
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c & libc.Uint32FromInt32(0x00FF))
				}
			}
		}
		(*TMem)(unsafe.Pointer(pMem)).Fn = int32(z) - int32(zOut)
		v1 = z
		z = z + 1
		**(**uint8)(__ccgo_up(v1)) = uint8(0)
	} else {
		if int32((*TMem)(unsafe.Pointer(pMem)).Fenc) == int32(SQLITE_UTF16LE) {
			/* UTF-16 Little-endian -> UTF-8 */
			for zIn < zTerm {
				v1 = zIn
				zIn = zIn + 1
				c = uint32(**(**uint8)(__ccgo_up(v1)))
				v1 = zIn
				zIn = zIn + 1
				c = c + uint32(int32(**(**uint8)(__ccgo_up(v1)))<<int32(8))
				if c >= uint32(0xd800) && c < uint32(0xe000) {
					if zIn < zTerm {
						v1 = zIn
						zIn = zIn + 1
						c2 = int32(**(**uint8)(__ccgo_up(v1)))
						v2 = zIn
						zIn = zIn + 1
						c2 = c2 + int32(**(**uint8)(__ccgo_up(v2)))<<int32(8)
						c = uint32(c2&libc.Int32FromInt32(0x03FF)) + c&uint32(0x003F)<<int32(10) + (c&uint32(0x03C0)+uint32(0x0040))<<int32(10)
					}
				}
				if c < uint32(0x00080) {
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c & libc.Uint32FromInt32(0xFF))
				} else {
					if c < uint32(0x00800) {
						v1 = z
						z = z + 1
						**(**uint8)(__ccgo_up(v1)) = uint8(int32(0xC0) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1F))))
						v1 = z
						z = z + 1
						**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F))))
					} else {
						if c < uint32(0x10000) {
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0xE0) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0F))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F))))
						} else {
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0xF0) + int32(uint8(c>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3F))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F))))
						}
					}
				}
			}
		} else {
			/* UTF-16 Big-endian -> UTF-8 */
			for zIn < zTerm {
				v1 = zIn
				zIn = zIn + 1
				c = uint32(int32(**(**uint8)(__ccgo_up(v1))) << int32(8))
				v1 = zIn
				zIn = zIn + 1
				c = c + uint32(**(**uint8)(__ccgo_up(v1)))
				if c >= uint32(0xd800) && c < uint32(0xe000) {
					if zIn < zTerm {
						v1 = zIn
						zIn = zIn + 1
						c21 = int32(**(**uint8)(__ccgo_up(v1))) << int32(8)
						v2 = zIn
						zIn = zIn + 1
						c21 = c21 + int32(**(**uint8)(__ccgo_up(v2)))
						c = uint32(c21&libc.Int32FromInt32(0x03FF)) + c&uint32(0x003F)<<int32(10) + (c&uint32(0x03C0)+uint32(0x0040))<<int32(10)
					}
				}
				if c < uint32(0x00080) {
					v1 = z
					z = z + 1
					**(**uint8)(__ccgo_up(v1)) = uint8(c & libc.Uint32FromInt32(0xFF))
				} else {
					if c < uint32(0x00800) {
						v1 = z
						z = z + 1
						**(**uint8)(__ccgo_up(v1)) = uint8(int32(0xC0) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1F))))
						v1 = z
						z = z + 1
						**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F))))
					} else {
						if c < uint32(0x10000) {
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0xE0) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0F))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F))))
						} else {
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0xF0) + int32(uint8(c>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3F))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3F))))
							v1 = z
							z = z + 1
							**(**uint8)(__ccgo_up(v1)) = uint8(int32(0x80) + int32(uint8(c&libc.Uint32FromInt32(0x3F))))
						}
					}
				}
			}
		}
		(*TMem)(unsafe.Pointer(pMem)).Fn = int32(z) - int32(zOut)
	}
	**(**uint8)(__ccgo_up(z)) = uint8(0)
	c = uint32(libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Term) | int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_AffMask)|libc.Int32FromInt32(MEM_Subtype)))
	_sqlite3VdbeMemRelease(tls, pMem)
	(*TMem)(unsafe.Pointer(pMem)).Fflags = uint16(c)
	(*TMem)(unsafe.Pointer(pMem)).Fenc = desiredEnc
	(*TMem)(unsafe.Pointer(pMem)).Fz = zOut
	(*TMem)(unsafe.Pointer(pMem)).FzMalloc = (*TMem)(unsafe.Pointer(pMem)).Fz
	(*TMem)(unsafe.Pointer(pMem)).FszMalloc = _sqlite3DbMallocSize(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).Fz)
	goto translate_out
translate_out:
	;
	return SQLITE_OK
	return r
}

// C documentation
//
//	/*
//	** If pMem is already a string, detect if it is a zero-terminated
//	** string, or make it into one if possible, and mark it as such.
//	**
//	** This is an optimization.  Correct operation continues even if
//	** this routine is a no-op.
//	**
//	** Return true if the strig is zero-terminated after this routine is
//	** called and false if it is not.
//	*/
func _sqlite3VdbeMemZeroTerminateIfAble(tls *libc.TLS, pMem uintptr) (r int32) {
	var v1 uintptr
	_ = v1
	if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&(libc.Int32FromInt32(MEM_Str)|libc.Int32FromInt32(MEM_Term)|libc.Int32FromInt32(MEM_Ephem)|libc.Int32FromInt32(MEM_Static)) != int32(MEM_Str) {
		/* pMem must be a string, and it cannot be an ephemeral or static string */
		return 0
	}
	if int32((*TMem)(unsafe.Pointer(pMem)).Fenc) != int32(SQLITE_UTF8) {
		return 0
	}
	if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Dyn) != 0 {
		if (*TMem)(unsafe.Pointer(pMem)).FxDel == __ccgo_fp(Xsqlite3_free) && Xsqlite3_msize(tls, (*TMem)(unsafe.Pointer(pMem)).Fz) >= uint64((*TMem)(unsafe.Pointer(pMem)).Fn+libc.Int32FromInt32(1)) {
			**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = 0
			v1 = pMem + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
			return int32(1)
		}
		if (*TMem)(unsafe.Pointer(pMem)).FxDel == __ccgo_fp(_sqlite3RCStrUnref) {
			/* Blindly assume that all RCStr objects are zero-terminated */
			v1 = pMem + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
			return int32(1)
		}
	} else {
		if (*TMem)(unsafe.Pointer(pMem)).FszMalloc >= (*TMem)(unsafe.Pointer(pMem)).Fn+int32(1) {
			**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = 0
			v1 = pMem + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
			return int32(1)
		}
	}
	return 0
}

// C documentation
//
//	/*
//	** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN
//	** QUERY PLAN output.
//	**
//	** Return SQLITE_ROW on success.  Return SQLITE_DONE if there are no
//	** more opcodes to be displayed.
//	*/
func _sqlite3VdbeNextOpcode(tls *libc.TLS, p uintptr, pSub uintptr, eMode int32, piPc uintptr, piAddr uintptr, paOp uintptr) (r int32) {
	var aOp, apSub uintptr
	var i, iPc, j, j1, nByte, nRow, nSub, rc, v2 int32
	_, _, _, _, _, _, _, _, _, _, _ = aOp, apSub, i, iPc, j, j1, nByte, nRow, nSub, rc, v2 /* Stop when row count reaches this */
	nSub = 0                                                                               /* Number of sub-vdbes seen so far */
	apSub = uintptr(0)                                                                     /* Next instruction address */
	rc = SQLITE_OK                                                                         /* Result code */
	aOp = uintptr(0)                                                                       /* Rowid.  Copy of value in *piPc */
	/* When the number of output rows reaches nRow, that means the
	 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
	 ** nRow is the sum of the number of rows in the main program, plus
	 ** the sum of the number of rows in all trigger subprograms encountered
	 ** so far.  The nRow value will increase as new trigger subprograms are
	 ** encountered, but p->pc will eventually catch up to nRow.
	 */
	nRow = (*TVdbe)(unsafe.Pointer(p)).FnOp
	if pSub != uintptr(0) {
		if int32((*TMem)(unsafe.Pointer(pSub)).Fflags)&int32(MEM_Blob) != 0 {
			/* pSub is initiallly NULL.  It is initialized to a BLOB by
			 ** the P4_SUBPROGRAM processing logic below */
			nSub = int32(uint32((*TMem)(unsafe.Pointer(pSub)).Fn) / uint32(4))
			apSub = (*TMem)(unsafe.Pointer(pSub)).Fz
		}
		i = 0
		for {
			if !(i < nSub) {
				break
			}
			nRow = nRow + (*TSubProgram)(unsafe.Pointer(**(**uintptr)(__ccgo_up(apSub + uintptr(i)*4)))).FnOp
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	iPc = **(**int32)(__ccgo_up(piPc))
	for int32(1) != 0 { /* Loop exits via break */
		v2 = iPc
		iPc = iPc + 1
		i = v2
		if i >= nRow {
			(*TVdbe)(unsafe.Pointer(p)).Frc = SQLITE_OK
			rc = int32(SQLITE_DONE)
			break
		}
		if i < (*TVdbe)(unsafe.Pointer(p)).FnOp {
			/* The rowid is small enough that we are still in the
			 ** main program. */
			aOp = (*TVdbe)(unsafe.Pointer(p)).FaOp
		} else {
			i = i - (*TVdbe)(unsafe.Pointer(p)).FnOp
			j = 0
			for {
				if !(i >= (*TSubProgram)(unsafe.Pointer(**(**uintptr)(__ccgo_up(apSub + uintptr(j)*4)))).FnOp) {
					break
				}
				i = i - (*TSubProgram)(unsafe.Pointer(**(**uintptr)(__ccgo_up(apSub + uintptr(j)*4)))).FnOp
				goto _3
			_3:
				;
				j = j + 1
			}
			aOp = (*TSubProgram)(unsafe.Pointer(**(**uintptr)(__ccgo_up(apSub + uintptr(j)*4)))).FaOp
		}
		/* When an OP_Program opcode is encounter (the only opcode that has
		 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
		 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
		 ** has not already been seen.
		 */
		if pSub != uintptr(0) && int32((**(**TOp)(__ccgo_up(aOp + uintptr(i)*20))).Fp4type) == -int32(4) {
			nByte = int32(uint32(nSub+libc.Int32FromInt32(1)) * uint32(4))
			j1 = 0
			for {
				if !(j1 < nSub) {
					break
				}
				if **(**uintptr)(__ccgo_up(apSub + uintptr(j1)*4)) == *(*uintptr)(unsafe.Pointer(aOp + uintptr(i)*20 + 16)) {
					break
				}
				goto _4
			_4:
				;
				j1 = j1 + 1
			}
			if j1 == nSub {
				(*TVdbe)(unsafe.Pointer(p)).Frc = _sqlite3VdbeMemGrow(tls, pSub, nByte, libc.BoolInt32(nSub != 0))
				if (*TVdbe)(unsafe.Pointer(p)).Frc != SQLITE_OK {
					rc = int32(SQLITE_ERROR)
					break
				}
				apSub = (*TMem)(unsafe.Pointer(pSub)).Fz
				v2 = nSub
				nSub = nSub + 1
				**(**uintptr)(__ccgo_up(apSub + uintptr(v2)*4)) = *(*uintptr)(unsafe.Pointer(aOp + uintptr(i)*20 + 16))
				(*TMem)(unsafe.Pointer(pSub)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pSub)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Blob))
				(*TMem)(unsafe.Pointer(pSub)).Fn = int32(uint32(nSub) * uint32(4))
				nRow = nRow + (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aOp + uintptr(i)*20 + 16)))).FnOp
			}
		}
		if eMode == 0 {
			break
		}
		if int32((**(**TOp)(__ccgo_up(aOp + uintptr(i)*20))).Fopcode) == int32(OP_Explain) {
			break
		}
		if int32((**(**TOp)(__ccgo_up(aOp + uintptr(i)*20))).Fopcode) == int32(OP_Init) && iPc > int32(1) {
			break
		}
	}
	**(**int32)(__ccgo_up(piPc)) = iPc
	**(**int32)(__ccgo_up(piAddr)) = i
	**(**uintptr)(__ccgo_up(paOp)) = aOp
	return rc
}

// C documentation
//
//	/*
//	** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
//	** then cursor passed as the second argument should point to the row about
//	** to be update or deleted. If the application calls sqlite3_preupdate_old(),
//	** the required value will be read from the row the cursor points to.
//	*/
func _sqlite3VdbePreUpdateHook(tls *libc.TLS, v uintptr, pCsr uintptr, op int32, zDb uintptr, pTab uintptr, iKey1 Ti64, iReg int32, iBlobWrite int32) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var db, zTbl uintptr
	var i, i1 int32
	var iKey2, v1 Ti64
	var _ /* preupdate at bp+0 */ TPreUpdate
	_, _, _, _, _, _ = db, i, i1, iKey2, zTbl, v1
	db = (*TVdbe)(unsafe.Pointer(v)).Fdb
	zTbl = (*TTable)(unsafe.Pointer(pTab)).FzName
	libc.Xmemset(tls, bp, 0, uint32(136))
	if libc.BoolInt32((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0)) == 0 {
		v1 = libc.Int64FromInt32(0)
		iKey2 = v1
		iKey1 = v1
		(**(**TPreUpdate)(__ccgo_up(bp))).FpPk = _sqlite3PrimaryKeyIndex(tls, pTab)
	} else {
		if op == int32(SQLITE_UPDATE) {
			iKey2 = *(*Ti64)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(v)).FaMem + uintptr(iReg)*40))
		} else {
			iKey2 = iKey1
		}
	}
	(**(**TPreUpdate)(__ccgo_up(bp))).Fv = v
	(**(**TPreUpdate)(__ccgo_up(bp))).FpCsr = pCsr
	(**(**TPreUpdate)(__ccgo_up(bp))).Fop = op
	(**(**TPreUpdate)(__ccgo_up(bp))).FiNewReg = iReg
	(**(**TPreUpdate)(__ccgo_up(bp))).FpKeyinfo = bp + 112
	(*TKeyInfo)(unsafe.Pointer((**(**TPreUpdate)(__ccgo_up(bp))).FpKeyinfo)).Fdb = db
	(*TKeyInfo)(unsafe.Pointer((**(**TPreUpdate)(__ccgo_up(bp))).FpKeyinfo)).Fenc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc
	(*TKeyInfo)(unsafe.Pointer((**(**TPreUpdate)(__ccgo_up(bp))).FpKeyinfo)).FnKeyField = uint16((*TTable)(unsafe.Pointer(pTab)).FnCol)
	(*TKeyInfo)(unsafe.Pointer((**(**TPreUpdate)(__ccgo_up(bp))).FpKeyinfo)).FaSortFlags = uintptr(0) /* Indicate .aColl, .nAllField uninit */
	(**(**TPreUpdate)(__ccgo_up(bp))).FiKey1 = iKey1
	(**(**TPreUpdate)(__ccgo_up(bp))).FiKey2 = iKey2
	(**(**TPreUpdate)(__ccgo_up(bp))).FpTab = pTab
	(**(**TPreUpdate)(__ccgo_up(bp))).FiBlobWrite = iBlobWrite
	(*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate = bp
	(*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, uintptr, Tsqlite3_int64, Tsqlite3_int64))(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2)
	(*Tsqlite3)(unsafe.Pointer(db)).FpPreUpdate = uintptr(0)
	_sqlite3DbFree(tls, db, (**(**TPreUpdate)(__ccgo_up(bp))).FaRecord)
	_vdbeFreeUnpacked(tls, db, int32((*TKeyInfo)(unsafe.Pointer((**(**TPreUpdate)(__ccgo_up(bp))).FpKeyinfo)).FnKeyField)+int32(1), (**(**TPreUpdate)(__ccgo_up(bp))).FpUnpacked)
	_vdbeFreeUnpacked(tls, db, int32((*TKeyInfo)(unsafe.Pointer((**(**TPreUpdate)(__ccgo_up(bp))).FpKeyinfo)).FnKeyField)+int32(1), (**(**TPreUpdate)(__ccgo_up(bp))).FpNewUnpacked)
	_sqlite3VdbeMemRelease(tls, bp+56)
	if (**(**TPreUpdate)(__ccgo_up(bp))).FaNew != 0 {
		i = 0
		for {
			if !(i < int32((*TVdbeCursor)(unsafe.Pointer(pCsr)).FnField)) {
				break
			}
			_sqlite3VdbeMemRelease(tls, (**(**TPreUpdate)(__ccgo_up(bp))).FaNew+uintptr(i)*40)
			goto _2
		_2:
			;
			i = i + 1
		}
		_sqlite3DbNNFreeNN(tls, db, (**(**TPreUpdate)(__ccgo_up(bp))).FaNew)
	}
	if (**(**TPreUpdate)(__ccgo_up(bp))).FapDflt != 0 {
		i1 = 0
		for {
			if !(i1 < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up((**(**TPreUpdate)(__ccgo_up(bp))).FapDflt + uintptr(i1)*4)))
			goto _3
		_3:
			;
			i1 = i1 + 1
		}
		_sqlite3DbFree(tls, db, (**(**TPreUpdate)(__ccgo_up(bp))).FapDflt)
	}
}

/************** End of vdbeaux.c *********************************************/
/************** Begin file vdbeapi.c *****************************************/
/*
** 2004 May 26
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains code use to implement APIs that are part of the
** VDBE.
 */
/* #include "sqliteInt.h" */
/* #include "vdbeInt.h" */
/* #include "opcodes.h" */

// C documentation
//
//	/*
//	** This function compares the two table rows or index records
//	** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
//	** or positive integer if key1 is less than, equal to or
//	** greater than key2.  The {nKey1, pKey1} key must be a blob
//	** created by the OP_MakeRecord opcode of the VDBE.  The pPKey2
//	** key must be a parsed key such as obtained from
//	** sqlite3VdbeParseRecord.
//	**
//	** If argument bSkip is non-zero, it is assumed that the caller has already
//	** determined that the first fields of the keys are equal.
//	**
//	** Key1 and Key2 do not have to contain the same number of fields. If all
//	** fields that appear in both keys are equal, then pPKey2->default_rc is
//	** returned.
//	**
//	** If database corruption is discovered, set pPKey2->errCode to
//	** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
//	** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
//	** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
//	*/
func _sqlite3VdbeRecordCompareWithSkip(tls *libc.TLS, nKey1 int32, pKey1 uintptr, pPKey2 uintptr, bSkip int32) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var aKey1, pKeyInfo, pRhs, v4 uintptr
	var d1, idx1, v1 Tu32
	var i, nCmp, nCmp1, nStr, rc, sortFlags, v2 int32
	var lhs, rhs Ti64
	var v5 bool
	var _ /* mem1 at bp+8 */ TMem
	var _ /* s1 at bp+48 */ Tu32
	var _ /* serial_type at bp+52 */ Tu32
	var _ /* szHdr1 at bp+0 */ Tu32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aKey1, d1, i, idx1, lhs, nCmp, nCmp1, nStr, pKeyInfo, pRhs, rc, rhs, sortFlags, v1, v2, v4, v5 /* Offset of first type in header */
	rc = 0                                                                                                                                             /* Return value */
	pRhs = (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FaMem
	aKey1 = pKey1
	/* If bSkip is true, then the caller has already determined that the first
	 ** two elements in the keys are equal. Fix the various stack variables so
	 ** that this routine begins comparing at the second field. */
	if bSkip != 0 {
		**(**Tu32)(__ccgo_up(bp + 48)) = uint32(**(**uint8)(__ccgo_up(aKey1 + 1)))
		if **(**Tu32)(__ccgo_up(bp + 48)) < uint32(0x80) {
			idx1 = uint32(2)
		} else {
			idx1 = uint32(int32(1) + int32(_sqlite3GetVarint32(tls, aKey1+1, bp+48)))
		}
		**(**Tu32)(__ccgo_up(bp)) = uint32(**(**uint8)(__ccgo_up(aKey1)))
		d1 = **(**Tu32)(__ccgo_up(bp)) + _sqlite3VdbeSerialTypeLen(tls, **(**Tu32)(__ccgo_up(bp + 48)))
		i = int32(1)
		pRhs += 40
	} else {
		v1 = uint32(**(**uint8)(__ccgo_up(aKey1)))
		**(**Tu32)(__ccgo_up(bp)) = v1
		if v1 < uint32(0x80) {
			idx1 = uint32(1)
		} else {
			idx1 = uint32(_sqlite3GetVarint32(tls, aKey1, bp))
		}
		d1 = **(**Tu32)(__ccgo_up(bp))
		i = 0
	}
	if d1 > uint32(nKey1) {
		(*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(92647)))
		return 0 /* Corruption */
	}
	/* Only needed by assert() statements */
	for int32(1) != 0 {
		/* RHS is an integer */
		if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
			**(**Tu32)(__ccgo_up(bp + 52)) = uint32(**(**uint8)(__ccgo_up(aKey1 + uintptr(idx1))))
			if **(**Tu32)(__ccgo_up(bp + 52)) >= uint32(10) {
				if **(**Tu32)(__ccgo_up(bp + 52)) == uint32(10) {
					v2 = -int32(1)
				} else {
					v2 = +libc.Int32FromInt32(1)
				}
				rc = v2
			} else {
				if **(**Tu32)(__ccgo_up(bp + 52)) == uint32(0) {
					rc = -int32(1)
				} else {
					if **(**Tu32)(__ccgo_up(bp + 52)) == uint32(7) {
						_serialGet7(tls, aKey1+uintptr(d1), bp+8)
						rc = -_sqlite3IntFloatCompare(tls, *(*Ti64)(unsafe.Pointer(pRhs)), *(*float64)(unsafe.Pointer(bp + 8)))
					} else {
						lhs = _vdbeRecordDecodeInt(tls, **(**Tu32)(__ccgo_up(bp + 52)), aKey1+uintptr(d1))
						rhs = *(*Ti64)(unsafe.Pointer(pRhs))
						if lhs < rhs {
							rc = -int32(1)
						} else {
							if lhs > rhs {
								rc = +libc.Int32FromInt32(1)
							}
						}
					}
				}
			}
		} else {
			if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Real) != 0 {
				**(**Tu32)(__ccgo_up(bp + 52)) = uint32(**(**uint8)(__ccgo_up(aKey1 + uintptr(idx1))))
				if **(**Tu32)(__ccgo_up(bp + 52)) >= uint32(10) {
					/* Serial types 12 or greater are strings and blobs (greater than
					 ** numbers). Types 10 and 11 are currently "reserved for future
					 ** use", so it doesn't really matter what the results of comparing
					 ** them to numeric values are.  */
					if **(**Tu32)(__ccgo_up(bp + 52)) == uint32(10) {
						v2 = -int32(1)
					} else {
						v2 = +libc.Int32FromInt32(1)
					}
					rc = v2
				} else {
					if **(**Tu32)(__ccgo_up(bp + 52)) == uint32(0) {
						rc = -int32(1)
					} else {
						if **(**Tu32)(__ccgo_up(bp + 52)) == uint32(7) {
							if _serialGet7(tls, aKey1+uintptr(d1), bp+8) != 0 {
								rc = -int32(1) /* mem1 is a NaN */
							} else {
								if *(*float64)(unsafe.Pointer(bp + 8)) < *(*float64)(unsafe.Pointer(pRhs)) {
									rc = -int32(1)
								} else {
									if *(*float64)(unsafe.Pointer(bp + 8)) > *(*float64)(unsafe.Pointer(pRhs)) {
										rc = +libc.Int32FromInt32(1)
									} else {
									}
								}
							}
						} else {
							_sqlite3VdbeSerialGet(tls, aKey1+uintptr(d1), **(**Tu32)(__ccgo_up(bp + 52)), bp+8)
							rc = _sqlite3IntFloatCompare(tls, *(*Ti64)(unsafe.Pointer(bp + 8)), *(*float64)(unsafe.Pointer(pRhs)))
						}
					}
				}
			} else {
				if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Str) != 0 {
					**(**Tu32)(__ccgo_up(bp + 52)) = uint32(**(**uint8)(__ccgo_up(aKey1 + uintptr(idx1))))
					if **(**Tu32)(__ccgo_up(bp + 52)) >= uint32(0x80) {
						_sqlite3GetVarint32(tls, aKey1+uintptr(idx1), bp+52)
					}
					if **(**Tu32)(__ccgo_up(bp + 52)) < uint32(12) {
						rc = -int32(1)
					} else {
						if !(**(**Tu32)(__ccgo_up(bp + 52))&libc.Uint32FromInt32(0x01) != 0) {
							rc = +libc.Int32FromInt32(1)
						} else {
							(**(**TMem)(__ccgo_up(bp + 8))).Fn = int32((**(**Tu32)(__ccgo_up(bp + 52)) - uint32(12)) / uint32(2))
							if v5 = d1+uint32((**(**TMem)(__ccgo_up(bp + 8))).Fn) > uint32(nKey1); !v5 {
								v4 = (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FpKeyInfo
								pKeyInfo = v4
							}
							if v5 || int32((*TKeyInfo)(unsafe.Pointer(v4)).FnAllField) <= i {
								(*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(92728)))
								return 0 /* Corruption */
							} else {
								if *(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(i)*4)) != 0 {
									(**(**TMem)(__ccgo_up(bp + 8))).Fenc = (*TKeyInfo)(unsafe.Pointer(pKeyInfo)).Fenc
									(**(**TMem)(__ccgo_up(bp + 8))).Fdb = (*TKeyInfo)(unsafe.Pointer(pKeyInfo)).Fdb
									(**(**TMem)(__ccgo_up(bp + 8))).Fflags = uint16(MEM_Str)
									(**(**TMem)(__ccgo_up(bp + 8))).Fz = aKey1 + uintptr(d1)
									rc = _vdbeCompareMemString(tls, bp+8, pRhs, *(*uintptr)(unsafe.Pointer(pKeyInfo + 20 + uintptr(i)*4)), pPKey2+23)
								} else {
									if (**(**TMem)(__ccgo_up(bp + 8))).Fn < (*TMem)(unsafe.Pointer(pRhs)).Fn {
										v2 = (**(**TMem)(__ccgo_up(bp + 8))).Fn
									} else {
										v2 = (*TMem)(unsafe.Pointer(pRhs)).Fn
									}
									nCmp = v2
									rc = libc.Xmemcmp(tls, aKey1+uintptr(d1), (*TMem)(unsafe.Pointer(pRhs)).Fz, uint32(nCmp))
									if rc == 0 {
										rc = (**(**TMem)(__ccgo_up(bp + 8))).Fn - (*TMem)(unsafe.Pointer(pRhs)).Fn
									}
								}
							}
						}
					}
				} else {
					if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Blob) != 0 {
						**(**Tu32)(__ccgo_up(bp + 52)) = uint32(**(**uint8)(__ccgo_up(aKey1 + uintptr(idx1))))
						if **(**Tu32)(__ccgo_up(bp + 52)) >= uint32(0x80) {
							_sqlite3GetVarint32(tls, aKey1+uintptr(idx1), bp+52)
						}
						if **(**Tu32)(__ccgo_up(bp + 52)) < uint32(12) || **(**Tu32)(__ccgo_up(bp + 52))&uint32(0x01) != 0 {
							rc = -int32(1)
						} else {
							nStr = int32((**(**Tu32)(__ccgo_up(bp + 52)) - uint32(12)) / uint32(2))
							if d1+uint32(nStr) > uint32(nKey1) {
								(*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(92758)))
								return 0 /* Corruption */
							} else {
								if int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Zero) != 0 {
									if !(_isAllZero(tls, aKey1+uintptr(d1), nStr) != 0) {
										rc = int32(1)
									} else {
										rc = nStr - *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pRhs)).Fu))
									}
								} else {
									if nStr < (*TMem)(unsafe.Pointer(pRhs)).Fn {
										v2 = nStr
									} else {
										v2 = (*TMem)(unsafe.Pointer(pRhs)).Fn
									}
									nCmp1 = v2
									rc = libc.Xmemcmp(tls, aKey1+uintptr(d1), (*TMem)(unsafe.Pointer(pRhs)).Fz, uint32(nCmp1))
									if rc == 0 {
										rc = nStr - (*TMem)(unsafe.Pointer(pRhs)).Fn
									}
								}
							}
						}
					} else {
						**(**Tu32)(__ccgo_up(bp + 52)) = uint32(**(**uint8)(__ccgo_up(aKey1 + uintptr(idx1))))
						if **(**Tu32)(__ccgo_up(bp + 52)) == uint32(0) || **(**Tu32)(__ccgo_up(bp + 52)) == uint32(10) || **(**Tu32)(__ccgo_up(bp + 52)) == uint32(7) && _serialGet7(tls, aKey1+uintptr(d1), bp+8) != 0 {
						} else {
							rc = int32(1)
						}
					}
				}
			}
		}
		if rc != 0 {
			sortFlags = int32(**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FpKeyInfo)).FaSortFlags + uintptr(i))))
			if sortFlags != 0 {
				if sortFlags&int32(KEYINFO_ORDER_BIGNULL) == 0 || sortFlags&int32(KEYINFO_ORDER_DESC) != libc.BoolInt32(**(**Tu32)(__ccgo_up(bp + 52)) == uint32(0) || int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Null) != 0) {
					rc = -rc
				}
			}
			/* See comment below */
			return rc
		}
		i = i + 1
		if i == int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FnField) {
			break
		}
		pRhs += 40
		d1 = d1 + _sqlite3VdbeSerialTypeLen(tls, **(**Tu32)(__ccgo_up(bp + 52)))
		if d1 > uint32(nKey1) {
			break
		}
		idx1 = idx1 + uint32(_sqlite3VarintLen(tls, uint64(**(**Tu32)(__ccgo_up(bp + 52)))))
		if idx1 >= **(**Tu32)(__ccgo_up(bp)) {
			(*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(92809)))
			return 0 /* Corrupt index */
		}
	}
	/* No memory allocation is ever used on mem1.  Prove this using
	 ** the following assert().  If the assert() fails, it indicates a
	 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
	/* rc==0 here means that one or both of the keys ran out of fields and
	 ** all the fields up to that point were equal. Return the default_rc
	 ** value.  */
	(*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FeqSeen = uint8(1)
	return int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fdefault_rc)
}

// C documentation
//
//	/*
//	** Given the nKey-byte encoding of a record in pKey[], populate the
//	** UnpackedRecord structure indicated by the fourth argument with the
//	** contents of the decoded record.
//	*/
func _sqlite3VdbeRecordUnpack(tls *libc.TLS, nKey int32, pKey uintptr, p uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aKey, pKeyInfo, pMem uintptr
	var d, idx Tu32
	var u, v3 Tu16
	var v1 int32
	var _ /* serial_type at bp+4 */ Tu32
	var _ /* szHdr at bp+0 */ Tu32
	_, _, _, _, _, _, _, _ = aKey, d, idx, pKeyInfo, pMem, u, v1, v3
	aKey = pKey
	pMem = (*TUnpackedRecord)(unsafe.Pointer(p)).FaMem
	pKeyInfo = (*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo
	(*TUnpackedRecord)(unsafe.Pointer(p)).Fdefault_rc = 0
	if int32(**(**uint8)(__ccgo_up(aKey))) < int32(libc.Uint8FromInt32(0x80)) {
		**(**Tu32)(__ccgo_up(bp)) = uint32(**(**uint8)(__ccgo_up(aKey)))
		v1 = libc.Int32FromInt32(1)
	} else {
		v1 = int32(_sqlite3GetVarint32(tls, aKey, bp))
	}
	idx = uint32(uint8(v1))
	d = **(**Tu32)(__ccgo_up(bp))
	u = uint16(0)
	for idx < **(**Tu32)(__ccgo_up(bp)) && d <= uint32(nKey) {
		if int32(**(**uint8)(__ccgo_up(aKey + uintptr(idx)))) < int32(libc.Uint8FromInt32(0x80)) {
			**(**Tu32)(__ccgo_up(bp + 4)) = uint32(**(**uint8)(__ccgo_up(aKey + uintptr(idx))))
			v1 = libc.Int32FromInt32(1)
		} else {
			v1 = int32(_sqlite3GetVarint32(tls, aKey+uintptr(idx), bp+4))
		}
		idx = idx + uint32(uint8(v1))
		(*TMem)(unsafe.Pointer(pMem)).Fenc = (*TKeyInfo)(unsafe.Pointer(pKeyInfo)).Fenc
		(*TMem)(unsafe.Pointer(pMem)).Fdb = (*TKeyInfo)(unsafe.Pointer(pKeyInfo)).Fdb
		/* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
		(*TMem)(unsafe.Pointer(pMem)).FszMalloc = 0
		(*TMem)(unsafe.Pointer(pMem)).Fz = uintptr(0)
		_sqlite3VdbeSerialGet(tls, aKey+uintptr(d), **(**Tu32)(__ccgo_up(bp + 4)), pMem)
		d = d + _sqlite3VdbeSerialTypeLen(tls, **(**Tu32)(__ccgo_up(bp + 4)))
		u = u + 1
		v3 = u
		if int32(v3) >= int32((*TUnpackedRecord)(unsafe.Pointer(p)).FnField) {
			break
		}
		pMem += 40
	}
	if d > uint32(nKey) && u != 0 {
		/* In a corrupt record entry, the last pMem might have been set up using
		 ** uninitialized memory. Overwrite its value with NULL, to prevent
		 ** warnings from MSAN. */
		_sqlite3VdbeMemSetNull(tls, pMem-libc.BoolUintptr(int32(u) < int32((*TUnpackedRecord)(unsafe.Pointer(p)).FnField))*40)
	}
	(*TUnpackedRecord)(unsafe.Pointer(p)).FnField = u
}

// C documentation
//
//	/*
//	** Mark the VDBE as one that can be run multiple times.
//	*/
func _sqlite3VdbeReusable(tls *libc.TLS, p uintptr) {
	var i int32
	_ = i
	i = int32(1)
	for {
		if !(i < (*TVdbe)(unsafe.Pointer(p)).FnOp) {
			break
		}
		if int32((**(**TOp)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i)*20))).Fopcode) == int32(OP_Expire) {
			(**(**TOp)(__ccgo_up((*TVdbe)(unsafe.Pointer(p)).FaOp + 1*20))).Fopcode = uint8(OP_Noop)
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Set the name of the idx'th column to be returned by the SQL statement.
//	** zName must be a pointer to a nul terminated string.
//	**
//	** This call must be made after a call to sqlite3VdbeSetNumCols().
//	**
//	** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
//	** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
//	** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
//	*/
func _sqlite3VdbeSetColName(tls *libc.TLS, p uintptr, idx int32, var1 int32, zName uintptr, __ccgo_fp_xDel uintptr) (r int32) {
	var pColName uintptr
	var rc int32
	_, _ = pColName, rc
	if (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FmallocFailed != 0 {
		return int32(SQLITE_NOMEM)
	}
	pColName = (*TVdbe)(unsafe.Pointer(p)).FaColName + uintptr(idx+var1*int32((*TVdbe)(unsafe.Pointer(p)).FnResAlloc))*40
	rc = _sqlite3VdbeMemSetText(tls, pColName, zName, int64(-int32(1)), __ccgo_fp_xDel)
	return rc
}

// C documentation
//
//	/*
//	** Set the number of result columns that will be returned by this SQL
//	** statement. This is now set at compile time, rather than during
//	** execution of the vdbe program so that sqlite3_column_count() can
//	** be called on an SQL statement before sqlite3_step().
//	*/
func _sqlite3VdbeSetNumCols(tls *libc.TLS, p uintptr, nResColumn int32) {
	var db uintptr
	var n int32
	var v1 Tu16
	_, _, _ = db, n, v1
	db = (*TVdbe)(unsafe.Pointer(p)).Fdb
	if (*TVdbe)(unsafe.Pointer(p)).FnResAlloc != 0 {
		_releaseMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName, int32((*TVdbe)(unsafe.Pointer(p)).FnResAlloc)*int32(COLNAME_N))
		_sqlite3DbFree(tls, db, (*TVdbe)(unsafe.Pointer(p)).FaColName)
	}
	n = nResColumn * int32(COLNAME_N)
	v1 = uint16(nResColumn)
	(*TVdbe)(unsafe.Pointer(p)).FnResAlloc = v1
	(*TVdbe)(unsafe.Pointer(p)).FnResColumn = v1
	(*TVdbe)(unsafe.Pointer(p)).FaColName = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(40)*uint32(n)))
	if (*TVdbe)(unsafe.Pointer(p)).FaColName == uintptr(0) {
		return
	}
	_initMemArray(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName, n, db, uint16(MEM_Null))
}

// C documentation
//
//	/*
//	** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
//	*/
func _sqlite3VdbeSorterClose(tls *libc.TLS, db uintptr, pCsr uintptr) {
	var ii int32
	var pSorter uintptr
	_, _ = ii, pSorter
	pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36))
	if pSorter != 0 {
		ii = 0
		for {
			if !(ii < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) {
				break
			}
			**(**Tu64)(__ccgo_up(db + 552)) += (*(*TSortSubtask)(unsafe.Pointer(pSorter + 72 + uintptr(ii)*88))).FnSpill
			goto _1
		_1:
			;
			ii = ii + 1
		}
		_sqlite3VdbeSorterReset(tls, db, pSorter)
		Xsqlite3_free(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory)
		_sqlite3DbFree(tls, db, pSorter)
		*(*uintptr)(unsafe.Pointer(pCsr + 36)) = uintptr(0)
	}
}

// C documentation
//
//	/*
//	** Compare the key in memory cell pVal with the key that the sorter cursor
//	** passed as the first argument currently points to. For the purposes of
//	** the comparison, ignore the rowid field at the end of each record.
//	**
//	** If the sorter cursor key contains any NULL values, consider it to be
//	** less than pVal. Even if pVal also contains NULL values.
//	**
//	** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
//	** Otherwise, set *pRes to a negative, zero or positive value if the
//	** key in pVal is smaller than, equal to or larger than the current sorter
//	** key.
//	**
//	** This routine forms the core of the OP_SorterCompare opcode, which in
//	** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
//	*/
func _sqlite3VdbeSorterCompare(tls *libc.TLS, pCsr uintptr, pVal uintptr, nKeyCol int32, pRes uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i int32
	var pKey, pKeyInfo, pSorter, r2, v1 uintptr
	var _ /* nKey at bp+0 */ int32
	_, _, _, _, _, _ = i, pKey, pKeyInfo, pSorter, r2, v1 /* Sorter key to compare pVal with */
	pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36))
	r2 = (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpUnpacked
	pKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pCsr)).FpKeyInfo
	if r2 == uintptr(0) {
		v1 = _sqlite3VdbeAllocUnpackedRecord(tls, pKeyInfo)
		(*TVdbeSorter)(unsafe.Pointer(pSorter)).FpUnpacked = v1
		r2 = v1
		if r2 == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		(*TUnpackedRecord)(unsafe.Pointer(r2)).FnField = uint16(nKeyCol)
	}
	pKey = _vdbeSorterRowkey(tls, pSorter, bp)
	_sqlite3VdbeRecordUnpack(tls, **(**int32)(__ccgo_up(bp)), pKey, r2)
	i = 0
	for {
		if !(i < nKeyCol) {
			break
		}
		if int32((**(**TMem)(__ccgo_up((*TUnpackedRecord)(unsafe.Pointer(r2)).FaMem + uintptr(i)*40))).Fflags)&int32(MEM_Null) != 0 {
			**(**int32)(__ccgo_up(pRes)) = -int32(1)
			return SQLITE_OK
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	**(**int32)(__ccgo_up(pRes)) = _sqlite3VdbeRecordCompare(tls, (*TMem)(unsafe.Pointer(pVal)).Fn, (*TMem)(unsafe.Pointer(pVal)).Fz, r2)
	return SQLITE_OK
}

/************** End of vdbesort.c ********************************************/
/************** Begin file vdbevtab.c ****************************************/
/*
** 2020-03-23
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file implements virtual-tables for examining the bytecode content
** of a prepared statement.
 */
/* #include "sqliteInt.h" */

/************** End of vdbevtab.c ********************************************/
/************** Begin file memjournal.c **************************************/
/*
** 2008 October 7
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains code use to implement an in-memory rollback journal.
** The in-memory rollback journal is used to journal transactions for
** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
**
** Update:  The in-memory journal is also used to temporarily cache
** smaller journals that are not critical for power-loss recovery.
** For example, statement journals that are not too big will be held
** entirely in memory, thus reducing the number of file I/O calls, and
** more importantly, reducing temporary file creation events.  If these
** journals become too large for memory, they are spilled to disk.  But
** in the common case, they are usually small and no file I/O needs to
** occur.
 */
/* #include "sqliteInt.h" */

// C documentation
//
//	/*
//	** Initialize the temporary index cursor just opened as a sorter cursor.
//	**
//	** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
//	** to determine the number of fields that should be compared from the
//	** records being sorted. However, if the value passed as argument nField
//	** is non-zero and the sorter is able to guarantee a stable sort, nField
//	** is used instead. This is used when sorting records for a CREATE INDEX
//	** statement. In this case, keys are always delivered to the sorter in
//	** order of the primary key, which happens to be make up the final part
//	** of the records being sorted. So if the sort is stable, there is never
//	** any reason to compare PK fields and they can be ignored for a small
//	** performance boost.
//	**
//	** The sorter can guarantee a stable sort when running in single-threaded
//	** mode, but not in multi-threaded mode.
//	**
//	** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
//	*/
func _sqlite3VdbeSorterInit(tls *libc.TLS, db uintptr, nField int32, pCsr uintptr) (r int32) {
	var i, nWorker, pgsz, rc, szKeyInfo, v2 int32
	var mxCache, sz Ti64
	var pBt, pKeyInfo, pSorter, pTask, v1 uintptr
	var szPma Tu32
	var v4 int64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, mxCache, nWorker, pBt, pKeyInfo, pSorter, pTask, pgsz, rc, sz, szKeyInfo, szPma, v1, v2, v4 /* Size of pSorter in bytes */
	rc = SQLITE_OK
	/* Initialize the upper limit on the number of worker threads */
	if _sqlite3TempInMemory(tls, db) != 0 || int32(_sqlite3Config.FbCoreMutex) == 0 {
		nWorker = 0
	} else {
		nWorker = **(**int32)(__ccgo_up(db + 120 + 11*4))
	}
	/* Do not allow the total number of threads (main thread + all workers)
	 ** to exceed the maximum merge count */
	szKeyInfo = int32(uint32(libc.UintptrFromInt32(0)+20) + uint32((*TKeyInfo)(unsafe.Pointer((*TVdbeCursor)(unsafe.Pointer(pCsr)).FpKeyInfo)).FnAllField)*libc.Uint32FromInt64(4))
	sz = int64(uint32(libc.UintptrFromInt32(0)+72) + uint32(nWorker+libc.Int32FromInt32(1))*libc.Uint32FromInt64(88))
	pSorter = _sqlite3DbMallocZero(tls, db, uint64(sz+int64(szKeyInfo)))
	*(*uintptr)(unsafe.Pointer(pCsr + 36)) = pSorter
	if pSorter == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpBt
		v1 = pSorter + uintptr(sz)
		pKeyInfo = v1
		(*TVdbeSorter)(unsafe.Pointer(pSorter)).FpKeyInfo = v1
		libc.Xmemcpy(tls, pKeyInfo, (*TVdbeCursor)(unsafe.Pointer(pCsr)).FpKeyInfo, uint32(szKeyInfo))
		(*TKeyInfo)(unsafe.Pointer(pKeyInfo)).Fdb = uintptr(0)
		if nField != 0 && nWorker == 0 {
			(*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField = uint16(nField)
		}
		/* It is OK that pKeyInfo reuses the aSortFlags field from pCsr->pKeyInfo,
		 ** since the pCsr->pKeyInfo->aSortFlags[] array is invariant and lives
		 ** longer that pSorter. */
		_sqlite3BtreeEnter(tls, pBt)
		v2 = _sqlite3BtreeGetPageSize(tls, pBt)
		pgsz = v2
		(*TVdbeSorter)(unsafe.Pointer(pSorter)).Fpgsz = v2
		_sqlite3BtreeLeave(tls, pBt)
		(*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask = uint8(nWorker + int32(1))
		(*TVdbeSorter)(unsafe.Pointer(pSorter)).FiPrev = uint8(nWorker - libc.Int32FromInt32(1))
		(*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUseThreads = libc.BoolUint8(int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask) > libc.Int32FromInt32(1))
		(*TVdbeSorter)(unsafe.Pointer(pSorter)).Fdb = db
		i = 0
		for {
			if !(i < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) {
				break
			}
			pTask = pSorter + 72 + uintptr(i)*88
			(*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter = pSorter
			goto _3
		_3:
			;
			i = i + 1
		}
		if !(_sqlite3TempInMemory(tls, db) != 0) { /* Cache size in bytes*/
			szPma = _sqlite3Config.FszPma
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).FmnPmaSize = int32(szPma * uint32(pgsz))
			mxCache = int64((*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).Fcache_size)
			if mxCache < 0 {
				/* A negative cache-size value C indicates that the cache is abs(C)
				 ** KiB in size.  */
				mxCache = mxCache * int64(-int32(1024))
			} else {
				mxCache = mxCache * int64(pgsz)
			}
			if mxCache < int64(libc.Int32FromInt32(1)<<libc.Int32FromInt32(29)) {
				v4 = mxCache
			} else {
				v4 = int64(libc.Int32FromInt32(1) << libc.Int32FromInt32(29))
			}
			mxCache = v4
			if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FmnPmaSize > int32(mxCache) {
				v2 = (*TVdbeSorter)(unsafe.Pointer(pSorter)).FmnPmaSize
			} else {
				v2 = int32(mxCache)
			}
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize = v2
			/* Avoid large memory allocations if the application has requested
			 ** SQLITE_CONFIG_SMALL_MALLOC. */
			if int32(_sqlite3Config.FbSmallMalloc) == 0 {
				(*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory = pgsz
				(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory = _sqlite3Malloc(tls, uint64(pgsz))
				if !((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0) {
					rc = int32(SQLITE_NOMEM)
				}
			}
		}
		if int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnAllField) < int32(13) && (*(*uintptr)(unsafe.Pointer(pKeyInfo + 20)) == uintptr(0) || *(*uintptr)(unsafe.Pointer(pKeyInfo + 20)) == (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl) && int32(**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags)))&int32(KEYINFO_ORDER_BIGNULL) == 0 {
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).FtypeMask = uint8(libc.Int32FromInt32(SORTER_TYPE_INTEGER) | libc.Int32FromInt32(SORTER_TYPE_TEXT))
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Reset a sorting cursor back to its original empty state.
//	*/
func _sqlite3VdbeSorterReset(tls *libc.TLS, db uintptr, pSorter uintptr) {
	var i int32
	var pTask uintptr
	_, _ = i, pTask
	_vdbeSorterJoinAll(tls, pSorter, SQLITE_OK)
	if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader != 0 {
		_vdbePmaReaderClear(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader)
		_sqlite3DbFree(tls, db, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader)
		(*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader = uintptr(0)
	}
	_vdbeMergeEngineFree(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpMerger)
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).FpMerger = uintptr(0)
	i = 0
	for {
		if !(i < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) {
			break
		}
		pTask = pSorter + 72 + uintptr(i)*88
		_vdbeSortSubtaskCleanup(tls, db, pTask)
		(*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter = pSorter
		goto _1
	_1:
		;
		i = i + 1
	}
	if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory == uintptr(0) {
		_vdbeSorterRecordFree(tls, uintptr(0), (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList)
	}
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList = uintptr(0)
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA = 0
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUsePMA = uint8(0)
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory = 0
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxKeysize = 0
	_sqlite3DbFree(tls, db, (*TVdbeSorter)(unsafe.Pointer(pSorter)).FpUnpacked)
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).FpUnpacked = uintptr(0)
}

// C documentation
//
//	/*
//	** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
//	** this function is called to prepare for iterating through the records
//	** in sorted order.
//	*/
func _sqlite3VdbeSorterRewind(tls *libc.TLS, pCsr uintptr, pbEof uintptr) (r int32) {
	var pSorter uintptr
	var rc int32
	_, _ = pSorter, rc
	rc = SQLITE_OK /* Return code */
	pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36))
	/* If no data has been written to disk, then do not do so now. Instead,
	 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
	 ** from the in-memory list.  */
	if int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUsePMA) == 0 {
		if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList != 0 {
			**(**int32)(__ccgo_up(pbEof)) = 0
			rc = _vdbeSorterSort(tls, pSorter+72, pSorter+40)
		} else {
			**(**int32)(__ccgo_up(pbEof)) = int32(1)
		}
		return rc
	}
	/* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
	 ** function flushes the contents of memory to disk, it immediately always
	 ** creates a new list consisting of a single key immediately afterwards.
	 ** So the list is never empty at this point.  */
	rc = _vdbeSorterFlushPMA(tls, pSorter)
	/* Join all threads */
	rc = _vdbeSorterJoinAll(tls, pSorter, rc)
	/* Assuming no errors have occurred, set up a merger structure to
	 ** incrementally read and merge all remaining PMAs.  */
	if rc == SQLITE_OK {
		rc = _vdbeSorterSetupMerge(tls, pSorter)
		**(**int32)(__ccgo_up(pbEof)) = 0
	}
	return rc
}

// C documentation
//
//	/*
//	** Copy the current sorter key into the memory cell pOut.
//	*/
func _sqlite3VdbeSorterRowkey(tls *libc.TLS, pCsr uintptr, pOut uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pKey, pSorter uintptr
	var _ /* nKey at bp+0 */ int32
	_, _ = pKey, pSorter /* Sorter key to copy into pOut */
	pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36))
	pKey = _vdbeSorterRowkey(tls, pSorter, bp)
	if _sqlite3VdbeMemClearAndResize(tls, pOut, **(**int32)(__ccgo_up(bp))) != 0 {
		return int32(SQLITE_NOMEM)
	}
	(*TMem)(unsafe.Pointer(pOut)).Fn = **(**int32)(__ccgo_up(bp))
	(*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Blob))
	libc.Xmemcpy(tls, (*TMem)(unsafe.Pointer(pOut)).Fz, pKey, uint32(**(**int32)(__ccgo_up(bp))))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Add a record to the sorter.
//	*/
func _sqlite3VdbeSorterWrite(tls *libc.TLS, pCsr uintptr, pVal uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aNew, pNew, pSorter, v1 uintptr
	var bFlush, iListOff, nMin, rc int32
	var nNew Tsqlite3_int64
	var nPMA, nReq Ti64
	var _ /* t at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _ = aNew, bFlush, iListOff, nMin, nNew, nPMA, nReq, pNew, pSorter, rc, v1
	rc = SQLITE_OK /* serial type of first record field */
	pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 36))
	**(**int32)(__ccgo_up(bp)) = int32(uint32(**(**Tu8)(__ccgo_up((*TMem)(unsafe.Pointer(pVal)).Fz + 1))))
	if **(**int32)(__ccgo_up(bp)) >= int32(0x80) {
		_sqlite3GetVarint32(tls, (*TMem)(unsafe.Pointer(pVal)).Fz+1, bp)
	}
	if **(**int32)(__ccgo_up(bp)) > 0 && **(**int32)(__ccgo_up(bp)) < int32(10) && **(**int32)(__ccgo_up(bp)) != int32(7) {
		v1 = pSorter + 68
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & libc.Int32FromInt32(SORTER_TYPE_INTEGER))
	} else {
		if **(**int32)(__ccgo_up(bp)) > int32(10) && **(**int32)(__ccgo_up(bp))&int32(0x01) != 0 {
			v1 = pSorter + 68
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & libc.Int32FromInt32(SORTER_TYPE_TEXT))
		} else {
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).FtypeMask = uint8(0)
		}
	}
	/* Figure out whether or not the current contents of memory should be
	 ** flushed to a PMA before continuing. If so, do so.
	 **
	 ** If using the single large allocation mode (pSorter->aMemory!=0), then
	 ** flush the contents of memory to a new PMA if (a) at least one value is
	 ** already in memory and (b) the new value will not fit in memory.
	 **
	 ** Or, if using separate allocations for each record, flush the contents
	 ** of memory to a PMA if either of the following are true:
	 **
	 **   * The total memory allocated for the in-memory list is greater
	 **     than (page-size * cache-size), or
	 **
	 **   * The total memory allocated for the in-memory list is greater
	 **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
	 */
	nReq = int64(uint32((*TMem)(unsafe.Pointer(pVal)).Fn) + uint32(8))
	nPMA = int64((*TMem)(unsafe.Pointer(pVal)).Fn + _sqlite3VarintLen(tls, uint64((*TMem)(unsafe.Pointer(pVal)).Fn)))
	if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize != 0 {
		if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0 {
			bFlush = libc.BoolInt32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory != 0 && int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory)+nReq > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize))
		} else {
			bFlush = libc.BoolInt32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize) || (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmnPmaSize) && _sqlite3HeapNearlyFull(tls) != 0)
		}
		if bFlush != 0 {
			rc = _vdbeSorterFlushPMA(tls, pSorter)
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA = 0
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory = 0
		}
	}
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA += nPMA
	if nPMA > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxKeysize) {
		(*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxKeysize = int32(nPMA)
	}
	if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0 {
		nMin = int32(int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory) + nReq)
		if nMin > (*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory {
			nNew = int64(2) * int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory)
			iListOff = -int32(1)
			if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList != 0 {
				iListOff = int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList) - int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory)
			}
			for nNew < int64(nMin) {
				nNew = nNew * int64(2)
			}
			if nNew > int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize) {
				nNew = int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FmxPmaSize)
			}
			if nNew < int64(nMin) {
				nNew = int64(nMin)
			}
			aNew = _sqlite3Realloc(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory, uint64(nNew))
			if !(aNew != 0) {
				return int32(SQLITE_NOMEM)
			}
			if iListOff >= 0 {
				(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList = aNew + uintptr(iListOff)
			}
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory = aNew
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory = int32(nNew)
		}
		pNew = (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory + uintptr((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiMemory)
		v1 = pSorter + 56
		*(*int32)(unsafe.Pointer(v1)) = int32(int64(*(*int32)(unsafe.Pointer(v1))) + (nReq+libc.Int64FromInt32(7))&int64(^libc.Int32FromInt32(7)))
		if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList != 0 {
			*(*int32)(unsafe.Pointer(pNew + 4)) = int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList) - int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory)
		}
	} else {
		pNew = _sqlite3Malloc(tls, uint64(nReq))
		if pNew == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		*(*uintptr)(unsafe.Pointer(pNew + 4)) = (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList
	}
	libc.Xmemcpy(tls, pNew+libc.UintptrFromInt32(1)*8, (*TMem)(unsafe.Pointer(pVal)).Fz, uint32((*TMem)(unsafe.Pointer(pVal)).Fn))
	(*TSorterRecord)(unsafe.Pointer(pNew)).FnVal = (*TMem)(unsafe.Pointer(pVal)).Fn
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList = pNew
	return rc
}

// C documentation
//
//	/*
//	** Expression pExpr is a vector that has been used in a context where
//	** it is not permitted. If pExpr is a sub-select vector, this routine
//	** loads the Parse object with a message of the form:
//	**
//	**   "sub-select returns N columns - expected 1"
//	**
//	** Or, if it is a regular scalar vector:
//	**
//	**   "row value misused"
//	*/
func _sqlite3VectorErrorMsg(tls *libc.TLS, pParse uintptr, pExpr uintptr) {
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
		_sqlite3SubselectError(tls, pParse, (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList)).FnExpr, int32(1))
	} else {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8125, 0)
	}
}

// C documentation
//
//	/*
//	** Return a pointer to a subexpression of pVector that is the i-th
//	** column of the vector (numbered starting with 0).  The caller must
//	** ensure that i is within range.
//	**
//	** If pVector is really a scalar (and "scalar" here includes subqueries
//	** that return a single column!) then return pVector unmodified.
//	**
//	** pVector retains ownership of the returned subexpression.
//	**
//	** If the vector is a (SELECT ...) then the expression returned is
//	** just the expression for the i-th term of the result set, and may
//	** not be ready for evaluation because the table cursor has not yet
//	** been positioned.
//	*/
func _sqlite3VectorFieldSubexpr(tls *libc.TLS, pVector uintptr, i int32) (r uintptr) {
	if _sqlite3ExprIsVector(tls, pVector) != 0 {
		if int32((*TExpr)(unsafe.Pointer(pVector)).Fop) == int32(TK_SELECT) || int32((*TExpr)(unsafe.Pointer(pVector)).Fop2) == int32(TK_SELECT) {
			return (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 20)))).FpEList + 8 + uintptr(i)*20))).FpExpr
		} else {
			return (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 20)) + 8 + uintptr(i)*20))).FpExpr
		}
	}
	return pVector
}

// C documentation
//
//	/*
//	** The parser calls this routine for each token after the first token
//	** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
//	*/
func _sqlite3VtabArgExtend(tls *libc.TLS, pParse uintptr, p uintptr) {
	var pArg uintptr
	_ = pArg
	pArg = pParse + 264
	if (*TToken)(unsafe.Pointer(pArg)).Fz == uintptr(0) {
		(*TToken)(unsafe.Pointer(pArg)).Fz = (*TToken)(unsafe.Pointer(p)).Fz
		(*TToken)(unsafe.Pointer(pArg)).Fn = (*TToken)(unsafe.Pointer(p)).Fn
	} else {
		(*TToken)(unsafe.Pointer(pArg)).Fn = uint32(t__predefined_ptrdiff_t((*TToken)(unsafe.Pointer(p)).Fz+uintptr((*TToken)(unsafe.Pointer(p)).Fn)) - int32((*TToken)(unsafe.Pointer(pArg)).Fz))
	}
}

// C documentation
//
//	/*
//	** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
//	** statement.  The module name has been parsed, but the optional list
//	** of parameters that follow the module name are still pending.
//	*/
func _sqlite3VtabBeginParse(tls *libc.TLS, pParse uintptr, pName1 uintptr, pName2 uintptr, pModuleName uintptr, ifNotExists int32) {
	var db, pTable uintptr
	var iDb int32
	_, _, _ = db, iDb, pTable /* Database connection */
	_sqlite3StartTable(tls, pParse, pName1, pName2, 0, 0, int32(1), ifNotExists)
	pTable = (*TParse)(unsafe.Pointer(pParse)).FpNewTable
	if pTable == uintptr(0) {
		return
	}
	(*TTable)(unsafe.Pointer(pTable)).FeTabType = uint8(TABTYP_VTAB)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	_addModuleArgument(tls, pParse, pTable, _sqlite3NameFromToken(tls, db, pModuleName))
	_addModuleArgument(tls, pParse, pTable, uintptr(0))
	_addModuleArgument(tls, pParse, pTable, _sqlite3DbStrDup(tls, db, (*TTable)(unsafe.Pointer(pTable)).FzName))
	(*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fn = uint32(t__predefined_ptrdiff_t((*TToken)(unsafe.Pointer(pModuleName)).Fz+uintptr((*TToken)(unsafe.Pointer(pModuleName)).Fn)) - int32((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz))
	/* Creating a virtual table invokes the authorization callback twice.
	 ** The first invocation, to obtain permission to INSERT a row into the
	 ** sqlite_schema table, has already been made by sqlite3StartTable().
	 ** The second call, to obtain permission to create the table, is made now.
	 */
	if (*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTable + 44))).FazArg != 0 {
		iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTable)).FpSchema)
		/* The database the table is being created in */
		_sqlite3AuthCheck(tls, pParse, int32(SQLITE_CREATE_VTABLE), (*TTable)(unsafe.Pointer(pTable)).FzName, **(**uintptr)(__ccgo_up((*(*struct {
			FnArg  int32
			FazArg uintptr
			Fp     uintptr
		})(unsafe.Pointer(pTable + 44))).FazArg)), (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*16))).FzDbSName)
	}
}

// C documentation
//
//	/*
//	** This function is invoked by the parser to call the xConnect() method
//	** of the virtual table pTab. If an error occurs, an error code is returned
//	** and an error left in pParse.
//	**
//	** This call is a no-op if table pTab is not a virtual table.
//	*/
func _sqlite3VtabCallConnect(tls *libc.TLS, pParse uintptr, pTab uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pMod, zMod, zModule uintptr
	var rc int32
	var _ /* zErr at bp+0 */ uintptr
	_, _, _, _, _ = db, pMod, rc, zMod, zModule
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if _sqlite3GetVTable(tls, db, pTab) != 0 {
		return SQLITE_OK
	}
	/* Locate the required virtual table module */
	zMod = **(**uintptr)(__ccgo_up((*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).FazArg))
	pMod = _sqlite3HashFind(tls, db+404, zMod)
	if !(pMod != 0) {
		zModule = **(**uintptr)(__ccgo_up((*(*struct {
			FnArg  int32
			FazArg uintptr
			Fp     uintptr
		})(unsafe.Pointer(pTab + 44))).FazArg))
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+24820, libc.VaList(bp+16, zModule))
		rc = int32(SQLITE_ERROR)
	} else {
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		rc = _vtabCallConstructor(tls, db, pTab, pMod, (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxConnect, bp)
		if rc != SQLITE_OK {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+4729, libc.VaList(bp+16, **(**uintptr)(__ccgo_up(bp))))
			(*TParse)(unsafe.Pointer(pParse)).Frc = rc
		}
		_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is invoked by the vdbe to call the xCreate method
//	** of the virtual table named zTab in database iDb.
//	**
//	** If an error occurs, *pzErr is set to point to an English language
//	** description of the error and an SQLITE_XXX error code is returned.
//	** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
//	*/
func _sqlite3VtabCallCreate(tls *libc.TLS, db uintptr, iDb int32, zTab uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pMod, pTab, zMod uintptr
	var rc int32
	_, _, _, _ = pMod, pTab, rc, zMod
	rc = SQLITE_OK
	pTab = _sqlite3FindTable(tls, db, zTab, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName)
	/* Locate the required virtual table module */
	zMod = **(**uintptr)(__ccgo_up((*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).FazArg))
	pMod = _sqlite3HashFind(tls, db+404, zMod)
	/* If the module has been registered and includes a Create method,
	 ** invoke it now. If the module has not been registered, return an
	 ** error. Otherwise, do nothing.
	 */
	if pMod == uintptr(0) || (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxCreate == uintptr(0) || (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxDestroy == uintptr(0) {
		**(**uintptr)(__ccgo_up(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+24820, libc.VaList(bp+8, zMod))
		rc = int32(SQLITE_ERROR)
	} else {
		rc = _vtabCallConstructor(tls, db, pTab, pMod, (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer(pMod)).FpModule)).FxCreate, pzErr)
	}
	/* Justification of ALWAYS():  The xConstructor method is required to
	 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
	if rc == SQLITE_OK && _sqlite3GetVTable(tls, db, pTab) != 0 {
		rc = _growVTrans(tls, db)
		if rc == SQLITE_OK {
			_addToVTrans(tls, db, _sqlite3GetVTable(tls, db, pTab))
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is invoked by the vdbe to call the xDestroy method
//	** of the virtual table named zTab in database iDb. This occurs
//	** when a DROP TABLE is mentioned.
//	**
//	** This call is a no-op if zTab is not a virtual table.
//	*/
func _sqlite3VtabCallDestroy(tls *libc.TLS, db uintptr, iDb int32, zTab uintptr) (r int32) {
	var p, pTab, xDestroy uintptr
	var rc int32
	_, _, _, _ = p, pTab, rc, xDestroy
	rc = SQLITE_OK
	pTab = _sqlite3FindTable(tls, db, zTab, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName)
	if pTab != uintptr(0) && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) && (*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).Fp != uintptr(0) {
		p = (*(*struct {
			FnArg  int32
			FazArg uintptr
			Fp     uintptr
		})(unsafe.Pointer(pTab + 44))).Fp
		for {
			if !(p != 0) {
				break
			}
			if (*Tsqlite3_vtab)(unsafe.Pointer((*TVTable)(unsafe.Pointer(p)).FpVtab)).FnRef > 0 {
				return int32(SQLITE_LOCKED)
			}
			goto _1
		_1:
			;
			p = (*TVTable)(unsafe.Pointer(p)).FpNext
		}
		p = _vtabDisconnectAll(tls, db, pTab)
		xDestroy = (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer(p)).FpMod)).FpModule)).FxDestroy
		if xDestroy == uintptr(0) {
			xDestroy = (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer(p)).FpMod)).FpModule)).FxDisconnect
		}
		(*TTable)(unsafe.Pointer(pTab)).FnTabRef = (*TTable)(unsafe.Pointer(pTab)).FnTabRef + 1
		rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xDestroy})))(tls, (*TVTable)(unsafe.Pointer(p)).FpVtab)
		/* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
		if rc == SQLITE_OK {
			(*TVTable)(unsafe.Pointer(p)).FpVtab = uintptr(0)
			(*(*struct {
				FnArg  int32
				FazArg uintptr
				Fp     uintptr
			})(unsafe.Pointer(pTab + 44))).Fp = uintptr(0)
			_sqlite3VtabUnlock(tls, p)
		}
		_sqlite3DeleteTable(tls, db, pTab)
	}
	return rc
}

// C documentation
//
//	/*
//	** Invoke the xCommit method of all virtual tables in the
//	** sqlite3.aVTrans array. Then clear the array itself.
//	*/
func _sqlite3VtabCommit(tls *libc.TLS, db uintptr) (r int32) {
	_callFinaliser(tls, db, int32(uint32(libc.UintptrFromInt32(0)+64)))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Construct and install a Module object for a virtual table.  When this
//	** routine is called, it is guaranteed that all appropriate locks are held
//	** and the module is not already part of the connection.
//	**
//	** If there already exists a module with zName, replace it with the new one.
//	** If pModule==0, then delete the module zName if it exists.
//	*/
func _sqlite3VtabCreateModule(tls *libc.TLS, db uintptr, zName uintptr, pModule uintptr, pAux uintptr, __ccgo_fp_xDestroy uintptr) (r uintptr) {
	var nName int32
	var pDel, pMod, zCopy uintptr
	_, _, _, _ = nName, pDel, pMod, zCopy
	if pModule == uintptr(0) {
		zCopy = zName
		pMod = uintptr(0)
	} else {
		nName = _sqlite3Strlen30(tls, zName)
		pMod = _sqlite3Malloc(tls, uint64(uint32(24)+uint32(nName)+uint32(1)))
		if pMod == uintptr(0) {
			_sqlite3OomFault(tls, db)
			return uintptr(0)
		}
		zCopy = pMod + 1*24
		libc.Xmemcpy(tls, zCopy, zName, uint32(nName+int32(1)))
		(*TModule)(unsafe.Pointer(pMod)).FzName = zCopy
		(*TModule)(unsafe.Pointer(pMod)).FpModule = pModule
		(*TModule)(unsafe.Pointer(pMod)).FpAux = pAux
		(*TModule)(unsafe.Pointer(pMod)).FxDestroy = __ccgo_fp_xDestroy
		(*TModule)(unsafe.Pointer(pMod)).FpEpoTab = uintptr(0)
		(*TModule)(unsafe.Pointer(pMod)).FnRefModule = int32(1)
	}
	pDel = _sqlite3HashInsert(tls, db+404, zCopy, pMod)
	if pDel != 0 {
		if pDel == pMod {
			_sqlite3OomFault(tls, db)
			_sqlite3DbFree(tls, db, pDel)
			pMod = uintptr(0)
		} else {
			_sqlite3VtabEponymousTableClear(tls, db, pDel)
			_sqlite3VtabModuleUnref(tls, db, pDel)
		}
	}
	return pMod
}

// C documentation
//
//	/*
//	** Check to see if virtual table module pMod can be have an eponymous
//	** virtual table instance.  If it can, create one if one does not already
//	** exist. Return non-zero if either the eponymous virtual table instance
//	** exists when this routine returns or if an attempt to create it failed
//	** and an error message was left in pParse.
//	**
//	** An eponymous virtual table instance is one that is named after its
//	** module, and more importantly, does not require a CREATE VIRTUAL TABLE
//	** statement in order to come into existence.  Eponymous virtual table
//	** instances always exist.  They cannot be DROP-ed.
//	**
//	** Any virtual table module for which xConnect and xCreate are the same
//	** method can have an eponymous virtual table instance.
//	*/
func _sqlite3VtabEponymousTableInit(tls *libc.TLS, pParse uintptr, pMod uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pModule, pTab uintptr
	var rc int32
	var _ /* zErr at bp+0 */ uintptr
	_, _, _, _ = db, pModule, pTab, rc
	pModule = (*TModule)(unsafe.Pointer(pMod)).FpModule
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if (*TModule)(unsafe.Pointer(pMod)).FpEpoTab != 0 {
		return int32(1)
	}
	if (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxCreate != uintptr(0) && (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxCreate != (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxConnect {
		return 0
	}
	pTab = _sqlite3DbMallocZero(tls, db, uint64(80))
	if pTab == uintptr(0) {
		return 0
	}
	(*TTable)(unsafe.Pointer(pTab)).FzName = _sqlite3DbStrDup(tls, db, (*TModule)(unsafe.Pointer(pMod)).FzName)
	if (*TTable)(unsafe.Pointer(pTab)).FzName == uintptr(0) {
		_sqlite3DbFree(tls, db, pTab)
		return 0
	}
	(*TModule)(unsafe.Pointer(pMod)).FpEpoTab = pTab
	(*TTable)(unsafe.Pointer(pTab)).FnTabRef = uint32(1)
	(*TTable)(unsafe.Pointer(pTab)).FeTabType = uint8(TABTYP_VTAB)
	(*TTable)(unsafe.Pointer(pTab)).FpSchema = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema
	(*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1))
	**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(TF_Eponymous)
	_addModuleArgument(tls, pParse, pTab, _sqlite3DbStrDup(tls, db, (*TTable)(unsafe.Pointer(pTab)).FzName))
	_addModuleArgument(tls, pParse, pTab, uintptr(0))
	_addModuleArgument(tls, pParse, pTab, _sqlite3DbStrDup(tls, db, (*TTable)(unsafe.Pointer(pTab)).FzName))
	(*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock = (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock + 1
	rc = _vtabCallConstructor(tls, db, pTab, pMod, (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxConnect, bp)
	(*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock = (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock - 1
	if rc != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+4729, libc.VaList(bp+16, **(**uintptr)(__ccgo_up(bp))))
		(*TParse)(unsafe.Pointer(pParse)).Frc = rc
		_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
		_sqlite3VtabEponymousTableClear(tls, db, pMod)
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** The parser calls this routine after the CREATE VIRTUAL TABLE statement
//	** has been completely parsed.
//	*/
func _sqlite3VtabFinishParse(tls *libc.TLS, pParse uintptr, pEnd uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var db, pOld, pSchema, pTab, v, zName, zStmt, zWhere, v2 uintptr
	var iDb, iReg, v1 int32
	_, _, _, _, _, _, _, _, _, _, _, _ = db, iDb, iReg, pOld, pSchema, pTab, v, zName, zStmt, zWhere, v1, v2
	pTab = (*TParse)(unsafe.Pointer(pParse)).FpNewTable /* The table being constructed */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb          /* The database connection */
	if pTab == uintptr(0) {
		return
	}
	_addArgumentToVtab(tls, pParse)
	(*TParse)(unsafe.Pointer(pParse)).FsArg.Fz = uintptr(0)
	if (*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).FnArg < int32(1) {
		return
	}
	/* If the CREATE VIRTUAL TABLE statement is being entered for the
	 ** first time (in other words if the virtual table is actually being
	 ** created now instead of just being read out of sqlite_schema) then
	 ** do additional initialization work and store the statement text
	 ** in the sqlite_schema table.
	 */
	if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) {
		_sqlite3MayAbort(tls, pParse)
		/* Compute the complete text of the CREATE VIRTUAL TABLE statement */
		if pEnd != 0 {
			(*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fn = uint32(int32((*TToken)(unsafe.Pointer(pEnd)).Fz)-int32((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz)) + (*TToken)(unsafe.Pointer(pEnd)).Fn
		}
		zStmt = _sqlite3MPrintf(tls, db, __ccgo_ts+24560, libc.VaList(bp+8, pParse+172))
		/* A slot for the record has already been allocated in the
		 ** schema table.  We just need to update that slot with all
		 ** the information we've collected.
		 **
		 ** The VM register number pParse->u1.cr.regRowid holds the rowid of an
		 ** entry in the sqlite_schema table that was created for this vtab
		 ** by sqlite3StartTable().
		 */
		iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
		_sqlite3NestedParse(tls, pParse, __ccgo_ts+24584, libc.VaList(bp+8, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, (*TTable)(unsafe.Pointer(pTab)).FzName, (*TTable)(unsafe.Pointer(pTab)).FzName, zStmt, (*(*struct {
			FaddrCrTab      int32
			FregRowid       int32
			FregRoot        int32
			FconstraintName TToken
		})(unsafe.Pointer(pParse + 188))).FregRowid))
		v = _sqlite3GetVdbe(tls, pParse)
		_sqlite3ChangeCookie(tls, pParse, iDb)
		_sqlite3VdbeAddOp0(tls, v, int32(OP_Expire))
		zWhere = _sqlite3MPrintf(tls, db, __ccgo_ts+24683, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, zStmt))
		_sqlite3VdbeAddParseSchemaOp(tls, v, iDb, zWhere, uint16(0))
		_sqlite3DbFree(tls, db, zStmt)
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		iReg = v1
		_sqlite3VdbeLoadString(tls, v, iReg, (*TTable)(unsafe.Pointer(pTab)).FzName)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_VCreate), iDb, iReg)
	} else {
		pSchema = (*TTable)(unsafe.Pointer(pTab)).FpSchema
		zName = (*TTable)(unsafe.Pointer(pTab)).FzName
		_sqlite3MarkAllShadowTablesOf(tls, db, pTab)
		pOld = _sqlite3HashInsert(tls, pSchema+8, zName, pTab)
		if pOld != 0 {
			_sqlite3OomFault(tls, db)
			/* Malloc must have failed inside HashInsert() */
			return
		}
		(*TParse)(unsafe.Pointer(pParse)).FpNewTable = uintptr(0)
	}
}

// C documentation
//
//	/*
//	** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
//	** array so that an OP_VBegin will get generated for it.  Add pTab to the
//	** array if it is missing.  If pTab is already in the array, this routine
//	** is a no-op.
//	*/
func _sqlite3VtabMakeWritable(tls *libc.TLS, pParse uintptr, pTab uintptr) {
	var apVtabLock, pToplevel, v1 uintptr
	var i, n, v3 int32
	_, _, _, _, _, _ = apVtabLock, i, n, pToplevel, v1, v3
	if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 {
		v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel
	} else {
		v1 = pParse
	}
	pToplevel = v1
	i = 0
	for {
		if !(i < (*TParse)(unsafe.Pointer(pToplevel)).FnVtabLock) {
			break
		}
		if pTab == **(**uintptr)(__ccgo_up((*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock + uintptr(i)*4)) {
			return
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	n = int32(uint32((*TParse)(unsafe.Pointer(pToplevel)).FnVtabLock+libc.Int32FromInt32(1)) * uint32(4))
	apVtabLock = _sqlite3Realloc(tls, (*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock, uint64(n))
	if apVtabLock != 0 {
		(*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock = apVtabLock
		v1 = pToplevel + 224
		v3 = *(*int32)(unsafe.Pointer(v1))
		*(*int32)(unsafe.Pointer(v1)) = *(*int32)(unsafe.Pointer(v1)) + 1
		**(**uintptr)(__ccgo_up((*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock + uintptr(v3)*4)) = pTab
	} else {
		_sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(pToplevel)).Fdb)
	}
}

// C documentation
//
//	/*
//	** The first parameter (pDef) is a function implementation.  The
//	** second parameter (pExpr) is the first argument to this function.
//	** If pExpr is a column in a virtual table, then let the virtual
//	** table implementation have an opportunity to overload the function.
//	**
//	** This routine is used to allow virtual table implementations to
//	** overload MATCH, LIKE, GLOB, and REGEXP operators.
//	**
//	** Return either the pDef argument (indicating no change) or a
//	** new FuncDef structure that is marked as ephemeral using the
//	** SQLITE_FUNC_EPHEM flag.
//	*/
func _sqlite3VtabOverloadFunction(tls *libc.TLS, db uintptr, pDef uintptr, nArg int32, pExpr uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pMod, pNew, pTab, pVtab uintptr
	var rc int32
	var _ /* pArg at bp+4 */ uintptr
	var _ /* xSFunc at bp+0 */ uintptr
	_, _, _, _, _ = pMod, pNew, pTab, pVtab, rc
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0)
	rc = 0
	/* Check to see the left operand is a column in a virtual table */
	if pExpr == uintptr(0) {
		return pDef
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) {
		return pDef
	}
	pTab = (*TExpr)(unsafe.Pointer(pExpr)).Fy.FpTab
	if pTab == uintptr(0) {
		return pDef
	}
	if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) {
		return pDef
	}
	pVtab = (*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, db, pTab))).FpVtab
	pMod = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule
	if (*Tsqlite3_module)(unsafe.Pointer(pMod)).FxFindFunction == uintptr(0) {
		return pDef
	}
	/* Call the xFindFunction method on the virtual table implementation
	 ** to see if the implementation wants to overload this function.
	 **
	 ** Though undocumented, we have historically always invoked xFindFunction
	 ** with an all lower-case function name.  Continue in this tradition to
	 ** avoid any chance of an incompatibility.
	 */
	rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pMod)).FxFindFunction})))(tls, pVtab, nArg, (*TFuncDef)(unsafe.Pointer(pDef)).FzName, bp, bp+4)
	if rc == 0 {
		return pDef
	}
	/* Create a new ephemeral function definition for the overloaded
	 ** function */
	pNew = _sqlite3DbMallocZero(tls, db, uint64(uint32(40)+uint32(_sqlite3Strlen30(tls, (*TFuncDef)(unsafe.Pointer(pDef)).FzName))+uint32(1)))
	if pNew == uintptr(0) {
		return pDef
	}
	**(**TFuncDef)(__ccgo_up(pNew)) = **(**TFuncDef)(__ccgo_up(pDef))
	(*TFuncDef)(unsafe.Pointer(pNew)).FzName = pNew + 1*40
	libc.Xmemcpy(tls, pNew+1*40, (*TFuncDef)(unsafe.Pointer(pDef)).FzName, uint32(_sqlite3Strlen30(tls, (*TFuncDef)(unsafe.Pointer(pDef)).FzName)+int32(1)))
	(*TFuncDef)(unsafe.Pointer(pNew)).FxSFunc = **(**uintptr)(__ccgo_up(bp))
	(*TFuncDef)(unsafe.Pointer(pNew)).FpUserData = **(**uintptr)(__ccgo_up(bp + 4))
	**(**Tu32)(__ccgo_up(pNew + 4)) |= uint32(SQLITE_FUNC_EPHEM)
	return pNew
}

// C documentation
//
//	/*
//	** Invoke the xRollback method of all virtual tables in the
//	** sqlite3.aVTrans array. Then clear the array itself.
//	*/
func _sqlite3VtabRollback(tls *libc.TLS, db uintptr) (r int32) {
	_callFinaliser(tls, db, int32(uint32(libc.UintptrFromInt32(0)+68)))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This routine is called to implement sqlite3_wal_checkpoint() and
//	** related interfaces.
//	**
//	** Obtain a CHECKPOINT lock and then backfill as much information as
//	** we can from WAL into the database.
//	**
//	** If parameter xBusy is not NULL, it is a pointer to a busy-handler
//	** callback. In this case this function runs a blocking checkpoint.
//	*/
func _sqlite3WalCheckpoint(tls *libc.TLS, pWal uintptr, db uintptr, eMode int32, __ccgo_fp_xBusy uintptr, pBusyArg uintptr, sync_flags int32, nBuf int32, zBuf uintptr, pnLog uintptr, pnCkpt uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var eMode2, rc, v1 int32
	var xBusy2 uintptr
	var _ /* isChanged at bp+0 */ int32
	_, _, _, _ = eMode2, rc, xBusy2, v1 /* Return code */
	**(**int32)(__ccgo_up(bp)) = 0      /* True if a new wal-index header is loaded */
	eMode2 = eMode                      /* Mode to pass to walCheckpoint() */
	xBusy2 = __ccgo_fp_xBusy            /* Busy handler for eMode2 */
	/* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
	 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
	if (*TWal)(unsafe.Pointer(pWal)).FreadOnly != 0 {
		return int32(SQLITE_READONLY)
	}
	/* Enable blocking locks, if possible. */
	if xBusy2 != 0 {
	}
	/* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
	 ** "checkpoint" lock on the database file.
	 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
	 ** checkpoint operation at the same time, the lock cannot be obtained and
	 ** SQLITE_BUSY is returned.
	 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
	 ** it will not be invoked in this case.
	 */
	if eMode != -int32(1) {
		rc = _walLockExclusive(tls, pWal, int32(WAL_CKPT_LOCK), int32(1))
		if rc == SQLITE_OK {
			(*TWal)(unsafe.Pointer(pWal)).FckptLock = uint8(1)
			/* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART
			 ** and TRUNCATE modes also obtain the exclusive "writer" lock on the
			 ** database file.
			 **
			 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
			 ** immediately, and a busy-handler is configured, it is invoked and the
			 ** writer lock retried until either the busy-handler returns 0 or the
			 ** lock is successfully obtained.
			 */
			if eMode != SQLITE_CHECKPOINT_PASSIVE {
				rc = _walBusyLock(tls, pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, int32(1))
				if rc == SQLITE_OK {
					(*TWal)(unsafe.Pointer(pWal)).FwriteLock = uint8(1)
				} else {
					if rc == int32(SQLITE_BUSY) {
						eMode2 = SQLITE_CHECKPOINT_PASSIVE
						xBusy2 = uintptr(0)
						rc = SQLITE_OK
					}
				}
			}
		}
	} else {
		rc = SQLITE_OK
	}
	/* Read the wal-index header. */
	if rc == SQLITE_OK {
		/* For a passive checkpoint, do not re-enable blocking locks after
		 ** reading the wal-index header. A passive checkpoint should not block
		 ** or invoke the busy handler. The only lock such a checkpoint may
		 ** attempt to obtain is a lock on a read-slot, and it should give up
		 ** immediately and do a partial checkpoint if it cannot obtain it. */
		rc = _walIndexReadHdr(tls, pWal, bp)
		if eMode2 > SQLITE_CHECKPOINT_PASSIVE {
		}
		if **(**int32)(__ccgo_up(bp)) != 0 && (*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FpDbFd)).FpMethods)).FiVersion >= int32(3) {
			_sqlite3OsUnfetch(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, 0, uintptr(0))
		}
	}
	/* Copy data from the log to the database file. */
	if rc == SQLITE_OK {
		_sqlite3FaultSim(tls, int32(660))
		if (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame != 0 && _walPagesize(tls, pWal) != nBuf {
			rc = _sqlite3CorruptError(tls, int32(71912))
		} else {
			if eMode2 != -int32(1) {
				rc = _walCheckpoint(tls, pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf)
			}
		}
		/* If no error occurred, set the output variables. */
		if rc == SQLITE_OK || rc == int32(SQLITE_BUSY) {
			if pnLog != 0 {
				**(**int32)(__ccgo_up(pnLog)) = int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame)
			}
			if pnCkpt != 0 {
				**(**int32)(__ccgo_up(pnCkpt)) = int32((*TWalCkptInfo)(unsafe.Pointer(_walCkptInfo(tls, pWal))).FnBackfill)
			}
		}
	}
	if **(**int32)(__ccgo_up(bp)) != 0 {
		/* If a new wal-index header was loaded before the checkpoint was
		 ** performed, then the pager-cache associated with pWal is now
		 ** out of date. So zero the cached wal-index header to ensure that
		 ** next time the pager opens a snapshot on this database it knows that
		 ** the cache needs to be reset.
		 */
		libc.Xmemset(tls, pWal+52, 0, uint32(48))
	}
	/* Release the locks. */
	_sqlite3WalEndWriteTransaction(tls, pWal)
	if (*TWal)(unsafe.Pointer(pWal)).FckptLock != 0 {
		_walUnlockExclusive(tls, pWal, int32(WAL_CKPT_LOCK), int32(1))
		(*TWal)(unsafe.Pointer(pWal)).FckptLock = uint8(0)
	}
	if rc == SQLITE_OK && eMode != eMode2 {
		v1 = int32(SQLITE_BUSY)
	} else {
		v1 = rc
	}
	return v1
}

// C documentation
//
//	/*
//	** Open a connection to the WAL file zWalName. The database file must
//	** already be opened on connection pDbFd. The buffer that zWalName points
//	** to must remain valid for the lifetime of the returned Wal* handle.
//	**
//	** A SHARED lock should be held on the database file when this function
//	** is called. The purpose of this SHARED lock is to prevent any other
//	** client from unlinking the WAL or wal-index file. If another process
//	** were to do this just after this client opened one of these files, the
//	** system would be badly broken.
//	**
//	** If the log file is successfully opened, SQLITE_OK is returned and
//	** *ppWal is set to point to a new WAL handle. If an error occurs,
//	** an SQLite error code is returned and *ppWal is left unmodified.
//	*/
func _sqlite3WalOpen(tls *libc.TLS, pVfs uintptr, pDbFd uintptr, zWalName uintptr, bNoShm int32, mxWalSize Ti64, ppWal uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iDC, rc, v1 int32
	var pRet uintptr
	var _ /* flags at bp+0 */ int32
	_, _, _, _ = iDC, pRet, rc, v1 /* Flags passed to OsOpen() */
	/* Verify the values of various constants.  Any changes to the values
	 ** of these constants would result in an incompatible on-disk format
	 ** for the -shm file.  Any change that causes one of these asserts to
	 ** fail is a backward compatibility problem, even if the change otherwise
	 ** works.
	 **
	 ** This table also serves as a helpful cross-reference when trying to
	 ** interpret hex dumps of the -shm file.
	 */
	/* In the amalgamation, the os_unix.c and os_win.c source files come before
	 ** this source file.  Verify that the #defines of the locking byte offsets
	 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
	 ** For that matter, if the lock offset ever changes from its initial design
	 ** value of 120, we need to know that so there is an assert() to check it.
	 */
	/* Allocate an instance of struct Wal to return. */
	**(**uintptr)(__ccgo_up(ppWal)) = uintptr(0)
	pRet = _sqlite3MallocZero(tls, uint64(uint32(128)+uint32((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile)))
	if !(pRet != 0) {
		return int32(SQLITE_NOMEM)
	}
	(*TWal)(unsafe.Pointer(pRet)).FpVfs = pVfs
	(*TWal)(unsafe.Pointer(pRet)).FpWalFd = pRet + 1*128
	(*TWal)(unsafe.Pointer(pRet)).FpDbFd = pDbFd
	(*TWal)(unsafe.Pointer(pRet)).FreadLock = int16(-int32(1))
	(*TWal)(unsafe.Pointer(pRet)).FmxWalSize = mxWalSize
	(*TWal)(unsafe.Pointer(pRet)).FzWalName = zWalName
	(*TWal)(unsafe.Pointer(pRet)).FsyncHeader = uint8(1)
	(*TWal)(unsafe.Pointer(pRet)).FpadToSectorBoundary = uint8(1)
	if bNoShm != 0 {
		v1 = int32(WAL_HEAPMEMORY_MODE)
	} else {
		v1 = WAL_NORMAL_MODE
	}
	(*TWal)(unsafe.Pointer(pRet)).FexclusiveMode = uint8(v1)
	/* Open file handle on the write-ahead log file. */
	**(**int32)(__ccgo_up(bp)) = libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_WAL)
	rc = _sqlite3OsOpen(tls, pVfs, zWalName, (*TWal)(unsafe.Pointer(pRet)).FpWalFd, **(**int32)(__ccgo_up(bp)), bp)
	if rc == SQLITE_OK && **(**int32)(__ccgo_up(bp))&int32(SQLITE_OPEN_READONLY) != 0 {
		(*TWal)(unsafe.Pointer(pRet)).FreadOnly = uint8(WAL_RDONLY)
	}
	if rc != SQLITE_OK {
		_walIndexClose(tls, pRet, 0)
		_sqlite3OsClose(tls, (*TWal)(unsafe.Pointer(pRet)).FpWalFd)
		Xsqlite3_free(tls, pRet)
	} else {
		iDC = _sqlite3OsDeviceCharacteristics(tls, pDbFd)
		if iDC&int32(SQLITE_IOCAP_SEQUENTIAL) != 0 {
			(*TWal)(unsafe.Pointer(pRet)).FsyncHeader = uint8(0)
		}
		if iDC&int32(SQLITE_IOCAP_POWERSAFE_OVERWRITE) != 0 {
			(*TWal)(unsafe.Pointer(pRet)).FpadToSectorBoundary = uint8(0)
		}
		**(**uintptr)(__ccgo_up(ppWal)) = pRet
	}
	return rc
}

// C documentation
//
//	/*
//	** Walk an expression tree.  Invoke the callback once for each node
//	** of the expression, while descending.  (In other words, the callback
//	** is invoked before visiting children.)
//	**
//	** The return value from the callback should be one of the WRC_*
//	** constants to specify how to proceed with the walk.
//	**
//	**    WRC_Continue      Continue descending down the tree.
//	**
//	**    WRC_Prune         Do not descend into child nodes, but allow
//	**                      the walk to continue with sibling nodes.
//	**
//	**    WRC_Abort         Do no more callbacks.  Unwind the stack and
//	**                      return from the top-level walk call.
//	**
//	** The return value from this routine is WRC_Abort to abandon the tree walk
//	** and WRC_Continue to continue.
//	*/
func _sqlite3WalkExprNN(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) {
	var rc int32
	_ = rc
	for int32(1) != 0 {
		rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TWalker)(unsafe.Pointer(pWalker)).FxExprCallback})))(tls, pWalker, pExpr)
		if rc != 0 {
			return rc & int32(WRC_Abort)
		}
		if !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)|libc.Int32FromInt32(EP_Leaf)) != libc.Uint32FromInt32(0)) {
			if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 && _sqlite3WalkExprNN(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) != 0 {
				return int32(WRC_Abort)
			}
			if (*TExpr)(unsafe.Pointer(pExpr)).FpRight != 0 {
				pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpRight
				continue
			} else {
				if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
					if _sqlite3WalkSelect(tls, pWalker, *(*uintptr)(unsafe.Pointer(pExpr + 20))) != 0 {
						return int32(WRC_Abort)
					}
				} else {
					if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0 {
						if _sqlite3WalkExprList(tls, pWalker, *(*uintptr)(unsafe.Pointer(pExpr + 20))) != 0 {
							return int32(WRC_Abort)
						}
					}
					if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
						if _walkWindowList(tls, pWalker, *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy)), int32(1)) != 0 {
							return int32(WRC_Abort)
						}
					}
				}
			}
		}
		break
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** This function sets the P4 value of an existing OP_Explain opcode to
//	** text describing the loop in pLevel. If the OP_Explain opcode already has
//	** a P4 value, it is freed before it is overwritten.
//	*/
func _sqlite3WhereAddExplainText(tls *libc.TLS, pParse uintptr, addr int32, pTabList uintptr, pLevel uintptr, wctrlFlags Tu16) {
	bp := tls.Alloc(160)
	defer tls.Free(160)
	var cRangeOp int8
	var db, pIdx, pItem, pLoop, pOp, zFmt, zRowid, v1, v2 uintptr
	var flags Tu32
	var isSearch int32
	var _ /* str at bp+0 */ TStrAccum
	var _ /* zBuf at bp+24 */ [100]int8
	_, _, _, _, _, _, _, _, _, _, _, _ = cRangeOp, db, flags, isSearch, pIdx, pItem, pLoop, pOp, zFmt, zRowid, v1, v2
	if (*TParse)(unsafe.Pointer(pParse)).FpToplevel != 0 {
		v1 = (*TParse)(unsafe.Pointer(pParse)).FpToplevel
	} else {
		v1 = pParse
	}
	if int32((*TParse)(unsafe.Pointer(v1)).Fexplain) == int32(2) || libc.Bool(0 != 0) {
		pOp = _sqlite3VdbeGetOp(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, addr)
		pItem = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
		db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Initial space for EQP output string */
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			return
		}
		pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
		flags = (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags
		isSearch = libc.BoolInt32(flags&uint32(libc.Int32FromInt32(WHERE_BTM_LIMIT)|libc.Int32FromInt32(WHERE_TOP_LIMIT)) != uint32(0) || flags&uint32(WHERE_VIRTUALTABLE) == uint32(0) && int32((*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pLoop + 24))).FnEq) > 0 || int32(wctrlFlags)&(libc.Int32FromInt32(WHERE_ORDERBY_MIN)|libc.Int32FromInt32(WHERE_ORDERBY_MAX)) != 0)
		_sqlite3StrAccumInit(tls, bp, db, bp+24, int32(100), int32(SQLITE_MAX_LENGTH))
		(**(**TStrAccum)(__ccgo_up(bp))).FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL)
		if isSearch != 0 {
			v1 = __ccgo_ts + 24887
		} else {
			v1 = __ccgo_ts + 24894
		}
		if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x40000>>18) != 0 {
			v2 = __ccgo_ts + 24899
		} else {
			v2 = __ccgo_ts + 1711
		}
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+24907, libc.VaList(bp+136, v1, pItem, v2))
		if flags&uint32(libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_VIRTUALTABLE)) == uint32(0) {
			zFmt = uintptr(0)
			pIdx = (*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FpIndex
			if !((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
				if isSearch != 0 {
					zFmt = __ccgo_ts + 12809
				}
			} else {
				if flags&uint32(WHERE_PARTIALIDX) != 0 {
					zFmt = __ccgo_ts + 24915
				} else {
					if flags&uint32(WHERE_AUTO_INDEX) != 0 {
						zFmt = __ccgo_ts + 24948
					} else {
						if flags&uint32(libc.Int32FromInt32(WHERE_IDX_ONLY)|libc.Int32FromInt32(WHERE_EXPRIDX)) != 0 {
							zFmt = __ccgo_ts + 24973
						} else {
							zFmt = __ccgo_ts + 24991
						}
					}
				}
			}
			if zFmt != 0 {
				Xsqlite3_str_append(tls, bp, __ccgo_ts+25000, int32(7))
				Xsqlite3_str_appendf(tls, bp, zFmt, libc.VaList(bp+136, (*TIndex)(unsafe.Pointer(pIdx)).FzName))
				_explainIndexRange(tls, bp, pLoop)
			}
		} else {
			if flags&uint32(WHERE_IPK) != uint32(0) && flags&uint32(WHERE_CONSTRAINT) != uint32(0) {
				zRowid = __ccgo_ts + 19186
				Xsqlite3_str_appendf(tls, bp, __ccgo_ts+25008, libc.VaList(bp+136, zRowid))
				if flags&uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ)|libc.Int32FromInt32(WHERE_COLUMN_IN)) != 0 {
					cRangeOp = int8('=')
				} else {
					if flags&uint32(WHERE_BOTH_LIMIT) == uint32(WHERE_BOTH_LIMIT) {
						Xsqlite3_str_appendf(tls, bp, __ccgo_ts+25039, libc.VaList(bp+136, zRowid))
						cRangeOp = int8('<')
					} else {
						if flags&uint32(WHERE_BTM_LIMIT) != 0 {
							cRangeOp = int8('>')
						} else {
							cRangeOp = int8('<')
						}
					}
				}
				Xsqlite3_str_appendf(tls, bp, __ccgo_ts+25049, libc.VaList(bp+136, int32(cRangeOp)))
			} else {
				if flags&uint32(WHERE_VIRTUALTABLE) != uint32(0) {
					Xsqlite3_str_appendall(tls, bp, __ccgo_ts+25054)
					if int32(Tu32(*(*uint8)(unsafe.Pointer(pLoop + 24 + 4))&0x4>>2)) != 0 {
						v1 = __ccgo_ts + 25076
					} else {
						v1 = __ccgo_ts + 25084
					}
					Xsqlite3_str_appendf(tls, bp, v1, libc.VaList(bp+136, (*(*struct {
						FidxNum    int32
						F__ccgo4   uint8
						FisOrdered Ti8
						FomitMask  Tu16
						FidxStr    uintptr
						FmHandleIn Tu32
					})(unsafe.Pointer(pLoop + 24))).FidxNum, (*(*struct {
						FidxNum    int32
						F__ccgo4   uint8
						FisOrdered Ti8
						FomitMask  Tu16
						FidxStr    uintptr
						FmHandleIn Tu32
					})(unsafe.Pointer(pLoop + 24))).FidxStr))
				}
			}
		}
		if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_LEFT) != 0 {
			Xsqlite3_str_appendf(tls, bp, __ccgo_ts+25090, 0)
		}
		_sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(pOp + 16)))
		(*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(7))
		*(*uintptr)(unsafe.Pointer(pOp + 16)) = _sqlite3StrAccumFinish(tls, bp)
	}
}

// C documentation
//
//	/*
//	** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the
//	** SELECT statement passed as the second argument. These terms are only
//	** added if:
//	**
//	**   1. The SELECT statement has a LIMIT clause, and
//	**   2. The SELECT statement is not an aggregate or DISTINCT query, and
//	**   3. The SELECT statement has exactly one object in its FROM clause, and
//	**      that object is a virtual table, and
//	**   4. There are no terms in the WHERE clause that will not be passed
//	**      to the virtual table xBestIndex method.
//	**   5. The ORDER BY clause, if any, will be made available to the xBestIndex
//	**      method.
//	**
//	** LIMIT and OFFSET terms are ignored by most of the planner code. They
//	** exist only so that they may be passed to the xBestIndex method of the
//	** single virtual table in the FROM clause of the SELECT.
//	*/
func _sqlite3WhereAddLimit(tls *libc.TLS, pWC uintptr, p uintptr) {
	var iCsr, ii int32
	var pExpr, pOrderBy, pParent uintptr
	_, _, _, _, _ = iCsr, ii, pExpr, pOrderBy, pParent
	/* 1 -- checked by caller */
	if (*TSelect)(unsafe.Pointer(p)).FpGroupBy == uintptr(0) && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(libc.Int32FromInt32(SF_Distinct)|libc.Int32FromInt32(SF_Aggregate)) == uint32(0) && ((*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc)).FnSrc == int32(1) && int32((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSTab)).FeTabType) == int32(TABTYP_VTAB)) {
		pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy
		iCsr = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FiCursor
		/* Check condition (4). Return early if it is not met. */
		ii = 0
		for {
			if !(ii < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) {
				break
			}
			if int32((**(**TWhereTerm)(__ccgo_up((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*48))).FwtFlags)&int32(TERM_CODED) != 0 {
				/* This term is a vector operation that has been decomposed into
				 ** other, subsequent terms.  It can be ignored. See tag-20220128a */
				goto _1
			}
			if (**(**TWhereTerm)(__ccgo_up((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*48))).FnChild != 0 {
				/* If this term has child terms, then they are also part of the
				 ** pWC->a[] array. So this term can be ignored, as a LIMIT clause
				 ** will only be added if each of the child terms passes the
				 ** (leftCursor==iCsr) test below.  */
				goto _1
			}
			if (**(**TWhereTerm)(__ccgo_up((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*48))).FleftCursor == iCsr && (**(**TWhereTerm)(__ccgo_up((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*48))).FprereqRight == uint64(0) {
				goto _1
			}
			/* If this term has a parent with exactly one child, and the parent will
			 ** be passed through to xBestIndex, then this term can be ignored.  */
			if (**(**TWhereTerm)(__ccgo_up((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*48))).FiParent >= 0 {
				pParent = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((**(**TWhereTerm)(__ccgo_up((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*48))).FiParent)*48
				if (*TWhereTerm)(unsafe.Pointer(pParent)).FleftCursor == iCsr && (*TWhereTerm)(unsafe.Pointer(pParent)).FprereqRight == uint64(0) && int32((*TWhereTerm)(unsafe.Pointer(pParent)).FnChild) == int32(1) {
					goto _1
				}
			}
			/* This term will not be passed through. Do not add a LIMIT clause. */
			return
			goto _1
		_1:
			;
			ii = ii + 1
		}
		/* Check condition (5). Return early if it is not met. */
		if pOrderBy != 0 {
			ii = 0
			for {
				if !(ii < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) {
					break
				}
				pExpr = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(ii)*20))).FpExpr
				if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLUMN) {
					return
				}
				if (*TExpr)(unsafe.Pointer(pExpr)).FiTable != iCsr {
					return
				}
				if int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(ii)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 {
					return
				}
				goto _2
			_2:
				;
				ii = ii + 1
			}
		}
		/* All conditions are met. Add the terms to the where-clause object. */
		if (*TSelect)(unsafe.Pointer(p)).FiOffset != 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Compound) == uint32(0) {
			_whereAddLimitExpr(tls, pWC, (*TSelect)(unsafe.Pointer(p)).FiOffset, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpLimit)).FpRight, iCsr, int32(SQLITE_INDEX_CONSTRAINT_OFFSET))
		}
		if (*TSelect)(unsafe.Pointer(p)).FiOffset == 0 || (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Compound) == uint32(0) {
			_whereAddLimitExpr(tls, pWC, (*TSelect)(unsafe.Pointer(p)).FiLimit, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpLimit)).FpLeft, iCsr, int32(SQLITE_INDEX_CONSTRAINT_LIMIT))
		}
	}
}

// C documentation
//
//	/*
//	** Generate the beginning of the loop used for WHERE clause processing.
//	** The return value is a pointer to an opaque structure that contains
//	** information needed to terminate the loop.  Later, the calling routine
//	** should invoke sqlite3WhereEnd() with the return value of this function
//	** in order to complete the WHERE clause processing.
//	**
//	** If an error occurs, this routine returns NULL.
//	**
//	** The basic idea is to do a nested loop, one loop for each table in
//	** the FROM clause of a select.  (INSERT and UPDATE statements are the
//	** same as a SELECT with only a single table in the FROM clause.)  For
//	** example, if the SQL is this:
//	**
//	**       SELECT * FROM t1, t2, t3 WHERE ...;
//	**
//	** Then the code generated is conceptually like the following:
//	**
//	**      foreach row1 in t1 do       \    Code generated
//	**        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
//	**          foreach row3 in t3 do   /
//	**            ...
//	**          end                     \    Code generated
//	**        end                        |-- by sqlite3WhereEnd()
//	**      end                         /
//	**
//	** Note that the loops might not be nested in the order in which they
//	** appear in the FROM clause if a different order is better able to make
//	** use of indices.  Note also that when the IN operator appears in
//	** the WHERE clause, it might result in additional nested loops for
//	** scanning through all values on the right-hand side of the IN.
//	**
//	** There are Btree cursors associated with each table.  t1 uses cursor
//	** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
//	** And so forth.  This routine generates code to open those VDBE cursors
//	** and sqlite3WhereEnd() generates the code to close them.
//	**
//	** The code that sqlite3WhereBegin() generates leaves the cursors named
//	** in pTabList pointing at their appropriate entries.  The [...] code
//	** can use OP_Column and OP_Rowid opcodes on these cursors to extract
//	** data from the various tables of the loop.
//	**
//	** If the WHERE clause is empty, the foreach loops must each scan their
//	** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
//	** the tables have indices and there are terms in the WHERE clause that
//	** refer to those indices, a complete table scan can be avoided and the
//	** code will run much faster.  Most of the work of this routine is checking
//	** to see if there are indices that can be used to speed up the loop.
//	**
//	** Terms of the WHERE clause are also used to limit which rows actually
//	** make it to the "..." in the middle of the loop.  After each "foreach",
//	** terms of the WHERE clause that use only terms in that loop and outer
//	** loops are evaluated and if false a jump is made around all subsequent
//	** inner loops (or around the "..." if the test occurs within the inner-
//	** most loop)
//	**
//	** OUTER JOINS
//	**
//	** An outer join of tables t1 and t2 is conceptually coded as follows:
//	**
//	**    foreach row1 in t1 do
//	**      flag = 0
//	**      foreach row2 in t2 do
//	**        start:
//	**          ...
//	**          flag = 1
//	**      end
//	**      if flag==0 then
//	**        move the row2 cursor to a null row
//	**        goto start
//	**      fi
//	**    end
//	**
//	** ORDER BY CLAUSE PROCESSING
//	**
//	** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
//	** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
//	** if there is one.  If there is no ORDER BY clause or if this routine
//	** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
//	**
//	** The iIdxCur parameter is the cursor number of an index.  If
//	** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
//	** to use for OR clause processing.  The WHERE clause should use this
//	** specific cursor.  If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
//	** the first cursor in an array of cursors for all indices.  iIdxCur should
//	** be used to compute the appropriate cursor depending on which index is
//	** used.
//	*/
func _sqlite3WhereBegin(tls *libc.TLS, pParse uintptr, pTabList uintptr, pWhere uintptr, pOrderBy uintptr, pResultSet uintptr, pSelect uintptr, wctrlFlags Tu16, iAuxArg int32) (r uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var addrExplain, bOnerow, iCur, iDb, iIndexCur, iOnce, ii, n, nByteWInfo, nTabList, op, op1, rc, wsFlags, wsFlags1, v1 int32
	var b, notReady TBitmask
	var bFordelete Tu8
	var db, p, pInfo, pIx, pJ, pLevel, pLoop, pMaskSet, pPk, pRJ, pSrc, pSubq, pT, pTab, pTabItem, pVTab, pWInfo, pX, v, v7, v8 uintptr
	var v19 bool
	var _ /* sWLB at bp+0 */ TWhereLoopBuilder
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrExplain, b, bFordelete, bOnerow, db, iCur, iDb, iIndexCur, iOnce, ii, n, nByteWInfo, nTabList, notReady, op, op1, p, pInfo, pIx, pJ, pLevel, pLoop, pMaskSet, pPk, pRJ, pSrc, pSubq, pT, pTab, pTabItem, pVTab, pWInfo, pX, rc, v, wsFlags, wsFlags1, v1, v19, v7, v8 /* Will become the return value of this function */
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe                                                                                                                                                                                                                                                                                                                                                          /* Return code */
	bFordelete = uint8(0)                                                                                                                                                                                                                                                                                                                                                                                 /* OPFLAG_FORDELETE or zero, as appropriate */
	/* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
	/* Variable initialization */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	libc.Xmemset(tls, bp, 0, uint32(32))
	/* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
	if pOrderBy != 0 && (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
		pOrderBy = uintptr(0)
		wctrlFlags = uint16(int32(wctrlFlags) & ^libc.Int32FromInt32(WHERE_WANT_DISTINCT))
		wctrlFlags = uint16(int32(wctrlFlags) | libc.Int32FromInt32(WHERE_KEEP_ALL_JOINS)) /* Disable omit-noop-join opt */
	}
	/* The number of tables in the FROM clause is limited by the number of
	 ** bits in a Bitmask
	 */
	if (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc > int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25334, libc.VaList(bp+40, int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))))
		return uintptr(0)
	}
	/* This function normally generates a nested loop for all tables in
	 ** pTabList.  But if the WHERE_OR_SUBCLAUSE flag is set, then we should
	 ** only generate code for the first table in pTabList and assume that
	 ** any cursors associated with subsequent tables are uninitialized.
	 */
	if int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 {
		v1 = int32(1)
	} else {
		v1 = (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc
	}
	nTabList = v1
	/* Allocate and initialize the WhereInfo structure that will become the
	 ** return value. A single allocation is used to store the WhereInfo
	 ** struct, the contents of WhereInfo.a[], the WhereClause structure
	 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
	 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
	 ** some architectures. Hence the ROUND8() below.
	 */
	nByteWInfo = int32((uint32(libc.UintptrFromInt32(0)+760) + uint32(nTabList)*libc.Uint32FromInt64(88) + libc.Uint32FromInt32(7)) & uint32(^libc.Int32FromInt32(7)))
	pWInfo = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(nByteWInfo)+uint32(72)))
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_sqlite3DbFree(tls, db, pWInfo)
		pWInfo = uintptr(0)
		goto whereBeginError
	}
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse = pParse
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList = pTabList
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy = pOrderBy
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FpResultSet = pResultSet
	v1 = -libc.Int32FromInt32(1)
	**(**int32)(__ccgo_up(pWInfo + 20 + 1*4)) = v1
	**(**int32)(__ccgo_up(pWInfo + 20)) = v1
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel = uint8(nTabList)
	v1 = _sqlite3VdbeMakeLabel(tls, pParse)
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FiContinue = v1
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak = v1
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags = wctrlFlags
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FiLimit = int16(iAuxArg)
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FsavedNQueryLoop = int32((*TParse)(unsafe.Pointer(pParse)).FnQueryLoop)
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect = pSelect
	libc.Xmemset(tls, pWInfo+45, 0, uint32(libc.UintptrFromInt32(0)+80)-uint32(libc.UintptrFromInt32(0)+45))
	libc.Xmemset(tls, pWInfo+760, 0, uint32(72)+uint32(nTabList)*uint32(88))
	/* ONEPASS defaults to OFF */
	pMaskSet = pWInfo + 496
	(*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).Fn = 0
	**(**int32)(__ccgo_up(pMaskSet + 8)) = -int32(99) /* Initialize ix[0] to a value that can never be
	 ** a valid cursor number, to avoid an initial
	 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
	(**(**TWhereLoopBuilder)(__ccgo_up(bp))).FpWInfo = pWInfo
	(**(**TWhereLoopBuilder)(__ccgo_up(bp))).FpWC = pWInfo + 80
	(**(**TWhereLoopBuilder)(__ccgo_up(bp))).FpNew = pWInfo + uintptr(nByteWInfo)
	_whereLoopInit(tls, (**(**TWhereLoopBuilder)(__ccgo_up(bp))).FpNew)
	/* Split the WHERE clause into separate subexpressions where each
	 ** subexpression is separated by an AND operator.
	 */
	_sqlite3WhereClauseInit(tls, pWInfo+80, pWInfo)
	_sqlite3WhereSplit(tls, pWInfo+80, pWhere, uint8(TK_AND))
	/* Special case: No FROM clause
	 */
	if nTabList == 0 {
		if pOrderBy != 0 {
			(*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = int8((*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr)
		}
		if int32(wctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_DistinctOpt)) == uint32(0) {
			(*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_UNIQUE)
		}
		if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect != 0 && (*TSelect)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect)).FselFlags&uint32(SF_MultiValue) == uint32(0) {
			_sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+25362, 0)
		}
	} else {
		/* Assign a bit from the bitmask to every term in the FROM clause.
		 **
		 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
		 **
		 ** The rule of the previous sentence ensures that if X is the bitmask for
		 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
		 ** Knowing the bitmask for all tables to the left of a left join is
		 ** important.  Ticket #3015.
		 **
		 ** Note that bitmasks are created for all pTabList->nSrc tables in
		 ** pTabList, not just the first nTabList tables.  nTabList is normally
		 ** equal to pTabList->nSrc but might be shortened to 1 if the
		 ** WHERE_OR_SUBCLAUSE flag is set.
		 */
		ii = 0
		for {
			_createMask(tls, pMaskSet, (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(ii)*48))).FiCursor)
			_sqlite3WhereTabFuncArgs(tls, pParse, pTabList+8+uintptr(ii)*48, pWInfo+80)
			goto _5
		_5:
			;
			ii = ii + 1
			v1 = ii
			if !(v1 < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) {
				break
			}
		}
	}
	/* Analyze all of the subexpressions. */
	_sqlite3WhereExprAnalyze(tls, pTabList, pWInfo+80)
	if pSelect != 0 && (*TSelect)(unsafe.Pointer(pSelect)).FpLimit != 0 {
		_sqlite3WhereAddLimit(tls, pWInfo+80, pSelect)
	}
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		goto whereBeginError
	}
	/* The False-WHERE-Term-Bypass optimization:
	 **
	 ** If there are WHERE terms that are false, then no rows will be output,
	 ** so skip over all of the code generated here.
	 **
	 ** Conditions:
	 **
	 **   (1)  The WHERE term must not refer to any tables in the join.
	 **   (2)  The term must not come from an ON clause on the
	 **        right-hand side of a LEFT or FULL JOIN.
	 **   (3)  The term must not come from an ON clause, or there must be
	 **        no RIGHT or FULL OUTER joins in pTabList.
	 **   (4)  If the expression contains non-deterministic functions
	 **        that are not within a sub-select. This is not required
	 **        for correctness but rather to preserves SQLite's legacy
	 **        behaviour in the following two cases:
	 **
	 **          WHERE random()>0;           -- eval random() once per row
	 **          WHERE (SELECT random())>0;  -- eval random() just once overall
	 **
	 ** Note that the Where term need not be a constant in order for this
	 ** optimization to apply, though it does need to be constant relative to
	 ** the current subquery (condition 1).  The term might include variables
	 ** from outer queries so that the value of the term changes from one
	 ** invocation of the current subquery to the next.
	 */
	ii = 0
	for {
		if !(ii < (*TWhereClause)(unsafe.Pointer((**(**TWhereLoopBuilder)(__ccgo_up(bp))).FpWC)).FnBase) {
			break
		}
		pT = (*TWhereClause)(unsafe.Pointer((**(**TWhereLoopBuilder)(__ccgo_up(bp))).FpWC)).Fa + uintptr(ii)*48 /* The expression of pT */
		if int32((*TWhereTerm)(unsafe.Pointer(pT)).FwtFlags)&int32(TERM_VIRTUAL) != 0 {
			goto _6
		}
		pX = (*TWhereTerm)(unsafe.Pointer(pT)).FpExpr
		if (*TWhereTerm)(unsafe.Pointer(pT)).FprereqAll == uint64(0) && (nTabList == 0 || _exprIsDeterministic(tls, pX) != 0) && !((*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)) != uint32(0) && int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0) {
			_sqlite3ExprIfFalse(tls, pParse, pX, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak, int32(SQLITE_JUMPIFNULL))
			v7 = pT + 10
			*(*Tu16)(unsafe.Pointer(v7)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v7))) | libc.Int32FromInt32(TERM_CODED))
		}
		goto _6
	_6:
		;
		ii = ii + 1
	}
	if int32(wctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 {
		if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_DistinctOpt)) != uint32(0) {
			/* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
			 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
			wctrlFlags = uint16(int32(wctrlFlags) & ^libc.Int32FromInt32(WHERE_WANT_DISTINCT))
			v7 = pWInfo + 40
			*(*Tu16)(unsafe.Pointer(v7)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v7))) & ^libc.Int32FromInt32(WHERE_WANT_DISTINCT))
		} else {
			if _isDistinctRedundant(tls, pParse, pTabList, pWInfo+80, pResultSet) != 0 {
				/* The DISTINCT marking is pointless.  Ignore it. */
				(*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_UNIQUE)
			} else {
				if pOrderBy == uintptr(0) {
					/* Try to ORDER BY the result set to make distinct processing easier */
					v7 = pWInfo + 40
					*(*Tu16)(unsafe.Pointer(v7)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v7))) | libc.Int32FromInt32(WHERE_DISTINCTBY))
					(*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy = pResultSet
				}
			}
		}
	}
	/* Construct the WhereLoop objects */
	if nTabList != int32(1) || _whereShortCut(tls, bp) == 0 {
		rc = _whereLoopAddAll(tls, bp)
		if rc != 0 {
			goto whereBeginError
		}
		/* If one or more WhereTerm.truthProb values were used in estimating
		 ** loop parameters, but then those truthProb values were subsequently
		 ** changed based on STAT4 information while computing subsequent loops,
		 ** then we need to rerun the whole loop building process so that all
		 ** loops will be built using the revised truthProb values. */
		if int32((**(**TWhereLoopBuilder)(__ccgo_up(bp))).FbldFlags2)&int32(SQLITE_BLDF2_2NDPASS) != 0 {
			for (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops != 0 {
				p = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops
				(*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops = (*TWhereLoop)(unsafe.Pointer(p)).FpNextLoop
				_whereLoopDelete(tls, db, p)
			}
			rc = _whereLoopAddAll(tls, bp)
			if rc != 0 {
				goto whereBeginError
			}
		}
		_wherePathSolver(tls, pWInfo, 0)
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			goto whereBeginError
		}
		if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy != 0 {
			_whereInterstageHeuristic(tls, pWInfo)
			if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut) < 0 {
				v1 = int32(1)
			} else {
				v1 = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut) + int32(1)
			}
			_wherePathSolver(tls, pWInfo, int16(v1))
			if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				goto whereBeginError
			}
		}
		/* TUNING:  Assume that a DISTINCT clause on a subquery reduces
		 ** the output size by a factor of 8 (LogEst -30).  Search for
		 ** tag-20250414a to see other cases.
		 */
		if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 {
			v7 = pWInfo + 50
			*(*TLogEst)(unsafe.Pointer(v7)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v7))) - libc.Int32FromInt32(30))
		}
	}
	if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy == uintptr(0) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ReverseOrder) != uint64(0) {
		_whereReverseScanOrder(tls, pWInfo)
	}
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		goto whereBeginError
	}
	/* Attempt to omit tables from a join that do not affect the result.
	 ** See the comment on whereOmitNoopJoin() for further information.
	 **
	 ** This query optimization is factored out into a separate "no-inline"
	 ** procedure to keep the sqlite3WhereBegin() procedure from becoming
	 ** too large.  If sqlite3WhereBegin() becomes too large, that prevents
	 ** some C-compiler optimizers from in-lining the
	 ** sqlite3WhereCodeOneLoopStart() procedure, and it is important to
	 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
	 */
	notReady = ^libc.Uint64FromInt32(0)
	if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) >= int32(2) && pResultSet != uintptr(0) && 0 == int32(wctrlFlags)&(libc.Int32FromInt32(WHERE_AGG_DISTINCT)|libc.Int32FromInt32(WHERE_KEEP_ALL_JOINS)) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_OmitNoopJoin)) == uint32(0) {
		notReady = _whereOmitNoopJoin(tls, pWInfo, notReady)
		nTabList = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)
	}
	/* Check to see if there are any SEARCH loops that might benefit from
	 ** using a Bloom filter.
	 */
	if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) >= int32(2) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_BloomFilter)) == uint32(0) {
		_whereCheckIfBloomFilterIsUseful(tls, pWInfo)
	}
	v7 = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse + 16
	*(*TLogEst)(unsafe.Pointer(v7)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v7))) + int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut))
	/* If the caller is an UPDATE or DELETE statement that is requesting
	 ** to use a one-pass algorithm, determine if this is appropriate.
	 **
	 ** A one-pass approach can be used if the caller has requested one
	 ** and either (a) the scan visits at most one row or (b) each
	 ** of the following are true:
	 **
	 **   * the caller has indicated that a one-pass approach can be used
	 **     with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
	 **   * the table is not a virtual table, and
	 **   * either the scan does not use the OR optimization or the caller
	 **     is a DELETE operation (WHERE_DUPLICATES_OK is only specified
	 **     for DELETE).
	 **
	 ** The last qualification is because an UPDATE statement uses
	 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
	 ** use a one-pass approach, and this is not set accurately for scans
	 ** that use the OR optimization.
	 */
	if int32(wctrlFlags)&int32(WHERE_ONEPASS_DESIRED) != 0 {
		wsFlags = int32((*TWhereLoop)(unsafe.Pointer((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760))).FpWLoop)).FwsFlags)
		bOnerow = libc.BoolInt32(wsFlags&int32(WHERE_ONEROW) != 0)
		if bOnerow != 0 || 0 != int32(wctrlFlags)&int32(WHERE_ONEPASS_MULTIROW) && !(int32((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpSTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) && (0 == wsFlags&int32(WHERE_MULTI_OR) || int32(wctrlFlags)&int32(WHERE_DUPLICATES_OK) != 0) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_OnePass)) == uint32(0) {
			if bOnerow != 0 {
				v1 = int32(ONEPASS_SINGLE)
			} else {
				v1 = int32(ONEPASS_MULTI)
			}
			(*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass = uint8(v1)
			if (*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpSTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) && wsFlags&int32(WHERE_IDX_ONLY) != 0 {
				if int32(wctrlFlags)&int32(WHERE_ONEPASS_MULTIROW) != 0 {
					bFordelete = uint8(OPFLAG_FORDELETE)
				}
				(*TWhereLoop)(unsafe.Pointer((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760))).FpWLoop)).FwsFlags = uint32(wsFlags & ^libc.Int32FromInt32(WHERE_IDX_ONLY))
			}
		}
	}
	/* Open all tables in the pTabList and any indices selected for
	 ** searching those tables.
	 */
	ii = 0
	pLevel = pWInfo + 760
	for {
		if !(ii < nTabList) {
			break
		}
		pTabItem = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
		pTab = (*TSrcItem)(unsafe.Pointer(pTabItem)).FpSTab
		iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
		pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk = _sqlite3VdbeMakeLabel(tls, pParse)
		if ii == 0 || int32((**(**TSrcItem)(__ccgo_up(pTabItem))).Ffg.Fjointype)&int32(JT_LEFT) != 0 {
			(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrHalt = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk
		} else {
			if (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(ii-int32(1))*88))).FpRJ != 0 {
				(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrHalt = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(ii-int32(1))*88))).FaddrBrk
			} else {
				(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrHalt = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(ii-int32(1))*88))).FaddrHalt
			}
		}
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Ephemeral) != uint32(0) || int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
			/* Do nothing */
		} else {
			if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != uint32(0) {
				pVTab = _sqlite3GetVTable(tls, db, pTab)
				iCur = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor
				_sqlite3VdbeAddOp4(tls, v, int32(OP_VOpen), iCur, 0, 0, pVTab, -int32(12))
			} else {
				if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
					/* noop */
				} else {
					if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IDX_ONLY) == uint32(0) && int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) == 0 || int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 {
						op = int32(OP_OpenRead)
						if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) != ONEPASS_OFF {
							op = int32(OP_OpenWrite)
							**(**int32)(__ccgo_up(pWInfo + 20)) = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor
						}
						_sqlite3OpenTable(tls, pParse, (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor, iDb, pTab, op)
						if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) == ONEPASS_OFF && int32((*TTable)(unsafe.Pointer(pTab)).FnCol) < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(libc.Int32FromInt32(TF_HasGenerated)|libc.Int32FromInt32(TF_WithoutRowid)) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_AUTO_INDEX)|libc.Int32FromInt32(WHERE_BLOOMFILTER)) == uint32(0) {
							/* If we know that only a prefix of the record will be used,
							 ** it is advantageous to reduce the "column count" field in
							 ** the P4 operand of the OP_OpenRead/Write opcode. */
							b = (*TSrcItem)(unsafe.Pointer(pTabItem)).FcolUsed
							n = 0
							for {
								if !(b != 0) {
									break
								}
								goto _15
							_15:
								;
								b = b >> int32(1)
								n = n + 1
							}
							_sqlite3VdbeChangeP4(tls, v, -int32(1), uintptr(n), -int32(3))
						}
						_sqlite3VdbeChangeP5(tls, v, uint16(bFordelete))
						if ii >= int32(2) && int32((**(**TSrcItem)(__ccgo_up(pTabItem))).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_LEFT)) == 0 && (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrHalt == (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760))).FaddrHalt {
							_sqlite3VdbeAddOp2(tls, v, int32(OP_IfEmpty), (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak)
						}
					} else {
						_sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab)).FzName)
					}
				}
			}
		}
		if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_INDEXED) != 0 {
			pIx = (*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FpIndex
			op1 = int32(OP_OpenRead)
			/* iAuxArg is always set to a positive value if ONEPASS is possible */
			if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) && int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 {
				/* This is one term of an OR-optimization using the PRIMARY KEY of a
				 ** WITHOUT ROWID table.  No need for a separate index */
				iIndexCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur
				op1 = 0
			} else {
				if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) != ONEPASS_OFF {
					pJ = (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pTabItem)).FpSTab)).FpIndex
					iIndexCur = iAuxArg
					for pJ != 0 && pJ != pIx {
						iIndexCur = iIndexCur + 1
						pJ = (*TIndex)(unsafe.Pointer(pJ)).FpNext
					}
					op1 = int32(OP_OpenWrite)
					**(**int32)(__ccgo_up(pWInfo + 20 + 1*4)) = iIndexCur
				} else {
					if iAuxArg != 0 && int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 {
						iIndexCur = iAuxArg
						op1 = int32(OP_ReopenIdx)
					} else {
						v7 = pParse + 44
						v1 = *(*int32)(unsafe.Pointer(v7))
						*(*int32)(unsafe.Pointer(v7)) = *(*int32)(unsafe.Pointer(v7)) + 1
						iIndexCur = v1
						if int32(uint32(*(*uint16)(unsafe.Pointer(pIx + 56))&0x800>>11)) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_IndexedExpr)) == uint32(0) {
							_whereAddIndexedExpr(tls, pParse, pIx, iIndexCur, pTabItem)
						}
						if (*TIndex)(unsafe.Pointer(pIx)).FpPartIdxWhere != 0 && int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_RIGHT) == 0 {
							_wherePartIdxExpr(tls, pParse, pIx, (*TIndex)(unsafe.Pointer(pIx)).FpPartIdxWhere, uintptr(0), iIndexCur, pTabItem)
						}
					}
				}
			}
			(*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur = iIndexCur
			if op1 != 0 {
				_sqlite3VdbeAddOp3(tls, v, op1, iIndexCur, int32((*TIndex)(unsafe.Pointer(pIx)).Ftnum), iDb)
				_sqlite3VdbeSetP4KeyInfo(tls, pParse, pIx)
				if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_CONSTRAINT) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_COLUMN_RANGE)|libc.Int32FromInt32(WHERE_SKIPSCAN)) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_BIGNULL_SORT) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_SEEKSCAN) == uint32(0) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_ORDERBY_MIN) == 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct) != int32(WHERE_DISTINCT_ORDERED) {
					_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_SEEKEQ))
				}
			}
		}
		if iDb >= 0 {
			_sqlite3CodeVerifySchema(tls, pParse, iDb)
		}
		if v19 = int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0; v19 {
			v7 = _sqlite3WhereMalloc(tls, pWInfo, uint64(20))
			(*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ = v7
		}
		if v19 && v7 != uintptr(0) {
			pRJ = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ
			v8 = pParse + 44
			v1 = *(*int32)(unsafe.Pointer(v8))
			*(*int32)(unsafe.Pointer(v8)) = *(*int32)(unsafe.Pointer(v8)) + 1
			(*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch = v1
			v7 = pParse + 48
			*(*int32)(unsafe.Pointer(v7)) = *(*int32)(unsafe.Pointer(v7)) + 1
			v1 = *(*int32)(unsafe.Pointer(v7))
			(*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregBloom = v1
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Blob), int32(65536), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregBloom)
			v7 = pParse + 48
			*(*int32)(unsafe.Pointer(v7)) = *(*int32)(unsafe.Pointer(v7)) + 1
			v1 = *(*int32)(unsafe.Pointer(v7))
			(*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregReturn = v1
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregReturn)
			if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, int32(1))
				pInfo = _sqlite3KeyInfoAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(1), 0)
				if pInfo != 0 {
					*(*uintptr)(unsafe.Pointer(pInfo + 20)) = uintptr(0)
					**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pInfo)).FaSortFlags)) = uint8(0)
					_sqlite3VdbeAppendP4(tls, v, pInfo, -int32(9))
				}
			} else {
				pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol))
				_sqlite3VdbeSetP4KeyInfo(tls, pParse, pPk)
			}
			**(**Tu32)(__ccgo_up(pLoop + 40)) &= uint32(^libc.Int32FromInt32(WHERE_IDX_ONLY))
			/* The nature of RIGHT JOIN processing is such that it messes up
			 ** the output order.  So omit any ORDER BY/GROUP BY elimination
			 ** optimizations.  We need to do an actual sort for RIGHT JOIN. */
			(*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = 0
			(*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_UNORDERED)
		}
		goto _14
	_14:
		;
		ii = ii + 1
		pLevel += 88
	}
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FiTop = _sqlite3VdbeCurrentAddr(tls, v)
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		goto whereBeginError
	}
	/* Generate the code to do the search.  Each iteration of the for
	 ** loop below generates code for a single nested loop of the VM
	 ** program.
	 */
	ii = 0
	for {
		if !(ii < nTabList) {
			break
		}
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			goto whereBeginError
		}
		pLevel = pWInfo + 760 + uintptr(ii)*88
		wsFlags1 = int32((*TWhereLoop)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop)).FwsFlags)
		pSrc = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
		if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x20>>5) != 0 {
			iOnce = 0
			pSubq = *(*uintptr)(unsafe.Pointer(pSrc + 44))
			if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x10>>4) == 0 {
				iOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once))
			} else {
				iOnce = 0
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn, (*TSubquery)(unsafe.Pointer(pSubq)).FaddrFillSub)
			if iOnce != 0 {
				_sqlite3VdbeJumpHere(tls, v, iOnce)
			}
		}
		if wsFlags1&(libc.Int32FromInt32(WHERE_AUTO_INDEX)|libc.Int32FromInt32(WHERE_BLOOMFILTER)) != 0 {
			if wsFlags1&int32(WHERE_AUTO_INDEX) != 0 {
				_constructAutomaticIndex(tls, pParse, pWInfo+80, notReady, pLevel)
			} else {
				_sqlite3ConstructBloomFilter(tls, pWInfo, ii, pLevel, notReady)
			}
			if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				goto whereBeginError
			}
		}
		addrExplain = _sqlite3WhereExplainOneScan(tls, pParse, pTabList, pLevel, wctrlFlags)
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBody = _sqlite3VdbeCurrentAddr(tls, v)
		notReady = _sqlite3WhereCodeOneLoopStart(tls, pParse, v, pWInfo, ii, pLevel, notReady)
		(*TWhereInfo)(unsafe.Pointer(pWInfo)).FiContinue = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont
		if wsFlags1&int32(WHERE_MULTI_OR) == 0 && int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) == 0 {
			_ = addrExplain
		}
		goto _26
	_26:
		;
		ii = ii + 1
	}
	/* Done. */
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FiEndWhere = _sqlite3VdbeCurrentAddr(tls, v)
	return pWInfo
	/* Jump here if malloc fails */
	goto whereBeginError
whereBeginError:
	;
	if pWInfo != 0 {
		(*TParse)(unsafe.Pointer(pParse)).FnQueryLoop = int16((*TWhereInfo)(unsafe.Pointer(pWInfo)).FsavedNQueryLoop)
		_whereInfoFree(tls, db, pWInfo)
	}
	return uintptr(0)
}

/*
** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
** index rather than the main table.  In SQLITE_DEBUG mode, we want
** to trace those changes if PRAGMA vdbe_addoptrace=on.  This routine
** does that.
 */

// C documentation
//
//	/*
//	** Deallocate a WhereClause structure.  The WhereClause structure
//	** itself is not freed.  This routine is the inverse of
//	** sqlite3WhereClauseInit().
//	*/
func _sqlite3WhereClauseClear(tls *libc.TLS, pWC uintptr) {
	var a, aLast, db uintptr
	_, _, _ = a, aLast, db
	db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse)).Fdb
	if (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm > 0 {
		a = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
		aLast = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm-int32(1))*48
		for int32(1) != 0 {
			if int32((*TWhereTerm)(unsafe.Pointer(a)).FwtFlags)&int32(TERM_DYNAMIC) != 0 {
				_sqlite3ExprDelete(tls, db, (*TWhereTerm)(unsafe.Pointer(a)).FpExpr)
			}
			if int32((*TWhereTerm)(unsafe.Pointer(a)).FwtFlags)&(libc.Int32FromInt32(TERM_ORINFO)|libc.Int32FromInt32(TERM_ANDINFO)) != 0 {
				if int32((*TWhereTerm)(unsafe.Pointer(a)).FwtFlags)&int32(TERM_ORINFO) != 0 {
					_whereOrInfoDelete(tls, db, *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(a)).Fu)))
				} else {
					_whereAndInfoDelete(tls, db, *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(a)).Fu)))
				}
			}
			if a == aLast {
				break
			}
			a += 48
		}
	}
}

// C documentation
//
//	/*
//	** Initialize a preallocated WhereClause structure.
//	*/
func _sqlite3WhereClauseInit(tls *libc.TLS, pWC uintptr, pWInfo uintptr) {
	(*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo = pWInfo
	(*TWhereClause)(unsafe.Pointer(pWC)).FhasOr = uint8(0)
	(*TWhereClause)(unsafe.Pointer(pWC)).FpOuter = uintptr(0)
	(*TWhereClause)(unsafe.Pointer(pWC)).FnTerm = 0
	(*TWhereClause)(unsafe.Pointer(pWC)).FnBase = 0
	(*TWhereClause)(unsafe.Pointer(pWC)).FnSlot = int32(libc.Uint32FromInt64(384) / libc.Uint32FromInt64(48))
	(*TWhereClause)(unsafe.Pointer(pWC)).Fa = pWC + 32
}

// C documentation
//
//	/*
//	** Generate code for the start of the iLevel-th loop in the WHERE clause
//	** implementation described by pWInfo.
//	*/
func _sqlite3WhereCodeOneLoopStart(tls *libc.TLS, pParse uintptr, v uintptr, pWInfo uintptr, iLevel int32, pLevel uintptr, notReady TBitmask) (r2 TBitmask) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var aMoveOp [4]Tu8
	var addrBrk, addrCont, addrExplain, addrNotFound, addrNxt, addrSeekScan, bRev, endEq, iCache, iCol, iCol1, iCovCur, iCur, iFld, iIdxCur, iIn, iLoop, iLoopBody, iNext, iPk, iPk1, iReg, iReleaseReg, iRetInit, iRowidReg, iSet, iTab, iTarget, iTerm, ii, j, jmp1, jmp11, k, memEndValue, nConstraint, nConstraint1, nExtraReg, nNotReady, nPk, nPk1, omitTable, op, op1, r, r1, r11, regBase, regBignull, regReturn, regRowid, regRowset, regYield, skipLikeAddr, start, startEq, start_constraints, testOp, untestedTerms, v1, v2 int32
	var bSeekPastNull, bStopAtNull, t1, t2 Tu8
	var db, origSrc, pAlt, pAndExpr, pCompare, pCov, pDelete, pE, pE1, pEnd, pExpr, pIdx, pLeft, pLoop, pOp, pOrExpr, pOrTab, pOrTerm, pOrWc, pPk, pPk1, pPk2, pPk3, pRJ, pRJ1, pRangeEnd, pRangeStart, pRight, pRight1, pRight2, pRight3, pStart, pSubLoop, pSubWInfo, pSubq, pTab, pTab1, pTabItem, pTerm, pWC, pX, pX1, t, zEndAff, v4, v8 uintptr
	var m TBitmask
	var nBtm, nEq, nTop Tu16
	var v6 uint32
	var v15 bool
	var _ /* rTemp at bp+0 */ int32
	var _ /* sEAlt at bp+8 */ TExpr
	var _ /* zStartAff at bp+4 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aMoveOp, addrBrk, addrCont, addrExplain, addrNotFound, addrNxt, addrSeekScan, bRev, bSeekPastNull, bStopAtNull, db, endEq, iCache, iCol, iCol1, iCovCur, iCur, iFld, iIdxCur, iIn, iLoop, iLoopBody, iNext, iPk, iPk1, iReg, iReleaseReg, iRetInit, iRowidReg, iSet, iTab, iTarget, iTerm, ii, j, jmp1, jmp11, k, m, memEndValue, nBtm, nConstraint, nConstraint1, nEq, nExtraReg, nNotReady, nPk, nPk1, nTop, omitTable, op, op1, origSrc, pAlt, pAndExpr, pCompare, pCov, pDelete, pE, pE1, pEnd, pExpr, pIdx, pLeft, pLoop, pOp, pOrExpr, pOrTab, pOrTerm, pOrWc, pPk, pPk1, pPk2, pPk3, pRJ, pRJ1, pRangeEnd, pRangeStart, pRight, pRight1, pRight2, pRight3, pStart, pSubLoop, pSubWInfo, pSubq, pTab, pTab1, pTabItem, pTerm, pWC, pX, pX1, r, r1, r11, regBase, regBignull, regReturn, regRowid, regRowset, regYield, skipLikeAddr, start, startEq, start_constraints, t, t1, t2, testOp, untestedTerms, zEndAff, v1, v15, v2, v4, v6, v8 /* Jump here to continue with next cycle */
	iRowidReg = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       /* Rowid is stored in this register, if not zero */
	iReleaseReg = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     /* Temp register to free before returning */
	pIdx = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   /* Iteration of constraint generator loop */
	pWC = pWInfo + 80
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
	pTabItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
	iCur = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor
	(*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady = notReady & ^_sqlite3WhereGetMask(tls, pWInfo+496, iCur)
	bRev = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask >> iLevel & uint64(1))
	/* Create labels for the "break" and "continue" instructions
	 ** for the current loop.  Jump to addrBrk to break out of a loop.
	 ** Jump to cont to go immediately to the next iteration of the
	 ** loop.
	 **
	 ** When there is an IN operator, we also have a "addrNxt" label that
	 ** means to continue with the next IN value combination.  When
	 ** there are no IN operators in the constraints, the "addrNxt" label
	 ** is the same as "addrBrk".
	 */
	v1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk
	(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt = v1
	addrBrk = v1
	v1 = _sqlite3VdbeMakeLabel(tls, pParse)
	(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont = v1
	addrCont = v1
	/* If this is the right table of a LEFT OUTER JOIN, allocate and
	 ** initialize a memory cell that records if this table matches any
	 ** row of the left table of the join.
	 */
	if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom) > 0 && int32((**(**TSrcItem)(__ccgo_up(pTabItem))).Ffg.Fjointype)&int32(JT_LEFT) != 0 {
		v4 = pParse + 48
		*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
		v1 = *(*int32)(unsafe.Pointer(v4))
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin = v1
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin)
	}
	/* Special case of a FROM clause subquery implemented as a co-routine */
	if int32(*(*uint32)(unsafe.Pointer(pTabItem + 12 + 4))&0x40>>6) != 0 {
		pSubq = *(*uintptr)(unsafe.Pointer(pTabItem + 44))
		regYield = (*TSubquery)(unsafe.Pointer(pSubq)).FregReturn
		_sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), regYield, 0, (*TSubquery)(unsafe.Pointer(pSubq)).FaddrFillSub)
		(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Yield), regYield, addrBrk)
		(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Goto)
	} else {
		if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != uint32(0) {
			nConstraint = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm)
			iReg = _sqlite3GetTempRange(tls, pParse, nConstraint+int32(2))
			addrNotFound = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk
			j = 0
			for {
				if !(j < nConstraint) {
					break
				}
				iTarget = iReg + j + int32(2)
				pTerm = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4))
				if pTerm == uintptr(0) {
					goto _5
				}
				if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_IN) != 0 {
					if j <= int32(31) {
						v6 = libc.Uint32FromInt32(1) << j
					} else {
						v6 = uint32(0)
					}
					if v6&(*(*struct {
						FidxNum    int32
						F__ccgo4   uint8
						FisOrdered Ti8
						FomitMask  Tu16
						FidxStr    uintptr
						FmHandleIn Tu32
					})(unsafe.Pointer(pLoop + 24))).FmHandleIn != 0 {
						v4 = pParse + 44
						v1 = *(*int32)(unsafe.Pointer(v4))
						*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
						iTab = v1
						v8 = pParse + 48
						*(*int32)(unsafe.Pointer(v8)) = *(*int32)(unsafe.Pointer(v8)) + 1
						v2 = *(*int32)(unsafe.Pointer(v8))
						iCache = v2
						_sqlite3CodeRhsOfIN(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, iTab, 0)
						_sqlite3VdbeAddOp3(tls, v, int32(OP_VInitIn), iTab, iTarget, iCache)
					} else {
						_codeEqualityTerm(tls, pParse, pTerm, pLevel, j, bRev, iTarget)
						addrNotFound = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt
					}
				} else {
					pRight = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight
					_codeExprOrVector(tls, pParse, pRight, iTarget, int32(1))
					if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeMatchOp) == int32(SQLITE_INDEX_CONSTRAINT_OFFSET) && int32(Tu32(*(*uint8)(unsafe.Pointer(pLoop + 24 + 4))&0x2>>1)) != 0 {
						_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TSelect)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect)).FiOffset)
					}
				}
				goto _5
			_5:
				;
				j = j + 1
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), (*(*struct {
				FidxNum    int32
				F__ccgo4   uint8
				FisOrdered Ti8
				FomitMask  Tu16
				FidxStr    uintptr
				FmHandleIn Tu32
			})(unsafe.Pointer(pLoop + 24))).FidxNum, iReg)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), nConstraint, iReg+int32(1))
			/* The instruction immediately prior to OP_VFilter must be an OP_Integer
			 ** that sets the "argc" value for xVFilter.  This is necessary for
			 ** resolveP2() to work correctly.  See tag-20250207a. */
			if int32(Tu32(*(*uint8)(unsafe.Pointer(pLoop + 24 + 4))&0x1>>0)) != 0 {
				v1 = -int32(7)
			} else {
				v1 = -int32(1)
			}
			_sqlite3VdbeAddOp4(tls, v, int32(OP_VFilter), iCur, addrNotFound, iReg, (*(*struct {
				FidxNum    int32
				F__ccgo4   uint8
				FisOrdered Ti8
				FomitMask  Tu16
				FidxStr    uintptr
				FmHandleIn Tu32
			})(unsafe.Pointer(pLoop + 24))).FidxStr, v1)
			libc.SetBitFieldPtr8Uint32(pLoop+24+4, libc.Uint32FromInt32(0), 0, 0x1)
			/* An OOM inside of AddOp4(OP_VFilter) instruction above might have freed
			 ** the u.vtab.idxStr.  NULL it out to prevent a use-after-free */
			if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				(*(*struct {
					FidxNum    int32
					F__ccgo4   uint8
					FisOrdered Ti8
					FomitMask  Tu16
					FidxStr    uintptr
					FmHandleIn Tu32
				})(unsafe.Pointer(pLoop + 24))).FidxStr = uintptr(0)
			}
			(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = iCur
			if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass != 0 {
				v1 = int32(OP_Noop)
			} else {
				v1 = int32(OP_VNext)
			}
			(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(v1)
			(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = _sqlite3VdbeCurrentAddr(tls, v)
			j = 0
			for {
				if !(j < nConstraint) {
					break
				}
				pTerm = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4))
				if j < int32(16) && int32((*(*struct {
					FidxNum    int32
					F__ccgo4   uint8
					FisOrdered Ti8
					FomitMask  Tu16
					FidxStr    uintptr
					FmHandleIn Tu32
				})(unsafe.Pointer(pLoop + 24))).FomitMask)>>j&int32(1) != 0 {
					_disableTerm(tls, pLevel, pTerm)
					goto _13
				}
				if v15 = int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_IN) != 0; v15 {
					if j <= int32(31) {
						v6 = libc.Uint32FromInt32(1) << j
					} else {
						v6 = uint32(0)
					}
				}
				if v15 && v6&(*(*struct {
					FidxNum    int32
					F__ccgo4   uint8
					FisOrdered Ti8
					FomitMask  Tu16
					FidxStr    uintptr
					FmHandleIn Tu32
				})(unsafe.Pointer(pLoop + 24))).FmHandleIn == uint32(0) && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { /* IN loop corresponding to the j-th constraint */
					/* Reload the constraint value into reg[iReg+j+2].  The same value
					 ** was loaded into the same register prior to the OP_VFilter, but
					 ** the xFilter implementation might have changed the datatype or
					 ** encoding of the value in the register, so it *must* be reloaded.
					 */
					iIn = 0
					for {
						if !(iIn < (*(*struct {
							FnIn     int32
							FaInLoop uintptr
						})(unsafe.Pointer(pLevel + 68))).FnIn) {
							break
						}
						pOp = _sqlite3VdbeGetOp(tls, v, (**(**TInLoop)(__ccgo_up((*(*struct {
							FnIn     int32
							FaInLoop uintptr
						})(unsafe.Pointer(pLevel + 68))).FaInLoop + uintptr(iIn)*20))).FaddrInTop)
						if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 == iReg+j+int32(2) || int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Rowid) && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 == iReg+j+int32(2) {
							_sqlite3VdbeAddOp3(tls, v, int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode), (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1, (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2, (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3)
							break
						}
						goto _16
					_16:
						;
						iIn = iIn + 1
					}
					/* Generate code that will continue to the next row if
					 ** the IN constraint is not satisfied
					 */
					pCompare = _sqlite3PExpr(tls, pParse, int32(TK_EQ), uintptr(0), uintptr(0))
					if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
						iFld = (*(*struct {
							FleftColumn int32
							FiField     int32
						})(unsafe.Pointer(pTerm + 24))).FiField
						pLeft = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft
						if iFld > 0 {
							(*TExpr)(unsafe.Pointer(pCompare)).FpLeft = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 20)) + 8 + uintptr(iFld-int32(1))*20))).FpExpr
						} else {
							(*TExpr)(unsafe.Pointer(pCompare)).FpLeft = pLeft
						}
						v4 = _sqlite3Expr(tls, db, int32(TK_REGISTER), uintptr(0))
						pRight1 = v4
						(*TExpr)(unsafe.Pointer(pCompare)).FpRight = v4
						if pRight1 != 0 {
							(*TExpr)(unsafe.Pointer(pRight1)).FiTable = iReg + j + int32(2)
							_sqlite3ExprIfFalse(tls, pParse, pCompare, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont, int32(SQLITE_JUMPIFNULL))
						}
						(*TExpr)(unsafe.Pointer(pCompare)).FpLeft = uintptr(0)
					}
					_sqlite3ExprDelete(tls, db, pCompare)
				}
				goto _13
			_13:
				;
				j = j + 1
			}
			/* These registers need to be preserved in case there is an IN operator
			 ** loop.  So we could deallocate the registers here (and potentially
			 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0.  But it seems
			 ** simpler and safer to simply not reuse the registers.
			 **
			 **    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
			 */
		} else {
			if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_COLUMN_IN)|libc.Int32FromInt32(WHERE_COLUMN_EQ)) != uint32(0) {
				/* Case 2:  We can directly reference a single row using an
				 **          equality comparison against the ROWID field.  Or
				 **          we reference multiple rows using a "rowid IN (...)"
				 **          construct.
				 */
				pTerm = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm))
				v4 = pParse + 48
				*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
				v1 = *(*int32)(unsafe.Pointer(v4))
				iReleaseReg = v1
				iRowidReg = _codeEqualityTerm(tls, pParse, pTerm, pLevel, 0, bRev, iReleaseReg)
				if iRowidReg != iReleaseReg {
					_sqlite3ReleaseTempReg(tls, pParse, iReleaseReg)
				}
				addrNxt = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt
				if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter != 0 {
					_sqlite3VdbeAddOp2(tls, v, int32(OP_MustBeInt), iRowidReg, addrNxt)
					_sqlite3VdbeAddOp4Int(tls, v, int32(OP_Filter), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, addrNxt, iRowidReg, int32(1))
					_filterPullDown(tls, pParse, pWInfo, iLevel, addrNxt, notReady)
				}
				_sqlite3VdbeAddOp3(tls, v, int32(OP_SeekRowid), iCur, addrNxt, iRowidReg)
				(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Noop)
			} else {
				if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_COLUMN_RANGE) != uint32(0) {
					/* Case 3:  We have an inequality comparison against the ROWID field.
					 */
					testOp = int32(OP_Noop)
					memEndValue = 0
					j = 0
					v4 = libc.UintptrFromInt32(0)
					pEnd = v4
					pStart = v4
					if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_BTM_LIMIT) != 0 {
						v1 = j
						j = j + 1
						pStart = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(v1)*4))
					}
					if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_TOP_LIMIT) != 0 {
						v1 = j
						j = j + 1
						pEnd = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(v1)*4))
					}
					if bRev != 0 {
						pTerm = pStart
						pStart = pEnd
						pEnd = pTerm
					}
					if pStart != 0 { /* Cursor seek operation */
						/* The following constant maps TK_xx codes into corresponding
						 ** seek opcodes.  It depends on a particular ordering of TK_xx
						 */
						aMoveOp = [4]Tu8{
							0: uint8(OP_SeekGT),
							1: uint8(OP_SeekLE),
							2: uint8(OP_SeekLT),
							3: uint8(OP_SeekGE),
						}
						/* Make sure the ordering.. */
						/*  ... of the TK_xx values... */
						/*  ... is correct. */
						pX = (*TWhereTerm)(unsafe.Pointer(pStart)).FpExpr
						/* transitive constraints */
						if _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pX)).FpRight) != 0 {
							v1 = _sqlite3GetTempReg(tls, pParse)
							**(**int32)(__ccgo_up(bp)) = v1
							r11 = v1
							_codeExprOrVector(tls, pParse, (*TExpr)(unsafe.Pointer(pX)).FpRight, r11, int32(1))
							op = int32(aMoveOp[(int32((*TExpr)(unsafe.Pointer(pX)).Fop)-int32(TK_GT)-int32(1))&int32(0x3)|int32(0x1)])
						} else {
							r11 = _sqlite3ExprCodeTemp(tls, pParse, (*TExpr)(unsafe.Pointer(pX)).FpRight, bp)
							_disableTerm(tls, pLevel, pStart)
							op = int32(aMoveOp[int32((*TExpr)(unsafe.Pointer(pX)).Fop)-int32(TK_GT)])
						}
						_sqlite3VdbeAddOp3(tls, v, op, iCur, addrBrk, r11)
						_sqlite3ReleaseTempReg(tls, pParse, **(**int32)(__ccgo_up(bp)))
					} else {
						if bRev != 0 {
							v1 = int32(OP_Last)
						} else {
							v1 = int32(OP_Rewind)
						}
						_sqlite3VdbeAddOp2(tls, v, v1, iCur, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrHalt)
					}
					if pEnd != 0 {
						pX1 = (*TWhereTerm)(unsafe.Pointer(pEnd)).FpExpr
						/* Transitive constraints */
						v4 = pParse + 48
						*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
						v1 = *(*int32)(unsafe.Pointer(v4))
						memEndValue = v1
						_codeExprOrVector(tls, pParse, (*TExpr)(unsafe.Pointer(pX1)).FpRight, memEndValue, int32(1))
						if 0 == _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pX1)).FpRight) && (int32((*TExpr)(unsafe.Pointer(pX1)).Fop) == int32(TK_LT) || int32((*TExpr)(unsafe.Pointer(pX1)).Fop) == int32(TK_GT)) {
							if bRev != 0 {
								v1 = int32(OP_Le)
							} else {
								v1 = int32(OP_Ge)
							}
							testOp = v1
						} else {
							if bRev != 0 {
								v1 = int32(OP_Lt)
							} else {
								v1 = int32(OP_Gt)
							}
							testOp = v1
						}
						if 0 == _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pX1)).FpRight) {
							_disableTerm(tls, pLevel, pEnd)
						}
					}
					start = _sqlite3VdbeCurrentAddr(tls, v)
					if bRev != 0 {
						v1 = int32(OP_Prev)
					} else {
						v1 = int32(OP_Next)
					}
					(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(v1)
					(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = iCur
					(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = start
					if testOp != int32(OP_Noop) {
						v4 = pParse + 48
						*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
						v1 = *(*int32)(unsafe.Pointer(v4))
						iRowidReg = v1
						_sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCur, iRowidReg)
						_sqlite3VdbeAddOp3(tls, v, testOp, memEndValue, addrBrk, iRowidReg)
						_sqlite3VdbeChangeP5(tls, v, uint16(libc.Int32FromInt32(SQLITE_AFF_NUMERIC)|libc.Int32FromInt32(SQLITE_JUMPIFNULL)))
					}
				} else {
					if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_INDEXED) != 0 {
						nEq = (*(*struct {
							FnEq          Tu16
							FnBtm         Tu16
							FnTop         Tu16
							FnDistinctCol Tu16
							FpIndex       uintptr
							FpOrderBy     uintptr
						})(unsafe.Pointer(pLoop + 24))).FnEq /* Number of == or IN terms */
						nBtm = (*(*struct {
							FnEq          Tu16
							FnBtm         Tu16
							FnTop         Tu16
							FnDistinctCol Tu16
							FpIndex       uintptr
							FpOrderBy     uintptr
						})(unsafe.Pointer(pLoop + 24))).FnBtm /* Length of BTM vector */
						nTop = (*(*struct {
							FnEq          Tu16
							FnBtm         Tu16
							FnTop         Tu16
							FnDistinctCol Tu16
							FpIndex       uintptr
							FpOrderBy     uintptr
						})(unsafe.Pointer(pLoop + 24))).FnTop /* Base register holding constraint values */
						pRangeStart = uintptr(0) /* Inequality constraint at range start */
						pRangeEnd = uintptr(0)   /* The VDBE cursor for the index */
						nExtraReg = 0            /* Affinity for start of range constraint */
						zEndAff = uintptr(0)     /* Affinity for end of range constraint */
						bSeekPastNull = uint8(0) /* True to seek past initial nulls */
						bStopAtNull = uint8(0)   /* True if we use the index only */
						regBignull = 0           /* big-null flag register */
						addrSeekScan = 0         /* Opcode of the OP_SeekScan, if any */
						pIdx = (*(*struct {
							FnEq          Tu16
							FnBtm         Tu16
							FnTop         Tu16
							FnDistinctCol Tu16
							FpIndex       uintptr
							FpOrderBy     uintptr
						})(unsafe.Pointer(pLoop + 24))).FpIndex
						iIdxCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur
						/* Find any inequality constraint terms for the start and end
						 ** of the range.
						 */
						j = int32(nEq)
						if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_BTM_LIMIT) != 0 {
							v1 = j
							j = j + 1
							pRangeStart = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(v1)*4))
							if nExtraReg > int32((*(*struct {
								FnEq          Tu16
								FnBtm         Tu16
								FnTop         Tu16
								FnDistinctCol Tu16
								FpIndex       uintptr
								FpOrderBy     uintptr
							})(unsafe.Pointer(pLoop + 24))).FnBtm) {
								v1 = nExtraReg
							} else {
								v1 = int32((*(*struct {
									FnEq          Tu16
									FnBtm         Tu16
									FnTop         Tu16
									FnDistinctCol Tu16
									FpIndex       uintptr
									FpOrderBy     uintptr
								})(unsafe.Pointer(pLoop + 24))).FnBtm)
							}
							nExtraReg = v1
							/* Like optimization range constraints always occur in pairs */
						}
						if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_TOP_LIMIT) != 0 {
							v1 = j
							j = j + 1
							pRangeEnd = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(v1)*4))
							if nExtraReg > int32((*(*struct {
								FnEq          Tu16
								FnBtm         Tu16
								FnTop         Tu16
								FnDistinctCol Tu16
								FpIndex       uintptr
								FpOrderBy     uintptr
							})(unsafe.Pointer(pLoop + 24))).FnTop) {
								v1 = nExtraReg
							} else {
								v1 = int32((*(*struct {
									FnEq          Tu16
									FnBtm         Tu16
									FnTop         Tu16
									FnDistinctCol Tu16
									FpIndex       uintptr
									FpOrderBy     uintptr
								})(unsafe.Pointer(pLoop + 24))).FnTop)
							}
							nExtraReg = v1
							if pRangeStart == uintptr(0) {
								j = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(nEq)*2)))
								if j >= 0 && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(j)*12 + 4))&0xf>>0)) == 0 || j == -int32(2) {
									bSeekPastNull = uint8(1)
								}
							}
						}
						/* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses
						 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
						 ** FIRST). In both cases separate ordered scans are made of those
						 ** index entries for which the column is null and for those for which
						 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
						 ** For DESC, NULL entries are scanned first.
						 */
						if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_TOP_LIMIT)|libc.Int32FromInt32(WHERE_BTM_LIMIT)) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_BIGNULL_SORT) != uint32(0) {
							nExtraReg = int32(1)
							bSeekPastNull = uint8(1)
							v4 = pParse + 48
							*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
							v2 = *(*int32)(unsafe.Pointer(v4))
							v1 = v2
							regBignull = v1
							(*TWhereLevel)(unsafe.Pointer(pLevel)).FregBignull = v1
							if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 {
								_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regBignull)
							}
							(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBignull = _sqlite3VdbeMakeLabel(tls, pParse)
						}
						/* If we are doing a reverse order scan on an ascending index, or
						 ** a forward order scan on a descending index, interchange the
						 ** start and end terms (pRangeStart and pRangeEnd).
						 */
						if int32(nEq) < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) && bRev == libc.BoolInt32(int32(**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(nEq)))) == SQLITE_SO_ASC) {
							t = pRangeEnd
							pRangeEnd = pRangeStart
							pRangeStart = t
							t1 = bSeekPastNull
							bSeekPastNull = bStopAtNull
							bStopAtNull = t1
							t2 = uint8(nBtm)
							nBtm = nTop
							nTop = uint16(t2)
						}
						if iLevel > 0 && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_SEEKSCAN) != uint32(0) {
							/* In case OP_SeekScan is used, ensure that the index cursor does not
							 ** point to a valid row for the first iteration of this loop. */
							_sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), iIdxCur)
						}
						/* Generate code to evaluate all constraint terms using == or IN
						 ** and store the values of those terms in an array of registers
						 ** starting at regBase.
						 */
						regBase = _codeAllEqualityTerms(tls, pParse, pLevel, bRev, nExtraReg, bp+4)
						if **(**uintptr)(__ccgo_up(bp + 4)) != 0 && nTop != 0 {
							zEndAff = _sqlite3DbStrDup(tls, db, **(**uintptr)(__ccgo_up(bp + 4))+uintptr(nEq))
						}
						if regBignull != 0 {
							v1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBignull
						} else {
							v1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt
						}
						addrNxt = v1
						startEq = libc.BoolInt32(!(pRangeStart != 0) || int32((*TWhereTerm)(unsafe.Pointer(pRangeStart)).FeOperator)&(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) != 0)
						endEq = libc.BoolInt32(!(pRangeEnd != 0) || int32((*TWhereTerm)(unsafe.Pointer(pRangeEnd)).FeOperator)&(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) != 0)
						start_constraints = libc.BoolInt32(pRangeStart != 0 || int32(nEq) > 0)
						/* Seek the index cursor to the start of the range. */
						nConstraint1 = int32(nEq)
						if pRangeStart != 0 {
							pRight2 = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pRangeStart)).FpExpr)).FpRight
							_codeExprOrVector(tls, pParse, pRight2, regBase+int32(nEq), int32(nBtm))
							if int32((*TWhereTerm)(unsafe.Pointer(pRangeStart)).FwtFlags)&int32(TERM_VNULL) == 0 && _sqlite3ExprCanBeNull(tls, pRight2) != 0 {
								_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regBase+int32(nEq), addrNxt)
							}
							if **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
								_updateRangeAffinityStr(tls, pRight2, int32(nBtm), **(**uintptr)(__ccgo_up(bp + 4))+uintptr(nEq))
							}
							nConstraint1 = nConstraint1 + int32(nBtm)
							if _sqlite3ExprIsVector(tls, pRight2) == 0 {
								_disableTerm(tls, pLevel, pRangeStart)
							} else {
								startEq = int32(1)
							}
							bSeekPastNull = uint8(0)
						} else {
							if bSeekPastNull != 0 {
								startEq = 0
								_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regBase+int32(nEq))
								start_constraints = int32(1)
								nConstraint1 = nConstraint1 + 1
							} else {
								if regBignull != 0 {
									_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regBase+int32(nEq))
									start_constraints = int32(1)
									nConstraint1 = nConstraint1 + 1
								}
							}
						}
						_codeApplyAffinity(tls, pParse, regBase, nConstraint1-int32(bSeekPastNull), **(**uintptr)(__ccgo_up(bp + 4)))
						if int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) > 0 && nConstraint1 == int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) {
							/* The skip-scan logic inside the call to codeAllEqualityConstraints()
							 ** above has already left the cursor sitting on the correct row,
							 ** so no further seeking is needed */
						} else {
							if regBignull != 0 {
								_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), regBignull)
							}
							if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter != 0 {
								_sqlite3VdbeAddOp4Int(tls, v, int32(OP_Filter), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregFilter, addrNxt, regBase, int32(nEq))
								_filterPullDown(tls, pParse, pWInfo, iLevel, addrNxt, notReady)
							}
							op1 = int32(_aStartOp[start_constraints<<int32(2)+startEq<<int32(1)+bRev])
							if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_SEEKSCAN) != uint32(0) && op1 == int32(OP_SeekGE) {
								/* TUNING:  The OP_SeekScan opcode seeks to reduce the number
								 ** of expensive seek operations by replacing a single seek with
								 ** 1 or more step operations.  The question is, how many steps
								 ** should we try before giving up and going with a seek.  The cost
								 ** of a seek is proportional to the logarithm of the of the number
								 ** of entries in the tree, so basing the number of steps to try
								 ** on the estimated number of rows in the btree seems like a good
								 ** guess. */
								addrSeekScan = _sqlite3VdbeAddOp1(tls, v, int32(OP_SeekScan), (int32(**(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst)))+int32(9))/int32(10))
								if pRangeStart != 0 || pRangeEnd != 0 {
									_sqlite3VdbeChangeP5(tls, v, uint16(1))
									_sqlite3VdbeChangeP2(tls, v, addrSeekScan, _sqlite3VdbeCurrentAddr(tls, v)+int32(1))
									addrSeekScan = 0
								}
							}
							_sqlite3VdbeAddOp4Int(tls, v, op1, iIdxCur, addrNxt, regBase, nConstraint1)
							if regBignull != 0 {
								_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, _sqlite3VdbeCurrentAddr(tls, v)+int32(2))
								op1 = int32(_aStartOp[libc.BoolInt32(nConstraint1 > int32(1))*int32(4)+int32(2)+bRev])
								_sqlite3VdbeAddOp4Int(tls, v, op1, iIdxCur, addrNxt, regBase, nConstraint1-startEq)
							}
						}
						/* Load the value for the inequality constraint at the end of the
						 ** range (if any).
						 */
						nConstraint1 = int32(nEq)
						if pRangeEnd != 0 {
							pRight3 = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pRangeEnd)).FpExpr)).FpRight
							_codeExprOrVector(tls, pParse, pRight3, regBase+int32(nEq), int32(nTop))
							if int32((*TWhereTerm)(unsafe.Pointer(pRangeEnd)).FwtFlags)&int32(TERM_VNULL) == 0 && _sqlite3ExprCanBeNull(tls, pRight3) != 0 {
								_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), regBase+int32(nEq), addrNxt)
							}
							if zEndAff != 0 {
								_updateRangeAffinityStr(tls, pRight3, int32(nTop), zEndAff)
								_codeApplyAffinity(tls, pParse, regBase+int32(nEq), int32(nTop), zEndAff)
							} else {
							}
							nConstraint1 = nConstraint1 + int32(nTop)
							if _sqlite3ExprIsVector(tls, pRight3) == 0 {
								_disableTerm(tls, pLevel, pRangeEnd)
							} else {
								endEq = int32(1)
							}
						} else {
							if bStopAtNull != 0 {
								if regBignull == 0 {
									_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regBase+int32(nEq))
									endEq = 0
								}
								nConstraint1 = nConstraint1 + 1
							}
						}
						if **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
							_sqlite3DbNNFreeNN(tls, db, **(**uintptr)(__ccgo_up(bp + 4)))
						}
						if zEndAff != 0 {
							_sqlite3DbNNFreeNN(tls, db, zEndAff)
						}
						/* Top of the loop body */
						(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = _sqlite3VdbeCurrentAddr(tls, v)
						/* Check if the index cursor is past the end of the range. */
						if nConstraint1 != 0 {
							if regBignull != 0 {
								/* Except, skip the end-of-range check while doing the NULL-scan */
								_sqlite3VdbeAddOp2(tls, v, int32(OP_IfNot), regBignull, _sqlite3VdbeCurrentAddr(tls, v)+int32(3))
							}
							op1 = int32(_aEndOp[bRev*int32(2)+endEq])
							_sqlite3VdbeAddOp4Int(tls, v, op1, iIdxCur, addrNxt, regBase, nConstraint1)
							if addrSeekScan != 0 {
								_sqlite3VdbeJumpHere(tls, v, addrSeekScan)
							}
						}
						if regBignull != 0 {
							/* During a NULL-scan, check to see if we have reached the end of
							 ** the NULLs */
							_sqlite3VdbeAddOp2(tls, v, int32(OP_If), regBignull, _sqlite3VdbeCurrentAddr(tls, v)+int32(2))
							op1 = int32(_aEndOp[bRev*int32(2)+int32(bSeekPastNull)])
							_sqlite3VdbeAddOp4Int(tls, v, op1, iIdxCur, addrNxt, regBase, nConstraint1+int32(bSeekPastNull))
						}
						if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_EARLYOUT) != uint32(0) {
							_sqlite3VdbeAddOp3(tls, v, int32(OP_SeekHit), iIdxCur, int32(nEq), int32(nEq))
						}
						/* Seek the table cursor, if required */
						omitTable = libc.BoolInt32((*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IDX_ONLY) != uint32(0) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&(libc.Int32FromInt32(WHERE_OR_SUBCLAUSE)|libc.Int32FromInt32(WHERE_RIGHT_JOIN)) == 0)
						if omitTable != 0 {
							/* pIdx is a covering index.  No need to access the main table. */
						} else {
							if (*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
								_codeDeferredSeek(tls, pWInfo, pIdx, iCur, iIdxCur)
							} else {
								if iCur != iIdxCur {
									pPk = _sqlite3PrimaryKeyIndex(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpTable)
									iRowidReg = _sqlite3GetTempRange(tls, pParse, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol))
									j = 0
									for {
										if !(j < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
											break
										}
										k = _sqlite3TableColumnToIndex(tls, pIdx, int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(j)*2))))
										_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iIdxCur, k, iRowidReg+j)
										goto _40
									_40:
										;
										j = j + 1
									}
									_sqlite3VdbeAddOp4Int(tls, v, int32(OP_NotFound), iCur, addrCont, iRowidReg, int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol))
								}
							}
						}
						if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin == 0 {
							/* If a partial index is driving the loop, try to eliminate WHERE clause
							 ** terms from the query that must be true due to the WHERE clause of
							 ** the partial index.  This optimization does not work on an outer join,
							 ** as shown by:
							 **
							 ** 2019-11-02 ticket 623eff57e76d45f6      (LEFT JOIN)
							 ** 2025-05-29 forum post 7dee41d32506c4ae  (RIGHT JOIN)
							 */
							if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 && (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ == uintptr(0) {
								_whereApplyPartialIndexConstraints(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere, iCur, pWC)
							}
						} else {
							/* The following assert() is not a requirement, merely an observation:
							 ** The OR-optimization doesn't work for the right hand table of
							 ** a LEFT JOIN: */
						}
						/* Record the instruction used to terminate the loop. */
						if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_ONEROW) != 0 || (*(*struct {
							FnIn     int32
							FaInLoop uintptr
						})(unsafe.Pointer(pLevel + 68))).FnIn != 0 && regBignull == 0 && _whereLoopIsOneRow(tls, pLoop) != 0 {
							(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Noop)
						} else {
							if bRev != 0 {
								(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Prev)
							} else {
								(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Next)
							}
						}
						(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = iIdxCur
						if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_UNQ_WANTED) != uint32(0) {
							v1 = int32(1)
						} else {
							v1 = 0
						}
						(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp3 = uint8(v1)
						if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_CONSTRAINT) == uint32(0) {
							(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp5 = uint8(SQLITE_STMTSTATUS_FULLSCAN_STEP)
						} else {
						}
						if omitTable != 0 {
							pIdx = uintptr(0)
						}
					} else {
						if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_MULTI_OR) != 0 { /* Shortened table list or OR-clause generation */
							pCov = uintptr(0)
							v4 = pParse + 44
							v1 = *(*int32)(unsafe.Pointer(v4))
							*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1 /* Potential covering index (or NULL) */
							iCovCur = v1
							v8 = pParse + 48
							*(*int32)(unsafe.Pointer(v8)) = *(*int32)(unsafe.Pointer(v8)) + 1
							v2 = *(*int32)(unsafe.Pointer(v8))             /* Cursor used for index scans (if any) */
							regReturn = v2                                 /* Register used with OP_Gosub */
							regRowset = 0                                  /* Register for RowSet object */
							regRowid = 0                                   /* Register holding rowid */
							iLoopBody = _sqlite3VdbeMakeLabel(tls, pParse) /* Address of regReturn init */
							untestedTerms = 0                              /* Loop counter */
							pAndExpr = uintptr(0)                          /* An ".. AND (...)" expression */
							pTab = (*TSrcItem)(unsafe.Pointer(pTabItem)).FpSTab
							pTerm = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm))
							pOrWc = *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu))
							(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Return)
							(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = regReturn
							/* Set up a new SrcList in pOrTab containing the table being scanned
							 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
							 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
							 */
							if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) > int32(1) || int32(*(*uint32)(unsafe.Pointer(pTabItem + 12 + 4))&0x40000>>18) != 0 { /* Original list of tables */
								nNotReady = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) - iLevel - int32(1)
								pOrTab = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(libc.UintptrFromInt32(0)+8)+uint32(nNotReady+libc.Int32FromInt32(1))*libc.Uint32FromInt64(48)))
								if pOrTab == uintptr(0) {
									return notReady
								}
								(*TSrcList)(unsafe.Pointer(pOrTab)).FnAlloc = uint32(uint8(nNotReady + libc.Int32FromInt32(1)))
								(*TSrcList)(unsafe.Pointer(pOrTab)).FnSrc = int32((*TSrcList)(unsafe.Pointer(pOrTab)).FnAlloc)
								libc.Xmemcpy(tls, pOrTab+8, pTabItem, uint32(48))
								origSrc = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8
								k = int32(1)
								for {
									if !(k <= nNotReady) {
										break
									}
									libc.Xmemcpy(tls, pOrTab+8+uintptr(k)*48, origSrc+uintptr((**(**TWhereLevel)(__ccgo_up(pLevel + uintptr(k)*88))).FiFrom)*48, uint32(48))
									goto _46
								_46:
									;
									k = k + 1
								}
								/* Clear the fromExists flag on the OR-optimized table entry so that
								 ** the calls to sqlite3WhereEnd() do not code early-exits after the
								 ** first row is visited. The early exit applies to this table's
								 ** overall loop - including the multiple OR branches and any WHERE
								 ** conditions not passed to the sub-loops - not to the sub-loops.  */
								libc.SetBitFieldPtr32Uint32(pOrTab+8+12+4, libc.Uint32FromInt32(0), 18, 0x40000)
							} else {
								pOrTab = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList
							}
							/* Initialize the rowset register to contain NULL. An SQL NULL is
							 ** equivalent to an empty rowset.  Or, create an ephemeral index
							 ** capable of holding primary keys in the case of a WITHOUT ROWID.
							 **
							 ** Also initialize regReturn to contain the address of the instruction
							 ** immediately following the OP_Return at the bottom of the loop. This
							 ** is required in a few obscure LEFT JOIN cases where control jumps
							 ** over the top of the loop into the body of it. In this case the
							 ** correct response for the end-of-loop code (the OP_Return) is to
							 ** fall through to the next instruction, just as an OP_Next does if
							 ** called on an uninitialized cursor.
							 */
							if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DUPLICATES_OK) == 0 {
								if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
									v4 = pParse + 48
									*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
									v1 = *(*int32)(unsafe.Pointer(v4))
									regRowset = v1
									_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regRowset)
								} else {
									pPk1 = _sqlite3PrimaryKeyIndex(tls, pTab)
									v4 = pParse + 44
									v1 = *(*int32)(unsafe.Pointer(v4))
									*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
									regRowset = v1
									_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), regRowset, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol))
									_sqlite3VdbeSetP4KeyInfo(tls, pParse, pPk1)
								}
								v4 = pParse + 48
								*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
								v1 = *(*int32)(unsafe.Pointer(v4))
								regRowid = v1
							}
							iRetInit = _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regReturn)
							/* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
							 ** Then for every term xN, evaluate as the subexpression: xN AND y
							 ** That way, terms in y that are factored into the disjunction will
							 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
							 **
							 ** Actually, each subexpression is converted to "xN AND w" where w is
							 ** the "interesting" terms of z - terms that did not originate in the
							 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
							 ** indices.
							 **
							 ** This optimization also only applies if the (x1 OR x2 OR ...) term
							 ** is not contained in the ON clause of a LEFT JOIN.
							 ** See ticket http://sqlite.org/src/info/f2369304e4
							 **
							 ** 2022-02-04:  Do not push down slices of a row-value comparison.
							 ** In other words, "w" or "y" may not be a slice of a vector.  Otherwise,
							 ** the initialization of the right-hand operand of the vector comparison
							 ** might not occur, or might occur only in an OR branch that is not
							 ** taken.  dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1.
							 **
							 ** 2022-03-03:  Do not push down expressions that involve subqueries.
							 ** The subquery might get coded as a subroutine.  Any table-references
							 ** in the subquery might be resolved to index-references for the index on
							 ** the OR branch in which the subroutine is coded.  But if the subroutine
							 ** is invoked from a different OR branch that uses a different index, such
							 ** index-references will not work.  tag-20220303a
							 ** https://sqlite.org/forum/forumpost/36937b197273d403
							 */
							if (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm > int32(1) {
								iTerm = 0
								for {
									if !(iTerm < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) {
										break
									}
									pExpr = (**(**TWhereTerm)(__ccgo_up((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iTerm)*48))).FpExpr
									if (*TWhereClause)(unsafe.Pointer(pWC)).Fa+uintptr(iTerm)*48 == pTerm {
										goto _53
									}
									if int32((**(**TWhereTerm)(__ccgo_up((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iTerm)*48))).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_CODED)|libc.Int32FromInt32(TERM_SLICE)) != 0 {
										goto _53
									}
									if int32((**(**TWhereTerm)(__ccgo_up((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iTerm)*48))).FeOperator)&int32(WO_ALL) == 0 {
										goto _53
									}
									if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Subquery)) != uint32(0) {
										goto _53
									} /* tag-20220303a */
									pExpr = _sqlite3ExprDup(tls, db, pExpr, 0)
									pAndExpr = _sqlite3ExprAnd(tls, pParse, pAndExpr, pExpr)
									goto _53
								_53:
									;
									iTerm = iTerm + 1
								}
								if pAndExpr != 0 {
									/* The extra 0x10000 bit on the opcode is masked off and does not
									 ** become part of the new Expr.op.  However, it does make the
									 ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
									 ** prevents sqlite3PExpr() from applying the AND short-circuit
									 ** optimization, which we do not want here. */
									pAndExpr = _sqlite3PExpr(tls, pParse, libc.Int32FromInt32(TK_AND)|libc.Int32FromInt32(0x10000), uintptr(0), pAndExpr)
								}
							}
							/* Run a separate WHERE clause for each term of the OR clause.  After
							 ** eliminating duplicates from other WHERE clauses, the action for each
							 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
							 */
							_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+25130, 0)
							ii = 0
							for {
								if !(ii < (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm) {
									break
								}
								pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa + uintptr(ii)*48
								if (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor == iCur || int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator)&int32(WO_AND) != 0 { /* Info for single OR-term scan */
									pOrExpr = (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr /* Local copy of OR clause term */
									jmp1 = 0                                                /* Address of jump operation */
									/* See TH3 vtab25.400 and ticket 614b25314c766238 */
									v4 = _sqlite3ExprDup(tls, db, pOrExpr, 0)
									pOrExpr = v4
									pDelete = v4
									if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
										_sqlite3ExprDelete(tls, db, pDelete)
										goto _54
									}
									if pAndExpr != 0 {
										(*TExpr)(unsafe.Pointer(pAndExpr)).FpLeft = pOrExpr
										pOrExpr = pAndExpr
									}
									/* Loop through table entries that match term pOrTerm. */
									_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+25145, libc.VaList(bp+72, ii+int32(1)))
									pSubWInfo = _sqlite3WhereBegin(tls, pParse, pOrTab, pOrExpr, uintptr(0), uintptr(0), uintptr(0), uint16(WHERE_OR_SUBCLAUSE), iCovCur)
									if pSubWInfo != 0 {
										addrExplain = _sqlite3WhereExplainOneScan(tls, pParse, pOrTab, pSubWInfo+760, uint16(0))
										_ = addrExplain
										/* This is the sub-WHERE clause body.  First skip over
										 ** duplicate rows from prior sub-WHERE clauses, and record the
										 ** rowid (or PRIMARY KEY) for the current row so that the same
										 ** row will be skipped in subsequent sub-WHERE clauses.
										 */
										if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DUPLICATES_OK) == 0 {
											if ii == (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm-int32(1) {
												v1 = -int32(1)
											} else {
												v1 = ii
											}
											iSet = v1
											if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
												_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, -int32(1), regRowid)
												jmp1 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_RowSetTest), regRowset, 0, regRowid, iSet)
											} else {
												pPk2 = _sqlite3PrimaryKeyIndex(tls, pTab)
												nPk = int32((*TIndex)(unsafe.Pointer(pPk2)).FnKeyCol)
												/* Read the PK into an array of temp registers. */
												r = _sqlite3GetTempRange(tls, pParse, nPk)
												iPk = 0
												for {
													if !(iPk < nPk) {
														break
													}
													iCol = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk2)).FaiColumn + uintptr(iPk)*2)))
													_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, iCol, r+iPk)
													goto _57
												_57:
													;
													iPk = iPk + 1
												}
												/* Check if the temp table already contains this key. If so,
												 ** the row has already been included in the result set and
												 ** can be ignored (by jumping past the Gosub below). Otherwise,
												 ** insert the key into the temp table and proceed with processing
												 ** the row.
												 **
												 ** Use some of the same optimizations as OP_RowSetTest: If iSet
												 ** is zero, assume that the key cannot already be present in
												 ** the temp table. And if iSet is -1, assume that there is no
												 ** need to insert the key into the temp table, as it will never
												 ** be tested for.  */
												if iSet != 0 {
													jmp1 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), regRowset, 0, r, nPk)
												}
												if iSet >= 0 {
													_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), r, nPk, regRowid)
													_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), regRowset, regRowid, r, nPk)
													if iSet != 0 {
														_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT))
													}
												}
												/* Release the array of temp registers */
												_sqlite3ReleaseTempRange(tls, pParse, r, nPk)
											}
										}
										/* Invoke the main loop body as a subroutine */
										_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), regReturn, iLoopBody)
										/* Jump here (skipping the main loop body subroutine) if the
										 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
										if jmp1 != 0 {
											_sqlite3VdbeJumpHere(tls, v, jmp1)
										}
										/* The pSubWInfo->untestedTerms flag means that this OR term
										 ** contained one or more AND term from a notReady table.  The
										 ** terms from the notReady table could not be tested and will
										 ** need to be tested later.
										 */
										if int32(uint32(*(*uint8)(unsafe.Pointer(pSubWInfo + 48))&0x2>>1)) != 0 {
											untestedTerms = int32(1)
										}
										/* If all of the OR-connected terms are optimized using the same
										 ** index, and the index is opened using the same cursor number
										 ** by each call to sqlite3WhereBegin() made by this loop, it may
										 ** be possible to use that index as a covering index.
										 **
										 ** If the call to sqlite3WhereBegin() above resulted in a scan that
										 ** uses an index, and this is either the first OR-connected term
										 ** processed or the index is the same as that used by all previous
										 ** terms, set pCov to the candidate covering index. Otherwise, set
										 ** pCov to NULL to indicate that no candidate covering index will
										 ** be available.
										 */
										pSubLoop = (*(*TWhereLevel)(unsafe.Pointer(pSubWInfo + 760))).FpWLoop
										if (*TWhereLoop)(unsafe.Pointer(pSubLoop)).FwsFlags&uint32(WHERE_INDEXED) != uint32(0) && (ii == 0 || (*(*struct {
											FnEq          Tu16
											FnBtm         Tu16
											FnTop         Tu16
											FnDistinctCol Tu16
											FpIndex       uintptr
											FpOrderBy     uintptr
										})(unsafe.Pointer(pSubLoop + 24))).FpIndex == pCov) && ((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) || !(int32(uint32(*(*uint16)(unsafe.Pointer((*(*struct {
											FnEq          Tu16
											FnBtm         Tu16
											FnTop         Tu16
											FnDistinctCol Tu16
											FpIndex       uintptr
											FpOrderBy     uintptr
										})(unsafe.Pointer(pSubLoop + 24))).FpIndex + 56))&0x3>>0)) == libc.Int32FromInt32(SQLITE_IDXTYPE_PRIMARYKEY))) {
											pCov = (*(*struct {
												FnEq          Tu16
												FnBtm         Tu16
												FnTop         Tu16
												FnDistinctCol Tu16
												FpIndex       uintptr
												FpOrderBy     uintptr
											})(unsafe.Pointer(pSubLoop + 24))).FpIndex
										} else {
											pCov = uintptr(0)
										}
										if _sqlite3WhereUsesDeferredSeek(tls, pSubWInfo) != 0 {
											libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 0, 0x1)
										}
										/* Finish the loop through table entries that match term pOrTerm. */
										_sqlite3WhereEnd(tls, pSubWInfo)
										_sqlite3VdbeExplainPop(tls, pParse)
									}
									_sqlite3ExprDelete(tls, db, pDelete)
								}
								goto _54
							_54:
								;
								ii = ii + 1
							}
							_sqlite3VdbeExplainPop(tls, pParse)
							*(*uintptr)(unsafe.Pointer(&(*TWhereLevel)(unsafe.Pointer(pLevel)).Fu)) = pCov
							if pCov != 0 {
								(*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur = iCovCur
							}
							if pAndExpr != 0 {
								(*TExpr)(unsafe.Pointer(pAndExpr)).FpLeft = uintptr(0)
								_sqlite3ExprDelete(tls, db, pAndExpr)
							}
							_sqlite3VdbeChangeP1(tls, v, iRetInit, _sqlite3VdbeCurrentAddr(tls, v))
							_sqlite3VdbeGoto(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk)
							_sqlite3VdbeResolveLabel(tls, v, iLoopBody)
							/* Set the P2 operand of the OP_Return opcode that will end the current
							 ** loop to point to this spot, which is the top of the next containing
							 ** loop.  The byte-code formatter will use that P2 value as a hint to
							 ** indent everything in between the this point and the final OP_Return.
							 ** See tag-20220407a in vdbe.c and shell.c */
							(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = _sqlite3VdbeCurrentAddr(tls, v)
							if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList != pOrTab {
								_sqlite3DbFreeNN(tls, db, pOrTab)
							}
							if !(untestedTerms != 0) {
								_disableTerm(tls, pLevel, pTerm)
							}
						} else {
							if int32(*(*uint32)(unsafe.Pointer(pTabItem + 12 + 4))&0x80>>7) != 0 {
								/* Tables marked isRecursive have only a single row that is stored in
								 ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
								(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = uint8(OP_Noop)
							} else {
								(*TWhereLevel)(unsafe.Pointer(pLevel)).Fop = _aStep[bRev]
								(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1 = iCur
								(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2 = int32(1) + _sqlite3VdbeAddOp2(tls, v, int32(_aStart[bRev]), iCur, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrHalt)
								(*TWhereLevel)(unsafe.Pointer(pLevel)).Fp5 = uint8(SQLITE_STMTSTATUS_FULLSCAN_STEP)
							}
						}
					}
				}
			}
		}
	}
	/* Insert code to test every subexpression that can be completely
	 ** computed using the current set of tables.
	 **
	 ** This loop may run between one and three times, depending on the
	 ** constraints to be generated. The value of stack variable iLoop
	 ** determines the constraints coded by each iteration, as follows:
	 **
	 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
	 ** iLoop==2: Code remaining expressions that do not contain correlated
	 **           sub-queries.
	 ** iLoop==3: Code all remaining expressions.
	 **
	 ** An effort is made to skip unnecessary iterations of the loop.
	 **
	 ** This optimization of causing simple query restrictions to occur before
	 ** more complex one is call the "push-down" optimization in MySQL.  Here
	 ** in SQLite, the name is "MySQL push-down", since there is also another
	 ** totally unrelated optimization called "WHERE-clause push-down".
	 ** Sometimes the qualifier is omitted, resulting in an ambiguity, so beware.
	 */
	if pIdx != 0 {
		v1 = int32(1)
	} else {
		v1 = int32(2)
	}
	iLoop = v1
	for cond := true; cond; cond = iLoop > 0 {
		iNext = 0 /* Next value for iLoop */
		pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
		j = (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm
		for {
			if !(j > 0) {
				break
			}
			skipLikeAddr = 0
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_CODED)) != 0 {
				goto _59
			}
			if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady != uint64(0) {
				libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 1, 0x2)
				goto _59
			}
			pE = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
			if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 {
				if !((*TExpr)(unsafe.Pointer(pE)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != libc.Uint32FromInt32(0)) {
					/* Defer processing WHERE clause constraints until after outer
					 ** join processing.  tag-20220513a */
					goto _59
				} else {
					if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_LEFT) == int32(JT_LEFT) && !((*TExpr)(unsafe.Pointer(pE)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) {
						goto _59
					} else {
						m = _sqlite3WhereGetMask(tls, pWInfo+496, *(*int32)(unsafe.Pointer(pE + 36)))
						if m&(*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady != 0 {
							/* An ON clause that is not ripe */
							goto _59
						}
					}
				}
			}
			if iLoop == int32(1) && !(_sqlite3ExprCoveredByIndex(tls, pE, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, pIdx) != 0) {
				iNext = int32(2)
				goto _59
			}
			if iLoop < int32(3) && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VARSELECT) != 0 {
				if iNext == 0 {
					iNext = int32(3)
				}
				goto _59
			}
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_LIKECOND) != 0 {
				/* If the TERM_LIKECOND flag is set, that means that the range search
				 ** is sufficient to guarantee that the LIKE operator is true, so we
				 ** can skip the call to the like(A,B) function.  But this only works
				 ** for strings.  So do not skip the call to the function on the pass
				 ** that compares BLOBs. */
				goto _59
			}
			_sqlite3ExprIfFalse(tls, pParse, pE, addrCont, int32(SQLITE_JUMPIFNULL))
			if skipLikeAddr != 0 {
				_sqlite3VdbeJumpHere(tls, v, skipLikeAddr)
			}
			v4 = pTerm + 10
			*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(TERM_CODED))
			goto _59
		_59:
			;
			j = j - 1
			pTerm += 48
		}
		iLoop = iNext
	}
	/* Insert code to test for implied constraints based on transitivity
	 ** of the "==" operator.
	 **
	 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
	 ** and we are coding the t1 loop and the t2 loop has not yet coded,
	 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
	 ** the implied "t1.a=123" constraint.
	 */
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
	j = (*TWhereClause)(unsafe.Pointer(pWC)).FnBase
	for {
		if !(j > 0) {
			break
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_CODED)) != 0 {
			goto _61
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) == 0 {
			goto _61
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_EQUIV) == 0 {
			goto _61
		}
		if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor != iCur {
			goto _61
		}
		if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 {
			goto _61
		}
		pE1 = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
		pAlt = _sqlite3WhereFindTerm(tls, pWC, iCur, (*(*struct {
			FleftColumn int32
			FiField     int32
		})(unsafe.Pointer(pTerm + 24))).FleftColumn, notReady, uint32(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IN)|libc.Int32FromInt32(WO_IS)), uintptr(0))
		if pAlt == uintptr(0) {
			goto _61
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pAlt)).FwtFlags)&int32(TERM_CODED) != 0 {
			goto _61
		}
		if (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pAlt)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Collate)) != uint32(0) {
			goto _61
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pAlt)).FeOperator)&int32(WO_IN) != 0 && (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pAlt)).FpExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) && (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pAlt)).FpExpr + 20)))).FpEList)).FnExpr > int32(1) {
			goto _61
		}
		**(**TExpr)(__ccgo_up(bp + 8)) = **(**TExpr)(__ccgo_up((*TWhereTerm)(unsafe.Pointer(pAlt)).FpExpr))
		(**(**TExpr)(__ccgo_up(bp + 8))).FpLeft = (*TExpr)(unsafe.Pointer(pE1)).FpLeft
		_sqlite3ExprIfFalse(tls, pParse, bp+8, addrCont, int32(SQLITE_JUMPIFNULL))
		v4 = pAlt + 10
		*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(TERM_CODED))
		goto _61
	_61:
		;
		j = j - 1
		pTerm += 48
	}
	/* For a RIGHT OUTER JOIN, record the fact that the current row has
	 ** been matched at least once.
	 */
	if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0 {
		jmp11 = 0
		pRJ = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ
		/* pTab is the right-hand table of the RIGHT JOIN.  Generate code that
		 ** will record that the current row of that table has been matched at
		 ** least once.  This is accomplished by storing the PK for the row in
		 ** both the iMatch index and the regBloom Bloom filter.
		 */
		pTab1 = (*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48))).FpSTab
		if (*TTable)(unsafe.Pointer(pTab1)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			r1 = _sqlite3GetTempRange(tls, pParse, int32(2))
			_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab1, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, -int32(1), r1+int32(1))
			nPk1 = int32(1)
		} else {
			pPk3 = _sqlite3PrimaryKeyIndex(tls, pTab1)
			nPk1 = int32((*TIndex)(unsafe.Pointer(pPk3)).FnKeyCol)
			r1 = _sqlite3GetTempRange(tls, pParse, nPk1+int32(1))
			iPk1 = 0
			for {
				if !(iPk1 < nPk1) {
					break
				}
				iCol1 = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk3)).FaiColumn + uintptr(iPk1)*2)))
				_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab1, iCur, iCol1, r1+int32(1)+iPk1)
				goto _63
			_63:
				;
				iPk1 = iPk1 + 1
			}
		}
		jmp11 = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, 0, r1+int32(1), nPk1)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), r1+int32(1), nPk1, r1)
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, r1, r1+int32(1), nPk1)
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_FilterAdd), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregBloom, 0, r1+int32(1), nPk1)
		_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_USESEEKRESULT))
		_sqlite3VdbeJumpHere(tls, v, jmp11)
		_sqlite3ReleaseTempRange(tls, pParse, r1, nPk1+int32(1))
	}
	/* For a LEFT OUTER JOIN, generate code that will record the fact that
	 ** at least one row of the right table has matched the left table.
	 */
	if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 {
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrFirst = _sqlite3VdbeCurrentAddr(tls, v)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin)
		if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ == uintptr(0) {
			goto code_outer_join_constraints /* WHERE clause constraints */
		}
	}
	if !((*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0) {
		goto _64
	}
	/* Create a subroutine used to process all interior loops and code
	 ** of the RIGHT JOIN.  During normal operation, the subroutine will
	 ** be in-line with the rest of the code.  But at the end, a separate
	 ** loop will run that invokes this subroutine for unmatched rows
	 ** of pTab, with all tables to left begin set to NULL.
	 */
	pRJ1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ
	_sqlite3VdbeAddOp2(tls, v, int32(OP_BeginSubrtn), 0, (*TWhereRightJoin)(unsafe.Pointer(pRJ1)).FregReturn)
	(*TWhereRightJoin)(unsafe.Pointer(pRJ1)).FaddrSubrtn = _sqlite3VdbeCurrentAddr(tls, v)
	(*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn = (*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn + 1
	/* WHERE clause constraints must be deferred until after outer join
	 ** row elimination has completed, since WHERE clause constraints apply
	 ** to the results of the OUTER JOIN.  The following loop generates the
	 ** appropriate WHERE clause constraint checks.  tag-20220513a.
	 */
	goto code_outer_join_constraints
code_outer_join_constraints:
	;
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
	j = libc.Int32FromInt32(0)
	for {
		if !(j < (*TWhereClause)(unsafe.Pointer(pWC)).FnBase) {
			break
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_CODED)) != 0 {
			goto _65
		}
		if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady != uint64(0) {
			goto _65
		}
		if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_LTORJ) != 0 {
			goto _65
		}
		_sqlite3ExprIfFalse(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, addrCont, int32(SQLITE_JUMPIFNULL))
		v4 = pTerm + 10
		*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(TERM_CODED))
		goto _65
	_65:
		;
		j = j + 1
		pTerm += 48
	}
_64:
	;
	return (*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady
}

// C documentation
//
//	/*
//	** Generate the end of the WHERE loop.  See comments on
//	** sqlite3WhereBegin() for additional information.
//	*/
func _sqlite3WhereEnd(tls *libc.TLS, pWInfo uintptr) {
	var addr, addrIfNull, addrSeek, bEarlyOut, i, iDb, iEnd, j, j1, k, last, m, n, n1, nRJ, op, r1, ws, x, v4 int32
	var db, p, pIdx, pIdx1, pIn, pIx, pLastOp, pLevel, pLoop, pOp, pParse, pPk, pRJ, pSrc, pTab, pTabItem, pTabList, v, v2 uintptr
	var v3, v5 bool
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrIfNull, addrSeek, bEarlyOut, db, i, iDb, iEnd, j, j1, k, last, m, n, n1, nRJ, op, p, pIdx, pIdx1, pIn, pIx, pLastOp, pLevel, pLoop, pOp, pParse, pPk, pRJ, pSrc, pTab, pTabItem, pTabList, r1, v, ws, x, v2, v3, v4, v5
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	pTabList = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	iEnd = _sqlite3VdbeCurrentAddr(tls, v)
	nRJ = 0
	addrSeek = 0
	/* Generate loop termination code.
	 */
	i = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) - int32(1)
	for {
		if !(i >= 0) {
			break
		}
		pLevel = pWInfo + 760 + uintptr(i)*88
		if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0 {
			/* Terminate the subroutine that forms the interior of the loop of
			 ** the RIGHT JOIN table */
			pRJ = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ
			_sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont)
			/* Replace addrCont with a new label that will never be used, just so
			 ** the subsequent call to resolve pLevel->addrCont will have something
			 ** to resolve. */
			(*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont = _sqlite3VdbeMakeLabel(tls, pParse)
			(*TWhereRightJoin)(unsafe.Pointer(pRJ)).FendSubrtn = _sqlite3VdbeCurrentAddr(tls, v)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregReturn, (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FaddrSubrtn, int32(1))
			nRJ = nRJ + 1
		}
		pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
		if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fop) != int32(OP_Noop) {
			if v3 = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct) == int32(WHERE_DISTINCT_ORDERED) && i == int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-int32(1) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_INDEXED) != uint32(0); v3 {
				v2 = (*(*struct {
					FnEq          Tu16
					FnBtm         Tu16
					FnTop         Tu16
					FnDistinctCol Tu16
					FpIndex       uintptr
					FpOrderBy     uintptr
				})(unsafe.Pointer(pLoop + 24))).FpIndex
				pIdx = v2
			}
			if v5 = v3 && int32(uint32(*(*uint16)(unsafe.Pointer(v2 + 56))&0x80>>7)) != 0; v5 {
				v4 = int32((*(*struct {
					FnEq          Tu16
					FnBtm         Tu16
					FnTop         Tu16
					FnDistinctCol Tu16
					FpIndex       uintptr
					FpOrderBy     uintptr
				})(unsafe.Pointer(pLoop + 24))).FnDistinctCol)
				n = v4
			}
			if v5 && v4 > 0 && int32(**(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst + uintptr(n)*2))) >= int32(36) {
				r1 = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
				addrIfNull = 0 /* Init to avoid false-positive compiler warning */
				if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 {
					addrIfNull = _sqlite3VdbeAddOp2(tls, v, int32(OP_IfNullRow), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, r1)
				}
				j = 0
				for {
					if !(j < n) {
						break
					}
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, j, r1+j)
					goto _6
				_6:
					;
					j = j + 1
				}
				**(**int32)(__ccgo_up(pParse + 48)) += n + int32(1)
				if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fop) == int32(OP_Prev) {
					v4 = int32(OP_SeekLT)
				} else {
					v4 = int32(OP_SeekGT)
				}
				op = v4
				addrSeek = _sqlite3VdbeAddOp4Int(tls, v, op, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, 0, r1, n)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), int32(1), (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2)
				if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 {
					_sqlite3VdbeJumpHere(tls, v, addrIfNull)
				}
			}
		}
		if int32(*(*uint32)(unsafe.Pointer(pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48 + 12 + 4))&0x40000>>18) != 0 {
			/* This is an EXISTS-to-JOIN optimization loop. If this loop sees a
			 ** successful row, it should break out of itself. */
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk)
		}
		_sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrCont)
		if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fop) != int32(OP_Noop) {
			_sqlite3VdbeAddOp3(tls, v, int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fop), (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1, (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2, int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fp3))
			_sqlite3VdbeChangeP5(tls, v, uint16((*TWhereLevel)(unsafe.Pointer(pLevel)).Fp5))
			if (*TWhereLevel)(unsafe.Pointer(pLevel)).FregBignull != 0 {
				_sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBignull)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_DecrJumpZero), (*TWhereLevel)(unsafe.Pointer(pLevel)).FregBignull, (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp2-int32(1))
			}
			if addrSeek != 0 {
				_sqlite3VdbeJumpHere(tls, v, addrSeek)
				addrSeek = 0
			}
		}
		if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_ABLE) != uint32(0) && (*(*struct {
			FnIn     int32
			FaInLoop uintptr
		})(unsafe.Pointer(pLevel + 68))).FnIn > 0 {
			_sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt)
			j1 = (*(*struct {
				FnIn     int32
				FaInLoop uintptr
			})(unsafe.Pointer(pLevel + 68))).FnIn
			pIn = (*(*struct {
				FnIn     int32
				FaInLoop uintptr
			})(unsafe.Pointer(pLevel + 68))).FaInLoop + uintptr(j1-int32(1))*20
			for {
				if !(j1 > 0) {
					break
				}
				_sqlite3VdbeJumpHere(tls, v, (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop+int32(1))
				if int32((*TInLoop)(unsafe.Pointer(pIn)).FeEndLoopOp) != int32(OP_Noop) {
					if (*TInLoop)(unsafe.Pointer(pIn)).FnPrefix != 0 {
						bEarlyOut = libc.BoolInt32((*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) == uint32(0) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IN_EARLYOUT) != uint32(0))
						if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 {
							/* For LEFT JOIN queries, cursor pIn->iCur may not have been
							 ** opened yet. This occurs for WHERE clauses such as
							 ** "a = ? AND b IN (...)", where the index is on (a, b). If
							 ** the RHS of the (a=?) is NULL, then the "b IN (...)" may
							 ** never have been coded, but the body of the loop run to
							 ** return the null-row. So, if the cursor is not open yet,
							 ** jump over the OP_Next or OP_Prev instruction about to
							 ** be coded.  */
							_sqlite3VdbeAddOp2(tls, v, int32(OP_IfNotOpen), (*TInLoop)(unsafe.Pointer(pIn)).FiCur, _sqlite3VdbeCurrentAddr(tls, v)+int32(2)+bEarlyOut)
						}
						if bEarlyOut != 0 {
							_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IfNoHope), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, _sqlite3VdbeCurrentAddr(tls, v)+int32(2), (*TInLoop)(unsafe.Pointer(pIn)).FiBase, (*TInLoop)(unsafe.Pointer(pIn)).FnPrefix)
							/* Retarget the OP_IsNull against the left operand of IN so
							 ** it jumps past the OP_IfNoHope.  This is because the
							 ** OP_IsNull also bypasses the OP_Affinity opcode that is
							 ** required by OP_IfNoHope. */
							_sqlite3VdbeJumpHere(tls, v, (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop+int32(1))
						}
					}
					_sqlite3VdbeAddOp2(tls, v, int32((*TInLoop)(unsafe.Pointer(pIn)).FeEndLoopOp), (*TInLoop)(unsafe.Pointer(pIn)).FiCur, (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop)
				}
				_sqlite3VdbeJumpHere(tls, v, (*TInLoop)(unsafe.Pointer(pIn)).FaddrInTop-int32(1))
				goto _8
			_8:
				;
				j1 = j1 - 1
				pIn -= 20
			}
		}
		_sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBrk)
		if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0 {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*TWhereRightJoin)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ)).FregReturn, 0, int32(1))
		}
		if (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip != 0 {
			_sqlite3VdbeGoto(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip)
			_sqlite3VdbeJumpHere(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip)
			_sqlite3VdbeJumpHere(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrSkip-int32(2))
		}
		if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin != 0 {
			ws = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags)
			addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfPos), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiLeftJoin)
			if ws&int32(WHERE_IDX_ONLY) == 0 {
				pSrc = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
				if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x40>>6) != 0 {
					n1 = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pSrc + 44)))).FregResult
					m = int32((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab)).FnCol)
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, n1, n1+m-int32(1))
				}
				_sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur)
			}
			if ws&int32(WHERE_INDEXED) != 0 || ws&int32(WHERE_MULTI_OR) != 0 && *(*uintptr)(unsafe.Pointer(&(*TWhereLevel)(unsafe.Pointer(pLevel)).Fu)) != 0 {
				if ws&int32(WHERE_MULTI_OR) != 0 {
					pIx = *(*uintptr)(unsafe.Pointer(&(*TWhereLevel)(unsafe.Pointer(pLevel)).Fu))
					iDb = _sqlite3SchemaToIndex(tls, db, (*TIndex)(unsafe.Pointer(pIx)).FpSchema)
					_sqlite3VdbeAddOp3(tls, v, int32(OP_ReopenIdx), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur, int32((*TIndex)(unsafe.Pointer(pIx)).Ftnum), iDb)
					_sqlite3VdbeSetP4KeyInfo(tls, pParse, pIx)
				}
				_sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur)
			}
			if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).Fop) == int32(OP_Return) {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TWhereLevel)(unsafe.Pointer(pLevel)).Fp1, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrFirst)
			} else {
				_sqlite3VdbeGoto(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrFirst)
			}
			_sqlite3VdbeJumpHere(tls, v, addr)
		}
		goto _1
	_1:
		;
		i = i - 1
	}
	i = 0
	pLevel = pWInfo + 760
	for {
		if !(i < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) {
			break
		}
		pIdx1 = uintptr(0)
		pTabItem = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
		pTab = (*TSrcItem)(unsafe.Pointer(pTabItem)).FpSTab
		pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
		/* Do RIGHT JOIN processing.  Generate code that will output the
		 ** unmatched rows of the right operand of the RIGHT JOIN with
		 ** all of the columns of the left operand set to NULL.
		 */
		if (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ != 0 {
			_sqlite3WhereRightJoinLoop(tls, pWInfo, i, pLevel)
			goto _9
		}
		/* For a co-routine, change all OP_Column references to the table of
		 ** the co-routine into OP_Copy of result contained in a register.
		 ** OP_Rowid becomes OP_Null.
		 */
		if int32(*(*uint32)(unsafe.Pointer(pTabItem + 12 + 4))&0x40>>6) != 0 {
			_translateColumnToCopy(tls, pParse, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBody, (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur, (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pTabItem + 44)))).FregResult, 0)
			goto _9
		}
		/* If this scan uses an index, make VDBE code substitutions to read data
		 ** from the index instead of from the table where possible.  In some cases
		 ** this optimization prevents the table from ever being read, which can
		 ** yield a significant performance boost.
		 **
		 ** Calls to the code generator in between sqlite3WhereBegin and
		 ** sqlite3WhereEnd will have created code that references the table
		 ** directly.  This loop scans all that code looking for opcodes
		 ** that reference the table and converts them into opcodes that
		 ** reference the index.
		 */
		if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_INDEXED)|libc.Int32FromInt32(WHERE_IDX_ONLY)) != 0 {
			pIdx1 = (*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FpIndex
		} else {
			if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_MULTI_OR) != 0 {
				pIdx1 = *(*uintptr)(unsafe.Pointer(&(*TWhereLevel)(unsafe.Pointer(pLevel)).Fu))
			}
		}
		if pIdx1 != 0 && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
			if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) == ONEPASS_OFF || !((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx1)).FpTable)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
				last = iEnd
			} else {
				last = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiEndWhere
			}
			if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx1 + 56))&0x800>>11)) != 0 {
				p = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr
				for p != 0 {
					if (*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur == (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur {
						(*TIndexedExpr)(unsafe.Pointer(p)).FiDataCur = -int32(1)
						(*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur = -int32(1)
					}
					p = (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext
				}
			}
			k = (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBody + int32(1)
			pOp = _sqlite3VdbeGetOp(tls, v, k)
			pLastOp = pOp + uintptr(last-k)*20
			for {
				if (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 != (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur {
					/* no-op */
				} else {
					if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) || int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Offset) {
						x = (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2
						if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Offset) {
							/* Do not need to translate the column number */
						} else {
							if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
								pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
								x = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(x)*2)))
							} else {
								x = int32(_sqlite3StorageColumnToTable(tls, pTab, int16(x)))
							}
						}
						x = _sqlite3TableColumnToIndex(tls, pIdx1, x)
						if x >= 0 {
							(*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = x
							(*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur
						} else {
							if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_IDX_ONLY)|libc.Int32FromInt32(WHERE_EXPRIDX)) != 0 {
								if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IDX_ONLY) != 0 {
									/* An error. pLoop is supposed to be a covering index loop,
									 ** and yet the VM code refers to a column of the table that
									 ** is not part of the index.  */
									_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25380, 0)
									(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_INTERNAL)
								} else {
									/* The WHERE_EXPRIDX flag is set by the planner when it is likely
									 ** that pLoop is a covering index loop, but it is not possible
									 ** to be 100% sure. In this case, any OP_Explain opcode
									 ** corresponding to this loop describes the index as a "COVERING
									 ** INDEX". But, pOp proves that pLoop is not actually a covering
									 ** index loop. So clear the WHERE_EXPRIDX flag and rewrite the
									 ** text that accompanies the OP_Explain opcode, if any.  */
									**(**Tu32)(__ccgo_up(pLoop + 40)) &= uint32(^libc.Int32FromInt32(WHERE_EXPRIDX))
									_sqlite3WhereAddExplainText(tls, pParse, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrBody-int32(1), pTabList, pLevel, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)
								}
							}
						}
					} else {
						if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Rowid) {
							(*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur
							(*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_IdxRowid)
						} else {
							if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_IfNullRow) {
								(*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur
							}
						}
					}
				}
				goto _11
			_11:
				;
				pOp += 20
				v2 = pOp
				if !(v2 < pLastOp) {
					break
				}
			}
		}
		goto _9
	_9:
		;
		i = i + 1
		pLevel += 88
	}
	/* The "break" point is here, just past the end of the outer loop.
	 ** Set it.
	 */
	_sqlite3VdbeResolveLabel(tls, v, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak)
	/* Final cleanup
	 */
	(*TParse)(unsafe.Pointer(pParse)).FnQueryLoop = int16((*TWhereInfo)(unsafe.Pointer(pWInfo)).FsavedNQueryLoop)
	_whereInfoFree(tls, db, pWInfo)
	v2 = pParse + 23
	*(*Tu8)(unsafe.Pointer(v2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v2))) - nRJ)
	return
}

/************** End of where.c ***********************************************/
/************** Begin file window.c ******************************************/
/*
** 2018 May 08
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
 */
/* #include "sqliteInt.h" */

/*
** SELECT REWRITING
**
**   Any SELECT statement that contains one or more window functions in
**   either the select list or ORDER BY clause (the only two places window
**   functions may be used) is transformed by function sqlite3WindowRewrite()
**   in order to support window function processing. For example, with the
**   schema:
**
**     CREATE TABLE t1(a, b, c, d, e, f, g);
**
**   the statement:
**
**     SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
**
**   is transformed to:
**
**     SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
**         SELECT a, e, c, d, b FROM t1 ORDER BY c, d
**     ) ORDER BY e;
**
**   The flattening optimization is disabled when processing this transformed
**   SELECT statement. This allows the implementation of the window function
**   (in this case max()) to process rows sorted in order of (c, d), which
**   makes things easier for obvious reasons. More generally:
**
**     * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
**       the sub-query.
**
**     * ORDER BY, LIMIT and OFFSET remain part of the parent query.
**
**     * Terminals from each of the expression trees that make up the
**       select-list and ORDER BY expressions in the parent query are
**       selected by the sub-query. For the purposes of the transformation,
**       terminals are column references and aggregate functions.
**
**   If there is more than one window function in the SELECT that uses
**   the same window declaration (the OVER bit), then a single scan may
**   be used to process more than one window function. For example:
**
**     SELECT max(b) OVER (PARTITION BY c ORDER BY d),
**            min(e) OVER (PARTITION BY c ORDER BY d)
**     FROM t1;
**
**   is transformed in the same way as the example above. However:
**
**     SELECT max(b) OVER (PARTITION BY c ORDER BY d),
**            min(e) OVER (PARTITION BY a ORDER BY b)
**     FROM t1;
**
**   Must be transformed to:
**
**     SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
**         SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
**           SELECT a, e, c, d, b FROM t1 ORDER BY a, b
**         ) ORDER BY c, d
**     ) ORDER BY e;
**
**   so that both min() and max() may process rows in the order defined by
**   their respective window declarations.
**
** INTERFACE WITH SELECT.C
**
**   When processing the rewritten SELECT statement, code in select.c calls
**   sqlite3WhereBegin() to begin iterating through the results of the
**   sub-query, which is always implemented as a co-routine. It then calls
**   sqlite3WindowCodeStep() to process rows and finish the scan by calling
**   sqlite3WhereEnd().
**
**   sqlite3WindowCodeStep() generates VM code so that, for each row returned
**   by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
**   When the sub-routine is invoked:
**
**     * The results of all window-functions for the row are stored
**       in the associated Window.regResult registers.
**
**     * The required terminal values are stored in the current row of
**       temp table Window.iEphCsr.
**
**   In some cases, depending on the window frame and the specific window
**   functions invoked, sqlite3WindowCodeStep() caches each entire partition
**   in a temp table before returning any rows. In other cases it does not.
**   This detail is encapsulated within this file, the code generated by
**   select.c is the same in either case.
**
** BUILT-IN WINDOW FUNCTIONS
**
**   This implementation features the following built-in window functions:
**
**     row_number()
**     rank()
**     dense_rank()
**     percent_rank()
**     cume_dist()
**     ntile(N)
**     lead(expr [, offset [, default]])
**     lag(expr [, offset [, default]])
**     first_value(expr)
**     last_value(expr)
**     nth_value(expr, N)
**
**   These are the same built-in window functions supported by Postgres.
**   Although the behaviour of aggregate window functions (functions that
**   can be used as either aggregates or window functions) allows them to
**   be implemented using an API, built-in window functions are much more
**   esoteric. Additionally, some window functions (e.g. nth_value())
**   may only be implemented by caching the entire partition in memory.
**   As such, some built-in window functions use the same API as aggregate
**   window functions and some are implemented directly using VDBE
**   instructions. Additionally, for those functions that use the API, the
**   window frame is sometimes modified before the SELECT statement is
**   rewritten. For example, regardless of the specified window frame, the
**   row_number() function always uses:
**
**     ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
**
**   See sqlite3WindowUpdate() for details.
**
**   As well as some of the built-in window functions, aggregate window
**   functions min() and max() are implemented using VDBE instructions if
**   the start of the window frame is declared as anything other than
**   UNBOUNDED PRECEDING.
 */

// C documentation
//
//	/*
//	** Add a single OP_Explain opcode that describes a Bloom filter.
//	**
//	** Or if not processing EXPLAIN QUERY PLAN and not in a SQLITE_DEBUG and/or
//	** SQLITE_ENABLE_STMT_SCANSTATUS build, then OP_Explain opcodes are not
//	** required and this routine is a no-op.
//	**
//	** If an OP_Explain opcode is added to the VM, its address is returned.
//	** Otherwise, if no OP_Explain is coded, zero is returned.
//	*/
func _sqlite3WhereExplainBloomFilter(tls *libc.TLS, pParse uintptr, pWInfo uintptr, pLevel uintptr) (r int32) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var db, pItem, pLoop, pTab, v, z, zMsg uintptr
	var i, ret int32
	var _ /* str at bp+0 */ TStrAccum
	var _ /* zBuf at bp+24 */ [100]int8
	_, _, _, _, _, _, _, _, _ = db, i, pItem, pLoop, pTab, ret, v, z, zMsg
	ret = 0
	pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* VM being constructed */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb   /* Initial space for EQP output string */
	_sqlite3StrAccumInit(tls, bp, db, bp+24, int32(100), int32(SQLITE_MAX_LENGTH))
	(**(**TStrAccum)(__ccgo_up(bp))).FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL)
	Xsqlite3_str_appendf(tls, bp, __ccgo_ts+25101, libc.VaList(bp+136, pItem))
	pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
	if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != 0 {
		pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
		if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) >= 0 {
			Xsqlite3_str_appendf(tls, bp, __ccgo_ts+24870, libc.VaList(bp+136, (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*12))).FzCnName))
		} else {
			Xsqlite3_str_appendf(tls, bp, __ccgo_ts+25122, 0)
		}
	} else {
		i = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip)
		for {
			if !(i < int32((*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FnEq)) {
				break
			}
			z = _explainIndexColumnName(tls, (*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FpIndex, i)
			if i > int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) {
				Xsqlite3_str_append(tls, bp, __ccgo_ts+24859, int32(5))
			}
			Xsqlite3_str_appendf(tls, bp, __ccgo_ts+24870, libc.VaList(bp+136, z))
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	Xsqlite3_str_append(tls, bp, __ccgo_ts+6474, int32(1))
	zMsg = _sqlite3StrAccumFinish(tls, bp)
	ret = _sqlite3VdbeAddOp4(tls, v, int32(OP_Explain), _sqlite3VdbeCurrentAddr(tls, v), (*TParse)(unsafe.Pointer(pParse)).FaddrExplain, 0, zMsg, -int32(7))
	return ret
}

// C documentation
//
//	/*
//	** These routines walk (recursively) an expression tree and generate
//	** a bitmask indicating which tables are used in that expression
//	** tree.
//	**
//	** sqlite3WhereExprUsage(MaskSet, Expr) ->
//	**
//	**       Return a Bitmask of all tables referenced by Expr.  Expr can be
//	**       be NULL, in which case 0 is returned.
//	**
//	** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
//	**
//	**       Same as sqlite3WhereExprUsage() except that Expr must not be
//	**       NULL.  The "NN" suffix on the name stands for "Not Null".
//	**
//	** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
//	**
//	**       Return a Bitmask of all tables referenced by every expression
//	**       in the expression list ExprList.  ExprList can be NULL, in which
//	**       case 0 is returned.
//	**
//	** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
//	**
//	**       Internal use only.  Called only by sqlite3WhereExprUsageNN() for
//	**       complex expressions that require pushing register values onto
//	**       the stack.  Many calls to sqlite3WhereExprUsageNN() do not need
//	**       the more complex analysis done by this routine.  Hence, the
//	**       computations done by this routine are broken out into a separate
//	**       "no-inline" function to avoid the stack push overhead in the
//	**       common case where it is not needed.
//	*/
func _sqlite3WhereExprUsageFull(tls *libc.TLS, pMaskSet uintptr, p uintptr) (r TBitmask) {
	var mask TBitmask
	var v1 uint64
	_, _ = mask, v1
	if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_IF_NULL_ROW) {
		v1 = _sqlite3WhereGetMask(tls, pMaskSet, (*TExpr)(unsafe.Pointer(p)).FiTable)
	} else {
		v1 = uint64(0)
	}
	mask = v1
	if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 {
		mask = mask | _sqlite3WhereExprUsageNN(tls, pMaskSet, (*TExpr)(unsafe.Pointer(p)).FpLeft)
	}
	if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 {
		mask = mask | _sqlite3WhereExprUsageNN(tls, pMaskSet, (*TExpr)(unsafe.Pointer(p)).FpRight)
	} else {
		if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
			if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_VarSelect)) != uint32(0) {
				(*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).FbVarSelect = int32(1)
			}
			mask = mask | _exprSelectUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(p + 20)))
		} else {
			if *(*uintptr)(unsafe.Pointer(p + 20)) != 0 {
				mask = mask | _sqlite3WhereExprListUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(p + 20)))
			}
		}
	}
	if (int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_FUNCTION) || int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_AGG_FUNCTION)) && (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_WinFunc) != uint32(0) {
		mask = mask | _sqlite3WhereExprListUsage(tls, pMaskSet, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)))).FpPartition)
		mask = mask | _sqlite3WhereExprListUsage(tls, pMaskSet, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)))).FpOrderBy)
		mask = mask | _sqlite3WhereExprUsage(tls, pMaskSet, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)))).FpFilter)
	}
	return mask
}

// C documentation
//
//	/*
//	** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
//	** where X is a reference to the iColumn of table iCur or of index pIdx
//	** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
//	** the op parameter.  Return a pointer to the term.  Return 0 if not found.
//	**
//	** If pIdx!=0 then it must be one of the indexes of table iCur.
//	** Search for terms matching the iColumn-th column of pIdx
//	** rather than the iColumn-th column of table iCur.
//	**
//	** The term returned might by Y=<expr> if there is another constraint in
//	** the WHERE clause that specifies that X=Y.  Any such constraints will be
//	** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
//	** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
//	** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
//	** other equivalent values.  Hence a search for X will return <expr> if X=A1
//	** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
//	**
//	** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
//	** then try for the one with no dependencies on <expr> - in other words where
//	** <expr> is a constant expression of some kind.  Only return entries of
//	** the form "X <op> Y" where Y is a column in another table if no terms of
//	** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
//	** exist, try to return a term that does not use WO_EQUIV.
//	*/
func _sqlite3WhereFindTerm(tls *libc.TLS, pWC uintptr, iCur int32, iColumn int32, notReady TBitmask, op Tu32, pIdx uintptr) (r uintptr) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var p, pResult uintptr
	var _ /* scan at bp+0 */ TWhereScan
	_, _ = p, pResult
	pResult = uintptr(0)
	p = _whereScanInit(tls, bp, pWC, iCur, iColumn, op, pIdx)
	op = op & uint32(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS))
	for p != 0 {
		if (*TWhereTerm)(unsafe.Pointer(p)).FprereqRight&notReady == uint64(0) {
			if (*TWhereTerm)(unsafe.Pointer(p)).FprereqRight == uint64(0) && uint32((*TWhereTerm)(unsafe.Pointer(p)).FeOperator)&op != uint32(0) {
				return p
			}
			if pResult == uintptr(0) {
				pResult = p
			}
		}
		p = _whereScanNext(tls, bp)
	}
	return pResult
}

// C documentation
//
//	/*
//	** While generating code for the min/max optimization, after handling
//	** the aggregate-step call to min() or max(), check to see if any
//	** additional looping is required.  If the output order is such that
//	** we are certain that the correct answer has already been found, then
//	** code an OP_Goto to by pass subsequent processing.
//	**
//	** Any extra OP_Goto that is coded here is an optimization.  The
//	** correct answer should be obtained regardless.  This OP_Goto just
//	** makes the answer appear faster.
//	*/
func _sqlite3WhereMinMaxOptEarlyOut(tls *libc.TLS, v uintptr, pWInfo uintptr) {
	var i int32
	var pInner uintptr
	_, _ = i, pInner
	if !(int32(uint32(*(*uint8)(unsafe.Pointer(pWInfo + 48))&0x4>>2)) != 0) {
		return
	}
	if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) == 0 {
		return
	}
	i = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) - int32(1)
	for {
		if !(i >= 0) {
			break
		}
		pInner = pWInfo + 760 + uintptr(i)*88
		if (*TWhereLoop)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pInner)).FpWLoop)).FwsFlags&uint32(WHERE_COLUMN_IN) != uint32(0) {
			_sqlite3VdbeGoto(tls, v, (*TWhereLevel)(unsafe.Pointer(pInner)).FaddrNxt)
			return
		}
		goto _1
	_1:
		;
		i = i - 1
	}
	_sqlite3VdbeGoto(tls, v, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiBreak)
}

// C documentation
//
//	/*
//	** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
//	** operate directly on the rowids returned by a WHERE clause.  Return
//	** ONEPASS_SINGLE (1) if the statement can operation directly because only
//	** a single row is to be changed.  Return ONEPASS_MULTI (2) if the one-pass
//	** optimization can be used on multiple
//	**
//	** If the ONEPASS optimization is used (if this routine returns true)
//	** then also write the indices of open cursors used by ONEPASS
//	** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
//	** table and iaCur[1] gets the cursor used by an auxiliary index.
//	** Either value may be -1, indicating that cursor is not used.
//	** Any cursors returned will have been opened for writing.
//	**
//	** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
//	** unable to use the ONEPASS optimization.
//	*/
func _sqlite3WhereOkOnePass(tls *libc.TLS, pWInfo uintptr, aiCur uintptr) (r int32) {
	libc.Xmemcpy(tls, aiCur, pWInfo+20, libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2))
	return int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass)
}

// C documentation
//
//	/*
//	** In the ORDER BY LIMIT optimization, if the inner-most loop is known
//	** to emit rows in increasing order, and if the last row emitted by the
//	** inner-most loop did not fit within the sorter, then we can skip all
//	** subsequent rows for the current iteration of the inner loop (because they
//	** will not fit in the sorter either) and continue with the second inner
//	** loop - the loop immediately outside the inner-most.
//	**
//	** When a row does not fit in the sorter (because the sorter already
//	** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
//	** label returned by this function.
//	**
//	** If the ORDER BY LIMIT optimization applies, the jump destination should
//	** be the continuation for the second-inner-most loop.  If the ORDER BY
//	** LIMIT optimization does not apply, then the jump destination should
//	** be the continuation for the inner-most loop.
//	**
//	** It is always safe for this routine to return the continuation of the
//	** inner-most loop, in the sense that a correct answer will result.
//	** Returning the continuation the second inner loop is an optimization
//	** that might make the code run a little faster, but should not change
//	** the final answer.
//	*/
func _sqlite3WhereOrderByLimitOptLabel(tls *libc.TLS, pWInfo uintptr) (r int32) {
	var pInner uintptr
	var v1 int32
	_, _ = pInner, v1
	if !(int32(uint32(*(*uint8)(unsafe.Pointer(pWInfo + 48))&0x4>>2)) != 0) {
		/* The ORDER BY LIMIT optimization does not apply.  Jump to the
		 ** continuation of the inner-most loop. */
		return (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiContinue
	}
	pInner = pWInfo + 760 + uintptr(int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-int32(1))*88
	if (*TWhereLevel)(unsafe.Pointer(pInner)).FpRJ != 0 {
		v1 = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FiContinue
	} else {
		v1 = (*TWhereLevel)(unsafe.Pointer(pInner)).FaddrNxt
	}
	return v1
}

// C documentation
//
//	/*
//	** Generate the code for the loop that finds all non-matched terms
//	** for a RIGHT JOIN.
//	*/
func _sqlite3WhereRightJoinLoop(tls *libc.TLS, pWInfo uintptr, iLevel int32, pLevel uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var addrCont, iCol, iCur, iIdxCur, iPk, jmp, k, nPk, r, v3 int32
	var mAll TBitmask
	var pFrom, pLoop, pParse, pPk, pRJ, pRight, pSubWInfo, pSubWhere, pSubq, pTab, pTabItem, pTerm, pWC, v, v4 uintptr
	var _ /* uSrc at bp+0 */ struct {
		F__ccgo_align [0]uint32
		FfromSpace    [0][56]Tu8
		FsSrc         TSrcList
		F__ccgo_pad2  [48]byte
	}
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrCont, iCol, iCur, iIdxCur, iPk, jmp, k, mAll, nPk, pFrom, pLoop, pParse, pPk, pRJ, pRight, pSubWInfo, pSubWhere, pSubq, pTab, pTabItem, pTerm, pWC, r, v, v3, v4
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	pRJ = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpRJ
	pSubWhere = uintptr(0)
	pWC = pWInfo + 80
	pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop
	pTabItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48
	mAll = uint64(0)
	_sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+25154, libc.VaList(bp+64, (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pTabItem)).FpSTab)).FzName))
	k = 0
	for {
		if !(k < iLevel) {
			break
		}
		pRight = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(k)*88))).FiFrom)*48
		mAll = mAll | (*TWhereLoop)(unsafe.Pointer((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(k)*88))).FpWLoop)).FmaskSelf
		if int32(*(*uint32)(unsafe.Pointer(pRight + 12 + 4))&0x40>>6) != 0 {
			pSubq = *(*uintptr)(unsafe.Pointer(pRight + 44))
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, (*TSubquery)(unsafe.Pointer(pSubq)).FregResult, (*TSubquery)(unsafe.Pointer(pSubq)).FregResult+(*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(pSubq)).FpSelect)).FpEList)).FnExpr-int32(1))
		}
		_sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(k)*88))).FiTabCur)
		iIdxCur = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(k)*88))).FiIdxCur
		if iIdxCur != 0 {
			_sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), iIdxCur)
		}
		goto _1
	_1:
		;
		k = k + 1
	}
	if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_LTORJ) == 0 {
		mAll = mAll | (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf
		k = 0
		for {
			if !(k < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) {
				break
			}
			pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(k)*48
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_SLICE)) != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) != int32(WO_ROWVAL) {
				break
			}
			if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll & ^mAll != 0 {
				goto _2
			}
			if (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) {
				goto _2
			}
			pSubWhere = _sqlite3ExprAnd(tls, pParse, pSubWhere, _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, 0))
			goto _2
		_2:
			;
			k = k + 1
		}
	}
	if (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur != 0 {
		/* pSubWhere may contain expressions that read from an index on the
		 ** table on the RHS of the right join. All such expressions first test
		 ** if the index is pointing at a NULL row, and if so, read from the
		 ** table cursor instead. So ensure that the index cursor really is
		 ** pointing at a NULL row here, so that no values are read from it during
		 ** the scan of the RHS of the RIGHT join below.  */
		_sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), (*TWhereLevel)(unsafe.Pointer(pLevel)).FiIdxCur)
	}
	pFrom = bp
	(*TSrcList)(unsafe.Pointer(pFrom)).FnSrc = int32(1)
	(*TSrcList)(unsafe.Pointer(pFrom)).FnAlloc = uint32(1)
	libc.Xmemcpy(tls, pFrom+8, pTabItem, uint32(48))
	(*(*TSrcItem)(unsafe.Pointer(pFrom + 8))).Ffg.Fjointype = uint8(0)
	(*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn = (*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn + 1
	pSubWInfo = _sqlite3WhereBegin(tls, pParse, pFrom, pSubWhere, uintptr(0), uintptr(0), uintptr(0), uint16(WHERE_RIGHT_JOIN), 0)
	if pSubWInfo != 0 {
		iCur = (*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur
		v4 = pParse + 48
		*(*int32)(unsafe.Pointer(v4)) = *(*int32)(unsafe.Pointer(v4)) + 1
		v3 = *(*int32)(unsafe.Pointer(v4))
		r = v3
		addrCont = _sqlite3WhereContinueLabel(tls, pSubWInfo)
		pTab = (*TSrcItem)(unsafe.Pointer(pTabItem)).FpSTab
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, -int32(1), r)
			nPk = int32(1)
		} else {
			pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
			nPk = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)
			**(**int32)(__ccgo_up(pParse + 48)) += nPk - int32(1)
			iPk = 0
			for {
				if !(iPk < nPk) {
					break
				}
				iCol = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(iPk)*2)))
				_sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, iCol, r+iPk)
				goto _5
			_5:
				;
				iPk = iPk + 1
			}
		}
		jmp = _sqlite3VdbeAddOp4Int(tls, v, int32(OP_Filter), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregBloom, 0, r, nPk)
		_sqlite3VdbeAddOp4Int(tls, v, int32(OP_Found), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch, addrCont, r, nPk)
		_sqlite3VdbeJumpHere(tls, v, jmp)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FregReturn, (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FaddrSubrtn)
		_sqlite3WhereEnd(tls, pSubWInfo)
	}
	_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSubWhere)
	_sqlite3VdbeExplainPop(tls, pParse)
	(*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn = (*TParse)(unsafe.Pointer(pParse)).FwithinRJSubrtn - 1
}

// C documentation
//
//	/*
//	** For table-valued-functions, transform the function arguments into
//	** new WHERE clause terms.
//	**
//	** Each function argument translates into an equality constraint against
//	** a HIDDEN column in the table.
//	*/
func _sqlite3WhereTabFuncArgs(tls *libc.TLS, pParse uintptr, pItem uintptr, pWC uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var j, k, v2 int32
	var joinType Tu32
	var pArgs, pColRef, pRhs, pTab, pTerm uintptr
	_, _, _, _, _, _, _, _, _ = j, joinType, k, pArgs, pColRef, pRhs, pTab, pTerm, v2
	if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x8>>3) == 0 {
		return
	}
	pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
	pArgs = *(*uintptr)(unsafe.Pointer(pItem + 32))
	if pArgs == uintptr(0) {
		return
	}
	v2 = libc.Int32FromInt32(0)
	k = v2
	j = v2
	for {
		if !(j < (*TExprList)(unsafe.Pointer(pArgs)).FnExpr) {
			break
		}
		for k < int32((*TTable)(unsafe.Pointer(pTab)).FnCol) && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(k)*12))).FcolFlags)&int32(COLFLAG_HIDDEN) == 0 {
			k = k + 1
		}
		if k >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25182, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, j))
			return
		}
		pColRef = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_COLUMN), uintptr(0), 0)
		if pColRef == uintptr(0) {
			return
		}
		(*TExpr)(unsafe.Pointer(pColRef)).FiTable = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor
		v2 = k
		k = k + 1
		(*TExpr)(unsafe.Pointer(pColRef)).FiColumn = int16(v2)
		(*TExpr)(unsafe.Pointer(pColRef)).Fy.FpTab = pTab
		**(**TBitmask)(__ccgo_up(pItem + 24)) |= _sqlite3ExprColUsed(tls, pColRef)
		pRhs = _sqlite3PExpr(tls, pParse, int32(TK_UPLUS), _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TExprList_item)(unsafe.Pointer(pArgs + 8 + uintptr(j)*20))).FpExpr, 0), uintptr(0))
		pTerm = _sqlite3PExpr(tls, pParse, int32(TK_EQ), pColRef, pRhs)
		if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_RIGHT)) != 0 {
			/* testtag-20230227a */
			/* testtag-20230227b */
			joinType = uint32(EP_OuterON)
		} else {
			/* testtag-20230227c */
			joinType = uint32(EP_InnerON)
		}
		_sqlite3SetJoinExpr(tls, pTerm, (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor, joinType)
		_whereClauseInsert(tls, pWC, pTerm, uint16(TERM_DYNAMIC))
		goto _1
	_1:
		;
		j = j + 1
	}
}

/************** End of whereexpr.c *******************************************/
/************** Begin file where.c *******************************************/
/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This module contains C code that generates VDBE code used to process
** the WHERE clause of SQL statements.  This module is responsible for
** generating the code that loops through a table looking for applicable
** rows.  Indices are selected and used to speed the search when doing
** so is applicable.  Because this module is responsible for selecting
** indices, you might also think of this module as the "query optimizer".
 */
/* #include "sqliteInt.h" */
/* #include "whereInt.h" */

// C documentation
//
//	/*
//	** Allocate and return a new Window object describing a Window Definition.
//	*/
func _sqlite3WindowAlloc(tls *libc.TLS, pParse uintptr, eType int32, eStart int32, pStart uintptr, eEnd int32, pEnd uintptr, eExclude Tu8) (r uintptr) {
	var bImplicitFrame int32
	var pWin uintptr
	_, _ = bImplicitFrame, pWin
	pWin = uintptr(0)
	bImplicitFrame = 0
	/* Parser assures the following: */
	if eType == 0 {
		bImplicitFrame = int32(1)
		eType = int32(TK_RANGE)
	}
	/* Additionally, the
	 ** starting boundary type may not occur earlier in the following list than
	 ** the ending boundary type:
	 **
	 **   UNBOUNDED PRECEDING
	 **   <expr> PRECEDING
	 **   CURRENT ROW
	 **   <expr> FOLLOWING
	 **   UNBOUNDED FOLLOWING
	 **
	 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
	 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
	 ** frame boundary.
	 */
	if eStart == int32(TK_CURRENT) && eEnd == int32(TK_PRECEDING) || eStart == int32(TK_FOLLOWING) && (eEnd == int32(TK_PRECEDING) || eEnd == int32(TK_CURRENT)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25689, 0)
		goto windowAllocErr
	}
	pWin = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(100))
	if pWin == uintptr(0) {
		goto windowAllocErr
	}
	(*TWindow)(unsafe.Pointer(pWin)).FeFrmType = uint8(eType)
	(*TWindow)(unsafe.Pointer(pWin)).FeStart = uint8(eStart)
	(*TWindow)(unsafe.Pointer(pWin)).FeEnd = uint8(eEnd)
	if int32(eExclude) == 0 && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_WindowFunc)) != uint32(0) {
		eExclude = uint8(TK_NO)
	}
	(*TWindow)(unsafe.Pointer(pWin)).FeExclude = eExclude
	(*TWindow)(unsafe.Pointer(pWin)).FbImplicitFrame = uint8(bImplicitFrame)
	(*TWindow)(unsafe.Pointer(pWin)).FpEnd = _sqlite3WindowOffsetExpr(tls, pParse, pEnd)
	(*TWindow)(unsafe.Pointer(pWin)).FpStart = _sqlite3WindowOffsetExpr(tls, pParse, pStart)
	return pWin
	goto windowAllocErr
windowAllocErr:
	;
	_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pEnd)
	_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pStart)
	return uintptr(0)
}

// C documentation
//
//	/*
//	** Attach window object pWin to expression p.
//	*/
func _sqlite3WindowAttach(tls *libc.TLS, pParse uintptr, p uintptr, pWin uintptr) {
	if p != 0 {
		*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fy)) = pWin
		**(**Tu32)(__ccgo_up(p + 4)) |= uint32(libc.Int32FromInt32(EP_WinFunc) | libc.Int32FromInt32(EP_FullSize))
		(*TWindow)(unsafe.Pointer(pWin)).FpOwner = p
		if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_Distinct) != 0 && int32((*TWindow)(unsafe.Pointer(pWin)).FeFrmType) != int32(TK_FILTER) {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25807, 0)
		}
	} else {
		_sqlite3WindowDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pWin)
	}
}

// C documentation
//
//	/*
//	** This is called by code in select.c before it calls sqlite3WhereBegin()
//	** to begin iterating through the sub-query results. It is used to allocate
//	** and initialize registers and cursors used by sqlite3WindowCodeStep().
//	*/
func _sqlite3WindowCodeInit(tls *libc.TLS, pParse uintptr, pSelect uintptr) {
	var nEphExpr, nExpr, v1 int32
	var p, pKeyInfo, pList, pMWin, pWin, v, v2 uintptr
	_, _, _, _, _, _, _, _, _, _ = nEphExpr, nExpr, p, pKeyInfo, pList, pMWin, pWin, v, v1, v2
	nEphExpr = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8 + 44)))).FpSelect)).FpEList)).FnExpr
	pMWin = (*TSelect)(unsafe.Pointer(pSelect)).FpWin
	v = _sqlite3GetVdbe(tls, pParse)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr, nEphExpr)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr+int32(1), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr+int32(2), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr+int32(3), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr)
	/* Allocate registers to use for PARTITION BY values, if any. Initialize
	 ** said registers to NULL.  */
	if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 {
		nExpr = (*TExprList)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pMWin)).FpPartition)).FnExpr
		(*TWindow)(unsafe.Pointer(pMWin)).FregPart = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += nExpr
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pMWin)).FregPart, (*TWindow)(unsafe.Pointer(pMWin)).FregPart+nExpr-int32(1))
	}
	v2 = pParse + 48
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	v1 = *(*int32)(unsafe.Pointer(v2))
	(*TWindow)(unsafe.Pointer(pMWin)).FregOne = v1
	_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), (*TWindow)(unsafe.Pointer(pMWin)).FregOne)
	if (*TWindow)(unsafe.Pointer(pMWin)).FeExclude != 0 {
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		(*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid = v1
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		(*TWindow)(unsafe.Pointer(pMWin)).FregEndRowid = v1
		v2 = pParse + 44
		v1 = *(*int32)(unsafe.Pointer(v2))
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		(*TWindow)(unsafe.Pointer(pMWin)).FcsrApp = v1
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWindow)(unsafe.Pointer(pMWin)).FregEndRowid)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pMWin)).FcsrApp, (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr)
		return
	}
	pWin = pMWin
	for {
		if !(pWin != 0) {
			break
		}
		p = (*TWindow)(unsafe.Pointer(pWin)).FpWFunc
		if (*TFuncDef)(unsafe.Pointer(p)).FfuncFlags&uint32(SQLITE_FUNC_MINMAX) != 0 && int32((*TWindow)(unsafe.Pointer(pWin)).FeStart) != int32(TK_UNBOUNDED) {
			pList = *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20))
			pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, pList, 0, 0)
			v2 = pParse + 44
			v1 = *(*int32)(unsafe.Pointer(v2))
			*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
			(*TWindow)(unsafe.Pointer(pWin)).FcsrApp = v1
			(*TWindow)(unsafe.Pointer(pWin)).FregApp = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
			**(**int32)(__ccgo_up(pParse + 48)) += int32(3)
			if pKeyInfo != 0 && int32(**(**int8)(__ccgo_up((*TFuncDef)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpWFunc)).FzName + 1))) == int32('i') {
				**(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags)) = uint8(KEYINFO_ORDER_DESC)
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, int32(2))
			_sqlite3VdbeAppendP4(tls, v, pKeyInfo, -int32(9))
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(1))
		} else {
			if (*TFuncDef)(unsafe.Pointer(p)).FzName == uintptr(unsafe.Pointer(&_nth_valueName)) || (*TFuncDef)(unsafe.Pointer(p)).FzName == uintptr(unsafe.Pointer(&_first_valueName)) {
				/* Allocate two registers at pWin->regApp. These will be used to
				 ** store the start and end index of the current frame.  */
				(*TWindow)(unsafe.Pointer(pWin)).FregApp = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
				v2 = pParse + 44
				v1 = *(*int32)(unsafe.Pointer(v2))
				*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
				(*TWindow)(unsafe.Pointer(pWin)).FcsrApp = v1
				**(**int32)(__ccgo_up(pParse + 48)) += int32(2)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr)
			} else {
				if (*TFuncDef)(unsafe.Pointer(p)).FzName == uintptr(unsafe.Pointer(&_leadName)) || (*TFuncDef)(unsafe.Pointer(p)).FzName == uintptr(unsafe.Pointer(&_lagName)) {
					v2 = pParse + 44
					v1 = *(*int32)(unsafe.Pointer(v2))
					*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
					(*TWindow)(unsafe.Pointer(pWin)).FcsrApp = v1
					_sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr)
				}
			}
		}
		goto _9
	_9:
		;
		pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin
	}
}

// C documentation
//
//	/*
//	** sqlite3WhereBegin() has already been called for the SELECT statement
//	** passed as the second argument when this function is invoked. It generates
//	** code to populate the Window.regResult register for each window function
//	** and invoke the sub-routine at instruction addrGosub once for each row.
//	** sqlite3WhereEnd() is always called before returning.
//	**
//	** This function handles several different types of window frames, which
//	** require slightly different processing. The following pseudo code is
//	** used to implement window frames of the form:
//	**
//	**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
//	**
//	** Other window frame types use variants of the following:
//	**
//	**     ... loop started by sqlite3WhereBegin() ...
//	**       if( new partition ){
//	**         Gosub flush
//	**       }
//	**       Insert new row into eph table.
//	**
//	**       if( first row of partition ){
//	**         // Rewind three cursors, all open on the eph table.
//	**         Rewind(csrEnd);
//	**         Rewind(csrStart);
//	**         Rewind(csrCurrent);
//	**
//	**         regEnd = <expr2>          // FOLLOWING expression
//	**         regStart = <expr1>        // PRECEDING expression
//	**       }else{
//	**         // First time this branch is taken, the eph table contains two
//	**         // rows. The first row in the partition, which all three cursors
//	**         // currently point to, and the following row.
//	**         AGGSTEP
//	**         if( (regEnd--)<=0 ){
//	**           RETURN_ROW
//	**           if( (regStart--)<=0 ){
//	**             AGGINVERSE
//	**           }
//	**         }
//	**       }
//	**     }
//	**     flush:
//	**       AGGSTEP
//	**       while( 1 ){
//	**         RETURN ROW
//	**         if( csrCurrent is EOF ) break;
//	**         if( (regStart--)<=0 ){
//	**           AggInverse(csrStart)
//	**           Next(csrStart)
//	**         }
//	**       }
//	**
//	** The pseudo-code above uses the following shorthand:
//	**
//	**   AGGSTEP:    invoke the aggregate xStep() function for each window function
//	**               with arguments read from the current row of cursor csrEnd, then
//	**               step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
//	**
//	**   RETURN_ROW: return a row to the caller based on the contents of the
//	**               current row of csrCurrent and the current state of all
//	**               aggregates. Then step cursor csrCurrent forward one row.
//	**
//	**   AGGINVERSE: invoke the aggregate xInverse() function for each window
//	**               functions with arguments read from the current row of cursor
//	**               csrStart. Then step csrStart forward one row.
//	**
//	** There are two other ROWS window frames that are handled significantly
//	** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
//	** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special
//	** cases because they change the order in which the three cursors (csrStart,
//	** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
//	** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
//	** three.
//	**
//	**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
//	**
//	**     ... loop started by sqlite3WhereBegin() ...
//	**       if( new partition ){
//	**         Gosub flush
//	**       }
//	**       Insert new row into eph table.
//	**       if( first row of partition ){
//	**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
//	**         regEnd = <expr2>
//	**         regStart = <expr1>
//	**       }else{
//	**         if( (regEnd--)<=0 ){
//	**           AGGSTEP
//	**         }
//	**         RETURN_ROW
//	**         if( (regStart--)<=0 ){
//	**           AGGINVERSE
//	**         }
//	**       }
//	**     }
//	**     flush:
//	**       if( (regEnd--)<=0 ){
//	**         AGGSTEP
//	**       }
//	**       RETURN_ROW
//	**
//	**
//	**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
//	**
//	**   ... loop started by sqlite3WhereBegin() ...
//	**     if( new partition ){
//	**       Gosub flush
//	**     }
//	**     Insert new row into eph table.
//	**     if( first row of partition ){
//	**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
//	**       regEnd = <expr2>
//	**       regStart = regEnd - <expr1>
//	**     }else{
//	**       AGGSTEP
//	**       if( (regEnd--)<=0 ){
//	**         RETURN_ROW
//	**       }
//	**       if( (regStart--)<=0 ){
//	**         AGGINVERSE
//	**       }
//	**     }
//	**   }
//	**   flush:
//	**     AGGSTEP
//	**     while( 1 ){
//	**       if( (regEnd--)<=0 ){
//	**         RETURN_ROW
//	**         if( eof ) break;
//	**       }
//	**       if( (regStart--)<=0 ){
//	**         AGGINVERSE
//	**         if( eof ) break
//	**       }
//	**     }
//	**     while( !eof csrCurrent ){
//	**       RETURN_ROW
//	**     }
//	**
//	** For the most part, the patterns above are adapted to support UNBOUNDED by
//	** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
//	** CURRENT ROW by assuming that it is equivalent to "0 PRECEDING/FOLLOWING".
//	** This is optimized of course - branches that will never be taken and
//	** conditions that are always true are omitted from the VM code. The only
//	** exceptional case is:
//	**
//	**   ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
//	**
//	**     ... loop started by sqlite3WhereBegin() ...
//	**     if( new partition ){
//	**       Gosub flush
//	**     }
//	**     Insert new row into eph table.
//	**     if( first row of partition ){
//	**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
//	**       regStart = <expr1>
//	**     }else{
//	**       AGGSTEP
//	**     }
//	**   }
//	**   flush:
//	**     AGGSTEP
//	**     while( 1 ){
//	**       if( (regStart--)<=0 ){
//	**         AGGINVERSE
//	**         if( eof ) break
//	**       }
//	**       RETURN_ROW
//	**     }
//	**     while( !eof csrCurrent ){
//	**       RETURN_ROW
//	**     }
//	**
//	** Also requiring special handling are the cases:
//	**
//	**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
//	**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
//	**
//	** when (expr1 < expr2). This is detected at runtime, not by this function.
//	** To handle this case, the pseudo-code programs depicted above are modified
//	** slightly to be:
//	**
//	**     ... loop started by sqlite3WhereBegin() ...
//	**     if( new partition ){
//	**       Gosub flush
//	**     }
//	**     Insert new row into eph table.
//	**     if( first row of partition ){
//	**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
//	**       regEnd = <expr2>
//	**       regStart = <expr1>
//	**       if( regEnd < regStart ){
//	**         RETURN_ROW
//	**         delete eph table contents
//	**         continue
//	**       }
//	**     ...
//	**
//	** The new "continue" statement in the above jumps to the next iteration
//	** of the outer loop - the one started by sqlite3WhereBegin().
//	**
//	** The various GROUPS cases are implemented using the same patterns as
//	** ROWS. The VM code is modified slightly so that:
//	**
//	**   1. The else branch in the main loop is only taken if the row just
//	**      added to the ephemeral table is the start of a new group. In
//	**      other words, it becomes:
//	**
//	**         ... loop started by sqlite3WhereBegin() ...
//	**         if( new partition ){
//	**           Gosub flush
//	**         }
//	**         Insert new row into eph table.
//	**         if( first row of partition ){
//	**           Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
//	**           regEnd = <expr2>
//	**           regStart = <expr1>
//	**         }else if( new group ){
//	**           ...
//	**         }
//	**       }
//	**
//	**   2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or
//	**      AGGINVERSE step processes the current row of the relevant cursor and
//	**      all subsequent rows belonging to the same group.
//	**
//	** RANGE window frames are a little different again. As for GROUPS, the
//	** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
//	** deal in groups instead of rows. As for ROWS and GROUPS, there are three
//	** basic cases:
//	**
//	**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
//	**
//	**     ... loop started by sqlite3WhereBegin() ...
//	**       if( new partition ){
//	**         Gosub flush
//	**       }
//	**       Insert new row into eph table.
//	**       if( first row of partition ){
//	**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
//	**         regEnd = <expr2>
//	**         regStart = <expr1>
//	**       }else{
//	**         AGGSTEP
//	**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
//	**           RETURN_ROW
//	**           while( csrStart.key + regStart) < csrCurrent.key ){
//	**             AGGINVERSE
//	**           }
//	**         }
//	**       }
//	**     }
//	**     flush:
//	**       AGGSTEP
//	**       while( 1 ){
//	**         RETURN ROW
//	**         if( csrCurrent is EOF ) break;
//	**           while( csrStart.key + regStart) < csrCurrent.key ){
//	**             AGGINVERSE
//	**           }
//	**         }
//	**       }
//	**
//	** In the above notation, "csr.key" means the current value of the ORDER BY
//	** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
//	** or <expr PRECEDING) read from cursor csr.
//	**
//	**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
//	**
//	**     ... loop started by sqlite3WhereBegin() ...
//	**       if( new partition ){
//	**         Gosub flush
//	**       }
//	**       Insert new row into eph table.
//	**       if( first row of partition ){
//	**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
//	**         regEnd = <expr2>
//	**         regStart = <expr1>
//	**       }else{
//	**         while( (csrEnd.key + regEnd) <= csrCurrent.key ){
//	**           AGGSTEP
//	**         }
//	**         while( (csrStart.key + regStart) < csrCurrent.key ){
//	**           AGGINVERSE
//	**         }
//	**         RETURN_ROW
//	**       }
//	**     }
//	**     flush:
//	**       while( (csrEnd.key + regEnd) <= csrCurrent.key ){
//	**         AGGSTEP
//	**       }
//	**       while( (csrStart.key + regStart) < csrCurrent.key ){
//	**         AGGINVERSE
//	**       }
//	**       RETURN_ROW
//	**
//	**   RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
//	**
//	**     ... loop started by sqlite3WhereBegin() ...
//	**       if( new partition ){
//	**         Gosub flush
//	**       }
//	**       Insert new row into eph table.
//	**       if( first row of partition ){
//	**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
//	**         regEnd = <expr2>
//	**         regStart = <expr1>
//	**       }else{
//	**         AGGSTEP
//	**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
//	**           while( (csrCurrent.key + regStart) > csrStart.key ){
//	**             AGGINVERSE
//	**           }
//	**           RETURN_ROW
//	**         }
//	**       }
//	**     }
//	**     flush:
//	**       AGGSTEP
//	**       while( 1 ){
//	**         while( (csrCurrent.key + regStart) > csrStart.key ){
//	**           AGGINVERSE
//	**           if( eof ) break "while( 1 )" loop.
//	**         }
//	**         RETURN_ROW
//	**       }
//	**       while( !eof csrCurrent ){
//	**         RETURN_ROW
//	**       }
//	**
//	** The text above leaves out many details. Refer to the code and comments
//	** below for a more complete picture.
//	*/
func _sqlite3WindowCodeStep(tls *libc.TLS, pParse uintptr, p uintptr, pWInfo uintptr, regGosub int32, addrGosub int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var addr, addr1, addrBreak, addrBreak1, addrBreak2, addrBreak3, addrEmpty, addrGe, addrGosubFlush, addrInteger, addrNe, addrNext, addrStart, addrStart1, bRPS, bRPS1, csrInput, csrWrite, iInput, lbl, lbl1, lblWhereEnd, nInput, nPart, nPeer, op, regEnd, regFlushPart, regNew, regNewPart, regNewPeer, regPeer, regRecord, regStart, v1 int32
	var pKeyInfo, pMWin, pOrderBy, pPart, v, v2 uintptr
	var _ /* s at bp+0 */ TWindowCodeArg
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addr1, addrBreak, addrBreak1, addrBreak2, addrBreak3, addrEmpty, addrGe, addrGosubFlush, addrInteger, addrNe, addrNext, addrStart, addrStart1, bRPS, bRPS1, csrInput, csrWrite, iInput, lbl, lbl1, lblWhereEnd, nInput, nPart, nPeer, op, pKeyInfo, pMWin, pOrderBy, pPart, regEnd, regFlushPart, regNew, regNewPart, regNewPeer, regPeer, regRecord, regStart, v, v1, v2
	pMWin = (*TSelect)(unsafe.Pointer(p)).FpWin
	pOrderBy = (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy
	v = _sqlite3GetVdbe(tls, pParse)                                                                                                /* Cursor used to write to eph. table */
	csrInput = (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FiCursor                                     /* Cursor of sub-select */
	nInput = int32((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSTab)).FnCol) /* Address of OP_Ne */
	addrGosubFlush = 0                                                                                                              /* Address of OP_Gosub to flush: */
	addrInteger = 0                                                                                                                 /* regNew array in record form */
	regNewPeer = 0                                                                                                                  /* Peer values for new row (part of regNew) */
	regPeer = 0                                                                                                                     /* Peer values for current row */
	regFlushPart = 0                                                                                                                /* Label just before sqlite3WhereEnd() code */
	regStart = 0                                                                                                                    /* Value of <expr> PRECEDING */
	regEnd = 0                                                                                                                      /* Value of <expr> FOLLOWING */
	lblWhereEnd = _sqlite3VdbeMakeLabel(tls, pParse)
	/* Fill in the context object */
	libc.Xmemset(tls, bp, 0, uint32(56))
	(**(**TWindowCodeArg)(__ccgo_up(bp))).FpParse = pParse
	(**(**TWindowCodeArg)(__ccgo_up(bp))).FpMWin = pMWin
	(**(**TWindowCodeArg)(__ccgo_up(bp))).FpVdbe = v
	(**(**TWindowCodeArg)(__ccgo_up(bp))).FregGosub = regGosub
	(**(**TWindowCodeArg)(__ccgo_up(bp))).FaddrGosub = addrGosub
	(**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr = (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr
	csrWrite = (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr + int32(1)
	(**(**TWindowCodeArg)(__ccgo_up(bp))).Fstart.Fcsr = (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr + int32(2)
	(**(**TWindowCodeArg)(__ccgo_up(bp))).Fend.Fcsr = (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr + int32(3)
	/* Figure out when rows may be deleted from the ephemeral table. There
	 ** are four options - they may never be deleted (eDelete==0), they may
	 ** be deleted as soon as they are no longer part of the window frame
	 ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row
	 ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
	 ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
	switch int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) {
	case int32(TK_FOLLOWING):
		if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_RANGE) && _windowExprGtZero(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpStart) != 0 {
			(**(**TWindowCodeArg)(__ccgo_up(bp))).FeDelete = int32(WINDOW_RETURN_ROW)
		}
	case int32(TK_UNBOUNDED):
		if _windowCacheFrame(tls, pMWin) == 0 {
			if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_PRECEDING) {
				if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_RANGE) && _windowExprGtZero(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpEnd) != 0 {
					(**(**TWindowCodeArg)(__ccgo_up(bp))).FeDelete = int32(WINDOW_AGGSTEP)
				}
			} else {
				(**(**TWindowCodeArg)(__ccgo_up(bp))).FeDelete = int32(WINDOW_RETURN_ROW)
			}
		}
	default:
		(**(**TWindowCodeArg)(__ccgo_up(bp))).FeDelete = int32(WINDOW_AGGINVERSE)
		break
	}
	/* Allocate registers for the array of values from the sub-query, the
	 ** same values in record form, and the rowid used to insert said record
	 ** into the ephemeral table.  */
	regNew = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
	**(**int32)(__ccgo_up(pParse + 48)) += nInput
	v2 = pParse + 48
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	v1 = *(*int32)(unsafe.Pointer(v2))
	regRecord = v1
	v2 = pParse + 48
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	v1 = *(*int32)(unsafe.Pointer(v2))
	(**(**TWindowCodeArg)(__ccgo_up(bp))).FregRowid = v1
	/* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
	 ** clause, allocate registers to store the results of evaluating each
	 ** <expr>.  */
	if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_PRECEDING) || int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) {
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		regStart = v1
	}
	if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_PRECEDING) || int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_FOLLOWING) {
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		regEnd = v1
	}
	/* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
	 ** registers to store copies of the ORDER BY expressions (peer values)
	 ** for the main loop, and for each cursor (start, current and end). */
	if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_ROWS) {
		if pOrderBy != 0 {
			v1 = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr
		} else {
			v1 = 0
		}
		nPeer = v1
		regNewPeer = regNew + (*TWindow)(unsafe.Pointer(pMWin)).FnBufferCol
		if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 {
			regNewPeer = regNewPeer + (*TExprList)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pMWin)).FpPartition)).FnExpr
		}
		regPeer = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += nPeer
		(**(**TWindowCodeArg)(__ccgo_up(bp))).Fstart.Freg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += nPeer
		(**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Freg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += nPeer
		(**(**TWindowCodeArg)(__ccgo_up(bp))).Fend.Freg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
		**(**int32)(__ccgo_up(pParse + 48)) += nPeer
	}
	/* Load the column values for the row returned by the sub-select
	 ** into an array of registers starting at regNew. Assemble them into
	 ** a record in register regRecord. */
	iInput = 0
	for {
		if !(iInput < nInput) {
			break
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), csrInput, iInput, regNew+iInput)
		goto _10
	_10:
		;
		iInput = iInput + 1
	}
	_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regNew, nInput, regRecord)
	/* An input row has just been read into an array of registers starting
	 ** at regNew. If the window has a PARTITION clause, this block generates
	 ** VM code to check if the input row is the start of a new partition.
	 ** If so, it does an OP_Gosub to an address to be filled in later. The
	 ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
	if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 {
		pPart = (*TWindow)(unsafe.Pointer(pMWin)).FpPartition
		nPart = (*TExprList)(unsafe.Pointer(pPart)).FnExpr
		regNewPart = regNew + (*TWindow)(unsafe.Pointer(pMWin)).FnBufferCol
		pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, pPart, 0, 0)
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		regFlushPart = v1
		addr = _sqlite3VdbeAddOp3(tls, v, int32(OP_Compare), regNewPart, (*TWindow)(unsafe.Pointer(pMWin)).FregPart, nPart)
		_sqlite3VdbeAppendP4(tls, v, pKeyInfo, -int32(9))
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Jump), addr+int32(2), addr+int32(4), addr+int32(2))
		addrGosubFlush = _sqlite3VdbeAddOp1(tls, v, int32(OP_Gosub), regFlushPart)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regNewPart, (*TWindow)(unsafe.Pointer(pMWin)).FregPart, nPart-int32(1))
	}
	/* Insert the new row into the ephemeral table */
	_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), csrWrite, (**(**TWindowCodeArg)(__ccgo_up(bp))).FregRowid)
	_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), csrWrite, regRecord, (**(**TWindowCodeArg)(__ccgo_up(bp))).FregRowid)
	addrNe = _sqlite3VdbeAddOp3(tls, v, int32(OP_Ne), (*TWindow)(unsafe.Pointer(pMWin)).FregOne, 0, (**(**TWindowCodeArg)(__ccgo_up(bp))).FregRowid)
	/* This block is run for the first row of each partition */
	(**(**TWindowCodeArg)(__ccgo_up(bp))).FregArg = _windowInitAccum(tls, pParse, pMWin)
	if regStart != 0 {
		_sqlite3ExprCode(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpStart, regStart)
		if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) {
			v1 = int32(3)
		} else {
			v1 = 0
		}
		_windowCheckValue(tls, pParse, regStart, 0+v1)
	}
	if regEnd != 0 {
		_sqlite3ExprCode(tls, pParse, (*TWindow)(unsafe.Pointer(pMWin)).FpEnd, regEnd)
		if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) {
			v1 = int32(3)
		} else {
			v1 = 0
		}
		_windowCheckValue(tls, pParse, regEnd, int32(1)+v1)
	}
	if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_RANGE) && int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) && regStart != 0 {
		if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) {
			v1 = int32(OP_Ge)
		} else {
			v1 = int32(OP_Le)
		}
		op = v1
		addrGe = _sqlite3VdbeAddOp3(tls, v, op, regStart, 0, regEnd)
		/* NeverNull because bound <expr> */
		/*   values previously checked */
		_windowAggFinal(tls, bp, 0)
		_sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr)
		_windowReturnOneRow(tls, bp)
		_sqlite3VdbeAddOp1(tls, v, int32(OP_ResetSorter), (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, lblWhereEnd)
		_sqlite3VdbeJumpHere(tls, v, addrGe)
	}
	if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) && int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) != int32(TK_RANGE) && regEnd != 0 {
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Subtract), regStart, regEnd, regStart)
	}
	if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) != int32(TK_UNBOUNDED) {
		_sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (**(**TWindowCodeArg)(__ccgo_up(bp))).Fstart.Fcsr)
	}
	_sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr)
	_sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), (**(**TWindowCodeArg)(__ccgo_up(bp))).Fend.Fcsr)
	if regPeer != 0 && pOrderBy != 0 {
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regNewPeer, regPeer, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr-int32(1))
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regPeer, (**(**TWindowCodeArg)(__ccgo_up(bp))).Fstart.Freg, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr-int32(1))
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regPeer, (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Freg, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr-int32(1))
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), regPeer, (**(**TWindowCodeArg)(__ccgo_up(bp))).Fend.Freg, (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr-int32(1))
	}
	_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, lblWhereEnd)
	_sqlite3VdbeJumpHere(tls, v, addrNe)
	/* Beginning of the block executed for the second and subsequent rows. */
	if regPeer != 0 {
		_windowIfNewPeer(tls, pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd)
	}
	if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) {
		_windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), 0, 0)
		if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) != int32(TK_UNBOUNDED) {
			if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) {
				lbl = _sqlite3VdbeMakeLabel(tls, pParse)
				addrNext = _sqlite3VdbeCurrentAddr(tls, v)
				_windowCodeRangeTest(tls, bp, int32(OP_Ge), (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr, regEnd, (**(**TWindowCodeArg)(__ccgo_up(bp))).Fend.Fcsr, lbl)
				_windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0)
				_windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrNext)
				_sqlite3VdbeResolveLabel(tls, v, lbl)
			} else {
				_windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), regEnd, 0)
				_windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0)
			}
		}
	} else {
		if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_PRECEDING) {
			bRPS = libc.BoolInt32(int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_PRECEDING) && int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE))
			_windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), regEnd, 0)
			if bRPS != 0 {
				_windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0)
			}
			_windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0)
			if !(bRPS != 0) {
				_windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0)
			}
		} else {
			addr1 = 0
			_windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), 0, 0)
			if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) != int32(TK_UNBOUNDED) {
				if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) {
					lbl1 = 0
					addr1 = _sqlite3VdbeCurrentAddr(tls, v)
					if regEnd != 0 {
						lbl1 = _sqlite3VdbeMakeLabel(tls, pParse)
						_windowCodeRangeTest(tls, bp, int32(OP_Ge), (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr, regEnd, (**(**TWindowCodeArg)(__ccgo_up(bp))).Fend.Fcsr, lbl1)
					}
					_windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0)
					_windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0)
					if regEnd != 0 {
						_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addr1)
						_sqlite3VdbeResolveLabel(tls, v, lbl1)
					}
				} else {
					if regEnd != 0 {
						addr1 = _sqlite3VdbeAddOp3(tls, v, int32(OP_IfPos), regEnd, 0, int32(1))
					}
					_windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0)
					_windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0)
					if regEnd != 0 {
						_sqlite3VdbeJumpHere(tls, v, addr1)
					}
				}
			}
		}
	}
	/* End of the main input loop */
	_sqlite3VdbeResolveLabel(tls, v, lblWhereEnd)
	_sqlite3WhereEnd(tls, pWInfo)
	/* Fall through */
	if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 {
		addrInteger = _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regFlushPart)
		_sqlite3VdbeJumpHere(tls, v, addrGosubFlush)
	}
	(**(**TWindowCodeArg)(__ccgo_up(bp))).FregRowid = 0
	addrEmpty = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), csrWrite)
	if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_PRECEDING) {
		bRPS1 = libc.BoolInt32(int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_PRECEDING) && int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE))
		_windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), regEnd, 0)
		if bRPS1 != 0 {
			_windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0)
		}
		_windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, 0)
	} else {
		if int32((*TWindow)(unsafe.Pointer(pMWin)).FeStart) == int32(TK_FOLLOWING) {
			_windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), 0, 0)
			if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) {
				addrStart = _sqlite3VdbeCurrentAddr(tls, v)
				addrBreak2 = _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, int32(1))
				addrBreak1 = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, int32(1))
			} else {
				if int32((*TWindow)(unsafe.Pointer(pMWin)).FeEnd) == int32(TK_UNBOUNDED) {
					addrStart = _sqlite3VdbeCurrentAddr(tls, v)
					addrBreak1 = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), regStart, int32(1))
					addrBreak2 = _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), 0, int32(1))
				} else {
					/* assert( regStart>=0 );
					 ** regEnd = regEnd - regStart;
					 ** regStart = 0;   */
					_sqlite3VdbeAddOp3(tls, v, int32(OP_Subtract), regStart, regEnd, regEnd)
					_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regStart)
					addrStart = _sqlite3VdbeCurrentAddr(tls, v)
					addrBreak1 = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), regEnd, int32(1))
					addrBreak2 = _windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, int32(1))
				}
			}
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrStart)
			_sqlite3VdbeJumpHere(tls, v, addrBreak2)
			addrStart = _sqlite3VdbeCurrentAddr(tls, v)
			addrBreak3 = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, int32(1))
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrStart)
			_sqlite3VdbeJumpHere(tls, v, addrBreak1)
			_sqlite3VdbeJumpHere(tls, v, addrBreak3)
		} else {
			_windowCodeOp(tls, bp, int32(WINDOW_AGGSTEP), 0, 0)
			addrStart1 = _sqlite3VdbeCurrentAddr(tls, v)
			addrBreak = _windowCodeOp(tls, bp, int32(WINDOW_RETURN_ROW), 0, int32(1))
			_windowCodeOp(tls, bp, int32(WINDOW_AGGINVERSE), regStart, 0)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrStart1)
			_sqlite3VdbeJumpHere(tls, v, addrBreak)
		}
	}
	_sqlite3VdbeJumpHere(tls, v, addrEmpty)
	_sqlite3VdbeAddOp1(tls, v, int32(OP_ResetSorter), (**(**TWindowCodeArg)(__ccgo_up(bp))).Fcurrent.Fcsr)
	if (*TWindow)(unsafe.Pointer(pMWin)).FpPartition != 0 {
		if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid != 0 {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32(1), (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, (*TWindow)(unsafe.Pointer(pMWin)).FregEndRowid)
		}
		_sqlite3VdbeChangeP1(tls, v, addrInteger, _sqlite3VdbeCurrentAddr(tls, v))
		_sqlite3VdbeAddOp1(tls, v, int32(OP_Return), regFlushPart)
	}
}

/************** End of window.c **********************************************/
/************** Begin file parse.c *******************************************/
/* This file is automatically generated by Lemon from input grammar
** source file "parse.y".
 */
/*
** 2001-09-15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains SQLite's SQL parser.
**
** The canonical source code to this file ("parse.y") is a Lemon grammar
** file that specifies the input grammar and actions to take while parsing.
** That input file is processed by Lemon to generate a C-language
** implementation of a parser for the given grammar.  You might be reading
** this comment as part of the translated C-code.  Edits should be made
** to the original parse.y sources.
 */

/* #include "sqliteInt.h" */

/*
** Verify that the pParse->isCreate field is set
 */

/*
** Disable all error recovery processing in the parser push-down
** automaton.
 */

/*
** Make yytestcase() the same as testcase()
 */

/*
** Indicate that sqlite3ParserFree() will never be called with a null
** pointer.
 */

/*
** In the amalgamation, the parse.c file generated by lemon and the
** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
** has access to the the size of the yyParser object and so the parser
** engine can be allocated from stack.  In that case, only the
** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
** omitted.
 */

/*
** Alternative datatype for the argument to the malloc() routine passed
** into sqlite3ParserAlloc().  The default is size_t.
 */

// C documentation
//
//	/*
//	** Register those built-in window functions that are not also aggregates.
//	*/
func _sqlite3WindowFunctions(tls *libc.TLS) {
	_sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aWindowFuncs)), int32(libc.Uint32FromInt64(600)/libc.Uint32FromInt64(40)))
}

// C documentation
//
//	/*
//	** If the SELECT statement passed as the second argument does not invoke
//	** any SQL window functions, this function is a no-op. Otherwise, it
//	** rewrites the SELECT statement so that window function xStep functions
//	** are invoked in the correct order as described under "SELECT REWRITING"
//	** at the top of this file.
//	*/
func _sqlite3WindowRewrite(tls *libc.TLS, pParse uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pArgs, pFilter, pGroupBy, pHaving, pMWin, pSort, pSrc, pSub, pTab, pTab2, pWhere, pWin, v, v2 uintptr
	var nSave, rc, v1 int32
	var selFlags Tu32
	var _ /* pSublist at bp+0 */ uintptr
	var _ /* w at bp+4 */ TWalker
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = db, nSave, pArgs, pFilter, pGroupBy, pHaving, pMWin, pSort, pSrc, pSub, pTab, pTab2, pWhere, pWin, rc, selFlags, v, v1, v2
	rc = SQLITE_OK
	if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 && (*TSelect)(unsafe.Pointer(p)).FpPrior == uintptr(0) && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_WinRewrite) == uint32(0) && !(int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= libc.Int32FromInt32(PARSE_MODE_RENAME)) {
		v = _sqlite3GetVdbe(tls, pParse)
		db = (*TParse)(unsafe.Pointer(pParse)).Fdb
		pSub = uintptr(0) /* The subquery */
		pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc
		pWhere = (*TSelect)(unsafe.Pointer(p)).FpWhere
		pGroupBy = (*TSelect)(unsafe.Pointer(p)).FpGroupBy
		pHaving = (*TSelect)(unsafe.Pointer(p)).FpHaving
		pSort = uintptr(0)
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* Expression list for sub-query */
		pMWin = (*TSelect)(unsafe.Pointer(p)).FpWin
		selFlags = (*TSelect)(unsafe.Pointer(p)).FselFlags
		pTab = _sqlite3DbMallocZero(tls, db, uint64(80))
		if pTab == uintptr(0) {
			return _sqlite3ErrorToParser(tls, db, int32(SQLITE_NOMEM))
		}
		_sqlite3AggInfoPersistWalkerInit(tls, bp+4, pParse)
		_sqlite3WalkSelect(tls, bp+4, p)
		if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) == uint32(0) {
			(**(**TWalker)(__ccgo_up(bp + 4))).FxExprCallback = __ccgo_fp(_disallowAggregatesInOrderByCb)
			(**(**TWalker)(__ccgo_up(bp + 4))).FxSelectCallback = uintptr(0)
			_sqlite3WalkExprList(tls, bp+4, (*TSelect)(unsafe.Pointer(p)).FpOrderBy)
		}
		(*TSelect)(unsafe.Pointer(p)).FpSrc = uintptr(0)
		(*TSelect)(unsafe.Pointer(p)).FpWhere = uintptr(0)
		(*TSelect)(unsafe.Pointer(p)).FpGroupBy = uintptr(0)
		(*TSelect)(unsafe.Pointer(p)).FpHaving = uintptr(0)
		**(**Tu32)(__ccgo_up(p + 4)) &= ^libc.Uint32FromInt32(SF_Aggregate)
		**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_WinRewrite)
		/* Create the ORDER BY clause for the sub-select. This is the concatenation
		 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
		 ** redundant, remove the ORDER BY from the parent SELECT.  */
		pSort = _exprListAppendList(tls, pParse, uintptr(0), (*TWindow)(unsafe.Pointer(pMWin)).FpPartition, int32(1))
		pSort = _exprListAppendList(tls, pParse, pSort, (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy, int32(1))
		if pSort != 0 && (*TSelect)(unsafe.Pointer(p)).FpOrderBy != 0 && (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy)).FnExpr <= (*TExprList)(unsafe.Pointer(pSort)).FnExpr {
			nSave = (*TExprList)(unsafe.Pointer(pSort)).FnExpr
			(*TExprList)(unsafe.Pointer(pSort)).FnExpr = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpOrderBy)).FnExpr
			if _sqlite3ExprListCompare(tls, pSort, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, -int32(1)) == 0 {
				_sqlite3ExprListDelete(tls, db, (*TSelect)(unsafe.Pointer(p)).FpOrderBy)
				(*TSelect)(unsafe.Pointer(p)).FpOrderBy = uintptr(0)
			}
			(*TExprList)(unsafe.Pointer(pSort)).FnExpr = nSave
		}
		/* Assign a cursor number for the ephemeral table used to buffer rows.
		 ** The OpenEphemeral instruction is coded later, after it is known how
		 ** many columns the table will have.  */
		v2 = pParse + 44
		v1 = *(*int32)(unsafe.Pointer(v2))
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		(*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr = v1
		**(**int32)(__ccgo_up(pParse + 44)) += int32(3)
		_selectWindowRewriteEList(tls, pParse, pMWin, pSrc, (*TSelect)(unsafe.Pointer(p)).FpEList, pTab, bp)
		_selectWindowRewriteEList(tls, pParse, pMWin, pSrc, (*TSelect)(unsafe.Pointer(p)).FpOrderBy, pTab, bp)
		if **(**uintptr)(__ccgo_up(bp)) != 0 {
			v1 = (*TExprList)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnExpr
		} else {
			v1 = 0
		}
		(*TWindow)(unsafe.Pointer(pMWin)).FnBufferCol = v1
		/* Append the PARTITION BY and ORDER BY expressions to the to the
		 ** sub-select expression list. They are required to figure out where
		 ** boundaries for partitions and sets of peer rows lie.  */
		**(**uintptr)(__ccgo_up(bp)) = _exprListAppendList(tls, pParse, **(**uintptr)(__ccgo_up(bp)), (*TWindow)(unsafe.Pointer(pMWin)).FpPartition, 0)
		**(**uintptr)(__ccgo_up(bp)) = _exprListAppendList(tls, pParse, **(**uintptr)(__ccgo_up(bp)), (*TWindow)(unsafe.Pointer(pMWin)).FpOrderBy, 0)
		/* Append the arguments passed to each window function to the
		 ** sub-select expression list. Also allocate two registers for each
		 ** window function - one for the accumulator, another for interim
		 ** results.  */
		pWin = pMWin
		for {
			if !(pWin != 0) {
				break
			}
			pArgs = *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20))
			if (*TFuncDef)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpWFunc)).FfuncFlags&uint32(SQLITE_SUBTYPE) != 0 {
				_selectWindowRewriteEList(tls, pParse, pMWin, pSrc, pArgs, pTab, bp)
				if **(**uintptr)(__ccgo_up(bp)) != 0 {
					v1 = (*TExprList)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnExpr
				} else {
					v1 = 0
				}
				(*TWindow)(unsafe.Pointer(pWin)).FiArgCol = v1
				(*TWindow)(unsafe.Pointer(pWin)).FbExprArgs = uint8(1)
			} else {
				if **(**uintptr)(__ccgo_up(bp)) != 0 {
					v1 = (*TExprList)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FnExpr
				} else {
					v1 = 0
				}
				(*TWindow)(unsafe.Pointer(pWin)).FiArgCol = v1
				**(**uintptr)(__ccgo_up(bp)) = _exprListAppendList(tls, pParse, **(**uintptr)(__ccgo_up(bp)), pArgs, 0)
			}
			if (*TWindow)(unsafe.Pointer(pWin)).FpFilter != 0 {
				pFilter = _sqlite3ExprDup(tls, db, (*TWindow)(unsafe.Pointer(pWin)).FpFilter, 0)
				**(**uintptr)(__ccgo_up(bp)) = _sqlite3ExprListAppend(tls, pParse, **(**uintptr)(__ccgo_up(bp)), pFilter)
			}
			v2 = pParse + 48
			*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
			v1 = *(*int32)(unsafe.Pointer(v2))
			(*TWindow)(unsafe.Pointer(pWin)).FregAccum = v1
			v2 = pParse + 48
			*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
			v1 = *(*int32)(unsafe.Pointer(v2))
			(*TWindow)(unsafe.Pointer(pWin)).FregResult = v1
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pWin)).FregAccum)
			goto _4
		_4:
			;
			pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin
		}
		/* If there is no ORDER BY or PARTITION BY clause, and the window
		 ** function accepts zero arguments, and there are no other columns
		 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
		 ** that pSublist is still NULL here. Add a constant expression here to
		 ** keep everything legal in this case.
		 */
		if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
			**(**uintptr)(__ccgo_up(bp)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3ExprInt32(tls, db, 0))
		}
		pSub = _sqlite3SelectNew(tls, pParse, **(**uintptr)(__ccgo_up(bp)), pSrc, pWhere, pGroupBy, pHaving, pSort, uint32(0), uintptr(0))
		(*TSelect)(unsafe.Pointer(p)).FpSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0))
		/* Due to db->mallocFailed test inside
		 ** of sqlite3DbMallocRawNN() called from
		 ** sqlite3SrcListAppend() */
		if (*TSelect)(unsafe.Pointer(p)).FpSrc == uintptr(0) {
			_sqlite3SelectDelete(tls, db, pSub)
		} else {
			if _sqlite3SrcItemAttachSubquery(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpSrc+8, pSub, 0) != 0 {
				libc.SetBitFieldPtr32Uint32((*TSelect)(unsafe.Pointer(p)).FpSrc+8+12+4, libc.Uint32FromInt32(1), 4, 0x10)
				_sqlite3SrcListAssignCursors(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpSrc)
				**(**Tu32)(__ccgo_up(pSub + 4)) |= uint32(libc.Int32FromInt32(SF_Expanded) | libc.Int32FromInt32(SF_OrderByReqd))
				pTab2 = _sqlite3ResultSetOfSelect(tls, pParse, pSub, int8(SQLITE_AFF_NONE))
				**(**Tu32)(__ccgo_up(pSub + 4)) |= selFlags & uint32(SF_Aggregate)
				if pTab2 == uintptr(0) {
					/* Might actually be some other kind of error, but in that case
					 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
					 ** the correct error message regardless. */
					rc = int32(SQLITE_NOMEM)
				} else {
					libc.Xmemcpy(tls, pTab, pTab2, uint32(80))
					**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(TF_Ephemeral)
					(*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpSTab = pTab
					pTab = pTab2
					libc.Xmemset(tls, bp+4, 0, uint32(28))
					(**(**TWalker)(__ccgo_up(bp + 4))).FxExprCallback = __ccgo_fp(_sqlite3WindowExtraAggFuncDepth)
					(**(**TWalker)(__ccgo_up(bp + 4))).FxSelectCallback = __ccgo_fp(_sqlite3WalkerDepthIncrease)
					(**(**TWalker)(__ccgo_up(bp + 4))).FxSelectCallback2 = __ccgo_fp(_sqlite3WalkerDepthDecrease)
					_sqlite3WalkSelect(tls, bp+4, pSub)
				}
			}
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			rc = int32(SQLITE_NOMEM)
		}
		/* Defer deleting the temporary table pTab because if an error occurred,
		 ** there could still be references to that table embedded in the
		 ** result-set or ORDER BY clause of the SELECT statement p.  */
		_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_sqlite3DbFree), pTab)
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is called immediately after resolving the function name
//	** for a window function within a SELECT statement. Argument pList is a
//	** linked list of WINDOW definitions for the current SELECT statement.
//	** Argument pFunc is the function definition just resolved and pWin
//	** is the Window object representing the associated OVER clause. This
//	** function updates the contents of pWin as follows:
//	**
//	**   * If the OVER clause referred to a named window (as in "max(x) OVER win"),
//	**     search list pList for a matching WINDOW definition, and update pWin
//	**     accordingly. If no such WINDOW clause can be found, leave an error
//	**     in pParse.
//	**
//	**   * If the function is a built-in window function that requires the
//	**     window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
//	**     of this file), pWin is updated here.
//	*/
func _sqlite3WindowUpdate(tls *libc.TLS, pParse uintptr, pList uintptr, pWin uintptr, pFunc uintptr) {
	var aUp [8]struct {
		FzFunc    uintptr
		FeFrmType int32
		FeStart   int32
		FeEnd     int32
	}
	var db, p, v2 uintptr
	var i int32
	_, _, _, _, _ = aUp, db, i, p, v2
	if (*TWindow)(unsafe.Pointer(pWin)).FzName != 0 && int32((*TWindow)(unsafe.Pointer(pWin)).FeFrmType) == 0 {
		p = _windowFind(tls, pParse, pList, (*TWindow)(unsafe.Pointer(pWin)).FzName)
		if p == uintptr(0) {
			return
		}
		(*TWindow)(unsafe.Pointer(pWin)).FpPartition = _sqlite3ExprListDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWindow)(unsafe.Pointer(p)).FpPartition, 0)
		(*TWindow)(unsafe.Pointer(pWin)).FpOrderBy = _sqlite3ExprListDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWindow)(unsafe.Pointer(p)).FpOrderBy, 0)
		(*TWindow)(unsafe.Pointer(pWin)).FpStart = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWindow)(unsafe.Pointer(p)).FpStart, 0)
		(*TWindow)(unsafe.Pointer(pWin)).FpEnd = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*TWindow)(unsafe.Pointer(p)).FpEnd, 0)
		(*TWindow)(unsafe.Pointer(pWin)).FeStart = (*TWindow)(unsafe.Pointer(p)).FeStart
		(*TWindow)(unsafe.Pointer(pWin)).FeEnd = (*TWindow)(unsafe.Pointer(p)).FeEnd
		(*TWindow)(unsafe.Pointer(pWin)).FeFrmType = (*TWindow)(unsafe.Pointer(p)).FeFrmType
		(*TWindow)(unsafe.Pointer(pWin)).FeExclude = (*TWindow)(unsafe.Pointer(p)).FeExclude
	} else {
		_sqlite3WindowChain(tls, pParse, pWin, pList)
	}
	if int32((*TWindow)(unsafe.Pointer(pWin)).FeFrmType) == int32(TK_RANGE) && ((*TWindow)(unsafe.Pointer(pWin)).FpStart != 0 || (*TWindow)(unsafe.Pointer(pWin)).FpEnd != 0) && ((*TWindow)(unsafe.Pointer(pWin)).FpOrderBy == uintptr(0) || (*TExprList)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOrderBy)).FnExpr != int32(1)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25529, 0)
	} else {
		if (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(SQLITE_FUNC_WINDOW) != 0 {
			db = (*TParse)(unsafe.Pointer(pParse)).Fdb
			if (*TWindow)(unsafe.Pointer(pWin)).FpFilter != 0 {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25600, 0)
			} else {
				aUp = [8]struct {
					FzFunc    uintptr
					FeFrmType int32
					FeStart   int32
					FeEnd     int32
				}{
					0: {
						FzFunc:    uintptr(unsafe.Pointer(&_row_numberName)),
						FeFrmType: int32(TK_ROWS),
						FeStart:   int32(TK_UNBOUNDED),
						FeEnd:     int32(TK_CURRENT),
					},
					1: {
						FzFunc:    uintptr(unsafe.Pointer(&_dense_rankName)),
						FeFrmType: int32(TK_RANGE),
						FeStart:   int32(TK_UNBOUNDED),
						FeEnd:     int32(TK_CURRENT),
					},
					2: {
						FzFunc:    uintptr(unsafe.Pointer(&_rankName)),
						FeFrmType: int32(TK_RANGE),
						FeStart:   int32(TK_UNBOUNDED),
						FeEnd:     int32(TK_CURRENT),
					},
					3: {
						FzFunc:    uintptr(unsafe.Pointer(&_percent_rankName)),
						FeFrmType: int32(TK_GROUPS),
						FeStart:   int32(TK_CURRENT),
						FeEnd:     int32(TK_UNBOUNDED),
					},
					4: {
						FzFunc:    uintptr(unsafe.Pointer(&_cume_distName)),
						FeFrmType: int32(TK_GROUPS),
						FeStart:   int32(TK_FOLLOWING),
						FeEnd:     int32(TK_UNBOUNDED),
					},
					5: {
						FzFunc:    uintptr(unsafe.Pointer(&_ntileName)),
						FeFrmType: int32(TK_ROWS),
						FeStart:   int32(TK_CURRENT),
						FeEnd:     int32(TK_UNBOUNDED),
					},
					6: {
						FzFunc:    uintptr(unsafe.Pointer(&_leadName)),
						FeFrmType: int32(TK_ROWS),
						FeStart:   int32(TK_UNBOUNDED),
						FeEnd:     int32(TK_UNBOUNDED),
					},
					7: {
						FzFunc:    uintptr(unsafe.Pointer(&_lagName)),
						FeFrmType: int32(TK_ROWS),
						FeStart:   int32(TK_UNBOUNDED),
						FeEnd:     int32(TK_CURRENT),
					},
				}
				i = 0
				for {
					if !(i < int32(libc.Uint32FromInt64(128)/libc.Uint32FromInt64(16))) {
						break
					}
					if (*TFuncDef)(unsafe.Pointer(pFunc)).FzName == aUp[i].FzFunc {
						_sqlite3ExprDelete(tls, db, (*TWindow)(unsafe.Pointer(pWin)).FpStart)
						_sqlite3ExprDelete(tls, db, (*TWindow)(unsafe.Pointer(pWin)).FpEnd)
						v2 = libc.UintptrFromInt32(0)
						(*TWindow)(unsafe.Pointer(pWin)).FpStart = v2
						(*TWindow)(unsafe.Pointer(pWin)).FpEnd = v2
						(*TWindow)(unsafe.Pointer(pWin)).FeFrmType = uint8(aUp[i].FeFrmType)
						(*TWindow)(unsafe.Pointer(pWin)).FeStart = uint8(aUp[i].FeStart)
						(*TWindow)(unsafe.Pointer(pWin)).FeEnd = uint8(aUp[i].FeEnd)
						(*TWindow)(unsafe.Pointer(pWin)).FeExclude = uint8(0)
						if int32((*TWindow)(unsafe.Pointer(pWin)).FeStart) == int32(TK_FOLLOWING) {
							(*TWindow)(unsafe.Pointer(pWin)).FpStart = _sqlite3ExprInt32(tls, db, int32(1))
						}
						break
					}
					goto _1
				_1:
					;
					i = i + 1
				}
			}
		}
	}
	(*TWindow)(unsafe.Pointer(pWin)).FpWFunc = pFunc
}

// C documentation
//
//	/*
//	** This routine is invoked once per CTE by the parser while parsing a
//	** WITH clause.  The CTE described by the third argument is added to
//	** the WITH clause of the second argument.  If the second argument is
//	** NULL, then a new WITH argument is created.
//	*/
func _sqlite3WithAdd(tls *libc.TLS, pParse uintptr, pWith uintptr, pCte uintptr) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pNew, zName, v3 uintptr
	var i, v2 int32
	_, _, _, _, _, _ = db, i, pNew, zName, v2, v3
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if pCte == uintptr(0) {
		return pWith
	}
	/* Check that the CTE name is unique within this WITH clause. If
	 ** not, store an error in the Parse structure. */
	zName = (*TCte)(unsafe.Pointer(pCte)).FzName
	if zName != 0 && pWith != 0 {
		i = 0
		for {
			if !(i < (*TWith)(unsafe.Pointer(pWith)).FnCte) {
				break
			}
			if _sqlite3StrICmp(tls, zName, (*(*TCte)(unsafe.Pointer(pWith + 12 + uintptr(i)*24))).FzName) == 0 {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17506, libc.VaList(bp+8, zName))
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	if pWith != 0 {
		pNew = _sqlite3DbRealloc(tls, db, pWith, uint64(uint32(libc.UintptrFromInt32(0)+12)+uint32((*TWith)(unsafe.Pointer(pWith)).FnCte+libc.Int32FromInt32(1))*libc.Uint32FromInt64(24)))
	} else {
		pNew = _sqlite3DbMallocZero(tls, db, uint64(uint32(libc.UintptrFromInt32(0)+12)+uint32(libc.Int32FromInt32(1))*libc.Uint32FromInt64(24)))
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_sqlite3CteDelete(tls, db, pCte)
		pNew = pWith
	} else {
		v3 = pNew
		v2 = *(*int32)(unsafe.Pointer(v3))
		*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
		*(*TCte)(unsafe.Pointer(pNew + 12 + uintptr(v2)*24)) = **(**TCte)(__ccgo_up(pCte))
		_sqlite3DbFree(tls, db, pCte)
	}
	return pNew
}

// C documentation
//
//	/*
//	** Create and return a deep copy of the object passed as the second
//	** argument. If an OOM condition is encountered, NULL is returned
//	** and the db->mallocFailed flag set.
//	*/
func _sqlite3WithDup(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) {
	var i int32
	var nByte Tsqlite3_int64
	var pRet uintptr
	_, _, _ = i, nByte, pRet
	pRet = uintptr(0)
	if p != 0 {
		nByte = int64(uint32(libc.UintptrFromInt32(0)+12) + uint32((*TWith)(unsafe.Pointer(p)).FnCte)*libc.Uint32FromInt64(24))
		pRet = _sqlite3DbMallocZero(tls, db, uint64(nByte))
		if pRet != 0 {
			(*TWith)(unsafe.Pointer(pRet)).FnCte = (*TWith)(unsafe.Pointer(p)).FnCte
			i = 0
			for {
				if !(i < (*TWith)(unsafe.Pointer(p)).FnCte) {
					break
				}
				(*(*TCte)(unsafe.Pointer(pRet + 12 + uintptr(i)*24))).FpSelect = _sqlite3SelectDup(tls, db, (*(*TCte)(unsafe.Pointer(p + 12 + uintptr(i)*24))).FpSelect, 0)
				(*(*TCte)(unsafe.Pointer(pRet + 12 + uintptr(i)*24))).FpCols = _sqlite3ExprListDup(tls, db, (*(*TCte)(unsafe.Pointer(p + 12 + uintptr(i)*24))).FpCols, 0)
				(*(*TCte)(unsafe.Pointer(pRet + 12 + uintptr(i)*24))).FzName = _sqlite3DbStrDup(tls, db, (*(*TCte)(unsafe.Pointer(p + 12 + uintptr(i)*24))).FzName)
				(*(*TCte)(unsafe.Pointer(pRet + 12 + uintptr(i)*24))).FeM10d = (*(*TCte)(unsafe.Pointer(p + 12 + uintptr(i)*24))).FeM10d
				goto _1
			_1:
				;
				i = i + 1
			}
		}
	}
	return pRet
}

func _sqlite3_geopoly_init(tls *libc.TLS, db uintptr) (r int32) {
	var enc, rc int32
	var i uint32
	_, _, _ = enc, i, rc
	rc = SQLITE_OK
	i = uint32(0)
	for {
		if !(i < libc.Uint32FromInt64(144)/libc.Uint32FromInt64(12) && rc == SQLITE_OK) {
			break
		}
		if _aFunc[i].FbPure != 0 {
			enc = libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DETERMINISTIC) | libc.Int32FromInt32(SQLITE_INNOCUOUS)
		} else {
			enc = libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(SQLITE_DIRECTONLY)
		}
		rc = Xsqlite3_create_function(tls, db, _aFunc[i].FzName, int32(_aFunc[i].FnArg), enc, uintptr(0), _aFunc[i].FxFunc, uintptr(0), uintptr(0))
		goto _1
	_1:
		;
		i = i + 1
	}
	i = uint32(0)
	for {
		if !(i < libc.Uint32FromInt64(12)/libc.Uint32FromInt64(12) && rc == SQLITE_OK) {
			break
		}
		rc = Xsqlite3_create_function(tls, db, _aAgg[i].FzName, int32(1), libc.Int32FromInt32(SQLITE_UTF8)|libc.Int32FromInt32(SQLITE_DETERMINISTIC)|libc.Int32FromInt32(SQLITE_INNOCUOUS), uintptr(0), uintptr(0), _aAgg[i].FxStep, _aAgg[i].FxFinal)
		goto _2
	_2:
		;
		i = i + 1
	}
	if rc == SQLITE_OK {
		rc = Xsqlite3_create_module_v2(tls, db, __ccgo_ts+31458, uintptr(unsafe.Pointer(&_geopolyModule)), uintptr(0), uintptr(0))
	}
	return rc
}

// C documentation
//
//	/*
//	** This routine is called once for each row in the result table.  Its job
//	** is to fill in the TabResult structure appropriately, allocating new
//	** memory as necessary.
//	*/
func _sqlite3_get_table_cb(tls *libc.TLS, pArg uintptr, nCol int32, argv uintptr, colv uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var azNew, p, z, v3 uintptr
	var i, n, need int32
	var v2 Tu32
	_, _, _, _, _, _, _, _ = azNew, i, n, need, p, z, v2, v3
	p = pArg /* A single column of result */
	/* Make sure there is enough space in p->azResult to hold everything
	 ** we need to remember from this invocation of the callback.
	 */
	if (*TTabResult)(unsafe.Pointer(p)).FnRow == uint32(0) && argv != uintptr(0) {
		need = nCol * int32(2)
	} else {
		need = nCol
	}
	if (*TTabResult)(unsafe.Pointer(p)).FnData+uint32(need) > (*TTabResult)(unsafe.Pointer(p)).FnAlloc {
		(*TTabResult)(unsafe.Pointer(p)).FnAlloc = (*TTabResult)(unsafe.Pointer(p)).FnAlloc*uint32(2) + uint32(need)
		azNew = _sqlite3Realloc(tls, (*TTabResult)(unsafe.Pointer(p)).FazResult, uint64(uint32(4)*(*TTabResult)(unsafe.Pointer(p)).FnAlloc))
		if azNew == uintptr(0) {
			goto malloc_failed
		}
		(*TTabResult)(unsafe.Pointer(p)).FazResult = azNew
	}
	/* If this is the first row, then generate an extra row containing
	 ** the names of all columns.
	 */
	if (*TTabResult)(unsafe.Pointer(p)).FnRow == uint32(0) {
		(*TTabResult)(unsafe.Pointer(p)).FnColumn = uint32(nCol)
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			z = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+8, **(**uintptr)(__ccgo_up(colv + uintptr(i)*4))))
			if z == uintptr(0) {
				goto malloc_failed
			}
			v3 = p + 20
			v2 = *(*Tu32)(unsafe.Pointer(v3))
			*(*Tu32)(unsafe.Pointer(v3)) = *(*Tu32)(unsafe.Pointer(v3)) + 1
			**(**uintptr)(__ccgo_up((*TTabResult)(unsafe.Pointer(p)).FazResult + uintptr(v2)*4)) = z
			goto _1
		_1:
			;
			i = i + 1
		}
	} else {
		if int32((*TTabResult)(unsafe.Pointer(p)).FnColumn) != nCol {
			Xsqlite3_free(tls, (*TTabResult)(unsafe.Pointer(p)).FzErrMsg)
			(*TTabResult)(unsafe.Pointer(p)).FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+23005, 0)
			(*TTabResult)(unsafe.Pointer(p)).Frc = int32(SQLITE_ERROR)
			return int32(1)
		}
	}
	/* Copy over the row data
	 */
	if argv != uintptr(0) {
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			if **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)) == uintptr(0) {
				z = uintptr(0)
			} else {
				n = _sqlite3Strlen30(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4))) + int32(1)
				z = Xsqlite3_malloc64(tls, uint64(n))
				if z == uintptr(0) {
					goto malloc_failed
				}
				libc.Xmemcpy(tls, z, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)), uint32(n))
			}
			v3 = p + 20
			v2 = *(*Tu32)(unsafe.Pointer(v3))
			*(*Tu32)(unsafe.Pointer(v3)) = *(*Tu32)(unsafe.Pointer(v3)) + 1
			**(**uintptr)(__ccgo_up((*TTabResult)(unsafe.Pointer(p)).FazResult + uintptr(v2)*4)) = z
			goto _4
		_4:
			;
			i = i + 1
		}
		(*TTabResult)(unsafe.Pointer(p)).FnRow = (*TTabResult)(unsafe.Pointer(p)).FnRow + 1
	}
	return 0
	goto malloc_failed
malloc_failed:
	;
	(*TTabResult)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM)
	return int32(1)
}

// C documentation
//
//	/*
//	** This routine implements a busy callback that sleeps and tries
//	** again until a timeout value is reached.  The timeout value is
//	** an integer number of milliseconds passed in as the first
//	** argument.
//	**
//	** Return non-zero to retry the lock.  Return zero to stop trying
//	** and cause SQLite to return SQLITE_BUSY.
//	*/
func _sqliteDefaultBusyCallback(tls *libc.TLS, ptr uintptr, count int32) (r int32) {
	var db uintptr
	var delay, prior, tmout int32
	_, _, _, _ = db, delay, prior, tmout
	db = ptr
	tmout = (*Tsqlite3)(unsafe.Pointer(db)).FbusyTimeout
	if count < int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(1)) {
		delay = int32(_delays[count])
		prior = int32(_totals[count])
	} else {
		delay = int32(_delays[int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(1))-libc.Int32FromInt32(1)])
		prior = int32(_totals[int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(1))-libc.Int32FromInt32(1)]) + delay*(count-(int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(1))-libc.Int32FromInt32(1)))
	}
	if prior+delay > tmout {
		delay = tmout - prior
		if delay <= 0 {
			return 0
		}
	}
	_sqlite3OsSleep(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, delay*int32(1000))
	return int32(1)
}

// C documentation
//
//	/*
//	** Clear the column names from every VIEW in database idx.
//	*/
func _sqliteViewResetAll(tls *libc.TLS, db uintptr, idx int32) {
	var i, pTab, v2 uintptr
	_, _, _ = i, pTab, v2
	if !(int32((*TSchema)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(idx)*16))).FpSchema)).FschemaFlags)&libc.Int32FromInt32(DB_UnresetViews) == libc.Int32FromInt32(DB_UnresetViews)) {
		return
	}
	i = (*THash)(unsafe.Pointer((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(idx)*16))).FpSchema + 8)).Ffirst
	for {
		if !(i != 0) {
			break
		}
		pTab = (*THashElem)(unsafe.Pointer(i)).Fdata
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
			_sqlite3DeleteColumnNames(tls, db, pTab)
		}
		goto _1
	_1:
		;
		i = (*THashElem)(unsafe.Pointer(i)).Fnext
	}
	v2 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(idx)*16))).FpSchema + 78
	*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) & ^libc.Int32FromInt32(DB_UnresetViews))
}

// C documentation
//
//	/*
//	** Attempt to extract a value from pExpr and use it to construct *ppVal.
//	**
//	** If pAlloc is not NULL, then an UnpackedRecord object is created for
//	** pAlloc if one does not exist and the new value is added to the
//	** UnpackedRecord object.
//	**
//	** A value is extracted in the following cases:
//	**
//	**  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
//	**
//	**  * The expression is a bound variable, and this is a reprepare, or
//	**
//	**  * The expression is a literal value.
//	**
//	** On success, *ppVal is made to point to the extracted value.  The caller
//	** is responsible for ensuring that the value is eventually freed.
//	*/
func _stat4ValueFromExpr(tls *libc.TLS, pParse uintptr, pExpr uintptr, affinity Tu8, pAlloc uintptr, ppVal uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, v, v1 uintptr
	var iBindVar, rc int32
	var _ /* pVal at bp+0 */ uintptr
	_, _, _, _, _ = db, iBindVar, rc, v, v1
	rc = SQLITE_OK
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	/* Skip over any TK_COLLATE nodes */
	pExpr = _sqlite3ExprSkipCollate(tls, pExpr)
	if !(pExpr != 0) {
		**(**uintptr)(__ccgo_up(bp)) = _valueNew(tls, db, pAlloc)
		if **(**uintptr)(__ccgo_up(bp)) != 0 {
			_sqlite3VdbeMemSetNull(tls, **(**uintptr)(__ccgo_up(bp)))
		}
	} else {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_VARIABLE) && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_EnableQPSG) == uint64(0) {
			iBindVar = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
			_sqlite3VdbeSetVarmask(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, iBindVar)
			v1 = (*TParse)(unsafe.Pointer(pParse)).FpReprepare
			v = v1
			if v1 != uintptr(0) {
				**(**uintptr)(__ccgo_up(bp)) = _valueNew(tls, db, pAlloc)
				if **(**uintptr)(__ccgo_up(bp)) != 0 {
					rc = _sqlite3VdbeMemCopy(tls, **(**uintptr)(__ccgo_up(bp)), (*TVdbe)(unsafe.Pointer(v)).FaVar+uintptr(iBindVar-int32(1))*40)
					_sqlite3ValueApplyAffinity(tls, **(**uintptr)(__ccgo_up(bp)), affinity, (*Tsqlite3)(unsafe.Pointer(db)).Fenc)
					(*Tsqlite3_value)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fdb = (*TParse)(unsafe.Pointer(pParse)).Fdb
				}
			}
		} else {
			rc = _valueFromExpr(tls, db, pExpr, (*Tsqlite3)(unsafe.Pointer(db)).Fenc, affinity, bp, pAlloc)
		}
	}
	**(**uintptr)(__ccgo_up(ppVal)) = **(**uintptr)(__ccgo_up(bp))
	return rc
}

// C documentation
//
//	/*
//	** Compute the best query strategy and return the result in idxNum.
//	**
//	**   idxNum-Bit        Meaning
//	**   ----------        ----------------------------------------------
//	**      0x01           There is a schema=? term in the WHERE clause
//	**      0x02           There is a name=? term in the WHERE clause
//	**      0x04           There is an aggregate=? term in the WHERE clause
//	**      0x08           Output should be ordered by name and path
//	*/
func _statBestIndex(tls *libc.TLS, tab uintptr, pIdxInfo uintptr) (r int32) {
	var i, iAgg, iName, iSchema, v2 int32
	_, _, _, _, _ = i, iAgg, iName, iSchema, v2
	iSchema = -int32(1)
	iName = -int32(1)
	iAgg = -int32(1)
	_ = tab
	/* Look for a valid schema=? constraint.  If found, change the idxNum to
	 ** 1 and request the value of that constraint be sent to xFilter.  And
	 ** lower the cost estimate to encourage the constrained version to be
	 ** used.
	 */
	i = 0
	for {
		if !(i < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint) {
			break
		}
		if int32((**(**Tsqlite3_index_constraint)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(i)*12))).Fop) != int32(SQLITE_INDEX_CONSTRAINT_EQ) {
			goto _1
		}
		if int32((**(**Tsqlite3_index_constraint)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(i)*12))).Fusable) == 0 {
			/* Force DBSTAT table should always be the right-most table in a join */
			return int32(SQLITE_CONSTRAINT)
		}
		switch (**(**Tsqlite3_index_constraint)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraint + uintptr(i)*12))).FiColumn {
		case 0: /* name */
			iName = i
		case int32(10): /* schema */
			iSchema = i
		case int32(11): /* aggregate */
			iAgg = i
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	i = 0
	if iSchema >= 0 {
		i = i + 1
		v2 = i
		(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iSchema)*8))).FargvIndex = v2
		(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iSchema)*8))).Fomit = uint8(1)
		**(**int32)(__ccgo_up(pIdxInfo + 20)) |= int32(0x01)
	}
	if iName >= 0 {
		i = i + 1
		v2 = i
		(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iName)*8))).FargvIndex = v2
		**(**int32)(__ccgo_up(pIdxInfo + 20)) |= int32(0x02)
	}
	if iAgg >= 0 {
		i = i + 1
		v2 = i
		(**(**Tsqlite3_index_constraint_usage)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage + uintptr(iAgg)*8))).FargvIndex = v2
		**(**int32)(__ccgo_up(pIdxInfo + 20)) |= int32(0x04)
	}
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(1)
	/* Records are always returned in ascending order of (name, path).
	 ** If this will satisfy the client, set the orderByConsumed flag so that
	 ** SQLite does not do an external sort.
	 */
	if (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnOrderBy == int32(1) && (**(**Tsqlite3_index_orderby)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).FiColumn == 0 && int32((**(**Tsqlite3_index_orderby)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).Fdesc) == 0 || (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnOrderBy == int32(2) && (**(**Tsqlite3_index_orderby)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).FiColumn == 0 && int32((**(**Tsqlite3_index_orderby)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy))).Fdesc) == 0 && (**(**Tsqlite3_index_orderby)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy + 1*8))).FiColumn == int32(1) && int32((**(**Tsqlite3_index_orderby)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaOrderBy + 1*8))).Fdesc) == 0 {
		(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).ForderByConsumed = int32(1)
		**(**int32)(__ccgo_up(pIdxInfo + 20)) |= int32(0x08)
	}
	**(**int32)(__ccgo_up(pIdxInfo + 56)) |= int32(SQLITE_INDEX_SCAN_HEX)
	return SQLITE_OK
}

func _statColumn(tls *libc.TLS, pCursor uintptr, ctx uintptr, i int32) (r int32) {
	var db, pCsr uintptr
	var iDb int32
	_, _, _ = db, iDb, pCsr
	pCsr = pCursor
	switch i {
	case 0: /* name */
		Xsqlite3_result_text(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FzName, -int32(1), uintptr(-libc.Int32FromInt32(1)))
	case int32(1): /* path */
		if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) {
			Xsqlite3_result_text(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath, -int32(1), uintptr(-libc.Int32FromInt32(1)))
		}
	case int32(2): /* pageno */
		if (*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0 {
			Xsqlite3_result_int64(tls, ctx, int64((*TStatCursor)(unsafe.Pointer(pCsr)).FnPage))
		} else {
			Xsqlite3_result_int64(tls, ctx, int64((*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno))
		}
	case int32(3): /* pagetype */
		if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) {
			Xsqlite3_result_text(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype, -int32(1), libc.UintptrFromInt32(0))
		}
	case int32(4): /* ncell */
		Xsqlite3_result_int64(tls, ctx, int64((*TStatCursor)(unsafe.Pointer(pCsr)).FnCell))
	case int32(5): /* payload */
		Xsqlite3_result_int64(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FnPayload)
	case int32(6): /* unused */
		Xsqlite3_result_int64(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FnUnused)
	case int32(7): /* mx_payload */
		Xsqlite3_result_int64(tls, ctx, int64((*TStatCursor)(unsafe.Pointer(pCsr)).FnMxPayload))
	case int32(8): /* pgoffset */
		if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) {
			Xsqlite3_result_int64(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FiOffset)
		}
	case int32(9): /* pgsize */
		Xsqlite3_result_int64(tls, ctx, (*TStatCursor)(unsafe.Pointer(pCsr)).FszPage)
	case int32(10): /* schema */
		db = Xsqlite3_context_db_handle(tls, ctx)
		iDb = (*TStatCursor)(unsafe.Pointer(pCsr)).FiDb
		Xsqlite3_result_text(tls, ctx, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName, -int32(1), libc.UintptrFromInt32(0))
	default: /* aggregate */
		Xsqlite3_result_int(tls, ctx, int32((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg))
		break
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Connect to or create a new DBSTAT virtual table.
//	*/
func _statConnect(tls *libc.TLS, db uintptr, pAux uintptr, argc int32, argv uintptr, ppVtab uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var iDb, rc int32
	var pTab uintptr
	var _ /* nm at bp+0 */ TToken
	_, _, _ = iDb, pTab, rc
	pTab = uintptr(0)
	rc = SQLITE_OK
	_ = pAux
	if argc >= int32(4) {
		_sqlite3TokenInit(tls, bp, **(**uintptr)(__ccgo_up(argv + 3*4)))
		iDb = _sqlite3FindDb(tls, db, bp)
		if iDb < 0 {
			**(**uintptr)(__ccgo_up(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+14659, libc.VaList(bp+16, **(**uintptr)(__ccgo_up(argv + 3*4))))
			return int32(SQLITE_ERROR)
		}
	} else {
		iDb = 0
	}
	Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_DIRECTONLY), 0)
	rc = Xsqlite3_declare_vtab(tls, db, uintptr(unsafe.Pointer(&_zDbstatSchema)))
	if rc == SQLITE_OK {
		pTab = Xsqlite3_malloc64(tls, uint64(20))
		if pTab == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		}
	}
	if rc == SQLITE_OK {
		libc.Xmemset(tls, pTab, 0, uint32(20))
		(*TStatTable)(unsafe.Pointer(pTab)).Fdb = db
		(*TStatTable)(unsafe.Pointer(pTab)).FiDb = iDb
	}
	**(**uintptr)(__ccgo_up(ppVtab)) = pTab
	return rc
}

// C documentation
//
//	/* Populate the StatPage object with information about the all
//	** cells found on the page currently under analysis.
//	*/
func _statDecodePage(tls *libc.TLS, pBt uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aData, aHdr, pCell uintptr
	var i, iNext, iOff, isLeaf, j, nHdr, nLocal, nOvfl, nUnused, nUsable, rc, szPage, v1 int32
	var iPrev Tu32
	var v2 uint32
	var _ /* dummy at bp+8 */ Tu64
	var _ /* nPayload at bp+0 */ Tu32
	var _ /* pPg at bp+16 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, aHdr, i, iNext, iOff, iPrev, isLeaf, j, nHdr, nLocal, nOvfl, nUnused, nUsable, pCell, rc, szPage, v1, v2
	aData = (*TStatPage)(unsafe.Pointer(p)).FaPg
	if (*TStatPage)(unsafe.Pointer(p)).FiPgno == uint32(1) {
		v1 = int32(100)
	} else {
		v1 = 0
	}
	aHdr = aData + uintptr(v1)
	(*TStatPage)(unsafe.Pointer(p)).Fflags = **(**Tu8)(__ccgo_up(aHdr))
	if int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x0A) || int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x0D) {
		isLeaf = int32(1)
		nHdr = int32(8)
	} else {
		if int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x05) || int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x02) {
			isLeaf = 0
			nHdr = int32(12)
		} else {
			goto statPageIsCorrupt
		}
	}
	if (*TStatPage)(unsafe.Pointer(p)).FiPgno == uint32(1) {
		nHdr = nHdr + int32(100)
	}
	(*TStatPage)(unsafe.Pointer(p)).FnCell = int32(**(**Tu8)(__ccgo_up(aHdr + 3)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(aHdr + 3 + 1)))
	(*TStatPage)(unsafe.Pointer(p)).FnMxPayload = 0
	szPage = _sqlite3BtreeGetPageSize(tls, pBt)
	nUnused = int32(**(**Tu8)(__ccgo_up(aHdr + 5)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(aHdr + 5 + 1))) - nHdr - int32(2)*(*TStatPage)(unsafe.Pointer(p)).FnCell
	nUnused = nUnused + int32(**(**Tu8)(__ccgo_up(aHdr + 7)))
	iOff = int32(**(**Tu8)(__ccgo_up(aHdr + 1)))<<int32(8) | int32(**(**Tu8)(__ccgo_up(aHdr + 1 + 1)))
	for iOff != 0 {
		if iOff >= szPage {
			goto statPageIsCorrupt
		}
		nUnused = nUnused + (int32(**(**Tu8)(__ccgo_up(aData + uintptr(iOff+int32(2)))))<<int32(8) | int32(**(**Tu8)(__ccgo_up(aData + uintptr(iOff+int32(2)) + 1))))
		iNext = int32(**(**Tu8)(__ccgo_up(aData + uintptr(iOff))))<<int32(8) | int32(**(**Tu8)(__ccgo_up(aData + uintptr(iOff) + 1)))
		if iNext < iOff+int32(4) && iNext > 0 {
			goto statPageIsCorrupt
		}
		iOff = iNext
	}
	(*TStatPage)(unsafe.Pointer(p)).FnUnused = nUnused
	if isLeaf != 0 {
		v2 = uint32(0)
	} else {
		v2 = _sqlite3Get4byte(tls, aHdr+8)
	}
	(*TStatPage)(unsafe.Pointer(p)).FiRightChildPg = v2
	if (*TStatPage)(unsafe.Pointer(p)).FnCell != 0 { /* Usable bytes per page */
		_sqlite3BtreeEnter(tls, pBt)
		nUsable = szPage - _sqlite3BtreeGetReserveNoMutex(tls, pBt)
		_sqlite3BtreeLeave(tls, pBt)
		(*TStatPage)(unsafe.Pointer(p)).FaCell = Xsqlite3_malloc64(tls, uint64(uint32((*TStatPage)(unsafe.Pointer(p)).FnCell+libc.Int32FromInt32(1))*uint32(24)))
		if (*TStatPage)(unsafe.Pointer(p)).FaCell == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, (*TStatPage)(unsafe.Pointer(p)).FaCell, 0, uint32((*TStatPage)(unsafe.Pointer(p)).FnCell+libc.Int32FromInt32(1))*uint32(24))
		i = 0
		for {
			if !(i < (*TStatPage)(unsafe.Pointer(p)).FnCell) {
				break
			}
			pCell = (*TStatPage)(unsafe.Pointer(p)).FaCell + uintptr(i)*24
			iOff = int32(**(**Tu8)(__ccgo_up(aData + uintptr(nHdr+i*int32(2)))))<<int32(8) | int32(**(**Tu8)(__ccgo_up(aData + uintptr(nHdr+i*int32(2)) + 1)))
			if iOff < nHdr || iOff >= szPage {
				goto statPageIsCorrupt
			}
			if !(isLeaf != 0) {
				(*TStatCell)(unsafe.Pointer(pCell)).FiChildPg = _sqlite3Get4byte(tls, aData+uintptr(iOff))
				iOff = iOff + int32(4)
			}
			if int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x05) {
				/* A table interior node. nPayload==0. */
			} else { /* Bytes of payload stored locally */
				if int32(**(**Tu8)(__ccgo_up(aData + uintptr(iOff)))) < int32(libc.Uint8FromInt32(0x80)) {
					**(**Tu32)(__ccgo_up(bp)) = uint32(**(**Tu8)(__ccgo_up(aData + uintptr(iOff))))
					v1 = libc.Int32FromInt32(1)
				} else {
					v1 = int32(_sqlite3GetVarint32(tls, aData+uintptr(iOff), bp))
				}
				iOff = iOff + int32(uint8(v1))
				if int32((*TStatPage)(unsafe.Pointer(p)).Fflags) == int32(0x0D) {
					iOff = iOff + int32(_sqlite3GetVarint(tls, aData+uintptr(iOff), bp+8))
				}
				if **(**Tu32)(__ccgo_up(bp)) > uint32((*TStatPage)(unsafe.Pointer(p)).FnMxPayload) {
					(*TStatPage)(unsafe.Pointer(p)).FnMxPayload = int32(**(**Tu32)(__ccgo_up(bp)))
				}
				nLocal = _getLocalPayload(tls, nUsable, (*TStatPage)(unsafe.Pointer(p)).Fflags, int32(**(**Tu32)(__ccgo_up(bp))))
				if nLocal < 0 {
					goto statPageIsCorrupt
				}
				(*TStatCell)(unsafe.Pointer(pCell)).FnLocal = nLocal
				if **(**Tu32)(__ccgo_up(bp)) > uint32(nLocal) {
					nOvfl = int32((**(**Tu32)(__ccgo_up(bp)) - uint32(nLocal) + uint32(nUsable) - uint32(4) - uint32(1)) / uint32(nUsable-libc.Int32FromInt32(4)))
					if iOff+nLocal+int32(4) > nUsable || **(**Tu32)(__ccgo_up(bp)) > uint32(0x7fffffff) {
						goto statPageIsCorrupt
					}
					(*TStatCell)(unsafe.Pointer(pCell)).FnLastOvfl = int32(**(**Tu32)(__ccgo_up(bp)) - uint32(nLocal) - uint32((nOvfl-int32(1))*(nUsable-int32(4))))
					(*TStatCell)(unsafe.Pointer(pCell)).FnOvfl = nOvfl
					(*TStatCell)(unsafe.Pointer(pCell)).FaOvfl = Xsqlite3_malloc64(tls, uint64(uint32(4)*uint32(nOvfl)))
					if (*TStatCell)(unsafe.Pointer(pCell)).FaOvfl == uintptr(0) {
						return int32(SQLITE_NOMEM)
					}
					**(**Tu32)(__ccgo_up((*TStatCell)(unsafe.Pointer(pCell)).FaOvfl)) = _sqlite3Get4byte(tls, aData+uintptr(iOff+nLocal))
					j = int32(1)
					for {
						if !(j < nOvfl) {
							break
						}
						iPrev = **(**Tu32)(__ccgo_up((*TStatCell)(unsafe.Pointer(pCell)).FaOvfl + uintptr(j-int32(1))*4))
						**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)
						rc = _sqlite3PagerGet(tls, _sqlite3BtreePager(tls, pBt), iPrev, bp+16, 0)
						if rc != SQLITE_OK {
							return rc
						}
						**(**Tu32)(__ccgo_up((*TStatCell)(unsafe.Pointer(pCell)).FaOvfl + uintptr(j)*4)) = _sqlite3Get4byte(tls, _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp + 16))))
						_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp + 16)))
						goto _5
					_5:
						;
						j = j + 1
					}
				}
			}
			goto _3
		_3:
			;
			i = i + 1
		}
	}
	return SQLITE_OK
	goto statPageIsCorrupt
statPageIsCorrupt:
	;
	(*TStatPage)(unsafe.Pointer(p)).Fflags = uint8(0)
	_statClearCells(tls, p)
	return SQLITE_OK
}

// C documentation
//
//	/* Initialize a cursor according to the query plan idxNum using the
//	** arguments in argv[0].  See statBestIndex() for a description of the
//	** meaning of the bits in idxNum.
//	*/
func _statFilter(tls *libc.TLS, pCursor uintptr, idxNum int32, idxStr uintptr, argc int32, argv uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iArg, rc, v1 int32
	var pCsr, pSql, pTab, zDbase, zName, zSql uintptr
	_, _, _, _, _, _, _, _, _ = iArg, pCsr, pSql, pTab, rc, zDbase, zName, zSql, v1
	pCsr = pCursor
	pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab /* String value of pSql */
	iArg = 0                                                       /* Count of argv[] parameters used so far */
	rc = SQLITE_OK                                                 /* Result of this operation */
	zName = uintptr(0)                                             /* Only provide analysis of this table */
	_ = argc
	_ = idxStr
	_statResetCsr(tls, pCsr)
	Xsqlite3_finalize(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt)
	(*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt = uintptr(0)
	if idxNum&int32(0x01) != 0 {
		v1 = iArg
		iArg = iArg + 1
		/* schema=? constraint is present.  Get its value */
		zDbase = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(v1)*4)))
		(*TStatCursor)(unsafe.Pointer(pCsr)).FiDb = _sqlite3FindDbName(tls, (*TStatTable)(unsafe.Pointer(pTab)).Fdb, zDbase)
		if (*TStatCursor)(unsafe.Pointer(pCsr)).FiDb < 0 {
			(*TStatCursor)(unsafe.Pointer(pCsr)).FiDb = 0
			(*TStatCursor)(unsafe.Pointer(pCsr)).FisEof = uint8(1)
			return SQLITE_OK
		}
	} else {
		(*TStatCursor)(unsafe.Pointer(pCsr)).FiDb = (*TStatTable)(unsafe.Pointer(pTab)).FiDb
	}
	if idxNum&int32(0x02) != 0 {
		/* name=? constraint is present */
		v1 = iArg
		iArg = iArg + 1
		zName = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(v1)*4)))
	}
	if idxNum&int32(0x04) != 0 {
		/* aggregate=? constraint is present */
		v1 = iArg
		iArg = iArg + 1
		(*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg = libc.BoolUint8(Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv + uintptr(v1)*4))) != float64(0))
	} else {
		(*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg = uint8(0)
	}
	pSql = Xsqlite3_str_new(tls, (*TStatTable)(unsafe.Pointer(pTab)).Fdb)
	Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+36048, libc.VaList(bp+8, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TStatTable)(unsafe.Pointer(pTab)).Fdb)).FaDb + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiDb)*16))).FzDbSName))
	if zName != 0 {
		Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+36203, libc.VaList(bp+8, zName))
	}
	if idxNum&int32(0x08) != 0 {
		Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+36217, 0)
	}
	zSql = Xsqlite3_str_finish(tls, pSql)
	if zSql == uintptr(0) {
		return int32(SQLITE_NOMEM)
	} else {
		rc = Xsqlite3_prepare_v2(tls, (*TStatTable)(unsafe.Pointer(pTab)).Fdb, zSql, -int32(1), pCsr+4, uintptr(0))
		Xsqlite3_free(tls, zSql)
	}
	if rc == SQLITE_OK {
		(*TStatCursor)(unsafe.Pointer(pCsr)).FiPage = -int32(1)
		rc = _statNext(tls, pCursor)
	}
	return rc
}

// C documentation
//
//	/*
//	** Implementation of the stat_get(P,J) SQL function.  This routine is
//	** used to query statistical information that has been gathered into
//	** the StatAccum object by prior calls to stat_push().  The P parameter
//	** has type BLOB but it is really just a pointer to the StatAccum object.
//	** The content to returned is determined by the parameter J
//	** which is one of the STAT_GET_xxxx values defined above.
//	**
//	** The stat_get(P,J) function is not available to generic SQL.  It is
//	** inserted as part of a manually constructed bytecode program.  (See
//	** the callStatGet() routine below.)  It is guaranteed that the P
//	** parameter will always be a pointer to a StatAccum object, never a
//	** NULL.
//	**
//	** If STAT4 is not enabled, then J is always
//	** STAT_GET_STAT1 and is hence omitted and this routine becomes
//	** a one-parameter function, stat_get(P), that always returns the
//	** stat1 table entry information.
//	*/
func _statGet(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var aCnt, p, pS uintptr
	var eCall, i, i1 int32
	var iVal, nDistinct Tu64
	var v1 uint64
	var _ /* sStat at bp+0 */ Tsqlite3_str
	var _ /* sStat at bp+24 */ Tsqlite3_str
	_, _, _, _, _, _, _, _, _ = aCnt, eCall, i, i1, iVal, nDistinct, p, pS, v1
	p = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv)))
	/* STAT4 has a parameter on this routine. */
	eCall = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	if eCall == STAT_GET_STAT1 {
		/* Loop counter */
		_sqlite3StrAccumInit(tls, bp, uintptr(0), uintptr(0), 0, ((*TStatAccum)(unsafe.Pointer(p)).FnKeyCol+int32(1))*int32(100))
		if (*TStatAccum)(unsafe.Pointer(p)).FnSkipAhead != 0 {
			v1 = (*TStatAccum)(unsafe.Pointer(p)).FnEst
		} else {
			v1 = (*TStatAccum)(unsafe.Pointer(p)).FnRow
		}
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+14221, libc.VaList(bp+56, v1))
		i = 0
		for {
			if !(i < (*TStatAccum)(unsafe.Pointer(p)).FnKeyCol) {
				break
			}
			nDistinct = **(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt + uintptr(i)*8)) + uint64(1)
			iVal = ((*TStatAccum)(unsafe.Pointer(p)).FnRow + nDistinct - uint64(1)) / nDistinct
			if iVal == uint64(2) && (*TStatAccum)(unsafe.Pointer(p)).FnRow*uint64(10) <= nDistinct*uint64(11) {
				iVal = uint64(1)
			}
			Xsqlite3_str_appendf(tls, bp, __ccgo_ts+14226, libc.VaList(bp+56, iVal))
			goto _2
		_2:
			;
			i = i + 1
		}
		_sqlite3ResultStrAccum(tls, context, bp)
	} else {
		if eCall == int32(STAT_GET_ROWID) {
			if (*TStatAccum)(unsafe.Pointer(p)).FiGet < 0 {
				_samplePushPrevious(tls, p, 0)
				(*TStatAccum)(unsafe.Pointer(p)).FiGet = 0
			}
			if (*TStatAccum)(unsafe.Pointer(p)).FiGet < (*TStatAccum)(unsafe.Pointer(p)).FnSample {
				pS = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*40
				if (*TStatSample)(unsafe.Pointer(pS)).FnRowid == uint32(0) {
					Xsqlite3_result_int64(tls, context, *(*Ti64)(unsafe.Pointer(pS + 16)))
				} else {
					Xsqlite3_result_blob(tls, context, *(*uintptr)(unsafe.Pointer(&(*TStatSample)(unsafe.Pointer(pS)).Fu)), int32((*TStatSample)(unsafe.Pointer(pS)).FnRowid), uintptr(-libc.Int32FromInt32(1)))
				}
			}
		} else {
			aCnt = uintptr(0)
			switch eCall {
			case int32(STAT_GET_NEQ):
				aCnt = (**(**TStatSample)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*40))).FanEq
			case int32(STAT_GET_NLT):
				aCnt = (**(**TStatSample)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*40))).FanLt
			default:
				aCnt = (**(**TStatSample)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*40))).FanDLt
				(*TStatAccum)(unsafe.Pointer(p)).FiGet = (*TStatAccum)(unsafe.Pointer(p)).FiGet + 1
				break
			}
			_sqlite3StrAccumInit(tls, bp+24, uintptr(0), uintptr(0), 0, (*TStatAccum)(unsafe.Pointer(p)).FnCol*int32(100))
			i1 = 0
			for {
				if !(i1 < (*TStatAccum)(unsafe.Pointer(p)).FnCol) {
					break
				}
				Xsqlite3_str_appendf(tls, bp+24, __ccgo_ts+14232, libc.VaList(bp+56, **(**TtRowcnt)(__ccgo_up(aCnt + uintptr(i1)*8))))
				goto _3
			_3:
				;
				i1 = i1 + 1
			}
			if (**(**Tsqlite3_str)(__ccgo_up(bp + 24))).FnChar != 0 {
				(**(**Tsqlite3_str)(__ccgo_up(bp + 24))).FnChar = (**(**Tsqlite3_str)(__ccgo_up(bp + 24))).FnChar - 1
			}
			_sqlite3ResultStrAccum(tls, context, bp+24)
		}
	}
	_ = argc
}

// C documentation
//
//	/*
//	** Load a copy of the page data for page iPg into the buffer belonging
//	** to page object pPg. Allocate the buffer if necessary. Return SQLITE_OK
//	** if successful, or an SQLite error code otherwise.
//	*/
func _statGetPage(tls *libc.TLS, pBt uintptr, iPg Tu32, pPg uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var a uintptr
	var pgsz, rc int32
	var _ /* pDbPage at bp+0 */ uintptr
	_, _, _ = a, pgsz, rc
	pgsz = _sqlite3BtreeGetPageSize(tls, pBt)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	if (*TStatPage)(unsafe.Pointer(pPg)).FaPg == uintptr(0) {
		(*TStatPage)(unsafe.Pointer(pPg)).FaPg = Xsqlite3_malloc(tls, pgsz+int32(DBSTAT_PAGE_PADDING_BYTES))
		if (*TStatPage)(unsafe.Pointer(pPg)).FaPg == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, (*TStatPage)(unsafe.Pointer(pPg)).FaPg+uintptr(pgsz), 0, uint32(DBSTAT_PAGE_PADDING_BYTES))
	}
	rc = _sqlite3PagerGet(tls, _sqlite3BtreePager(tls, pBt), iPg, bp, 0)
	if rc == SQLITE_OK {
		a = _sqlite3PagerGetData(tls, **(**uintptr)(__ccgo_up(bp)))
		libc.Xmemcpy(tls, (*TStatPage)(unsafe.Pointer(pPg)).FaPg, a, uint32(pgsz))
		_sqlite3PagerUnref(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	return rc
}

// C documentation
//
//	/*
//	** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters
//	** are:
//	**     N:    The number of columns in the index including the rowid/pk (note 1)
//	**     K:    The number of columns in the index excluding the rowid/pk.
//	**     C:    Estimated number of rows in the index
//	**     L:    A limit on the number of rows to scan, or 0 for no-limit
//	**
//	** Note 1:  In the special case of the covering index that implements a
//	** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
//	** total number of columns in the table.
//	**
//	** For indexes on ordinary rowid tables, N==K+1.  But for indexes on
//	** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
//	** PRIMARY KEY of the table.  The covering index that implements the
//	** original WITHOUT ROWID table as N==K as a special case.
//	**
//	** This routine allocates the StatAccum object in heap memory. The return
//	** value is a pointer to the StatAccum object.  The datatype of the
//	** return value is BLOB, but it is really just a pointer to the StatAccum
//	** object.
//	*/
func _statInit(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var db, p, pSpace uintptr
	var i, mxSample, nCol, nColUp, nKeyCol, v1 int32
	var n Ti64
	_, _, _, _, _, _, _, _, _, _ = db, i, mxSample, n, nCol, nColUp, nKeyCol, p, pSpace, v1 /* Bytes of space to allocate */
	db = Xsqlite3_context_db_handle(tls, context)
	if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) {
		v1 = int32(SQLITE_STAT4_SAMPLES)
	} else {
		v1 = 0
	} /* Database connection */
	/* Maximum number of samples.  0 if STAT4 data is not collected */
	mxSample = v1
	/* Decode the three function arguments */
	_ = argc
	nCol = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv)))
	nColUp = nCol
	nKeyCol = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	/* Allocate the space required for the StatAccum object */
	n = int64(uint32(120) + uint32(8)*uint32(nColUp)) /* StatAccum.anDLt */
	n = n + int64(uint32(8)*uint32(nColUp))           /* StatAccum.anEq */
	if mxSample != 0 {
		n = n + int64(uint32(8)*uint32(nColUp)+uint32(40)*uint32(nCol+mxSample)+libc.Uint32FromInt64(8)*libc.Uint32FromInt32(3)*uint32(nColUp)*uint32(nCol+mxSample))
	}
	p = _sqlite3DbMallocZero(tls, db, uint64(n))
	if p == uintptr(0) {
		Xsqlite3_result_error_nomem(tls, context)
		return
	}
	(*TStatAccum)(unsafe.Pointer(p)).Fdb = db
	(*TStatAccum)(unsafe.Pointer(p)).FnEst = uint64(Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 2*4))))
	(*TStatAccum)(unsafe.Pointer(p)).FnRow = uint64(0)
	(*TStatAccum)(unsafe.Pointer(p)).FnLimit = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 3*4)))
	(*TStatAccum)(unsafe.Pointer(p)).FnCol = nCol
	(*TStatAccum)(unsafe.Pointer(p)).FnKeyCol = nKeyCol
	(*TStatAccum)(unsafe.Pointer(p)).FnSkipAhead = uint8(0)
	(*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt = p + 1*120
	(*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq = (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt + uintptr(nColUp)*8
	if (*TStatAccum)(unsafe.Pointer(p)).FnLimit == 0 {
		v1 = mxSample
	} else {
		v1 = 0
	}
	(*TStatAccum)(unsafe.Pointer(p)).FmxSample = v1
	if mxSample != 0 { /* Used to iterate through p->aSample[] */
		(*TStatAccum)(unsafe.Pointer(p)).FiGet = -int32(1)
		(*TStatAccum)(unsafe.Pointer(p)).FnPSample = (*TStatAccum)(unsafe.Pointer(p)).FnEst/uint64(mxSample/libc.Int32FromInt32(3)+libc.Int32FromInt32(1)) + libc.Uint64FromInt32(1)
		(*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanLt = (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(nColUp)*8
		(*TStatAccum)(unsafe.Pointer(p)).FiPrn = uint32(0x689e962d)*uint32(nCol) ^ uint32(0xd0944565)*uint32(Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 2*4))))
		/* Set up the StatAccum.a[] and aBest[] arrays */
		(*TStatAccum)(unsafe.Pointer(p)).Fa = (*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanLt + uintptr(nColUp)*8
		(*TStatAccum)(unsafe.Pointer(p)).FaBest = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(mxSample)*40
		pSpace = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(mxSample+nCol)*40
		i = 0
		for {
			if !(i < mxSample+nCol) {
				break
			}
			(**(**TStatSample)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FanEq = pSpace
			pSpace = pSpace + uintptr(libc.Uint32FromInt64(8)*uint32(nColUp))
			(**(**TStatSample)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FanLt = pSpace
			pSpace = pSpace + uintptr(libc.Uint32FromInt64(8)*uint32(nColUp))
			(**(**TStatSample)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*40))).FanDLt = pSpace
			pSpace = pSpace + uintptr(libc.Uint32FromInt64(8)*uint32(nColUp))
			goto _3
		_3:
			;
			i = i + 1
		}
		i = 0
		for {
			if !(i < nCol) {
				break
			}
			(**(**TStatSample)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).FaBest + uintptr(i)*40))).FiCol = i
			goto _4
		_4:
			;
			i = i + 1
		}
	}
	/* Return a pointer to the allocated object to the caller.  Note that
	 ** only the pointer (the 2nd parameter) matters.  The size of the object
	 ** (given by the 3rd parameter) is never used and can be any positive
	 ** value. */
	Xsqlite3_result_blob(tls, context, p, int32(120), __ccgo_fp(_statAccumDestructor))
}

// C documentation
//
//	/*
//	** Move a DBSTAT cursor to the next entry.  Normally, the next
//	** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0),
//	** the next entry is the next btree.
//	*/
func _statNext(tls *libc.TLS, pCursor uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i, iOvfl, nPayload, nUsable, rc, v3 int32
	var iRoot Tu32
	var p, p1, pBt, pCell, pCsr, pPager, pTab, z, v1 uintptr
	var _ /* nPage at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iOvfl, iRoot, nPayload, nUsable, p, p1, pBt, pCell, pCsr, pPager, pTab, rc, z, v1, v3
	pCsr = pCursor
	pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCursor)).FpVtab
	pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TStatTable)(unsafe.Pointer(pTab)).Fdb)).FaDb + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiDb)*16))).FpBt
	pPager = _sqlite3BtreePager(tls, pBt)
	Xsqlite3_free(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath)
	(*TStatCursor)(unsafe.Pointer(pCsr)).FzPath = uintptr(0)
	goto statNextRestart
statNextRestart:
	;
	if (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage < 0 {
		/* Start measuring space on the next btree */
		_statResetCounts(tls, pCsr)
		rc = Xsqlite3_step(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt)
		if rc == int32(SQLITE_ROW) {
			iRoot = uint32(Xsqlite3_column_int64(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt, int32(1)))
			_sqlite3PagerPagecount(tls, pPager, bp)
			if **(**int32)(__ccgo_up(bp)) == 0 {
				(*TStatCursor)(unsafe.Pointer(pCsr)).FisEof = uint8(1)
				return Xsqlite3_reset(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt)
			}
			rc = _statGetPage(tls, pBt, iRoot, pCsr+16)
			(**(**TStatPage)(__ccgo_up(pCsr + 16))).FiPgno = iRoot
			(**(**TStatPage)(__ccgo_up(pCsr + 16))).FiCell = 0
			if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) {
				v1 = Xsqlite3_mprintf(tls, __ccgo_ts+35993, 0)
				z = v1
				(**(**TStatPage)(__ccgo_up(pCsr + 16))).FzPath = v1
				if z == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
				}
			}
			(*TStatCursor)(unsafe.Pointer(pCsr)).FiPage = 0
			(*TStatCursor)(unsafe.Pointer(pCsr)).FnPage = int32(1)
		} else {
			(*TStatCursor)(unsafe.Pointer(pCsr)).FisEof = uint8(1)
			return Xsqlite3_reset(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt)
		}
	} else {
		/* Continue analyzing the btree previously started */
		p = pCsr + 16 + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiPage)*40
		if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) {
			_statResetCounts(tls, pCsr)
		}
		for (*TStatPage)(unsafe.Pointer(p)).FiCell < (*TStatPage)(unsafe.Pointer(p)).FnCell {
			pCell = (*TStatPage)(unsafe.Pointer(p)).FaCell + uintptr((*TStatPage)(unsafe.Pointer(p)).FiCell)*24
			for (*TStatCell)(unsafe.Pointer(pCell)).FiOvfl < (*TStatCell)(unsafe.Pointer(pCell)).FnOvfl {
				_sqlite3BtreeEnter(tls, pBt)
				nUsable = _sqlite3BtreeGetPageSize(tls, pBt) - _sqlite3BtreeGetReserveNoMutex(tls, pBt)
				_sqlite3BtreeLeave(tls, pBt)
				(*TStatCursor)(unsafe.Pointer(pCsr)).FnPage = (*TStatCursor)(unsafe.Pointer(pCsr)).FnPage + 1
				_statSizeAndOffset(tls, pCsr)
				if (*TStatCell)(unsafe.Pointer(pCell)).FiOvfl < (*TStatCell)(unsafe.Pointer(pCell)).FnOvfl-int32(1) {
					**(**Ti64)(__ccgo_up(pCsr + 1336)) += int64(nUsable - int32(4))
				} else {
					**(**Ti64)(__ccgo_up(pCsr + 1336)) += int64((*TStatCell)(unsafe.Pointer(pCell)).FnLastOvfl)
					**(**Ti64)(__ccgo_up(pCsr + 1328)) += int64(nUsable - int32(4) - (*TStatCell)(unsafe.Pointer(pCell)).FnLastOvfl)
				}
				iOvfl = (*TStatCell)(unsafe.Pointer(pCell)).FiOvfl
				(*TStatCell)(unsafe.Pointer(pCell)).FiOvfl = (*TStatCell)(unsafe.Pointer(pCell)).FiOvfl + 1
				if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) {
					(*TStatCursor)(unsafe.Pointer(pCsr)).FzName = Xsqlite3_column_text(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt, 0)
					(*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno = **(**Tu32)(__ccgo_up((*TStatCell)(unsafe.Pointer(pCell)).FaOvfl + uintptr(iOvfl)*4))
					(*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 35995
					v1 = Xsqlite3_mprintf(tls, __ccgo_ts+36004, libc.VaList(bp+16, (*TStatPage)(unsafe.Pointer(p)).FzPath, (*TStatPage)(unsafe.Pointer(p)).FiCell, iOvfl))
					z = v1
					(*TStatCursor)(unsafe.Pointer(pCsr)).FzPath = v1
					if z == uintptr(0) {
						v3 = int32(SQLITE_NOMEM)
					} else {
						v3 = SQLITE_OK
					}
					return v3
				}
			}
			if (*TStatPage)(unsafe.Pointer(p)).FiRightChildPg != 0 {
				break
			}
			(*TStatPage)(unsafe.Pointer(p)).FiCell = (*TStatPage)(unsafe.Pointer(p)).FiCell + 1
		}
		if !((*TStatPage)(unsafe.Pointer(p)).FiRightChildPg != 0) || (*TStatPage)(unsafe.Pointer(p)).FiCell > (*TStatPage)(unsafe.Pointer(p)).FnCell {
			_statClearPage(tls, p)
			(*TStatCursor)(unsafe.Pointer(pCsr)).FiPage = (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage - 1
			if (*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0 && (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage < 0 {
				/* label-statNext-done:  When computing aggregate space usage over
				 ** an entire btree, this is the exit point from this function */
				return SQLITE_OK
			}
			goto statNextRestart /* Tail recursion */
		}
		(*TStatCursor)(unsafe.Pointer(pCsr)).FiPage = (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage + 1
		if (*TStatCursor)(unsafe.Pointer(pCsr)).FiPage >= int32(libc.Uint32FromInt64(1280)/libc.Uint32FromInt64(40)) {
			_statResetCsr(tls, pCsr)
			return _sqlite3CorruptError(tls, int32(232421))
		}
		if (*TStatPage)(unsafe.Pointer(p)).FiCell == (*TStatPage)(unsafe.Pointer(p)).FnCell {
			(**(**TStatPage)(__ccgo_up(p + 1*40))).FiPgno = (*TStatPage)(unsafe.Pointer(p)).FiRightChildPg
		} else {
			(**(**TStatPage)(__ccgo_up(p + 1*40))).FiPgno = (**(**TStatCell)(__ccgo_up((*TStatPage)(unsafe.Pointer(p)).FaCell + uintptr((*TStatPage)(unsafe.Pointer(p)).FiCell)*24))).FiChildPg
		}
		rc = _statGetPage(tls, pBt, (**(**TStatPage)(__ccgo_up(p + 1*40))).FiPgno, p+1*40)
		(*TStatCursor)(unsafe.Pointer(pCsr)).FnPage = (*TStatCursor)(unsafe.Pointer(pCsr)).FnPage + 1
		(**(**TStatPage)(__ccgo_up(p + 1*40))).FiCell = 0
		if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) {
			v1 = Xsqlite3_mprintf(tls, __ccgo_ts+36016, libc.VaList(bp+16, (*TStatPage)(unsafe.Pointer(p)).FzPath, (*TStatPage)(unsafe.Pointer(p)).FiCell))
			z = v1
			(**(**TStatPage)(__ccgo_up(p + 1*40))).FzPath = v1
			if z == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
			}
		}
		(*TStatPage)(unsafe.Pointer(p)).FiCell = (*TStatPage)(unsafe.Pointer(p)).FiCell + 1
	}
	/* Populate the StatCursor fields with the values to be returned
	 ** by the xColumn() and xRowid() methods.
	 */
	if rc == SQLITE_OK {
		p1 = pCsr + 16 + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiPage)*40
		(*TStatCursor)(unsafe.Pointer(pCsr)).FzName = Xsqlite3_column_text(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt, 0)
		(*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno = (*TStatPage)(unsafe.Pointer(p1)).FiPgno
		rc = _statDecodePage(tls, pBt, p1)
		if rc == SQLITE_OK {
			_statSizeAndOffset(tls, pCsr)
			switch int32((*TStatPage)(unsafe.Pointer(p1)).Fflags) {
			case int32(0x05): /* table internal */
				fallthrough
			case int32(0x02): /* index internal */
				(*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 36024
			case int32(0x0D): /* table leaf */
				fallthrough
			case int32(0x0A): /* index leaf */
				(*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 36033
			default:
				(*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 36038
				break
			}
			**(**int32)(__ccgo_up(pCsr + 1320)) += (*TStatPage)(unsafe.Pointer(p1)).FnCell
			**(**Ti64)(__ccgo_up(pCsr + 1328)) += int64((*TStatPage)(unsafe.Pointer(p1)).FnUnused)
			if (*TStatPage)(unsafe.Pointer(p1)).FnMxPayload > (*TStatCursor)(unsafe.Pointer(pCsr)).FnMxPayload {
				(*TStatCursor)(unsafe.Pointer(pCsr)).FnMxPayload = (*TStatPage)(unsafe.Pointer(p1)).FnMxPayload
			}
			if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) {
				v1 = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+16, (*TStatPage)(unsafe.Pointer(p1)).FzPath))
				z = v1
				(*TStatCursor)(unsafe.Pointer(pCsr)).FzPath = v1
				if z == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
				}
			}
			nPayload = 0
			i = 0
			for {
				if !(i < (*TStatPage)(unsafe.Pointer(p1)).FnCell) {
					break
				}
				nPayload = nPayload + (**(**TStatCell)(__ccgo_up((*TStatPage)(unsafe.Pointer(p1)).FaCell + uintptr(i)*24))).FnLocal
				goto _6
			_6:
				;
				i = i + 1
			}
			**(**Ti64)(__ccgo_up(pCsr + 1336)) += int64(nPayload)
			/* If computing aggregate space usage by btree, continue with the
			 ** next page.  The loop will exit via the return at label-statNext-done
			 */
			if (*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0 {
				goto statNextRestart
			}
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Implementation of the stat_push SQL function:  stat_push(P,C,R)
//	** Arguments:
//	**
//	**    P     Pointer to the StatAccum object created by stat_init()
//	**    C     Index of left-most column to differ from previous row
//	**    R     Rowid for the current row.  Might be a key record for
//	**          WITHOUT ROWID tables.
//	**
//	** The purpose of this routine is to collect statistical data and/or
//	** samples from the index being analyzed into the StatAccum object.
//	** The stat_get() SQL function will be used afterwards to
//	** retrieve the information gathered.
//	**
//	** This SQL function usually returns NULL, but might return an integer
//	** if it wants the byte-code to do special processing.
//	**
//	** The R parameter is only used for STAT4
//	*/
func _statPush(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var i, iChng int32
	var nLt TtRowcnt
	var p uintptr
	var v4 Tu32
	_, _, _, _, _ = i, iChng, nLt, p, v4
	/* The three function arguments */
	p = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv)))
	iChng = Xsqlite3_value_int(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	_ = argc
	_ = context
	if (*TStatAccum)(unsafe.Pointer(p)).FnRow == uint64(0) {
		/* This is the first call to this function. Do initialization. */
		i = 0
		for {
			if !(i < (*TStatAccum)(unsafe.Pointer(p)).FnCol) {
				break
			}
			**(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8)) = uint64(1)
			goto _1
		_1:
			;
			i = i + 1
		}
	} else {
		/* Second and subsequent calls get processed here */
		if (*TStatAccum)(unsafe.Pointer(p)).FmxSample != 0 {
			_samplePushPrevious(tls, p, iChng)
		}
		/* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
		 ** to the current row of the index. */
		i = 0
		for {
			if !(i < iChng) {
				break
			}
			**(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8)) = **(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8)) + 1
			goto _2
		_2:
			;
			i = i + 1
		}
		i = iChng
		for {
			if !(i < (*TStatAccum)(unsafe.Pointer(p)).FnCol) {
				break
			}
			**(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt + uintptr(i)*8)) = **(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt + uintptr(i)*8)) + 1
			if (*TStatAccum)(unsafe.Pointer(p)).FmxSample != 0 {
				**(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanLt + uintptr(i)*8)) += **(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8))
			}
			**(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanEq + uintptr(i)*8)) = uint64(1)
			goto _3
		_3:
			;
			i = i + 1
		}
	}
	(*TStatAccum)(unsafe.Pointer(p)).FnRow = (*TStatAccum)(unsafe.Pointer(p)).FnRow + 1
	if (*TStatAccum)(unsafe.Pointer(p)).FmxSample != 0 {
		if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 2*4))) == int32(SQLITE_INTEGER) {
			_sampleSetRowidInt64(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, p+40, Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 2*4))))
		} else {
			_sampleSetRowid(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, p+40, Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 2*4))), Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv + 2*4))))
		}
		v4 = (*TStatAccum)(unsafe.Pointer(p)).FiPrn*libc.Uint32FromInt32(1103515245) + libc.Uint32FromInt32(12345)
		(*TStatAccum)(unsafe.Pointer(p)).FiPrn = v4
		(*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FiHash = v4
		nLt = **(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanLt + uintptr((*TStatAccum)(unsafe.Pointer(p)).FnCol-int32(1))*8))
		/* Check if this is to be a periodic sample. If so, add it. */
		if nLt/(*TStatAccum)(unsafe.Pointer(p)).FnPSample != (nLt+uint64(1))/(*TStatAccum)(unsafe.Pointer(p)).FnPSample {
			(*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FisPSample = uint8(1)
			(*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FiCol = 0
			_sampleInsert(tls, p, p+40, (*TStatAccum)(unsafe.Pointer(p)).FnCol-int32(1))
			(*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FisPSample = uint8(0)
		}
		/* Update the aBest[] array. */
		i = 0
		for {
			if !(i < (*TStatAccum)(unsafe.Pointer(p)).FnCol-int32(1)) {
				break
			}
			(*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FiCol = i
			if i >= iChng || _sampleIsBetterPost(tls, p, p+40, (*TStatAccum)(unsafe.Pointer(p)).FaBest+uintptr(i)*40) != 0 {
				_sampleCopy(tls, p, (*TStatAccum)(unsafe.Pointer(p)).FaBest+uintptr(i)*40, p+40)
			}
			goto _5
		_5:
			;
			i = i + 1
		}
	} else {
		if (*TStatAccum)(unsafe.Pointer(p)).FnLimit != 0 && (*TStatAccum)(unsafe.Pointer(p)).FnRow > uint64((*TStatAccum)(unsafe.Pointer(p)).FnLimit)*uint64(int32((*TStatAccum)(unsafe.Pointer(p)).FnSkipAhead)+libc.Int32FromInt32(1)) {
			(*TStatAccum)(unsafe.Pointer(p)).FnSkipAhead = (*TStatAccum)(unsafe.Pointer(p)).FnSkipAhead + 1
			Xsqlite3_result_int(tls, context, libc.BoolInt32(**(**TtRowcnt)(__ccgo_up((*TStatAccum)(unsafe.Pointer(p)).Fcurrent.FanDLt)) > uint64(0)))
		}
	}
}

func _statResetCsr(tls *libc.TLS, pCsr uintptr) {
	var i int32
	_ = i
	/* In some circumstances, specifically if an OOM has occurred, the call
	 ** to sqlite3_reset() may cause the pager to be reset (emptied). It is
	 ** important that statClearPage() is called to free any page refs before
	 ** this happens. dbsqlfuzz 9ed3e4e3816219d3509d711636c38542bf3f40b1. */
	i = 0
	for {
		if !(i < int32(libc.Uint32FromInt64(1280)/libc.Uint32FromInt64(40))) {
			break
		}
		_statClearPage(tls, pCsr+16+uintptr(i)*40)
		Xsqlite3_free(tls, (**(**TStatPage)(__ccgo_up(pCsr + 16 + uintptr(i)*40))).FaPg)
		(**(**TStatPage)(__ccgo_up(pCsr + 16 + uintptr(i)*40))).FaPg = uintptr(0)
		goto _1
	_1:
		;
		i = i + 1
	}
	Xsqlite3_reset(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FpStmt)
	(*TStatCursor)(unsafe.Pointer(pCsr)).FiPage = 0
	Xsqlite3_free(tls, (*TStatCursor)(unsafe.Pointer(pCsr)).FzPath)
	(*TStatCursor)(unsafe.Pointer(pCsr)).FzPath = uintptr(0)
	(*TStatCursor)(unsafe.Pointer(pCsr)).FisEof = uint8(0)
}

// C documentation
//
//	/*
//	** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
//	** the current value of pCsr->iPageno.
//	*/
func _statSizeAndOffset(tls *libc.TLS, pCsr uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var fd, pBt, pPager, pTab uintptr
	var _ /* x at bp+0 */ [2]Tsqlite3_int64
	_, _, _, _ = fd, pBt, pPager, pTab
	pTab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(pCsr)).FpVtab
	pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer((*TStatTable)(unsafe.Pointer(pTab)).Fdb)).FaDb + uintptr((*TStatTable)(unsafe.Pointer(pTab)).FiDb)*16))).FpBt
	pPager = _sqlite3BtreePager(tls, pBt)
	/* If connected to a ZIPVFS backend, find the page size and
	 ** offset from ZIPVFS.
	 */
	fd = _sqlite3PagerFile(tls, pPager)
	(**(**[2]Tsqlite3_int64)(__ccgo_up(bp)))[0] = int64((*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno)
	if _sqlite3OsFileControl(tls, fd, int32(230440), bp) == SQLITE_OK {
		(*TStatCursor)(unsafe.Pointer(pCsr)).FiOffset = (**(**[2]Tsqlite3_int64)(__ccgo_up(bp)))[0]
		**(**Ti64)(__ccgo_up(pCsr + 1352)) += (**(**[2]Tsqlite3_int64)(__ccgo_up(bp)))[int32(1)]
	} else {
		/* Not ZIPVFS: The default page size and offset */
		**(**Ti64)(__ccgo_up(pCsr + 1352)) += int64(_sqlite3BtreeGetPageSize(tls, pBt))
		(*TStatCursor)(unsafe.Pointer(pCsr)).FiOffset = (*TStatCursor)(unsafe.Pointer(pCsr)).FszPage * int64((*TStatCursor)(unsafe.Pointer(pCsr)).FiPageno-libc.Uint32FromInt32(1))
	}
}

// C documentation
//
//	/*
//	**    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
//	**
//	** Return a string described by FORMAT.  Conversions as follows:
//	**
//	**   %d  day of month  01-31
//	**   %e  day of month  1-31
//	**   %f  ** fractional seconds  SS.SSS
//	**   %F  ISO date.  YYYY-MM-DD
//	**   %G  ISO year corresponding to %V 0000-9999.
//	**   %g  2-digit ISO year corresponding to %V 00-99
//	**   %H  hour 00-24
//	**   %k  hour  0-24  (leading zero converted to space)
//	**   %I  hour 01-12
//	**   %j  day of year 001-366
//	**   %J  ** julian day number
//	**   %l  hour  1-12  (leading zero converted to space)
//	**   %m  month 01-12
//	**   %M  minute 00-59
//	**   %p  "AM" or "PM"
//	**   %P  "am" or "pm"
//	**   %R  time as HH:MM
//	**   %s  seconds since 1970-01-01
//	**   %S  seconds 00-59
//	**   %T  time as HH:MM:SS
//	**   %u  day of week 1-7  Monday==1, Sunday==7
//	**   %w  day of week 0-6  Sunday==0, Monday==1
//	**   %U  week of year 00-53  (First Sunday is start of week 01)
//	**   %V  week of year 01-53  (First week containing Thursday is week 01)
//	**   %W  week of year 00-53  (First Monday is start of week 01)
//	**   %Y  year 0000-9999
//	**   %%  %
//	*/
func _strftimeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(208)
	defer tls.Free(208)
	var c, cf int8
	var db, zFmt, v3 uintptr
	var h int32
	var i, j, v2 Tsize_t
	var iS Ti64
	var s float64
	var _ /* sRes at bp+48 */ Tsqlite3_str
	var _ /* x at bp+0 */ TDateTime
	var _ /* y at bp+120 */ TDateTime
	var _ /* y at bp+72 */ TDateTime
	_, _, _, _, _, _, _, _, _, _, _ = c, cf, db, h, i, iS, j, s, zFmt, v2, v3
	if argc == 0 {
		return
	}
	zFmt = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	if zFmt == uintptr(0) || _isDate(tls, context, argc-int32(1), argv+uintptr(1)*4, bp) != 0 {
		return
	}
	db = Xsqlite3_context_db_handle(tls, context)
	_sqlite3StrAccumInit(tls, bp+48, uintptr(0), uintptr(0), 0, **(**int32)(__ccgo_up(db + 120)))
	_computeJD(tls, bp)
	_computeYMD_HMS(tls, bp)
	v2 = libc.Uint32FromInt32(0)
	j = v2
	i = v2
	for {
		if !(**(**int8)(__ccgo_up(zFmt + uintptr(i))) != 0) {
			break
		}
		if int32(**(**int8)(__ccgo_up(zFmt + uintptr(i)))) != int32('%') {
			goto _1
		}
		if j < i {
			Xsqlite3_str_append(tls, bp+48, zFmt+uintptr(j), int32(i-j))
		}
		i = i + 1
		j = i + uint32(1)
		cf = **(**int8)(__ccgo_up(zFmt + uintptr(i)))
		switch int32(cf) {
		case int32('d'): /* Fall thru */
			fallthrough
		case int32('e'):
			if int32(cf) == int32('d') {
				v3 = __ccgo_ts + 1398
			} else {
				v3 = __ccgo_ts + 1403
			}
			Xsqlite3_str_appendf(tls, bp+48, v3, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp))).FD))
		case int32('f'): /* Fractional seconds.  (Non-standard) */
			s = (**(**TDateTime)(__ccgo_up(bp))).Fs
			if s > float64(59.999) {
				s = float64(59.999)
			}
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1407, libc.VaList(bp+176, s))
		case int32('F'):
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1414, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp))).FY, (**(**TDateTime)(__ccgo_up(bp))).FM, (**(**TDateTime)(__ccgo_up(bp))).FD))
		case int32('G'): /* Fall thru */
			fallthrough
		case int32('g'):
			**(**TDateTime)(__ccgo_up(bp + 72)) = **(**TDateTime)(__ccgo_up(bp))
			/* Move y so that it is the Thursday in the same week as x */
			(**(**TDateTime)(__ccgo_up(bp + 72))).FiJD += int64((int32(3) - _daysAfterMonday(tls, bp)) * int32(86400000))
			(**(**TDateTime)(__ccgo_up(bp + 72))).FvalidYMD = 0
			_computeYMD(tls, bp+72)
			if int32(cf) == int32('g') {
				Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1398, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp + 72))).FY%int32(100)))
			} else {
				Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1429, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp + 72))).FY))
			}
		case int32('H'):
			fallthrough
		case int32('k'):
			if int32(cf) == int32('H') {
				v3 = __ccgo_ts + 1398
			} else {
				v3 = __ccgo_ts + 1403
			}
			Xsqlite3_str_appendf(tls, bp+48, v3, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp))).Fh))
		case int32('I'): /* Fall thru */
			fallthrough
		case int32('l'):
			h = (**(**TDateTime)(__ccgo_up(bp))).Fh
			if h > int32(12) {
				h = h - int32(12)
			}
			if h == 0 {
				h = int32(12)
			}
			if int32(cf) == int32('I') {
				v3 = __ccgo_ts + 1398
			} else {
				v3 = __ccgo_ts + 1403
			}
			Xsqlite3_str_appendf(tls, bp+48, v3, libc.VaList(bp+176, h))
		case int32('j'): /* Day of year.  Jan01==1, Jan02==2, and so forth */
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1434, libc.VaList(bp+176, _daysAfterJan01(tls, bp)+int32(1)))
		case int32('J'): /* Julian day number.  (Non-standard) */
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1439, libc.VaList(bp+176, float64((**(**TDateTime)(__ccgo_up(bp))).FiJD)/float64(8.64e+07)))
		case int32('m'):
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1398, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp))).FM))
		case int32('M'):
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1398, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp))).Fm))
		case int32('p'): /* Fall thru */
			fallthrough
		case int32('P'):
			if (**(**TDateTime)(__ccgo_up(bp))).Fh >= int32(12) {
				if int32(cf) == int32('p') {
					v3 = __ccgo_ts + 1445
				} else {
					v3 = __ccgo_ts + 1448
				}
				Xsqlite3_str_append(tls, bp+48, v3, int32(2))
			} else {
				if int32(cf) == int32('p') {
					v3 = __ccgo_ts + 1451
				} else {
					v3 = __ccgo_ts + 1454
				}
				Xsqlite3_str_append(tls, bp+48, v3, int32(2))
			}
		case int32('R'):
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1457, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp))).Fh, (**(**TDateTime)(__ccgo_up(bp))).Fm))
		case int32('s'):
			if int32(uint32(*(*uint8)(unsafe.Pointer(bp + 44))&0x4>>2)) != 0 {
				Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1467, libc.VaList(bp+176, float64((**(**TDateTime)(__ccgo_up(bp))).FiJD-libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000000))/float64(1000)))
			} else {
				iS = (**(**TDateTime)(__ccgo_up(bp))).FiJD/libc.Int64FromInt32(1000) - libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000)
				Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1472, libc.VaList(bp+176, iS))
			}
		case int32('S'):
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1398, libc.VaList(bp+176, int32((**(**TDateTime)(__ccgo_up(bp))).Fs)))
		case int32('T'):
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1477, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp))).Fh, (**(**TDateTime)(__ccgo_up(bp))).Fm, int32((**(**TDateTime)(__ccgo_up(bp))).Fs)))
		case int32('u'): /* Day of week.  1 to 7.  Monday==1, Sunday==7 */
			fallthrough
		case int32('w'): /* Day of week.  0 to 6.  Sunday==0, Monday==1 */
			c = int8(int32(int8(_daysAfterSunday(tls, bp))) + int32('0'))
			if int32(c) == int32('0') && int32(cf) == int32('u') {
				c = int8('7')
			}
			Xsqlite3_str_appendchar(tls, bp+48, int32(1), c)
		case int32('U'): /* Week num. 00-53. First Sun of the year is week 01 */
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1398, libc.VaList(bp+176, (_daysAfterJan01(tls, bp)-_daysAfterSunday(tls, bp)+int32(7))/int32(7)))
		case int32('V'): /* Week num. 01-53. First week with a Thur is week 01 */
			**(**TDateTime)(__ccgo_up(bp + 120)) = **(**TDateTime)(__ccgo_up(bp))
			/* Adjust y so that is the Thursday in the same week as x */
			(**(**TDateTime)(__ccgo_up(bp + 120))).FiJD += int64((int32(3) - _daysAfterMonday(tls, bp)) * int32(86400000))
			(**(**TDateTime)(__ccgo_up(bp + 120))).FvalidYMD = 0
			_computeYMD(tls, bp+120)
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1398, libc.VaList(bp+176, _daysAfterJan01(tls, bp+120)/int32(7)+int32(1)))
		case int32('W'): /* Week num. 00-53. First Mon of the year is week 01 */
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1398, libc.VaList(bp+176, (_daysAfterJan01(tls, bp)-_daysAfterMonday(tls, bp)+int32(7))/int32(7)))
		case int32('Y'):
			Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1429, libc.VaList(bp+176, (**(**TDateTime)(__ccgo_up(bp))).FY))
		case int32('%'):
			Xsqlite3_str_appendchar(tls, bp+48, int32(1), int8('%'))
		default:
			Xsqlite3_str_reset(tls, bp+48)
			return
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if j < i {
		Xsqlite3_str_append(tls, bp+48, zFmt+uintptr(j), int32(i-j))
	}
	_sqlite3ResultStrAccum(tls, context, bp+48)
}

// C documentation
//
//	/*
//	** Scan through the expression pExpr.  Replace every reference to
//	** a column in table number iTable with a copy of the iColumn-th
//	** entry in pEList.  (But leave references to the ROWID column
//	** unchanged.)
//	**
//	** This routine is part of the flattening procedure.  A subquery
//	** whose result set is defined by pEList appears as entry in the
//	** FROM clause of a SELECT such that the VDBE cursor assigned to that
//	** FORM clause entry is iTable.  This routine makes the necessary
//	** changes to pExpr so that it refers directly to the source table
//	** of the subquery rather the result set of the subquery.
//	*/
func _substExpr(tls *libc.TLS, pSubst uintptr, pExpr uintptr) (r uintptr) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var db, pColl, pCopy, pNat, pNew, pWin, v1 uintptr
	var iColumn int32
	var _ /* ifNullRow at bp+0 */ TExpr
	_, _, _, _, _, _, _, _ = db, iColumn, pColl, pCopy, pNat, pNew, pWin, v1
	if pExpr == uintptr(0) {
		return uintptr(0)
	}
	if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) && *(*int32)(unsafe.Pointer(pExpr + 36)) == (*TSubstContext)(unsafe.Pointer(pSubst)).FiTable {
		*(*int32)(unsafe.Pointer(pExpr + 36)) = (*TSubstContext)(unsafe.Pointer(pSubst)).FiNewTable
	}
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TSubstContext)(unsafe.Pointer(pSubst)).FiTable && !((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != libc.Uint32FromInt32(0)) {
		iColumn = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)
		pCopy = (*(*TExprList_item)(unsafe.Pointer((*TSubstContext)(unsafe.Pointer(pSubst)).FpEList + 8 + uintptr(iColumn)*20))).FpExpr
		if _sqlite3ExprIsVector(tls, pCopy) != 0 {
			_sqlite3VectorErrorMsg(tls, (*TSubstContext)(unsafe.Pointer(pSubst)).FpParse, pCopy)
		} else {
			db = (*TParse)(unsafe.Pointer((*TSubstContext)(unsafe.Pointer(pSubst)).FpParse)).Fdb
			if (*TSubstContext)(unsafe.Pointer(pSubst)).FisOuterJoin != 0 && (int32((*TExpr)(unsafe.Pointer(pCopy)).Fop) != int32(TK_COLUMN) || (*TExpr)(unsafe.Pointer(pCopy)).FiTable != (*TSubstContext)(unsafe.Pointer(pSubst)).FiNewTable) {
				libc.Xmemset(tls, bp, 0, uint32(52))
				(**(**TExpr)(__ccgo_up(bp))).Fop = uint8(TK_IF_NULL_ROW)
				(**(**TExpr)(__ccgo_up(bp))).FpLeft = pCopy
				(**(**TExpr)(__ccgo_up(bp))).FiTable = (*TSubstContext)(unsafe.Pointer(pSubst)).FiNewTable
				(**(**TExpr)(__ccgo_up(bp))).FiColumn = int16(-int32(99))
				(**(**TExpr)(__ccgo_up(bp))).Fflags = uint32(EP_IfNullRow)
				pCopy = bp
			}
			pNew = _sqlite3ExprDup(tls, db, pCopy, 0)
			if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
				_sqlite3ExprDelete(tls, db, pNew)
				return pExpr
			}
			if (*TSubstContext)(unsafe.Pointer(pSubst)).FisOuterJoin != 0 {
				**(**Tu32)(__ccgo_up(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_CanBeNull))
			}
			if int32((*TExpr)(unsafe.Pointer(pNew)).Fop) == int32(TK_TRUEFALSE) {
				*(*int32)(unsafe.Pointer(pNew + 8)) = _sqlite3ExprTruthValue(tls, pNew)
				(*TExpr)(unsafe.Pointer(pNew)).Fop = uint8(TK_INTEGER)
				**(**Tu32)(__ccgo_up(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_IntValue))
			}
			/* Ensure that the expression now has an implicit collation sequence,
			 ** just as it did when it was a column of a view or sub-query. */
			pNat = _sqlite3ExprCollSeq(tls, (*TSubstContext)(unsafe.Pointer(pSubst)).FpParse, pNew)
			pColl = _sqlite3ExprCollSeq(tls, (*TSubstContext)(unsafe.Pointer(pSubst)).FpParse, (*(*TExprList_item)(unsafe.Pointer((*TSubstContext)(unsafe.Pointer(pSubst)).FpCList + 8 + uintptr(iColumn)*20))).FpExpr)
			if pNat != pColl || int32((*TExpr)(unsafe.Pointer(pNew)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pNew)).Fop) != int32(TK_COLLATE) {
				if pColl != 0 {
					v1 = (*TCollSeq)(unsafe.Pointer(pColl)).FzName
				} else {
					v1 = __ccgo_ts + 6454
				}
				pNew = _sqlite3ExprAddCollateString(tls, (*TSubstContext)(unsafe.Pointer(pSubst)).FpParse, pNew, v1)
			}
			**(**Tu32)(__ccgo_up(pNew + 4)) &= ^uint32(libc.Int32FromInt32(EP_Collate))
			if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) {
				_sqlite3SetJoinExpr(tls, pNew, *(*int32)(unsafe.Pointer(pExpr + 36)), (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)))
			}
			_sqlite3ExprDelete(tls, db, pExpr)
			pExpr = pNew
		}
	} else {
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_IF_NULL_ROW) && (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*TSubstContext)(unsafe.Pointer(pSubst)).FiTable {
			(*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TSubstContext)(unsafe.Pointer(pSubst)).FiNewTable
		}
		if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_FUNCTION) && int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2) >= (*TSubstContext)(unsafe.Pointer(pSubst)).FnSelDepth {
			(*TExpr)(unsafe.Pointer(pExpr)).Fop2 = (*TExpr)(unsafe.Pointer(pExpr)).Fop2 - 1
		}
		(*TExpr)(unsafe.Pointer(pExpr)).FpLeft = _substExpr(tls, pSubst, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
		(*TExpr)(unsafe.Pointer(pExpr)).FpRight = _substExpr(tls, pSubst, (*TExpr)(unsafe.Pointer(pExpr)).FpRight)
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
			_substSelect(tls, pSubst, *(*uintptr)(unsafe.Pointer(pExpr + 20)), int32(1))
		} else {
			_substExprList(tls, pSubst, *(*uintptr)(unsafe.Pointer(pExpr + 20)))
		}
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
			pWin = *(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fy))
			(*TWindow)(unsafe.Pointer(pWin)).FpFilter = _substExpr(tls, pSubst, (*TWindow)(unsafe.Pointer(pWin)).FpFilter)
			_substExprList(tls, pSubst, (*TWindow)(unsafe.Pointer(pWin)).FpPartition)
			_substExprList(tls, pSubst, (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy)
		}
	}
	return pExpr
}

// C documentation
//
//	/*
//	** Implementation of the substr() function.
//	**
//	** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
//	** p1 is 1-indexed.  So substr(x,1,1) returns the first character
//	** of x.  If x is text, then we actually count UTF-8 characters.
//	** If x is a blob, then we count bytes.
//	**
//	** If p1 is negative, then we begin abs(p1) from the end of x[].
//	**
//	** If p2 is negative, return the p2 characters preceding p1.
//	*/
func _substrFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var len1, p0type int32
	var p1, p2, v6 Ti64
	var z, z2, v2 uintptr
	_, _, _, _, _, _, _, _ = len1, p0type, p1, p2, z, z2, v2, v6
	p0type = Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv)))
	p1 = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	if p0type == int32(SQLITE_BLOB) {
		len1 = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
		z = Xsqlite3_value_blob(tls, **(**uintptr)(__ccgo_up(argv)))
		if z == uintptr(0) {
			return
		}
	} else {
		z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		if z == uintptr(0) {
			return
		}
		len1 = 0
		if p1 < 0 {
			z2 = z
			for {
				if !(**(**uint8)(__ccgo_up(z2)) != 0) {
					break
				}
				v2 = z2
				z2 = z2 + 1
				if int32(**(**uint8)(__ccgo_up(v2))) >= int32(0xc0) {
					for int32(**(**uint8)(__ccgo_up(z2)))&int32(0xc0) == int32(0x80) {
						z2 = z2 + 1
					}
				}
				goto _1
			_1:
				;
				len1 = len1 + 1
			}
		}
	}
	if argc == int32(3) {
		p2 = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv + 2*4)))
		if p2 == 0 && Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 2*4))) == int32(SQLITE_NULL) {
			return
		}
	} else {
		p2 = int64(**(**int32)(__ccgo_up(Xsqlite3_context_db_handle(tls, context) + 120)))
	}
	if p1 == 0 {
		if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv + 1*4))) == int32(SQLITE_NULL) {
			return
		}
	}
	if p1 < 0 {
		p1 = p1 + int64(len1)
		if p1 < 0 {
			if p2 < 0 {
				p2 = 0
			} else {
				p2 = p2 + p1
			}
			p1 = 0
		}
	} else {
		if p1 > 0 {
			p1 = p1 - 1
		} else {
			if p2 > 0 {
				p2 = p2 - 1
			}
		}
	}
	if p2 < 0 {
		if p2 < -p1 {
			p2 = p1
		} else {
			p2 = -p2
		}
		p1 = p1 - p2
	}
	if p0type != int32(SQLITE_BLOB) {
		for **(**uint8)(__ccgo_up(z)) != 0 && p1 != 0 {
			v2 = z
			z = z + 1
			if int32(**(**uint8)(__ccgo_up(v2))) >= int32(0xc0) {
				for int32(**(**uint8)(__ccgo_up(z)))&int32(0xc0) == int32(0x80) {
					z = z + 1
				}
			}
			p1 = p1 - 1
		}
		z2 = z
		for {
			if !(**(**uint8)(__ccgo_up(z2)) != 0 && p2 != 0) {
				break
			}
			v2 = z2
			z2 = z2 + 1
			if int32(**(**uint8)(__ccgo_up(v2))) >= int32(0xc0) {
				for int32(**(**uint8)(__ccgo_up(z2)))&int32(0xc0) == int32(0x80) {
					z2 = z2 + 1
				}
			}
			goto _4
		_4:
			;
			p2 = p2 - 1
		}
		Xsqlite3_result_text64(tls, context, z, uint64(int32(z2)-int32(z)), uintptr(-libc.Int32FromInt32(1)), uint8(SQLITE_UTF8))
	} else {
		if p1 >= int64(len1) {
			v6 = libc.Int64FromInt32(0)
			p2 = v6
			p1 = v6
		} else {
			if p2 > int64(len1)-p1 {
				p2 = int64(len1) - p1
			}
		}
		Xsqlite3_result_blob64(tls, context, z+uintptr(p1), uint64(p2), uintptr(-libc.Int32FromInt32(1)))
	}
}

// C documentation
//
//	/*
//	** Sync the journal. In other words, make sure all the pages that have
//	** been written to the journal have actually reached the surface of the
//	** disk and can be restored in the event of a hot-journal rollback.
//	**
//	** If the Pager.noSync flag is set, then this function is a no-op.
//	** Otherwise, the actions required depend on the journal-mode and the
//	** device characteristics of the file-system, as follows:
//	**
//	**   * If the journal file is an in-memory journal file, no action need
//	**     be taken.
//	**
//	**   * Otherwise, if the device does not support the SAFE_APPEND property,
//	**     then the nRec field of the most recently written journal header
//	**     is updated to contain the number of journal records that have
//	**     been written following it. If the pager is operating in full-sync
//	**     mode, then the journal file is synced before this field is updated.
//	**
//	**   * If the device does not support the SEQUENTIAL property, then
//	**     journal file is synced.
//	**
//	** Or, in pseudo-code:
//	**
//	**   if( NOT <in-memory journal> ){
//	**     if( NOT SAFE_APPEND ){
//	**       if( <full-sync mode> ) xSync(<journal file>);
//	**       <update nRec field>
//	**     }
//	**     if( NOT SEQUENTIAL ) xSync(<journal file>);
//	**   }
//	**
//	** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
//	** page currently held in memory before returning SQLITE_OK. If an IO
//	** error is encountered, then the IO error code is returned to the caller.
//	*/
func _syncJournal(tls *libc.TLS, pPager uintptr, newHdr int32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var iDc, rc, v1 int32
	var iNextHdrOffset Ti64
	var _ /* aMagic at bp+0 */ [8]Tu8
	var _ /* zHeader at bp+8 */ [12]Tu8
	_, _, _, _ = iDc, iNextHdrOffset, rc, v1 /* Return code */
	rc = _sqlite3PagerExclusiveLock(tls, pPager)
	if rc != SQLITE_OK {
		return rc
	}
	if !((*TPager)(unsafe.Pointer(pPager)).FnoSync != 0) {
		if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(0) && int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) != int32(PAGER_JOURNALMODE_MEMORY) {
			iDc = _sqlite3OsDeviceCharacteristics(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd)
			if 0 == iDc&int32(SQLITE_IOCAP_SAFE_APPEND) {
				libc.Xmemcpy(tls, bp+8, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8))
				_sqlite3Put4byte(tls, bp+8+uintptr(8), uint32((*TPager)(unsafe.Pointer(pPager)).FnRec))
				iNextHdrOffset = _journalHdrOffset(tls, pPager)
				rc = _sqlite3OsRead(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp, int32(8), iNextHdrOffset)
				if rc == SQLITE_OK && 0 == libc.Xmemcmp(tls, bp, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8)) {
					rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, uintptr(unsafe.Pointer(&_zerobyte)), int32(1), iNextHdrOffset)
				}
				if rc != SQLITE_OK && rc != libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(2)<<libc.Int32FromInt32(8) {
					return rc
				}
				/* Write the nRec value into the journal file header. If in
				 ** full-synchronous mode, sync the journal first. This ensures that
				 ** all data has really hit the disk before nRec is updated to mark
				 ** it as a candidate for rollback.
				 **
				 ** This is not required if the persistent media supports the
				 ** SAFE_APPEND property. Because in this case it is not possible
				 ** for garbage data to be appended to the file, the nRec field
				 ** is populated with 0xFFFFFFFF when the journal header is written
				 ** and never needs to be updated.
				 */
				if (*TPager)(unsafe.Pointer(pPager)).FfullSync != 0 && 0 == iDc&int32(SQLITE_IOCAP_SEQUENTIAL) {
					rc = _sqlite3OsSync(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags))
					if rc != SQLITE_OK {
						return rc
					}
				}
				rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp+8, int32(12), (*TPager)(unsafe.Pointer(pPager)).FjournalHdr)
				if rc != SQLITE_OK {
					return rc
				}
			}
			if 0 == iDc&int32(SQLITE_IOCAP_SEQUENTIAL) {
				if int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags) == int32(SQLITE_SYNC_FULL) {
					v1 = int32(SQLITE_SYNC_DATAONLY)
				} else {
					v1 = 0
				}
				rc = _sqlite3OsSync(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, int32((*TPager)(unsafe.Pointer(pPager)).FsyncFlags)|v1)
				if rc != SQLITE_OK {
					return rc
				}
			}
			(*TPager)(unsafe.Pointer(pPager)).FjournalHdr = (*TPager)(unsafe.Pointer(pPager)).FjournalOff
			if newHdr != 0 && 0 == iDc&int32(SQLITE_IOCAP_SAFE_APPEND) {
				(*TPager)(unsafe.Pointer(pPager)).FnRec = 0
				rc = _writeJournalHdr(tls, pPager)
				if rc != SQLITE_OK {
					return rc
				}
			}
		} else {
			(*TPager)(unsafe.Pointer(pPager)).FjournalHdr = (*TPager)(unsafe.Pointer(pPager)).FjournalOff
		}
	}
	/* Unless the pager is in noSync mode, the journal file was just
	 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
	 ** all pages.
	 */
	_sqlite3PcacheClearSyncFlags(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache)
	(*TPager)(unsafe.Pointer(pPager)).FeState = uint8(PAGER_WRITER_DBMOD)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a
//	** table that has a column named zCol.  The search is left-to-right.
//	** The first match found is returned.
//	**
//	** When found, set *piTab and *piCol to the table index and column index
//	** of the matching column and return TRUE.
//	**
//	** If not found, return FALSE.
//	*/
func _tableAndColumnIndex(tls *libc.TLS, pSrc uintptr, iStart int32, iEnd int32, zCol uintptr, piTab uintptr, piCol uintptr, bIgnoreHidden int32) (r int32) {
	var i, iCol int32
	_, _ = i, iCol /* Index of column matching zCol */
	/* Both or neither are NULL */
	i = iStart
	for {
		if !(i <= iEnd) {
			break
		}
		iCol = _sqlite3ColumnIndex(tls, (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48))).FpSTab, zCol)
		if iCol >= 0 && (bIgnoreHidden == 0 || libc.BoolInt32(int32((*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*48))).FpSTab)).FaCol+uintptr(iCol)*12)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0) == 0) {
			if piTab != 0 {
				_sqlite3SrcItemColumnUsed(tls, pSrc+8+uintptr(i)*48, iCol)
				**(**int32)(__ccgo_up(piTab)) = i
				**(**int32)(__ccgo_up(piCol)) = iCol
			}
			return int32(1)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** Return TRUE if the WHERE clause term pTerm is of a form where it
//	** could be used with an index to access pSrc, assuming an appropriate
//	** index existed.
//	*/
func _termCanDriveIndex(tls *libc.TLS, pTerm uintptr, pSrc uintptr, notReady TBitmask) (r int32) {
	var aff int8
	var leftCol int32
	_, _ = aff, leftCol
	if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor != (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor {
		return 0
	}
	if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) == 0 {
		return 0
	}
	if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 && !(_constraintCompatibleWithOuterJoin(tls, pTerm, pSrc) != 0) {
		return 0 /* See https://sqlite.org/forum/forumpost/51e6959f61 */
	}
	if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&notReady != uint64(0) {
		return 0
	}
	leftCol = (*(*struct {
		FleftColumn int32
		FiField     int32
	})(unsafe.Pointer(pTerm + 24))).FleftColumn
	if leftCol < 0 {
		return 0
	}
	aff = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab)).FaCol + uintptr(leftCol)*12))).Faffinity
	if !(_sqlite3IndexAffinityOk(tls, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, aff) != 0) {
		return 0
	}
	return _columnIsGoodIndexCandidate(tls, (*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab, leftCol)
}

// C documentation
//
//	/*
//	** timediff(DATE1, DATE2)
//	**
//	** Return the amount of time that must be added to DATE2 in order to
//	** convert it into DATE2.  The time difference format is:
//	**
//	**     +YYYY-MM-DD HH:MM:SS.SSS
//	**
//	** The initial "+" becomes "-" if DATE1 occurs before DATE2.  For
//	** date/time values A and B, the following invariant should hold:
//	**
//	**     datetime(A) == (datetime(B, timediff(A,B))
//	**
//	** Both DATE arguments must be either a julian day number, or an
//	** ISO-8601 string.  The unix timestamps are not supported by this
//	** routine.
//	*/
func _timediffFunc(tls *libc.TLS, context uintptr, NotUsed1 int32, argv uintptr) {
	bp := tls.Alloc(192)
	defer tls.Free(192)
	var M, Y int32
	var sign int8
	var v1 uintptr
	var _ /* d1 at bp+0 */ TDateTime
	var _ /* d2 at bp+48 */ TDateTime
	var _ /* sRes at bp+96 */ Tsqlite3_str
	_, _, _, _ = M, Y, sign, v1
	_ = NotUsed1
	if _isDate(tls, context, int32(1), argv, bp) != 0 {
		return
	}
	if _isDate(tls, context, int32(1), argv+1*4, bp+48) != 0 {
		return
	}
	_computeYMD_HMS(tls, bp)
	_computeYMD_HMS(tls, bp+48)
	if (**(**TDateTime)(__ccgo_up(bp))).FiJD >= (**(**TDateTime)(__ccgo_up(bp + 48))).FiJD {
		sign = int8('+')
		Y = (**(**TDateTime)(__ccgo_up(bp))).FY - (**(**TDateTime)(__ccgo_up(bp + 48))).FY
		if Y != 0 {
			(**(**TDateTime)(__ccgo_up(bp + 48))).FY = (**(**TDateTime)(__ccgo_up(bp))).FY
			(**(**TDateTime)(__ccgo_up(bp + 48))).FvalidJD = 0
			_computeJD(tls, bp+48)
		}
		M = (**(**TDateTime)(__ccgo_up(bp))).FM - (**(**TDateTime)(__ccgo_up(bp + 48))).FM
		if M < 0 {
			Y = Y - 1
			M = M + int32(12)
		}
		if M != 0 {
			(**(**TDateTime)(__ccgo_up(bp + 48))).FM = (**(**TDateTime)(__ccgo_up(bp))).FM
			(**(**TDateTime)(__ccgo_up(bp + 48))).FvalidJD = 0
			_computeJD(tls, bp+48)
		}
		for (**(**TDateTime)(__ccgo_up(bp))).FiJD < (**(**TDateTime)(__ccgo_up(bp + 48))).FiJD {
			M = M - 1
			if M < 0 {
				M = int32(11)
				Y = Y - 1
			}
			(**(**TDateTime)(__ccgo_up(bp + 48))).FM = (**(**TDateTime)(__ccgo_up(bp + 48))).FM - 1
			if (**(**TDateTime)(__ccgo_up(bp + 48))).FM < int32(1) {
				(**(**TDateTime)(__ccgo_up(bp + 48))).FM = int32(12)
				(**(**TDateTime)(__ccgo_up(bp + 48))).FY = (**(**TDateTime)(__ccgo_up(bp + 48))).FY - 1
			}
			(**(**TDateTime)(__ccgo_up(bp + 48))).FvalidJD = 0
			_computeJD(tls, bp+48)
		}
		(**(**TDateTime)(__ccgo_up(bp))).FiJD -= (**(**TDateTime)(__ccgo_up(bp + 48))).FiJD
		v1 = bp
		*(*Tsqlite3_int64)(unsafe.Pointer(v1)) = Tsqlite3_int64(uint64(*(*Tsqlite3_int64)(unsafe.Pointer(v1))) + libc.Uint64FromInt32(1486995408)*libc.Uint64FromInt32(100000))
	} else { /* d1<d2 */
		sign = int8('-')
		Y = (**(**TDateTime)(__ccgo_up(bp + 48))).FY - (**(**TDateTime)(__ccgo_up(bp))).FY
		if Y != 0 {
			(**(**TDateTime)(__ccgo_up(bp + 48))).FY = (**(**TDateTime)(__ccgo_up(bp))).FY
			(**(**TDateTime)(__ccgo_up(bp + 48))).FvalidJD = 0
			_computeJD(tls, bp+48)
		}
		M = (**(**TDateTime)(__ccgo_up(bp + 48))).FM - (**(**TDateTime)(__ccgo_up(bp))).FM
		if M < 0 {
			Y = Y - 1
			M = M + int32(12)
		}
		if M != 0 {
			(**(**TDateTime)(__ccgo_up(bp + 48))).FM = (**(**TDateTime)(__ccgo_up(bp))).FM
			(**(**TDateTime)(__ccgo_up(bp + 48))).FvalidJD = 0
			_computeJD(tls, bp+48)
		}
		for (**(**TDateTime)(__ccgo_up(bp))).FiJD > (**(**TDateTime)(__ccgo_up(bp + 48))).FiJD {
			M = M - 1
			if M < 0 {
				M = int32(11)
				Y = Y - 1
			}
			(**(**TDateTime)(__ccgo_up(bp + 48))).FM = (**(**TDateTime)(__ccgo_up(bp + 48))).FM + 1
			if (**(**TDateTime)(__ccgo_up(bp + 48))).FM > int32(12) {
				(**(**TDateTime)(__ccgo_up(bp + 48))).FM = int32(1)
				(**(**TDateTime)(__ccgo_up(bp + 48))).FY = (**(**TDateTime)(__ccgo_up(bp + 48))).FY + 1
			}
			(**(**TDateTime)(__ccgo_up(bp + 48))).FvalidJD = 0
			_computeJD(tls, bp+48)
		}
		(**(**TDateTime)(__ccgo_up(bp))).FiJD = (**(**TDateTime)(__ccgo_up(bp + 48))).FiJD - (**(**TDateTime)(__ccgo_up(bp))).FiJD
		v1 = bp
		*(*Tsqlite3_int64)(unsafe.Pointer(v1)) = Tsqlite3_int64(uint64(*(*Tsqlite3_int64)(unsafe.Pointer(v1))) + libc.Uint64FromInt32(1486995408)*libc.Uint64FromInt32(100000))
	}
	_clearYMD_HMS_TZ(tls, bp)
	_computeYMD_HMS(tls, bp)
	_sqlite3StrAccumInit(tls, bp+96, uintptr(0), uintptr(0), 0, int32(100))
	Xsqlite3_str_appendf(tls, bp+96, __ccgo_ts+1492, libc.VaList(bp+128, int32(sign), Y, M, (**(**TDateTime)(__ccgo_up(bp))).FD-int32(1), (**(**TDateTime)(__ccgo_up(bp))).Fh, (**(**TDateTime)(__ccgo_up(bp))).Fm, (**(**TDateTime)(__ccgo_up(bp))).Fs))
	_sqlite3ResultStrAccum(tls, context, bp+96)
}

// C documentation
//
//	/*
//	** Assuming the input DateTime is UTC, move it to its localtime equivalent.
//	*/
func _toLocaltime(tls *libc.TLS, p uintptr, pCtx uintptr) (r int32) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var iYearDiff int32
	var _ /* sLocal at bp+4 */ Ttm
	var _ /* t at bp+0 */ Ttime_t
	var _ /* x at bp+40 */ TDateTime
	_ = iYearDiff
	/* Initialize the contents of sLocal to avoid a compiler warning. */
	libc.Xmemset(tls, bp+4, 0, uint32(36))
	_computeJD(tls, p)
	if (*TDateTime)(unsafe.Pointer(p)).FiJD < libc.Int64FromInt32(2108667600)*libc.Int64FromInt32(100000) || (*TDateTime)(unsafe.Pointer(p)).FiJD > libc.Int64FromInt32(2130141456)*libc.Int64FromInt32(100000) {
		/* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
		 ** works for years between 1970 and 2037. For dates outside this range,
		 ** SQLite attempts to map the year into an equivalent year within this
		 ** range, do the calculation, then map the year back.
		 */
		**(**TDateTime)(__ccgo_up(bp + 40)) = **(**TDateTime)(__ccgo_up(p))
		_computeYMD_HMS(tls, bp+40)
		iYearDiff = int32(2000) + (**(**TDateTime)(__ccgo_up(bp + 40))).FY%int32(4) - (**(**TDateTime)(__ccgo_up(bp + 40))).FY
		(**(**TDateTime)(__ccgo_up(bp + 40))).FY += iYearDiff
		(**(**TDateTime)(__ccgo_up(bp + 40))).FvalidJD = 0
		_computeJD(tls, bp+40)
		**(**Ttime_t)(__ccgo_up(bp)) = int32((**(**TDateTime)(__ccgo_up(bp + 40))).FiJD/libc.Int64FromInt32(1000) - libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000))
	} else {
		iYearDiff = 0
		**(**Ttime_t)(__ccgo_up(bp)) = int32((*TDateTime)(unsafe.Pointer(p)).FiJD/libc.Int64FromInt32(1000) - libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000))
	}
	if _osLocaltime(tls, bp, bp+4) != 0 {
		Xsqlite3_result_error(tls, pCtx, __ccgo_ts+1256, -int32(1))
		return int32(SQLITE_ERROR)
	}
	(*TDateTime)(unsafe.Pointer(p)).FY = (**(**Ttm)(__ccgo_up(bp + 4))).Ftm_year + int32(1900) - iYearDiff
	(*TDateTime)(unsafe.Pointer(p)).FM = (**(**Ttm)(__ccgo_up(bp + 4))).Ftm_mon + int32(1)
	(*TDateTime)(unsafe.Pointer(p)).FD = (**(**Ttm)(__ccgo_up(bp + 4))).Ftm_mday
	(*TDateTime)(unsafe.Pointer(p)).Fh = (**(**Ttm)(__ccgo_up(bp + 4))).Ftm_hour
	(*TDateTime)(unsafe.Pointer(p)).Fm = (**(**Ttm)(__ccgo_up(bp + 4))).Ftm_min
	(*TDateTime)(unsafe.Pointer(p)).Fs = float64((**(**Ttm)(__ccgo_up(bp + 4))).Ftm_sec) + float64(float64((*TDateTime)(unsafe.Pointer(p)).FiJD%libc.Int64FromInt32(1000))*float64(0.001))
	(*TDateTime)(unsafe.Pointer(p)).FvalidYMD = int8(1)
	(*TDateTime)(unsafe.Pointer(p)).FvalidHMS = int8(1)
	(*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0
	libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1)
	(*TDateTime)(unsafe.Pointer(p)).Ftz = 0
	libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 1, 0x2)
	return SQLITE_OK
}

// C documentation
//
//	/* Construct a new Expr object from a single token */
func _tokenExpr(tls *libc.TLS, pParse uintptr, op int32, _t TToken) (r uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*TToken)(unsafe.Pointer(bp)) = _t
	var p, v1 uintptr
	_, _ = p, v1
	p = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(uint32(52)+(**(**TToken)(__ccgo_up(bp))).Fn+uint32(1)))
	if p != 0 {
		/* memset(p, 0, sizeof(Expr)); */
		(*TExpr)(unsafe.Pointer(p)).Fop = uint8(op)
		(*TExpr)(unsafe.Pointer(p)).FaffExpr = 0
		(*TExpr)(unsafe.Pointer(p)).Fflags = uint32(EP_Leaf)
		/* p->iAgg = -1; // Not required */
		v1 = libc.UintptrFromInt32(0)
		(*TExpr)(unsafe.Pointer(p)).FpRight = v1
		(*TExpr)(unsafe.Pointer(p)).FpLeft = v1
		(*TExpr)(unsafe.Pointer(p)).FpAggInfo = uintptr(0)
		libc.Xmemset(tls, p+20, 0, uint32(4))
		libc.Xmemset(tls, p+44, 0, uint32(8))
		(*TExpr)(unsafe.Pointer(p)).Fop2 = uint8(0)
		(*TExpr)(unsafe.Pointer(p)).FiTable = 0
		(*TExpr)(unsafe.Pointer(p)).FiColumn = 0
		*(*uintptr)(unsafe.Pointer(p + 8)) = p + 1*52
		libc.Xmemcpy(tls, *(*uintptr)(unsafe.Pointer(p + 8)), (**(**TToken)(__ccgo_up(bp))).Fz, (**(**TToken)(__ccgo_up(bp))).Fn)
		**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(p + 8)) + uintptr((**(**TToken)(__ccgo_up(bp))).Fn))) = 0
		*(*int32)(unsafe.Pointer(p + 36)) = int32((**(**TToken)(__ccgo_up(bp))).Fz) - int32((*TParse)(unsafe.Pointer(pParse)).FzTail)
		if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(p + 8)))))])&int32(0x80) != 0 {
			_sqlite3DequoteExpr(tls, p)
		}
		(*TExpr)(unsafe.Pointer(p)).FnHeight = int32(1)
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
			return _sqlite3RenameTokenMap(tls, pParse, p, bp)
		}
	}
	return p
}

// C documentation
//
//	/*
//	** If the pBase expression originated in the ON or USING clause of
//	** a join, then transfer the appropriate markings over to derived.
//	*/
func _transferJoinMarkings(tls *libc.TLS, pDerived uintptr, pBase uintptr) {
	if pDerived != 0 && (*TExpr)(unsafe.Pointer(pBase)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON)) != uint32(0) {
		**(**Tu32)(__ccgo_up(pDerived + 4)) |= (*TExpr)(unsafe.Pointer(pBase)).Fflags & uint32(libc.Int32FromInt32(EP_OuterON)|libc.Int32FromInt32(EP_InnerON))
		*(*int32)(unsafe.Pointer(pDerived + 36)) = *(*int32)(unsafe.Pointer(pBase + 36))
	}
}

// C documentation
//
//	/*
//	** Convert OP_Column opcodes to OP_Copy in previously generated code.
//	**
//	** This routine runs over generated VDBE code and translates OP_Column
//	** opcodes into OP_Copy when the table is being accessed via co-routine
//	** instead of via table lookup.
//	**
//	** If the iAutoidxCur is not zero, then any OP_Rowid instructions on
//	** cursor iTabCur are transformed into OP_Sequence opcode for the
//	** iAutoidxCur cursor, in order to generate unique rowids for the
//	** automatic index being generated.
//	*/
func _translateColumnToCopy(tls *libc.TLS, pParse uintptr, iStart int32, iTabCur int32, iRegister int32, iAutoidxCur int32) {
	var iEnd int32
	var pOp, v uintptr
	_, _, _ = iEnd, pOp, v
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	pOp = _sqlite3VdbeGetOp(tls, v, iStart)
	iEnd = _sqlite3VdbeCurrentAddr(tls, v)
	if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0 {
		return
	}
	for {
		if !(iStart < iEnd) {
			break
		}
		if (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 != iTabCur {
			goto _1
		}
		if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) {
			(*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_Copy)
			(*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = (*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 + iRegister
			(*TVdbeOp)(unsafe.Pointer(pOp)).Fp2 = (*TVdbeOp)(unsafe.Pointer(pOp)).Fp3
			(*TVdbeOp)(unsafe.Pointer(pOp)).Fp3 = 0
			(*TVdbeOp)(unsafe.Pointer(pOp)).Fp5 = uint16(2) /* Cause the MEM_Subtype flag to be cleared */
		} else {
			if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Rowid) {
				(*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_Sequence)
				(*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = iAutoidxCur
			}
		}
		goto _1
	_1:
		;
		iStart = iStart + 1
		pOp += 20
	}
}

/*
** Two routines for printing the content of an sqlite3_index_info
** structure.  Used for testing and debugging only.  If neither
** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
** are no-ops.
 */

// C documentation
//
//	/*
//	** Allocate space to hold a new trigger step.  The allocated space
//	** holds both the TriggerStep object and the TriggerStep.target.z string.
//	**
//	** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
//	*/
func _triggerStepAllocate(tls *libc.TLS, pParse uintptr, op Tu8, pTabList uintptr, zStart uintptr, zEnd uintptr) (r uintptr) {
	var db, pNew, pTriggerStep uintptr
	_, _, _ = db, pNew, pTriggerStep
	pNew = (*TParse)(unsafe.Pointer(pParse)).FpNewTrigger
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pTriggerStep = uintptr(0)
	if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
		if pNew != 0 && (*TTrigger)(unsafe.Pointer(pNew)).FpSchema != (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*16))).FpSchema && *(*uintptr)(unsafe.Pointer(pTabList + 8 + 44)) != 0 {
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23517, 0)
		} else {
			pTriggerStep = _sqlite3DbMallocZero(tls, db, uint64(44))
			if pTriggerStep != 0 {
				(*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpSrc = _sqlite3SrcListDup(tls, db, pTabList, int32(EXPRDUP_REDUCE))
				(*TTriggerStep)(unsafe.Pointer(pTriggerStep)).Fop = op
				(*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FzSpan = _triggerSpanDup(tls, db, zStart, zEnd)
				if (*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpSrc != 0 && int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
					_sqlite3RenameTokenRemap(tls, pParse, (*(*TSrcItem)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(pTriggerStep)).FpSrc + 8))).FzName, (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FzName)
				}
			}
		}
	}
	_sqlite3SrcListDelete(tls, db, pTabList)
	return pTriggerStep
}

// C documentation
//
//	/*
//	** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
//	** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
//	*/
func _trimFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	var aLen, azChar, z, zCharSet, zIn, v1 uintptr
	var flags, i, nChar int32
	var len1, len11, nIn uint32
	_, _, _, _, _, _, _, _, _, _, _, _ = aLen, azChar, flags, i, len1, len11, nChar, nIn, z, zCharSet, zIn, v1 /* Loop counter */
	aLen = uintptr(0)                                                                                          /* Length of each character in zCharSet */
	azChar = uintptr(0)                                                                                        /* Number of characters in zCharSet */
	if Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv))) == int32(SQLITE_NULL) {
		return
	}
	zIn = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	if zIn == uintptr(0) {
		return
	}
	nIn = uint32(Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv))))
	if argc == int32(1) {
		nChar = int32(1)
		aLen = uintptr(unsafe.Pointer(&_lenOne))
		azChar = uintptr(unsafe.Pointer(&_azOne))
		zCharSet = uintptr(0)
	} else {
		v1 = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
		zCharSet = v1
		if v1 == uintptr(0) {
			return
		} else {
			z = zCharSet
			nChar = libc.Int32FromInt32(0)
			for {
				if !(**(**uint8)(__ccgo_up(z)) != 0) {
					break
				}
				v1 = z
				z = z + 1
				if int32(**(**uint8)(__ccgo_up(v1))) >= int32(0xc0) {
					for int32(**(**uint8)(__ccgo_up(z)))&int32(0xc0) == int32(0x80) {
						z = z + 1
					}
				}
				goto _2
			_2:
				;
				nChar = nChar + 1
			}
			if nChar > 0 {
				azChar = _contextMalloc(tls, context, int64(nChar)*int64(libc.Uint32FromInt64(4)+libc.Uint32FromInt64(4)))
				if azChar == uintptr(0) {
					return
				}
				aLen = azChar + uintptr(nChar)*4
				z = zCharSet
				nChar = libc.Int32FromInt32(0)
				for {
					if !(**(**uint8)(__ccgo_up(z)) != 0) {
						break
					}
					**(**uintptr)(__ccgo_up(azChar + uintptr(nChar)*4)) = z
					v1 = z
					z = z + 1
					if int32(**(**uint8)(__ccgo_up(v1))) >= int32(0xc0) {
						for int32(**(**uint8)(__ccgo_up(z)))&int32(0xc0) == int32(0x80) {
							z = z + 1
						}
					}
					**(**uint32)(__ccgo_up(aLen + uintptr(nChar)*4)) = uint32(int32(z) - int32(**(**uintptr)(__ccgo_up(azChar + uintptr(nChar)*4))))
					goto _4
				_4:
					;
					nChar = nChar + 1
				}
			}
		}
	}
	if nChar > 0 {
		flags = int32(Xsqlite3_user_data(tls, context))
		if flags&int32(1) != 0 {
			for nIn > uint32(0) {
				len1 = uint32(0)
				i = 0
				for {
					if !(i < nChar) {
						break
					}
					len1 = **(**uint32)(__ccgo_up(aLen + uintptr(i)*4))
					if len1 <= nIn && libc.Xmemcmp(tls, zIn, **(**uintptr)(__ccgo_up(azChar + uintptr(i)*4)), len1) == 0 {
						break
					}
					goto _6
				_6:
					;
					i = i + 1
				}
				if i >= nChar {
					break
				}
				zIn = zIn + uintptr(len1)
				nIn = nIn - len1
			}
		}
		if flags&int32(2) != 0 {
			for nIn > uint32(0) {
				len11 = uint32(0)
				i = 0
				for {
					if !(i < nChar) {
						break
					}
					len11 = **(**uint32)(__ccgo_up(aLen + uintptr(i)*4))
					if len11 <= nIn && libc.Xmemcmp(tls, zIn+uintptr(nIn-len11), **(**uintptr)(__ccgo_up(azChar + uintptr(i)*4)), len11) == 0 {
						break
					}
					goto _7
				_7:
					;
					i = i + 1
				}
				if i >= nChar {
					break
				}
				nIn = nIn - len11
			}
		}
		if zCharSet != 0 {
			Xsqlite3_free(tls, azChar)
		}
	}
	Xsqlite3_result_text(tls, context, zIn, int32(nIn), uintptr(-libc.Int32FromInt32(1)))
}

// C documentation
//
//	/*
//	** The unhex() function. This function may be invoked with either one or
//	** two arguments. In both cases the first argument is interpreted as text
//	** a text value containing a set of pairs of hexadecimal digits which are
//	** decoded and returned as a blob.
//	**
//	** If there is only a single argument, then it must consist only of an
//	** even number of hexadecimal digits. Otherwise, return NULL.
//	**
//	** Or, if there is a second argument, then any character that appears in
//	** the second argument is also allowed to appear between pairs of hexadecimal
//	** digits in the first argument. If any other character appears in the
//	** first argument, or if one of the allowed characters appears between
//	** two hexadecimal digits that make up a single byte, NULL is returned.
//	**
//	** The following expressions are all true:
//	**
//	**     unhex('ABCD')       IS x'ABCD'
//	**     unhex('AB CD')      IS NULL
//	**     unhex('AB CD', ' ') IS x'ABCD'
//	**     unhex('A BCD', ' ') IS NULL
//	*/
func _unhexFunc(tls *libc.TLS, pCtx uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var c, d, v2 Tu8
	var ch Tu32
	var nHex, nPass int32
	var p, pBlob, zPass, v1 uintptr
	var v3 uint32
	var _ /* zHex at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _ = c, ch, d, nHex, nPass, p, pBlob, zPass, v1, v2, v3
	zPass = __ccgo_ts + 1711
	nPass = 0
	**(**uintptr)(__ccgo_up(bp)) = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	nHex = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
	pBlob = uintptr(0)
	p = uintptr(0)
	if argc == int32(2) {
		zPass = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
		nPass = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 1*4)))
	}
	if !(**(**uintptr)(__ccgo_up(bp)) != 0) || !(zPass != 0) {
		return
	}
	v1 = _contextMalloc(tls, pCtx, int64(nHex/int32(2)+int32(1)))
	pBlob = v1
	p = v1
	if pBlob != 0 { /* Least significant digit of next byte */
		for {
			v2 = **(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))
			c = v2
			if !(int32(v2) != 0x00) {
				break
			}
			for !(int32(_sqlite3CtypeMap[c])&libc.Int32FromInt32(0x08) != 0) {
				if int32(**(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))) < int32(0x80) {
					v1 = **(**uintptr)(__ccgo_up(bp))
					**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
					v3 = uint32(**(**Tu8)(__ccgo_up(v1)))
				} else {
					v3 = _sqlite3Utf8Read(tls, bp)
				}
				ch = v3
				if !(_strContainsChar(tls, zPass, nPass, ch) != 0) {
					goto unhex_null
				}
				c = **(**Tu8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp))))
				if int32(c) == 0x00 {
					goto unhex_done
				}
			}
			**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
			v1 = **(**uintptr)(__ccgo_up(bp))
			**(**uintptr)(__ccgo_up(bp)) = **(**uintptr)(__ccgo_up(bp)) + 1
			d = **(**Tu8)(__ccgo_up(v1))
			if !(int32(_sqlite3CtypeMap[d])&libc.Int32FromInt32(0x08) != 0) {
				goto unhex_null
			}
			v1 = p
			p = p + 1
			**(**Tu8)(__ccgo_up(v1)) = uint8(int32(_sqlite3HexToInt(tls, int32(c)))<<int32(4) | int32(_sqlite3HexToInt(tls, int32(d))))
		}
	}
	goto unhex_done
unhex_done:
	;
	Xsqlite3_result_blob(tls, pCtx, pBlob, int32(p)-int32(pBlob), __ccgo_fp(Xsqlite3_free))
	return
	goto unhex_null
unhex_null:
	;
	Xsqlite3_free(tls, pBlob)
	return
}

// C documentation
//
//	/*
//	** Implementation of the UNISTR() function.
//	**
//	** This is intended to be a work-alike of the UNISTR() function in
//	** PostgreSQL.  Quoting from the PG documentation (PostgreSQL 17 -
//	** scraped on 2025-02-22):
//	**
//	**    Evaluate escaped Unicode characters in the argument. Unicode
//	**    characters can be specified as \XXXX (4 hexadecimal digits),
//	**    \+XXXXXX (6 hexadecimal digits), \uXXXX (4 hexadecimal digits),
//	**    or \UXXXXXXXX (8 hexadecimal digits). To specify a backslash,
//	**    write two backslashes. All other characters are taken literally.
//	*/
func _unistrFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, j, n, nIn, v1 int32
	var z, zIn, zOut uintptr
	var _ /* v at bp+0 */ Tu32
	_, _, _, _, _, _, _, _ = i, j, n, nIn, z, zIn, zOut, v1
	_ = argc
	zIn = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
	if zIn == uintptr(0) {
		return
	}
	nIn = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv)))
	zOut = Xsqlite3_malloc64(tls, uint64(nIn+int32(1)))
	if zOut == uintptr(0) {
		Xsqlite3_result_error_nomem(tls, context)
		return
	}
	v1 = libc.Int32FromInt32(0)
	j = v1
	i = v1
	for i < nIn {
		z = libc.Xstrchr(tls, zIn+uintptr(i), int32('\\'))
		if z == uintptr(0) {
			n = nIn - i
			libc.Xmemmove(tls, zOut+uintptr(j), zIn+uintptr(i), uint32(n))
			j = j + n
			break
		}
		n = int32(z) - t__predefined_ptrdiff_t(zIn+uintptr(i))
		if n > 0 {
			libc.Xmemmove(tls, zOut+uintptr(j), zIn+uintptr(i), uint32(n))
			j = j + n
			i = i + n
		}
		if int32(**(**int8)(__ccgo_up(zIn + uintptr(i+int32(1))))) == int32('\\') {
			i = i + int32(2)
			v1 = j
			j = j + 1
			**(**int8)(__ccgo_up(zOut + uintptr(v1))) = int8('\\')
		} else {
			if int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(zIn + uintptr(i+int32(1)))))])&int32(0x08) != 0 {
				if !(_isNHex(tls, zIn+uintptr(i+int32(1)), int32(4), bp) != 0) {
					goto unistr_error
				}
				i = i + int32(5)
				j = j + _sqlite3AppendOneUtf8Character(tls, zOut+uintptr(j), **(**Tu32)(__ccgo_up(bp)))
			} else {
				if int32(**(**int8)(__ccgo_up(zIn + uintptr(i+int32(1))))) == int32('+') {
					if !(_isNHex(tls, zIn+uintptr(i+int32(2)), int32(6), bp) != 0) {
						goto unistr_error
					}
					i = i + int32(8)
					j = j + _sqlite3AppendOneUtf8Character(tls, zOut+uintptr(j), **(**Tu32)(__ccgo_up(bp)))
				} else {
					if int32(**(**int8)(__ccgo_up(zIn + uintptr(i+int32(1))))) == int32('u') {
						if !(_isNHex(tls, zIn+uintptr(i+int32(2)), int32(4), bp) != 0) {
							goto unistr_error
						}
						i = i + int32(6)
						j = j + _sqlite3AppendOneUtf8Character(tls, zOut+uintptr(j), **(**Tu32)(__ccgo_up(bp)))
					} else {
						if int32(**(**int8)(__ccgo_up(zIn + uintptr(i+int32(1))))) == int32('U') {
							if !(_isNHex(tls, zIn+uintptr(i+int32(2)), int32(8), bp) != 0) {
								goto unistr_error
							}
							i = i + int32(10)
							j = j + _sqlite3AppendOneUtf8Character(tls, zOut+uintptr(j), **(**Tu32)(__ccgo_up(bp)))
						} else {
							goto unistr_error
						}
					}
				}
			}
		}
	}
	**(**int8)(__ccgo_up(zOut + uintptr(j))) = 0
	Xsqlite3_result_text64(tls, context, zOut, uint64(j), __ccgo_fp(Xsqlite3_free), uint8(SQLITE_UTF8_ZT))
	return
	goto unistr_error
unistr_error:
	;
	Xsqlite3_free(tls, zOut)
	Xsqlite3_result_error(tls, context, __ccgo_ts+17793, -int32(1))
	return
}

// C documentation
//
//	/* Undo the work of sqlite3SetJoinExpr().  This is used when a LEFT JOIN
//	** is simplified into an ordinary JOIN, and when an ON expression is
//	** "pushed down" into the WHERE clause of a subquery.
//	**
//	** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into
//	** an ordinary term that omits the EP_OuterON mark.  Or if iTable<0, then
//	** just clear every EP_OuterON and EP_InnerON mark from the expression tree.
//	**
//	** If nullable is true, that means that Expr p might evaluate to NULL even
//	** if it is a reference to a NOT NULL column.  This can happen, for example,
//	** if the table that p references is on the left side of a RIGHT JOIN.
//	** If nullable is true, then take care to not remove the EP_CanBeNull bit.
//	** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c
//	*/
func _unsetJoinExpr(tls *libc.TLS, p uintptr, iTable int32, nullable int32) {
	var i int32
	_ = i
	for p != 0 {
		if iTable < 0 || (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) && *(*int32)(unsafe.Pointer(p + 36)) == iTable {
			**(**Tu32)(__ccgo_up(p + 4)) &= ^uint32(libc.Int32FromInt32(EP_OuterON) | libc.Int32FromInt32(EP_InnerON))
			if iTable >= 0 {
				**(**Tu32)(__ccgo_up(p + 4)) |= uint32(libc.Int32FromInt32(EP_InnerON))
			}
		}
		if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(p)).FiTable == iTable && !(nullable != 0) {
			**(**Tu32)(__ccgo_up(p + 4)) &= ^uint32(libc.Int32FromInt32(EP_CanBeNull))
		}
		if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_FUNCTION) {
			if *(*uintptr)(unsafe.Pointer(p + 20)) != 0 {
				i = 0
				for {
					if !(i < (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)))).FnExpr) {
						break
					}
					_unsetJoinExpr(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 20)) + 8 + uintptr(i)*20))).FpExpr, iTable, nullable)
					goto _1
				_1:
					;
					i = i + 1
				}
			}
		}
		_unsetJoinExpr(tls, (*TExpr)(unsafe.Pointer(p)).FpLeft, iTable, nullable)
		p = (*TExpr)(unsafe.Pointer(p)).FpRight
	}
}

// C documentation
//
//	/*
//	** Generate code that will update the accumulator memory cells for an
//	** aggregate based on the current cursor position.
//	**
//	** If regAcc is non-zero and there are no min() or max() aggregates
//	** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
//	** registers if register regAcc contains 0. The caller will take care
//	** of setting and clearing regAcc.
//	**
//	** For an ORDER BY aggregate, the actual accumulator memory cell update
//	** is deferred until after all input rows have been received, so that they
//	** can be run in the requested order.  In that case, instead of invoking
//	** OP_AggStep to update the accumulator, just add the arguments that would
//	** have been passed into OP_AggStep into the sorting ephemeral table
//	** (along with the appropriate sort key).
//	*/
func _updateAccumulator(tls *libc.TLS, pParse uintptr, regAcc int32, pAggInfo uintptr, eDistinctType int32) {
	var addrHitTest, addrNext, i, j, jj, kk, nArg, regAgg, regAggSz, regBase, regDistinct, regHit, v2 int32
	var pC, pColl, pF, pFilter, pItem, pList, pOBList, v, v3 uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrHitTest, addrNext, i, j, jj, kk, nArg, pC, pColl, pF, pFilter, pItem, pList, pOBList, regAgg, regAggSz, regBase, regDistinct, regHit, v, v2, v3
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	regHit = 0
	addrHitTest = 0
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return
	}
	(*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(1)
	i = 0
	pF = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc
	for {
		if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc) {
			break
		}
		addrNext = 0
		regAggSz = 0
		regDistinct = 0
		pList = *(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr + 20))
		if (*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) {
			pFilter = (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).Fy)))).FpFilter
			if (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator != 0 && (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 && regAcc != 0 {
				/* If regAcc==0, there there exists some min() or max() function
				 ** without a FILTER clause that will ensure the magnet registers
				 ** are populated. */
				if regHit == 0 {
					v3 = pParse + 48
					*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
					v2 = *(*int32)(unsafe.Pointer(v3))
					regHit = v2
				}
				/* If this is the first row of the group (regAcc contains 0), clear the
				 ** "magnet" register regHit so that the accumulator registers
				 ** are populated if the FILTER clause jumps over the the
				 ** invocation of min() or max() altogether. Or, if this is not
				 ** the first row (regAcc contains 1), set the magnet register so that
				 ** the accumulators are not populated unless the min()/max() is invoked
				 ** and indicates that they should be.  */
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Copy), regAcc, regHit)
			}
			addrNext = _sqlite3VdbeMakeLabel(tls, pParse)
			_sqlite3ExprIfFalse(tls, pParse, pFilter, addrNext, int32(SQLITE_JUMPIFNULL))
		}
		if (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab >= 0 { /* The ORDER BY clause */
			nArg = (*TExprList)(unsafe.Pointer(pList)).FnExpr
			pOBList = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).FpLeft + 20))
			regAggSz = (*TExprList)(unsafe.Pointer(pOBList)).FnExpr
			if !((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBUnique != 0) {
				regAggSz = regAggSz + 1 /* One register for OP_Sequence */
			}
			if (*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload != 0 {
				regAggSz = regAggSz + nArg
			}
			if (*TAggInfo_func)(unsafe.Pointer(pF)).FbUseSubtype != 0 {
				regAggSz = regAggSz + nArg
			}
			regAggSz = regAggSz + 1 /* One extra register to hold result of MakeRecord */
			regAgg = _sqlite3GetTempRange(tls, pParse, regAggSz)
			regDistinct = regAgg
			_sqlite3ExprCodeExprList(tls, pParse, pOBList, regAgg, 0, uint8(SQLITE_ECEL_DUP))
			jj = (*TExprList)(unsafe.Pointer(pOBList)).FnExpr
			if !((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBUnique != 0) {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Sequence), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, regAgg+jj)
				jj = jj + 1
			}
			if (*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload != 0 {
				regDistinct = regAgg + jj
				_sqlite3ExprCodeExprList(tls, pParse, pList, regDistinct, 0, uint8(SQLITE_ECEL_DUP))
				jj = jj + nArg
			}
			if (*TAggInfo_func)(unsafe.Pointer(pF)).FbUseSubtype != 0 {
				if (*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload != 0 {
					v2 = regDistinct
				} else {
					v2 = regAgg
				}
				regBase = v2
				kk = 0
				for {
					if !(kk < nArg) {
						break
					}
					_sqlite3VdbeAddOp2(tls, v, int32(OP_GetSubtype), regBase+kk, regAgg+jj)
					goto _5
				_5:
					;
					kk = kk + 1
					jj = jj + 1
				}
			}
		} else {
			if pList != 0 {
				nArg = (*TExprList)(unsafe.Pointer(pList)).FnExpr
				regAgg = _sqlite3GetTempRange(tls, pParse, nArg)
				regDistinct = regAgg
				_sqlite3ExprCodeExprList(tls, pParse, pList, regAgg, 0, uint8(SQLITE_ECEL_DUP))
			} else {
				nArg = 0
				regAgg = 0
			}
		}
		if (*TAggInfo_func)(unsafe.Pointer(pF)).FiDistinct >= 0 && pList != 0 {
			if addrNext == 0 {
				addrNext = _sqlite3VdbeMakeLabel(tls, pParse)
			}
			(*TAggInfo_func)(unsafe.Pointer(pF)).FiDistinct = _codeDistinct(tls, pParse, eDistinctType, (*TAggInfo_func)(unsafe.Pointer(pF)).FiDistinct, addrNext, pList, regDistinct)
		}
		if (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab >= 0 {
			/* Insert a new record into the ORDER BY table */
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regAgg, regAggSz-int32(1), regAgg+regAggSz-int32(1))
			_sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab, regAgg+regAggSz-int32(1), regAgg, regAggSz-int32(1))
			_sqlite3ReleaseTempRange(tls, pParse, regAgg, regAggSz)
		} else {
			/* Invoke the AggStep function */
			if (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 {
				pColl = uintptr(0)
				/* pList!=0 if pF->pFunc has NEEDCOLL */
				j = 0
				pItem = pList + 8
				for {
					if !(!(pColl != 0) && j < nArg) {
						break
					}
					pColl = _sqlite3ExprCollSeq(tls, pParse, (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr)
					goto _6
				_6:
					;
					j = j + 1
					pItem += 20
				}
				if !(pColl != 0) {
					pColl = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FpDfltColl
				}
				if regHit == 0 && (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator != 0 {
					v3 = pParse + 48
					*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
					v2 = *(*int32)(unsafe.Pointer(v3))
					regHit = v2
				}
				_sqlite3VdbeAddOp4(tls, v, int32(OP_CollSeq), regHit, 0, 0, pColl, -int32(2))
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_AggStep), 0, regAgg, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+(*TAggInfo)(unsafe.Pointer(pAggInfo)).FnColumn+i)
			_sqlite3VdbeAppendP4(tls, v, (*TAggInfo_func)(unsafe.Pointer(pF)).FpFunc, -int32(8))
			_sqlite3VdbeChangeP5(tls, v, uint16(nArg))
			_sqlite3ReleaseTempRange(tls, pParse, regAgg, nArg)
		}
		if addrNext != 0 {
			_sqlite3VdbeResolveLabel(tls, v, addrNext)
		}
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			return
		}
		goto _1
	_1:
		;
		i = i + 1
		pF += 24
	}
	if regHit == 0 && (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator != 0 {
		regHit = regAcc
	}
	if regHit != 0 {
		addrHitTest = _sqlite3VdbeAddOp1(tls, v, int32(OP_If), regHit)
	}
	i = 0
	pC = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol
	for {
		if !(i < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator) {
			break
		}
		_sqlite3ExprCode(tls, pParse, (*TAggInfo_col)(unsafe.Pointer(pC)).FpCExpr, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+i)
		if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
			return
		}
		goto _9
	_9:
		;
		i = i + 1
		pC += 20
	}
	(*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode = uint8(0)
	if addrHitTest != 0 {
		_sqlite3VdbeJumpHereOrPopInst(tls, v, addrHitTest)
	}
}

// C documentation
//
//	/*
//	** Assuming both the pLimit and pOrderBy parameters are NULL, this function
//	** generates VM code to run the query:
//	**
//	**   SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
//	**
//	** and write the results to the ephemeral table already opened as cursor
//	** iEph. None of pChanges, pTabList or pWhere are modified or consumed by
//	** this function, they must be deleted by the caller.
//	**
//	** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view:
//	**
//	**   SELECT <other-columns>, pChanges FROM pTabList
//	**   WHERE pWhere
//	**   GROUP BY <other-columns>
//	**   ORDER BY pOrderBy LIMIT pLimit
//	**
//	** If pTab is a view, the GROUP BY clause is omitted.
//	**
//	** Exactly how results are written to table iEph, and exactly what
//	** the <other-columns> in the query above are is determined by the type
//	** of table pTabList->a[0].pTab.
//	**
//	** If the table is a WITHOUT ROWID table, then argument pPk must be its
//	** PRIMARY KEY. In this case <other-columns> are the primary key columns
//	** of the table, in order. The results of the query are written to ephemeral
//	** table iEph as index keys, using OP_IdxInsert.
//	**
//	** If the table is actually a view, then <other-columns> are all columns of
//	** the view. The results are written to the ephemeral table iEph as records
//	** with automatically assigned integer keys.
//	**
//	** If the table is a virtual or ordinary intkey table, then <other-columns>
//	** is its rowid. For a virtual table, the results are written to iEph as
//	** records with automatically assigned integer keys For intkey tables, the
//	** rowid value in <other-columns> is used as the integer key, and the
//	** remaining fields make up the table record.
//	*/
func _updateFromSelect(tls *libc.TLS, pParse uintptr, iEph int32, pPk uintptr, pChanges uintptr, pTabList uintptr, pWhere uintptr, pOrderBy uintptr, pLimit uintptr) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var db, pGrp, pLimit2, pList, pNew, pOrderBy2, pSelect, pSrc, pTab, pWhere2 uintptr
	var eDest, i, v2 int32
	var _ /* dest at bp+0 */ TSelectDest
	_, _, _, _, _, _, _, _, _, _, _, _, _ = db, eDest, i, pGrp, pLimit2, pList, pNew, pOrderBy2, pSelect, pSrc, pTab, pWhere2, v2
	pSelect = uintptr(0)
	pList = uintptr(0)
	pGrp = uintptr(0)
	pLimit2 = uintptr(0)
	pOrderBy2 = uintptr(0)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8))).FpSTab
	_ = pOrderBy
	_ = pLimit
	pSrc = _sqlite3SrcListDup(tls, db, pTabList, 0)
	pWhere2 = _sqlite3ExprDup(tls, db, pWhere, 0)
	if pSrc != 0 {
		(*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FiCursor = -int32(1)
		(*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpSTab)).FnTabRef = (*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpSTab)).FnTabRef - 1
		(*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FpSTab = uintptr(0)
	}
	if pPk != 0 {
		i = 0
		for {
			if !(i < int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
				break
			}
			pNew = _exprRowColumn(tls, pParse, int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))))
			pList = _sqlite3ExprListAppend(tls, pParse, pList, pNew)
			goto _1
		_1:
			;
			i = i + 1
		}
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
			v2 = int32(SRT_Table)
		} else {
			v2 = int32(SRT_Upfrom)
		}
		eDest = v2
	} else {
		if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) {
			i = 0
			for {
				if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
					break
				}
				pList = _sqlite3ExprListAppend(tls, pParse, pList, _exprRowColumn(tls, pParse, i))
				goto _3
			_3:
				;
				i = i + 1
			}
			eDest = int32(SRT_Table)
		} else {
			if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
				v2 = int32(SRT_Table)
			} else {
				v2 = int32(SRT_Upfrom)
			}
			eDest = v2
			pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3PExpr(tls, pParse, int32(TK_ROW), uintptr(0), uintptr(0)))
		}
	}
	if pChanges != 0 {
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pChanges)).FnExpr) {
				break
			}
			pList = _sqlite3ExprListAppend(tls, pParse, pList, _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*20))).FpExpr, 0))
			goto _5
		_5:
			;
			i = i + 1
		}
	}
	pSelect = _sqlite3SelectNew(tls, pParse, pList, pSrc, pWhere2, pGrp, uintptr(0), pOrderBy2, uint32(libc.Int32FromInt32(SF_UFSrcCheck)|libc.Int32FromInt32(SF_IncludeHidden)|libc.Int32FromInt32(SF_UpdateFrom)), pLimit2)
	if pSelect != 0 {
		**(**Tu32)(__ccgo_up(pSelect + 4)) |= uint32(SF_OrderByReqd)
	}
	_sqlite3SelectDestInit(tls, bp, eDest, iEph)
	if pPk != 0 {
		v2 = int32((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)
	} else {
		v2 = -int32(1)
	}
	(**(**TSelectDest)(__ccgo_up(bp))).FiSDParm2 = v2
	_sqlite3Select(tls, pParse, pSelect, bp)
	_sqlite3SelectDelete(tls, db, pSelect)
}

// C documentation
//
//	/*
//	** Generate code for an UPDATE of a virtual table.
//	**
//	** There are two possible strategies - the default and the special
//	** "onepass" strategy. Onepass is only used if the virtual table
//	** implementation indicates that pWhere may match at most one row.
//	**
//	** The default strategy is to create an ephemeral table that contains
//	** for each row to be changed:
//	**
//	**   (A)  The original rowid of that row.
//	**   (B)  The revised rowid for the row.
//	**   (C)  The content of every column in the row.
//	**
//	** Then loop through the contents of this ephemeral table executing a
//	** VUpdate for each row. When finished, drop the ephemeral table.
//	**
//	** The "onepass" strategy does not use an ephemeral table. Instead, it
//	** stores the same values (A, B and C above) in a register array and
//	** makes a single invocation of VUpdate.
//	*/
func _updateVirtualTable(tls *libc.TLS, pParse uintptr, pSrc uintptr, pTab uintptr, pChanges uintptr, pRowid uintptr, aXRef uintptr, pWhere uintptr, onError int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var addr, eOnePass, ephemTab, i, iCsr, nArg, regArg, regRec, regRowid, v1 int32
	var db, pList, pPk, pPk1, pRow, pRowExpr, pVTab, pWInfo, v, v2 uintptr
	var iPk, iPk1 Ti16
	var _ /* aDummy at bp+0 */ [2]int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, db, eOnePass, ephemTab, i, iCsr, iPk, iPk1, nArg, pList, pPk, pPk1, pRow, pRowExpr, pVTab, pWInfo, regArg, regRec, regRowid, v, v1, v2
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* Loop counter */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb   /* Database connection */
	pVTab = _sqlite3GetVTable(tls, db, pTab)
	pWInfo = uintptr(0)
	nArg = int32(2) + int32((*TTable)(unsafe.Pointer(pTab)).FnCol) /* Register for ephemeral table rowid */
	iCsr = (*(*TSrcItem)(unsafe.Pointer(pSrc + 8))).FiCursor       /* Address of OP_OpenEphemeral */
	/* Allocate nArg registers in which to gather the arguments for VUpdate. Then
	 ** create and open the ephemeral table in which the records created from
	 ** these arguments will be temporarily stored. */
	v2 = pParse + 44
	v1 = *(*int32)(unsafe.Pointer(v2))
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	ephemTab = v1
	addr = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), ephemTab, nArg)
	regArg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1)
	**(**int32)(__ccgo_up(pParse + 48)) += nArg
	if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc > int32(1) {
		pPk = uintptr(0)
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			if pRowid != 0 {
				pRow = _sqlite3ExprDup(tls, db, pRowid, 0)
			} else {
				pRow = _sqlite3PExpr(tls, pParse, int32(TK_ROW), uintptr(0), uintptr(0))
			}
		} else { /* PRIMARY KEY column */
			pPk = _sqlite3PrimaryKeyIndex(tls, pTab)
			iPk = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn))
			if **(**int32)(__ccgo_up(aXRef + uintptr(iPk)*4)) >= 0 {
				pRow = _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(**(**int32)(__ccgo_up(aXRef + uintptr(iPk)*4)))*20))).FpExpr, 0)
			} else {
				pRow = _exprRowColumn(tls, pParse, int32(iPk))
			}
		}
		pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), pRow)
		i = 0
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			if **(**int32)(__ccgo_up(aXRef + uintptr(i)*4)) >= 0 {
				pList = _sqlite3ExprListAppend(tls, pParse, pList, _sqlite3ExprDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(**(**int32)(__ccgo_up(aXRef + uintptr(i)*4)))*20))).FpExpr, 0))
			} else {
				pRowExpr = _exprRowColumn(tls, pParse, i)
				if pRowExpr != 0 {
					(*TExpr)(unsafe.Pointer(pRowExpr)).Fop2 = uint8(OPFLAG_NOCHNG)
				}
				pList = _sqlite3ExprListAppend(tls, pParse, pList, pRowExpr)
			}
			goto _3
		_3:
			;
			i = i + 1
		}
		_updateFromSelect(tls, pParse, ephemTab, pPk, pList, pSrc, pWhere, uintptr(0), uintptr(0))
		_sqlite3ExprListDelete(tls, db, pList)
		eOnePass = ONEPASS_OFF
	} else {
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		regRec = v1
		v2 = pParse + 48
		*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
		v1 = *(*int32)(unsafe.Pointer(v2))
		regRowid = v1
		/* Start scanning the virtual table */
		pWInfo = _sqlite3WhereBegin(tls, pParse, pSrc, pWhere, uintptr(0), uintptr(0), uintptr(0), uint16(WHERE_ONEPASS_DESIRED), 0)
		if pWInfo == uintptr(0) {
			return
		}
		/* Populate the argument registers. */
		i = 0
		for {
			if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
				break
			}
			if **(**int32)(__ccgo_up(aXRef + uintptr(i)*4)) >= 0 {
				_sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(**(**int32)(__ccgo_up(aXRef + uintptr(i)*4)))*20))).FpExpr, regArg+int32(2)+i)
			} else {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_VColumn), iCsr, i, regArg+int32(2)+i)
				_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_NOCHNG)) /* For sqlite3_vtab_nochange() */
			}
			goto _8
		_8:
			;
			i = i + 1
		}
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCsr, regArg)
			if pRowid != 0 {
				_sqlite3ExprCode(tls, pParse, pRowid, regArg+int32(1))
			} else {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iCsr, regArg+int32(1))
			}
		} else { /* PRIMARY KEY column */
			pPk1 = _sqlite3PrimaryKeyIndex(tls, pTab)
			iPk1 = **(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk1)).FaiColumn))
			_sqlite3VdbeAddOp3(tls, v, int32(OP_VColumn), iCsr, int32(iPk1), regArg)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), regArg+int32(2)+int32(iPk1), regArg+int32(1))
		}
		eOnePass = _sqlite3WhereOkOnePass(tls, pWInfo, bp)
		/* There is no ONEPASS_MULTI on virtual tables */
		if eOnePass != 0 {
			/* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
			 ** above. */
			_sqlite3VdbeChangeToNoop(tls, v, addr)
			_sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iCsr)
		} else {
			/* Create a record from the argument register contents and insert it into
			 ** the ephemeral table. */
			_sqlite3MultiWrite(tls, pParse)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), regArg, nArg, regRec)
			_sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), ephemTab, regRowid)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), ephemTab, regRec, regRowid)
		}
	}
	if eOnePass == ONEPASS_OFF {
		/* End the virtual table scan */
		if (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc == int32(1) {
			_sqlite3WhereEnd(tls, pWInfo)
		}
		/* Begin scanning through the ephemeral table. */
		addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), ephemTab)
		/* Extract arguments from the current row of the ephemeral table and
		 ** invoke the VUpdate method.  */
		i = 0
		for {
			if !(i < nArg) {
				break
			}
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), ephemTab, i, regArg+i)
			goto _9
		_9:
			;
			i = i + 1
		}
	}
	_sqlite3VtabMakeWritable(tls, pParse, pTab)
	_sqlite3VdbeAddOp4(tls, v, int32(OP_VUpdate), 0, nArg, regArg, pVTab, -int32(12))
	if onError == int32(OE_Default) {
		v1 = int32(OE_Abort)
	} else {
		v1 = onError
	}
	_sqlite3VdbeChangeP5(tls, v, uint16(v1))
	_sqlite3MayAbort(tls, pParse)
	/* End of the ephemeral table scan. Or, if using the onepass strategy,
	 ** jump to here if the scan visited zero rows. */
	if eOnePass == ONEPASS_OFF {
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), ephemTab, addr+int32(1))
		_sqlite3VdbeJumpHere(tls, v, addr)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Close), ephemTab, 0)
	} else {
		_sqlite3WhereEnd(tls, pWInfo)
	}
}

/************** End of update.c **********************************************/
/************** Begin file upsert.c ******************************************/
/*
** 2018-04-12
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code to implement various aspects of UPSERT
** processing and handling of the Upsert object.
 */
/* #include "sqliteInt.h" */

// C documentation
//
//	/*
//	** Extract a value from the supplied expression in the manner described
//	** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
//	** using valueNew().
//	**
//	** If pCtx is NULL and an error occurs after the sqlite3_value object
//	** has been allocated, it is freed before returning. Or, if pCtx is not
//	** NULL, it is assumed that the caller will free any allocated object
//	** in all cases.
//	*/
func _valueFromExpr(tls *libc.TLS, db uintptr, pExpr uintptr, enc Tu8, affinity Tu8, ppVal uintptr, pCtx uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var aff Tu8
	var nVal, negInt, op, rc, v1 int32
	var pLeft, zNeg, zVal, v3 uintptr
	var _ /* iVal at bp+8 */ Ti64
	var _ /* pVal at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _ = aff, nVal, negInt, op, pLeft, rc, zNeg, zVal, v1, v3
	zVal = uintptr(0)
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	negInt = int32(1)
	zNeg = __ccgo_ts + 1711
	rc = SQLITE_OK
	for {
		v1 = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop)
		op = v1
		if !(v1 == int32(TK_UPLUS) || op == int32(TK_SPAN)) {
			break
		}
		pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
	}
	if op == int32(TK_REGISTER) {
		op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop2)
	}
	/* Compressed expressions only appear when parsing the DEFAULT clause
	 ** on a table column definition, and hence only when pCtx==0.  This
	 ** check ensures that an EP_TokenOnly expression is never passed down
	 ** into valueFromFunction(). */
	if op == int32(TK_CAST) {
		aff = uint8(_sqlite3AffinityType(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), uintptr(0)))
		rc = _valueFromExpr(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, enc, aff, ppVal, pCtx)
		if **(**uintptr)(__ccgo_up(ppVal)) != 0 {
			if int32((*Tsqlite3_value)(unsafe.Pointer(**(**uintptr)(__ccgo_up(ppVal)))).Fflags)&int32(MEM_Zero) != 0 {
				v1 = _sqlite3VdbeMemExpandBlob(tls, **(**uintptr)(__ccgo_up(ppVal)))
			} else {
				v1 = 0
			}
			rc = v1
			_sqlite3VdbeMemCast(tls, **(**uintptr)(__ccgo_up(ppVal)), aff, enc)
			_sqlite3ValueApplyAffinity(tls, **(**uintptr)(__ccgo_up(ppVal)), affinity, enc)
		}
		return rc
	}
	/* Handle negative integers in a single step.  This is needed in the
	 ** case when the value is -9223372036854775808. Except - do not do this
	 ** for hexadecimal literals.  */
	if op == int32(TK_UMINUS) {
		pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
		if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_INTEGER) || int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_FLOAT) {
			if (*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != uint32(0) || int32(**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pLeft + 8))))) != int32('0') || int32(**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pLeft + 8)) + 1))) & ^libc.Int32FromInt32(0x20) != int32('X') {
				pExpr = pLeft
				op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop)
				negInt = -int32(1)
				zNeg = __ccgo_ts + 6442
			}
		}
	}
	if op == int32(TK_STRING) || op == int32(TK_FLOAT) || op == int32(TK_INTEGER) {
		**(**uintptr)(__ccgo_up(bp)) = _valueNew(tls, db, pCtx)
		if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
			goto no_mem
		}
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != uint32(0) {
			_sqlite3VdbeMemSetInt64(tls, **(**uintptr)(__ccgo_up(bp)), int64(*(*int32)(unsafe.Pointer(pExpr + 8)))*int64(negInt))
		} else {
			if op == int32(TK_INTEGER) && 0 == _sqlite3DecOrHexToI64(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), bp+8) {
				_sqlite3VdbeMemSetInt64(tls, **(**uintptr)(__ccgo_up(bp)), **(**Ti64)(__ccgo_up(bp + 8))*int64(negInt))
			} else {
				zVal = _sqlite3MPrintf(tls, db, __ccgo_ts+6444, libc.VaList(bp+24, zNeg, *(*uintptr)(unsafe.Pointer(pExpr + 8))))
				if zVal == uintptr(0) {
					goto no_mem
				}
				_sqlite3ValueSetStr(tls, **(**uintptr)(__ccgo_up(bp)), -int32(1), zVal, uint8(SQLITE_UTF8), __ccgo_fp(_sqlite3RowSetClear))
			}
		}
		if int32(affinity) == int32(SQLITE_AFF_BLOB) {
			if op == int32(TK_FLOAT) {
				_sqlite3AtoF(tls, (*Tsqlite3_value)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fz, **(**uintptr)(__ccgo_up(bp)))
				(*Tsqlite3_value)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fflags = uint16(MEM_Real)
			} else {
				if op == int32(TK_INTEGER) {
					/* This case is required by -9223372036854775808 and other strings
					 ** that look like integers but cannot be handled by the
					 ** sqlite3DecOrHexToI64() call above.  */
					_sqlite3ValueApplyAffinity(tls, **(**uintptr)(__ccgo_up(bp)), uint8(SQLITE_AFF_NUMERIC), uint8(SQLITE_UTF8))
				}
			}
		} else {
			_sqlite3ValueApplyAffinity(tls, **(**uintptr)(__ccgo_up(bp)), affinity, uint8(SQLITE_UTF8))
		}
		if int32((*Tsqlite3_value)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)|libc.Int32FromInt32(MEM_Real)) != 0 {
			v3 = **(**uintptr)(__ccgo_up(bp)) + 16
			*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) & ^libc.Int32FromInt32(MEM_Str))
		}
		if int32(enc) != int32(SQLITE_UTF8) {
			rc = _sqlite3VdbeChangeEncoding(tls, **(**uintptr)(__ccgo_up(bp)), int32(enc))
		}
	} else {
		if op == int32(TK_UMINUS) {
			/* This branch happens for multiple negative signs.  Ex: -(-5) */
			if SQLITE_OK == _valueFromExpr(tls, db, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft, enc, affinity, bp, pCtx) && **(**uintptr)(__ccgo_up(bp)) != uintptr(0) {
				_sqlite3VdbeMemNumerify(tls, **(**uintptr)(__ccgo_up(bp)))
				if int32((*Tsqlite3_value)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fflags)&int32(MEM_Real) != 0 {
					*(*float64)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))) = -*(*float64)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp))))
				} else {
					if *(*Ti64)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))) == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)) {
						*(*float64)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))) = -float64(int64(-libc.Int32FromInt32(1)) - (libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)))
						(*Tsqlite3_value)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fflags = uint16(int32((*Tsqlite3_value)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Real))
					} else {
						*(*Ti64)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))) = -*(*Ti64)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp))))
					}
				}
				_sqlite3ValueApplyAffinity(tls, **(**uintptr)(__ccgo_up(bp)), affinity, enc)
			}
		} else {
			if op == int32(TK_NULL) {
				**(**uintptr)(__ccgo_up(bp)) = _valueNew(tls, db, pCtx)
				if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
					goto no_mem
				}
				_sqlite3VdbeMemSetNull(tls, **(**uintptr)(__ccgo_up(bp)))
			} else {
				if op == int32(TK_BLOB) {
					**(**uintptr)(__ccgo_up(bp)) = _valueNew(tls, db, pCtx)
					if !(**(**uintptr)(__ccgo_up(bp)) != 0) {
						goto no_mem
					}
					zVal = *(*uintptr)(unsafe.Pointer(pExpr + 8)) + 2
					nVal = _sqlite3Strlen30(tls, zVal) - int32(1)
					_sqlite3VdbeMemSetStr(tls, **(**uintptr)(__ccgo_up(bp)), _sqlite3HexToBlob(tls, db, zVal, nVal), int64(nVal/int32(2)), uint8(0), __ccgo_fp(_sqlite3RowSetClear))
				} else {
					if op == int32(TK_FUNCTION) && pCtx != uintptr(0) {
						rc = _valueFromFunction(tls, db, pExpr, enc, affinity, bp, pCtx)
					} else {
						if op == int32(TK_TRUEFALSE) {
							**(**uintptr)(__ccgo_up(bp)) = _valueNew(tls, db, pCtx)
							if **(**uintptr)(__ccgo_up(bp)) != 0 {
								(*Tsqlite3_value)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).Fflags = uint16(MEM_Int)
								*(*Ti64)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))) = libc.BoolInt64(int32(**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(pExpr + 8)) + 4))) == 0)
								_sqlite3ValueApplyAffinity(tls, **(**uintptr)(__ccgo_up(bp)), affinity, enc)
							}
						}
					}
				}
			}
		}
	}
	**(**uintptr)(__ccgo_up(ppVal)) = **(**uintptr)(__ccgo_up(bp))
	return rc
	goto no_mem
no_mem:
	;
	if pCtx == uintptr(0) || (*TParse)(unsafe.Pointer((*TValueNewStat4Ctx)(unsafe.Pointer(pCtx)).FpParse)).FnErr == 0 {
		_sqlite3OomFault(tls, db)
	}
	_sqlite3DbFree(tls, db, zVal)
	if pCtx == uintptr(0) {
		_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	return int32(SQLITE_NOMEM)
}

// C documentation
//
//	/*
//	** The expression object indicated by the second argument is guaranteed
//	** to be a scalar SQL function. If
//	**
//	**   * all function arguments are SQL literals,
//	**   * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
//	**   * the SQLITE_FUNC_NEEDCOLL function flag is not set,
//	**
//	** then this routine attempts to invoke the SQL function. Assuming no
//	** error occurs, output parameter (*ppVal) is set to point to a value
//	** object containing the result before returning SQLITE_OK.
//	**
//	** Affinity aff is applied to the result of the function before returning.
//	** If the result is a text value, the sqlite3_value object uses encoding
//	** enc.
//	**
//	** If the conditions above are not met, this function returns SQLITE_OK
//	** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
//	** NULL and an SQLite error code returned.
//	*/
func _valueFromFunction(tls *libc.TLS, db uintptr, p uintptr, enc Tu8, aff Tu8, ppVal uintptr, pCtx uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var apVal, pFunc, pList, pVal uintptr
	var i, nVal, rc int32
	var _ /* ctx at bp+0 */ Tsqlite3_context
	_, _, _, _, _, _, _ = apVal, i, nVal, pFunc, pList, pVal, rc /* Context object for function invocation */
	apVal = uintptr(0)                                           /* Function arguments */
	nVal = 0                                                     /* Number of function arguments */
	pFunc = uintptr(0)                                           /* Function definition */
	pVal = uintptr(0)                                            /* New value */
	rc = SQLITE_OK                                               /* Return code */
	pList = uintptr(0)                                           /* Iterator variable */
	pList = *(*uintptr)(unsafe.Pointer(p + 20))
	if pList != 0 {
		nVal = (*TExprList)(unsafe.Pointer(pList)).FnExpr
	}
	pFunc = _sqlite3FindFunction(tls, db, *(*uintptr)(unsafe.Pointer(p + 8)), nVal, enc, uint8(0))
	if (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)|libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG)) == uint32(0) || (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)|libc.Int32FromInt32(SQLITE_FUNC_RUNONLY)) != uint32(0) {
		return SQLITE_OK
	}
	if pList != 0 {
		apVal = _sqlite3DbMallocZero(tls, db, uint64(uint32(4)*uint32(nVal)))
		if apVal == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
			goto value_from_function_out
		}
		i = 0
		for {
			if !(i < nVal) {
				break
			}
			rc = _sqlite3Stat4ValueFromExpr(tls, (*TValueNewStat4Ctx)(unsafe.Pointer(pCtx)).FpParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr, aff, apVal+uintptr(i)*4)
			if **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4)) == uintptr(0) || rc != SQLITE_OK {
				goto value_from_function_out
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	pVal = _valueNew(tls, db, pCtx)
	if pVal == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
		goto value_from_function_out
	}
	libc.Xmemset(tls, bp, 0, uint32(28))
	(**(**Tsqlite3_context)(__ccgo_up(bp))).FpOut = pVal
	(**(**Tsqlite3_context)(__ccgo_up(bp))).FpFunc = pFunc
	(**(**Tsqlite3_context)(__ccgo_up(bp))).Fenc = (*Tsqlite3)(unsafe.Pointer(db)).Fenc
	(*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer(pFunc)).FxSFunc})))(tls, bp, nVal, apVal)
	if (**(**Tsqlite3_context)(__ccgo_up(bp))).FisError != 0 {
		rc = (**(**Tsqlite3_context)(__ccgo_up(bp))).FisError
		_sqlite3ErrorMsg(tls, (*TValueNewStat4Ctx)(unsafe.Pointer(pCtx)).FpParse, __ccgo_ts+4729, libc.VaList(bp+40, Xsqlite3_value_text(tls, pVal)))
	} else {
		_sqlite3ValueApplyAffinity(tls, pVal, aff, uint8(SQLITE_UTF8))
		rc = _sqlite3VdbeChangeEncoding(tls, pVal, int32(enc))
		if rc == SQLITE_OK && _sqlite3VdbeMemTooBig(tls, pVal) != 0 {
			rc = int32(SQLITE_TOOBIG)
			(*TParse)(unsafe.Pointer((*TValueNewStat4Ctx)(unsafe.Pointer(pCtx)).FpParse)).FnErr = (*TParse)(unsafe.Pointer((*TValueNewStat4Ctx)(unsafe.Pointer(pCtx)).FpParse)).FnErr + 1
		}
	}
	goto value_from_function_out
value_from_function_out:
	;
	if rc != SQLITE_OK {
		pVal = uintptr(0)
		(*TParse)(unsafe.Pointer((*TValueNewStat4Ctx)(unsafe.Pointer(pCtx)).FpParse)).Frc = rc
	}
	if apVal != 0 {
		i = 0
		for {
			if !(i < nVal) {
				break
			}
			_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up(apVal + uintptr(i)*4)))
			goto _2
		_2:
			;
			i = i + 1
		}
		_sqlite3DbFreeNN(tls, db, apVal)
	}
	**(**uintptr)(__ccgo_up(ppVal)) = pVal
	return rc
}

// C documentation
//
//	/*
//	** Implementation of sqlite3_vtab_in_first() (if bNext==0) and
//	** sqlite3_vtab_in_next() (if bNext!=0).
//	*/
func _valueFromValueList(tls *libc.TLS, pVal uintptr, ppOut uintptr, bNext int32) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var iOff, rc, v1 int32
	var pOut, pRhs, zBuf uintptr
	var sz Tu32
	var _ /* dummy at bp+0 */ int32
	var _ /* iSerial at bp+48 */ Tu32
	var _ /* sMem at bp+8 */ TMem
	_, _, _, _, _, _, _ = iOff, pOut, pRhs, rc, sz, zBuf, v1
	**(**uintptr)(__ccgo_up(ppOut)) = uintptr(0)
	if pVal == uintptr(0) {
		return _sqlite3MisuseError(tls, int32(94730))
	}
	if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&int32(MEM_Dyn) == 0 || (*Tsqlite3_value)(unsafe.Pointer(pVal)).FxDel != __ccgo_fp(_sqlite3VdbeValueListFree) {
		return int32(SQLITE_ERROR)
	} else {
		pRhs = (*Tsqlite3_value)(unsafe.Pointer(pVal)).Fz
	}
	if bNext != 0 {
		rc = _sqlite3BtreeNext(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr, 0)
	} else {
		**(**int32)(__ccgo_up(bp)) = 0
		rc = _sqlite3BtreeFirst(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr, bp)
		if _sqlite3BtreeEof(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr) != 0 {
			rc = int32(SQLITE_DONE)
		}
	}
	if rc == SQLITE_OK { /* Raw content of current row */
		libc.Xmemset(tls, bp+8, 0, uint32(40))
		sz = _sqlite3BtreePayloadSize(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr)
		rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr, sz, bp+8)
		if rc == SQLITE_OK {
			zBuf = (**(**TMem)(__ccgo_up(bp + 8))).Fz
			pOut = (*TValueList)(unsafe.Pointer(pRhs)).FpOut
			if int32(**(**Tu8)(__ccgo_up(zBuf + 1))) < int32(libc.Uint8FromInt32(0x80)) {
				**(**Tu32)(__ccgo_up(bp + 48)) = uint32(**(**Tu8)(__ccgo_up(zBuf + 1)))
				v1 = libc.Int32FromInt32(1)
			} else {
				v1 = int32(_sqlite3GetVarint32(tls, zBuf+1, bp+48))
			}
			iOff = int32(1) + int32(uint8(v1))
			_sqlite3VdbeSerialGet(tls, zBuf+uintptr(iOff), **(**Tu32)(__ccgo_up(bp + 48)), pOut)
			(*Tsqlite3_value)(unsafe.Pointer(pOut)).Fenc = (*Tsqlite3)(unsafe.Pointer((*Tsqlite3_value)(unsafe.Pointer(pOut)).Fdb)).Fenc
			if int32((*Tsqlite3_value)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Ephem) != 0 && _sqlite3VdbeMemMakeWriteable(tls, pOut) != 0 {
				rc = int32(SQLITE_NOMEM)
			} else {
				**(**uintptr)(__ccgo_up(ppOut)) = pOut
			}
		}
		_sqlite3VdbeMemRelease(tls, bp+8)
	}
	return rc
}

// C documentation
//
//	/*
//	** Allocate and return a pointer to a new sqlite3_value object. If
//	** the second argument to this function is NULL, the object is allocated
//	** by calling sqlite3ValueNew().
//	**
//	** Otherwise, if the second argument is non-zero, then this function is
//	** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
//	** already been allocated, allocate the UnpackedRecord structure that
//	** that function will return to its caller here. Then return a pointer to
//	** an sqlite3_value within the UnpackedRecord.a[] array.
//	*/
func _valueNew(tls *libc.TLS, db uintptr, p uintptr) (r uintptr) {
	var i, nCol int32
	var nByte Ti64
	var pIdx, pRec uintptr
	_, _, _, _, _ = i, nByte, nCol, pIdx, pRec
	if p != 0 {
		pRec = **(**uintptr)(__ccgo_up((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FppRec))
		if pRec == uintptr(0) {
			pIdx = (*TValueNewStat4Ctx)(unsafe.Pointer(p)).FpIdx   /* Counter variable */
			nCol = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) /* Number of index columns including rowid */
			nByte = int64(uint32(40)*uint32(nCol) + (libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
			pRec = _sqlite3DbMallocZero(tls, db, uint64(nByte))
			if pRec != 0 {
				(*TUnpackedRecord)(unsafe.Pointer(pRec)).FpKeyInfo = _sqlite3KeyInfoOfIndex(tls, (*TValueNewStat4Ctx)(unsafe.Pointer(p)).FpParse, pIdx)
				if (*TUnpackedRecord)(unsafe.Pointer(pRec)).FpKeyInfo != 0 {
					(*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem = pRec + uintptr((libc.Uint32FromInt64(32)+libc.Uint32FromInt32(7))&uint32(^libc.Int32FromInt32(7)))
					i = 0
					for {
						if !(i < nCol) {
							break
						}
						(**(**TMem)(__ccgo_up((*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem + uintptr(i)*40))).Fflags = uint16(MEM_Null)
						(**(**TMem)(__ccgo_up((*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem + uintptr(i)*40))).Fdb = db
						goto _1
					_1:
						;
						i = i + 1
					}
				} else {
					_sqlite3DbFreeNN(tls, db, pRec)
					pRec = uintptr(0)
				}
			}
			if pRec == uintptr(0) {
				return uintptr(0)
			}
			**(**uintptr)(__ccgo_up((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FppRec)) = pRec
		}
		(*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField = uint16((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FiVal + int32(1))
		_sqlite3VdbeMemSetNull(tls, (*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem+uintptr((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FiVal)*40)
		return (*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem + uintptr((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FiVal)*40
	}
	return _sqlite3ValueNew(tls, db)
}

// C documentation
//
//	/*
//	** The pVal argument is known to be a value other than NULL.
//	** Convert it into a string with encoding enc and return a pointer
//	** to a zero-terminated version of that string.
//	*/
func _valueToText(tls *libc.TLS, pVal uintptr, enc Tu8) (r uintptr) {
	var v1 int32
	var v2 uintptr
	_, _ = v1, v2
	if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&(libc.Int32FromInt32(MEM_Blob)|libc.Int32FromInt32(MEM_Str)) != 0 {
		if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fflags)&int32(MEM_Zero) != 0 {
			v1 = _sqlite3VdbeMemExpandBlob(tls, pVal)
		} else {
			v1 = 0
		}
		if v1 != 0 {
			return uintptr(0)
		}
		v2 = pVal + 16
		*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(MEM_Str))
		if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fenc) != int32(enc) & ^libc.Int32FromInt32(SQLITE_UTF16_ALIGNED) {
			_sqlite3VdbeChangeEncoding(tls, pVal, int32(enc) & ^libc.Int32FromInt32(SQLITE_UTF16_ALIGNED))
		}
		if int32(enc)&int32(SQLITE_UTF16_ALIGNED) != 0 && int32(1) == int32(1)&int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fz) {
			if _sqlite3VdbeMemMakeWriteable(tls, pVal) != SQLITE_OK {
				return uintptr(0)
			}
		}
		_sqlite3VdbeMemNulTerminate(tls, pVal) /* IMP: R-31275-44060 */
	} else {
		_sqlite3VdbeMemStringify(tls, pVal, enc, uint8(0))
	}
	if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fenc) == int32(enc) & ^libc.Int32FromInt32(SQLITE_UTF16_ALIGNED) {
		return (*Tsqlite3_value)(unsafe.Pointer(pVal)).Fz
	} else {
		return uintptr(0)
	}
	return r
}

// C documentation
//
//	/*
//	** Change the value of the P4 operand for a specific instruction.
//	** This routine is useful when a large program is loaded from a
//	** static array using sqlite3VdbeAddOpList but we want to make a
//	** few minor changes to the program.
//	**
//	** If n>=0 then the P4 operand is dynamic, meaning that a copy of
//	** the string is made into memory obtained from sqlite3_malloc().
//	** A value of n==0 means copy bytes of zP4 up to and including the
//	** first null byte.  If n>0 then copy n+1 bytes of zP4.
//	**
//	** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
//	** to a string or structure that is guaranteed to exist for the lifetime of
//	** the Vdbe. In these cases we can just copy the pointer.
//	**
//	** If addr<0 then change P4 on the most recently inserted instruction.
//	*/
func _vdbeChangeP4Full(tls *libc.TLS, p uintptr, pOp uintptr, zP4 uintptr, n int32) {
	if (*TOp)(unsafe.Pointer(pOp)).Fp4type != 0 {
		(*TOp)(unsafe.Pointer(pOp)).Fp4type = 0
		*(*uintptr)(unsafe.Pointer(pOp + 16)) = uintptr(0)
	}
	if n < 0 {
		_sqlite3VdbeChangeP4(tls, p, (int32(pOp)-int32((*TVdbe)(unsafe.Pointer(p)).FaOp))/20, zP4, n)
	} else {
		if n == 0 {
			n = _sqlite3Strlen30(tls, zP4)
		}
		*(*uintptr)(unsafe.Pointer(pOp + 16)) = _sqlite3DbStrNDup(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, zP4, uint64(n))
		(*TOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(7))
	}
}

// C documentation
//
//	/*
//	** For OP_Column, factor out the case where content is loaded from
//	** overflow pages, so that the code to implement this case is separate
//	** the common case where all content fits on the page.  Factoring out
//	** the code reduces register pressure and helps the common case
//	** to run faster.
//	*/
func _vdbeColumnFromOverflow(tls *libc.TLS, pC uintptr, iCol int32, t Tu32, iOffset Ti64, cacheStatus Tu32, colCacheCtr Tu32, pDest uintptr) (r int32) {
	var db, pBuf, pCache, v1 uintptr
	var encoding, len1, rc int32
	_, _, _, _, _, _, _ = db, encoding, len1, pBuf, pCache, rc, v1
	db = (*TMem)(unsafe.Pointer(pDest)).Fdb
	encoding = int32((*TMem)(unsafe.Pointer(pDest)).Fenc)
	len1 = int32(_sqlite3VdbeSerialTypeLen(tls, t))
	if len1 > **(**int32)(__ccgo_up(db + 120)) {
		return int32(SQLITE_TOOBIG)
	}
	if len1 > int32(4000) && (*TVdbeCursor)(unsafe.Pointer(pC)).FpKeyInfo == uintptr(0) {
		if int32(TBool(*(*uint8)(unsafe.Pointer(pC + 8))&0x10>>4)) == 0 {
			(*TVdbeCursor)(unsafe.Pointer(pC)).FpCache = _sqlite3DbMallocZero(tls, db, uint64(32))
			if (*TVdbeCursor)(unsafe.Pointer(pC)).FpCache == uintptr(0) {
				return int32(SQLITE_NOMEM)
			}
			libc.SetBitFieldPtr8Uint32(pC+8, libc.Uint32FromInt32(1), 4, 0x10)
		}
		pCache = (*TVdbeCursor)(unsafe.Pointer(pC)).FpCache
		if (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue == uintptr(0) || (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FiCol != iCol || (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FcacheStatus != cacheStatus || (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FcolCacheCtr != colCacheCtr || (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FiOffset != _sqlite3BtreeOffset(tls, *(*uintptr)(unsafe.Pointer(pC + 36))) {
			if (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue != 0 {
				_sqlite3RCStrUnref(tls, (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue)
			}
			v1 = _sqlite3RCStrNew(tls, uint64(len1+int32(3)))
			(*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue = v1
			pBuf = v1
			if pBuf == uintptr(0) {
				return int32(SQLITE_NOMEM)
			}
			rc = _sqlite3BtreePayload(tls, *(*uintptr)(unsafe.Pointer(pC + 36)), uint32(iOffset), uint32(len1), pBuf)
			if rc != 0 {
				return rc
			}
			**(**int8)(__ccgo_up(pBuf + uintptr(len1))) = 0
			**(**int8)(__ccgo_up(pBuf + uintptr(len1+int32(1)))) = 0
			**(**int8)(__ccgo_up(pBuf + uintptr(len1+int32(2)))) = 0
			(*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FiCol = iCol
			(*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FcacheStatus = cacheStatus
			(*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FcolCacheCtr = colCacheCtr
			(*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FiOffset = _sqlite3BtreeOffset(tls, *(*uintptr)(unsafe.Pointer(pC + 36)))
		} else {
			pBuf = (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue
		}
		_sqlite3RCStrRef(tls, pBuf)
		if t&uint32(1) != 0 {
			rc = _sqlite3VdbeMemSetStr(tls, pDest, pBuf, int64(len1), uint8(encoding), __ccgo_fp(_sqlite3RCStrUnref))
			v1 = pDest + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
		} else {
			rc = _sqlite3VdbeMemSetStr(tls, pDest, pBuf, int64(len1), uint8(0), __ccgo_fp(_sqlite3RCStrUnref))
		}
	} else {
		rc = _sqlite3VdbeMemFromBtree(tls, *(*uintptr)(unsafe.Pointer(pC + 36)), uint32(iOffset), uint32(len1), pDest)
		if rc != 0 {
			return rc
		}
		_sqlite3VdbeSerialGet(tls, (*TMem)(unsafe.Pointer(pDest)).Fz, t, pDest)
		if t&uint32(1) != uint32(0) && encoding == int32(SQLITE_UTF8) {
			**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pDest)).Fz + uintptr(len1))) = 0
			v1 = pDest + 16
			*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
		}
	}
	v1 = pDest + 16
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) & ^libc.Int32FromInt32(MEM_Ephem))
	return rc
}

// C documentation
//
//	/*
//	** A read or write transaction may or may not be active on database handle
//	** db. If a transaction is active, commit it. If there is a
//	** write-transaction spanning more than one database file, this routine
//	** takes care of the super-journal trickery.
//	*/
func _vdbeCommit(tls *libc.TLS, db uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i, nMainFile, nTrans, needXcommit, rc, retryCount, txn, v5 int32
	var offset Ti64
	var pBt, pBt1, pBt2, pBt3, pBt4, pBt5, pPager, pVfs, zFile, zMainFile, zSuper uintptr
	var v6 bool
	var _ /* iRandom at bp+8 */ Tu32
	var _ /* pSuperJrnl at bp+0 */ uintptr
	var _ /* res at bp+4 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, nMainFile, nTrans, needXcommit, offset, pBt, pBt1, pBt2, pBt3, pBt4, pBt5, pPager, pVfs, rc, retryCount, txn, zFile, zMainFile, zSuper, v5, v6
	nTrans = 0 /* Number of databases with an active write-transaction
	 ** that are candidates for a two-phase commit using a
	 ** super-journal */
	rc = SQLITE_OK
	needXcommit = 0
	/* Before doing anything else, call the xSync() callback for any
	 ** virtual module tables written in this transaction. This has to
	 ** be done before determining whether a super-journal file is
	 ** required, as an xSync() callback may add an attached database
	 ** to the transaction.
	 */
	rc = _sqlite3VtabSync(tls, db, p)
	/* This loop determines (a) if the commit hook should be invoked and
	 ** (b) how many database files have open write transactions, not
	 ** including the temp database. (b) is important because if more than
	 ** one database file has an open write transaction, a super-journal
	 ** file is required for an atomic commit.
	 */
	i = 0
	for {
		if !(rc == SQLITE_OK && i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
			break
		}
		pBt = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
		if _sqlite3BtreeTxnState(tls, pBt) == int32(SQLITE_TXN_WRITE) {
			needXcommit = int32(1)
			_sqlite3BtreeEnter(tls, pBt)
			pPager = _sqlite3BtreePager(tls, pBt)
			if int32((**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).Fsafety_level) != int32(PAGER_SYNCHRONOUS_OFF) && _aMJNeeded[_sqlite3PagerGetJournalMode(tls, pPager)] != 0 && _sqlite3PagerIsMemdb(tls, pPager) == 0 {
				nTrans = nTrans + 1
			}
			rc = _sqlite3PagerExclusiveLock(tls, pPager)
			_sqlite3BtreeLeave(tls, pBt)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if rc != SQLITE_OK {
		return rc
	}
	/* If there are any write-transactions at all, invoke the commit hook */
	if needXcommit != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FxCommitCallback != 0 {
		rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3)(unsafe.Pointer(db)).FxCommitCallback})))(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpCommitArg)
		if rc != 0 {
			return libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
		}
	}
	/* The simple case - no more than one database file (not counting the
	 ** TEMP database) has a transaction active.   There is no need for the
	 ** super-journal.
	 **
	 ** If the return value of sqlite3BtreeGetFilename() is a zero length
	 ** string, it means the main database is :memory: or a temp file.  In
	 ** that case we do not support atomic multi-file commits, so use the
	 ** simple case then too.
	 */
	if 0 == _sqlite3Strlen30(tls, _sqlite3BtreeGetFilename(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpBt)) || nTrans <= int32(1) {
		if needXcommit != 0 {
			i = 0
			for {
				if !(rc == SQLITE_OK && i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
					break
				}
				pBt1 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
				if _sqlite3BtreeTxnState(tls, pBt1) >= int32(SQLITE_TXN_WRITE) {
					rc = _sqlite3BtreeCommitPhaseOne(tls, pBt1, uintptr(0))
				}
				goto _2
			_2:
				;
				i = i + 1
			}
		}
		/* Do the commit only if all databases successfully complete phase 1.
		 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
		 ** IO error while deleting or truncating a journal file. It is unlikely,
		 ** but could happen. In this case abandon processing and return the error.
		 */
		i = 0
		for {
			if !(rc == SQLITE_OK && i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			pBt2 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
			txn = _sqlite3BtreeTxnState(tls, pBt2)
			if txn != SQLITE_TXN_NONE {
				rc = _sqlite3BtreeCommitPhaseTwo(tls, pBt2, 0)
			}
			goto _3
		_3:
			;
			i = i + 1
		}
		if rc == SQLITE_OK {
			_sqlite3VtabCommit(tls, db)
		}
	} else {
		pVfs = (*Tsqlite3)(unsafe.Pointer(db)).FpVfs
		zSuper = uintptr(0) /* File-name for the super-journal */
		zMainFile = _sqlite3BtreeGetFilename(tls, (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpBt)
		**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		offset = 0
		retryCount = 0
		/* Select a super-journal file name */
		nMainFile = _sqlite3Strlen30(tls, zMainFile)
		zSuper = _sqlite3MPrintf(tls, db, __ccgo_ts+6555, libc.VaList(bp+24, 0, zMainFile, 0))
		if zSuper == uintptr(0) {
			return int32(SQLITE_NOMEM)
		}
		zSuper = zSuper + uintptr(4)
		for cond := true; cond; cond = rc == SQLITE_OK && **(**int32)(__ccgo_up(bp + 4)) != 0 {
			if retryCount != 0 {
				if retryCount > int32(100) {
					Xsqlite3_log(tls, int32(SQLITE_FULL), __ccgo_ts+6567, libc.VaList(bp+24, zSuper))
					_sqlite3OsDelete(tls, pVfs, zSuper, 0)
					break
				} else {
					if retryCount == int32(1) {
						Xsqlite3_log(tls, int32(SQLITE_FULL), __ccgo_ts+6581, libc.VaList(bp+24, zSuper))
					}
				}
			}
			retryCount = retryCount + 1
			Xsqlite3_randomness(tls, int32(4), bp+8)
			Xsqlite3_snprintf(tls, int32(13), zSuper+uintptr(nMainFile), __ccgo_ts+6596, libc.VaList(bp+24, **(**Tu32)(__ccgo_up(bp + 8))>>libc.Int32FromInt32(8)&uint32(0xffffff), **(**Tu32)(__ccgo_up(bp + 8))&uint32(0xff)))
			/* The antipenultimate character of the super-journal name must
			 ** be "9" to avoid name collisions when using 8+3 filenames. */
			rc = _sqlite3OsAccess(tls, pVfs, zSuper, SQLITE_ACCESS_EXISTS, bp+4)
		}
		if rc == SQLITE_OK {
			/* Open the super-journal. */
			rc = _sqlite3OsOpenMalloc(tls, pVfs, zSuper, bp, libc.Int32FromInt32(SQLITE_OPEN_READWRITE)|libc.Int32FromInt32(SQLITE_OPEN_CREATE)|libc.Int32FromInt32(SQLITE_OPEN_EXCLUSIVE)|libc.Int32FromInt32(SQLITE_OPEN_SUPER_JOURNAL), uintptr(0))
		}
		if rc != SQLITE_OK {
			_sqlite3DbFree(tls, db, zSuper-uintptr(4))
			return rc
		}
		/* Write the name of each database file in the transaction into the new
		 ** super-journal file. If an error occurs at this point close
		 ** and delete the super-journal file. All the individual journal files
		 ** still have 'null' as the super-journal pointer, so they will roll
		 ** back independently if a failure occurs.
		 */
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			pBt3 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
			if _sqlite3BtreeTxnState(tls, pBt3) == int32(SQLITE_TXN_WRITE) {
				zFile = _sqlite3BtreeGetJournalname(tls, pBt3)
				if zFile == uintptr(0) {
					goto _4 /* Ignore TEMP and :memory: databases */
				}
				rc = _sqlite3OsWrite(tls, **(**uintptr)(__ccgo_up(bp)), zFile, _sqlite3Strlen30(tls, zFile)+int32(1), offset)
				offset = offset + int64(_sqlite3Strlen30(tls, zFile)+int32(1))
				if rc != SQLITE_OK {
					_sqlite3OsCloseFree(tls, **(**uintptr)(__ccgo_up(bp)))
					_sqlite3OsDelete(tls, pVfs, zSuper, 0)
					_sqlite3DbFree(tls, db, zSuper-uintptr(4))
					return rc
				}
			}
			goto _4
		_4:
			;
			i = i + 1
		}
		/* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
		 ** flag is set this is not required.
		 */
		if v6 = 0 == _sqlite3OsDeviceCharacteristics(tls, **(**uintptr)(__ccgo_up(bp)))&int32(SQLITE_IOCAP_SEQUENTIAL); v6 {
			v5 = _sqlite3OsSync(tls, **(**uintptr)(__ccgo_up(bp)), int32(SQLITE_SYNC_NORMAL))
			rc = v5
		}
		if v6 && SQLITE_OK != v5 {
			_sqlite3OsCloseFree(tls, **(**uintptr)(__ccgo_up(bp)))
			_sqlite3OsDelete(tls, pVfs, zSuper, 0)
			_sqlite3DbFree(tls, db, zSuper-uintptr(4))
			return rc
		}
		/* Sync all the db files involved in the transaction. The same call
		 ** sets the super-journal pointer in each individual journal. If
		 ** an error occurs here, do not delete the super-journal file.
		 **
		 ** If the error occurs during the first call to
		 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
		 ** super-journal file will be orphaned. But we cannot delete it,
		 ** in case the super-journal file name was written into the journal
		 ** file before the failure occurred.
		 */
		i = 0
		for {
			if !(rc == SQLITE_OK && i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			pBt4 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
			if pBt4 != 0 {
				rc = _sqlite3BtreeCommitPhaseOne(tls, pBt4, zSuper)
			}
			goto _7
		_7:
			;
			i = i + 1
		}
		_sqlite3OsCloseFree(tls, **(**uintptr)(__ccgo_up(bp)))
		if rc != SQLITE_OK {
			_sqlite3DbFree(tls, db, zSuper-uintptr(4))
			return rc
		}
		/* Delete the super-journal file. This commits the transaction. After
		 ** doing this the directory is synced again before any individual
		 ** transaction files are deleted.
		 */
		rc = _sqlite3OsDelete(tls, pVfs, zSuper, int32(1))
		_sqlite3DbFree(tls, db, zSuper-uintptr(4))
		zSuper = uintptr(0)
		if rc != 0 {
			return rc
		}
		/* All files and directories have already been synced, so the following
		 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
		 ** deleting or truncating journals. If something goes wrong while
		 ** this is happening we don't really care. The integrity of the
		 ** transaction is already guaranteed, but some stray 'cold' journals
		 ** may be lying around. Returning an error code won't help matters.
		 */
		_sqlite3BeginBenignMalloc(tls)
		i = 0
		for {
			if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) {
				break
			}
			pBt5 = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*16))).FpBt
			if pBt5 != 0 {
				_sqlite3BtreeCommitPhaseTwo(tls, pBt5, int32(1))
			}
			goto _8
		_8:
			;
			i = i + 1
		}
		_sqlite3EndBenignMalloc(tls)
		_sqlite3VtabCommit(tls, db)
	}
	return rc
}

// C documentation
//
//	/*
//	** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
//	** of the data stored in aFile[1] is the same as that used by regular PMAs,
//	** except that the number-of-bytes varint is omitted from the start.
//	*/
func _vdbeIncrPopulate(tls *libc.TLS, pIncr uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var iEof, iStart Ti64
	var nKey, rc, rc2 int32
	var pMerger, pOut, pReader, pTask uintptr
	var _ /* dummy at bp+48 */ int32
	var _ /* writer at bp+0 */ TPmaWriter
	_, _, _, _, _, _, _, _, _ = iEof, iStart, nKey, pMerger, pOut, pReader, pTask, rc, rc2
	rc = SQLITE_OK
	iStart = (*TIncrMerger)(unsafe.Pointer(pIncr)).FiStartOff
	pOut = pIncr + 32 + 1*16
	pTask = (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask
	pMerger = (*TIncrMerger)(unsafe.Pointer(pIncr)).FpMerger
	_vdbePmaWriterInit(tls, (*TSorterFile)(unsafe.Pointer(pOut)).FpFd, bp, (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).Fpgsz, iStart)
	for rc == SQLITE_OK {
		pReader = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(**(**int32)(__ccgo_up((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + 1*4)))*56
		nKey = (*TPmaReader)(unsafe.Pointer(pReader)).FnKey
		iEof = (**(**TPmaWriter)(__ccgo_up(bp))).FiWriteOff + int64((**(**TPmaWriter)(__ccgo_up(bp))).FiBufEnd)
		/* Check if the output file is full or if the input has been exhausted.
		 ** In either case exit the loop. */
		if (*TPmaReader)(unsafe.Pointer(pReader)).FpFd == uintptr(0) {
			break
		}
		if iEof+int64(nKey)+int64(_sqlite3VarintLen(tls, uint64(nKey))) > iStart+int64((*TIncrMerger)(unsafe.Pointer(pIncr)).FmxSz) {
			break
		}
		/* Write the next key to the output. */
		_vdbePmaWriteVarint(tls, bp, uint64(nKey))
		_vdbePmaWriteBlob(tls, bp, (*TPmaReader)(unsafe.Pointer(pReader)).FaKey, nKey)
		rc = _vdbeMergeEngineStep(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpMerger, bp+48)
	}
	rc2 = _vdbePmaWriterFinish(tls, bp, pOut+8, pTask+80)
	if rc == SQLITE_OK {
		rc = rc2
	}
	return rc
}

// C documentation
//
//	/*
//	** This function compares the unpacked record with the current key that
//	** cursor pCur points to. If bInt is false, all fields for which the
//	** corresponding bit in parameter "mask" is set are ignored. Or, if
//	** bInt is true, then a difference of BTREE_ULPDISTORTION or fewer ULPs
//	** in real values is overlooked for fields with the corresponding bit
//	** set in mask.
//	**
//	** Return the usual less than zero, zero, or greater than zero if the
//	** remaining fields of the cursor cursor key are less than, equal to or
//	** greater than those in (*p).
//	*/
func _vdbeIsMatchingIndexKey(tls *libc.TLS, pCur uintptr, bInt int32, mask TBitmask, p uintptr, piRes uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var aRec uintptr
	var idxHdr, idxRec, nRec Tu32
	var ii, nCol, nSerial, rc, res, v1 int32
	var _ /* iSerial at bp+44 */ Tu32
	var _ /* mem at bp+0 */ TMem
	var _ /* szHdr at bp+40 */ Tu32
	_, _, _, _, _, _, _, _, _, _ = aRec, idxHdr, idxRec, ii, nCol, nRec, nSerial, rc, res, v1
	aRec = uintptr(0)
	nRec = uint32(0)
	rc = SQLITE_OK
	libc.Xmemset(tls, bp, 0, uint32(40))
	(**(**TMem)(__ccgo_up(bp))).Fenc = (*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo)).Fenc
	(**(**TMem)(__ccgo_up(bp))).Fdb = (*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo)).Fdb
	nRec = _sqlite3BtreePayloadSize(tls, pCur)
	if nRec > uint32(0x7fffffff) {
		return _sqlite3CorruptError(tls, int32(93336))
	}
	/* Allocate 5 extra bytes at the end of the buffer. This allows the
	 ** getVarint32() call below to read slightly past the end of the buffer
	 ** if the record is corrupt. */
	aRec = _sqlite3MallocZero(tls, uint64(nRec+uint32(5)))
	if aRec == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
	} else {
		rc = _sqlite3BtreePayload(tls, pCur, uint32(0), nRec, aRec)
	}
	if rc == SQLITE_OK {
		**(**Tu32)(__ccgo_up(bp + 40)) = uint32(0) /* Size of record header in bytes */
		idxHdr = uint32(0)                         /* Current index in header */
		if int32(**(**Tu8)(__ccgo_up(aRec))) < int32(libc.Uint8FromInt32(0x80)) {
			**(**Tu32)(__ccgo_up(bp + 40)) = uint32(**(**Tu8)(__ccgo_up(aRec)))
			v1 = libc.Int32FromInt32(1)
		} else {
			v1 = int32(_sqlite3GetVarint32(tls, aRec, bp+40))
		}
		idxHdr = uint32(uint8(v1))
		if **(**Tu32)(__ccgo_up(bp + 40)) > uint32(98307) {
			rc = int32(SQLITE_CORRUPT)
		} else {
			res = 0                                 /* Result of this function call */
			idxRec = **(**Tu32)(__ccgo_up(bp + 40)) /* Index of next field in record body */
			ii = 0                                  /* Iterator variable */
			nCol = int32((*TKeyInfo)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo)).FnAllField)
			ii = 0
			for {
				if !(ii < nCol && rc == SQLITE_OK) {
					break
				}
				**(**Tu32)(__ccgo_up(bp + 44)) = uint32(0)
				nSerial = 0
				if idxHdr >= **(**Tu32)(__ccgo_up(bp + 40)) {
					rc = _sqlite3CorruptError(tls, int32(93367))
					break
				}
				if int32(**(**Tu8)(__ccgo_up(aRec + uintptr(idxHdr)))) < int32(libc.Uint8FromInt32(0x80)) {
					**(**Tu32)(__ccgo_up(bp + 44)) = uint32(**(**Tu8)(__ccgo_up(aRec + uintptr(idxHdr))))
					v1 = libc.Int32FromInt32(1)
				} else {
					v1 = int32(_sqlite3GetVarint32(tls, aRec+uintptr(idxHdr), bp+44))
				}
				idxHdr = idxHdr + uint32(uint8(v1))
				nSerial = int32(_sqlite3VdbeSerialTypeLen(tls, **(**Tu32)(__ccgo_up(bp + 44))))
				if idxRec+uint32(nSerial) > nRec {
					rc = _sqlite3CorruptError(tls, int32(93373))
				} else {
					_sqlite3VdbeSerialGet(tls, aRec+uintptr(idxRec), **(**Tu32)(__ccgo_up(bp + 44)), bp)
					if _vdbeSkipField(tls, mask, ii, (*TUnpackedRecord)(unsafe.Pointer(p)).FaMem+uintptr(ii)*40, bp, bInt) == 0 {
						res = _sqlite3MemCompare(tls, bp, (*TUnpackedRecord)(unsafe.Pointer(p)).FaMem+uintptr(ii)*40, *(*uintptr)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(p)).FpKeyInfo + 20 + uintptr(ii)*4)))
						if res != 0 {
							break
						}
					}
				}
				idxRec = idxRec + _sqlite3VdbeSerialTypeLen(tls, **(**Tu32)(__ccgo_up(bp + 44)))
				goto _2
			_2:
				;
				ii = ii + 1
			}
			**(**int32)(__ccgo_up(piRes)) = res
		}
	}
	Xsqlite3_free(tls, aRec)
	return rc
}

// C documentation
//
//	/*
//	** If the memory cell contains a value that must be freed by
//	** invoking the external callback in Mem.xDel, then this routine
//	** will free that value.  It also sets Mem.flags to MEM_Null.
//	**
//	** This is a helper routine for sqlite3VdbeMemSetNull() and
//	** for sqlite3VdbeMemRelease().  Use those other routines as the
//	** entry point for releasing Mem resources.
//	*/
func _vdbeMemClearExternAndSetNull(tls *libc.TLS, p uintptr) {
	if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Agg) != 0 {
		_sqlite3VdbeMemFinalize(tls, p, *(*uintptr)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(p)).Fu)))
	}
	if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Dyn) != 0 {
		(*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TMem)(unsafe.Pointer(p)).FxDel})))(tls, (*TMem)(unsafe.Pointer(p)).Fz)
	}
	(*TMem)(unsafe.Pointer(p)).Fflags = uint16(MEM_Null)
}

// C documentation
//
//	/*
//	** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
//	** into a buffer.
//	*/
func _vdbeMemRenderNum(tls *libc.TLS, sz int32, zBuf uintptr, p uintptr) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var v1 int32
	var _ /* acc at bp+0 */ TStrAccum
	_ = v1
	if int32((*TMem)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(MEM_Int)|libc.Int32FromInt32(MEM_IntReal)) != 0 {
		(*TMem)(unsafe.Pointer(p)).Fn = _sqlite3Int64ToText(tls, *(*Ti64)(unsafe.Pointer(p)), zBuf)
		if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_IntReal) != 0 {
			libc.Xmemcpy(tls, zBuf+uintptr((*TMem)(unsafe.Pointer(p)).Fn), __ccgo_ts+6433, uint32(3))
			**(**int32)(__ccgo_up(p + 12)) += int32(2)
		}
	} else {
		_sqlite3StrAccumInit(tls, bp, uintptr(0), zBuf, sz, 0)
		if (*TMem)(unsafe.Pointer(p)).Fdb != 0 {
			v1 = int32((*Tsqlite3)(unsafe.Pointer((*TMem)(unsafe.Pointer(p)).Fdb)).FnFpDigit)
		} else {
			v1 = int32(17)
		}
		Xsqlite3_str_appendf(tls, bp, __ccgo_ts+6436, libc.VaList(bp+32, v1, *(*float64)(unsafe.Pointer(p))))
		**(**int8)(__ccgo_up(zBuf + uintptr((**(**TStrAccum)(__ccgo_up(bp))).FnChar))) = 0 /* Fast version of sqlite3StrAccumFinish(&acc) */
		(*TMem)(unsafe.Pointer(p)).Fn = int32((**(**TStrAccum)(__ccgo_up(bp))).FnChar)
	}
}

// C documentation
//
//	/*
//	** Initialize the MergeEngine object passed as the second argument. Once this
//	** function returns, the first key of merged data may be read from the
//	** MergeEngine object in the usual fashion.
//	**
//	** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
//	** objects attached to the PmaReader objects that the merger reads from have
//	** already been populated, but that they have not yet populated aFile[0] and
//	** set the PmaReader objects up to read from it. In this case all that is
//	** required is to call vdbePmaReaderNext() on each PmaReader to point it at
//	** its first key.
//	**
//	** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
//	** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
//	** to pMerger.
//	**
//	** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
//	*/
func _vdbeMergeEngineInit(tls *libc.TLS, pTask uintptr, pMerger uintptr, eMode int32) (r int32) {
	var i, nTree, rc int32
	_, _, _ = i, nTree, rc
	rc = SQLITE_OK /* Number of subtrees to merge */
	/* Failure to allocate the merge would have been detected prior to
	 ** invoking this routine */
	/* eMode is always INCRINIT_NORMAL in single-threaded mode */
	/* Verify that the MergeEngine is assigned to a single thread */
	(*TMergeEngine)(unsafe.Pointer(pMerger)).FpTask = pTask
	nTree = (*TMergeEngine)(unsafe.Pointer(pMerger)).FnTree
	i = 0
	for {
		if !(i < nTree) {
			break
		}
		if libc.Bool(int32(SQLITE_MAX_WORKER_THREADS) > 0) && eMode == int32(INCRINIT_ROOT) {
			/* PmaReaders should be normally initialized in order, as if they are
			 ** reading from the same temp file this makes for more linear file IO.
			 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
			 ** in use it will block the vdbePmaReaderNext() call while it uses
			 ** the main thread to fill its buffer. So calling PmaReaderNext()
			 ** on this PmaReader before any of the multi-threaded PmaReaders takes
			 ** better advantage of multi-processor hardware. */
			rc = _vdbePmaReaderNext(tls, (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr+uintptr(nTree-i-int32(1))*56)
		} else {
			rc = _vdbePmaReaderIncrInit(tls, (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr+uintptr(i)*56, INCRINIT_NORMAL)
		}
		if rc != SQLITE_OK {
			return rc
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	i = (*TMergeEngine)(unsafe.Pointer(pMerger)).FnTree - int32(1)
	for {
		if !(i > 0) {
			break
		}
		_vdbeMergeEngineCompare(tls, pMerger, i)
		goto _2
	_2:
		;
		i = i - 1
	}
	return int32((*TUnpackedRecord)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked)).FerrCode)
}

// C documentation
//
//	/*
//	** Allocate a new MergeEngine object capable of handling up to
//	** nReader PmaReader inputs.
//	**
//	** nReader is automatically rounded up to the next power of two.
//	** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
//	*/
func _vdbeMergeEngineNew(tls *libc.TLS, nReader int32) (r uintptr) {
	var N int32
	var nByte Ti64
	var pNew, v1 uintptr
	_, _, _, _ = N, nByte, pNew, v1
	N = int32(2) /* Pointer to allocated object to return */
	for N < nReader {
		N = N + N
	}
	nByte = int64(uint32(16) + uint32(N)*(libc.Uint32FromInt64(4)+libc.Uint32FromInt64(56)))
	if _sqlite3FaultSim(tls, int32(100)) != 0 {
		v1 = uintptr(0)
	} else {
		v1 = _sqlite3MallocZero(tls, uint64(nByte))
	}
	pNew = v1
	if pNew != 0 {
		(*TMergeEngine)(unsafe.Pointer(pNew)).FnTree = N
		(*TMergeEngine)(unsafe.Pointer(pNew)).FpTask = uintptr(0)
		(*TMergeEngine)(unsafe.Pointer(pNew)).FaReadr = pNew + 1*16
		(*TMergeEngine)(unsafe.Pointer(pNew)).FaTree = (*TMergeEngine)(unsafe.Pointer(pNew)).FaReadr + uintptr(N)*56
	}
	return pNew
}

// C documentation
//
//	/*
//	** Advance the MergeEngine to its next entry.
//	** Set *pbEof to true there is no next entry because
//	** the MergeEngine has reached the end of all its inputs.
//	**
//	** Return SQLITE_OK if successful or an error code if an error occurs.
//	*/
func _vdbeMergeEngineStep(tls *libc.TLS, pMerger uintptr, pbEof uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, iPrev, iRes, rc, v2 int32
	var pReadr1, pReadr2, pTask uintptr
	var _ /* bCached at bp+0 */ int32
	_, _, _, _, _, _, _, _ = i, iPrev, iRes, pReadr1, pReadr2, pTask, rc, v2
	iPrev = **(**int32)(__ccgo_up((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + 1*4)) /* Index of PmaReader to advance */
	pTask = (*TMergeEngine)(unsafe.Pointer(pMerger)).FpTask
	/* Advance the current PmaReader */
	rc = _vdbePmaReaderNext(tls, (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr+uintptr(iPrev)*56)
	/* Update contents of aTree[] */
	if rc == SQLITE_OK { /* Second PmaReader to compare */
		**(**int32)(__ccgo_up(bp)) = 0
		/* Find the first two PmaReaders to compare. The one that was just
		 ** advanced (iPrev) and the one next to it in the array.  */
		pReadr1 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(iPrev&libc.Int32FromInt32(0xFFFE))*56
		pReadr2 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(iPrev|libc.Int32FromInt32(0x0001))*56
		i = ((*TMergeEngine)(unsafe.Pointer(pMerger)).FnTree + iPrev) / int32(2)
		for {
			if !(i > 0) {
				break
			}
			if (*TPmaReader)(unsafe.Pointer(pReadr1)).FpFd == uintptr(0) {
				iRes = +libc.Int32FromInt32(1)
			} else {
				if (*TPmaReader)(unsafe.Pointer(pReadr2)).FpFd == uintptr(0) {
					iRes = -int32(1)
				} else {
					iRes = (*(*func(*libc.TLS, uintptr, uintptr, uintptr, int32, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*TSortSubtask)(unsafe.Pointer(pTask)).FxCompare})))(tls, pTask, bp, (*TPmaReader)(unsafe.Pointer(pReadr1)).FaKey, (*TPmaReader)(unsafe.Pointer(pReadr1)).FnKey, (*TPmaReader)(unsafe.Pointer(pReadr2)).FaKey, (*TPmaReader)(unsafe.Pointer(pReadr2)).FnKey)
				}
			}
			/* If pReadr1 contained the smaller value, set aTree[i] to its index.
			 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
			 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
			 ** pKey2 to point to the record belonging to pReadr2.
			 **
			 ** Alternatively, if pReadr2 contains the smaller of the two values,
			 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
			 ** was actually called above, then pTask->pUnpacked now contains
			 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
			 ** vdbeSorterCompare() from decoding pReadr2 again.
			 **
			 ** If the two values were equal, then the value from the oldest
			 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
			 ** is sorted from oldest to newest, so pReadr1 contains older values
			 ** than pReadr2 iff (pReadr1<pReadr2).  */
			if iRes < 0 || iRes == 0 && pReadr1 < pReadr2 {
				**(**int32)(__ccgo_up((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + uintptr(i)*4)) = (int32(pReadr1) - int32((*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr)) / 56
				pReadr2 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(**(**int32)(__ccgo_up((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + uintptr(i^int32(0x0001))*4)))*56
				**(**int32)(__ccgo_up(bp)) = 0
			} else {
				if (*TPmaReader)(unsafe.Pointer(pReadr1)).FpFd != 0 {
					**(**int32)(__ccgo_up(bp)) = 0
				}
				**(**int32)(__ccgo_up((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + uintptr(i)*4)) = (int32(pReadr2) - int32((*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr)) / 56
				pReadr1 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(**(**int32)(__ccgo_up((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + uintptr(i^int32(0x0001))*4)))*56
			}
			goto _1
		_1:
			;
			i = i / int32(2)
		}
		**(**int32)(__ccgo_up(pbEof)) = libc.BoolInt32((**(**TPmaReader)(__ccgo_up((*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(**(**int32)(__ccgo_up((*TMergeEngine)(unsafe.Pointer(pMerger)).FaTree + 1*4)))*56))).FpFd == uintptr(0))
	}
	if rc == SQLITE_OK {
		v2 = int32((*TUnpackedRecord)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked)).FerrCode)
	} else {
		v2 = rc
	}
	return v2
}

// C documentation
//
//	/*
//	** Read the next nByte bytes of data from the PMA p.
//	** If successful, set *ppOut to point to a buffer containing the data
//	** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
//	** error code.
//	**
//	** The buffer returned in *ppOut is only valid until the
//	** next call to this function.
//	*/
func _vdbePmaReadBlob(tls *libc.TLS, p uintptr, nByte int32, ppOut uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aNew uintptr
	var iBuf, nAvail, nCopy, nRead, nRem, rc, rc1 int32
	var nNew Tsqlite3_int64
	var v1 int64
	var _ /* aNext at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _ = aNew, iBuf, nAvail, nCopy, nNew, nRead, nRem, rc, rc1, v1 /* Bytes of data available in buffer */
	if (*TPmaReader)(unsafe.Pointer(p)).FaMap != 0 {
		**(**uintptr)(__ccgo_up(ppOut)) = (*TPmaReader)(unsafe.Pointer(p)).FaMap + uintptr((*TPmaReader)(unsafe.Pointer(p)).FiReadOff)
		**(**Ti64)(__ccgo_up(p)) += int64(nByte)
		return SQLITE_OK
	}
	/* If there is no more data to be read from the buffer, read the next
	 ** p->nBuffer bytes of data from the file into it. Or, if there are less
	 ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
	iBuf = int32((*TPmaReader)(unsafe.Pointer(p)).FiReadOff % int64((*TPmaReader)(unsafe.Pointer(p)).FnBuffer))
	if iBuf == 0 { /* sqlite3OsRead() return code */
		/* Determine how many bytes of data to read. */
		if (*TPmaReader)(unsafe.Pointer(p)).FiEof-(*TPmaReader)(unsafe.Pointer(p)).FiReadOff > int64((*TPmaReader)(unsafe.Pointer(p)).FnBuffer) {
			nRead = (*TPmaReader)(unsafe.Pointer(p)).FnBuffer
		} else {
			nRead = int32((*TPmaReader)(unsafe.Pointer(p)).FiEof - (*TPmaReader)(unsafe.Pointer(p)).FiReadOff)
		}
		/* Readr data from the file. Return early if an error occurs. */
		rc = _sqlite3OsRead(tls, (*TPmaReader)(unsafe.Pointer(p)).FpFd, (*TPmaReader)(unsafe.Pointer(p)).FaBuffer, nRead, (*TPmaReader)(unsafe.Pointer(p)).FiReadOff)
		if rc != SQLITE_OK {
			return rc
		}
	}
	nAvail = (*TPmaReader)(unsafe.Pointer(p)).FnBuffer - iBuf
	if nByte <= nAvail {
		/* The requested data is available in the in-memory buffer. In this
		 ** case there is no need to make a copy of the data, just return a
		 ** pointer into the buffer to the caller.  */
		**(**uintptr)(__ccgo_up(ppOut)) = (*TPmaReader)(unsafe.Pointer(p)).FaBuffer + uintptr(iBuf)
		**(**Ti64)(__ccgo_up(p)) += int64(nByte)
	} else { /* Bytes remaining to copy */
		/* Extend the p->aAlloc[] allocation if required. */
		if (*TPmaReader)(unsafe.Pointer(p)).FnAlloc < nByte {
			if int64(libc.Int32FromInt32(128)) > int64(2)*int64((*TPmaReader)(unsafe.Pointer(p)).FnAlloc) {
				v1 = int64(libc.Int32FromInt32(128))
			} else {
				v1 = int64(2) * int64((*TPmaReader)(unsafe.Pointer(p)).FnAlloc)
			}
			nNew = v1
			for int64(nByte) > nNew {
				nNew = nNew * int64(2)
			}
			aNew = _sqlite3Realloc(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc, uint64(nNew))
			if !(aNew != 0) {
				return int32(SQLITE_NOMEM)
			}
			(*TPmaReader)(unsafe.Pointer(p)).FnAlloc = int32(nNew)
			(*TPmaReader)(unsafe.Pointer(p)).FaAlloc = aNew
		}
		/* Copy as much data as is available in the buffer into the start of
		 ** p->aAlloc[].  */
		libc.Xmemcpy(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc, (*TPmaReader)(unsafe.Pointer(p)).FaBuffer+uintptr(iBuf), uint32(nAvail))
		**(**Ti64)(__ccgo_up(p)) += int64(nAvail)
		nRem = nByte - nAvail
		/* The following loop copies up to p->nBuffer bytes per iteration into
		 ** the p->aAlloc[] buffer.  */
		for nRem > 0 { /* Number of bytes to copy */
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* Pointer to buffer to copy data from */
			nCopy = nRem
			if nRem > (*TPmaReader)(unsafe.Pointer(p)).FnBuffer {
				nCopy = (*TPmaReader)(unsafe.Pointer(p)).FnBuffer
			}
			rc1 = _vdbePmaReadBlob(tls, p, nCopy, bp)
			if rc1 != SQLITE_OK {
				return rc1
			}
			libc.Xmemcpy(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc+uintptr(nByte-nRem), **(**uintptr)(__ccgo_up(bp)), uint32(nCopy))
			nRem = nRem - nCopy
		}
		**(**uintptr)(__ccgo_up(ppOut)) = (*TPmaReader)(unsafe.Pointer(p)).FaAlloc
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
//	** no error occurs, or an SQLite error code if one does.
//	*/
func _vdbePmaReaderNext(tls *libc.TLS, pReadr uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bEof, rc int32
	var pIncr uintptr
	var _ /* nRec at bp+0 */ Tu64
	_, _, _ = bEof, pIncr, rc
	rc = SQLITE_OK                        /* Return Code */
	**(**Tu64)(__ccgo_up(bp)) = uint64(0) /* Size of record in bytes */
	if (*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff >= (*TPmaReader)(unsafe.Pointer(pReadr)).FiEof {
		pIncr = (*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr
		bEof = int32(1)
		if pIncr != 0 {
			rc = _vdbeIncrSwap(tls, pIncr)
			if rc == SQLITE_OK && (*TIncrMerger)(unsafe.Pointer(pIncr)).FbEof == 0 {
				rc = _vdbePmaReaderSeek(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask, pReadr, pIncr+32, (*TIncrMerger)(unsafe.Pointer(pIncr)).FiStartOff)
				bEof = 0
			}
		}
		if bEof != 0 {
			/* This is an EOF condition */
			_vdbePmaReaderClear(tls, pReadr)
			return rc
		}
	}
	if rc == SQLITE_OK {
		rc = _vdbePmaReadVarint(tls, pReadr, bp)
	}
	if rc == SQLITE_OK {
		(*TPmaReader)(unsafe.Pointer(pReadr)).FnKey = int32(**(**Tu64)(__ccgo_up(bp)))
		rc = _vdbePmaReadBlob(tls, pReadr, int32(**(**Tu64)(__ccgo_up(bp))), pReadr+32)
	}
	return rc
}

// C documentation
//
//	/*
//	** Attach PmaReader pReadr to file pFile (if it is not already attached to
//	** that file) and seek it to offset iOff within the file.  Return SQLITE_OK
//	** if successful, or an SQLite error code if an error occurs.
//	*/
func _vdbePmaReaderSeek(tls *libc.TLS, pTask uintptr, pReadr uintptr, pFile uintptr, iOff Ti64) (r int32) {
	var iBuf, nRead, pgsz, rc int32
	_, _, _, _ = iBuf, nRead, pgsz, rc
	rc = SQLITE_OK
	if _sqlite3FaultSim(tls, int32(201)) != 0 {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
	}
	if (*TPmaReader)(unsafe.Pointer(pReadr)).FaMap != 0 {
		_sqlite3OsUnfetch(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FpFd, 0, (*TPmaReader)(unsafe.Pointer(pReadr)).FaMap)
		(*TPmaReader)(unsafe.Pointer(pReadr)).FaMap = uintptr(0)
	}
	(*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff = iOff
	(*TPmaReader)(unsafe.Pointer(pReadr)).FiEof = (*TSorterFile)(unsafe.Pointer(pFile)).FiEof
	(*TPmaReader)(unsafe.Pointer(pReadr)).FpFd = (*TSorterFile)(unsafe.Pointer(pFile)).FpFd
	rc = _vdbeSorterMapFile(tls, pTask, pFile, pReadr+44)
	if rc == SQLITE_OK && (*TPmaReader)(unsafe.Pointer(pReadr)).FaMap == uintptr(0) {
		pgsz = (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).Fpgsz
		iBuf = int32((*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff % int64(pgsz))
		if (*TPmaReader)(unsafe.Pointer(pReadr)).FaBuffer == uintptr(0) {
			(*TPmaReader)(unsafe.Pointer(pReadr)).FaBuffer = _sqlite3Malloc(tls, uint64(pgsz))
			if (*TPmaReader)(unsafe.Pointer(pReadr)).FaBuffer == uintptr(0) {
				rc = int32(SQLITE_NOMEM)
			}
			(*TPmaReader)(unsafe.Pointer(pReadr)).FnBuffer = pgsz
		}
		if rc == SQLITE_OK && iBuf != 0 {
			nRead = pgsz - iBuf
			if (*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff+int64(nRead) > (*TPmaReader)(unsafe.Pointer(pReadr)).FiEof {
				nRead = int32((*TPmaReader)(unsafe.Pointer(pReadr)).FiEof - (*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff)
			}
			rc = _sqlite3OsRead(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FpFd, (*TPmaReader)(unsafe.Pointer(pReadr)).FaBuffer+uintptr(iBuf), nRead, (*TPmaReader)(unsafe.Pointer(pReadr)).FiReadOff)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Write nData bytes of data to the PMA. Return SQLITE_OK
//	** if successful, or an SQLite error code if an error occurs.
//	*/
func _vdbePmaWriteBlob(tls *libc.TLS, p uintptr, pData uintptr, nData int32) {
	var nCopy, nRem, v1 int32
	_, _, _ = nCopy, nRem, v1
	nRem = nData
	for nRem > 0 && (*TPmaWriter)(unsafe.Pointer(p)).FeFWErr == 0 {
		nCopy = nRem
		if nCopy > (*TPmaWriter)(unsafe.Pointer(p)).FnBuffer-(*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd {
			nCopy = (*TPmaWriter)(unsafe.Pointer(p)).FnBuffer - (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd
		}
		libc.Xmemcpy(tls, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer+uintptr((*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd), pData+uintptr(nData-nRem), uint32(nCopy))
		**(**int32)(__ccgo_up(p + 16)) += nCopy
		if (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd == (*TPmaWriter)(unsafe.Pointer(p)).FnBuffer {
			(*TPmaWriter)(unsafe.Pointer(p)).FeFWErr = _sqlite3OsWrite(tls, (*TPmaWriter)(unsafe.Pointer(p)).FpFd, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer+uintptr((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart), (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd-(*TPmaWriter)(unsafe.Pointer(p)).FiBufStart, (*TPmaWriter)(unsafe.Pointer(p)).FiWriteOff+int64((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart))
			**(**Tu64)(__ccgo_up(p + 40)) += uint64((*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd - (*TPmaWriter)(unsafe.Pointer(p)).FiBufStart)
			v1 = libc.Int32FromInt32(0)
			(*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd = v1
			(*TPmaWriter)(unsafe.Pointer(p)).FiBufStart = v1
			**(**Ti64)(__ccgo_up(p + 24)) += int64((*TPmaWriter)(unsafe.Pointer(p)).FnBuffer)
		}
		nRem = nRem - nCopy
	}
}

// C documentation
//
//	/*
//	** Flush any buffered data to disk and clean up the PMA-writer object.
//	** The results of using the PMA-writer after this call are undefined.
//	** Return SQLITE_OK if flushing the buffered data succeeds or is not
//	** required. Otherwise, return an SQLite error code.
//	**
//	** Before returning, set *piEof to the offset immediately following the
//	** last byte written to the file. Also, increment (*pnSpill) by the total
//	** number of bytes written to the file.
//	*/
func _vdbePmaWriterFinish(tls *libc.TLS, p uintptr, piEof uintptr, pnSpill uintptr) (r int32) {
	var rc int32
	_ = rc
	if (*TPmaWriter)(unsafe.Pointer(p)).FeFWErr == 0 && (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer != 0 && (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd > (*TPmaWriter)(unsafe.Pointer(p)).FiBufStart {
		(*TPmaWriter)(unsafe.Pointer(p)).FeFWErr = _sqlite3OsWrite(tls, (*TPmaWriter)(unsafe.Pointer(p)).FpFd, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer+uintptr((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart), (*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd-(*TPmaWriter)(unsafe.Pointer(p)).FiBufStart, (*TPmaWriter)(unsafe.Pointer(p)).FiWriteOff+int64((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart))
		**(**Tu64)(__ccgo_up(p + 40)) += uint64((*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd - (*TPmaWriter)(unsafe.Pointer(p)).FiBufStart)
	}
	**(**Ti64)(__ccgo_up(piEof)) = (*TPmaWriter)(unsafe.Pointer(p)).FiWriteOff + int64((*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd)
	**(**Tu64)(__ccgo_up(pnSpill)) += (*TPmaWriter)(unsafe.Pointer(p)).FnPmaSpill
	Xsqlite3_free(tls, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer)
	rc = (*TPmaWriter)(unsafe.Pointer(p)).FeFWErr
	libc.Xmemset(tls, p, 0, uint32(48))
	return rc
}

// C documentation
//
//	/*
//	** Initialize a PMA-writer object.
//	*/
func _vdbePmaWriterInit(tls *libc.TLS, pFd uintptr, p uintptr, nBuf int32, iStart Ti64) {
	var v1 int32
	_ = v1
	libc.Xmemset(tls, p, 0, uint32(48))
	(*TPmaWriter)(unsafe.Pointer(p)).FaBuffer = _sqlite3Malloc(tls, uint64(nBuf))
	if !((*TPmaWriter)(unsafe.Pointer(p)).FaBuffer != 0) {
		(*TPmaWriter)(unsafe.Pointer(p)).FeFWErr = int32(SQLITE_NOMEM)
	} else {
		v1 = int32(iStart % int64(nBuf))
		(*TPmaWriter)(unsafe.Pointer(p)).FiBufStart = v1
		(*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd = v1
		(*TPmaWriter)(unsafe.Pointer(p)).FiWriteOff = iStart - int64((*TPmaWriter)(unsafe.Pointer(p)).FiBufStart)
		(*TPmaWriter)(unsafe.Pointer(p)).FnBuffer = nBuf
		(*TPmaWriter)(unsafe.Pointer(p)).FpFd = pFd
	}
}

// C documentation
//
//	/*
//	** This function is an optimized version of sqlite3VdbeRecordCompare()
//	** that (a) the first field of pPKey2 is an integer, and (b) the
//	** size-of-header varint at the start of (pKey1/nKey1) fits in a single
//	** byte (i.e. is less than 128).
//	**
//	** To avoid concerns about buffer overreads, this routine is only used
//	** on schemas where the maximum valid header size is 63 bytes or less.
//	*/
func _vdbeRecordCompareInt(tls *libc.TLS, nKey1 int32, pKey1 uintptr, pPKey2 uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aKey uintptr
	var lhs, v Ti64
	var res, serial_type int32
	var _ /* x at bp+8 */ Tu64
	var _ /* y at bp+0 */ Tu32
	_, _, _, _, _ = aKey, lhs, res, serial_type, v
	aKey = pKey1 + uintptr(int32(**(**Tu8)(__ccgo_up(pKey1)))&int32(0x3F))
	serial_type = int32(**(**Tu8)(__ccgo_up(pKey1 + 1)))
	switch serial_type {
	case int32(1): /* 1-byte signed integer */
		lhs = int64(int8(**(**Tu8)(__ccgo_up(aKey))))
	case int32(2): /* 2-byte signed integer */
		lhs = int64(libc.Int32FromInt32(256)*int32(int8(**(**Tu8)(__ccgo_up(aKey)))) | int32(**(**Tu8)(__ccgo_up(aKey + 1))))
	case int32(3): /* 3-byte signed integer */
		lhs = int64(libc.Int32FromInt32(65536)*int32(int8(**(**Tu8)(__ccgo_up(aKey)))) | int32(**(**Tu8)(__ccgo_up(aKey + 1)))<<libc.Int32FromInt32(8) | int32(**(**Tu8)(__ccgo_up(aKey + 2))))
	case int32(4): /* 4-byte signed integer */
		**(**Tu32)(__ccgo_up(bp)) = uint32(**(**Tu8)(__ccgo_up(aKey)))<<libc.Int32FromInt32(24) | uint32(int32(**(**Tu8)(__ccgo_up(aKey + 1)))<<libc.Int32FromInt32(16)) | uint32(int32(**(**Tu8)(__ccgo_up(aKey + 2)))<<libc.Int32FromInt32(8)) | uint32(**(**Tu8)(__ccgo_up(aKey + 3)))
		lhs = int64(**(**int32)(__ccgo_up(bp)))
	case int32(5): /* 6-byte signed integer */
		lhs = int64(uint32(**(**Tu8)(__ccgo_up(aKey + libc.UintptrFromInt32(2))))<<libc.Int32FromInt32(24)|uint32(int32(**(**Tu8)(__ccgo_up(aKey + libc.UintptrFromInt32(2) + 1)))<<libc.Int32FromInt32(16))|uint32(int32(**(**Tu8)(__ccgo_up(aKey + libc.UintptrFromInt32(2) + 2)))<<libc.Int32FromInt32(8))|uint32(**(**Tu8)(__ccgo_up(aKey + libc.UintptrFromInt32(2) + 3)))) + libc.Int64FromInt32(1)<<libc.Int32FromInt32(32)*int64(libc.Int32FromInt32(256)*int32(int8(**(**Tu8)(__ccgo_up(aKey))))|int32(**(**Tu8)(__ccgo_up(aKey + 1))))
	case int32(6): /* 8-byte signed integer */
		**(**Tu64)(__ccgo_up(bp + 8)) = uint64(uint32(**(**Tu8)(__ccgo_up(aKey)))<<libc.Int32FromInt32(24) | uint32(int32(**(**Tu8)(__ccgo_up(aKey + 1)))<<libc.Int32FromInt32(16)) | uint32(int32(**(**Tu8)(__ccgo_up(aKey + 2)))<<libc.Int32FromInt32(8)) | uint32(**(**Tu8)(__ccgo_up(aKey + 3))))
		**(**Tu64)(__ccgo_up(bp + 8)) = **(**Tu64)(__ccgo_up(bp + 8))<<libc.Int32FromInt32(32) | uint64(uint32(**(**Tu8)(__ccgo_up(aKey + libc.UintptrFromInt32(4))))<<libc.Int32FromInt32(24)|uint32(int32(**(**Tu8)(__ccgo_up(aKey + libc.UintptrFromInt32(4) + 1)))<<libc.Int32FromInt32(16))|uint32(int32(**(**Tu8)(__ccgo_up(aKey + libc.UintptrFromInt32(4) + 2)))<<libc.Int32FromInt32(8))|uint32(**(**Tu8)(__ccgo_up(aKey + libc.UintptrFromInt32(4) + 3))))
		lhs = **(**Ti64)(__ccgo_up(bp + 8))
	case int32(8):
		lhs = 0
	case int32(9):
		lhs = int64(1)
		break
		/* This case could be removed without changing the results of running
		 ** this code. Including it causes gcc to generate a faster switch
		 ** statement (since the range of switch targets now starts at zero and
		 ** is contiguous) but does not cause any duplicate code to be generated
		 ** (as gcc is clever enough to combine the two like cases). Other
		 ** compilers might be similar.  */
		fallthrough
	case 0:
		fallthrough
	case int32(7):
		return _sqlite3VdbeRecordCompare(tls, nKey1, pKey1, pPKey2)
	default:
		return _sqlite3VdbeRecordCompare(tls, nKey1, pKey1, pPKey2)
	}
	v = *(*Ti64)(unsafe.Pointer(pPKey2 + 8))
	if v > lhs {
		res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr1)
	} else {
		if v < lhs {
			res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr2)
		} else {
			if int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FnField) > int32(1) {
				/* The first fields of the two keys are equal. Compare the trailing
				 ** fields.  */
				res = _sqlite3VdbeRecordCompareWithSkip(tls, nKey1, pKey1, pPKey2, int32(1))
			} else {
				/* The first fields of the two keys are equal and there are no trailing
				 ** fields. Return pPKey2->default_rc in this case. */
				res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fdefault_rc)
				(*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FeqSeen = uint8(1)
			}
		}
	}
	return res
}

// C documentation
//
//	/*
//	** This function is an optimized version of sqlite3VdbeRecordCompare()
//	** that (a) the first field of pPKey2 is a string, that (b) the first field
//	** uses the collation sequence BINARY and (c) that the size-of-header varint
//	** at the start of (pKey1/nKey1) fits in a single byte.
//	*/
func _vdbeRecordCompareString(tls *libc.TLS, nKey1 int32, pKey1 uintptr, pPKey2 uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aKey1 uintptr
	var nCmp, nStr, res, szHdr, v1 int32
	var _ /* serial_type at bp+0 */ int32
	_, _, _, _, _, _ = aKey1, nCmp, nStr, res, szHdr, v1
	aKey1 = pKey1
	**(**int32)(__ccgo_up(bp)) = int32(int8(**(**Tu8)(__ccgo_up(aKey1 + 1))))
	goto vrcs_restart
vrcs_restart:
	;
	if **(**int32)(__ccgo_up(bp)) < int32(12) {
		if **(**int32)(__ccgo_up(bp)) < 0 {
			_sqlite3GetVarint32(tls, aKey1+1, bp)
			if **(**int32)(__ccgo_up(bp)) >= int32(12) {
				goto vrcs_restart
			}
		}
		res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr1) /* (pKey1/nKey1) is a number or a null */
	} else {
		if !(**(**int32)(__ccgo_up(bp))&libc.Int32FromInt32(0x01) != 0) {
			res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr2) /* (pKey1/nKey1) is a blob */
		} else {
			szHdr = int32(**(**Tu8)(__ccgo_up(aKey1)))
			nStr = (**(**int32)(__ccgo_up(bp)) - int32(12)) / int32(2)
			if szHdr+nStr > nKey1 {
				(*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(92972)))
				return 0 /* Corruption */
			}
			if (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fn < nStr {
				v1 = (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fn
			} else {
				v1 = nStr
			}
			nCmp = v1
			res = libc.Xmemcmp(tls, aKey1+uintptr(szHdr), (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fu.Fz, uint32(nCmp))
			if res > 0 {
				res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr2)
			} else {
				if res < 0 {
					res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr1)
				} else {
					res = nStr - (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fn
					if res == 0 {
						if int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FnField) > int32(1) {
							res = _sqlite3VdbeRecordCompareWithSkip(tls, nKey1, pKey1, pPKey2, int32(1))
						} else {
							res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fdefault_rc)
							(*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FeqSeen = uint8(1)
						}
					} else {
						if res > 0 {
							res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr2)
						} else {
							res = int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).Fr1)
						}
					}
				}
			}
		}
	}
	return res
}

// C documentation
//
//	/*
//	** Helper function for vdbeIsMatchingIndexKey(). Return true if column
//	** iCol should be ignored when comparing a record with a record from
//	** an index on disk. The field should be ignored if:
//	**
//	**   * the corresponding bit in mask is set, and
//	**   * either:
//	**       - bIntegrity is false, or
//	**       - the two Mem values are both real values that differ by
//	**         BTREE_ULPDISTORTION or fewer ULPs.
//	*/
func _vdbeSkipField(tls *libc.TLS, mask TBitmask, iCol int32, pMem1 uintptr, pMem2 uintptr, bIntegrity int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var v1 uint64
	var _ /* m1 at bp+0 */ Tu64
	var _ /* m2 at bp+8 */ Tu64
	_ = v1
	if iCol >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) || mask&(libc.Uint64FromInt32(1)<<iCol) == uint64(0) {
		return 0
	}
	if bIntegrity == 0 {
		return int32(1)
	}
	if int32((*TMem)(unsafe.Pointer(pMem1)).Fflags)&int32(MEM_Real) != 0 && int32((*TMem)(unsafe.Pointer(pMem2)).Fflags)&int32(MEM_Real) != 0 {
		libc.Xmemcpy(tls, bp, pMem1, uint32(8))
		libc.Xmemcpy(tls, bp+8, pMem2, uint32(8))
		if **(**Tu64)(__ccgo_up(bp)) < **(**Tu64)(__ccgo_up(bp + 8)) {
			v1 = **(**Tu64)(__ccgo_up(bp + 8)) - **(**Tu64)(__ccgo_up(bp))
		} else {
			v1 = **(**Tu64)(__ccgo_up(bp)) - **(**Tu64)(__ccgo_up(bp + 8))
		}
		if v1 <= uint64(BTREE_ULPDISTORTION) {
			return int32(1)
		}
	}
	return 0
}

// C documentation
//
//	/*
//	** A specially optimized version of vdbeSorterCompare() that assumes that
//	** the first field of each key is a TEXT value and that the collation
//	** sequence to compare them with is BINARY.
//	*/
func _vdbeSorterCompareText(tls *libc.TLS, pTask uintptr, pbKey2Cached uintptr, pKey1 uintptr, nKey1 int32, pKey2 uintptr, nKey2 int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var p1, p2, v1, v2 uintptr
	var res, v11 int32
	var _ /* n1 at bp+0 */ int32
	var _ /* n2 at bp+4 */ int32
	_, _, _, _, _, _ = p1, p2, res, v1, v2, v11
	p1 = pKey1
	p2 = pKey2
	v1 = p1 + uintptr(**(**Tu8)(__ccgo_up(p1))) /* Pointer to value 1 */
	v2 = p2 + uintptr(**(**Tu8)(__ccgo_up(p2)))
	**(**int32)(__ccgo_up(bp)) = int32(uint32(**(**Tu8)(__ccgo_up(p1 + 1))))
	if **(**int32)(__ccgo_up(bp)) >= int32(0x80) {
		_sqlite3GetVarint32(tls, p1+1, bp)
	}
	**(**int32)(__ccgo_up(bp + 4)) = int32(uint32(**(**Tu8)(__ccgo_up(p2 + 1))))
	if **(**int32)(__ccgo_up(bp + 4)) >= int32(0x80) {
		_sqlite3GetVarint32(tls, p2+1, bp+4)
	}
	if **(**int32)(__ccgo_up(bp)) < **(**int32)(__ccgo_up(bp + 4)) {
		v11 = **(**int32)(__ccgo_up(bp))
	} else {
		v11 = **(**int32)(__ccgo_up(bp + 4))
	}
	res = libc.Xmemcmp(tls, v1, v2, uint32((v11-int32(13))/int32(2)))
	if res == 0 {
		res = **(**int32)(__ccgo_up(bp)) - **(**int32)(__ccgo_up(bp + 4))
	}
	if res == 0 {
		if int32((*TKeyInfo)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo)).FnKeyField) > int32(1) {
			res = _vdbeSorterCompareTail(tls, pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2)
		}
	} else {
		if **(**Tu8)(__ccgo_up((*TKeyInfo)(unsafe.Pointer((*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).FpKeyInfo)).FaSortFlags)) != 0 {
			res = res * -int32(1)
		}
	}
	return res
}

// C documentation
//
//	/*
//	** Flush the current contents of VdbeSorter.list to a new PMA, possibly
//	** using a background thread.
//	*/
func _vdbeSorterFlushPMA(tls *libc.TLS, pSorter uintptr) (r int32) {
	var aMem, pCtx, pTask uintptr
	var i, iTest, nWorker, rc int32
	_, _, _, _, _, _, _ = aMem, i, iTest, nWorker, pCtx, pTask, rc
	rc = SQLITE_OK
	pTask = uintptr(0) /* Thread context used to create new PMA */
	nWorker = int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask) - int32(1)
	/* Set the flag to indicate that at least one PMA has been written.
	 ** Or will be, anyhow.  */
	(*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUsePMA = uint8(1)
	/* Select a sub-task to sort and flush the current list of in-memory
	 ** records to disk. If the sorter is running in multi-threaded mode,
	 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
	 ** the background thread from a sub-tasks previous turn is still running,
	 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
	 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
	 ** sub-tasks are preferred as they use background threads - the final
	 ** sub-task uses the main thread. */
	i = 0
	for {
		if !(i < nWorker) {
			break
		}
		iTest = (int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FiPrev) + i + int32(1)) % nWorker
		pTask = pSorter + 72 + uintptr(iTest)*88
		if (*TSortSubtask)(unsafe.Pointer(pTask)).FbDone != 0 {
			rc = _vdbeSorterJoinThread(tls, pTask)
		}
		if rc != SQLITE_OK || (*TSortSubtask)(unsafe.Pointer(pTask)).FpThread == uintptr(0) {
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if rc == SQLITE_OK {
		if i == nWorker {
			/* Use the foreground thread for this operation */
			rc = _vdbeSorterListToPMA(tls, pSorter+72+uintptr(nWorker)*88, pSorter+40)
		} else {
			aMem = (*TSortSubtask)(unsafe.Pointer(pTask)).Flist.FaMemory
			pCtx = pTask
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).FiPrev = uint8((int32(pTask) - t__predefined_ptrdiff_t(pSorter+72)) / 88)
			(*TSortSubtask)(unsafe.Pointer(pTask)).Flist = (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList = uintptr(0)
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FszPMA = 0
			if aMem != 0 {
				(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory = aMem
				(*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory = _sqlite3MallocSize(tls, aMem)
			} else {
				if (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0 {
					(*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory = _sqlite3Malloc(tls, uint64((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnMemory))
					if !((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory != 0) {
						return int32(SQLITE_NOMEM)
					}
				}
			}
			rc = _vdbeSorterCreateThread(tls, pTask, __ccgo_fp(_vdbeSorterFlushThread), pCtx)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Join all outstanding threads launched by SorterWrite() to create
//	** level-0 PMAs.
//	*/
func _vdbeSorterJoinAll(tls *libc.TLS, pSorter uintptr, rcin int32) (r int32) {
	var i, rc, rc2 int32
	var pTask uintptr
	_, _, _, _ = i, pTask, rc, rc2
	rc = rcin
	/* This function is always called by the main user thread.
	 **
	 ** If this function is being called after SorterRewind() has been called,
	 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
	 ** is currently attempt to join one of the other threads. To avoid a race
	 ** condition where this thread also attempts to join the same object, join
	 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
	i = int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask) - int32(1)
	for {
		if !(i >= 0) {
			break
		}
		pTask = pSorter + 72 + uintptr(i)*88
		rc2 = _vdbeSorterJoinThread(tls, pTask)
		if rc == SQLITE_OK {
			rc = rc2
		}
		goto _1
	_1:
		;
		i = i - 1
	}
	return rc
}

// C documentation
//
//	/*
//	** Write the current contents of in-memory linked-list pList to a level-0
//	** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
//	** successful, or an SQLite error code otherwise.
//	**
//	** The format of a PMA is:
//	**
//	**     * A varint. This varint contains the total number of bytes of content
//	**       in the PMA (not including the varint itself).
//	**
//	**     * One or more records packed end-to-end in order of ascending keys.
//	**       Each record consists of a varint followed by a blob of data (the
//	**       key). The varint is the number of bytes in the blob of data.
//	*/
func _vdbeSorterListToPMA(tls *libc.TLS, pTask uintptr, pList uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var db, p, pNext uintptr
	var rc int32
	var _ /* writer at bp+0 */ TPmaWriter
	_, _, _, _ = db, p, pNext, rc
	db = (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).Fdb
	rc = SQLITE_OK /* Object used to write to the file */
	libc.Xmemset(tls, bp, 0, uint32(48))
	/* If the first temporary PMA file has not been opened, open it now. */
	if (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd == uintptr(0) {
		rc = _vdbeSorterOpenTempFile(tls, db, 0, pTask+48)
	}
	/* Try to get the file to memory map */
	if rc == SQLITE_OK {
		_vdbeSorterExtendFile(tls, db, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FiEof+(*TSorterList)(unsafe.Pointer(pList)).FszPMA+int64(9))
	}
	/* Sort the list */
	if rc == SQLITE_OK {
		rc = _vdbeSorterSort(tls, pTask, pList)
	}
	if rc == SQLITE_OK {
		pNext = uintptr(0)
		_vdbePmaWriterInit(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FpFd, bp, (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)).Fpgsz, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile.FiEof)
		(*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA = (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA + 1
		_vdbePmaWriteVarint(tls, bp, uint64((*TSorterList)(unsafe.Pointer(pList)).FszPMA))
		p = (*TSorterList)(unsafe.Pointer(pList)).FpList
		for {
			if !(p != 0) {
				break
			}
			pNext = *(*uintptr)(unsafe.Pointer(p + 4))
			_vdbePmaWriteVarint(tls, bp, uint64((*TSorterRecord)(unsafe.Pointer(p)).FnVal))
			_vdbePmaWriteBlob(tls, bp, p+libc.UintptrFromInt32(1)*8, (*TSorterRecord)(unsafe.Pointer(p)).FnVal)
			if (*TSorterList)(unsafe.Pointer(pList)).FaMemory == uintptr(0) {
				Xsqlite3_free(tls, p)
			}
			goto _1
		_1:
			;
			p = pNext
		}
		(*TSorterList)(unsafe.Pointer(pList)).FpList = p
		rc = _vdbePmaWriterFinish(tls, bp, pTask+48+8, pTask+80)
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is called as part of a SorterRewind() operation on a sorter
//	** that has already written two or more level-0 PMAs to one or more temp
//	** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
//	** can be used to incrementally merge all PMAs on disk.
//	**
//	** If successful, SQLITE_OK is returned and *ppOut set to point to the
//	** MergeEngine object at the root of the tree before returning. Or, if an
//	** error occurs, an SQLite error code is returned and the final value
//	** of *ppOut is undefined.
//	*/
func _vdbeSorterMergeTreeBuild(tls *libc.TLS, pSorter uintptr, ppOut uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, iSeq, iTask, nDepth, nReader, rc, v3 int32
	var pMain, pTask uintptr
	var _ /* iReadOff at bp+8 */ Ti64
	var _ /* pMerger at bp+16 */ uintptr
	var _ /* pRoot at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _ = i, iSeq, iTask, nDepth, nReader, pMain, pTask, rc, v3
	pMain = uintptr(0)
	rc = SQLITE_OK
	/* If the sorter uses more than one task, then create the top-level
	 ** MergeEngine here. This MergeEngine will read data from exactly
	 ** one PmaReader per sub-task.  */
	if int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask) > int32(1) {
		pMain = _vdbeMergeEngineNew(tls, int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask))
		if pMain == uintptr(0) {
			rc = int32(SQLITE_NOMEM)
		}
	}
	iTask = 0
	for {
		if !(rc == SQLITE_OK && iTask < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) {
			break
		}
		pTask = pSorter + 72 + uintptr(iTask)*88
		if libc.Bool(false) || (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA != 0 {
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* Root node of tree for this task */
			nDepth = _vdbeSorterTreeDepth(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA)
			**(**Ti64)(__ccgo_up(bp + 8)) = 0
			if (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA <= int32(SORTER_MAX_MERGE_COUNT) {
				rc = _vdbeMergeEngineLevel0(tls, pTask, (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA, bp+8, bp)
			} else {
				iSeq = 0
				**(**uintptr)(__ccgo_up(bp)) = _vdbeMergeEngineNew(tls, int32(SORTER_MAX_MERGE_COUNT))
				if **(**uintptr)(__ccgo_up(bp)) == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
				}
				i = 0
				for {
					if !(i < (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA && rc == SQLITE_OK) {
						break
					}
					**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0) /* Number of level-0 PMAs to merge */
					if (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA-i < int32(SORTER_MAX_MERGE_COUNT) {
						v3 = (*TSortSubtask)(unsafe.Pointer(pTask)).FnPMA - i
					} else {
						v3 = int32(SORTER_MAX_MERGE_COUNT)
					}
					nReader = v3
					rc = _vdbeMergeEngineLevel0(tls, pTask, nReader, bp+8, bp+16)
					if rc == SQLITE_OK {
						v3 = iSeq
						iSeq = iSeq + 1
						rc = _vdbeSorterAddToTree(tls, pTask, nDepth, v3, **(**uintptr)(__ccgo_up(bp)), **(**uintptr)(__ccgo_up(bp + 16)))
					}
					goto _2
				_2:
					;
					i = i + int32(SORTER_MAX_MERGE_COUNT)
				}
			}
			if rc == SQLITE_OK {
				if pMain != uintptr(0) {
					rc = _vdbeIncrMergerNew(tls, pTask, **(**uintptr)(__ccgo_up(bp)), (*TMergeEngine)(unsafe.Pointer(pMain)).FaReadr+uintptr(iTask)*56+48)
				} else {
					pMain = **(**uintptr)(__ccgo_up(bp))
				}
			} else {
				_vdbeMergeEngineFree(tls, **(**uintptr)(__ccgo_up(bp)))
			}
		}
		goto _1
	_1:
		;
		iTask = iTask + 1
	}
	if rc != SQLITE_OK {
		_vdbeMergeEngineFree(tls, pMain)
		pMain = uintptr(0)
	}
	**(**uintptr)(__ccgo_up(ppOut)) = pMain
	return rc
}

// C documentation
//
//	/*
//	** This function is called as part of an sqlite3VdbeSorterRewind() operation
//	** on a sorter that has written two or more PMAs to temporary files. It sets
//	** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
//	** (for multi-threaded sorters) so that it can be used to iterate through
//	** all records stored in the sorter.
//	**
//	** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
//	*/
func _vdbeSorterSetupMerge(tls *libc.TLS, pSorter uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, p, pIncr, pLast, pReadr, pTask0, v3 uintptr
	var i, iTask, rc int32
	var xCompare TSorterCompare
	var _ /* pMain at bp+0 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _ = db, i, iTask, p, pIncr, pLast, pReadr, pTask0, rc, xCompare, v3 /* Return code */
	pTask0 = pSorter + 72
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	db = (*TVdbeSorter)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask0)).FpSorter)).Fdb
	xCompare = _vdbeSorterGetCompare(tls, pSorter)
	i = 0
	for {
		if !(i < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) {
			break
		}
		(*(*TSortSubtask)(unsafe.Pointer(pSorter + 72 + uintptr(i)*88))).FxCompare = xCompare
		goto _1
	_1:
		;
		i = i + 1
	}
	rc = _vdbeSorterMergeTreeBuild(tls, pSorter, bp)
	if rc == SQLITE_OK {
		if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUseThreads != 0 {
			pReadr = uintptr(0)
			pLast = pSorter + 72 + uintptr(int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)-int32(1))*88
			rc = _vdbeSortAllocUnpacked(tls, pLast)
			if rc == SQLITE_OK {
				pReadr = _sqlite3DbMallocZero(tls, db, uint64(56))
				(*TVdbeSorter)(unsafe.Pointer(pSorter)).FpReader = pReadr
				if pReadr == uintptr(0) {
					rc = int32(SQLITE_NOMEM)
				}
			}
			if rc == SQLITE_OK {
				rc = _vdbeIncrMergerNew(tls, pLast, **(**uintptr)(__ccgo_up(bp)), pReadr+48)
				if rc == SQLITE_OK {
					_vdbeIncrMergerSetThreads(tls, (*TPmaReader)(unsafe.Pointer(pReadr)).FpIncr)
					iTask = 0
					for {
						if !(iTask < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)-int32(1)) {
							break
						}
						v3 = (**(**TPmaReader)(__ccgo_up((*TMergeEngine)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaReadr + uintptr(iTask)*56))).FpIncr
						pIncr = v3
						if v3 != 0 {
							_vdbeIncrMergerSetThreads(tls, pIncr)
						}
						goto _2
					_2:
						;
						iTask = iTask + 1
					}
					iTask = 0
					for {
						if !(rc == SQLITE_OK && iTask < int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)) {
							break
						}
						/* Check that:
						 **
						 **   a) The incremental merge object is configured to use the
						 **      right task, and
						 **   b) If it is using task (nTask-1), it is configured to run
						 **      in single-threaded mode. This is important, as the
						 **      root merge (INCRINIT_ROOT) will be using the same task
						 **      object.
						 */
						p = (*TMergeEngine)(unsafe.Pointer(**(**uintptr)(__ccgo_up(bp)))).FaReadr + uintptr(iTask)*56
						rc = _vdbePmaReaderIncrInit(tls, p, int32(INCRINIT_TASK))
						goto _4
					_4:
						;
						iTask = iTask + 1
					}
				}
				**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
			}
			if rc == SQLITE_OK {
				rc = _vdbePmaReaderIncrMergeInit(tls, pReadr, int32(INCRINIT_ROOT))
			}
		} else {
			rc = _vdbeMergeEngineInit(tls, pTask0, **(**uintptr)(__ccgo_up(bp)), INCRINIT_NORMAL)
			(*TVdbeSorter)(unsafe.Pointer(pSorter)).FpMerger = **(**uintptr)(__ccgo_up(bp))
			**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
		}
	}
	if rc != SQLITE_OK {
		_vdbeMergeEngineFree(tls, **(**uintptr)(__ccgo_up(bp)))
	}
	return rc
}

// C documentation
//
//	/*
//	** Sort the linked list of records headed at pTask->pList. Return
//	** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
//	** an error occurs.
//	*/
func _vdbeSorterSort(tls *libc.TLS, pTask uintptr, pList uintptr) (r int32) {
	bp := tls.Alloc(256)
	defer tls.Free(256)
	var i, rc int32
	var p, pNext, v3 uintptr
	var _ /* aSlot at bp+0 */ [64]uintptr
	_, _, _, _, _ = i, p, pNext, rc, v3
	rc = _vdbeSortAllocUnpacked(tls, pTask)
	if rc != SQLITE_OK {
		return rc
	}
	p = (*TSorterList)(unsafe.Pointer(pList)).FpList
	(*TSortSubtask)(unsafe.Pointer(pTask)).FxCompare = _vdbeSorterGetCompare(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).FpSorter)
	libc.Xmemset(tls, bp, 0, uint32(256))
	for p != 0 {
		if (*TSorterList)(unsafe.Pointer(pList)).FaMemory != 0 {
			if p == (*TSorterList)(unsafe.Pointer(pList)).FaMemory {
				pNext = uintptr(0)
			} else {
				pNext = (*TSorterList)(unsafe.Pointer(pList)).FaMemory + uintptr(*(*int32)(unsafe.Pointer(p + 4)))
			}
		} else {
			pNext = *(*uintptr)(unsafe.Pointer(p + 4))
		}
		*(*uintptr)(unsafe.Pointer(p + 4)) = uintptr(0)
		i = 0
		for {
			if !((**(**[64]uintptr)(__ccgo_up(bp)))[i] != 0) {
				break
			}
			p = _vdbeSorterMerge(tls, pTask, p, (**(**[64]uintptr)(__ccgo_up(bp)))[i])
			/* ,--Each aSlot[] holds twice as much as the previous. So we cannot use
			 ** |  up all 64 aSlots[] with only a 64-bit address space.
			 ** v                                                                */
			(**(**[64]uintptr)(__ccgo_up(bp)))[i] = uintptr(0)
			goto _1
		_1:
			;
			i = i + 1
		}
		(**(**[64]uintptr)(__ccgo_up(bp)))[i] = p
		p = pNext
	}
	p = uintptr(0)
	i = 0
	for {
		if !(i < int32(libc.Uint32FromInt64(256)/libc.Uint32FromInt64(4))) {
			break
		}
		if (**(**[64]uintptr)(__ccgo_up(bp)))[i] == uintptr(0) {
			goto _2
		}
		if p != 0 {
			v3 = _vdbeSorterMerge(tls, pTask, p, (**(**[64]uintptr)(__ccgo_up(bp)))[i])
		} else {
			v3 = (**(**[64]uintptr)(__ccgo_up(bp)))[i]
		}
		p = v3
		goto _2
	_2:
		;
		i = i + 1
	}
	(*TSorterList)(unsafe.Pointer(pList)).FpList = p
	return int32((*TUnpackedRecord)(unsafe.Pointer((*TSortSubtask)(unsafe.Pointer(pTask)).FpUnpacked)).FerrCode)
}

// C documentation
//
//	/******************************* sqlite3_bind_  ***************************
//	**
//	** Routines used to attach values to wildcards in a compiled SQL statement.
//	*/
//	/*
//	** Unbind the value bound to variable i in virtual machine p. This is the
//	** the same as binding a NULL value to the column. If the "i" parameter is
//	** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK.
//	**
//	** A successful evaluation of this routine acquires the mutex on p.
//	** the mutex is released if any kind of error occurs.
//	**
//	** The error code stored in database p->db is overwritten with the return
//	** value in any case.
//	**
//	** (tag-20240917-01) If  vdbeUnbind(p,(u32)(i-1))  returns SQLITE_OK,
//	** that means all of the the following will be true:
//	**
//	**     p!=0
//	**     p->pVar!=0
//	**     i>0
//	**     i<=p->nVar
//	**
//	** An assert() is normally added after vdbeUnbind() to help static analyzers
//	** realize this.
//	*/
func _vdbeUnbind(tls *libc.TLS, p uintptr, i uint32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pVar uintptr
	var v1 uint32
	var v2 bool
	_, _, _ = pVar, v1, v2
	if _vdbeSafetyNotNull(tls, p) != 0 {
		return _sqlite3MisuseError(tls, int32(95346))
	}
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
	if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) != int32(VDBE_READY_STATE) {
		_sqlite3Error(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, _sqlite3MisuseError(tls, int32(95350)))
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
		Xsqlite3_log(tls, int32(SQLITE_MISUSE), __ccgo_ts+6890, libc.VaList(bp+8, (*TVdbe)(unsafe.Pointer(p)).FzSql))
		return _sqlite3MisuseError(tls, int32(95354))
	}
	if i >= uint32((*TVdbe)(unsafe.Pointer(p)).FnVar) {
		_sqlite3Error(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, int32(SQLITE_RANGE))
		Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex)
		return int32(SQLITE_RANGE)
	}
	pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr(i)*40
	_sqlite3VdbeMemRelease(tls, pVar)
	(*TMem)(unsafe.Pointer(pVar)).Fflags = uint16(MEM_Null)
	(*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FerrCode = SQLITE_OK
	/* If the bit corresponding to this variable in Vdbe.expmask is set, then
	 ** binding a new value to this variable invalidates the current query plan.
	 **
	 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
	 ** parameter in the WHERE clause might influence the choice of query plan
	 ** for a statement, then the statement will be automatically recompiled,
	 ** as if there had been a schema change, on the first sqlite3_step() call
	 ** following any change to the bindings of that parameter.
	 */
	if v2 = (*TVdbe)(unsafe.Pointer(p)).Fexpmask != uint32(0); v2 {
		if i >= uint32(31) {
			v1 = uint32(0x80000000)
		} else {
			v1 = libc.Uint32FromInt32(1) << i
		}
	}
	if v2 && (*TVdbe)(unsafe.Pointer(p)).Fexpmask&v1 != uint32(0) {
		libc.SetBitFieldPtr16Uint32(p+152, libc.Uint32FromInt32(1), 0, 0x3)
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Allocate and populate an UnpackedRecord structure based on the serialized
//	** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
//	** if successful, or a NULL pointer if an OOM error is encountered.
//	*/
func _vdbeUnpackRecord(tls *libc.TLS, pKeyInfo uintptr, nKey int32, pKey uintptr) (r uintptr) {
	var pRet uintptr
	_ = pRet /* Return value */
	pRet = _sqlite3VdbeAllocUnpackedRecord(tls, pKeyInfo)
	if pRet != 0 {
		libc.Xmemset(tls, (*TUnpackedRecord)(unsafe.Pointer(pRet)).FaMem, 0, uint32(40)*uint32(int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField)+libc.Int32FromInt32(1)))
		_sqlite3VdbeRecordUnpack(tls, nKey, pKey, pRet)
	}
	return pRet
}

// C documentation
//
//	/*
//	** The Table structure pTable is really a VIEW.  Fill in the names of
//	** the columns of the view in the pTable structure.  Return non-zero if
//	** there are errors.  If an error is seen an error message is left
//	** in pParse->zErrMsg.
//	*/
func _viewGetColumnNames(tls *libc.TLS, pParse uintptr, pTable uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pSel, pSelTab, v2 uintptr
	var eParseMode Tu8
	var nErr, nSelect, nTab, rc, v1 int32
	var xAuth Tsqlite3_xauth
	_, _, _, _, _, _, _, _, _, _, _ = db, eParseMode, nErr, nSelect, nTab, pSel, pSelTab, rc, xAuth, v1, v2 /* Copy of the SELECT that implements the view */
	nErr = 0                                                                                                /* Number of errors encountered */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb                                                              /* Saved xAuth pointer */
	if int32((*TTable)(unsafe.Pointer(pTable)).FeTabType) == int32(TABTYP_VTAB) {
		(*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock = (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock + 1
		rc = _sqlite3VtabCallConnect(tls, pParse, pTable)
		(*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock = (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock - 1
		return rc
	}
	/* A positive nCol means the columns names for this view are
	 ** already known.  This routine is not called unless either the
	 ** table is virtual or nCol is zero.
	 */
	/* A negative nCol is a special marker meaning that we are currently
	 ** trying to compute the column names.  If we enter this routine with
	 ** a negative nCol, it means two or more views form a loop, like this:
	 **
	 **     CREATE VIEW one AS SELECT * FROM two;
	 **     CREATE VIEW two AS SELECT * FROM one;
	 **
	 ** Actually, the error above is now caught prior to reaching this point.
	 ** But the following test is still important as it does come up
	 ** in the following:
	 **
	 **     CREATE TABLE main.ex1(a);
	 **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
	 **     SELECT * FROM temp.ex1;
	 */
	if int32((*TTable)(unsafe.Pointer(pTable)).FnCol) < 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16056, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTable)).FzName))
		return int32(1)
	}
	/* If we get this far, it means we need to compute the table names.
	 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
	 ** "*" elements in the results set of the view and will assign cursors
	 ** to the elements of the FROM clause.  But we do not want these changes
	 ** to be permanent.  So the computation is done on a copy of the SELECT
	 ** statement that defines the view.
	 */
	pSel = _sqlite3SelectDup(tls, db, (*(*struct {
		FpSelect uintptr
	})(unsafe.Pointer(&(*TTable)(unsafe.Pointer(pTable)).Fu))).FpSelect, 0)
	if pSel != 0 {
		eParseMode = (*TParse)(unsafe.Pointer(pParse)).FeParseMode
		nTab = (*TParse)(unsafe.Pointer(pParse)).FnTab
		nSelect = (*TParse)(unsafe.Pointer(pParse)).FnSelect
		(*TParse)(unsafe.Pointer(pParse)).FeParseMode = uint8(PARSE_MODE_NORMAL)
		_sqlite3SrcListAssignCursors(tls, pParse, (*TSelect)(unsafe.Pointer(pSel)).FpSrc)
		(*TTable)(unsafe.Pointer(pTable)).FnCol = int16(-int32(1))
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable + 1
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0)
		xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth
		(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0)
		pSelTab = _sqlite3ResultSetOfSelect(tls, pParse, pSel, int8(SQLITE_AFF_NONE))
		(*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth
		(*TParse)(unsafe.Pointer(pParse)).FnTab = nTab
		(*TParse)(unsafe.Pointer(pParse)).FnSelect = nSelect
		if pSelTab == uintptr(0) {
			(*TTable)(unsafe.Pointer(pTable)).FnCol = 0
			nErr = nErr + 1
		} else {
			if (*TTable)(unsafe.Pointer(pTable)).FpCheck != 0 {
				/* CREATE VIEW name(arglist) AS ...
				 ** The names of the columns in the table are taken from
				 ** arglist which is stored in pTable->pCheck.  The pCheck field
				 ** normally holds CHECK constraints on an ordinary table, but for
				 ** a VIEW it holds the list of column names.
				 */
				_sqlite3ColumnsFromExprList(tls, pParse, (*TTable)(unsafe.Pointer(pTable)).FpCheck, pTable+34, pTable+4)
				if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 && int32((*TTable)(unsafe.Pointer(pTable)).FnCol) == (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpEList)).FnExpr {
					_sqlite3SubqueryColumnTypes(tls, pParse, pTable, pSel, int8(SQLITE_AFF_NONE))
				}
			} else {
				/* CREATE VIEW name AS...  without an argument list.  Construct
				 ** the column names from the SELECT statement that defines the view.
				 */
				(*TTable)(unsafe.Pointer(pTable)).FnCol = (*TTable)(unsafe.Pointer(pSelTab)).FnCol
				(*TTable)(unsafe.Pointer(pTable)).FaCol = (*TTable)(unsafe.Pointer(pSelTab)).FaCol
				**(**Tu32)(__ccgo_up(pTable + 28)) |= (*TTable)(unsafe.Pointer(pSelTab)).FtabFlags & uint32(COLFLAG_NOINSERT)
				(*TTable)(unsafe.Pointer(pSelTab)).FnCol = 0
				(*TTable)(unsafe.Pointer(pSelTab)).FaCol = uintptr(0)
			}
		}
		(*TTable)(unsafe.Pointer(pTable)).FnNVCol = (*TTable)(unsafe.Pointer(pTable)).FnCol
		_sqlite3DeleteTable(tls, db, pSelTab)
		_sqlite3SelectDelete(tls, db, pSel)
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable - 1
		if (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0 {
			v1 = 0
		} else {
			v1 = int32((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FszTrue)
		}
		(*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(v1)
		(*TParse)(unsafe.Pointer(pParse)).FeParseMode = eParseMode
	} else {
		nErr = nErr + 1
	}
	v2 = (*TTable)(unsafe.Pointer(pTable)).FpSchema + 78
	*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(DB_UnresetViews))
	if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
		_sqlite3DeleteColumnNames(tls, db, pTable)
	}
	return nErr + (*TParse)(unsafe.Pointer(pParse)).FnErr
}

// C documentation
//
//	/*
//	** The table object reference passed as the second argument to this function
//	** must represent a virtual table. This function invokes the xBestIndex()
//	** method of the virtual table with the sqlite3_index_info object that
//	** comes in as the 3rd argument to this function.
//	**
//	** If an error occurs, pParse is populated with an error message and an
//	** appropriate error code is returned.  A return of SQLITE_CONSTRAINT from
//	** xBestIndex is not considered an error.  SQLITE_CONSTRAINT indicates that
//	** the current configuration of "unusable" flags in sqlite3_index_info can
//	** not result in a valid plan.
//	**
//	** Whether or not an error is returned, it is the responsibility of the
//	** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
//	** that this is required.
//	*/
func _vtabBestIndex(tls *libc.TLS, pParse uintptr, pTab uintptr, p uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var pVtab uintptr
	var rc int32
	_, _ = pVtab, rc
	pVtab = (*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pTab))).FpVtab
	(*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnSchemaLock = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnSchemaLock + 1
	rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule)).FxBestIndex})))(tls, pVtab, p)
	(*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnSchemaLock = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnSchemaLock - 1
	if rc != SQLITE_OK && rc != int32(SQLITE_CONSTRAINT) {
		if rc == int32(SQLITE_NOMEM) {
			_sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb)
		} else {
			if !((*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg != 0) {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+4729, libc.VaList(bp+8, _sqlite3ErrStr(tls, rc)))
			} else {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+4729, libc.VaList(bp+8, (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg))
			}
		}
	}
	if (*TVTable)(unsafe.Pointer((*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).Fp)).FbAllSchemas != 0 {
		_sqlite3VtabUsesAllSchemas(tls, pParse)
	}
	Xsqlite3_free(tls, (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg)
	(*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg = uintptr(0)
	return rc
}

// C documentation
//
//	/*
//	** Invoke a virtual table constructor (either xCreate or xConnect). The
//	** pointer to the function to invoke is passed as the fourth parameter
//	** to this procedure.
//	*/
func _vtabCallConstructor(tls *libc.TLS, db uintptr, pTab uintptr, pMod uintptr, __ccgo_fp_xConstruct uintptr, pzErr uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var azArg, pCtx, pVTable, zFormat, zModuleName, zType, v6 uintptr
	var i, iCol, iDb, j, nArg, nDel, nType, rc, v4 int32
	var oooHidden Tu16
	var _ /* sCtx at bp+0 */ TVtabCtx
	var _ /* zErr at bp+16 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = azArg, i, iCol, iDb, j, nArg, nDel, nType, oooHidden, pCtx, pVTable, rc, zFormat, zModuleName, zType, v4, v6
	nArg = (*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).FnArg
	**(**uintptr)(__ccgo_up(bp + 16)) = uintptr(0)
	azArg = (*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).FazArg
	/* Check that the virtual-table is not already being initialized */
	pCtx = (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx
	for {
		if !(pCtx != 0) {
			break
		}
		if (*TVtabCtx)(unsafe.Pointer(pCtx)).FpTab == pTab {
			**(**uintptr)(__ccgo_up(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+24702, libc.VaList(bp+32, (*TTable)(unsafe.Pointer(pTab)).FzName))
			return int32(SQLITE_LOCKED)
		}
		goto _1
	_1:
		;
		pCtx = (*TVtabCtx)(unsafe.Pointer(pCtx)).FpPrior
	}
	zModuleName = _sqlite3DbStrDup(tls, db, (*TTable)(unsafe.Pointer(pTab)).FzName)
	if !(zModuleName != 0) {
		return int32(SQLITE_NOMEM)
	}
	pVTable = _sqlite3MallocZero(tls, uint64(28))
	if !(pVTable != 0) {
		_sqlite3OomFault(tls, db)
		_sqlite3DbFree(tls, db, zModuleName)
		return int32(SQLITE_NOMEM)
	}
	(*TVTable)(unsafe.Pointer(pVTable)).Fdb = db
	(*TVTable)(unsafe.Pointer(pVTable)).FpMod = pMod
	(*TVTable)(unsafe.Pointer(pVTable)).FeVtabRisk = uint8(SQLITE_VTABRISK_Normal)
	iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema)
	**(**uintptr)(__ccgo_up((*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).FazArg + 1*4)) = (**(**TDb)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*16))).FzDbSName
	/* Invoke the virtual table constructor */
	(**(**TVtabCtx)(__ccgo_up(bp))).FpTab = pTab
	(**(**TVtabCtx)(__ccgo_up(bp))).FpVTable = pVTable
	(**(**TVtabCtx)(__ccgo_up(bp))).FpPrior = (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx
	(**(**TVtabCtx)(__ccgo_up(bp))).FbDeclared = 0
	(*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx = bp
	(*TTable)(unsafe.Pointer(pTab)).FnTabRef = (*TTable)(unsafe.Pointer(pTab)).FnTabRef + 1
	rc = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{__ccgo_fp_xConstruct})))(tls, db, (*TModule)(unsafe.Pointer(pMod)).FpAux, nArg, azArg, pVTable+8, bp+16)
	_sqlite3DeleteTable(tls, db, pTab)
	(*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx = (**(**TVtabCtx)(__ccgo_up(bp))).FpPrior
	if rc == int32(SQLITE_NOMEM) {
		_sqlite3OomFault(tls, db)
	}
	if SQLITE_OK != rc {
		if **(**uintptr)(__ccgo_up(bp + 16)) == uintptr(0) {
			**(**uintptr)(__ccgo_up(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+24744, libc.VaList(bp+32, zModuleName))
		} else {
			**(**uintptr)(__ccgo_up(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+4729, libc.VaList(bp+32, **(**uintptr)(__ccgo_up(bp + 16))))
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 16)))
		}
		_sqlite3DbFree(tls, db, pVTable)
	} else {
		if (*TVTable)(unsafe.Pointer(pVTable)).FpVtab != 0 {
			/* Justification of ALWAYS():  A correct vtab constructor must allocate
			 ** the sqlite3_vtab object if successful.  */
			libc.Xmemset(tls, (*TVTable)(unsafe.Pointer(pVTable)).FpVtab, 0, uint32(12))
			(*Tsqlite3_vtab)(unsafe.Pointer((*TVTable)(unsafe.Pointer(pVTable)).FpVtab)).FpModule = (*TModule)(unsafe.Pointer(pMod)).FpModule
			(*TModule)(unsafe.Pointer(pMod)).FnRefModule = (*TModule)(unsafe.Pointer(pMod)).FnRefModule + 1
			(*TVTable)(unsafe.Pointer(pVTable)).FnRef = int32(1)
			if (**(**TVtabCtx)(__ccgo_up(bp))).FbDeclared == 0 {
				zFormat = __ccgo_ts + 24774
				**(**uintptr)(__ccgo_up(pzErr)) = _sqlite3MPrintf(tls, db, zFormat, libc.VaList(bp+32, zModuleName))
				_sqlite3VtabUnlock(tls, pVTable)
				rc = int32(SQLITE_ERROR)
			} else {
				oooHidden = uint16(0)
				/* If everything went according to plan, link the new VTable structure
				 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
				 ** columns of the table to see if any of them contain the token "hidden".
				 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
				 ** the type string.  */
				(*TVTable)(unsafe.Pointer(pVTable)).FpNext = (*(*struct {
					FnArg  int32
					FazArg uintptr
					Fp     uintptr
				})(unsafe.Pointer(pTab + 44))).Fp
				(*(*struct {
					FnArg  int32
					FazArg uintptr
					Fp     uintptr
				})(unsafe.Pointer(pTab + 44))).Fp = pVTable
				iCol = 0
				for {
					if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) {
						break
					}
					zType = _sqlite3ColumnType(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(iCol)*12, __ccgo_ts+1711)
					i = 0
					nType = _sqlite3Strlen30(tls, zType)
					i = 0
					for {
						if !(i < nType) {
							break
						}
						if 0 == Xsqlite3_strnicmp(tls, __ccgo_ts+19077, zType+uintptr(i), int32(6)) && (i == 0 || int32(**(**int8)(__ccgo_up(zType + uintptr(i-int32(1))))) == int32(' ')) && (int32(**(**int8)(__ccgo_up(zType + uintptr(i+int32(6))))) == int32('\000') || int32(**(**int8)(__ccgo_up(zType + uintptr(i+int32(6))))) == int32(' ')) {
							break
						}
						goto _3
					_3:
						;
						i = i + 1
					}
					if i < nType {
						if **(**int8)(__ccgo_up(zType + uintptr(i+int32(6)))) != 0 {
							v4 = int32(1)
						} else {
							v4 = 0
						}
						nDel = int32(6) + v4
						j = i
						for {
							if !(j+nDel <= nType) {
								break
							}
							**(**int8)(__ccgo_up(zType + uintptr(j))) = **(**int8)(__ccgo_up(zType + uintptr(j+nDel)))
							goto _5
						_5:
							;
							j = j + 1
						}
						if int32(**(**int8)(__ccgo_up(zType + uintptr(i)))) == int32('\000') && i > 0 {
							**(**int8)(__ccgo_up(zType + uintptr(i-int32(1)))) = int8('\000')
						}
						v6 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12 + 10
						*(*Tu16)(unsafe.Pointer(v6)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v6))) | libc.Int32FromInt32(COLFLAG_HIDDEN))
						**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(TF_HasHidden)
						oooHidden = uint16(TF_OOOHidden)
					} else {
						**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(oooHidden)
					}
					goto _2
				_2:
					;
					iCol = iCol + 1
				}
			}
		}
	}
	_sqlite3DbFree(tls, db, zModuleName)
	return rc
}

// C documentation
//
//	/* Return true if table pTab is read-only.
//	**
//	** A table is read-only if any of the following are true:
//	**
//	**   1) It is a virtual table and no implementation of the xUpdate method
//	**      has been provided
//	**
//	**   2) A trigger is currently being coded and the table is a virtual table
//	**      that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and
//	**      the table is not SQLITE_VTAB_INNOCUOUS.
//	**
//	**   3) It is a system table (i.e. sqlite_schema), this call is not
//	**      part of a nested parse and writable_schema pragma has not
//	**      been specified
//	**
//	**   4) The table is a shadow table, the database connection is in
//	**      defensive mode, and the current sqlite3_prepare()
//	**      is for a top-level SQL statement.
//	*/
func _vtabIsReadOnly(tls *libc.TLS, pParse uintptr, pTab uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	if (*Tsqlite3_module)(unsafe.Pointer((*TModule)(unsafe.Pointer((*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pTab))).FpMod)).FpModule)).FxUpdate == uintptr(0) {
		return int32(1)
	}
	/* Within triggers:
	 **   *  Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY
	 **      virtual tables
	 **   *  Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
	 **      virtual tables if PRAGMA trusted_schema=ON.
	 */
	if ((*TParse)(unsafe.Pointer(pParse)).FpToplevel != uintptr(0) || int32((*TParse)(unsafe.Pointer(pParse)).FprepFlags)&int32(SQLITE_PREPARE_FROM_DDL) != 0) && int32((*TVTable)(unsafe.Pointer((*(*struct {
		FnArg  int32
		FazArg uintptr
		Fp     uintptr
	})(unsafe.Pointer(pTab + 44))).Fp)).FeVtabRisk) > libc.BoolInt32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_TrustedSchema) != uint64(0)) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+17567, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName))
	}
	return 0
}

// C documentation
//
//	/*
//	** Open a transaction in a connection where the shared-memory is read-only
//	** and where we cannot verify that there is a separate write-capable connection
//	** on hand to keep the shared-memory up-to-date with the WAL file.
//	**
//	** This can happen, for example, when the shared-memory is implemented by
//	** memory-mapping a *-shm file, where a prior writer has shut down and
//	** left the *-shm file on disk, and now the present connection is trying
//	** to use that database but lacks write permission on the *-shm file.
//	** Other scenarios are also possible, depending on the VFS implementation.
//	**
//	** Precondition:
//	**
//	**    The *-wal file has been read and an appropriate wal-index has been
//	**    constructed in pWal->apWiData[] using heap memory instead of shared
//	**    memory.
//	**
//	** If this function returns SQLITE_OK, then the read transaction has
//	** been successfully opened. In this case output variable (*pChanged)
//	** is set to true before returning if the caller should discard the
//	** contents of the page cache before proceeding. Or, if it returns
//	** WAL_RETRY, then the heap memory wal-index has been discarded and
//	** the caller should retry opening the read transaction from the
//	** beginning (including attempting to map the *-shm file).
//	**
//	** If an error occurs, an SQLite error code is returned.
//	*/
func _walBeginShmUnreliable(tls *libc.TLS, pWal uintptr, pChanged uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var aData, aFrame uintptr
	var aSaveCksum [2]Tu32
	var i, rc, szFrame, v1 int32
	var iOffset Ti64
	var _ /* aBuf at bp+8 */ [32]Tu8
	var _ /* nTruncate at bp+48 */ Tu32
	var _ /* pDummy at bp+40 */ uintptr
	var _ /* pgno at bp+44 */ Tu32
	var _ /* szWal at bp+0 */ Ti64
	_, _, _, _, _, _, _, _ = aData, aFrame, aSaveCksum, i, iOffset, rc, szFrame, v1 /* Buffer to load WAL header into */
	aFrame = uintptr(0)                                                             /* Saved copy of pWal->hdr.aFrameCksum */
	/* Take WAL_READ_LOCK(0). This has the effect of preventing any
	 ** writers from running a checkpoint, but does not stop them
	 ** from running recovery.  */
	rc = _walLockShared(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(0))
	if rc != SQLITE_OK {
		if rc == int32(SQLITE_BUSY) {
			rc = -int32(1)
		}
		goto begin_unreliable_shm_out
	}
	(*TWal)(unsafe.Pointer(pWal)).FreadLock = 0
	/* Check to see if a separate writer has attached to the shared-memory area,
	 ** thus making the shared-memory "reliable" again.  Do this by invoking
	 ** the xShmMap() routine of the VFS and looking to see if the return
	 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
	 **
	 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
	 ** cause the heap-memory WAL-index to be discarded and the actual
	 ** shared memory to be used in its place.
	 **
	 ** This step is important because, even though this connection is holding
	 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
	 ** have already checkpointed the WAL file and, while the current
	 ** is active, wrap the WAL and start overwriting frames that this
	 ** process wants to use.
	 **
	 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
	 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
	 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
	 ** even if some external agent does a "chmod" to make the shared-memory
	 ** writable by us, until sqlite3OsShmUnmap() has been called.
	 ** This is a requirement on the VFS implementation.
	 */
	rc = _sqlite3OsShmMap(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, 0, int32(libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4)), 0, bp+40)
	/* SQLITE_OK not possible for read-only connection */
	if rc != libc.Int32FromInt32(SQLITE_READONLY)|libc.Int32FromInt32(5)<<libc.Int32FromInt32(8) {
		if rc == int32(SQLITE_READONLY) {
			v1 = -int32(1)
		} else {
			v1 = rc
		}
		rc = v1
		goto begin_unreliable_shm_out
	}
	/* We reach this point only if the real shared-memory is still unreliable.
	 ** Assume the in-memory WAL-index substitute is correct and load it
	 ** into pWal->hdr.
	 */
	libc.Xmemcpy(tls, pWal+52, _walIndexHdr(tls, pWal), uint32(48))
	/* Make sure some writer hasn't come in and changed the WAL file out
	 ** from under us, then disconnected, while we were not looking.
	 */
	rc = _sqlite3OsFileSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp)
	if rc != SQLITE_OK {
		goto begin_unreliable_shm_out
	}
	if **(**Ti64)(__ccgo_up(bp)) < int64(WAL_HDRSIZE) {
		/* If the wal file is too small to contain a wal-header and the
		 ** wal-index header has mxFrame==0, then it must be safe to proceed
		 ** reading the database file only. However, the page cache cannot
		 ** be trusted, as a read/write connection may have connected, written
		 ** the db, run a checkpoint, truncated the wal file and disconnected
		 ** since this client's last read transaction.  */
		**(**int32)(__ccgo_up(pChanged)) = int32(1)
		if (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame == uint32(0) {
			v1 = SQLITE_OK
		} else {
			v1 = -int32(1)
		}
		rc = v1
		goto begin_unreliable_shm_out
	}
	/* Check the salt keys at the start of the wal file still match. */
	rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp+8, int32(WAL_HDRSIZE), 0)
	if rc != SQLITE_OK {
		goto begin_unreliable_shm_out
	}
	if libc.Xmemcmp(tls, pWal+52+32, bp+8+16, uint32(8)) != 0 {
		/* Some writer has wrapped the WAL file while we were not looking.
		 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
		 ** rebuilt. */
		rc = -int32(1)
		goto begin_unreliable_shm_out
	}
	/* Allocate a buffer to read frames into */
	szFrame = int32((*TWal)(unsafe.Pointer(pWal)).FszPage + uint32(WAL_FRAME_HDRSIZE))
	aFrame = Xsqlite3_malloc64(tls, uint64(szFrame))
	if aFrame == uintptr(0) {
		rc = int32(SQLITE_NOMEM)
		goto begin_unreliable_shm_out
	}
	aData = aFrame + 24
	/* Check to see if a complete transaction has been appended to the
	 ** wal file since the heap-memory wal-index was created. If so, the
	 ** heap-memory wal-index is discarded and WAL_RETRY returned to
	 ** the caller.  */
	aSaveCksum[0] = **(**Tu32)(__ccgo_up(pWal + 52 + 24))
	aSaveCksum[int32(1)] = **(**Tu32)(__ccgo_up(pWal + 52 + 24 + 1*4))
	iOffset = libc.Int64FromInt32(WAL_HDRSIZE) + int64((*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame+libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64((*TWal)(unsafe.Pointer(pWal)).FszPage+libc.Uint32FromInt32(WAL_FRAME_HDRSIZE))
	for {
		if !(iOffset+int64(szFrame) <= **(**Ti64)(__ccgo_up(bp))) {
			break
		} /* dbsize field from frame header */
		/* Read and decode the next log frame. */
		rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, aFrame, szFrame, iOffset)
		if rc != SQLITE_OK {
			break
		}
		if !(_walDecodeFrame(tls, pWal, bp+44, bp+48, aData, aFrame) != 0) {
			break
		}
		/* If nTruncate is non-zero, then a complete transaction has been
		 ** appended to this wal file. Set rc to WAL_RETRY and break out of
		 ** the loop.  */
		if **(**Tu32)(__ccgo_up(bp + 48)) != 0 {
			rc = -int32(1)
			break
		}
		goto _3
	_3:
		;
		iOffset = iOffset + int64(szFrame)
	}
	**(**Tu32)(__ccgo_up(pWal + 52 + 24)) = aSaveCksum[0]
	**(**Tu32)(__ccgo_up(pWal + 52 + 24 + 1*4)) = aSaveCksum[int32(1)]
	goto begin_unreliable_shm_out
begin_unreliable_shm_out:
	;
	Xsqlite3_free(tls, aFrame)
	if rc != SQLITE_OK {
		i = 0
		for {
			if !(i < (*TWal)(unsafe.Pointer(pWal)).FnWiData) {
				break
			}
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(i)*4)))
			**(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(i)*4)) = uintptr(0)
			goto _4
		_4:
			;
			i = i + 1
		}
		(*TWal)(unsafe.Pointer(pWal)).FbShmUnreliable = uint8(0)
		_sqlite3WalEndReadTransaction(tls, pWal)
		**(**int32)(__ccgo_up(pChanged)) = int32(1)
	}
	return rc
}

/*
** The final argument passed to walTryBeginRead() is of type (int*). The
** caller should invoke walTryBeginRead as follows:
**
**   int cnt = 0;
**   do {
**     rc = walTryBeginRead(..., &cnt);
**   }while( rc==WAL_RETRY );
**
** The final value of "cnt" is of no use to the caller. It is used by
** the implementation of walTryBeginRead() as follows:
**
**   + Each time walTryBeginRead() is called, it is incremented. Once
**     it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead()
**     has many times been invoked and failed with WAL_RETRY - walTryBeginRead()
**     returns SQLITE_PROTOCOL.
**
**   + If SQLITE_ENABLE_SETLK_TIMEOUT is defined and walTryBeginRead() failed
**     because a blocking lock timed out (SQLITE_BUSY_TIMEOUT from the OS
**     layer), the WAL_RETRY_BLOCKED_MASK bit is set in "cnt". In this case
**     the next invocation of walTryBeginRead() may omit an expected call to
**     sqlite3OsSleep(). There has already been a delay when the previous call
**     waited on a lock.
 */

// C documentation
//
//	/*
//	** Copy as much content as we can from the WAL back into the database file
//	** in response to an sqlite3_wal_checkpoint() request or the equivalent.
//	**
//	** The amount of information copies from WAL to database might be limited
//	** by active readers.  This routine will never overwrite a database page
//	** that a concurrent reader might be using.
//	**
//	** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
//	** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if
//	** checkpoints are always run by a background thread or background
//	** process, foreground threads will never block on a lengthy fsync call.
//	**
//	** Fsync is called on the WAL before writing content out of the WAL and
//	** into the database.  This ensures that if the new content is persistent
//	** in the WAL and can be recovered following a power-loss or hard reset.
//	**
//	** Fsync is also called on the database file if (and only if) the entire
//	** WAL content is copied into the database file.  This second fsync makes
//	** it safe to delete the WAL since the new content will persist in the
//	** database file.
//	**
//	** This routine uses and updates the nBackfill field of the wal-index header.
//	** This is the only routine that will increase the value of nBackfill.
//	** (A WAL reset or recovery will revert nBackfill to zero, but not increase
//	** its value.)
//	**
//	** The caller must be holding sufficient locks to ensure that no other
//	** checkpoint is running (in any other thread or process) at the same
//	** time.
//	*/
func _walCheckpoint(tls *libc.TLS, pWal uintptr, db uintptr, eMode int32, __ccgo_fp_xBusy uintptr, pBusyArg uintptr, sync_flags int32, zBuf uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var bChg, i, rc, szPage, v3 int32
	var iMark, mxPage, mxSafeFrame, nBackfill, y Tu32
	var iOffset, szDb Ti64
	var pInfo, pLive uintptr
	var v2 uint32
	var v4 bool
	var _ /* iDbpage at bp+4 */ Tu32
	var _ /* iFrame at bp+8 */ Tu32
	var _ /* nReq at bp+16 */ Ti64
	var _ /* nSize at bp+24 */ Ti64
	var _ /* pIter at bp+0 */ uintptr
	var _ /* salt1 at bp+32 */ Tu32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bChg, i, iMark, iOffset, mxPage, mxSafeFrame, nBackfill, pInfo, pLive, rc, szDb, szPage, y, v2, v3, v4
	rc = SQLITE_OK                            /* Database page-size */
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0) /* Wal iterator context */
	**(**Tu32)(__ccgo_up(bp + 4)) = uint32(0) /* Next database page to write */
	**(**Tu32)(__ccgo_up(bp + 8)) = uint32(0) /* The checkpoint status information */
	szPage = _walPagesize(tls, pWal)
	pInfo = _walCkptInfo(tls, pWal)
	if (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill < (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame {
		/* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
		 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
		/* Compute in mxSafeFrame the index of the last frame of the WAL that is
		 ** safe to write into the database.  Frames beyond mxSafeFrame might
		 ** overwrite database pages that are in use by active readers and thus
		 ** cannot be backfilled from the WAL.
		 */
		mxSafeFrame = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame
		mxPage = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage
		i = int32(1)
		for {
			if !(i < libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)) {
				break
			}
			y = libc.AtomicLoadNUint32(pInfo+4+uintptr(i)*4, libc.Int32FromInt32(__ATOMIC_RELAXED))
			if mxSafeFrame > y {
				rc = _walBusyLock(tls, pWal, __ccgo_fp_xBusy, pBusyArg, int32(3)+i, int32(1))
				if rc == SQLITE_OK {
					if i == int32(1) {
						v2 = mxSafeFrame
					} else {
						v2 = uint32(READMARK_NOT_USED)
					}
					iMark = v2
					libc.AtomicStoreNUint32(pInfo+4+uintptr(i)*4, iMark, libc.Int32FromInt32(__ATOMIC_RELAXED))
					_walUnlockExclusive(tls, pWal, int32(3)+i, int32(1))
				} else {
					if rc == int32(SQLITE_BUSY) {
						mxSafeFrame = y
						__ccgo_fp_xBusy = uintptr(0)
					} else {
						goto walcheckpoint_out
					}
				}
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		/* Allocate the iterator */
		if (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill < mxSafeFrame {
			rc = _walIteratorInit(tls, pWal, (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill, bp)
		}
		if v4 = **(**uintptr)(__ccgo_up(bp)) != 0; v4 {
			v3 = _walBusyLock(tls, pWal, __ccgo_fp_xBusy, pBusyArg, libc.Int32FromInt32(3)+libc.Int32FromInt32(0), int32(1))
			rc = v3
		}
		if v4 && v3 == SQLITE_OK {
			nBackfill = (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill
			pLive = _walIndexHdr(tls, pWal)
			/* Now that read-lock slot 0 is locked, check that the wal has not been
			 ** wrapped since the header was read for this checkpoint. If it was, then
			 ** there was no work to do anyway.  In this case the
			 ** (pInfo->nBackfill<pWal->hdr.mxFrame) test above only passed because
			 ** pInfo->nBackfill had already been set to 0 by the writer that wrapped
			 ** the wal file. It would also be dangerous to proceed, as there may be
			 ** fewer than pWal->hdr.mxFrame valid frames in the wal file.  */
			bChg = libc.Xmemcmp(tls, pLive+32, pWal+52+32, uint32(8))
			if 0 == bChg {
				(*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted = mxSafeFrame
				/* Sync the WAL to disk */
				rc = _sqlite3OsSync(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, sync_flags>>int32(2)&int32(0x03))
				/* If the database may grow as a result of this checkpoint, hint
				 ** about the eventual size of the db file to the VFS layer.
				 */
				if rc == SQLITE_OK {
					**(**Ti64)(__ccgo_up(bp + 16)) = int64(mxPage) * int64(szPage) /* Current size of database file */
					_sqlite3OsFileControl(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, int32(SQLITE_FCNTL_CKPT_START), uintptr(0))
					rc = _sqlite3OsFileSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, bp+24)
					if rc == SQLITE_OK && **(**Ti64)(__ccgo_up(bp + 24)) < **(**Ti64)(__ccgo_up(bp + 16)) {
						if **(**Ti64)(__ccgo_up(bp + 24))+int64(65536)+int64((*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame)*int64(szPage) < **(**Ti64)(__ccgo_up(bp + 16)) {
							/* If the size of the final database is larger than the current
							 ** database plus the amount of data in the wal file, plus the
							 ** maximum size of the pending-byte page (65536 bytes), then
							 ** must be corruption somewhere.  */
							rc = _sqlite3CorruptError(tls, int32(69812))
						} else {
							_sqlite3OsFileControlHint(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, int32(SQLITE_FCNTL_SIZE_HINT), bp+16)
						}
					}
				}
				/* Iterate through the contents of the WAL, copying data to the
				 ** db file */
				for rc == SQLITE_OK && 0 == _walIteratorNext(tls, **(**uintptr)(__ccgo_up(bp)), bp+4, bp+8) {
					if libc.AtomicLoadNInt32(db+312, libc.Int32FromInt32(__ATOMIC_RELAXED)) != 0 {
						if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
							v3 = int32(SQLITE_NOMEM)
						} else {
							v3 = int32(SQLITE_INTERRUPT)
						}
						rc = v3
						break
					}
					if **(**Tu32)(__ccgo_up(bp + 8)) <= nBackfill || **(**Tu32)(__ccgo_up(bp + 8)) > mxSafeFrame || **(**Tu32)(__ccgo_up(bp + 4)) > mxPage {
						continue
					}
					iOffset = int64(WAL_HDRSIZE) + int64(**(**Tu32)(__ccgo_up(bp + 8))-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) + int64(WAL_FRAME_HDRSIZE)
					/* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
					rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, zBuf, szPage, iOffset)
					if rc != SQLITE_OK {
						break
					}
					iOffset = int64(**(**Tu32)(__ccgo_up(bp + 4))-libc.Uint32FromInt32(1)) * int64(szPage)
					rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, zBuf, szPage, iOffset)
					if rc != SQLITE_OK {
						break
					}
				}
				_sqlite3OsFileControl(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, int32(SQLITE_FCNTL_CKPT_DONE), uintptr(0))
				/* If work was actually accomplished... */
				if rc == SQLITE_OK {
					if mxSafeFrame == (*TWalIndexHdr)(unsafe.Pointer(_walIndexHdr(tls, pWal))).FmxFrame {
						szDb = int64((*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage) * int64(szPage)
						rc = _sqlite3OsTruncate(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, szDb)
						if rc == SQLITE_OK {
							rc = _sqlite3OsSync(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, sync_flags>>int32(2)&int32(0x03))
						}
					}
					if rc == SQLITE_OK {
						libc.AtomicStoreNUint32(pInfo, mxSafeFrame, libc.Int32FromInt32(__ATOMIC_RELAXED))
					}
				}
			}
			/* Release the reader lock held while backfilling */
			_walUnlockExclusive(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(0), int32(1))
		}
		if rc == int32(SQLITE_BUSY) {
			/* Reset the return code so as not to report a checkpoint failure
			 ** just because there are active readers.  */
			rc = SQLITE_OK
		}
	}
	/* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
	 ** entire wal file has been copied into the database file, then block
	 ** until all readers have finished using the wal file. This ensures that
	 ** the next process to write to the database restarts the wal file.
	 */
	if rc == SQLITE_OK && eMode != SQLITE_CHECKPOINT_PASSIVE {
		if (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill < (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame {
			rc = int32(SQLITE_BUSY)
		} else {
			if eMode >= int32(SQLITE_CHECKPOINT_RESTART) {
				Xsqlite3_randomness(tls, int32(4), bp+32)
				rc = _walBusyLock(tls, pWal, __ccgo_fp_xBusy, pBusyArg, libc.Int32FromInt32(3)+libc.Int32FromInt32(1), libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)-libc.Int32FromInt32(1))
				if rc == SQLITE_OK {
					if eMode == int32(SQLITE_CHECKPOINT_TRUNCATE) {
						/* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
						 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
						 ** truncates the log file to zero bytes just prior to a
						 ** successful return.
						 **
						 ** In theory, it might be safe to do this without updating the
						 ** wal-index header in shared memory, as all subsequent reader or
						 ** writer clients should see that the entire log file has been
						 ** checkpointed and behave accordingly. This seems unsafe though,
						 ** as it would leave the system in a state where the contents of
						 ** the wal-index header do not match the contents of the
						 ** file-system. To avoid this, update the wal-index header to
						 ** indicate that the log file contains zero valid frames.  */
						_walRestartHdr(tls, pWal, **(**Tu32)(__ccgo_up(bp + 32)))
						rc = _sqlite3OsTruncate(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, 0)
					}
					_walUnlockExclusive(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(1), libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)-libc.Int32FromInt32(1))
				}
			}
		}
	}
	goto walcheckpoint_out
walcheckpoint_out:
	;
	_walIteratorFree(tls, **(**uintptr)(__ccgo_up(bp)))
	return rc
}

// C documentation
//
//	/*
//	** Remove entries from the hash table that point to WAL slots greater
//	** than pWal->hdr.mxFrame.
//	**
//	** This function is called whenever pWal->hdr.mxFrame is decreased due
//	** to a rollback or savepoint.
//	**
//	** At most only the hash table containing pWal->hdr.mxFrame needs to be
//	** updated.  Any later hash tables will be automatically cleared when
//	** pWal->hdr.mxFrame advances to the point where those hash tables are
//	** actually needed.
//	*/
func _walCleanupHash(tls *libc.TLS, pWal uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, iLimit, nByte int32
	var _ /* sLoc at bp+0 */ TWalHashLoc
	_, _, _ = i, iLimit, nByte /* Hash table location */
	iLimit = 0                 /* Used to iterate through aHash[] */
	if (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame == uint32(0) {
		return
	}
	/* Obtain pointers to the hash-table and page-number array containing
	 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
	 ** that the page said hash-table and array reside on is already mapped.(1)
	 */
	i = _walHashGet(tls, pWal, _walFramePage(tls, (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame), bp)
	if i != 0 {
		return
	} /* Defense-in-depth, in case (1) above is wrong */
	/* Zero all hash-table entries that correspond to frame numbers greater
	 ** than pWal->hdr.mxFrame.
	 */
	iLimit = int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame - (**(**TWalHashLoc)(__ccgo_up(bp))).FiZero)
	i = 0
	for {
		if !(i < libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2)) {
			break
		}
		if int32(**(**Tht_slot)(__ccgo_up((**(**TWalHashLoc)(__ccgo_up(bp))).FaHash + uintptr(i)*2))) > iLimit {
			**(**Tht_slot)(__ccgo_up((**(**TWalHashLoc)(__ccgo_up(bp))).FaHash + uintptr(i)*2)) = uint16(0)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	/* Zero the entries in the aPgno array that correspond to frames with
	 ** frame numbers greater than pWal->hdr.mxFrame.
	 */
	nByte = int32((**(**TWalHashLoc)(__ccgo_up(bp))).FaHash) - int32((**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno+uintptr(iLimit)*4)
	libc.Xmemset(tls, (**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno+uintptr(iLimit)*4, 0, uint32(nByte))
}

// C documentation
//
//	/*
//	** Check to see if the frame with header in aFrame[] and content
//	** in aData[] is valid.  If it is a valid frame, fill *piPage and
//	** *pnTruncate and return true.  Return if the frame is not valid.
//	*/
func _walDecodeFrame(tls *libc.TLS, pWal uintptr, piPage uintptr, pnTruncate uintptr, aData uintptr, aFrame uintptr) (r int32) {
	var aCksum uintptr
	var nativeCksum int32
	var pgno Tu32
	_, _, _ = aCksum, nativeCksum, pgno /* True for native byte-order checksums */
	aCksum = pWal + 52 + 24             /* Page number of the frame */
	/* A frame is only valid if the salt values in the frame-header
	 ** match the salt values in the wal-header.
	 */
	if libc.Xmemcmp(tls, pWal+52+32, aFrame+8, uint32(8)) != 0 {
		return 0
	}
	/* A frame is only valid if the page number is greater than zero.
	 */
	pgno = _sqlite3Get4byte(tls, aFrame)
	if pgno == uint32(0) {
		return 0
	}
	/* Need a valid page size
	 */
	if !((*TWal)(unsafe.Pointer(pWal)).FszPage != 0) {
		return 0
	}
	/* A frame is only valid if a checksum of the WAL header,
	 ** all prior frames, the first 16 bytes of this frame-header,
	 ** and the frame-data matches the checksum in the last 8
	 ** bytes of this frame-header.
	 */
	nativeCksum = libc.BoolInt32(int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum) == SQLITE_BIGENDIAN)
	_walChecksumBytes(tls, nativeCksum, aFrame, int32(8), aCksum, aCksum)
	_walChecksumBytes(tls, nativeCksum, aData, int32((*TWal)(unsafe.Pointer(pWal)).FszPage), aCksum, aCksum)
	if **(**Tu32)(__ccgo_up(aCksum)) != _sqlite3Get4byte(tls, aFrame+16) || **(**Tu32)(__ccgo_up(aCksum + 1*4)) != _sqlite3Get4byte(tls, aFrame+20) {
		/* Checksum failed. */
		return 0
	}
	/* If we reach this point, the frame is valid.  Return the page number
	 ** and the new database size.
	 */
	**(**Tu32)(__ccgo_up(piPage)) = pgno
	**(**Tu32)(__ccgo_up(pnTruncate)) = _sqlite3Get4byte(tls, aFrame+4)
	return int32(1)
}

// C documentation
//
//	/*
//	** This function encodes a single frame header and writes it to a buffer
//	** supplied by the caller. A frame-header is made up of a series of
//	** 4-byte big-endian integers, as follows:
//	**
//	**     0: Page number.
//	**     4: For commit records, the size of the database image in pages
//	**        after the commit. For all other records, zero.
//	**     8: Salt-1 (copied from the wal-header)
//	**    12: Salt-2 (copied from the wal-header)
//	**    16: Checksum-1.
//	**    20: Checksum-2.
//	*/
func _walEncodeFrame(tls *libc.TLS, pWal uintptr, iPage Tu32, nTruncate Tu32, aData uintptr, aFrame uintptr) {
	var aCksum uintptr
	var nativeCksum int32
	_, _ = aCksum, nativeCksum /* True for native byte-order checksums */
	aCksum = pWal + 52 + 24
	_sqlite3Put4byte(tls, aFrame, iPage)
	_sqlite3Put4byte(tls, aFrame+4, nTruncate)
	if (*TWal)(unsafe.Pointer(pWal)).FiReCksum == uint32(0) {
		libc.Xmemcpy(tls, aFrame+8, pWal+52+32, uint32(8))
		nativeCksum = libc.BoolInt32(int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum) == SQLITE_BIGENDIAN)
		_walChecksumBytes(tls, nativeCksum, aFrame, int32(8), aCksum, aCksum)
		_walChecksumBytes(tls, nativeCksum, aData, int32((*TWal)(unsafe.Pointer(pWal)).FszPage), aCksum, aCksum)
		_sqlite3Put4byte(tls, aFrame+16, **(**Tu32)(__ccgo_up(aCksum)))
		_sqlite3Put4byte(tls, aFrame+20, **(**Tu32)(__ccgo_up(aCksum + 1*4)))
	} else {
		libc.Xmemset(tls, aFrame+8, 0, uint32(16))
	}
}

// C documentation
//
//	/*
//	** Search the wal file for page pgno. If found, set *piRead to the frame that
//	** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
//	** to zero.
//	**
//	** Return SQLITE_OK if successful, or an error code if an error occurs. If an
//	** error does occur, the final value of *piRead is undefined.
//	*/
func _walFindFrame(tls *libc.TLS, pWal uintptr, pgno TPgno, piRead uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iFrame, iH, iLast, iRead, v2 Tu32
	var iHash, iKey, iMinHash, nCollide, rc, v3 int32
	var _ /* sLoc at bp+0 */ TWalHashLoc
	_, _, _, _, _, _, _, _, _, _, _ = iFrame, iH, iHash, iKey, iLast, iMinHash, iRead, nCollide, rc, v2, v3
	iRead = uint32(0) /* If !=0, WAL frame to return data from */
	iLast = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame
	/* This routine is only be called from within a read transaction. */
	/* If the "last page" field of the wal-index header snapshot is 0, then
	 ** no data will be read from the wal under any circumstances. Return early
	 ** in this case as an optimization.  Likewise, if pWal->readLock==0,
	 ** then the WAL is ignored by the reader so return early, as if the
	 ** WAL were empty.
	 */
	if iLast == uint32(0) || int32((*TWal)(unsafe.Pointer(pWal)).FreadLock) == 0 && int32((*TWal)(unsafe.Pointer(pWal)).FbShmUnreliable) == 0 {
		**(**Tu32)(__ccgo_up(piRead)) = uint32(0)
		return SQLITE_OK
	}
	/* Search the hash table or tables for an entry matching page number
	 ** pgno. Each iteration of the following for() loop searches one
	 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
	 **
	 ** This code might run concurrently to the code in walIndexAppend()
	 ** that adds entries to the wal-index (and possibly to this hash
	 ** table). This means the value just read from the hash
	 ** slot (aHash[iKey]) may have been added before or after the
	 ** current read transaction was opened. Values added after the
	 ** read transaction was opened may have been written incorrectly -
	 ** i.e. these slots may contain garbage data. However, we assume
	 ** that any slots written before the current read transaction was
	 ** opened remain unmodified.
	 **
	 ** For the reasons above, the if(...) condition featured in the inner
	 ** loop of the following block is more stringent that would be required
	 ** if we had exclusive access to the hash-table:
	 **
	 **   (aPgno[iFrame]==pgno):
	 **     This condition filters out normal hash-table collisions.
	 **
	 **   (iFrame<=iLast):
	 **     This condition filters out entries that were added to the hash
	 **     table after the current read-transaction had started.
	 */
	iMinHash = _walFramePage(tls, (*TWal)(unsafe.Pointer(pWal)).FminFrame)
	iHash = _walFramePage(tls, iLast)
	for {
		if !(iHash >= iMinHash) {
			break
		}
		rc = _walHashGet(tls, pWal, iHash, bp)
		if rc != SQLITE_OK {
			return rc
		}
		nCollide = libc.Int32FromInt32(HASHTABLE_NPAGE) * libc.Int32FromInt32(2)
		iKey = _walHash(tls, pgno)
		for {
			v2 = uint32(libc.AtomicLoadNUint16((**(**TWalHashLoc)(__ccgo_up(bp))).FaHash+uintptr(iKey)*2, libc.Int32FromInt32(__ATOMIC_RELAXED)))
			iH = v2
			if !(v2 != uint32(0)) {
				break
			}
			iFrame = iH + (**(**TWalHashLoc)(__ccgo_up(bp))).FiZero
			if iFrame <= iLast && iFrame >= (*TWal)(unsafe.Pointer(pWal)).FminFrame && **(**Tu32)(__ccgo_up((**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno + uintptr((iH-uint32(1))&uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)-libc.Int32FromInt32(1)))*4)) == pgno {
				iRead = iFrame
			}
			v3 = nCollide
			nCollide = nCollide - 1
			if v3 == 0 {
				**(**Tu32)(__ccgo_up(piRead)) = uint32(0)
				return _sqlite3CorruptError(tls, int32(71119))
			}
			iKey = _walNextHash(tls, iKey)
		}
		if iRead != 0 {
			break
		}
		goto _1
	_1:
		;
		iHash = iHash - 1
	}
	**(**Tu32)(__ccgo_up(piRead)) = iRead
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Return the number of the wal-index page that contains the hash-table
//	** and page-number array that contain entries corresponding to WAL frame
//	** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
//	** are numbered starting from 0.
//	*/
func _walFramePage(tls *libc.TLS, iFrame Tu32) (r int32) {
	var iHash int32
	_ = iHash
	iHash = int32((iFrame + uint32(HASHTABLE_NPAGE) - (libc.Uint32FromInt32(HASHTABLE_NPAGE) - (libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4)) - uint32(1)) / uint32(HASHTABLE_NPAGE))
	return iHash
}

// C documentation
//
//	/*
//	** Write a set of frames to the log. The caller must hold the write-lock
//	** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
//	*/
func _walFrames(tls *libc.TLS, pWal uintptr, szPage int32, pList uintptr, nTruncate TPgno, isCommit int32, sync_flags int32) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var bSync, nDbSize, nExtra, rc, sectorSize, szFrame, v1 int32
	var iFirst, iFrame Tu32
	var iOff, iOffset, sz Ti64
	var p, pData, pLast, pLive, v3 uintptr
	var v4 uint32
	var _ /* aCksum at bp+56 */ [2]Tu32
	var _ /* aWalHdr at bp+24 */ [32]Tu8
	var _ /* iWrite at bp+64 */ Tu32
	var _ /* w at bp+0 */ TWalWriter
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bSync, iFirst, iFrame, iOff, iOffset, nDbSize, nExtra, p, pData, pLast, pLive, rc, sectorSize, sz, szFrame, v1, v3, v4 /* Iterator to run through pList with. */
	pLast = uintptr(0)                                                                                                                                                            /* Last frame in list */
	nExtra = 0                                                                                                                                                                    /* The writer */
	iFirst = uint32(0)                                                                                                                                                            /* Pointer to shared header */
	/* If this frame set completes a transaction, then nTruncate>0.  If
	 ** nTruncate==0 then this frame set does not complete the transaction. */
	pLive = _walIndexHdr(tls, pWal)
	if libc.Xmemcmp(tls, pWal+52, pLive, uint32(48)) != 0 {
		iFirst = (*TWalIndexHdr)(unsafe.Pointer(pLive)).FmxFrame + uint32(1)
	}
	/* See if it is possible to write these frames into the start of the
	 ** log file, instead of appending to it at pWal->hdr.mxFrame.
	 */
	v1 = _walRestartLog(tls, pWal)
	rc = v1
	if SQLITE_OK != v1 {
		return rc
	}
	/* If this is the first frame written into the log, write the WAL
	 ** header to the start of the WAL file. See comments at the top of
	 ** this source file for a description of the WAL header format.
	 */
	iFrame = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame
	if iFrame == uint32(0) { /* Checksum for wal-header */
		_sqlite3Put4byte(tls, bp+24, uint32(libc.Int32FromInt32(WAL_MAGIC)|libc.Int32FromInt32(SQLITE_BIGENDIAN)))
		_sqlite3Put4byte(tls, bp+24+4, uint32(WAL_MAX_VERSION))
		_sqlite3Put4byte(tls, bp+24+8, uint32(szPage))
		_sqlite3Put4byte(tls, bp+24+12, (*TWal)(unsafe.Pointer(pWal)).FnCkpt)
		if (*TWal)(unsafe.Pointer(pWal)).FnCkpt == uint32(0) {
			Xsqlite3_randomness(tls, int32(8), pWal+52+32)
		}
		libc.Xmemcpy(tls, bp+24+16, pWal+52+32, uint32(8))
		_walChecksumBytes(tls, int32(1), bp+24, libc.Int32FromInt32(WAL_HDRSIZE)-libc.Int32FromInt32(2)*libc.Int32FromInt32(4), uintptr(0), bp+56)
		_sqlite3Put4byte(tls, bp+24+24, (**(**[2]Tu32)(__ccgo_up(bp + 56)))[0])
		_sqlite3Put4byte(tls, bp+24+28, (**(**[2]Tu32)(__ccgo_up(bp + 56)))[int32(1)])
		(*TWal)(unsafe.Pointer(pWal)).FszPage = uint32(szPage)
		(*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum = uint8(SQLITE_BIGENDIAN)
		**(**Tu32)(__ccgo_up(pWal + 52 + 24)) = (**(**[2]Tu32)(__ccgo_up(bp + 56)))[0]
		**(**Tu32)(__ccgo_up(pWal + 52 + 24 + 1*4)) = (**(**[2]Tu32)(__ccgo_up(bp + 56)))[int32(1)]
		(*TWal)(unsafe.Pointer(pWal)).FtruncateOnCommit = uint8(1)
		rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp+24, int32(32), 0)
		if rc != SQLITE_OK {
			return rc
		}
		/* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
		 ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
		 ** an out-of-order write following a WAL restart could result in
		 ** database corruption.  See the ticket:
		 **
		 **     https://sqlite.org/src/info/ff5be73dee
		 */
		if (*TWal)(unsafe.Pointer(pWal)).FsyncHeader != 0 {
			rc = _sqlite3OsSync(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, sync_flags>>int32(2)&int32(0x03))
			if rc != 0 {
				return rc
			}
		}
	}
	if int32((*TWal)(unsafe.Pointer(pWal)).FszPage) != szPage {
		return _sqlite3CorruptError(tls, int32(71646)) /* TH3 test case: cov1/corrupt155.test */
	}
	/* Setup information needed to write frames into the WAL */
	(**(**TWalWriter)(__ccgo_up(bp))).FpWal = pWal
	(**(**TWalWriter)(__ccgo_up(bp))).FpFd = (*TWal)(unsafe.Pointer(pWal)).FpWalFd
	(**(**TWalWriter)(__ccgo_up(bp))).FiSyncPoint = 0
	(**(**TWalWriter)(__ccgo_up(bp))).FsyncFlags = sync_flags
	(**(**TWalWriter)(__ccgo_up(bp))).FszPage = szPage
	iOffset = libc.Int64FromInt32(WAL_HDRSIZE) + int64(iFrame+libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE))
	szFrame = szPage + int32(WAL_FRAME_HDRSIZE)
	/* Write all frames into the log file exactly once */
	p = pList
	for {
		if !(p != 0) {
			break
		} /* 0 normally.  Positive == commit flag */
		/* Check if this page has already been written into the wal file by
		 ** the current transaction. If so, overwrite the existing frame and
		 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
		 ** checksums must be recomputed when the transaction is committed.  */
		if iFirst != 0 && ((*TPgHdr)(unsafe.Pointer(p)).FpDirty != 0 || isCommit == 0) {
			**(**Tu32)(__ccgo_up(bp + 64)) = uint32(0)
			_walFindFrame(tls, pWal, (*TPgHdr)(unsafe.Pointer(p)).Fpgno, bp+64)
			if **(**Tu32)(__ccgo_up(bp + 64)) >= iFirst {
				iOff = int64(WAL_HDRSIZE) + int64(**(**Tu32)(__ccgo_up(bp + 64))-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) + int64(WAL_FRAME_HDRSIZE)
				if (*TWal)(unsafe.Pointer(pWal)).FiReCksum == uint32(0) || **(**Tu32)(__ccgo_up(bp + 64)) < (*TWal)(unsafe.Pointer(pWal)).FiReCksum {
					(*TWal)(unsafe.Pointer(pWal)).FiReCksum = **(**Tu32)(__ccgo_up(bp + 64))
				}
				pData = (*TPgHdr)(unsafe.Pointer(p)).FpData
				rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, pData, szPage, iOff)
				if rc != 0 {
					return rc
				}
				v3 = p + 28
				*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) & ^libc.Int32FromInt32(PGHDR_WAL_APPEND))
				goto _2
			}
		}
		iFrame = iFrame + 1
		if isCommit != 0 && (*TPgHdr)(unsafe.Pointer(p)).FpDirty == uintptr(0) {
			v4 = nTruncate
		} else {
			v4 = uint32(0)
		}
		nDbSize = int32(v4)
		rc = _walWriteOneFrame(tls, bp, p, nDbSize, iOffset)
		if rc != 0 {
			return rc
		}
		pLast = p
		iOffset = iOffset + int64(szFrame)
		v3 = p + 28
		*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | libc.Int32FromInt32(PGHDR_WAL_APPEND))
		goto _2
	_2:
		;
		p = (*TPgHdr)(unsafe.Pointer(p)).FpDirty
	}
	/* Recalculate checksums within the wal file if required. */
	if isCommit != 0 && (*TWal)(unsafe.Pointer(pWal)).FiReCksum != 0 {
		rc = _walRewriteChecksums(tls, pWal, iFrame)
		if rc != 0 {
			return rc
		}
	}
	/* If this is the end of a transaction, then we might need to pad
	 ** the transaction and/or sync the WAL file.
	 **
	 ** Padding and syncing only occur if this set of frames complete a
	 ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
	 ** or synchronous==OFF, then no padding or syncing are needed.
	 **
	 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
	 ** needed and only the sync is done.  If padding is needed, then the
	 ** final frame is repeated (with its commit mark) until the next sector
	 ** boundary is crossed.  Only the part of the WAL prior to the last
	 ** sector boundary is synced; the part of the last frame that extends
	 ** past the sector boundary is written after the sync.
	 */
	if isCommit != 0 && sync_flags&int32(0x03) != 0 {
		bSync = int32(1)
		if (*TWal)(unsafe.Pointer(pWal)).FpadToSectorBoundary != 0 {
			sectorSize = _sqlite3SectorSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd)
			(**(**TWalWriter)(__ccgo_up(bp))).FiSyncPoint = (iOffset + int64(sectorSize) - int64(1)) / int64(sectorSize) * int64(sectorSize)
			bSync = libc.BoolInt32((**(**TWalWriter)(__ccgo_up(bp))).FiSyncPoint == iOffset)
			for iOffset < (**(**TWalWriter)(__ccgo_up(bp))).FiSyncPoint {
				rc = _walWriteOneFrame(tls, bp, pLast, int32(nTruncate), iOffset)
				if rc != 0 {
					return rc
				}
				iOffset = iOffset + int64(szFrame)
				nExtra = nExtra + 1
			}
		}
		if bSync != 0 {
			rc = _sqlite3OsSync(tls, (**(**TWalWriter)(__ccgo_up(bp))).FpFd, sync_flags&int32(0x03))
		}
	}
	/* If this frame set completes the first transaction in the WAL and
	 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
	 ** journal size limit, if possible.
	 */
	if isCommit != 0 && (*TWal)(unsafe.Pointer(pWal)).FtruncateOnCommit != 0 && (*TWal)(unsafe.Pointer(pWal)).FmxWalSize >= 0 {
		sz = (*TWal)(unsafe.Pointer(pWal)).FmxWalSize
		if int64(WAL_HDRSIZE)+int64(iFrame+uint32(nExtra)+libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) > (*TWal)(unsafe.Pointer(pWal)).FmxWalSize {
			sz = libc.Int64FromInt32(WAL_HDRSIZE) + int64(iFrame+uint32(nExtra)+libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE))
		}
		_walLimitSize(tls, pWal, sz)
		(*TWal)(unsafe.Pointer(pWal)).FtruncateOnCommit = uint8(0)
	}
	/* Append data to the wal-index. It is not necessary to lock the
	 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
	 ** guarantees that there are no other writers, and no data that may
	 ** be in use by existing readers is being overwritten.
	 */
	iFrame = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame
	p = pList
	for {
		if !(p != 0 && rc == SQLITE_OK) {
			break
		}
		if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&int32(PGHDR_WAL_APPEND) == 0 {
			goto _6
		}
		iFrame = iFrame + 1
		rc = _walIndexAppend(tls, pWal, iFrame, (*TPgHdr)(unsafe.Pointer(p)).Fpgno)
		goto _6
	_6:
		;
		p = (*TPgHdr)(unsafe.Pointer(p)).FpDirty
	}
	for rc == SQLITE_OK && nExtra > 0 {
		iFrame = iFrame + 1
		nExtra = nExtra - 1
		rc = _walIndexAppend(tls, pWal, iFrame, (*TPgHdr)(unsafe.Pointer(pLast)).Fpgno)
	}
	if rc == SQLITE_OK {
		/* Update the private copy of the header. */
		(*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage = uint16(szPage&libc.Int32FromInt32(0xff00) | szPage>>libc.Int32FromInt32(16))
		(*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame = iFrame
		if isCommit != 0 {
			(*TWal)(unsafe.Pointer(pWal)).Fhdr.FiChange = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FiChange + 1
			(*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage = nTruncate
		}
		/* If this is a commit, update the wal-index header too. */
		if isCommit != 0 {
			_walIndexWriteHdr(tls, pWal)
			(*TWal)(unsafe.Pointer(pWal)).FiCallback = iFrame
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Return pointers to the hash table and page number array stored on
//	** page iHash of the wal-index. The wal-index is broken into 32KB pages
//	** numbered starting from 0.
//	**
//	** Set output variable pLoc->aHash to point to the start of the hash table
//	** in the wal-index file. Set pLoc->iZero to one less than the frame
//	** number of the first frame indexed by this hash table. If a
//	** slot in the hash table is set to N, it refers to frame number
//	** (pLoc->iZero+N) in the log.
//	**
//	** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
//	** first frame indexed by the hash table, frame (pLoc->iZero).
//	*/
func _walHashGet(tls *libc.TLS, pWal uintptr, iHash int32, pLoc uintptr) (r int32) {
	var rc int32
	_ = rc /* Return code */
	rc = _walIndexPage(tls, pWal, iHash, pLoc+4)
	if (*TWalHashLoc)(unsafe.Pointer(pLoc)).FaPgno != 0 {
		(*TWalHashLoc)(unsafe.Pointer(pLoc)).FaHash = (*TWalHashLoc)(unsafe.Pointer(pLoc)).FaPgno + 4096*4
		if iHash == 0 {
			(*TWalHashLoc)(unsafe.Pointer(pLoc)).FaPgno = (*TWalHashLoc)(unsafe.Pointer(pLoc)).FaPgno + uintptr((libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4))*4
			(*TWalHashLoc)(unsafe.Pointer(pLoc)).FiZero = uint32(0)
		} else {
			(*TWalHashLoc)(unsafe.Pointer(pLoc)).FiZero = libc.Uint32FromInt32(HASHTABLE_NPAGE) - (libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4) + uint32((iHash-int32(1))*int32(HASHTABLE_NPAGE))
		}
	} else {
		if rc == SQLITE_OK {
			rc = int32(SQLITE_ERROR)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Set an entry in the wal-index that will map database page number
//	** pPage into WAL frame iFrame.
//	*/
func _walIndexAppend(tls *libc.TLS, pWal uintptr, iFrame Tu32, iPage Tu32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iKey, idx, nByte, nCollide, rc, v2 int32
	var _ /* sLoc at bp+0 */ TWalHashLoc
	_, _, _, _, _, _ = iKey, idx, nByte, nCollide, rc, v2 /* Wal-index hash table location */
	rc = _walHashGet(tls, pWal, _walFramePage(tls, iFrame), bp)
	/* Assuming the wal-index file was successfully mapped, populate the
	 ** page number array and hash table entry.
	 */
	if rc == SQLITE_OK { /* Number of hash collisions */
		idx = int32(iFrame - (**(**TWalHashLoc)(__ccgo_up(bp))).FiZero)
		/* If this is the first entry to be added to this hash-table, zero the
		 ** entire hash table and aPgno[] array before proceeding.
		 */
		if idx == int32(1) {
			nByte = int32((**(**TWalHashLoc)(__ccgo_up(bp))).FaHash+uintptr(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))*2) - int32((**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno)
			libc.Xmemset(tls, (**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno, 0, uint32(nByte))
		}
		/* If the entry in aPgno[] is already set, then the previous writer
		 ** must have exited unexpectedly in the middle of a transaction (after
		 ** writing one or more dirty pages to the WAL to free up memory).
		 ** Remove the remnants of that writers uncommitted transaction from
		 ** the hash-table before writing any new entries.
		 */
		if **(**Tu32)(__ccgo_up((**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno + uintptr(idx-int32(1))*4)) != 0 {
			_walCleanupHash(tls, pWal)
		}
		/* Write the aPgno[] array entry and the hash-table slot. */
		nCollide = idx
		iKey = _walHash(tls, iPage)
		for {
			if !(**(**Tht_slot)(__ccgo_up((**(**TWalHashLoc)(__ccgo_up(bp))).FaHash + uintptr(iKey)*2)) != 0) {
				break
			}
			v2 = nCollide
			nCollide = nCollide - 1
			if v2 == 0 {
				return _sqlite3CorruptError(tls, int32(68860))
			}
			goto _1
		_1:
			;
			iKey = _walNextHash(tls, iKey)
		}
		**(**Tu32)(__ccgo_up((**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno + uintptr((idx-int32(1))&(libc.Int32FromInt32(HASHTABLE_NPAGE)-libc.Int32FromInt32(1)))*4)) = iPage
		libc.AtomicStoreNUint16((**(**TWalHashLoc)(__ccgo_up(bp))).FaHash+uintptr(iKey)*2, uint16(idx), libc.Int32FromInt32(__ATOMIC_RELAXED))
	}
	return rc
}

// C documentation
//
//	/*
//	** Close an open wal-index.
//	*/
func _walIndexClose(tls *libc.TLS, pWal uintptr, isDelete int32) {
	var i int32
	_ = i
	if int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) == int32(WAL_HEAPMEMORY_MODE) || (*TWal)(unsafe.Pointer(pWal)).FbShmUnreliable != 0 {
		i = 0
		for {
			if !(i < (*TWal)(unsafe.Pointer(pWal)).FnWiData) {
				break
			}
			Xsqlite3_free(tls, **(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(i)*4)))
			**(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(i)*4)) = uintptr(0)
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	if int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) != int32(WAL_HEAPMEMORY_MODE) {
		_sqlite3OsShmUnmap(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, isDelete)
	}
}

// C documentation
//
//	/*
//	** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
//	** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
//	** numbered from zero.
//	**
//	** If the wal-index is currently smaller the iPage pages then the size
//	** of the wal-index might be increased, but only if it is safe to do
//	** so.  It is safe to enlarge the wal-index if pWal->writeLock is true
//	** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
//	**
//	** Three possible result scenarios:
//	**
//	**   (1)  rc==SQLITE_OK    and *ppPage==Requested-Wal-Index-Page
//	**   (2)  rc>=SQLITE_ERROR and *ppPage==NULL
//	**   (3)  rc==SQLITE_OK    and *ppPage==NULL  // only if iPage==0
//	**
//	** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
//	*/
func _walIndexPageRealloc(tls *libc.TLS, pWal uintptr, iPage int32, ppPage uintptr) (r int32) {
	var apNew, v1 uintptr
	var nByte Tsqlite3_int64
	var rc int32
	_, _, _, _ = apNew, nByte, rc, v1
	rc = SQLITE_OK
	/* Enlarge the pWal->apWiData[] array if required */
	if (*TWal)(unsafe.Pointer(pWal)).FnWiData <= iPage {
		nByte = int64(4) * (int64(1) + int64(iPage))
		apNew = _sqlite3Realloc(tls, (*TWal)(unsafe.Pointer(pWal)).FapWiData, uint64(nByte))
		if !(apNew != 0) {
			**(**uintptr)(__ccgo_up(ppPage)) = uintptr(0)
			return int32(SQLITE_NOMEM)
		}
		libc.Xmemset(tls, apNew+uintptr((*TWal)(unsafe.Pointer(pWal)).FnWiData)*4, 0, uint32(4)*uint32(iPage+libc.Int32FromInt32(1)-(*TWal)(unsafe.Pointer(pWal)).FnWiData))
		(*TWal)(unsafe.Pointer(pWal)).FapWiData = apNew
		(*TWal)(unsafe.Pointer(pWal)).FnWiData = iPage + int32(1)
	}
	/* Request a pointer to the required page from the VFS */
	if int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) == int32(WAL_HEAPMEMORY_MODE) {
		**(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*4)) = _sqlite3MallocZero(tls, uint64(libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4)))
		if !(**(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*4)) != 0) {
			rc = int32(SQLITE_NOMEM)
		}
	} else {
		rc = _sqlite3OsShmMap(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, iPage, int32(libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4)), int32((*TWal)(unsafe.Pointer(pWal)).FwriteLock), (*TWal)(unsafe.Pointer(pWal)).FapWiData+uintptr(iPage)*4)
		if rc == SQLITE_OK {
			if iPage > 0 && _sqlite3FaultSim(tls, int32(600)) != 0 {
				rc = int32(SQLITE_NOMEM)
			}
		} else {
			if rc&int32(0xff) == int32(SQLITE_READONLY) {
				v1 = pWal + 46
				*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(WAL_SHM_RDONLY))
				if rc == int32(SQLITE_READONLY) {
					rc = SQLITE_OK
				}
			}
		}
	}
	**(**uintptr)(__ccgo_up(ppPage)) = **(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*4))
	return rc
}

// C documentation
//
//	/*
//	** Recover the wal-index by reading the write-ahead log file.
//	**
//	** This routine first tries to establish an exclusive lock on the
//	** wal-index to prevent other threads/processes from doing anything
//	** with the WAL or wal-index while recovery is running.  The
//	** WAL_RECOVER_LOCK is also held so that other threads will know
//	** that this thread is running recovery.  If unable to establish
//	** the necessary locks, this routine returns SQLITE_BUSY.
//	*/
func _walIndexRecover(tls *libc.TLS, pWal uintptr) (r int32) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var aData, aFrame, aPrivate, pInfo uintptr
	var aFrameCksum [2]Tu32
	var i, iLock, isValid, rc, szFrame, szPage int32
	var iFirst, iFrame, iLast, iLastFrame, iPg, magic, nHdr, nHdr32, version Tu32
	var iOffset Ti64
	var v2, v3 uint32
	var _ /* aBuf at bp+8 */ [32]Tu8
	var _ /* aShare at bp+40 */ uintptr
	var _ /* nSize at bp+0 */ Ti64
	var _ /* nTruncate at bp+48 */ Tu32
	var _ /* pgno at bp+44 */ Tu32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aData, aFrame, aFrameCksum, aPrivate, i, iFirst, iFrame, iLast, iLastFrame, iLock, iOffset, iPg, isValid, magic, nHdr, nHdr32, pInfo, rc, szFrame, szPage, version, v2, v3 /* Size of log file */
	aFrameCksum = [2]Tu32{}                                                                                                                                                                                                                          /* Lock offset to lock for checkpoint */
	/* Obtain an exclusive lock on all byte in the locking range not already
	 ** locked by the caller. The caller is guaranteed to have locked the
	 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
	 ** If successful, the same bytes that are locked here are unlocked before
	 ** this function returns.
	 */
	iLock = int32(WAL_ALL_BUT_WRITE) + int32((*TWal)(unsafe.Pointer(pWal)).FckptLock)
	rc = _walLockExclusive(tls, pWal, iLock, libc.Int32FromInt32(3)+libc.Int32FromInt32(0)-iLock)
	if rc != 0 {
		return rc
	}
	libc.Xmemset(tls, pWal+52, 0, uint32(48))
	rc = _sqlite3OsFileSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp)
	if rc != SQLITE_OK {
		goto recovery_error
	}
	if **(**Ti64)(__ccgo_up(bp)) > int64(WAL_HDRSIZE) { /* Buffer to load WAL header into */
		aPrivate = uintptr(0) /* Heap copy of *-shm hash being populated */
		aFrame = uintptr(0)   /* Last frame in wal, based on nSize alone */
		/* Read in the WAL header. */
		rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp+8, int32(WAL_HDRSIZE), 0)
		if rc != SQLITE_OK {
			goto recovery_error
		}
		/* If the database page size is not a power of two, or is greater than
		 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
		 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
		 ** WAL file.
		 */
		magic = _sqlite3Get4byte(tls, bp+8)
		szPage = int32(_sqlite3Get4byte(tls, bp+8+8))
		if magic&uint32(0xFFFFFFFE) != uint32(WAL_MAGIC) || szPage&(szPage-int32(1)) != 0 || szPage > int32(SQLITE_MAX_PAGE_SIZE) || szPage < int32(512) {
			goto finished
		}
		(*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum = uint8(magic & libc.Uint32FromInt32(0x00000001))
		(*TWal)(unsafe.Pointer(pWal)).FszPage = uint32(szPage)
		(*TWal)(unsafe.Pointer(pWal)).FnCkpt = _sqlite3Get4byte(tls, bp+8+12)
		libc.Xmemcpy(tls, pWal+52+32, bp+8+16, uint32(8))
		/* Verify that the WAL header checksum is correct */
		_walChecksumBytes(tls, libc.BoolInt32(int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum) == SQLITE_BIGENDIAN), bp+8, libc.Int32FromInt32(WAL_HDRSIZE)-libc.Int32FromInt32(2)*libc.Int32FromInt32(4), uintptr(0), pWal+52+24)
		if **(**Tu32)(__ccgo_up(pWal + 52 + 24)) != _sqlite3Get4byte(tls, bp+8+24) || **(**Tu32)(__ccgo_up(pWal + 52 + 24 + 1*4)) != _sqlite3Get4byte(tls, bp+8+28) {
			goto finished
		}
		/* Verify that the version number on the WAL format is one that
		 ** are able to understand */
		version = _sqlite3Get4byte(tls, bp+8+4)
		if version != uint32(WAL_MAX_VERSION) {
			rc = _sqlite3CantopenError(tls, int32(68992))
			goto finished
		}
		/* Malloc a buffer to read frames into. */
		szFrame = szPage + int32(WAL_FRAME_HDRSIZE)
		aFrame = Xsqlite3_malloc64(tls, uint64(uint32(szFrame)+(libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4))))
		if !(aFrame != 0) {
			rc = int32(SQLITE_NOMEM)
			goto recovery_error
		}
		aData = aFrame + 24
		aPrivate = aData + uintptr(szPage)
		/* Read all frames from the log file. */
		iLastFrame = uint32((**(**Ti64)(__ccgo_up(bp)) - int64(WAL_HDRSIZE)) / int64(szFrame))
		iPg = uint32(0)
		for {
			if !(iPg <= uint32(_walFramePage(tls, iLastFrame))) {
				break
			}
			if iLastFrame < libc.Uint32FromInt32(HASHTABLE_NPAGE)-(libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4)+iPg*uint32(HASHTABLE_NPAGE) {
				v2 = iLastFrame
			} else {
				v2 = libc.Uint32FromInt32(HASHTABLE_NPAGE) - (libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4) + iPg*uint32(HASHTABLE_NPAGE)
			} /* Index of last frame read */
			iLast = v2
			if iPg == uint32(0) {
				v3 = uint32(0)
			} else {
				v3 = libc.Uint32FromInt32(HASHTABLE_NPAGE) - (libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2)+libc.Uint32FromInt64(40))/libc.Uint32FromInt64(4) + (iPg-uint32(1))*uint32(HASHTABLE_NPAGE)
			}
			iFirst = uint32(1) + v3
			rc = _walIndexPage(tls, pWal, int32(iPg), bp+40)
			if **(**uintptr)(__ccgo_up(bp + 40)) == uintptr(0) {
				break
			}
			**(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPg)*4)) = aPrivate
			iFrame = iFirst
			for {
				if !(iFrame <= iLast) {
					break
				}
				iOffset = libc.Int64FromInt32(WAL_HDRSIZE) + int64(iFrame-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) /* dbsize field from frame header */
				/* Read and decode the next log frame. */
				rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, aFrame, szFrame, iOffset)
				if rc != SQLITE_OK {
					break
				}
				isValid = _walDecodeFrame(tls, pWal, bp+44, bp+48, aData, aFrame)
				if !(isValid != 0) {
					break
				}
				rc = _walIndexAppend(tls, pWal, iFrame, **(**Tu32)(__ccgo_up(bp + 44)))
				if rc != SQLITE_OK {
					break
				}
				/* If nTruncate is non-zero, this is a commit record. */
				if **(**Tu32)(__ccgo_up(bp + 48)) != 0 {
					(*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame = iFrame
					(*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage = **(**Tu32)(__ccgo_up(bp + 48))
					(*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage = uint16(szPage&libc.Int32FromInt32(0xff00) | szPage>>libc.Int32FromInt32(16))
					aFrameCksum[0] = **(**Tu32)(__ccgo_up(pWal + 52 + 24))
					aFrameCksum[int32(1)] = **(**Tu32)(__ccgo_up(pWal + 52 + 24 + 1*4))
				}
				goto _4
			_4:
				;
				iFrame = iFrame + 1
			}
			**(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPg)*4)) = **(**uintptr)(__ccgo_up(bp + 40))
			if iPg == uint32(0) {
				v2 = libc.Uint32FromInt64(48)*libc.Uint32FromInt32(2) + libc.Uint32FromInt64(40)
			} else {
				v2 = uint32(0)
			}
			nHdr = v2
			nHdr32 = nHdr / uint32(4)
			/* Memcpy() should work fine here, on all reasonable implementations.
			 ** Technically, memcpy() might change the destination to some
			 ** intermediate value before setting to the final value, and that might
			 ** cause a concurrent reader to malfunction.  Memcpy() is allowed to
			 ** do that, according to the spec, but no memcpy() implementation that
			 ** we know of actually does that, which is why we say that memcpy()
			 ** is safe for this.  Memcpy() is certainly a lot faster.
			 */
			libc.Xmemcpy(tls, **(**uintptr)(__ccgo_up(bp + 40))+uintptr(nHdr32)*4, aPrivate+uintptr(nHdr32)*4, libc.Uint32FromInt64(2)*uint32(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint32FromInt32(HASHTABLE_NPAGE)*libc.Uint32FromInt64(4)-nHdr)
			if iFrame <= iLast {
				break
			}
			goto _1
		_1:
			;
			iPg = iPg + 1
		}
		Xsqlite3_free(tls, aFrame)
	}
	goto finished
finished:
	;
	if rc == SQLITE_OK {
		**(**Tu32)(__ccgo_up(pWal + 52 + 24)) = aFrameCksum[0]
		**(**Tu32)(__ccgo_up(pWal + 52 + 24 + 1*4)) = aFrameCksum[int32(1)]
		_walIndexWriteHdr(tls, pWal)
		/* Reset the checkpoint-header. This is safe because this thread is
		 ** currently holding locks that exclude all other writers and
		 ** checkpointers. Then set the values of read-mark slots 1 through N.
		 */
		pInfo = _walCkptInfo(tls, pWal)
		(*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfill = uint32(0)
		(*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame
		**(**Tu32)(__ccgo_up(pInfo + 4)) = uint32(0)
		i = int32(1)
		for {
			if !(i < libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)) {
				break
			}
			rc = _walLockExclusive(tls, pWal, int32(3)+i, int32(1))
			if rc == SQLITE_OK {
				if i == int32(1) && (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame != 0 {
					**(**Tu32)(__ccgo_up(pInfo + 4 + uintptr(i)*4)) = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame
				} else {
					**(**Tu32)(__ccgo_up(pInfo + 4 + uintptr(i)*4)) = uint32(READMARK_NOT_USED)
				}
				_walUnlockExclusive(tls, pWal, int32(3)+i, int32(1))
			} else {
				if rc != int32(SQLITE_BUSY) {
					goto recovery_error
				}
			}
			goto _6
		_6:
			;
			i = i + 1
		}
		/* If more than one frame was recovered from the log file, report an
		 ** event via sqlite3_log(). This is to help with identifying performance
		 ** problems caused by applications routinely shutting down without
		 ** checkpointing the log file.
		 */
		if (*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage != 0 {
			Xsqlite3_log(tls, libc.Int32FromInt32(SQLITE_NOTICE)|libc.Int32FromInt32(1)<<libc.Int32FromInt32(8), __ccgo_ts+5493, libc.VaList(bp+64, (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame, (*TWal)(unsafe.Pointer(pWal)).FzWalName))
		}
	}
	goto recovery_error
recovery_error:
	;
	_walUnlockExclusive(tls, pWal, iLock, libc.Int32FromInt32(3)+libc.Int32FromInt32(0)-iLock)
	return rc
}

// C documentation
//
//	/*
//	** Try to read the wal-index header.  Return 0 on success and 1 if
//	** there is a problem.
//	**
//	** The wal-index is in shared memory.  Another thread or process might
//	** be writing the header at the same time this procedure is trying to
//	** read it, which might result in inconsistency.  A dirty read is detected
//	** by verifying that both copies of the header are the same and also by
//	** a checksum on the header.
//	**
//	** If and only if the read is consistent and the header is different from
//	** pWal->hdr, then pWal->hdr is updated to the content of the new header
//	** and *pChanged is set to 1.
//	**
//	** If the checksum cannot be verified return non-zero. If the header
//	** is read successfully and the checksum verified, return zero.
//	*/
func _walIndexTryHdr(tls *libc.TLS, pWal uintptr, pChanged uintptr) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var aHdr uintptr
	var _ /* aCksum at bp+0 */ [2]Tu32
	var _ /* h1 at bp+8 */ TWalIndexHdr
	var _ /* h2 at bp+56 */ TWalIndexHdr
	_ = aHdr /* Header in shared memory */
	/* The first page of the wal-index must be mapped at this point. */
	/* Read the header. This might happen concurrently with a write to the
	 ** same area of shared memory on a different CPU in a SMP,
	 ** meaning it is possible that an inconsistent snapshot is read
	 ** from the file. If this happens, return non-zero.
	 **
	 ** tag-20200519-1:
	 ** There are two copies of the header at the beginning of the wal-index.
	 ** When reading, read [0] first then [1].  Writes are in the reverse order.
	 ** Memory barriers are used to prevent the compiler or the hardware from
	 ** reordering the reads and writes.  TSAN and similar tools can sometimes
	 ** give false-positive warnings about these accesses because the tools do not
	 ** account for the double-read and the memory barrier. The use of mutexes
	 ** here would be problematic as the memory being accessed is potentially
	 ** shared among multiple processes and not all mutex implementations work
	 ** reliably in that environment.
	 */
	aHdr = _walIndexHdr(tls, pWal)
	libc.Xmemcpy(tls, bp+8, aHdr, uint32(48)) /* Possible TSAN false-positive */
	_walShmBarrier(tls, pWal)
	libc.Xmemcpy(tls, bp+56, aHdr+1*48, uint32(48))
	if libc.Xmemcmp(tls, bp+8, bp+56, uint32(48)) != 0 {
		return int32(1) /* Dirty read */
	}
	if int32((**(**TWalIndexHdr)(__ccgo_up(bp + 8))).FisInit) == 0 {
		return int32(1) /* Malformed header - probably all zeros */
	}
	_walChecksumBytes(tls, int32(1), bp+8, int32(libc.Uint32FromInt64(48)-libc.Uint32FromInt64(8)), uintptr(0), bp)
	if (**(**[2]Tu32)(__ccgo_up(bp)))[0] != **(**Tu32)(__ccgo_up(bp + 8 + 40)) || (**(**[2]Tu32)(__ccgo_up(bp)))[int32(1)] != **(**Tu32)(__ccgo_up(bp + 8 + 40 + 1*4)) {
		return int32(1) /* Checksum does not match */
	}
	if libc.Xmemcmp(tls, pWal+52, bp+8, uint32(48)) != 0 {
		**(**int32)(__ccgo_up(pChanged)) = int32(1)
		libc.Xmemcpy(tls, pWal+52, bp+8, uint32(48))
		(*TWal)(unsafe.Pointer(pWal)).FszPage = uint32(int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage)&int32(0xfe00) + int32((*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage)&int32(0x0001)<<int32(16))
	}
	/* The header was successfully read. Return zero. */
	return 0
}

/*
** This is the value that walTryBeginRead returns when it needs to
** be retried.
 */

// C documentation
//
//	/*
//	** Write the header information in pWal->hdr into the wal-index.
//	**
//	** The checksum on pWal->hdr is updated before it is written.
//	*/
func _walIndexWriteHdr(tls *libc.TLS, pWal uintptr) {
	var aHdr uintptr
	var nCksum int32
	_, _ = aHdr, nCksum
	aHdr = _walIndexHdr(tls, pWal)
	nCksum = int32(uint32(libc.UintptrFromInt32(0) + 40))
	(*TWal)(unsafe.Pointer(pWal)).Fhdr.FisInit = uint8(1)
	(*TWal)(unsafe.Pointer(pWal)).Fhdr.FiVersion = uint32(WALINDEX_MAX_VERSION)
	_walChecksumBytes(tls, int32(1), pWal+52, nCksum, uintptr(0), pWal+52+40)
	/* Possible TSAN false-positive.  See tag-20200519-1 */
	libc.Xmemcpy(tls, aHdr+1*48, pWal+52, uint32(48))
	_walShmBarrier(tls, pWal)
	libc.Xmemcpy(tls, aHdr, pWal+52, uint32(48))
}

// C documentation
//
//	/*
//	** Construct a WalInterator object that can be used to loop over all
//	** pages in the WAL following frame nBackfill in ascending order. Frames
//	** nBackfill or earlier may be included - excluding them is an optimization
//	** only. The caller must hold the checkpoint lock.
//	**
//	** On success, make *pp point to the newly allocated WalInterator object
//	** return SQLITE_OK. Otherwise, return an error code. If this routine
//	** returns an error, the value of *pp is undefined.
//	**
//	** The calling routine should invoke walIteratorFree() to destroy the
//	** WalIterator object when it has finished with it.
//	*/
func _walIteratorInit(tls *libc.TLS, pWal uintptr, nBackfill Tu32, pp uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aIndex, aTmp, p uintptr
	var i, j, nSegment, rc int32
	var iLast Tu32
	var nByte Tsqlite3_int64
	var v1 uint32
	var _ /* nEntry at bp+12 */ int32
	var _ /* sLoc at bp+0 */ TWalHashLoc
	_, _, _, _, _, _, _, _, _, _ = aIndex, aTmp, i, iLast, j, nByte, nSegment, p, rc, v1 /* Temp space used by merge-sort */
	rc = SQLITE_OK                                                                       /* Return Code */
	/* This routine only runs while holding the checkpoint lock. And
	 ** it only runs if there is actually content in the log (mxFrame>0).
	 */
	iLast = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame
	/* Allocate space for the WalIterator object. */
	nSegment = _walFramePage(tls, iLast) + int32(1)
	nByte = int64(uint32(libc.UintptrFromInt32(0)+8) + uint32(nSegment)*uint32(20) + iLast*uint32(2))
	if iLast > uint32(HASHTABLE_NPAGE) {
		v1 = uint32(HASHTABLE_NPAGE)
	} else {
		v1 = iLast
	}
	p = Xsqlite3_malloc64(tls, uint64(nByte+int64(uint32(2)*v1)))
	if !(p != 0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemset(tls, p, 0, uint32(nByte))
	(*TWalIterator)(unsafe.Pointer(p)).FnSegment = nSegment
	aTmp = p + uintptr(nByte)
	i = _walFramePage(tls, nBackfill+uint32(1))
	for {
		if !(rc == SQLITE_OK && i < nSegment) {
			break
		}
		rc = _walHashGet(tls, pWal, i, bp)
		if rc == SQLITE_OK { /* Sorted index for this segment */
			if i+int32(1) == nSegment {
				**(**int32)(__ccgo_up(bp + 12)) = int32(iLast - (**(**TWalHashLoc)(__ccgo_up(bp))).FiZero)
			} else {
				**(**int32)(__ccgo_up(bp + 12)) = (int32((**(**TWalHashLoc)(__ccgo_up(bp))).FaHash) - int32((**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno)) / 4
			}
			aIndex = p + 8 + uintptr((*TWalIterator)(unsafe.Pointer(p)).FnSegment)*20 + uintptr((**(**TWalHashLoc)(__ccgo_up(bp))).FiZero)*2
			(**(**TWalHashLoc)(__ccgo_up(bp))).FiZero = (**(**TWalHashLoc)(__ccgo_up(bp))).FiZero + 1
			j = 0
			for {
				if !(j < **(**int32)(__ccgo_up(bp + 12))) {
					break
				}
				**(**Tht_slot)(__ccgo_up(aIndex + uintptr(j)*2)) = uint16(j)
				goto _3
			_3:
				;
				j = j + 1
			}
			_walMergesort(tls, (**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno, aTmp, aIndex, bp+12)
			(*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*20))).FiZero = int32((**(**TWalHashLoc)(__ccgo_up(bp))).FiZero)
			(*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*20))).FnEntry = **(**int32)(__ccgo_up(bp + 12))
			(*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*20))).FaIndex = aIndex
			(*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*20))).FaPgno = (**(**TWalHashLoc)(__ccgo_up(bp))).FaPgno
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	if rc != SQLITE_OK {
		_walIteratorFree(tls, p)
		p = uintptr(0)
	}
	**(**uintptr)(__ccgo_up(pp)) = p
	return rc
}

// C documentation
//
//	/*
//	** Find the smallest page number out of all pages held in the WAL that
//	** has not been returned by any prior invocation of this method on the
//	** same WalIterator object.   Write into *piFrame the frame index where
//	** that page was last written into the WAL.  Write into *piPage the page
//	** number.
//	**
//	** Return 0 on success.  If there are no pages in the WAL with a page
//	** number larger than *piPage, then return 1.
//	*/
func _walIteratorNext(tls *libc.TLS, p uintptr, piPage uintptr, piFrame uintptr) (r int32) {
	var i int32
	var iMin, iPg, iRet, v2 Tu32
	var pSegment uintptr
	_, _, _, _, _, _ = i, iMin, iPg, iRet, pSegment, v2 /* Result pgno must be greater than iMin */
	iRet = uint32(0xFFFFFFFF)                           /* For looping through segments */
	iMin = (*TWalIterator)(unsafe.Pointer(p)).FiPrior
	i = (*TWalIterator)(unsafe.Pointer(p)).FnSegment - int32(1)
	for {
		if !(i >= 0) {
			break
		}
		pSegment = p + 8 + uintptr(i)*20
		for (*TWalSegment)(unsafe.Pointer(pSegment)).FiNext < (*TWalSegment)(unsafe.Pointer(pSegment)).FnEntry {
			iPg = **(**Tu32)(__ccgo_up((*TWalSegment)(unsafe.Pointer(pSegment)).FaPgno + uintptr(**(**Tht_slot)(__ccgo_up((*TWalSegment)(unsafe.Pointer(pSegment)).FaIndex + uintptr((*TWalSegment)(unsafe.Pointer(pSegment)).FiNext)*2)))*4))
			if iPg > iMin {
				if iPg < iRet {
					iRet = iPg
					**(**Tu32)(__ccgo_up(piFrame)) = uint32((*TWalSegment)(unsafe.Pointer(pSegment)).FiZero + int32(**(**Tht_slot)(__ccgo_up((*TWalSegment)(unsafe.Pointer(pSegment)).FaIndex + uintptr((*TWalSegment)(unsafe.Pointer(pSegment)).FiNext)*2))))
				}
				break
			}
			(*TWalSegment)(unsafe.Pointer(pSegment)).FiNext = (*TWalSegment)(unsafe.Pointer(pSegment)).FiNext + 1
		}
		goto _1
	_1:
		;
		i = i - 1
	}
	v2 = iRet
	(*TWalIterator)(unsafe.Pointer(p)).FiPrior = v2
	**(**Tu32)(__ccgo_up(piPage)) = v2
	return libc.BoolInt32(iRet == uint32(0xFFFFFFFF))
}

// C documentation
//
//	/*
//	** This function merges two sorted lists into a single sorted list.
//	**
//	** aLeft[] and aRight[] are arrays of indices.  The sort key is
//	** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
//	** is guaranteed for all J<K:
//	**
//	**        aContent[aLeft[J]] < aContent[aLeft[K]]
//	**        aContent[aRight[J]] < aContent[aRight[K]]
//	**
//	** This routine overwrites aRight[] with a new (probably longer) sequence
//	** of indices such that the aRight[] contains every index that appears in
//	** either aLeft[] or the old aRight[] and such that the second condition
//	** above is still met.
//	**
//	** The aContent[aLeft[X]] values will be unique for all X.  And the
//	** aContent[aRight[X]] values will be unique too.  But there might be
//	** one or more combinations of X and Y such that
//	**
//	**      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
//	**
//	** When that happens, omit the aLeft[X] and use the aRight[Y] index.
//	*/
func _walMerge(tls *libc.TLS, aContent uintptr, aLeft uintptr, nLeft int32, paRight uintptr, pnRight uintptr, aTmp uintptr) {
	var aRight uintptr
	var dbpage TPgno
	var iLeft, iOut, iRight, nRight, v1 int32
	var logpage Tht_slot
	_, _, _, _, _, _, _, _ = aRight, dbpage, iLeft, iOut, iRight, logpage, nRight, v1
	iLeft = 0  /* Current index in aLeft */
	iRight = 0 /* Current index in aRight */
	iOut = 0   /* Current index in output buffer */
	nRight = **(**int32)(__ccgo_up(pnRight))
	aRight = **(**uintptr)(__ccgo_up(paRight))
	for iRight < nRight || iLeft < nLeft {
		if iLeft < nLeft && (iRight >= nRight || **(**Tu32)(__ccgo_up(aContent + uintptr(**(**Tht_slot)(__ccgo_up(aLeft + uintptr(iLeft)*2)))*4)) < **(**Tu32)(__ccgo_up(aContent + uintptr(**(**Tht_slot)(__ccgo_up(aRight + uintptr(iRight)*2)))*4))) {
			v1 = iLeft
			iLeft = iLeft + 1
			logpage = **(**Tht_slot)(__ccgo_up(aLeft + uintptr(v1)*2))
		} else {
			v1 = iRight
			iRight = iRight + 1
			logpage = **(**Tht_slot)(__ccgo_up(aRight + uintptr(v1)*2))
		}
		dbpage = **(**Tu32)(__ccgo_up(aContent + uintptr(logpage)*4))
		v1 = iOut
		iOut = iOut + 1
		**(**Tht_slot)(__ccgo_up(aTmp + uintptr(v1)*2)) = logpage
		if iLeft < nLeft && **(**Tu32)(__ccgo_up(aContent + uintptr(**(**Tht_slot)(__ccgo_up(aLeft + uintptr(iLeft)*2)))*4)) == dbpage {
			iLeft = iLeft + 1
		}
	}
	**(**uintptr)(__ccgo_up(paRight)) = aLeft
	**(**int32)(__ccgo_up(pnRight)) = iOut
	libc.Xmemcpy(tls, aLeft, aTmp, uint32(2)*uint32(iOut))
}

// C documentation
//
//	/*
//	** Sort the elements in list aList using aContent[] as the sort key.
//	** Remove elements with duplicate keys, preferring to keep the
//	** larger aList[] values.
//	**
//	** The aList[] entries are indices into aContent[].  The values in
//	** aList[] are to be sorted so that for all J<K:
//	**
//	**      aContent[aList[J]] < aContent[aList[K]]
//	**
//	** For any X and Y such that
//	**
//	**      aContent[aList[X]] == aContent[aList[Y]]
//	**
//	** Keep the larger of the two values aList[X] and aList[Y] and discard
//	** the smaller.
//	*/
func _walMergesort(tls *libc.TLS, aContent uintptr, aBuffer uintptr, aList uintptr, pnList uintptr) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var iList, nList int32
	var iSub Tu32
	var p, p1 uintptr
	var _ /* aMerge at bp+4 */ uintptr
	var _ /* aSub at bp+8 */ [13]struct {
		FnList int32
		FaList uintptr
	}
	var _ /* nMerge at bp+0 */ int32
	_, _, _, _, _ = iList, iSub, nList, p, p1
	nList = **(**int32)(__ccgo_up(pnList))        /* Size of input list */
	**(**int32)(__ccgo_up(bp)) = 0                /* Number of elements in list aMerge */
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* Index into input list */
	iSub = uint32(0)                              /* Array of sub-lists */
	libc.Xmemset(tls, bp+8, 0, uint32(104))
	iList = 0
	for {
		if !(iList < nList) {
			break
		}
		**(**int32)(__ccgo_up(bp)) = int32(1)
		**(**uintptr)(__ccgo_up(bp + 4)) = aList + uintptr(iList)*2
		iSub = uint32(0)
		for {
			if !(iList&(int32(1)<<iSub) != 0) {
				break
			}
			p = bp + 8 + uintptr(iSub)*8
			_walMerge(tls, aContent, (*struct {
				FnList int32
				FaList uintptr
			})(unsafe.Pointer(p)).FaList, (*struct {
				FnList int32
				FaList uintptr
			})(unsafe.Pointer(p)).FnList, bp+4, bp, aBuffer)
			goto _2
		_2:
			;
			iSub = iSub + 1
		}
		(**(**[13]struct {
			FnList int32
			FaList uintptr
		})(__ccgo_up(bp + 8)))[iSub].FaList = **(**uintptr)(__ccgo_up(bp + 4))
		(**(**[13]struct {
			FnList int32
			FaList uintptr
		})(__ccgo_up(bp + 8)))[iSub].FnList = **(**int32)(__ccgo_up(bp))
		goto _1
	_1:
		;
		iList = iList + 1
	}
	iSub = iSub + 1
	for {
		if !(iSub < uint32(int32(libc.Uint32FromInt64(104)/libc.Uint32FromInt64(8)))) {
			break
		}
		if nList&(int32(1)<<iSub) != 0 {
			p1 = bp + 8 + uintptr(iSub)*8
			_walMerge(tls, aContent, (*struct {
				FnList int32
				FaList uintptr
			})(unsafe.Pointer(p1)).FaList, (*struct {
				FnList int32
				FaList uintptr
			})(unsafe.Pointer(p1)).FnList, bp+4, bp, aBuffer)
		}
		goto _3
	_3:
		;
		iSub = iSub + 1
	}
	**(**int32)(__ccgo_up(pnList)) = **(**int32)(__ccgo_up(bp))
}

// C documentation
//
//	/*
//	** The following is guaranteed when this function is called:
//	**
//	**   a) the WRITER lock is held,
//	**   b) the entire log file has been checkpointed, and
//	**   c) any existing readers are reading exclusively from the database
//	**      file - there are no readers that may attempt to read a frame from
//	**      the log file.
//	**
//	** This function updates the shared-memory structures so that the next
//	** client to write to the database (which may be this one) does so by
//	** writing frames into the start of the log file.
//	**
//	** The value of parameter salt1 is used as the aSalt[1] value in the
//	** new wal-index header. It should be passed a pseudo-random value (i.e.
//	** one obtained from sqlite3_randomness()).
//	*/
func _walRestartHdr(tls *libc.TLS, pWal uintptr, _salt1 Tu32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	*(*Tu32)(unsafe.Pointer(bp)) = _salt1
	var aSalt, pInfo uintptr
	var i int32
	_, _, _ = aSalt, i, pInfo
	pInfo = _walCkptInfo(tls, pWal) /* Loop counter */
	aSalt = pWal + 52 + 32          /* Big-endian salt values */
	(*TWal)(unsafe.Pointer(pWal)).FnCkpt = (*TWal)(unsafe.Pointer(pWal)).FnCkpt + 1
	(*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame = uint32(0)
	_sqlite3Put4byte(tls, aSalt, uint32(1)+_sqlite3Get4byte(tls, aSalt))
	libc.Xmemcpy(tls, pWal+52+32+1*4, bp, uint32(4))
	_walIndexWriteHdr(tls, pWal)
	libc.AtomicStoreNUint32(pInfo, uint32(libc.Int32FromInt32(0)), libc.Int32FromInt32(__ATOMIC_RELAXED))
	(*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted = uint32(0)
	**(**Tu32)(__ccgo_up(pInfo + 4 + 1*4)) = uint32(0)
	i = int32(2)
	for {
		if !(i < libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)) {
			break
		}
		**(**Tu32)(__ccgo_up(pInfo + 4 + uintptr(i)*4)) = uint32(READMARK_NOT_USED)
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** This function is called as part of committing a transaction within which
//	** one or more frames have been overwritten. It updates the checksums for
//	** all frames written to the wal file by the current transaction starting
//	** with the earliest to have been overwritten.
//	**
//	** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
//	*/
func _walRewriteChecksums(tls *libc.TLS, pWal uintptr, iLast Tu32) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aBuf uintptr
	var iCksumOff, iOff Ti64
	var iPgno, iRead, nDbSize Tu32
	var rc, szPage int32
	var _ /* aFrame at bp+0 */ [24]Tu8
	_, _, _, _, _, _, _, _ = aBuf, iCksumOff, iOff, iPgno, iRead, nDbSize, rc, szPage
	szPage = int32((*TWal)(unsafe.Pointer(pWal)).FszPage) /* Database page size */
	rc = SQLITE_OK
	aBuf = Xsqlite3_malloc(tls, szPage+int32(WAL_FRAME_HDRSIZE))
	if aBuf == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	/* Find the checksum values to use as input for the recalculating the
	 ** first checksum. If the first frame is frame 1 (implying that the current
	 ** transaction restarted the wal file), these values must be read from the
	 ** wal-file header. Otherwise, read them from the frame header of the
	 ** previous frame.  */
	if (*TWal)(unsafe.Pointer(pWal)).FiReCksum == uint32(1) {
		iCksumOff = int64(24)
	} else {
		iCksumOff = int64(WAL_HDRSIZE) + int64((*TWal)(unsafe.Pointer(pWal)).FiReCksum-libc.Uint32FromInt32(1)-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) + int64(16)
	}
	rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, aBuf, int32(libc.Uint32FromInt64(4)*libc.Uint32FromInt32(2)), iCksumOff)
	**(**Tu32)(__ccgo_up(pWal + 52 + 24)) = _sqlite3Get4byte(tls, aBuf)
	**(**Tu32)(__ccgo_up(pWal + 52 + 24 + 1*4)) = _sqlite3Get4byte(tls, aBuf+uintptr(4))
	iRead = (*TWal)(unsafe.Pointer(pWal)).FiReCksum
	(*TWal)(unsafe.Pointer(pWal)).FiReCksum = uint32(0)
	for {
		if !(rc == SQLITE_OK && iRead <= iLast) {
			break
		}
		iOff = libc.Int64FromInt32(WAL_HDRSIZE) + int64(iRead-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE))
		rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, aBuf, szPage+int32(WAL_FRAME_HDRSIZE), iOff)
		if rc == SQLITE_OK {
			iPgno = _sqlite3Get4byte(tls, aBuf)
			nDbSize = _sqlite3Get4byte(tls, aBuf+4)
			_walEncodeFrame(tls, pWal, iPgno, nDbSize, aBuf+24, bp)
			rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp, int32(24), iOff)
		}
		goto _1
	_1:
		;
		iRead = iRead + 1
	}
	Xsqlite3_free(tls, aBuf)
	return rc
}

// C documentation
//
//	/*
//	** This function does the work of sqlite3WalSnapshotRecover().
//	*/
func _walSnapshotRecover(tls *libc.TLS, pWal uintptr, pBuf1 uintptr, pBuf2 uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var i, pgno Tu32
	var iDbOff, iWalOff Ti64
	var pInfo uintptr
	var rc, szPage int32
	var _ /* sLoc at bp+8 */ TWalHashLoc
	var _ /* szDb at bp+0 */ Ti64
	_, _, _, _, _, _, _ = i, iDbOff, iWalOff, pInfo, pgno, rc, szPage
	szPage = int32((*TWal)(unsafe.Pointer(pWal)).FszPage) /* Size of db file in bytes */
	rc = _sqlite3OsFileSize(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, bp)
	if rc == SQLITE_OK {
		pInfo = _walCkptInfo(tls, pWal)
		i = (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted
		i = (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted
		for {
			if !(i > libc.AtomicLoadNUint32(pInfo, libc.Int32FromInt32(__ATOMIC_RELAXED))) {
				break
			} /* Offset of wal file entry */
			rc = _walHashGet(tls, pWal, _walFramePage(tls, i), bp+8)
			if rc != SQLITE_OK {
				break
			}
			pgno = **(**Tu32)(__ccgo_up((**(**TWalHashLoc)(__ccgo_up(bp + 8))).FaPgno + uintptr(i-(**(**TWalHashLoc)(__ccgo_up(bp + 8))).FiZero-uint32(1))*4))
			iDbOff = int64(pgno-libc.Uint32FromInt32(1)) * int64(szPage)
			if iDbOff+int64(szPage) <= **(**Ti64)(__ccgo_up(bp)) {
				iWalOff = int64(WAL_HDRSIZE) + int64(i-libc.Uint32FromInt32(1))*int64(szPage+libc.Int32FromInt32(WAL_FRAME_HDRSIZE)) + int64(WAL_FRAME_HDRSIZE)
				rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, pBuf1, szPage, iWalOff)
				if rc == SQLITE_OK {
					rc = _sqlite3OsRead(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, pBuf2, szPage, iDbOff)
				}
				if rc != SQLITE_OK || 0 == libc.Xmemcmp(tls, pBuf1, pBuf2, uint32(szPage)) {
					break
				}
			}
			(*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted = i - uint32(1)
			goto _1
		_1:
			;
			i = i - 1
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Attempt to start a read transaction.  This might fail due to a race or
//	** other transient condition.  When that happens, it returns WAL_RETRY to
//	** indicate to the caller that it is safe to retry immediately.
//	**
//	** On success return SQLITE_OK.  On a permanent failure (such an
//	** I/O error or an SQLITE_BUSY because another process is running
//	** recovery) return a positive error code.
//	**
//	** The useWal parameter is true to force the use of the WAL and disable
//	** the case where the WAL is bypassed because it has been completely
//	** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr()
//	** to make a copy of the wal-index header into pWal->hdr.  If the
//	** wal-index header has changed, *pChanged is set to 1 (as an indication
//	** to the caller that the local page cache is obsolete and needs to be
//	** flushed.)  When useWal==1, the wal-index header is assumed to already
//	** be loaded and the pChanged parameter is unused.
//	**
//	** The caller must set the cnt parameter to the number of prior calls to
//	** this routine during the current read attempt that returned WAL_RETRY.
//	** This routine will start taking more aggressive measures to clear the
//	** race conditions after multiple WAL_RETRY returns, and after an excessive
//	** number of errors will ultimately return SQLITE_PROTOCOL.  The
//	** SQLITE_PROTOCOL return indicates that some other process has gone rogue
//	** and is not honoring the locking protocol.  There is a vanishingly small
//	** chance that SQLITE_PROTOCOL could be returned because of a run of really
//	** bad luck when there is lots of contention for the wal-index, but that
//	** possibility is so small that it can be safely neglected, we believe.
//	**
//	** On success, this routine obtains a read lock on
//	** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
//	** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
//	** that means the Wal does not hold any read lock.  The reader must not
//	** access any database page that is modified by a WAL frame up to and
//	** including frame number aReadMark[pWal->readLock].  The reader will
//	** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
//	** Or if pWal->readLock==0, then the reader will ignore the WAL
//	** completely and get all content directly from the database file.
//	** If the useWal parameter is 1 then the WAL will never be ignored and
//	** this routine will always set pWal->readLock>0 on success.
//	** When the read transaction is completed, the caller must release the
//	** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
//	**
//	** This routine uses the nBackfill and aReadMark[] fields of the header
//	** to select a particular WAL_READ_LOCK() that strives to let the
//	** checkpoint process do as much work as possible.  This routine might
//	** update values of the aReadMark[] array in the header, but if it does
//	** so it takes care to hold an exclusive lock on the corresponding
//	** WAL_READ_LOCK() while changing values.
//	*/
func _walTryBeginRead(tls *libc.TLS, pWal uintptr, pChanged uintptr, useWal int32, pCnt uintptr) (r int32) {
	var cnt, i, mxI, nDelay, rc, v1 int32
	var mxFrame, mxReadMark, thisMark Tu32
	var pInfo uintptr
	_, _, _, _, _, _, _, _, _, _ = cnt, i, mxFrame, mxI, mxReadMark, nDelay, pInfo, rc, thisMark, v1 /* Checkpoint information in wal-index */
	rc = SQLITE_OK                                                                                   /* Return code  */
	/* Not currently locked */
	/* useWal may only be set for read/write connections */
	/* Take steps to avoid spinning forever if there is a protocol error.
	 **
	 ** Circumstances that cause a RETRY should only last for the briefest
	 ** instances of time.  No I/O or other system calls are done while the
	 ** locks are held, so the locks should not be held for very long. But
	 ** if we are unlucky, another process that is holding a lock might get
	 ** paged out or take a page-fault that is time-consuming to resolve,
	 ** during the few nanoseconds that it is holding the lock.  In that case,
	 ** it might take longer than normal for the lock to free.
	 **
	 ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
	 ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
	 ** is more of a scheduler yield than an actual delay.  But on the 10th
	 ** an subsequent retries, the delays start becoming longer and longer,
	 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
	 ** The total delay time before giving up is less than 10 seconds.
	 */
	**(**int32)(__ccgo_up(pCnt)) = **(**int32)(__ccgo_up(pCnt)) + 1
	if **(**int32)(__ccgo_up(pCnt)) > int32(5) {
		nDelay = int32(1) /* Pause time in microseconds */
		cnt = **(**int32)(__ccgo_up(pCnt)) & ^libc.Int32FromInt32(WAL_RETRY_BLOCKED_MASK)
		if cnt > int32(WAL_RETRY_PROTOCOL_LIMIT) {
			return int32(SQLITE_PROTOCOL)
		}
		if **(**int32)(__ccgo_up(pCnt)) >= int32(10) {
			nDelay = (cnt - int32(9)) * (cnt - int32(9)) * int32(39)
		}
		_sqlite3OsSleep(tls, (*TWal)(unsafe.Pointer(pWal)).FpVfs, nDelay)
		**(**int32)(__ccgo_up(pCnt)) &= ^libc.Int32FromInt32(WAL_RETRY_BLOCKED_MASK)
	}
	if !(useWal != 0) {
		if int32((*TWal)(unsafe.Pointer(pWal)).FbShmUnreliable) == 0 {
			rc = _walIndexReadHdr(tls, pWal, pChanged)
		}
		if rc == int32(SQLITE_BUSY) {
			/* If there is not a recovery running in another thread or process
			 ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
			 ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
			 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
			 ** would be technically correct.  But the race is benign since with
			 ** WAL_RETRY this routine will be called again and will probably be
			 ** right on the second iteration.
			 */
			if **(**uintptr)(__ccgo_up((*TWal)(unsafe.Pointer(pWal)).FapWiData)) == uintptr(0) {
				/* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
				 ** We assume this is a transient condition, so return WAL_RETRY. The
				 ** xShmMap() implementation used by the default unix and win32 VFS
				 ** modules may return SQLITE_BUSY due to a race condition in the
				 ** code that determines whether or not the shared-memory region
				 ** must be zeroed before the requested page is returned.
				 */
				rc = -int32(1)
			} else {
				v1 = _walLockShared(tls, pWal, int32(WAL_RECOVER_LOCK))
				rc = v1
				if SQLITE_OK == v1 {
					_walUnlockShared(tls, pWal, int32(WAL_RECOVER_LOCK))
					rc = -int32(1)
				} else {
					if rc == int32(SQLITE_BUSY) {
						rc = libc.Int32FromInt32(SQLITE_BUSY) | libc.Int32FromInt32(1)<<libc.Int32FromInt32(8)
					}
				}
			}
		}
		if rc != SQLITE_OK {
			return rc
		} else {
			if (*TWal)(unsafe.Pointer(pWal)).FbShmUnreliable != 0 {
				return _walBeginShmUnreliable(tls, pWal, pChanged)
			}
		}
	}
	pInfo = _walCkptInfo(tls, pWal)
	/* Wal frame to lock to */
	if !(useWal != 0) && libc.AtomicLoadNUint32(pInfo, libc.Int32FromInt32(__ATOMIC_RELAXED)) == (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame && ((*TWal)(unsafe.Pointer(pWal)).FbGetSnapshot == 0 && (*TWal)(unsafe.Pointer(pWal)).FpSnapshot == uintptr(0) || (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame == uint32(0)) {
		/* The WAL has been completely backfilled (or it is empty).
		 ** and can be safely ignored.
		 */
		rc = _walLockShared(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(0))
		_walShmBarrier(tls, pWal)
		if rc == SQLITE_OK {
			if libc.Xmemcmp(tls, _walIndexHdr(tls, pWal), pWal+52, uint32(48)) != 0 {
				/* It is not safe to allow the reader to continue here if frames
				 ** may have been appended to the log before READ_LOCK(0) was obtained.
				 ** When holding READ_LOCK(0), the reader ignores the entire log file,
				 ** which implies that the database file contains a trustworthy
				 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
				 ** happening, this is usually correct.
				 **
				 ** However, if frames have been appended to the log (or if the log
				 ** is wrapped and written for that matter) before the READ_LOCK(0)
				 ** is obtained, that is not necessarily true. A checkpointer may
				 ** have started to backfill the appended frames but crashed before
				 ** it finished. Leaving a corrupt image in the database file.
				 */
				_walUnlockShared(tls, pWal, libc.Int32FromInt32(3)+libc.Int32FromInt32(0))
				return -int32(1)
			}
			(*TWal)(unsafe.Pointer(pWal)).FreadLock = 0
			return SQLITE_OK
		} else {
			if rc != int32(SQLITE_BUSY) {
				return rc
			}
		}
	}
	/* If we get this far, it means that the reader will want to use
	 ** the WAL to get at content from recent commits.  The job now is
	 ** to select one of the aReadMark[] entries that is closest to
	 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
	 */
	mxReadMark = uint32(0)
	mxI = 0
	mxFrame = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame
	if (*TWal)(unsafe.Pointer(pWal)).FpSnapshot != 0 && (*TWalIndexHdr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FpSnapshot)).FmxFrame < mxFrame {
		mxFrame = (*TWalIndexHdr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FpSnapshot)).FmxFrame
	}
	i = int32(1)
	for {
		if !(i < libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)) {
			break
		}
		thisMark = libc.AtomicLoadNUint32(pInfo+4+uintptr(i)*4, libc.Int32FromInt32(__ATOMIC_RELAXED))
		if mxReadMark <= thisMark && thisMark <= mxFrame {
			mxReadMark = thisMark
			mxI = i
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	if int32((*TWal)(unsafe.Pointer(pWal)).FreadOnly)&int32(WAL_SHM_RDONLY) == 0 && (mxReadMark < mxFrame || mxI == 0) {
		i = int32(1)
		for {
			if !(i < libc.Int32FromInt32(SQLITE_SHM_NLOCK)-libc.Int32FromInt32(3)) {
				break
			}
			rc = _walLockExclusive(tls, pWal, int32(3)+i, int32(1))
			if rc == SQLITE_OK {
				libc.AtomicStoreNUint32(pInfo+4+uintptr(i)*4, mxFrame, libc.Int32FromInt32(__ATOMIC_RELAXED))
				mxReadMark = mxFrame
				mxI = i
				_walUnlockExclusive(tls, pWal, int32(3)+i, int32(1))
				break
			} else {
				if rc != int32(SQLITE_BUSY) {
					return rc
				}
			}
			goto _3
		_3:
			;
			i = i + 1
		}
	}
	if mxI == 0 {
		if rc == int32(SQLITE_BUSY) {
			v1 = -int32(1)
		} else {
			v1 = libc.Int32FromInt32(SQLITE_READONLY) | libc.Int32FromInt32(5)<<libc.Int32FromInt32(8)
		}
		return v1
	}
	rc = _walLockShared(tls, pWal, int32(3)+mxI)
	if rc != 0 {
		if rc&int32(0xFF) == int32(SQLITE_BUSY) {
			v1 = -int32(1)
		} else {
			v1 = rc
		}
		return v1
	}
	/* Now that the read-lock has been obtained, check that neither the
	 ** value in the aReadMark[] array or the contents of the wal-index
	 ** header have changed.
	 **
	 ** It is necessary to check that the wal-index header did not change
	 ** between the time it was read and when the shared-lock was obtained
	 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
	 ** that the log file may have been wrapped by a writer, or that frames
	 ** that occur later in the log than pWal->hdr.mxFrame may have been
	 ** copied into the database by a checkpointer. If either of these things
	 ** happened, then reading the database with the current value of
	 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
	 ** instead.
	 **
	 ** Before checking that the live wal-index header has not changed
	 ** since it was read, set Wal.minFrame to the first frame in the wal
	 ** file that has not yet been checkpointed. This client will not need
	 ** to read any frames earlier than minFrame from the wal file - they
	 ** can be safely read directly from the database file.
	 **
	 ** Because a ShmBarrier() call is made between taking the copy of
	 ** nBackfill and checking that the wal-header in shared-memory still
	 ** matches the one cached in pWal->hdr, it is guaranteed that the
	 ** checkpointer that set nBackfill was not working with a wal-index
	 ** header newer than that cached in pWal->hdr. If it were, that could
	 ** cause a problem. The checkpointer could omit to checkpoint
	 ** a version of page X that lies before pWal->minFrame (call that version
	 ** A) on the basis that there is a newer version (version B) of the same
	 ** page later in the wal file. But if version B happens to like past
	 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
	 ** that it can read version A from the database file. However, since
	 ** we can guarantee that the checkpointer that set nBackfill could not
	 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
	 */
	(*TWal)(unsafe.Pointer(pWal)).FminFrame = uint32(int32(libc.AtomicLoadNUint32(pInfo, libc.Int32FromInt32(__ATOMIC_RELAXED))) + int32(1))
	_walShmBarrier(tls, pWal)
	if libc.AtomicLoadNUint32(pInfo+4+uintptr(mxI)*4, libc.Int32FromInt32(__ATOMIC_RELAXED)) != mxReadMark || libc.Xmemcmp(tls, _walIndexHdr(tls, pWal), pWal+52, uint32(48)) != 0 {
		_walUnlockShared(tls, pWal, int32(3)+mxI)
		return -int32(1)
	} else {
		(*TWal)(unsafe.Pointer(pWal)).FreadLock = int16(mxI)
	}
	return rc
}

// C documentation
//
//	/*
//	** Write out a single frame of the WAL
//	*/
func _walWriteOneFrame(tls *libc.TLS, p uintptr, pPage uintptr, nTruncate int32, iOffset Tsqlite3_int64) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var pData uintptr
	var rc int32
	var _ /* aFrame at bp+0 */ [24]Tu8
	_, _ = pData, rc /* Buffer to assemble frame-header in */
	pData = (*TPgHdr)(unsafe.Pointer(pPage)).FpData
	_walEncodeFrame(tls, (*TWalWriter)(unsafe.Pointer(p)).FpWal, (*TPgHdr)(unsafe.Pointer(pPage)).Fpgno, uint32(nTruncate), pData, bp)
	rc = _walWriteToLog(tls, p, bp, int32(24), iOffset)
	if rc != 0 {
		return rc
	}
	/* Write the page data */
	rc = _walWriteToLog(tls, p, pData, (*TWalWriter)(unsafe.Pointer(p)).FszPage, iOffset+int64(24))
	return rc
}

// C documentation
//
//	/*
//	** The index pIdx is used by a query and contains one or more expressions.
//	** In other words pIdx is an index on an expression.  iIdxCur is the cursor
//	** number for the index and iDataCur is the cursor number for the corresponding
//	** table.
//	**
//	** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
//	** each of the expressions in the index so that the expression code generator
//	** will know to replace occurrences of the indexed expression with
//	** references to the corresponding column of the index.
//	*/
func _whereAddIndexedExpr(tls *libc.TLS, pParse uintptr, pIdx uintptr, iIdxCur int32, pTabItem uintptr) {
	var i, j int32
	var p, pArg, pExpr, pTab uintptr
	_, _, _, _, _, _ = i, j, p, pArg, pExpr, pTab
	pTab = (*TIndex)(unsafe.Pointer(pIdx)).FpTable
	i = 0
	for {
		if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) {
			break
		}
		j = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2)))
		if j == -int32(2) {
			pExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*20))).FpExpr
		} else {
			if j >= 0 && int32((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*12))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 {
				pExpr = _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(j)*12)
			} else {
				goto _1
			}
		}
		if _sqlite3ExprIsConstant(tls, uintptr(0), pExpr) != 0 {
			goto _1
		}
		p = _sqlite3DbMallocRaw(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(24))
		if p == uintptr(0) {
			break
		}
		(*TIndexedExpr)(unsafe.Pointer(p)).FpIENext = (*TParse)(unsafe.Pointer(pParse)).FpIdxEpr
		(*TIndexedExpr)(unsafe.Pointer(p)).FpExpr = _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, 0)
		(*TIndexedExpr)(unsafe.Pointer(p)).FiDataCur = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor
		(*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur = iIdxCur
		(*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCol = i
		(*TIndexedExpr)(unsafe.Pointer(p)).FbMaybeNullRow = libc.BoolUint8(int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0)
		if _sqlite3IndexAffinityStr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pIdx) != 0 {
			(*TIndexedExpr)(unsafe.Pointer(p)).Faff = uint8(**(**int8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FzColAff + uintptr(i))))
		}
		(*TParse)(unsafe.Pointer(pParse)).FpIdxEpr = p
		if (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext == uintptr(0) {
			pArg = pParse + 80
			_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_whereIndexedExprCleanup), pArg)
		}
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or
//	** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the
//	** where-clause passed as the first argument. The value for the term
//	** is found in register iReg.
//	**
//	** In the common case where the value is a simple integer
//	** (example: "LIMIT 5 OFFSET 10") then the expression codes as a
//	** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value().
//	** If not, then it codes as a TK_REGISTER expression.
//	*/
func _whereAddLimitExpr(tls *libc.TLS, pWC uintptr, iReg int32, pExpr uintptr, iCsr int32, eMatchOp int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var db, pNew, pParse, pTerm, pVal, pVal1 uintptr
	var idx int32
	var _ /* iVal at bp+0 */ int32
	_, _, _, _, _, _, _ = db, idx, pNew, pParse, pTerm, pVal, pVal1
	pParse = (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	**(**int32)(__ccgo_up(bp)) = 0
	if _sqlite3ExprIsInteger(tls, pExpr, bp, pParse) != 0 && **(**int32)(__ccgo_up(bp)) >= 0 {
		pVal = _sqlite3ExprInt32(tls, db, **(**int32)(__ccgo_up(bp)))
		if pVal == uintptr(0) {
			return
		}
		pNew = _sqlite3PExpr(tls, pParse, int32(TK_MATCH), uintptr(0), pVal)
	} else {
		pVal1 = _sqlite3ExprAlloc(tls, db, int32(TK_REGISTER), uintptr(0), 0)
		if pVal1 == uintptr(0) {
			return
		}
		(*TExpr)(unsafe.Pointer(pVal1)).FiTable = iReg
		pNew = _sqlite3PExpr(tls, pParse, int32(TK_MATCH), uintptr(0), pVal1)
	}
	if pNew != 0 {
		idx = _whereClauseInsert(tls, pWC, pNew, uint16(libc.Int32FromInt32(TERM_DYNAMIC)|libc.Int32FromInt32(TERM_VIRTUAL)))
		pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idx)*48
		(*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor = iCsr
		(*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(WO_AUX)
		(*TWhereTerm)(unsafe.Pointer(pTerm)).FeMatchOp = uint8(eMatchOp)
	}
}

// C documentation
//
//	/*
//	** The pTruth expression is always true because it is the WHERE clause
//	** a partial index that is driving a query loop.  Look through all of the
//	** WHERE clause terms on the query, and if any of those terms must be
//	** true because pTruth is true, then mark those WHERE clause terms as
//	** coded.
//	*/
func _whereApplyPartialIndexConstraints(tls *libc.TLS, pTruth uintptr, iTabCur int32, pWC uintptr) {
	var i int32
	var pExpr, pTerm, v2 uintptr
	_, _, _, _ = i, pExpr, pTerm, v2
	for int32((*TExpr)(unsafe.Pointer(pTruth)).Fop) == int32(TK_AND) {
		_whereApplyPartialIndexConstraints(tls, (*TExpr)(unsafe.Pointer(pTruth)).FpLeft, iTabCur, pWC)
		pTruth = (*TExpr)(unsafe.Pointer(pTruth)).FpRight
	}
	i = 0
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
	for {
		if !(i < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) {
			break
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_CODED) != 0 {
			goto _1
		}
		pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
		if _sqlite3ExprCompare(tls, uintptr(0), pExpr, pTruth, iTabCur) == 0 {
			v2 = pTerm + 10
			*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(TERM_CODED))
		}
		goto _1
	_1:
		;
		i = i + 1
		pTerm += 48
	}
}

// C documentation
//
//	/*
//	** Check to see if there are any SEARCH loops that might benefit from
//	** using a Bloom filter.  Consider a Bloom filter if:
//	**
//	**   (1)  The SEARCH happens more than N times where N is the number
//	**        of rows in the table that is being considered for the Bloom
//	**        filter.
//	**   (2)  Some searches are expected to find zero rows.  (This is determined
//	**        by the WHERE_SELFCULL flag on the term.)
//	**   (3)  Bloom-filter processing is not disabled.  (Checked by the
//	**        caller.)
//	**   (4)  The size of the table being searched is known by ANALYZE.
//	**
//	** This block of code merely checks to see if a Bloom filter would be
//	** appropriate, and if so sets the WHERE_BLOOMFILTER flag on the
//	** WhereLoop.  The implementation of the Bloom filter comes further
//	** down where the code for each WhereLoop is generated.
//	*/
func _whereCheckIfBloomFilterIsUseful(tls *libc.TLS, pWInfo uintptr) {
	var i int32
	var nSearch TLogEst
	var pItem, pLoop, pTab uintptr
	var reqFlags uint32
	_, _, _, _, _, _ = i, nSearch, pItem, pLoop, pTab, reqFlags
	nSearch = 0
	i = 0
	for {
		if !(i < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) {
			break
		}
		pLoop = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(i)*88))).FpWLoop
		reqFlags = uint32(libc.Int32FromInt32(WHERE_SELFCULL) | libc.Int32FromInt32(WHERE_COLUMN_EQ))
		pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab)*48
		pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasStat1) == uint32(0) {
			break
		}
		**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(TF_MaybeReanalyze)
		if i >= int32(1) && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&reqFlags == reqFlags && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_INDEXED)) != uint32(0) {
			if int32(nSearch) > int32((*TTable)(unsafe.Pointer(pTab)).FnRowLogEst) {
				**(**Tu32)(__ccgo_up(pLoop + 40)) |= uint32(WHERE_BLOOMFILTER)
				**(**Tu32)(__ccgo_up(pLoop + 40)) &= uint32(^libc.Int32FromInt32(WHERE_IDX_ONLY))
			}
		}
		nSearch = int16(int32(nSearch) + int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut))
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Add a single new WhereTerm entry to the WhereClause object pWC.
//	** The new WhereTerm object is constructed from Expr p and with wtFlags.
//	** The index in pWC->a[] of the new WhereTerm is returned on success.
//	** 0 is returned if the new WhereTerm could not be added due to a memory
//	** allocation error.  The memory allocation failure will be recorded in
//	** the db->mallocFailed flag so that higher-level functions can detect it.
//	**
//	** This routine will increase the size of the pWC->a[] array as necessary.
//	**
//	** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
//	** for freeing the expression p is assumed by the WhereClause object pWC.
//	** This is true even if this routine fails to allocate a new WhereTerm.
//	**
//	** WARNING:  This routine might reallocate the space used to store
//	** WhereTerms.  All pointers to WhereTerms should be invalidated after
//	** calling this routine.  Such pointers may be reinitialized by referencing
//	** the pWC->a[] array.
//	*/
func _whereClauseInsert(tls *libc.TLS, pWC uintptr, p uintptr, wtFlags Tu16) (r int32) {
	var db, pOld, pTerm, v3 uintptr
	var idx, v1, v2 int32
	_, _, _, _, _, _, _ = db, idx, pOld, pTerm, v1, v2, v3
	if (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm >= (*TWhereClause)(unsafe.Pointer(pWC)).FnSlot {
		pOld = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
		db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse)).Fdb
		(*TWhereClause)(unsafe.Pointer(pWC)).Fa = _sqlite3WhereMalloc(tls, (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo, uint64(uint32(48)*uint32((*TWhereClause)(unsafe.Pointer(pWC)).FnSlot)*uint32(2)))
		if (*TWhereClause)(unsafe.Pointer(pWC)).Fa == uintptr(0) {
			if int32(wtFlags)&int32(TERM_DYNAMIC) != 0 {
				_sqlite3ExprDelete(tls, db, p)
			}
			(*TWhereClause)(unsafe.Pointer(pWC)).Fa = pOld
			return 0
		}
		libc.Xmemcpy(tls, (*TWhereClause)(unsafe.Pointer(pWC)).Fa, pOld, uint32(48)*uint32((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm))
		(*TWhereClause)(unsafe.Pointer(pWC)).FnSlot = (*TWhereClause)(unsafe.Pointer(pWC)).FnSlot * int32(2)
	}
	v3 = pWC + 12
	v2 = *(*int32)(unsafe.Pointer(v3))
	*(*int32)(unsafe.Pointer(v3)) = *(*int32)(unsafe.Pointer(v3)) + 1
	v1 = v2
	idx = v1
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(v1)*48
	if int32(wtFlags)&int32(TERM_VIRTUAL) == 0 {
		(*TWhereClause)(unsafe.Pointer(pWC)).FnBase = (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm
	}
	if p != 0 && (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Unlikely)) != uint32(0) {
		(*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb = int16(int32(_sqlite3LogEst(tls, uint64((*TExpr)(unsafe.Pointer(p)).FiTable))) - int32(270))
	} else {
		(*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb = int16(1)
	}
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr = _sqlite3ExprSkipCollateAndLikely(tls, p)
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags = wtFlags
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FpWC = pWC
	(*TWhereTerm)(unsafe.Pointer(pTerm)).FiParent = -int32(1)
	libc.Xmemset(tls, pTerm+12, 0, libc.Uint32FromInt64(48)-uint32(libc.UintptrFromInt32(0)+12))
	return idx
}

// C documentation
//
//	/*
//	** Estimate the number of rows that will be returned based on
//	** an IN constraint where the right-hand side of the IN operator
//	** is a list of values.  Example:
//	**
//	**        WHERE x IN (1,2,3,4)
//	**
//	** Write the estimated row count into *pnRow and return SQLITE_OK.
//	** If unable to make an estimate, leave *pnRow unchanged and return
//	** non-zero.
//	**
//	** This routine can fail if it is unable to load a collating sequence
//	** required for string comparison, or if unable to allocate memory
//	** for a UTF conversion required for comparison.  The error is stored
//	** in the pParse structure.
//	*/
func _whereInScanEst(tls *libc.TLS, pParse uintptr, pBuilder uintptr, pList uintptr, pnRow uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, nRecValid, rc int32
	var nRow0 Ti64
	var nRowEst TtRowcnt
	var p uintptr
	var _ /* nEst at bp+0 */ TtRowcnt
	_, _, _, _, _, _ = i, nRecValid, nRow0, nRowEst, p, rc
	p = (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew + 24))).FpIndex
	nRow0 = int64(_sqlite3LogEstToInt(tls, **(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(p)).FaiRowLogEst))))
	nRecValid = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid
	rc = SQLITE_OK      /* Number of rows for a single term */
	nRowEst = uint64(0) /* Loop counter */
	i = 0
	for {
		if !(rc == SQLITE_OK && i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
			break
		}
		**(**TtRowcnt)(__ccgo_up(bp)) = uint64(nRow0)
		rc = _whereEqualScanEst(tls, pParse, pBuilder, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr, bp)
		nRowEst = nRowEst + **(**TtRowcnt)(__ccgo_up(bp))
		(*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid = nRecValid
		goto _1
	_1:
		;
		i = i + 1
	}
	if rc == SQLITE_OK {
		if nRowEst > uint64(nRow0) {
			nRowEst = uint64(nRow0)
		}
		**(**TtRowcnt)(__ccgo_up(pnRow)) = nRowEst
	}
	return rc
}

// C documentation
//
//	/*
//	** This routine implements a heuristic designed to improve query planning.
//	** This routine is called in between the first and second call to
//	** wherePathSolver().  Hence the name "Interstage" "Heuristic".
//	**
//	** The first call to wherePathSolver() (hereafter just "solver()") computes
//	** the best path without regard to the order of the outputs.  The second call
//	** to the solver() builds upon the first call to try to find an alternative
//	** path that satisfies the ORDER BY clause.
//	**
//	** This routine looks at the results of the first solver() run, and for
//	** every FROM clause term in the resulting query plan that uses an equality
//	** constraint against an index, disable other WhereLoops for that same
//	** FROM clause term that would try to do a full-table scan.  This prevents
//	** an index search from being converted into a full-table scan in order to
//	** satisfy an ORDER BY clause, since even though we might get slightly better
//	** performance using the full-scan without sorting if the output size
//	** estimates are very precise, we might also get severe performance
//	** degradation using the full-scan if the output size estimate is too large.
//	** It is better to err on the side of caution.
//	**
//	** Except, if the first solver() call generated a full-table scan in an outer
//	** loop then stop this analysis at the first full-scan, since the second
//	** solver() run might try to swap that full-scan for another in order to
//	** get the output into the correct order.  In other words, we allow a
//	** rewrite like this:
//	**
//	**     First Solver()                      Second Solver()
//	**       |-- SCAN t1                         |-- SCAN t2
//	**       |-- SEARCH t2                       `-- SEARCH t1
//	**       `-- SORT USING B-TREE
//	**
//	** The purpose of this routine is to disallow rewrites such as:
//	**
//	**     First Solver()                      Second Solver()
//	**       |-- SEARCH t1                       |-- SCAN t2     <--- bad!
//	**       |-- SEARCH t2                       `-- SEARCH t1
//	**       `-- SORT USING B-TREE
//	**
//	** See test cases in test/whereN.test for the real-world query that
//	** originally provoked this heuristic.
//	*/
func _whereInterstageHeuristic(tls *libc.TLS, pWInfo uintptr) {
	var i int32
	var iTab Tu8
	var p, pLoop uintptr
	_, _, _, _ = i, iTab, p, pLoop
	i = 0
	for {
		if !(i < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) {
			break
		}
		p = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(i)*88))).FpWLoop
		if p == uintptr(0) {
			break
		}
		if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != uint32(0) {
			/* Treat a vtab scan as similar to a full-table scan */
			break
		}
		if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ)|libc.Int32FromInt32(WHERE_COLUMN_NULL)|libc.Int32FromInt32(WHERE_COLUMN_IN)) != uint32(0) {
			iTab = (*TWhereLoop)(unsafe.Pointer(p)).FiTab
			pLoop = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops
			for {
				if !(pLoop != 0) {
					break
				}
				if int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab) != int32(iTab) {
					goto _2
				}
				if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_CONSTRAINT)|libc.Int32FromInt32(WHERE_AUTO_INDEX)) != uint32(0) {
					/* Auto-index and index-constrained loops allowed to remain */
					goto _2
				}
				(*TWhereLoop)(unsafe.Pointer(pLoop)).Fprereq = uint64(-libc.Int32FromInt32(1)) /* Prevent 2nd solver() from using this one */
				goto _2
			_2:
				;
				pLoop = (*TWhereLoop)(unsafe.Pointer(pLoop)).FpNextLoop
			}
		} else {
			break
		}
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** pIdx is an index that covers all of the low-number columns used by
//	** pWInfo->pSelect (columns from 0 through 62) or an index that has
//	** expressions terms.  Hence, we cannot determine whether or not it is
//	** a covering index by using the colUsed bitmasks.  We have to do a search
//	** to see if the index is covering.  This routine does that search.
//	**
//	** The return value is one of these:
//	**
//	**      0                The index is definitely not a covering index
//	**
//	**      WHERE_IDX_ONLY   The index is definitely a covering index
//	**
//	**      WHERE_EXPRIDX    The index is likely a covering index, but it is
//	**                       difficult to determine precisely because of the
//	**                       expressions that are indexed.  Score it as a
//	**                       covering index, but still keep the main table open
//	**                       just in case we need it.
//	**
//	** This routine is an optimization.  It is always safe to return zero.
//	** But returning one of the other two values when zero should have been
//	** returned can lead to incorrect bytecode and assertion faults.
//	*/
func _whereIsCoveringIndex(tls *libc.TLS, pWInfo uintptr, pIdx uintptr, iTabCur int32) (r Tu32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var i, rc int32
	var _ /* ck at bp+0 */ TCoveringIndexCheck
	var _ /* w at bp+12 */ TWalker
	_, _ = i, rc
	if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect == uintptr(0) {
		/* We don't have access to the full query, so we cannot check to see
		 ** if pIdx is covering.  Assume it is not. */
		return uint32(0)
	}
	if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x800>>11)) == 0 {
		i = 0
		for {
			if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)) {
				break
			}
			if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2))) >= int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) {
				break
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		if i >= int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) {
			/* pIdx does not index any columns greater than 62, but we know from
			 ** colMask that columns greater than 62 are used, so this is not a
			 ** covering index */
			return uint32(0)
		}
	}
	(**(**TCoveringIndexCheck)(__ccgo_up(bp))).FpIdx = pIdx
	(**(**TCoveringIndexCheck)(__ccgo_up(bp))).FiTabCur = iTabCur
	(**(**TCoveringIndexCheck)(__ccgo_up(bp))).FbExpr = uint8(0)
	(**(**TCoveringIndexCheck)(__ccgo_up(bp))).FbUnidx = uint8(0)
	libc.Xmemset(tls, bp+12, 0, uint32(28))
	(**(**TWalker)(__ccgo_up(bp + 12))).FxExprCallback = __ccgo_fp(_whereIsCoveringIndexWalkCallback)
	(**(**TWalker)(__ccgo_up(bp + 12))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop)
	*(*uintptr)(unsafe.Pointer(bp + 12 + 24)) = bp
	_sqlite3WalkSelect(tls, bp+12, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect)
	if (**(**TCoveringIndexCheck)(__ccgo_up(bp))).FbUnidx != 0 {
		rc = 0
	} else {
		if (**(**TCoveringIndexCheck)(__ccgo_up(bp))).FbExpr != 0 {
			rc = int32(WHERE_EXPRIDX)
		} else {
			rc = int32(WHERE_IDX_ONLY)
		}
	}
	return uint32(rc)
}

// C documentation
//
//	/*
//	** Information passed in is pWalk->u.pCovIdxCk.  Call it pCk.
//	**
//	** If the Expr node references the table with cursor pCk->iTabCur, then
//	** make sure that column is covered by the index pCk->pIdx.  We know that
//	** all columns less than 63 (really BMS-1) are covered, so we don't need
//	** to check them.  But we do need to check any column at 63 or greater.
//	**
//	** If the index does not cover the column, then set pWalk->eCode to
//	** non-zero and return WRC_Abort to stop the search.
//	**
//	** If this node does not disprove that the index can be a covering index,
//	** then just return WRC_Continue, to continue the search.
//	**
//	** If pCk->pIdx contains indexed expressions and one of those expressions
//	** matches pExpr, then prune the search.
//	*/
func _whereIsCoveringIndexWalkCallback(tls *libc.TLS, pWalk uintptr, pExpr uintptr) (r int32) {
	var aiColumn, pCk, pIdx uintptr
	var i int32
	var nColumn Tu16
	_, _, _, _, _ = aiColumn, i, nColumn, pCk, pIdx /* Info about this search */
	pCk = *(*uintptr)(unsafe.Pointer(pWalk + 24))
	pIdx = (*TCoveringIndexCheck)(unsafe.Pointer(pCk)).FpIdx
	if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_COLUMN) {
		/* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
		if (*TExpr)(unsafe.Pointer(pExpr)).FiTable != (*TCoveringIndexCheck)(unsafe.Pointer(pCk)).FiTabCur {
			return WRC_Continue
		}
		pIdx = (*TCoveringIndexCheck)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalk + 24)))).FpIdx
		aiColumn = (*TIndex)(unsafe.Pointer(pIdx)).FaiColumn
		nColumn = (*TIndex)(unsafe.Pointer(pIdx)).FnColumn
		i = 0
		for {
			if !(i < int32(nColumn)) {
				break
			}
			if int32(**(**Ti16)(__ccgo_up(aiColumn + uintptr(i)*2))) == int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) {
				return WRC_Continue
			}
			goto _1
		_1:
			;
			i = i + 1
		}
		(*TCoveringIndexCheck)(unsafe.Pointer(pCk)).FbUnidx = uint8(1)
		return int32(WRC_Abort)
	} else {
		if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x800>>11)) != 0 && _exprIsCoveredByIndex(tls, pExpr, pIdx, (*TCoveringIndexCheck)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalk + 24)))).FiTabCur) != 0 {
			(*TCoveringIndexCheck)(unsafe.Pointer(pCk)).FbExpr = uint8(1)
			return int32(WRC_Prune)
		}
	}
	return WRC_Continue
}

// C documentation
//
//	/*
//	** Estimate the location of a particular key among all keys in an
//	** index.  Store the results in aStat as follows:
//	**
//	**    aStat[0]      Est. number of rows less than pRec
//	**    aStat[1]      Est. number of rows equal to pRec
//	**
//	** Return the index of the sample that is the smallest sample that
//	** is greater than or equal to pRec. Note that this index is not an index
//	** into the aSample[] array - it is an index into a virtual set of samples
//	** based on the contents of aSample[] and the number of fields in record
//	** pRec.
//	*/
func _whereKeyStats(tls *libc.TLS, pParse uintptr, pIdx uintptr, pRec uintptr, roundUp int32, aStat uintptr) (r int32) {
	var aSample uintptr
	var i, iCol, iMin, iSamp, iSample, iTest, n, nField, res, v1 int32
	var iGap, iLower, iUpper TtRowcnt
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = aSample, i, iCol, iGap, iLower, iMin, iSamp, iSample, iTest, iUpper, n, nField, res, v1
	aSample = (*TIndex)(unsafe.Pointer(pIdx)).FaSample /* Smallest sample larger than or equal to pRec */
	iMin = 0                                           /* Number of fields in pRec */
	iLower = uint64(0)                                 /* anLt[] + anEq[] of largest sample pRec is > */
	_ = pParse
	/* Do a binary search to find the first sample greater than or equal
	 ** to pRec. If pRec contains a single field, the set of samples to search
	 ** is simply the aSample[] array. If the samples in aSample[] contain more
	 ** than one fields, all fields following the first are ignored.
	 **
	 ** If pRec contains N fields, where N is more than one, then as well as the
	 ** samples in aSample[] (truncated to N fields), the search also has to
	 ** consider prefixes of those samples. For example, if the set of samples
	 ** in aSample is:
	 **
	 **     aSample[0] = (a, 5)
	 **     aSample[1] = (a, 10)
	 **     aSample[2] = (b, 5)
	 **     aSample[3] = (c, 100)
	 **     aSample[4] = (c, 105)
	 **
	 ** Then the search space should ideally be the samples above and the
	 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
	 ** the code actually searches this set:
	 **
	 **     0: (a)
	 **     1: (a, 5)
	 **     2: (a, 10)
	 **     3: (a, 10)
	 **     4: (b)
	 **     5: (b, 5)
	 **     6: (c)
	 **     7: (c, 100)
	 **     8: (c, 105)
	 **     9: (c, 105)
	 **
	 ** For each sample in the aSample[] array, N samples are present in the
	 ** effective sample array. In the above, samples 0 and 1 are based on
	 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
	 **
	 ** Often, sample i of each block of N effective samples has (i+1) fields.
	 ** Except, each sample may be extended to ensure that it is greater than or
	 ** equal to the previous sample in the array. For example, in the above,
	 ** sample 2 is the first sample of a block of N samples, so at first it
	 ** appears that it should be 1 field in size. However, that would make it
	 ** smaller than sample 1, so the binary search would not work. As a result,
	 ** it is extended to two fields. The duplicates that this creates do not
	 ** cause any problems.
	 */
	if !((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
		nField = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)
	} else {
		nField = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)
	}
	if int32((*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField) < nField {
		v1 = int32((*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField)
	} else {
		v1 = nField
	}
	nField = v1
	iCol = 0
	iSample = (*TIndex)(unsafe.Pointer(pIdx)).FnSample * nField
	for cond := true; cond; cond = res != 0 && iMin < iSample { /* Number of fields in test sample */
		iTest = (iMin + iSample) / int32(2)
		iSamp = iTest / nField
		if iSamp > 0 {
			/* The proposed effective sample is a prefix of sample aSample[iSamp].
			 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
			 ** fields that is greater than the previous effective sample.  */
			n = iTest%nField + int32(1)
			for {
				if !(n < nField) {
					break
				}
				if **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(iSamp-int32(1))*20))).FanLt + uintptr(n-int32(1))*8)) != **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(iSamp)*20))).FanLt + uintptr(n-int32(1))*8)) {
					break
				}
				goto _2
			_2:
				;
				n = n + 1
			}
		} else {
			n = iTest + int32(1)
		}
		(*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField = uint16(n)
		res = _sqlite3VdbeRecordCompare(tls, (**(**TIndexSample)(__ccgo_up(aSample + uintptr(iSamp)*20))).Fn, (**(**TIndexSample)(__ccgo_up(aSample + uintptr(iSamp)*20))).Fp, pRec)
		if res < 0 {
			iLower = **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(iSamp)*20))).FanLt + uintptr(n-int32(1))*8)) + **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(iSamp)*20))).FanEq + uintptr(n-int32(1))*8))
			iMin = iTest + int32(1)
		} else {
			if res == 0 && n < nField {
				iLower = **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(iSamp)*20))).FanLt + uintptr(n-int32(1))*8))
				iMin = iTest + int32(1)
				res = -int32(1)
			} else {
				iSample = iTest
				iCol = n - int32(1)
			}
		}
	}
	i = iSample / nField
	if res == 0 {
		/* Record pRec is equal to sample i */
		**(**TtRowcnt)(__ccgo_up(aStat)) = **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(i)*20))).FanLt + uintptr(iCol)*8))
		**(**TtRowcnt)(__ccgo_up(aStat + 1*8)) = **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(i)*20))).FanEq + uintptr(iCol)*8))
	} else {
		if i >= (*TIndex)(unsafe.Pointer(pIdx)).FnSample {
			iUpper = (*TIndex)(unsafe.Pointer(pIdx)).FnRowEst0
		} else {
			iUpper = **(**TtRowcnt)(__ccgo_up((**(**TIndexSample)(__ccgo_up(aSample + uintptr(i)*20))).FanLt + uintptr(iCol)*8))
		}
		if iLower >= iUpper {
			iGap = uint64(0)
		} else {
			iGap = iUpper - iLower
		}
		if roundUp != 0 {
			iGap = iGap * uint64(2) / uint64(3)
		} else {
			iGap = iGap / uint64(3)
		}
		**(**TtRowcnt)(__ccgo_up(aStat)) = iLower + iGap
		**(**TtRowcnt)(__ccgo_up(aStat + 1*8)) = **(**TtRowcnt)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaAvgEq + uintptr(nField-int32(1))*8))
	}
	/* Restore the pRec->nField value before returning.  */
	(*TUnpackedRecord)(unsafe.Pointer(pRec)).FnField = uint16(nField)
	return i
}

// C documentation
//
//	/*
//	** Add all WhereLoop objects for all tables
//	*/
func _whereLoopAddAll(tls *libc.TLS, pBuilder uintptr) (r int32) {
	var bFirstPastRJ, hasRightCrossJoin, i, iTab, rc int32
	var db, p, pEnd, pItem, pNew, pTabList, pTerm, pWC, pWInfo uintptr
	var mPrereq, mPrior, mUnusable TBitmask
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bFirstPastRJ, db, hasRightCrossJoin, i, iTab, mPrereq, mPrior, mUnusable, p, pEnd, pItem, pNew, pTabList, pTerm, pWC, pWInfo, rc
	pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo
	mPrereq = uint64(0)
	mPrior = uint64(0)
	pTabList = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList
	pEnd = pTabList + 8 + uintptr((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)*48
	db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb
	rc = SQLITE_OK
	bFirstPastRJ = 0
	hasRightCrossJoin = 0
	/* Loop over the tables in the join, from left to right */
	pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew
	/* Verify that pNew has already been initialized */
	(*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FiPlanLimit = uint32(SQLITE_QUERY_PLANNER_LIMIT)
	iTab = 0
	pItem = pTabList + 8
	for {
		if !(pItem < pEnd) {
			break
		}
		mUnusable = uint64(0)
		(*TWhereLoop)(unsafe.Pointer(pNew)).FiTab = uint8(iTab)
		**(**uint32)(__ccgo_up(pBuilder + 28)) += uint32(SQLITE_QUERY_PLANNER_LIMIT_INCR)
		(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf = _sqlite3WhereGetMask(tls, pWInfo+496, (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor)
		if bFirstPastRJ != 0 || int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)|libc.Int32FromInt32(JT_LTORJ)) != 0 {
			/* Add prerequisites to prevent reordering of FROM clause terms
			 ** across CROSS joins and outer joins.  The bFirstPastRJ boolean
			 ** prevents the right operand of a RIGHT JOIN from being swapped with
			 ** other elements even further to the right.
			 **
			 ** The hasRightCrossJoin flag prevent FROM-clause terms from moving
			 ** from the right side of a LEFT JOIN or CROSS JOIN over to the
			 ** left side of that same join.  This is a required restriction in
			 ** the case of LEFT JOIN - an incorrect answer may results if it is
			 ** not enforced.  This restriction is not required for CROSS JOIN.
			 ** It is provided merely as a means of controlling join order, under
			 ** the theory that no real-world queries that care about performance
			 ** actually use the CROSS JOIN syntax.
			 */
			if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_CROSS)) != 0 {
				hasRightCrossJoin = int32(1)
			}
			mPrereq = mPrereq | mPrior
			bFirstPastRJ = libc.BoolInt32(int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0)
		} else {
			if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x40000>>18) != 0 {
				/* joins that result from the EXISTS-to-JOIN optimization should not
				 ** be moved to the left of any of their dependencies */
				pWC = pWInfo + 80
				i = (*TWhereClause)(unsafe.Pointer(pWC)).FnBase
				pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
				for {
					if !(i > 0) {
						break
					}
					if (*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf&(*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll != uint64(0) {
						mPrereq = mPrereq | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&((*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf-uint64(1))
					}
					goto _2
				_2:
					;
					i = i - 1
					pTerm += 48
				}
			} else {
				if !(hasRightCrossJoin != 0) {
					mPrereq = uint64(0)
				}
			}
		}
		if int32((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSTab)).FeTabType) == int32(TABTYP_VTAB) {
			p = pItem + 1*48
			for {
				if !(p < pEnd) {
					break
				}
				if mUnusable != 0 || int32((*TSrcItem)(unsafe.Pointer(p)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_OUTER)|libc.Int32FromInt32(JT_CROSS)) != 0 {
					mUnusable = mUnusable | _sqlite3WhereGetMask(tls, pWInfo+496, (*TSrcItem)(unsafe.Pointer(p)).FiCursor)
				}
				goto _3
			_3:
				;
				p += 48
			}
			rc = _whereLoopAddVirtual(tls, pBuilder, mPrereq, mUnusable)
		} else {
			rc = _whereLoopAddBtree(tls, pBuilder, mPrereq)
		}
		if rc == SQLITE_OK && (*TWhereClause)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC)).FhasOr != 0 {
			rc = _whereLoopAddOr(tls, pBuilder, mPrereq, mUnusable)
		}
		mPrior = mPrior | (*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf
		if rc != 0 || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 {
			if rc == int32(SQLITE_DONE) {
				/* We hit the query planner search limit set by iPlanLimit */
				Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+25281, 0)
				rc = SQLITE_OK
			} else {
				break
			}
		}
		goto _1
	_1:
		;
		iTab = iTab + 1
		pItem += 48
	}
	_whereLoopClear(tls, db, pNew)
	return rc
}

// C documentation
//
//	/*
//	** Add all WhereLoop objects for a single table of the join where the table
//	** is identified by pBuilder->pNew->iTab.  That table is guaranteed to be
//	** a b-tree table, not a virtual table.
//	**
//	** The costs (WhereLoop.rRun) of the b-tree loops added by this function
//	** are calculated as follows:
//	**
//	** For a full scan, assuming the table (or index) contains nRow rows:
//	**
//	**     cost = nRow * 3.0                    // full-table scan
//	**     cost = nRow * K                      // scan of covering index
//	**     cost = nRow * (K+3.0)                // scan of non-covering index
//	**
//	** where K is a value between 1.1 and 3.0 set based on the relative
//	** estimated average size of the index and table records.
//	**
//	** For an index scan, where nVisit is the number of index rows visited
//	** by the scan, and nSeek is the number of seek operations required on
//	** the index b-tree:
//	**
//	**     cost = nSeek * (log(nRow) + K * nVisit)          // covering index
//	**     cost = nSeek * (log(nRow) + (K+3.0) * nVisit)    // non-covering index
//	**
//	** Normally, nSeek is 1. nSeek values greater than 1 come about if the
//	** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
//	** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
//	**
//	** The estimated values (nRow, nVisit, nSeek) often contain a large amount
//	** of uncertainty.  For this reason, scoring is designed to pick plans that
//	** "do the least harm" if the estimates are inaccurate.  For example, a
//	** log(nRow) factor is omitted from a non-covering index scan in order to
//	** bias the scoring in favor of using an index, since the worst-case
//	** performance of using an index is far better than the worst-case performance
//	** of a full table scan.
//	*/
func _whereLoopAddBtree(tls *libc.TLS, pBuilder uintptr, mPrereq TBitmask) (r int32) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var b, iCur, iSortIdx, ii, rc, v5 int32
	var isCov Tu32
	var nLookup, rLogSize, rSize TLogEst
	var pFirst, pNew, pProbe, pSrc, pTab, pTabList, pTerm, pTerm1, pWC, pWC2, pWCEnd, pWInfo, v2 uintptr
	var _ /* aiColumnPk at bp+108 */ Ti16
	var _ /* aiRowEstPk at bp+104 */ [2]TLogEst
	var _ /* m at bp+112 */ TBitmask
	var _ /* sPk at bp+0 */ TIndex
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = b, iCur, iSortIdx, ii, isCov, nLookup, pFirst, pNew, pProbe, pSrc, pTab, pTabList, pTerm, pTerm1, pWC, pWC2, pWCEnd, pWInfo, rLogSize, rSize, rc, v2, v5 /* The aiRowLogEst[] value for the sPk index */
	**(**Ti16)(__ccgo_up(bp + 108)) = int16(-int32(1))                                                                                                                                                                             /* Template WhereLoop object */
	rc = SQLITE_OK                                                                                                                                                                                                                 /* Return code */
	iSortIdx = int32(1)                                                                                                                                                                                                            /* Table being queried */
	pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew
	pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo
	pTabList = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList
	pSrc = pTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pNew)).FiTab)*48
	pTab = (*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab
	pWC = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC
	if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x2>>1) != 0 {
		/* An INDEXED BY clause specifies a particular index to use */
		pProbe = *(*uintptr)(unsafe.Pointer(pSrc + 36))
	} else {
		if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) {
			pProbe = (*TTable)(unsafe.Pointer(pTab)).FpIndex
		} else { /* First of real indices on the table */
			libc.Xmemset(tls, bp, 0, uint32(104))
			(**(**TIndex)(__ccgo_up(bp))).FnKeyCol = uint16(1)
			(**(**TIndex)(__ccgo_up(bp))).FnColumn = uint16(1)
			(**(**TIndex)(__ccgo_up(bp))).FaiColumn = bp + 108
			(**(**TIndex)(__ccgo_up(bp))).FaiRowLogEst = bp + 104
			(**(**TIndex)(__ccgo_up(bp))).FonError = uint8(OE_Replace)
			(**(**TIndex)(__ccgo_up(bp))).FpTable = pTab
			(**(**TIndex)(__ccgo_up(bp))).FszIdxRow = int16(3) /* TUNING: Interior rows of IPK table are very small */
			libc.SetBitFieldPtr16Uint32(bp+56, libc.Uint32FromInt32(SQLITE_IDXTYPE_IPK), 0, 0x3)
			(**(**[2]TLogEst)(__ccgo_up(bp + 104)))[0] = (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst
			(**(**[2]TLogEst)(__ccgo_up(bp + 104)))[int32(1)] = 0
			pFirst = (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab)).FpIndex
			if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x1>>0) == 0 {
				/* The real indices of the table are only considered if the
				 ** NOT INDEXED qualifier is omitted from the FROM clause */
				(**(**TIndex)(__ccgo_up(bp))).FpNext = pFirst
			}
			pProbe = bp
		}
	}
	rSize = (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst
	/* Automatic indexes */
	if !((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpOrSet != 0) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&(libc.Int32FromInt32(WHERE_RIGHT_JOIN)|libc.Int32FromInt32(WHERE_OR_SUBCLAUSE)) == 0 && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb)).Fflags&uint64(SQLITE_AutoIndex) != uint64(0) && !(int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x2>>1) != 0) && !(int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x1>>0) != 0) && !(int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x10>>4) != 0) && !(int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x80>>7) != 0) && int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&int32(JT_RIGHT) == 0 {
		pWCEnd = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm)*48
		rLogSize = _estLog(tls, rSize)
		pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
		for {
			if !(rc == SQLITE_OK && pTerm < pWCEnd) {
				break
			}
			if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf != 0 {
				goto _1
			}
			if _termCanDriveIndex(tls, pTerm, pSrc, uint64(0)) != 0 {
				(*(*struct {
					FnEq          Tu16
					FnBtm         Tu16
					FnTop         Tu16
					FnDistinctCol Tu16
					FpIndex       uintptr
					FpOrderBy     uintptr
				})(unsafe.Pointer(pNew + 24))).FnEq = uint16(1)
				(*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip = uint16(0)
				(*(*struct {
					FnEq          Tu16
					FnBtm         Tu16
					FnTop         Tu16
					FnDistinctCol Tu16
					FpIndex       uintptr
					FpOrderBy     uintptr
				})(unsafe.Pointer(pNew + 24))).FpIndex = uintptr(0)
				(*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = uint16(1)
				**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm)) = pTerm
				/* TUNING: One-time cost for computing the automatic index is
				 ** estimated to be X*N*log2(N) where N is the number of rows in
				 ** the table being indexed and where X is 7 (LogEst=28) for normal
				 ** tables or 0.5 (LogEst=-10) for views and subqueries.  The value
				 ** of X is smaller for views and subqueries so that the query planner
				 ** will be more aggressive about generating automatic indexes for
				 ** those objects, since there is no opportunity to add schema
				 ** indexes on subqueries and views. */
				(*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = int16(int32(rLogSize) + int32(rSize))
				if !(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == libc.Int32FromInt32(TABTYP_VIEW)) && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Ephemeral) == uint32(0) {
					v2 = pNew + 18
					*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) + libc.Int32FromInt32(28))
				} else {
					v2 = pNew + 18
					*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) - libc.Int32FromInt32(25)) /* Greatly reduced setup cost for auto indexes
					 ** on ephemeral materializations of views */
				}
				if int32((*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup) < 0 {
					(*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0
				}
				/* TUNING: Each index lookup yields 20 rows in the table.  This
				 ** is more than the usual guess of 10 rows, since we have no way
				 ** of knowing how selective the index will ultimately be.  It would
				 ** not be unreasonable to make this value much larger. */
				(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = int16(43)
				(*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = _sqlite3LogEstAdd(tls, rLogSize, (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut)
				(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_AUTO_INDEX)
				(*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = mPrereq | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight
				rc = _whereLoopInsert(tls, pBuilder, pNew)
			}
			goto _1
		_1:
			;
			pTerm += 48
		}
	}
	/* Loop over all indices. If there was an INDEXED BY clause, then only
	 ** consider index pProbe.  */
	for {
		if !(rc == SQLITE_OK && pProbe != 0) {
			break
		}
		if (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere != uintptr(0) && !(_whereUsablePartialIndex(tls, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor, (*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype, pWC, (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere) != 0) {
			/* See ticket [98d973b8f5] */
			goto _4 /* Partial index inappropriate for this query */
		}
		if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x100>>8)) != 0 {
			goto _4
		}
		rSize = **(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst))
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnEq = uint16(0)
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnBtm = uint16(0)
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnTop = uint16(0)
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnDistinctCol = uint16(0)
		(*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip = uint16(0)
		(*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = uint16(0)
		(*TWhereLoop)(unsafe.Pointer(pNew)).FiSortIdx = uint8(0)
		(*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0
		(*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = mPrereq
		(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = rSize
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FpIndex = pProbe
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FpOrderBy = uintptr(0)
		b = _indexMightHelpWithOrderBy(tls, pBuilder, pProbe, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor)
		/* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
		if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_IPK) {
			/* Integer primary key index */
			(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_IPK)
			/* Full table scan */
			if b != 0 {
				v5 = iSortIdx
			} else {
				v5 = 0
			}
			(*TWhereLoop)(unsafe.Pointer(pNew)).FiSortIdx = uint8(v5)
			/* TUNING: Cost of full table scan is 3.0*N.  The 3.0 factor is an
			 ** extra cost designed to discourage the use of full table scans,
			 ** since index lookups have better worst-case performance if our
			 ** stat guesses are wrong.  Reduce the 3.0 penalty slightly
			 ** (to 2.75) if we have valid STAT4 information for the table.
			 ** At 2.75, a full table scan is preferred over using an index on
			 ** a column with just two distinct values where each value has about
			 ** an equal number of appearances.  Without STAT4 data, we still want
			 ** to use an index in that case, since the constraint might be for
			 ** the scarcer of the two values, and in that case an index lookup is
			 ** better.
			 */
			(*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = int16(int32(rSize) + int32(16) - int32(2)*libc.BoolInt32((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasStat4) != uint32(0)))
			_whereLoopOutputAdjust(tls, pWC, pNew, rSize)
			if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x4>>2) != 0 {
				if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x40>>6) != 0 {
					**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_COROUTINE)
				}
				/* Do not set btree.pOrderBy for a recursive CTE. In this case
				 ** the ORDER BY clause does not determine the overall order that
				 ** rows are emitted from the CTE in.  */
				if (*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pSrc + 44)))).FpSelect)).FselFlags&uint32(SF_Recursive) == uint32(0) {
					(*(*struct {
						FnEq          Tu16
						FnBtm         Tu16
						FnTop         Tu16
						FnDistinctCol Tu16
						FpIndex       uintptr
						FpOrderBy     uintptr
					})(unsafe.Pointer(pNew + 24))).FpOrderBy = (*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pSrc + 44)))).FpSelect)).FpOrderBy
				}
			} else {
				if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x40000>>18) != 0 {
					(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = 0
				}
			}
			rc = _whereLoopInsert(tls, pBuilder, pNew)
			(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = rSize
			if rc != 0 {
				break
			}
		} else {
			if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x20>>5)) != 0 {
				**(**TBitmask)(__ccgo_up(bp + 112)) = uint64(0)
				(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_IDX_ONLY) | libc.Int32FromInt32(WHERE_INDEXED))
			} else {
				**(**TBitmask)(__ccgo_up(bp + 112)) = (*TSrcItem)(unsafe.Pointer(pSrc)).FcolUsed & (*TIndex)(unsafe.Pointer(pProbe)).FcolNotIdxed
				if (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere != 0 {
					_wherePartIdxExpr(tls, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse, pProbe, (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere, bp+112, 0, uintptr(0))
				}
				(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_INDEXED)
				if **(**TBitmask)(__ccgo_up(bp + 112)) == libc.Uint64FromInt32(1)<<(int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1)) || int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x800>>11)) != 0 && !(int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x400>>10)) != 0) && **(**TBitmask)(__ccgo_up(bp + 112)) != uint64(0) {
					isCov = _whereIsCoveringIndex(tls, pWInfo, pProbe, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor)
					if isCov == uint32(0) {
					} else {
						**(**TBitmask)(__ccgo_up(bp + 112)) = uint64(0)
						**(**Tu32)(__ccgo_up(pNew + 40)) |= isCov
						if isCov&uint32(WHERE_IDX_ONLY) != 0 {
						} else {
						}
					}
				} else {
					if **(**TBitmask)(__ccgo_up(bp + 112)) == uint64(0) && ((*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) || (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect != uintptr(0) || _sqlite3FaultSim(tls, int32(700)) != 0) {
						(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_IDX_ONLY) | libc.Int32FromInt32(WHERE_INDEXED))
					}
				}
			}
			/* Full scan via index */
			if b != 0 || !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) || (*TIndex)(unsafe.Pointer(pProbe)).FpPartIdxWhere != uintptr(0) || int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x2>>1) != 0 || **(**TBitmask)(__ccgo_up(bp + 112)) == uint64(0) && int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x4>>2)) == 0 && int32((*TIndex)(unsafe.Pointer(pProbe)).FszIdxRow) < int32((*TTable)(unsafe.Pointer(pTab)).FszTabRow) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_ONEPASS_DESIRED) == 0 && _sqlite3Config.FbUseCis != 0 && (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_CoverIdxScan)) == uint32(0) {
				if b != 0 {
					v5 = iSortIdx
				} else {
					v5 = 0
				}
				(*TWhereLoop)(unsafe.Pointer(pNew)).FiSortIdx = uint8(v5)
				/* The cost of visiting the index rows is N*K, where K is
				 ** between 1.1 and 3.0, depending on the relative sizes of the
				 ** index and table rows. */
				(*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = int16(int32(rSize) + int32(1) + int32(15)*int32((*TIndex)(unsafe.Pointer(pProbe)).FszIdxRow)/int32((*TTable)(unsafe.Pointer(pTab)).FszTabRow))
				if **(**TBitmask)(__ccgo_up(bp + 112)) != uint64(0) {
					/* If this is a non-covering index scan, add in the cost of
					 ** doing table lookups.  The cost will be 3x the number of
					 ** lookups.  Take into account WHERE clause terms that can be
					 ** satisfied using just the index, and that do not require a
					 ** table lookup. */
					nLookup = int16(int32(rSize) + int32(16))
					iCur = (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor
					pWC2 = pWInfo + 80
					ii = 0
					for {
						if !(ii < (*TWhereClause)(unsafe.Pointer(pWC2)).FnTerm) {
							break
						}
						pTerm1 = (*TWhereClause)(unsafe.Pointer(pWC2)).Fa + uintptr(ii)*48
						if !(_sqlite3ExprCoveredByIndex(tls, (*TWhereTerm)(unsafe.Pointer(pTerm1)).FpExpr, iCur, pProbe) != 0) {
							break
						}
						/* pTerm can be evaluated using just the index.  So reduce
						 ** the expected number of table lookups accordingly */
						if int32((*TWhereTerm)(unsafe.Pointer(pTerm1)).FtruthProb) <= 0 {
							nLookup = int16(int32(nLookup) + int32((*TWhereTerm)(unsafe.Pointer(pTerm1)).FtruthProb))
						} else {
							nLookup = nLookup - 1
							if int32((*TWhereTerm)(unsafe.Pointer(pTerm1)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0 {
								nLookup = int16(int32(nLookup) - libc.Int32FromInt32(19))
							}
						}
						goto _7
					_7:
						;
						ii = ii + 1
					}
					(*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = _sqlite3LogEstAdd(tls, (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun, nLookup)
				}
				_whereLoopOutputAdjust(tls, pWC, pNew, rSize)
				if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 && (*TIndex)(unsafe.Pointer(pProbe)).FaColExpr != 0 {
					/* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
					 ** because the cursor used to access the index might not be
					 ** positioned to the correct row during the right-join no-match
					 ** loop. */
				} else {
					if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x40000>>18) != 0 {
						(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = 0
					}
					rc = _whereLoopInsert(tls, pBuilder, pNew)
				}
				(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = rSize
				if rc != 0 {
					break
				}
			}
		}
		(*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FbldFlags1 = uint8(0)
		rc = _whereLoopAddBtreeIndex(tls, pBuilder, pSrc, pProbe, 0)
		if int32((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FbldFlags1) == int32(SQLITE_BLDF1_INDEXED) {
			/* If a non-unique index is used, or if a prefix of the key for
			 ** unique index is used (making the index functionally non-unique)
			 ** then the sqlite_stat1 data becomes important for scoring the
			 ** plan */
			**(**Tu32)(__ccgo_up(pTab + 28)) |= uint32(TF_MaybeReanalyze)
		}
		_sqlite3Stat4ProbeFree(tls, (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpRec)
		(*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid = 0
		(*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpRec = uintptr(0)
		goto _4
	_4:
		;
		if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x2>>1) != 0 {
			v2 = uintptr(0)
		} else {
			v2 = (*TIndex)(unsafe.Pointer(pProbe)).FpNext
		}
		pProbe = v2
		iSortIdx = iSortIdx + 1
	}
	return rc
}

// C documentation
//
//	/*
//	** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
//	** index pIndex. Try to match one more.
//	**
//	** When this function is called, pBuilder->pNew->nOut contains the
//	** number of rows expected to be visited by filtering using the nEq
//	** terms only. If it is modified, this value is restored before this
//	** function returns.
//	**
//	** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
//	** a fake index used for the INTEGER PRIMARY KEY.
//	*/
func _whereLoopAddBtreeIndex(tls *libc.TLS, pBuilder uintptr, pSrc uintptr, pProbe uintptr, nInMul TLogEst) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var M, logK, nIter, nOutUnadjusted, rCostIdx, rLogSize, rSize, saved_nOut, x TLogEst
	var bRedundant, i, iCol, nEq, nIn, nRecValid, nVecLen, opMask, rc, v21 int32
	var db, pBtm, pExpr, pExpr1, pNew, pParse, pTerm, pTop, pWInfo, v2 uintptr
	var eOp, saved_nBtm, saved_nEq, saved_nLTerm, saved_nSkip, saved_nTop, v4 Tu16
	var saved_prereq TBitmask
	var saved_wsFlags Tu32
	var v22 bool
	var _ /* nOut at bp+96 */ TtRowcnt
	var _ /* scan at bp+0 */ TWhereScan
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = M, bRedundant, db, eOp, i, iCol, logK, nEq, nIn, nIter, nOutUnadjusted, nRecValid, nVecLen, opMask, pBtm, pExpr, pExpr1, pNew, pParse, pTerm, pTop, pWInfo, rCostIdx, rLogSize, rSize, rc, saved_nBtm, saved_nEq, saved_nLTerm, saved_nOut, saved_nSkip, saved_nTop, saved_prereq, saved_wsFlags, x, v2, v21, v22, v4
	pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo /* WHERE analyze context */
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse          /* Parsing context */
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb                      /* Original value of pNew->nOut */
	rc = SQLITE_OK                                                  /* Logarithm of table size */
	pTop = uintptr(0)
	pBtm = uintptr(0) /* Top and bottom range constraints */
	pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return (*TParse)(unsafe.Pointer(pParse)).Frc
	}
	if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_BTM_LIMIT) != 0 {
		opMask = libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ))
	} else {
		opMask = libc.Int32FromInt32(WO_EQ) | libc.Int32FromInt32(WO_IN) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_ISNULL) | libc.Int32FromInt32(WO_IS)
	}
	if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x4>>2)) != 0 {
		opMask = opMask & ^(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) | libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LE)-libc.Int32FromInt32(TK_EQ)))
	}
	saved_nEq = (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pNew + 24))).FnEq
	saved_nBtm = (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pNew + 24))).FnBtm
	saved_nTop = (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pNew + 24))).FnTop
	saved_nSkip = (*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip
	saved_nLTerm = (*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm
	saved_wsFlags = (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags
	saved_prereq = (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq
	saved_nOut = (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut
	pTerm = _whereScanInit(tls, bp, (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor, int32(saved_nEq), uint32(opMask), pProbe)
	(*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0
	rSize = **(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst))
	rLogSize = _estLog(tls, rSize)
	for {
		if !(rc == SQLITE_OK && pTerm != uintptr(0)) {
			break
		}
		eOp = (*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator /* nOut before IN() and WHERE adjustments */
		nIn = 0
		nRecValid = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid
		if (int32(eOp) == int32(WO_ISNULL) || int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VNULL) != 0) && _indexColumnNotNull(tls, pProbe, int32(saved_nEq)) != 0 {
			goto _1 /* ignore IS [NOT] NULL constraints on NOT NULL columns */
		}
		if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf != 0 {
			goto _1
		}
		/* Do not allow the upper bound of a LIKE optimization range constraint
		 ** to mix with a lower range bound from some other source */
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_LIKEOPT) != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) == libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) {
			goto _1
		}
		if int32((*TSrcItem)(unsafe.Pointer(pSrc)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)|libc.Int32FromInt32(JT_RIGHT)) != 0 && !(_constraintCompatibleWithOuterJoin(tls, pTerm, pSrc) != 0) {
			goto _1
		}
		if int32((*TIndex)(unsafe.Pointer(pProbe)).FonError) != OE_None && int32(saved_nEq) == int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol)-int32(1) {
			v2 = pBuilder + 24
			*(*uint8)(unsafe.Pointer(v2)) = uint8(int32(*(*uint8)(unsafe.Pointer(v2))) | libc.Int32FromInt32(SQLITE_BLDF1_UNIQUE))
		} else {
			v2 = pBuilder + 24
			*(*uint8)(unsafe.Pointer(v2)) = uint8(int32(*(*uint8)(unsafe.Pointer(v2))) | libc.Int32FromInt32(SQLITE_BLDF1_INDEXED))
		}
		(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = saved_wsFlags
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnEq = saved_nEq
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnBtm = saved_nBtm
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnTop = saved_nTop
		(*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = saved_nLTerm
		if int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm) >= int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLSlot) && _whereLoopResize(tls, db, pNew, int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)+int32(1)) != 0 {
			break /* OOM while trying to enlarge the pNew->aLTerm array */
		}
		v2 = pNew + 44
		v4 = *(*Tu16)(unsafe.Pointer(v2))
		*(*Tu16)(unsafe.Pointer(v2)) = *(*Tu16)(unsafe.Pointer(v2)) + 1
		**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(v4)*4)) = pTerm
		(*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = (saved_prereq | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight) & ^(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf
		if int32(eOp)&int32(WO_IN) != 0 {
			pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
			if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
				bRedundant = 0
				nIn = int32(46)
				/* The expression may actually be of the form (x, y) IN (SELECT...).
				 ** In this case there is a separate term for each of (x) and (y).
				 ** However, the nIn multiplier should only be applied once, not once
				 ** for each such term. The following loop checks that pTerm is the
				 ** first such term in use, and sets nIn back to 0 if it is not. */
				i = 0
				for {
					if !(i < int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)-int32(1)) {
						break
					}
					if **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(i)*4)) != 0 && (*TWhereTerm)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(i)*4)))).FpExpr == pExpr {
						nIn = 0
						if (*(*struct {
							FleftColumn int32
							FiField     int32
						})(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(i)*4)) + 24))).FiField == (*(*struct {
							FleftColumn int32
							FiField     int32
						})(unsafe.Pointer(pTerm + 24))).FiField {
							/* Detect when two or more columns of an index match the same
							 ** column of a vector IN operater, and avoid adding the column
							 ** to the WhereLoop more than once.  See tag-20250707-01
							 ** in test/rowvalue.test */
							bRedundant = int32(1)
						}
					}
					goto _6
				_6:
					;
					i = i + 1
				}
				if bRedundant != 0 {
					(*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = (*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm - 1
					goto _1
				}
			} else {
				if *(*uintptr)(unsafe.Pointer(pExpr + 20)) != 0 && (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr != 0 {
					/* "x IN (value, value, ...)" */
					nIn = int32(_sqlite3LogEst(tls, uint64((*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FnExpr)))
				}
			}
			if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x80>>7)) != 0 && int32(rLogSize) >= int32(10) {
				/* Let:
				 **   N = the total number of rows in the table
				 **   K = the number of entries on the RHS of the IN operator
				 **   M = the number of rows in the table that match terms to the
				 **       to the left in the same index.  If the IN operator is on
				 **       the left-most index column, M==N.
				 **
				 ** Given the definitions above, it is better to omit the IN operator
				 ** from the index lookup and instead do a scan of the M elements,
				 ** testing each scanned row against the IN operator separately, if:
				 **
				 **        M*log(K) < K*log(N)
				 **
				 ** Our estimates for M, K, and N might be inaccurate, so we build in
				 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
				 ** with the index, as using an index has better worst-case behavior.
				 ** If we do not have real sqlite_stat1 data, always prefer to use
				 ** the index.  Do not bother with this optimization on very small
				 ** tables (less than 2 rows) as it is pointless in that case.
				 */
				M = **(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(saved_nEq)*2))
				logK = _estLog(tls, int16(nIn))
				/* TUNING      v-----  10 to bias toward indexed IN */
				x = int16(int32(M) + int32(logK) + int32(10) - (nIn + int32(rLogSize)))
				if int32(x) >= 0 {
				} else {
					if int32(nInMul) < int32(2) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_SeekScan)) == uint32(0) {
						**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_IN_SEEKSCAN)
					} else {
						goto _1
					}
				}
			}
			**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_COLUMN_IN)
		} else {
			if int32(eOp)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0 {
				iCol = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiColumn + uintptr(saved_nEq)*2)))
				**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_COLUMN_EQ)
				if iCol == -int32(1) || iCol >= 0 && int32(nInMul) == 0 && int32(saved_nEq) == int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol)-int32(1) {
					if iCol == -int32(1) || int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x8>>3)) != 0 || int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol) == int32(1) && (*TIndex)(unsafe.Pointer(pProbe)).FonError != 0 && int32(eOp)&int32(WO_EQ) != 0 {
						**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_ONEROW)
					} else {
						**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_UNQ_WANTED)
					}
				}
				if int32((**(**TWhereScan)(__ccgo_up(bp))).FiEquiv) > int32(1) {
					**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_TRANSCONS)
				}
			} else {
				if int32(eOp)&int32(WO_ISNULL) != 0 {
					**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_COLUMN_NULL)
				} else {
					nVecLen = _whereRangeVectorLen(tls, pParse, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor, pProbe, int32(saved_nEq), pTerm)
					if int32(eOp)&(libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ))|libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GE)-libc.Int32FromInt32(TK_EQ))) != 0 {
						**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(libc.Int32FromInt32(WHERE_COLUMN_RANGE) | libc.Int32FromInt32(WHERE_BTM_LIMIT))
						(*(*struct {
							FnEq          Tu16
							FnBtm         Tu16
							FnTop         Tu16
							FnDistinctCol Tu16
							FpIndex       uintptr
							FpOrderBy     uintptr
						})(unsafe.Pointer(pNew + 24))).FnBtm = uint16(nVecLen)
						pBtm = pTerm
						pTop = uintptr(0)
						if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_LIKEOPT) != 0 {
							/* Range constraints that come from the LIKE optimization are
							 ** always used in pairs. */
							pTop = pTerm + 1*48
							if _whereLoopResize(tls, db, pNew, int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)+int32(1)) != 0 {
								break
							} /* OOM */
							v2 = pNew + 44
							v4 = *(*Tu16)(unsafe.Pointer(v2))
							*(*Tu16)(unsafe.Pointer(v2)) = *(*Tu16)(unsafe.Pointer(v2)) + 1
							**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(v4)*4)) = pTop
							**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_TOP_LIMIT)
							(*(*struct {
								FnEq          Tu16
								FnBtm         Tu16
								FnTop         Tu16
								FnDistinctCol Tu16
								FpIndex       uintptr
								FpOrderBy     uintptr
							})(unsafe.Pointer(pNew + 24))).FnTop = uint16(1)
						}
					} else {
						**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(libc.Int32FromInt32(WHERE_COLUMN_RANGE) | libc.Int32FromInt32(WHERE_TOP_LIMIT))
						(*(*struct {
							FnEq          Tu16
							FnBtm         Tu16
							FnTop         Tu16
							FnDistinctCol Tu16
							FpIndex       uintptr
							FpOrderBy     uintptr
						})(unsafe.Pointer(pNew + 24))).FnTop = uint16(nVecLen)
						pTop = pTerm
						if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_BTM_LIMIT) != uint32(0) {
							v2 = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)-int32(2))*4))
						} else {
							v2 = uintptr(0)
						}
						pBtm = v2
					}
				}
			}
		}
		/* At this point pNew->nOut is set to the number of rows expected to
		 ** be visited by the index scan before considering term pTerm, or the
		 ** values of nIn and nInMul. In other words, assuming that all
		 ** "x IN(...)" terms are replaced with "x = ?". This block updates
		 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul).  */
		if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_COLUMN_RANGE) != 0 {
			/* Adjust nOut using stat4 data. Or, if there is no stat4
			 ** data, using some other estimate.  */
			_whereRangeScanEst(tls, pParse, pBuilder, pBtm, pTop, pNew)
		} else {
			v2 = pNew + 24
			*(*Tu16)(unsafe.Pointer(v2)) = *(*Tu16)(unsafe.Pointer(v2)) + 1
			v4 = *(*Tu16)(unsafe.Pointer(v2))
			nEq = int32(v4)
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb) <= 0 && int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiColumn + uintptr(saved_nEq)*2))) >= 0 {
				v2 = pNew + 22
				*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) + int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb))
				v2 = pNew + 22
				*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) - nIn)
			} else {
				**(**TtRowcnt)(__ccgo_up(bp + 96)) = uint64(0)
				if int32(nInMul) == 0 && (*TIndex)(unsafe.Pointer(pProbe)).FnSample != 0 && int32((*(*struct {
					FnEq          Tu16
					FnBtm         Tu16
					FnTop         Tu16
					FnDistinctCol Tu16
					FpIndex       uintptr
					FpOrderBy     uintptr
				})(unsafe.Pointer(pNew + 24))).FnEq) <= (*TIndex)(unsafe.Pointer(pProbe)).FnSampleCol && (int32(eOp)&int32(WO_IN) == 0 || (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(EP_xIsSelect) == uint32(0)) && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0) {
					pExpr1 = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
					if int32(eOp)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_ISNULL)|libc.Int32FromInt32(WO_IS)) != 0 {
						rc = _whereEqualScanEst(tls, pParse, pBuilder, (*TExpr)(unsafe.Pointer(pExpr1)).FpRight, bp+96)
					} else {
						rc = _whereInScanEst(tls, pParse, pBuilder, *(*uintptr)(unsafe.Pointer(pExpr1 + 20)), bp+96)
					}
					if rc == int32(SQLITE_NOTFOUND) {
						rc = SQLITE_OK
					}
					if rc != SQLITE_OK {
						break
					} /* Jump out of the pTerm loop */
					if **(**TtRowcnt)(__ccgo_up(bp + 96)) != 0 {
						(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = _sqlite3LogEst(tls, **(**TtRowcnt)(__ccgo_up(bp + 96)))
						if nEq == int32(1) && int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut)+int32(10) > int32(**(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst))) {
							v2 = pTerm + 10
							*(*Tu16)(unsafe.Pointer(v2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v2))) | libc.Int32FromInt32(TERM_HIGHTRUTH))
							if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_HEURTRUTH) != 0 {
								/* If the term has previously been used with an assumption of
								 ** higher selectivity, then set the flag to rerun the
								 ** loop computations. */
								v2 = pBuilder + 25
								*(*uint8)(unsafe.Pointer(v2)) = uint8(int32(*(*uint8)(unsafe.Pointer(v2))) | libc.Int32FromInt32(SQLITE_BLDF2_2NDPASS))
							}
						}
						if int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut) > int32(saved_nOut) {
							(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut
						}
						v2 = pNew + 22
						*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) - nIn)
					}
				}
				if **(**TtRowcnt)(__ccgo_up(bp + 96)) == uint64(0) {
					v2 = pNew + 22
					*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) + (int32(**(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(nEq)*2))) - int32(**(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(nEq-int32(1))*2)))))
					if int32(eOp)&int32(WO_ISNULL) != 0 {
						/* TUNING: If there is no likelihood() value, assume that a
						 ** "col IS NULL" expression matches twice as many rows
						 ** as (col=?). */
						v2 = pNew + 22
						*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) + libc.Int32FromInt32(10))
					}
				}
			}
		}
		/* Set rCostIdx to the estimated cost of visiting selected rows in the
		 ** index.  The estimate is the sum of two values:
		 **   1.  The cost of doing one search-by-key to find the first matching
		 **       entry
		 **   2.  Stepping forward in the index pNew->nOut times to find all
		 **       additional matching entries.
		 */
		if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_IPK) {
			/* The pProbe->szIdxRow is low for an IPK table since the interior
			 ** pages are small.  Thus szIdxRow gives a good estimate of seek cost.
			 ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
			 ** under-estimate the scanning cost. */
			rCostIdx = int16(int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut) + int32(16))
		} else {
			rCostIdx = int16(int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut) + int32(1) + int32(15)*int32((*TIndex)(unsafe.Pointer(pProbe)).FszIdxRow)/int32((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab)).FszTabRow))
		}
		rCostIdx = _sqlite3LogEstAdd(tls, rLogSize, rCostIdx)
		/* Estimate the cost of running the loop.  If all data is coming
		 ** from the index, then this is just the cost of doing the index
		 ** lookup and scan.  But if some data is coming out of the main table,
		 ** we also have to add in the cost of doing pNew->nOut searches to
		 ** locate the row in the main table that corresponds to the index entry.
		 */
		(*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = rCostIdx
		if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_IDX_ONLY)|libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_EXPRIDX)) == uint32(0) {
			(*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = _sqlite3LogEstAdd(tls, (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun, int16(int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut)+int32(16)))
		}
		nOutUnadjusted = (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut
		v2 = pNew + 20
		*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) + (int32(nInMul) + nIn))
		v2 = pNew + 22
		*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) + (int32(nInMul) + nIn))
		_whereLoopOutputAdjust(tls, (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC, pNew, rSize)
		if int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x40000>>18) != 0 {
			(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = 0
		}
		rc = _whereLoopInsert(tls, pBuilder, pNew)
		if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_COLUMN_RANGE) != 0 {
			(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut
		} else {
			(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = nOutUnadjusted
		}
		if (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags&uint32(WHERE_TOP_LIMIT) == uint32(0) && int32((*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnEq) < int32((*TIndex)(unsafe.Pointer(pProbe)).FnColumn) && (int32((*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnEq) < int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol) || int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x3>>0)) != int32(SQLITE_IDXTYPE_PRIMARYKEY)) {
			if int32((*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pNew + 24))).FnEq) > int32(3) {
				_sqlite3ProgressCheck(tls, pParse)
			}
			_whereLoopAddBtreeIndex(tls, pBuilder, pSrc, pProbe, int16(int32(nInMul)+nIn))
		}
		(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut
		(*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FnRecValid = nRecValid
		goto _1
	_1:
		;
		pTerm = _whereScanNext(tls, bp)
	}
	(*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = saved_prereq
	(*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pNew + 24))).FnEq = saved_nEq
	(*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pNew + 24))).FnBtm = saved_nBtm
	(*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pNew + 24))).FnTop = saved_nTop
	(*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip = saved_nSkip
	(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = saved_wsFlags
	(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut
	(*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = saved_nLTerm
	/* Consider using a skip-scan if there are no WHERE clause constraints
	 ** available for the left-most terms of the index, and if the average
	 ** number of repeats in the left-most terms is at least 18.
	 **
	 ** The magic number 18 is selected on the basis that scanning 17 rows
	 ** is almost always quicker than an index seek (even though if the index
	 ** contains fewer than 2^17 rows we assume otherwise in other parts of
	 ** the code). And, even if it is not, it should not be too much slower.
	 ** On the other hand, the extra seeks could end up being significantly
	 ** more expensive.  */
	if v22 = int32(saved_nEq) == int32(saved_nSkip) && int32(saved_nEq)+int32(1) < int32((*TIndex)(unsafe.Pointer(pProbe)).FnKeyCol) && int32(saved_nEq) == int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm) && int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x40>>6)) == 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 56))&0x80>>7)) != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_SkipScan)) == uint32(0) && int32(**(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(int32(saved_nEq)+int32(1))*2))) >= int32(42) && int32(*(*uint32)(unsafe.Pointer(pSrc + 12 + 4))&0x40000>>18) == 0; v22 {
		v21 = _whereLoopResize(tls, db, pNew, int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)+int32(1))
		rc = v21
	}
	if v22 && v21 == SQLITE_OK {
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnEq = (*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnEq + 1
		(*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip = (*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip + 1
		v2 = pNew + 44
		v4 = *(*Tu16)(unsafe.Pointer(v2))
		*(*Tu16)(unsafe.Pointer(v2)) = *(*Tu16)(unsafe.Pointer(v2)) + 1
		**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(v4)*4)) = uintptr(0)
		**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_SKIPSCAN)
		nIter = int16(int32(**(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(saved_nEq)*2))) - int32(**(**TLogEst)(__ccgo_up((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst + uintptr(int32(saved_nEq)+int32(1))*2))))
		v2 = pNew + 22
		*(*TLogEst)(unsafe.Pointer(v2)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v2))) - int32(nIter))
		/* TUNING:  Because uncertainties in the estimates for skip-scan queries,
		 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
		nIter = int16(int32(nIter) + libc.Int32FromInt32(5))
		_whereLoopAddBtreeIndex(tls, pBuilder, pSrc, pProbe, int16(int32(nIter)+int32(nInMul)))
		(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = saved_nOut
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pNew + 24))).FnEq = saved_nEq
		(*TWhereLoop)(unsafe.Pointer(pNew)).FnSkip = saved_nSkip
		(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = saved_wsFlags
	}
	return rc
}

// C documentation
//
//	/*
//	** Add WhereLoop entries to handle OR terms.  This works for either
//	** btrees or virtual tables.
//	*/
func _whereLoopAddOr(tls *libc.TLS, pBuilder uintptr, mPrereq TBitmask, mUnusable TBitmask) (r int32) {
	bp := tls.Alloc(624)
	defer tls.Free(624)
	var i, iCur, j, once, rc int32
	var pItem, pNew, pOrTerm, pOrWC, pOrWCEnd, pTerm, pWC, pWCEnd, pWInfo uintptr
	var _ /* sCur at bp+504 */ TWhereOrSet
	var _ /* sPrev at bp+560 */ TWhereOrSet
	var _ /* sSubBuild at bp+416 */ TWhereLoopBuilder
	var _ /* sSum at bp+448 */ TWhereOrSet
	var _ /* tempWC at bp+0 */ TWhereClause
	_, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iCur, j, once, pItem, pNew, pOrTerm, pOrWC, pOrWCEnd, pTerm, pWC, pWCEnd, pWInfo, rc
	pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo
	rc = SQLITE_OK
	pWC = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC
	pWCEnd = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm)*48
	pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew
	libc.Xmemset(tls, bp+448, 0, uint32(56))
	pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pNew)).FiTab)*48
	iCur = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor
	/* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
	if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&int32(JT_RIGHT) != 0 {
		return SQLITE_OK
	}
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
	for {
		if !(pTerm < pWCEnd && rc == SQLITE_OK) {
			break
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_OR) != 0 && (*TWhereOrInfo)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)))).Findexable&(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf != uint64(0) {
			pOrWC = *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu))
			pOrWCEnd = (*TWhereClause)(unsafe.Pointer(pOrWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pOrWC)).FnTerm)*48
			once = int32(1)
			**(**TWhereLoopBuilder)(__ccgo_up(bp + 416)) = **(**TWhereLoopBuilder)(__ccgo_up(pBuilder))
			(**(**TWhereLoopBuilder)(__ccgo_up(bp + 416))).FpOrSet = bp + 504
			pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWC)).Fa
			for {
				if !(pOrTerm < pOrWCEnd) {
					break
				}
				if int32((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FeOperator)&int32(WO_AND) != 0 {
					(**(**TWhereLoopBuilder)(__ccgo_up(bp + 416))).FpWC = *(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pOrTerm)).Fu))
				} else {
					if (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor == iCur {
						(**(**TWhereClause)(__ccgo_up(bp))).FpWInfo = (*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo
						(**(**TWhereClause)(__ccgo_up(bp))).FpOuter = pWC
						(**(**TWhereClause)(__ccgo_up(bp))).Fop = uint8(TK_AND)
						(**(**TWhereClause)(__ccgo_up(bp))).FnTerm = int32(1)
						(**(**TWhereClause)(__ccgo_up(bp))).FnBase = int32(1)
						(**(**TWhereClause)(__ccgo_up(bp))).Fa = pOrTerm
						(**(**TWhereLoopBuilder)(__ccgo_up(bp + 416))).FpWC = bp
					} else {
						goto _2
					}
				}
				(**(**TWhereOrSet)(__ccgo_up(bp + 504))).Fn = uint16(0)
				if int32((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSTab)).FeTabType) == int32(TABTYP_VTAB) {
					rc = _whereLoopAddVirtual(tls, bp+416, mPrereq, mUnusable)
				} else {
					rc = _whereLoopAddBtree(tls, bp+416, mPrereq)
				}
				if rc == SQLITE_OK {
					rc = _whereLoopAddOr(tls, bp+416, mPrereq, mUnusable)
				}
				if int32((**(**TWhereOrSet)(__ccgo_up(bp + 504))).Fn) == 0 {
					(**(**TWhereOrSet)(__ccgo_up(bp + 448))).Fn = uint16(0)
					break
				} else {
					if once != 0 {
						_whereOrMove(tls, bp+448, bp+504)
						once = 0
					} else {
						_whereOrMove(tls, bp+560, bp+448)
						(**(**TWhereOrSet)(__ccgo_up(bp + 448))).Fn = uint16(0)
						i = 0
						for {
							if !(i < int32((**(**TWhereOrSet)(__ccgo_up(bp + 560))).Fn)) {
								break
							}
							j = 0
							for {
								if !(j < int32((**(**TWhereOrSet)(__ccgo_up(bp + 504))).Fn)) {
									break
								}
								_whereOrInsert(tls, bp+448, (**(**TWhereOrCost)(__ccgo_up(bp + 560 + 8 + uintptr(i)*16))).Fprereq|(**(**TWhereOrCost)(__ccgo_up(bp + 504 + 8 + uintptr(j)*16))).Fprereq, _sqlite3LogEstAdd(tls, (**(**TWhereOrCost)(__ccgo_up(bp + 560 + 8 + uintptr(i)*16))).FrRun, (**(**TWhereOrCost)(__ccgo_up(bp + 504 + 8 + uintptr(j)*16))).FrRun), _sqlite3LogEstAdd(tls, (**(**TWhereOrCost)(__ccgo_up(bp + 560 + 8 + uintptr(i)*16))).FnOut, (**(**TWhereOrCost)(__ccgo_up(bp + 504 + 8 + uintptr(j)*16))).FnOut))
								goto _4
							_4:
								;
								j = j + 1
							}
							goto _3
						_3:
							;
							i = i + 1
						}
					}
				}
				goto _2
			_2:
				;
				pOrTerm += 48
			}
			(*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = uint16(1)
			**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm)) = pTerm
			(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_MULTI_OR)
			(*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0
			(*TWhereLoop)(unsafe.Pointer(pNew)).FiSortIdx = uint8(0)
			libc.Xmemset(tls, pNew+24, 0, uint32(16))
			i = 0
			for {
				if !(rc == SQLITE_OK && i < int32((**(**TWhereOrSet)(__ccgo_up(bp + 448))).Fn)) {
					break
				}
				/* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
				 ** of all sub-scans required by the OR-scan. However, due to rounding
				 ** errors, it may be that the cost of the OR-scan is equal to its
				 ** most expensive sub-scan. Add the smallest possible penalty
				 ** (equivalent to multiplying the cost by 1.07) to ensure that
				 ** this does not happen. Otherwise, for WHERE clauses such as the
				 ** following where there is an index on "y":
				 **
				 **     WHERE likelihood(x=?, 0.99) OR y=?
				 **
				 ** the planner may elect to "OR" together a full-table scan and an
				 ** index lookup. And other similarly odd results.  */
				(*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = int16(int32((**(**TWhereOrCost)(__ccgo_up(bp + 448 + 8 + uintptr(i)*16))).FrRun) + int32(1))
				(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = (**(**TWhereOrCost)(__ccgo_up(bp + 448 + 8 + uintptr(i)*16))).FnOut
				(*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = (**(**TWhereOrCost)(__ccgo_up(bp + 448 + 8 + uintptr(i)*16))).Fprereq
				rc = _whereLoopInsert(tls, pBuilder, pNew)
				goto _5
			_5:
				;
				i = i + 1
			}
		}
		goto _1
	_1:
		;
		pTerm += 48
	}
	return rc
}

// C documentation
//
//	/*
//	** Add all WhereLoop objects for a table of the join identified by
//	** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
//	**
//	** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
//	** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
//	** entries that occur before the virtual table in the FROM clause and are
//	** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
//	** mUnusable mask contains all FROM clause entries that occur after the
//	** virtual table and are separated from it by at least one LEFT or
//	** CROSS JOIN.
//	**
//	** For example, if the query were:
//	**
//	**   ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
//	**
//	** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
//	**
//	** All the tables in mPrereq must be scanned before the current virtual
//	** table. So any terms for which all prerequisites are satisfied by
//	** mPrereq may be specified as "usable" in all calls to xBestIndex.
//	** Conversely, all tables in mUnusable must be scanned after the current
//	** virtual table, so any terms for which the prerequisites overlap with
//	** mUnusable should always be configured as "not-usable" for xBestIndex.
//	*/
func _whereLoopAddVirtual(tls *libc.TLS, pBuilder uintptr, mPrereq TBitmask, mUnusable TBitmask) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, iTerm, nConstraint, rc, seenZero, seenZeroNoIN int32
	var mBest, mBestNoIn, mNext, mPrev, mThis, v1 TBitmask
	var p, pNew, pParse, pSrc, pWC, pWInfo uintptr
	var v2 bool
	var _ /* bIn at bp+0 */ int32
	var _ /* bRetry at bp+8 */ int32
	var _ /* mNoOmit at bp+4 */ Tu16
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iTerm, mBest, mBestNoIn, mNext, mPrev, mThis, nConstraint, p, pNew, pParse, pSrc, pWC, pWInfo, rc, seenZero, seenZeroNoIN, v1, v2
	rc = SQLITE_OK
	**(**int32)(__ccgo_up(bp + 8)) = 0 /* True to retry with LIMIT/OFFSET disabled */
	pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse
	pWC = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC
	pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew
	pSrc = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pNew)).FiTab)*48
	p = _allocateIndexInfo(tls, pWInfo, pWC, mUnusable, pSrc, bp+4)
	if p == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	(*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0
	(*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_VIRTUALTABLE)
	(*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = uint16(0)
	libc.SetBitFieldPtr8Uint32(pNew+24+4, libc.Uint32FromInt32(0), 0, 0x1)
	nConstraint = (*Tsqlite3_index_info)(unsafe.Pointer(p)).FnConstraint
	if _whereLoopResize(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pNew, nConstraint) != 0 {
		_freeIndexInfo(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p)
		return int32(SQLITE_NOMEM)
	}
	/* First call xBestIndex() with all constraints usable. */
	rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, uint64(-libc.Int32FromInt32(1)), uint16(0), p, **(**Tu16)(__ccgo_up(bp + 4)), bp, bp+8)
	if **(**int32)(__ccgo_up(bp + 8)) != 0 {
		rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, uint64(-libc.Int32FromInt32(1)), uint16(0), p, **(**Tu16)(__ccgo_up(bp + 4)), bp, uintptr(0))
	}
	/* If the call to xBestIndex() with all terms enabled produced a plan
	 ** that does not require any source tables (IOW: a plan with mBest==0)
	 ** and does not use an IN(...) operator, then there is no point in making
	 ** any further calls to xBestIndex() since they will all return the same
	 ** result (if the xBestIndex() implementation is sane). */
	if v2 = rc == SQLITE_OK; v2 {
		v1 = (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq & ^mPrereq
		mBest = v1
	}
	if v2 && (v1 != uint64(0) || **(**int32)(__ccgo_up(bp)) != 0) {
		seenZero = 0     /* True if a plan with no prereqs seen */
		seenZeroNoIN = 0 /* Plan with no prereqs and no IN(...) seen */
		mPrev = uint64(0)
		mBestNoIn = uint64(0)
		/* If the plan produced by the earlier call uses an IN(...) term, call
		 ** xBestIndex again, this time with IN(...) terms disabled. */
		if **(**int32)(__ccgo_up(bp)) != 0 {
			rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, uint64(-libc.Int32FromInt32(1)), uint16(WO_IN), p, **(**Tu16)(__ccgo_up(bp + 4)), bp, uintptr(0))
			mBestNoIn = (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq & ^mPrereq
			if mBestNoIn == uint64(0) {
				seenZero = int32(1)
				seenZeroNoIN = int32(1)
			}
		}
		/* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
		 ** in the set of terms that apply to the current virtual table.  */
		for rc == SQLITE_OK {
			mNext = uint64(-libc.Int32FromInt32(1))
			i = 0
			for {
				if !(i < nConstraint) {
					break
				}
				iTerm = (**(**Tsqlite3_index_constraint)(__ccgo_up((*Tsqlite3_index_info)(unsafe.Pointer(p)).FaConstraint + uintptr(i)*12))).FiTermOffset
				mThis = (*TWhereTerm)(unsafe.Pointer(_termFromWhereClause(tls, pWC, iTerm))).FprereqRight & ^mPrereq
				if mThis > mPrev && mThis < mNext {
					mNext = mThis
				}
				goto _3
			_3:
				;
				i = i + 1
			}
			mPrev = mNext
			if mNext == uint64(-libc.Int32FromInt32(1)) {
				break
			}
			if mNext == mBest || mNext == mBestNoIn {
				continue
			}
			rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, mNext|mPrereq, uint16(0), p, **(**Tu16)(__ccgo_up(bp + 4)), bp, uintptr(0))
			if (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq == mPrereq {
				seenZero = int32(1)
				if **(**int32)(__ccgo_up(bp)) == 0 {
					seenZeroNoIN = int32(1)
				}
			}
		}
		/* If the calls to xBestIndex() in the above loop did not find a plan
		 ** that requires no source tables at all (i.e. one guaranteed to be
		 ** usable), make a call here with all source tables disabled */
		if rc == SQLITE_OK && seenZero == 0 {
			rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, mPrereq, uint16(0), p, **(**Tu16)(__ccgo_up(bp + 4)), bp, uintptr(0))
			if **(**int32)(__ccgo_up(bp)) == 0 {
				seenZeroNoIN = int32(1)
			}
		}
		/* If the calls to xBestIndex() have so far failed to find a plan
		 ** that requires no source tables at all and does not use an IN(...)
		 ** operator, make a final call to obtain one here.  */
		if rc == SQLITE_OK && seenZeroNoIN == 0 {
			rc = _whereLoopAddVirtualOne(tls, pBuilder, mPrereq, mPrereq, uint16(WO_IN), p, **(**Tu16)(__ccgo_up(bp + 4)), bp, uintptr(0))
		}
	}
	_freeIndexInfo(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p)
	return rc
}

// C documentation
//
//	/*
//	** Argument pIdxInfo is already populated with all constraints that may
//	** be used by the virtual table identified by pBuilder->pNew->iTab. This
//	** function marks a subset of those constraints usable, invokes the
//	** xBestIndex method and adds the returned plan to pBuilder.
//	**
//	** A constraint is marked usable if:
//	**
//	**   * Argument mUsable indicates that its prerequisites are available, and
//	**
//	**   * It is not one of the operators specified in the mExclude mask passed
//	**     as the fourth argument (which in practice is either WO_IN or 0).
//	**
//	** Argument mPrereq is a mask of tables that must be scanned before the
//	** virtual table in question. These are added to the plans prerequisites
//	** before it is added to pBuilder.
//	**
//	** Output parameter *pbIn is set to true if the plan added to pBuilder
//	** uses one or more WO_IN terms, or false otherwise.
//	*/
func _whereLoopAddVirtualOne(tls *libc.TLS, pBuilder uintptr, mPrereq TBitmask, mUsable TBitmask, mExclude Tu16, pIdxInfo uintptr, mNoOmit Tu16, pbIn uintptr, pbRetryLimit uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, iTerm, j, mxTerm, nConstraint, rc, v3 int32
	var pHidden, pIdxCons, pNew, pParse, pSrc, pTerm, pTerm1, pUsage, pWC, v4 uintptr
	var v5 bool
	var v7 uint32
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iTerm, j, mxTerm, nConstraint, pHidden, pIdxCons, pNew, pParse, pSrc, pTerm, pTerm1, pUsage, pWC, rc, v3, v4, v5, v7
	pWC = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC
	pHidden = pIdxInfo + 1*72
	pUsage = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FaConstraintUsage
	rc = SQLITE_OK
	pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew
	pParse = (*TWhereInfo)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo)).FpParse
	pSrc = (*TWhereInfo)(unsafe.Pointer((*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pNew)).FiTab)*48
	nConstraint = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint
	**(**int32)(__ccgo_up(pbIn)) = 0
	(*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = mPrereq
	/* Set the usable flag on the subset of constraints identified by
	 ** arguments mUsable and mExclude. */
	pIdxCons = **(**uintptr)(__ccgo_up(pIdxInfo + 4))
	i = 0
	for {
		if !(i < nConstraint) {
			break
		}
		pTerm = _termFromWhereClause(tls, pWC, (*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).FiTermOffset)
		(*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).Fusable = uint8(0)
		if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight&mUsable == (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(mExclude) == 0 && (pbRetryLimit != 0 || !(_isLimitTerm(tls, pTerm) != 0)) {
			(*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).Fusable = uint8(1)
		}
		goto _1
	_1:
		;
		i = i + 1
		pIdxCons += 12
	}
	/* Initialize the output fields of the sqlite3_index_info structure */
	libc.Xmemset(tls, pUsage, 0, uint32(8)*uint32(nConstraint))
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr = uintptr(0)
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum = 0
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).ForderByConsumed = 0
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = float64(1e+99) / libc.Float64FromInt32(2)
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(25)
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxFlags = 0
	(*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FmHandleIn = uint32(0)
	/* Invoke the virtual table xBestIndex() method */
	rc = _vtabBestIndex(tls, pParse, (*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab, pIdxInfo)
	if rc != 0 {
		if rc == int32(SQLITE_CONSTRAINT) {
			/* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
			 ** that the particular combination of parameters provided is unusable.
			 ** Make no entries in the loop table.
			 */
			_freeIdxStr(tls, pIdxInfo)
			return SQLITE_OK
		}
		return rc
	}
	mxTerm = -int32(1)
	libc.Xmemset(tls, (*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm, 0, uint32(4)*uint32(nConstraint))
	libc.Xmemset(tls, pNew+24, 0, uint32(16))
	pIdxCons = **(**uintptr)(__ccgo_up(pIdxInfo + 4))
	i = 0
	for {
		if !(i < nConstraint) {
			break
		}
		v3 = (**(**Tsqlite3_index_constraint_usage)(__ccgo_up(pUsage + uintptr(i)*8))).FargvIndex - libc.Int32FromInt32(1)
		iTerm = v3
		if v3 >= 0 {
			j = (*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).FiTermOffset
			if v5 = iTerm >= nConstraint || j < 0; !v5 {
				v4 = _termFromWhereClause(tls, pWC, j)
				pTerm1 = v4
			}
			if v5 || v4 == uintptr(0) || **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(iTerm)*4)) != uintptr(0) || int32((*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).Fusable) == 0 {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25255, libc.VaList(bp+8, (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab)).FzName))
				_freeIdxStr(tls, pIdxInfo)
				return int32(SQLITE_ERROR)
			}
			**(**TBitmask)(__ccgo_up(pNew)) |= (*TWhereTerm)(unsafe.Pointer(pTerm1)).FprereqRight
			**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(iTerm)*4)) = pTerm1
			if iTerm > mxTerm {
				mxTerm = iTerm
			}
			if (**(**Tsqlite3_index_constraint_usage)(__ccgo_up(pUsage + uintptr(i)*8))).Fomit != 0 {
				if i < int32(16) && int32(1)<<i&int32(mNoOmit) == 0 {
					v4 = pNew + 24 + 6
					*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(1)<<iTerm)
				} else {
				}
				if int32((*TWhereTerm)(unsafe.Pointer(pTerm1)).FeMatchOp) == int32(SQLITE_INDEX_CONSTRAINT_OFFSET) {
					libc.SetBitFieldPtr8Uint32(pNew+24+4, libc.Uint32FromInt32(1), 1, 0x2)
				}
			}
			if i <= int32(31) {
				v7 = libc.Uint32FromInt32(1) << i
			} else {
				v7 = uint32(0)
			}
			if v7&(*THiddenIndexInfo)(unsafe.Pointer(pHidden)).FmHandleIn != 0 {
				(*(*struct {
					FidxNum    int32
					F__ccgo4   uint8
					FisOrdered Ti8
					FomitMask  Tu16
					FidxStr    uintptr
					FmHandleIn Tu32
				})(unsafe.Pointer(pNew + 24))).FmHandleIn |= libc.Uint32FromInt32(1) << iTerm
			} else {
				if int32((*TWhereTerm)(unsafe.Pointer(pTerm1)).FeOperator)&int32(WO_IN) != 0 {
					/* A virtual table that is constrained by an IN clause may not
					 ** consume the ORDER BY clause because (1) the order of IN terms
					 ** is not necessarily related to the order of output terms and
					 ** (2) Multiple outputs from a single IN value will not merge
					 ** together.  */
					(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).ForderByConsumed = 0
					**(**int32)(__ccgo_up(pIdxInfo + 56)) &= ^libc.Int32FromInt32(SQLITE_INDEX_SCAN_UNIQUE)
					**(**int32)(__ccgo_up(pbIn)) = int32(1)
				}
			}
			/* Unless pbRetryLimit is non-NULL, there should be no LIMIT/OFFSET
			 ** terms. And if there are any, they should follow all other terms. */
			if _isLimitTerm(tls, pTerm1) != 0 && (**(**int32)(__ccgo_up(pbIn)) != 0 || !(_allConstraintsUsed(tls, pUsage, i) != 0)) {
				/* If there is an IN(...) term handled as an == (separate call to
				 ** xFilter for each value on the RHS of the IN) and a LIMIT or
				 ** OFFSET term handled as well, the plan is unusable. Similarly,
				 ** if there is a LIMIT/OFFSET and there are other unused terms,
				 ** the plan cannot be used. In these cases set variable *pbRetryLimit
				 ** to true to tell the caller to retry with LIMIT and OFFSET
				 ** disabled. */
				_freeIdxStr(tls, pIdxInfo)
				**(**int32)(__ccgo_up(pbRetryLimit)) = int32(1)
				return SQLITE_OK
			}
		}
		goto _2
	_2:
		;
		i = i + 1
		pIdxCons += 12
	}
	(*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm = uint16(mxTerm + int32(1))
	i = 0
	for {
		if !(i <= mxTerm) {
			break
		}
		if **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(i)*4)) == uintptr(0) {
			/* The non-zero argvIdx values must be contiguous.  Raise an
			 ** error if they are not */
			_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25255, libc.VaList(bp+8, (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpSTab)).FzName))
			_freeIdxStr(tls, pIdxInfo)
			return int32(SQLITE_ERROR)
		}
		goto _8
	_8:
		;
		i = i + 1
	}
	(*(*struct {
		FidxNum    int32
		F__ccgo4   uint8
		FisOrdered Ti8
		FomitMask  Tu16
		FidxStr    uintptr
		FmHandleIn Tu32
	})(unsafe.Pointer(pNew + 24))).FidxNum = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxNum
	libc.SetBitFieldPtr8Uint32(pNew+24+4, uint32((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FneedToFreeIdxStr), 0, 0x1)
	(*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FneedToFreeIdxStr = 0
	(*(*struct {
		FidxNum    int32
		F__ccgo4   uint8
		FisOrdered Ti8
		FomitMask  Tu16
		FidxStr    uintptr
		FmHandleIn Tu32
	})(unsafe.Pointer(pNew + 24))).FidxStr = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr
	if (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).ForderByConsumed != 0 {
		v3 = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnOrderBy
	} else {
		v3 = 0
	}
	(*(*struct {
		FidxNum    int32
		F__ccgo4   uint8
		FisOrdered Ti8
		FomitMask  Tu16
		FidxStr    uintptr
		FmHandleIn Tu32
	})(unsafe.Pointer(pNew + 24))).FisOrdered = int8(v3)
	libc.SetBitFieldPtr8Uint32(pNew+24+4, libc.BoolUint32((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxFlags&libc.Int32FromInt32(SQLITE_INDEX_SCAN_HEX) != libc.Int32FromInt32(0)), 2, 0x4)
	(*TWhereLoop)(unsafe.Pointer(pNew)).FrSetup = 0
	(*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = _sqlite3LogEstFromDouble(tls, (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost)
	(*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = _sqlite3LogEst(tls, uint64((*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows))
	/* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
	 ** that the scan will visit at most one row. Clear it otherwise. */
	if (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxFlags&int32(SQLITE_INDEX_SCAN_UNIQUE) != 0 {
		**(**Tu32)(__ccgo_up(pNew + 40)) |= uint32(WHERE_ONEROW)
	} else {
		**(**Tu32)(__ccgo_up(pNew + 40)) &= uint32(^libc.Int32FromInt32(WHERE_ONEROW))
	}
	rc = _whereLoopInsert(tls, pBuilder, pNew)
	if int32(Tu32(*(*uint8)(unsafe.Pointer(pNew + 24 + 4))&0x1>>0)) != 0 {
		Xsqlite3_free(tls, (*(*struct {
			FidxNum    int32
			F__ccgo4   uint8
			FisOrdered Ti8
			FomitMask  Tu16
			FidxStr    uintptr
			FmHandleIn Tu32
		})(unsafe.Pointer(pNew + 24))).FidxStr)
		libc.SetBitFieldPtr8Uint32(pNew+24+4, libc.Uint32FromInt32(0), 0, 0x1)
	}
	return rc
}

// C documentation
//
//	/*
//	** Return TRUE if X is a proper subset of Y but is of equal or less cost.
//	** In other words, return true if all constraints of X are also part of Y
//	** and Y has additional constraints that might speed the search that X lacks
//	** but the cost of running X is not more than the cost of running Y.
//	**
//	** In other words, return true if the cost relationship between X and Y
//	** is inverted and needs to be adjusted.
//	**
//	** Case 1:
//	**
//	**   (1a)  X and Y use the same index.
//	**   (1b)  X has fewer == terms than Y
//	**   (1c)  Neither X nor Y use skip-scan
//	**   (1d)  X does not have a a greater cost than Y
//	**
//	** Case 2:
//	**
//	**   (2a)  X has the same or lower cost, or returns the same or fewer rows,
//	**         than Y.
//	**   (2b)  X uses fewer WHERE clause terms than Y
//	**   (2c)  Every WHERE clause term used by X is also used by Y
//	**   (2d)  X skips at least as many columns as Y
//	**   (2e)  If X is a covering index, than Y is too
//	*/
func _whereLoopCheaperProperSubset(tls *libc.TLS, pX uintptr, pY uintptr) (r int32) {
	var i, j int32
	_, _ = i, j
	if int32((*TWhereLoop)(unsafe.Pointer(pX)).FrRun) > int32((*TWhereLoop)(unsafe.Pointer(pY)).FrRun) && int32((*TWhereLoop)(unsafe.Pointer(pX)).FnOut) > int32((*TWhereLoop)(unsafe.Pointer(pY)).FnOut) {
		return 0
	} /* (1d) and (2a) */
	if int32((*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pX + 24))).FnEq) < int32((*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pY + 24))).FnEq) && (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pX + 24))).FpIndex == (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pY + 24))).FpIndex && int32((*TWhereLoop)(unsafe.Pointer(pX)).FnSkip) == 0 && int32((*TWhereLoop)(unsafe.Pointer(pY)).FnSkip) == 0 {
		return int32(1) /* Case 1 is true */
	}
	if int32((*TWhereLoop)(unsafe.Pointer(pX)).FnLTerm)-int32((*TWhereLoop)(unsafe.Pointer(pX)).FnSkip) >= int32((*TWhereLoop)(unsafe.Pointer(pY)).FnLTerm)-int32((*TWhereLoop)(unsafe.Pointer(pY)).FnSkip) {
		return 0 /* (2b) */
	}
	if int32((*TWhereLoop)(unsafe.Pointer(pY)).FnSkip) > int32((*TWhereLoop)(unsafe.Pointer(pX)).FnSkip) {
		return 0
	} /* (2d) */
	i = int32((*TWhereLoop)(unsafe.Pointer(pX)).FnLTerm) - int32(1)
	for {
		if !(i >= 0) {
			break
		}
		if **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pX)).FaLTerm + uintptr(i)*4)) == uintptr(0) {
			goto _1
		}
		j = int32((*TWhereLoop)(unsafe.Pointer(pY)).FnLTerm) - int32(1)
		for {
			if !(j >= 0) {
				break
			}
			if **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pY)).FaLTerm + uintptr(j)*4)) == **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pX)).FaLTerm + uintptr(i)*4)) {
				break
			}
			goto _2
		_2:
			;
			j = j - 1
		}
		if j < 0 {
			return 0
		} /* (2c) */
		goto _1
	_1:
		;
		i = i - 1
	}
	if (*TWhereLoop)(unsafe.Pointer(pX)).FwsFlags&uint32(WHERE_IDX_ONLY) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pY)).FwsFlags&uint32(WHERE_IDX_ONLY) == uint32(0) {
		return 0 /* (2e) */
	}
	return int32(1) /* Case 2 is true */
}

// C documentation
//
//	/*
//	** Deallocate internal memory used by a WhereLoop object.  Leave the
//	** object in an initialized state, as if it had been newly allocated.
//	*/
func _whereLoopClear(tls *libc.TLS, db uintptr, p uintptr) {
	if (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm != p+60 {
		_sqlite3DbFreeNN(tls, db, (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm)
		(*TWhereLoop)(unsafe.Pointer(p)).FaLTerm = p + 60
		(*TWhereLoop)(unsafe.Pointer(p)).FnLSlot = uint16(int32(libc.Uint32FromInt64(12) / libc.Uint32FromInt64(4)))
	}
	_whereLoopClearUnion(tls, db, p)
	(*TWhereLoop)(unsafe.Pointer(p)).FnLTerm = uint16(0)
	(*TWhereLoop)(unsafe.Pointer(p)).FwsFlags = uint32(0)
}

// C documentation
//
//	/*
//	** Search the list of WhereLoops in *ppPrev looking for one that can be
//	** replaced by pTemplate.
//	**
//	** Return NULL if pTemplate does not belong on the WhereLoop list.
//	** In other words if pTemplate ought to be dropped from further consideration.
//	**
//	** If pX is a WhereLoop that pTemplate can replace, then return the
//	** link that points to pX.
//	**
//	** If pTemplate cannot replace any existing element of the list but needs
//	** to be added to the list as a new entry, then return a pointer to the
//	** tail of the list.
//	*/
func _whereLoopFindLesser(tls *libc.TLS, ppPrev uintptr, pTemplate uintptr) (r uintptr) {
	var p uintptr
	_ = p
	p = **(**uintptr)(__ccgo_up(ppPrev))
	for {
		if !(p != 0) {
			break
		}
		if int32((*TWhereLoop)(unsafe.Pointer(p)).FiTab) != int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FiTab) || int32((*TWhereLoop)(unsafe.Pointer(p)).FiSortIdx) != int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FiSortIdx) {
			/* If either the iTab or iSortIdx values for two WhereLoop are different
			 ** then those WhereLoops need to be considered separately.  Neither is
			 ** a candidate to replace the other. */
			goto _1
		}
		/* In the current implementation, the rSetup value is either zero
		 ** or the cost of building an automatic index (NlogN) and the NlogN
		 ** is the same for compatible WhereLoops. */
		/* whereLoopAddBtree() always generates and inserts the automatic index
		 ** case first.  Hence compatible candidate WhereLoops never have a larger
		 ** rSetup. Call this SETUP-INVARIANT */
		/* Any loop using an application-defined index (or PRIMARY KEY or
		 ** UNIQUE constraint) with one or more == constraints is better
		 ** than an automatic index. Unless it is a skip-scan. */
		if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(WHERE_AUTO_INDEX) != uint32(0) && int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnSkip) == 0 && (*TWhereLoop)(unsafe.Pointer(pTemplate)).FwsFlags&uint32(WHERE_INDEXED) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(pTemplate)).FwsFlags&uint32(WHERE_COLUMN_EQ) != uint32(0) && (*TWhereLoop)(unsafe.Pointer(p)).Fprereq&(*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq == (*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq {
			break
		}
		/* If existing WhereLoop p is better than pTemplate, pTemplate can be
		 ** discarded.  WhereLoop p is better if:
		 **   (1)  p has no more dependencies than pTemplate, and
		 **   (2)  p has an equal or lower cost than pTemplate
		 */
		if (*TWhereLoop)(unsafe.Pointer(p)).Fprereq&(*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq == (*TWhereLoop)(unsafe.Pointer(p)).Fprereq && int32((*TWhereLoop)(unsafe.Pointer(p)).FrSetup) <= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrSetup) && int32((*TWhereLoop)(unsafe.Pointer(p)).FrRun) <= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun) && int32((*TWhereLoop)(unsafe.Pointer(p)).FnOut) <= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut) {
			return uintptr(0) /* Discard pTemplate */
		}
		/* If pTemplate is always better than p, then cause p to be overwritten
		 ** with pTemplate.  pTemplate is better than p if:
		 **   (1)  pTemplate has no more dependencies than p, and
		 **   (2)  pTemplate has an equal or lower cost than p.
		 */
		if (*TWhereLoop)(unsafe.Pointer(p)).Fprereq&(*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq == (*TWhereLoop)(unsafe.Pointer(pTemplate)).Fprereq && int32((*TWhereLoop)(unsafe.Pointer(p)).FrRun) >= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FrRun) && int32((*TWhereLoop)(unsafe.Pointer(p)).FnOut) >= int32((*TWhereLoop)(unsafe.Pointer(pTemplate)).FnOut) {
			/* SETUP-INVARIANT above */
			break /* Cause p to be overwritten by pTemplate */
		}
		goto _1
	_1:
		;
		ppPrev = p + 56
		p = **(**uintptr)(__ccgo_up(ppPrev))
	}
	return ppPrev
}

// C documentation
//
//	/*
//	** Convert bulk memory into a valid WhereLoop that can be passed
//	** to whereLoopClear harmlessly.
//	*/
func _whereLoopInit(tls *libc.TLS, p uintptr) {
	(*TWhereLoop)(unsafe.Pointer(p)).FaLTerm = p + 60
	(*TWhereLoop)(unsafe.Pointer(p)).FnLTerm = uint16(0)
	(*TWhereLoop)(unsafe.Pointer(p)).FnLSlot = uint16(int32(libc.Uint32FromInt64(12) / libc.Uint32FromInt64(4)))
	(*TWhereLoop)(unsafe.Pointer(p)).FwsFlags = uint32(0)
}

// C documentation
//
//	/*
//	** Loop pLoop is a WHERE_INDEXED level that uses at least one IN(...)
//	** operator. Return true if level pLoop is guaranteed to visit only one
//	** row for each key generated for the index.
//	*/
func _whereLoopIsOneRow(tls *libc.TLS, pLoop uintptr) (r int32) {
	var ii int32
	_ = ii
	if (*TIndex)(unsafe.Pointer((*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FpIndex)).FonError != 0 && int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) == 0 && int32((*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FnEq) == int32((*TIndex)(unsafe.Pointer((*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FpIndex)).FnKeyCol) {
		ii = 0
		for {
			if !(ii < int32((*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FnEq)) {
				break
			}
			if int32((*TWhereTerm)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(ii)*4)))).FeOperator)&(libc.Int32FromInt32(WO_IS)|libc.Int32FromInt32(WO_ISNULL)) != 0 {
				return 0
			}
			goto _1
		_1:
			;
			ii = ii + 1
		}
		return int32(1)
	}
	return 0
}

// C documentation
//
//	/*
//	** Adjust the WhereLoop.nOut value downward to account for terms of the
//	** WHERE clause that reference the loop but which are not used by an
//	** index.
//	*
//	** For every WHERE clause term that is not used by the index
//	** and which has a truth probability assigned by one of the likelihood(),
//	** likely(), or unlikely() SQL functions, reduce the estimated number
//	** of output rows by the probability specified.
//	**
//	** TUNING:  For every WHERE clause term that is not used by the index
//	** and which does not have an assigned truth probability, heuristics
//	** described below are used to try to estimate the truth probability.
//	** TODO --> Perhaps this is something that could be improved by better
//	** table statistics.
//	**
//	** Heuristic 1:  Estimate the truth probability as 93.75%.  The 93.75%
//	** value corresponds to -1 in LogEst notation, so this means decrement
//	** the WhereLoop.nOut field for every such WHERE clause term.
//	**
//	** Heuristic 2:  If there exists one or more WHERE clause terms of the
//	** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
//	** final output row estimate is no greater than 1/4 of the total number
//	** of rows in the table.  In other words, assume that x==EXPR will filter
//	** out at least 3 out of 4 rows.  If EXPR is -1 or 0 or 1, then maybe the
//	** "x" column is boolean or else -1 or 0 or 1 is a common default value
//	** on the "x" column and so in that case only cap the output row estimate
//	** at 1/2 instead of 1/4.
//	**
//	** Heuristic 3:  If there is a LIKE or GLOB (or REGEXP or MATCH) operator
//	** with a large constant pattern, then reduce the size of the search
//	** space according to the length of the pattern, under the theory that
//	** longer patterns are less likely to match.  This heuristic was added
//	** to give better output-row count estimates when preparing queries for
//	** the Join-Order Benchmarks.  See forum thread 2026-01-30T09:57:54z
//	*/
func _whereLoopOutputAdjust(tls *libc.TLS, pWC uintptr, pLoop uintptr, nRow TLogEst) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var eOp, i, j, szPattern int32
	var iReduce TLogEst
	var notAllowed TBitmask
	var pOpExpr, pRHS, pRight, pTerm, pX, v3 uintptr
	var _ /* k at bp+0 */ int32
	_, _, _, _, _, _, _, _, _, _, _, _ = eOp, i, iReduce, j, notAllowed, pOpExpr, pRHS, pRight, pTerm, pX, szPattern, v3
	notAllowed = ^((*TWhereLoop)(unsafe.Pointer(pLoop)).Fprereq | (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf)
	iReduce = 0 /* pLoop->nOut should not exceed nRow-iReduce */
	i = (*TWhereClause)(unsafe.Pointer(pWC)).FnBase
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
	for {
		if !(i > 0) {
			break
		}
		if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&notAllowed != uint64(0) {
			goto _1
		}
		if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf == uint64(0) {
			goto _1
		}
		if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VIRTUAL) != 0 {
			goto _1
		}
		j = int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm) - int32(1)
		for {
			if !(j >= 0) {
				break
			}
			pX = **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4))
			if pX == uintptr(0) {
				goto _2
			}
			if pX == pTerm {
				break
			}
			if (*TWhereTerm)(unsafe.Pointer(pX)).FiParent >= 0 && (*TWhereClause)(unsafe.Pointer(pWC)).Fa+uintptr((*TWhereTerm)(unsafe.Pointer(pX)).FiParent)*48 == pTerm {
				break
			}
			goto _2
		_2:
			;
			j = j - 1
		}
		if j < 0 {
			_sqlite3ProgressCheck(tls, (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse)
			if (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf == (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll {
				/* If there are extra terms in the WHERE clause not used by an index
				 ** that depend only on the table being scanned, and that will tend to
				 ** cause many rows to be omitted, then mark that table as
				 ** "self-culling".
				 **
				 ** 2022-03-24:  Self-culling only applies if either the extra terms
				 ** are straight comparison operators that are non-true with NULL
				 ** operand, or if the loop is not an OUTER JOIN.
				 */
				if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(0x3f) != 0 || int32((*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab)*48))).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) == 0 {
					**(**Tu32)(__ccgo_up(pLoop + 40)) |= uint32(WHERE_SELFCULL)
				}
			}
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb) <= 0 {
				/* If a truth probability is specified using the likelihood() hints,
				 ** then use the probability provided by the application. */
				v3 = pLoop + 22
				*(*TLogEst)(unsafe.Pointer(v3)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v3))) + int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb))
			} else {
				/* In the absence of explicit truth probabilities, use heuristics to
				 ** guess a reasonable truth probability. */
				pOpExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
				(*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut = (*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut - 1
				if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_HIGHTRUTH) == 0 {
					pRight = (*TExpr)(unsafe.Pointer(pOpExpr)).FpRight
					**(**int32)(__ccgo_up(bp)) = 0
					if _sqlite3ExprIsInteger(tls, pRight, bp, uintptr(0)) != 0 && **(**int32)(__ccgo_up(bp)) >= -int32(1) && **(**int32)(__ccgo_up(bp)) <= int32(1) {
						**(**int32)(__ccgo_up(bp)) = int32(10)
					} else {
						**(**int32)(__ccgo_up(bp)) = int32(20)
					}
					if int32(iReduce) < **(**int32)(__ccgo_up(bp)) {
						v3 = pTerm + 10
						*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | libc.Int32FromInt32(TERM_HEURTRUTH))
						iReduce = int16(**(**int32)(__ccgo_up(bp)))
					}
				} else {
					if (*TExpr)(unsafe.Pointer(pOpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InfixFunc)) != uint32(0) && int32((*TExpr)(unsafe.Pointer(pOpExpr)).Fop) == int32(TK_FUNCTION) {
						eOp = _sqlite3ExprIsLikeOperator(tls, pOpExpr)
						if eOp > 0 {
							pRHS = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOpExpr + 20)) + 8))).FpExpr
							eOp = libc.BoolInt32(eOp == int32(SQLITE_INDEX_CONSTRAINT_LIKE))
							szPattern = _estLikePatternLength(tls, pRHS, uint16(eOp))
							if szPattern > 0 {
								v3 = pLoop + 22
								*(*TLogEst)(unsafe.Pointer(v3)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v3))) - szPattern*libc.Int32FromInt32(2))
							}
						}
					}
				}
			}
		}
		goto _1
	_1:
		;
		i = i - 1
		pTerm += 48
	}
	if int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut) > int32(nRow)-int32(iReduce) {
		(*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut = int16(int32(nRow) - int32(iReduce))
	}
}

// C documentation
//
//	/*
//	** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
//	*/
func _whereLoopResize(tls *libc.TLS, db uintptr, p uintptr, n int32) (r int32) {
	var paNew uintptr
	_ = paNew
	if int32((*TWhereLoop)(unsafe.Pointer(p)).FnLSlot) >= n {
		return SQLITE_OK
	}
	n = (n + int32(7)) & ^libc.Int32FromInt32(7)
	paNew = _sqlite3DbMallocRawNN(tls, db, uint64(uint32(4)*uint32(n)))
	if paNew == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemcpy(tls, paNew, (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm, uint32(4)*uint32((*TWhereLoop)(unsafe.Pointer(p)).FnLSlot))
	if (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm != p+60 {
		_sqlite3DbFreeNN(tls, db, (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm)
	}
	(*TWhereLoop)(unsafe.Pointer(p)).FaLTerm = paNew
	(*TWhereLoop)(unsafe.Pointer(p)).FnLSlot = uint16(n)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Transfer content from the second pLoop into the first.
//	*/
func _whereLoopXfer(tls *libc.TLS, db uintptr, pTo uintptr, pFrom uintptr) (r int32) {
	_whereLoopClearUnion(tls, db, pTo)
	if int32((*TWhereLoop)(unsafe.Pointer(pFrom)).FnLTerm) > int32((*TWhereLoop)(unsafe.Pointer(pTo)).FnLSlot) && _whereLoopResize(tls, db, pTo, int32((*TWhereLoop)(unsafe.Pointer(pFrom)).FnLTerm)) != 0 {
		libc.Xmemset(tls, pTo, 0, uint32(libc.UintptrFromInt32(0)+48))
		return int32(SQLITE_NOMEM)
	}
	libc.Xmemcpy(tls, pTo, pFrom, uint32(libc.UintptrFromInt32(0)+48))
	libc.Xmemcpy(tls, (*TWhereLoop)(unsafe.Pointer(pTo)).FaLTerm, (*TWhereLoop)(unsafe.Pointer(pFrom)).FaLTerm, uint32((*TWhereLoop)(unsafe.Pointer(pTo)).FnLTerm)*uint32(4))
	if (*TWhereLoop)(unsafe.Pointer(pFrom)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != 0 {
		libc.SetBitFieldPtr8Uint32(pFrom+24+4, libc.Uint32FromInt32(0), 0, 0x1)
	} else {
		if (*TWhereLoop)(unsafe.Pointer(pFrom)).FwsFlags&uint32(WHERE_AUTO_INDEX) != uint32(0) {
			(*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pFrom + 24))).FpIndex = uintptr(0)
		}
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Return the N-th AND-connected subterm of pTerm.  Or if pTerm is not
//	** a conjunction, then return just pTerm when N==0.  If N is exceeds
//	** the number of available subterms, return NULL.
//	*/
func _whereNthSubterm(tls *libc.TLS, pTerm uintptr, N int32) (r uintptr) {
	var v1 uintptr
	_ = v1
	if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) != int32(WO_AND) {
		if N == 0 {
			v1 = pTerm
		} else {
			v1 = uintptr(0)
		}
		return v1
	}
	if N < (*TWhereAndInfo)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)))).Fwc.FnTerm {
		return (*TWhereAndInfo)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&(*TWhereTerm)(unsafe.Pointer(pTerm)).Fu)))).Fwc.Fa + uintptr(N)*48
	}
	return uintptr(0)
}

// C documentation
//
//	/* Attempt to omit tables from a join that do not affect the result.
//	** For a table to not affect the result, the following must be true:
//	**
//	**   1) The query must not be an aggregate.
//	**   2) The table must be the RHS of a LEFT JOIN.
//	**   3) Either the query must be DISTINCT, or else the ON or USING clause
//	**      must contain a constraint that limits the scan of the table to
//	**      at most a single row.
//	**   4) The table must not be referenced by any part of the query apart
//	**      from its own USING or ON clause.
//	**   5) The table must not have an inner-join ON or USING clause if there is
//	**      a RIGHT JOIN anywhere in the query.  Otherwise the ON/USING clause
//	**      might move from the right side to the left side of the RIGHT JOIN.
//	**      Note: Due to (2), this condition can only arise if the table is
//	**      the right-most table of a subquery that was flattened into the
//	**      main query and that subquery was the right-hand operand of an
//	**      inner join that held an ON or USING clause.
//	**   6) The ORDER BY clause has 63 or fewer terms
//	**   7) The omit-noop-join optimization is enabled.
//	**
//	** Items (1), (6), and (7) are checked by the caller.
//	**
//	** For example, given:
//	**
//	**     CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
//	**     CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
//	**     CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
//	**
//	** then table t2 can be omitted from the following:
//	**
//	**     SELECT v1, v3 FROM t1
//	**       LEFT JOIN t2 ON (t1.ipk=t2.ipk)
//	**       LEFT JOIN t3 ON (t1.ipk=t3.ipk)
//	**
//	** or from:
//	**
//	**     SELECT DISTINCT v1, v3 FROM t1
//	**       LEFT JOIN t2
//	**       LEFT JOIN t3 ON (t1.ipk=t3.ipk)
//	*/
func _whereOmitNoopJoin(tls *libc.TLS, pWInfo uintptr, notReady TBitmask) (r TBitmask) {
	var hasRightJoin, i, nByte int32
	var m1, tabUsed TBitmask
	var pEnd, pItem, pLoop, pTerm, v4 uintptr
	_, _, _, _, _, _, _, _, _, _ = hasRightJoin, i, m1, nByte, pEnd, pItem, pLoop, pTerm, tabUsed, v4
	/* Preconditions checked by the caller */
	/* These two preconditions checked by the caller combine to guarantee
	 ** condition (1) of the header comment */
	tabUsed = _sqlite3WhereExprListUsage(tls, pWInfo+496, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpResultSet)
	if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy != 0 {
		tabUsed = tabUsed | _sqlite3WhereExprListUsage(tls, pWInfo+496, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)
	}
	hasRightJoin = libc.BoolInt32(int32((*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8))).Ffg.Fjointype)&int32(JT_LTORJ) != 0)
	i = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel) - int32(1)
	for {
		if !(i >= int32(1)) {
			break
		}
		pLoop = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760 + uintptr(i)*88))).FpWLoop
		pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab)*48
		if int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_RIGHT)) != int32(JT_LEFT) {
			goto _1
		}
		if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) == 0 && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_ONEROW) == uint32(0) {
			goto _1
		}
		if tabUsed&(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf != uint64(0) {
			goto _1
		}
		pEnd = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa + uintptr((*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.FnTerm)*48
		pTerm = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa
		for {
			if !(pTerm < pEnd) {
				break
			}
			if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf != uint64(0) {
				if !((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) || *(*int32)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr + 36)) != (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor {
					break
				}
			}
			if hasRightJoin != 0 && (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_InnerON)) != uint32(0) && *(*int32)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr + 36)) == (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor {
				break /* restriction (5) */
			}
			goto _2
		_2:
			;
			pTerm += 48
		}
		if pTerm < pEnd {
			goto _1
		}
		m1 = libc.Uint64FromInt32(1)<<i - uint64(1)
		(*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask = m1&(*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask | (*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask>>libc.Int32FromInt32(1) & ^m1
		notReady = notReady & ^(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf
		pTerm = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FsWC.Fa
		for {
			if !(pTerm < pEnd) {
				break
			}
			if (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll&(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf != uint64(0) {
				v4 = pTerm + 10
				*(*Tu16)(unsafe.Pointer(v4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v4))) | libc.Int32FromInt32(TERM_CODED))
				(*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqAll = uint64(0)
			}
			goto _3
		_3:
			;
			pTerm += 48
		}
		if i != int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-int32(1) {
			nByte = int32(uint32(int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-libc.Int32FromInt32(1)-i) * uint32(88))
			libc.Xmemmove(tls, pWInfo+760+uintptr(i)*88, pWInfo+760+uintptr(i+int32(1))*88, uint32(nByte))
		}
		(*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel - 1
		goto _1
	_1:
		;
		i = i - 1
	}
	return notReady
}

// C documentation
//
//	/*
//	** This function is called for a partial index - one with a WHERE clause - in
//	** two scenarios. In both cases, it determines whether or not the WHERE
//	** clause on the index implies that a column of the table may be safely
//	** replaced by a constant expression. For example, in the following
//	** SELECT:
//	**
//	**   CREATE INDEX i1 ON t1(b, c) WHERE a=<expr>;
//	**   SELECT a, b, c FROM t1 WHERE a=<expr> AND b=?;
//	**
//	** The "a" in the select-list may be replaced by <expr>, iff:
//	**
//	**    (a) <expr> is a constant expression, and
//	**    (b) The (a=<expr>) comparison uses the BINARY collation sequence, and
//	**    (c) Column "a" has an affinity other than NONE or BLOB.
//	**
//	** If argument pItem is NULL, then pMask must not be NULL. In this case this
//	** function is being called as part of determining whether or not pIdx
//	** is a covering index. This function clears any bits in (*pMask)
//	** corresponding to columns that may be replaced by constants as described
//	** above.
//	**
//	** Otherwise, if pItem is not NULL, then this function is being called
//	** as part of coding a loop that uses index pIdx. In this case, add entries
//	** to the Parse.pIdxPartExpr list for each column that can be replaced
//	** by a constant.
//	*/
func _wherePartIdxExpr(tls *libc.TLS, pParse uintptr, pIdx uintptr, pPart uintptr, pMask uintptr, iIdxCur int32, pItem uintptr) {
	var aff Tu8
	var bNullRow int32
	var db, p, pArg, pLeft, pRight uintptr
	_, _, _, _, _, _, _ = aff, bNullRow, db, p, pArg, pLeft, pRight
	if int32((*TExpr)(unsafe.Pointer(pPart)).Fop) == int32(TK_AND) {
		_wherePartIdxExpr(tls, pParse, pIdx, (*TExpr)(unsafe.Pointer(pPart)).FpRight, pMask, iIdxCur, pItem)
		pPart = (*TExpr)(unsafe.Pointer(pPart)).FpLeft
	}
	if int32((*TExpr)(unsafe.Pointer(pPart)).Fop) == int32(TK_EQ) || int32((*TExpr)(unsafe.Pointer(pPart)).Fop) == int32(TK_IS) {
		pLeft = (*TExpr)(unsafe.Pointer(pPart)).FpLeft
		pRight = (*TExpr)(unsafe.Pointer(pPart)).FpRight
		if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) != int32(TK_COLUMN) {
			return
		}
		if !(_sqlite3ExprIsConstant(tls, uintptr(0), pRight) != 0) {
			return
		}
		if !(_sqlite3IsBinary(tls, _sqlite3ExprCompareCollSeq(tls, pParse, pPart)) != 0) {
			return
		}
		if int32((*TExpr)(unsafe.Pointer(pLeft)).FiColumn) < 0 {
			return
		}
		aff = uint8((**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pLeft)).FiColumn)*12))).Faffinity)
		if int32(aff) >= int32(SQLITE_AFF_TEXT) {
			if pItem != 0 {
				db = (*TParse)(unsafe.Pointer(pParse)).Fdb
				p = _sqlite3DbMallocRaw(tls, db, uint64(24))
				if p != 0 {
					bNullRow = libc.BoolInt32(int32((*TSrcItem)(unsafe.Pointer(pItem)).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) != 0)
					(*TIndexedExpr)(unsafe.Pointer(p)).FpExpr = _sqlite3ExprDup(tls, db, pRight, 0)
					(*TIndexedExpr)(unsafe.Pointer(p)).FiDataCur = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor
					(*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCur = iIdxCur
					(*TIndexedExpr)(unsafe.Pointer(p)).FiIdxCol = int32((*TExpr)(unsafe.Pointer(pLeft)).FiColumn)
					(*TIndexedExpr)(unsafe.Pointer(p)).FbMaybeNullRow = uint8(bNullRow)
					(*TIndexedExpr)(unsafe.Pointer(p)).FpIENext = (*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr
					(*TIndexedExpr)(unsafe.Pointer(p)).Faff = aff
					(*TParse)(unsafe.Pointer(pParse)).FpIdxPartExpr = p
					if (*TIndexedExpr)(unsafe.Pointer(p)).FpIENext == uintptr(0) {
						pArg = pParse + 84
						_sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_whereIndexedExprCleanup), pArg)
					}
				}
			} else {
				if int32((*TExpr)(unsafe.Pointer(pLeft)).FiColumn) < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) {
					**(**TBitmask)(__ccgo_up(pMask)) &= ^(libc.Uint64FromInt32(1) << (*TExpr)(unsafe.Pointer(pLeft)).FiColumn)
				}
			}
		}
	}
}

// C documentation
//
//	/* Implementation of the order-by-subquery optimization:
//	**
//	** WhereLoop pLoop, which the iLoop-th term of the nested loop, is really
//	** a subquery or CTE that has an ORDER BY clause.  See if any of the terms
//	** in the subquery ORDER BY clause will satisfy pOrderBy from the outer
//	** query.  Mark off all satisfied terms (by setting bits in *pOBSat) and
//	** return TRUE if they do.  If not, return false.
//	**
//	** Example:
//	**
//	**    CREATE TABLE t1(a,b,c, PRIMARY KEY(a,b));
//	**    CREATE TABLE t2(x,y);
//	**    WITH t3(p,q) AS MATERIALIZED (SELECT x+y, x-y FROM t2 ORDER BY x+y)
//	**       SELECT * FROM t3 JOIN t1 ON a=q ORDER BY p, b;
//	**
//	** The CTE named "t3" comes out in the natural order of "p", so the first
//	** first them of "ORDER BY p,b" is satisfied by a sequential scan of "t3"
//	** and sorting only needs to occur on the second term "b".
//	**
//	** Limitations:
//	**
//	** (1)  The optimization is not applied if the outer ORDER BY contains
//	**      a COLLATE clause.  The optimization might be applied if the
//	**      outer ORDER BY uses NULLS FIRST, NULLS LAST, ASC, and/or DESC as
//	**      long as the subquery ORDER BY does the same.  But if the
//	**      outer ORDER BY uses COLLATE, even a redundant COLLATE, the
//	**      optimization is bypassed.
//	**
//	** (2)  The subquery ORDER BY terms must exactly match subquery result
//	**      columns, including any COLLATE annotations.  This routine relies
//	**      on iOrderByCol to do matching between order by terms and result
//	**      columns, and iOrderByCol will not be set if the result column
//	**      and ORDER BY collations differ.
//	**
//	** (3)  The subquery and outer ORDER BY can be in opposite directions as
//	**      long as  the subquery is materialized.  If the subquery is
//	**      implemented as a co-routine, the sort orders must be in the same
//	**      direction because there is no way to run a co-routine backwards.
//	*/
func _wherePathMatchSubqueryOB(tls *libc.TLS, pWInfo uintptr, pLoop uintptr, iLoop int32, iCur int32, pOrderBy uintptr, pRevMask uintptr, pOBSat uintptr) (r int32) {
	var iOB, jSub int32
	var pOBExpr, pSubOB uintptr
	var rev, revIdx, sfOB, sfSub Tu8
	_, _, _, _, _, _, _, _ = iOB, jSub, pOBExpr, pSubOB, rev, revIdx, sfOB, sfSub /* Index into pSubOB->a[] */
	rev = uint8(0)                                                                /* True if iOB and jSub sort in opposite directions */
	revIdx = uint8(0)                                                             /* Complete ORDER BY on the subquery */
	pSubOB = (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FpOrderBy
	iOB = 0
	for {
		if !(libc.Uint64FromInt32(1)<<iOB&**(**TBitmask)(__ccgo_up(pOBSat)) != uint64(0)) {
			break
		}
		goto _1
	_1:
		;
		iOB = iOB + 1
	}
	jSub = 0
	for {
		if !(jSub < (*TExprList)(unsafe.Pointer(pSubOB)).FnExpr && iOB < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) {
			break
		}
		if int32(*(*Tu16)(unsafe.Pointer(pSubOB + 8 + uintptr(jSub)*20 + 16))) == 0 {
			break
		}
		pOBExpr = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(iOB)*20))).FpExpr
		if int32((*TExpr)(unsafe.Pointer(pOBExpr)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pOBExpr)).Fop) != int32(TK_AGG_COLUMN) {
			break
		}
		if (*TExpr)(unsafe.Pointer(pOBExpr)).FiTable != iCur {
			break
		}
		if int32((*TExpr)(unsafe.Pointer(pOBExpr)).FiColumn) != int32(*(*Tu16)(unsafe.Pointer(pSubOB + 8 + uintptr(jSub)*20 + 16)))-int32(1) {
			break
		}
		if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_GROUPBY) == 0 {
			sfOB = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(iOB)*20))).Ffg.FsortFlags /* sortFlags for iOB */
			sfSub = (*(*TExprList_item)(unsafe.Pointer(pSubOB + 8 + uintptr(jSub)*20))).Ffg.FsortFlags /* sortFlags for jSub */
			if int32(sfSub)&int32(KEYINFO_ORDER_BIGNULL) != int32(sfOB)&int32(KEYINFO_ORDER_BIGNULL) {
				break
			}
			revIdx = uint8(int32(sfSub) & int32(KEYINFO_ORDER_DESC))
			if jSub > 0 {
				if int32(rev)^int32(revIdx) != int32(sfOB)&int32(KEYINFO_ORDER_DESC) {
					break
				}
			} else {
				rev = uint8(int32(revIdx) ^ int32(sfOB)&int32(KEYINFO_ORDER_DESC))
				if rev != 0 {
					if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_COROUTINE) != uint32(0) {
						/* Cannot run a co-routine in reverse order */
						break
					}
					**(**TBitmask)(__ccgo_up(pRevMask)) |= libc.Uint64FromInt32(1) << iLoop
				}
			}
		}
		**(**TBitmask)(__ccgo_up(pOBSat)) |= libc.Uint64FromInt32(1) << iOB
		goto _2
	_2:
		;
		jSub = jSub + 1
		iOB = iOB + 1
	}
	return libc.BoolInt32(jSub > 0)
}

// C documentation
//
//	/*
//	** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
//	** parameters) to see if it outputs rows in the requested ORDER BY
//	** (or GROUP BY) without requiring a separate sort operation.  Return N:
//	**
//	**   N>0:   N terms of the ORDER BY clause are satisfied
//	**   N==0:  No terms of the ORDER BY clause are satisfied
//	**   N<0:   Unknown yet how many terms of ORDER BY might be satisfied.
//	**
//	** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
//	** strict.  With GROUP BY and DISTINCT the only requirement is that
//	** equivalent rows appear immediately adjacent to one another.  GROUP BY
//	** and DISTINCT do not require rows to appear in any particular order as long
//	** as equivalent rows are grouped together.  Thus for GROUP BY and DISTINCT
//	** the pOrderBy terms can be matched in any order.  With ORDER BY, the
//	** pOrderBy terms must be matched in strict left-to-right order.
//	*/
func _wherePathSatisfiesOrderBy(tls *libc.TLS, pWInfo uintptr, pOrderBy uintptr, pPath uintptr, wctrlFlags Tu16, nLoop Tu16, pLast uintptr, pRevMask uintptr) (r Ti8) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var bOnce, distinctColumns, isMatch, isOrderDistinct, rev, revIdx, revSet, v5 Tu8
	var db, p, pColl, pColl1, pColl2, pIndex, pIxExpr, pLoop, pOBExpr, pParse, pTerm, pX, v4 uintptr
	var eOp, eqOpMask, nColumn, nKeyCol, nOrderBy Tu16
	var i, iColumn, iCur, iLoop, j int32
	var m, mTerm, obDone, orderDistinctMask, ready TBitmask
	var v11 uint64
	var _ /* obSat at bp+0 */ TBitmask
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bOnce, db, distinctColumns, eOp, eqOpMask, i, iColumn, iCur, iLoop, isMatch, isOrderDistinct, j, m, mTerm, nColumn, nKeyCol, nOrderBy, obDone, orderDistinctMask, p, pColl, pColl1, pColl2, pIndex, pIxExpr, pLoop, pOBExpr, pParse, pTerm, pX, ready, rev, revIdx, revSet, v11, v4, v5 /* A column number within table iCur */
	pLoop = uintptr(0)                                                                                                                                                                                                                                                                                                                                                                                      /* The index associated with pLoop */
	db = (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb                                                                                                                                                                                                                                                                                                                       /* Database connection */
	**(**TBitmask)(__ccgo_up(bp)) = uint64(0)                                                                                                                                                                                                                                                                                                                                                               /* Mask of inner loops */
	/*
	 ** We say the WhereLoop is "one-row" if it generates no more than one
	 ** row of output.  A WhereLoop is one-row if all of the following are true:
	 **  (a) All index columns match with WHERE_COLUMN_EQ.
	 **  (b) The index is unique
	 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
	 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
	 **
	 ** We say the WhereLoop is "order-distinct" if the set of columns from
	 ** that WhereLoop that are in the ORDER BY clause are different for every
	 ** row of the WhereLoop.  Every one-row WhereLoop is automatically
	 ** order-distinct.   A WhereLoop that has no columns in the ORDER BY clause
	 ** is not order-distinct. To be order-distinct is not quite the same as being
	 ** UNIQUE since a UNIQUE column or index can have multiple rows that
	 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
	 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
	 **
	 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
	 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
	 ** automatically order-distinct.
	 */
	if nLoop != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_OrderByIdxJoin)) != uint32(0) {
		return 0
	}
	nOrderBy = uint16((*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr)
	if int32(nOrderBy) > int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8))-libc.Int32FromInt32(1) {
		return 0
	} /* Cannot optimize overly large ORDER BYs */
	isOrderDistinct = uint8(1)
	obDone = libc.Uint64FromInt32(1)<<nOrderBy - uint64(1)
	orderDistinctMask = uint64(0)
	ready = uint64(0)
	eqOpMask = uint16(libc.Int32FromInt32(WO_EQ) | libc.Int32FromInt32(WO_IS) | libc.Int32FromInt32(WO_ISNULL))
	if int32(wctrlFlags)&(libc.Int32FromInt32(WHERE_ORDERBY_LIMIT)|libc.Int32FromInt32(WHERE_ORDERBY_MAX)|libc.Int32FromInt32(WHERE_ORDERBY_MIN)) != 0 {
		eqOpMask = uint16(int32(eqOpMask) | libc.Int32FromInt32(WO_IN))
	}
	iLoop = 0
	for {
		if !(isOrderDistinct != 0 && **(**TBitmask)(__ccgo_up(bp)) < obDone && iLoop <= int32(nLoop)) {
			break
		}
		if iLoop > 0 {
			ready = ready | (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf
		}
		if iLoop < int32(nLoop) {
			pLoop = **(**uintptr)(__ccgo_up((*TWherePath)(unsafe.Pointer(pPath)).FaLoop + uintptr(iLoop)*4))
			if int32(wctrlFlags)&int32(WHERE_ORDERBY_LIMIT) != 0 {
				goto _1
			}
		} else {
			pLoop = pLast
		}
		if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != 0 {
			if (*(*struct {
				FidxNum    int32
				F__ccgo4   uint8
				FisOrdered Ti8
				FomitMask  Tu16
				FidxStr    uintptr
				FmHandleIn Tu32
			})(unsafe.Pointer(pLoop + 24))).FisOrdered != 0 && (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy == pOrderBy {
				**(**TBitmask)(__ccgo_up(bp)) = obDone
			} else {
				/* No further ORDER BY terms may be matched. So this call should
				 ** return >=0, not -1. Clear isOrderDistinct to ensure it does so. */
				isOrderDistinct = uint8(0)
			}
			break
		}
		iCur = (*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab)*48))).FiCursor
		/* Mark off any ORDER BY term X that is a column in the table of
		 ** the current loop for which there is term in the WHERE
		 ** clause of the form X IS NULL or X=? that reference only outer
		 ** loops.
		 */
		i = 0
		for {
			if !(i < int32(nOrderBy)) {
				break
			}
			if libc.Uint64FromInt32(1)<<i&**(**TBitmask)(__ccgo_up(bp)) != 0 {
				goto _2
			}
			pOBExpr = _sqlite3ExprSkipCollateAndLikely(tls, (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr)
			if pOBExpr == uintptr(0) {
				goto _2
			}
			if int32((*TExpr)(unsafe.Pointer(pOBExpr)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pOBExpr)).Fop) != int32(TK_AGG_COLUMN) {
				goto _2
			}
			if (*TExpr)(unsafe.Pointer(pOBExpr)).FiTable != iCur {
				goto _2
			}
			pTerm = _sqlite3WhereFindTerm(tls, pWInfo+80, iCur, int32((*TExpr)(unsafe.Pointer(pOBExpr)).FiColumn), ^ready, uint32(eqOpMask), uintptr(0))
			if pTerm == uintptr(0) {
				goto _2
			}
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) == int32(WO_IN) {
				/* IN terms are only valid for sorting in the ORDER BY LIMIT
				 ** optimization, and then only if they are actually used
				 ** by the query plan */
				j = 0
				for {
					if !(j < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm) && pTerm != **(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4))) {
						break
					}
					goto _3
				_3:
					;
					j = j + 1
				}
				if j >= int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm) {
					goto _2
				}
			}
			if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0 && int32((*TExpr)(unsafe.Pointer(pOBExpr)).FiColumn) >= 0 {
				pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse
				pColl1 = _sqlite3ExprNNCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr)
				pColl2 = _sqlite3ExprCompareCollSeq(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)
				if pColl2 == uintptr(0) || _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl1)).FzName, (*TCollSeq)(unsafe.Pointer(pColl2)).FzName) != 0 {
					goto _2
				}
			}
			**(**TBitmask)(__ccgo_up(bp)) = **(**TBitmask)(__ccgo_up(bp)) | libc.Uint64FromInt32(1)<<i
			goto _2
		_2:
			;
			i = i + 1
		}
		if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_ONEROW) == uint32(0) {
			if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_IPK) != 0 {
				if (*(*struct {
					FnEq          Tu16
					FnBtm         Tu16
					FnTop         Tu16
					FnDistinctCol Tu16
					FpIndex       uintptr
					FpOrderBy     uintptr
				})(unsafe.Pointer(pLoop + 24))).FpOrderBy != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_OrderBySubq)) == uint32(0) && _wherePathMatchSubqueryOB(tls, pWInfo, pLoop, iLoop, iCur, pOrderBy, pRevMask, bp) != 0 {
					nColumn = uint16(0)
					isOrderDistinct = uint8(0)
				} else {
					nColumn = uint16(1)
				}
				pIndex = uintptr(0)
				nKeyCol = uint16(0)
			} else {
				v4 = (*(*struct {
					FnEq          Tu16
					FnBtm         Tu16
					FnTop         Tu16
					FnDistinctCol Tu16
					FpIndex       uintptr
					FpOrderBy     uintptr
				})(unsafe.Pointer(pLoop + 24))).FpIndex
				pIndex = v4
				if v4 == uintptr(0) || int32(uint32(*(*uint16)(unsafe.Pointer(pIndex + 56))&0x4>>2)) != 0 {
					return 0
				} else {
					nKeyCol = (*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol
					nColumn = (*TIndex)(unsafe.Pointer(pIndex)).FnColumn
					/* All relevant terms of the index must also be non-NULL in order
					 ** for isOrderDistinct to be true.  So the isOrderDistinct value
					 ** computed here might be a false positive.  Corrections will be
					 ** made at tag-20210426-1 below */
					isOrderDistinct = libc.BoolUint8(int32((*TIndex)(unsafe.Pointer(pIndex)).FonError) != OE_None && (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_SKIPSCAN) == uint32(0))
				}
			}
			/* Loop through all columns of the index and deal with the ones
			 ** that are not constrained by == or IN.
			 */
			v5 = libc.Uint8FromInt32(0)
			revSet = v5
			rev = v5
			distinctColumns = uint8(0)
			j = 0
			for {
				if !(j < int32(nColumn)) {
					break
				}
				bOnce = uint8(1) /* True to run the ORDER BY search loop */
				if j < int32((*(*struct {
					FnEq          Tu16
					FnBtm         Tu16
					FnTop         Tu16
					FnDistinctCol Tu16
					FpIndex       uintptr
					FpOrderBy     uintptr
				})(unsafe.Pointer(pLoop + 24))).FnEq) && j >= int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) {
					eOp = (*TWhereTerm)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)))).FeOperator
					/* Skip over == and IS and ISNULL terms.  (Also skip IN terms when
					 ** doing WHERE_ORDERBY_LIMIT processing).  Except, IS and ISNULL
					 ** terms imply that the index is not UNIQUE NOT NULL in which case
					 ** the loop need to be marked as not order-distinct because it can
					 ** have repeated NULL rows.
					 **
					 ** If the current term is a column of an ((?,?) IN (SELECT...))
					 ** expression for which the SELECT returns more than one column,
					 ** check that it is the only column used by this loop. Otherwise,
					 ** if it is one of two or more, none of the columns can be
					 ** considered to match an ORDER BY term.
					 */
					if int32(eOp)&int32(eqOpMask) != 0 {
						if int32(eOp)&(libc.Int32FromInt32(WO_ISNULL)|libc.Int32FromInt32(WO_IS)) != 0 {
							isOrderDistinct = uint8(0)
						}
						goto _6
					} else {
						if int32(eOp)&int32(WO_IN) != 0 {
							/* ALWAYS() justification: eOp is an equality operator due to the
							 ** j<pLoop->u.btree.nEq constraint above.  Any equality other
							 ** than WO_IN is captured by the previous "if".  So this one
							 ** always has to be WO_IN. */
							pX = (*TWhereTerm)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)))).FpExpr
							i = j + int32(1)
							for {
								if !(i < int32((*(*struct {
									FnEq          Tu16
									FnBtm         Tu16
									FnTop         Tu16
									FnDistinctCol Tu16
									FpIndex       uintptr
									FpOrderBy     uintptr
								})(unsafe.Pointer(pLoop + 24))).FnEq)) {
									break
								}
								if (*TWhereTerm)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*4)))).FpExpr == pX {
									bOnce = uint8(0)
									break
								}
								goto _7
							_7:
								;
								i = i + 1
							}
						}
					}
				}
				/* Get the column number in the table (iColumn) and sort order
				 ** (revIdx) for the j-th column of the index.
				 */
				if pIndex != 0 {
					iColumn = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaiColumn + uintptr(j)*2)))
					revIdx = uint8(int32(**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FaSortOrder + uintptr(j)))) & int32(KEYINFO_ORDER_DESC))
					if iColumn == int32((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FpTable)).FiPKey) {
						iColumn = -int32(1)
					}
				} else {
					iColumn = -int32(1)
					revIdx = uint8(0)
				}
				/* An unconstrained column that might be NULL means that this
				 ** WhereLoop is not well-ordered.  tag-20210426-1
				 */
				if isOrderDistinct != 0 {
					if iColumn >= 0 && j >= int32((*(*struct {
						FnEq          Tu16
						FnBtm         Tu16
						FnTop         Tu16
						FnDistinctCol Tu16
						FpIndex       uintptr
						FpOrderBy     uintptr
					})(unsafe.Pointer(pLoop + 24))).FnEq) && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FpTable)).FaCol + uintptr(iColumn)*12 + 4))&0xf>>0)) == 0 {
						isOrderDistinct = uint8(0)
					}
					if iColumn == -int32(2) {
						isOrderDistinct = uint8(0)
					}
				}
				/* Find the ORDER BY term that corresponds to the j-th column
				 ** of the index and mark that ORDER BY term having been satisfied.
				 */
				isMatch = uint8(0)
				i = 0
				for {
					if !(bOnce != 0 && i < int32(nOrderBy)) {
						break
					}
					if libc.Uint64FromInt32(1)<<i&**(**TBitmask)(__ccgo_up(bp)) != 0 {
						goto _8
					}
					pOBExpr = _sqlite3ExprSkipCollateAndLikely(tls, (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr)
					if pOBExpr == uintptr(0) {
						goto _8
					}
					if int32(wctrlFlags)&(libc.Int32FromInt32(WHERE_GROUPBY)|libc.Int32FromInt32(WHERE_DISTINCTBY)) == 0 {
						bOnce = uint8(0)
					}
					if iColumn >= -int32(1) {
						if int32((*TExpr)(unsafe.Pointer(pOBExpr)).Fop) != int32(TK_COLUMN) && int32((*TExpr)(unsafe.Pointer(pOBExpr)).Fop) != int32(TK_AGG_COLUMN) {
							goto _8
						}
						if (*TExpr)(unsafe.Pointer(pOBExpr)).FiTable != iCur {
							goto _8
						}
						if int32((*TExpr)(unsafe.Pointer(pOBExpr)).FiColumn) != iColumn {
							goto _8
						}
					} else {
						pIxExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FaColExpr + 8 + uintptr(j)*20))).FpExpr
						if _sqlite3ExprCompareSkip(tls, pOBExpr, pIxExpr, iCur) != 0 {
							goto _8
						}
					}
					if iColumn != -int32(1) {
						pColl = _sqlite3ExprNNCollSeq(tls, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse, (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr)
						if _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FzName, **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(j)*4))) != 0 {
							goto _8
						}
					}
					if int32(wctrlFlags)&int32(WHERE_DISTINCTBY) != 0 {
						(*(*struct {
							FnEq          Tu16
							FnBtm         Tu16
							FnTop         Tu16
							FnDistinctCol Tu16
							FpIndex       uintptr
							FpOrderBy     uintptr
						})(unsafe.Pointer(pLoop + 24))).FnDistinctCol = uint16(j + int32(1))
					}
					isMatch = uint8(1)
					break
					goto _8
				_8:
					;
					i = i + 1
				}
				if isMatch != 0 && int32(wctrlFlags)&int32(WHERE_GROUPBY) == 0 {
					/* Make sure the sort order is compatible in an ORDER BY clause.
					 ** Sort order is irrelevant for a GROUP BY clause. */
					if revSet != 0 {
						if int32(rev)^int32(revIdx) != int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_DESC) {
							isMatch = uint8(0)
						}
					} else {
						rev = uint8(int32(revIdx) ^ int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_DESC))
						if rev != 0 {
							**(**TBitmask)(__ccgo_up(pRevMask)) |= libc.Uint64FromInt32(1) << iLoop
						}
						revSet = uint8(1)
					}
				}
				if isMatch != 0 && int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 {
					if j == int32((*(*struct {
						FnEq          Tu16
						FnBtm         Tu16
						FnTop         Tu16
						FnDistinctCol Tu16
						FpIndex       uintptr
						FpOrderBy     uintptr
					})(unsafe.Pointer(pLoop + 24))).FnEq) {
						**(**Tu32)(__ccgo_up(pLoop + 40)) |= uint32(WHERE_BIGNULL_SORT)
					} else {
						isMatch = uint8(0)
					}
				}
				if isMatch != 0 {
					if iColumn == -int32(1) {
						distinctColumns = uint8(1)
					}
					**(**TBitmask)(__ccgo_up(bp)) = **(**TBitmask)(__ccgo_up(bp)) | libc.Uint64FromInt32(1)<<i
				} else {
					/* No match found */
					if j == 0 || j < int32(nKeyCol) {
						isOrderDistinct = uint8(0)
					}
					break
				}
				goto _6
			_6:
				;
				j = j + 1
			} /* end Loop over all index columns */
			if distinctColumns != 0 {
				isOrderDistinct = uint8(1)
			}
		} /* end-if not one-row */
		/* Mark off any other ORDER BY terms that reference pLoop */
		if isOrderDistinct != 0 {
			orderDistinctMask = orderDistinctMask | (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf
			i = 0
			for {
				if !(i < int32(nOrderBy)) {
					break
				}
				if libc.Uint64FromInt32(1)<<i&**(**TBitmask)(__ccgo_up(bp)) != 0 {
					goto _9
				}
				p = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*20))).FpExpr
				mTerm = _sqlite3WhereExprUsage(tls, pWInfo+496, p)
				if mTerm == uint64(0) && !(_sqlite3ExprIsConstant(tls, uintptr(0), p) != 0) {
					goto _9
				}
				if mTerm & ^orderDistinctMask == uint64(0) {
					**(**TBitmask)(__ccgo_up(bp)) = **(**TBitmask)(__ccgo_up(bp)) | libc.Uint64FromInt32(1)<<i
				}
				goto _9
			_9:
				;
				i = i + 1
			}
		}
		goto _1
	_1:
		;
		iLoop = iLoop + 1
	} /* End the loop over all WhereLoops from outer-most down to inner-most */
	if **(**TBitmask)(__ccgo_up(bp)) == obDone {
		return int8(nOrderBy)
	}
	if !(isOrderDistinct != 0) {
		i = int32(nOrderBy) - int32(1)
		for {
			if !(i > 0) {
				break
			}
			if i < int32(libc.Uint32FromInt64(8)*libc.Uint32FromInt32(8)) {
				v11 = libc.Uint64FromInt32(1)<<i - uint64(1)
			} else {
				v11 = uint64(0)
			}
			m = v11
			if **(**TBitmask)(__ccgo_up(bp))&m == m {
				return int8(i)
			}
			goto _10
		_10:
			;
			i = i - 1
		}
		return 0
	}
	return int8(-int32(1))
}

// C documentation
//
//	/*
//	** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
//	** attempts to find the lowest cost path that visits each WhereLoop
//	** once.  This path is then loaded into the pWInfo->a[].pWLoop fields.
//	**
//	** Assume that the total number of output rows that will need to be sorted
//	** will be nRowEst (in the 10*log2 representation).  Or, ignore sorting
//	** costs if nRowEst==0.
//	**
//	** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
//	** error occurs.
//	*/
func _wherePathSolver(tls *libc.TLS, pWInfo uintptr, nRowEst TLogEst) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aFrom, aSortCost, aTo, pFrom, pLevel, pParse, pSpace, pTo, pWLoop, pX, v11 uintptr
	var iLoop, ii, jj, mxChoice, mxI, nFrom, nLoop, nOrder, nOrderBy, nSpace, nTo, rc, rc1, v2 int32
	var isOrdered Ti8
	var maskNew TBitmask
	var mxCost, mxUnsort, nOut, rCost, rUnsort TLogEst
	var wsFlags Tu32
	var _ /* m at bp+16 */ TBitmask
	var _ /* notUsed at bp+8 */ TBitmask
	var _ /* revMask at bp+0 */ TBitmask
	var _ /* revMask at bp+24 */ TBitmask
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aFrom, aSortCost, aTo, iLoop, ii, isOrdered, jj, maskNew, mxChoice, mxCost, mxI, mxUnsort, nFrom, nLoop, nOrder, nOrderBy, nOut, nSpace, nTo, pFrom, pLevel, pParse, pSpace, pTo, pWLoop, pX, rCost, rUnsort, rc, rc1, wsFlags, v11, v2 /* Loop counters */
	mxI = 0                                                                                                                                                                                                                                                                                                                                     /* Number of ORDER BY clause terms */
	mxCost = 0                                                                                                                                                                                                                                                                                                                                  /* Maximum cost of a set of paths */
	mxUnsort = 0                                                                                                                                                                                                                                                                                                                                /* Used to divy up the pSpace memory */
	aSortCost = uintptr(0)                                                                                                                                                                                                                                                                                                                      /* Bytes of space allocated at pSpace */
	pParse = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse
	nLoop = int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)
	/* TUNING: mxChoice is the maximum number of possible paths to preserve
	 ** at each step.  Based on the number of loops in the FROM clause:
	 **
	 **     nLoop      mxChoice
	 **     -----      --------
	 **       1            1            // the most common case
	 **       2            5
	 **       3+        12 or 18        // see computeMxChoice()
	 */
	if nLoop <= int32(1) {
		mxChoice = int32(1)
	} else {
		if nLoop == int32(2) {
			mxChoice = int32(5)
		} else {
			if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
				mxChoice = int32(1)
			} else {
				mxChoice = _computeMxChoice(tls, pWInfo)
			}
		}
	}
	/* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
	 ** case the purpose of this call is to estimate the number of rows returned
	 ** by the overall query. Once this estimate has been obtained, the caller
	 ** will invoke this function a second time, passing the estimate as the
	 ** nRowEst parameter.  */
	if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy == uintptr(0) || int32(nRowEst) == 0 {
		nOrderBy = 0
	} else {
		nOrderBy = (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr
	}
	/* Allocate and initialize space for aTo, aFrom and aSortCost[] */
	nSpace = int32((uint32(32) + uint32(4)*uint32(nLoop)) * uint32(mxChoice) * uint32(2))
	nSpace = int32(uint32(nSpace) + libc.Uint32FromInt64(2)*uint32(nOrderBy))
	pSpace = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(nSpace))
	if pSpace == uintptr(0) {
		return int32(SQLITE_NOMEM)
	}
	aTo = pSpace
	aFrom = aTo + uintptr(mxChoice)*32
	libc.Xmemset(tls, aFrom, 0, uint32(32))
	pX = aFrom + uintptr(mxChoice)*32
	ii = mxChoice * int32(2)
	pFrom = aTo
	for {
		if !(ii > 0) {
			break
		}
		(*TWherePath)(unsafe.Pointer(pFrom)).FaLoop = pX
		goto _1
	_1:
		;
		ii = ii - 1
		pFrom += 32
		pX = pX + uintptr(nLoop)*4
	}
	if nOrderBy != 0 {
		/* If there is an ORDER BY clause and it is not being ignored, set up
		 ** space for the aSortCost[] array. Each element of the aSortCost array
		 ** is either zero - meaning it has not yet been initialized - or the
		 ** cost of sorting nRowEst rows of data where the first X terms of
		 ** the ORDER BY clause are already in order, where X is the array
		 ** index.  */
		aSortCost = pX
		libc.Xmemset(tls, aSortCost, 0, uint32(2)*uint32(nOrderBy))
	}
	/* Seed the search with a single WherePath containing zero WhereLoops.
	 **
	 ** TUNING: Do not let the number of iterations go above 28.  If the cost
	 ** of computing an automatic index is not paid back within the first 28
	 ** rows, then do not use the automatic index. */
	if int32((*TParse)(unsafe.Pointer(pParse)).FnQueryLoop) < int32(48) {
		v2 = int32((*TParse)(unsafe.Pointer(pParse)).FnQueryLoop)
	} else {
		v2 = int32(48)
	}
	(**(**TWherePath)(__ccgo_up(aFrom))).FnRow = int16(v2)
	nFrom = int32(1)
	if nOrderBy != 0 {
		/* If nLoop is zero, then there are no FROM terms in the query. Since
		 ** in this case the query may return a maximum of one row, the results
		 ** are already in the requested order. Set isOrdered to nOrderBy to
		 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
		 ** -1, indicating that the result set may or may not be ordered,
		 ** depending on the loops added to the current plan.  */
		if nLoop > 0 {
			v2 = -int32(1)
		} else {
			v2 = nOrderBy
		}
		(**(**TWherePath)(__ccgo_up(aFrom))).FisOrdered = int8(v2)
	}
	/* Compute successively longer WherePaths using the previous generation
	 ** of WherePaths as the basis for the next.  Keep track of the mxChoice
	 ** best paths at each generation */
	iLoop = 0
	for {
		if !(iLoop < nLoop) {
			break
		}
		nTo = 0
		ii = 0
		pFrom = aFrom
		for {
			if !(ii < nFrom) {
				break
			}
			pWLoop = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops
			for {
				if !(pWLoop != 0) {
					break
				} /* Mask of rev-order loops for (..) */
				if (*TWhereLoop)(unsafe.Pointer(pWLoop)).Fprereq & ^(*TWherePath)(unsafe.Pointer(pFrom)).FmaskLoop != uint64(0) {
					goto _6
				}
				if (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf&(*TWherePath)(unsafe.Pointer(pFrom)).FmaskLoop != uint64(0) {
					goto _6
				}
				if (*TWhereLoop)(unsafe.Pointer(pWLoop)).FwsFlags&uint32(WHERE_AUTO_INDEX) != uint32(0) && int32((*TWherePath)(unsafe.Pointer(pFrom)).FnRow) < int32(3) {
					/* Do not use an automatic index if the this loop is expected
					 ** to run less than 1.25 times.  It is tempting to also exclude
					 ** automatic index usage on an outer loop, but sometimes an automatic
					 ** index is useful in the outer loop of a correlated subquery. */
					goto _6
				}
				/* At this point, pWLoop is a candidate to be the next loop.
				 ** Compute its cost */
				rUnsort = int16(int32((*TWhereLoop)(unsafe.Pointer(pWLoop)).FrRun) + int32((*TWherePath)(unsafe.Pointer(pFrom)).FnRow))
				if (*TWhereLoop)(unsafe.Pointer(pWLoop)).FrSetup != 0 {
					rUnsort = _sqlite3LogEstAdd(tls, (*TWhereLoop)(unsafe.Pointer(pWLoop)).FrSetup, rUnsort)
				}
				rUnsort = _sqlite3LogEstAdd(tls, rUnsort, (*TWherePath)(unsafe.Pointer(pFrom)).FrUnsort)
				nOut = int16(int32((*TWherePath)(unsafe.Pointer(pFrom)).FnRow) + int32((*TWhereLoop)(unsafe.Pointer(pWLoop)).FnOut))
				maskNew = (*TWherePath)(unsafe.Pointer(pFrom)).FmaskLoop | (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf
				isOrdered = (*TWherePath)(unsafe.Pointer(pFrom)).FisOrdered
				if int32(isOrdered) < 0 {
					**(**TBitmask)(__ccgo_up(bp)) = uint64(0)
					isOrdered = _wherePathSatisfiesOrderBy(tls, pWInfo, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy, pFrom, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags, uint16(iLoop), pWLoop, bp)
				} else {
					**(**TBitmask)(__ccgo_up(bp)) = (*TWherePath)(unsafe.Pointer(pFrom)).FrevLoop
				}
				if int32(isOrdered) >= 0 && int32(isOrdered) < nOrderBy {
					if int32(**(**TLogEst)(__ccgo_up(aSortCost + uintptr(isOrdered)*2))) == 0 {
						**(**TLogEst)(__ccgo_up(aSortCost + uintptr(isOrdered)*2)) = _whereSortingCost(tls, pWInfo, nRowEst, nOrderBy, int32(isOrdered))
					}
					/* TUNING:  Add a small extra penalty (3) to sorting as an
					 ** extra encouragement to the query planner to select a plan
					 ** where the rows emerge in the correct order without any sorting
					 ** required. */
					rCost = int16(int32(_sqlite3LogEstAdd(tls, rUnsort, **(**TLogEst)(__ccgo_up(aSortCost + uintptr(isOrdered)*2)))) + int32(3))
				} else {
					rCost = rUnsort
					rUnsort = int16(int32(rUnsort) - libc.Int32FromInt32(2)) /* TUNING:  Slight bias in favor of no-sort plans */
				}
				/* Check to see if pWLoop should be added to the set of
				 ** mxChoice best-so-far paths.
				 **
				 ** First look for an existing path among best-so-far paths
				 ** that:
				 **     (1) covers the same set of loops, and
				 **     (2) has a compatible isOrdered value.
				 **
				 ** "Compatible isOrdered value" means either
				 **     (A) both have isOrdered==-1, or
				 **     (B) both have isOrder>=0, or
				 **     (C) ordering does not matter because this is the last round
				 **         of the solver.
				 **
				 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
				 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
				 ** of legal values for isOrdered, -1..64.
				 */
				jj = 0
				pTo = aTo
				for {
					if !(jj < nTo) {
						break
					}
					if (*TWherePath)(unsafe.Pointer(pTo)).FmaskLoop == maskNew && ((int32((*TWherePath)(unsafe.Pointer(pTo)).FisOrdered)^int32(isOrdered))&int32(0x80) == 0 || iLoop == nLoop-int32(1)) {
						break
					}
					goto _7
				_7:
					;
					jj = jj + 1
					pTo += 32
				}
				if jj >= nTo {
					/* None of the existing best-so-far paths match the candidate. */
					if nTo >= mxChoice && (int32(rCost) > int32(mxCost) || int32(rCost) == int32(mxCost) && int32(rUnsort) >= int32(mxUnsort)) {
						/* The current candidate is no better than any of the mxChoice
						 ** paths currently in the best-so-far buffer.  So discard
						 ** this candidate as not viable. */
						goto _6
					}
					/* If we reach this points it means that the new candidate path
					 ** needs to be added to the set of best-so-far paths. */
					if nTo < mxChoice {
						/* Increase the size of the aTo set by one */
						v2 = nTo
						nTo = nTo + 1
						jj = v2
					} else {
						/* New path replaces the prior worst to keep count below mxChoice */
						jj = mxI
					}
					pTo = aTo + uintptr(jj)*32
				} else {
					/* Control reaches here if best-so-far path pTo=aTo[jj] covers the
					 ** same set of loops and has the same isOrdered setting as the
					 ** candidate path.  Check to see if the candidate should replace
					 ** pTo or if the candidate should be skipped.
					 **
					 ** The conditional is an expanded vector comparison equivalent to:
					 **   (pTo->rCost,pTo->nRow,pTo->rUnsort) <= (rCost,nOut,rUnsort)
					 */
					if int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) < int32(rCost) || int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) == int32(rCost) && int32((*TWherePath)(unsafe.Pointer(pTo)).FnRow) < int32(nOut) || int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) == int32(rCost) && int32((*TWherePath)(unsafe.Pointer(pTo)).FnRow) == int32(nOut) && int32((*TWherePath)(unsafe.Pointer(pTo)).FrUnsort) < int32(rUnsort) || int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) == int32(rCost) && int32((*TWherePath)(unsafe.Pointer(pTo)).FnRow) == int32(nOut) && int32((*TWherePath)(unsafe.Pointer(pTo)).FrUnsort) == int32(rUnsort) && _whereLoopIsNoBetter(tls, pWLoop, **(**uintptr)(__ccgo_up((*TWherePath)(unsafe.Pointer(pTo)).FaLoop + uintptr(iLoop)*4))) != 0 {
						/* Discard the candidate path from further consideration */
						goto _6
					}
					/* Control reaches here if the candidate path is better than the
					 ** pTo path.  Replace pTo with the candidate. */
				}
				/* pWLoop is a winner.  Add it to the set of best so far */
				(*TWherePath)(unsafe.Pointer(pTo)).FmaskLoop = (*TWherePath)(unsafe.Pointer(pFrom)).FmaskLoop | (*TWhereLoop)(unsafe.Pointer(pWLoop)).FmaskSelf
				(*TWherePath)(unsafe.Pointer(pTo)).FrevLoop = **(**TBitmask)(__ccgo_up(bp))
				(*TWherePath)(unsafe.Pointer(pTo)).FnRow = nOut
				(*TWherePath)(unsafe.Pointer(pTo)).FrCost = rCost
				(*TWherePath)(unsafe.Pointer(pTo)).FrUnsort = rUnsort
				(*TWherePath)(unsafe.Pointer(pTo)).FisOrdered = isOrdered
				libc.Xmemcpy(tls, (*TWherePath)(unsafe.Pointer(pTo)).FaLoop, (*TWherePath)(unsafe.Pointer(pFrom)).FaLoop, uint32(4)*uint32(iLoop))
				**(**uintptr)(__ccgo_up((*TWherePath)(unsafe.Pointer(pTo)).FaLoop + uintptr(iLoop)*4)) = pWLoop
				if nTo >= mxChoice {
					mxI = 0
					mxCost = (**(**TWherePath)(__ccgo_up(aTo))).FrCost
					mxUnsort = (**(**TWherePath)(__ccgo_up(aTo))).FnRow
					jj = int32(1)
					pTo = aTo + 1*32
					for {
						if !(jj < mxChoice) {
							break
						}
						if int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) > int32(mxCost) || int32((*TWherePath)(unsafe.Pointer(pTo)).FrCost) == int32(mxCost) && int32((*TWherePath)(unsafe.Pointer(pTo)).FrUnsort) > int32(mxUnsort) {
							mxCost = (*TWherePath)(unsafe.Pointer(pTo)).FrCost
							mxUnsort = (*TWherePath)(unsafe.Pointer(pTo)).FrUnsort
							mxI = jj
						}
						goto _9
					_9:
						;
						jj = jj + 1
						pTo += 32
					}
				}
				goto _6
			_6:
				;
				pWLoop = (*TWhereLoop)(unsafe.Pointer(pWLoop)).FpNextLoop
			}
			goto _5
		_5:
			;
			ii = ii + 1
			pFrom += 32
		}
		/* Swap the roles of aFrom and aTo for the next generation */
		pFrom = aTo
		aTo = aFrom
		aFrom = pFrom
		nFrom = nTo
		goto _4
	_4:
		;
		iLoop = iLoop + 1
	}
	if nFrom == 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+25316, 0)
		_sqlite3DbFreeNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSpace)
		return int32(SQLITE_ERROR)
	}
	/* Only one path is available, which is the best path */
	pFrom = aFrom
	/* Load the lowest cost path into pWInfo */
	iLoop = 0
	for {
		if !(iLoop < nLoop) {
			break
		}
		pLevel = pWInfo + 760 + uintptr(iLoop)*88
		v11 = **(**uintptr)(__ccgo_up((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(iLoop)*4))
		pWLoop = v11
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop = v11
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom = (*TWhereLoop)(unsafe.Pointer(pWLoop)).FiTab
		(*TWhereLevel)(unsafe.Pointer(pLevel)).FiTabCur = (*(*TSrcItem)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*48))).FiCursor
		goto _10
	_10:
		;
		iLoop = iLoop + 1
	}
	if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DISTINCTBY) == 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct) == WHERE_DISTINCT_NOOP && nRowEst != 0 {
		rc = int32(_wherePathSatisfiesOrderBy(tls, pWInfo, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpResultSet, pFrom, uint16(WHERE_DISTINCTBY), uint16(nLoop-int32(1)), **(**uintptr)(__ccgo_up((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(nLoop-int32(1))*4)), bp+8))
		if rc == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpResultSet)).FnExpr {
			(*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_ORDERED)
		}
	}
	libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(0), 2, 0x4)
	if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy != 0 {
		(*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = (*TWherePath)(unsafe.Pointer(pFrom)).FisOrdered
		if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DISTINCTBY) != 0 {
			if int32((*TWherePath)(unsafe.Pointer(pFrom)).FisOrdered) == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr {
				(*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_ORDERED)
			}
			/* vvv--- See check-in [12ad822d9b827777] on 2023-03-16 ---vvv */
		} else {
			(*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask = (*TWherePath)(unsafe.Pointer(pFrom)).FrevLoop
			if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) <= 0 {
				(*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = 0
				if nLoop > 0 {
					wsFlags = (*TWhereLoop)(unsafe.Pointer(**(**uintptr)(__ccgo_up((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(nLoop-int32(1))*4)))).FwsFlags
					if wsFlags&uint32(WHERE_ONEROW) == uint32(0) && wsFlags&uint32(libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_COLUMN_IN)) != uint32(libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_COLUMN_IN)) {
						**(**TBitmask)(__ccgo_up(bp + 16)) = uint64(0)
						rc1 = int32(_wherePathSatisfiesOrderBy(tls, pWInfo, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy, pFrom, uint16(WHERE_ORDERBY_LIMIT), uint16(nLoop-int32(1)), **(**uintptr)(__ccgo_up((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(nLoop-int32(1))*4)), bp+16))
						if rc1 == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr {
							libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 2, 0x4)
							(*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask = **(**TBitmask)(__ccgo_up(bp + 16))
						}
					}
				}
			} else {
				if nLoop != 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) == int32(1) && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&(libc.Int32FromInt32(WHERE_ORDERBY_MIN)|libc.Int32FromInt32(WHERE_ORDERBY_MAX)) != 0 {
					libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 2, 0x4)
				}
			}
		}
		if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_SORTBYGROUP) != 0 && int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat) == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr && nLoop > 0 {
			**(**TBitmask)(__ccgo_up(bp + 24)) = uint64(0)
			nOrder = int32(_wherePathSatisfiesOrderBy(tls, pWInfo, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy, pFrom, uint16(0), uint16(nLoop-int32(1)), **(**uintptr)(__ccgo_up((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(nLoop-int32(1))*4)), bp+24))
			if nOrder == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr {
				libc.SetBitFieldPtr8Uint32(pWInfo+48, libc.Uint32FromInt32(1), 3, 0x8)
				(*TWhereInfo)(unsafe.Pointer(pWInfo)).FrevMask = **(**TBitmask)(__ccgo_up(bp + 24))
			}
		}
	}
	(*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut = (*TWherePath)(unsafe.Pointer(pFrom)).FnRow
	/* Free temporary memory and return success */
	_sqlite3DbFreeNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pSpace)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This function is called to estimate the number of rows visited by a
//	** range-scan on a skip-scan index. For example:
//	**
//	**   CREATE INDEX i1 ON t1(a, b, c);
//	**   SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
//	**
//	** Value pLoop->nOut is currently set to the estimated number of rows
//	** visited for scanning (a=? AND b=?). This function reduces that estimate
//	** by some factor to account for the (c BETWEEN ? AND ?) expression based
//	** on the stat4 data for the index. this scan will be performed multiple
//	** times (once for each (a,b) combination that matches a=?) is dealt with
//	** by the caller.
//	**
//	** It does this by scanning through all stat4 samples, comparing values
//	** extracted from pLower and pUpper with the corresponding column in each
//	** sample. If L and U are the number of samples found to be less than or
//	** equal to the values extracted from pLower and pUpper respectively, and
//	** N is the total number of samples, the pLoop->nOut value is adjusted
//	** as follows:
//	**
//	**   nOut = nOut * ( min(U - L, 1) / N )
//	**
//	** If pLower is NULL, or a value cannot be extracted from the term, L is
//	** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
//	** U is set to N.
//	**
//	** Normally, this function sets *pbDone to 1 before returning. However,
//	** if no value can be extracted from either pLower or pUpper (and so the
//	** estimate of the number of rows delivered remains unchanged), *pbDone
//	** is left as is.
//	**
//	** If an error occurs, an SQLite error code is returned. Otherwise,
//	** SQLITE_OK.
//	*/
func _whereRangeSkipScanEst(tls *libc.TLS, pParse uintptr, pLower uintptr, pUpper uintptr, pLoop uintptr, pbDone uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var aff Tu8
	var db, p, pColl, v3 uintptr
	var i, nAdjust, nDiff, nEq, nLower, nUpper, rc, res, res1, v1 int32
	var _ /* p1 at bp+0 */ uintptr
	var _ /* p2 at bp+4 */ uintptr
	var _ /* pVal at bp+8 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aff, db, i, nAdjust, nDiff, nEq, nLower, nUpper, p, pColl, rc, res, res1, v1, v3
	p = (*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FpIndex
	nEq = int32((*(*struct {
		FnEq          Tu16
		FnBtm         Tu16
		FnTop         Tu16
		FnDistinctCol Tu16
		FpIndex       uintptr
		FpOrderBy     uintptr
	})(unsafe.Pointer(pLoop + 24))).FnEq)
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	nLower = -int32(1)
	nUpper = (*TIndex)(unsafe.Pointer(p)).FnSample + int32(1)
	rc = SQLITE_OK
	aff = uint8(_sqlite3IndexColumnAffinity(tls, db, p, nEq))
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)     /* Value extracted from pLower */
	**(**uintptr)(__ccgo_up(bp + 4)) = uintptr(0) /* Value extracted from pUpper */
	**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0) /* Value extracted from record */
	pColl = _sqlite3LocateCollSeq(tls, pParse, **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(p)).FazColl + uintptr(nEq)*4)))
	if pLower != 0 {
		rc = _sqlite3Stat4ValueFromExpr(tls, pParse, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pLower)).FpExpr)).FpRight, aff, bp)
		nLower = 0
	}
	if pUpper != 0 && rc == SQLITE_OK {
		rc = _sqlite3Stat4ValueFromExpr(tls, pParse, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pUpper)).FpExpr)).FpRight, aff, bp+4)
		if **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
			v1 = 0
		} else {
			v1 = (*TIndex)(unsafe.Pointer(p)).FnSample
		}
		nUpper = v1
	}
	if **(**uintptr)(__ccgo_up(bp)) != 0 || **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
		i = 0
		for {
			if !(rc == SQLITE_OK && i < (*TIndex)(unsafe.Pointer(p)).FnSample) {
				break
			}
			rc = _sqlite3Stat4Column(tls, db, (**(**TIndexSample)(__ccgo_up((*TIndex)(unsafe.Pointer(p)).FaSample + uintptr(i)*20))).Fp, (**(**TIndexSample)(__ccgo_up((*TIndex)(unsafe.Pointer(p)).FaSample + uintptr(i)*20))).Fn, nEq, bp+8)
			if rc == SQLITE_OK && **(**uintptr)(__ccgo_up(bp)) != 0 {
				res = _sqlite3MemCompare(tls, **(**uintptr)(__ccgo_up(bp)), **(**uintptr)(__ccgo_up(bp + 8)), pColl)
				if res >= 0 {
					nLower = nLower + 1
				}
			}
			if rc == SQLITE_OK && **(**uintptr)(__ccgo_up(bp + 4)) != 0 {
				res1 = _sqlite3MemCompare(tls, **(**uintptr)(__ccgo_up(bp + 4)), **(**uintptr)(__ccgo_up(bp + 8)), pColl)
				if res1 >= 0 {
					nUpper = nUpper + 1
				}
			}
			goto _2
		_2:
			;
			i = i + 1
		}
		nDiff = nUpper - nLower
		if nDiff <= 0 {
			nDiff = int32(1)
		}
		/* If there is both an upper and lower bound specified, and the
		 ** comparisons indicate that they are close together, use the fallback
		 ** method (assume that the scan visits 1/64 of the rows) for estimating
		 ** the number of rows visited. Otherwise, estimate the number of rows
		 ** using the method described in the header comment for this function. */
		if nDiff != int32(1) || pUpper == uintptr(0) || pLower == uintptr(0) {
			nAdjust = int32(_sqlite3LogEst(tls, uint64((*TIndex)(unsafe.Pointer(p)).FnSample))) - int32(_sqlite3LogEst(tls, uint64(nDiff)))
			v3 = pLoop + 22
			*(*TLogEst)(unsafe.Pointer(v3)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(v3))) - nAdjust)
			**(**int32)(__ccgo_up(pbDone)) = int32(1)
		}
	} else {
	}
	_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up(bp)))
	_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up(bp + 4)))
	_sqlite3ValueFree(tls, **(**uintptr)(__ccgo_up(bp + 8)))
	return rc
}

// C documentation
//
//	/*
//	** Term pTerm is a vector range comparison operation. The first comparison
//	** in the vector can be optimized using column nEq of the index. This
//	** function returns the total number of vector elements that can be used
//	** as part of the range comparison.
//	**
//	** For example, if the query is:
//	**
//	**   WHERE a = ? AND (b, c, d) > (?, ?, ?)
//	**
//	** and the index:
//	**
//	**   CREATE INDEX ... ON (a, b, c, d, e)
//	**
//	** then this function would be invoked with nEq=1. The value returned in
//	** this case is 3.
//	*/
func _whereRangeVectorLen(tls *libc.TLS, pParse uintptr, iCur int32, pIdx uintptr, nEq int32, pTerm uintptr) (r int32) {
	var aff, idxaff int8
	var i, nCmp, v1 int32
	var pColl, pLhs, pRhs, t uintptr
	_, _, _, _, _, _, _, _, _ = aff, i, idxaff, nCmp, pColl, pLhs, pRhs, t, v1
	nCmp = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft)
	if nCmp < int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn)-nEq {
		v1 = nCmp
	} else {
		v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) - nEq
	}
	nCmp = v1
	i = int32(1)
	for {
		if !(i < nCmp) {
			break
		} /* Comparison affinity */
		idxaff = 0
		pLhs = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft + 20)) + 8 + uintptr(i)*20))).FpExpr
		pRhs = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight
		if (*TExpr)(unsafe.Pointer(pRhs)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
			pRhs = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pRhs + 20)))).FpEList + 8 + uintptr(i)*20))).FpExpr
		} else {
			pRhs = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pRhs + 20)) + 8 + uintptr(i)*20))).FpExpr
		}
		/* Check that the LHS of the comparison is a column reference to
		 ** the right column of the right source table. And that the sort
		 ** order of the index column is the same as the sort order of the
		 ** leftmost index column.  */
		if int32((*TExpr)(unsafe.Pointer(pLhs)).Fop) != int32(TK_COLUMN) || (*TExpr)(unsafe.Pointer(pLhs)).FiTable != iCur || int32((*TExpr)(unsafe.Pointer(pLhs)).FiColumn) != int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i+nEq)*2))) || int32(**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(i+nEq)))) != int32(**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(nEq)))) {
			break
		}
		aff = _sqlite3CompareAffinity(tls, pRhs, _sqlite3ExprAffinity(tls, pLhs))
		idxaff = _sqlite3TableColumnAffinity(tls, (*TIndex)(unsafe.Pointer(pIdx)).FpTable, int32((*TExpr)(unsafe.Pointer(pLhs)).FiColumn))
		if int32(aff) != int32(idxaff) {
			break
		}
		if (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Commuted)) != uint32(0) {
			t = pRhs
			pRhs = pLhs
			pLhs = t
		}
		pColl = _sqlite3BinaryCompareCollSeq(tls, pParse, pLhs, pRhs)
		if pColl == uintptr(0) {
			break
		}
		if _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FzName, **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i+nEq)*4))) != 0 {
			break
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	return i
}

/*
** Adjust the cost C by the costMult factor T.  This only occurs if
** compiled with -DSQLITE_ENABLE_COSTMULT
 */

// C documentation
//
//	/*
//	** Set the reverse-scan order mask to one for all tables in the query
//	** with the exception of MATERIALIZED common table expressions that have
//	** their own internal ORDER BY clauses.
//	**
//	** This implements the PRAGMA reverse_unordered_selects=ON setting.
//	** (Also SQLITE_DBCONFIG_REVERSE_SCANORDER).
//	*/
func _whereReverseScanOrder(tls *libc.TLS, pWInfo uintptr) {
	var ii int32
	var pItem uintptr
	_, _ = ii, pItem
	ii = 0
	for {
		if !(ii < (*TSrcList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList)).FnSrc) {
			break
		}
		pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr(ii)*48
		if !(int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x200>>9) != 0) || int32((*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 36)))).FeM10d) != M10d_Yes || int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x4>>2) == 0 || (*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 44)))).FpSelect)).FpOrderBy == uintptr(0) {
			**(**TBitmask)(__ccgo_up(pWInfo + 72)) |= libc.Uint64FromInt32(1) << ii
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
}

// C documentation
//
//	/*
//	** Advance to the next WhereTerm that matches according to the criteria
//	** established when the pScan object was initialized by whereScanInit().
//	** Return NULL if there are no more matching WhereTerms.
//	*/
func _whereScanNext(tls *libc.TLS, pScan uintptr) (r uintptr) {
	var iColumn Ti16
	var iCur, j, k int32
	var pColl, pParse, pTerm, pWC, pX, zCollName, v2 uintptr
	var v3 bool
	_, _, _, _, _, _, _, _, _, _, _, _ = iColumn, iCur, j, k, pColl, pParse, pTerm, pWC, pX, zCollName, v2, v3 /* The term being tested */
	k = (*TWhereScan)(unsafe.Pointer(pScan)).Fk                                                                /* Where to start scanning */
	pWC = (*TWhereScan)(unsafe.Pointer(pScan)).FpWC
	for int32(1) != 0 {
		iColumn = **(**Ti16)(__ccgo_up(pScan + 72 + uintptr(int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv)-int32(1))*2))
		iCur = **(**int32)(__ccgo_up(pScan + 28 + uintptr(int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv)-int32(1))*4))
		for cond := true; cond; cond = pWC != uintptr(0) {
			pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(k)*48
			for {
				if !(k < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) {
					break
				}
				if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor == iCur && (*(*struct {
					FleftColumn int32
					FiField     int32
				})(unsafe.Pointer(pTerm + 24))).FleftColumn == int32(iColumn) && (int32(iColumn) != -int32(2) || _sqlite3ExprCompareSkip(tls, (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft, (*TWhereScan)(unsafe.Pointer(pScan)).FpIdxExpr, iCur) == 0) && (int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv) <= int32(1) || !((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0))) {
					if v3 = int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_EQUIV) != 0 && int32((*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv) < int32(libc.Uint32FromInt64(44)/libc.Uint32FromInt64(4)); v3 {
						v2 = _whereRightSubexprIsColumn(tls, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)
						pX = v2
					}
					if v3 && v2 != uintptr(0) {
						j = 0
						for {
							if !(j < int32((*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv)) {
								break
							}
							if **(**int32)(__ccgo_up(pScan + 28 + uintptr(j)*4)) == (*TExpr)(unsafe.Pointer(pX)).FiTable && int32(**(**Ti16)(__ccgo_up(pScan + 72 + uintptr(j)*2))) == int32((*TExpr)(unsafe.Pointer(pX)).FiColumn) {
								break
							}
							goto _4
						_4:
							;
							j = j + 1
						}
						if j == int32((*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv) {
							**(**int32)(__ccgo_up(pScan + 28 + uintptr(j)*4)) = (*TExpr)(unsafe.Pointer(pX)).FiTable
							**(**Ti16)(__ccgo_up(pScan + 72 + uintptr(j)*2)) = (*TExpr)(unsafe.Pointer(pX)).FiColumn
							(*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv = (*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv + 1
						}
					}
					if uint32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(*TWhereScan)(unsafe.Pointer(pScan)).FopMask != uint32(0) {
						/* Verify the affinity and collating sequence match */
						if (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName != 0 && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_ISNULL) == 0 {
							pParse = (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse
							pX = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
							if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_IN) != 0 {
								zCollName = _indexInAffinityOk(tls, pParse, pTerm, uint8((*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff))
								if !(zCollName != 0) {
									goto _1
								}
							} else {
								if !(_sqlite3IndexAffinityOk(tls, pX, (*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff) != 0) {
									goto _1
								}
								pColl = _sqlite3ExprCompareCollSeq(tls, pParse, pX)
								if pColl != 0 {
									v2 = (*TCollSeq)(unsafe.Pointer(pColl)).FzName
								} else {
									v2 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY))
								}
								zCollName = v2
							}
							if _sqlite3StrICmp(tls, zCollName, (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName) != 0 {
								goto _1
							}
						}
						if v3 = int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0; v3 {
							pX = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight
						}
						if v3 && pX != libc.UintptrFromInt32(0) && int32((*TExpr)(unsafe.Pointer(pX)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pX)).FiTable == **(**int32)(__ccgo_up(pScan + 28)) && int32((*TExpr)(unsafe.Pointer(pX)).FiColumn) == int32(**(**Ti16)(__ccgo_up(pScan + 72))) {
							goto _1
						}
						(*TWhereScan)(unsafe.Pointer(pScan)).FpWC = pWC
						(*TWhereScan)(unsafe.Pointer(pScan)).Fk = k + int32(1)
						return pTerm
					}
				}
				goto _1
			_1:
				;
				k = k + 1
				pTerm += 48
			}
			pWC = (*TWhereClause)(unsafe.Pointer(pWC)).FpOuter
			k = 0
		}
		if int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv) >= int32((*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv) {
			break
		}
		pWC = (*TWhereScan)(unsafe.Pointer(pScan)).FpOrigWC
		k = 0
		(*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv = (*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv + 1
	}
	return uintptr(0)
}

// C documentation
//
//	/*
//	** Most queries use only a single table (they are not joins) and have
//	** simple == constraints against indexed fields.  This routine attempts
//	** to plan those simple cases using much less ceremony than the
//	** general-purpose query planner, and thereby yield faster sqlite3_prepare()
//	** times for the common case.
//	**
//	** Return non-zero on success, if this query can be handled by this
//	** no-frills query planner.  Return zero if this query needs the
//	** general-purpose query planner.
//	*/
func _whereShortCut(tls *libc.TLS, pBuilder uintptr) (r int32) {
	bp := tls.Alloc(96)
	defer tls.Free(96)
	var iCur, j, opMask, v2 int32
	var pIdx, pItem, pLoop, pTab, pTerm, pWC, pWInfo uintptr
	var _ /* scan at bp+0 */ TWhereScan
	_, _, _, _, _, _, _, _, _, _, _ = iCur, j, opMask, pIdx, pItem, pLoop, pTab, pTerm, pWC, pWInfo, v2
	pWInfo = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWInfo
	if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 {
		return 0
	}
	pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8
	pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpSTab
	if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) {
		return 0
	}
	if int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x2>>1) != 0 || int32(*(*uint32)(unsafe.Pointer(pItem + 12 + 4))&0x1>>0) != 0 {
		return 0
	}
	iCur = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor
	pWC = pWInfo + 80
	pLoop = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew
	(*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags = uint32(0)
	(*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip = uint16(0)
	pTerm = _whereScanInit(tls, bp, pWC, iCur, -int32(1), uint32(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)), uintptr(0))
	for pTerm != 0 && (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight != 0 {
		pTerm = _whereScanNext(tls, bp)
	}
	if pTerm != 0 {
		(*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ) | libc.Int32FromInt32(WHERE_IPK) | libc.Int32FromInt32(WHERE_ONEROW))
		**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm)) = pTerm
		(*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm = uint16(1)
		(*(*struct {
			FnEq          Tu16
			FnBtm         Tu16
			FnTop         Tu16
			FnDistinctCol Tu16
			FpIndex       uintptr
			FpOrderBy     uintptr
		})(unsafe.Pointer(pLoop + 24))).FnEq = uint16(1)
		/* TUNING: Cost of a rowid lookup is 10 */
		(*TWhereLoop)(unsafe.Pointer(pLoop)).FrRun = int16(33) /* 33==sqlite3LogEst(10) */
	} else {
		pIdx = (*TTable)(unsafe.Pointer(pTab)).FpIndex
		for {
			if !(pIdx != 0) {
				break
			}
			if !(int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) != libc.Int32FromInt32(OE_None)) || (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != uintptr(0) || int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) > int32(libc.Uint32FromInt64(12)/libc.Uint32FromInt64(4)) {
				goto _1
			}
			if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x8>>3)) != 0 {
				v2 = libc.Int32FromInt32(WO_EQ) | libc.Int32FromInt32(WO_IS)
			} else {
				v2 = int32(WO_EQ)
			}
			opMask = v2
			j = 0
			for {
				if !(j < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) {
					break
				}
				pTerm = _whereScanInit(tls, bp, pWC, iCur, j, uint32(opMask), pIdx)
				for pTerm != 0 && (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight != 0 {
					pTerm = _whereScanNext(tls, bp)
				}
				if pTerm == uintptr(0) {
					break
				}
				**(**uintptr)(__ccgo_up((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*4)) = pTerm
				goto _3
			_3:
				;
				j = j + 1
			}
			if j != int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) {
				goto _1
			}
			(*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ) | libc.Int32FromInt32(WHERE_ONEROW) | libc.Int32FromInt32(WHERE_INDEXED))
			if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 56))&0x20>>5)) != 0 || (*TSrcItem)(unsafe.Pointer(pItem)).FcolUsed&(*TIndex)(unsafe.Pointer(pIdx)).FcolNotIdxed == uint64(0) {
				**(**Tu32)(__ccgo_up(pLoop + 40)) |= uint32(WHERE_IDX_ONLY)
			}
			(*TWhereLoop)(unsafe.Pointer(pLoop)).FnLTerm = uint16(j)
			(*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FnEq = uint16(j)
			(*(*struct {
				FnEq          Tu16
				FnBtm         Tu16
				FnTop         Tu16
				FnDistinctCol Tu16
				FpIndex       uintptr
				FpOrderBy     uintptr
			})(unsafe.Pointer(pLoop + 24))).FpIndex = pIdx
			/* TUNING: Cost of a unique index lookup is 15 */
			(*TWhereLoop)(unsafe.Pointer(pLoop)).FrRun = int16(39) /* 39==sqlite3LogEst(15) */
			break
			goto _1
		_1:
			;
			pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext
		}
	}
	if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags != 0 {
		(*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut = libc.Int16FromInt32(1)
		(*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760))).FpWLoop = pLoop
		(*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf = uint64(1) /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
		(*(*TWhereLevel)(unsafe.Pointer(pWInfo + 760))).FiTabCur = iCur
		(*TWhereInfo)(unsafe.Pointer(pWInfo)).FnRowOut = int16(1)
		if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy != 0 {
			(*TWhereInfo)(unsafe.Pointer(pWInfo)).FnOBSat = int8((*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr)
		}
		if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_WANT_DISTINCT) != 0 {
			(*TWhereInfo)(unsafe.Pointer(pWInfo)).FeDistinct = uint8(WHERE_DISTINCT_UNIQUE)
		}
		if int32((**(**TWhereScan)(__ccgo_up(bp))).FiEquiv) > int32(1) {
			**(**Tu32)(__ccgo_up(pLoop + 40)) |= uint32(WHERE_TRANSCONS)
		}
		return int32(1)
	}
	return 0
}

// C documentation
//
//	/* Check to see if a partial index with pPartIndexWhere can be used
//	** in the current query.  Return true if it can be and false if not.
//	*/
func _whereUsablePartialIndex(tls *libc.TLS, iTab int32, jointype Tu8, pWC uintptr, pWhere uintptr) (r int32) {
	var i int32
	var pExpr, pParse, pTerm uintptr
	_, _, _, _ = i, pExpr, pParse, pTerm
	if int32(jointype)&int32(JT_LTORJ) != 0 {
		return 0
	}
	pParse = (*TWhereInfo)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).FpWInfo)).FpParse
	for int32((*TExpr)(unsafe.Pointer(pWhere)).Fop) == int32(TK_AND) {
		if !(_whereUsablePartialIndex(tls, iTab, jointype, pWC, (*TExpr)(unsafe.Pointer(pWhere)).FpLeft) != 0) {
			return 0
		}
		pWhere = (*TExpr)(unsafe.Pointer(pWhere)).FpRight
	}
	i = 0
	pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa
	for {
		if !(i < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) {
			break
		}
		pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr
		if (!((*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != libc.Uint32FromInt32(0)) || *(*int32)(unsafe.Pointer(pExpr + 36)) == iTab) && (int32(jointype)&int32(JT_OUTER) == 0 || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0)) && _sqlite3ExprImpliesExpr(tls, pParse, pExpr, pWhere, iTab) != 0 && !(_sqlite3ExprImpliesExpr(tls, pParse, pExpr, pWhere, -int32(1)) != 0) && int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VNULL) == 0 {
			return int32(1)
		}
		goto _1
	_1:
		;
		i = i + 1
		pTerm += 48
	}
	return 0
}

// C documentation
//
//	/*
//	** Check the existence and status of a file.
//	*/
func _winAccess(tls *libc.TLS, pVfs uintptr, zFilename uintptr, flags int32, pResOut uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var attr TDWORD
	var noRetry, rc, v1 int32
	var zConverted uintptr
	var _ /* cnt at bp+4 */ int32
	var _ /* lastErrno at bp+0 */ TDWORD
	var _ /* sAttrData at bp+8 */ TWIN32_FILE_ATTRIBUTE_DATA
	_, _, _, _, _ = attr, noRetry, rc, zConverted, v1
	rc = 0
	**(**TDWORD)(__ccgo_up(bp)) = uint32(0)
	noRetry = 0 /* Do not use winRetryIoerr() */
	_ = pVfs
	if flags&int32(NORETRY) != 0 {
		noRetry = int32(1)
		flags = flags & ^libc.Int32FromInt32(NORETRY)
	}
	if zFilename == uintptr(0) {
		**(**int32)(__ccgo_up(pResOut)) = 0
		return SQLITE_OK
	}
	zConverted = _winConvertFromUtf8Filename(tls, zFilename)
	if zConverted == uintptr(0) {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
	}
	if int32(1) != 0 {
		**(**int32)(__ccgo_up(bp + 4)) = 0
		libc.Xmemset(tls, bp+8, 0, uint32(36))
		for {
			v1 = (*(*func(*libc.TLS, TLPCWSTR, TGET_FILEEX_INFO_LEVELS, TLPVOID) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(22)].FpCurrent})))(tls, zConverted, int32(_GetFileExInfoStandard), bp+8)
			rc = v1
			if !(!(v1 != 0) && !(noRetry != 0) && _winRetryIoerr(tls, bp+4, bp) != 0) {
				break
			} /* Loop until true */
		}
		if rc != 0 {
			/* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
			 ** as if it does not exist.
			 */
			if flags == SQLITE_ACCESS_EXISTS && (**(**TWIN32_FILE_ATTRIBUTE_DATA)(__ccgo_up(bp + 8))).FnFileSizeHigh == uint32(0) && (**(**TWIN32_FILE_ATTRIBUTE_DATA)(__ccgo_up(bp + 8))).FnFileSizeLow == uint32(0) {
				attr = uint32(-libc.Int32FromInt32(1))
			} else {
				attr = (**(**TWIN32_FILE_ATTRIBUTE_DATA)(__ccgo_up(bp + 8))).FdwFileAttributes
			}
		} else {
			if noRetry != 0 {
				**(**TDWORD)(__ccgo_up(bp)) = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
			}
			_winLogIoerr(tls, **(**int32)(__ccgo_up(bp + 4)), int32(54525))
			if **(**TDWORD)(__ccgo_up(bp)) != uint32(2) && **(**TDWORD)(__ccgo_up(bp)) != uint32(3) {
				Xsqlite3_free(tls, zConverted)
				return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(13)<<libc.Int32FromInt32(8), **(**TDWORD)(__ccgo_up(bp)), __ccgo_ts+5192, zFilename, int32(54528))
			} else {
				attr = uint32(-libc.Int32FromInt32(1))
			}
		}
	} else {
		attr = (*(*func(*libc.TLS, TLPCSTR) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(20)].FpCurrent})))(tls, zConverted)
	}
	Xsqlite3_free(tls, zConverted)
	switch flags {
	case int32(SQLITE_ACCESS_READ):
		fallthrough
	case SQLITE_ACCESS_EXISTS:
		rc = libc.BoolInt32(attr != uint32(-libc.Int32FromInt32(1)))
	case int32(SQLITE_ACCESS_READWRITE):
		rc = libc.BoolInt32(attr != uint32(-libc.Int32FromInt32(1)) && attr&uint32(FILE_ATTRIBUTE_READONLY) == uint32(0))
	default:
	}
	**(**int32)(__ccgo_up(pResOut)) = rc
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This routine checks if there is a RESERVED lock held on the specified
//	** file by this or any other process. If such a lock is held, return
//	** non-zero, otherwise zero.
//	*/
func _winCheckReservedLock(tls *libc.TLS, id uintptr, pResOut uintptr) (r int32) {
	var pFile uintptr
	var res int32
	_, _ = pFile, res
	pFile = id
	if int32((*TwinFile)(unsafe.Pointer(pFile)).Flocktype) >= int32(RESERVED_LOCK) {
		res = int32(1)
	} else {
		res = _winLockFile(tls, pFile+8, uint32(libc.Int32FromInt32(LOCKFILE_FAIL_IMMEDIATELY)), uint32(_sqlite3PendingByte+libc.Int32FromInt32(1)), uint32(0), uint32(1), uint32(0))
		if res != 0 {
			_winUnlockFile(tls, pFile+8, uint32(_sqlite3PendingByte+libc.Int32FromInt32(1)), uint32(0), uint32(1), uint32(0))
		}
		res = libc.BoolInt32(!(res != 0))
	}
	**(**int32)(__ccgo_up(pResOut)) = res
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Close pDbFd's connection to shared-memory.  Delete the underlying
//	** *-shm file if deleteFlag is true.
//	*/
func _winCloseSharedMemory(tls *libc.TLS, pDbFd uintptr, deleteFlag int32) (r int32) {
	var p, pShmNode, pp uintptr
	_, _, _ = p, pShmNode, pp /* The underlying shared-memory file */
	p = (*TwinFile)(unsafe.Pointer(pDbFd)).FpShm
	if p == uintptr(0) {
		return SQLITE_OK
	}
	if (*TwinShm)(unsafe.Pointer(p)).FhShm != uintptr(int32(-libc.Int32FromInt32(1))) {
		(*(*func(*libc.TLS, THANDLE) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(3)].FpCurrent})))(tls, (*TwinShm)(unsafe.Pointer(p)).FhShm)
	}
	_winShmEnterMutex(tls)
	pShmNode = (*TwinShm)(unsafe.Pointer(p)).FpShmNode
	/* Remove this connection from the winShmNode.pWinShmList list */
	Xsqlite3_mutex_enter(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
	pp = pShmNode + 40
	for {
		if !(**(**uintptr)(__ccgo_up(pp)) != p) {
			break
		}
		goto _1
	_1:
		;
		pp = **(**uintptr)(__ccgo_up(pp)) + 16
	}
	**(**uintptr)(__ccgo_up(pp)) = (*TwinShm)(unsafe.Pointer(p)).FpWinShmNext
	Xsqlite3_mutex_leave(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
	_winShmPurge(tls, (*TwinFile)(unsafe.Pointer(pDbFd)).FpVfs, deleteFlag)
	_winShmLeaveMutex(tls)
	/* Free the connection p */
	Xsqlite3_free(tls, p)
	(*TwinFile)(unsafe.Pointer(pDbFd)).FpShm = uintptr(0)
	return SQLITE_OK
}

/*
** testfixture builds may set this global variable to true via a
** Tcl interface. This forces the VFS to use the locking normally
** only used for UNC paths for all files.
 */

// C documentation
//
//	/*
//	** Control and query of the open file handle.
//	*/
func _winFileControl(tls *libc.TLS, id uintptr, op int32, pArg uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var a, pFile uintptr
	var newLimit Ti64
	var newSz Tsqlite3_int64
	var phFile TLPHANDLE
	var rc, rc1, rc2 int32
	var _ /* oldSz at bp+0 */ Tsqlite3_int64
	var _ /* zTFile at bp+8 */ uintptr
	_, _, _, _, _, _, _, _ = a, newLimit, newSz, pFile, phFile, rc, rc1, rc2
	pFile = id
	switch op {
	case int32(SQLITE_FCNTL_LOCKSTATE):
		**(**int32)(__ccgo_up(pArg)) = int32((*TwinFile)(unsafe.Pointer(pFile)).Flocktype)
		return SQLITE_OK
	case int32(SQLITE_FCNTL_LAST_ERRNO):
		**(**int32)(__ccgo_up(pArg)) = int32((*TwinFile)(unsafe.Pointer(pFile)).FlastErrno)
		return SQLITE_OK
	case int32(SQLITE_FCNTL_CHUNK_SIZE):
		(*TwinFile)(unsafe.Pointer(pFile)).FszChunk = **(**int32)(__ccgo_up(pArg))
		return SQLITE_OK
	case int32(SQLITE_FCNTL_SIZE_HINT):
		if (*TwinFile)(unsafe.Pointer(pFile)).FszChunk > 0 {
			rc = _winFileSize(tls, id, bp)
			if rc == SQLITE_OK {
				newSz = **(**Tsqlite3_int64)(__ccgo_up(pArg))
				if newSz > **(**Tsqlite3_int64)(__ccgo_up(bp)) {
					rc = _winTruncate(tls, id, newSz)
				}
			}
			return rc
		}
		return SQLITE_OK
	case int32(SQLITE_FCNTL_PERSIST_WAL):
		_winModeBit(tls, pFile, uint8(WINFILE_PERSIST_WAL), pArg)
		return SQLITE_OK
	case int32(SQLITE_FCNTL_POWERSAFE_OVERWRITE):
		_winModeBit(tls, pFile, uint8(WINFILE_PSOW), pArg)
		return SQLITE_OK
	case int32(SQLITE_FCNTL_VFSNAME):
		**(**uintptr)(__ccgo_up(pArg)) = Xsqlite3_mprintf(tls, __ccgo_ts+4729, libc.VaList(bp+24, (*Tsqlite3_vfs)(unsafe.Pointer((*TwinFile)(unsafe.Pointer(pFile)).FpVfs)).FzName))
		return SQLITE_OK
	case int32(SQLITE_FCNTL_WIN32_AV_RETRY):
		a = pArg
		if **(**int32)(__ccgo_up(a)) > 0 {
			_winIoerrRetry = **(**int32)(__ccgo_up(a))
		} else {
			**(**int32)(__ccgo_up(a)) = _winIoerrRetry
		}
		if **(**int32)(__ccgo_up(a + 1*4)) > 0 {
			_winIoerrRetryDelay = **(**int32)(__ccgo_up(a + 1*4))
		} else {
			**(**int32)(__ccgo_up(a + 1*4)) = _winIoerrRetryDelay
		}
		return SQLITE_OK
	case int32(SQLITE_FCNTL_WIN32_GET_HANDLE):
		phFile = pArg
		**(**THANDLE)(__ccgo_up(phFile)) = (*TwinFile)(unsafe.Pointer(pFile)).Fh
		return SQLITE_OK
	case int32(SQLITE_FCNTL_NULL_IO):
		(*(*func(*libc.TLS, THANDLE) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(3)].FpCurrent})))(tls, (*TwinFile)(unsafe.Pointer(pFile)).Fh)
		(*TwinFile)(unsafe.Pointer(pFile)).Fh = libc.UintptrFromInt32(0)
		return SQLITE_OK
	case int32(SQLITE_FCNTL_TEMPFILENAME):
		**(**uintptr)(__ccgo_up(bp + 8)) = uintptr(0)
		rc1 = _winGetTempname(tls, (*TwinFile)(unsafe.Pointer(pFile)).FpVfs, bp+8)
		if rc1 == SQLITE_OK {
			**(**uintptr)(__ccgo_up(pArg)) = **(**uintptr)(__ccgo_up(bp + 8))
		}
		return rc1
	case int32(SQLITE_FCNTL_MMAP_SIZE):
		newLimit = **(**Ti64)(__ccgo_up(pArg))
		rc2 = SQLITE_OK
		if newLimit > _sqlite3Config.FmxMmap {
			newLimit = _sqlite3Config.FmxMmap
		}
		/* The value of newLimit may be eventually cast to (SIZE_T) and passed
		 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
		 ** least a 64-bit type. */
		if newLimit > 0 && libc.Bool(uint32(4) < uint32(8)) {
			newLimit = newLimit & libc.Int64FromInt32(0x7FFFFFFF)
		}
		**(**Ti64)(__ccgo_up(pArg)) = (*TwinFile)(unsafe.Pointer(pFile)).FmmapSizeMax
		if newLimit >= 0 && newLimit != (*TwinFile)(unsafe.Pointer(pFile)).FmmapSizeMax && (*TwinFile)(unsafe.Pointer(pFile)).FnFetchOut == 0 {
			(*TwinFile)(unsafe.Pointer(pFile)).FmmapSizeMax = newLimit
			if (*TwinFile)(unsafe.Pointer(pFile)).FmmapSize > 0 {
				_winUnmapfile(tls, pFile)
				rc2 = _winMapfile(tls, pFile, int64(-int32(1)))
			}
		}
		return rc2
	}
	return int32(SQLITE_NOTFOUND)
}

// C documentation
//
//	/*
//	** Turn a relative pathname into a full pathname.  Write the full
//	** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
//	** bytes in size.
//	*/
func _winFullPathnameNoMutex(tls *libc.TLS, pVfs uintptr, zRelative uintptr, nFull int32, zFull uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var nByte, v1 int32
	var zConverted, zOut, zTemp1 uintptr
	var zTemp TLPWSTR
	_, _, _, _, _, _ = nByte, zConverted, zOut, zTemp, zTemp1, v1
	/* If this path name begins with "/X:" or "\\?\", where "X" is any
	 ** alphabetic character, discard the initial "/" from the pathname.
	 */
	if int32(**(**int8)(__ccgo_up(zRelative))) == int32('/') && (_winIsDriveLetterAndColon(tls, zRelative+uintptr(1)) != 0 || _winIsLongPathPrefix(tls, zRelative+uintptr(1)) != 0) {
		zRelative = zRelative + 1
	}
	/* It's odd to simulate an io-error here, but really this is just
	 ** using the io-error infrastructure to test that SQLite handles this
	 ** function failing. This function could fail if, for example, the
	 ** current working directory has been unlinked.
	 */
	if Xsqlite3_data_directory != 0 && !(_winIsVerbatimPathname(tls, zRelative) != 0) {
		/*
		 ** NOTE: We are dealing with a relative path name and the data
		 **       directory has been set.  Therefore, use it as the basis
		 **       for converting the relative path name to an absolute
		 **       one by prepending the data directory and a backslash.
		 */
		if nFull < (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname {
			v1 = nFull
		} else {
			v1 = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname
		}
		Xsqlite3_snprintf(tls, v1, zFull, __ccgo_ts+5202, libc.VaList(bp+8, Xsqlite3_data_directory, int32('\\'), zRelative))
		return SQLITE_OK
	}
	zConverted = _winConvertFromUtf8Filename(tls, zRelative)
	if zConverted == uintptr(0) {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
	}
	if int32(1) != 0 {
		nByte = int32((*(*func(*libc.TLS, TLPCWSTR, TDWORD, TLPWSTR, uintptr) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(25)].FpCurrent})))(tls, zConverted, uint32(0), uintptr(0), uintptr(0)))
		if nByte == 0 {
			Xsqlite3_free(tls, zConverted)
			return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_CANTOPEN)|libc.Int32FromInt32(3)<<libc.Int32FromInt32(8), (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls), __ccgo_ts+5209, zRelative, int32(54830))
		}
		nByte = nByte + int32(3)
		zTemp = _sqlite3MallocZero(tls, uint64(uint32(nByte)*uint32(2)))
		if zTemp == uintptr(0) {
			Xsqlite3_free(tls, zConverted)
			return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
		}
		nByte = int32((*(*func(*libc.TLS, TLPCWSTR, TDWORD, TLPWSTR, uintptr) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(25)].FpCurrent})))(tls, zConverted, uint32(nByte), zTemp, uintptr(0)))
		if nByte == 0 {
			Xsqlite3_free(tls, zConverted)
			Xsqlite3_free(tls, zTemp)
			return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_CANTOPEN)|libc.Int32FromInt32(3)<<libc.Int32FromInt32(8), (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls), __ccgo_ts+5226, zRelative, int32(54843))
		}
		Xsqlite3_free(tls, zConverted)
		zOut = _winUnicodeToUtf8(tls, zTemp)
		Xsqlite3_free(tls, zTemp)
	} else {
		nByte = int32((*(*func(*libc.TLS, TLPCSTR, TDWORD, TLPSTR, uintptr) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(24)].FpCurrent})))(tls, zConverted, uint32(0), uintptr(0), uintptr(0)))
		if nByte == 0 {
			Xsqlite3_free(tls, zConverted)
			return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_CANTOPEN)|libc.Int32FromInt32(3)<<libc.Int32FromInt32(8), (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls), __ccgo_ts+5243, zRelative, int32(54856))
		}
		zTemp1 = _sqlite3MallocZero(tls, uint64(uint32(nByte)*uint32(1)+libc.Uint32FromInt32(3)*libc.Uint32FromInt64(1)))
		if zTemp1 == uintptr(0) {
			Xsqlite3_free(tls, zConverted)
			return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
		}
		nByte = int32((*(*func(*libc.TLS, TLPCSTR, TDWORD, TLPSTR, uintptr) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(24)].FpCurrent})))(tls, zConverted, uint32(nByte+int32(3)), zTemp1, uintptr(0)))
		if nByte == 0 {
			Xsqlite3_free(tls, zConverted)
			Xsqlite3_free(tls, zTemp1)
			return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_CANTOPEN)|libc.Int32FromInt32(3)<<libc.Int32FromInt32(8), (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls), __ccgo_ts+5260, zRelative, int32(54868))
		}
		Xsqlite3_free(tls, zConverted)
		zOut = _winMbcsToUtf8(tls, zTemp1, (*(*func(*libc.TLS) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[0].FpCurrent})))(tls))
		Xsqlite3_free(tls, zTemp1)
	}
	if zOut != 0 {
		if nFull < (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname {
			v1 = nFull
		} else {
			v1 = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname
		}
		Xsqlite3_snprintf(tls, v1, zFull, __ccgo_ts+4729, libc.VaList(bp+8, zOut))
		Xsqlite3_free(tls, zOut)
		return SQLITE_OK
	} else {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
	}
	return r
}

// C documentation
//
//	/*
//	** The return value of winGetLastErrorMsg
//	** is zero if the error message fits in the buffer, or non-zero
//	** otherwise (if the message was truncated).
//	*/
func _winGetLastErrorMsg(tls *libc.TLS, lastErrno TDWORD, nBuf int32, zBuf uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var dwLen TDWORD
	var zOut uintptr
	var _ /* zTemp at bp+4 */ uintptr
	var _ /* zTempWide at bp+0 */ TLPWSTR
	_, _ = dwLen, zOut
	/* FormatMessage returns 0 on failure.  Otherwise it
	 ** returns the number of TCHARs written to the output
	 ** buffer, excluding the terminating null char.
	 */
	dwLen = uint32(0)
	zOut = uintptr(0)
	if int32(1) != 0 {
		**(**TLPWSTR)(__ccgo_up(bp)) = libc.UintptrFromInt32(0)
		dwLen = (*(*func(*libc.TLS, TDWORD, TLPCVOID, TDWORD, TDWORD, TLPWSTR, TDWORD, uintptr) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(15)].FpCurrent})))(tls, uint32(libc.Int32FromInt32(FORMAT_MESSAGE_ALLOCATE_BUFFER)|libc.Int32FromInt32(FORMAT_MESSAGE_FROM_SYSTEM)|libc.Int32FromInt32(FORMAT_MESSAGE_IGNORE_INSERTS)), libc.UintptrFromInt32(0), lastErrno, uint32(0), bp, uint32(0), uintptr(0))
		if dwLen > uint32(0) {
			/* allocate a buffer and convert to UTF8 */
			_sqlite3BeginBenignMalloc(tls)
			zOut = _winUnicodeToUtf8(tls, **(**TLPWSTR)(__ccgo_up(bp)))
			_sqlite3EndBenignMalloc(tls)
			/* free the system buffer allocated by FormatMessage */
			(*(*func(*libc.TLS, THLOCAL) THLOCAL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(46)].FpCurrent})))(tls, **(**TLPWSTR)(__ccgo_up(bp)))
		}
	} else {
		**(**uintptr)(__ccgo_up(bp + 4)) = libc.UintptrFromInt32(0)
		dwLen = (*(*func(*libc.TLS, TDWORD, TLPCVOID, TDWORD, TDWORD, TLPSTR, TDWORD, uintptr) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(14)].FpCurrent})))(tls, uint32(libc.Int32FromInt32(FORMAT_MESSAGE_ALLOCATE_BUFFER)|libc.Int32FromInt32(FORMAT_MESSAGE_FROM_SYSTEM)|libc.Int32FromInt32(FORMAT_MESSAGE_IGNORE_INSERTS)), libc.UintptrFromInt32(0), lastErrno, uint32(0), bp+4, uint32(0), uintptr(0))
		if dwLen > uint32(0) {
			/* allocate a buffer and convert to UTF8 */
			_sqlite3BeginBenignMalloc(tls)
			zOut = _winMbcsToUtf8(tls, **(**uintptr)(__ccgo_up(bp + 4)), (*(*func(*libc.TLS) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[0].FpCurrent})))(tls))
			_sqlite3EndBenignMalloc(tls)
			/* free the system buffer allocated by FormatMessage */
			(*(*func(*libc.TLS, THLOCAL) THLOCAL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(46)].FpCurrent})))(tls, **(**uintptr)(__ccgo_up(bp + 4)))
		}
	}
	if uint32(0) == dwLen {
		Xsqlite3_snprintf(tls, nBuf, zBuf, __ccgo_ts+4732, libc.VaList(bp+16, lastErrno, lastErrno))
	} else {
		/* copy a maximum of nBuf chars to output buffer */
		Xsqlite3_snprintf(tls, nBuf, zBuf, __ccgo_ts+4729, libc.VaList(bp+16, zOut))
		/* free the UTF8 buffer */
		Xsqlite3_free(tls, zOut)
	}
	return 0
}

// C documentation
//
//	/*
//	** Acquire a reader lock.
//	** Different API routines are called depending on whether or not this
//	** is Win9x or WinNT.
//	*/
func _winGetReadLock(tls *libc.TLS, pFile uintptr, bBlock int32) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var mask TDWORD
	var res, v1 int32
	var _ /* lk at bp+0 */ int32
	_, _, _ = mask, res, v1
	if bBlock != 0 {
		v1 = int32(LOCKFILE_FAIL_IMMEDIATELY)
	} else {
		v1 = 0
	}
	mask = uint32(^v1)
	if int32(1) != 0 {
		res = _winLockFile(tls, pFile+8, uint32(libc.Int32FromInt32(LOCKFILE_FAIL_IMMEDIATELY))&mask, uint32(_sqlite3PendingByte+libc.Int32FromInt32(2)), uint32(0), uint32(SHARED_SIZE), uint32(0))
	} else {
		Xsqlite3_randomness(tls, int32(4), bp)
		(*TwinFile)(unsafe.Pointer(pFile)).FsharedLockByte = int16(**(**int32)(__ccgo_up(bp)) & libc.Int32FromInt32(0x7fffffff) % (libc.Int32FromInt32(SHARED_SIZE) - libc.Int32FromInt32(1)))
		res = _winLockFile(tls, pFile+8, uint32(libc.Int32FromInt32(LOCKFILE_FAIL_IMMEDIATELY)|libc.Int32FromInt32(LOCKFILE_EXCLUSIVE_LOCK))&mask, uint32(_sqlite3PendingByte+int32(2)+int32((*TwinFile)(unsafe.Pointer(pFile)).FsharedLockByte)), uint32(0), uint32(1), uint32(0))
	}
	if res == 0 {
		(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
		/* No need to log a failure to lock */
	}
	return res
}

// C documentation
//
//	/*
//	** Return the value of a system call.  Return NULL if zName is not a
//	** recognized system call name.  NULL is also returned if the system call
//	** is currently undefined.
//	*/
func _winGetSystemCall(tls *libc.TLS, pNotUsed uintptr, zName uintptr) (r Tsqlite3_syscall_ptr) {
	var i uint32
	_ = i
	_ = pNotUsed
	i = uint32(0)
	for {
		if !(i < libc.Uint32FromInt64(972)/libc.Uint32FromInt64(12)) {
			break
		}
		if libc.Xstrcmp(tls, zName, _aSyscall[i].FzName) == 0 {
			return _aSyscall[i].FpCurrent
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	return uintptr(0)
}

// C documentation
//
//	/*
//	** Create a temporary file name and store the resulting pointer into pzBuf.
//	** The pointer returned in pzBuf must be freed via sqlite3_free().
//	*/
func _winGetTempname(tls *libc.TLS, pVfs uintptr, pzBuf uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var i, j Tsize_t
	var nBuf, nDir, nLen, nMax Ti64
	var nDirLen, nPre int32
	var pid TDWORD
	var zBuf, zMbcsPath, zMulti, zUtf8, v2 uintptr
	var zWidePath TLPWSTR
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, j, nBuf, nDir, nDirLen, nLen, nMax, nPre, pid, zBuf, zMbcsPath, zMulti, zUtf8, zWidePath, v2
	nPre = _sqlite3Strlen30(tls, __ccgo_ts+5071)
	/* It's odd to simulate an io-error here, but really this is just
	 ** using the io-error infrastructure to test that SQLite handles this
	 ** function failing.
	 */
	/* Allocate a temporary buffer to store the fully qualified file
	 ** name for the temporary file.  If this fails, we cannot continue.
	 */
	nMax = int64((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname)
	nBuf = int64(2) + nMax
	zBuf = _sqlite3MallocZero(tls, uint64(nBuf))
	if !(zBuf != 0) {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
	}
	/* Figure out the effective temporary directory.  First, check if one
	 ** has been explicitly set by the application; otherwise, use the one
	 ** configured by the operating system.
	 */
	nDir = nMax - int64(nPre+libc.Int32FromInt32(15))
	if _winTempDirDefined(tls) != 0 {
		nDirLen = _sqlite3Strlen30(tls, Xsqlite3_temp_directory)
		if nDirLen > 0 {
			if !(int32(**(**int8)(__ccgo_up(Xsqlite3_temp_directory + uintptr(nDirLen-int32(1))))) == int32('/') || int32(**(**int8)(__ccgo_up(Xsqlite3_temp_directory + uintptr(nDirLen-int32(1))))) == int32('\\')) {
				nDirLen = nDirLen + 1
			}
			if int64(nDirLen) > nDir {
				Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)))
				Xsqlite3_free(tls, zBuf)
				return _winLogErrorAtLine(tls, int32(SQLITE_ERROR), uint32(0), __ccgo_ts+5079, uintptr(0), int32(53885))
			}
			Xsqlite3_snprintf(tls, int32(nMax), zBuf, __ccgo_ts+4729, libc.VaList(bp+8, Xsqlite3_temp_directory))
		}
		Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)))
	} else {
		if int32(1) != 0 {
			zWidePath = _sqlite3MallocZero(tls, uint64(nMax*int64(2)))
			if !(zWidePath != 0) {
				Xsqlite3_free(tls, zBuf)
				return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
			}
			if (*(*func(*libc.TLS, TDWORD, TLPWSTR) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(32)].FpCurrent})))(tls, uint32(nMax), zWidePath) == uint32(0) {
				Xsqlite3_free(tls, zWidePath)
				Xsqlite3_free(tls, zBuf)
				return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(25)<<libc.Int32FromInt32(8), (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls), __ccgo_ts+5095, uintptr(0), int32(53952))
			}
			zMulti = _winUnicodeToUtf8(tls, zWidePath)
			if zMulti != 0 {
				Xsqlite3_snprintf(tls, int32(nMax), zBuf, __ccgo_ts+4729, libc.VaList(bp+8, zMulti))
				Xsqlite3_free(tls, zMulti)
				Xsqlite3_free(tls, zWidePath)
			} else {
				Xsqlite3_free(tls, zWidePath)
				Xsqlite3_free(tls, zBuf)
				return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
			}
		} else {
			zMbcsPath = _sqlite3MallocZero(tls, uint64(nMax))
			if !(zMbcsPath != 0) {
				Xsqlite3_free(tls, zBuf)
				return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
			}
			if (*(*func(*libc.TLS, TDWORD, TLPSTR) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(31)].FpCurrent})))(tls, uint32(nMax), zMbcsPath) == uint32(0) {
				Xsqlite3_free(tls, zBuf)
				return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(25)<<libc.Int32FromInt32(8), (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls), __ccgo_ts+5111, uintptr(0), int32(53979))
			}
			zUtf8 = _winMbcsToUtf8(tls, zMbcsPath, (*(*func(*libc.TLS) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[0].FpCurrent})))(tls))
			if zUtf8 != 0 {
				Xsqlite3_snprintf(tls, int32(nMax), zBuf, __ccgo_ts+4729, libc.VaList(bp+8, zUtf8))
				Xsqlite3_free(tls, zUtf8)
			} else {
				Xsqlite3_free(tls, zBuf)
				return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
			}
		}
	}
	/*
	 ** Check to make sure the temporary directory ends with an appropriate
	 ** separator.  If it does not and there is not enough space left to add
	 ** one, fail.
	 */
	if !(_winMakeEndInDirSep(tls, int32(nDir+int64(1)), zBuf) != 0) {
		Xsqlite3_free(tls, zBuf)
		return _winLogErrorAtLine(tls, int32(SQLITE_ERROR), uint32(0), __ccgo_ts+5127, uintptr(0), int32(54002))
	}
	/*
	 ** Check that the output buffer is large enough for the temporary file
	 ** name in the following format:
	 **
	 **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
	 **
	 ** If not, return SQLITE_ERROR.  The number 17 is used here in order to
	 ** account for the space used by the 15 character random suffix and the
	 ** two trailing NUL characters.  The final directory separator character
	 ** has already added if it was not already present.
	 */
	nLen = int64(_sqlite3Strlen30(tls, zBuf))
	if nLen+int64(nPre)+int64(17) > nBuf {
		Xsqlite3_free(tls, zBuf)
		return _winLogErrorAtLine(tls, int32(SQLITE_ERROR), uint32(0), __ccgo_ts+5143, uintptr(0), int32(54020))
	}
	Xsqlite3_snprintf(tls, int32(nBuf-int64(16)-nLen), zBuf+uintptr(nLen), __ccgo_ts+5071, 0)
	j = uint32(_sqlite3Strlen30(tls, zBuf))
	Xsqlite3_randomness(tls, int32(15), zBuf+uintptr(j))
	pid = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(17)].FpCurrent})))(tls)
	i = uint32(0)
	for {
		if !(i < uint32(15)) {
			break
		}
		v2 = zBuf + uintptr(j)
		*(*int8)(unsafe.Pointer(v2)) = int8(uint32(*(*int8)(unsafe.Pointer(v2))) + pid&libc.Uint32FromInt32(0xff))
		pid = pid >> uint32(8)
		**(**int8)(__ccgo_up(zBuf + uintptr(j))) = _zChars[uint32(uint8(**(**int8)(__ccgo_up(zBuf + uintptr(j)))))%(libc.Uint32FromInt64(63)-libc.Uint32FromInt32(1))]
		goto _1
	_1:
		;
		i = i + 1
		j = j + 1
	}
	**(**int8)(__ccgo_up(zBuf + uintptr(j))) = 0
	**(**int8)(__ccgo_up(zBuf + uintptr(j+uint32(1)))) = 0
	**(**uintptr)(__ccgo_up(pzBuf)) = zBuf
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Close the handle passed as the only argument.
//	*/
func _winHandleClose(tls *libc.TLS, h THANDLE) {
	if h != uintptr(int32(-libc.Int32FromInt32(1))) {
		(*(*func(*libc.TLS, THANDLE) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(3)].FpCurrent})))(tls, h)
	}
}

// C documentation
//
//	/*
//	** Lock a region of nByte bytes starting at offset offset of file hFile.
//	** Take an EXCLUSIVE lock if parameter bExclusive is true, or a SHARED lock
//	** otherwise. If nMs is greater than zero and the lock cannot be obtained
//	** immediately, block for that many ms before giving up.
//	**
//	** This function returns SQLITE_OK if the lock is obtained successfully. If
//	** some other process holds the lock, SQLITE_BUSY is returned if nMs==0, or
//	** SQLITE_BUSY_TIMEOUT otherwise. Or, if an error occurs, SQLITE_IOERR.
//	*/
func _winHandleLockTimeout(tls *libc.TLS, _hFile THANDLE, offset TDWORD, nByte TDWORD, bExcl int32, nMs TDWORD) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	*(*THANDLE)(unsafe.Pointer(bp)) = _hFile
	var flags TDWORD
	var rc, v1 int32
	var ret TBOOL
	var _ /* ovlp at bp+4 */ TOVERLAPPED
	_, _, _, _ = flags, rc, ret, v1
	if bExcl != 0 {
		v1 = int32(LOCKFILE_EXCLUSIVE_LOCK)
	} else {
		v1 = 0
	}
	flags = uint32(int32(LOCKFILE_FAIL_IMMEDIATELY) | v1)
	rc = SQLITE_OK
	if !(libc.Int32FromInt32(1) != 0) {
		ret = _winLockFile(tls, bp, flags, offset, uint32(0), nByte, uint32(0))
	} else {
		libc.Xmemset(tls, bp+4, 0, uint32(20))
		(**(**TOVERLAPPED)(__ccgo_up(bp + 4))).F__ccgo2_8.F__ccgo0_0.FOffset = offset
		ret = (*(*func(*libc.TLS, THANDLE, TDWORD, TDWORD, TDWORD, TDWORD, TLPOVERLAPPED) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(48)].FpCurrent})))(tls, **(**THANDLE)(__ccgo_up(bp)), flags, uint32(0), nByte, uint32(0), bp+4)
	}
	if rc == SQLITE_OK && !(ret != 0) {
		rc = int32(SQLITE_BUSY)
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is used to open a handle on a *-shm file.
//	**
//	** If SQLITE_ENABLE_SETLK_TIMEOUT is defined at build time, then the file
//	** is opened with FILE_FLAG_OVERLAPPED specified. If not, it is not.
//	*/
func _winHandleOpen(tls *libc.TLS, zUtf8 uintptr, pbReadonly uintptr, ph uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var flag_overlapped TDWORD
	var rc, v1 int32
	var zConverted uintptr
	var _ /* bReadonly at bp+0 */ int32
	var _ /* h at bp+4 */ THANDLE
	_, _, _, _ = flag_overlapped, rc, zConverted, v1
	rc = SQLITE_OK
	zConverted = uintptr(0)
	**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(pbReadonly))
	**(**THANDLE)(__ccgo_up(bp + 4)) = uintptr(int32(-libc.Int32FromInt32(1)))
	flag_overlapped = uint32(0)
	/* Convert the filename to the system encoding. */
	zConverted = _winConvertFromUtf8Filename(tls, zUtf8)
	if zConverted == uintptr(0) {
		rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
		goto winopenfile_out
	}
	/* Ensure the file we are trying to open is not actually a directory. */
	if _winIsDir(tls, zConverted) != 0 {
		rc = libc.Int32FromInt32(SQLITE_CANTOPEN) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
		goto winopenfile_out
	}
	/* TODO: platforms.
	 ** TODO: retry-on-ioerr.
	 */
	if int32(1) != 0 {
		if **(**int32)(__ccgo_up(bp)) != 0 {
			v1 = 0
		} else {
			v1 = int32(0x40000000)
		}
		**(**THANDLE)(__ccgo_up(bp + 4)) = (*(*func(*libc.TLS, TLPCWSTR, TDWORD, TDWORD, TLPSECURITY_ATTRIBUTES, TDWORD, TDWORD, THANDLE) THANDLE)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, zConverted, libc.Uint32FromUint32(0x80000000)|uint32(v1), uint32(libc.Int32FromInt32(FILE_SHARE_READ)|libc.Int32FromInt32(FILE_SHARE_WRITE)), libc.UintptrFromInt32(0), uint32(OPEN_ALWAYS), uint32(FILE_ATTRIBUTE_NORMAL)|flag_overlapped, libc.UintptrFromInt32(0))
	} else {
		/* Due to pre-processor directives earlier in this file,
		 ** SQLITE_WIN32_HAS_ANSI is always defined if osIsNT() is false. */
		if **(**int32)(__ccgo_up(bp)) != 0 {
			v1 = 0
		} else {
			v1 = int32(0x40000000)
		}
		**(**THANDLE)(__ccgo_up(bp + 4)) = (*(*func(*libc.TLS, TLPCSTR, TDWORD, TDWORD, TLPSECURITY_ATTRIBUTES, TDWORD, TDWORD, THANDLE) THANDLE)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(4)].FpCurrent})))(tls, zConverted, libc.Uint32FromUint32(0x80000000)|uint32(v1), uint32(libc.Int32FromInt32(FILE_SHARE_READ)|libc.Int32FromInt32(FILE_SHARE_WRITE)), libc.UintptrFromInt32(0), uint32(OPEN_ALWAYS), uint32(FILE_ATTRIBUTE_NORMAL)|flag_overlapped, libc.UintptrFromInt32(0))
	}
	if **(**THANDLE)(__ccgo_up(bp + 4)) == uintptr(int32(-libc.Int32FromInt32(1))) {
		if **(**int32)(__ccgo_up(bp)) == 0 {
			**(**int32)(__ccgo_up(bp)) = int32(1)
			rc = _winHandleOpen(tls, zUtf8, bp, bp+4)
		} else {
			rc = _sqlite3CantopenError(tls, int32(52974))
		}
	}
	goto winopenfile_out
winopenfile_out:
	;
	Xsqlite3_free(tls, zConverted)
	**(**int32)(__ccgo_up(pbReadonly)) = **(**int32)(__ccgo_up(bp))
	**(**THANDLE)(__ccgo_up(ph)) = **(**THANDLE)(__ccgo_up(bp + 4))
	return rc
}

// C documentation
//
//	/*
//	** Return TRUE if the named file is really a directory.  Return false if
//	** it is something other than a directory, or if there is any kind of memory
//	** allocation failure.
//	*/
func _winIsDir(tls *libc.TLS, zConverted uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var attr TDWORD
	var rc, v1 int32
	var _ /* cnt at bp+4 */ int32
	var _ /* lastErrno at bp+0 */ TDWORD
	var _ /* sAttrData at bp+8 */ TWIN32_FILE_ATTRIBUTE_DATA
	_, _, _ = attr, rc, v1
	rc = 0
	if int32(1) != 0 {
		**(**int32)(__ccgo_up(bp + 4)) = 0
		libc.Xmemset(tls, bp+8, 0, uint32(36))
		for {
			v1 = (*(*func(*libc.TLS, TLPCWSTR, TGET_FILEEX_INFO_LEVELS, TLPVOID) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(22)].FpCurrent})))(tls, zConverted, int32(_GetFileExInfoStandard), bp+8)
			rc = v1
			if !(!(v1 != 0) && _winRetryIoerr(tls, bp+4, bp) != 0) {
				break
			}
		}
		if !(rc != 0) {
			return 0 /* Invalid name? */
		}
		attr = (**(**TWIN32_FILE_ATTRIBUTE_DATA)(__ccgo_up(bp + 8))).FdwFileAttributes
	} else {
		attr = (*(*func(*libc.TLS, TLPCSTR) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(20)].FpCurrent})))(tls, zConverted)
	}
	return libc.BoolInt32(attr != uint32(-libc.Int32FromInt32(1)) && attr&uint32(FILE_ATTRIBUTE_DIRECTORY) != 0)
}

/*
** The Windows version of xAccess() accepts an extra bit in the flags
** parameter that prevents an anti-virus retry loop.
 */

// C documentation
//
//	/*
//	** Lock the file with the lock specified by parameter locktype - one
//	** of the following:
//	**
//	**     (1) SHARED_LOCK
//	**     (2) RESERVED_LOCK
//	**     (3) PENDING_LOCK
//	**     (4) EXCLUSIVE_LOCK
//	**
//	** Sometimes when requesting one lock state, additional lock states
//	** are inserted in between.  The locking might fail on one of the later
//	** transitions leaving the lock state different from what it started but
//	** still short of its goal.  The following chart shows the allowed
//	** transitions and the inserted intermediate states:
//	**
//	**    UNLOCKED -> SHARED
//	**    SHARED -> RESERVED
//	**    SHARED -> (PENDING) -> EXCLUSIVE
//	**    RESERVED -> (PENDING) -> EXCLUSIVE
//	**    PENDING -> EXCLUSIVE
//	**
//	** This routine will only increase a lock.  The winUnlock() routine
//	** erases all locks at once and returns us immediately to locking level 0.
//	** It is not possible to lower the locking level one step at a time.  You
//	** must go straight to locking level 0.
//	*/
func _winLock(tls *libc.TLS, id uintptr, locktype int32) (r int32) {
	var cnt, flags, gotPendingLock, newLocktype, rc, res int32
	var lastErrno TDWORD
	var pFile uintptr
	_, _, _, _, _, _, _, _ = cnt, flags, gotPendingLock, lastErrno, newLocktype, pFile, rc, res
	rc = SQLITE_OK     /* Return code from subroutines */
	res = int32(1)     /* Set pFile->locktype to this value before exiting */
	gotPendingLock = 0 /* True if we acquired a PENDING lock this time */
	pFile = id
	lastErrno = uint32(0)
	/* If there is already a lock of this type or more restrictive on the
	 ** OsFile, do nothing. Don't use the end_lock: exit path, as
	 ** sqlite3OsEnterMutex() hasn't been called yet.
	 */
	if int32((*TwinFile)(unsafe.Pointer(pFile)).Flocktype) >= locktype {
		return SQLITE_OK
	}
	/* Do not allow any kind of write-lock on a read-only database
	 */
	if int32((*TwinFile)(unsafe.Pointer(pFile)).FctrlFlags)&int32(WINFILE_RDONLY) != 0 && locktype >= int32(RESERVED_LOCK) {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(15)<<libc.Int32FromInt32(8)
	}
	/* Make sure the locking sequence is correct
	 */
	/* Lock the PENDING_LOCK byte if we need to acquire an EXCLUSIVE lock or
	 ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
	 ** the PENDING_LOCK byte is temporary.
	 */
	newLocktype = int32((*TwinFile)(unsafe.Pointer(pFile)).Flocktype)
	if locktype == int32(SHARED_LOCK) || locktype == int32(EXCLUSIVE_LOCK) && int32((*TwinFile)(unsafe.Pointer(pFile)).Flocktype) == int32(RESERVED_LOCK) {
		cnt = int32(3)
		/* Flags for the LockFileEx() call. This should be an exclusive lock if
		 ** this call is to obtain EXCLUSIVE, or a shared lock if this call is to
		 ** obtain SHARED.  */
		flags = int32(LOCKFILE_FAIL_IMMEDIATELY)
		if locktype == int32(EXCLUSIVE_LOCK) {
			flags = flags | int32(LOCKFILE_EXCLUSIVE_LOCK)
		}
		for cnt > 0 {
			/* Try 3 times to get the pending lock.  This is needed to work
			 ** around problems caused by indexing and/or anti-virus software on
			 ** Windows systems.
			 **
			 ** If you are using this code as a model for alternative VFSes, do not
			 ** copy this retry logic.  It is a hack intended for Windows only.  */
			res = _winLockFile(tls, pFile+8, uint32(flags), uint32(_sqlite3PendingByte), uint32(0), uint32(1), uint32(0))
			if res != 0 {
				break
			}
			lastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
			if lastErrno == uint32(6) {
				(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = lastErrno
				rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(15)<<libc.Int32FromInt32(8)
				return rc
			}
			cnt = cnt - 1
			if cnt > 0 {
				Xsqlite3_win32_sleep(tls, uint32(1))
			}
		}
		gotPendingLock = res
	}
	/* Acquire a shared lock
	 */
	if locktype == int32(SHARED_LOCK) && res != 0 {
		res = _winGetReadLock(tls, pFile, 0)
		if res != 0 {
			newLocktype = int32(SHARED_LOCK)
		} else {
			lastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
		}
	}
	/* Acquire a RESERVED lock
	 */
	if locktype == int32(RESERVED_LOCK) && res != 0 {
		res = _winLockFile(tls, pFile+8, uint32(libc.Int32FromInt32(LOCKFILE_FAIL_IMMEDIATELY)|libc.Int32FromInt32(LOCKFILE_EXCLUSIVE_LOCK)), uint32(_sqlite3PendingByte+libc.Int32FromInt32(1)), uint32(0), uint32(1), uint32(0))
		if res != 0 {
			newLocktype = int32(RESERVED_LOCK)
		} else {
			lastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
		}
	}
	/* Acquire a PENDING lock
	 */
	if locktype == int32(EXCLUSIVE_LOCK) && res != 0 {
		newLocktype = int32(PENDING_LOCK)
		gotPendingLock = 0
	}
	/* Acquire an EXCLUSIVE lock
	 */
	if locktype == int32(EXCLUSIVE_LOCK) && res != 0 {
		_winUnlockReadLock(tls, pFile)
		res = _winLockFile(tls, pFile+8, uint32(libc.Int32FromInt32(LOCKFILE_FAIL_IMMEDIATELY)|libc.Int32FromInt32(LOCKFILE_EXCLUSIVE_LOCK)), uint32(_sqlite3PendingByte+libc.Int32FromInt32(2)), uint32(0), uint32(SHARED_SIZE), uint32(0))
		if res != 0 {
			newLocktype = int32(EXCLUSIVE_LOCK)
		} else {
			lastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
			_winGetReadLock(tls, pFile, 0)
		}
	}
	/* If we are holding a PENDING lock that ought to be released, then
	 ** release it now.
	 */
	if gotPendingLock != 0 && locktype == int32(SHARED_LOCK) {
		_winUnlockFile(tls, pFile+8, uint32(_sqlite3PendingByte), uint32(0), uint32(1), uint32(0))
	}
	/* Update the state of the lock has held in the file descriptor then
	 ** return the appropriate result code.
	 */
	if res != 0 {
		rc = SQLITE_OK
	} else {
		(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = lastErrno
		rc = int32(SQLITE_BUSY)
	}
	(*TwinFile)(unsafe.Pointer(pFile)).Flocktype = uint8(newLocktype)
	return rc
}

// C documentation
//
//	/*
//	** Lock a file region.
//	*/
func _winLockFile(tls *libc.TLS, phFile TLPHANDLE, flags TDWORD, offsetLow TDWORD, offsetHigh TDWORD, numBytesLow TDWORD, numBytesHigh TDWORD) (r TBOOL) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var _ /* ovlp at bp+0 */ TOVERLAPPED
	if int32(1) != 0 {
		libc.Xmemset(tls, bp, 0, uint32(20))
		(**(**TOVERLAPPED)(__ccgo_up(bp))).F__ccgo2_8.F__ccgo0_0.FOffset = offsetLow
		(**(**TOVERLAPPED)(__ccgo_up(bp))).F__ccgo2_8.F__ccgo0_0.FOffsetHigh = offsetHigh
		return (*(*func(*libc.TLS, THANDLE, TDWORD, TDWORD, TDWORD, TDWORD, TLPOVERLAPPED) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(48)].FpCurrent})))(tls, **(**THANDLE)(__ccgo_up(phFile)), flags, uint32(0), numBytesLow, numBytesHigh, bp)
	} else {
		return (*(*func(*libc.TLS, THANDLE, TDWORD, TDWORD, TDWORD, TDWORD) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(47)].FpCurrent})))(tls, **(**THANDLE)(__ccgo_up(phFile)), offsetLow, offsetHigh, numBytesLow, numBytesHigh)
	}
	return r
}

var _winLongPathNolockVfs = Tsqlite3_vfs{
	FiVersion:   int32(3),
	FszOsFile:   int32(64),
	FmxPathname: int32(libc.Uint32FromInt64(2) * uint32(libc.Int32FromInt32(UNICODE_STRING_MAX_CHARS))),
	FzName:      __ccgo_ts + 5309,
	FpAppData:   uintptr(unsafe.Pointer(&_winNolockAppData)),
}

var _winLongPathVfs = Tsqlite3_vfs{
	FiVersion:   int32(3),
	FszOsFile:   int32(64),
	FmxPathname: int32(libc.Uint32FromInt64(2) * uint32(libc.Int32FromInt32(UNICODE_STRING_MAX_CHARS))),
	FzName:      __ccgo_ts + 5283,
	FpAppData:   uintptr(unsafe.Pointer(&_winAppData)),
}

// C documentation
//
//	/*
//	** Memory map or remap the file opened by file-descriptor pFd (if the file
//	** is already mapped, the existing mapping is replaced by the new). Or, if
//	** there already exists a mapping for this file, and there are still
//	** outstanding xFetch() references to it, this function is a no-op.
//	**
//	** If parameter nByte is non-negative, then it is the requested size of
//	** the mapping to create. Otherwise, if nByte is less than zero, then the
//	** requested size is the size of the file on disk. The actual size of the
//	** created mapping is either the requested size or the value configured
//	** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
//	**
//	** SQLITE_OK is returned if no error occurs (even if the mapping is not
//	** recreated as a result of outstanding references) or an SQLite error
//	** code otherwise.
//	*/
func _winMapfile(tls *libc.TLS, pFd uintptr, nByte Tsqlite3_int64) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var flags, protect TDWORD
	var pNew uintptr
	var rc int32
	var _ /* nMap at bp+0 */ Tsqlite3_int64
	_, _, _, _ = flags, pNew, protect, rc
	**(**Tsqlite3_int64)(__ccgo_up(bp)) = nByte
	if (*TwinFile)(unsafe.Pointer(pFd)).FnFetchOut > 0 {
		return SQLITE_OK
	}
	if **(**Tsqlite3_int64)(__ccgo_up(bp)) < 0 {
		rc = _winFileSize(tls, pFd, bp)
		if rc != 0 {
			return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(7)<<libc.Int32FromInt32(8)
		}
	}
	if **(**Tsqlite3_int64)(__ccgo_up(bp)) > (*TwinFile)(unsafe.Pointer(pFd)).FmmapSizeMax {
		**(**Tsqlite3_int64)(__ccgo_up(bp)) = (*TwinFile)(unsafe.Pointer(pFd)).FmmapSizeMax
	}
	**(**Tsqlite3_int64)(__ccgo_up(bp)) = **(**Tsqlite3_int64)(__ccgo_up(bp)) & ^int64(_winSysInfo.FdwPageSize-libc.Uint32FromInt32(1))
	if **(**Tsqlite3_int64)(__ccgo_up(bp)) == 0 && (*TwinFile)(unsafe.Pointer(pFd)).FmmapSize > 0 {
		_winUnmapfile(tls, pFd)
	}
	if **(**Tsqlite3_int64)(__ccgo_up(bp)) != (*TwinFile)(unsafe.Pointer(pFd)).FmmapSize {
		pNew = uintptr(0)
		protect = uint32(PAGE_READONLY)
		flags = uint32(SECTION_MAP_READ)
		_winUnmapfile(tls, pFd)
		(*TwinFile)(unsafe.Pointer(pFd)).FhMap = (*(*func(*libc.TLS, THANDLE, TLPSECURITY_ATTRIBUTES, TDWORD, TDWORD, TDWORD, TLPCWSTR) THANDLE)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TwinFile)(unsafe.Pointer(pFd)).Fh, libc.UintptrFromInt32(0), protect, uint32(**(**Tsqlite3_int64)(__ccgo_up(bp))>>libc.Int32FromInt32(32)&libc.Int64FromUint32(0xffffffff)), uint32(**(**Tsqlite3_int64)(__ccgo_up(bp))&libc.Int64FromUint32(0xffffffff)), libc.UintptrFromInt32(0))
		if (*TwinFile)(unsafe.Pointer(pFd)).FhMap == libc.UintptrFromInt32(0) {
			(*TwinFile)(unsafe.Pointer(pFd)).FlastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
			rc = _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(24)<<libc.Int32FromInt32(8), (*TwinFile)(unsafe.Pointer(pFd)).FlastErrno, __ccgo_ts+5047, (*TwinFile)(unsafe.Pointer(pFd)).FzPath, int32(53596))
			/* Log the error, but continue normal operation using xRead/xWrite */
			return SQLITE_OK
		}
		pNew = (*(*func(*libc.TLS, THANDLE, TDWORD, TDWORD, TDWORD, TSIZE_T) TLPVOID)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(49)].FpCurrent})))(tls, (*TwinFile)(unsafe.Pointer(pFd)).FhMap, flags, uint32(0), uint32(0), uint32(**(**Tsqlite3_int64)(__ccgo_up(bp))))
		if pNew == libc.UintptrFromInt32(0) {
			(*(*func(*libc.TLS, THANDLE) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(3)].FpCurrent})))(tls, (*TwinFile)(unsafe.Pointer(pFd)).FhMap)
			(*TwinFile)(unsafe.Pointer(pFd)).FhMap = libc.UintptrFromInt32(0)
			(*TwinFile)(unsafe.Pointer(pFd)).FlastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
			rc = _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(24)<<libc.Int32FromInt32(8), (*TwinFile)(unsafe.Pointer(pFd)).FlastErrno, __ccgo_ts+5059, (*TwinFile)(unsafe.Pointer(pFd)).FzPath, int32(53610))
			/* Log the error, but continue normal operation using xRead/xWrite */
			return SQLITE_OK
		}
		(*TwinFile)(unsafe.Pointer(pFd)).FpMapRegion = pNew
		(*TwinFile)(unsafe.Pointer(pFd)).FmmapSize = **(**Tsqlite3_int64)(__ccgo_up(bp))
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
//	** code page.
//	**
//	** Space to hold the returned string is obtained from sqlite3_malloc().
//	*/
func _winMbcsToUnicode(tls *libc.TLS, zText uintptr, useAnsi int32) (r TLPWSTR) {
	var codepage, nWideChar, v1 int32
	var zMbcsText TLPWSTR
	_, _, _, _ = codepage, nWideChar, zMbcsText, v1
	if useAnsi != 0 {
		v1 = CP_ACP
	} else {
		v1 = int32(CP_OEMCP)
	}
	codepage = v1
	nWideChar = (*(*func(*libc.TLS, TUINT, TDWORD, TLPCSTR, int32, TLPWSTR, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(50)].FpCurrent})))(tls, uint32(codepage), uint32(0), zText, -int32(1), libc.UintptrFromInt32(0), 0)
	if nWideChar == 0 {
		return uintptr(0)
	}
	zMbcsText = _sqlite3MallocZero(tls, uint64(uint32(nWideChar)*uint32(2)))
	if zMbcsText == uintptr(0) {
		return uintptr(0)
	}
	nWideChar = (*(*func(*libc.TLS, TUINT, TDWORD, TLPCSTR, int32, TLPWSTR, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(50)].FpCurrent})))(tls, uint32(codepage), uint32(0), zText, -int32(1), zMbcsText, nWideChar)
	if nWideChar == 0 {
		Xsqlite3_free(tls, zMbcsText)
		zMbcsText = uintptr(0)
	}
	return zMbcsText
}

// C documentation
//
//	/*
//	** If *pArg is initially negative then this is a query.  Set *pArg to
//	** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
//	**
//	** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
//	*/
func _winModeBit(tls *libc.TLS, pFile uintptr, mask uint8, pArg uintptr) {
	var v1 uintptr
	_ = v1
	if **(**int32)(__ccgo_up(pArg)) < 0 {
		**(**int32)(__ccgo_up(pArg)) = libc.BoolInt32(int32((*TwinFile)(unsafe.Pointer(pFile)).FctrlFlags)&int32(mask) != 0)
	} else {
		if **(**int32)(__ccgo_up(pArg)) == 0 {
			v1 = pFile + 16
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) & ^int32(mask))
		} else {
			v1 = pFile + 16
			*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | int32(mask))
		}
	}
}

// C documentation
//
//	/*
//	** Return the name of the first system call after zName.  If zName==NULL
//	** then return the name of the first system call.  Return NULL if zName
//	** is the last system call or if zName is not the name of a valid
//	** system call.
//	*/
func _winNextSystemCall(tls *libc.TLS, p uintptr, zName uintptr) (r uintptr) {
	var i int32
	_ = i
	i = -int32(1)
	_ = p
	if zName != 0 {
		i = 0
		for {
			if !(i < int32(libc.Uint32FromInt64(972)/libc.Uint32FromInt64(12))-libc.Int32FromInt32(1)) {
				break
			}
			if libc.Xstrcmp(tls, zName, _aSyscall[i].FzName) == 0 {
				break
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	}
	i = i + 1
	for {
		if !(i < int32(libc.Uint32FromInt64(972)/libc.Uint32FromInt64(12))) {
			break
		}
		if _aSyscall[i].FpCurrent != uintptr(0) {
			return _aSyscall[i].FzName
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	return uintptr(0)
}

var _winNolockVfs = Tsqlite3_vfs{
	FiVersion:   int32(3),
	FszOsFile:   int32(64),
	FmxPathname: libc.Int32FromInt32(MAX_PATH) * libc.Int32FromInt32(4),
	FzName:      __ccgo_ts + 5298,
	FpAppData:   uintptr(unsafe.Pointer(&_winNolockAppData)),
}

// C documentation
//
//	/*
//	** Open a file.
//	*/
func _winOpen(tls *libc.TLS, pVfs uintptr, zName uintptr, id uintptr, flags int32, pOutFlags uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var dwCreationDisposition, dwDesiredAccess, dwFlagsAndAttributes, dwShareMode TDWORD
	var h THANDLE
	var isCreate, isDelete, isExclusive, isReadWrite, isReadonly, rc, rc2, rc21 int32
	var pAppData, pFile, zConverted, zUtf8Name, v1 uintptr
	var _ /* cnt at bp+4 */ int32
	var _ /* isRO at bp+8 */ int32
	var _ /* lastErrno at bp+0 */ TDWORD
	var _ /* zTmpname at bp+12 */ uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = dwCreationDisposition, dwDesiredAccess, dwFlagsAndAttributes, dwShareMode, h, isCreate, isDelete, isExclusive, isReadWrite, isReadonly, pAppData, pFile, rc, rc2, rc21, zConverted, zUtf8Name, v1
	**(**TDWORD)(__ccgo_up(bp)) = uint32(0)
	dwFlagsAndAttributes = uint32(0)
	pFile = id        /* Filename in OS encoding */
	zUtf8Name = zName /* Filename in UTF-8 encoding */
	**(**int32)(__ccgo_up(bp + 4)) = 0
	**(**int32)(__ccgo_up(bp + 8)) = 0 /* file is known to be accessible readonly */
	/* If argument zPath is a NULL pointer, this function is required to open
	 ** a temporary file. Use this buffer to store the file name in.
	 */
	**(**uintptr)(__ccgo_up(bp + 12)) = uintptr(0) /* For temporary filename, if necessary. */
	rc = SQLITE_OK                                 /* Function Return Code */
	isExclusive = flags & int32(SQLITE_OPEN_EXCLUSIVE)
	isDelete = flags & int32(SQLITE_OPEN_DELETEONCLOSE)
	isCreate = flags & int32(SQLITE_OPEN_CREATE)
	isReadonly = flags & int32(SQLITE_OPEN_READONLY)
	isReadWrite = flags & int32(SQLITE_OPEN_READWRITE)
	/* Check the following statements are true:
	 **
	 **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
	 **   (b) if CREATE is set, then READWRITE must also be set, and
	 **   (c) if EXCLUSIVE is set, then CREATE must also be set.
	 **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
	 */
	/* The main DB, main journal, WAL file and super-journal are never
	 ** automatically deleted. Nor are they ever temporary files.  */
	/* Assert that the upper layer has set one of the "file-type" flags. */
	libc.Xmemset(tls, pFile, 0, uint32(64))
	(*TwinFile)(unsafe.Pointer(pFile)).Fh = uintptr(int32(-libc.Int32FromInt32(1)))
	/* If the second argument to this function is NULL, generate a
	 ** temporary file name to use
	 */
	if !(zUtf8Name != 0) {
		rc = _winGetTempname(tls, pVfs, bp+12)
		if rc != SQLITE_OK {
			return rc
		}
		zUtf8Name = **(**uintptr)(__ccgo_up(bp + 12))
	}
	/* Database filenames are double-zero terminated if they are not
	 ** URIs with parameters.  Hence, they can always be passed into
	 ** sqlite3_uri_parameter().
	 */
	/* Convert the filename to the system encoding. */
	zConverted = _winConvertFromUtf8Filename(tls, zUtf8Name)
	if zConverted == uintptr(0) {
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 12)))
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
	}
	if _winIsDir(tls, zConverted) != 0 {
		Xsqlite3_free(tls, zConverted)
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 12)))
		return libc.Int32FromInt32(SQLITE_CANTOPEN) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
	}
	if isReadWrite != 0 {
		dwDesiredAccess = libc.Uint32FromUint32(0x80000000) | uint32(libc.Int32FromInt32(0x40000000))
	} else {
		dwDesiredAccess = libc.Uint32FromUint32(0x80000000)
	}
	/* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
	 ** created. SQLite doesn't use it to indicate "exclusive access"
	 ** as it is usually understood.
	 */
	if isExclusive != 0 {
		/* Creates a new file, only if it does not already exist. */
		/* If the file exists, it fails. */
		dwCreationDisposition = uint32(CREATE_NEW)
	} else {
		if isCreate != 0 {
			/* Open existing file, or create if it doesn't exist */
			dwCreationDisposition = uint32(OPEN_ALWAYS)
		} else {
			/* Opens a file, only if it exists. */
			dwCreationDisposition = uint32(OPEN_EXISTING)
		}
	}
	if 0 == Xsqlite3_uri_boolean(tls, zName, __ccgo_ts+5159, 0) {
		dwShareMode = uint32(libc.Int32FromInt32(FILE_SHARE_READ) | libc.Int32FromInt32(FILE_SHARE_WRITE))
	} else {
		dwShareMode = uint32(0)
	}
	if isDelete != 0 {
		dwFlagsAndAttributes = uint32(libc.Int32FromInt32(FILE_ATTRIBUTE_TEMPORARY) | libc.Int32FromInt32(FILE_ATTRIBUTE_HIDDEN) | libc.Int32FromInt32(FILE_FLAG_DELETE_ON_CLOSE))
	} else {
		dwFlagsAndAttributes = uint32(FILE_ATTRIBUTE_NORMAL)
	}
	/* Reports from the internet are that performance is always
	 ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
	if int32(1) != 0 {
		for cond := true; cond; cond = _winRetryIoerr(tls, bp+4, bp) != 0 {
			h = (*(*func(*libc.TLS, TLPCWSTR, TDWORD, TDWORD, TLPSECURITY_ATTRIBUTES, TDWORD, TDWORD, THANDLE) THANDLE)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, zConverted, dwDesiredAccess, dwShareMode, libc.UintptrFromInt32(0), dwCreationDisposition, dwFlagsAndAttributes, libc.UintptrFromInt32(0))
			if h != uintptr(int32(-libc.Int32FromInt32(1))) {
				break
			}
			if isReadWrite != 0 {
				_sqlite3BeginBenignMalloc(tls)
				rc2 = _winAccess(tls, pVfs, zUtf8Name, libc.Int32FromInt32(SQLITE_ACCESS_READ)|libc.Int32FromInt32(NORETRY), bp+8)
				_sqlite3EndBenignMalloc(tls)
				if rc2 == SQLITE_OK && **(**int32)(__ccgo_up(bp + 8)) != 0 {
					break
				}
			}
		}
	} else {
		for cond := true; cond; cond = _winRetryIoerr(tls, bp+4, bp) != 0 {
			h = (*(*func(*libc.TLS, TLPCSTR, TDWORD, TDWORD, TLPSECURITY_ATTRIBUTES, TDWORD, TDWORD, THANDLE) THANDLE)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(4)].FpCurrent})))(tls, zConverted, dwDesiredAccess, dwShareMode, libc.UintptrFromInt32(0), dwCreationDisposition, dwFlagsAndAttributes, libc.UintptrFromInt32(0))
			if h != uintptr(int32(-libc.Int32FromInt32(1))) {
				break
			}
			if isReadWrite != 0 {
				_sqlite3BeginBenignMalloc(tls)
				rc21 = _winAccess(tls, pVfs, zUtf8Name, libc.Int32FromInt32(SQLITE_ACCESS_READ)|libc.Int32FromInt32(NORETRY), bp+8)
				_sqlite3EndBenignMalloc(tls)
				if rc21 == SQLITE_OK && **(**int32)(__ccgo_up(bp + 8)) != 0 {
					break
				}
			}
		}
	}
	_winLogIoerr(tls, **(**int32)(__ccgo_up(bp + 4)), int32(54286))
	if h == uintptr(int32(-libc.Int32FromInt32(1))) {
		Xsqlite3_free(tls, zConverted)
		Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 12)))
		if isReadWrite != 0 && **(**int32)(__ccgo_up(bp + 8)) != 0 && !(isExclusive != 0) {
			return _winOpen(tls, pVfs, zName, id, (flags|int32(SQLITE_OPEN_READONLY)) & ^(libc.Int32FromInt32(SQLITE_OPEN_CREATE)|libc.Int32FromInt32(SQLITE_OPEN_READWRITE)), pOutFlags)
		} else {
			(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = **(**TDWORD)(__ccgo_up(bp))
			_winLogErrorAtLine(tls, int32(SQLITE_CANTOPEN), (*TwinFile)(unsafe.Pointer(pFile)).FlastErrno, __ccgo_ts+5169, zUtf8Name, int32(54301))
			return _sqlite3CantopenError(tls, int32(54302))
		}
	}
	if pOutFlags != 0 {
		if isReadWrite != 0 {
			**(**int32)(__ccgo_up(pOutFlags)) = int32(SQLITE_OPEN_READWRITE)
		} else {
			**(**int32)(__ccgo_up(pOutFlags)) = int32(SQLITE_OPEN_READONLY)
		}
	}
	pAppData = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FpAppData
	Xsqlite3_free(tls, zConverted)
	Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(bp + 12)))
	if pAppData != 0 {
		v1 = (*TwinVfsAppData)(unsafe.Pointer(pAppData)).FpMethod
	} else {
		v1 = uintptr(unsafe.Pointer(&_winIoMethod))
	}
	(*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods = v1
	(*TwinFile)(unsafe.Pointer(pFile)).FpVfs = pVfs
	(*TwinFile)(unsafe.Pointer(pFile)).Fh = h
	if isReadonly != 0 {
		v1 = pFile + 16
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(WINFILE_RDONLY))
	}
	if flags&int32(SQLITE_OPEN_MAIN_DB) != 0 && Xsqlite3_uri_boolean(tls, zName, __ccgo_ts+5177, int32(SQLITE_POWERSAFE_OVERWRITE)) != 0 {
		v1 = pFile + 16
		*(*Tu8)(unsafe.Pointer(v1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(v1))) | libc.Int32FromInt32(WINFILE_PSOW))
	}
	(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = uint32(0)
	(*TwinFile)(unsafe.Pointer(pFile)).FzPath = zName
	(*TwinFile)(unsafe.Pointer(pFile)).FhMap = libc.UintptrFromInt32(0)
	(*TwinFile)(unsafe.Pointer(pFile)).FpMapRegion = uintptr(0)
	(*TwinFile)(unsafe.Pointer(pFile)).FmmapSize = 0
	(*TwinFile)(unsafe.Pointer(pFile)).FmmapSizeMax = _sqlite3Config.FszMmap
	return rc
}

// C documentation
//
//	/*
//	** Open the shared-memory area associated with database file pDbFd.
//	*/
func _winOpenSharedMemory(tls *libc.TLS, pDbFd uintptr) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var nName, rc int32
	var p, pNew, pShmNode uintptr
	var _ /* h at bp+0 */ THANDLE
	_, _, _, _, _ = nName, p, pNew, pShmNode, rc /* The connection to be opened */
	pShmNode = uintptr(0)                        /* The underlying mmapped file */
	rc = SQLITE_OK                               /* Size of zName in bytes */
	/* Not previously opened */
	/* Allocate space for the new sqlite3_shm object.  Also speculatively
	 ** allocate space for a new winShmNode and filename.  */
	p = _sqlite3MallocZero(tls, uint64(20))
	if p == uintptr(0) {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
	}
	nName = _sqlite3Strlen30(tls, (*TwinFile)(unsafe.Pointer(pDbFd)).FzPath)
	pNew = _sqlite3MallocZero(tls, uint64(int64(48)+int64(nName)+int64(17)))
	if pNew == uintptr(0) {
		Xsqlite3_free(tls, p)
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
	}
	(*TwinShmNode)(unsafe.Pointer(pNew)).FzFilename = pNew + 1*48
	(*TwinShmNode)(unsafe.Pointer(pNew)).FhSharedShm = uintptr(int32(-libc.Int32FromInt32(1)))
	(*TwinShmNode)(unsafe.Pointer(pNew)).FisUnlocked = int32(1)
	(*TwinShmNode)(unsafe.Pointer(pNew)).FbUseSharedLockHandle = _winIsUNCPath(tls, (*TwinFile)(unsafe.Pointer(pDbFd)).FzPath)
	Xsqlite3_snprintf(tls, nName+int32(15), (*TwinShmNode)(unsafe.Pointer(pNew)).FzFilename, __ccgo_ts+4966, libc.VaList(bp+16, (*TwinFile)(unsafe.Pointer(pDbFd)).FzPath))
	/* Look to see if there is an existing winShmNode that can be used.
	 ** If no matching winShmNode currently exists, then create a new one.  */
	_winShmEnterMutex(tls)
	pShmNode = _winShmNodeList
	for {
		if !(pShmNode != 0) {
			break
		}
		/* TBD need to come up with better match here.  Perhaps
		 ** use FILE_ID_BOTH_DIR_INFO Structure.  */
		if _sqlite3StrICmp(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).FzFilename, (*TwinShmNode)(unsafe.Pointer(pNew)).FzFilename) == 0 {
			break
		}
		goto _1
	_1:
		;
		pShmNode = (*TwinShmNode)(unsafe.Pointer(pShmNode)).FpNext
	}
	if pShmNode == uintptr(0) {
		pShmNode = pNew
		/* Allocate a mutex for this winShmNode object, if one is required. */
		if _sqlite3Config.FbCoreMutex != 0 {
			(*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex = Xsqlite3_mutex_alloc(tls, SQLITE_MUTEX_FAST)
			if (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex == uintptr(0) {
				rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
			}
		}
		/* Open a file-handle to use for mappings, and for the DMS lock. */
		if rc == SQLITE_OK {
			**(**THANDLE)(__ccgo_up(bp)) = uintptr(int32(-libc.Int32FromInt32(1)))
			(*TwinShmNode)(unsafe.Pointer(pShmNode)).FisReadonly = Xsqlite3_uri_boolean(tls, (*TwinFile)(unsafe.Pointer(pDbFd)).FzPath, __ccgo_ts+4973, 0)
			rc = _winHandleOpen(tls, (*TwinShmNode)(unsafe.Pointer(pNew)).FzFilename, pShmNode+20, bp)
			(*TwinShmNode)(unsafe.Pointer(pShmNode)).FhSharedShm = **(**THANDLE)(__ccgo_up(bp))
		}
		/* If successful, link the new winShmNode into the global list. If an
		 ** error occurred, free the object. */
		if rc == SQLITE_OK {
			(*TwinShmNode)(unsafe.Pointer(pShmNode)).FpNext = _winShmNodeList
			_winShmNodeList = pShmNode
			pNew = uintptr(0)
		} else {
			Xsqlite3_mutex_free(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
			if (*TwinShmNode)(unsafe.Pointer(pShmNode)).FhSharedShm != uintptr(int32(-libc.Int32FromInt32(1))) {
				(*(*func(*libc.TLS, THANDLE) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(3)].FpCurrent})))(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).FhSharedShm)
			}
		}
	}
	/* If no error has occurred, link the winShm object to the winShmNode and
	 ** the winShm to pDbFd.  */
	if rc == SQLITE_OK {
		Xsqlite3_mutex_enter(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
		(*TwinShm)(unsafe.Pointer(p)).FpShmNode = pShmNode
		(*TwinShm)(unsafe.Pointer(p)).FpWinShmNext = (*TwinShmNode)(unsafe.Pointer(pShmNode)).FpWinShmList
		(*TwinShmNode)(unsafe.Pointer(pShmNode)).FpWinShmList = p
		(*TwinFile)(unsafe.Pointer(pDbFd)).FpShm = p
		Xsqlite3_mutex_leave(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
	} else {
		if p != 0 {
			Xsqlite3_free(tls, p)
		}
	}
	_winShmLeaveMutex(tls)
	Xsqlite3_free(tls, pNew)
	/* Open a file-handle on the *-shm file for this connection. This file-handle
	 ** is only used for locking. The mapping of the *-shm file is created using
	 ** the shared file handle in winShmNode.hSharedShm.  */
	if rc == SQLITE_OK && (*TwinShmNode)(unsafe.Pointer(pShmNode)).FbUseSharedLockHandle == 0 {
		(*TwinShm)(unsafe.Pointer(p)).FbReadonly = Xsqlite3_uri_boolean(tls, (*TwinFile)(unsafe.Pointer(pDbFd)).FzPath, __ccgo_ts+4973, 0)
		rc = _winHandleOpen(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).FzFilename, p+12, p+8)
		if rc != SQLITE_OK {
			_winCloseSharedMemory(tls, pDbFd, 0)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Write up to nBuf bytes of randomness into zBuf.
//	*/
func _winRandomness(tls *libc.TLS, pVfs uintptr, nBuf int32, zBuf uintptr) (r int32) {
	bp := tls.Alloc(48)
	defer tls.Free(48)
	var v1 int32
	var _ /* cnt at bp+36 */ TDWORD
	var _ /* e at bp+0 */ TEntropyGatherer
	var _ /* i at bp+40 */ TLARGE_INTEGER
	var _ /* pid at bp+32 */ TDWORD
	var _ /* x at bp+16 */ TSYSTEMTIME
	_ = v1
	_ = pVfs
	libc.Xmemset(tls, zBuf, 0, uint32(nBuf))
	(**(**TEntropyGatherer)(__ccgo_up(bp))).Fa = zBuf
	(**(**TEntropyGatherer)(__ccgo_up(bp))).Fna = nBuf
	(**(**TEntropyGatherer)(__ccgo_up(bp))).FnXor = 0
	(**(**TEntropyGatherer)(__ccgo_up(bp))).Fi = 0
	(*(*func(*libc.TLS, TLPSYSTEMTIME))(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(29)].FpCurrent})))(tls, bp+16)
	_xorMemory(tls, bp, bp+16, int32(16))
	**(**TDWORD)(__ccgo_up(bp + 32)) = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(17)].FpCurrent})))(tls)
	_xorMemory(tls, bp, bp+32, int32(4))
	**(**TDWORD)(__ccgo_up(bp + 36)) = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(33)].FpCurrent})))(tls)
	_xorMemory(tls, bp, bp+36, int32(4))
	(*(*func(*libc.TLS, uintptr) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(51)].FpCurrent})))(tls, bp+40)
	_xorMemory(tls, bp, bp+40, int32(8))
	if (**(**TEntropyGatherer)(__ccgo_up(bp))).FnXor > nBuf {
		v1 = nBuf
	} else {
		v1 = (**(**TEntropyGatherer)(__ccgo_up(bp))).FnXor
	}
	return v1
}

// C documentation
//
//	/*
//	** Read data from a file into a buffer.  Return SQLITE_OK if all
//	** bytes were read successfully and SQLITE_IOERR if anything goes
//	** wrong.
//	*/
func _winRead(tls *libc.TLS, id uintptr, pBuf uintptr, amt int32, offset Tsqlite3_int64) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var nCopy int32
	var pFile uintptr
	var _ /* lastErrno at bp+28 */ TDWORD
	var _ /* nRead at bp+20 */ TDWORD
	var _ /* nRetry at bp+24 */ int32
	var _ /* overlapped at bp+0 */ TOVERLAPPED
	_, _ = nCopy, pFile                 /* The offset for ReadFile. */
	pFile = id                          /* Number of bytes actually read from file */
	**(**int32)(__ccgo_up(bp + 24)) = 0 /* Number of retrys */
	/* Deal with as much of this read request as possible by transferring
	 ** data from the memory mapping using memcpy().  */
	if offset < (*TwinFile)(unsafe.Pointer(pFile)).FmmapSize {
		if offset+int64(amt) <= (*TwinFile)(unsafe.Pointer(pFile)).FmmapSize {
			libc.Xmemcpy(tls, pBuf, (*TwinFile)(unsafe.Pointer(pFile)).FpMapRegion+uintptr(offset), uint32(amt))
			return SQLITE_OK
		} else {
			nCopy = int32((*TwinFile)(unsafe.Pointer(pFile)).FmmapSize - offset)
			libc.Xmemcpy(tls, pBuf, (*TwinFile)(unsafe.Pointer(pFile)).FpMapRegion+uintptr(offset), uint32(nCopy))
			pBuf = pBuf + uintptr(nCopy)
			amt = amt - nCopy
			offset = offset + int64(nCopy)
		}
	}
	libc.Xmemset(tls, bp, 0, uint32(20))
	(**(**TOVERLAPPED)(__ccgo_up(bp))).F__ccgo2_8.F__ccgo0_0.FOffset = uint32(int32(offset & libc.Int64FromUint32(0xffffffff)))
	(**(**TOVERLAPPED)(__ccgo_up(bp))).F__ccgo2_8.F__ccgo0_0.FOffsetHigh = uint32(int32(offset >> libc.Int32FromInt32(32) & libc.Int64FromInt32(0x7fffffff)))
	for !((*(*func(*libc.TLS, THANDLE, TLPVOID, TDWORD, TLPDWORD, TLPOVERLAPPED) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(52)].FpCurrent})))(tls, (*TwinFile)(unsafe.Pointer(pFile)).Fh, pBuf, uint32(amt), bp+20, bp) != 0) && (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls) != uint32(38) {
		if _winRetryIoerr(tls, bp+24, bp+28) != 0 {
			continue
		}
		(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = **(**TDWORD)(__ccgo_up(bp + 28))
		return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(1)<<libc.Int32FromInt32(8), (*TwinFile)(unsafe.Pointer(pFile)).FlastErrno, __ccgo_ts+4854, (*TwinFile)(unsafe.Pointer(pFile)).FzPath, int32(51616))
	}
	_winLogIoerr(tls, **(**int32)(__ccgo_up(bp + 24)), int32(51619))
	if **(**TDWORD)(__ccgo_up(bp + 20)) < uint32(amt) {
		/* Unread parts of the buffer must be zero-filled */
		libc.Xmemset(tls, pBuf+uintptr(**(**TDWORD)(__ccgo_up(bp + 20))), 0, uint32(uint32(amt)-**(**TDWORD)(__ccgo_up(bp + 20))))
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(2)<<libc.Int32FromInt32(8)
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** This is the xSetSystemCall() method of sqlite3_vfs for all of the
//	** "win32" VFSes.  Return SQLITE_OK upon successfully updating the
//	** system call pointer, or SQLITE_NOTFOUND if there is no configurable
//	** system call named zName.
//	*/
func _winSetSystemCall(tls *libc.TLS, pNotUsed uintptr, zName uintptr, __ccgo_fp_pNewFunc Tsqlite3_syscall_ptr) (r int32) {
	var i uint32
	var rc int32
	_, _ = i, rc
	rc = int32(SQLITE_NOTFOUND)
	_ = pNotUsed
	if zName == uintptr(0) {
		/* If no zName is given, restore all system calls to their default
		 ** settings and return NULL
		 */
		rc = SQLITE_OK
		i = uint32(0)
		for {
			if !(i < libc.Uint32FromInt64(972)/libc.Uint32FromInt64(12)) {
				break
			}
			if _aSyscall[i].FpDefault != 0 {
				_aSyscall[i].FpCurrent = _aSyscall[i].FpDefault
			}
			goto _1
		_1:
			;
			i = i + 1
		}
	} else {
		/* If zName is specified, operate on only the one system call
		 ** specified.
		 */
		i = uint32(0)
		for {
			if !(i < libc.Uint32FromInt64(972)/libc.Uint32FromInt64(12)) {
				break
			}
			if libc.Xstrcmp(tls, zName, _aSyscall[i].FzName) == 0 {
				if _aSyscall[i].FpDefault == uintptr(0) {
					_aSyscall[i].FpDefault = _aSyscall[i].FpCurrent
				}
				rc = SQLITE_OK
				if __ccgo_fp_pNewFunc == uintptr(0) {
					__ccgo_fp_pNewFunc = _aSyscall[i].FpDefault
				}
				_aSyscall[i].FpCurrent = __ccgo_fp_pNewFunc
				break
			}
			goto _2
		_2:
			;
			i = i + 1
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Change the lock state for a shared-memory segment.
//	*/
func _winShmLock(tls *libc.TLS, fd uintptr, ofst int32, n int32, flags int32) (r int32) {
	var bExcl, rc, v2 int32
	var h THANDLE
	var mask Tu16
	var nMs TDWORD
	var p, pDbFd, pShm, pShm1, pShmNode uintptr
	_, _, _, _, _, _, _, _, _, _, _ = bExcl, h, mask, nMs, p, pDbFd, pShm, pShm1, pShmNode, rc, v2
	pDbFd = fd /* Connection holding shared memory */
	p = (*TwinFile)(unsafe.Pointer(pDbFd)).FpShm
	rc = SQLITE_OK                                                                     /* Result code */
	mask = uint16(libc.Uint32FromUint32(1)<<(ofst+n) - libc.Uint32FromUint32(1)<<ofst) /* Mask of locks to [un]take */
	if p == uintptr(0) {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(20)<<libc.Int32FromInt32(8)
	}
	pShmNode = (*TwinShm)(unsafe.Pointer(p)).FpShmNode
	if pShmNode == uintptr(0) {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(20)<<libc.Int32FromInt32(8)
	}
	/* Check that, if this to be a blocking lock, no locks that occur later
	 ** in the following list than the lock being obtained are already held:
	 **
	 **   1. Recovery lock (ofst==2).
	 **   2. Checkpointer lock (ofst==1).
	 **   3. Write lock (ofst==0).
	 **   4. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
	 **
	 ** In other words, if this is a blocking lock, none of the locks that
	 ** occur later in the above list than the lock being obtained may be
	 ** held.
	 */
	/* Check if there is any work to do. There are three cases:
	 **
	 **    a) An unlock operation where there are locks to unlock,
	 **    b) An shared lock where the requested lock is not already held
	 **    c) An exclusive lock where the requested lock is not already held
	 **
	 ** The SQLite core never requests an exclusive lock that it already holds.
	 ** This is assert()ed immediately below.  */
	if flags&int32(SQLITE_SHM_UNLOCK) != 0 && (int32((*TwinShm)(unsafe.Pointer(p)).FexclMask)|int32((*TwinShm)(unsafe.Pointer(p)).FsharedMask))&int32(mask) != 0 || flags == libc.Int32FromInt32(SQLITE_SHM_SHARED)|libc.Int32FromInt32(SQLITE_SHM_LOCK) && 0 == int32((*TwinShm)(unsafe.Pointer(p)).FsharedMask)&int32(mask) || flags == libc.Int32FromInt32(SQLITE_SHM_EXCLUSIVE)|libc.Int32FromInt32(SQLITE_SHM_LOCK) {
		h = (*TwinShm)(unsafe.Pointer(p)).FhShm
		if flags&int32(SQLITE_SHM_UNLOCK) != 0 {
			/* Case (a) - unlock.  */
			if (*TwinShmNode)(unsafe.Pointer(pShmNode)).FbUseSharedLockHandle != 0 {
				h = (*TwinShmNode)(unsafe.Pointer(pShmNode)).FhSharedShm
				if flags&int32(SQLITE_SHM_SHARED) != 0 {
					Xsqlite3_mutex_enter(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
					pShm = (*TwinShmNode)(unsafe.Pointer(pShmNode)).FpWinShmList
					for {
						if !(pShm != 0) {
							break
						}
						if pShm != p && int32((*TwinShm)(unsafe.Pointer(pShm)).FsharedMask)&int32(mask) != 0 {
							/* Another connection within this process is also holding this
							 ** SHARED lock. So do not actually release the OS lock.  */
							h = uintptr(int32(-libc.Int32FromInt32(1)))
							break
						}
						goto _1
					_1:
						;
						pShm = (*TwinShm)(unsafe.Pointer(pShm)).FpWinShmNext
					}
					Xsqlite3_mutex_leave(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
				}
			}
			if h != uintptr(int32(-libc.Int32FromInt32(1))) {
				rc = _winHandleUnlock(tls, h, ofst+(libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4), n)
			}
			/* If successful, also clear the bits in sharedMask/exclMask */
			if rc == SQLITE_OK {
				(*TwinShm)(unsafe.Pointer(p)).FexclMask = uint16(int32((*TwinShm)(unsafe.Pointer(p)).FexclMask) & ^int32(mask))
				(*TwinShm)(unsafe.Pointer(p)).FsharedMask = uint16(int32((*TwinShm)(unsafe.Pointer(p)).FsharedMask) & ^int32(mask))
			}
		} else {
			if flags&int32(SQLITE_SHM_EXCLUSIVE) != 0 {
				v2 = int32(1)
			} else {
				v2 = 0
			}
			bExcl = v2
			nMs = uint32(0)
			if (*TwinShmNode)(unsafe.Pointer(pShmNode)).FbUseSharedLockHandle != 0 {
				h = (*TwinShmNode)(unsafe.Pointer(pShmNode)).FhSharedShm
				Xsqlite3_mutex_enter(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
				pShm1 = (*TwinShmNode)(unsafe.Pointer(pShmNode)).FpWinShmList
				for {
					if !(pShm1 != 0) {
						break
					}
					if bExcl != 0 {
						if (int32((*TwinShm)(unsafe.Pointer(pShm1)).FsharedMask)|int32((*TwinShm)(unsafe.Pointer(pShm1)).FexclMask))&int32(mask) != 0 {
							rc = int32(SQLITE_BUSY)
							h = uintptr(int32(-libc.Int32FromInt32(1)))
						}
					} else {
						if int32((*TwinShm)(unsafe.Pointer(pShm1)).FsharedMask)&int32(mask) != 0 {
							h = uintptr(int32(-libc.Int32FromInt32(1)))
						} else {
							if int32((*TwinShm)(unsafe.Pointer(pShm1)).FexclMask)&int32(mask) != 0 {
								rc = int32(SQLITE_BUSY)
								h = uintptr(int32(-libc.Int32FromInt32(1)))
							}
						}
					}
					goto _3
				_3:
					;
					pShm1 = (*TwinShm)(unsafe.Pointer(pShm1)).FpWinShmNext
				}
				Xsqlite3_mutex_leave(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
			}
			if h != uintptr(int32(-libc.Int32FromInt32(1))) {
				rc = _winHandleLockTimeout(tls, h, uint32(ofst+(libc.Int32FromInt32(22)+libc.Int32FromInt32(SQLITE_SHM_NLOCK))*libc.Int32FromInt32(4)), uint32(n), bExcl, nMs)
			}
			if rc == SQLITE_OK {
				if bExcl != 0 {
					(*TwinShm)(unsafe.Pointer(p)).FexclMask = uint16(int32((*TwinShm)(unsafe.Pointer(p)).FexclMask) | int32(mask))
				} else {
					(*TwinShm)(unsafe.Pointer(p)).FsharedMask = uint16(int32((*TwinShm)(unsafe.Pointer(p)).FsharedMask) | int32(mask))
				}
			}
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** This function is called to obtain a pointer to region iRegion of the
//	** shared-memory associated with the database file fd. Shared-memory regions
//	** are numbered starting from zero. Each shared-memory region is szRegion
//	** bytes in size.
//	**
//	** If an error occurs, an error code is returned and *pp is set to NULL.
//	**
//	** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
//	** region has not been allocated (by any client, including one running in a
//	** separate process), then *pp is set to NULL and SQLITE_OK returned. If
//	** isWrite is non-zero and the requested shared-memory region has not yet
//	** been allocated, it is allocated by this function.
//	**
//	** If the shared-memory region has already been allocated or is allocated by
//	** this call as described above, then it is mapped into this processes
//	** address space (if it is not already), *pp is set to point to the mapped
//	** memory and SQLITE_OK returned.
//	*/
func _winShmMap(tls *libc.TLS, fd uintptr, iRegion int32, szRegion int32, isWrite int32, pp uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var apNew, p, pDbFd, pMap, pShm, pShmNode uintptr
	var flags, protect TDWORD
	var hMap, hShared THANDLE
	var iOffset, iOffset1, nByte Ti64
	var iOffsetShift, iOffsetShift1, rc int32
	var _ /* sz at bp+0 */ Tsqlite3_int64
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = apNew, flags, hMap, hShared, iOffset, iOffset1, iOffsetShift, iOffsetShift1, nByte, p, pDbFd, pMap, pShm, pShmNode, protect, rc
	pDbFd = fd
	pShm = (*TwinFile)(unsafe.Pointer(pDbFd)).FpShm
	protect = uint32(PAGE_READWRITE)
	flags = uint32(libc.Int32FromInt32(SECTION_MAP_WRITE) | libc.Int32FromInt32(SECTION_MAP_READ))
	rc = SQLITE_OK
	if !(pShm != 0) {
		rc = _winOpenSharedMemory(tls, pDbFd)
		if rc != SQLITE_OK {
			return rc
		}
		pShm = (*TwinFile)(unsafe.Pointer(pDbFd)).FpShm
	}
	pShmNode = (*TwinShm)(unsafe.Pointer(pShm)).FpShmNode
	Xsqlite3_mutex_enter(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
	if (*TwinShmNode)(unsafe.Pointer(pShmNode)).FisUnlocked != 0 {
		/* Take the DMS lock. */
		rc = _winLockSharedMemory(tls, pShmNode, uint32(0))
		if rc != SQLITE_OK {
			goto shmpage_out
		}
	}
	if (*TwinShmNode)(unsafe.Pointer(pShmNode)).FnRegion <= iRegion {
		hShared = (*TwinShmNode)(unsafe.Pointer(pShmNode)).FhSharedShm /* New aRegion[] array */
		nByte = (int64(iRegion) + int64(1)) * int64(szRegion)          /* Current size of wal-index file */
		(*TwinShmNode)(unsafe.Pointer(pShmNode)).FszRegion = szRegion
		/* The requested region is not mapped into this processes address space.
		 ** Check to see if it has been allocated (i.e. if the wal-index file is
		 ** large enough to contain the requested region).
		 */
		rc = _winHandleSize(tls, hShared, bp)
		if rc != SQLITE_OK {
			rc = _winLogErrorAtLine(tls, rc, (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls), __ccgo_ts+4986, (*TwinFile)(unsafe.Pointer(pDbFd)).FzPath, int32(53402))
			goto shmpage_out
		}
		if **(**Tsqlite3_int64)(__ccgo_up(bp)) < nByte {
			/* The requested memory region does not exist. If isWrite is set to
			 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
			 **
			 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
			 ** the requested memory region.  */
			if !(isWrite != 0) {
				goto shmpage_out
			}
			rc = _winHandleTruncate(tls, hShared, nByte)
			if rc != SQLITE_OK {
				rc = _winLogErrorAtLine(tls, rc, (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls), __ccgo_ts+4997, (*TwinFile)(unsafe.Pointer(pDbFd)).FzPath, int32(53415))
				goto shmpage_out
			}
		}
		/* Map the requested memory region into this processes address space. */
		apNew = Xsqlite3_realloc64(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).FaRegion, uint64((int64(iRegion)+int64(1))*int64(8)))
		if !(apNew != 0) {
			rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(12)<<libc.Int32FromInt32(8)
			goto shmpage_out
		}
		(*TwinShmNode)(unsafe.Pointer(pShmNode)).FaRegion = apNew
		if (*TwinShmNode)(unsafe.Pointer(pShmNode)).FisReadonly != 0 {
			protect = uint32(PAGE_READONLY)
			flags = uint32(SECTION_MAP_READ)
		}
		for (*TwinShmNode)(unsafe.Pointer(pShmNode)).FnRegion <= iRegion {
			hMap = libc.UintptrFromInt32(0) /* file-mapping handle */
			pMap = uintptr(0)               /* Mapped memory region */
			hMap = (*(*func(*libc.TLS, THANDLE, TLPSECURITY_ATTRIBUTES, TDWORD, TDWORD, TDWORD, TLPCWSTR) THANDLE)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, hShared, libc.UintptrFromInt32(0), protect, uint32(0), uint32(nByte), libc.UintptrFromInt32(0))
			if hMap != 0 {
				iOffset = int64((*TwinShmNode)(unsafe.Pointer(pShmNode)).FnRegion * szRegion)
				iOffsetShift = int32(iOffset % int64(_winSysInfo.FdwAllocationGranularity))
				pMap = (*(*func(*libc.TLS, THANDLE, TDWORD, TDWORD, TDWORD, TSIZE_T) TLPVOID)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(49)].FpCurrent})))(tls, hMap, flags, uint32(0), uint32(iOffset-int64(iOffsetShift)), uint32(int64(szRegion)+int64(iOffsetShift)))
			}
			if !(pMap != 0) {
				(*TwinShmNode)(unsafe.Pointer(pShmNode)).FlastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
				rc = _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(21)<<libc.Int32FromInt32(8), (*TwinShmNode)(unsafe.Pointer(pShmNode)).FlastErrno, __ccgo_ts+5008, (*TwinFile)(unsafe.Pointer(pDbFd)).FzPath, int32(53459))
				if hMap != 0 {
					(*(*func(*libc.TLS, THANDLE) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(3)].FpCurrent})))(tls, hMap)
				}
				goto shmpage_out
			}
			(**(**TShmRegion)(__ccgo_up((*TwinShmNode)(unsafe.Pointer(pShmNode)).FaRegion + uintptr((*TwinShmNode)(unsafe.Pointer(pShmNode)).FnRegion)*8))).FpMap = pMap
			(**(**TShmRegion)(__ccgo_up((*TwinShmNode)(unsafe.Pointer(pShmNode)).FaRegion + uintptr((*TwinShmNode)(unsafe.Pointer(pShmNode)).FnRegion)*8))).FhMap = hMap
			(*TwinShmNode)(unsafe.Pointer(pShmNode)).FnRegion = (*TwinShmNode)(unsafe.Pointer(pShmNode)).FnRegion + 1
		}
	}
	goto shmpage_out
shmpage_out:
	;
	if (*TwinShmNode)(unsafe.Pointer(pShmNode)).FnRegion > iRegion {
		iOffset1 = int64(iRegion) * int64(szRegion)
		iOffsetShift1 = int32(iOffset1 % int64(_winSysInfo.FdwAllocationGranularity))
		p = (**(**TShmRegion)(__ccgo_up((*TwinShmNode)(unsafe.Pointer(pShmNode)).FaRegion + uintptr(iRegion)*8))).FpMap
		**(**uintptr)(__ccgo_up(pp)) = p + uintptr(iOffsetShift1)
	} else {
		**(**uintptr)(__ccgo_up(pp)) = uintptr(0)
	}
	if (*TwinShmNode)(unsafe.Pointer(pShmNode)).FisReadonly != 0 && rc == SQLITE_OK {
		rc = int32(SQLITE_READONLY)
	}
	Xsqlite3_mutex_leave(tls, (*TwinShmNode)(unsafe.Pointer(pShmNode)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Purge the winShmNodeList list of all entries with winShmNode.pWinShmList==0.
//	**
//	** This is not a VFS shared-memory method; it is a utility function called
//	** by VFS shared-memory methods.
//	*/
func _winShmPurge(tls *libc.TLS, pVfs uintptr, deleteFlag int32) {
	var bRc TBOOL
	var i int32
	var p, pp, v1 uintptr
	_, _, _, _, _ = bRc, i, p, pp, v1
	pp = uintptr(unsafe.Pointer(&_winShmNodeList))
	for {
		v1 = **(**uintptr)(__ccgo_up(pp))
		p = v1
		if !(v1 != uintptr(0)) {
			break
		}
		if (*TwinShmNode)(unsafe.Pointer(p)).FpWinShmList == uintptr(0) {
			if (*TwinShmNode)(unsafe.Pointer(p)).Fmutex != 0 {
				Xsqlite3_mutex_free(tls, (*TwinShmNode)(unsafe.Pointer(p)).Fmutex)
			}
			i = 0
			for {
				if !(i < (*TwinShmNode)(unsafe.Pointer(p)).FnRegion) {
					break
				}
				bRc = (*(*func(*libc.TLS, TLPCVOID) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(59)].FpCurrent})))(tls, (**(**TShmRegion)(__ccgo_up((*TwinShmNode)(unsafe.Pointer(p)).FaRegion + uintptr(i)*8))).FpMap)
				_ = bRc
				bRc = (*(*func(*libc.TLS, THANDLE) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(3)].FpCurrent})))(tls, (**(**TShmRegion)(__ccgo_up((*TwinShmNode)(unsafe.Pointer(p)).FaRegion + uintptr(i)*8))).FhMap)
				_ = bRc
				goto _2
			_2:
				;
				i = i + 1
			}
			_winHandleClose(tls, (*TwinShmNode)(unsafe.Pointer(p)).FhSharedShm)
			if deleteFlag != 0 {
				_sqlite3BeginBenignMalloc(tls)
				_winDelete(tls, pVfs, (*TwinShmNode)(unsafe.Pointer(p)).FzFilename, 0)
				_sqlite3EndBenignMalloc(tls)
			}
			**(**uintptr)(__ccgo_up(pp)) = (*TwinShmNode)(unsafe.Pointer(p)).FpNext
			Xsqlite3_free(tls, (*TwinShmNode)(unsafe.Pointer(p)).FaRegion)
			Xsqlite3_free(tls, p)
		} else {
			pp = p + 44
		}
	}
}

// C documentation
//
//	/*
//	** Make sure all writes to a particular file are committed to disk.
//	*/
func _winSync(tls *libc.TLS, id uintptr, flags int32) (r int32) {
	var pFile uintptr
	var rc TBOOL
	_, _ = pFile, rc
	/*
	 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
	 ** OSTRACE() macros.
	 */
	pFile = id
	/* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
	/* Unix cannot, but some systems may return SQLITE_FULL from here. This
	 ** line is to test that doing so does not cause any problems.
	 */
	_ = flags
	/* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
	 ** no-op
	 */
	if (*TwinFile)(unsafe.Pointer(pFile)).FpMapRegion != 0 {
		if (*(*func(*libc.TLS, TLPCVOID, TSIZE_T) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(71)].FpCurrent})))(tls, (*TwinFile)(unsafe.Pointer(pFile)).FpMapRegion, uint32(0)) != 0 {
		} else {
			(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
			return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(24)<<libc.Int32FromInt32(8), (*TwinFile)(unsafe.Pointer(pFile)).FlastErrno, __ccgo_ts+4908, (*TwinFile)(unsafe.Pointer(pFile)).FzPath, int32(51939))
		}
	}
	rc = (*(*func(*libc.TLS, THANDLE) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(13)].FpCurrent})))(tls, (*TwinFile)(unsafe.Pointer(pFile)).Fh)
	if rc != 0 {
		return SQLITE_OK
	} else {
		(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
		return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(4)<<libc.Int32FromInt32(8), (*TwinFile)(unsafe.Pointer(pFile)).FlastErrno, __ccgo_ts+4917, (*TwinFile)(unsafe.Pointer(pFile)).FzPath, int32(51954))
	}
	return r
}

// C documentation
//
//	/*
//	** Lower the locking level on file descriptor id to locktype.  locktype
//	** must be either NO_LOCK or SHARED_LOCK.
//	**
//	** If the locking level of the file descriptor is already at or below
//	** the requested locking level, this routine is a no-op.
//	**
//	** It is not possible for this routine to fail if the second argument
//	** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
//	** might return SQLITE_IOERR;
//	*/
func _winUnlock(tls *libc.TLS, id uintptr, locktype int32) (r int32) {
	var pFile uintptr
	var rc, type1 int32
	_, _, _ = pFile, rc, type1
	pFile = id
	rc = SQLITE_OK
	type1 = int32((*TwinFile)(unsafe.Pointer(pFile)).Flocktype)
	if type1 >= int32(EXCLUSIVE_LOCK) {
		_winUnlockFile(tls, pFile+8, uint32(_sqlite3PendingByte+libc.Int32FromInt32(2)), uint32(0), uint32(SHARED_SIZE), uint32(0))
		if locktype == int32(SHARED_LOCK) && !(_winGetReadLock(tls, pFile, 0) != 0) {
			/* This should never happen.  We should always be able to
			 ** reacquire the read lock */
			rc = _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(8)<<libc.Int32FromInt32(8), (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls), __ccgo_ts+4956, (*TwinFile)(unsafe.Pointer(pFile)).FzPath, int32(52318))
		}
	}
	if type1 >= int32(RESERVED_LOCK) {
		_winUnlockFile(tls, pFile+8, uint32(_sqlite3PendingByte+libc.Int32FromInt32(1)), uint32(0), uint32(1), uint32(0))
	}
	if locktype == NO_LOCK && type1 >= int32(SHARED_LOCK) {
		_winUnlockReadLock(tls, pFile)
	}
	if type1 >= int32(PENDING_LOCK) {
		_winUnlockFile(tls, pFile+8, uint32(_sqlite3PendingByte), uint32(0), uint32(1), uint32(0))
	}
	(*TwinFile)(unsafe.Pointer(pFile)).Flocktype = uint8(locktype)
	return rc
}

/******************************************************************************
****************************** No-op Locking **********************************
**
** Of the various locking implementations available, this is by far the
** simplest:  locking is ignored.  No attempt is made to lock the database
** file for reading or writing.
**
** This locking mode is appropriate for use on read-only databases
** (ex: databases that are burned into CD-ROM, for example.)  It can
** also be used if the application employs some external mechanism to
** prevent simultaneous access of the same database by two or more
** database connections.  But there is a serious risk of database
** corruption if this locking mode is used in situations where multiple
** database connections are accessing the same database file at the same
** time and one or more of those connections are writing.
 */

// C documentation
//
//	/*
//	** Unlock a file region.
//	 */
func _winUnlockFile(tls *libc.TLS, phFile TLPHANDLE, offsetLow TDWORD, offsetHigh TDWORD, numBytesLow TDWORD, numBytesHigh TDWORD) (r TBOOL) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var _ /* ovlp at bp+0 */ TOVERLAPPED
	if int32(1) != 0 {
		libc.Xmemset(tls, bp, 0, uint32(20))
		(**(**TOVERLAPPED)(__ccgo_up(bp))).F__ccgo2_8.F__ccgo0_0.FOffset = offsetLow
		(**(**TOVERLAPPED)(__ccgo_up(bp))).F__ccgo2_8.F__ccgo0_0.FOffsetHigh = offsetHigh
		return (*(*func(*libc.TLS, THANDLE, TDWORD, TDWORD, TDWORD, TLPOVERLAPPED) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(58)].FpCurrent})))(tls, **(**THANDLE)(__ccgo_up(phFile)), uint32(0), numBytesLow, numBytesHigh, bp)
	} else {
		return (*(*func(*libc.TLS, THANDLE, TDWORD, TDWORD, TDWORD, TDWORD) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(57)].FpCurrent})))(tls, **(**THANDLE)(__ccgo_up(phFile)), offsetLow, offsetHigh, numBytesLow, numBytesHigh)
	}
	return r
}

// C documentation
//
//	/*
//	** Undo a readlock
//	*/
func _winUnlockReadLock(tls *libc.TLS, pFile uintptr) (r int32) {
	var lastErrno, v1 TDWORD
	var res int32
	var v2 bool
	_, _, _, _ = lastErrno, res, v1, v2
	if int32(1) != 0 {
		res = _winUnlockFile(tls, pFile+8, uint32(_sqlite3PendingByte+libc.Int32FromInt32(2)), uint32(0), uint32(SHARED_SIZE), uint32(0))
	} else {
		res = _winUnlockFile(tls, pFile+8, uint32(_sqlite3PendingByte+int32(2)+int32((*TwinFile)(unsafe.Pointer(pFile)).FsharedLockByte)), uint32(0), uint32(1), uint32(0))
	}
	if v2 = res == 0; v2 {
		v1 = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
		lastErrno = v1
	}
	if v2 && v1 != uint32(158) {
		(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = lastErrno
		_winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(8)<<libc.Int32FromInt32(8), (*TwinFile)(unsafe.Pointer(pFile)).FlastErrno, __ccgo_ts+4938, (*TwinFile)(unsafe.Pointer(pFile)).FzPath, int32(52076))
	}
	return res
}

// C documentation
//
//	/*
//	** Convert a UTF-8 string to Microsoft Unicode.
//	**
//	** Space to hold the returned string is obtained from sqlite3_malloc().
//	*/
func _winUtf8ToUnicode(tls *libc.TLS, zText uintptr) (r TLPWSTR) {
	var nChar int32
	var zWideText TLPWSTR
	_, _ = nChar, zWideText
	nChar = (*(*func(*libc.TLS, TUINT, TDWORD, TLPCSTR, int32, TLPWSTR, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(50)].FpCurrent})))(tls, uint32(CP_UTF8), uint32(0), zText, -int32(1), libc.UintptrFromInt32(0), 0)
	if nChar == 0 {
		return uintptr(0)
	}
	zWideText = _sqlite3MallocZero(tls, uint64(uint32(nChar)*uint32(2)))
	if zWideText == uintptr(0) {
		return uintptr(0)
	}
	nChar = (*(*func(*libc.TLS, TUINT, TDWORD, TLPCSTR, int32, TLPWSTR, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(50)].FpCurrent})))(tls, uint32(CP_UTF8), uint32(0), zText, -int32(1), zWideText, nChar)
	if nChar == 0 {
		Xsqlite3_free(tls, zWideText)
		zWideText = uintptr(0)
	}
	return zWideText
}

var _winVfs = Tsqlite3_vfs{
	FiVersion:   int32(3),
	FszOsFile:   int32(64),
	FmxPathname: libc.Int32FromInt32(MAX_PATH) * libc.Int32FromInt32(4),
	FzName:      __ccgo_ts + 5277,
	FpAppData:   uintptr(unsafe.Pointer(&_winAppData)),
}

// C documentation
//
//	/*
//	** Write data from a buffer into a file.  Return SQLITE_OK on success
//	** or some other error code on failure.
//	*/
func _winWrite(tls *libc.TLS, id uintptr, pBuf uintptr, amt int32, offset Tsqlite3_int64) (r int32) {
	bp := tls.Alloc(32)
	defer tls.Free(32)
	var aRem, pFile uintptr
	var nRem, rc int32
	var _ /* lastErrno at bp+28 */ TDWORD
	var _ /* nRetry at bp+0 */ int32
	var _ /* nWrite at bp+24 */ TDWORD
	var _ /* overlapped at bp+4 */ TOVERLAPPED
	_, _, _, _ = aRem, nRem, pFile, rc
	rc = 0                         /* True if error has occurred, else false */
	pFile = id                     /* File handle */
	**(**int32)(__ccgo_up(bp)) = 0 /* Number of retries */
	/* The offset for WriteFile. */
	aRem = pBuf                                  /* Data yet to be written */
	nRem = amt                                   /* Bytes written by each WriteFile() call */
	**(**TDWORD)(__ccgo_up(bp + 28)) = uint32(0) /* Value returned by GetLastError() */
	libc.Xmemset(tls, bp+4, 0, uint32(20))
	(**(**TOVERLAPPED)(__ccgo_up(bp + 4))).F__ccgo2_8.F__ccgo0_0.FOffset = uint32(int32(offset & libc.Int64FromUint32(0xffffffff)))
	(**(**TOVERLAPPED)(__ccgo_up(bp + 4))).F__ccgo2_8.F__ccgo0_0.FOffsetHigh = uint32(int32(offset >> libc.Int32FromInt32(32) & libc.Int64FromInt32(0x7fffffff)))
	for nRem > 0 {
		if !((*(*func(*libc.TLS, THANDLE, TLPCVOID, TDWORD, TLPDWORD, TLPOVERLAPPED) TBOOL)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(61)].FpCurrent})))(tls, (*TwinFile)(unsafe.Pointer(pFile)).Fh, aRem, uint32(nRem), bp+24, bp+4) != 0) {
			if _winRetryIoerr(tls, bp, bp+28) != 0 {
				continue
			}
			break
		}
		if **(**TDWORD)(__ccgo_up(bp + 24)) == uint32(0) || **(**TDWORD)(__ccgo_up(bp + 24)) > uint32(nRem) {
			**(**TDWORD)(__ccgo_up(bp + 28)) = (*(*func(*libc.TLS) TDWORD)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls)
			break
		}
		offset = offset + int64(**(**TDWORD)(__ccgo_up(bp + 24)))
		(**(**TOVERLAPPED)(__ccgo_up(bp + 4))).F__ccgo2_8.F__ccgo0_0.FOffset = uint32(int32(offset & libc.Int64FromUint32(0xffffffff)))
		(**(**TOVERLAPPED)(__ccgo_up(bp + 4))).F__ccgo2_8.F__ccgo0_0.FOffsetHigh = uint32(int32(offset >> libc.Int32FromInt32(32) & libc.Int64FromInt32(0x7fffffff)))
		aRem = aRem + uintptr(**(**TDWORD)(__ccgo_up(bp + 24)))
		nRem = int32(uint32(nRem) - **(**TDWORD)(__ccgo_up(bp + 24)))
	}
	if nRem > 0 {
		(*TwinFile)(unsafe.Pointer(pFile)).FlastErrno = **(**TDWORD)(__ccgo_up(bp + 28))
		rc = int32(1)
	}
	if rc != 0 {
		if (*TwinFile)(unsafe.Pointer(pFile)).FlastErrno == uint32(39) || (*TwinFile)(unsafe.Pointer(pFile)).FlastErrno == uint32(112) {
			return _winLogErrorAtLine(tls, int32(SQLITE_FULL), (*TwinFile)(unsafe.Pointer(pFile)).FlastErrno, __ccgo_ts+4862, (*TwinFile)(unsafe.Pointer(pFile)).FzPath, int32(51728))
		}
		return _winLogErrorAtLine(tls, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(3)<<libc.Int32FromInt32(8), (*TwinFile)(unsafe.Pointer(pFile)).FlastErrno, __ccgo_ts+4872, (*TwinFile)(unsafe.Pointer(pFile)).FzPath, int32(51733))
	} else {
		_winLogIoerr(tls, **(**int32)(__ccgo_up(bp)), int32(51736))
	}
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Generate VM code to invoke either xStep() (if bInverse is 0) or
//	** xInverse (if bInverse is non-zero) for each window function in the
//	** linked list starting at pMWin. Or, for built-in window functions
//	** that do not use the standard function API, generate the required
//	** inline VM code.
//	**
//	** If argument csr is greater than or equal to 0, then argument reg is
//	** the first register in an array of registers guaranteed to be large
//	** enough to hold the array of arguments for each function. In this case
//	** the arguments are extracted from the current row of csr into the
//	** array of registers before invoking OP_AggStep or OP_AggInverse
//	**
//	** Or, if csr is less than zero, then the array of registers at reg is
//	** already populated with all columns from the current row of the sub-query.
//	**
//	** If argument regPartSize is non-zero, then it is a register containing the
//	** number of rows in the current partition.
//	*/
func _windowAggStep(tls *libc.TLS, p uintptr, pMWin uintptr, csr int32, bInverse int32, reg int32) {
	var addrIf, addrIsNull, i, iEnd, iOp, nArg, regArg, regTmp, v2 int32
	var pColl, pFunc, pOp, pParse, pWin, v uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrIf, addrIsNull, i, iEnd, iOp, nArg, pColl, pFunc, pOp, pParse, pWin, regArg, regTmp, v, v2
	pParse = (*TWindowCodeArg)(unsafe.Pointer(p)).FpParse
	v = _sqlite3GetVdbe(tls, pParse)
	pWin = pMWin
	for {
		if !(pWin != 0) {
			break
		}
		pFunc = (*TWindow)(unsafe.Pointer(pWin)).FpWFunc
		if (*TWindow)(unsafe.Pointer(pWin)).FbExprArgs != 0 {
			v2 = 0
		} else {
			v2 = _windowArgCount(tls, pWin)
		}
		nArg = v2
		addrIf = 0
		/* All OVER clauses in the same window function aggregate step must
		 ** be the same. */
		i = 0
		for {
			if !(i < nArg) {
				break
			}
			if i != int32(1) || (*TFuncDef)(unsafe.Pointer(pFunc)).FzName != uintptr(unsafe.Pointer(&_nth_valueName)) {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), csr, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol+i, reg+i)
			} else {
				_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol+i, reg+i)
			}
			goto _3
		_3:
			;
			i = i + 1
		}
		regArg = reg
		if (*TWindow)(unsafe.Pointer(pWin)).FpFilter != 0 {
			regTmp = _sqlite3GetTempReg(tls, pParse)
			_sqlite3VdbeAddOp3(tls, v, int32(OP_Column), csr, (*TWindow)(unsafe.Pointer(pWin)).FiArgCol+nArg, regTmp)
			addrIf = _sqlite3VdbeAddOp3(tls, v, int32(OP_IfNot), regTmp, 0, int32(1))
			_sqlite3ReleaseTempReg(tls, pParse, regTmp)
		}
		if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid == 0 && (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(SQLITE_FUNC_MINMAX) != 0 && int32((*TWindow)(unsafe.Pointer(pWin)).FeStart) != int32(TK_UNBOUNDED) {
			addrIsNull = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), regArg)
			if bInverse == 0 {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(1), int32(1))
				_sqlite3VdbeAddOp2(tls, v, int32(OP_SCopy), regArg, (*TWindow)(unsafe.Pointer(pWin)).FregApp)
				_sqlite3VdbeAddOp3(tls, v, int32(OP_MakeRecord), (*TWindow)(unsafe.Pointer(pWin)).FregApp, int32(2), (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(2))
				_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(2))
			} else {
				_sqlite3VdbeAddOp4Int(tls, v, int32(OP_SeekGE), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp, 0, regArg, int32(1))
				_sqlite3VdbeAddOp1(tls, v, int32(OP_Delete), (*TWindow)(unsafe.Pointer(pWin)).FcsrApp)
				_sqlite3VdbeJumpHere(tls, v, _sqlite3VdbeCurrentAddr(tls, v)-int32(2))
			}
			_sqlite3VdbeJumpHere(tls, v, addrIsNull)
		} else {
			if (*TWindow)(unsafe.Pointer(pWin)).FregApp != 0 {
				_sqlite3VdbeAddOp2(tls, v, int32(OP_AddImm), (*TWindow)(unsafe.Pointer(pWin)).FregApp+int32(1)-bInverse, int32(1))
			} else {
				if (*TFuncDef)(unsafe.Pointer(pFunc)).FxSFunc != __ccgo_fp(_noopStepFunc) {
					if (*TWindow)(unsafe.Pointer(pWin)).FbExprArgs != 0 {
						iOp = _sqlite3VdbeCurrentAddr(tls, v)
						nArg = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)))).FnExpr
						regArg = _sqlite3GetTempRange(tls, pParse, nArg)
						_sqlite3ExprCodeExprList(tls, pParse, *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)), regArg, 0, uint8(0))
						iEnd = _sqlite3VdbeCurrentAddr(tls, v)
						for {
							if !(iOp < iEnd) {
								break
							}
							pOp = _sqlite3VdbeGetOp(tls, v, iOp)
							if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_Column) && (*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 == (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr {
								(*TVdbeOp)(unsafe.Pointer(pOp)).Fp1 = csr
							}
							goto _4
						_4:
							;
							iOp = iOp + 1
						}
					}
					if (*TFuncDef)(unsafe.Pointer(pFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 {
						pColl = _sqlite3ExprNNCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 20)) + 8))).FpExpr)
						_sqlite3VdbeAddOp4(tls, v, int32(OP_CollSeq), 0, 0, 0, pColl, -int32(2))
					}
					if bInverse != 0 {
						v2 = int32(OP_AggInverse)
					} else {
						v2 = int32(OP_AggStep)
					}
					_sqlite3VdbeAddOp3(tls, v, v2, bInverse, regArg, (*TWindow)(unsafe.Pointer(pWin)).FregAccum)
					_sqlite3VdbeAppendP4(tls, v, pFunc, -int32(8))
					_sqlite3VdbeChangeP5(tls, v, uint16(nArg))
					if (*TWindow)(unsafe.Pointer(pWin)).FbExprArgs != 0 {
						_sqlite3ReleaseTempRange(tls, pParse, regArg, nArg)
					}
				}
			}
		}
		if addrIf != 0 {
			_sqlite3VdbeJumpHere(tls, v, addrIf)
		}
		goto _1
	_1:
		;
		pWin = (*TWindow)(unsafe.Pointer(pWin)).FpNextWin
	}
}

/*
** Values that may be passed as the second argument to windowCodeOp().
 */

// C documentation
//
//	/*
//	** This function is called as part of generating VM programs for RANGE
//	** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
//	** the ORDER BY term in the window, and that argument op is OP_Ge, it generates
//	** code equivalent to:
//	**
//	**   if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
//	**
//	** The value of parameter op may also be OP_Gt or OP_Le. In these cases the
//	** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
//	**
//	** If the sort-order for the ORDER BY term in the window is DESC, then the
//	** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is
//	** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
//	** ">" becomes "<", and so on. So, with DESC sort order, if the argument op
//	** is OP_Ge, the generated code is equivalent to:
//	**
//	**   if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
//	**
//	** A special type of arithmetic is used such that if csr1.peerVal is not
//	** a numeric type (real or integer), then the result of the addition
//	** or subtraction is a a copy of csr1.peerVal.
//	*/
func _windowCodeRangeTest(tls *libc.TLS, p uintptr, op int32, csr1 int32, regVal int32, csr2 int32, lbl int32) {
	var addr, addrDone, addrGe, arith, reg1, reg2, regString, v1 int32
	var pColl, pOrderBy, pParse, v, v2 uintptr
	_, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrDone, addrGe, arith, pColl, pOrderBy, pParse, reg1, reg2, regString, v, v1, v2
	pParse = (*TWindowCodeArg)(unsafe.Pointer(p)).FpParse
	v = _sqlite3GetVdbe(tls, pParse)
	pOrderBy = (*TWindow)(unsafe.Pointer((*TWindowCodeArg)(unsafe.Pointer(p)).FpMWin)).FpOrderBy /* ORDER BY clause for window */
	reg1 = _sqlite3GetTempReg(tls, pParse)                                                       /* Reg. for csr1.peerVal+regVal */
	reg2 = _sqlite3GetTempReg(tls, pParse)
	v2 = pParse + 48
	*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
	v1 = *(*int32)(unsafe.Pointer(v2)) /* Reg. for csr2.peerVal */
	regString = v1                     /* Reg. for constant value '' */
	arith = int32(OP_Add)              /* Jump destination */
	addrDone = _sqlite3VdbeMakeLabel(tls, pParse)
	/* Read the peer-value from each cursor into a register */
	_windowReadPeerValues(tls, p, csr1, reg1)
	_windowReadPeerValues(tls, p, csr2, reg2)
	if int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_DESC) != 0 {
		switch op {
		case int32(OP_Ge):
			op = int32(OP_Le)
		case int32(OP_Gt):
			op = int32(OP_Lt)
		default:
			op = int32(OP_Ge)
			break
		}
		arith = int32(OP_Subtract)
	}
	/* If the BIGNULL flag is set for the ORDER BY, then it is required to
	 ** consider NULL values to be larger than all other values, instead of
	 ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this
	 ** (and adding that capability causes a performance regression), so
	 ** instead if the BIGNULL flag is set then cases where either reg1 or
	 ** reg2 are NULL are handled separately in the following block. The code
	 ** generated is equivalent to:
	 **
	 **   if( reg1 IS NULL ){
	 **     if( op==OP_Ge ) goto lbl;
	 **     if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl;
	 **     if( op==OP_Le && reg2 IS NULL ) goto lbl;
	 **   }else if( reg2 IS NULL ){
	 **     if( op==OP_Le ) goto lbl;
	 **   }
	 **
	 ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is
	 ** not taken, control jumps over the comparison operator coded below this
	 ** block.  */
	if int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 {
		/* This block runs if reg1 contains a NULL. */
		addr = _sqlite3VdbeAddOp1(tls, v, int32(OP_NotNull), reg1)
		switch op {
		case int32(OP_Ge):
			_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, lbl)
		case int32(OP_Gt):
			_sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), reg2, lbl)
		case int32(OP_Le):
			_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), reg2, lbl)
		default: /* no-op */
			break
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Goto), 0, addrDone)
		/* This block runs if reg1 is not NULL, but reg2 is. */
		_sqlite3VdbeJumpHere(tls, v, addr)
		if op == int32(OP_Gt) || op == int32(OP_Ge) {
			v1 = addrDone
		} else {
			v1 = lbl
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), reg2, v1)
	}
	/* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
	 ** This block adds (or subtracts for DESC) the numeric value in regVal
	 ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob),
	 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
	 **
	 **   if( reg1>='' ) goto addrGe;
	 **   reg1 = reg1 +/- regVal
	 **   addrGe:
	 **
	 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
	 ** the add/subtract is skipped for these, as required. If reg1 is a NULL,
	 ** then the arithmetic is performed, but since adding or subtracting from
	 ** NULL is always NULL anyway, this case is handled as required too.  */
	_sqlite3VdbeAddOp4(tls, v, int32(OP_String8), 0, regString, 0, __ccgo_ts+1711, -int32(1))
	addrGe = _sqlite3VdbeAddOp3(tls, v, int32(OP_Ge), regString, 0, reg1)
	if op == int32(OP_Ge) && arith == int32(OP_Add) || op == int32(OP_Le) && arith == int32(OP_Subtract) {
		_sqlite3VdbeAddOp3(tls, v, op, reg2, lbl, reg1)
	}
	_sqlite3VdbeAddOp3(tls, v, arith, regVal, reg1, reg1)
	_sqlite3VdbeJumpHere(tls, v, addrGe)
	/* Compare registers reg2 and reg1, taking the jump if required. Note that
	 ** control skips over this test if the BIGNULL flag is set and either
	 ** reg1 or reg2 contain a NULL value.  */
	_sqlite3VdbeAddOp3(tls, v, op, reg2, lbl, reg1)
	pColl = _sqlite3ExprNNCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8))).FpExpr)
	_sqlite3VdbeAppendP4(tls, v, pColl, -int32(2))
	_sqlite3VdbeChangeP5(tls, v, uint16(SQLITE_NULLEQ))
	_sqlite3VdbeResolveLabel(tls, v, addrDone)
	_sqlite3ReleaseTempReg(tls, pParse, reg1)
	_sqlite3ReleaseTempReg(tls, pParse, reg2)
}

// C documentation
//
//	/*
//	** The journal file must be open when this routine is called. A journal
//	** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
//	** current location.
//	**
//	** The format for the journal header is as follows:
//	** - 8 bytes: Magic identifying journal format.
//	** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
//	** - 4 bytes: Random number used for page hash.
//	** - 4 bytes: Initial database page count.
//	** - 4 bytes: Sector size used by the process that wrote this journal.
//	** - 4 bytes: Database page size.
//	**
//	** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
//	*/
func _writeJournalHdr(tls *libc.TLS, pPager uintptr) (r int32) {
	var ii, rc int32
	var nHeader, nWrite Tu32
	var zHeader uintptr
	var v2 Ti64
	_, _, _, _, _, _ = ii, nHeader, nWrite, rc, zHeader, v2
	rc = SQLITE_OK                                                /* Return code */
	zHeader = (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace        /* Temporary space used to build header */
	nHeader = uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize) /* Loop counter */
	/* Journal file must be open. */
	if nHeader > (*TPager)(unsafe.Pointer(pPager)).FsectorSize {
		nHeader = (*TPager)(unsafe.Pointer(pPager)).FsectorSize
	}
	/* If there are active savepoints and any of them were created
	 ** since the most recent journal header was written, update the
	 ** PagerSavepoint.iHdrOffset fields now.
	 */
	ii = 0
	for {
		if !(ii < (*TPager)(unsafe.Pointer(pPager)).FnSavepoint) {
			break
		}
		if (**(**TPagerSavepoint)(__ccgo_up((*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(ii)*48))).FiHdrOffset == 0 {
			(**(**TPagerSavepoint)(__ccgo_up((*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(ii)*48))).FiHdrOffset = (*TPager)(unsafe.Pointer(pPager)).FjournalOff
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	v2 = _journalHdrOffset(tls, pPager)
	(*TPager)(unsafe.Pointer(pPager)).FjournalOff = v2
	(*TPager)(unsafe.Pointer(pPager)).FjournalHdr = v2
	/*
	 ** Write the nRec Field - the number of page records that follow this
	 ** journal header. Normally, zero is written to this value at this time.
	 ** After the records are added to the journal (and the journal synced,
	 ** if in full-sync mode), the zero is overwritten with the true number
	 ** of records (see syncJournal()).
	 **
	 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
	 ** reading the journal this value tells SQLite to assume that the
	 ** rest of the journal file contains valid page records. This assumption
	 ** is dangerous, as if a failure occurred whilst writing to the journal
	 ** file it may contain some garbage data. There are two scenarios
	 ** where this risk can be ignored:
	 **
	 **   * When the pager is in no-sync mode. Corruption can follow a
	 **     power failure in this case anyway.
	 **
	 **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
	 **     that garbage data is never appended to the journal file.
	 */
	if (*TPager)(unsafe.Pointer(pPager)).FnoSync != 0 || int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_MEMORY) || _sqlite3OsDeviceCharacteristics(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd)&int32(SQLITE_IOCAP_SAFE_APPEND) != 0 {
		libc.Xmemcpy(tls, zHeader, uintptr(unsafe.Pointer(&_aJournalMagic)), uint32(8))
		_sqlite3Put4byte(tls, zHeader+uintptr(8), uint32(0xffffffff))
	} else {
		libc.Xmemset(tls, zHeader, 0, libc.Uint32FromInt64(8)+libc.Uint32FromInt32(4))
	}
	/* The random check-hash initializer */
	if int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) != int32(PAGER_JOURNALMODE_MEMORY) {
		Xsqlite3_randomness(tls, int32(4), pPager+52)
	}
	_sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(4)), (*TPager)(unsafe.Pointer(pPager)).FcksumInit)
	/* The initial database size */
	_sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(8)), (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize)
	/* The assumed sector size for this process */
	_sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(12)), (*TPager)(unsafe.Pointer(pPager)).FsectorSize)
	/* The page size */
	_sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(16)), uint32((*TPager)(unsafe.Pointer(pPager)).FpageSize))
	/* Initializing the tail of the buffer is not necessary.  Everything
	 ** works find if the following memset() is omitted.  But initializing
	 ** the memory prevents valgrind from complaining, so we are willing to
	 ** take the performance hit.
	 */
	libc.Xmemset(tls, zHeader+uintptr(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(20)), 0, nHeader-(libc.Uint32FromInt64(8)+libc.Uint32FromInt32(20)))
	/* In theory, it is only necessary to write the 28 bytes that the
	 ** journal header consumes to the journal file here. Then increment the
	 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
	 ** record is written to the following sector (leaving a gap in the file
	 ** that will be implicitly filled in by the OS).
	 **
	 ** However it has been discovered that on some systems this pattern can
	 ** be significantly slower than contiguously writing data to the file,
	 ** even if that means explicitly writing data to the block of
	 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
	 ** is done.
	 **
	 ** The loop is required here in case the sector-size is larger than the
	 ** database page size. Since the zHeader buffer is only Pager.pageSize
	 ** bytes in size, more than one call to sqlite3OsWrite() may be required
	 ** to populate the entire journal header sector.
	 */
	nWrite = uint32(0)
	for {
		if !(rc == SQLITE_OK && nWrite < (*TPager)(unsafe.Pointer(pPager)).FsectorSize) {
			break
		}
		rc = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, zHeader, int32(nHeader), (*TPager)(unsafe.Pointer(pPager)).FjournalOff)
		**(**Ti64)(__ccgo_up(pPager + 80)) += int64(nHeader)
		goto _3
	_3:
		;
		nWrite = nWrite + nHeader
	}
	return rc
}

// C documentation
//
//	/*
//	** Write the supplied super-journal name into the journal file for pager
//	** pPager at the current location. The super-journal name must be the last
//	** thing written to a journal file. If the pager is in full-sync mode, the
//	** journal file descriptor is advanced to the next sector boundary before
//	** anything is written. The format is:
//	**
//	**   + 4 bytes: PAGER_SJ_PGNO.
//	**   + N bytes: super-journal filename in utf-8.
//	**   + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
//	**   + 4 bytes: super-journal name checksum.
//	**   + 8 bytes: aJournalMagic[].
//	**
//	** The super-journal page checksum is the sum of the bytes in the super-journal
//	** name, where each byte is interpreted as a signed 8-bit integer.
//	**
//	** If zSuper is a NULL pointer (occurs for a single database transaction),
//	** this call is a no-op.
//	*/
func _writeSuperJournal(tls *libc.TLS, pPager uintptr, zSuper uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var cksum Tu32
	var iHdrOff Ti64
	var nSuper, rc, v2, v3, v5, v7, v9 int32
	var v10, v4, v6, v8 bool
	var _ /* jrnlSize at bp+0 */ Ti64
	_, _, _, _, _, _, _, _, _, _, _, _, _ = cksum, iHdrOff, nSuper, rc, v10, v2, v3, v4, v5, v6, v7, v8, v9 /* Size of journal file on disk */
	cksum = uint32(0)                                                                                       /* Checksum of string zSuper */
	if !(zSuper != 0) || int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) == int32(PAGER_JOURNALMODE_MEMORY) || !((*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != libc.UintptrFromInt32(0)) {
		return SQLITE_OK
	}
	(*TPager)(unsafe.Pointer(pPager)).FsetSuper = uint8(1)
	/* Calculate the length in bytes and the checksum of zSuper */
	nSuper = 0
	for {
		if !(**(**int8)(__ccgo_up(zSuper + uintptr(nSuper))) != 0) {
			break
		}
		cksum = cksum + uint32(**(**int8)(__ccgo_up(zSuper + uintptr(nSuper))))
		goto _1
	_1:
		;
		nSuper = nSuper + 1
	}
	/* If in full-sync mode, advance to the next disk sector before writing
	 ** the super-journal name. This is in case the previous page written to
	 ** the journal has already been synced.
	 */
	if (*TPager)(unsafe.Pointer(pPager)).FfullSync != 0 {
		(*TPager)(unsafe.Pointer(pPager)).FjournalOff = _journalHdrOffset(tls, pPager)
	}
	iHdrOff = (*TPager)(unsafe.Pointer(pPager)).FjournalOff
	/* Write the super-journal data to the end of the journal file. If
	 ** an error occurs, return the error code to the caller.
	 */
	v2 = _write32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff, (*TPager)(unsafe.Pointer(pPager)).FlckPgno)
	rc = v2
	if v4 = 0 != v2; !v4 {
		v3 = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, zSuper, nSuper, iHdrOff+int64(4))
		rc = v3
	}
	if v6 = v4 || 0 != v3; !v6 {
		v5 = _write32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(4)+int64(nSuper), uint32(nSuper))
		rc = v5
	}
	if v8 = v6 || 0 != v5; !v8 {
		v7 = _write32bits(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, iHdrOff+int64(4)+int64(nSuper)+int64(4), cksum)
		rc = v7
	}
	if v10 = v8 || 0 != v7; !v10 {
		v9 = _sqlite3OsWrite(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, uintptr(unsafe.Pointer(&_aJournalMagic)), int32(8), iHdrOff+int64(4)+int64(nSuper)+int64(8))
		rc = v9
	}
	if v10 || 0 != v9 {
		return rc
	}
	**(**Ti64)(__ccgo_up(pPager + 80)) += int64(nSuper + libc.Int32FromInt32(20))
	/* If the pager is in persistent-journal mode, then the physical
	 ** journal-file may extend past the end of the super-journal name
	 ** and 8 bytes of magic data just written to the file. This is
	 ** dangerous because the code to rollback a hot-journal file
	 ** will not be able to find the super-journal name to determine
	 ** whether or not the journal is hot.
	 **
	 ** Easiest thing to do in this scenario is to truncate the journal
	 ** file to the required size.
	 */
	v2 = _sqlite3OsFileSize(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, bp)
	rc = v2
	if SQLITE_OK == v2 && **(**Ti64)(__ccgo_up(bp)) > (*TPager)(unsafe.Pointer(pPager)).FjournalOff {
		rc = _sqlite3OsTruncate(tls, (*TPager)(unsafe.Pointer(pPager)).Fjfd, (*TPager)(unsafe.Pointer(pPager)).FjournalOff)
	}
	return rc
}

// C documentation
//
//	/*
//	** Check to see if index pSrc is compatible as a source of data
//	** for index pDest in an insert transfer optimization.  The rules
//	** for a compatible index:
//	**
//	**    *   The index is over the same set of columns
//	**    *   The same DESC and ASC markings occurs on all columns
//	**    *   The same onError processing (OE_Abort, OE_Ignore, etc)
//	**    *   The same collating sequence on each column
//	**    *   The index has the exact same WHERE clause
//	*/
func _xferCompatibleIndex(tls *libc.TLS, pDest uintptr, pSrc uintptr) (r int32) {
	var i int32
	_ = i
	if int32((*TIndex)(unsafe.Pointer(pDest)).FnKeyCol) != int32((*TIndex)(unsafe.Pointer(pSrc)).FnKeyCol) || int32((*TIndex)(unsafe.Pointer(pDest)).FnColumn) != int32((*TIndex)(unsafe.Pointer(pSrc)).FnColumn) {
		return 0 /* Different number of columns */
	}
	if int32((*TIndex)(unsafe.Pointer(pDest)).FonError) != int32((*TIndex)(unsafe.Pointer(pSrc)).FonError) {
		return 0 /* Different conflict resolution strategies */
	}
	i = 0
	for {
		if !(i < int32((*TIndex)(unsafe.Pointer(pSrc)).FnKeyCol)) {
			break
		}
		if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pSrc)).FaiColumn + uintptr(i)*2))) != int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pDest)).FaiColumn + uintptr(i)*2))) {
			return 0 /* Different columns indexed */
		}
		if int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pSrc)).FaiColumn + uintptr(i)*2))) == -int32(2) {
			if _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pSrc)).FaColExpr + 8 + uintptr(i)*20))).FpExpr, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pDest)).FaColExpr + 8 + uintptr(i)*20))).FpExpr, -int32(1)) != 0 {
				return 0 /* Different expressions in the index */
			}
		}
		if int32(**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pSrc)).FaSortOrder + uintptr(i)))) != int32(**(**Tu8)(__ccgo_up((*TIndex)(unsafe.Pointer(pDest)).FaSortOrder + uintptr(i)))) {
			return 0 /* Different sort orders */
		}
		if Xsqlite3_stricmp(tls, **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pSrc)).FazColl + uintptr(i)*4)), **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pDest)).FazColl + uintptr(i)*4))) != 0 {
			return 0 /* Different collating sequences */
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if _sqlite3ExprCompare(tls, uintptr(0), (*TIndex)(unsafe.Pointer(pSrc)).FpPartIdxWhere, (*TIndex)(unsafe.Pointer(pDest)).FpPartIdxWhere, -int32(1)) != 0 {
		return 0 /* Different WHERE clauses */
	}
	/* If no test above fails then the indices must be compatible */
	return int32(1)
}

// C documentation
//
//	/*
//	** Attempt the transfer optimization on INSERTs of the form
//	**
//	**     INSERT INTO tab1 SELECT * FROM tab2;
//	**
//	** The xfer optimization transfers raw records from tab2 over to tab1.
//	** Columns are not decoded and reassembled, which greatly improves
//	** performance.  Raw index records are transferred in the same way.
//	**
//	** The xfer optimization is only attempted if tab1 and tab2 are compatible.
//	** There are lots of rules for determining compatibility - see comments
//	** embedded in the code for details.
//	**
//	** This routine returns TRUE if the optimization is guaranteed to be used.
//	** Sometimes the xfer optimization will only work if the destination table
//	** is empty - a factor that can only be determined at run-time.  In that
//	** case, this routine generates code for the xfer optimization but also
//	** does a test to see if the destination table is empty and jumps over the
//	** xfer optimization code if the test fails.  In that case, this routine
//	** returns FALSE so that the caller will know to go ahead and generate
//	** an unoptimized transfer.  This routine also returns FALSE if there
//	** is no chance that the xfer optimization can be applied.
//	**
//	** This optimization is particularly useful at making VACUUM run faster.
//	*/
func _xferOptimization(tls *libc.TLS, pParse uintptr, pDest uintptr, pSelect uintptr, onError int32, iDbDest int32) (r int32) {
	var addr1, addr2, destHasUniqueIdx, emptyDestTest, emptySrcTest, i, iDbSrc, iDest, iSrc, regAutoinc, regData, regRowid, v4 int32
	var db, pDestCol, pDestExpr, pDestIdx, pEList, pItem, pSrc, pSrcCol, pSrcExpr, pSrcIdx, v, zColl, v5 uintptr
	var idxInsFlags, insFlags Tu8
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr1, addr2, db, destHasUniqueIdx, emptyDestTest, emptySrcTest, i, iDbSrc, iDest, iSrc, idxInsFlags, insFlags, pDestCol, pDestExpr, pDestIdx, pEList, pItem, pSrc, pSrcCol, pSrcExpr, pSrcIdx, regAutoinc, regData, regRowid, v, zColl, v4, v5
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb /* Loop addresses */
	emptyDestTest = 0                          /* Address of test for empty pDest */
	emptySrcTest = 0                           /* Memory register used by AUTOINC */
	destHasUniqueIdx = 0                       /* Registers holding data and rowid */
	if (*TParse)(unsafe.Pointer(pParse)).FpWith != 0 || (*TSelect)(unsafe.Pointer(pSelect)).FpWith != 0 {
		/* Do not attempt to process this query if there are an WITH clauses
		 ** attached to it. Proceeding may generate a false "no such table: xxx"
		 ** error if pSelect reads from a CTE named "xxx".  */
		return 0
	}
	if int32((*TTable)(unsafe.Pointer(pDest)).FeTabType) == int32(TABTYP_VTAB) {
		return 0 /* tab1 must not be a virtual table */
	}
	if onError == int32(OE_Default) {
		if int32((*TTable)(unsafe.Pointer(pDest)).FiPKey) >= 0 {
			onError = int32((*TTable)(unsafe.Pointer(pDest)).FkeyConf)
		}
		if onError == int32(OE_Default) {
			onError = int32(OE_Abort)
		}
	}
	/* allocated even if there is no FROM clause */
	if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc)).FnSrc != int32(1) {
		return 0 /* FROM clause must have exactly one term */
	}
	if int32(*(*uint32)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8 + 12 + 4))&0x4>>2) != 0 {
		return 0 /* FROM clause cannot contain a subquery */
	}
	if (*TSelect)(unsafe.Pointer(pSelect)).FpWhere != 0 {
		return 0 /* SELECT may not have a WHERE clause */
	}
	if (*TSelect)(unsafe.Pointer(pSelect)).FpOrderBy != 0 {
		return 0 /* SELECT may not have an ORDER BY clause */
	}
	/* Do not need to test for a HAVING clause.  If HAVING is present but
	 ** there is no ORDER BY, we will get an error. */
	if (*TSelect)(unsafe.Pointer(pSelect)).FpGroupBy != 0 {
		return 0 /* SELECT may not have a GROUP BY clause */
	}
	if (*TSelect)(unsafe.Pointer(pSelect)).FpLimit != 0 {
		return 0 /* SELECT may not have a LIMIT clause */
	}
	if (*TSelect)(unsafe.Pointer(pSelect)).FpPrior != 0 {
		return 0 /* SELECT may not be a compound query */
	}
	if (*TSelect)(unsafe.Pointer(pSelect)).FselFlags&uint32(SF_Distinct) != 0 {
		return 0 /* SELECT may not be DISTINCT */
	}
	pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList
	if (*TExprList)(unsafe.Pointer(pEList)).FnExpr != int32(1) {
		return 0 /* The result set must have exactly one column */
	}
	if int32((*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pEList + 8))).FpExpr)).Fop) != int32(TK_ASTERISK) {
		return 0 /* The result set must be the special operator "*" */
	}
	/* At this point we have established that the statement is of the
	 ** correct syntactic form to participate in this optimization.  Now
	 ** we have to check the semantics.
	 */
	pItem = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8
	pSrc = _sqlite3LocateTableItem(tls, pParse, uint32(0), pItem)
	if pSrc == uintptr(0) {
		return 0 /* FROM clause does not contain a real table */
	}
	if (*TTable)(unsafe.Pointer(pSrc)).Ftnum == (*TTable)(unsafe.Pointer(pDest)).Ftnum && (*TTable)(unsafe.Pointer(pSrc)).FpSchema == (*TTable)(unsafe.Pointer(pDest)).FpSchema {
		/* Possible due to bad sqlite_schema.rootpage */
		return 0 /* tab1 and tab2 may not be the same table */
	}
	if libc.BoolInt32((*TTable)(unsafe.Pointer(pDest)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0)) != libc.BoolInt32((*TTable)(unsafe.Pointer(pSrc)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0)) {
		return 0 /* source and destination must both be WITHOUT ROWID or not */
	}
	if !(int32((*TTable)(unsafe.Pointer(pSrc)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) {
		return 0 /* tab2 may not be a view or virtual table */
	}
	if int32((*TTable)(unsafe.Pointer(pDest)).FnCol) != int32((*TTable)(unsafe.Pointer(pSrc)).FnCol) {
		return 0 /* Number of columns must be the same in tab1 and tab2 */
	}
	if int32((*TTable)(unsafe.Pointer(pDest)).FiPKey) != int32((*TTable)(unsafe.Pointer(pSrc)).FiPKey) {
		return 0 /* Both tables must have the same INTEGER PRIMARY KEY */
	}
	if (*TTable)(unsafe.Pointer(pDest)).FtabFlags&uint32(TF_Strict) != uint32(0) && (*TTable)(unsafe.Pointer(pSrc)).FtabFlags&uint32(TF_Strict) == uint32(0) {
		return 0 /* Cannot feed from a non-strict into a strict table */
	}
	i = 0
	for {
		if !(i < int32((*TTable)(unsafe.Pointer(pDest)).FnCol)) {
			break
		}
		pDestCol = (*TTable)(unsafe.Pointer(pDest)).FaCol + uintptr(i)*12
		pSrcCol = (*TTable)(unsafe.Pointer(pSrc)).FaCol + uintptr(i)*12
		/* Even if tables t1 and t2 have identical schemas, if they contain
		 ** generated columns, then this statement is semantically incorrect:
		 **
		 **     INSERT INTO t2 SELECT * FROM t1;
		 **
		 ** The reason is that generated column values are returned by the
		 ** the SELECT statement on the right but the INSERT statement on the
		 ** left wants them to be omitted.
		 **
		 ** Nevertheless, this is a useful notational shorthand to tell SQLite
		 ** to do a bulk transfer all of the content from t1 over to t2.
		 **
		 ** We could, in theory, disable this (except for internal use by the
		 ** VACUUM command where it is actually needed).  But why do that?  It
		 ** seems harmless enough, and provides a useful service.
		 */
		if int32((*TColumn)(unsafe.Pointer(pDestCol)).FcolFlags)&int32(COLFLAG_GENERATED) != int32((*TColumn)(unsafe.Pointer(pSrcCol)).FcolFlags)&int32(COLFLAG_GENERATED) {
			return 0 /* Both columns have the same generated-column type */
		}
		/* But the transfer is only allowed if both the source and destination
		 ** tables have the exact same expressions for generated columns.
		 ** This requirement could be relaxed for VIRTUAL columns, I suppose.
		 */
		if int32((*TColumn)(unsafe.Pointer(pDestCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 {
			if _sqlite3ExprCompare(tls, uintptr(0), _sqlite3ColumnExpr(tls, pSrc, pSrcCol), _sqlite3ColumnExpr(tls, pDest, pDestCol), -int32(1)) != 0 {
				return 0 /* Different generator expressions */
			}
		}
		if int32((*TColumn)(unsafe.Pointer(pDestCol)).Faffinity) != int32((*TColumn)(unsafe.Pointer(pSrcCol)).Faffinity) {
			return 0 /* Affinity must be the same on all columns */
		}
		if Xsqlite3_stricmp(tls, _sqlite3ColumnColl(tls, pDestCol), _sqlite3ColumnColl(tls, pSrcCol)) != 0 {
			return 0 /* Collating sequence must be the same on all columns */
		}
		if int32(uint32(*(*uint8)(unsafe.Pointer(pDestCol + 4))&0xf>>0)) != 0 && !(int32(uint32(*(*uint8)(unsafe.Pointer(pSrcCol + 4))&0xf>>0)) != 0) {
			return 0 /* tab2 must be NOT NULL if tab1 is */
		}
		/* Default values for second and subsequent columns need to match. */
		if int32((*TColumn)(unsafe.Pointer(pDestCol)).FcolFlags)&int32(COLFLAG_GENERATED) == 0 && i > 0 {
			pDestExpr = _sqlite3ColumnExpr(tls, pDest, pDestCol)
			pSrcExpr = _sqlite3ColumnExpr(tls, pSrc, pSrcCol)
			if libc.BoolInt32(pDestExpr == uintptr(0)) != libc.BoolInt32(pSrcExpr == uintptr(0)) || pDestExpr != uintptr(0) && libc.Xstrcmp(tls, *(*uintptr)(unsafe.Pointer(pDestExpr + 8)), *(*uintptr)(unsafe.Pointer(pSrcExpr + 8))) != 0 {
				return 0 /* Default values must be the same for all columns */
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	pDestIdx = (*TTable)(unsafe.Pointer(pDest)).FpIndex
	for {
		if !(pDestIdx != 0) {
			break
		}
		if int32((*TIndex)(unsafe.Pointer(pDestIdx)).FonError) != OE_None {
			destHasUniqueIdx = int32(1)
		}
		pSrcIdx = (*TTable)(unsafe.Pointer(pSrc)).FpIndex
		for {
			if !(pSrcIdx != 0) {
				break
			}
			if _xferCompatibleIndex(tls, pDestIdx, pSrcIdx) != 0 {
				break
			}
			goto _3
		_3:
			;
			pSrcIdx = (*TIndex)(unsafe.Pointer(pSrcIdx)).FpNext
		}
		if pSrcIdx == uintptr(0) {
			return 0 /* pDestIdx has no corresponding index in pSrc */
		}
		if (*TIndex)(unsafe.Pointer(pSrcIdx)).Ftnum == (*TIndex)(unsafe.Pointer(pDestIdx)).Ftnum && (*TTable)(unsafe.Pointer(pSrc)).FpSchema == (*TTable)(unsafe.Pointer(pDest)).FpSchema && _sqlite3FaultSim(tls, int32(411)) == SQLITE_OK {
			/* The sqlite3FaultSim() call allows this corruption test to be
			 ** bypassed during testing, in order to exercise other corruption tests
			 ** further downstream. */
			return 0 /* Corrupt schema - two indexes on the same btree */
		}
		goto _2
	_2:
		;
		pDestIdx = (*TIndex)(unsafe.Pointer(pDestIdx)).FpNext
	}
	if (*TTable)(unsafe.Pointer(pDest)).FpCheck != 0 && (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) && _sqlite3ExprListCompare(tls, (*TTable)(unsafe.Pointer(pSrc)).FpCheck, (*TTable)(unsafe.Pointer(pDest)).FpCheck, -int32(1)) != 0 {
		return 0 /* Tables have different CHECK constraints.  Ticket #2252 */
	}
	/* Disallow the transfer optimization if the destination table contains
	 ** any foreign key constraints.  This is more restrictive than necessary.
	 ** But the main beneficiary of the transfer optimization is the VACUUM
	 ** command, and the VACUUM command disables foreign key constraints.  So
	 ** the extra complication to make this rule less restrictive is probably
	 ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
	 */
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != uint64(0) && (*(*struct {
		FaddColOffset int32
		FpFKey        uintptr
		FpDfltList    uintptr
	})(unsafe.Pointer(pDest + 44))).FpFKey != uintptr(0) {
		return 0
	}
	if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&(uint64(libc.Int32FromInt32(0x00001))<<libc.Int32FromInt32(32)) != uint64(0) {
		return 0 /* xfer opt does not play well with PRAGMA count_changes */
	}
	/* If we get this far, it means that the xfer optimization is at
	 ** least a possibility, though it might only work if the destination
	 ** table (tab1) is initially empty.
	 */
	iDbSrc = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pSrc)).FpSchema)
	v = _sqlite3GetVdbe(tls, pParse)
	_sqlite3CodeVerifySchema(tls, pParse, iDbSrc)
	v5 = pParse + 44
	v4 = *(*int32)(unsafe.Pointer(v5))
	*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
	iSrc = v4
	v5 = pParse + 44
	v4 = *(*int32)(unsafe.Pointer(v5))
	*(*int32)(unsafe.Pointer(v5)) = *(*int32)(unsafe.Pointer(v5)) + 1
	iDest = v4
	regAutoinc = _autoIncBegin(tls, pParse, iDbDest, pDest)
	regData = _sqlite3GetTempReg(tls, pParse)
	_sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, regData)
	regRowid = _sqlite3GetTempReg(tls, pParse)
	_sqlite3OpenTable(tls, pParse, iDest, iDbDest, pDest, int32(OP_OpenWrite))
	if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) && (int32((*TTable)(unsafe.Pointer(pDest)).FiPKey) < 0 && (*TTable)(unsafe.Pointer(pDest)).FpIndex != uintptr(0) || destHasUniqueIdx != 0 || onError != int32(OE_Abort) && onError != int32(OE_Rollback)) {
		/* In some circumstances, we are able to run the xfer optimization
		 ** only if the destination table is initially empty. Unless the
		 ** DBFLAG_Vacuum flag is set, this block generates code to make
		 ** that determination. If DBFLAG_Vacuum is set, then the destination
		 ** table is always empty.
		 **
		 ** Conditions under which the destination must be empty:
		 **
		 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
		 **     (If the destination is not initially empty, the rowid fields
		 **     of index entries might need to change.)
		 **
		 ** (2) The destination has a unique index.  (The xfer optimization
		 **     is unable to test uniqueness.)
		 **
		 ** (3) onError is something other than OE_Abort and OE_Rollback.
		 */
		addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iDest, 0)
		emptyDestTest = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto))
		_sqlite3VdbeJumpHere(tls, v, addr1)
	}
	if (*TTable)(unsafe.Pointer(pSrc)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) {
		_sqlite3OpenTable(tls, pParse, iSrc, iDbSrc, pSrc, int32(OP_OpenRead))
		emptySrcTest = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iSrc, 0)
		if int32((*TTable)(unsafe.Pointer(pDest)).FiPKey) >= 0 {
			addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iSrc, regRowid)
			if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) {
				addr2 = _sqlite3VdbeAddOp3(tls, v, int32(OP_NotExists), iDest, 0, regRowid)
				_sqlite3RowidConstraint(tls, pParse, onError, pDest)
				_sqlite3VdbeJumpHere(tls, v, addr2)
			}
			_autoIncStep(tls, pParse, regAutoinc, regRowid)
		} else {
			if (*TTable)(unsafe.Pointer(pDest)).FpIndex == uintptr(0) && !((*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&libc.Uint32FromInt32(DBFLAG_VacuumInto) != 0) {
				addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_NewRowid), iDest, regRowid)
			} else {
				addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rowid), iSrc, regRowid)
			}
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) != 0 {
			_sqlite3VdbeAddOp1(tls, v, int32(OP_SeekEnd), iDest)
			insFlags = uint8(libc.Int32FromInt32(OPFLAG_APPEND) | libc.Int32FromInt32(OPFLAG_USESEEKRESULT) | libc.Int32FromInt32(OPFLAG_PREFORMAT))
		} else {
			insFlags = uint8(libc.Int32FromInt32(OPFLAG_NCHANGE) | libc.Int32FromInt32(OPFLAG_LASTROWID) | libc.Int32FromInt32(OPFLAG_APPEND) | libc.Int32FromInt32(OPFLAG_PREFORMAT))
		}
		if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_RowData), iSrc, regData, int32(1))
			insFlags = uint8(int32(insFlags) & ^libc.Int32FromInt32(OPFLAG_PREFORMAT))
		} else {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_RowCell), iDest, iSrc, regRowid)
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iDest, regData, regRowid)
		if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) {
			_sqlite3VdbeChangeP4(tls, v, -int32(1), pDest, -int32(5))
		}
		_sqlite3VdbeChangeP5(tls, v, uint16(insFlags))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iSrc, addr1)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iSrc, 0)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iDest, 0)
	} else {
		_sqlite3TableLock(tls, pParse, iDbDest, (*TTable)(unsafe.Pointer(pDest)).Ftnum, uint8(1), (*TTable)(unsafe.Pointer(pDest)).FzName)
		_sqlite3TableLock(tls, pParse, iDbSrc, (*TTable)(unsafe.Pointer(pSrc)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pSrc)).FzName)
	}
	pDestIdx = (*TTable)(unsafe.Pointer(pDest)).FpIndex
	for {
		if !(pDestIdx != 0) {
			break
		}
		idxInsFlags = uint8(0)
		pSrcIdx = (*TTable)(unsafe.Pointer(pSrc)).FpIndex
		for {
			if !(pSrcIdx != 0) {
				break
			}
			if _xferCompatibleIndex(tls, pDestIdx, pSrcIdx) != 0 {
				break
			}
			goto _9
		_9:
			;
			pSrcIdx = (*TIndex)(unsafe.Pointer(pSrcIdx)).FpNext
		}
		_sqlite3VdbeAddOp3(tls, v, int32(OP_OpenRead), iSrc, int32((*TIndex)(unsafe.Pointer(pSrcIdx)).Ftnum), iDbSrc)
		_sqlite3VdbeSetP4KeyInfo(tls, pParse, pSrcIdx)
		_sqlite3VdbeAddOp3(tls, v, int32(OP_OpenWrite), iDest, int32((*TIndex)(unsafe.Pointer(pDestIdx)).Ftnum), iDbDest)
		_sqlite3VdbeSetP4KeyInfo(tls, pParse, pDestIdx)
		_sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_BULKCSR))
		addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iSrc, 0)
		if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) != 0 {
			/* This INSERT command is part of a VACUUM operation, which guarantees
			 ** that the destination table is empty. If all indexed columns use
			 ** collation sequence BINARY, then it can also be assumed that the
			 ** index will be populated by inserting keys in strictly sorted
			 ** order. In this case, instead of seeking within the b-tree as part
			 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
			 ** OP_IdxInsert to seek to the point within the b-tree where each key
			 ** should be inserted. This is faster.
			 **
			 ** If any of the indexed columns use a collation sequence other than
			 ** BINARY, this optimization is disabled. This is because the user
			 ** might change the definition of a collation sequence and then run
			 ** a VACUUM command. In that case keys may not be written in strictly
			 ** sorted order.  */
			i = 0
			for {
				if !(i < int32((*TIndex)(unsafe.Pointer(pSrcIdx)).FnColumn)) {
					break
				}
				zColl = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pSrcIdx)).FazColl + uintptr(i)*4))
				if Xsqlite3_stricmp(tls, uintptr(unsafe.Pointer(&_sqlite3StrBINARY)), zColl) != 0 {
					break
				}
				goto _10
			_10:
				;
				i = i + 1
			}
			if i == int32((*TIndex)(unsafe.Pointer(pSrcIdx)).FnColumn) {
				idxInsFlags = uint8(libc.Int32FromInt32(OPFLAG_USESEEKRESULT) | libc.Int32FromInt32(OPFLAG_PREFORMAT))
				_sqlite3VdbeAddOp1(tls, v, int32(OP_SeekEnd), iDest)
				_sqlite3VdbeAddOp2(tls, v, int32(OP_RowCell), iDest, iSrc)
			}
		} else {
			if !((*TTable)(unsafe.Pointer(pSrc)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pDestIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
				idxInsFlags = uint8(int32(idxInsFlags) | libc.Int32FromInt32(OPFLAG_NCHANGE))
			}
		}
		if int32(idxInsFlags) != libc.Int32FromInt32(OPFLAG_USESEEKRESULT)|libc.Int32FromInt32(OPFLAG_PREFORMAT) {
			_sqlite3VdbeAddOp3(tls, v, int32(OP_RowData), iSrc, regData, int32(1))
			if (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags&uint32(DBFLAG_Vacuum) == uint32(0) && !((*TTable)(unsafe.Pointer(pDest)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pDestIdx + 56))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) {
				_codeWithoutRowidPreupdate(tls, pParse, pDest, iDest, regData)
			}
		}
		_sqlite3VdbeAddOp2(tls, v, int32(OP_IdxInsert), iDest, regData)
		_sqlite3VdbeChangeP5(tls, v, uint16(int32(idxInsFlags)|int32(OPFLAG_APPEND)))
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Next), iSrc, addr1+int32(1))
		_sqlite3VdbeJumpHere(tls, v, addr1)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iSrc, 0)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iDest, 0)
		goto _8
	_8:
		;
		pDestIdx = (*TIndex)(unsafe.Pointer(pDestIdx)).FpNext
	}
	if emptySrcTest != 0 {
		_sqlite3VdbeJumpHere(tls, v, emptySrcTest)
	}
	_sqlite3ReleaseTempReg(tls, pParse, regRowid)
	_sqlite3ReleaseTempReg(tls, pParse, regData)
	if emptyDestTest != 0 {
		_sqlite3AutoincrementEnd(tls, pParse)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Halt), SQLITE_OK, 0)
		_sqlite3VdbeJumpHere(tls, v, emptyDestTest)
		_sqlite3VdbeAddOp2(tls, v, int32(OP_Close), iDest, 0)
		return 0
	} else {
		return int32(1)
	}
	return r
}

// C documentation
//
//	/* Mix sz bytes of entropy into p. */
func _xorMemory(tls *libc.TLS, p uintptr, x uintptr, sz int32) {
	var j, k, v3 int32
	var v2 uintptr
	_, _, _, _ = j, k, v2, v3
	j = 0
	k = (*TEntropyGatherer)(unsafe.Pointer(p)).Fi
	for {
		if !(j < sz) {
			break
		}
		v3 = k
		k = k + 1
		v2 = (*TEntropyGatherer)(unsafe.Pointer(p)).Fa + uintptr(v3)
		*(*uint8)(unsafe.Pointer(v2)) = uint8(int32(*(*uint8)(unsafe.Pointer(v2))) ^ int32(**(**uint8)(__ccgo_up(x + uintptr(j)))))
		if k >= (*TEntropyGatherer)(unsafe.Pointer(p)).Fna {
			k = 0
		}
		goto _1
	_1:
		;
		j = j + 1
	}
	(*TEntropyGatherer)(unsafe.Pointer(p)).Fi = k
	**(**int32)(__ccgo_up(p + 12)) += sz
}

// C documentation
//
//	/*
//	** Try to increase the size of the parser stack.  Return the number
//	** of errors.  Return 0 on success.
//	*/
func _yyGrowStack(tls *libc.TLS, p uintptr) (r int32) {
	var idx, nLimit, newSize, oldSize int32
	var pNew uintptr
	_, _, _, _, _ = idx, nLimit, newSize, oldSize, pNew
	oldSize = int32(1) + (int32((*TyyParser)(unsafe.Pointer(p)).FyystackEnd)-int32((*TyyParser)(unsafe.Pointer(p)).Fyystack))/12
	nLimit = _parserStackSizeLimit(tls, (*TyyParser)(unsafe.Pointer(p)).FpParse)
	newSize = oldSize*int32(2) + int32(100)
	if newSize > nLimit {
		newSize = nLimit
		if newSize <= oldSize {
			return int32(1)
		}
	}
	idx = (int32((*TyyParser)(unsafe.Pointer(p)).Fyytos) - int32((*TyyParser)(unsafe.Pointer(p)).Fyystack)) / 12
	if (*TyyParser)(unsafe.Pointer(p)).Fyystack == p+16 {
		pNew = _parserStackRealloc(tls, uintptr(0), uint64(uint32(newSize)*uint32(12)), (*TyyParser)(unsafe.Pointer(p)).FpParse)
		if pNew == uintptr(0) {
			return int32(1)
		}
		libc.Xmemcpy(tls, pNew, (*TyyParser)(unsafe.Pointer(p)).Fyystack, uint32(oldSize)*uint32(12))
	} else {
		pNew = _parserStackRealloc(tls, (*TyyParser)(unsafe.Pointer(p)).Fyystack, uint64(uint32(newSize)*uint32(12)), (*TyyParser)(unsafe.Pointer(p)).FpParse)
		if pNew == uintptr(0) {
			return int32(1)
		}
	}
	(*TyyParser)(unsafe.Pointer(p)).Fyystack = pNew
	(*TyyParser)(unsafe.Pointer(p)).Fyytos = (*TyyParser)(unsafe.Pointer(p)).Fyystack + uintptr(idx)*12
	(*TyyParser)(unsafe.Pointer(p)).FyystackEnd = (*TyyParser)(unsafe.Pointer(p)).Fyystack + uintptr(newSize-int32(1))*12
	return 0
}

/* Datatype of the argument to the memory allocated passed as the
** second argument to sqlite3ParserAlloc() below.  This can be changed by
** putting an appropriate #define in the %include section of the input
** grammar.
 */

// C documentation
//
//	/*
//	** Perform a reduce action and the shift that must immediately
//	** follow the reduce.
//	**
//	** The yyLookahead and yyLookaheadToken parameters provide reduce actions
//	** access to the lookahead token (if any).  The yyLookahead will be YYNOCODE
//	** if the lookahead token has already been consumed.  As this procedure is
//	** only called from one place, optimizing compilers will in-line it, which
//	** means that the extra parameters have no performance impact.
//	*/
func _yy_reduce(tls *libc.TLS, yypParser uintptr, yyruleno uint32, yyLookahead int32, yyLookaheadToken TToken, pParse uintptr) (r uint16) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var bNot, bNot1, i, nExpr, yygoto, yysize, v353 int32
	var n Tu32
	var op Tu8
	var p, p1, p2, p3, p4, p5, pB, pDot, pFrom, pFromClause, pLeft, pLhs, pList, pList1, pList2, pList3, pList4, pNew, pOld, pRHS, pRhs, pRight, pSelect, pSelectRHS, pSrc, pSubquery, pSubquery1, temp1, temp11, temp2, temp21, temp3, temp4, yymsp, v352 uintptr
	var yyact uint16
	var v357 TToken
	var _ /* all at bp+72 */ TToken
	var _ /* as at bp+48 */ TToken
	var _ /* dest at bp+8 */ TSelectDest
	var _ /* iValue at bp+56 */ int32
	var _ /* t at bp+64 */ TToken
	var _ /* x at bp+40 */ TToken
	var _ /* yylhsminor at bp+0 */ TYYMINORTYPE
	_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bNot, bNot1, i, n, nExpr, op, p, p1, p2, p3, p4, p5, pB, pDot, pFrom, pFromClause, pLeft, pLhs, pList, pList1, pList2, pList3, pList4, pNew, pOld, pRHS, pRhs, pRight, pSelect, pSelectRHS, pSrc, pSubquery, pSubquery1, temp1, temp11, temp2, temp21, temp3, temp4, yyact, yygoto, yymsp, yysize, v352, v353, v357 /* Amount to pop the stack */
	_ = yyLookahead
	_ = yyLookaheadToken
	yymsp = (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos
	switch yyruleno {
	case uint32(0):
		goto _1
	case uint32(1):
		goto _2
	case uint32(2):
		goto _3
	case uint32(3):
		goto _4
	case uint32(4):
		goto _5
	case uint32(6):
		goto _6
	case uint32(5):
		goto _7
	case uint32(7):
		goto _8
	case uint32(328):
		goto _9
	case uint32(9):
		goto _10
	case uint32(8):
		goto _11
	case uint32(10):
		goto _12
	case uint32(11):
		goto _13
	case uint32(12):
		goto _14
	case uint32(13):
		goto _15
	case uint32(14):
		goto _16
	case uint32(18):
		goto _17
	case uint32(15):
		goto _18
	case uint32(47):
		goto _19
	case uint32(62):
		goto _20
	case uint32(72):
		goto _21
	case uint32(81):
		goto _22
	case uint32(100):
		goto _23
	case uint32(246):
		goto _24
	case uint32(16):
		goto _25
	case uint32(17):
		goto _26
	case uint32(19):
		goto _27
	case uint32(20):
		goto _28
	case uint32(21):
		goto _29
	case uint32(22):
		goto _30
	case uint32(23):
		goto _31
	case uint32(24):
		goto _32
	case uint32(25):
		goto _33
	case uint32(65):
		goto _34
	case uint32(26):
		goto _35
	case uint32(106):
		goto _36
	case uint32(27):
		goto _37
	case uint32(28):
		goto _38
	case uint32(29):
		goto _39
	case uint32(30):
		goto _40
	case uint32(31):
		goto _41
	case uint32(67):
		goto _42
	case uint32(32):
		goto _43
	case uint32(33):
		goto _44
	case uint32(34):
		goto _45
	case uint32(35):
		goto _46
	case uint32(36):
		goto _47
	case uint32(37):
		goto _48
	case uint32(38):
		goto _49
	case uint32(39):
		goto _50
	case uint32(40):
		goto _51
	case uint32(41):
		goto _52
	case uint32(42):
		goto _53
	case uint32(43):
		goto _54
	case uint32(44):
		goto _55
	case uint32(45):
		goto _56
	case uint32(46):
		goto _57
	case uint32(48):
		goto _58
	case uint32(49):
		goto _59
	case uint32(50):
		goto _60
	case uint32(51):
		goto _61
	case uint32(52):
		goto _62
	case uint32(53):
		goto _63
	case uint32(54):
		goto _64
	case uint32(55):
		goto _65
	case uint32(56):
		goto _66
	case uint32(57):
		goto _67
	case uint32(58):
		goto _68
	case uint32(59):
		goto _69
	case uint32(60):
		goto _70
	case uint32(76):
		goto _71
	case uint32(61):
		goto _72
	case uint32(173):
		goto _73
	case uint32(80):
		goto _74
	case uint32(63):
		goto _75
	case uint32(219):
		goto _76
	case uint32(222):
		goto _77
	case uint32(247):
		goto _78
	case uint32(64):
		goto _79
	case uint32(66):
		goto _80
	case uint32(68):
		goto _81
	case uint32(69):
		goto _82
	case uint32(70):
		goto _83
	case uint32(71):
		goto _84
	case uint32(75):
		goto _85
	case uint32(73):
		goto _86
	case uint32(74):
		goto _87
	case uint32(77):
		goto _88
	case uint32(174):
		goto _89
	case uint32(78):
		goto _90
	case uint32(79):
		goto _91
	case uint32(82):
		goto _92
	case uint32(83):
		goto _93
	case uint32(84):
		goto _94
	case uint32(85):
		goto _95
	case uint32(86):
		goto _96
	case uint32(87):
		goto _97
	case uint32(88):
		goto _98
	case uint32(91):
		goto _99
	case uint32(89):
		goto _100
	case uint32(90):
		goto _101
	case uint32(92):
		goto _102
	case uint32(93):
		goto _103
	case uint32(94):
		goto _104
	case uint32(95):
		goto _105
	case uint32(97):
		goto _106
	case uint32(96):
		goto _107
	case uint32(98):
		goto _108
	case uint32(99):
		goto _109
	case uint32(134):
		goto _110
	case uint32(101):
		goto _111
	case uint32(144):
		goto _112
	case uint32(234):
		goto _113
	case uint32(237):
		goto _114
	case uint32(242):
		goto _115
	case uint32(102):
		goto _116
	case uint32(103):
		goto _117
	case uint32(104):
		goto _118
	case uint32(117):
		goto _119
	case uint32(105):
		goto _120
	case uint32(258):
		goto _121
	case uint32(259):
		goto _122
	case uint32(110):
		goto _123
	case uint32(107):
		goto _124
	case uint32(108):
		goto _125
	case uint32(109):
		goto _126
	case uint32(111):
		goto _127
	case uint32(112):
		goto _128
	case uint32(113):
		goto _129
	case uint32(114):
		goto _130
	case uint32(115):
		goto _131
	case uint32(131):
		goto _132
	case uint32(116):
		goto _133
	case uint32(120):
		goto _134
	case uint32(118):
		goto _135
	case uint32(121):
		goto _136
	case uint32(119):
		goto _137
	case uint32(122):
		goto _138
	case uint32(123):
		goto _139
	case uint32(124):
		goto _140
	case uint32(125):
		goto _141
	case uint32(126):
		goto _142
	case uint32(127):
		goto _143
	case uint32(128):
		goto _144
	case uint32(129):
		goto _145
	case uint32(130):
		goto _146
	case uint32(132):
		goto _147
	case uint32(133):
		goto _148
	case uint32(145):
		goto _149
	case uint32(135):
		goto _150
	case uint32(136):
		goto _151
	case uint32(137):
		goto _152
	case uint32(138):
		goto _153
	case uint32(139):
		goto _154
	case uint32(143):
		goto _155
	case uint32(140):
		goto _156
	case uint32(141):
		goto _157
	case uint32(142):
		goto _158
	case uint32(148):
		goto _159
	case uint32(146):
		goto _160
	case uint32(153):
		goto _161
	case uint32(155):
		goto _162
	case uint32(232):
		goto _163
	case uint32(233):
		goto _164
	case uint32(252):
		goto _165
	case uint32(154):
		goto _166
	case uint32(147):
		goto _167
	case uint32(156):
		goto _168
	case uint32(231):
		goto _169
	case uint32(251):
		goto _170
	case uint32(149):
		goto _171
	case uint32(150):
		goto _172
	case uint32(151):
		goto _173
	case uint32(152):
		goto _174
	case uint32(157):
		goto _175
	case uint32(158):
		goto _176
	case uint32(159):
		goto _177
	case uint32(160):
		goto _178
	case uint32(161):
		goto _179
	case uint32(162):
		goto _180
	case uint32(163):
		goto _181
	case uint32(164):
		goto _182
	case uint32(165):
		goto _183
	case uint32(166):
		goto _184
	case uint32(167):
		goto _185
	case uint32(168):
		goto _186
	case uint32(169):
		goto _187
	case uint32(170):
		goto _188
	case uint32(171):
		goto _189
	case uint32(172):
		goto _190
	case uint32(175):
		goto _191
	case uint32(176):
		goto _192
	case uint32(177):
		goto _193
	case uint32(178):
		goto _194
	case uint32(179):
		goto _195
	case uint32(180):
		goto _196
	case uint32(181):
		goto _197
	case uint32(182):
		goto _198
	case uint32(184):
		goto _199
	case uint32(183):
		goto _200
	case uint32(185):
		goto _201
	case uint32(186):
		goto _202
	case uint32(187):
		goto _203
	case uint32(188):
		goto _204
	case uint32(189):
		goto _205
	case uint32(190):
		goto _206
	case uint32(191):
		goto _207
	case uint32(192):
		goto _208
	case uint32(193):
		goto _209
	case uint32(194):
		goto _210
	case uint32(195):
		goto _211
	case uint32(196):
		goto _212
	case uint32(197):
		goto _213
	case uint32(199):
		goto _214
	case uint32(198):
		goto _215
	case uint32(200):
		goto _216
	case uint32(201):
		goto _217
	case uint32(202):
		goto _218
	case uint32(203):
		goto _219
	case uint32(204):
		goto _220
	case uint32(205):
		goto _221
	case uint32(206):
		goto _222
	case uint32(207):
		goto _223
	case uint32(208):
		goto _224
	case uint32(209):
		goto _225
	case uint32(210):
		goto _226
	case uint32(211):
		goto _227
	case uint32(212):
		goto _228
	case uint32(213):
		goto _229
	case uint32(215):
		goto _230
	case uint32(214):
		goto _231
	case uint32(216):
		goto _232
	case uint32(217):
		goto _233
	case uint32(221):
		goto _234
	case uint32(218):
		goto _235
	case uint32(220):
		goto _236
	case uint32(223):
		goto _237
	case uint32(224):
		goto _238
	case uint32(225):
		goto _239
	case uint32(226):
		goto _240
	case uint32(227):
		goto _241
	case uint32(228):
		goto _242
	case uint32(229):
		goto _243
	case uint32(230):
		goto _244
	case uint32(235):
		goto _245
	case uint32(236):
		goto _246
	case uint32(243):
		goto _247
	case uint32(238):
		goto _248
	case uint32(239):
		goto _249
	case uint32(281):
		goto _250
	case uint32(240):
		goto _251
	case uint32(241):
		goto _252
	case uint32(244):
		goto _253
	case uint32(245):
		goto _254
	case uint32(248):
		goto _255
	case uint32(249):
		goto _256
	case uint32(250):
		goto _257
	case uint32(253):
		goto _258
	case uint32(254):
		goto _259
	case uint32(255):
		goto _260
	case uint32(256):
		goto _261
	case uint32(257):
		goto _262
	case uint32(260):
		goto _263
	case uint32(261):
		goto _264
	case uint32(262):
		goto _265
	case uint32(263):
		goto _266
	case uint32(264):
		goto _267
	case uint32(266):
		goto _268
	case uint32(265):
		goto _269
	case uint32(267):
		goto _270
	case uint32(286):
		goto _271
	case uint32(268):
		goto _272
	case uint32(287):
		goto _273
	case uint32(269):
		goto _274
	case uint32(270):
		goto _275
	case uint32(271):
		goto _276
	case uint32(272):
		goto _277
	case uint32(273):
		goto _278
	case uint32(274):
		goto _279
	case uint32(275):
		goto _280
	case uint32(276):
		goto _281
	case uint32(277):
		goto _282
	case uint32(278):
		goto _283
	case uint32(279):
		goto _284
	case uint32(280):
		goto _285
	case uint32(282):
		goto _286
	case uint32(283):
		goto _287
	case uint32(284):
		goto _288
	case uint32(285):
		goto _289
	case uint32(288):
		goto _290
	case uint32(289):
		goto _291
	case uint32(290):
		goto _292
	case uint32(291):
		goto _293
	case uint32(292):
		goto _294
	case uint32(293):
		goto _295
	case uint32(294):
		goto _296
	case uint32(295):
		goto _297
	case uint32(296):
		goto _298
	case uint32(297):
		goto _299
	case uint32(298):
		goto _300
	case uint32(299):
		goto _301
	case uint32(300):
		goto _302
	case uint32(301):
		goto _303
	case uint32(302):
		goto _304
	case uint32(303):
		goto _305
	case uint32(304):
		goto _306
	case uint32(305):
		goto _307
	case uint32(307):
		goto _308
	case uint32(306):
		goto _309
	case uint32(308):
		goto _310
	case uint32(310):
		goto _311
	case uint32(309):
		goto _312
	case uint32(311):
		goto _313
	case uint32(312):
		goto _314
	case uint32(313):
		goto _315
	case uint32(314):
		goto _316
	case uint32(315):
		goto _317
	case uint32(316):
		goto _318
	case uint32(317):
		goto _319
	case uint32(318):
		goto _320
	case uint32(319):
		goto _321
	case uint32(320):
		goto _322
	case uint32(321):
		goto _323
	case uint32(322):
		goto _324
	case uint32(323):
		goto _325
	case uint32(324):
		goto _326
	case uint32(325):
		goto _327
	case uint32(326):
		goto _328
	case uint32(327):
		goto _329
	case uint32(331):
		goto _330
	case uint32(329):
		goto _331
	case uint32(332):
		goto _332
	case uint32(330):
		goto _333
	case uint32(334):
		goto _334
	case uint32(333):
		goto _335
	case uint32(335):
		goto _336
	case uint32(336):
		goto _337
	case uint32(338):
		goto _338
	case uint32(337):
		goto _339
	case uint32(339):
		goto _340
	case uint32(340):
		goto _341
	case uint32(341):
		goto _342
	case uint32(342):
		goto _343
	case uint32(343):
		goto _344
	case uint32(344):
		goto _345
	case uint32(345):
		goto _346
	case uint32(346):
		goto _347
	case uint32(347):
		goto _348
	default:
		goto _349
	}
	goto _350
_1:
	; /* explain ::= EXPLAIN */
	if (*TParse)(unsafe.Pointer(pParse)).FpReprepare == uintptr(0) {
		(*TParse)(unsafe.Pointer(pParse)).Fexplain = uint8(1)
	}
	goto _350
_2:
	; /* explain ::= EXPLAIN QUERY PLAN */
	if (*TParse)(unsafe.Pointer(pParse)).FpReprepare == uintptr(0) {
		(*TParse)(unsafe.Pointer(pParse)).Fexplain = uint8(2)
	}
	goto _350
_3:
	; /* cmdx ::= cmd */
	_sqlite3FinishCoding(tls, pParse)
	goto _350
_4:
	; /* cmd ::= BEGIN transtype trans_opt */
	_sqlite3BeginTransaction(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_5:
	; /* transtype ::= */
	*(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = int32(TK_DEFERRED)
	goto _350
_7:
	; /* transtype ::= DEFERRED */
_6:
	;
_8:
	;
_9:
	;
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32((**(**TyyStackEntry)(__ccgo_up(yymsp))).Fmajor) /*A-overwrites-X*/
	goto _350
_11:
	; /* cmd ::= COMMIT|END trans_opt */
_10:
	;
	_sqlite3EndTransaction(tls, pParse, int32((**(**TyyStackEntry)(__ccgo_up(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor))
	goto _350
_12:
	; /* cmd ::= SAVEPOINT nm */
	_sqlite3Savepoint(tls, pParse, SAVEPOINT_BEGIN, yymsp+4)
	goto _350
_13:
	; /* cmd ::= RELEASE savepoint_opt nm */
	_sqlite3Savepoint(tls, pParse, int32(SAVEPOINT_RELEASE), yymsp+4)
	goto _350
_14:
	; /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
	_sqlite3Savepoint(tls, pParse, int32(SAVEPOINT_ROLLBACK), yymsp+4)
	goto _350
_15:
	; /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
	_sqlite3StartTable(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, yymsp+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), 0, 0, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	goto _350
_16:
	; /* createkw ::= CREATE */
	_disableLookaside(tls, pParse)
	goto _350
_18:
	; /* ifnotexists ::= */
_17:
	;
_19:
	;
_20:
	;
_21:
	;
_22:
	;
_23:
	;
_24:
	;
	*(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = 0
	goto _350
_25:
	; /* ifnotexists ::= IF NOT EXISTS */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = int32(1)
	goto _350
_26:
	; /* temp ::= TEMP */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = libc.BoolInt32(int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Finit1.Fbusy) == 0)
	goto _350
_27:
	; /* create_table_args ::= LP columnlist conslist_opt RP table_option_set */
	_sqlite3EndTable(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*Tu32)(unsafe.Pointer(yymsp + 4)), uintptr(0))
	goto _350
_28:
	; /* create_table_args ::= AS select */
	_sqlite3EndTable(tls, pParse, uintptr(0), uintptr(0), uint32(0), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	_sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_29:
	; /* table_option_set ::= */
	*(*Tu32)(unsafe.Pointer(yymsp + 1*12 + 4)) = uint32(0)
	goto _350
_30:
	; /* table_option_set ::= table_option_set COMMA table_option */
	*(*Tu32)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = *(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) | *(*Tu32)(unsafe.Pointer(yymsp + 4))
	*(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*Tu32)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_31:
	; /* table_option ::= WITHOUT nm */
	if *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) == uint32(5) && Xsqlite3_strnicmp(tls, *(*uintptr)(unsafe.Pointer(yymsp + 4)), __ccgo_ts+19186, int32(5)) == 0 {
		*(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uint32(libc.Int32FromInt32(TF_WithoutRowid) | libc.Int32FromInt32(TF_NoVisibleRowid))
	} else {
		*(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uint32(0)
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+26229, libc.VaList(bp+88, *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))))
	}
	goto _350
_32:
	; /* table_option ::= nm */
	if *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) == uint32(6) && Xsqlite3_strnicmp(tls, *(*uintptr)(unsafe.Pointer(yymsp + 4)), __ccgo_ts+19122, int32(6)) == 0 {
		*(*Tu32)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = uint32(TF_Strict)
	} else {
		*(*Tu32)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = uint32(0)
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+26229, libc.VaList(bp+88, *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))))
	}
	*(*Tu32)(unsafe.Pointer(yymsp + 4)) = *(*Tu32)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_33:
	; /* columnname ::= nm typetoken */
	_sqlite3AddColumn(tls, pParse, *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*TToken)(unsafe.Pointer(yymsp + 4)))
	goto _350
_35:
	; /* typetoken ::= */
_34:
	;
_36:
	;
	*(*uint32)(unsafe.Pointer(yymsp + 1*12 + 4 + 4)) = uint32(0)
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0)
	goto _350
_37:
	; /* typetoken ::= typename LP signed RP */
	*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)) = uint32(t__predefined_ptrdiff_t(*(*uintptr)(unsafe.Pointer(yymsp + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + 4 + 4)))) - int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))))
	goto _350
_38:
	; /* typetoken ::= typename LP signed COMMA signed RP */
	*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4 + 4)) = uint32(t__predefined_ptrdiff_t(*(*uintptr)(unsafe.Pointer(yymsp + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + 4 + 4)))) - int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4))))
	goto _350
_39:
	; /* typename ::= typename ID|STRING */
	*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) + uint32(int32(*(*uintptr)(unsafe.Pointer(yymsp + 4)))-int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))))
	goto _350
_40:
	; /* scanpt ::= */
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = yyLookaheadToken.Fz
	goto _350
_41:
	; /* scantok ::= */
	*(*TToken)(unsafe.Pointer(yymsp + 1*12 + 4)) = yyLookaheadToken
	goto _350
_43:
	; /* ccons ::= CONSTRAINT nm */
_42:
	;
	(*(*struct {
		FaddrCrTab      int32
		FregRowid       int32
		FregRoot        int32
		FconstraintName TToken
	})(unsafe.Pointer(pParse + 188))).FconstraintName = *(*TToken)(unsafe.Pointer(yymsp + 4))
	goto _350
_44:
	; /* ccons ::= DEFAULT scantok term */
	_sqlite3AddDefaultValue(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4))))
	goto _350
_45:
	; /* ccons ::= DEFAULT LP expr RP */
	_sqlite3AddDefaultValue(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))+uintptr(1), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_46:
	; /* ccons ::= DEFAULT PLUS scantok term */
	_sqlite3AddDefaultValue(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4))))
	goto _350
_47:
	; /* ccons ::= DEFAULT MINUS scantok term */
	p = _sqlite3PExpr(tls, pParse, int32(TK_UMINUS), *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0))
	_sqlite3AddDefaultValue(tls, pParse, p, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4))))
	goto _350
_48:
	; /* ccons ::= DEFAULT scantok ID|INDEXED */
	p1 = _tokenExpr(tls, pParse, int32(TK_STRING), *(*TToken)(unsafe.Pointer(yymsp + 4)))
	if p1 != 0 {
		_sqlite3ExprIdToTrueFalse(tls, p1)
	}
	_sqlite3AddDefaultValue(tls, pParse, p1, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + 4 + 4))))
	goto _350
_49:
	; /* ccons ::= NOT NULL onconf */
	_sqlite3AddNotNull(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + 4)))
	goto _350
_50:
	; /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
	_sqlite3AddPrimaryKey(tls, pParse, uintptr(0), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	goto _350
_51:
	; /* ccons ::= UNIQUE onconf */
	_sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), uintptr(0), *(*int32)(unsafe.Pointer(yymsp + 4)), uintptr(0), uintptr(0), 0, 0, uint8(SQLITE_IDXTYPE_UNIQUE))
	goto _350
_52:
	; /* ccons ::= CHECK LP expr RP */
	_sqlite3AddCheckConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_53:
	; /* ccons ::= REFERENCES nm eidlist_opt refargs */
	_sqlite3CreateForeignKey(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)))
	goto _350
_54:
	; /* ccons ::= defer_subclause */
	_sqlite3DeferForeignKey(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + 4)))
	goto _350
_55:
	; /* ccons ::= COLLATE ID|STRING */
	_sqlite3AddCollateType(tls, pParse, yymsp+4)
	goto _350
_56:
	; /* generated ::= LP expr RP */
	_sqlite3AddGenerated(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0))
	goto _350
_57:
	; /* generated ::= LP expr RP ID */
	_sqlite3AddGenerated(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+4)
	goto _350
_58:
	; /* autoinc ::= AUTOINCR */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(1)
	goto _350
_59:
	;                                                                                                        /* refargs ::= */
	*(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = libc.Int32FromInt32(OE_None) * libc.Int32FromInt32(0x0101) /* EV: R-19803-45884 */
	goto _350
_60:
	; /* refargs ::= refargs refarg */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) & ^*(*int32)(unsafe.Pointer(yymsp + 4 + 4)) | *(*int32)(unsafe.Pointer(yymsp + 4))
	goto _350
_61:
	; /* refarg ::= MATCH nm */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = 0
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = 0x000000
	goto _350
_62:
	; /* refarg ::= ON INSERT refact */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = 0
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4 + 4)) = 0x000000
	goto _350
_63:
	; /* refarg ::= ON DELETE refact */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + 4))
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4 + 4)) = int32(0x0000ff)
	goto _350
_64:
	; /* refarg ::= ON UPDATE refact */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + 4)) << int32(8)
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4 + 4)) = int32(0x00ff00)
	goto _350
_65:
	;                                                                                              /* refact ::= SET NULL */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(OE_SetNull) /* EV: R-33326-45252 */
	goto _350
_66:
	;                                                                                              /* refact ::= SET DEFAULT */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(OE_SetDflt) /* EV: R-33326-45252 */
	goto _350
_67:
	;                                                        /* refact ::= CASCADE */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Cascade) /* EV: R-33326-45252 */
	goto _350
_68:
	;                                                         /* refact ::= RESTRICT */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Restrict) /* EV: R-33326-45252 */
	goto _350
_69:
	;                                                                                    /* refact ::= NO ACTION */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = OE_None /* EV: R-33326-45252 */
	goto _350
_70:
	; /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = 0
	goto _350
_72:
	; /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
_71:
	;
_73:
	;
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + 4))
	goto _350
_75:
	; /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
_74:
	;
_76:
	;
_77:
	;
_78:
	;
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(1)
	goto _350
_79:
	; /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = 0
	goto _350
_80:
	; /* tconscomma ::= COMMA */
	(*(*struct {
		FaddrCrTab      int32
		FregRowid       int32
		FregRoot        int32
		FconstraintName TToken
	})(unsafe.Pointer(pParse + 188))).FconstraintName.Fn = uint32(0)
	goto _350
_81:
	; /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
	_sqlite3AddPrimaryKey(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), 0)
	goto _350
_82:
	; /* tcons ::= UNIQUE LP sortlist RP onconf */
	_sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)), uintptr(0), uintptr(0), 0, 0, uint8(SQLITE_IDXTYPE_UNIQUE))
	goto _350
_83:
	; /* tcons ::= CHECK LP expr RP onconf */
	_sqlite3AddCheckConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_84:
	; /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
	_sqlite3CreateForeignKey(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	_sqlite3DeferForeignKey(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + 4)))
	goto _350
_86:
	; /* onconf ::= */
_85:
	;
	*(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = int32(OE_Default)
	goto _350
_87:
	; /* onconf ::= ON CONFLICT resolvetype */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*int32)(unsafe.Pointer(yymsp + 4))
	goto _350
_88:
	; /* resolvetype ::= IGNORE */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Ignore)
	goto _350
_90:
	; /* resolvetype ::= REPLACE */
_89:
	;
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Replace)
	goto _350
_91:
	; /* cmd ::= DROP TABLE ifexists fullname */
	_sqlite3DropTable(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), 0, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_92:
	; /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
	_sqlite3CreateView(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(8))*12+4, yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)))
	goto _350
_93:
	; /* cmd ::= DROP VIEW ifexists fullname */
	_sqlite3DropTable(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), int32(1), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_94:
	; /* cmd ::= select */
	**(**TSelectDest)(__ccgo_up(bp + 8)) = TSelectDest{
		FeDest: uint8(SRT_Output),
	}
	if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmDbFlags&uint32(DBFLAG_EncodingFixed) != uint32(0) || _sqlite3ReadSchema(tls, pParse) == SQLITE_OK {
		_sqlite3Select(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), bp+8)
	}
	_sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_95:
	; /* select ::= WITH wqlist selectnowith */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _attachWithToSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_96:
	; /* select ::= WITH RECURSIVE wqlist selectnowith */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _attachWithToSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_97:
	; /* select ::= selectnowith */
	p2 = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	if p2 != 0 {
		_parserDoubleLinkSelect(tls, pParse, p2)
	}
	goto _350
_98:
	; /* selectnowith ::= selectnowith multiselect_op oneselect */
	pRhs = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	pLhs = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))
	if pRhs != 0 && (*TSelect)(unsafe.Pointer(pRhs)).FpPrior != 0 {
		(**(**TToken)(__ccgo_up(bp + 40))).Fn = uint32(0)
		_parserDoubleLinkSelect(tls, pParse, pRhs)
		pFrom = _sqlite3SrcListAppendFromTerm(tls, pParse, uintptr(0), uintptr(0), uintptr(0), bp+40, pRhs, uintptr(0))
		pRhs = _sqlite3SelectNew(tls, pParse, uintptr(0), pFrom, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0))
	}
	if pRhs != 0 {
		(*TSelect)(unsafe.Pointer(pRhs)).Fop = uint8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
		(*TSelect)(unsafe.Pointer(pRhs)).FpPrior = pLhs
		if pLhs != 0 {
			**(**Tu32)(__ccgo_up(pLhs + 4)) &= ^libc.Uint32FromInt32(SF_MultiValue)
		}
		**(**Tu32)(__ccgo_up(pRhs + 4)) &= ^libc.Uint32FromInt32(SF_MultiValue)
		if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != int32(TK_ALL) {
			libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 2, 0x4)
		}
	} else {
		_sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pLhs)
	}
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = pRhs
	goto _350
_100:
	; /* multiselect_op ::= UNION */
_99:
	;
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32((**(**TyyStackEntry)(__ccgo_up(yymsp))).Fmajor) /*A-overwrites-OP*/
	goto _350
_101:
	; /* multiselect_op ::= UNION ALL */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(TK_ALL)
	goto _350
_102:
	; /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)) = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uint32(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4))), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_103:
	; /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(9))*12 + 4)) = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uint32(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4))), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(9))*12 + 4)) != 0 {
		(*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(9))*12 + 4)))).FpWinDefn = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))
	} else {
		_sqlite3WindowListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	}
	goto _350
_104:
	; /* values ::= VALUES LP nexprlist RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_Values), uintptr(0))
	goto _350
_105:
	; /* oneselect ::= mvalues */
	_sqlite3MultiValuesEnd(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_107:
	; /* mvalues ::= values COMMA LP nexprlist RP */
_106:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3MultiValues(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_108:
	; /* distinct ::= DISTINCT */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(SF_Distinct)
	goto _350
_109:
	; /* distinct ::= ALL */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(SF_All)
	goto _350
_111:
	; /* sclp ::= */
_110:
	;
_112:
	;
_113:
	;
_114:
	;
_115:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0)
	goto _350
_116:
	; /* selcollist ::= sclp scanpt expr scanpt as */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	if *(*uint32)(unsafe.Pointer(yymsp + 4 + 4)) > uint32(0) {
		_sqlite3ExprListSetName(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+4, int32(1))
	}
	_sqlite3ExprListSetSpan(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_117:
	; /* selcollist ::= sclp scanpt STAR */
	p3 = _sqlite3Expr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_ASTERISK), uintptr(0))
	_sqlite3ExprSetErrorOffset(tls, p3, int32(*(*uintptr)(unsafe.Pointer(yymsp + 4)))-int32((*TParse)(unsafe.Pointer(pParse)).FzTail))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), p3)
	goto _350
_118:
	; /* selcollist ::= sclp scanpt nm DOT STAR */
	pRight = _sqlite3PExpr(tls, pParse, int32(TK_ASTERISK), uintptr(0), uintptr(0))
	_sqlite3ExprSetErrorOffset(tls, pRight, int32(*(*uintptr)(unsafe.Pointer(yymsp + 4)))-int32((*TParse)(unsafe.Pointer(pParse)).FzTail))
	pLeft = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	pDot = _sqlite3PExpr(tls, pParse, int32(TK_DOT), pLeft, pRight)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), pDot)
	goto _350
_120:
	; /* as ::= AS nm */
_119:
	;
_121:
	;
_122:
	;
	*(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TToken)(unsafe.Pointer(yymsp + 4))
	goto _350
_124:
	; /* from ::= */
_123:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0)
	goto _350
_125:
	; /* from ::= FROM seltablist */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	_sqlite3SrcListShiftJoinType(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_126:
	; /* stl_prefix ::= seltablist joinop */
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 && (*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FnSrc > 0 {
		(*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) + 8 + uintptr((*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FnSrc-int32(1))*48))).Ffg.Fjointype = uint8(*(*int32)(unsafe.Pointer(yymsp + 4)))
	}
	goto _350
_127:
	; /* seltablist ::= stl_prefix nm dbnm as on_using */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0), yymsp+4)
	goto _350
_128:
	; /* seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, uintptr(0), yymsp+4)
	_sqlite3SrcListIndexedBy(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4)
	goto _350
_129:
	; /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(6))*12+4, yymsp+uintptr(-libc.Int32FromInt32(5))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0), yymsp+4)
	_sqlite3SrcListFuncArgs(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))
	goto _350
_130:
	; /* seltablist ::= stl_prefix LP select RP as on_using */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), yymsp+4)
	goto _350
_131:
	; /* seltablist ::= stl_prefix LP seltablist RP as on_using */
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) == uintptr(0) && *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) == uint32(0) && *(*uintptr)(unsafe.Pointer(yymsp + 4)) == uintptr(0) && *(*uintptr)(unsafe.Pointer(yymsp + 4 + 4)) == uintptr(0) {
		*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))
	} else {
		if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != uintptr(0) && (*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))).FnSrc == int32(1) {
			*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0), yymsp+4)
			if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) != 0 {
				pNew = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) + 8 + uintptr((*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)))).FnSrc-int32(1))*48
				pOld = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) + 8
				(*TSrcItem)(unsafe.Pointer(pNew)).FzName = (*TSrcItem)(unsafe.Pointer(pOld)).FzName
				if int32(*(*uint32)(unsafe.Pointer(pOld + 12 + 4))&0x4>>2) != 0 {
					libc.SetBitFieldPtr32Uint32(pNew+12+4, libc.Uint32FromInt32(1), 2, 0x4)
					*(*uintptr)(unsafe.Pointer(pNew + 44)) = *(*uintptr)(unsafe.Pointer(pOld + 44))
					*(*uintptr)(unsafe.Pointer(pOld + 44)) = uintptr(0)
					libc.SetBitFieldPtr32Uint32(pOld+12+4, libc.Uint32FromInt32(0), 2, 0x4)
					if (*TSelect)(unsafe.Pointer((*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNew + 44)))).FpSelect)).FselFlags&uint32(SF_NestedFrom) != uint32(0) {
						libc.SetBitFieldPtr32Uint32(pNew+12+4, libc.Uint32FromInt32(1), 14, 0x4000)
					}
				} else {
					*(*uintptr)(unsafe.Pointer(pNew + 44)) = *(*uintptr)(unsafe.Pointer(pOld + 44))
					*(*uintptr)(unsafe.Pointer(pOld + 44)) = uintptr(0)
				}
				if int32(*(*uint32)(unsafe.Pointer(pOld + 12 + 4))&0x8>>3) != 0 {
					*(*uintptr)(unsafe.Pointer(pNew + 32)) = *(*uintptr)(unsafe.Pointer(pOld + 32))
					*(*uintptr)(unsafe.Pointer(pOld + 32)) = uintptr(0)
					libc.SetBitFieldPtr32Uint32(pOld+12+4, libc.Uint32FromInt32(0), 3, 0x8)
					libc.SetBitFieldPtr32Uint32(pNew+12+4, libc.Uint32FromInt32(1), 3, 0x8)
				}
				(*TSrcItem)(unsafe.Pointer(pOld)).FzName = uintptr(0)
			}
			_sqlite3SrcListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))
		} else {
			_sqlite3SrcListShiftJoinType(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))
			pSubquery = _sqlite3SelectNew(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_NestedFrom), uintptr(0))
			*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, pSubquery, yymsp+4)
		}
	}
	goto _350
_133:
	; /* dbnm ::= */
_132:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0)
	*(*uint32)(unsafe.Pointer(yymsp + 1*12 + 4 + 4)) = uint32(0)
	goto _350
_135:
	; /* fullname ::= nm */
_134:
	;
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+4, uintptr(0))
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) != 0 {
		_sqlite3RenameTokenMap(tls, pParse, (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) + 8))).FzName, yymsp+4)
	}
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_137:
	; /* fullname ::= nm DOT nm */
_136:
	;
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4)
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) != 0 {
		_sqlite3RenameTokenMap(tls, pParse, (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) + 8))).FzName, yymsp+4)
	}
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_138:
	; /* xfullname ::= nm AS nm */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, uintptr(0))
	if *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) != 0 {
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
			_sqlite3RenameTokenMap(tls, pParse, (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) + 8))).FzName, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4)
		} else {
			(*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) + 8))).FzAlias = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, yymsp+4)
		}
	}
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_139:
	; /* xfullname ::= nm DOT nm AS nm */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4)
	if *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) != 0 {
		if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
			_sqlite3RenameTokenMap(tls, pParse, (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) + 8))).FzName, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4)
		} else {
			(*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) + 8))).FzAlias = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, yymsp+4)
		}
	}
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_140:
	; /* joinop ::= COMMA|JOIN */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(JT_INNER)
	goto _350
_141:
	;                                                                                                                                                                               /* joinop ::= JOIN_KW JOIN */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3JoinType(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0), uintptr(0)) /*X-overwrites-A*/
	goto _350
_142:
	;                                                                                                                                                                                                                /* joinop ::= JOIN_KW nm JOIN */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3JoinType(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, uintptr(0)) /*X-overwrites-A*/
	goto _350
_143:
	;                                                                                                                                                                                                                                                 /* joinop ::= JOIN_KW nm nm JOIN */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3JoinType(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4) /*X-overwrites-A*/
	goto _350
_144:
	; /* on_using ::= ON expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = uintptr(0)
	goto _350
_145:
	; /* on_using ::= USING LP idlist RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = uintptr(0)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _350
_146:
	; /* on_using ::= */
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0)
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4 + 4)) = uintptr(0)
	goto _350
_147:
	; /* indexed_by ::= INDEXED BY nm */
	*(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*TToken)(unsafe.Pointer(yymsp + 4))
	goto _350
_148:
	; /* indexed_by ::= NOT INDEXED */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uintptr(0)
	*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = uint32(1)
	goto _350
_150:
	; /* orderby_opt ::= ORDER BY sortlist */
_149:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	goto _350
_151:
	; /* sortlist ::= sortlist COMMA expr sortorder nulls */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	_sqlite3ExprListSetSortOrder(tls, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)))
	goto _350
_152:
	;                                                                                                                                                                                                            /* sortlist ::= expr sortorder nulls */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))) /*A-overwrites-Y*/
	_sqlite3ExprListSetSortOrder(tls, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)))
	goto _350
_153:
	; /* sortorder ::= ASC */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = SQLITE_SO_ASC
	goto _350
_154:
	; /* sortorder ::= DESC */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(SQLITE_SO_DESC)
	goto _350
_156:
	; /* sortorder ::= */
_155:
	;
	*(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = -int32(1)
	goto _350
_157:
	; /* nulls ::= NULLS FIRST */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = SQLITE_SO_ASC
	goto _350
_158:
	; /* nulls ::= NULLS LAST */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(SQLITE_SO_DESC)
	goto _350
_160:
	; /* having_opt ::= */
_159:
	;
_161:
	;
_162:
	;
_163:
	;
_164:
	;
_165:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0)
	goto _350
_167:
	; /* having_opt ::= HAVING expr */
_166:
	;
_168:
	;
_169:
	;
_170:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	goto _350
_171:
	; /* limit_opt ::= LIMIT expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0))
	goto _350
_172:
	; /* limit_opt ::= LIMIT expr OFFSET expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_173:
	; /* limit_opt ::= LIMIT expr COMMA expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	goto _350
_174:
	; /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
	_sqlite3SrcListIndexedBy(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4)
	_sqlite3DeleteFrom(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0), uintptr(0))
	goto _350
_175:
	; /* where_opt_ret ::= RETURNING selcollist */
	_sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uintptr(0)
	goto _350
_176:
	; /* where_opt_ret ::= WHERE expr RETURNING selcollist */
	_sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))
	goto _350
_177:
	; /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
	_sqlite3SrcListIndexedBy(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4)
	_sqlite3ExprListCheckLength(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), __ccgo_ts+26256)
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 {
		pFromClause = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
		if (*TSrcList)(unsafe.Pointer(pFromClause)).FnSrc > int32(1) {
			pSubquery1 = _sqlite3SelectNew(tls, pParse, uintptr(0), pFromClause, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_NestedFrom), uintptr(0))
			(**(**TToken)(__ccgo_up(bp + 48))).Fn = uint32(0)
			(**(**TToken)(__ccgo_up(bp + 48))).Fz = uintptr(0)
			pFromClause = _sqlite3SrcListAppendFromTerm(tls, pParse, uintptr(0), uintptr(0), uintptr(0), bp+48, pSubquery1, uintptr(0))
		}
		*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3SrcListAppendList(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), pFromClause)
	}
	_sqlite3Update(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), uintptr(0), uintptr(0), uintptr(0))
	goto _350
_178:
	; /* setlist ::= setlist COMMA nm EQ expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	_sqlite3ExprListSetName(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, int32(1))
	goto _350
_179:
	; /* setlist ::= setlist COMMA LP idlist RP EQ expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)) = _sqlite3ExprListAppendVector(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_180:
	; /* setlist ::= nm EQ expr */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	_sqlite3ExprListSetName(tls, pParse, *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, int32(1))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_181:
	; /* setlist ::= LP idlist RP EQ expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppendVector(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_182:
	; /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
	_sqlite3Insert(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_183:
	; /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
	_sqlite3Insert(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), uintptr(0))
	goto _350
_184:
	; /* upsert ::= */
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0)
	goto _350
_185:
	; /* upsert ::= RETURNING selcollist */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uintptr(0)
	_sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_186:
	; /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(11))*12 + 4)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_187:
	; /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_188:
	; /* upsert ::= ON CONFLICT DO NOTHING returning */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0))
	goto _350
_189:
	; /* upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0))
	goto _350
_190:
	; /* returning ::= RETURNING selcollist */
	_sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_191:
	; /* idlist_opt ::= */
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0)
	goto _350
_192:
	; /* idlist_opt ::= LP idlist RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _350
_193:
	; /* idlist ::= idlist COMMA nm */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3IdListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+4)
	goto _350
_194:
	;                                                                                               /* idlist ::= nm */
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = _sqlite3IdListAppend(tls, pParse, uintptr(0), yymsp+4) /*A-overwrites-Y*/
	goto _350
_195:
	; /* expr ::= LP expr RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _350
_196:
	;                                                                                                                     /* expr ::= ID|INDEXED|JOIN_KW */
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-X*/
	goto _350
_197:
	; /* expr ::= nm DOT nm */
	temp1 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	temp2 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3PExpr(tls, pParse, int32(TK_DOT), temp1, temp2)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_198:
	; /* expr ::= nm DOT nm DOT nm */
	temp11 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
	temp21 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	temp3 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + 4)))
	temp4 = _sqlite3PExpr(tls, pParse, int32(TK_DOT), temp21, temp3)
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) {
		_sqlite3RenameTokenRemap(tls, pParse, uintptr(0), temp11)
	}
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3PExpr(tls, pParse, int32(TK_DOT), temp11, temp4)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_200:
	; /* term ::= NULL|FLOAT|BLOB */
_199:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = _tokenExpr(tls, pParse, int32((**(**TyyStackEntry)(__ccgo_up(yymsp))).Fmajor), *(*TToken)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-X*/
	goto _350
_201:
	; /* term ::= INTEGER */
	if _sqlite3GetInt32(tls, *(*uintptr)(unsafe.Pointer(yymsp + 4)), bp+56) == 0 {
		*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_INTEGER), yymsp+4, 0)
	} else {
		*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprInt32(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, **(**int32)(__ccgo_up(bp + 56)))
	}
	if *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) != 0 {
		*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) + 36)) = int32(*(*uintptr)(unsafe.Pointer(yymsp + 4))) - int32((*TParse)(unsafe.Pointer(pParse)).FzTail)
	}
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_202:
	; /* expr ::= VARIABLE */
	if !(int32(**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(yymsp + 4))))) == int32('#') && int32(_sqlite3CtypeMap[uint8(**(**int8)(__ccgo_up(*(*uintptr)(unsafe.Pointer(yymsp + 4)) + 1)))])&int32(0x04) != 0) {
		n = *(*uint32)(unsafe.Pointer(yymsp + 4 + 4))
		*(*uintptr)(unsafe.Pointer(yymsp + 4)) = _tokenExpr(tls, pParse, int32(TK_VARIABLE), *(*TToken)(unsafe.Pointer(yymsp + 4)))
		_sqlite3ExprAssignVarNumber(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), n)
	} else {
		/* When doing a nested parse, one can include terms in an expression
		 ** that look like this:   #1 #2 ...  These terms refer to registers
		 ** in the virtual machine.  #N is the N-th register. */
		**(**TToken)(__ccgo_up(bp + 64)) = *(*TToken)(unsafe.Pointer(yymsp + 4)) /*A-overwrites-X*/
		if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 {
			_parserSyntaxError(tls, pParse, bp+64)
			*(*uintptr)(unsafe.Pointer(yymsp + 4)) = uintptr(0)
		} else {
			*(*uintptr)(unsafe.Pointer(yymsp + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_REGISTER), uintptr(0), uintptr(0))
			if *(*uintptr)(unsafe.Pointer(yymsp + 4)) != 0 {
				_sqlite3GetInt32(tls, (**(**TToken)(__ccgo_up(bp + 64))).Fz+1, *(*uintptr)(unsafe.Pointer(yymsp + 4))+28)
			}
		}
	}
	goto _350
_203:
	; /* expr ::= expr COLLATE ID|STRING */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprAddCollateToken(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+4, int32(1))
	goto _350
_204:
	; /* expr ::= CAST LP expr AS typetoken RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_CAST), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, int32(1))
	_sqlite3ExprAttachSubtrees(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), uintptr(0))
	goto _350
_205:
	; /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprFunction(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_206:
	; /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprFunction(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(7))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)))
	_sqlite3ExprAddFunctionOrderBy(tls, pParse, *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_207:
	; /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprFunction(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, 0)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_208:
	; /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprFunction(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(5))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))
	_sqlite3WindowAttach(tls, pParse, *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_209:
	; /* expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprFunction(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(8))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)))
	_sqlite3WindowAttach(tls, pParse, *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	_sqlite3ExprAddFunctionOrderBy(tls, pParse, *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_210:
	; /* expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprFunction(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, 0)
	_sqlite3WindowAttach(tls, pParse, *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_211:
	; /* term ::= CTIME_KW */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprFunction(tls, pParse, uintptr(0), yymsp+4, 0)
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_212:
	; /* expr ::= LP nexprlist COMMA expr RP */
	pList = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_VECTOR), uintptr(0), uintptr(0))
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 {
		*(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 20)) = pList
		i = 0
		for {
			if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
				break
			}
			**(**Tu32)(__ccgo_up(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 4)) |= (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*20))).FpExpr)).Fflags & uint32(libc.Int32FromInt32(EP_Collate)|libc.Int32FromInt32(EP_Subquery)|libc.Int32FromInt32(EP_HasFunc))
			goto _351
		_351:
			;
			i = i + 1
		}
	} else {
		_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pList)
	}
	goto _350
_213:
	; /* expr ::= expr AND expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprAnd(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_215:
	; /* expr ::= expr OR expr */
_214:
	;
_216:
	;
_217:
	;
_218:
	;
_219:
	;
_220:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32((**(**TyyStackEntry)(__ccgo_up(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_221:
	; /* likeop ::= NOT LIKE_KW|MATCH */
	*(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TToken)(unsafe.Pointer(yymsp + 4))
	*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) |= uint32(0x80000000) /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/
	goto _350
_222:
	; /* expr ::= expr likeop expr */
	bNot = int32(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) & uint32(0x80000000))
	*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) &= uint32(0x7fffffff)
	pList1 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	pList1 = _sqlite3ExprListAppend(tls, pParse, pList1, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprFunction(tls, pParse, pList1, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, 0)
	if bNot != 0 {
		*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), uintptr(0))
	}
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) != 0 {
		**(**Tu32)(__ccgo_up(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) + 4)) |= uint32(EP_InfixFunc)
	}
	goto _350
_223:
	; /* expr ::= expr likeop expr ESCAPE expr */
	bNot1 = int32(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)) & uint32(0x80000000))
	*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)) &= uint32(0x7fffffff)
	pList2 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	pList2 = _sqlite3ExprListAppend(tls, pParse, pList2, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
	pList2 = _sqlite3ExprListAppend(tls, pParse, pList2, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprFunction(tls, pParse, pList2, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, 0)
	if bNot1 != 0 {
		*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
	}
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 {
		**(**Tu32)(__ccgo_up(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 4)) |= uint32(EP_InfixFunc)
	}
	goto _350
_224:
	; /* expr ::= expr ISNULL|NOTNULL */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3PExprIsNull(tls, pParse, int32((**(**TyyStackEntry)(__ccgo_up(yymsp))).Fmajor), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_225:
	; /* expr ::= expr NOT NULL */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExprIsNull(tls, pParse, int32(TK_NOTNULL), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	goto _350
_226:
	; /* expr ::= expr IS expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExprIs(tls, pParse, int32(TK_IS), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_227:
	; /* expr ::= expr IS NOT expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3PExprIs(tls, pParse, int32(TK_ISNOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_228:
	; /* expr ::= expr IS NOT DISTINCT FROM expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3PExprIs(tls, pParse, int32(TK_IS), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_229:
	; /* expr ::= expr IS DISTINCT FROM expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExprIs(tls, pParse, int32(TK_ISNOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_231:
	; /* expr ::= NOT expr */
_230:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32((**(**TyyStackEntry)(__ccgo_up(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor), *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0)) /*A-overwrites-B*/
	goto _350
_232:
	; /* expr ::= PLUS|MINUS expr */
	p4 = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	op = uint8(int32((**(**TyyStackEntry)(__ccgo_up(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor) + (libc.Int32FromInt32(TK_UPLUS) - libc.Int32FromInt32(TK_PLUS)))
	if p4 != 0 && int32((*TExpr)(unsafe.Pointer(p4)).Fop) == int32(TK_UPLUS) {
		(*TExpr)(unsafe.Pointer(p4)).Fop = op
		*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = p4
	} else {
		*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(op), p4, uintptr(0))
		/*A-overwrites-B*/
	}
	goto _350
_233:
	; /* expr ::= expr PTR expr */
	pList3 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	pList3 = _sqlite3ExprListAppend(tls, pParse, pList3, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3ExprFunction(tls, pParse, pList3, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, 0)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_235:
	; /* between_op ::= BETWEEN */
_234:
	;
	*(*int32)(unsafe.Pointer(yymsp + 4)) = 0
	goto _350
_236:
	; /* expr ::= expr between_op expr AND expr */
	pList4 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	pList4 = _sqlite3ExprListAppend(tls, pParse, pList4, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_BETWEEN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 {
		*(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 20)) = pList4
		_sqlite3ExprSetHeightAndFlags(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
	} else {
		_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pList4)
	}
	if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 {
		*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
	}
	goto _350
_237:
	; /* expr ::= expr in_op LP exprlist RP */
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) == uintptr(0) {
		if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 {
			v352 = __ccgo_ts + 9395
		} else {
			v352 = __ccgo_ts + 9400
		}
		/* Expressions of the form
		 **
		 **      expr1 IN ()
		 **      expr1 NOT IN ()
		 **
		 ** simplify to constants 0 (false) and 1 (true), respectively.
		 **
		 ** Except, do not apply this optimization if expr1 contains a function
		 ** because that function might be an aggregate (we don't know yet whether
		 ** it is or not) and if it is an aggregate, that could change the meaning
		 ** of the whole query.
		 */
		pB = _sqlite3Expr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_STRING), v352)
		if pB != 0 {
			_sqlite3ExprIdToTrueFalse(tls, pB)
		}
		if !((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))).Fflags&uint32(libc.Int32FromInt32(EP_HasFunc)) != libc.Uint32FromInt32(0)) {
			_sqlite3ExprUnmapAndDelete(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
			*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = pB
		} else {
			if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 {
				v353 = int32(TK_OR)
			} else {
				v353 = int32(TK_AND)
			}
			*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, v353, pB, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
		}
	} else {
		pRHS = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) + 8))).FpExpr
		if (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FnExpr == int32(1) && _sqlite3ExprIsConstant(tls, pParse, pRHS) != 0 && int32((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))).Fop) != int32(TK_VECTOR) {
			(*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) + 8))).FpExpr = uintptr(0)
			_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
			pRHS = _sqlite3PExpr(tls, pParse, int32(TK_UPLUS), pRHS, uintptr(0))
			*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_EQ), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), pRHS)
		} else {
			if (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FnExpr == int32(1) && int32((*TExpr)(unsafe.Pointer(pRHS)).Fop) == int32(TK_SELECT) {
				*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
				_sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(pRHS + 20)))
				*(*uintptr)(unsafe.Pointer(pRHS + 20)) = uintptr(0)
				_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
			} else {
				*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
				if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) == uintptr(0) {
					_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
				} else {
					if int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))).FpLeft)).Fop) == int32(TK_VECTOR) {
						nExpr = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))).FpLeft + 20)))).FnExpr
						pSelectRHS = _sqlite3ExprListToValues(tls, pParse, nExpr, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
						if pSelectRHS != 0 {
							_parserDoubleLinkSelect(tls, pParse, pSelectRHS)
							_sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), pSelectRHS)
						}
					} else {
						*(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 20)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
						_sqlite3ExprSetHeightAndFlags(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
					}
				}
			}
		}
		if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 {
			*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
		}
	}
	goto _350
_238:
	; /* expr ::= LP select RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_SELECT), uintptr(0), uintptr(0))
	_sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_239:
	; /* expr ::= expr in_op LP select RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
	_sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 {
		*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
	}
	goto _350
_240:
	; /* expr ::= expr in_op nm dbnm paren_exprlist */
	pSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4)
	pSelect = _sqlite3SelectNew(tls, pParse, uintptr(0), pSrc, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0))
	if *(*uintptr)(unsafe.Pointer(yymsp + 4)) != 0 {
		if pSelect != 0 {
			v352 = pSrc
		} else {
			v352 = uintptr(0)
		}
		_sqlite3SrcListFuncArgs(tls, pParse, v352, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	}
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
	_sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), pSelect)
	if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 {
		*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uintptr(0))
	}
	goto _350
_241:
	; /* expr ::= EXISTS LP select RP */
	v352 = _sqlite3PExpr(tls, pParse, int32(TK_EXISTS), uintptr(0), uintptr(0))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = v352
	p5 = v352
	_sqlite3PExprAddSelect(tls, pParse, p5, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_242:
	; /* expr ::= CASE case_operand case_exprlist case_else END */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_CASE), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), uintptr(0))
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) != 0 {
		if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 {
			v352 = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
		} else {
			v352 = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))
		}
		*(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) + 20)) = v352
		_sqlite3ExprSetHeightAndFlags(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
	} else {
		_sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	}
	goto _350
_243:
	; /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_244:
	; /* case_exprlist ::= WHEN expr THEN expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_245:
	; /* nexprlist ::= nexprlist COMMA expr */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_246:
	;                                                                                                                                /* nexprlist ::= expr */
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-Y*/
	goto _350
_248:
	; /* paren_exprlist ::= LP exprlist RP */
_247:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _350
_249:
	; /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
	_sqlite3CreateIndex(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(7))*12+4, yymsp+uintptr(-libc.Int32FromInt32(6))*12+4, _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, uintptr(0)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(10))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(11))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + 4)), SQLITE_SO_ASC, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)), uint8(SQLITE_IDXTYPE_APPDEF))
	if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*TParse)(unsafe.Pointer(pParse)).FpNewIndex != 0 {
		_sqlite3RenameTokenMap(tls, pParse, (*TIndex)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).FpNewIndex)).FzName, yymsp+uintptr(-libc.Int32FromInt32(4))*12+4)
	}
	goto _350
_251:
	; /* uniqueflag ::= UNIQUE */
_250:
	;
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Abort)
	goto _350
_252:
	; /* uniqueflag ::= */
	*(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = OE_None
	goto _350
_253:
	; /* eidlist ::= eidlist COMMA nm collate sortorder */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _parserAddExprIdListTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4)))
	goto _350
_254:
	;                                                                                                                                                                                                                                                                                               /* eidlist ::= nm collate sortorder */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _parserAddExprIdListTerm(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-Y*/
	goto _350
_255:
	; /* cmd ::= DROP INDEX ifexists fullname */
	_sqlite3DropIndex(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_256:
	; /* cmd ::= VACUUM vinto */
	_sqlite3Vacuum(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_257:
	; /* cmd ::= VACUUM nm vinto */
	_sqlite3Vacuum(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_258:
	; /* cmd ::= PRAGMA nm dbnm */
	_sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, yymsp+4, uintptr(0), 0)
	goto _350
_259:
	; /* cmd ::= PRAGMA nm dbnm EQ nmnum */
	_sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4, 0)
	goto _350
_260:
	; /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
	_sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, 0)
	goto _350
_261:
	; /* cmd ::= PRAGMA nm dbnm EQ minus_num */
	_sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4, int32(1))
	goto _350
_262:
	; /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
	_sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, int32(1))
	goto _350
_263:
	; /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
	(**(**TToken)(__ccgo_up(bp + 72))).Fz = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))
	(**(**TToken)(__ccgo_up(bp + 72))).Fn = uint32(int32(*(*uintptr)(unsafe.Pointer(yymsp + 4)))-int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))) + *(*uint32)(unsafe.Pointer(yymsp + 4 + 4))
	_sqlite3FinishTrigger(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), bp+72)
	goto _350
_264:
	; /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
	_sqlite3BeginTrigger(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(7))*12+4, yymsp+uintptr(-libc.Int32FromInt32(6))*12+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(10))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)))
	if *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4 + 4)) == uint32(0) {
		v357 = *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4))
	} else {
		v357 = *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4))
	}
	*(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(10))*12 + 4)) = v357 /*A-overwrites-T*/
	goto _350
_265:
	;                                                                                            /* trigger_time ::= BEFORE|AFTER */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32((**(**TyyStackEntry)(__ccgo_up(yymsp))).Fmajor) /*A-overwrites-X*/
	goto _350
_266:
	; /* trigger_time ::= INSTEAD OF */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = int32(TK_INSTEAD)
	goto _350
_267:
	; /* trigger_time ::= */
	*(*int32)(unsafe.Pointer(yymsp + 1*12 + 4)) = int32(TK_BEFORE)
	goto _350
_269:
	; /* trigger_event ::= DELETE|INSERT */
_268:
	;
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32((**(**TyyStackEntry)(__ccgo_up(yymsp))).Fmajor) /*A-overwrites-X*/
	*(*uintptr)(unsafe.Pointer(yymsp + 4 + 4)) = uintptr(0)
	goto _350
_270:
	; /* trigger_event ::= UPDATE OF idlist */
	*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = int32(TK_UPDATE)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	goto _350
_272:
	; /* when_clause ::= */
_271:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = uintptr(0)
	goto _350
_274:
	; /* when_clause ::= WHEN expr */
_273:
	;
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	goto _350
_275:
	; /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
	(*TTriggerStep)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))).FpLast)).FpNext = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	(*TTriggerStep)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))).FpLast = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _350
_276:
	; /* trigger_cmd_list ::= trigger_cmd SEMI */
	(*TTriggerStep)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FpLast = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _350
_277:
	; /* tridxby ::= INDEXED BY nm */
	_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+26265, 0)
	goto _350
_278:
	; /* tridxby ::= NOT INDEXED */
	_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+26349, 0)
	goto _350
_279:
	; /* trigger_cmd ::= UPDATE orconf xfullname tridxby SET setlist from where_opt scanpt */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3TriggerUpdateStep(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uint8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_280:
	;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* trigger_cmd ::= scanpt insert_cmd INTO xfullname idlist_opt select upsert scanpt */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3TriggerInsertStep(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), uint8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) /*yylhsminor.yy427-overwrites-yymsp[-6].minor.yy144*/
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_281:
	; /* trigger_cmd ::= DELETE FROM xfullname tridxby where_opt scanpt */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3TriggerDeleteStep(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_282:
	;                                                                                                                                                                                                                                                                                                                                           /* trigger_cmd ::= scanpt select scanpt */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3TriggerSelectStep(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4))) /*yylhsminor.yy427-overwrites-yymsp[-1].minor.yy555*/
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_283:
	; /* expr ::= RAISE LP IGNORE RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_RAISE), uintptr(0), uintptr(0))
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) != 0 {
		(*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))).FaffExpr = int8(OE_Ignore)
	}
	goto _350
_284:
	; /* expr ::= RAISE LP raisetype COMMA expr RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3PExpr(tls, pParse, int32(TK_RAISE), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0))
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) != 0 {
		(*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)))).FaffExpr = int8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))
	}
	goto _350
_285:
	; /* raisetype ::= ROLLBACK */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Rollback)
	goto _350
_286:
	; /* raisetype ::= FAIL */
	*(*int32)(unsafe.Pointer(yymsp + 4)) = int32(OE_Fail)
	goto _350
_287:
	; /* cmd ::= DROP TRIGGER ifexists fullname */
	_sqlite3DropTrigger(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	goto _350
_288:
	; /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
	_sqlite3Attach(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_289:
	; /* cmd ::= DETACH database_kw_opt expr */
	_sqlite3Detach(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_290:
	; /* cmd ::= REINDEX */
	_sqlite3Reindex(tls, pParse, uintptr(0), uintptr(0))
	goto _350
_291:
	; /* cmd ::= REINDEX nm dbnm */
	_sqlite3Reindex(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, yymsp+4)
	goto _350
_292:
	; /* cmd ::= ANALYZE */
	_sqlite3Analyze(tls, pParse, uintptr(0), uintptr(0))
	goto _350
_293:
	; /* cmd ::= ANALYZE nm dbnm */
	_sqlite3Analyze(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4, yymsp+4)
	goto _350
_294:
	; /* cmd ::= ALTER TABLE fullname RENAME TO nm */
	_sqlite3AlterRenameTable(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), yymsp+4)
	goto _350
_295:
	; /* cmd ::= alter_add carglist */
	*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)) = uint32(int32((*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz)-int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))) + (*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fn
	_sqlite3AlterFinishAddColumn(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*12+4)
	goto _350
_296:
	; /* alter_add ::= ALTER TABLE fullname ADD kwcolumn_opt nm typetoken */
	_disableLookaside(tls, pParse)
	_sqlite3AlterBeginAddColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
	_sqlite3AddColumn(tls, pParse, *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*TToken)(unsafe.Pointer(yymsp + 4)))
	*(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)) = *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _350
_297:
	; /* cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
	_sqlite3AlterDropColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), yymsp+4)
	goto _350
_298:
	; /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
	_sqlite3AlterRenameColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4)
	goto _350
_299:
	; /* cmd ::= ALTER TABLE fullname DROP CONSTRAINT nm */
	_sqlite3AlterDropConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), yymsp+4, uintptr(0))
	goto _350
_300:
	; /* cmd ::= ALTER TABLE fullname ALTER kwcolumn_opt nm DROP NOT NULL */
	_sqlite3AlterDropConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(3))*12+4)
	goto _350
_301:
	; /* cmd ::= ALTER TABLE fullname ALTER kwcolumn_opt nm SET NOT NULL onconf */
	_sqlite3AlterSetNotNull(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4)
	goto _350
_302:
	; /* cmd ::= ALTER TABLE fullname ADD CONSTRAINT nm CHECK LP expr RP onconf */
	_sqlite3AlterAddConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(6))*12+4, yymsp+uintptr(-libc.Int32FromInt32(5))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))+uintptr(1), int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))-int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))-int32(1), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	goto _350
_303:
	; /* cmd ::= ALTER TABLE fullname ADD CHECK LP expr RP onconf */
	_sqlite3AlterAddConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))+uintptr(1), int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))-int32(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)))-int32(1), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	goto _350
_304:
	; /* cmd ::= create_vtab */
	_sqlite3VtabFinishParse(tls, pParse, uintptr(0))
	goto _350
_305:
	; /* cmd ::= create_vtab LP vtabarglist RP */
	_sqlite3VtabFinishParse(tls, pParse, yymsp+4)
	goto _350
_306:
	; /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
	_sqlite3VtabBeginParse(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*12+4, yymsp+uintptr(-libc.Int32FromInt32(2))*12+4, yymsp+4, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)))
	goto _350
_307:
	; /* vtabarg ::= */
	_sqlite3VtabArgInit(tls, pParse)
	goto _350
_309:
	; /* vtabargtoken ::= ANY */
_308:
	;
_310:
	;
	_sqlite3VtabArgExtend(tls, pParse, yymsp+4)
	goto _350
_312:
	; /* with ::= WITH wqlist */
_311:
	;
	_sqlite3WithPush(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uint8(1))
	goto _350
_313:
	; /* wqas ::= AS */
	*(*Tu8)(unsafe.Pointer(yymsp + 4)) = uint8(M10d_Any)
	goto _350
_314:
	; /* wqas ::= AS MATERIALIZED */
	*(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uint8(M10d_Yes)
	goto _350
_315:
	; /* wqas ::= AS NOT MATERIALIZED */
	*(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = uint8(M10d_No)
	goto _350
_316:
	;                                                                                                                                                                                                                                                                                                                                                                                             /* wqitem ::= withnm eidlist_opt wqas LP select RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = _sqlite3CteNew(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(5))*12+4, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4))) /*A-overwrites-X*/
	goto _350
_317:
	; /* withnm ::= nm */
	libc.SetBitFieldPtr16Uint32(pParse+28, libc.Uint32FromInt32(1), 6, 0x40)
	goto _350
_318:
	;                                                                                                                         /* wqlist ::= wqitem */
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = _sqlite3WithAdd(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 4))) /*A-overwrites-X*/
	goto _350
_319:
	; /* wqlist ::= wqlist COMMA wqitem */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = _sqlite3WithAdd(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	goto _350
_320:
	; /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
	_sqlite3WindowChain(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)))
	(*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 4)))).FpNextWin = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4))
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_321:
	; /* windowdefn ::= nm AS LP window RP */
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 {
		(*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FzName = _sqlite3DbStrNDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)), uint64(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4 + 4))))
	}
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_322:
	; /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0))
	goto _350
_323:
	; /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(5))*12+4)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_324:
	; /* window ::= ORDER BY sortlist frame_opt */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), uintptr(0))
	goto _350
_325:
	; /* window ::= nm ORDER BY sortlist frame_opt */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), yymsp+uintptr(-libc.Int32FromInt32(4))*12+4)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_326:
	; /* window ::= nm frame_opt */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*12+4)
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_327:
	; /* frame_opt ::= */
	*(*uintptr)(unsafe.Pointer(yymsp + 1*12 + 4)) = _sqlite3WindowAlloc(tls, pParse, 0, int32(TK_UNBOUNDED), uintptr(0), int32(TK_CURRENT), uintptr(0), uint8(0))
	goto _350
_328:
	; /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3WindowAlloc(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)), int32(TK_CURRENT), uintptr(0), *(*Tu8)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_329:
	; /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3WindowAlloc(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4 + 4)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4 + 4)), *(*Tu8)(unsafe.Pointer(yymsp + 4)))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_331:
	; /* frame_bound_s ::= frame_bound */
_330:
	;
	*(*TFrameBound)(unsafe.Pointer(bp)) = *(*TFrameBound)(unsafe.Pointer(yymsp + 4))
	*(*TFrameBound)(unsafe.Pointer(yymsp + 4)) = *(*TFrameBound)(unsafe.Pointer(bp))
	goto _350
_333:
	; /* frame_bound_s ::= UNBOUNDED PRECEDING */
_332:
	;
_334:
	;
	(*(*TFrameBound)(unsafe.Pointer(bp))).FeType = int32((**(**TyyStackEntry)(__ccgo_up(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor)
	(*(*TFrameBound)(unsafe.Pointer(bp))).FpExpr = uintptr(0)
	*(*TFrameBound)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TFrameBound)(unsafe.Pointer(bp))
	goto _350
_335:
	; /* frame_bound ::= expr PRECEDING|FOLLOWING */
	(*(*TFrameBound)(unsafe.Pointer(bp))).FeType = int32((**(**TyyStackEntry)(__ccgo_up(yymsp))).Fmajor)
	(*(*TFrameBound)(unsafe.Pointer(bp))).FpExpr = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	*(*TFrameBound)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*TFrameBound)(unsafe.Pointer(bp))
	goto _350
_336:
	; /* frame_exclude_opt ::= */
	*(*Tu8)(unsafe.Pointer(yymsp + 1*12 + 4)) = uint8(0)
	goto _350
_337:
	; /* frame_exclude_opt ::= EXCLUDE frame_exclude */
	*(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*Tu8)(unsafe.Pointer(yymsp + 4))
	goto _350
_339:
	; /* frame_exclude ::= NO OTHERS */
_338:
	;
	*(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = uint8((**(**TyyStackEntry)(__ccgo_up(yymsp + uintptr(-libc.Int32FromInt32(1))*12))).Fmajor) /*A-overwrites-X*/
	goto _350
_340:
	;                                                                                          /* frame_exclude ::= GROUP|TIES */
	*(*Tu8)(unsafe.Pointer(yymsp + 4)) = uint8((**(**TyyStackEntry)(__ccgo_up(yymsp))).Fmajor) /*A-overwrites-X*/
	goto _350
_341:
	; /* window_clause ::= WINDOW windowdefn_list */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	goto _350
_342:
	; /* filter_over ::= filter_clause over_clause */
	if *(*uintptr)(unsafe.Pointer(yymsp + 4)) != 0 {
		(*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 4)))).FpFilter = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	} else {
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))
	}
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_343:
	; /* filter_over ::= over_clause */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_344:
	; /* filter_over ::= filter_clause */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(100))
	if *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) != 0 {
		(*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))))).FeFrmType = uint8(TK_FILTER)
		(*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))))).FpFilter = *(*uintptr)(unsafe.Pointer(yymsp + 4))
	} else {
		_sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 4)))
	}
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_345:
	; /* over_clause ::= OVER LP window RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _350
_346:
	; /* over_clause ::= OVER nm */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(100))
	if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)) != 0 {
		(*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4)))).FzName = _sqlite3DbStrNDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 4)), uint64(*(*uint32)(unsafe.Pointer(yymsp + 4 + 4))))
	}
	goto _350
_347:
	; /* filter_clause ::= FILTER LP WHERE expr RP */
	*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*12 + 4)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*12 + 4))
	goto _350
_348:
	; /* term ::= QNUMBER */
	*(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))) = _tokenExpr(tls, pParse, int32((**(**TyyStackEntry)(__ccgo_up(yymsp))).Fmajor), *(*TToken)(unsafe.Pointer(yymsp + 4)))
	_sqlite3DequoteNumber(tls, pParse, *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp)))))
	*(*uintptr)(unsafe.Pointer(yymsp + 4)) = *(*uintptr)(unsafe.Pointer(&**(**TYYMINORTYPE)(__ccgo_up(bp))))
	goto _350
_349:
	;
	goto _350
	/********** End reduce actions ************************************************/
_350:
	;
	yygoto = int32(_yyRuleInfoLhs[yyruleno])
	yysize = int32(_yyRuleInfoNRhs[yyruleno])
	yyact = _yy_find_reduce_action(tls, (**(**TyyStackEntry)(__ccgo_up(yymsp + uintptr(yysize)*12))).Fstateno, uint16(yygoto))
	/* There are no SHIFTREDUCE actions on nonterminals because the table
	 ** generator has simplified them to pure REDUCE actions. */
	/* It is not possible for a REDUCE to be followed by an error */
	yymsp = yymsp + uintptr(yysize+int32(1))*12
	(*TyyParser)(unsafe.Pointer(yypParser)).Fyytos = yymsp
	(*TyyStackEntry)(unsafe.Pointer(yymsp)).Fstateno = yyact
	(*TyyStackEntry)(unsafe.Pointer(yymsp)).Fmajor = uint16(yygoto)
	return yyact
}

/*
** The following code executes when the parse fails
 */

// C documentation
//
//	/*
//	** Perform a shift action.
//	*/
func _yy_shift(tls *libc.TLS, yypParser uintptr, yyNewState uint16, yyMajor uint16, yyMinor TToken) {
	var yytos uintptr
	_ = yytos
	(*TyyParser)(unsafe.Pointer(yypParser)).Fyytos += 12
	yytos = (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos
	if yytos > (*TyyParser)(unsafe.Pointer(yypParser)).FyystackEnd {
		if _yyGrowStack(tls, yypParser) != 0 {
			(*TyyParser)(unsafe.Pointer(yypParser)).Fyytos -= 12
			_yyStackOverflow(tls, yypParser)
			return
		}
		yytos = (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos
	}
	if int32(yyNewState) > int32(YY_MAX_SHIFT) {
		yyNewState = uint16(int32(yyNewState) + (libc.Int32FromInt32(YY_MIN_REDUCE) - libc.Int32FromInt32(YY_MIN_SHIFTREDUCE)))
	}
	(*TyyStackEntry)(unsafe.Pointer(yytos)).Fstateno = yyNewState
	(*TyyStackEntry)(unsafe.Pointer(yytos)).Fmajor = yyMajor
	*(*TToken)(unsafe.Pointer(yytos + 4)) = yyMinor
}

// C documentation
//
//	/*
//	** Set up a raw page so that it looks like a database page holding
//	** no entries.
//	*/
func _zeroPage(tls *libc.TLS, pPage uintptr, flags int32) {
	var data, pBt uintptr
	var first, hdr, v1 int32
	_, _, _, _, _ = data, first, hdr, pBt, v1
	data = (*TMemPage)(unsafe.Pointer(pPage)).FaData
	pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt
	hdr = int32((*TMemPage)(unsafe.Pointer(pPage)).FhdrOffset)
	if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_FAST_SECURE) != 0 {
		libc.Xmemset(tls, data+uintptr(hdr), 0, (*TBtShared)(unsafe.Pointer(pBt)).FusableSize-uint32(hdr))
	}
	**(**uint8)(__ccgo_up(data + uintptr(hdr))) = uint8(int8(flags))
	if flags&int32(PTF_LEAF) == 0 {
		v1 = int32(12)
	} else {
		v1 = int32(8)
	}
	first = hdr + v1
	libc.Xmemset(tls, data+uintptr(hdr+int32(1)), 0, uint32(4))
	**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(7)))) = uint8(0)
	**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(5)))) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FusableSize >> libc.Int32FromInt32(8))
	**(**uint8)(__ccgo_up(data + uintptr(hdr+int32(5)) + 1)) = uint8((*TBtShared)(unsafe.Pointer(pBt)).FusableSize)
	(*TMemPage)(unsafe.Pointer(pPage)).FnFree = int32(uint16((*TBtShared)(unsafe.Pointer(pBt)).FusableSize - uint32(first)))
	_decodeFlags(tls, pPage, flags)
	(*TMemPage)(unsafe.Pointer(pPage)).FcellOffset = uint16(first)
	(*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd = data + uintptr((*TBtShared)(unsafe.Pointer(pBt)).FpageSize)
	(*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx = data + uintptr(first)
	(*TMemPage)(unsafe.Pointer(pPage)).FaDataOfst = data + uintptr((*TMemPage)(unsafe.Pointer(pPage)).FchildPtrSize)
	(*TMemPage)(unsafe.Pointer(pPage)).FnOverflow = uint8(0)
	(*TMemPage)(unsafe.Pointer(pPage)).FmaskPage = uint16((*TBtShared)(unsafe.Pointer(pBt)).FpageSize - libc.Uint32FromInt32(1))
	(*TMemPage)(unsafe.Pointer(pPage)).FnCell = uint16(0)
	(*TMemPage)(unsafe.Pointer(pPage)).FisInit = uint8(1)
}

func init() {
	p := unsafe.Pointer(&_aSyscall)
	*(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(libc.XAreFileApisANSI)
	*(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(libc.XCloseHandle)
	*(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(libc.XCreateFileA)
	*(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(libc.XCreateFileW)
	*(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(libc.XCreateFileMappingW)
	*(*uintptr)(unsafe.Add(p, 100)) = __ccgo_fp(libc.XCreateMutexW)
	*(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(libc.XDeleteFileA)
	*(*uintptr)(unsafe.Add(p, 124)) = __ccgo_fp(libc.XDeleteFileW)
	*(*uintptr)(unsafe.Add(p, 160)) = __ccgo_fp(libc.XFlushFileBuffers)
	*(*uintptr)(unsafe.Add(p, 172)) = __ccgo_fp(libc.XFormatMessageA)
	*(*uintptr)(unsafe.Add(p, 184)) = __ccgo_fp(libc.XFormatMessageW)
	*(*uintptr)(unsafe.Add(p, 196)) = __ccgo_fp(libc.XFreeLibrary)
	*(*uintptr)(unsafe.Add(p, 208)) = __ccgo_fp(libc.XGetCurrentProcessId)
	*(*uintptr)(unsafe.Add(p, 220)) = __ccgo_fp(libc.XGetDiskFreeSpaceA)
	*(*uintptr)(unsafe.Add(p, 232)) = __ccgo_fp(libc.XGetDiskFreeSpaceW)
	*(*uintptr)(unsafe.Add(p, 244)) = __ccgo_fp(libc.XGetFileAttributesA)
	*(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(libc.XGetFileAttributesW)
	*(*uintptr)(unsafe.Add(p, 268)) = __ccgo_fp(libc.XGetFileAttributesExW)
	*(*uintptr)(unsafe.Add(p, 280)) = __ccgo_fp(libc.XGetFileSize)
	*(*uintptr)(unsafe.Add(p, 292)) = __ccgo_fp(libc.XGetFullPathNameA)
	*(*uintptr)(unsafe.Add(p, 304)) = __ccgo_fp(libc.XGetFullPathNameW)
	*(*uintptr)(unsafe.Add(p, 316)) = __ccgo_fp(libc.XGetLastError)
	*(*uintptr)(unsafe.Add(p, 328)) = __ccgo_fp(libc.XGetProcAddress)
	*(*uintptr)(unsafe.Add(p, 340)) = __ccgo_fp(libc.XGetSystemInfo)
	*(*uintptr)(unsafe.Add(p, 352)) = __ccgo_fp(libc.XGetSystemTime)
	*(*uintptr)(unsafe.Add(p, 364)) = __ccgo_fp(libc.XGetSystemTimeAsFileTime)
	*(*uintptr)(unsafe.Add(p, 376)) = __ccgo_fp(libc.XGetTempPathA)
	*(*uintptr)(unsafe.Add(p, 388)) = __ccgo_fp(libc.XGetTempPathW)
	*(*uintptr)(unsafe.Add(p, 400)) = __ccgo_fp(libc.XGetTickCount)
	*(*uintptr)(unsafe.Add(p, 436)) = __ccgo_fp(libc.XHeapAlloc)
	*(*uintptr)(unsafe.Add(p, 448)) = __ccgo_fp(libc.XHeapCreate)
	*(*uintptr)(unsafe.Add(p, 460)) = __ccgo_fp(libc.XHeapDestroy)
	*(*uintptr)(unsafe.Add(p, 472)) = __ccgo_fp(libc.XHeapFree)
	*(*uintptr)(unsafe.Add(p, 484)) = __ccgo_fp(libc.XHeapReAlloc)
	*(*uintptr)(unsafe.Add(p, 496)) = __ccgo_fp(libc.XHeapSize)
	*(*uintptr)(unsafe.Add(p, 508)) = __ccgo_fp(libc.XHeapValidate)
	*(*uintptr)(unsafe.Add(p, 520)) = __ccgo_fp(libc.XHeapCompact)
	*(*uintptr)(unsafe.Add(p, 532)) = __ccgo_fp(libc.XLoadLibraryA)
	*(*uintptr)(unsafe.Add(p, 544)) = __ccgo_fp(libc.XLoadLibraryW)
	*(*uintptr)(unsafe.Add(p, 556)) = __ccgo_fp(libc.XLocalFree)
	*(*uintptr)(unsafe.Add(p, 568)) = __ccgo_fp(libc.XLockFile)
	*(*uintptr)(unsafe.Add(p, 580)) = __ccgo_fp(libc.XLockFileEx)
	*(*uintptr)(unsafe.Add(p, 592)) = __ccgo_fp(libc.XMapViewOfFile)
	*(*uintptr)(unsafe.Add(p, 604)) = __ccgo_fp(libc.XMultiByteToWideChar)
	*(*uintptr)(unsafe.Add(p, 616)) = __ccgo_fp(libc.XQueryPerformanceCounter)
	*(*uintptr)(unsafe.Add(p, 628)) = __ccgo_fp(libc.XReadFile)
	*(*uintptr)(unsafe.Add(p, 640)) = __ccgo_fp(libc.XSetEndOfFile)
	*(*uintptr)(unsafe.Add(p, 652)) = __ccgo_fp(libc.XSetFilePointer)
	*(*uintptr)(unsafe.Add(p, 664)) = __ccgo_fp(libc.XSleep)
	*(*uintptr)(unsafe.Add(p, 676)) = __ccgo_fp(libc.XSystemTimeToFileTime)
	*(*uintptr)(unsafe.Add(p, 688)) = __ccgo_fp(libc.XUnlockFile)
	*(*uintptr)(unsafe.Add(p, 700)) = __ccgo_fp(libc.XUnlockFileEx)
	*(*uintptr)(unsafe.Add(p, 712)) = __ccgo_fp(libc.XUnmapViewOfFile)
	*(*uintptr)(unsafe.Add(p, 724)) = __ccgo_fp(libc.XWideCharToMultiByte)
	*(*uintptr)(unsafe.Add(p, 736)) = __ccgo_fp(libc.XWriteFile)
	*(*uintptr)(unsafe.Add(p, 748)) = __ccgo_fp(libc.XWaitForSingleObject)
	*(*uintptr)(unsafe.Add(p, 760)) = __ccgo_fp(libc.XWaitForSingleObjectEx)
	*(*uintptr)(unsafe.Add(p, 784)) = __ccgo_fp(libc.XOutputDebugStringA)
	*(*uintptr)(unsafe.Add(p, 796)) = __ccgo_fp(libc.XOutputDebugStringW)
	*(*uintptr)(unsafe.Add(p, 808)) = __ccgo_fp(libc.XGetProcessHeap)
	*(*uintptr)(unsafe.Add(p, 856)) = __ccgo_fp(libc.XFlushViewOfFile)
	*(*uintptr)(unsafe.Add(p, 892)) = __ccgo_fp(libc.XGetModuleHandleW)
}

/* End of the overrideable system calls */

func init() {
	p := unsafe.Pointer(&_winNolockVfs)
	*(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_winOpen)
	*(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_winDelete)
	*(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_winAccess)
	*(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_winFullPathname)
	*(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_winDlOpen)
	*(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_winDlError)
	*(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_winDlSym)
	*(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_winDlClose)
	*(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_winRandomness)
	*(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_winSleep)
	*(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_winCurrentTime)
	*(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_winGetLastError)
	*(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_winCurrentTimeInt64)
	*(*uintptr)(unsafe.Add(p, 76)) = __ccgo_fp(_winSetSystemCall)
	*(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_winGetSystemCall)
	*(*uintptr)(unsafe.Add(p, 84)) = __ccgo_fp(_winNextSystemCall)
}

func init() {
	p := unsafe.Pointer(&_winIoMethod)
	*(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_winClose)
	*(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_winRead)
	*(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_winWrite)
	*(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_winTruncate)
	*(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_winSync)
	*(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_winFileSize)
	*(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_winLock)
	*(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_winUnlock)
	*(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_winCheckReservedLock)
	*(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_winFileControl)
	*(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_winSectorSize)
	*(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_winDeviceCharacteristics)
	*(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_winShmMap)
	*(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_winShmLock)
	*(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_winShmBarrier)
	*(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_winShmUnmap)
	*(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_winFetch)
	*(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_winUnfetch)
}

func init() {
	p := unsafe.Pointer(&_winVfs)
	*(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_winOpen)
	*(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_winDelete)
	*(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_winAccess)
	*(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_winFullPathname)
	*(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_winDlOpen)
	*(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_winDlError)
	*(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_winDlSym)
	*(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_winDlClose)
	*(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_winRandomness)
	*(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_winSleep)
	*(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_winCurrentTime)
	*(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_winGetLastError)
	*(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_winCurrentTimeInt64)
	*(*uintptr)(unsafe.Add(p, 76)) = __ccgo_fp(_winSetSystemCall)
	*(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_winGetSystemCall)
	*(*uintptr)(unsafe.Add(p, 84)) = __ccgo_fp(_winNextSystemCall)
}

func init() {
	p := unsafe.Pointer(&_winIoNolockMethod)
	*(*uintptr)(unsafe.Add(p, 4)) = __ccgo_fp(_winClose)
	*(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_winRead)
	*(*uintptr)(unsafe.Add(p, 12)) = __ccgo_fp(_winWrite)
	*(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_winTruncate)
	*(*uintptr)(unsafe.Add(p, 20)) = __ccgo_fp(_winSync)
	*(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_winFileSize)
	*(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_winNolockLock)
	*(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_winNolockUnlock)
	*(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_winNolockCheckReservedLock)
	*(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_winFileControl)
	*(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_winSectorSize)
	*(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_winDeviceCharacteristics)
	*(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_winShmMap)
	*(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_winShmLock)
	*(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_winShmBarrier)
	*(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_winShmUnmap)
	*(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_winFetch)
	*(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_winUnfetch)
}

func init() {
	p := unsafe.Pointer(&_winLongPathNolockVfs)
	*(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_winOpen)
	*(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_winDelete)
	*(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_winAccess)
	*(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_winFullPathname)
	*(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_winDlOpen)
	*(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_winDlError)
	*(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_winDlSym)
	*(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_winDlClose)
	*(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_winRandomness)
	*(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_winSleep)
	*(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_winCurrentTime)
	*(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_winGetLastError)
	*(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_winCurrentTimeInt64)
	*(*uintptr)(unsafe.Add(p, 76)) = __ccgo_fp(_winSetSystemCall)
	*(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_winGetSystemCall)
	*(*uintptr)(unsafe.Add(p, 84)) = __ccgo_fp(_winNextSystemCall)
}

func init() {
	p := unsafe.Pointer(&_winLongPathVfs)
	*(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_winOpen)
	*(*uintptr)(unsafe.Add(p, 28)) = __ccgo_fp(_winDelete)
	*(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_winAccess)
	*(*uintptr)(unsafe.Add(p, 36)) = __ccgo_fp(_winFullPathname)
	*(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_winDlOpen)
	*(*uintptr)(unsafe.Add(p, 44)) = __ccgo_fp(_winDlError)
	*(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_winDlSym)
	*(*uintptr)(unsafe.Add(p, 52)) = __ccgo_fp(_winDlClose)
	*(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_winRandomness)
	*(*uintptr)(unsafe.Add(p, 60)) = __ccgo_fp(_winSleep)
	*(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_winCurrentTime)
	*(*uintptr)(unsafe.Add(p, 68)) = __ccgo_fp(_winGetLastError)
	*(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_winCurrentTimeInt64)
	*(*uintptr)(unsafe.Add(p, 76)) = __ccgo_fp(_winSetSystemCall)
	*(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_winGetSystemCall)
	*(*uintptr)(unsafe.Add(p, 84)) = __ccgo_fp(_winNextSystemCall)
}

type t__WIDL_wtypes_generated_name_00000001 = struct {
	FpwszName [0]uintptr
	FdwValue  TDWORD
}

type t__WIDL_wtypes_generated_name_00000002 = struct {
	FhRemote [0]uintptr
	FhInproc TLONG
}

type t__WIDL_wtypes_generated_name_00000003 = struct {
	F__ccgo_align [0]uint32
	FhRemote      [0]uintptr
	FhInproc64    [0]TINT64
	FhInproc      TLONG
	F__ccgo_pad3  [4]byte
}

type t__WIDL_wtypes_generated_name_00000004 = struct {
	F__ccgo_align [0]uint32
	FhRemote      [0]uintptr
	FhInproc64    [0]TINT64
	FhInproc      TLONG
	F__ccgo_pad3  [4]byte
}

type t__WIDL_wtypes_generated_name_00000005 = struct {
	F__ccgo_align [0]uint32
	FhRemote      [0]uintptr
	FhInproc64    [0]TINT64
	FhInproc      TLONG
	F__ccgo_pad3  [4]byte
}

type t__WIDL_wtypes_generated_name_00000006 = struct {
	F__ccgo_align [0]uint32
	FhRemote      [0]uintptr
	FhInproc64    [0]TINT64
	FhInproc      TLONG
	F__ccgo_pad3  [4]byte
}

type t__WIDL_wtypes_generated_name_00000007 = struct {
	F__ccgo_align [0]uint32
	FhRemote      [0]uintptr
	FhInproc64    [0]TINT64
	FhInproc      TLONG
	F__ccgo_pad3  [4]byte
}

type t__WIDL_wtypes_generated_name_00000008 = struct {
	F__ccgo_align [0]uint32
	FhRemote      [0]uintptr
	FhInproc64    [0]TINT64
	FhInproc      TLONG
	F__ccgo_pad3  [4]byte
}

type t__mingw_dbl_type_t = struct {
	F__ccgo_align [0]uint32
	Fval          [0]uint64
	Flh           [0]struct {
		Flow  uint32
		Fhigh uint32
	}
	Fx float64
}

type t__mingw_ldbl_type_t = struct {
	F__ccgo_align [0]uint32
	Flh           [0]struct {
		Flow      uint32
		Fhigh     uint32
		F__ccgo8  uint32
		F__ccgo12 uint32
	}
	Fx           float64
	F__ccgo_pad2 [8]byte
}

type t__timeb64 = struct {
	F__ccgo_align [0]uint32
	Ftime         t__time64_t
	Fmillitm      uint16
	Ftimezone     int16
	Fdstflag      int16
	F__ccgo_pad4  [2]byte
}

const ua_CharUpperW = 0

const ua_lstrcmpW = 0

const ua_lstrcmpiW = 0

const ua_lstrlenW = 0

const ua_wcschr = 0

const ua_wcscpy = 0

const ua_wcsicmp = 0

const ua_wcslen = 0

const ua_wcsrchr = 0
