#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 */
int /*<<< orphan*/ printf(char *);
int get_next_codeword(long *bits_read, unsigned char *source,
                      int codeword_size) {
  unsigned char b0, b1, b2;
  int codeword;
  b0 = source[*bits_read / 8];
  b1 = source[*bits_read / 8 + 1];
  b2 = source[*bits_read / 8 + 2];
  codeword = ((b2 << 16) + (b1 << 8) + b0);
  codeword = codeword >> (*bits_read % 8);
  switch (codeword_size) {
  case 0x9:
    codeword = codeword & 0x1ff;
    break;
  case 0xa:
    codeword = codeword & 0x3ff;
    break;
  case 0xb:
    codeword = codeword & 0x7ff;
    break;
  case 0xc:
    codeword = codeword & 0xfff;
    break;
  default:
    printf("Error: weird codeword size!\n");
    break;
  }
  *bits_read += codeword_size;
  return (codeword);
}


x86 assembly (clang, -O0):
<get_next_codeword>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rcx,%rdx
add    $0x7,%rdx
test   %rcx,%rcx
cmovs  %rdx,%rcx
sar    $0x3,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x15(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,%rdx
add    $0x7,%rdx
test   %rax,%rax
cmovs  %rdx,%rax
sar    $0x3,%rax
mov    0x1(%rax,%rcx,1),%al
mov    %al,-0x16(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,%rdx
add    $0x7,%rdx
test   %rax,%rax
cmovs  %rdx,%rax
sar    $0x3,%rax
mov    0x2(%rax,%rcx,1),%al
mov    %al,-0x17(%rbp)
movzbl -0x17(%rbp),%eax
shl    $0x10,%eax
movzbl -0x16(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x15(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x8(%rbp),%rcx
mov    (%rcx),%rdx
mov    %edx,%ecx
mov    %ecx,%esi
add    $0x7,%esi
test   %rdx,%rdx
mov    %ecx,%edx
cmovs  %esi,%edx
and    $0xf8,%edx
sub    %edx,%ecx
sar    %cl,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0xfffffff7,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x3,%eax
ja     1236 <get_next_codeword+0x126>
mov    -0x28(%rbp),%rax
lea    0xe13(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x1c(%rbp),%eax
and    $0x1ff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1242 <get_next_codeword+0x132>
mov    -0x1c(%rbp),%eax
and    $0x3ff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1242 <get_next_codeword+0x132>
mov    -0x1c(%rbp),%eax
and    $0x7ff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1242 <get_next_codeword+0x132>
mov    -0x1c(%rbp),%eax
and    $0xfff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1242 <get_next_codeword+0x132>
lea    0xdd3(%rip),%rdi
call   1030 <printf@plt>
movslq -0x14(%rbp),%rcx
mov    -0x8(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_next_codeword>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    (%rdi),%rax
lea    0x7(%rax),%rdi
test   %rax,%rax
cmovns %rax,%rdi
sar    $0x3,%rdi
movzbl 0x2(%rsi,%rdi,1),%ecx
shl    $0x10,%ecx
movzwl (%rsi,%rdi,1),%ebp
or     %ecx,%ebp
shl    $0x3,%edi
mov    %eax,%ecx
sub    %edi,%ecx
shr    %cl,%ebp
mov    %edx,%ebx
lea    -0x9(%rbx),%ecx
cmp    $0x3,%ecx
ja     1162 <get_next_codeword+0x52>
lea    0xeaf(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
and    $0x1ff,%ebp
jmp    1189 <get_next_codeword+0x79>
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%rax
jmp    1189 <get_next_codeword+0x79>
and    $0x3ff,%ebp
jmp    1189 <get_next_codeword+0x79>
and    $0x7ff,%ebp
jmp    1189 <get_next_codeword+0x79>
and    $0xfff,%ebp
movslq %ebx,%rcx
add    %rax,%rcx
mov    %rcx,(%r14)
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Error: weird codeword size!\0A\00"
define i32 @get_next_codeword(i64* %0, i8* %1, i32 %2) {
%4 = alloca i64*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
store i64* %0, i64** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
%12 = load i64*, i64** %4
%13 = load i64, i64* %12
%14 = sdiv i64 %13, 8
%15 = getelementptr inbounds i8, i8* %11, i64 %14
%16 = load i8, i8* %15
store i8 %16, i8* %7
%17 = load i8*, i8** %5
%18 = load i64*, i64** %4
%19 = load i64, i64* %18
%20 = sdiv i64 %19, 8
%21 = add nsw i64 %20, 1
%22 = getelementptr inbounds i8, i8* %17, i64 %21
%23 = load i8, i8* %22
store i8 %23, i8* %8
%24 = load i8*, i8** %5
%25 = load i64*, i64** %4
%26 = load i64, i64* %25
%27 = sdiv i64 %26, 8
%28 = add nsw i64 %27, 2
%29 = getelementptr inbounds i8, i8* %24, i64 %28
%30 = load i8, i8* %29
store i8 %30, i8* %9
%31 = load i8, i8* %9
%32 = zext i8 %31 to i32
%33 = shl i32 %32, 16
%34 = load i8, i8* %8
%35 = zext i8 %34 to i32
%36 = shl i32 %35, 8
%37 = add nsw i32 %33, %36
%38 = load i8, i8* %7
%39 = zext i8 %38 to i32
%40 = add nsw i32 %37, %39
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = load i64*, i64** %4
%43 = load i64, i64* %42
%44 = srem i64 %43, 8
%45 = trunc i64 %44 to i32
%46 = ashr i32 %41, %45
store i32 %46, i32* %10
%47 = load i32, i32* %6
switch i32 %47, label %60 [
i32 9, label %48
i32 10, label %51
i32 11, label %54
i32 12, label %57
]
48: ; preds = %3
%49 = load i32, i32* %10
%50 = and i32 %49, 511
store i32 %50, i32* %10
br label %62
51: ; preds = %3
%52 = load i32, i32* %10
%53 = and i32 %52, 1023
store i32 %53, i32* %10
br label %62
54: ; preds = %3
%55 = load i32, i32* %10
%56 = and i32 %55, 2047
store i32 %56, i32* %10
br label %62
57: ; preds = %3
%58 = load i32, i32* %10
%59 = and i32 %58, 4095
store i32 %59, i32* %10
br label %62
60: ; preds = %3
%61 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %62
62: ; preds = %60, %57, %54, %51, %48
%63 = load i32, i32* %6
%64 = sext i32 %63 to i64
%65 = load i64*, i64** %4
%66 = load i64, i64* %65
%67 = add nsw i64 %66, %64
store i64 %67, i64* %65
%68 = load i32, i32* %10
ret i32 %68
}

LLVM IR (-O2):
@str = [28 x i8] c"Error: weird codeword size!\00"
define i32 @get_next_codeword(i64* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i64, i64* %0
%5 = sdiv i64 %4, 8
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = load i8, i8* %6
%8 = add nsw i64 %5, 1
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = load i8, i8* %9
%11 = add nsw i64 %5, 2
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = shl nuw nsw i32 %14, 16
%16 = zext i8 %10 to i32
%17 = shl nuw nsw i32 %16, 8
%18 = zext i8 %7 to i32
%19 = or i32 %17, %18
%20 = or i32 %19, %15
%21 = srem i64 %4, 8
%22 = trunc i64 %21 to i32
%23 = lshr i32 %20, %22
switch i32 %2, label %32 [
i32 9, label %24
i32 10, label %26
i32 11, label %28
i32 12, label %30
]
24: ; preds = %3
%25 = and i32 %23, 511
br label %35
26: ; preds = %3
%27 = and i32 %23, 1023
br label %35
28: ; preds = %3
%29 = and i32 %23, 2047
br label %35
30: ; preds = %3
%31 = and i32 %23, 4095
br label %35
32: ; preds = %3
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%34 = load i64, i64* %0
br label %35
35: ; preds = %32, %30, %28, %26, %24
%36 = phi i64 [ %34, %32 ], [ %4, %30 ], [ %4, %28 ], [ %4, %26 ], [ %4, %24 ]
%37 = phi i32 [ %23, %32 ], [ %31, %30 ], [ %29, %28 ], [ %27, %26 ], [ %25, %24 ]
%38 = sext i32 %2 to i64
%39 = add nsw i64 %36, %38
store i64 %39, i64* %0
ret i32 %37
}