#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*/ STDIN_FILENO;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int isatty(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(char *, char *);
char *readline(char *prompt) {
  static int interactive = -1;
  int len = 80;
  int i = 0;
  char *ret = (char *)malloc(sizeof(char) * len);
  char foo = 0;
  if (interactive < 0) {
    interactive = isatty(STDIN_FILENO);
  }
  if (interactive) {
    printf(prompt);
    fflush(stdout);
  }
  while (foo != '\n') {
    if (read(STDIN_FILENO, &foo, 1) <= 0) {
      free(ret);
      return NULL;
    }
    if (i >= len - 2) {
      char *tmp = (char *)malloc(sizeof(char) * len * 2);
      len *= 2;
      strcpy(tmp, ret);
      free(ret);
      ret = tmp;
    }
    ret[i] = foo;
    i++;
  }
  ret[i] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x50,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
cmpl   $0x0,0x2eb0(%rip)
jge    11c2 <readline+0x52>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1050 <isatty@plt>
mov    %eax,0x2e96(%rip)
cmpl   $0x0,0x2e8f(%rip)
je     11e6 <readline+0x76>
mov    -0x10(%rbp),%rdi
call   1060 <printf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
jmp    11eb <readline+0x7b>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
je     129a <readline+0x12a>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jg     122f <readline+0xbf>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ae <readline+0x13e>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jl     127e <readline+0x10e>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
shl    $0x1,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x21(%rbp),%dl
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11eb <readline+0x7b>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x50,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
movb   $0x0,0x7(%rsp)
mov    0x2ec2(%rip),%eax
test   %eax,%eax
js     1251 <readline+0xe1>
test   %eax,%eax
je     11b8 <readline+0x48>
mov    %rbx,%rdi
call   1060 <printf@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    $0x50,%r12d
xor    %ebx,%ebx
mov    0x2e11(%rip),%rbp
lea    0x7(%rsp),%r15
jmp    11e4 <readline+0x74>
xchg   %ax,%ax
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmpb   $0xa,0x7(%rsp)
je     122b <readline+0xbb>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    1234 <readline+0xc4>
lea    -0x2(%r12),%eax
cltq
cmp    %rax,%rbx
jl     11d0 <readline+0x60>
add    %r12d,%r12d
mov    %r12d,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13,%r14
jmp    11d0 <readline+0x60>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
jmp    123f <readline+0xcf>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1050 <isatty@plt>
mov    %eax,0x2df3(%rip)
test   %eax,%eax
jne    11a2 <readline+0x32>
jmp    11b8 <readline+0x48>

LLVM IR (-O0):
@readline.interactive = internal global i32 -1
@STDIN_FILENO = global i32 0
@stdout = global i32 0
define i8* @readline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i8*
store i8* %0, i8** %3
store i32 80, i32* %4
store i32 0, i32* %5
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = mul i64 1, %10
%12 = trunc i64 %11 to i32
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %6
store i8 0, i8* %7
%15 = load i32, i32* @readline.interactive
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i32, i32* @STDIN_FILENO
%19 = call i32 @isatty(i32 %18)
store i32 %19, i32* @readline.interactive
br label %20
20: ; preds = %17, %1
%21 = load i32, i32* @readline.interactive
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8*, i8** %3
%25 = call i32 @printf(i8* %24)
%26 = load i32, i32* @stdout
%27 = call i32 @fflush(i32 %26)
br label %28
28: ; preds = %23, %20
br label %29
29: ; preds = %61, %28
%30 = load i8, i8* %7
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 10
br i1 %32, label %33, label %69
33: ; preds = %29
%34 = load i32, i32* @STDIN_FILENO
%35 = call i64 @read(i32 %34, i8* %7, i32 1)
%36 = icmp sle i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i8*, i8** %6
%39 = call i32 @free(i8* %38)
store i8* null, i8** %2
br label %75
40: ; preds = %33
%41 = load i32, i32* %5
%42 = load i32, i32* %4
%43 = sub nsw i32 %42, 2
%44 = icmp sge i32 %41, %43
br i1 %44, label %45, label %61
45: ; preds = %40
%46 = load i32, i32* %4
%47 = sext i32 %46 to i64
%48 = mul i64 1, %47
%49 = mul i64 %48, 2
%50 = trunc i64 %49 to i32
%51 = call i64 @malloc(i32 %50)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %8
%53 = load i32, i32* %4
%54 = mul nsw i32 %53, 2
store i32 %54, i32* %4
%55 = load i8*, i8** %8
%56 = load i8*, i8** %6
%57 = call i32 @strcpy(i8* %55, i8* %56)
%58 = load i8*, i8** %6
%59 = call i32 @free(i8* %58)
%60 = load i8*, i8** %8
store i8* %60, i8** %6
br label %61
61: ; preds = %45, %40
%62 = load i8, i8* %7
%63 = load i8*, i8** %6
%64 = load i32, i32* %5
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
store i8 %62, i8* %66
%67 = load i32, i32* %5
%68 = add nsw i32 %67, 1
store i32 %68, i32* %5
br label %29
69: ; preds = %29
%70 = load i8*, i8** %6
%71 = load i32, i32* %5
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %70, i64 %72
store i8 0, i8* %73
%74 = load i8*, i8** %6
store i8* %74, i8** %2
br label %75
75: ; preds = %69, %37
%76 = load i8*, i8** %2
ret i8* %76
}

