// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: walIndexAppend
static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
  int rc;                         /* Return code */
  u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
  volatile u32 *aPgno = 0;        /* Page number array */
  volatile ht_slot *aHash = 0;    /* Hash table */

  rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);

  /* Assuming the wal-index file was successfully mapped, populate the
  ** page number array and hash table entry.
  */
  if( rc==SQLITE_OK ){
    int iKey;                     /* Hash table key */
    int idx;                      /* Value to write to hash-table slot */
    int nCollide;                 /* Number of hash collisions */

    idx = iFrame - iZero;
    assert( idx <= HASHTABLE_NSLOT/2 + 1 );
    
    /* If this is the first entry to be added to this hash-table, zero the
    ** entire hash table and aPgno[] array before proceding. 
    */
    if( idx==1 ){
      int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
      memset((void*)&aPgno[1], 0, nByte);
    }

    /* If the entry in aPgno[] is already set, then the previous writer
    ** must have exited unexpectedly in the middle of a transaction (after
    ** writing one or more dirty pages to the WAL to free up memory). 
    ** Remove the remnants of that writers uncommitted transaction from 
    ** the hash-table before writing any new entries.
    */
    if( aPgno[idx] ){
      walCleanupHash(pWal);
      assert( !aPgno[idx] );
    }

    /* Write the aPgno[] array entry and the hash-table slot. */
    nCollide = idx;
    for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
      if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
    }
    aPgno[idx] = iPage;
    aHash[iKey] = (ht_slot)idx;

#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
    /* Verify that the number of entries in the hash table exactly equals
    ** the number of entries in the mapping region.
    */
    {
      int i;           /* Loop counter */
      int nEntry = 0;  /* Number of entries in the hash table */
      for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
      assert( nEntry==idx );
    }

    /* Verify that the every entry in the mapping region is reachable
    ** via the hash table.  This turns out to be a really, really expensive
    ** thing to check, so only do this occasionally - not on every
    ** iteration.
    */
    if( (idx&0x3ff)==0 ){
      int i;           /* Loop counter */
      for(i=1; i<=idx; i++){
        for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
          if( aHash[iKey]==i ) break;
        }
        assert( aHash[iKey]==i );
      }
    }
#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
  }


  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebx
movl %esi, %r14d
movq %rdi, %r12
leal 0x1000(%r14), %eax
movabsq $0xffffffff021, %r15    # imm = 0xFFFFFFFF021
addq %rax, %r15
shrq $0xc, %r15
leaq -0x40(%rbp), %rdx
movl %r15d, %esi
callq 0x1bf63
testl %eax, %eax
je 0x1b927
movl %eax, %r13d
jmp 0x1b9ff
movl %ebx, -0x34(%rbp)
movq -0x40(%rbp), %r13
movl %r15d, %ebx
shll $0xc, %ebx
addl $-0x22, %ebx
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x88, %eax
cmovneq %rcx, %rax
cmovel %r15d, %ebx
leaq (%rax,%r13), %rsi
movl %r14d, %r15d
subl %ebx, %r15d
cmpl $0x1, %r15d
movq %rsi, -0x30(%rbp)
jne 0x1b974
movl $0x8000, %edx           # imm = 0x8000
subq %rax, %rdx
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x8200
movq -0x30(%rbp), %rsi
movslq %r15d, %rdx
cmpl $0x0, -0x4(%rsi,%rdx,4)
je 0x1b990
movq %r12, %rdi
movq %rdx, %r12
callq 0x1c6df
movq %r12, %rdx
movq -0x30(%rbp), %rsi
movl -0x34(%rbp), %ecx
imull $0x17f, %ecx, %eax      # imm = 0x17F
andl $0x1fff, %eax           # imm = 0x1FFF
cmpw $0x0, 0x4000(%r13,%rax,2)
je 0x1b9c8
notl %r14d
addl %ebx, %r14d
incl %r14d
je 0x1b9da
incl %eax
andl $0x1fff, %eax           # imm = 0x1FFF
cmpw $0x0, 0x4000(%r13,%rax,2)
jne 0x1b9b0
movl %ecx, -0x4(%rsi,%rdx,4)
movw %r15w, 0x4000(%r13,%rax,2)
xorl %r13d, %r13d
jmp 0x1b9ff
leaq 0x5de33(%rip), %rsi     # 0x79814
leaq 0x5db51(%rip), %rcx     # 0x79539
movl $0xb, %r13d
movl $0xb, %edi
movl $0xb5c8, %edx           # imm = 0xB5C8
xorl %eax, %eax
callq 0xb9c7
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq