#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 DECODER_DATA {
  int dummy;
};

/* Variables and functions */
int next_bit(struct DECODER_DATA *);
int get_bits(struct DECODER_DATA *dec, int category) {
  unsigned int i;
  int result = 0;
  for (i = 0; i < category; i++)
    result = (result << 1) | next_bit(dec);
  if (result >= (1 << (category - 1)))
    return result;
  else
    return result - ((1 << category) - 1);
}


x86 assembly (clang, -O0):
<get_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1163 <get_bits+0x53>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <next_bit@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112d <get_bits+0x1d>
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
jl     1188 <get_bits+0x78>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119f <get_bits+0x8f>
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
sub    $0x1,%ecx
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_bits>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
xor    %r15d,%r15d
mov    $0x0,%ebp
test   %esi,%esi
je     1141 <get_bits+0x31>
mov    %rdi,%r12
xor    %ebp,%ebp
mov    %r14d,%ebx
nop
add    %ebp,%ebp
mov    %r12,%rdi
call   1030 <next_bit@plt>
or     %eax,%ebp
add    $0xffffffff,%ebx
jne    1130 <get_bits+0x20>
lea    -0x1(%r14),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    $0xffffffff,%edx
mov    %r14d,%ecx
shl    %cl,%edx
add    $0x1,%edx
cmp    %eax,%ebp
cmovl  %edx,%r15d
add    %ebp,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.DECODER_DATA = type { i32 }
define i32 @get_bits(%struct.DECODER_DATA* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.DECODER_DATA*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store %struct.DECODER_DATA* %0, %struct.DECODER_DATA** %4
store i32 %1, i32* %5
store i32 0, i32* %7
store i32 0, i32* %6
br label %8
8: ; preds = %18, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = icmp ult i32 %9, %10
br i1 %11, label %12, label %21
12: ; preds = %8
%13 = load i32, i32* %7
%14 = shl i32 %13, 1
%15 = load %struct.DECODER_DATA*, %struct.DECODER_DATA** %4
%16 = call i32 @next_bit(%struct.DECODER_DATA* %15)
%17 = or i32 %14, %16
store i32 %17, i32* %7
br label %18
18: ; preds = %12
%19 = load i32, i32* %6
%20 = add i32 %19, 1
store i32 %20, i32* %6
br label %8
21: ; preds = %8
%22 = load i32, i32* %7
%23 = load i32, i32* %5
%24 = sub nsw i32 %23, 1
%25 = shl i32 1, %24
%26 = icmp sge i32 %22, %25
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i32, i32* %7
store i32 %28, i32* %3
br label %35
29: ; preds = %21
%30 = load i32, i32* %7
%31 = load i32, i32* %5
%32 = shl i32 1, %31
%33 = sub nsw i32 %32, 1
%34 = sub nsw i32 %30, %33
store i32 %34, i32* %3
br label %35
35: ; preds = %29, %27
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.DECODER_DATA = type { i32 }
define i32 @get_bits(%struct.DECODER_DATA* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %12, label %4
4: ; preds = %2, %4
%5 = phi i32 [ %9, %4 ], [ 0, %2 ]
%6 = phi i32 [ %10, %4 ], [ 0, %2 ]
%7 = shl i32 %5, 1
%8 = tail call i32 @next_bit(%struct.DECODER_DATA* %0)
%9 = or i32 %8, %7
%10 = add nuw i32 %6, 1
%11 = icmp eq i32 %10, %1
br i1 %11, label %12, label %4
12: ; preds = %4, %2
%13 = phi i32 [ 0, %2 ], [ %9, %4 ]
%14 = add nsw i32 %1, -1
%15 = shl nuw i32 1, %14
%16 = icmp slt i32 %13, %15
%17 = shl nsw i32 -1, %1
%18 = add nsw i32 %17, 1
%19 = select i1 %16, i32 %18, i32 0
%20 = add i32 %19, %13
ret i32 %20
}