#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 O_WRONLY;
int /*<<< orphan*/ close(int);
char *mapfile(char const *, int *);
int /*<<< orphan*/ munmap(char *, int);
int open(char const *, int, int);
int write(int, char *, int);
int f_cp(const char *src, const char *dst, int mode) {
  char *p;
  int fd, size, ret = -1;
  if ((p = mapfile(src, &size)) == NULL)
    return -1;
  if ((fd = open(dst, O_WRONLY | O_CREAT | mode, 0750)) != -1) {
    if (write(fd, p, size) == size)
      ret = 0;
    close(fd);
  }
  munmap(p, size);
  return ret;
}


x86 assembly (clang, -O0):
<f_cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0xffffffff,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <mapfile@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1191 <f_cp+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <f_cp+0xa9>
mov    -0x18(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
or     -0x1c(%rbp),%esi
mov    $0x1e8,%edx
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     11e7 <f_cp+0x97>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <write@plt>
cmp    -0x30(%rbp),%eax
jne    11df <f_cp+0x8f>
movl   $0x0,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1050 <munmap@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f_cp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
lea    0x4(%rsp),%rsi
call   1070 <mapfile@plt>
test   %rax,%rax
je     11b9 <f_cp+0x69>
mov    %rax,%r15
mov    0x2e5b(%rip),%rax
mov    0x2e7c(%rip),%rcx
or     (%rax),%ebp
or     (%rcx),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1e8,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11c0 <f_cp+0x70>
mov    %eax,%ebx
mov    0x4(%rsp),%edx
mov    %eax,%edi
mov    %r15,%rsi
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    0x4(%rsp),%eax
setne  %bpl
neg    %ebp
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11c5 <f_cp+0x75>
mov    $0xffffffff,%ebp
jmp    11d1 <f_cp+0x81>
mov    $0xffffffff,%ebp
mov    0x4(%rsp),%esi
mov    %r15,%rdi
call   1050 <munmap@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @f_cp(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 -1, i32* %11
%12 = load i8*, i8** %5
%13 = call i8* @mapfile(i8* %12, i32* %10)
store i8* %13, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %41
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* %7
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %17, i32 %22, i32 488)
store i32 %23, i32* %9
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %36
25: ; preds = %16
%26 = load i32, i32* %9
%27 = load i8*, i8** %8
%28 = load i32, i32* %10
%29 = call i32 @write(i32 %26, i8* %27, i32 %28)
%30 = load i32, i32* %10
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 0, i32* %11
br label %33
33: ; preds = %32, %25
%34 = load i32, i32* %9
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %16
%37 = load i8*, i8** %8
%38 = load i32, i32* %10
%39 = call i32 @munmap(i8* %37, i32 %38)
%40 = load i32, i32* %11
store i32 %40, i32* %4
br label %41
41: ; preds = %36, %15
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @f_cp(i8* %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i8* @mapfile(i8* %0, i32* %4)
%7 = icmp eq i8* %6, null
br i1 %7, label %26, label %8
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 = call i32 @open(i8* %1, i32 %12, i32 488)
%14 = icmp eq i32 %13, -1
br i1 %14, label %22, label %15
15: ; preds = %8
%16 = load i32, i32* %4
%17 = call i32 @write(i32 %13, i8* %6, i32 %16)
%18 = load i32, i32* %4
%19 = icmp ne i32 %17, %18
%20 = sext i1 %19 to i32
%21 = call i32 @close(i32 %13)
br label %22
22: ; preds = %15, %8
%23 = phi i32 [ %20, %15 ], [ -1, %8 ]
%24 = load i32, i32* %4
%25 = call i32 @munmap(i8* %6, i32 %24)
br label %26
26: ; preds = %3, %22
%27 = phi i32 [ %23, %22 ], [ -1, %3 ]
ret i32 %27
}