#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*/ exit(int);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int *);
char *malloc(int);
char *malloc_or_die(int);
int /*<<< orphan*/ memcpy(char *, int *, int);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
int lvl_load_read_line(FILE *f, char **buffer, int *length) {
  int c;
  struct {
    char *data;
    int size, cap;
  } result = {NULL, 0, 0};
  while ((c = fgetc(f)) != '\n' && c != EOF) {
    do {
      if ((result).cap == 0) {
        (result).data = malloc(sizeof(*((result).data)));
        if (!(result).data) {
          fprintf(stderr, "Allocation failure.\n");
          exit(1);
        }
        (result).cap = 1;
      } else if ((result).cap == (result).size) {
        (result).cap *= 2;
        (result).data =
            realloc((result).data, (result).cap * sizeof(*((result).data)));
        if (!(result).data) {
          fprintf(stderr, "Allocation failure.\n");
          exit(1);
        }
      }
      (result).data[(result).size++] = (c);
    } while (0);
  }
  *length = result.size;
  *buffer = malloc_or_die(result.size);
  memcpy(*buffer, result.data, result.size);
  do {
    free((result).data);
    (result).data = NULL;
    (result).size = 0;
    (result).cap = 0;
  } while (0);
  return c;
}


x86 assembly (clang, -O0):
<lvl_load_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x31(%rbp)
je     11e2 <lvl_load_read_line+0x52>
mov    -0x1c(%rbp),%eax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11f2 <lvl_load_read_line+0x62>
jmp    12c4 <lvl_load_read_line+0x134>
jmp    11f7 <lvl_load_read_line+0x67>
cmpl   $0x0,-0x24(%rbp)
jne    1245 <lvl_load_read_line+0xb5>
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1239 <lvl_load_read_line+0xa9>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x1,-0x24(%rbp)
jmp    12a5 <lvl_load_read_line+0x115>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    12a0 <lvl_load_read_line+0x110>
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   10a0 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    129b <lvl_load_read_line+0x10b>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12a0 <lvl_load_read_line+0x110>
jmp    12a5 <lvl_load_read_line+0x115>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11b4 <lvl_load_read_line+0x24>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%edi
call   1090 <malloc_or_die@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lvl_load_read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r13
call   1040 <fgetc@plt>
mov    %eax,%ebp
xor    %r12d,%r12d
cmp    $0xa,%eax
je     122b <lvl_load_read_line+0xab>
mov    %r15,0x8(%rsp)
mov    0x2e32(%rip),%r15
cmp    (%r15),%ebp
je     122f <lvl_load_read_line+0xaf>
mov    %r14,0x10(%rsp)
xor    %ebx,%ebx
xor    %r14d,%r14d
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
test   %r14d,%r14d
je     11f0 <lvl_load_read_line+0x70>
mov    %r14d,%eax
cmp    %rax,%rbx
jne    1208 <lvl_load_read_line+0x88>
add    %r14d,%r14d
mov    %r12,%rdi
mov    %r14d,%esi
call   1090 <realloc@plt>
jmp    1200 <lvl_load_read_line+0x80>
nopl   (%rax)
mov    $0x1,%r14d
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     1269 <lvl_load_read_line+0xe9>
mov    %bpl,(%r12,%rbx,1)
add    $0x1,%rbx
mov    %r13,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
cmp    $0xa,%eax
je     1224 <lvl_load_read_line+0xa4>
cmp    (%r15),%ebp
jne    11d0 <lvl_load_read_line+0x50>
mov    0x10(%rsp),%r14
jmp    1231 <lvl_load_read_line+0xb1>
xor    %ebx,%ebx
jmp    1236 <lvl_load_read_line+0xb6>
xor    %ebx,%ebx
mov    0x8(%rsp),%r15
mov    %ebx,(%r15)
mov    %ebx,%edi
call   1080 <malloc_or_die@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.anon = type { i8*, i32, i32 }
@EOF = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Allocation failure.\0A\00"
define i32 @lvl_load_read_line(i32* %0, i8** %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.anon
store i32* %0, i32** %4
store i8** %1, i8*** %5
store i32* %2, i32** %6
%9 = bitcast %struct.anon* %8 to i8*
br label %10
10: ; preds = %75, %3
%11 = load i32*, i32** %4
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* %7
%13 = icmp ne i32 %12, 10
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* %7
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br label %18
18: ; preds = %14, %10
%19 = phi i1 [ false, %10 ], [ %17, %14 ]
br i1 %19, label %20, label %76
20: ; preds = %18
br label %21
21: ; preds = %20
%22 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 2
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = call i8* @malloc(i32 1)
%27 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
store i8* %26, i8** %27
%28 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = icmp ne i8* %29, null
br i1 %30, label %35, label %31
31: ; preds = %25
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %25
%36 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 2
store i32 1, i32* %36
br label %65
37: ; preds = %21
%38 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 2
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 1
%41 = load i32, i32* %40
%42 = icmp eq i32 %39, %41
br i1 %42, label %43, label %64
43: ; preds = %37
%44 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 2
%45 = load i32, i32* %44
%46 = mul nsw i32 %45, 2
store i32 %46, i32* %44
%47 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 2
%50 = load i32, i32* %49
%51 = sext i32 %50 to i64
%52 = mul i64 %51, 1
%53 = trunc i64 %52 to i32
%54 = call i8* @realloc(i8* %48, i32 %53)
%55 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
store i8* %54, i8** %55
%56 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = icmp ne i8* %57, null
br i1 %58, label %63, label %59
59: ; preds = %43
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %43
br label %64
64: ; preds = %63, %37
br label %65
65: ; preds = %64, %35
%66 = load i32, i32* %7
%67 = trunc i32 %66 to i8
%68 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
%69 = load i8*, i8** %68
%70 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 1
%71 = load i32, i32* %70
%72 = add nsw i32 %71, 1
store i32 %72, i32* %70
%73 = sext i32 %71 to i64
%74 = getelementptr inbounds i8, i8* %69, i64 %73
store i8 %67, i8* %74
br label %75
75: ; preds = %65
br label %10
76: ; preds = %18
%77 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 1
%78 = load i32, i32* %77
%79 = load i32*, i32** %6
store i32 %78, i32* %79
%80 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 1
%81 = load i32, i32* %80
%82 = call i8* @malloc_or_die(i32 %81)
%83 = load i8**, i8*** %5
store i8* %82, i8** %83
%84 = load i8**, i8*** %5
%85 = load i8*, i8** %84
%86 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
%87 = load i8*, i8** %86
%88 = bitcast i8* %87 to i32*
%89 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 1
%90 = load i32, i32* %89
%91 = call i32 @memcpy(i8* %85, i32* %88, i32 %90)
br label %92
92: ; preds = %76
%93 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
%94 = load i8*, i8** %93
%95 = bitcast i8* %94 to i32*
%96 = call i32 @free(i32* %95)
%97 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 0
store i8* null, i8** %97
%98 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 1
store i32 0, i32* %98
%99 = getelementptr inbounds %struct.anon, %struct.anon* %8, i32 0, i32 2
store i32 0, i32* %99
br label %100
100: ; preds = %92
%101 = load i32, i32* %7
ret i32 %101
}

LLVM IR (-O2):
@EOF = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Allocation failure.\0A\00"
define i32 @lvl_load_read_line(i32* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = tail call i32 @fgetc(i32* %0)
%5 = icmp ne i32 %4, 10
%6 = load i32, i32* @EOF
%7 = icmp ne i32 %4, %6
%8 = select i1 %5, i1 %7, i1 false
br i1 %8, label %9, label %46
9: ; preds = %3, %33
%10 = phi i64 [ %37, %33 ], [ 0, %3 ]
%11 = phi i32 [ %39, %33 ], [ %4, %3 ]
%12 = phi i32 [ %35, %33 ], [ 0, %3 ]
%13 = phi i8* [ %34, %33 ], [ null, %3 ]
%14 = icmp eq i32 %12, 0
br i1 %14, label %15, label %22
15: ; preds = %9
%16 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %33
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %9
%23 = zext i32 %12 to i64
%24 = icmp eq i64 %10, %23
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = shl nsw i32 %12, 1
%27 = tail call i8* @realloc(i8* %13, i32 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %15, %22, %25
%34 = phi i8* [ %27, %25 ], [ %13, %22 ], [ %16, %15 ]
%35 = phi i32 [ %26, %25 ], [ %12, %22 ], [ 1, %15 ]
%36 = trunc i32 %11 to i8
%37 = add nuw i64 %10, 1
%38 = getelementptr inbounds i8, i8* %34, i64 %10
store i8 %36, i8* %38
%39 = tail call i32 @fgetc(i32* %0)
%40 = icmp ne i32 %39, 10
%41 = load i32, i32* @EOF
%42 = icmp ne i32 %39, %41
%43 = select i1 %40, i1 %42, i1 false
br i1 %43, label %9, label %44
44: ; preds = %33
%45 = trunc i64 %37 to i32
br label %46
46: ; preds = %44, %3
%47 = phi i8* [ null, %3 ], [ %34, %44 ]
%48 = phi i32 [ 0, %3 ], [ %45, %44 ]
%49 = phi i32 [ %4, %3 ], [ %39, %44 ]
store i32 %48, i32* %2
%50 = tail call i8* @malloc_or_die(i32 %48)
store i8* %50, i8** %1
%51 = bitcast i8* %47 to i32*
%52 = tail call i32 @memcpy(i8* %50, i32* %51, i32 %48)
%53 = tail call i32 @free(i32* %51)
ret i32 %49
}