#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*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
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("cpmldr.bin", O_RDONLY)) == -1) {
    perror("file cpmldr.bin");
    exit(1);
  }
  while ((readed = read(fd, (char *)sector, 128)) == 128)
    write(drivea, (char *)sector, 128);
  write(drivea, (char *)sector, 128);
  close(fd);
  lseek(drivea, 128 * 26, SEEK_SET);
  if ((fd = open("ccp.bin", O_RDONLY)) == -1) {
    perror("file ccp.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    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x98(%rbp)
cmp    $0xffffffff,%eax
jne    11cc <main+0x4c>
lea    0xe43(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x94(%rbp)
cmp    $0xffffffff,%eax
jne    1206 <main+0x86>
lea    0xe22(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    -0x94(%rbp),%edi
call   1060 <close@plt>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
lea    0xdca(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x94(%rbp)
cmp    $0xffffffff,%eax
jne    128c <main+0x10c>
lea    0xdaa(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1291 <main+0x111>
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x9c(%rbp)
cmp    $0x80,%eax
jne    12d5 <main+0x155>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
jmp    1291 <main+0x111>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    -0x94(%rbp),%edi
call   1060 <close@plt>
mov    -0x98(%rbp),%edi
mov    0x2cd4(%rip),%rax
mov    (%rax),%edx
mov    $0xd00,%esi
call   1040 <lseek@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%esi
lea    0xd1c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x94(%rbp)
cmp    $0xffffffff,%eax
jne    134a <main+0x1ca>
lea    0xcfc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    134f <main+0x1cf>
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x9c(%rbp)
cmp    $0x80,%eax
jne    1393 <main+0x213>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
jmp    134f <main+0x1cf>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    -0x94(%rbp),%edi
call   1060 <close@plt>
mov    -0x98(%rbp),%edi
call   1060 <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    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1325 <main+0x1a5>
mov    %eax,%r14d
mov    0x2e33(%rip),%r15
mov    (%r15),%esi
lea    0xe5f(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     132e <main+0x1ae>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    (%r15),%esi
lea    0xe1b(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1337 <main+0x1b7>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x80,%edx
call   1070 <read@plt>
cmp    $0x80,%eax
jne    1266 <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   1070 <read@plt>
cmp    $0x80,%eax
je     1240 <main+0xc0>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x80,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    $0xd00,%esi
call   1040 <lseek@plt>
mov    (%r15),%esi
lea    0xd9f(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1340 <main+0x1c0>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x80,%edx
call   1070 <read@plt>
cmp    $0x80,%eax
jne    12f6 <main+0x176>
mov    %rsp,%rbx
cs 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   1070 <read@plt>
cmp    $0x80,%eax
je     12d0 <main+0x150>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x80,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd4(%rip),%rdi
jmp    1347 <main+0x1c7>
lea    0xce4(%rip),%rdi
jmp    1347 <main+0x1c7>
lea    0xce9(%rip),%rdi
jmp    1347 <main+0x1c7>
lea    0xcf0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <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"cpmldr.bin\00"
@.str.5 = [16 x i8] c"file cpmldr.bin\00"
@SEEK_SET = global i32 0
@.str.6 = [8 x i8] c"ccp.bin\00"
@.str.7 = [13 x i8] c"file ccp.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 = load i32, i32* @SEEK_SET
%54 = call i32 @lseek(i32 %52, i32 3328, i32 %53)
%55 = load i32, i32* @O_RDONLY
%56 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %55)
store i32 %56, i32* %3
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %46
%59 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %46
br label %62
62: ; preds = %67, %61
%63 = load i32, i32* %3
%64 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%65 = call i32 @read(i32 %63, i8* %64, i32 128)
store i32 %65, i32* %5
%66 = icmp eq i32 %65, 128
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = load i32, i32* %4
%69 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%70 = call i32 @write(i32 %68, i8* %69, i32 128)
br label %62
71: ; preds = %62
%72 = load i32, i32* %4
%73 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%74 = call i32 @write(i32 %72, i8* %73, i32 128)
%75 = load i32, i32* %3
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* %4
%78 = call i32 @close(i32 %77)
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"cpmldr.bin\00"
@.str.5 = [16 x i8] c"file cpmldr.bin\00"
@SEEK_SET = global i32 0
@.str.6 = [8 x i8] c"ccp.bin\00"
@.str.7 = [13 x i8] c"file ccp.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 = load i32, i32* @SEEK_SET
%38 = call i32 @lseek(i32 %4, i32 3328, i32 %37)
%39 = load i32, i32* @O_RDONLY
%40 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %45, label %42
42: ; preds = %34
%43 = call i32 @read(i32 %40, i8* %2, i32 128)
%44 = icmp eq i32 %43, 128
br i1 %44, label %48, label %52
45: ; preds = %34
%46 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %42, %48
%49 = call i32 @write(i32 %4, i8* %2, i32 128)
%50 = call i32 @read(i32 %40, i8* %2, i32 128)
%51 = icmp eq i32 %50, 128
br i1 %51, label %48, label %52
52: ; preds = %48, %42
%53 = call i32 @write(i32 %4, i8* %2, i32 128)
%54 = call i32 @close(i32 %40)
%55 = call i32 @close(i32 %4)
ret i32 0
}