#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 O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int open(char const *, int, ...);
int read(int, char *, int);
int write(int, char *, int);
int copy_file(const char *src, const char *dst) {
  char buf[4096];
  int dstfd, srcfd, bytes_read, bytes_written, total_read = 0;
  if (src == NULL)
    return 1;
  if (dst == NULL)
    return 2;
  srcfd = open(src, O_RDONLY);
  if (srcfd < 0)
    return 3;
  dstfd = open(dst, O_RDWR | O_CREAT | O_TRUNC, 0666);
  if (dstfd < 0) {
    close(srcfd);
    return 4;
  }
  while (bytes_read = read(srcfd, buf, 4096)) {
    total_read += bytes_read;
    if (write(dstfd, buf, bytes_read) != bytes_read)
      return 5;
  }
  close(dstfd);
  close(srcfd);
  return 0;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1034(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <copy_file+0x34>
movl   $0x1,-0x4(%rbp)
jmp    12a2 <copy_file+0x162>
cmpq   $0x0,-0x18(%rbp)
jne    118b <copy_file+0x4b>
movl   $0x2,-0x4(%rbp)
jmp    12a2 <copy_file+0x162>
mov    -0x10(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1028(%rbp)
cmpl   $0x0,-0x1028(%rbp)
jge    11be <copy_file+0x7e>
movl   $0x3,-0x4(%rbp)
jmp    12a2 <copy_file+0x162>
mov    -0x18(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jge    1213 <copy_file+0xd3>
mov    -0x1028(%rbp),%edi
call   1040 <close@plt>
movl   $0x4,-0x4(%rbp)
jmp    12a2 <copy_file+0x162>
jmp    1218 <copy_file+0xd8>
mov    -0x1028(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x102c(%rbp)
cmp    $0x0,%eax
je     1285 <copy_file+0x145>
mov    -0x102c(%rbp),%eax
add    -0x1034(%rbp),%eax
mov    %eax,-0x1034(%rbp)
mov    -0x1024(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x102c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x102c(%rbp),%eax
je     1280 <copy_file+0x140>
movl   $0x5,-0x4(%rbp)
jmp    12a2 <copy_file+0x162>
jmp    1218 <copy_file+0xd8>
mov    -0x1024(%rbp),%edi
call   1040 <close@plt>
mov    -0x1028(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
test   %rdi,%rdi
je     11dd <copy_file+0x9d>
mov    %rsi,%r14
test   %rsi,%rsi
je     11e4 <copy_file+0xa4>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11eb <copy_file+0xab>
mov    %eax,%r15d
mov    0x2e58(%rip),%rax
mov    0x2e71(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11f2 <copy_file+0xb2>
mov    %eax,%ebp
mov    %rsp,%r14
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11f9 <copy_file+0xb9>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     11b0 <copy_file+0x70>
mov    $0x5,%ebp
jmp    120a <copy_file+0xca>
mov    $0x1,%ebp
jmp    120a <copy_file+0xca>
mov    $0x2,%ebp
jmp    120a <copy_file+0xca>
mov    $0x3,%ebp
jmp    120a <copy_file+0xca>
mov    $0x4,%ebp
jmp    1202 <copy_file+0xc2>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
mov    %r15d,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @copy_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %11
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 1, i32* %3
br label %62
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 2, i32* %3
br label %62
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_RDONLY
%22 = call i32 (i8*, i32, ...) @open(i8* %20, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 3, i32* %3
br label %62
26: ; preds = %19
%27 = load i8*, i8** %5
%28 = load i32, i32* @O_RDWR
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = call i32 (i8*, i32, ...) @open(i8* %27, i32 %32, i32 438)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
store i32 4, i32* %3
br label %62
39: ; preds = %26
br label %40
40: ; preds = %56, %39
%41 = load i32, i32* %8
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 4096)
store i32 %43, i32* %9
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %57
45: ; preds = %40
%46 = load i32, i32* %9
%47 = load i32, i32* %11
%48 = add nsw i32 %47, %46
store i32 %48, i32* %11
%49 = load i32, i32* %7
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%51 = load i32, i32* %9
%52 = call i32 @write(i32 %49, i8* %50, i32 %51)
%53 = load i32, i32* %9
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %45
store i32 5, i32* %3
br label %62
56: ; preds = %45
br label %40
57: ; preds = %40
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
store i32 0, i32* %3
br label %62
62: ; preds = %57, %55, %36, %25, %18, %14
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @copy_file(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8* %0, null
br i1 %5, label %31, label %6
6: ; preds = %2
%7 = icmp eq i8* %1, null
br i1 %7, label %31, label %8
8: ; preds = %6
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %31, label %12
12: ; preds = %8
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %17, i32 438)
%19 = icmp slt i32 %18, 0
br i1 %19, label %28, label %20
20: ; preds = %12, %23
%21 = call i32 @read(i32 %10, i8* %4, i32 4096)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = call i32 @write(i32 %18, i8* %4, i32 %21)
%25 = icmp eq i32 %24, %21
br i1 %25, label %20, label %31
26: ; preds = %20
%27 = tail call i32 @close(i32 %18)
br label %28
28: ; preds = %12, %26
%29 = phi i32 [ 0, %26 ], [ 4, %12 ]
%30 = tail call i32 @close(i32 %10)
br label %31
31: ; preds = %23, %28, %8, %6, %2
%32 = phi i32 [ 1, %2 ], [ 2, %6 ], [ 3, %8 ], [ %29, %28 ], [ 5, %23 ]
ret i32 %32
}