// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3LeaveMutexAndCloseZombie
SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
  HashElem *i;                    /* Hash table iterator */
  int j;

  /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
  ** or if the connection has not yet been closed by sqlite3_close_v2(),
  ** then just leave the mutex and return.
  */
  if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
    sqlite3_mutex_leave(db->mutex);
    return;
  }

  /* If we reach this point, it means that the database connection has
  ** closed all sqlite3_stmt and sqlite3_backup objects and has been
  ** passed to sqlite3_close (meaning that it is a zombie).  Therefore,
  ** go ahead and free all resources.
  */

  /* If a transaction is open, roll it back. This also ensures that if
  ** any database schemas have been modified by an uncommitted transaction
  ** they are reset. And that the required b-tree mutex is held to make
  ** the pager rollback and schema reset an atomic operation. */
  sqlite3RollbackAll(db, SQLITE_OK);

  /* Free any outstanding Savepoint structures. */
  sqlite3CloseSavepoints(db);

  /* Close all database connections */
  for(j=0; j<db->nDb; j++){
    struct Db *pDb = &db->aDb[j];
    if( pDb->pBt ){
      sqlite3BtreeClose(pDb->pBt);
      pDb->pBt = 0;
      if( j!=1 ){
        pDb->pSchema = 0;
      }
    }
  }
  /* Clear the TEMP schema separately and last */
  if( db->aDb[1].pSchema ){
    sqlite3SchemaClear(db->aDb[1].pSchema);
  }
  sqlite3VtabUnlockList(db);

  /* Free up the array of auxiliary databases */
  sqlite3CollapseDatabaseArray(db);
  assert( db->nDb<=2 );
  assert( db->aDb==db->aDbStatic );

  /* Tell the code in notify.c that the connection no longer holds any
  ** locks and does not require any further unlock-notify callbacks.
  */
  sqlite3ConnectionClosed(db);

  for(j=0; j<ArraySize(db->aFunc.a); j++){
    FuncDef *pNext, *pHash, *p;
    for(p=db->aFunc.a[j]; p; p=pHash){
      pHash = p->pHash;
      while( p ){
        functionDestroy(db, p);
        pNext = p->pNext;
        sqlite3DbFree(db, p);
        p = pNext;
      }
    }
  }
  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
    /* Invoke any destructors registered for collation sequence user data. */
    for(j=0; j<3; j++){
      if( pColl[j].xDel ){
        pColl[j].xDel(pColl[j].pUser);
      }
    }
    sqlite3DbFree(db, pColl);
  }
  sqlite3HashClear(&db->aCollSeq);
#ifndef SQLITE_OMIT_VIRTUALTABLE
  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
    Module *pMod = (Module *)sqliteHashData(i);
    if( pMod->xDestroy ){
      pMod->xDestroy(pMod->pAux);
    }
    sqlite3DbFree(db, pMod);
  }
  sqlite3HashClear(&db->aModule);
#endif

  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
  if( db->pErr ){
    sqlite3ValueFree(db->pErr);
  }
  sqlite3CloseExtensions(db);

  db->magic = SQLITE_MAGIC_ERROR;

  /* The temp-database schema is allocated differently from the other schema
  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
  ** So it needs to be freed here. Todo: Why not roll the temp schema into
  ** the same sqliteMalloc() as the one that allocates the database 
  ** structure?
  */
  sqlite3DbFree(db, db->aDb[1].pSchema);
  sqlite3_mutex_leave(db->mutex);
  db->magic = SQLITE_MAGIC_CLOSED;
  sqlite3_mutex_free(db->mutex);
  assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
  if( db->lookaside.bMalloced ){
    sqlite3_free(db->lookaside.pStart);
  }
  sqlite3_free(db);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x64cffc7f, 0x5c(%rdi) # imm = 0x64CFFC7F
jne 0xd9a1
movq %rdi, %rbx
callq 0x5b7db
testl %eax, %eax
je 0xd9b0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2067d
movq %rbx, %rdi
callq 0x2076b
pushq $0x8
popq %r15
movslq 0x28(%rbx), %rax
movq 0x20(%rbx), %r12
cmpq %rax, %r14
jge 0xd9fe
movq (%r12,%r15), %rdi
testq %rdi, %rdi
je 0xd9f5
callq 0x20ae6
andq $0x0, (%r12,%r15)
cmpq $0x1, %r14
je 0xd9f5
andq $0x0, 0x10(%r12,%r15)
incq %r14
addq $0x20, %r15
jmp 0xd9c9
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0xda0d
callq 0x2bdcf
movq %rbx, %rdi
callq 0x316c4
movq %rbx, %rdi
callq 0x316fd
xorl %r15d, %r15d
cmpq $0x17, %r15
je 0xda65
movq 0x1d8(%rbx,%r15,8), %r14
testq %r14, %r14
je 0xda60
movq 0x38(%r14), %r12
testq %r14, %r14
je 0xda5b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5b81d
movq 0x10(%r14), %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x968b
movq %r13, %r14
jmp 0xda37
movq %r12, %r14
jmp 0xda2e
incq %r15
jmp 0xda20
leaq 0x298(%rbx), %r15
pushq $0x20
popq %r12
movq (%r15), %r15
testq %r15, %r15
je 0xdaab
movq 0x10(%r15), %r14
movq %r12, %r13
cmpq $0x98, %r13
je 0xda9e
movq (%r14,%r13), %rax
testq %rax, %rax
je 0xda98
movq -0x10(%r14,%r13), %rdi
callq *%rax
addq $0x28, %r13
jmp 0xda7f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x968b
jmp 0xda70
leaq 0x290(%rbx), %rdi
callq 0x2be96
leaq 0x1b0(%rbx), %r15
movq (%r15), %r15
testq %r15, %r15
je 0xdae6
movq 0x10(%r15), %r14
movq 0x18(%r14), %rax
testq %rax, %rax
je 0xdad9
movq 0x10(%r14), %rdi
callq *%rax
movq %rbx, %rdi
movq %r14, %rsi
callq 0x968b
jmp 0xdabe
leaq 0x1a8(%rbx), %rdi
callq 0x2be96
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0xbdb1
movq 0x130(%rbx), %rdi
testq %rdi, %rdi
je 0xdb11
callq 0x12a43
movl $0xb5357930, 0x5c(%rbx) # imm = 0xB5357930
movq 0x20(%rbx), %rax
movq 0x38(%rax), %rsi
movq %rbx, %rdi
callq 0x968b
movl $0x9f3c2d33, 0x5c(%rbx) # imm = 0x9F3C2D33
cmpb $0x0, 0x153(%rbx)
je 0xdb44
movq 0x170(%rbx), %rdi
callq 0x9c7b
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9c7b