int opcode_size(unsigned int opcode) {
  if ((opcode & 0xff00) == 0)
    return 2;
  if ((opcode & 0xf800) == 0)
    return 4;
  if ((opcode & 0xf800) == 0x7800)
    return 4;
  if ((opcode & 0xf000) == 0xc000)
    return 4;
  if ((opcode & 0xf100) == 0x2000)
    return 4;
  if ((opcode & 0xfff0) == 0x30e0)
    return 4;
  if ((opcode & 0xf008) == 0x6008)
    return 4;
  if ((opcode & 0xf808) == 0x7008)
    return 4;
  opcode >>= 8;
  if (opcode == 0x0c || opcode == 0x0d || opcode == 0x31)
    return 4;
  return 2;
}


x86 assembly (clang, -O0):
<opcode_size>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xff00,%eax
cmp    $0x0,%eax
jne    1124 <opcode_size+0x24>
movl   $0x2,-0x4(%rbp)
jmp    1235 <opcode_size+0x135>
mov    -0x8(%rbp),%eax
and    $0xf800,%eax
cmp    $0x0,%eax
jne    1141 <opcode_size+0x41>
movl   $0x4,-0x4(%rbp)
jmp    1235 <opcode_size+0x135>
mov    -0x8(%rbp),%eax
and    $0xf800,%eax
cmp    $0x7800,%eax
jne    1160 <opcode_size+0x60>
movl   $0x4,-0x4(%rbp)
jmp    1235 <opcode_size+0x135>
mov    -0x8(%rbp),%eax
and    $0xf000,%eax
cmp    $0xc000,%eax
jne    117f <opcode_size+0x7f>
movl   $0x4,-0x4(%rbp)
jmp    1235 <opcode_size+0x135>
mov    -0x8(%rbp),%eax
and    $0xf100,%eax
cmp    $0x2000,%eax
jne    119e <opcode_size+0x9e>
movl   $0x4,-0x4(%rbp)
jmp    1235 <opcode_size+0x135>
mov    -0x8(%rbp),%eax
and    $0xfff0,%eax
cmp    $0x30e0,%eax
jne    11bd <opcode_size+0xbd>
movl   $0x4,-0x4(%rbp)
jmp    1235 <opcode_size+0x135>
mov    -0x8(%rbp),%eax
and    $0xf008,%eax
cmp    $0x6008,%eax
jne    11dc <opcode_size+0xdc>
movl   $0x4,-0x4(%rbp)
jmp    1235 <opcode_size+0x135>
mov    -0x8(%rbp),%eax
and    $0xf808,%eax
cmp    $0x7008,%eax
jne    11fb <opcode_size+0xfb>
movl   $0x4,-0x4(%rbp)
jmp    1235 <opcode_size+0x135>
mov    -0x8(%rbp),%eax
shr    $0x8,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0xc,-0x8(%rbp)
je     1222 <opcode_size+0x122>
cmpl   $0xd,-0x8(%rbp)
je     1222 <opcode_size+0x122>
cmpl   $0x31,-0x8(%rbp)
jne    122e <opcode_size+0x12e>
movl   $0x4,-0x4(%rbp)
jmp    1235 <opcode_size+0x135>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<opcode_size>:
test   $0xff00,%edi
je     117b <opcode_size+0x7b>
mov    $0x4,%eax
mov    %edi,%ecx
and    $0xf800,%cx
je     117a <opcode_size+0x7a>
movzwl %cx,%ecx
cmp    $0x7800,%ecx
je     117a <opcode_size+0x7a>
movd   %edi,%xmm0
pshufd $0x0,%xmm0,%xmm0
pand   0xece(%rip),%xmm0
pcmpeqd 0xed6(%rip),%xmm0
movmskps %xmm0,%ecx
test   %ecx,%ecx
jne    117a <opcode_size+0x7a>
mov    %edi,%ecx
and    $0xf100,%ecx
cmp    $0x2000,%ecx
je     117a <opcode_size+0x7a>
mov    %edi,%eax
and    $0xfffffe00,%eax
and    $0xffffff00,%edi
xor    %ecx,%ecx
cmp    $0x3100,%edi
sete   %cl
cmp    $0xc00,%eax
lea    0x2(%rcx,%rcx,1),%ecx
mov    $0x4,%eax
cmovne %ecx,%eax
ret
mov    $0x2,%eax
ret

