; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 20. Dynamic Programming (FormAPalindrome)

; 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: mustprogress uwtable
define dso_local noundef i32 @_Z3lcsiiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES4_(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !9
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  %19 = zext i32 %18 to i64
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  %22 = zext i32 %21 to i64
  %23 = call ptr @llvm.stacksave.p0()
  store ptr %23, ptr %9, align 8
  %24 = mul nuw i64 %19, %22
  %25 = alloca i32, i64 %24, align 16
  store i64 %19, ptr %10, align 8
  store i64 %22, ptr %11, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %56, %4
  %27 = load i32, ptr %12, align 4, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = icmp sle i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  store i32 2, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %59

31:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  store i32 0, ptr %14, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %52, %31
  %33 = load i32, ptr %14, align 4, !tbaa !5
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = icmp sle i32 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  store i32 5, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  br label %55

37:                                               ; preds = %32
  %38 = load i32, ptr %12, align 4, !tbaa !5
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %43, label %40

40:                                               ; preds = %37
  %41 = load i32, ptr %14, align 4, !tbaa !5
  %42 = icmp eq i32 %41, 0
  br i1 %42, label %43, label %51

43:                                               ; preds = %40, %37
  %44 = load i32, ptr %12, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = mul nsw i64 %45, %22
  %47 = getelementptr inbounds i32, ptr %25, i64 %46
  %48 = load i32, ptr %14, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i32, ptr %47, i64 %49
  store i32 0, ptr %50, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %43, %40
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %14, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %14, align 4, !tbaa !5
  br label %32, !llvm.loop !11

55:                                               ; preds = %36
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %12, align 4, !tbaa !5
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %12, align 4, !tbaa !5
  br label %26, !llvm.loop !13

59:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  store i32 1, ptr %15, align 4, !tbaa !5
  br label %60

60:                                               ; preds = %135, %59
  %61 = load i32, ptr %15, align 4, !tbaa !5
  %62 = load i32, ptr %5, align 4, !tbaa !5
  %63 = icmp sle i32 %61, %62
  br i1 %63, label %65, label %64

64:                                               ; preds = %60
  store i32 8, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  br label %138

65:                                               ; preds = %60
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #5
  store i32 1, ptr %16, align 4, !tbaa !5
  br label %66

66:                                               ; preds = %131, %65
  %67 = load i32, ptr %16, align 4, !tbaa !5
  %68 = load i32, ptr %6, align 4, !tbaa !5
  %69 = icmp sle i32 %67, %68
  br i1 %69, label %71, label %70

70:                                               ; preds = %66
  store i32 11, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #5
  br label %134

71:                                               ; preds = %66
  %72 = load i32, ptr %15, align 4, !tbaa !5
  %73 = sub nsw i32 %72, 1
  %74 = sext i32 %73 to i64
  %75 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %2, i64 noundef %74)
  %76 = load i8, ptr %75, align 1, !tbaa !14
  %77 = sext i8 %76 to i32
  %78 = load i32, ptr %16, align 4, !tbaa !5
  %79 = sub nsw i32 %78, 1
  %80 = sext i32 %79 to i64
  %81 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %3, i64 noundef %80)
  %82 = load i8, ptr %81, align 1, !tbaa !14
  %83 = sext i8 %82 to i32
  %84 = icmp eq i32 %77, %83
  br i1 %84, label %85, label %104

85:                                               ; preds = %71
  %86 = load i32, ptr %15, align 4, !tbaa !5
  %87 = sub nsw i32 %86, 1
  %88 = sext i32 %87 to i64
  %89 = mul nsw i64 %88, %22
  %90 = getelementptr inbounds i32, ptr %25, i64 %89
  %91 = load i32, ptr %16, align 4, !tbaa !5
  %92 = sub nsw i32 %91, 1
  %93 = sext i32 %92 to i64
  %94 = getelementptr inbounds i32, ptr %90, i64 %93
  %95 = load i32, ptr %94, align 4, !tbaa !5
  %96 = add nsw i32 1, %95
  %97 = load i32, ptr %15, align 4, !tbaa !5
  %98 = sext i32 %97 to i64
  %99 = mul nsw i64 %98, %22
  %100 = getelementptr inbounds i32, ptr %25, i64 %99
  %101 = load i32, ptr %16, align 4, !tbaa !5
  %102 = sext i32 %101 to i64
  %103 = getelementptr inbounds i32, ptr %100, i64 %102
  store i32 %96, ptr %103, align 4, !tbaa !5
  br label %130

104:                                              ; preds = %71
  %105 = load i32, ptr %15, align 4, !tbaa !5
  %106 = sub nsw i32 %105, 1
  %107 = sext i32 %106 to i64
  %108 = mul nsw i64 %107, %22
  %109 = getelementptr inbounds i32, ptr %25, i64 %108
  %110 = load i32, ptr %16, align 4, !tbaa !5
  %111 = sext i32 %110 to i64
  %112 = getelementptr inbounds i32, ptr %109, i64 %111
  %113 = load i32, ptr %15, align 4, !tbaa !5
  %114 = sext i32 %113 to i64
  %115 = mul nsw i64 %114, %22
  %116 = getelementptr inbounds i32, ptr %25, i64 %115
  %117 = load i32, ptr %16, align 4, !tbaa !5
  %118 = sub nsw i32 %117, 1
  %119 = sext i32 %118 to i64
  %120 = getelementptr inbounds i32, ptr %116, i64 %119
  %121 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %112, ptr noundef nonnull align 4 dereferenceable(4) %120)
  %122 = load i32, ptr %121, align 4, !tbaa !5
  %123 = load i32, ptr %15, align 4, !tbaa !5
  %124 = sext i32 %123 to i64
  %125 = mul nsw i64 %124, %22
  %126 = getelementptr inbounds i32, ptr %25, i64 %125
  %127 = load i32, ptr %16, align 4, !tbaa !5
  %128 = sext i32 %127 to i64
  %129 = getelementptr inbounds i32, ptr %126, i64 %128
  store i32 %122, ptr %129, align 4, !tbaa !5
  br label %130

130:                                              ; preds = %104, %85
  br label %131

131:                                              ; preds = %130
  %132 = load i32, ptr %16, align 4, !tbaa !5
  %133 = add nsw i32 %132, 1
  store i32 %133, ptr %16, align 4, !tbaa !5
  br label %66, !llvm.loop !15

134:                                              ; preds = %70
  br label %135

135:                                              ; preds = %134
  %136 = load i32, ptr %15, align 4, !tbaa !5
  %137 = add nsw i32 %136, 1
  store i32 %137, ptr %15, align 4, !tbaa !5
  br label %60, !llvm.loop !16

138:                                              ; preds = %64
  %139 = load i32, ptr %5, align 4, !tbaa !5
  %140 = sext i32 %139 to i64
  %141 = mul nsw i64 %140, %22
  %142 = getelementptr inbounds i32, ptr %25, i64 %141
  %143 = load i32, ptr %6, align 4, !tbaa !5
  %144 = sext i32 %143 to i64
  %145 = getelementptr inbounds i32, ptr %142, i64 %144
  %146 = load i32, ptr %145, align 4, !tbaa !5
  store i32 1, ptr %13, align 4
  %147 = load ptr, ptr %9, align 8
  call void @llvm.stackrestore.p0(ptr %147)
  ret i32 %146
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

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

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

; Function Attrs: mustprogress nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) #3 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

attributes #0 = { mustprogress 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 }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress 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 #4 = { inlinehint mustprogress 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 #5 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !12}


; 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: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z3lcsiiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES4_(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = add i32 %0, 1
  %6 = zext i32 %5 to i64
  %7 = add i32 %1, 1
  %8 = zext i32 %7 to i64
  %9 = mul nuw i64 %8, %6
  %10 = alloca i32, i64 %9, align 16
  %.not44 = icmp slt i32 %0, 0
  br i1 %.not44, label %._crit_edge51, label %.preheader41.lr.ph

.preheader41.lr.ph:                               ; preds = %4
  %.not3942 = icmp slt i32 %1, 0
  br i1 %.not3942, label %.preheader40, label %.preheader41.preheader

.preheader41.preheader:                           ; preds = %.preheader41.lr.ph
  %11 = shl nuw nsw i64 %8, 2
  %smax = tail call i32 @llvm.smax.i32(i32 %5, i32 1)
  %wide.trip.count61 = zext nneg i32 %smax to i64
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %10, i8 0, i64 %11, i1 false), !tbaa !5
  %exitcond62.peel = icmp slt i32 %5, 2
  br i1 %exitcond62.peel, label %.preheader40, label %.preheader41

