#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 */

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ logprintf(char *, char const *, ...);
char *malloc(int);
int open(char const *, int);
size_t read(int, char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ test_file(char const *);
int /*<<< orphan*/ write(int, char *, size_t);
void cp_file(const char *src, const char *dest) {
  int src_fd, dest_fd;
  char *buf;
  size_t size;
  if (!test_file(src)) {
    logprintf("1file %s doesn't exist, so can't copy", src);
    return;
  }
  buf = malloc(65536);
  if (buf == NULL) {
    logprintf("2%s: %s", "2can't allocate buffer", strerror(errno));
    return;
  }
  src_fd = open(src, O_RDONLY);
  if (src_fd < 0) {
    logprintf("2%s: %s", "2can't open source file", strerror(errno));
    return;
  }
  dest_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC);
  if (dest_fd < 0) {
    logprintf("2%s: %s", "2can't open destination file", strerror(errno));
    close(src_fd);
    return;
  }
  do {
    size = read(src_fd, buf, 65536);
    if (size == -1) {
      logprintf("2%s: %s", "error reading from source file", strerror(errno));
      break;
    }
    write(dest_fd, buf, size);
    if (size == -1) {
      logprintf("2%s: %s", "error writing to destination file",
                strerror(errno));
      break;
    }
  } while (size == 65536);
  close(src_fd);
  close(dest_fd);
  free(buf);
}


x86 assembly (clang, -O0):
<cp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <test_file@plt>
cmp    $0x0,%eax
jne    11c9 <cp_file+0x39>
mov    -0x8(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   10b0 <logprintf@plt>
jmp    1378 <cp_file+0x1e8>
mov    $0x10000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120c <cp_file+0x7c>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%edx
lea    0xe2d(%rip),%rdi
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   10b0 <logprintf@plt>
jmp    1378 <cp_file+0x1e8>
mov    -0x8(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1255 <cp_file+0xc5>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%edx
lea    0xde4(%rip),%rdi
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   10b0 <logprintf@plt>
jmp    1378 <cp_file+0x1e8>
mov    -0x10(%rbp),%rdi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
mov    0x2d8f(%rip),%rax
or     (%rax),%esi
mov    0x2d7e(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12b8 <cp_file+0x128>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%edx
lea    0xd89(%rip),%rdi
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   10b0 <logprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    1378 <cp_file+0x1e8>
jmp    12bd <cp_file+0x12d>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x10000,%edx
call   1070 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1307 <cp_file+0x177>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%edx
lea    0xd32(%rip),%rdi
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   10b0 <logprintf@plt>
jmp    135f <cp_file+0x1cf>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <write@plt>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    134c <cp_file+0x1bc>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%edx
lea    0xced(%rip),%rdi
lea    0xd59(%rip),%rsi
mov    $0x0,%al
call   10b0 <logprintf@plt>
jmp    135f <cp_file+0x1cf>
jmp    1351 <cp_file+0x1c1>
cmpq   $0x10000,-0x28(%rbp)
je     12bd <cp_file+0x12d>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbp
call   1050 <test_file@plt>
test   %eax,%eax
je     1249 <cp_file+0xb9>
mov    $0x10000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1264 <cp_file+0xd4>
mov    %rax,%rbx
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1282 <cp_file+0xf2>
mov    %eax,%r14d
mov    0x2dd5(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12b1 <cp_file+0x121>
mov    %eax,%r15d
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x10000,%edx
call   1070 <read@plt>
cmp    $0xffffffffffffffff,%rax
je     12e8 <cp_file+0x158>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    $0x10000,%rbp
je     1210 <cp_file+0x80>
jmp    130d <cp_file+0x17d>
lea    0xdb0(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <logprintf@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdad(%rip),%rdi
lea    0xdae(%rip),%rsi
jmp    129e <cp_file+0x10e>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd8f(%rip),%rdi
lea    0xda7(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <logprintf@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd60(%rip),%rdi
lea    0xd90(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <logprintf@plt>
mov    %r14d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <close@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd29(%rip),%rdi
lea    0xd76(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <logprintf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"1file %s doesn't exist, so can't copy\00"
@.str.1 = [8 x i8] c"2%s: %s\00"
@.str.2 = [23 x i8] c"2can't allocate buffer\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [24 x i8] c"2can't open source file\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [29 x i8] c"2can't open destination file\00"
@.str.5 = [31 x i8] c"error reading from source file\00"
@.str.6 = [34 x i8] c"error writing to destination file\00"
define void @cp_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i32 @test_file(i8* %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %82
15: ; preds = %2
%16 = call i8* @malloc(i32 65536)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %82
23: ; preds = %15
%24 = load i8*, i8** %3
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %82
33: ; preds = %23
%34 = load i8*, i8** %4
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %34, i32 %39)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %33
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %5
%48 = call i32 @close(i32 %47)
br label %82
49: ; preds = %33
br label %50
50: ; preds = %72, %49
%51 = load i32, i32* %5
%52 = load i8*, i8** %7
%53 = call i64 @read(i32 %51, i8* %52, i32 65536)
store i64 %53, i64* %8
%54 = load i64, i64* %8
%55 = icmp eq i64 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %50
%57 = load i32, i32* @errno
%58 = call i32 @strerror(i32 %57)
%59 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %58)
br label %75
60: ; preds = %50
%61 = load i32, i32* %6
%62 = load i8*, i8** %7
%63 = load i64, i64* %8
%64 = call i32 @write(i32 %61, i8* %62, i64 %63)
%65 = load i64, i64* %8
%66 = icmp eq i64 %65, -1
br i1 %66, label %67, label %71
67: ; preds = %60
%68 = load i32, i32* @errno
%69 = call i32 @strerror(i32 %68)
%70 = call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i32 %69)
br label %75
71: ; preds = %60
br label %72
72: ; preds = %71
%73 = load i64, i64* %8
%74 = icmp eq i64 %73, 65536
br i1 %74, label %50, label %75
75: ; preds = %72, %67, %56
%76 = load i32, i32* %5
%77 = call i32 @close(i32 %76)
%78 = load i32, i32* %6
%79 = call i32 @close(i32 %78)
%80 = load i8*, i8** %7
%81 = call i32 @free(i8* %80)
br label %82
82: ; preds = %75, %43, %29, %19, %12
ret void
}

LLVM IR (-O2):
@.str = [38 x i8] c"1file %s doesn't exist, so can't copy\00"
@.str.1 = [8 x i8] c"2%s: %s\00"
@.str.2 = [23 x i8] c"2can't allocate buffer\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [24 x i8] c"2can't open source file\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [29 x i8] c"2can't open destination file\00"
@.str.5 = [31 x i8] c"error reading from source file\00"
define void @cp_file(i8* %0, i8* nocapture %1) {
%3 = tail call i32 @test_file(i8* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %49
7: ; preds = %2
%8 = tail call dereferenceable_or_null(65536) i8* @malloc(i32 65536)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %49
14: ; preds = %7
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %0, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @errno
%20 = tail call i32 @strerror(i32 %19)
%21 = tail call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %49
22: ; preds = %14
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_TRUNC
%27 = or i32 %25, %26
%28 = tail call i32 @open(i8* %1, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %22
%31 = load i32, i32* @errno
%32 = tail call i32 @strerror(i32 %31)
%33 = tail call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = tail call i32 @close(i32 %16)
br label %49
35: ; preds = %22, %42
%36 = tail call i64 @read(i32 %16, i8* %8, i32 65536)
%37 = icmp eq i64 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = tail call i32 @strerror(i32 %39)
%41 = tail call i32 (i8*, i8*, ...) @logprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %40)
br label %45
42: ; preds = %35
%43 = tail call i32 @write(i32 %28, i8* %8, i64 %36)
%44 = icmp eq i64 %36, 65536
br i1 %44, label %35, label %45
45: ; preds = %42, %38
%46 = tail call i32 @close(i32 %16)
%47 = tail call i32 @close(i32 %28)
%48 = tail call i32 @free(i8* %8)
br label %49
49: ; preds = %45, %30, %18, %10, %5
ret void
}