// File: 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
movq %rsp, %rbp
subq $0x11f0, %rsp           # imm = 0x11F0
movl 0x40(%rbp), %eax
movq 0x38(%rbp), %rax
movl 0x30(%rbp), %eax
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x4e40
leaq -0x110(%rbp), %rdi
movl $0x1, %esi
callq 0xa1e0
movl -0xc(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0xa1e0
movl -0x1c(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0xa1e0
movl -0x20(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0xa1e0
leaq -0x110(%rbp), %rdi
movl $0x13, %esi
callq 0xa1e0
leaq -0x110(%rbp), %rdi
movl $0x1, %esi
callq 0xa1e0
movl 0x10(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0xa1e0
movq -0x28(%rbp), %rsi
movl 0x10(%rbp), %eax
movl %eax, %edx
leaq -0x110(%rbp), %rdi
callq 0x4e70
movl 0x20(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0xa1e0
movq 0x18(%rbp), %rsi
movl 0x20(%rbp), %eax
movl %eax, %edx
leaq -0x110(%rbp), %rdi
callq 0x4e70
movl 0x30(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0xa1e0
movq 0x28(%rbp), %rsi
movl 0x30(%rbp), %eax
movl %eax, %edx
leaq -0x110(%rbp), %rdi
callq 0x4e70
movl 0x40(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0xa1e0
movq 0x38(%rbp), %rsi
movl 0x40(%rbp), %eax
movl %eax, %edx
leaq -0x110(%rbp), %rdi
callq 0x4e70
leaq -0x160(%rbp), %rsi
leaq -0x110(%rbp), %rdi
callq 0x9a20
leaq -0x160(%rbp), %rdi
addq $0x40, %rdi
xorl %esi, %esi
callq 0x42a0
leaq -0x160(%rbp), %rdi
addq $0x44, %rdi
xorl %esi, %esi
callq 0x42a0
leaq -0x560(%rbp), %rdi
leaq -0x160(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
movl $0x48, %ecx
callq 0xa230
movq -0x30(%rbp), %rdi
leaq -0x560(%rbp), %rsi
callq 0xa3a0
leaq -0x160(%rbp), %rdi
addq $0x40, %rdi
movl $0x1, %esi
callq 0x42a0
leaq -0x560(%rbp), %rdi
leaq -0x160(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
movl $0x48, %ecx
callq 0xa230
movq -0x30(%rbp), %rdi
addq $0x400, %rdi            # imm = 0x400
leaq -0x560(%rbp), %rsi
callq 0xa3a0
leaq -0x160(%rbp), %rdi
movl $0x48, %esi
callq 0x3890
leaq -0x560(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x3890
movl -0x1c(%rbp), %ecx
andl $0x3, %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shrl $0x2, %eax
movl %eax, -0x564(%rbp)
movl $0x0, -0xd84(%rbp)
movl -0xd84(%rbp), %eax
cmpl -0x20(%rbp), %eax
jae 0xa121
cmpl $0x0, -0xd84(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0xd88(%rbp)
movl $0x0, -0xd8c(%rbp)
cmpl $0x4, -0xd8c(%rbp)
jae 0xa10b
movl -0xd84(%rbp), %esi
movl -0xd8c(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
leaq -0xd80(%rbp), %rdi
callq 0xa3d0
xorl %eax, %eax
cmpl $0x0, -0xd88(%rbp)
movb %al, -0x11d1(%rbp)
je 0x9f6b
cmpl $0x0, -0xd8c(%rbp)
sete %al
movb %al, -0x11d1(%rbp)
movb -0x11d1(%rbp), %dl
xorl %eax, %eax
movl $0x2, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0xd90(%rbp)
movl -0xd8c(%rbp), %eax
imull -0x564(%rbp), %eax
addl -0xd90(%rbp), %eax
movl %eax, -0xd94(%rbp)
movl -0xd8c(%rbp), %eax
addl $0x1, %eax
imull -0x564(%rbp), %eax
movl %eax, -0xd98(%rbp)
movl -0xd94(%rbp), %eax
movl %eax, -0xd9c(%rbp)
movl -0xd9c(%rbp), %eax
cmpl -0xd98(%rbp), %eax
jae 0xa0f5
movq -0x30(%rbp), %rax
movq %rax, -0x11e0(%rbp)
leaq -0xd80(%rbp), %rdi
callq 0xa480
movl %eax, %ecx
movq -0x11e0(%rbp), %rax
movl %ecx, %ecx
shlq $0xa, %rcx
addq %rcx, %rax
movq %rax, -0xda8(%rbp)
movq -0x30(%rbp), %rax
movl -0xd9c(%rbp), %ecx
shlq $0xa, %rcx
addq %rcx, %rax
movq %rax, -0xdb0(%rbp)
cmpl $0x0, -0xd9c(%rbp)
jne 0xa03f
movq -0x30(%rbp), %rax
movl -0x1c(%rbp), %ecx
shlq $0xa, %rcx
addq %rcx, %rax
addq $-0x400, %rax           # imm = 0xFC00
movq %rax, -0x11e8(%rbp)
jmp 0xa05d
movq -0x30(%rbp), %rax
movl -0xd9c(%rbp), %ecx
shlq $0xa, %rcx
addq %rcx, %rax
addq $-0x400, %rax           # imm = 0xFC00
movq %rax, -0x11e8(%rbp)
movq -0x11e8(%rbp), %rax
movq %rax, -0xdb8(%rbp)
movq -0xdb8(%rbp), %rsi
leaq -0x968(%rbp), %rdi
callq 0xa610
movq -0xda8(%rbp), %rsi
leaq -0x968(%rbp), %rdi
callq 0xa660
cmpl $0x0, -0xd88(%rbp)
je 0xa0af
movq -0xdb0(%rbp), %rdi
leaq -0x968(%rbp), %rsi
callq 0xa610
jmp 0xa0c2
movq -0xdb0(%rbp), %rdi
leaq -0x968(%rbp), %rsi
callq 0xa660
leaq -0x968(%rbp), %rdi
callq 0xa6b0
movq -0xdb0(%rbp), %rdi
leaq -0x968(%rbp), %rsi
callq 0xa660
movl -0xd9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd9c(%rbp)
jmp 0x9fbf
jmp 0xa0f7
movl -0xd8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8c(%rbp)
jmp 0x9f1e
jmp 0xa10d
movl -0xd84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd84(%rbp)
jmp 0x9ef0
leaq -0x968(%rbp), %rdi
callq 0xd550
leaq -0x11c0(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
shlq $0xa, %rax
addq %rax, %rsi
callq 0xd5a0
movq -0x18(%rbp), %rax
movq %rax, -0x11c8(%rbp)
movq $0x0, -0x11d0(%rbp)
movq -0x11d0(%rbp), %rax
movl -0x1c(%rbp), %ecx
shll $0x7, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0xa1a0
movq -0x11c8(%rbp), %rax
movq -0x11d0(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x11d0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x11d0(%rbp)
jmp 0xa162
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
leaq -0x11c0(%rbp), %rdx
movl $0x400, %ecx            # imm = 0x400
callq 0xa230
leaq -0x11c0(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x3890
addq $0x11f0, %rsp           # imm = 0x11F0
popq %rbp
retq
nopw %cs:(%rax,%rax)