// File: /workspace/github_c_makefile_star2_O1/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 $0xcc8, %rsp            # imm = 0xCC8
movq %r9, %r14
movl %r8d, %r12d
movl %ecx, %r13d
movq %rdx, 0x18(%rsp)
movl %esi, %r15d
movq %rdi, 0x30(%rsp)
movaps 0x599c(%rip), %xmm0     # 0x95b0
leaq 0x8c0(%rsp), %rbp
movaps %xmm0, (%rbp)
movaps 0x59b9(%rip), %xmm0     # 0x95e0
movaps %xmm0, 0x30(%rbp)
movaps 0x599e(%rip), %xmm0     # 0x95d0
movaps %xmm0, 0x20(%rbp)
movaps 0x5983(%rip), %xmm0     # 0x95c0
movaps %xmm0, 0x10(%rbp)
xorl $0x1010040, (%rbp)      # imm = 0x1010040
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rbp)
movq $0x40, 0xd8(%rbp)
xorl %ebx, %ebx
movq %rbx, 0xd0(%rbp)
movq %rbp, %rdi
movl $0x1, %esi
callq 0x41f1
movq %rbp, %rdi
movl %r15d, 0x28(%rsp)
movl %r15d, %esi
callq 0x41f1
movq %rbp, %rdi
movl %r13d, %r15d
movl %r13d, %esi
callq 0x41f1
movq %rbp, %rdi
movl %r12d, 0xc(%rsp)
movl %r12d, %esi
callq 0x41f1
movq %rbp, %rdi
movl $0x13, %esi
callq 0x41f1
movq %rbp, %rdi
movl $0x1, %esi
callq 0x41f1
movq %rbp, %rdi
movl 0xd00(%rsp), %r12d
movl %r12d, %esi
callq 0x41f1
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x225d
movq %rbp, %rdi
movl 0xd10(%rsp), %r14d
movl %r14d, %esi
callq 0x41f1
movq %rbp, %rdi
movq 0xd08(%rsp), %rsi
movq %r14, %rdx
callq 0x225d
movq %rbp, %rdi
movl 0xd20(%rsp), %r14d
movl %r14d, %esi
callq 0x41f1
movq %rbp, %rdi
movq 0xd18(%rsp), %rsi
movq %r14, %rdx
callq 0x225d
movq %rbp, %rdi
movl 0xd30(%rsp), %r14d
movl %r14d, %esi
callq 0x41f1
movq %rbp, %rdi
movq 0xd28(%rsp), %rsi
movq %r14, %rdx
callq 0x225d
leaq 0x60(%rsp), %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0x242c
movq %rbx, 0x40(%r14)
leaq 0x4c0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdx
movl $0x48, %ecx
callq 0x4218
leaq (%rsp,%rbx), %rdi
addq $0x4c0, %rdi            # imm = 0x4C0
callq 0x2428
movq %rax, 0xa8(%rsp,%rbx)
addq $0x8, %rbx
cmpq $0x400, %rbx            # imm = 0x400
jne 0x3d79
leaq 0xa8(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x10b0
leaq 0x60(%rsp), %rdx
movl $0x1, 0x40(%rdx)
leaq 0x4c0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl $0x48, %ecx
callq 0x4218
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x4c0, %rdi            # imm = 0x4C0
callq 0x2428
movq %rax, 0xa8(%rsp,%rbx)
addq $0x8, %rbx
cmpq $0x400, %rbx            # imm = 0x400
jne 0x3ddd
leaq 0x400(%r14), %rdi
leaq 0xa8(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x10b0
xorl %eax, %eax
movb $0x0, 0x60(%rsp,%rax)
incq %rax
cmpq $0x48, %rax
jne 0x3e1d
xorl %eax, %eax
movb $0x0, 0x4c0(%rsp,%rax)
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x3e2d
xorl %eax, %eax
movq $0x0, 0xa8(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x3e42
movl %r15d, %r14d
andl $-0x4, %r14d
cmpl $0x0, 0xc(%rsp)
je 0x413b
movl 0xc(%rsp), %eax
movq %rax, 0x38(%rsp)
movl %r15d, %eax
shrl $0x2, %r15d
movl %r15d, 0x2c(%rsp)
andl $-0x4, %eax
shlq $0x8, %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
movq 0x10(%rsp), %rcx
movl %ecx, 0x4a8(%rsp)
movl %ebx, 0x4ac(%rsp)
movl %r14d, 0x4b0(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x4b4(%rsp)
movl $0x0, 0x4b8(%rsp)
movl %ebx, %eax
orl %ecx, %eax
je 0x3ee5
movl $0x0, 0x4bc(%rsp)
jmp 0x3f04
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x4b8(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x7267
xorl %r15d, %r15d
movq %rbx, %rax
orq 0x10(%rsp), %rax
sete %r15b
movl 0x2c(%rsp), %ecx
movl %ecx, %eax
imull %ebx, %eax
leal (%rax,%r15,2), %eax
movq %rbx, 0x48(%rsp)
leal 0x1(%rbx), %edx
imull %ecx, %edx
movq %rdx, 0x58(%rsp)
cmpl %edx, %eax
jae 0x4102
addl %r15d, %r15d
movl %eax, %r12d
shll $0xa, %r15d
addq 0x20(%rsp), %r15
xorl %ebx, %ebx
movq %r12, 0x50(%rsp)
testb $0x7f, 0x4bc(%rsp)
jne 0x3f6c
incl 0x4b8(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x7267
movl 0x4bc(%rsp), %edx
leal 0x1(%rdx), %eax
movl %eax, 0x4bc(%rsp)
movl 0x4b0(%rsp), %ecx
movl 0x4a8(%rsp), %edi
movl 0x4ac(%rsp), %esi
movl %ecx, %r8d
shrl $0x2, %r8d
movl %edx, %r9d
andl $0x7f, %r9d
leal 0x1(%rsi), %eax
andl $0x3, %eax
imull %r8d, %eax
testl %edi, %edi
movl $0x3, %r10d
cmovnel %r10d, %esi
cmovel %edi, %eax
imull %r8d, %esi
leal (%rsi,%rdx), %edi
decl %edi
movl 0xa8(%rsp,%r9,8), %r8d
imulq %r8, %r8
shrq $0x20, %r8
imulq %r8, %rdi
shrq $0x20, %rdi
addl %esi, %edx
addl $-0x2, %edx
addq %rdx, %rax
subq %rdi, %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rbp
testq %r12, %r12
movl %r12d, %esi
movq %r14, %r13
cmovel %r14d, %esi
decl %esi
shlq $0xa, %rsi
movq 0x18(%rsp), %r14
addq %r14, %rsi
shlq $0xa, %rbp
addq %r14, %rbp
movl $0x400, %edx            # imm = 0x400
leaq 0x4c0(%rsp), %rdi
callq 0x10b0
cmpq $0x0, 0x10(%rsp)
je 0x4087
xorl %eax, %eax
movq (%rbp,%rax,8), %rcx
xorq %rcx, 0x4c0(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x402b
xorl %eax, %eax
movq %r13, %r14
movq 0x4c0(%rsp,%rax,8), %rcx
xorq %rcx, (%r15,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x4048
leaq 0x4c0(%rsp), %rdi
callq 0x7320
xorl %eax, %eax
movq 0x4c0(%rsp,%rax,8), %rcx
xorq %rcx, (%r15,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x406e
jmp 0x40ea
movq 0x50(%rsp), %rax
leaq (%rbx,%rax), %rdi
shlq $0xa, %rdi
addq %r14, %rdi
xorl %eax, %eax
movq (%rbp,%rax,8), %rcx
xorq %rcx, 0x4c0(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x4099
movl $0x400, %edx            # imm = 0x400
leaq 0x4c0(%rsp), %r14
movq %r14, %rsi
callq 0x10b0
movq %r14, %rdi
callq 0x7320
xorl %eax, %eax
movq %r13, %r14
movq 0x4c0(%rsp,%rax,8), %rcx
xorq %rcx, (%r15,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x40d3
incq %r12
incq %rbx
addq $0x400, %r15            # imm = 0x400
cmpq 0x58(%rsp), %r12
jb 0x3f4e
movq 0x48(%rsp), %rbx
incq %rbx
movq 0x20(%rsp), %rax
addq 0x40(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x4, %rbx
jne 0x3ea1
movq 0x10(%rsp), %rcx
incq %rcx
movq %rcx, 0x10(%rsp)
cmpq 0x38(%rsp), %rcx
jne 0x3e95
xorl %eax, %eax
movq $0x0, 0xa8(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x413d
xorl %eax, %eax
movq 0x18(%rsp), %rbx
movq $0x0, 0x4c0(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x415b
leal -0x1(%r14), %eax
shlq $0xa, %rax
addq %rbx, %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq %rdx, 0x8c0(%rsp,%rcx,8)
incq %rcx
cmpq $0x80, %rcx
jne 0x417f
leaq 0x8c0(%rsp), %rdx
movq 0x30(%rsp), %rdi
movl 0x28(%rsp), %esi
movl $0x400, %ecx            # imm = 0x400
callq 0x4218
xorl %eax, %eax
movb $0x0, 0x8c0(%rsp,%rax)
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x41b4
shll $0x7, %r14d
je 0x41df
xorl %eax, %eax
movq $0x0, (%rbx,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x41cf
addq $0xcc8, %rsp            # imm = 0xCC8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq