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

//go:build (darwin && amd64) || (darwin && arm64) || (freebsd && amd64) || (freebsd && arm64)

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

type Tvm_offset_t = uint64

type Tvm_size_t = uint64

const __INTMAX_FMTd__ = "ld"

const __INTMAX_FMTi__ = "li"

const __UINTMAX_FMTX__ = "lX"

const __UINTMAX_FMTo__ = "lo"

const __UINTMAX_FMTu__ = "lu"

const __UINTMAX_FMTx__ = "lx"

// 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 + 18
			*(*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 libc.Int32FromUint16((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) & ^libc.Int32FromInt32(WO_EQUIV) == 0 {
				goto _2
			}
			if libc.Int32FromUint16((*TWhereTerm)(unsafe.Pointer(pTerm)).FwtFlags)&int32(TERM_VNULL) != 0 {
				goto _2
			}
			if libc.Int32FromUint8((*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 + 18
			*(*Tu16)(unsafe.Pointer(v3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v3))) | libc.Int32FromInt32(TERM_OK))
			goto _2
		_2:
			;
			i = i + 1
			pTerm += 56
		}
		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)*32))).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 libc.Int32FromUint8((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*32))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 {
				break
			}
			/* First case - a direct column references without a COLLATE operator */
			if libc.Int32FromUint8((*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 = libc.Int32FromUint8((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE); v7 {
				v3 = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft
				pE2 = v3
			}
			if v7 && libc.Int32FromUint8((*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)*16)
				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(libc.Int32FromUint16((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_SORTBYGROUP) != 0)
			nOrderBy = n
			if libc.Int32FromUint16((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_DISTINCTBY) != 0 && !(int32(*(*uint32)(unsafe.Pointer(pSrc + 24 + 4))&0x8000>>15) != 0) {
				eDistinct = int32(2) + bSortByGroup
			} else {
				if libc.Int32FromUint16((*TWhereInfo)(unsafe.Pointer(pWInfo)).FwctrlFlags)&int32(WHERE_GROUPBY) != 0 {
					eDistinct = int32(1) - bSortByGroup
				} else {
					if libc.Int32FromUint16((*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(uint64(96)+(libc.Uint64FromInt64(12)+libc.Uint64FromInt64(8))*libc.Uint64FromInt32(nTerm)+uint64(8)*libc.Uint64FromInt32(nOrderBy)+(uint64(libc.UintptrFromInt32(0)+32)+libc.Uint64FromInt32(nTerm)*uint64(8))))
	if pIdxInfo == uintptr(0) {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1672, 0)
		return uintptr(0)
	}
	pHidden = pIdxInfo + 1*96
	pIdxCons = pHidden + 32 + uintptr(nTerm)*8
	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 = libc.Uint64FromInt64(libc.Int64FromUint64((*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 < libc.Int32FromUint16((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol)) {
				break
			}
			iCol = int32(**(**Ti16)(__ccgo_up((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2)))
			if iCol >= libc.Int32FromUint64(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(8))-libc.Int32FromInt32(1) {
				iCol = libc.Int32FromUint64(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(8)) - libc.Int32FromInt32(1)
			}
			**(**Tsqlite3_uint64)(__ccgo_up(pIdxInfo + 88)) |= 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 libc.Int32FromUint16((*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 + 32))).FleftColumn
			(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).FiTermOffset = i
			op = libc.Uint16FromInt32(libc.Int32FromUint16((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator) & int32(WO_ALL))
			if libc.Int32FromUint16(op) == int32(WO_IN) {
				if libc.Int32FromUint16((*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 + 20)) |= v12
				}
				op = uint16(WO_EQ)
			}
			if libc.Int32FromUint16(op) == int32(WO_AUX) {
				(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).Fop = (*TWhereTerm)(unsafe.Pointer(pTerm)).FeMatchOp
			} else {
				if libc.Int32FromUint16(op)&(libc.Int32FromInt32(WO_ISNULL)|libc.Int32FromInt32(WO_IS)) != 0 {
					if libc.Int32FromUint16(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 libc.Int32FromUint16(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 = libc.Uint16FromInt32(int32(mNoOmit) | libc.Int32FromInt32(1)<<j)
						}
						if libc.Int32FromUint16(op) == libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_LT)-libc.Int32FromInt32(TK_EQ)) {
							(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).Fop = libc.Uint8FromInt32(libc.Int32FromInt32(WO_EQ) << (libc.Int32FromInt32(TK_LE) - libc.Int32FromInt32(TK_EQ)))
						}
						if libc.Int32FromUint16(op) == libc.Int32FromInt32(WO_EQ)<<(libc.Int32FromInt32(TK_GT)-libc.Int32FromInt32(TK_EQ)) {
							(**(**Tsqlite3_index_constraint)(__ccgo_up(pIdxCons + uintptr(j)*12))).Fop = libc.Uint8FromInt32(libc.Int32FromInt32(WO_EQ) << (libc.Int32FromInt32(TK_GE) - libc.Int32FromInt32(TK_EQ)))
						}
					}
				}
			}
			j = j + 1
			goto _11
		_11:
			;
			i = i + 1
			pTerm += 56
		}
		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)*32))).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 = libc.Uint8FromInt32(libc.Int32FromUint8((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*32))).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
//
//	/*
//	** 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, libc.Uint64FromInt32(nByte+int32(8)))
	if p != 0 {
		**(**Tsqlite3_int64)(__ccgo_up(p)) = int64(nByte)
		p += 8
	} else {
		Xsqlite3_log(tls, int32(SQLITE_NOMEM), __ccgo_ts+1598, 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, libc.Uint64FromInt32(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+1636, libc.VaList(bp+8, _sqlite3MemSize(tls, pPrior), nByte))
	}
	return p
}

// 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 libc.Int32FromUint8((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 && libc.Int32FromUint8((*Tsqlite3)(unsafe.Pointer(db)).FbBenignMalloc) == 0 {
		(*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed = uint8(1)
		if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec > 0 {
			libc.AtomicStoreNInt32(db+432, 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+1672, 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
//
//	/*
//	** 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 + 1702
	}
	*(*uintptr)(unsafe.Pointer(pMem)) = v1
	(*TMem)(unsafe.Pointer(pMem)).Fz = pPtr
	(*TMem)(unsafe.Pointer(pMem)).Fflags = libc.Uint16FromInt32(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
//
//	/*
//	** 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 + 1702
	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*8)))
		nPass = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + 1*8)))
	}
	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 !(libc.Int32FromUint8(v2) != 0x00) {
				break
			}
			for !(libc.Int32FromUint8(_sqlite3CtypeMap[c])&libc.Int32FromInt32(0x08) != 0) {
				if libc.Int32FromUint8(**(**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 libc.Int32FromUint8(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 !(libc.Int32FromUint8(_sqlite3CtypeMap[d])&libc.Int32FromInt32(0x08) != 0) {
				goto unhex_null
			}
			v1 = p
			p = p + 1
			**(**Tu8)(__ccgo_up(v1)) = libc.Uint8FromInt32(libc.Int32FromUint8(_sqlite3HexToInt(tls, libc.Int32FromUint8(c)))<<int32(4) | libc.Int32FromUint8(_sqlite3HexToInt(tls, libc.Int32FromUint8(d))))
		}
	}
	goto unhex_done
unhex_done:
	;
	Xsqlite3_result_blob(tls, pCtx, pBlob, int32(int64(p)-int64(pBlob)), __ccgo_fp(Xsqlite3_free))
	return
	goto unhex_null
unhex_null:
	;
	Xsqlite3_free(tls, pBlob)
	return
}

// C documentation
//
//	/*
//	** Attempt to set the size of the memory mapping maintained by file
//	** descriptor pFd to nNew bytes. Any existing mapping is discarded.
//	**
//	** If successful, this function sets the following variables:
//	**
//	**       unixFile.pMapRegion
//	**       unixFile.mmapSize
//	**       unixFile.mmapSizeActual
//	**
//	** If unsuccessful, an error message is logged via sqlite3_log() and
//	** the three variables above are zeroed. In this case SQLite should
//	** continue accessing the database using the xRead() and xWrite()
//	** methods.
//	*/
func _unixRemapfile(tls *libc.TLS, pFd uintptr, nNew Ti64) {
	var flags, h, szSyspage int32
	var nOrig, nReuse Ti64
	var pNew, pOrig, pReq, zErr uintptr
	var v1 Tsqlite3_int64
	_, _, _, _, _, _, _, _, _, _ = flags, h, nOrig, nReuse, pNew, pOrig, pReq, szSyspage, zErr, v1
	zErr = __ccgo_ts + 3698
	h = (*TunixFile)(unsafe.Pointer(pFd)).Fh                  /* File descriptor open on db file */
	pOrig = (*TunixFile)(unsafe.Pointer(pFd)).FpMapRegion     /* Pointer to current file mapping */
	nOrig = (*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeActual /* Size of pOrig region in bytes */
	pNew = uintptr(0)                                         /* Location of new mapping */
	flags = int32(PROT_READ)                                  /* Flags to pass to mmap() */
	if pOrig != 0 {
		szSyspage = (*(*func(*libc.TLS) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(25)].FpCurrent})))(tls)
		nReuse = (*TunixFile)(unsafe.Pointer(pFd)).FmmapSize & int64(^(szSyspage - libc.Int32FromInt32(1)))
		pReq = pOrig + uintptr(nReuse)
		/* Unmap any pages of the existing mapping that cannot be reused. */
		if nReuse != nOrig {
			(*(*func(*libc.TLS, uintptr, Tsize_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(23)].FpCurrent})))(tls, pReq, libc.Uint64FromInt64(nOrig-nReuse))
		}
		pNew = (*(*func(*libc.TLS, uintptr, Tsize_t, int32, int32, int32, Toff_t) uintptr)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(22)].FpCurrent})))(tls, pReq, libc.Uint64FromInt64(nNew-nReuse), flags, int32(MAP_SHARED), h, nReuse)
		if pNew != uintptr(-libc.Int32FromInt32(1)) {
			if pNew != pReq {
				(*(*func(*libc.TLS, uintptr, Tsize_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(23)].FpCurrent})))(tls, pNew, libc.Uint64FromInt64(nNew-nReuse))
				pNew = uintptr(0)
			} else {
				pNew = pOrig
			}
		}
		/* The attempt to extend the existing mapping failed. Free it. */
		if pNew == uintptr(-libc.Int32FromInt32(1)) || pNew == uintptr(0) {
			(*(*func(*libc.TLS, uintptr, Tsize_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(23)].FpCurrent})))(tls, pOrig, libc.Uint64FromInt64(nReuse))
		}
	}
	/* If pNew is still NULL, try to create an entirely new mapping. */
	if pNew == uintptr(0) {
		pNew = (*(*func(*libc.TLS, uintptr, Tsize_t, int32, int32, int32, Toff_t) uintptr)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(22)].FpCurrent})))(tls, uintptr(0), libc.Uint64FromInt64(nNew), flags, int32(MAP_SHARED), h, 0)
	}
	if pNew == uintptr(-libc.Int32FromInt32(1)) {
		pNew = uintptr(0)
		nNew = 0
		_unixLogErrorAtLine(tls, SQLITE_OK, zErr, (*TunixFile)(unsafe.Pointer(pFd)).FzPath, int32(45847))
		/* If the mmap() above failed, assume that all subsequent mmap() calls
		 ** will probably fail too. Fall back to using xRead/xWrite exclusively
		 ** in this case.  */
		(*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeMax = 0
	}
	(*TunixFile)(unsafe.Pointer(pFd)).FpMapRegion = pNew
	v1 = nNew
	(*TunixFile)(unsafe.Pointer(pFd)).FmmapSizeActual = v1
	(*TunixFile)(unsafe.Pointer(pFd)).FmmapSize = v1
}

// 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 + 60
	*(*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 libc.Int32FromUint8((*(*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 libc.Int32FromUint8((*(*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+1702, -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)
}
