; LLVM IR from Tunghohin/Competitive_Coding :: Luogu/p3834.ll (Z5queryiiiii)

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

@tr = external dso_local global [4200210 x %struct.Node], align 16

; 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 @_Z5queryiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %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 i32 %0, ptr %7, align 4, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !5
  %15 = load i32, ptr %9, align 4, !tbaa !5
  %16 = load i32, ptr %10, align 4, !tbaa !5
  %17 = icmp eq i32 %15, %16
  br i1 %17, label %18, label %20

18:                                               ; preds = %5
  %19 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %19, ptr %6, align 4
  br label %81

20:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.Node, ptr %23, i32 0, i32 0
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.Node, ptr %27, i32 0, i32 2
  %29 = load i32, ptr %28, align 4, !tbaa !11
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %31
  %33 = getelementptr inbounds %struct.Node, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %35
  %37 = getelementptr inbounds %struct.Node, ptr %36, i32 0, i32 2
  %38 = load i32, ptr %37, align 4, !tbaa !11
  %39 = sub nsw i32 %29, %38
  store i32 %39, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %40 = load i32, ptr %9, align 4, !tbaa !5
  %41 = load i32, ptr %10, align 4, !tbaa !5
  %42 = add nsw i32 %40, %41
  %43 = ashr i32 %42, 1
  store i32 %43, ptr %13, align 4, !tbaa !5
  %44 = load i32, ptr %11, align 4, !tbaa !5
  %45 = load i32, ptr %12, align 4, !tbaa !5
  %46 = icmp sle i32 %44, %45
  br i1 %46, label %47, label %62

47:                                               ; preds = %20
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %49
  %51 = getelementptr inbounds %struct.Node, ptr %50, i32 0, i32 0
  %52 = load i32, ptr %51, align 4, !tbaa !9
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %54
  %56 = getelementptr inbounds %struct.Node, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %56, align 4, !tbaa !9
  %58 = load i32, ptr %9, align 4, !tbaa !5
  %59 = load i32, ptr %13, align 4, !tbaa !5
  %60 = load i32, ptr %11, align 4, !tbaa !5
  %61 = call noundef i32 @_Z5queryiiiii(i32 noundef %52, i32 noundef %57, i32 noundef %58, i32 noundef %59, i32 noundef %60)
  store i32 %61, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %80

62:                                               ; preds = %20
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %64
  %66 = getelementptr inbounds %struct.Node, ptr %65, i32 0, i32 1
  %67 = load i32, ptr %66, align 4, !tbaa !12
  %68 = load i32, ptr %8, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %69
  %71 = getelementptr inbounds %struct.Node, ptr %70, i32 0, i32 1
  %72 = load i32, ptr %71, align 4, !tbaa !12
  %73 = load i32, ptr %13, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  %75 = load i32, ptr %10, align 4, !tbaa !5
  %76 = load i32, ptr %11, align 4, !tbaa !5
  %77 = load i32, ptr %12, align 4, !tbaa !5
  %78 = sub nsw i32 %76, %77
  %79 = call noundef i32 @_Z5queryiiiii(i32 noundef %67, i32 noundef %72, i32 noundef %74, i32 noundef %75, i32 noundef %78)
  store i32 %79, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %80

80:                                               ; preds = %62, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %81

81:                                               ; preds = %80, %18
  %82 = load i32, ptr %6, align 4
  ret i32 %82
}

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, !6, i64 0}
!10 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = !{!10, !6, i64 8}
!12 = !{!10, !6, i64 4}


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

%struct.Node = type { i32, i32, i32 }

@tr = external dso_local local_unnamed_addr global [4200210 x %struct.Node], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  br label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %23, %5
  %.tr.ph = phi i32 [ %25, %23 ], [ %0, %5 ]
  %.tr27.ph = phi i32 [ %27, %23 ], [ %1, %5 ]
  %.tr28.ph = phi i32 [ %28, %23 ], [ %2, %5 ]
  %.tr29.ph = phi i32 [ %.tr29, %23 ], [ %3, %5 ]
  %.tr30.ph = phi i32 [ %29, %23 ], [ %4, %5 ]
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.outer, %7
  %.tr = phi i32 [ %10, %7 ], [ %.tr.ph, %tailrecurse.outer ]
  %.tr27 = phi i32 [ %16, %7 ], [ %.tr27.ph, %tailrecurse.outer ]
  %.tr29 = phi i32 [ %22, %7 ], [ %.tr29.ph, %tailrecurse.outer ]
  %6 = icmp eq i32 %.tr28.ph, %.tr29
  br i1 %6, label %30, label %7

7:                                                ; preds = %tailrecurse
  %8 = sext i32 %.tr to i64
  %9 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %11, i32 2
  %13 = load i32, ptr %12, align 4, !tbaa !10
  %14 = sext i32 %.tr27 to i64
  %15 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [4200210 x %struct.Node], ptr @tr, i64 0, i64 %17, i32 2
  %19 = load i32, ptr %18, align 4, !tbaa !10
  %20 = sub nsw i32 %13, %19
  %21 = add nsw i32 %.tr29, %.tr28.ph
  %22 = ashr i32 %21, 1
  %.not = icmp slt i32 %20, %.tr30.ph
  br i1 %.not, label %23, label %tailrecurse

23:                                               ; preds = %7
  %24 = getelementptr inbounds i8, ptr %9, i64 4
  %25 = load i32, ptr %24, align 4, !tbaa !11
  %26 = getelementptr inbounds i8, ptr %15, i64 4
  %27 = load i32, ptr %26, align 4, !tbaa !11
  %28 = add nsw i32 %22, 1
  %29 = sub nsw i32 %.tr30.ph, %20
  br label %tailrecurse.outer

30:                                               ; preds = %tailrecurse
  ret i32 %.tr28.ph
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, 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}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!6, !7, i64 4}


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