#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 getchar();
int /*<<< orphan*/ mrsh_exit(int /*<<< orphan*/ *);
char *mrsh_malloc(int);
char *mrsh_realloc(char *, int);
char *mrsh_read_line(void) {
  int buflen = 1024;
  char *buf = mrsh_malloc(buflen);
  for (int c, pos = 0;; pos++) {
    c = getchar();
    if (c == EOF) {
      mrsh_exit(NULL);
    } else if (c == '\n') {
      buf[pos] = '\0';
      return buf;
    } else {
      buf[pos] = c;
    }
    if (pos + 1 >= buflen) {
      buflen += 1024;
      buf = mrsh_realloc(buf, buflen);
    }
  }
}


x86 assembly (clang, -O0):
<mrsh_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x400,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <mrsh_malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%eax
jne    118c <mrsh_read_line+0x4c>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <mrsh_exit@plt>
jmp    11c1 <mrsh_read_line+0x81>
cmpl   $0xa,-0x14(%rbp)
jne    11ac <mrsh_read_line+0x6c>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11c1 <mrsh_read_line+0x81>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cmp    -0x4(%rbp),%eax
jl     11eb <mrsh_read_line+0xab>
mov    -0x4(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1060 <mrsh_realloc@plt>
mov    %rax,-0x10(%rbp)
jmp    11f0 <mrsh_read_line+0xb0>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1162 <mrsh_read_line+0x22>

x86 assembly (clang, -O2):
<mrsh_read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x400,%ebp
mov    $0x400,%edi
call   1030 <mrsh_malloc@plt>
mov    %rax,%r14
mov    0x2e88(%rip),%r15
xor    %ebx,%ebx
jmp    117f <mrsh_read_line+0x3f>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
call   1050 <mrsh_exit@plt>
add    $0x1,%rbx
cmp    %ebx,%ebp
jle    1196 <mrsh_read_line+0x56>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    (%r15),%eax
je     1170 <mrsh_read_line+0x30>
cmp    $0xa,%eax
je     11ab <mrsh_read_line+0x6b>
mov    %al,(%r14,%rbx,1)
jmp    1177 <mrsh_read_line+0x37>
add    $0x400,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <mrsh_realloc@plt>
mov    %rax,%r14
jmp    117f <mrsh_read_line+0x3f>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define i8* @mrsh_read_line() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 1024, i32* %1
%5 = load i32, i32* %1
%6 = call i8* @mrsh_malloc(i32 %5)
store i8* %6, i8** %2
store i32 0, i32* %4
br label %7
7: ; preds = %43, %0
%8 = call i32 (...) @getchar()
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = call i32 @mrsh_exit(i32* null)
br label %31
14: ; preds = %7
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, 10
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i8*, i8** %2
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
%22 = load i8*, i8** %2
ret i8* %22
23: ; preds = %14
%24 = load i32, i32* %3
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %2
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
br label %30
30: ; preds = %23
br label %31
31: ; preds = %30, %12
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
%34 = load i32, i32* %1
%35 = icmp sge i32 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = load i32, i32* %1
%38 = add nsw i32 %37, 1024
store i32 %38, i32* %1
%39 = load i8*, i8** %2
%40 = load i32, i32* %1
%41 = call i8* @mrsh_realloc(i8* %39, i32 %40)
store i8* %41, i8** %2
br label %42
42: ; preds = %36, %31
br label %43
43: ; preds = %42
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %7
}

LLVM IR (-O2):
@EOF = global i32 0
define i8* @mrsh_read_line() {
%1 = tail call i8* @mrsh_malloc(i32 1024)
br label %2
2: ; preds = %25, %0
%3 = phi i64 [ %22, %25 ], [ 0, %0 ]
%4 = phi i8* [ %27, %25 ], [ %1, %0 ]
%5 = phi i32 [ %26, %25 ], [ 1024, %0 ]
br label %6
6: ; preds = %2, %21
%7 = phi i64 [ %22, %21 ], [ %3, %2 ]
%8 = tail call i32 (...) @getchar()
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @mrsh_exit(i32* null)
br label %21
13: ; preds = %6
%14 = icmp eq i32 %8, 10
br i1 %14, label %15, label %18
15: ; preds = %13
%16 = and i64 %7, 4294967295
%17 = getelementptr inbounds i8, i8* %4, i64 %16
store i8 0, i8* %17
ret i8* %4
18: ; preds = %13
%19 = trunc i32 %8 to i8
%20 = getelementptr inbounds i8, i8* %4, i64 %7
store i8 %19, i8* %20
br label %21
21: ; preds = %18, %11
%22 = add nuw i64 %7, 1
%23 = trunc i64 %22 to i32
%24 = icmp sgt i32 %5, %23
br i1 %24, label %6, label %25
25: ; preds = %21
%26 = add nuw nsw i32 %5, 1024
%27 = tail call i8* @mrsh_realloc(i8* %4, i32 %26)
br label %2
}