// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc68, %rsp            # imm = 0xC68
movq %r9, (%rsp)
movl %r8d, %ebx
movl %ecx, %r14d
movq %rdx, %r12
movl %esi, %r15d
movq %rdi, 0x28(%rsp)
leaq 0x868(%rsp), %r13
movq %r13, %rdi
callq 0x2f20
pushq $0x1
popq %rbp
movq %r13, %rdi
movl %ebp, %esi
callq 0x4c81
movq %r13, %rdi
movl %r15d, 0x18(%rsp)
movl %r15d, %esi
callq 0x4c81
movq %r13, %rdi
movl %r14d, %esi
callq 0x4c81
movq %r13, %rdi
movl %ebx, 0xc(%rsp)
movl %ebx, %esi
callq 0x4c81
pushq $0x13
popq %rsi
movq %r13, %rdi
callq 0x4c81
movq %r13, %rdi
movl %ebp, %esi
callq 0x4c81
movq %r13, %rdi
movl 0xca0(%rsp), %ebx
movl %ebx, %esi
callq 0x4c81
movq %r13, %rdi
movq (%rsp), %rsi
movq %rbx, %rdx
callq 0x2f2c
movq %r13, %rdi
movl 0xcb0(%rsp), %ebx
movl %ebx, %esi
callq 0x4c81
movq %r13, %rdi
movq 0xca8(%rsp), %rsi
movq %rbx, %rdx
callq 0x2f2c
movq %r13, %rdi
movl 0xcc0(%rsp), %ebx
movl %ebx, %esi
callq 0x4c81
movq %r13, %rdi
movq 0xcb8(%rsp), %rsi
movq %rbx, %rdx
callq 0x2f2c
movq %r13, %rdi
movl 0xcd0(%rsp), %ebx
movl %ebx, %esi
callq 0x4c81
movq %r13, %rdi
movq 0xcc8(%rsp), %rsi
movq %rbx, %rdx
callq 0x2f2c
leaq 0x460(%rsp), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x471f
andq $0x0, 0x40(%rbx)
leaq 0x40(%rsp), %r15
pushq $0x48
popq %rbp
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x4cae
movq %r12, %rdi
movq %r15, %rsi
callq 0x4d6c
movl $0x1, 0x40(%rbx)
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x4cae
leaq 0x400(%r12), %rdi
movq %r15, %rsi
callq 0x4d6c
pushq $0x48
popq %rsi
movq %rbx, %rdi
callq 0x25a4
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdi
callq 0x25a4
movl %r14d, %eax
andl $-0x4, %eax
movq %rax, 0x10(%rsp)
shrl $0x2, %r14d
movq %r14, 0x20(%rsp)
movq %r12, (%rsp)
leaq -0x400(%r12), %rax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
cmpl 0xc(%rsp), %r15d
je 0x4bf7
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq $0x4, %r14
je 0x4bef
movl %r15d, 0x440(%rsp)
movl %r14d, 0x444(%rsp)
movq 0x10(%rsp), %rax
movl %eax, 0x448(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x44c(%rsp)
andl $0x0, 0x450(%rsp)
movl %r15d, %eax
orl %r14d, %eax
je 0x4a8c
andl $0x0, 0x454(%rsp)
xorl %ebx, %ebx
jmp 0x4aad
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x450(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x8a3c
pushq $0x2
popq %rax
movl %eax, %ebx
incq %r14
movq %r14, 0x30(%rsp)
imulq 0x20(%rsp), %r14
movl %ebp, 0x1c(%rsp)
addl %ebp, %ebx
movq %rbx, %r12
shlq $0xa, %r12
addq (%rsp), %r12
cmpq %r14, %rbx
jae 0x4bdd
movl 0x454(%rsp), %eax
movl %eax, %edx
andl $0x7f, %edx
jne 0x4b00
incl 0x450(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x8a3c
movl 0x454(%rsp), %eax
movl %eax, %edx
andl $0x7f, %edx
leal 0x1(%rax), %ecx
movl %ecx, 0x454(%rsp)
movl 0x448(%rsp), %ecx
movl 0x440(%rsp), %edi
movl 0x444(%rsp), %esi
movl %ecx, %r8d
shrl $0x2, %r8d
leal 0x1(%rsi), %ebp
andl $0x3, %ebp
imull %r8d, %ebp
testl %edi, %edi
pushq $0x3
popq %r9
cmovnel %r9d, %esi
cmovel %edi, %ebp
imull %r8d, %esi
leal (%rsi,%rax), %edi
decl %edi
movl %edx, %edx
movl 0x40(%rsp,%rdx,8), %edx
imulq %rdx, %rdx
shrq $0x20, %rdx
imulq %rdx, %rdi
shrq $0x20, %rdi
addl %esi, %eax
addl $-0x2, %eax
subq %rdi, %rax
addq %rax, %rbp
cmpq %rcx, %rbp
movl $0x0, %eax
cmovbl %eax, %ecx
subl %ecx, %ebp
shlq $0xa, %rbp
addq (%rsp), %rbp
testq %rbx, %rbx
movq %rbx, %rsi
cmoveq 0x10(%rsp), %rsi
shlq $0xa, %rsi
addq 0x38(%rsp), %rsi
movq %r13, %rdi
callq 0x4d76
movq %r13, %rdi
movq %rbp, %rsi
callq 0x4d8e
movq %r12, %rdi
movq %r13, %rsi
testl %r15d, %r15d
je 0x4bb6
callq 0x4d8e
jmp 0x4bbb
callq 0x4d76
movq %r13, %rdi
callq 0x4da6
movq %r12, %rdi
movq %r13, %rsi
callq 0x4d8e
incq %rbx
addq $0x400, %r12            # imm = 0x400
jmp 0x4acc
movl 0x1c(%rsp), %ebp
addl 0x20(%rsp), %ebp
movq 0x30(%rsp), %r14
jmp 0x4a3f
incl %r15d
jmp 0x4a2f
leaq 0x868(%rsp), %rdi
callq 0x5727
movq 0x10(%rsp), %rbx
leal -0x1(%rbx), %esi
shlq $0xa, %rsi
addq (%rsp), %rsi
leaq 0x460(%rsp), %rdi
movl $0x80, %edx
callq 0x47c0
shll $0x7, %ebx
movq %rbx, %rdx
xorl %eax, %eax
movq (%rsp), %rcx
cmpq %rax, %rdx
je 0x4c44
movq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x4c32
leaq 0x460(%rsp), %rbx
movq 0x28(%rsp), %rdi
movl 0x18(%rsp), %esi
movq %rbx, %rdx
movl $0x400, %ecx            # imm = 0x400
callq 0x4cae
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0x25a4
addq $0xc68, %rsp            # imm = 0xC68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq