// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: vdbeCommit
static int vdbeCommit(sqlite3 *db, Vdbe *p){
  int i;
  int nTrans = 0;  /* Number of databases with an active write-transaction
                   ** that are candidates for a two-phase commit using a
                   ** super-journal */
  int rc = SQLITE_OK;
  int needXcommit = 0;

#ifdef SQLITE_OMIT_VIRTUALTABLE
  /* With this option, sqlite3VtabSync() is defined to be simply
  ** SQLITE_OK so p is not used.
  */
  UNUSED_PARAMETER(p);
#endif

  /* 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(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.
  */
  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
    Btree *pBt = db->aDb[i].pBt;
    if( sqlite3BtreeIsInTrans(pBt) ){
      /* Whether or not a database might need a super-journal depends upon
      ** its journal mode (among other things).  This matrix determines which
      ** journal modes use a super-journal and which do not */
      static const u8 aMJNeeded[] = {
        /* DELETE   */  1,
        /* PERSIST   */ 1,
        /* OFF       */ 0,
        /* TRUNCATE  */ 1,
        /* MEMORY    */ 0,
        /* WAL       */ 0
      };
      Pager *pPager;   /* Pager associated with pBt */
      needXcommit = 1;
      sqlite3BtreeEnter(pBt);
      pPager = sqlite3BtreePager(pBt);
      if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
       && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
       && sqlite3PagerIsMemdb(pPager)==0
      ){
        assert( i!=1 );
        nTrans++;
      }
      rc = sqlite3PagerExclusiveLock(pPager);
      sqlite3BtreeLeave(pBt);
    }
  }
  if( rc!=SQLITE_OK ){
    return rc;
  }

  /* If there are any write-transactions at all, invoke the commit hook */
  if( needXcommit && db->xCommitCallback ){
    rc = db->xCommitCallback(db->pCommitArg);
    if( rc ){
      return SQLITE_CONSTRAINT_COMMITHOOK;
    }
  }

  /* 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(sqlite3BtreeGetFilename(db->aDb[0].pBt))
   || nTrans<=1
  ){
    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
      Btree *pBt = db->aDb[i].pBt;
      if( pBt ){
        rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
      }
    }

    /* 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.
    */
    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
      Btree *pBt = db->aDb[i].pBt;
      if( pBt ){
        rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
      }
    }
    if( rc==SQLITE_OK ){
      sqlite3VtabCommit(db);
    }
  }

  /* The complex case - There is a multi-file write-transaction active.
  ** This requires a super-journal file to ensure the transaction is
  ** committed atomically.
  */
