; LLVM IR from tangjz/acm-icpc :: codeforces/complete/500G.ll (Z3dfsi)

; 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 [200001 x i32], align 16
@dep = external dso_local global [200001 x i32], align 16
@fa = external dso_local global [200001 x [19 x i32]], align 16
@e = external dso_local global [400002 x %struct.Edge], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3dfsi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [200001 x i32], ptr @lnk, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %10, ptr %3, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %90, %1
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %96

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [400002 x %struct.Edge], ptr @e, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.Edge, ptr %18, i32 0, i32 0
  store ptr %19, ptr %5, align 8, !tbaa !9
  %20 = load ptr, ptr %5, align 8, !tbaa !9
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %23
  %25 = getelementptr inbounds [19 x i32], ptr %24, i64 0, i64 0
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = icmp eq i32 %21, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %15
  store i32 4, ptr %4, align 4
  br label %87

29:                                               ; preds = %15
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [200001 x i32], ptr @dep, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [200001 x i32], ptr @dep, i64 0, i64 %37
  store i32 %34, ptr %38, align 4, !tbaa !5
  %39 = load i32, ptr %2, align 4, !tbaa !5
  %40 = load ptr, ptr %5, align 8, !tbaa !9
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %42
  %44 = getelementptr inbounds [19 x i32], ptr %43, i64 0, i64 0
  store i32 %39, ptr %44, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %81, %29
  %46 = load ptr, ptr %5, align 8, !tbaa !9
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %48
  %50 = load i32, ptr %6, align 4, !tbaa !5
  %51 = sub nsw i32 %50, 1
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [19 x i32], ptr %49, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %57, label %56

56:                                               ; preds = %45
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %84

57:                                               ; preds = %45
  %58 = load ptr, ptr %5, align 8, !tbaa !9
  %59 = load i32, ptr %58, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %60
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = sub nsw i32 %62, 1
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [19 x i32], ptr %61, i64 0, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %67
  %69 = load i32, ptr %6, align 4, !tbaa !5
  %70 = sub nsw i32 %69, 1
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [19 x i32], ptr %68, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !5
  %74 = load ptr, ptr %5, align 8, !tbaa !9
  %75 = load i32, ptr %74, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %76
  %78 = load i32, ptr %6, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [19 x i32], ptr %77, i64 0, i64 %79
  store i32 %73, ptr %80, align 4, !tbaa !5
  br label %81

81:                                               ; preds = %57
  %82 = load i32, ptr %6, align 4, !tbaa !5
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %6, align 4, !tbaa !5
  br label %45, !llvm.loop !11

84:                                               ; preds = %56
  %85 = load ptr, ptr %5, align 8, !tbaa !9
  %86 = load i32, ptr %85, align 4, !tbaa !5
  call void @_Z3dfsi(i32 noundef %86)
  store i32 0, ptr %4, align 4
  br label %87

87:                                               ; preds = %84, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  %88 = load i32, ptr %4, align 4
  switch i32 %88, label %97 [
    i32 0, label %89
    i32 4, label %90
  ]

89:                                               ; preds = %87
  br label %90

90:                                               ; preds = %89, %87
  %91 = load i32, ptr %3, align 4, !tbaa !5
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [400002 x %struct.Edge], ptr @e, i64 0, i64 %92
  %94 = getelementptr inbounds %struct.Edge, ptr %93, i32 0, i32 1
  %95 = load i32, ptr %94, align 4, !tbaa !13
  store i32 %95, ptr %3, align 4, !tbaa !5
  br label %11, !llvm.loop !15

96:                                               ; preds = %14
  ret void

97:                                               ; preds = %87
  unreachable
}

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


; 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 [200001 x i32], align 16
@dep = external dso_local local_unnamed_addr global [200001 x i32], align 16
@fa = external dso_local local_unnamed_addr global [200001 x [19 x i32]], align 16
@e = external dso_local local_unnamed_addr global [400002 x %struct.Edge], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z3dfsi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [200001 x i32], ptr @lnk, i64 0, i64 %2
  %.01824 = load i32, ptr %3, align 4, !tbaa !5
  %.not25 = icmp eq i32 %.01824, 0
  br i1 %.not25, label %._crit_edge29, label %.lr.ph28

.lr.ph28:                                         ; preds = %1
  %4 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %2
  %5 = getelementptr inbounds [200001 x i32], ptr @dep, i64 0, i64 %2
  %.not2122 = icmp eq i32 %0, 0
  br label %6

._crit_edge29:                                    ; preds = %24, %1
  ret void

6:                                                ; preds = %.lr.ph28, %24
  %.01826 = phi i32 [ %.01824, %.lr.ph28 ], [ %.018, %24 ]
  %7 = sext i32 %.01826 to i64
  %8 = getelementptr inbounds [400002 x %struct.Edge], ptr @e, i64 0, i64 %7
  %9 = load i32, ptr %8, align 8, !tbaa !5
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = icmp eq i32 %9, %10
  br i1 %11, label %24, label %12

12:                                               ; preds = %6
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  %15 = sext i32 %9 to i64
  %16 = getelementptr inbounds [200001 x i32], ptr @dep, i64 0, i64 %15
  store i32 %14, ptr %16, align 4, !tbaa !5
  %17 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %15
  store i32 %0, ptr %17, align 4, !tbaa !5
  br i1 %.not2122, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %12
  tail call void @_Z3dfsi(i32 noundef %9)
  br label %24

.lr.ph:                                           ; preds = %12, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %12 ]
  %18 = phi i32 [ %22, %.lr.ph ], [ %0, %12 ]
  %19 = phi i64 [ %indvars.iv, %.lr.ph ], [ 0, %12 ]
  %20 = sext i32 %18 to i64
  %21 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %20, i64 %19
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = getelementptr inbounds [200001 x [19 x i32]], ptr @fa, i64 0, i64 %15, i64 %indvars.iv
  store i32 %22, ptr %23, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.not21 = icmp eq i32 %22, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph, !llvm.loop !9

24:                                               ; preds = %6, %._crit_edge
  %25 = getelementptr inbounds i8, ptr %8, i64 4
  %.018 = load i32, ptr %25, align 4, !tbaa !5
  %.not = icmp eq i32 %.018, 0
  br i1 %.not, label %._crit_edge29, label %6, !llvm.loop !11
}

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

!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"}
!11 = distinct !{!11, !10}


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