// File: /workspace/github_c_makefile_star2_O1/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
movl $0x4, %edi
movl $0x1, %esi
callq 0x3b04
testl %eax, %eax
jne 0x1275
leaq 0x5da2(%rip), %rdi      # 0x7004
leaq 0x5da5(%rip), %rsi      # 0x700e
leaq 0x5db4(%rip), %rdx      # 0x7024
callq 0x4808
callq 0x57d4
leaq 0x8e37(%rip), %rsi      # 0xa0b8
movl $0x14b800, %edx         # imm = 0x14B800
movl $0x4, %edi
callq 0x65ec
cmpq $0x14b800, %rax         # imm = 0x14B800
je 0x12c4
cmpq $-0x1, %rax
jne 0x12b1
leaq 0x5d5f(%rip), %rdi      # 0x7004
leaq 0x5d62(%rip), %rsi      # 0x700e
callq 0x4934
leaq 0x5d4c(%rip), %rdi      # 0x7004
leaq 0x5d86(%rip), %rsi      # 0x7045
callq 0x49a2
movl $0x4, %edi
callq 0x1080
leaq 0x154663(%rip), %rdi    # 0x155938
leaq 0x1545dc(%rip), %rsi    # 0x1558b8
leaq 0x8dd5(%rip), %rdx      # 0xa0b8
callq 0x58ac
testl %eax, %eax
je 0x12ff
leaq 0x5d11(%rip), %rdi      # 0x7004
leaq 0x5d73(%rip), %rsi      # 0x706d
callq 0x4934
leaq 0x154632(%rip), %rsi    # 0x155938
movl $0xf0, %edx
movl $0x1, %edi
callq 0x6bd0
cmpl $-0x1, %eax
jne 0x132d
leaq 0x5ce3(%rip), %rdi      # 0x7004
leaq 0x5d57(%rip), %rsi      # 0x707f
callq 0x4934
leaq 0x254704(%rip), %rbx    # 0x255a38
leaq 0x15457d(%rip), %r14    # 0x1558b8
leaq 0x154596(%rip), %rdx    # 0x1558d8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1bb0
movl $0x40, %esi
movq %r14, %rdi
callq 0x6524
xorps %xmm0, %xmm0
movups %xmm0, 0x1545c4(%rip)   # 0x155928
movups %xmm0, 0x1545ad(%rip)   # 0x155918
movups %xmm0, 0x154596(%rip)   # 0x155908
movups %xmm0, 0x15457f(%rip)   # 0x1558f8
leaq 0x154578(%rip), %r15    # 0x1558f8
movl $0x40, %ecx
movq %rbx, %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0x1bfc
leaq 0x2546f6(%rip), %r14    # 0x255a90
movq %r14, %rdi
movq %r15, %rsi
callq 0x5e68
movl $0x40, %esi
movq %r15, %rdi
callq 0x6524
leaq 0x15457f(%rip), %r15    # 0x155938
movl $0xf0, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x5f26
movl $0xf0, %esi
movq %r15, %rdi
callq 0x6524
leaq 0x15465b(%rip), %r15    # 0x155a38
leaq 0x5c20(%rip), %r12      # 0x7004
movabsq $0x4000000000000001, %rbp # imm = 0x4000000000000001
movl $0x100000, %edx         # imm = 0x100000
xorl %edi, %edi
movq %r15, %rsi
callq 0x65ec
movq %rax, %r13
cmpq $-0x1, %rax
jne 0x1415
movq %r12, %rdi
leaq 0x5c87(%rip), %rsi      # 0x7097
callq 0x4934
movq 0x25470c(%rip), %rax    # 0x255b28
addq %r13, %rax
movq %rax, 0x254702(%rip)    # 0x255b28
cmpq %rbp, %rax
jb 0x143a
movq %r12, %rdi
leaq 0x5c76(%rip), %rsi      # 0x70ab
callq 0x49a2
movq %rbx, %rdi
movq %r15, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x1bfc
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x5f26
movl $0x1, %edi
movq %r15, %rsi
movq %r13, %rdx
callq 0x6bd0
cmpl $-0x1, %eax
jne 0x147d
movq %r12, %rdi
leaq 0x5c07(%rip), %rsi      # 0x707f
callq 0x4934
cmpq $0x100000, %r13         # imm = 0x100000
je 0x13ee
leaq 0x2545ff(%rip), %rdi    # 0x255a90
leaq 0x154590(%rip), %rbx    # 0x155a28
movq %rbx, %rsi
callq 0x6308
movl $0x10, %edx
movl $0x1, %edi
movq %rbx, %rsi
callq 0x6bd0
cmpl $-0x1, %eax
jne 0x14ca
leaq 0x5b46(%rip), %rdi      # 0x7004
leaq 0x5bba(%rip), %rsi      # 0x707f
callq 0x4934
movl $0x1, %edi
callq 0x4f14
cmpl $-0x1, %eax
jne 0x14ec
leaq 0x5b24(%rip), %rdi      # 0x7004
leaq 0x5b98(%rip), %rsi      # 0x707f
callq 0x4934
xorl %edi, %edi
callq 0x49e8
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq