; LLVM IR from AlgoGenesis/C :: Graph Algorithms/Island Counting Algorithm (Program)

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @countIslandsBFS(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca [5 x [5 x i32]], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 100, ptr %3) #3
  call void @llvm.memset.p0.i64(ptr align 16 %3, i8 0, i64 100, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %48, %1
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp slt i32 %9, 5
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %51

12:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %44, %12
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = icmp slt i32 %14, 5
  br i1 %15, label %17, label %16

16:                                               ; preds = %13
  store i32 5, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %47

17:                                               ; preds = %13
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [5 x i32], ptr %18, i64 %20
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [5 x i32], ptr %21, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = icmp eq i32 %25, 1
  br i1 %26, label %27, label %43

27:                                               ; preds = %17
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [5 x [5 x i32]], ptr %3, i64 0, i64 %29
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [5 x i32], ptr %30, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %43, label %36

36:                                               ; preds = %27
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds [5 x [5 x i32]], ptr %3, i64 0, i64 0
  %39 = load i32, ptr %5, align 4, !tbaa !9
  %40 = load i32, ptr %7, align 4, !tbaa !9
  call void @bfs(ptr noundef %37, ptr noundef %38, i32 noundef %39, i32 noundef %40)
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %4, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %36, %27, %17
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %7, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %7, align 4, !tbaa !9
  br label %13, !llvm.loop !11

47:                                               ; preds = %16
  br label %48

48:                                               ; preds = %47
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %5, align 4, !tbaa !9
  br label %8, !llvm.loop !13

51:                                               ; preds = %11
  %52 = load i32, ptr %4, align 4, !tbaa !9
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 100, ptr %3) #3
  ret i32 %52
}

; 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
declare dso_local void @bfs(ptr noundef, ptr noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @countIslandsBFS(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca [5 x [5 x i32]], align 16
  call void @llvm.lifetime.start.p0(i64 100, ptr nonnull %2) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(100) %2, i8 0, i64 100, i1 false)
  br label %.preheader

.preheader:                                       ; preds = %1, %53
  %indvars.iv = phi i64 [ 0, %1 ], [ %indvars.iv.next, %53 ]
  %.01417 = phi i32 [ 0, %1 ], [ %.2.4, %53 ]
  %3 = getelementptr inbounds [5 x i32], ptr %0, i64 %indvars.iv, i64 0
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 1
  br i1 %5, label %7, label %13

6:                                                ; preds = %53
  call void @llvm.lifetime.end.p0(i64 100, ptr nonnull %2) #3
  ret i32 %.2.4

7:                                                ; preds = %.preheader
  %8 = getelementptr inbounds [5 x [5 x i32]], ptr %2, i64 0, i64 %indvars.iv, i64 0
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %10, label %13

10:                                               ; preds = %7
  %11 = trunc nuw nsw i64 %indvars.iv to i32
  call void @bfs(ptr noundef nonnull %0, ptr noundef nonnull %2, i32 noundef %11, i32 noundef 0)
  %12 = add nsw i32 %.01417, 1
  br label %13

13:                                               ; preds = %.preheader, %7, %10
  %.2 = phi i32 [ %.01417, %7 ], [ %12, %10 ], [ %.01417, %.preheader ]
  %14 = getelementptr inbounds [5 x i32], ptr %0, i64 %indvars.iv, i64 1
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp eq i32 %15, 1
  br i1 %16, label %17, label %23

17:                                               ; preds = %13
  %18 = getelementptr inbounds [5 x [5 x i32]], ptr %2, i64 0, i64 %indvars.iv, i64 1
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %.not.1 = icmp eq i32 %19, 0
  br i1 %.not.1, label %20, label %23

20:                                               ; preds = %17
  %21 = trunc nuw nsw i64 %indvars.iv to i32
  call void @bfs(ptr noundef nonnull %0, ptr noundef nonnull %2, i32 noundef %21, i32 noundef 1)
  %22 = add nsw i32 %.2, 1
  br label %23

23:                                               ; preds = %20, %17, %13
  %.2.1 = phi i32 [ %.2, %17 ], [ %22, %20 ], [ %.2, %13 ]
  %24 = getelementptr inbounds [5 x i32], ptr %0, i64 %indvars.iv, i64 2
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp eq i32 %25, 1
  br i1 %26, label %27, label %33

27:                                               ; preds = %23
  %28 = getelementptr inbounds [5 x [5 x i32]], ptr %2, i64 0, i64 %indvars.iv, i64 2
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %.not.2 = icmp eq i32 %29, 0
  br i1 %.not.2, label %30, label %33

30:                                               ; preds = %27
  %31 = trunc nuw nsw i64 %indvars.iv to i32
  call void @bfs(ptr noundef nonnull %0, ptr noundef nonnull %2, i32 noundef %31, i32 noundef 2)
  %32 = add nsw i32 %.2.1, 1
  br label %33

33:                                               ; preds = %30, %27, %23
  %.2.2 = phi i32 [ %.2.1, %27 ], [ %32, %30 ], [ %.2.1, %23 ]
  %34 = getelementptr inbounds [5 x i32], ptr %0, i64 %indvars.iv, i64 3
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = icmp eq i32 %35, 1
  br i1 %36, label %37, label %43

37:                                               ; preds = %33
  %38 = getelementptr inbounds [5 x [5 x i32]], ptr %2, i64 0, i64 %indvars.iv, i64 3
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %.not.3 = icmp eq i32 %39, 0
  br i1 %.not.3, label %40, label %43

40:                                               ; preds = %37
  %41 = trunc nuw nsw i64 %indvars.iv to i32
  call void @bfs(ptr noundef nonnull %0, ptr noundef nonnull %2, i32 noundef %41, i32 noundef 3)
  %42 = add nsw i32 %.2.2, 1
  br label %43

43:                                               ; preds = %40, %37, %33
  %.2.3 = phi i32 [ %.2.2, %37 ], [ %42, %40 ], [ %.2.2, %33 ]
  %44 = getelementptr inbounds [5 x i32], ptr %0, i64 %indvars.iv, i64 4
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = icmp eq i32 %45, 1
  br i1 %46, label %47, label %53

47:                                               ; preds = %43
  %48 = getelementptr inbounds [5 x [5 x i32]], ptr %2, i64 0, i64 %indvars.iv, i64 4
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %.not.4 = icmp eq i32 %49, 0
  br i1 %.not.4, label %50, label %53

50:                                               ; preds = %47
  %51 = trunc nuw nsw i64 %indvars.iv to i32
  call void @bfs(ptr noundef nonnull %0, ptr noundef nonnull %2, i32 noundef %51, i32 noundef 4)
  %52 = add nsw i32 %.2.3, 1
  br label %53

53:                                               ; preds = %50, %47, %43
  %.2.4 = phi i32 [ %.2.3, %47 ], [ %52, %50 ], [ %.2.3, %43 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 5
  br i1 %exitcond.not, label %6, label %.preheader, !llvm.loop !9
}

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass