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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int lzma_crc32_le(unsigned char const *, int, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ std_err();
int /*<<< orphan*/ write_output_file(char *, int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  uint8_t decrypt = 0;
  uint8_t encrypt = 0;
  uint32_t buffer_size = 0;
  FILE *pFile;
  printf("\n");
  printf("         De Blob 2 Checksum Resigner tool by Fredric Baeckstrom  \n");
  printf("  ======================================================= version %s "
         "===\n\n",
         "1.0.0");
  if (argc <= 2) {
    printf("Usage: %s <savefile> <output>\n", argv[0]);
    return (-1);
  }
  char *file_input = argv[1];
  char *file_output = argv[2];
  printf("- Open input file:    %s\n", file_input);
  pFile = fopen(file_input, "rb");
  if (!pFile)
    std_err();
  fseek(pFile, 0L, SEEK_END);
  buffer_size = ftell(pFile);
  fseek(pFile, 0L, SEEK_SET);
  char *buffer = (char *)malloc(buffer_size);
  memset(buffer, 0, buffer_size);
  fread(buffer, 1, buffer_size, pFile);
  fclose(pFile);
  printf("- Data size: 0x%08x  (%u)\n", buffer_size, buffer_size);
  buffer[8] = 0x00;
  buffer[9] = 0x00;
  buffer[10] = 0x00;
  buffer[11] = 0x00;
  uint32_t new_checksum =
      lzma_crc32_le((const unsigned char *)buffer, buffer_size, 0xFFFFFFFF);
  printf("- New Checksum: 0x%08x  (%u)\n", new_checksum, new_checksum);
  buffer[8] = ((new_checksum >> 24) & 0xff);
  buffer[9] = ((new_checksum >> 16) & 0xff);
  buffer[10] = ((new_checksum >> 8) & 0xff);
  buffer[11] = (new_checksum & 0xff);
  write_output_file((char *)file_output, 0, (char *)buffer, buffer_size);
  printf("\n- Writing data to file, done.\n");
  free(buffer);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe35(%rip),%rdi
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jg     1247 <main+0x87>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d9 <main+0x219>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1297 <main+0xd7>
mov    $0x0,%al
call   10e0 <std_err@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1090 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%rax
movb   $0x0,0x8(%rax)
mov    -0x40(%rbp),%rax
movb   $0x0,0x9(%rax)
mov    -0x40(%rbp),%rax
movb   $0x0,0xa(%rax)
mov    -0x40(%rbp),%rax
movb   $0x0,0xb(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0xffffffff,%edx
call   10d0 <lzma_crc32_le@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%esi
mov    -0x44(%rbp),%edx
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x44(%rbp),%eax
sar    $0x18,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x40(%rbp),%rax
mov    %cl,0x8(%rax)
mov    -0x44(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x40(%rbp),%rax
mov    %cl,0x9(%rax)
mov    -0x44(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x40(%rbp),%rax
mov    %cl,0xa(%rax)
mov    -0x44(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x40(%rbp),%rax
mov    %cl,0xb(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
xor    %esi,%esi
call   1080 <write_output_file@plt>
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%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,%rbx
mov    %edi,%ebp
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0xec5(%rip),%rdi
call   1050 <puts@plt>
lea    0xdf3(%rip),%rdi
lea    0xe37(%rip),%rsi
xor    %eax,%eax
call   1080 <printf@plt>
cmp    $0x2,%ebp
jg     123c <main+0x5c>
mov    (%rbx),%rsi
lea    0xe27(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xffffffff,%r15d
jmp    1342 <main+0x162>
mov    0x8(%rbx),%rbp
mov    0x10(%rbx),%r14
lea    0xe25(%rip),%rdi
xor    %r15d,%r15d
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xe2b(%rip),%rsi
mov    %rbp,%rdi
call   10e0 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1276 <main+0x96>
xor    %eax,%eax
call   1100 <std_err@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r12,%rdi
call   10b0 <ftell@plt>
mov    %eax,%r13d
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r13d,%edi
call   10c0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   1090 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %r12,%rcx
call   1060 <fread@plt>
mov    %r12,%rdi
call   1070 <fclose@plt>
lea    0xdac(%rip),%rdi
mov    %r13d,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1080 <printf@plt>
movl   $0x0,0x8(%rbx)
mov    %rbx,%rdi
mov    %r13d,%esi
mov    $0xffffffff,%edx
call   10f0 <lzma_crc32_le@plt>
mov    %eax,%ebp
lea    0xd9a(%rip),%rdi
mov    %eax,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
bswap  %ebp
mov    %ebp,0x8(%rbx)
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r13d,%ecx
call   10a0 <write_output_file@plt>
lea    0xdd3(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [67 x i8] c" De Blob 2 Checksum Resigner tool by Fredric Baeckstrom \0A\00"
@.str.2 = [75 x i8] c" ======================================================= version %s ===\0A\0A\00"
@.str.3 = [6 x i8] c"1.0.0\00"
@.str.4 = [31 x i8] c"Usage: %s <savefile> <output>\0A\00"
@.str.5 = [26 x i8] c"- Open input file: %s\0A\00"
@.str.6 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.7 = [27 x i8] c"- Data size: 0x%08x (%u)\0A\00"
@.str.8 = [30 x i8] c"- New Checksum: 0x%08x (%u)\0A\00"
@.str.9 = [32 x i8] c"\0A- Writing data to file, done.\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 i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%17 = load i32, i32* %4
%18 = icmp sle i32 %17, 2
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %22)
store i32 -1, i32* %3
br label %107
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
store i8* %27, i8** %10
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
store i8* %30, i8** %11
%31 = load i8*, i8** %10
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %10
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %34, i32** %9
%35 = load i32*, i32** %9
%36 = icmp ne i32* %35, null
br i1 %36, label %39, label %37
37: ; preds = %24
%38 = call i32 (...) @std_err()
br label %39
39: ; preds = %37, %24
%40 = load i32*, i32** %9
%41 = load i32, i32* @SEEK_END
%42 = call i32 @fseek(i32* %40, i64 0, i32 %41)
%43 = load i32*, i32** %9
%44 = call i32 @ftell(i32* %43)
store i32 %44, i32* %8
%45 = load i32*, i32** %9
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @fseek(i32* %45, i64 0, i32 %46)
%48 = load i32, i32* %8
%49 = call i64 @malloc(i32 %48)
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %12
%51 = load i8*, i8** %12
%52 = load i32, i32* %8
%53 = call i32 @memset(i8* %51, i32 0, i32 %52)
%54 = load i8*, i8** %12
%55 = load i32, i32* %8
%56 = load i32*, i32** %9
%57 = call i32 @fread(i8* %54, i32 1, i32 %55, i32* %56)
%58 = load i32*, i32** %9
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* %8
%61 = load i32, i32* %8
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %60, i32 %61)
%63 = load i8*, i8** %12
%64 = getelementptr inbounds i8, i8* %63, i64 8
store i8 0, i8* %64
%65 = load i8*, i8** %12
%66 = getelementptr inbounds i8, i8* %65, i64 9
store i8 0, i8* %66
%67 = load i8*, i8** %12
%68 = getelementptr inbounds i8, i8* %67, i64 10
store i8 0, i8* %68
%69 = load i8*, i8** %12
%70 = getelementptr inbounds i8, i8* %69, i64 11
store i8 0, i8* %70
%71 = load i8*, i8** %12
%72 = load i32, i32* %8
%73 = call i32 @lzma_crc32_le(i8* %71, i32 %72, i32 -1)
store i32 %73, i32* %13
%74 = load i32, i32* %13
%75 = load i32, i32* %13
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i32 %74, i32 %75)
%77 = load i32, i32* %13
%78 = ashr i32 %77, 24
%79 = and i32 %78, 255
%80 = trunc i32 %79 to i8
%81 = load i8*, i8** %12
%82 = getelementptr inbounds i8, i8* %81, i64 8
store i8 %80, i8* %82
%83 = load i32, i32* %13
%84 = ashr i32 %83, 16
%85 = and i32 %84, 255
%86 = trunc i32 %85 to i8
%87 = load i8*, i8** %12
%88 = getelementptr inbounds i8, i8* %87, i64 9
store i8 %86, i8* %88
%89 = load i32, i32* %13
%90 = ashr i32 %89, 8
%91 = and i32 %90, 255
%92 = trunc i32 %91 to i8
%93 = load i8*, i8** %12
%94 = getelementptr inbounds i8, i8* %93, i64 10
store i8 %92, i8* %94
%95 = load i32, i32* %13
%96 = and i32 %95, 255
%97 = trunc i32 %96 to i8
%98 = load i8*, i8** %12
%99 = getelementptr inbounds i8, i8* %98, i64 11
store i8 %97, i8* %99
%100 = load i8*, i8** %11
%101 = load i8*, i8** %12
%102 = load i32, i32* %8
%103 = call i32 @write_output_file(i8* %100, i32 0, i8* %101, i32 %102)
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.9, i64 0, i64 0))
%105 = load i8*, i8** %12
%106 = call i32 @free(i8* %105)
br label %107
107: ; preds = %39, %19
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@.str.2 = [75 x i8] c" ======================================================= version %s ===\0A\0A\00"
@.str.3 = [6 x i8] c"1.0.0\00"
@.str.4 = [31 x i8] c"Usage: %s <savefile> <output>\0A\00"
@.str.5 = [26 x i8] c"- Open input file: %s\0A\00"
@.str.6 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.7 = [27 x i8] c"- Data size: 0x%08x (%u)\0A\00"
@.str.8 = [30 x i8] c"- New Checksum: 0x%08x (%u)\0A\00"
@str = [66 x i8] c" De Blob 2 Checksum Resigner tool by Fredric Baeckstrom \00"
@str.10 = [31 x i8] c"\0A- Writing data to file, done.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @putchar(i32 10)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%6 = icmp slt i32 %0, 3
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %8)
br label %49
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %12)
%16 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 (...) @std_err()
br label %20
20: ; preds = %18, %10
%21 = load i32, i32* @SEEK_END
%22 = tail call i32 @fseek(i32* %16, i64 0, i32 %21)
%23 = tail call i32 @ftell(i32* %16)
%24 = load i32, i32* @SEEK_SET
%25 = tail call i32 @fseek(i32* %16, i64 0, i32 %24)
%26 = tail call i64 @malloc(i32 %23)
%27 = inttoptr i64 %26 to i8*
%28 = tail call i32 @memset(i8* %27, i32 0, i32 %23)
%29 = tail call i32 @fread(i8* %27, i32 1, i32 %23, i32* %16)
%30 = tail call i32 @fclose(i32* %16)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %23, i32 %23)
%32 = getelementptr inbounds i8, i8* %27, i64 8
%33 = getelementptr inbounds i8, i8* %27, i64 9
%34 = getelementptr inbounds i8, i8* %27, i64 10
%35 = getelementptr inbounds i8, i8* %27, i64 11
%36 = bitcast i8* %32 to i32*
store i32 0, i32* %36
%37 = tail call i32 @lzma_crc32_le(i8* %27, i32 %23, i32 -1)
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i32 %37, i32 %37)
%39 = lshr i32 %37, 24
%40 = trunc i32 %39 to i8
store i8 %40, i8* %32
%41 = lshr i32 %37, 16
%42 = trunc i32 %41 to i8
store i8 %42, i8* %33
%43 = lshr i32 %37, 8
%44 = trunc i32 %43 to i8
store i8 %44, i8* %34
%45 = trunc i32 %37 to i8
store i8 %45, i8* %35
%46 = tail call i32 @write_output_file(i8* %14, i32 0, i8* %27, i32 %23)
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
%48 = tail call i32 @free(i8* %27)
br label %49
49: ; preds = %20, %7
%50 = phi i32 [ -1, %7 ], [ 0, %20 ]
ret i32 %50
}