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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
long fread(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(long);
unsigned char *read_file(const char *name, long size) {
  FILE *f;
  unsigned char *data = NULL;
  f = fopen(name, "r");
  if (f != NULL) {
    data = (unsigned char *)malloc(size);
    if (data != NULL) {
      if (fread(data, sizeof(char), size, f) != size) {
        free(data);
        data = NULL;
      }
    }
    fclose(f);
  }
  return data;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11de <read_file+0x8e>
mov    -0x10(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d5 <read_file+0x85>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x10(%rbp),%rax
je     11d0 <read_file+0x80>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
jmp    11d5 <read_file+0x85>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
lea    0xea1(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a8 <read_file+0x58>
mov    %rax,%r14
mov    %r15,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     119c <read_file+0x4c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
je     119e <read_file+0x4e>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    11aa <read_file+0x5a>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @read_file(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
store i8* null, i8** %6
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %31
11: ; preds = %2
%12 = load i64, i64* %4
%13 = call i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %28
17: ; preds = %11
%18 = load i8*, i8** %6
%19 = load i64, i64* %4
%20 = load i32*, i32** %5
%21 = call i64 @fread(i8* %18, i32 1, i64 %19, i32* %20)
%22 = load i64, i64* %4
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
store i8* null, i8** %6
br label %27
27: ; preds = %24, %17
br label %28
28: ; preds = %27, %11
%29 = load i32*, i32** %5
%30 = call i32 @fclose(i32* %29)
br label %31
31: ; preds = %28, %2
%32 = load i8*, i8** %6
ret i8* %32
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @read_file(i8* nocapture %0, i64 %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %17, label %5
5: ; preds = %2
%6 = tail call i64 @malloc(i64 %1)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = tail call i64 @fread(i8* %7, i32 1, i64 %1, i32* %3)
%11 = icmp eq i64 %10, %1
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @free(i8* %7)
br label %14
14: ; preds = %9, %12, %5
%15 = phi i8* [ null, %12 ], [ %7, %9 ], [ null, %5 ]
%16 = tail call i32 @fclose(i32* %3)
br label %17
17: ; preds = %14, %2
%18 = phi i8* [ %15, %14 ], [ null, %2 ]
ret i8* %18
}