#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();
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
char *pshell_read_line(void) {
  int bufsize = 1024;
  int position = 0;
  char *buffer = malloc(sizeof(char) * bufsize);
  int c;
  if (!buffer) {
    fprintf(stderr, "pshell: 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 = realloc(buffer, bufsize);
      if (!buffer) {
        fprintf(stderr, "pshell: allocation error\n");
        exit(EXIT_FAILURE);
      }
    }
  }
}


x86 assembly (clang, -O0):
<pshell_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 <pshell_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 <pshell_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 <pshell_read_line+0x82>
cmpl   $0xa,-0x14(%rbp)
jne    11e8 <pshell_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 <pshell_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 <pshell_read_line+0x106>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    125b <pshell_read_line+0x10b>
jmp    11ac <pshell_read_line+0x5c>

x86 assembly (clang, -O2):
<pshell_read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x400,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11da <pshell_read_line+0x8a>
mov    %rax,%r15
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     11c7 <pshell_read_line+0x77>
mov    0x2e6a(%rip),%r14
cmp    (%r14),%eax
je     11c7 <pshell_read_line+0x77>
mov    $0x400,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %ebx,%ebp
jg     11b4 <pshell_read_line+0x64>
add    $0x400,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11da <pshell_read_line+0x8a>
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     11c5 <pshell_read_line+0x75>
cmp    (%r14),%eax
jne    1190 <pshell_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    0xe16(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

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