// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: walIndexAppend
static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
  int rc;                         /* Return code */
  WalHashLoc sLoc;                /* Wal-index hash table location */

  rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);

  /* 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 - sLoc.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 proceeding.
    */
    if( idx==1 ){
      int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT]
                               - (u8 *)&sLoc.aPgno[1]);
      memset((void*)&sLoc.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( sLoc.aPgno[idx] ){
      walCleanupHash(pWal);
      assert( !sLoc.aPgno[idx] );
    }

    /* Write the aPgno[] array entry and the hash-table slot. */
    nCollide = idx;
    for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
      if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
    }
    sLoc.aPgno[idx] = iPage;
    AtomicStore(&sLoc.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( sLoc.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(sLoc.aPgno[i]);
            sLoc.aHash[iKey];
            iKey=walNextHash(iKey)){
          if( sLoc.aHash[iKey]==i ) break;
        }
        assert( sLoc.aHash[iKey]==i );
      }
    }
#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
  }


  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %r15
leal 0x1000(%r14), %eax
movabsq $0xffffffff021, %rsi    # imm = 0xFFFFFFFF021
addq %rax, %rsi
shrq $0xc, %rsi
leaq 0x10(%rsp), %rdx
callq 0x292625
movl %eax, %ebx
testl %eax, %eax
jne 0x291267
movl 0x20(%rsp), %r13d
movl %r14d, %r12d
subl %r13d, %r12d
cmpl $0x1, %r12d
jne 0x2911c9
movl 0x10(%rsp), %eax
movq 0x18(%rsp), %rdi
addq $0x4, %rdi
subl %edi, %eax
addl $0x4000, %eax           # imm = 0x4000
movslq %eax, %rdx
xorl %esi, %esi
callq 0x173a0
movq 0x18(%rsp), %rdx
movslq %r12d, %rsi
cmpl $0x0, (%rdx,%rsi,4)
je 0x2911f3
movq %r15, %rdi
movq %rdx, %r15
movl %ebp, 0xc(%rsp)
movq %rsi, %rbp
callq 0x2927e9
movq %rbp, %rsi
movl 0xc(%rsp), %ebp
movq %r15, %rdx
imull $0x17f, %ebp, %eax      # imm = 0x17F
movq 0x10(%rsp), %rcx
andl $0x1fff, %eax           # imm = 0x1FFF
cmpw $0x0, (%rcx,%rax,2)
sete %r15b
je 0x291255
testl %r12d, %r12d
je 0x29122d
subl %r14d, %r13d
incl %eax
andl $0x1fff, %eax           # imm = 0x1FFF
cmpw $0x0, (%rcx,%rax,2)
sete %r15b
je 0x291255
incl %r13d
jne 0x291216
leaq 0x14e0eb(%rip), %rsi    # 0x3df31f
leaq 0x14e1f5(%rip), %rdx    # 0x3df430
leaq 0x14df43(%rip), %r8     # 0x3df185
movl $0xb, %edi
movl $0xec56, %ecx           # imm = 0xEC56
xorl %eax, %eax
callq 0x27e637
jmp 0x29125d
movl %ebp, (%rdx,%rsi,4)
movw %r12w, (%rcx,%rax,2)
movl $0xb, %eax
testb %r15b, %r15b
je 0x291269
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq