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

/* Variables and functions */
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stdout;
int cat_fd(int fd) {
  char buf[4096] = {0};
  ssize_t bytes_read = 1;
  while (bytes_read > 0) {
    bytes_read = read(fd, buf, sizeof(buf) - 1);
    buf[bytes_read] = 0;
    fputs(buf, stdout);
  }
  if (bytes_read == -1) {
    perror("cat: error reading file");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cat_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
movq   $0x1,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jbe    11be <cat_fd+0x7e>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0xfff,%edx
call   1050 <read@plt>
mov    %rax,-0x1018(%rbp)
mov    -0x1018(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1030 <fputs@plt>
jmp    116c <cat_fd+0x2c>
cmpq   $0xffffffffffffffff,-0x1018(%rbp)
ff
jne    11e4 <cat_fd+0xa4>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11eb <cat_fd+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %edi,%r14d
mov    %rsp,%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    0x2e87(%rip),%r15
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xfff,%edx
call   1050 <read@plt>
mov    %rax,%rbp
movb   $0x0,(%rsp,%rax,1)
mov    (%r15),%esi
mov    %rbx,%rdi
call   1030 <fputs@plt>
test   %rbp,%rbp
jne    1160 <cat_fd+0x30>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [24 x i8] c"cat: error reading file\00"
define i32 @cat_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca i64
store i32 %0, i32* %3
%6 = bitcast [4096 x i8]* %4 to i8*
store i64 1, i64* %5
br label %7
7: ; preds = %10, %1
%8 = load i64, i64* %5
%9 = icmp ugt i64 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %7
%11 = load i32, i32* %3
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%13 = call i64 @read(i32 %11, i8* %12, i32 4095)
store i64 %13, i64* %5
%14 = load i64, i64* %5
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %14
store i8 0, i8* %15
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* @stdout
%18 = call i32 @fputs(i8* %16, i32 %17)
br label %7
19: ; preds = %7
%20 = load i64, i64* %5
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %25
24: ; preds = %19
store i32 0, i32* %2
br label %25
25: ; preds = %24, %22
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@stdout = global i32 0
define i32 @cat_fd(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %1, %4
%5 = call i64 @read(i32 %0, i8* %3, i32 4095)
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %5
store i8 0, i8* %6
%7 = load i32, i32* @stdout
%8 = call i32 @fputs(i8* %3, i32 %7)
%9 = icmp eq i64 %5, 0
br i1 %9, label %10, label %4
10: ; preds = %4
ret i32 0
}