// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: lockBtree
static int lockBtree(BtShared *pBt){
  int rc;              /* Result code from subfunctions */
  MemPage *pPage1;     /* Page 1 of the database file */
  u32 nPage;           /* Number of pages in the database */
  u32 nPageFile = 0;   /* Number of pages in the database file */
  u32 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, (int*)&nPageFile);
  if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
    nPage = nPageFile;
  }
  if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
    nPage = 0;
  }
  if( nPage>0 ){
    u32 pageSize;
    u32 usableSize;
    u8 *page1 = pPage1->aData;
    rc = SQLITE_NOTADB;
    /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
    ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
    ** 61 74 20 33 00. */
    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{
        setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
        if( isOpen==0 ){
          releasePageOne(pPage1);
          return SQLITE_OK;
        }
      }
      rc = SQLITE_NOTADB;
    }else{
      setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
    }
#endif

    /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
    ** fractions and the leaf payload fraction values must be 64, 32, and 32.
    **
    ** 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;
    }
    /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
    ** determined by the 2-byte integer located at an offset of 16 bytes from
    ** the beginning of the database file. */
    pageSize = (page1[16]<<8) | (page1[17]<<16);
    /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
    ** between 512 and 65536 inclusive. */
    if( ((pageSize-1)&pageSize)!=0
     || pageSize>SQLITE_MAX_PAGE_SIZE
     || pageSize<=256
    ){
      goto page1_init_failed;
    }
    pBt->btsFlags |= BTS_PAGESIZE_FIXED;
    assert( (pageSize & 7)==0 );
    /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
    ** integer at offset 20 is the number of bytes of space at the end of
    ** each page to reserve for extensions.
    **
    ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
    ** determined by the one-byte unsigned integer found at an offset of 20
    ** into the database file header. */
    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.
      */
      releasePageOne(pPage1);
      pBt->usableSize = usableSize;
      pBt->pageSize = pageSize;
      freeTempSpace(pBt);
      rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
                                   pageSize-usableSize);
      return rc;
    }
    if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
      rc = SQLITE_CORRUPT_BKPT;
      goto page1_init_failed;
    }
    /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
    ** be less than 480. In other words, if the page size is 512, then the
    ** reserved space size cannot exceed 32. */
    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:
  releasePageOne(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 0x3e6c20
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3e64c3
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3e689d
movq -0x10(%rbp), %rdi
movl $0x1, %esi
leaq -0x20(%rbp), %rdx
xorl %ecx, %ecx
callq 0x3e6f40
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3e64eb
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3e689d
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x1c, %rdi
callq 0x3db720
movl %eax, -0x2c(%rbp)
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x3c65f0
cmpl $0x0, -0x24(%rbp)
je 0x3e6541
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 0x3e6547
movl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rax), %rax
andq $0x2000000, %rax        # imm = 0x2000000
cmpq $0x0, %rax
je 0x3e6566
movl $0x0, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jbe 0x3e67ba
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x1a, -0x14(%rbp)
movq -0x40(%rbp), %rax
movups (%rax), %xmm0
movaps 0x1c477f(%rip), %xmm1   # 0x5aad10
pcmpeqb %xmm1, %xmm0
pmovmskb %xmm0, %eax
subl $0xffff, %eax           # imm = 0xFFFF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x3e65ae
jmp 0x3e6882
movq -0x40(%rbp), %rax
movzbl 0x12(%rax), %eax
cmpl $0x2, %eax
jle 0x3e65ca
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %ecx
orl $0x1, %ecx
movw %cx, 0x28(%rax)
movq -0x40(%rbp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x2, %eax
jle 0x3e65dc
jmp 0x3e6882
movq -0x40(%rbp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x2, %eax
jne 0x3e6644
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0x3e6644
movl $0x0, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x44(%rbp), %rsi
callq 0x3e6fb0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3e661e
jmp 0x3e6882
cmpl $0x0, -0x44(%rbp)
jne 0x3e6639
movq -0x20(%rbp), %rdi
callq 0x3e7040
movl $0x0, -0x4(%rbp)
jmp 0x3e689d
jmp 0x3e663b
movl $0x1a, -0x14(%rbp)
jmp 0x3e6646
jmp 0x3e6646
movq -0x40(%rbp), %rdi
addq $0x15, %rdi
leaq 0x1c4b7c(%rip), %rsi    # 0x5ab1d1
movl $0x3, %edx
callq 0x192c0
cmpl $0x0, %eax
je 0x3e6669
jmp 0x3e6882
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 0x3e66a4
cmpl $0x10000, -0x30(%rbp)   # imm = 0x10000
ja 0x3e66a4
cmpl $0x100, -0x30(%rbp)     # imm = 0x100
ja 0x3e66a9
jmp 0x3e6882
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %ecx
orl $0x2, %ecx
movw %cx, 0x28(%rax)
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 0x34(%rcx), %eax
je 0x3e6722
movq -0x20(%rbp), %rdi
callq 0x3e7040
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x10(%rbp), %rdi
callq 0x3e7060
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x34, %rsi
movl -0x30(%rbp), %edx
subl -0x34(%rbp), %edx
callq 0x3db760
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3e689d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3e70c0
cmpl $0x0, %eax
jne 0x3e674e
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jbe 0x3e674e
movl $0x10855, %edi          # imm = 0x10855
callq 0x3dff80
movl %eax, -0x14(%rbp)
jmp 0x3e6882
cmpl $0x1e0, -0x34(%rbp)     # imm = 0x1E0
jae 0x3e675c
jmp 0x3e6882
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x40(%rbp), %rdi
addq $0x34, %rdi
callq 0x3db720
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 0x3db720
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 0x38(%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, 0x2a(%rax)
movq -0x10(%rbp), %rax
movl 0x38(%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, 0x2c(%rax)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %eax
subl $0x23, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x2e(%rax)
movq -0x10(%rbp), %rax
movl 0x38(%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, 0x30(%rax)
movq -0x10(%rbp), %rax
movzwl 0x2a(%rax), %eax
cmpl $0x7f, %eax
jle 0x3e6852
movq -0x10(%rbp), %rax
movb $0x7f, 0x25(%rax)
jmp 0x3e6863
movq -0x10(%rbp), %rax
movw 0x2a(%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, 0x40(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x3e689d
movq -0x20(%rbp), %rdi
callq 0x3e7040
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 %cs:(%rax,%rax)