#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 uint8_t;
typedef int int32_t;

/* Variables and functions */
scalar_t__ SendCommand(int *, int);
int /*<<< orphan*/ UARTReceiveData(int *, int);
scalar_t__ UARTSendData(int const *, int);
int32_t AutoBaud(void) {
  static uint8_t const pui8SyncPattern[] = {0x55, 0x55};
  uint8_t ui8Command;
  uint8_t ui8Ack;
  if (UARTSendData(pui8SyncPattern, 2)) {
    return (-1);
  }
  do {
    UARTReceiveData(&ui8Ack, 1);
  } while (ui8Ack == 0);
  if (ui8Ack != 0xcc) {
    return (-1);
  }
  ui8Command = 0x20;
  if (SendCommand(&ui8Command, 1) < 0) {
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<AutoBaud>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xec1(%rip),%rdi
mov    $0x2,%esi
call   1040 <UARTSendData@plt>
cmp    $0x0,%rax
je     115f <AutoBaud+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <AutoBaud+0x97>
jmp    1164 <AutoBaud+0x34>
lea    -0xc(%rbp),%rdi
mov    $0x1,%esi
call   1030 <UARTReceiveData@plt>
cmpl   $0x0,-0xc(%rbp)
je     1164 <AutoBaud+0x34>
cmpl   $0xcc,-0xc(%rbp)
je     1195 <AutoBaud+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <AutoBaud+0x97>
movl   $0x20,-0x8(%rbp)
lea    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1050 <SendCommand@plt>
cmp    $0x0,%rax
jge    11c0 <AutoBaud+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <AutoBaud+0x97>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AutoBaud>:
push   %r14
push   %rbx
push   %rax
lea    0xec5(%rip),%rdi
mov    $0x2,%esi
call   1040 <UARTSendData@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    1199 <AutoBaud+0x69>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <UARTReceiveData@plt>
mov    (%rsp),%eax
test   %eax,%eax
je     1160 <AutoBaud+0x30>
cmp    $0xcc,%eax
jne    1199 <AutoBaud+0x69>
movl   $0x20,0x4(%rsp)
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
call   1050 <SendCommand@plt>
mov    %rax,%r14
sar    $0x3f,%r14
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@AutoBaud.pui8SyncPattern = internal constant [2 x i32] [i32 85, i32 85]
define i32 @AutoBaud() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i64 @UARTSendData(i32* getelementptr inbounds ([2 x i32], [2 x i32]* @AutoBaud.pui8SyncPattern, i64 0, i64 0), i32 2)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 -1, i32* %1
br label %22
7: ; preds = %0
br label %8
8: ; preds = %10, %7
%9 = call i32 @UARTReceiveData(i32* %3, i32 1)
br label %10
10: ; preds = %8
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
br i1 %12, label %8, label %13
13: ; preds = %10
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 204
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -1, i32* %1
br label %22
17: ; preds = %13
store i32 32, i32* %2
%18 = call i64 @SendCommand(i32* %2, i32 1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 -1, i32* %1
br label %22
21: ; preds = %17
store i32 0, i32* %1
br label %22
22: ; preds = %21, %20, %16, %6
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@AutoBaud.pui8SyncPattern = internal constant [2 x i32] [i32 85, i32 85]
define i32 @AutoBaud() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i64 @UARTSendData(i32* getelementptr inbounds ([2 x i32], [2 x i32]* @AutoBaud.pui8SyncPattern, i64 0, i64 0), i32 2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %0, %7
%8 = call i32 @UARTReceiveData(i32* %2, i32 1)
%9 = load i32, i32* %2
switch i32 %9, label %14 [
i32 0, label %7
i32 204, label %10
]
10: ; preds = %7
store i32 32, i32* %1
%11 = call i64 @SendCommand(i32* %1, i32 1)
%12 = ashr i64 %11, 63
%13 = trunc i64 %12 to i32
br label %14
14: ; preds = %7, %10, %0
%15 = phi i32 [ -1, %0 ], [ %13, %10 ], [ -1, %7 ]
ret i32 %15
}