#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*/ _Decrypt(unsigned char *, int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int _DecryptFile(const char *filename0, const char *filename1, int lfsr0,
                 int lfsr1) {
  unsigned char buffer[0x800];
  int result, block;
  FILE *file0, *file1;
  result = 1;
  if ((file0 = fopen(filename0, "rb")) == 0) {
    printf("error: can't open input file '%s'\n", filename0);
  } else {
    if ((file1 = fopen(filename1, "wb")) == 0) {
      printf("error: can't create/truncate output file '%s'\n", filename1);
    } else {
      for (block = 0;; block++) {
        if (block % 10 == 0) {
          printf("\rdecrypting, block %d", block);
          fflush(stdout);
        }
        if (fread(buffer, 1, sizeof buffer, file0) != sizeof buffer) {
          result = 0;
          break;
        }
        if (buffer[0x14] & 0x10) {
          _Decrypt(buffer, lfsr0, lfsr1);
          buffer[0x14] &= ~0x10;
        }
        if (fwrite(buffer, 1, sizeof buffer, file1) != sizeof buffer) {
          printf("\nerror: can't write to output file\n");
          break;
        }
      }
      fclose(file1);
      printf("\rdecrypting, done          \n");
    }
    fclose(file0);
  }
  return result;
}


x86 assembly (clang, -O0):
<_DecryptFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x1,-0x824(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x830(%rbp)
cmp    $0x0,%rax
jne    11cb <_DecryptFile+0x5b>
mov    -0x8(%rbp),%rsi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1332 <_DecryptFile+0x1c2>
mov    -0x10(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x838(%rbp)
cmp    $0x0,%rax
jne    1203 <_DecryptFile+0x93>
mov    -0x10(%rbp),%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1326 <_DecryptFile+0x1b6>
movl   $0x0,-0x828(%rbp)
mov    -0x828(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1246 <_DecryptFile+0xd6>
mov    -0x828(%rbp),%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    -0x820(%rbp),%rdi
mov    -0x830(%rbp),%rcx
mov    $0x1,%esi
mov    $0x800,%edx
call   1040 <fread@plt>
cltq
cmp    $0x800,%rax
je     1280 <_DecryptFile+0x110>
movl   $0x0,-0x824(%rbp)
jmp    130c <_DecryptFile+0x19c>
movzbl -0x80c(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     12b5 <_DecryptFile+0x145>
lea    -0x820(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <_Decrypt@plt>
movzbl -0x80c(%rbp),%eax
and    $0xffffffef,%eax
mov    %al,-0x80c(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x838(%rbp),%rcx
mov    $0x1,%esi
mov    $0x800,%edx
call   1090 <fwrite@plt>
cltq
cmp    $0x800,%rax
je     12f3 <_DecryptFile+0x183>
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    130c <_DecryptFile+0x19c>
jmp    12f8 <_DecryptFile+0x188>
mov    -0x828(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x828(%rbp)
jmp    120d <_DecryptFile+0x9d>
mov    -0x838(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x830(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x824(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_DecryptFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %ecx,0xc(%rsp)
mov    %edx,0x8(%rsp)
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe5a(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1289 <_DecryptFile+0x109>
mov    %rax,%r13
lea    0xe68(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12a1 <_DecryptFile+0x121>
mov    %rax,%rbp
mov    $0xcccccccd,%r14d
lea    0x10(%rsp),%rbx
xor    %r15d,%r15d
xor    %r12d,%r12d
jmp    1218 <_DecryptFile+0x98>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x800,%edx
mov    %rbp,%rcx
call   10a0 <fwrite@plt>
add    $0x1,%r12d
add    $0xffffffff,%r15d
cmp    $0x800,%eax
jne    12b9 <_DecryptFile+0x139>
mov    %r12d,%eax
imul   %r14,%rax
shr    $0x23,%rax
lea    (%rax,%rax,4),%eax
add    %eax,%eax
add    %r15d,%eax
jne    124c <_DecryptFile+0xcc>
lea    0xe24(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x800,%edx
mov    %r13,%rcx
call   1050 <fread@plt>
cmp    $0x800,%eax
jne    12cc <_DecryptFile+0x14c>
testb  $0x10,0x24(%rsp)
je     11f0 <_DecryptFile+0x70>
mov    %rbx,%rdi
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
call   1030 <_Decrypt@plt>
andb   $0xef,0x24(%rsp)
jmp    11f0 <_DecryptFile+0x70>
lea    0xd73(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebx
jmp    12ea <_DecryptFile+0x16a>
lea    0xd81(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebx
jmp    12e2 <_DecryptFile+0x162>
lea    0xdae(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
jmp    12ce <_DecryptFile+0x14e>
xor    %ebx,%ebx
mov    %rbp,%rdi
call   1060 <fclose@plt>
lea    0xdb4(%rip),%rdi
call   1040 <puts@plt>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    %ebx,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [35 x i8] c"error: can't open input file '%s'\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [47 x i8] c"error: can't create/truncate output file '%s'\0A\00"
@.str.4 = [22 x i8] c"\0Ddecrypting, block %d\00"
@stdout = global i32 0
@.str.5 = [36 x i8] c"\0Aerror: can't write to output file\0A\00"
@.str.6 = [29 x i8] c"\0Ddecrypting, done \0A\00"
define i32 @_DecryptFile(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2048 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 1, i32* %10
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %12
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i8*, i8** %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %79
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %13
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %76
27: ; preds = %20
store i32 0, i32* %11
br label %28
28: ; preds = %69, %27
%29 = load i32, i32* %11
%30 = srem i32 %29, 10
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* %11
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @stdout
%36 = call i32 @fflush(i32 %35)
br label %37
37: ; preds = %32, %28
%38 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%39 = load i32*, i32** %12
%40 = call i32 @fread(i8* %38, i32 1, i32 2048, i32* %39)
%41 = sext i32 %40 to i64
%42 = icmp ne i64 %41, 2048
br i1 %42, label %43, label %44
43: ; preds = %37
store i32 0, i32* %10
br label %72
44: ; preds = %37
%45 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 20
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = and i32 %47, 16
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %60
50: ; preds = %44
%51 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%52 = load i32, i32* %7
%53 = load i32, i32* %8
%54 = call i32 @_Decrypt(i8* %51, i32 %52, i32 %53)
%55 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 20
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = and i32 %57, -17
%59 = trunc i32 %58 to i8
store i8 %59, i8* %55
br label %60
60: ; preds = %50, %44
%61 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%62 = load i32*, i32** %13
%63 = call i32 @fwrite(i8* %61, i32 1, i32 2048, i32* %62)
%64 = sext i32 %63 to i64
%65 = icmp ne i64 %64, 2048
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %72
68: ; preds = %60
br label %69
69: ; preds = %68
%70 = load i32, i32* %11
%71 = add nsw i32 %70, 1
store i32 %71, i32* %11
br label %28
72: ; preds = %66, %43
%73 = load i32*, i32** %13
%74 = call i32 @fclose(i32* %73)
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %76
76: ; preds = %72, %24
%77 = load i32*, i32** %12
%78 = call i32 @fclose(i32* %77)
br label %79
79: ; preds = %76, %17
%80 = load i32, i32* %10
ret i32 %80
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [35 x i8] c"error: can't open input file '%s'\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [47 x i8] c"error: can't create/truncate output file '%s'\0A\00"
@.str.4 = [22 x i8] c"\0Ddecrypting, block %d\00"
@stdout = global i32 0
@str = [35 x i8] c"\0Aerror: can't write to output file\00"
@str.7 = [28 x i8] c"\0Ddecrypting, done \00"
define i32 @_DecryptFile(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [2048 x i8]
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %50
11: ; preds = %4
%12 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 20
br label %18
16: ; preds = %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %47
18: ; preds = %37, %14
%19 = phi i32 [ %40, %37 ], [ 0, %14 ]
%20 = urem i32 %19, 10
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%24 = load i32, i32* @stdout
%25 = call i32 @fflush(i32 %24)
br label %26
26: ; preds = %22, %18
%27 = call i32 @fread(i8* %6, i32 1, i32 2048, i32* %7)
%28 = icmp eq i32 %27, 2048
br i1 %28, label %29, label %43
29: ; preds = %26
%30 = load i8, i8* %15
%31 = and i8 %30, 16
%32 = icmp eq i8 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = call i32 @_Decrypt(i8* %6, i32 %2, i32 %3)
%35 = load i8, i8* %15
%36 = and i8 %35, -17
store i8 %36, i8* %15
br label %37
37: ; preds = %33, %29
%38 = call i32 @fwrite(i8* %6, i32 1, i32 2048, i32* %12)
%39 = icmp eq i32 %38, 2048
%40 = add nuw nsw i32 %19, 1
br i1 %39, label %18, label %41
41: ; preds = %37
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %26, %41
%44 = phi i32 [ 1, %41 ], [ 0, %26 ]
%45 = call i32 @fclose(i32* %12)
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
br label %47
47: ; preds = %43, %16
%48 = phi i32 [ 1, %16 ], [ %44, %43 ]
%49 = call i32 @fclose(i32* %7)
br label %50
50: ; preds = %47, %9
%51 = phi i32 [ 1, %9 ], [ %48, %47 ]
ret i32 %51
}