// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: detachFunc
static void detachFunc(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **argv
){
  const char *zName = (const char *)sqlite3_value_text(argv[0]);
  sqlite3 *db = sqlite3_context_db_handle(context);
  int i;
  Db *pDb = 0;
  HashElem *pEntry;
  char zErr[128];

  UNUSED_PARAMETER(NotUsed);

  if( zName==0 ) zName = "";
  for(i=0; i<db->nDb; i++){
    pDb = &db->aDb[i];
    if( pDb->pBt==0 ) continue;
    if( sqlite3DbIsNamed(db, i, zName) ) break;
  }

  if( i>=db->nDb ){
    sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
    goto detach_error;
  }
  if( i<2 ){
    sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
    goto detach_error;
  }
  if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
    sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
    goto detach_error;
  }

  /* If any TEMP triggers reference the schema being detached, move those
  ** triggers to reference the TEMP schema itself. */
  assert( db->aDb[1].pSchema );
  pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
  while( pEntry ){
    Trigger *pTrig = (Trigger*)sqliteHashData(pEntry);
    if( pTrig->pTabSchema==pDb->pSchema ){
      pTrig->pTabSchema = pTrig->pSchema;
    }
    pEntry = sqliteHashNext(pEntry);
  }

  sqlite3BtreeClose(pDb->pBt);
  pDb->pBt = 0;
  pDb->pSchema = 0;
  sqlite3CollapseDatabaseArray(db);
  return;

detach_error:
  sqlite3_result_error(context, zErr, -1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq (%rdx), %rdi
movl $0x1, %esi
callq 0x278305
movq %rbx, 0x8(%rsp)
movq (%rbx), %rcx
testq %rax, %rax
leaq 0xe156d(%rip), %r15     # 0x3ca170
cmovneq %rax, %r15
movq 0x28(%rcx), %rax
movslq 0x28(%rax), %rbx
testq %rbx, %rbx
jle 0x2e8c4b
movq %rax, (%rsp)
movq 0x20(%rax), %rbp
leaq 0x18(%rbp), %r12
xorl %r14d, %r14d
movq -0x10(%r12), %r13
testq %r13, %r13
je 0x2e8c3f
movq %rbp, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq 0x2a5bfa
testl %eax, %eax
jne 0x2e8ca3
incq %r14
addq $0x20, %r12
cmpq %r14, %rbx
jne 0x2e8c23
leaq 0xf08cc(%rip), %rdx     # 0x3d951e
leaq 0x10(%rsp), %r14
movl $0x80, %edi
movq %r14, %rsi
movq %r15, %rcx
xorl %eax, %eax
callq 0x274c41
movq 0x8(%rsp), %rax
movl $0x1, 0x24(%rax)
movq (%rax), %rdi
movq %r14, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq $-0x1, %r8
callq 0x278594
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x2, %r14
jae 0x2e8cb2
leaq 0xf0883(%rip), %rdx     # 0x3d9533
jmp 0x2e8c52
leaq 0xf0894(%rip), %rdx     # 0x3d954d
cmpb $0x0, 0x10(%r13)
jne 0x2e8c52
cmpl $0x0, 0x18(%r13)
jne 0x2e8c52
movq 0x38(%rbp), %rax
movq 0x40(%rax), %rax
testq %rax, %rax
movq (%rsp), %rbx
je 0x2e8cf6
movq (%r12), %rcx
movq 0x10(%rax), %rdx
cmpq %rcx, 0x30(%rdx)
jne 0x2e8cee
movq 0x28(%rdx), %rsi
movq %rsi, 0x30(%rdx)
movq (%rax), %rax
testq %rax, %rax
jne 0x2e8cdc
movq %r13, %rdi
callq 0x290e57
xorl %eax, %eax
movq %rax, -0x10(%r12)
movq %rax, (%r12)
movq %rbx, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2a90a7