; LLVM IR from tangjz/acm-icpc :: codeforces/complete/700C.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 }

@lnk = external dso_local global [1001 x i32], align 16
@idx = external dso_local global i32, align 4
@pre = external dso_local global [1001 x i32], align 16
@tot = external dso_local global i32, align 4
@e = external dso_local global [60002 x %struct.Edge], align 16
@bri = external dso_local global [30001 x i8], align 16

; 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 ptr, align 8
  %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 [1001 x i32], ptr @pre, i64 0, i64 %12
  store i32 %10, ptr %13, align 4, !tbaa !5
  store i32 %10, 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 [1001 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 = %83, %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 %89

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

27:                                               ; preds = %22
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = ashr i32 %28, 1
  %30 = load i32, ptr @idx, align 4, !tbaa !5
  %31 = icmp eq i32 %29, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %27, %22
  br label %83

33:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [60002 x %struct.Edge], ptr @e, i64 0, i64 %35
  %37 = getelementptr inbounds %struct.Edge, ptr %36, i32 0, i32 1
  store ptr %37, ptr %7, align 8, !tbaa !9
  %38 = load ptr, ptr %7, align 8, !tbaa !9
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [1001 x i32], ptr @pre, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = icmp eq i32 %42, -1
  br i1 %43, label %44, label %67

44:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %45 = load ptr, ptr %7, align 8, !tbaa !9
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = call noundef i32 @_Z6tarjanii(i32 noundef %46, i32 noundef %47)
  store i32 %48, ptr %8, align 4, !tbaa !5
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = load i32, ptr %8, align 4, !tbaa !5
  %51 = icmp sgt i32 %49, %50
  br i1 %51, label %52, label %54

52:                                               ; preds = %44
  %53 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %53, ptr %5, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %52, %44
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [1001 x i32], ptr @pre, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = load i32, ptr %8, align 4, !tbaa !5
  %60 = icmp slt i32 %58, %59
  br i1 %60, label %61, label %66

61:                                               ; preds = %54
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = ashr i32 %62, 1
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [30001 x i8], ptr @bri, i64 0, i64 %64
  store i8 1, ptr %65, align 1, !tbaa !11
  br label %66

66:                                               ; preds = %61, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %82

67:                                               ; preds = %33
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = load ptr, ptr %7, align 8, !tbaa !9
  %70 = load i32, ptr %69, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [1001 x i32], ptr @pre, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !5
  %74 = icmp sgt i32 %68, %73
  br i1 %74, label %75, label %81

75:                                               ; preds = %67
  %76 = load ptr, ptr %7, align 8, !tbaa !9
  %77 = load i32, ptr %76, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [1001 x i32], ptr @pre, i64 0, i64 %78
  %80 = load i32, ptr %79, align 4, !tbaa !5
  store i32 %80, ptr %5, align 4, !tbaa !5
  br label %81

81:                                               ; preds = %75, %67
  br label %82

82:                                               ; preds = %81, %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %83

83:                                               ; preds = %82, %32
  %84 = load i32, ptr %6, align 4, !tbaa !5
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds [60002 x %struct.Edge], ptr @e, i64 0, i64 %85
  %87 = getelementptr inbounds %struct.Edge, ptr %86, i32 0, i32 0
  %88 = load i32, ptr %87, align 8, !tbaa !13
  store i32 %88, ptr %6, align 4, !tbaa !5
  br label %18, !llvm.loop !15

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

; 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"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 }

@lnk = external dso_local local_unnamed_addr global [1001 x i32], align 16
@idx = external dso_local local_unnamed_addr global i32, align 4
@pre = external dso_local local_unnamed_addr global [1001 x i32], align 16
@tot = external dso_local local_unnamed_addr global i32, align 4
@e = external dso_local local_unnamed_addr global [60002 x %struct.Edge], align 16
@bri = external dso_local local_unnamed_addr global [30001 x i8], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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 [1001 x i32], ptr @pre, i64 0, i64 %5
  store i32 %4, ptr %6, align 4, !tbaa !5
  %7 = getelementptr inbounds [1001 x i32], ptr @lnk, i64 0, i64 %5
  %.02025 = load i32, ptr %7, align 4, !tbaa !5
  %.not26 = icmp eq i32 %.02025, -1
  br i1 %.not26, label %._crit_edge, label %.lr.ph

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

.lr.ph:                                           ; preds = %2, %30
  %.02028 = phi i32 [ %.020, %30 ], [ %.02025, %2 ]
  %.027 = phi i32 [ %.3, %30 ], [ %4, %2 ]
  %8 = xor i32 %.02028, %1
  %9 = icmp eq i32 %8, 1
  br i1 %9, label %30, label %10

10:                                               ; preds = %.lr.ph
  %11 = ashr i32 %.02028, 1
  %12 = load i32, ptr @idx, align 4, !tbaa !5
  %13 = icmp eq i32 %11, %12
  br i1 %13, label %30, label %14

14:                                               ; preds = %10
  %15 = sext i32 %.02028 to i64
  %16 = getelementptr inbounds [60002 x %struct.Edge], ptr @e, i64 0, i64 %15, i32 1
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [1001 x i32], ptr @pre, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = icmp eq i32 %20, -1
  br i1 %21, label %22, label %29

22:                                               ; preds = %14
  %23 = tail call noundef i32 @_Z6tarjanii(i32 noundef %17, i32 noundef %.02028)
  %spec.select = tail call i32 @llvm.smin.i32(i32 %.027, i32 %23)
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = icmp slt i32 %24, %23
  br i1 %25, label %26, label %30

26:                                               ; preds = %22
  %27 = sext i32 %11 to i64
  %28 = getelementptr inbounds [30001 x i8], ptr @bri, i64 0, i64 %27
  store i8 1, ptr %28, align 1, !tbaa !9
  br label %30

29:                                               ; preds = %14
  %spec.select24 = tail call i32 @llvm.smin.i32(i32 %.027, i32 %20)
  br label %30

30:                                               ; preds = %29, %26, %22, %.lr.ph, %10
  %.3 = phi i32 [ %.027, %.lr.ph ], [ %.027, %10 ], [ %spec.select, %26 ], [ %spec.select, %22 ], [ %spec.select24, %29 ]
  %31 = sext i32 %.02028 to i64
  %32 = getelementptr inbounds [60002 x %struct.Edge], ptr @e, i64 0, i64 %31
  %.020 = load i32, ptr %32, align 8, !tbaa !5
  %.not = icmp eq i32 %.020, -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 nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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