#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;
typedef int uint32_t;

/* Variables and functions */
int get_hid_record_size(uint8_t *hid_report_descriptor, int size, int type) {
  uint8_t i, j = 0;
  uint8_t offset;
  int record_size[3] = {0, 0, 0};
  int nb_bits = 0, nb_items = 0;
  bool found_record_marker;
  found_record_marker = false;
  for (i = hid_report_descriptor[0] + 1; i < size; i += offset) {
    offset = (hid_report_descriptor[i] & 0x03) + 1;
    if (offset == 4)
      offset = 5;
    switch (hid_report_descriptor[i] & 0xFC) {
    case 0x74:
      nb_bits = hid_report_descriptor[i + 1];
      break;
    case 0x94:
      nb_items = 0;
      for (j = 1; j < offset; j++) {
        nb_items = ((uint32_t)hid_report_descriptor[i + j]) << (8 * (j - 1));
      }
      break;
    case 0x80:
      found_record_marker = true;
      j = 0;
      break;
    case 0x90:
      found_record_marker = true;
      j = 1;
      break;
    case 0xb0:
      found_record_marker = true;
      j = 2;
      break;
    case 0xC0:
      nb_items = 0;
      nb_bits = 0;
      break;
    default:
      continue;
    }
    if (found_record_marker) {
      found_record_marker = false;
      record_size[j] += nb_items * nb_bits;
    }
  }
  if ((type < 0x01) || (type > 0x03)) {
    return 0;
  } else {
    return (record_size[type - 0x01] + 7) / 8;
  }
}


