puthex(n, obuf) unsigned n;
char *obuf;
{
  int i, nyb;
  for (i = 3; i >= 0; --i) {
    nyb = (n >> (i * 4)) & 0xF;
    nyb += (nyb > 9) ? 'A' - 10 : '0';
    fputc(nyb, obuf);
  }
}


x86 assembly (clang, -O0):
<puthex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1176 <puthex+0x66>
mov    -0x8(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0x2,%ecx
shr    %cl,%eax
and    $0xf,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edx
mov    $0x30,%eax
mov    $0x37,%ecx
cmp    $0x9,%edx
cmovg  %ecx,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1030 <fputc@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1126 <puthex+0x16>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<puthex>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
mov    %edi,%eax
shr    $0xc,%eax
and    $0xf,%eax
lea    0x30(%rax),%ecx
lea    0x37(%rax),%edi
cmp    $0xa,%eax
cmovb  %ecx,%edi
xor    %eax,%eax
call   1030 <fputc@plt>
mov    %ebx,%eax
shr    $0x8,%eax
and    $0xf,%eax
lea    0x30(%rax),%ecx
lea    0x37(%rax),%edi
cmp    $0xa,%eax
cmovb  %ecx,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <fputc@plt>
mov    %ebx,%eax
shr    $0x4,%eax
and    $0xf,%eax
lea    0x30(%rax),%ecx
lea    0x37(%rax),%edi
cmp    $0xa,%eax
cmovb  %ecx,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <fputc@plt>
and    $0xf,%ebx
lea    0x30(%rbx),%eax
lea    0x37(%rbx),%edi
cmp    $0xa,%ebx
cmovb  %eax,%edi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <fputc@plt>

LLVM IR (-O0):
define i32 @puthex(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 3, i32* %6
br label %8
8: ; preds = %26, %2
%9 = load i32, i32* %6
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %29
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i32, i32* %6
%14 = mul nsw i32 %13, 4
%15 = lshr i32 %12, %14
%16 = and i32 %15, 15
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp sgt i32 %17, 9
%19 = zext i1 %18 to i64
%20 = select i1 %18, i32 55, i32 48
%21 = load i32, i32* %7
%22 = add nsw i32 %21, %20
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = load i8*, i8** %5
%25 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @fputc to i32 (i32, i8*, ...)*)(i32 %23, i8* %24)
br label %26
26: ; preds = %11
%27 = load i32, i32* %6
%28 = add nsw i32 %27, -1
store i32 %28, i32* %6
br label %8
29: ; preds = %8
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
define i32 @puthex(i32 %0, i8* %1) {
%3 = lshr i32 %0, 12
%4 = and i32 %3, 15
%5 = icmp ugt i32 %4, 9
%6 = select i1 %5, i32 55, i32 48
%7 = add nuw nsw i32 %6, %4
%8 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @fputc to i32 (i32, i8*, ...)*)(i32 %7, i8* %1)
%9 = lshr i32 %0, 8
%10 = and i32 %9, 15
%11 = icmp ugt i32 %10, 9
%12 = select i1 %11, i32 55, i32 48
%13 = add nuw nsw i32 %12, %10
%14 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @fputc to i32 (i32, i8*, ...)*)(i32 %13, i8* %1)
%15 = lshr i32 %0, 4
%16 = and i32 %15, 15
%17 = icmp ugt i32 %16, 9
%18 = select i1 %17, i32 55, i32 48
%19 = add nuw nsw i32 %18, %16
%20 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @fputc to i32 (i32, i8*, ...)*)(i32 %19, i8* %1)
%21 = and i32 %0, 15
%22 = icmp ugt i32 %21, 9
%23 = select i1 %22, i32 55, i32 48
%24 = add nuw nsw i32 %23, %21
%25 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @fputc to i32 (i32, i8*, ...)*)(i32 %24, i8* %1)
ret i32 undef
}