; LLVM IR from sbshah97/DS-Algo-Handbook :: Algorithms/Graph_Algorithms/Prim's_MST (prims)

; 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 { i64, i64, ptr }

@arr = external dso_local global [1000005 x %struct.node], align 16
@heapsize = external dso_local global i64, 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @insert(i64 noundef %0, i64 noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  %7 = load i64, ptr @heapsize, align 8, !tbaa !5
  %8 = add nsw i64 %7, 1
  store i64 %8, ptr @heapsize, align 8, !tbaa !5
  %9 = load i64, ptr %4, align 8, !tbaa !5
  %10 = load i64, ptr @heapsize, align 8, !tbaa !5
  %11 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %10
  %12 = getelementptr inbounds %struct.node, ptr %11, i32 0, i32 1
  store i64 %9, ptr %12, align 8, !tbaa !9
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = load i64, ptr @heapsize, align 8, !tbaa !5
  %15 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %14
  %16 = getelementptr inbounds %struct.node, ptr %15, i32 0, i32 0
  store i64 %13, ptr %16, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %17 = load i64, ptr @heapsize, align 8, !tbaa !5
  %18 = sdiv i64 %17, 2
  store i64 %18, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %19 = load i64, ptr @heapsize, align 8, !tbaa !5
  store i64 %19, ptr %6, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %32, %2
  %21 = load i64, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.node, ptr %22, i32 0, i32 1
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = load i64, ptr %4, align 8, !tbaa !5
  %26 = icmp sgt i64 %24, %25
  br i1 %26, label %27, label %30

27:                                               ; preds = %20
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = icmp sge i64 %28, 1
  br label %30

30:                                               ; preds = %27, %20
  %31 = phi i1 [ false, %20 ], [ %29, %27 ]
  br i1 %31, label %32, label %58

32:                                               ; preds = %30
  %33 = load i64, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.node, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !9
  %37 = load i64, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.node, ptr %38, i32 0, i32 1
  store i64 %36, ptr %39, align 8, !tbaa !9
  %40 = load i64, ptr %4, align 8, !tbaa !5
  %41 = load i64, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.node, ptr %42, i32 0, i32 1
  store i64 %40, ptr %43, align 8, !tbaa !9
  %44 = load i64, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %44
  %46 = getelementptr inbounds %struct.node, ptr %45, i32 0, i32 0
  %47 = load i64, ptr %46, align 8, !tbaa !12
  %48 = load i64, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.node, ptr %49, i32 0, i32 0
  store i64 %47, ptr %50, align 8, !tbaa !12
  %51 = load i64, ptr %3, align 8, !tbaa !5
  %52 = load i64, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %52
  %54 = getelementptr inbounds %struct.node, ptr %53, i32 0, i32 0
  store i64 %51, ptr %54, align 8, !tbaa !12
  %55 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %55, ptr %6, align 8, !tbaa !5
  %56 = load i64, ptr %5, align 8, !tbaa !5
  %57 = sdiv i64 %56, 2
  store i64 %57, ptr %5, align 8, !tbaa !5
  br label %20, !llvm.loop !13

58:                                               ; preds = %30
  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 = { 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 = { 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/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"node", !6, i64 0, !6, i64 8, !11, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 0}
!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 { i64, i64, ptr }

@arr = external dso_local local_unnamed_addr global [1000005 x %struct.node], align 16
@heapsize = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @insert(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = load i64, ptr @heapsize, align 8, !tbaa !5
  %4 = add nsw i64 %3, 1
  store i64 %4, ptr @heapsize, align 8, !tbaa !5
  %5 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %4, i32 1
  store i64 %1, ptr %5, align 8, !tbaa !9
  %6 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %4
  store i64 %0, ptr %6, align 8, !tbaa !12
  %7 = sdiv i64 %4, 2
  %8 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %7
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %11 = icmp sgt i64 %10, %1
  %12 = icmp sgt i64 %3, 0
  %13 = and i1 %12, %11
  br i1 %13, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2, %.lr.ph
  %14 = phi i64 [ %23, %.lr.ph ], [ %10, %2 ]
  %15 = phi ptr [ %22, %.lr.ph ], [ %9, %2 ]
  %16 = phi ptr [ %21, %.lr.ph ], [ %8, %2 ]
  %.019 = phi i64 [ %.01518, %.lr.ph ], [ %4, %2 ]
  %.01518 = phi i64 [ %20, %.lr.ph ], [ %7, %2 ]
  %17 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %.019
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  store i64 %14, ptr %18, align 8, !tbaa !9
  store i64 %1, ptr %15, align 8, !tbaa !9
  %19 = load i64, ptr %16, align 8, !tbaa !12
  store i64 %19, ptr %17, align 8, !tbaa !12
  store i64 %0, ptr %16, align 8, !tbaa !12
  %20 = lshr i64 %.01518, 1
  %21 = getelementptr inbounds [1000005 x %struct.node], ptr @arr, i64 0, i64 %20
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %24 = icmp sgt i64 %23, %1
  %25 = icmp ugt i64 %.01518, 1
  %26 = and i1 %25, %24
  br i1 %26, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %2
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"node", !6, i64 0, !6, i64 8, !11, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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