#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
char *kl_strerror(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ *vfs_fopen(char *, char *);
void cmd_cat(char *cmd, char *args) {
  if (*args == '\0') {
    printf("Usage: cat <file>\n");
    return;
  }
  FILE *fh = vfs_fopen(args, "r");
  if (!fh) {
    printf("Cannot open %s: %s\n", args, kl_strerror(errno));
    return;
  }
  char buf[1024];
  while (fgets(buf, 1024, fh)) {
    fputs(buf, stdout);
  }
  if (buf[strlen(buf) - 1] != '\n') {
    putchar('\n');
  }
  if (ferror(fh)) {
    printf("Cannot read %s: %s\n", args, strerror(errno));
  }
  fclose(fh);
}


x86 assembly (clang, -O0):
<cmd_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11d6 <cmd_cat+0x36>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    12e2 <cmd_cat+0x142>
mov    -0x10(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1060 <vfs_fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122b <cmd_cat+0x8b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   10b0 <kl_strerror@plt>
mov    -0x428(%rbp),%rsi
mov    %rax,%rdx
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    12e2 <cmd_cat+0x142>
jmp    1230 <cmd_cat+0x90>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     1269 <cmd_cat+0xc9>
lea    -0x420(%rbp),%rdi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
call   1090 <fputs@plt>
jmp    1230 <cmd_cat+0x90>
lea    -0x420(%rbp),%rdi
call   1070 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     1295 <cmd_cat+0xf5>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12d9 <cmd_cat+0x139>
mov    -0x10(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x430(%rbp),%rsi
mov    %rax,%rdx
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_cat>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
cmpb   $0x0,(%rsi)
je     1295 <cmd_cat+0xe5>
mov    %rsi,%r14
lea    0xe2d(%rip),%rsi
mov    %r14,%rdi
call   1070 <vfs_fopen@plt>
test   %rax,%rax
je     12b1 <cmd_cat+0x101>
mov    %rax,%r12
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   10b0 <fgets@plt>
test   %rax,%rax
je     1231 <cmd_cat+0x81>
mov    0x2dd2(%rip),%r13
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   10a0 <fputs@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   10b0 <fgets@plt>
test   %rax,%rax
jne    1210 <cmd_cat+0x60>
mov    %r15,%rdi
call   1080 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r15,1)
je     124d <cmd_cat+0x9d>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r12,%rdi
call   1050 <ferror@plt>
test   %rax,%rax
je     127c <cmd_cat+0xcc>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xda7(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd8e(%rip),%rdi
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <puts@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   10c0 <kl_strerror@plt>
lea    0xd3c(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1090 <printf@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Usage: cat <file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [20 x i8] c"Cannot read %s: %s\0A\00"
define void @cmd_cat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [1024 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %56
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32* @vfs_fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %5
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = load i32, i32* @errno
%21 = call i8* @kl_strerror(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %19, i8* %21)
br label %56
23: ; preds = %13
br label %24
24: ; preds = %29, %23
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%26 = load i32*, i32** %5
%27 = call i64 @fgets(i8* %25, i32 1024, i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%31 = load i32, i32* @stdout
%32 = call i32 @fputs(i8* %30, i32 %31)
br label %24
33: ; preds = %24
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 10
br i1 %41, label %42, label %44
42: ; preds = %33
%43 = call i32 @putchar(i8 signext 10)
br label %44
44: ; preds = %42, %33
%45 = load i32*, i32** %5
%46 = call i64 @ferror(i32* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %44
%49 = load i8*, i8** %4
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %49, i8* %51)
br label %53
53: ; preds = %48, %44
%54 = load i32*, i32** %5
%55 = call i32 @fclose(i32* %54)
br label %56
56: ; preds = %53, %18, %11
ret void
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [20 x i8] c"Cannot read %s: %s\0A\00"
@str = [18 x i8] c"Usage: cat <file>\00"
define void @cmd_cat(i8* nocapture readnone %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %42
8: ; preds = %2
%9 = tail call i32* @vfs_fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = tail call i8* @kl_strerror(i32 %12)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %13)
br label %42
15: ; preds = %8
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = call i64 @fgets(i8* %16, i32 1024, i32* %9)
%18 = icmp eq i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15, %19
%20 = load i32, i32* @stdout
%21 = call i32 @fputs(i8* %16, i32 %20)
%22 = call i64 @fgets(i8* %16, i32 1024, i32* %9)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %19
24: ; preds = %19, %15
%25 = call i32 @strlen(i8* %16)
%26 = add nsw i32 %25, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 10
br i1 %30, label %33, label %31
31: ; preds = %24
%32 = call i32 @putchar(i8 signext 10)
br label %33
33: ; preds = %31, %24
%34 = call i64 @ferror(i32* %9)
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %38)
br label %40
40: ; preds = %36, %33
%41 = call i32 @fclose(i32* %9)
br label %42
42: ; preds = %11, %40, %6
ret void
}