void bcd_convert_16bit(unsigned int num, char *dest) {
  dest[0] = (num / 10000);
  dest[1] = ((num % 10000) / 1000);
  dest[2] = ((num % 1000) / 100);
  dest[3] = ((num % 100) / 10);
  dest[4] = (num % 10);
}


x86 assembly (clang, -O0):
<bcd_convert_16bit>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x2710,%ecx
xor    %edx,%edx
div    %ecx
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x4(%rbp),%eax
mov    $0x2710,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,%eax
mov    $0x64,%ecx
xor    %edx,%edx
div    %ecx
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x4(%rbp),%eax
mov    $0x64,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %dl,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x4(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<bcd_convert_16bit>:
mov    %edi,%eax
mov    $0xd1b71759,%ecx
imul   %rax,%rcx
shr    $0x2d,%rcx
mov    %cl,(%rsi)
imul   $0x2710,%ecx,%ecx
mov    %edi,%edx
sub    %ecx,%edx
movzwl %dx,%ecx
shr    $0x3,%ecx
imul   $0x20c5,%ecx,%ecx
shr    $0x14,%ecx
mov    %cl,0x1(%rsi)
imul   $0x10624dd3,%rax,%rcx
shr    $0x26,%rcx
imul   $0x3e8,%ecx,%ecx
mov    %edi,%edx
sub    %ecx,%edx
movzwl %dx,%ecx
shr    $0x2,%ecx
imul   $0x147b,%ecx,%ecx
shr    $0x11,%ecx
mov    %cl,0x2(%rsi)
imul   $0x51eb851f,%rax,%rcx
shr    $0x25,%rcx
imul   $0x64,%ecx,%ecx
mov    %edi,%edx
sub    %ecx,%edx
imul   $0xcd,%edx,%ecx
movzwl %cx,%ecx
shr    $0xb,%ecx
mov    %cl,0x3(%rsi)
mov    $0xcccccccd,%ecx
imul   %rax,%rcx
shr    $0x23,%rcx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%eax
sub    %eax,%edi
mov    %dil,0x4(%rsi)
ret

LLVM IR (-O0):
define void @bcd_convert_16bit(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = load i32, i32* %3
%6 = udiv i32 %5, 10000
%7 = trunc i32 %6 to i8
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %8, i64 0
store i8 %7, i8* %9
%10 = load i32, i32* %3
%11 = urem i32 %10, 10000
%12 = udiv i32 %11, 1000
%13 = trunc i32 %12 to i8
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %13, i8* %15
%16 = load i32, i32* %3
%17 = urem i32 %16, 1000
%18 = udiv i32 %17, 100
%19 = trunc i32 %18 to i8
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i64 2
store i8 %19, i8* %21
%22 = load i32, i32* %3
%23 = urem i32 %22, 100
%24 = udiv i32 %23, 10
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 3
store i8 %25, i8* %27
%28 = load i32, i32* %3
%29 = urem i32 %28, 10
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 4
store i8 %30, i8* %32
ret void
}

LLVM IR (-O2):
define void @bcd_convert_16bit(i32 %0, i8* nocapture writeonly %1) {
%3 = udiv i32 %0, 10000
%4 = trunc i32 %3 to i8
store i8 %4, i8* %1
%5 = urem i32 %0, 10000
%6 = trunc i32 %5 to i16
%7 = udiv i16 %6, 1000
%8 = trunc i16 %7 to i8
%9 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %8, i8* %9
%10 = urem i32 %0, 1000
%11 = trunc i32 %10 to i16
%12 = udiv i16 %11, 100
%13 = trunc i16 %12 to i8
%14 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %13, i8* %14
%15 = urem i32 %0, 100
%16 = trunc i32 %15 to i8
%17 = udiv i8 %16, 10
%18 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %17, i8* %18
%19 = urem i32 %0, 10
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %20, i8* %21
ret void
}