#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 int getbitu(unsigned char const *, int, int);
int getbits(const unsigned char *buff, int pos, int len) {
  unsigned int bits = getbitu(buff, pos, len);
  if (len <= 0 || 32 <= len || !(bits & (1u << (len - 1)))) {
    return (int)bits;
  }
  return (int)(bits | (~0u << len));
}


x86 assembly (clang, -O0):
<getbits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <getbitu@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1169 <getbits+0x59>
mov    $0x20,%eax
cmp    -0x18(%rbp),%eax
jle    1169 <getbits+0x59>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    1174 <getbits+0x64>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1188 <getbits+0x78>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%ecx
mov    $0xffffffff,%edx
shl    %cl,%edx
mov    %edx,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbits>:
push   %rbx
mov    %edx,%ebx
call   1030 <getbitu@plt>
lea    -0x20(%rbx),%ecx
cmp    $0xffffffe1,%ecx
jb     1136 <getbits+0x26>
lea    -0x1(%rbx),%ecx
movzbl %cl,%ecx
bt     %ecx,%eax
jae    1136 <getbits+0x26>
mov    $0xffffffff,%edx
mov    %ebx,%ecx
shl    %cl,%edx
or     %edx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @getbits(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = load i32, i32* %7
%12 = call i32 @getbitu(i8* %9, i32 %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %7
%14 = icmp sle i32 %13, 0
br i1 %14, label %25, label %15
15: ; preds = %3
%16 = load i32, i32* %7
%17 = icmp sle i32 32, %16
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = load i32, i32* %8
%20 = load i32, i32* %7
%21 = sub nsw i32 %20, 1
%22 = shl i32 1, %21
%23 = and i32 %19, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %18, %15, %3
%26 = load i32, i32* %8
store i32 %26, i32* %4
br label %32
27: ; preds = %18
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = shl i32 -1, %29
%31 = or i32 %28, %30
store i32 %31, i32* %4
br label %32
32: ; preds = %27, %25
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
define i32 @getbits(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 @getbitu(i8* %0, i32 %1, i32 %2)
%5 = add i32 %2, -32
%6 = icmp ult i32 %5, -31
br i1 %6, label %15, label %7
7: ; preds = %3
%8 = add nsw i32 %2, -1
%9 = shl nuw nsw i32 1, %8
%10 = and i32 %4, %9
%11 = icmp eq i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = shl nsw i32 -1, %2
%14 = or i32 %4, %13
br label %15
15: ; preds = %3, %7, %12
%16 = phi i32 [ %14, %12 ], [ %4, %7 ], [ %4, %3 ]
ret i32 %16
}