#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 */
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ die(char *);
int read(int, char *, int);
int write(int, char *, int);
void copy_data(int from, int to) {
  char buff[4096];
  int len;
  while ((len = read(from, buff, sizeof(buff))) > 0) {
    if (write(to, buff, len) != len) {
      die("Write error");
      return;
    }
  }
  if (len < 0) {
    die("Read error");
  }
}


x86 assembly (clang, -O0):
<copy_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
jle    119b <copy_data+0x6b>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1014(%rbp),%eax
je     1196 <copy_data+0x66>
lea    0xe74(%rip),%rdi
call   1050 <die@plt>
jmp    11b4 <copy_data+0x84>
jmp    1141 <copy_data+0x11>
cmpl   $0x0,-0x1014(%rbp)
jge    11b4 <copy_data+0x84>
lea    0xe5d(%rip),%rdi
call   1050 <die@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_data>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %esi,%r15d
mov    %edi,%ebx
mov    %rsp,%r12
lea    0xeb2(%rip),%r14
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1178 <copy_data+0x48>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1150 <copy_data+0x20>
jmp    1191 <copy_data+0x61>
js     118a <copy_data+0x5a>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe7b(%rip),%r14
mov    %r14,%rdi
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <die@plt>

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

LLVM IR (-O2):
@.str = [12 x i8] c"Write error\00"
@.str.1 = [11 x i8] c"Read error\00"
define void @copy_data(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %8, %2
%6 = call i32 @read(i32 %0, i8* %4, i32 4096)
%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, %6
br i1 %10, label %5, label %13
11: ; preds = %5
%12 = icmp slt i32 %6, 0
br i1 %12, label %13, label %16
13: ; preds = %8, %11
%14 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %8 ]
%15 = tail call i32 @die(i8* %14)
br label %16
16: ; preds = %13, %11
ret void
}