#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;
scalar_t__ close(int);
int /*<<< orphan*/ die(char const *);
int open(char const *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
scalar_t__ write(int /*<<< orphan*/, unsigned char *, int);
void do_cat(const char *path) {
  int fd;
  unsigned char buf[2048];
  int n;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    die(path);
  for (;;) {
    n = read(fd, buf, sizeof buf);
    if (n < 0)
      die(path);
    if (n == 0)
      break;
    if (write(STDOUT_FILENO, buf, n) < 0)
      die(path);
  }
  if (close(fd) < 0)
    die(path);
}


x86 assembly (clang, -O0):
<do_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1187 <do_cat+0x37>
mov    -0x8(%rbp),%rdi
call   1070 <die@plt>
jmp    118c <do_cat+0x3c>
mov    -0xc(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1050 <read@plt>
mov    %eax,-0x814(%rbp)
cmpl   $0x0,-0x814(%rbp)
jge    11bc <do_cat+0x6c>
mov    -0x8(%rbp),%rdi
call   1070 <die@plt>
cmpl   $0x0,-0x814(%rbp)
jne    11ce <do_cat+0x7e>
jmp    1201 <do_cat+0xb1>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    -0x810(%rbp),%rsi
mov    -0x814(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11fc <do_cat+0xac>
mov    -0x8(%rbp),%rdi
call   1070 <die@plt>
jmp    118c <do_cat+0x3c>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    121c <do_cat+0xcc>
mov    -0x8(%rbp),%rdi
call   1070 <die@plt>
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_cat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rdi,%r14
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    117f <do_cat+0x2f>
mov    %r14,%rdi
call   1070 <die@plt>
mov    %rsp,%rbx
mov    0x2e67(%rip),%r12
jmp    11ab <do_cat+0x5b>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1070 <die@plt>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1177 <do_cat+0x27>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1190 <do_cat+0x40>
jne    1198 <do_cat+0x48>
mov    %r15d,%edi
call   1040 <close@plt>
test   %rax,%rax
js     11e0 <do_cat+0x90>
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <die@plt>

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@STDOUT_FILENO = global i32 0
define void @do_cat(i8* %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %17, %1
%8 = tail call i32 @die(i8* %0)
br label %9
9: ; preds = %1, %7
br label %10
10: ; preds = %9, %17
%11 = call i32 @read(i32 %5, i8* %3, i32 2048)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @die(i8* %0)
br label %17
15: ; preds = %10
%16 = icmp eq i32 %11, 0
br i1 %16, label %21, label %17
17: ; preds = %13, %15
%18 = load i32, i32* @STDOUT_FILENO
%19 = call i64 @write(i32 %18, i8* %3, i32 %11)
%20 = icmp slt i64 %19, 0
br i1 %20, label %7, label %10
21: ; preds = %15
%22 = tail call i64 @close(i32 %5)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @die(i8* %0)
br label %26
26: ; preds = %24, %21
ret void
}