; LLVM IR from tangjz/acm-icpc :: codeforces/gym/100540H.ll (Z6tarjanii)

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

@tot = external dso_local global i32, align 4
@lnk = external dso_local global [10001 x i32], align 16
@pre = external dso_local global [10001 x i32], align 16
@e = external dso_local global [200002 x %struct.Edge], align 16

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z6tarjanii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, 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
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %9 = load i32, ptr @tot, align 4, !tbaa !5
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @tot, align 4, !tbaa !5
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [10001 x i32], ptr @pre, i64 0, i64 %12
  store i32 %9, ptr %13, align 4, !tbaa !5
  store i32 %9, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [10001 x i32], ptr @lnk, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  store i32 %17, ptr %6, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %73, %2
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = icmp ne i32 %19, -1
  br i1 %20, label %22, label %21

21:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %79

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

26:                                               ; preds = %22
  br label %73

27:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [200002 x %struct.Edge], ptr @e, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.Edge, ptr %30, i32 0, i32 1
  %32 = load i32, ptr %31, align 4, !tbaa !9
  store i32 %32, ptr %7, align 4, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [10001 x i32], ptr @pre, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = icmp eq i32 %36, -1
  br i1 %37, label %38, label %59

38:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = xor i32 %40, 1
  %42 = call noundef i32 @_Z6tarjanii(i32 noundef %39, i32 noundef %41)
  store i32 %42, ptr %8, align 4, !tbaa !5
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = icmp sgt i32 %43, %44
  br i1 %45, label %46, label %48

46:                                               ; preds = %38
  %47 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %47, ptr %5, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %46, %38
  %49 = load i32, ptr %3, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [10001 x i32], ptr @pre, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = icmp slt i32 %52, %53
  br i1 %54, label %55, label %58

55:                                               ; preds = %48
  %56 = load i32, ptr %3, align 4, !tbaa !5
  %57 = load i32, ptr %7, align 4, !tbaa !5
  call void @_Z5mergeii(i32 noundef %56, i32 noundef %57)
  br label %58

58:                                               ; preds = %55, %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %72

59:                                               ; preds = %27
  %60 = load i32, ptr %5, align 4, !tbaa !5
  %61 = load i32, ptr %7, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [10001 x i32], ptr @pre, i64 0, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !5
  %65 = icmp sgt i32 %60, %64
  br i1 %65, label %66, label %71

66:                                               ; preds = %59
  %67 = load i32, ptr %7, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [10001 x i32], ptr @pre, i64 0, i64 %68
  %70 = load i32, ptr %69, align 4, !tbaa !5
  store i32 %70, ptr %5, align 4, !tbaa !5
  br label %71

71:                                               ; preds = %66, %59
  br label %72

72:                                               ; preds = %71, %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %73

73:                                               ; preds = %72, %26
  %74 = load i32, ptr %6, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [200002 x %struct.Edge], ptr @e, i64 0, i64 %75
  %77 = getelementptr inbounds %struct.Edge, ptr %76, i32 0, i32 0
  %78 = load i32, ptr %77, align 8, !tbaa !11
  store i32 %78, ptr %6, align 4, !tbaa !5
  br label %18, !llvm.loop !12

79:                                               ; preds = %21
  %80 = load i32, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %80
}

; 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

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 = { 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 = !{!10, !6, i64 4}
!10 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4}
!11 = !{!10, !6, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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.Edge = type { i32, i32 }

@tot = external dso_local local_unnamed_addr global i32, align 4
@lnk = external dso_local local_unnamed_addr global [10001 x i32], align 16
@pre = external dso_local local_unnamed_addr global [10001 x i32], align 16
@e = external dso_local local_unnamed_addr global [200002 x %struct.Edge], align 16

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z6tarjanii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @tot, align 4, !tbaa !5
  %4 = add nsw i32 %3, 1
  store i32 %4, ptr @tot, align 4, !tbaa !5
  %5 = sext i32 %0 to i64
  %6 = getelementptr inbounds [10001 x i32], ptr @pre, i64 0, i64 %5
  store i32 %3, ptr %6, align 4, !tbaa !5
  %7 = getelementptr inbounds [10001 x i32], ptr @lnk, i64 0, i64 %5
  %.02125 = load i32, ptr %7, align 4, !tbaa !5
  %.not26 = icmp eq i32 %.02125, -1
  br i1 %.not26, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph._crit_edge, %2
  %.0.lcssa = phi i32 [ %3, %2 ], [ %.3, %.lr.ph._crit_edge ]
  ret i32 %.0.lcssa

.lr.ph:                                           ; preds = %2, %.lr.ph._crit_edge
  %.02128 = phi i32 [ %.021, %.lr.ph._crit_edge ], [ %.02125, %2 ]
  %.027 = phi i32 [ %.3, %.lr.ph._crit_edge ], [ %3, %2 ]
  %8 = icmp eq i32 %.02128, %1
  %.pre = sext i32 %.02128 to i64
  br i1 %8, label %.lr.ph._crit_edge, label %9

9:                                                ; preds = %.lr.ph
  %10 = getelementptr inbounds [200002 x %struct.Edge], ptr @e, i64 0, i64 %.pre, i32 1
  %11 = load i32, ptr %10, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [10001 x i32], ptr @pre, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp eq i32 %14, -1
  br i1 %15, label %16, label %22

16:                                               ; preds = %9
  %17 = xor i32 %.02128, 1
  %18 = tail call noundef i32 @_Z6tarjanii(i32 noundef %11, i32 noundef %17)
  %spec.select = tail call i32 @llvm.smin.i32(i32 %.027, i32 %18)
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = icmp slt i32 %19, %18
  br i1 %20, label %21, label %.lr.ph._crit_edge

21:                                               ; preds = %16
  tail call void @_Z5mergeii(i32 noundef %0, i32 noundef %11)
  br label %.lr.ph._crit_edge

22:                                               ; preds = %9
  %spec.select24 = tail call i32 @llvm.smin.i32(i32 %.027, i32 %14)
  br label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph, %22, %21, %16
  %.3 = phi i32 [ %spec.select24, %22 ], [ %spec.select, %21 ], [ %spec.select, %16 ], [ %.027, %.lr.ph ]
  %23 = getelementptr inbounds [200002 x %struct.Edge], ptr @e, i64 0, i64 %.pre
  %.021 = load i32, ptr %23, align 8, !tbaa !5
  %.not = icmp eq i32 %.021, -1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #1

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 speculatable willreturn memory(none) }

!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 = !{!10, !6, i64 4}
!10 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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