; LLVM IR from sbshah97/DS-Algo-Handbook :: Data-Structures/Disjoint_Sets (DisjointSet)

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

@.str = external hidden unnamed_addr constant [54 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [6 x i8], align 1
@edg = external dso_local global [500001 x [2 x i64]], align 16
@.str.4 = external hidden unnamed_addr constant [39 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [42 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i64 @findroot(ptr noundef, i64 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: nounwind uwtable
declare dso_local void @disunion(ptr noundef, i64 noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca [100001 x i64], align 16
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store i64 0, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 800008, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  store i64 0, ptr %12, align 8, !tbaa !5
  %13 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %2, ptr noundef %3)
  %15 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  store i64 1, ptr %6, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %23, %0
  %17 = load i64, ptr %6, align 8, !tbaa !5
  %18 = load i64, ptr %2, align 8, !tbaa !5
  %19 = icmp sle i64 %17, %18
  br i1 %19, label %20, label %26

20:                                               ; preds = %16
  %21 = load i64, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds [100001 x i64], ptr %9, i64 0, i64 %21
  store i64 -1, ptr %22, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %20
  %24 = load i64, ptr %6, align 8, !tbaa !5
  %25 = add nsw i64 %24, 1
  store i64 %25, ptr %6, align 8, !tbaa !5
  br label %16, !llvm.loop !9

26:                                               ; preds = %16
  store i64 0, ptr %6, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %75, %26
  %28 = load i64, ptr %6, align 8, !tbaa !5
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = icmp slt i64 %28, %29
  br i1 %30, label %31, label %78

31:                                               ; preds = %27
  %32 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %33 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %4, ptr noundef %5)
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = load i64, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds [500001 x [2 x i64]], ptr @edg, i64 0, i64 %35
  %37 = getelementptr inbounds [2 x i64], ptr %36, i64 0, i64 0
  store i64 %34, ptr %37, align 16, !tbaa !5
  %38 = load i64, ptr %5, align 8, !tbaa !5
  %39 = load i64, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds [500001 x [2 x i64]], ptr @edg, i64 0, i64 %39
  %41 = getelementptr inbounds [2 x i64], ptr %40, i64 0, i64 1
  store i64 %38, ptr %41, align 8, !tbaa !5
  %42 = getelementptr inbounds [100001 x i64], ptr %9, i64 0, i64 0
  %43 = load i64, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds [500001 x [2 x i64]], ptr @edg, i64 0, i64 %43
  %45 = getelementptr inbounds [2 x i64], ptr %44, i64 0, i64 0
  %46 = load i64, ptr %45, align 16, !tbaa !5
  %47 = call i64 @findroot(ptr noundef %42, i64 noundef %46)
  store i64 %47, ptr %10, align 8, !tbaa !5
  %48 = getelementptr inbounds [100001 x i64], ptr %9, i64 0, i64 0
  %49 = load i64, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds [500001 x [2 x i64]], ptr @edg, i64 0, i64 %49
  %51 = getelementptr inbounds [2 x i64], ptr %50, i64 0, i64 1
  %52 = load i64, ptr %51, align 8, !tbaa !5
  %53 = call i64 @findroot(ptr noundef %48, i64 noundef %52)
  store i64 %53, ptr %11, align 8, !tbaa !5
  %54 = load i64, ptr %10, align 8, !tbaa !5
  %55 = load i64, ptr %11, align 8, !tbaa !5
  %56 = icmp eq i64 %54, %55
  br i1 %56, label %57, label %67

57:                                               ; preds = %31
  %58 = load i64, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds [500001 x [2 x i64]], ptr @edg, i64 0, i64 %58
  %60 = getelementptr inbounds [2 x i64], ptr %59, i64 0, i64 0
  %61 = load i64, ptr %60, align 16, !tbaa !5
  %62 = load i64, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds [500001 x [2 x i64]], ptr @edg, i64 0, i64 %62
  %64 = getelementptr inbounds [2 x i64], ptr %63, i64 0, i64 1
  %65 = load i64, ptr %64, align 8, !tbaa !5
  %66 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i64 noundef %61, i64 noundef %65)
  br label %75

67:                                               ; preds = %31
  %68 = load i64, ptr %10, align 8, !tbaa !5
  %69 = load i64, ptr %11, align 8, !tbaa !5
  %70 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, i64 noundef %68, i64 noundef %69)
  %71 = getelementptr inbounds [100001 x i64], ptr %9, i64 0, i64 0
  %72 = load i64, ptr %10, align 8, !tbaa !5
  %73 = load i64, ptr %11, align 8, !tbaa !5
  call void @disunion(ptr noundef %71, i64 noundef %72, i64 noundef %73)
  br label %74

74:                                               ; preds = %67
  br label %75

75:                                               ; preds = %74, %57
  %76 = load i64, ptr %6, align 8, !tbaa !5
  %77 = add nsw i64 %76, 1
  store i64 %77, ptr %6, align 8, !tbaa !5
  br label %27, !llvm.loop !11

78:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 800008, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  ret i32 0
}

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

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

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 willreturn memory(argmem: readwrite) }
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 = !{!"long long", !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"

@.str = external hidden unnamed_addr constant [54 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [6 x i8], align 1
@edg = external dso_local local_unnamed_addr global [500001 x [2 x i64]], align 16
@.str.4 = external hidden unnamed_addr constant [39 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [42 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i64 @findroot(ptr noundef, i64 noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @disunion(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca [100001 x i64], align 16
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #4
  call void @llvm.lifetime.start.p0(i64 800008, ptr nonnull %5) #4
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %1, ptr noundef nonnull %2)
  %8 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %.not19 = icmp slt i64 %9, 1
  br i1 %.not19, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %scevgep = getelementptr inbounds i8, ptr %5, i64 8
  %10 = shl nuw i64 %9, 3
  call void @llvm.memset.p0.i64(ptr nonnull align 8 %scevgep, i8 -1, i64 %10, i1 false), !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %.lr.ph.preheader, %0
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = icmp sgt i64 %11, 0
  br i1 %12, label %.lr.ph22, label %._crit_edge

.lr.ph22:                                         ; preds = %.preheader, %29
  %.121 = phi i64 [ %30, %29 ], [ 0, %.preheader ]
  %13 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %3, ptr noundef nonnull %4)
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds [500001 x [2 x i64]], ptr @edg, i64 0, i64 %.121
  store i64 %15, ptr %16, align 16, !tbaa !5
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %16, i64 8
  store i64 %17, ptr %18, align 8, !tbaa !5
  %19 = call i64 @findroot(ptr noundef nonnull %5, i64 noundef %15)
  %20 = load i64, ptr %18, align 8, !tbaa !5
  %21 = call i64 @findroot(ptr noundef nonnull %5, i64 noundef %20)
  %22 = icmp eq i64 %19, %21
  br i1 %22, label %23, label %27

23:                                               ; preds = %.lr.ph22
  %24 = load i64, ptr %16, align 16, !tbaa !5
  %25 = load i64, ptr %18, align 8, !tbaa !5
  %26 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i64 noundef %24, i64 noundef %25)
  br label %29

27:                                               ; preds = %.lr.ph22
  %28 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, i64 noundef %19, i64 noundef %21)
  call void @disunion(ptr noundef nonnull %5, i64 noundef %19, i64 noundef %21)
  br label %29

29:                                               ; preds = %27, %23
  %30 = add nuw nsw i64 %.121, 1
  %31 = load i64, ptr %2, align 8, !tbaa !5
  %32 = icmp slt i64 %30, %31
  br i1 %32, label %.lr.ph22, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %29, %.preheader
  call void @llvm.lifetime.end.p0(i64 800008, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #4
  ret i32 0
}

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass