// File: /workspace/github_c_makefile_star2_O3/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
movl $0x8, %edi
movl $0x1, %esi
callq 0x3c64
testl %eax, %eax
jne 0x12e9
leaq 0x6d2e(%rip), %rdi      # 0x8004
leaq 0x6d31(%rip), %rsi      # 0x800e
leaq 0x6d40(%rip), %rdx      # 0x8024
callq 0x4c10
callq 0x5c48
leaq 0x8(%rsp), %rsi
xorl %edi, %edi
callq 0x1140
testl %eax, %eax
je 0x1307
movq 0x10d5bb(%rip), %rax    # 0x10e8c0
jmp 0x134c
movq 0x38(%rsp), %rsi
movq %rsi, 0x10d59d(%rip)    # 0x10e8b0
xorl %edi, %edi
movl $0x1, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x9d87(%rip)      # 0xb0b8
cmpq $-0x1, %rax
je 0x12fe
movq %rax, 0x10d582(%rip)    # 0x10e8c0
movq 0x10d56b(%rip), %rcx    # 0x10e8b0
movq %rcx, 0x10d57c(%rip)    # 0x10e8c8
testq %rax, %rax
jne 0x138c
leaq 0x10d570(%rip), %rdi    # 0x10e8c8
xorl %esi, %esi
callq 0x5c88
movq %rax, 0x10d552(%rip)    # 0x10e8b8
testq %rax, %rax
jne 0x1385
leaq 0x6c92(%rip), %rdi      # 0x8004
leaq 0x6ccc(%rip), %rsi      # 0x8045
callq 0x4d3f
movq 0x10d533(%rip), %rax    # 0x10e8b8
movq %rax, 0x10d534(%rip)    # 0x10e8c0
movq 0x10d535(%rip), %rax    # 0x10e8c8
movabsq $0x4000000000000001, %rcx # imm = 0x4000000000000001
cmpq %rcx, %rax
jb 0x13bc
leaq 0x6c5b(%rip), %rdi      # 0x8004
leaq 0x6cac(%rip), %rsi      # 0x805c
callq 0x4dad
movq 0x10d50c(%rip), %rax    # 0x10e8c8
cmpq $0xff, %rax
ja 0x13de
leaq 0x6c39(%rip), %rdi      # 0x8004
leaq 0x6c9e(%rip), %rsi      # 0x8070
callq 0x4dad
movq 0x10d4ea(%rip), %rax    # 0x10e8c8
addq $-0x10, %rax
movq %rax, 0x10d4df(%rip)    # 0x10e8c8
leaq 0x9ce8(%rip), %rsi      # 0xb0d8
movl $0x3700, %edx           # imm = 0x3700
movl $0x8, %edi
callq 0x6b40
cmpq $0x3700, %rax           # imm = 0x3700
je 0x1433
cmpq $-0x1, %rax
jne 0x1420
leaq 0x6bf0(%rip), %rdi      # 0x8004
leaq 0x6bf3(%rip), %rsi      # 0x800e
callq 0x4d3f
leaq 0x6bdd(%rip), %rdi      # 0x8004
leaq 0x6c55(%rip), %rsi      # 0x8083
callq 0x4dad
movl $0x8, %edi
callq 0x1090
movq 0x10d47c(%rip), %rsi    # 0x10e8c0
leaq 0xd38d(%rip), %rdi      # 0xe7d8
leaq 0x9c86(%rip), %rdx      # 0xb0d8
callq 0x5dec
testl %eax, %eax
je 0x146c
leaq 0xd376(%rip), %rdi      # 0xe7d8
movl $0x40, %esi
callq 0x6a90
leaq 0x9c65(%rip), %rdi      # 0xb0d8
movl $0x3700, %esi           # imm = 0x3700
callq 0x6a90
leaq 0x10d3d4(%rip), %rbx    # 0x10e858
leaq 0xd34d(%rip), %r14      # 0xe7d8
leaq 0xd366(%rip), %rdx      # 0xe7f8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1cd4
movl $0x40, %esi
movq %r14, %rdi
callq 0x6a90
xorps %xmm0, %xmm0
movups %xmm0, 0xd394(%rip)     # 0xe848
movups %xmm0, 0xd37d(%rip)     # 0xe838
movups %xmm0, 0xd366(%rip)     # 0xe828
movups %xmm0, 0xd34f(%rip)     # 0xe818
leaq 0xd348(%rip), %r14      # 0xe818
movl $0x40, %ecx
movq %rbx, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0x1d20
movq 0x10d3d6(%rip), %rsi    # 0x10e8c0
movq 0x10d3d7(%rip), %rdx    # 0x10e8c8
leaq (%rsi,%rdx), %rdi
movq %r14, %rcx
callq 0x6a2d
testl %eax, %eax
je 0x1514
leaq 0x6afc(%rip), %rdi      # 0x8004
leaq 0x6b9c(%rip), %rsi      # 0x80ab
callq 0x4dad
leaq 0xd2fd(%rip), %rdi      # 0xe818
movl $0x40, %esi
callq 0x6a90
movl $0xf0, %edx
addq 0x10d38f(%rip), %rdx    # 0x10e8c0
movq %rdx, 0x10d388(%rip)    # 0x10e8c0
movq $-0xf0, %rcx
addq 0x10d382(%rip), %rcx    # 0x10e8c8
movq %rcx, 0x10d37b(%rip)    # 0x10e8c8
cmpq $0x100000, %rcx         # imm = 0x100000
jb 0x15e0
leaq 0x10d2f7(%rip), %rbx    # 0x10e858
leaq 0xd2f0(%rip), %r14      # 0xe858
movl $0x100000, %r13d        # imm = 0x100000
leaq 0x6a8f(%rip), %r15      # 0x8004
leaq 0x6b41(%rip), %r12      # 0x80bd
movq $-0x100000, %rbp        # imm = 0xFFF00000
movl $0x100000, %ecx         # imm = 0x100000
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1d20
movl $0x100000, %edx         # imm = 0x100000
movl $0x1, %edi
movq %r14, %rsi
callq 0x70f8
cmpl $-0x1, %eax
jne 0x15b5
movq %r15, %rdi
movq %r12, %rsi
callq 0x4d3f
movq 0x10d30c(%rip), %rcx    # 0x10e8c8
addq %rbp, %rcx
movq %rcx, 0x10d302(%rip)    # 0x10e8c8
movq 0x10d2f3(%rip), %rdx    # 0x10e8c0
addq %r13, %rdx
movq %rdx, 0x10d2e9(%rip)    # 0x10e8c0
cmpq $0xfffff, %rcx          # imm = 0xFFFFF
ja 0x1583
testq %rcx, %rcx
je 0x1627
leaq 0x10d26c(%rip), %rdi    # 0x10e858
leaq 0xd265(%rip), %rbx      # 0xe858
movq %rbx, %rsi
callq 0x1d20
movq 0x10d2c6(%rip), %rdx    # 0x10e8c8
movl $0x1, %edi
movq %rbx, %rsi
callq 0x70f8
cmpl $-0x1, %eax
jne 0x1627
leaq 0x69e9(%rip), %rdi      # 0x8004
leaq 0x6a9b(%rip), %rsi      # 0x80bd
callq 0x4d3f
movl $0x1, %edi
callq 0x53c4
cmpl $-0x1, %eax
jne 0x1649
leaq 0x69c7(%rip), %rdi      # 0x8004
leaq 0x6a79(%rip), %rsi      # 0x80bd
callq 0x4d3f
xorl %edi, %edi
callq 0x4df3
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq