// File: /workspace/github_c_makefile_star2_O2/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 $0xb8, %rsp
movq %rcx, %r13
movl %edx, %r12d
movq %rsi, %r14
movq %rdi, %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
pushq $0x1
popq %r15
movl %r12d, %ecx
shlq %cl, %r15
leaq 0x20(%r15), %rdi
callq 0x2230
testq %rax, %rax
je 0x47e0
movq %rax, %rbx
movq %r14, 0x18(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x8(%rsp)
addl $-0x5, %r12d
movq %r12, 0x10(%rsp)
testq %r13, %r13
je 0x46e7
movq (%r13), %rax
movq %rax, 0x20(%rsp)
addq %r15, 0x8(%rsp)
leaq 0x40(%rsp), %r13
leaq 0x20(%rsp), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x4cea
movq %rbp, %rdi
callq 0x2110
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x6ac8
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x4d29
addq %rbx, %r15
pushq $0x20
popq %rbp
movq %rbx, %r12
cmpq %r15, %r12
jae 0x475e
leaq 0x20(%r12), %r14
movq %r13, %rdi
callq 0x6aa8
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x6ac8
movq %r13, %rdi
movq %r14, %rsi
callq 0x6b28
movq %r14, %r12
jmp 0x472e
addq $-0x20, %r15
xorl %r14d, %r14d
leaq 0x40(%rsp), %r13
pushq $0x20
popq %rbp
movq 0x10(%rsp), %r12
movq %r14, %rax
movl %r12d, %ecx
shrq %cl, %rax
testq %rax, %rax
jne 0x47b1
movq %r13, %rdi
callq 0x6aa8
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x6ac8
movq %r13, %rdi
movq %r15, %rsi
callq 0x6b28
movl (%r15), %r15d
andl 0x8(%rsp), %r15d
addq %rbx, %r15
incq %r14
jmp 0x4772
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movq 0x18(%rsp), %rax
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq %rbx, %rdi
callq 0x2040
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3395(%rip), %rdi      # 0x7b7c
xorl %eax, %eax
callq 0x3af1