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

/* Variables and functions */
int fls(int);
int rbsp_write_bit(struct rbsp *, int /*<<< orphan*/);
int rbsp_write_bits(struct rbsp *, int, int);
int rbsp_write_uev(struct rbsp *rbsp, unsigned int value) {
  int i;
  int ret;
  int tmp = value + 1;
  int leading_zero_bits = fls(tmp) - 1;
  for (i = 0; i < leading_zero_bits; i++) {
    ret = rbsp_write_bit(rbsp, 0);
    if (ret)
      return ret;
  }
  return rbsp_write_bits(rbsp, leading_zero_bits + 1, tmp);
}


x86 assembly (clang, -O0):
<rbsp_write_uev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <fls@plt>
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    119f <rbsp_write_uev+0x6f>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <rbsp_write_bit@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     118c <rbsp_write_uev+0x5c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b4 <rbsp_write_uev+0x84>
jmp    1191 <rbsp_write_uev+0x61>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115d <rbsp_write_uev+0x2d>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
mov    -0x20(%rbp),%edx
call   1050 <rbsp_write_bits@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rbsp_write_uev>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
add    $0x1,%r15d
mov    %r15d,%edi
call   1040 <fls@plt>
mov    %eax,%r14d
cmp    $0x2,%eax
jl     1173 <rbsp_write_uev+0x43>
lea    -0x1(%r14),%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <rbsp_write_bit@plt>
test   %eax,%eax
jne    118b <rbsp_write_uev+0x5b>
add    $0xffffffff,%ebp
jne    1160 <rbsp_write_uev+0x30>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <rbsp_write_bits@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rbsp = type { i32 }
define i32 @rbsp_write_uev(%struct.rbsp* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.rbsp*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.rbsp* %0, %struct.rbsp** %4
store i32 %1, i32* %5
%10 = load i32, i32* %5
%11 = add i32 %10, 1
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = call i32 @fls(i32 %12)
%14 = sub nsw i32 %13, 1
store i32 %14, i32* %9
store i32 0, i32* %6
br label %15
15: ; preds = %27, %2
%16 = load i32, i32* %6
%17 = load i32, i32* %9
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %30
19: ; preds = %15
%20 = load %struct.rbsp*, %struct.rbsp** %4
%21 = call i32 @rbsp_write_bit(%struct.rbsp* %20, i32 0)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = load i32, i32* %7
store i32 %25, i32* %3
br label %36
26: ; preds = %19
br label %27
27: ; preds = %26
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %15
30: ; preds = %15
%31 = load %struct.rbsp*, %struct.rbsp** %4
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
%34 = load i32, i32* %8
%35 = call i32 @rbsp_write_bits(%struct.rbsp* %31, i32 %33, i32 %34)
store i32 %35, i32* %3
br label %36
36: ; preds = %30, %24
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.rbsp = type { i32 }
define i32 @rbsp_write_uev(%struct.rbsp* %0, i32 %1) {
%3 = add i32 %1, 1
%4 = tail call i32 @fls(i32 %3)
%5 = add i32 %4, -1
%6 = icmp sgt i32 %4, 1
br i1 %6, label %9, label %14
7: ; preds = %9
%8 = icmp eq i32 %13, %5
br i1 %8, label %14, label %9
9: ; preds = %2, %7
%10 = phi i32 [ %13, %7 ], [ 0, %2 ]
%11 = tail call i32 @rbsp_write_bit(%struct.rbsp* %0, i32 0)
%12 = icmp eq i32 %11, 0
%13 = add nuw nsw i32 %10, 1
br i1 %12, label %7, label %16
14: ; preds = %7, %2
%15 = tail call i32 @rbsp_write_bits(%struct.rbsp* %0, i32 %4, i32 %3)
br label %16
16: ; preds = %9, %14
%17 = phi i32 [ %15, %14 ], [ %11, %9 ]
ret i32 %17
}