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

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

; Function Attrs: nounwind uwtable
define dso_local void @disunion(ptr noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !9
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = load i64, ptr %5, align 8, !tbaa !9
  %9 = getelementptr inbounds i64, ptr %7, i64 %8
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %11 = sitofp i64 %10 to double
  %12 = call double @llvm.fabs.f64(double %11)
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = getelementptr inbounds i64, ptr %13, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !9
  %17 = sitofp i64 %16 to double
  %18 = call double @llvm.fabs.f64(double %17)
  %19 = fcmp ogt double %12, %18
  br i1 %19, label %20, label %37

20:                                               ; preds = %3
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = getelementptr inbounds i64, ptr %21, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = getelementptr inbounds i64, ptr %25, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !9
  %29 = add nsw i64 %24, %28
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load i64, ptr %6, align 8, !tbaa !9
  %32 = getelementptr inbounds i64, ptr %30, i64 %31
  store i64 %29, ptr %32, align 8, !tbaa !9
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load i64, ptr %5, align 8, !tbaa !9
  %36 = getelementptr inbounds i64, ptr %34, i64 %35
  store i64 %33, ptr %36, align 8, !tbaa !9
  br label %54

37:                                               ; preds = %3
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load i64, ptr %5, align 8, !tbaa !9
  %40 = getelementptr inbounds i64, ptr %38, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !9
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i64, ptr %6, align 8, !tbaa !9
  %44 = getelementptr inbounds i64, ptr %42, i64 %43
  %45 = load i64, ptr %44, align 8, !tbaa !9
  %46 = add nsw i64 %41, %45
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = load i64, ptr %5, align 8, !tbaa !9
  %49 = getelementptr inbounds i64, ptr %47, i64 %48
  store i64 %46, ptr %49, align 8, !tbaa !9
  %50 = load i64, ptr %5, align 8, !tbaa !9
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i64, ptr %6, align 8, !tbaa !9
  %53 = getelementptr inbounds i64, ptr %51, i64 %52
  store i64 %50, ptr %53, align 8, !tbaa !9
  br label %54

54:                                               ; preds = %37, %20
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fabs.f64(double) #1

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local void @disunion(ptr nocapture noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i64, ptr %0, i64 %1
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = sitofp i64 %5 to double
  %7 = tail call double @llvm.fabs.f64(double %6)
  %8 = getelementptr inbounds i64, ptr %0, i64 %2
  %9 = load i64, ptr %8, align 8, !tbaa !5
  %10 = sitofp i64 %9 to double
  %11 = tail call double @llvm.fabs.f64(double %10)
  %12 = fcmp ogt double %7, %11
  %13 = add nsw i64 %9, %5
  br i1 %12, label %14, label %15

14:                                               ; preds = %3
  store i64 %13, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %4, align 8, !tbaa !5
  br label %16

15:                                               ; preds = %3
  store i64 %13, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %8, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %15, %14
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fabs.f64(double) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass