; LLVM IR from VAR-solutions/Algorithms :: Dynamic Programming/max_subarray_sum.ll (Z14maxSubArraySumPii)

; 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 nounwind uwtable
define dso_local noundef i32 @_Z14maxSubArraySumPii(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 -2147483648, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %38, %2
  %12 = load i32, ptr %10, align 4, !tbaa !9
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %41

16:                                               ; preds = %11
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load i32, ptr %10, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i32, ptr %17, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = add nsw i32 %22, %21
  store i32 %23, ptr %6, align 4, !tbaa !9
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %27, label %31

27:                                               ; preds = %16
  %28 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %28, ptr %5, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %29, ptr %7, align 4, !tbaa !9
  %30 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %30, ptr %8, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %27, %16
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = icmp slt i32 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %31
  store i32 0, ptr %6, align 4, !tbaa !9
  %35 = load i32, ptr %10, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %9, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %34, %31
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %10, align 4, !tbaa !9
  br label %11, !llvm.loop !11

41:                                               ; preds = %15
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = load i32, ptr %7, align 4, !tbaa !9
  %44 = sub nsw i32 %42, %43
  %45 = add nsw i32 %44, 1
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %45
}

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef range(i32 -2147483647, -2147483648) i32 @_Z14maxSubArraySumPii(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %4 = icmp eq i32 %1, 1
  br i1 %4, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.118.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.118.1, %.lr.ph ]
  %.116.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.116.1, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.lr.ph ]
  %.01429.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %.1.1, %.lr.ph ]
  %.01528.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %.116.1, %.lr.ph ]
  %.01727.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %.118.1, %.lr.ph ]
  %.01926.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %.120.1, %.lr.ph ]
  %.02125.unr = phi i32 [ -2147483648, %.lr.ph.preheader ], [ %.122.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %5 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = add nsw i32 %6, %.01926.unr
  %8 = icmp slt i32 %.02125.unr, %7
  %.118.epil = select i1 %8, i32 %.01429.unr, i32 %.01727.unr
  %9 = trunc nuw nsw i64 %indvars.iv.unr to i32
  %.116.epil = select i1 %8, i32 %9, i32 %.01528.unr
  br label %._crit_edge.loopexit

._crit_edge.loopexit:                             ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.118.lcssa = phi i32 [ %.118.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.118.epil, %.lr.ph.epil ]
  %.116.lcssa = phi i32 [ %.116.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.116.epil, %.lr.ph.epil ]
  %10 = sub i32 %.116.lcssa, %.118.lcssa
  %11 = add i32 %10, 1
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %2
  %reass.sub = phi i32 [ 1, %2 ], [ %11, %._crit_edge.loopexit ]
  ret i32 %reass.sub

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %.01429 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.1.1, %.lr.ph ]
  %.01528 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.116.1, %.lr.ph ]
  %.01727 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.118.1, %.lr.ph ]
  %.01926 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.120.1, %.lr.ph ]
  %.02125 = phi i32 [ -2147483648, %.lr.ph.preheader.new ], [ %.122.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %12 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = add nsw i32 %13, %.01926
  %15 = icmp slt i32 %.02125, %14
  %.122 = tail call i32 @llvm.smax.i32(i32 %.02125, i32 %14)
  %.118 = select i1 %15, i32 %.01429, i32 %.01727
  %16 = trunc nuw nsw i64 %indvars.iv to i32
  %.116 = select i1 %15, i32 %16, i32 %.01528
  %17 = icmp slt i32 %14, 0
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %.120 = tail call i32 @llvm.smax.i32(i32 %14, i32 0)
  %18 = trunc nuw nsw i64 %indvars.iv.next to i32
  %.1 = select i1 %17, i32 %18, i32 %.01429
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = add nsw i32 %20, %.120
  %22 = icmp slt i32 %.122, %21
  %.122.1 = tail call i32 @llvm.smax.i32(i32 %.122, i32 %21)
  %.118.1 = select i1 %22, i32 %.1, i32 %.118
  %23 = trunc nuw nsw i64 %indvars.iv.next to i32
  %.116.1 = select i1 %22, i32 %23, i32 %.116
  %24 = icmp slt i32 %21, 0
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %.120.1 = tail call i32 @llvm.smax.i32(i32 %21, i32 0)
  %25 = trunc nuw nsw i64 %indvars.iv.next.1 to i32
  %.1.1 = select i1 %24, i32 %25, i32 %.1
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9
}

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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