#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 {
  scalar_t__ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ PROT_READ;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, scalar_t__);
int open(char *, int, ...);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, void *, scalar_t__);
int copy_file(char *src, char *dst) {
  struct stat st;
  int src_fd = open(src, O_RDONLY);
  if (src_fd < 0) {
    fprintf(stderr, "Cannot open file %s (error code %d)\n", src, errno);
    return -1;
  }
  int dst_fd = open(dst, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  if (dst_fd < 0) {
    fprintf(stderr, "Cannot open file %s (error code %d)\n", dst, errno);
    close(src_fd);
    return -1;
  }
  if (fstat(src_fd, &st) < 0) {
    fprintf(stderr, "Cannot get size of file %s (return code %d)\n", src,
            errno);
    close(src_fd);
    close(dst_fd);
    return -1;
  }
  void *mem = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, src_fd, 0);
  if (mem == MAP_FAILED) {
    fprintf(stderr, "Cannot mmap file %s (error code %d)\n", src, errno);
    close(src_fd);
    close(dst_fd);
    return -1;
  }
  if (write(dst_fd, mem, st.st_size) != st.st_size) {
    fprintf(stderr, "Cannot write file %s (error code %d)\n", dst, errno);
    close(src_fd);
    close(dst_fd);
    munmap(mem, st.st_size);
    return -1;
  }
  close(src_fd);
  close(dst_fd);
  munmap(mem, st.st_size);
  return 0;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11cf <copy_file+0x5f>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    0x2df3(%rip),%rax
mov    (%rax),%ecx
lea    0xe42(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a3 <copy_file+0x233>
mov    -0x18(%rbp),%rdi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2dc4(%rip),%rax
or     (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
mov    0x2dca(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    124a <copy_file+0xda>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    0x2d80(%rip),%rax
mov    (%rax),%ecx
lea    0xdcf(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a3 <copy_file+0x233>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    129e <copy_file+0x12e>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    0x2d34(%rip),%rax
mov    (%rax),%ecx
lea    0xda8(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a3 <copy_file+0x233>
mov    -0x20(%rbp),%rsi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
mov    0x2d2e(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2cc5(%rip),%rcx
cmp    (%rcx),%rax
jne    131a <copy_file+0x1aa>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    0x2cb8(%rip),%rax
mov    (%rax),%ecx
lea    0xd59(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a3 <copy_file+0x233>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x20(%rbp),%rax
je     137f <copy_file+0x20f>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    0x2c60(%rip),%rax
mov    (%rax),%ecx
lea    0xd26(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <munmap@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a3 <copy_file+0x233>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbp
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1261 <copy_file+0xf1>
mov    %eax,%ebx
mov    0x2de6(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
mov    0x2de3(%rip),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1284 <copy_file+0x114>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %ebx,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     12a9 <copy_file+0x139>
mov    (%rsp),%r12
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    0x2ddc(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r13
mov    0x2d78(%rip),%rax
cmp    (%rax),%r13
je     12c4 <copy_file+0x154>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1030 <write@plt>
cmp    %r12,%rax
jne    1308 <copy_file+0x198>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1070 <munmap@plt>
xor    %eax,%eax
jmp    12f9 <copy_file+0x189>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%ecx
lea    0xd86(%rip),%rsi
mov    %rbp,%rdx
call   1060 <fprintf@plt>
jmp    12f4 <copy_file+0x184>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    0x2d14(%rip),%rax
mov    (%rax),%ecx
lea    0xd63(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
mov    %ebx,%edi
jmp    12ef <copy_file+0x17f>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
mov    0x2cef(%rip),%rax
mov    (%rax),%ecx
lea    0xd63(%rip),%rsi
jmp    12dd <copy_file+0x16d>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
mov    0x2cd4(%rip),%rax
mov    (%rax),%ecx
lea    0xd75(%rip),%rsi
mov    %rbp,%rdx
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
mov    0x2c90(%rip),%rax
mov    (%rax),%ecx
lea    0xd56(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1070 <munmap@plt>
jmp    12f4 <copy_file+0x184>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [37 x i8] c"Cannot open file %s (error code %d)\0A\00"
@errno = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [45 x i8] c"Cannot get size of file %s (return code %d)\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [37 x i8] c"Cannot mmap file %s (error code %d)\0A\00"
@.str.3 = [38 x i8] c"Cannot write file %s (error code %d)\0A\00"
define i32 @copy_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = load i32, i32* @errno
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %18)
store i32 -1, i32* %3
br label %103
20: ; preds = %2
%21 = load i8*, i8** %5
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %22, %23
%25 = load i32, i32* @O_EXCL
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %27, %28
%30 = call i32 (i8*, i32, ...) @open(i8* %21, i32 %26, i32 %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %20
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %5
%36 = load i32, i32* @errno
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %35, i32 %36)
%38 = load i32, i32* %7
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %3
br label %103
40: ; preds = %20
%41 = load i32, i32* %7
%42 = call i64 @fstat(i32 %41, %struct.stat* %6)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %53
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %4
%47 = load i32, i32* @errno
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %46, i32 %47)
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %8
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %3
br label %103
53: ; preds = %40
%54 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%55 = load i64, i64* %54
%56 = load i32, i32* @PROT_READ
%57 = load i32, i32* @MAP_SHARED
%58 = load i32, i32* %7
%59 = call i8* @mmap(i32* null, i64 %55, i32 %56, i32 %57, i32 %58, i32 0)
store i8* %59, i8** %9
%60 = load i8*, i8** %9
%61 = load i8*, i8** @MAP_FAILED
%62 = icmp eq i8* %60, %61
br i1 %62, label %63, label %72
63: ; preds = %53
%64 = load i32, i32* @stderr
%65 = load i8*, i8** %4
%66 = load i32, i32* @errno
%67 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %65, i32 %66)
%68 = load i32, i32* %7
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %8
%71 = call i32 @close(i32 %70)
store i32 -1, i32* %3
br label %103
72: ; preds = %53
%73 = load i32, i32* %8
%74 = load i8*, i8** %9
%75 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%76 = load i64, i64* %75
%77 = call i64 @write(i32 %73, i8* %74, i64 %76)
%78 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%79 = load i64, i64* %78
%80 = icmp ne i64 %77, %79
br i1 %80, label %81, label %94
81: ; preds = %72
%82 = load i32, i32* @stderr
%83 = load i8*, i8** %5
%84 = load i32, i32* @errno
%85 = call i32 @fprintf(i32 %82, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %83, i32 %84)
%86 = load i32, i32* %7
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* %8
%89 = call i32 @close(i32 %88)
%90 = load i8*, i8** %9
%91 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%92 = load i64, i64* %91
%93 = call i32 @munmap(i8* %90, i64 %92)
store i32 -1, i32* %3
br label %103
94: ; preds = %72
%95 = load i32, i32* %7
%96 = call i32 @close(i32 %95)
%97 = load i32, i32* %8
%98 = call i32 @close(i32 %97)
%99 = load i8*, i8** %9
%100 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%101 = load i64, i64* %100
%102 = call i32 @munmap(i8* %99, i64 %101)
store i32 0, i32* %3
br label %103
103: ; preds = %94, %81, %63, %44, %33, %15
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [37 x i8] c"Cannot open file %s (error code %d)\0A\00"
@errno = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [45 x i8] c"Cannot get size of file %s (return code %d)\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [37 x i8] c"Cannot mmap file %s (error code %d)\0A\00"
@.str.3 = [38 x i8] c"Cannot write file %s (error code %d)\0A\00"
define i32 @copy_file(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %10)
br label %65
12: ; preds = %2
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_EXCL
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %19, %18
%21 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %17, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %12
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %25)
%27 = tail call i32 @close(i32 %6)
br label %65
28: ; preds = %12
%29 = call i64 @fstat(i32 %6, %struct.stat* %3)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %33)
%35 = tail call i32 @close(i32 %6)
%36 = tail call i32 @close(i32 %21)
br label %65
37: ; preds = %28
%38 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%39 = load i64, i64* %38
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @MAP_SHARED
%42 = tail call i8* @mmap(i32* null, i64 %39, i32 %40, i32 %41, i32 %6, i32 0)
%43 = load i8*, i8** @MAP_FAILED
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %51
45: ; preds = %37
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = tail call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %47)
%49 = tail call i32 @close(i32 %6)
%50 = tail call i32 @close(i32 %21)
br label %65
51: ; preds = %37
%52 = tail call i64 @write(i32 %21, i8* %42, i64 %39)
%53 = icmp eq i64 %52, %39
br i1 %53, label %61, label %54
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = tail call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %1, i32 %56)
%58 = tail call i32 @close(i32 %6)
%59 = tail call i32 @close(i32 %21)
%60 = tail call i32 @munmap(i8* %42, i64 %39)
br label %65
61: ; preds = %51
%62 = tail call i32 @close(i32 %6)
%63 = tail call i32 @close(i32 %21)
%64 = tail call i32 @munmap(i8* %42, i64 %39)
br label %65
65: ; preds = %23, %31, %61, %54, %45, %8
%66 = phi i32 [ -1, %8 ], [ -1, %23 ], [ -1, %31 ], [ -1, %45 ], [ -1, %54 ], [ 0, %61 ]
ret i32 %66
}