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

@spanedge = external dso_local global i64, align 8
@counter = external dso_local global i64, align 8
@res = external dso_local global i64, align 8
@ptrarr = external dso_local global [1000005 x ptr], align 16
@vis = external dso_local global [1000005 x i64], align 16
@poped = external dso_local global %struct.node, 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
declare dso_local void @insert(i64 noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @pop(ptr dead_on_unwind noalias writable sret(%struct.node) align 8) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @prims(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca %struct.node, align 8
  %6 = alloca %struct.node, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  %7 = load i64, ptr @spanedge, align 8, !tbaa !5
  %8 = load i64, ptr @counter, align 8, !tbaa !5
  %9 = icmp eq i64 %7, %8
  br i1 %9, label %10, label %12

10:                                               ; preds = %1
  %11 = load i64, ptr @res, align 8, !tbaa !5
  store i64 %11, ptr %2, align 8
  br label %54

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds [1000005 x ptr], ptr @ptrarr, i64 0, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  store ptr %15, ptr %4, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %33, %12
  %17 = load ptr, ptr %4, align 8, !tbaa !9
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %37

19:                                               ; preds = %16
  %20 = load ptr, ptr %4, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.node, ptr %20, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = getelementptr inbounds [1000005 x i64], ptr @vis, i64 0, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !5
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %33

26:                                               ; preds = %19
  %27 = load ptr, ptr %4, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.node, ptr %27, i32 0, i32 0
  %29 = load i64, ptr %28, align 8, !tbaa !11
  %30 = load ptr, ptr %4, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.node, ptr %30, i32 0, i32 1
  %32 = load i64, ptr %31, align 8, !tbaa !13
  call void @insert(i64 noundef %29, i64 noundef %32)
  br label %33

33:                                               ; preds = %26, %19
  %34 = load ptr, ptr %4, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.node, ptr %34, i32 0, i32 2
  %36 = load ptr, ptr %35, align 8, !tbaa !14
  store ptr %36, ptr %4, align 8, !tbaa !9
  br label %16, !llvm.loop !15

37:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 24, ptr %5) #3
  call void @pop(ptr dead_on_unwind writable sret(%struct.node) align 8 %5)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 @poped, ptr align 8 %5, i64 24, i1 false), !tbaa.struct !17
  call void @llvm.lifetime.end.p0(i64 24, ptr %5) #3
  br label %38

38:                                               ; preds = %43, %37
  %39 = load i64, ptr @poped, align 8, !tbaa !11
  %40 = getelementptr inbounds [1000005 x i64], ptr @vis, i64 0, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = icmp eq i64 %41, 1
  br i1 %42, label %43, label %44

43:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 24, ptr %6) #3
  call void @pop(ptr dead_on_unwind writable sret(%struct.node) align 8 %6)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 @poped, ptr align 8 %6, i64 24, i1 false), !tbaa.struct !17
  call void @llvm.lifetime.end.p0(i64 24, ptr %6) #3
  br label %38, !llvm.loop !18

44:                                               ; preds = %38
  %45 = load i64, ptr @res, align 8, !tbaa !5
  %46 = load i64, ptr getelementptr inbounds (%struct.node, ptr @poped, i32 0, i32 1), align 8, !tbaa !13
  %47 = add nsw i64 %45, %46
  store i64 %47, ptr @res, align 8, !tbaa !5
  %48 = load i64, ptr @counter, align 8, !tbaa !5
  %49 = add nsw i64 %48, 1
  store i64 %49, ptr @counter, align 8, !tbaa !5
  %50 = load i64, ptr @poped, align 8, !tbaa !11
  %51 = getelementptr inbounds [1000005 x i64], ptr @vis, i64 0, i64 %50
  store i64 1, ptr %51, align 8, !tbaa !5
  %52 = load i64, ptr @poped, align 8, !tbaa !11
  %53 = call i64 @prims(i64 noundef %52)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %54

