; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Model Algorithms/Graphs (Detect Cycle in an Undirected Graph using Union Find)

; 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.Graph = type { i32, i32, ptr }
%struct.subset = type { i32, i32 }
%struct.Edge = type { i32, i32 }

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z8isCyclicP5Graph(ptr noundef %0) #2 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Graph, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = mul i64 %13, 8
  %15 = call noalias ptr @malloc(i64 noundef %14) #4
  store ptr %15, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !12
  br label %16

16:                                               ; preds = %35, %1
  %17 = load i32, ptr %5, align 4, !tbaa !12
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.Graph, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !9
  %21 = icmp slt i32 %17, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %38

23:                                               ; preds = %16
  %24 = load i32, ptr %5, align 4, !tbaa !12
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !12
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.subset, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.subset, ptr %28, i32 0, i32 0
  store i32 %24, ptr %29, align 4, !tbaa !13
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !12
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds %struct.subset, ptr %30, i64 %32
  %34 = getelementptr inbounds %struct.subset, ptr %33, i32 0, i32 1
  store i32 0, ptr %34, align 4, !tbaa !15
  br label %35

35:                                               ; preds = %23
  %36 = load i32, ptr %5, align 4, !tbaa !12
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %5, align 4, !tbaa !12
  br label %16, !llvm.loop !16

38:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !12
  br label %39

39:                                               ; preds = %78, %38
  %40 = load i32, ptr %6, align 4, !tbaa !12
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.Graph, ptr %41, i32 0, i32 1
  %43 = load i32, ptr %42, align 4, !tbaa !18
  %44 = icmp slt i32 %40, %43
  br i1 %44, label %46, label %45

45:                                               ; preds = %39
  store i32 5, ptr %7, align 4
  br label %81

46:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Graph, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !19
  %51 = load i32, ptr %6, align 4, !tbaa !12
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds %struct.Edge, ptr %50, i64 %52
  %54 = getelementptr inbounds %struct.Edge, ptr %53, i32 0, i32 0
  %55 = load i32, ptr %54, align 4, !tbaa !20
  %56 = call noundef i32 @_Z4FindP6subseti(ptr noundef %47, i32 noundef %55)
  store i32 %56, ptr %8, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Graph, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %59, align 8, !tbaa !19
  %61 = load i32, ptr %6, align 4, !tbaa !12
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds %struct.Edge, ptr %60, i64 %62
  %64 = getelementptr inbounds %struct.Edge, ptr %63, i32 0, i32 1
  %65 = load i32, ptr %64, align 4, !tbaa !22
  %66 = call noundef i32 @_Z4FindP6subseti(ptr noundef %57, i32 noundef %65)
  store i32 %66, ptr %9, align 4, !tbaa !12
  %67 = load i32, ptr %8, align 4, !tbaa !12
  %68 = load i32, ptr %9, align 4, !tbaa !12
  %69 = icmp eq i32 %67, %68
  br i1 %69, label %70, label %71

70:                                               ; preds = %46
  store i1 true, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %75

71:                                               ; preds = %46
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = load i32, ptr %8, align 4, !tbaa !12
  %74 = load i32, ptr %9, align 4, !tbaa !12
  call void @_Z5UnionP6subsetii(ptr noundef %72, i32 noundef %73, i32 noundef %74)
  store i32 0, ptr %7, align 4
  br label %75

75:                                               ; preds = %71, %70
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %76 = load i32, ptr %7, align 4
  switch i32 %76, label %81 [
    i32 0, label %77
  ]

77:                                               ; preds = %75
  br label %78

78:                                               ; preds = %77
  %79 = load i32, ptr %6, align 4, !tbaa !12
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %6, align 4, !tbaa !12
  br label %39, !llvm.loop !23

81:                                               ; preds = %75, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %82 = load i32, ptr %7, align 4
  switch i32 %82, label %84 [
    i32 5, label %83
  ]

83:                                               ; preds = %81
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %84

