#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 */

/* Variables and functions */
unsigned short getbits(char, unsigned char *, int *);
int getvalue(unsigned char *pointer, int *offset, char bitmask,
             char packet_length) {
  static unsigned short addvalue = 0;
  static unsigned short outvalue = 0;
  int loopcount;
  for (loopcount = 0; loopcount < 5; loopcount++) {
    if (((bitmask >> (4 - loopcount)) & 0x01) != 0) {
      addvalue |= (getbits(1, pointer, offset) << (0xF - loopcount));
    }
  }
  if (packet_length > 8) {
    outvalue = getbits(packet_length - 8, pointer, offset) << 8;
    outvalue |= getbits(8, pointer, offset);
  } else {
    outvalue = getbits(packet_length, pointer, offset);
  }
  outvalue &= (0xFFFF ^ (0xFFFF << packet_length));
  outvalue += addvalue;
  return outvalue;
}


x86 assembly (clang, -O0):
<getvalue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %cl,-0x11(%rbp)
mov    %al,-0x12(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x5,-0x18(%rbp)
jge    1199 <getvalue+0x89>
movsbl -0x11(%rbp),%eax
mov    $0x4,%ecx
sub    -0x18(%rbp),%ecx
sar    %cl,%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1186 <getvalue+0x76>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
call   1030 <getbits@plt>
movzwl %ax,%eax
mov    $0xf,%ecx
sub    -0x18(%rbp),%ecx
shl    %cl,%eax
mov    %eax,%ecx
movzwl 0x2ead(%rip),%eax
or     %ecx,%eax
mov    %ax,0x2ea4(%rip)
jmp    118b <getvalue+0x7b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1131 <getvalue+0x21>
movsbl -0x12(%rbp),%eax
cmp    $0x8,%eax
jle    11f4 <getvalue+0xe4>
movsbl -0x12(%rbp),%eax
sub    $0x8,%eax
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
movsbl %al,%edi
call   1030 <getbits@plt>
movzwl %ax,%eax
shl    $0x8,%eax
mov    %ax,0x2e62(%rip)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x8,%edi
call   1030 <getbits@plt>
movzwl %ax,%ecx
movzwl 0x2e46(%rip),%eax
or     %ecx,%eax
mov    %ax,0x2e3d(%rip)
jmp    120e <getvalue+0xfe>
mov    -0x12(%rbp),%al
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
movsbl %al,%edi
call   1030 <getbits@plt>
mov    %ax,0x2e1e(%rip)
movsbl -0x12(%rbp),%ecx
mov    $0xffff,%eax
shl    %cl,%eax
mov    %eax,%ecx
xor    $0xffff,%ecx
movzwl 0x2e04(%rip),%eax
and    %ecx,%eax
mov    %ax,0x2dfb(%rip)
movzwl 0x2df2(%rip),%ecx
movzwl 0x2ded(%rip),%eax
add    %ecx,%eax
mov    %ax,0x2de4(%rip)
movzwl 0x2ddd(%rip),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getvalue>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rbx
test   $0x10,%bpl
jne    1192 <getvalue+0x82>
test   $0x8,%bpl
jne    11b2 <getvalue+0xa2>
test   $0x4,%bpl
jne    11d6 <getvalue+0xc6>
test   $0x2,%bpl
jne    11fa <getvalue+0xea>
test   $0x1,%bpl
jne    121e <getvalue+0x10e>
cmp    $0x9,%r14b
jl     1242 <getvalue+0x132>
lea    -0x8(%r14),%eax
movsbl %al,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <getbits@plt>
shl    $0x8,%eax
mov    %ax,0x2eb6(%rip)
mov    $0x8,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <getbits@plt>
or     0x2e9f(%rip),%ax
jmp    1251 <getvalue+0x141>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <getbits@plt>
shl    $0xf,%eax
or     %ax,0x2e7e(%rip)
test   $0x8,%bpl
je     1132 <getvalue+0x22>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <getbits@plt>
shl    $0xe,%eax
or     %ax,0x2e5e(%rip)
test   $0x4,%bpl
je     113c <getvalue+0x2c>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <getbits@plt>
shl    $0xd,%eax
or     %ax,0x2e3a(%rip)
test   $0x2,%bpl
je     1146 <getvalue+0x36>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <getbits@plt>
shl    $0xc,%eax
or     %ax,0x2e16(%rip)
test   $0x1,%bpl
je     1150 <getvalue+0x40>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <getbits@plt>
shl    $0xb,%eax
or     %ax,0x2df2(%rip)
cmp    $0x9,%r14b
jge    115a <getvalue+0x4a>
movsbl %r14b,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <getbits@plt>
mov    $0xffff,%edx
mov    %r14d,%ecx
shl    %cl,%edx
not    %edx
and    %edx,%eax
add    0x2dc4(%rip),%ax
mov    %ax,0x2dbf(%rip)
movzwl %ax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@getvalue.addvalue = internal global i16 0
@getvalue.outvalue = internal global i16 0
define i32 @getvalue(i8* %0, i32* %1, i8 signext %2, i8 signext %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8
%8 = alloca i8
%9 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i8 %2, i8* %7
store i8 %3, i8* %8
store i32 0, i32* %9
br label %10
10: ; preds = %34, %4
%11 = load i32, i32* %9
%12 = icmp slt i32 %11, 5
br i1 %12, label %13, label %37
13: ; preds = %10
%14 = load i8, i8* %7
%15 = sext i8 %14 to i32
%16 = load i32, i32* %9
%17 = sub nsw i32 4, %16
%18 = ashr i32 %15, %17
%19 = and i32 %18, 1
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %13
%22 = load i8*, i8** %5
%23 = load i32*, i32** %6
%24 = call zeroext i16 @getbits(i8 signext 1, i8* %22, i32* %23)
%25 = zext i16 %24 to i32
%26 = load i32, i32* %9
%27 = sub nsw i32 15, %26
%28 = shl i32 %25, %27
%29 = load i16, i16* @getvalue.addvalue
%30 = zext i16 %29 to i32
%31 = or i32 %30, %28
%32 = trunc i32 %31 to i16
store i16 %32, i16* @getvalue.addvalue
br label %33
33: ; preds = %21, %13
br label %34
34: ; preds = %33
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
store i32 %36, i32* %9
br label %10
37: ; preds = %10
%38 = load i8, i8* %8
%39 = sext i8 %38 to i32
%40 = icmp sgt i32 %39, 8
br i1 %40, label %41, label %60
41: ; preds = %37
%42 = load i8, i8* %8
%43 = sext i8 %42 to i32
%44 = sub nsw i32 %43, 8
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %5
%47 = load i32*, i32** %6
%48 = call zeroext i16 @getbits(i8 signext %45, i8* %46, i32* %47)
%49 = zext i16 %48 to i32
%50 = shl i32 %49, 8
%51 = trunc i32 %50 to i16
store i16 %51, i16* @getvalue.outvalue
%52 = load i8*, i8** %5
%53 = load i32*, i32** %6
%54 = call zeroext i16 @getbits(i8 signext 8, i8* %52, i32* %53)
%55 = zext i16 %54 to i32
%56 = load i16, i16* @getvalue.outvalue
%57 = zext i16 %56 to i32
%58 = or i32 %57, %55
%59 = trunc i32 %58 to i16
store i16 %59, i16* @getvalue.outvalue
br label %65
60: ; preds = %37
%61 = load i8, i8* %8
%62 = load i8*, i8** %5
%63 = load i32*, i32** %6
%64 = call zeroext i16 @getbits(i8 signext %61, i8* %62, i32* %63)
store i16 %64, i16* @getvalue.outvalue
br label %65
65: ; preds = %60, %41
%66 = load i8, i8* %8
%67 = sext i8 %66 to i32
%68 = shl i32 65535, %67
%69 = xor i32 65535, %68
%70 = load i16, i16* @getvalue.outvalue
%71 = zext i16 %70 to i32
%72 = and i32 %71, %69
%73 = trunc i32 %72 to i16
store i16 %73, i16* @getvalue.outvalue
%74 = load i16, i16* @getvalue.addvalue
%75 = zext i16 %74 to i32
%76 = load i16, i16* @getvalue.outvalue
%77 = zext i16 %76 to i32
%78 = add nsw i32 %77, %75
%79 = trunc i32 %78 to i16
store i16 %79, i16* @getvalue.outvalue
%80 = load i16, i16* @getvalue.outvalue
%81 = zext i16 %80 to i32
ret i32 %81
}

LLVM IR (-O2):
@getvalue.addvalue = internal unnamed_addr global i16 0
@getvalue.outvalue = internal unnamed_addr global i16 0
define i32 @getvalue(i8* %0, i32* %1, i8 signext %2, i8 signext %3) {
%5 = zext i8 %2 to i32
%6 = and i32 %5, 16
%7 = icmp eq i32 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = tail call zeroext i16 @getbits(i8 signext 1, i8* %0, i32* %1)
%10 = shl i16 %9, 15
%11 = load i16, i16* @getvalue.addvalue
%12 = or i16 %11, %10
store i16 %12, i16* @getvalue.addvalue
br label %13
13: ; preds = %4, %8
%14 = and i32 %5, 8
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = tail call zeroext i16 @getbits(i8 signext 1, i8* %0, i32* %1)
%18 = shl i16 %17, 14
%19 = load i16, i16* @getvalue.addvalue
%20 = or i16 %19, %18
store i16 %20, i16* @getvalue.addvalue
br label %21
21: ; preds = %16, %13
%22 = and i32 %5, 4
%23 = icmp eq i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = tail call zeroext i16 @getbits(i8 signext 1, i8* %0, i32* %1)
%26 = shl i16 %25, 13
%27 = load i16, i16* @getvalue.addvalue
%28 = or i16 %27, %26
store i16 %28, i16* @getvalue.addvalue
br label %29
29: ; preds = %24, %21
%30 = and i32 %5, 2
%31 = icmp eq i32 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %29
%33 = tail call zeroext i16 @getbits(i8 signext 1, i8* %0, i32* %1)
%34 = shl i16 %33, 12
%35 = load i16, i16* @getvalue.addvalue
%36 = or i16 %35, %34
store i16 %36, i16* @getvalue.addvalue
br label %37
37: ; preds = %32, %29
%38 = and i32 %5, 1
%39 = icmp eq i32 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %37
%41 = tail call zeroext i16 @getbits(i8 signext 1, i8* %0, i32* %1)
%42 = shl i16 %41, 11
%43 = load i16, i16* @getvalue.addvalue
%44 = or i16 %43, %42
store i16 %44, i16* @getvalue.addvalue
br label %45
45: ; preds = %40, %37
%46 = icmp sgt i8 %3, 8
br i1 %46, label %47, label %54
47: ; preds = %45
%48 = add nsw i8 %3, -8
%49 = tail call zeroext i16 @getbits(i8 signext %48, i8* %0, i32* %1)
%50 = shl i16 %49, 8
store i16 %50, i16* @getvalue.outvalue
%51 = tail call zeroext i16 @getbits(i8 signext 8, i8* %0, i32* %1)
%52 = load i16, i16* @getvalue.outvalue
%53 = or i16 %52, %51
br label %56
54: ; preds = %45
%55 = tail call zeroext i16 @getbits(i8 signext %3, i8* %0, i32* %1)
br label %56
56: ; preds = %54, %47
%57 = phi i16 [ %55, %54 ], [ %53, %47 ]
%58 = zext i8 %3 to i32
%59 = shl i32 65535, %58
%60 = trunc i32 %59 to i16
%61 = xor i16 %60, -1
%62 = and i16 %57, %61
%63 = load i16, i16* @getvalue.addvalue
%64 = add i16 %63, %62
store i16 %64, i16* @getvalue.outvalue
%65 = zext i16 %64 to i32
ret i32 %65
}