; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 17. Graph (BFS graph)

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

@f = external dso_local global i32, align 4
@r = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@a = external dso_local global [100 x [100 x i32]], align 16
@visited = external dso_local global [100 x i32], align 16
@q = external dso_local global [100 x i32], align 16
@level = external dso_local global [100 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local void @bfs(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, 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 %4

4:                                                ; preds = %61, %1
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp sle i32 %5, %6
  br i1 %7, label %9, label %8

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

9:                                                ; preds = %4
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100 x [100 x i32]], ptr @a, i64 0, i64 %11
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [100 x i32], ptr %12, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %30

18:                                               ; preds = %9
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %30, label %24

24:                                               ; preds = %18
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = load i32, ptr @r, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr @r, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %28
  store i32 %25, ptr %29, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %24, %18, %9
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100 x [100 x i32]], ptr @a, i64 0, i64 %32
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100 x i32], ptr %33, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %60

39:                                               ; preds = %30
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %60, label %45

45:                                               ; preds = %39
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [100 x i32], ptr @level, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %60, label %51

51:                                               ; preds = %45
  %52 = load i32, ptr %2, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [100 x i32], ptr @level, i64 0, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [100 x i32], ptr @level, i64 0, i64 %58
  store i32 %56, ptr %59, align 4, !tbaa !5
  br label %60

60:                                               ; preds = %51, %45, %39, %30
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %3, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %3, align 4, !tbaa !5
  br label %4, !llvm.loop !9

64:                                               ; preds = %8
  %65 = load i32, ptr @f, align 4, !tbaa !5
  %66 = load i32, ptr @r, align 4, !tbaa !5
  %67 = icmp sle i32 %65, %66
  br i1 %67, label %68, label %80

68:                                               ; preds = %64
  %69 = load i32, ptr @f, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %70
  %72 = load i32, ptr %71, align 4, !tbaa !5
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %73
  store i32 1, ptr %74, align 4, !tbaa !5
  %75 = load i32, ptr @f, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr @f, align 4, !tbaa !5
  %77 = sext i32 %75 to i64
  %78 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %77
  %79 = load i32, ptr %78, align 4, !tbaa !5
  call void @bfs(i32 noundef %79)
  br label %80

80:                                               ; preds = %68, %64
  ret void
}

