// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3RunVacuum
int sqlite3RunVacuum(
  char **pzErrMsg,        /* Write error message here */
  sqlite3 *db,            /* Database connection */
  int iDb,                /* Which attached DB to vacuum */
  sqlite3_value *pOut     /* Write results here, if not NULL. VACUUM INTO */
){
  int rc = SQLITE_OK;     /* Return code from service routines */
  Btree *pMain;           /* The database being vacuumed */
  Btree *pTemp;           /* The temporary database we vacuum into */
  u32 saved_mDbFlags;     /* Saved value of db->mDbFlags */
  u64 saved_flags;        /* Saved value of db->flags */
  int saved_nChange;      /* Saved value of db->nChange */
  int saved_nTotalChange; /* Saved value of db->nTotalChange */
  u32 saved_openFlags;    /* Saved value of db->openFlags */
  u8 saved_mTrace;        /* Saved trace settings */
  Db *pDb = 0;            /* Database to detach at end of vacuum */
  int isMemDb;            /* True if vacuuming a :memory: database */
  int nRes;               /* Bytes of reserved space at the end of each page */
  int nDb;                /* Number of attached databases */
  const char *zDbMain;    /* Schema name of database to vacuum */
  const char *zOut;       /* Name of output file */

  if( !db->autoCommit ){
    sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
    return SQLITE_ERROR; /* IMP: R-12218-18073 */
  }
  if( db->nVdbeActive>1 ){
    sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
    return SQLITE_ERROR; /* IMP: R-15610-35227 */
  }
  saved_openFlags = db->openFlags;
  if( pOut ){
    if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
      sqlite3SetString(pzErrMsg, db, "non-text filename");
      return SQLITE_ERROR;
    }
    zOut = (const char*)sqlite3_value_text(pOut);
    db->openFlags &= ~SQLITE_OPEN_READONLY;
    db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
  }else{
    zOut = "";
  }

  /* 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 = db->flags;
  saved_mDbFlags = db->mDbFlags;
  saved_nChange = db->nChange;
  saved_nTotalChange = db->nTotalChange;
  saved_mTrace = db->mTrace;
  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
  db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
  db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
                   | SQLITE_Defensive | SQLITE_CountRows);
  db->mTrace = 0;

  zDbMain = db->aDb[iDb].zDbSName;
  pMain = db->aDb[iDb].pBt;
  isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));

  /* Attach the temporary database as 'vacuum_db'. 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 optimisation would be to use a non-journaled pager.
  ** (Later:) I tried setting "PRAGMA vacuum_db.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.
  */
  nDb = db->nDb;
  rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
  db->openFlags = saved_openFlags;
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
  assert( (db->nDb-1)==nDb );
  pDb = &db->aDb[nDb];
  assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
  pTemp = pDb->pBt;
  if( pOut ){
    sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
    i64 sz = 0;
    if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
      rc = SQLITE_ERROR;
      sqlite3SetString(pzErrMsg, db, "output file already exists");
      goto end_of_vacuum;
    }
    db->mDbFlags |= DBFLAG_VacuumInto;
  }
  nRes = sqlite3BtreeGetRequestedReserve(pMain);

  sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
  sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
  sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|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(db, pzErrMsg, "BEGIN");
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
  rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
  if( rc!=SQLITE_OK ) goto end_of_vacuum;

  /* Do not attempt to change the page size for a WAL database */
  if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
                                               ==PAGER_JOURNALMODE_WAL ){
    db->nextPagesize = 0;
  }

  if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
   || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
   || NEVER(db->mallocFailed)
  ){
    rc = SQLITE_NOMEM_BKPT;
    goto end_of_vacuum;
  }

#ifndef SQLITE_OMIT_AUTOVACUUM
  sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
                                           sqlite3BtreeGetAutoVacuum(pMain));
