#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*/ free(unsigned char *);
long read_file(char const *, unsigned char **);
long write_file(char const *, unsigned char *, long);
long copy_file(const char *src_name, const char *dst_name) {
  unsigned char *buf;
  long bytes_written;
  long bytes_read;
  bytes_read = read_file(src_name, &buf);
  if (bytes_read > 0) {
    bytes_written = write_file(dst_name, buf, bytes_read);
    if (bytes_written != bytes_read) {
      bytes_read = -1;
    }
    free(buf);
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <read_file@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    1190 <copy_file+0x60>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <write_file@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1187 <copy_file+0x57>
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rsp,%rsi
call   1050 <read_file@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    116d <copy_file+0x3d>
mov    (%rsp),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <write_file@plt>
cmp    %rbx,%rax
mov    $0xffffffffffffffff,%rax
cmovne %rax,%rbx
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @copy_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i64 @read_file(i8* %8, i8** %5)
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8*, i8** %5
%15 = load i64, i64* %7
%16 = call i64 @write_file(i8* %13, i8* %14, i64 %15)
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = load i64, i64* %7
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %12
store i64 -1, i64* %7
br label %21
21: ; preds = %20, %12
%22 = load i8*, i8** %5
%23 = call i32 @free(i8* %22)
br label %24
24: ; preds = %21, %2
%25 = load i64, i64* %7
ret i64 %25
}

LLVM IR (-O2):
define i64 @copy_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i64 @read_file(i8* %0, i8** %3)
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = call i64 @write_file(i8* %1, i8* %8, i64 %5)
%10 = icmp eq i64 %9, %5
%11 = select i1 %10, i64 %5, i64 -1
%12 = load i8*, i8** %3
%13 = call i32 @free(i8* %12)
br label %14
14: ; preds = %7, %2
%15 = phi i64 [ %11, %7 ], [ %5, %2 ]
ret i64 %15
}