#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 uint32_t;

/* Variables and functions */
scalar_t__ __rom_pr1(char *, size_t, int, char, char, char);
size_t ui32tostrrom(char *restrict buf, size_t bsz, uint32_t d) {
  size_t res;
  for (res = 0; d >= 1000 && res < bsz; d -= 1000) {
    buf[res++] = 'M';
  }
  res += __rom_pr1(buf + res, bsz - res, d / 100U, 'C', 'M', 'D');
  d %= 100;
  res += __rom_pr1(buf + res, bsz - res, d / 10U, 'X', 'C', 'L');
  d %= 10;
  res += __rom_pr1(buf + res, bsz - res, d, 'I', 'X', 'V');
  return res;
}


x86 assembly (clang, -O0):
<ui32tostrrom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x3e8,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jl     114b <ui32tostrrom+0x3b>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
setb   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    115b <ui32tostrrom+0x4b>
jmp    1182 <ui32tostrrom+0x72>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
movb   $0x4d,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
sub    $0x3e8,%eax
mov    %eax,-0x14(%rbp)
jmp    112b <ui32tostrrom+0x1b>
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    $0x64,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%edx
mov    $0x43,%ecx
mov    $0x4d,%r8d
mov    $0x44,%r9d
call   1030 <__rom_pr1@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%edx
mov    $0x58,%ecx
mov    $0x43,%r8d
mov    $0x4c,%r9d
call   1030 <__rom_pr1@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x49,%ecx
mov    $0x58,%r8d
mov    $0x56,%r9d
call   1030 <__rom_pr1@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ui32tostrrom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
xor    %r13d,%r13d
cmp    $0x3e8,%edx
jl     11a3 <ui32tostrrom+0x83>
test   %r15,%r15
je     11a3 <ui32tostrrom+0x83>
cmp    $0x7cf,%r12d
mov    $0x7cf,%eax
cmovl  %r12d,%eax
neg    %eax
add    %r12d,%eax
add    $0x3e7,%eax
imul   $0x10624dd3,%rax,%rcx
shr    $0x26,%rcx
lea    -0x1(%r15),%rdx
cmp    %rcx,%rdx
cmovb  %rdx,%rcx
xor    %ebx,%ebx
cmp    $0x3e8,%eax
cmovae %rcx,%rbx
lea    0x1(%rbx),%r13
mov    %r14,%rdi
mov    $0x4d,%esi
mov    %r13,%rdx
call   1030 <memset@plt>
imul   $0xfffffc18,%ebx,%eax
add    %eax,%r12d
add    $0xfffffc18,%r12d
lea    (%r14,%r13,1),%rdi
mov    %r15,%rsi
sub    %r13,%rsi
mov    %r12d,%eax
imul   $0x51eb851f,%rax,%rdx
shr    $0x25,%rdx
mov    $0x43,%ecx
mov    $0x4d,%r8d
mov    $0x44,%r9d
call   1040 <__rom_pr1@plt>
mov    %rax,%rbp
add    %r13,%rbp
movslq %r12d,%rbx
imul   $0x51eb851f,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
imul   $0x64,%eax,%eax
sub    %eax,%ebx
lea    (%r14,%rbp,1),%rdi
mov    %r15,%rsi
sub    %rbp,%rsi
mov    $0xcccccccd,%edx
imul   %rbx,%rdx
shr    $0x23,%rdx
mov    $0x58,%ecx
mov    $0x43,%r8d
mov    $0x4c,%r9d
call   1040 <__rom_pr1@plt>
mov    %rax,%r12
add    %rbp,%r12
movsbl %bl,%eax
imul   $0x67,%eax,%eax
movzwl %ax,%eax
movswl %ax,%ecx
shr    $0xf,%eax
shr    $0xa,%ecx
add    %al,%cl
movzbl %cl,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %al,%bl
movsbl %bl,%edx
add    %r12,%r14
sub    %r12,%r15
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x49,%ecx
mov    $0x58,%r8d
mov    $0x56,%r9d
call   1040 <__rom_pr1@plt>
add    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @ui32tostrrom(i8* noalias %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
store i64 0, i64* %7
br label %8
8: ; preds = %22, %3
%9 = load i32, i32* %6
%10 = icmp sge i32 %9, 1000
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i64, i64* %7
%13 = load i64, i64* %5
%14 = icmp ult i64 %12, %13
br label %15
15: ; preds = %11, %8
%16 = phi i1 [ false, %8 ], [ %14, %11 ]
br i1 %16, label %17, label %25
17: ; preds = %15
%18 = load i8*, i8** %4
%19 = load i64, i64* %7
%20 = add i64 %19, 1
store i64 %20, i64* %7
%21 = getelementptr inbounds i8, i8* %18, i64 %19
store i8 77, i8* %21
br label %22
22: ; preds = %17
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, 1000
store i32 %24, i32* %6
br label %8
25: ; preds = %15
%26 = load i8*, i8** %4
%27 = load i64, i64* %7
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i64, i64* %5
%30 = load i64, i64* %7
%31 = sub i64 %29, %30
%32 = load i32, i32* %6
%33 = udiv i32 %32, 100
%34 = call i64 @__rom_pr1(i8* %28, i64 %31, i32 %33, i8 signext 67, i8 signext 77, i8 signext 68)
%35 = load i64, i64* %7
%36 = add i64 %35, %34
store i64 %36, i64* %7
%37 = load i32, i32* %6
%38 = srem i32 %37, 100
store i32 %38, i32* %6
%39 = load i8*, i8** %4
%40 = load i64, i64* %7
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = load i64, i64* %5
%43 = load i64, i64* %7
%44 = sub i64 %42, %43
%45 = load i32, i32* %6
%46 = udiv i32 %45, 10
%47 = call i64 @__rom_pr1(i8* %41, i64 %44, i32 %46, i8 signext 88, i8 signext 67, i8 signext 76)
%48 = load i64, i64* %7
%49 = add i64 %48, %47
store i64 %49, i64* %7
%50 = load i32, i32* %6
%51 = srem i32 %50, 10
store i32 %51, i32* %6
%52 = load i8*, i8** %4
%53 = load i64, i64* %7
%54 = getelementptr inbounds i8, i8* %52, i64 %53
%55 = load i64, i64* %5
%56 = load i64, i64* %7
%57 = sub i64 %55, %56
%58 = load i32, i32* %6
%59 = call i64 @__rom_pr1(i8* %54, i64 %57, i32 %58, i8 signext 73, i8 signext 88, i8 signext 86)
%60 = load i64, i64* %7
%61 = add i64 %60, %59
store i64 %61, i64* %7
%62 = load i64, i64* %7
ret i64 %62
}

LLVM IR (-O2):
define i64 @ui32tostrrom(i8* noalias %0, i64 %1, i32 %2) {
%4 = icmp sgt i32 %2, 999
%5 = icmp ne i64 %1, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %22
7: ; preds = %3
%8 = add nuw i32 %2, 999
%9 = call i32 @llvm.smin.i32(i32 %2, i32 1999)
%10 = sub nuw i32 %8, %9
%11 = udiv i32 %10, 1000
%12 = zext i32 %11 to i64
%13 = add i64 %1, -1
%14 = icmp ult i32 %10, 1000
%15 = call i64 @llvm.umin.i64(i64 %13, i64 %12)
%16 = select i1 %14, i64 0, i64 %15
%17 = add nuw nsw i64 %16, 1
%18 = trunc i64 %16 to i32
%19 = mul nsw i32 %18, -1000
%20 = add nsw i32 %2, -1000
%21 = add nsw i32 %19, %20
br label %22
22: ; preds = %7, %3
%23 = phi i32 [ %2, %3 ], [ %21, %7 ]
%24 = phi i64 [ 0, %3 ], [ %17, %7 ]
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = sub i64 %1, %24
%27 = udiv i32 %23, 100
%28 = tail call i64 @__rom_pr1(i8* %25, i64 %26, i32 %27, i8 signext 67, i8 signext 77, i8 signext 68)
%29 = add i64 %28, %24
%30 = srem i32 %23, 100
%31 = getelementptr inbounds i8, i8* %0, i64 %29
%32 = sub i64 %1, %29
%33 = udiv i32 %30, 10
%34 = tail call i64 @__rom_pr1(i8* %31, i64 %32, i32 %33, i8 signext 88, i8 signext 67, i8 signext 76)
%35 = add i64 %29, %34
%36 = trunc i32 %30 to i8
%37 = srem i8 %36, 10
%38 = sext i8 %37 to i32
%39 = getelementptr inbounds i8, i8* %0, i64 %35
%40 = sub i64 %1, %35
%41 = tail call i64 @__rom_pr1(i8* %39, i64 %40, i32 %38, i8 signext 73, i8 signext 88, i8 signext 86)
%42 = add i64 %35, %41
ret i64 %42
}