#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_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int read(int, char *, int);
scalar_t__ write(int, char *, int);
int f_cp(char *src, char *dst, int mode) {
  int fsrc, fdst, ret;
  ret = 0;
  if ((fsrc = open(src, O_RDONLY)) >= 0) {
    ret = -1;
    if ((fdst = open(dst, O_WRONLY | O_CREAT | mode, 0600)) >= 0) {
      char pool[4096];
      src = pool;
      do {
        ret = read(fsrc, src, 4096);
        if (ret <= 0)
          break;
      } while (write(fdst, src, ret) > 0);
      close(fdst);
    }
    close(fsrc);
  }
  return ret;
}


x86 assembly (clang, -O0):
<f_cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jl     1211 <f_cp+0xd1>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
or     -0x14(%rbp),%esi
mov    $0x180,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     1209 <f_cp+0xc9>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11e3 <f_cp+0xa3>
jmp    1201 <f_cp+0xc1>
jmp    11e8 <f_cp+0xa8>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jg     11c0 <f_cp+0x80>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f_cp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %edx,%ebx
mov    %rsi,%r15
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11dd <f_cp+0x9d>
mov    %eax,%r14d
mov    0x2e56(%rip),%rax
mov    0x2e7f(%rip),%rcx
or     (%rax),%ebx
or     (%rcx),%ebx
mov    %r15,%rdi
mov    %ebx,%esi
mov    $0x180,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     11d0 <f_cp+0x90>
mov    %eax,%ebx
mov    %rsp,%r15
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11c7 <f_cp+0x87>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %rax,%rax
jg     11a0 <f_cp+0x60>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11d5 <f_cp+0x95>
mov    $0xffffffff,%ebp
mov    %r14d,%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_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @f_cp(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4096 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %9
%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* %7
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %46
15: ; preds = %3
store i32 -1, i32* %9
%16 = load i8*, i8** %5
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = load i32, i32* %6
%21 = or i32 %19, %20
%22 = call i32 (i8*, i32, ...) @open(i8* %16, i32 %21, i32 384)
store i32 %22, i32* %8
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %43
24: ; preds = %15
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
store i8* %25, i8** %4
br label %26
26: ; preds = %34, %24
%27 = load i32, i32* %7
%28 = load i8*, i8** %4
%29 = call i32 @read(i32 %27, i8* %28, i32 4096)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %40
33: ; preds = %26
br label %34
34: ; preds = %33
%35 = load i32, i32* %8
%36 = load i8*, i8** %4
%37 = load i32, i32* %9
%38 = call i64 @write(i32 %35, i8* %36, i32 %37)
%39 = icmp sgt i64 %38, 0
br i1 %39, label %26, label %40
40: ; preds = %34, %32
%41 = load i32, i32* %8
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40, %15
%44 = load i32, i32* %7
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %43, %3
%47 = load i32, i32* %9
ret i32 %47
}

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