; LLVM IR from div-bargali/Data-Structures-and-Algorithms :: C/Algorithms/Graph-Algorithms (Breadth_first_search)

; 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
@i = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@a = external dso_local global [20 x [20 x i32]], align 16
@visited = external dso_local global [20 x i32], align 16
@q = external dso_local global [20 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local void @bfs(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  store i32 1, ptr @i, align 4, !tbaa !5
  br label %3

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

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

16:                                               ; preds = %7
  %17 = load i32, ptr @i, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [20 x i32], ptr @visited, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %28, label %22

22:                                               ; preds = %16
  %23 = load i32, ptr @i, align 4, !tbaa !5
  %24 = load i32, ptr @r, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr @r, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [20 x i32], ptr @q, i64 0, i64 %26
  store i32 %23, ptr %27, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %22, %16, %7
  br label %29

29:                                               ; preds = %28
  %30 = load i32, ptr @i, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr @i, align 4, !tbaa !5
  br label %3, !llvm.loop !9

32:                                               ; preds = %3
  %33 = load i32, ptr @f, align 4, !tbaa !5
  %34 = load i32, ptr @r, align 4, !tbaa !5
  %35 = icmp sle i32 %33, %34
  br i1 %35, label %36, label %48

36:                                               ; preds = %32
  %37 = load i32, ptr @f, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [20 x i32], ptr @q, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [20 x i32], ptr @visited, i64 0, i64 %41
  store i32 1, ptr %42, align 4, !tbaa !5
  %43 = load i32, ptr @f, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr @f, align 4, !tbaa !5
  %45 = sext i32 %43 to i64
  %46 = getelementptr inbounds [20 x i32], ptr @q, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  call void @bfs(i32 noundef %47)
  br label %48

48:                                               ; preds = %36, %32
  ret void
}

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

!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
@i = 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 [20 x [20 x i32]], align 16
@visited = external dso_local local_unnamed_addr global [20 x i32], align 16
@q = external dso_local local_unnamed_addr global [20 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
  %.not5 = icmp slt i32 %2, 1
  %r.promoted7 = load i32, ptr @r, align 4, !tbaa !5
  %f.promoted = load i32, ptr @f, align 4, !tbaa !5
  br i1 %.not5, 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
  %.not2.us14 = icmp sgt i32 %f.promoted, %r.promoted7
  br i1 %.not2.us14, label %.split13.us, label %tailrecurse.us.preheader

tailrecurse.us.preheader:                         ; preds = %.split.us
  %5 = sext i32 %f.promoted to i64
  %6 = add i32 %r.promoted7, 1
  %7 = sub i32 %6, %f.promoted
  %8 = sub i32 %r.promoted7, %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.iv21.prol = phi i64 [ %indvars.iv.next22.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 [20 x i32], ptr @q, i64 0, i64 %indvars.iv21.prol
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [20 x i32], ptr @visited, i64 0, i64 %11
  store i32 1, ptr %12, align 4, !tbaa !5
  %indvars.iv.next22.prol = add nsw i64 %indvars.iv21.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.iv21.unr = phi i64 [ %5, %tailrecurse.us.preheader ], [ %indvars.iv.next22.prol, %tailrecurse.us.prol ]
  %13 = icmp ult i32 %8, 3
  br i1 %13, label %tailrecurse.us..split13.us_crit_edge, label %tailrecurse.us

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

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

tailrecurse:                                      ; preds = %tailrecurse.preheader, %46
  %indvars.iv18 = phi i64 [ %4, %tailrecurse.preheader ], [ %indvars.iv.next19, %46 ]
  %r.promoted11 = phi i32 [ %r.promoted7, %tailrecurse.preheader ], [ %r.promoted8, %46 ]
  %.tr = phi i32 [ %0, %tailrecurse.preheader ], [ %48, %46 ]
  %30 = sext i32 %.tr to i64
  br label %31

31:                                               ; preds = %tailrecurse, %43
  %indvars.iv = phi i64 [ 1, %tailrecurse ], [ %indvars.iv.next, %43 ]
  %r.promoted9 = phi i32 [ %r.promoted11, %tailrecurse ], [ %r.promoted8, %43 ]
  %32 = phi i32 [ %r.promoted11, %tailrecurse ], [ %44, %43 ]
  %33 = getelementptr inbounds [20 x [20 x i32]], ptr @a, i64 0, i64 %30, i64 %indvars.iv
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %.not3 = icmp eq i32 %34, 0
  br i1 %.not3, label %43, label %35

35:                                               ; preds = %31
  %36 = getelementptr inbounds [20 x i32], ptr @visited, i64 0, i64 %indvars.iv
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %.not4 = icmp eq i32 %37, 0
  br i1 %.not4, label %38, label %43

38:                                               ; preds = %35
  %39 = add nsw i32 %32, 1
  store i32 %39, ptr @r, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [20 x i32], ptr @q, i64 0, i64 %40
  %42 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %42, ptr %41, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %31, %35, %38
  %r.promoted8 = phi i32 [ %r.promoted9, %31 ], [ %r.promoted9, %35 ], [ %39, %38 ]
  %44 = phi i32 [ %32, %31 ], [ %32, %35 ], [ %39, %38 ]
  %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 %31, !llvm.loop !11

._crit_edge:                                      ; preds = %43
  %45 = sext i32 %r.promoted8 to i64
  %.not2 = icmp sgt i64 %indvars.iv18, %45
  br i1 %.not2, label %.split13.us, label %46

46:                                               ; preds = %._crit_edge
  %47 = getelementptr inbounds [20 x i32], ptr @q, i64 0, i64 %indvars.iv18
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [20 x i32], ptr @visited, i64 0, i64 %49
  store i32 1, ptr %50, align 4, !tbaa !5
  %indvars.iv.next19 = add nsw i64 %indvars.iv18, 1
  %51 = trunc nsw i64 %indvars.iv.next19 to i32
  store i32 %51, ptr @f, align 4, !tbaa !5
  br label %tailrecurse

.split13.us:                                      ; preds = %._crit_edge, %.split.us, %tailrecurse.us..split13.us_crit_edge
  %.us-phi = phi i32 [ 1, %tailrecurse.us..split13.us_crit_edge ], [ 1, %.split.us ], [ %3, %._crit_edge ]
  store i32 %.us-phi, ptr @i, align 4, !tbaa !5
  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,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass