#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 *strcat(char *, char *);
int match_and_replace(int number) {
  int value[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  char *numeral[13] = {"M",  "CM", "D",  "CD", "C",  "XC", "L",
                       "XL", "X",  "IX", "V",  "IV", "I"};
  int i;
  char *result;
  i = 0;
  while (i < 13) {
    while (number >= value[i]) {
      number -= value[i];
      result = strcat(result, numeral[i]);
    }
    i++;
  }
  return (0);
}


x86 assembly (clang, -O0):
<match_and_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xec7(%rip),%rsi
mov    $0x34,%edx
call   1030 <memcpy@plt>
lea    -0xb0(%rbp),%rdi
lea    0x2c5f(%rip),%rsi
mov    $0x68,%edx
call   1030 <memcpy@plt>
movl   $0x0,-0xb4(%rbp)
cmpl   $0xd,-0xb4(%rbp)
jge    11d9 <match_and_replace+0xb9>
jmp    1177 <match_and_replace+0x57>
mov    -0x4(%rbp),%eax
movslq -0xb4(%rbp),%rcx
cmp    -0x40(%rbp,%rcx,4),%eax
jl     11c5 <match_and_replace+0xa5>
movslq -0xb4(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0xc0(%rbp),%rdi
movslq -0xb4(%rbp),%rax
mov    -0xb0(%rbp,%rax,8),%rsi
ff
call   1040 <strcat@plt>
mov    %rax,-0xc0(%rbp)
jmp    1177 <match_and_replace+0x57>
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
jmp    1165 <match_and_replace+0x45>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match_and_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %r15d,%r15d
lea    0xed9(%rip),%r12
lea    0xf06(%rip),%r13
jmp    113a <match_and_replace+0x2a>
add    $0x1,%r15
cmp    $0xd,%r15
je     1160 <match_and_replace+0x50>
mov    (%r12,%r15,4),%ebp
cmp    %ebp,%ebx
jl     1130 <match_and_replace+0x20>
movslq 0x0(%r13,%r15,4),%r14
add    %r13,%r14
nopw   0x0(%rax,%rax,1)
sub    %ebp,%ebx
mov    %r14,%rsi
call   1030 <strcat@plt>
cmp    %ebp,%ebx
jge    1150 <match_and_replace+0x40>
jmp    1130 <match_and_replace+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.match_and_replace.value = [13 x i32] [i32 1000, i32 900, i32 500, i32 400, i32 100, i32 90, i32 50, i32 40, i32 10, i32 9, i32 5, i32 4, i32 1]
@.str = [2 x i8] c"M\00"
@.str.1 = [3 x i8] c"CM\00"
@.str.2 = [2 x i8] c"D\00"
@.str.3 = [3 x i8] c"CD\00"
@.str.4 = [2 x i8] c"C\00"
@.str.5 = [3 x i8] c"XC\00"
@.str.6 = [2 x i8] c"L\00"
@.str.7 = [3 x i8] c"XL\00"
@.str.8 = [2 x i8] c"X\00"
@.str.9 = [3 x i8] c"IX\00"
@.str.10 = [2 x i8] c"V\00"
@.str.11 = [3 x i8] c"IV\00"
@.str.12 = [2 x i8] c"I\00"
@__const.match_and_replace.numeral = [13 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i32 0, i32 0)]
define i32 @match_and_replace(i32 %0) {
%2 = alloca i32
%3 = alloca [13 x i32]
%4 = alloca [13 x i8*]
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
%7 = bitcast [13 x i32]* %3 to i8*
%8 = bitcast [13 x i8*]* %4 to i8*
store i32 0, i32* %5
br label %9
9: ; preds = %33, %1
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 13
br i1 %11, label %12, label %36
12: ; preds = %9
br label %13
13: ; preds = %20, %12
%14 = load i32, i32* %2
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [13 x i32], [13 x i32]* %3, i64 0, i64 %16
%18 = load i32, i32* %17
%19 = icmp sge i32 %14, %18
br i1 %19, label %20, label %33
20: ; preds = %13
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [13 x i32], [13 x i32]* %3, i64 0, i64 %22
%24 = load i32, i32* %23
%25 = load i32, i32* %2
%26 = sub nsw i32 %25, %24
store i32 %26, i32* %2
%27 = load i8*, i8** %6
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [13 x i8*], [13 x i8*]* %4, i64 0, i64 %29
%31 = load i8*, i8** %30
%32 = call i8* @strcat(i8* %27, i8* %31)
store i8* %32, i8** %6
br label %13
33: ; preds = %13
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
br label %9
36: ; preds = %9
ret i32 0
}

LLVM IR (-O2):
@__const.match_and_replace.value = [13 x i32] [i32 1000, i32 900, i32 500, i32 400, i32 100, i32 90, i32 50, i32 40, i32 10, i32 9, i32 5, i32 4, i32 1]
@.str = [2 x i8] c"M\00"
@.str.1 = [3 x i8] c"CM\00"
@.str.2 = [2 x i8] c"D\00"
@.str.3 = [3 x i8] c"CD\00"
@.str.4 = [2 x i8] c"C\00"
@.str.5 = [3 x i8] c"XC\00"
@.str.6 = [2 x i8] c"L\00"
@.str.7 = [3 x i8] c"XL\00"
@.str.8 = [2 x i8] c"X\00"
@.str.9 = [3 x i8] c"IX\00"
@.str.10 = [2 x i8] c"V\00"
@.str.11 = [3 x i8] c"IV\00"
@.str.12 = [2 x i8] c"I\00"
@reltable.match_and_replace = [13 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.2 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.4 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.5 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.6 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.7 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.8 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.9 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.10 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.11 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.12 to i64), i64 ptrtoint ([13 x i32]* @reltable.match_and_replace to i64)) to i32)]
define i32 @match_and_replace(i32 %0) {
br label %2
2: ; preds = %1, %16
%3 = phi i64 [ 0, %1 ], [ %18, %16 ]
%4 = phi i32 [ %0, %1 ], [ %17, %16 ]
%5 = getelementptr inbounds [13 x i32], [13 x i32]* @__const.match_and_replace.value, i64 0, i64 %3
%6 = load i32, i32* %5
%7 = icmp slt i32 %4, %6
br i1 %7, label %16, label %8
8: ; preds = %2
%9 = shl i64 %3, 2
%10 = call i8* @llvm.load.relative.i64(i8* bitcast ([13 x i32]* @reltable.match_and_replace to i8*), i64 %9)
br label %11
11: ; preds = %8, %11
%12 = phi i32 [ %4, %8 ], [ %13, %11 ]
%13 = sub nsw i32 %12, %6
%14 = tail call i8* @strcat(i8* dereferenceable(1) undef, i8* dereferenceable(1) %10)
%15 = icmp slt i32 %13, %6
br i1 %15, label %16, label %11
16: ; preds = %11, %2
%17 = phi i32 [ %4, %2 ], [ %13, %11 ]
%18 = add nuw nsw i64 %3, 1
%19 = icmp eq i64 %18, 13
br i1 %19, label %20, label %2
20: ; preds = %16
ret i32 0
}