; LLVM IR from tangjz/acm-icpc :: codeforces/complete/700C.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 [1001 x i32], align 16
@idx = external dso_local global i32, align 4
@bcnt = external dso_local global i32, align 4
@bno = external dso_local global [1001 x i32], align 16
@e = external dso_local global [60002 x %struct.Edge], align 16
@bri = external dso_local global [30001 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 uwtable
define dso_local void @_Z3dfsi(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %4 = load i32, ptr @bcnt, align 4, !tbaa !5
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [1001 x i32], ptr @bno, i64 0, i64 %6
  store i32 %4, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [1001 x i32], ptr @lnk, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %11, ptr %3, align 4, !tbaa !5
  br label %12

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

15:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %51

16:                                               ; preds = %12
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = ashr i32 %17, 1
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [30001 x i8], ptr @bri, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !9, !range !11, !noundef !12
  %22 = trunc i8 %21 to i1
  br i1 %22, label %44, label %23

23:                                               ; preds = %16
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [60002 x %struct.Edge], ptr @e, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.Edge, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !13
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [1001 x i32], ptr @bno, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = icmp eq i32 %31, -1
  br i1 %32, label %33, label %44

33:                                               ; preds = %23
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = ashr i32 %34, 1
  %36 = load i32, ptr @idx, align 4, !tbaa !5
  %37 = icmp ne i32 %35, %36
  br i1 %37, label %38, label %44

38:                                               ; preds = %33
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [60002 x %struct.Edge], ptr @e, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.Edge, ptr %41, i32 0, i32 1
  %43 = load i32, ptr %42, align 4, !tbaa !13
  call void @_Z3dfsi(i32 noundef %43)
  br label %44

44:                                               ; preds = %38, %33, %23, %16
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [60002 x %struct.Edge], ptr @e, i64 0, i64 %47
  %49 = getelementptr inbounds %struct.Edge, ptr %48, i32 0, i32 0
  %50 = load i32, ptr %49, align 8, !tbaa !15
  store i32 %50, ptr %3, align 4, !tbaa !5
  br label %12, !llvm.loop !16

51:                                               ; preds = %15
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !6, i64 4}
!14 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4}
!15 = !{!14, !6, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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
@bcnt = external dso_local local_unnamed_addr global i32, align 4
@bno = external dso_local local_unnamed_addr global [1001 x i32], align 16
@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 void @_Z3dfsi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @bcnt, align 4, !tbaa !5
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [1001 x i32], ptr @bno, i64 0, i64 %3
  store i32 %2, ptr %4, align 4, !tbaa !5
  %5 = getelementptr inbounds [1001 x i32], ptr @lnk, i64 0, i64 %3
  %.09 = load i32, ptr %5, align 4, !tbaa !5
  %.not10 = icmp eq i32 %.09, -1
  br i1 %.not10, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph._crit_edge, %1
  ret void

.lr.ph:                                           ; preds = %1, %.lr.ph._crit_edge
  %.011 = phi i32 [ %.0, %.lr.ph._crit_edge ], [ %.09, %1 ]
  %6 = ashr i32 %.011, 1
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [30001 x i8], ptr @bri, i64 0, i64 %7
  %9 = load i8, ptr %8, align 1, !tbaa !9, !range !11, !noundef !12
  %10 = trunc nuw i8 %9 to i1
  %.pre = sext i32 %.011 to i64
  br i1 %10, label %.lr.ph._crit_edge, label %11

11:                                               ; preds = %.lr.ph
  %12 = getelementptr inbounds [60002 x %struct.Edge], ptr @e, i64 0, i64 %.pre, i32 1
  %13 = load i32, ptr %12, align 4, !tbaa !13
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [1001 x i32], ptr @bno, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp ne i32 %16, -1
  %18 = load i32, ptr @idx, align 4
  %.not8 = icmp eq i32 %6, %18
  %or.cond = select i1 %17, i1 true, i1 %.not8
  br i1 %or.cond, label %.lr.ph._crit_edge, label %19

19:                                               ; preds = %11
  tail call void @_Z3dfsi(i32 noundef %13)
  br label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph, %11, %19
  %20 = getelementptr inbounds [60002 x %struct.Edge], ptr @e, i64 0, i64 %.pre
  %.0 = load i32, ptr %20, align 8, !tbaa !5
  %.not = icmp eq i32 %.0, -1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15
}

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


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