#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*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int f_in;
  int f_out;
  int count;
  char buffer[512];
  f_in = open("boot.bin", O_RDONLY);
  if (f_in < 0) {
    perror("open boot.bin error:");
    return 0;
  }
  f_out = open("floppy.vfd", O_WRONLY);
  if (f_out < 0) {
    perror("open floppy.vfd error:");
    return 0;
  }
  while ((count = read(f_in, buffer, 512)) > 0) {
    write(f_out, buffer, count);
    memset(buffer, 0, 512);
  }
  printf("Write image ok!\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b3 <main+0x53>
lea    0xe67(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    124f <main+0xef>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ed <main+0x8d>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    124f <main+0xef>
jmp    11f2 <main+0x92>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    123c <main+0xdc>
mov    -0x18(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
jmp    11f2 <main+0x92>
lea    0xdfd(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     120c <main+0xac>
mov    %eax,%r14d
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1215 <main+0xb5>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x200,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11ef <main+0x8f>
mov    %rsp,%rbx
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     11c0 <main+0x60>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
jmp    121c <main+0xbc>
lea    0xe0d(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
jmp    1200 <main+0xa0>

LLVM IR (-O0):
@.str = [9 x i8] c"boot.bin\00"
@O_RDONLY = global i32 0
@.str.1 = [21 x i8] c"open boot.bin error:\00"
@.str.2 = [11 x i8] c"floppy.vfd\00"
@O_WRONLY = global i32 0
@.str.3 = [23 x i8] c"open floppy.vfd error:\00"
@.str.4 = [17 x i8] c"Write image ok!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %38
16: ; preds = %2
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %38
23: ; preds = %16
br label %24
24: ; preds = %29, %23
%25 = load i32, i32* %6
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 512)
store i32 %27, i32* %8
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %24
%30 = load i32, i32* %7
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%32 = load i32, i32* %8
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%35 = call i32 @memset(i8* %34, i32 0, i32 512)
br label %24
36: ; preds = %24
%37 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %38
38: ; preds = %36, %21, %14
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [9 x i8] c"boot.bin\00"
@O_RDONLY = global i32 0
@.str.1 = [21 x i8] c"open boot.bin error:\00"
@.str.2 = [11 x i8] c"floppy.vfd\00"
@O_WRONLY = global i32 0
@.str.3 = [23 x i8] c"open floppy.vfd error:\00"
@str = [16 x i8] c"Write image ok!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %27
10: ; preds = %2
%11 = load i32, i32* @O_WRONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = call i32 @read(i32 %6, i8* %4, i32 512)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %19, label %25
17: ; preds = %10
%18 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %27
19: ; preds = %14, %19
%20 = phi i32 [ %23, %19 ], [ %15, %14 ]
%21 = call i32 @write(i32 %12, i8* %4, i32 %20)
%22 = call i32 @memset(i8* %4, i32 0, i32 512)
%23 = call i32 @read(i32 %6, i8* %4, i32 512)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %19, label %25
25: ; preds = %19, %14
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %27
27: ; preds = %25, %17, %8
%28 = phi i32 [ 0, %8 ], [ 0, %17 ], [ 1, %25 ]
ret i32 %28
}