#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_usec;
  scalar_t__ tv_sec;
};
struct termios {
  int dummy;
};

/* Variables and functions */
scalar_t__ EAGAIN;
int TIOCM_DTR;
int TIOCM_RTS;
scalar_t__ errno;
int get_serial_lines(int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ set_serial_lines(int, int);
int setup_serial_port(int, int, struct termios *);
int tcgetattr(int, struct termios *);
int time(int /*<<< orphan*/ *);
int /*<<< orphan*/ usleep(int);
scalar_t__ wait_for_input(int, struct timeval *);
int find_legacy_mouse(int fd) {
  int modem_lines;
  int temp;
  int done;
  int starttime;
  unsigned char resp[2];
  struct timeval timo;
  struct termios portattr;
  ;
  ;
  temp = tcgetattr(fd, &portattr);
  if (temp < 0)
    return 1;
  temp = setup_serial_port(fd, 7, &portattr);
  if (temp < 0)
    return 1;
  modem_lines = get_serial_lines(fd);
  modem_lines &= ~(TIOCM_RTS | TIOCM_DTR);
  set_serial_lines(fd, modem_lines);
  usleep(200000);
  modem_lines |= TIOCM_DTR | TIOCM_RTS;
  set_serial_lines(fd, modem_lines);
  done = 0;
  starttime = time(NULL);
  while (!done) {
    timo.tv_sec = 0;
    timo.tv_usec = 250000;
    if (wait_for_input(fd, &timo) > 0) {
      temp = read(fd, resp, 1);
      if (temp < 0) {
        if (errno != EAGAIN)
          return 1;
      } else {
        done = 1;
      }
    } else
      done = 1;
    if (time(NULL) - starttime > 2)
      done = 1;
  }
  if (*resp == 'M') {
    ;
    ;
    return 3;
  } else
    return 2;
}


x86 assembly (clang, -O0):
<find_legacy_mouse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1090 <tcgetattr@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11b0 <find_legacy_mouse+0x30>
movl   $0x1,-0x4(%rbp)
jmp    1323 <find_legacy_mouse+0x1a3>
mov    -0x8(%rbp),%edi
mov    $0x7,%esi
lea    -0x38(%rbp),%rdx
call   1040 <setup_serial_port@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11da <find_legacy_mouse+0x5a>
movl   $0x1,-0x4(%rbp)
jmp    1323 <find_legacy_mouse+0x1a3>
mov    -0x8(%rbp),%edi
call   1080 <get_serial_lines@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    0x2deb(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <set_serial_lines@plt>
mov    $0x30d40,%edi
call   10a0 <usleep@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    0x2dc3(%rip),%rcx
or     (%rcx),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <set_serial_lines@plt>
movl   $0x0,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1261 <find_legacy_mouse+0xe1>
jmp    1303 <find_legacy_mouse+0x183>
movq   $0x0,-0x28(%rbp)
movl   $0x3d090,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1050 <wait_for_input@plt>
cmp    $0x0,%rax
jle    12db <find_legacy_mouse+0x15b>
mov    -0x8(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12cf <find_legacy_mouse+0x14f>
mov    0x2d25(%rip),%rax
mov    (%rax),%rax
mov    0x2d0b(%rip),%rcx
cmp    (%rcx),%rax
je     12ca <find_legacy_mouse+0x14a>
movl   $0x1,-0x4(%rbp)
jmp    1323 <find_legacy_mouse+0x1a3>
jmp    12d6 <find_legacy_mouse+0x156>
movl   $0x1,-0x14(%rbp)
jmp    12e2 <find_legacy_mouse+0x162>
movl   $0x1,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
sub    -0x18(%rbp),%eax
cmp    $0x2,%eax
jle    12fe <find_legacy_mouse+0x17e>
movl   $0x1,-0x14(%rbp)
jmp    124b <find_legacy_mouse+0xcb>
movzbl -0x1a(%rbp),%eax
cmp    $0x4d,%eax
jne    131c <find_legacy_mouse+0x19c>
movl   $0x3,-0x4(%rbp)
jmp    1323 <find_legacy_mouse+0x1a3>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_legacy_mouse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%rsi
call   1090 <tcgetattr@plt>
mov    $0x1,%ebp
test   %eax,%eax
js     129b <find_legacy_mouse+0x11b>
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    $0x7,%esi
call   1040 <setup_serial_port@plt>
test   %eax,%eax
js     129b <find_legacy_mouse+0x11b>
mov    %ebx,%edi
call   1080 <get_serial_lines@plt>
mov    0x2e1a(%rip),%r14
mov    0x2e0b(%rip),%r15
mov    (%r15),%ebp
or     (%r14),%ebp
not    %ebp
and    %eax,%ebp
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <set_serial_lines@plt>
mov    $0x30d40,%edi
call   10a0 <usleep@plt>
or     (%r15),%ebp
or     (%r14),%ebp
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <set_serial_lines@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%ebp
lea    0x18(%rsp),%r14
lea    0xe(%rsp),%r15
mov    0x2da5(%rip),%r13
nopl   0x0(%rax,%rax,1)
movq   $0x0,0x20(%rsp)
movl   $0x3d090,0x18(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <wait_for_input@plt>
test   %rax,%rax
jle    1253 <find_legacy_mouse+0xd3>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1260 <find_legacy_mouse+0xe0>
xor    %r12d,%r12d
jmp    1273 <find_legacy_mouse+0xf3>
nopl   0x0(%rax,%rax,1)
mov    0x2d69(%rip),%rax
mov    (%rax),%rax
mov    $0x1,%r12b
cmp    0x0(%r13),%rax
jne    1296 <find_legacy_mouse+0x116>
xor    %edi,%edi
call   1070 <time@plt>
sub    %ebp,%eax
cmp    $0x2,%eax
jg     1286 <find_legacy_mouse+0x106>
test   %r12b,%r12b
jne    1220 <find_legacy_mouse+0xa0>
xor    %ebp,%ebp
cmpb   $0x4d,0xe(%rsp)
sete   %bpl
or     $0x2,%ebp
jmp    129b <find_legacy_mouse+0x11b>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
%struct.termios = type { i32 }
@TIOCM_RTS = global i32 0
@TIOCM_DTR = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @find_legacy_mouse(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i8]
%9 = alloca %struct.timeval
%10 = alloca %struct.termios
store i32 %0, i32* %3
%11 = load i32, i32* %3
%12 = call i32 @tcgetattr(i32 %11, %struct.termios* %10)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 1, i32* %2
br label %83
16: ; preds = %1
%17 = load i32, i32* %3
%18 = call i32 @setup_serial_port(i32 %17, i32 7, %struct.termios* %10)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 1, i32* %2
br label %83
22: ; preds = %16
%23 = load i32, i32* %3
%24 = call i32 @get_serial_lines(i32 %23)
store i32 %24, i32* %4
%25 = load i32, i32* @TIOCM_RTS
%26 = load i32, i32* @TIOCM_DTR
%27 = or i32 %25, %26
%28 = xor i32 %27, -1
%29 = load i32, i32* %4
%30 = and i32 %29, %28
store i32 %30, i32* %4
%31 = load i32, i32* %3
%32 = load i32, i32* %4
%33 = call i32 @set_serial_lines(i32 %31, i32 %32)
%34 = call i32 @usleep(i32 200000)
%35 = load i32, i32* @TIOCM_DTR
%36 = load i32, i32* @TIOCM_RTS
%37 = or i32 %35, %36
%38 = load i32, i32* %4
%39 = or i32 %38, %37
store i32 %39, i32* %4
%40 = load i32, i32* %3
%41 = load i32, i32* %4
%42 = call i32 @set_serial_lines(i32 %40, i32 %41)
store i32 0, i32* %6
%43 = call i32 @time(i32* null)
store i32 %43, i32* %7
br label %44
44: ; preds = %75, %22
%45 = load i32, i32* %6
%46 = icmp ne i32 %45, 0
%47 = xor i1 %46, true
br i1 %47, label %48, label %76
48: ; preds = %44
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %49
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 250000, i32* %50
%51 = load i32, i32* %3
%52 = call i64 @wait_for_input(i32 %51, %struct.timeval* %9)
%53 = icmp sgt i64 %52, 0
br i1 %53, label %54, label %68
54: ; preds = %48
%55 = load i32, i32* %3
%56 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 1)
store i32 %57, i32* %5
%58 = load i32, i32* %5
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %54
%61 = load i64, i64* @errno
%62 = load i64, i64* @EAGAIN
%63 = icmp ne i64 %61, %62
br i1 %63, label %64, label %65
64: ; preds = %60
store i32 1, i32* %2
br label %83
65: ; preds = %60
br label %67
66: ; preds = %54
store i32 1, i32* %6
br label %67
67: ; preds = %66, %65
br label %69
68: ; preds = %48
store i32 1, i32* %6
br label %69
69: ; preds = %68, %67
%70 = call i32 @time(i32* null)
%71 = load i32, i32* %7
%72 = sub nsw i32 %70, %71
%73 = icmp sgt i32 %72, 2
br i1 %73, label %74, label %75
74: ; preds = %69
store i32 1, i32* %6
br label %75
75: ; preds = %74, %69
br label %44
76: ; preds = %44
%77 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%78 = load i8, i8* %77
%79 = zext i8 %78 to i32
%80 = icmp eq i32 %79, 77
br i1 %80, label %81, label %82
81: ; preds = %76
store i32 3, i32* %2
br label %83
82: ; preds = %76
store i32 2, i32* %2
br label %83
83: ; preds = %82, %81, %64, %21, %15
%84 = load i32, i32* %2
ret i32 %84
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.termios = type { i32 }
@TIOCM_RTS = global i32 0
@TIOCM_DTR = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @find_legacy_mouse(i32 %0) {
%2 = alloca [2 x i8]
%3 = alloca %struct.timeval
%4 = alloca %struct.termios
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.timeval* %3 to i8*
%7 = bitcast %struct.termios* %4 to i8*
%8 = call i32 @tcgetattr(i32 %0, %struct.termios* %4)
%9 = icmp slt i32 %8, 0
br i1 %9, label %50, label %10
10: ; preds = %1
%11 = call i32 @setup_serial_port(i32 %0, i32 7, %struct.termios* %4)
%12 = icmp slt i32 %11, 0
br i1 %12, label %50, label %13
13: ; preds = %10
%14 = call i32 @get_serial_lines(i32 %0)
%15 = load i32, i32* @TIOCM_RTS
%16 = load i32, i32* @TIOCM_DTR
%17 = or i32 %16, %15
%18 = xor i32 %17, -1
%19 = and i32 %14, %18
%20 = call i32 @set_serial_lines(i32 %0, i32 %19)
%21 = call i32 @usleep(i32 200000)
%22 = load i32, i32* @TIOCM_DTR
%23 = load i32, i32* @TIOCM_RTS
%24 = or i32 %22, %19
%25 = or i32 %24, %23
%26 = call i32 @set_serial_lines(i32 %0, i32 %25)
%27 = call i32 @time(i32* null)
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
br label %30
30: ; preds = %13, %40
store i64 0, i64* %28
store i32 250000, i32* %29
%31 = call i64 @wait_for_input(i32 %0, %struct.timeval* %3)
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = call i32 @read(i32 %0, i8* %5, i32 1)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %50
40: ; preds = %30, %33, %36
%41 = phi i1 [ false, %30 ], [ false, %33 ], [ true, %36 ]
%42 = call i32 @time(i32* null)
%43 = sub nsw i32 %42, %27
%44 = icmp slt i32 %43, 3
%45 = and i1 %44, %41
br i1 %45, label %30, label %46
46: ; preds = %40
%47 = load i8, i8* %5
%48 = icmp eq i8 %47, 77
%49 = select i1 %48, i32 3, i32 2
br label %50
50: ; preds = %36, %46, %10, %1
%51 = phi i32 [ 1, %1 ], [ 1, %10 ], [ %49, %46 ], [ 1, %36 ]
ret i32 %51
}