54:                                               ; preds = %44, %10
  %55 = load i64, ptr %2, align 8
  ret i64 %55
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"node", !6, i64 0, !6, i64 8, !10, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !10, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{i64 0, i64 8, !5, i64 8, i64 8, !5, i64 16, i64 8, !9}
!18 = distinct !{!18, !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"

%struct.node = type { i64, i64, ptr }

@spanedge = external dso_local local_unnamed_addr global i64, align 8
@counter = external dso_local local_unnamed_addr global i64, align 8
@res = external dso_local local_unnamed_addr global i64, align 8
@ptrarr = external dso_local local_unnamed_addr global [1000005 x ptr], align 16
@vis = external dso_local local_unnamed_addr global [1000005 x i64], align 16
@poped = external dso_local local_unnamed_addr global %struct.node, align 8

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @insert(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @pop(ptr dead_on_unwind noalias writable sret(%struct.node) align 8) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i64 @prims(i64 noundef %0) local_unnamed_addr #1 {
  %2 = alloca %struct.node, align 8
  %3 = alloca %struct.node, align 8
  %4 = load i64, ptr @spanedge, align 8, !tbaa !5
  %5 = load i64, ptr @counter, align 8, !tbaa !5
  %6 = icmp eq i64 %4, %5
  br i1 %6, label %7, label %9

7:                                                ; preds = %1
  %8 = load i64, ptr @res, align 8, !tbaa !5
  br label %35

9:                                                ; preds = %1
  %10 = getelementptr inbounds [1000005 x ptr], ptr @ptrarr, i64 0, i64 %0
  %.08 = load ptr, ptr %10, align 8, !tbaa !9
  %.not9 = icmp eq ptr %.08, null
  br i1 %.not9, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %9, %18
  %.010 = phi ptr [ %.0, %18 ], [ %.08, %9 ]
  %11 = load i64, ptr %.010, align 8, !tbaa !11
  %12 = getelementptr inbounds [1000005 x i64], ptr @vis, i64 0, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = icmp eq i64 %13, 0
  br i1 %14, label %15, label %18

15:                                               ; preds = %.lr.ph
  %16 = getelementptr inbounds i8, ptr %.010, i64 8
  %17 = load i64, ptr %16, align 8, !tbaa !13
  tail call void @insert(i64 noundef %11, i64 noundef %17)
  br label %18

18:                                               ; preds = %15, %.lr.ph
  %19 = getelementptr inbounds i8, ptr %.010, i64 16
  %.0 = load ptr, ptr %19, align 8, !tbaa !9
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %18, %9
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %2) #3
  call void @pop(ptr dead_on_unwind nonnull writable sret(%struct.node) align 8 %2)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) @poped, ptr noundef nonnull align 8 dereferenceable(24) %2, i64 24, i1 false), !tbaa.struct !16
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %2) #3
  %20 = load i64, ptr @poped, align 8, !tbaa !11
  %21 = getelementptr inbounds [1000005 x i64], ptr @vis, i64 0, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = icmp eq i64 %22, 1
  br i1 %23, label %.lr.ph13, label %._crit_edge14

.lr.ph13:                                         ; preds = %._crit_edge, %.lr.ph13
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #3
  call void @pop(ptr dead_on_unwind nonnull writable sret(%struct.node) align 8 %3)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) @poped, ptr noundef nonnull align 8 dereferenceable(24) %3, i64 24, i1 false), !tbaa.struct !16
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #3
  %24 = load i64, ptr @poped, align 8, !tbaa !11
  %25 = getelementptr inbounds [1000005 x i64], ptr @vis, i64 0, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = icmp eq i64 %26, 1
  br i1 %27, label %.lr.ph13, label %._crit_edge14, !llvm.loop !17

._crit_edge14:                                    ; preds = %.lr.ph13, %._crit_edge
  %.lcssa11 = phi i64 [ %20, %._crit_edge ], [ %24, %.lr.ph13 ]
  %28 = getelementptr inbounds [1000005 x i64], ptr @vis, i64 0, i64 %.lcssa11
  %29 = load i64, ptr @res, align 8, !tbaa !5
  %30 = load i64, ptr getelementptr inbounds (i8, ptr @poped, i64 8), align 8, !tbaa !13
  %31 = add nsw i64 %30, %29
  store i64 %31, ptr @res, align 8, !tbaa !5
  %32 = load i64, ptr @counter, align 8, !tbaa !5
  %33 = add nsw i64 %32, 1
  store i64 %33, ptr @counter, align 8, !tbaa !5
  store i64 1, ptr %28, align 8, !tbaa !5
  %34 = call i64 @prims(i64 noundef %.lcssa11)
  br label %35

35:                                               ; preds = %._crit_edge14, %7
  %.06 = phi i64 [ %8, %7 ], [ undef, %._crit_edge14 ]
  ret i64 %.06
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"node", !6, i64 0, !6, i64 8, !10, i64 16}
!13 = !{!12, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{i64 0, i64 8, !5, i64 8, i64 8, !5, i64 16, i64 8, !9}
!17 = distinct !{!17, !15}


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