#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 */
struct stat {
  unsigned int st_size;
};
typedef unsigned char real_size_vmlinux;
typedef int /*<<< orphan*/ magic_str;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
unsigned int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stat(char *, struct stat *);
unsigned int strlen(unsigned char const *);
unsigned char *strstr(unsigned char *, unsigned char const *);
int main(int argc, char **argv) {
  unsigned char *buffer = malloc(64 * 1024);
  struct stat s;
  unsigned int size_vmlinux = 0, real_size_vmlinux = 0;
  const unsigned char *magic_str = "ACME_PART_MAGIC";
  unsigned int loop;
  unsigned char *magic;
  if (argc != 3) {
    printf("%s in out\n", argv[0]);
    return 1;
  }
  printf("Generating image\n");
  FILE *vmlinux = fopen(argv[1], "r");
  FILE *vmlinux_out = fopen(argv[2], "w");
  if ((!vmlinux) || (!vmlinux_out)) {
    printf("Error opening a file\n");
    return 1;
  }
  stat(argv[1], &s);
  size_vmlinux = s.st_size;
  real_size_vmlinux = (size_vmlinux & 0xffff0000) + 0x10000;
  printf("vmlinux = 0x%.08X / 0x%.08X\n", size_vmlinux, real_size_vmlinux);
  unsigned int t = fread(buffer, 1, 64 * 1024, vmlinux);
  for (loop = 0; loop < (64 * 1024) - sizeof(magic_str); loop++) {
    if (buffer[loop] == magic_str[0]) {
      if ((magic = strstr(&buffer[loop], magic_str))) {
        printf("Magic at 0x%.08X %p %p\n", magic - buffer, magic, buffer);
        printf("Found Magic %X%X%X%X\n", buffer[loop + strlen(magic_str)],
               buffer[loop + strlen(magic_str) + 2],
               buffer[loop + strlen(magic_str) + 1],
               buffer[loop + strlen(magic_str) + 3]);
        buffer[loop + strlen(magic_str)] = real_size_vmlinux >> 24;
        buffer[loop + strlen(magic_str) + 2] = (real_size_vmlinux >> 16) & 0xff;
        buffer[loop + strlen(magic_str) + 1] = (real_size_vmlinux >> 8) & 0xff;
        buffer[loop + strlen(magic_str) + 3] = (real_size_vmlinux)&0xff;
        printf("Replaced with %.02X%.02X%.02X%.02X\n",
               buffer[loop + strlen(magic_str)],
               buffer[loop + strlen(magic_str) + 2],
               buffer[loop + strlen(magic_str) + 1],
               buffer[loop + strlen(magic_str) + 3]);
      }
    }
  }
  fwrite(buffer, 1, 64 * 1024, vmlinux_out);
  real_size_vmlinux -= 64 * 1024;
  do {
    real_size_vmlinux -= 64 * 1024;
    memset(buffer, 0, 64 * 1024);
    fread(buffer, 1, 64 * 1024, vmlinux);
    fwrite(buffer, 1, 64 * 1024, vmlinux_out);
  } while (real_size_vmlinux);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x10000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    0xe34(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11fb <main+0x6b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16df <main+0x54f>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe15(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdff(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     124f <main+0xbf>
cmpq   $0x0,-0x50(%rbp)
jne    1269 <main+0xd9>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16df <main+0x54f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0xffff0000,%eax
add    $0x10000,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1030 <fread@plt>
mov    %eax,-0x54(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    $0xfff8,%rax
jae    1663 <main+0x4d3>
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
mov    -0x30(%rbp),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1650 <main+0x4c0>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     164b <main+0x4bb>
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rax
sub    %rax,%rsi
mov    -0x40(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x124(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x124(%rbp),%ecx
mov    %eax,%edx
mov    -0x120(%rbp),%rax
add    %edx,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0xf4(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x110(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x114(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x114(%rbp),%ecx
mov    %eax,%edx
mov    -0x110(%rbp),%rax
add    %edx,%ecx
add    $0x2,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0xf0(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x100(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x104(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x104(%rbp),%ecx
mov    %eax,%edx
mov    -0x100(%rbp),%rax
add    %edx,%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0xec(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xe8(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0xf8(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xf8(%rbp),%edi
mov    -0xf4(%rbp),%esi
mov    -0xf0(%rbp),%edx
mov    -0xec(%rbp),%ecx
mov    %eax,%r8d
mov    -0xe8(%rbp),%rax
add    %r8d,%edi
add    $0x3,%edi
mov    %edi,%edi
movzbl (%rax,%rdi,1),%r8d
lea    0xc45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
shr    $0x18,%eax
mov    %al,-0xd9(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0xe0(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xe0(%rbp),%ecx
mov    -0xd9(%rbp),%dl
mov    %eax,%esi
mov    -0xd8(%rbp),%rax
add    %esi,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,-0xc9(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0xd0(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xd0(%rbp),%ecx
mov    -0xc9(%rbp),%dl
mov    %eax,%esi
mov    -0xc8(%rbp),%rax
add    %esi,%ecx
add    $0x2,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,-0xb9(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0xc0(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xc0(%rbp),%ecx
mov    -0xb9(%rbp),%dl
mov    %eax,%esi
mov    -0xb8(%rbp),%rax
add    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
and    $0xff,%eax
mov    %al,-0xa9(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0xb0(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb0(%rbp),%ecx
mov    -0xa9(%rbp),%dl
mov    %eax,%esi
mov    -0xa8(%rbp),%rax
add    %esi,%ecx
add    $0x3,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x9c(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x9c(%rbp),%ecx
mov    %eax,%edx
mov    -0x98(%rbp),%rax
add    %edx,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8c(%rbp),%ecx
mov    %eax,%edx
mov    -0x88(%rbp),%rax
add    %edx,%ecx
add    $0x2,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x68(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x7c(%rbp),%ecx
mov    %eax,%edx
mov    -0x78(%rbp),%rax
add    %edx,%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x64(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x70(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x70(%rbp),%edi
mov    -0x6c(%rbp),%esi
mov    -0x68(%rbp),%edx
mov    -0x64(%rbp),%ecx
mov    %eax,%r8d
mov    -0x60(%rbp),%rax
add    %r8d,%edi
add    $0x3,%edi
mov    %edi,%edi
movzbl (%rax,%rdi,1),%r8d
lea    0xa4e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1650 <main+0x4c0>
jmp    1655 <main+0x4c5>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12c5 <main+0x135>
mov    -0x18(%rbp),%rdi
mov    -0x50(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   10a0 <fwrite@plt>
mov    -0x28(%rbp),%eax
sub    $0x10000,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
sub    $0x10000,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
mov    -0x50(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   10a0 <fwrite@plt>
cmpl   $0x0,-0x28(%rbp)
jne    1685 <main+0x4f5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r12
mov    %edi,%ebp
mov    $0x10000,%edi
call   1090 <malloc@plt>
cmp    $0x3,%ebp
jne    13e2 <main+0x242>
mov    %rax,%rbx
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r12),%rdi
lea    0xe3a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
mov    0x10(%r12),%rdi
lea    0xe28(%rip),%rsi
call   10a0 <fopen@plt>
test   %r14,%r14
je     13fe <main+0x25e>
test   %rax,%rax
je     13fe <main+0x25e>
mov    %rax,0x18(%rsp)
mov    0x8(%r12),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
mov    0x8(%rsp),%esi
mov    %esi,%r15d
and    $0xffff0000,%r15d
lea    0x10000(%r15),%ebp
lea    0xde3(%rip),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %r14,0x20(%rsp)
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %ebp,%eax
shr    $0x18,%eax
mov    %eax,0x10(%rsp)
shr    $0x10,%ebp
mov    %ebp,0x14(%rsp)
xor    %r13d,%r13d
jmp    1291 <main+0xf1>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r13
cmp    $0xfff8,%r13
je     1411 <main+0x271>
cmpb   $0x41,(%rbx,%r13,1)
jne    1280 <main+0xe0>
lea    (%rbx,%r13,1),%rdi
lea    0xd5d(%rip),%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
je     1280 <main+0xe0>
mov    %rax,%rsi
sub    %rbx,%rsi
lea    0xd82(%rip),%rdi
mov    %rax,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd32(%rip),%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
movzbl (%rbx,%rax,1),%eax
mov    %eax,0x4(%rsp)
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
add    $0x2,%eax
movzbl (%rbx,%rax,1),%ebp
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
add    $0x1,%eax
movzbl (%rbx,%rax,1),%r14d
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
add    $0x3,%eax
movzbl (%rbx,%rax,1),%r8d
lea    0xd34(%rip),%rdi
mov    0x4(%rsp),%esi
mov    %ebp,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
mov    0x10(%rsp),%ecx
mov    %cl,(%rbx,%rax,1)
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
add    $0x2,%eax
mov    0x14(%rsp),%ecx
mov    %cl,(%rbx,%rax,1)
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
add    $0x1,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
add    $0x3,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
movzbl (%rbx,%rax,1),%eax
mov    %eax,0x4(%rsp)
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
add    $0x2,%eax
movzbl (%rbx,%rax,1),%r14d
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
add    $0x1,%eax
movzbl (%rbx,%rax,1),%ebp
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %r13d,%eax
add    $0x3,%eax
movzbl (%rbx,%rax,1),%r8d
lea    0xc9d(%rip),%rdi
mov    0x4(%rsp),%esi
mov    %r14d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1280 <main+0xe0>
mov    (%r12),%rsi
lea    0xc23(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    1482 <main+0x2e2>
lea    0xc9a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    1482 <main+0x2e2>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    0x18(%rsp),%r12
mov    %r12,%rcx
call   10b0 <fwrite@plt>
neg    %r15d
mov    0x20(%rsp),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %r12,%rcx
call   10b0 <fwrite@plt>
add    $0x10000,%r15d
jne    1440 <main+0x2a0>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [16 x i8] c"ACME_PART_MAGIC\00"
@.str.1 = [11 x i8] c"%s in out\0A\00"
@.str.2 = [18 x i8] c"Generating image\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [22 x i8] c"Error opening a file\0A\00"
@.str.6 = [29 x i8] c"vmlinux = 0x%.08X / 0x%.08X\0A\00"
@.str.7 = [24 x i8] c"Magic at 0x%.08X %p %p\0A\00"
@.str.8 = [22 x i8] c"Found Magic %X%X%X%X\0A\00"
@.str.9 = [36 x i8] c"Replaced with %.02X%.02X%.02X%.02X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = call i8* @malloc(i32 65536)
store i8* %16, i8** %6
store i32 0, i32* %8
store i32 0, i32* %9
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %10
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 3
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 ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 1, i32* %3
br label %241
24: ; preds = %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %29, i32** %13
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %33, i32** %14
%34 = load i32*, i32** %13
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %39
36: ; preds = %24
%37 = load i32*, i32** %14
%38 = icmp ne i32* %37, null
br i1 %38, label %41, label %39
39: ; preds = %36, %24
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %241
41: ; preds = %36
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32 @stat(i8* %44, %struct.stat* %7)
%46 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%47 = load i32, i32* %46
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = and i32 %48, -65536
%50 = add i32 %49, 65536
store i32 %50, i32* %9
%51 = load i32, i32* %8
%52 = load i32, i32* %9
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %51, i32 %52)
%54 = load i8*, i8** %6
%55 = load i32*, i32** %13
%56 = call i32 @fread(i8* %54, i32 1, i32 65536, i32* %55)
store i32 %56, i32* %15
store i32 0, i32* %11
br label %57
57: ; preds = %217, %41
%58 = load i32, i32* %11
%59 = zext i32 %58 to i64
%60 = icmp ult i64 %59, 65528
br i1 %60, label %61, label %220
61: ; preds = %57
%62 = load i8*, i8** %6
%63 = load i32, i32* %11
%64 = zext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
%66 = load i8, i8* %65
%67 = zext i8 %66 to i32
%68 = load i8*, i8** %10
%69 = getelementptr inbounds i8, i8* %68, i64 0
%70 = load i8, i8* %69
%71 = zext i8 %70 to i32
%72 = icmp eq i32 %67, %71
br i1 %72, label %73, label %216
73: ; preds = %61
%74 = load i8*, i8** %6
%75 = load i32, i32* %11
%76 = zext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = load i8*, i8** %10
%79 = call i8* @strstr(i8* %77, i8* %78)
store i8* %79, i8** %12
%80 = icmp ne i8* %79, null
br i1 %80, label %81, label %215
81: ; preds = %73
%82 = load i8*, i8** %12
%83 = load i8*, i8** %6
%84 = ptrtoint i8* %82 to i64
%85 = ptrtoint i8* %83 to i64
%86 = sub i64 %84, %85
%87 = load i8*, i8** %12
%88 = load i8*, i8** %6
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i64 %86, i8* %87, i8* %88)
%90 = load i8*, i8** %6
%91 = load i32, i32* %11
%92 = load i8*, i8** %10
%93 = call i32 @strlen(i8* %92)
%94 = add i32 %91, %93
%95 = zext i32 %94 to i64
%96 = getelementptr inbounds i8, i8* %90, i64 %95
%97 = load i8, i8* %96
%98 = zext i8 %97 to i32
%99 = load i8*, i8** %6
%100 = load i32, i32* %11
%101 = load i8*, i8** %10
%102 = call i32 @strlen(i8* %101)
%103 = add i32 %100, %102
%104 = add i32 %103, 2
%105 = zext i32 %104 to i64
%106 = getelementptr inbounds i8, i8* %99, i64 %105
%107 = load i8, i8* %106
%108 = zext i8 %107 to i32
%109 = load i8*, i8** %6
%110 = load i32, i32* %11
%111 = load i8*, i8** %10
%112 = call i32 @strlen(i8* %111)
%113 = add i32 %110, %112
%114 = add i32 %113, 1
%115 = zext i32 %114 to i64
%116 = getelementptr inbounds i8, i8* %109, i64 %115
%117 = load i8, i8* %116
%118 = zext i8 %117 to i32
%119 = load i8*, i8** %6
%120 = load i32, i32* %11
%121 = load i8*, i8** %10
%122 = call i32 @strlen(i8* %121)
%123 = add i32 %120, %122
%124 = add i32 %123, 3
%125 = zext i32 %124 to i64
%126 = getelementptr inbounds i8, i8* %119, i64 %125
%127 = load i8, i8* %126
%128 = zext i8 %127 to i32
%129 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i32 %98, i32 %108, i32 %118, i32 %128)
%130 = load i32, i32* %9
%131 = lshr i32 %130, 24
%132 = trunc i32 %131 to i8
%133 = load i8*, i8** %6
%134 = load i32, i32* %11
%135 = load i8*, i8** %10
%136 = call i32 @strlen(i8* %135)
%137 = add i32 %134, %136
%138 = zext i32 %137 to i64
%139 = getelementptr inbounds i8, i8* %133, i64 %138
store i8 %132, i8* %139
%140 = load i32, i32* %9
%141 = lshr i32 %140, 16
%142 = and i32 %141, 255
%143 = trunc i32 %142 to i8
%144 = load i8*, i8** %6
%145 = load i32, i32* %11
%146 = load i8*, i8** %10
%147 = call i32 @strlen(i8* %146)
%148 = add i32 %145, %147
%149 = add i32 %148, 2
%150 = zext i32 %149 to i64
%151 = getelementptr inbounds i8, i8* %144, i64 %150
store i8 %143, i8* %151
%152 = load i32, i32* %9
%153 = lshr i32 %152, 8
%154 = and i32 %153, 255
%155 = trunc i32 %154 to i8
%156 = load i8*, i8** %6
%157 = load i32, i32* %11
%158 = load i8*, i8** %10
%159 = call i32 @strlen(i8* %158)
%160 = add i32 %157, %159
%161 = add i32 %160, 1
%162 = zext i32 %161 to i64
%163 = getelementptr inbounds i8, i8* %156, i64 %162
store i8 %155, i8* %163
%164 = load i32, i32* %9
%165 = and i32 %164, 255
%166 = trunc i32 %165 to i8
%167 = load i8*, i8** %6
%168 = load i32, i32* %11
%169 = load i8*, i8** %10
%170 = call i32 @strlen(i8* %169)
%171 = add i32 %168, %170
%172 = add i32 %171, 3
%173 = zext i32 %172 to i64
%174 = getelementptr inbounds i8, i8* %167, i64 %173
store i8 %166, i8* %174
%175 = load i8*, i8** %6
%176 = load i32, i32* %11
%177 = load i8*, i8** %10
%178 = call i32 @strlen(i8* %177)
%179 = add i32 %176, %178
%180 = zext i32 %179 to i64
%181 = getelementptr inbounds i8, i8* %175, i64 %180
%182 = load i8, i8* %181
%183 = zext i8 %182 to i32
%184 = load i8*, i8** %6
%185 = load i32, i32* %11
%186 = load i8*, i8** %10
%187 = call i32 @strlen(i8* %186)
%188 = add i32 %185, %187
%189 = add i32 %188, 2
%190 = zext i32 %189 to i64
%191 = getelementptr inbounds i8, i8* %184, i64 %190
%192 = load i8, i8* %191
%193 = zext i8 %192 to i32
%194 = load i8*, i8** %6
%195 = load i32, i32* %11
%196 = load i8*, i8** %10
%197 = call i32 @strlen(i8* %196)
%198 = add i32 %195, %197
%199 = add i32 %198, 1
%200 = zext i32 %199 to i64
%201 = getelementptr inbounds i8, i8* %194, i64 %200
%202 = load i8, i8* %201
%203 = zext i8 %202 to i32
%204 = load i8*, i8** %6
%205 = load i32, i32* %11
%206 = load i8*, i8** %10
%207 = call i32 @strlen(i8* %206)
%208 = add i32 %205, %207
%209 = add i32 %208, 3
%210 = zext i32 %209 to i64
%211 = getelementptr inbounds i8, i8* %204, i64 %210
%212 = load i8, i8* %211
%213 = zext i8 %212 to i32
%214 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.9, i64 0, i64 0), i32 %183, i32 %193, i32 %203, i32 %213)
br label %215
215: ; preds = %81, %73
br label %216
216: ; preds = %215, %61
br label %217
217: ; preds = %216
%218 = load i32, i32* %11
%219 = add i32 %218, 1
store i32 %219, i32* %11
br label %57
220: ; preds = %57
%221 = load i8*, i8** %6
%222 = load i32*, i32** %14
%223 = call i32 @fwrite(i8* %221, i32 1, i32 65536, i32* %222)
%224 = load i32, i32* %9
%225 = sub i32 %224, 65536
store i32 %225, i32* %9
br label %226
226: ; preds = %237, %220
%227 = load i32, i32* %9
%228 = sub i32 %227, 65536
store i32 %228, i32* %9
%229 = load i8*, i8** %6
%230 = call i32 @memset(i8* %229, i32 0, i32 65536)
%231 = load i8*, i8** %6
%232 = load i32*, i32** %13
%233 = call i32 @fread(i8* %231, i32 1, i32 65536, i32* %232)
%234 = load i8*, i8** %6
%235 = load i32*, i32** %14
%236 = call i32 @fwrite(i8* %234, i32 1, i32 65536, i32* %235)
br label %237
237: ; preds = %226
%238 = load i32, i32* %9
%239 = icmp ne i32 %238, 0
br i1 %239, label %226, label %240
240: ; preds = %237
store i32 0, i32* %3
br label %241
241: ; preds = %240, %39, %19
%242 = load i32, i32* %3
ret i32 %242
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [16 x i8] c"ACME_PART_MAGIC\00"
@.str.1 = [11 x i8] c"%s in out\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"w\00"
@.str.6 = [29 x i8] c"vmlinux = 0x%.08X / 0x%.08X\0A\00"
@.str.7 = [24 x i8] c"Magic at 0x%.08X %p %p\0A\00"
@.str.8 = [22 x i8] c"Found Magic %X%X%X%X\0A\00"
@.str.9 = [36 x i8] c"Replaced with %.02X%.02X%.02X%.02X\0A\00"
@str = [17 x i8] c"Generating image\00"
@str.10 = [21 x i8] c"Error opening a file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = tail call dereferenceable_or_null(65536) i8* @malloc(i32 65536)
%5 = bitcast %struct.stat* %3 to i8*
%6 = icmp eq i32 %0, 3
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %134
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%18 = icmp ne i32* %14, null
%19 = icmp ne i32* %17, null
%20 = and i1 %18, %19
br i1 %20, label %23, label %21
21: ; preds = %10
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.10, i64 0, i64 0))
br label %134
23: ; preds = %10
%24 = load i8*, i8** %12
%25 = call i32 @stat(i8* %24, %struct.stat* %3)
%26 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%27 = load i32, i32* %26
%28 = and i32 %27, -65536
%29 = add i32 %28, 65536
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %27, i32 %29)
%31 = tail call i32 @fread(i8* %4, i32 1, i32 65536, i32* %14)
%32 = ptrtoint i8* %4 to i64
%33 = lshr i32 %29, 24
%34 = trunc i32 %33 to i8
%35 = lshr exact i32 %29, 16
%36 = trunc i32 %35 to i8
br label %37
37: ; preds = %23, %122
%38 = phi i64 [ 0, %23 ], [ %123, %122 ]
%39 = getelementptr inbounds i8, i8* %4, i64 %38
%40 = load i8, i8* %39
%41 = icmp eq i8 %40, 65
br i1 %41, label %42, label %122
42: ; preds = %37
%43 = tail call i8* @strstr(i8* %39, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%44 = icmp eq i8* %43, null
br i1 %44, label %122, label %45
45: ; preds = %42
%46 = ptrtoint i8* %43 to i64
%47 = sub i64 %46, %32
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i64 %47, i8* %43, i8* %4)
%49 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%50 = trunc i64 %38 to i32
%51 = add i32 %49, %50
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %4, i64 %52
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%57 = trunc i64 %38 to i32
%58 = add i32 %57, 2
%59 = add i32 %58, %56
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %4, i64 %60
%62 = load i8, i8* %61
%63 = zext i8 %62 to i32
%64 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%65 = trunc i64 %38 to i32
%66 = add i32 %65, 1
%67 = add i32 %66, %64
%68 = zext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %4, i64 %68
%70 = load i8, i8* %69
%71 = zext i8 %70 to i32
%72 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%73 = trunc i64 %38 to i32
%74 = add i32 %73, 3
%75 = add i32 %74, %72
%76 = zext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %4, i64 %76
%78 = load i8, i8* %77
%79 = zext i8 %78 to i32
%80 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i32 %55, i32 %63, i32 %71, i32 %79)
%81 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%82 = add i32 %81, %50
%83 = zext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %4, i64 %83
store i8 %34, i8* %84
%85 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%86 = add i32 %58, %85
%87 = zext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %4, i64 %87
store i8 %36, i8* %88
%89 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%90 = add i32 %66, %89
%91 = zext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %4, i64 %91
store i8 0, i8* %92
%93 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%94 = add i32 %74, %93
%95 = zext i32 %94 to i64
%96 = getelementptr inbounds i8, i8* %4, i64 %95
store i8 0, i8* %96
%97 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%98 = add i32 %97, %50
%99 = zext i32 %98 to i64
%100 = getelementptr inbounds i8, i8* %4, i64 %99
%101 = load i8, i8* %100
%102 = zext i8 %101 to i32
%103 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%104 = add i32 %58, %103
%105 = zext i32 %104 to i64
%106 = getelementptr inbounds i8, i8* %4, i64 %105
%107 = load i8, i8* %106
%108 = zext i8 %107 to i32
%109 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%110 = add i32 %66, %109
%111 = zext i32 %110 to i64
%112 = getelementptr inbounds i8, i8* %4, i64 %111
%113 = load i8, i8* %112
%114 = zext i8 %113 to i32
%115 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%116 = add i32 %74, %115
%117 = zext i32 %116 to i64
%118 = getelementptr inbounds i8, i8* %4, i64 %117
%119 = load i8, i8* %118
%120 = zext i8 %119 to i32
%121 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.9, i64 0, i64 0), i32 %102, i32 %108, i32 %114, i32 %120)
br label %122
122: ; preds = %37, %45, %42
%123 = add nuw nsw i64 %38, 1
%124 = icmp eq i64 %123, 65528
br i1 %124, label %125, label %37
125: ; preds = %122
%126 = tail call i32 @fwrite(i8* %4, i32 1, i32 65536, i32* %17)
br label %127
127: ; preds = %127, %125
%128 = phi i32 [ %28, %125 ], [ %129, %127 ]
%129 = add i32 %128, -65536
%130 = tail call i32 @memset(i8* %4, i32 0, i32 65536)
%131 = tail call i32 @fread(i8* %4, i32 1, i32 65536, i32* %14)
%132 = tail call i32 @fwrite(i8* %4, i32 1, i32 65536, i32* %17)
%133 = icmp eq i32 %129, 0
br i1 %133, label %134, label %127
134: ; preds = %127, %21, %7
%135 = phi i32 [ 1, %7 ], [ 1, %21 ], [ 0, %127 ]
ret i32 %135
}