#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *);
int _copy(char *path, char *source) {
  char buf[2048];
  size_t bs = sizeof(buf);
  int i = 0;
  FILE *s, *t;
  errno = 0;
  if ((s = fopen(source, "r")) == NULL) {
    return -errno;
  }
  if ((t = fopen(path, "w+")) == NULL) {
    fclose(s);
    return -errno;
  }
  while (fgets(buf, bs, s) != NULL) {
    if (fprintf(t, buf) == -1) {
      i = -errno;
      goto cleanup;
    }
  }
  goto cleanup;
cleanup:
  fclose(s);
  fclose(t);
  return i;
}


x86 assembly (clang, -O0):
<_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x800,-0x828(%rbp)
movl   $0x0,-0x82c(%rbp)
mov    0x2e71(%rip),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    0xe80(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x838(%rbp)
cmp    $0x0,%rax
jne    11a9 <_copy+0x69>
xor    %eax,%eax
mov    0x2e41(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <_copy+0x12f>
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x840(%rbp)
cmp    $0x0,%rax
jne    11e9 <_copy+0xa9>
mov    -0x838(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2e01(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <_copy+0x12f>
jmp    11ee <_copy+0xae>
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rsi
mov    -0x838(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1249 <_copy+0x109>
mov    -0x840(%rbp),%rdi
lea    -0x820(%rbp),%rsi
call   1050 <fprintf@plt>
cmp    $0xffffffff,%eax
jne    1244 <_copy+0x104>
xor    %eax,%eax
mov    0x2da9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x82c(%rbp)
jmp    124e <_copy+0x10e>
jmp    11ee <_copy+0xae>
jmp    124e <_copy+0x10e>
mov    -0x838(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x840(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x82c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rsi,%rax
mov    %rdi,%rbp
mov    0x2e86(%rip),%r15
movl   $0x0,(%r15)
lea    0xe98(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d4 <_copy+0x94>
mov    %rax,%rbx
lea    0xe83(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11cc <_copy+0x8c>
mov    %rax,%r14
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x800,%esi
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11db <_copy+0x9b>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1050 <fprintf@plt>
cmp    $0xffffffff,%eax
jne    11a0 <_copy+0x60>
xor    %ebp,%ebp
sub    (%r15),%ebp
jmp    11dd <_copy+0x9d>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebp,%ebp
sub    (%r15),%ebp
jmp    11ed <_copy+0xad>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"w+\00"
define i32 @_copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [2048 x i8]
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 2048, i64* %7
store i32 0, i32* %8
store i32 0, i32* @errno
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %9
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
store i32 %16, i32* %3
br label %49
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %10
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32*, i32** %9
%23 = call i32 @fclose(i32* %22)
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
store i32 %25, i32* %3
br label %49
26: ; preds = %17
br label %27
27: ; preds = %41, %26
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%29 = load i64, i64* %7
%30 = load i32*, i32** %9
%31 = call i32* @fgets(i8* %28, i64 %29, i32* %30)
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %42
33: ; preds = %27
%34 = load i32*, i32** %10
%35 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%36 = call i32 @fprintf(i32* %34, i8* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* @errno
%40 = sub nsw i32 0, %39
store i32 %40, i32* %8
br label %43
41: ; preds = %33
br label %27
42: ; preds = %27
br label %43
43: ; preds = %42, %38
%44 = load i32*, i32** %9
%45 = call i32 @fclose(i32* %44)
%46 = load i32*, i32** %10
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %8
store i32 %48, i32* %3
br label %49
49: ; preds = %43, %21, %14
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"w+\00"
define i32 @_copy(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
store i32 0, i32* @errno
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %30
10: ; preds = %2
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @fclose(i32* %5)
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
br label %30
17: ; preds = %10, %20
%18 = call i32* @fgets(i8* %4, i64 2048, i32* %5)
%19 = icmp eq i32* %18, null
br i1 %19, label %26, label %20
20: ; preds = %17
%21 = call i32 @fprintf(i32* %11, i8* %4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %17
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
br label %26
26: ; preds = %17, %23
%27 = phi i32 [ %25, %23 ], [ 0, %17 ]
%28 = call i32 @fclose(i32* %5)
%29 = call i32 @fclose(i32* %11)
br label %30
30: ; preds = %26, %13, %7
%31 = phi i32 [ %9, %7 ], [ %16, %13 ], [ %27, %26 ]
ret i32 %31
}