#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 APDSSTATE {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ aP_getbit_safe(struct APDSSTATE *, unsigned int *);
int aP_getgamma_safe(struct APDSSTATE *ud, unsigned int *result) {
  unsigned int bit;
  unsigned int v = 1;
  do {
    if (!aP_getbit_safe(ud, &bit)) {
      return 0;
    }
    if (v & 0x80000000) {
      return 0;
    }
    v = (v << 1) + bit;
    if (!aP_getbit_safe(ud, &bit)) {
      return 0;
    }
  } while (bit);
  *result = v;
  return 1;
}


x86 assembly (clang, -O0):
<aP_getgamma_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <aP_getbit_safe@plt>
cmp    $0x0,%eax
jne    1149 <aP_getgamma_safe+0x39>
movl   $0x0,-0x4(%rbp)
jmp    11b3 <aP_getgamma_safe+0xa3>
mov    -0x20(%rbp),%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
je     1166 <aP_getgamma_safe+0x56>
movl   $0x0,-0x4(%rbp)
jmp    11b3 <aP_getgamma_safe+0xa3>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <aP_getbit_safe@plt>
cmp    $0x0,%eax
jne    1194 <aP_getgamma_safe+0x84>
movl   $0x0,-0x4(%rbp)
jmp    11b3 <aP_getgamma_safe+0xa3>
jmp    1199 <aP_getgamma_safe+0x89>
cmpl   $0x0,-0x1c(%rbp)
jne    1127 <aP_getgamma_safe+0x17>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aP_getgamma_safe>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x1,%ebx
lea    0xc(%rsp),%r15
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <aP_getbit_safe@plt>
test   %eax,%eax
je     1167 <aP_getgamma_safe+0x57>
test   %ebx,%ebx
js     1167 <aP_getgamma_safe+0x57>
add    %ebx,%ebx
add    0xc(%rsp),%ebx
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <aP_getbit_safe@plt>
test   %eax,%eax
je     1167 <aP_getgamma_safe+0x57>
cmpl   $0x0,0xc(%rsp)
jne    1130 <aP_getgamma_safe+0x20>
mov    %ebx,(%r14)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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