// File: chahk0129[P]hash_bench/./index/cuckoo_hash.h
// Function: CuckooHash<char [32]>::Update(char (&) [32], long)
bool CuckooHash<Key_t>::Update(Key_t& key, Value_t value) {
    size_t f_hash, s_hash;
    if constexpr(sizeof(Key_t) > 8){
        f_hash = hash_funcs[0](key, sizeof(Key_t), _seed);
        s_hash = hash_funcs[1](key, sizeof(Key_t), _seed);
    }
    else{
        f_hash = hash_funcs[0](&key, sizeof(Key_t), _seed);
        s_hash = hash_funcs[1](&key, sizeof(Key_t), _seed);
    }

RETRY:
    while(resizing_lock){
        asm("nop");
    }

    auto f_idx = f_hash % capacity;
    auto s_idx = s_hash % capacity;

    { // try first hashing
        unique_lock<shared_mutex> lock(mutex[f_idx/locksize]);
        if constexpr(sizeof(Key_t) > 8){
            if(memcmp(table[f_idx].key, key, sizeof(Key_t)) == 0){
		memcpy(&table[f_idx].value, &value, sizeof(Value_t));
                return true;
            }
        }
        else{
            if(memcmp(&table[f_idx].key, &key, sizeof(Key_t)) == 0){
		memcpy(&table[f_idx].value, &value, sizeof(Value_t));
                return true;
            }
        }
    }

    { // try second hashing
        unique_lock<shared_mutex> lock(mutex[s_idx/locksize]);
        if constexpr(sizeof(Key_t) > 8){
            if(memcmp(table[s_idx].key, key, sizeof(Key_t)) == 0){
		memcpy(&table[s_idx].value, &value, sizeof(Value_t));
                return true;
            }
        }
        else{
            if(memcmp(&table[s_idx].key, &key, sizeof(Key_t)) == 0){
		memcpy(&table[s_idx].value, &value, sizeof(Value_t));
                return true;
            }
        }
    }
                                                                           
    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x70(%rbp)
movq 0x3b51d(%rip), %rax     # 0x5a480
movq -0x18(%rbp), %rdi
movq 0x8(%rcx), %rdx
movl $0x20, %esi
callq *%rax
movq -0x70(%rbp), %rcx
movq %rax, -0x28(%rbp)
movq 0x3b507(%rip), %rax     # 0x5a488
movq -0x18(%rbp), %rdi
movq 0x8(%rcx), %rdx
movl $0x20, %esi
callq *%rax
movq %rax, -0x30(%rbp)
jmp 0x1ef96
movq -0x70(%rbp), %rax
cmpl $0x0, 0xc8(%rax)
je 0x1efa6
nop
jmp 0x1ef96
movq -0x70(%rbp), %rcx
movq -0x28(%rbp), %rax
xorl %edx, %edx
divq 0x30(%rcx)
movq %rdx, -0x38(%rbp)
movq -0x30(%rbp), %rax
xorl %edx, %edx
divq 0x30(%rcx)
movq %rdx, -0x40(%rbp)
movq 0xd0(%rcx), %rsi
movq -0x38(%rbp), %rax
movslq 0xdc(%rcx), %rcx
xorl %edx, %edx
divq %rcx
imulq $0x38, %rax, %rax
addq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x1dd90
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rdi
imulq $0x28, -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movl $0x20, %edx
callq 0x6190
cmpl $0x0, %eax
jne 0x1f035
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq -0x20(%rbp), %rcx
movq %rcx, 0x20(%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0x54(%rbp)
jmp 0x1f03c
movl $0x0, -0x54(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x1ddd0
movl -0x54(%rbp), %eax
testl %eax, %eax
je 0x1f053
jmp 0x1f04e
jmp 0x1f0e5
movq -0x70(%rbp), %rcx
movq 0xd0(%rcx), %rsi
movq -0x40(%rbp), %rax
movslq 0xdc(%rcx), %rcx
xorl %edx, %edx
divq %rcx
imulq $0x38, %rax, %rax
addq %rax, %rsi
leaq -0x68(%rbp), %rdi
callq 0x1dd90
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rdi
imulq $0x28, -0x40(%rbp), %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movl $0x20, %edx
callq 0x6190
cmpl $0x0, %eax
jne 0x1f0c6
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq -0x20(%rbp), %rcx
movq %rcx, 0x20(%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0x54(%rbp)
jmp 0x1f0cd
movl $0x0, -0x54(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x1ddd0
movl -0x54(%rbp), %eax
testl %eax, %eax
je 0x1f0e1
jmp 0x1f0df
jmp 0x1f0e5
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq