// File: /workspace/github_cpp_makefile_star2_O2/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 $0xe8, %rsp
movq %rdx, %r12
movq %rsi, %r13
movq (%rdi), %r14
movq 0x8(%rdi), %rax
movq %rax, -0x38(%rbp)
movzbl (%r14), %r15d
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x2786
movq %rax, -0x50(%rbp)
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2220
leaq -0xd0(%rbp), %rdi
movq %r12, %rsi
callq 0x2220
leaq -0xf0(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
movq %rbx, %rsi
callq 0x27ac
leaq -0xd0(%rbp), %rdi
callq 0x2120
leaq -0x110(%rbp), %rdi
callq 0x2120
leaq -0xb0(%rbp), %rdi
leaq -0xf0(%rbp), %rbx
movq %rbx, %rsi
callq 0x2220
leaq -0xb0(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x284a
movl %eax, %r12d
leaq -0xb0(%rbp), %rdi
callq 0x2120
leaq -0x90(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movq %rsi, -0x30(%rbp)
callq 0x2220
leaq -0x90(%rbp), %rdi
movl %r15d, %esi
callq 0x28b2
movq %rax, %r13
leaq -0x90(%rbp), %rdi
callq 0x2120
movl %r12d, %edi
movq %r13, %rsi
movq -0x50(%rbp), %rdx
callq 0x295c
movl %r12d, %ecx
movl %eax, %eax
movq %rcx, -0x70(%rbp)
movq %rax, -0x68(%rbp)
movl %r15d, %ecx
movsd -0x38(%rbp), %xmm0
unpcklps 0x1371(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1379(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x50(%rbp)
xorl %eax, %eax
movq %rcx, -0x58(%rbp)
cmpq $0x2, %rax
je 0x2d30
movq %rax, -0x38(%rbp)
movl -0x70(%rbp,%rax,8), %eax
imull %ecx, %eax
leal 0x28(,%rax,4), %r12d
movl %r12d, %ecx
shrl $0x3, %ecx
cvtsi2sd %ecx, %xmm0
movapd -0x50(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2d4e
addl %r15d, %eax
leal 0x28(,%rax,4), %ebx
cmpl %ebx, %r12d
jae 0x2d13
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq 0x29dc
cmpq %r13, %rax
je 0x2d20
addl %r15d, %r12d
jmp 0x2cf6
movq -0x38(%rbp), %rax
incq %rax
movq -0x58(%rbp), %rcx
jmp 0x2cbe
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0x2a9e
leaq -0xf0(%rbp), %rdi
callq 0x2120
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1387(%rip), %rdi      # 0x40dc
leaq 0x12ee(%rip), %rsi      # 0x404a
leaq 0x139f(%rip), %rcx      # 0x4102
movl $0xa7, %edx
callq 0x2180
jmp 0x2daa
jmp 0x2daa
movq %rax, %r14
leaq -0x90(%rbp), %rdi
jmp 0x2d9f
jmp 0x2daa
movq %rbx, -0x30(%rbp)
movq %rax, %r14
leaq -0xb0(%rbp), %rdi
jmp 0x2d9f
jmp 0x2da6
movq %rbx, -0x30(%rbp)
movq %rax, %r14
leaq -0xd0(%rbp), %rdi
callq 0x2120
jmp 0x2dad
movq %rbx, -0x30(%rbp)
movq %rax, %r14
movq -0x30(%rbp), %rdi
callq 0x2120
movq %r14, %rdi
callq 0x2290