#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;
};
typedef int /*<<< orphan*/ resp;

/* Variables and functions */
scalar_t__ EAGAIN;
int TIOCM_DTR;
int TIOCM_RTS;
scalar_t__ errno;
int get_serial_lines(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ set_serial_lines(int, int);
int setup_serial_port(int, int, struct termios *);
scalar_t__ strstr(unsigned char *, char *);
int tcgetattr(int, struct termios *);
int time(int /*<<< orphan*/ *);
int /*<<< orphan*/ usleep(int);
scalar_t__ wait_for_input(int, struct timeval *);
int /*<<< orphan*/ write(int, char *, int);
int find_legacy_modem(int fd) {
  int modem_lines;
  int temp;
  int done;
  int respindex;
  int starttime;
  unsigned char resp[10], buf[2];
  struct timeval timo;
  struct termios portattr;
  ;
  ;
  temp = tcgetattr(fd, &portattr);
  if (temp < 0)
    return 1;
  temp = setup_serial_port(fd, 8, &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);
  ;
  ;
  write(fd, "AT\r", 3);
  done = 0;
  respindex = 0;
  starttime = time(NULL);
  memset(resp, 0, sizeof(resp));
  while (!done) {
    timo.tv_sec = 0;
    timo.tv_usec = 250000;
    if (wait_for_input(fd, &timo) > 0) {
      temp = read(fd, buf, 1);
      if (temp < 0) {
        if (errno != EAGAIN)
          return 1;
      } else {
        resp[respindex++] = buf[0];
      }
    } else
      done = 1;
    if (time(NULL) - starttime > 5)
      done = 1;
    if (respindex > 9)
      done = 1;
  }
  if (strstr(resp, "OK"))
    return 3;
  else
    return 2;
}


x86 assembly (clang, -O0):
<find_legacy_modem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   10b0 <tcgetattr@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11e0 <find_legacy_modem+0x30>
movl   $0x1,-0x4(%rbp)
jmp    1383 <find_legacy_modem+0x1d3>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
lea    -0x40(%rbp),%rdx
call   1050 <setup_serial_port@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    120a <find_legacy_modem+0x5a>
movl   $0x1,-0x4(%rbp)
jmp    1383 <find_legacy_modem+0x1d3>
mov    -0x8(%rbp),%edi
call   10a0 <get_serial_lines@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    0x2dbb(%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>
mov    $0x30d40,%edi
call   10d0 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    0xdb4(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x26(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <memset@plt>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1296 <find_legacy_modem+0xe6>
jmp    1356 <find_legacy_modem+0x1a6>
movq   $0x0,-0x30(%rbp)
movl   $0x3d090,-0x38(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1060 <wait_for_input@plt>
cmp    $0x0,%rax
jle    131d <find_legacy_modem+0x16d>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1304 <find_legacy_modem+0x154>
mov    0x2cf0(%rip),%rax
mov    (%rax),%rax
mov    0x2cd6(%rip),%rcx
cmp    (%rcx),%rax
je     12ff <find_legacy_modem+0x14f>
movl   $0x1,-0x4(%rbp)
jmp    1383 <find_legacy_modem+0x1d3>
jmp    1318 <find_legacy_modem+0x168>
mov    -0x28(%rbp),%cl
mov    -0x18(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
cltq
mov    %cl,-0x26(%rbp,%rax,1)
jmp    1324 <find_legacy_modem+0x174>
movl   $0x1,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
sub    -0x1c(%rbp),%eax
cmp    $0x5,%eax
jle    1340 <find_legacy_modem+0x190>
movl   $0x1,-0x14(%rbp)
cmpl   $0x9,-0x18(%rbp)
jle    1351 <find_legacy_modem+0x1a1>
movl   $0x1,-0x14(%rbp)
jmp    1280 <find_legacy_modem+0xd0>
lea    -0x26(%rbp),%rdi
lea    0xca3(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     137c <find_legacy_modem+0x1cc>
movl   $0x3,-0x4(%rbp)
jmp    1383 <find_legacy_modem+0x1d3>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_legacy_modem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   10b0 <tcgetattr@plt>
mov    $0x1,%ebp
test   %eax,%eax
js     131a <find_legacy_modem+0x16a>
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
mov    $0x8,%esi
call   1050 <setup_serial_port@plt>
test   %eax,%eax
js     131a <find_legacy_modem+0x16a>
mov    %ebx,%edi
call   10a0 <get_serial_lines@plt>
mov    0x2dea(%rip),%rcx
mov    0x2ddb(%rip),%rdx
or     (%rcx),%eax
or     (%rdx),%eax
mov    %ebx,%edi
mov    %eax,%esi
call   1030 <set_serial_lines@plt>
mov    $0x30d40,%edi
call   10d0 <usleep@plt>
lea    0xddd(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1040 <write@plt>
xor    %ebp,%ebp
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%r12d
lea    0xe(%rsp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <memset@plt>
lea    0x18(%rsp),%r14
lea    0x6(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
movq   $0x0,0x20(%rsp)
movl   $0x3d090,0x18(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <wait_for_input@plt>
test   %rax,%rax
jle    12b0 <find_legacy_modem+0x100>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12c0 <find_legacy_modem+0x110>
movzbl 0x6(%rsp),%eax
movslq %ebp,%rcx
add    $0x1,%ebp
mov    %al,0xe(%rsp,%rcx,1)
mov    $0x1,%r13b
jmp    12d9 <find_legacy_modem+0x129>
nopw   0x0(%rax,%rax,1)
xor    %r13d,%r13d
jmp    12d9 <find_legacy_modem+0x129>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2d09(%rip),%rax
mov    (%rax),%rax
mov    $0x1,%r13b
mov    0x2cec(%rip),%rcx
cmp    (%rcx),%rax
jne    1315 <find_legacy_modem+0x165>
xor    %edi,%edi
call   1090 <time@plt>
cmp    $0x9,%ebp
jg     12f6 <find_legacy_modem+0x146>
sub    %r12d,%eax
cmp    $0x5,%eax
jg     12f6 <find_legacy_modem+0x146>
test   %r13b,%r13b
jne    1260 <find_legacy_modem+0xb0>
lea    0xd07(%rip),%rsi
lea    0xe(%rsp),%rdi
call   10c0 <strstr@plt>
cmp    $0x1,%rax
mov    $0x3,%ebp
sbb    $0x0,%ebp
jmp    131a <find_legacy_modem+0x16a>
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
@.str = [4 x i8] c"AT\0D\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.1 = [3 x i8] c"OK\00"
define i32 @find_legacy_modem(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [10 x i8]
%10 = alloca [2 x i8]
%11 = alloca %struct.timeval
%12 = alloca %struct.termios
store i32 %0, i32* %3
%13 = load i32, i32* %3
%14 = call i32 @tcgetattr(i32 %13, %struct.termios* %12)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 1, i32* %2
br label %89
18: ; preds = %1
%19 = load i32, i32* %3
%20 = call i32 @setup_serial_port(i32 %19, i32 8, %struct.termios* %12)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 1, i32* %2
br label %89
24: ; preds = %18
%25 = load i32, i32* %3
%26 = call i32 @get_serial_lines(i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* @TIOCM_RTS
%28 = load i32, i32* @TIOCM_DTR
%29 = or i32 %27, %28
%30 = load i32, i32* %4
%31 = or i32 %30, %29
store i32 %31, i32* %4
%32 = load i32, i32* %3
%33 = load i32, i32* %4
%34 = call i32 @set_serial_lines(i32 %32, i32 %33)
%35 = call i32 @usleep(i32 200000)
%36 = load i32, i32* %3
%37 = call i32 @write(i32 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
store i32 0, i32* %6
store i32 0, i32* %7
%38 = call i32 @time(i32* null)
store i32 %38, i32* %8
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%40 = call i32 @memset(i8* %39, i32 0, i32 10)
br label %41
41: ; preds = %82, %24
%42 = load i32, i32* %6
%43 = icmp ne i32 %42, 0
%44 = xor i1 %43, true
br i1 %44, label %45, label %83
45: ; preds = %41
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %46
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 250000, i32* %47
%48 = load i32, i32* %3
%49 = call i64 @wait_for_input(i32 %48, %struct.timeval* %11)
%50 = icmp sgt i64 %49, 0
br i1 %50, label %51, label %71
51: ; preds = %45
%52 = load i32, i32* %3
%53 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 1)
store i32 %54, i32* %5
%55 = load i32, i32* %5
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %51
%58 = load i64, i64* @errno
%59 = load i64, i64* @EAGAIN
%60 = icmp ne i64 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %57
store i32 1, i32* %2
br label %89
62: ; preds = %57
br label %70
63: ; preds = %51
%64 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%65 = load i8, i8* %64
%66 = load i32, i32* %7
%67 = add nsw i32 %66, 1
store i32 %67, i32* %7
%68 = sext i32 %66 to i64
%69 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 %68
store i8 %65, i8* %69
br label %70
70: ; preds = %63, %62
br label %72
71: ; preds = %45
store i32 1, i32* %6
br label %72
72: ; preds = %71, %70
%73 = call i32 @time(i32* null)
%74 = load i32, i32* %8
%75 = sub nsw i32 %73, %74
%76 = icmp sgt i32 %75, 5
br i1 %76, label %77, label %78
77: ; preds = %72
store i32 1, i32* %6
br label %78
78: ; preds = %77, %72
%79 = load i32, i32* %7
%80 = icmp sgt i32 %79, 9
br i1 %80, label %81, label %82
81: ; preds = %78
store i32 1, i32* %6
br label %82
82: ; preds = %81, %78
br label %41
83: ; preds = %41
%84 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%85 = call i64 @strstr(i8* %84, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %88
87: ; preds = %83
store i32 3, i32* %2
br label %89
88: ; preds = %83
store i32 2, i32* %2
br label %89
89: ; preds = %88, %87, %61, %23, %17
%90 = load i32, i32* %2
ret i32 %90
}

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