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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fsync(int);
int open(char *, int, ...);
scalar_t__ read(int, char *, int);
scalar_t__ write(int, char *, scalar_t__);
int cp(char *src, char *dst, mode_t mode) {
  int sfd, dfd;
  char buf[8192];
  ssize_t size;
  if ((sfd = open(src, O_RDONLY)) < 0) {
    return -1;
  }
  if ((dfd = open(dst, O_CREAT | O_TRUNC | O_RDWR, mode)) < 0) {
    return -1;
  }
  while ((size = read(sfd, buf, 8192)) > 0) {
    if (write(dfd, buf, size) < size)
      return -1;
  }
  fsync(dfd);
  close(sfd);
  close(dfd);
  return 0;
}


x86 assembly (clang, -O0):
<cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1192 <cp+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <cp+0x100>
mov    -0x18(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11d3 <cp+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <cp+0x100>
jmp    11d8 <cp+0x88>
mov    -0x20(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0x2000,%edx
call   1050 <read@plt>
mov    %rax,-0x2038(%rbp)
cmp    $0x0,%rax
jle    1231 <cp+0xe1>
mov    -0x24(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    -0x2038(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x2038(%rbp),%rax
jge    122c <cp+0xdc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <cp+0x100>
jmp    11d8 <cp+0x88>
mov    -0x24(%rbp),%edi
call   1060 <fsync@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1208 <cp+0xb8>
mov    %eax,%r12d
mov    0x2e6b(%rip),%rax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1208 <cp+0xb8>
mov    %eax,%r15d
mov    %rsp,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x2000,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11ed <cp+0x9d>
mov    %rax,%rbx
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbx,%rax
jge    11c0 <cp+0x70>
jmp    1208 <cp+0xb8>
mov    %r15d,%edi
call   1060 <fsync@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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