; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (13)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define dso_local i32 @romanToInt(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %4

4:                                                ; preds = %34, %21, %1
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = load i8, ptr %5, align 1, !tbaa !11
  %7 = sext i8 %6 to i32
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %15

9:                                                ; preds = %4
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %10, i64 1
  %12 = load i8, ptr %11, align 1, !tbaa !11
  %13 = sext i8 %12 to i32
  %14 = icmp ne i32 %13, 0
  br label %15

15:                                               ; preds = %9, %4
  %16 = phi i1 [ false, %4 ], [ %14, %9 ]
  br i1 %16, label %17, label %42

17:                                               ; preds = %15
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = call i32 @is_special(ptr noundef %18)
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %34

21:                                               ; preds = %17
  %22 = load i32, ptr %3, align 4, !tbaa !9
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = load i8, ptr %23, align 1, !tbaa !11
  %25 = call i32 @rtoi(i8 noundef signext %24)
  %26 = sub nsw i32 %22, %25
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %27, i64 1
  %29 = load i8, ptr %28, align 1, !tbaa !11
  %30 = call i32 @rtoi(i8 noundef signext %29)
  %31 = add nsw i32 %26, %30
  store i32 %31, ptr %3, align 4, !tbaa !9
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 2
  store ptr %33, ptr %2, align 8, !tbaa !5
  br label %4, !llvm.loop !12

34:                                               ; preds = %17
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = call i32 @rtoi(i8 noundef signext %36)
  %38 = load i32, ptr %3, align 4, !tbaa !9
  %39 = add nsw i32 %38, %37
  store i32 %39, ptr %3, align 4, !tbaa !9
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i32 1
  store ptr %41, ptr %2, align 8, !tbaa !5
  br label %4, !llvm.loop !12

42:                                               ; preds = %15
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = load i8, ptr %43, align 1, !tbaa !11
  %45 = sext i8 %44 to i32
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %53

47:                                               ; preds = %42
  %48 = load i32, ptr %3, align 4, !tbaa !9
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = load i8, ptr %49, align 1, !tbaa !11
  %51 = call i32 @rtoi(i8 noundef signext %50)
  %52 = add nsw i32 %48, %51
  br label %55

53:                                               ; preds = %42
  %54 = load i32, ptr %3, align 4, !tbaa !9
  br label %55

55:                                               ; preds = %53, %47
  %56 = phi i32 [ %52, %47 ], [ %54, %53 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %56
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare i32 @is_special(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare i32 @rtoi(i8 noundef signext) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define dso_local i32 @romanToInt(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i8, ptr %0, align 1, !tbaa !5
  %.not19 = icmp eq i8 %2, 0
  br i1 %.not19, label %.critedge.thread, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 1
  %4 = load i8, ptr %3, align 1, !tbaa !5
  %.not1428 = icmp eq i8 %4, 0
  br i1 %.not1428, label %.critedge, label %.lr.ph31

.lr.ph:                                           ; preds = %.backedge
  %5 = getelementptr inbounds i8, ptr %.013.be, i64 1
  %6 = load i8, ptr %5, align 1, !tbaa !5
  %.not14 = icmp eq i8 %6, 0
  br i1 %.not14, label %.critedge, label %.lr.ph31, !llvm.loop !8

.lr.ph31:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %7 = phi ptr [ %5, %.lr.ph ], [ %3, %.lr.ph.preheader ]
  %.0132030 = phi ptr [ %.013.be, %.lr.ph ], [ %0, %.lr.ph.preheader ]
  %.02129 = phi i32 [ %.0.be, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %8 = tail call i32 @is_special(ptr noundef nonnull %.0132030)
  %.not16 = icmp eq i32 %8, 0
  %9 = load i8, ptr %.0132030, align 1, !tbaa !5
  %10 = tail call i32 @rtoi(i8 noundef signext %9)
  br i1 %.not16, label %18, label %11

11:                                               ; preds = %.lr.ph31
  %12 = sub i32 %.02129, %10
  %13 = load i8, ptr %7, align 1, !tbaa !5
  %14 = tail call i32 @rtoi(i8 noundef signext %13)
  %15 = add nsw i32 %12, %14
  %16 = getelementptr inbounds i8, ptr %.0132030, i64 2
  br label %.backedge

.backedge:                                        ; preds = %11, %18
  %.013.be = phi ptr [ %16, %11 ], [ %7, %18 ]
  %.0.be = phi i32 [ %15, %11 ], [ %19, %18 ]
  %17 = load i8, ptr %.013.be, align 1, !tbaa !5
  %.not = icmp eq i8 %17, 0
  br i1 %.not, label %.critedge.thread, label %.lr.ph, !llvm.loop !8

18:                                               ; preds = %.lr.ph31
  %19 = add nsw i32 %10, %.02129
  br label %.backedge

.critedge:                                        ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa = phi i8 [ %2, %.lr.ph.preheader ], [ %17, %.lr.ph ]
  %.021.lcssa = phi i32 [ 0, %.lr.ph.preheader ], [ %.0.be, %.lr.ph ]
  %20 = tail call i32 @rtoi(i8 noundef signext %.lcssa)
  %21 = add nsw i32 %20, %.021.lcssa
  br label %.critedge.thread

.critedge.thread:                                 ; preds = %.backedge, %1, %.critedge
  %22 = phi i32 [ %21, %.critedge ], [ 0, %1 ], [ %.0.be, %.backedge ]
  ret i32 %22
}

; Function Attrs: inlinehint nounwind uwtable
declare i32 @is_special(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare i32 @rtoi(i8 noundef signext) local_unnamed_addr #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { inlinehint nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass