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

    struct stat st;
    long long r;

    if (!checkfd(8, POLLIN)) die_usage(NAME, "read secretkey failed", "<ciphertext 8<secretkey >message");
    limits();

    /* mmap input */
    if (fstat(0, &st) == 0) {
        maplen = st.st_size;
        map = mmap(0, maplen, PROT_READ, MAP_SHARED, 0, 0);
        if (map != MAP_FAILED) {
            input = map;
            inputlen = maplen;
        }
    }
    /* fallback to malloc(3) */
    if (!input) {
        buf = memreadall(&inputlen, 0);
        if (!buf) die_temp(NAME, "read ciphertext failed");
        input = buf;
    }

    if (inputlen > MAX) die_perm(NAME, "ciphertext too long");
    if (inputlen < crypto_kem_mceliece8192128sha512_CIPHERTEXTBYTES + poly1305_BYTES) die_perm(NAME, "short ciphert text");
    inputlen -= poly1305_BYTES;

    /* get secret key */
    r = readblock(8, g.sk, sizeof g.sk);
    if (r == -1) die_temp(NAME, "read secretkey failed");
    if (r != sizeof g.sk) die_perm(NAME, "read secretkey failed: short secret key");
    close(8);

    /* KEM */
    if (crypto_kem_mceliece8192128sha512_dec(g.k, input, g.sk) != 0) {
        randombytes(g.k, sizeof g.k);
    }
    randombytes(g.sk, sizeof g.sk);

    /* symetric key and nonce for encryption */
    chacha20_init(&g.chacha20ctx, /* key */g.k, /* nonce */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);

    /* verify authenticator */
    if (poly1305_verify(input + inputlen, input, inputlen, g.ak) != 0) die_perm(NAME, "decryption failed");
    randombytes(g.ak, sizeof g.ak);

    input += crypto_kem_mceliece8192128sha512_CIPHERTEXTBYTES;
    inputlen -= crypto_kem_mceliece8192128sha512_CIPHERTEXTBYTES;

    /* read encrypted input, decrypt and write output */
    while (inputlen >= sizeof g.output) {
        chacha20_blocks(&g.chacha20ctx, g.output, input, sizeof g.output);
        if (writeall(1, g.output, sizeof g.output) == -1) die_temp(NAME, "write message failed");
        inputlen -= sizeof g.output;
        input += sizeof g.output;
    }
    if (inputlen > 0) {
        chacha20_blocks(&g.chacha20ctx, g.output, input, inputlen);
        if (writeall(1, g.output, inputlen) == -1) die_temp(NAME, "write message failed");
    }
    if (fsyncfd(1) == -1) die_temp(NAME, "write message failed");
    die(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
pushq $0x8
popq %rdi
pushq $0x1
popq %rsi
callq 0x3bb8
testl %eax, %eax
jne 0x12cc
leaq 0x5d4b(%rip), %rdi      # 0x7004
leaq 0x5d4e(%rip), %rsi      # 0x700e
leaq 0x5d5d(%rip), %rdx      # 0x7024
callq 0x48a0
callq 0x54d4
leaq 0x8(%rsp), %rsi
xorl %edi, %edi
callq 0x1130
testl %eax, %eax
je 0x12ea
movq 0x10d5c8(%rip), %rax    # 0x10e8b0
jmp 0x132a
movq 0x38(%rsp), %rsi
movq %rsi, 0x10d5aa(%rip)    # 0x10e8a0
pushq $0x1
popq %rcx
xorl %edi, %edi
movl %ecx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x9da1(%rip)      # 0xb0b0
cmpq $-0x1, %rax
je 0x12e1
movq %rax, 0x10d594(%rip)    # 0x10e8b0
movq 0x10d57d(%rip), %rcx    # 0x10e8a0
movq %rcx, 0x10d58e(%rip)    # 0x10e8b8
testq %rax, %rax
jne 0x136a
leaq 0x10d582(%rip), %rdi    # 0x10e8b8
xorl %esi, %esi
callq 0x5510
movq %rax, 0x10d564(%rip)    # 0x10e8a8
testq %rax, %rax
jne 0x1363
leaq 0x5cb4(%rip), %rdi      # 0x7004
leaq 0x5cee(%rip), %rsi      # 0x7045
callq 0x49bf
movq 0x10d545(%rip), %rax    # 0x10e8a8
movq %rax, 0x10d546(%rip)    # 0x10e8b0
movq 0x10d547(%rip), %rax    # 0x10e8b8
movabsq $0x4000000000000001, %rcx # imm = 0x4000000000000001
cmpq %rcx, %rax
jb 0x139a
leaq 0x5c7d(%rip), %rdi      # 0x7004
leaq 0x5cce(%rip), %rsi      # 0x705c
callq 0x4a2b
movq 0x10d51e(%rip), %rax    # 0x10e8b8
cmpq $0xff, %rax
ja 0x13bc
leaq 0x5c5b(%rip), %rdi      # 0x7004
leaq 0x5cc0(%rip), %rsi      # 0x7070
callq 0x4a2b
movq 0x10d4fc(%rip), %rax    # 0x10e8b8
addq $-0x10, %rax
movq %rax, 0x10d4f1(%rip)    # 0x10e8b8
leaq 0x9cfa(%rip), %rsi      # 0xb0c8
pushq $0x8
popq %rdi
movl $0x3700, %edx           # imm = 0x3700
callq 0x6198
cmpq $0x3700, %rax           # imm = 0x3700
je 0x140f
cmpq $-0x1, %rax
jne 0x13fc
leaq 0x5c14(%rip), %rdi      # 0x7004
leaq 0x5c17(%rip), %rsi      # 0x700e
callq 0x49bf
leaq 0x5c01(%rip), %rdi      # 0x7004
leaq 0x5c79(%rip), %rsi      # 0x7083
callq 0x4a2b
pushq $0x8
popq %rdi
callq 0x1090
movq 0x10d492(%rip), %rsi    # 0x10e8b0
leaq 0xd3a3(%rip), %rdi      # 0xe7c8
leaq 0x9c9c(%rip), %rdx      # 0xb0c8
callq 0x566e
testl %eax, %eax
je 0x1444
leaq 0xd38c(%rip), %rdi      # 0xe7c8
pushq $0x40
popq %rsi
callq 0x610c
leaq 0x9c7d(%rip), %rdi      # 0xb0c8
movl $0x3700, %esi           # imm = 0x3700
callq 0x610c
leaq 0x10d3ec(%rip), %rbx    # 0x10e848
leaq 0xd365(%rip), %r14      # 0xe7c8
leaq 0xd37e(%rip), %rdx      # 0xe7e8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1cc8
pushq $0x40
popq %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x610c
xorps %xmm0, %xmm0
movups %xmm0, 0xd3aa(%rip)     # 0xe838
movups %xmm0, 0xd393(%rip)     # 0xe828
movups %xmm0, 0xd37c(%rip)     # 0xe818
movups %xmm0, 0xd365(%rip)     # 0xe808
leaq 0xd35e(%rip), %r14      # 0xe808
movq %rbx, %rdi
movq %r14, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x1d11
movq 0x10d3ee(%rip), %rsi    # 0x10e8b0
movq 0x10d3ef(%rip), %rdx    # 0x10e8b8
leaq (%rsi,%rdx), %rdi
movq %r14, %rcx
callq 0x60d1
testl %eax, %eax
je 0x14ec
leaq 0x5b24(%rip), %rdi      # 0x7004
leaq 0x5bc4(%rip), %rsi      # 0x70ab
callq 0x4a2b
leaq 0xd315(%rip), %rdi      # 0xe808
pushq $0x40
popq %rsi
callq 0x610c
movl $0xf0, %edx
addq 0x10d3a9(%rip), %rdx    # 0x10e8b0
movq %rdx, 0x10d3a2(%rip)    # 0x10e8b0
movq $-0xf0, %rcx
addq 0x10d39c(%rip), %rcx    # 0x10e8b8
movq %rcx, 0x10d395(%rip)    # 0x10e8b8
leaq 0x10d31e(%rip), %rbx    # 0x10e848
leaq 0xd317(%rip), %r14      # 0xe848
movl $0x100000, %r13d        # imm = 0x100000
pushq $0x1
popq %rbp
leaq 0x5b7c(%rip), %r12      # 0x70bd
movq $-0x100000, %r15        # imm = 0xFFF00000
cmpq $0x100000, %rcx         # imm = 0x100000
jb 0x15a8
movl $0x100000, %ecx         # imm = 0x100000
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1d11
movl $0x100000, %edx         # imm = 0x100000
movl %ebp, %edi
movq %r14, %rsi
callq 0x6770
cmpl $-0x1, %eax
jne 0x1584
leaq 0x5a88(%rip), %rdi      # 0x7004
movq %r12, %rsi
callq 0x49bf
movq 0x10d32d(%rip), %rcx    # 0x10e8b8
addq %r15, %rcx
movq %rcx, 0x10d323(%rip)    # 0x10e8b8
movq 0x10d314(%rip), %rdx    # 0x10e8b0
addq %r13, %rdx
movq %rdx, 0x10d30a(%rip)    # 0x10e8b0
jmp 0x1548
testq %rcx, %rcx
je 0x15ed
leaq 0x10d294(%rip), %rdi    # 0x10e848
leaq 0xd28d(%rip), %rbx      # 0xe848
movq %rbx, %rsi
callq 0x1d11
movq 0x10d2ee(%rip), %rdx    # 0x10e8b8
pushq $0x1
popq %rdi
movq %rbx, %rsi
callq 0x6770
cmpl $-0x1, %eax
jne 0x15ed
leaq 0x5a23(%rip), %rdi      # 0x7004
leaq 0x5ad5(%rip), %rsi      # 0x70bd
callq 0x49bf
pushq $0x1
popq %rdi
callq 0x4fac
cmpl $-0x1, %eax
jne 0x160d
leaq 0x5a03(%rip), %rdi      # 0x7004
leaq 0x5ab5(%rip), %rsi      # 0x70bd
callq 0x49bf
xorl %edi, %edi
callq 0x4a6f
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq