#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned int,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  const unsigned int romoffset = 0x1000;
  unsigned int linelength;
  unsigned int lineoffset;
  unsigned int bytebuf;
  FILE *from;
  FILE *fpatch;
  fpatch = fopen("spacehigh.ihx", "r");
  from = fopen("invaders.f", "r+");
  int i;
  while (!feof(fpatch)) {
    fscanf(fpatch, ":%2x%4x00", &linelength, &lineoffset);
    if (linelength == 0) {
      break;
    }
    fseek(from, lineoffset - romoffset, SEEK_SET);
    for (i = 0; i < linelength; i++) {
      fscanf(fpatch, "%2x", &bytebuf);
      fwrite(&bytebuf, 1, 1, from);
    }
    fscanf(fpatch, "%*s\n");
    printf("l %x, %x", linelength, lineoffset);
    printf("\n");
  }
  printf("\n");
  fclose(fpatch);
  fclose(from);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1000,-0x8(%rbp)
lea    0xe73(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
lea    0xe6c(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d2 <main+0x62>
jmp    129a <main+0x12a>
mov    -0x28(%rbp),%rdi
lea    0xe41(%rip),%rsi
lea    -0xc(%rbp),%rdx
lea    -0x10(%rbp),%rcx
mov    $0x0,%al
call   1050 <fscanf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11fb <main+0x8b>
jmp    129a <main+0x12a>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
sub    $0x1000,%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1261 <main+0xf1>
mov    -0x28(%rbp),%rdi
lea    0xdf4(%rip),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x20(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    121d <main+0xad>
mov    -0x28(%rbp),%rdi
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b4 <main+0x44>
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe6b(%rip),%rdi
lea    0xe72(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
lea    0xe65(%rip),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     11f1 <main+0x71>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%r13
lea    0xe2b(%rip),%rbp
lea    0x14(%rsp),%r12
jmp    1251 <main+0xd1>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
lea    0xe12(%rip),%rsi
xor    %eax,%eax
call   1060 <fscanf@plt>
mov    0xc(%rsp),%esi
mov    0x10(%rsp),%edx
lea    0xe01(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    11c6 <main+0x46>
mov    %rbx,%rdi
lea    0xdc3(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1060 <fscanf@plt>
cmpl   $0x0,0xc(%rsp)
je     11c6 <main+0x46>
mov    0x10(%rsp),%esi
mov    $0xfffff000,%eax
add    %eax,%esi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1090 <fseek@plt>
cmpl   $0x0,0xc(%rsp)
je     1210 <main+0x90>
xor    %r15d,%r15d
nop
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <fscanf@plt>
movsbl 0x14(%rsp),%edi
mov    %r14,%rsi
call   1070 <fputc@plt>
add    $0x1,%r15d
cmp    0xc(%rsp),%r15d
jb     12a0 <main+0x120>
jmp    1210 <main+0x90>

LLVM IR (-O0):
@.str = [14 x i8] c"spacehigh.ihx\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"invaders.f\00"
@.str.3 = [3 x i8] c"r+\00"
@.str.4 = [10 x i8] c":%2x%4x00\00"
@SEEK_SET = global i32 0
@.str.5 = [4 x i8] c"%2x\00"
@.str.6 = [5 x i8] c"%*s\0A\00"
@.str.7 = [9 x i8] c"l %x, %x\00"
@.str.8 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %1
store i32 4096, i32* %2
%9 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %7
%10 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %10, i32** %6
br label %11
11: ; preds = %40, %0
%12 = load i32*, i32** %7
%13 = call i32 @feof(i32* %12)
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
br i1 %15, label %16, label %47
16: ; preds = %11
%17 = load i32*, i32** %7
%18 = call i32 (i32*, i8*, ...) @fscanf(i32* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32* %3, i32* %4)
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %47
22: ; preds = %16
%23 = load i32*, i32** %6
%24 = load i32, i32* %4
%25 = sub i32 %24, 4096
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %23, i32 %25, i32 %26)
store i32 0, i32* %8
br label %28
28: ; preds = %37, %22
%29 = load i32, i32* %8
%30 = load i32, i32* %3
%31 = icmp ult i32 %29, %30
br i1 %31, label %32, label %40
32: ; preds = %28
%33 = load i32*, i32** %7
%34 = call i32 (i32*, i8*, ...) @fscanf(i32* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* %5)
%35 = load i32*, i32** %6
%36 = call i32 @fwrite(i32* %5, i32 1, i32 1, i32* %35)
br label %37
37: ; preds = %32
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
br label %28
40: ; preds = %28
%41 = load i32*, i32** %7
%42 = call i32 (i32*, i8*, ...) @fscanf(i32* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%43 = load i32, i32* %3
%44 = load i32, i32* %4
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %43, i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
br label %11
47: ; preds = %21, %11
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%49 = load i32*, i32** %7
%50 = call i32 @fclose(i32* %49)
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"spacehigh.ihx\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"invaders.f\00"
@.str.3 = [3 x i8] c"r+\00"
@.str.4 = [10 x i8] c":%2x%4x00\00"
@SEEK_SET = global i32 0
@.str.5 = [4 x i8] c"%2x\00"
@.str.6 = [5 x i8] c"%*s\0A\00"
@.str.7 = [9 x i8] c"l %x, %x\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%9 = call i32 @feof(i32* %7)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %39
11: ; preds = %0, %31
%12 = call i32 (i32*, i8*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32* %1, i32* %2)
%13 = load i32, i32* %1
%14 = icmp eq i32 %13, 0
br i1 %14, label %39, label %15
15: ; preds = %11
%16 = load i32, i32* %2
%17 = add i32 %16, -4096
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %8, i32 %17, i32 %18)
%20 = load i32, i32* %1
%21 = icmp eq i32 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %15, %22
%23 = phi i32 [ %28, %22 ], [ 0, %15 ]
%24 = call i32 (i32*, i8*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%25 = load i8, i8* %6
%26 = sext i8 %25 to i32
%27 = call i32 @fputc(i32 %26, i32* %8)
%28 = add nuw nsw i32 %23, 1
%29 = load i32, i32* %1
%30 = icmp ult i32 %28, %29
br i1 %30, label %22, label %31
31: ; preds = %22, %15
%32 = call i32 (i32*, i8*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%33 = load i32, i32* %1
%34 = load i32, i32* %2
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %33, i32 %34)
%36 = call i32 @putchar(i32 10)
%37 = call i32 @feof(i32* %7)
%38 = icmp eq i32 %37, 0
br i1 %38, label %11, label %39
39: ; preds = %31, %11, %0
%40 = call i32 @putchar(i32 10)
%41 = call i32 @fclose(i32* %7)
%42 = call i32 @fclose(i32* %8)
ret i32 0
}