#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 roman_digit_value(char const);
int strlen(char const *);
int roman_to_arabic(const char *roman) {
  int arabic = 0;
  int prev_digit = 0;
  const char *tail_ptr = roman + strlen(roman) - 1;
  while (tail_ptr >= roman) {
    int digit = roman_digit_value(*tail_ptr);
    if (digit >= prev_digit)
      arabic += digit;
    else
      arabic -= digit;
    prev_digit = digit;
    tail_ptr--;
  }
  return arabic;
}


x86 assembly (clang, -O0):
<roman_to_arabic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jb     11b8 <roman_to_arabic+0x98>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <roman_digit_value@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jl     1196 <roman_to_arabic+0x76>
mov    -0x1c(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11a1 <roman_to_arabic+0x81>
mov    -0x1c(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    115f <roman_to_arabic+0x3f>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<roman_to_arabic>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
cltq
lea    (%r14,%rax,1),%rbx
add    $0xffffffffffffffff,%rbx
xor    %ebp,%ebp
cmp    %r14,%rbx
jb     1170 <roman_to_arabic+0x50>
xor    %ebp,%ebp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
movsbl (%rbx),%edi
call   1040 <roman_digit_value@plt>
mov    %eax,%ecx
neg    %ecx
cmp    %r15d,%eax
cmovge %eax,%ecx
add    %ecx,%ebp
add    $0xffffffffffffffff,%rbx
mov    %eax,%r15d
cmp    %r14,%rbx
jae    1150 <roman_to_arabic+0x30>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @roman_to_arabic(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
%7 = load i8*, i8** %2
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %7, i64 %10
%12 = getelementptr inbounds i8, i8* %11, i64 -1
store i8* %12, i8** %5
br label %13
13: ; preds = %32, %1
%14 = load i8*, i8** %5
%15 = load i8*, i8** %2
%16 = icmp uge i8* %14, %15
br i1 %16, label %17, label %36
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = call i32 @roman_digit_value(i8 signext %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = load i32, i32* %4
%23 = icmp sge i32 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i32, i32* %6
%26 = load i32, i32* %3
%27 = add nsw i32 %26, %25
store i32 %27, i32* %3
br label %32
28: ; preds = %17
%29 = load i32, i32* %6
%30 = load i32, i32* %3
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %3
br label %32
32: ; preds = %28, %24
%33 = load i32, i32* %6
store i32 %33, i32* %4
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i32 -1
store i8* %35, i8** %5
br label %13
36: ; preds = %13
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
define i32 @roman_to_arabic(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = sext i32 %2 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = getelementptr inbounds i8, i8* %4, i64 -1
%6 = icmp ult i8* %5, %0
br i1 %6, label %19, label %7
7: ; preds = %1, %7
%8 = phi i8* [ %17, %7 ], [ %5, %1 ]
%9 = phi i32 [ %16, %7 ], [ 0, %1 ]
%10 = phi i32 [ %12, %7 ], [ 0, %1 ]
%11 = load i8, i8* %8
%12 = tail call i32 @roman_digit_value(i8 signext %11)
%13 = icmp slt i32 %12, %10
%14 = sub i32 0, %12
%15 = select i1 %13, i32 %14, i32 %12
%16 = add i32 %15, %9
%17 = getelementptr inbounds i8, i8* %8, i64 -1
%18 = icmp ult i8* %17, %0
br i1 %18, label %19, label %7
19: ; preds = %7, %1
%20 = phi i32 [ 0, %1 ], [ %16, %7 ]
ret i32 %20
}