// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2160
movl -0x14(%rbp), %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xc8(%rbp)
movl -0x14(%rbp), %eax
subl $0x5, %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0xd0(%rbp)
movq -0xc0(%rbp), %rdi
addq $0x20, %rdi
callq 0x2240
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
jne 0x49b8
leaq 0x72b6(%rip), %rdi      # 0xbc67
movb $0x0, %al
callq 0x3340
cmpq $0x0, -0x20(%rbp)
je 0x49ca
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x6100
leaq -0xb0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x2110
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rsi
movq %rax, %rdx
callq 0xa650
leaq -0xb0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movq -0xd8(%rbp), %rdx
callq 0x6170
movq -0xd8(%rbp), %rax
addq $0x20, %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq -0xd8(%rbp), %rcx
addq -0xc0(%rbp), %rcx
addq $0x20, %rcx
cmpq %rcx, %rax
jae 0x4aa8
leaq -0xb0(%rbp), %rdi
callq 0xa5d0
leaq -0xb0(%rbp), %rdi
movq -0xe8(%rbp), %rsi
addq $-0x20, %rsi
movl $0x20, %edx
callq 0xa650
leaq -0xb0(%rbp), %rdi
movq -0xe8(%rbp), %rsi
callq 0xa6f0
movq -0xe8(%rbp), %rax
addq $0x20, %rax
movq %rax, -0xe8(%rbp)
jmp 0x4a3b
movq -0xd8(%rbp), %rax
addq -0xc0(%rbp), %rax
addq $-0x20, %rax
movq %rax, -0xe0(%rbp)
movq $0x0, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jae 0x4b98
leaq -0xb0(%rbp), %rdi
callq 0xa5d0
leaq -0xb0(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movl $0x20, %edx
callq 0xa650
leaq -0xb0(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0xa6f0
movq -0xe0(%rbp), %rax
movzbl 0x3(%rax), %eax
shlq $0x18, %rax
movq -0xe0(%rbp), %rcx
movzbl 0x2(%rcx), %ecx
shlq $0x10, %rcx
orq %rcx, %rax
movq -0xe0(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
shlq $0x8, %rcx
orq %rcx, %rax
movq -0xe0(%rbp), %rcx
movzbl (%rcx), %ecx
shlq $0x0, %rcx
orq %rcx, %rax
movq %rax, -0xf0(%rbp)
movq -0xd8(%rbp), %rax
movq -0xf0(%rbp), %rcx
andq -0xc8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x4acc
movq -0x10(%rbp), %rax
movq -0xe0(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rax)
movq -0xd8(%rbp), %rdi
callq 0x2040
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)