// File: /workspace/github_cpp_makefile_star2_O1/occivink[P]pyct/monocypher.cpp
// Function: crypto_argon2i_general
void crypto_argon2i_general(u8       *hash,      u32 hash_size,
                            void     *work_area, u32 nb_blocks,
                            u32 nb_iterations,
                            const u8 *password,  u32 password_size,
                            const u8 *salt,      u32 salt_size,
                            const u8 *key,       u32 key_size,
                            const u8 *ad,        u32 ad_size)
{
    // work area seen as blocks (must be suitably aligned)
    block *blocks = (block*)work_area;
    {
        crypto_blake2b_ctx ctx;
        crypto_blake2b_init(&ctx);

        blake_update_32      (&ctx, 1            ); // p: number of threads
        blake_update_32      (&ctx, hash_size    );
        blake_update_32      (&ctx, nb_blocks    );
        blake_update_32      (&ctx, nb_iterations);
        blake_update_32      (&ctx, 0x13         ); // v: version number
        blake_update_32      (&ctx, 1            ); // y: Argon2i
        blake_update_32      (&ctx,           password_size);
        crypto_blake2b_update(&ctx, password, password_size);
        blake_update_32      (&ctx,           salt_size);
        crypto_blake2b_update(&ctx, salt,     salt_size);
        blake_update_32      (&ctx,           key_size);
        crypto_blake2b_update(&ctx, key,      key_size);
        blake_update_32      (&ctx,           ad_size);
        crypto_blake2b_update(&ctx, ad,       ad_size);

        u8 initial_hash[72]; // 64 bytes plus 2 words for future hashes
        crypto_blake2b_final(&ctx, initial_hash);

        // fill first 2 blocks
        u8 hash_area[1024];
        store32_le(initial_hash + 64, 0); // first  additional word
        store32_le(initial_hash + 68, 0); // second additional word
        extended_hash(hash_area, 1024, initial_hash, 72);
        load_block(blocks, hash_area);

        store32_le(initial_hash + 64, 1); // slight modification
        extended_hash(hash_area, 1024, initial_hash, 72);
        load_block(blocks + 1, hash_area);

        WIPE_BUFFER(initial_hash);
        WIPE_BUFFER(hash_area);
    }

    // Actual number of blocks
    nb_blocks -= nb_blocks & 3; // round down to 4 p (p == 1 thread)
    const u32 segment_size = nb_blocks >> 2;

    // fill (then re-fill) the rest of the blocks
    block tmp;
    gidx_ctx ctx; // public information, no need to wipe
    FOR_T (u32, pass_number, 0, nb_iterations) {
        int first_pass = pass_number == 0;

        FOR_T (u32, segment, 0, 4) {
            gidx_init(&ctx, pass_number, segment, nb_blocks, nb_iterations);

            // On the first segment of the first pass,
            // blocks 0 and 1 are already filled.
            // We use the offset to skip them.
            u32 start_offset  = first_pass && segment == 0 ? 2 : 0;
            u32 segment_start = segment * segment_size + start_offset;
            u32 segment_end   = (segment + 1) * segment_size;
            FOR_T (u32, current_block, segment_start, segment_end) {
                block *reference = blocks + gidx_next(&ctx);
                block *current   = blocks + current_block;
                block *previous  = current_block == 0
                                 ? blocks + nb_blocks - 1
                                 : blocks + current_block - 1;
                // Apply compression function G,
                // And copy it (or XOR it) to the current block.
                copy_block(&tmp, previous);
                xor_block (&tmp, reference);
                if (first_pass) { copy_block(current, &tmp); }
                else            { xor_block (current, &tmp); }
                g_rounds  (&tmp);
                xor_block (current, &tmp);
            }
        }
    }
    wipe_block(&tmp);
    u8 final_block[1024];
    store_block(final_block, blocks + (nb_blocks - 1));

    // wipe work area
    volatile u64 *p = (u64*)work_area;
    ZERO(p, 128 * nb_blocks);

    // hash the very last block with H' into the output hash
    extended_hash(hash, hash_size, final_block, 1024);
    WIPE_BUFFER(final_block);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xcb8, %rsp            # imm = 0xCB8
movq %r9, 0x28(%rsp)
movl %r8d, %r14d
movl %ecx, %ebx
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, 0x40(%rsp)
movaps 0x85b7(%rip), %xmm0     # 0xd0a0
leaq 0x4b0(%rsp), %r13
movaps %xmm0, (%r13)
movaps 0x85d3(%rip), %xmm0     # 0xd0d0
movaps %xmm0, 0x30(%r13)
movaps 0x85b7(%rip), %xmm0     # 0xd0c0
movaps %xmm0, 0x20(%r13)
movaps 0x859b(%rip), %xmm0     # 0xd0b0
movaps %xmm0, 0x10(%r13)
xorl $0x1010040, (%r13)      # imm = 0x1010040
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%r13)
movq $0x40, 0xd8(%r13)
xorl %r15d, %r15d
movq %r15, 0xd0(%r13)
movq %r13, %rdi
movl $0x1, %esi
callq 0x5077
movq %r13, %rdi
movl %ebp, 0x34(%rsp)
movl %ebp, %esi
callq 0x5077
movq %r13, %rdi
movq %rbx, %rbp
movl %ebx, %esi
callq 0x5077
movq %r13, %rdi
movl %r14d, 0xc(%rsp)
movl %r14d, %esi
callq 0x5077
movq %r13, %rdi
movl $0x13, %esi
callq 0x5077
movq %r13, %rdi
movl $0x1, %esi
callq 0x5077
movq %r13, %rdi
movl 0xcf0(%rsp), %ebx
movl %ebx, %esi
callq 0x5077
movq %r13, %rdi
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
movq %r12, %r14
callq 0x300e
movq %r13, %rdi
movl 0xd00(%rsp), %ebx
movl %ebx, %esi
callq 0x5077
movq %r13, %rdi
movq 0xcf8(%rsp), %rsi
movq %rbx, %rdx
callq 0x300e
movq %r13, %rdi
movl 0xd10(%rsp), %ebx
movl %ebx, %esi
callq 0x5077
movq %r13, %rdi
movq 0xd08(%rsp), %rsi
movq %rbx, %rdx
callq 0x300e
movq %r13, %rdi
movl 0xd20(%rsp), %ebx
movl %ebx, %esi
callq 0x5077
movq %r13, %rdi
movq 0xd18(%rsp), %rsi
movq %rbx, %rdx
callq 0x300e
leaq 0x8b0(%rsp), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x48ff
movq %r15, 0x40(%rbx)
leaq 0x90(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
movl $0x48, %ecx
callq 0x509e
leaq (%rsp,%r15), %rdi
addq $0x90, %rdi
callq 0x9550
movq %rax, (%r14,%r15)
addq $0x8, %r15
cmpq $0x400, %r15            # imm = 0x400
jne 0x4c52
leaq 0x8b0(%rsp), %rdx
movl $0x1, 0x40(%rdx)
leaq 0x90(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x48, %ecx
callq 0x509e
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x90, %rdi
callq 0x9550
movq %rax, 0x400(%r14,%rbx)
addq $0x8, %rbx
cmpq $0x400, %rbx            # imm = 0x400
jne 0x4c9b
xorl %eax, %eax
movb $0x0, 0x8b0(%rsp,%rax)
incq %rax
cmpq $0x48, %rax
jne 0x4cc2
xorl %eax, %eax
movb $0x0, 0x90(%rsp,%rax)
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x4cd5
movl %ebp, %ebx
andl $-0x4, %ebx
cmpl $0x0, 0xc(%rsp)
je 0x4fe9
shrl $0x2, %ebp
leaq -0x400(%r14), %rax
movq %rax, 0x68(%rsp)
movl %ebx, %eax
movq %rax, 0x60(%rsp)
movq %rbp, 0x10(%rsp)
shlq $0xa, %rbp
movq %rbp, 0x48(%rsp)
movl $0x0, 0x8(%rsp)
movq %r14, 0x70(%rsp)
movq %rbx, 0x50(%rsp)
movq %r14, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
xorl %edx, %edx
movl 0x8(%rsp), %ecx
movl %ecx, 0x490(%rsp)
movl %edx, 0x494(%rsp)
movl %ebx, 0x498(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x49c(%rsp)
movl $0x0, 0x4a0(%rsp)
movl %ecx, %eax
movq %rdx, %rbx
orl %edx, %eax
je 0x4d86
movl $0x0, 0x4a4(%rsp)
xorl %r12d, %r12d
jmp 0x4dab
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x4a0(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x9554
movl $0x2, %r12d
movq 0x10(%rsp), %rdx
movl %edx, %eax
movq %rbx, %rcx
imull %ecx, %eax
addl %r12d, %eax
leaq 0x1(%rbx), %rcx
movq %rcx, 0x58(%rsp)
imull %edx, %ecx
cmpl %ecx, %eax
jae 0x4fa3
movq 0x10(%rsp), %rax
imulq %rax, %rbx
movl %r12d, %esi
addq %rsi, %rbx
movq %rbx, 0x80(%rsp)
addq 0x18(%rsp), %rsi
subl %r12d, %eax
movq %rax, 0x78(%rsp)
shll $0xa, %r12d
addq 0x20(%rsp), %r12
xorl %r15d, %r15d
testb $0x7f, 0x4a4(%rsp)
jne 0x4e23
incl 0x4a0(%rsp)
leaq 0x90(%rsp), %rdi
movq %rsi, %rbx
callq 0x9554
movq %rbx, %rsi
movq 0x80(%rsp), %rax
movq %r15, 0x38(%rsp)
addq %r15, %rax
shlq $0xa, %rax
addq %r14, %rax
movq %rax, 0x88(%rsp)
movl 0x4a4(%rsp), %ebp
leal 0x1(%rbp), %eax
movl %eax, 0x4a4(%rsp)
movl 0x498(%rsp), %r13d
movl 0x490(%rsp), %ebx
movl 0x494(%rsp), %r14d
movl %ebp, %eax
andl $0x7f, %eax
movl 0x90(%rsp,%rax,8), %r15d
testq %rsi, %rsi
movq %rsi, 0x28(%rsp)
cmoveq 0x60(%rsp), %rsi
shlq $0xa, %rsi
addq 0x68(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
leaq 0x4b0(%rsp), %rdi
callq 0x2170
movl %r13d, %ecx
shrl $0x2, %ecx
leal 0x1(%r14), %eax
andl $0x3, %eax
imull %ecx, %eax
testl %ebx, %ebx
movl $0x3, %edx
cmovnel %edx, %r14d
cmovel %ebx, %eax
imull %ecx, %r14d
leal (%r14,%rbp), %ecx
addl $-0x2, %ecx
leal (%r14,%rbp), %edx
decl %edx
movq 0x70(%rsp), %r14
imulq %r15, %r15
shrq $0x20, %r15
imulq %rdx, %r15
shrq $0x20, %r15
subq %r15, %rcx
addq %rcx, %rax
cmpq %r13, %rax
movl $0x0, %ecx
cmovbl %ecx, %r13d
subl %r13d, %eax
shlq $0xa, %rax
addq %r14, %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
xorq %rdx, 0x4b0(%rsp,%rcx,8)
incq %rcx
cmpq $0x80, %rcx
jne 0x4f02
cmpl $0x0, 0x8(%rsp)
je 0x4f41
xorl %eax, %eax
movq 0x38(%rsp), %r15
movq 0x4b0(%rsp,%rax,8), %rcx
xorq %rcx, (%r12,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x4f28
jmp 0x4f60
movl $0x400, %edx            # imm = 0x400
movq 0x88(%rsp), %rdi
leaq 0x4b0(%rsp), %rsi
callq 0x2170
movq 0x38(%rsp), %r15
leaq 0x4b0(%rsp), %rdi
callq 0x51d2
xorl %eax, %eax
movq 0x4b0(%rsp,%rax,8), %rcx
xorq %rcx, (%r12,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x4f6f
movq 0x28(%rsp), %rsi
incq %rsi
incq %r15
addq $0x400, %r12            # imm = 0x400
cmpq 0x78(%rsp), %r15
jne 0x4dff
movq 0x18(%rsp), %rax
addq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
addq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rdx
cmpq $0x4, %rdx
movq 0x50(%rsp), %rbx
jne 0x4d3e
movl 0x8(%rsp), %eax
incl %eax
movl %eax, 0x8(%rsp)
cmpl 0xc(%rsp), %eax
jne 0x4d2e
xorl %eax, %eax
movq $0x0, 0x4b0(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x4feb
leal -0x1(%rbx), %esi
shlq $0xa, %rsi
addq %r14, %rsi
leaq 0x8b0(%rsp), %rdi
movl $0x80, %edx
callq 0x49ca
shll $0x7, %ebx
je 0x5035
xorl %eax, %eax
movq $0x0, (%r14,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x5025
leaq 0x8b0(%rsp), %rdx
movq 0x40(%rsp), %rdi
movl 0x34(%rsp), %esi
movl $0x400, %ecx            # imm = 0x400
callq 0x509e
xorl %eax, %eax
movb $0x0, 0x8b0(%rsp,%rax)
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x5052
addq $0xcb8, %rsp            # imm = 0xCB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq