; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2018-19/Open 2019/Gold/walk (12:15).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"

%struct.DSU = type { [7501 x i32], [7501 x i32] }

@N = external dso_local global i32, align 4
@K = external dso_local global i32, align 4
@ed = external dso_local global [28128750 x i32], align 16
@dist = external dso_local global [60000000 x i32], align 16

; 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 uwtable
define dso_local noundef i32 @_Z7kruskalv() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca %struct.DSU, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 60008, ptr %3) #4
  call void @_ZN3DSUILi7501EEC2Ev(ptr noundef nonnull align 4 dereferenceable(60008) %3)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %53, %0
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = load i32, ptr @N, align 4, !tbaa !5
  %12 = load i32, ptr @N, align 4, !tbaa !5
  %13 = add nsw i32 %12, 1
  %14 = mul nsw i32 %11, %13
  %15 = sdiv i32 %14, 2
  %16 = icmp slt i32 %10, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %9
  store i32 2, ptr %5, align 4
  br label %56

18:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [28128750 x i32], ptr @ed, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  store i32 %22, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr @N, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  %26 = sdiv i32 %23, %25
  store i32 %26, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = load i32, ptr @N, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  %30 = srem i32 %27, %29
  store i32 %30, ptr %8, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = call noundef zeroext i1 @_ZN3DSUILi7501EE5uniteEii(ptr noundef nonnull align 4 dereferenceable(60008) %3, i32 noundef %31, i32 noundef %32)
  br i1 %33, label %34, label %49

34:                                               ; preds = %18
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %2, align 4, !tbaa !5
  %37 = load i32, ptr %2, align 4, !tbaa !5
  %38 = load i32, ptr @N, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  %40 = load i32, ptr @K, align 4, !tbaa !5
  %41 = sub nsw i32 %39, %40
  %42 = icmp eq i32 %37, %41
  br i1 %42, label %43, label %48

43:                                               ; preds = %34
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [60000000 x i32], ptr @dist, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  store i32 %47, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %50

48:                                               ; preds = %34
  br label %49

49:                                               ; preds = %48, %18
  store i32 0, ptr %5, align 4
  br label %50

50:                                               ; preds = %49, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %51 = load i32, ptr %5, align 4
  switch i32 %51, label %56 [
    i32 0, label %52
  ]

52:                                               ; preds = %50
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %4, align 4, !tbaa !5
  br label %9, !llvm.loop !9

56:                                               ; preds = %50, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %57 = load i32, ptr %5, align 4
  switch i32 %57, label %59 [
    i32 2, label %58
  ]

58:                                               ; preds = %56
  call void @exit(i32 noundef 5) #5
  unreachable

59:                                               ; preds = %56
  call void @llvm.lifetime.end.p0(i64 60008, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %60 = load i32, ptr %1, align 4
  ret i32 %60
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3DSUILi7501EEC2Ev(ptr noundef nonnull align 4 dereferenceable(60008)) unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN3DSUILi7501EE5uniteEii(ptr noundef nonnull align 4 dereferenceable(60008), i32 noundef, i32 noundef) #1 align 2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3

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

%struct.DSU = type { [7501 x i32], [7501 x i32] }

@N = external dso_local local_unnamed_addr global i32, align 4
@K = external dso_local local_unnamed_addr global i32, align 4
@ed = external dso_local local_unnamed_addr global [28128750 x i32], align 16
@dist = external dso_local local_unnamed_addr global [60000000 x i32], align 16

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7kruskalv() local_unnamed_addr #1 {
  %1 = alloca %struct.DSU, align 4
  call void @llvm.lifetime.start.p0(i64 60008, ptr nonnull %1) #4
  call void @_ZN3DSUILi7501EEC2Ev(ptr noundef nonnull align 4 dereferenceable(60008) %1)
  %2 = load i32, ptr @N, align 4, !tbaa !5
  %3 = add nsw i32 %2, 1
  %4 = mul nsw i32 %3, %2
  %.not30 = icmp sgt i32 %4, 1
  br i1 %.not30, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %17
  %indvars.iv = phi i64 [ %indvars.iv.next, %17 ], [ 0, %0 ]
  %5 = phi i32 [ %.pre-phi, %17 ], [ %3, %0 ]
  %.01432 = phi i32 [ %.216.ph, %17 ], [ 0, %0 ]
  %6 = getelementptr inbounds [28128750 x i32], ptr @ed, i64 0, i64 %indvars.iv
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = sdiv i32 %7, %5
  %9 = srem i32 %7, %5
  %10 = call noundef zeroext i1 @_ZN3DSUILi7501EE5uniteEii(ptr noundef nonnull align 4 dereferenceable(60008) %1, i32 noundef %8, i32 noundef %9)
  %.pre = load i32, ptr @N, align 4, !tbaa !5
  br i1 %10, label %11, label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %.pre35 = add nsw i32 %.pre, 1
  br label %17

11:                                               ; preds = %.lr.ph
  %12 = add nsw i32 %.01432, 1
  %13 = add nsw i32 %.pre, 1
  %14 = load i32, ptr @K, align 4, !tbaa !5
  %15 = sub i32 %13, %14
  %16 = icmp eq i32 %12, %15
  br i1 %16, label %21, label %17

17:                                               ; preds = %.lr.ph._crit_edge, %11
  %.pre-phi = phi i32 [ %.pre35, %.lr.ph._crit_edge ], [ %13, %11 ]
  %.216.ph = phi i32 [ %.01432, %.lr.ph._crit_edge ], [ %12, %11 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %18 = mul nsw i32 %.pre-phi, %.pre
  %19 = sdiv i32 %18, 2
  %20 = sext i32 %19 to i64
  %.not = icmp slt i64 %indvars.iv.next, %20
  br i1 %.not, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %17, %0
  call void @exit(i32 noundef 5) #5
  unreachable

21:                                               ; preds = %11
  %22 = sext i32 %7 to i64
  %23 = getelementptr inbounds [60000000 x i32], ptr @dist, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 60008, ptr nonnull %1) #4
  ret i32 %24
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3DSUILi7501EEC2Ev(ptr noundef nonnull align 4 dereferenceable(60008)) unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN3DSUILi7501EE5uniteEii(ptr noundef nonnull align 4 dereferenceable(60008), i32 noundef, i32 noundef) local_unnamed_addr #1 align 2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #3

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


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