// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x14(%rbp), %edi
callq 0x242c0
movq -0x48(%rbp), %rdi
movl %eax, %esi
leaq -0x30(%rbp), %rdx
leaq -0x28(%rbp), %rcx
leaq -0x20(%rbp), %r8
callq 0x24220
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x22f51
movl -0x14(%rbp), %eax
subl -0x20(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x1, -0x38(%rbp)
jne 0x22ec7
movq -0x30(%rbp), %rax
addq $0x4000, %rax           # imm = 0x4000
movq -0x28(%rbp), %rcx
addq $0x4, %rcx
subq %rcx, %rax
movl %eax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
addq $0x4, %rdi
movslq -0x40(%rbp), %rdx
xorl %esi, %esi
callq 0x8200
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl $0x0, %eax
je 0x22ee0
movq -0x10(%rbp), %rdi
callq 0x242f0
movl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x18(%rbp), %edi
callq 0x243f0
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movw (%rax,%rcx,2), %ax
cmpw $0x0, %ax
je 0x22f31
movl -0x3c(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x3c(%rbp)
cmpl $0x0, %eax
jne 0x22f22
movl $0xb5c8, %edi           # imm = 0xB5C8
callq 0x24410
movl %eax, -0x4(%rbp)
jmp 0x22f57
jmp 0x22f24
movl -0x34(%rbp), %edi
callq 0x24450
movl %eax, -0x34(%rbp)
jmp 0x22ef1
movl -0x18(%rbp), %edx
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
movw %ax, %dx
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq