// File: /workspace/github_c_makefile_star2_O0/ukontainer[P]sqlite-bench/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", 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++){
      char *z = db->aDb[i].zDbSName;
      assert( z && zName );
      if( sqlite3StrICmp(z, zName)==0 ){
        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);
    sqlite3_free( zPath );
    db->nDb++;
  }
  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( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
  if( rc==SQLITE_OK && pNew->zDbSName==0 ){
    rc = SQLITE_NOMEM_BKPT;
  }


#ifdef SQLITE_HAS_CODEC
  if( rc==SQLITE_OK ){
    extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
    int nKey;
    char *zKey;
    int t = sqlite3_value_type(argv[2]);
    switch( t ){
      case SQLITE_INTEGER:
      case SQLITE_FLOAT:
        zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
        rc = SQLITE_ERROR;
        break;
        
      case SQLITE_TEXT:
      case SQLITE_BLOB:
        nKey = sqlite3_value_bytes(argv[2]);
        zKey = (char *)sqlite3_value_blob(argv[2]);
        rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
        break;

      case SQLITE_NULL:
        /* No key specified.  Use the key from the main database */
        sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
        if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
          rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
        }
        break;
    }
  }
#endif

  /* 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);
    rc = sqlite3Init(db, &zErrDyn);
    sqlite3BtreeLeaveAll(db);
    assert( zErrDyn==0 || rc!=SQLITE_OK );
  }
#ifdef SQLITE_USER_AUTHENTICATION
  if( rc==SQLITE_OK ){
    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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x40eb80
movq %rax, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x40d230
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x40d230
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x49ede5
leaq 0x10112c(%rip), %rax    # 0x59ff0d
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x49edf7
leaq 0x10111a(%rip), %rax    # 0x59ff0d
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
movq -0x28(%rbp), %rcx
movl 0x98(%rcx), %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jl 0x49ee34
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x98(%rax), %edx
leaq 0xe2b9c(%rip), %rsi     # 0x5819c0
movb $0x0, %al
callq 0x411210
movq %rax, -0x68(%rbp)
jmp 0x49f2c1
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x49ee9f
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x409750
cmpl $0x0, %eax
jne 0x49ee92
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0xe2b63(%rip), %rsi     # 0x5819e5
movb $0x0, %al
callq 0x411210
movq %rax, -0x68(%rbp)
jmp 0x49f2c1
jmp 0x49ee94
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x49ee3b
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
addq $0x238, %rcx            # imm = 0x238
cmpq %rcx, %rax
jne 0x49eeed
movq -0x28(%rbp), %rdi
movl $0x60, %esi
callq 0x41b3d0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x49eed5
jmp 0x49f2f9
movq -0x58(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rsi
movl $0x40, %edx
callq 0x401040
jmp 0x49ef21
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
addl $0x1, %eax
movslq %eax, %rdx
shlq $0x5, %rdx
callq 0x41b350
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x49ef1f
jmp 0x49f2f9
jmp 0x49ef21
movq -0x58(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movslq 0x28(%rcx), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x4010b0
movq -0x28(%rbp), %rax
movl 0x4c(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
leaq -0x70(%rbp), %rcx
leaq -0x40(%rbp), %r8
leaq -0x48(%rbp), %r9
callq 0x49f510
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x49efbe
cmpl $0x7, -0x20(%rbp)
jne 0x49ef9e
movq -0x28(%rbp), %rdi
callq 0x40e530
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x40daa0
movq -0x48(%rbp), %rdi
callq 0x406240
jmp 0x49f2f9
movl -0x4c(%rbp), %eax
orl $0x100, %eax            # imm = 0x100
movl %eax, -0x4c(%rbp)
movq -0x70(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x60(%rbp), %rcx
addq $0x8, %rcx
movl -0x4c(%rbp), %r9d
xorl %r8d, %r8d
callq 0x41f610
movl %eax, -0x20(%rbp)
movq -0x40(%rbp), %rdi
callq 0x406240
movq -0x28(%rbp), %rax
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
movq -0x28(%rbp), %rax
movb $0x0, 0x69(%rax)
cmpl $0x13, -0x20(%rbp)
jne 0x49f032
movl $0x1, -0x20(%rbp)
movq -0x28(%rbp), %rdi
leaq 0xe29e1(%rip), %rsi     # 0x581a03
movb $0x0, %al
callq 0x411210
movq %rax, -0x68(%rbp)
jmp 0x49f13f
cmpl $0x0, -0x20(%rbp)
jne 0x49f13d
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x49fdf0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x60(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x49f06c
movl $0x7, -0x20(%rbp)
jmp 0x49f0b4
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x70(%rax), %eax
cmpl $0x0, %eax
je 0x49f0b2
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rax
movzbl 0x71(%rax), %eax
movq -0x28(%rbp), %rcx
movzbl 0x5e(%rcx), %ecx
cmpl %ecx, %eax
je 0x49f0b2
movq -0x28(%rbp), %rdi
leaq 0xdffbd(%rip), %rsi     # 0x57f05d
movb $0x0, %al
callq 0x411210
movq %rax, -0x68(%rbp)
movl $0x1, -0x20(%rbp)
jmp 0x49f0b4
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x40b350
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x405570
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movq -0x28(%rbp), %rax
movzbl 0x63(%rax), %esi
callq 0x49dfb0
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x49deb0
movq -0x90(%rbp), %rdi
movl %eax, %esi
callq 0x49deb0
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x28(%rbp), %rax
movl 0x30(%rax), %esi
andl $0x38, %esi
orl $0x3, %esi
callq 0x451b50
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x40c4a0
jmp 0x49f13f
movq -0x60(%rbp), %rax
movb $0x3, 0x10(%rax)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x40eaf0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, (%rax)
cmpl $0x0, -0x20(%rbp)
jne 0x49f175
movq -0x60(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x49f175
movl $0x7, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x49f1b5
movq -0x28(%rbp), %rdi
callq 0x405540
movq -0x28(%rbp), %rax
movb $0x0, 0xb4(%rax)
movq -0x28(%rbp), %rax
movl 0x2c(%rax), %ecx
andl $-0x9, %ecx
movl %ecx, 0x2c(%rax)
movq -0x28(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x419310
movl %eax, -0x20(%rbp)
movq -0x28(%rbp), %rdi
callq 0x405630
cmpl $0x0, -0x20(%rbp)
je 0x49f2bf
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
subl $0x1, %eax
movl %eax, -0x84(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x84(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x49f247
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x84(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
callq 0x431e90
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x84(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x84(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq $0x0, 0x18(%rax)
movq -0x28(%rbp), %rdi
callq 0x40bcf0
movl -0x84(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x28(%rax)
cmpl $0x7, -0x20(%rbp)
je 0x49f26c
cmpl $0xc0a, -0x20(%rbp)     # imm = 0xC0A
jne 0x49f29a
movq -0x28(%rbp), %rdi
callq 0x40e530
movq -0x28(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x4058a0
movq -0x28(%rbp), %rdi
leaq 0xdfd67(%rip), %rsi     # 0x57eff4
movb $0x0, %al
callq 0x411210
movq %rax, -0x68(%rbp)
jmp 0x49f2bd
cmpq $0x0, -0x68(%rbp)
jne 0x49f2bb
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xe2770(%rip), %rsi     # 0x581a20
movb $0x0, %al
callq 0x411210
movq %rax, -0x68(%rbp)
jmp 0x49f2bd
jmp 0x49f2c1
jmp 0x49f2f9
cmpq $0x0, -0x68(%rbp)
je 0x49f2e7
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x40daa0
movq -0x28(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x4058a0
cmpl $0x0, -0x20(%rbp)
je 0x49f2f9
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x40e390
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)