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

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

@heapsize = external dso_local global i64, align 8
@spanedge = external dso_local global i64, align 8
@counter = external dso_local global i64, align 8
@res = external dso_local global i64, align 8
@ptrarr = external dso_local global [1000005 x ptr], align 16
@vis = external dso_local global [1000005 x i64], align 16
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @adjinsert(ptr noundef, i64 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

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @prims(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 i64, align 8
  %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
  call void @llvm.lifetime.start.p0(i64 8, 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
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef %3)
  store i64 0, ptr %4, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %64, %0
  %15 = load i64, ptr %4, align 8, !tbaa !5
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %18, label %67

18:                                               ; preds = %14
  store i64 0, ptr %5, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %27, %18
  %20 = load i64, ptr %5, align 8, !tbaa !5
  %21 = icmp slt i64 %20, 1000005
  br i1 %21, label %22, label %30

22:                                               ; preds = %19
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds [1000005 x ptr], ptr @ptrarr, i64 0, i64 %23
  store ptr null, ptr %24, align 8, !tbaa !9
  %25 = load i64, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds [1000005 x i64], ptr @vis, i64 0, i64 %25
  store i64 0, ptr %26, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %22
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = add nsw i64 %28, 1
  store i64 %29, ptr %5, align 8, !tbaa !5
  br label %19, !llvm.loop !11

30:                                               ; preds = %19
  %31 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.5, ptr noundef %8, ptr noundef %9)
  store i64 0, ptr @heapsize, align 8, !tbaa !5
  store i64 0, ptr %2, align 8, !tbaa !5
  %32 = load i64, ptr %8, align 8, !tbaa !5
  %33 = sub nsw i64 %32, 1
  store i64 %33, ptr @spanedge, align 8, !tbaa !5
  store i64 0, ptr @res, align 8, !tbaa !5
  store i64 0, ptr @counter, align 8, !tbaa !5
  store i64 0, ptr %5, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %56, %30
  %35 = load i64, ptr %5, align 8, !tbaa !5
  %36 = load i64, ptr %9, align 8, !tbaa !5
  %37 = icmp slt i64 %35, %36
  br i1 %37, label %38, label %59

38:                                               ; preds = %34
  %39 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.6, ptr noundef %10, ptr noundef %11, ptr noundef %12)
  %40 = load i64, ptr %10, align 8, !tbaa !5
  %41 = load i64, ptr %11, align 8, !tbaa !5
  %42 = icmp ne i64 %40, %41
  br i1 %42, label %43, label %52

43:                                               ; preds = %38
  %44 = load i64, ptr %10, align 8, !tbaa !5
  %45 = getelementptr inbounds [1000005 x ptr], ptr @ptrarr, i64 0, i64 %44
  %46 = load i64, ptr %11, align 8, !tbaa !5
  %47 = load i64, ptr %12, align 8, !tbaa !5
  call void @adjinsert(ptr noundef %45, i64 noundef %46, i64 noundef %47)
  %48 = load i64, ptr %11, align 8, !tbaa !5
  %49 = getelementptr inbounds [1000005 x ptr], ptr @ptrarr, i64 0, i64 %48
  %50 = load i64, ptr %10, align 8, !tbaa !5
  %51 = load i64, ptr %12, align 8, !tbaa !5
  call void @adjinsert(ptr noundef %49, i64 noundef %50, i64 noundef %51)
  br label %52

52:                                               ; preds = %43, %38
  %53 = load i64, ptr %12, align 8, !tbaa !5
  %54 = load i64, ptr %2, align 8, !tbaa !5
  %55 = add nsw i64 %54, %53
  store i64 %55, ptr %2, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %52
  %57 = load i64, ptr %5, align 8, !tbaa !5
  %58 = add nsw i64 %57, 1
  store i64 %58, ptr %5, align 8, !tbaa !5
  br label %34, !llvm.loop !13

59:                                               ; preds = %34
  store i64 1, ptr getelementptr inbounds ([1000005 x i64], ptr @vis, i64 0, i64 1), align 8, !tbaa !5
  %60 = load i64, ptr %2, align 8, !tbaa !5
  %61 = call i64 @prims(i64 noundef 1)
  %62 = sub nsw i64 %60, %61
  %63 = call i32 (ptr, ...) @printf(ptr noundef @.str.7, i64 noundef %62)
  br label %64

64:                                               ; preds = %59
  %65 = load i64, ptr %4, align 8, !tbaa !5
  %66 = add nsw i64 %65, 1
  store i64 %66, ptr %4, align 8, !tbaa !5
  br label %14, !llvm.loop !14

67:                                               ; preds = %14
  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 8, 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 @__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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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

@heapsize = external dso_local local_unnamed_addr global i64, align 8
@spanedge = external dso_local local_unnamed_addr global i64, align 8
@counter = external dso_local local_unnamed_addr global i64, align 8
@res = external dso_local local_unnamed_addr global i64, align 8
@ptrarr = external dso_local global [1000005 x ptr], align 16
@vis = external dso_local local_unnamed_addr global [1000005 x i64], align 16
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @adjinsert(ptr noundef, i64 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: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local i64 @prims(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 i64, align 8
  %6 = alloca i64, align 8
  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 8, ptr nonnull %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #4
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull %1)
  %8 = load i64, ptr %1, align 8, !tbaa !5
  %9 = icmp sgt i64 %8, 0
  br i1 %9, label %.preheader, label %._crit_edge18

.preheader:                                       ; preds = %0, %._crit_edge
  %.01017 = phi i64 [ %34, %._crit_edge ], [ 0, %0 ]
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8000040) @ptrarr, i8 0, i64 8000040, i1 false), !tbaa !9
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8000040) @vis, i8 0, i64 8000040, i1 false), !tbaa !5
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.5, ptr noundef nonnull %2, ptr noundef nonnull %3)
  store i64 0, ptr @heapsize, align 8, !tbaa !5
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = add nsw i64 %11, -1
  store i64 %12, ptr @spanedge, align 8, !tbaa !5
  store i64 0, ptr @res, align 8, !tbaa !5
  store i64 0, ptr @counter, align 8, !tbaa !5
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = icmp sgt i64 %13, 0
  br i1 %14, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %25
  %.016 = phi i64 [ %27, %25 ], [ 0, %.preheader ]
  %.115 = phi i64 [ %28, %25 ], [ 0, %.preheader ]
  %15 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.6, ptr noundef nonnull %4, ptr noundef nonnull %5, ptr noundef nonnull %6)
  %16 = load i64, ptr %4, align 8, !tbaa !5
  %17 = load i64, ptr %5, align 8, !tbaa !5
  %.not = icmp eq i64 %16, %17
  br i1 %.not, label %25, label %18

18:                                               ; preds = %.lr.ph
  %19 = getelementptr inbounds [1000005 x ptr], ptr @ptrarr, i64 0, i64 %16
  %20 = load i64, ptr %6, align 8, !tbaa !5
  call void @adjinsert(ptr noundef nonnull %19, i64 noundef %17, i64 noundef %20)
  %21 = load i64, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds [1000005 x ptr], ptr @ptrarr, i64 0, i64 %21
  %23 = load i64, ptr %4, align 8, !tbaa !5
  %24 = load i64, ptr %6, align 8, !tbaa !5
  call void @adjinsert(ptr noundef nonnull %22, i64 noundef %23, i64 noundef %24)
  br label %25

25:                                               ; preds = %18, %.lr.ph
  %26 = load i64, ptr %6, align 8, !tbaa !5
  %27 = add nsw i64 %26, %.016
  %28 = add nuw nsw i64 %.115, 1
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = icmp slt i64 %28, %29
  br i1 %30, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %25, %.preheader
  %.0.lcssa = phi i64 [ 0, %.preheader ], [ %27, %25 ]
  store i64 1, ptr getelementptr inbounds (i8, ptr @vis, i64 8), align 8, !tbaa !5
  %31 = call i64 @prims(i64 noundef 1)
  %32 = sub nsw i64 %.0.lcssa, %31
  %33 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7, i64 noundef %32)
  %34 = add nuw nsw i64 %.01017, 1
  %35 = load i64, ptr %1, align 8, !tbaa !5
  %36 = icmp slt i64 %34, %35
  br i1 %36, label %.preheader, label %._crit_edge18, !llvm.loop !13

._crit_edge18:                                    ; preds = %._crit_edge, %0
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 8, 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 @__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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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