#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 */
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char EOF;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ getline(char **, size_t *, int /*<<< orphan*/);
unsigned char *malloc(int);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
unsigned char *realloc(unsigned char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tty_raw();
int /*<<< orphan*/ tty_reset();
unsigned char *read_shell(int *count) {
  fprintf(stderr, "$ ");
  tty_reset();
  char *line;
  size_t len = 0;
  ssize_t size = getline(&line, &len, stdin);
  FILE *out = popen(line, "r");
  char c;
  int index = 0;
  int page = 1024;
  unsigned char *buf = malloc(page * sizeof(unsigned char));
  while ((c = getc(out)) != EOF) {
    buf[index] = (unsigned char)c;
    index++;
    if (index % page == 0) {
      page *= 2;
      unsigned char *tmp = realloc(buf, page);
      if (tmp != NULL) {
        buf = tmp;
      } else {
        fprintf(stderr, "WARN: out of space.\n");
        break;
      }
    }
  }
  *count = index;
  int status = pclose(out);
  fprintf(stderr, "[Exited: %d]\n", status);
  tty_raw();
  return buf;
}


x86 assembly (clang, -O0):
<read_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   10b0 <tty_reset@plt>
movq   $0x0,-0x18(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <getline@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x400,-0x34(%rbp)
movslq -0x34(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   10a0 <getc@plt>
mov    %al,-0x29(%rbp)
movsbl %al,%eax
mov    0x2dba(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     12af <read_shell+0x11f>
mov    -0x29(%rbp),%dl
mov    -0x40(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cltd
idivl  -0x34(%rbp)
cmp    $0x0,%edx
jne    12aa <read_shell+0x11a>
mov    -0x34(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1289 <read_shell+0xf9>
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    12a5 <read_shell+0x115>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd6c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12af <read_shell+0x11f>
jmp    12aa <read_shell+0x11a>
jmp    1210 <read_shell+0x80>
mov    -0x30(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
mov    -0x4c(%rbp),%edx
lea    0xd43(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <tty_raw@plt>
mov    -0x40(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_shell>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbp
mov    0x2e28(%rip),%rbx
mov    (%rbx),%edi
lea    0xe4f(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   10b0 <tty_reset@plt>
movq   $0x0,0x8(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1090 <getline@plt>
mov    0x10(%rsp),%rdi
lea    0xe14(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,%r15
mov    $0x400,%r12d
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   10a0 <getc@plt>
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%al
je     12a3 <read_shell+0x113>
mov    %rbp,(%rsp)
xor    %r14d,%r14d
jmp    124c <read_shell+0xbc>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rbx
mov    %r15,%rdi
call   10a0 <getc@plt>
mov    %rbp,%r14
mov    %rbx,%r13
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%al
je     1292 <read_shell+0x102>
mov    %al,0x0(%r13,%r14,1)
lea    0x1(%r14),%rbp
mov    %ebp,%eax
cltd
idiv   %r12d
test   %edx,%edx
jne    1230 <read_shell+0xa0>
add    %r12d,%r12d
mov    %r13,%rdi
mov    %r12d,%esi
call   1070 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1233 <read_shell+0xa3>
add    $0x1,%r14d
mov    0x2d50(%rip),%rbx
mov    (%rbx),%edi
lea    0xd7c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    129f <read_shell+0x10f>
mov    %rbx,%r13
mov    %ebp,%r14d
mov    0x2d31(%rip),%rbx
mov    (%rsp),%rbp
mov    %r14d,0x0(%rbp)
mov    %r15,%rdi
call   1040 <pclose@plt>
mov    (%rbx),%edi
lea    0xd62(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1030 <tty_raw@plt>
mov    %r13,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [3 x i8] c"$ \00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.2 = [21 x i8] c"WARN: out of space.\0A\00"
@.str.3 = [14 x i8] c"[Exited: %d]\0A\00"
define i8* @read_shell(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i32* %0, i32** %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (...) @tty_reset()
store i64 0, i64* %4
%16 = load i32, i32* @stdin
%17 = call i32 @getline(i8** %3, i64* %4, i32 %16)
store i32 %17, i32* %5
%18 = load i8*, i8** %3
%19 = call i32* @popen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
store i32 0, i32* %8
store i32 1024, i32* %9
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 1
%23 = trunc i64 %22 to i32
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %10
br label %25
25: ; preds = %58, %1
%26 = load i32*, i32** %6
%27 = call signext i8 @getc(i32* %26)
store i8 %27, i8* %7
%28 = sext i8 %27 to i32
%29 = load i8, i8* @EOF
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %28, %30
br i1 %31, label %32, label %59
32: ; preds = %25
%33 = load i8, i8* %7
%34 = load i8*, i8** %10
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 %33, i8* %37
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = load i32, i32* %9
%42 = srem i32 %40, %41
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %58
44: ; preds = %32
%45 = load i32, i32* %9
%46 = mul nsw i32 %45, 2
store i32 %46, i32* %9
%47 = load i8*, i8** %10
%48 = load i32, i32* %9
%49 = call i8* @realloc(i8* %47, i32 %48)
store i8* %49, i8** %11
%50 = load i8*, i8** %11
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %54
52: ; preds = %44
%53 = load i8*, i8** %11
store i8* %53, i8** %10
br label %57
54: ; preds = %44
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %59
57: ; preds = %52
br label %58
58: ; preds = %57, %32
br label %25
59: ; preds = %54, %25
%60 = load i32, i32* %8
%61 = load i32*, i32** %2
store i32 %60, i32* %61
%62 = load i32*, i32** %6
%63 = call i32 @pclose(i32* %62)
store i32 %63, i32* %12
%64 = load i32, i32* @stderr
%65 = load i32, i32* %12
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %65)
%67 = call i32 (...) @tty_raw()
%68 = load i8*, i8** %10
ret i8* %68
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [3 x i8] c"$ \00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.2 = [21 x i8] c"WARN: out of space.\0A\00"
@.str.3 = [14 x i8] c"[Exited: %d]\0A\00"
define i8* @read_shell(i32* nocapture writeonly %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (...) @tty_reset()
%7 = bitcast i8** %2 to i8*
%8 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%9 = load i32, i32* @stdin
%10 = call i32 @getline(i8** %2, i64* %3, i32 %9)
%11 = load i8*, i8** %2
%12 = call i32* @popen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%14 = call signext i8 @getc(i32* %12)
%15 = load i8, i8* @EOF
%16 = icmp eq i8 %14, %15
br i1 %16, label %43, label %17
17: ; preds = %1, %35
%18 = phi i64 [ %23, %35 ], [ 0, %1 ]
%19 = phi i8 [ %38, %35 ], [ %14, %1 ]
%20 = phi i32 [ %37, %35 ], [ 1024, %1 ]
%21 = phi i8* [ %36, %35 ], [ %13, %1 ]
%22 = getelementptr inbounds i8, i8* %21, i64 %18
store i8 %19, i8* %22
%23 = add nuw i64 %18, 1
%24 = trunc i64 %23 to i32
%25 = srem i32 %24, %20
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %17
%28 = shl nsw i32 %20, 1
%29 = call i8* @realloc(i8* %21, i32 %28)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = trunc i64 %23 to i32
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %43
35: ; preds = %27, %17
%36 = phi i8* [ %21, %17 ], [ %29, %27 ]
%37 = phi i32 [ %20, %17 ], [ %28, %27 ]
%38 = call signext i8 @getc(i32* %12)
%39 = load i8, i8* @EOF
%40 = icmp eq i8 %38, %39
br i1 %40, label %41, label %17
41: ; preds = %35
%42 = trunc i64 %23 to i32
br label %43
43: ; preds = %41, %1, %31
%44 = phi i8* [ %21, %31 ], [ %13, %1 ], [ %36, %41 ]
%45 = phi i32 [ %32, %31 ], [ 0, %1 ], [ %42, %41 ]
store i32 %45, i32* %0
%46 = call i32 @pclose(i32* %12)
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%49 = call i32 (...) @tty_raw()
ret i8* %44
}