#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 */

/* Variables and functions */
unsigned char proper_to_bcd_single(unsigned int);
unsigned char *proper_to_bcd(unsigned int value, unsigned char *target,
                             size_t len) {
  while (value > 0 && len > 0) {
    target[len - 1] = proper_to_bcd_single(value & 0xff);
    value /= 100;
    len--;
  }
  return target;
}


x86 assembly (clang, -O0):
<proper_to_bcd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x4(%rbp)
mov    %al,-0x19(%rbp)
jbe    113d <proper_to_bcd+0x2d>
cmpq   $0x0,-0x18(%rbp)
seta   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    114d <proper_to_bcd+0x3d>
jmp    118c <proper_to_bcd+0x7c>
mov    -0x4(%rbp),%edi
and    $0xff,%edi
call   1030 <proper_to_bcd_single@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
mov    $0x64,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1123 <proper_to_bcd+0x13>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proper_to_bcd>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
test   %edi,%edi
je     1159 <proper_to_bcd+0x49>
mov    %rdx,%r15
test   %rdx,%rdx
je     1159 <proper_to_bcd+0x49>
mov    %edi,%ebx
add    $0xffffffffffffffff,%r15
nopw   0x0(%rax,%rax,1)
movzbl %bl,%edi
call   1030 <proper_to_bcd_single@plt>
mov    %al,(%r14,%r15,1)
add    $0xffffffffffffffff,%r15
setb   %al
cmp    $0x64,%ebx
jb     1159 <proper_to_bcd+0x49>
mov    %ebx,%ecx
imul   $0x51eb851f,%rcx,%rbx
shr    $0x25,%rbx
test   %al,%al
jne    1130 <proper_to_bcd+0x20>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @proper_to_bcd(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %7
7: ; preds = %15, %3
%8 = load i32, i32* %4
%9 = icmp ugt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i64, i64* %6
%12 = icmp ugt i64 %11, 0
br label %13
13: ; preds = %10, %7
%14 = phi i1 [ false, %7 ], [ %12, %10 ]
br i1 %14, label %15, label %27
15: ; preds = %13
%16 = load i32, i32* %4
%17 = and i32 %16, 255
%18 = call zeroext i8 @proper_to_bcd_single(i32 %17)
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
%21 = sub i64 %20, 1
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 %18, i8* %22
%23 = load i32, i32* %4
%24 = udiv i32 %23, 100
store i32 %24, i32* %4
%25 = load i64, i64* %6
%26 = add i64 %25, -1
store i64 %26, i64* %6
br label %7
27: ; preds = %13
%28 = load i8*, i8** %5
ret i8* %28
}

LLVM IR (-O2):
define i8* @proper_to_bcd(i32 %0, i8* returned writeonly %1, i64 %2) {
%4 = icmp ne i32 %0, 0
%5 = icmp ne i64 %2, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %18
7: ; preds = %3, %7
%8 = phi i64 [ %12, %7 ], [ %2, %3 ]
%9 = phi i32 [ %14, %7 ], [ %0, %3 ]
%10 = and i32 %9, 255
%11 = tail call zeroext i8 @proper_to_bcd_single(i32 %10)
%12 = add i64 %8, -1
%13 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 %11, i8* %13
%14 = udiv i32 %9, 100
%15 = icmp ugt i32 %9, 99
%16 = icmp ne i64 %12, 0
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %7, label %18
18: ; preds = %7, %3
ret i8* %1
}