// File: /workspace/github_cpp_makefile_star2_O1/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 $0xf8, %rsp
movq %rdx, %r12
movl 0x10(%rdi), %eax
movl %eax, -0x2c(%rbp)
movq (%rdi), %rdx
movq 0x8(%rdi), %rax
movq %rax, -0x40(%rbp)
movzbl (%rdx), %eax
movq %rax, -0x38(%rbp)
movl $0x1, %eax
xorl %r15d, %r15d
movq %r15, %rcx
shlq $0x8, %rcx
movzbl (%rdx,%rax), %r15d
orq %rcx, %r15
incq %rax
cmpq $0x5, %rax
jne 0x2541
movq %rdx, -0x50(%rbp)
leaq -0xf8(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
addq %rax, %rdx
leaq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x3980
leaq -0xd8(%rbp), %r14
movq %r14, -0x10(%r14)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq -0xe8(%rbp), %rdi
callq 0x3980
leaq -0x88(%rbp), %rdi
leaq -0x108(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq 0x28f4
movq -0xe8(%rbp), %rdi
cmpq %r14, %rdi
je 0x25cf
callq 0x21c0
movq -0x108(%rbp), %rdi
cmpq %rbx, %rdi
je 0x25e0
callq 0x21c0
leaq -0xb8(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0xc8(%rbp), %rdi
callq 0x3980
leaq -0xc8(%rbp), %rdi
movl %r15d, %esi
callq 0x2a16
movl %eax, %r14d
movq -0xc8(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2628
callq 0x21c0
leaq -0x98(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0xa8(%rbp), %rdi
callq 0x3980
leaq -0xa8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2a8c
movq %rax, %r13
movq -0xa8(%rbp), %rdi
cmpq %rbx, %rdi
je 0x2671
callq 0x21c0
movl %r14d, %edi
movq %r13, %rsi
movl %r15d, %edx
callq 0x2b36
movl %eax, %r12d
callq 0x20a0
testb $0x1, %al
cmovel %r12d, %r14d
movsd -0x40(%rbp), %xmm0
unpcklps 0x2987(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x298f(%rip), %xmm0     # 0x5030
movq -0x38(%rbp), %r12
movl %r12d, %eax
movq %rax, -0x68(%rbp)
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x120(%rbp)
movl -0x2c(%rbp), %eax
movq %r15, -0x60(%rbp)
movq %r13, %r15
testl %eax, %eax
je 0x27f8
movl %eax, -0x2c(%rbp)
movq %r14, %rax
movq %r14, -0x40(%rbp)
imull -0x68(%rbp), %eax
leal (%rax,%r12), %ecx
leal 0x28(,%rcx,4), %r14d
movl %r14d, %ecx
shrl $0x3, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movapd -0x120(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2832
leal 0x28(,%rax,4), %r13d
cmpl %r14d, %r13d
setb %bl
jae 0x2773
movq -0x50(%rbp), %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x2bd4
testq %rax, %rax
je 0x27ce
movq -0x40(%rbp), %rax
movq %r12, %rdx
leal 0x1(,%rax,4), %r12d
imull %edx, %r12d
addl $0x28, %r12d
addl %edx, %r13d
cmpl %r14d, %r12d
setb %bl
jae 0x2773
movq -0x50(%rbp), %rdi
movl %r12d, %esi
movq -0x38(%rbp), %rdx
callq 0x2bd4
movq -0x38(%rbp), %rdx
addl %edx, %r12d
testq %rax, %rax
jne 0x274a
jmp 0x27d1
movq %r15, -0x58(%rbp)
callq 0x20a0
orl $-0x4, %eax
movq -0x38(%rbp), %r15
imull %r15d, %eax
addl %eax, %r13d
movq -0x50(%rbp), %r14
movq %r14, %rdi
movl %r13d, %esi
movl %r15d, %edx
callq 0x2bd4
movq %rax, %r12
movq %r14, %rdi
movl %r13d, %esi
movl %r15d, %edx
movq -0x58(%rbp), %rcx
callq 0x2ca6
movq -0x60(%rbp), %rdx
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x2b36
movl %eax, %r14d
movl -0x2c(%rbp), %eax
decl %eax
movq %r12, %r15
jmp 0x27ea
movq %r12, %rdx
movq -0x50(%rbp), %rdi
movl %r13d, %esi
movq %r15, %rcx
callq 0x2ca6
movl -0x2c(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %r14
testb %bl, %bl
movq -0x38(%rbp), %r12
je 0x26ca
jmp 0x280b
leaq 0x2af2(%rip), %rdi      # 0x52f1
callq 0x2230
movl $0x0, -0x44(%rbp)
leaq -0x78(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x281d
callq 0x21c0
movl -0x44(%rbp), %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x285c(%rip), %rdi      # 0x5095
leaq 0x281a(%rip), %rsi      # 0x505a
leaq 0x2872(%rip), %rcx      # 0x50b9
movl $0x5e, %edx
callq 0x2160
jmp 0x28a2
movq %rax, %r15
movq -0xa8(%rbp), %rdi
jmp 0x286b
jmp 0x28a2
movq %rax, %r15
movq -0xc8(%rbp), %rdi
cmpq %rbx, %rdi
je 0x28a5
callq 0x21c0
jmp 0x28a5
jmp 0x28a2
movq %rax, %r15
movq -0xe8(%rbp), %rdi
cmpq %r14, %rdi
je 0x2892
callq 0x21c0
jmp 0x2892
movq %rax, %r15
movq -0x108(%rbp), %rdi
cmpq %rbx, %rdi
jne 0x28b2
jmp 0x28b7
jmp 0x28a2
movq %rax, %r15
leaq -0x78(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x28b7
callq 0x21c0
movq %r15, %rdi
callq 0x2280
nop