#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 */
char hex_char(int);
int /*<<< orphan*/ uart_puts(char *);
void uart_putx(uint32_t x, unsigned digits) {
  char buf[9];
  int idx = 7;
  buf[8] = 0;
  do {
    buf[idx--] = hex_char(x & 0xf);
    x >>= 4;
  } while (x || ((7 - idx) < digits));
  uart_puts(&buf[idx + 1]);
}


x86 assembly (clang, -O0):
<uart_putx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x7,-0x18(%rbp)
movb   $0x0,-0x9(%rbp)
mov    -0x4(%rbp),%edi
and    $0xf,%edi
call   1030 <hex_char@plt>
mov    %al,%cl
mov    -0x18(%rbp),%eax
mov    %eax,%edx
add    $0xffffffff,%edx
mov    %edx,-0x18(%rbp)
cltq
mov    %cl,-0x11(%rbp,%rax,1)
mov    -0x4(%rbp),%eax
sar    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    $0x1,%al
cmpl   $0x0,-0x4(%rbp)
mov    %al,-0x19(%rbp)
jne    1180 <uart_putx+0x60>
mov    $0x7,%eax
sub    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
setb   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1139 <uart_putx+0x19>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
lea    -0x11(%rbp),%rdi
add    %rax,%rdi
call   1040 <uart_puts@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_putx>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%r12d
lea    0xf(%rsp),%r15
movb   $0x0,0xf(%rsp)
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12d,%ebp
mov    %eax,%ebx
mov    %r12d,%edi
and    $0xf,%edi
call   1030 <hex_char@plt>
mov    %al,-0x1(%r15)
add    $0xffffffffffffffff,%r15
sar    $0x4,%r12d
lea    0x1(%rbx),%eax
cmp    $0xf,%ebp
ja     1150 <uart_putx+0x30>
cmp    %r14d,%ebx
jb     1150 <uart_putx+0x30>
mov    %r15,%rdi
call   1040 <uart_puts@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @uart_putx(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [9 x i8]
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 7, i32* %6
%7 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 8
store i8 0, i8* %7
br label %8
8: ; preds = %26, %2
%9 = load i32, i32* %3
%10 = and i32 %9, 15
%11 = call signext i8 @hex_char(i32 %10)
%12 = load i32, i32* %6
%13 = add nsw i32 %12, -1
store i32 %13, i32* %6
%14 = sext i32 %12 to i64
%15 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 %14
store i8 %11, i8* %15
%16 = load i32, i32* %3
%17 = ashr i32 %16, 4
store i32 %17, i32* %3
br label %18
18: ; preds = %8
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %18
%22 = load i32, i32* %6
%23 = sub nsw i32 7, %22
%24 = load i32, i32* %4
%25 = icmp ult i32 %23, %24
br label %26
26: ; preds = %21, %18
%27 = phi i1 [ true, %18 ], [ %25, %21 ]
br i1 %27, label %8, label %28
28: ; preds = %26
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 %31
%33 = call i32 @uart_puts(i8* %32)
ret void
}

LLVM IR (-O2):
define void @uart_putx(i32 %0, i32 %1) {
%3 = alloca [9 x i8]
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 8
store i8 0, i8* %5
br label %6
6: ; preds = %6, %2
%7 = phi i64 [ %11, %6 ], [ 7, %2 ]
%8 = phi i32 [ %13, %6 ], [ %0, %2 ]
%9 = and i32 %8, 15
%10 = tail call signext i8 @hex_char(i32 %9)
%11 = add i64 %7, -1
%12 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 %7
store i8 %10, i8* %12
%13 = ashr i32 %8, 4
%14 = icmp ugt i32 %8, 15
%15 = trunc i64 %7 to i32
%16 = sub i32 8, %15
%17 = icmp ult i32 %16, %1
%18 = select i1 %14, i1 true, i1 %17
br i1 %18, label %6, label %19
19: ; preds = %6
%20 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 %7
%21 = call i32 @uart_puts(i8* %20)
ret void
}