// File: /workspace/github_c_makefile_star2_O3/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, (%rsp)
movl %r8d, %r13d
movl %ecx, %r14d
movq %rdx, %r15
movl %esi, %r12d
movq %rdi, 0x30(%rsp)
movaps 0x59c6(%rip), %xmm0     # 0x95c0
leaq 0x8c0(%rsp), %rbp
movaps %xmm0, (%rbp)
movaps 0x59e3(%rip), %xmm0     # 0x95f0
movaps %xmm0, 0x30(%rbp)
movaps 0x59c8(%rip), %xmm0     # 0x95e0
movaps %xmm0, 0x20(%rbp)
movaps 0x59ad(%rip), %xmm0     # 0x95d0
movaps %xmm0, 0x10(%rbp)
movabsq $0x6a09e667f2bdc948, %rax # imm = 0x6A09E667F2BDC948
movq %rax, (%rbp)
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 0x41d9
movq %rbp, %rdi
movl %r12d, 0x20(%rsp)
movl %r12d, %esi
callq 0x41d9
movq %rbp, %rdi
movl %r14d, %r12d
movl %r14d, %esi
callq 0x41d9
movq %rbp, %rdi
movl %r13d, 0xc(%rsp)
movl %r13d, %esi
callq 0x41d9
movq %rbp, %rdi
movl $0x13, %esi
callq 0x41d9
movq %rbp, %rdi
movl $0x1, %esi
callq 0x41d9
movq %rbp, %rdi
movl 0xd00(%rsp), %r14d
movl %r14d, %esi
callq 0x41d9
movq %rbp, %rdi
movq (%rsp), %rsi
movq %r14, %rdx
callq 0x224b
movq %rbp, %rdi
movl 0xd10(%rsp), %r14d
movl %r14d, %esi
callq 0x41d9
movq %rbp, %rdi
movq 0xd08(%rsp), %rsi
movq %r14, %rdx
callq 0x224b
movq %rbp, %rdi
movl 0xd20(%rsp), %r14d
movl %r14d, %esi
callq 0x41d9
movq %rbp, %rdi
movq 0xd18(%rsp), %rsi
movq %r14, %rdx
callq 0x224b
movq %rbp, %rdi
movl 0xd30(%rsp), %r14d
movl %r14d, %esi
callq 0x41d9
movq %rbp, %rdi
movq 0xd28(%rsp), %rsi
movq %r14, %rdx
callq 0x224b
leaq 0x60(%rsp), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x240d
movq %rbx, 0x40(%r13)
leaq 0x4c0(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r13, %rdx
movl $0x48, %ecx
callq 0x4200
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %r14, %rsi
callq 0x10b0
movl $0x1, 0x40(%r13)
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r13, %rdx
movl $0x48, %ecx
callq 0x4200
movq %r15, 0x10(%rsp)
leaq 0x400(%r15), %rdi
movl $0x400, %edx            # imm = 0x400
movq %r14, %rsi
callq 0x10b0
movb $0x0, 0x60(%rsp,%rbx)
incq %rbx
cmpq $0x48, %rbx
jne 0x3db0
xorl %eax, %eax
movb $0x0, 0x4c0(%rsp,%rax)
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x3dc0
xorl %eax, %eax
movq 0x10(%rsp), %rcx
movq $0x0, 0xa8(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x3dda
movl %r12d, %r15d
andl $-0x4, %r15d
cmpl $0x0, 0xc(%rsp)
je 0x4123
movl 0xc(%rsp), %eax
movq %rax, 0x38(%rsp)
movl %r12d, %eax
shrl $0x2, %r12d
movl %r12d, 0x24(%rsp)
andl $-0x4, %eax
shlq $0x8, %rax
movq %rax, 0x40(%rsp)
movq $0x0, (%rsp)
movq %r15, 0x28(%rsp)
movq %rcx, 0x18(%rsp)
xorl %ebx, %ebx
movq (%rsp), %rcx
movl %ecx, 0x4a8(%rsp)
movl %ebx, 0x4ac(%rsp)
movl %r15d, 0x4b0(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x4b4(%rsp)
movl $0x0, 0x4b8(%rsp)
movl %ebx, %eax
orl %ecx, %eax
je 0x3e7b
movl $0x0, 0x4bc(%rsp)
jmp 0x3e9a
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x4b8(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x7232
xorl %r13d, %r13d
movq %rbx, %rax
orq (%rsp), %rax
sete %r13b
movl 0x24(%rsp), %ecx
movl %ecx, %eax
imull %ebx, %eax
leal (%rax,%r13,2), %eax
movq %rbx, 0x48(%rsp)
leal 0x1(%rbx), %edx
imull %ecx, %edx
movq %rdx, 0x58(%rsp)
cmpl %edx, %eax
jae 0x40e7
addl %r13d, %r13d
movl %eax, %ebx
movl 0x4bc(%rsp), %r14d
shll $0xa, %r13d
addq 0x18(%rsp), %r13
xorl %r12d, %r12d
movq %rbx, 0x50(%rsp)
movl %r14d, %esi
andl $0x7f, %esi
jne 0x3f15
incl 0x4b8(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x7232
movl 0x4bc(%rsp), %r14d
movl %r14d, %esi
andl $0x7f, %esi
movl %r14d, %edx
leal 0x1(%rdx), %r14d
movl %r14d, 0x4bc(%rsp)
movl 0x4b0(%rsp), %ecx
movl 0x4a8(%rsp), %r8d
movl 0x4ac(%rsp), %edi
movl %ecx, %r9d
shrl $0x2, %r9d
leal 0x1(%rdi), %eax
andl $0x3, %eax
imull %r9d, %eax
testl %r8d, %r8d
movl $0x3, %r10d
cmovnel %r10d, %edi
cmovel %r8d, %eax
imull %r9d, %edi
leal (%rdi,%rdx), %r8d
decl %r8d
movl %esi, %esi
movl 0xa8(%rsp,%rsi,8), %esi
imulq %rsi, %rsi
shrq $0x20, %rsi
imulq %rsi, %r8
shrq $0x20, %r8
addl %edi, %edx
addl $-0x2, %edx
addq %rdx, %rax
subq %r8, %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rbp
testq %rbx, %rbx
movl %ebx, %esi
cmovel %r15d, %esi
decl %esi
shlq $0xa, %rsi
movq 0x10(%rsp), %r15
addq %r15, %rsi
shlq $0xa, %rbp
addq %r15, %rbp
movl $0x400, %edx            # imm = 0x400
leaq 0x4c0(%rsp), %rdi
callq 0x10b0
cmpq $0x0, (%rsp)
je 0x4052
xorl %eax, %eax
movups (%rbp,%rax,8), %xmm0
movups 0x4c0(%rsp,%rax,8), %xmm1
xorps %xmm0, %xmm1
movups %xmm1, 0x4c0(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x80, %rax
jne 0x3fd0
xorl %eax, %eax
movq 0x28(%rsp), %r15
movups 0x4c0(%rsp,%rax,8), %xmm0
movups (%r13,%rax,8), %xmm1
xorps %xmm0, %xmm1
movups %xmm1, (%r13,%rax,8)
addq $0x2, %rax
cmpq $0x80, %rax
jne 0x3ffb
leaq 0x4c0(%rsp), %rdi
callq 0x72f1
xorl %eax, %eax
movups 0x4c0(%rsp,%rax,8), %xmm0
movups (%r13,%rax,8), %xmm1
xorps %xmm0, %xmm1
movups %xmm1, (%r13,%rax,8)
addq $0x2, %rax
cmpq $0x80, %rax
jne 0x402d
jmp 0x40cf
movq 0x50(%rsp), %rax
leaq (%r12,%rax), %rdi
shlq $0xa, %rdi
addq %r15, %rdi
xorl %eax, %eax
movups (%rbp,%rax,8), %xmm0
movups 0x4c0(%rsp,%rax,8), %xmm1
xorps %xmm0, %xmm1
movups %xmm1, 0x4c0(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x80, %rax
jne 0x4064
movl $0x400, %edx            # imm = 0x400
leaq 0x4c0(%rsp), %r15
movq %r15, %rsi
callq 0x10b0
movq %r15, %rdi
callq 0x72f1
xorl %eax, %eax
movq 0x28(%rsp), %r15
movups 0x4c0(%rsp,%rax,8), %xmm0
movups (%r13,%rax,8), %xmm1
xorps %xmm0, %xmm1
movups %xmm1, (%r13,%rax,8)
addq $0x2, %rax
cmpq $0x80, %rax
jne 0x40ac
incq %rbx
incq %r12
addq $0x400, %r13            # imm = 0x400
cmpq 0x58(%rsp), %rbx
jb 0x3eeb
movq 0x48(%rsp), %rbx
incq %rbx
movq 0x18(%rsp), %rax
addq 0x40(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x4, %rbx
jne 0x3e38
movq (%rsp), %rcx
incq %rcx
movq %rcx, (%rsp)
cmpq 0x38(%rsp), %rcx
movq 0x10(%rsp), %rcx
jne 0x3e31
xorl %eax, %eax
movq $0x0, 0xa8(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x4125
xorl %eax, %eax
movq $0x0, 0x4c0(%rsp,%rax,8)
incq %rax
cmpq $0x80, %rax
jne 0x413e
leal -0x1(%r15), %eax
shlq $0xa, %rax
addq %rcx, %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq %rdx, 0x8c0(%rsp,%rcx,8)
incq %rcx
cmpq $0x80, %rcx
jne 0x4162
leaq 0x8c0(%rsp), %rdx
movq 0x30(%rsp), %rdi
movl 0x20(%rsp), %esi
movl $0x400, %ecx            # imm = 0x400
callq 0x4200
xorl %eax, %eax
movb $0x0, 0x8c0(%rsp,%rax)
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x4197
shll $0x7, %r15d
movq 0x10(%rsp), %rcx
je 0x41c7
xorl %eax, %eax
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x41b7
addq $0xcc8, %rsp            # imm = 0xCC8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq