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

/* Variables and functions */
int eb_serial_recv(unsigned char *, int *);
int /*<<< orphan*/ memset(unsigned char *, char, int);
int eb_bus_wait_syn(int *skip) {
  unsigned char buf[100];
  int buflen, ret, i, found;
  found = 99;
  do {
    memset(buf, '\0', sizeof(buf));
    buflen = sizeof(buf);
    ret = eb_serial_recv(buf, &buflen);
    if (ret < 0)
      return -1;
    if (buflen > 0) {
      i = 0;
      while (i < buflen) {
        if (buf[i] == 0xAA && (i + 1) == buflen) {
          found = 1;
          break;
        }
        i++;
      }
      if (*skip > 0)
        *skip -= 1;
    }
  } while (found == 99);
  return 0;
}


x86 assembly (clang, -O0):
<eb_bus_wait_syn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x63,-0x90(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <memset@plt>
movl   $0x64,-0x84(%rbp)
lea    -0x80(%rbp),%rdi
lea    -0x84(%rbp),%rsi
call   1030 <eb_serial_recv@plt>
mov    %eax,-0x88(%rbp)
cmpl   $0x0,-0x88(%rbp)
jge    1182 <eb_bus_wait_syn+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <eb_bus_wait_syn+0x110>
cmpl   $0x0,-0x84(%rbp)
jle    1217 <eb_bus_wait_syn+0xf7>
movl   $0x0,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
cmp    -0x84(%rbp),%eax
jge    11fa <eb_bus_wait_syn+0xda>
movslq -0x8c(%rbp),%rax
movzbl -0x80(%rbp,%rax,1),%eax
cmp    $0xaa,%eax
jne    11e6 <eb_bus_wait_syn+0xc6>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
cmp    -0x84(%rbp),%eax
jne    11e6 <eb_bus_wait_syn+0xc6>
movl   $0x1,-0x90(%rbp)
jmp    11fa <eb_bus_wait_syn+0xda>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    1199 <eb_bus_wait_syn+0x79>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jle    1212 <eb_bus_wait_syn+0xf2>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1217 <eb_bus_wait_syn+0xf7>
jmp    121c <eb_bus_wait_syn+0xfc>
cmpl   $0x63,-0x90(%rbp)
je     1139 <eb_bus_wait_syn+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eb_bus_wait_syn>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rdi,%r14
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
jmp    114a <eb_bus_wait_syn+0x2a>
nopl   0x0(%rax,%rax,1)
mov    $0x63,%eax
cmp    $0x63,%eax
jne    11b4 <eb_bus_wait_syn+0x94>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1040 <memset@plt>
movl   $0x64,0xc(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <eb_serial_recv@plt>
test   %eax,%eax
js     11b8 <eb_bus_wait_syn+0x98>
mov    0xc(%rsp),%eax
test   %eax,%eax
jle    1140 <eb_bus_wait_syn+0x20>
lea    -0x1(%rax),%rcx
xor    %edx,%edx
xchg   %ax,%ax
cmp    %rdx,%rax
je     11a0 <eb_bus_wait_syn+0x80>
mov    %rdx,%rsi
add    $0x1,%rdx
cmpb   $0xaa,0x10(%rsp,%rsi,1)
jne    1180 <eb_bus_wait_syn+0x60>
cmp    %rsi,%rcx
jne    1180 <eb_bus_wait_syn+0x60>
mov    $0x1,%eax
jmp    11a5 <eb_bus_wait_syn+0x85>
nop
mov    $0x63,%eax
mov    (%r14),%ecx
test   %ecx,%ecx
jle    1145 <eb_bus_wait_syn+0x25>
add    $0xffffffff,%ecx
mov    %ecx,(%r14)
jmp    1145 <eb_bus_wait_syn+0x25>
xor    %eax,%eax
jmp    11bd <eb_bus_wait_syn+0x9d>
mov    $0xffffffff,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @eb_bus_wait_syn(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i32 99, i32* %8
br label %9
9: ; preds = %51, %1
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i8 signext 0, i32 100)
store i32 100, i32* %5
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%13 = call i32 @eb_serial_recv(i8* %12, i32* %5)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %9
store i32 -1, i32* %2
br label %55
17: ; preds = %9
%18 = load i32, i32* %5
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %50
20: ; preds = %17
store i32 0, i32* %7
br label %21
21: ; preds = %38, %20
%22 = load i32, i32* %7
%23 = load i32, i32* %5
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %41
25: ; preds = %21
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = icmp eq i32 %30, 170
br i1 %31, label %32, label %38
32: ; preds = %25
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
%35 = load i32, i32* %5
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 1, i32* %8
br label %41
38: ; preds = %32, %25
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %21
41: ; preds = %37, %21
%42 = load i32*, i32** %3
%43 = load i32, i32* %42
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32*, i32** %3
%47 = load i32, i32* %46
%48 = sub nsw i32 %47, 1
store i32 %48, i32* %46
br label %49
49: ; preds = %45, %41
br label %50
50: ; preds = %49, %17
br label %51
51: ; preds = %50
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, 99
br i1 %53, label %9, label %54
54: ; preds = %51
store i32 0, i32* %2
br label %55
55: ; preds = %54, %16
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
define i32 @eb_bus_wait_syn(i32* nocapture %0) {
%2 = alloca [100 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
br label %6
6: ; preds = %31, %1
%7 = call i32 @memset(i8* %4, i8 signext 0, i32 100)
store i32 100, i32* %3
%8 = call i32 @eb_serial_recv(i8* %4, i32* %3)
%9 = icmp slt i32 %8, 0
br i1 %9, label %34, label %10
10: ; preds = %6
%11 = load i32, i32* %3
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %10
%14 = zext i32 %11 to i64
br label %15
15: ; preds = %13, %18
%16 = phi i64 [ 0, %13 ], [ %22, %18 ]
%17 = icmp eq i64 %16, %14
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %16
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, -86
%22 = add nuw nsw i64 %16, 1
%23 = icmp eq i64 %22, %14
%24 = select i1 %21, i1 %23, i1 false
br i1 %24, label %25, label %15
25: ; preds = %18, %15
%26 = phi i32 [ 99, %15 ], [ 1, %18 ]
%27 = load i32, i32* %0
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = add nsw i32 %27, -1
store i32 %30, i32* %0
br label %31
31: ; preds = %10, %29, %25
%32 = phi i32 [ %26, %29 ], [ %26, %25 ], [ 99, %10 ]
%33 = icmp eq i32 %32, 99
br i1 %33, label %6, label %34
34: ; preds = %31, %6
%35 = phi i32 [ -1, %6 ], [ 0, %31 ]
ret i32 %35
}