void int_to_string_hex(int input, char *str, int hex_flag) {
  int i;
  static char base[] = "0123456789abcdef";
  for (i = 9; i > 1; --i) {
    str[i] = base[input & 0xf];
    input >>= 4;
  }
  str[0] = '0';
  str[1] = 'x';
  str[10] = '\0';
  return;
}


x86 assembly (clang, -O0):
<int_to_string_hex>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x9,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jle    1154 <int_to_string_hex+0x54>
mov    -0x4(%rbp),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0x2f01(%rip),%rax
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
sar    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1115 <int_to_string_hex+0x15>
mov    -0x10(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x78,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0xa(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<int_to_string_hex>:
mov    %edi,%eax
mov    %eax,%edx
and    $0xf,%edx
lea    0xef2(%rip),%rcx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x9(%rsi)
mov    %edi,%edx
shr    $0x4,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x8(%rsi)
mov    %edi,%edx
shr    $0x8,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x7(%rsi)
mov    %edi,%edx
shr    $0xc,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x6(%rsi)
mov    %edi,%edx
shr    $0x10,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x5(%rsi)
mov    %edi,%edx
shr    $0x14,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x4(%rsi)
shr    $0x18,%edi
and    $0xf,%edi
mov    (%rdi,%rcx,1),%dl
mov    %dl,0x3(%rsi)
shr    $0x1c,%rax
mov    (%rax,%rcx,1),%al
mov    %al,0x2(%rsi)
movw   $0x7830,(%rsi)
movb   $0x0,0xa(%rsi)
ret

LLVM IR (-O0):
@int_to_string_hex.base = internal global [17 x i8] c"0123456789abcdef\00"
define void @int_to_string_hex(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 9, i32* %7
br label %8
8: ; preds = %23, %3
%9 = load i32, i32* %7
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %26
11: ; preds = %8
%12 = load i32, i32* %4
%13 = and i32 %12, 15
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [17 x i8], [17 x i8]* @int_to_string_hex.base, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 %16, i8* %20
%21 = load i32, i32* %4
%22 = ashr i32 %21, 4
store i32 %22, i32* %4
br label %23
23: ; preds = %11
%24 = load i32, i32* %7
%25 = add nsw i32 %24, -1
store i32 %25, i32* %7
br label %8
26: ; preds = %8
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 0
store i8 48, i8* %28
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8 120, i8* %30
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 10
store i8 0, i8* %32
ret void
}

LLVM IR (-O2):
@int_to_string_hex.base = internal unnamed_addr constant [17 x i8] c"0123456789abcdef\00"
define void @int_to_string_hex(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = and i32 %0, 15
%5 = zext i32 %4 to i64
%6 = getelementptr inbounds [17 x i8], [17 x i8]* @int_to_string_hex.base, i64 0, i64 %5
%7 = load i8, i8* %6
%8 = getelementptr inbounds i8, i8* %1, i64 9
store i8 %7, i8* %8
%9 = lshr i32 %0, 4
%10 = and i32 %9, 15
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds [17 x i8], [17 x i8]* @int_to_string_hex.base, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = getelementptr inbounds i8, i8* %1, i64 8
store i8 %13, i8* %14
%15 = lshr i32 %0, 8
%16 = and i32 %15, 15
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds [17 x i8], [17 x i8]* @int_to_string_hex.base, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = getelementptr inbounds i8, i8* %1, i64 7
store i8 %19, i8* %20
%21 = lshr i32 %0, 12
%22 = and i32 %21, 15
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [17 x i8], [17 x i8]* @int_to_string_hex.base, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = getelementptr inbounds i8, i8* %1, i64 6
store i8 %25, i8* %26
%27 = lshr i32 %0, 16
%28 = and i32 %27, 15
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds [17 x i8], [17 x i8]* @int_to_string_hex.base, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = getelementptr inbounds i8, i8* %1, i64 5
store i8 %31, i8* %32
%33 = lshr i32 %0, 20
%34 = and i32 %33, 15
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds [17 x i8], [17 x i8]* @int_to_string_hex.base, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %37, i8* %38
%39 = lshr i32 %0, 24
%40 = and i32 %39, 15
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds [17 x i8], [17 x i8]* @int_to_string_hex.base, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %43, i8* %44
%45 = lshr i32 %0, 28
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds [17 x i8], [17 x i8]* @int_to_string_hex.base, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %48, i8* %49
store i8 48, i8* %1
%50 = getelementptr inbounds i8, i8* %1, i64 1
store i8 120, i8* %50
%51 = getelementptr inbounds i8, i8* %1, i64 10
store i8 0, i8* %51
ret void
}