// File: /workspace/github_c_makefile_star2_O1/sbp[P]tweetnacl-tools/tweetnacl-decrypt.c
// Function: main
int main(int argc, char *argv[]) {
    if (argc != 5) error(2,
        "Usage: tweetnacl-decrypt send-key.pub recv-key.sec text.enc text.txt");

    // This will also erroneously fail if the file "-" exists
    if (file_exists(argv[4])) errorf(1, "File <%s> exists", argv[4]);

    // Alice has sent to Bob, not surprisingly
    unsigned char a_public_key[crypto_box_PUBLICKEYBYTES];
    unsigned char b_secret_key[crypto_box_SECRETKEYBYTES];

    read_key(argv[1], a_public_key, crypto_box_PUBLICKEYBYTES);
    read_key(argv[2], b_secret_key, crypto_box_SECRETKEYBYTES);

    unsigned char nonce[crypto_box_NONCEBYTES];

    // Input
    Content c = read_file(argv[3]);
    memcpy(nonce, c.bytes, crypto_box_NONCEBYTES);

    long esize = c.size - crypto_box_NONCEBYTES + crypto_box_BOXZEROBYTES;
    unsigned char *encrypted = malloc(esize);
    if (encrypted == NULL) error(1, "Malloc failed!");
    memset(encrypted, 0, crypto_box_BOXZEROBYTES);
    memcpy(encrypted + crypto_box_BOXZEROBYTES,
        c.bytes + crypto_box_NONCEBYTES, c.size - crypto_box_NONCEBYTES);
    // Equivalently, esize - crypto_box_BOXZEROBYTES
    free(c.bytes);

    // Output
    unsigned char *message = calloc(esize, sizeof(unsigned char));
    if (message == NULL) error(1, "Calloc failed!");

    // Encrypt
    crypto_box_open(message, encrypted, esize,
        nonce, a_public_key, b_secret_key);
    free(encrypted);

    if (strcmp(argv[4], "-") != 0) {
        FILE *out = create_file(argv[4]);
        fwrite(message + crypto_box_ZEROBYTES,
               esize - crypto_box_ZEROBYTES, 1, out);
        fclose(out);
    } else {
        fwrite(message + crypto_box_ZEROBYTES,
               esize - crypto_box_ZEROBYTES, 1, stdout);
    }
    free(message);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
cmpl $0x5, %edi
je 0x374f
leaq 0x114b(%rip), %rsi      # 0x4890
movl $0x2, %edi
callq 0x1344
movq 0x20(%rbx), %rdi
callq 0x1498
testl %eax, %eax
je 0x3771
movq 0x20(%rbx), %rdx
leaq 0x116e(%rip), %rsi      # 0x48d5
movl $0x1, %edi
callq 0x138c
movq 0x8(%rbx), %rdi
leaq 0x40(%rsp), %rsi
movl $0x20, %edx
callq 0x14ac
movq 0x10(%rbx), %rdi
leaq 0x20(%rsp), %rsi
movl $0x20, %edx
callq 0x14ac
movq 0x18(%rbx), %rdi
callq 0x150a
movq %rax, %r14
movq %rdx, %r15
movups (%rdx), %xmm0
movaps %xmm0, (%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x10(%rsp)
leaq -0x18(%r14), %rbp
leaq -0x8(%r14), %r13
movq %r13, %rdi
callq 0x10f0
movq %rax, %r12
testq %rax, %rax
jne 0x37df
leaq 0x844(%rip), %rsi       # 0x4019
movl $0x1, %edi
callq 0x1344
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq %r12, %rdi
addq $0x10, %rdi
movq %r15, %rsi
addq $0x18, %rsi
movq %rbp, %rdx
callq 0x10e0
movq %r15, %rdi
callq 0x1030
movl $0x1, %esi
movq %r13, %rdi
callq 0x10a0
movq %rax, %r15
testq %rax, %rax
jne 0x382b
leaq 0x10c5(%rip), %rsi      # 0x48e6
movl $0x1, %edi
callq 0x1344
movq %rsp, %rcx
leaq 0x40(%rsp), %r8
leaq 0x20(%rsp), %r9
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x25fe
movq %r12, %rdi
callq 0x1030
movq 0x20(%rbx), %rbx
leaq 0x109c(%rip), %rsi      # 0x48f5
movq %rbx, %rdi
callq 0x10b0
testl %eax, %eax
je 0x3895
movq %rbx, %rdi
callq 0x145c
movq %rax, %rbx
movq %r15, %rdi
addq $0x20, %rdi
addq $-0x28, %r14
movl $0x1, %edx
movq %r14, %rsi
movq %rax, %rcx
callq 0x1170
movq %rbx, %rdi
callq 0x1050
jmp 0x38b7
movq %r15, %rdi
addq $0x20, %rdi
addq $-0x28, %r14
movq 0x3731(%rip), %rax      # 0x6fd8
movq (%rax), %rcx
movl $0x1, %edx
movq %r14, %rsi
callq 0x1170
movq %r15, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq