// File: yoavweiss[P]cache-digests-cuckoo/cuckoo.cpp
// Function: Cuckoo::add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
unsigned Cuckoo::add(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);
    //* `maxcount` - max number of cuckoo hops
    //* `digest-value`
    int maxCount = m_maxCount;
    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 `dest_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`, `dest_fingerprint` and `N` as inputs.
    unsigned long h2 = alternateHash(h1, destinationFingerprintValue, entries);
    // 8. Let `h` be either `h1` or `h2`, picked in random.
    int randomNumber = rand() % 2;
    unsigned long h = (randomNumber != 0) ? h1 : h2;
    // 9. While `maxcount` is larger than zero:
    while (maxCount) {
        // 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;
        // Make sure we're not writing outside the table.
        assert(ceil(positionEnd / 8) <= digestSize);
        unsigned long fingerprintValue;
        // 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 all zeros, set `bits` to `dest_fingerprint` and terminate these steps.
            if (fingerprintValue == 0) {
                writeFingerprint(digest, positionStart, fingerprintSize, destinationFingerprintValue);
                return maxCount;
            }
            // 3. Add `f` to `position_start`.
            positionStart += fingerprintSize;
        }
        // 4. Let `e` be a random number from 0 to `b`.
        int elementToThrow = rand() % BucketSize;
        unsigned positionStartBefore = positionStart;
        // 5. Substract `f` * (`b` - `e`) from `position_start`.
        positionStart -= fingerprintSize * (BucketSize - elementToThrow);
        // 6. Let `bits` be `f` bits from `digest_value` starting at `position_start`.
        // 7. Let `fingerprint` be the value of bits, read as big endian.
        fingerprintValue = readFingerprint(digest, positionStart, fingerprintSize);
        // 8. Set `bits` to `dest_fingerprint`.
        writeFingerprint(digest, positionStart, fingerprintSize, destinationFingerprintValue);
        // 9. Set `dest_fingerprint` to `fingerprint`.
        destinationFingerprintValue = fingerprintValue;
        // 10. Let `h` be {{hash2}} with `h`, `dest_fingerprint` and `N` as inputs.
        h = alternateHash(h, destinationFingerprintValue, entries);
        // 11. Substract 1 from `maxcount`.
        --maxCount;
    }
    // 10. Return an error.
    printf("ERROR - maxcount reached\n");
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rsi, -0x150(%rbp)
movq %rdx, -0x148(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
movl %ecx, -0x24(%rbp)
movq (%rax), %rcx
movq %rcx, -0x30(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x2b50
movq -0x150(%rbp), %rsi
movq %rax, -0x48(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x2110
movq -0x148(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x2110
jmp 0x2727
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
callq 0x2bb0
jmp 0x2740
leaq -0xa8(%rbp), %rdi
callq 0x2190
leaq -0x88(%rbp), %rdi
callq 0x2190
leaq -0xe0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x2110
jmp 0x276a
movl -0x48(%rbp), %esi
leaq -0xe0(%rbp), %rdi
callq 0x2cc0
movl %eax, -0x154(%rbp)
jmp 0x2781
movl -0x154(%rbp), %eax
movl %eax, %eax
movq %rax, -0x160(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x2190
movq -0x160(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x2110
jmp 0x27bc
movl -0x3c(%rbp), %esi
leaq -0x108(%rbp), %rdi
callq 0x2d60
movq %rax, -0x168(%rbp)
jmp 0x27d4
leaq -0x108(%rbp), %rdi
callq 0x2190
movq -0x168(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl -0xc0(%rbp), %edi
movq -0xe8(%rbp), %rsi
movl -0x48(%rbp), %edx
callq 0x2e70
movl %eax, -0x16c(%rbp)
jmp 0x280b
movl -0x16c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x110(%rbp)
callq 0x2090
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0x114(%rbp)
cmpl $0x0, -0x114(%rbp)
je 0x2846
movq -0xc0(%rbp), %rax
movq %rax, -0x178(%rbp)
jmp 0x2854
movq -0x110(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
movq %rax, -0x120(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2afc
movl -0x120(%rbp), %ecx
movl -0x3c(%rbp), %eax
imull %eax, %ecx
movl %ecx, %eax
leal 0x28(,%rax,4), %eax
movl %eax, -0x124(%rbp)
movl -0x124(%rbp), %edx
movl -0x3c(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,4), %eax
movl %eax, -0x128(%rbp)
movl -0x128(%rbp), %edi
shrl $0x3, %edi
callq 0x41e0
movsd %xmm0, -0x180(%rbp)
jmp 0x28b5
movsd -0x180(%rbp), %xmm1
movaps 0x374c(%rip), %xmm2     # 0x6010
movq -0x38(%rbp), %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x374b(%rip), %xmm2     # 0x6020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x28ef
jmp 0x29ae
leaq 0x37a3(%rip), %rdi      # 0x6099
leaq 0x374d(%rip), %rsi      # 0x604a
movl $0x5e, %edx
leaq 0x37b4(%rip), %rcx      # 0x60bd
callq 0x21b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x2940
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x2190
leaq -0x88(%rbp), %rdi
callq 0x2190
jmp 0x2b3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x2b32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x2190
jmp 0x2b32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x2190
jmp 0x2b32
jmp 0x29b0
movl -0x124(%rbp), %eax
cmpl -0x128(%rbp), %eax
jae 0x2a35
movq -0x30(%rbp), %rdi
movl -0x124(%rbp), %esi
movl -0x3c(%rbp), %edx
callq 0x2f50
movq %rax, -0x188(%rbp)
jmp 0x29d9
movq -0x188(%rbp), %rax
movq %rax, -0x130(%rbp)
cmpq $0x0, -0x130(%rbp)
jne 0x2a21
movq -0x30(%rbp), %rdi
movl -0x124(%rbp), %esi
movl -0x3c(%rbp), %edx
movq -0xe8(%rbp), %rcx
callq 0x3070
jmp 0x2a0c
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x134(%rbp)
jmp 0x2b1d
movl -0x3c(%rbp), %eax
addl -0x124(%rbp), %eax
movl %eax, -0x124(%rbp)
jmp 0x29b0
callq 0x2090
andl $0x3, %eax
movl %eax, -0x138(%rbp)
movl -0x124(%rbp), %eax
movl %eax, -0x13c(%rbp)
movl -0x3c(%rbp), %ecx
movl -0x138(%rbp), %edx
movl $0x4, %eax
subl %edx, %eax
imull %eax, %ecx
movl -0x124(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x124(%rbp)
movq -0x30(%rbp), %rdi
movl -0x124(%rbp), %esi
movl -0x3c(%rbp), %edx
callq 0x2f50
movq %rax, -0x190(%rbp)
jmp 0x2a8b
movq -0x190(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x30(%rbp), %rdi
movl -0x124(%rbp), %esi
movl -0x3c(%rbp), %edx
movq -0xe8(%rbp), %rcx
callq 0x3070
jmp 0x2ab4
movq -0x130(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl -0x120(%rbp), %edi
movq -0xe8(%rbp), %rsi
movl -0x48(%rbp), %edx
callq 0x2e70
movl %eax, -0x194(%rbp)
jmp 0x2adf
movl -0x194(%rbp), %eax
movl %eax, %eax
movq %rax, -0x120(%rbp)
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2862
leaq 0x35ed(%rip), %rdi      # 0x60f0
xorl %eax, %eax
callq 0x2040
jmp 0x2b0c
movl $0x0, -0x4(%rbp)
movl $0x1, -0x134(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x2190
movl -0x4(%rbp), %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
leaq -0x68(%rbp), %rdi
callq 0x2190
movq -0xb0(%rbp), %rdi
callq 0x2330
nopw (%rax,%rax)