// File: /workspace/github_c_makefile_star2_O1/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 %rsi, %r14
movl %edi, -0x38(%rbp)
movq (%rdx), %rcx
movq %rcx, -0x60(%rbp)
movb 0x8(%rcx), %al
movb %al, -0x71(%rbp)
movq (%rcx), %rax
movq %rax, -0x98(%rbp)
movsbl (%rsi), %eax
testl %eax, %eax
movq %rdx, -0x50(%rbp)
js 0x3058c
movq %rdx, %rsi
movl %eax, -0x40(%rbp)
movl $0x1, %r13d
jmp 0x305a0
leaq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x216ad
movq -0x50(%rbp), %rsi
movzbl %al, %r13d
xorl %r12d, %r12d
movl -0x40(%rbp), %eax
xorl %r8d, %r8d
movl %eax, -0x34(%rbp)
movl %eax, %r15d
movq %r14, -0x48(%rbp)
cmpl -0x34(%rbp), %r13d
jae 0x306f5
movzwl 0x8(%rsi), %eax
cmpl %eax, %r12d
jge 0x306f5
movl %r13d, -0x54(%rbp)
movl %r13d, %edi
movsbl (%r14,%rdi), %eax
testl %eax, %eax
js 0x305e5
movl %eax, -0x3c(%rbp)
movl $0x1, %r13d
jmp 0x305fb
addq %r14, %rdi
leaq -0x3c(%rbp), %rsi
movl %r8d, %ebx
callq 0x216ad
movl %ebx, %r8d
movzbl %al, %r13d
movl -0x3c(%rbp), %esi
leal (%r15,%rsi), %eax
addl $0x2, %eax
cmpl -0x38(%rbp), %eax
jbe 0x3063a
cmpl $0xc, %esi
jb 0x30616
leal -0xc(%rsi), %ecx
shrl %ecx
jmp 0x30621
leaq 0x45dc5(%rip), %rax     # 0x763e2
movzbl (%rsi,%rax), %ecx
addl %r15d, %ecx
movl $0x3, %eax
cmpl -0x38(%rbp), %ecx
jbe 0x3063a
movl %r15d, %ecx
movq -0x50(%rbp), %rsi
jmp 0x306e1
movl %r15d, %edi
addq %r14, %rdi
leaq -0x98(%rbp), %rdx
callq 0x2c565
addl %r15d, %eax
movl %eax, -0x2c(%rbp)
movslq %r12d, %r15
imulq $0x38, %r15, %rsi
movq -0x50(%rbp), %r14
addq 0x18(%r14), %rsi
movl %r12d, %ebx
movq -0x60(%rbp), %r12
movq 0x18(%r12,%r15,8), %rdx
leaq -0x98(%rbp), %rdi
callq 0x2c122
testl %eax, %eax
je 0x306a8
movq 0x10(%r12), %rcx
movl %eax, %r8d
negl %r8d
cmpb $0x0, (%rcx,%r15)
cmovel %eax, %r8d
movb 0xa(%r14), %cl
movl $0x1, %eax
testb $0x4, %cl
movq %r14, %rsi
jne 0x306bb
movl %r8d, -0x30(%rbp)
jmp 0x306b2
incl %ebx
xorl %r8d, %r8d
xorl %eax, %eax
movq %r14, %rsi
movq -0x48(%rbp), %r14
movl %ebx, %r12d
jmp 0x306de
movzwl 0x8(%rsi), %edx
decl %edx
movl %ebx, %r12d
cmpl %edx, %ebx
movq -0x48(%rbp), %r14
jne 0x306da
andb $-0x5, %cl
movb %cl, 0xa(%rsi)
movq -0x80(%rbp), %rcx
movq %rcx, 0x10(%rsi)
movl %r8d, -0x30(%rbp)
movl -0x2c(%rbp), %ecx
addl -0x54(%rbp), %r13d
movl %ecx, %r15d
testl %eax, %eax
je 0x305b3
cmpl $0x3, %eax
jne 0x30719
movzbl 0xa(%rsi), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x1, %cl
jne 0x3071c
cmpl -0x34(%rbp), %r13d
movl $0x1, %eax
cmovael %r8d, %eax
testb $0x2, %cl
cmovnel %r8d, %eax
jmp 0x3071c
movl -0x30(%rbp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq