// File: 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
subq $0x180, %rsp            # imm = 0x180
movq %rsi, -0x148(%rbp)
movq %rdx, -0x140(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x20(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x2b50
movq -0x148(%rbp), %rsi
movq %rax, -0x38(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x2110
movq -0x140(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x2110
jmp 0x326e
leaq -0x58(%rbp), %rdi
leaq -0x78(%rbp), %rsi
leaq -0x98(%rbp), %rdx
callq 0x2bb0
jmp 0x3284
leaq -0x98(%rbp), %rdi
callq 0x2190
leaq -0x78(%rbp), %rdi
callq 0x2190
leaq -0xd0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x2110
jmp 0x32ab
movl -0x38(%rbp), %esi
leaq -0xd0(%rbp), %rdi
callq 0x2cc0
movl %eax, -0x14c(%rbp)
jmp 0x32c2
movl -0x14c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x158(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x2190
movq -0x158(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x2110
jmp 0x32fd
movl -0x2c(%rbp), %esi
leaq -0xf8(%rbp), %rdi
callq 0x2d60
movq %rax, -0x160(%rbp)
jmp 0x3315
leaq -0xf8(%rbp), %rdi
callq 0x2190
movq -0x160(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0xb0(%rbp), %edi
movq -0xd8(%rbp), %rsi
movl -0x38(%rbp), %edx
callq 0x2e70
movl %eax, -0x164(%rbp)
jmp 0x334c
movl -0x164(%rbp), %eax
movl %eax, %eax
movq %rax, -0x100(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x108(%rbp)
movl $0x0, -0x114(%rbp)
cmpl $0x2, -0x114(%rbp)
jge 0x3577
movslq -0x114(%rbp), %rax
movq -0x110(%rbp,%rax,8), %rax
movq %rax, -0x120(%rbp)
movl -0x120(%rbp), %ecx
movl -0x2c(%rbp), %eax
imull %eax, %ecx
movl %ecx, %eax
leal 0x28(,%rax,4), %eax
movl %eax, -0x124(%rbp)
movl -0x124(%rbp), %edx
movl -0x2c(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,4), %eax
movl %eax, -0x128(%rbp)
movl -0x124(%rbp), %edi
shrl $0x3, %edi
callq 0x41e0
movsd %xmm0, -0x170(%rbp)
jmp 0x33ed
movsd -0x170(%rbp), %xmm1
movaps 0x2c14(%rip), %xmm2     # 0x6010
movq -0x28(%rbp), %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x2c13(%rip), %xmm2     # 0x6020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x3427
jmp 0x34e3
leaq 0x2cdc(%rip), %rdi      # 0x610a
leaq 0x2c15(%rip), %rsi      # 0x604a
movl $0xa7, %edx
leaq 0x2cef(%rip), %rcx      # 0x6130
callq 0x21b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x3478
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x2190
leaq -0x78(%rbp), %rdi
callq 0x2190
jmp 0x359c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x3593
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x2190
jmp 0x3593
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x2190
jmp 0x3593
jmp 0x34e5
movl -0x124(%rbp), %eax
cmpl -0x128(%rbp), %eax
jae 0x3561
movq -0x20(%rbp), %rdi
movl -0x124(%rbp), %esi
movl -0x2c(%rbp), %edx
callq 0x2f50
movq %rax, -0x178(%rbp)
jmp 0x350e
movq -0x178(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rax
cmpq -0xd8(%rbp), %rax
jne 0x3550
movq -0x20(%rbp), %rdi
movl -0x124(%rbp), %esi
movl -0x2c(%rbp), %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3070
jmp 0x3544
movl $0x1, -0x134(%rbp)
jmp 0x3581
movl -0x2c(%rbp), %eax
addl -0x124(%rbp), %eax
movl %eax, -0x124(%rbp)
jmp 0x34e5
jmp 0x3563
movl -0x114(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
jmp 0x3381
movl $0x0, -0x134(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x2190
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0x2190
movq -0xa0(%rbp), %rdi
callq 0x2330
nopl (%rax,%rax)