#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;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
scalar_t__ read(int, char **, int);
scalar_t__ write(int, char **, scalar_t__);
int file_copy(char *src_file, char *dest_file) {
  int source_fd = open(src_file, O_RDONLY);
  if (source_fd != -1) {
    int destFd =
        open(dest_file, O_WRONLY | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
    if (destFd == -1) {
      return -1;
    }
    ssize_t ret_in;
    ssize_t ret_out;
    char buffer[4096];
    while ((ret_in = read(source_fd, &buffer, 4096)) > 0) {
      ret_out = write(destFd, &buffer, (ssize_t)ret_in);
      if (ret_out != ret_in) {
        return -1;
      }
    }
    close(source_fd);
    close(destFd);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     123e <file_copy+0xfe>
mov    -0x18(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
mov    0x2e1d(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c5 <file_copy+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <file_copy+0x105>
jmp    11ca <file_copy+0x8a>
mov    -0x1c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jle    1222 <file_copy+0xe2>
mov    -0x20(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     121d <file_copy+0xdd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <file_copy+0x105>
jmp    11ca <file_copy+0x8a>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <file_copy+0x105>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_copy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%rbp
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1206 <file_copy+0xc6>
mov    %eax,%r15d
mov    0x2e35(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
mov    0x2e3a(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1206 <file_copy+0xc6>
mov    %eax,%r12d
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11f3 <file_copy+0xb3>
mov    %rax,%rbp
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
je     11c0 <file_copy+0x80>
mov    $0xffffffff,%r14d
jmp    1206 <file_copy+0xc6>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r12d,%edi
call   1040 <close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
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
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
define i32 @file_copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca [4096 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %51
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IWUSR
%24 = load i32, i32* @S_IRUSR
%25 = or i32 %23, %24
%26 = call i32 (i8*, i32, ...) @open(i8* %17, i32 %22, i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %16
store i32 -1, i32* %3
br label %52
30: ; preds = %16
br label %31
31: ; preds = %45, %30
%32 = load i32, i32* %6
%33 = bitcast [4096 x i8]* %10 to i8**
%34 = call i64 @read(i32 %32, i8** %33, i32 4096)
store i64 %34, i64* %8
%35 = icmp sgt i64 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %31
%37 = load i32, i32* %7
%38 = bitcast [4096 x i8]* %10 to i8**
%39 = load i64, i64* %8
%40 = call i64 @write(i32 %37, i8** %38, i64 %39)
store i64 %40, i64* %9
%41 = load i64, i64* %9
%42 = load i64, i64* %8
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %36
store i32 -1, i32* %3
br label %52
45: ; preds = %36
br label %31
46: ; preds = %31
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
store i32 0, i32* %3
br label %52
51: ; preds = %2
store i32 -1, i32* %3
br label %52
52: ; preds = %51, %46, %44, %29
%53 = load i32, i32* %3
ret i32 %53
}

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