#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 */
struct ringbuf {
  int dummy;
};

/* Variables and functions */
int ringbuf_filled(struct ringbuf *);
int ringbuf_sneak(struct ringbuf *, int);
int legoscanpacket(struct ringbuf *buf, int *size, int finished) {
  unsigned char checksum, lastbyte;
  int pos = 0, avail;
  avail = ringbuf_filled(buf);
  if (avail >= 1024 - 16)
    return (2);
  if (avail < 7)
    return (((finished) && (avail > 0)) ? 2 : 0);
  if (ringbuf_sneak(buf, pos) != 0x55)
    return (2);
  else
    pos++;
  if (ringbuf_sneak(buf, pos) != 0xff)
    return (2);
  else
    pos++;
  if (ringbuf_sneak(buf, pos) != 0x00)
    return (2);
  else
    pos++;
  avail -= pos;
  checksum = lastbyte = 0;
  while ((avail >= 2) &&
         (ringbuf_sneak(buf, pos) == (0xff ^ ringbuf_sneak(buf, pos + 1)))) {
    checksum = (checksum + lastbyte) & 0xff;
    lastbyte = ringbuf_sneak(buf, pos);
    pos += 2;
    avail -= 2;
  }
  if ((avail < 2) && (!finished))
    return (0);
  if ((checksum == lastbyte) && (pos >= 7)) {
    *size = pos;
    return (1);
  } else
    return (2);
}


x86 assembly (clang, -O0):
<legoscanpacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ringbuf_filled@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x3f0,-0x28(%rbp)
jl     115f <legoscanpacket+0x3f>
movl   $0x2,-0x4(%rbp)
jmp    130b <legoscanpacket+0x1eb>
cmpl   $0x7,-0x28(%rbp)
jge    119a <legoscanpacket+0x7a>
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x29(%rbp)
je     1182 <legoscanpacket+0x62>
cmpl   $0x0,-0x28(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%dl
xor    %eax,%eax
mov    $0x2,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    130b <legoscanpacket+0x1eb>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <ringbuf_sneak@plt>
cmp    $0x55,%eax
je     11bb <legoscanpacket+0x9b>
movl   $0x2,-0x4(%rbp)
jmp    130b <legoscanpacket+0x1eb>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <ringbuf_sneak@plt>
cmp    $0xff,%eax
je     11e7 <legoscanpacket+0xc7>
movl   $0x2,-0x4(%rbp)
jmp    130b <legoscanpacket+0x1eb>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <ringbuf_sneak@plt>
cmp    $0x0,%eax
je     1211 <legoscanpacket+0xf1>
movl   $0x2,-0x4(%rbp)
jmp    130b <legoscanpacket+0x1eb>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
movb   $0x0,-0x1e(%rbp)
movb   $0x0,-0x1d(%rbp)
xor    %eax,%eax
cmpl   $0x2,-0x28(%rbp)
mov    %al,-0x2a(%rbp)
jl     126d <legoscanpacket+0x14d>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <ringbuf_sneak@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
call   1030 <ringbuf_sneak@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
xor    $0xff,%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    127d <legoscanpacket+0x15d>
jmp    12b5 <legoscanpacket+0x195>
movzbl -0x1d(%rbp),%eax
movzbl -0x1e(%rbp),%ecx
add    %ecx,%eax
and    $0xff,%eax
mov    %al,-0x1d(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <ringbuf_sneak@plt>
mov    %al,-0x1e(%rbp)
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x28(%rbp)
jmp    122d <legoscanpacket+0x10d>
cmpl   $0x2,-0x28(%rbp)
jge    12d5 <legoscanpacket+0x1b5>
cmpl   $0x0,-0x1c(%rbp)
jne    12d5 <legoscanpacket+0x1b5>
movl   $0x0,-0x4(%rbp)
jmp    130b <legoscanpacket+0x1eb>
movzbl -0x1d(%rbp),%eax
movzbl -0x1e(%rbp),%ecx
cmp    %ecx,%eax
jne    1304 <legoscanpacket+0x1e4>
cmpl   $0x7,-0x24(%rbp)
jl     1304 <legoscanpacket+0x1e4>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    130b <legoscanpacket+0x1eb>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<legoscanpacket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r13
call   1040 <ringbuf_filled@plt>
mov    $0x2,%r14d
cmp    $0x3ef,%eax
jg     119e <legoscanpacket+0x7e>
mov    %eax,%ebx
cmp    $0x6,%eax
jg     1164 <legoscanpacket+0x44>
test   %ebp,%ebp
setne  %al
test   %ebx,%ebx
setg   %cl
and    %al,%cl
movzbl %cl,%r14d
add    %r14d,%r14d
jmp    119e <legoscanpacket+0x7e>
mov    %r13,%rdi
xor    %esi,%esi
call   1030 <ringbuf_sneak@plt>
cmp    $0x55,%eax
jne    119e <legoscanpacket+0x7e>
mov    %r13,%rdi
mov    $0x1,%esi
call   1030 <ringbuf_sneak@plt>
cmp    $0xff,%eax
jne    119e <legoscanpacket+0x7e>
mov    $0x2,%r14d
mov    %r13,%rdi
mov    $0x2,%esi
call   1030 <ringbuf_sneak@plt>
test   %eax,%eax
je     11b0 <legoscanpacket+0x90>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,0xc(%rsp)
mov    %r15,0x10(%rsp)
add    $0xffffffff,%ebx
xor    %r12d,%r12d
mov    $0x3,%ebp
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %ebp,%esi
call   1030 <ringbuf_sneak@plt>
mov    %eax,%r14d
lea    0x1(%rbp),%esi
mov    %r13,%rdi
call   1030 <ringbuf_sneak@plt>
xor    $0xff,%eax
cmp    %eax,%r14d
jne    1219 <legoscanpacket+0xf9>
add    %r15d,%r12d
movzbl %r12b,%r15d
mov    %r13,%rdi
mov    %ebp,%esi
call   1030 <ringbuf_sneak@plt>
add    $0x2,%ebp
movzbl %al,%r12d
add    $0xfffffffe,%ebx
cmp    $0x3,%ebx
jg     11d0 <legoscanpacket+0xb0>
cmpl   $0x0,0xc(%rsp)
je     123f <legoscanpacket+0x11f>
cmp    %r12d,%r15d
jne    1247 <legoscanpacket+0x127>
cmp    $0x7,%ebp
mov    $0x2,%r14d
mov    0x10(%rsp),%rax
jb     119e <legoscanpacket+0x7e>
mov    %ebp,(%rax)
mov    $0x1,%r14d
jmp    119e <legoscanpacket+0x7e>
xor    %r14d,%r14d
jmp    119e <legoscanpacket+0x7e>
mov    $0x2,%r14d
jmp    119e <legoscanpacket+0x7e>

LLVM IR (-O0):
%struct.ringbuf = type { i32 }
define i32 @legoscanpacket(%struct.ringbuf* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.ringbuf*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
store %struct.ringbuf* %0, %struct.ringbuf** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %10
%12 = load %struct.ringbuf*, %struct.ringbuf** %5
%13 = call i32 @ringbuf_filled(%struct.ringbuf* %12)
store i32 %13, i32* %11
%14 = load i32, i32* %11
%15 = icmp sge i32 %14, 1008
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 2, i32* %4
br label %112
17: ; preds = %3
%18 = load i32, i32* %11
%19 = icmp slt i32 %18, 7
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %11
%25 = icmp sgt i32 %24, 0
br label %26
26: ; preds = %23, %20
%27 = phi i1 [ false, %20 ], [ %25, %23 ]
%28 = zext i1 %27 to i64
%29 = select i1 %27, i32 2, i32 0
store i32 %29, i32* %4
br label %112
30: ; preds = %17
%31 = load %struct.ringbuf*, %struct.ringbuf** %5
%32 = load i32, i32* %10
%33 = call i32 @ringbuf_sneak(%struct.ringbuf* %31, i32 %32)
%34 = icmp ne i32 %33, 85
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 2, i32* %4
br label %112
36: ; preds = %30
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
store i32 %38, i32* %10
br label %39
39: ; preds = %36
%40 = load %struct.ringbuf*, %struct.ringbuf** %5
%41 = load i32, i32* %10
%42 = call i32 @ringbuf_sneak(%struct.ringbuf* %40, i32 %41)
%43 = icmp ne i32 %42, 255
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 2, i32* %4
br label %112
45: ; preds = %39
%46 = load i32, i32* %10
%47 = add nsw i32 %46, 1
store i32 %47, i32* %10
br label %48
48: ; preds = %45
%49 = load %struct.ringbuf*, %struct.ringbuf** %5
%50 = load i32, i32* %10
%51 = call i32 @ringbuf_sneak(%struct.ringbuf* %49, i32 %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %48
store i32 2, i32* %4
br label %112
54: ; preds = %48
%55 = load i32, i32* %10
%56 = add nsw i32 %55, 1
store i32 %56, i32* %10
br label %57
57: ; preds = %54
%58 = load i32, i32* %10
%59 = load i32, i32* %11
%60 = sub nsw i32 %59, %58
store i32 %60, i32* %11
store i8 0, i8* %9
store i8 0, i8* %8
br label %61
61: ; preds = %76, %57
%62 = load i32, i32* %11
%63 = icmp sge i32 %62, 2
br i1 %63, label %64, label %74
64: ; preds = %61
%65 = load %struct.ringbuf*, %struct.ringbuf** %5
%66 = load i32, i32* %10
%67 = call i32 @ringbuf_sneak(%struct.ringbuf* %65, i32 %66)
%68 = load %struct.ringbuf*, %struct.ringbuf** %5
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
%71 = call i32 @ringbuf_sneak(%struct.ringbuf* %68, i32 %70)
%72 = xor i32 255, %71
%73 = icmp eq i32 %67, %72
br label %74
74: ; preds = %64, %61
%75 = phi i1 [ false, %61 ], [ %73, %64 ]
br i1 %75, label %76, label %92
76: ; preds = %74
%77 = load i8, i8* %8
%78 = zext i8 %77 to i32
%79 = load i8, i8* %9
%80 = zext i8 %79 to i32
%81 = add nsw i32 %78, %80
%82 = and i32 %81, 255
%83 = trunc i32 %82 to i8
store i8 %83, i8* %8
%84 = load %struct.ringbuf*, %struct.ringbuf** %5
%85 = load i32, i32* %10
%86 = call i32 @ringbuf_sneak(%struct.ringbuf* %84, i32 %85)
%87 = trunc i32 %86 to i8
store i8 %87, i8* %9
%88 = load i32, i32* %10
%89 = add nsw i32 %88, 2
store i32 %89, i32* %10
%90 = load i32, i32* %11
%91 = sub nsw i32 %90, 2
store i32 %91, i32* %11
br label %61
92: ; preds = %74
%93 = load i32, i32* %11
%94 = icmp slt i32 %93, 2
br i1 %94, label %95, label %99
95: ; preds = %92
%96 = load i32, i32* %7
%97 = icmp ne i32 %96, 0
br i1 %97, label %99, label %98
98: ; preds = %95
store i32 0, i32* %4
br label %112
99: ; preds = %95, %92
%100 = load i8, i8* %8
%101 = zext i8 %100 to i32
%102 = load i8, i8* %9
%103 = zext i8 %102 to i32
%104 = icmp eq i32 %101, %103
br i1 %104, label %105, label %111
105: ; preds = %99
%106 = load i32, i32* %10
%107 = icmp sge i32 %106, 7
br i1 %107, label %108, label %111
108: ; preds = %105
%109 = load i32, i32* %10
%110 = load i32*, i32** %6
store i32 %109, i32* %110
store i32 1, i32* %4
br label %112
111: ; preds = %105, %99
store i32 2, i32* %4
br label %112
112: ; preds = %111, %108, %98, %53, %44, %35, %26, %16
%113 = load i32, i32* %4
ret i32 %113
}

LLVM IR (-O2):
%struct.ringbuf = type { i32 }
define i32 @legoscanpacket(%struct.ringbuf* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = tail call i32 @ringbuf_filled(%struct.ringbuf* %0)
%5 = icmp sgt i32 %4, 1007
br i1 %5, label %52, label %6
6: ; preds = %3
%7 = icmp slt i32 %4, 7
br i1 %7, label %8, label %13
8: ; preds = %6
%9 = icmp ne i32 %2, 0
%10 = icmp sgt i32 %4, 0
%11 = select i1 %9, i1 %10, i1 false
%12 = select i1 %11, i32 2, i32 0
br label %52
13: ; preds = %6
%14 = tail call i32 @ringbuf_sneak(%struct.ringbuf* %0, i32 0)
%15 = icmp eq i32 %14, 85
br i1 %15, label %16, label %52
16: ; preds = %13
%17 = tail call i32 @ringbuf_sneak(%struct.ringbuf* %0, i32 1)
%18 = icmp eq i32 %17, 255
br i1 %18, label %19, label %52
19: ; preds = %16
%20 = tail call i32 @ringbuf_sneak(%struct.ringbuf* %0, i32 2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %52
22: ; preds = %19
%23 = add nsw i32 %4, -3
br label %24
24: ; preds = %22, %34
%25 = phi i32 [ %39, %34 ], [ %23, %22 ]
%26 = phi i32 [ %38, %34 ], [ 3, %22 ]
%27 = phi i32 [ %40, %34 ], [ 0, %22 ]
%28 = phi i32 [ %36, %34 ], [ 0, %22 ]
%29 = tail call i32 @ringbuf_sneak(%struct.ringbuf* %0, i32 %26)
%30 = add nuw nsw i32 %26, 1
%31 = tail call i32 @ringbuf_sneak(%struct.ringbuf* %0, i32 %30)
%32 = xor i32 %31, 255
%33 = icmp eq i32 %29, %32
br i1 %33, label %34, label %44
34: ; preds = %24
%35 = add nuw nsw i32 %27, %28
%36 = and i32 %35, 255
%37 = tail call i32 @ringbuf_sneak(%struct.ringbuf* %0, i32 %26)
%38 = add nuw nsw i32 %26, 2
%39 = add nsw i32 %25, -2
%40 = and i32 %37, 255
%41 = icmp sgt i32 %25, 3
br i1 %41, label %24, label %42
42: ; preds = %34
%43 = icmp eq i32 %2, 0
br i1 %43, label %52, label %44
44: ; preds = %24, %42
%45 = phi i32 [ %36, %42 ], [ %28, %24 ]
%46 = phi i32 [ %40, %42 ], [ %27, %24 ]
%47 = phi i32 [ %38, %42 ], [ %26, %24 ]
%48 = icmp eq i32 %45, %46
%49 = icmp ugt i32 %47, 6
%50 = select i1 %48, i1 %49, i1 false
br i1 %50, label %51, label %52
51: ; preds = %44
store i32 %47, i32* %1
br label %52
52: ; preds = %44, %42, %19, %16, %13, %3, %51, %8
%53 = phi i32 [ %12, %8 ], [ 1, %51 ], [ 2, %3 ], [ 2, %13 ], [ 2, %16 ], [ 2, %19 ], [ 0, %42 ], [ 2, %44 ]
ret i32 %53
}