#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_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, size_t, int, int /*<<< orphan*/ *);
unsigned char *malloc(size_t);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ rename(char *, char *);
size_t search_bin(unsigned char *, size_t);
int main(int argc, char **argv) {
  unsigned char *bin_data = NULL;
  size_t bin_size = 0;
  size_t locate = 0;
  {
    FILE *fp = fopen("SCBIOS.COM", "r+b");
    if (fp) {
      fseek(fp, 0, SEEK_END);
      bin_size = ftell(fp);
      fseek(fp, 0, SEEK_SET);
      bin_data = malloc(bin_size);
      if (bin_data) {
        fread(bin_data, bin_size, 1, fp);
      }
      fclose(fp);
    } else {
      printf("SCBIOS not found.\n");
      return -1;
    }
  }
  if (bin_data == NULL) {
    printf("file open error.");
    return -2;
  }
  locate = search_bin(bin_data, bin_size);
  if (locate != 0) {
    rename("SCBIOS.COM", "SCBIOS.ORG");
    bin_data[locate] = 0xc3;
    {
      FILE *fp = fopen("SCBIOS.COM", "wb");
      fwrite(bin_data, bin_size, 1, fp);
      fclose(fp);
    }
  } else {
    printf("No need patch.");
  }
  free(bin_data);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe1b(%rip),%rdi
lea    0xe1f(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1271 <main+0xc1>
mov    -0x30(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1263 <main+0xb3>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1289 <main+0xd9>
lea    0xd97(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <main+0x18e>
cmpq   $0x0,-0x18(%rbp)
jne    12ac <main+0xfc>
lea    0xd87(%rip),%rdi
call   1060 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    133e <main+0x18e>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <search_bin@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1322 <main+0x172>
lea    0xd31(%rip),%rdi
lea    0xd5d(%rip),%rsi
call   10c0 <rename@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0xc3,(%rax,%rcx,1)
lea    0xd12(%rip),%rdi
lea    0xd49(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
jmp    132e <main+0x17e>
lea    0xd18(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe31(%rip),%rdi
lea    0xe35(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12a4 <main+0xe4>
mov    %rax,%rbx
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <ftell@plt>
mov    %rax,%r14
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r14,%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12b7 <main+0xf7>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <search_bin@plt>
test   %rax,%rax
je     12d2 <main+0x112>
mov    %rax,%rbx
lea    0xd9f(%rip),%r12
lea    0xdb8(%rip),%rsi
mov    %r12,%rdi
call   10d0 <rename@plt>
movb   $0xc3,(%r15,%rbx,1)
lea    0xdaf(%rip),%rsi
mov    %r12,%rdi
call   10c0 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    12de <main+0x11e>
lea    0xd92(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
jmp    12e8 <main+0x128>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xd49(%rip),%rdi
call   1070 <printf@plt>
mov    $0xfffffffe,%eax
jmp    12e8 <main+0x128>
lea    0xd55(%rip),%rdi
call   1070 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"SCBIOS.COM\00"
@.str.1 = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"SCBIOS not found.\0A\00"
@.str.3 = [17 x i8] c"file open error.\00"
@.str.4 = [11 x i8] c"SCBIOS.ORG\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [15 x i8] c"No need patch.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i64 0, i64* %7
store i64 0, i64* %8
%11 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %35
14: ; preds = %2
%15 = load i32*, i32** %9
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %9
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %7
%20 = load i32*, i32** %9
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i64, i64* %7
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %14
%28 = load i8*, i8** %6
%29 = load i64, i64* %7
%30 = load i32*, i32** %9
%31 = call i32 @fread(i8* %28, i64 %29, i32 1, i32* %30)
br label %32
32: ; preds = %27, %14
%33 = load i32*, i32** %9
%34 = call i32 @fclose(i32* %33)
br label %37
35: ; preds = %2
%36 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
37: ; preds = %32
%38 = load i8*, i8** %6
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -2, i32* %3
br label %65
42: ; preds = %37
%43 = load i8*, i8** %6
%44 = load i64, i64* %7
%45 = call i64 @search_bin(i8* %43, i64 %44)
store i64 %45, i64* %8
%46 = load i64, i64* %8
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %60
48: ; preds = %42
%49 = call i32 @rename(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%50 = load i8*, i8** %6
%51 = load i64, i64* %8
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8 -61, i8* %52
%53 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %53, i32** %10
%54 = load i8*, i8** %6
%55 = load i64, i64* %7
%56 = load i32*, i32** %10
%57 = call i32 @fwrite(i8* %54, i64 %55, i32 1, i32* %56)
%58 = load i32*, i32** %10
%59 = call i32 @fclose(i32* %58)
br label %62
60: ; preds = %42
%61 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %60, %48
%63 = load i8*, i8** %6
%64 = call i32 @free(i8* %63)
store i32 0, i32* %3
br label %65
65: ; preds = %62, %40, %35
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [11 x i8] c"SCBIOS.COM\00"
@.str.1 = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [17 x i8] c"file open error.\00"
@.str.4 = [11 x i8] c"SCBIOS.ORG\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [15 x i8] c"No need patch.\00"
@str = [18 x i8] c"SCBIOS not found.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i64 @ftell(i32* %3)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = tail call i8* @malloc(i64 %8)
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %18
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %33
15: ; preds = %5
%16 = tail call i32 @fclose(i32* %3)
%17 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %33
18: ; preds = %5
%19 = tail call i32 @fread(i8* %11, i64 %8, i32 1, i32* %3)
%20 = tail call i32 @fclose(i32* %3)
%21 = tail call i64 @search_bin(i8* %11, i64 %8)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %18
%24 = tail call i32 @rename(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%25 = getelementptr inbounds i8, i8* %11, i64 %21
store i8 -61, i8* %25
%26 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%27 = tail call i32 @fwrite(i8* %11, i64 %8, i32 1, i32* %26)
%28 = tail call i32 @fclose(i32* %26)
br label %31
29: ; preds = %18
%30 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
%32 = tail call i32 @free(i8* %11)
br label %33
33: ; preds = %13, %31, %15
%34 = phi i32 [ -2, %15 ], [ 0, %31 ], [ -1, %13 ]
ret i32 %34
}