#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 */
struct stat {
  size_t st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ dtree_error_from_errno();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
size_t fread(void *, int, size_t const, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
void *malloc(size_t const);
void *file_read_and_close(FILE *file, size_t *flen) {
  struct stat file_stat;
  if (fstat(fileno(file), &file_stat)) {
    dtree_error_from_errno();
    fclose(file);
    return NULL;
  }
  const size_t fsize = file_stat.st_size;
  void *m = malloc(fsize + 1);
  if (m == NULL) {
    dtree_error_from_errno();
    fclose(file);
    return NULL;
  }
  size_t rlen = fread(m, 1, fsize, file);
  if (rlen < fsize) {
    dtree_error_from_errno();
    free(m);
    fclose(file);
    return NULL;
  }
  ((char *)m)[fsize] = '\0';
  fclose(file);
  *flen = fsize;
  return m;
}


x86 assembly (clang, -O0):
<file_read_and_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11bb <file_read_and_close+0x4b>
mov    $0x0,%al
call   1090 <dtree_error_from_errno@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1272 <file_read_and_close+0x102>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11fc <file_read_and_close+0x8c>
mov    $0x0,%al
call   1090 <dtree_error_from_errno@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1272 <file_read_and_close+0x102>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    124a <file_read_and_close+0xda>
mov    $0x0,%al
call   1090 <dtree_error_from_errno@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1272 <file_read_and_close+0x102>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read_and_close>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
call   1060 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
je     11b2 <file_read_and_close+0x42>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <dtree_error_from_errno@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%rsp),%r12
lea    0x1(%r12),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1192 <file_read_and_close+0x22>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jae    11fb <file_read_and_close+0x8b>
xor    %eax,%eax
call   1090 <dtree_error_from_errno@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %ebx,%ebx
jmp    11a3 <file_read_and_close+0x33>
movb   $0x0,(%rbx,%r12,1)
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r12,(%r15)
jmp    11a3 <file_read_and_close+0x33>

LLVM IR (-O0):
%struct.stat = type { i64 }
define i8* @file_read_and_close(i32* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64*
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i32* %0, i32** %4
store i64* %1, i64** %5
%10 = load i32*, i32** %4
%11 = call i32 @fileno(i32* %10)
%12 = call i64 @fstat(i32 %11, %struct.stat* %6)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = call i32 (...) @dtree_error_from_errno()
%16 = load i32*, i32** %4
%17 = call i32 @fclose(i32* %16)
store i8* null, i8** %3
br label %53
18: ; preds = %2
%19 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%20 = load i64, i64* %19
store i64 %20, i64* %7
%21 = load i64, i64* %7
%22 = add i64 %21, 1
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = call i32 (...) @dtree_error_from_errno()
%28 = load i32*, i32** %4
%29 = call i32 @fclose(i32* %28)
store i8* null, i8** %3
br label %53
30: ; preds = %18
%31 = load i8*, i8** %8
%32 = load i64, i64* %7
%33 = load i32*, i32** %4
%34 = call i64 @fread(i8* %31, i32 1, i64 %32, i32* %33)
store i64 %34, i64* %9
%35 = load i64, i64* %9
%36 = load i64, i64* %7
%37 = icmp ult i64 %35, %36
br i1 %37, label %38, label %44
38: ; preds = %30
%39 = call i32 (...) @dtree_error_from_errno()
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
%42 = load i32*, i32** %4
%43 = call i32 @fclose(i32* %42)
store i8* null, i8** %3
br label %53
44: ; preds = %30
%45 = load i8*, i8** %8
%46 = load i64, i64* %7
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8 0, i8* %47
%48 = load i32*, i32** %4
%49 = call i32 @fclose(i32* %48)
%50 = load i64, i64* %7
%51 = load i64*, i64** %5
store i64 %50, i64* %51
%52 = load i8*, i8** %8
store i8* %52, i8** %3
br label %53
53: ; preds = %44, %38, %26, %14
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
%struct.stat = type { i64 }
define noalias i8* @file_read_and_close(i32* nocapture %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @fileno(i32* %0)
%6 = call i64 @fstat(i32 %5, %struct.stat* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 (...) @dtree_error_from_errno()
%10 = tail call i32 @fclose(i32* %0)
br label %30
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i64, i64* %12
%14 = add i64 %13, 1
%15 = tail call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 (...) @dtree_error_from_errno()
%19 = tail call i32 @fclose(i32* %0)
br label %30
20: ; preds = %11
%21 = tail call i64 @fread(i8* %15, i32 1, i64 %13, i32* %0)
%22 = icmp ult i64 %21, %13
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = tail call i32 (...) @dtree_error_from_errno()
%25 = tail call i32 @free(i8* %15)
%26 = tail call i32 @fclose(i32* %0)
br label %30
27: ; preds = %20
%28 = getelementptr inbounds i8, i8* %15, i64 %13
store i8 0, i8* %28
%29 = tail call i32 @fclose(i32* %0)
store i64 %13, i64* %1
br label %30
30: ; preds = %17, %27, %23, %8
%31 = phi i8* [ null, %8 ], [ null, %17 ], [ null, %23 ], [ %15, %27 ]
ret i8* %31
}