#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 */
int CheckSum(int *, int);
scalar_t__ UARTReceiveData(int *, int);
scalar_t__ UARTSendData(int *, int);
int32_t SendPacket(uint8_t *pui8Data, uint8_t ui8Size, bool bAck) {
  uint8_t ui8CheckSum;
  uint8_t ui8Ack;
  ui8CheckSum = CheckSum(pui8Data, ui8Size);
  ui8Size += 2;
  if (UARTSendData(&ui8Size, 1)) {
    return (-1);
  }
  if (UARTSendData(&ui8CheckSum, 1)) {
    return (-1);
  }
  ui8Size -= 2;
  if (UARTSendData(pui8Data, ui8Size)) {
    return (-1);
  }
  if (!bAck) {
    return (0);
  }
  do {
    if (UARTReceiveData(&ui8Ack, 1)) {
      return (-1);
    }
  } while (ui8Ack == 0);
  if (ui8Ack != 0xcc) {
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<SendPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <CheckSum@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
lea    -0x14(%rbp),%rdi
mov    $0x1,%esi
call   1040 <UARTSendData@plt>
cmp    $0x0,%rax
je     117e <SendPacket+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <SendPacket+0x10b>
lea    -0x1c(%rbp),%rdi
mov    $0x1,%esi
call   1040 <UARTSendData@plt>
cmp    $0x0,%rax
je     11a2 <SendPacket+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <SendPacket+0x10b>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <UARTSendData@plt>
cmp    $0x0,%rax
je     11cd <SendPacket+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <SendPacket+0x10b>
cmpl   $0x0,-0x18(%rbp)
jne    11e3 <SendPacket+0xb3>
movl   $0x0,-0x4(%rbp)
jmp    123b <SendPacket+0x10b>
jmp    11e8 <SendPacket+0xb8>
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1030 <UARTReceiveData@plt>
cmp    $0x0,%rax
je     120c <SendPacket+0xdc>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <SendPacket+0x10b>
jmp    1211 <SendPacket+0xe1>
cmpl   $0x0,-0x20(%rbp)
je     11e8 <SendPacket+0xb8>
cmpl   $0xcc,-0x20(%rbp)
je     1234 <SendPacket+0x104>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <SendPacket+0x10b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SendPacket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%rbp
call   1050 <CheckSum@plt>
mov    %eax,0xc(%rsp)
add    $0x2,%ebx
mov    %ebx,0x4(%rsp)
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
call   1040 <UARTSendData@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11c3 <SendPacket+0x93>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
call   1040 <UARTSendData@plt>
test   %rax,%rax
jne    11c3 <SendPacket+0x93>
mov    0x4(%rsp),%esi
add    $0xfffffffe,%esi
mov    %esi,0x4(%rsp)
mov    %rbp,%rdi
call   1040 <UARTSendData@plt>
test   %rax,%rax
jne    11c3 <SendPacket+0x93>
test   %r14d,%r14d
je     11c1 <SendPacket+0x91>
lea    0x8(%rsp),%rbp
nop
mov    %rbp,%rdi
mov    $0x1,%esi
call   1030 <UARTReceiveData@plt>
test   %rax,%rax
jne    11c3 <SendPacket+0x93>
mov    0x8(%rsp),%eax
test   %eax,%eax
je     11a0 <SendPacket+0x70>
cmp    $0xcc,%eax
jne    11c3 <SendPacket+0x93>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @SendPacket(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32*, i32** %5
%11 = load i32, i32* %6
%12 = call i32 @CheckSum(i32* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %6
%14 = add nsw i32 %13, 2
store i32 %14, i32* %6
%15 = call i64 @UARTSendData(i32* %6, i32 1)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %48
18: ; preds = %3
%19 = call i64 @UARTSendData(i32* %8, i32 1)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %4
br label %48
22: ; preds = %18
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, 2
store i32 %24, i32* %6
%25 = load i32*, i32** %5
%26 = load i32, i32* %6
%27 = call i64 @UARTSendData(i32* %25, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 -1, i32* %4
br label %48
30: ; preds = %22
%31 = load i32, i32* %7
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %30
store i32 0, i32* %4
br label %48
34: ; preds = %30
br label %35
35: ; preds = %40, %34
%36 = call i64 @UARTReceiveData(i32* %9, i32 1)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 -1, i32* %4
br label %48
39: ; preds = %35
br label %40
40: ; preds = %39
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, 0
br i1 %42, label %35, label %43
43: ; preds = %40
%44 = load i32, i32* %9
%45 = icmp ne i32 %44, 204
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 -1, i32* %4
br label %48
47: ; preds = %43
store i32 0, i32* %4
br label %48
48: ; preds = %47, %46, %38, %33, %29, %21, %17
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
define i32 @SendPacket(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = tail call i32 @CheckSum(i32* %0, i32 %1)
store i32 %9, i32* %5
%10 = add nsw i32 %1, 2
store i32 %10, i32* %4
%11 = call i64 @UARTSendData(i32* %4, i32 1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %3
%14 = call i64 @UARTSendData(i32* %5, i32 1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = load i32, i32* %4
%18 = add nsw i32 %17, -2
store i32 %18, i32* %4
%19 = call i64 @UARTSendData(i32* %0, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %16
%22 = icmp eq i32 %2, 0
br i1 %22, label %29, label %23
23: ; preds = %21, %26
%24 = call i64 @UARTReceiveData(i32* %6, i32 1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i32, i32* %6
switch i32 %27, label %28 [
i32 0, label %23
i32 204, label %29
]
28: ; preds = %23, %26
br label %29
29: ; preds = %26, %28, %21, %16, %13, %3
%30 = phi i32 [ -1, %3 ], [ -1, %13 ], [ -1, %16 ], [ 0, %21 ], [ -1, %28 ], [ 0, %26 ]
ret i32 %30
}