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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int close(int);
unsigned long lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(unsigned long);
int open(char *, int /*<<< orphan*/);
unsigned long read(int, char *, unsigned long);
int openreadclose(char *fn, char **buf, unsigned long *len) {
  int fd = open(fn, O_RDONLY);
  if (fd < 0)
    return -1;
  if (!*buf) {
    *len = lseek(fd, 0, SEEK_END);
    lseek(fd, 0, SEEK_SET);
    *buf = (char *)malloc(*len + 1);
    if (!*buf) {
      close(fd);
      return -1;
    }
  }
  *len = read(fd, *buf, *len);
  if (*len != (unsigned long)-1)
    (*buf)[*len] = 0;
  return close(fd);
}


x86 assembly (clang, -O0):
<openreadclose>:
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    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    118f <openreadclose+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <openreadclose+0x109>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    120e <openreadclose+0xbe>
mov    -0x24(%rbp),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1209 <openreadclose+0xb9>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <openreadclose+0x109>
jmp    120e <openreadclose+0xbe>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
call   1050 <read@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0xffffffffffffffff,(%rax)
je     124e <openreadclose+0xfe>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openreadclose>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11e0 <openreadclose+0x90>
mov    %eax,%ebp
mov    (%r14),%rsi
test   %rsi,%rsi
jne    11b4 <openreadclose+0x64>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,(%rbx)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    (%rbx),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%rsi
mov    %rax,(%r14)
test   %rax,%rax
je     11d9 <openreadclose+0x89>
mov    (%rbx),%rdx
mov    %ebp,%edi
call   1050 <read@plt>
mov    %rax,(%rbx)
cmp    $0xffffffffffffffff,%rax
je     11ce <openreadclose+0x7e>
mov    (%r14),%rcx
movb   $0x0,(%rcx,%rax,1)
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @openreadclose(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %60
15: ; preds = %3
%16 = load i8**, i8*** %6
%17 = load i8*, i8** %16
%18 = icmp ne i8* %17, null
br i1 %18, label %40, label %19
19: ; preds = %15
%20 = load i32, i32* %8
%21 = load i32, i32* @SEEK_END
%22 = call i64 @lseek(i32 %20, i32 0, i32 %21)
%23 = load i64*, i64** %7
store i64 %22, i64* %23
%24 = load i32, i32* %8
%25 = load i32, i32* @SEEK_SET
%26 = call i64 @lseek(i32 %24, i32 0, i32 %25)
%27 = load i64*, i64** %7
%28 = load i64, i64* %27
%29 = add i64 %28, 1
%30 = call i64 @malloc(i64 %29)
%31 = inttoptr i64 %30 to i8*
%32 = load i8**, i8*** %6
store i8* %31, i8** %32
%33 = load i8**, i8*** %6
%34 = load i8*, i8** %33
%35 = icmp ne i8* %34, null
br i1 %35, label %39, label %36
36: ; preds = %19
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %4
br label %60
39: ; preds = %19
br label %40
40: ; preds = %39, %15
%41 = load i32, i32* %8
%42 = load i8**, i8*** %6
%43 = load i8*, i8** %42
%44 = load i64*, i64** %7
%45 = load i64, i64* %44
%46 = call i64 @read(i32 %41, i8* %43, i64 %45)
%47 = load i64*, i64** %7
store i64 %46, i64* %47
%48 = load i64*, i64** %7
%49 = load i64, i64* %48
%50 = icmp ne i64 %49, -1
br i1 %50, label %51, label %57
51: ; preds = %40
%52 = load i8**, i8*** %6
%53 = load i8*, i8** %52
%54 = load i64*, i64** %7
%55 = load i64, i64* %54
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 0, i8* %56
br label %57
57: ; preds = %51, %40
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
store i32 %59, i32* %4
br label %60
60: ; preds = %57, %36, %14
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @openreadclose(i8* nocapture %0, i8** nocapture %1, i64* nocapture %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 %32, label %7
7: ; preds = %3
%8 = load i8*, i8** %1
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %22
10: ; preds = %7
%11 = load i32, i32* @SEEK_END
%12 = tail call i64 @lseek(i32 %5, i32 0, i32 %11)
store i64 %12, i64* %2
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @lseek(i32 %5, i32 0, i32 %13)
%15 = load i64, i64* %2
%16 = add i64 %15, 1
%17 = tail call i64 @malloc(i64 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %1
%19 = icmp eq i64 %17, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = tail call i32 @close(i32 %5)
br label %32
22: ; preds = %10, %7
%23 = phi i8* [ %18, %10 ], [ %8, %7 ]
%24 = load i64, i64* %2
%25 = tail call i64 @read(i32 %5, i8* %23, i64 %24)
store i64 %25, i64* %2
%26 = icmp eq i64 %25, -1
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = load i8*, i8** %1
%29 = getelementptr inbounds i8, i8* %28, i64 %25
store i8 0, i8* %29
br label %30
30: ; preds = %27, %22
%31 = tail call i32 @close(i32 %5)
br label %32
32: ; preds = %3, %30, %20
%33 = phi i32 [ %31, %30 ], [ -1, %20 ], [ -1, %3 ]
ret i32 %33
}