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

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, char *, char *);
scalar_t__ read(int, char *, int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, scalar_t__);
int cat(int fd, char *filename) {
  char buf[4096];
  ssize_t nread;
  do {
    nread = read(fd, buf, sizeof(buf));
    if (nread < 0) {
      printf("cat: read error on %s: %s\n", filename, strerror(errno));
      return 1;
    }
    write(STDOUT_FILENO, buf, nread);
  } while (nread);
  return 0;
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jge    11b6 <cat+0x76>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1020(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1020(%rbp),%rsi
mov    %rax,%rdx
lea    0xe5b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e7 <cat+0xa7>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%rdx
call   1030 <write@plt>
cmpq   $0x0,-0x1018(%rbp)
jne    1152 <cat+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%r15
mov    0x2e92(%rip),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
js     118f <cat+0x4f>
mov    %rax,%rbx
mov    (%r12),%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
test   %rbx,%rbx
jne    1160 <cat+0x20>
xor    %eax,%eax
jmp    11b4 <cat+0x74>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe5c(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"cat: read error on %s: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @cat(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [4096 x i8]
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
br label %8
8: ; preds = %24, %2
%9 = load i32, i32* %4
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%11 = call i64 @read(i32 %9, i8* %10, i32 4096)
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %8
%15 = load i8*, i8** %5
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
store i32 1, i32* %3
br label %28
19: ; preds = %8
%20 = load i32, i32* @STDOUT_FILENO
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%22 = load i64, i64* %7
%23 = call i32 @write(i32 %20, i8* %21, i64 %22)
br label %24
24: ; preds = %19
%25 = load i64, i64* %7
%26 = icmp ne i64 %25, 0
br i1 %26, label %8, label %27
27: ; preds = %24
store i32 0, i32* %3
br label %28
28: ; preds = %27, %14
%29 = load i32, i32* %3
ret i32 %29
}

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