#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint8_t;

/* Variables and functions */
uint8_t bin8tobcd(uint8_t decimal) {
  uint8_t res = 0;
  uint8_t n = 0;
  while (decimal >= 10) {
    n++;
    decimal -= 10;
  }
  res = n << 4;
  n = 0;
  while (decimal >= 1) {
    n++;
    decimal -= 1;
  }
  res |= n;
  return res;
}


x86 assembly (clang, -O0):
<bin8tobcd>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0x4(%rbp)
jl     1136 <bin8tobcd+0x36>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
sub    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    1115 <bin8tobcd+0x15>
mov    -0xc(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x1,-0x4(%rbp)
jl     1167 <bin8tobcd+0x67>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1146 <bin8tobcd+0x46>
mov    -0xc(%rbp),%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin8tobcd>:
xor    %eax,%eax
cmp    $0xa,%edi
jl     113c <bin8tobcd+0x3c>
cmp    $0x13,%edi
mov    $0x13,%ecx
cmovl  %edi,%ecx
neg    %ecx
lea    (%rdi,%rcx,1),%edx
add    $0x9,%edx
mov    $0xcccccccd,%ecx
imul   %rdx,%rcx
shr    $0x23,%rcx
lea    (%rcx,%rcx,1),%edx
lea    (%rdx,%rdx,4),%edx
neg    %edx
add    %edx,%edi
add    $0xfffffff6,%edi
shl    $0x4,%ecx
add    $0x10,%ecx
jmp    113e <bin8tobcd+0x3e>
xor    %ecx,%ecx
test   %edi,%edi
cmovns %edi,%eax
or     %ecx,%eax
ret

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

LLVM IR (-O2):
define i32 @bin8tobcd(i32 %0) {
%2 = icmp sgt i32 %0, 9
br i1 %2, label %3, label %13
3: ; preds = %1
%4 = add nuw i32 %0, 9
%5 = call i32 @llvm.smin.i32(i32 %0, i32 19)
%6 = sub nuw i32 %4, %5
%7 = udiv i32 %6, 10
%8 = mul nsw i32 %7, -10
%9 = add nsw i32 %0, -10
%10 = add nsw i32 %8, %9
%11 = shl nuw i32 %7, 4
%12 = add nuw i32 %11, 16
br label %13
13: ; preds = %3, %1
%14 = phi i32 [ %0, %1 ], [ %10, %3 ]
%15 = phi i32 [ 0, %1 ], [ %12, %3 ]
%16 = icmp sgt i32 %14, 0
%17 = select i1 %16, i32 %14, i32 0
%18 = or i32 %17, %15
ret i32 %18
}