#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 */
scalar_t__ getbits(unsigned char *, int, int *, int *);
int /*<<< orphan*/ putbits(unsigned char, unsigned char *, int, int *, int *);
void interleave_header(unsigned char *bd_header, long channels, long bytes) {
  unsigned char buffer[(8 + 1) * 32];
  int i, ch;
  int byte_ptr, bit_ptr;
  byte_ptr = 0;
  bit_ptr = 7;
  for (i = 0; i < 8 * bytes * channels; i++) {
    buffer[i] = (unsigned char)getbits(bd_header, 1, &byte_ptr, &bit_ptr);
  }
  byte_ptr = 0;
  bit_ptr = 7;
  for (i = 0; i < 8 * bytes; i++) {
    for (ch = 0; ch < channels; ch++) {
      putbits(buffer[i + ch * 8 * bytes], bd_header, 1, &byte_ptr, &bit_ptr);
    }
  }
}


x86 assembly (clang, -O0):
<interleave_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x14c(%rbp)
movl   $0x7,-0x150(%rbp)
movl   $0x0,-0x144(%rbp)
movslq -0x144(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x3,%rcx
imul   -0x10(%rbp),%rcx
cmp    %rcx,%rax
jge    11b2 <interleave_header+0x92>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
lea    -0x14c(%rbp),%rdx
lea    -0x150(%rbp),%rcx
call   1030 <getbits@plt>
mov    %al,%cl
movslq -0x144(%rbp),%rax
mov    %cl,-0x140(%rbp,%rax,1)
mov    -0x144(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x144(%rbp)
jmp    1155 <interleave_header+0x35>
movl   $0x0,-0x14c(%rbp)
movl   $0x7,-0x150(%rbp)
movl   $0x0,-0x144(%rbp)
movslq -0x144(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x3,%rcx
cmp    %rcx,%rax
jge    1271 <interleave_header+0x151>
movl   $0x0,-0x148(%rbp)
movslq -0x148(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    1258 <interleave_header+0x138>
movslq -0x144(%rbp),%rax
mov    -0x148(%rbp),%ecx
shl    $0x3,%ecx
movslq %ecx,%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    -0x140(%rbp,%rax,1),%al
mov    -0x8(%rbp),%rsi
mov    $0x1,%edx
lea    -0x14c(%rbp),%rcx
lea    -0x150(%rbp),%r8
movzbl %al,%edi
call   1040 <putbits@plt>
mov    -0x148(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x148(%rbp)
jmp    11f2 <interleave_header+0xd2>
jmp    125d <interleave_header+0x13d>
mov    -0x144(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x144(%rbp)
jmp    11d0 <interleave_header+0xb0>
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<interleave_header>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x158,%rsp
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
movl   $0x7,0x8(%rsp)
mov    %rdx,0x10(%rsp)
lea    0x0(,%rdx,8),%rbp
mov    %rbp,%r14
mov    %rsi,0x20(%rsp)
imul   %rsi,%r14
test   %r14,%r14
jle    1191 <interleave_header+0x71>
xor    %r15d,%r15d
lea    0xc(%rsp),%r12
lea    0x8(%rsp),%r13
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r13,%rcx
call   1030 <getbits@plt>
mov    %al,0x30(%rsp,%r15,1)
add    $0x1,%r15
cmp    %r15,%r14
jne    1170 <interleave_header+0x50>
movl   $0x0,0xc(%rsp)
movl   $0x7,0x8(%rsp)
cmpq   $0x0,0x10(%rsp)
jle    1232 <interleave_header+0x112>
cmp    $0x2,%rbp
mov    $0x1,%eax
cmovge %rbp,%rax
mov    %rax,0x28(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x18(%rsp)
xor    %eax,%eax
lea    0xc(%rsp),%r15
lea    0x8(%rsp),%r12
jmp    11f6 <interleave_header+0xd6>
nopw   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rax
add    $0x1,%rax
addq   $0x1,0x18(%rsp)
cmp    0x28(%rsp),%rax
je     1232 <interleave_header+0x112>
mov    %rax,0x10(%rsp)
cmpq   $0x0,0x20(%rsp)
jle    11e0 <interleave_header+0xc0>
mov    0x20(%rsp),%r13
mov    0x18(%rsp),%r14
nopl   (%rax)
movzbl (%r14),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
mov    %r12,%r8
call   1040 <putbits@plt>
add    %rbp,%r14
add    $0xffffffffffffffff,%r13
jne    1210 <interleave_header+0xf0>
jmp    11e0 <interleave_header+0xc0>
add    $0x158,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @interleave_header(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca [288 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
store i32 0, i32* %10
store i32 7, i32* %11
store i32 0, i32* %8
br label %12
12: ; preds = %27, %3
%13 = load i32, i32* %8
%14 = sext i32 %13 to i64
%15 = load i64, i64* %6
%16 = mul nsw i64 8, %15
%17 = load i64, i64* %5
%18 = mul nsw i64 %16, %17
%19 = icmp slt i64 %14, %18
br i1 %19, label %20, label %30
20: ; preds = %12
%21 = load i8*, i8** %4
%22 = call i64 @getbits(i8* %21, i32 1, i32* %10, i32* %11)
%23 = trunc i64 %22 to i8
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [288 x i8], [288 x i8]* %7, i64 0, i64 %25
store i8 %23, i8* %26
br label %27
27: ; preds = %20
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %12
30: ; preds = %12
store i32 0, i32* %10
store i32 7, i32* %11
store i32 0, i32* %8
br label %31
31: ; preds = %60, %30
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = load i64, i64* %6
%35 = mul nsw i64 8, %34
%36 = icmp slt i64 %33, %35
br i1 %36, label %37, label %63
37: ; preds = %31
store i32 0, i32* %9
br label %38
38: ; preds = %56, %37
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = load i64, i64* %5
%42 = icmp slt i64 %40, %41
br i1 %42, label %43, label %59
43: ; preds = %38
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = load i32, i32* %9
%47 = mul nsw i32 %46, 8
%48 = sext i32 %47 to i64
%49 = load i64, i64* %6
%50 = mul nsw i64 %48, %49
%51 = add nsw i64 %45, %50
%52 = getelementptr inbounds [288 x i8], [288 x i8]* %7, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = load i8*, i8** %4
%55 = call i32 @putbits(i8 zeroext %53, i8* %54, i32 1, i32* %10, i32* %11)
br label %56
56: ; preds = %43
%57 = load i32, i32* %9
%58 = add nsw i32 %57, 1
store i32 %58, i32* %9
br label %38
59: ; preds = %38
br label %60
60: ; preds = %59
%61 = load i32, i32* %8
%62 = add nsw i32 %61, 1
store i32 %62, i32* %8
br label %31
63: ; preds = %31
ret void
}

LLVM IR (-O2):
define void @interleave_header(i8* %0, i64 %1, i64 %2) {
%4 = alloca [288 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = getelementptr inbounds [288 x i8], [288 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
store i32 0, i32* %5
store i32 7, i32* %6
%10 = shl i64 %2, 3
%11 = mul i64 %10, %1
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %3, %13
%14 = phi i64 [ %18, %13 ], [ 0, %3 ]
%15 = call i64 @getbits(i8* %0, i32 1, i32* %5, i32* %6)
%16 = trunc i64 %15 to i8
%17 = getelementptr inbounds [288 x i8], [288 x i8]* %4, i64 0, i64 %14
store i8 %16, i8* %17
%18 = add nuw nsw i64 %14, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %20, label %13
20: ; preds = %13, %3
store i32 0, i32* %5
store i32 7, i32* %6
%21 = icmp sgt i64 %2, 0
br i1 %21, label %22, label %40
22: ; preds = %20
%23 = icmp sgt i64 %1, 0
%24 = call i64 @llvm.smax.i64(i64 %10, i64 1)
br label %25
25: ; preds = %22, %37
%26 = phi i64 [ 0, %22 ], [ %38, %37 ]
br i1 %23, label %27, label %37
27: ; preds = %25, %27
%28 = phi i64 [ %35, %27 ], [ 0, %25 ]
%29 = shl nsw i64 %28, 3
%30 = mul nsw i64 %29, %2
%31 = add nsw i64 %30, %26
%32 = getelementptr inbounds [288 x i8], [288 x i8]* %4, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = call i32 @putbits(i8 zeroext %33, i8* %0, i32 1, i32* %5, i32* %6)
%35 = add nuw nsw i64 %28, 1
%36 = icmp eq i64 %35, %1
br i1 %36, label %37, label %27
37: ; preds = %27, %25
%38 = add nuw nsw i64 %26, 1
%39 = icmp eq i64 %38, %24
br i1 %39, label %40, label %25
40: ; preds = %37, %20
ret void
}