// File: /workspace/github_cpp_makefile_star2_O2/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 %rsi, %r13
movq (%rdi), %rbx
movq 0x8(%rdi), %rax
movq %rax, -0x40(%rbp)
movzbl (%rbx), %r14d
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x2786
movq %rax, -0x30(%rbp)
leaq -0x100(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2220
leaq -0xc0(%rbp), %rdi
movq %r12, %rsi
callq 0x2220
leaq -0xe0(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
movq %r15, %rsi
callq 0x27ac
leaq -0xc0(%rbp), %rdi
callq 0x2120
leaq -0x100(%rbp), %rdi
callq 0x2120
leaq -0xa0(%rbp), %rdi
leaq -0xe0(%rbp), %r15
movq %r15, %rsi
callq 0x2220
leaq -0xa0(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x284a
movl %eax, %r12d
leaq -0xa0(%rbp), %rdi
callq 0x2120
leaq -0x80(%rbp), %rdi
movq %r15, %rsi
callq 0x2220
leaq -0x80(%rbp), %rdi
movl %r14d, %esi
callq 0x28b2
movq %rax, %r13
leaq -0x80(%rbp), %rdi
callq 0x2120
movl %r12d, %edi
movq %r13, %rsi
movq -0x30(%rbp), %rdx
callq 0x295c
movl %r12d, %ecx
movl %eax, %eax
movq %rcx, -0x60(%rbp)
movq %rax, -0x58(%rbp)
movl %r14d, %eax
movq %rax, -0x48(%rbp)
movsd -0x40(%rbp), %xmm0
unpcklps 0x1140(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1148(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x40(%rbp)
xorl %eax, %eax
movq %rax, -0x30(%rbp)
cmpq $0x2, %rax
je 0x2f4f
movq -0x30(%rbp), %rax
movl -0x60(%rbp,%rax,8), %eax
imull -0x48(%rbp), %eax
leal 0x28(,%rax,4), %r15d
movl %r15d, %ecx
shrl $0x3, %ecx
cvtsi2sd %ecx, %xmm0
movapd -0x40(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2f77
addl %r14d, %eax
leal 0x28(,%rax,4), %r12d
cmpl %r12d, %r15d
jae 0x2f46
movq %rbx, %rdi
movl %r15d, %esi
movl %r14d, %edx
callq 0x29dc
addl %r14d, %r15d
cmpq %r13, %rax
jne 0x2f29
jmp 0x2f4f
movq -0x30(%rbp), %rax
incq %rax
jmp 0x2eeb
cmpq $0x2, -0x30(%rbp)
setb %bl
leaq -0xe0(%rbp), %rdi
callq 0x2120
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x115e(%rip), %rdi      # 0x40dc
leaq 0x10c5(%rip), %rsi      # 0x404a
leaq 0x11a4(%rip), %rcx      # 0x4130
movl $0xdc, %edx
callq 0x2180
jmp 0x2fc2
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
jmp 0x2fbb
jmp 0x2fc2
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
jmp 0x2fbb
jmp 0x2fc2
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x2120
jmp 0x2fc5
movq %rax, %rbx
movq %r15, %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x2290