#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*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t write(int, char *, size_t);
void copy(int fd_in, int fd_out) {
  char buffer[1024] = {'\0'};
  size_t len;
  while ((len = read(fd_in, buffer, 1024)) != 0) {
    if (write(fd_out, buffer, len) != len) {
      fprintf(stderr, "write error:%s\n", strerror(errno));
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     120d <copy+0xad>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x418(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x418(%rbp),%rax
je     1208 <copy+0xa8>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x41c(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x41c(%rbp),%edi
mov    %rax,%rdx
lea    0xe07(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1184 <copy+0x24>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
mov    %rsp,%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11ed <copy+0x8d>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
je     1190 <copy+0x30>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe27(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"write error:%s\0A\00"
@errno = global i32 0
define void @copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = bitcast [1024 x i8]* %5 to i8*
br label %8
8: ; preds = %26, %2
%9 = load i32, i32* %3
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = call i64 @read(i32 %9, i8* %10, i32 1024)
store i64 %11, i64* %6
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %8
%14 = load i32, i32* %4
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = load i64, i64* %6
%17 = call i64 @write(i32 %14, i8* %15, i64 %16)
%18 = load i64, i64* %6
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %26
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %13
br label %8
27: ; preds = %8
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"write error:%s\0A\00"
@errno = global i32 0
define void @copy(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %8, %2
%6 = call i64 @read(i32 %0, i8* %4, i32 1024)
%7 = icmp eq i64 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %5
%9 = call i64 @write(i32 %1, i8* %4, i64 %6)
%10 = icmp eq i64 %9, %6
br i1 %10, label %5, label %11
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %5
ret void
}