LLVM IR (-O0):
define i32 @opcode_size(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = and i32 %4, 65280
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 2, i32* %2
br label %56
8: ; preds = %1
%9 = load i32, i32* %3
%10 = and i32 %9, 63488
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
store i32 4, i32* %2
br label %56
13: ; preds = %8
%14 = load i32, i32* %3
%15 = and i32 %14, 63488
%16 = icmp eq i32 %15, 30720
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 4, i32* %2
br label %56
18: ; preds = %13
%19 = load i32, i32* %3
%20 = and i32 %19, 61440
%21 = icmp eq i32 %20, 49152
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 4, i32* %2
br label %56
23: ; preds = %18
%24 = load i32, i32* %3
%25 = and i32 %24, 61696
%26 = icmp eq i32 %25, 8192
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 4, i32* %2
br label %56
28: ; preds = %23
%29 = load i32, i32* %3
%30 = and i32 %29, 65520
%31 = icmp eq i32 %30, 12512
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 4, i32* %2
br label %56
33: ; preds = %28
%34 = load i32, i32* %3
%35 = and i32 %34, 61448
%36 = icmp eq i32 %35, 24584
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 4, i32* %2
br label %56
38: ; preds = %33
%39 = load i32, i32* %3
%40 = and i32 %39, 63496
%41 = icmp eq i32 %40, 28680
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 4, i32* %2
br label %56
43: ; preds = %38
%44 = load i32, i32* %3
%45 = lshr i32 %44, 8
store i32 %45, i32* %3
%46 = load i32, i32* %3
%47 = icmp eq i32 %46, 12
br i1 %47, label %54, label %48
48: ; preds = %43
%49 = load i32, i32* %3
%50 = icmp eq i32 %49, 13
br i1 %50, label %54, label %51
51: ; preds = %48
%52 = load i32, i32* %3
%53 = icmp eq i32 %52, 49
br i1 %53, label %54, label %55
54: ; preds = %51, %48, %43
store i32 4, i32* %2
br label %56
55: ; preds = %51
store i32 2, i32* %2
br label %56
56: ; preds = %55, %54, %42, %37, %32, %27, %22, %17, %12, %7
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
define i32 @opcode_size(i32 %0) {
%2 = and i32 %0, 65280
%3 = icmp eq i32 %2, 0
br i1 %3, label %24, label %4
4: ; preds = %1
%5 = trunc i32 %0 to i16
%6 = and i16 %5, -2048
switch i16 %6, label %7 [
i16 0, label %24
i16 30720, label %24
]
7: ; preds = %4
%8 = and i32 %0, 61696
%9 = icmp eq i32 %8, 8192
%10 = insertelement <4 x i32> poison, i32 %0, i64 0
%11 = shufflevector <4 x i32> %10, <4 x i32> poison, <4 x i32> zeroinitializer
%12 = and <4 x i32> %11, <i32 63496, i32 61448, i32 65520, i32 61440>
%13 = icmp eq <4 x i32> %12, <i32 28680, i32 24584, i32 12512, i32 49152>
%14 = bitcast <4 x i1> %13 to i4
%15 = icmp ne i4 %14, 0
%16 = or i1 %15, %9
br i1 %16, label %24, label %17
17: ; preds = %7
%18 = and i32 %0, -512
%19 = icmp eq i32 %18, 3072
%20 = and i32 %0, -256
%21 = icmp eq i32 %20, 12544
%22 = or i1 %19, %21
%23 = select i1 %22, i32 4, i32 2
br label %24
24: ; preds = %17, %7, %4, %4, %1
%25 = phi i32 [ 2, %1 ], [ 4, %4 ], [ 4, %4 ], [ 4, %7 ], [ %23, %17 ]
ret i32 %25
}