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

/* Variables and functions */
int BUFSIZ;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int copy(const char *from_filepath, const char *to_filepath) {
  int result = EXIT_FAILURE;
  char buffer[BUFSIZ];
  size_t nitems;
  FILE *from_file = fopen(from_filepath, "r");
  ;
  if (from_file != NULL) {
    FILE *to_file = fopen(to_filepath, "w");
    ;
    if (to_file != NULL) {
      while ((nitems = fread(buffer, sizeof(char), sizeof(buffer), from_file)) >
             0) {
        if (fwrite(buffer, sizeof(char), nitems, to_file) != nitems) {
          fprintf(stderr, "ERROR: Failure while copying file, errno = %d.\n",
                  errno);
          goto exit_copy;
        }
      }
      result = EXIT_SUCCESS;
      fclose(to_file);
    } else {
      fprintf(stderr,
              "ERROR: Unable to open destination filepath for writing, errno = "
              "%d.\n",
              errno);
    }
    fclose(from_file);
  } else {
    fprintf(stderr,
            "ERROR: Unable to open source filepath for reading, errno = %d.\n",
            errno);
  }
exit_copy:
  return result;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1293 <copy+0x143>
mov    -0x10(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1267 <copy+0x117>
jmp    11df <copy+0x8f>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jbe    124d <copy+0xfd>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
cmp    -0x30(%rbp),%rax
je     1248 <copy+0xf8>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
lea    0xdc6(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12b6 <copy+0x166>
jmp    11df <copy+0x8f>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1285 <copy+0x135>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
lea    0xdb4(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12b1 <copy+0x161>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
lea    0xdcd(%rip),%rsi
call   1050 <fprintf@plt>
jmp    12b6 <copy+0x166>
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    0x2e60(%rip),%rax
mov    (%rax),%r14d
mov    0x2e66(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe72(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1217 <copy+0xc7>
mov    %rax,%r15
lea    0xe60(%rip),%rsi
mov    %r13,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1237 <copy+0xe7>
mov    %rax,%r13
mov    %r14d,-0x2c(%rbp)
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1257 <copy+0x107>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r13,%rcx
call   1070 <fwrite@plt>
cmp    %r14,%rax
je     11c0 <copy+0x70>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0xdf8(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x2c(%rbp),%r14d
jmp    1271 <copy+0x121>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1271 <copy+0x121>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%edx
lea    0xde4(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1269 <copy+0x119>
mov    0x2d62(%rip),%rax
mov    (%rax),%r14d
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [48 x i8] c"ERROR: Failure while copying file, errno = %d.\0A\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.3 = [69 x i8] c"ERROR: Unable to open destination filepath for writing, errno = %d.\0A\00"
@.str.4 = [64 x i8] c"ERROR: Unable to open source filepath for reading, errno = %d.\0A\00"
define i32 @copy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = load i32, i32* @EXIT_FAILURE
store i32 %11, i32* %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** %3
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %9
%18 = load i32*, i32** %9
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %53
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %10
%23 = load i32*, i32** %10
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %46
25: ; preds = %20
br label %26
26: ; preds = %41, %25
%27 = trunc i64 %13 to i32
%28 = load i32*, i32** %9
%29 = call i64 @fread(i8* %15, i32 1, i32 %27, i32* %28)
store i64 %29, i64* %8
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %42
31: ; preds = %26
%32 = load i64, i64* %8
%33 = load i32*, i32** %10
%34 = call i64 @fwrite(i8* %15, i32 1, i64 %32, i32* %33)
%35 = load i64, i64* %8
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %41
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %58
41: ; preds = %31
br label %26
42: ; preds = %26
%43 = load i32, i32* @EXIT_SUCCESS
store i32 %43, i32* %5
%44 = load i32*, i32** %10
%45 = call i32 @fclose(i32* %44)
br label %50
46: ; preds = %20
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %46, %42
%51 = load i32*, i32** %9
%52 = call i32 @fclose(i32* %51)
br label %57
53: ; preds = %2
%54 = load i32, i32* @stderr
%55 = load i32, i32* @errno
%56 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %53, %50
br label %58
58: ; preds = %57, %37
%59 = load i32, i32* %5
%60 = load i8*, i8** %6
ret i32 %59
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [48 x i8] c"ERROR: Failure while copying file, errno = %d.\0A\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.3 = [69 x i8] c"ERROR: Unable to open destination filepath for writing, errno = %d.\0A\00"
@.str.4 = [64 x i8] c"ERROR: Unable to open source filepath for reading, errno = %d.\0A\00"
define i32 @copy(i8* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* @EXIT_FAILURE
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %32, label %9
9: ; preds = %2
%10 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %25, label %12
12: ; preds = %9, %15
%13 = call i64 @fread(i8* %6, i32 1, i32 %4, i32* %7)
%14 = icmp eq i64 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %12
%16 = call i64 @fwrite(i8* %6, i32 1, i64 %13, i32* %10)
%17 = icmp eq i64 %16, %13
br i1 %17, label %12, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %36
22: ; preds = %12
%23 = load i32, i32* @EXIT_SUCCESS
%24 = tail call i32 @fclose(i32* %10)
br label %29
25: ; preds = %9
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %22
%30 = phi i32 [ %23, %22 ], [ %3, %25 ]
%31 = tail call i32 @fclose(i32* %7)
br label %36
32: ; preds = %2
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %32, %18, %29
%37 = phi i32 [ %3, %32 ], [ %3, %18 ], [ %30, %29 ]
ret i32 %37
}