#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 EXIT_FAILURE;
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdout;
void cat(FILE *f, int *err) {
  char buf[4096];
  size_t n;
  while (1) {
    n = fread(buf, 1, sizeof(buf), f);
    if (n == 0 && ferror(f)) {
      perror("cat: fread");
      *err = EXIT_FAILURE;
      return;
    }
    if (n == 0 && feof(f)) {
      return;
    }
    if (fwrite(buf, 1, n, stdout) < n) {
      perror("cat: fwrite");
      *err = EXIT_FAILURE;
      return;
    }
  }
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jne    11c5 <cat+0x75>
mov    -0x8(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11c5 <cat+0x75>
lea    0xe54(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    123e <cat+0xee>
cmpq   $0x0,-0x1018(%rbp)
jne    11eb <cat+0x9b>
mov    -0x8(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11eb <cat+0x9b>
jmp    123e <cat+0xee>
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rdx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1070 <fwrite@plt>
cmp    -0x1018(%rbp),%rax
jae    1239 <cat+0xe9>
lea    0xdeb(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    123e <cat+0xee>
jmp    1163 <cat+0x13>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
mov    %rsp,%r12
lea    0xe8f(%rip),%r15
mov    0x2e60(%rip),%rbp
jmp    1198 <cat+0x48>
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp),%ecx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1070 <fwrite@plt>
cmp    %rbx,%rax
jb     11e1 <cat+0x91>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1180 <cat+0x30>
mov    %r13,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11e8 <cat+0x98>
mov    %r13,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     1180 <cat+0x30>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe23(%rip),%r15
mov    %r15,%rdi
call   1060 <perror@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
jmp    11cf <cat+0x7f>

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

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