#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*/ close(int);
char *malloc(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main(int argc, char **argv) {
  char *buf;
  int fd, res;
  if (argc == 1) {
    printf("cat: must specify a file\n");
    return -1;
  }
  buf = malloc(256);
  fd = open(argv[1], O_RDONLY, 0);
  if (fd < 0) {
    printf("cat: no such file or directory: %s\n", argv[1]);
    return 0;
  }
  while ((res = read(fd, buf, 255))) {
    buf[res] = 0;
    printf(buf);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    118a <main+0x3a>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <main+0xdc>
mov    $0x100,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11df <main+0x8f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    122c <main+0xdc>
jmp    11e4 <main+0x94>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     121d <main+0xcd>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e4 <main+0x94>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jne    1180 <main+0x20>
lea    0xeb1(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
jmp    11f3 <main+0x93>
mov    %rsi,%r15
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%r15),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     11e1 <main+0x81>
mov    %eax,%ebp
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11d5 <main+0x75>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b0 <main+0x50>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %r14d,%r14d
jmp    11f3 <main+0x93>
mov    0x8(%r15),%rsi
lea    0xe14(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"cat: must specify a file\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [36 x i8] c"cat: no such file or directory: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
13: ; preds = %2
%14 = call i8* @malloc(i32 256)
store i8* %14, i8** %6
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18, i32 0)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %13
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 0, i32* %3
br label %43
27: ; preds = %13
br label %28
28: ; preds = %33, %27
%29 = load i32, i32* %7
%30 = load i8*, i8** %6
%31 = call i32 @read(i32 %29, i8* %30, i32 255)
store i32 %31, i32* %8
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %28
%34 = load i8*, i8** %6
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %6
%39 = call i32 (i8*, ...) @printf(i8* %38)
br label %28
40: ; preds = %28
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
store i32 0, i32* %3
br label %43
43: ; preds = %40, %22, %11
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [36 x i8] c"cat: no such file or directory: %s\0A\00"
@str = [25 x i8] c"cat: must specify a file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %28
6: ; preds = %2
%7 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %6
%14 = tail call i32 @read(i32 %11, i8* %7, i32 255)
%15 = icmp eq i32 %14, 0
br i1 %15, label %26, label %19
16: ; preds = %6
%17 = load i8*, i8** %8
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %28
19: ; preds = %13, %19
%20 = phi i32 [ %24, %19 ], [ %14, %13 ]
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %7, i64 %21
store i8 0, i8* %22
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %7)
%24 = tail call i32 @read(i32 %11, i8* %7, i32 255)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %19
26: ; preds = %19, %13
%27 = tail call i32 @close(i32 %11)
br label %28
28: ; preds = %26, %16, %4
%29 = phi i32 [ -1, %4 ], [ 0, %16 ], [ 0, %26 ]
ret i32 %29
}