#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 /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, ...);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ flushWrite();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int const);
int linenoiseProbe(void) {
  int stdin_fileno = fileno(stdin);
  int flags = fcntl(stdin_fileno, F_GETFL);
  flags |= O_NONBLOCK;
  int res = fcntl(stdin_fileno, F_SETFL, flags);
  if (res != 0) {
    return -1;
  }
  fprintf(stdout, "\x1b[5n");
  flushWrite();
  int timeout_ms = 200;
  const int retry_ms = 10;
  size_t read_bytes = 0;
  while (timeout_ms > 0 && read_bytes < 4) {
    usleep(retry_ms * 1000);
    timeout_ms -= retry_ms;
    char c;
    int cb = read(stdin_fileno, &c, 1);
    if (cb < 0) {
      continue;
    }
    read_bytes += cb;
  }
  flags &= ~O_NONBLOCK;
  res = fcntl(stdin_fileno, F_SETFL, flags);
  if (res != 0) {
    return -1;
  }
  if (read_bytes < 4) {
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<linenoiseProbe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11cd <linenoiseProbe+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <linenoiseProbe+0x16f>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1070 <flushWrite@plt>
movl   $0xc8,-0x14(%rbp)
movl   $0xa,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x29(%rbp)
jle    1219 <linenoiseProbe+0xb9>
cmpq   $0x4,-0x20(%rbp)
setb   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1229 <linenoiseProbe+0xc9>
jmp    1270 <linenoiseProbe+0x110>
mov    $0x2710,%edi
call   1080 <usleep@plt>
mov    -0x14(%rbp),%eax
sub    $0xa,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    125f <linenoiseProbe+0xff>
jmp    11ff <linenoiseProbe+0x9f>
movslq -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ff <linenoiseProbe+0x9f>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     12b1 <linenoiseProbe+0x151>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <linenoiseProbe+0x16f>
cmpq   $0x4,-0x20(%rbp)
jae    12c8 <linenoiseProbe+0x168>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12cf <linenoiseProbe+0x16f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linenoiseProbe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%ebx
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%r14d
mov    0x2e4e(%rip),%rax
or     (%rax),%r14d
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    125c <linenoiseProbe+0xfc>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1070 <flushWrite@plt>
mov    $0xc8,%r12d
lea    0x7(%rsp),%rbp
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
mov    %rax,%r13
mov    $0x2710,%edi
call   1080 <usleep@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
cmovs  %r15d,%eax
add    %r13,%rax
cmp    $0xb,%r12d
jb     1225 <linenoiseProbe+0xc5>
add    $0xfffffff6,%r12d
cmp    $0x4,%rax
jb     11f0 <linenoiseProbe+0x90>
xor    %ebp,%ebp
cmp    $0x4,%rax
setae  %bpl
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,%r14d
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
lea    -0x2(%rbp,%rbp,1),%ecx
neg    %eax
sbb    %eax,%eax
or     %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@stdout = global i32 0
@.str = [5 x i8] c"\1B[5n\00"
define i32 @linenoiseProbe() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8
%9 = alloca i32
%10 = load i32, i32* @stdin
%11 = call i32 @fileno(i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = load i32, i32* @F_GETFL
%14 = call i32 (i32, i32, ...) @fcntl(i32 %12, i32 %13)
store i32 %14, i32* %3
%15 = load i32, i32* @O_NONBLOCK
%16 = load i32, i32* %3
%17 = or i32 %16, %15
store i32 %17, i32* %3
%18 = load i32, i32* %2
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* %3
%21 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %0
store i32 -1, i32* %1
br label %68
25: ; preds = %0
%26 = load i32, i32* @stdout
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%28 = call i32 (...) @flushWrite()
store i32 200, i32* %5
store i32 10, i32* %6
store i64 0, i64* %7
br label %29
29: ; preds = %46, %45, %25
%30 = load i32, i32* %5
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i64, i64* %7
%34 = icmp ult i64 %33, 4
br label %35
35: ; preds = %32, %29
%36 = phi i1 [ false, %29 ], [ %34, %32 ]
br i1 %36, label %37, label %51
37: ; preds = %35
%38 = call i32 @usleep(i32 10000)
%39 = load i32, i32* %5
%40 = sub nsw i32 %39, 10
store i32 %40, i32* %5
%41 = load i32, i32* %2
%42 = call i32 @read(i32 %41, i8* %8, i32 1)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %37
br label %29
46: ; preds = %37
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = load i64, i64* %7
%50 = add i64 %49, %48
store i64 %50, i64* %7
br label %29
51: ; preds = %35
%52 = load i32, i32* @O_NONBLOCK
%53 = xor i32 %52, -1
%54 = load i32, i32* %3
%55 = and i32 %54, %53
store i32 %55, i32* %3
%56 = load i32, i32* %2
%57 = load i32, i32* @F_SETFL
%58 = load i32, i32* %3
%59 = call i32 (i32, i32, ...) @fcntl(i32 %56, i32 %57, i32 %58)
store i32 %59, i32* %4
%60 = load i32, i32* %4
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %51
store i32 -1, i32* %1
br label %68
63: ; preds = %51
%64 = load i64, i64* %7
%65 = icmp ult i64 %64, 4
br i1 %65, label %66, label %67
66: ; preds = %63
store i32 -2, i32* %1
br label %68
67: ; preds = %63
store i32 0, i32* %1
br label %68
68: ; preds = %67, %66, %62, %24
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
@stdin = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@stdout = global i32 0
@.str = [5 x i8] c"\1B[5n\00"
define i32 @linenoiseProbe() {
%1 = alloca i8
%2 = load i32, i32* @stdin
%3 = tail call i32 @fileno(i32 %2)
%4 = load i32, i32* @F_GETFL
%5 = tail call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %4)
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = load i32, i32* @F_SETFL
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %8, i32 %7)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %37
11: ; preds = %0
%12 = load i32, i32* @stdout
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 (...) @flushWrite()
br label %15
15: ; preds = %11, %15
%16 = phi i64 [ 0, %11 ], [ %24, %15 ]
%17 = phi i32 [ 200, %11 ], [ %19, %15 ]
%18 = tail call i32 @usleep(i32 10000)
%19 = add nsw i32 %17, -10
%20 = call i32 @read(i32 %3, i8* %1, i32 1)
%21 = icmp sgt i32 %20, 0
%22 = select i1 %21, i32 %20, i32 0
%23 = zext i32 %22 to i64
%24 = add nuw nsw i64 %16, %23
%25 = icmp ugt i32 %17, 10
%26 = icmp ult i64 %24, 4
%27 = select i1 %25, i1 %26, i1 false
br i1 %27, label %15, label %28
28: ; preds = %15
%29 = load i32, i32* @O_NONBLOCK
%30 = xor i32 %29, -1
%31 = and i32 %7, %30
%32 = load i32, i32* @F_SETFL
%33 = tail call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %32, i32 %31)
%34 = icmp eq i32 %33, 0
%35 = select i1 %26, i32 -2, i32 0
%36 = select i1 %34, i32 %35, i32 -1
br label %37
37: ; preds = %0, %28
%38 = phi i32 [ %36, %28 ], [ -1, %0 ]
ret i32 %38
}