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

/* Variables and functions */
int /*<<< orphan*/ chmod(char const *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *);
int copyfile(const char *from, const char *to) {
  FILE *in, *out;
  char buf[100000];
  in = fopen(from, "r");
  if (in == NULL) {
    printf("Could not open %s to read", from);
    return -1;
  }
  out = fopen(to, "w");
  if (out == NULL) {
    printf("Could not open %s to write", to);
    return -1;
  }
  while (1) {
    int cnt = fread(buf, 1, 100000, in);
    if (cnt <= 0)
      break;
    fwrite(buf, 1, cnt, out);
  }
  fclose(in);
  fclose(out);
  chmod(to, 0644);
  return 0;
}


x86 assembly (clang, -O0):
<copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x186e0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ae <copyfile+0x4e>
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <copyfile+0x107>
mov    -0x18(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e9 <copyfile+0x89>
mov    -0x18(%rbp),%rsi
lea    0xe46(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <copyfile+0x107>
jmp    11ee <copyfile+0x8e>
lea    -0x186d0(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x186a0,%edx
call   1030 <fread@plt>
mov    %eax,-0x186d4(%rbp)
cmpl   $0x0,-0x186d4(%rbp)
jg     1220 <copyfile+0xc0>
jmp    1240 <copyfile+0xe0>
lea    -0x186d0(%rbp),%rdi
mov    -0x186d4(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
jmp    11ee <copyfile+0x8e>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1a4,%esi
call   1060 <chmod@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x186e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x186a8,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     120c <copyfile+0xac>
mov    %rax,%r12
lea    0xe89(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1218 <copyfile+0xb8>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x186a0,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    11eb <copyfile+0x8b>
mov    %rsp,%rbx
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x186a0,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     11c0 <copyfile+0x60>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
mov    $0x1a4,%esi
call   1060 <chmod@plt>
xor    %eax,%eax
jmp    122c <copyfile+0xcc>
lea    0xdef(%rip),%rdi
mov    %rbx,%rsi
jmp    1222 <copyfile+0xc2>
lea    0xdff(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x186a8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"Could not open %s to read\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [27 x i8] c"Could not open %s to write\00"
define i32 @copyfile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [100000 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %45
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %23)
store i32 -1, i32* %3
br label %45
25: ; preds = %17
br label %26
26: ; preds = %25, %33
%27 = getelementptr inbounds [100000 x i8], [100000 x i8]* %8, i64 0, i64 0
%28 = load i32*, i32** %6
%29 = call i32 @fread(i8* %27, i32 1, i32 100000, i32* %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %38
33: ; preds = %26
%34 = getelementptr inbounds [100000 x i8], [100000 x i8]* %8, i64 0, i64 0
%35 = load i32, i32* %9
%36 = load i32*, i32** %7
%37 = call i32 @fwrite(i8* %34, i32 1, i32 %35, i32* %36)
br label %26
38: ; preds = %32
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %5
%44 = call i32 @chmod(i8* %43, i32 420)
store i32 0, i32* %3
br label %45
45: ; preds = %38, %22, %14
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"Could not open %s to read\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [27 x i8] c"Could not open %s to write\00"
define i32 @copyfile(i8* %0, i8* %1) {
%3 = alloca [100000 x i8]
%4 = getelementptr inbounds [100000 x i8], [100000 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %26
9: ; preds = %2
%10 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = call i32 @fread(i8* %4, i32 1, i32 100000, i32* %5)
%14 = icmp slt i32 %13, 1
br i1 %14, label %22, label %17
15: ; preds = %9
%16 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %26
17: ; preds = %12, %17
%18 = phi i32 [ %20, %17 ], [ %13, %12 ]
%19 = call i32 @fwrite(i8* %4, i32 1, i32 %18, i32* %10)
%20 = call i32 @fread(i8* %4, i32 1, i32 100000, i32* %5)
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %17
22: ; preds = %17, %12
%23 = tail call i32 @fclose(i32* %5)
%24 = tail call i32 @fclose(i32* %10)
%25 = tail call i32 @chmod(i8* %1, i32 420)
br label %26
26: ; preds = %22, %15, %7
%27 = phi i32 [ -1, %7 ], [ -1, %15 ], [ 0, %22 ]
ret i32 %27
}