#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*/ errno;
int /*<<< orphan*/ error_reason(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
scalar_t__ misc_malloc(size_t);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int misc_read_special_file(const char *filename, char **buffer, size_t *size,
                           int nil_terminate) {
  FILE *file;
  char *data;
  char *new_data;
  size_t count;
  size_t current_size;
  int current;
  file = fopen(filename, "r");
  if (file == NULL) {
    error_reason(strerror(errno));
    return -1;
  }
  current_size = 1024;
  count = 0;
  data = (char *)misc_malloc(current_size);
  if (data == NULL) {
    fclose(file);
    return -1;
  }
  current = fgetc(file);
  while (current != EOF || nil_terminate) {
    if (current == EOF) {
      current = 0;
      nil_terminate = 0;
    }
    data[count++] = (char)current;
    if (count >= current_size) {
      new_data = (char *)misc_malloc(current_size * 2);
      if (new_data == NULL) {
        free(data);
        fclose(file);
        return -1;
      }
      memcpy(new_data, data, current_size);
      free(data);
      data = new_data;
      current_size *= 2;
    }
    current = fgetc(file);
  }
  fclose(file);
  *buffer = data;
  if (size)
    *size = count;
  return 0;
}


x86 assembly (clang, -O0):
<misc_read_special_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d7 <misc_read_special_file+0x57>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%edi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <misc_read_special_file+0x1c4>
movq   $0x400,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
call   1080 <misc_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1214 <misc_read_special_file+0x94>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <misc_read_special_file+0x1c4>
mov    -0x30(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%ecx
mov    $0x1,%al
mov    0x2dbc(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x55(%rbp)
jne    1241 <misc_read_special_file+0xc1>
cmpl   $0x0,-0x24(%rbp)
setne  %al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    1251 <misc_read_special_file+0xd1>
jmp    1313 <misc_read_special_file+0x193>
mov    -0x54(%rbp),%eax
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%eax
jne    1271 <misc_read_special_file+0xf1>
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x54(%rbp),%eax
mov    %al,%dl
mov    -0x38(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x48(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x48(%rbp),%rax
cmp    -0x50(%rbp),%rax
jb     1302 <misc_read_special_file+0x182>
mov    -0x50(%rbp),%rdi
shl    $0x1,%rdi
call   1080 <misc_malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12d4 <misc_read_special_file+0x154>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1344 <misc_read_special_file+0x1c4>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x50(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x54(%rbp)
jmp    1220 <misc_read_special_file+0xa0>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     133d <misc_read_special_file+0x1bd>
mov    -0x48(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_read_special_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r13d
mov    %rdx,%rbx
mov    %rsi,%rbp
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     11ef <misc_read_special_file+0x6f>
mov    $0x400,%edi
call   1080 <misc_malloc@plt>
test   %rax,%rax
je     12b0 <misc_read_special_file+0x130>
mov    %rax,%r15
mov    (%rsp),%rdi
call   1060 <fgetc@plt>
mov    0x2e16(%rip),%rcx
mov    (%rcx),%ecx
cmp    %ecx,%eax
mov    %rbx,0x10(%rsp)
mov    %rbp,0x8(%rsp)
jne    1209 <misc_read_special_file+0x89>
test   %r13d,%r13d
jne    1209 <misc_read_special_file+0x89>
xor    %r12d,%r12d
jmp    1286 <misc_read_special_file+0x106>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%edi
call   1030 <error_reason@plt>
jmp    12b9 <misc_read_special_file+0x139>
mov    $0x400,%r14d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    %ecx,%eax
mov    $0x0,%ecx
cmove  %ecx,%r13d
cmove  %ecx,%eax
mov    %al,(%r15,%r12,1)
add    $0x1,%r12
cmp    %r14,%r12
jb     126b <misc_read_special_file+0xeb>
lea    (%r14,%r14,1),%rbp
mov    %rbp,%rdi
call   1080 <misc_malloc@plt>
test   %rax,%rax
je     12a8 <misc_read_special_file+0x128>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <memcpy@plt>
mov    %r15,%rdi
call   1040 <free@plt>
mov    %rbx,%r15
mov    %rbp,%r14
mov    (%rsp),%rdi
call   1060 <fgetc@plt>
mov    0x2d6d(%rip),%rcx
mov    (%rcx),%ecx
cmp    %ecx,%eax
jne    1220 <misc_read_special_file+0xa0>
test   %r13d,%r13d
jne    1220 <misc_read_special_file+0xa0>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rax
mov    %r15,(%rax)
xor    %eax,%eax
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
je     12be <misc_read_special_file+0x13e>
mov    %r12,(%rcx)
jmp    12be <misc_read_special_file+0x13e>
mov    %r15,%rdi
call   1040 <free@plt>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@EOF = global i32 0
define i32 @misc_read_special_file(i8* %0, i8** %1, i64* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i64* %2, i64** %8
store i32 %3, i32* %9
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %10
%18 = load i32*, i32** %10
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %4
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @error_reason(i32 %22)
store i32 -1, i32* %5
br label %96
24: ; preds = %4
store i64 1024, i64* %14
store i64 0, i64* %13
%25 = load i64, i64* %14
%26 = call i64 @misc_malloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %11
%28 = load i8*, i8** %11
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32*, i32** %10
%32 = call i32 @fclose(i32* %31)
store i32 -1, i32* %5
br label %96
33: ; preds = %24
%34 = load i32*, i32** %10
%35 = call i32 @fgetc(i32* %34)
store i32 %35, i32* %15
br label %36
36: ; preds = %82, %33
%37 = load i32, i32* %15
%38 = load i32, i32* @EOF
%39 = icmp ne i32 %37, %38
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = load i32, i32* %9
%42 = icmp ne i32 %41, 0
br label %43
43: ; preds = %40, %36
%44 = phi i1 [ true, %36 ], [ %42, %40 ]
br i1 %44, label %45, label %85
45: ; preds = %43
%46 = load i32, i32* %15
%47 = load i32, i32* @EOF
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 0, i32* %15
store i32 0, i32* %9
br label %50
50: ; preds = %49, %45
%51 = load i32, i32* %15
%52 = trunc i32 %51 to i8
%53 = load i8*, i8** %11
%54 = load i64, i64* %13
%55 = add i64 %54, 1
store i64 %55, i64* %13
%56 = getelementptr inbounds i8, i8* %53, i64 %54
store i8 %52, i8* %56
%57 = load i64, i64* %13
%58 = load i64, i64* %14
%59 = icmp uge i64 %57, %58
br i1 %59, label %60, label %82
60: ; preds = %50
%61 = load i64, i64* %14
%62 = mul i64 %61, 2
%63 = call i64 @misc_malloc(i64 %62)
%64 = inttoptr i64 %63 to i8*
store i8* %64, i8** %12
%65 = load i8*, i8** %12
%66 = icmp eq i8* %65, null
br i1 %66, label %67, label %72
67: ; preds = %60
%68 = load i8*, i8** %11
%69 = call i32 @free(i8* %68)
%70 = load i32*, i32** %10
%71 = call i32 @fclose(i32* %70)
store i32 -1, i32* %5
br label %96
72: ; preds = %60
%73 = load i8*, i8** %12
%74 = load i8*, i8** %11
%75 = load i64, i64* %14
%76 = call i32 @memcpy(i8* %73, i8* %74, i64 %75)
%77 = load i8*, i8** %11
%78 = call i32 @free(i8* %77)
%79 = load i8*, i8** %12
store i8* %79, i8** %11
%80 = load i64, i64* %14
%81 = mul i64 %80, 2
store i64 %81, i64* %14
br label %82
82: ; preds = %72, %50
%83 = load i32*, i32** %10
%84 = call i32 @fgetc(i32* %83)
store i32 %84, i32* %15
br label %36
85: ; preds = %43
%86 = load i32*, i32** %10
%87 = call i32 @fclose(i32* %86)
%88 = load i8*, i8** %11
%89 = load i8**, i8*** %7
store i8* %88, i8** %89
%90 = load i64*, i64** %8
%91 = icmp ne i64* %90, null
br i1 %91, label %92, label %95
92: ; preds = %85
%93 = load i64, i64* %13
%94 = load i64*, i64** %8
store i64 %93, i64* %94
br label %95
95: ; preds = %92, %85
store i32 0, i32* %5
br label %96
96: ; preds = %95, %67, %30, %20
%97 = load i32, i32* %5
ret i32 %97
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@EOF = global i32 0
define i32 @misc_read_special_file(i8* nocapture %0, i8** nocapture writeonly %1, i64* writeonly %2, i32 %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @error_reason(i32 %9)
br label %62
11: ; preds = %4
%12 = tail call i64 @misc_malloc(i64 1024)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @fclose(i32* %5)
br label %62
16: ; preds = %11
%17 = inttoptr i64 %12 to i8*
%18 = tail call i32 @fgetc(i32* %5)
%19 = load i32, i32* @EOF
%20 = icmp ne i32 %18, %19
%21 = icmp ne i32 %3, 0
%22 = or i1 %20, %21
br i1 %22, label %23, label %56
23: ; preds = %16, %48
%24 = phi i32 [ %52, %48 ], [ %19, %16 ]
%25 = phi i32 [ %51, %48 ], [ %18, %16 ]
%26 = phi i64 [ %50, %48 ], [ 1024, %16 ]
%27 = phi i64 [ %34, %48 ], [ 0, %16 ]
%28 = phi i8* [ %49, %48 ], [ %17, %16 ]
%29 = phi i32 [ %31, %48 ], [ %3, %16 ]
%30 = icmp eq i32 %25, %24
%31 = select i1 %30, i32 0, i32 %29
%32 = trunc i32 %25 to i8
%33 = select i1 %30, i8 0, i8 %32
%34 = add i64 %27, 1
%35 = getelementptr inbounds i8, i8* %28, i64 %27
store i8 %33, i8* %35
%36 = icmp ult i64 %34, %26
br i1 %36, label %48, label %37
37: ; preds = %23
%38 = shl i64 %26, 1
%39 = tail call i64 @misc_malloc(i64 %38)
%40 = inttoptr i64 %39 to i8*
%41 = icmp eq i64 %39, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = tail call i32 @free(i8* %28)
%44 = tail call i32 @fclose(i32* %5)
br label %62
45: ; preds = %37
%46 = tail call i32 @memcpy(i8* %40, i8* %28, i64 %26)
%47 = tail call i32 @free(i8* %28)
br label %48
48: ; preds = %45, %23
%49 = phi i8* [ %40, %45 ], [ %28, %23 ]
%50 = phi i64 [ %38, %45 ], [ %26, %23 ]
%51 = tail call i32 @fgetc(i32* %5)
%52 = load i32, i32* @EOF
%53 = icmp ne i32 %51, %52
%54 = icmp ne i32 %31, 0
%55 = select i1 %53, i1 true, i1 %54
br i1 %55, label %23, label %56
56: ; preds = %48, %16
%57 = phi i8* [ %17, %16 ], [ %49, %48 ]
%58 = phi i64 [ 0, %16 ], [ %34, %48 ]
%59 = tail call i32 @fclose(i32* %5)
store i8* %57, i8** %1
%60 = icmp eq i64* %2, null
br i1 %60, label %62, label %61
61: ; preds = %56
store i64 %58, i64* %2
br label %62
62: ; preds = %56, %61, %42, %14, %7
%63 = phi i32 [ -1, %7 ], [ -1, %14 ], [ -1, %42 ], [ 0, %61 ], [ 0, %56 ]
ret i32 %63
}