84:                                               ; preds = %83, %81
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %85 = load i1, ptr %2, align 1
  ret i1 %85
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind 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 #2 = { 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 = { nounwind }
attributes #4 = { nounwind 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, !11, i64 0}
!10 = !{!"_ZTS5Graph", !11, i64 0, !11, i64 4, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !11, i64 0}
!14 = !{!"_ZTS6subset", !11, i64 0, !11, i64 4}
!15 = !{!14, !11, i64 4}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!10, !11, i64 4}
!19 = !{!10, !6, i64 8}
!20 = !{!21, !11, i64 0}
!21 = !{!"_ZTS4Edge", !11, i64 0, !11, i64 4}
!22 = !{!21, !11, i64 4}
!23 = distinct !{!23, !17}


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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z8isCyclicP5Graph(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = load i32, ptr %0, align 8, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = shl nsw i64 %3, 3
  %5 = tail call noalias ptr @malloc(i64 noundef %4) #2
  %6 = icmp sgt i32 %2, 0
  br i1 %6, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %1
  %wide.trip.count = zext nneg i32 %2 to i64
  %min.iters.check = icmp ult i32 %2, 4
  br i1 %min.iters.check, label %.lr.ph.preheader46, label %vector.ph

.lr.ph.preheader46:                               ; 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 %wide.trip.count, 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>
  %7 = or disjoint i64 %index, 2
  %8 = getelementptr inbounds %struct.subset, ptr %5, i64 %index
  %9 = getelementptr inbounds %struct.subset, ptr %5, i64 %7
  %interleaved.vec = shufflevector <2 x i32> %vec.ind, <2 x i32> zeroinitializer, <4 x i32> <i32 0, i32 2, i32 1, i32 3>
  store <4 x i32> %interleaved.vec, ptr %8, align 4, !tbaa !11
  %interleaved.vec45 = shufflevector <2 x i32> %step.add, <2 x i32> zeroinitializer, <4 x i32> <i32 0, i32 2, i32 1, i32 3>
  store <4 x i32> %interleaved.vec45, ptr %9, align 4, !tbaa !11
  %index.next = add nuw i64 %index, 4
  %vec.ind.next = add <2 x i32> %vec.ind, <i32 4, i32 4>
  %10 = icmp eq i64 %index.next, %n.vec
  br i1 %10, label %middle.block, label %vector.body, !llvm.loop !12

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader46

.preheader:                                       ; preds = %.lr.ph, %middle.block, %1
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = getelementptr inbounds i8, ptr %0, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !16
  %.not3235 = icmp sgt i32 %13, 0
  br i1 %.not3235, label %.lr.ph38, label %.critedge

.lr.ph:                                           ; preds = %.lr.ph.preheader46, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader46 ]
  %14 = getelementptr inbounds %struct.subset, ptr %5, i64 %indvars.iv
  %15 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %15, ptr %14, align 4, !tbaa !17
  %16 = getelementptr inbounds i8, ptr %14, i64 4
  store i32 0, ptr %16, align 4, !tbaa !19
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !20

.lr.ph38:                                         ; preds = %.preheader, %25
  %indvars.iv42 = phi i64 [ %indvars.iv.next43, %25 ], [ 0, %.preheader ]
  %17 = load ptr, ptr %11, align 8, !tbaa !21
  %18 = getelementptr inbounds %struct.Edge, ptr %17, i64 %indvars.iv42
  %19 = load i32, ptr %18, align 4, !tbaa !22
  %20 = tail call noundef i32 @_Z4FindP6subseti(ptr noundef %5, i32 noundef %19)
  %21 = load ptr, ptr %11, align 8, !tbaa !21
  %22 = getelementptr inbounds %struct.Edge, ptr %21, i64 %indvars.iv42, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !24
  %24 = tail call noundef i32 @_Z4FindP6subseti(ptr noundef %5, i32 noundef %23)
  %.not = icmp eq i32 %20, %24
  br i1 %.not, label %.critedge, label %25

25:                                               ; preds = %.lr.ph38
  tail call void @_Z5UnionP6subsetii(ptr noundef %5, i32 noundef %20, i32 noundef %24)
  %indvars.iv.next43 = add nuw nsw i64 %indvars.iv42, 1
  %26 = load i32, ptr %12, align 4, !tbaa !16
  %27 = sext i32 %26 to i64
  %.not32 = icmp slt i64 %indvars.iv.next43, %27
  br i1 %.not32, label %.lr.ph38, label %.critedge, !llvm.loop !25

.critedge:                                        ; preds = %.lr.ph38, %25, %.preheader
  %.not32.lcssa = phi i1 [ false, %.preheader ], [ %.not, %25 ], [ %.not, %.lr.ph38 ]
  ret i1 %.not32.lcssa
}

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 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 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, !7, i64 0}
!6 = !{!"_ZTS5Graph", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13, !14, !15}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!"llvm.loop.isvectorized", i32 1}
!15 = !{!"llvm.loop.unroll.runtime.disable"}
!16 = !{!6, !7, i64 4}
!17 = !{!18, !7, i64 0}
!18 = !{!"_ZTS6subset", !7, i64 0, !7, i64 4}
!19 = !{!18, !7, i64 4}
!20 = distinct !{!20, !13, !15, !14}
!21 = !{!6, !10, i64 8}
!22 = !{!23, !7, i64 0}
!23 = !{!"_ZTS4Edge", !7, i64 0, !7, i64 4}
!24 = !{!23, !7, i64 4}
!25 = distinct !{!25, !13}


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