#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 */
scalar_t__ bytesWaiting(int);
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ usleep(int);
int waitForETX(int, unsigned char *, int *);
int waitForAnswer(int fd, unsigned char *buf, int *len) {
  int loop = 0;
  *len = 0;
  while (loop < 30) {
    if (bytesWaiting(fd)) {
      read(fd, &(buf[*len]), 1);
      *len = *len + 1;
      if (*len >= 2 && buf[*len - 2] == 0x1b && buf[*len - 1] == 0x02) {
        buf[0] = 0x1b;
        buf[1] = 0x02;
        *len = 2;
        return (waitForETX(fd, buf, len));
      }
    } else {
      usleep(100);
      loop++;
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<waitForAnswer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x1e,-0x1c(%rbp)
jge    123f <waitForAnswer+0xff>
mov    -0x8(%rbp),%edi
call   1050 <bytesWaiting@plt>
cmp    $0x0,%rax
je     1227 <waitForAnswer+0xe7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x2,(%rax)
jl     1222 <waitForAnswer+0xe2>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x1b,%eax
jne    1222 <waitForAnswer+0xe2>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x2,%eax
jne    1222 <waitForAnswer+0xe2>
mov    -0x10(%rbp),%rax
movb   $0x1b,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x2,0x1(%rax)
mov    -0x18(%rbp),%rax
movl   $0x2,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <waitForETX@plt>
mov    %eax,-0x4(%rbp)
jmp    1246 <waitForAnswer+0x106>
jmp    123a <waitForAnswer+0xfa>
mov    $0x64,%edi
call   1060 <usleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1164 <waitForAnswer+0x24>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitForAnswer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
movl   $0x0,(%rdx)
xor    %ebp,%ebp
jmp    1172 <waitForAnswer+0x32>
nopw   0x0(%rax,%rax,1)
mov    $0x64,%edi
call   1060 <usleep@plt>
add    $0x1,%ebp
cmp    $0x1e,%ebp
jge    11d2 <waitForAnswer+0x92>
mov    %r15d,%edi
call   1050 <bytesWaiting@plt>
test   %rax,%rax
je     1160 <waitForAnswer+0x20>
movslq (%rbx),%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    (%rbx),%eax
lea    0x1(%rax),%ecx
mov    %ecx,(%rbx)
test   %eax,%eax
jle    116d <waitForAnswer+0x2d>
lea    -0x1(%rax),%ecx
cmpb   $0x1b,(%r14,%rcx,1)
jne    116d <waitForAnswer+0x2d>
cmpb   $0x2,(%r14,%rax,1)
jne    116d <waitForAnswer+0x2d>
movw   $0x21b,(%r14)
movl   $0x2,(%rbx)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <waitForETX@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @waitForAnswer(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 0, i32* %8
%9 = load i32*, i32** %7
store i32 0, i32* %9
br label %10
10: ; preds = %67, %3
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 30
br i1 %12, label %13, label %68
13: ; preds = %10
%14 = load i32, i32* %5
%15 = call i64 @bytesWaiting(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %63
17: ; preds = %13
%18 = load i32, i32* %5
%19 = load i8*, i8** %6
%20 = load i32*, i32** %7
%21 = load i32, i32* %20
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = call i32 @read(i32 %18, i8* %23, i32 1)
%25 = load i32*, i32** %7
%26 = load i32, i32* %25
%27 = add nsw i32 %26, 1
%28 = load i32*, i32** %7
store i32 %27, i32* %28
%29 = load i32*, i32** %7
%30 = load i32, i32* %29
%31 = icmp sge i32 %30, 2
br i1 %31, label %32, label %62
32: ; preds = %17
%33 = load i8*, i8** %6
%34 = load i32*, i32** %7
%35 = load i32, i32* %34
%36 = sub nsw i32 %35, 2
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %33, i64 %37
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = icmp eq i32 %40, 27
br i1 %41, label %42, label %62
42: ; preds = %32
%43 = load i8*, i8** %6
%44 = load i32*, i32** %7
%45 = load i32, i32* %44
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %43, i64 %47
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = icmp eq i32 %50, 2
br i1 %51, label %52, label %62
52: ; preds = %42
%53 = load i8*, i8** %6
%54 = getelementptr inbounds i8, i8* %53, i64 0
store i8 27, i8* %54
%55 = load i8*, i8** %6
%56 = getelementptr inbounds i8, i8* %55, i64 1
store i8 2, i8* %56
%57 = load i32*, i32** %7
store i32 2, i32* %57
%58 = load i32, i32* %5
%59 = load i8*, i8** %6
%60 = load i32*, i32** %7
%61 = call i32 @waitForETX(i32 %58, i8* %59, i32* %60)
store i32 %61, i32* %4
br label %69
62: ; preds = %42, %32, %17
br label %67
63: ; preds = %13
%64 = call i32 @usleep(i32 100)
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %67
67: ; preds = %63, %62
br label %10
68: ; preds = %10
store i32 0, i32* %4
br label %69
69: ; preds = %68, %52
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
define i32 @waitForAnswer(i32 %0, i8* %1, i32* %2) {
store i32 0, i32* %2
br label %4
4: ; preds = %3, %33
%5 = phi i32 [ 0, %3 ], [ %34, %33 ]
%6 = tail call i64 @bytesWaiting(i32 %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %30, label %8
8: ; preds = %4
%9 = load i32, i32* %2
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = tail call i32 @read(i32 %0, i8* %11, i32 1)
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
store i32 %14, i32* %2
%15 = icmp sgt i32 %13, 0
br i1 %15, label %16, label %33
16: ; preds = %8
%17 = add nsw i32 %13, -1
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %1, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 27
br i1 %21, label %22, label %33
22: ; preds = %16
%23 = zext i32 %13 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 2
br i1 %26, label %27, label %33
27: ; preds = %22
store i8 27, i8* %1
%28 = getelementptr inbounds i8, i8* %1, i64 1
store i8 2, i8* %28
store i32 2, i32* %2
%29 = tail call i32 @waitForETX(i32 %0, i8* %1, i32* %2)
br label %36
30: ; preds = %4
%31 = tail call i32 @usleep(i32 100)
%32 = add nsw i32 %5, 1
br label %33
33: ; preds = %8, %16, %22, %30
%34 = phi i32 [ %5, %22 ], [ %5, %16 ], [ %5, %8 ], [ %32, %30 ]
%35 = icmp slt i32 %34, 30
br i1 %35, label %4, label %36
36: ; preds = %33, %27
%37 = phi i32 [ %29, %27 ], [ 0, %33 ]
ret i32 %37
}