// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: lockBtree
static int lockBtree(BtShared *pBt){
  int rc;              /* Result code from subfunctions */
  MemPage *pPage1;     /* Page 1 of the database file */
  int nPage;           /* Number of pages in the database */
  int nPageFile = 0;   /* Number of pages in the database file */
  int nPageHeader;     /* Number of pages in the database according to hdr */

  assert( sqlite3_mutex_held(pBt->mutex) );
  assert( pBt->pPage1==0 );
  rc = sqlite3PagerSharedLock(pBt->pPager);
  if( rc!=SQLITE_OK ) return rc;
  rc = btreeGetPage(pBt, 1, &pPage1, 0);
  if( rc!=SQLITE_OK ) return rc;

  /* Do some checking to help insure the file we opened really is
  ** a valid database file. 
  */
  nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
  sqlite3PagerPagecount(pBt->pPager, &nPageFile);
  if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
    nPage = nPageFile;
  }
  if( nPage>0 ){
    u32 pageSize;
    u32 usableSize;
    u8 *page1 = pPage1->aData;
    rc = SQLITE_NOTADB;
    if( memcmp(page1, zMagicHeader, 16)!=0 ){
      goto page1_init_failed;
    }

#ifdef SQLITE_OMIT_WAL
    if( page1[18]>1 ){
      pBt->btsFlags |= BTS_READ_ONLY;
    }
    if( page1[19]>1 ){
      goto page1_init_failed;
    }
#else
    if( page1[18]>2 ){
      pBt->btsFlags |= BTS_READ_ONLY;
    }
    if( page1[19]>2 ){
      goto page1_init_failed;
    }

    /* If the write version is set to 2, this database should be accessed
    ** in WAL mode. If the log is not already open, open it now. Then 
    ** return SQLITE_OK and return without populating BtShared.pPage1.
    ** The caller detects this and calls this function again. This is
    ** required as the version of page 1 currently in the page1 buffer
    ** may not be the latest version - there may be a newer one in the log
    ** file.
    */
    if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
      int isOpen = 0;
      rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
      if( rc!=SQLITE_OK ){
        goto page1_init_failed;
      }else if( isOpen==0 ){
        releasePage(pPage1);
        return SQLITE_OK;
      }
      rc = SQLITE_NOTADB;
    }
#endif

    /* The maximum embedded fraction must be exactly 25%.  And the minimum
    ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
    ** The original design allowed these amounts to vary, but as of
    ** version 3.6.0, we require them to be fixed.
    */
    if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
      goto page1_init_failed;
    }
    pageSize = (page1[16]<<8) | (page1[17]<<16);
    if( ((pageSize-1)&pageSize)!=0
     || pageSize>SQLITE_MAX_PAGE_SIZE 
     || pageSize<=256 
    ){
      goto page1_init_failed;
    }
    assert( (pageSize & 7)==0 );
    usableSize = pageSize - page1[20];
    if( (u32)pageSize!=pBt->pageSize ){
      /* After reading the first page of the database assuming a page size
      ** of BtShared.pageSize, we have discovered that the page-size is
      ** actually pageSize. Unlock the database, leave pBt->pPage1 at
      ** zero and return SQLITE_OK. The caller will call this function
      ** again with the correct page-size.
      */
      releasePage(pPage1);
      pBt->usableSize = usableSize;
      pBt->pageSize = pageSize;
      freeTempSpace(pBt);
      rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
                                   pageSize-usableSize);
      return rc;
    }
    if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
      rc = SQLITE_CORRUPT_BKPT;
      goto page1_init_failed;
    }
    if( usableSize<480 ){
      goto page1_init_failed;
    }
    pBt->pageSize = pageSize;
    pBt->usableSize = usableSize;
#ifndef SQLITE_OMIT_AUTOVACUUM
    pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
    pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
#endif
  }

  /* maxLocal is the maximum amount of payload to store locally for
  ** a cell.  Make sure it is small enough so that at least minFanout
  ** cells can will fit on one page.  We assume a 10-byte page header.
  ** Besides the payload, the cell must store:
  **     2-byte pointer to the cell
  **     4-byte child pointer
  **     9-byte nKey value
  **     4-byte nData value
  **     4-byte overflow page pointer
  ** So a cell consists of a 2-byte pointer, a header which is as much as
  ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
  ** page pointer.
  */
  pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
  pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
  pBt->maxLeaf = (u16)(pBt->usableSize - 35);
  pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
  if( pBt->maxLocal>127 ){
    pBt->max1bytePayload = 127;
  }else{
    pBt->max1bytePayload = (u8)pBt->maxLocal;
  }
  assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
  pBt->pPage1 = pPage1;
  pBt->nPage = nPage;
  return SQLITE_OK;

page1_init_failed:
  releasePage(pPage1);
  pBt->pPage1 = 0;
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x29b50
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x29293
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2963e
movq -0x10(%rbp), %rdi
movl $0x1, %esi
leaq -0x20(%rbp), %rdx
xorl %ecx, %ecx
callq 0x29ed0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x292bb
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2963e
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x1c, %rdi
callq 0x20a50
movl %eax, -0x2c(%rbp)
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0xef60
cmpl $0x0, -0x24(%rbp)
je 0x29311
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
addq $0x18, %rax
movq -0x20(%rbp), %rcx
movq 0x50(%rcx), %rcx
addq $0x5c, %rcx
movl (%rcx), %ecx
movl (%rax), %eax
subl %ecx, %eax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x29317
movl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x2955b
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x1a, -0x14(%rbp)
movq -0x40(%rbp), %rax
movups (%rax), %xmm0
movaps 0x84bbe(%rip), %xmm1    # 0xadf00
pcmpeqb %xmm1, %xmm0
pmovmskb %xmm0, %eax
subl $0xffff, %eax           # imm = 0xFFFF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x2935f
jmp 0x29623
movq -0x40(%rbp), %rax
movzbl 0x12(%rax), %eax
cmpl $0x2, %eax
jle 0x2937b
movq -0x10(%rbp), %rax
movzwl 0x26(%rax), %ecx
orl $0x1, %ecx
movw %cx, 0x26(%rax)
movq -0x40(%rbp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x2, %eax
jle 0x2938d
jmp 0x29623
movq -0x40(%rbp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x2, %eax
jne 0x293f3
movq -0x10(%rbp), %rax
movzwl 0x26(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x293f3
movl $0x0, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x44(%rbp), %rsi
callq 0x29f40
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x293cf
jmp 0x29623
cmpl $0x0, -0x44(%rbp)
jne 0x293ea
movq -0x20(%rbp), %rdi
callq 0x29fd0
movl $0x0, -0x4(%rbp)
jmp 0x2963e
jmp 0x293ec
movl $0x1a, -0x14(%rbp)
movq -0x40(%rbp), %rdi
addq $0x15, %rdi
leaq 0x84f4e(%rip), %rsi     # 0xae350
movl $0x3, %edx
callq 0x81c0
cmpl $0x0, %eax
je 0x29416
jmp 0x29623
movq -0x40(%rbp), %rax
movzbl 0x10(%rax), %eax
shll $0x8, %eax
movq -0x40(%rbp), %rcx
movzbl 0x11(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
subl $0x1, %eax
andl -0x30(%rbp), %eax
cmpl $0x0, %eax
jne 0x29451
cmpl $0x10000, -0x30(%rbp)   # imm = 0x10000
ja 0x29451
cmpl $0x100, -0x30(%rbp)     # imm = 0x100
ja 0x29456
jmp 0x29623
movl -0x30(%rbp), %eax
movq -0x40(%rbp), %rcx
movzbl 0x14(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x30(%rcx), %eax
je 0x294c0
movq -0x20(%rbp), %rdi
callq 0x29fd0
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x10(%rbp), %rdi
callq 0x2a000
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x30, %rsi
movl -0x30(%rbp), %edx
subl -0x34(%rbp), %edx
callq 0x20a90
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2963e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x2c(%rax), %eax
andl $0x10000, %eax          # imm = 0x10000
cmpl $0x0, %eax
jne 0x294ef
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x294ef
movl $0xcb54, %edi           # imm = 0xCB54
callq 0x24410
movl %eax, -0x14(%rbp)
jmp 0x29623
cmpl $0x1e0, -0x34(%rbp)     # imm = 0x1E0
jae 0x294fd
jmp 0x29623
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x40(%rbp), %rdi
addq $0x34, %rdi
callq 0x20a50
movl %eax, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x21(%rax)
movq -0x40(%rbp), %rdi
addq $0x40, %rdi
callq 0x20a50
movl %eax, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x22(%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
subl $0xc, %eax
shll $0x6, %eax
movl $0xff, %ecx
xorl %edx, %edx
divl %ecx
subl $0x17, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x28(%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
subl $0xc, %eax
shll $0x5, %eax
movl $0xff, %ecx
xorl %edx, %edx
divl %ecx
subl $0x17, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2a(%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
subl $0x23, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2c(%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
subl $0xc, %eax
shll $0x5, %eax
movl $0xff, %ecx
xorl %edx, %edx
divl %ecx
subl $0x17, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2e(%rax)
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
cmpl $0x7f, %eax
jle 0x295f3
movq -0x10(%rbp), %rax
movb $0x7f, 0x25(%rax)
jmp 0x29604
movq -0x10(%rbp), %rax
movw 0x28(%rax), %ax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x25(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x3c(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x2963e
movq -0x20(%rbp), %rdi
callq 0x29fd0
movq -0x10(%rbp), %rax
movq $0x0, 0x18(%rax)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)