#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*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
char *realloc(char *, size_t);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
size_t strlen(char *);
char *local_readline(const char *prompt) {
  char *buf = NULL, *buf2;
  size_t len, alloc = 256;
  if (prompt)
    fprintf(stdout, "%s", prompt);
  buf = malloc(alloc);
  if (!buf)
    return NULL;
  buf2 = buf;
  do {
    if (!fgets(buf2, 256, stdin))
      break;
    len = strlen(buf2);
    if ((len > 0) && (buf2[len - 1] == '\n')) {
      buf2[--len] = 0;
      return buf;
    }
    len += (buf2 - buf);
    alloc += 256;
    if (!(buf2 = realloc(buf, alloc)))
      break;
    buf = buf2;
    buf2 = &buf[len];
  } while (1);
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<local_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x100,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a0 <local_readline+0x40>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe65(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c5 <local_readline+0x65>
movq   $0x0,-0x8(%rbp)
jmp    12bd <local_readline+0x15d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    11f2 <local_readline+0x92>
jmp    12ac <local_readline+0x14c>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    1248 <local_readline+0xe8>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1248 <local_readline+0xe8>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x28(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12bd <local_readline+0x15d>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    $0x100,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1289 <local_readline+0x129>
jmp    12ac <local_readline+0x14c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    11cd <local_readline+0x6d>
jmp    12ac <local_readline+0x14c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<local_readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1188 <local_readline+0x28>
mov    %rdi,%rdx
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x100,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1234 <local_readline+0xd4>
mov    %rax,%rbx
mov    0x2e43(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rax,%rdi
mov    $0x100,%esi
call   1050 <fgets@plt>
test   %eax,%eax
je     1224 <local_readline+0xc4>
mov    $0x200,%r14d
mov    %rbx,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
test   %rax,%rax
je     11e8 <local_readline+0x88>
cmpb   $0xa,-0x1(%r13,%rbx,1)
je     1249 <local_readline+0xe9>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
je     1229 <local_readline+0xc9>
mov    %rax,%r12
sub    %r15,%rbx
add    %r13,%rbx
add    %rax,%rbx
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1050 <fgets@plt>
add    $0x100,%r14
mov    %r12,%r15
test   %eax,%eax
jne    11d0 <local_readline+0x70>
jmp    122c <local_readline+0xcc>
mov    %rbx,%r12
jmp    122c <local_readline+0xcc>
mov    %r15,%r12
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,-0x1(%rbx,%r13,1)
jmp    1237 <local_readline+0xd7>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
define i8* @local_readline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
store i64 256, i64* %7
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @stdout
%12 = load i8*, i8** %3
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %10, %1
%15 = load i64, i64* %7
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %14
store i8* null, i8** %2
br label %71
20: ; preds = %14
%21 = load i8*, i8** %4
store i8* %21, i8** %5
br label %22
22: ; preds = %67, %20
%23 = load i8*, i8** %5
%24 = load i32, i32* @stdin
%25 = call i32 @fgets(i8* %23, i32 256, i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %22
br label %68
28: ; preds = %22
%29 = load i8*, i8** %5
%30 = call i64 @strlen(i8* %29)
store i64 %30, i64* %6
%31 = load i64, i64* %6
%32 = icmp ugt i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %28
%34 = load i8*, i8** %5
%35 = load i64, i64* %6
%36 = sub i64 %35, 1
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 10
br i1 %40, label %41, label %47
41: ; preds = %33
%42 = load i8*, i8** %5
%43 = load i64, i64* %6
%44 = add i64 %43, -1
store i64 %44, i64* %6
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %4
store i8* %46, i8** %2
br label %71
47: ; preds = %33, %28
%48 = load i8*, i8** %5
%49 = load i8*, i8** %4
%50 = ptrtoint i8* %48 to i64
%51 = ptrtoint i8* %49 to i64
%52 = sub i64 %50, %51
%53 = load i64, i64* %6
%54 = add i64 %53, %52
store i64 %54, i64* %6
%55 = load i64, i64* %7
%56 = add i64 %55, 256
store i64 %56, i64* %7
%57 = load i8*, i8** %4
%58 = load i64, i64* %7
%59 = call i8* @realloc(i8* %57, i64 %58)
store i8* %59, i8** %5
%60 = icmp ne i8* %59, null
br i1 %60, label %62, label %61
61: ; preds = %47
br label %68
62: ; preds = %47
%63 = load i8*, i8** %5
store i8* %63, i8** %4
%64 = load i8*, i8** %4
%65 = load i64, i64* %6
%66 = getelementptr inbounds i8, i8* %64, i64 %65
store i8* %66, i8** %5
br label %67
67: ; preds = %62
br i1 true, label %22, label %68
68: ; preds = %67, %61, %27
%69 = load i8*, i8** %4
%70 = call i32 @free(i8* %69)
store i8* null, i8** %2
br label %71
71: ; preds = %68, %41, %19
%72 = load i8*, i8** %2
ret i8* %72
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
define i8* @local_readline(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = load i32, i32* @stdout
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %6
6: ; preds = %3, %1
%7 = tail call dereferenceable_or_null(256) i8* @malloc(i64 256)
%8 = icmp eq i8* %7, null
br i1 %8, label %42, label %9
9: ; preds = %6
%10 = load i32, i32* @stdin
%11 = tail call i32 @fgets(i8* %7, i32 256, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %39, label %13
13: ; preds = %9, %30
%14 = phi i64 [ %27, %30 ], [ 256, %9 ]
%15 = phi i8* [ %35, %30 ], [ %7, %9 ]
%16 = phi i8* [ %28, %30 ], [ %7, %9 ]
%17 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %13
%20 = add i64 %17, -1
%21 = getelementptr inbounds i8, i8* %15, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = getelementptr inbounds i8, i8* %15, i64 %20
store i8 0, i8* %25
br label %42
26: ; preds = %19, %13
%27 = add i64 %14, 256
%28 = tail call i8* @realloc(i8* %16, i64 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %39, label %30
30: ; preds = %26
%31 = ptrtoint i8* %15 to i64
%32 = ptrtoint i8* %16 to i64
%33 = sub i64 %31, %32
%34 = add i64 %33, %17
%35 = getelementptr inbounds i8, i8* %28, i64 %34
%36 = load i32, i32* @stdin
%37 = tail call i32 @fgets(i8* %35, i32 256, i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %13
39: ; preds = %30, %26, %9
%40 = phi i8* [ %7, %9 ], [ %16, %26 ], [ %28, %30 ]
%41 = tail call i32 @free(i8* %40)
br label %42
42: ; preds = %6, %39, %24
%43 = phi i8* [ %16, %24 ], [ null, %39 ], [ null, %6 ]
ret i8* %43
}