#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
FILE *decrypt_tsc(const char *filename) {
  long fileSize;
  unsigned char key;
  FILE *infile, *outfile;
  if ((infile = fopen(filename, "rb")) == NULL) {
    printf("Failed to open input file.\n");
    exit(1);
  }
  if ((outfile = fopen("tscomp.tmp", "wb")) == NULL) {
    printf("Failed to create temporary file.\n");
    exit(1);
  }
  fseek(infile, 0, SEEK_END);
  fileSize = ftell(infile);
  fseek(infile, fileSize / 2, SEEK_SET);
  fread(&key, 1, 1, infile);
  fseek(infile, 0, SEEK_SET);
  for (long i = 0; i < fileSize; i++) {
    unsigned char c;
    fread(&c, 1, 1, infile);
    if (i != fileSize / 2)
      c -= key;
    fwrite(&c, 1, 1, outfile);
  }
  fclose(infile);
  fclose(outfile);
  return fopen("tscomp.tmp", "rb");
}


x86 assembly (clang, -O0):
<decrypt_tsc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c0 <decrypt_tsc+0x40>
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe58(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11f7 <decrypt_tsc+0x77>
lea    0xe45(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,%rsi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    12e9 <decrypt_tsc+0x169>
mov    -0x20(%rbp),%rcx
lea    -0x31(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
je     12c4 <decrypt_tsc+0x144>
movzbl -0x11(%rbp),%ecx
movzbl -0x31(%rbp),%eax
sub    %ecx,%eax
mov    %al,-0x31(%rbp)
mov    -0x28(%rbp),%rcx
lea    -0x31(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10a0 <fwrite@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    126f <decrypt_tsc+0xef>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd1d(%rip),%rdi
lea    0xcf7(%rip),%rsi
call   1080 <fopen@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decrypt_tsc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe6e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12b0 <decrypt_tsc+0x130>
mov    %rax,%r15
lea    0xe59(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12b9 <decrypt_tsc+0x139>
mov    %rax,%r14
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
mov    %rax,%rbx
shr    $0x3f,%rbx
add    %rax,%rbx
sar    %rbx
mov    0x2de4(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <fseek@plt>
lea    0x7(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    0x0(%rbp),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %r12,%r12
jle    127f <decrypt_tsc+0xff>
movzbl 0x7(%rsp),%ebp
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x6(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %r13,%rbx
movzbl 0x6(%rsp),%eax
mov    %ebp,%ecx
mov    $0x0,%edx
cmove  %edx,%ecx
sub    %cl,%al
movsbl %al,%edi
mov    %r14,%rsi
call   1060 <fputc@plt>
add    $0x1,%r13
cmp    %r13,%r12
jne    1240 <decrypt_tsc+0xc0>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xd6d(%rip),%rdi
lea    0xd63(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <fopen@plt>
lea    0xd7b(%rip),%rdi
jmp    12c0 <decrypt_tsc+0x140>
lea    0xd51(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"Failed to open input file.\0A\00"
@.str.2 = [11 x i8] c"tscomp.tmp\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [34 x i8] c"Failed to create temporary file.\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32* @decrypt_tsc(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %5
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %16, i32** %6
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = load i32*, i32** %5
%23 = load i32, i32* @SEEK_END
%24 = call i32 @fseek(i32* %22, i64 0, i32 %23)
%25 = load i32*, i32** %5
%26 = call i64 @ftell(i32* %25)
store i64 %26, i64* %3
%27 = load i32*, i32** %5
%28 = load i64, i64* %3
%29 = sdiv i64 %28, 2
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @fseek(i32* %27, i64 %29, i32 %30)
%32 = load i32*, i32** %5
%33 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %32)
%34 = load i32*, i32** %5
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @fseek(i32* %34, i64 0, i32 %35)
store i64 0, i64* %7
br label %37
37: ; preds = %58, %21
%38 = load i64, i64* %7
%39 = load i64, i64* %3
%40 = icmp slt i64 %38, %39
br i1 %40, label %41, label %61
41: ; preds = %37
%42 = load i32*, i32** %5
%43 = call i32 @fread(i8* %8, i32 1, i32 1, i32* %42)
%44 = load i64, i64* %7
%45 = load i64, i64* %3
%46 = sdiv i64 %45, 2
%47 = icmp ne i64 %44, %46
br i1 %47, label %48, label %55
48: ; preds = %41
%49 = load i8, i8* %4
%50 = zext i8 %49 to i32
%51 = load i8, i8* %8
%52 = zext i8 %51 to i32
%53 = sub nsw i32 %52, %50
%54 = trunc i32 %53 to i8
store i8 %54, i8* %8
br label %55
55: ; preds = %48, %41
%56 = load i32*, i32** %6
%57 = call i32 @fwrite(i8* %8, i32 1, i32 1, i32* %56)
br label %58
58: ; preds = %55
%59 = load i64, i64* %7
%60 = add nsw i64 %59, 1
store i64 %60, i64* %7
br label %37
61: ; preds = %37
%62 = load i32*, i32** %5
%63 = call i32 @fclose(i32* %62)
%64 = load i32*, i32** %6
%65 = call i32 @fclose(i32* %64)
%66 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
ret i32* %66
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.2 = [11 x i8] c"tscomp.tmp\00"
@.str.3 = [3 x i8] c"wb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@str = [33 x i8] c"Failed to create temporary file.\00"
@str.5 = [27 x i8] c"Failed to open input file.\00"
define noalias i32* @decrypt_tsc(i8* nocapture %0) {
%2 = alloca i8
%3 = alloca i8
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @fseek(i32* %4, i64 0, i32 %16)
%18 = tail call i64 @ftell(i32* %4)
%19 = sdiv i64 %18, 2
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @fseek(i32* %4, i64 %19, i32 %20)
%22 = call i32 @fread(i8* %2, i32 1, i32 1, i32* %4)
%23 = load i32, i32* @SEEK_SET
%24 = tail call i32 @fseek(i32* %4, i64 0, i32 %23)
%25 = icmp sgt i64 %18, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = load i8, i8* %2
br label %32
28: ; preds = %32, %15
%29 = tail call i32 @fclose(i32* %4)
%30 = tail call i32 @fclose(i32* %10)
%31 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
ret i32* %31
32: ; preds = %26, %32
%33 = phi i64 [ 0, %26 ], [ %41, %32 ]
%34 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %4)
%35 = icmp eq i64 %33, %19
%36 = load i8, i8* %3
%37 = select i1 %35, i8 0, i8 %27
%38 = sub i8 %36, %37
%39 = sext i8 %38 to i32
%40 = tail call i32 @fputc(i32 %39, i32* %10)
%41 = add nuw nsw i64 %33, 1
%42 = icmp eq i64 %41, %18
br i1 %42, label %28, label %32
}