; LLVM IR from tangjz/acm-icpc :: lydsy/4499.ll (Z5queryiiii)

; 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.std::pair" = type { i32, i32 }

@seg = external dso_local global [400002 x %"struct.std::pair"], 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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local i64 @_Z5mergeSt4pairIiiES0_(i64, i64) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; 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 i64 @_Z5queryiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #4 {
  %5 = alloca %"struct.std::pair", align 4
  %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 %"struct.std::pair", align 4
  %13 = alloca %"struct.std::pair", align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  %14 = load i32, ptr %8, align 4, !tbaa !5
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = icmp sle i32 %14, %15
  br i1 %16, label %17, label %27

17:                                               ; preds = %4
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = load i32, ptr %9, align 4, !tbaa !5
  %20 = icmp sle i32 %18, %19
  br i1 %20, label %21, label %27

21:                                               ; preds = %17
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = call noundef i32 @_Z7seg_idxii(i32 noundef %22, i32 noundef %23)
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [400002 x %"struct.std::pair"], ptr @seg, i64 0, i64 %25
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %5, ptr align 8 %26, i64 8, i1 false)
  br label %68

27:                                               ; preds = %17, %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = add nsw i32 %28, %29
  %31 = ashr i32 %30, 1
  store i32 %31, ptr %10, align 4, !tbaa !5
  %32 = load i32, ptr %9, align 4, !tbaa !5
  %33 = load i32, ptr %10, align 4, !tbaa !5
  %34 = icmp sle i32 %32, %33
  br i1 %34, label %35, label %41

35:                                               ; preds = %27
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = load i32, ptr %10, align 4, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = load i32, ptr %9, align 4, !tbaa !5
  %40 = call i64 @_Z5queryiiii(i32 noundef %36, i32 noundef %37, i32 noundef %38, i32 noundef %39)
  store i64 %40, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %67

41:                                               ; preds = %27
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = load i32, ptr %10, align 4, !tbaa !5
  %44 = icmp sgt i32 %42, %43
  br i1 %44, label %45, label %52

45:                                               ; preds = %41
  %46 = load i32, ptr %10, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = load i32, ptr %8, align 4, !tbaa !5
  %50 = load i32, ptr %9, align 4, !tbaa !5
  %51 = call i64 @_Z5queryiiii(i32 noundef %47, i32 noundef %48, i32 noundef %49, i32 noundef %50)
  store i64 %51, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %67

52:                                               ; preds = %41
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = load i32, ptr %10, align 4, !tbaa !5
  %55 = load i32, ptr %8, align 4, !tbaa !5
  %56 = load i32, ptr %9, align 4, !tbaa !5
  %57 = call i64 @_Z5queryiiii(i32 noundef %53, i32 noundef %54, i32 noundef %55, i32 noundef %56)
  store i64 %57, ptr %12, align 4
  %58 = load i32, ptr %10, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = load i32, ptr %8, align 4, !tbaa !5
  %62 = load i32, ptr %9, align 4, !tbaa !5
  %63 = call i64 @_Z5queryiiii(i32 noundef %59, i32 noundef %60, i32 noundef %61, i32 noundef %62)
  store i64 %63, ptr %13, align 4
  %64 = load i64, ptr %12, align 4
  %65 = load i64, ptr %13, align 4
  %66 = call i64 @_Z5mergeSt4pairIiiES0_(i64 %64, i64 %65)
  store i64 %66, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %67

67:                                               ; preds = %52, %45, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  br label %68

68:                                               ; preds = %67, %21
  %69 = load i64, ptr %5, align 4
  ret i64 %69
}

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


; 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.std::pair" = type { i32, i32 }

@seg = external dso_local local_unnamed_addr global [400002 x %"struct.std::pair"], align 16

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local i64 @_Z5mergeSt4pairIiiES0_(i64, i64) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local i64 @_Z5queryiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #2 {
  br label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %14, %4
  %.tr.ph = phi i32 [ %15, %14 ], [ %0, %4 ]
  %.tr38.ph = phi i32 [ %.us-phi43, %14 ], [ %1, %4 ]
  %.not = icmp slt i32 %.tr.ph, %2
  br i1 %.not, label %tailrecurse.us, label %tailrecurse

tailrecurse.us:                                   ; preds = %tailrecurse.outer, %tailrecurse.us
  %.tr38.us = phi i32 [ %6, %tailrecurse.us ], [ %.tr38.ph, %tailrecurse.outer ]
  %5 = add i32 %.tr38.us, %.tr.ph
  %6 = ashr i32 %5, 1
  %.not37.us = icmp slt i32 %6, %3
  br i1 %.not37.us, label %.split.us, label %tailrecurse.us

tailrecurse:                                      ; preds = %tailrecurse.outer, %10
  %.tr38 = phi i32 [ %12, %10 ], [ %.tr38.ph, %tailrecurse.outer ]
  %.not36 = icmp sgt i32 %.tr38, %3
  br i1 %.not36, label %10, label %common.ret

common.ret:                                       ; preds = %tailrecurse
  %7 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %.tr.ph, i32 noundef %.tr38)
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [400002 x %"struct.std::pair"], ptr @seg, i64 0, i64 %8
  %.sroa.029.0.copyload = load i64, ptr %9, align 8
  br label %common.ret63

10:                                               ; preds = %tailrecurse
  %11 = add i32 %.tr38, %.tr.ph
  %12 = ashr i32 %11, 1
  %.not37 = icmp slt i32 %12, %3
  br i1 %.not37, label %.split.us, label %tailrecurse

.split.us:                                        ; preds = %10, %tailrecurse.us
  %.us-phi = phi i32 [ %6, %tailrecurse.us ], [ %12, %10 ]
  %.us-phi43 = phi i32 [ %.tr38.us, %tailrecurse.us ], [ %.tr38, %10 ]
  %13 = icmp slt i32 %.us-phi, %2
  br i1 %13, label %14, label %16

14:                                               ; preds = %.split.us
  %15 = add nsw i32 %.us-phi, 1
  br label %tailrecurse.outer

common.ret63:                                     ; preds = %16, %common.ret
  %common.ret63.op = phi i64 [ %.sroa.029.0.copyload, %common.ret ], [ %20, %16 ]
  ret i64 %common.ret63.op

16:                                               ; preds = %.split.us
  %17 = tail call i64 @_Z5queryiiii(i32 noundef %.tr.ph, i32 noundef %.us-phi, i32 noundef %2, i32 noundef %3)
  %18 = add nsw i32 %.us-phi, 1
  %19 = tail call i64 @_Z5queryiiii(i32 noundef %18, i32 noundef %.us-phi43, i32 noundef %2, i32 noundef %3)
  %20 = tail call i64 @_Z5mergeSt4pairIiiES0_(i64 %17, i64 %19)
  br label %common.ret63
}

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


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