#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;
};

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ write(int, char *, int);
bool copy_file(const char *src_dir, const char *dst_dir,
               const char *file_name) {
  int destfd = -1;
  bool return_code = true;
  char src_file_name[1024];
  sprintf(src_file_name, "%s/%s", src_dir, file_name);
  char dst_file_name[1024];
  sprintf(dst_file_name, "%s/%s", dst_dir, file_name);
  int sourcefd = open(src_file_name, O_RDONLY);
  if (sourcefd < 0) {
    perror("Error opening source file");
    return_code = false;
    goto ret;
  }
  struct stat buf;
  int res = fstat(sourcefd, &buf);
  if (res != 0) {
    perror("Error reading source file permissions");
    return_code = false;
    goto ret;
  }
  destfd = open(dst_file_name, O_CREAT | O_EXCL | O_WRONLY, buf.st_mode);
  if (destfd == -1) {
    perror("Error opening destination file");
    return_code = false;
    goto ret;
  }
  char buffer[1024];
  for (int size = read(sourcefd, buffer, 1024); size != 0;
       size = read(sourcefd, buffer, 1024))
    write(destfd, buffer, size);
ret:
  if (sourcefd != -1)
    close(sourcefd);
  if (destfd != -1)
    close(destfd);
  return return_code;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe55(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe3a(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x420(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0x0,-0x824(%rbp)
jge    120d <copy_file+0x9d>
lea    0xe0a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x20(%rbp)
jmp    1304 <copy_file+0x194>
mov    -0x824(%rbp),%edi
lea    -0x828(%rbp),%rsi
call   1090 <fstat@plt>
mov    %eax,-0x82c(%rbp)
cmpl   $0x0,-0x82c(%rbp)
je     124a <copy_file+0xda>
lea    0xde7(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x20(%rbp)
jmp    1304 <copy_file+0x194>
lea    -0x820(%rbp),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    0x2d6f(%rip),%rax
or     (%rax),%esi
mov    0x2d56(%rip),%rax
or     (%rax),%esi
mov    -0x828(%rbp),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    129e <copy_file+0x12e>
lea    0xdb9(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x20(%rbp)
jmp    1304 <copy_file+0x194>
mov    -0x824(%rbp),%edi
lea    -0xc30(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0xc34(%rbp)
cmpl   $0x0,-0xc34(%rbp)
je     12ff <copy_file+0x18f>
mov    -0x1c(%rbp),%edi
lea    -0xc30(%rbp),%rsi
mov    -0xc34(%rbp),%edx
call   1030 <write@plt>
mov    -0x824(%rbp),%edi
lea    -0xc30(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0xc34(%rbp)
jmp    12bb <copy_file+0x14b>
jmp    1304 <copy_file+0x194>
cmpl   $0xffffffff,-0x824(%rbp)
je     131c <copy_file+0x1ac>
mov    -0x824(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
je     132e <copy_file+0x1be>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
add    $0xc40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xc18,%rsp
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%rdx
lea    0xe73(%rip),%r14
lea    0x810(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
call   1080 <sprintf@plt>
lea    0x410(%rsp),%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1080 <sprintf@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     12a1 <copy_file+0x131>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
call   1090 <fstat@plt>
test   %eax,%eax
jne    12ba <copy_file+0x14a>
mov    0x2e02(%rip),%rax
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db8(%rip),%rax
or     (%rax),%esi
mov    0x8(%rsp),%edx
lea    0x410(%rsp),%rdi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     12c3 <copy_file+0x153>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1299 <copy_file+0x129>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    1250 <copy_file+0xe0>
mov    $0x1,%r14d
cmp    $0xffffffff,%r15d
jne    12d7 <copy_file+0x167>
cmp    $0xffffffff,%ebp
je     1288 <copy_file+0x118>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0xc18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%r14d
jmp    12d7 <copy_file+0x167>
lea    0xd5e(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%r15d
jne    12d7 <copy_file+0x167>
jmp    127c <copy_file+0x10c>
lea    0xd5f(%rip),%rdi
jmp    12ca <copy_file+0x15a>
lea    0xd7c(%rip),%rdi
call   1070 <perror@plt>
xor    %r14d,%r14d
mov    $0xffffffff,%ebp
mov    %r15d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebp
jne    1281 <copy_file+0x111>
jmp    1288 <copy_file+0x118>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"Error opening source file\00"
@.str.2 = [38 x i8] c"Error reading source file permissions\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [31 x i8] c"Error opening destination file\00"
define i32 @copy_file(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca %struct.stat
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %2)
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
%12 = load i32, i32* @O_RDONLY
%13 = call i32 (i8*, i32, ...) @open(i8* %8, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %39
17: ; preds = %3
%18 = call i32 @fstat(i32 %13, %struct.stat* %6)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %43
20: ; preds = %17
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_EXCL
%23 = or i32 %22, %21
%24 = load i32, i32* @O_WRONLY
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %25, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %43, label %30
30: ; preds = %20
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%32 = call i32 @read(i32 %13, i8* %31, i32 1024)
%33 = icmp eq i32 %32, 0
br i1 %33, label %46, label %34
34: ; preds = %30, %34
%35 = phi i32 [ %37, %34 ], [ %32, %30 ]
%36 = call i32 @write(i32 %28, i8* %31, i32 %35)
%37 = call i32 @read(i32 %13, i8* %31, i32 1024)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %34
39: ; preds = %34, %15
%40 = phi i32 [ 0, %15 ], [ 1, %34 ]
%41 = phi i32 [ -1, %15 ], [ %28, %34 ]
%42 = icmp eq i32 %13, -1
br i1 %42, label %50, label %46
43: ; preds = %20, %17
%44 = phi i8* [ getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), %17 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), %20 ]
%45 = tail call i32 @perror(i8* %44)
br label %46
46: ; preds = %43, %30, %39
%47 = phi i32 [ %41, %39 ], [ %28, %30 ], [ -1, %43 ]
%48 = phi i32 [ %40, %39 ], [ 1, %30 ], [ 0, %43 ]
%49 = tail call i32 @close(i32 %13)
br label %50
50: ; preds = %46, %39
%51 = phi i32 [ %47, %46 ], [ %41, %39 ]
%52 = phi i32 [ %48, %46 ], [ %40, %39 ]
%53 = icmp eq i32 %51, -1
br i1 %53, label %56, label %54
54: ; preds = %50
%55 = tail call i32 @close(i32 %51)
br label %56
56: ; preds = %54, %50
ret i32 %52
}