// File: /workspace/github_c_makefile_star2_O2/janmojzis[P]mcencrypt/mcencrypt.c
// Function: main
int main(int argc, char **argv) {

    long long r;

    /* get publickey */
    if (!checkfd(4, POLLIN)) die_usage(NAME, "read publickey failed", "<message 4<publickey >ciphertext");
    limits();
    r = readblock(4, g.pk, sizeof g.pk);
    if (r == -1) die_temp(NAME, "read publickey failed");
    if (r != sizeof g.pk) die_perm(NAME, "read publickey failed: short public key");
    close(4);

    /* KEM - generate ciphertext + symetric key */
    if (crypto_kem_mceliece8192128sha512_enc(g.c, g.k, g.pk) != 0) die_temp(NAME, "encryption failed");
    if (writeall(1, g.c, sizeof g.c) == -1) die_temp(NAME, "write ciphertext failed");

    /* symetric key and nonce for encryption */
    chacha20_init(&g.chacha20ctx, g.k, g.k + chacha20_KEYBYTES);
    randombytes(g.k, sizeof g.k);

    /* symetric key for authentication */
    memset(g.ak, 0, sizeof g.ak);
    chacha20_blocks(&g.chacha20ctx, g.ak, g.ak, sizeof g.ak);
    poly1305_init(&g.poly1305ctx, g.ak);
    randombytes(g.ak, sizeof g.ak);

    /* authenticate ciphertext */
    poly1305_blocks(&g.poly1305ctx, g.c, sizeof g.c); /* XXX sizeof g.c must be power of 16 */
    randombytes(g.c, sizeof g.c);

    /* read input and write encrypted output */
    for (;;) {
        r = readblock(0, g.buf, sizeof g.buf);
        if (r == -1) die_temp(NAME, "read message failed");
        inputlen += r;
        if (inputlen > MAX) die_perm(NAME, "read message failed: input message too long");
        chacha20_blocks(&g.chacha20ctx, g.buf, g.buf, r);
        poly1305_blocks(&g.poly1305ctx, g.buf, r);
        if (writeall(1, g.buf, r) == -1) die_temp(NAME, "write ciphertext failed");
        if (r != sizeof g.buf) break;
    }

    /* write authenticator */
    poly1305_final(&g.poly1305ctx, g.a);
    if (writeall(1, g.a, sizeof g.a) == -1) die_temp(NAME, "write ciphertext failed");
    if (fsyncfd(1) == -1) die_temp(NAME, "write ciphertext failed");

    die(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
pushq $0x4
popq %rdi
pushq $0x1
popq %rsi
callq 0x3a78
testl %eax, %eax
jne 0x1261
leaq 0x5db6(%rip), %rdi      # 0x7004
leaq 0x5db9(%rip), %rsi      # 0x700e
leaq 0x5dc8(%rip), %rdx      # 0x7024
callq 0x4760
callq 0x5394
leaq 0x9e4b(%rip), %rsi      # 0xb0b8
pushq $0x4
popq %rdi
movl $0x14b800, %edx         # imm = 0x14B800
callq 0x6058
cmpq $0x14b800, %rax         # imm = 0x14B800
je 0x12ae
cmpq $-0x1, %rax
jne 0x129b
leaq 0x5d75(%rip), %rdi      # 0x7004
leaq 0x5d78(%rip), %rsi      # 0x700e
callq 0x487f
leaq 0x5d62(%rip), %rdi      # 0x7004
leaq 0x5d9c(%rip), %rsi      # 0x7045
callq 0x48eb
pushq $0x4
popq %rdi
callq 0x1080
leaq 0x15567b(%rip), %rdi    # 0x156938
leaq 0x1555f4(%rip), %rsi    # 0x1568b8
leaq 0x9ded(%rip), %rdx      # 0xb0b8
callq 0x5468
testl %eax, %eax
je 0x12e7
leaq 0x5d29(%rip), %rdi      # 0x7004
leaq 0x5d8b(%rip), %rsi      # 0x706d
callq 0x487f
leaq 0x15564a(%rip), %rsi    # 0x156938
pushq $0x1
popq %rdi
movl $0xf0, %edx
callq 0x6630
cmpl $-0x1, %eax
jne 0x1313
leaq 0x5cfd(%rip), %rdi      # 0x7004
leaq 0x5d71(%rip), %rsi      # 0x707f
callq 0x487f
leaq 0x25571e(%rip), %rbx    # 0x256a38
leaq 0x155597(%rip), %r14    # 0x1568b8
leaq 0x1555b0(%rip), %rdx    # 0x1568d8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1b88
pushq $0x40
popq %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x5fcc
xorps %xmm0, %xmm0
movups %xmm0, 0x1555dc(%rip)   # 0x156928
movups %xmm0, 0x1555c5(%rip)   # 0x156918
movups %xmm0, 0x1555ae(%rip)   # 0x156908
movups %xmm0, 0x155597(%rip)   # 0x1568f8
leaq 0x155590(%rip), %r12    # 0x1568f8
movq %rbx, %rdi
movq %r12, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x1bd1
leaq 0x255710(%rip), %r14    # 0x256a90
movq %r14, %rdi
movq %r12, %rsi
callq 0x5a50
movq %r12, %rdi
movq %r15, %rsi
callq 0x5fcc
leaq 0x15559b(%rip), %r15    # 0x156938
movl $0xf0, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x5af2
movl $0xf0, %esi
movq %r15, %rdi
callq 0x5fcc
leaq 0x155677(%rip), %r15    # 0x156a38
leaq 0x5c3c(%rip), %r12      # 0x7004
movabsq $0x4000000000000001, %rbx # imm = 0x4000000000000001
pushq $0x1
popq %r13
movl $0x100000, %edx         # imm = 0x100000
xorl %edi, %edi
movq %r15, %rsi
callq 0x6058
movq %rax, %rbp
cmpq $-0x1, %rax
jne 0x13fd
movq %r12, %rdi
leaq 0x5c9f(%rip), %rsi      # 0x7097
callq 0x487f
movq 0x255724(%rip), %rax    # 0x256b28
addq %rbp, %rax
movq %rax, 0x25571a(%rip)    # 0x256b28
cmpq %rbx, %rax
jb 0x1422
movq %r12, %rdi
leaq 0x5c8e(%rip), %rsi      # 0x70ab
callq 0x48eb
leaq 0x25560f(%rip), %rdi    # 0x256a38
movq %r15, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x1bd1
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x5af2
movl %r13d, %edi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x6630
cmpl $-0x1, %eax
jne 0x1467
movq %r12, %rdi
leaq 0x5c1d(%rip), %rsi      # 0x707f
callq 0x487f
cmpq $0x100000, %rbp         # imm = 0x100000
je 0x13d6
leaq 0x255615(%rip), %rdi    # 0x256a90
leaq 0x1555a6(%rip), %rbx    # 0x156a28
movq %rbx, %rsi
callq 0x5db1
pushq $0x1
popq %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x6630
cmpl $-0x1, %eax
jne 0x14b0
leaq 0x5b60(%rip), %rdi      # 0x7004
leaq 0x5bd4(%rip), %rsi      # 0x707f
callq 0x487f
pushq $0x1
popq %rdi
callq 0x4e6c
cmpl $-0x1, %eax
jne 0x14d0
leaq 0x5b40(%rip), %rdi      # 0x7004
leaq 0x5bb4(%rip), %rsi      # 0x707f
callq 0x487f
xorl %edi, %edi
callq 0x492f
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq