; LLVM IR from tangjz/acm-icpc :: self_training/2016-07-24/A.ll (Z3dfsiRi)

; 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 [201 x i32], align 16
@idx = external dso_local global i32, align 4
@e = external dso_local global [402 x %struct.Edge], align 16
@vis = external dso_local global [201 x i8], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z3dfsiRi(i32 noundef %0, ptr noundef nonnull align 4 dereferenceable(4) %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [201 x i8], ptr @vis, i64 0, i64 %10
  store i8 1, ptr %11, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [201 x i32], ptr @lnk, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  store i32 %15, ptr %7, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %59, %2
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = icmp ne i32 %17, -1
  br i1 %18, label %20, label %19

19:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %65

20:                                               ; preds = %16
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = ashr i32 %21, 1
  %23 = load i32, ptr @idx, align 4, !tbaa !5
  %24 = icmp eq i32 %22, %23
  br i1 %24, label %35, label %25

25:                                               ; preds = %20
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [402 x %struct.Edge], ptr @e, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.Edge, ptr %28, i32 0, i32 1
  %30 = load i32, ptr %29, align 4, !tbaa !13
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [201 x i8], ptr @vis, i64 0, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !11, !range !15, !noundef !16
  %34 = trunc i8 %33 to i1
  br i1 %34, label %35, label %36

35:                                               ; preds = %25, %20
  br label %59

36:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [402 x %struct.Edge], ptr @e, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.Edge, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !13
  %42 = load ptr, ptr %4, align 8, !tbaa !9
  %43 = call noundef i32 @_Z3dfsiRi(i32 noundef %41, ptr noundef nonnull align 4 dereferenceable(4) %42)
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %8, align 4, !tbaa !5
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = load i32, ptr %8, align 4, !tbaa !5
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %48, label %51

48:                                               ; preds = %36
  %49 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %49, ptr %6, align 4, !tbaa !5
  %50 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %50, ptr %5, align 4, !tbaa !5
  br label %58

51:                                               ; preds = %36
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = icmp slt i32 %52, %53
  br i1 %54, label %55, label %57

55:                                               ; preds = %51
  %56 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %56, ptr %6, align 4, !tbaa !5
  br label %57

57:                                               ; preds = %55, %51
  br label %58

58:                                               ; preds = %57, %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %59

59:                                               ; preds = %58, %35
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [402 x %struct.Edge], ptr @e, i64 0, i64 %61
  %63 = getelementptr inbounds %struct.Edge, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %63, align 8, !tbaa !17
  store i32 %64, ptr %7, align 4, !tbaa !5
  br label %16, !llvm.loop !18

65:                                               ; preds = %19
  %66 = load ptr, ptr %4, align 8, !tbaa !9
  %67 = load i32, ptr %66, align 4, !tbaa !5
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = load i32, ptr %6, align 4, !tbaa !5
  %70 = add nsw i32 %68, %69
  %71 = icmp slt i32 %67, %70
  br i1 %71, label %72, label %77

72:                                               ; preds = %65
  %73 = load i32, ptr %5, align 4, !tbaa !5
  %74 = load i32, ptr %6, align 4, !tbaa !5
  %75 = add nsw i32 %73, %74
  %76 = load ptr, ptr %4, align 8, !tbaa !9
  store i32 %75, ptr %76, align 4, !tbaa !5
  br label %77

77:                                               ; preds = %72, %65
  %78 = load i32, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %78
}

; 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 4}
!14 = !{!"_ZTS4Edge", !6, i64 0, !6, i64 4}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!14, !6, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"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 [201 x i32], align 16
@idx = external dso_local local_unnamed_addr global i32, align 4
@e = external dso_local local_unnamed_addr global [402 x %struct.Edge], align 16
@vis = external dso_local local_unnamed_addr global [201 x i8], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z3dfsiRi(i32 noundef %0, ptr nocapture noundef nonnull align 4 dereferenceable(4) %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [201 x i8], ptr @vis, i64 0, i64 %3
  store i8 1, ptr %4, align 1, !tbaa !5
  %5 = getelementptr inbounds [201 x i32], ptr @lnk, i64 0, i64 %3
  %.02130 = load i32, ptr %5, align 4, !tbaa !9
  %.not31 = icmp eq i32 %.02130, -1
  br i1 %.not31, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %.pre36 = load i32, ptr @idx, align 4, !tbaa !9
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph._crit_edge, %2
  %.022.lcssa = phi i32 [ 0, %2 ], [ %.224, %.lr.ph._crit_edge ]
  %.0.lcssa = phi i32 [ 0, %2 ], [ %.2, %.lr.ph._crit_edge ]
  %6 = load i32, ptr %1, align 4, !tbaa !9
  %7 = add nsw i32 %.0.lcssa, %.022.lcssa
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %25, label %26

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph._crit_edge
  %9 = phi i32 [ %23, %.lr.ph._crit_edge ], [ %.pre36, %.lr.ph.preheader ]
  %.02134 = phi i32 [ %.021, %.lr.ph._crit_edge ], [ %.02130, %.lr.ph.preheader ]
  %.033 = phi i32 [ %.2, %.lr.ph._crit_edge ], [ 0, %.lr.ph.preheader ]
  %.02232 = phi i32 [ %.224, %.lr.ph._crit_edge ], [ 0, %.lr.ph.preheader ]
  %10 = ashr i32 %.02134, 1
  %11 = icmp eq i32 %10, %9
  %.pre37 = sext i32 %.02134 to i64
  br i1 %11, label %.lr.ph._crit_edge, label %12

12:                                               ; preds = %.lr.ph
  %13 = getelementptr inbounds [402 x %struct.Edge], ptr @e, i64 0, i64 %.pre37, i32 1
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [201 x i8], ptr @vis, i64 0, i64 %15
  %17 = load i8, ptr %16, align 1, !tbaa !5, !range !13, !noundef !14
  %18 = trunc nuw i8 %17 to i1
  br i1 %18, label %.lr.ph._crit_edge, label %19

19:                                               ; preds = %12
  %20 = tail call noundef i32 @_Z3dfsiRi(i32 noundef %14, ptr noundef nonnull align 4 dereferenceable(4) %1)
  %21 = add nsw i32 %20, 1
  %.not28 = icmp sgt i32 %.033, %20
  %.pre = load i32, ptr @idx, align 4, !tbaa !9
  br i1 %.not28, label %22, label %.lr.ph._crit_edge

22:                                               ; preds = %19
  %.not29 = icmp sgt i32 %.02232, %20
  %spec.select = select i1 %.not29, i32 %.02232, i32 %21
  br label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph, %22, %19, %12
  %23 = phi i32 [ %.pre, %22 ], [ %.pre, %19 ], [ %9, %12 ], [ %9, %.lr.ph ]
  %.224 = phi i32 [ %spec.select, %22 ], [ %.033, %19 ], [ %.02232, %12 ], [ %.02232, %.lr.ph ]
  %.2 = phi i32 [ %.033, %22 ], [ %21, %19 ], [ %.033, %12 ], [ %.033, %.lr.ph ]
  %24 = getelementptr inbounds [402 x %struct.Edge], ptr @e, i64 0, i64 %.pre37
  %.021 = load i32, ptr %24, align 8, !tbaa !9
  %.not = icmp eq i32 %.021, -1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

25:                                               ; preds = %._crit_edge
  store i32 %7, ptr %1, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %25, %._crit_edge
  ret i32 %.0.lcssa
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, 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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"_ZTS4Edge", !10, i64 0, !10, i64 4}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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