// Code generated by modernc.org/undup from the per-target sqlite_*.go files; DO NOT EDIT.

//go:build (freebsd && 386) || (linux && 386) || (windows && 386)

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

type TColumn = struct {
	FzCnName  uintptr
	F__ccgo4  uint8
	Faffinity int8
	FszEst    Tu8
	FhName    Tu8
	FiDflt    Tu16
	FcolFlags Tu16
}

type TExpr = struct {
	Fop      Tu8
	FaffExpr int8
	Fop2     Tu8
	Fflags   Tu32
	Fu       struct {
		FiValue [0]int32
		FzToken uintptr
	}
	FpLeft  uintptr
	FpRight uintptr
	Fx      struct {
		FpSelect [0]uintptr
		FpList   uintptr
	}
	FnHeight int32
	FiTable  int32
	FiColumn TynVar
	FiAgg    Ti16
	Fw       struct {
		FiOfst [0]int32
		FiJoin int32
	}
	FpAggInfo uintptr
	Fy        struct {
		FpWin [0]uintptr
		FnReg [0]int32
		Fsub  [0]struct {
			FiAddr     int32
			FregReturn int32
		}
		FpTab        uintptr
		F__ccgo_pad4 [4]byte
	}
}

type Tfloat_t = float64

// C documentation
//
//	/*
//	** Return the sqlite3_file for the main database given the name
//	** of the corresponding WAL or Journal name as passed into
//	** xOpen.
//	*/
func Xsqlite3_database_file_object(tls *libc.TLS, zName uintptr) (r uintptr) {
	var p, pPager uintptr
	_, _ = p, pPager
	for int32(**(**int8)(__ccgo_up(zName + uintptr(-libc.Int32FromInt32(1))))) != 0 || int32(**(**int8)(__ccgo_up(zName + uintptr(-libc.Int32FromInt32(2))))) != 0 || int32(**(**int8)(__ccgo_up(zName + uintptr(-libc.Int32FromInt32(3))))) != 0 || int32(**(**int8)(__ccgo_up(zName + uintptr(-libc.Int32FromInt32(4))))) != 0 {
		zName = zName - 1
	}
	p = zName - uintptr(4) - uintptr(4)
	pPager = **(**uintptr)(__ccgo_up(p))
	return (*TPager)(unsafe.Pointer(pPager)).Ffd
}

// C documentation
//
//	/*
//	** Print into memory obtained from sqlite3_malloc().  Omit the internal
//	** %-conversion extensions.
//	*/
func Xsqlite3_vmprintf(tls *libc.TLS, 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
	if Xsqlite3_initialize(tls) != 0 {
		return uintptr(0)
	}
	_sqlite3StrAccumInit(tls, bp+72, uintptr(0), bp, int32(70), int32(SQLITE_MAX_LENGTH))
	Xsqlite3_str_vappendf(tls, bp+72, zFormat, ap)
	z = _sqlite3StrAccumFinish(tls, bp+72)
	return z
}

const __i386 = 1

const __i386__ = 1

const __i686 = 1

const __i686__ = 1

const __pentiumpro = 1

const __pentiumpro__ = 1

// C documentation
//
//	/*
//	** pExpr is a comparison operator.  Return the type affinity that should
//	** be applied to both operands prior to doing the comparison.
//	*/
func _comparisonAffinity(tls *libc.TLS, pExpr uintptr) (r int8) {
	var aff int8
	_ = aff
	aff = _sqlite3ExprAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft)
	if (*TExpr)(unsafe.Pointer(pExpr)).FpRight != 0 {
		aff = _sqlite3CompareAffinity(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight, aff)
	} else {
		if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) {
			aff = _sqlite3CompareAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 20)))).FpEList + 8))).FpExpr, aff)
		} else {
			if int32(aff) == 0 {
				aff = int8(SQLITE_AFF_BLOB)
			}
		}
	}
	return aff
}

