// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x20, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x4, %edi
movl $0x1, %esi
callq 0x86f0
cmpl $0x0, %eax
jne 0x1284
leaq 0xdd93(%rip), %rdi      # 0xf004
leaq 0xdd96(%rip), %rsi      # 0xf00e
leaq 0xdda5(%rip), %rdx      # 0xf024
callq 0xa1d0
callq 0xbb80
movl $0x4, %edi
leaq 0x10e23(%rip), %rsi     # 0x120b8
movl $0x14b800, %edx         # imm = 0x14B800
callq 0xd480
movq %rax, -0x18(%rbp)
cmpq $-0x1, -0x18(%rbp)
jne 0x12bd
leaq 0xdd53(%rip), %rdi      # 0xf004
leaq 0xdd56(%rip), %rsi      # 0xf00e
callq 0xa350
cmpq $0x14b800, -0x18(%rbp)  # imm = 0x14B800
je 0x12da
leaq 0xdd36(%rip), %rdi      # 0xf004
leaq 0xdd70(%rip), %rsi      # 0xf045
callq 0xa3d0
movl $0x4, %edi
callq 0x1080
leaq 0x10dcd(%rip), %rdx     # 0x120b8
movq %rdx, %rdi
addq $0x14b880, %rdi         # imm = 0x14B880
movq %rdx, %rsi
addq $0x14b800, %rsi         # imm = 0x14B800
callq 0xbd00
cmpl $0x0, %eax
je 0x131c
leaq 0xdcf4(%rip), %rdi      # 0xf004
leaq 0xdd56(%rip), %rsi      # 0xf06d
callq 0xa350
movl $0x1, %edi
leaq 0x10d90(%rip), %rsi     # 0x120b8
addq $0x14b880, %rsi         # imm = 0x14B880
movl $0xf0, %edx
callq 0xdff0
cmpl $-0x1, %eax
jne 0x1351
leaq 0xdcbf(%rip), %rdi      # 0xf004
leaq 0xdd33(%rip), %rsi      # 0xf07f
callq 0xa350
leaq 0x10d60(%rip), %rsi     # 0x120b8
movq %rsi, %rdi
addq $0x24b980, %rdi         # imm = 0x24B980
addq $0x14b800, %rsi         # imm = 0x14B800
movq %rsi, %rdx
addq $0x20, %rdx
callq 0x2190
leaq 0x10d3c(%rip), %rdi     # 0x120b8
addq $0x14b800, %rdi         # imm = 0x14B800
movl $0x40, %esi
callq 0xd3b0
leaq 0x10d24(%rip), %rdi     # 0x120b8
addq $0x14b840, %rdi         # imm = 0x14B840
xorl %esi, %esi
movl $0x40, %edx
callq 0x1070
leaq 0x10d0a(%rip), %rdx     # 0x120b8
movq %rdx, %rdi
addq $0x24b980, %rdi         # imm = 0x24B980
addq $0x14b840, %rdx         # imm = 0x14B840
movl $0x40, %ecx
movq %rdx, %rsi
callq 0x22e0
leaq 0x10ce5(%rip), %rsi     # 0x120b8
movq %rsi, %rdi
addq $0x24b9d8, %rdi         # imm = 0x24B9D8
addq $0x14b840, %rsi         # imm = 0x14B840
callq 0xc6f0
leaq 0x10cc8(%rip), %rdi     # 0x120b8
addq $0x14b840, %rdi         # imm = 0x14B840
movl $0x40, %esi
callq 0xd3b0
leaq 0x10cb0(%rip), %rsi     # 0x120b8
movq %rsi, %rdi
addq $0x24b9d8, %rdi         # imm = 0x24B9D8
addq $0x14b880, %rsi         # imm = 0x14B880
movl $0xf0, %edx
callq 0xc8e0
leaq 0x10c8e(%rip), %rdi     # 0x120b8
addq $0x14b880, %rdi         # imm = 0x14B880
movl $0xf0, %esi
callq 0xd3b0
xorl %edi, %edi
leaq 0x10c74(%rip), %rsi     # 0x120b8
addq $0x14b980, %rsi         # imm = 0x14B980
movl $0x100000, %edx         # imm = 0x100000
callq 0xd480
movq %rax, -0x18(%rbp)
cmpq $-0x1, -0x18(%rbp)
jne 0x1473
leaq 0xdb9d(%rip), %rdi      # 0xf004
leaq 0xdc29(%rip), %rsi      # 0xf097
callq 0xa350
movq -0x18(%rbp), %rax
addq 0x25c6aa(%rip), %rax    # 0x25db28
movq %rax, 0x25c6a3(%rip)    # 0x25db28
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
cmpq %rax, 0x25c692(%rip)    # 0x25db28
jbe 0x14ab
leaq 0xdb65(%rip), %rdi      # 0xf004
leaq 0xdc05(%rip), %rsi      # 0xf0ab
callq 0xa3d0
movq -0x18(%rbp), %rcx
leaq 0x10c02(%rip), %rdx     # 0x120b8
movq %rdx, %rdi
addq $0x24b980, %rdi         # imm = 0x24B980
addq $0x14b980, %rdx         # imm = 0x14B980
movq %rdx, %rsi
callq 0x22e0
movq -0x18(%rbp), %rdx
leaq 0x10bde(%rip), %rsi     # 0x120b8
movq %rsi, %rdi
addq $0x24b9d8, %rdi         # imm = 0x24B9D8
addq $0x14b980, %rsi         # imm = 0x14B980
callq 0xc8e0
movq -0x18(%rbp), %rdx
movl $0x1, %edi
leaq 0x10bb8(%rip), %rsi     # 0x120b8
addq $0x14b980, %rsi         # imm = 0x14B980
callq 0xdff0
cmpl $-0x1, %eax
jne 0x1524
leaq 0xdaec(%rip), %rdi      # 0xf004
leaq 0xdb60(%rip), %rsi      # 0xf07f
callq 0xa350
cmpq $0x100000, -0x18(%rbp)  # imm = 0x100000
je 0x1530
jmp 0x1535
jmp 0x143b
leaq 0x10b7c(%rip), %rsi     # 0x120b8
movq %rsi, %rdi
addq $0x24b9d8, %rdi         # imm = 0x24B9D8
addq $0x14b970, %rsi         # imm = 0x14B970
callq 0xce50
movl $0x1, %edi
leaq 0x10b5a(%rip), %rsi     # 0x120b8
addq $0x14b970, %rsi         # imm = 0x14B970
movl $0x10, %edx
callq 0xdff0
cmpl $-0x1, %eax
jne 0x1587
leaq 0xda89(%rip), %rdi      # 0xf004
leaq 0xdafd(%rip), %rsi      # 0xf07f
callq 0xa350
movl $0x1, %edi
callq 0xb180
cmpl $-0x1, %eax
jne 0x15a9
leaq 0xda67(%rip), %rdi      # 0xf004
leaq 0xdadb(%rip), %rsi      # 0xf07f
callq 0xa350
xorl %edi, %edi
callq 0xa420
xorl %eax, %eax
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax,%rax)