#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 copy_file_internal(int /*<<< orphan*/ *const, int /*<<< orphan*/ *const);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *const);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ remove(char const *);
int copy_file(const char src[], const char dst[]) {
  FILE *const src_fp = fopen(src, "rb");
  FILE *const dst_fp = fopen(dst, "wb");
  int result;
  result = copy_file_internal(src_fp, dst_fp);
  if (dst_fp != NULL) {
    (void)fclose(dst_fp);
  }
  if (src_fp != NULL) {
    (void)fclose(src_fp);
  }
  if (result != 0) {
    (void)remove(dst);
  }
  return result;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <copy_file_internal@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     119c <copy_file+0x5c>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     11b0 <copy_file+0x70>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x24(%rbp)
je     11c3 <copy_file+0x83>
mov    -0x10(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xea3(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <copy_file_internal@plt>
mov    %eax,%r15d
test   %rbp,%rbp
je     1186 <copy_file+0x46>
mov    %rbp,%rdi
call   1040 <fclose@plt>
test   %rbx,%rbx
je     1193 <copy_file+0x53>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r15d,%r15d
je     11a0 <copy_file+0x60>
mov    %r14,%rdi
call   1030 <remove@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @copy_file(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 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @copy_file_internal(i32* %3, i32* %4)
%6 = icmp eq i32* %4, null
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @fclose(i32* %4)
br label %9
9: ; preds = %7, %2
%10 = icmp eq i32* %3, null
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = tail call i32 @fclose(i32* %3)
br label %13
13: ; preds = %11, %9
%14 = icmp eq i32 %5, 0
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = tail call i32 @remove(i8* %1)
br label %17
17: ; preds = %15, %13
ret i32 %5
}