#endif

  /* Query the schema of the main database. Create a mirror schema
  ** in the temporary database.
  */
  db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
  rc = execSqlF(db, pzErrMsg,
      "SELECT sql FROM \"%w\".sqlite_schema"
      " WHERE type='table'AND name<>'sqlite_sequence'"
      " AND coalesce(rootpage,1)>0",
      zDbMain
  );
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
  rc = execSqlF(db, pzErrMsg,
      "SELECT sql FROM \"%w\".sqlite_schema"
      " WHERE type='index'",
      zDbMain
  );
  if( rc!=SQLITE_OK ) goto end_of_vacuum;
  db->init.iDb = 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(db, pzErrMsg,
      "SELECT'INSERT INTO vacuum_db.'||quote(name)"
      "||' SELECT*FROM\"%w\".'||quote(name)"
      "FROM vacuum_db.sqlite_schema "
      "WHERE type='table'AND coalesce(rootpage,1)>0",
      zDbMain
  );
  assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
  db->mDbFlags &= ~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(db, pzErrMsg,
      "INSERT INTO vacuum_db.sqlite_schema"
      " SELECT*FROM \"%w\".sqlite_schema"
      " WHERE type IN('view','trigger')"
      " OR(type='table'AND rootpage=0)",
      zDbMain
  );
  if( rc ) goto end_of_vacuum;

  /* 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().
  */
  {
    u32 meta;
    int i;

    /* This array determines which meta meta values are preserved in the
    ** vacuum.  Even entries are the meta value number and odd entries
    ** are an increment to apply to the meta value after the vacuum.
    ** The increment is used to increase the schema cookie so that other
    ** connections to the same database will know to reread the schema.
    */
    static const unsigned char aCopy[] = {
       BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
       BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
       BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
       BTREE_USER_VERSION,       0,  /* Preserve the user version */
       BTREE_APPLICATION_ID,     0,  /* Preserve the application id */
    };

    assert( 1==sqlite3BtreeIsInTrans(pTemp) );
    assert( pOut!=0 || 1==sqlite3BtreeIsInTrans(pMain) );

    /* Copy Btree meta values */
    for(i=0; i<ArraySize(aCopy); i+=2){
      /* GetMeta() and UpdateMeta() cannot fail in this context because
      ** we already have page 1 loaded into cache and marked dirty. */
      sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
      rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
      if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
    }

    if( pOut==0 ){
      rc = sqlite3BtreeCopyFile(pMain, pTemp);
    }
    if( rc!=SQLITE_OK ) goto end_of_vacuum;
    rc = sqlite3BtreeCommit(pTemp);
    if( rc!=SQLITE_OK ) goto end_of_vacuum;
#ifndef SQLITE_OMIT_AUTOVACUUM
    if( pOut==0 ){
      sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
    }
#endif
  }

  assert( rc==SQLITE_OK );
  if( pOut==0 ){
    rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
  }

end_of_vacuum:
  /* Restore the original value of db->flags */
  db->init.iDb = 0;
  db->mDbFlags = saved_mDbFlags;
  db->flags = saved_flags;
  db->nChange = saved_nChange;
  db->nTotalChange = saved_nTotalChange;
  db->mTrace = saved_mTrace;
  sqlite3BtreeSetPageSize(pMain, -1, 0, 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.
  */
  db->autoCommit = 1;

  if( pDb ){
    sqlite3BtreeClose(pDb->pBt);
    pDb->pBt = 0;
    pDb->pSchema = 0;
  }

  /* This both clears the schemas and reduces the size of the db->aDb[]
  ** array. */
  sqlite3ResetAllSchemasOfConnection(db);

  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %rbx
movq %rdi, %r15
cmpb $0x0, 0x5f(%rsi)
je 0x24497d
cmpl $0x2, 0xc0(%rbx)
jl 0x244986
leaq 0x13258b(%rip), %rdx    # 0x376f06
jmp 0x2449aa
leaq 0x13255a(%rip), %rdx    # 0x376ede
jmp 0x2449aa
movl %edx, %ebp
movl 0x4c(%rbx), %esi
testq %rcx, %rcx
je 0x2449bd
movl 0x8(%rcx), %eax
movabsq $0x4000400040004, %rdx  # imm = 0x4000400040004
btq %rax, %rdx
jb 0x2449cf
leaq 0x132587(%rip), %rdx    # 0x376f31
movq %r15, %rdi
movq %rbx, %rsi
callq 0x24a76d
pushq $0x1
popq %rbp
jmp 0x244d7b
movl %esi, 0x28(%rsp)
movq %rcx, 0x40(%rsp)
leaq 0x125ba3(%rip), %rcx    # 0x36a570
jmp 0x2449ef
movl %esi, 0x28(%rsp)
movq %rcx, 0x40(%rsp)
movq %rcx, %rdi
callq 0x2243e3
movq %rax, %rcx
movl 0x4c(%rbx), %eax
andl $-0x8, %eax
orl $0x6, %eax
movl %eax, 0x4c(%rbx)
movq 0x30(%rbx), %rdx
movl 0x2c(%rbx), %esi
movsd 0x74(%rbx), %xmm0
movaps %xmm0, 0x60(%rsp)
movb 0x68(%rbx), %al
movb %al, 0xf(%rsp)
movl %esi, 0x38(%rsp)
movl %esi, %eax
orl $0x6, %eax
movl %eax, 0x2c(%rbx)
movabsq $-0x110005202, %rax     # imm = 0xFFFFFFFEEFFFADFE
movq %rdx, 0x58(%rsp)
andq %rdx, %rax
orq $0x201, %rax            # imm = 0x201
movq %rax, 0x30(%rbx)
movb $0x0, 0x68(%rbx)
movq 0x20(%rbx), %rax
movslq %ebp, %r14
shlq $0x5, %r14
movq (%rax,%r14), %rdx
movq %rdx, 0x50(%rsp)
movq 0x8(%rax,%r14), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rax), %rax
movq (%rax), %rax
movb 0x10(%rax), %r12b
movslq 0x28(%rbx), %r13
leaq 0x1324dc(%rip), %rdx    # 0x376f43
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x24c795
movl %eax, %ebp
movl 0x28(%rsp), %eax
movl %eax, 0x4c(%rbx)
testl %ebp, %ebp
je 0x244a8e
xorl %r15d, %r15d
movq 0x18(%rsp), %r14
jmp 0x244d20
movq %r15, 0x28(%rsp)
movb %r12b, 0xe(%rsp)
movq 0x20(%rbx), %rax
movq %r13, %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdx
movq %rdx, 0x10(%rsp)
movq 0x8(%rax,%rcx), %rbp
movq 0x40(%rsp), %r15
testq %r15, %r15
je 0x244afd
movq 0x8(%rbp), %rax
movq (%rax), %rax
movq 0x48(%rax), %rdi
andq $0x0, 0x80(%rsp)
movq (%rdi), %rax
testq %rax, %rax
je 0x244af9
leaq 0x80(%rsp), %rsi
callq *0x30(%rax)
testl %eax, %eax
jne 0x244b8d
cmpq $0x0, 0x80(%rsp)
jg 0x244b8d
orb $0x8, 0x2c(%rbx)
movq %r13, 0x48(%rsp)
movq %rbp, %r13
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
callq 0x22c260
movl %eax, %r12d
movq 0x20(%rbx), %rax
movq 0x18(%rax,%r14), %rax
movq %rbp, %r14
movl 0x74(%rax), %esi
movq %r13, %rdi
callq 0x24a7b6
movq %rbp, %rdi
xorl %esi, %esi
callq 0x24c857
movq %r13, %rdi
movl %eax, %esi
callq 0x24c857
pushq $0x21
popq %rsi
movq %r13, %rdi
callq 0x24c8a6
leaq 0x132423(%rip), %rdx    # 0x376f75
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x24c946
testl %eax, %eax
jne 0x244b86
movl %r12d, 0x34(%rsp)
movq %r13, 0x20(%rsp)
xorl %esi, %esi
testq %r15, %r15
sete %sil
addl %esi, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x222db8
testl %eax, %eax
je 0x244bae
movl %eax, %ebp
jmp 0x244d1b
pushq $0x1
popq %rbp
leaq 0x1323c3(%rip), %rdx    # 0x376f5a
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x24a76d
movq 0x18(%rsp), %r14
jmp 0x244d1b
movq 0x8(%r14), %rax
movq (%rax), %rcx
cmpb $0x5, 0x9(%rcx)
jne 0x244bbf
andl $0x0, 0x6c(%rbx)
movl 0x34(%rax), %esi
movq 0x20(%rsp), %rdi
movl 0x34(%rsp), %r13d
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x22c27b
pushq $0x7
popq %rbp
testl %eax, %eax
jne 0x244d1b
cmpb $0x0, 0xe(%rsp)
je 0x244d01
cmpb $0x0, 0x61(%rbx)
jne 0x244d1b
movsbl 0x64(%rbx), %esi
testl %esi, %esi
jns 0x244c08
movq %r14, %rdi
callq 0x24ca33
movl %eax, %esi
movq 0x20(%rsp), %rdi
callq 0x24ca00
movq 0x48(%rsp), %rax
movb %al, 0xb4(%rbx)
leaq 0x132357(%rip), %rdx    # 0x376f7b
movq %rbx, %rdi
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq 0x50(%rsp), %r13
movq %r13, %rcx
xorl %eax, %eax
callq 0x24c795
testl %eax, %eax
jne 0x244b86
leaq 0x13239a(%rip), %rdx    # 0x376fe7
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rcx
xorl %eax, %eax
callq 0x24c795
testl %eax, %eax
jne 0x244b86
movb $0x0, 0xb4(%rbx)
leaq 0x1323aa(%rip), %rdx    # 0x37701d
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rcx
xorl %eax, %eax
callq 0x24c795
andb $-0x5, 0x2c(%rbx)
testl %eax, %eax
jne 0x244b86
leaq 0x13241e(%rip), %rdx    # 0x3770b4
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rcx
xorl %eax, %eax
callq 0x24c795
testl %eax, %eax
jne 0x244cf5
xorl %r14d, %r14d
leaq 0x12d660(%rip), %r12    # 0x372314
leaq 0x3c(%rsp), %r13
cmpq $0x9, %r14
ja 0x244d8f
movzbl (%r14,%r12), %ebp
movq 0x18(%rsp), %rdi
movl %ebp, %esi
movq %r13, %rdx
callq 0x242986
movzbl 0x1(%r14,%r12), %edx
addl 0x3c(%rsp), %edx
movq 0x20(%rsp), %rdi
movl %ebp, %esi
callq 0x2235ae
addq $0x2, %r14
testl %eax, %eax
je 0x244cb9
movl %eax, %ebp
movq 0x10(%rsp), %r15
jmp 0x244a84
movl 0x6c(%rbx), %esi
movq 0x20(%rsp), %rdi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x22c27b
testl %eax, %eax
je 0x244bec
movq 0x10(%rsp), %r15
movb $0x0, 0xb4(%rbx)
movl 0x38(%rsp), %eax
movl %eax, 0x2c(%rbx)
movq 0x58(%rsp), %rax
movq %rax, 0x30(%rbx)
movaps 0x60(%rsp), %xmm0
movlps %xmm0, 0x74(%rbx)
movb 0xf(%rsp), %al
movb %al, 0x68(%rbx)
pushq $-0x1
popq %rsi
pushq $0x1
popq %rcx
movq %r14, %rdi
xorl %edx, %edx
callq 0x22c27b
movb $0x1, 0x5f(%rbx)
testq %r15, %r15
je 0x244d73
movq 0x8(%r15), %rdi
callq 0x239820
andq $0x0, 0x8(%r15)
andq $0x0, 0x18(%r15)
movq %rbx, %rdi
callq 0x2235f9
movl %ebp, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r15, %r15
je 0x244dfc
movq 0x20(%rsp), %rdi
callq 0x24a7c9
movl %eax, %ebp
testl %eax, %eax
setne %al
testq %r15, %r15
jne 0x244dc6
testl %ebp, %ebp
jne 0x244dc6
movq 0x20(%rsp), %rdi
callq 0x24ca33
movq 0x18(%rsp), %rdi
movl %eax, %esi
callq 0x24ca00
xorl %eax, %eax
testq %r15, %r15
jne 0x244cf7
testb %al, %al
jne 0x244cf7
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x34(%rax), %esi
pushq $0x1
popq %rcx
movq 0x18(%rsp), %r14
movq %r14, %rdi
movl 0x34(%rsp), %edx
callq 0x22c27b
jmp 0x244b86
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
movq %rax, 0x8(%rdx)
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rax, 0x8(%rcx)
movq (%rdx), %rax
movq 0x48(%rax), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x244e53
movslq 0x34(%rcx), %rdx
movl 0x40(%rcx), %ecx
imulq %rdx, %rcx
leaq 0x78(%rsp), %rdx
movq %rcx, (%rdx)
pushq $0xb
popq %rsi
callq *0x50(%rax)
cmpl $0xc, %eax
je 0x244e53
movl %eax, %ebp
testl %eax, %eax
jne 0x244cf7
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %r13
movaps %xmm0, 0x30(%r13)
movaps %xmm0, 0x20(%r13)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, (%r13)
andq $0x0, 0x40(%r13)
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq %rax, 0x20(%r13)
movq %rcx, 0x28(%r13)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%r13)
movl $0x1, 0x18(%r13)
movq %r13, %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
callq 0x2228f9
movq %r13, %rdi
callq 0x223ce3
testl %eax, %eax
je 0x244ed6
movl %eax, %ebp
movq 0x88(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
cmpb $0x0, 0x10(%rdi)
jne 0x244cf7
callq 0x234787
jmp 0x244cf7
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
andw $-0x3, 0x28(%rax)
jmp 0x244d94