; LLVM IR from ahampriyanshu/algo-ds-101 :: Data-Structures/Tree/Segment-Tree (Segment-Tree)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @getSum(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) #1 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %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 %8, align 8, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !9
  store i32 %2, ptr %10, align 4, !tbaa !9
  store i32 %3, ptr %11, align 4, !tbaa !9
  store i32 %4, ptr %12, align 4, !tbaa !9
  store i32 %5, ptr %13, align 4, !tbaa !9
  %15 = load i32, ptr %9, align 4, !tbaa !9
  %16 = load i32, ptr %12, align 4, !tbaa !9
  %17 = icmp sge i32 %15, %16
  br i1 %17, label %22, label %18

18:                                               ; preds = %6
  %19 = load i32, ptr %10, align 4, !tbaa !9
  %20 = load i32, ptr %11, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %22, label %23

22:                                               ; preds = %18, %6
  store i32 0, ptr %7, align 4
  br label %62

23:                                               ; preds = %18
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = load i32, ptr %11, align 4, !tbaa !9
  %26 = icmp sle i32 %24, %25
  br i1 %26, label %27, label %38

27:                                               ; preds = %23
  %28 = load i32, ptr %12, align 4, !tbaa !9
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  %31 = icmp sle i32 %28, %30
  br i1 %31, label %32, label %38

32:                                               ; preds = %27
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load i32, ptr %13, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %33, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !9
  store i32 %37, ptr %7, align 4
  br label %62

38:                                               ; preds = %27, %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  %39 = load i32, ptr %11, align 4, !tbaa !9
  %40 = load i32, ptr %12, align 4, !tbaa !9
  %41 = add nsw i32 %39, %40
  %42 = sdiv i32 %41, 2
  store i32 %42, ptr %14, align 4, !tbaa !9
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = load i32, ptr %11, align 4, !tbaa !9
  %47 = load i32, ptr %14, align 4, !tbaa !9
  %48 = load i32, ptr %13, align 4, !tbaa !9
  %49 = mul nsw i32 %48, 2
  %50 = add nsw i32 %49, 1
  %51 = call i32 @getSum(ptr noundef %43, i32 noundef %44, i32 noundef %45, i32 noundef %46, i32 noundef %47, i32 noundef %50)
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  %53 = load i32, ptr %9, align 4, !tbaa !9
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = load i32, ptr %14, align 4, !tbaa !9
  %56 = load i32, ptr %12, align 4, !tbaa !9
  %57 = load i32, ptr %13, align 4, !tbaa !9
  %58 = mul nsw i32 %57, 2
  %59 = add nsw i32 %58, 2
  %60 = call i32 @getSum(ptr noundef %52, i32 noundef %53, i32 noundef %54, i32 noundef %55, i32 noundef %56, i32 noundef %59)
  %61 = add nsw i32 %51, %60
  store i32 %61, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  br label %62

62:                                               ; preds = %38, %32, %22
  %63 = load i32, ptr %7, align 4
  ret i32 %63
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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}


; 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: nofree nosync nounwind memory(argmem: read) uwtable
define dso_local i32 @getSum(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #0 {
  %.not = icmp sge i32 %1, %4
  %7 = icmp slt i32 %2, %3
  %or.cond39 = or i1 %.not, %7
  br i1 %or.cond39, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %6
  %8 = add i32 %2, 1
  %.not29 = icmp slt i32 %8, %4
  br i1 %.not29, label %tailrecurse.us, label %.lr.ph.split.split

tailrecurse.us:                                   ; preds = %.lr.ph, %tailrecurse.us
  %.tr3542.us = phi i32 [ %14, %tailrecurse.us ], [ %5, %.lr.ph ]
  %.tr3341.us = phi i32 [ %10, %tailrecurse.us ], [ %3, %.lr.ph ]
  %accumulator.tr40.us = phi i32 [ %15, %tailrecurse.us ], [ 0, %.lr.ph ]
  %9 = add nsw i32 %.tr3341.us, %4
  %10 = sdiv i32 %9, 2
  %11 = shl nsw i32 %.tr3542.us, 1
  %12 = or disjoint i32 %11, 1
  %13 = tail call i32 @getSum(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %.tr3341.us, i32 noundef %10, i32 noundef %12)
  %14 = add nsw i32 %11, 2
  %15 = add nsw i32 %13, %accumulator.tr40.us
  %16 = icmp sgt i32 %10, %2
  br i1 %16, label %.loopexit, label %tailrecurse.us

.lr.ph.split.split:                               ; preds = %.lr.ph, %tailrecurse
  %.tr3542 = phi i32 [ %25, %tailrecurse ], [ %5, %.lr.ph ]
  %.tr3341 = phi i32 [ %21, %tailrecurse ], [ %3, %.lr.ph ]
  %accumulator.tr40 = phi i32 [ %26, %tailrecurse ], [ 0, %.lr.ph ]
  %.not28 = icmp slt i32 %.tr3341, %1
  br i1 %.not28, label %tailrecurse, label %.split.us

.split.us:                                        ; preds = %.lr.ph.split.split
  %17 = sext i32 %.tr3542 to i64
  %18 = getelementptr inbounds i32, ptr %0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  br label %.loopexit

tailrecurse:                                      ; preds = %.lr.ph.split.split
  %20 = add nsw i32 %.tr3341, %4
  %21 = sdiv i32 %20, 2
  %22 = shl nsw i32 %.tr3542, 1
  %23 = or disjoint i32 %22, 1
  %24 = tail call i32 @getSum(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %.tr3341, i32 noundef %21, i32 noundef %23)
  %25 = add nsw i32 %22, 2
  %26 = add nsw i32 %24, %accumulator.tr40
  %27 = icmp sgt i32 %21, %2
  br i1 %27, label %.loopexit, label %.lr.ph.split.split

.loopexit:                                        ; preds = %tailrecurse, %tailrecurse.us, %6, %.split.us
  %accumulator.tr38 = phi i32 [ %accumulator.tr40, %.split.us ], [ 0, %6 ], [ %15, %tailrecurse.us ], [ %26, %tailrecurse ]
  %.0 = phi i32 [ %19, %.split.us ], [ 0, %6 ], [ 0, %tailrecurse.us ], [ 0, %tailrecurse ]
  %accumulator.ret.tr = add nsw i32 %.0, %accumulator.tr38
  ret i32 %accumulator.ret.tr
}

attributes #0 = { nofree nosync nounwind 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" }

!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/C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass