// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: attachFunc
static void attachFunc(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **argv
){
  int i;
  int rc = 0;
  sqlite3 *db = sqlite3_context_db_handle(context);
  const char *zName;
  const char *zFile;
  char *zPath = 0;
  char *zErr = 0;
  unsigned int flags;
  Db *aNew;                 /* New array of Db pointers */
  Db *pNew;                 /* Db object for the newly attached database */
  char *zErrDyn = 0;
  sqlite3_vfs *pVfs;

  UNUSED_PARAMETER(NotUsed);
  zFile = (const char *)sqlite3_value_text(argv[0]);
  zName = (const char *)sqlite3_value_text(argv[1]);
  if( zFile==0 ) zFile = "";
  if( zName==0 ) zName = "";

#ifdef SQLITE_ENABLE_DESERIALIZE
# define REOPEN_AS_MEMDB(db)  (db->init.reopenMemdb)
#else
# define REOPEN_AS_MEMDB(db)  (0)
#endif

  if( REOPEN_AS_MEMDB(db) ){
    /* This is not a real ATTACH.  Instead, this routine is being called
    ** from sqlite3_deserialize() to close database db->init.iDb and
    ** reopen it as a MemDB */
    pVfs = sqlite3_vfs_find("memdb");
    if( pVfs==0 ) return;
    pNew = &db->aDb[db->init.iDb];
    if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
    pNew->pBt = 0;
    pNew->pSchema = 0;
    rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
  }else{
    /* This is a real ATTACH
    **
    ** Check for the following errors:
    **
    **     * Too many attached databases,
    **     * Transaction currently open
    **     * Specified database name already being used.
    */
    if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
      zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
        db->aLimit[SQLITE_LIMIT_ATTACHED]
      );
      goto attach_error;
    }
    for(i=0; i<db->nDb; i++){
      assert( zName );
      if( sqlite3DbIsNamed(db, i, zName) ){
        zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
        goto attach_error;
      }
    }

    /* Allocate the new entry in the db->aDb[] array and initialize the schema
    ** hash tables.
    */
    if( db->aDb==db->aDbStatic ){
      aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
      if( aNew==0 ) return;
      memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
    }else{
      aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
      if( aNew==0 ) return;
    }
    db->aDb = aNew;
    pNew = &db->aDb[db->nDb];
    memset(pNew, 0, sizeof(*pNew));

    /* Open the database file. If the btree is successfully opened, use
    ** it to obtain the database schema. At this point the schema may
    ** or may not be initialized.
    */
    flags = db->openFlags;
    rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
    if( rc!=SQLITE_OK ){
      if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
      sqlite3_result_error(context, zErr, -1);
      sqlite3_free(zErr);
      return;
    }
    assert( pVfs );
    flags |= SQLITE_OPEN_MAIN_DB;
    rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
    db->nDb++;
    pNew->zDbSName = sqlite3DbStrDup(db, zName);
  }
  db->noSharedCache = 0;
  if( rc==SQLITE_CONSTRAINT ){
    rc = SQLITE_ERROR;
    zErrDyn = sqlite3MPrintf(db, "database is already attached");
  }else if( rc==SQLITE_OK ){
    Pager *pPager;
    pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
    if( !pNew->pSchema ){
      rc = SQLITE_NOMEM_BKPT;
    }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
      zErrDyn = sqlite3MPrintf(db,
        "attached databases must use the same text encoding as main database");
      rc = SQLITE_ERROR;
    }
    sqlite3BtreeEnter(pNew->pBt);
    pPager = sqlite3BtreePager(pNew->pBt);
    sqlite3PagerLockingMode(pPager, db->dfltLockMode);
    sqlite3BtreeSecureDelete(pNew->pBt,
                             sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
    sqlite3BtreeSetPagerFlags(pNew->pBt,
                      PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
#endif
    sqlite3BtreeLeave(pNew->pBt);
  }
  pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
  if( rc==SQLITE_OK && pNew->zDbSName==0 ){
    rc = SQLITE_NOMEM_BKPT;
  }
  sqlite3_free_filename( zPath );

  /* If the file was opened successfully, read the schema for the new database.
  ** If this fails, or if opening the file failed, then close the file and
  ** remove the entry from the db->aDb[] array. i.e. put everything back the
  ** way we found it.
  */
  if( rc==SQLITE_OK ){
    sqlite3BtreeEnterAll(db);
    db->init.iDb = 0;
    db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
    if( !REOPEN_AS_MEMDB(db) ){
      rc = sqlite3Init(db, &zErrDyn);
    }
    sqlite3BtreeLeaveAll(db);
    assert( zErrDyn==0 || rc!=SQLITE_OK );
  }
#ifdef SQLITE_USER_AUTHENTICATION
  if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
    u8 newAuth = 0;
    rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
    if( newAuth<db->auth.authLevel ){
      rc = SQLITE_AUTH_USER;
    }
  }
#endif
  if( rc ){
    if( !REOPEN_AS_MEMDB(db) ){
      int iDb = db->nDb - 1;
      assert( iDb>=2 );
      if( db->aDb[iDb].pBt ){
        sqlite3BtreeClose(db->aDb[iDb].pBt);
        db->aDb[iDb].pBt = 0;
        db->aDb[iDb].pSchema = 0;
      }
      sqlite3ResetAllSchemasOfConnection(db);
      db->nDb = iDb;
      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
        sqlite3OomFault(db);
        sqlite3DbFree(db, zErrDyn);
        zErrDyn = sqlite3MPrintf(db, "out of memory");
      }else if( zErrDyn==0 ){
        zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
      }
    }
    goto attach_error;
  }

  return;

attach_error:
  /* Return an error if we get here */
  if( zErrDyn ){
    sqlite3_result_error(context, zErrDyn, -1);
    sqlite3DbFree(db, zErrDyn);
  }
  if( rc ) sqlite3_result_error_code(context, rc);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x28(%rax), %r14
andq $0x0, 0x28(%rsp)
movq %rdx, %r12
andq $0x0, 0x20(%rsp)
andq $0x0, 0x8(%rsp)
movq (%rdx), %rdi
callq 0x2243e3
movq %rax, %r15
movq 0x8(%r12), %rdi
callq 0x2243e3
testq %r15, %r15
leaq 0xe6ada(%rip), %rcx     # 0x36a570
cmoveq %rcx, %r15
movq %rax, %r13
testq %rax, %rax
cmoveq %rcx, %r13
movslq 0x28(%r14), %r12
movl 0x98(%r14), %edx
leal 0x2(%rdx), %eax
cmpl %eax, %r12d
jge 0x283b07
movq %rbx, 0x10(%rsp)
xorl %ebp, %ebp
testl %r12d, %r12d
movl $0x0, %ebx
cmovgl %r12d, %ebx
cmpl %ebp, %ebx
je 0x283b5c
movq %r14, %rdi
movl %ebp, %esi
movq %r13, %rdx
callq 0x24b1aa
incl %ebp
testl %eax, %eax
je 0x283aca
leaq 0xf5d75(%rip), %rsi     # 0x379861
xorl %ebp, %ebp
movq %r14, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x226ec3
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rbx
jmp 0x283b1f
leaq 0xf5d2e(%rip), %rsi     # 0x37983c
xorl %ebp, %ebp
movq %r14, %rdi
xorl %eax, %eax
callq 0x226ec3
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x283b3f
pushq $-0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2246d3
movq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x21f26b
testl %ebp, %ebp
je 0x283b4d
movq %rbx, %rdi
movl %ebp, %esi
callq 0x224aee
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%r14), %rsi
leaq 0x268(%r14), %rax
cmpq %rax, %rsi
je 0x283b8e
shlq $0x5, %r12
addq $0x20, %r12
movq %r14, %rdi
movq %r12, %rdx
callq 0x22d1b5
movq %rax, %r12
testq %rax, %rax
movq 0x10(%rsp), %rbx
jne 0x283bd0
jmp 0x283b4d
pushq $0x60
popq %rsi
movq %r14, %rdi
callq 0x22d205
testq %rax, %rax
movq 0x10(%rsp), %rbx
je 0x283b4d
movq %rax, %r12
movq 0x20(%r14), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups 0x30(%rax), %xmm3
movups %xmm3, 0x30(%r12)
movups %xmm2, 0x20(%r12)
movups %xmm1, 0x10(%r12)
movups %xmm0, (%r12)
movq %r12, 0x20(%r14)
movslq 0x28(%r14), %rbp
shlq $0x5, %rbp
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r12,%rbp)
movups %xmm0, (%r12,%rbp)
movl 0x4c(%r14), %eax
leaq 0x18(%rsp), %rdx
movl %eax, (%rdx)
movq (%r14), %rax
movq 0x18(%rax), %rdi
leaq 0x30(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x20(%rsp), %r9
movq %r15, %rsi
callq 0x283fcb
testl %eax, %eax
je 0x283c4d
cmpl $0x7, %eax
jne 0x283c24
movq %r14, %rdi
callq 0x224bb1
movq 0x20(%rsp), %r14
pushq $-0x1
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2246d3
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f7f9
addq %rbp, %r12
movl $0x100, %r9d            # imm = 0x100
orl 0x18(%rsp), %r9d
movl %r9d, 0x18(%rsp)
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rbx
leaq 0x8(%r12), %rcx
movq %rbx, %rsi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0x22f0cc
movl %eax, %ebp
incl 0x28(%r14)
movq %r14, %rdi
movq %r13, %rsi
callq 0x22572a
movq %rax, (%r12)
movb $0x0, 0x69(%r14)
testl %ebp, %ebp
je 0x283d70
cmpl $0x13, %ebp
jne 0x283cbd
leaq 0xf5bd4(%rip), %rsi     # 0x37987f
movq %r14, %rdi
xorl %eax, %eax
callq 0x226ec3
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rbp
movb $0x3, 0x10(%r12)
movq %rbx, %rdi
callq 0x22cc7d
testl %ebp, %ebp
jne 0x283cfb
movq %r14, %rdi
callq 0x21f0e2
movb $0x0, 0xb4(%r14)
andb $-0x11, 0x2c(%r14)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x22be24
movl %eax, %ebp
testl %eax, %eax
je 0x283b4d
movslq 0x28(%r14), %r12
decq %r12
movq 0x20(%r14), %rax
movq %r12, %rbx
shlq $0x5, %rbx
movq 0x8(%rax,%rbx), %rdi
testq %rdi, %rdi
je 0x283d2c
callq 0x239820
movq 0x20(%r14), %rax
andq $0x0, 0x8(%rax,%rbx)
andq $0x0, 0x18(%rax,%rbx)
movq %r14, %rdi
callq 0x2235f9
movl %r12d, 0x28(%r14)
cmpl $0xc0a, %ebp            # imm = 0xC0A
je 0x283d45
cmpl $0x7, %ebp
jne 0x283db9
movq %r14, %rdi
callq 0x224bb1
movq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x21f26b
leaq 0x908d0(%rip), %rsi     # 0x314631
movq %r14, %rdi
xorl %eax, %eax
callq 0x226ec3
jmp 0x283afb
movq 0x8(%r12), %rsi
movq %r14, %rdi
callq 0x2844ce
movq %rax, 0x18(%r12)
xorl %ebp, %ebp
testq %rax, %rax
je 0x283de5
cmpb $0x0, 0x70(%rax)
je 0x283ded
movb 0x71(%rax), %al
cmpb 0x5e(%r14), %al
movb $0x1, %r13b
je 0x283df0
leaq 0xf2ced(%rip), %rsi     # 0x376a8f
xorl %r13d, %r13d
movq %r14, %rdi
xorl %eax, %eax
callq 0x226ec3
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rbp
jmp 0x283df0
movq 0x8(%rsp), %rax
testq %rax, %rax
movq 0x10(%rsp), %rbx
jne 0x283b24
leaq 0xf5ac9(%rip), %rsi     # 0x37989c
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x226ec3
jmp 0x283b1a
pushq $0x7
popq %rbp
xorl %r13d, %r13d
jmp 0x283df0
movb $0x1, %r13b
movl %r13d, 0x1c(%rsp)
movq 0x8(%r12), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0x8(%rax)
movq (%rax), %rdi
movzbl 0x63(%r14), %esi
callq 0x28342c
movq 0x8(%r12), %r13
movq 0x20(%r14), %rax
movq 0x8(%rax), %rdi
pushq $-0x1
popq %rsi
callq 0x2833fc
movq %r13, %rdi
movl %eax, %esi
callq 0x2833fc
movq 0x8(%r12), %rdi
movl 0x30(%r14), %esi
andl $0x38, %esi
orl $0x3, %esi
callq 0x24c8a6
movb $0x3, 0x10(%r12)
cmpb $0x0, 0x1c(%rsp)
je 0x283cc3
xorl %eax, %eax
cmpq $0x0, (%r12)
pushq $0x7
popq %rbp
cmovnel %eax, %ebp
jmp 0x283cc3