// File: /workspace/github_c_makefile_star2_O1/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x90, %rsp
movl $0x8, %edi
movl $0x1, %esi
callq 0x3c48
testl %eax, %eax
jne 0x12e8
leaq 0x5d2f(%rip), %rdi      # 0x7004
leaq 0x5d32(%rip), %rsi      # 0x700e
leaq 0x5d41(%rip), %rdx      # 0x7024
callq 0x494c
callq 0x5918
movq %rsp, %rsi
xorl %edi, %edi
callq 0x1140
testl %eax, %eax
jne 0x1340
movq 0x30(%rsp), %rsi
movq %rsi, 0x10c5a9(%rip)    # 0x10d8b0
xorl %edi, %edi
movl $0x1, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x8d93(%rip)      # 0xa0b8
cmpq $-0x1, %rax
je 0x1340
movq %rax, 0x10c58e(%rip)    # 0x10d8c0
movq 0x10c577(%rip), %rax    # 0x10d8b0
movq %rax, 0x10c588(%rip)    # 0x10d8c8
cmpq $0x0, 0x10c578(%rip)    # 0x10d8c0
jne 0x1385
leaq 0x10c577(%rip), %rdi    # 0x10d8c8
xorl %esi, %esi
callq 0x5958
movq %rax, 0x10c559(%rip)    # 0x10d8b8
testq %rax, %rax
jne 0x1377
leaq 0x5c99(%rip), %rdi      # 0x7004
leaq 0x5cd3(%rip), %rsi      # 0x7045
callq 0x4a78
movq 0x10c53a(%rip), %rax    # 0x10d8b8
movq %rax, 0x10c53b(%rip)    # 0x10d8c0
movabsq $0x4000000000000001, %rax # imm = 0x4000000000000001
cmpq %rax, 0x10c532(%rip)    # 0x10d8c8
jb 0x13ab
leaq 0x5c65(%rip), %rdi      # 0x7004
leaq 0x5cb6(%rip), %rsi      # 0x705c
callq 0x4ae6
cmpq $0xff, 0x10c512(%rip) # 0x10d8c8
ja 0x13cb
leaq 0x5c45(%rip), %rdi      # 0x7004
leaq 0x5caa(%rip), %rsi      # 0x7070
callq 0x4ae6
addq $-0x10, 0x10c4f5(%rip)  # 0x10d8c8
leaq 0x8cfe(%rip), %rsi      # 0xa0d8
movl $0x3700, %edx           # imm = 0x3700
movl $0x8, %edi
callq 0x6730
cmpq $0x3700, %rax           # imm = 0x3700
je 0x141d
cmpq $-0x1, %rax
jne 0x140a
leaq 0x5c06(%rip), %rdi      # 0x7004
leaq 0x5c09(%rip), %rsi      # 0x700e
callq 0x4a78
leaq 0x5bf3(%rip), %rdi      # 0x7004
leaq 0x5c6b(%rip), %rsi      # 0x7083
callq 0x4ae6
movl $0x8, %edi
callq 0x1090
movq 0x10c492(%rip), %rsi    # 0x10d8c0
leaq 0xc3a3(%rip), %rdi      # 0xd7d8
leaq 0x8c9c(%rip), %rdx      # 0xa0d8
callq 0x5aca
testl %eax, %eax
je 0x1456
leaq 0xc38c(%rip), %rdi      # 0xd7d8
movl $0x40, %esi
callq 0x6668
leaq 0x8c7b(%rip), %rdi      # 0xa0d8
movl $0x3700, %esi           # imm = 0x3700
callq 0x6668
leaq 0x10c3ea(%rip), %rbx    # 0x10d858
leaq 0xc363(%rip), %r14      # 0xd7d8
leaq 0xc37c(%rip), %rdx      # 0xd7f8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1cf4
movl $0x40, %esi
movq %r14, %rdi
callq 0x6668
xorps %xmm0, %xmm0
movups %xmm0, 0xc3aa(%rip)     # 0xd848
movups %xmm0, 0xc393(%rip)     # 0xd838
movups %xmm0, 0xc37c(%rip)     # 0xd828
movups %xmm0, 0xc365(%rip)     # 0xd818
leaq 0xc35e(%rip), %r14      # 0xd818
movl $0x40, %ecx
movq %rbx, %rdi
movq %r14, %rsi
movq %r14, %rdx
callq 0x1d40
movq 0x10c3ec(%rip), %rsi    # 0x10d8c0
movq 0x10c3ed(%rip), %rdx    # 0x10d8c8
leaq (%rsi,%rdx), %rdi
movq %r14, %rcx
callq 0x662c
testl %eax, %eax
je 0x14fe
leaq 0x5b12(%rip), %rdi      # 0x7004
leaq 0x5bb2(%rip), %rsi      # 0x70ab
callq 0x4ae6
leaq 0xc313(%rip), %rdi      # 0xd818
movl $0x40, %esi
callq 0x6668
addq $0xf0, 0x10c3a6(%rip) # 0x10d8c0
movq $-0xf0, %rcx
addq 0x10c3a0(%rip), %rcx    # 0x10d8c8
movq %rcx, 0x10c399(%rip)    # 0x10d8c8
cmpq $0x100000, %rcx         # imm = 0x100000
jb 0x15bd
leaq 0x10c315(%rip), %rbx    # 0x10d858
leaq 0xc30e(%rip), %r14      # 0xd858
leaq 0x5ab3(%rip), %r15      # 0x7004
leaq 0x5b65(%rip), %r12      # 0x70bd
movq $-0x100000, %r13        # imm = 0xFFF00000
movq 0x10c35a(%rip), %rdx    # 0x10d8c0
movl $0x100000, %ecx         # imm = 0x100000
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1d40
movl $0x100000, %edx         # imm = 0x100000
movl $0x1, %edi
movq %r14, %rsi
callq 0x6d14
cmpl $-0x1, %eax
jne 0x1598
movq %r15, %rdi
movq %r12, %rsi
callq 0x4a78
movq 0x10c329(%rip), %rcx    # 0x10d8c8
addq %r13, %rcx
movq %rcx, 0x10c31f(%rip)    # 0x10d8c8
addq $0x100000, 0x10c30c(%rip) # imm = 0x100000
cmpq $0xfffff, %rcx          # imm = 0xFFFFF
ja 0x155f
testq %rcx, %rcx
je 0x160b
movq 0x10c2f7(%rip), %rdx    # 0x10d8c0
leaq 0x10c288(%rip), %rdi    # 0x10d858
leaq 0xc281(%rip), %rbx      # 0xd858
movq %rbx, %rsi
callq 0x1d40
movq 0x10c2e2(%rip), %rdx    # 0x10d8c8
movl $0x1, %edi
movq %rbx, %rsi
callq 0x6d14
cmpl $-0x1, %eax
jne 0x160b
leaq 0x5a05(%rip), %rdi      # 0x7004
leaq 0x5ab7(%rip), %rsi      # 0x70bd
callq 0x4a78
movl $0x1, %edi
callq 0x5058
cmpl $-0x1, %eax
jne 0x162d
leaq 0x59e3(%rip), %rdi      # 0x7004
leaq 0x5a95(%rip), %rsi      # 0x70bd
callq 0x4a78
xorl %edi, %edi
callq 0x4b2c
xorl %eax, %eax
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop