#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*/ *fopen(char *, char *);
int fread(char **, int, int, int /*<<< orphan*/ *);
int fwrite(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink(char *);
int common_copy(char *src, char *dest) {
  FILE *sf, *df;
  char buf[4096];
  int len, ret = 1;
  sf = fopen(src, "rb");
  if (!sf)
    return 0;
  df = fopen(dest, "wb");
  if (!df) {
    fclose(sf);
    return 0;
  }
  while ((len = fread(&buf, 1, 4096, sf)) > 0) {
    if (fwrite(&buf, 1, len, df) != len) {
      ret = 0;
      break;
    }
  }
  fclose(sf);
  fclose(df);
  if (!ret)
    unlink(dest);
  return ret;
}


x86 assembly (clang, -O0):
<common_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x1038(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe88(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1198 <common_copy+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1266 <common_copy+0x116>
mov    -0x18(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cc <common_copy+0x7c>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1266 <common_copy+0x116>
jmp    11d1 <common_copy+0x81>
lea    -0x1030(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %eax,-0x1034(%rbp)
cmp    $0x0,%eax
jle    1235 <common_copy+0xe5>
lea    -0x1030(%rbp),%rdi
mov    -0x1034(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
cmp    -0x1034(%rbp),%eax
je     1230 <common_copy+0xe0>
movl   $0x0,-0x1038(%rbp)
jmp    1235 <common_copy+0xe5>
jmp    11d1 <common_copy+0x81>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x1038(%rbp)
jne    125d <common_copy+0x10d>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x1038(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<common_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     11fa <common_copy+0xaa>
mov    %rax,%r13
lea    0xe7d(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f2 <common_copy+0xa2>
mov    %rax,%r12
mov    %rsp,%rbx
mov    $0x1,%r15d
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11d4 <common_copy+0x84>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1070 <fwrite@plt>
cmp    %ebp,%eax
je     11a0 <common_copy+0x50>
xor    %r15d,%r15d
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
test   %ebp,%ebp
jle    11fa <common_copy+0xaa>
mov    %r14,%rdi
call   1030 <unlink@plt>
jmp    11fa <common_copy+0xaa>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r15d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 @common_copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [4096 x i8]
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 1, i32* %10
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i32 0, i32* %3
br label %51
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32*, i32** %6
%23 = call i32 @fclose(i32* %22)
store i32 0, i32* %3
br label %51
24: ; preds = %16
br label %25
25: ; preds = %38, %24
%26 = bitcast [4096 x i8]* %8 to i8**
%27 = load i32*, i32** %6
%28 = call i32 @fread(i8** %26, i32 1, i32 4096, i32* %27)
store i32 %28, i32* %9
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %25
%31 = bitcast [4096 x i8]* %8 to i8**
%32 = load i32, i32* %9
%33 = load i32*, i32** %7
%34 = call i32 @fwrite(i8** %31, i32 1, i32 %32, i32* %33)
%35 = load i32, i32* %9
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %30
store i32 0, i32* %10
br label %39
38: ; preds = %30
br label %25
39: ; preds = %37, %25
%40 = load i32*, i32** %6
%41 = call i32 @fclose(i32* %40)
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %10
%45 = icmp ne i32 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %39
%47 = load i8*, i8** %5
%48 = call i32 @unlink(i8* %47)
br label %49
49: ; preds = %46, %39
%50 = load i32, i32* %10
store i32 %50, i32* %3
br label %51
51: ; preds = %49, %21, %15
%52 = load i32, i32* %3
ret i32 %52
}

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