#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 /*<<< orphan*/ serial_putc(int);
void serial_put_hex(unsigned int data, unsigned bitlen) {
  int i;
  for (i = bitlen - 4; i >= 0; i -= 4) {
    if ((data >> i) == 0) {
      serial_putc(0x30);
      continue;
    }
    unsigned char s = (data >> i) & 0xf;
    if (s < 10)
      serial_putc(0x30 + s);
    else
      serial_putc(0x61 + s - 10);
  }
}


x86 assembly (clang, -O0):
<serial_put_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     119f <serial_put_hex+0x8f>
mov    -0x4(%rbp),%eax
mov    -0xc(%rbp),%ecx
shr    %cl,%eax
cmp    $0x0,%eax
jne    1151 <serial_put_hex+0x41>
mov    $0x30,%edi
call   1030 <serial_putc@plt>
jmp    1191 <serial_put_hex+0x81>
mov    -0x4(%rbp),%eax
mov    -0xc(%rbp),%ecx
shr    %cl,%eax
and    $0xf,%eax
mov    %al,-0xd(%rbp)
movzbl -0xd(%rbp),%eax
cmp    $0xa,%eax
jge    117d <serial_put_hex+0x6d>
movzbl -0xd(%rbp),%edi
add    $0x30,%edi
call   1030 <serial_putc@plt>
jmp    118c <serial_put_hex+0x7c>
movzbl -0xd(%rbp),%edi
add    $0x61,%edi
sub    $0xa,%edi
call   1030 <serial_putc@plt>
jmp    1191 <serial_put_hex+0x81>
mov    -0xc(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0xc(%rbp)
jmp    1127 <serial_put_hex+0x17>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_put_hex>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
add    $0xfffffffc,%ebx
js     1155 <serial_put_hex+0x45>
mov    %edi,%ebp
jmp    112f <serial_put_hex+0x1f>
xchg   %ax,%ax
mov    $0x30,%edi
call   1030 <serial_putc@plt>
add    $0xfffffffc,%ebx
js     1155 <serial_put_hex+0x45>
mov    %ebp,%edi
mov    %ebx,%ecx
shr    %cl,%edi
test   %edi,%edi
je     1120 <serial_put_hex+0x10>
and    $0xf,%edi
cmp    $0x9,%edi
ja     1150 <serial_put_hex+0x40>
or     $0x30,%edi
jmp    1125 <serial_put_hex+0x15>
cs nopw 0x0(%rax,%rax,1)
add    $0x57,%edi
jmp    1125 <serial_put_hex+0x15>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @serial_put_hex(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sub i32 %7, 4
store i32 %8, i32* %5
br label %9
9: ; preds = %40, %2
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %43
12: ; preds = %9
%13 = load i32, i32* %3
%14 = load i32, i32* %5
%15 = lshr i32 %13, %14
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @serial_putc(i32 48)
br label %40
19: ; preds = %12
%20 = load i32, i32* %3
%21 = load i32, i32* %5
%22 = lshr i32 %20, %21
%23 = and i32 %22, 15
%24 = trunc i32 %23 to i8
store i8 %24, i8* %6
%25 = load i8, i8* %6
%26 = zext i8 %25 to i32
%27 = icmp slt i32 %26, 10
br i1 %27, label %28, label %33
28: ; preds = %19
%29 = load i8, i8* %6
%30 = zext i8 %29 to i32
%31 = add nsw i32 48, %30
%32 = call i32 @serial_putc(i32 %31)
br label %39
33: ; preds = %19
%34 = load i8, i8* %6
%35 = zext i8 %34 to i32
%36 = add nsw i32 97, %35
%37 = sub nsw i32 %36, 10
%38 = call i32 @serial_putc(i32 %37)
br label %39
39: ; preds = %33, %28
br label %40
40: ; preds = %39, %17
%41 = load i32, i32* %5
%42 = sub nsw i32 %41, 4
store i32 %42, i32* %5
br label %9
43: ; preds = %9
ret void
}

LLVM IR (-O2):
define void @serial_put_hex(i32 %0, i32 %1) {
%3 = add i32 %1, -4
%4 = icmp sgt i32 %3, -1
br i1 %4, label %5, label %21
5: ; preds = %2, %16
%6 = phi i32 [ %19, %16 ], [ %3, %2 ]
%7 = lshr i32 %0, %6
%8 = icmp eq i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %5
%10 = and i32 %7, 15
%11 = icmp ult i32 %10, 10
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = or i32 %10, 48
br label %16
14: ; preds = %9
%15 = add nuw nsw i32 %10, 87
br label %16
16: ; preds = %5, %12, %14
%17 = phi i32 [ %13, %12 ], [ %15, %14 ], [ 48, %5 ]
%18 = tail call i32 @serial_putc(i32 %17)
%19 = add i32 %6, -4
%20 = icmp sgt i32 %19, -1
br i1 %20, label %5, label %21
21: ; preds = %16, %2
ret void
}