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

/* Variables and functions */
int REG_EXTENDED;
int REG_NOSUB;
int /*<<< orphan*/ regcomp(int /*<<< orphan*/ *, char *, int);
int regexec(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/,
            int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
bool rnc_valid(const char *num) {
  if (!*num) {
    return false;
  } else {
    regex_t re;
    regcomp(&re,
            "^"
            "(M(MM?)?)?"
            "(CM|CD|D|D?C(CC?)?)?"
            "(XC|XL|L|L?X(XX?)?)?"
            "(IX|IV|V|V?I(II?)?)?"
            "$",
            REG_EXTENDED | REG_NOSUB);
    int match = regexec(&re, num, 0, NULL, 0);
    regfree(&re);
    return match == 0;
  }
}


x86 assembly (clang, -O0):
<rnc_valid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1155 <rnc_valid+0x25>
movl   $0x0,-0x4(%rbp)
jmp    11a9 <rnc_valid+0x79>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
mov    0x2e7b(%rip),%rax
or     (%rax),%edx
lea    -0x14(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1040 <regcomp@plt>
mov    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdi
xor    %r8d,%r8d
xor    %eax,%eax
mov    %eax,%ecx
mov    %r8d,%edx
call   1050 <regexec@plt>
mov    %eax,-0x18(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <regfree@plt>
cmpl   $0x0,-0x18(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rnc_valid>:
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,(%rdi)
je     1187 <rnc_valid+0x57>
mov    %rdi,%rbx
mov    0x2e95(%rip),%rax
mov    0x2e96(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xeab(%rip),%rsi
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1040 <regcomp@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <regexec@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <regfree@plt>
xor    %eax,%eax
test   %ebx,%ebx
sete   %al
jmp    1189 <rnc_valid+0x59>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [73 x i8] c"^(M(MM?)?)?(CM|CD|D|D?C(CC?)?)?(XC|XL|L|L?X(XX?)?)?(IX|IV|V|V?I(II?)?)?$\00"
@REG_EXTENDED = global i32 0
@REG_NOSUB = global i32 0
define i32 @rnc_valid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i8, i8* %6
%8 = icmp ne i8 %7, 0
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 0, i32* %2
br label %21
10: ; preds = %1
%11 = load i32, i32* @REG_EXTENDED
%12 = load i32, i32* @REG_NOSUB
%13 = or i32 %11, %12
%14 = call i32 @regcomp(i32* %4, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i8*, i8** %3
%16 = call i32 @regexec(i32* %4, i8* %15, i32 0, i32* null, i32 0)
store i32 %16, i32* %5
%17 = call i32 @regfree(i32* %4)
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 0
%20 = zext i1 %19 to i32
store i32 %20, i32* %2
br label %21
21: ; preds = %10, %9
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@.str = [73 x i8] c"^(M(MM?)?)?(CM|CD|D|D?C(CC?)?)?(XC|XL|L|L?X(XX?)?)?(IX|IV|V|V?I(II?)?)?$\00"
@REG_EXTENDED = global i32 0
@REG_NOSUB = global i32 0
define i32 @rnc_valid(i8* %0) {
%2 = alloca i32
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %15, label %5
5: ; preds = %1
%6 = bitcast i32* %2 to i8*
%7 = load i32, i32* @REG_EXTENDED
%8 = load i32, i32* @REG_NOSUB
%9 = or i32 %8, %7
%10 = call i32 @regcomp(i32* %2, i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 @regexec(i32* %2, i8* %0, i32 0, i32* null, i32 0)
%12 = call i32 @regfree(i32* %2)
%13 = icmp eq i32 %11, 0
%14 = zext i1 %13 to i32
br label %15
15: ; preds = %1, %5
%16 = phi i32 [ %14, %5 ], [ 0, %1 ]
ret i32 %16
}