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

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ read(int, char *, int);
scalar_t__ write(int /*<<< orphan*/, char *, scalar_t__);
bool cat_file(int fd) {
  char buf[1024];
  ssize_t bytes_read;
  while ((bytes_read = read(fd, buf, 1024)) > 0) {
    char *bufPtr = buf;
    do {
      ssize_t bytes_written = write(STDOUT_FILENO, bufPtr, bytes_read);
      if (bytes_written < 0)
        return false;
      bufPtr += bytes_written;
      bytes_read -= bytes_written;
    } while (bytes_read > 0);
  }
  return bytes_read >= 0;
}


x86 assembly (clang, -O0):
<cat_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
jle    11de <cat_file+0xbe>
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    -0x420(%rbp),%rsi
mov    -0x418(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jge    119e <cat_file+0x7e>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <cat_file+0xd1>
mov    -0x428(%rbp),%rax
add    -0x420(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    -0x428(%rbp),%rcx
mov    -0x418(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jg     1161 <cat_file+0x41>
jmp    112e <cat_file+0xe>
cmpq   $0x0,-0x418(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %edi,%r15d
mov    %rsp,%r14
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    119a <cat_file+0x7a>
mov    0x2e9f(%rip),%r12
mov    %r14,%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     11a4 <cat_file+0x84>
sub    %rax,%rbx
add    %rax,%rbp
test   %rbx,%rbx
jg     1160 <cat_file+0x40>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,%rbx
mov    %r14,%rbp
test   %rax,%rax
jg     1160 <cat_file+0x40>
xor    %eax,%eax
test   %rbx,%rbx
sete   %al
jmp    11a6 <cat_file+0x86>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
define i32 @cat_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %3
br label %8
8: ; preds = %33, %1
%9 = load i32, i32* %3
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = call i64 @read(i32 %9, i8* %10, i32 1024)
store i64 %11, i64* %5
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %34
13: ; preds = %8
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8* %14, i8** %6
br label %15
15: ; preds = %30, %13
%16 = load i32, i32* @STDOUT_FILENO
%17 = load i8*, i8** %6
%18 = load i64, i64* %5
%19 = call i64 @write(i32 %16, i8* %17, i64 %18)
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 0, i32* %2
br label %38
23: ; preds = %15
%24 = load i64, i64* %7
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i64 %24
store i8* %26, i8** %6
%27 = load i64, i64* %7
%28 = load i64, i64* %5
%29 = sub nsw i64 %28, %27
store i64 %29, i64* %5
br label %30
30: ; preds = %23
%31 = load i64, i64* %5
%32 = icmp sgt i64 %31, 0
br i1 %32, label %15, label %33
33: ; preds = %30
br label %8
34: ; preds = %8
%35 = load i64, i64* %5
%36 = icmp sge i64 %35, 0
%37 = zext i1 %36 to i32
store i32 %37, i32* %2
br label %38
38: ; preds = %34, %22
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
define i32 @cat_file(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i64 @read(i32 %0, i8* %3, i32 1024)
%5 = icmp sgt i64 %4, 0
br i1 %5, label %9, label %22
6: ; preds = %16
%7 = call i64 @read(i32 %0, i8* %3, i32 1024)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %19, label %22
9: ; preds = %1, %19
%10 = phi i64 [ %20, %19 ], [ %4, %1 ]
%11 = phi i8* [ %21, %19 ], [ %3, %1 ]
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i64 @write(i32 %12, i8* %11, i64 %10)
%14 = icmp slt i64 %13, 0
%15 = sub nsw i64 %10, %13
br i1 %14, label %26, label %16
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %11, i64 %13
%18 = icmp sgt i64 %15, 0
br i1 %18, label %19, label %6
19: ; preds = %16, %6
%20 = phi i64 [ %15, %16 ], [ %7, %6 ]
%21 = phi i8* [ %17, %16 ], [ %3, %6 ]
br label %9
22: ; preds = %6, %1
%23 = phi i64 [ %4, %1 ], [ %7, %6 ]
%24 = icmp eq i64 %23, 0
%25 = zext i1 %24 to i32
br label %26
26: ; preds = %9, %22
%27 = phi i32 [ %25, %22 ], [ 0, %9 ]
ret i32 %27
}