#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 EAGAIN;
int EINVAL;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ isprint(int);
int /*<<< orphan*/ kbhit();
int /*<<< orphan*/ nonblock(int /*<<< orphan*/);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
int xbee_readline(char *buffer, int length) {
  static int state = 0;
  int c;
  static char *cursor;
  if (buffer == NULL || length < 1) {
    return -EINVAL;
  }
  switch (state) {
  default:
  case 0:
    nonblock(0);
    state = 1;
  case 1:
    cursor = buffer;
    *buffer = '\0';
    state = 2;
  case 2:
    if (!kbhit()) {
      usleep(1000);
      return -EAGAIN;
    }
    c = getchar();
    switch (c) {
    case 0x7F:
    case '\b':
      if (buffer != cursor) {
        fputs("\b \b", stdout);
        cursor--;
      }
      break;
    case '\n':
    case '\r':
      putchar('\n');
      state = 1;
      return cursor - buffer;
    default:
      if (isprint(c) && (cursor - buffer < length - 1)) {
        *cursor++ = c;
        putchar(c);
      } else {
        putchar('\x07');
      }
      break;
    }
    *cursor = 0;
    fflush(stdout);
  }
  return -EAGAIN;
}


x86 assembly (clang, -O0):
<xbee_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a4 <xbee_readline+0x24>
cmpl   $0x1,-0x14(%rbp)
jge    11b7 <xbee_readline+0x37>
xor    %eax,%eax
mov    0x2e33(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1398 <xbee_readline+0x218>
mov    0x2eab(%rip),%eax
mov    %eax,-0x1c(%rbp)
test   %eax,%eax
je     11f4 <xbee_readline+0x74>
jmp    11cd <xbee_readline+0x4d>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
je     1205 <xbee_readline+0x85>
jmp    11de <xbee_readline+0x5e>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     1221 <xbee_readline+0xa1>
jmp    11ef <xbee_readline+0x6f>
jmp    11f4 <xbee_readline+0x74>
xor    %edi,%edi
call   1040 <nonblock@plt>
movl   $0x1,0x2e63(%rip)
mov    -0x10(%rbp),%rax
mov    %rax,0x2e60(%rip)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x2,0x2e47(%rip)
mov    $0x0,%al
call   1050 <kbhit@plt>
cmp    $0x0,%eax
jne    124e <xbee_readline+0xce>
mov    $0x3e8,%edi
call   10a0 <usleep@plt>
xor    %eax,%eax
mov    0x2d84(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1398 <xbee_readline+0x218>
xor    %eax,%eax
call   1070 <getchar@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0x8,%eax
je     129f <xbee_readline+0x11f>
jmp    126c <xbee_readline+0xec>
mov    -0x20(%rbp),%eax
sub    $0xa,%eax
je     12dc <xbee_readline+0x15c>
jmp    127d <xbee_readline+0xfd>
mov    -0x20(%rbp),%eax
sub    $0xd,%eax
je     12dc <xbee_readline+0x15c>
jmp    128e <xbee_readline+0x10e>
mov    -0x20(%rbp),%eax
sub    $0x7f,%eax
jne    1306 <xbee_readline+0x186>
jmp    129f <xbee_readline+0x11f>
mov    -0x10(%rbp),%rax
cmp    0x2dc6(%rip),%rax
je     12d7 <xbee_readline+0x157>
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
lea    0xd40(%rip),%rdi
call   1060 <fputs@plt>
mov    0x2da4(%rip),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,0x2d99(%rip)
jmp    1372 <xbee_readline+0x1f2>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x1,0x2d78(%rip)
mov    0x2d79(%rip),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    1398 <xbee_readline+0x218>
mov    -0x18(%rbp),%edi
call   1090 <isprint@plt>
cmp    $0x0,%eax
je     1363 <xbee_readline+0x1e3>
mov    0x2d52(%rip),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jge    1363 <xbee_readline+0x1e3>
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    0x2d2d(%rip),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,0x2d1f(%rip)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
movsbl %al,%edi
call   1030 <putchar@plt>
jmp    136d <xbee_readline+0x1ed>
mov    $0x7,%edi
call   1030 <putchar@plt>
jmp    1372 <xbee_readline+0x1f2>
mov    0x2cf7(%rip),%rax
movb   $0x0,(%rax)
mov    0x2c55(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
xor    %eax,%eax
mov    0x2c35(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xbee_readline>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     11e5 <xbee_readline+0x65>
mov    %esi,%r14d
test   %esi,%esi
jle    11e5 <xbee_readline+0x65>
mov    %rdi,%rbx
mov    0x2ecf(%rip),%eax
cmp    $0x1,%eax
je     11aa <xbee_readline+0x2a>
cmp    $0x2,%eax
je     11be <xbee_readline+0x3e>
xor    %edi,%edi
call   1040 <nonblock@plt>
mov    %rbx,0x2ebf(%rip)
movb   $0x0,(%rbx)
movl   $0x2,0x2eaa(%rip)
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <kbhit@plt>
test   %eax,%eax
je     11f1 <xbee_readline+0x71>
xor    %eax,%eax
call   1070 <getchar@plt>
mov    %eax,%ebp
cmp    $0xc,%eax
jg     1207 <xbee_readline+0x87>
cmp    $0x8,%ebp
je     1211 <xbee_readline+0x91>
cmp    $0xa,%ebp
je     1239 <xbee_readline+0xb9>
jmp    1257 <xbee_readline+0xd7>
mov    0x2df4(%rip),%rax
jmp    12b5 <xbee_readline+0x135>
mov    $0x3e8,%edi
call   10a0 <usleep@plt>
mov    0x2dc6(%rip),%rax
jmp    12b7 <xbee_readline+0x137>
cmp    $0xd,%ebp
je     1239 <xbee_readline+0xb9>
cmp    $0x7f,%ebp
jne    1257 <xbee_readline+0xd7>
cmp    %rbx,0x2e58(%rip)
je     1296 <xbee_readline+0x116>
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    0xdd6(%rip),%rdi
call   1060 <fputs@plt>
addq   $0xffffffffffffffff,0x2e39(%rip)
ff
jmp    1296 <xbee_readline+0x116>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x1,0x2e1b(%rip)
mov    0x2e1d(%rip),%ebp
sub    %ebx,%ebp
jmp    12b9 <xbee_readline+0x139>
mov    %ebp,%edi
call   1090 <isprint@plt>
test   %eax,%eax
je     128c <xbee_readline+0x10c>
mov    0x2e07(%rip),%rax
mov    %rax,%rcx
sub    %rbx,%rcx
add    $0xffffffff,%r14d
cmp    %r14,%rcx
jge    128c <xbee_readline+0x10c>
lea    0x1(%rax),%rcx
mov    %rcx,0x2ded(%rip)
mov    %bpl,(%rax)
movsbl %bpl,%edi
jmp    1291 <xbee_readline+0x111>
mov    $0x7,%edi
call   1030 <putchar@plt>
mov    0x2dd3(%rip),%rax
movb   $0x0,(%rax)
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    0x2d13(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@xbee_readline.state = internal global i32 0
@xbee_readline.cursor = internal global i8* null
@EINVAL = global i32 0
@EAGAIN = global i32 0
@.str = [4 x i8] c"\08 \08\00"
@stdout = global i32 0
define i32 @xbee_readline(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %9, %2
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
store i32 %14, i32* %3
br label %83
15: ; preds = %9
%16 = load i32, i32* @xbee_readline.state
switch i32 %16, label %17 [
i32 0, label %18
i32 1, label %20
i32 2, label %23
]
17: ; preds = %15
br label %18
18: ; preds = %15, %17
%19 = call i32 @nonblock(i32 0)
store i32 1, i32* @xbee_readline.state
br label %20
20: ; preds = %15, %18
%21 = load i8*, i8** %4
store i8* %21, i8** @xbee_readline.cursor
%22 = load i8*, i8** %4
store i8 0, i8* %22
store i32 2, i32* @xbee_readline.state
br label %23
23: ; preds = %15, %20
%24 = call i32 (...) @kbhit()
%25 = icmp ne i32 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = call i32 @usleep(i32 1000)
%28 = load i32, i32* @EAGAIN
%29 = sub nsw i32 0, %28
store i32 %29, i32* %3
br label %83
30: ; preds = %23
%31 = call i32 (...) @getchar()
store i32 %31, i32* %6
%32 = load i32, i32* %6
switch i32 %32, label %51 [
i32 127, label %33
i32 8, label %33
i32 10, label %43
i32 13, label %43
]
33: ; preds = %30, %30
%34 = load i8*, i8** %4
%35 = load i8*, i8** @xbee_readline.cursor
%36 = icmp ne i8* %34, %35
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* @stdout
%39 = call i32 @fputs(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %38)
%40 = load i8*, i8** @xbee_readline.cursor
%41 = getelementptr inbounds i8, i8* %40, i32 -1
store i8* %41, i8** @xbee_readline.cursor
br label %42
42: ; preds = %37, %33
br label %76
43: ; preds = %30, %30
%44 = call i32 @putchar(i8 signext 10)
store i32 1, i32* @xbee_readline.state
%45 = load i8*, i8** @xbee_readline.cursor
%46 = load i8*, i8** %4
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %46 to i64
%49 = sub i64 %47, %48
%50 = trunc i64 %49 to i32
store i32 %50, i32* %3
br label %83
51: ; preds = %30
%52 = load i32, i32* %6
%53 = call i32 @isprint(i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %73
55: ; preds = %51
%56 = load i8*, i8** @xbee_readline.cursor
%57 = load i8*, i8** %4
%58 = ptrtoint i8* %56 to i64
%59 = ptrtoint i8* %57 to i64
%60 = sub i64 %58, %59
%61 = load i32, i32* %5
%62 = sub nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = icmp slt i64 %60, %63
br i1 %64, label %65, label %73
65: ; preds = %55
%66 = load i32, i32* %6
%67 = trunc i32 %66 to i8
%68 = load i8*, i8** @xbee_readline.cursor
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** @xbee_readline.cursor
store i8 %67, i8* %68
%70 = load i32, i32* %6
%71 = trunc i32 %70 to i8
%72 = call i32 @putchar(i8 signext %71)
br label %75
73: ; preds = %55, %51
%74 = call i32 @putchar(i8 signext 7)
br label %75
75: ; preds = %73, %65
br label %76
76: ; preds = %75, %42
%77 = load i8*, i8** @xbee_readline.cursor
store i8 0, i8* %77
%78 = load i32, i32* @stdout
%79 = call i32 @fflush(i32 %78)
br label %80
80: ; preds = %76
%81 = load i32, i32* @EAGAIN
%82 = sub nsw i32 0, %81
store i32 %82, i32* %3
br label %83
83: ; preds = %80, %43, %26, %12
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@xbee_readline.state = internal unnamed_addr global i32 0
@xbee_readline.cursor = internal unnamed_addr global i8* null
@EINVAL = global i32 0
@EAGAIN = global i32 0
@.str = [4 x i8] c"\08 \08\00"
@stdout = global i32 0
define i32 @xbee_readline(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp slt i32 %1, 1
%5 = or i1 %3, %4
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @EINVAL
%8 = sub nsw i32 0, %7
br label %61
9: ; preds = %2
%10 = load i32, i32* @xbee_readline.state
switch i32 %10, label %11 [
i32 2, label %14
i32 1, label %13
]
11: ; preds = %9
%12 = tail call i32 @nonblock(i32 0)
br label %13
13: ; preds = %9, %11
store i8* %0, i8** @xbee_readline.cursor
store i8 0, i8* %0
store i32 2, i32* @xbee_readline.state
br label %14
14: ; preds = %9, %13
%15 = tail call i32 (...) @kbhit()
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = tail call i32 @usleep(i32 1000)
%19 = load i32, i32* @EAGAIN
%20 = sub nsw i32 0, %19
br label %61
21: ; preds = %14
%22 = tail call i32 (...) @getchar()
switch i32 %22, label %38 [
i32 127, label %23
i32 8, label %23
i32 10, label %31
i32 13, label %31
]
23: ; preds = %21, %21
%24 = load i8*, i8** @xbee_readline.cursor
%25 = icmp eq i8* %24, %0
br i1 %25, label %55, label %26
26: ; preds = %23
%27 = load i32, i32* @stdout
%28 = tail call i32 @fputs(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = load i8*, i8** @xbee_readline.cursor
%30 = getelementptr inbounds i8, i8* %29, i64 -1
store i8* %30, i8** @xbee_readline.cursor
br label %55
31: ; preds = %21, %21
%32 = tail call i32 @putchar(i8 signext 10)
store i32 1, i32* @xbee_readline.state
%33 = load i8*, i8** @xbee_readline.cursor
%34 = ptrtoint i8* %33 to i64
%35 = ptrtoint i8* %0 to i64
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
br label %61
38: ; preds = %21
%39 = tail call i32 @isprint(i32 %22)
%40 = icmp eq i32 %39, 0
br i1 %40, label %53, label %41
41: ; preds = %38
%42 = load i8*, i8** @xbee_readline.cursor
%43 = ptrtoint i8* %42 to i64
%44 = ptrtoint i8* %0 to i64
%45 = sub i64 %43, %44
%46 = add nsw i32 %1, -1
%47 = zext i32 %46 to i64
%48 = icmp slt i64 %45, %47
br i1 %48, label %49, label %53
49: ; preds = %41
%50 = trunc i32 %22 to i8
%51 = getelementptr inbounds i8, i8* %42, i64 1
store i8* %51, i8** @xbee_readline.cursor
store i8 %50, i8* %42
%52 = tail call i32 @putchar(i8 signext %50)
br label %55
53: ; preds = %41, %38
%54 = tail call i32 @putchar(i8 signext 7)
br label %55
55: ; preds = %49, %53, %23, %26
%56 = load i8*, i8** @xbee_readline.cursor
store i8 0, i8* %56
%57 = load i32, i32* @stdout
%58 = tail call i32 @fflush(i32 %57)
%59 = load i32, i32* @EAGAIN
%60 = sub nsw i32 0, %59
br label %61
61: ; preds = %55, %31, %17, %6
%62 = phi i32 [ %8, %6 ], [ %60, %55 ], [ %37, %31 ], [ %20, %17 ]
ret i32 %62
}