#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 rbsp_write_bit(struct rbsp *, int);
int rbsp_write_bits(struct rbsp *rbsp, int num, int value) {
  int ret;
  while (num--) {
    ret = rbsp_write_bit(rbsp, (value >> num) & 1);
    if (ret)
      return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<rbsp_write_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     1167 <rbsp_write_bits+0x57>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%ecx
sar    %cl,%esi
and    $0x1,%esi
call   1030 <rbsp_write_bit@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1162 <rbsp_write_bits+0x52>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    116e <rbsp_write_bits+0x5e>
jmp    1122 <rbsp_write_bits+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rbsp_write_bits>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
add    $0xffffffff,%ebp
nop
cmp    $0xffffffff,%ebp
je     1140 <rbsp_write_bits+0x30>
mov    %r14d,%esi
mov    %ebp,%ecx
shr    %cl,%esi
and    $0x1,%esi
mov    %rbx,%rdi
call   1030 <rbsp_write_bit@plt>
add    $0xffffffff,%ebp
test   %eax,%eax
je     1120 <rbsp_write_bits+0x10>
jmp    1142 <rbsp_write_bits+0x32>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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