// File: /workspace/github_c_makefile_star2_O3/ukontainer[P]sqlite-bench/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
                   ** master-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 master 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 master 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 master journal depends upon
      ** its journal mode (among other things).  This matrix determines which
      ** journal modes use a master 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
  ** master-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 master journal file to ensure the transaction is
  ** committed atomically.
  */
#ifndef SQLITE_OMIT_DISKIO
  else{
    sqlite3_vfs *pVfs = db->pVfs;
    char *zMaster = 0;   /* File-name for the master journal */
    char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
    sqlite3_file *pMaster = 0;
    i64 offset = 0;
    int res;
    int retryCount = 0;
    int nMainFile;

    /* Select a master journal file name */
    nMainFile = sqlite3Strlen30(zMainFile);
    zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
    if( zMaster==0 ) return SQLITE_NOMEM_BKPT;
    do {
      u32 iRandom;
      if( retryCount ){
        if( retryCount>100 ){
          sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
          sqlite3OsDelete(pVfs, zMaster, 0);
          break;
        }else if( retryCount==1 ){
          sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
        }
      }
      retryCount++;
      sqlite3_randomness(sizeof(iRandom), &iRandom);
      sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
                               (iRandom>>8)&0xffffff, iRandom&0xff);
      /* The antipenultimate character of the master journal name must
      ** be "9" to avoid name collisions when using 8+3 filenames. */
      assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
      sqlite3FileSuffix3(zMainFile, zMaster);
      rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
    }while( rc==SQLITE_OK && res );
    if( rc==SQLITE_OK ){
      /* Open the master journal. */
      rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 
          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
          SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
      );
    }
    if( rc!=SQLITE_OK ){
      sqlite3DbFree(db, zMaster);
      return rc;
    }
 
    /* Write the name of each database file in the transaction into the new
    ** master journal file. If an error occurs at this point close
    ** and delete the master journal file. All the individual journal files
    ** still have 'null' as the master 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(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
        offset += sqlite3Strlen30(zFile)+1;
        if( rc!=SQLITE_OK ){
          sqlite3OsCloseFree(pMaster);
          sqlite3OsDelete(pVfs, zMaster, 0);
          sqlite3DbFree(db, zMaster);
          return rc;
        }
      }
    }

    /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
    ** flag is set this is not required.
    */
    if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
     && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
    ){
      sqlite3OsCloseFree(pMaster);
      sqlite3OsDelete(pVfs, zMaster, 0);
      sqlite3DbFree(db, zMaster);
      return rc;
    }

    /* Sync all the db files involved in the transaction. The same call
    ** sets the master journal pointer in each individual journal. If
    ** an error occurs here, do not delete the master journal file.
    **
    ** If the error occurs during the first call to
    ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
    ** master journal file will be orphaned. But we cannot delete it,
    ** in case the master 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, zMaster);
      }
    }
    sqlite3OsCloseFree(pMaster);
    assert( rc!=SQLITE_BUSY );
    if( rc!=SQLITE_OK ){
      sqlite3DbFree(db, zMaster);
      return rc;
    }

    /* Delete the master journal file. This commits the transaction. After
    ** doing this the directory is synced again before any individual
    ** transaction files are deleted.
    */
    rc = sqlite3OsDelete(pVfs, zMaster, 1);
    sqlite3DbFree(db, zMaster);
    zMaster = 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 $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x1e0(%rdi), %r12
movq $0x0, 0x1e0(%rdi)
xorl %r13d, %r13d
movslq 0x1bc(%rbx), %rax
cmpq %rax, %r13
jge 0x423db0
movq (%r12,%r13,8), %rax
movq 0x10(%rax), %r15
xorl %ebp, %ebp
testq %r15, %r15
je 0x423d9d
movq (%r15), %rax
movq 0x78(%rax), %rax
testq %rax, %rax
je 0x423d9d
movq %r15, %rdi
callq *%rax
movl %eax, %ebp
movq %r14, %rdi
movq %r15, %rsi
callq 0x424c4c
incq %r13
testl %ebp, %ebp
je 0x423d64
movq %r12, 0x1e0(%rbx)
jmp 0x42427a
movq %r12, 0x1e0(%rbx)
xorl %r12d, %r12d
movq $-0x1, %r13
movl $0x0, (%rsp)
xorl %r15d, %r15d
movslq 0x28(%rbx), %rax
incq %r13
cmpq %rax, %r13
jge 0x423e69
movq 0x20(%rbx), %rax
movq 0x8(%rax,%r12), %r14
xorl %ebp, %ebp
testq %r14, %r14
je 0x423e58
cmpb $0x2, 0x10(%r14)
jne 0x423e58
cmpb $0x0, 0x11(%r14)
je 0x423e10
incl 0x14(%r14)
cmpb $0x0, 0x12(%r14)
jne 0x423e10
movq %r14, %rdi
callq 0x420275
movq 0x20(%rbx), %rax
movq 0x8(%r14), %rcx
movq (%rcx), %rdi
cmpb $0x1, 0x10(%rax,%r12)
je 0x423e36
movzbl 0x9(%rdi), %eax
movl $0x34, %ecx
btq %rax, %rcx
jb 0x423e36
cmpb $0x1, 0x10(%rdi)
adcl $0x0, (%rsp)
callq 0x41c3bc
movl %eax, %ebp
movl $0x1, %r15d
cmpb $0x0, 0x11(%r14)
je 0x423e58
decl 0x14(%r14)
jne 0x423e58
movq %r14, %rdi
callq 0x4202fb
addq $0x20, %r12
testl %ebp, %ebp
je 0x423dcb
jmp 0x42427a
testl %r15d, %r15d
je 0x423e90
movq 0x100(%rbx), %rax
testq %rax, %rax
je 0x423e90
movq 0xf8(%rbx), %rdi
callq *%rax
movl $0x213, %ebp            # imm = 0x213
testl %eax, %eax
jne 0x42427a
movq 0x20(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %r14
movb 0x13(%r14), %bpl
testb %bpl, %bpl
je 0x423eb1
leaq 0x14e49e(%rip), %rdi    # 0x57234d
jmp 0x423ebd
movq 0xd0(%r14), %rdi
testq %rdi, %rdi
je 0x423eef
callq 0x401100
shlq $0x22, %rax
je 0x423eef
cmpl $0x1, (%rsp)
jle 0x423eef
movq (%rbx), %r15
testb %bpl, %bpl
je 0x423f73
movq $0x0, 0x18(%rsp)
leaq 0x14e463(%rip), %r14    # 0x57234d
jmp 0x423f8c
movq $-0x1, %r14
movl $0x8, %r15d
movslq 0x28(%rbx), %rax
incq %r14
cmpq %rax, %r14
jge 0x423f2f
movq 0x20(%rbx), %rax
movq (%rax,%r15), %rdi
testq %rdi, %rdi
je 0x423f20
xorl %esi, %esi
callq 0x40a38c
movl %eax, %ebp
jmp 0x423f22
xorl %ebp, %ebp
addq $0x20, %r15
testl %ebp, %ebp
je 0x423efc
jmp 0x42427a
movq $-0x1, %r14
movl $0x8, %r15d
movslq 0x28(%rbx), %rax
incq %r14
cmpq %rax, %r14
jge 0x42426b
movq 0x20(%rbx), %rax
movq (%rax,%r15), %rdi
testq %rdi, %rdi
je 0x423f64
xorl %esi, %esi
callq 0x40a22c
movl %eax, %ebp
jmp 0x423f66
xorl %ebp, %ebp
addq $0x20, %r15
testl %ebp, %ebp
je 0x423f3c
jmp 0x42427a
movq 0xd0(%r14), %r14
movq $0x0, 0x18(%rsp)
testq %r14, %r14
je 0x42417d
movq %r14, %rdi
callq 0x401100
movq %rax, %r12
andl $0x3fffffff, %r12d      # imm = 0x3FFFFFFF
leaq 0x12cb64(%rip), %rsi    # 0x550b09
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x40e55c
testq %rax, %rax
je 0x424058
movq %r15, 0x8(%rsp)
movq %rax, (%rsp)
addq %rax, %r12
leaq 0x12cb68(%rip), %r15    # 0x550b36
leaq 0x24(%rsp), %r14
xorl %r13d, %r13d
testl %r13d, %r13d
movq (%rsp), %rbp
je 0x424001
cmpl $0x64, %r13d
ja 0x424062
cmpl $0x1, %r13d
jne 0x424001
movl $0xd, %edi
leaq 0x12cb30(%rip), %rsi    # 0x550b27
movq %rbp, %rdx
xorl %eax, %eax
callq 0x40765d
movl $0x4, %edi
leaq 0x20(%rsp), %rsi
callq 0x4077d3
movl 0x20(%rsp), %ecx
movzbl %cl, %r8d
shrl $0x8, %ecx
movl $0xd, %edi
movq %r12, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x407596
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %r14, %rcx
callq *0x38(%rdi)
movl %eax, %ebp
testl %eax, %eax
jne 0x42404d
incl %r13d
cmpl $0x0, 0x24(%rsp)
jne 0x423fd6
testl %ebp, %ebp
movq 0x8(%rsp), %r14
jne 0x4240a8
jmp 0x424089
movl $0x7, %ebp
jmp 0x42427a
leaq 0x12cab0(%rip), %rsi    # 0x550b19
movl $0xd, %edi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x40765d
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq *0x30(%r14)
leaq 0x18(%rsp), %rdx
movq %r14, %rdi
movq (%rsp), %rsi
movl $0x4016, %ecx           # imm = 0x4016
xorl %r8d, %r8d
callq 0x424bb7
testl %eax, %eax
je 0x4240b9
movl %eax, %ebp
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x415f0a
jmp 0x42427a
movl 0x28(%rbx), %eax
movq 0x18(%rsp), %rcx
movq %rcx, 0x10(%rsp)
testl %eax, %eax
jle 0x42414e
movl $0x8, %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x20(%rbx), %rcx
movq (%rcx,%r15), %rcx
testq %rcx, %rcx
je 0x42413f
cmpb $0x2, 0x10(%rcx)
jne 0x42413f
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
movq 0xd8(%rcx), %r14
testq %r14, %r14
je 0x42413f
movq %r14, %rdi
callq 0x401100
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
leal 0x1(%rax), %edx
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %r13, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x42428b
movq %r14, %rdi
callq 0x401100
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 0x4240da
movq 0x10(%rsp), %r13
movq (%r13), %rax
movq %r13, %rdi
callq *0x60(%rax)
btl $0xa, %eax
jb 0x424188
movq (%r13), %rax
movq %r13, %rdi
movl $0x2, %esi
callq *0x28(%rax)
testl %eax, %eax
je 0x424188
movl %eax, %ebp
jmp 0x424292
xorl %r14d, %r14d
xorl %r12d, %r12d
jmp 0x423f9e
movq $-0x1, %r14
movl $0x8, %r12d
movslq 0x28(%rbx), %r15
incq %r14
cmpq %r15, %r14
jge 0x4241c7
movq 0x20(%rbx), %rax
movq (%rax,%r12), %rdi
testq %rdi, %rdi
je 0x4241bb
movq (%rsp), %rsi
callq 0x40a38c
movl %eax, %ebp
jmp 0x4241bd
xorl %ebp, %ebp
addq $0x20, %r12
testl %ebp, %ebp
je 0x424195
jmp 0x4241c9
xorl %ebp, %ebp
movq (%r13), %rax
testq %rax, %rax
je 0x4241e0
movq %r13, %rdi
callq *0x8(%rax)
movq $0x0, (%r13)
movq %r13, %rdi
callq 0x4054f6
cmpq %r15, %r14
jl 0x4240a8
movq 0x8(%rsp), %rdi
movq (%rsp), %r14
movq %r14, %rsi
movl $0x1, %edx
callq *0x30(%rdi)
movl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
callq 0x415f0a
testl %ebp, %ebp
jne 0x42427a
movq 0x180b9b(%rip), %rax    # 0x5a4db8
testq %rax, %rax
je 0x424224
callq *%rax
movl 0x28(%rbx), %eax
testl %eax, %eax
jle 0x42425d
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x20(%rbx), %rcx
movq (%rcx,%r14), %rdi
testq %rdi, %rdi
je 0x42424e
movl $0x1, %esi
callq 0x40a22c
movl 0x28(%rbx), %eax
incq %r15
movslq %eax, %rcx
addq $0x20, %r14
cmpq %rcx, %r15
jl 0x424234
movq 0x180b5c(%rip), %rax    # 0x5a4dc0
testq %rax, %rax
je 0x42426b
callq *%rax
movq %rbx, %rdi
movl $0x80, %esi
callq 0x424a13
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebp
movq 0x10(%rsp), %r13
movq (%r13), %rax
testq %rax, %rax
movq 0x8(%rsp), %r14
je 0x4242ae
movq %r13, %rdi
callq *0x8(%rax)
movq $0x0, (%r13)
movq %r13, %rdi
callq 0x4054f6
movq %r14, %rdi
movq (%rsp), %rsi
xorl %edx, %edx
callq *0x30(%r14)
jmp 0x4240a8