// C documentation
//
//	/*
//	** Return true if any of the result-set columns in the compound query
//	** have incompatible affinities on one or more arms of the compound.
//	*/
func _compoundHasDifferentAffinities(tls *libc.TLS, p uintptr) (r int32) {
	var aff int8
	var ii int32
	var pList, pSub1 uintptr
	_, _, _, _ = aff, ii, pList, pSub1
	pList = (*TSelect)(unsafe.Pointer(p)).FpEList
	ii = 0
	for {
		if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) {
			break
		}
		aff = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*20))).FpExpr)
		pSub1 = (*TSelect)(unsafe.Pointer(p)).FpPrior
		for {
			if !(pSub1 != 0) {
				break
			}
			if int32(_sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub1)).FpEList + 8 + uintptr(ii)*20))).FpExpr)) != int32(aff) {
				return int32(1)
			}
			goto _2
		_2:
			;
			pSub1 = (*TSelect)(unsafe.Pointer(pSub1)).FpPrior
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	return 0
}

// C documentation
//
//	/*
//	** Return true if all expressions in the expression-list passed as the
//	** only argument are both constant and have no affinity.
//	*/
func _exprListIsNoAffinity(tls *libc.TLS, pParse uintptr, pRow uintptr) (r int32) {
	var ii int32
	var pExpr uintptr
	_, _ = ii, pExpr
	if _exprListIsConstant(tls, pParse, pRow) == 0 {
		return 0
	}
	ii = 0
	for {
		if !(ii < (*TExprList)(unsafe.Pointer(pRow)).FnExpr) {
			break
		}
		pExpr = (*(*TExprList_item)(unsafe.Pointer(pRow + 8 + uintptr(ii)*20))).FpExpr
		if 0 != int32(_sqlite3ExprAffinity(tls, pExpr)) {
			return 0
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** Return an Expr object that refers to a memory register corresponding
//	** to column iCol of table pTab.
//	**
//	** regBase is the first of an array of register that contains the data
//	** for pTab.  regBase itself holds the rowid.  regBase+1 holds the first
//	** column.  regBase+2 holds the second column, and so forth.
//	*/
func _exprTableRegister(tls *libc.TLS, pParse uintptr, pTab uintptr, regBase int32, iCol Ti16) (r uintptr) {
	var db, pCol, pExpr, zColl uintptr
	_, _, _, _ = db, pCol, pExpr, zColl
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	pExpr = _sqlite3Expr(tls, db, int32(TK_REGISTER), uintptr(0))
	if pExpr != 0 {
		if int32(iCol) >= 0 && int32(iCol) != int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) {
			pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12
			(*TExpr)(unsafe.Pointer(pExpr)).FiTable = regBase + int32(_sqlite3TableColumnToStorage(tls, pTab, iCol)) + int32(1)
			(*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = (*TColumn)(unsafe.Pointer(pCol)).Faffinity
			zColl = _sqlite3ColumnColl(tls, pCol)
			if zColl == uintptr(0) {
				zColl = (*TCollSeq)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl)).FzName
			}
			pExpr = _sqlite3ExprAddCollateString(tls, pParse, pExpr, zColl)
		} else {
			(*TExpr)(unsafe.Pointer(pExpr)).FiTable = regBase
			(*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = int8(SQLITE_AFF_INTEGER)
		}
	}
	return pExpr
}

// C documentation
//
//	/*
//	** Process time function arguments.  argv[0] is a date-time stamp.
//	** argv[1] and following are modifiers.  Parse them all and write
//	** the resulting time into the DateTime structure p.  Return 0
//	** on success and 1 if there are any errors.
//	**
//	** If there are zero parameters (if even argv[0] is undefined)
//	** then assume a default value of "now" for argv[0].
//	*/
func _isDate(tls *libc.TLS, context uintptr, argc int32, argv uintptr, p uintptr) (r int32) {
	var eType, i, n, v1 int32
	var z uintptr
	_, _, _, _, _ = eType, i, n, z, v1
	libc.Xmemset(tls, p, 0, uint32(48))
	if argc == 0 {
		if !(_sqlite3NotPureFunc(tls, context) != 0) {
			return int32(1)
		}
		return _setDateTimeToCurrent(tls, context, p)
	}
	v1 = Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv)))
	eType = v1
	if v1 == int32(SQLITE_FLOAT) || eType == int32(SQLITE_INTEGER) {
		_setRawDateNumber(tls, p, Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv))))
	} else {
		z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv)))
		if !(z != 0) || _parseDateOrTime(tls, context, z, p) != 0 {
			return int32(1)
		}
	}
	i = int32(1)
	for {
		if !(i < argc) {
			break
		}
		z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
		n = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
		if z == uintptr(0) || _parseModifier(tls, context, z, n, p, i) != 0 {
			return int32(1)
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	_computeJD(tls, p)
	if int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x2>>1)) != 0 || !(_validJulianDay(tls, (*TDateTime)(unsafe.Pointer(p)).FiJD) != 0) {
		return int32(1)
	}
	if argc == int32(1) && (*TDateTime)(unsafe.Pointer(p)).FvalidYMD != 0 && (*TDateTime)(unsafe.Pointer(p)).FD > int32(28) {
		/* Make sure a YYYY-MM-DD is normalized.
		 ** Example: 2023-02-31 -> 2023-03-03 */
		(*TDateTime)(unsafe.Pointer(p)).FvalidYMD = 0
	}
	return 0
}

