#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 %zu bytes of memory", current_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 %zu bytes of memory", current_size * 2);
        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    1359 <util_proc_read_special_file+0x1e9>
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    -0x50(%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    1359 <util_proc_read_special_file+0x1e9>
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    1328 <util_proc_read_special_file+0x1b8>
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     1317 <util_proc_read_special_file+0x1a7>
mov    -0x50(%rbp),%rdi
shl    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12e9 <util_proc_read_special_file+0x179>
mov    -0x50(%rbp),%rsi
shl    $0x1,%rsi
lea    0xd51(%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    1359 <util_proc_read_special_file+0x1e9>
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     1352 <util_proc_read_special_file+0x1e2>
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    $0x28,%rsp
mov    %ecx,%ebx
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe70(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11db <util_proc_read_special_file+0x6b>
mov    %rax,%r15
mov    $0x400,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11f1 <util_proc_read_special_file+0x81>
mov    %rax,%r14
mov    %r15,%rdi
call   1060 <fgetc@plt>
mov    0x2e2a(%rip),%r13
mov    0x0(%r13),%ecx
cmp    %ecx,%eax
mov    %rbp,0x18(%rsp)
mov    %r12,0x10(%rsp)
jne    120c <util_proc_read_special_file+0x9c>
test   %ebx,%ebx
jne    120c <util_proc_read_special_file+0x9c>
xor    %ebp,%ebp
jmp    1299 <util_proc_read_special_file+0x129>
lea    0xe20(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12dd <util_proc_read_special_file+0x16d>
lea    0xe1d(%rip),%rdi
mov    $0x400,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
jmp    12d8 <util_proc_read_special_file+0x168>
mov    $0x400,%r12d
xor    %ebp,%ebp
mov    %r15,0x8(%rsp)
nopl   0x0(%rax)
cmp    %ecx,%eax
mov    $0x0,%ecx
cmove  %ecx,%ebx
cmove  %ecx,%eax
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %r12,%rbp
jb     1285 <util_proc_read_special_file+0x115>
mov    %rbp,0x20(%rsp)
mov    %r14,%r15
mov    %ebx,%r14d
lea    (%r12,%r12,1),%rbp
mov    %rbp,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     12ba <util_proc_read_special_file+0x14a>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <memcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
mov    %rbp,%r12
mov    0x8(%rsp),%r15
mov    %r14d,%ebx
mov    %rax,%r14
mov    0x20(%rsp),%rbp
mov    %r15,%rdi
call   1060 <fgetc@plt>
mov    0x0(%r13),%ecx
cmp    %ecx,%eax
jne    1220 <util_proc_read_special_file+0xb0>
test   %ebx,%ebx
jne    1220 <util_proc_read_special_file+0xb0>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%rax
mov    %r14,(%rax)
xor    %eax,%eax
mov    0x18(%rsp),%rcx
test   %rcx,%rcx
je     12e2 <util_proc_read_special_file+0x172>
mov    %rbp,(%rcx)
jmp    12e2 <util_proc_read_special_file+0x172>
lea    0xd54(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x28,%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 = [39 x i8] c"Could not allocate %zu 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 %100
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 %34
29: ; preds = %23
%30 = load i64, i64* %14
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i64 %30)
%32 = load i32*, i32** %10
%33 = call i32 @fclose(i32* %32)
store i32 -1, i32* %5
br label %100
34: ; preds = %23
%35 = load i32*, i32** %10
%36 = call i32 @fgetc(i32* %35)
store i32 %36, i32* %15
br label %37
37: ; preds = %86, %34
%38 = load i32, i32* %15
%39 = load i32, i32* @EOF
%40 = icmp ne i32 %38, %39
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = load i32, i32* %9
%43 = icmp ne i32 %42, 0
br label %44
44: ; preds = %41, %37
%45 = phi i1 [ true, %37 ], [ %43, %41 ]
br i1 %45, label %46, label %89
46: ; preds = %44
%47 = load i32, i32* %15
%48 = load i32, i32* @EOF
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 0, i32* %15
store i32 0, i32* %9
br label %51
51: ; preds = %50, %46
%52 = load i32, i32* %15
%53 = trunc i32 %52 to i8
%54 = load i8*, i8** %11
%55 = load i64, i64* %13
%56 = add i64 %55, 1
store i64 %56, i64* %13
%57 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 %53, i8* %57
%58 = load i64, i64* %13
%59 = load i64, i64* %14
%60 = icmp uge i64 %58, %59
br i1 %60, label %61, label %86
61: ; preds = %51
%62 = load i64, i64* %14
%63 = mul i64 %62, 2
%64 = call i64 @malloc(i64 %63)
%65 = inttoptr i64 %64 to i8*
store i8* %65, i8** %12
%66 = load i8*, i8** %12
%67 = icmp eq i8* %66, null
br i1 %67, label %68, label %76
68: ; preds = %61
%69 = load i64, i64* %14
%70 = mul i64 %69, 2
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i64 %70)
%72 = load i8*, i8** %11
%73 = call i32 @free(i8* %72)
%74 = load i32*, i32** %10
%75 = call i32 @fclose(i32* %74)
store i32 -1, i32* %5
br label %100
76: ; preds = %61
%77 = load i8*, i8** %12
%78 = load i8*, i8** %11
%79 = load i64, i64* %14
%80 = call i32 @memcpy(i8* %77, i8* %78, i64 %79)
%81 = load i8*, i8** %11
%82 = call i32 @free(i8* %81)
%83 = load i8*, i8** %12
store i8* %83, i8** %11
%84 = load i64, i64* %14
%85 = mul i64 %84, 2
store i64 %85, i64* %14
br label %86
86: ; preds = %76, %51
%87 = load i32*, i32** %10
%88 = call i32 @fgetc(i32* %87)
store i32 %88, i32* %15
br label %37
89: ; preds = %44
%90 = load i32*, i32** %10
%91 = call i32 @fclose(i32* %90)
%92 = load i8*, i8** %11
%93 = load i8**, i8*** %7
store i8* %92, i8** %93
%94 = load i64*, i64** %8
%95 = icmp ne i64* %94, null
br i1 %95, label %96, label %99
96: ; preds = %89
%97 = load i64, i64* %13
%98 = load i64*, i64** %8
store i64 %97, i64* %98
br label %99
99: ; preds = %96, %89
store i32 0, i32* %5
br label %100
100: ; preds = %99, %68, %29, %20
%101 = load i32, i32* %5
ret i32 %101
}

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