#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memmove(char *, unsigned char *, int);
int /*<<< orphan*/ memset(char *, int, int);
char *next_param(int *, char ***);
int /*<<< orphan*/ peek_param(int, char **);
unsigned char *read_hex_file(char *, int, int, int *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  next_param(&argc, &argv);
  char *template = NULL;
  if (strcmp(peek_param(argc, argv), "--template") == 0) {
    next_param(&argc, &argv);
    template = next_param(&argc, &argv);
  }
  char *cpm_loader = next_param(&argc, &argv);
  char *cpm22 = next_param(&argc, &argv);
  char *bios = next_param(&argc, &argv);
  char *output = next_param(&argc, &argv);
  if (argc != 0) {
    usage();
  }
  int size = 1024 * 1024 * 8;
  char *disk = (char *)malloc(size);
  memset(disk, 0xE5, size);
  if (template != NULL) {
    FILE *f = fopen(template, "rb");
    fread(disk, 1, size, f);
    fclose(f);
  }
  unsigned char *buffer;
  int size_read;
  buffer = read_hex_file(cpm_loader, 0x100, 2 * 128, &size_read);
  memmove(disk, buffer, 128 * 2);
  free(buffer);
  buffer = read_hex_file(cpm22, 0xE400, 46 * 128, &size_read);
  memmove(disk + 128 * 2, buffer, 128 * 46);
  free(buffer);
  buffer = read_hex_file(bios, 0xFA00, 128 * 64, &size_read);
  memmove(disk + 128 * 64, buffer, size_read);
  free(buffer);
  FILE *f = fopen(output, "wb");
  fwrite(disk, 1, size, f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <next_param@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10b0 <peek_param@plt>
mov    %eax,%edi
lea    0xdf0(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    123d <main+0x6d>
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <next_param@plt>
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <next_param@plt>
mov    %rax,-0x18(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <next_param@plt>
mov    %rax,-0x20(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <next_param@plt>
mov    %rax,-0x28(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <next_param@plt>
mov    %rax,-0x30(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <next_param@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1292 <main+0xc2>
mov    $0x0,%al
call   10c0 <usage@plt>
movl   $0x800000,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    $0xe5,%esi
call   1060 <memset@plt>
cmpq   $0x0,-0x18(%rbp)
je     12f3 <main+0x123>
mov    -0x18(%rbp),%rdi
lea    0xd3f(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x50(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x50(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
mov    $0x100,%edx
lea    -0x5c(%rbp),%rcx
mov    %edx,%esi
call   1080 <read_hex_file@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    $0x100,%edx
call   10d0 <memmove@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
mov    $0xe400,%esi
mov    $0x1700,%edx
lea    -0x5c(%rbp),%rcx
call   1080 <read_hex_file@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
add    $0x100,%rdi
mov    -0x58(%rbp),%rsi
mov    $0x1700,%edx
call   10d0 <memmove@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
mov    $0xfa00,%esi
mov    $0x2000,%edx
lea    -0x5c(%rbp),%rcx
call   1080 <read_hex_file@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
add    $0x2000,%rdi
mov    -0x58(%rbp),%rsi
mov    -0x5c(%rbp),%edx
call   10d0 <memmove@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
lea    0xc65(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x68(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x68(%rbp),%rcx
mov    $0x1,%esi
call   10f0 <fwrite@plt>
mov    -0x68(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x70,%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    %edi,0xc(%rsp)
mov    %rsi,0x10(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <next_param@plt>
mov    0xc(%rsp),%edi
mov    0x10(%rsp),%rsi
call   10b0 <peek_param@plt>
lea    0xdf5(%rip),%rsi
mov    %eax,%edi
call   1070 <strcmp@plt>
test   %rax,%rax
je     121c <main+0x4c>
xor    %r14d,%r14d
jmp    123f <main+0x6f>
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <next_param@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <next_param@plt>
mov    %rax,%r14
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <next_param@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <next_param@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <next_param@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <next_param@plt>
mov    %rax,0x20(%rsp)
cmpl   $0x0,0xc(%rsp)
je     1291 <main+0xc1>
xor    %eax,%eax
call   10c0 <usage@plt>
mov    $0x800000,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xe5,%esi
mov    $0x800000,%edx
call   1060 <memset@plt>
test   %r14,%r14
je     12e4 <main+0x114>
lea    0xd4f(%rip),%rsi
mov    %r14,%rdi
call   10e0 <fopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x800000,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
lea    0x1c(%rsp),%r14
mov    %r13,%rdi
mov    $0x100,%esi
mov    $0x100,%edx
mov    %r14,%rcx
call   1080 <read_hex_file@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0x100,%edx
call   10d0 <memmove@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
mov    $0xe400,%esi
mov    $0x1700,%edx
mov    %r14,%rcx
call   1080 <read_hex_file@plt>
mov    %rax,%rbp
lea    0x100(%rbx),%rdi
mov    %rax,%rsi
mov    $0x1700,%edx
call   10d0 <memmove@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
mov    $0xfa00,%esi
mov    $0x2000,%edx
mov    %r14,%rcx
call   1080 <read_hex_file@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
add    $0x2000,%rdi
mov    0x1c(%rsp),%edx
mov    %rax,%rsi
call   10d0 <memmove@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
lea    0xc84(%rip),%rsi
mov    0x20(%rsp),%rdi
call   10e0 <fopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x800000,%edx
mov    %rax,%rcx
call   10f0 <fwrite@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"--template\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = call i8* @next_param(i32* %4, i8*** %5)
store i8* null, i8** %6
%18 = load i32, i32* %4
%19 = load i8**, i8*** %5
%20 = call i32 @peek_param(i32 %18, i8** %19)
%21 = call i64 @strcmp(i32 %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = call i8* @next_param(i32* %4, i8*** %5)
%25 = call i8* @next_param(i32* %4, i8*** %5)
store i8* %25, i8** %6
br label %26
26: ; preds = %23, %2
%27 = call i8* @next_param(i32* %4, i8*** %5)
store i8* %27, i8** %7
%28 = call i8* @next_param(i32* %4, i8*** %5)
store i8* %28, i8** %8
%29 = call i8* @next_param(i32* %4, i8*** %5)
store i8* %29, i8** %9
%30 = call i8* @next_param(i32* %4, i8*** %5)
store i8* %30, i8** %10
%31 = load i32, i32* %4
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 (...) @usage()
br label %35
35: ; preds = %33, %26
store i32 8388608, i32* %11
%36 = load i32, i32* %11
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %12
%39 = load i8*, i8** %12
%40 = load i32, i32* %11
%41 = call i32 @memset(i8* %39, i32 229, i32 %40)
%42 = load i8*, i8** %6
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %53
44: ; preds = %35
%45 = load i8*, i8** %6
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %46, i32** %13
%47 = load i8*, i8** %12
%48 = load i32, i32* %11
%49 = load i32*, i32** %13
%50 = call i32 @fread(i8* %47, i32 1, i32 %48, i32* %49)
%51 = load i32*, i32** %13
%52 = call i32 @fclose(i32* %51)
br label %53
53: ; preds = %44, %35
%54 = load i8*, i8** %7
%55 = call i8* @read_hex_file(i8* %54, i32 256, i32 256, i32* %15)
store i8* %55, i8** %14
%56 = load i8*, i8** %12
%57 = load i8*, i8** %14
%58 = call i32 @memmove(i8* %56, i8* %57, i32 256)
%59 = load i8*, i8** %14
%60 = call i32 @free(i8* %59)
%61 = load i8*, i8** %8
%62 = call i8* @read_hex_file(i8* %61, i32 58368, i32 5888, i32* %15)
store i8* %62, i8** %14
%63 = load i8*, i8** %12
%64 = getelementptr inbounds i8, i8* %63, i64 256
%65 = load i8*, i8** %14
%66 = call i32 @memmove(i8* %64, i8* %65, i32 5888)
%67 = load i8*, i8** %14
%68 = call i32 @free(i8* %67)
%69 = load i8*, i8** %9
%70 = call i8* @read_hex_file(i8* %69, i32 64000, i32 8192, i32* %15)
store i8* %70, i8** %14
%71 = load i8*, i8** %12
%72 = getelementptr inbounds i8, i8* %71, i64 8192
%73 = load i8*, i8** %14
%74 = load i32, i32* %15
%75 = call i32 @memmove(i8* %72, i8* %73, i32 %74)
%76 = load i8*, i8** %14
%77 = call i32 @free(i8* %76)
%78 = load i8*, i8** %10
%79 = call i32* @fopen(i8* %78, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %79, i32** %16
%80 = load i8*, i8** %12
%81 = load i32, i32* %11
%82 = load i32*, i32** %16
%83 = call i32 @fwrite(i8* %80, i32 1, i32 %81, i32* %82)
%84 = load i32*, i32** %16
%85 = call i32 @fclose(i32* %84)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"--template\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%6 = call i8* @next_param(i32* %3, i8*** %4)
%7 = load i32, i32* %3
%8 = load i8**, i8*** %4
%9 = call i32 @peek_param(i32 %7, i8** %8)
%10 = call i64 @strcmp(i32 %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i8* @next_param(i32* %3, i8*** %4)
%14 = call i8* @next_param(i32* %3, i8*** %4)
br label %15
15: ; preds = %12, %2
%16 = phi i8* [ %14, %12 ], [ null, %2 ]
%17 = call i8* @next_param(i32* %3, i8*** %4)
%18 = call i8* @next_param(i32* %3, i8*** %4)
%19 = call i8* @next_param(i32* %3, i8*** %4)
%20 = call i8* @next_param(i32* %3, i8*** %4)
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = call i32 (...) @usage()
br label %25
25: ; preds = %23, %15
%26 = call i64 @malloc(i32 8388608)
%27 = inttoptr i64 %26 to i8*
%28 = call i32 @memset(i8* %27, i32 229, i32 8388608)
%29 = icmp eq i8* %16, null
br i1 %29, label %34, label %30
30: ; preds = %25
%31 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @fread(i8* %27, i32 1, i32 8388608, i32* %31)
%33 = call i32 @fclose(i32* %31)
br label %34
34: ; preds = %30, %25
%35 = bitcast i32* %5 to i8*
%36 = call i8* @read_hex_file(i8* %17, i32 256, i32 256, i32* %5)
%37 = call i32 @memmove(i8* %27, i8* %36, i32 256)
%38 = call i32 @free(i8* %36)
%39 = call i8* @read_hex_file(i8* %18, i32 58368, i32 5888, i32* %5)
%40 = getelementptr inbounds i8, i8* %27, i64 256
%41 = call i32 @memmove(i8* %40, i8* %39, i32 5888)
%42 = call i32 @free(i8* %39)
%43 = call i8* @read_hex_file(i8* %19, i32 64000, i32 8192, i32* %5)
%44 = getelementptr inbounds i8, i8* %27, i64 8192
%45 = load i32, i32* %5
%46 = call i32 @memmove(i8* %44, i8* %43, i32 %45)
%47 = call i32 @free(i8* %43)
%48 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @fwrite(i8* %27, i32 1, i32 8388608, i32* %48)
%50 = call i32 @fclose(i32* %48)
ret i32 0
}