#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 BUFSIZ;
int EOF;
int read(int, char *, int);
int write(int, char *, int);
int filecopy(int fd1) {
  int status;
  char buf[BUFSIZ];
  while (((status = read(fd1, buf, BUFSIZ)) != EOF) && status != 0) {
    if (write(1, buf, status) != status) {
      return -3;
    }
  }
  if (status == EOF) {
    return -2;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<filecopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
mov    0x2e6e(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x31(%rbp)
je     118f <filecopy+0x6f>
cmpl   $0x0,-0xc(%rbp)
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    119f <filecopy+0x7f>
jmp    11d1 <filecopy+0xb1>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0xc(%rbp),%eax
je     11cc <filecopy+0xac>
movl   $0xfffffffd,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1204 <filecopy+0xe4>
jmp    1157 <filecopy+0x37>
mov    -0xc(%rbp),%eax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%eax
jne    11f6 <filecopy+0xd6>
movl   $0xfffffffe,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    1204 <filecopy+0xe4>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filecopy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2ea0(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%r15
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rsi
call   1040 <read@plt>
mov    %eax,%ebx
mov    0x2e87(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%eax
test   %ebx,%ebx
je     11a2 <filecopy+0x82>
cmp    %eax,%ebx
je     11a2 <filecopy+0x82>
mov    $0x1,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    11b1 <filecopy+0x91>
mov    (%r12),%edx
mov    %r14d,%edi
mov    %r15,%rsi
call   1040 <read@plt>
mov    %eax,%ebx
jmp    1170 <filecopy+0x50>
xor    %ecx,%ecx
cmp    %eax,%ebx
setne  %cl
lea    (%rcx,%rcx,1),%eax
add    $0xfffffffe,%eax
jmp    11b6 <filecopy+0x96>
mov    $0xfffffffd,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@EOF = global i32 0
define i32 @filecopy(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @BUFSIZ
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %5
%11 = alloca i8, i64 %9
store i64 %9, i64* %6
br label %12
12: ; preds = %29, %1
%13 = load i32, i32* %3
%14 = load i32, i32* @BUFSIZ
%15 = call i32 @read(i32 %13, i8* %11, i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 0
br label %21
21: ; preds = %18, %12
%22 = phi i1 [ false, %12 ], [ %20, %18 ]
br i1 %22, label %23, label %30
23: ; preds = %21
%24 = load i32, i32* %4
%25 = call i32 @write(i32 1, i8* %11, i32 %24)
%26 = load i32, i32* %4
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -3, i32* %2
store i32 1, i32* %7
br label %36
29: ; preds = %23
br label %12
30: ; preds = %21
%31 = load i32, i32* %4
%32 = load i32, i32* @EOF
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -2, i32* %2
store i32 1, i32* %7
br label %36
35: ; preds = %30
store i32 0, i32* %2
store i32 1, i32* %7
br label %36
36: ; preds = %35, %34, %28
%37 = load i8*, i8** %5
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@EOF = global i32 0
define i32 @filecopy(i32 %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @read(i32 %0, i8* %4, i32 %2)
%6 = load i32, i32* @EOF
%7 = icmp ne i32 %5, %6
%8 = icmp ne i32 %5, 0
%9 = and i1 %8, %7
br i1 %9, label %10, label %21
10: ; preds = %1, %14
%11 = phi i32 [ %16, %14 ], [ %5, %1 ]
%12 = call i32 @write(i32 1, i8* %4, i32 %11)
%13 = icmp eq i32 %12, %11
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = load i32, i32* @BUFSIZ
%16 = call i32 @read(i32 %0, i8* %4, i32 %15)
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
%19 = icmp ne i32 %16, 0
%20 = and i1 %19, %18
br i1 %20, label %10, label %21
21: ; preds = %14, %1
%22 = phi i32 [ %5, %1 ], [ %16, %14 ]
%23 = phi i32 [ %6, %1 ], [ %17, %14 ]
%24 = icmp eq i32 %22, %23
%25 = select i1 %24, i32 -2, i32 0
br label %26
26: ; preds = %10, %21
%27 = phi i32 [ %25, %21 ], [ -3, %10 ]
ret i32 %27
}