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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
struct INI *_ini_open_mem(char *, size_t, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
struct INI *ini_open(const char *file) {
  FILE *f;
  char *buf, *ptr;
  size_t len, left;
  struct INI *ini = NULL;
  f = fopen(file, "r");
  if (!f) {
    perror("Unable to open file");
    return NULL;
  }
  fseek(f, 0, SEEK_END);
  len = ftell(f);
  if (!len) {
    fprintf(stderr, "ERROR: File is empty\n");
    goto error_fclose;
  }
  buf = malloc(len);
  if (!buf) {
    perror("Unable to allocate memory");
    goto error_fclose;
  }
  rewind(f);
  for (left = len, ptr = buf; left;) {
    size_t tmp = fread(ptr, 1, left, f);
    if (tmp == 0) {
      if (feof(f))
        break;
      free(buf);
      perror("Unable to read file");
      goto error_fclose;
    }
    left -= tmp;
    ptr += tmp;
  }
  ini = _ini_open_mem(buf, len - left, true);
error_fclose:
  fclose(f);
  return ini;
}


x86 assembly (clang, -O0):
<ini_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120c <ini_open+0x4c>
lea    0xe08(%rip),%rdi
call   10e0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1341 <ini_open+0x181>
mov    -0x18(%rbp),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1252 <ini_open+0x92>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1330 <ini_open+0x170>
mov    -0x30(%rbp),%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    127b <ini_open+0xbb>
lea    0xdbb(%rip),%rdi
call   10e0 <perror@plt>
jmp    1330 <ini_open+0x170>
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1316 <ini_open+0x156>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12f6 <ini_open+0x136>
mov    -0x18(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%rax
je     12dc <ini_open+0x11c>
jmp    1316 <ini_open+0x156>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xd5a(%rip),%rdi
call   10e0 <perror@plt>
jmp    1330 <ini_open+0x170>
mov    -0x48(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1294 <ini_open+0xd4>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
sub    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <_ini_open_mem@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ini_open>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xe30(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     12a6 <ini_open+0xe6>
mov    %rax,%r12
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %r12,%rdi
call   1080 <ftell@plt>
test   %rax,%rax
je     1255 <ini_open+0x95>
mov    %rax,%r14
mov    %rax,%rdi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12b6 <ini_open+0xf6>
mov    %rax,%r15
mov    %r12,%rdi
call   1060 <rewind@plt>
mov    %r14,%r13
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r13,%rdx
mov    %r12,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     126e <ini_open+0xae>
add    %rax,%rbx
sub    %rax,%r13
jne    1230 <ini_open+0x70>
xor    %r13d,%r13d
jmp    127b <ini_open+0xbb>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
call   1070 <fprintf@plt>
xor    %ebx,%ebx
jmp    1291 <ini_open+0xd1>
mov    %r12,%rdi
call   1090 <feof@plt>
test   %rax,%rax
je     12bf <ini_open+0xff>
sub    %r13,%r14
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <_ini_open_mem@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd55(%rip),%rdi
call   10e0 <perror@plt>
xor    %ebx,%ebx
jmp    1299 <ini_open+0xd9>
lea    0xd6f(%rip),%rdi
jmp    12ce <ini_open+0x10e>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xd78(%rip),%rdi
call   10e0 <perror@plt>
xor    %ebx,%ebx
jmp    1291 <ini_open+0xd1>

LLVM IR (-O0):
%struct.INI = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"Unable to open file\00"
@SEEK_END = global i32 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"ERROR: File is empty\0A\00"
@.str.3 = [26 x i8] c"Unable to allocate memory\00"
@.str.4 = [20 x i8] c"Unable to read file\00"
define %struct.INI* @ini_open(i8* %0) {
%2 = alloca %struct.INI*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.INI*
%10 = alloca i64
store i8* %0, i8** %3
store %struct.INI* null, %struct.INI** %9
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store %struct.INI* null, %struct.INI** %2
br label %76
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %4
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = icmp ne i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %72
28: ; preds = %17
%29 = load i64, i64* %7
%30 = call i8* @malloc(i64 %29)
store i8* %30, i8** %5
%31 = load i8*, i8** %5
%32 = icmp ne i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %72
35: ; preds = %28
%36 = load i32*, i32** %4
%37 = call i32 @rewind(i32* %36)
%38 = load i64, i64* %7
store i64 %38, i64* %8
%39 = load i8*, i8** %5
store i8* %39, i8** %6
br label %40
40: ; preds = %59, %35
%41 = load i64, i64* %8
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %66
43: ; preds = %40
%44 = load i8*, i8** %6
%45 = load i64, i64* %8
%46 = load i32*, i32** %4
%47 = call i64 @fread(i8* %44, i32 1, i64 %45, i32* %46)
store i64 %47, i64* %10
%48 = load i64, i64* %10
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %43
%51 = load i32*, i32** %4
%52 = call i64 @feof(i32* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
br label %66
55: ; preds = %50
%56 = load i8*, i8** %5
%57 = call i32 @free(i8* %56)
%58 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %72
59: ; preds = %43
%60 = load i64, i64* %10
%61 = load i64, i64* %8
%62 = sub i64 %61, %60
store i64 %62, i64* %8
%63 = load i64, i64* %10
%64 = load i8*, i8** %6
%65 = getelementptr inbounds i8, i8* %64, i64 %63
store i8* %65, i8** %6
br label %40
66: ; preds = %54, %40
%67 = load i8*, i8** %5
%68 = load i64, i64* %7
%69 = load i64, i64* %8
%70 = sub i64 %68, %69
%71 = call %struct.INI* @_ini_open_mem(i8* %67, i64 %70, i32 1)
store %struct.INI* %71, %struct.INI** %9
br label %72
72: ; preds = %66, %55, %33, %25
%73 = load i32*, i32** %4
%74 = call i32 @fclose(i32* %73)
%75 = load %struct.INI*, %struct.INI** %9
store %struct.INI* %75, %struct.INI** %2
br label %76
76: ; preds = %72, %15
%77 = load %struct.INI*, %struct.INI** %2
ret %struct.INI* %77
}

LLVM IR (-O2):
%struct.INI = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"Unable to open file\00"
@SEEK_END = global i32 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"ERROR: File is empty\0A\00"
@.str.3 = [26 x i8] c"Unable to allocate memory\00"
@.str.4 = [20 x i8] c"Unable to read file\00"
define %struct.INI* @ini_open(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %43
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i32 0, i32 %7)
%9 = tail call i64 @ftell(i32* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %40
14: ; preds = %6
%15 = tail call i8* @malloc(i64 %9)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %40
19: ; preds = %14
%20 = tail call i32 @rewind(i32* %2)
br label %21
21: ; preds = %19, %32
%22 = phi i64 [ %33, %32 ], [ %9, %19 ]
%23 = phi i8* [ %34, %32 ], [ %15, %19 ]
%24 = tail call i64 @fread(i8* %23, i32 1, i64 %22, i32* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = tail call i64 @feof(i32* %2)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = tail call i32 @free(i8* %15)
%31 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %40
32: ; preds = %21
%33 = sub i64 %22, %24
%34 = getelementptr inbounds i8, i8* %23, i64 %24
%35 = icmp eq i64 %33, 0
br i1 %35, label %36, label %21
36: ; preds = %32, %26
%37 = phi i64 [ %22, %26 ], [ 0, %32 ]
%38 = sub i64 %9, %37
%39 = tail call %struct.INI* @_ini_open_mem(i8* %15, i64 %38, i32 1)
br label %40
40: ; preds = %29, %36, %17, %11
%41 = phi %struct.INI* [ %39, %36 ], [ null, %17 ], [ null, %11 ], [ null, %29 ]
%42 = tail call i32 @fclose(i32* %2)
br label %43
43: ; preds = %40, %4
%44 = phi %struct.INI* [ %41, %40 ], [ null, %4 ]
ret %struct.INI* %44
}