#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int, unsigned int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
scalar_t__ strstr(char *, char *);
void fs_2_h_patch_x1000(const char *in_path, const char *out_path) {
  char buffer[1024];
  unsigned int checksum = 0;
  FILE *fp_r = fopen(in_path, "r");
  FILE *fp_r2 = fopen("_fs.h", "r");
  FILE *fp_w = fopen(out_path, "w");
  if (fp_r) {
    while (fgets(buffer, sizeof(buffer) / sizeof(buffer[0]), fp_r)) {
      if (strstr(buffer, "//CheckSum")) {
        sscanf(buffer, "//CheckSum: %x", &checksum);
        break;
      }
      if (strstr(buffer, "//UserCode")) {
        sscanf(buffer, "//UserCode: %x", &checksum);
        break;
      }
    }
    fclose(fp_r);
  }
  if (fp_r2 && fp_w) {
    while (fgets(buffer, sizeof(buffer) / sizeof(buffer[0]), fp_r2)) {
      if (strstr(buffer,
                 "static unsigned char FS_chksum[2] = { 0xFF, 0xFF }; ")) {
        sprintf(buffer,
                "static unsigned char FS_chksum[2] = { 0x%02x, 0x%02x };\n",
                (checksum >> 8) & 0xFF, (checksum >> 0) & 0xFF);
        fprintf(fp_w, "%s", buffer);
      } else {
        fprintf(fp_w, "%s", buffer);
      }
    }
    fclose(fp_r);
    printf("Done\n");
  }
  if (fp_r2) {
    fclose(fp_r2);
  }
  if (fp_w) {
    fclose(fp_w);
  }
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x414(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x420(%rbp)
lea    0xe47(%rip),%rdi
lea    0xe3e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x428(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     12a3 <fs_2_h_patch_x1000+0x123>
jmp    11f8 <fs_2_h_patch_x1000+0x78>
lea    -0x410(%rbp),%rdi
mov    -0x420(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1297 <fs_2_h_patch_x1000+0x117>
lea    -0x410(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     1256 <fs_2_h_patch_x1000+0xd6>
lea    -0x410(%rbp),%rdi
lea    0xdd0(%rip),%rsi
lea    -0x414(%rbp),%rdx
call   1070 <sscanf@plt>
jmp    1297 <fs_2_h_patch_x1000+0x117>
lea    -0x410(%rbp),%rdi
lea    0xdc0(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     1292 <fs_2_h_patch_x1000+0x112>
lea    -0x410(%rbp),%rdi
lea    0xdae(%rip),%rsi
lea    -0x414(%rbp),%rdx
call   1070 <sscanf@plt>
jmp    1297 <fs_2_h_patch_x1000+0x117>
jmp    11f8 <fs_2_h_patch_x1000+0x78>
mov    -0x420(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x428(%rbp)
je     138a <fs_2_h_patch_x1000+0x20a>
cmpq   $0x0,-0x430(%rbp)
je     138a <fs_2_h_patch_x1000+0x20a>
jmp    12c4 <fs_2_h_patch_x1000+0x144>
lea    -0x410(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1372 <fs_2_h_patch_x1000+0x1f2>
lea    -0x410(%rbp),%rdi
lea    0xd4a(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     1353 <fs_2_h_patch_x1000+0x1d3>
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%edx
shr    $0x8,%edx
and    $0xff,%edx
mov    -0x414(%rbp),%ecx
shr    $0x0,%ecx
and    $0xff,%ecx
lea    0xd44(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x430(%rbp),%rdi
lea    -0x410(%rbp),%rdx
lea    0xd63(%rip),%rsi
call   1060 <fprintf@plt>
jmp    136d <fs_2_h_patch_x1000+0x1ed>
mov    -0x430(%rbp),%rdi
lea    -0x410(%rbp),%rdx
lea    0xd44(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c4 <fs_2_h_patch_x1000+0x144>
mov    -0x420(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xd2a(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x428(%rbp)
je     13a4 <fs_2_h_patch_x1000+0x224>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x430(%rbp)
je     13be <fs_2_h_patch_x1000+0x23e>
mov    -0x430(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
movl   $0x0,0xc(%rsp)
lea    0xe5d(%rip),%rbp
mov    %rbp,%rsi
call   1080 <fopen@plt>
mov    %rax,%r15
lea    0xe4d(%rip),%rdi
mov    %rbp,%rsi
call   1080 <fopen@plt>
mov    %rax,%r13
lea    0xe41(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %r15,%r15
je     124c <fs_2_h_patch_x1000+0xcc>
lea    0x10(%rsp),%rbp
lea    0xe27(%rip),%rbx
lea    0xe3a(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1244 <fs_2_h_patch_x1000+0xc4>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
jne    122e <fs_2_h_patch_x1000+0xae>
mov    %rbp,%rdi
mov    %r12,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11f0 <fs_2_h_patch_x1000+0x70>
lea    0xe03(%rip),%rsi
jmp    1235 <fs_2_h_patch_x1000+0xb5>
lea    0xde0(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1070 <sscanf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %r13,%r13
je     12eb <fs_2_h_patch_x1000+0x16b>
test   %r14,%r14
je     12eb <fs_2_h_patch_x1000+0x16b>
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12d7 <fs_2_h_patch_x1000+0x157>
lea    0xdc2(%rip),%r12
lea    0x10(%rsp),%rbx
lea    0xdeb(%rip),%rbp
jmp    12b0 <fs_2_h_patch_x1000+0x130>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <fputs@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12d7 <fs_2_h_patch_x1000+0x157>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     1290 <fs_2_h_patch_x1000+0x110>
mov    0xc(%rsp),%eax
movzbl %ah,%edx
movzbl %al,%ecx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <sprintf@plt>
jmp    1290 <fs_2_h_patch_x1000+0x110>
mov    %r15,%rdi
call   1040 <fclose@plt>
lea    0xdc6(%rip),%rdi
call   1030 <puts@plt>
test   %r13,%r13
je     12f8 <fs_2_h_patch_x1000+0x178>
mov    %r13,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     1305 <fs_2_h_patch_x1000+0x185>
mov    %r14,%rdi
call   1040 <fclose@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"_fs.h\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [11 x i8] c"//CheckSum\00"
@.str.4 = [15 x i8] c"//CheckSum: %x\00"
@.str.5 = [11 x i8] c"//UserCode\00"
@.str.6 = [15 x i8] c"//UserCode: %x\00"
@.str.7 = [53 x i8] c"static unsigned char FS_chksum[2] = { 0xFF, 0xFF }; \00"
@.str.8 = [57 x i8] c"static unsigned char FS_chksum[2] = { 0x%02x, 0x%02x };\0A\00"
@.str.9 = [3 x i8] c"%s\00"
@.str.10 = [6 x i8] c"Done\0A\00"
define void @fs_2_h_patch_x1000(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %6
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i8*, i8** %4
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %14, i32** %9
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %41
17: ; preds = %2
br label %18
18: ; preds = %37, %17
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = load i32*, i32** %7
%21 = call i64 @fgets(i8* %19, i32 1024, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %38
23: ; preds = %18
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%25 = call i64 @strstr(i8* %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32* %6)
br label %38
30: ; preds = %23
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%32 = call i64 @strstr(i8* %31, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%36 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32* %6)
br label %38
37: ; preds = %30
br label %18
38: ; preds = %34, %27, %18
%39 = load i32*, i32** %7
%40 = call i32 @fclose(i32* %39)
br label %41
41: ; preds = %38, %2
%42 = load i32*, i32** %8
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %78
44: ; preds = %41
%45 = load i32*, i32** %9
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %78
47: ; preds = %44
br label %48
48: ; preds = %73, %47
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%50 = load i32*, i32** %8
%51 = call i64 @fgets(i8* %49, i32 1024, i32* %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %74
53: ; preds = %48
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%55 = call i64 @strstr(i8* %54, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0))
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %69
57: ; preds = %53
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%59 = load i32, i32* %6
%60 = lshr i32 %59, 8
%61 = and i32 %60, 255
%62 = load i32, i32* %6
%63 = lshr i32 %62, 0
%64 = and i32 %63, 255
%65 = call i32 @sprintf(i8* %58, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.8, i64 0, i64 0), i32 %61, i32 %64)
%66 = load i32*, i32** %9
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%68 = call i32 @fprintf(i32* %66, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* %67)
br label %73
69: ; preds = %53
%70 = load i32*, i32** %9
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%72 = call i32 @fprintf(i32* %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* %71)
br label %73
73: ; preds = %69, %57
br label %48
74: ; preds = %48
%75 = load i32*, i32** %7
%76 = call i32 @fclose(i32* %75)
%77 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0))
br label %78
78: ; preds = %74, %44, %41
%79 = load i32*, i32** %8
%80 = icmp ne i32* %79, null
br i1 %80, label %81, label %84
81: ; preds = %78
%82 = load i32*, i32** %8
%83 = call i32 @fclose(i32* %82)
br label %84
84: ; preds = %81, %78
%85 = load i32*, i32** %9
%86 = icmp ne i32* %85, null
br i1 %86, label %87, label %90
87: ; preds = %84
%88 = load i32*, i32** %9
%89 = call i32 @fclose(i32* %88)
br label %90
90: ; preds = %87, %84
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"_fs.h\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [11 x i8] c"//CheckSum\00"
@.str.4 = [15 x i8] c"//CheckSum: %x\00"
@.str.5 = [11 x i8] c"//UserCode\00"
@.str.6 = [15 x i8] c"//UserCode: %x\00"
@.str.7 = [53 x i8] c"static unsigned char FS_chksum[2] = { 0xFF, 0xFF }; \00"
@.str.8 = [57 x i8] c"static unsigned char FS_chksum[2] = { 0x%02x, 0x%02x };\0A\00"
@str = [5 x i8] c"Done\00"
define void @fs_2_h_patch_x1000(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %7, null
br i1 %10, label %25, label %11
11: ; preds = %2, %17
%12 = call i64 @fgets(i8* %5, i32 1024, i32* %7)
%13 = icmp eq i64 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %11
%15 = call i64 @strstr(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i64 @strstr(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %11, label %20
20: ; preds = %17, %14
%21 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), %14 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), %17 ]
%22 = call i32 @sscanf(i8* %5, i8* %21, i32* %4)
br label %23
23: ; preds = %11, %20
%24 = call i32 @fclose(i32* %7)
br label %25
25: ; preds = %23, %2
%26 = icmp ne i32* %8, null
%27 = icmp ne i32* %9, null
%28 = and i1 %26, %27
br i1 %28, label %29, label %48
29: ; preds = %25
%30 = call i64 @fgets(i8* %5, i32 1024, i32* %8)
%31 = icmp eq i64 %30, 0
br i1 %31, label %45, label %32
32: ; preds = %29, %41
%33 = call i64 @strstr(i8* %5, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %32
%36 = load i32, i32* %4
%37 = lshr i32 %36, 8
%38 = and i32 %37, 255
%39 = and i32 %36, 255
%40 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.8, i64 0, i64 0), i32 %38, i32 %39)
br label %41
41: ; preds = %32, %35
%42 = call i32 @fputs(i8* %5, i32* %9)
%43 = call i64 @fgets(i8* %5, i32 1024, i32* %8)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %32
45: ; preds = %41, %29
%46 = call i32 @fclose(i32* %7)
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %48
48: ; preds = %45, %25
br i1 %26, label %49, label %51
49: ; preds = %48
%50 = call i32 @fclose(i32* %8)
br label %51
51: ; preds = %49, %48
br i1 %27, label %52, label %54
52: ; preds = %51
%53 = call i32 @fclose(i32* %9)
br label %54
54: ; preds = %52, %51
ret void
}