#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int open(char const *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
char *read_from_file(const char *filename, size_t length) {
  char *buffer;
  int fd;
  ssize_t bytes_read;
  buffer = (char *)malloc(length);
  if (buffer == NULL)
    return NULL;
  fd = open(filename, O_RDONLY);
  if (fd == -1) {
    free(buffer);
    return NULL;
  }
  bytes_read = read(fd, buffer, length);
  if (bytes_read != length) {
    free(buffer);
    close(fd);
    return NULL;
  }
  close(fd);
  return buffer;
}


x86 assembly (clang, -O0):
<read_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1185 <read_from_file+0x35>
movq   $0x0,-0x8(%rbp)
jmp    120a <read_from_file+0xba>
mov    -0x10(%rbp),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ba <read_from_file+0x6a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    120a <read_from_file+0xba>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11fa <read_from_file+0xaa>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    120a <read_from_file+0xba>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b6 <read_from_file+0x66>
mov    %rax,%rbx
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     119d <read_from_file+0x4d>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
cmp    %r14,%rax
jne    11a7 <read_from_file+0x57>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11b8 <read_from_file+0x68>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11b6 <read_from_file+0x66>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i8* @read_from_file(i8* nocapture %0, i64 %1) {
%3 = tail call i64 @malloc(i64 %1)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %20, label %6
6: ; preds = %2
%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 %10, label %12
10: ; preds = %6
%11 = tail call i32 @free(i8* %4)
br label %20
12: ; preds = %6
%13 = tail call i64 @read(i32 %8, i8* %4, i64 %1)
%14 = icmp eq i64 %13, %1
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = tail call i32 @free(i8* %4)
%17 = tail call i32 @close(i32 %8)
br label %20
18: ; preds = %12
%19 = tail call i32 @close(i32 %8)
br label %20
20: ; preds = %2, %18, %15, %10
%21 = phi i8* [ null, %10 ], [ null, %15 ], [ %4, %18 ], [ null, %2 ]
ret i8* %21
}