#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 */
void *calloc(size_t, size_t);
int errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(void *, size_t, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
void *read_binary_file(char const *file_name, size_t size, size_t *_count) {
  FILE *stream;
  struct stat st[1];
  void *buffer = NULL;
  size_t count;
  stream = fopen(file_name, "rb");
  if (stream == NULL)
    goto failure;
  if (fstat(fileno(stream), st) != 0)
    goto failure;
  if (st->st_size % size != 0)
    goto failure;
  count = st->st_size / size;
  if (_count != NULL)
    *_count = count;
  buffer = calloc(count, size);
  if (buffer == NULL)
    goto failure;
  if (fread(buffer, size, count, stream) != count)
    goto failure;
  if (fclose(stream) != 0)
    goto failure;
  return buffer;
failure:
  if (buffer != NULL)
    free(buffer);
  if (stream != NULL) {
    int e = errno;
    fclose(stream);
    errno = e;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<read_binary_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b0 <read_binary_file+0x40>
jmp    127e <read_binary_file+0x10e>
mov    -0x28(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
lea    -0x30(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     11d3 <read_binary_file+0x63>
jmp    127e <read_binary_file+0x10e>
mov    -0x30(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
cmp    $0x0,%rdx
je     11ee <read_binary_file+0x7e>
jmp    127e <read_binary_file+0x10e>
mov    -0x30(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1214 <read_binary_file+0xa4>
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <calloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1235 <read_binary_file+0xc5>
jmp    127e <read_binary_file+0x10e>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <fread@plt>
cmp    -0x40(%rbp),%rax
je     1259 <read_binary_file+0xe9>
jmp    127e <read_binary_file+0x10e>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     1271 <read_binary_file+0x101>
jmp    127e <read_binary_file+0x10e>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c6 <read_binary_file+0x156>
cmpq   $0x0,-0x38(%rbp)
je     1292 <read_binary_file+0x122>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
je     12be <read_binary_file+0x14e>
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x44(%rbp),%ecx
mov    0x2d24(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_binary_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xe78(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1235 <read_binary_file+0xc5>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jne    1222 <read_binary_file+0xb2>
mov    (%rsp),%rax
mov    %rax,%rcx
or     %r14,%rcx
shr    $0x20,%rcx
je     11cf <read_binary_file+0x5f>
xor    %edx,%edx
div    %r14
mov    %rax,%r12
test   %rdx,%rdx
jne    1222 <read_binary_file+0xb2>
jmp    11dc <read_binary_file+0x6c>
xor    %edx,%edx
div    %r14d
mov    %eax,%r12d
test   %rdx,%rdx
jne    1222 <read_binary_file+0xb2>
test   %r15,%r15
je     11e4 <read_binary_file+0x74>
mov    %r12,(%r15)
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <calloc@plt>
test   %rax,%rax
je     1222 <read_binary_file+0xb2>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jne    121a <read_binary_file+0xaa>
mov    %r13,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     1237 <read_binary_file+0xc7>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2db7(%rip),%rbx
mov    (%rbx),%ebp
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %ebp,(%rbx)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [3 x i8] c"rb\00"
@errno = global i32 0
define i8* @read_binary_file(i8* %0, i64 %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca [1 x %struct.stat]
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i8* null, i8** %10
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %3
br label %67
18: ; preds = %3
%19 = load i32*, i32** %8
%20 = call i32 @fileno(i32* %19)
%21 = getelementptr inbounds [1 x %struct.stat], [1 x %struct.stat]* %9, i64 0, i64 0
%22 = call i64 @fstat(i32 %20, %struct.stat* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
br label %67
25: ; preds = %18
%26 = getelementptr inbounds [1 x %struct.stat], [1 x %struct.stat]* %9, i64 0, i64 0
%27 = getelementptr inbounds %struct.stat, %struct.stat* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = load i64, i64* %6
%30 = urem i64 %28, %29
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %25
br label %67
33: ; preds = %25
%34 = getelementptr inbounds [1 x %struct.stat], [1 x %struct.stat]* %9, i64 0, i64 0
%35 = getelementptr inbounds %struct.stat, %struct.stat* %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = load i64, i64* %6
%38 = udiv i64 %36, %37
store i64 %38, i64* %11
%39 = load i64*, i64** %7
%40 = icmp ne i64* %39, null
br i1 %40, label %41, label %44
41: ; preds = %33
%42 = load i64, i64* %11
%43 = load i64*, i64** %7
store i64 %42, i64* %43
br label %44
44: ; preds = %41, %33
%45 = load i64, i64* %11
%46 = load i64, i64* %6
%47 = call i8* @calloc(i64 %45, i64 %46)
store i8* %47, i8** %10
%48 = load i8*, i8** %10
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %51
50: ; preds = %44
br label %67
51: ; preds = %44
%52 = load i8*, i8** %10
%53 = load i64, i64* %6
%54 = load i64, i64* %11
%55 = load i32*, i32** %8
%56 = call i64 @fread(i8* %52, i64 %53, i64 %54, i32* %55)
%57 = load i64, i64* %11
%58 = icmp ne i64 %56, %57
br i1 %58, label %59, label %60
59: ; preds = %51
br label %67
60: ; preds = %51
%61 = load i32*, i32** %8
%62 = call i64 @fclose(i32* %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %60
br label %67
65: ; preds = %60
%66 = load i8*, i8** %10
store i8* %66, i8** %4
br label %82
67: ; preds = %64, %59, %50, %32, %24, %17
%68 = load i8*, i8** %10
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i8*, i8** %10
%72 = call i32 @free(i8* %71)
br label %73
73: ; preds = %70, %67
%74 = load i32*, i32** %8
%75 = icmp ne i32* %74, null
br i1 %75, label %76, label %81
76: ; preds = %73
%77 = load i32, i32* @errno
store i32 %77, i32* %12
%78 = load i32*, i32** %8
%79 = call i64 @fclose(i32* %78)
%80 = load i32, i32* %12
store i32 %80, i32* @errno
br label %81
81: ; preds = %76, %73
store i8* null, i8** %4
br label %82
82: ; preds = %81, %65
%83 = load i8*, i8** %4
ret i8* %83
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [3 x i8] c"rb\00"
@errno = global i32 0
define i8* @read_binary_file(i8* nocapture %0, i64 %1, i64* writeonly %2) {
%4 = alloca [1 x %struct.stat]
%5 = bitcast [1 x %struct.stat]* %4 to i8*
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %36, label %8
8: ; preds = %3
%9 = tail call i32 @fileno(i32* %6)
%10 = getelementptr inbounds [1 x %struct.stat], [1 x %struct.stat]* %4, i64 0, i64 0
%11 = call i64 @fstat(i32 %9, %struct.stat* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %8
%14 = getelementptr inbounds [1 x %struct.stat], [1 x %struct.stat]* %4, i64 0, i64 0, i32 0
%15 = load i64, i64* %14
%16 = urem i64 %15, %1
%17 = udiv i64 %15, %1
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %33
19: ; preds = %13
%20 = icmp eq i64* %2, null
br i1 %20, label %22, label %21
21: ; preds = %19
store i64 %17, i64* %2
br label %22
22: ; preds = %21, %19
%23 = tail call i8* @calloc(i64 %17, i64 %1)
%24 = icmp eq i8* %23, null
br i1 %24, label %33, label %25
25: ; preds = %22
%26 = tail call i64 @fread(i8* %23, i64 %1, i64 %17, i32* %6)
%27 = icmp eq i64 %26, %17
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = tail call i64 @fclose(i32* %6)
%30 = icmp eq i64 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %28, %25
%32 = tail call i32 @free(i8* %23)
br label %33
33: ; preds = %22, %13, %8, %31
%34 = load i32, i32* @errno
%35 = tail call i64 @fclose(i32* %6)
store i32 %34, i32* @errno
br label %36
36: ; preds = %3, %33, %28
%37 = phi i8* [ %23, %28 ], [ null, %33 ], [ null, %3 ]
ret i8* %37
}