#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*/ close(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, size_t);
ssize_t read_text(const char *path, char *buf, size_t max_len) {
  ssize_t len;
  int fd;
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return fd;
  len = read(fd, buf, max_len - 1);
  if (len < 0)
    goto out;
  buf[len] = 0;
out:
  close(fd);
  return len;
}


x86 assembly (clang, -O0):
<read_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    116e <read_text+0x3e>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ae <read_text+0x7e>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1194 <read_text+0x64>
jmp    11a0 <read_text+0x70>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1178 <read_text+0x48>
add    $0xffffffffffffffff,%r15
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     116f <read_text+0x3f>
mov    %ebp,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @read_text(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* %9
store i32 %16, i32* %4
br label %35
17: ; preds = %3
%18 = load i32, i32* %9
%19 = load i8*, i8** %6
%20 = load i64, i64* %7
%21 = sub i64 %20, 1
%22 = call i32 @read(i32 %18, i8* %19, i64 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %17
br label %31
26: ; preds = %17
%27 = load i8*, i8** %6
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %26, %25
%32 = load i32, i32* %9
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %31, %15
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @read_text(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %3
%8 = add i64 %2, -1
%9 = tail call i32 @read(i32 %5, i8* %1, i64 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %7, %11
%15 = tail call i32 @close(i32 %5)
br label %16
16: ; preds = %3, %14
%17 = phi i32 [ %9, %14 ], [ %5, %3 ]
ret i32 %17
}