#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 *mysh_get_line() {
  int c;
  int position;
  char *buffer = malloc(sizeof(char) * 1024);
  int bufsize = 1024;
  if (buffer == NULL) {
    fprintf(stderr, "Failed to allocate memory!\n");
    exit(EXIT_FAILURE);
  }
  position = 0;
  while (1) {
    c = getchar();
    if (c == EOF || c == '\n') {
      buffer[position] = '\0';
      return buffer;
    }
    buffer[position++] = c;
    if (position >= bufsize) {
      bufsize = bufsize + 1024;
      buffer = realloc(buffer, bufsize);
      if (buffer == NULL) {
        fprintf(stderr, "Failed to allocate memory!\n");
        exit(EXIT_FAILURE);
      }
    }
  }
}


x86 assembly (clang, -O0):
<mysh_get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x400,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119b <mysh_get_line+0x4b>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%eax
je     11c8 <mysh_get_line+0x78>
cmpl   $0xa,-0x4(%rbp)
jne    11de <mysh_get_line+0x8e>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x4(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x8(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1252 <mysh_get_line+0x102>
mov    -0x14(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    124d <mysh_get_line+0xfd>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1252 <mysh_get_line+0x102>
jmp    11a2 <mysh_get_line+0x52>

x86 assembly (clang, -O2):
<mysh_get_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x400,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11da <mysh_get_line+0x8a>
mov    %rax,%r15
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     11c7 <mysh_get_line+0x77>
mov    0x2e6a(%rip),%r14
cmp    (%r14),%eax
je     11c7 <mysh_get_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 <mysh_get_line+0x64>
add    $0x400,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11da <mysh_get_line+0x8a>
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%eax
je     11c5 <mysh_get_line+0x75>
cmp    (%r14),%eax
jne    1190 <mysh_get_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 = [28 x i8] c"Failed to allocate memory!\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
define i8* @mysh_get_line() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = call i8* @malloc(i32 1024)
store i8* %5, i8** %3
store i32 1024, i32* %4
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
store i32 0, i32* %2
br label %14
14: ; preds = %13, %53
%15 = call i32 (...) @getchar()
store i32 %15, i32* %1
%16 = load i32, i32* %1
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = load i32, i32* %1
%21 = icmp eq i32 %20, 10
br i1 %21, label %22, label %28
22: ; preds = %19, %14
%23 = load i8*, i8** %3
%24 = load i32, i32* %2
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %3
ret i8* %27
28: ; preds = %19
%29 = load i32, i32* %1
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %3
%32 = load i32, i32* %2
%33 = add nsw i32 %32, 1
store i32 %33, i32* %2
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 %30, i8* %35
%36 = load i32, i32* %2
%37 = load i32, i32* %4
%38 = icmp sge i32 %36, %37
br i1 %38, label %39, label %53
39: ; preds = %28
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1024
store i32 %41, i32* %4
%42 = load i8*, i8** %3
%43 = load i32, i32* %4
%44 = call i8* @realloc(i8* %42, i32 %43)
store i8* %44, i8** %3
%45 = load i8*, i8** %3
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %52
47: ; preds = %39
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %39
br label %53
53: ; preds = %52, %28
br label %14
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Failed to allocate memory!\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
define i8* @mysh_get_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 ([28 x i8], [28 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 %26, 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 [ %26, %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 = add nuw i64 %21, 1
%27 = getelementptr inbounds i8, i8* %24, i64 %21
store i8 %25, i8* %27
%28 = trunc i64 %26 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 ([28 x i8], [28 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
}