#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 /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ err(int, char *, ...);
int read(int, char *, int);
int write(int /*<<< orphan*/, char *, int);
void docat(const char *name, int fd) {
  char buf[1024];
  int len, wr, wrtot;
  while ((len = read(fd, buf, sizeof(buf))) > 0) {
    wrtot = 0;
    while (wrtot < len) {
      wr = write(STDOUT_FILENO, buf + wrtot, len - wrtot);
      if (wr < 0) {
        err(1, "stdout");
      }
      wrtot += wr;
    }
  }
  if (len < 0) {
    err(1, "%s", name);
  }
}


x86 assembly (clang, -O0):
<docat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    11ee <docat+0xbe>
movl   $0x0,-0x41c(%rbp)
mov    -0x41c(%rbp),%eax
cmp    -0x414(%rbp),%eax
jge    11e9 <docat+0xb9>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
movslq -0x41c(%rbp),%rax
add    %rax,%rsi
mov    -0x414(%rbp),%edx
sub    -0x41c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jge    11d2 <docat+0xa2>
mov    $0x1,%edi
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x418(%rbp),%eax
add    -0x41c(%rbp),%eax
mov    %eax,-0x41c(%rbp)
jmp    116f <docat+0x3f>
jmp    1142 <docat+0x12>
cmpl   $0x0,-0x414(%rbp)
jge    1212 <docat+0xe2>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<docat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %esi,%r15d
mov    %rdi,0x8(%rsp)
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11d6 <docat+0xa6>
mov    0x2e88(%rip),%r14
lea    0xe91(%rip),%r13
jmp    1198 <docat+0x68>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11d6 <docat+0xa6>
xor    %r12d,%r12d
jmp    11a8 <docat+0x78>
nopl   (%rax)
add    %ebp,%r12d
cmp    %r12d,%ebx
jle    1180 <docat+0x50>
mov    (%r14),%edi
movslq %r12d,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    %ebx,%edx
sub    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11a0 <docat+0x70>
mov    $0x1,%edi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <err@plt>
jmp    11a0 <docat+0x70>
test   %ebx,%ebx
js     11ec <docat+0xbc>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe14(%rip),%rsi
mov    $0x1,%edi
mov    0x8(%rsp),%rdx
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <err@plt>

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

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [7 x i8] c"stdout\00"
@.str.1 = [3 x i8] c"%s\00"
define void @docat(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %1, i8* %4, i32 1024)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %10, label %25
7: ; preds = %22
%8 = call i32 @read(i32 %1, i8* %4, i32 1024)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %25
10: ; preds = %2, %7
%11 = phi i32 [ %8, %7 ], [ %5, %2 ]
br label %12
12: ; preds = %10, %22
%13 = phi i32 [ %23, %22 ], [ 0, %10 ]
%14 = load i32, i32* @STDOUT_FILENO
%15 = sext i32 %13 to i64
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %15
%17 = sub nsw i32 %11, %13
%18 = call i32 @write(i32 %14, i8* %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %12
%23 = add nsw i32 %18, %13
%24 = icmp sgt i32 %11, %23
br i1 %24, label %12, label %7
25: ; preds = %7, %2
%26 = phi i32 [ %5, %2 ], [ %8, %7 ]
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %28, %25
ret void
}