; LLVM IR from AlgoGenesis/C :: Binary Index Tree/FenwickTree/Build (build_fenwick_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: nounwind uwtable
define dso_local void @build_fenwick_tree(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %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
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, 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
  br label %10

10:                                               ; preds = %20, %3
  %11 = load i32, ptr %7, align 4, !tbaa !9
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = icmp sle i32 %11, %12
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %10
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %16, i64 %18
  store i32 0, ptr %19, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %15
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %7, align 4, !tbaa !9
  br label %10, !llvm.loop !11

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

24:                                               ; preds = %55, %23
  %25 = load i32, ptr %8, 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
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %58

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %9, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %36, %29
  %33 = load i32, ptr %9, align 4, !tbaa !9
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = icmp sle i32 %33, %34
  br i1 %35, label %36, label %54

36:                                               ; preds = %32
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i32, ptr %37, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i32, ptr %42, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !9
  %47 = add nsw i32 %46, %41
  store i32 %47, ptr %45, align 4, !tbaa !9
  %48 = load i32, ptr %9, align 4, !tbaa !9
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = sub nsw i32 0, %49
  %51 = and i32 %48, %50
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = add nsw i32 %52, %51
  store i32 %53, ptr %9, align 4, !tbaa !9
  br label %32, !llvm.loop !13

54:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %8, align 4, !tbaa !9
  br label %24, !llvm.loop !14

58:                                               ; preds = %28
  ret void
}

; 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 = { 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/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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @build_fenwick_tree(ptr nocapture noundef readonly %0, ptr nocapture noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.not21 = icmp slt i32 %2, 0
  br i1 %.not21, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %3
  %4 = add nuw i32 %2, 1
  %5 = zext i32 %4 to i64
  %6 = shl nuw nsw i64 %5, 2
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %1, i8 0, i64 %6, i1 false), !tbaa !5
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph27.preheader

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

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

.loopexit:                                        ; preds = %33
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %8 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %9 = trunc nuw nsw i64 %indvars.iv.next.1 to i32
  br label %10

10:                                               ; preds = %10, %.loopexit
  %.024.1 = phi i32 [ %9, %.loopexit ], [ %18, %10 ]
  %11 = load i32, ptr %8, align 4, !tbaa !5
  %12 = sext i32 %.024.1 to i64
  %13 = getelementptr inbounds i32, ptr %1, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = add nsw i32 %14, %11
  store i32 %15, ptr %13, align 4, !tbaa !5
  %16 = sub nsw i32 0, %.024.1
  %17 = and i32 %.024.1, %16
  %18 = add nsw i32 %17, %.024.1
  %.not20.1 = icmp sgt i32 %18, %2
  br i1 %.not20.1, label %.loopexit.1, label %10, !llvm.loop !9

.loopexit.1:                                      ; preds = %10
  %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.ph25, !llvm.loop !11

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.loopexit.1, %.lr.ph27.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph27.preheader ], [ %indvars.iv.next.1, %.loopexit.1 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph25.epil

.lr.ph25.epil:                                    ; preds = %._crit_edge.loopexit.unr-lcssa
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %20 = trunc i64 %indvars.iv.unr to i32
  %21 = add i32 %20, 1
  br label %22

22:                                               ; preds = %22, %.lr.ph25.epil
  %.024.epil = phi i32 [ %21, %.lr.ph25.epil ], [ %30, %22 ]
  %23 = load i32, ptr %19, align 4, !tbaa !5
  %24 = sext i32 %.024.epil to i64
  %25 = getelementptr inbounds i32, ptr %1, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = add nsw i32 %26, %23
  store i32 %27, ptr %25, align 4, !tbaa !5
  %28 = sub nsw i32 0, %.024.epil
  %29 = and i32 %.024.epil, %28
  %30 = add nsw i32 %29, %.024.epil
  %.not20.epil = icmp sgt i32 %30, %2
  br i1 %.not20.epil, label %._crit_edge, label %22, !llvm.loop !9

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %22, %3, %.preheader
  ret void

.lr.ph25:                                         ; preds = %.loopexit.1, %.lr.ph27.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph27.preheader.new ], [ %indvars.iv.next.1, %.loopexit.1 ]
  %niter = phi i64 [ 0, %.lr.ph27.preheader.new ], [ %niter.next.1, %.loopexit.1 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %31 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %32 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %33

33:                                               ; preds = %.lr.ph25, %33
  %.024 = phi i32 [ %32, %.lr.ph25 ], [ %41, %33 ]
  %34 = load i32, ptr %31, align 4, !tbaa !5
  %35 = sext i32 %.024 to i64
  %36 = getelementptr inbounds i32, ptr %1, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = add nsw i32 %37, %34
  store i32 %38, ptr %36, align 4, !tbaa !5
  %39 = sub nsw i32 0, %.024
  %40 = and i32 %.024, %39
  %41 = add nsw i32 %40, %.024
  %.not20 = icmp sgt i32 %41, %2
  br i1 %.not20, label %.loopexit, label %33, !llvm.loop !9
}

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

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

!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"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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