/*
** The following routines implement the various date and time functions
** of SQLite.
 */

func _jsonAppendChar(tls *libc.TLS, p uintptr, c int8) {
	var v1 Tu64
	var v2 uintptr
	_, _ = v1, v2
	if (*TJsonString)(unsafe.Pointer(p)).FnUsed >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc {
		_jsonAppendCharExpand(tls, p, c)
	} else {
		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))) = c
	}
}

// C documentation
//
//	/* Append a single character
//	*/
func _jsonAppendCharExpand(tls *libc.TLS, p uintptr, c int8) {
	var v1 Tu64
	var v2 uintptr
	_, _ = v1, v2
	if _jsonStringGrow(tls, p, uint32(1)) != 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))) = c
}

// C documentation
//
//	/*
//	** Implementation of the json_array(VALUE,...) function.  Return a JSON
//	** array that contains all values given in arguments.  Or if any argument
//	** is a BLOB, throw an error.
//	*/
func _jsonArrayFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(128)
	defer tls.Free(128)
	var i int32
	var _ /* jx at bp+0 */ TJsonString
	_ = i
	_jsonStringInit(tls, bp, ctx)
	_jsonAppendChar(tls, bp, int8('['))
	i = 0
	for {
		if !(i < argc) {
			break
		}
		_jsonAppendSeparator(tls, bp)
		_jsonAppendSqlValue(tls, bp, **(**uintptr)(__ccgo_up(argv + uintptr(i)*4)))
		goto _1
	_1:
		;
		i = i + 1
	}
	_jsonAppendChar(tls, bp, int8(']'))
	_jsonReturnString(tls, bp, uintptr(0), uintptr(0))
	Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
}

// C documentation
//
//	/****************************************************************************
//	** Aggregate SQL function implementations
//	****************************************************************************/
//	/*
//	** json_group_array(VALUE)
//	**
//	** Return a JSON array composed of all values in the aggregate.
//	*/
func _jsonArrayStep(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	var pStr uintptr
	_ = pStr
	_ = argc
	pStr = Xsqlite3_aggregate_context(tls, ctx, int32(128))
	if pStr != 0 {
		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
		_jsonAppendSqlValue(tls, pStr, **(**uintptr)(__ccgo_up(argv)))
	}
}

