#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_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  unsigned char sector[128];
  int fd, drivea, readed;
  if ((drivea = open("../disks/drivea.cpm", O_WRONLY)) == -1) {
    perror("file ../disks/drivea.cpm");
    exit(1);
  }
  if ((fd = open("boot.bin", O_RDONLY)) == -1) {
    perror("file boot.bin");
    exit(1);
  }
  memset((char *)sector, 0, 128);
  read(fd, (char *)sector, 128);
  close(fd);
  write(drivea, (char *)sector, 128);
  if ((fd = open("mpmldr.bin", O_RDONLY)) == -1) {
    perror("file mpmldr.bin");
    exit(1);
  }
  while ((readed = read(fd, (char *)sector, 128)) == 128)
    write(drivea, (char *)sector, 128);
  write(drivea, (char *)sector, 128);
  close(fd);
  close(drivea);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x98(%rbp)
cmp    $0xffffffff,%eax
jne    11bc <main+0x4c>
lea    0xe53(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x94(%rbp)
cmp    $0xffffffff,%eax
jne    11f6 <main+0x86>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    -0x94(%rbp),%edi
call   1050 <close@plt>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    0xdda(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x94(%rbp)
cmp    $0xffffffff,%eax
jne    127c <main+0x10c>
lea    0xdba(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1281 <main+0x111>
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x9c(%rbp)
cmp    $0x80,%eax
jne    12c5 <main+0x155>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
jmp    1281 <main+0x111>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    -0x94(%rbp),%edi
call   1050 <close@plt>
mov    -0x98(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1285 <main+0x115>
mov    %eax,%r14d
mov    0x2e43(%rip),%r15
mov    (%r15),%esi
lea    0xe6f(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     128e <main+0x11e>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    (%r15),%esi
lea    0xe2b(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1297 <main+0x127>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x80,%edx
call   1060 <read@plt>
cmp    $0x80,%eax
jne    1256 <main+0xe6>
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
cmp    $0x80,%eax
je     1230 <main+0xc0>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x80,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd74(%rip),%rdi
jmp    129e <main+0x12e>
lea    0xd84(%rip),%rdi
jmp    129e <main+0x12e>
lea    0xd89(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"../disks/drivea.cpm\00"
@O_WRONLY = global i32 0
@.str.1 = [25 x i8] c"file ../disks/drivea.cpm\00"
@.str.2 = [9 x i8] c"boot.bin\00"
@O_RDONLY = global i32 0
@.str.3 = [14 x i8] c"file boot.bin\00"
@.str.4 = [11 x i8] c"mpmldr.bin\00"
@.str.5 = [16 x i8] c"file mpmldr.bin\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %4
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %13)
store i32 %14, i32* %3
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 128)
%22 = load i32, i32* %3
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 128)
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %4
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%29 = call i32 @write(i32 %27, i8* %28, i32 128)
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %30)
store i32 %31, i32* %3
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %19
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %19
br label %37
37: ; preds = %42, %36
%38 = load i32, i32* %3
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 128)
store i32 %40, i32* %5
%41 = icmp eq i32 %40, 128
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = load i32, i32* %4
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%45 = call i32 @write(i32 %43, i8* %44, i32 128)
br label %37
46: ; preds = %37
%47 = load i32, i32* %4
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%49 = call i32 @write(i32 %47, i8* %48, i32 128)
%50 = load i32, i32* %3
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %4
%53 = call i32 @close(i32 %52)
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"../disks/drivea.cpm\00"
@O_WRONLY = global i32 0
@.str.1 = [25 x i8] c"file ../disks/drivea.cpm\00"
@.str.2 = [9 x i8] c"boot.bin\00"
@O_RDONLY = global i32 0
@.str.3 = [14 x i8] c"file boot.bin\00"
@.str.4 = [11 x i8] c"mpmldr.bin\00"
@.str.5 = [16 x i8] c"file mpmldr.bin\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = call i32 @memset(i8* %2, i32 0, i32 128)
%18 = call i32 @read(i32 %11, i8* %2, i32 128)
%19 = call i32 @close(i32 %11)
%20 = call i32 @write(i32 %4, i8* %2, i32 128)
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %27, label %24
24: ; preds = %16
%25 = call i32 @read(i32 %22, i8* %2, i32 128)
%26 = icmp eq i32 %25, 128
br i1 %26, label %30, label %34
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %24, %30
%31 = call i32 @write(i32 %4, i8* %2, i32 128)
%32 = call i32 @read(i32 %22, i8* %2, i32 128)
%33 = icmp eq i32 %32, 128
br i1 %33, label %30, label %34
34: ; preds = %30, %24
%35 = call i32 @write(i32 %4, i8* %2, i32 128)
%36 = call i32 @close(i32 %22)
%37 = call i32 @close(i32 %4)
ret i32 0
}