// File: /workspace/github_cpp_makefile_star2_O1/yoavweiss[P]cache-digests-cuckoo/cuckoo.cpp
// Function: Cuckoo::remove(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void Cuckoo::remove(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`
    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 outside 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`, set `bits` to all zeros and terminate these steps.
            if (fingerprintValue == destinationFingerprintValue) {
                writeFingerprint(digest, positionStart, fingerprintSize, 0);
                return;
            }
            // 3. Add `f` to `position_start`.
            positionStart += fingerprintSize;
        }
    }
}

// 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, -0x2c(%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 0x2de1
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 0x2e6b
callq 0x21c0
movq -0xf0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2e7c
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 0x2ec2
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 -0x2c(%rbp), %esi
callq 0x2a8c
movq %rax, %r12
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2f04
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)
movl -0x2c(%rbp), %eax
movq %rax, -0x40(%rbp)
movsd -0x38(%rbp), %xmm0
unpcklps 0x20ee(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x20f6(%rip), %xmm0     # 0x5030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x100(%rbp)
xorl %eax, %eax
movl -0x2c(%rbp), %r13d
movq %rax, -0x38(%rbp)
movl -0x70(%rbp,%rax,8), %eax
imull -0x40(%rbp), %eax
leal 0x28(,%rax,4), %r15d
movl %r15d, %ecx
shrl $0x3, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movapd -0x100(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2fed
addl %r13d, %eax
leal 0x28(,%rax,4), %ebx
cmpl %ebx, %r15d
jae 0x2faa
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x2bd4
cmpq %r12, %rax
je 0x2fb9
addl %r13d, %r15d
jmp 0x2f8d
movq -0x38(%rbp), %rcx
leaq 0x1(%rcx), %rax
testq %rcx, %rcx
je 0x2f54
jmp 0x2fc9
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x2ca6
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2fdb
callq 0x21c0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20f8(%rip), %rdi      # 0x50ec
leaq 0x205f(%rip), %rsi      # 0x505a
leaq 0x2110(%rip), %rcx      # 0x5112
movl $0xa7, %edx
callq 0x2160
jmp 0x3039
jmp 0x3039
movq %rax, %r14
movq -0x90(%rbp), %rdi
cmpq %rbx, %rdi
jne 0x3032
jmp 0x303c
jmp 0x3039
movq %rax, %r14
movq -0xb0(%rbp), %rdi
cmpq %r12, %rdi
je 0x303c
callq 0x21c0
jmp 0x303c
movq %rax, %r14
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x304e
callq 0x21c0
movq %r14, %rdi
callq 0x2280
movq %rax, %r14
movq -0xd0(%rbp), %rdi
cmpq %r13, %rdi
je 0x306f
callq 0x21c0
jmp 0x306f
movq %rax, %r14
movq -0xf0(%rbp), %rdi
cmpq %rbx, %rdi
jne 0x3049
jmp 0x304e
nop