// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x8, %edi
movl $0x1, %esi
callq 0x8840
cmpl $0x0, %eax
jne 0x1307
leaq 0xdd10(%rip), %rdi      # 0xf004
leaq 0xdd13(%rip), %rsi      # 0xf00e
leaq 0xdd22(%rip), %rdx      # 0xf024
callq 0xa320
callq 0xbcd0
xorl %edi, %edi
leaq -0xa0(%rbp), %rsi
callq 0x1130
cmpl $0x0, %eax
jne 0x137d
movq -0x70(%rbp), %rax
movq %rax, 0x114576(%rip)    # 0x1158a0
movq 0x11456f(%rip), %rsi    # 0x1158a0
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %ecx
xorl %r8d, %r8d
movq %r9, %rdi
movl %ecx, %edx
callq 0x1070
movq %rax, 0x10d61(%rip)     # 0x120b0
movq $-0x1, %rax
cmpq %rax, 0x10d53(%rip)     # 0x120b0
je 0x137b
movq 0x10d4a(%rip), %rax     # 0x120b0
movq %rax, 0x114543(%rip)    # 0x1158b0
movq 0x11452c(%rip), %rax    # 0x1158a0
movq %rax, 0x11453d(%rip)    # 0x1158b8
jmp 0x137d
cmpq $0x0, 0x11452b(%rip)    # 0x1158b0
jne 0x13c7
leaq 0x11452a(%rip), %rdi    # 0x1158b8
xorl %esi, %esi
callq 0xbd20
movq %rax, 0x11450c(%rip)    # 0x1158a8
cmpq $0x0, 0x114504(%rip)    # 0x1158a8
jne 0x13b9
leaq 0xdc57(%rip), %rdi      # 0xf004
leaq 0xdc91(%rip), %rsi      # 0xf045
callq 0xa4a0
movq 0x1144e8(%rip), %rax    # 0x1158a8
movq %rax, 0x1144e9(%rip)    # 0x1158b0
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
cmpq %rax, 0x1144e0(%rip)    # 0x1158b8
jbe 0x13ed
leaq 0xdc23(%rip), %rdi      # 0xf004
leaq 0xdc74(%rip), %rsi      # 0xf05c
callq 0xa520
cmpq $0x100, 0x1144c0(%rip) # imm = 0x100
jae 0x140d
leaq 0xdc03(%rip), %rdi      # 0xf004
leaq 0xdc68(%rip), %rsi      # 0xf070
callq 0xa520
movq 0x1144a4(%rip), %rax    # 0x1158b8
subq $0x10, %rax
movq %rax, 0x114499(%rip)    # 0x1158b8
movl $0x8, %edi
leaq 0x10c9d(%rip), %rsi     # 0x120c8
movl $0x3700, %edx           # imm = 0x3700
callq 0xd5d0
movq %rax, -0xa8(%rbp)
cmpq $-0x1, -0xa8(%rbp)
jne 0x1459
leaq 0xdbb7(%rip), %rdi      # 0xf004
leaq 0xdbba(%rip), %rsi      # 0xf00e
callq 0xa4a0
cmpq $0x3700, -0xa8(%rbp) # imm = 0x3700
je 0x1479
leaq 0xdb97(%rip), %rdi      # 0xf004
leaq 0xdc0f(%rip), %rsi      # 0xf083
callq 0xa520
movl $0x8, %edi
callq 0x1090
movq 0x114426(%rip), %rsi    # 0x1158b0
leaq 0x10c37(%rip), %rdx     # 0x120c8
movq %rdx, %rdi
addq $0x3700, %rdi           # imm = 0x3700
callq 0xbf90
cmpl $0x0, %eax
je 0x14bd
leaq 0x10c1c(%rip), %rdi     # 0x120c8
addq $0x3700, %rdi           # imm = 0x3700
movl $0x40, %esi
callq 0xd500
leaq 0x10c04(%rip), %rdi     # 0x120c8
movl $0x3700, %esi           # imm = 0x3700
callq 0xd500
leaq 0x10bf3(%rip), %rsi     # 0x120c8
movq %rsi, %rdi
addq $0x103780, %rdi         # imm = 0x103780
addq $0x3700, %rsi           # imm = 0x3700
movq %rsi, %rdx
addq $0x20, %rdx
callq 0x22e0
leaq 0x10bcf(%rip), %rdi     # 0x120c8
addq $0x3700, %rdi           # imm = 0x3700
movl $0x40, %esi
callq 0xd500
leaq 0x10bb7(%rip), %rdi     # 0x120c8
addq $0x3740, %rdi           # imm = 0x3740
xorl %esi, %esi
movl $0x40, %edx
callq 0x1080
leaq 0x10b9d(%rip), %rdx     # 0x120c8
movq %rdx, %rdi
addq $0x103780, %rdi         # imm = 0x103780
addq $0x3740, %rdx           # imm = 0x3740
movl $0x40, %ecx
movq %rdx, %rsi
callq 0x2430
movq 0x114360(%rip), %rdi    # 0x1158b0
addq 0x114361(%rip), %rdi    # 0x1158b8
movq 0x114352(%rip), %rsi    # 0x1158b0
movq 0x114353(%rip), %rdx    # 0x1158b8
leaq 0x10b5c(%rip), %rcx     # 0x120c8
addq $0x3740, %rcx           # imm = 0x3740
callq 0xd470
cmpl $0x0, %eax
je 0x1590
leaq 0xda80(%rip), %rdi      # 0xf004
leaq 0xdb20(%rip), %rsi      # 0xf0ab
callq 0xa520
leaq 0x10b31(%rip), %rdi     # 0x120c8
addq $0x3740, %rdi           # imm = 0x3740
movl $0x40, %esi
callq 0xd500
movq 0x114301(%rip), %rax    # 0x1158b0
addq $0xf0, %rax
movq %rax, 0x1142f4(%rip)    # 0x1158b0
movq 0x1142f5(%rip), %rax    # 0x1158b8
subq $0xf0, %rax
movq %rax, 0x1142e8(%rip)    # 0x1158b8
cmpq $0x100000, 0x1142dd(%rip) # imm = 0x100000
jb 0x166c
movq 0x1142c8(%rip), %rdx    # 0x1158b0
leaq 0x10ad9(%rip), %rsi     # 0x120c8
movq %rsi, %rdi
addq $0x103780, %rdi         # imm = 0x103780
addq $0x3780, %rsi           # imm = 0x3780
movl $0x100000, %ecx         # imm = 0x100000
callq 0x2430
movl $0x1, %edi
leaq 0x10ab2(%rip), %rsi     # 0x120c8
addq $0x3780, %rsi           # imm = 0x3780
movl $0x100000, %edx         # imm = 0x100000
callq 0xe140
cmpl $-0x1, %eax
jne 0x163f
leaq 0xd9d1(%rip), %rdi      # 0xf004
leaq 0xda83(%rip), %rsi      # 0xf0bd
callq 0xa4a0
movq 0x114272(%rip), %rax    # 0x1158b8
subq $0x100000, %rax         # imm = 0x100000
movq %rax, 0x114265(%rip)    # 0x1158b8
movq 0x114256(%rip), %rax    # 0x1158b0
addq $0x100000, %rax         # imm = 0x100000
movq %rax, 0x114249(%rip)    # 0x1158b0
jmp 0x15d0
cmpq $0x0, 0x114244(%rip)    # 0x1158b8
jbe 0x16da
movq 0x114233(%rip), %rdx    # 0x1158b0
movq 0x114234(%rip), %rcx    # 0x1158b8
leaq 0x10a3d(%rip), %rsi     # 0x120c8
movq %rsi, %rdi
addq $0x103780, %rdi         # imm = 0x103780
addq $0x3780, %rsi           # imm = 0x3780
callq 0x2430
movq 0x114210(%rip), %rdx    # 0x1158b8
movl $0x1, %edi
leaq 0x10a14(%rip), %rsi     # 0x120c8
addq $0x3780, %rsi           # imm = 0x3780
callq 0xe140
cmpl $-0x1, %eax
jne 0x16d8
leaq 0xd938(%rip), %rdi      # 0xf004
leaq 0xd9ea(%rip), %rsi      # 0xf0bd
callq 0xa4a0
jmp 0x16da
movl $0x1, %edi
callq 0xb2d0
cmpl $-0x1, %eax
jne 0x16fc
leaq 0xd914(%rip), %rdi      # 0xf004
leaq 0xd9c6(%rip), %rsi      # 0xf0bd
callq 0xa4a0
xorl %edi, %edi
callq 0xa570
xorl %eax, %eax
addq $0xb0, %rsp
popq %rbp
retq
nop