#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 */
scalar_t__ EOF;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
scalar_t__ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *);
void *realloc(char *, int);
char *readPacketPolicy(char *filename) {
  int i = 1;
  FILE *fairu;
  char *buffer = malloc(sizeof(char));
  char *policies = malloc(sizeof(char));
  void *bak_pointer;
  if (fairu = fopen(filename, "r")) {
    while ((fgetc(fairu) != EOF) && (!feof(fairu) || !ferror(fairu))) {
      if (bak_pointer = realloc(policies, (i + 1) * sizeof(char))) {
        policies = bak_pointer;
      } else {
        printf("HEAP CORRUPTED\n");
      }
      fscanf(fairu, "%s\n", buffer);
      memcpy((policies + i), buffer, sizeof(char));
      i++;
    }
  }
  return policies;
}


x86 assembly (clang, -O0):
<readPacketPolicy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe36(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12c4 <readPacketPolicy+0x134>
jmp    11e2 <readPacketPolicy+0x52>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %rax,%rcx
xor    %eax,%eax
mov    0x2df1(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x31(%rbp)
je     1236 <readPacketPolicy+0xa6>
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x32(%rbp)
je     1230 <readPacketPolicy+0xa0>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x32(%rbp)
mov    -0x32(%rbp),%al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1246 <readPacketPolicy+0xb6>
jmp    12bf <readPacketPolicy+0x12f>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   10a0 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1278 <readPacketPolicy+0xe8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1284 <readPacketPolicy+0xf4>
lea    0xd83(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd7f(%rip),%rsi
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
movslq -0xc(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1080 <memcpy@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11e2 <readPacketPolicy+0x52>
jmp    12c4 <readPacketPolicy+0x134>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readPacketPolicy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
lea    0xe41(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     11e4 <readPacketPolicy+0x54>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <fgetc@plt>
mov    0x2e0a(%rip),%r12
cmp    (%r12),%rax
jne    11f9 <readPacketPolicy+0x69>
mov    %rbx,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%r13d
jmp    124a <readPacketPolicy+0xba>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
lea    0xde8(%rip),%rsi
mov    %r14,%rdx
call   1050 <fscanf@plt>
mov    %rbp,%rdi
add    %r13,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1080 <memcpy@plt>
mov    %r15,%rdi
call   1060 <fgetc@plt>
add    $0x1,%r13
mov    %rbp,%rbx
cmp    (%r12),%rax
je     11e7 <readPacketPolicy+0x57>
mov    %r15,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     1262 <readPacketPolicy+0xd2>
mov    %r15,%rdi
call   1040 <ferror@plt>
test   %eax,%eax
jne    11e4 <readPacketPolicy+0x54>
lea    0x1(%r13),%esi
mov    %rbx,%rdi
call   10a0 <realloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1210 <readPacketPolicy+0x80>
lea    0xd89(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rbp
jmp    1210 <readPacketPolicy+0x80>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i64 0
@.str.1 = [16 x i8] c"HEAP CORRUPTED\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i8* @readPacketPolicy(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %2
store i32 1, i32* %3
%8 = call i8* @malloc(i32 1)
store i8* %8, i8** %5
%9 = call i8* @malloc(i32 1)
store i8* %9, i8** %6
%10 = load i8*, i8** %2
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %58
13: ; preds = %1
br label %14
14: ; preds = %45, %13
%15 = load i32*, i32** %4
%16 = call i64 @fgetc(i32* %15)
%17 = load i64, i64* @EOF
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %30
19: ; preds = %14
%20 = load i32*, i32** %4
%21 = call i32 @feof(i32* %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32*, i32** %4
%25 = call i32 @ferror(i32* %24)
%26 = icmp ne i32 %25, 0
%27 = xor i1 %26, true
br label %28
28: ; preds = %23, %19
%29 = phi i1 [ true, %19 ], [ %27, %23 ]
br label %30
30: ; preds = %28, %14
%31 = phi i1 [ false, %14 ], [ %29, %28 ]
br i1 %31, label %32, label %57
32: ; preds = %30
%33 = load i8*, i8** %6
%34 = load i32, i32* %3
%35 = add nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 1
%38 = trunc i64 %37 to i32
%39 = call i8* @realloc(i8* %33, i32 %38)
store i8* %39, i8** %7
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = load i8*, i8** %7
store i8* %42, i8** %6
br label %45
43: ; preds = %32
%44 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %45
45: ; preds = %43, %41
%46 = load i32*, i32** %4
%47 = load i8*, i8** %5
%48 = call i32 @fscanf(i32* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = load i8*, i8** %6
%50 = load i32, i32* %3
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8*, i8** %5
%54 = call i32 @memcpy(i8* %52, i8* %53, i32 1)
%55 = load i32, i32* %3
%56 = add nsw i32 %55, 1
store i32 %56, i32* %3
br label %14
57: ; preds = %30
br label %58
58: ; preds = %57, %1
%59 = load i8*, i8** %6
ret i8* %59
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i64 0
@.str.2 = [4 x i8] c"%s\0A\00"
@str = [15 x i8] c"HEAP CORRUPTED\00"
define i8* @readPacketPolicy(i8* nocapture %0) {
%2 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%3 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %33, label %6
6: ; preds = %1
%7 = tail call i64 @fgetc(i32* %4)
%8 = load i64, i64* @EOF
%9 = icmp eq i64 %7, %8
br i1 %9, label %33, label %10
10: ; preds = %6, %25
%11 = phi i64 [ %19, %25 ], [ 1, %6 ]
%12 = phi i8* [ %26, %25 ], [ %3, %6 ]
%13 = tail call i32 @feof(i32* %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = tail call i32 @ferror(i32* %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %10, %15
%19 = add nuw i64 %11, 1
%20 = trunc i64 %19 to i32
%21 = tail call i8* @realloc(i8* %12, i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %18, %23
%26 = phi i8* [ %12, %23 ], [ %21, %18 ]
%27 = tail call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%28 = getelementptr inbounds i8, i8* %26, i64 %11
%29 = tail call i32 @memcpy(i8* %28, i8* %2, i32 1)
%30 = tail call i64 @fgetc(i32* %4)
%31 = load i64, i64* @EOF
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %10
33: ; preds = %25, %15, %6, %1
%34 = phi i8* [ %3, %1 ], [ %3, %6 ], [ %26, %25 ], [ %12, %15 ]
ret i8* %34
}