#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*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int util_proc_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) {
    printf("Could not open %s\n", filename);
    return -1;
  }
  current_size = 1024;
  count = 0;
  data = (char *)malloc(current_size);
  if (data == NULL) {
    printf("Could not allocate %lld bytes of memory", (unsigned long long)size);
    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 *)malloc(current_size * 2);
      if (new_data == NULL) {
        printf("Could not allocate %lld bytes of memory",
               (unsigned long long)size);
        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):
<util_proc_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    0xe6e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c4 <util_proc_read_special_file+0x54>
mov    -0x10(%rbp),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <util_proc_read_special_file+0x1e5>
movq   $0x400,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1213 <util_proc_read_special_file+0xa3>
mov    -0x20(%rbp),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <util_proc_read_special_file+0x1e5>
mov    -0x30(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%ecx
mov    $0x1,%al
mov    0x2dbd(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x55(%rbp)
jne    1240 <util_proc_read_special_file+0xd0>
cmpl   $0x0,-0x24(%rbp)
setne  %al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    1250 <util_proc_read_special_file+0xe0>
jmp    1324 <util_proc_read_special_file+0x1b4>
mov    -0x54(%rbp),%eax
mov    0x2d8e(%rip),%rcx
cmp    (%rcx),%eax
jne    1270 <util_proc_read_special_file+0x100>
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     1313 <util_proc_read_special_file+0x1a3>
mov    -0x50(%rbp),%rdi
shl    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12e5 <util_proc_read_special_file+0x175>
mov    -0x20(%rbp),%rsi
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <util_proc_read_special_file+0x1e5>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x38(%rbp),%rdi
call   1030 <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    121f <util_proc_read_special_file+0xaf>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     134e <util_proc_read_special_file+0x1de>
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):
<util_proc_read_special_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r13d
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe6f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     11e1 <util_proc_read_special_file+0x71>
mov    $0x400,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11f7 <util_proc_read_special_file+0x87>
mov    %rax,%r14
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    0x2e24(%rip),%rcx
mov    (%rcx),%ecx
cmp    %ecx,%eax
mov    %rbp,0x8(%rsp)
mov    %r15,0x10(%rsp)
jne    120d <util_proc_read_special_file+0x9d>
test   %r13d,%r13d
jne    120d <util_proc_read_special_file+0x9d>
xor    %r12d,%r12d
jmp    1289 <util_proc_read_special_file+0x119>
lea    0xe1a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12ce <util_proc_read_special_file+0x15e>
lea    0xe17(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12c5 <util_proc_read_special_file+0x155>
mov    $0x400,%r15d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
cmp    %ecx,%eax
mov    $0x0,%ecx
cmove  %ecx,%r13d
cmove  %ecx,%eax
mov    %al,(%r14,%r12,1)
add    $0x1,%r12
cmp    %r15,%r12
jb     126f <util_proc_read_special_file+0xff>
lea    (%r15,%r15,1),%rbp
mov    %rbp,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     12aa <util_proc_read_special_file+0x13a>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%r14
mov    %rbp,%r15
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    0x2d6a(%rip),%rcx
mov    (%rcx),%ecx
cmp    %ecx,%eax
jne    1220 <util_proc_read_special_file+0xb0>
test   %r13d,%r13d
jne    1220 <util_proc_read_special_file+0xb0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%rax
mov    %r14,(%rax)
xor    %eax,%eax
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     12d3 <util_proc_read_special_file+0x163>
mov    %r12,(%rcx)
jmp    12d3 <util_proc_read_special_file+0x163>
lea    0xd64(%rip),%rdi
mov    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1040 <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"
@.str.1 = [19 x i8] c"Could not open %s\0A\00"
@.str.2 = [40 x i8] c"Could not allocate %lld bytes of memory\00"
@EOF = global i32 0
define i32 @util_proc_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 %23
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 -1, i32* %5
br label %101
23: ; preds = %4
store i64 1024, i64* %14
store i64 0, i64* %13
%24 = load i64, i64* %14
%25 = call i64 @malloc(i64 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %11
%27 = load i8*, i8** %11
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %35
29: ; preds = %23
%30 = load i64*, i64** %8
%31 = ptrtoint i64* %30 to i64
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %31)
%33 = load i32*, i32** %10
%34 = call i32 @fclose(i32* %33)
store i32 -1, i32* %5
br label %101
35: ; preds = %23
%36 = load i32*, i32** %10
%37 = call i32 @fgetc(i32* %36)
store i32 %37, i32* %15
br label %38
38: ; preds = %87, %35
%39 = load i32, i32* %15
%40 = load i32, i32* @EOF
%41 = icmp ne i32 %39, %40
br i1 %41, label %45, label %42
42: ; preds = %38
%43 = load i32, i32* %9
%44 = icmp ne i32 %43, 0
br label %45
45: ; preds = %42, %38
%46 = phi i1 [ true, %38 ], [ %44, %42 ]
br i1 %46, label %47, label %90
47: ; preds = %45
%48 = load i32, i32* %15
%49 = load i32, i32* @EOF
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 0, i32* %15
store i32 0, i32* %9
br label %52
52: ; preds = %51, %47
%53 = load i32, i32* %15
%54 = trunc i32 %53 to i8
%55 = load i8*, i8** %11
%56 = load i64, i64* %13
%57 = add i64 %56, 1
store i64 %57, i64* %13
%58 = getelementptr inbounds i8, i8* %55, i64 %56
store i8 %54, i8* %58
%59 = load i64, i64* %13
%60 = load i64, i64* %14
%61 = icmp uge i64 %59, %60
br i1 %61, label %62, label %87
62: ; preds = %52
%63 = load i64, i64* %14
%64 = mul i64 %63, 2
%65 = call i64 @malloc(i64 %64)
%66 = inttoptr i64 %65 to i8*
store i8* %66, i8** %12
%67 = load i8*, i8** %12
%68 = icmp eq i8* %67, null
br i1 %68, label %69, label %77
69: ; preds = %62
%70 = load i64*, i64** %8
%71 = ptrtoint i64* %70 to i64
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %71)
%73 = load i8*, i8** %11
%74 = call i32 @free(i8* %73)
%75 = load i32*, i32** %10
%76 = call i32 @fclose(i32* %75)
store i32 -1, i32* %5
br label %101
77: ; preds = %62
%78 = load i8*, i8** %12
%79 = load i8*, i8** %11
%80 = load i64, i64* %14
%81 = call i32 @memcpy(i8* %78, i8* %79, i64 %80)
%82 = load i8*, i8** %11
%83 = call i32 @free(i8* %82)
%84 = load i8*, i8** %12
store i8* %84, i8** %11
%85 = load i64, i64* %14
%86 = mul i64 %85, 2
store i64 %86, i64* %14
br label %87
87: ; preds = %77, %52
%88 = load i32*, i32** %10
%89 = call i32 @fgetc(i32* %88)
store i32 %89, i32* %15
br label %38
90: ; preds = %45
%91 = load i32*, i32** %10
%92 = call i32 @fclose(i32* %91)
%93 = load i8*, i8** %11
%94 = load i8**, i8*** %7
store i8* %93, i8** %94
%95 = load i64*, i64** %8
%96 = icmp ne i64* %95, null
br i1 %96, label %97, label %100
97: ; preds = %90
%98 = load i64, i64* %13
%99 = load i64*, i64** %8
store i64 %98, i64* %99
br label %100
100: ; preds = %97, %90
store i32 0, i32* %5
br label %101
101: ; preds = %100, %69, %29, %20
%102 = load i32, i32* %5
ret i32 %102
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Could not open %s\0A\00"
@.str.2 = [40 x i8] c"Could not allocate %lld bytes of memory\00"
@EOF = global i32 0
define i32 @util_proc_read_special_file(i8* %0, i8** nocapture writeonly %1, i64* %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 %9
7: ; preds = %4
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %64
9: ; preds = %4
%10 = tail call i64 @malloc(i64 1024)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = ptrtoint i64* %2 to i64
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %13)
%15 = tail call i32 @fclose(i32* %5)
br label %64
16: ; preds = %9
%17 = inttoptr i64 %10 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 %58
23: ; preds = %16, %50
%24 = phi i32 [ %54, %50 ], [ %19, %16 ]
%25 = phi i32 [ %53, %50 ], [ %18, %16 ]
%26 = phi i64 [ %52, %50 ], [ 1024, %16 ]
%27 = phi i64 [ %34, %50 ], [ 0, %16 ]
%28 = phi i8* [ %51, %50 ], [ %17, %16 ]
%29 = phi i32 [ %31, %50 ], [ %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 %50, label %37
37: ; preds = %23
%38 = shl i64 %26, 1
%39 = tail call i64 @malloc(i64 %38)
%40 = inttoptr i64 %39 to i8*
%41 = icmp eq i64 %39, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = ptrtoint i64* %2 to i64
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %43)
%45 = tail call i32 @free(i8* %28)
%46 = tail call i32 @fclose(i32* %5)
br label %64
47: ; preds = %37
%48 = tail call i32 @memcpy(i8* %40, i8* %28, i64 %26)
%49 = tail call i32 @free(i8* %28)
br label %50
50: ; preds = %47, %23
%51 = phi i8* [ %40, %47 ], [ %28, %23 ]
%52 = phi i64 [ %38, %47 ], [ %26, %23 ]
%53 = tail call i32 @fgetc(i32* %5)
%54 = load i32, i32* @EOF
%55 = icmp ne i32 %53, %54
%56 = icmp ne i32 %31, 0
%57 = select i1 %55, i1 true, i1 %56
br i1 %57, label %23, label %58
58: ; preds = %50, %16
%59 = phi i8* [ %17, %16 ], [ %51, %50 ]
%60 = phi i64 [ 0, %16 ], [ %34, %50 ]
%61 = tail call i32 @fclose(i32* %5)
store i8* %59, i8** %1
%62 = icmp eq i64* %2, null
br i1 %62, label %64, label %63
63: ; preds = %58
store i64 %60, i64* %2
br label %64
64: ; preds = %58, %63, %42, %12, %7
%65 = phi i32 [ -1, %7 ], [ -1, %12 ], [ -1, %42 ], [ 0, %63 ], [ 0, %58 ]
ret i32 %65
}