#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ lseek(int, long, int /*<<< orphan*/);
scalar_t__ memcmp(char *, char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  char buf[16];
  if (argc != 2) {
    fprintf(stderr, "%s [binary].\n", argv[0]);
    exit(1);
  }
  fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    perror(argv[1]);
    exit(1);
  }
  if (read(fd, buf, 16) != 16) {
    fprintf(stderr, "%s: could not read the header.\n", argv[1]);
    exit(1);
  }
  if (memcmp(buf + 3, "FZX", 3)) {
    fprintf(stderr, "%s: not a fuzix binary.\n", argv[1]);
    exit(1);
  }
  buf[5] = 'L';
  if (lseek(fd, 0L, SEEK_SET) < 0) {
    perror(argv[1]);
    exit(1);
  }
  if (write(fd, buf, 16) != 16 || close(fd)) {
    fprintf(stderr, "%s: header rewrite failed.\n", argv[1]);
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d6 <main+0x46>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1210 <main+0x80>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
cmp    $0x10,%eax
je     1251 <main+0xc1>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdcc(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x30(%rbp),%rdi
add    $0x3,%rdi
lea    0xdce(%rip),%rsi
mov    $0x3,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     129b <main+0x10b>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xda6(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movb   $0x4c,-0x2b(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d27(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    12d5 <main+0x145>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
cmp    $0x10,%eax
jne    1301 <main+0x171>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     1328 <main+0x198>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd32(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1260 <main+0xd0>
mov    0x8(%rbx),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1237 <main+0xa7>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1060 <read@plt>
cmp    $0x10,%eax
jne    1275 <main+0xe5>
lea    0x3(%rsp),%rdi
lea    0xe4f(%rip),%rsi
mov    $0x3,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
jne    128b <main+0xfb>
movb   $0x4c,0x5(%rsp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     1237 <main+0xa7>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1030 <write@plt>
cmp    $0x10,%eax
jne    124a <main+0xba>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
jne    124a <main+0xba>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x8(%rbx),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xded(%rip),%rsi
jmp    129f <main+0x10f>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd8d(%rip),%rsi
jmp    129f <main+0x10f>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd85(%rip),%rsi
jmp    129f <main+0x10f>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd93(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"%s [binary].\0A\00"
@O_RDWR = global i32 0
@.str.1 = [32 x i8] c"%s: could not read the header.\0A\00"
@.str.2 = [4 x i8] c"FZX\00"
@.str.3 = [25 x i8] c"%s: not a fuzix binary.\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [28 x i8] c"%s: header rewrite failed.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [16 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @perror(i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %17
%32 = load i32, i32* %6
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 16)
%35 = icmp ne i32 %34, 16
br i1 %35, label %36, label %43
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %31
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%45 = getelementptr inbounds i8, i8* %44, i64 3
%46 = call i64 @memcmp(i8* %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %43
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 5
store i8 76, i8* %56
%57 = load i32, i32* %6
%58 = load i32, i32* @SEEK_SET
%59 = call i64 @lseek(i32 %57, i64 0, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %55
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 1
%64 = load i8*, i8** %63
%65 = call i32 @perror(i8* %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %55
%68 = load i32, i32* %6
%69 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%70 = call i32 @write(i32 %68, i8* %69, i32 16)
%71 = icmp ne i32 %70, 16
br i1 %71, label %76, label %72
72: ; preds = %67
%73 = load i32, i32* %6
%74 = call i64 @close(i32 %73)
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %83
76: ; preds = %72, %67
%77 = load i32, i32* @stderr
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 1
%80 = load i8*, i8** %79
%81 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %80)
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %72
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"%s [binary].\0A\00"
@O_RDWR = global i32 0
@.str.1 = [32 x i8] c"%s: could not read the header.\0A\00"
@.str.2 = [4 x i8] c"FZX\00"
@.str.3 = [25 x i8] c"%s: not a fuzix binary.\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [28 x i8] c"%s: header rewrite failed.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i8*, i8** %12
%19 = tail call i32 @perror(i8* %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = call i32 @read(i32 %15, i8* %4, i32 16)
%23 = icmp eq i32 %22, 16
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %12
%27 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 3
%31 = call i64 @memcmp(i8* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %12
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %29
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 5
store i8 76, i8* %39
%40 = load i32, i32* @SEEK_SET
%41 = call i64 @lseek(i32 %15, i64 0, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = load i8*, i8** %12
%45 = call i32 @perror(i8* %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %38
%48 = call i32 @write(i32 %15, i8* %4, i32 16)
%49 = icmp eq i32 %48, 16
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = call i64 @close(i32 %15)
%52 = icmp eq i64 %51, 0
br i1 %52, label %58, label %53
53: ; preds = %50, %47
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %12
%56 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %55)
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %50
ret i32 0
}