// File: /workspace/github_c_makefile_star2_O0/LoupVaillant[P]Monocypher-Utils/src/monocypher.c
// 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
        block tmp_block;
        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(&tmp_block, hash_area);
        copy_block(blocks, &tmp_block);

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

        WIPE_BUFFER(initial_hash);
        WIPE_BUFFER(hash_area);
        wipe_block(&tmp_block);
    }

    // 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;
    FOR (pass_number, 0, nb_iterations) {
        int first_pass = pass_number == 0;

        FOR (segment, 0, 4) {
            gidx_init(&ctx, (u32)pass_number, (u32)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 = (u32)segment * segment_size + start_offset;
            u32 segment_end   = ((u32)segment + 1) * segment_size;
            FOR (current_block, segment_start, segment_end) {
                u32 reference_block = gidx_next(&ctx);
                u32 previous_block  = current_block == 0
                                    ? nb_blocks - 1
                                    : (u32)current_block - 1;
                block *c = blocks + current_block;
                block *p = blocks + previous_block;
                block *r = blocks + reference_block;
                if (first_pass) { g_copy(c, p, r, &tmp); }
                else            { g_xor (c, p, r, &tmp); }
            }
        }
    }
    wipe_block(&ctx.b);
    wipe_block(&tmp);
    // hash the very last block with H' into the output hash
    u8 final_block[1024];
    store_block(final_block, blocks + (nb_blocks - 1));
    extended_hash(hash, hash_size, final_block, 1024);
    WIPE_BUFFER(final_block);

    // wipe work area
    volatile u64 *p = (u64*)work_area;
    FOR (i, 0, 128 * nb_blocks) {
        p[i] = 0;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x15f0, %rsp           # imm = 0x15F0
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 0x3200
leaq -0x110(%rbp), %rdi
movl $0x1, %esi
callq 0x8110
movl -0xc(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0x8110
movl -0x1c(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0x8110
movl -0x20(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0x8110
leaq -0x110(%rbp), %rdi
movl $0x13, %esi
callq 0x8110
leaq -0x110(%rbp), %rdi
movl $0x1, %esi
callq 0x8110
movl 0x10(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0x8110
movq -0x28(%rbp), %rsi
movl 0x10(%rbp), %eax
movl %eax, %edx
leaq -0x110(%rbp), %rdi
callq 0x30c0
movl 0x20(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0x8110
movq 0x18(%rbp), %rsi
movl 0x20(%rbp), %eax
movl %eax, %edx
leaq -0x110(%rbp), %rdi
callq 0x30c0
movl 0x30(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0x8110
movq 0x28(%rbp), %rsi
movl 0x30(%rbp), %eax
movl %eax, %edx
leaq -0x110(%rbp), %rdi
callq 0x30c0
movl 0x40(%rbp), %esi
leaq -0x110(%rbp), %rdi
callq 0x8110
movq 0x38(%rbp), %rsi
movl 0x40(%rbp), %eax
movl %eax, %edx
leaq -0x110(%rbp), %rdi
callq 0x30c0
leaq -0x160(%rbp), %rsi
leaq -0x110(%rbp), %rdi
callq 0x3340
leaq -0x160(%rbp), %rdi
addq $0x40, %rdi
xorl %esi, %esi
callq 0x22b0
leaq -0x160(%rbp), %rdi
addq $0x44, %rdi
xorl %esi, %esi
callq 0x22b0
leaq -0x960(%rbp), %rdi
leaq -0x160(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
movl $0x48, %ecx
callq 0x8160
leaq -0x960(%rbp), %rsi
leaq -0x560(%rbp), %rdi
callq 0x82d0
movq -0x30(%rbp), %rdi
leaq -0x560(%rbp), %rsi
callq 0x8330
leaq -0x160(%rbp), %rdi
addq $0x40, %rdi
movl $0x1, %esi
callq 0x22b0
leaq -0x960(%rbp), %rdi
leaq -0x160(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
movl $0x48, %ecx
callq 0x8160
leaq -0x960(%rbp), %rsi
leaq -0x560(%rbp), %rdi
callq 0x82d0
movq -0x30(%rbp), %rdi
addq $0x400, %rdi            # imm = 0x400
leaq -0x560(%rbp), %rsi
callq 0x8330
leaq -0x160(%rbp), %rdi
movl $0x48, %esi
callq 0x1b40
leaq -0x960(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x1b40
leaq -0x560(%rbp), %rdi
callq 0x8380
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, -0x964(%rbp)
movq $0x0, -0x1188(%rbp)
movq -0x1188(%rbp), %rax
movl -0x20(%rbp), %ecx
cmpq %rcx, %rax
jae 0x8050
cmpq $0x0, -0x1188(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x118c(%rbp)
movq $0x0, -0x1198(%rbp)
cmpq $0x4, -0x1198(%rbp)
jae 0x8037
movq -0x1188(%rbp), %rax
movl %eax, %esi
movq -0x1198(%rbp), %rax
movl %eax, %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
leaq -0x1180(%rbp), %rdi
callq 0x83d0
xorl %eax, %eax
cmpl $0x0, -0x118c(%rbp)
movb %al, -0x15e1(%rbp)
je 0x7ebc
cmpq $0x0, -0x1198(%rbp)
sete %al
movb %al, -0x15e1(%rbp)
movb -0x15e1(%rbp), %dl
xorl %eax, %eax
movl $0x2, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x119c(%rbp)
movq -0x1198(%rbp), %rax
imull -0x964(%rbp), %eax
addl -0x119c(%rbp), %eax
movl %eax, -0x11a0(%rbp)
movq -0x1198(%rbp), %rax
addl $0x1, %eax
imull -0x964(%rbp), %eax
movl %eax, -0x11a4(%rbp)
movl -0x11a0(%rbp), %eax
movq %rax, -0x11b0(%rbp)
movq -0x11b0(%rbp), %rax
movl -0x11a4(%rbp), %ecx
cmpq %rcx, %rax
jae 0x801e
leaq -0x1180(%rbp), %rdi
callq 0x8480
movl %eax, -0x11b4(%rbp)
cmpq $0x0, -0x11b0(%rbp)
jne 0x7f53
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x15e8(%rbp)
jmp 0x7f63
movq -0x11b0(%rbp), %rax
subl $0x1, %eax
movl %eax, -0x15e8(%rbp)
movl -0x15e8(%rbp), %eax
movl %eax, -0x11b8(%rbp)
movq -0x30(%rbp), %rax
movq -0x11b0(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movq %rax, -0x11c0(%rbp)
movq -0x30(%rbp), %rax
movl -0x11b8(%rbp), %ecx
shlq $0xa, %rcx
addq %rcx, %rax
movq %rax, -0x11c8(%rbp)
movq -0x30(%rbp), %rax
movl -0x11b4(%rbp), %ecx
shlq $0xa, %rcx
addq %rcx, %rax
movq %rax, -0x11d0(%rbp)
cmpl $0x0, -0x118c(%rbp)
je 0x7fe4
movq -0x11c0(%rbp), %rdi
movq -0x11c8(%rbp), %rsi
movq -0x11d0(%rbp), %rdx
leaq -0xd68(%rbp), %rcx
callq 0x85e0
jmp 0x8005
movq -0x11c0(%rbp), %rdi
movq -0x11c8(%rbp), %rsi
movq -0x11d0(%rbp), %rdx
leaq -0xd68(%rbp), %rcx
callq 0x8640
jmp 0x8007
movq -0x11b0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x11b0(%rbp)
jmp 0x7f13
jmp 0x8020
movq -0x1198(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1198(%rbp)
jmp 0x7e67
jmp 0x8039
movq -0x1188(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1188(%rbp)
jmp 0x7e33
leaq -0x1180(%rbp), %rdi
callq 0x8380
leaq -0xd68(%rbp), %rdi
callq 0x8380
leaq -0x15d0(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
shlq $0xa, %rax
addq %rax, %rsi
callq 0x86a0
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
leaq -0x15d0(%rbp), %rdx
movl $0x400, %ecx            # imm = 0x400
callq 0x8160
leaq -0x15d0(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x1b40
movq -0x18(%rbp), %rax
movq %rax, -0x15d8(%rbp)
movq $0x0, -0x15e0(%rbp)
movq -0x15e0(%rbp), %rax
movl -0x1c(%rbp), %ecx
shll $0x7, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x8104
movq -0x15d8(%rbp), %rax
movq -0x15e0(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x15e0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x15e0(%rbp)
jmp 0x80c6
addq $0x15f0, %rsp           # imm = 0x15F0
popq %rbp
retq
nopl (%rax)