#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 */
scalar_t__ EOF;
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int copy_file_to_stdout(char *file) {
  int readcnt, writecnt;
  char buffer[1000];
  FILE *stream = fopen(file, "r");
  if (stream == NULL)
    return 1;
  while (1) {
    readcnt = fread(buffer, 1, 1000, stream);
    if (readcnt == 0) {
      if (feof(stream))
        return 0;
      else if (ferror(stream))
        return 2;
      else
        return 3;
    }
    writecnt = fwrite(buffer, 1, readcnt, stdout);
    if (writecnt != readcnt)
      return 4;
  }
  if (fclose(stream) == EOF)
    return 5;
}


x86 assembly (clang, -O0):
<copy_file_to_stdout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x408(%rbp)
cmpq   $0x0,-0x408(%rbp)
jne    1190 <copy_file_to_stdout+0x40>
movl   $0x1,-0x4(%rbp)
jmp    124c <copy_file_to_stdout+0xfc>
jmp    1195 <copy_file_to_stdout+0x45>
lea    -0x400(%rbp),%rdi
mov    -0x408(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1040 <fread@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    120f <copy_file_to_stdout+0xbf>
mov    -0x408(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11e1 <copy_file_to_stdout+0x91>
movl   $0x0,-0x4(%rbp)
jmp    124c <copy_file_to_stdout+0xfc>
mov    -0x408(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1203 <copy_file_to_stdout+0xb3>
movl   $0x2,-0x4(%rbp)
jmp    124c <copy_file_to_stdout+0xfc>
movl   $0x3,-0x4(%rbp)
jmp    124c <copy_file_to_stdout+0xfc>
lea    -0x400(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1247 <copy_file_to_stdout+0xf7>
movl   $0x4,-0x4(%rbp)
jmp    124c <copy_file_to_stdout+0xfc>
jmp    1195 <copy_file_to_stdout+0x45>
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file_to_stdout>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x3e8,%rsp
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b8 <copy_file_to_stdout+0x68>
mov    %rax,%r14
mov    %rsp,%rbx
mov    0x2e65(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     11bf <copy_file_to_stdout+0x6f>
mov    %eax,%ebp
mov    (%r15),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   1070 <fwrite@plt>
cmp    %ebp,%eax
je     1180 <copy_file_to_stdout+0x30>
mov    $0x4,%eax
jmp    11e7 <copy_file_to_stdout+0x97>
mov    $0x1,%eax
jmp    11e7 <copy_file_to_stdout+0x97>
mov    %r14,%rdi
call   1050 <feof@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11e7 <copy_file_to_stdout+0x97>
mov    %r14,%rdi
call   1030 <ferror@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
or     $0x2,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stdout = global i32 0
@EOF = global i64 0
define i32 @copy_file_to_stdout(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1000 x i8]
%7 = alloca i32*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 1, i32* %2
br label %41
13: ; preds = %1
br label %14
14: ; preds = %13, %40
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%16 = load i32*, i32** %7
%17 = call i32 @fread(i8* %15, i32 1, i32 1000, i32* %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %14
%21 = load i32*, i32** %7
%22 = call i64 @feof(i32* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i32 0, i32* %2
br label %41
25: ; preds = %20
%26 = load i32*, i32** %7
%27 = call i64 @ferror(i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 2, i32* %2
br label %41
30: ; preds = %25
store i32 3, i32* %2
br label %41
31: ; preds = %14
%32 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%33 = load i32, i32* %4
%34 = load i32, i32* @stdout
%35 = call i32 @fwrite(i8* %32, i32 1, i32 %33, i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = load i32, i32* %4
%38 = icmp ne i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %31
store i32 4, i32* %2
br label %41
40: ; preds = %31
br label %14
41: ; preds = %39, %30, %29, %24, %12
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stdout = global i32 0
@EOF = global i64 0
define i32 @copy_file_to_stdout(i8* nocapture %0) {
%2 = alloca [1000 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %20, label %6
6: ; preds = %1, %16
%7 = call i32 @fread(i8* %3, i32 1, i32 1000, i32* %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %6
%10 = call i64 @feof(i32* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = call i64 @ferror(i32* %4)
%14 = icmp eq i64 %13, 0
%15 = select i1 %14, i32 3, i32 2
br label %20
16: ; preds = %6
%17 = load i32, i32* @stdout
%18 = call i32 @fwrite(i8* %3, i32 1, i32 %7, i32 %17)
%19 = icmp eq i32 %18, %7
br i1 %19, label %6, label %20
20: ; preds = %16, %12, %9, %1
%21 = phi i32 [ 1, %1 ], [ 0, %9 ], [ %15, %12 ], [ 4, %16 ]
ret i32 %21
}