; LLVM IR from div-bargali/Data-Structures-and-Algorithms :: C/Algorithms/Dynamic-Programming (LonestCommonSubsequence)

; 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
declare dso_local i32 @max(i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @LCS(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  %10 = load i32, ptr %8, align 4, !tbaa !9
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %15, label %12

12:                                               ; preds = %4
  %13 = load i32, ptr %9, align 4, !tbaa !9
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %12, %4
  store i32 0, ptr %5, align 4
  br label %55

16:                                               ; preds = %12
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = sub nsw i32 %18, 1
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i8, ptr %17, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = sext i8 %22 to i32
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = load i32, ptr %9, align 4, !tbaa !9
  %26 = sub nsw i32 %25, 1
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %24, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !11
  %30 = sext i8 %29 to i32
  %31 = icmp eq i32 %23, %30
  br i1 %31, label %32, label %41

32:                                               ; preds = %16
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = sub nsw i32 %35, 1
  %37 = load i32, ptr %9, align 4, !tbaa !9
  %38 = sub nsw i32 %37, 1
  %39 = call i32 @LCS(ptr noundef %33, ptr noundef %34, i32 noundef %36, i32 noundef %38)
  %40 = add nsw i32 1, %39
  store i32 %40, ptr %5, align 4
  br label %55

41:                                               ; preds = %16
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = sub nsw i32 %44, 1
  %46 = load i32, ptr %9, align 4, !tbaa !9
  %47 = call i32 @LCS(ptr noundef %42, ptr noundef %43, i32 noundef %45, i32 noundef %46)
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = sub nsw i32 %51, 1
  %53 = call i32 @LCS(ptr noundef %48, ptr noundef %49, i32 noundef %50, i32 noundef %52)
  %54 = call i32 @max(i32 noundef %47, i32 noundef %53)
  store i32 %54, ptr %5, align 4
  br label %55

55:                                               ; preds = %41, %32, %15
  %56 = load i32, ptr %5, align 4
  ret i32 %56
}

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" }

!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}


; 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
declare dso_local i32 @max(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @LCS(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %2, 0
  %6 = icmp eq i32 %3, 0
  %or.cond29 = or i1 %5, %6
  br i1 %or.cond29, label %common.ret65, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %7 = sext i32 %3 to i64
  %8 = sext i32 %2 to i64
  %9 = add i32 %3, -1
  %10 = add i32 %2, -1
  %umin = tail call i32 @llvm.umin.i32(i32 %9, i32 %10)
  %11 = add nuw i32 %umin, 1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %tailrecurse
  %indvars.iv40 = phi i64 [ %8, %.lr.ph.preheader ], [ %indvars.iv.next41, %tailrecurse ]
  %indvars.iv = phi i64 [ %7, %.lr.ph.preheader ], [ %indvars.iv.next, %tailrecurse ]
  %accumulator.tr30 = phi i32 [ 0, %.lr.ph.preheader ], [ %17, %tailrecurse ]
  %indvars.iv.next41 = add nsw i64 %indvars.iv40, -1
  %12 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next41
  %13 = load i8, ptr %12, align 1, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %14 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.next
  %15 = load i8, ptr %14, align 1, !tbaa !5
  %16 = icmp eq i8 %13, %15
  br i1 %16, label %tailrecurse, label %18

tailrecurse:                                      ; preds = %.lr.ph
  %17 = add nuw i32 %accumulator.tr30, 1
  %exitcond = icmp eq i32 %accumulator.tr30, %umin
  br i1 %exitcond, label %common.ret65, label %.lr.ph

common.ret65:                                     ; preds = %4, %tailrecurse, %18
  %common.ret65.op = phi i32 [ %accumulator.ret.tr, %18 ], [ 0, %4 ], [ %11, %tailrecurse ]
  ret i32 %common.ret65.op

18:                                               ; preds = %.lr.ph
  %19 = trunc nsw i64 %indvars.iv40 to i32
  %20 = trunc nsw i64 %indvars.iv.next41 to i32
  %21 = trunc nsw i64 %indvars.iv to i32
  %22 = trunc nsw i64 %indvars.iv.next to i32
  %23 = tail call i32 @LCS(ptr noundef nonnull %0, ptr noundef nonnull %1, i32 noundef %20, i32 noundef %21)
  %24 = tail call i32 @LCS(ptr noundef nonnull %0, ptr noundef nonnull %1, i32 noundef %19, i32 noundef %22)
  %25 = tail call i32 @max(i32 noundef %23, i32 noundef %24)
  %accumulator.ret.tr = add nsw i32 %25, %accumulator.tr30
  br label %common.ret65
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #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 speculatable willreturn memory(none) }

!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"}


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