// C documentation
//
//	/*
//	** This method works for both json_group_array() and json_group_object().
//	** It works by removing the first element of the group by searching forward
//	** to the first comma (",") that is not within a string and deleting all
//	** text through that comma.
//	*/
func _jsonGroupInverse(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
	var c, v2 int8
	var i uint32
	var inStr, nNest int32
	var pStr, z uintptr
	var v3 bool
	_, _, _, _, _, _, _, _ = c, i, inStr, nNest, pStr, z, v2, v3
	inStr = 0
	nNest = 0
	_ = argc
	_ = argv
	pStr = Xsqlite3_aggregate_context(tls, ctx, 0)
	/* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
	 ** always have been called to initialize it */
	if !(pStr != 0) {
		return
	}
	z = (*TJsonString)(unsafe.Pointer(pStr)).FzBuf
	i = uint32(1)
	for {
		if v3 = uint64(i) < (*TJsonString)(unsafe.Pointer(pStr)).FnUsed; v3 {
			v2 = **(**int8)(__ccgo_up(z + uintptr(i)))
			c = v2
		}
		if !(v3 && (int32(v2) != int32(',') || inStr != 0 || nNest != 0)) {
			break
		}
		if int32(c) == int32('"') {
			inStr = libc.BoolInt32(!(inStr != 0))
		} else {
			if int32(c) == int32('\\') {
				i = i + 1
			} else {
				if !(inStr != 0) {
					if int32(c) == int32('{') || int32(c) == int32('[') {
						nNest = nNest + 1
					}
					if int32(c) == int32('}') || int32(c) == int32(']') {
						nNest = nNest - 1
					}
				}
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if uint64(i) < (*TJsonString)(unsafe.Pointer(pStr)).FnUsed {
		**(**Tu64)(__ccgo_up(pStr + 16)) -= uint64(i)
		libc.Xmemmove(tls, z+1, z+uintptr(i+uint32(1)), uint32((*TJsonString)(unsafe.Pointer(pStr)).FnUsed)-uint32(1))
		**(**int8)(__ccgo_up(z + uintptr((*TJsonString)(unsafe.Pointer(pStr)).FnUsed))) = 0
	} else {
		(*TJsonString)(unsafe.Pointer(pStr)).FnUsed = uint64(1)
	}
}

// C documentation
//
//	/*
//	** This is the routine that actually formats the sqlite3_log() message.
//	** We house it in a separate routine from sqlite3_log() to avoid using
//	** stack space on small-stack systems when logging is disabled.
//	**
//	** sqlite3_log() must render into a static buffer.  It cannot dynamically
//	** allocate memory because it might be called while the memory allocator
//	** mutex is held.
//	**
//	** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
//	** certain format characters (%q) or for very large precisions or widths.
//	** Care must be taken that any sqlite3_log() calls that occur while the
//	** memory mutex is held do not use these mechanisms.
//	*/
func _renderLogMsg(tls *libc.TLS, iErrCode int32, zFormat uintptr, ap Tva_list) {
	bp := tls.Alloc(736)
	defer tls.Free(736)
	var _ /* acc at bp+0 */ TStrAccum
	var _ /* zMsg at bp+24 */ [700]int8 /* Complete log message */
	_sqlite3StrAccumInit(tls, bp, uintptr(0), bp+24, int32(700), 0)
	Xsqlite3_str_vappendf(tls, bp, zFormat, ap)
	(*(*func(*libc.TLS, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.FxLog})))(tls, _sqlite3Config.FpLogArg, iErrCode, _sqlite3StrAccumFinish(tls, bp))
}

// C documentation
//
//	/*
//	** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
//	** interface.
//	**
//	** For each FROM-clause subquery, add Column.zType, Column.zColl, and
//	** Column.affinity information to the Table structure that represents
//	** the result set of that subquery.
//	**
//	** The Table structure that represents the result set was constructed
//	** by selectExpander() but the type and collation and affinity information
//	** was omitted at that point because identifiers had not yet been resolved.
//	** This routine is called after identifier resolution.
//	*/
func _selectAddSubqueryTypeInfo(tls *libc.TLS, pWalker uintptr, p uintptr) {
	var i int32
	var pFrom, pParse, pSel, pTab, pTabList uintptr
	_, _, _, _, _, _ = i, pFrom, pParse, pSel, pTab, pTabList
	if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_HasTypeInfo) != 0 {
		return
	}
	**(**Tu32)(__ccgo_up(p + 4)) |= uint32(SF_HasTypeInfo)
	pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse
	pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc
	i = 0
	pFrom = pTabList + 8
	for {
		if !(i < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) {
			break
		}
		pTab = (*TSrcItem)(unsafe.Pointer(pFrom)).FpSTab
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Ephemeral) != uint32(0) && int32(*(*uint32)(unsafe.Pointer(pFrom + 12 + 4))&0x4>>2) != 0 {
			/* A sub-query in the FROM clause of a SELECT */
			pSel = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pFrom + 44)))).FpSelect
			_sqlite3SubqueryColumnTypes(tls, pParse, pTab, pSel, int8(SQLITE_AFF_NONE))
		}
		goto _1
	_1:
		;
		i = i + 1
		pFrom += 48
	}
}

// C documentation
//
//	/*
//	** This function is a no-op if *pRc is other than SQLITE_OK when it is
//	** called. Otherwise, append the string zStr enclosed in quotes (") and
//	** with any embedded quote characters escaped to the buffer. No
//	** nul-terminator byte is written.
//	**
//	** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
//	** returning.
//	*/
func _sessionAppendIdent(tls *libc.TLS, p uintptr, zStr uintptr, pRc uintptr) {
	var nStr int32
	var zIn, zOut, v1, v2 uintptr
	_, _, _, _, _ = nStr, zIn, zOut, v1, v2
	nStr = _sqlite3Strlen30(tls, zStr)*int32(2) + int32(2) + int32(2)
	if 0 == _sessionBufferGrow(tls, p, int64(nStr), pRc) {
		zOut = (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf)
		zIn = zStr
		v1 = zOut
		zOut = zOut + 1
		**(**int8)(__ccgo_up(v1)) = int8('"')
		if zIn != uintptr(0) {
			for **(**int8)(__ccgo_up(zIn)) != 0 {
				if int32(**(**int8)(__ccgo_up(zIn))) == int32('"') {
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8('"')
				}
				v1 = zOut
				zOut = zOut + 1
				v2 = zIn
				zIn = zIn + 1
				**(**int8)(__ccgo_up(v1)) = **(**int8)(__ccgo_up(v2))
			}
		}
		v1 = zOut
		zOut = zOut + 1
		**(**int8)(__ccgo_up(v1)) = int8('"')
		(*TSessionBuffer)(unsafe.Pointer(p)).FnBuf = int32(zOut) - int32((*TSessionBuffer)(unsafe.Pointer(p)).FaBuf)
		**(**Tu8)(__ccgo_up((*TSessionBuffer)(unsafe.Pointer(p)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf))) = uint8(0x00)
	}
}

func _sqlite3DbStrNDup(tls *libc.TLS, db uintptr, z uintptr, n Tu64) (r uintptr) {
	var zNew, v1 uintptr
	_, _ = zNew, v1
	if z != 0 {
		v1 = _sqlite3DbMallocRawNN(tls, db, n+uint64(1))
	} else {
		v1 = uintptr(0)
	}
	zNew = v1
	if zNew != 0 {
		libc.Xmemcpy(tls, zNew, z, uint32(n))
		**(**int8)(__ccgo_up(zNew + uintptr(n))) = 0
	}
	return zNew
}

