#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 */
struct Heap {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char EOF;
char *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
char getc(int /*<<< orphan*/ *);
int heap_add(char *, struct Heap *);
int /*<<< orphan*/ printf(char *);
void ReadFile(struct Heap *heap) {
  FILE *fr;
  fr = fopen("p022_names.txt", "r");
  if (fr == NULL) {
    printf("ERROR opening file FILE_NAME\n");
    exit(1);
  }
  char *buffer, *ref;
  int error = 0;
  while (error >= 0) {
    buffer = calloc(20, sizeof(char));
    if (buffer == NULL) {
      printf("ERROR: Out of memory\n");
      exit(1);
    }
    ref = buffer;
    *ref = (char)getc(fr);
    while ((*ref != ',') && (*ref != EOF)) {
      if (((*ref >= 'A') && (*ref <= 'Z')) || (*ref == ','))
        ref++;
      *ref = getc(fr);
    }
    *ref = 0;
    error = heap_add(buffer, heap);
  }
  return;
}


x86 assembly (clang, -O0):
<ReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a4 <ReadFile+0x44>
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     12ae <ReadFile+0x14e>
mov    $0x14,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e9 <ReadFile+0x89>
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <getc@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2c,%ecx
mov    %al,-0x25(%rbp)
je     1230 <ReadFile+0xd0>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
mov    0x2dc3(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1240 <ReadFile+0xe0>
jmp    1292 <ReadFile+0x132>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x41,%eax
jl     1260 <ReadFile+0x100>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5a,%eax
jle    1270 <ReadFile+0x110>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2c,%eax
jne    127c <ReadFile+0x11c>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <getc@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
jmp    1202 <ReadFile+0xa2>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <heap_add@plt>
mov    %eax,-0x24(%rbp)
jmp    11ab <ReadFile+0x4b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadFile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
lea    0xe8d(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     122e <ReadFile+0xce>
mov    %rax,%r13
mov    0x2e56(%rip),%r12
jmp    11b2 <ReadFile+0x52>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movb   $0x0,(%rbx)
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <heap_add@plt>
test   %eax,%eax
js     120e <ReadFile+0xae>
mov    $0x14,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     1218 <ReadFile+0xb8>
mov    %rax,%r15
mov    %r13,%rdi
call   1080 <getc@plt>
mov    %al,(%r15)
mov    %r15,%rbx
cmp    $0x2c,%al
je     11a0 <ReadFile+0x40>
mov    %r15,%rbx
cmp    (%r12),%al
je     11a0 <ReadFile+0x40>
mov    %r15,%rbx
nopw   0x0(%rax,%rax,1)
add    $0xbf,%al
cmp    $0x1a,%al
adc    $0x0,%rbx
mov    %r13,%rdi
call   1080 <getc@plt>
mov    %al,(%rbx)
cmp    $0x2c,%al
je     11a0 <ReadFile+0x40>
cmp    (%r12),%al
jne    11f0 <ReadFile+0x90>
jmp    11a0 <ReadFile+0x40>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdf1(%rip),%rdi
jmp    121f <ReadFile+0xbf>

LLVM IR (-O0):
%struct.Heap = type { i32 }
@.str = [15 x i8] c"p022_names.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"ERROR opening file FILE_NAME\0A\00"
@.str.3 = [22 x i8] c"ERROR: Out of memory\0A\00"
@EOF = global i8 0
define void @ReadFile(%struct.Heap* %0) {
%2 = alloca %struct.Heap*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store %struct.Heap* %0, %struct.Heap** %2
%7 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
store i32 0, i32* %6
br label %14
14: ; preds = %65, %13
%15 = load i32, i32* %6
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %70
17: ; preds = %14
%18 = call i8* @calloc(i32 20, i32 1)
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = load i8*, i8** %4
store i8* %25, i8** %5
%26 = load i32*, i32** %3
%27 = call signext i8 @getc(i32* %26)
%28 = load i8*, i8** %5
store i8 %27, i8* %28
br label %29
29: ; preds = %61, %24
%30 = load i8*, i8** %5
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 44
br i1 %33, label %34, label %41
34: ; preds = %29
%35 = load i8*, i8** %5
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = load i8, i8* @EOF
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %37, %39
br label %41
41: ; preds = %34, %29
%42 = phi i1 [ false, %29 ], [ %40, %34 ]
br i1 %42, label %43, label %65
43: ; preds = %41
%44 = load i8*, i8** %5
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp sge i32 %46, 65
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = load i8*, i8** %5
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp sle i32 %51, 90
br i1 %52, label %58, label %53
53: ; preds = %48, %43
%54 = load i8*, i8** %5
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 44
br i1 %57, label %58, label %61
58: ; preds = %53, %48
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %5
br label %61
61: ; preds = %58, %53
%62 = load i32*, i32** %3
%63 = call signext i8 @getc(i32* %62)
%64 = load i8*, i8** %5
store i8 %63, i8* %64
br label %29
65: ; preds = %41
%66 = load i8*, i8** %5
store i8 0, i8* %66
%67 = load i8*, i8** %4
%68 = load %struct.Heap*, %struct.Heap** %2
%69 = call i32 @heap_add(i8* %67, %struct.Heap* %68)
store i32 %69, i32* %6
br label %14
70: ; preds = %14
ret void
}

LLVM IR (-O2):
%struct.Heap = type { i32 }
@.str = [15 x i8] c"p022_names.txt\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@str = [21 x i8] c"ERROR: Out of memory\00"
@str.4 = [29 x i8] c"ERROR opening file FILE_NAME\00"
define void @ReadFile(%struct.Heap* %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.4, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1, %31
%8 = tail call dereferenceable_or_null(20) i8* @calloc(i32 20, i32 1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %7
%14 = tail call signext i8 @getc(i32* %2)
store i8 %14, i8* %8
%15 = icmp eq i8 %14, 44
%16 = load i8, i8* @EOF
%17 = icmp eq i8 %14, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %31, label %19
19: ; preds = %13, %19
%20 = phi i8 [ %26, %19 ], [ %14, %13 ]
%21 = phi i8* [ %25, %19 ], [ %8, %13 ]
%22 = add i8 %20, -65
%23 = icmp ult i8 %22, 26
%24 = zext i1 %23 to i64
%25 = getelementptr i8, i8* %21, i64 %24
%26 = tail call signext i8 @getc(i32* %2)
store i8 %26, i8* %25
%27 = icmp eq i8 %26, 44
%28 = load i8, i8* @EOF
%29 = icmp eq i8 %26, %28
%30 = select i1 %27, i1 true, i1 %29
br i1 %30, label %31, label %19
31: ; preds = %19, %13
%32 = phi i8* [ %8, %13 ], [ %25, %19 ]
store i8 0, i8* %32
%33 = tail call i32 @heap_add(i8* %8, %struct.Heap* %0)
%34 = icmp sgt i32 %33, -1
br i1 %34, label %7, label %35
35: ; preds = %31
ret void
}