; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x11-栈/131. 直方图中最大的矩形.ll (Z3getPi)

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

@n = external dso_local global i32, align 4
@h = external dso_local global [100010 x i32], align 16
@stk = external dso_local global [100010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3getPi(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  store i32 -1, ptr @h, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr %4, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %41, %1
  %6 = load i32, ptr %4, align 4, !tbaa !9
  %7 = load i32, ptr @n, align 4, !tbaa !9
  %8 = icmp sle i32 %6, %7
  br i1 %8, label %10, label %9

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

10:                                               ; preds = %5
  br label %11

11:                                               ; preds = %24, %10
  %12 = load i32, ptr %3, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [100010 x i32], ptr @stk, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !9
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !9
  %23 = icmp sge i32 %18, %22
  br i1 %23, label %24, label %27

24:                                               ; preds = %11
  %25 = load i32, ptr %3, align 4, !tbaa !9
  %26 = add nsw i32 %25, -1
  store i32 %26, ptr %3, align 4, !tbaa !9
  br label %11, !llvm.loop !11

27:                                               ; preds = %11
  %28 = load i32, ptr %3, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100010 x i32], ptr @stk, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = load i32, ptr %4, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  store i32 %31, ptr %35, align 4, !tbaa !9
  %36 = load i32, ptr %4, align 4, !tbaa !9
  %37 = load i32, ptr %3, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %3, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [100010 x i32], ptr @stk, i64 0, i64 %39
  store i32 %36, ptr %40, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %27
  %42 = load i32, ptr %4, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %4, align 4, !tbaa !9
  br label %5, !llvm.loop !13

44:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

; 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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !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}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@h = external dso_local local_unnamed_addr global [100010 x i32], align 16
@stk = external dso_local local_unnamed_addr global [100010 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: write, inaccessiblemem: none) uwtable
define dso_local void @_Z3getPi(ptr nocapture noundef writeonly %0) local_unnamed_addr #0 {
  store i32 -1, ptr @h, align 16, !tbaa !5
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not12 = icmp slt i32 %2, 1
  br i1 %.not12, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %1, %12
  %indvars.iv16 = phi i64 [ %indvars.iv.next17, %12 ], [ 1, %1 ]
  %.0913 = phi i32 [ %15, %12 ], [ 0, %1 ]
  %3 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %indvars.iv16
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = sext i32 %.0913 to i64
  br label %6

._crit_edge:                                      ; preds = %12, %1
  ret void

6:                                                ; preds = %6, %.preheader
  %indvars.iv = phi i64 [ %indvars.iv.next, %6 ], [ %5, %.preheader ]
  %7 = getelementptr inbounds [100010 x i32], ptr @stk, i64 0, i64 %indvars.iv
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %.not10 = icmp slt i32 %11, %4
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  br i1 %.not10, label %12, label %6, !llvm.loop !9

12:                                               ; preds = %6
  %13 = trunc nsw i64 %indvars.iv to i32
  %14 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv16
  store i32 %8, ptr %14, align 4, !tbaa !5
  %15 = add nsw i32 %13, 1
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100010 x i32], ptr @stk, i64 0, i64 %16
  %18 = trunc nuw nsw i64 %indvars.iv16 to i32
  store i32 %18, ptr %17, align 4, !tbaa !5
  %indvars.iv.next17 = add nuw nsw i64 %indvars.iv16, 1
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %.not.not = icmp slt i64 %indvars.iv16, %20
  br i1 %.not.not, label %.preheader, label %._crit_edge, !llvm.loop !11
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: write, 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, !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"}
!11 = distinct !{!11, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass