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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ cannot(char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char **, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int fwrite(char **, int, int, int /*<<< orphan*/ *);
long copy(const char *name, FILE *out, const char *outname) {
  FILE *f;
  long size;
  f = fopen(name, "rb");
  if (f == NULL)
    cannot("open", name);
  if (fseek(f, 0, SEEK_END) != 0)
    cannot("seek", name);
  size = ftell(f);
  if (fseek(f, 0, SEEK_SET) != 0)
    cannot("seek", name);
  for (;;) {
    char b[BUFSIZ];
    int n = fread(&b, 1, sizeof(b), f);
    if (n == 0) {
      if (ferror(f))
        cannot("read", name);
      else
        break;
    }
    if (fwrite(&b, n, 1, out) != 1)
      cannot("write", outname);
  }
  fclose(f);
  return size;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c3 <copy+0x43>
mov    -0x8(%rbp),%rsi
lea    0xe45(%rip),%rdi
call   1060 <cannot@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     11f1 <copy+0x71>
mov    -0x8(%rbp),%rsi
lea    0xe1c(%rip),%rdi
call   1060 <cannot@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     122c <copy+0xac>
mov    -0x8(%rbp),%rsi
lea    0xde1(%rip),%rdi
call   1060 <cannot@plt>
jmp    1231 <copy+0xb1>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    12b3 <copy+0x133>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12a2 <copy+0x122>
mov    -0x8(%rbp),%rsi
lea    0xd75(%rip),%rdi
call   1060 <cannot@plt>
jmp    12ae <copy+0x12e>
movl   $0x2,-0x40(%rbp)
jmp    12e8 <copy+0x168>
jmp    12b3 <copy+0x133>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
je     12e1 <copy+0x161>
mov    -0x18(%rbp),%rsi
lea    0xd36(%rip),%rdi
call   1060 <cannot@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     1304 <copy+0x184>
jmp    12ff <copy+0x17f>
jmp    1309 <copy+0x189>
jmp    1231 <copy+0xb1>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x40(%rbp)
mov    %rsi,%r12
lea    0xe61(%rip),%rsi
mov    %rdi,-0x30(%rbp)
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c0 <copy+0x40>
lea    0xe4c(%rip),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <cannot@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     11e8 <copy+0x68>
lea    0xe29(%rip),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <cannot@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     1223 <copy+0xa3>
lea    0xdf5(%rip),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <cannot@plt>
jmp    1223 <copy+0xa3>
xchg   %ax,%ax
mov    %r14,%rsp
mov    %rsp,%r14
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r15
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,%r13d
test   %eax,%eax
jne    1277 <copy+0xf7>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     12a4 <copy+0x124>
lea    0xd9f(%rip),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <cannot@plt>
mov    %r15,%rdi
mov    %r13d,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
je     1220 <copy+0xa0>
lea    0xd7c(%rip),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <cannot@plt>
jmp    1220 <copy+0xa0>
mov    %r14,%rsp
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rax
lea    -0x28(%rbp),%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 = [5 x i8] c"open\00"
@SEEK_END = global i32 0
@.str.2 = [5 x i8] c"seek\00"
@SEEK_SET = global i32 0
@BUFSIZ = global i32 0
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [6 x i8] c"write\00"
define i64 @copy(i8* %0, i32* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%13 = load i8*, i8** %4
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i8*, i8** %4
%19 = call i32 @cannot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %3
%21 = load i32*, i32** %7
%22 = load i32, i32* @SEEK_END
%23 = call i64 @fseek(i32* %21, i32 0, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = call i32 @cannot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25, %20
%29 = load i32*, i32** %7
%30 = call i64 @ftell(i32* %29)
store i64 %30, i64* %8
%31 = load i32*, i32** %7
%32 = load i32, i32* @SEEK_SET
%33 = call i64 @fseek(i32* %31, i32 0, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i8*, i8** %4
%37 = call i32 @cannot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %28
br label %39
39: ; preds = %72, %38
%40 = load i32, i32* @BUFSIZ
%41 = zext i32 %40 to i64
%42 = call i8* @llvm.stacksave()
store i8* %42, i8** %9
%43 = alloca i8, i64 %41
store i64 %41, i64* %10
%44 = bitcast i8* %43 to i8**
%45 = trunc i64 %41 to i32
%46 = load i32*, i32** %7
%47 = call i32 @fread(i8** %44, i32 1, i32 %45, i32* %46)
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %39
%51 = load i32*, i32** %7
%52 = call i64 @ferror(i32* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i8*, i8** %4
%56 = call i32 @cannot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %55)
br label %58
57: ; preds = %50
store i32 2, i32* %12
br label %69
58: ; preds = %54
br label %59
59: ; preds = %58, %39
%60 = bitcast i8* %43 to i8**
%61 = load i32, i32* %11
%62 = load i32*, i32** %5
%63 = call i32 @fwrite(i8** %60, i32 %61, i32 1, i32* %62)
%64 = icmp ne i32 %63, 1
br i1 %64, label %65, label %68
65: ; preds = %59
%66 = load i8*, i8** %6
%67 = call i32 @cannot(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %66)
br label %68
68: ; preds = %65, %59
store i32 0, i32* %12
br label %69
69: ; preds = %68, %57
%70 = load i8*, i8** %9
%71 = load i32, i32* %12
switch i32 %71, label %77 [
i32 0, label %72
i32 2, label %73
]
72: ; preds = %69
br label %39
73: ; preds = %69
%74 = load i32*, i32** %7
%75 = call i32 @fclose(i32* %74)
%76 = load i64, i64* %8
ret i64 %76
77: ; preds = %69
unreachable
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [5 x i8] c"open\00"
@SEEK_END = global i32 0
@.str.2 = [5 x i8] c"seek\00"
@SEEK_SET = global i32 0
@BUFSIZ = global i32 0
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [6 x i8] c"write\00"
define i64 @copy(i8* %0, i32* nocapture %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @cannot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i64 @fseek(i32* %4, i32 0, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @cannot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %14
14: ; preds = %12, %8
%15 = tail call i64 @ftell(i32* %4)
%16 = load i32, i32* @SEEK_SET
%17 = tail call i64 @fseek(i32* %4, i32 0, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = tail call i32 @cannot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %19, %14
br label %22
22: ; preds = %21, %40
%23 = load i32, i32* @BUFSIZ
%24 = zext i32 %23 to i64
%25 = tail call i8* @llvm.stacksave()
%26 = alloca i8, i64 %24
%27 = bitcast i8* %26 to i8**
%28 = call i32 @fread(i8** %27, i32 1, i32 %23, i32* %4)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %22
%31 = tail call i64 @ferror(i32* %4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %41, label %33
33: ; preds = %30
%34 = tail call i32 @cannot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %35
35: ; preds = %33, %22
%36 = call i32 @fwrite(i8** %27, i32 %28, i32 1, i32* %1)
%37 = icmp eq i32 %36, 1
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = tail call i32 @cannot(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %40
40: ; preds = %35, %38
tail call void @llvm.stackrestore(i8* %25)
br label %22
41: ; preds = %30
tail call void @llvm.stackrestore(i8* %25)
%42 = tail call i32 @fclose(i32* %4)
ret i64 %15
}