; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 05. Arrays/23 Max Sum of K Consecutive Element/23a Max Sum of K Consecutive element_5ec04140 (Z7maxKSumPiii)

; 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 @_Z7maxKSumPiii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %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
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, 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
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %82

19:                                               ; preds = %3
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = icmp eq i32 %20, %21
  br i1 %22, label %23, label %42

23:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %37, %23
  %25 = load i32, ptr %12, align 4, !tbaa !9
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %40

29:                                               ; preds = %24
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load i32, ptr %12, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i32, ptr %31, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = add nsw i32 %30, %35
  store i32 %36, ptr %11, align 4, !tbaa !9
  br label %37

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

40:                                               ; preds = %28
  %41 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %41, ptr %4, align 4
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %82

42:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %77, %42
  %44 = load i32, ptr %13, align 4, !tbaa !9
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = sub nsw i32 %45, %46
  %48 = icmp sle i32 %44, %47
  br i1 %48, label %50, label %49

49:                                               ; preds = %43
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  br label %80

50:                                               ; preds = %43
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  %51 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %51, ptr %14, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %67, %50
  %53 = load i32, ptr %14, align 4, !tbaa !9
  %54 = load i32, ptr %13, align 4, !tbaa !9
  %55 = load i32, ptr %7, align 4, !tbaa !9
  %56 = add nsw i32 %54, %55
  %57 = icmp slt i32 %53, %56
  br i1 %57, label %59, label %58

58:                                               ; preds = %52
  store i32 8, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  br label %70

59:                                               ; preds = %52
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load i32, ptr %14, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds i32, ptr %60, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !9
  %65 = load i32, ptr %9, align 4, !tbaa !9
  %66 = add nsw i32 %65, %64
  store i32 %66, ptr %9, align 4, !tbaa !9
  br label %67

67:                                               ; preds = %59
  %68 = load i32, ptr %14, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %14, align 4, !tbaa !9
  br label %52, !llvm.loop !13

70:                                               ; preds = %58
  %71 = load i32, ptr %9, align 4, !tbaa !9
  %72 = load i32, ptr %8, align 4, !tbaa !9
  %73 = icmp sgt i32 %71, %72
  br i1 %73, label %74, label %76

74:                                               ; preds = %70
  %75 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %75, ptr %8, align 4, !tbaa !9
  br label %76

76:                                               ; preds = %74, %70
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %13, align 4, !tbaa !9
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %13, align 4, !tbaa !9
  br label %43, !llvm.loop !14

80:                                               ; preds = %49
  %81 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %81, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %82

82:                                               ; preds = %80, %40, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %83 = load i32, ptr %4, align 4
  ret i32 %83
}

; 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"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !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 nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i32 @_Z7maxKSumPiii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp slt i32 %1, %2
  br i1 %4, label %.loopexit, label %5

5:                                                ; preds = %3
  %6 = icmp eq i32 %1, %2
  br i1 %6, label %.preheader, label %.preheader35.preheader

.preheader35.preheader:                           ; preds = %5
  %7 = add i32 %1, 1
  %8 = sub i32 %7, %2
  %wide.trip.count = zext i32 %8 to i64
  %9 = icmp sgt i32 %2, 0
  br label %.preheader35

.preheader:                                       ; preds = %5
  %10 = icmp sgt i32 %1, 0
  br i1 %10, label %.lr.ph46.preheader, label %.loopexit

.lr.ph46.preheader:                               ; preds = %.preheader
  %wide.trip.count57 = zext nneg i32 %1 to i64
  %min.iters.check67 = icmp ult i32 %1, 8
  br i1 %min.iters.check67, label %.lr.ph46.preheader82, label %vector.ph68

vector.ph68:                                      ; preds = %.lr.ph46.preheader
  %n.vec70 = and i64 %wide.trip.count57, 2147483640
  br label %vector.body72

vector.body72:                                    ; preds = %vector.body72, %vector.ph68
  %index73 = phi i64 [ 0, %vector.ph68 ], [ %index.next78, %vector.body72 ]
  %vec.phi74 = phi <4 x i32> [ zeroinitializer, %vector.ph68 ], [ %13, %vector.body72 ]
  %vec.phi75 = phi <4 x i32> [ zeroinitializer, %vector.ph68 ], [ %14, %vector.body72 ]
  %11 = getelementptr inbounds i32, ptr %0, i64 %index73
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  %wide.load76 = load <4 x i32>, ptr %11, align 4, !tbaa !5
  %wide.load77 = load <4 x i32>, ptr %12, align 4, !tbaa !5
  %13 = add <4 x i32> %wide.load76, %vec.phi74
  %14 = add <4 x i32> %wide.load77, %vec.phi75
  %index.next78 = add nuw i64 %index73, 8
  %15 = icmp eq i64 %index.next78, %n.vec70
  br i1 %15, label %middle.block65, label %vector.body72, !llvm.loop !9

middle.block65:                                   ; preds = %vector.body72
  %bin.rdx79 = add <4 x i32> %14, %13
  %16 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx79)
  %cmp.n80 = icmp eq i64 %n.vec70, %wide.trip.count57
  br i1 %cmp.n80, label %.loopexit, label %.lr.ph46.preheader82

.lr.ph46.preheader82:                             ; preds = %middle.block65, %.lr.ph46.preheader
  %indvars.iv54.ph = phi i64 [ 0, %.lr.ph46.preheader ], [ %n.vec70, %middle.block65 ]
  %.02944.ph = phi i32 [ 0, %.lr.ph46.preheader ], [ %16, %middle.block65 ]
  br label %.lr.ph46

.lr.ph46:                                         ; preds = %.lr.ph46.preheader82, %.lr.ph46
  %indvars.iv54 = phi i64 [ %indvars.iv.next55, %.lr.ph46 ], [ %indvars.iv54.ph, %.lr.ph46.preheader82 ]
  %.02944 = phi i32 [ %19, %.lr.ph46 ], [ %.02944.ph, %.lr.ph46.preheader82 ]
  %17 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv54
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, %.02944
  %indvars.iv.next55 = add nuw nsw i64 %indvars.iv54, 1
  %exitcond58.not = icmp eq i64 %indvars.iv.next55, %wide.trip.count57
  br i1 %exitcond58.not, label %.loopexit, label %.lr.ph46, !llvm.loop !13

.preheader35:                                     ; preds = %.preheader35.preheader, %._crit_edge
  %indvars.iv = phi i64 [ 0, %.preheader35.preheader ], [ %indvars.iv.next, %._crit_edge ]
  %.03141 = phi i32 [ 0, %.preheader35.preheader ], [ %spec.select, %._crit_edge ]
  %20 = trunc i64 %indvars.iv to i32
  %21 = add i32 %20, %2
  %22 = trunc i64 %indvars.iv to i32
  %23 = add i32 %22, 1
  %smax = tail call i32 @llvm.smax.i32(i32 %21, i32 %23)
  %24 = trunc i64 %indvars.iv to i32
  %25 = xor i32 %24, -1
  %26 = add i32 %smax, %25
  %27 = zext i32 %26 to i64
  %28 = add nuw nsw i64 %27, 1
  %indvars53 = trunc i64 %indvars.iv to i32
  %29 = add nsw i32 %indvars53, %2
  br i1 %9, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader35
  %min.iters.check = icmp ult i32 %26, 7
  br i1 %min.iters.check, label %.lr.ph.preheader85, label %vector.ph

.lr.ph.preheader85:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv50.ph = phi i64 [ %indvars.iv, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.03038.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %36, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %28, 8589934584
  %ind.end = add nuw i64 %indvars.iv, %n.vec
  %30 = getelementptr i32, ptr %0, i64 %indvars.iv
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %33, %vector.body ]
  %vec.phi63 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %34, %vector.body ]
  %31 = getelementptr i32, ptr %30, i64 %index
  %32 = getelementptr inbounds i8, ptr %31, i64 16
  %wide.load = load <4 x i32>, ptr %31, align 4, !tbaa !5
  %wide.load64 = load <4 x i32>, ptr %32, align 4, !tbaa !5
  %33 = add <4 x i32> %wide.load, %vec.phi
  %34 = add <4 x i32> %wide.load64, %vec.phi63
  %index.next = add nuw i64 %index, 8
  %35 = icmp eq i64 %index.next, %n.vec
  br i1 %35, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %34, %33
  %36 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %28, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader85

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %.preheader35
  %.030.lcssa = phi i32 [ 0, %.preheader35 ], [ %36, %middle.block ], [ %39, %.lr.ph ]
  %spec.select = tail call i32 @llvm.smax.i32(i32 %.030.lcssa, i32 %.03141)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit, label %.preheader35, !llvm.loop !15

.lr.ph:                                           ; preds = %.lr.ph.preheader85, %.lr.ph
  %indvars.iv50 = phi i64 [ %indvars.iv.next51, %.lr.ph ], [ %indvars.iv50.ph, %.lr.ph.preheader85 ]
  %.03038 = phi i32 [ %39, %.lr.ph ], [ %.03038.ph, %.lr.ph.preheader85 ]
  %37 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv50
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = add nsw i32 %38, %.03038
  %indvars.iv.next51 = add nuw nsw i64 %indvars.iv50, 1
  %40 = trunc nuw i64 %indvars.iv.next51 to i32
  %41 = icmp sgt i32 %29, %40
  br i1 %41, label %.lr.ph, label %._crit_edge, !llvm.loop !16

.loopexit:                                        ; preds = %._crit_edge, %.lr.ph46, %middle.block65, %.preheader, %3
  %.032 = phi i32 [ -1, %3 ], [ 0, %.preheader ], [ %16, %middle.block65 ], [ %19, %.lr.ph46 ], [ %spec.select, %._crit_edge ]
  ret i32 %.032
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x 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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = distinct !{!14, !10, !11, !12}
!15 = distinct !{!15, !10}
!16 = distinct !{!16, !10, !12, !11}


; Effective O3 passes: ADCEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass