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

/* Variables and functions */
int ceil_multiple(unsigned int, int);
uint32_t read_bits(const uint8_t *src, unsigned n, unsigned offset) {
  uint32_t res = 0;
  uint32_t mask = 1 << n;
  unsigned i = 0;
  mask = mask | (mask - 1);
  src += offset / 8;
  offset %= 8;
  mask <<= offset;
  for (i = 0; i < ceil_multiple(offset + n, 8) / 8; ++i) {
    res |= (((uint32_t)src[i] << (i * 8)) & mask) >> offset;
  }
  return res;
}


x86 assembly (clang, -O0):
<read_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%ecx
shr    $0x3,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
and    $0x7,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x18(%rbp),%eax
shl    %cl,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
add    -0xc(%rbp),%edi
mov    $0x8,%esi
call   1030 <ceil_multiple@plt>
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jae    11d7 <read_bits+0xc7>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x3,%ecx
shl    %cl,%eax
and    -0x18(%rbp),%eax
mov    -0x10(%rbp),%ecx
sar    %cl,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117d <read_bits+0x6d>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_bits>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,(%rsp)
mov    $0x1f,%cl
sub    %r15b,%cl
mov    $0xffffffff,%r13d
shr    %cl,%r13d
mov    %edx,%r12d
and    $0x7,%r12d
mov    %r12d,%ecx
shl    %cl,%r13d
add    %r12d,%r15d
mov    %r15d,%edi
mov    $0x8,%esi
call   1030 <ceil_multiple@plt>
add    $0x7,%eax
xor    %ebx,%ebx
cmp    $0xf,%eax
jb     11aa <read_bits+0x9a>
and    $0xfffffff8,%ebp
add    %rbp,(%rsp)
xor    %ebp,%ebp
xor    %r14d,%r14d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%rsp),%rax
mov    (%rax,%r14,8),%eax
mov    %ebp,%ecx
shl    %cl,%eax
and    %r13d,%eax
mov    %r12d,%ecx
sar    %cl,%eax
or     %eax,%ebx
add    $0x1,%r14
mov    %r15d,%edi
mov    $0x8,%esi
call   1030 <ceil_multiple@plt>
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x3,%ecx
add    $0x8,%ebp
cmp    %rcx,%r14
jb     1170 <read_bits+0x60>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_bits(i64* %0, i32 %1, i32 %2) {
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i64* %0, i64** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%10 = load i32, i32* %5
%11 = shl i32 1, %10
store i32 %11, i32* %8
store i32 0, i32* %9
%12 = load i32, i32* %8
%13 = load i32, i32* %8
%14 = sub nsw i32 %13, 1
%15 = or i32 %12, %14
store i32 %15, i32* %8
%16 = load i32, i32* %6
%17 = udiv i32 %16, 8
%18 = load i64*, i64** %4
%19 = zext i32 %17 to i64
%20 = getelementptr inbounds i64, i64* %18, i64 %19
store i64* %20, i64** %4
%21 = load i32, i32* %6
%22 = urem i32 %21, 8
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = load i32, i32* %8
%25 = shl i32 %24, %23
store i32 %25, i32* %8
store i32 0, i32* %9
br label %26
26: ; preds = %50, %3
%27 = load i32, i32* %9
%28 = load i32, i32* %6
%29 = load i32, i32* %5
%30 = add i32 %28, %29
%31 = call i32 @ceil_multiple(i32 %30, i32 8)
%32 = sdiv i32 %31, 8
%33 = icmp ult i32 %27, %32
br i1 %33, label %34, label %53
34: ; preds = %26
%35 = load i64*, i64** %4
%36 = load i32, i32* %9
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i64, i64* %35, i64 %37
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i32
%41 = load i32, i32* %9
%42 = mul i32 %41, 8
%43 = shl i32 %40, %42
%44 = load i32, i32* %8
%45 = and i32 %43, %44
%46 = load i32, i32* %6
%47 = ashr i32 %45, %46
%48 = load i32, i32* %7
%49 = or i32 %48, %47
store i32 %49, i32* %7
br label %50
50: ; preds = %34
%51 = load i32, i32* %9
%52 = add i32 %51, 1
store i32 %52, i32* %9
br label %26
53: ; preds = %26
%54 = load i32, i32* %7
ret i32 %54
}

LLVM IR (-O2):
define i32 @read_bits(i64* nocapture %0, i32 %1, i32 %2) {
%4 = sub i32 31, %1
%5 = lshr i32 -1, %4
%6 = lshr i32 %2, 3
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds i64, i64* %0, i64 %7
%9 = and i32 %2, 7
%10 = shl i32 %5, %9
%11 = add i32 %9, %1
%12 = tail call i32 @ceil_multiple(i32 %11, i32 8)
%13 = add i32 %12, 7
%14 = icmp ult i32 %13, 15
br i1 %14, label %32, label %15
15: ; preds = %3, %15
%16 = phi i64 [ %27, %15 ], [ 0, %3 ]
%17 = phi i32 [ %26, %15 ], [ 0, %3 ]
%18 = getelementptr inbounds i64, i64* %8, i64 %16
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i32
%21 = trunc i64 %16 to i32
%22 = shl i32 %21, 3
%23 = shl i32 %20, %22
%24 = and i32 %23, %10
%25 = ashr i32 %24, %9
%26 = or i32 %25, %17
%27 = add nuw nsw i64 %16, 1
%28 = tail call i32 @ceil_multiple(i32 %11, i32 8)
%29 = sdiv i32 %28, 8
%30 = zext i32 %29 to i64
%31 = icmp ult i64 %27, %30
br i1 %31, label %15, label %32
32: ; preds = %15, %3
%33 = phi i32 [ 0, %3 ], [ %26, %15 ]
ret i32 %33
}