#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_SET;
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *readRotateXOR(int /*<<< orphan*/ *, int, int);
void unpackSave() {
  FILE *file;
  unsigned char *data;
  unsigned char magic[4];
  file = fopen("game1", "rb");
  fseek(file, 0x253c5, SEEK_SET);
  fread(magic, 1, 4, file);
  if (magic[0] != 'm' || magic[1] != 's' || magic[2] != 'q')
    die("Invalid file: game1\n");
  data = readRotateXOR(file, 4608, 0x800);
  fclose(file);
  file = fopen("save", "wb");
  fwrite(data, 1, 4608, file);
  fclose(file);
  free(data);
}


x86 assembly (clang, -O0):
<unpackSave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe71(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
mov    $0x253c5,%esi
call   1070 <fseek@plt>
lea    -0x14(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1050 <fread@plt>
movzbl -0x14(%rbp),%eax
cmp    $0x6d,%eax
jne    11f4 <unpackSave+0x74>
movzbl -0x13(%rbp),%eax
cmp    $0x73,%eax
jne    11f4 <unpackSave+0x74>
movzbl -0x12(%rbp),%eax
cmp    $0x71,%eax
je     1200 <unpackSave+0x80>
lea    0xe0e(%rip),%rdi
call   1090 <die@plt>
mov    -0x8(%rbp),%rdi
mov    $0x1200,%esi
mov    $0x800,%edx
call   1040 <readRotateXOR@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fclose@plt>
lea    0xdf7(%rip),%rdi
lea    0xdf5(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1200,%edx
call   10a0 <fwrite@plt>
mov    -0x8(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unpackSave>:
push   %r14
push   %rbx
push   %rax
lea    0xe75(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x253c5,%esi
call   1070 <fseek@plt>
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmpb   $0x6d,0x4(%rsp)
jne    11dc <unpackSave+0x5c>
cmpb   $0x73,0x5(%rsp)
jne    11dc <unpackSave+0x5c>
cmpb   $0x71,0x6(%rsp)
je     11e8 <unpackSave+0x68>
lea    0xe26(%rip),%rdi
call   1090 <die@plt>
mov    %rbx,%rdi
mov    $0x1200,%esi
mov    $0x800,%edx
call   1040 <readRotateXOR@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xe12(%rip),%rdi
lea    0xe10(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1200,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"game1\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_SET = global i32 0
@.str.2 = [21 x i8] c"Invalid file: game1\0A\00"
@.str.3 = [5 x i8] c"save\00"
@.str.4 = [3 x i8] c"wb\00"
define void @unpackSave() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca [4 x i8]
%4 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = load i32, i32* @SEEK_SET
%7 = call i32 @fseek(i32* %5, i32 152517, i32 %6)
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%9 = load i32*, i32** %1
%10 = call i32 @fread(i8* %8, i32 1, i32 4, i32* %9)
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = icmp ne i32 %13, 109
br i1 %14, label %25, label %15
15: ; preds = %0
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = icmp ne i32 %18, 115
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = icmp ne i32 %23, 113
br i1 %24, label %25, label %27
25: ; preds = %20, %15, %0
%26 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = load i32*, i32** %1
%29 = call i8* @readRotateXOR(i32* %28, i32 4608, i32 2048)
store i8* %29, i8** %2
%30 = load i32*, i32** %1
%31 = call i32 @fclose(i32* %30)
%32 = call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %32, i32** %1
%33 = load i8*, i8** %2
%34 = load i32*, i32** %1
%35 = call i32 @fwrite(i8* %33, i32 1, i32 4608, i32* %34)
%36 = load i32*, i32** %1
%37 = call i32 @fclose(i32* %36)
%38 = load i8*, i8** %2
%39 = call i32 @free(i8* %38)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"game1\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_SET = global i32 0
@.str.2 = [21 x i8] c"Invalid file: game1\0A\00"
@.str.3 = [5 x i8] c"save\00"
@.str.4 = [3 x i8] c"wb\00"
define void @unpackSave() {
%1 = alloca [4 x i8]
%2 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @fseek(i32* %3, i32 152517, i32 %4)
%6 = call i32 @fread(i8* %2, i32 1, i32 4, i32* %3)
%7 = load i8, i8* %2
%8 = icmp ne i8 %7, 109
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 1
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 115
%12 = select i1 %8, i1 true, i1 %11
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 2
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 113
%16 = select i1 %12, i1 true, i1 %15
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = tail call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %0, %17
%20 = tail call i8* @readRotateXOR(i32* %3, i32 4608, i32 2048)
%21 = tail call i32 @fclose(i32* %3)
%22 = tail call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%23 = tail call i32 @fwrite(i8* %20, i32 1, i32 4608, i32* %22)
%24 = tail call i32 @fclose(i32* %22)
%25 = tail call i32 @free(i8* %20)
ret void
}