; LLVM IR from Dev-XYS/Algorithms :: Persistent-Segment-Tree(Sum).ll (Z5queryiiP4node)

; 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"

%struct.node = type { i32, i32, i32, ptr, ptr }

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5queryiiP4node(i32 noundef %0, i32 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = 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
  %10 = load ptr, ptr %7, align 8, !tbaa !9
  %11 = getelementptr inbounds %struct.node, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = icmp eq i32 %12, %13
  br i1 %14, label %15, label %25

15:                                               ; preds = %3
  %16 = load ptr, ptr %7, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.node, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = icmp eq i32 %18, %19
  br i1 %20, label %21, label %25

21:                                               ; preds = %15
  %22 = load ptr, ptr %7, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.node, ptr %22, i32 0, i32 2
  %24 = load i32, ptr %23, align 8, !tbaa !14
  store i32 %24, ptr %4, align 4
  br label %71

25:                                               ; preds = %15, %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %26 = load ptr, ptr %7, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.node, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 8, !tbaa !11
  %29 = load ptr, ptr %7, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.node, ptr %29, i32 0, i32 1
  %31 = load i32, ptr %30, align 4, !tbaa !13
  %32 = add nsw i32 %28, %31
  %33 = ashr i32 %32, 1
  store i32 %33, ptr %8, align 4, !tbaa !5
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = icmp sle i32 %34, %35
  br i1 %36, label %37, label %44

37:                                               ; preds = %25
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = load ptr, ptr %7, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.node, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  %43 = call noundef i32 @_Z5queryiiP4node(i32 noundef %38, i32 noundef %39, ptr noundef %42)
  store i32 %43, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %70

44:                                               ; preds = %25
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = load i32, ptr %8, align 4, !tbaa !5
  %47 = icmp sgt i32 %45, %46
  br i1 %47, label %48, label %55

48:                                               ; preds = %44
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = load i32, ptr %6, align 4, !tbaa !5
  %51 = load ptr, ptr %7, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.node, ptr %51, i32 0, i32 4
  %53 = load ptr, ptr %52, align 8, !tbaa !16
  %54 = call noundef i32 @_Z5queryiiP4node(i32 noundef %49, i32 noundef %50, ptr noundef %53)
  store i32 %54, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %70

55:                                               ; preds = %44
  %56 = load i32, ptr %5, align 4, !tbaa !5
  %57 = load i32, ptr %8, align 4, !tbaa !5
  %58 = load ptr, ptr %7, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.node, ptr %58, i32 0, i32 3
  %60 = load ptr, ptr %59, align 8, !tbaa !15
  %61 = call noundef i32 @_Z5queryiiP4node(i32 noundef %56, i32 noundef %57, ptr noundef %60)
  %62 = load i32, ptr %8, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  %64 = load i32, ptr %6, align 4, !tbaa !5
  %65 = load ptr, ptr %7, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.node, ptr %65, i32 0, i32 4
  %67 = load ptr, ptr %66, align 8, !tbaa !16
  %68 = call noundef i32 @_Z5queryiiP4node(i32 noundef %63, i32 noundef %64, ptr noundef %67)
  %69 = add nsw i32 %61, %68
  store i32 %69, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %70

70:                                               ; preds = %55, %48, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %71

71:                                               ; preds = %70, %21
  %72 = load i32, ptr %4, align 4
  ret i32 %72
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"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 = !{!12, !6, i64 0}
!12 = !{!"_ZTS4node", !6, i64 0, !6, i64 4, !6, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!12, !6, i64 4}
!14 = !{!12, !6, i64 8}
!15 = !{!12, !10, i64 16}
!16 = !{!12, !10, i64 24}


; 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 nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryiiP4node(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = load i32, ptr %2, align 8, !tbaa !5
  %5 = icmp eq i32 %4, %0
  %6 = getelementptr inbounds i8, ptr %2, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = icmp eq i32 %7, %1
  %or.cond3440 = select i1 %5, i1 %8, i1 false
  br i1 %or.cond3440, label %tailrecurse.outer._crit_edge, label %tailrecurse._crit_edge.lr.ph

tailrecurse._crit_edge.lr.ph:                     ; preds = %3, %tailrecurse.outer
  %9 = phi i32 [ %35, %tailrecurse.outer ], [ %7, %3 ]
  %10 = phi i32 [ %32, %tailrecurse.outer ], [ %4, %3 ]
  %.tr29.ph43 = phi ptr [ %30, %tailrecurse.outer ], [ %2, %3 ]
  %.tr.ph42 = phi i32 [ %28, %tailrecurse.outer ], [ %0, %3 ]
  %accumulator.tr.ph41 = phi i32 [ %31, %tailrecurse.outer ], [ 0, %3 ]
  br label %tailrecurse._crit_edge

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse.backedge, %3
  %accumulator.tr.ph.lcssa = phi i32 [ 0, %3 ], [ %accumulator.tr.ph41, %tailrecurse.backedge ], [ %31, %tailrecurse.outer ]
  %.tr29.lcssa = phi ptr [ %2, %3 ], [ %.tr29.be, %tailrecurse.backedge ], [ %30, %tailrecurse.outer ]
  %11 = getelementptr inbounds i8, ptr %.tr29.lcssa, i64 8
  %12 = load i32, ptr %11, align 8, !tbaa !12
  %accumulator.ret.tr = add nsw i32 %12, %accumulator.tr.ph.lcssa
  ret i32 %accumulator.ret.tr

tailrecurse._crit_edge:                           ; preds = %tailrecurse._crit_edge.lr.ph, %tailrecurse.backedge
  %13 = phi i32 [ %9, %tailrecurse._crit_edge.lr.ph ], [ %21, %tailrecurse.backedge ]
  %14 = phi i32 [ %10, %tailrecurse._crit_edge.lr.ph ], [ %18, %tailrecurse.backedge ]
  %.tr2935 = phi ptr [ %.tr29.ph43, %tailrecurse._crit_edge.lr.ph ], [ %.tr29.be, %tailrecurse.backedge ]
  %15 = add nsw i32 %13, %14
  %16 = ashr i32 %15, 1
  %.not = icmp slt i32 %16, %1
  br i1 %.not, label %23, label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %tailrecurse._crit_edge, %23
  %.sink = phi i64 [ 24, %23 ], [ 16, %tailrecurse._crit_edge ]
  %17 = getelementptr inbounds i8, ptr %.tr2935, i64 %.sink
  %.tr29.be = load ptr, ptr %17, align 8, !tbaa !13
  %18 = load i32, ptr %.tr29.be, align 8, !tbaa !5
  %19 = icmp eq i32 %18, %.tr.ph42
  %20 = getelementptr inbounds i8, ptr %.tr29.be, i64 4
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %22 = icmp eq i32 %21, %1
  %or.cond = select i1 %19, i1 %22, i1 false
  br i1 %or.cond, label %tailrecurse.outer._crit_edge, label %tailrecurse._crit_edge

23:                                               ; preds = %tailrecurse._crit_edge
  %24 = icmp slt i32 %16, %.tr.ph42
  br i1 %24, label %tailrecurse.backedge, label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %23
  %25 = getelementptr inbounds i8, ptr %.tr2935, i64 16
  %26 = load ptr, ptr %25, align 8, !tbaa !14
  %27 = tail call noundef i32 @_Z5queryiiP4node(i32 noundef %.tr.ph42, i32 noundef %16, ptr noundef %26)
  %28 = add nsw i32 %16, 1
  %29 = getelementptr inbounds i8, ptr %.tr2935, i64 24
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = add nsw i32 %27, %accumulator.tr.ph41
  %32 = load i32, ptr %30, align 8, !tbaa !5
  %33 = icmp eq i32 %32, %28
  %34 = getelementptr inbounds i8, ptr %30, i64 4
  %35 = load i32, ptr %34, align 4, !tbaa !11
  %36 = icmp eq i32 %35, %1
  %or.cond34 = select i1 %33, i1 %36, i1 false
  br i1 %or.cond34, label %tailrecurse.outer._crit_edge, label %tailrecurse._crit_edge.lr.ph
}

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(read, inaccessiblemem: none) 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, !7, i64 0}
!6 = !{!"_ZTS4node", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !7, i64 8}
!13 = !{!10, !10, i64 0}
!14 = !{!6, !10, i64 16}
!15 = !{!6, !10, i64 24}


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