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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
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, ret = 0;
  struct stat st;
  char buf[4096];
  if (stat(src, &st) < 0) {
    logger(-1, errno, "Unable to stat %s", src);
    return -1;
  }
  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_TRUNC | 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,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11bb <cp_file+0x5b>
mov    0x2e28(%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    1331 <cp_file+0x1d1>
mov    -0x18(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1205 <cp_file+0xa5>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xe1e(%rip),%rdx
call   1070 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <cp_file+0x1d1>
mov    -0x10(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
mov    0x2db6(%rip),%rax
or     (%rax),%esi
mov    -0x28(%rbp),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    126c <cp_file+0x10c>
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xdbf(%rip),%rdx
call   1070 <logger@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <cp_file+0x1d1>
jmp    1271 <cp_file+0x111>
mov    -0x1c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1297 <cp_file+0x137>
jmp    131b <cp_file+0x1bb>
cmpl   $0x0,-0x24(%rbp)
jge    12cb <cp_file+0x16b>
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xd6a(%rip),%rdx
call   1070 <logger@plt>
movl   $0xffffffff,-0x24(%rbp)
jmp    131b <cp_file+0x1bb>
jmp    12d0 <cp_file+0x170>
mov    -0x20(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1316 <cp_file+0x1b6>
mov    0x2ccd(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xd36(%rip),%rdx
call   1070 <logger@plt>
movl   $0xffffffff,-0x24(%rbp)
jmp    131b <cp_file+0x1bb>
jmp    1271 <cp_file+0x111>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%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     1242 <cp_file+0xe2>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1254 <cp_file+0xf4>
mov    %eax,%r13d
mov    0x2e46(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
mov    0x8(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1279 <cp_file+0x119>
mov    %eax,%ebx
lea    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
je     12a1 <cp_file+0x141>
js     12a6 <cp_file+0x146>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11f0 <cp_file+0x90>
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    0xe0c(%rip),%rdx
mov    $0xffffffff,%r12d
mov    $0xffffffff,%edi
mov    %r14,%rcx
jmp    12c4 <cp_file+0x164>
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
lea    0xdae(%rip),%rdx
jmp    1264 <cp_file+0x104>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    0xdae(%rip),%rdx
mov    $0xffffffff,%r12d
mov    $0xffffffff,%edi
mov    %r15,%rcx
call   1070 <logger@plt>
jmp    12d8 <cp_file+0x178>
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
lea    0xd89(%rip),%rdx
mov    $0xffffffff,%r12d
mov    $0xffffffff,%edi
mov    %r14,%rcx
call   1070 <logger@plt>
mov    %r13d,%edi
jmp    12d3 <cp_file+0x173>
xor    %r12d,%r12d
jmp    12c9 <cp_file+0x169>
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
lea    0xd6e(%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 }
@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_TRUNC = 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 %struct.stat
%10 = alloca [4096 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %8
%11 = load i8*, i8** %5
%12 = call i64 @stat(i8* %11, %struct.stat* %9)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = load i8*, i8** %5
%17 = call i32 @logger(i32 -1, i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %77
18: ; preds = %2
%19 = load i8*, i8** %5
%20 = load i32, i32* @O_RDONLY
%21 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = load i8*, i8** %5
%26 = call i32 @logger(i32 -1, i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 -1, i32* %3
br label %77
27: ; preds = %18
%28 = load i8*, i8** %4
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = load i32, i32* @O_RDWR
%33 = or i32 %31, %32
%34 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 (i8*, i32, ...) @open(i8* %28, i32 %33, i32 %35)
store i32 %36, i32* %7
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %27
%39 = load i32, i32* @errno
%40 = load i8*, i8** %4
%41 = call i32 @logger(i32 -1, i32 %39, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %3
br label %77
44: ; preds = %27
br label %45
45: ; preds = %44, %70
%46 = load i32, i32* %6
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 4096)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %52, label %51
51: ; preds = %45
br label %71
52: ; preds = %45
%53 = load i32, i32* %8
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load i32, i32* @errno
%57 = load i8*, i8** %5
%58 = call i32 @logger(i32 -1, i32 %56, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %57)
store i32 -1, i32* %8
br label %71
59: ; preds = %52
br label %60
60: ; preds = %59
%61 = load i32, i32* %7
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%63 = load i32, i32* %8
%64 = call i64 @write(i32 %61, i8* %62, i32 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %60
%67 = load i32, i32* @errno
%68 = load i8*, i8** %4
%69 = call i32 @logger(i32 -1, i32 %67, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %68)
store i32 -1, i32* %8
br label %71
70: ; preds = %60
br label %45
71: ; preds = %66, %55, %51
%72 = load i32, i32* %6
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* %7
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %8
store i32 %76, i32* %3
br label %77
77: ; preds = %71, %38, %23, %14
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.stat = type { 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_TRUNC = 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 %51
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 %51
19: ; preds = %12
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %21, %20
%23 = load i32, i32* @O_RDWR
%24 = or i32 %22, %23
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 (i8*, i32, ...) @open(i8* %0, i32 %24, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %19
%30 = load i32, i32* @errno
%31 = call i32 @logger(i32 -1, i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%32 = call i32 @close(i32 %14)
br label %51
33: ; preds = %19, %41
%34 = call i32 @read(i32 %14, i8* %6, i32 4096)
%35 = icmp eq i32 %34, 0
br i1 %35, label %47, label %36
36: ; preds = %33
%37 = icmp slt i32 %34, 0
br i1 %37, label %38, label %41
38: ; preds = %36
%39 = load i32, i32* @errno
%40 = call i32 @logger(i32 -1, i32 %39, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %47
41: ; preds = %36
%42 = call i64 @write(i32 %27, i8* %6, i32 %34)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %33
44: ; preds = %41
%45 = load i32, i32* @errno
%46 = call i32 @logger(i32 -1, i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %47
47: ; preds = %33, %44, %38
%48 = phi i32 [ -1, %38 ], [ -1, %44 ], [ 0, %33 ]
%49 = call i32 @close(i32 %14)
%50 = call i32 @close(i32 %27)
br label %51
51: ; preds = %47, %29, %16, %9
%52 = phi i32 [ -1, %9 ], [ -1, %16 ], [ -1, %29 ], [ %48, %47 ]
ret i32 %52
}