.preheader41:                                     ; preds = %.preheader41.preheader, %.preheader41
  %indvar = phi i64 [ %indvar.next, %.preheader41 ], [ 1, %.preheader41.preheader ]
  %12 = mul nuw nsw i64 %indvar, %8
  %13 = getelementptr inbounds i32, ptr %10, i64 %12
  store i32 0, ptr %13, align 4, !tbaa !5
  %indvar.next = add nuw nsw i64 %indvar, 1
  %exitcond62 = icmp eq i64 %indvar.next, %wide.trip.count61
  br i1 %exitcond62, label %.preheader40, label %.preheader41, !llvm.loop !9

.preheader40:                                     ; preds = %.preheader41.preheader, %.preheader41, %.preheader41.lr.ph
  %.not3749 = icmp eq i32 %0, 0
  %.not3846 = icmp slt i32 %1, 1
  %or.cond = or i1 %.not3749, %.not3846
  br i1 %or.cond, label %._crit_edge51, label %.preheader

.preheader:                                       ; preds = %.preheader40, %._crit_edge48
  %indvars.iv68 = phi i64 [ %indvars.iv.next69, %._crit_edge48 ], [ 1, %.preheader40 ]
  %14 = add nsw i64 %indvars.iv68, -1
  %15 = mul nuw nsw i64 %14, %8
  %16 = getelementptr inbounds i32, ptr %10, i64 %15
  %17 = mul nuw nsw i64 %indvars.iv68, %8
  %18 = getelementptr inbounds i32, ptr %10, i64 %17
  br label %25

._crit_edge51:                                    ; preds = %._crit_edge48, %4, %.preheader40
  %19 = sext i32 %0 to i64
  %20 = mul nsw i64 %8, %19
  %21 = getelementptr inbounds i32, ptr %10, i64 %20
  %22 = sext i32 %1 to i64
  %23 = getelementptr inbounds i32, ptr %21, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  ret i32 %24

._crit_edge48:                                    ; preds = %41
  %indvars.iv.next69 = add nuw nsw i64 %indvars.iv68, 1
  %exitcond72.not = icmp eq i64 %indvars.iv.next69, %6
  br i1 %exitcond72.not, label %._crit_edge51, label %.preheader, !llvm.loop !12

25:                                               ; preds = %.preheader, %41
  %indvars.iv63 = phi i64 [ 1, %.preheader ], [ %indvars.iv.next64, %41 ]
  %26 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %2, i64 noundef %14)
  %27 = load i8, ptr %26, align 1, !tbaa !13
  %28 = add nsw i64 %indvars.iv63, -1
  %29 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %3, i64 noundef %28)
  %30 = load i8, ptr %29, align 1, !tbaa !13
  %31 = icmp eq i8 %27, %30
  br i1 %31, label %32, label %36

32:                                               ; preds = %25
  %33 = getelementptr inbounds i32, ptr %16, i64 %28
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  br label %41

36:                                               ; preds = %25
  %37 = getelementptr inbounds i32, ptr %16, i64 %indvars.iv63
  %38 = getelementptr inbounds i32, ptr %18, i64 %28
  %39 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %37, ptr noundef nonnull align 4 dereferenceable(4) %38)
  %40 = load i32, ptr %39, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %32, %36
  %.sink = phi i32 [ %35, %32 ], [ %40, %36 ]
  %42 = getelementptr inbounds i32, ptr %18, i64 %indvars.iv63
  store i32 %.sink, ptr %42, align 4, !tbaa !5
  %indvars.iv.next64 = add nuw nsw i64 %indvars.iv63, 1
  %exitcond67.not = icmp eq i64 %indvars.iv.next64, %8
  br i1 %exitcond67.not, label %._crit_edge48, label %25, !llvm.loop !14
}

; Function Attrs: mustprogress nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #3

attributes #0 = { mustprogress 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 mustprogress 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10, !11}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.peeled.count", i32 1}
!12 = distinct !{!12, !10}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopFullUnrollPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass