// File: yoavweiss[P]cache-digests-cuckoo/cuckoo.cpp
// Function: Cuckoo::writeFingerprint(unsigned char*, unsigned int, unsigned int, unsigned long)
void Cuckoo::writeFingerprint(unsigned char* hash, unsigned positionInBits, unsigned fingerprintSizeInBits, unsigned long fingerprintValue) {
    const float Bits = 8.0;
    unsigned endPositionInBits = positionInBits + fingerprintSizeInBits;
    unsigned startPositionInBytes = floor((float)positionInBits / Bits);
    unsigned endPositionInBytes = ceil((float)endPositionInBits / Bits);
    unsigned lengthInBytes = endPositionInBytes - startPositionInBytes;
    unsigned long hashValue = bigEndianRead((const unsigned char *)(hash), startPositionInBytes, lengthInBytes);
    unsigned extraBitsAtStart = positionInBits - startPositionInBytes * Bits;
    unsigned extraBitsAtEnd = endPositionInBytes * Bits - endPositionInBits;
    unsigned extraBytes = sizeof(hashValue) - lengthInBytes;
    unsigned long bitmap = -1;
    bitmap >>= extraBitsAtEnd + fingerprintSizeInBits;
    bitmap <<= extraBitsAtEnd + fingerprintSizeInBits;
    bitmap += (unsigned)(pow(2, extraBitsAtEnd)) - 1;
    hashValue &= bitmap;
    fingerprintValue <<= extraBitsAtEnd;
    hashValue |= fingerprintValue;
    bigEndianWrite(hash, startPositionInBytes, lengthInBytes, hashValue);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movl $0x41000000, -0x1c(%rbp) # imm = 0x41000000
movl -0xc(%rbp), %eax
movl -0x10(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x20(%rbp)
movl -0xc(%rbp), %eax
cvtsi2ss %rax, %xmm0
movss 0x2f88(%rip), %xmm1     # 0x6030
movss %xmm1, -0x54(%rbp)
mulss %xmm1, %xmm0
callq 0x4360
movss -0x54(%rbp), %xmm1
cvttss2si %xmm0, %rax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
cvtsi2ss %rax, %xmm0
mulss %xmm1, %xmm0
callq 0x4380
cvttss2si %xmm0, %rax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl -0x24(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x2c(%rbp), %eax
movl %eax, %edx
callq 0x2b50
movq %rax, -0x38(%rbp)
movl -0xc(%rbp), %eax
cvtsi2ss %rax, %xmm0
movl -0x24(%rbp), %eax
cvtsi2ss %rax, %xmm1
movss 0x2f20(%rip), %xmm2     # 0x6034
mulss %xmm2, %xmm1
subss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, -0x3c(%rbp)
movl -0x28(%rbp), %eax
cvtsi2ss %rax, %xmm0
movl -0x20(%rbp), %eax
cvtsi2ss %rax, %xmm1
mulss %xmm2, %xmm0
subss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, -0x40(%rbp)
movl -0x2c(%rbp), %ecx
movl $0x8, %eax
subl %ecx, %eax
movl %eax, -0x44(%rbp)
movq $-0x1, -0x50(%rbp)
movl -0x40(%rbp), %ecx
movl -0x10(%rbp), %eax
addl %eax, %ecx
movq -0x50(%rbp), %rax
shrq %cl, %rax
movq %rax, -0x50(%rbp)
movl -0x40(%rbp), %ecx
movl -0x10(%rbp), %eax
addl %eax, %ecx
movq -0x50(%rbp), %rax
shlq %cl, %rax
movq %rax, -0x50(%rbp)
movl -0x40(%rbp), %esi
movl $0x2, %edi
callq 0x41b0
cvttsd2si %xmm0, %rax
subl $0x1, %eax
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
andq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x40(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, %ecx
shlq %cl, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
orq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x2c(%rbp), %eax
movl %eax, %edx
movq -0x38(%rbp), %rcx
callq 0x2600
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)