; LLVM IR from TheAlgorithms/C-Plus-Plus :: graph/kruskal.ll (Z7kruskalv)

; 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::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<std::pair<long, std::pair<long, long>>, std::allocator<std::pair<long, std::pair<long, long>>>>::_Vector_impl" }
%"struct.std::_Vector_base<std::pair<long, std::pair<long, long>>, std::allocator<std::pair<long, std::pair<long, long>>>>::_Vector_impl" = type { %"struct.std::_Vector_base<std::pair<long, std::pair<long, long>>, std::allocator<std::pair<long, std::pair<long, long>>>>::_Vector_impl_data" }
%"struct.std::_Vector_base<std::pair<long, std::pair<long, long>>, std::allocator<std::pair<long, std::pair<long, long>>>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%"struct.std::pair" = type { i64, %"struct.std::pair.0" }
%"struct.std::pair.0" = type { i64, i64 }

@edge = external dso_local global i64, align 8
@edges = external dso_local global %"class.std::vector", 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: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z4rooti(i32 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z4joinii(i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z7kruskalv() #1 {
  %1 = alloca i64, align 8
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  store i64 0, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %44, %0
  %6 = load i32, ptr %2, align 4, !tbaa !9
  %7 = sext i32 %6 to i64
  %8 = load i64, ptr @edge, align 8, !tbaa !5
  %9 = icmp slt i64 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  br label %47

11:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %12 = load i32, ptr %2, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorISt4pairIlS0_IllEESaIS2_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @edges, i64 noundef %13) #2
  %15 = getelementptr inbounds %"struct.std::pair", ptr %14, i32 0, i32 1
  %16 = getelementptr inbounds %"struct.std::pair.0", ptr %15, i32 0, i32 0
  %17 = load i64, ptr %16, align 8, !tbaa !11
  store i64 %17, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %18 = load i32, ptr %2, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorISt4pairIlS0_IllEESaIS2_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @edges, i64 noundef %19) #2
  %21 = getelementptr inbounds %"struct.std::pair", ptr %20, i32 0, i32 1
  %22 = getelementptr inbounds %"struct.std::pair.0", ptr %21, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !14
  store i64 %23, ptr %4, align 8, !tbaa !5
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = trunc i64 %24 to i32
  %26 = call noundef i32 @_Z4rooti(i32 noundef %25)
  %27 = load i64, ptr %4, align 8, !tbaa !5
  %28 = trunc i64 %27 to i32
  %29 = call noundef i32 @_Z4rooti(i32 noundef %28)
  %30 = icmp ne i32 %26, %29
  br i1 %30, label %31, label %43

31:                                               ; preds = %11
  %32 = load i32, ptr %2, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorISt4pairIlS0_IllEESaIS2_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @edges, i64 noundef %33) #2
  %35 = getelementptr inbounds %"struct.std::pair", ptr %34, i32 0, i32 0
  %36 = load i64, ptr %35, align 8, !tbaa !15
  %37 = load i64, ptr %1, align 8, !tbaa !5
  %38 = add nsw i64 %37, %36
  store i64 %38, ptr %1, align 8, !tbaa !5
  %39 = load i64, ptr %3, align 8, !tbaa !5
  %40 = trunc i64 %39 to i32
  %41 = load i64, ptr %4, align 8, !tbaa !5
  %42 = trunc i64 %41 to i32
  call void @_Z4joinii(i32 noundef %40, i32 noundef %42)
  br label %43

43:                                               ; preds = %31, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %2, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %2, align 4, !tbaa !9
  br label %5, !llvm.loop !16

47:                                               ; preds = %10
  %48 = load i64, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret i64 %48
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorISt4pairIlS0_IllEESaIS2_EEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #1 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTSSt4pairIlS_IllEE", !6, i64 0, !13, i64 8}
!13 = !{!"_ZTSSt4pairIllE", !6, i64 0, !6, i64 8}
!14 = !{!12, !6, i64 16}
!15 = !{!12, !6, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<std::pair<long, std::pair<long, long>>, std::allocator<std::pair<long, std::pair<long, long>>>>::_Vector_impl" }
%"struct.std::_Vector_base<std::pair<long, std::pair<long, long>>, std::allocator<std::pair<long, std::pair<long, long>>>>::_Vector_impl" = type { %"struct.std::_Vector_base<std::pair<long, std::pair<long, long>>, std::allocator<std::pair<long, std::pair<long, long>>>>::_Vector_impl_data" }
%"struct.std::_Vector_base<std::pair<long, std::pair<long, long>>, std::allocator<std::pair<long, std::pair<long, long>>>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@edge = external dso_local local_unnamed_addr global i64, align 8
@edges = external dso_local global %"class.std::vector", align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z4rooti(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z4joinii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z7kruskalv() local_unnamed_addr #0 {
  %1 = load i64, ptr @edge, align 8, !tbaa !5
  %2 = icmp sgt i64 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %17, %0
  %.0.lcssa = phi i64 [ 0, %0 ], [ %.1, %17 ]
  ret i64 %.0.lcssa

.lr.ph:                                           ; preds = %0, %17
  %indvars.iv = phi i64 [ %indvars.iv.next, %17 ], [ 0, %0 ]
  %.014 = phi i64 [ %.1, %17 ], [ 0, %0 ]
  %3 = tail call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorISt4pairIlS0_IllEESaIS2_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @edges, i64 noundef %indvars.iv) #1
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !9
  %6 = tail call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorISt4pairIlS0_IllEESaIS2_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @edges, i64 noundef %indvars.iv) #1
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %8 = load i64, ptr %7, align 8, !tbaa !12
  %9 = trunc i64 %5 to i32
  %10 = tail call noundef i32 @_Z4rooti(i32 noundef %9)
  %11 = trunc i64 %8 to i32
  %12 = tail call noundef i32 @_Z4rooti(i32 noundef %11)
  %.not = icmp eq i32 %10, %12
  br i1 %.not, label %17, label %13

13:                                               ; preds = %.lr.ph
  %14 = tail call noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorISt4pairIlS0_IllEESaIS2_EEixEm(ptr noundef nonnull align 8 dereferenceable(24) @edges, i64 noundef %indvars.iv) #1
  %15 = load i64, ptr %14, align 8, !tbaa !13
  %16 = add nsw i64 %15, %.014
  tail call void @_Z4joinii(i32 noundef %9, i32 noundef %11)
  br label %17

17:                                               ; preds = %13, %.lr.ph
  %.1 = phi i64 [ %16, %13 ], [ %.014, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %18 = load i64, ptr @edge, align 8, !tbaa !5
  %19 = icmp sgt i64 %18, %indvars.iv.next
  br i1 %19, label %.lr.ph, label %._crit_edge, !llvm.loop !14
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(24) ptr @_ZNSt6vectorISt4pairIlS0_IllEESaIS2_EEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 2

attributes #0 = { mustprogress 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 = { 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", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"_ZTSSt4pairIlS_IllEE", !6, i64 0, !11, i64 8}
!11 = !{!"_ZTSSt4pairIllE", !6, i64 0, !6, i64 8}
!12 = !{!10, !6, i64 16}
!13 = !{!10, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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