int int_to_str_hex(unsigned int *pval, char *s) {
  unsigned int v = *pval;
  int i;
  s[0] = '0';
  s[1] = 'x';
  for (i = 0; i < 8; i++) {
    int n = (v & 0xF0000000) >> 28;
    s[i + 2] = n + ((n < 10) ? '0' : 'A' - 10);
    v <<= 4;
  }
  return i + 2;
}


x86 assembly (clang, -O0):
<int_to_str_hex>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x78,0x1(%rax)
movl   $0x0,-0x18(%rbp)
cmpl   $0x8,-0x18(%rbp)
jge    1184 <int_to_str_hex+0x84>
mov    -0x14(%rbp),%eax
and    $0xf0000000,%eax
shr    $0x1c,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x1c(%rbp),%esi
mov    $0x37,%ecx
mov    $0x30,%edx
cmp    $0xa,%esi
cmovl  %edx,%ecx
add    %ecx,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112b <int_to_str_hex+0x2b>
mov    -0x18(%rbp),%eax
add    $0x2,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<int_to_str_hex>:
mov    (%rdi),%eax
movw   $0x7830,(%rsi)
mov    %eax,%ecx
shl    $0x4,%ecx
movd   %eax,%xmm0
pshufd $0x50,%xmm0,%xmm1
movdqa %xmm0,%xmm2
pslld  $0x8,%xmm2
pslld  $0xc,%xmm1
pshufd $0x55,%xmm1,%xmm1
punpckldq %xmm1,%xmm2
movd   %ecx,%xmm1
punpckldq %xmm1,%xmm0
punpcklqdq %xmm2,%xmm0
movdqa %xmm0,%xmm1
psrld  $0x1c,%xmm1
pxor   0xeb7(%rip),%xmm0
movdqa 0xebf(%rip),%xmm2
pcmpgtd %xmm0,%xmm2
movdqa %xmm2,%xmm0
pandn  0xebf(%rip),%xmm0
pand   0xec7(%rip),%xmm2
por    %xmm0,%xmm2
paddd  %xmm1,%xmm2
packuswb %xmm2,%xmm2
packuswb %xmm2,%xmm2
movd   %xmm2,0x2(%rsi)
mov    %eax,%ecx
shl    $0x10,%ecx
mov    %ecx,%edx
shr    $0x1c,%edx
lea    0x37(%rdx),%edi
or     $0x30,%edx
cmp    $0xa0000000,%ecx
cmovae %edi,%edx
mov    %dl,0x6(%rsi)
mov    %eax,%ecx
shl    $0x14,%ecx
mov    %ecx,%edx
shr    $0x1c,%edx
lea    0x37(%rdx),%edi
or     $0x30,%edx
cmp    $0xa0000000,%ecx
cmovae %edi,%edx
mov    %dl,0x7(%rsi)
mov    %eax,%ecx
shl    $0x18,%ecx
mov    %ecx,%edx
shr    $0x1c,%edx
lea    0x37(%rdx),%edi
or     $0x30,%edx
cmp    $0xa0000000,%ecx
cmovae %edi,%edx
mov    %dl,0x8(%rsi)
mov    %eax,%ecx
shl    $0x1c,%ecx
and    $0xf,%eax
lea    0x37(%rax),%edx
or     $0x30,%eax
cmp    $0xa0000000,%ecx
cmovae %edx,%eax
mov    %al,0x9(%rsi)
mov    $0xa,%eax
ret

LLVM IR (-O0):
define i32 @int_to_str_hex(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
%8 = load i32*, i32** %3
%9 = load i32, i32* %8
store i32 %9, i32* %5
%10 = load i8*, i8** %4
%11 = getelementptr inbounds i8, i8* %10, i64 0
store i8 48, i8* %11
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 1
store i8 120, i8* %13
store i32 0, i32* %6
br label %14
14: ; preds = %35, %2
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 8
br i1 %16, label %17, label %38
17: ; preds = %14
%18 = load i32, i32* %5
%19 = and i32 %18, -268435456
%20 = lshr i32 %19, 28
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 10
%24 = zext i1 %23 to i64
%25 = select i1 %23, i32 48, i32 55
%26 = add nsw i32 %21, %25
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** %4
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 %27, i8* %32
%33 = load i32, i32* %5
%34 = shl i32 %33, 4
store i32 %34, i32* %5
br label %35
35: ; preds = %17
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %14
38: ; preds = %14
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 2
ret i32 %40
}

LLVM IR (-O2):
define i32 @int_to_str_hex(i32* nocapture %0, i8* nocapture writeonly %1) {
%3 = load i32, i32* %0
store i8 48, i8* %1
%4 = getelementptr inbounds i8, i8* %1, i64 1
store i8 120, i8* %4
%5 = getelementptr inbounds i8, i8* %1, i64 2
%6 = shl i32 %3, 4
%7 = insertelement <2 x i32> poison, i32 %3, i64 0
%8 = shufflevector <2 x i32> %7, <2 x i32> poison, <2 x i32> zeroinitializer
%9 = shl <2 x i32> %8, <i32 8, i32 12>
%10 = insertelement <4 x i32> poison, i32 %3, i64 0
%11 = insertelement <4 x i32> %10, i32 %6, i64 1
%12 = shufflevector <2 x i32> %9, <2 x i32> poison, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
%13 = shufflevector <4 x i32> %11, <4 x i32> %12, <4 x i32> <i32 0, i32 1, i32 4, i32 5>
%14 = lshr <4 x i32> %13, <i32 28, i32 28, i32 28, i32 28>
%15 = icmp ult <4 x i32> %13, <i32 -1610612736, i32 -1610612736, i32 -1610612736, i32 -1610612736>
%16 = select <4 x i1> %15, <4 x i32> <i32 48, i32 48, i32 48, i32 48>, <4 x i32> <i32 55, i32 55, i32 55, i32 55>
%17 = add nuw nsw <4 x i32> %16, %14
%18 = trunc <4 x i32> %17 to <4 x i8>
%19 = bitcast i8* %5 to <4 x i8>*
store <4 x i8> %18, <4 x i8>* %19
%20 = shl i32 %3, 16
%21 = lshr i32 %20, 28
%22 = icmp ult i32 %20, -1610612736
%23 = select i1 %22, i32 48, i32 55
%24 = add nuw nsw i32 %23, %21
%25 = trunc i32 %24 to i8
%26 = getelementptr inbounds i8, i8* %1, i64 6
store i8 %25, i8* %26
%27 = shl i32 %3, 20
%28 = lshr i32 %27, 28
%29 = icmp ult i32 %27, -1610612736
%30 = select i1 %29, i32 48, i32 55
%31 = add nuw nsw i32 %30, %28
%32 = trunc i32 %31 to i8
%33 = getelementptr inbounds i8, i8* %1, i64 7
store i8 %32, i8* %33
%34 = shl i32 %3, 24
%35 = lshr i32 %34, 28
%36 = icmp ult i32 %34, -1610612736
%37 = select i1 %36, i32 48, i32 55
%38 = add nuw nsw i32 %37, %35
%39 = trunc i32 %38 to i8
%40 = getelementptr inbounds i8, i8* %1, i64 8
store i8 %39, i8* %40
%41 = shl i32 %3, 28
%42 = and i32 %3, 15
%43 = icmp ult i32 %41, -1610612736
%44 = select i1 %43, i32 48, i32 55
%45 = add nuw nsw i32 %44, %42
%46 = trunc i32 %45 to i8
%47 = getelementptr inbounds i8, i8* %1, i64 9
store i8 %46, i8* %47
ret i32 10
}