// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, %rbx
movq %rsi, %r12
movl 0x10(%rdi), %eax
movl %eax, -0x2c(%rbp)
movq (%rdi), %r14
movq 0x8(%rdi), %rax
movq %rax, -0x60(%rbp)
movzbl (%r14), %r15d
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x2786
movq %rax, -0x48(%rbp)
leaq -0x100(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x2220
leaq -0xc0(%rbp), %rdi
movq %rbx, %rsi
callq 0x2220
leaq -0xe0(%rbp), %rdi
leaq -0x100(%rbp), %rbx
leaq -0xc0(%rbp), %rdx
movq %rbx, %rsi
callq 0x27ac
leaq -0xc0(%rbp), %rdi
callq 0x2120
leaq -0x100(%rbp), %rdi
callq 0x2120
leaq -0xa0(%rbp), %rdi
leaq -0xe0(%rbp), %rbx
movq %rbx, %rsi
callq 0x2220
leaq -0xa0(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x284a
movl %eax, %r13d
leaq -0xa0(%rbp), %rdi
callq 0x2120
leaq -0x80(%rbp), %rdi
movq %rbx, %rsi
movq %rbx, -0x38(%rbp)
callq 0x2220
leaq -0x80(%rbp), %rdi
movl %r15d, %esi
callq 0x28b2
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x2120
movl %r13d, %edi
movq %rbx, -0x40(%rbp)
movq %rbx, %rsi
movq -0x48(%rbp), %rdx
callq 0x295c
movl %eax, %ebx
callq 0x20a0
testb $0x1, %al
cmovel %ebx, %r13d
movsd -0x60(%rbp), %xmm0
unpcklps 0x1a04(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1a0c(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x60(%rbp)
movl -0x2c(%rbp), %r12d
subl $0x1, %r12d
jb 0x2706
movl %r13d, -0x4c(%rbp)
movl %r13d, %eax
imull %r15d, %eax
leal (%rax,%r15), %ecx
leal 0x28(,%rcx,4), %r13d
movl %r13d, %ecx
shrl $0x3, %ecx
cvtsi2sd %ecx, %xmm0
movapd -0x60(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2714
leal 0x28(,%rax,4), %ebx
cmpl %r13d, %ebx
jae 0x2686
movq %r14, %rdi
movl %ebx, %esi
movl %r15d, %edx
callq 0x29dc
testq %rax, %rax
je 0x26d4
addl %r15d, %ebx
jmp 0x266a
callq 0x20a0
orl $-0x4, %eax
imull %r15d, %eax
addl %eax, %ebx
movq %r14, %rdi
movl %ebx, %esi
movl %r15d, %edx
callq 0x29dc
movq %rax, %r13
movq %r14, %rdi
movl %ebx, %esi
movl %r15d, %edx
movq -0x40(%rbp), %rcx
callq 0x2a9e
movl -0x4c(%rbp), %edi
movq %r13, %rsi
movq -0x48(%rbp), %rdx
callq 0x295c
movq %r13, -0x40(%rbp)
movl %r12d, -0x2c(%rbp)
movl %eax, %r13d
jmp 0x2629
movq %r14, %rdi
movl %ebx, %esi
movl %r15d, %edx
movq -0x40(%rbp), %rcx
callq 0x2a9e
leaq -0xe0(%rbp), %rdi
callq 0x2120
movl -0x2c(%rbp), %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1baa(%rip), %rdi      # 0x42b7
callq 0x2250
jmp 0x26e5
leaq 0x196a(%rip), %rdi      # 0x4085
leaq 0x1928(%rip), %rsi      # 0x404a
leaq 0x1980(%rip), %rcx      # 0x40a9
pushq $0x5e
popq %rdx
callq 0x2180
jmp 0x2771
jmp 0x2771
movq %rax, %r14
leaq -0x80(%rbp), %rdi
jmp 0x2764
jmp 0x2771
movq %rbx, -0x38(%rbp)
movq %rax, %r14
leaq -0xa0(%rbp), %rdi
jmp 0x2764
movq %rbx, -0x38(%rbp)
jmp 0x2771
movq %rbx, -0x38(%rbp)
movq %rax, %r14
leaq -0xc0(%rbp), %rdi
callq 0x2120
jmp 0x2774
movq %r13, -0x38(%rbp)
jmp 0x2771
movq %rax, %r14
movq -0x38(%rbp), %rdi
callq 0x2120
movq %r14, %rdi
callq 0x2290
nop