#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 */
struct stat {
  int st_size;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ block;

/* Variables and functions */
scalar_t__ fstat(int, struct stat *);
int read(int, char *, int);
int write(int, char *, int);
int filecopy(int out, int in) {
  char block[32 * 4096];
  ssize_t todo;
  struct stat st;
  if (fstat(in, &st) < 0) {
    ;
    return -1;
  }
  if (!st.st_size) {
    ;
    return 0;
  };
  todo = st.st_size;
  do {
    ssize_t wr_off = 0;
    ssize_t readsize = todo;
    if (todo > sizeof(block))
      readsize = sizeof(block);
    ssize_t res = read(in, block, readsize);
    if (res <= 0) {
      ;
      return -1;
    } else {
      todo -= res;
    }
    do {
      ssize_t written = write(out, block + wr_off, res);
      if (written <= 0) {
        ;
        return -1;
      } else {
        res -= written;
        wr_off += written;
      }
    } while (res);
  } while (todo);
  return 0;
}


x86 assembly (clang, -O0):
<filecopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20030,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20018(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%rax
jge    1166 <filecopy+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <filecopy+0x16a>
cmpl   $0x0,-0x20018(%rbp)
jne    117f <filecopy+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    129a <filecopy+0x16a>
mov    -0x20018(%rbp),%eax
mov    %eax,-0x20014(%rbp)
movl   $0x0,-0x2001c(%rbp)
mov    -0x20014(%rbp),%eax
mov    %eax,-0x20020(%rbp)
movslq -0x20014(%rbp),%rax
cmp    $0x20000,%rax
jbe    11be <filecopy+0x8e>
movl   $0x20000,-0x20020(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20010(%rbp),%rsi
mov    -0x20020(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x20024(%rbp)
cmpl   $0x0,-0x20024(%rbp)
jg     11f2 <filecopy+0xc2>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <filecopy+0x16a>
mov    -0x20024(%rbp),%ecx
mov    -0x20014(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20014(%rbp)
jmp    120b <filecopy+0xdb>
mov    -0x8(%rbp),%edi
lea    -0x20010(%rbp),%rsi
movslq -0x2001c(%rbp),%rax
add    %rax,%rsi
mov    -0x20024(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20028(%rbp)
cmpl   $0x0,-0x20028(%rbp)
jg     1249 <filecopy+0x119>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <filecopy+0x16a>
mov    -0x20028(%rbp),%ecx
mov    -0x20024(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20024(%rbp)
mov    -0x20028(%rbp),%eax
add    -0x2001c(%rbp),%eax
mov    %eax,-0x2001c(%rbp)
jmp    1274 <filecopy+0x144>
cmpl   $0x0,-0x20024(%rbp)
jne    120b <filecopy+0xdb>
jmp    1286 <filecopy+0x156>
cmpl   $0x0,-0x20014(%rbp)
jne    118b <filecopy+0x5b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filecopy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20018,%rsp
mov    %esi,%r15d
mov    %edi,%r13d
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
call   1050 <fstat@plt>
test   %rax,%rax
js     11ba <filecopy+0x8a>
mov    0x8(%rsp),%r12d
test   %r12d,%r12d
je     11c1 <filecopy+0x91>
lea    0x10(%rsp),%r14
cmp    $0x20000,%r12d
mov    $0x20000,%edx
cmovb  %r12d,%edx
mov    %r15d,%edi
mov    %r14,%rsi
call   1040 <read@plt>
test   %eax,%eax
jle    11ba <filecopy+0x8a>
mov    %eax,%ebx
sub    %eax,%r12d
xor    %ebp,%ebp
xchg   %ax,%ax
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    %r13d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    11ba <filecopy+0x8a>
sub    %eax,%ebx
add    %eax,%ebp
test   %ebx,%ebx
jne    1190 <filecopy+0x60>
xor    %eax,%eax
test   %r12d,%r12d
jne    1168 <filecopy+0x38>
jmp    11c3 <filecopy+0x93>
mov    $0xffffffff,%eax
jmp    11c3 <filecopy+0x93>
xor    %eax,%eax
add    $0x20018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @filecopy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [131072 x i8]
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%13 = load i32, i32* %5
%14 = call i64 @fstat(i32 %13, %struct.stat* %8)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %71
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%19 = load i32, i32* %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %17
store i32 0, i32* %3
br label %71
22: ; preds = %17
%23 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %7
br label %25
25: ; preds = %67, %22
store i32 0, i32* %9
%26 = load i32, i32* %7
store i32 %26, i32* %10
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = icmp ugt i64 %28, 131072
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 131072, i32* %10
br label %31
31: ; preds = %30, %25
%32 = load i32, i32* %5
%33 = getelementptr inbounds [131072 x i8], [131072 x i8]* %6, i64 0, i64 0
%34 = load i32, i32* %10
%35 = call i32 @read(i32 %32, i8* %33, i32 %34)
store i32 %35, i32* %11
%36 = load i32, i32* %11
%37 = icmp sle i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %31
store i32 -1, i32* %3
br label %71
39: ; preds = %31
%40 = load i32, i32* %11
%41 = load i32, i32* %7
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %7
br label %43
43: ; preds = %39
br label %44
44: ; preds = %63, %43
%45 = load i32, i32* %4
%46 = getelementptr inbounds [131072 x i8], [131072 x i8]* %6, i64 0, i64 0
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i32, i32* %11
%51 = call i32 @write(i32 %45, i8* %49, i32 %50)
store i32 %51, i32* %12
%52 = load i32, i32* %12
%53 = icmp sle i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %44
store i32 -1, i32* %3
br label %71
55: ; preds = %44
%56 = load i32, i32* %12
%57 = load i32, i32* %11
%58 = sub nsw i32 %57, %56
store i32 %58, i32* %11
%59 = load i32, i32* %12
%60 = load i32, i32* %9
%61 = add nsw i32 %60, %59
store i32 %61, i32* %9
br label %62
62: ; preds = %55
br label %63
63: ; preds = %62
%64 = load i32, i32* %11
%65 = icmp ne i32 %64, 0
br i1 %65, label %44, label %66
66: ; preds = %63
br label %67
67: ; preds = %66
%68 = load i32, i32* %7
%69 = icmp ne i32 %68, 0
br i1 %69, label %25, label %70
70: ; preds = %67
store i32 0, i32* %3
br label %71
71: ; preds = %70, %54, %38, %21, %16
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @filecopy(i32 %0, i32 %1) {
%3 = alloca [131072 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [131072 x i8], [131072 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i64 @fstat(i32 %1, %struct.stat* %4)
%8 = icmp slt i64 %7, 0
br i1 %8, label %34, label %9
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, 0
br i1 %12, label %34, label %13
13: ; preds = %9, %32
%14 = phi i32 [ %20, %32 ], [ %11, %9 ]
%15 = icmp ult i32 %14, 131072
%16 = select i1 %15, i32 %14, i32 131072
%17 = call i32 @read(i32 %1, i8* %5, i32 %16)
%18 = icmp slt i32 %17, 1
br i1 %18, label %34, label %19
19: ; preds = %13
%20 = sub nsw i32 %14, %17
br label %21
21: ; preds = %29, %19
%22 = phi i32 [ 0, %19 ], [ %30, %29 ]
%23 = phi i32 [ %17, %19 ], [ %28, %29 ]
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds [131072 x i8], [131072 x i8]* %3, i64 0, i64 %24
%26 = call i32 @write(i32 %0, i8* %25, i32 %23)
%27 = icmp slt i32 %26, 1
%28 = sub nsw i32 %23, %26
br i1 %27, label %34, label %29
29: ; preds = %21
%30 = add nsw i32 %26, %22
%31 = icmp eq i32 %28, 0
br i1 %31, label %32, label %21
32: ; preds = %29
%33 = icmp eq i32 %20, 0
br i1 %33, label %34, label %13
34: ; preds = %13, %32, %21, %9, %2
%35 = phi i32 [ -1, %2 ], [ 0, %9 ], [ -1, %21 ], [ -1, %13 ], [ 0, %32 ]
ret i32 %35
}