#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;

/* Variables and functions */
int EXIT_FAILURE;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, int);
int write(int /*<<< orphan*/, char *, size_t);
void cat(int fd, int *err) {
  char buf[4096];
  size_t n;
  while (1) {
    if ((n = read(fd, buf, sizeof(buf))) == -1) {
      perror("cat: read");
      *err = EXIT_FAILURE;
      return;
    }
    if (n == 0) {
      return;
    }
    if (write(STDOUT_FILENO, buf, n) == -1) {
      perror("cat: write");
      *err = EXIT_FAILURE;
      return;
    }
  }
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    1187 <cat+0x57>
lea    0xe92(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11e4 <cat+0xb4>
cmpq   $0x0,-0x1018(%rbp)
jne    119a <cat+0x6a>
jmp    11e4 <cat+0xb4>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11df <cat+0xaf>
lea    0xe44(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11e4 <cat+0xb4>
jmp    1142 <cat+0x12>
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    %edi,%ebp
mov    %rsp,%rbx
lea    0xeb0(%rip),%r15
mov    0x2e99(%rip),%r13
lea    0xeac(%rip),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %rax,%rax
je     11a5 <cat+0x75>
cmp    $0xffffffffffffffff,%rax
je     1191 <cat+0x61>
mov    0x0(%r13),%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1160 <cat+0x30>
mov    %r12,%r15
mov    %r15,%rdi
call   1050 <perror@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"cat: read\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [11 x i8] c"cat: write\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, %30
%8 = load i32, i32* %3
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%10 = call i64 @read(i32 %8, i8* %9, i32 4096)
store i64 %10, i64* %6
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = load i32*, i32** %4
store i32 %14, i32* %15
br label %31
16: ; preds = %7
%17 = load i64, i64* %6
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
br label %31
20: ; preds = %16
%21 = load i32, i32* @STDOUT_FILENO
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%23 = load i64, i64* %6
%24 = call i32 @write(i32 %21, i8* %22, i64 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = load i32*, i32** %4
store i32 %28, i32* %29
br label %31
30: ; preds = %20
br label %7
31: ; preds = %26, %19, %12
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"cat: read\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [11 x i8] c"cat: write\00"
define void @cat(i32 %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 = %7, %2
%6 = call i64 @read(i32 %0, i8* %4, i32 4096)
switch i64 %6, label %7 [
i64 -1, label %11
i64 0, label %15
]
7: ; preds = %5
%8 = load i32, i32* @STDOUT_FILENO
%9 = call i32 @write(i32 %8, i8* %4, i64 %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %5
11: ; preds = %7, %5
%12 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %7 ]
%13 = tail call i32 @perror(i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %1
br label %15
15: ; preds = %5, %11
ret void
}