// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r12
movq %rdi, %r15
movq (%rdi), %rax
movq 0x28(%rax), %r14
xorl %r13d, %r13d
movq %r13, 0x28(%rsp)
movq %r13, 0x20(%rsp)
movq %r13, (%rsp)
movq (%rdx), %rdi
movl $0x1, %esi
callq 0x40af5d
movq %rax, %rbp
movq 0x8(%r12), %rdi
movl $0x1, %esi
callq 0x40af5d
testq %rbp, %rbp
leaq 0xfde94(%rip), %rcx     # 0x57234d
cmoveq %rcx, %rbp
movq %rax, %r12
testq %rax, %rax
cmoveq %rcx, %r12
movl 0x28(%r14), %ebx
movl 0x98(%r14), %edx
leal 0x2(%rdx), %eax
cmpl %eax, %ebx
jge 0x47454b
movq %rbp, 0x18(%rsp)
movq %r15, 0x8(%rsp)
movq 0x20(%r14), %r13
testl %ebx, %ebx
jle 0x47450f
movq %rbx, %rbp
shlq $0x5, %rbp
xorl %r15d, %r15d
movq (%r13,%r15), %rdi
movq %r12, %rsi
callq 0x4079b2
testl %eax, %eax
je 0x474562
addq $0x20, %r15
cmpq %r15, %rbp
jne 0x4744f5
leaq 0x238(%r14), %rax
cmpq %rax, %r13
je 0x4745db
movslq %ebx, %rdx
shlq $0x5, %rdx
addq $0x20, %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x415f7d
movq %rax, %r13
testq %rax, %rax
movq 0x8(%rsp), %r15
jne 0x47461c
jmp 0x4745cc
leaq 0xdf7f7(%rip), %rsi     # 0x553d49
movq %r14, %rdi
xorl %eax, %eax
callq 0x40e55c
movq %rax, (%rsp)
jmp 0x474582
leaq 0xdf805(%rip), %rsi     # 0x553d6e
xorl %r13d, %r13d
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x40e55c
movq %rax, (%rsp)
movq 0x8(%rsp), %r15
testq %rax, %rax
je 0x4745bc
movl $0x1, 0x24(%r15)
movq (%r15), %rdi
movq %rax, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq $-0x1, %r8
callq 0x40b1de
movq (%rsp), %rsi
testq %rsi, %rsi
je 0x4745bc
movq %r14, %rdi
callq 0x415f0a
testl %r13d, %r13d
je 0x4745cc
movq %r15, %rdi
movl %r13d, %esi
callq 0x40b68b
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x60, %esi
movq %r14, %rdi
callq 0x415fb1
testq %rax, %rax
movq 0x8(%rsp), %r15
je 0x4745cc
movq %rax, %r13
movq 0x20(%r14), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups 0x30(%rax), %xmm3
movups %xmm3, 0x30(%r13)
movups %xmm2, 0x20(%r13)
movups %xmm1, 0x10(%r13)
movups %xmm0, (%r13)
movq %r13, 0x20(%r14)
movslq 0x28(%r14), %rbp
shlq $0x5, %rbp
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r13,%rbp)
movups %xmm0, (%r13,%rbp)
movl 0x4c(%r14), %eax
leaq 0x14(%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 0x18(%rsp), %rsi
callq 0x474b50
testl %eax, %eax
je 0x4746d9
cmpl $0x7, %eax
jne 0x47469a
cmpb $0x0, 0x61(%r14)
jne 0x47469a
cmpb $0x0, 0x62(%r14)
jne 0x47469a
movb $0x1, 0x61(%r14)
cmpl $0x0, 0xc4(%r14)
jle 0x474693
movl $0x1, 0x158(%r14)
incl 0x160(%r14)
movq 0x20(%rsp), %r14
movl $0x1, 0x24(%r15)
movq (%r15), %rdi
movq %r14, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movq $-0x1, %r8
callq 0x40b1de
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4054f6
addq %rbp, %r13
movl $0x100, %r9d            # imm = 0x100
orl 0x14(%rsp), %r9d
movl %r9d, 0x14(%rsp)
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rbp
leaq 0x8(%r13), %rbx
movq %rbp, %rsi
movq %r14, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x41853e
movq %rbp, %rdi
movl %eax, %ebp
callq 0x4054f6
incl 0x28(%r14)
movb $0x0, 0x69(%r14)
testl %ebp, %ebp
je 0x47474a
cmpl $0x13, %ebp
jne 0x47480a
leaq 0xdf65a(%rip), %rsi     # 0x553d8c
movq %r14, %rdi
xorl %eax, %eax
callq 0x40e55c
movq %rax, (%rsp)
movl $0x1, %ebp
jmp 0x47480a
movq 0x8(%r13), %rsi
movq %r14, %rdi
callq 0x4750fc
movq %rax, 0x18(%r13)
testq %rax, %rax
je 0x47478c
xorl %ebp, %ebp
cmpb $0x0, 0x70(%rax)
je 0x474791
movb 0x71(%rax), %al
cmpb 0x5e(%r14), %al
je 0x474791
leaq 0xdcc7c(%rip), %rsi     # 0x5513f3
movq %r14, %rdi
xorl %eax, %eax
callq 0x40e55c
movq %rax, (%rsp)
movl $0x1, %ebp
jmp 0x474791
movl $0x7, %ebp
movq (%rbx), %rdi
callq 0x40959e
movq (%rbx), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rax), %rax
movq (%rax), %rax
cmpb $0x0, 0x10(%rax)
jne 0x4747c7
movb 0x63(%r14), %cl
movq 0x120(%rax), %rdx
testq %rdx, %rdx
je 0x4747c4
cmpb $0x2, 0x3f(%rdx)
je 0x4747c7
movb %cl, 0x8(%rax)
movq 0x20(%r14), %rax
movq 0x8(%rax), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x473b35
movq 0x8(%rsp), %rdi
movl %eax, %esi
callq 0x473b35
movq (%rbx), %rdi
movl 0x30(%r14), %esi
andl $0x38, %esi
orl $0x3, %esi
callq 0x439751
movq (%rbx), %rdi
cmpb $0x0, 0x11(%rdi)
je 0x47480a
decl 0x14(%rdi)
jne 0x47480a
callq 0x4202fb
movb $0x3, 0x10(%r13)
movq %r14, %rdi
movq %r12, %rsi
callq 0x40c587
movq %rax, (%r13)
xorl %ecx, %ecx
testq %rax, %rax
movl $0x7, %r13d
cmovnel %ecx, %r13d
testl %ebp, %ebp
cmovnel %ebp, %r13d
testl %r13d, %r13d
jne 0x47487a
cmpb $0x0, 0x69(%r14)
jne 0x474847
movq %r14, %rdi
callq 0x4182ff
movb $0x0, 0xb4(%r14)
andb $-0x9, 0x2c(%r14)
movq %rsp, %rsi
movq %r14, %rdi
callq 0x414e5d
movl %eax, %r13d
cmpb $0x0, 0x69(%r14)
jne 0x474871
movq %r14, %rdi
callq 0x418357
testl %r13d, %r13d
je 0x4745cc
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 0x4748ab
callq 0x4244b9
movq 0x20(%r14), %rax
xorl %ecx, %ecx
movq %rcx, 0x8(%rax,%rbx)
movq %rcx, 0x18(%rax,%rbx)
movq %r14, %rdi
callq 0x409ed8
movl %r12d, 0x28(%r14)
cmpl $0xc0a, %r13d           # imm = 0xC0A
movq 0x18(%rsp), %rdx
je 0x4748cb
cmpl $0x7, %r13d
jne 0x474921
cmpb $0x0, 0x61(%r14)
jne 0x4748fa
cmpb $0x0, 0x62(%r14)
jne 0x4748fa
movb $0x1, 0x61(%r14)
cmpl $0x0, 0xc4(%r14)
jle 0x4748f3
movl $0x1, 0x158(%r14)
incl 0x160(%r14)
movq (%rsp), %rsi
testq %rsi, %rsi
je 0x47490b
movq %r14, %rdi
callq 0x415f0a
leaq 0xdca78(%rip), %rsi     # 0x55138a
movq %r14, %rdi
xorl %eax, %eax
callq 0x40e55c
jmp 0x47455c
movq (%rsp), %rax
testq %rax, %rax
jne 0x474587
leaq 0xdf474(%rip), %rsi     # 0x553da9
movq %r14, %rdi
xorl %eax, %eax
callq 0x40e55c
jmp 0x47455c