// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, %rbx
movq 0x210(%rdi), %r12
movq $0x0, 0x210(%rdi)
cmpl $0x0, 0x1ec(%rdi)
jle 0x29a2e0
movq %rsi, %r14
xorl %r13d, %r13d
movq (%r12,%r13,8), %rax
movq 0x10(%rax), %r15
movl $0x0, %ebp
testq %r15, %r15
je 0x29a2cb
movq (%r15), %rax
movq 0x78(%rax), %rax
testq %rax, %rax
je 0x29a2cb
movq %r15, %rdi
callq *%rax
movl %eax, %ebp
movq %r14, %rdi
movq %r15, %rsi
callq 0x29b1a8
testl %ebp, %ebp
jne 0x29a2e2
incq %r13
movslq 0x1ec(%rbx), %rax
cmpq %rax, %r13
jl 0x29a29b
jmp 0x29a2e2
xorl %ebp, %ebp
movq %r12, 0x210(%rbx)
testl %ebp, %ebp
jne 0x29a834
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 0x29a36b
movq 0x20(%rbx), %rcx
movq -0x8(%rcx,%r12), %rdx
xorl %ebp, %ebp
testq %rdx, %rdx
je 0x29a35e
cmpb $0x2, 0x10(%rdx)
jne 0x29a35e
movq (%rdx), %rax
movq 0x8(%rdx), %rdx
movq %rax, 0x8(%rdx)
movq (%rdx), %rdi
cmpb $0x1, (%rcx,%r12)
je 0x29a352
movzbl 0x9(%rdi), %eax
btq %rax, %r13
jb 0x29a352
cmpb $0x1, 0x10(%rdi)
adcl $0x0, %r14d
callq 0x2928c6
movl %eax, %ebp
movl $0x1, %eax
addq $0x20, %r12
testl %ebp, %ebp
je 0x29a309
jmp 0x29a834
testl %eax, %eax
je 0x29a391
movq 0x108(%rbx), %rax
testq %rax, %rax
je 0x29a391
movq 0x100(%rbx), %rdi
callq *%rax
movl $0x213, %ebp            # imm = 0x213
testl %eax, %eax
jne 0x29a834
movq 0x20(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %r15
movb 0x13(%r15), %bpl
testb %bpl, %bpl
je 0x29a3b2
leaq 0x140eab(%rip), %rdi    # 0x3db25b
jmp 0x29a3b9
movq 0xd0(%r15), %rdi
testq %rdi, %rdi
je 0x29a3cc
callq 0x172f0
shlq $0x22, %rax
sete %al
jmp 0x29a3ce
movb $0x1, %al
testb %al, %al
jne 0x29a3ed
cmpl $0x1, %r14d
jle 0x29a3ed
testb %bpl, %bpl
je 0x29a47b
leaq 0x140e73(%rip), %r15    # 0x3db25b
jmp 0x29a482
cmpl $0x0, 0x28(%rbx)
jle 0x29a42d
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
movq (%rax,%r14), %rdi
movl $0x0, %ebp
testq %rdi, %rdi
je 0x29a417
xorl %esi, %esi
callq 0x28118d
movl %eax, %ebp
testl %ebp, %ebp
jne 0x29a42f
incq %r15
movslq 0x28(%rbx), %rax
addq $0x20, %r14
cmpq %rax, %r15
jl 0x29a3fc
jmp 0x29a42f
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x29a834
movq $-0x1, %r14
movl $0x8, %r15d
movslq 0x28(%rbx), %rax
incq %r14
cmpq %rax, %r14
jge 0x29a825
movq 0x20(%rbx), %rax
movq (%rax,%r15), %rdi
testq %rdi, %rdi
je 0x29a46c
xorl %esi, %esi
callq 0x2810a8
movl %eax, %ebp
jmp 0x29a46e
xorl %ebp, %ebp
addq $0x20, %r15
testl %ebp, %ebp
je 0x29a444
jmp 0x29a834
movq 0xd0(%r15), %r15
movq (%rbx), %r14
movq $0x0, 0x28(%rsp)
testq %r15, %r15
je 0x29a4a7
movq %r15, %rdi
callq 0x172f0
movq %rax, %r12
andl $0x3fffffff, %r12d      # imm = 0x3FFFFFFF
jmp 0x29a4aa
xorl %r12d, %r12d
leaq 0x145036(%rip), %rsi    # 0x3df4e7
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x284f95
testq %rax, %rax
je 0x29a5d1
leaq (%rax,%r12), %r15
addq $0x4, %r15
movq %rax, 0x10(%rsp)
addq $0x4, %rax
movq %rax, 0x8(%rsp)
xorl %r12d, %r12d
testl %r12d, %r12d
je 0x29a53c
cmpl $0x65, %r12d
jl 0x29a51e
xorl %r13d, %r13d
movl $0xd, %edi
leaq 0x144ff4(%rip), %rsi    # 0x3df4f3
movq 0x8(%rsp), %rbp
movq %rbp, %rdx
xorl %eax, %eax
callq 0x27e637
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq *0x30(%r14)
xorl %ebp, %ebp
jmp 0x29a587
cmpl $0x1, %r12d
jne 0x29a53c
movl $0xd, %edi
leaq 0x144fd1(%rip), %rsi    # 0x3df501
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x27e637
incl %r12d
movl $0x4, %edi
leaq 0x38(%rsp), %rsi
callq 0x27e7ad
movl 0x38(%rsp), %ecx
movzbl %cl, %r8d
shrl $0x8, %ecx
movl $0xd, %edi
movq %r15, %rsi
leaq 0x144fa8(%rip), %rdx    # 0x3df510
xorl %eax, %eax
callq 0x27e570
movq %r14, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
leaq 0x3c(%rsp), %rcx
callq *0x38(%r14)
movl %eax, %ebp
movb $0x1, %r13b
testb %r13b, %r13b
je 0x29a59b
testl %ebp, %ebp
jne 0x29a59b
cmpl $0x0, 0x3c(%rsp)
jne 0x29a4e5
testl %ebp, %ebp
jne 0x29a5bb
leaq 0x28(%rsp), %rdx
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movl $0x4016, %ecx           # imm = 0x4016
xorl %r8d, %r8d
callq 0x29b113
movl %eax, %ebp
testl %ebp, %ebp
je 0x29a5db
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x28c3e7
jmp 0x29a834
movl $0x7, %ebp
jmp 0x29a834
cmpl $0x0, 0x28(%rbx)
jle 0x29a715
xorl %r13d, %r13d
movl $0x8, %r12d
movq 0x28(%rsp), %r15
xorl %ecx, %ecx
movq $0x0, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %r15, 0x40(%rsp)
movq 0x20(%rbx), %rax
movq (%rax,%r12), %rax
testq %rax, %rax
je 0x29a6ce
cmpb $0x2, 0x10(%rax)
jne 0x29a6ce
movq %r15, %r14
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0xd8(%rax), %r15
testq %r15, %r15
je 0x29a6d4
movq %r15, %rdi
callq 0x172f0
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
leal 0x1(%rax), %edx
movq %r14, %rdi
movq (%r14), %rax
movq %r15, %rsi
movq 0x18(%rsp), %r14
movq %r14, %rcx
callq *0x18(%rax)
movl %eax, %ebp
movq %r15, %rdi
callq 0x172f0
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
addq %rax, %r14
incq %r14
movq %r14, 0x18(%rsp)
testl %ebp, %ebp
je 0x29a6e2
movq 0x40(%rsp), %r15
movq (%r15), %rax
testq %rax, %rax
je 0x29a699
movq %r15, %rdi
callq *0x8(%rax)
movq $0x0, (%r15)
movq %r15, %rdi
callq 0x27c490
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq *0x30(%r14)
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x28c3e7
movl $0x1, %eax
movl %ebp, %ecx
movl %ebp, 0x34(%rsp)
jmp 0x29a6f2
movl %ecx, %ebp
xorl %eax, %eax
jmp 0x29a6f2
movl $0xf, %eax
movl 0x34(%rsp), %ebp
movq %r14, %r15
jmp 0x29a6ed
xorl %ebp, %ebp
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x40(%rsp), %r15
movq 0x20(%rsp), %r14
cmpl $0xf, %eax
je 0x29a6ff
testl %eax, %eax
jne 0x29a834
incq %r13
movslq 0x28(%rbx), %rax
addq $0x20, %r12
cmpq %rax, %r13
jl 0x29a608
jmp 0x29a717
xorl %ecx, %ecx
movq 0x28(%rsp), %r15
movq (%r15), %rax
movq %r15, %rdi
movl %ecx, %ebp
callq *0x60(%rax)
btl $0xa, %eax
jb 0x29a773
movq (%r15), %rax
movq %r15, %rdi
movl $0x2, %esi
callq *0x28(%rax)
testl %eax, %eax
je 0x29a771
movl %eax, %ebp
movq (%r15), %rax
testq %rax, %rax
je 0x29a756
movq %r15, %rdi
callq *0x8(%rax)
movq $0x0, (%r15)
movq %r15, %rdi
callq 0x27c490
movq %r14, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq *0x30(%r14)
jmp 0x29a5bf
xorl %ebp, %ebp
movq %r15, 0x18(%rsp)
testl %ebp, %ebp
sete %r12b
je 0x29a845
movq 0x18(%rsp), %r15
movq (%r15), %rax
testq %rax, %rax
je 0x29a79e
movq %r15, %rdi
callq *0x8(%rax)
movq $0x0, (%r15)
movq %r15, %rdi
callq 0x27c490
testb %r12b, %r12b
je 0x29a5bf
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movl $0x1, %edx
callq *0x30(%r14)
movl %eax, %ebp
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x28c3e7
testl %ebp, %ebp
jne 0x29a834
movq 0x22f66e(%rip), %rax    # 0x4c9e48
testq %rax, %rax
je 0x29a7e1
callq *%rax
cmpl $0x0, 0x28(%rbx)
jle 0x29a817
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
movq (%rax,%r14), %rdi
testq %rdi, %rdi
je 0x29a807
movl $0x1, %esi
callq 0x2810a8
incq %r15
movslq 0x28(%rbx), %rax
addq $0x20, %r14
cmpq %rax, %r15
jl 0x29a7f0
movq 0x22f632(%rip), %rax    # 0x4c9e50
testq %rax, %rax
je 0x29a825
callq *%rax
movq %rbx, %rdi
movl $0x80, %esi
callq 0x29afd3
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x28(%rbx)
jle 0x29a784
movl $0x8, %r13d
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
movq (%rax,%r13), %rdi
movl $0x0, %ebp
testq %rdi, %rdi
je 0x29a876
movq 0x8(%rsp), %rsi
callq 0x28118d
movl %eax, %ebp
testl %ebp, %ebp
sete %r12b
jne 0x29a784
incq %r15
movslq 0x28(%rbx), %rax
addq $0x20, %r13
cmpq %rax, %r15
jl 0x29a858
jmp 0x29a784