void printnum(void (*putch)(int, void *), void *putdat, unsigned long num,
              unsigned base, int width, int padc) {
  if (num >= base) {
    printnum(putch, putdat, num / base, base, width - 1, padc);
  } else {
    while (--width > 0)
      putch(padc, putdat);
  }
  putch("0123456789abcdef"[num % base], putdat);
}


x86 assembly (clang, -O0):
<printnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
cmp    %rcx,%rax
jb     116f <printnum+0x5f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
sub    $0x1,%r8d
mov    -0x24(%rbp),%r9d
call   1030 <printnum@plt>
jmp    119d <printnum+0x8d>
jmp    1174 <printnum+0x64>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    1198 <printnum+0x88>
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   *%rax
jmp    1174 <printnum+0x64>
jmp    119d <printnum+0x8d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
xor    %edx,%edx
div    %rcx
mov    -0x30(%rbp),%rax
lea    0xe44(%rip),%rcx
movsbl (%rcx,%rdx,1),%edi
mov    -0x10(%rbp),%rsi
call   *%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printnum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r13d
mov    %r8d,%ebx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r8
mov    %ecx,%r12d
cmp    %rdx,%r12
mov    %rdi,%rbp
jbe    1155 <printnum+0x45>
cmp    $0x2,%ebx
jl     118a <printnum+0x7a>
add    $0x1,%ebx
nopl   (%rax)
mov    %r13d,%edi
mov    %r15,%rsi
call   *%rbp
mov    %rbp,%r8
add    $0xffffffff,%ebx
cmp    $0x2,%ebx
ja     1140 <printnum+0x30>
jmp    118a <printnum+0x7a>
mov    %r14,%rax
shr    $0x20,%rax
je     1168 <printnum+0x58>
mov    %r14,%rax
xor    %edx,%edx
div    %r12
jmp    1170 <printnum+0x60>
mov    %r14d,%eax
xor    %edx,%edx
div    %r12d
add    $0xffffffff,%ebx
mov    %r8,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %ebx,%r8d
mov    %r13d,%r9d
call   1030 <printnum@plt>
mov    %rbp,%r8
mov    %r14,%rax
shr    $0x20,%rax
je     119d <printnum+0x8d>
mov    %r14,%rax
xor    %edx,%edx
div    %r12
jmp    11a5 <printnum+0x95>
mov    %r14d,%eax
xor    %edx,%edx
div    %r12d
lea    0xe54(%rip),%rax
movsbl (%rdx,%rax,1),%edi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    *%r8

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

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
define void @printnum(void (i32, i8*)* nocapture %0, i8* %1, i64 %2, i32 %3, i32 %4, i32 %5) {
%7 = zext i32 %3 to i64
%8 = icmp ugt i64 %7, %2
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = icmp sgt i32 %4, 1
br i1 %10, label %14, label %18
11: ; preds = %6
%12 = udiv i64 %2, %7
%13 = add nsw i32 %4, -1
tail call void @printnum(void (i32, i8*)* %0, i8* %1, i64 %12, i32 %3, i32 %13, i32 %5)
br label %18
14: ; preds = %9, %14
%15 = phi i32 [ %16, %14 ], [ %4, %9 ]
%16 = add nsw i32 %15, -1
tail call void %0(i32 %5, i8* %1)
%17 = icmp ugt i32 %15, 2
br i1 %17, label %14, label %18
18: ; preds = %14, %9, %11
%19 = urem i64 %2, %7
%20 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
tail call void %0(i32 %22, i8* %1)
ret void
}