#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 /*<<< orphan*/ F_OK;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
int write(int, char *, int);
int copy_file(char *src, char *dst) {
  int src_fd;
  int dst_fd;
  char buffer[1024];
  int chars_read;
  int chars_written;
  struct stat src_stat;
  src_fd = open(src, O_RDONLY);
  if (src_fd < 0) {
    perror("open");
    exit(1);
  }
  if (access(dst, F_OK) == 0) {
    dst_fd = open(dst, O_WRONLY | O_TRUNC);
  } else {
    if (stat(src, &src_stat) == 0) {
      dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, src_stat.st_mode);
    } else {
      perror("stat");
      exit(1);
    }
  }
  if (dst_fd < 0) {
    perror("open");
    exit(1);
  }
  do {
    chars_read = read(src_fd, buffer, 1024);
    if (chars_read < 0) {
      perror("read");
      exit(1);
    }
    chars_written = write(dst_fd, buffer, chars_read);
    if (chars_written < 0) {
      perror("write");
      exit(1);
    }
  } while (chars_read);
  if (close(src_fd) < 0) {
    perror("close");
    exit(1);
  }
  if (close(dst_fd) < 0) {
    perror("close");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ca <copy_file+0x4a>
lea    0xe45(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    120b <copy_file+0x8b>
mov    -0x10(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
jmp    1274 <copy_file+0xf4>
mov    -0x8(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1259 <copy_file+0xd9>
mov    -0x10(%rbp),%rdi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2dbf(%rip),%rax
or     (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
mov    -0x430(%rbp),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
jmp    126f <copy_file+0xef>
lea    0xda5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1274 <copy_file+0xf4>
cmpl   $0x0,-0x18(%rbp)
jge    1294 <copy_file+0x114>
lea    0xd7b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1299 <copy_file+0x119>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    12d6 <copy_file+0x156>
lea    0xd43(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0x0,-0x428(%rbp)
jge    1314 <copy_file+0x194>
lea    0xd0a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1319 <copy_file+0x199>
cmpl   $0x0,-0x424(%rbp)
jne    1299 <copy_file+0x119>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    134e <copy_file+0x1ce>
lea    0xcd6(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1376 <copy_file+0x1f6>
lea    0xcae(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     12b4 <copy_file+0x134>
mov    %eax,%r15d
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     1205 <copy_file+0x85>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    12c6 <copy_file+0x146>
mov    0x2ddf(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dfd(%rip),%rax
or     (%rax),%esi
mov    0x8(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <open@plt>
jmp    1221 <copy_file+0xa1>
mov    0x2db4(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     12b4 <copy_file+0x134>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1295 <copy_file+0x115>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     129e <copy_file+0x11e>
test   %ebp,%ebp
jne    1240 <copy_file+0xc0>
mov    %r15d,%edi
call   1040 <close@plt>
test   %rax,%rax
js     12bd <copy_file+0x13d>
mov    %r14d,%edi
call   1040 <close@plt>
test   %rax,%rax
js     12bd <copy_file+0x13d>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6e(%rip),%rdi
jmp    12a5 <copy_file+0x125>
lea    0xd6a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd45(%rip),%rdi
jmp    12a5 <copy_file+0x125>
lea    0xd51(%rip),%rdi
jmp    12a5 <copy_file+0x125>
lea    0xd38(%rip),%rdi
jmp    12a5 <copy_file+0x125>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@F_OK = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"stat\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [6 x i8] c"close\00"
define i32 @copy_file(i8* %0, i8* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @F_OK
%15 = tail call i64 @access(i8* %1, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %19, %18
%21 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %20)
br label %37
22: ; preds = %13
%23 = call i64 @stat(i8* %0, %struct.stat* %4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = load i32, i32* @O_WRONLY
%27 = load i32, i32* @O_CREAT
%28 = or i32 %27, %26
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 (i8*, i32, ...) @open(i8* %1, i32 %30, i32 %32)
br label %37
34: ; preds = %22
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %25, %17
%38 = phi i32 [ %21, %17 ], [ %33, %25 ]
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %37, %55
%44 = call i32 @read(i32 %8, i8* %5, i32 1024)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %43
%50 = call i32 @write(i32 %38, i8* %5, i32 %44)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %49
%56 = icmp eq i32 %44, 0
br i1 %56, label %57, label %43
57: ; preds = %55
%58 = call i64 @close(i32 %8)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %57
%64 = call i64 @close(i32 %38)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %63
ret i32 0
}