; 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 = { 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 #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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@f = external dso_local local_unnamed_addr global i32, align 4
@r = external dso_local local_unnamed_addr global i32, align 4
@n = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global [100 x [100 x i32]], align 16
@visited = external dso_local local_unnamed_addr global [100 x i32], align 16
@q = external dso_local local_unnamed_addr global [100 x i32], align 16
@level = external dso_local local_unnamed_addr global [100 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @bfs(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not19 = icmp slt i32 %2, 1
  %r.promoted22 = load i32, ptr @r, align 4, !tbaa !5
  %f.promoted = load i32, ptr @f, align 4, !tbaa !5
  br i1 %.not19, label %.split.us, label %tailrecurse.preheader

tailrecurse.preheader:                            ; preds = %1
  %3 = add nuw i32 %2, 1
  %4 = sext i32 %f.promoted to i64
  %wide.trip.count = zext i32 %3 to i64
  br label %tailrecurse

.split.us:                                        ; preds = %1
  %.not13.us30 = icmp sgt i32 %f.promoted, %r.promoted22
  br i1 %.not13.us30, label %.split29.us, label %tailrecurse.us.preheader

tailrecurse.us.preheader:                         ; preds = %.split.us
  %5 = sext i32 %f.promoted to i64
  %6 = add i32 %r.promoted22, 1
  %7 = sub i32 %6, %f.promoted
  %8 = sub i32 %r.promoted22, %f.promoted
  %xtraiter = and i32 %7, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %tailrecurse.us.prol.loopexit, label %tailrecurse.us.prol

tailrecurse.us.prol:                              ; preds = %tailrecurse.us.preheader, %tailrecurse.us.prol
  %indvars.iv35.prol = phi i64 [ %indvars.iv.next36.prol, %tailrecurse.us.prol ], [ %5, %tailrecurse.us.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %tailrecurse.us.prol ], [ 0, %tailrecurse.us.preheader ]
  %9 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %indvars.iv35.prol
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %11
  store i32 1, ptr %12, align 4, !tbaa !5
  %indvars.iv.next36.prol = add nsw i64 %indvars.iv35.prol, 1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %tailrecurse.us.prol.loopexit, label %tailrecurse.us.prol, !llvm.loop !9

tailrecurse.us.prol.loopexit:                     ; preds = %tailrecurse.us.prol, %tailrecurse.us.preheader
  %indvars.iv35.unr = phi i64 [ %5, %tailrecurse.us.preheader ], [ %indvars.iv.next36.prol, %tailrecurse.us.prol ]
  %13 = icmp ult i32 %8, 3
  br i1 %13, label %tailrecurse.us..split29.us_crit_edge, label %tailrecurse.us

tailrecurse.us:                                   ; preds = %tailrecurse.us.prol.loopexit, %tailrecurse.us
  %indvars.iv35 = phi i64 [ %indvars.iv.next36.3, %tailrecurse.us ], [ %indvars.iv35.unr, %tailrecurse.us.prol.loopexit ]
  %14 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %indvars.iv35
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %16
  store i32 1, ptr %17, align 4, !tbaa !5
  %indvars.iv.next36 = add nsw i64 %indvars.iv35, 1
  %18 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %indvars.iv.next36
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %20
  store i32 1, ptr %21, align 4, !tbaa !5
  %indvars.iv.next36.1 = add nsw i64 %indvars.iv35, 2
  %22 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %indvars.iv.next36.1
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %24
  store i32 1, ptr %25, align 4, !tbaa !5
  %indvars.iv.next36.2 = add nsw i64 %indvars.iv35, 3
  %26 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %indvars.iv.next36.2
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %28
  store i32 1, ptr %29, align 4, !tbaa !5
  %indvars.iv.next36.3 = add nsw i64 %indvars.iv35, 4
  %lftr.wideiv.3 = trunc i64 %indvars.iv.next36.3 to i32
  %exitcond38.not.3 = icmp eq i32 %6, %lftr.wideiv.3
  br i1 %exitcond38.not.3, label %tailrecurse.us..split29.us_crit_edge, label %tailrecurse.us

tailrecurse.us..split29.us_crit_edge:             ; preds = %tailrecurse.us, %tailrecurse.us.prol.loopexit
  store i32 %6, ptr @f, align 4, !tbaa !5
  br label %.split29.us

tailrecurse:                                      ; preds = %tailrecurse.preheader, %52
  %indvars.iv32 = phi i64 [ %4, %tailrecurse.preheader ], [ %indvars.iv.next33, %52 ]
  %r.promoted27 = phi i32 [ %r.promoted22, %tailrecurse.preheader ], [ %r.promoted24, %52 ]
  %.tr = phi i32 [ %0, %tailrecurse.preheader ], [ %54, %52 ]
  %30 = sext i32 %.tr to i64
  %31 = getelementptr inbounds [100 x i32], ptr @level, i64 0, i64 %30
  br label %33

._crit_edge:                                      ; preds = %50
  %32 = sext i32 %r.promoted24 to i64
  %.not13 = icmp sgt i64 %indvars.iv32, %32
  br i1 %.not13, label %.split29.us, label %52

33:                                               ; preds = %tailrecurse, %50
  %indvars.iv = phi i64 [ 1, %tailrecurse ], [ %indvars.iv.next, %50 ]
  %r.promoted25 = phi i32 [ %r.promoted27, %tailrecurse ], [ %r.promoted24, %50 ]
  %34 = phi i32 [ %r.promoted27, %tailrecurse ], [ %51, %50 ]
  %35 = getelementptr inbounds [100 x [100 x i32]], ptr @a, i64 0, i64 %30, i64 %indvars.iv
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %.not14 = icmp eq i32 %36, 0
  br i1 %.not14, label %50, label %37

37:                                               ; preds = %33
  %38 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %indvars.iv
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %.not15 = icmp eq i32 %39, 0
  br i1 %.not15, label %40, label %50

40:                                               ; preds = %37
  %41 = add nsw i32 %34, 1
  store i32 %41, ptr @r, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %42
  %44 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %44, ptr %43, align 4, !tbaa !5
  %45 = getelementptr inbounds [100 x i32], ptr @level, i64 0, i64 %indvars.iv
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %.not18 = icmp eq i32 %46, 0
  br i1 %.not18, label %47, label %50

47:                                               ; preds = %40
  %48 = load i32, ptr %31, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %45, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %37, %33, %40, %47
  %r.promoted24 = phi i32 [ %r.promoted25, %33 ], [ %41, %40 ], [ %41, %47 ], [ %r.promoted25, %37 ]
  %51 = phi i32 [ %34, %33 ], [ %41, %40 ], [ %41, %47 ], [ %34, %37 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %33, !llvm.loop !11

52:                                               ; preds = %._crit_edge
  %53 = getelementptr inbounds [100 x i32], ptr @q, i64 0, i64 %indvars.iv32
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [100 x i32], ptr @visited, i64 0, i64 %55
  store i32 1, ptr %56, align 4, !tbaa !5
  %indvars.iv.next33 = add nsw i64 %indvars.iv32, 1
  %57 = trunc nsw i64 %indvars.iv.next33 to i32
  store i32 %57, ptr @f, align 4, !tbaa !5
  br label %tailrecurse

.split29.us:                                      ; preds = %._crit_edge, %.split.us, %tailrecurse.us..split29.us_crit_edge
  ret void
}

attributes #0 = { 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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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