// File: occivink[P]pyct/pyct.cpp
// Function: hash_password(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned long, unsigned long)
Hash hash_password(string& password, const string& salt, u64 iterations, u64 work_area_size) {
    if (salt.size() < 8)
        throw invalid_argument{"Salt too small"};
    if (work_area_size < 8*1024)
        throw invalid_argument{"Work area is too small"};
    if (iterations == 0)
        throw invalid_argument{"Need at least one iteration"};

    auto rem = work_area_size % 1024;
    if (rem > 0)
        work_area_size = work_area_size - rem + 1024;
    u64 nb_blocks = work_area_size / 1024;

    vector<u8> work_area(work_area_size);

    Hash hash;
    crypto_argon2i(hash.data(), hash.size(),
                   work_area.data(), nb_blocks,
                   iterations,
                   reinterpret_cast<const u8*>(&password[0]), password.size(),
                   reinterpret_cast<const u8*>(&salt[0]), salt.size());

    crypto_wipe(&password[0], password.size());
    password = {};
    return hash;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, -0x88(%rbp)
movq %rdi, -0x80(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x31e0
cmpq $0x8, %rax
jae 0x14742
movl $0x10, %edi
callq 0x3140
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
leaq 0x8a77(%rip), %rsi      # 0x1d17b
callq 0x3530
jmp 0x1470b
movq -0x90(%rbp), %rdi
movq 0xf8b7(%rip), %rsi      # 0x23fd0
movq 0xf8a0(%rip), %rdx      # 0x23fc0
callq 0x34c0
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x3200
jmp 0x149df
cmpq $0x2000, -0x28(%rbp)    # imm = 0x2000
jae 0x147a8
movl $0x10, %edi
callq 0x3140
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
leaq 0x8a20(%rip), %rsi      # 0x1d18a
callq 0x3530
jmp 0x14771
movq -0x98(%rbp), %rdi
movq 0xf851(%rip), %rsi      # 0x23fd0
movq 0xf83a(%rip), %rdx      # 0x23fc0
callq 0x34c0
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x3200
jmp 0x149df
cmpq $0x0, -0x20(%rbp)
jne 0x1480b
movl $0x10, %edi
callq 0x3140
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
leaq 0x89d4(%rip), %rsi      # 0x1d1a1
callq 0x3530
jmp 0x147d4
movq -0xa0(%rbp), %rdi
movq 0xf7ee(%rip), %rsi      # 0x23fd0
movq 0xf7d7(%rip), %rdx      # 0x23fc0
callq 0x34c0
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x3200
jmp 0x149df
movq -0x28(%rbp), %rax
andq $0x3ff, %rax            # imm = 0x3FF
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jbe 0x14832
movq -0x28(%rbp), %rax
subq -0x40(%rbp), %rax
addq $0x400, %rax            # imm = 0x400
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
shrq $0xa, %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x172a0
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x172c0
jmp 0x14872
leaq -0x61(%rbp), %rdi
callq 0x17330
movq -0x88(%rbp), %rdi
callq 0x17350
movq -0x88(%rbp), %rdi
movq %rax, -0xd8(%rbp)
callq 0x17370
movl %eax, -0xcc(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x17380
movq %rax, -0xc8(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0xc0(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0xbc(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3560
movq %rax, -0xb8(%rbp)
jmp 0x148d8
movq -0x10(%rbp), %rdi
callq 0x31e0
movl %eax, -0xdc(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3360
movq %rax, -0xe8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x31e0
movq -0xe8(%rbp), %r11
movl -0xdc(%rbp), %r10d
movq -0xd8(%rbp), %rdi
movl -0xcc(%rbp), %esi
movq -0xc8(%rbp), %rdx
movl -0xc0(%rbp), %ecx
movl -0xbc(%rbp), %r8d
movq -0xb8(%rbp), %r9
movl %eax, %ebx
movq %rsp, %rax
movl %ebx, 0x10(%rax)
movq %r11, 0x8(%rax)
movl %r10d, (%rax)
callq 0xd5d0
jmp 0x14950
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3560
movq %rax, -0xf0(%rbp)
jmp 0x14966
movq -0x10(%rbp), %rdi
callq 0x31e0
movq -0xf0(%rbp), %rdi
movq %rax, %rsi
callq 0x3890
jmp 0x14980
leaq -0x78(%rbp), %rdi
callq 0x173a0
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x32b0
jmp 0x1499c
leaq -0x60(%rbp), %rdi
callq 0x17210
movq -0x80(%rbp), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x17330
jmp 0x149df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x17210
movq -0x30(%rbp), %rdi
callq 0x34f0
nopl (%rax,%rax)