// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: walIteratorInit
static int walIteratorInit(Wal *pWal, WalIterator **pp){
  WalIterator *p;                 /* Return value */
  int nSegment;                   /* Number of segments to merge */
  u32 iLast;                      /* Last frame in log */
  int 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 *)sqlite3ScratchMalloc(nByte);
  if( !p ){
    return SQLITE_NOMEM;
  }
  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 *)sqlite3ScratchMalloc(
      sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
  );
  if( !aTmp ){
    rc = SQLITE_NOMEM;
  }

  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
    volatile ht_slot *aHash;
    u32 iZero;
    volatile u32 *aPgno;

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

      aPgno++;
      if( (i+1)==nSegment ){
        nEntry = (int)(iLast - iZero);
      }else{
        nEntry = (int)((u32*)aHash - (u32*)aPgno);
      }
      aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
      iZero++;
  
      for(j=0; j<nEntry; j++){
        aIndex[j] = (ht_slot)j;
      }
      walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
      p->aSegment[i].iZero = iZero;
      p->aSegment[i].nEntry = nEntry;
      p->aSegment[i].aIndex = aIndex;
      p->aSegment[i].aPgno = (u32 *)aPgno;
    }
  }
  sqlite3ScratchFree(aTmp);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %edi
callq 0x242c0
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x5, %rax
addq $0x28, %rax
movl -0x28(%rbp), %ecx
shlq %rcx
addq %rcx, %rax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x1b160
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x25fda
movl $0x7, -0x4(%rbp)
jmp 0x261bf
movq -0x20(%rbp), %rdi
movslq -0x2c(%rbp), %rdx
xorl %esi, %esi
callq 0x8200
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpl $0x1000, -0x28(%rbp)    # imm = 0x1000
jbe 0x26006
movl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x6c(%rbp)
jmp 0x2600c
movl -0x28(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, %eax
shlq %rax
movl %eax, %edi
callq 0x1b160
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x2602d
movl $0x7, -0x3c(%rbp)
movl $0x0, -0x30(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x3c(%rbp)
movb %al, -0x6d(%rbp)
jne 0x2604b
movl -0x30(%rbp), %eax
cmpl -0x24(%rbp), %eax
setl %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
testb $0x1, %al
jne 0x26057
jmp 0x26196
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
leaq -0x48(%rbp), %rdx
leaq -0x58(%rbp), %rcx
leaq -0x4c(%rbp), %r8
callq 0x24220
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x26186
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
cmpl -0x24(%rbp), %eax
jne 0x2609e
movl -0x28(%rbp), %eax
subl -0x4c(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x260b0
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
movl %eax, -0x60(%rbp)
movq -0x20(%rbp), %rax
addq $0x8, %rax
movq -0x20(%rbp), %rcx
movslq 0x4(%rcx), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl -0x4c(%rbp), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x26109
movl -0x5c(%rbp), %eax
movw %ax, %dx
movq -0x68(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x260e4
movq -0x58(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x262f0
movl -0x4c(%rbp), %ecx
movq -0x20(%rbp), %rax
addq $0x8, %rax
movslq -0x30(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
movl -0x60(%rbp), %ecx
movq -0x20(%rbp), %rax
addq $0x8, %rax
movslq -0x30(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movq -0x68(%rbp), %rcx
movq -0x20(%rbp), %rax
addq $0x8, %rax
movslq -0x30(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rcx
movq -0x20(%rbp), %rax
addq $0x8, %rax
movslq -0x30(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
jmp 0x26188
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x26034
movq -0x38(%rbp), %rdi
callq 0x1b220
cmpl $0x0, -0x3c(%rbp)
je 0x261ae
movq -0x20(%rbp), %rdi
callq 0x262d0
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)