#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 /*<<< orphan*/ error(char *);
int read(int, char *, int);
int write(int, char *, int);
void filecopy(int ifd, int ofd) {
  int n;
  char buf[BUFSIZ];
  while ((n = read(ifd, buf, BUFSIZ)) > 0)
    if (write(ofd, buf, n) != n) {
      error("cat: write error");
    }
}


x86 assembly (clang, -O0):
<filecopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e9b(%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,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    11b4 <filecopy+0x84>
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
cmp    -0xc(%rbp),%eax
je     11af <filecopy+0x7f>
lea    0xe56(%rip),%rdi
call   1050 <error@plt>
jmp    116a <filecopy+0x3a>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
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    %esi,%r14d
mov    %edi,%r15d
mov    0x2e95(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%r13
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rsi
call   1040 <read@plt>
test   %eax,%eax
jle    11b4 <filecopy+0x84>
mov    %eax,%ebx
jmp    1195 <filecopy+0x65>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12),%edx
mov    %r15d,%edi
mov    %r13,%rsi
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11b4 <filecopy+0x84>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1180 <filecopy+0x50>
lea    0xe53(%rip),%rdi
call   1050 <error@plt>
jmp    1180 <filecopy+0x50>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [17 x i8] c"cat: write error\00"
define void @filecopy(i32 %0, i32 %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @read(i32 %0, i8* %5, i32 %3)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %2, %14
%9 = phi i32 [ %16, %14 ], [ %6, %2 ]
%10 = call i32 @write(i32 %1, i8* %5, i32 %9)
%11 = icmp eq i32 %10, %9
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = load i32, i32* @BUFSIZ
%16 = call i32 @read(i32 %0, i8* %5, i32 %15)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %8, label %18
18: ; preds = %14, %2
ret void
}