#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 size_t ssize_t;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
size_t EIO;
size_t fread(char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
ssize_t fcopy(FILE *source, FILE *dest) {
  size_t bytes_read;
  ssize_t total_bytes_read = 0;
  char buffer[BUFSIZ];
  while ((bytes_read = fread(buffer, sizeof(char), sizeof(buffer), source)) >
         0) {
    if (fwrite(buffer, sizeof(char), bytes_read, dest) != bytes_read) {
      return -EIO;
    }
    total_bytes_read += bytes_read;
  }
  return total_bytes_read;
}


x86 assembly (clang, -O0):
<fcopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jbe    11db <fcopy+0xbb>
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
cmp    -0x20(%rbp),%rax
je     11ca <fcopy+0xaa>
xor    %eax,%eax
mov    0x2e29(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    11ea <fcopy+0xca>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1168 <fcopy+0x48>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcopy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r15
mov    0x2e9c(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r13
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11a9 <fcopy+0x89>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    -0x30(%rbp),%rcx
call   1040 <fwrite@plt>
cmp    %r14,%rax
jne    11ad <fcopy+0x8d>
add    %r14,%rbx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1170 <fcopy+0x50>
jmp    11b9 <fcopy+0x99>
xor    %ebx,%ebx
jmp    11b9 <fcopy+0x99>
mov    0x2e2c(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%rbx
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@EIO = global i64 0
define i64 @fcopy(i32* %0, i32* %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 0, i64* %7
%11 = load i32, i32* @BUFSIZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
br label %15
15: ; preds = %29, %2
%16 = trunc i64 %12 to i32
%17 = load i32*, i32** %4
%18 = call i64 @fread(i8* %14, i32 1, i32 %16, i32* %17)
store i64 %18, i64* %6
%19 = icmp ugt i64 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %15
%21 = load i64, i64* %6
%22 = load i32*, i32** %5
%23 = call i64 @fwrite(i8* %14, i32 1, i64 %21, i32* %22)
%24 = load i64, i64* %6
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i64, i64* @EIO
%28 = sub i64 0, %27
store i64 %28, i64* %3
store i32 1, i32* %10
br label %35
29: ; preds = %20
%30 = load i64, i64* %6
%31 = load i64, i64* %7
%32 = add i64 %31, %30
store i64 %32, i64* %7
br label %15
33: ; preds = %15
%34 = load i64, i64* %7
store i64 %34, i64* %3
store i32 1, i32* %10
br label %35
35: ; preds = %33, %26
%36 = load i8*, i8** %8
%37 = load i64, i64* %3
ret i64 %37
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@EIO = global i64 0
define i64 @fcopy(i32* nocapture %0, i32* nocapture %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i64 @fread(i8* %5, i32 1, i32 %3, i32* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %20, label %11
8: ; preds = %11
%9 = call i64 @fread(i8* %5, i32 1, i32 %3, i32* %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %2, %8
%12 = phi i64 [ %9, %8 ], [ %6, %2 ]
%13 = phi i64 [ %16, %8 ], [ 0, %2 ]
%14 = call i64 @fwrite(i8* %5, i32 1, i64 %12, i32* %1)
%15 = icmp eq i64 %14, %12
%16 = add i64 %12, %13
br i1 %15, label %8, label %17
17: ; preds = %11
%18 = load i64, i64* @EIO
%19 = sub i64 0, %18
br label %20
20: ; preds = %8, %2, %17
%21 = phi i64 [ %19, %17 ], [ 0, %2 ], [ %16, %8 ]
ret i64 %21
}