// C documentation
//
//	/*
//	** Insert a new FuncDef into a FuncDefHash hash table.
//	*/
func _sqlite3InsertBuiltinFuncs(tls *libc.TLS, aDef uintptr, nDef int32) {
	var h, i, nName int32
	var pOther, zName uintptr
	_, _, _, _, _ = h, i, nName, pOther, zName
	i = 0
	for {
		if !(i < nDef) {
			break
		}
		zName = (**(**TFuncDef)(__ccgo_up(aDef + uintptr(i)*40))).FzName
		nName = _sqlite3Strlen30(tls, zName)
		h = (int32(**(**int8)(__ccgo_up(zName))) + nName) % int32(SQLITE_FUNC_HASH_SZ)
		pOther = _sqlite3FunctionSearch(tls, h, zName)
		if pOther != 0 {
			(**(**TFuncDef)(__ccgo_up(aDef + uintptr(i)*40))).FpNext = (*TFuncDef)(unsafe.Pointer(pOther)).FpNext
			(*TFuncDef)(unsafe.Pointer(pOther)).FpNext = aDef + uintptr(i)*40
		} else {
			(**(**TFuncDef)(__ccgo_up(aDef + uintptr(i)*40))).FpNext = uintptr(0)
			*(*uintptr)(unsafe.Pointer(aDef + uintptr(i)*40 + 36)) = **(**uintptr)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(h)*4))
			**(**uintptr)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(h)*4)) = aDef + uintptr(i)*40
		}
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Subqueries store the original database, table and column names for their
//	** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN",
//	** and mark the expression-list item by setting ExprList.a[].fg.eEName
//	** to ENAME_TAB.
//	**
//	** Check to see if the zSpan/eEName of the expression-list item passed to this
//	** routine matches the zDb, zTab, and zCol.  If any of zDb, zTab, and zCol are
//	** NULL then those fields will match anything. Return true if there is a match,
//	** or false otherwise.
//	**
//	** SF_NestedFrom subqueries also store an entry for the implicit rowid (or
//	** _rowid_, or oid) column by setting ExprList.a[].fg.eEName to ENAME_ROWID,
//	** and setting zSpan to "DATABASE.TABLE.<rowid-alias>". This type of pItem
//	** argument matches if zCol is a rowid alias. If it is not NULL, (*pbRowid)
//	** is set to 1 if there is this kind of match.
//	*/
func _sqlite3MatchEName(tls *libc.TLS, pItem uintptr, zCol uintptr, zTab uintptr, zDb uintptr, pbRowid uintptr) (r int32) {
	var eEName, n int32
	var zSpan uintptr
	_, _, _ = eEName, n, zSpan
	eEName = int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 8 + 4)) & 0x3 >> 0))
	if eEName != int32(ENAME_TAB) && (eEName != int32(ENAME_ROWID) || pbRowid == uintptr(0)) {
		return 0
	}
	zSpan = (*TExprList_item)(unsafe.Pointer(pItem)).FzEName
	n = 0
	for {
		if !(**(**int8)(__ccgo_up(zSpan + uintptr(n))) != 0 && int32(**(**int8)(__ccgo_up(zSpan + uintptr(n)))) != int32('.')) {
			break
		}
		goto _1
	_1:
		;
		n = n + 1
	}
	if zDb != 0 && (Xsqlite3_strnicmp(tls, zSpan, zDb, n) != 0 || int32(**(**int8)(__ccgo_up(zDb + uintptr(n)))) != 0) {
		return 0
	}
	zSpan = zSpan + uintptr(n+int32(1))
	n = 0
	for {
		if !(**(**int8)(__ccgo_up(zSpan + uintptr(n))) != 0 && int32(**(**int8)(__ccgo_up(zSpan + uintptr(n)))) != int32('.')) {
			break
		}
		goto _2
	_2:
		;
		n = n + 1
	}
	if zTab != 0 && (Xsqlite3_strnicmp(tls, zSpan, zTab, n) != 0 || int32(**(**int8)(__ccgo_up(zTab + uintptr(n)))) != 0) {
		return 0
	}
	zSpan = zSpan + uintptr(n+int32(1))
	if zCol != 0 {
		if eEName == int32(ENAME_TAB) && _sqlite3StrICmp(tls, zSpan, zCol) != 0 {
			return 0
		}
		if eEName == int32(ENAME_ROWID) && _sqlite3IsRowid(tls, zCol) == 0 {
			return 0
		}
	}
	if eEName == int32(ENAME_ROWID) {
		**(**int32)(__ccgo_up(pbRowid)) = int32(1)
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** Run the parser and code generator recursively in order to generate
//	** code for the SQL statement given onto the end of the pParse context
//	** currently under construction.  Notes:
//	**
//	**   *  The final OP_Halt is not appended and other initialization
//	**      and finalization steps are omitted because those are handling by the
//	**      outermost parser.
//	**
//	**   *  Built-in SQL functions always take precedence over application-defined
//	**      SQL functions.  In other words, it is not possible to override a
//	**      built-in function.
//	*/
func _sqlite3NestedParse(tls *libc.TLS, pParse uintptr, zFormat uintptr, va uintptr) {
	bp := tls.Alloc(80)
	defer tls.Free(80)
	var ap Tva_list
	var db, zSql uintptr
	var savedDbFlags Tu32
	var _ /* saveBuf at bp+0 */ [76]int8
	_, _, _, _ = ap, db, savedDbFlags, zSql
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	savedDbFlags = (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return
	}
	if (*TParse)(unsafe.Pointer(pParse)).FeParseMode != 0 {
		return
	}
	/* Nesting should only be of limited depth */
	ap = va
	zSql = _sqlite3VMPrintf(tls, db, zFormat, ap)
	_ = ap
	if zSql == uintptr(0) {
		/* This can result either from an OOM or because the formatted string
		 ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
		 ** an error */
		if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
			(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_TOOBIG)
		}
		(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
		return
	}
	(*TParse)(unsafe.Pointer(pParse)).Fnested = (*TParse)(unsafe.Pointer(pParse)).Fnested + 1
	libc.Xmemcpy(tls, bp, pParse+uintptr(uint32(libc.UintptrFromInt32(0)+204)), libc.Uint32FromInt64(280)-uint32(libc.UintptrFromInt32(0)+204))
	libc.Xmemset(tls, pParse+uintptr(uint32(libc.UintptrFromInt32(0)+204)), 0, libc.Uint32FromInt64(280)-uint32(libc.UintptrFromInt32(0)+204))
	**(**Tu32)(__ccgo_up(db + 24)) |= uint32(DBFLAG_PreferBuiltin)
	_sqlite3RunParser(tls, pParse, zSql)
	(*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags = savedDbFlags
	_sqlite3DbFree(tls, db, zSql)
	libc.Xmemcpy(tls, pParse+uintptr(uint32(libc.UintptrFromInt32(0)+204)), bp, libc.Uint32FromInt64(280)-uint32(libc.UintptrFromInt32(0)+204))
	(*TParse)(unsafe.Pointer(pParse)).Fnested = (*TParse)(unsafe.Pointer(pParse)).Fnested - 1
}

// C documentation
//
//	/*
//	** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
//	*/
func _sqlite3Prepare16(tls *libc.TLS, db uintptr, zSql uintptr, nBytes int32, prepFlags Tu32, ppStmt uintptr, pzTail uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var chars_parsed, rc, sz, sz1 int32
	var z, z1, zSql8 uintptr
	var _ /* zTail8 at bp+0 */ uintptr
	_, _, _, _, _, _, _ = chars_parsed, rc, sz, sz1, z, z1, zSql8
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	rc = SQLITE_OK
	**(**uintptr)(__ccgo_up(ppStmt)) = uintptr(0)
	if !(_sqlite3SafetyCheckOk(tls, db) != 0) || zSql == uintptr(0) {
		return _sqlite3MisuseError(tls, int32(148771))
	}
	/* Make sure nBytes is non-negative and correct.  It should be the
	 ** number of bytes until the end of the input buffer or until the first
	 ** U+0000 character.  If the input nBytes is odd, convert it into
	 ** an even number.  If the input nBytes is negative, then the input
	 ** must be terminated by at least one U+0000 character */
	if nBytes >= 0 {
		z = zSql
		sz = 0
		for {
			if !(sz < nBytes && (int32(**(**int8)(__ccgo_up(z + uintptr(sz)))) != 0 || int32(**(**int8)(__ccgo_up(z + uintptr(sz+int32(1))))) != 0)) {
				break
			}
			goto _1
		_1:
			;
			sz = sz + int32(2)
		}
		nBytes = sz
	} else {
		z1 = zSql
		sz1 = 0
		for {
			if !(int32(**(**int8)(__ccgo_up(z1 + uintptr(sz1)))) != 0 || int32(**(**int8)(__ccgo_up(z1 + uintptr(sz1+int32(1))))) != 0) {
				break
			}
			goto _2
		_2:
			;
			sz1 = sz1 + int32(2)
		}
		nBytes = sz1
	}
	Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	zSql8 = _sqlite3Utf16to8(tls, db, zSql, nBytes, uint8(SQLITE_UTF16LE))
	if zSql8 != 0 {
		rc = _sqlite3LockAndPrepare(tls, db, zSql8, -int32(1), prepFlags, uintptr(0), ppStmt, bp)
	}
	if **(**uintptr)(__ccgo_up(bp)) != 0 && pzTail != 0 {
		/* If sqlite3_prepare returns a tail pointer, we calculate the
		 ** equivalent pointer into the UTF-16 string by counting the unicode
		 ** characters between zSql8 and zTail8, and then returning a pointer
		 ** the same number of characters into the UTF-16 string.
		 */
		chars_parsed = _sqlite3Utf8CharLen(tls, zSql8, int32(**(**uintptr)(__ccgo_up(bp)))-int32(zSql8))
		**(**uintptr)(__ccgo_up(pzTail)) = zSql + uintptr(_sqlite3Utf16ByteLen(tls, zSql, nBytes, chars_parsed))
	}
	_sqlite3DbFree(tls, db, zSql8)
	rc = _sqlite3ApiExit(tls, db, rc)
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// C documentation
//
//	/*
//	** Return the affinity character for a single column of a table.
//	*/
func _sqlite3TableColumnAffinity(tls *libc.TLS, pTab uintptr, iCol int32) (r int8) {
	if iCol < 0 || iCol >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) {
		return int8(SQLITE_AFF_INTEGER)
	}
	return (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*12))).Faffinity
}

