#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*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char const *);
char *realloc(char *, size_t);
int /*<<< orphan*/ stdin;
size_t strlen(char *);
char *readline(const char *prompt) {
  size_t sz = 0;
  char *buf = NULL;
  char *step_start;
  printf(prompt);
  while (1) {
    sz += 10;
    buf = realloc(buf, sz + 1);
    if (NULL == buf) {
      perror("realloc() failed");
      return NULL;
    }
    step_start = buf + sz - 10;
    memset(step_start, 0, 10 + 1);
    if (NULL == fgets(step_start, 10 + 1, stdin)) {
      break;
    }
    size_t len = strlen(step_start);
    if (len != 10 || step_start[10 - 1] == '\n') {
      step_start[len - 1] = '\0';
      break;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    11cb <readline+0x6b>
lea    0xe47(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1260 <readline+0x100>
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xfffffffffffffff6,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xb,%edx
call   1050 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
mov    $0xb,%esi
call   1060 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1215 <readline+0xb5>
jmp    1258 <readline+0xf8>
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xa,-0x30(%rbp)
jne    123e <readline+0xde>
mov    -0x28(%rbp),%rax
movsbl 0x9(%rax),%eax
cmp    $0xa,%eax
jne    1253 <readline+0xf3>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1258 <readline+0xf8>
jmp    1185 <readline+0x25>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
call   1040 <printf@plt>
xor    %r14d,%r14d
mov    0x2e71(%rip),%r15
xor    %ecx,%ecx
nopl   0x0(%rax)
mov    %rcx,%r12
lea    0xb(%rcx),%rsi
mov    %r14,%rdi
call   1070 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11f5 <readline+0x95>
lea    (%r14,%r12,1),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xb,%edx
call   1050 <memset@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0xb,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11e6 <readline+0x86>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0xa,%rax
jne    11da <readline+0x7a>
lea    0xa(%r12),%rcx
cmpb   $0xa,0x9(%r14,%r12,1)
jne    1180 <readline+0x20>
mov    %r14,%rcx
add    %rax,%rcx
movb   $0x0,-0x1(%r12,%rcx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
jmp    11e6 <readline+0x86>

LLVM IR (-O0):
@.str = [17 x i8] c"realloc() failed\00"
@stdin = global i32 0
define i8* @readline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store i64 0, i64* %4
store i8* null, i8** %5
%8 = load i8*, i8** %3
%9 = call i32 @printf(i8* %8)
br label %10
10: ; preds = %1, %49
%11 = load i64, i64* %4
%12 = add i64 %11, 10
store i64 %12, i64* %4
%13 = load i8*, i8** %5
%14 = load i64, i64* %4
%15 = add i64 %14, 1
%16 = call i8* @realloc(i8* %13, i64 %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = icmp eq i8* null, %17
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %52
21: ; preds = %10
%22 = load i8*, i8** %5
%23 = load i64, i64* %4
%24 = getelementptr inbounds i8, i8* %22, i64 %23
%25 = getelementptr inbounds i8, i8* %24, i64 -10
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = call i32 @memset(i8* %26, i32 0, i32 11)
%28 = load i8*, i8** %6
%29 = load i32, i32* @stdin
%30 = call i32* @fgets(i8* %28, i32 11, i32 %29)
%31 = icmp eq i32* null, %30
br i1 %31, label %32, label %33
32: ; preds = %21
br label %50
33: ; preds = %21
%34 = load i8*, i8** %6
%35 = call i64 @strlen(i8* %34)
store i64 %35, i64* %7
%36 = load i64, i64* %7
%37 = icmp ne i64 %36, 10
br i1 %37, label %44, label %38
38: ; preds = %33
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 9
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 10
br i1 %43, label %44, label %49
44: ; preds = %38, %33
%45 = load i8*, i8** %6
%46 = load i64, i64* %7
%47 = sub i64 %46, 1
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
br label %50
49: ; preds = %38
br label %10
50: ; preds = %44, %32
%51 = load i8*, i8** %5
store i8* %51, i8** %2
br label %52
52: ; preds = %50, %19
%53 = load i8*, i8** %2
ret i8* %53
}

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