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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
scalar_t__ read(int, char *, int);
void test_cat(char *fn) {
  int file = open(fn, O_RDONLY);
  if (file >= 0) {
    char c = 0;
    while (read(file, &c, sizeof(c)) > 0) {
      printf("%02x ", c);
    }
    close(file);
    puts("");
  } else {
    printf("Cannot open %s\n", fn);
  }
}


x86 assembly (clang, -O0):
<test_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11ca <test_cat+0x7a>
movb   $0x0,-0xd(%rbp)
mov    -0xc(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jle    11b1 <test_cat+0x61>
movsbl -0xd(%rbp),%esi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    117f <test_cat+0x2f>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
jmp    11dc <test_cat+0x8c>
mov    -0x8(%rbp),%rsi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_cat>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11dd <test_cat+0x8d>
mov    %eax,%ebx
movb   $0x0,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    11c3 <test_cat+0x73>
lea    0xe6e(%rip),%r14
lea    0xf(%rsp),%r15
nopw   0x0(%rax,%rax,1)
movsbl 0xf(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jg     11a0 <test_cat+0x50>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xa,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <putchar@plt>
lea    0xe22(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <printf@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [6 x i8] c"%02x \00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [16 x i8] c"Cannot open %s\0A\00"
define void @test_cat(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %1
store i8 0, i8* %4
br label %11
11: ; preds = %15, %10
%12 = load i32, i32* %3
%13 = call i64 @read(i32 %12, i8* %4, i32 1)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i8, i8* %4
%17 = sext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %11
19: ; preds = %11
%20 = load i32, i32* %3
%21 = call i32 @close(i32 %20)
%22 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %26
23: ; preds = %1
%24 = load i8*, i8** %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23, %19
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [6 x i8] c"%02x \00"
@.str.2 = [16 x i8] c"Cannot open %s\0A\00"
define void @test_cat(i8* %0) {
%2 = alloca i8
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %18
6: ; preds = %1
store i8 0, i8* %2
%7 = call i64 @read(i32 %4, i8* %2, i32 1)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %6, %9
%10 = load i8, i8* %2
%11 = sext i8 %10 to i32
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i64 @read(i32 %4, i8* %2, i32 1)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %9, label %15
15: ; preds = %9, %6
%16 = tail call i32 @close(i32 %4)
%17 = tail call i32 @putchar(i32 10)
br label %20
18: ; preds = %1
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %20
20: ; preds = %18, %15
ret void
}