#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int read(int, char *, size_t);
int /*<<< orphan*/ stderr;
int cat(const char *file, char *out, size_t len) {
  if (!out || !len)
    return -2;
  int fd = open(file, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "Opening %s failed: ", file);
    perror(0);
    return -3;
  }
  ssize_t r = read(fd, out, len - 1);
  if (r == -1) {
    fprintf(stderr, "Reading from %s failed: ", file);
    perror(0);
    return -1;
  }
  out[r] = 0;
  int ret = close(fd);
  if (ret == -1) {
    fprintf(stderr, "Closing %s failed: ", file);
    perror(0);
    return -4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     117a <cat+0x2a>
cmpq   $0x0,-0x20(%rbp)
jne    1186 <cat+0x36>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1272 <cat+0x122>
mov    -0x10(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11d1 <cat+0x81>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe47(%rip),%rsi
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1272 <cat+0x122>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    121e <cat+0xce>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe0e(%rip),%rsi
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <cat+0x122>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    126b <cat+0x11b>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdda(%rip),%rsi
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1272 <cat+0x122>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xfffffffe,%r15d
test   %rsi,%rsi
je     11fd <cat+0xad>
mov    %rdx,%rbp
test   %rdx,%rdx
je     11fd <cat+0xad>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11c9 <cat+0x79>
mov    %eax,%r15d
add    $0xffffffffffffffff,%rbp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11d8 <cat+0x88>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %r15d,%edi
call   1030 <close@plt>
xor    %r15d,%r15d
cmp    $0xffffffff,%eax
jne    11fd <cat+0xad>
mov    $0xfffffffc,%r15d
lea    0xe66(%rip),%rsi
jmp    11e5 <cat+0x95>
mov    $0xfffffffd,%r15d
lea    0xe2a(%rip),%rsi
jmp    11e5 <cat+0x95>
mov    $0xffffffff,%r15d
lea    0xe2f(%rip),%rsi
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Opening %s failed: \00"
@.str.1 = [25 x i8] c"Reading from %s failed: \00"
@.str.2 = [20 x i8] c"Closing %s failed: \00"
define i32 @cat(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i64, i64* %7
%15 = icmp ne i64 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %13, %3
store i32 -2, i32* %4
br label %56
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %5
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i32 @perror(i32 0)
store i32 -3, i32* %4
br label %56
28: ; preds = %17
%29 = load i32, i32* %8
%30 = load i8*, i8** %6
%31 = load i64, i64* %7
%32 = sub i64 %31, 1
%33 = call i32 @read(i32 %29, i8* %30, i64 %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %5
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = call i32 @perror(i32 0)
store i32 -1, i32* %4
br label %56
41: ; preds = %28
%42 = load i8*, i8** %6
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %41
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %5
%53 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = call i32 @perror(i32 0)
store i32 -4, i32* %4
br label %56
55: ; preds = %41
store i32 0, i32* %4
br label %56
56: ; preds = %55, %50, %36, %23, %16
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Opening %s failed: \00"
@.str.1 = [25 x i8] c"Reading from %s failed: \00"
@.str.2 = [20 x i8] c"Closing %s failed: \00"
define i32 @cat(i8* %0, i8* %1, i64 %2) {
%4 = icmp ne i8* %1, null
%5 = icmp ne i64 %2, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %26
7: ; preds = %3
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %20, label %11
11: ; preds = %7
%12 = add i64 %2, -1
%13 = tail call i32 @read(i32 %9, i8* %1, i64 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %20, label %15
15: ; preds = %11
%16 = sext i32 %13 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 0, i8* %17
%18 = tail call i32 @close(i32 %9)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %15, %11, %7
%21 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), %7 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %15 ]
%22 = phi i32 [ -3, %7 ], [ -1, %11 ], [ -4, %15 ]
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* %21, i8* %0)
%25 = tail call i32 @perror(i32 0)
br label %26
26: ; preds = %20, %15, %3
%27 = phi i32 [ -2, %3 ], [ 0, %15 ], [ %22, %20 ]
ret i32 %27
}