#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_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getchar();
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
char *lsh_read_line(void) {
  int bufsize = 1024;
  int position = 0;
  char *buffer = malloc(sizeof(char) * bufsize);
  int c;
  if (!buffer) {
    fprintf(stderr, "lsh: allocation error\n");
    exit(EXIT_FAILURE);
  }
  while (1) {
    c = getchar();
    if (c == EOF) {
      exit(EXIT_SUCCESS);
    } else if (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, "lsh: allocation error\n");
        exit(EXIT_FAILURE);
      }
    }
  }
}


x86 assembly (clang, -O0):
<lsh_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 <lsh_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 <lsh_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
jne    11d6 <lsh_read_line+0x86>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0xa,-0x14(%rbp)
jne    11f6 <lsh_read_line+0xa6>
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)
jmp    120b <lsh_read_line+0xbb>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jl     126e <lsh_read_line+0x11e>
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    1269 <lsh_read_line+0x119>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    126e <lsh_read_line+0x11e>
jmp    11ac <lsh_read_line+0x5c>

x86 assembly (clang, -O2):
<lsh_read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x400,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11c8 <lsh_read_line+0x78>
mov    %rax,%r15
xor    %eax,%eax
call   1030 <getchar@plt>
mov    0x2e71(%rip),%r14
cmp    (%r14),%eax
je     1200 <lsh_read_line+0xb0>
mov    $0x400,%ebp
xor    %ebx,%ebx
jmp    11a0 <lsh_read_line+0x50>
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <getchar@plt>
add    $0x1,%rbx
cmp    (%r14),%eax
je     1200 <lsh_read_line+0xb0>
cmp    $0xa,%eax
je     11eb <lsh_read_line+0x9b>
mov    %al,(%r15,%rbx,1)
lea    0x1(%rbx),%eax
cmp    %eax,%ebp
jg     1190 <lsh_read_line+0x40>
add    $0x400,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1190 <lsh_read_line+0x40>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

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