// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xcb8, %rsp            # imm = 0xCB8
movq %r9, 0x8(%rsp)
movl %r8d, %r14d
movl %r8d, 0x10(%rsp)
movl %ecx, %r12d
movq %rdx, %rbx
movl %esi, %r15d
movq %rdi, 0x28(%rsp)
leaq 0x4b0(%rsp), %rbp
movq %rbp, %rdi
callq 0x2194
pushq $0x1
popq %r13
movq %rbp, %rdi
movl %r13d, %esi
callq 0x3e65
movq %rbp, %rdi
movl %r15d, 0x24(%rsp)
movl %r15d, %esi
callq 0x3e65
movq %rbp, %rdi
movl %r12d, %esi
callq 0x3e65
movq %rbp, %rdi
movl %r14d, %esi
callq 0x3e65
pushq $0x13
popq %rsi
movq %rbp, %rdi
callq 0x3e65
movq %rbp, %rdi
movl %r13d, %esi
callq 0x3e65
movq %rbp, %rdi
movl 0xcf0(%rsp), %r14d
movl %r14d, %esi
callq 0x3e65
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x2100
movq %rbp, %rdi
movl 0xd00(%rsp), %r14d
movl %r14d, %esi
callq 0x3e65
movq %rbp, %rdi
movq 0xcf8(%rsp), %rsi
movq %r14, %rdx
callq 0x2100
movq %rbp, %rdi
movl 0xd10(%rsp), %r14d
movl %r14d, %esi
callq 0x3e65
movq %rbp, %rdi
movq 0xd08(%rsp), %rsi
movq %r14, %rdx
callq 0x2100
movq %rbp, %rdi
movl 0xd20(%rsp), %r14d
movl %r14d, %esi
callq 0x3e65
movq %rbp, %rdi
movq 0xd18(%rsp), %rsi
movq %r14, %rdx
callq 0x2100
leaq 0x460(%rsp), %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2217
andq $0x0, 0x40(%r15)
leaq 0x8b0(%rsp), %r13
pushq $0x48
popq %rbp
movq %r13, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
movl %ebp, %ecx
callq 0x3e92
leaq 0x48(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x3f50
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3f68
movl $0x1, 0x40(%r15)
movq %r13, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
movl %ebp, %ecx
callq 0x3e92
movq %r14, %rdi
movq %r13, %rsi
callq 0x3f50
movq %rbx, 0x18(%rsp)
leaq 0x400(%rbx), %rdi
movq %r14, %rsi
callq 0x3f68
pushq $0x48
popq %rsi
movq %r15, %rdi
callq 0x1734
movl $0x400, %esi            # imm = 0x400
movq %r13, %rdi
callq 0x1734
movq %r14, %rdi
callq 0x3f80
movl %r12d, %eax
andl $-0x4, %eax
movq %rax, 0x8(%rsp)
shrl $0x2, %r12d
movl %r12d, 0x14(%rsp)
movl 0x10(%rsp), %eax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
cmpq 0x30(%rsp), %r14
je 0x3db2
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpq $0x4, %rbx
je 0x3daa
movl %r14d, 0x448(%rsp)
movl %ebx, 0x44c(%rsp)
movq 0x8(%rsp), %rax
movl %eax, 0x450(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x454(%rsp)
andl $0x0, 0x458(%rsp)
movl %ebx, %eax
orl %r14d, %eax
je 0x3c31
andl $0x0, 0x45c(%rsp)
jmp 0x3c4d
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x458(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x69c1
xorl %eax, %eax
movq %rbx, %rcx
orq %r14, %rcx
sete %al
movl 0x14(%rsp), %edx
movl %edx, %ecx
imull %ebx, %ecx
leal (%rcx,%rax,2), %r12d
movq %rbx, 0x38(%rsp)
incl %ebx
imull %edx, %ebx
movq %r15, 0x40(%rsp)
leal (%r15,%rax,2), %ebp
shlq $0xa, %rbp
addq 0x18(%rsp), %rbp
cmpq %rbx, %r12
jae 0x3d93
movl 0x45c(%rsp), %ecx
movl %ecx, %edx
andl $0x7f, %edx
jne 0x3cb5
incl 0x458(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x69c1
movl 0x45c(%rsp), %ecx
movl %ecx, %edx
andl $0x7f, %edx
leal 0x1(%rcx), %eax
movl %eax, 0x45c(%rsp)
movl 0x450(%rsp), %esi
movl 0x448(%rsp), %r8d
movl 0x44c(%rsp), %edi
movl %esi, %r9d
shrl $0x2, %r9d
leal 0x1(%rdi), %eax
andl $0x3, %eax
imull %r9d, %eax
testl %r8d, %r8d
pushq $0x3
popq %r10
cmovnel %r10d, %edi
cmovel %r8d, %eax
imull %r9d, %edi
leal (%rdi,%rcx), %r8d
decl %r8d
movl %edx, %edx
movl 0x48(%rsp,%rdx,8), %edx
imulq %rdx, %rdx
shrq $0x20, %rdx
imulq %rdx, %r8
shrq $0x20, %r8
addl %edi, %ecx
addl $-0x2, %ecx
addq %rcx, %rax
subq %r8, %rax
xorl %edx, %edx
divq %rsi
testq %r12, %r12
movl %r12d, %esi
cmovel 0x8(%rsp), %esi
decl %esi
shlq $0xa, %rsi
movq 0x18(%rsp), %rax
addq %rax, %rsi
shlq $0xa, %rdx
leaq (%rdx,%rax), %r15
movq %r13, %rdi
callq 0x3f68
movq %r13, %rdi
movq %r15, %rsi
callq 0x73e8
movq %rbp, %rdi
movq %r13, %rsi
testq %r14, %r14
je 0x3d6c
callq 0x73e8
jmp 0x3d71
callq 0x3f68
movq %r13, %rdi
callq 0x6a67
movq %rbp, %rdi
movq %r13, %rsi
callq 0x73e8
incq %r12
addq $0x400, %rbp            # imm = 0x400
jmp 0x3c81
movq 0x38(%rsp), %rbx
incq %rbx
movq 0x40(%rsp), %r15
addl 0x14(%rsp), %r15d
jmp 0x3be8
incq %r14
jmp 0x3bd8
leaq 0x48(%rsp), %rdi
callq 0x3f80
leaq 0x8b0(%rsp), %rdi
callq 0x3f80
movq 0x8(%rsp), %rax
leal -0x1(%rax), %ebx
shlq $0xa, %rbx
addq 0x18(%rsp), %rbx
xorl %r14d, %r14d
cmpq $0x400, %r14            # imm = 0x400
je 0x3e00
leaq (%rsp,%r14), %rdi
addq $0x4b0, %rdi            # imm = 0x4B0
movq (%rbx,%r14), %rsi
callq 0x3991
addq $0x8, %r14
jmp 0x3ddd
leaq 0x4b0(%rsp), %r14
movq 0x28(%rsp), %rdi
movl 0x24(%rsp), %esi
movq %r14, %rdx
movl $0x400, %ecx            # imm = 0x400
callq 0x3e92
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdi
callq 0x1734
movq 0x8(%rsp), %rax
shll $0x7, %eax
movq %rax, 0x8(%rsp)
xorl %eax, %eax
movq 0x18(%rsp), %rcx
cmpq %rax, 0x8(%rsp)
je 0x3e53
movq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x3e3f
addq $0xcb8, %rsp            # imm = 0xCB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq