#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*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int copy(int in, int out) {
  char buf[1024];
  int sread;
  while ((sread = read(in, buf, 1024)) > 0) {
    if (write(out, buf, sread) == -1) {
      perror("write");
      return -1;
    }
  }
  if (sread == -1) {
    perror("read");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    119f <copy+0x6f>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    119a <copy+0x6a>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <copy+0x9b>
jmp    1141 <copy+0x11>
cmpl   $0xffffffff,-0x414(%rbp)
jne    11c4 <copy+0x94>
lea    0xe53(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <copy+0x9b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r15d
mov    %edi,%ebx
mov    %rsp,%rbp
lea    0xeb4(%rip),%r14
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1177 <copy+0x47>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1150 <copy+0x20>
jmp    1195 <copy+0x65>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
je     118e <copy+0x5e>
mov    %ecx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe71(%rip),%r14
mov    %r14,%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ecx
jmp    117e <copy+0x4e>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %8
8: ; preds = %21, %2
%9 = load i32, i32* %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 1024)
store i32 %11, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %8
%14 = load i32, i32* %5
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* %7
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %28
21: ; preds = %13
br label %8
22: ; preds = %8
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %28
27: ; preds = %22
store i32 0, i32* %3
br label %28
28: ; preds = %27, %25, %19
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @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 i32 @read(i32 %0, i8* %4, i32 1024)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = call i32 @write(i32 %1, i8* %4, i32 %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %13, label %5
11: ; preds = %5
%12 = icmp eq i32 %6, -1
br i1 %12, label %13, label %16
13: ; preds = %8, %11
%14 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %8 ]
%15 = tail call i32 @perror(i8* %14)
br label %16
16: ; preds = %13, %11
%17 = phi i32 [ 0, %11 ], [ -1, %13 ]
ret i32 %17
}