// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x3af8(%rip), %rsi      # 0x77c7
callq 0x22d0
testq %rax, %rax
je 0x407f
movq %rax, %r15
leaq 0x30(%rsp), %rdi
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x20e0
testq %rax, %rax
je 0x409f
movq %r15, %rdi
callq 0x2100
movzbl 0x39(%rsp), %edx
cmpl $0x3, %edx
jne 0x40ab
leaq 0x50(%rsp), %r14
movzbl -0x18(%r14), %ebp
testl %ebp, %ebp
je 0x3e6c
leaq 0x3c(%rsp), %r15
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x2370
movl %eax, %r12d
leaq 0x100(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4b5c
testl %eax, %eax
je 0x3d69
leaq 0x100(%rsp), %rsi
pushq $0x6e
popq %rdx
movl %r12d, %edi
callq 0x2320
testl %eax, %eax
je 0x3e81
movl %r12d, %edi
callq 0x2170
leaq 0x3951(%rip), %rsi      # 0x76c9
leaq 0x100(%rsp), %r12
movq %r12, %rdi
callq 0x4127
leaq 0x10(%rsp), %r13
leaq 0x30(%rsp), %rcx
movq %r12, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x4678
leaq 0x90(%rsp), %r12
movq %r12, %rdi
callq 0x6aa8
pushq $0x20
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x6ac8
leaq 0x70(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x6b28
pushq $0x14
popq %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x2260
testl %eax, %eax
jne 0x40bc
cmpl $0x0, 0x73f6(%rip)      # 0xb1e0
je 0x3efc
movabsq $0x1ffffffff, %rax      # imm = 0x1FFFFFFFF
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x2370
movl %eax, 0x8(%rsp)
cmpl $-0x1, %eax
je 0x3eee
movl %eax, %ebp
leaq 0x100(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4b5c
testl %eax, %eax
je 0x3efc
callq 0x2360
testl %eax, %eax
je 0x3f46
cmpl $-0x1, %eax
jne 0x3efc
callq 0x2070
movl (%rax), %edi
callq 0x2350
leaq 0x3a87(%rip), %rdi      # 0x78e4
movq %rax, %rsi
xorl %eax, %eax
callq 0x4c25
jmp 0x3efc
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups %xmm1, 0x10(%rbx)
movups %xmm0, (%rbx)
jmp 0x3f34
leaq 0x10(%rsp), %rsi
pushq $0x20
popq %rdx
movl %r12d, %edi
callq 0x21b0
movq %rax, %r13
movl %r12d, %edi
callq 0x2170
cmpq $0x20, %r13
jne 0x3d71
leaq 0x90(%rsp), %r12
movq %r12, %rdi
callq 0x6aa8
leaq 0x10(%rsp), %rsi
pushq $0x20
popq %rdx
movq %r12, %rdi
callq 0x6ac8
leaq 0x70(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x6b28
pushq $0x14
popq %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x2260
testl %eax, %eax
jne 0x3d71
jmp 0x3efc
leaq 0x39d1(%rip), %rdi      # 0x78c6
xorl %eax, %eax
callq 0x4c25
leaq 0x100(%rsp), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x4ed8
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
callq 0x4f49
pushq $0x20
popq %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4f5a
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x2170
pushq $0x1
popq %rdi
callq 0x2170
movl $0xfffffe7f, %edi       # imm = 0xFFFFFE7F
callq 0x21f0
leaq 0x102(%rsp), %rbx
movq %rbx, %rdi
callq 0x2080
testl %eax, %eax
je 0x3f83
callq 0x2070
movl (%rax), %edi
cmpl $0x2, %edi
jne 0x4105
leaq 0x100(%rsp), %rsi
pushq $0x6e
popq %rdx
movl %ebp, %edi
callq 0x2290
testl %eax, %eax
jne 0x40ca
movl %ebp, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x2250
testl %eax, %eax
jne 0x40f1
pushq $0x2
popq %rbx
leaq 0x8(%rsp), %r14
pushq $0x1
popq %r15
leaq 0x10(%rsp), %r12
pushq $0x20
popq %r13
leaq 0x39d4(%rip), %rbp      # 0x79a1
movl %ebx, %edi
callq 0x2170
imull $0x3e8, 0x7202(%rip), %edx # imm = 0x3E8
movq %r14, %rdi
movq %r15, %rsi
callq 0x2280
testl %eax, %eax
js 0x4022
je 0x404c
movl 0x8(%rsp), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22f0
cmpl $-0x1, %eax
je 0x3fd4
movl %eax, %ebx
movl %eax, %edi
movq %r12, %rsi
movq %r13, %rdx
callq 0x20f0
cmpq $0x20, %rax
je 0x3fcd
movq %rbp, %rdi
xorl %eax, %eax
callq 0x4c25
jmp 0x3fcd
leaq 0x102(%rsp), %rdi
callq 0x2080
callq 0x2070
movl (%rax), %edi
callq 0x2350
leaq 0x3932(%rip), %rdi      # 0x7974
movq %rax, %rsi
xorl %eax, %eax
callq 0x3af1
leaq 0x102(%rsp), %rdi
callq 0x2080
movq 0x6f98(%rip), %rax      # 0xaff8
movq (%rax), %rsi
leaq 0x3922(%rip), %rdi      # 0x798c
callq 0x2150
movl 0x8(%rsp), %edi
callq 0x2170
xorl %edi, %edi
callq 0x2310
callq 0x2070
movl (%rax), %edi
callq 0x2350
leaq 0x3738(%rip), %rdi      # 0x77ca
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3af1
leaq 0x3753(%rip), %rdi      # 0x77f9
movq %r14, %rsi
jmp 0x4045
leaq 0x3765(%rip), %rdi      # 0x7817
pushq $0x3
popq %rsi
xorl %eax, %eax
callq 0x3af1
leaq 0x3787(%rip), %rdi      # 0x784a
xorl %eax, %eax
callq 0x3af1
callq 0x2070
movl (%rax), %edi
cmpl $0x62, %edi
je 0x40fd
callq 0x2350
leaq 0x3846(%rip), %rdi      # 0x7928
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4c25
jmp 0x40fd
callq 0x2070
movl (%rax), %edi
cmpl $0x62, %edi
jne 0x4116
pushq $0x1
popq %rdi
callq 0x2310
callq 0x2350
leaq 0x37f0(%rip), %rdi      # 0x7901
jmp 0x4042
callq 0x2350
leaq 0x382b(%rip), %rdi      # 0x794d
jmp 0x4042