; LLVM IR from manishbisht/Competitive-Programming :: Data Structures/07 Disjoint Set/DisjointSet.ll (Z5Unionii)

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

%"class.std::map" = type { %"class.std::_Rb_tree" }
%"class.std::_Rb_tree" = type { %"struct.std::_Rb_tree<int, std::pair<const int, disjointSetNode *>, std::_Select1st<std::pair<const int, disjointSetNode *>>, std::less<int>>::_Rb_tree_impl" }
%"struct.std::_Rb_tree<int, std::pair<const int, disjointSetNode *>, std::_Select1st<std::pair<const int, disjointSetNode *>>, std::less<int>>::_Rb_tree_impl" = type { [8 x i8], %"struct.std::_Rb_tree_header" }
%"struct.std::_Rb_tree_header" = type { %"struct.std::_Rb_tree_node_base", i64 }
%"struct.std::_Rb_tree_node_base" = type { i32, ptr, ptr, ptr }
%struct.disjointSetNode = type { i32, i32, ptr, i32 }

@disjointSet = external dso_local global %"class.std::map", align 8

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

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

; 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
declare dso_local noundef ptr @_Z7findSetP15disjointSetNode(ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5Unionii(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %8 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIiP15disjointSetNodeSt4lessIiESaISt4pairIKiS1_EEEixERS5_(ptr noundef nonnull align 8 dereferenceable(48) @disjointSet, ptr noundef nonnull align 4 dereferenceable(4) %3)
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = call noundef ptr @_Z7findSetP15disjointSetNode(ptr noundef %9)
  store ptr %10, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %11 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIiP15disjointSetNodeSt4lessIiESaISt4pairIKiS1_EEEixERS5_(ptr noundef nonnull align 8 dereferenceable(48) @disjointSet, ptr noundef nonnull align 4 dereferenceable(4) %4)
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = call noundef ptr @_Z7findSetP15disjointSetNode(ptr noundef %12)
  store ptr %13, ptr %6, align 8, !tbaa !9
  %14 = load ptr, ptr %5, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.disjointSetNode, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = load ptr, ptr %6, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.disjointSetNode, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = icmp eq i32 %16, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %2
  store i32 1, ptr %7, align 4
  br label %72

22:                                               ; preds = %2
  %23 = load ptr, ptr %5, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.disjointSetNode, ptr %23, i32 0, i32 1
  %25 = load i32, ptr %24, align 4, !tbaa !13
  %26 = load ptr, ptr %6, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.disjointSetNode, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !13
  %29 = icmp sge i32 %25, %28
  br i1 %29, label %30, label %60

30:                                               ; preds = %22
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.disjointSetNode, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !13
  %34 = load ptr, ptr %5, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.disjointSetNode, ptr %34, i32 0, i32 1
  store i32 %33, ptr %35, align 4, !tbaa !13
  %36 = icmp ne i32 %33, 0
  br i1 %36, label %37, label %42

37:                                               ; preds = %30
  %38 = load ptr, ptr %5, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.disjointSetNode, ptr %38, i32 0, i32 1
  %40 = load i32, ptr %39, align 4, !tbaa !13
  %41 = add nsw i32 %40, 1
  br label %46

42:                                               ; preds = %30
  %43 = load ptr, ptr %5, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.disjointSetNode, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !13
  br label %46

46:                                               ; preds = %42, %37
  %47 = phi i32 [ %41, %37 ], [ %45, %42 ]
  %48 = load ptr, ptr %5, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.disjointSetNode, ptr %48, i32 0, i32 1
  store i32 %47, ptr %49, align 4, !tbaa !13
  %50 = load ptr, ptr %5, align 8, !tbaa !9
  %51 = load ptr, ptr %6, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.disjointSetNode, ptr %51, i32 0, i32 2
  store ptr %50, ptr %52, align 8, !tbaa !14
  %53 = load ptr, ptr %6, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.disjointSetNode, ptr %53, i32 0, i32 3
  %55 = load i32, ptr %54, align 8, !tbaa !15
  %56 = load ptr, ptr %5, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.disjointSetNode, ptr %56, i32 0, i32 3
  %58 = load i32, ptr %57, align 8, !tbaa !15
  %59 = add nsw i32 %58, %55
  store i32 %59, ptr %57, align 8, !tbaa !15
  br label %71

60:                                               ; preds = %22
  %61 = load ptr, ptr %6, align 8, !tbaa !9
  %62 = load ptr, ptr %5, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.disjointSetNode, ptr %62, i32 0, i32 2
  store ptr %61, ptr %63, align 8, !tbaa !14
  %64 = load ptr, ptr %5, align 8, !tbaa !9
  %65 = getelementptr inbounds %struct.disjointSetNode, ptr %64, i32 0, i32 3
  %66 = load i32, ptr %65, align 8, !tbaa !15
  %67 = load ptr, ptr %6, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.disjointSetNode, ptr %67, i32 0, i32 3
  %69 = load i32, ptr %68, align 8, !tbaa !15
  %70 = add nsw i32 %69, %66
  store i32 %70, ptr %68, align 8, !tbaa !15
  br label %71

71:                                               ; preds = %60, %46
  store i32 1, ptr %7, align 4
  br label %72

72:                                               ; preds = %71, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS15disjointSetNode", !6, i64 0, !6, i64 4, !10, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 4}
!14 = !{!12, !10, i64 8}
!15 = !{!12, !6, i64 16}


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

%"class.std::map" = type { %"class.std::_Rb_tree" }
%"class.std::_Rb_tree" = type { %"struct.std::_Rb_tree<int, std::pair<const int, disjointSetNode *>, std::_Select1st<std::pair<const int, disjointSetNode *>>, std::less<int>>::_Rb_tree_impl" }
%"struct.std::_Rb_tree<int, std::pair<const int, disjointSetNode *>, std::_Select1st<std::pair<const int, disjointSetNode *>>, std::less<int>>::_Rb_tree_impl" = type { [8 x i8], %"struct.std::_Rb_tree_header" }
%"struct.std::_Rb_tree_header" = type { %"struct.std::_Rb_tree_node_base", i64 }
%"struct.std::_Rb_tree_node_base" = type { i32, ptr, ptr, ptr }

@disjointSet = external dso_local global %"class.std::map", align 8

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z7findSetP15disjointSetNode(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5Unionii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %5 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIiP15disjointSetNodeSt4lessIiESaISt4pairIKiS1_EEEixERS5_(ptr noundef nonnull align 8 dereferenceable(48) @disjointSet, ptr noundef nonnull align 4 dereferenceable(4) %3)
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = call noundef ptr @_Z7findSetP15disjointSetNode(ptr noundef %6)
  %8 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt3mapIiP15disjointSetNodeSt4lessIiESaISt4pairIKiS1_EEEixERS5_(ptr noundef nonnull align 8 dereferenceable(48) @disjointSet, ptr noundef nonnull align 4 dereferenceable(4) %4)
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = call noundef ptr @_Z7findSetP15disjointSetNode(ptr noundef %9)
  %11 = load i32, ptr %7, align 8, !tbaa !11
  %12 = load i32, ptr %10, align 8, !tbaa !11
  %13 = icmp eq i32 %11, %12
  br i1 %13, label %28, label %14

14:                                               ; preds = %2
  %15 = getelementptr inbounds i8, ptr %7, i64 4
  %16 = load i32, ptr %15, align 4, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %10, i64 4
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %.not = icmp slt i32 %16, %18
  br i1 %.not, label %.sink.split, label %19

19:                                               ; preds = %14
  %.not17 = icmp eq i32 %18, 0
  %20 = add nsw i32 %18, 1
  %21 = select i1 %.not17, i32 0, i32 %20
  store i32 %21, ptr %15, align 4, !tbaa !13
  br label %.sink.split

.sink.split:                                      ; preds = %14, %19
  %.sink24 = phi ptr [ %10, %19 ], [ %7, %14 ]
  %.sink = phi ptr [ %7, %19 ], [ %10, %14 ]
  %22 = getelementptr inbounds i8, ptr %.sink24, i64 8
  store ptr %.sink, ptr %22, align 8, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %.sink24, i64 16
  %24 = load i32, ptr %23, align 8, !tbaa !15
  %25 = getelementptr inbounds i8, ptr %.sink, i64 16
  %26 = load i32, ptr %25, align 8, !tbaa !15
  %27 = add nsw i32 %26, %24
  store i32 %27, ptr %25, align 8, !tbaa !15
  br label %28

28:                                               ; preds = %.sink.split, %2
  ret void
}

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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS15disjointSetNode", !6, i64 0, !6, i64 4, !10, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 4}
!14 = !{!12, !10, i64 8}
!15 = !{!12, !6, i64 16}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass