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

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ keyval_append_error_va(char *, char const *);
struct keyval_node *keyval_parse_string(char *);
char *realloc(char *, scalar_t__);
char *strcat(char *, char *);
scalar_t__ strlen(char *);
struct keyval_node *keyval_parse_file(const char *filename) {
  char buf[512];
  FILE *file = fopen(filename, "r");
  char *data;
  struct keyval_node *node;
  if (!file) {
    keyval_append_error_va("keyval: error: could not open file `%s'\n",
                           filename);
    return 0;
  }
  data = calloc(512, sizeof(char));
  while (fgets(buf, 512, file)) {
    data = realloc(data, strlen(data) + strlen(buf) + 1);
    data = strcat(data, buf);
  }
  fclose(file);
  node = keyval_parse_string(data);
  free(data);
  return node;
}


x86 assembly (clang, -O0):
<keyval_parse_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    11f1 <keyval_parse_file+0x51>
mov    -0x10(%rbp),%rsi
lea    0xe23(%rip),%rdi
call   1050 <keyval_append_error_va@plt>
movq   $0x0,-0x8(%rbp)
jmp    12cc <keyval_parse_file+0x12c>
mov    $0x200,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x220(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rdx
mov    $0x200,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1296 <keyval_parse_file+0xf6>
mov    -0x220(%rbp),%rax
mov    %rax,-0x230(%rbp)
mov    -0x220(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x238(%rbp)
lea    -0x210(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x238(%rbp),%rsi
mov    -0x230(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   10a0 <realloc@plt>
mov    %rax,-0x220(%rbp)
mov    -0x220(%rbp),%rdi
lea    -0x210(%rbp),%rsi
call   10c0 <strcat@plt>
mov    %rax,-0x220(%rbp)
jmp    1207 <keyval_parse_file+0x67>
mov    -0x218(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x220(%rbp),%rdi
call   1090 <keyval_parse_string@plt>
mov    %rax,-0x228(%rbp)
mov    -0x220(%rbp),%rdi
call   1030 <free@plt>
mov    -0x228(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keyval_parse_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%rbx
lea    0xe48(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1263 <keyval_parse_file+0xc3>
mov    %rax,%r14
mov    $0x200,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1246 <keyval_parse_file+0xa6>
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    (%r12,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   10a0 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   10c0 <strcat@plt>
mov    %r15,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1200 <keyval_parse_file+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1090 <keyval_parse_string@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1275 <keyval_parse_file+0xd5>
lea    0xd98(%rip),%rdi
mov    %rbx,%rsi
call   1050 <keyval_append_error_va@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.keyval_node = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [41 x i8] c"keyval: error: could not open file `%s'\0A\00"
define %struct.keyval_node* @keyval_parse_file(i8* %0) {
%2 = alloca %struct.keyval_node*
%3 = alloca i8*
%4 = alloca [512 x i8]
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca %struct.keyval_node*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @keyval_append_error_va(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store %struct.keyval_node* null, %struct.keyval_node** %2
br label %42
15: ; preds = %1
%16 = call i8* @calloc(i32 512, i32 1)
store i8* %16, i8** %6
br label %17
17: ; preds = %22, %15
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %5
%20 = call i64 @fgets(i8* %18, i32 512, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %17
%23 = load i8*, i8** %6
%24 = load i8*, i8** %6
%25 = call i64 @strlen(i8* %24)
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%27 = call i64 @strlen(i8* %26)
%28 = add nsw i64 %25, %27
%29 = add nsw i64 %28, 1
%30 = call i8* @realloc(i8* %23, i64 %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%33 = call i8* @strcat(i8* %31, i8* %32)
store i8* %33, i8** %6
br label %17
34: ; preds = %17
%35 = load i32*, i32** %5
%36 = call i32 @fclose(i32* %35)
%37 = load i8*, i8** %6
%38 = call %struct.keyval_node* @keyval_parse_string(i8* %37)
store %struct.keyval_node* %38, %struct.keyval_node** %7
%39 = load i8*, i8** %6
%40 = call i32 @free(i8* %39)
%41 = load %struct.keyval_node*, %struct.keyval_node** %7
store %struct.keyval_node* %41, %struct.keyval_node** %2
br label %42
42: ; preds = %34, %12
%43 = load %struct.keyval_node*, %struct.keyval_node** %2
ret %struct.keyval_node* %43
}

LLVM IR (-O2):
%struct.keyval_node = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [41 x i8] c"keyval: error: could not open file `%s'\0A\00"
define %struct.keyval_node* @keyval_parse_file(i8* %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%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 %6, label %8
6: ; preds = %1
%7 = tail call i32 @keyval_append_error_va(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
8: ; preds = %1
%9 = tail call dereferenceable_or_null(512) i8* @calloc(i32 512, i32 1)
%10 = call i64 @fgets(i8* %3, i32 512, i32* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %22, label %12
12: ; preds = %8, %12
%13 = phi i8* [ %18, %12 ], [ %9, %8 ]
%14 = call i64 @strlen(i8* dereferenceable(1) %13)
%15 = call i64 @strlen(i8* %3)
%16 = add i64 %14, 1
%17 = add i64 %16, %15
%18 = call i8* @realloc(i8* %13, i64 %17)
%19 = call i8* @strcat(i8* dereferenceable(1) %18, i8* %3)
%20 = call i64 @fgets(i8* %3, i32 512, i32* %4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %12
22: ; preds = %12, %8
%23 = phi i8* [ %9, %8 ], [ %18, %12 ]
%24 = call i32 @fclose(i32* %4)
%25 = call %struct.keyval_node* @keyval_parse_string(i8* %23)
%26 = call i32 @free(i8* %23)
br label %27
27: ; preds = %22, %6
%28 = phi %struct.keyval_node* [ %25, %22 ], [ null, %6 ]
ret %struct.keyval_node* %28
}