#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 scalar_t__ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ kclose(int);
int /*<<< orphan*/ kconsoleRawOutput(char *, scalar_t__);
int kopen(char *, int);
int /*<<< orphan*/ kprintf(char *, char *);
int /*<<< orphan*/ kputchar(char);
int /*<<< orphan*/ kputs(char *);
scalar_t__ kread(int, char *, int);
int cmdDisplayFileContents(int argc, char **argv) {
  if (argc <= 1) {
    kputs("(x) Expected argument to command 'cat'\n");
  } else {
    int fd = kopen(argv[1], 0x0000);
    if (fd == -1) {
      kprintf("(x) Failed to open \"%s\" for reading\n", argv[1]);
    } else {
      char buf[512];
      uint32_t numBytesRead = (uint32_t)kread(fd, &buf[0], 512);
      while (numBytesRead) {
        kconsoleRawOutput(&buf[0], numBytesRead);
        numBytesRead = (uint32_t)kread(fd, &buf[0], 512);
      }
      kclose(fd);
      kputchar('\n');
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<cmdDisplayFileContents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jg     119d <cmdDisplayFileContents+0x2d>
lea    0xe6d(%rip),%rdi
call   1060 <kputs@plt>
jmp    1245 <cmdDisplayFileContents+0xd5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %esi,%esi
call   1050 <kopen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d2 <cmdDisplayFileContents+0x62>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe60(%rip),%rdi
call   1090 <kprintf@plt>
jmp    1240 <cmdDisplayFileContents+0xd0>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1080 <kread@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
je     122e <cmdDisplayFileContents+0xbe>
lea    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%rsi
call   1030 <kconsoleRawOutput@plt>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1080 <kread@plt>
mov    %rax,-0x228(%rbp)
jmp    11ed <cmdDisplayFileContents+0x7d>
mov    -0x14(%rbp),%edi
call   1040 <kclose@plt>
mov    $0xa,%edi
call   1070 <kputchar@plt>
jmp    1245 <cmdDisplayFileContents+0xd5>
mov    $0x1,%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmdDisplayFileContents>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
cmp    $0x1,%edi
jg     118c <cmdDisplayFileContents+0x1c>
lea    0xe7b(%rip),%rdi
call   1060 <kputs@plt>
jmp    1202 <cmdDisplayFileContents+0x92>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
xor    %esi,%esi
call   1050 <kopen@plt>
cmp    $0xffffffff,%eax
je     11f2 <cmdDisplayFileContents+0x82>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1080 <kread@plt>
test   %rax,%rax
je     11df <cmdDisplayFileContents+0x6f>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <kconsoleRawOutput@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <kread@plt>
test   %rax,%rax
jne    11c0 <cmdDisplayFileContents+0x50>
mov    %ebp,%edi
call   1040 <kclose@plt>
mov    $0xa,%edi
call   1070 <kputchar@plt>
jmp    1202 <cmdDisplayFileContents+0x92>
mov    0x8(%rbx),%rsi
lea    0xe2b(%rip),%rdi
call   1090 <kprintf@plt>
mov    $0x1,%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"(x) Expected argument to command 'cat'\0A\00"
@.str.1 = [37 x i8] c"(x) Failed to open \22%s\22 for reading\0A\00"
define i32 @cmdDisplayFileContents(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i64
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i32, i32* %3
%9 = icmp sle i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @kputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %43
12: ; preds = %2
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @kopen(i8* %15, i32 0)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %12
%20 = load i8**, i8*** %4
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @kprintf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %42
24: ; preds = %12
%25 = load i32, i32* %5
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%27 = call i64 @kread(i32 %25, i8* %26, i32 512)
store i64 %27, i64* %7
br label %28
28: ; preds = %31, %24
%29 = load i64, i64* %7
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%33 = load i64, i64* %7
%34 = call i32 @kconsoleRawOutput(i8* %32, i64 %33)
%35 = load i32, i32* %5
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%37 = call i64 @kread(i32 %35, i8* %36, i32 512)
store i64 %37, i64* %7
br label %28
38: ; preds = %28
%39 = load i32, i32* %5
%40 = call i32 @kclose(i32 %39)
%41 = call i32 @kputchar(i8 signext 10)
br label %42
42: ; preds = %38, %19
br label %43
43: ; preds = %42, %10
ret i32 1
}

LLVM IR (-O2):
@.str = [40 x i8] c"(x) Expected argument to command 'cat'\0A\00"
@.str.1 = [37 x i8] c"(x) Failed to open \22%s\22 for reading\0A\00"
define i32 @cmdDisplayFileContents(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @kputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %27
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @kopen(i8* %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %8
%14 = tail call i32 @kprintf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %27
15: ; preds = %7
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%17 = call i64 @kread(i32 %10, i8* %16, i32 512)
%18 = icmp eq i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15, %19
%20 = phi i64 [ %22, %19 ], [ %17, %15 ]
%21 = call i32 @kconsoleRawOutput(i8* %16, i64 %20)
%22 = call i64 @kread(i32 %10, i8* %16, i32 512)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %19
24: ; preds = %19, %15
%25 = call i32 @kclose(i32 %10)
%26 = call i32 @kputchar(i8 signext 10)
br label %27
27: ; preds = %12, %24, %5
ret i32 1
}