; LLVM IR from tangjz/acm-icpc :: codeforces/gym/100827C.ll (Z3bfsv)

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

@n = external dso_local global i32, align 4
@s = external dso_local global i32, align 4
@t = external dso_local global i32, align 4
@lnk = external dso_local global [1001 x i32], align 16
@pos = external dso_local global [1001 x i32], align 16
@e = external dso_local global [7001 x %struct.Edge], align 16
@_ZZ3bfsvE3que = external hidden global [1001 x i32], align 16

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z3bfsv() #1 {
  %1 = alloca i1, align 1
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = load i32, ptr @n, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = mul i64 %8, 4
  call void @llvm.memset.p0.i64(ptr align 16 @pos, i8 -1, i64 %9, i1 false)
  %10 = load i32, ptr @s, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [1001 x i32], ptr @pos, i64 0, i64 %11
  store i32 0, ptr %12, align 4, !tbaa !5
  %13 = load i32, ptr @s, align 4, !tbaa !5
  store i32 %13, ptr @_ZZ3bfsvE3que, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %93, %0
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %4, align 4
  br label %96

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [1001 x i32], ptr @_ZZ3bfsvE3que, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  store i32 %23, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [1001 x i32], ptr @lnk, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  store i32 %27, ptr %6, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %81, %19
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = icmp ne i32 %29, -1
  br i1 %30, label %32, label %31

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

32:                                               ; preds = %28
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [7001 x %struct.Edge], ptr @e, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.Edge, ptr %35, i32 0, i32 2
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = icmp sgt i32 %37, 0
  br i1 %38, label %39, label %80

39:                                               ; preds = %32
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [7001 x %struct.Edge], ptr @e, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.Edge, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !11
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [1001 x i32], ptr @pos, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = icmp eq i32 %47, -1
  br i1 %48, label %49, label %80

49:                                               ; preds = %39
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [1001 x i32], ptr @pos, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  %55 = load i32, ptr %6, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [7001 x %struct.Edge], ptr @e, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.Edge, ptr %57, i32 0, i32 1
  %59 = load i32, ptr %58, align 4, !tbaa !11
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [1001 x i32], ptr @pos, i64 0, i64 %60
  store i32 %54, ptr %61, align 4, !tbaa !5
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [7001 x %struct.Edge], ptr @e, i64 0, i64 %63
  %65 = getelementptr inbounds %struct.Edge, ptr %64, i32 0, i32 1
  %66 = load i32, ptr %65, align 4, !tbaa !11
  %67 = load i32, ptr @t, align 4, !tbaa !5
  %68 = icmp eq i32 %66, %67
  br i1 %68, label %69, label %70

69:                                               ; preds = %49
  store i1 true, ptr %1, align 1
  store i32 1, ptr %4, align 4
  br label %87

70:                                               ; preds = %49
  %71 = load i32, ptr %6, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [7001 x %struct.Edge], ptr @e, i64 0, i64 %72
  %74 = getelementptr inbounds %struct.Edge, ptr %73, i32 0, i32 1
  %75 = load i32, ptr %74, align 4, !tbaa !11
  %76 = load i32, ptr %3, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %3, align 4, !tbaa !5
  %78 = sext i32 %76 to i64
  %79 = getelementptr inbounds [1001 x i32], ptr @_ZZ3bfsvE3que, i64 0, i64 %78
  store i32 %75, ptr %79, align 4, !tbaa !5
  br label %80

80:                                               ; preds = %70, %39, %32
  br label %81

