#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;

/* Variables and functions */
int /*<<< orphan*/ read_n_bytes(int, int *, int);
int stm32w_checksum(int *, int);
int /*<<< orphan*/ warning(char *, int);
int stm32w_read_frame(int fd, uint8_t *buf) {
  while (1) {
    read_n_bytes(fd, buf, 2);
    if ((buf[0] == 0x15) && (buf[1] == 0xFF)) {
      break;
    }
  }
  read_n_bytes(fd, buf + 2, 1);
  int frame_length = *(buf + 2);
  read_n_bytes(fd, buf + 3, frame_length + 1);
  int cksum = stm32w_checksum(buf + 2, frame_length);
  if (buf[2 + frame_length + 1] != 0x0C) {
    warning("expecting 0x0C frame terminator, but got %02X",
            buf[2 + frame_length + 1]);
  }
  return (cksum == buf[2 + frame_length]) ? frame_length + 4 : -1;
}


x86 assembly (clang, -O0):
<stm32w_read_frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1040 <read_n_bytes@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x15,(%rax)
jne    1173 <stm32w_read_frame+0x43>
mov    -0x10(%rbp),%rax
cmpl   $0xff,0x4(%rax)
jne    1173 <stm32w_read_frame+0x43>
jmp    1178 <stm32w_read_frame+0x48>
jmp    113f <stm32w_read_frame+0xf>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
mov    $0x1,%edx
call   1040 <read_n_bytes@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0xc,%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1040 <read_n_bytes@plt>
mov    -0x10(%rbp),%rdi
add    $0x8,%rdi
mov    -0x14(%rbp),%esi
call   1050 <stm32w_checksum@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x2,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
cmpl   $0xc,(%rax,%rcx,4)
je     11f9 <stm32w_read_frame+0xc9>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x2,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe0c(%rip),%rdi
call   1030 <warning@plt>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%edx
add    $0x2,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jne    1220 <stm32w_read_frame+0xf0>
mov    -0x14(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    122d <stm32w_read_frame+0xfd>
mov    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    122d <stm32w_read_frame+0xfd>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stm32w_read_frame>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1040 <read_n_bytes@plt>
cmpl   $0x15,(%rbx)
jne    1140 <stm32w_read_frame+0x10>
cmpl   $0xff,0x4(%rbx)
jne    1140 <stm32w_read_frame+0x10>
lea    0x8(%rbx),%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read_n_bytes@plt>
mov    0x8(%rbx),%eax
movslq %eax,%r14
lea    0xc(%rbx),%rsi
lea    0x1(%r14),%edx
mov    %ebp,%edi
call   1040 <read_n_bytes@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1050 <stm32w_checksum@plt>
mov    %eax,%ebp
mov    0xc(%rbx,%r14,4),%esi
cmp    $0xc,%esi
je     11a8 <stm32w_read_frame+0x78>
lea    0xe5d(%rip),%rdi
call   1030 <warning@plt>
cmp    0x8(%rbx,%r14,4),%ebp
lea    0x4(%r14),%ecx
mov    $0xffffffff,%eax
cmove  %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"expecting 0x0C frame terminator, but got %02X\00"
define i32 @stm32w_read_frame(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
br label %7
7: ; preds = %2, %21
%8 = load i32, i32* %3
%9 = load i32*, i32** %4
%10 = call i32 @read_n_bytes(i32 %8, i32* %9, i32 2)
%11 = load i32*, i32** %4
%12 = getelementptr inbounds i32, i32* %11, i64 0
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 21
br i1 %14, label %15, label %21
15: ; preds = %7
%16 = load i32*, i32** %4
%17 = getelementptr inbounds i32, i32* %16, i64 1
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 255
br i1 %19, label %20, label %21
20: ; preds = %15
br label %22
21: ; preds = %15, %7
br label %7
22: ; preds = %20
%23 = load i32, i32* %3
%24 = load i32*, i32** %4
%25 = getelementptr inbounds i32, i32* %24, i64 2
%26 = call i32 @read_n_bytes(i32 %23, i32* %25, i32 1)
%27 = load i32*, i32** %4
%28 = getelementptr inbounds i32, i32* %27, i64 2
%29 = load i32, i32* %28
store i32 %29, i32* %5
%30 = load i32, i32* %3
%31 = load i32*, i32** %4
%32 = getelementptr inbounds i32, i32* %31, i64 3
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
%35 = call i32 @read_n_bytes(i32 %30, i32* %32, i32 %34)
%36 = load i32*, i32** %4
%37 = getelementptr inbounds i32, i32* %36, i64 2
%38 = load i32, i32* %5
%39 = call i32 @stm32w_checksum(i32* %37, i32 %38)
store i32 %39, i32* %6
%40 = load i32*, i32** %4
%41 = load i32, i32* %5
%42 = add nsw i32 2, %41
%43 = add nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %40, i64 %44
%46 = load i32, i32* %45
%47 = icmp ne i32 %46, 12
br i1 %47, label %48, label %57
48: ; preds = %22
%49 = load i32*, i32** %4
%50 = load i32, i32* %5
%51 = add nsw i32 2, %50
%52 = add nsw i32 %51, 1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %49, i64 %53
%55 = load i32, i32* %54
%56 = call i32 @warning(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %48, %22
%58 = load i32, i32* %6
%59 = load i32*, i32** %4
%60 = load i32, i32* %5
%61 = add nsw i32 2, %60
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %59, i64 %62
%64 = load i32, i32* %63
%65 = icmp eq i32 %58, %64
br i1 %65, label %66, label %69
66: ; preds = %57
%67 = load i32, i32* %5
%68 = add nsw i32 %67, 4
br label %70
69: ; preds = %57
br label %70
70: ; preds = %69, %66
%71 = phi i32 [ %68, %66 ], [ -1, %69 ]
ret i32 %71
}

LLVM IR (-O2):
@.str = [46 x i8] c"expecting 0x0C frame terminator, but got %02X\00"
define i32 @stm32w_read_frame(i32 %0, i32* %1) {
%3 = getelementptr inbounds i32, i32* %1, i64 1
br label %4
4: ; preds = %11, %2
%5 = tail call i32 @read_n_bytes(i32 %0, i32* %1, i32 2)
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, 21
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, 255
br i1 %10, label %12, label %11
11: ; preds = %8, %4
br label %4
12: ; preds = %8
%13 = getelementptr inbounds i32, i32* %1, i64 2
%14 = tail call i32 @read_n_bytes(i32 %0, i32* %13, i32 1)
%15 = load i32, i32* %13
%16 = getelementptr inbounds i32, i32* %1, i64 3
%17 = add nsw i32 %15, 1
%18 = tail call i32 @read_n_bytes(i32 %0, i32* %16, i32 %17)
%19 = tail call i32 @stm32w_checksum(i32* %13, i32 %15)
%20 = add nsw i32 %15, 3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %1, i64 %21
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 12
br i1 %24, label %27, label %25
25: ; preds = %12
%26 = tail call i32 @warning(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %27
27: ; preds = %25, %12
%28 = add nsw i32 %15, 2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %1, i64 %29
%31 = load i32, i32* %30
%32 = icmp eq i32 %19, %31
%33 = add nsw i32 %15, 4
%34 = select i1 %32, i32 %33, i32 -1
ret i32 %34
}