; LLVM IR from ahampriyanshu/algo-ds-101 :: Algorithms/Graph/Kruskal's_MST (KruskalMST)

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

@nVertices = external dso_local global i32, align 4
@GraphEdgeWeights = external dso_local global [30 x [30 x i32]], align 16
@.str.3 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [15 x i8], align 1

; 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 @KruskalMST() #1

; Function Attrs: nounwind uwtable
declare dso_local void @PrintResult() #1

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
define dso_local void @main() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %5 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef @nVertices)
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %63, %0
  %8 = load i32, ptr %1, align 4, !tbaa !5
  %9 = load i32, ptr @nVertices, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %11, label %66

11:                                               ; preds = %7
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %59, %11
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = load i32, ptr @nVertices, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %62

16:                                               ; preds = %12
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = icmp ne i32 %17, %18
  br i1 %19, label %20, label %51

20:                                               ; preds = %16
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %24, label %36

24:                                               ; preds = %20
  %25 = load i32, ptr %1, align 4, !tbaa !5
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, i32 noundef %25, i32 noundef %26)
  %28 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef %4)
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = load i32, ptr %1, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [30 x [30 x i32]], ptr @GraphEdgeWeights, i64 0, i64 %31
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [30 x i32], ptr %32, i64 0, i64 %34
  store i32 %29, ptr %35, align 4, !tbaa !5
  br label %50

36:                                               ; preds = %20
  %37 = load i32, ptr %2, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [30 x [30 x i32]], ptr @GraphEdgeWeights, i64 0, i64 %38
  %40 = load i32, ptr %1, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [30 x i32], ptr %39, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = load i32, ptr %1, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [30 x [30 x i32]], ptr @GraphEdgeWeights, i64 0, i64 %45
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [30 x i32], ptr %46, i64 0, i64 %48
  store i32 %43, ptr %49, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %36, %24
  br label %58

51:                                               ; preds = %16
  %52 = load i32, ptr %1, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [30 x [30 x i32]], ptr @GraphEdgeWeights, i64 0, i64 %53
  %55 = load i32, ptr %2, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [30 x i32], ptr %54, i64 0, i64 %56
  store i32 0, ptr %57, align 4, !tbaa !5
  br label %58

58:                                               ; preds = %51, %50
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %2, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %2, align 4, !tbaa !5
  br label %12, !llvm.loop !9

62:                                               ; preds = %12
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %1, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %1, align 4, !tbaa !5
  br label %7, !llvm.loop !11

66:                                               ; preds = %7
  call void @KruskalMST()
  call void @PrintResult()
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #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 = { "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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

@nVertices = external dso_local global i32, align 4
@GraphEdgeWeights = external dso_local local_unnamed_addr global [30 x [30 x i32]], align 16
@.str.3 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [15 x i8], align 1

; 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 @KruskalMST() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @PrintResult() local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @main() local_unnamed_addr #1 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  %2 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull @nVertices)
  %4 = load i32, ptr @nVertices, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.preheader, label %._crit_edge21

.preheader:                                       ; preds = %0, %._crit_edge
  %6 = phi i32 [ %31, %._crit_edge ], [ %4, %0 ]
  %indvars.iv24 = phi i64 [ %indvars.iv.next25, %._crit_edge ], [ 0, %0 ]
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %8 = trunc nuw nsw i64 %indvars.iv24 to i32
  %9 = getelementptr inbounds [30 x [30 x i32]], ptr @GraphEdgeWeights, i64 0, i64 %indvars.iv24, i64 %indvars.iv24
  br label %10

10:                                               ; preds = %.lr.ph, %26
  %11 = phi i32 [ %6, %.lr.ph ], [ %27, %26 ]
  %12 = phi i32 [ %6, %.lr.ph ], [ %28, %26 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %26 ]
  %.not = icmp eq i64 %indvars.iv24, %indvars.iv
  br i1 %.not, label %25, label %13

13:                                               ; preds = %10
  %14 = icmp ult i64 %indvars.iv24, %indvars.iv
  br i1 %14, label %15, label %21

15:                                               ; preds = %13
  %16 = trunc nuw nsw i64 %indvars.iv to i32
  %17 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, i32 noundef %8, i32 noundef %16)
  %18 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull %1)
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = getelementptr inbounds [30 x [30 x i32]], ptr @GraphEdgeWeights, i64 0, i64 %indvars.iv24, i64 %indvars.iv
  store i32 %19, ptr %20, align 4, !tbaa !5
  %.pre = load i32, ptr @nVertices, align 4, !tbaa !5
  br label %26

21:                                               ; preds = %13
  %22 = getelementptr inbounds [30 x [30 x i32]], ptr @GraphEdgeWeights, i64 0, i64 %indvars.iv, i64 %indvars.iv24
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = getelementptr inbounds [30 x [30 x i32]], ptr @GraphEdgeWeights, i64 0, i64 %indvars.iv24, i64 %indvars.iv
  store i32 %23, ptr %24, align 4, !tbaa !5
  br label %26

25:                                               ; preds = %10
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %25, %21, %15
  %27 = phi i32 [ %11, %25 ], [ %11, %21 ], [ %.pre, %15 ]
  %28 = phi i32 [ %12, %25 ], [ %12, %21 ], [ %.pre, %15 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %29 = sext i32 %28 to i64
  %30 = icmp slt i64 %indvars.iv.next, %29
  br i1 %30, label %10, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %26, %.preheader
  %31 = phi i32 [ %6, %.preheader ], [ %27, %26 ]
  %indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, 1
  %32 = sext i32 %31 to i64
  %33 = icmp slt i64 %indvars.iv.next25, %32
  br i1 %33, label %.preheader, label %._crit_edge21, !llvm.loop !11

._crit_edge21:                                    ; preds = %._crit_edge, %0
  call void @KruskalMST()
  call void @PrintResult()
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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 = { nofree nounwind "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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10, !12}
!12 = !{!"llvm.loop.unswitch.partial.disable"}


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