#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int file_copy(char *oldname, char *newname) {
  FILE *fold, *fnew;
  int c;
  if ((fold = fopen(oldname, "rb")) == NULL)
    return -1;
  if ((fnew = fopen(newname, "wb")) == NULL) {
    fclose(fold);
    return -1;
  }
  while (1) {
    c = fgetc(fold);
    if (!feof(fold))
      fputc(c, fnew);
    else
      break;
  }
  fclose(fnew);
  fclose(fold);
  return 0;
}


x86 assembly (clang, -O0):
<file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    118a <file_copy+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <file_copy+0xc4>
mov    -0x18(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11bd <file_copy+0x6d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <file_copy+0xc4>
jmp    11c2 <file_copy+0x72>
mov    -0x20(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
jne    11f1 <file_copy+0xa1>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1050 <fputc@plt>
jmp    11f6 <file_copy+0xa6>
jmp    11fb <file_copy+0xab>
jmp    11c2 <file_copy+0x72>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_copy>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11be <file_copy+0x6e>
mov    %rax,%rbx
lea    0xe91(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c5 <file_copy+0x75>
mov    %rax,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11b2 <file_copy+0x62>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
jmp    1190 <file_copy+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %ebp,%ebp
jmp    11ca <file_copy+0x7a>
mov    $0xffffffff,%ebp
jmp    11d2 <file_copy+0x82>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @file_copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %38
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32*, i32** %6
%19 = call i32 @fclose(i32* %18)
store i32 -1, i32* %3
br label %38
20: ; preds = %13
br label %21
21: ; preds = %20, %32
%22 = load i32*, i32** %6
%23 = call i32 @fgetc(i32* %22)
store i32 %23, i32* %8
%24 = load i32*, i32** %6
%25 = call i32 @feof(i32* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %21
%28 = load i32, i32* %8
%29 = load i32*, i32** %7
%30 = call i32 @fputc(i32 %28, i32* %29)
br label %32
31: ; preds = %21
br label %33
32: ; preds = %27
br label %21
33: ; preds = %31
%34 = load i32*, i32** %7
%35 = call i32 @fclose(i32* %34)
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
store i32 0, i32* %3
br label %38
38: ; preds = %33, %17, %12
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @file_copy(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %23, label %5
5: ; preds = %2
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %20, label %8
8: ; preds = %5
%9 = tail call i32 @fgetc(i32* %3)
%10 = tail call i32 @feof(i32* %3)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %8, %12
%13 = phi i32 [ %15, %12 ], [ %9, %8 ]
%14 = tail call i32 @fputc(i32 %13, i32* %6)
%15 = tail call i32 @fgetc(i32* %3)
%16 = tail call i32 @feof(i32* %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %12, label %18
18: ; preds = %12, %8
%19 = tail call i32 @fclose(i32* %6)
br label %20
20: ; preds = %5, %18
%21 = phi i32 [ 0, %18 ], [ -1, %5 ]
%22 = tail call i32 @fclose(i32* %3)
br label %23
23: ; preds = %20, %2
%24 = phi i32 [ -1, %2 ], [ %21, %20 ]
ret i32 %24
}