#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__);
int cat_fd(int fd) {
  char buf[4096];
  ssize_t nread;
  while ((nread = read(fd, buf, sizeof buf)) > 0) {
    ssize_t ntotalwritten = 0;
    while (ntotalwritten < nread) {
      ssize_t nwritten =
          write(STDOUT_FILENO, buf + ntotalwritten, nread - ntotalwritten);
      if (nwritten < 1)
        return -1;
      ntotalwritten += nwritten;
    }
  }
  return nread == 0 ? 0 : -1;
}


x86 assembly (clang, -O0):
<cat_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jle    11dc <cat_fd+0xbc>
movq   $0x0,-0x1020(%rbp)
mov    -0x1020(%rbp),%rax
cmp    -0x1018(%rbp),%rax
jge    11d7 <cat_fd+0xb7>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
add    -0x1020(%rbp),%rsi
mov    -0x1018(%rbp),%rdx
sub    -0x1020(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x1,-0x1028(%rbp)
01
jge    11bd <cat_fd+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <cat_fd+0xd4>
mov    -0x1028(%rbp),%rax
add    -0x1020(%rbp),%rax
mov    %rax,-0x1020(%rbp)
jmp    115e <cat_fd+0x3e>
jmp    112e <cat_fd+0xe>
mov    -0x1018(%rbp),%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_fd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %edi,%r15d
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    1198 <cat_fd+0x78>
mov    0x2ea2(%rip),%r12
mov    %rsp,%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdx
sub    %rbp,%rdx
jle    1180 <cat_fd+0x60>
mov    (%r12),%edi
lea    (%rsp,%rbp,1),%rsi
call   1030 <write@plt>
add    %rax,%rbp
test   %rax,%rax
jg     1160 <cat_fd+0x40>
jmp    119f <cat_fd+0x7f>
nop
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jg     1151 <cat_fd+0x31>
neg    %rbx
sbb    %eax,%eax
jmp    11a4 <cat_fd+0x84>
mov    $0xffffffff,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@STDOUT_FILENO = 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
%4 = call i64 @read(i32 %0, i8* %3, i32 4096)
%5 = icmp sgt i64 %4, 0
br i1 %5, label %9, label %21
6: ; preds = %11
%7 = call i64 @read(i32 %0, i8* %3, i32 4096)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %1, %6
%10 = phi i64 [ %7, %6 ], [ %4, %1 ]
br label %11
11: ; preds = %9, %14
%12 = phi i64 [ %20, %14 ], [ 0, %9 ]
%13 = icmp sgt i64 %10, %12
br i1 %13, label %14, label %6
14: ; preds = %11
%15 = load i32, i32* @STDOUT_FILENO
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %12
%17 = sub nsw i64 %10, %12
%18 = call i64 @write(i32 %15, i8* %16, i64 %17)
%19 = icmp slt i64 %18, 1
%20 = add nsw i64 %18, %12
br i1 %19, label %25, label %11
21: ; preds = %6, %1
%22 = phi i64 [ %4, %1 ], [ %7, %6 ]
%23 = icmp ne i64 %22, 0
%24 = sext i1 %23 to i32
br label %25
25: ; preds = %14, %21
%26 = phi i32 [ %24, %21 ], [ -1, %14 ]
ret i32 %26
}