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

/* Variables and functions */
scalar_t__ read_bit(struct state *);
unsigned int read_n10(struct state *, int);
int read_number(struct state *, unsigned int);
unsigned read_long_number(struct state *state) {
  unsigned order = read_n10(state, 6);
  if (order == 6)
    return 0;
  if (order == 0) {
    if (!read_bit(state))
      return 1;
    return read_bit(state) ? 2 : 3;
  }
  return (1 << (order + 2)) - 1 - read_number(state, order + 1);
}


x86 assembly (clang, -O0):
<read_long_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x6,%esi
call   1040 <read_n10@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x6,-0x14(%rbp)
jne    1163 <read_long_number+0x33>
movl   $0x0,-0x4(%rbp)
jmp    11dd <read_long_number+0xad>
cmpl   $0x0,-0x14(%rbp)
jne    11b1 <read_long_number+0x81>
mov    -0x10(%rbp),%rdi
call   1050 <read_bit@plt>
cmp    $0x0,%rax
jne    118c <read_long_number+0x5c>
movl   $0x1,-0x4(%rbp)
jmp    11dd <read_long_number+0xad>
mov    -0x10(%rbp),%rdi
call   1050 <read_bit@plt>
mov    %rax,%rdx
mov    $0x3,%eax
mov    $0x2,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11dd <read_long_number+0xad>
mov    -0x14(%rbp),%ecx
add    $0x2,%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1030 <read_number@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_long_number>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x6,%esi
call   1040 <read_n10@plt>
cmp    $0x6,%eax
je     116c <read_long_number+0x3c>
test   %eax,%eax
jne    1170 <read_long_number+0x40>
mov    %rbx,%rdi
call   1050 <read_bit@plt>
test   %rax,%rax
je     118d <read_long_number+0x5d>
mov    %rbx,%rdi
call   1050 <read_bit@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
or     $0x2,%ebp
jmp    1192 <read_long_number+0x62>
xor    %ebp,%ebp
jmp    1192 <read_long_number+0x62>
lea    0x2(%rax),%ecx
mov    $0xffffffff,%ebp
shl    %cl,%ebp
not    %ebp
add    $0x1,%eax
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <read_number@plt>
sub    %eax,%ebp
jmp    1192 <read_long_number+0x62>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.state = type { i32 }
define i32 @read_long_number(%struct.state* %0) {
%2 = tail call i32 @read_n10(%struct.state* %0, i32 6)
switch i32 %2, label %10 [
i32 6, label %17
i32 0, label %3
]
3: ; preds = %1
%4 = tail call i64 @read_bit(%struct.state* %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = tail call i64 @read_bit(%struct.state* %0)
%8 = icmp eq i64 %7, 0
%9 = select i1 %8, i32 3, i32 2
br label %17
10: ; preds = %1
%11 = add i32 %2, 2
%12 = shl nsw i32 -1, %11
%13 = xor i32 %12, -1
%14 = add i32 %2, 1
%15 = tail call i32 @read_number(%struct.state* %0, i32 %14)
%16 = sub nsw i32 %13, %15
br label %17
17: ; preds = %3, %1, %10, %6
%18 = phi i32 [ %9, %6 ], [ %16, %10 ], [ 0, %1 ], [ 1, %3 ]
ret i32 %18
}