#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 BUFSIZ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char **, int, int, int /*<<< orphan*/ *);
size_t fwrite(char **, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int clone_file(const char *to_filename, const char *from_filename) {
  char *buf[BUFSIZ];
  FILE *in, *out;
  size_t len;
  if (NULL == (in = fopen(from_filename, "rb"))) {
    fprintf(stderr, "ERROR - cannot open '%s' in READ mode\n", from_filename);
    return 1;
  }
  if (NULL == (out = fopen(to_filename, "wb"))) {
    fprintf(stderr, "ERROR - cannot open '%s' in WRITE mode\n", to_filename);
    return 1;
  }
  while (0 < (len = fread(buf, sizeof(char), BUFSIZ, in))) {
    if (len != fwrite(buf, sizeof(char), len, out)) {
      fprintf(stderr, "ERROR - cannot write %ld bytes into %s\n", len,
              to_filename);
    }
  }
  fclose(out);
  fclose(in);
  return 0;
}


x86 assembly (clang, -O0):
<clone_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x30(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11dd <clone_file+0x8d>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12d1 <clone_file+0x181>
mov    -0x10(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x38(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    122d <clone_file+0xdd>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12d1 <clone_file+0x181>
jmp    1232 <clone_file+0xe2>
mov    -0x50(%rbp),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
mov    %rcx,-0x40(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jae    12b1 <clone_file+0x161>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
cmp    %rcx,%rax
je     12ac <clone_file+0x15c>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1232 <clone_file+0xe2>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clone_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe77(%rip),%rsi
mov    %r12,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     124c <clone_file+0xfc>
mov    %rax,%r15
lea    0xe86(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1261 <clone_file+0x111>
mov    %rax,%r12
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1238 <clone_file+0xe8>
mov    %rax,%r13
jmp    1201 <clone_file+0xb1>
nopl   0x0(%rax,%rax,1)
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %rax,%r13
test   %rax,%rax
je     1238 <clone_file+0xe8>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r13,%rdx
mov    %r12,%rcx
call   1070 <fwrite@plt>
cmp    %rax,%r13
je     11e0 <clone_file+0x90>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
mov    %r13,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11e0 <clone_file+0x90>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1280 <clone_file+0x130>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
mov    %r12,%rdx
jmp    1274 <clone_file+0x124>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"ERROR - cannot open '%s' in READ mode\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [40 x i8] c"ERROR - cannot open '%s' in WRITE mode\0A\00"
@.str.4 = [40 x i8] c"ERROR - cannot write %ld bytes into %s\0A\00"
define i32 @clone_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i32, i32* @BUFSIZ
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8*, i64 %13
store i64 %13, i64* %7
%16 = load i8*, i8** %5
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %8
%18 = icmp eq i32* null, %17
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %5
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 1, i32* %3
store i32 1, i32* %11
br label %54
23: ; preds = %2
%24 = load i8*, i8** %4
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %9
%26 = icmp eq i32* null, %25
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %4
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %29)
store i32 1, i32* %3
store i32 1, i32* %11
br label %54
31: ; preds = %23
br label %32
32: ; preds = %48, %31
%33 = load i32, i32* @BUFSIZ
%34 = load i32*, i32** %8
%35 = call i64 @fread(i8** %15, i32 1, i32 %33, i32* %34)
store i64 %35, i64* %10
%36 = icmp ult i64 0, %35
br i1 %36, label %37, label %49
37: ; preds = %32
%38 = load i64, i64* %10
%39 = load i64, i64* %10
%40 = load i32*, i32** %9
%41 = call i64 @fwrite(i8** %15, i32 1, i64 %39, i32* %40)
%42 = icmp ne i64 %38, %41
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = load i64, i64* %10
%46 = load i8*, i8** %4
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i64 %45, i8* %46)
br label %48
48: ; preds = %43, %37
br label %32
49: ; preds = %32
%50 = load i32*, i32** %9
%51 = call i32 @fclose(i32* %50)
%52 = load i32*, i32** %8
%53 = call i32 @fclose(i32* %52)
store i32 0, i32* %3
store i32 1, i32* %11
br label %54
54: ; preds = %49, %27, %19
%55 = load i8*, i8** %6
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"ERROR - cannot open '%s' in READ mode\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [40 x i8] c"ERROR - cannot open '%s' in WRITE mode\0A\00"
@.str.4 = [40 x i8] c"ERROR - cannot write %ld bytes into %s\0A\00"
define i32 @clone_file(i8* %0, i8* %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8*, i64 %4
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %35
11: ; preds = %2
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = load i32, i32* @BUFSIZ
%16 = call i64 @fread(i8** %5, i32 1, i32 %15, i32* %6)
%17 = icmp eq i64 %16, 0
br i1 %17, label %32, label %21
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %35
21: ; preds = %14, %28
%22 = phi i64 [ %30, %28 ], [ %16, %14 ]
%23 = call i64 @fwrite(i8** %5, i32 1, i64 %22, i32* %12)
%24 = icmp eq i64 %22, %23
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i64 %22, i8* %0)
br label %28
28: ; preds = %25, %21
%29 = load i32, i32* @BUFSIZ
%30 = call i64 @fread(i8** %5, i32 1, i32 %29, i32* %6)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %21
32: ; preds = %28, %14
%33 = tail call i32 @fclose(i32* %12)
%34 = tail call i32 @fclose(i32* %6)
br label %35
35: ; preds = %32, %18, %8
%36 = phi i32 [ 1, %8 ], [ 1, %18 ], [ 0, %32 ]
ret i32 %36
}