// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movl %edx, %r12d
movl %esi, %r13d
leal 0x1000(%r13), %eax
movabsq $0xffffffff021, %r15    # imm = 0xFFFFFFFF021
addq %rax, %r15
shrq $0xc, %r15
leaq -0x38(%rbp), %rdx
movq %rdi, -0x30(%rbp)
movl %r15d, %esi
callq 0x1c0bd
movl %eax, %ebx
testl %eax, %eax
je 0x1baaf
xorl %r15d, %r15d
xorl %edx, %edx
xorl %r14d, %r14d
jmp 0x1badf
movq -0x38(%rbp), %r14
testl %r15d, %r15d
je 0x1bac2
shll $0xc, %r15d
addl $-0x22, %r15d
jmp 0x1bad0
leaq 0x88(%r14), %rax
movq %rax, -0x38(%rbp)
xorl %r15d, %r15d
addq $0x4000, %r14           # imm = 0x4000
movq -0x38(%rbp), %rdx
addq $-0x4, %rdx
testl %ebx, %ebx
jne 0x1bba7
movq %r13, -0x48(%rbp)
subl %r15d, %r13d
cmpl $0x1, %r13d
movq %rdx, -0x40(%rbp)
jne 0x1bb14
leaq 0x4(%rdx), %rdi
movl %r14d, %eax
subl %edi, %eax
addl $0x4000, %eax           # imm = 0x4000
movslq %eax, %rdx
xorl %esi, %esi
callq 0x8200
movq -0x40(%rbp), %rdx
movslq %r13d, %rsi
cmpl $0x0, (%rdx,%rsi,4)
je 0x1bb38
movq -0x30(%rbp), %rdi
movl %r12d, -0x30(%rbp)
movq %rsi, %r12
callq 0x1c86e
movq %r12, %rsi
movq -0x40(%rbp), %rdx
movl -0x30(%rbp), %r12d
movl %r12d, %ecx
imull $0x17f, %r12d, %eax     # imm = 0x17F
andl $0x1fff, %eax           # imm = 0x1FFF
cmpw $0x0, (%r14,%rax,2)
sete %r12b
je 0x1bb95
testl %r13d, %r13d
je 0x1bb74
subl -0x48(%rbp), %r15d
incl %eax
andl $0x1fff, %eax           # imm = 0x1FFF
cmpw $0x0, (%r14,%rax,2)
sete %r12b
je 0x1bb95
incl %r15d
jne 0x1bb5c
leaq 0x5dc99(%rip), %rsi     # 0x79814
leaq 0x5d9b7(%rip), %rcx     # 0x79539
movl $0xb, %edi
movl $0xb5c8, %edx           # imm = 0xB5C8
xorl %eax, %eax
callq 0xb8cc
jmp 0x1bb9d
movl %ecx, (%rdx,%rsi,4)
movw %r13w, (%r14,%rax,2)
movl $0xb, %eax
testb %r12b, %r12b
je 0x1bba9
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq