char *readcons(char *prompt, char *def, int raw) {
  int fdin, fdout, ctl, n;
  char line[10];
  char *s;
  fdin = open("/dev/cons", 0);
  if (fdin < 0)
    fdin = 0;
  fdout = open("/dev/cons", 1);
  if (fdout < 0)
    fdout = 1;
  if (def != ((void *)0))
    fprint(fdout, "%s[%s]: ", prompt, def);
  else
    fprint(fdout, "%s: ", prompt);
  if (raw) {
    ctl = open("/dev/consctl", 1);
    if (ctl >= 0)
      write(ctl, "rawon", 5);
  } else
    ctl = -1;
  s = estrdup("");
  for (;;) {
    n = read(fdin, line, 1);
    if (n == 0) {
    Error:
      close(fdin);
      close(fdout);
      if (ctl >= 0)
        close(ctl);
      free(s);
      return ((void *)0);
    }
    if (n < 0)
      goto Error;
    if (line[0] == 0x7f)
      goto Error;
    if (n == 0 || line[0] == '\n' || line[0] == '\r') {
      if (raw) {
        write(ctl, "rawoff", 6);
        write(fdout, "\n", 1);
      }
      close(ctl);
      close(fdin);
      close(fdout);
      if (*s == 0 && def != ((void *)0))
        s = estrappend(s, "%s", def);
      return s;
    }
    if (line[0] == '\b') {
      if (strlen(s) > 0)
        s[strlen(s) - 1] = 0;
    } else if (line[0] == 0x15) {
      if (def != ((void *)0))
        fprint(fdout, "\n%s[%s]: ", prompt, def);
      else
        fprint(fdout, "\n%s: ", prompt);
      s[0] = 0;
    } else {
      s = estrappend(s, "%c", line[0]);
    }
  }
  return ((void *)0);
}


