; LLVM IR from lzyrapx/Competitive-Programming :: Xiangtan Invitation Contest 2017/1267(H)（树上dfs）（树的直径）.ll (Z5solvev)

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

@vis = external dso_local global [100010 x i8], align 16
@dist = external dso_local global i64, align 8
@point = external dso_local global i32, align 4
@res = external dso_local global [3 x [100010 x i64]], align 16
@n = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3dfsixi(i32 noundef, i64 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 uwtable
define dso_local void @_Z5solvev() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  call void @llvm.memset.p0.i64(ptr align 16 @vis, i8 0, i64 100010, i1 false)
  store i64 -4557430888798830399, ptr @dist, align 8, !tbaa !5
  call void @_Z3dfsixi(i32 noundef 1, i64 noundef 0, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #5
  %5 = load i32, ptr @point, align 4, !tbaa !9
  store i32 %5, ptr %1, align 4, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 16 @vis, i8 0, i64 100010, i1 false)
  store i64 -4557430888798830399, ptr @dist, align 8, !tbaa !5
  %6 = load i32, ptr %1, align 4, !tbaa !9
  call void @_Z3dfsixi(i32 noundef %6, i64 noundef 0, i32 noundef 1)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  %7 = load i32, ptr @point, align 4, !tbaa !9
  store i32 %7, ptr %2, align 4, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 16 @vis, i8 0, i64 100010, i1 false)
  %8 = load i32, ptr %2, align 4, !tbaa !9
  call void @_Z3dfsixi(i32 noundef %8, i64 noundef 0, i32 noundef 2)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %9 = load i32, ptr %2, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds ([3 x [100010 x i64]], ptr @res, i64 0, i64 1), i64 0, i64 %10
  %12 = load i64, ptr %11, align 8, !tbaa !5
  store i64 %12, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %38, %0
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = load i32, ptr @n, align 4, !tbaa !9
  %16 = icmp sle i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %41

18:                                               ; preds = %13
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = load i32, ptr %1, align 4, !tbaa !9
  %21 = icmp eq i32 %19, %20
  br i1 %21, label %26, label %22

22:                                               ; preds = %18
  %23 = load i32, ptr %4, align 4, !tbaa !9
  %24 = load i32, ptr %2, align 4, !tbaa !9
  %25 = icmp eq i32 %23, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %22, %18
  br label %38

27:                                               ; preds = %22
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds ([3 x [100010 x i64]], ptr @res, i64 0, i64 1), i64 0, i64 %29
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds ([3 x [100010 x i64]], ptr @res, i64 0, i64 2), i64 0, i64 %32
  %34 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %30, ptr noundef nonnull align 8 dereferenceable(8) %33)
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = load i64, ptr %3, align 8, !tbaa !5
  %37 = add nsw i64 %36, %35
  store i64 %37, ptr %3, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %27, %26
  %39 = load i32, ptr %4, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %4, align 4, !tbaa !9
  br label %13, !llvm.loop !11

41:                                               ; preds = %17
  %42 = load i64, ptr %3, align 8, !tbaa !5
  %43 = call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %42)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #5
  ret void
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #3

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { inlinehint 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 #4 = { "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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@vis = external dso_local local_unnamed_addr global [100010 x i8], align 16
@dist = external dso_local local_unnamed_addr global i64, align 8
@point = external dso_local local_unnamed_addr global i32, align 4
@res = external dso_local global [3 x [100010 x i64]], align 16
@n = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [7 x i8], align 1

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solvev() local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(100010) @vis, i8 0, i64 100010, i1 false)
  store i64 -4557430888798830399, ptr @dist, align 8, !tbaa !5
  tail call void @_Z3dfsixi(i32 noundef 1, i64 noundef 0, i32 noundef 0)
  %1 = load i32, ptr @point, align 4, !tbaa !9
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(100010) @vis, i8 0, i64 100010, i1 false)
  store i64 -4557430888798830399, ptr @dist, align 8, !tbaa !5
  tail call void @_Z3dfsixi(i32 noundef %1, i64 noundef 0, i32 noundef 1)
  %2 = load i32, ptr @point, align 4, !tbaa !9
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(100010) @vis, i8 0, i64 100010, i1 false)
  tail call void @_Z3dfsixi(i32 noundef %2, i64 noundef 0, i32 noundef 2)
  %3 = sext i32 %2 to i64
  %4 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds (i8, ptr @res, i64 800080), i64 0, i64 %3
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = load i32, ptr @n, align 4, !tbaa !9
  %.not14 = icmp slt i32 %6, 0
  br i1 %.not14, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %7 = zext i32 %2 to i64
  %8 = zext i32 %1 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %19, %0
  %.013.lcssa = phi i64 [ %5, %0 ], [ %.1, %19 ]
  %9 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %.013.lcssa)
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %19
  %10 = phi i32 [ %6, %.lr.ph.preheader ], [ %20, %19 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %19 ]
  %.01315 = phi i64 [ %5, %.lr.ph.preheader ], [ %.1, %19 ]
  %11 = icmp eq i64 %indvars.iv, %8
  %12 = icmp eq i64 %indvars.iv, %7
  %or.cond = or i1 %11, %12
  br i1 %or.cond, label %19, label %13

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds (i8, ptr @res, i64 800080), i64 0, i64 %indvars.iv
  %15 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds (i8, ptr @res, i64 1600160), i64 0, i64 %indvars.iv
  %16 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %14, ptr noundef nonnull align 8 dereferenceable(8) %15)
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = add nsw i64 %17, %.01315
  %.pre = load i32, ptr @n, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %.lr.ph, %13
  %20 = phi i32 [ %10, %.lr.ph ], [ %.pre, %13 ]
  %.1 = phi i64 [ %.01315, %.lr.ph ], [ %18, %13 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %21 = sext i32 %20 to i64
  %.not.not = icmp slt i64 %indvars.iv, %21
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !11
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) 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 nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { inlinehint 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 = { 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" }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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