#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 EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ counting_free(char *);
char *counting_malloc(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, int);
int /*<<< orphan*/ stdin;
char *read_prompt() {
  char *buffer;
  int buffer_size;
  char *p;
  int character;
  int length = 0;
  buffer = counting_malloc(20);
  if (buffer == NULL) {
    perror("Unable to allocate necessary memory");
    exit(EXIT_FAILURE);
  }
  buffer_size = 20;
  p = buffer;
  while (1) {
    character = fgetc(stdin);
    if (character == EOF) {
      if (length > 0) {
        *p = '\0';
      } else {
        counting_free(buffer);
        buffer = NULL;
      }
      break;
    } else if (character == '\n') {
      *p = '\0';
      break;
    }
    *p = character;
    p++;
    length++;
    if (length >= buffer_size) {
      buffer_size += 20;
      buffer = realloc(buffer, buffer_size);
      if (buffer == NULL) {
        perror("Unable to allocate necessary memory");
        exit(EXIT_FAILURE);
      }
      p = buffer + length;
    }
  }
  if (!buffer) {
    printf("\n");
  }
  return buffer;
}


x86 assembly (clang, -O0):
<read_prompt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x20(%rbp)
mov    $0x14,%edi
call   1060 <counting_malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b2 <read_prompt+0x42>
lea    0xe61(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x14,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1050 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%eax
jne    1210 <read_prompt+0xa0>
cmpl   $0x0,-0x20(%rbp)
jle    11fa <read_prompt+0x8a>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    120b <read_prompt+0x9b>
mov    -0x8(%rbp),%rdi
call   1030 <counting_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a9 <read_prompt+0x139>
cmpl   $0xa,-0x1c(%rbp)
jne    1226 <read_prompt+0xb6>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    12a9 <read_prompt+0x139>
jmp    122b <read_prompt+0xbb>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     12a4 <read_prompt+0x134>
mov    -0xc(%rbp),%eax
add    $0x14,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1295 <read_prompt+0x125>
lea    0xd7e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    11c1 <read_prompt+0x51>
cmpq   $0x0,-0x8(%rbp)
jne    12c0 <read_prompt+0x150>
lea    0xd69(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_prompt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x14,%edi
call   1060 <counting_malloc@plt>
test   %rax,%rax
je     1242 <read_prompt+0xd2>
mov    %rax,%r14
mov    0x2e50(%rip),%r15
mov    (%r15),%edi
call   1050 <fgetc@plt>
mov    0x2e39(%rip),%r13
mov    0x0(%r13),%ecx
cmp    %ecx,%eax
jne    11c6 <read_prompt+0x56>
mov    %r14,%rdi
call   1030 <counting_free@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
xor    %r14d,%r14d
jmp    1230 <read_prompt+0xc0>
mov    %ecx,(%rsp)
mov    $0x14,%ebp
mov    $0x1,%r12d
mov    %eax,0x4(%rsp)
mov    %r14,%rbx
jmp    11f6 <read_prompt+0x86>
nopl   (%rax)
add    $0x1,%rbx
mov    (%r15),%edi
call   1050 <fgetc@plt>
add    $0x1,%r12
cmp    0x0(%r13),%eax
je     121f <read_prompt+0xaf>
cmp    $0xa,%eax
je     1228 <read_prompt+0xb8>
mov    %al,(%rbx)
cmp    %r12d,%ebp
jg     11e0 <read_prompt+0x70>
add    $0x14,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
test   %rax,%rax
je     1242 <read_prompt+0xd2>
mov    %rax,%r14
mov    %rax,%rbx
add    %r12,%rbx
jmp    11e4 <read_prompt+0x74>
mov    (%rsp),%eax
cmp    %eax,0x4(%rsp)
je     11af <read_prompt+0x3f>
movb   $0x0,(%rbx)
test   %r14,%r14
je     11b7 <read_prompt+0x47>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb7(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"Unable to allocate necessary memory\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32 0
@EOF = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i8* @read_prompt() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %5
%6 = call i8* @counting_malloc(i32 20)
store i8* %6, i8** %1
%7 = load i8*, i8** %1
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
store i32 20, i32* %2
%14 = load i8*, i8** %1
store i8* %14, i8** %3
br label %15
15: ; preds = %13, %64
%16 = load i32, i32* @stdin
%17 = call i32 @fgetc(i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = load i32, i32* @EOF
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %30
21: ; preds = %15
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = load i8*, i8** %3
store i8 0, i8* %25
br label %29
26: ; preds = %21
%27 = load i8*, i8** %1
%28 = call i32 @counting_free(i8* %27)
store i8* null, i8** %1
br label %29
29: ; preds = %26, %24
br label %65
30: ; preds = %15
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = load i8*, i8** %3
store i8 0, i8* %34
br label %65
35: ; preds = %30
br label %36
36: ; preds = %35
%37 = load i32, i32* %4
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %3
store i8 %38, i8* %39
%40 = load i8*, i8** %3
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %3
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = load i32, i32* %2
%46 = icmp sge i32 %44, %45
br i1 %46, label %47, label %64
47: ; preds = %36
%48 = load i32, i32* %2
%49 = add nsw i32 %48, 20
store i32 %49, i32* %2
%50 = load i8*, i8** %1
%51 = load i32, i32* %2
%52 = call i8* @realloc(i8* %50, i32 %51)
store i8* %52, i8** %1
%53 = load i8*, i8** %1
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %59
55: ; preds = %47
%56 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %47
%60 = load i8*, i8** %1
%61 = load i32, i32* %5
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8* %63, i8** %3
br label %64
64: ; preds = %59, %36
br label %15
65: ; preds = %33, %29
%66 = load i8*, i8** %1
%67 = icmp ne i8* %66, null
br i1 %67, label %70, label %68
68: ; preds = %65
%69 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %70
70: ; preds = %68, %65
%71 = load i8*, i8** %1
ret i8* %71
}

LLVM IR (-O2):
@.str = [36 x i8] c"Unable to allocate necessary memory\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32 0
@EOF = global i32 0
define i8* @read_prompt() {
%1 = tail call i8* @counting_malloc(i32 20)
%2 = icmp eq i8* %1, null
br i1 %2, label %8, label %3
3: ; preds = %0
%4 = load i32, i32* @stdin
%5 = tail call i32 @fgetc(i32 %4)
%6 = load i32, i32* @EOF
%7 = icmp eq i32 %5, %6
br i1 %7, label %13, label %16
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %39
br i1 %7, label %13, label %47
13: ; preds = %3, %12
%14 = phi i8* [ %42, %12 ], [ %1, %3 ]
%15 = tail call i32 @counting_free(i8* %14)
br label %51
16: ; preds = %3, %39
%17 = phi i64 [ %26, %39 ], [ 0, %3 ]
%18 = phi i32 [ %44, %39 ], [ %5, %3 ]
%19 = phi i8* [ %42, %39 ], [ %1, %3 ]
%20 = phi i8* [ %41, %39 ], [ %1, %3 ]
%21 = phi i32 [ %40, %39 ], [ 20, %3 ]
%22 = icmp eq i32 %18, 10
br i1 %22, label %47, label %23
23: ; preds = %16
%24 = trunc i32 %18 to i8
store i8 %24, i8* %20
%25 = getelementptr inbounds i8, i8* %20, i64 1
%26 = add nuw i64 %17, 1
%27 = trunc i64 %26 to i32
%28 = icmp sgt i32 %21, %27
br i1 %28, label %39, label %29
29: ; preds = %23
%30 = add nsw i32 %21, 20
%31 = tail call i8* @realloc(i8* %19, i32 %30)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %29
%38 = getelementptr inbounds i8, i8* %31, i64 %26
br label %39
39: ; preds = %37, %23
%40 = phi i32 [ %30, %37 ], [ %21, %23 ]
%41 = phi i8* [ %38, %37 ], [ %25, %23 ]
%42 = phi i8* [ %31, %37 ], [ %19, %23 ]
%43 = load i32, i32* @stdin
%44 = tail call i32 @fgetc(i32 %43)
%45 = load i32, i32* @EOF
%46 = icmp eq i32 %44, %45
br i1 %46, label %12, label %16
47: ; preds = %16, %12
%48 = phi i8* [ %41, %12 ], [ %20, %16 ]
%49 = phi i8* [ %42, %12 ], [ %19, %16 ]
store i8 0, i8* %48
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %53
51: ; preds = %13, %47
%52 = tail call i32 @putchar(i32 10)
br label %53
53: ; preds = %51, %47
%54 = phi i8* [ null, %51 ], [ %49, %47 ]
ret i8* %54
}