#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*/ chmem_fzx1(int /*<<< orphan*/ *, unsigned char *, int,
                              char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ memcmp(unsigned char *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *fp;
  unsigned char buf[32];
  if (argc != 2 && argc != 3) {
    fprintf(stderr, "%s [executable] {size}\n", argv[0]);
    exit(1);
  }
  if (argc == 2)
    fp = fopen(argv[1], "r");
  else
    fp = fopen(argv[1], "r+");
  if (fp == NULL) {
    perror(argv[1]);
    exit(1);
  }
  if (fread(buf, 32, 1, fp) != 1) {
    fprintf(stderr, "%s: too short ?\n", argv[0]);
    exit(1);
  }
  if (memcmp(buf + 3, "FZX1", 4) == 0)
    chmem_fzx1(fp, buf, argc, argv);
  else {
    fprintf(stderr, "%s: not a Fuzix binary format.\n", argv[1]);
    exit(1);
  }
  fclose(fp);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d0 <main+0x50>
cmpl   $0x3,-0x8(%rbp)
je     11d0 <main+0x50>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3f(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    11f7 <main+0x77>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    120f <main+0x8f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe14(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1231 <main+0xb1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x20,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1277 <main+0xf7>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb5(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x40(%rbp),%rdi
add    $0x3,%rdi
lea    0xda8(%rip),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
jne    12b3 <main+0x133>
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    -0x10(%rbp),%rcx
call   1050 <chmem_fzx1@plt>
jmp    12da <main+0x15a>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd68(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    11cd <main+0x4d>
mov    %edi,%ebp
cmp    $0x2,%edi
mov    0x8(%rbx),%rdi
lea    0xe73(%rip),%rax
lea    0xe6e(%rip),%rsi
cmove  %rax,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
jne    11ef <main+0x6f>
mov    0x8(%rbx),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe20(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x20,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1246 <main+0xc6>
lea    0x3(%rsp),%rdi
lea    0xe16(%rip),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
jne    125b <main+0xdb>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1050 <chmem_fzx1@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdc4(%rip),%rsi
jmp    11e0 <main+0x60>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xdc4(%rip),%rsi
jmp    11e0 <main+0x60>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"%s [executable] {size}\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [17 x i8] c"%s: too short ?\0A\00"
@.str.4 = [5 x i8] c"FZX1\00"
@.str.5 = [32 x i8] c"%s: not a Fuzix binary format.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [32 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 %20
10: ; preds = %2
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %20
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %10, %2
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 2
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %6
br label %33
28: ; preds = %20
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %6
br label %33
33: ; preds = %28, %23
%34 = load i32*, i32** %6
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i32 @perror(i8* %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%44 = load i32*, i32** %6
%45 = call i32 @fread(i8* %43, i32 32, i32 1, i32* %44)
%46 = icmp ne i32 %45, 1
br i1 %46, label %47, label %54
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 0
%51 = load i8*, i8** %50
%52 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %42
%55 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%56 = getelementptr inbounds i8, i8* %55, i64 3
%57 = call i64 @memcmp(i8* %56, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %54
%60 = load i32*, i32** %6
%61 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%62 = load i32, i32* %4
%63 = load i8**, i8*** %5
%64 = call i32 @chmem_fzx1(i32* %60, i8* %61, i32 %62, i8** %63)
br label %72
65: ; preds = %54
%66 = load i32, i32* @stderr
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 1
%69 = load i8*, i8** %68
%70 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %69)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %59
%73 = load i32*, i32** %6
%74 = call i32 @fclose(i32* %73)
%75 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"%s [executable] {size}\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [17 x i8] c"%s: too short ?\0A\00"
@.str.4 = [5 x i8] c"FZX1\00"
@.str.5 = [32 x i8] c"%s: not a Fuzix binary format.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = and i32 %0, -2
%6 = icmp eq i32 %5, 2
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = icmp eq i32 %0, 2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = select i1 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0)
%17 = tail call i32* @fopen(i8* %15, i8* %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 @perror(i8* %21)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %12
%25 = call i32 @fread(i8* %4, i32 32, i32 1, i32* %17)
%26 = icmp eq i32 %25, 1
br i1 %26, label %32, label %27
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %1
%30 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 3
%34 = call i64 @memcmp(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = call i32 @chmem_fzx1(i32* %17, i8* %4, i32 %0, i8** %1)
%38 = call i32 @fclose(i32* %17)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = getelementptr inbounds i8*, i8** %1, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %43)
%45 = call i32 @exit(i32 1)
unreachable
}