#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, int);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int write_to_fd(FILE *fout, FILE *fin, int size) {
  char *buffer;
  int chunk;
  size_t ret;
  buffer = malloc((64 * 1024));
  while (size > 0) {
    chunk = size < (64 * 1024) ? size : (64 * 1024);
    ret = fread(buffer, 1, chunk, fin);
    if (ret != chunk) {
      fprintf(stderr, "%d vs. %d\n", ret, chunk);
      perror("fread");
      return -1;
    }
    ret = fwrite(buffer, 1, chunk, fout);
    if (ret != chunk) {
      perror("fwrite");
      return -1;
    }
    size -= chunk;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_to_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0x10000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1256 <write_to_fd+0x106>
cmpl   $0x10000,-0x1c(%rbp)
jge    1193 <write_to_fd+0x43>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    11a0 <write_to_fd+0x50>
mov    $0x10000,%eax
mov    %eax,-0x3c(%rbp)
jmp    11a0 <write_to_fd+0x50>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmp    %rcx,%rax
je     1204 <write_to_fd+0xb4>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
lea    0xe19(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe18(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <write_to_fd+0x10d>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmp    %rcx,%rax
je     1246 <write_to_fd+0xf6>
lea    0xddc(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <write_to_fd+0x10d>
mov    -0x2c(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1171 <write_to_fd+0x21>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r13
mov    $0x10000,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
test   %ebp,%ebp
jle    11cc <write_to_fd+0x7c>
cs nopw 0x0(%rax,%rax,1)
cmp    $0x10000,%ebp
mov    $0x10000,%r12d
cmovb  %ebp,%r12d
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
cmp    %r12,%rax
jne    11db <write_to_fd+0x8b>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
cmp    %r14,%rax
jne    11ff <write_to_fd+0xaf>
sub    %r12d,%ebp
test   %ebp,%ebp
jg     1180 <write_to_fd+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    %r14,%rdx
mov    %r12d,%ecx
call   1040 <fprintf@plt>
lea    0xe0e(%rip),%rdi
jmp    1206 <write_to_fd+0xb6>
lea    0xe0b(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11cc <write_to_fd+0x7c>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [11 x i8] c"%d vs. %d\0A\00"
@.str.1 = [6 x i8] c"fread\00"
@.str.2 = [7 x i8] c"fwrite\00"
define i32 @write_to_fd(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%11 = call i8* @malloc(i32 65536)
store i8* %11, i8** %8
br label %12
12: ; preds = %48, %3
%13 = load i32, i32* %7
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %52
15: ; preds = %12
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 65536
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load i32, i32* %7
br label %21
20: ; preds = %15
br label %21
21: ; preds = %20, %18
%22 = phi i32 [ %19, %18 ], [ 65536, %20 ]
store i32 %22, i32* %9
%23 = load i8*, i8** %8
%24 = load i32, i32* %9
%25 = load i32*, i32** %6
%26 = call i64 @fread(i8* %23, i32 1, i32 %24, i32* %25)
store i64 %26, i64* %10
%27 = load i64, i64* %10
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = icmp ne i64 %27, %29
br i1 %30, label %31, label %37
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = load i64, i64* %10
%34 = load i32, i32* %9
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %33, i32 %34)
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %53
37: ; preds = %21
%38 = load i8*, i8** %8
%39 = load i32, i32* %9
%40 = load i32*, i32** %5
%41 = call i64 @fwrite(i8* %38, i32 1, i32 %39, i32* %40)
store i64 %41, i64* %10
%42 = load i64, i64* %10
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = icmp ne i64 %42, %44
br i1 %45, label %46, label %48
46: ; preds = %37
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %53
48: ; preds = %37
%49 = load i32, i32* %9
%50 = load i32, i32* %7
%51 = sub nsw i32 %50, %49
store i32 %51, i32* %7
br label %12
52: ; preds = %12
store i32 0, i32* %4
br label %53
53: ; preds = %52, %46, %31
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [11 x i8] c"%d vs. %d\0A\00"
@.str.1 = [6 x i8] c"fread\00"
@.str.2 = [7 x i8] c"fwrite\00"
define i32 @write_to_fd(i32* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = tail call dereferenceable_or_null(65536) i8* @malloc(i32 65536)
%5 = icmp sgt i32 %2, 0
br i1 %5, label %8, label %25
6: ; preds = %18
%7 = icmp sgt i32 %21, 0
br i1 %7, label %8, label %25
8: ; preds = %3, %6
%9 = phi i32 [ %21, %6 ], [ %2, %3 ]
%10 = icmp ult i32 %9, 65536
%11 = select i1 %10, i32 %9, i32 65536
%12 = tail call i64 @fread(i8* %4, i32 1, i32 %11, i32* %1)
%13 = zext i32 %11 to i64
%14 = icmp eq i64 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %12, i32 %11)
br label %22
18: ; preds = %8
%19 = tail call i64 @fwrite(i8* %4, i32 1, i32 %11, i32* %0)
%20 = icmp eq i64 %19, %12
%21 = sub nsw i32 %9, %11
br i1 %20, label %6, label %22
22: ; preds = %18, %15
%23 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %15 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %18 ]
%24 = tail call i32 @perror(i8* %23)
br label %25
25: ; preds = %6, %22, %3
%26 = phi i32 [ 0, %3 ], [ -1, %22 ], [ 0, %6 ]
ret i32 %26
}