#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("Usage: %s [filename]\n", argv[0]);
    printf("Where [filename] is the CoD4 server ELF\n");
  } else {
    FILE *file = fopen(argv[1], "rb+");
    if (file == 0)
      printf("Could not open %s for reading/writing\n", argv[1]);
    else {
      char byte_check[2];
      char magic_byte[1] = {0x15};
      fseek(file, 0x001305E2 - 1, SEEK_SET);
      fread(&byte_check, 1, 2, file);
      if (byte_check[0] == 0x04 && byte_check[1] == 0x14) {
        printf("Patching ELF\n");
        fseek(file, 0x001305E2, SEEK_SET);
        fwrite(&magic_byte, 1, 1, file);
        fseek(file, 0x001305E2, SEEK_SET);
        fread(&byte_check, 1, 1, file);
        if (byte_check[0] == 0x15)
          printf("Patch successful\n");
        else
          printf("Patch failed\n");
      } else if (byte_check[1] == 0x15)
        printf("ELF already patched\n");
      else
        printf("Incorrect CoD4 ELF, not patching\n");
    }
  }
  return (1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1198 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fb <main+0x1ab>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe98(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d6 <main+0x86>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f6 <main+0x1a6>
mov    0xef3(%rip),%al
mov    %al,-0x1b(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    $0x1305e1,%esi
call   1050 <fseek@plt>
lea    -0x1a(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fread@plt>
movsbl -0x1a(%rbp),%eax
cmp    $0x4,%eax
jne    12be <main+0x16e>
movsbl -0x19(%rbp),%eax
cmp    $0x14,%eax
jne    12be <main+0x16e>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
mov    $0x1305e2,%esi
call   1050 <fseek@plt>
lea    -0x1b(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
mov    $0x1305e2,%esi
call   1050 <fseek@plt>
lea    -0x1a(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
movsbl -0x1a(%rbp),%eax
cmp    $0x15,%eax
jne    12ab <main+0x15b>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b9 <main+0x169>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f1 <main+0x1a1>
movsbl -0x19(%rbp),%eax
cmp    $0x15,%eax
jne    12de <main+0x18e>
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ec <main+0x19c>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f1 <main+0x1a1>
jmp    12f6 <main+0x1a6>
jmp    12fb <main+0x1ab>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1233 <main+0xd3>
mov    0x8(%rbx),%rdi
lea    0xe9b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     124d <main+0xed>
mov    %rax,%r14
mov    0x2e4d(%rip),%rbx
mov    (%rbx),%edx
mov    %rax,%rdi
mov    $0x1305e1,%esi
call   1070 <fseek@plt>
lea    0x6(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    0x7(%rsp),%al
cmpb   $0x4,0x6(%rsp)
jne    1261 <main+0x101>
cmp    $0x14,%al
jne    1261 <main+0x101>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%edx
mov    %r14,%rdi
mov    $0x1305e2,%esi
call   1070 <fseek@plt>
mov    $0x15,%edi
mov    %r14,%rsi
call   1060 <fputc@plt>
mov    (%rbx),%edx
mov    %r14,%rdi
mov    $0x1305e2,%esi
call   1070 <fseek@plt>
lea    0x6(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmpb   $0x15,0x6(%rsp)
lea    0xe66(%rip),%rax
lea    0xe52(%rip),%rdi
jmp    1271 <main+0x111>
mov    (%rbx),%rsi
lea    0xdc3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe56(%rip),%rdi
jmp    1275 <main+0x115>
mov    0x8(%rbx),%rsi
lea    0xdc2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    127a <main+0x11a>
cmp    $0x15,%al
lea    0xdf8(%rip),%rax
lea    0xdd0(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Usage: %s [filename]\0A\00"
@.str.1 = [41 x i8] c"Where [filename] is the CoD4 server ELF\0A\00"
@.str.2 = [4 x i8] c"rb+\00"
@.str.3 = [39 x i8] c"Could not open %s for reading/writing\0A\00"
@__const.main.magic_byte = [1 x i8] c"\15"
@SEEK_SET = global i32 0
@.str.4 = [14 x i8] c"Patching ELF\0A\00"
@.str.5 = [18 x i8] c"Patch successful\0A\00"
@.str.6 = [14 x i8] c"Patch failed\0A\00"
@.str.7 = [21 x i8] c"ELF already patched\0A\00"
@.str.8 = [34 x i8] c"Incorrect CoD4 ELF, not patching\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [2 x i8]
%8 = alloca [1 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %81
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %17
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %80
29: ; preds = %17
%30 = bitcast [1 x i8]* %8 to i8*
%31 = load i32*, i32** %6
%32 = load i32, i32* @SEEK_SET
%33 = call i32 @fseek(i32* %31, i32 1246689, i32 %32)
%34 = bitcast [2 x i8]* %7 to i8**
%35 = load i32*, i32** %6
%36 = call i32 @fread(i8** %34, i32 1, i32 2, i32* %35)
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 4
br i1 %40, label %41, label %69
41: ; preds = %29
%42 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 20
br i1 %45, label %46, label %69
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32*, i32** %6
%49 = load i32, i32* @SEEK_SET
%50 = call i32 @fseek(i32* %48, i32 1246690, i32 %49)
%51 = bitcast [1 x i8]* %8 to i8**
%52 = load i32*, i32** %6
%53 = call i32 @fwrite(i8** %51, i32 1, i32 1, i32* %52)
%54 = load i32*, i32** %6
%55 = load i32, i32* @SEEK_SET
%56 = call i32 @fseek(i32* %54, i32 1246690, i32 %55)
%57 = bitcast [2 x i8]* %7 to i8**
%58 = load i32*, i32** %6
%59 = call i32 @fread(i8** %57, i32 1, i32 1, i32* %58)
%60 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 21
br i1 %63, label %64, label %66
64: ; preds = %46
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %68
66: ; preds = %46
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %68
68: ; preds = %66, %64
br label %79
69: ; preds = %41, %29
%70 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = icmp eq i32 %72, 21
br i1 %73, label %74, label %76
74: ; preds = %69
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
br label %78
76: ; preds = %69
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
br label %78
78: ; preds = %76, %74
br label %79
79: ; preds = %78, %68
br label %80
80: ; preds = %79, %24
br label %81
81: ; preds = %80, %11
ret i32 1
}

LLVM IR (-O2):
@.str = [22 x i8] c"Usage: %s [filename]\0A\00"
@.str.2 = [4 x i8] c"rb+\00"
@.str.3 = [39 x i8] c"Could not open %s for reading/writing\0A\00"
@SEEK_SET = global i32 0
@str = [33 x i8] c"Incorrect CoD4 ELF, not patching\00"
@str.9 = [20 x i8] c"ELF already patched\00"
@str.10 = [13 x i8] c"Patching ELF\00"
@str.11 = [13 x i8] c"Patch failed\00"
@str.12 = [17 x i8] c"Patch successful\00"
@str.13 = [40 x i8] c"Where [filename] is the CoD4 server ELF\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i8]
%4 = icmp eq i32 %0, 2
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.13, i64 0, i64 0))
br label %46
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i8*, i8** %10
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %15)
br label %46
17: ; preds = %9
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %12, i32 1246689, i32 %19)
%21 = bitcast [2 x i8]* %3 to i8**
%22 = call i32 @fread(i8** %21, i32 1, i32 2, i32* %12)
%23 = load i8, i8* %18
%24 = icmp eq i8 %23, 4
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 20
%28 = select i1 %24, i1 %27, i1 false
br i1 %28, label %29, label %40
29: ; preds = %17
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @fseek(i32* %12, i32 1246690, i32 %31)
%33 = tail call i32 @fputc(i32 21, i32* %12)
%34 = load i32, i32* @SEEK_SET
%35 = tail call i32 @fseek(i32* %12, i32 1246690, i32 %34)
%36 = call i32 @fread(i8** %21, i32 1, i32 1, i32* %12)
%37 = load i8, i8* %18
%38 = icmp eq i8 %37, 21
%39 = select i1 %38, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @str.12, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.11, i64 0, i64 0)
br label %43
40: ; preds = %17
%41 = icmp eq i8 %26, 21
%42 = select i1 %41, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0)
br label %43
43: ; preds = %40, %29
%44 = phi i8* [ %39, %29 ], [ %42, %40 ]
%45 = tail call i32 @puts(i8* dereferenceable(1) %44)
br label %46
46: ; preds = %14, %43, %5
ret i32 1
}