#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*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
void copy_file(const char *infile, const char *outfile) {
  FILE *in_fd = fopen(infile, "r");
  assert(in_fd != NULL);
  FILE *out_fd = fopen(outfile, "w");
  assert(out_fd != NULL);
  int num_read_bytes = 0;
  int num_write_bytes = 0;
  char buf[4096];
  while (!feof(in_fd)) {
    num_read_bytes = fread(buf, 1, 4096, in_fd);
    num_write_bytes = fwrite(buf, 1, num_read_bytes, out_fd);
    assert(num_read_bytes == num_write_bytes);
  }
  fclose(in_fd);
  fclose(out_fd);
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11eb <copy_file+0x8b>
jmp    123b <copy_file+0xdb>
lea    -0x1030(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
lea    -0x1030(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
jmp    11cd <copy_file+0x6d>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
lea    0xe6e(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    1202 <copy_file+0xa2>
mov    %rsp,%r15
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
xor    %edi,%edi
cmp    %eax,%ebp
sete   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11c0 <copy_file+0x60>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
define void @copy_file(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp ne i32* %4, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp ne i32* %8, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%13 = tail call i32 @feof(i32* %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %2, %15
%16 = call i32 @fread(i8* %12, i32 1, i32 4096, i32* %4)
%17 = call i32 @fwrite(i8* %12, i32 1, i32 %16, i32* %8)
%18 = icmp eq i32 %16, %17
%19 = zext i1 %18 to i32
%20 = tail call i32 @assert(i32 %19)
%21 = tail call i32 @feof(i32* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %15, label %23
23: ; preds = %15, %2
%24 = tail call i32 @fclose(i32* %4)
%25 = tail call i32 @fclose(i32* %8)
ret void
}