#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, int);
int strlen(char *);
char *_getline(FILE *in) {
  int capacity = 80;
  int length = 0;
  char *line = malloc(capacity * sizeof(char));
  while (!feof(in)) {
    fgets(&line[length], capacity - length, in);
    length = strlen(line);
    if (!feof(in) && line[length - 1] != '\n') {
      capacity = capacity * 2;
      line = realloc(line, capacity * sizeof(char));
    } else {
      return line;
    }
  }
}


x86 assembly (clang, -O0):
<_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x50,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    119b <_getline+0x4b>
jmp    1224 <_getline+0xd4>
mov    -0x20(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
jne    1212 <_getline+0xc2>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     1212 <_getline+0xc2>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    121f <_getline+0xcf>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1224 <_getline+0xd4>
jmp    117d <_getline+0x2d>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x50,%ebp
mov    $0x50,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     1186 <_getline+0x36>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r15,%rax,1),%rdi
mov    %ebp,%esi
sub    %eax,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    1178 <_getline+0x28>
movslq %ebx,%rax
cmpb   $0xa,-0x1(%rax,%r15,1)
je     1178 <_getline+0x28>
add    %ebp,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     1190 <_getline+0x40>
jmp    1178 <_getline+0x28>

LLVM IR (-O0):
define i8* @_getline(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32* %0, i32** %3
store i32 80, i32* %4
store i32 0, i32* %5
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 1
%10 = trunc i64 %9 to i32
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %6
br label %12
12: ; preds = %52, %1
%13 = load i32*, i32** %3
%14 = call i32 @feof(i32* %13)
%15 = icmp ne i32 %14, 0
%16 = xor i1 %15, true
br i1 %16, label %17, label %53
17: ; preds = %12
%18 = load i8*, i8** %6
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = sub nsw i32 %22, %23
%25 = load i32*, i32** %3
%26 = call i32 @fgets(i8* %21, i32 %24, i32* %25)
%27 = load i8*, i8** %6
%28 = call i32 @strlen(i8* %27)
store i32 %28, i32* %5
%29 = load i32*, i32** %3
%30 = call i32 @feof(i32* %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %50, label %32
32: ; preds = %17
%33 = load i8*, i8** %6
%34 = load i32, i32* %5
%35 = sub nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 10
br i1 %40, label %41, label %50
41: ; preds = %32
%42 = load i32, i32* %4
%43 = mul nsw i32 %42, 2
store i32 %43, i32* %4
%44 = load i8*, i8** %6
%45 = load i32, i32* %4
%46 = sext i32 %45 to i64
%47 = mul i64 %46, 1
%48 = trunc i64 %47 to i32
%49 = call i8* @realloc(i8* %44, i32 %48)
store i8* %49, i8** %6
br label %52
50: ; preds = %32, %17
%51 = load i8*, i8** %6
store i8* %51, i8** %2
br label %53
52: ; preds = %41
br label %12
53: ; preds = %50, %12
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
define i8* @_getline(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(80) i8* @malloc(i32 80)
%3 = tail call i32 @feof(i32* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %27
5: ; preds = %1, %22
%6 = phi i8* [ %24, %22 ], [ %2, %1 ]
%7 = phi i32 [ %13, %22 ], [ 0, %1 ]
%8 = phi i32 [ %23, %22 ], [ 80, %1 ]
%9 = sext i32 %7 to i64
%10 = getelementptr inbounds i8, i8* %6, i64 %9
%11 = sub nsw i32 %8, %7
%12 = tail call i32 @fgets(i8* %10, i32 %11, i32* %0)
%13 = tail call i32 @strlen(i8* %6)
%14 = tail call i32 @feof(i32* %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %5
%17 = add nsw i32 %13, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %6, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 10
br i1 %21, label %27, label %22
22: ; preds = %16
%23 = shl nsw i32 %8, 1
%24 = tail call i8* @realloc(i8* %6, i32 %23)
%25 = tail call i32 @feof(i32* %0)
%26 = icmp eq i32 %25, 0
br i1 %26, label %5, label %27
27: ; preds = %16, %5, %22, %1
%28 = phi i8* [ %2, %1 ], [ %24, %22 ], [ %6, %5 ], [ %6, %16 ]
ret i8* %28
}