#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int fgetbit(int /*<<< orphan*/ *, int /*<<< orphan*/);
int fgetbitlen(FILE *f) {
  int lbase = 2;
  int nbit = 1;
  int len = 0;
  while (fgetbit(f, 0)) {
    lbase <<= 1;
    nbit++;
  }
  while (nbit--) {
    len |= fgetbit(f, 0);
    len <<= 1;
  }
  return lbase + (len >> 1);
}


x86 assembly (clang, -O0):
<fgetbitlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x2,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <fgetbit@plt>
cmp    $0x0,%eax
je     115c <fgetbitlen+0x4c>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1131 <fgetbitlen+0x21>
jmp    1161 <fgetbitlen+0x51>
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
cmp    $0x0,%eax
je     1194 <fgetbitlen+0x84>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <fgetbit@plt>
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1161 <fgetbitlen+0x51>
mov    -0xc(%rbp),%eax
mov    -0x14(%rbp),%ecx
sar    $0x1,%ecx
add    %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetbitlen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <fgetbit@plt>
test   %eax,%eax
je     1156 <fgetbitlen+0x46>
mov    $0x2,%r15d
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %r15d,%r15d
add    $0x1,%ebp
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <fgetbit@plt>
test   %eax,%eax
jne    1140 <fgetbitlen+0x30>
jmp    1170 <fgetbitlen+0x60>
mov    $0x1,%ebp
mov    $0x2,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <fgetbit@plt>
or     %eax,%ebx
add    %ebx,%ebx
add    $0xffffffff,%ebp
jne    1170 <fgetbitlen+0x60>
sar    %ebx
add    %r15d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fgetbitlen(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
store i32 2, i32* %3
store i32 1, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %10, %1
%7 = load i32*, i32** %2
%8 = call i32 @fgetbit(i32* %7, i32 0)
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = load i32, i32* %3
%12 = shl i32 %11, 1
store i32 %12, i32* %3
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
store i32 %14, i32* %4
br label %6
15: ; preds = %6
br label %16
16: ; preds = %20, %15
%17 = load i32, i32* %4
%18 = add nsw i32 %17, -1
store i32 %18, i32* %4
%19 = icmp ne i32 %17, 0
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = load i32*, i32** %2
%22 = call i32 @fgetbit(i32* %21, i32 0)
%23 = load i32, i32* %5
%24 = or i32 %23, %22
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = shl i32 %25, 1
store i32 %26, i32* %5
br label %16
27: ; preds = %16
%28 = load i32, i32* %3
%29 = load i32, i32* %5
%30 = ashr i32 %29, 1
%31 = add nsw i32 %28, %30
ret i32 %31
}

LLVM IR (-O2):
define i32 @fgetbitlen(i32* %0) {
%2 = tail call i32 @fgetbit(i32* %0, i32 0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %7, %1
%5 = phi i32 [ 2, %1 ], [ %10, %7 ]
%6 = phi i32 [ 1, %1 ], [ %11, %7 ]
br label %14
7: ; preds = %1, %7
%8 = phi i32 [ %11, %7 ], [ 1, %1 ]
%9 = phi i32 [ %10, %7 ], [ 2, %1 ]
%10 = shl i32 %9, 1
%11 = add nuw nsw i32 %8, 1
%12 = tail call i32 @fgetbit(i32* %0, i32 0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %4, label %7
14: ; preds = %4, %14
%15 = phi i32 [ 0, %4 ], [ %20, %14 ]
%16 = phi i32 [ %6, %4 ], [ %17, %14 ]
%17 = add nsw i32 %16, -1
%18 = tail call i32 @fgetbit(i32* %0, i32 0)
%19 = or i32 %18, %15
%20 = shl i32 %19, 1
%21 = icmp eq i32 %17, 0
br i1 %21, label %22, label %14
22: ; preds = %14
%23 = ashr exact i32 %20, 1
%24 = add nsw i32 %23, %5
ret i32 %24
}