#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 int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int creat(char const *, int /*<<< orphan*/);
int errno;
scalar_t__ fsync(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
scalar_t__ write(int, char *, int);
int cp_file(const char *src_path, const char *dst_path) {
  char buf[512];
  int src_file, dst_file;
  int ret, bytesread;
  if (-1 == (src_file = open(src_path, O_RDONLY))) {
    printf("can't open file %s\n", src_path);
    return -errno;
  }
  if (0 > (dst_file = creat(dst_path, 0))) {
    ret = -errno;
    printf("can't open file %s\n", dst_path);
    close(src_file);
    return ret;
  }
  lseek(dst_file, 0, SEEK_SET);
  bytesread = 0;
  while ((bytesread = read(src_file, buf, sizeof(buf))) > 0) {
    if (write(dst_file, buf, bytesread) <= 0) {
      printf("WR ERR!\n");
      break;
    }
  }
  ret = 0;
  if (fsync(dst_file)) {
    ret = -errno;
  }
  close(src_file);
  close(dst_file);
  return ret;
}


x86 assembly (clang, -O0):
<cp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x224(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11df <cp_file+0x5f>
mov    -0x10(%rbp),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    0x2e03(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1307 <cp_file+0x187>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   10a0 <creat@plt>
mov    %eax,%ecx
mov    %ecx,-0x228(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    1238 <cp_file+0xb8>
xor    %eax,%eax
mov    0x2dd3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x22c(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x224(%rbp),%edi
call   1060 <close@plt>
mov    -0x22c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1307 <cp_file+0x187>
mov    -0x228(%rbp),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
movl   $0x0,-0x230(%rbp)
mov    -0x224(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x230(%rbp)
cmp    $0x0,%eax
jle    12b8 <cp_file+0x138>
mov    -0x228(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x230(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jg     12b3 <cp_file+0x133>
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b8 <cp_file+0x138>
jmp    1258 <cp_file+0xd8>
movl   $0x0,-0x22c(%rbp)
mov    -0x228(%rbp),%edi
call   1080 <fsync@plt>
cmp    $0x0,%rax
je     12e8 <cp_file+0x168>
xor    %eax,%eax
mov    0x2cf8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x22c(%rbp)
mov    -0x224(%rbp),%edi
call   1060 <close@plt>
mov    -0x228(%rbp),%edi
call   1060 <close@plt>
mov    -0x22c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     124a <cp_file+0xba>
mov    %eax,%r12d
mov    %r15,%rdi
xor    %esi,%esi
call   10b0 <creat@plt>
test   %eax,%eax
js     126a <cp_file+0xda>
mov    %eax,%ebp
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1221 <cp_file+0x91>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
jg     11f0 <cp_file+0x60>
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1090 <fsync@plt>
mov    0x2da9(%rip),%rcx
xor    %r14d,%r14d
sub    (%rcx),%r14d
xor    %ecx,%ecx
test   %rax,%rax
cmove  %ecx,%r14d
mov    %r12d,%edi
call   1070 <close@plt>
mov    %ebp,%edi
jmp    128b <cp_file+0xfb>
lea    0xdaf(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d73(%rip),%rax
sub    (%rax),%r14d
jmp    1290 <cp_file+0x100>
mov    0x2d67(%rip),%rax
xor    %r14d,%r14d
sub    (%rax),%r14d
lea    0xd82(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    %r14d,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"can't open file %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [9 x i8] c"WR ERR!\0A\00"
define i32 @cp_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [512 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = icmp eq i32 -1, %13
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
store i32 %19, i32* %3
br label %63
20: ; preds = %2
%21 = load i8*, i8** %5
%22 = call i32 @creat(i8* %21, i32 0)
store i32 %22, i32* %8
%23 = icmp sgt i32 0, %22
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
store i32 %26, i32* %9
%27 = load i8*, i8** %5
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %9
store i32 %31, i32* %3
br label %63
32: ; preds = %20
%33 = load i32, i32* %8
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @lseek(i32 %33, i32 0, i32 %34)
store i32 0, i32* %10
br label %36
36: ; preds = %49, %32
%37 = load i32, i32* %7
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 512)
store i32 %39, i32* %10
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %36
%42 = load i32, i32* %8
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%44 = load i32, i32* %10
%45 = call i64 @write(i32 %42, i8* %43, i32 %44)
%46 = icmp sle i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %50
49: ; preds = %41
br label %36
50: ; preds = %47, %36
store i32 0, i32* %9
%51 = load i32, i32* %8
%52 = call i64 @fsync(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i32, i32* @errno
%56 = sub nsw i32 0, %55
store i32 %56, i32* %9
br label %57
57: ; preds = %54, %50
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %9
store i32 %62, i32* %3
br label %63
63: ; preds = %57, %24, %15
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"can't open file %s\0A\00"
@errno = global i32 0
@SEEK_SET = global i32 0
@str = [8 x i8] c"WR ERR!\00"
define i32 @cp_file(i8* %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
br label %39
12: ; preds = %2
%13 = tail call i32 @creat(i8* %1, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1)
%19 = tail call i32 @close(i32 %6)
br label %39
20: ; preds = %12
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %13, i32 0, i32 %21)
br label %23
23: ; preds = %26, %20
%24 = call i32 @read(i32 %6, i8* %4, i32 512)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = call i64 @write(i32 %13, i8* %4, i32 %24)
%28 = icmp slt i64 %27, 1
br i1 %28, label %29, label %23
29: ; preds = %26
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %31
31: ; preds = %23, %29
%32 = tail call i64 @fsync(i32 %13)
%33 = icmp eq i64 %32, 0
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
%36 = select i1 %33, i32 0, i32 %35
%37 = tail call i32 @close(i32 %6)
%38 = tail call i32 @close(i32 %13)
br label %39
39: ; preds = %31, %15, %8
%40 = phi i32 [ %11, %8 ], [ %17, %15 ], [ %36, %31 ]
ret i32 %40
}