// File: /workspace/github_c_makefile_star2_O0/skeeto[P]enchive/src/enchive.c
// Function: load_seckey
static void
load_seckey(const char *file, uint8_t *seckey)
{
    FILE *secfile;
    chacha_ctx cha[1];
    SHA256_CTX sha[1];
    uint8_t buf[8 + 4 + 20 + 32];            /* entire key file contents */
    uint8_t protect[32];                     /* protection key */
    uint8_t protect_hash[SHA256_BLOCK_SIZE]; /* hash of protection key */
    int iexp;
    int version;

    uint8_t *buf_iv           = buf + SECFILE_IV;
    uint8_t *buf_iterations   = buf + SECFILE_ITERATIONS;
    uint8_t *buf_version      = buf + SECFILE_VERSION;
    uint8_t *buf_protect_hash = buf + SECFILE_PROTECT_HASH;
    uint8_t *buf_seckey       = buf + SECFILE_SECKEY;

    /* Read the entire file into buf. */
    secfile = fopen(file, "rb");
    if (!secfile)
        fatal("failed to open key file for reading '%s' -- %s",
              file, strerror(errno));
    if (!fread(buf, sizeof(buf), 1, secfile))
        fatal("failed to read key file -- %s", file);
    fclose(secfile);

    version = buf_version[0];
    if (version != ENCHIVE_FORMAT_VERSION)
        fatal("secret key version mismatch -- expected %d, got %d",
              ENCHIVE_FORMAT_VERSION, version);

    iexp = buf_iterations[0];
    if (iexp) {
        /* Secret key is encrypted. */
        int agent_success = agent_read(protect, buf_iv);
        if (agent_success) {
            /* Check validity of agent key. */
            sha256_init(sha);
            sha256_update(sha, protect, 32);
            sha256_final(sha, protect_hash);
            agent_success = !memcmp(protect_hash, buf_protect_hash, 20);
        }

        if (!agent_success) {
            /* Ask user for passphrase. */
            char pass[ENCHIVE_PASSPHRASE_MAX];
            get_passphrase(pass, sizeof(pass), "passphrase: ");
            key_derive(pass, protect, iexp, buf_iv);

            /* Validate passphrase. */
            sha256_init(sha);
            sha256_update(sha, protect, sizeof(protect));
            sha256_final(sha, protect_hash);
            if (memcmp(protect_hash, buf_protect_hash, 20) != 0)
                fatal("wrong passphrase");
        }

        /* We have the correct protection key. Start the agent? */
        if (!agent_success && global_agent_timeout)
            agent_run(protect, buf_iv);

        /* Decrypt the key into the output. */
        chacha_keysetup(cha, protect, 256);
        chacha_ivsetup(cha, buf_iv);
        chacha_encrypt(cha, buf_seckey, seckey, 32);
    } else {
        /* Key is unencrypted, copy into output. */
        memcpy(seckey, buf_seckey, 32);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5a0, %rsp            # imm = 0x5A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x110(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0x110(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x168(%rbp)
leaq -0x110(%rbp), %rax
addq $0x9, %rax
movq %rax, -0x170(%rbp)
leaq -0x110(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x178(%rbp)
leaq -0x110(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x180(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x7413(%rip), %rsi      # 0xb8a7
callq 0x22d0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x44d3
movq -0x8(%rbp), %rax
movq %rax, -0x598(%rbp)
callq 0x2070
movl (%rax), %edi
callq 0x2350
movq -0x598(%rbp), %rsi
movq %rax, %rdx
leaq 0x73de(%rip), %rdi      # 0xb8aa
movb $0x0, %al
callq 0x3340
leaq -0x110(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl $0x40, %esi
movl $0x1, %edx
callq 0x20e0
cmpq $0x0, %rax
jne 0x4505
movq -0x8(%rbp), %rsi
leaq 0x73db(%rip), %rdi      # 0xb8d9
movb $0x0, %al
callq 0x3340
movq -0x18(%rbp), %rdi
callq 0x2100
movq -0x170(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x158(%rbp)
cmpl $0x3, -0x158(%rbp)
je 0x4540
movl -0x158(%rbp), %edx
leaq 0x73c3(%rip), %rdi      # 0xb8f7
movl $0x3, %esi
movb $0x0, %al
callq 0x3340
movq -0x168(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x154(%rbp)
cmpl $0x0, -0x154(%rbp)
je 0x46ef
leaq -0x130(%rbp), %rdi
movq -0x160(%rbp), %rsi
callq 0x54e0
movl %eax, -0x184(%rbp)
cmpl $0x0, -0x184(%rbp)
je 0x45e1
leaq -0xd0(%rbp), %rdi
callq 0xa5d0
leaq -0xd0(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl $0x20, %edx
callq 0xa650
leaq -0xd0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0xa6f0
leaq -0x150(%rbp), %rdi
movq -0x178(%rbp), %rsi
movl $0x14, %edx
callq 0x21c0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x184(%rbp)
cmpl $0x0, -0x184(%rbp)
jne 0x468a
leaq -0x590(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
leaq 0x71a8(%rip), %rdx      # 0xb7a9
callq 0x4730
leaq -0x590(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl -0x154(%rbp), %edx
movq -0x160(%rbp), %rcx
callq 0x4920
leaq -0xd0(%rbp), %rdi
callq 0xa5d0
leaq -0xd0(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl $0x20, %edx
callq 0xa650
leaq -0xd0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0xa6f0
leaq -0x150(%rbp), %rdi
movq -0x178(%rbp), %rsi
movl $0x14, %edx
callq 0x21c0
cmpl $0x0, %eax
je 0x4688
leaq 0x72a9(%rip), %rdi      # 0xb92a
movb $0x0, %al
callq 0x3340
jmp 0x468a
cmpl $0x0, -0x184(%rbp)
jne 0x46af
cmpl $0x0, 0xac06(%rip)      # 0xf2a0
je 0x46af
leaq -0x130(%rbp), %rdi
movq -0x160(%rbp), %rsi
callq 0x55a0
leaq -0x60(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x6890
leaq -0x60(%rbp), %rdi
movq -0x160(%rbp), %rsi
callq 0x6b60
leaq -0x60(%rbp), %rdi
movq -0x180(%rbp), %rsi
movq -0x10(%rbp), %rdx
movl $0x20, %ecx
callq 0x6bf0
jmp 0x4718
movq -0x10(%rbp), %rax
movq -0x180(%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)
addq $0x5a0, %rsp            # imm = 0x5A0
popq %rbp
retq
nopw %cs:(%rax,%rax)