unsigned int DecimalToBCD(unsigned int value) {
  unsigned int accumulator = 0;
  unsigned int divisor = 10;
  int i;
  for (i = 0; i < 8; i++) {
    unsigned int temp;
    temp = value % divisor;
    value -= temp;
    temp /= divisor / 10;
    accumulator += temp << (i * 4);
    divisor *= 10;
  }
  return accumulator;
}


x86 assembly (clang, -O0):
<DecimalToBCD>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0xa,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x8,-0x10(%rbp)
jge    117a <DecimalToBCD+0x7a>
mov    -0x4(%rbp),%eax
xor    %edx,%edx
divl   -0xc(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
xor    %edx,%edx
div    %ecx
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%ecx
shl    $0x2,%ecx
shl    %cl,%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
imul   $0xa,-0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    111c <DecimalToBCD+0x1c>
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<DecimalToBCD>:
mov    %edi,%eax
mov    $0xcccccccd,%ecx
imul   %rax,%rcx
shr    $0x23,%rcx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%eax
sub    %eax,%edi
imul   $0x51eb851f,%rax,%rcx
shr    $0x25,%rcx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
imul   $0xcd,%eax,%eax
shr    $0x8,%eax
add    %al,%al
and    $0xf0,%al
movzbl %al,%eax
or     %edi,%eax
imul   $0x10624dd3,%rcx,%rdx
shr    $0x26,%rdx
imul   $0x3e8,%edx,%edx
sub    %edx,%ecx
movzwl %cx,%ecx
shr    $0x2,%ecx
imul   $0x147b,%ecx,%esi
shr    $0x9,%esi
and    $0xf00,%esi
or     %eax,%esi
mov    $0xd1b71759,%ecx
mov    %rdx,%rax
imul   %rcx,%rax
shr    $0x2d,%rax
imul   $0x2710,%eax,%eax
sub    %eax,%edx
movzwl %dx,%edx
shr    $0x3,%edx
imul   $0x20c5,%edx,%edx
shr    $0x8,%edx
and    $0x1f000,%edx
or     %esi,%edx
mov    %eax,%esi
shr    $0x5,%esi
imul   $0xa7c5ac5,%rsi,%rsi
shr    $0x27,%rsi
imul   $0x186a0,%esi,%esi
sub    %esi,%eax
imul   %rcx,%rax
shr    $0x20,%rax
and    $0x1e000,%eax
lea    (%rdx,%rax,8),%eax
imul   $0x431bde83,%rsi,%rcx
shr    $0x32,%rcx
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%esi
shr    $0x5,%esi
imul   $0xa7c5ac5,%rsi,%rdx
shr    $0x20,%rdx
shl    $0xd,%edx
and    $0xf00000,%edx
add    %eax,%edx
imul   $0x6b5fca6b,%rcx,%rax
shr    $0x36,%rax
imul   $0x989680,%eax,%eax
sub    %eax,%ecx
imul   $0x431bde83,%rcx,%rcx
shr    $0x20,%rcx
shl    $0x6,%ecx
and    $0x1f000000,%ecx
add    %edx,%ecx
imul   $0x55e63b89,%rax,%rdx
shr    $0x39,%rdx
imul   $0x5f5e100,%edx,%edx
sub    %edx,%eax
imul   $0x6b5fca6b,%rax,%rax
shr    $0x20,%rax
shl    $0x6,%eax
and    $0xf0000000,%eax
add    %ecx,%eax
ret

LLVM IR (-O0):
define i32 @DecimalToBCD(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 10, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %29, %1
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 8
br i1 %9, label %10, label %32
10: ; preds = %7
%11 = load i32, i32* %2
%12 = load i32, i32* %4
%13 = urem i32 %11, %12
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = load i32, i32* %2
%16 = sub i32 %15, %14
store i32 %16, i32* %2
%17 = load i32, i32* %4
%18 = udiv i32 %17, 10
%19 = load i32, i32* %6
%20 = udiv i32 %19, %18
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = load i32, i32* %5
%23 = mul nsw i32 %22, 4
%24 = shl i32 %21, %23
%25 = load i32, i32* %3
%26 = add i32 %25, %24
store i32 %26, i32* %3
%27 = load i32, i32* %4
%28 = mul i32 %27, 10
store i32 %28, i32* %4
br label %29
29: ; preds = %10
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %7
32: ; preds = %7
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
define i32 @DecimalToBCD(i32 %0) {
%2 = urem i32 %0, 10
%3 = sub nuw i32 %0, %2
%4 = urem i32 %3, 100
%5 = sub nuw i32 %3, %4
%6 = trunc i32 %4 to i8
%7 = udiv i8 %6, 10
%8 = shl nuw i8 %7, 4
%9 = zext i8 %8 to i32
%10 = or i32 %2, %9
%11 = urem i32 %5, 1000
%12 = sub nuw i32 %5, %11
%13 = trunc i32 %11 to i16
%14 = udiv i16 %13, 100
%15 = shl nuw nsw i16 %14, 8
%16 = zext i16 %15 to i32
%17 = or i32 %10, %16
%18 = urem i32 %12, 10000
%19 = sub nuw i32 %12, %18
%20 = trunc i32 %18 to i16
%21 = udiv i16 %20, 1000
%22 = zext i16 %21 to i32
%23 = shl nuw nsw i32 %22, 12
%24 = or i32 %23, %17
%25 = urem i32 %19, 100000
%26 = sub nuw i32 %19, %25
%27 = udiv i32 %25, 10000
%28 = shl nuw nsw i32 %27, 16
%29 = add nuw nsw i32 %28, %24
%30 = urem i32 %26, 1000000
%31 = sub nuw i32 %26, %30
%32 = udiv i32 %30, 100000
%33 = shl nuw nsw i32 %32, 20
%34 = add nuw nsw i32 %33, %29
%35 = urem i32 %31, 10000000
%36 = sub nuw i32 %31, %35
%37 = udiv i32 %35, 1000000
%38 = shl nuw nsw i32 %37, 24
%39 = add nuw nsw i32 %38, %34
%40 = urem i32 %36, 100000000
%41 = udiv i32 %40, 10000000
%42 = shl nuw i32 %41, 28
%43 = add i32 %42, %39
ret i32 %43
}