#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int bail(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int write(int /*<<< orphan*/, char *, int);
int cat(const char *fname) {
  int filedes = 0, retrn = 0;
  char block[131072];
  if ((filedes = open(fname, O_RDONLY)) == -1)
    return bail("cat: %s: %s\n", fname, strerror(errno));
  do {
    retrn = read(filedes, block, 131072);
    if (retrn == -1) {
      close(filedes);
      return bail("cat: %s: %s\n", fname, strerror(errno));
    }
    if (!retrn)
      continue;
    retrn = write(STDOUT_FILENO, block, retrn);
    if (retrn == -1) {
      close(filedes);
      return bail("cat: %s: %s\n", fname, strerror(errno));
    }
  } while (retrn > 0);
  close(filedes);
  return 0;
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20040,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11d1 <cat+0x71>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20028(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20028(%rbp),%rsi
mov    %eax,%edx
lea    0xe3c(%rip),%rdi
call   1070 <bail@plt>
mov    %eax,-0x4(%rbp)
jmp    12c5 <cat+0x165>
jmp    11d6 <cat+0x76>
mov    -0x14(%rbp),%edi
lea    -0x20020(%rbp),%rsi
mov    $0x20000,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1235 <cat+0xd5>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20030(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20030(%rbp),%rsi
mov    %eax,%edx
lea    0xdd8(%rip),%rdi
call   1070 <bail@plt>
mov    %eax,-0x4(%rbp)
jmp    12c5 <cat+0x165>
cmpl   $0x0,-0x18(%rbp)
jne    1244 <cat+0xe4>
jmp    12ac <cat+0x14c>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    -0x20020(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12a7 <cat+0x147>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20038(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20038(%rbp),%rsi
mov    %eax,%edx
lea    0xd66(%rip),%rdi
call   1070 <bail@plt>
mov    %eax,-0x4(%rbp)
jmp    12c5 <cat+0x165>
jmp    12ac <cat+0x14c>
cmpl   $0x0,-0x18(%rbp)
jg     11d6 <cat+0x76>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x20008,%rsp
mov    %rdi,%r14
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11dc <cat+0x7c>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    0x2e61(%rip),%r15
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x20000,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11be <cat+0x5e>
cmp    $0xffffffff,%eax
je     11d5 <cat+0x75>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11d5 <cat+0x75>
test   %eax,%eax
jg     1190 <cat+0x30>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe0f(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
add    $0x20008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <bail@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [13 x i8] c"cat: %s: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @cat(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [131072 x i8]
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 @bail(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %14)
store i32 %15, i32* %2
br label %55
16: ; preds = %1
br label %17
17: ; preds = %49, %16
%18 = load i32, i32* %4
%19 = getelementptr inbounds [131072 x i8], [131072 x i8]* %6, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 131072)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
%26 = load i8*, i8** %3
%27 = load i32, i32* @errno
%28 = call i32 @strerror(i32 %27)
%29 = call i32 @bail(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %26, i32 %28)
store i32 %29, i32* %2
br label %55
30: ; preds = %17
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %30
br label %49
34: ; preds = %30
%35 = load i32, i32* @STDOUT_FILENO
%36 = getelementptr inbounds [131072 x i8], [131072 x i8]* %6, i64 0, i64 0
%37 = load i32, i32* %5
%38 = call i32 @write(i32 %35, i8* %36, i32 %37)
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %48
41: ; preds = %34
%42 = load i32, i32* %4
%43 = call i32 @close(i32 %42)
%44 = load i8*, i8** %3
%45 = load i32, i32* @errno
%46 = call i32 @strerror(i32 %45)
%47 = call i32 @bail(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %44, i32 %46)
store i32 %47, i32* %2
br label %55
48: ; preds = %34
br label %49
49: ; preds = %48, %33
%50 = load i32, i32* %5
%51 = icmp sgt i32 %50, 0
br i1 %51, label %17, label %52
52: ; preds = %49
%53 = load i32, i32* %4
%54 = call i32 @close(i32 %53)
store i32 0, i32* %2
br label %55
55: ; preds = %52, %41, %23, %11
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [13 x i8] c"cat: %s: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @cat(i8* %0) {
%2 = alloca [131072 x i8]
%3 = getelementptr inbounds [131072 x i8], [131072 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @bail(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %9)
br label %31
11: ; preds = %1, %27
%12 = call i32 @read(i32 %5, i8* %3, i32 131072)
switch i32 %12, label %18 [
i32 -1, label %13
i32 0, label %29
]
13: ; preds = %11
%14 = tail call i32 @close(i32 %5)
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @bail(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %16)
br label %31
18: ; preds = %11
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @write(i32 %19, i8* %3, i32 %12)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = tail call i32 @close(i32 %5)
%24 = load i32, i32* @errno
%25 = tail call i32 @strerror(i32 %24)
%26 = tail call i32 @bail(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %25)
br label %31
27: ; preds = %18
%28 = icmp sgt i32 %20, 0
br i1 %28, label %11, label %29
29: ; preds = %11, %27
%30 = tail call i32 @close(i32 %5)
br label %31
31: ; preds = %29, %22, %13, %7
%32 = phi i32 [ %10, %7 ], [ %17, %13 ], [ %26, %22 ], [ 0, %29 ]
ret i32 %32
}