; LLVM IR from tangjz/acm-icpc :: TDL_training/2015-11-02/C - Necklace - tjz.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
@used = external dso_local global [200002 x i8], align 16
@e = external dso_local global [200002 x %struct.Edge], 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 [10001 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 [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 = %81, %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 %87

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 %27, label %28

27:                                               ; preds = %22
  br label %81

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

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

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

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

56:                                               ; preds = %49
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = xor i32 %57, 1
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [200002 x i8], ptr @used, i64 0, i64 %59
  store i8 1, ptr %60, align 1, !tbaa !11
  %61 = load i32, ptr %6, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [200002 x i8], ptr @used, i64 0, i64 %62
  store i8 1, ptr %63, align 1, !tbaa !11
  br label %64

64:                                               ; preds = %56, %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %80

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

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

79:                                               ; preds = %73, %65
  br label %80

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

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

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

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

@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
@used = external dso_local local_unnamed_addr global [200002 x i8], align 16
@e = external dso_local local_unnamed_addr global [200002 x %struct.Edge], 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 [10001 x i32], ptr @pre, i64 0, i64 %5
  store i32 %4, ptr %6, align 4, !tbaa !5
  %7 = getelementptr inbounds [10001 x i32], ptr @lnk, i64 0, i64 %5
  %.02026 = load i32, ptr %7, align 4, !tbaa !5
  %.not27 = icmp eq i32 %.02026, -1
  br i1 %.not27, label %._crit_edge, label %.lr.ph

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

.lr.ph:                                           ; preds = %2, %.lr.ph._crit_edge
  %.02029 = phi i32 [ %.020, %.lr.ph._crit_edge ], [ %.02026, %2 ]
  %.028 = phi i32 [ %.3, %.lr.ph._crit_edge ], [ %4, %2 ]
  %8 = xor i32 %.02029, 1
  %9 = icmp eq i32 %8, %1
  %.pre = sext i32 %.02029 to i64
  br i1 %9, label %.lr.ph._crit_edge, label %10

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

16:                                               ; preds = %10
  %17 = tail call noundef i32 @_Z6tarjanii(i32 noundef %12, i32 noundef %.02029)
  %spec.select = tail call i32 @llvm.smin.i32(i32 %.028, i32 %17)
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = icmp slt i32 %18, %17
  br i1 %19, label %20, label %.lr.ph._crit_edge

20:                                               ; preds = %16
  %21 = sext i32 %8 to i64
  %22 = getelementptr inbounds [200002 x i8], ptr @used, i64 0, i64 %21
  store i8 1, ptr %22, align 1, !tbaa !9
  %23 = getelementptr inbounds [200002 x i8], ptr @used, i64 0, i64 %.pre
  store i8 1, ptr %23, align 1, !tbaa !9
  br label %.lr.ph._crit_edge

24:                                               ; preds = %10
  %spec.select25 = tail call i32 @llvm.smin.i32(i32 %.028, i32 %15)
  br label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph, %24, %20, %16
  %.3 = phi i32 [ %spec.select25, %24 ], [ %spec.select, %20 ], [ %spec.select, %16 ], [ %.028, %.lr.ph ]
  %25 = getelementptr inbounds [200002 x %struct.Edge], ptr @e, i64 0, i64 %.pre
  %.020 = load i32, ptr %25, 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