#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*/);
scalar_t__ gc_alloc(int, char *);
int /*<<< orphan*/ gc_free(char *);
int isatty(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(char *, char *);
char *read_read(char *prompt) {
  static int interactive = -1;
  int len = 80;
  int i = 0;
  char *ret = (char *)gc_alloc(sizeof(char) * len, "read_read");
  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) {
      gc_free(ret);
      return NULL;
    }
    if (i >= len - 2) {
      char *tmp = (char *)gc_alloc(sizeof(char) * len * 2, "read_read");
      len *= 2;
      strcpy(tmp, ret);
      gc_free(ret);
      ret = tmp;
    }
    ret[i] = foo;
    i++;
  }
  ret[i] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<read_read>:
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
lea    0xe65(%rip),%rsi
call   1080 <gc_alloc@plt>
mov    %rax,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
cmpl   $0x0,0x2ea9(%rip)
jge    11c9 <read_read+0x59>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
mov    %eax,0x2e8f(%rip)
cmpl   $0x0,0x2e88(%rip)
je     11ed <read_read+0x7d>
mov    -0x10(%rbp),%rdi
call   1050 <printf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    11f2 <read_read+0x82>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
je     12a8 <read_read+0x138>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jg     1236 <read_read+0xc6>
mov    -0x20(%rbp),%rdi
call   1090 <gc_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <read_read+0x14c>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jl     128c <read_read+0x11c>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
shl    $0x1,%rax
mov    %eax,%edi
lea    0xda4(%rip),%rsi
call   1080 <gc_alloc@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   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1090 <gc_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    11f2 <read_read+0x82>
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):
<read_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe7b(%rip),%rsi
mov    $0x50,%edi
call   1080 <gc_alloc@plt>
mov    %rax,%r14
movb   $0x0,0x7(%rsp)
mov    0x2ebb(%rip),%eax
test   %eax,%eax
js     1268 <read_read+0xf8>
test   %eax,%eax
je     11bf <read_read+0x4f>
mov    %rbx,%rdi
call   1050 <printf@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    $0x50,%r13d
xor    %ebp,%ebp
mov    0x2e0a(%rip),%r12
lea    0x7(%rsp),%r15
jmp    11f4 <read_read+0x84>
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0x7(%rsp),%eax
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmpb   $0xa,0x7(%rsp)
je     1242 <read_read+0xd2>
mov    (%r12),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    124b <read_read+0xdb>
lea    -0x2(%r13),%eax
cltq
cmp    %rax,%rbp
jl     11e0 <read_read+0x70>
add    %r13d,%r13d
mov    %r13d,%edi
lea    0xdde(%rip),%rsi
call   1080 <gc_alloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1090 <gc_free@plt>
mov    %rbx,%r14
jmp    11e0 <read_read+0x70>
mov    %ebp,%eax
movb   $0x0,(%r14,%rax,1)
jmp    1256 <read_read+0xe6>
mov    %r14,%rdi
call   1090 <gc_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    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
mov    %eax,0x2ddc(%rip)
test   %eax,%eax
jne    11a9 <read_read+0x39>
jmp    11bf <read_read+0x4f>

LLVM IR (-O0):
@read_read.interactive = internal global i32 -1
@.str = [10 x i8] c"read_read\00"
@STDIN_FILENO = global i32 0
@stdout = global i32 0
define i8* @read_read(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 @gc_alloc(i32 %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %6
store i8 0, i8* %7
%15 = load i32, i32* @read_read.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* @read_read.interactive
br label %20
20: ; preds = %17, %1
%21 = load i32, i32* @read_read.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 @gc_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 @gc_alloc(i32 %50, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%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 @gc_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):
@read_read.interactive = internal unnamed_addr global i32 -1
@.str = [10 x i8] c"read_read\00"
@STDIN_FILENO = global i32 0
@stdout = global i32 0
define i8* @read_read(i8* nocapture %0) {
%2 = alloca i8
%3 = tail call i64 @gc_alloc(i32 80, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%4 = inttoptr i64 %3 to i8*
store i8 0, i8* %2
%5 = load i32, i32* @read_read.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* @read_read.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 @gc_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 @gc_alloc(i32 %32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%34 = inttoptr i64 %33 to i8*
%35 = tail call i32 @strcpy(i8* %34, i8* %20)
%36 = tail call i32 @gc_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
}