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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unshield_deobfuscate(unsigned char *, size_t,
                                        unsigned int *);
int main(int argc, char **argv) {
  unsigned seed = 0;
  FILE *input = NULL;
  FILE *output = NULL;
  size_t size;
  unsigned char buffer[16384];
  if (argc != 3) {
    fprintf(stderr,
            "Syntax:\n"
            "  %s INPUT-FILE OUTPUT-FILE\n",
            argv[0]);
    exit(1);
  }
  input = fopen(argv[1], "r");
  if (!input) {
    fprintf(stderr, "Failed to open %s for reading\n", argv[1]);
    exit(2);
  }
  output = fopen(argv[2], "w");
  if (!output) {
    fprintf(stderr, "Failed to open %s for writing\n", argv[2]);
    exit(3);
  }
  while ((size = fread(buffer, 1, sizeof(buffer), input)) != 0) {
    unshield_deobfuscate(buffer, size, &seed);
    if (fwrite(buffer, 1, size, output) != size) {
      fprintf(stderr, "Failed to write %lu bytes to %s\n", (unsigned long)size,
              argv[2]);
      exit(4);
    }
  }
  fclose(input);
  fclose(output);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11d2 <main+0x62>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe44(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121e <main+0xae>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe19(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126a <main+0xfa>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
jmp    126f <main+0xff>
lea    -0x4030(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4000,%edx
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1300 <main+0x190>
lea    -0x4030(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <unshield_deobfuscate@plt>
lea    -0x4030(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    -0x30(%rbp),%rax
je     12fb <main+0x18b>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xd7d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
jmp    126f <main+0xff>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x4030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4018,%rsp
mov    %rsi,%r14
movl   $0x0,0xc(%rsp)
cmp    $0x3,%edi
jne    126f <main+0xff>
mov    0x8(%r14),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1293 <main+0x123>
mov    %rax,%r15
mov    0x10(%r14),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12b8 <main+0x148>
mov    %rax,%r12
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     124b <main+0xdb>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1050 <unshield_deobfuscate@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1090 <fwrite@plt>
cmp    %rbx,%rax
je     11e0 <main+0x70>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r14),%rcx
lea    0xe30(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x4018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd7e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd80(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r14),%rdx
lea    0xd7c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Syntax:\0A %s INPUT-FILE OUTPUT-FILE\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"Failed to open %s for reading\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [31 x i8] c"Failed to open %s for writing\0A\00"
@.str.5 = [33 x i8] c"Failed to write %lu bytes to %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca [16384 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32* null, i32** %7
store i32* null, i32** %8
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %7
%25 = load i32*, i32** %7
%26 = icmp ne i32* %25, null
br i1 %26, label %34, label %27
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %20
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %38, i32** %8
%39 = load i32*, i32** %8
%40 = icmp ne i32* %39, null
br i1 %40, label %48, label %41
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = load i8*, i8** %44
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 3)
unreachable
48: ; preds = %34
br label %49
49: ; preds = %72, %48
%50 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%51 = load i32*, i32** %7
%52 = call i64 @fread(i8* %50, i32 1, i32 16384, i32* %51)
store i64 %52, i64* %9
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %73
54: ; preds = %49
%55 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%56 = load i64, i64* %9
%57 = call i32 @unshield_deobfuscate(i8* %55, i64 %56, i32* %6)
%58 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%59 = load i64, i64* %9
%60 = load i32*, i32** %8
%61 = call i64 @fwrite(i8* %58, i32 1, i64 %59, i32* %60)
%62 = load i64, i64* %9
%63 = icmp ne i64 %61, %62
br i1 %63, label %64, label %72
64: ; preds = %54
%65 = load i32, i32* @stderr
%66 = load i64, i64* %9
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 2
%69 = load i8*, i8** %68
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %66, i8* %69)
%71 = call i32 @exit(i32 4)
unreachable
72: ; preds = %54
br label %49
73: ; preds = %49
%74 = load i32*, i32** %7
%75 = call i32 @fclose(i32* %74)
%76 = load i32*, i32** %8
%77 = call i32 @fclose(i32* %76)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Syntax:\0A %s INPUT-FILE OUTPUT-FILE\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"Failed to open %s for reading\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [31 x i8] c"Failed to open %s for writing\0A\00"
@.str.5 = [33 x i8] c"Failed to write %lu bytes to %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [16384 x i8]
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 3
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %14
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 2)
unreachable
23: ; preds = %13
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = tail call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %24
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = tail call i32 @exit(i32 3)
unreachable
33: ; preds = %23, %36
%34 = call i64 @fread(i8* %6, i32 1, i32 16384, i32* %16)
%35 = icmp eq i64 %34, 0
br i1 %35, label %45, label %36
36: ; preds = %33
%37 = call i32 @unshield_deobfuscate(i8* %6, i64 %34, i32* %3)
%38 = call i64 @fwrite(i8* %6, i32 1, i64 %34, i32* %26)
%39 = icmp eq i64 %38, %34
br i1 %39, label %33, label %40
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %24
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %34, i8* %42)
%44 = call i32 @exit(i32 4)
unreachable
45: ; preds = %33
%46 = call i32 @fclose(i32* %16)
%47 = call i32 @fclose(i32* %26)
ret i32 0
}