#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 /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ chmod(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ stat(char *, struct stat *);
void copy_file(char *origin, char *dest) {
  FILE *in = fopen(origin, "r"), *out;
  char *buf[1024];
  struct stat file_stat;
  if (in == NULL) {
    printf("Error: Origin file %s can't be opened\n", origin);
    return;
  }
  stat(origin, &file_stat);
  out = fopen(dest, "w");
  if (out == NULL) {
    printf("Error: Destination file %s can't be opened\n", dest);
    return;
  }
  printf("%s -> %s\n", origin, dest);
  while (!feof(in)) {
    int i = fread(buf, sizeof(char), sizeof(buf), in);
    if (i > 0)
      fwrite(buf, sizeof(char), i, out);
  }
  fclose(in);
  fclose(out);
  chmod(dest, file_stat.st_mode);
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c9 <copy_file+0x49>
mov    -0x8(%rbp),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12b1 <copy_file+0x131>
mov    -0x8(%rbp),%rdi
lea    -0x2028(%rbp),%rsi
call   1070 <stat@plt>
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120f <copy_file+0x8f>
mov    -0x10(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12b1 <copy_file+0x131>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1243 <copy_file+0xc3>
jmp    1290 <copy_file+0x110>
lea    -0x2020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %eax,-0x202c(%rbp)
cmpl   $0x0,-0x202c(%rbp)
jle    128b <copy_file+0x10b>
lea    -0x2020(%rbp),%rdi
mov    -0x202c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    1225 <copy_file+0xa5>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
mov    -0x2028(%rbp),%esi
call   1080 <chmod@plt>
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2018,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1216 <copy_file+0x96>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1070 <stat@plt>
lea    0xe6d(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1222 <copy_file+0xa2>
mov    %rax,%r15
lea    0xe84(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     1241 <copy_file+0xc1>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%esi
mov    %r14,%rdi
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1080 <chmod@plt>
lea    0xde5(%rip),%rdi
mov    %r12,%rsi
jmp    122c <copy_file+0xac>
lea    0xe02(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <printf@plt>
lea    0x10(%rsp),%r12
jmp    125c <copy_file+0xdc>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11ec <copy_file+0x6c>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1250 <copy_file+0xd0>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
jmp    1250 <copy_file+0xd0>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [39 x i8] c"Error: Origin file %s can't be opened\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [44 x i8] c"Error: Destination file %s can't be opened\0A\00"
@.str.4 = [10 x i8] c"%s -> %s\0A\00"
define void @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 %struct.stat
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %57
17: ; preds = %2
%18 = load i8*, i8** %3
%19 = call i32 @stat(i8* %18, %struct.stat* %8)
%20 = load i8*, i8** %4
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = load i8*, i8** %4
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %57
27: ; preds = %17
%28 = load i8*, i8** %3
%29 = load i8*, i8** %4
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %28, i8* %29)
br label %31
31: ; preds = %47, %27
%32 = load i32*, i32** %5
%33 = call i32 @feof(i32* %32)
%34 = icmp ne i32 %33, 0
%35 = xor i1 %34, true
br i1 %35, label %36, label %48
36: ; preds = %31
%37 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %7, i64 0, i64 0
%38 = load i32*, i32** %5
%39 = call i32 @fread(i8** %37, i32 1, i32 8192, i32* %38)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %36
%43 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %7, i64 0, i64 0
%44 = load i32, i32* %9
%45 = load i32*, i32** %6
%46 = call i32 @fwrite(i8** %43, i32 1, i32 %44, i32* %45)
br label %47
47: ; preds = %42, %36
br label %31
48: ; preds = %31
%49 = load i32*, i32** %5
%50 = call i32 @fclose(i32* %49)
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
%53 = load i8*, i8** %4
%54 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @chmod(i8* %53, i32 %55)
br label %57
57: ; preds = %48, %24, %14
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [39 x i8] c"Error: Origin file %s can't be opened\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [44 x i8] c"Error: Destination file %s can't be opened\0A\00"
@.str.4 = [10 x i8] c"%s -> %s\0A\00"
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca [1024 x i8*]
%4 = alloca %struct.stat
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = bitcast [1024 x i8*]* %3 to i8*
%7 = bitcast %struct.stat* %4 to i8*
%8 = icmp eq i32* %5, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %37
11: ; preds = %2
%12 = call i32 @stat(i8* %0, %struct.stat* %4)
%13 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %37
17: ; preds = %11
%18 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %1)
%19 = tail call i32 @feof(i32* %5)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %3, i64 0, i64 0
br label %23
23: ; preds = %21, %28
%24 = call i32 @fread(i8** %22, i32 1, i32 8192, i32* %5)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @fwrite(i8** %22, i32 1, i32 %24, i32* %13)
br label %28
28: ; preds = %26, %23
%29 = tail call i32 @feof(i32* %5)
%30 = icmp eq i32 %29, 0
br i1 %30, label %23, label %31
31: ; preds = %28, %17
%32 = tail call i32 @fclose(i32* %5)
%33 = tail call i32 @fclose(i32* %13)
%34 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%35 = load i32, i32* %34
%36 = tail call i32 @chmod(i8* %1, i32 %35)
br label %37
37: ; preds = %31, %15, %9
ret void
}