#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ _fsop_close_safe(int);
int fsop_fsend(int, char const *, size_t);
int /*<<< orphan*/ fsop_unlink(char const *);
int open(char const *, int, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
ssize_t fsop_cp(const char *src, const char *dest, size_t block) {
  int dfd = 0;
  ssize_t count = 0;
  struct stat st;
  if (stat(src, &st) < 0)
    return -1;
  fsop_unlink(dest);
  if ((dfd = open(dest, O_WRONLY | O_CREAT, st.st_mode)) < 0)
    return -1;
  count = fsop_fsend(dfd, src, block);
  _fsop_close_safe(dfd);
  return count;
}


x86 assembly (clang, -O0):
<fsop_cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    1195 <fsop_cp+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <fsop_cp+0xa5>
mov    -0x18(%rbp),%rdi
call   1030 <fsop_unlink@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
mov    -0x30(%rbp),%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11d4 <fsop_cp+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <fsop_cp+0xa5>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <fsop_fsend@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <_fsop_close_safe@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsop_cp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsp,%rsi
call   1040 <stat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     11b3 <fsop_cp+0x63>
mov    %rbx,%rdi
call   1030 <fsop_unlink@plt>
mov    0x2e4f(%rip),%rax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    (%rsp),%edx
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11b3 <fsop_cp+0x63>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <fsop_fsend@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <_fsop_close_safe@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @fsop_cp(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
store i32 0, i32* %9
%11 = load i8*, i8** %5
%12 = call i64 @stat(i8* %11, %struct.stat* %10)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %35
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i32 @fsop_unlink(i8* %16)
%18 = load i8*, i8** %6
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @open(i8* %18, i32 %21, i32 %23)
store i32 %24, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %15
store i32 -1, i32* %4
br label %35
27: ; preds = %15
%28 = load i32, i32* %8
%29 = load i8*, i8** %5
%30 = load i64, i64* %7
%31 = call i32 @fsop_fsend(i32 %28, i8* %29, i64 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %8
%33 = call i32 @_fsop_close_safe(i32 %32)
%34 = load i32, i32* %9
store i32 %34, i32* %4
br label %35
35: ; preds = %27, %26, %14
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @fsop_cp(i8* %0, i8* %1, i64 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %20, label %8
8: ; preds = %3
%9 = call i32 @fsop_unlink(i8* %1)
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @open(i8* %1, i32 %12, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %8
%18 = call i32 @fsop_fsend(i32 %15, i8* %0, i64 %2)
%19 = call i32 @_fsop_close_safe(i32 %15)
br label %20
20: ; preds = %8, %3, %17
%21 = phi i32 [ %18, %17 ], [ -1, %3 ], [ -1, %8 ]
ret i32 %21
}