#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 */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ readfs;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int readStatus(int fd) {
  char buf[255];
  int i, res, cpos = 0;
  fd_set readfs;
  struct timeval Timeout;
  bzero(&readfs, sizeof(readfs));
  while (1) {
    FD_SET(fd, &readfs);
    Timeout.tv_usec = 0;
    Timeout.tv_sec = 2;
    res = select(fd + 1, &readfs, NULL, NULL, &Timeout);
    if (FD_ISSET(fd, &readfs)) {
      res = read(fd, &buf[cpos], 255);
      if (res > 0) {
        cpos += res;
        for (int i = 4; i < res + cpos; i++) {
          if (buf[i] == 0x20) {
            int pos = buf[i - 3] * 100 + buf[i - 2] * 10 + buf[i - 1];
            tcflush(fd, TCIFLUSH);
            return pos;
          }
        }
      }
    } else {
      tcflush(fd, TCIFLUSH);
      return -1;
    }
  }
  tcflush(fd, TCIFLUSH);
  return -1;
}


x86 assembly (clang, -O0):
<readStatus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x11c(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x4,%esi
call   1070 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
call   1080 <FD_SET@plt>
movq   $0x0,-0x128(%rbp)
movl   $0x2,-0x130(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x120(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x130(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x118(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12f8 <readStatus+0x198>
mov    -0x8(%rbp),%edi
movslq -0x11c(%rbp),%rax
lea    -0x110(%rbp),%rsi
add    %rax,%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jle    12f3 <readStatus+0x193>
mov    -0x118(%rbp),%eax
add    -0x11c(%rbp),%eax
mov    %eax,-0x11c(%rbp)
movl   $0x4,-0x134(%rbp)
mov    -0x134(%rbp),%eax
mov    -0x118(%rbp),%ecx
add    -0x11c(%rbp),%ecx
cmp    %ecx,%eax
jge    12ee <readStatus+0x18e>
movslq -0x134(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x20,%eax
jne    12d5 <readStatus+0x175>
mov    -0x134(%rbp),%eax
sub    $0x3,%eax
cltq
movsbl -0x110(%rbp,%rax,1),%eax
ff
imul   $0x64,%eax,%eax
mov    -0x134(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl -0x110(%rbp,%rcx,1),%ecx
ff
imul   $0xa,%ecx,%ecx
add    %ecx,%eax
mov    -0x134(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl -0x110(%rbp,%rcx,1),%ecx
ff
add    %ecx,%eax
mov    %eax,-0x138(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x138(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    131a <readStatus+0x1ba>
jmp    12da <readStatus+0x17a>
mov    -0x134(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x134(%rbp)
jmp    1239 <readStatus+0xd9>
jmp    12f3 <readStatus+0x193>
jmp    1315 <readStatus+0x1b5>
mov    -0x8(%rbp),%edi
mov    0x2cde(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <readStatus+0x1ba>
jmp    1189 <readStatus+0x29>
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readStatus>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
mov    $0x4,%esi
call   1070 <bzero@plt>
lea    0x1(%rbx),%r15d
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
movq   $0x0,0x18(%rsp)
movl   $0x2,0x10(%rsp)
xor    %ebp,%ebp
lea    0x10(%rsp),%r8
mov    %r15d,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     12c7 <readStatus+0x167>
movabs $0xffffffff00000000,%r12
ff ff ff
lea    0xc(%rsp),%r13
jmp    1232 <readStatus+0xd2>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r13,%rsi
call   1080 <FD_SET@plt>
movq   $0x0,0x18(%rsp)
movl   $0x2,0x10(%rsp)
mov    %r15d,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   1060 <select@plt>
mov    %ebx,%edi
mov    %r13,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12c7 <readStatus+0x167>
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x20,%rsi
mov    %ebx,%edi
mov    $0xff,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11f0 <readStatus+0x90>
add    %eax,%ebp
add    %ebp,%eax
cmp    $0x5,%eax
jl     11f0 <readStatus+0x90>
mov    %eax,%ecx
mov    $0x4,%edx
xor    %eax,%eax
nop
cmpb   $0x20,0x20(%rsp,%rdx,1)
je     1278 <readStatus+0x118>
add    $0x1,%rdx
add    %r12,%rax
cmp    %rdx,%rcx
jne    1260 <readStatus+0x100>
jmp    11f0 <readStatus+0x90>
movabs $0x100000000,%rcx
sub    %rax,%rcx
sar    $0x20,%rcx
movsbl 0x20(%rsp,%rcx,1),%ecx
imul   $0x64,%ecx,%ecx
movabs $0x200000000,%rdx
sub    %rax,%rdx
sar    $0x20,%rdx
movsbl 0x20(%rsp,%rdx,1),%edx
lea    (%rdx,%rdx,4),%edx
lea    (%rcx,%rdx,2),%ecx
movabs $0x300000000,%rdx
sub    %rax,%rdx
sar    $0x20,%rdx
movsbl 0x20(%rsp,%rdx,1),%r14d
add    %ecx,%r14d
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    %r14d,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@TCIFLUSH = global i32 0
define i32 @readStatus(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %7
%12 = call i32 @bzero(i32* %8, i32 4)
br label %13
13: ; preds = %1, %86
%14 = load i32, i32* %3
%15 = call i32 @FD_SET(i32 %14, i32* %8)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 2, i32* %17
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
%20 = call i32 @select(i32 %19, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %20, i32* %6
%21 = load i32, i32* %3
%22 = call i64 @FD_ISSET(i32 %21, i32* %8)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %82
24: ; preds = %13
%25 = load i32, i32* %3
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %27
%29 = call i32 @read(i32 %25, i8* %28, i32 255)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %81
32: ; preds = %24
%33 = load i32, i32* %6
%34 = load i32, i32* %7
%35 = add nsw i32 %34, %33
store i32 %35, i32* %7
store i32 4, i32* %10
br label %36
36: ; preds = %77, %32
%37 = load i32, i32* %10
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = add nsw i32 %38, %39
%41 = icmp slt i32 %37, %40
br i1 %41, label %42, label %80
42: ; preds = %36
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 32
br i1 %48, label %49, label %76
49: ; preds = %42
%50 = load i32, i32* %10
%51 = sub nsw i32 %50, 3
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = mul nsw i32 %55, 100
%57 = load i32, i32* %10
%58 = sub nsw i32 %57, 2
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = mul nsw i32 %62, 10
%64 = add nsw i32 %56, %63
%65 = load i32, i32* %10
%66 = sub nsw i32 %65, 1
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %67
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = add nsw i32 %64, %70
store i32 %71, i32* %11
%72 = load i32, i32* %3
%73 = load i32, i32* @TCIFLUSH
%74 = call i32 @tcflush(i32 %72, i32 %73)
%75 = load i32, i32* %11
store i32 %75, i32* %2
br label %87
76: ; preds = %42
br label %77
77: ; preds = %76
%78 = load i32, i32* %10
%79 = add nsw i32 %78, 1
store i32 %79, i32* %10
br label %36
80: ; preds = %36
br label %81
81: ; preds = %80, %24
br label %86
82: ; preds = %13
%83 = load i32, i32* %3
%84 = load i32, i32* @TCIFLUSH
%85 = call i32 @tcflush(i32 %83, i32 %84)
store i32 -1, i32* %2
br label %87
86: ; preds = %81
br label %13
87: ; preds = %82, %49
%88 = load i32, i32* %2
ret i32 %88
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@TCIFLUSH = global i32 0
define i32 @readStatus(i32 %0) {
%2 = alloca [255 x i8]
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = call i32 @bzero(i32* %3, i32 4)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%11 = add nsw i32 %0, 1
%12 = call i32 @FD_SET(i32 %0, i32* %3)
store i64 0, i64* %9
store i32 2, i32* %10
%13 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%14 = call i64 @FD_ISSET(i32 %0, i32* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %65, label %16
16: ; preds = %1, %59
%17 = phi i32 [ %60, %59 ], [ 0, %1 ]
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %18
%20 = call i32 @read(i32 %0, i8* %19, i32 255)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %59
22: ; preds = %16
%23 = add nsw i32 %20, %17
%24 = add i32 %23, %20
%25 = icmp sgt i32 %24, 4
br i1 %25, label %26, label %59
26: ; preds = %22
%27 = zext i32 %24 to i64
br label %28
28: ; preds = %26, %56
%29 = phi i64 [ 4, %26 ], [ %57, %56 ]
%30 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 32
br i1 %32, label %33, label %56
33: ; preds = %28
%34 = shl i64 %29, 32
%35 = add i64 %34, -12884901888
%36 = ashr exact i64 %35, 32
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = mul nsw i32 %39, 100
%41 = shl i64 %29, 32
%42 = add i64 %41, -8589934592
%43 = ashr exact i64 %42, 32
%44 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = mul nsw i32 %46, 10
%48 = add nsw i32 %47, %40
%49 = shl i64 %29, 32
%50 = add i64 %49, -4294967296
%51 = ashr exact i64 %50, 32
%52 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = add nsw i32 %48, %54
br label %65
56: ; preds = %28
%57 = add nuw nsw i64 %29, 1
%58 = icmp eq i64 %57, %27
br i1 %58, label %59, label %28
59: ; preds = %56, %22, %16
%60 = phi i32 [ %17, %16 ], [ %23, %22 ], [ %23, %56 ]
%61 = call i32 @FD_SET(i32 %0, i32* %3)
store i64 0, i64* %9
store i32 2, i32* %10
%62 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%63 = call i64 @FD_ISSET(i32 %0, i32* %3)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %16
65: ; preds = %59, %1, %33
%66 = phi i32 [ %55, %33 ], [ -1, %1 ], [ -1, %59 ]
%67 = load i32, i32* @TCIFLUSH
%68 = call i32 @tcflush(i32 %0, i32 %67)
ret i32 %66
}