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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int, ...);
scalar_t__ read(int, char *, int);
scalar_t__ write(int, char *, scalar_t__);
int copy_file(const char *src, const char *dest, mode_t mode) {
  char buf[32768];
  int sfd, dfd;
  ssize_t len;
  sfd = open(src, O_RDONLY);
  if (sfd < 0)
    return -1;
  dfd = open(dest, O_WRONLY | O_CREAT, mode);
  if (dfd < 0) {
    close(sfd);
    return -1;
  }
  while ((len = read(sfd, buf, sizeof(buf))) > 0) {
    len = write(dfd, buf, len);
    if (len < 0) {
      close(sfd);
      close(dfd);
      return -1;
    }
  }
  close(sfd);
  close(dfd);
  return 0;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8024(%rbp)
cmpl   $0x0,-0x8024(%rbp)
jge    1189 <copy_file+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <copy_file+0x132>
mov    -0x18(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8028(%rbp)
cmpl   $0x0,-0x8028(%rbp)
jge    11d3 <copy_file+0x93>
mov    -0x8024(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <copy_file+0x132>
jmp    11d8 <copy_file+0x98>
mov    -0x8024(%rbp),%edi
lea    -0x8020(%rbp),%rsi
mov    $0x8000,%edx
call   1050 <read@plt>
mov    %rax,-0x8030(%rbp)
cmp    $0x0,%rax
jle    1255 <copy_file+0x115>
mov    -0x8028(%rbp),%edi
lea    -0x8020(%rbp),%rsi
mov    -0x8030(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x8030(%rbp)
cmpq   $0x0,-0x8030(%rbp)
jge    1250 <copy_file+0x110>
mov    -0x8024(%rbp),%edi
call   1040 <close@plt>
mov    -0x8028(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <copy_file+0x132>
jmp    11d8 <copy_file+0x98>
mov    -0x8024(%rbp),%edi
call   1040 <close@plt>
mov    -0x8028(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x8030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x8008,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11cf <copy_file+0x8f>
mov    %eax,%r15d
mov    0x2e57(%rip),%rax
mov    0x2e80(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11d7 <copy_file+0x97>
mov    %eax,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x8000,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11e2 <copy_file+0xa2>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    11a0 <copy_file+0x60>
mov    $0xffffffff,%r14d
jmp    11e5 <copy_file+0xa5>
mov    $0xffffffff,%r14d
jmp    11f4 <copy_file+0xb4>
mov    $0xffffffff,%r14d
mov    %r15d,%ebp
jmp    11ed <copy_file+0xad>
xor    %r14d,%r14d
mov    %r15d,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x8008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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