// File: /workspace/github_c_makefile_star2_O0/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 %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
leaq -0x70(%rbp), %rdi
movl $0x18, %esi
callq 0x1280
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x1822(%rip), %rsi      # 0x6966
callq 0x10a0
cmpl $0x0, %eax
je 0x5178
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x14a0
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rcx
movl $0x18, %esi
movl $0x1, %edx
callq 0x1160
jmp 0x51ba
movq 0x3e59(%rip), %rax      # 0x8fd8
movq (%rax), %rax
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
movl $0x18, %esi
callq 0x1410
movq %rax, %rdi
movq -0x78(%rbp), %rcx
movl $0x30, %esi
movl $0x1, %edx
callq 0x1160
movq -0x78(%rbp), %rsi
leaq 0xe64(%rip), %rdi       # 0x6019
callq 0x1060
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x15b0
movq %rax, -0x88(%rbp)
movq %rdx, -0x80(%rbp)
movq -0x88(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
callq 0x10e0
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
jne 0x5212
movl $0x1, %edi
leaq 0xe0e(%rip), %rsi       # 0x601b
callq 0x1340
movq -0x98(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1070
movq -0x98(%rbp), %rdi
addq $0x20, %rdi
movq -0x80(%rbp), %rsi
movq -0x88(%rbp), %rdx
callq 0x10d0
movq -0x80(%rbp), %rdi
callq 0x1030
movq -0x90(%rbp), %rdi
movl $0x1, %esi
callq 0x1090
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jne 0x527c
movl $0x1, %edi
leaq 0x16f1(%rip), %rsi      # 0x6968
callq 0x1340
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
leaq -0x70(%rbp), %rcx
leaq -0x50(%rbp), %r8
leaq -0x30(%rbp), %r9
callq 0x33f0
movq -0x98(%rbp), %rdi
callq 0x1030
movq -0x78(%rbp), %rax
movq 0x3d1f(%rip), %rcx      # 0x8fd8
cmpq (%rcx), %rax
je 0x52e4
movq -0xa0(%rbp), %rdi
addq $0x10, %rdi
movq -0x90(%rbp), %rsi
subq $0x10, %rsi
movq -0x78(%rbp), %rcx
movl $0x1, %edx
callq 0x1160
jmp 0x5330
movq -0xa0(%rbp), %rdi
addq $0x10, %rdi
movq -0x90(%rbp), %rax
subq $0x10, %rax
movl %eax, %esi
callq 0x1410
movq %rax, %rdi
movq -0x90(%rbp), %rsi
subq $0x10, %rsi
shlq %rsi
movq -0x78(%rbp), %rcx
movl $0x1, %edx
callq 0x1160
movq -0x78(%rbp), %rsi
leaq 0xcee(%rip), %rdi       # 0x6019
callq 0x1060
movq -0xa0(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0xa0, %rsp
popq %rbp
retq