#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned long fread(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned long fwrite(unsigned char *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  unsigned char *patchme = NULL;
  unsigned long j = 0, k = 0;
  char chFilename[1024];
  signed int exit_code = -1;
  FILE *fp = NULL;
  if (argc < 2) {
    printf("What kernel image do you want me to patch?\n");
    exit_code = -1;
  } else {
    strcpy(chFilename, argv[1]);
    fp = fopen(chFilename, "rb+");
    if (!fp) {
      printf("Could not find kernel image: %s\n", chFilename);
      exit_code = -2;
    } else {
      patchme = (unsigned char *)calloc(4, (1024 * 1024));
      j = fread(patchme, sizeof(unsigned char), 4 * (1024 * 1024), fp);
      if (patchme[0x2c] == 0x01) {
        printf("This kernel image is already patched\n");
        fclose(fp);
        exit_code = 0;
      } else {
        patchme[0x2c] = 0x01;
        rewind(fp);
        k = fwrite(patchme, sizeof(unsigned char), j, fp);
        fclose(fp);
        if (k == j) {
          printf(
              "Kernel image succesfully patched (%ld-%ld bytes read/write)\n",
              j, k);
          exit_code = 0;
        } else {
          printf("There was a problem writing the new kernel image! (bytes "
                 "read=%ld, written=%ld)\n",
                 j, k);
          exit_code = -3;
        }
      }
    }
  }
  exit(exit_code);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%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)
movl   $0xffffffff,-0x434(%rbp)
ff ff ff
movq   $0x0,-0x440(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11fd <main+0x6d>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x434(%rbp)
ff ff ff
jmp    1367 <main+0x1d7>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x430(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
jne    125d <main+0xcd>
lea    -0x430(%rbp),%rsi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xfffffffe,-0x434(%rbp)
ff ff ff
jmp    1362 <main+0x1d2>
mov    $0x4,%edi
mov    $0x100000,%esi
call   1080 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x440(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400000,%edx
call   1040 <fread@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movzbl 0x2c(%rax),%eax
cmp    $0x1,%eax
jne    12c8 <main+0x138>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x440(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x434(%rbp)
jmp    135d <main+0x1cd>
mov    -0x18(%rbp),%rax
movb   $0x1,0x2c(%rax)
mov    -0x440(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x440(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    %rax,-0x28(%rbp)
mov    -0x440(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1338 <main+0x1a8>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x434(%rbp)
jmp    1358 <main+0x1c8>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xfffffffd,-0x434(%rbp)
ff ff ff
jmp    135d <main+0x1cd>
jmp    1362 <main+0x1d2>
jmp    1367 <main+0x1d7>
mov    -0x434(%rbp),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
cmp    $0x1,%edi
jg     11c7 <main+0x27>
lea    0xf20(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x8(%rsi),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe23(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
jne    1205 <main+0x65>
lea    0xe13(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xfffffffe,%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
mov    $0x4,%edi
mov    $0x100000,%esi
call   1090 <calloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x400000,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmpb   $0x1,0x2c(%r15)
jne    1251 <main+0xb1>
lea    0xe76(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    %rax,%r14
movb   $0x1,0x2c(%r15)
mov    %rbx,%rdi
call   1080 <rewind@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <fclose@plt>
cmp    %r14,%r15
jne    12a1 <main+0x101>
lea    0xd9a(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
call   10b0 <exit@plt>
lea    0xdba(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xfffffffd,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [44 x i8] c"What kernel image do you want me to patch?\0A\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [33 x i8] c"Could not find kernel image: %s\0A\00"
@.str.3 = [38 x i8] c"This kernel image is already patched\0A\00"
@.str.4 = [61 x i8] c"Kernel image succesfully patched (%ld-%ld bytes read/write)\0A\00"
@.str.5 = [81 x i8] c"There was a problem writing the new kernel image! (bytes read=%ld, written=%ld)\0A\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 [1024 x i8]
%10 = alloca i32
%11 = 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
store i32 -1, i32* %10
store i32* null, i32** %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %10
br label %69
16: ; preds = %2
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @strcpy(i8* %17, i8* %20)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %11
%24 = load i32*, i32** %11
%25 = icmp ne i32* %24, null
br i1 %25, label %29, label %26
26: ; preds = %16
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %27)
store i32 -2, i32* %10
br label %68
29: ; preds = %16
%30 = call i64 @calloc(i32 4, i32 1048576)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = load i32*, i32** %11
%34 = call i64 @fread(i8* %32, i32 1, i32 4194304, i32* %33)
store i64 %34, i64* %7
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i64 44
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %44
40: ; preds = %29
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32*, i32** %11
%43 = call i32 @fclose(i32* %42)
store i32 0, i32* %10
br label %67
44: ; preds = %29
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i64 44
store i8 1, i8* %46
%47 = load i32*, i32** %11
%48 = call i32 @rewind(i32* %47)
%49 = load i8*, i8** %6
%50 = load i64, i64* %7
%51 = load i32*, i32** %11
%52 = call i64 @fwrite(i8* %49, i32 1, i64 %50, i32* %51)
store i64 %52, i64* %8
%53 = load i32*, i32** %11
%54 = call i32 @fclose(i32* %53)
%55 = load i64, i64* %8
%56 = load i64, i64* %7
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %62
58: ; preds = %44
%59 = load i64, i64* %7
%60 = load i64, i64* %8
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0), i64 %59, i64 %60)
store i32 0, i32* %10
br label %66
62: ; preds = %44
%63 = load i64, i64* %7
%64 = load i64, i64* %8
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.5, i64 0, i64 0), i64 %63, i64 %64)
store i32 -3, i32* %10
br label %66
66: ; preds = %62, %58
br label %67
67: ; preds = %66, %40
br label %68
68: ; preds = %67, %26
br label %69
69: ; preds = %68, %14
%70 = load i32, i32* %10
%71 = call i32 @exit(i32 %70)
unreachable
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [33 x i8] c"Could not find kernel image: %s\0A\00"
@.str.4 = [61 x i8] c"Kernel image succesfully patched (%ld-%ld bytes read/write)\0A\00"
@.str.5 = [81 x i8] c"There was a problem writing the new kernel image! (bytes read=%ld, written=%ld)\0A\00"
@str = [37 x i8] c"This kernel image is already patched\00"
@str.6 = [43 x i8] c"What kernel image do you want me to patch?\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.6, i64 0, i64 0))
br label %35
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @strcpy(i8* %4, i8* %10)
%12 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %35
16: ; preds = %8
%17 = call i64 @calloc(i32 4, i32 1048576)
%18 = inttoptr i64 %17 to i8*
%19 = call i64 @fread(i8* %18, i32 1, i32 4194304, i32* %12)
%20 = getelementptr inbounds i8, i8* %18, i64 44
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%25 = call i32 @fclose(i32* %12)
br label %35
26: ; preds = %16
store i8 1, i8* %20
%27 = call i32 @rewind(i32* %12)
%28 = call i64 @fwrite(i8* %18, i32 1, i64 %19, i32* %12)
%29 = call i32 @fclose(i32* %12)
%30 = icmp eq i64 %28, %19
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0), i64 %19, i64 %19)
br label %35
33: ; preds = %26
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @.str.5, i64 0, i64 0), i64 %19, i64 %28)
br label %35
35: ; preds = %14, %31, %33, %23, %6
%36 = phi i32 [ -1, %6 ], [ 0, %23 ], [ 0, %31 ], [ -3, %33 ], [ -2, %14 ]
%37 = call i32 @exit(i32 %36)
unreachable
}