#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 /*<<< orphan*/ STDIN_FILENO;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
int linenoiseProbe() {
  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");
  int timeout_ms = 200;
  size_t read_bytes = 0;
  while (timeout_ms > 0 && read_bytes < 4) {
    usleep(10000);
    char c;
    int cb = fread(&c, 1, 1, stdin);
    read_bytes += cb;
    timeout_ms--;
  }
  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    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11a8 <linenoiseProbe+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <linenoiseProbe+0x15b>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xc8,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x21(%rbp)
jle    11e6 <linenoiseProbe+0xa6>
cmpq   $0x4,-0x18(%rbp)
setb   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11f6 <linenoiseProbe+0xb6>
jmp    1236 <linenoiseProbe+0xf6>
mov    $0x2710,%edi
call   1060 <usleep@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%ecx
lea    -0x19(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11cc <linenoiseProbe+0x8c>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     127d <linenoiseProbe+0x13d>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <linenoiseProbe+0x15b>
cmpq   $0x4,-0x18(%rbp)
jae    1294 <linenoiseProbe+0x154>
movl   $0xfffffffe,-0x4(%rbp)
jmp    129b <linenoiseProbe+0x15b>
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    0x2e66(%rip),%r14
mov    (%r14),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    %eax,%r13d
mov    0x2e79(%rip),%rax
or     (%rax),%r13d
mov    (%r14),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    122e <linenoiseProbe+0xee>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xc8,%ebp
xor    %eax,%eax
mov    0x2e26(%rip),%r15
lea    0x7(%rsp),%rbx
nop
mov    %rax,%r12
mov    $0x2710,%edi
call   1060 <usleep@plt>
mov    (%r15),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cltq
add    %r12,%rax
cmp    $0x2,%ebp
jb     11f5 <linenoiseProbe+0xb5>
add    $0xffffffff,%ebp
cmp    $0x4,%rax
jb     11c0 <linenoiseProbe+0x80>
xor    %ebx,%ebx
cmp    $0x4,%rax
setae  %bl
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,%r13d
mov    (%r14),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
lea    (%rbx,%rbx,1),%ecx
add    $0xfffffffe,%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_FILENO = 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"
@stdin = global i32 0
define i32 @linenoiseProbe() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8
%7 = alloca i32
%8 = load i32, i32* @STDIN_FILENO
%9 = load i32, i32* @F_GETFL
%10 = call i32 (i32, i32, ...) @fcntl(i32 %8, i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* @O_NONBLOCK
%12 = load i32, i32* %2
%13 = or i32 %12, %11
store i32 %13, i32* %2
%14 = load i32, i32* @STDIN_FILENO
%15 = load i32, i32* @F_SETFL
%16 = load i32, i32* %2
%17 = call i32 (i32, i32, ...) @fcntl(i32 %14, i32 %15, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %0
store i32 -1, i32* %1
br label %59
21: ; preds = %0
%22 = load i32, i32* @stdout
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 200, i32* %4
store i64 0, i64* %5
br label %24
24: ; preds = %32, %21
%25 = load i32, i32* %4
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i64, i64* %5
%29 = icmp ult i64 %28, 4
br label %30
30: ; preds = %27, %24
%31 = phi i1 [ false, %24 ], [ %29, %27 ]
br i1 %31, label %32, label %42
32: ; preds = %30
%33 = call i32 @usleep(i32 10000)
%34 = load i32, i32* @stdin
%35 = call i32 @fread(i8* %6, i32 1, i32 1, i32 %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = load i64, i64* %5
%39 = add i64 %38, %37
store i64 %39, i64* %5
%40 = load i32, i32* %4
%41 = add nsw i32 %40, -1
store i32 %41, i32* %4
br label %24
42: ; preds = %30
%43 = load i32, i32* @O_NONBLOCK
%44 = xor i32 %43, -1
%45 = load i32, i32* %2
%46 = and i32 %45, %44
store i32 %46, i32* %2
%47 = load i32, i32* @STDIN_FILENO
%48 = load i32, i32* @F_SETFL
%49 = load i32, i32* %2
%50 = call i32 (i32, i32, ...) @fcntl(i32 %47, i32 %48, i32 %49)
store i32 %50, i32* %3
%51 = load i32, i32* %3
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %42
store i32 -1, i32* %1
br label %59
54: ; preds = %42
%55 = load i64, i64* %5
%56 = icmp ult i64 %55, 4
br i1 %56, label %57, label %58
57: ; preds = %54
store i32 -2, i32* %1
br label %59
58: ; preds = %54
store i32 0, i32* %1
br label %59
59: ; preds = %58, %57, %53, %20
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@STDIN_FILENO = 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"
@stdin = global i32 0
define i32 @linenoiseProbe() {
%1 = alloca i8
%2 = load i32, i32* @STDIN_FILENO
%3 = load i32, i32* @F_GETFL
%4 = tail call i32 (i32, i32, ...) @fcntl(i32 %2, i32 %3)
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%7 = load i32, i32* @STDIN_FILENO
%8 = load i32, i32* @F_SETFL
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %7, i32 %8, i32 %6)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %36
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))
br label %14
14: ; preds = %11, %14
%15 = phi i64 [ 0, %11 ], [ %21, %14 ]
%16 = phi i32 [ 200, %11 ], [ %22, %14 ]
%17 = call i32 @usleep(i32 10000)
%18 = load i32, i32* @stdin
%19 = call i32 @fread(i8* %1, i32 1, i32 1, i32 %18)
%20 = sext i32 %19 to i64
%21 = add nsw i64 %15, %20
%22 = add nsw i32 %16, -1
%23 = icmp ugt i32 %16, 1
%24 = icmp ult i64 %21, 4
%25 = select i1 %23, i1 %24, i1 false
br i1 %25, label %14, label %26
26: ; preds = %14
%27 = load i32, i32* @O_NONBLOCK
%28 = xor i32 %27, -1
%29 = and i32 %6, %28
%30 = load i32, i32* @STDIN_FILENO
%31 = load i32, i32* @F_SETFL
%32 = call i32 (i32, i32, ...) @fcntl(i32 %30, i32 %31, i32 %29)
%33 = icmp eq i32 %32, 0
%34 = select i1 %24, i32 -2, i32 0
%35 = select i1 %33, i32 %34, i32 -1
br label %36
36: ; preds = %0, %26
%37 = phi i32 [ %35, %26 ], [ -1, %0 ]
ret i32 %37
}