#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 /*<<< orphan*/ values;

/* Variables and functions */
char *calloc(int, int);
int extract_roman_value(int const, int, char *);
int /*<<< orphan*/ is_valid_integer(int);
const char *integer_to_roman_numeral(int integer) {
  if (!is_valid_integer(integer))
    return NULL;
  char *result = calloc(64, sizeof(char));
  int remaining = integer;
  const int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  for (int i = 0; i < sizeof(values) / sizeof(*values); i++) {
    remaining = extract_roman_value(values[i], remaining, result);
  }
  return result;
}


x86 assembly (clang, -O0):
<integer_to_roman_numeral>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <is_valid_integer@plt>
cmp    $0x0,%eax
jne    1169 <integer_to_roman_numeral+0x29>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <integer_to_roman_numeral+0x99>
mov    $0x40,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe73(%rip),%rsi
mov    $0x34,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x54(%rbp)
movslq -0x54(%rbp),%rax
cmp    $0xd,%rax
jae    11d1 <integer_to_roman_numeral+0x91>
movslq -0x54(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%edi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1060 <extract_roman_value@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    119e <integer_to_roman_numeral+0x5e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integer_to_roman_numeral>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
call   1030 <is_valid_integer@plt>
test   %eax,%eax
je     1219 <integer_to_roman_numeral+0xe9>
mov    $0x40,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
mov    $0x3e8,%edi
mov    %ebp,%esi
mov    %rax,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x384,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x1f4,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x190,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x64,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x5a,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x32,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x28,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0xa,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x9,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x5,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x4,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
mov    $0x1,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1050 <extract_roman_value@plt>
jmp    121b <integer_to_roman_numeral+0xeb>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.integer_to_roman_numeral.values = [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]
define i8* @integer_to_roman_numeral(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [13 x i32]
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @is_valid_integer(i32 %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %1
store i8* null, i8** %2
br label %33
12: ; preds = %1
%13 = call i8* @calloc(i32 64, i32 1)
store i8* %13, i8** %4
%14 = load i32, i32* %3
store i32 %14, i32* %5
%15 = bitcast [13 x i32]* %6 to i8*
store i32 0, i32* %7
br label %16
16: ; preds = %28, %12
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = icmp ult i64 %18, 13
br i1 %19, label %20, label %31
20: ; preds = %16
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [13 x i32], [13 x i32]* %6, i64 0, i64 %22
%24 = load i32, i32* %23
%25 = load i32, i32* %5
%26 = load i8*, i8** %4
%27 = call i32 @extract_roman_value(i32 %24, i32 %25, i8* %26)
store i32 %27, i32* %5
br label %28
28: ; preds = %20
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
br label %16
31: ; preds = %16
%32 = load i8*, i8** %4
store i8* %32, i8** %2
br label %33
33: ; preds = %31, %11
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
define i8* @integer_to_roman_numeral(i32 %0) {
%2 = tail call i32 @is_valid_integer(i32 %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %19, label %4
4: ; preds = %1
%5 = tail call dereferenceable_or_null(64) i8* @calloc(i32 64, i32 1)
%6 = tail call i32 @extract_roman_value(i32 1000, i32 %0, i8* %5)
%7 = tail call i32 @extract_roman_value(i32 900, i32 %6, i8* %5)
%8 = tail call i32 @extract_roman_value(i32 500, i32 %7, i8* %5)
%9 = tail call i32 @extract_roman_value(i32 400, i32 %8, i8* %5)
%10 = tail call i32 @extract_roman_value(i32 100, i32 %9, i8* %5)
%11 = tail call i32 @extract_roman_value(i32 90, i32 %10, i8* %5)
%12 = tail call i32 @extract_roman_value(i32 50, i32 %11, i8* %5)
%13 = tail call i32 @extract_roman_value(i32 40, i32 %12, i8* %5)
%14 = tail call i32 @extract_roman_value(i32 10, i32 %13, i8* %5)
%15 = tail call i32 @extract_roman_value(i32 9, i32 %14, i8* %5)
%16 = tail call i32 @extract_roman_value(i32 5, i32 %15, i8* %5)
%17 = tail call i32 @extract_roman_value(i32 4, i32 %16, i8* %5)
%18 = tail call i32 @extract_roman_value(i32 1, i32 %17, i8* %5)
br label %19
19: ; preds = %4, %1
%20 = phi i8* [ null, %1 ], [ %5, %4 ]
ret i8* %20
}