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

; 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.Graph = type { i32, ptr, ptr }

; Function Attrs: nounwind uwtable
declare dso_local void @bfs(ptr noundef, i32 noundef) #0

; 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

; Function Attrs: nounwind uwtable
declare dso_local ptr @createGraph(i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @addEdge(ptr noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @printGraph(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  %4 = call ptr @createGraph(i32 noundef 9)
  store ptr %4, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %5, i32 noundef 0, i32 noundef 8)
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %6, i32 noundef 0, i32 noundef 3)
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %7, i32 noundef 0, i32 noundef 1)
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %8, i32 noundef 4, i32 noundef 8)
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %9, i32 noundef 3, i32 noundef 4)
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %10, i32 noundef 3, i32 noundef 2)
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %11, i32 noundef 1, i32 noundef 7)
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %12, i32 noundef 2, i32 noundef 7)
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %13, i32 noundef 2, i32 noundef 5)
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  call void @addEdge(ptr noundef %14, i32 noundef 5, i32 noundef 6)
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  call void @printGraph(ptr noundef %15)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %33, %0
  %17 = load i32, ptr %3, align 4, !tbaa !9
  %18 = icmp slt i32 %17, 9
  br i1 %18, label %20, label %19

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

20:                                               ; preds = %16
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Graph, ptr %21, i32 0, i32 2
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %23, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %32

29:                                               ; preds = %20
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = load i32, ptr %3, align 4, !tbaa !9
  call void @bfs(ptr noundef %30, i32 noundef %31)
  br label %32

32:                                               ; preds = %29, %20
  br label %33

33:                                               ; preds = %32
  %34 = load i32, ptr %3, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %3, align 4, !tbaa !9
  br label %16, !llvm.loop !13

36:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  ret i32 0
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"Graph", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local void @bfs(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @createGraph(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @addEdge(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @printGraph(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call ptr @createGraph(i32 noundef 9)
  tail call void @addEdge(ptr noundef %1, i32 noundef 0, i32 noundef 8)
  tail call void @addEdge(ptr noundef %1, i32 noundef 0, i32 noundef 3)
  tail call void @addEdge(ptr noundef %1, i32 noundef 0, i32 noundef 1)
  tail call void @addEdge(ptr noundef %1, i32 noundef 4, i32 noundef 8)
  tail call void @addEdge(ptr noundef %1, i32 noundef 3, i32 noundef 4)
  tail call void @addEdge(ptr noundef %1, i32 noundef 3, i32 noundef 2)
  tail call void @addEdge(ptr noundef %1, i32 noundef 1, i32 noundef 7)
  tail call void @addEdge(ptr noundef %1, i32 noundef 2, i32 noundef 7)
  tail call void @addEdge(ptr noundef %1, i32 noundef 2, i32 noundef 5)
  tail call void @addEdge(ptr noundef %1, i32 noundef 5, i32 noundef 6)
  tail call void @printGraph(ptr noundef %1)
  %2 = getelementptr inbounds i8, ptr %1, i64 16
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = load i32, ptr %3, align 4, !tbaa !11
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %7

6:                                                ; preds = %0
  tail call void @bfs(ptr noundef nonnull %1, i32 noundef 0)
  %.pre = load ptr, ptr %2, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %0, %6
  %8 = phi ptr [ %3, %0 ], [ %.pre, %6 ]
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %7
  tail call void @bfs(ptr noundef nonnull %1, i32 noundef 1)
  %.pre18 = load ptr, ptr %2, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %12, %7
  %14 = phi ptr [ %.pre18, %12 ], [ %8, %7 ]
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %13
  tail call void @bfs(ptr noundef nonnull %1, i32 noundef 2)
  %.pre19 = load ptr, ptr %2, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %18, %13
  %20 = phi ptr [ %.pre19, %18 ], [ %14, %13 ]
  %21 = getelementptr inbounds i8, ptr %20, i64 12
  %22 = load i32, ptr %21, align 4, !tbaa !11
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %19
  tail call void @bfs(ptr noundef nonnull %1, i32 noundef 3)
  %.pre20 = load ptr, ptr %2, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %24, %19
  %26 = phi ptr [ %.pre20, %24 ], [ %20, %19 ]
  %27 = getelementptr inbounds i8, ptr %26, i64 16
  %28 = load i32, ptr %27, align 4, !tbaa !11
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %25
  tail call void @bfs(ptr noundef nonnull %1, i32 noundef 4)
  %.pre21 = load ptr, ptr %2, align 8, !tbaa !5
  br label %31

31:                                               ; preds = %30, %25
  %32 = phi ptr [ %.pre21, %30 ], [ %26, %25 ]
  %33 = getelementptr inbounds i8, ptr %32, i64 20
  %34 = load i32, ptr %33, align 4, !tbaa !11
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %31
  tail call void @bfs(ptr noundef nonnull %1, i32 noundef 5)
  %.pre22 = load ptr, ptr %2, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %36, %31
  %38 = phi ptr [ %.pre22, %36 ], [ %32, %31 ]
  %39 = getelementptr inbounds i8, ptr %38, i64 24
  %40 = load i32, ptr %39, align 4, !tbaa !11
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %43

42:                                               ; preds = %37
  tail call void @bfs(ptr noundef nonnull %1, i32 noundef 6)
  %.pre23 = load ptr, ptr %2, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %42, %37
  %44 = phi ptr [ %.pre23, %42 ], [ %38, %37 ]
  %45 = getelementptr inbounds i8, ptr %44, i64 28
  %46 = load i32, ptr %45, align 4, !tbaa !11
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %49

48:                                               ; preds = %43
  tail call void @bfs(ptr noundef nonnull %1, i32 noundef 7)
  %.pre24 = load ptr, ptr %2, align 8, !tbaa !5
  br label %49

49:                                               ; preds = %48, %43
  %50 = phi ptr [ %.pre24, %48 ], [ %44, %43 ]
  %51 = getelementptr inbounds i8, ptr %50, i64 32
  %52 = load i32, ptr %51, align 4, !tbaa !11
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %55

54:                                               ; preds = %49
  tail call void @bfs(ptr noundef nonnull %1, i32 noundef 8)
  br label %55

55:                                               ; preds = %54, %49
  ret i32 0
}

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, !10, i64 16}
!6 = !{!"Graph", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass