// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3VdbeRecordCompare
SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
  int nKey1, const void *pKey1, /* Left key */
  UnpackedRecord *pPKey2        /* Right key */
){
  u32 d1;            /* Offset into aKey[] of next data element */
  u32 idx1;          /* Offset into aKey[] of next header element */
  u32 szHdr1;        /* Number of bytes in header */
  int i = 0;
  int rc = 0;
  const unsigned char *aKey1 = (const unsigned char *)pKey1;
  KeyInfo *pKeyInfo;
  Mem mem1;

  pKeyInfo = pPKey2->pKeyInfo;
  mem1.enc = pKeyInfo->enc;
  mem1.db = pKeyInfo->db;
  /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */

  /* Compilers may complain that mem1.u.i is potentially uninitialized.
  ** We could initialize it, as shown here, to silence those complaints.
  ** But in fact, mem1.u.i will never actually be used uninitialized, and doing 
  ** the unnecessary initialization has a measurable negative performance
  ** impact, since this routine is a very high runner.  And so, we choose
  ** to ignore the compiler warnings and leave this variable uninitialized.
  */
  /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
  
  idx1 = getVarint32(aKey1, szHdr1);
  d1 = szHdr1;
  assert( pKeyInfo->nField+1>=pPKey2->nField );
  assert( pKeyInfo->aSortOrder!=0 );
  while( idx1<szHdr1 && i<pPKey2->nField ){
    u32 serial_type1;

    /* Read the serial types for the next element in each key. */
    idx1 += getVarint32( aKey1+idx1, serial_type1 );

    /* Verify that there is enough key space remaining to avoid
    ** a buffer overread.  The "d1+serial_type1+2" subexpression will
    ** always be greater than or equal to the amount of required key space.
    ** Use that approximation to avoid the more expensive call to
    ** sqlite3VdbeSerialTypeLen() in the common case.
    */
    if( d1+serial_type1+2>(u32)nKey1
     && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 
    ){
      break;
    }

    /* Extract the values to be compared.
    */
    d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);

    /* Do the comparison
    */
    rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
    if( rc!=0 ){
      assert( mem1.zMalloc==0 );  /* See comment below */

      /* Invert the result if we are using DESC sort order. */
      if( pKeyInfo->aSortOrder[i] ){
        rc = -rc;
      }
    
      /* If the PREFIX_SEARCH flag is set and all fields except the final
      ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
      ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
      ** This is used by the OP_IsUnique opcode.
      */
      if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
        assert( idx1==szHdr1 && rc );
        assert( mem1.flags & MEM_Int );
        pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
        pPKey2->rowid = mem1.u.i;
      }
    
      return rc;
    }
    i++;
  }

  /* No memory allocation is ever used on mem1.  Prove this using
  ** the following assert().  If the assert() fails, it indicates a
  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
  */
  assert( mem1.zMalloc==0 );

  /* rc==0 here means that one of the keys ran out of fields and
  ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
  ** flag is set, then break the tie by treating key2 as larger.
  ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
  ** are considered to be equal.  Otherwise, the longer key is the 
  ** larger.  As it happens, the pPKey2 will always be the longer
  ** if there is a difference.
  */
  assert( rc==0 );
  if( pPKey2->flags & UNPACKED_INCRKEY ){
    rc = -1;
  }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
    /* Leave rc==0 */
  }else if( idx1<szHdr1 ){
    rc = 1;
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r13
movq %rsi, %r15
movl %edi, -0x30(%rbp)
movq (%rdx), %rcx
movb 0x8(%rcx), %al
movb %al, -0x71(%rbp)
movq %rcx, -0x40(%rbp)
movq (%rcx), %rax
movq %rax, -0x98(%rbp)
movsbl (%rsi), %edx
testl %edx, %edx
js 0x30e59
movl $0x1, %r8d
jmp 0x30e70
leaq -0x5c(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2134f
movzbl %al, %r8d
movl (%r12), %edx
cmpl %edx, %r8d
jae 0x30fb5
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %edx, %r9d
movl %edx, -0x2c(%rbp)
movq %r15, -0x48(%rbp)
movzwl 0x8(%r13), %eax
cmpq %rax, %r14
jae 0x30fb5
movq %r14, -0x50(%rbp)
movq %r12, -0x58(%rbp)
movl %r8d, %edi
movsbl (%r15,%rdi), %esi
testl %esi, %esi
js 0x30eb2
movl $0x1, %eax
jmp 0x30ed0
addq %r15, %rdi
leaq -0x38(%rbp), %rsi
movl %r8d, %ebx
movq %r9, %r14
callq 0x2134f
movq %r14, %r9
movl %ebx, %r8d
movzbl %al, %eax
movl -0x38(%rbp), %esi
addl %eax, %r8d
leal (%r9,%rsi), %eax
addl $0x2, %eax
cmpl -0x30(%rbp), %eax
jbe 0x30f04
cmpl $0xc, %esi
jb 0x30eeb
leal -0xc(%rsi), %eax
shrl %eax
jmp 0x30ef8
movl %esi, %eax
leaq 0x4553e(%rip), %rcx     # 0x76432
movzbl (%rax,%rcx), %eax
addl %r9d, %eax
cmpl -0x30(%rbp), %eax
ja 0x30fb2
movl %r8d, -0x34(%rbp)
movl %r9d, %edi
addq %r15, %rdi
leaq -0x98(%rbp), %rdx
movq %r9, %rbx
callq 0x2ce53
movq %r13, %rcx
movl %eax, %r13d
movq %rcx, %r15
movq 0x18(%rcx), %rsi
movq -0x58(%rbp), %r12
addq %r12, %rsi
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %r14
movq 0x18(%rax,%r14,8), %rdx
leaq -0x98(%rbp), %rdi
callq 0x2ca3b
testl %eax, %eax
jne 0x30f74
addl %ebx, %r13d
incq %r14
addq $0x38, %r12
movl %r13d, %r9d
movl -0x2c(%rbp), %edx
movl -0x34(%rbp), %r8d
cmpl %edx, %r8d
movq %r15, %r13
movq -0x48(%rbp), %r15
jb 0x30e89
jmp 0x30fb5
movl %eax, %ecx
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdx
movl %ecx, %eax
negl %eax
cmpb $0x0, (%rdx,%r14)
cmovel %ecx, %eax
movb 0xa(%r15), %cl
testb $0x4, %cl
je 0x30fd5
movzwl 0x8(%r15), %edx
subl %edx, %r14d
cmpl $-0x1, %r14d
jne 0x30fd5
andb $-0x5, %cl
movb %cl, 0xa(%r15)
movq -0x80(%rbp), %rcx
movq %rcx, 0x10(%r15)
jmp 0x30fd5
movl -0x2c(%rbp), %edx
movzbl 0xa(%r13), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x1, %cl
jne 0x30fd5
testb $0x2, %cl
sete %al
cmpl %edx, %r8d
setb %cl
andb %al, %cl
movzbl %cl, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq