#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 fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(size_t const);
char *realloc(char *, size_t);
int /*<<< orphan*/ stdin;
char *get_line(void) {
  const size_t size_delta = 10;
  char *buffer = malloc(size_delta);
  char *current_position = buffer;
  size_t max_length = size_delta;
  size_t length = 0;
  int character;
  if (current_position == NULL) {
    return NULL;
  }
  while (1) {
    character = fgetc(stdin);
    if (character == '\n') {
      break;
    }
    if (++length >= max_length) {
      char *new_buffer = realloc(buffer, max_length += size_delta);
      if (new_buffer == NULL) {
        free(buffer);
        return NULL;
      }
      current_position = new_buffer + (current_position - buffer);
      buffer = new_buffer;
    }
    *current_position++ = character;
  }
  *current_position = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movq   $0xa,-0x10(%rbp)
mov    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0xa,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118e <get_line+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    124b <get_line+0x10b>
jmp    1193 <get_line+0x53>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1040 <fgetc@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xa,-0x34(%rbp)
jne    11b3 <get_line+0x73>
jmp    123c <get_line+0xfc>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
cmp    -0x28(%rbp),%rax
jb     1221 <get_line+0xe1>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0xa,%rsi
mov    %rsi,-0x28(%rbp)
call   1060 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1203 <get_line+0xc3>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    124b <get_line+0x10b>
mov    -0x40(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x34(%rbp),%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    1193 <get_line+0x53>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xa,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d7 <get_line+0x97>
mov    %rax,%r14
mov    0x2e84(%rip),%r15
mov    (%r15),%edi
call   1040 <fgetc@plt>
cmp    $0xa,%eax
jne    1179 <get_line+0x39>
mov    %r14,%rbx
movb   $0x0,(%rbx)
jmp    11da <get_line+0x9a>
mov    %eax,%r13d
mov    $0xa,%r12d
mov    $0x1,%ebp
mov    %r14,%rbx
jmp    11ab <get_line+0x6b>
nopl   0x0(%rax)
mov    %r13b,(%rbx)
add    $0x1,%rbx
mov    (%r15),%edi
call   1040 <fgetc@plt>
mov    %eax,%r13d
add    $0x1,%rbp
cmp    $0xa,%eax
je     1174 <get_line+0x34>
cmp    %r12,%rbp
jb     1190 <get_line+0x50>
add    $0xa,%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     11cf <get_line+0x8f>
sub    %r14,%rbx
add    %rax,%rbx
mov    %rax,%r14
jmp    1190 <get_line+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stdin = global i32 0
define i8* @get_line() {
%1 = tail call dereferenceable_or_null(10) i8* @malloc(i64 10)
%2 = icmp eq i8* %1, null
br i1 %2, label %38, label %3
3: ; preds = %0
%4 = load i32, i32* @stdin
%5 = tail call i32 @fgetc(i32 %4)
%6 = icmp eq i32 %5, 10
br i1 %6, label %35, label %7
7: ; preds = %3, %26
%8 = phi i32 [ %33, %26 ], [ %5, %3 ]
%9 = phi i8* [ %29, %26 ], [ %1, %3 ]
%10 = phi i64 [ %13, %26 ], [ 0, %3 ]
%11 = phi i64 [ %28, %26 ], [ 10, %3 ]
%12 = phi i8* [ %31, %26 ], [ %1, %3 ]
%13 = add i64 %10, 1
%14 = icmp ult i64 %13, %11
br i1 %14, label %26, label %15
15: ; preds = %7
%16 = add i64 %11, 10
%17 = tail call i8* @realloc(i8* %9, i64 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @free(i8* %9)
br label %38
21: ; preds = %15
%22 = ptrtoint i8* %12 to i64
%23 = ptrtoint i8* %9 to i64
%24 = sub i64 %22, %23
%25 = getelementptr inbounds i8, i8* %17, i64 %24
br label %26
26: ; preds = %21, %7
%27 = phi i8* [ %25, %21 ], [ %12, %7 ]
%28 = phi i64 [ %16, %21 ], [ %11, %7 ]
%29 = phi i8* [ %17, %21 ], [ %9, %7 ]
%30 = trunc i32 %8 to i8
%31 = getelementptr inbounds i8, i8* %27, i64 1
store i8 %30, i8* %27
%32 = load i32, i32* @stdin
%33 = tail call i32 @fgetc(i32 %32)
%34 = icmp eq i32 %33, 10
br i1 %34, label %35, label %7
35: ; preds = %26, %3
%36 = phi i8* [ %1, %3 ], [ %31, %26 ]
%37 = phi i8* [ %1, %3 ], [ %29, %26 ]
store i8 0, i8* %36
br label %38
38: ; preds = %19, %0, %35
%39 = phi i8* [ %37, %35 ], [ null, %0 ], [ null, %19 ]
ret i8* %39
}