#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 */

/* Variables and functions */
int bsr(unsigned int);
int /*<<< orphan*/ out_char(unsigned int);
void out_hex(unsigned value, unsigned bitlen) {
  int i;
  for (i = bsr(value | 1 << bitlen) & 0xfc; i >= 0; i -= 4) {
    unsigned a = (value >> i) & 0xf;
    if (a >= 10)
      a += 7;
    a += 0x30;
    out_char(a);
  }
}


x86 assembly (clang, -O0):
<out_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
or     %eax,%edi
call   1030 <bsr@plt>
and    $0xfc,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1194 <out_hex+0x74>
mov    -0x4(%rbp),%eax
mov    -0xc(%rbp),%ecx
shr    %cl,%eax
and    $0xf,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jb     1175 <out_hex+0x55>
mov    -0x10(%rbp),%eax
add    $0x7,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x30,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <out_char@plt>
mov    -0xc(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0xc(%rbp)
jmp    114a <out_hex+0x2a>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<out_hex>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
bts    %esi,%edi
call   1030 <bsr@plt>
mov    %eax,%ebp
and    $0xfc,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%eax
mov    %ebp,%ecx
shr    %cl,%eax
and    $0xf,%eax
lea    0x7(%rax),%edi
cmp    $0xa,%eax
cmovb  %eax,%edi
add    $0x30,%edi
call   1040 <out_char@plt>
add    $0xfffffffc,%ebp
cmp    $0xfffffffc,%ebp
jne    1140 <out_hex+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @out_hex(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = load i32, i32* %4
%9 = shl i32 1, %8
%10 = or i32 %7, %9
%11 = call i32 @bsr(i32 %10)
%12 = and i32 %11, 252
store i32 %12, i32* %5
br label %13
13: ; preds = %31, %2
%14 = load i32, i32* %5
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = load i32, i32* %3
%18 = load i32, i32* %5
%19 = lshr i32 %17, %18
%20 = and i32 %19, 15
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp uge i32 %21, 10
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* %6
%25 = add i32 %24, 7
store i32 %25, i32* %6
br label %26
26: ; preds = %23, %16
%27 = load i32, i32* %6
%28 = add i32 %27, 48
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = call i32 @out_char(i32 %29)
br label %31
31: ; preds = %26
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 4
store i32 %33, i32* %5
br label %13
34: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @out_hex(i32 %0, i32 %1) {
%3 = shl nuw i32 1, %1
%4 = or i32 %3, %0
%5 = tail call i32 @bsr(i32 %4)
%6 = and i32 %5, 252
br label %7
7: ; preds = %2, %7
%8 = phi i32 [ %6, %2 ], [ %16, %7 ]
%9 = lshr i32 %0, %8
%10 = and i32 %9, 15
%11 = icmp ugt i32 %10, 9
%12 = add nuw nsw i32 %10, 7
%13 = select i1 %11, i32 %12, i32 %10
%14 = add nuw nsw i32 %13, 48
%15 = tail call i32 @out_char(i32 %14)
%16 = add nsw i32 %8, -4
%17 = icmp eq i32 %8, 0
br i1 %17, label %18, label %7
18: ; preds = %7
ret void
}