#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 */
char *malloc(int);
int numToChar(int);
int /*<<< orphan*/ sprintf(char *, char *, char);
char *intToRoman(int num) {
  char *result = malloc(100);
  int step = 1000;
  int count = 0;
  char *cur = result;
  while (step > 0) {
    count = num / step;
    if (count == 9) {
      sprintf(cur++, "%c", numToChar(step));
      sprintf(cur++, "%c", numToChar(step * 10));
      count -= 9;
    }
    if (count >= 5) {
      sprintf(cur++, "%c", numToChar(step * 5));
      count -= 5;
    }
    if (count == 4) {
      sprintf(cur++, "%c", numToChar(step));
      sprintf(cur++, "%c", numToChar(step * 5));
      count -= 4;
    }
    while (count > 0) {
      sprintf(cur++, "%c", numToChar(step));
      count--;
    }
    num %= step;
    step /= 10;
  }
  sprintf(cur, "%c", '\0');
  return result;
}


x86 assembly (clang, -O0):
<intToRoman>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x64,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x3e8,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    12ed <intToRoman+0x1bd>
mov    -0x4(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,-0x18(%rbp)
cmpl   $0x9,-0x18(%rbp)
jne    11dd <intToRoman+0xad>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <numToChar@plt>
mov    -0x30(%rbp),%rdi
lea    0xe60(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
imul   $0xa,-0x14(%rbp),%edi
call   1030 <numToChar@plt>
mov    -0x28(%rbp),%rdi
lea    0xe34(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%eax
sub    $0x9,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x5,-0x18(%rbp)
jl     121c <intToRoman+0xec>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
imul   $0x5,-0x14(%rbp),%edi
call   1030 <numToChar@plt>
mov    -0x38(%rbp),%rdi
lea    0xdf5(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%eax
sub    $0x5,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jne    1286 <intToRoman+0x156>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <numToChar@plt>
mov    -0x48(%rbp),%rdi
lea    0xdb7(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
imul   $0x5,-0x14(%rbp),%edi
call   1030 <numToChar@plt>
mov    -0x40(%rbp),%rdi
lea    0xd8b(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x18(%rbp)
jmp    128b <intToRoman+0x15b>
cmpl   $0x0,-0x18(%rbp)
jle    12ce <intToRoman+0x19e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <numToChar@plt>
mov    -0x50(%rbp),%rdi
lea    0xd48(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    128b <intToRoman+0x15b>
mov    -0x14(%rbp),%ecx
mov    -0x4(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x4(%rbp)
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
jmp    115f <intToRoman+0x2f>
mov    -0x20(%rbp),%rdi
lea    0xd08(%rip),%rsi
xor    %edx,%edx
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intToRoman>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
mov    $0x64,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    $0x3e8,%r13d
mov    %rax,(%rsp)
jmp    11a3 <intToRoman+0x83>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%r14
mov    %r13d,%edi
call   1030 <numToChar@plt>
mov    %al,(%r14)
movb   $0x0,0x1(%r14)
lea    0x2(%r14),%rbx
imul   %r13d,%r12d
mov    %r12d,%edi
call   1030 <numToChar@plt>
mov    %al,0x1(%r14)
movb   $0x0,0x2(%r14)
mov    %rbx,%r14
movslq %r13d,%rax
imul   $0x66666667,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
cmp    $0x9,%r13d
mov    %eax,%r13d
jle    1222 <intToRoman+0x102>
mov    %r15d,%eax
cltd
idiv   %r13d
mov    $0xa,%r12d
mov    %edx,%r15d
cmp    $0x9,%eax
je     1153 <intToRoman+0x33>
mov    %eax,%ebp
cmp    $0x5,%eax
jl     11e0 <intToRoman+0xc0>
lea    0x1(%r14),%rbx
lea    0x0(%r13,%r13,4),%edi
call   1030 <numToChar@plt>
mov    %al,(%r14)
movb   $0x0,0x1(%r14)
add    $0xfffffffb,%ebp
jmp    11e3 <intToRoman+0xc3>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rbx
mov    $0x5,%r12d
cmp    $0x4,%ebp
je     1150 <intToRoman+0x30>
test   %ebp,%ebp
jle    117c <intToRoman+0x5c>
add    $0x1,%ebp
nopl   0x0(%rax)
lea    0x1(%rbx),%r14
mov    %r13d,%edi
call   1030 <numToChar@plt>
mov    %al,(%rbx)
movb   $0x0,0x1(%rbx)
add    $0xffffffff,%ebp
mov    %r14,%rbx
cmp    $0x1,%ebp
ja     1200 <intToRoman+0xe0>
jmp    117f <intToRoman+0x5f>
movw   $0x0,(%r14)
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%c\00"
define i8* @intToRoman(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
%7 = call i8* @malloc(i32 100)
store i8* %7, i8** %3
store i32 1000, i32* %4
store i32 0, i32* %5
%8 = load i8*, i8** %3
store i8* %8, i8** %6
br label %9
9: ; preds = %79, %1
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %85
12: ; preds = %9
%13 = load i32, i32* %2
%14 = load i32, i32* %4
%15 = sdiv i32 %13, %14
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 9
br i1 %17, label %18, label %34
18: ; preds = %12
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %6
%21 = load i32, i32* %4
%22 = call i32 @numToChar(i32 %21)
%23 = trunc i32 %22 to i8
%24 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %23)
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %6
%27 = load i32, i32* %4
%28 = mul nsw i32 %27, 10
%29 = call i32 @numToChar(i32 %28)
%30 = trunc i32 %29 to i8
%31 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %30)
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 9
store i32 %33, i32* %5
br label %34
34: ; preds = %18, %12
%35 = load i32, i32* %5
%36 = icmp sge i32 %35, 5
br i1 %36, label %37, label %47
37: ; preds = %34
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %6
%40 = load i32, i32* %4
%41 = mul nsw i32 %40, 5
%42 = call i32 @numToChar(i32 %41)
%43 = trunc i32 %42 to i8
%44 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %43)
%45 = load i32, i32* %5
%46 = sub nsw i32 %45, 5
store i32 %46, i32* %5
br label %47
47: ; preds = %37, %34
%48 = load i32, i32* %5
%49 = icmp eq i32 %48, 4
br i1 %49, label %50, label %66
50: ; preds = %47
%51 = load i8*, i8** %6
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %6
%53 = load i32, i32* %4
%54 = call i32 @numToChar(i32 %53)
%55 = trunc i32 %54 to i8
%56 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %55)
%57 = load i8*, i8** %6
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %6
%59 = load i32, i32* %4
%60 = mul nsw i32 %59, 5
%61 = call i32 @numToChar(i32 %60)
%62 = trunc i32 %61 to i8
%63 = call i32 @sprintf(i8* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %62)
%64 = load i32, i32* %5
%65 = sub nsw i32 %64, 4
store i32 %65, i32* %5
br label %66
66: ; preds = %50, %47
br label %67
67: ; preds = %70, %66
%68 = load i32, i32* %5
%69 = icmp sgt i32 %68, 0
br i1 %69, label %70, label %79
70: ; preds = %67
%71 = load i8*, i8** %6
%72 = getelementptr inbounds i8, i8* %71, i32 1
store i8* %72, i8** %6
%73 = load i32, i32* %4
%74 = call i32 @numToChar(i32 %73)
%75 = trunc i32 %74 to i8
%76 = call i32 @sprintf(i8* %71, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %75)
%77 = load i32, i32* %5
%78 = add nsw i32 %77, -1
store i32 %78, i32* %5
br label %67
79: ; preds = %67
%80 = load i32, i32* %4
%81 = load i32, i32* %2
%82 = srem i32 %81, %80
store i32 %82, i32* %2
%83 = load i32, i32* %4
%84 = sdiv i32 %83, 10
store i32 %84, i32* %4
br label %9
85: ; preds = %9
%86 = load i8*, i8** %6
%87 = call i32 @sprintf(i8* %86, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext 0)
%88 = load i8*, i8** %3
ret i8* %88
}

LLVM IR (-O2):
define noalias i8* @intToRoman(i32 %0) {
%2 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
br label %3
3: ; preds = %1, %42
%4 = phi i8* [ %2, %1 ], [ %43, %42 ]
%5 = phi i32 [ 1000, %1 ], [ %44, %42 ]
%6 = phi i32 [ %0, %1 ], [ %8, %42 ]
%7 = sdiv i32 %6, %5
%8 = srem i32 %6, %5
%9 = icmp eq i32 %7, 9
br i1 %9, label %32, label %10
10: ; preds = %3
%11 = icmp sgt i32 %7, 4
br i1 %11, label %12, label %18
12: ; preds = %10
%13 = getelementptr i8, i8* %4, i64 1
%14 = mul nuw nsw i32 %5, 5
%15 = tail call i32 @numToChar(i32 %14)
%16 = trunc i32 %15 to i8
store i8 %16, i8* %4
store i8 0, i8* %13
%17 = add nsw i32 %7, -5
br label %18
18: ; preds = %12, %10
%19 = phi i32 [ %17, %12 ], [ %7, %10 ]
%20 = phi i8* [ %13, %12 ], [ %4, %10 ]
%21 = icmp eq i32 %19, 4
br i1 %21, label %32, label %22
22: ; preds = %18
%23 = icmp sgt i32 %19, 0
br i1 %23, label %24, label %42
24: ; preds = %22, %24
%25 = phi i8* [ %27, %24 ], [ %20, %22 ]
%26 = phi i32 [ %30, %24 ], [ %19, %22 ]
%27 = getelementptr i8, i8* %25, i64 1
%28 = tail call i32 @numToChar(i32 %5)
%29 = trunc i32 %28 to i8
store i8 %29, i8* %25
store i8 0, i8* %27
%30 = add nsw i32 %26, -1
%31 = icmp ugt i32 %26, 1
br i1 %31, label %24, label %42
32: ; preds = %18, %3
%33 = phi i8* [ %4, %3 ], [ %20, %18 ]
%34 = phi i32 [ 10, %3 ], [ 5, %18 ]
%35 = getelementptr i8, i8* %33, i64 1
%36 = tail call i32 @numToChar(i32 %5)
%37 = trunc i32 %36 to i8
store i8 %37, i8* %33
store i8 0, i8* %35
%38 = getelementptr i8, i8* %33, i64 2
%39 = mul nuw nsw i32 %5, %34
%40 = tail call i32 @numToChar(i32 %39)
%41 = trunc i32 %40 to i8
store i8 %41, i8* %35
store i8 0, i8* %38
br label %42
42: ; preds = %24, %32, %22
%43 = phi i8* [ %20, %22 ], [ %38, %32 ], [ %27, %24 ]
%44 = sdiv i32 %5, 10
%45 = icmp sgt i32 %5, 9
br i1 %45, label %3, label %46
46: ; preds = %42
store i8 0, i8* %43
%47 = getelementptr i8, i8* %43, i64 1
store i8 0, i8* %47
ret i8* %2
}