#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*/ UART1_Data_Ready();
unsigned char UART_Read();
int receive_frame(unsigned char *buf) {
  int i, length;
  buf[0] = 0x7E;
  for (i = 1; i < 3; i++) {
    while (!UART1_Data_Ready()) {
    };
    buf[i] = UART_Read();
  }
  length = (((unsigned int)buf[1]) << 8) | ((unsigned int)buf[2]);
  for (i = 3; i < length + 4; i++) {
    while (!UART1_Data_Ready()) {
    }
    buf[i] = UART_Read();
  }
  return length + 4;
}


x86 assembly (clang, -O0):
<receive_frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x7e,(%rax)
movl   $0x1,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    118c <receive_frame+0x6c>
jmp    1149 <receive_frame+0x29>
mov    $0x0,%al
call   1040 <UART1_Data_Ready@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1165 <receive_frame+0x45>
jmp    116a <receive_frame+0x4a>
jmp    1149 <receive_frame+0x29>
mov    $0x0,%al
call   1030 <UART_Read@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113a <receive_frame+0x1a>
mov    -0x8(%rbp),%rax
movzbl 0x1(%rax),%eax
shl    $0x8,%eax
mov    -0x8(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x10(%rbp)
movl   $0x3,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    $0x4,%ecx
cmp    %ecx,%eax
jge    1204 <receive_frame+0xe4>
jmp    11c1 <receive_frame+0xa1>
mov    $0x0,%al
call   1040 <UART1_Data_Ready@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11dd <receive_frame+0xbd>
jmp    11e2 <receive_frame+0xc2>
jmp    11c1 <receive_frame+0xa1>
mov    $0x0,%al
call   1030 <UART_Read@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ab <receive_frame+0x8b>
mov    -0x10(%rbp),%eax
add    $0x4,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_frame>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
movb   $0x7e,(%rdi)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <UART1_Data_Ready@plt>
test   %eax,%eax
je     1130 <receive_frame+0x10>
xor    %eax,%eax
call   1030 <UART_Read@plt>
mov    %al,0x1(%r14)
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <UART1_Data_Ready@plt>
test   %eax,%eax
je     1150 <receive_frame+0x30>
xor    %eax,%eax
call   1030 <UART_Read@plt>
mov    %al,0x2(%r14)
movzbl 0x1(%r14),%ecx
shl    $0x8,%rcx
movzbl %al,%eax
lea    (%rcx,%rax,1),%r15
add    $0x4,%r15
mov    $0x3,%ebx
nop
xor    %eax,%eax
call   1040 <UART1_Data_Ready@plt>
test   %eax,%eax
je     1180 <receive_frame+0x60>
xor    %eax,%eax
call   1030 <UART_Read@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %r15,%rbx
jne    1180 <receive_frame+0x60>
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @receive_frame(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = getelementptr inbounds i8, i8* %5, i64 0
store i8 126, i8* %6
store i32 1, i32* %3
br label %7
7: ; preds = %22, %1
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 3
br i1 %9, label %10, label %25
10: ; preds = %7
br label %11
11: ; preds = %15, %10
%12 = call i32 (...) @UART1_Data_Ready()
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %16
15: ; preds = %11
br label %11
16: ; preds = %11
%17 = call zeroext i8 (...) @UART_Read()
%18 = load i8*, i8** %2
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 %17, i8* %21
br label %22
22: ; preds = %16
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %7
25: ; preds = %7
%26 = load i8*, i8** %2
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = shl i32 %29, 8
%31 = load i8*, i8** %2
%32 = getelementptr inbounds i8, i8* %31, i64 2
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = or i32 %30, %34
store i32 %35, i32* %4
store i32 3, i32* %3
br label %36
36: ; preds = %53, %25
%37 = load i32, i32* %3
%38 = load i32, i32* %4
%39 = add nsw i32 %38, 4
%40 = icmp slt i32 %37, %39
br i1 %40, label %41, label %56
41: ; preds = %36
br label %42
42: ; preds = %46, %41
%43 = call i32 (...) @UART1_Data_Ready()
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
br i1 %45, label %46, label %47
46: ; preds = %42
br label %42
47: ; preds = %42
%48 = call zeroext i8 (...) @UART_Read()
%49 = load i8*, i8** %2
%50 = load i32, i32* %3
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 %48, i8* %52
br label %53
53: ; preds = %47
%54 = load i32, i32* %3
%55 = add nsw i32 %54, 1
store i32 %55, i32* %3
br label %36
56: ; preds = %36
%57 = load i32, i32* %4
%58 = add nsw i32 %57, 4
ret i32 %58
}

LLVM IR (-O2):
define i32 @receive_frame(i8* nocapture %0) {
store i8 126, i8* %0
br label %2
2: ; preds = %1, %2
%3 = tail call i32 (...) @UART1_Data_Ready()
%4 = icmp eq i32 %3, 0
br i1 %4, label %2, label %5
5: ; preds = %2
%6 = tail call zeroext i8 (...) @UART_Read()
%7 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %6, i8* %7
br label %8
8: ; preds = %8, %5
%9 = tail call i32 (...) @UART1_Data_Ready()
%10 = icmp eq i32 %9, 0
br i1 %10, label %8, label %11
11: ; preds = %8
%12 = tail call zeroext i8 (...) @UART_Read()
%13 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %12, i8* %13
%14 = load i8, i8* %7
%15 = zext i8 %14 to i32
%16 = shl nuw nsw i32 %15, 8
%17 = zext i8 %12 to i32
%18 = or i32 %16, %17
%19 = add nuw nsw i32 %18, 4
%20 = zext i32 %19 to i64
br label %21
21: ; preds = %11, %26
%22 = phi i64 [ 3, %11 ], [ %29, %26 ]
br label %23
23: ; preds = %21, %23
%24 = tail call i32 (...) @UART1_Data_Ready()
%25 = icmp eq i32 %24, 0
br i1 %25, label %23, label %26
26: ; preds = %23
%27 = tail call zeroext i8 (...) @UART_Read()
%28 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 %27, i8* %28
%29 = add nuw nsw i64 %22, 1
%30 = icmp eq i64 %29, %20
br i1 %30, label %31, label %21
31: ; preds = %26
ret i32 %19
}