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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ memcpy(unsigned int *, char *, int);
void bitstream_read_uint32_from_bitoffset(char **b, uint32_t *val_out,
                                          const unsigned int nbits,
                                          unsigned int *bit_pos) {
  unsigned int bit_offset, int_offset;
  assert(nbits <= 32);
  int_offset = (*bit_pos - *bit_pos % 32) / 32;
  bit_offset = *bit_pos % 32;
  memcpy(val_out, *b + int_offset * sizeof(uint32_t), sizeof(uint32_t));
  *val_out <<= bit_offset;
  *val_out >>= 32 - nbits;
  if (32 < bit_offset + nbits) {
    int nbits2;
    uint32_t val;
    nbits2 = (bit_offset + nbits) % 32;
    memcpy(&val, *b + (int_offset + 1) * sizeof(uint32_t), sizeof(uint32_t));
    *val_out |= val >> (32 - nbits2);
  }
  *bit_pos += nbits;
}


x86 assembly (clang, -O0):
<bitstream_read_uint32_from_bitoffset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0x20,-0x14(%rbp)
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
and    $0x1f,%ecx
sub    %ecx,%eax
shr    $0x5,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
and    $0x1f,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%eax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
shl    %cl,%edx
mov    %edx,%ecx
mov    %ecx,(%rax)
mov    $0x20,%ecx
sub    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
shr    %cl,%edx
mov    %edx,%ecx
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
add    -0x14(%rbp),%ecx
mov    $0x20,%eax
cmp    %ecx,%eax
jae    1207 <bitstream_read_uint32_from_bitoffset+0xe7>
mov    -0x24(%rbp),%eax
add    -0x14(%rbp),%eax
and    $0x1f,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x2,%rax
add    %rax,%rsi
lea    -0x30(%rbp),%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%eax
mov    $0x20,%ecx
sub    -0x2c(%rbp),%ecx
shr    %cl,%eax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitstream_read_uint32_from_bitoffset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%r12
xor    %edi,%edi
cmp    $0x21,%edx
setb   %dil
call   1030 <assert@plt>
mov    (%r14),%ebp
mov    %ebp,%ebx
and    $0x1f,%ebx
shr    $0x3,%rbp
and    $0x1ffffffc,%ebp
mov    (%r12),%rsi
add    %rbp,%rsi
mov    %r15,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    (%r15),%eax
mov    %ebx,%ecx
shl    %cl,%eax
mov    %r13d,%ecx
neg    %cl
shr    %cl,%eax
mov    %eax,(%r15)
add    %r13d,%ebx
cmp    $0x21,%ebx
jb     11b1 <bitstream_read_uint32_from_bitoffset+0x91>
mov    (%r12),%rax
mov    %ebp,%ecx
lea    (%rcx,%rax,1),%rsi
add    $0x4,%rsi
lea    0x4(%rsp),%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x4(%rsp),%eax
and    $0x1f,%bl
neg    %bl
mov    %ebx,%ecx
shr    %cl,%eax
or     %eax,(%r15)
add    %r13d,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @bitstream_read_uint32_from_bitoffset(i8** %0, i32* %1, i32 %2, i32* %3) {
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%13 = load i32, i32* %7
%14 = icmp ule i32 %13, 32
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i32*, i32** %8
%18 = load i32, i32* %17
%19 = load i32*, i32** %8
%20 = load i32, i32* %19
%21 = urem i32 %20, 32
%22 = sub i32 %18, %21
%23 = udiv i32 %22, 32
store i32 %23, i32* %10
%24 = load i32*, i32** %8
%25 = load i32, i32* %24
%26 = urem i32 %25, 32
store i32 %26, i32* %9
%27 = load i32*, i32** %6
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = load i32, i32* %10
%31 = zext i32 %30 to i64
%32 = mul i64 %31, 4
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = call i32 @memcpy(i32* %27, i8* %33, i32 4)
%35 = load i32, i32* %9
%36 = load i32*, i32** %6
%37 = load i32, i32* %36
%38 = shl i32 %37, %35
store i32 %38, i32* %36
%39 = load i32, i32* %7
%40 = sub i32 32, %39
%41 = load i32*, i32** %6
%42 = load i32, i32* %41
%43 = lshr i32 %42, %40
store i32 %43, i32* %41
%44 = load i32, i32* %9
%45 = load i32, i32* %7
%46 = add i32 %44, %45
%47 = icmp ult i32 32, %46
br i1 %47, label %48, label %68
48: ; preds = %4
%49 = load i32, i32* %9
%50 = load i32, i32* %7
%51 = add i32 %49, %50
%52 = urem i32 %51, 32
store i32 %52, i32* %11
%53 = load i8**, i8*** %5
%54 = load i8*, i8** %53
%55 = load i32, i32* %10
%56 = add i32 %55, 1
%57 = zext i32 %56 to i64
%58 = mul i64 %57, 4
%59 = getelementptr inbounds i8, i8* %54, i64 %58
%60 = call i32 @memcpy(i32* %12, i8* %59, i32 4)
%61 = load i32, i32* %12
%62 = load i32, i32* %11
%63 = sub nsw i32 32, %62
%64 = lshr i32 %61, %63
%65 = load i32*, i32** %6
%66 = load i32, i32* %65
%67 = or i32 %66, %64
store i32 %67, i32* %65
br label %68
68: ; preds = %48, %4
%69 = load i32, i32* %7
%70 = load i32*, i32** %8
%71 = load i32, i32* %70
%72 = add i32 %71, %69
store i32 %72, i32* %70
ret void
}

LLVM IR (-O2):
define void @bitstream_read_uint32_from_bitoffset(i8** nocapture %0, i32* %1, i32 %2, i32* nocapture %3) {
%5 = alloca i32
%6 = icmp ult i32 %2, 33
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i32, i32* %3
%10 = and i32 %9, 31
%11 = load i8*, i8** %0
%12 = lshr i32 %9, 3
%13 = and i32 %12, 536870908
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
%16 = tail call i32 @memcpy(i32* %1, i8* %15, i32 4)
%17 = load i32, i32* %1
%18 = shl i32 %17, %10
%19 = sub i32 32, %2
%20 = lshr i32 %18, %19
store i32 %20, i32* %1
%21 = add i32 %10, %2
%22 = icmp ugt i32 %21, 32
br i1 %22, label %23, label %36
23: ; preds = %4
%24 = bitcast i32* %5 to i8*
%25 = and i32 %21, 31
%26 = load i8*, i8** %0
%27 = add nuw nsw i32 %13, 4
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = call i32 @memcpy(i32* %5, i8* %29, i32 4)
%31 = load i32, i32* %5
%32 = sub nuw nsw i32 32, %25
%33 = lshr i32 %31, %32
%34 = load i32, i32* %1
%35 = or i32 %34, %33
store i32 %35, i32* %1
br label %36
36: ; preds = %23, %4
%37 = load i32, i32* %3
%38 = add i32 %37, %2
store i32 %38, i32* %3
ret void
}