; LLVM IR from VastoLorde95/Competitive-Programming :: HackerEarth/Upgrade/A.ll (Z8query_upiib)

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

@p = external dso_local global [17 x [131072 x i32]], align 16
@chain_id = external dso_local global [131072 x i32], align 16
@chain_parent = external dso_local global [131072 x i32], align 16
@pos = external dso_local global [131072 x i32], align 16
@treap = external dso_local global ptr, align 8

; 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
declare dso_local noundef i32 @_ZN5Treap11range_queryERPNS_4nodeEii(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z8query_upiib(i32 noundef %0, i32 noundef %1, i1 noundef zeroext %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %9 = zext i1 %2 to i8
  store i8 %9, ptr %6, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !11
  br label %10

10:                                               ; preds = %58, %3
  br label %11

11:                                               ; preds = %10
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [131072 x i32], ptr @chain_id, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [131072 x i32], ptr @chain_id, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp eq i32 %15, %19
  br i1 %20, label %21, label %58

21:                                               ; preds = %11
  %22 = load i8, ptr %6, align 1, !tbaa !9, !range !13, !noundef !14
  %23 = trunc i8 %22 to i1
  br i1 %23, label %43, label %24

24:                                               ; preds = %21
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = icmp ne i32 %25, %26
  br i1 %27, label %28, label %42

28:                                               ; preds = %24
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = call noundef i32 @_ZN5Treap11range_queryERPNS_4nodeEii(ptr noundef nonnull align 8 dereferenceable(8) @treap, i32 noundef %33, i32 noundef %37)
  %39 = sext i32 %38 to i64
  %40 = load i64, ptr %7, align 8, !tbaa !11
  %41 = add nsw i64 %40, %39
  store i64 %41, ptr %7, align 8, !tbaa !11
  br label %42

42:                                               ; preds = %28, %24
  br label %56

43:                                               ; preds = %21
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = call noundef i32 @_ZN5Treap11range_queryERPNS_4nodeEii(ptr noundef nonnull align 8 dereferenceable(8) @treap, i32 noundef %47, i32 noundef %51)
  %53 = sext i32 %52 to i64
  %54 = load i64, ptr %7, align 8, !tbaa !11
  %55 = add nsw i64 %54, %53
  store i64 %55, ptr %7, align 8, !tbaa !11
  br label %56

56:                                               ; preds = %43, %42
  %57 = load i64, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %57

58:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [131072 x i32], ptr @chain_id, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [131072 x i32], ptr @chain_parent, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  store i32 %65, ptr %8, align 4, !tbaa !5
  %66 = load i32, ptr %8, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %67
  %69 = load i32, ptr %68, align 4, !tbaa !5
  %70 = load i32, ptr %4, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !5
  %74 = call noundef i32 @_ZN5Treap11range_queryERPNS_4nodeEii(ptr noundef nonnull align 8 dereferenceable(8) @treap, i32 noundef %69, i32 noundef %73)
  %75 = sext i32 %74 to i64
  %76 = load i64, ptr %7, align 8, !tbaa !11
  %77 = add nsw i64 %76, %75
  store i64 %77, ptr %7, align 8, !tbaa !11
  %78 = load i32, ptr %8, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [131072 x i32], ptr @p, i64 0, i64 %79
  %81 = load i32, ptr %80, align 4, !tbaa !5
  store i32 %81, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %10, !llvm.loop !15
}

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 = !{!"bool", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@p = external dso_local local_unnamed_addr global [17 x [131072 x i32]], align 16
@chain_id = external dso_local local_unnamed_addr global [131072 x i32], align 16
@chain_parent = external dso_local local_unnamed_addr global [131072 x i32], align 16
@pos = external dso_local local_unnamed_addr global [131072 x i32], align 16
@treap = external dso_local global ptr, align 8

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN5Treap11range_queryERPNS_4nodeEii(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z8query_upiib(i32 noundef %0, i32 noundef %1, i1 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = sext i32 %1 to i64
  %5 = getelementptr inbounds [131072 x i32], ptr @chain_id, i64 0, i64 %4
  %6 = sext i32 %0 to i64
  %7 = getelementptr inbounds [131072 x i32], ptr @chain_id, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = icmp eq i32 %8, %9
  br i1 %10, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.017.lcssa = phi i64 [ 0, %3 ], [ %37, %.lr.ph ]
  %.0.lcssa = phi i32 [ %0, %3 ], [ %39, %.lr.ph ]
  %.lcssa19 = phi i64 [ %6, %3 ], [ %40, %.lr.ph ]
  br i1 %2, label %16, label %11

11:                                               ; preds = %._crit_edge
  %.not = icmp eq i32 %.0.lcssa, %1
  br i1 %.not, label %24, label %12

12:                                               ; preds = %11
  %13 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %4
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  br label %.sink.split

16:                                               ; preds = %._crit_edge
  %17 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %4
  %18 = load i32, ptr %17, align 4, !tbaa !5
  br label %.sink.split

.sink.split:                                      ; preds = %16, %12
  %.sink = phi i32 [ %15, %12 ], [ %18, %16 ]
  %19 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %.lcssa19
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = tail call noundef i32 @_ZN5Treap11range_queryERPNS_4nodeEii(ptr noundef nonnull align 8 dereferenceable(8) @treap, i32 noundef %.sink, i32 noundef %20)
  %22 = sext i32 %21 to i64
  %23 = add nsw i64 %.017.lcssa, %22
  br label %24

24:                                               ; preds = %.sink.split, %11
  %.1 = phi i64 [ %.017.lcssa, %11 ], [ %23, %.sink.split ]
  ret i64 %.1

.lr.ph:                                           ; preds = %3, %.lr.ph
  %25 = phi i32 [ %42, %.lr.ph ], [ %8, %3 ]
  %26 = phi i64 [ %40, %.lr.ph ], [ %6, %3 ]
  %.01720 = phi i64 [ %37, %.lr.ph ], [ 0, %3 ]
  %27 = sext i32 %25 to i64
  %28 = getelementptr inbounds [131072 x i32], ptr @chain_parent, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = getelementptr inbounds [131072 x i32], ptr @pos, i64 0, i64 %26
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = tail call noundef i32 @_ZN5Treap11range_queryERPNS_4nodeEii(ptr noundef nonnull align 8 dereferenceable(8) @treap, i32 noundef %32, i32 noundef %34)
  %36 = sext i32 %35 to i64
  %37 = add nsw i64 %.01720, %36
  %38 = getelementptr inbounds [131072 x i32], ptr @p, i64 0, i64 %30
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [131072 x i32], ptr @chain_id, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = icmp eq i32 %42, %43
  br i1 %44, label %._crit_edge, label %.lr.ph, !llvm.loop !9
}

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

!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,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass