// File: /workspace/github_c_makefile_star2_O3/skeeto[P]enchive/src/enchive.c
// Function: key_derive
static void
key_derive(const char *passphrase, uint8_t *buf, int iexp, const uint8_t *salt)
{
    uint8_t salt32[SHA256_BLOCK_SIZE] = {0};
    SHA256_CTX ctx[1];
    unsigned long i;
    unsigned long memlen = 1UL << iexp;
    unsigned long mask = memlen - 1;
    unsigned long iterations = 1UL << (iexp - 5);
    uint8_t *memory, *memptr, *p;

    memory = malloc(memlen + SHA256_BLOCK_SIZE);
    if (!memory)
        fatal("not enough memory for key derivation");

    if (salt)
        memcpy(salt32, salt, 8);
    hmac_init(ctx, salt32);
    sha256_update(ctx, (uint8_t *)passphrase, strlen(passphrase));
    hmac_final(ctx, salt32, memory);

    for (p = memory + SHA256_BLOCK_SIZE;
         p < memory + memlen + SHA256_BLOCK_SIZE;
         p += SHA256_BLOCK_SIZE) {
        sha256_init(ctx);
        sha256_update(ctx, p - SHA256_BLOCK_SIZE, SHA256_BLOCK_SIZE);
        sha256_final(ctx, p);
    }

    memptr = memory + memlen - SHA256_BLOCK_SIZE;
    for (i = 0; i < iterations; i++) {
        unsigned long offset;
        sha256_init(ctx);
        sha256_update(ctx, memptr, SHA256_BLOCK_SIZE);
        sha256_final(ctx, memptr);
        offset = ((unsigned long)memptr[3] << 24 |
                  (unsigned long)memptr[2] << 16 |
                  (unsigned long)memptr[1] <<  8 |
                  (unsigned long)memptr[0] <<  0);
        memptr = memory + (offset & mask);
    }

    memcpy(buf, memptr, SHA256_BLOCK_SIZE);
    free(memory);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, %rbx
movl %edx, %r13d
movq %rsi, 0x8(%rsp)
movq %rdi, %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl $0x1, %r14d
movl %r13d, %ecx
shlq %cl, %r14
leaq 0x20(%r14), %rdi
callq 0x2230
testq %rax, %rax
je 0x4b81
movq %rax, %r15
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leal -0x5(%r13), %eax
movq %rax, (%rsp)
testq %rbx, %rbx
je 0x4a89
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
addq %r14, %r12
leaq 0x30(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x4fd2
movq %rbp, %rdi
callq 0x2110
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x7032
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x501b
addq %r15, %r14
cmpl $0x3f, %r13d
je 0x4b04
leaq 0x30(%rsp), %rbx
movq %r15, %r13
leaq 0x20(%r13), %rbp
movq %rbx, %rdi
callq 0x700c
movl $0x20, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7032
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x709b
movq %rbp, %r13
cmpq %r14, %rbp
jb 0x4ad5
addq $-0x20, %r14
movl $0x1, %ebx
leaq 0x30(%rsp), %r13
movq (%rsp), %rbp
movq %r13, %rdi
callq 0x700c
movl $0x20, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x7032
movq %r13, %rdi
movq %r14, %rsi
callq 0x709b
movl (%r14), %r14d
movq %rbx, %rax
movl %ebp, %ecx
shrq %cl, %rax
andl %r12d, %r14d
addq %r15, %r14
incq %rbx
testq %rax, %rax
je 0x4b16
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movq 0x8(%rsp), %rax
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq %r15, %rdi
callq 0x2040
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3ffb(%rip), %rdi      # 0x8b83
xorl %eax, %eax
callq 0x3ea8