// File: /workspace/github_cpp_makefile_star2_O3/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 $0xca8, %rsp            # imm = 0xCA8
movq %r9, 0x28(%rsp)
movl %r8d, %r14d
movl %ecx, %ebx
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, 0x40(%rsp)
movaps 0x85ec(%rip), %xmm0     # 0xd0c0
leaq 0x4a0(%rsp), %r13
movaps %xmm0, (%r13)
movaps 0x8608(%rip), %xmm0     # 0xd0f0
movaps %xmm0, 0x30(%r13)
movaps 0x85ec(%rip), %xmm0     # 0xd0e0
movaps %xmm0, 0x20(%r13)
movaps 0x85d0(%rip), %xmm0     # 0xd0d0
movaps %xmm0, 0x10(%r13)
movabsq $0x6a09e667f2bdc948, %rax # imm = 0x6A09E667F2BDC948
movq %rax, (%r13)
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 0x503b
movq %r13, %rdi
movl %ebp, 0x34(%rsp)
movl %ebp, %esi
callq 0x503b
movq %r13, %rdi
movq %rbx, %rbp
movl %ebx, %esi
callq 0x503b
movq %r13, %rdi
movl %r14d, 0xc(%rsp)
movl %r14d, %esi
callq 0x503b
movq %r13, %rdi
movl $0x13, %esi
callq 0x503b
movq %r13, %rdi
movl $0x1, %esi
callq 0x503b
movq %r13, %rdi
movl 0xce0(%rsp), %ebx
movl %ebx, %esi
callq 0x503b
movq %r13, %rdi
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
callq 0x2f4e
movq %r13, %rdi
movl 0xcf0(%rsp), %ebx
movl %ebx, %esi
callq 0x503b
movq %r13, %rdi
movq 0xce8(%rsp), %rsi
movq %rbx, %rdx
callq 0x2f4e
movq %r13, %rdi
movl 0xd00(%rsp), %ebx
movl %ebx, %esi
callq 0x503b
movq %r13, %rdi
movq 0xcf8(%rsp), %rsi
movq %rbx, %rdx
callq 0x2f4e
movq %r13, %rdi
movl 0xd10(%rsp), %ebx
movl %ebx, %esi
callq 0x503b
movq %r13, %rdi
movq 0xd08(%rsp), %rsi
movq %rbx, %rdx
callq 0x2f4e
leaq 0x8a0(%rsp), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x47a9
movq %r15, 0x40(%r14)
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdx
movl $0x48, %ecx
callq 0x5062
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2170
movl $0x1, 0x40(%r14)
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdx
movl $0x48, %ecx
callq 0x5062
leaq 0x400(%r12), %rdi
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rsi
callq 0x2170
movb $0x0, 0x8a0(%rsp,%r15)
incq %r15
cmpq $0x48, %r15
jne 0x4c85
xorl %eax, %eax
movb $0x0, 0x80(%rsp,%rax)
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x4c99
movl %ebp, %ebx
andl $-0x4, %ebx
cmpl $0x0, 0xc(%rsp)
je 0x4fad
shrl $0x2, %ebp
leaq -0x400(%r12), %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 %rbx, 0x50(%rsp)
movq %r12, 0x38(%rsp)
movq 0x38(%rsp), %r13
movq %r12, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
xorl %edx, %edx
movl 0x8(%rsp), %ecx
movl %ecx, 0x480(%rsp)
movl %edx, 0x484(%rsp)
movl %ebx, 0x488(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x48c(%rsp)
movl $0x0, 0x490(%rsp)
movl %ecx, %eax
movq %rdx, %rbx
orl %edx, %eax
je 0x4d50
movl $0x0, 0x494(%rsp)
xorl %r12d, %r12d
jmp 0x4d75
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x490(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x9728
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 0x4f62
movq 0x10(%rsp), %rax
imulq %rax, %rbx
movl %r12d, %r15d
addq %r15, %rbx
movq %rbx, 0x78(%rsp)
addq 0x18(%rsp), %r15
subl %r12d, %eax
movq %rax, 0x70(%rsp)
movl 0x494(%rsp), %eax
shll $0xa, %r12d
addq 0x20(%rsp), %r12
xorl %r14d, %r14d
movl %eax, %edx
andl $0x7f, %edx
jne 0x4df4
incl 0x490(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x9728
movl 0x494(%rsp), %eax
movl %eax, %edx
andl $0x7f, %edx
movl %eax, %ecx
movq 0x78(%rsp), %rax
leaq (%rax,%r14), %rbx
shlq $0xa, %rbx
addq %r13, %rbx
leal 0x1(%rcx), %eax
movl %eax, 0x28(%rsp)
movl %eax, 0x494(%rsp)
movl 0x488(%rsp), %eax
movl 0x480(%rsp), %edi
movl 0x484(%rsp), %esi
movl %eax, %r8d
shrl $0x2, %r8d
leal 0x1(%rsi), %ebp
andl $0x3, %ebp
imull %r8d, %ebp
testl %edi, %edi
cmovel %edi, %ebp
movl $0x3, %edi
cmovnel %edi, %esi
imull %r8d, %esi
leal (%rsi,%rcx), %edi
decl %edi
movl %edx, %edx
movl 0x80(%rsp,%rdx,8), %edx
imulq %rdx, %rdx
shrq $0x20, %rdx
imulq %rdx, %rdi
addl %esi, %ecx
addl $-0x2, %ecx
shrq $0x20, %rdi
subq %rdi, %rcx
addq %rcx, %rbp
cmpq %rax, %rbp
movl $0x0, %ecx
cmovbl %ecx, %eax
subl %eax, %ebp
testq %r15, %r15
movq %r15, %rsi
cmoveq 0x60(%rsp), %rsi
shlq $0xa, %rsi
addq 0x68(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
leaq 0x4a0(%rsp), %rdi
callq 0x2170
shlq $0xa, %rbp
addq %r13, %rbp
xorl %eax, %eax
movups (%rbp,%rax,8), %xmm0
movups 0x4a0(%rsp,%rax,8), %xmm1
xorps %xmm0, %xmm1
movups %xmm1, 0x4a0(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x80, %rax
jne 0x4eb1
cmpl $0x0, 0x8(%rsp)
je 0x4f01
xorl %eax, %eax
movups 0x4a0(%rsp,%rax,8), %xmm0
movups (%r12,%rax,8), %xmm1
xorps %xmm0, %xmm1
movups %xmm1, (%r12,%rax,8)
addq $0x2, %rax
cmpq $0x80, %rax
jne 0x4ede
jmp 0x4f16
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
leaq 0x4a0(%rsp), %rsi
callq 0x2170
leaq 0x4a0(%rsp), %rdi
callq 0x52a4
xorl %eax, %eax
movups 0x4a0(%rsp,%rax,8), %xmm0
movups (%r12,%rax,8), %xmm1
xorps %xmm0, %xmm1
movups %xmm1, (%r12,%rax,8)
addq $0x2, %rax
cmpq $0x80, %rax
jne 0x4f25
incq %r15
incq %r14
addq $0x400, %r12            # imm = 0x400
cmpq 0x70(%rsp), %r14
movl 0x28(%rsp), %eax
jne 0x4dcd
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 0x4d08
movl 0x8(%rsp), %eax
incl %eax
movl %eax, 0x8(%rsp)
cmpl 0xc(%rsp), %eax
movq 0x38(%rsp), %r12
jne 0x4cf8
xorl %eax, %eax
movq $0x0, 0x4a0(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x4faf
leal -0x1(%rbx), %esi
shlq $0xa, %rsi
addq %r12, %rsi
leaq 0x8a0(%rsp), %rdi
movl $0x80, %edx
callq 0x4874
shll $0x7, %ebx
je 0x4ff9
xorl %eax, %eax
movq $0x0, (%r12,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x4fe9
leaq 0x8a0(%rsp), %rdx
movq 0x40(%rsp), %rdi
movl 0x34(%rsp), %esi
movl $0x400, %ecx            # imm = 0x400
callq 0x5062
xorl %eax, %eax
movb $0x0, 0x8a0(%rsp,%rax)
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x5016
addq $0xca8, %rsp            # imm = 0xCA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq