; LLVM IR from ravireddy07/cpp-book :: src/Algorithm-Based/Kruskals.ll (Z8kruskalsP4Edgeii)

; 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.Edge = type { i32, i32, i32 }
%class.Set = type { i32, i32 }

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4findiP3Set(i32 noundef, ptr noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5UnionP3Setii(ptr noundef, i32 noundef, i32 noundef) #0

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z3cmp4EdgeS_(i64, i32, i64, i32) #2

; 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) #3

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z8kruskalsP4Edgeii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %class.Edge, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds %class.Edge, ptr %15, i64 %17
  call void @_ZSt4sortIP4EdgePFbS0_S0_EEvT_S4_T0_(ptr noundef %14, ptr noundef %18, ptr noundef @_Z3cmp4EdgeS_)
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #7
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %20, i64 8)
  %22 = extractvalue { i64, i1 } %21, 1
  %23 = extractvalue { i64, i1 } %21, 0
  %24 = select i1 %22, i64 -1, i64 %23
  %25 = call noalias noundef nonnull ptr @_Znam(i64 noundef %24) #8
  store ptr %25, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #7
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %43, %3
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #7
  br label %46

31:                                               ; preds = %26
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds %class.Set, ptr %33, i64 %35
  %37 = getelementptr inbounds %class.Set, ptr %36, i32 0, i32 0
  store i32 %32, ptr %37, align 4, !tbaa !11
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds %class.Set, ptr %38, i64 %40
  %42 = getelementptr inbounds %class.Set, ptr %41, i32 0, i32 1
  store i32 -1, ptr %42, align 4, !tbaa !13
  br label %43

43:                                               ; preds = %31
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %8, align 4, !tbaa !9
  br label %26, !llvm.loop !14

46:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #7
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #7
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %77, %46
  %48 = load i32, ptr %10, align 4, !tbaa !9
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = icmp slt i32 %48, %49
  br i1 %50, label %52, label %51

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

52:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 12, ptr %11) #7
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds %class.Edge, ptr %53, i64 %55
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %11, ptr align 4 %56, i64 12, i1 false), !tbaa.struct !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #7
  %57 = getelementptr inbounds %class.Edge, ptr %11, i32 0, i32 0
  %58 = load i32, ptr %57, align 4, !tbaa !17
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = call noundef i32 @_Z4findiP3Set(i32 noundef %58, ptr noundef %59)
  store i32 %60, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #7
  %61 = getelementptr inbounds %class.Edge, ptr %11, i32 0, i32 1
  %62 = load i32, ptr %61, align 4, !tbaa !19
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = call noundef i32 @_Z4findiP3Set(i32 noundef %62, ptr noundef %63)
  store i32 %64, ptr %13, align 4, !tbaa !9
  %65 = load i32, ptr %12, align 4, !tbaa !9
  %66 = load i32, ptr %13, align 4, !tbaa !9
  %67 = icmp ne i32 %65, %66
  br i1 %67, label %68, label %76

68:                                               ; preds = %52
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = load i32, ptr %12, align 4, !tbaa !9
  %71 = load i32, ptr %13, align 4, !tbaa !9
  call void @_Z5UnionP3Setii(ptr noundef %69, i32 noundef %70, i32 noundef %71)
  %72 = getelementptr inbounds %class.Edge, ptr %11, i32 0, i32 2
  %73 = load i32, ptr %72, align 4, !tbaa !20
  %74 = load i32, ptr %9, align 4, !tbaa !9
  %75 = add nsw i32 %74, %73
  store i32 %75, ptr %9, align 4, !tbaa !9
  br label %76

76:                                               ; preds = %68, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #7
  call void @llvm.lifetime.end.p0(i64 12, ptr %11) #7
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %10, align 4, !tbaa !9
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %10, align 4, !tbaa !9
  br label %47, !llvm.loop !21

80:                                               ; preds = %51
  %81 = load i32, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #7
  ret i32 %81
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIP4EdgePFbS0_S0_EEvT_S4_T0_(ptr noundef, ptr noundef, ptr noundef) #4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64) #5

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #6

attributes #0 = { mustprogress 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 willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { inlinehint mustprogress 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 #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { nobuiltin allocsize(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 #7 = { nounwind }
attributes #8 = { builtin allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS3Set", !10, i64 0, !10, i64 4}
!13 = !{!12, !10, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{i64 0, i64 4, !9, i64 4, i64 4, !9, i64 8, i64 4, !9}
!17 = !{!18, !10, i64 0}
!18 = !{!"_ZTS4Edge", !10, i64 0, !10, i64 4, !10, i64 8}
!19 = !{!18, !10, i64 4}
!20 = !{!18, !10, i64 8}
!21 = distinct !{!21, !15}


; 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.Edge = type { i32, i32, i32 }
%class.Set = type { i32, i32 }

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z4findiP3Set(i32 noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z3cmp4EdgeS_(i64, i32, i64, i32) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z8kruskalsP4Edgeii(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %1 to i64
  %5 = getelementptr inbounds %class.Edge, ptr %0, i64 %4
  tail call void @_ZSt4sortIP4EdgePFbS0_S0_EEvT_S4_T0_(ptr noundef %0, ptr noundef %5, ptr noundef nonnull @_Z3cmp4EdgeS_)
  %6 = zext i32 %2 to i64
  %7 = icmp slt i32 %2, 0
  %8 = shl nuw nsw i64 %6, 3
  %9 = select i1 %7, i64 -1, i64 %8
  %10 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %9) #4
  %11 = icmp sgt i32 %2, 0
  br i1 %11, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %3
  %min.iters.check = icmp ult i32 %2, 4
  br i1 %min.iters.check, label %.lr.ph.preheader41, label %vector.ph

.lr.ph.preheader41:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %6, 2147483644
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <2 x i32> [ <i32 0, i32 1>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %step.add = add <2 x i32> %vec.ind, <i32 2, i32 2>
  %12 = or disjoint i64 %index, 2
  %13 = getelementptr inbounds %class.Set, ptr %10, i64 %index
  %14 = getelementptr inbounds %class.Set, ptr %10, i64 %12
  %interleaved.vec = shufflevector <2 x i32> %vec.ind, <2 x i32> <i32 -1, i32 -1>, <4 x i32> <i32 0, i32 2, i32 1, i32 3>
  store <4 x i32> %interleaved.vec, ptr %13, align 4, !tbaa !5
  %interleaved.vec40 = shufflevector <2 x i32> %step.add, <2 x i32> <i32 -1, i32 -1>, <4 x i32> <i32 0, i32 2, i32 1, i32 3>
  store <4 x i32> %interleaved.vec40, ptr %14, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %vec.ind.next = add <2 x i32> %vec.ind, <i32 4, i32 4>
  %15 = icmp eq i64 %index.next, %n.vec
  br i1 %15, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %6
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader41

.preheader:                                       ; preds = %.lr.ph, %middle.block, %3
  %16 = icmp sgt i32 %1, 0
  br i1 %16, label %.lr.ph32.preheader, label %._crit_edge

.lr.ph32.preheader:                               ; preds = %.preheader
  %wide.trip.count37 = zext nneg i32 %1 to i64
  br label %.lr.ph32

.lr.ph:                                           ; preds = %.lr.ph.preheader41, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader41 ]
  %17 = getelementptr inbounds %class.Set, ptr %10, i64 %indvars.iv
  %18 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %18, ptr %17, align 4, !tbaa !13
  %19 = getelementptr inbounds i8, ptr %17, i64 4
  store i32 -1, ptr %19, align 4, !tbaa !15
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %6
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %25, %.preheader
  %.025.lcssa = phi i32 [ 0, %.preheader ], [ %.1, %25 ]
  ret i32 %.025.lcssa

.lr.ph32:                                         ; preds = %.lr.ph32.preheader, %25
  %indvars.iv34 = phi i64 [ 0, %.lr.ph32.preheader ], [ %indvars.iv.next35, %25 ]
  %.02531 = phi i32 [ 0, %.lr.ph32.preheader ], [ %.1, %25 ]
  %20 = getelementptr inbounds %class.Edge, ptr %0, i64 %indvars.iv34
  %.sroa.0.0.copyload = load i32, ptr %20, align 4, !tbaa !5
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %20, i64 4
  %.sroa.4.0.copyload = load i32, ptr %.sroa.4.0..sroa_idx, align 4, !tbaa !5
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %20, i64 8
  %.sroa.5.0.copyload = load i32, ptr %.sroa.5.0..sroa_idx, align 4, !tbaa !5
  %21 = tail call noundef i32 @_Z4findiP3Set(i32 noundef %.sroa.0.0.copyload, ptr noundef nonnull %10)
  %22 = tail call noundef i32 @_Z4findiP3Set(i32 noundef %.sroa.4.0.copyload, ptr noundef nonnull %10)
  %.not = icmp eq i32 %21, %22
  br i1 %.not, label %25, label %23

23:                                               ; preds = %.lr.ph32
  tail call void @_Z5UnionP3Setii(ptr noundef nonnull %10, i32 noundef %21, i32 noundef %22)
  %24 = add nsw i32 %.sroa.5.0.copyload, %.02531
  br label %25

25:                                               ; preds = %23, %.lr.ph32
  %.1 = phi i32 [ %24, %23 ], [ %.02531, %.lr.ph32 ]
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
  %exitcond38.not = icmp eq i64 %indvars.iv.next35, %wide.trip.count37
  br i1 %exitcond38.not, label %._crit_edge, label %.lr.ph32, !llvm.loop !17
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIP4EdgePFbS0_S0_EEvT_S4_T0_(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #3

attributes #0 = { mustprogress 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 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 = { inlinehint mustprogress 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 #3 = { nobuiltin allocsize(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 #4 = { builtin allocsize(0) }

!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++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = !{!14, !6, i64 0}
!14 = !{!"_ZTS3Set", !6, i64 0, !6, i64 4}
!15 = !{!14, !6, i64 4}
!16 = distinct !{!16, !10, !12, !11}
!17 = distinct !{!17, !10}


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