#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ bl_decrypt(char *, unsigned short);
int /*<<< orphan*/ bl_encrypt(char *, unsigned short);
int /*<<< orphan*/ fail_stream();
int /*<<< orphan*/ fail_usage();
size_t fread(char *, int, unsigned short, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freopen(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  size_t readcount;
  unsigned short int ioblocksize;
  int mode, i;
  unsigned short int key;
  char *buf;
  buf = malloc(8 * sizeof(char));
  if (argc < 3)
    fail_usage();
  if (strcmp(argv[1], "--decrypt") == 0) {
    mode = 1;
    ioblocksize = 8;
  } else if (strcmp(argv[1], "--encrypt") == 0) {
    mode = 2;
    ioblocksize = 7;
  } else
    fail_usage();
  key = ((unsigned short int)*argv[2]) % 16;
  if (!freopen(NULL, "rb", stdin))
    fail_stream();
  while ((readcount = fread(buf, sizeof(char), ioblocksize, stdin)) > 0) {
    if (mode == 1) {
      bl_decrypt(buf, key);
      fwrite(buf, sizeof(char), (size_t)buf[7], stdout);
    } else if (mode == 2) {
      if (readcount < ioblocksize)
        for (i = readcount; i < 7; ++i)
          buf[i] = '!';
      buf[7] = (char)readcount;
      bl_encrypt(buf, key);
      fwrite(buf, sizeof(char), 8, stdout);
    }
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x8,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11d5 <main+0x35>
mov    $0x0,%al
call   10c0 <fail_usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1c(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1205 <main+0x65>
movl   $0x1,-0x20(%rbp)
movw   $0x8,-0x1a(%rbp)
jmp    1241 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf6(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1235 <main+0x95>
movl   $0x2,-0x20(%rbp)
movw   $0x7,-0x1a(%rbp)
jmp    123c <main+0x9c>
mov    $0x0,%al
call   10c0 <fail_usage@plt>
jmp    1241 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
movzwl %ax,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %dx,%ax
mov    %ax,-0x26(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    0xda2(%rip),%rsi
call   1070 <freopen@plt>
cmp    $0x0,%eax
jne    1287 <main+0xe7>
mov    $0x0,%al
call   1060 <fail_stream@plt>
jmp    128c <main+0xec>
mov    -0x30(%rbp),%rdi
mov    -0x1a(%rbp),%ax
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%ecx
mov    $0x1,%esi
movzwl %ax,%edx
call   1040 <fread@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jbe    1381 <main+0x1e1>
cmpl   $0x1,-0x20(%rbp)
jne    12f6 <main+0x156>
mov    -0x30(%rbp),%rdi
movzwl -0x26(%rbp),%esi
call   10a0 <bl_decrypt@plt>
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rax
movsbq 0x7(%rax),%rax
mov    %eax,%edx
mov    0x2cf3(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
jmp    137c <main+0x1dc>
cmpl   $0x2,-0x20(%rbp)
jne    1377 <main+0x1d7>
mov    -0x18(%rbp),%rax
movzwl -0x1a(%rbp),%ecx
cmp    %rcx,%rax
jae    1341 <main+0x1a1>
mov    -0x18(%rbp),%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x7,-0x24(%rbp)
jge    133c <main+0x19c>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x21,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1318 <main+0x178>
jmp    1341 <main+0x1a1>
mov    -0x18(%rbp),%rax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %cl,0x7(%rax)
mov    -0x30(%rbp),%rdi
movzwl -0x26(%rbp),%esi
call   1050 <bl_encrypt@plt>
mov    -0x30(%rbp),%rdi
mov    0x2c72(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x8,%edx
call   10b0 <fwrite@plt>
jmp    137c <main+0x1dc>
jmp    128c <main+0xec>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x8,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
cmp    $0x2,%ebp
jg     11d9 <main+0x29>
xor    %eax,%eax
call   10d0 <fail_usage@plt>
mov    0x8(%r14),%rdi
lea    0xe1c(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1210 <main+0x60>
mov    0x8(%r14),%rdi
lea    0xe11(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1221 <main+0x71>
xor    %eax,%eax
call   10d0 <fail_usage@plt>
mov    %eax,0x4(%rsp)
jmp    122e <main+0x7e>
mov    $0x8,%bp
mov    $0x1,%r12b
movl   $0x0,0x4(%rsp)
jmp    122e <main+0x7e>
mov    $0x7,%bp
mov    $0x1,%al
mov    %eax,0x4(%rsp)
xor    %r12d,%r12d
mov    0x10(%r14),%rax
movzbl (%rax),%r15d
mov    0x2dab(%rip),%r13
mov    0x0(%r13),%edx
lea    0xdcc(%rip),%rsi
xor    %edi,%edi
call   1080 <freopen@plt>
test   %eax,%eax
jne    125a <main+0xaa>
xor    %eax,%eax
call   1060 <fail_stream@plt>
mov    0x0(%r13),%ecx
movzwl %bp,%r14d
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
call   1040 <fread@plt>
test   %rax,%rax
je     131a <main+0x16a>
mov    %rax,%rbp
and    $0xf,%r15d
movzwl %r15w,%r15d
jmp    12d1 <main+0x121>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15d,%esi
call   10b0 <bl_decrypt@plt>
movsbl 0x7(%rbx),%edx
mov    0x2d32(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    0x0(%r13),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
call   1040 <fread@plt>
mov    %rax,%rbp
test   %rax,%rax
je     131a <main+0x16a>
test   $0x1,%r12b
jne    1290 <main+0xe0>
testb  $0x1,0x4(%rsp)
je     12b5 <main+0x105>
cmp    %r14,%rbp
jae    1304 <main+0x154>
cmp    $0x6,%ebp
jg     1304 <main+0x154>
movslq %ebp,%rax
lea    (%rbx,%rax,1),%rdi
mov    $0x6,%edx
sub    %eax,%edx
add    $0x1,%rdx
mov    $0x21,%esi
call   1070 <memset@plt>
mov    %bpl,0x7(%rbx)
mov    %rbx,%rdi
mov    %r15d,%esi
call   1050 <bl_encrypt@plt>
mov    $0x8,%edx
jmp    129f <main+0xef>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"--decrypt\00"
@.str.1 = [10 x i8] c"--encrypt\00"
@.str.2 = [3 x i8] c"rb\00"
@stdin = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i16
%8 = alloca i32
%9 = alloca i32
%10 = alloca i16
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i8* @malloc(i32 8)
store i8* %12, i8** %11
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (...) @fail_usage()
br label %17
17: ; preds = %15, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 1, i32* %8
store i16 8, i16* %7
br label %34
24: ; preds = %17
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 2, i32* %8
store i16 7, i16* %7
br label %33
31: ; preds = %24
%32 = call i32 (...) @fail_usage()
br label %33
33: ; preds = %31, %30
br label %34
34: ; preds = %33, %23
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i16
%40 = zext i16 %39 to i32
%41 = srem i32 %40, 16
%42 = trunc i32 %41 to i16
store i16 %42, i16* %10
%43 = load i32, i32* @stdin
%44 = call i32 @freopen(i32* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %34
%47 = call i32 (...) @fail_stream()
br label %48
48: ; preds = %46, %34
br label %49
49: ; preds = %105, %48
%50 = load i8*, i8** %11
%51 = load i16, i16* %7
%52 = load i32, i32* @stdin
%53 = call i64 @fread(i8* %50, i32 1, i16 zeroext %51, i32 %52)
store i64 %53, i64* %6
%54 = icmp ugt i64 %53, 0
br i1 %54, label %55, label %106
55: ; preds = %49
%56 = load i32, i32* %8
%57 = icmp eq i32 %56, 1
br i1 %57, label %58, label %70
58: ; preds = %55
%59 = load i8*, i8** %11
%60 = load i16, i16* %10
%61 = call i32 @bl_decrypt(i8* %59, i16 zeroext %60)
%62 = load i8*, i8** %11
%63 = load i8*, i8** %11
%64 = getelementptr inbounds i8, i8* %63, i64 7
%65 = load i8, i8* %64
%66 = sext i8 %65 to i64
%67 = trunc i64 %66 to i32
%68 = load i32, i32* @stdout
%69 = call i32 @fwrite(i8* %62, i32 1, i32 %67, i32 %68)
br label %105
70: ; preds = %55
%71 = load i32, i32* %8
%72 = icmp eq i32 %71, 2
br i1 %72, label %73, label %104
73: ; preds = %70
%74 = load i64, i64* %6
%75 = load i16, i16* %7
%76 = zext i16 %75 to i64
%77 = icmp ult i64 %74, %76
br i1 %77, label %78, label %93
78: ; preds = %73
%79 = load i64, i64* %6
%80 = trunc i64 %79 to i32
store i32 %80, i32* %9
br label %81
81: ; preds = %89, %78
%82 = load i32, i32* %9
%83 = icmp slt i32 %82, 7
br i1 %83, label %84, label %92
84: ; preds = %81
%85 = load i8*, i8** %11
%86 = load i32, i32* %9
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %85, i64 %87
store i8 33, i8* %88
br label %89
89: ; preds = %84
%90 = load i32, i32* %9
%91 = add nsw i32 %90, 1
store i32 %91, i32* %9
br label %81
92: ; preds = %81
br label %93
93: ; preds = %92, %73
%94 = load i64, i64* %6
%95 = trunc i64 %94 to i8
%96 = load i8*, i8** %11
%97 = getelementptr inbounds i8, i8* %96, i64 7
store i8 %95, i8* %97
%98 = load i8*, i8** %11
%99 = load i16, i16* %10
%100 = call i32 @bl_encrypt(i8* %98, i16 zeroext %99)
%101 = load i8*, i8** %11
%102 = load i32, i32* @stdout
%103 = call i32 @fwrite(i8* %101, i32 1, i32 8, i32 %102)
br label %104
104: ; preds = %93, %70
br label %105
105: ; preds = %104, %58
br label %49
106: ; preds = %49
%107 = load i8*, i8** %11
%108 = call i32 @free(i8* %107)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"--decrypt\00"
@.str.1 = [10 x i8] c"--encrypt\00"
@.str.2 = [3 x i8] c"rb\00"
@stdin = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @fail_usage()
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %7
%13 = load i8*, i8** %8
%14 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 (...) @fail_usage()
br label %18
18: ; preds = %12, %7, %16
%19 = phi i1 [ undef, %16 ], [ true, %7 ], [ false, %12 ]
%20 = phi i1 [ undef, %16 ], [ false, %7 ], [ true, %12 ]
%21 = phi i16 [ undef, %16 ], [ 8, %7 ], [ 7, %12 ]
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = load i8, i8* %23
%25 = and i8 %24, 15
%26 = zext i8 %25 to i16
%27 = load i32, i32* @stdin
%28 = tail call i32 @freopen(i32* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = tail call i32 (...) @fail_stream()
br label %32
32: ; preds = %30, %18
%33 = load i32, i32* @stdin
%34 = tail call i64 @fread(i8* %3, i32 1, i16 zeroext %21, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %69, label %36
36: ; preds = %32
%37 = zext i16 %21 to i64
%38 = getelementptr inbounds i8, i8* %3, i64 7
br label %39
39: ; preds = %36, %65
%40 = phi i64 [ %34, %36 ], [ %67, %65 ]
br i1 %19, label %41, label %45
41: ; preds = %39
%42 = tail call i32 @bl_decrypt(i8* %3, i16 zeroext %26)
%43 = load i8, i8* %38
%44 = sext i8 %43 to i32
br label %61
45: ; preds = %39
br i1 %20, label %46, label %65
46: ; preds = %45
%47 = icmp ult i64 %40, %37
%48 = trunc i64 %40 to i32
%49 = icmp slt i32 %48, 7
%50 = and i1 %47, %49
br i1 %50, label %51, label %58
51: ; preds = %46
%52 = shl i64 %40, 32
%53 = ashr exact i64 %52, 32
%54 = getelementptr i8, i8* %3, i64 %53
%55 = sub i64 6, %40
%56 = and i64 %55, 4294967295
%57 = add nuw nsw i64 %56, 1
br label %58
58: ; preds = %51, %46
%59 = trunc i64 %40 to i8
store i8 %59, i8* %38
%60 = tail call i32 @bl_encrypt(i8* %3, i16 zeroext %26)
br label %61
61: ; preds = %41, %58
%62 = phi i32 [ 8, %58 ], [ %44, %41 ]
%63 = load i32, i32* @stdout
%64 = tail call i32 @fwrite(i8* %3, i32 1, i32 %62, i32 %63)
br label %65
65: ; preds = %61, %45
%66 = load i32, i32* @stdin
%67 = tail call i64 @fread(i8* %3, i32 1, i16 zeroext %21, i32 %66)
%68 = icmp eq i64 %67, 0
br i1 %68, label %69, label %39
69: ; preds = %65, %32
%70 = tail call i32 @free(i8* %3)
ret i32 0
}