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

/* Variables and functions */
int put_bit(struct bits_writer *, int);
int write_bits(struct bits_writer *bits, int b, int n) {
  for (int i = 0; i < n; ++i) {
    int ret = put_bit(bits, (b >> i) & 1);
    if (ret)
      return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1174 <write_bits+0x64>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%ecx
sar    %cl,%esi
and    $0x1,%esi
call   1030 <put_bit@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1161 <write_bits+0x51>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117b <write_bits+0x6b>
jmp    1166 <write_bits+0x56>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1129 <write_bits+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_bits>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    114d <write_bits+0x3d>
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%esi
mov    %ebp,%ecx
shr    %cl,%esi
and    $0x1,%esi
mov    %r15,%rdi
call   1030 <put_bit@plt>
test   %eax,%eax
jne    114f <write_bits+0x3f>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1130 <write_bits+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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