; LLVM IR from sunkafei/xcpc-algorithm-templates :: 算法/数据结构/可持久化整数集合.ll (Z5countii)

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

@T = external dso_local global [5250000 x %struct.node], align 16
@len = external dso_local global i32, align 4
@info = external dso_local global [210000 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z7rankingi(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
define dso_local noundef i32 @_Z5countii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, 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
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %11 = load i32, ptr @len, align 4, !tbaa !5
  store i32 %11, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = call noundef i32 @_Z7rankingi(i32 noundef %12)
  store i32 %13, ptr %8, align 4, !tbaa !5
  %14 = load i32, ptr %8, align 4, !tbaa !5
  %15 = load i32, ptr @len, align 4, !tbaa !5
  %16 = icmp sgt i32 %14, %15
  br i1 %16, label %24, label %17

17:                                               ; preds = %2
  %18 = load i32, ptr %8, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [210000 x i32], ptr @info, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = icmp ne i32 %21, %22
  br i1 %23, label %24, label %25

24:                                               ; preds = %17, %2
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %69

25:                                               ; preds = %17
  br label %26

26:                                               ; preds = %62, %25
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %30, label %37

30:                                               ; preds = %26
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [5250000 x %struct.node], ptr @T, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.node, ptr %33, i32 0, i32 2
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = icmp sgt i32 %35, 0
  br label %37

37:                                               ; preds = %30, %26
  %38 = phi i1 [ false, %26 ], [ %36, %30 ]
  br i1 %38, label %39, label %63

39:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = add nsw i32 %40, %41
  %43 = sdiv i32 %42, 2
  store i32 %43, ptr %10, align 4, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = load i32, ptr %10, align 4, !tbaa !5
  %46 = icmp sle i32 %44, %45
  br i1 %46, label %47, label %54

47:                                               ; preds = %39
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [5250000 x %struct.node], ptr @T, i64 0, i64 %49
  %51 = getelementptr inbounds %struct.node, ptr %50, i32 0, i32 0
  %52 = load i32, ptr %51, align 4, !tbaa !11
  store i32 %52, ptr %4, align 4, !tbaa !5
  %53 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %53, ptr %7, align 4, !tbaa !5
  br label %62

54:                                               ; preds = %39
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [5250000 x %struct.node], ptr @T, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.node, ptr %57, i32 0, i32 1
  %59 = load i32, ptr %58, align 4, !tbaa !12
  store i32 %59, ptr %4, align 4, !tbaa !5
  %60 = load i32, ptr %10, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %6, align 4, !tbaa !5
  br label %62

62:                                               ; preds = %54, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %26, !llvm.loop !13

63:                                               ; preds = %37
  %64 = load i32, ptr %4, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [5250000 x %struct.node], ptr @T, i64 0, i64 %65
  %67 = getelementptr inbounds %struct.node, ptr %66, i32 0, i32 2
  %68 = load i32, ptr %67, align 4, !tbaa !9
  store i32 %68, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %69

69:                                               ; preds = %63, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %70 = load i32, ptr %3, align 4
  ret i32 %70
}

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

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

@T = external dso_local local_unnamed_addr global [5250000 x %struct.node], align 16
@len = external dso_local local_unnamed_addr global i32, align 4
@info = external dso_local local_unnamed_addr global [210000 x i32], align 16

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5countii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @len, align 4, !tbaa !5
  %4 = tail call noundef i32 @_Z7rankingi(i32 noundef %1)
  %5 = load i32, ptr @len, align 4, !tbaa !5
  %6 = icmp sgt i32 %4, %5
  br i1 %6, label %25, label %7

7:                                                ; preds = %2
  %8 = sext i32 %4 to i64
  %9 = getelementptr inbounds [210000 x i32], ptr @info, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not = icmp eq i32 %10, %1
  br i1 %.not, label %.preheader, label %25

.preheader:                                       ; preds = %7
  %11 = icmp sgt i32 %3, 1
  br i1 %11, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %.preheader, %17
  %.01728 = phi i32 [ %.1, %17 ], [ %0, %.preheader ]
  %.01827 = phi i32 [ %.119, %17 ], [ %3, %.preheader ]
  %.02026 = phi i32 [ %.121, %17 ], [ 1, %.preheader ]
  %12 = sext i32 %.01728 to i64
  %13 = getelementptr inbounds [5250000 x %struct.node], ptr @T, i64 0, i64 %12
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = icmp sgt i32 %15, 0
  br i1 %16, label %17, label %.critedge

17:                                               ; preds = %.lr.ph
  %18 = add nuw nsw i32 %.01827, %.02026
  %19 = lshr i32 %18, 1
  %.not25 = icmp sgt i32 %4, %19
  %20 = add nuw nsw i32 %19, 1
  %.121 = select i1 %.not25, i32 %20, i32 %.02026
  %.119 = select i1 %.not25, i32 %.01827, i32 %19
  %.1.in.idx = select i1 %.not25, i64 4, i64 0
  %.1.in = getelementptr inbounds i8, ptr %13, i64 %.1.in.idx
  %.1 = load i32, ptr %.1.in, align 4, !tbaa !5
  %21 = icmp slt i32 %.121, %.119
  br i1 %21, label %.lr.ph, label %.critedge, !llvm.loop !11

.critedge:                                        ; preds = %.lr.ph, %17, %.preheader
  %.017.lcssa = phi i32 [ %0, %.preheader ], [ %.1, %17 ], [ %.01728, %.lr.ph ]
  %22 = sext i32 %.017.lcssa to i64
  %23 = getelementptr inbounds [5250000 x %struct.node], ptr @T, i64 0, i64 %22, i32 2
  %24 = load i32, ptr %23, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %2, %7, %.critedge
  %.0 = phi i32 [ %24, %.critedge ], [ 0, %7 ], [ 0, %2 ]
  ret i32 %.0
}

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 = !{!10, !6, i64 8}
!10 = !{!"_ZTS4node", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass