// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc8, %rsp
movq %rdx, %rbx
movl 0x10(%rdi), %eax
movl %eax, -0x2c(%rbp)
movq (%rdi), %r14
movq 0x8(%rdi), %rax
movq %rax, -0x40(%rbp)
movzbl (%r14), %r15d
xorl %r12d, %r12d
movl $0x1, %eax
movl %r12d, %ecx
shll $0x8, %ecx
movzbl (%r14,%rax), %r12d
orl %ecx, %r12d
incq %rax
cmpq $0x5, %rax
jne 0x2534
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
addq %rax, %rdx
leaq -0x60(%rbp), %rdi
movq %rax, %rsi
callq 0x387a
leaq -0xd0(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0xe0(%rbp), %rdi
callq 0x387a
leaq -0x80(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x2850
movq -0xe0(%rbp), %rdi
cmpq %r13, %rdi
je 0x25af
callq 0x21c0
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x25c1
callq 0x21c0
leaq -0xb0(%rbp), %r13
movq %r13, -0x10(%r13)
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %rdi
callq 0x387a
leaq -0xc0(%rbp), %rdi
movl %r12d, %esi
callq 0x296e
movl %eax, %ebx
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0x2605
callq 0x21c0
leaq -0x90(%rbp), %r13
movq %r13, -0x10(%r13)
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
addq %rsi, %rdx
leaq -0xa0(%rbp), %rdi
callq 0x387a
leaq -0xa0(%rbp), %rdi
movl %r15d, %esi
callq 0x29e4
movq %rax, -0x38(%rbp)
movq -0xa0(%rbp), %rdi
cmpq %r13, %rdi
je 0x264b
callq 0x21c0
movl %ebx, %r13d
movl %ebx, %edi
movq -0x38(%rbp), %rsi
movl %r12d, %edx
callq 0x2a8e
movl %eax, %ebx
callq 0x20a0
cmpl $0x0, -0x2c(%rbp)
je 0x2744
testb $0x1, %al
cmovel %ebx, %r13d
movsd -0x40(%rbp), %xmm0
unpcklps 0x29a1(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x29a9(%rip), %xmm0     # 0x5030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0xf0(%rbp)
movl %r13d, -0x40(%rbp)
movl %r13d, %eax
imull %r15d, %eax
leal (%rax,%r15), %ecx
leal 0x28(,%rcx,4), %ebx
movl %ebx, %ecx
shrl $0x3, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movapd -0xf0(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2792
leal 0x28(,%rax,4), %r13d
cmpl %ebx, %r13d
jae 0x26f4
movq %r14, %rdi
movl %r13d, %esi
movl %r15d, %edx
callq 0x2b2c
testq %rax, %rax
je 0x2759
addl %r15d, %r13d
jmp 0x26d7
callq 0x20a0
orl $-0x4, %eax
imull %r15d, %eax
addl %eax, %r13d
movq %r14, %rdi
movl %r13d, %esi
movl %r15d, %edx
callq 0x2b2c
movq %rax, %rbx
movq %r14, %rdi
movl %r13d, %esi
movl %r15d, %edx
movq -0x38(%rbp), %rcx
callq 0x2bfe
movl -0x40(%rbp), %edi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x2a8e
movl %eax, %r13d
movq %rbx, -0x38(%rbp)
decl -0x2c(%rbp)
jne 0x269b
leaq 0x2ba6(%rip), %rdi      # 0x52f1
callq 0x2230
movl $0x0, -0x2c(%rbp)
jmp 0x276b
movq %r14, %rdi
movl %r13d, %esi
movl %r15d, %edx
movq -0x38(%rbp), %rcx
callq 0x2bfe
leaq -0x70(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x277d
callq 0x21c0
movl -0x2c(%rbp), %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28fc(%rip), %rdi      # 0x5095
leaq 0x28ba(%rip), %rsi      # 0x505a
leaq 0x2912(%rip), %rcx      # 0x50b9
movl $0x5e, %edx
callq 0x2160
jmp 0x27fe
jmp 0x27fe
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
jmp 0x27cd
jmp 0x27fe
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0x2801
callq 0x21c0
jmp 0x2801
jmp 0x27fe
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
cmpq %r13, %rdi
je 0x27f4
callq 0x21c0
jmp 0x27f4
movq %rax, %rbx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
jmp 0x2809
movq %rax, %rbx
leaq -0x70(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2813
callq 0x21c0
movq %rbx, %rdi
callq 0x2280
nop