// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: walIteratorInit
static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
  WalIterator *p;                 /* Return value */
  int nSegment;                   /* Number of segments to merge */
  u32 iLast;                      /* Last frame in log */
  sqlite3_int64 nByte;            /* Number of bytes to allocate */
  int i;                          /* Iterator variable */
  ht_slot *aTmp;                  /* Temp space used by merge-sort */
  int rc = SQLITE_OK;             /* Return Code */

  /* This routine only runs while holding the checkpoint lock. And
  ** it only runs if there is actually content in the log (mxFrame>0).
  */
  assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
  iLast = pWal->hdr.mxFrame;

  /* Allocate space for the WalIterator object. */
  nSegment = walFramePage(iLast) + 1;
  nByte = sizeof(WalIterator)
        + (nSegment-1)*sizeof(struct WalSegment)
        + iLast*sizeof(ht_slot);
  p = (WalIterator *)sqlite3_malloc64(nByte);
  if( !p ){
    return SQLITE_NOMEM_BKPT;
  }
  memset(p, 0, nByte);
  p->nSegment = nSegment;

  /* Allocate temporary space used by the merge-sort routine. This block
  ** of memory will be freed before this function returns.
  */
  aTmp = (ht_slot *)sqlite3_malloc64(
      sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
  );
  if( !aTmp ){
    rc = SQLITE_NOMEM_BKPT;
  }

  for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
    WalHashLoc sLoc;

    rc = walHashGet(pWal, i, &sLoc);
    if( rc==SQLITE_OK ){
      int j;                      /* Counter variable */
      int nEntry;                 /* Number of entries in this segment */
      ht_slot *aIndex;            /* Sorted index for this segment */

      sLoc.aPgno++;
      if( (i+1)==nSegment ){
        nEntry = (int)(iLast - sLoc.iZero);
      }else{
        nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
      }
      aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
      sLoc.iZero++;

      for(j=0; j<nEntry; j++){
        aIndex[j] = (ht_slot)j;
      }
      walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
      p->aSegment[i].iZero = sLoc.iZero;
      p->aSegment[i].nEntry = nEntry;
      p->aSegment[i].aIndex = aIndex;
      p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
    }
  }
  sqlite3_free(aTmp);

  if( rc!=SQLITE_OK ){
    walIteratorFree(p);
    p = 0;
  }
  *pp = p;
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %edi
callq 0x3dfc40
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x5, %rax
addq $0x28, %rax
movl -0x30(%rbp), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x3c07c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x3e5c92
movl $0x7, -0x4(%rbp)
jmp 0x3e5e7e
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rdx
xorl %esi, %esi
callq 0x19360
movl -0x2c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpl $0x1000, -0x30(%rbp)    # imm = 0x1000
jbe 0x3e5cbe
movl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x7c(%rbp)
jmp 0x3e5cc4
movl -0x30(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
shlq %rdi
callq 0x3c07c0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x3e5ce5
movl $0x7, -0x4c(%rbp)
movl -0x14(%rbp), %edi
addl $0x1, %edi
callq 0x3dfc40
movl %eax, -0x3c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x4c(%rbp)
movb %al, -0x7d(%rbp)
jne 0x3e5d0a
movl -0x3c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
setl %al
movb %al, -0x7d(%rbp)
movb -0x7d(%rbp), %al
testb $0x1, %al
jne 0x3e5d16
jmp 0x3e5e4d
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
leaq -0x68(%rbp), %rdx
callq 0x3dfeb0
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x3e5e3d
movq -0x60(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x60(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cmpl -0x2c(%rbp), %eax
jne 0x3e5d55
movl -0x30(%rbp), %eax
subl -0x58(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0x3e5d67
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
movl %eax, -0x70(%rbp)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq -0x28(%rbp), %rcx
movslq 0x4(%rcx), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl -0x58(%rbp), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x70(%rbp), %eax
jge 0x3e5dc0
movl -0x6c(%rbp), %eax
movw %ax, %dx
movq -0x78(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x3e5d9b
movq -0x60(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x78(%rbp), %rdx
leaq -0x70(%rbp), %rcx
callq 0x3e5fb0
movl -0x58(%rbp), %ecx
movq -0x28(%rbp), %rax
addq $0x8, %rax
movslq -0x3c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
movl -0x70(%rbp), %ecx
movq -0x28(%rbp), %rax
addq $0x8, %rax
movslq -0x3c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movq -0x78(%rbp), %rcx
movq -0x28(%rbp), %rax
addq $0x8, %rax
movslq -0x3c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rcx
movq -0x28(%rbp), %rax
addq $0x8, %rax
movslq -0x3c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
jmp 0x3e5e3f
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x3e5cf3
movq -0x48(%rbp), %rdi
callq 0x3c0800
cmpl $0x0, -0x4c(%rbp)
je 0x3e5e6d
movq -0x28(%rbp), %rdi
callq 0x3e5f90
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)