#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ isdigit(char);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *int_to_roman(char *input) {
  int i;
  for (i = 0; i < strlen(input); i++) {
    if (!isdigit(input[i])) {
      printf("Error: Enter a whole number\n");
      exit(EXIT_FAILURE);
    }
  }
  int input_number = atoi(input);
  if (input_number <= 0 || input_number >= 4000) {
    printf("Error: Enter a number between 1 and 3999\n");
    exit(EXIT_FAILURE);
  }
  int ints[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  char *nums[] = {"M",  "CM", "D",  "CD", "C",  "XC", "L",
                  "XL", "X",  "IX", "V",  "IV", "I"};
  char *result = NULL;
  for (i = 0; i < 13; i++) {
    int count = input_number / ints[i];
    int j;
    for (j = 0; j < count; j++) {
      if (!result) {
        result = malloc(strlen(nums[i]) + 1);
        strcpy(result, nums[i]);
        continue;
      }
      strcat(result, nums[i]);
    }
    input_number -= ints[i] * count;
  }
  return result;
}


x86 assembly (clang, -O0):
<int_to_roman>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0xd4(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0xd4(%rbp),%eax
cmp    %ecx,%eax
jge    120f <int_to_roman+0x7f>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    11fc <int_to_roman+0x6c>
lea    0xe17(%rip),%rdi
call   1060 <printf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    1201 <int_to_roman+0x71>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a6 <int_to_roman+0x16>
mov    -0x8(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1232 <int_to_roman+0xa2>
cmpl   $0xfa0,-0x10(%rbp)
jl     124c <int_to_roman+0xbc>
lea    0xde4(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x50(%rbp),%rdi
lea    0xe09(%rip),%rsi
mov    $0x34,%edx
call   1070 <memcpy@plt>
lea    -0xc0(%rbp),%rdi
lea    0x2b41(%rip),%rsi
mov    $0x68,%edx
call   1070 <memcpy@plt>
movq   $0x0,-0xc8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xd,-0xc(%rbp)
jge    1361 <int_to_roman+0x1d1>
mov    -0x10(%rbp),%eax
movslq -0xc(%rbp),%rcx
cltd
idivl  -0x50(%rbp,%rcx,4)
mov    %eax,-0xcc(%rbp)
movl   $0x0,-0xd0(%rbp)
mov    -0xd0(%rbp),%eax
cmp    -0xcc(%rbp),%eax
jge    133c <int_to_roman+0x1ac>
cmpq   $0x0,-0xc8(%rbp)
jne    1310 <int_to_roman+0x180>
movslq -0xc(%rbp),%rax
mov    -0xc0(%rbp,%rax,8),%rdi
ff
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0xc8(%rbp)
mov    -0xc8(%rbp),%rdi
movslq -0xc(%rbp),%rax
mov    -0xc0(%rbp,%rax,8),%rsi
ff
call   1040 <strcpy@plt>
jmp    1328 <int_to_roman+0x198>
mov    -0xc8(%rbp),%rdi
movslq -0xc(%rbp),%rax
mov    -0xc0(%rbp,%rax,8),%rsi
ff
call   10a0 <strcat@plt>
mov    -0xd0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd0(%rbp)
jmp    12b1 <int_to_roman+0x121>
movslq -0xc(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%ecx
imul   -0xcc(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    128b <int_to_roman+0xfb>
mov    -0xc8(%rbp),%rax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<int_to_roman>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1060 <strlen@plt>
test   %eax,%eax
jle    11c4 <int_to_roman+0x44>
xor    %ebp,%ebp
nopl   0x0(%rax)
movsbl (%rbx,%rbp,1),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     126c <int_to_roman+0xec>
add    $0x1,%rbp
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     11a0 <int_to_roman+0x20>
mov    %rbx,%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
lea    -0xfa0(%r14),%eax
cmp    $0xfffff061,%eax
jb     1286 <int_to_roman+0x106>
xor    %r12d,%r12d
lea    0xe15(%rip),%r13
xor    %ebx,%ebx
jmp    11fa <int_to_roman+0x7a>
nop
add    $0x1,%r12
cmp    $0xd,%r12
je     125a <int_to_roman+0xda>
mov    %r14d,%eax
cltd
idivl  0x0(%r13,%r12,4)
mov    %edx,%r14d
test   %eax,%eax
jle    11f0 <int_to_roman+0x70>
mov    %eax,%ebp
lea    0x2b9d(%rip),%rax
mov    (%rax,%r12,8),%r15
jmp    1230 <int_to_roman+0xb0>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
add    $0xffffffff,%ebp
je     11f0 <int_to_roman+0x70>
test   %rbx,%rbx
jne    1220 <int_to_roman+0xa0>
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
add    $0xffffffff,%ebp
jne    1230 <int_to_roman+0xb0>
jmp    11f0 <int_to_roman+0x70>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdbb(%rip),%rdi
jmp    1273 <int_to_roman+0xf3>

LLVM IR (-O0):
@.str = [29 x i8] c"Error: Enter a whole number\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [42 x i8] c"Error: Enter a number between 1 and 3999\0A\00"
@__const.int_to_roman.ints = [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 = [2 x i8] c"M\00"
@.str.3 = [3 x i8] c"CM\00"
@.str.4 = [2 x i8] c"D\00"
@.str.5 = [3 x i8] c"CD\00"
@.str.6 = [2 x i8] c"C\00"
@.str.7 = [3 x i8] c"XC\00"
@.str.8 = [2 x i8] c"L\00"
@.str.9 = [3 x i8] c"XL\00"
@.str.10 = [2 x i8] c"X\00"
@.str.11 = [3 x i8] c"IX\00"
@.str.12 = [2 x i8] c"V\00"
@.str.13 = [3 x i8] c"IV\00"
@.str.14 = [2 x i8] c"I\00"
@__const.int_to_roman.nums = [13 x i8*] [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), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.14, i32 0, i32 0)]
define i8* @int_to_roman(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [13 x i32]
%6 = alloca [13 x i8*]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
br label %10
10: ; preds = %28, %1
%11 = load i32, i32* %3
%12 = load i8*, i8** %2
%13 = call i32 @strlen(i8* %12)
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %31
15: ; preds = %10
%16 = load i8*, i8** %2
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = call i32 @isdigit(i8 signext %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %15
%24 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %15
br label %28
28: ; preds = %27
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %10
31: ; preds = %10
%32 = load i8*, i8** %2
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp sle i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %31
%37 = load i32, i32* %4
%38 = icmp sge i32 %37, 4000
br i1 %38, label %39, label %43
39: ; preds = %36, %31
%40 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %36
%44 = bitcast [13 x i32]* %5 to i8*
%45 = bitcast [13 x i8*]* %6 to i8*
store i8* null, i8** %7
store i32 0, i32* %3
br label %46
46: ; preds = %96, %43
%47 = load i32, i32* %3
%48 = icmp slt i32 %47, 13
br i1 %48, label %49, label %99
49: ; preds = %46
%50 = load i32, i32* %4
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [13 x i32], [13 x i32]* %5, i64 0, i64 %52
%54 = load i32, i32* %53
%55 = sdiv i32 %50, %54
store i32 %55, i32* %8
store i32 0, i32* %9
br label %56
56: ; preds = %84, %49
%57 = load i32, i32* %9
%58 = load i32, i32* %8
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %87
60: ; preds = %56
%61 = load i8*, i8** %7
%62 = icmp ne i8* %61, null
br i1 %62, label %77, label %63
63: ; preds = %60
%64 = load i32, i32* %3
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [13 x i8*], [13 x i8*]* %6, i64 0, i64 %65
%67 = load i8*, i8** %66
%68 = call i32 @strlen(i8* %67)
%69 = add nsw i32 %68, 1
%70 = call i8* @malloc(i32 %69)
store i8* %70, i8** %7
%71 = load i8*, i8** %7
%72 = load i32, i32* %3
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [13 x i8*], [13 x i8*]* %6, i64 0, i64 %73
%75 = load i8*, i8** %74
%76 = call i32 @strcpy(i8* %71, i8* %75)
br label %84
77: ; preds = %60
%78 = load i8*, i8** %7
%79 = load i32, i32* %3
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [13 x i8*], [13 x i8*]* %6, i64 0, i64 %80
%82 = load i8*, i8** %81
%83 = call i32 @strcat(i8* %78, i8* %82)
br label %84
84: ; preds = %77, %63
%85 = load i32, i32* %9
%86 = add nsw i32 %85, 1
store i32 %86, i32* %9
br label %56
87: ; preds = %56
%88 = load i32, i32* %3
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds [13 x i32], [13 x i32]* %5, i64 0, i64 %89
%91 = load i32, i32* %90
%92 = load i32, i32* %8
%93 = mul nsw i32 %91, %92
%94 = load i32, i32* %4
%95 = sub nsw i32 %94, %93
store i32 %95, i32* %4
br label %96
96: ; preds = %87
%97 = load i32, i32* %3
%98 = add nsw i32 %97, 1
store i32 %98, i32* %3
br label %46
99: ; preds = %46
%100 = load i8*, i8** %7
ret i8* %100
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@__const.int_to_roman.ints = [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 = [2 x i8] c"M\00"
@.str.3 = [3 x i8] c"CM\00"
@.str.4 = [2 x i8] c"D\00"
@.str.5 = [3 x i8] c"CD\00"
@.str.6 = [2 x i8] c"C\00"
@.str.7 = [3 x i8] c"XC\00"
@.str.8 = [2 x i8] c"L\00"
@.str.9 = [3 x i8] c"XL\00"
@.str.10 = [2 x i8] c"X\00"
@.str.11 = [3 x i8] c"IX\00"
@.str.12 = [2 x i8] c"V\00"
@.str.13 = [3 x i8] c"IV\00"
@.str.14 = [2 x i8] c"I\00"
@__const.int_to_roman.nums = [13 x i8*] [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), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.14, i32 0, i32 0)]
@str = [41 x i8] c"Error: Enter a number between 1 and 3999\00"
@str.15 = [28 x i8] c"Error: Enter a whole number\00"
define i8* @int_to_roman(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %8, label %19
4: ; preds = %8
%5 = tail call i32 @strlen(i8* %0)
%6 = sext i32 %5 to i64
%7 = icmp slt i64 %14, %6
br i1 %7, label %8, label %19
8: ; preds = %1, %4
%9 = phi i64 [ %14, %4 ], [ 0, %1 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = tail call i32 @isdigit(i8 signext %11)
%13 = icmp eq i32 %12, 0
%14 = add nuw nsw i64 %9, 1
br i1 %13, label %15, label %4
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.15, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %4, %1
%20 = tail call i32 @atoi(i8* %0)
%21 = add i32 %20, -4000
%22 = icmp ult i32 %21, -3999
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %19, %54
%28 = phi i64 [ %57, %54 ], [ 0, %19 ]
%29 = phi i8* [ %55, %54 ], [ null, %19 ]
%30 = phi i32 [ %34, %54 ], [ %20, %19 ]
%31 = getelementptr inbounds [13 x i32], [13 x i32]* @__const.int_to_roman.ints, i64 0, i64 %28
%32 = load i32, i32* %31
%33 = sdiv i32 %30, %32
%34 = srem i32 %30, %32
%35 = icmp sgt i32 %33, 0
br i1 %35, label %36, label %54
36: ; preds = %27
%37 = getelementptr inbounds [13 x i8*], [13 x i8*]* @__const.int_to_roman.nums, i64 0, i64 %28
%38 = load i8*, i8** %37
br label %39
39: ; preds = %36, %50
%40 = phi i32 [ 0, %36 ], [ %52, %50 ]
%41 = phi i8* [ %29, %36 ], [ %51, %50 ]
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %48
43: ; preds = %39
%44 = tail call i32 @strlen(i8* %38)
%45 = add nsw i32 %44, 1
%46 = tail call i8* @malloc(i32 %45)
%47 = tail call i32 @strcpy(i8* %46, i8* %38)
br label %50
48: ; preds = %39
%49 = tail call i32 @strcat(i8* %41, i8* %38)
br label %50
50: ; preds = %48, %43
%51 = phi i8* [ %41, %48 ], [ %46, %43 ]
%52 = add nuw nsw i32 %40, 1
%53 = icmp eq i32 %52, %33
br i1 %53, label %54, label %39
54: ; preds = %50, %27
%55 = phi i8* [ %29, %27 ], [ %51, %50 ]
%56 = mul nsw i32 %33, %32
%57 = add nuw nsw i64 %28, 1
%58 = icmp eq i64 %57, 13
br i1 %58, label %59, label %27
59: ; preds = %54
ret i8* %55
}