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

/* Variables and functions */
int /*<<< orphan*/ get_file_size(int);
char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int /*<<< orphan*/);
int write(int, char *, int /*<<< orphan*/);
copy(int in, int out) {
  char *buff = malloc(get_file_size(in));
  if (read(in, buff, get_file_size(in)) == -1) {
    perror("read");
    return 0;
  }
  if (write(out, buff, get_file_size(in)) == -1) {
    perror("write");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <get_file_size@plt>
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <get_file_size@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11b6 <copy+0x66>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1202 <copy+0xb2>
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <get_file_size@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11fb <copy+0xab>
lea    0xe1b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1202 <copy+0xb2>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
call   1060 <get_file_size@plt>
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <get_file_size@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11a5 <copy+0x55>
mov    %ebp,%edi
call   1060 <get_file_size@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    $0xffffffff,%ecx
je     11ae <copy+0x5e>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe54(%rip),%rdi
jmp    11b5 <copy+0x65>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
jmp    11a0 <copy+0x50>

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

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @copy(i32 %0, i32 %1) {
%3 = tail call i32 @get_file_size(i32 %0)
%4 = tail call i8* @malloc(i32 %3)
%5 = tail call i32 @get_file_size(i32 %0)
%6 = tail call i32 @read(i32 %0, i8* %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = tail call i32 @get_file_size(i32 %0)
%10 = tail call i32 @write(i32 %1, i8* %4, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8, %2
%13 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %8 ]
%14 = tail call i32 @perror(i8* %13)
br label %15
15: ; preds = %12, %8
%16 = phi i32 [ 1, %8 ], [ 0, %12 ]
ret i32 %16
}