; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 17. Graph (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"

@a = external dso_local global [6 x [6 x i32]], align 16
@state = external dso_local global [6 x i32], align 16
@.str.4 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare i32 @printf(ptr noundef, ...) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @bfs(i32 noundef %0) #2 {
  %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) #3
  %4 = load i32, ptr %2, align 4, !tbaa !5
  call void @insert_q(i32 noundef %4)
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [6 x i32], ptr @state, i64 0, i64 %6
  store i32 2, ptr %7, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %41, %1
  %9 = call i32 @isEmpty_queue()
  %10 = icmp ne i32 %9, 0
  %11 = xor i1 %10, true
  br i1 %11, label %12, label %50

12:                                               ; preds = %8
  %13 = call i32 @pop_queue()
  store i32 %13, ptr %2, align 4, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %38, %12
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 6
  br i1 %16, label %17, label %41

17:                                               ; preds = %14
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [6 x [6 x i32]], ptr @a, i64 0, i64 %19
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [6 x i32], ptr %20, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = icmp eq i32 %24, 1
  br i1 %25, label %26, label %37

26:                                               ; preds = %17
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [6 x i32], ptr @state, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = icmp eq i32 %30, 1
  br i1 %31, label %32, label %37

32:                                               ; preds = %26
  %33 = load i32, ptr %3, align 4, !tbaa !5
  call void @insert_q(i32 noundef %33)
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [6 x i32], ptr @state, i64 0, i64 %35
  store i32 2, ptr %36, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %32, %26, %17
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %3, align 4, !tbaa !5
  br label %14, !llvm.loop !9

41:                                               ; preds = %14
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [6 x i32], ptr @state, i64 0, i64 %43
  store i32 3, ptr %44, align 4, !tbaa !5
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = add nsw i32 %45, 65
  %47 = trunc i32 %46 to i8
  %48 = sext i8 %47 to i32
  %49 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i32 noundef %48)
  br label %8, !llvm.loop !11

50:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @insert_q(i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @pop_queue() #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @isEmpty_queue() #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { 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"}
!11 = distinct !{!11, !10}


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

@a = external dso_local local_unnamed_addr global [6 x [6 x i32]], align 16
@state = external dso_local local_unnamed_addr global [6 x i32], align 16
@.str.4 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @bfs(i32 noundef %0) local_unnamed_addr #1 {
  tail call void @insert_q(i32 noundef %0)
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [6 x i32], ptr @state, i64 0, i64 %2
  store i32 2, ptr %3, align 4, !tbaa !5
  %4 = tail call i32 @isEmpty_queue()
  %.not12 = icmp eq i32 %4, 0
  br i1 %.not12, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1, %48
  %5 = tail call i32 @pop_queue()
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [6 x [6 x i32]], ptr @a, i64 0, i64 %6, i64 0
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %9 = icmp eq i32 %8, 1
  %10 = load i32, ptr @state, align 16
  %11 = icmp eq i32 %10, 1
  %or.cond = select i1 %9, i1 %11, i1 false
  br i1 %or.cond, label %12, label %13

12:                                               ; preds = %.lr.ph
  tail call void @insert_q(i32 noundef 0)
  store i32 2, ptr @state, align 16, !tbaa !5
  br label %13

13:                                               ; preds = %.lr.ph, %12
  %14 = getelementptr inbounds [6 x [6 x i32]], ptr @a, i64 0, i64 %6, i64 1
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp eq i32 %15, 1
  %17 = load i32, ptr getelementptr inbounds (i8, ptr @state, i64 4), align 4
  %18 = icmp eq i32 %17, 1
  %or.cond16 = select i1 %16, i1 %18, i1 false
  br i1 %or.cond16, label %19, label %20

19:                                               ; preds = %13
  tail call void @insert_q(i32 noundef 1)
  store i32 2, ptr getelementptr inbounds (i8, ptr @state, i64 4), align 4, !tbaa !5
  br label %20

20:                                               ; preds = %19, %13
  %21 = getelementptr inbounds [6 x [6 x i32]], ptr @a, i64 0, i64 %6, i64 2
  %22 = load i32, ptr %21, align 8, !tbaa !5
  %23 = icmp eq i32 %22, 1
  %24 = load i32, ptr getelementptr inbounds (i8, ptr @state, i64 8), align 8
  %25 = icmp eq i32 %24, 1
  %or.cond18 = select i1 %23, i1 %25, i1 false
  br i1 %or.cond18, label %26, label %27

26:                                               ; preds = %20
  tail call void @insert_q(i32 noundef 2)
  store i32 2, ptr getelementptr inbounds (i8, ptr @state, i64 8), align 8, !tbaa !5
  br label %27

27:                                               ; preds = %26, %20
  %28 = getelementptr inbounds [6 x [6 x i32]], ptr @a, i64 0, i64 %6, i64 3
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp eq i32 %29, 1
  %31 = load i32, ptr getelementptr inbounds (i8, ptr @state, i64 12), align 4
  %32 = icmp eq i32 %31, 1
  %or.cond20 = select i1 %30, i1 %32, i1 false
  br i1 %or.cond20, label %33, label %34

33:                                               ; preds = %27
  tail call void @insert_q(i32 noundef 3)
  store i32 2, ptr getelementptr inbounds (i8, ptr @state, i64 12), align 4, !tbaa !5
  br label %34

34:                                               ; preds = %33, %27
  %35 = getelementptr inbounds [6 x [6 x i32]], ptr @a, i64 0, i64 %6, i64 4
  %36 = load i32, ptr %35, align 8, !tbaa !5
  %37 = icmp eq i32 %36, 1
  %38 = load i32, ptr getelementptr inbounds (i8, ptr @state, i64 16), align 16
  %39 = icmp eq i32 %38, 1
  %or.cond22 = select i1 %37, i1 %39, i1 false
  br i1 %or.cond22, label %40, label %41

40:                                               ; preds = %34
  tail call void @insert_q(i32 noundef 4)
  store i32 2, ptr getelementptr inbounds (i8, ptr @state, i64 16), align 16, !tbaa !5
  br label %41

41:                                               ; preds = %40, %34
  %42 = getelementptr inbounds [6 x [6 x i32]], ptr @a, i64 0, i64 %6, i64 5
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = icmp eq i32 %43, 1
  %45 = load i32, ptr getelementptr inbounds (i8, ptr @state, i64 20), align 4
  %46 = icmp eq i32 %45, 1
  %or.cond24 = select i1 %44, i1 %46, i1 false
  br i1 %or.cond24, label %47, label %48

47:                                               ; preds = %41
  tail call void @insert_q(i32 noundef 5)
  store i32 2, ptr getelementptr inbounds (i8, ptr @state, i64 20), align 4, !tbaa !5
  br label %48

48:                                               ; preds = %47, %41
  %49 = getelementptr inbounds [6 x i32], ptr @state, i64 0, i64 %6
  store i32 3, ptr %49, align 4, !tbaa !5
  %50 = shl i32 %5, 24
  %sext = add i32 %50, 1090519040
  %51 = ashr exact i32 %sext, 24
  %52 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %51)
  %53 = tail call i32 @isEmpty_queue()
  %.not = icmp eq i32 %53, 0
  br i1 %.not, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %48, %1
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local void @insert_q(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @pop_queue() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @isEmpty_queue() local_unnamed_addr #1

attributes #0 = { nofree nounwind "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 = { 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"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass