// File: /workspace/github_cpp_makefile_star2_O1/yoavweiss[P]cache-digests-cuckoo/cuckoo.cpp
// Function: Cuckoo::query(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool Cuckoo::query(std::string URL, std::string ETag) {
    //Given the following inputs:
    //
    //* `URL` a string corresponding to the Effective Request URI ({{RFC7230}}, Section 5.5) of a cached
    //response {{RFC7234}}.
    //* `ETag` a string corresponding to the entity-tag {{RFC7232}} of a cached response {{RFC7234}} (if
    //the ETag is available; otherwise, null).
    //* `digest-value`, an array of bits.
    const unsigned char* digest = m_digest;
    size_t digestSize = m_digestSize;

    // 1. Let `f` be the value of the first byte of `digest-value`.
    unsigned fingerprintSize = digest[0];
    // 3. Let `N` be the value of the second to fifth bytes of `digest-value` in big endian form.
    unsigned long entries = bigEndianRead(digest, 1, 4);
    // 4. Let `key` be the return value of {{key}} with `URL` and `ETag` as inputs.
    std::string keyStr = key(URL, ETag);
    // 5. Let `h1` be the return value of {{hash}} with `key` and `N` as inputs.
    unsigned long h1 = hash(keyStr, entries);
    // 6. Let `fingerprint` be the return value of {{fingerprint}} with `key` and `f` as inputs.
    unsigned long destinationFingerprintValue = fingerprint(keyStr, fingerprintSize);
    // 7. Let `h2` be the return value of {{hash2}} with `h1`, `fingerprint` and `N` as inputs.
    unsigned long h2 = alternateHash(h1, destinationFingerprintValue, entries);
    // 8. Let `hashes` be an array containing `h1` and `h2`.
    unsigned long hashes[2];
    hashes[0] = h1;
    hashes[1] = h2;
    // 9. For each `h` in `hashes`:
    for (int i = 0; i < 2; ++i) {
        unsigned long h = hashes[i];
        // 1. Let `position_start` be 40 + `h` \* `f` \* `b`.
        unsigned positionStart = 40 + h * fingerprintSize * BucketSize;
        // 2. Let `position_end` be `position_start` + `f` \* `b`.
        unsigned positionEnd = positionStart + fingerprintSize * BucketSize;
        unsigned long fingerprintValue;
        // Make sure we're not reading beyond the table.
        assert(ceil(positionStart / 8) <= digestSize);
        // 3. While `position_start` < `position_end`:
        while (positionStart < positionEnd) {
            // 1. Let `bits` be `f` bits from `digest_value` starting at `position_start`.
            fingerprintValue = readFingerprint(digest, positionStart, fingerprintSize);
            // 2. If `bits` is `fingerprint`, return true
            if (fingerprintValue == destinationFingerprintValue) {
                return true;
            }
            // 3. Add `f` to `position_start`.
            positionStart += fingerprintSize;
        }
    }
    // 14. Return false.
    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, %r12
movq (%rdi), %r14
movq 0x8(%rdi), %rax
movq %rax, -0x38(%rbp)
movzbl (%r14), %eax
movl %eax, -0x30(%rbp)
movl $0x1, %eax
xorl %r15d, %r15d
movq %r15, %rcx
shlq $0x8, %rcx
movzbl (%r14,%rax), %r15d
orq %rcx, %r15
incq %rax
cmpq $0x5, %rax
jne 0x30af
leaq -0xe0(%rbp), %rax
movq %rax, %rbx
movq %rax, -0x10(%rax)
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
addq %rax, %rdx
leaq -0xf0(%rbp), %rdi
movq %rax, %rsi
callq 0x3980
leaq -0xc0(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %rdi
callq 0x3980
leaq -0x60(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
callq 0x28f4
movq -0xd0(%rbp), %rdi
cmpq %r13, %rdi
je 0x3139
callq 0x21c0
movq -0xf0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x314a
callq 0x21c0
leaq -0xa0(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0xb0(%rbp), %rdi
callq 0x3980
leaq -0xb0(%rbp), %rdi
movl %r15d, %esi
callq 0x2a16
movl %eax, %r13d
movq -0xb0(%rbp), %rdi
cmpq %r12, %rdi
je 0x3190
callq 0x21c0
leaq -0x80(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x90(%rbp), %rdi
callq 0x3980
leaq -0x90(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x2a8c
movq %rax, %r12
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x31d2
callq 0x21c0
movl %r13d, %edi
movq %r12, %rsi
movl %r15d, %edx
callq 0x2b36
movl %r13d, %ecx
movl %eax, %eax
movq %rcx, -0x70(%rbp)
movq %rax, -0x68(%rbp)
movsd -0x38(%rbp), %xmm0
unpcklps 0x1e27(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movl -0x30(%rbp), %eax
movq %rax, -0x40(%rbp)
subpd 0x1e28(%rip), %xmm0     # 0x5030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x100(%rbp)
movb $0x1, %bl
xorl %eax, %eax
movl -0x30(%rbp), %r15d
movq %rax, -0x38(%rbp)
movl -0x70(%rbp,%rax,8), %eax
imull -0x40(%rbp), %eax
leal 0x28(,%rax,4), %r13d
movl %r13d, %ecx
shrl $0x3, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movapd -0x100(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x32bb
movb %bl, -0x29(%rbp)
addl %r15d, %eax
leal 0x28(,%rax,4), %ebx
cmpl %ebx, %r13d
jae 0x327d
movq %r14, %rdi
movl %r13d, %esi
movl %r15d, %edx
callq 0x2bd4
cmpq %r12, %rax
je 0x328f
addl %r15d, %r13d
jmp 0x3260
movq -0x38(%rbp), %rcx
leaq 0x1(%rcx), %rax
testq %rcx, %rcx
sete %bl
je 0x3224
jmp 0x3292
movb -0x29(%rbp), %bl
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x32a4
callq 0x21c0
andb $0x1, %bl
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e2a(%rip), %rdi      # 0x50ec
leaq 0x1d91(%rip), %rsi      # 0x505a
leaq 0x1e70(%rip), %rcx      # 0x5140
movl $0xdc, %edx
callq 0x2160
jmp 0x3305
movq %rax, %r14
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
jne 0x32fe
jmp 0x3308
jmp 0x3305
movq %rax, %r14
movq -0xb0(%rbp), %rdi
cmpq %r12, %rdi
je 0x3308
callq 0x21c0
jmp 0x3308
movq %rax, %r14
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x331a
callq 0x21c0
movq %r14, %rdi
callq 0x2280
movq %rax, %r14
movq -0xd0(%rbp), %rdi
cmpq %r13, %rdi
je 0x333b
callq 0x21c0
jmp 0x333b
movq %rax, %r14
movq -0xf0(%rbp), %rdi
cmpq %rbx, %rdi
jne 0x3315
jmp 0x331a