#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ feof(int /*<<< orphan*/);
char *fgets(char *, unsigned long, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ malloc(unsigned long);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
char *getline_unlim() {
  enum { FGETS_BUFSIZE = 30 };
  int flag_newline = 0;
  unsigned long block_size = FGETS_BUFSIZE;
  char *line = (char *)malloc(block_size * sizeof(char));
  char *character;
  char *result;
  while (!flag_newline) {
    result = fgets(line + strlen(line), block_size, stdin);
    if (!result) {
      if (feof(stdin)) {
        return line;
      } else {
        fprintf(stderr, "%s", strerror(errno));
        exit(EXIT_FAILURE);
      }
    } else {
      character = strchr(line, '\n');
      if (character) {
        *character = '\0';
        flag_newline = 1;
      } else {
        block_size *= 2;
        line = realloc(line, strlen(line) + block_size);
      }
    }
  }
  return line;
}


x86 assembly (clang, -O0):
<getline_unlim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0xc(%rbp)
movq   $0x1e,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x0,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ce <getline_unlim+0x3e>
jmp    12d4 <getline_unlim+0x144>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1268 <getline_unlim+0xd8>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     122e <getline_unlim+0x9e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12dc <getline_unlim+0x14c>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xdab(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1298 <getline_unlim+0x108>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0xc(%rbp)
jmp    12ca <getline_unlim+0x13a>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
cltq
add    -0x18(%rbp),%rax
mov    %eax,%esi
call   1090 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    12cf <getline_unlim+0x13f>
jmp    11b8 <getline_unlim+0x28>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getline_unlim>:
push   %r15
push   %r14
push   %rbx
mov    $0x1e,%ebx
mov    $0x1e,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %r14,%rdi
mov    0x2e2c(%rip),%r15
mov    (%r15),%edx
mov    $0x1e,%esi
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1050 <fgets@plt>
test   %rax,%rax
je     121b <getline_unlim+0x8b>
mov    %r14,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    125e <getline_unlim+0xce>
add    %rbx,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%esi
mov    %r14,%rdi
call   1090 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %r14,%rdi
mov    (%r15),%edx
mov    %rbx,%rsi
jmp    11d0 <getline_unlim+0x40>
mov    (%r15),%edi
call   1070 <feof@plt>
test   %rax,%rax
jne    1261 <getline_unlim+0xd1>
mov    0x2da9(%rip),%rax
mov    (%rax),%ebx
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdba(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movb   $0x0,(%rax)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
@stderr = global i32 0
@.str = [3 x i8] c"%s\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @getline_unlim() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %2
store i64 30, i64* %3
%7 = load i64, i64* %3
%8 = mul i64 %7, 1
%9 = call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %4
br label %11
11: ; preds = %58, %0
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %59
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = load i64, i64* %3
%22 = load i32, i32* @stdin
%23 = call i8* @fgets(i8* %20, i64 %21, i32 %22)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %39, label %26
26: ; preds = %15
%27 = load i32, i32* @stdin
%28 = call i64 @feof(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i8*, i8** %4
store i8* %31, i8** %1
br label %61
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %15
%40 = load i8*, i8** %4
%41 = call i8* @strchr(i8* %40, i8 signext 10)
store i8* %41, i8** %5
%42 = load i8*, i8** %5
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = load i8*, i8** %5
store i8 0, i8* %45
store i32 1, i32* %2
br label %57
46: ; preds = %39
%47 = load i64, i64* %3
%48 = mul i64 %47, 2
store i64 %48, i64* %3
%49 = load i8*, i8** %4
%50 = load i8*, i8** %4
%51 = call i32 @strlen(i8* %50)
%52 = sext i32 %51 to i64
%53 = load i64, i64* %3
%54 = add i64 %52, %53
%55 = trunc i64 %54 to i32
%56 = call i8* @realloc(i8* %49, i32 %55)
store i8* %56, i8** %4
br label %57
57: ; preds = %46, %44
br label %58
58: ; preds = %57
br label %11
59: ; preds = %11
%60 = load i8*, i8** %4
store i8* %60, i8** %1
br label %61
61: ; preds = %59, %30
%62 = load i8*, i8** %1
ret i8* %62
}

LLVM IR (-O2):
@stdin = global i32 0
@stderr = global i32 0
@.str = [3 x i8] c"%s\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i8* @getline_unlim() {
%1 = tail call i64 @malloc(i64 30)
%2 = inttoptr i64 %1 to i8*
%3 = tail call i32 @strlen(i8* %2)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %2, i64 %4
%6 = load i32, i32* @stdin
%7 = tail call i8* @fgets(i8* %5, i64 30, i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %21
9: ; preds = %26, %0
%10 = phi i8* [ %2, %0 ], [ %31, %26 ]
%11 = load i32, i32* @stdin
%12 = tail call i64 @feof(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %39
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %0, %26
%22 = phi i64 [ %27, %26 ], [ 30, %0 ]
%23 = phi i8* [ %31, %26 ], [ %2, %0 ]
%24 = tail call i8* @strchr(i8* dereferenceable(1) %23, i8 signext 10)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %38
26: ; preds = %21
%27 = shl i64 %22, 1
%28 = tail call i32 @strlen(i8* %23)
%29 = trunc i64 %27 to i32
%30 = add i32 %28, %29
%31 = tail call i8* @realloc(i8* %23, i32 %30)
%32 = tail call i32 @strlen(i8* %31)
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i32, i32* @stdin
%36 = tail call i8* @fgets(i8* %34, i64 %27, i32 %35)
%37 = icmp eq i8* %36, null
br i1 %37, label %9, label %21
38: ; preds = %21
store i8 0, i8* %24
br label %39
39: ; preds = %38, %9
%40 = phi i8* [ %23, %38 ], [ %10, %9 ]
ret i8* %40
}