#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 */
struct stat {
  int st_blksize;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ fwrite(char *, scalar_t__, int, int /*<<< orphan*/);
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char const *, char *);
bool cat_file(const char *file) {
  bool success = true;
  char *buf = NULL;
  struct stat st;
  ssize_t ret;
  int fd;
  if (strcmp(file, "-") == 0) {
    fd = 0;
  } else {
    fd = open(file, O_RDONLY);
    if (fd < 0) {
      perror("cat: open");
      return false;
    }
  }
  if (fstat(fd, &st) != 0) {
    perror("cat: fstat");
    close(fd);
    return false;
  }
  if (st.st_blksize == 0) {
    fprintf(stderr, "cat: warning: st_blksize is 0\n");
    st.st_blksize = 1;
  }
  buf = malloc(st.st_blksize);
  if (!buf) {
    perror("cat: malloc");
    close(fd);
    return false;
  }
  while (true) {
    ret = read(fd, buf, st.st_blksize);
    if (ret < 0) {
      perror("cat: read");
      success = false;
      break;
    } else if (ret == 0) {
      break;
    }
    fwrite(buf, ret, 1, stdout);
  }
  free(buf);
  close(fd);
  return success;
}


x86 assembly (clang, -O0):
<cat_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11e1 <cat_file+0x41>
movl   $0x0,-0x34(%rbp)
jmp    121d <cat_file+0x7d>
mov    -0x10(%rbp),%rdi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1218 <cat_file+0x78>
lea    0xdfb(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1337 <cat_file+0x197>
jmp    121d <cat_file+0x7d>
mov    -0x34(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
je     1253 <cat_file+0xb3>
lea    0xdd2(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1337 <cat_file+0x197>
cmpl   $0x0,-0x28(%rbp)
jne    1279 <cat_file+0xd9>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12b0 <cat_file+0x110>
lea    0xd9f(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1337 <cat_file+0x197>
jmp    12b5 <cat_file+0x115>
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    12eb <cat_file+0x14b>
lea    0xd68(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x14(%rbp)
jmp    1320 <cat_file+0x180>
cmpq   $0x0,-0x30(%rbp)
jne    12fb <cat_file+0x15b>
jmp    1320 <cat_file+0x180>
jmp    1300 <cat_file+0x160>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2cc1(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
jmp    12b5 <cat_file+0x115>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xe4a(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11ec <cat_file+0x4c>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11ef <cat_file+0x4f>
lea    0xe23(%rip),%rdi
call   10a0 <perror@plt>
xor    %r15d,%r15d
jmp    12aa <cat_file+0x10a>
xor    %r14d,%r14d
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
call   10c0 <fstat@plt>
test   %rax,%rax
jne    12ba <cat_file+0x11a>
mov    0x8(%rsp),%ebp
test   %ebp,%ebp
jne    1227 <cat_file+0x87>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12c3 <cat_file+0x123>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
test   %rax,%rax
js     128b <cat_file+0xeb>
mov    0x2d7d(%rip),%r12
mov    $0x1,%r15d
nopl   0x0(%rax)
test   %rax,%rax
je     129a <cat_file+0xfa>
mov    (%r12),%ecx
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
test   %rax,%rax
jns    1260 <cat_file+0xc0>
lea    0xdb0(%rip),%rdi
call   10a0 <perror@plt>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4b(%rip),%rdi
jmp    12ca <cat_file+0x12a>
lea    0xd6c(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %r15d,%r15d
jmp    12aa <cat_file+0x10a>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"-\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"cat: open\00"
@.str.2 = [11 x i8] c"cat: fstat\00"
@stderr = global i32 0
@.str.3 = [31 x i8] c"cat: warning: st_blksize is 0\0A\00"
@.str.4 = [12 x i8] c"cat: malloc\00"
@.str.5 = [10 x i8] c"cat: read\00"
@stdout = global i32 0
define i32 @cat_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
store i32 1, i32* %4
store i8* null, i8** %5
%9 = load i8*, i8** %3
%10 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %8
br label %22
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %75
21: ; preds = %13
br label %22
22: ; preds = %21, %12
%23 = load i32, i32* %8
%24 = call i64 @fstat(i32 %23, %struct.stat* %6)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
store i32 0, i32* %2
br label %75
30: ; preds = %22
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%37 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
store i32 1, i32* %37
br label %38
38: ; preds = %34, %30
%39 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i8* @malloc(i32 %40)
store i8* %41, i8** %5
%42 = load i8*, i8** %5
%43 = icmp ne i8* %42, null
br i1 %43, label %48, label %44
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
store i32 0, i32* %2
br label %75
48: ; preds = %38
br label %49
49: ; preds = %48, %64
%50 = load i32, i32* %8
%51 = load i8*, i8** %5
%52 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @read(i32 %50, i8* %51, i32 %53)
store i64 %54, i64* %7
%55 = load i64, i64* %7
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %4
br label %69
59: ; preds = %49
%60 = load i64, i64* %7
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %59
br label %69
63: ; preds = %59
br label %64
64: ; preds = %63
%65 = load i8*, i8** %5
%66 = load i64, i64* %7
%67 = load i32, i32* @stdout
%68 = call i32 @fwrite(i8* %65, i64 %66, i32 1, i32 %67)
br label %49
69: ; preds = %62, %57
%70 = load i8*, i8** %5
%71 = call i32 @free(i8* %70)
%72 = load i32, i32* %8
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* %4
store i32 %74, i32* %2
br label %75
75: ; preds = %69, %44, %26, %19
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"-\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"cat: open\00"
@.str.2 = [11 x i8] c"cat: fstat\00"
@stderr = global i32 0
@.str.3 = [31 x i8] c"cat: warning: st_blksize is 0\0A\00"
@.str.4 = [12 x i8] c"cat: malloc\00"
@.str.5 = [10 x i8] c"cat: read\00"
@stdout = global i32 0
define i32 @cat_file(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %50
12: ; preds = %1, %6
%13 = phi i32 [ %8, %6 ], [ 0, %1 ]
%14 = call i64 @fstat(i32 %13, %struct.stat* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @close(i32 %13)
br label %50
19: ; preds = %12
%20 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %23, %19
%27 = phi i32 [ 1, %23 ], [ %21, %19 ]
%28 = tail call i8* @malloc(i32 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %26
%31 = tail call i64 @read(i32 %13, i8* %28, i32 %27)
%32 = icmp slt i64 %31, 0
br i1 %32, label %36, label %38
33: ; preds = %26
%34 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%35 = tail call i32 @close(i32 %13)
br label %50
36: ; preds = %41, %30
%37 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %46
38: ; preds = %30, %41
%39 = phi i64 [ %44, %41 ], [ %31, %30 ]
%40 = icmp eq i64 %39, 0
br i1 %40, label %46, label %41
41: ; preds = %38
%42 = load i32, i32* @stdout
%43 = tail call i32 @fwrite(i8* %28, i64 %39, i32 1, i32 %42)
%44 = tail call i64 @read(i32 %13, i8* %28, i32 %27)
%45 = icmp slt i64 %44, 0
br i1 %45, label %36, label %38
46: ; preds = %38, %36
%47 = phi i32 [ 0, %36 ], [ 1, %38 ]
%48 = tail call i32 @free(i8* %28)
%49 = tail call i32 @close(i32 %13)
br label %50
50: ; preds = %46, %33, %16, %10
%51 = phi i32 [ 0, %16 ], [ %47, %46 ], [ 0, %33 ], [ 0, %10 ]
ret i32 %51
}