#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_getbit_safe(struct blz_state *, unsigned int *);
int blz_getgamma_safe(struct blz_state *bs, unsigned long *result) {
  unsigned int bit;
  unsigned long v = 1;
  do {
    if (!blz_getbit_safe(bs, &bit)) {
      return 0;
    }
    if (v & 0x80000000UL) {
      return 0;
    }
    v = (v << 1) + bit;
    if (!blz_getbit_safe(bs, &bit)) {
      return 0;
    }
  } while (bit);
  *result = v;
  return 1;
}


x86 assembly (clang, -O0):
<blz_getgamma_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x1,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <blz_getbit_safe@plt>
cmp    $0x0,%eax
jne    114a <blz_getgamma_safe+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <blz_getgamma_safe+0xb3>
movabs $0x80000000,%rax
and    -0x28(%rbp),%rax
cmp    $0x0,%rax
je     116e <blz_getgamma_safe+0x5e>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <blz_getgamma_safe+0xb3>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    -0x1c(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <blz_getbit_safe@plt>
cmp    $0x0,%eax
jne    11a2 <blz_getgamma_safe+0x92>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <blz_getgamma_safe+0xb3>
jmp    11a7 <blz_getgamma_safe+0x97>
cmpl   $0x0,-0x1c(%rbp)
jne    1128 <blz_getgamma_safe+0x18>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blz_getgamma_safe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x1,%ebx
lea    0x4(%rsp),%r15
xor    %r13d,%r13d
xchg   %ax,%ax
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <blz_getbit_safe@plt>
test   %eax,%eax
je     1179 <blz_getgamma_safe+0x69>
mov    %ebx,%eax
and    $0x80000000,%eax
test   %rax,%rax
jne    1179 <blz_getgamma_safe+0x69>
mov    0x4(%rsp),%ebp
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <blz_getbit_safe@plt>
test   %eax,%eax
je     1179 <blz_getgamma_safe+0x69>
lea    0x0(,%rbx,2),%rbx
add    %rbp,%rbx
cmpl   $0x0,0x4(%rsp)
jne    1130 <blz_getgamma_safe+0x20>
mov    %rbx,(%r14)
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.blz_state = type { i32 }
define i32 @blz_getgamma_safe(%struct.blz_state* %0, i64* %1) {
%3 = alloca i32
%4 = alloca %struct.blz_state*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i64
store %struct.blz_state* %0, %struct.blz_state** %4
store i64* %1, i64** %5
store i64 1, i64* %7
br label %8
8: ; preds = %29, %2
%9 = load %struct.blz_state*, %struct.blz_state** %4
%10 = call i32 @blz_getbit_safe(%struct.blz_state* %9, i32* %6)
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %8
store i32 0, i32* %3
br label %35
13: ; preds = %8
%14 = load i64, i64* %7
%15 = and i64 %14, 2147483648
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 0, i32* %3
br label %35
18: ; preds = %13
%19 = load i64, i64* %7
%20 = shl i64 %19, 1
%21 = load i32, i32* %6
%22 = zext i32 %21 to i64
%23 = add i64 %20, %22
store i64 %23, i64* %7
%24 = load %struct.blz_state*, %struct.blz_state** %4
%25 = call i32 @blz_getbit_safe(%struct.blz_state* %24, i32* %6)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %18
store i32 0, i32* %3
br label %35
28: ; preds = %18
br label %29
29: ; preds = %28
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %8, label %32
32: ; preds = %29
%33 = load i64, i64* %7
%34 = load i64*, i64** %5
store i64 %33, i64* %34
store i32 1, i32* %3
br label %35
35: ; preds = %32, %27, %17, %12
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.blz_state = type { i32 }
define i32 @blz_getgamma_safe(%struct.blz_state* %0, i64* nocapture writeonly %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
br label %5
5: ; preds = %19, %2
%6 = phi i64 [ 1, %2 ], [ %16, %19 ]
%7 = call i32 @blz_getbit_safe(%struct.blz_state* %0, i32* %3)
%8 = icmp ne i32 %7, 0
%9 = and i64 %6, 2147483648
%10 = icmp eq i64 %9, 0
%11 = select i1 %8, i1 %10, i1 false
br i1 %11, label %12, label %23
12: ; preds = %5
%13 = shl i64 %6, 1
%14 = load i32, i32* %3
%15 = zext i32 %14 to i64
%16 = add i64 %13, %15
%17 = call i32 @blz_getbit_safe(%struct.blz_state* %0, i32* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %12
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %5
22: ; preds = %19
store i64 %16, i64* %1
br label %23
23: ; preds = %12, %5, %22
%24 = phi i32 [ 1, %22 ], [ 0, %5 ], [ 0, %12 ]
ret i32 %24
}