; LLVM IR from ncduy0303/Competitive-Programming :: Graphs/Graph Traversal/Flood Fill.ll (Z3dfsii)

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

@visited = external dso_local global [1001 x [1001 x i32]], align 16
@_ZL2di = external hidden constant [4 x i32], align 16
@_ZL2dj = external hidden constant [4 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z5validii(i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3dfsii(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [1001 x [1001 x i32]], ptr @visited, i64 0, i64 %9
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [1001 x i32], ptr %10, i64 0, i64 %12
  store i32 1, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %47, %2
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 4
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %50

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [4 x i32], ptr @_ZL2di, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = add nsw i32 %19, %23
  store i32 %24, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [4 x i32], ptr @_ZL2dj, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = add nsw i32 %25, %29
  store i32 %30, ptr %7, align 4, !tbaa !5
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = call noundef zeroext i1 @_Z5validii(i32 noundef %31, i32 noundef %32)
  br i1 %33, label %34, label %46

34:                                               ; preds = %18
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1001 x [1001 x i32]], ptr @visited, i64 0, i64 %36
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [1001 x i32], ptr %37, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %46, label %43

43:                                               ; preds = %34
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = load i32, ptr %7, align 4, !tbaa !5
  call void @_Z3dfsii(i32 noundef %44, i32 noundef %45)
  br label %46

46:                                               ; preds = %43, %34, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %47

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

50:                                               ; preds = %17
  ret void
}

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

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

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

@visited = external dso_local local_unnamed_addr global [1001 x [1001 x i32]], align 16
@_ZL2di = external hidden local_unnamed_addr constant [4 x i32], align 16
@_ZL2dj = external hidden local_unnamed_addr constant [4 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z5validii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3dfsii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %47, %2
  %.tr = phi i32 [ %0, %2 ], [ %43, %47 ]
  %.tr16 = phi i32 [ %1, %2 ], [ %45, %47 ]
  %3 = sext i32 %.tr to i64
  %4 = sext i32 %.tr16 to i64
  %5 = getelementptr inbounds [1001 x [1001 x i32]], ptr @visited, i64 0, i64 %3, i64 %4
  store i32 1, ptr %5, align 4, !tbaa !5
  %6 = load i32, ptr @_ZL2di, align 16, !tbaa !5
  %7 = add nsw i32 %6, %.tr
  %8 = load i32, ptr @_ZL2dj, align 16, !tbaa !5
  %9 = add nsw i32 %8, %.tr16
  %10 = tail call noundef zeroext i1 @_Z5validii(i32 noundef %7, i32 noundef %9)
  br i1 %10, label %11, label %17

11:                                               ; preds = %tailrecurse
  %12 = sext i32 %7 to i64
  %13 = sext i32 %9 to i64
  %14 = getelementptr inbounds [1001 x [1001 x i32]], ptr @visited, i64 0, i64 %12, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %16, label %17

16:                                               ; preds = %11
  tail call void @_Z3dfsii(i32 noundef %7, i32 noundef %9)
  br label %17

17:                                               ; preds = %16, %11, %tailrecurse
  %18 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2di, i64 4), align 4, !tbaa !5
  %19 = add nsw i32 %18, %.tr
  %20 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2dj, i64 4), align 4, !tbaa !5
  %21 = add nsw i32 %20, %.tr16
  %22 = tail call noundef zeroext i1 @_Z5validii(i32 noundef %19, i32 noundef %21)
  br i1 %22, label %23, label %29

23:                                               ; preds = %17
  %24 = sext i32 %19 to i64
  %25 = sext i32 %21 to i64
  %26 = getelementptr inbounds [1001 x [1001 x i32]], ptr @visited, i64 0, i64 %24, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %.not.1 = icmp eq i32 %27, 0
  br i1 %.not.1, label %28, label %29

28:                                               ; preds = %23
  tail call void @_Z3dfsii(i32 noundef %19, i32 noundef %21)
  br label %29

29:                                               ; preds = %28, %23, %17
  %30 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2di, i64 8), align 8, !tbaa !5
  %31 = add nsw i32 %30, %.tr
  %32 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2dj, i64 8), align 8, !tbaa !5
  %33 = add nsw i32 %32, %.tr16
  %34 = tail call noundef zeroext i1 @_Z5validii(i32 noundef %31, i32 noundef %33)
  br i1 %34, label %35, label %41

35:                                               ; preds = %29
  %36 = sext i32 %31 to i64
  %37 = sext i32 %33 to i64
  %38 = getelementptr inbounds [1001 x [1001 x i32]], ptr @visited, i64 0, i64 %36, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %.not.2 = icmp eq i32 %39, 0
  br i1 %.not.2, label %40, label %41

40:                                               ; preds = %35
  tail call void @_Z3dfsii(i32 noundef %31, i32 noundef %33)
  br label %41

41:                                               ; preds = %40, %35, %29
  %42 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2di, i64 12), align 4, !tbaa !5
  %43 = add nsw i32 %42, %.tr
  %44 = load i32, ptr getelementptr inbounds (i8, ptr @_ZL2dj, i64 12), align 4, !tbaa !5
  %45 = add nsw i32 %44, %.tr16
  %46 = tail call noundef zeroext i1 @_Z5validii(i32 noundef %43, i32 noundef %45)
  br i1 %46, label %47, label %52

47:                                               ; preds = %41
  %48 = sext i32 %43 to i64
  %49 = sext i32 %45 to i64
  %50 = getelementptr inbounds [1001 x [1001 x i32]], ptr @visited, i64 0, i64 %48, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %.not.3 = icmp eq i32 %51, 0
  br i1 %.not.3, label %tailrecurse, label %52

52:                                               ; preds = %47, %41
  ret void
}

attributes #0 = { mustprogress 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++ TBAA"}


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