// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rdi, %rbx
movq 0x1e0(%rdi), %r12
movq $0x0, 0x1e0(%rdi)
cmpl $0x0, 0x1bc(%rdi)
jle 0x423e13
movq %rsi, %r14
xorl %r13d, %r13d
movq (%r12,%r13,8), %rax
movq 0x10(%rax), %r15
movl $0x0, %ebp
testq %r15, %r15
je 0x423dfe
movq (%r15), %rax
movq 0x78(%rax), %rax
testq %rax, %rax
je 0x423dfe
movq %r15, %rdi
callq *%rax
movl %eax, %ebp
movq %r14, %rdi
movq %r15, %rsi
callq 0x424d67
testl %ebp, %ebp
jne 0x423e15
incq %r13
movslq 0x1bc(%rbx), %rax
cmpq %rax, %r13
jl 0x423dce
jmp 0x423e15
xorl %ebp, %ebp
movq %r12, 0x1e0(%rbx)
testl %ebp, %ebp
jne 0x42438a
xorl %r12d, %r12d
movq $-0x1, %r13
movl $0x0, 0x8(%rsp)
xorl %r15d, %r15d
movslq 0x28(%rbx), %rax
incq %r13
cmpq %rax, %r13
jge 0x423ed8
movq 0x20(%rbx), %rax
movq 0x8(%rax,%r12), %r14
xorl %ebp, %ebp
testq %r14, %r14
je 0x423ec7
cmpb $0x2, 0x10(%r14)
jne 0x423ec7
cmpb $0x0, 0x11(%r14)
je 0x423e7a
incl 0x14(%r14)
cmpb $0x0, 0x12(%r14)
jne 0x423e7a
movq %r14, %rdi
callq 0x42017c
movq 0x8(%r14), %rax
movq (%rax), %rdi
movq 0x20(%rbx), %rax
cmpb $0x1, 0x10(%rax,%r12)
je 0x423ea5
movzbl 0x9(%rdi), %eax
movl $0x34, %ecx
btq %rax, %rcx
jb 0x423ea5
cmpb $0x1, 0x10(%rdi)
adcl $0x0, 0x8(%rsp)
callq 0x41c596
movl %eax, %ebp
movl $0x1, %r15d
cmpb $0x0, 0x11(%r14)
je 0x423ec7
decl 0x14(%r14)
jne 0x423ec7
movq %r14, %rdi
callq 0x420202
addq $0x20, %r12
testl %ebp, %ebp
je 0x423e39
jmp 0x42438a
testl %r15d, %r15d
je 0x423eff
movq 0x100(%rbx), %rax
testq %rax, %rax
je 0x423eff
movq 0xf8(%rbx), %rdi
callq *%rax
movl $0x213, %ebp            # imm = 0x213
testl %eax, %eax
jne 0x42438a
movq 0x20(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax), %r14
movb 0x13(%r14), %bpl
testb %bpl, %bpl
je 0x423f20
leaq 0x14e34f(%rip), %rdi    # 0x57226d
jmp 0x423f27
movq 0xd0(%r14), %rdi
testq %rdi, %rdi
je 0x423f3a
callq 0x401100
shlq $0x22, %rax
sete %al
jmp 0x423f3c
movb $0x1, %al
testb %al, %al
jne 0x423f5c
cmpl $0x1, 0x8(%rsp)
jle 0x423f5c
testb %bpl, %bpl
je 0x423fea
leaq 0x14e316(%rip), %r15    # 0x57226d
jmp 0x423ff1
cmpl $0x0, 0x28(%rbx)
jle 0x423f9c
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
movq (%rax,%r14), %rdi
movl $0x0, %ebp
testq %rdi, %rdi
je 0x423f86
xorl %esi, %esi
callq 0x40a1b2
movl %eax, %ebp
testl %ebp, %ebp
jne 0x423f9e
incq %r15
movslq 0x28(%rbx), %rax
addq $0x20, %r14
cmpq %rax, %r15
jl 0x423f6b
jmp 0x423f9e
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x42438a
movq $-0x1, %r14
movl $0x8, %r15d
movslq 0x28(%rbx), %rax
incq %r14
cmpq %rax, %r14
jge 0x42437b
movq 0x20(%rbx), %rax
movq (%rax,%r15), %rdi
testq %rdi, %rdi
je 0x423fdb
xorl %esi, %esi
callq 0x40a079
movl %eax, %ebp
jmp 0x423fdd
xorl %ebp, %ebp
addq $0x20, %r15
testl %ebp, %ebp
je 0x423fb3
jmp 0x42438a
movq 0xd0(%r14), %r15
movq (%rbx), %r14
movq $0x0, 0x28(%rsp)
testq %r15, %r15
je 0x424016
movq %r15, %rdi
callq 0x401100
movq %rax, %r12
andl $0x3fffffff, %r12d      # imm = 0x3FFFFFFF
jmp 0x424019
xorl %r12d, %r12d
leaq 0x12ca61(%rip), %rsi    # 0x550a81
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x40e309
testq %rax, %rax
je 0x424129
movq %rax, 0x8(%rsp)
addq %rax, %r12
xorl %r15d, %r15d
testl %r15d, %r15d
movq 0x8(%rsp), %rbp
je 0x424096
cmpl $0x65, %r15d
jl 0x42407a
xorl %r13d, %r13d
movl $0xd, %edi
leaq 0x12ca31(%rip), %rsi    # 0x550a91
movq %rbp, %rdx
xorl %eax, %eax
callq 0x40743a
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq *0x30(%r14)
xorl %ebp, %ebp
jmp 0x4240df
cmpl $0x1, %r15d
jne 0x424096
movl $0xd, %edi
leaq 0x12ca13(%rip), %rsi    # 0x550a9f
movq %rbp, %rdx
xorl %eax, %eax
callq 0x40743a
incl %r15d
movl $0x4, %edi
leaq 0x30(%rsp), %rsi
callq 0x4075b0
movl 0x30(%rsp), %ecx
movzbl %cl, %r8d
shrl $0x8, %ecx
movl $0xd, %edi
movq %r12, %rsi
leaq 0x12c9ec(%rip), %rdx    # 0x550aae
xorl %eax, %eax
callq 0x407373
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
leaq 0x34(%rsp), %rcx
callq *0x38(%r14)
movl %eax, %ebp
movb $0x1, %r13b
testb %r13b, %r13b
je 0x4240f3
testl %ebp, %ebp
jne 0x4240f3
cmpl $0x0, 0x34(%rsp)
jne 0x424041
testl %ebp, %ebp
jne 0x424113
leaq 0x28(%rsp), %rdx
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movl $0x4016, %ecx           # imm = 0x4016
xorl %r8d, %r8d
callq 0x424cd2
movl %eax, %ebp
testl %ebp, %ebp
je 0x424133
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x415dca
jmp 0x42438a
movl $0x7, %ebp
jmp 0x42438a
movq %r14, 0x20(%rsp)
cmpl $0x0, 0x28(%rbx)
jle 0x424262
xorl %r12d, %r12d
movl $0x8, %r15d
movq 0x28(%rsp), %rcx
xorl %edx, %edx
xorl %r14d, %r14d
movq %rcx, 0x10(%rsp)
movq 0x20(%rbx), %rax
movq (%rax,%r15), %rax
testq %rax, %rax
je 0x42421e
cmpb $0x2, 0x10(%rax)
jne 0x42421e
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0xd8(%rax), %r13
testq %r13, %r13
je 0x424225
movq %rcx, %rbp
movq %r13, %rdi
callq 0x401100
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
leal 0x1(%rax), %edx
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq *0x18(%rax)
movl %eax, %ebp
movq %r13, %rdi
callq 0x401100
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
addq %rax, %r14
incq %r14
testl %ebp, %ebp
je 0x424231
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x4241e9
movq 0x10(%rsp), %rdi
callq *0x8(%rax)
movq 0x10(%rsp), %rdi
movq $0x0, (%rdi)
callq 0x405420
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %r13
movq %r13, %rsi
xorl %edx, %edx
callq *0x30(%rdi)
movq %r13, %rsi
movl $0x1, %r13d
testq %rsi, %rsi
je 0x424216
movq %rbx, %rdi
callq 0x415dca
movl %ebp, %edx
movl %ebp, 0x1c(%rsp)
jmp 0x424238
movl %edx, %ebp
xorl %r13d, %r13d
jmp 0x42423d
movl $0xf, %r13d
movl 0x1c(%rsp), %ebp
jmp 0x42423d
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %edx, %edx
movq 0x10(%rsp), %rcx
cmpl $0xf, %r13d
je 0x42424c
testl %r13d, %r13d
jne 0x42438a
incq %r12
movslq 0x28(%rbx), %rax
addq $0x20, %r15
cmpq %rax, %r12
jl 0x42415a
jmp 0x424264
xorl %edx, %edx
movq 0x28(%rsp), %r15
movq (%r15), %rax
movq %r15, %rdi
movl %edx, %ebp
callq *0x60(%rax)
btl $0xa, %eax
movq 0x20(%rsp), %r14
jb 0x4242ce
movq (%r15), %rax
movq %r15, %rdi
movl $0x2, %esi
callq *0x28(%rax)
testl %eax, %eax
je 0x4242cc
movl %eax, %ebp
movq (%r15), %rax
testq %rax, %rax
je 0x4242a8
movq %r15, %rdi
callq *0x8(%rax)
movq $0x0, (%r15)
movq %r15, %rdi
callq 0x405420
movq %r14, %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
callq *0x30(%r14)
movq %rbx, %rdi
movq %r15, %rsi
jmp 0x42411f
xorl %ebp, %ebp
movq %r15, %r14
testl %ebp, %ebp
sete %r15b
je 0x42439b
movq (%r14), %rax
testq %rax, %rax
je 0x4242f2
movq %r14, %rdi
callq *0x8(%rax)
movq $0x0, (%r14)
movq %r14, %rdi
callq 0x405420
testb %r15b, %r15b
je 0x424117
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
movl $0x1, %edx
callq *0x30(%rdi)
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x415dca
testl %ebp, %ebp
jne 0x42438a
movq 0x180a88(%rip), %rax    # 0x5a4db8
testq %rax, %rax
je 0x424337
callq *%rax
cmpl $0x0, 0x28(%rbx)
jle 0x42436d
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
movq (%rax,%r14), %rdi
testq %rdi, %rdi
je 0x42435d
movl $0x1, %esi
callq 0x40a079
incq %r15
movslq 0x28(%rbx), %rax
addq $0x20, %r14
cmpq %rax, %r15
jl 0x424346
movq 0x180a4c(%rip), %rax    # 0x5a4dc0
testq %rax, %rax
je 0x42437b
callq *%rax
movq %rbx, %rdi
movl $0x80, %esi
callq 0x424b2e
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x28(%rbx)
jle 0x4242dd
movl $0x8, %r13d
xorl %r12d, %r12d
movq 0x20(%rbx), %rax
movq (%rax,%r13), %rdi
movl $0x0, %ebp
testq %rdi, %rdi
je 0x4243cc
movq 0x8(%rsp), %rsi
callq 0x40a1b2
movl %eax, %ebp
testl %ebp, %ebp
sete %r15b
jne 0x4242dd
incq %r12
movslq 0x28(%rbx), %rax
addq $0x20, %r13
cmpq %rax, %r12
jl 0x4243ae
jmp 0x4242dd