// File: /workspace/github_c_makefile_star2_O2/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
movl %edi, -0x30(%rbp)
movq (%rdx), %rcx
movb 0x8(%rcx), %al
movb %al, -0x71(%rbp)
movq %rcx, -0x48(%rbp)
movq (%rcx), %rax
movq %rax, -0x98(%rbp)
movsbl (%rsi), %ecx
testl %ecx, %ecx
movq %rsi, -0x38(%rbp)
js 0x2bff4
movq %rsi, %rbx
pushq $0x1
popq %rdx
jmp 0x2c015
movq %r13, %rbx
leaq -0x5c(%rbp), %r13
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x1e561
movzbl %al, %edx
movl (%r13), %ecx
movq %rbx, %r13
movq -0x38(%rbp), %rbx
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %ecx, %r8d
movl %ecx, -0x2c(%rbp)
cmpl %ecx, %edx
jae 0x2c145
movzwl 0x8(%r13), %eax
cmpq %rax, %r14
jae 0x2c145
movq %r14, -0x50(%rbp)
movq %r12, -0x58(%rbp)
movl %edx, %edi
movsbl (%rbx,%rdi), %esi
testl %esi, %esi
js 0x2c04e
pushq $0x1
popq %rax
jmp 0x2c06c
addq %rbx, %rdi
leaq -0x40(%rbp), %rsi
movl %edx, %r15d
movq %r8, %r14
callq 0x1e561
movq %r14, %r8
movl %r15d, %edx
movzbl %al, %eax
movl -0x40(%rbp), %esi
addl %eax, %edx
leal (%r8,%rsi), %eax
addl $0x2, %eax
cmpl -0x30(%rbp), %eax
jbe 0x2c09f
cmpl $0xc, %esi
jb 0x2c086
leal -0xc(%rsi), %eax
shrl %eax
jmp 0x2c093
movl %esi, %eax
leaq 0x3c363(%rip), %rcx     # 0x683f2
movzbl (%rax,%rcx), %eax
addl %r8d, %eax
cmpl -0x30(%rbp), %eax
ja 0x2c142
movl %edx, -0x3c(%rbp)
movl %r8d, %edi
addq %rbx, %rdi
leaq -0x98(%rbp), %rdx
movq %r8, %r15
callq 0x29f23
movq %r13, %rcx
movl %eax, %r13d
movq %rcx, %rbx
movq 0x18(%rcx), %rsi
movq -0x58(%rbp), %r12
addq %r12, %rsi
movq -0x48(%rbp), %rax
movq -0x50(%rbp), %r14
movq 0x18(%rax,%r14,8), %rdx
leaq -0x98(%rbp), %rdi
callq 0x29b5f
testl %eax, %eax
jne 0x2c107
addl %r15d, %r13d
incq %r14
addq $0x38, %r12
movl %r13d, %r8d
movq %rbx, %r13
movl -0x2c(%rbp), %ecx
movq -0x38(%rbp), %rbx
movl -0x3c(%rbp), %edx
jmp 0x2c021
movl %eax, %ecx
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rdx
movl %ecx, %eax
negl %eax
cmpb $0x0, (%rdx,%r14)
cmovel %ecx, %eax
movb 0xa(%rbx), %cl
testb $0x4, %cl
je 0x2c162
movzwl 0x8(%rbx), %edx
subl %edx, %r14d
cmpl $-0x1, %r14d
jne 0x2c162
andb $-0x5, %cl
movb %cl, 0xa(%rbx)
movq -0x80(%rbp), %rcx
movq %rcx, 0x10(%rbx)
jmp 0x2c162
movl -0x2c(%rbp), %ecx
movzbl 0xa(%r13), %eax
testb $0x1, %al
jne 0x2c15f
testb $0x2, %al
sete %al
cmpl %ecx, %edx
setb %cl
andb %al, %cl
movzbl %cl, %eax
jmp 0x2c162
pushq $-0x1
popq %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq