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


x86 assembly (clang, -O0):
<waitForETX>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x2,0x2eef(%rip)
movl   $0x0,0x2ee9(%rip)
movl   $0xffffffff,0x2ee7(%rip)
ff ff ff
cmpl   $0x1e,0x2ed8(%rip)
jge    1271 <waitForETX+0x141>
mov    -0x8(%rbp),%edi
call   1040 <bytesWaiting@plt>
mov    %eax,0x2ec8(%rip)
cmpl   $0x0,0x2ec1(%rip)
jle    1253 <waitForETX+0x123>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq 0x2ea5(%rip),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    0x2e92(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e89(%rip)
cmpl   $0x5,0x2e82(%rip)
jle    11d1 <waitForETX+0xa1>
mov    -0x10(%rbp),%rax
movzbl 0x5(%rax),%eax
add    $0x9,%eax
mov    %eax,0x2e77(%rip)
cmpl   $0x0,0x2e70(%rip)
jle    124e <waitForETX+0x11e>
mov    0x2e58(%rip),%eax
cmp    0x2e5e(%rip),%eax
jl     124e <waitForETX+0x11e>
mov    -0x10(%rbp),%rax
mov    0x2e4e(%rip),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x1b,%eax
jne    1242 <waitForETX+0x112>
mov    -0x10(%rbp),%rax
mov    0x2e31(%rip),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x3,%eax
jne    1242 <waitForETX+0x112>
mov    0x2e18(%rip),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1278 <waitForETX+0x148>
movl   $0x0,-0x4(%rbp)
jmp    1278 <waitForETX+0x148>
jmp    126c <waitForETX+0x13c>
mov    $0x64,%edi
call   1050 <usleep@plt>
mov    0x2ddd(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2dd4(%rip)
jmp    1161 <waitForETX+0x31>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitForETX>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x2,0x2ef6(%rip)
movl   $0x0,0x2ef0(%rip)
movl   $0xffffffff,0x2eea(%rip)
ff ff ff
jmp    116f <waitForETX+0x3f>
nopl   0x0(%rax)
mov    0x2eda(%rip),%eax
cmp    $0x1e,%eax
jge    1215 <waitForETX+0xe5>
mov    %ebp,%edi
call   1040 <bytesWaiting@plt>
test   %eax,%eax
jle    11c0 <waitForETX+0x90>
movslq 0x2ebb(%rip),%rsi
add    %rbx,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <read@plt>
mov    0x2ea6(%rip),%ecx
lea    0x1(%rcx),%eax
mov    %eax,0x2e9d(%rip)
cmp    $0x4,%ecx
jle    11e0 <waitForETX+0xb0>
movzbl 0x5(%rbx),%ecx
add    $0x9,%ecx
mov    %ecx,0x2e93(%rip)
test   %ecx,%ecx
jg     11ee <waitForETX+0xbe>
jmp    1160 <waitForETX+0x30>
nopw   0x0(%rax,%rax,1)
mov    $0x64,%edi
call   1050 <usleep@plt>
mov    0x2e70(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e67(%rip)
cmp    $0x1e,%eax
jl     116f <waitForETX+0x3f>
jmp    1215 <waitForETX+0xe5>
mov    0x2e5e(%rip),%ecx
test   %ecx,%ecx
jle    1160 <waitForETX+0x30>
cmp    %ecx,%eax
jl     1160 <waitForETX+0x30>
movslq %ecx,%rdx
xor    %eax,%eax
cmpb   $0x1b,-0x2(%rdx,%rbx,1)
jne    1217 <waitForETX+0xe7>
lea    -0x1(%rcx),%edx
cmpb   $0x3,(%rbx,%rdx,1)
jne    1217 <waitForETX+0xe7>
mov    %ecx,(%r14)
mov    $0x1,%eax
jmp    1217 <waitForETX+0xe7>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@waitForETX.pos = internal unnamed_addr global i32 0
@waitForETX.loop = internal unnamed_addr global i32 0
@waitForETX.dlen = internal unnamed_addr global i32 0
define i32 @waitForETX(i32 %0, i8* nocapture %1, i32* nocapture writeonly %2) {
store i32 2, i32* @waitForETX.pos
store i32 0, i32* @waitForETX.loop
store i32 -1, i32* @waitForETX.dlen
%4 = getelementptr inbounds i8, i8* %1, i64 5
br label %5
5: ; preds = %3, %46
%6 = tail call i32 @bytesWaiting(i32 %0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %42
8: ; preds = %5
%9 = load i32, i32* @waitForETX.pos
%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* @waitForETX.pos
%14 = add nsw i32 %13, 1
store i32 %14, i32* @waitForETX.pos
%15 = icmp sgt i32 %13, 4
br i1 %15, label %18, label %16
16: ; preds = %8
%17 = load i32, i32* @waitForETX.dlen
br label %22
18: ; preds = %8
%19 = load i8, i8* %4
%20 = zext i8 %19 to i32
%21 = add nuw nsw i32 %20, 9
store i32 %21, i32* @waitForETX.dlen
br label %22
22: ; preds = %16, %18
%23 = phi i32 [ %17, %16 ], [ %21, %18 ]
%24 = icmp slt i32 %23, 1
%25 = icmp slt i32 %14, %23
%26 = select i1 %24, i1 true, i1 %25
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = load i32, i32* @waitForETX.loop
br label %46
29: ; preds = %22
%30 = add nsw i32 %23, -2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %1, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 27
br i1 %34, label %35, label %49
35: ; preds = %29
%36 = add nsw i32 %23, -1
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %1, i64 %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 3
br i1 %40, label %41, label %49
41: ; preds = %35
store i32 %23, i32* %2
br label %49
42: ; preds = %5
%43 = tail call i32 @usleep(i32 100)
%44 = load i32, i32* @waitForETX.loop
%45 = add nsw i32 %44, 1
store i32 %45, i32* @waitForETX.loop
br label %46
46: ; preds = %27, %42
%47 = phi i32 [ %28, %27 ], [ %45, %42 ]
%48 = icmp slt i32 %47, 30
br i1 %48, label %5, label %49
49: ; preds = %46, %29, %35, %41
%50 = phi i32 [ 1, %41 ], [ 0, %35 ], [ 0, %29 ], [ 0, %46 ]
ret i32 %50
}