#ifndef SQLITE_OMIT_DISKIO
  else{
    sqlite3_vfs *pVfs = db->pVfs;
    char *zSuper = 0;   /* File-name for the super-journal */
    char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
    sqlite3_file *pSuperJrnl = 0;
    i64 offset = 0;
    int res;
    int retryCount = 0;
    int nMainFile;

    /* Select a super-journal file name */
    nMainFile = sqlite3Strlen30(zMainFile);
    zSuper = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0);
    if( zSuper==0 ) return SQLITE_NOMEM_BKPT;
    zSuper += 4;
    do {
      u32 iRandom;
      if( retryCount ){
        if( retryCount>100 ){
          sqlite3_log(SQLITE_FULL, "MJ delete: %s", zSuper);
          sqlite3OsDelete(pVfs, zSuper, 0);
          break;
        }else if( retryCount==1 ){
          sqlite3_log(SQLITE_FULL, "MJ collide: %s", zSuper);
        }
      }
      retryCount++;
      sqlite3_randomness(sizeof(iRandom), &iRandom);
      sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
                               (iRandom>>8)&0xffffff, iRandom&0xff);
      /* The antipenultimate character of the super-journal name must
      ** be "9" to avoid name collisions when using 8+3 filenames. */
      assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
      sqlite3FileSuffix3(zMainFile, zSuper);
      rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res);
    }while( rc==SQLITE_OK && res );
    if( rc==SQLITE_OK ){
      /* Open the super-journal. */
      rc = sqlite3OsOpenMalloc(pVfs, zSuper, &pSuperJrnl,
          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
          SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_SUPER_JOURNAL, 0
      );
    }
    if( rc!=SQLITE_OK ){
      sqlite3DbFree(db, zSuper-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.
    */
    for(i=0; i<db->nDb; i++){
      Btree *pBt = db->aDb[i].pBt;
      if( sqlite3BtreeIsInTrans(pBt) ){
        char const *zFile = sqlite3BtreeGetJournalname(pBt);
        if( zFile==0 ){
          continue;  /* Ignore TEMP and :memory: databases */
        }
        assert( zFile[0]!=0 );
        rc = sqlite3OsWrite(pSuperJrnl, zFile, sqlite3Strlen30(zFile)+1,offset);
        offset += sqlite3Strlen30(zFile)+1;
        if( rc!=SQLITE_OK ){
          sqlite3OsCloseFree(pSuperJrnl);
          sqlite3OsDelete(pVfs, zSuper, 0);
          sqlite3DbFree(db, zSuper-4);
          return rc;
        }
      }
    }

    /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
    ** flag is set this is not required.
    */
    if( 0==(sqlite3OsDeviceCharacteristics(pSuperJrnl)&SQLITE_IOCAP_SEQUENTIAL)
     && SQLITE_OK!=(rc = sqlite3OsSync(pSuperJrnl, SQLITE_SYNC_NORMAL))
    ){
      sqlite3OsCloseFree(pSuperJrnl);
      sqlite3OsDelete(pVfs, zSuper, 0);
      sqlite3DbFree(db, zSuper-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.
    */
    for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
      Btree *pBt = db->aDb[i].pBt;
      if( pBt ){
        rc = sqlite3BtreeCommitPhaseOne(pBt, zSuper);
      }
    }
    sqlite3OsCloseFree(pSuperJrnl);
    assert( rc!=SQLITE_BUSY );
    if( rc!=SQLITE_OK ){
      sqlite3DbFree(db, zSuper-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(pVfs, zSuper, 1);
    sqlite3DbFree(db, zSuper-4);
    zSuper = 0;
    if( rc ){
      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.
    */
    disable_simulated_io_errors();
    sqlite3BeginBenignMalloc();
    for(i=0; i<db->nDb; i++){
      Btree *pBt = db->aDb[i].pBt;
      if( pBt ){
        sqlite3BtreeCommitPhaseTwo(pBt, 1);
      }
    }
    sqlite3EndBenignMalloc();
    enable_simulated_io_errors();

    sqlite3VtabCommit(db);
  }
#endif

  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x210(%rdi), %r12
movq $0x0, 0x210(%rdi)
xorl %r13d, %r13d
movslq 0x1ec(%rbx), %rax
cmpq %rax, %r13
jge 0x29054d
movq (%r12,%r13,8), %rax
movq 0x10(%rax), %r15
xorl %ebp, %ebp
testq %r15, %r15
je 0x29053a
movq (%r15), %rax
movq 0x78(%rax), %rax
testq %rax, %rax
je 0x29053a
movq %r15, %rdi
callq *%rax
movl %eax, %ebp
movq %r14, %rdi
movq %r15, %rsi
callq 0x2914cc
incq %r13
testl %ebp, %ebp
je 0x290501
movq %r12, 0x210(%rbx)
jmp 0x290a20
movq %r12, 0x210(%rbx)
xorl %r14d, %r14d
movq $-0x1, %r15
movl $0x10, %r12d
movl $0x34, %r13d
xorl %eax, %eax
movslq 0x28(%rbx), %rcx
incq %r15
cmpq %rcx, %r15
jge 0x2905ce
movq 0x20(%rbx), %rcx
movq -0x8(%rcx,%r12), %rdx
xorl %ebp, %ebp
testq %rdx, %rdx
je 0x2905c1
cmpb $0x2, 0x10(%rdx)
jne 0x2905c1
movq (%rdx), %rax
movq 0x8(%rdx), %rdx
movq %rax, 0x8(%rdx)
movq (%rdx), %rdi
cmpb $0x1, (%rcx,%r12)
je 0x2905b5
movzbl 0x9(%rdi), %eax
btq %rax, %r13
jb 0x2905b5
cmpb $0x1, 0x10(%rdi)
adcl $0x0, %r14d
callq 0x288aa2
movl %eax, %ebp
movl $0x1, %eax
addq $0x20, %r12
testl %ebp, %ebp
je 0x29056c
jmp 0x290a20
testl %eax, %eax
je 0x2905f4
movq 0x108(%rbx), %rax
testq %rax, %rax
je 0x2905f4
movq 0x100(%rbx), %rdi
callq *%rax
movl $0x213, %ebp            # imm = 0x213
testl %eax, %eax
jne 0x290a20
movq 0x20(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %r15
movb 0x13(%r15), %bpl
testb %bpl, %bpl
je 0x290615
leaq 0x1414c8(%rip), %rdi    # 0x3d1adb
jmp 0x290621
movq 0xd0(%r15), %rdi
testq %rdi, %rdi
je 0x290653
callq 0x172f0
shlq $0x22, %rax
je 0x290653
cmpl $0x1, %r14d
jle 0x290653
movq (%rbx), %r12
testb %bpl, %bpl
je 0x2906d7
movq $0x0, 0x28(%rsp)
leaq 0x14148d(%rip), %r14    # 0x3d1adb
jmp 0x2906f0
movq $-0x1, %r14
movl $0x8, %r15d
movslq 0x28(%rbx), %rax
incq %r14
cmpq %rax, %r14
jge 0x290693
movq 0x20(%rbx), %rax
movq (%rax,%r15), %rdi
testq %rdi, %rdi
je 0x290684
xorl %esi, %esi
callq 0x2777e5
movl %eax, %ebp
jmp 0x290686
xorl %ebp, %ebp
addq $0x20, %r15
testl %ebp, %ebp
je 0x290660
jmp 0x290a20
movq $-0x1, %r14
movl $0x8, %r15d
movslq 0x28(%rbx), %rax
incq %r14
cmpq %rax, %r14
jge 0x290a11
movq 0x20(%rbx), %rax
movq (%rax,%r15), %rdi
testq %rdi, %rdi
je 0x2906c8
xorl %esi, %esi
callq 0x2776f1
movl %eax, %ebp
jmp 0x2906ca
xorl %ebp, %ebp
addq $0x20, %r15
testl %ebp, %ebp
je 0x2906a0
jmp 0x290a20
movq 0xd0(%r15), %r14
movq $0x0, 0x28(%rsp)
testq %r14, %r14
je 0x29091f
movq %r14, %rdi
callq 0x172f0
movq %rax, %r15
andl $0x3fffffff, %r15d      # imm = 0x3FFFFFFF
leaq 0x14568a(%rip), %rsi    # 0x3d5d93
movq %rbx, %rdi
xorl %edx, %edx
movq %r14, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x27b56a
testq %rax, %rax
je 0x2907d1
movq %r12, 0x8(%rsp)
leaq (%rax,%r15), %r13
addq $0x4, %r13
movq %rax, 0x18(%rsp)
addq $0x4, %rax
movq %rax, 0x10(%rsp)
leaq 0x145676(%rip), %r12    # 0x3d5dbc
leaq 0x34(%rsp), %r14
xorl %r15d, %r15d
testl %r15d, %r15d
movq 0x10(%rsp), %rbp
je 0x29077a
cmpl $0x64, %r15d
ja 0x2907db
cmpl $0x1, %r15d
jne 0x29077a
movl $0xd, %edi
leaq 0x14563d(%rip), %rsi    # 0x3d5dad
movq %rbp, %rdx
xorl %eax, %eax
callq 0x274d08
movl $0x4, %edi
leaq 0x30(%rsp), %rsi
callq 0x274e7e
movl 0x30(%rsp), %ecx
movzbl %cl, %r8d
shrl $0x8, %ecx
movl $0xd, %edi
movq %r13, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x274c41
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %r14, %rcx
callq *0x38(%rdi)
movl %eax, %ebp
testl %eax, %eax
jne 0x2907c6
incl %r15d
cmpl $0x0, 0x34(%rsp)
jne 0x29074e
testl %ebp, %ebp
movq 0x8(%rsp), %r14
jne 0x290822
jmp 0x290802
movl $0x7, %ebp
jmp 0x290a20
leaq 0x1455bd(%rip), %rsi    # 0x3d5d9f
movl $0xd, %edi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x274d08
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq *0x30(%r14)
leaq 0x28(%rsp), %rdx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movl $0x4016, %ecx           # imm = 0x4016
xorl %r8d, %r8d
callq 0x291412
testl %eax, %eax
je 0x290837
movl %eax, %ebp
movq 0x18(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x282904
jmp 0x290a20
movl 0x28(%rbx), %eax
movq 0x28(%rsp), %rcx
movq %rcx, 0x20(%rsp)
testl %eax, %eax
jle 0x2908cc
movl $0x8, %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x20(%rbx), %rcx
movq (%rcx,%r15), %rcx
testq %rcx, %rcx
je 0x2908bd
cmpb $0x2, 0x10(%rcx)
jne 0x2908bd
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
movq 0xd8(%rcx), %r14
testq %r14, %r14
je 0x2908bd
movq %r14, %rdi
callq 0x172f0
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
leal 0x1(%rax), %edx
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %r13, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x290a31
movq %r14, %rdi
callq 0x172f0
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
addq %rax, %r13
incq %r13
movl 0x28(%rbx), %eax
incq %r12
movslq %eax, %rcx
addq $0x20, %r15
cmpq %rcx, %r12
jl 0x290858
movq 0x20(%rsp), %r13
movq (%r13), %rax
movq %r13, %rdi
callq *0x60(%rax)
btl $0xa, %eax
jb 0x29092a
movq (%r13), %rax
movq %r13, %rdi
movl $0x2, %esi
callq *0x28(%rax)
testl %eax, %eax
je 0x29092a
movl %eax, %ebp
movq (%r13), %rax
testq %rax, %rax
movq 0x8(%rsp), %r14
je 0x290912
movq %r13, %rdi
callq *0x8(%rax)
movq $0x0, (%r13)
movq %r13, %rdi
callq 0x272c00
jmp 0x290a7d
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0x290702
movq $-0x1, %r14
movl $0x8, %r12d
movslq 0x28(%rbx), %r15
incq %r14
cmpq %r15, %r14
jge 0x29096a
movq 0x20(%rbx), %rax
movq (%rax,%r12), %rdi
testq %rdi, %rdi
je 0x29095e
movq 0x10(%rsp), %rsi
callq 0x2777e5
movl %eax, %ebp
jmp 0x290960
xorl %ebp, %ebp
addq $0x20, %r12
testl %ebp, %ebp
je 0x290937
jmp 0x29096c
xorl %ebp, %ebp
movq (%r13), %rax
testq %rax, %rax
je 0x290983
movq %r13, %rdi
callq *0x8(%rax)
movq $0x0, (%r13)
movq %r13, %rdi
callq 0x272c00
cmpq %r15, %r14
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r14
jl 0x290827
movq 0x10(%rsp), %rsi
movl $0x1, %edx
callq *0x30(%rdi)
movl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
callq 0x282904
testl %ebp, %ebp
jne 0x290a20
movq 0x22e485(%rip), %rax    # 0x4bee48
testq %rax, %rax
je 0x2909ca
callq *%rax
movl 0x28(%rbx), %eax
testl %eax, %eax
jle 0x290a03
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x20(%rbx), %rcx
movq (%rcx,%r14), %rdi
testq %rdi, %rdi
je 0x2909f4
movl $0x1, %esi
callq 0x2776f1
movl 0x28(%rbx), %eax
incq %r15
movslq %eax, %rcx
addq $0x20, %r14
cmpq %rcx, %r15
jl 0x2909da
movq 0x22e446(%rip), %rax    # 0x4bee50
testq %rax, %rax
je 0x290a11
callq *%rax
movq %rbx, %rdi
movl $0x80, %esi
callq 0x2912d2
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebp
movq 0x20(%rsp), %r15
movq (%r15), %rax
testq %rax, %rax
je 0x290a4d
movq %r15, %rdi
callq *0x8(%rax)
movq $0x0, (%r15)
cmpl $0x0, 0x22ac0c(%rip)    # 0x4bb660
movq 0x8(%rsp), %r14
je 0x290a74
movq %r15, %rdi
callq *0x22ac34(%rip)         # 0x4bb698
cltq
subq %rax, 0x22e08b(%rip)    # 0x4beaf8
decq 0x22e0cc(%rip)          # 0x4beb40
movq %r15, %rdi
callq *0x22ac0b(%rip)         # 0x4bb688
movq %r14, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq *0x30(%r14)
jmp 0x290822