#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*/ uint8_t;
typedef int uint32_t;

/* Variables and functions */
int CheckSum(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ NakPacket();
int /*<<< orphan*/ UARTReceive(int /*<<< orphan*/ *, int);
int ReceivePacket(uint8_t *pui8Data, uint32_t *pui32Size) {
  uint32_t ui32Size, ui32CheckSum;
  ui32Size = 0;
  while (ui32Size == 0) {
    UARTReceive((uint8_t *)&ui32Size, 1);
  }
  ui32Size -= 2;
  UARTReceive((uint8_t *)&ui32CheckSum, 1);
  if (*pui32Size >= ui32Size) {
    UARTReceive(pui8Data, ui32Size);
    if (CheckSum(pui8Data, ui32Size) != (ui32CheckSum & 0xff)) {
      NakPacket();
      return (-1);
    }
  } else {
    while (ui32Size--) {
      UARTReceive(pui8Data, 1);
    }
    return (-1);
  }
  *pui32Size = ui32Size;
  return (0);
}


x86 assembly (clang, -O0):
<ReceivePacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1164 <ReceivePacket+0x34>
lea    -0x1c(%rbp),%rdi
mov    $0x1,%esi
call   1030 <UARTReceive@plt>
jmp    1147 <ReceivePacket+0x17>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1030 <UARTReceive@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
cmp    -0x1c(%rbp),%eax
jl     11cb <ReceivePacket+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <UARTReceive@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <CheckSum@plt>
mov    -0x20(%rbp),%ecx
and    $0xff,%ecx
cmp    %ecx,%eax
je     11c6 <ReceivePacket+0x96>
mov    $0x0,%al
call   1040 <NakPacket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <ReceivePacket+0xe3>
jmp    1203 <ReceivePacket+0xd3>
jmp    11d0 <ReceivePacket+0xa0>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
je     11f7 <ReceivePacket+0xc7>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1030 <UARTReceive@plt>
jmp    11d0 <ReceivePacket+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <ReceivePacket+0xe3>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReceivePacket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x1,%esi
call   1030 <UARTReceive@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
je     1150 <ReceivePacket+0x20>
add    $0xfffffffe,%eax
mov    %eax,0x8(%rsp)
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
call   1030 <UARTReceive@plt>
mov    0x8(%rsp),%esi
cmp    %esi,(%r14)
jge    11b6 <ReceivePacket+0x86>
mov    $0xffffffff,%ebp
sub    $0x1,%esi
mov    %esi,0x8(%rsp)
jb     11ea <ReceivePacket+0xba>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <UARTReceive@plt>
subl   $0x1,0x8(%rsp)
jae    11a0 <ReceivePacket+0x70>
jmp    11ea <ReceivePacket+0xba>
mov    %rbx,%rdi
call   1030 <UARTReceive@plt>
mov    0x8(%rsp),%esi
mov    %rbx,%rdi
call   1050 <CheckSum@plt>
movzbl 0xc(%rsp),%ecx
cmp    %ecx,%eax
jne    11de <ReceivePacket+0xae>
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
xor    %ebp,%ebp
jmp    11ea <ReceivePacket+0xba>
xor    %eax,%eax
call   1040 <NakPacket@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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