x86 assembly (clang, -O0):
<readcons>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    0xe56(%rip),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c7 <readcons+0x37>
movl   $0x0,-0x20(%rbp)
lea    0xe32(%rip),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ee <readcons+0x5e>
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1217 <readcons+0x87>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprint@plt>
jmp    122c <readcons+0x9c>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprint@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1271 <readcons+0xe1>
lea    0xdcd(%rip),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     126c <readcons+0xdc>
mov    -0x28(%rbp),%edi
lea    0xdb7(%rip),%rsi
mov    $0x5,%edx
mov    $0x0,%al
call   1040 <write@plt>
jmp    1278 <readcons+0xe8>
movl   $0xffffffff,-0x28(%rbp)
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1090 <estrdup@plt>
cltq
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x36(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1080 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12ef <readcons+0x15f>
jmp    12b1 <readcons+0x121>
mov    -0x20(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
mov    -0x24(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
cmpl   $0x0,-0x28(%rbp)
jl     12d9 <readcons+0x149>
mov    -0x28(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    148e <readcons+0x2fe>
cmpl   $0x0,-0x2c(%rbp)
jge    12fe <readcons+0x16e>
jmp    12b1 <readcons+0x121>
movsbl -0x36(%rbp),%eax
cmp    $0x7f,%eax
jne    1310 <readcons+0x180>
jmp    12b1 <readcons+0x121>
cmpl   $0x0,-0x2c(%rbp)
je     1334 <readcons+0x1a4>
movsbl -0x36(%rbp),%eax
cmp    $0xa,%eax
je     1334 <readcons+0x1a4>
movsbl -0x36(%rbp),%eax
cmp    $0xd,%eax
jne    13cc <readcons+0x23c>
cmpl   $0x0,-0x1c(%rbp)
je     136a <readcons+0x1da>
mov    -0x28(%rbp),%edi
lea    0xcd5(%rip),%rsi
mov    $0x6,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x24(%rbp),%edi
lea    0xcc6(%rip),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x28(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
mov    -0x20(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
mov    -0x24(%rbp),%edi
mov    $0x0,%al
call   1070 <close@plt>
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    13bf <readcons+0x22f>
cmpq   $0x0,-0x18(%rbp)
je     13bf <readcons+0x22f>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xc74(%rip),%rsi
mov    $0x0,%al
call   10b0 <estrappend@plt>
cltq
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    148e <readcons+0x2fe>
movsbl -0x36(%rbp),%eax
cmp    $0x8,%eax
jne    1411 <readcons+0x281>
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jbe    140c <readcons+0x27c>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1489 <readcons+0x2f9>
movsbl -0x36(%rbp),%eax
cmp    $0x15,%eax
jne    1468 <readcons+0x2d8>
cmpq   $0x0,-0x18(%rbp)
je     1447 <readcons+0x2b7>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xbee(%rip),%rsi
mov    $0x0,%al
call   1060 <fprint@plt>
jmp    145c <readcons+0x2cc>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    0xbde(%rip),%rsi
mov    $0x0,%al
call   1060 <fprint@plt>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
jmp    1484 <readcons+0x2f4>
mov    -0x40(%rbp),%rdi
movsbl -0x36(%rbp),%edx
lea    0xbc2(%rip),%rsi
mov    $0x0,%al
call   10b0 <estrappend@plt>
cltq
mov    %rax,-0x40(%rbp)
jmp    1489 <readcons+0x2f9>
jmp    128c <readcons+0xfc>
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readcons>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe8a(%rip),%r13
mov    %r13,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
mov    $0x0,%ebx
cmovns %eax,%ebx
mov    $0x1,%r15d
mov    %r13,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%r13d
test   %eax,%eax
cmovs  %r15d,%r13d
test   %rbp,%rbp
mov    %rbp,0x20(%rsp)
je     123a <readcons+0xaa>
lea    0xe6f(%rip),%rsi
mov    %r13d,%edi
mov    %r14,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1060 <fprint@plt>
test   %r12d,%r12d
je     1253 <readcons+0xc3>
lea    0xe33(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     1259 <readcons+0xc9>
lea    0xe26(%rip),%rsi
mov    %r15d,%edi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <write@plt>
jmp    1259 <readcons+0xc9>
lea    0xe2b(%rip),%rsi
mov    %r13d,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprint@plt>
test   %r12d,%r12d
jne    1208 <readcons+0x78>
mov    $0xffffffff,%r15d
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1090 <estrdup@plt>
movslq %eax,%rbp
lea    0x16(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1080 <read@plt>
test   %eax,%eax
mov    %r15d,0xc(%rsp)
jle    1357 <readcons+0x1c7>
mov    0x16(%rsp),%al
cmp    $0x7f,%al
je     1357 <readcons+0x1c7>
mov    %r12d,0x10(%rsp)
lea    0x16(%rsp),%r15
lea    0xd59(%rip),%r12
nopw   0x0(%rax,%rax,1)
lea    -0x8(%rax),%ecx
cmp    $0xd,%cl
ja     12e0 <readcons+0x150>
movzbl %cl,%ecx
movslq (%r12,%rcx,4),%rcx
add    %r12,%rcx
jmp    *%rcx
mov    %rbp,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     1335 <readcons+0x1a5>
movb   $0x0,-0x1(%rax,%rbp,1)
jmp    1335 <readcons+0x1a5>
nopl   0x0(%rax,%rax,1)
movsbl %al,%edx
mov    %rbp,%rdi
lea    0xd84(%rip),%rsi
xor    %eax,%eax
call   10b0 <estrappend@plt>
movslq %eax,%rbp
jmp    1335 <readcons+0x1a5>
nopl   0x0(%rax)
mov    0x20(%rsp),%rcx
mov    %r13d,%edi
test   %rcx,%rcx
je     1320 <readcons+0x190>
lea    0xd4d(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprint@plt>
jmp    1331 <readcons+0x1a1>
lea    0xd44(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprint@plt>
movb   $0x0,0x0(%rbp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1080 <read@plt>
test   %eax,%eax
jle    1357 <readcons+0x1c7>
movzbl 0x16(%rsp),%eax
cmp    $0x7f,%al
jne    12b0 <readcons+0x120>
mov    %ebx,%edi
xor    %eax,%eax
call   1070 <close@plt>
mov    %r13d,%edi
xor    %eax,%eax
call   1070 <close@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
js     1379 <readcons+0x1e9>
xor    %eax,%eax
call   1070 <close@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmpl   $0x0,0x10(%rsp)
mov    0xc(%rsp),%r14d
je     13cd <readcons+0x23d>
lea    0xcad(%rip),%rsi
mov    %r14d,%edi
mov    $0x6,%edx
xor    %eax,%eax
call   1040 <write@plt>
lea    0xc9e(%rip),%rsi
mov    %r13d,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <write@plt>
mov    %r14d,%edi
xor    %eax,%eax
call   1070 <close@plt>
mov    %ebx,%edi
xor    %eax,%eax
call   1070 <close@plt>
mov    %r13d,%edi
xor    %eax,%eax
call   1070 <close@plt>
mov    0x20(%rsp),%rdx
test   %rdx,%rdx
je     1383 <readcons+0x1f3>
cmpb   $0x0,0x0(%rbp)
jne    1383 <readcons+0x1f3>
lea    0xc5d(%rip),%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   10b0 <estrappend@plt>
movslq %eax,%rbp
jmp    1383 <readcons+0x1f3>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/cons\00"
@.str.1 = [9 x i8] c"%s[%s]: \00"
@.str.2 = [5 x i8] c"%s: \00"
@.str.3 = [13 x i8] c"/dev/consctl\00"
@.str.4 = [6 x i8] c"rawon\00"
@.str.5 = [1 x i8] zeroinitializer
@.str.6 = [7 x i8] c"rawoff\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [3 x i8] c"%s\00"
@.str.9 = [10 x i8] c"\0A%s[%s]: \00"
@.str.10 = [6 x i8] c"\0A%s: \00"
@.str.11 = [3 x i8] c"%c\00"
define i8* @readcons(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [10 x i8]
%13 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%14 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 0, i32* %8
br label %18
18: ; preds = %17, %3
%19 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 1, i32* %9
br label %23
23: ; preds = %22, %18
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i32, i32* %9
%28 = load i8*, i8** %5
%29 = load i8*, i8** %6
%30 = call i32 (i32, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fprint to i32 (i32, i8*, i8*, i8*, ...)*)(i32 %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %29)
br label %35
31: ; preds = %23
%32 = load i32, i32* %9
%33 = load i8*, i8** %5
%34 = call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @fprint to i32 (i32, i8*, i8*, ...)*)(i32 %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %31, %26
%36 = load i32, i32* %7
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %35
%39 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 1)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %10
%44 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
br label %45
45: ; preds = %42, %38
br label %47
46: ; preds = %35
store i32 -1, i32* %10
br label %47
47: ; preds = %46, %45
%48 = call i32 (i8*, ...) bitcast (i32 (...)* @estrdup to i32 (i8*, ...)*)(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0))
%49 = sext i32 %48 to i64
%50 = inttoptr i64 %49 to i8*
store i8* %50, i8** %13
br label %51
51: ; preds = %168, %47
%52 = load i32, i32* %8
%53 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%54 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %52, i8* %53, i32 1)
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %70
57: ; preds = %51
br label %58
58: ; preds = %79, %73, %57
%59 = load i32, i32* %8
%60 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %59)
%61 = load i32, i32* %9
%62 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %61)
%63 = load i32, i32* %10
%64 = icmp sge i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %58
%66 = load i32, i32* %10
%67 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %66)
br label %68
68: ; preds = %65, %58
%69 = load i8*, i8** %13
store i8* null, i8** %4
br label %169
70: ; preds = %51
%71 = load i32, i32* %11
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %70
br label %58
74: ; preds = %70
%75 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = icmp eq i32 %77, 127
br i1 %78, label %79, label %80
79: ; preds = %74
br label %58
80: ; preds = %74
%81 = load i32, i32* %11
%82 = icmp eq i32 %81, 0
br i1 %82, label %93, label %83
83: ; preds = %80
%84 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = icmp eq i32 %86, 10
br i1 %87, label %93, label %88
88: ; preds = %83
%89 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%90 = load i8, i8* %89
%91 = sext i8 %90 to i32
%92 = icmp eq i32 %91, 13
br i1 %92, label %93, label %123
93: ; preds = %88, %83, %80
%94 = load i32, i32* %7
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %101
96: ; preds = %93
%97 = load i32, i32* %10
%98 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %97, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 6)
%99 = load i32, i32* %9
%100 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %99, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1)
br label %101
101: ; preds = %96, %93
%102 = load i32, i32* %10
%103 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %102)
%104 = load i32, i32* %8
%105 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %104)
%106 = load i32, i32* %9
%107 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %106)
%108 = load i8*, i8** %13
%109 = load i8, i8* %108
%110 = sext i8 %109 to i32
%111 = icmp eq i32 %110, 0
br i1 %111, label %112, label %121
112: ; preds = %101
%113 = load i8*, i8** %6
%114 = icmp ne i8* %113, null
br i1 %114, label %115, label %121
115: ; preds = %112
%116 = load i8*, i8** %13
%117 = load i8*, i8** %6
%118 = call i32 (i8*, i8*, i8*, ...) bitcast (i32 (...)* @estrappend to i32 (i8*, i8*, i8*, ...)*)(i8* %116, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %117)
%119 = sext i32 %118 to i64
%120 = inttoptr i64 %119 to i8*
store i8* %120, i8** %13
br label %121
121: ; preds = %115, %112, %101
%122 = load i8*, i8** %13
store i8* %122, i8** %4
br label %169
123: ; preds = %88
%124 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%125 = load i8, i8* %124
%126 = sext i8 %125 to i32
%127 = icmp eq i32 %126, 8
br i1 %127, label %128, label %139
128: ; preds = %123
%129 = load i8*, i8** %13
%130 = call i64 @strlen(i8* %129)
%131 = icmp ugt i64 %130, 0
br i1 %131, label %132, label %138
132: ; preds = %128
%133 = load i8*, i8** %13
%134 = load i8*, i8** %13
%135 = call i64 @strlen(i8* %134)
%136 = sub i64 %135, 1
%137 = getelementptr inbounds i8, i8* %133, i64 %136
store i8 0, i8* %137
br label %138
138: ; preds = %132, %128
br label %168
139: ; preds = %123
%140 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%141 = load i8, i8* %140
%142 = sext i8 %141 to i32
%143 = icmp eq i32 %142, 21
br i1 %143, label %144, label %159
144: ; preds = %139
%145 = load i8*, i8** %6
%146 = icmp ne i8* %145, null
br i1 %146, label %147, label %152
147: ; preds = %144
%148 = load i32, i32* %9
%149 = load i8*, i8** %5
%150 = load i8*, i8** %6
%151 = call i32 (i32, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fprint to i32 (i32, i8*, i8*, i8*, ...)*)(i32 %148, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8* %149, i8* %150)
br label %156
152: ; preds = %144
%153 = load i32, i32* %9
%154 = load i8*, i8** %5
%155 = call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @fprint to i32 (i32, i8*, i8*, ...)*)(i32 %153, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0), i8* %154)
br label %156
156: ; preds = %152, %147
%157 = load i8*, i8** %13
%158 = getelementptr inbounds i8, i8* %157, i64 0
store i8 0, i8* %158
br label %167
159: ; preds = %139
%160 = load i8*, i8** %13
%161 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%162 = load i8, i8* %161
%163 = sext i8 %162 to i32
%164 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @estrappend to i32 (i8*, i8*, i32, ...)*)(i8* %160, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i32 %163)
%165 = sext i32 %164 to i64
%166 = inttoptr i64 %165 to i8*
store i8* %166, i8** %13
br label %167
167: ; preds = %159, %156
br label %168
168: ; preds = %167, %138
br label %51
169: ; preds = %121, %68
%170 = load i8*, i8** %4
ret i8* %170
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/cons\00"
@.str.1 = [9 x i8] c"%s[%s]: \00"
@.str.2 = [5 x i8] c"%s: \00"
@.str.3 = [13 x i8] c"/dev/consctl\00"
@.str.4 = [6 x i8] c"rawon\00"
@.str.5 = [1 x i8] zeroinitializer
@.str.6 = [7 x i8] c"rawoff\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [3 x i8] c"%s\00"
@.str.9 = [10 x i8] c"\0A%s[%s]: \00"
@.str.10 = [6 x i8] c"\0A%s: \00"
@.str.11 = [3 x i8] c"%c\00"
define i8* @readcons(i8* %0, i8* %1, i32 %2) {
%4 = alloca [10 x i8]
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%6 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%7 = icmp sgt i32 %6, 0
%8 = select i1 %7, i32 %6, i32 0
%9 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 1)
%10 = icmp slt i32 %9, 0
%11 = select i1 %10, i32 1, i32 %9
%12 = icmp ne i8* %1, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = tail call i32 (i32, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fprint to i32 (i32, i8*, i8*, i8*, ...)*)(i32 %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
br label %17
15: ; preds = %3
%16 = tail call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @fprint to i32 (i32, i8*, i8*, ...)*)(i32 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %15, %13
%18 = icmp eq i32 %2, 0
br i1 %18, label %24, label %19
19: ; preds = %17
%20 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @open to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 1)
%21 = icmp sgt i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
br label %24
24: ; preds = %17, %19, %22
%25 = phi i32 [ %20, %22 ], [ %20, %19 ], [ -1, %17 ]
%26 = tail call i32 (i8*, ...) bitcast (i32 (...)* @estrdup to i32 (i8*, ...)*)(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0))
%27 = sext i32 %26 to i64
%28 = inttoptr i64 %27 to i8*
%29 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %8, i8* %5, i32 1)
%30 = icmp slt i32 %29, 1
%31 = load i8, i8* %5
%32 = icmp eq i8 %31, 127
%33 = select i1 %30, i1 true, i1 %32
br i1 %33, label %34, label %42
34: ; preds = %77, %24
%35 = phi i8* [ %28, %24 ], [ %78, %77 ]
%36 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %8)
%37 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %11)
%38 = icmp sgt i32 %25, -1
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %25)
br label %41
41: ; preds = %39, %34
br label %84
42: ; preds = %24, %77
%43 = phi i8 [ %81, %77 ], [ %31, %24 ]
%44 = phi i8* [ %78, %77 ], [ %28, %24 ]
switch i8 %43, label %72 [
i8 13, label %45
i8 10, label %45
i8 8, label %60
i8 21, label %66
]
45: ; preds = %42, %42
br i1 %18, label %49, label %46
46: ; preds = %45
%47 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 6)
%48 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1)
br label %49
49: ; preds = %46, %45
%50 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %25)
%51 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %8)
%52 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %11)
%53 = load i8, i8* %44
%54 = icmp eq i8 %53, 0
%55 = and i1 %12, %54
br i1 %55, label %56, label %84
56: ; preds = %49
%57 = call i32 (i8*, i8*, i8*, ...) bitcast (i32 (...)* @estrappend to i32 (i8*, i8*, i8*, ...)*)(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %1)
%58 = sext i32 %57 to i64
%59 = inttoptr i64 %58 to i8*
br label %84
60: ; preds = %42
%61 = call i64 @strlen(i8* dereferenceable(1) %44)
%62 = icmp eq i64 %61, 0
br i1 %62, label %77, label %63
63: ; preds = %60
%64 = add i64 %61, -1
%65 = getelementptr inbounds i8, i8* %44, i64 %64
store i8 0, i8* %65
br label %77
66: ; preds = %42
br i1 %12, label %67, label %69
67: ; preds = %66
%68 = call i32 (i32, i8*, i8*, i8*, ...) bitcast (i32 (...)* @fprint to i32 (i32, i8*, i8*, i8*, ...)*)(i32 %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8* %0, i8* %1)
br label %71
69: ; preds = %66
%70 = call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @fprint to i32 (i32, i8*, i8*, ...)*)(i32 %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0), i8* %0)
br label %71
71: ; preds = %69, %67
store i8 0, i8* %44
br label %77
72: ; preds = %42
%73 = sext i8 %43 to i32
%74 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @estrappend to i32 (i8*, i8*, i32, ...)*)(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i32 %73)
%75 = sext i32 %74 to i64
%76 = inttoptr i64 %75 to i8*
br label %77
77: ; preds = %71, %72, %60, %63
%78 = phi i8* [ %44, %63 ], [ %44, %60 ], [ %44, %71 ], [ %76, %72 ]
%79 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %8, i8* %5, i32 1)
%80 = icmp slt i32 %79, 1
%81 = load i8, i8* %5
%82 = icmp eq i8 %81, 127
%83 = select i1 %80, i1 true, i1 %82
br i1 %83, label %34, label %42
84: ; preds = %49, %56, %41
%85 = phi i8* [ null, %41 ], [ %59, %56 ], [ %44, %49 ]
ret i8* %85
}