#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*/ errno;
int /*<<< orphan*/ free(char *);
int getchar();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, int);
char *strerror(int /*<<< orphan*/);
char *getUserInput() {
  printf("$ ");
  int bufferSize = 256;
  char *input = (char *)malloc(bufferSize);
  if (input == NULL) {
    printf("error: %s\n", strerror(errno));
  }
  int c;
  int inputSize = 0;
  while ((c = getchar()) != '\n') {
    input[inputSize] = (char)c;
    if (++inputSize == bufferSize) {
      bufferSize = bufferSize * 2;
      if ((input = realloc(input, bufferSize)) == NULL)
        free(input);
    }
  }
  input[inputSize] = '\0';
  return input;
}


x86 assembly (clang, -O0):
<getUserInput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x100,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b3 <getUserInput+0x53>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xa,%eax
je     1225 <getUserInput+0xc5>
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmp    -0x4(%rbp),%eax
jne    1220 <getUserInput+0xc0>
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    121b <getUserInput+0xbb>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
jmp    1220 <getUserInput+0xc0>
jmp    11ba <getUserInput+0x5a>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getUserInput>:
push   %rbp
push   %r14
push   %rbx
lea    0xe95(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x100,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11a3 <getUserInput+0x43>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe6a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
je     1203 <getUserInput+0xa3>
mov    $0x100,%ebp
xor    %ebx,%ebx
jmp    11dc <getUserInput+0x7c>
xor    %edi,%edi
call   1030 <free@plt>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
je     1201 <getUserInput+0xa1>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %ebp,%eax
cmp    %rax,%rbx
jne    11d0 <getUserInput+0x70>
add    %ebp,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
test   %rax,%rax
je     11ba <getUserInput+0x5a>
mov    %rax,%r14
jmp    11d0 <getUserInput+0x70>
mov    %ebx,%ebx
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"$ \00"
@.str.1 = [11 x i8] c"error: %s\0A\00"
@errno = global i32 0
define i8* @getUserInput() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 256, i32* %1
%6 = load i32, i32* %1
%7 = call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %11, %0
store i32 0, i32* %4
br label %16
16: ; preds = %41, %15
%17 = call i32 (...) @getchar()
store i32 %17, i32* %3
%18 = icmp ne i32 %17, 10
br i1 %18, label %19, label %42
19: ; preds = %16
%20 = load i32, i32* %3
%21 = trunc i32 %20 to i8
%22 = load i8*, i8** %2
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 %21, i8* %25
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
%28 = load i32, i32* %1
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %41
30: ; preds = %19
%31 = load i32, i32* %1
%32 = mul nsw i32 %31, 2
store i32 %32, i32* %1
%33 = load i8*, i8** %2
%34 = load i32, i32* %1
%35 = call i8* @realloc(i8* %33, i32 %34)
store i8* %35, i8** %2
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = load i8*, i8** %2
%39 = call i32 @free(i8* %38)
br label %40
40: ; preds = %37, %30
br label %41
41: ; preds = %40, %19
br label %16
42: ; preds = %16
%43 = load i8*, i8** %2
%44 = load i32, i32* %4
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"$ \00"
@.str.1 = [11 x i8] c"error: %s\0A\00"
@errno = global i32 0
define i8* @getUserInput() {
%1 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%2 = tail call i64 @malloc(i32 256)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %9
9: ; preds = %5, %0
%10 = tail call i32 (...) @getchar()
%11 = icmp eq i32 %10, 10
br i1 %11, label %35, label %12
12: ; preds = %9, %28
%13 = phi i64 [ %19, %28 ], [ 0, %9 ]
%14 = phi i32 [ %31, %28 ], [ %10, %9 ]
%15 = phi i32 [ %30, %28 ], [ 256, %9 ]
%16 = phi i8* [ %29, %28 ], [ %3, %9 ]
%17 = trunc i32 %14 to i8
%18 = getelementptr inbounds i8, i8* %16, i64 %13
store i8 %17, i8* %18
%19 = add nuw i64 %13, 1
%20 = zext i32 %15 to i64
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %12
%23 = shl nsw i32 %15, 1
%24 = tail call i8* @realloc(i8* %16, i32 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @free(i8* null)
br label %28
28: ; preds = %22, %26, %12
%29 = phi i8* [ null, %26 ], [ %24, %22 ], [ %16, %12 ]
%30 = phi i32 [ %23, %26 ], [ %23, %22 ], [ %15, %12 ]
%31 = tail call i32 (...) @getchar()
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %12
33: ; preds = %28
%34 = and i64 %19, 4294967295
br label %35
35: ; preds = %33, %9
%36 = phi i8* [ %3, %9 ], [ %29, %33 ]
%37 = phi i64 [ 0, %9 ], [ %34, %33 ]
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8 0, i8* %38
ret i8* %36
}