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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
size_t read(int, int /*<<< orphan*/ *, size_t);
uint8_t *file_read(const char *name, size_t *retlen) {
  int fd;
  size_t count;
  uint8_t *buf, *p;
  struct stat st;
  size_t len;
  if ((fd = open(name, O_RDONLY)) < 0)
    return NULL;
  if (fstat(fd, &st) < 0)
    return NULL;
  if (st.st_size == 0)
    return NULL;
  if (NULL == (buf = (uint8_t *)malloc(st.st_size + 1)))
    return NULL;
  p = buf;
  len = st.st_size;
  while (((count = read(fd, p, len)) > 0) && len > 0) {
    p += count;
    len -= count;
  }
  if (len != 0) {
    free(buf);
    return NULL;
  }
  *retlen = st.st_size;
  return buf;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    118b <file_read+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1298 <file_read+0x148>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jge    11ae <file_read+0x5e>
movq   $0x0,-0x8(%rbp)
jmp    1298 <file_read+0x148>
cmpl   $0x0,-0x40(%rbp)
jne    11c5 <file_read+0x75>
movq   $0x0,-0x8(%rbp)
jmp    1298 <file_read+0x148>
mov    -0x40(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x30(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11ef <file_read+0x9f>
movq   $0x0,-0x8(%rbp)
jmp    1298 <file_read+0x148>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
movslq -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x49(%rbp)
jbe    1230 <file_read+0xe0>
cmpq   $0x0,-0x48(%rbp)
seta   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    1240 <file_read+0xf0>
jmp    1264 <file_read+0x114>
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
jmp    11ff <file_read+0xaf>
cmpq   $0x0,-0x48(%rbp)
je     1285 <file_read+0x135>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1298 <file_read+0x148>
movslq -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     1200 <file_read+0xb0>
mov    %eax,%r13d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
js     1200 <file_read+0xb0>
movslq 0x8(%rsp),%r14
test   %r14,%r14
je     1200 <file_read+0xb0>
lea    0x1(%r14),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1200 <file_read+0xb0>
mov    %rax,%r12
mov    %rbx,0x10(%rsp)
mov    %r13d,%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
test   %rax,%rax
je     11f8 <file_read+0xa8>
mov    %rax,%rbx
mov    %r14,%r15
mov    %r12,%rbp
nopw   0x0(%rax,%rax,1)
lea    0x0(%rbp,%rbx,4),%rbp
mov    %r15,%rdx
sub    %rbx,%rdx
mov    %r13d,%edi
mov    %rbp,%rsi
call   1040 <read@plt>
sub    %rbx,%r15
je     11f3 <file_read+0xa3>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <file_read+0x80>
test   %r15,%r15
je     1215 <file_read+0xc5>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%rax
mov    %r14,(%rax)
jmp    1203 <file_read+0xb3>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define i32* @file_read(i8* %0, i64* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca %struct.stat
%11 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32* null, i32** %3
br label %70
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i64 @fstat(i32 %18, %struct.stat* %10)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32* null, i32** %3
br label %70
22: ; preds = %17
%23 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32* null, i32** %3
br label %70
27: ; preds = %22
%28 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%29 = load i32, i32* %28
%30 = add nsw i32 %29, 1
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i32*
store i32* %32, i32** %8
%33 = icmp eq i32* null, %32
br i1 %33, label %34, label %35
34: ; preds = %27
store i32* null, i32** %3
br label %70
35: ; preds = %27
%36 = load i32*, i32** %8
store i32* %36, i32** %9
%37 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%38 = load i32, i32* %37
%39 = sext i32 %38 to i64
store i64 %39, i64* %11
br label %40
40: ; preds = %51, %35
%41 = load i32, i32* %6
%42 = load i32*, i32** %9
%43 = load i64, i64* %11
%44 = call i64 @read(i32 %41, i32* %42, i64 %43)
store i64 %44, i64* %7
%45 = icmp ugt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i64, i64* %11
%48 = icmp ugt i64 %47, 0
br label %49
49: ; preds = %46, %40
%50 = phi i1 [ false, %40 ], [ %48, %46 ]
br i1 %50, label %51, label %58
51: ; preds = %49
%52 = load i64, i64* %7
%53 = load i32*, i32** %9
%54 = getelementptr inbounds i32, i32* %53, i64 %52
store i32* %54, i32** %9
%55 = load i64, i64* %7
%56 = load i64, i64* %11
%57 = sub i64 %56, %55
store i64 %57, i64* %11
br label %40
58: ; preds = %49
%59 = load i64, i64* %11
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i32*, i32** %8
%63 = call i32 @free(i32* %62)
store i32* null, i32** %3
br label %70
64: ; preds = %58
%65 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%66 = load i32, i32* %65
%67 = sext i32 %66 to i64
%68 = load i64*, i64** %5
store i64 %67, i64* %68
%69 = load i32*, i32** %8
store i32* %69, i32** %3
br label %70
70: ; preds = %64, %61, %34, %26, %21, %16
%71 = load i32*, i32** %3
ret i32* %71
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define i32* @file_read(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %39, label %8
8: ; preds = %2
%9 = call i64 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %39, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %39, label %15
15: ; preds = %11
%16 = add nsw i32 %13, 1
%17 = tail call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i32*
%19 = icmp eq i64 %17, 0
br i1 %19, label %39, label %20
20: ; preds = %15
%21 = sext i32 %13 to i64
%22 = tail call i64 @read(i32 %6, i32* %18, i64 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %20, %24
%25 = phi i64 [ %30, %24 ], [ %22, %20 ]
%26 = phi i64 [ %29, %24 ], [ %21, %20 ]
%27 = phi i32* [ %28, %24 ], [ %18, %20 ]
%28 = getelementptr inbounds i32, i32* %27, i64 %25
%29 = sub i64 %26, %25
%30 = tail call i64 @read(i32 %6, i32* %28, i64 %29)
%31 = icmp ne i64 %30, 0
%32 = icmp ne i64 %29, 0
%33 = and i1 %32, %31
br i1 %33, label %24, label %34
34: ; preds = %24
%35 = icmp eq i64 %29, 0
br i1 %35, label %38, label %36
36: ; preds = %20, %34
%37 = tail call i32 @free(i32* %18)
br label %39
38: ; preds = %34
store i64 %21, i64* %1
br label %39
39: ; preds = %15, %11, %8, %2, %38, %36
%40 = phi i32* [ null, %36 ], [ %18, %38 ], [ null, %2 ], [ null, %8 ], [ null, %11 ], [ null, %15 ]
ret i32* %40
}