#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*/ dbgprintf(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void copy_file(const char *from, const char *to) {
  enum { bufsize = 16 };
  char buf[bufsize];
  size_t n;
  FILE *fin, *fout;
  if ((fin = fopen(from, "r")) == NULL) {
    dbgprintf("\"%s\": %s\n", from, strerror(errno));
    return;
  }
  if ((fout = fopen(to, "w")) == NULL) {
    dbgprintf("\"%s\": %s\n", to, strerror(errno));
    fclose(fin);
    return;
  }
  while ((n = fread(buf, 1, bufsize, fin)) > 0)
    fwrite(buf, 1, n, fout);
  fclose(fin);
  fclose(fout);
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11bb <copy_file+0x5b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xe51(%rip),%rdi
call   1050 <dbgprintf@plt>
jmp    1266 <copy_file+0x106>
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    120f <copy_file+0xaf>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xe06(%rip),%rdi
call   1050 <dbgprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1266 <copy_file+0x106>
jmp    1214 <copy_file+0xb4>
lea    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jbe    1254 <copy_file+0xf4>
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
jmp    1214 <copy_file+0xb4>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe88(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11fa <copy_file+0x9a>
mov    %rax,%r14
lea    0xe80(%rip),%rsi
mov    %r15,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1224 <copy_file+0xc4>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11ed <copy_file+0x8d>
mov    %rsp,%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r12,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11c0 <copy_file+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
jmp    1246 <copy_file+0xe6>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf3(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <dbgprintf@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdc9(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1050 <dbgprintf@plt>
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [10 x i8] c"\22%s\22: %s\0A\00"
@errno = global i32 0
@.str.2 = [2 x i8] c"w\00"
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [16 x i8]
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 @dbgprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 %15)
br label %44
17: ; preds = %2
%18 = load i8*, i8** %4
%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** %8
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = call i32 @dbgprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %22, i32 %24)
%26 = load i32*, i32** %7
%27 = call i32 @fclose(i32* %26)
br label %44
28: ; preds = %17
br label %29
29: ; preds = %34, %28
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%31 = load i32*, i32** %7
%32 = call i64 @fread(i8* %30, i32 1, i32 16, i32* %31)
store i64 %32, i64* %6
%33 = icmp ugt i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%36 = load i64, i64* %6
%37 = load i32*, i32** %8
%38 = call i32 @fwrite(i8* %35, i32 1, i64 %36, i32* %37)
br label %29
39: ; preds = %29
%40 = load i32*, i32** %7
%41 = call i32 @fclose(i32* %40)
%42 = load i32*, i32** %8
%43 = call i32 @fclose(i32* %42)
br label %44
44: ; preds = %39, %21, %12
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [10 x i8] c"\22%s\22: %s\0A\00"
@errno = global i32 0
@.str.2 = [2 x i8] c"w\00"
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 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 %11
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @dbgprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %9)
br label %30
11: ; preds = %2
%12 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = call i64 @fread(i8* %4, i32 1, i32 16, i32* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %27, label %22
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = tail call i32 @strerror(i32 %18)
%20 = tail call i32 @dbgprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %19)
%21 = tail call i32 @fclose(i32* %5)
br label %30
22: ; preds = %14, %22
%23 = phi i64 [ %25, %22 ], [ %15, %14 ]
%24 = call i32 @fwrite(i8* %4, i32 1, i64 %23, i32* %12)
%25 = call i64 @fread(i8* %4, i32 1, i32 16, i32* %5)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %22
27: ; preds = %22, %14
%28 = tail call i32 @fclose(i32* %5)
%29 = tail call i32 @fclose(i32* %12)
br label %30
30: ; preds = %27, %17, %7
ret void
}