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

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

@tot = external dso_local global i32, align 4
@lnk = external dso_local global [400001 x i32], align 16
@ord = external dso_local global [400001 x i32], align 16
@fa = external dso_local global [400001 x i32], align 16
@e = external dso_local global [800003 x %struct.Edge], align 16
@ban = external dso_local global [400001 x i8], 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 void @_Z3bfsi(i32 noundef %0) #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 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  store i32 0, ptr @tot, align 4, !tbaa !5
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %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 = sext i32 %10 to i64
  %12 = getelementptr inbounds [400001 x i32], ptr @ord, i64 0, i64 %11
  store i32 %8, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %71, %1
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = load i32, ptr @tot, align 4, !tbaa !5
  %16 = icmp sle i32 %14, %15
  br i1 %16, label %18, label %17

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

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

27:                                               ; preds = %64, %18
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %31, label %30

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

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

43:                                               ; preds = %31
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = ashr i32 %44, 1
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [400001 x i8], ptr @ban, i64 0, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !11, !range !13, !noundef !14
  %49 = trunc i8 %48 to i1
  br i1 %49, label %50, label %51

50:                                               ; preds = %43, %31
  store i32 7, ptr %4, align 4
  br label %61

51:                                               ; preds = %43
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = load i32, ptr %7, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [400001 x i32], ptr @fa, i64 0, i64 %54
  store i32 %52, ptr %55, align 4, !tbaa !5
  %56 = load i32, ptr %7, align 4, !tbaa !5
  %57 = load i32, ptr @tot, align 4, !tbaa !5
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr @tot, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [400001 x i32], ptr @ord, i64 0, i64 %59
  store i32 %56, ptr %60, align 4, !tbaa !5
  store i32 0, ptr %4, align 4
  br label %61

61:                                               ; preds = %51, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %62 = load i32, ptr %4, align 4
  switch i32 %62, label %75 [
    i32 0, label %63
    i32 7, label %64
  ]

63:                                               ; preds = %61
  br label %64

64:                                               ; preds = %63, %61
  %65 = load i32, ptr %6, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [800003 x %struct.Edge], ptr @e, i64 0, i64 %66
  %68 = getelementptr inbounds %struct.Edge, ptr %67, i32 0, i32 0
  %69 = load i32, ptr %68, align 4, !tbaa !15
  store i32 %69, ptr %6, align 4, !tbaa !5
  br label %27, !llvm.loop !16

70:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %3, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %3, align 4, !tbaa !5
  br label %13, !llvm.loop !18

74:                                               ; preds = %17
  ret void

75:                                               ; preds = %61
  unreachable
}

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 = { 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, !6, i64 8}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!10, !6, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


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

@tot = external dso_local local_unnamed_addr global i32, align 4
@lnk = external dso_local local_unnamed_addr global [400001 x i32], align 16
@ord = external dso_local local_unnamed_addr global [400001 x i32], align 16
@fa = external dso_local local_unnamed_addr global [400001 x i32], align 16
@e = external dso_local local_unnamed_addr global [800003 x %struct.Edge], align 16
@ban = external dso_local local_unnamed_addr global [400001 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z3bfsi(i32 noundef %0) local_unnamed_addr #0 {
.lr.ph29.preheader:
  store i32 1, ptr @tot, align 4, !tbaa !5
  store i32 %0, ptr getelementptr inbounds (i8, ptr @ord, i64 4), align 4, !tbaa !5
  br label %.lr.ph29

._crit_edge30:                                    ; preds = %._crit_edge
  ret void

.lr.ph29:                                         ; preds = %.lr.ph29.preheader, %._crit_edge
  %indvars.iv = phi i64 [ 1, %.lr.ph29.preheader ], [ %indvars.iv.next, %._crit_edge ]
  %tot.promoted2426 = phi i32 [ 1, %.lr.ph29.preheader ], [ %tot.promoted23, %._crit_edge ]
  %1 = getelementptr inbounds [400001 x i32], ptr @ord, i64 0, i64 %indvars.iv
  %2 = load i32, ptr %1, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = getelementptr inbounds [400001 x i32], ptr @lnk, i64 0, i64 %3
  %.01417 = load i32, ptr %4, align 4, !tbaa !5
  %.not1618 = icmp eq i32 %.01417, 0
  br i1 %.not1618, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph29
  %5 = getelementptr inbounds [400001 x i32], ptr @fa, i64 0, i64 %3
  br label %7

._crit_edge:                                      ; preds = %27, %.lr.ph29
  %tot.promoted23 = phi i32 [ %tot.promoted2426, %.lr.ph29 ], [ %tot.promoted21, %27 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %6 = sext i32 %tot.promoted23 to i64
  %.not.not = icmp slt i64 %indvars.iv, %6
  br i1 %.not.not, label %.lr.ph29, label %._crit_edge30, !llvm.loop !9

7:                                                ; preds = %.lr.ph, %27
  %tot.promoted22 = phi i32 [ %tot.promoted2426, %.lr.ph ], [ %tot.promoted21, %27 ]
  %.01419 = phi i32 [ %.01417, %.lr.ph ], [ %.014, %27 ]
  %8 = phi i32 [ %tot.promoted2426, %.lr.ph ], [ %28, %27 ]
  %9 = sext i32 %.01419 to i64
  %10 = getelementptr inbounds [800003 x %struct.Edge], ptr @e, i64 0, i64 %9
  %11 = getelementptr inbounds i8, ptr %10, i64 4
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = icmp eq i32 %12, %13
  br i1 %14, label %27, label %15

15:                                               ; preds = %7
  %16 = ashr i32 %.01419, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [400001 x i8], ptr @ban, i64 0, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !13, !range !15, !noundef !16
  %20 = trunc nuw i8 %19 to i1
  br i1 %20, label %27, label %21

21:                                               ; preds = %15
  %22 = sext i32 %12 to i64
  %23 = getelementptr inbounds [400001 x i32], ptr @fa, i64 0, i64 %22
  store i32 %2, ptr %23, align 4, !tbaa !5
  %24 = add nsw i32 %8, 1
  store i32 %24, ptr @tot, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [400001 x i32], ptr @ord, i64 0, i64 %25
  store i32 %12, ptr %26, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %7, %15, %21
  %tot.promoted21 = phi i32 [ %tot.promoted22, %7 ], [ %tot.promoted22, %15 ], [ %24, %21 ]
  %28 = phi i32 [ %8, %7 ], [ %8, %15 ], [ %24, %21 ]
  %.014 = load i32, ptr %10, align 4, !tbaa !5
  %.not16 = icmp eq i32 %.014, 0
  br i1 %.not16, label %._crit_edge, label %7, !llvm.loop !17
}

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

!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 = !{!12, !6, i64 4}
!12 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4, !6, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !10}


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