#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 st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ logger(int, int /*<<< orphan*/, char *, char *);
int open(char *, int, ...);
int read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
scalar_t__ write(int, char *, int);
int cp_file(char *dst, char *src) {
  int fd_src, fd_dst, len, ret = 0;
  struct stat st;
  char buf[4096];
  if (stat(src, &st) < 0) {
    logger(-1, errno, "Unable to stat %s", src);
    return -1;
  }
  len = st.st_size;
  if ((fd_src = open(src, O_RDONLY)) < 0) {
    logger(-1, errno, "Unable to open %s", src);
    return -1;
  }
  if ((fd_dst = open(dst, O_CREAT | O_RDWR, st.st_mode)) < 0) {
    logger(-1, errno, "Unable to open %s", dst);
    close(fd_src);
    return -1;
  }
  while (1) {
    ret = read(fd_src, buf, sizeof(buf));
    if (!ret)
      break;
    else if (ret < 0) {
      logger(-1, errno, "Unable to read from %s", src);
      ret = -1;
      break;
    }
    if (write(fd_dst, buf, ret) < 0) {
      logger(-1, errno, "Unable to write to %s", dst);
      ret = -1;
      break;
    }
  }
  close(fd_src);
  close(fd_dst);
  return ret;
}


x86 assembly (clang, -O0):
<cp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11bb <cp_file+0x5b>
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xe56(%rip),%rdx
call   1070 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <cp_file+0x1ce>
mov    -0x30(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    120b <cp_file+0xab>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xe18(%rip),%rdx
call   1070 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <cp_file+0x1ce>
mov    -0x10(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    0x2dc1(%rip),%rax
or     (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1269 <cp_file+0x109>
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xdc2(%rip),%rdx
call   1070 <logger@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <cp_file+0x1ce>
jmp    126e <cp_file+0x10e>
mov    -0x1c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1294 <cp_file+0x134>
jmp    1318 <cp_file+0x1b8>
cmpl   $0x0,-0x28(%rbp)
jge    12c8 <cp_file+0x168>
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xd6d(%rip),%rdx
call   1070 <logger@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    1318 <cp_file+0x1b8>
jmp    12cd <cp_file+0x16d>
mov    -0x20(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1313 <cp_file+0x1b3>
mov    0x2cd8(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xd39(%rip),%rdx
call   1070 <logger@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    1318 <cp_file+0x1b8>
jmp    126e <cp_file+0x10e>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1060 <stat@plt>
test   %rax,%rax
js     1232 <cp_file+0xd2>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1244 <cp_file+0xe4>
mov    %eax,%r13d
mov    0x2e46(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0xc(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1269 <cp_file+0x109>
mov    %eax,%ebx
lea    0x10(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1291 <cp_file+0x131>
js     1296 <cp_file+0x136>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11e0 <cp_file+0x80>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
lea    0xe1c(%rip),%rdx
mov    $0xffffffff,%r12d
mov    $0xffffffff,%edi
mov    %r14,%rcx
jmp    12b4 <cp_file+0x154>
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
lea    0xdbe(%rip),%rdx
jmp    1254 <cp_file+0xf4>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    0xdbe(%rip),%rdx
mov    $0xffffffff,%r12d
mov    $0xffffffff,%edi
mov    %r15,%rcx
call   1070 <logger@plt>
jmp    12c8 <cp_file+0x168>
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
lea    0xd99(%rip),%rdx
mov    $0xffffffff,%r12d
mov    $0xffffffff,%edi
mov    %r14,%rcx
call   1070 <logger@plt>
mov    %r13d,%edi
jmp    12c3 <cp_file+0x163>
xor    %r12d,%r12d
jmp    12b9 <cp_file+0x159>
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
lea    0xd7e(%rip),%rdx
mov    $0xffffffff,%r12d
mov    $0xffffffff,%edi
mov    %r15,%rcx
call   1070 <logger@plt>
mov    %r13d,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r12d,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@errno = global i32 0
@.str = [18 x i8] c"Unable to stat %s\00"
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"Unable to open %s\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.2 = [23 x i8] c"Unable to read from %s\00"
@.str.3 = [22 x i8] c"Unable to write to %s\00"
define i32 @cp_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca [4096 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %9
%12 = load i8*, i8** %5
%13 = call i64 @stat(i8* %12, %struct.stat* %10)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = load i8*, i8** %5
%18 = call i32 @logger(i32 -1, i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %78
19: ; preds = %2
%20 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%21 = load i32, i32* %20
store i32 %21, i32* %8
%22 = load i8*, i8** %5
%23 = load i32, i32* @O_RDONLY
%24 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i32, i32* @errno
%28 = load i8*, i8** %5
%29 = call i32 @logger(i32 -1, i32 %27, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 -1, i32* %3
br label %78
30: ; preds = %19
%31 = load i8*, i8** %4
%32 = load i32, i32* @O_CREAT
%33 = load i32, i32* @O_RDWR
%34 = or i32 %32, %33
%35 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 (i8*, i32, ...) @open(i8* %31, i32 %34, i32 %36)
store i32 %37, i32* %7
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %30
%40 = load i32, i32* @errno
%41 = load i8*, i8** %4
%42 = call i32 @logger(i32 -1, i32 %40, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
store i32 -1, i32* %3
br label %78
45: ; preds = %30
br label %46
46: ; preds = %45, %71
%47 = load i32, i32* %6
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 4096)
store i32 %49, i32* %9
%50 = load i32, i32* %9
%51 = icmp ne i32 %50, 0
br i1 %51, label %53, label %52
52: ; preds = %46
br label %72
53: ; preds = %46
%54 = load i32, i32* %9
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i32, i32* @errno
%58 = load i8*, i8** %5
%59 = call i32 @logger(i32 -1, i32 %57, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %58)
store i32 -1, i32* %9
br label %72
60: ; preds = %53
br label %61
61: ; preds = %60
%62 = load i32, i32* %7
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%64 = load i32, i32* %9
%65 = call i64 @write(i32 %62, i8* %63, i32 %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %61
%68 = load i32, i32* @errno
%69 = load i8*, i8** %4
%70 = call i32 @logger(i32 -1, i32 %68, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %69)
store i32 -1, i32* %9
br label %72
71: ; preds = %61
br label %46
72: ; preds = %67, %56, %52
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %7
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* %9
store i32 %77, i32* %3
br label %78
78: ; preds = %72, %39, %26, %15
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@errno = global i32 0
@.str = [18 x i8] c"Unable to stat %s\00"
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"Unable to open %s\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.2 = [23 x i8] c"Unable to read from %s\00"
@.str.3 = [22 x i8] c"Unable to write to %s\00"
define i32 @cp_file(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [4096 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = call i64 @stat(i8* %1, %struct.stat* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = call i32 @logger(i32 -1, i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %49
12: ; preds = %2
%13 = load i32, i32* @O_RDONLY
%14 = call i32 (i8*, i32, ...) @open(i8* %1, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = call i32 @logger(i32 -1, i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %49
19: ; preds = %12
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_RDWR
%22 = or i32 %21, %20
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 (i8*, i32, ...) @open(i8* %0, i32 %22, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = load i32, i32* @errno
%29 = call i32 @logger(i32 -1, i32 %28, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%30 = call i32 @close(i32 %14)
br label %49
31: ; preds = %19, %39
%32 = call i32 @read(i32 %14, i8* %6, i32 4096)
%33 = icmp eq i32 %32, 0
br i1 %33, label %45, label %34
34: ; preds = %31
%35 = icmp slt i32 %32, 0
br i1 %35, label %36, label %39
36: ; preds = %34
%37 = load i32, i32* @errno
%38 = call i32 @logger(i32 -1, i32 %37, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %45
39: ; preds = %34
%40 = call i64 @write(i32 %25, i8* %6, i32 %32)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %31
42: ; preds = %39
%43 = load i32, i32* @errno
%44 = call i32 @logger(i32 -1, i32 %43, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %45
45: ; preds = %31, %42, %36
%46 = phi i32 [ -1, %36 ], [ -1, %42 ], [ 0, %31 ]
%47 = call i32 @close(i32 %14)
%48 = call i32 @close(i32 %25)
br label %49
49: ; preds = %45, %27, %16, %9
%50 = phi i32 [ -1, %9 ], [ -1, %16 ], [ -1, %27 ], [ %46, %45 ]
ret i32 %50
}