// File: 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
subq $0x190, %rsp            # imm = 0x190
movq %rsi, -0x158(%rbp)
movq %rdx, -0x150(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x28(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x28(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x2b50
movq -0x158(%rbp), %rsi
movq %rax, -0x40(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x2110
movq -0x150(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x2110
jmp 0x362e
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0x2bb0
jmp 0x3644
leaq -0xa0(%rbp), %rdi
callq 0x2190
leaq -0x80(%rbp), %rdi
callq 0x2190
leaq -0xd8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2110
jmp 0x366b
movl -0x40(%rbp), %esi
leaq -0xd8(%rbp), %rdi
callq 0x2cc0
movl %eax, -0x15c(%rbp)
jmp 0x3682
movl -0x15c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x168(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x2190
movq -0x168(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2110
jmp 0x36bd
movl -0x34(%rbp), %esi
leaq -0x100(%rbp), %rdi
callq 0x2d60
movq %rax, -0x170(%rbp)
jmp 0x36d5
leaq -0x100(%rbp), %rdi
callq 0x2190
movq -0x170(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl -0xb8(%rbp), %edi
movq -0xe0(%rbp), %rsi
movl -0x40(%rbp), %edx
callq 0x2e70
movl %eax, -0x174(%rbp)
jmp 0x370c
movl -0x174(%rbp), %eax
movl %eax, %eax
movq %rax, -0x108(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x118(%rbp)
movl $0x0, -0x124(%rbp)
cmpl $0x2, -0x124(%rbp)
jge 0x3923
movslq -0x124(%rbp), %rax
movq -0x120(%rbp,%rax,8), %rax
movq %rax, -0x130(%rbp)
movl -0x130(%rbp), %ecx
movl -0x34(%rbp), %eax
imull %eax, %ecx
movl %ecx, %eax
leal 0x28(,%rax,4), %eax
movl %eax, -0x134(%rbp)
movl -0x134(%rbp), %edx
movl -0x34(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,4), %eax
movl %eax, -0x138(%rbp)
movl -0x134(%rbp), %edi
shrl $0x3, %edi
callq 0x41e0
movsd %xmm0, -0x180(%rbp)
jmp 0x37ad
movsd -0x180(%rbp), %xmm1
movaps 0x2854(%rip), %xmm2     # 0x6010
movq -0x30(%rbp), %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x2853(%rip), %xmm2     # 0x6020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x37e7
jmp 0x38a3
leaq 0x291c(%rip), %rdi      # 0x610a
leaq 0x2855(%rip), %rsi      # 0x604a
movl $0xdc, %edx
leaq 0x295d(%rip), %rcx      # 0x615e
callq 0x21b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
jmp 0x3838
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x2190
leaq -0x80(%rbp), %rdi
callq 0x2190
jmp 0x3951
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
jmp 0x3948
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x2190
jmp 0x3948
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x2190
jmp 0x3948
jmp 0x38a5
movl -0x134(%rbp), %eax
cmpl -0x138(%rbp), %eax
jae 0x390d
movq -0x28(%rbp), %rdi
movl -0x134(%rbp), %esi
movl -0x34(%rbp), %edx
callq 0x2f50
movq %rax, -0x188(%rbp)
jmp 0x38ce
movq -0x188(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
cmpq -0xe0(%rbp), %rax
jne 0x38fc
movb $0x1, -0x1(%rbp)
movl $0x1, -0x144(%rbp)
jmp 0x3931
movl -0x34(%rbp), %eax
addl -0x134(%rbp), %eax
movl %eax, -0x134(%rbp)
jmp 0x38a5
jmp 0x390f
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x3741
movb $0x0, -0x1(%rbp)
movl $0x1, -0x144(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x2190
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x2190
movq -0xa8(%rbp), %rdi
callq 0x2330
nopl (%rax)