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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ewe(char *, char *);
int open(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ write(int, char *, int);
void copy_file(char *output, char *input) {
  char buf[8192];
  struct stat results;
  int in_fd = open(input, O_RDONLY);
  if (in_fd == -1) {
    ewe("File %s can not be opened\n", input);
  }
  stat(input, &results);
  int out_fd = open(output, O_WRONLY | O_CREAT | O_TRUNC, results.st_mode);
  if (in_fd == -1) {
    ewe("File %s can not be opened\n", output);
  }
  while (1) {
    ssize_t result = read(in_fd, buf, 8192 * sizeof(char));
    if (result == 0) {
      break;
    }
    write(out_fd, buf, result * sizeof(char));
  }
  close(in_fd);
  close(out_fd);
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x201c(%rbp)
cmpl   $0xffffffff,-0x201c(%rbp)
jne    11aa <copy_file+0x4a>
mov    -0x10(%rbp),%rsi
lea    0xe5b(%rip),%rdi
call   1040 <ewe@plt>
mov    -0x10(%rbp),%rdi
lea    -0x2018(%rbp),%rsi
call   1070 <stat@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
mov    0x2e19(%rip),%rax
or     (%rax),%esi
mov    -0x2018(%rbp),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x2020(%rbp)
cmpl   $0xffffffff,-0x201c(%rbp)
jne    1209 <copy_file+0xa9>
mov    -0x8(%rbp),%rsi
lea    0xdfc(%rip),%rdi
call   1040 <ewe@plt>
jmp    120e <copy_file+0xae>
mov    -0x201c(%rbp),%edi
lea    -0x2010(%rbp),%rsi
mov    $0x2000,%edx
call   1060 <read@plt>
mov    %eax,-0x2024(%rbp)
cmpl   $0x0,-0x2024(%rbp)
jne    123d <copy_file+0xdd>
jmp    1261 <copy_file+0x101>
mov    -0x2020(%rbp),%edi
lea    -0x2010(%rbp),%rsi
movslq -0x2024(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
call   1030 <write@plt>
jmp    120e <copy_file+0xae>
mov    -0x201c(%rbp),%edi
call   1050 <close@plt>
mov    -0x2020(%rbp),%edi
call   1050 <close@plt>
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2018,%rsp
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    119d <copy_file+0x3d>
lea    0xe6b(%rip),%rdi
mov    %rbp,%rsi
call   1040 <ewe@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <stat@plt>
mov    0x2e0f(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
mov    0x8(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%r15d
jne    11ea <copy_file+0x8a>
lea    0xe1e(%rip),%rdi
mov    %r14,%rsi
call   1040 <ewe@plt>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x2000,%edx
call   1060 <read@plt>
test   %eax,%eax
je     1230 <copy_file+0xd0>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    1210 <copy_file+0xb0>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
add    $0x2018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [27 x i8] c"File %s can not be opened\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [8192 x i8]
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%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 eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @ewe(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %2
%19 = load i8*, i8** %4
%20 = call i32 @stat(i8* %19, %struct.stat* %6)
%21 = load i8*, i8** %3
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %22, %23
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 (i8*, i32, ...) @open(i8* %21, i32 %26, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = load i8*, i8** %3
%34 = call i32 @ewe(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %18
br label %36
36: ; preds = %35, %43
%37 = load i32, i32* %7
%38 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 8192)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
br label %51
43: ; preds = %36
%44 = load i32, i32* %8
%45 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = mul i64 %47, 1
%49 = trunc i64 %48 to i32
%50 = call i32 @write(i32 %44, i8* %45, i32 %49)
br label %36
51: ; preds = %42
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [27 x i8] c"File %s can not be opened\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca [8192 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [8192 x i8], [8192 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* %1, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @ewe(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %12
12: ; preds = %10, %2
%13 = call i32 @stat(i8* %1, %struct.stat* %4)
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %18, i32 %20)
br i1 %9, label %22, label %24
22: ; preds = %12
%23 = tail call i32 @ewe(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %24
24: ; preds = %22, %12
%25 = call i32 @read(i32 %8, i8* %5, i32 8192)
%26 = icmp eq i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %24, %27
%28 = phi i32 [ %30, %27 ], [ %25, %24 ]
%29 = call i32 @write(i32 %21, i8* %5, i32 %28)
%30 = call i32 @read(i32 %8, i8* %5, i32 8192)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %27
32: ; preds = %27, %24
%33 = tail call i32 @close(i32 %8)
%34 = tail call i32 @close(i32 %21)
ret void
}