#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 O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int buf_count;
  int file1, file2;
  char buf[512];
  file1 = open(argv[1], O_RDONLY);
  if (file1 < 0) {
    perror("file1 open error");
    exit(0);
  }
  file2 = open(argv[2], O_WRONLY | O_CREAT, 0666);
  if (file2 < 0) {
    perror("file2 open error");
    exit(0);
  }
  do {
    buf_count = read(file1, buf, 512);
    write(file2, buf, buf_count);
  } while (buf_count == 512);
  close(file1);
  close(file2);
  return 0;
}


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    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b1 <main+0x51>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f7 <main+0x97>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    11fc <main+0x9c>
mov    -0x18(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
cmpl   $0x200,-0x14(%rbp)
je     11fc <main+0x9c>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     1207 <main+0xa7>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
mov    0x2e32(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     1210 <main+0xb0>
mov    %eax,%r15d
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x200,%ebp
je     11c0 <main+0x60>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf2(%rip),%rdi
jmp    1217 <main+0xb7>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [17 x i8] c"file1 open error\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [17 x i8] c"file2 open error\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 i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = call i32 (i8*, i32, ...) @open(i8* %23, i32 %26, i32 438)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %20
%31 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %20
br label %34
34: ; preds = %42, %33
%35 = load i32, i32* %7
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 512)
store i32 %37, i32* %6
%38 = load i32, i32* %8
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%40 = load i32, i32* %6
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
br label %42
42: ; preds = %34
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, 512
br i1 %44, label %34, label %45
45: ; preds = %42
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [17 x i8] c"file1 open error\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [17 x i8] c"file2 open error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 (i8*, i32, ...) @open(i8* %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = tail call i32 (i8*, i32, ...) @open(i8* %15, i32 %18, i32 438)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %13, %24
%25 = call i32 @read(i32 %8, i8* %4, i32 512)
%26 = call i32 @write(i32 %19, i8* %4, i32 %25)
%27 = icmp eq i32 %25, 512
br i1 %27, label %24, label %28
28: ; preds = %24
%29 = tail call i32 @close(i32 %8)
%30 = tail call i32 @close(i32 %19)
ret i32 0
}