// File: /workspace/github_c_makefile_star2_O0/skeeto[P]enchive/src/enchive.c
// Function: write_seckey
static void
write_seckey(char *file, const uint8_t *seckey, int iexp)
{
    FILE *secfile;
    chacha_ctx cha[1];
    SHA256_CTX sha[1];
    uint8_t buf[8 + 1 + 3 + 20 + 32] = {0}; /* entire file contents */
    uint8_t protect[32];

    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;

    buf_version[0] = ENCHIVE_FORMAT_VERSION;

    if (iexp) {
        /* Prompt for a passphrase. */
        char pass[2][ENCHIVE_PASSPHRASE_MAX];
        get_passphrase(pass[0], sizeof(pass[0]),
                       "protection passphrase (empty for none): ");
        if (!pass[0][0]) {
            /* Nevermind. */
            iexp = 0;
        }  else {
            get_passphrase(pass[1], sizeof(pass[0]),
                           "protection passphrase (repeat): ");
            if (strcmp(pass[0], pass[1]) != 0)
                fatal("protection passphrases don't match");

            /* Generate an IV to double as salt. */
            secure_entropy(buf_iv, 8);

            key_derive(pass[0], protect, iexp, buf_iv);
            buf_iterations[0] = iexp;

            sha256_init(sha);
            sha256_update(sha, protect, sizeof(protect));
            sha256_final(sha, buf_protect_hash);
        }
    }

    if (iexp) {
        /* Encrypt using key derived from passphrase. */
        chacha_keysetup(cha, protect, 256);
        chacha_ivsetup(cha, buf_iv);
        chacha_encrypt(cha, seckey, buf_seckey, 32);
    } else {
        /* Copy key to output buffer. */
        memcpy(buf_seckey, seckey, 32);
    }

    secfile = secure_creat(file);
    if (!secfile)
        fatal("failed to open key file for writing '%s'", file);
    cleanup_register(secfile, file);
    if (!fwrite(buf, sizeof(buf), 1, secfile))
        fatal("failed to write key file '%s'", file);
    cleanup_closed(secfile);
    if (fclose(secfile))
        fatal("failed to flush key file '%s' -- %s", file, strerror(errno));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x970, %rsp            # imm = 0x970
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2160
leaq -0x110(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0x110(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x140(%rbp)
leaq -0x110(%rbp), %rax
addq $0x9, %rax
movq %rax, -0x148(%rbp)
leaq -0x110(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x150(%rbp)
leaq -0x110(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x158(%rbp)
movq -0x148(%rbp), %rax
movb $0x3, (%rax)
cmpl $0x0, -0x14(%rbp)
je 0x4ed1
leaq -0x960(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
leaq 0x6ed8(%rip), %rdx      # 0xbcce
callq 0x4730
cmpb $0x0, -0x960(%rbp)
jne 0x4e10
movl $0x0, -0x14(%rbp)
jmp 0x4ecf
leaq -0x960(%rbp), %rdi
addq $0x400, %rdi            # imm = 0x400
movl $0x400, %esi            # imm = 0x400
leaq 0x6ecd(%rip), %rdx      # 0xbcf7
callq 0x4730
leaq -0x960(%rbp), %rdi
leaq -0x960(%rbp), %rsi
addq $0x400, %rsi            # imm = 0x400
callq 0x21e0
cmpl $0x0, %eax
je 0x4e5c
leaq 0x6ec3(%rip), %rdi      # 0xbd18
movb $0x0, %al
callq 0x3340
movq -0x138(%rbp), %rdi
movl $0x8, %esi
callq 0x6210
leaq -0x960(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl -0x14(%rbp), %edx
movq -0x138(%rbp), %rcx
callq 0x4920
movl -0x14(%rbp), %eax
movb %al, %cl
movq -0x140(%rbp), %rax
movb %cl, (%rax)
leaq -0xd0(%rbp), %rdi
callq 0xa5d0
leaq -0xd0(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl $0x20, %edx
callq 0xa650
leaq -0xd0(%rbp), %rdi
movq -0x150(%rbp), %rsi
callq 0xa6f0
jmp 0x4ed1
cmpl $0x0, -0x14(%rbp)
je 0x4f17
leaq -0x60(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x6890
leaq -0x60(%rbp), %rdi
movq -0x138(%rbp), %rsi
callq 0x6b60
leaq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x158(%rbp), %rdx
movl $0x20, %ecx
callq 0x6bf0
jmp 0x4f40
movq -0x158(%rbp), %rax
movq -0x10(%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 -0x8(%rbp), %rdi
callq 0x6290
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x4f66
movq -0x8(%rbp), %rsi
leaq 0x6ddc(%rip), %rdi      # 0xbd3b
movb $0x0, %al
callq 0x3340
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x62f0
leaq -0x110(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x40, %esi
movl $0x1, %edx
callq 0x2330
cmpq $0x0, %rax
jne 0x4fa5
movq -0x8(%rbp), %rsi
leaq 0x6dc6(%rip), %rdi      # 0xbd64
movb $0x0, %al
callq 0x3340
movq -0x20(%rbp), %rdi
callq 0x6380
movq -0x20(%rbp), %rdi
callq 0x2100
cmpl $0x0, %eax
je 0x4feb
movq -0x8(%rbp), %rax
movq %rax, -0x968(%rbp)
callq 0x2070
movl (%rax), %edi
callq 0x2350
movq -0x968(%rbp), %rsi
movq %rax, %rdx
leaq 0x6d9e(%rip), %rdi      # 0xbd82
movb $0x0, %al
callq 0x3340
addq $0x970, %rsp            # imm = 0x970
popq %rbp
retq
nopw %cs:(%rax,%rax)