x86 assembly (clang, -O0):
<get_hid_record_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12f6 <get_hid_record_size+0x1e6>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
and    $0x3,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x4,-0x24(%rbp)
jne    118b <get_hid_record_size+0x7b>
movl   $0x5,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
and    $0xfc,%eax
mov    %eax,-0x40(%rbp)
sub    $0x74,%eax
je     120b <get_hid_record_size+0xfb>
jmp    11ac <get_hid_record_size+0x9c>
mov    -0x40(%rbp),%eax
sub    $0x80,%eax
je     126e <get_hid_record_size+0x15e>
jmp    11bf <get_hid_record_size+0xaf>
mov    -0x40(%rbp),%eax
sub    $0x90,%eax
je     1281 <get_hid_record_size+0x171>
jmp    11d2 <get_hid_record_size+0xc2>
mov    -0x40(%rbp),%eax
sub    $0x94,%eax
je     1223 <get_hid_record_size+0x113>
jmp    11e5 <get_hid_record_size+0xd5>
mov    -0x40(%rbp),%eax
sub    $0xb0,%eax
je     1294 <get_hid_record_size+0x184>
jmp    11f8 <get_hid_record_size+0xe8>
mov    -0x40(%rbp),%eax
sub    $0xc0,%eax
je     12a7 <get_hid_record_size+0x197>
jmp    12ba <get_hid_record_size+0x1aa>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x34(%rbp)
jmp    12bf <get_hid_record_size+0x1af>
movl   $0x0,-0x38(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1269 <get_hid_record_size+0x159>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    -0x20(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
shl    $0x3,%ecx
shl    %cl,%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1231 <get_hid_record_size+0x121>
jmp    12bf <get_hid_record_size+0x1af>
movl   $0x1,-0x3c(%rbp)
movl   $0x0,-0x20(%rbp)
jmp    12bf <get_hid_record_size+0x1af>
movl   $0x1,-0x3c(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    12bf <get_hid_record_size+0x1af>
movl   $0x1,-0x3c(%rbp)
movl   $0x2,-0x20(%rbp)
jmp    12bf <get_hid_record_size+0x1af>
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
jmp    12bf <get_hid_record_size+0x1af>
jmp    12e8 <get_hid_record_size+0x1d8>
cmpl   $0x0,-0x3c(%rbp)
je     12e3 <get_hid_record_size+0x1d3>
movl   $0x0,-0x3c(%rbp)
mov    -0x38(%rbp),%ecx
imul   -0x34(%rbp),%ecx
movslq -0x20(%rbp),%rax
add    -0x30(%rbp,%rax,4),%ecx
mov    %ecx,-0x30(%rbp,%rax,4)
jmp    12e8 <get_hid_record_size+0x1d8>
mov    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    115a <get_hid_record_size+0x4a>
cmpl   $0x1,-0x18(%rbp)
jl     130a <get_hid_record_size+0x1fa>
cmpl   $0x3,-0x18(%rbp)
jle    1316 <get_hid_record_size+0x206>
movl   $0x0,-0x4(%rbp)
jmp    1330 <get_hid_record_size+0x220>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x30(%rbp,%rax,4),%eax
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hid_record_size>:
push   %rbp
push   %r14
push   %rbx
movl   $0x0,-0x8(%rsp)
movq   $0x0,-0x10(%rsp)
mov    (%rdi),%r10d
add    $0x1,%r10d
cmp    %esi,%r10d
jge    11e4 <get_hid_record_size+0xe4>
xor    %r11d,%r11d
mov    $0x5,%r8d
xor    %r9d,%r9d
jmp    1162 <get_hid_record_size+0x62>
lea    (%rax,%r10,1),%ecx
add    $0xffffffff,%ecx
movslq %ecx,%rcx
mov    (%rdi,%rcx,4),%ebp
lea    -0x8(,%rax,8),%ecx
add    $0xf8,%cl
shl    %cl,%ebp
mov    %r9d,%r14d
nop
mov    %r14d,%r9d
mov    %ebp,%r11d
add    %eax,%r10d
cmp    %esi,%r10d
jge    11e4 <get_hid_record_size+0xe4>
movslq %r10d,%rbx
mov    (%rdi,%rbx,4),%ecx
mov    %ecx,%eax
and    $0x3,%eax
add    $0x1,%eax
cmp    $0x4,%eax
cmove  %r8d,%eax
and    $0xfc,%cl
cmp    $0xaf,%cl
jg     11a0 <get_hid_record_size+0xa0>
cmp    $0x80,%cl
je     11c4 <get_hid_record_size+0xc4>
cmp    $0x90,%cl
je     11c8 <get_hid_record_size+0xc8>
cmp    $0x94,%cl
jne    1156 <get_hid_record_size+0x56>
cmp    $0x2,%eax
jae    1133 <get_hid_record_size+0x33>
mov    %r9d,%r14d
xor    %ebp,%ebp
jmp    1150 <get_hid_record_size+0x50>
nopw   0x0(%rax,%rax,1)
cmp    $0xb0,%cl
je     11cf <get_hid_record_size+0xcf>
mov    $0x0,%r14d
mov    $0x0,%ebp
cmp    $0xc0,%cl
je     1150 <get_hid_record_size+0x50>
cmp    $0x74,%cl
jne    1156 <get_hid_record_size+0x56>
mov    0x4(%rdi,%rbx,4),%r14d
mov    %r11d,%ebp
jmp    1150 <get_hid_record_size+0x50>
xor    %ecx,%ecx
jmp    11d4 <get_hid_record_size+0xd4>
mov    $0x1,%ecx
jmp    11d4 <get_hid_record_size+0xd4>
mov    $0x2,%ecx
mov    %r11d,%ebx
imul   %r9d,%ebx
add    %ebx,-0x10(%rsp,%rcx,4)
jmp    1156 <get_hid_record_size+0x56>
lea    -0x4(%rdx),%ecx
xor    %eax,%eax
cmp    $0xfffffffd,%ecx
jb     1203 <get_hid_record_size+0x103>
add    $0xffffffff,%edx
mov    -0x10(%rsp,%rdx,4),%eax
lea    0x7(%rax),%ecx
add    $0xe,%eax
test   %ecx,%ecx
cmovns %ecx,%eax
sar    $0x3,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_hid_record_size(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [3 x i32]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %9
%15 = bitcast [3 x i32]* %11 to i8*
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
%16 = load i32*, i32** %5
%17 = getelementptr inbounds i32, i32* %16, i64 0
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 1
store i32 %19, i32* %8
br label %20
20: ; preds = %88, %3
%21 = load i32, i32* %8
%22 = load i32, i32* %6
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %92
24: ; preds = %20
%25 = load i32*, i32** %5
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %28
%30 = and i32 %29, 3
%31 = add nsw i32 %30, 1
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp eq i32 %32, 4
br i1 %33, label %34, label %35
34: ; preds = %24
store i32 5, i32* %10
br label %35
35: ; preds = %34, %24
%36 = load i32*, i32** %5
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = and i32 %40, 252
switch i32 %41, label %74 [
i32 116, label %42
i32 148, label %49
i32 128, label %70
i32 144, label %71
i32 176, label %72
i32 192, label %73
]
42: ; preds = %35
%43 = load i32*, i32** %5
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %43, i64 %46
%48 = load i32, i32* %47
store i32 %48, i32* %12
br label %75
49: ; preds = %35
store i32 0, i32* %13
store i32 1, i32* %9
br label %50
50: ; preds = %66, %49
%51 = load i32, i32* %9
%52 = load i32, i32* %10
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %69
54: ; preds = %50
%55 = load i32*, i32** %5
%56 = load i32, i32* %8
%57 = load i32, i32* %9
%58 = add nsw i32 %56, %57
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %55, i64 %59
%61 = load i32, i32* %60
%62 = load i32, i32* %9
%63 = sub nsw i32 %62, 1
%64 = mul nsw i32 8, %63
%65 = shl i32 %61, %64
store i32 %65, i32* %13
br label %66
66: ; preds = %54
%67 = load i32, i32* %9
%68 = add nsw i32 %67, 1
store i32 %68, i32* %9
br label %50
69: ; preds = %50
br label %75
70: ; preds = %35
store i32 1, i32* %14
store i32 0, i32* %9
br label %75
71: ; preds = %35
store i32 1, i32* %14
store i32 1, i32* %9
br label %75
72: ; preds = %35
store i32 1, i32* %14
store i32 2, i32* %9
br label %75
73: ; preds = %35
store i32 0, i32* %13
store i32 0, i32* %12
br label %75
74: ; preds = %35
br label %88
75: ; preds = %73, %72, %71, %70, %69, %42
%76 = load i32, i32* %14
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %87
78: ; preds = %75
store i32 0, i32* %14
%79 = load i32, i32* %13
%80 = load i32, i32* %12
%81 = mul nsw i32 %79, %80
%82 = load i32, i32* %9
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 %83
%85 = load i32, i32* %84
%86 = add nsw i32 %85, %81
store i32 %86, i32* %84
br label %87
87: ; preds = %78, %75
br label %88
88: ; preds = %87, %74
%89 = load i32, i32* %10
%90 = load i32, i32* %8
%91 = add nsw i32 %90, %89
store i32 %91, i32* %8
br label %20
92: ; preds = %20
%93 = load i32, i32* %7
%94 = icmp slt i32 %93, 1
br i1 %94, label %98, label %95
95: ; preds = %92
%96 = load i32, i32* %7
%97 = icmp sgt i32 %96, 3
br i1 %97, label %98, label %99
98: ; preds = %95, %92
store i32 0, i32* %4
br label %107
99: ; preds = %95
%100 = load i32, i32* %7
%101 = sub nsw i32 %100, 1
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 %102
%104 = load i32, i32* %103
%105 = add nsw i32 %104, 7
%106 = sdiv i32 %105, 8
store i32 %106, i32* %4
br label %107
107: ; preds = %99, %98
%108 = load i32, i32* %4
ret i32 %108
}

LLVM IR (-O2):
define i32 @get_hid_record_size(i32* nocapture %0, i32 %1, i32 %2) {
%4 = alloca [3 x i32]
%5 = bitcast [3 x i32]* %4 to i8*
%6 = load i32, i32* %0
%7 = add nsw i32 %6, 1
%8 = icmp slt i32 %7, %1
br i1 %8, label %9, label %54
9: ; preds = %3, %49
%10 = phi i32 [ %51, %49 ], [ 0, %3 ]
%11 = phi i32 [ %50, %49 ], [ 0, %3 ]
%12 = phi i32 [ %52, %49 ], [ %7, %3 ]
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = and i32 %15, 3
%17 = add nuw nsw i32 %16, 1
%18 = icmp eq i32 %17, 4
%19 = select i1 %18, i32 5, i32 %17
%20 = trunc i32 %15 to i8
%21 = and i8 %20, -4
switch i8 %21, label %49 [
i8 116, label %33
i8 -108, label %22
i8 -128, label %43
i8 -112, label %38
i8 -80, label %39
i8 -64, label %40
]
22: ; preds = %9
%23 = icmp ugt i32 %19, 1
br i1 %23, label %24, label %40
24: ; preds = %22
%25 = add nsw i32 %19, -1
%26 = add nsw i32 %25, %12
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %0, i64 %27
%29 = load i32, i32* %28
%30 = shl nuw nsw i32 %25, 3
%31 = add nsw i32 %30, -8
%32 = shl i32 %29, %31
br label %40
33: ; preds = %9
%34 = add nsw i32 %12, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %0, i64 %35
%37 = load i32, i32* %36
br label %40
38: ; preds = %9
br label %43
39: ; preds = %9
br label %43
40: ; preds = %22, %24, %33, %9
%41 = phi i32 [ %37, %33 ], [ 0, %9 ], [ %11, %24 ], [ %11, %22 ]
%42 = phi i32 [ %10, %33 ], [ 0, %9 ], [ %32, %24 ], [ 0, %22 ]
br label %49
43: ; preds = %38, %39, %9
%44 = phi i64 [ 2, %39 ], [ 1, %38 ], [ 0, %9 ]
%45 = mul nsw i32 %10, %11
%46 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 %44
%47 = load i32, i32* %46
%48 = add nsw i32 %47, %45
store i32 %48, i32* %46
br label %49
49: ; preds = %40, %43, %9
%50 = phi i32 [ %11, %9 ], [ %11, %43 ], [ %41, %40 ]
%51 = phi i32 [ %10, %9 ], [ %10, %43 ], [ %42, %40 ]
%52 = add nsw i32 %19, %12
%53 = icmp slt i32 %52, %1
br i1 %53, label %9, label %54
54: ; preds = %49, %3
%55 = add i32 %2, -4
%56 = icmp ult i32 %55, -3
br i1 %56, label %64, label %57
57: ; preds = %54
%58 = add nsw i32 %2, -1
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 %59
%61 = load i32, i32* %60
%62 = add nsw i32 %61, 7
%63 = sdiv i32 %62, 8
br label %64
64: ; preds = %54, %57
%65 = phi i32 [ %63, %57 ], [ 0, %54 ]
ret i32 %65
}