#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getchar();
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stderr;
char *rshell_read_line(void) {
  int bufsize = 1024;
  int position = 0;
  char *buffer = (char *)malloc(sizeof(char) * bufsize);
  int c;
  if (!buffer) {
    fprintf(stderr, "rhsell: allocation error\n");
    exit(EXIT_FAILURE);
  }
  while (1) {
    c = getchar();
    if (c == EOF || c == '\n') {
      buffer[position] = '\0';
      return buffer;
    } else {
      buffer[position] = c;
    }
    position++;
    if (position >= bufsize) {
      bufsize += 1024;
      buffer = (char *)realloc(buffer, bufsize);
      if (!buffer) {
        fprintf(stderr, "rshell: allocation error\n");
        exit(EXIT_FAILURE);
      }
    }
  }
}


x86 assembly (clang, -O0):
<rshell_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x400,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a7 <rshell_read_line+0x57>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11ac <rshell_read_line+0x5c>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%eax
je     11d2 <rshell_read_line+0x82>
cmpl   $0xa,-0x14(%rbp)
jne    11e8 <rshell_read_line+0x98>
mov    -0x10(%rbp),%rax
movslq -0x8(%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 -0x8(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jl     125b <rshell_read_line+0x10b>
mov    -0x4(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1256 <rshell_read_line+0x106>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    125b <rshell_read_line+0x10b>
jmp    11ac <rshell_read_line+0x5c>

x86 assembly (clang, -O2):
<rshell_read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x400,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11fd <rshell_read_line+0xad>
mov    %rax,%r15
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     11c7 <rshell_read_line+0x77>
mov    0x2e66(%rip),%r14
cmp    (%r14),%eax
je     11c7 <rshell_read_line+0x77>
mov    $0x400,%ebp
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %ebx,%ebp
jg     11b4 <rshell_read_line+0x64>
add    $0x400,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11da <rshell_read_line+0x8a>
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     11c5 <rshell_read_line+0x75>
cmp    (%r14),%eax
jne    1190 <rshell_read_line+0x40>
mov    %ebx,%ebx
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
jmp    11ea <rshell_read_line+0x9a>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"rhsell: allocation error\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
@.str.1 = [26 x i8] c"rshell: allocation error\0A\00"
define i8* @rshell_read_line() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 1024, i32* %1
store i32 0, i32* %2
%5 = load i32, i32* %1
%6 = sext i32 %5 to i64
%7 = mul i64 1, %6
%8 = trunc i64 %7 to i32
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = icmp ne i8* %11, null
br i1 %12, label %18, label %13
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
br label %19
19: ; preds = %18, %61
%20 = call i32 (...) @getchar()
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %33
27: ; preds = %24, %19
%28 = load i8*, i8** %3
%29 = load i32, i32* %2
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %3
ret i8* %32
33: ; preds = %24
%34 = load i32, i32* %4
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %3
%37 = load i32, i32* %2
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 %35, i8* %39
br label %40
40: ; preds = %33
%41 = load i32, i32* %2
%42 = add nsw i32 %41, 1
store i32 %42, i32* %2
%43 = load i32, i32* %2
%44 = load i32, i32* %1
%45 = icmp sge i32 %43, %44
br i1 %45, label %46, label %61
46: ; preds = %40
%47 = load i32, i32* %1
%48 = add nsw i32 %47, 1024
store i32 %48, i32* %1
%49 = load i8*, i8** %3
%50 = load i32, i32* %1
%51 = call i64 @realloc(i8* %49, i32 %50)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %3
%53 = load i8*, i8** %3
%54 = icmp ne i8* %53, null
br i1 %54, label %60, label %55
55: ; preds = %46
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %46
br label %61
61: ; preds = %60, %40
br label %19
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"rhsell: allocation error\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
@.str.1 = [26 x i8] c"rshell: allocation error\0A\00"
define i8* @rshell_read_line() {
%1 = tail call i64 @malloc(i32 1024)
%2 = inttoptr i64 %1 to i8*
%3 = icmp eq i64 %1, 0
br i1 %3, label %10, label %4
4: ; preds = %0
%5 = tail call i32 (...) @getchar()
%6 = load i32, i32* @EOF
%7 = icmp eq i32 %5, %6
%8 = icmp eq i32 %5, 10
%9 = or i1 %8, %7
br i1 %9, label %17, label %21
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %41
%16 = and i64 %28, 4294967295
br label %17
17: ; preds = %15, %4
%18 = phi i64 [ 0, %4 ], [ %16, %15 ]
%19 = phi i8* [ %2, %4 ], [ %42, %15 ]
%20 = getelementptr inbounds i8, i8* %19, i64 %18
store i8 0, i8* %20
ret i8* %19
21: ; preds = %4, %41
%22 = phi i64 [ %28, %41 ], [ 0, %4 ]
%23 = phi i32 [ %44, %41 ], [ %5, %4 ]
%24 = phi i32 [ %43, %41 ], [ 1024, %4 ]
%25 = phi i8* [ %42, %41 ], [ %2, %4 ]
%26 = trunc i32 %23 to i8
%27 = getelementptr inbounds i8, i8* %25, i64 %22
store i8 %26, i8* %27
%28 = add nuw i64 %22, 1
%29 = trunc i64 %28 to i32
%30 = icmp sgt i32 %24, %29
br i1 %30, label %41, label %31
31: ; preds = %21
%32 = add nsw i32 %24, 1024
%33 = tail call i64 @realloc(i8* %25, i32 %32)
%34 = inttoptr i64 %33 to i8*
%35 = icmp eq i64 %33, 0
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %31, %21
%42 = phi i8* [ %34, %31 ], [ %25, %21 ]
%43 = phi i32 [ %32, %31 ], [ %24, %21 ]
%44 = tail call i32 (...) @getchar()
%45 = load i32, i32* @EOF
%46 = icmp eq i32 %44, %45
%47 = icmp eq i32 %44, 10
%48 = or i1 %47, %46
br i1 %48, label %15, label %21
}