#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 /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TIOCCBRK;
int /*<<< orphan*/ TIOCSBRK;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ msleep(int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
int wait_for_aa(int fd) {
  int ret, count = 0;
  unsigned char buf[1];
  ioctl(fd, TIOCSBRK, NULL);
  msleep(16);
  ioctl(fd, TIOCCBRK, NULL);
  tcflush(fd, TCIOFLUSH);
  write(fd, "\x00", 1);
  usleep(16000);
  while (count++ < 200) {
    ret = read(fd, buf, 1);
    if (ret > 0) {
      return 0;
      if (buf[0] == 0xaa)
        return 0;
    }
    usleep(50000);
  }
  return 1;
}


x86 assembly (clang, -O0):
<wait_for_aa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <ioctl@plt>
mov    $0x10,%edi
call   1040 <msleep@plt>
mov    -0x8(%rbp),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <ioctl@plt>
mov    -0x8(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1050 <tcflush@plt>
mov    -0x8(%rbp),%edi
lea    0xe3f(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x3e80,%edi
call   1080 <usleep@plt>
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x10(%rbp)
cmp    $0xc8,%eax
jge    1224 <wait_for_aa+0xc4>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1215 <wait_for_aa+0xb5>
movl   $0x0,-0x4(%rbp)
jmp    122b <wait_for_aa+0xcb>
mov    $0xc350,%edi
call   1080 <usleep@plt>
jmp    11d5 <wait_for_aa+0x75>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_aa>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1060 <ioctl@plt>
mov    $0x10,%edi
call   1040 <msleep@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <ioctl@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <tcflush@plt>
lea    0xe51(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x3e80,%edi
call   1080 <usleep@plt>
mov    $0xc8,%ebx
lea    0x7(%rsp),%r15
nop
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     11f8 <wait_for_aa+0x98>
mov    $0xc350,%edi
call   1080 <usleep@plt>
add    $0xffffffff,%ebx
jne    11d0 <wait_for_aa+0x70>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@TIOCSBRK = global i32 0
@TIOCCBRK = global i32 0
@TCIOFLUSH = global i32 0
@.str = [2 x i8] zeroinitializer
define i32 @wait_for_aa(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1 x i8]
store i32 %0, i32* %3
store i32 0, i32* %5
%7 = load i32, i32* %3
%8 = load i32, i32* @TIOCSBRK
%9 = call i32 @ioctl(i32 %7, i32 %8, i32* null)
%10 = call i32 @msleep(i32 16)
%11 = load i32, i32* %3
%12 = load i32, i32* @TIOCCBRK
%13 = call i32 @ioctl(i32 %11, i32 %12, i32* null)
%14 = load i32, i32* %3
%15 = load i32, i32* @TCIOFLUSH
%16 = call i32 @tcflush(i32 %14, i32 %15)
%17 = load i32, i32* %3
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%19 = call i32 @usleep(i32 16000)
br label %20
20: ; preds = %31, %1
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 1
store i32 %22, i32* %5
%23 = icmp slt i32 %21, 200
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i32, i32* %3
%26 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 1)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 0, i32* %2
br label %34
31: ; preds = %24
%32 = call i32 @usleep(i32 50000)
br label %20
33: ; preds = %20
store i32 1, i32* %2
br label %34
34: ; preds = %33, %30
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@TIOCSBRK = global i32 0
@TIOCCBRK = global i32 0
@TCIOFLUSH = global i32 0
@.str = [2 x i8] zeroinitializer
define i32 @wait_for_aa(i32 %0) {
%2 = alloca [1 x i8]
%3 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @TIOCSBRK
%5 = tail call i32 @ioctl(i32 %0, i32 %4, i32* null)
%6 = tail call i32 @msleep(i32 16)
%7 = load i32, i32* @TIOCCBRK
%8 = tail call i32 @ioctl(i32 %0, i32 %7, i32* null)
%9 = load i32, i32* @TCIOFLUSH
%10 = tail call i32 @tcflush(i32 %0, i32 %9)
%11 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%12 = tail call i32 @usleep(i32 16000)
br label %13
13: ; preds = %1, %17
%14 = phi i32 [ 1, %1 ], [ %19, %17 ]
%15 = call i32 @read(i32 %0, i8* %3, i32 1)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = tail call i32 @usleep(i32 50000)
%19 = add nuw nsw i32 %14, 1
%20 = icmp eq i32 %19, 201
br i1 %20, label %21, label %13
21: ; preds = %17, %13
%22 = phi i32 [ 0, %13 ], [ 1, %17 ]
ret i32 %22
}