81:                                               ; preds = %80
  %82 = load i32, ptr %6, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [7001 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 4, !tbaa !12
  store i32 %86, ptr %6, align 4, !tbaa !5
  br label %28, !llvm.loop !13

87:                                               ; preds = %69, %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %88 = load i32, ptr %4, align 4
  switch i32 %88, label %90 [
    i32 5, label %89
  ]

89:                                               ; preds = %87
  store i32 0, ptr %4, align 4
  br label %90

90:                                               ; preds = %89, %87
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %91 = load i32, ptr %4, align 4
  switch i32 %91, label %96 [
    i32 0, label %92
  ]

92:                                               ; preds = %90
  br label %93

93:                                               ; preds = %92
  %94 = load i32, ptr %2, align 4, !tbaa !5
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %2, align 4, !tbaa !5
  br label %14, !llvm.loop !15

96:                                               ; preds = %90, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  %97 = load i32, ptr %4, align 4
  switch i32 %97, label %101 [
    i32 2, label %98
    i32 1, label %99
  ]

98:                                               ; preds = %96
  store i1 false, ptr %1, align 1
  br label %99

99:                                               ; preds = %98, %96
  %100 = load i1, ptr %1, align 1
  ret i1 %100

101:                                              ; preds = %96
  unreachable
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 8}
!10 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@s = external dso_local local_unnamed_addr global i32, align 4
@t = external dso_local local_unnamed_addr global i32, align 4
@lnk = external dso_local local_unnamed_addr global [1001 x i32], align 16
@pos = external dso_local local_unnamed_addr global [1001 x i32], align 16
@e = external dso_local local_unnamed_addr global [7001 x %struct.Edge], align 16
@_ZZ3bfsvE3que = external hidden local_unnamed_addr global [1001 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z3bfsv() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = sext i32 %1 to i64
  %3 = shl nsw i64 %2, 2
  tail call void @llvm.memset.p0.i64(ptr nonnull align 16 @pos, i8 -1, i64 %3, i1 false)
  %4 = load i32, ptr @s, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [1001 x i32], ptr @pos, i64 0, i64 %5
  store i32 0, ptr %6, align 4, !tbaa !5
  store i32 %4, ptr @_ZZ3bfsvE3que, align 16, !tbaa !5
  %7 = load i32, ptr @t, align 4
  br label %8

8:                                                ; preds = %0, %._crit_edge
  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next, %._crit_edge ]
  %.02137 = phi i32 [ 1, %0 ], [ %.122.lcssa, %._crit_edge ]
  %9 = getelementptr inbounds [1001 x i32], ptr @_ZZ3bfsvE3que, i64 0, i64 %indvars.iv
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [1001 x i32], ptr @lnk, i64 0, i64 %11
  %.032 = load i32, ptr %12, align 4, !tbaa !5
  %.not33 = icmp eq i32 %.032, -1
  br i1 %.not33, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %8
  %13 = getelementptr inbounds [1001 x i32], ptr @pos, i64 0, i64 %11
  br label %14

14:                                               ; preds = %.lr.ph, %35
  %.035 = phi i32 [ %.032, %.lr.ph ], [ %.0, %35 ]
  %.12234 = phi i32 [ %.02137, %.lr.ph ], [ %.223, %35 ]
  %15 = sext i32 %.035 to i64
  %16 = getelementptr inbounds [7001 x %struct.Edge], ptr @e, i64 0, i64 %15
  %17 = getelementptr inbounds i8, ptr %16, i64 8
  %18 = load i32, ptr %17, align 4, !tbaa !9
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %20, label %35

20:                                               ; preds = %14
  %21 = getelementptr inbounds i8, ptr %16, i64 4
  %22 = load i32, ptr %21, align 4, !tbaa !11
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1001 x i32], ptr @pos, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp eq i32 %25, -1
  br i1 %26, label %27, label %35

27:                                               ; preds = %20
  %28 = load i32, ptr %13, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %24, align 4, !tbaa !5
  %30 = icmp eq i32 %22, %7
  br i1 %30, label %.loopexit, label %31

31:                                               ; preds = %27
  %32 = add nsw i32 %.12234, 1
  %33 = sext i32 %.12234 to i64
  %34 = getelementptr inbounds [1001 x i32], ptr @_ZZ3bfsvE3que, i64 0, i64 %33
  store i32 %22, ptr %34, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %14, %20, %31
  %.223 = phi i32 [ %32, %31 ], [ %.12234, %20 ], [ %.12234, %14 ]
  %.0 = load i32, ptr %16, align 4, !tbaa !5
  %.not = icmp eq i32 %.0, -1
  br i1 %.not, label %._crit_edge, label %14, !llvm.loop !12

._crit_edge:                                      ; preds = %35, %8
  %.122.lcssa = phi i32 [ %.02137, %8 ], [ %.223, %35 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %36 = sext i32 %.122.lcssa to i64
  %.not27 = icmp slt i64 %indvars.iv.next, %36
  br i1 %.not27, label %8, label %.loopexit, !llvm.loop !14

.loopexit:                                        ; preds = %._crit_edge, %27
  %.not2731 = phi i1 [ true, %27 ], [ false, %._crit_edge ]
  ret i1 %.not2731
}

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

attributes #0 = { mustprogress nofree norecurse 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

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


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