#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 /*<<< orphan*/ STDOUT_FILENO;
int close(int);
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, size_t);
int write(int /*<<< orphan*/, char *, int);
ssize_t read_textfile(const char *filename, size_t letters) {
  char *print_this;
  int fd;
  ssize_t r_result;
  ssize_t w_result;
  ssize_t c_close;
  if (filename == NULL)
    return (0);
  print_this = malloc(sizeof(char) * letters);
  fd = open(filename, O_RDONLY);
  if (fd == -1)
    return (0);
  r_result = read(fd, print_this, letters);
  if (r_result == -1)
    return (-1);
  w_result = write(STDOUT_FILENO, print_this, r_result);
  if (w_result == -1)
    return (-1);
  c_close = close(fd);
  if (c_close == -1)
    return (-1);
  return (r_result);
}


x86 assembly (clang, -O0):
<read_textfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1177 <read_textfile+0x27>
movl   $0x0,-0x4(%rbp)
jmp    1233 <read_textfile+0xe3>
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b5 <read_textfile+0x65>
movl   $0x0,-0x4(%rbp)
jmp    1233 <read_textfile+0xe3>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11de <read_textfile+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <read_textfile+0xe3>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    120c <read_textfile+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <read_textfile+0xe3>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    122d <read_textfile+0xdd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <read_textfile+0xe3>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_textfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r12d,%r12d
test   %rdi,%rdi
je     11cc <read_textfile+0x7c>
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11cc <read_textfile+0x7c>
mov    %eax,%r15d
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1050 <read@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     11cc <read_textfile+0x7c>
mov    %eax,%ebp
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11cc <read_textfile+0x7c>
mov    %r15d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebp
mov    %ebp,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @read_textfile(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* %3
br label %49
14: ; preds = %2
%15 = load i64, i64* %5
%16 = mul i64 1, %15
%17 = trunc i64 %16 to i32
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %6
%19 = load i8*, i8** %4
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %14
store i32 0, i32* %3
br label %49
25: ; preds = %14
%26 = load i32, i32* %7
%27 = load i8*, i8** %6
%28 = load i64, i64* %5
%29 = call i32 @read(i32 %26, i8* %27, i64 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -1, i32* %3
br label %49
33: ; preds = %25
%34 = load i32, i32* @STDOUT_FILENO
%35 = load i8*, i8** %6
%36 = load i32, i32* %8
%37 = call i32 @write(i32 %34, i8* %35, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 -1, i32* %3
br label %49
41: ; preds = %33
%42 = load i32, i32* %7
%43 = call i32 @close(i32 %42)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 -1, i32* %3
br label %49
47: ; preds = %41
%48 = load i32, i32* %8
store i32 %48, i32* %3
br label %49
49: ; preds = %47, %46, %40, %32, %24, %13
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @read_textfile(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %21, label %4
4: ; preds = %2
%5 = trunc i64 %1 to i32
%6 = tail call i8* @malloc(i32 %5)
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %21, label %10
10: ; preds = %4
%11 = tail call i32 @read(i32 %8, i8* %6, i64 %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = load i32, i32* @STDOUT_FILENO
%15 = tail call i32 @write(i32 %14, i8* %6, i32 %11)
%16 = icmp eq i32 %15, -1
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = tail call i32 @close(i32 %8)
%19 = icmp eq i32 %18, -1
%20 = select i1 %19, i32 -1, i32 %11
br label %21
21: ; preds = %17, %13, %10, %4, %2
%22 = phi i32 [ 0, %2 ], [ 0, %4 ], [ -1, %10 ], [ -1, %13 ], [ %20, %17 ]
ret i32 %22
}