; LLVM IR from ShahjalalShohag/code-library :: Data Structures (Cartesian 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"

%"struct.std::pair" = type { i32, i32 }

@n = external dso_local global i32, align 4
@inv = external dso_local global [100009 x i64], align 16
@ans = external dso_local global i64, align 8
@a = external dso_local global [100009 x %"struct.std::pair"], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3dfsi(i32 noundef) #0

; 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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z14cartesian_treev() #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %"struct.std::pair", align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  store i64 1, ptr getelementptr inbounds ([100009 x i64], ptr @inv, i64 0, i64 1), align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 2, ptr %2, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %28, %0
  %10 = load i32, ptr %2, align 4, !tbaa !9
  %11 = icmp slt i32 %10, 100009
  br i1 %11, label %13, label %12

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

13:                                               ; preds = %9
  %14 = load i32, ptr %2, align 4, !tbaa !9
  %15 = srem i32 1000000007, %14
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100009 x i64], ptr @inv, i64 0, i64 %16
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = load i32, ptr %2, align 4, !tbaa !9
  %20 = sdiv i32 1000000007, %19
  %21 = sub nsw i32 1000000007, %20
  %22 = sext i32 %21 to i64
  %23 = mul nsw i64 %18, %22
  %24 = srem i64 %23, 1000000007
  %25 = load i32, ptr %2, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100009 x i64], ptr @inv, i64 0, i64 %26
  store i64 %24, ptr %27, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %13
  %29 = load i32, ptr %2, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %2, align 4, !tbaa !9
  br label %9, !llvm.loop !11

31:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %32 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %3)
  br label %33

33:                                               ; preds = %56, %31
  %34 = load i32, ptr %3, align 4, !tbaa !9
  %35 = add nsw i32 %34, -1
  store i32 %35, ptr %3, align 4, !tbaa !9
  %36 = icmp ne i32 %34, 0
  br i1 %36, label %37, label %68

37:                                               ; preds = %33
  %38 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 1, ptr %4, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %53, %37
  %40 = load i32, ptr %4, align 4, !tbaa !9
  %41 = load i32, ptr @n, align 4, !tbaa !9
  %42 = icmp sle i32 %40, %41
  br i1 %42, label %44, label %43

43:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %56

44:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %45 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %5)
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %46 = load i32, ptr %4, align 4, !tbaa !9
  %47 = sub nsw i32 0, %46
  store i32 %47, ptr %7, align 4, !tbaa !9
  %48 = call i64 @_ZSt9make_pairIRiiESt4pairINSt25__strip_reference_wrapperINSt5decayIT_E4typeEE6__typeENS2_INS3_IT0_E4typeEE6__typeEEOS4_OS9_(ptr noundef nonnull align 4 dereferenceable(4) %5, ptr noundef nonnull align 4 dereferenceable(4) %7)
  store i64 %48, ptr %6, align 4
  %49 = load i32, ptr %4, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [100009 x %"struct.std::pair"], ptr @a, i64 0, i64 %50
  %52 = call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt4pairIiiEaSEOS0_(ptr noundef nonnull align 4 dereferenceable(8) %51, ptr noundef nonnull align 4 dereferenceable(8) %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %53

53:                                               ; preds = %44
  %54 = load i32, ptr %4, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %4, align 4, !tbaa !9
  br label %39, !llvm.loop !13

56:                                               ; preds = %43
  %57 = load i32, ptr @n, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = mul nsw i64 1, %58
  %60 = load i64, ptr getelementptr inbounds ([100009 x i64], ptr @inv, i64 0, i64 2), align 16, !tbaa !5
  %61 = mul nsw i64 %59, %60
  %62 = srem i64 %61, 1000000007
  store i64 %62, ptr @ans, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %63 = call noundef i32 @_Z14cartesian_treev()
  store i32 %63, ptr %8, align 4, !tbaa !9
  %64 = load i32, ptr %8, align 4, !tbaa !9
  %65 = call noundef i32 @_Z3dfsi(i32 noundef %64)
  %66 = load i64, ptr @ans, align 8, !tbaa !5
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %66)
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %33, !llvm.loop !14

68:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

; Function Attrs: mustprogress uwtable
declare dso_local i64 @_ZSt9make_pairIRiiESt4pairINSt25__strip_reference_wrapperINSt5decayIT_E4typeEE6__typeENS2_INS3_IT0_E4typeEE6__typeEEOS4_OS9_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt4pairIiiEaSEOS0_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(8)) #4 align 2

declare i32 @printf(ptr noundef, ...) #3

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress norecurse 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 = { "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 #4 = { 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 #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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple 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"

%"struct.std::pair" = type { i32, i32 }

@n = external dso_local global i32, align 4
@inv = external dso_local local_unnamed_addr global [100009 x i64], align 16
@ans = external dso_local local_unnamed_addr global i64, align 8
@a = external dso_local global [100009 x %"struct.std::pair"], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3dfsi(i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z14cartesian_treev() local_unnamed_addr #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca %"struct.std::pair", align 8
  %4 = alloca i32, align 4
  store i64 1, ptr getelementptr inbounds (i8, ptr @inv, i64 8), align 8, !tbaa !5
  br label %9

5:                                                ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %7 = load i32, ptr %1, align 4, !tbaa !9
  %8 = add nsw i32 %7, -1
  store i32 %8, ptr %1, align 4, !tbaa !9
  %.not16 = icmp eq i32 %7, 0
  br i1 %.not16, label %._crit_edge19, label %.lr.ph18

9:                                                ; preds = %0, %9
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next, %9 ]
  %10 = trunc nuw nsw i64 %indvars.iv to i32
  %11 = urem i32 1000000007, %10
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds [100009 x i64], ptr @inv, i64 0, i64 %12
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = udiv i32 1000000007, %10
  %16 = sub nuw nsw i32 1000000007, %15
  %17 = zext nneg i32 %16 to i64
  %18 = mul nsw i64 %14, %17
  %19 = srem i64 %18, 1000000007
  %20 = getelementptr inbounds [100009 x i64], ptr @inv, i64 0, i64 %indvars.iv
  store i64 %19, ptr %20, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 100009
  br i1 %exitcond.not, label %5, label %9, !llvm.loop !11

.lr.ph18:                                         ; preds = %5, %._crit_edge
  %21 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %22 = load i32, ptr @n, align 4, !tbaa !9
  %.not1214 = icmp slt i32 %22, 1
  br i1 %.not1214, label %.lr.ph18.._crit_edge_crit_edge, label %.lr.ph

.lr.ph18.._crit_edge_crit_edge:                   ; preds = %.lr.ph18
  %.pre = sext i32 %22 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph18.._crit_edge_crit_edge
  %.pre-phi = phi i64 [ %.pre, %.lr.ph18.._crit_edge_crit_edge ], [ %39, %.lr.ph ]
  %23 = load i64, ptr getelementptr inbounds (i8, ptr @inv, i64 16), align 16, !tbaa !5
  %24 = mul nsw i64 %23, %.pre-phi
  %25 = srem i64 %24, 1000000007
  store i64 %25, ptr @ans, align 8, !tbaa !5
  %26 = call noundef i32 @_Z14cartesian_treev()
  %27 = call noundef i32 @_Z3dfsi(i32 noundef %26)
  %28 = load i64, ptr @ans, align 8, !tbaa !5
  %29 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %28)
  %30 = load i32, ptr %1, align 4, !tbaa !9
  %31 = add nsw i32 %30, -1
  store i32 %31, ptr %1, align 4, !tbaa !9
  %.not = icmp eq i32 %30, 0
  br i1 %.not, label %._crit_edge19, label %.lr.ph18, !llvm.loop !13

.lr.ph:                                           ; preds = %.lr.ph18, %.lr.ph
  %indvars.iv22 = phi i64 [ %indvars.iv.next23, %.lr.ph ], [ 1, %.lr.ph18 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #5
  %32 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #5
  %33 = trunc i64 %indvars.iv22 to i32
  %34 = sub i32 0, %33
  store i32 %34, ptr %4, align 4, !tbaa !9
  %35 = call i64 @_ZSt9make_pairIRiiESt4pairINSt25__strip_reference_wrapperINSt5decayIT_E4typeEE6__typeENS2_INS3_IT0_E4typeEE6__typeEEOS4_OS9_(ptr noundef nonnull align 4 dereferenceable(4) %2, ptr noundef nonnull align 4 dereferenceable(4) %4)
  store i64 %35, ptr %3, align 8
  %36 = getelementptr inbounds [100009 x %"struct.std::pair"], ptr @a, i64 0, i64 %indvars.iv22
  %37 = call noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt4pairIiiEaSEOS0_(ptr noundef nonnull align 4 dereferenceable(8) %36, ptr noundef nonnull align 4 dereferenceable(8) %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #5
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 1
  %38 = load i32, ptr @n, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %.not12.not = icmp slt i64 %indvars.iv22, %39
  br i1 %.not12.not, label %.lr.ph, label %._crit_edge, !llvm.loop !14

._crit_edge19:                                    ; preds = %._crit_edge, %5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
declare dso_local i64 @_ZSt9make_pairIRiiESt4pairINSt25__strip_reference_wrapperINSt5decayIT_E4typeEE6__typeENS2_INS3_IT0_E4typeEE6__typeEEOS4_OS9_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(8) ptr @_ZNSt4pairIiiEaSEOS0_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(8)) local_unnamed_addr #4 align 2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress norecurse 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 = { nofree nounwind "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 #4 = { 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 #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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple 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}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass