#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, scalar_t__);
int copy_data(int srcfd, int dstfd) {
  char buf[8192];
  ssize_t n;
  int total = 0;
  while ((n = read(srcfd, buf, sizeof(buf))) > 0) {
    char *bp = buf;
    ssize_t r = 0;
    while (n > 0 && (r = write(dstfd, bp, n)) > 0) {
      bp += r;
      n -= r;
      total += r;
    }
    if (r == 0) {
      fprintf(stderr, "Warning: EOF writing to output file.\n");
      return (-1);
    }
  }
  return (total);
}


x86 assembly (clang, -O0):
<copy_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x201c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2010(%rbp),%rsi
mov    $0x2000,%edx
call   1040 <read@plt>
mov    %rax,-0x2018(%rbp)
cmp    $0x0,%rax
jle    1259 <copy_data+0x129>
lea    -0x2010(%rbp),%rax
mov    %rax,-0x2028(%rbp)
movq   $0x0,-0x2030(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x2018(%rbp)
mov    %al,-0x2031(%rbp)
jle    11c9 <copy_data+0x99>
mov    -0xc(%rbp),%edi
mov    -0x2028(%rbp),%rsi
mov    -0x2018(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x2030(%rbp)
cmp    $0x0,%rax
setg   %al
mov    %al,-0x2031(%rbp)
mov    -0x2031(%rbp),%al
test   $0x1,%al
jne    11dc <copy_data+0xac>
jmp    1225 <copy_data+0xf5>
mov    -0x2030(%rbp),%rax
add    -0x2028(%rbp),%rax
mov    %rax,-0x2028(%rbp)
mov    -0x2030(%rbp),%rcx
mov    -0x2018(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x2018(%rbp)
mov    -0x2030(%rbp),%rcx
movslq -0x201c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x201c(%rbp)
jmp    1189 <copy_data+0x59>
cmpq   $0x0,-0x2030(%rbp)
jne    1254 <copy_data+0x124>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <copy_data+0x132>
jmp    114b <copy_data+0x1b>
mov    -0x201c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
mov    %esi,%r12d
mov    %edi,%r14d
xor    %r13d,%r13d
mov    %rsp,%r15
jmp    1155 <copy_data+0x25>
nop
test   %rax,%rax
je     1190 <copy_data+0x60>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x2000,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11ab <copy_data+0x7b>
mov    %rax,%rbp
mov    %r15,%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
jle    1150 <copy_data+0x20>
add    %rax,%rbx
add    %eax,%r13d
sub    %rax,%rbp
jg     1170 <copy_data+0x40>
jmp    1150 <copy_data+0x20>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Warning: EOF writing to output file.\0A\00"
define i32 @copy_data(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [8192 x i8]
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %8
br label %11
11: ; preds = %47, %2
%12 = load i32, i32* %4
%13 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%14 = call i64 @read(i32 %12, i8* %13, i32 8192)
store i64 %14, i64* %7
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %48
16: ; preds = %11
%17 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
store i8* %17, i8** %9
store i64 0, i64* %10
br label %18
18: ; preds = %29, %16
%19 = load i64, i64* %7
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* %5
%23 = load i8*, i8** %9
%24 = load i64, i64* %7
%25 = call i64 @write(i32 %22, i8* %23, i64 %24)
store i64 %25, i64* %10
%26 = icmp sgt i64 %25, 0
br label %27
27: ; preds = %21, %18
%28 = phi i1 [ false, %18 ], [ %26, %21 ]
br i1 %28, label %29, label %41
29: ; preds = %27
%30 = load i64, i64* %10
%31 = load i8*, i8** %9
%32 = getelementptr inbounds i8, i8* %31, i64 %30
store i8* %32, i8** %9
%33 = load i64, i64* %10
%34 = load i64, i64* %7
%35 = sub nsw i64 %34, %33
store i64 %35, i64* %7
%36 = load i64, i64* %10
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = add nsw i64 %38, %36
%40 = trunc i64 %39 to i32
store i32 %40, i32* %8
br label %18
41: ; preds = %27
%42 = load i64, i64* %10
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
47: ; preds = %41
br label %11
48: ; preds = %11
%49 = load i32, i32* %8
store i32 %49, i32* %3
br label %50
50: ; preds = %48, %44
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Warning: EOF writing to output file.\0A\00"
define i32 @copy_data(i32 %0, i32 %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %21, %2
%6 = phi i32 [ 0, %2 ], [ %22, %21 ]
%7 = call i64 @read(i32 %0, i8* %4, i32 8192)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %5, %15
%10 = phi i8* [ %16, %15 ], [ %4, %5 ]
%11 = phi i32 [ %19, %15 ], [ %6, %5 ]
%12 = phi i64 [ %17, %15 ], [ %7, %5 ]
%13 = call i64 @write(i32 %1, i8* %10, i64 %12)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %9
%16 = getelementptr inbounds i8, i8* %10, i64 %13
%17 = sub nsw i64 %12, %13
%18 = trunc i64 %13 to i32
%19 = add i32 %11, %18
%20 = icmp sgt i64 %17, 0
br i1 %20, label %9, label %21
21: ; preds = %9, %15
%22 = phi i32 [ %19, %15 ], [ %11, %9 ]
%23 = icmp eq i64 %13, 0
br i1 %23, label %24, label %5
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %5, %24
%28 = phi i32 [ -1, %24 ], [ %6, %5 ]
ret i32 %28
}