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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, size_t);
int /*<<< orphan*/ write_buff_out(char *, int, int /*<<< orphan*/ *);
void copy_file(FILE *infile, FILE *outfile, char *buf, size_t size) {
  ssize_t i;
  while ((i = read(fileno(infile), buf, size)) > 0) {
    write_buff_out(buf, i, outfile);
  }
  if (i == -1) {
    perror("read file");
    fclose(infile);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    11b1 <copy_file+0x51>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1040 <write_buff_out@plt>
jmp    1178 <copy_file+0x18>
cmpl   $0xffffffff,-0x24(%rbp)
jne    11da <copy_file+0x7a>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <read@plt>
test   %eax,%eax
jle    11a8 <copy_file+0x48>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r12,%rdx
call   1040 <write_buff_out@plt>
mov    %r14,%rdi
jmp    1180 <copy_file+0x20>
cmp    $0xffffffff,%eax
je     11b9 <copy_file+0x59>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"read file\00"
define void @copy_file(i32* %0, i32* %1, i8* %2, i64 %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
br label %10
10: ; preds = %17, %4
%11 = load i32*, i32** %5
%12 = call i32 @fileno(i32* %11)
%13 = load i8*, i8** %7
%14 = load i64, i64* %8
%15 = call i32 @read(i32 %12, i8* %13, i64 %14)
store i32 %15, i32* %9
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %10
%18 = load i8*, i8** %7
%19 = load i32, i32* %9
%20 = load i32*, i32** %6
%21 = call i32 @write_buff_out(i8* %18, i32 %19, i32* %20)
br label %10
22: ; preds = %10
%23 = load i32, i32* %9
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"read file\00"
define void @copy_file(i32* nocapture %0, i32* %1, i8* %2, i64 %3) {
%5 = tail call i32 @fileno(i32* %0)
%6 = tail call i32 @read(i32 %5, i8* %2, i64 %3)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %4, %8
%9 = phi i32 [ %12, %8 ], [ %6, %4 ]
%10 = tail call i32 @write_buff_out(i8* %2, i32 %9, i32* %1)
%11 = tail call i32 @fileno(i32* %0)
%12 = tail call i32 @read(i32 %11, i8* %2, i64 %3)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %8, label %14
14: ; preds = %8, %4
%15 = phi i32 [ %6, %4 ], [ %12, %8 ]
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @fclose(i32* %0)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14
ret void
}