#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 /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
char *realloc(char *, int);
char *GetNextLine(FILE *f) {
  char *linestr = NULL;
  int count = 0;
  linestr = (char *)malloc(1 * sizeof(char));
  if (linestr == NULL)
    return NULL;
  linestr[0] = '\0';
  while (1) {
    linestr = realloc(linestr, count + 2);
    if (linestr == NULL) {
      free(linestr);
      return NULL;
    }
    if (fgets(&(linestr[count]), 2, f) == NULL) {
      if (count == 0) {
        free(linestr);
        return NULL;
      }
    }
    if (linestr[count] == '\n') {
      linestr[count] = '\0';
      return linestr;
    }
    count++;
  }
}


x86 assembly (clang, -O0):
<GetNextLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1181 <GetNextLine+0x41>
movq   $0x0,-0x8(%rbp)
jmp    1240 <GetNextLine+0x100>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x2,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bc <GetNextLine+0x7c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1240 <GetNextLine+0x100>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
mov    $0x2,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1204 <GetNextLine+0xc4>
cmpl   $0x0,-0x1c(%rbp)
jne    11ff <GetNextLine+0xbf>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1240 <GetNextLine+0x100>
jmp    1204 <GetNextLine+0xc4>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1232 <GetNextLine+0xf2>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1240 <GetNextLine+0x100>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1188 <GetNextLine+0x48>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetNextLine>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x1,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11cd <GetNextLine+0x8d>
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    $0x2,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     11c1 <GetNextLine+0x81>
mov    %rax,%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%r15,%rbx,1),%r12
mov    %r12,%rdi
mov    $0x2,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    119e <GetNextLine+0x5e>
test   %rbx,%rbx
je     11d2 <GetNextLine+0x92>
cmpb   $0xa,(%r12)
je     11df <GetNextLine+0x9f>
lea    0x1(%rbx),%r12
add    $0x3,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
mov    %r12,%rbx
test   %rax,%rax
jne    1180 <GetNextLine+0x40>
xor    %r15d,%r15d
xor    %edi,%edi
call   1030 <free@plt>
jmp    11e4 <GetNextLine+0xa4>
xor    %r15d,%r15d
jmp    11e4 <GetNextLine+0xa4>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    11e4 <GetNextLine+0xa4>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @GetNextLine(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
store i32* %0, i32** %3
store i8* null, i8** %4
store i32 0, i32* %5
%6 = call i64 @malloc(i32 1)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %56
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
br label %14
14: ; preds = %11, %53
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = add nsw i32 %16, 2
%18 = call i8* @realloc(i8* %15, i32 %17)
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 = %14
%22 = load i8*, i8** %4
%23 = call i32 @free(i8* %22)
store i8* null, i8** %2
br label %56
24: ; preds = %14
%25 = load i8*, i8** %4
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32*, i32** %3
%30 = call i32* @fgets(i8* %28, i32 2, i32* %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %39
32: ; preds = %24
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i8*, i8** %4
%37 = call i32 @free(i8* %36)
store i8* null, i8** %2
br label %56
38: ; preds = %32
br label %39
39: ; preds = %38, %24
%40 = load i8*, i8** %4
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 10
br i1 %46, label %47, label %53
47: ; preds = %39
%48 = load i8*, i8** %4
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %4
store i8* %52, i8** %2
br label %56
53: ; preds = %39
%54 = load i32, i32* %5
%55 = add nsw i32 %54, 1
store i32 %55, i32* %5
br label %14
56: ; preds = %47, %35, %21, %10
%57 = load i8*, i8** %2
ret i8* %57
}

LLVM IR (-O2):
define i8* @GetNextLine(i32* nocapture %0) {
%2 = tail call i64 @malloc(i32 1)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %30, label %5
5: ; preds = %1
store i8 0, i8* %3
%6 = tail call i8* @realloc(i8* %3, i32 2)
%7 = icmp eq i8* %6, null
br i1 %7, label %14, label %16
8: ; preds = %26
%9 = add nuw i64 %17, 1
%10 = trunc i64 %17 to i32
%11 = add i32 %10, 3
%12 = tail call i8* @realloc(i8* %18, i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %8, %5
%15 = tail call i32 @free(i8* null)
br label %30
16: ; preds = %5, %8
%17 = phi i64 [ %9, %8 ], [ 0, %5 ]
%18 = phi i8* [ %12, %8 ], [ %6, %5 ]
%19 = getelementptr inbounds i8, i8* %18, i64 %17
%20 = tail call i32* @fgets(i8* %19, i32 2, i32* %0)
%21 = icmp eq i32* %20, null
%22 = icmp eq i64 %17, 0
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = tail call i32 @free(i8* %18)
br label %30
26: ; preds = %16
%27 = load i8, i8* %19
%28 = icmp eq i8 %27, 10
br i1 %28, label %29, label %8
29: ; preds = %26
store i8 0, i8* %19
br label %30
30: ; preds = %1, %29, %24, %14
%31 = phi i8* [ null, %14 ], [ null, %24 ], [ %18, %29 ], [ null, %1 ]
ret i8* %31
}