func _unicodeSetCategories(tls *libc.TLS, p uintptr, zCat uintptr) (r int32) {
	var z uintptr
	_ = z
	z = zCat
	for **(**int8)(__ccgo_up(z)) != 0 {
		for int32(**(**int8)(__ccgo_up(z))) == int32(' ') || int32(**(**int8)(__ccgo_up(z))) == int32('\t') {
			z = z + 1
		}
		if **(**int8)(__ccgo_up(z)) != 0 && _sqlite3Fts5UnicodeCatParse(tls, z, p+148) != 0 {
			return int32(SQLITE_ERROR)
		}
		for int32(**(**int8)(__ccgo_up(z))) != int32(' ') && int32(**(**int8)(__ccgo_up(z))) != int32('\t') && int32(**(**int8)(__ccgo_up(z))) != int32('\000') {
			z = z + 1
		}
	}
	_sqlite3Fts5UnicodeAscii(tls, p+148, p)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** It is already known that pMem contains an unterminated string.
//	** Add the zero terminator.
//	**
//	** Three bytes of zero are added.  In this way, there is guaranteed
//	** to be a double-zero byte at an even byte boundary in order to
//	** terminate a UTF16 string, even if the initial size of the buffer
//	** is an odd number of bytes.
//	*/
func _vdbeMemAddTerminator(tls *libc.TLS, pMem uintptr) (r int32) {
	var v1 uintptr
	_ = v1
	if _sqlite3VdbeMemGrow(tls, pMem, (*TMem)(unsafe.Pointer(pMem)).Fn+int32(3), int32(1)) != 0 {
		return int32(SQLITE_NOMEM)
	}
	**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = 0
	**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(1)))) = 0
	**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(2)))) = 0
	v1 = pMem + 16
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** Initialize a WHERE clause scanner object.  Return a pointer to the
//	** first match.  Return NULL if there are no matches.
//	**
//	** The scanner will be searching the WHERE clause pWC.  It will look
//	** for terms of the form "X <op> <expr>" where X is column iColumn of table
//	** iCur.   Or if pIdx!=0 then X is column iColumn of index pIdx.  pIdx
//	** must be one of the indexes of table iCur.
//	**
//	** The <op> must be one of the operators described by opMask.
//	**
//	** If the search is for X and the WHERE clause contains terms of the
//	** form X=Y then this routine might also return terms of the form
//	** "Y <op> <expr>".  The number of levels of transitivity is limited,
//	** but is enough to handle most commonly occurring SQL statements.
//	**
//	** If X is not the INTEGER PRIMARY KEY then X must be compatible with
//	** index pIdx.
//	*/
func _whereScanInit(tls *libc.TLS, pScan uintptr, pWC uintptr, iCur int32, iColumn int32, opMask Tu32, pIdx uintptr) (r uintptr) {
	var j int32
	_ = j
	(*TWhereScan)(unsafe.Pointer(pScan)).FpOrigWC = pWC
	(*TWhereScan)(unsafe.Pointer(pScan)).FpWC = pWC
	(*TWhereScan)(unsafe.Pointer(pScan)).FpIdxExpr = uintptr(0)
	(*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff = 0
	(*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = uintptr(0)
	(*TWhereScan)(unsafe.Pointer(pScan)).FopMask = opMask
	(*TWhereScan)(unsafe.Pointer(pScan)).Fk = 0
	**(**int32)(__ccgo_up(pScan + 28)) = iCur
	(*TWhereScan)(unsafe.Pointer(pScan)).FnEquiv = uint8(1)
	(*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv = uint8(1)
	if pIdx != 0 {
		j = iColumn
		iColumn = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(j)*2)))
		if iColumn == int32((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FiPKey) {
			iColumn = -int32(1)
		} else {
			if iColumn >= 0 {
				(*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff = (**(**TColumn)(__ccgo_up((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(iColumn)*12))).Faffinity
				(*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*4))
			} else {
				if iColumn == -int32(2) {
					(*TWhereScan)(unsafe.Pointer(pScan)).FpIdxExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(j)*20))).FpExpr
					(*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = **(**uintptr)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*4))
					**(**Ti16)(__ccgo_up(pScan + 72)) = int16(-libc.Int32FromInt32(2))
					return _whereScanInitIndexExpr(tls, pScan)
				}
			}
		}
	} else {
		if iColumn == -int32(2) {
			return uintptr(0)
		}
	}
	**(**Ti16)(__ccgo_up(pScan + 72)) = int16(iColumn)
	return _whereScanNext(tls, pScan)
}

const i386 = 1
