#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 */
char getc(int /*<<< orphan*/);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ stdin;
char *get_line(const char *prompt) {
  printf("%s", prompt);
  char c = 0;
  size_t i = 0;
  size_t stringlen = 10;
  char *buffer = (char *)malloc(stringlen * sizeof(char));
  while ((c = getc(stdin)) != '\n') {
    buffer[i++] = c;
    if (i >= stringlen) {
      stringlen *= 2;
      buffer = (char *)realloc(buffer, stringlen * sizeof(char));
    }
  }
  buffer[i] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xea9(%rip),%rdi
call   1030 <printf@plt>
movb   $0x0,-0x9(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0xa,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1060 <getc@plt>
mov    %al,-0x9(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     11eb <get_line+0xab>
mov    -0x9(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x18(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     11e6 <get_line+0xa6>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
shl    $0x0,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
jmp    1181 <get_line+0x41>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rsi
lea    0xeae(%rip),%rdi
call   1030 <printf@plt>
mov    $0xa,%r12d
mov    $0xa,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    0x2e77(%rip),%r15
mov    (%r15),%edi
call   1060 <getc@plt>
xor    %ebx,%ebx
jmp    1188 <get_line+0x48>
nopl   (%rax)
mov    (%r15),%edi
call   1060 <getc@plt>
cmp    $0xa,%al
je     11ac <get_line+0x6c>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %r12,%rbx
jb     1180 <get_line+0x40>
add    %r12,%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <realloc@plt>
mov    %rax,%r14
jmp    1180 <get_line+0x40>
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
define i8* @get_line(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %7)
store i8 0, i8* %3
store i64 0, i64* %4
store i64 10, i64* %5
%9 = load i64, i64* %5
%10 = mul i64 %9, 1
%11 = call i64 @malloc(i64 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %6
br label %13
13: ; preds = %35, %1
%14 = load i32, i32* @stdin
%15 = call signext i8 @getc(i32 %14)
store i8 %15, i8* %3
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 10
br i1 %17, label %18, label %36
18: ; preds = %13
%19 = load i8, i8* %3
%20 = load i8*, i8** %6
%21 = load i64, i64* %4
%22 = add i64 %21, 1
store i64 %22, i64* %4
%23 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 %19, i8* %23
%24 = load i64, i64* %4
%25 = load i64, i64* %5
%26 = icmp uge i64 %24, %25
br i1 %26, label %27, label %35
27: ; preds = %18
%28 = load i64, i64* %5
%29 = mul i64 %28, 2
store i64 %29, i64* %5
%30 = load i8*, i8** %6
%31 = load i64, i64* %5
%32 = mul i64 %31, 1
%33 = call i64 @realloc(i8* %30, i64 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %6
br label %35
35: ; preds = %27, %18
br label %13
36: ; preds = %13
%37 = load i8*, i8** %6
%38 = load i64, i64* %4
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %6
ret i8* %40
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
define i8* @get_line(i8* %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i64 @malloc(i64 10)
%4 = inttoptr i64 %3 to i8*
%5 = load i32, i32* @stdin
%6 = tail call signext i8 @getc(i32 %5)
%7 = icmp eq i8 %6, 10
br i1 %7, label %26, label %8
8: ; preds = %1, %20
%9 = phi i8 [ %24, %20 ], [ %6, %1 ]
%10 = phi i8* [ %22, %20 ], [ %4, %1 ]
%11 = phi i64 [ %21, %20 ], [ 10, %1 ]
%12 = phi i64 [ %13, %20 ], [ 0, %1 ]
%13 = add i64 %12, 1
%14 = getelementptr inbounds i8, i8* %10, i64 %12
store i8 %9, i8* %14
%15 = icmp ult i64 %13, %11
br i1 %15, label %20, label %16
16: ; preds = %8
%17 = shl i64 %11, 1
%18 = tail call i64 @realloc(i8* %10, i64 %17)
%19 = inttoptr i64 %18 to i8*
br label %20
20: ; preds = %16, %8
%21 = phi i64 [ %17, %16 ], [ %11, %8 ]
%22 = phi i8* [ %19, %16 ], [ %10, %8 ]
%23 = load i32, i32* @stdin
%24 = tail call signext i8 @getc(i32 %23)
%25 = icmp eq i8 %24, 10
br i1 %25, label %26, label %8
26: ; preds = %20, %1
%27 = phi i64 [ 0, %1 ], [ %13, %20 ]
%28 = phi i8* [ %4, %1 ], [ %22, %20 ]
%29 = getelementptr inbounds i8, i8* %28, i64 %27
store i8 0, i8* %29
ret i8* %28
}