#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 NetFrame {
  int dummy;
};
struct Ferret {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ FRAMERR(struct NetFrame *, char *, char *, unsigned int,
                           unsigned int);
int /*<<< orphan*/ dispatch_ethertype(struct Ferret *, struct NetFrame *,
                                      unsigned char const *, unsigned int,
                                      unsigned int, unsigned int);
int /*<<< orphan*/ process_llc_frame(struct Ferret *, struct NetFrame *,
                                     unsigned char const *, unsigned int);
void process_8021q_vlan(struct Ferret *ferret, struct NetFrame *frame,
                        const unsigned char *px, unsigned length,
                        unsigned oui) {
  unsigned ethertype = 0;
  unsigned vlan_id = 0;
  if (length < 4)
    return;
q_in_q:
  vlan_id |= (*((unsigned char *)(px + 0) + 0) << 8 |
              *((unsigned char *)(px + 0) + 1) << 0) &
             0xFFF;
  ethertype = (*((unsigned char *)(px + 2) + 0) << 8 |
               *((unsigned char *)(px + 2) + 1) << 0);
  if (ethertype <= 1536) {
    unsigned new_length = ethertype;
    px += 4;
    length -= 4;
    if (new_length > length) {
      FRAMERR(frame, "%s: unknown value: 0x%x (%d)\n", "ethertype", ethertype,
              ethertype);
      ;
      return;
    } else if (new_length < length)
      length = new_length;
    process_llc_frame(ferret, frame, px, length);
    return;
  } else if (ethertype == 0x8100 || ethertype == 0x9100 ||
             ethertype == 0x88a8) {
    px += 4;
    length -= 4;
    vlan_id <<= 12;
    goto q_in_q;
  } else {
    dispatch_ethertype(ferret, frame, px + 4, length - 4, oui, ethertype);
  }
}


x86 assembly (clang, -O0):
<process_8021q_vlan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jae    1168 <process_8021q_vlan+0x38>
jmp    12a3 <process_8021q_vlan+0x173>
jmp    116d <process_8021q_vlan+0x3d>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
shl    $0x8,%eax
mov    -0x18(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
shl    $0x0,%ecx
or     %ecx,%eax
and    $0xfff,%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movzbl 0x2(%rax),%eax
shl    $0x8,%eax
mov    -0x18(%rbp),%rcx
movzbl 0x3(%rcx),%ecx
shl    $0x0,%ecx
or     %ecx,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x600,-0x24(%rbp)
ja     1231 <process_8021q_vlan+0x101>
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jbe    1201 <process_8021q_vlan+0xd1>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    -0x24(%rbp),%r8d
lea    0xe10(%rip),%rsi
lea    0xe27(%rip),%rdx
call   1040 <FRAMERR@plt>
jmp    12a3 <process_8021q_vlan+0x173>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    1213 <process_8021q_vlan+0xe3>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1218 <process_8021q_vlan+0xe8>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1050 <process_llc_frame@plt>
jmp    12a3 <process_8021q_vlan+0x173>
cmpl   $0x8100,-0x24(%rbp)
je     1258 <process_8021q_vlan+0x128>
cmpl   $0x9100,-0x24(%rbp)
je     1258 <process_8021q_vlan+0x128>
cmpl   $0x88a8,-0x24(%rbp)
jne    127b <process_8021q_vlan+0x14b>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%eax
shl    $0xc,%eax
mov    %eax,-0x28(%rbp)
jmp    116d <process_8021q_vlan+0x3d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x4,%rdx
mov    -0x1c(%rbp),%ecx
sub    $0x4,%ecx
mov    -0x20(%rbp),%r8d
mov    -0x24(%rbp),%r9d
call   1030 <dispatch_ethertype@plt>
jmp    12a3 <process_8021q_vlan+0x173>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_8021q_vlan>:
cmp    $0x4,%ecx
jae    1136 <process_8021q_vlan+0x6>
ret
movzwl 0x2(%rdx),%r9d
shl    $0x10,%r9d
bswap  %r9d
cmp    $0x601,%r9d
jb     11a8 <process_8021q_vlan+0x78>
add    $0x4,%rdx
neg    %ecx
jmp    117c <process_8021q_vlan+0x4c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzwl 0x2(%rdx),%r9d
shl    $0x10,%r9d
bswap  %r9d
add    $0x4,%rdx
add    $0x4,%ecx
cmp    $0x601,%r9d
jb     11a2 <process_8021q_vlan+0x72>
cmp    $0x88a8,%r9d
je     1160 <process_8021q_vlan+0x30>
mov    %r9d,%eax
and    $0xefff,%eax
cmp    $0x8100,%eax
je     1160 <process_8021q_vlan+0x30>
mov    $0xfffffffc,%eax
sub    %ecx,%eax
mov    %eax,%ecx
jmp    1030 <dispatch_ethertype@plt>
add    $0xfffffffffffffffc,%rdx
neg    %ecx
add    $0xfffffffc,%ecx
cmp    %ecx,%r9d
jbe    11cf <process_8021q_vlan+0x9f>
lea    0xe49(%rip),%rax
lea    0xe60(%rip),%rdx
mov    %rsi,%rdi
mov    %rax,%rsi
mov    %r9d,%ecx
mov    %r9d,%r8d
jmp    1040 <FRAMERR@plt>
add    $0x4,%rdx
cmp    %ecx,%r9d
cmovb  %r9d,%ecx
jmp    1050 <process_llc_frame@plt>

LLVM IR (-O0):
%struct.Ferret = type { i32 }
%struct.NetFrame = type { i32 }
@.str = [30 x i8] c"%s: unknown value: 0x%x (%d)\0A\00"
@.str.1 = [10 x i8] c"ethertype\00"
define void @process_8021q_vlan(%struct.Ferret* %0, %struct.NetFrame* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca %struct.Ferret*
%7 = alloca %struct.NetFrame*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store %struct.Ferret* %0, %struct.Ferret** %6
store %struct.NetFrame* %1, %struct.NetFrame** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
%14 = load i32, i32* %9
%15 = icmp ult i32 %14, 4
br i1 %15, label %16, label %17
16: ; preds = %5
br label %104
17: ; preds = %5
br label %18
18: ; preds = %86, %17
%19 = load i8*, i8** %8
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = shl i32 %23, 8
%25 = load i8*, i8** %8
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = shl i32 %29, 0
%31 = or i32 %24, %30
%32 = and i32 %31, 4095
%33 = load i32, i32* %12
%34 = or i32 %33, %32
store i32 %34, i32* %12
%35 = load i8*, i8** %8
%36 = getelementptr inbounds i8, i8* %35, i64 2
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = shl i32 %39, 8
%41 = load i8*, i8** %8
%42 = getelementptr inbounds i8, i8* %41, i64 2
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = shl i32 %45, 0
%47 = or i32 %40, %46
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = icmp ule i32 %48, 1536
br i1 %49, label %50, label %77
50: ; preds = %18
%51 = load i32, i32* %11
store i32 %51, i32* %13
%52 = load i8*, i8** %8
%53 = getelementptr inbounds i8, i8* %52, i64 4
store i8* %53, i8** %8
%54 = load i32, i32* %9
%55 = sub i32 %54, 4
store i32 %55, i32* %9
%56 = load i32, i32* %13
%57 = load i32, i32* %9
%58 = icmp ugt i32 %56, %57
br i1 %58, label %59, label %64
59: ; preds = %50
%60 = load %struct.NetFrame*, %struct.NetFrame** %7
%61 = load i32, i32* %11
%62 = load i32, i32* %11
%63 = call i32 @FRAMERR(%struct.NetFrame* %60, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %61, i32 %62)
br label %104
64: ; preds = %50
%65 = load i32, i32* %13
%66 = load i32, i32* %9
%67 = icmp ult i32 %65, %66
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = load i32, i32* %13
store i32 %69, i32* %9
br label %70
70: ; preds = %68, %64
br label %71
71: ; preds = %70
%72 = load %struct.Ferret*, %struct.Ferret** %6
%73 = load %struct.NetFrame*, %struct.NetFrame** %7
%74 = load i8*, i8** %8
%75 = load i32, i32* %9
%76 = call i32 @process_llc_frame(%struct.Ferret* %72, %struct.NetFrame* %73, i8* %74, i32 %75)
br label %104
77: ; preds = %18
%78 = load i32, i32* %11
%79 = icmp eq i32 %78, 33024
br i1 %79, label %86, label %80
80: ; preds = %77
%81 = load i32, i32* %11
%82 = icmp eq i32 %81, 37120
br i1 %82, label %86, label %83
83: ; preds = %80
%84 = load i32, i32* %11
%85 = icmp eq i32 %84, 34984
br i1 %85, label %86, label %93
86: ; preds = %83, %80, %77
%87 = load i8*, i8** %8
%88 = getelementptr inbounds i8, i8* %87, i64 4
store i8* %88, i8** %8
%89 = load i32, i32* %9
%90 = sub i32 %89, 4
store i32 %90, i32* %9
%91 = load i32, i32* %12
%92 = shl i32 %91, 12
store i32 %92, i32* %12
br label %18
93: ; preds = %83
%94 = load %struct.Ferret*, %struct.Ferret** %6
%95 = load %struct.NetFrame*, %struct.NetFrame** %7
%96 = load i8*, i8** %8
%97 = getelementptr inbounds i8, i8* %96, i64 4
%98 = load i32, i32* %9
%99 = sub i32 %98, 4
%100 = load i32, i32* %10
%101 = load i32, i32* %11
%102 = call i32 @dispatch_ethertype(%struct.Ferret* %94, %struct.NetFrame* %95, i8* %97, i32 %99, i32 %100, i32 %101)
br label %103
103: ; preds = %93
br label %104
104: ; preds = %16, %59, %71, %103
ret void
}

LLVM IR (-O2):
%struct.Ferret = type { i32 }
%struct.NetFrame = type { i32 }
@.str = [30 x i8] c"%s: unknown value: 0x%x (%d)\0A\00"
@.str.1 = [10 x i8] c"ethertype\00"
define void @process_8021q_vlan(%struct.Ferret* %0, %struct.NetFrame* %1, i8* %2, i32 %3, i32 %4) {
%6 = icmp ult i32 %3, 4
br i1 %6, label %52, label %7
7: ; preds = %5
%8 = getelementptr inbounds i8, i8* %2, i64 2
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
%11 = shl nuw nsw i32 %10, 8
%12 = getelementptr inbounds i8, i8* %2, i64 3
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = or i32 %11, %14
%16 = icmp ult i32 %15, 1537
br i1 %16, label %17, label %30
17: ; preds = %40, %7
%18 = phi i32 [ %3, %7 ], [ %39, %40 ]
%19 = phi i8* [ %2, %7 ], [ %38, %40 ]
%20 = phi i32 [ %15, %7 ], [ %48, %40 ]
%21 = add i32 %18, -4
%22 = icmp ugt i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = tail call i32 @FRAMERR(%struct.NetFrame* %1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %20)
br label %52
25: ; preds = %17
%26 = getelementptr inbounds i8, i8* %19, i64 4
%27 = icmp ult i32 %20, %21
%28 = select i1 %27, i32 %20, i32 %21
%29 = tail call i32 @process_llc_frame(%struct.Ferret* %0, %struct.NetFrame* %1, i8* %26, i32 %28)
br label %52
30: ; preds = %7, %40
%31 = phi i32 [ %48, %40 ], [ %15, %7 ]
%32 = phi i8* [ %38, %40 ], [ %2, %7 ]
%33 = phi i32 [ %39, %40 ], [ %3, %7 ]
%34 = and i32 %31, 61439
%35 = icmp eq i32 %34, 33024
%36 = icmp eq i32 %31, 34984
%37 = or i1 %36, %35
%38 = getelementptr inbounds i8, i8* %32, i64 4
%39 = add i32 %33, -4
br i1 %37, label %40, label %50
40: ; preds = %30
%41 = getelementptr inbounds i8, i8* %32, i64 6
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = shl nuw nsw i32 %43, 8
%45 = getelementptr inbounds i8, i8* %32, i64 7
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = or i32 %44, %47
%49 = icmp ult i32 %48, 1537
br i1 %49, label %17, label %30
50: ; preds = %30
%51 = tail call i32 @dispatch_ethertype(%struct.Ferret* %0, %struct.NetFrame* %1, i8* %38, i32 %39, i32 %4, i32 %31)
br label %52
52: ; preds = %23, %25, %5, %50
ret void
}