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

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

    // Alice is sending to Bob, not surprisingly
    unsigned char a_secret_key[crypto_box_SECRETKEYBYTES];
    unsigned char b_public_key[crypto_box_PUBLICKEYBYTES];

    read_key(argv[1], a_secret_key, crypto_box_SECRETKEYBYTES);
    read_key(argv[2], b_public_key, crypto_box_PUBLICKEYBYTES);

    unsigned char nonce[crypto_box_NONCEBYTES];
    randombytes(nonce, sizeof(nonce));

    FILE *out;
    if (strcmp(argv[4], "-") != 0) {
        out = create_file(argv[4]);
        fwrite(nonce, sizeof(nonce), 1, out);
    } else {
        out = stdout;
        fwrite(bytes_to_hex(nonce, sizeof(nonce)), sizeof(nonce) * 2, 1, out);
        fputs("\n", out);
    }

    // Input
    // unsigned char *message = read_file(argv[3]);
    Content c = read_file(argv[3]);
    long psize = crypto_box_ZEROBYTES + c.size;
    unsigned char *padded = malloc(psize);
    if (padded == NULL) error(1, "Malloc failed!");
    memset(padded, 0, crypto_box_ZEROBYTES);
    memcpy(padded + crypto_box_ZEROBYTES, c.bytes, c.size);
    free(c.bytes);

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

    // Encrypt
    crypto_box(encrypted, padded, psize, nonce, b_public_key, a_secret_key);
    free(padded);

    if (out != stdout) {
        fwrite(encrypted + crypto_box_BOXZEROBYTES,
               psize - crypto_box_BOXZEROBYTES, 1, out);
    } else {
        fwrite(bytes_to_hex(encrypted + crypto_box_BOXZEROBYTES,
                            psize - crypto_box_BOXZEROBYTES),
               (psize - crypto_box_BOXZEROBYTES) * 2, 1, out);
        fputs("\n", out);
    }
    free(encrypted);
    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rsi, %r14
cmpl $0x5, %edi
je 0x329c
leaq 0x168c(%rip), %rsi      # 0x4920
pushq $0x2
popq %rdi
callq 0x12f4
movq 0x20(%r14), %rdi
callq 0x143c
testl %eax, %eax
je 0x32bc
movq 0x20(%r14), %rdx
leaq 0x16b1(%rip), %rsi      # 0x4965
pushq $0x1
popq %rdi
callq 0x1336
movq 0x8(%r14), %rdi
leaq 0x40(%rsp), %rsi
pushq $0x20
popq %rbx
movl %ebx, %edx
callq 0x1450
movq 0x10(%r14), %rdi
leaq 0x20(%rsp), %rsi
movl %ebx, %edx
callq 0x1450
movq %rsp, %rdi
pushq $0x18
popq %rsi
callq 0x126c
movq 0x20(%r14), %rdi
cmpb $0x2d, (%rdi)
jne 0x32fd
cmpb $0x0, 0x1(%rdi)
je 0x341f
callq 0x1402
movq %rax, %rbx
movq %rsp, %rdi
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x1150
movq 0x18(%r14), %rdi
callq 0x14aa
movq %rax, %r14
movq %rdx, %r15
leaq 0x20(%rax), %r13
movq %r13, %rdi
callq 0x10d0
movq %rax, %r12
testq %rax, %rax
jne 0x3348
leaq 0xcd9(%rip), %rsi       # 0x4019
pushq $0x1
popq %rdi
callq 0x12f4
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r12)
movups %xmm0, (%r12)
movq %r12, %rdi
addq $0x20, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10c0
movq %r15, %rdi
callq 0x1030
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0x1090
movq %rax, %r15
testq %rax, %rax
jne 0x3392
leaq 0x15ee(%rip), %rsi      # 0x4978
pushq $0x1
popq %rdi
callq 0x12f4
movq %rsp, %rcx
leaq 0x20(%rsp), %r8
leaq 0x40(%rsp), %r9
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2303
movq %r12, %rdi
callq 0x1030
movq 0x3c1c(%rip), %rax      # 0x6fd8
movq %r15, %rdi
addq $0x10, %rdi
addq $0x10, %r14
cmpq (%rax), %rbx
je 0x33dc
pushq $0x1
popq %rdx
movq %r14, %rsi
movq %rbx, %rcx
callq 0x1150
jmp 0x3407
movl %r14d, %esi
callq 0x1387
addq %r14, %r14
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0x1150
leaq 0x1577(%rip), %rdi      # 0x4976
movq %rbx, %rsi
callq 0x1060
movq %r15, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x3bb2(%rip), %rax      # 0x6fd8
movq (%rax), %rbx
movq %rsp, %rdi
pushq $0x18
popq %rsi
callq 0x1387
pushq $0x30
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %rbx, %rcx
callq 0x1150
leaq 0x152a(%rip), %rdi      # 0x4976
movq %rbx, %rsi
callq 0x1060
jmp 0x3316