#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *calloc(int /*<<< orphan*/, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char const *, struct stat *);
int cp(const char *src, const char *dest) {
  struct stat st;
  int r;
  if (stat(src, &st) == 0) {
    char *buf = calloc(st.st_size, 1);
    if (buf) {
      FILE *fp = fopen(src, "r");
      if (fp) {
        r = fread(buf, 1, st.st_size, fp);
        fclose(fp);
        if (r > 0 && (fp = fopen(dest, "w"))) {
          fwrite(buf, r, 1, fp);
          fclose(fp);
          return 1;
        } else {
          perror("fopen dest");
        }
      } else {
        perror("fopen src");
      }
      free(buf);
    } else {
      perror("calloc");
    }
  } else {
    perror("stat");
  }
  return 0;
}


x86 assembly (clang, -O0):
<cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    1291 <cp+0x111>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1280 <cp+0x100>
mov    -0x10(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1266 <cp+0xe6>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x24(%rbp)
jle    1255 <cp+0xd5>
mov    -0x18(%rbp),%rdi
lea    0xdea(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1255 <cp+0xd5>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a4 <cp+0x124>
lea    0xda8(%rip),%rdi
call   1090 <perror@plt>
jmp    1272 <cp+0xf2>
lea    0xda2(%rip),%rdi
call   1090 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    128c <cp+0x10c>
lea    0xd92(%rip),%rdi
call   1090 <perror@plt>
jmp    129d <cp+0x11d>
lea    0xd88(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    1238 <cp+0xb8>
mov    0x8(%rsp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     1241 <cp+0xc1>
mov    %rax,%r15
lea    0xe3a(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     124f <cp+0xcf>
mov    %rax,%rbp
mov    0x8(%rsp),%edx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1050 <fclose@plt>
lea    0xe09(%rip),%r12
test   %ebx,%ebx
jle    1256 <cp+0xd6>
lea    0xdfc(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1256 <cp+0xd6>
mov    %rax,%rbp
mov    $0x1,%r14d
mov    %r15,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
jmp    1269 <cp+0xe9>
lea    0xde1(%rip),%rdi
jmp    1248 <cp+0xc8>
lea    0xdd1(%rip),%rdi
call   1090 <perror@plt>
jmp    1266 <cp+0xe6>
lea    0xdb9(%rip),%r12
mov    %r12,%rdi
call   1090 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"fopen dest\00"
@.str.3 = [10 x i8] c"fopen src\00"
@.str.4 = [7 x i8] c"calloc\00"
@.str.5 = [5 x i8] c"stat\00"
define i32 @cp(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i64 @stat(i8* %10, %struct.stat* %6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %56
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i8* @calloc(i32 %15, i32 1)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %53
19: ; preds = %13
%20 = load i8*, i8** %4
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %9
%22 = load i32*, i32** %9
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %48
24: ; preds = %19
%25 = load i8*, i8** %8
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32*, i32** %9
%29 = call i32 @fread(i8* %25, i32 1, i32 %27, i32* %28)
store i32 %29, i32* %7
%30 = load i32*, i32** %9
%31 = call i32 @fclose(i32* %30)
%32 = load i32, i32* %7
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %45
34: ; preds = %24
%35 = load i8*, i8** %5
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %36, i32** %9
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %45
38: ; preds = %34
%39 = load i8*, i8** %8
%40 = load i32, i32* %7
%41 = load i32*, i32** %9
%42 = call i32 @fwrite(i8* %39, i32 %40, i32 1, i32* %41)
%43 = load i32*, i32** %9
%44 = call i32 @fclose(i32* %43)
store i32 1, i32* %3
br label %59
45: ; preds = %34, %24
%46 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %45
br label %50
48: ; preds = %19
%49 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %47
%51 = load i8*, i8** %8
%52 = call i32 @free(i8* %51)
br label %55
53: ; preds = %13
%54 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
br label %58
56: ; preds = %2
%57 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %56, %55
store i32 0, i32* %3
br label %59
59: ; preds = %58, %38
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"fopen dest\00"
@.str.3 = [10 x i8] c"fopen src\00"
@.str.4 = [7 x i8] c"calloc\00"
@.str.5 = [5 x i8] c"stat\00"
define i32 @cp(i8* %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %32
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i8* @calloc(i32 %9, i32 1)
%11 = icmp eq i8* %10, null
br i1 %11, label %30, label %12
12: ; preds = %7
%13 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = load i32, i32* %8
%17 = call i32 @fread(i8* %10, i32 1, i32 %16, i32* %13)
%18 = call i32 @fclose(i32* %13)
%19 = icmp sgt i32 %17, 0
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = call i32 @fwrite(i8* %10, i32 %17, i32 1, i32* %21)
%25 = call i32 @fclose(i32* %21)
br label %34
26: ; preds = %12, %15, %20
%27 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %20 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %15 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %12 ]
%28 = call i32 @perror(i8* %27)
%29 = call i32 @free(i8* %10)
br label %34
30: ; preds = %7
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %34
32: ; preds = %2
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32, %30, %26, %23
%35 = phi i32 [ 1, %23 ], [ 0, %26 ], [ 0, %30 ], [ 0, %32 ]
ret i32 %35
}