// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x5, -0x8(%rbp)
je 0x50d0
movl $0x2, %edi
leaq 0x1845(%rip), %rsi      # 0x6910
callq 0x1340
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1500
cmpl $0x0, %eax
je 0x50fb
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
movl $0x1, %edi
leaq 0x185f(%rip), %rsi      # 0x6955
callq 0x13a0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x30(%rbp), %rsi
movl $0x20, %edx
callq 0x1540
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x50(%rbp), %rsi
movl $0x20, %edx
callq 0x1540
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x15b0
movq %rax, -0x80(%rbp)
movq %rdx, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x70(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x68(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x80(%rbp), %rax
subq $0x18, %rax
addq $0x10, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x10e0
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
jne 0x5198
movl $0x1, %edi
leaq 0xe88(%rip), %rsi       # 0x601b
callq 0x1340
movq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1070
movq -0x90(%rbp), %rdi
addq $0x10, %rdi
movq -0x78(%rbp), %rsi
addq $0x18, %rsi
movq -0x80(%rbp), %rdx
subq $0x18, %rdx
callq 0x10d0
movq -0x78(%rbp), %rdi
callq 0x1030
movq -0x88(%rbp), %rdi
movl $0x1, %esi
callq 0x1090
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
jne 0x5207
movl $0x1, %edi
leaq 0x1764(%rip), %rsi      # 0x6966
callq 0x1340
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
leaq -0x70(%rbp), %rcx
leaq -0x30(%rbp), %r8
leaq -0x50(%rbp), %r9
callq 0x3440
movq -0x90(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x172d(%rip), %rsi      # 0x6975
callq 0x10a0
cmpl $0x0, %eax
je 0x529b
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x14a0
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
addq $0x20, %rdi
movq -0x88(%rbp), %rsi
subq $0x20, %rsi
movq -0xa0(%rbp), %rcx
movl $0x1, %edx
callq 0x1160
movq -0xa0(%rbp), %rdi
callq 0x1050
jmp 0x52c5
movq -0x98(%rbp), %rdi
addq $0x20, %rdi
movq -0x88(%rbp), %rsi
subq $0x20, %rsi
movq 0x3d20(%rip), %rax      # 0x8fd8
movq (%rax), %rcx
movl $0x1, %edx
callq 0x1160
movq -0x98(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0xa0, %rsp
popq %rbp
retq