#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 EOF;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
char *calloc(size_t, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *bin_to_str(char *filename, char *(*func)(char *, size_t)) {
  size_t szBytes;
  char *bytes, *buffer;
  int c;
  FILE *fp;
  if (!filename)
    return NULL;
  fp = fopen(filename, "rb");
  if (!fp)
    return NULL;
  fseek(fp, 0, SEEK_END);
  szBytes = ftell(fp);
  buffer = calloc(szBytes + 1, sizeof(*buffer));
  if (!buffer)
    return NULL;
  fseek(fp, 0, SEEK_SET);
  bytes = buffer;
  while ((c = fgetc(fp)) != EOF && bytes < buffer + szBytes) {
    *bytes = c;
    bytes++;
  }
  fclose(fp);
  if (func == NULL)
    return buffer;
  else {
    bytes = func(buffer, szBytes);
    free(buffer);
    return bytes;
  }
}


x86 assembly (clang, -O0):
<bin_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1198 <bin_to_str+0x28>
movq   $0x0,-0x8(%rbp)
jmp    12d6 <bin_to_str+0x166>
mov    -0x10(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11c4 <bin_to_str+0x54>
movq   $0x0,-0x8(%rbp)
jmp    12d6 <bin_to_str+0x166>
mov    -0x40(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x40(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1213 <bin_to_str+0xa3>
movq   $0x0,-0x8(%rbp)
jmp    12d6 <bin_to_str+0x166>
mov    -0x40(%rbp),%rdi
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
xor    %eax,%eax
mov    0x2d9a(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x41(%rbp)
je     1266 <bin_to_str+0xf6>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
add    -0x20(%rbp),%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1276 <bin_to_str+0x106>
jmp    1292 <bin_to_str+0x122>
mov    -0x34(%rbp),%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    122f <bin_to_str+0xbf>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12b3 <bin_to_str+0x143>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d6 <bin_to_str+0x166>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   *%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin_to_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1257 <bin_to_str+0xe7>
mov    %rsi,%rbp
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1257 <bin_to_str+0xe7>
mov    %rax,%rbx
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
lea    0x1(%rax),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     1257 <bin_to_str+0xe7>
mov    %rax,%r15
mov    %rbp,(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    0x2de3(%rip),%r13
cmp    0x0(%r13),%eax
je     122e <bin_to_str+0xbe>
test   %r12,%r12
jle    122e <bin_to_str+0xbe>
lea    (%r15,%r12,1),%rbp
lea    0x1(%r15),%r14
mov    %al,-0x1(%r14)
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    0x0(%r13),%eax
je     122e <bin_to_str+0xbe>
lea    0x1(%r14),%rcx
cmp    %rbp,%r14
mov    %rcx,%r14
jb     1210 <bin_to_str+0xa0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    (%rsp),%rcx
test   %rcx,%rcx
je     125a <bin_to_str+0xea>
mov    %r15,%rdi
mov    %r12,%rsi
call   *%rcx
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
jmp    125d <bin_to_str+0xed>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EOF = global i32 0
define i8* @bin_to_str(i8* %0, i8* (i8*, i64)* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8* (i8*, i64)*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
store i8* %0, i8** %4
store i8* (i8*, i64)* %1, i8* (i8*, i64)** %5
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i8* null, i8** %3
br label %71
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %10
%17 = load i32*, i32** %10
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %14
store i8* null, i8** %3
br label %71
20: ; preds = %14
%21 = load i32*, i32** %10
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %10
%25 = call i64 @ftell(i32* %24)
store i64 %25, i64* %6
%26 = load i64, i64* %6
%27 = add i64 %26, 1
%28 = call i8* @calloc(i64 %27, i32 1)
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %20
store i8* null, i8** %3
br label %71
32: ; preds = %20
%33 = load i32*, i32** %10
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %33, i32 0, i32 %34)
%36 = load i8*, i8** %8
store i8* %36, i8** %7
br label %37
37: ; preds = %50, %32
%38 = load i32*, i32** %10
%39 = call i32 @fgetc(i32* %38)
store i32 %39, i32* %9
%40 = load i32, i32* @EOF
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i8*, i8** %7
%44 = load i8*, i8** %8
%45 = load i64, i64* %6
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = icmp ult i8* %43, %46
br label %48
48: ; preds = %42, %37
%49 = phi i1 [ false, %37 ], [ %47, %42 ]
br i1 %49, label %50, label %56
50: ; preds = %48
%51 = load i32, i32* %9
%52 = trunc i32 %51 to i8
%53 = load i8*, i8** %7
store i8 %52, i8* %53
%54 = load i8*, i8** %7
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %7
br label %37
56: ; preds = %48
%57 = load i32*, i32** %10
%58 = call i32 @fclose(i32* %57)
%59 = load i8* (i8*, i64)*, i8* (i8*, i64)** %5
%60 = icmp eq i8* (i8*, i64)* %59, null
br i1 %60, label %61, label %63
61: ; preds = %56
%62 = load i8*, i8** %8
store i8* %62, i8** %3
br label %71
63: ; preds = %56
%64 = load i8* (i8*, i64)*, i8* (i8*, i64)** %5
%65 = load i8*, i8** %8
%66 = load i64, i64* %6
%67 = call i8* %64(i8* %65, i64 %66)
store i8* %67, i8** %7
%68 = load i8*, i8** %8
%69 = call i32 @free(i8* %68)
%70 = load i8*, i8** %7
store i8* %70, i8** %3
br label %71
71: ; preds = %63, %61, %31, %19, %13
%72 = load i8*, i8** %3
ret i8* %72
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EOF = global i32 0
define i8* @bin_to_str(i8* %0, i8* (i8*, i64)* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %39, label %4
4: ; preds = %2
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %39, label %7
7: ; preds = %4
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %5, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %5)
%11 = add i64 %10, 1
%12 = tail call i8* @calloc(i64 %11, i32 1)
%13 = icmp eq i8* %12, null
br i1 %13, label %39, label %14
14: ; preds = %7
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %5, i32 0, i32 %15)
%17 = tail call i32 @fgetc(i32* %5)
%18 = load i32, i32* @EOF
%19 = icmp ne i32 %17, %18
%20 = getelementptr inbounds i8, i8* %12, i64 %10
%21 = icmp sgt i64 %10, 0
%22 = and i1 %19, %21
br i1 %22, label %23, label %33
23: ; preds = %14, %23
%24 = phi i32 [ %28, %23 ], [ %17, %14 ]
%25 = phi i8* [ %27, %23 ], [ %12, %14 ]
%26 = trunc i32 %24 to i8
store i8 %26, i8* %25
%27 = getelementptr inbounds i8, i8* %25, i64 1
%28 = tail call i32 @fgetc(i32* %5)
%29 = load i32, i32* @EOF
%30 = icmp ne i32 %28, %29
%31 = icmp ult i8* %27, %20
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %23, label %33
33: ; preds = %23, %14
%34 = tail call i32 @fclose(i32* %5)
%35 = icmp eq i8* (i8*, i64)* %1, null
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = tail call i8* %1(i8* %12, i64 %10)
%38 = tail call i32 @free(i8* %12)
br label %39
39: ; preds = %33, %7, %4, %2, %36
%40 = phi i8* [ %37, %36 ], [ null, %2 ], [ null, %4 ], [ null, %7 ], [ %12, %33 ]
ret i8* %40
}