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

/* Variables and functions */
int AckPacket();
scalar_t__ CheckSum(scalar_t__ *, scalar_t__);
int NakPacket();
scalar_t__ UARTReceiveData(scalar_t__ *, scalar_t__);
int32_t GetPacket(uint8_t *pui8Data, uint8_t *pui8Size) {
  uint8_t ui8CheckSum;
  uint8_t ui8Size;
  do {
    if (UARTReceiveData(&ui8Size, 1)) {
      return (-1);
    }
  } while (ui8Size == 0);
  if (UARTReceiveData(&ui8CheckSum, 1)) {
    return (-1);
  }
  *pui8Size = ui8Size - 2;
  if (UARTReceiveData(pui8Data, *pui8Size)) {
    *pui8Size = 0;
    return (-1);
  }
  if (CheckSum(pui8Data, *pui8Size) != ui8CheckSum) {
    *pui8Size = 0;
    return (NakPacket());
  }
  return (AckPacket());
}


x86 assembly (clang, -O0):
<GetPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1030 <UARTReceiveData@plt>
cmp    $0x0,%rax
je     1174 <GetPacket+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <GetPacket+0xe6>
jmp    1179 <GetPacket+0x39>
cmpq   $0x0,-0x28(%rbp)
je     1150 <GetPacket+0x10>
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1030 <UARTReceiveData@plt>
cmp    $0x0,%rax
je     11a8 <GetPacket+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <GetPacket+0xe6>
mov    -0x28(%rbp),%rcx
sub    $0x2,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1030 <UARTReceiveData@plt>
cmp    $0x0,%rax
je     11e8 <GetPacket+0xa8>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <GetPacket+0xe6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1060 <CheckSum@plt>
cmp    -0x20(%rbp),%rax
je     121c <GetPacket+0xdc>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1040 <NakPacket@plt>
mov    %eax,-0x4(%rbp)
jmp    1226 <GetPacket+0xe6>
mov    $0x0,%al
call   1050 <AckPacket@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetPacket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rbx
mov    $0xffffffff,%ebp
nopw   0x0(%rax,%rax,1)
mov    $0x1,%esi
mov    %rbx,%rdi
call   1030 <UARTReceiveData@plt>
test   %rax,%rax
jne    11db <GetPacket+0x9b>
cmpq   $0x0,0x8(%rsp)
je     1160 <GetPacket+0x20>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
call   1030 <UARTReceiveData@plt>
test   %rax,%rax
jne    11db <GetPacket+0x9b>
mov    0x8(%rsp),%rsi
add    $0xfffffffffffffffe,%rsi
mov    %rsi,(%r14)
mov    %r15,%rdi
call   1030 <UARTReceiveData@plt>
test   %rax,%rax
je     11b0 <GetPacket+0x70>
movq   $0x0,(%r14)
jmp    11db <GetPacket+0x9b>
mov    (%r14),%rsi
mov    %r15,%rdi
call   1060 <CheckSum@plt>
cmp    0x10(%rsp),%rax
jne    11cb <GetPacket+0x8b>
xor    %eax,%eax
call   1050 <AckPacket@plt>
jmp    11d9 <GetPacket+0x99>
movq   $0x0,(%r14)
xor    %eax,%eax
call   1040 <NakPacket@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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