#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 */
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ uart_putc(int);
int /*<<< orphan*/ uart_puts(char *);
void uart_puthexn(uint32_t val, unsigned digits) {
  int i;
  uint32_t digit;
  uart_puts("0x");
  for (i = digits - 1; i >= 0; i--) {
    digit = (val >> (4 * i)) & 0xf;
    if (digit >= 0 && digit <= 9) {
      uart_putc(digit + '0');
    } else {
      uart_putc(digit + 'A' - 10);
    }
  }
}


x86 assembly (clang, -O0):
<uart_puthexn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    0xecb(%rip),%rdi
call   1040 <uart_puts@plt>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11a3 <uart_puthexn+0x83>
mov    -0x4(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x2,%ecx
sar    %cl,%eax
and    $0xf,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1182 <uart_puthexn+0x62>
cmpl   $0x9,-0x10(%rbp)
jg     1182 <uart_puthexn+0x62>
mov    -0x10(%rbp),%edi
add    $0x30,%edi
call   1030 <uart_putc@plt>
jmp    1190 <uart_puthexn+0x70>
mov    -0x10(%rbp),%edi
add    $0x41,%edi
sub    $0xa,%edi
call   1030 <uart_putc@plt>
jmp    1195 <uart_puthexn+0x75>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1143 <uart_puthexn+0x23>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_puthexn>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xed0(%rip),%rdi
call   1040 <uart_puts@plt>
mov    %ebp,%ebx
add    $0xffffffff,%ebx
js     1173 <uart_puthexn+0x53>
lea    -0x4(,%rbp,4),%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14d,%eax
mov    %ebp,%ecx
sar    %cl,%eax
and    $0xf,%eax
lea    0x30(%rax),%ecx
lea    0x37(%rax),%edi
cmp    $0xa,%eax
cmovb  %ecx,%edi
call   1030 <uart_putc@plt>
add    $0xfffffffc,%ebp
add    $0xffffffff,%ebx
jns    1150 <uart_puthexn+0x30>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
define void @uart_puthexn(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 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %4
%9 = sub i32 %8, 1
store i32 %9, i32* %5
br label %10
10: ; preds = %34, %2
%11 = load i32, i32* %5
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %37
13: ; preds = %10
%14 = load i32, i32* %3
%15 = load i32, i32* %5
%16 = mul nsw i32 4, %15
%17 = ashr i32 %14, %16
%18 = and i32 %17, 15
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %13
%22 = load i32, i32* %6
%23 = icmp sle i32 %22, 9
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 48
%27 = call i32 @uart_putc(i32 %26)
br label %33
28: ; preds = %21, %13
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 65
%31 = sub nsw i32 %30, 10
%32 = call i32 @uart_putc(i32 %31)
br label %33
33: ; preds = %28, %24
br label %34
34: ; preds = %33
%35 = load i32, i32* %5
%36 = add nsw i32 %35, -1
store i32 %36, i32* %5
br label %10
37: ; preds = %10
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
define void @uart_puthexn(i32 %0, i32 %1) {
%3 = tail call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = add i32 %1, -1
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %18
6: ; preds = %2, %6
%7 = phi i32 [ %16, %6 ], [ %4, %2 ]
%8 = shl nsw i32 %7, 2
%9 = ashr i32 %0, %8
%10 = and i32 %9, 15
%11 = icmp ult i32 %10, 10
%12 = or i32 %10, 48
%13 = add nuw nsw i32 %10, 55
%14 = select i1 %11, i32 %12, i32 %13
%15 = tail call i32 @uart_putc(i32 %14)
%16 = add i32 %7, -1
%17 = icmp sgt i32 %16, -1
br i1 %17, label %6, label %18
18: ; preds = %6, %2
ret void
}