void writex(int fd, char *s, int l, char *p) {
  unsigned long i = (long)p;
  char b[10];
  p = b + sizeof b;
  do
    *--p = "0123456789abcdef"[i % 16];
  while (i /= 16);
  *--p = 'x';
  *--p = '0';
  write(2, s, l);
  write(2, p, b + sizeof b - p);
}


x86 assembly (clang, -O0):
<writex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x32(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rcx
and    $0xf,%rcx
lea    0xeb7(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x28(%rbp),%rax
shr    $0x4,%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    113a <writex+0x2a>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x78,-0x1(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x30,-0x1(%rax)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x2,%edi
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x20(%rbp),%rsi
lea    -0x32(%rbp),%rdx
add    $0xa,%rdx
mov    -0x20(%rbp),%rax
sub    %rax,%rdx
mov    $0x2,%edi
mov    $0x0,%al
call   1030 <write@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writex>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x16(%rsp),%rbx
mov    $0x2,%r14d
lea    0xed7(%rip),%r8
mov    %rcx,%rdi
nopl   0x0(%rax)
mov    %ecx,%eax
and    $0xf,%eax
movzbl (%rax,%r8,1),%eax
mov    %al,0x1(%rbx)
shr    $0x4,%rdi
add    $0x1,%r14
add    $0xffffffffffffffff,%rbx
cmp    $0xf,%rcx
mov    %rdi,%rcx
ja     1130 <writex+0x20>
movw   $0x7830,(%rbx)
mov    $0x2,%edi
xor    %eax,%eax
call   1030 <write@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <write@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789abcdef\00"
define void @writex(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [10 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%11 = load i8*, i8** %8
%12 = ptrtoint i8* %11 to i64
store i64 %12, i64* %9
%13 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%14 = getelementptr inbounds i8, i8* %13, i64 10
store i8* %14, i8** %8
br label %15
15: ; preds = %22, %4
%16 = load i64, i64* %9
%17 = urem i64 %16, 16
%18 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = load i8*, i8** %8
%21 = getelementptr inbounds i8, i8* %20, i32 -1
store i8* %21, i8** %8
store i8 %19, i8* %21
br label %22
22: ; preds = %15
%23 = load i64, i64* %9
%24 = udiv i64 %23, 16
store i64 %24, i64* %9
%25 = icmp ne i64 %24, 0
br i1 %25, label %15, label %26
26: ; preds = %22
%27 = load i8*, i8** %8
%28 = getelementptr inbounds i8, i8* %27, i32 -1
store i8* %28, i8** %8
store i8 120, i8* %28
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %8
store i8 48, i8* %30
%31 = load i8*, i8** %6
%32 = load i32, i32* %7
%33 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 2, i8* %31, i32 %32)
%34 = load i8*, i8** %8
%35 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%36 = getelementptr inbounds i8, i8* %35, i64 10
%37 = load i8*, i8** %8
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i64, ...)*)(i32 2, i8* %34, i64 %40)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
define void @writex(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca [10 x i8]
%6 = ptrtoint i8* %3 to i64
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 10
br label %9
9: ; preds = %9, %4
%10 = phi i8* [ %8, %4 ], [ %15, %9 ]
%11 = phi i64 [ %6, %4 ], [ %16, %9 ]
%12 = and i64 %11, 15
%13 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = getelementptr inbounds i8, i8* %10, i64 -1
store i8 %14, i8* %15
%16 = lshr i64 %11, 4
%17 = icmp ult i64 %11, 16
br i1 %17, label %18, label %9
18: ; preds = %9
%19 = getelementptr inbounds i8, i8* %10, i64 -2
store i8 120, i8* %19
%20 = getelementptr inbounds i8, i8* %10, i64 -3
store i8 48, i8* %20
%21 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i32, ...)*)(i32 2, i8* %1, i32 %2)
%22 = ptrtoint i8* %8 to i64
%23 = ptrtoint i8* %20 to i64
%24 = sub i64 %22, %23
%25 = call i32 (i32, i8*, i64, ...) bitcast (i32 (...)* @write to i32 (i32, i8*, i64, ...)*)(i32 2, i8* %20, i64 %24)
ret void
}