#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 */
struct blz_state {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ blz_putbit(struct blz_state *, int);
void blz_putgamma(struct blz_state *bs, unsigned long val) {
  unsigned long mask = val >> 1;
  while (mask & (mask - 1)) {
    mask &= mask - 1;
  }
  blz_putbit(bs, (val & mask) ? 1 : 0);
  while (mask >>= 1) {
    blz_putbit(bs, 1);
    blz_putbit(bs, (val & mask) ? 1 : 0);
  }
  blz_putbit(bs, 0);
}


x86 assembly (clang, -O0):
<blz_putgamma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     115a <blz_putgamma+0x4a>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
and    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    112c <blz_putgamma+0x1c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rcx
and    -0x18(%rbp),%rcx
xor    %esi,%esi
mov    $0x1,%eax
cmp    $0x0,%rcx
cmovne %eax,%esi
call   1030 <blz_putbit@plt>
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11c1 <blz_putgamma+0xb1>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1030 <blz_putbit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rcx
and    -0x18(%rbp),%rcx
xor    %esi,%esi
mov    $0x1,%eax
cmp    $0x0,%rcx
cmovne %eax,%esi
call   1030 <blz_putbit@plt>
jmp    1179 <blz_putgamma+0x69>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <blz_putbit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blz_putgamma>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rax
shr    %rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rax,%rbx
add    $0xffffffffffffffff,%rax
and    %rbx,%rax
jne    1130 <blz_putgamma+0x20>
xor    %esi,%esi
test   %r14,%rbx
setne  %sil
mov    %r15,%rdi
call   1030 <blz_putbit@plt>
cmp    $0x2,%rbx
jb     118a <blz_putgamma+0x7a>
mov    %rbx,%r12
cs nopw 0x0(%rax,%rax,1)
shr    %r12
mov    %r15,%rdi
mov    $0x1,%esi
call   1030 <blz_putbit@plt>
xor    %esi,%esi
test   %r14,%r12
setne  %sil
mov    %r15,%rdi
call   1030 <blz_putbit@plt>
cmp    $0x3,%rbx
mov    %r12,%rbx
ja     1160 <blz_putgamma+0x50>
mov    %r15,%rdi
xor    %esi,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <blz_putbit@plt>

LLVM IR (-O0):
%struct.blz_state = type { i32 }
define void @blz_putgamma(%struct.blz_state* %0, i64 %1) {
%3 = alloca %struct.blz_state*
%4 = alloca i64
%5 = alloca i64
store %struct.blz_state* %0, %struct.blz_state** %3
store i64 %1, i64* %4
%6 = load i64, i64* %4
%7 = lshr i64 %6, 1
store i64 %7, i64* %5
br label %8
8: ; preds = %14, %2
%9 = load i64, i64* %5
%10 = load i64, i64* %5
%11 = sub i64 %10, 1
%12 = and i64 %9, %11
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %8
%15 = load i64, i64* %5
%16 = sub i64 %15, 1
%17 = load i64, i64* %5
%18 = and i64 %17, %16
store i64 %18, i64* %5
br label %8
19: ; preds = %8
%20 = load %struct.blz_state*, %struct.blz_state** %3
%21 = load i64, i64* %4
%22 = load i64, i64* %5
%23 = and i64 %21, %22
%24 = icmp ne i64 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i32 1, i32 0
%27 = call i32 @blz_putbit(%struct.blz_state* %20, i32 %26)
br label %28
28: ; preds = %32, %19
%29 = load i64, i64* %5
%30 = lshr i64 %29, 1
store i64 %30, i64* %5
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %28
%33 = load %struct.blz_state*, %struct.blz_state** %3
%34 = call i32 @blz_putbit(%struct.blz_state* %33, i32 1)
%35 = load %struct.blz_state*, %struct.blz_state** %3
%36 = load i64, i64* %4
%37 = load i64, i64* %5
%38 = and i64 %36, %37
%39 = icmp ne i64 %38, 0
%40 = zext i1 %39 to i64
%41 = select i1 %39, i32 1, i32 0
%42 = call i32 @blz_putbit(%struct.blz_state* %35, i32 %41)
br label %28
43: ; preds = %28
%44 = load %struct.blz_state*, %struct.blz_state** %3
%45 = call i32 @blz_putbit(%struct.blz_state* %44, i32 0)
ret void
}

LLVM IR (-O2):
%struct.blz_state = type { i32 }
define void @blz_putgamma(%struct.blz_state* %0, i64 %1) {
%3 = lshr i64 %1, 1
br label %4
4: ; preds = %4, %2
%5 = phi i64 [ %3, %2 ], [ %7, %4 ]
%6 = add i64 %5, -1
%7 = and i64 %6, %5
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %4
9: ; preds = %4
%10 = and i64 %5, %1
%11 = icmp ne i64 %10, 0
%12 = zext i1 %11 to i32
%13 = tail call i32 @blz_putbit(%struct.blz_state* %0, i32 %12)
%14 = icmp ult i64 %5, 2
br i1 %14, label %24, label %15
15: ; preds = %9, %15
%16 = phi i64 [ %17, %15 ], [ %5, %9 ]
%17 = lshr i64 %16, 1
%18 = tail call i32 @blz_putbit(%struct.blz_state* %0, i32 1)
%19 = and i64 %17, %1
%20 = icmp ne i64 %19, 0
%21 = zext i1 %20 to i32
%22 = tail call i32 @blz_putbit(%struct.blz_state* %0, i32 %21)
%23 = icmp ult i64 %16, 4
br i1 %23, label %24, label %15
24: ; preds = %15, %9
%25 = tail call i32 @blz_putbit(%struct.blz_state* %0, i32 0)
ret void
}