#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*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ cleanup(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ warn(char *, ...);
int write(int /*<<< orphan*/, char *, int);
void cat(char *fname) {
  int fd, n;
  char buf[2048];
  if ((fd = open(fname, O_RDONLY, 0)) < 0) {
    warn("%s", fname);
    (void)cleanup(0);
    exit(1);
  }
  while ((n = read(fd, buf, sizeof(buf))) > 0)
    if (write(STDOUT_FILENO, buf, n) != n) {
      warn("write");
      (void)cleanup(0);
      exit(1);
    }
  if (n == -1) {
    warn("read");
    (void)cleanup(0);
    exit(1);
  }
  (void)close(fd);
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11c2 <cat+0x52>
mov    -0x8(%rbp),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
xor    %edi,%edi
call   1070 <cleanup@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11c7 <cat+0x57>
mov    -0xc(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1060 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    122c <cat+0xbc>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    -0x810(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <write@plt>
cmp    -0x10(%rbp),%eax
je     1227 <cat+0xb7>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
xor    %edi,%edi
call   1070 <cleanup@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11c7 <cat+0x57>
cmpl   $0xffffffff,-0x10(%rbp)
jne    1255 <cat+0xe5>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
xor    %edi,%edi
call   1070 <cleanup@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
add    $0x810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%rbx
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1210 <cat+0xa0>
mov    %eax,%r14d
mov    %rsp,%rbx
mov    0x2e4f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11f6 <cat+0x86>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     11b0 <cat+0x40>
lea    0xe25(%rip),%rdi
xor    %eax,%eax
call   1030 <warn@plt>
xor    %edi,%edi
call   1070 <cleanup@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmp    $0xffffffff,%ebp
je     1223 <cat+0xb3>
mov    %r14d,%edi
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>
lea    0xde9(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
jmp    11e5 <cat+0x75>
lea    0xddf(%rip),%rdi
jmp    11de <cat+0x6e>

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [3 x i8] c"%s\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
define void @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, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i32 @cleanup(i32 0)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1, %14
%12 = call i32 @read(i32 %5, i8* %3, i32 2048)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @write(i32 %15, i8* %3, i32 %12)
%17 = icmp eq i32 %16, %12
br i1 %17, label %11, label %18
18: ; preds = %14
%19 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @cleanup(i32 0)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %11
%23 = icmp eq i32 %12, -1
br i1 %23, label %24, label %28
24: ; preds = %22
%25 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @cleanup(i32 0)
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = tail call i32 @close(i32 %5)
ret void
}