LLVM IR (-O2):
@readline.interactive = internal unnamed_addr global i32 -1
@STDIN_FILENO = global i32 0
@stdout = global i32 0
define i8* @readline(i8* nocapture %0) {
%2 = alloca i8
%3 = tail call i64 @malloc(i32 80)
%4 = inttoptr i64 %3 to i8*
store i8 0, i8* %2
%5 = load i32, i32* @readline.interactive
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @STDIN_FILENO
%9 = tail call i32 @isatty(i32 %8)
store i32 %9, i32* @readline.interactive
br label %10
10: ; preds = %7, %1
%11 = phi i32 [ %9, %7 ], [ %5, %1 ]
%12 = icmp eq i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = tail call i32 @printf(i8* dereferenceable(1) %0)
%15 = load i32, i32* @stdout
%16 = tail call i32 @fflush(i32 %15)
br label %17
17: ; preds = %10, %13
br label %18
18: ; preds = %17, %37
%19 = phi i64 [ %42, %37 ], [ 0, %17 ]
%20 = phi i8* [ %39, %37 ], [ %4, %17 ]
%21 = phi i32 [ %38, %37 ], [ 80, %17 ]
%22 = load i32, i32* @STDIN_FILENO
%23 = call i64 @read(i32 %22, i8* %2, i32 1)
%24 = icmp slt i64 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = tail call i32 @free(i8* %20)
br label %48
27: ; preds = %18
%28 = add nsw i32 %21, -2
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %19, %29
br i1 %30, label %37, label %31
31: ; preds = %27
%32 = shl i32 %21, 1
%33 = tail call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
%35 = tail call i32 @strcpy(i8* %34, i8* %20)
%36 = tail call i32 @free(i8* %20)
br label %37
37: ; preds = %31, %27
%38 = phi i32 [ %32, %31 ], [ %21, %27 ]
%39 = phi i8* [ %34, %31 ], [ %20, %27 ]
%40 = load i8, i8* %2
%41 = getelementptr inbounds i8, i8* %39, i64 %19
store i8 %40, i8* %41
%42 = add nuw i64 %19, 1
%43 = load i8, i8* %2
%44 = icmp eq i8 %43, 10
br i1 %44, label %45, label %18
45: ; preds = %37
%46 = and i64 %42, 4294967295
%47 = getelementptr inbounds i8, i8* %39, i64 %46
store i8 0, i8* %47
br label %48
48: ; preds = %45, %25
%49 = phi i8* [ null, %25 ], [ %39, %45 ]
ret i8* %49
}