; LLVM IR from brpapa/algorithms :: notebook/graphs/flood-fill.ll (Z4walkiicc)

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

@grid = external dso_local global [9 x [9 x i8]], align 16
@dr = external dso_local global [8 x i32], align 16
@dc = external dso_local global [8 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4walkiicc(i32 noundef %0, i32 noundef %1, i8 noundef signext %2, i8 noundef signext %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i8 %2, ptr %8, align 1, !tbaa !9
  store i8 %3, ptr %9, align 1, !tbaa !9
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %25, label %16

16:                                               ; preds = %4
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = icmp eq i32 %17, 9
  br i1 %18, label %25, label %19

19:                                               ; preds = %16
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = icmp slt i32 %20, 0
  br i1 %21, label %25, label %22

22:                                               ; preds = %19
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = icmp eq i32 %23, 9
  br i1 %24, label %25, label %26

25:                                               ; preds = %22, %19, %16, %4
  store i32 0, ptr %5, align 4
  br label %76

26:                                               ; preds = %22
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [9 x [9 x i8]], ptr @grid, i64 0, i64 %28
  %30 = load i32, ptr %7, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [9 x i8], ptr %29, i64 0, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !9
  %34 = sext i8 %33 to i32
  %35 = load i8, ptr %8, align 1, !tbaa !9
  %36 = sext i8 %35 to i32
  %37 = icmp ne i32 %34, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %26
  store i32 0, ptr %5, align 4
  br label %76

39:                                               ; preds = %26
  %40 = load i8, ptr %9, align 1, !tbaa !9
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [9 x [9 x i8]], ptr @grid, i64 0, i64 %42
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [9 x i8], ptr %43, i64 0, i64 %45
  store i8 %40, ptr %46, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 1, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %71, %39
  %48 = load i32, ptr %11, align 4, !tbaa !5
  %49 = icmp slt i32 %48, 8
  br i1 %49, label %51, label %50

50:                                               ; preds = %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %74

51:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = load i32, ptr %11, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [8 x i32], ptr @dr, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = add nsw i32 %52, %56
  store i32 %57, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = load i32, ptr %11, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [8 x i32], ptr @dc, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = add nsw i32 %58, %62
  store i32 %63, ptr %13, align 4, !tbaa !5
  %64 = load i32, ptr %12, align 4, !tbaa !5
  %65 = load i32, ptr %13, align 4, !tbaa !5
  %66 = load i8, ptr %8, align 1, !tbaa !9
  %67 = load i8, ptr %9, align 1, !tbaa !9
  %68 = call noundef i32 @_Z4walkiicc(i32 noundef %64, i32 noundef %65, i8 noundef signext %66, i8 noundef signext %67)
  %69 = load i32, ptr %10, align 4, !tbaa !5
  %70 = add nsw i32 %69, %68
  store i32 %70, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %71

71:                                               ; preds = %51
  %72 = load i32, ptr %11, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %11, align 4, !tbaa !5
  br label %47, !llvm.loop !10

74:                                               ; preds = %50
  %75 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %75, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %76

76:                                               ; preds = %74, %38, %25
  %77 = load i32, ptr %5, align 4
  ret i32 %77
}

; 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

attributes #0 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"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"

@grid = external dso_local local_unnamed_addr global [9 x [9 x i8]], align 16
@dr = external dso_local local_unnamed_addr global [8 x i32], align 16
@dc = external dso_local local_unnamed_addr global [8 x i32], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z4walkiicc(i32 noundef %0, i32 noundef %1, i8 noundef signext %2, i8 noundef signext %3) local_unnamed_addr #0 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %.loopexit.loopexit, %4
  %accumulator.tr = phi i32 [ 0, %4 ], [ %60, %.loopexit.loopexit ]
  %.tr = phi i32 [ %0, %4 ], [ %57, %.loopexit.loopexit ]
  %.tr35 = phi i32 [ %1, %4 ], [ %59, %.loopexit.loopexit ]
  %5 = icmp eq i32 %.tr, 9
  %6 = or i32 %.tr35, %.tr
  %7 = icmp slt i32 %6, 0
  %or.cond3 = or i1 %5, %7
  %8 = icmp eq i32 %.tr35, 9
  %or.cond5 = or i1 %8, %or.cond3
  br i1 %or.cond5, label %.loopexit, label %9

9:                                                ; preds = %tailrecurse
  %10 = zext nneg i32 %.tr to i64
  %11 = zext nneg i32 %.tr35 to i64
  %12 = getelementptr inbounds [9 x [9 x i8]], ptr @grid, i64 0, i64 %10, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !5
  %.not = icmp eq i8 %13, %2
  br i1 %.not, label %.loopexit.loopexit, label %.loopexit

.loopexit.loopexit:                               ; preds = %9
  store i8 %3, ptr %12, align 1, !tbaa !5
  %14 = load i32, ptr @dr, align 16, !tbaa !8
  %15 = add nsw i32 %14, %.tr
  %16 = load i32, ptr @dc, align 16, !tbaa !8
  %17 = add nsw i32 %16, %.tr35
  %18 = tail call noundef i32 @_Z4walkiicc(i32 noundef %15, i32 noundef %17, i8 noundef signext %2, i8 noundef signext %3)
  %19 = add nsw i32 %18, 1
  %20 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 4), align 4, !tbaa !8
  %21 = add nsw i32 %20, %.tr
  %22 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 4), align 4, !tbaa !8
  %23 = add nsw i32 %22, %.tr35
  %24 = tail call noundef i32 @_Z4walkiicc(i32 noundef %21, i32 noundef %23, i8 noundef signext %2, i8 noundef signext %3)
  %25 = add nsw i32 %24, %19
  %26 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 8), align 8, !tbaa !8
  %27 = add nsw i32 %26, %.tr
  %28 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 8), align 8, !tbaa !8
  %29 = add nsw i32 %28, %.tr35
  %30 = tail call noundef i32 @_Z4walkiicc(i32 noundef %27, i32 noundef %29, i8 noundef signext %2, i8 noundef signext %3)
  %31 = add nsw i32 %30, %25
  %32 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 12), align 4, !tbaa !8
  %33 = add nsw i32 %32, %.tr
  %34 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 12), align 4, !tbaa !8
  %35 = add nsw i32 %34, %.tr35
  %36 = tail call noundef i32 @_Z4walkiicc(i32 noundef %33, i32 noundef %35, i8 noundef signext %2, i8 noundef signext %3)
  %37 = add nsw i32 %36, %31
  %38 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 16), align 16, !tbaa !8
  %39 = add nsw i32 %38, %.tr
  %40 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 16), align 16, !tbaa !8
  %41 = add nsw i32 %40, %.tr35
  %42 = tail call noundef i32 @_Z4walkiicc(i32 noundef %39, i32 noundef %41, i8 noundef signext %2, i8 noundef signext %3)
  %43 = add nsw i32 %42, %37
  %44 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 20), align 4, !tbaa !8
  %45 = add nsw i32 %44, %.tr
  %46 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 20), align 4, !tbaa !8
  %47 = add nsw i32 %46, %.tr35
  %48 = tail call noundef i32 @_Z4walkiicc(i32 noundef %45, i32 noundef %47, i8 noundef signext %2, i8 noundef signext %3)
  %49 = add nsw i32 %48, %43
  %50 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 24), align 8, !tbaa !8
  %51 = add nsw i32 %50, %.tr
  %52 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 24), align 8, !tbaa !8
  %53 = add nsw i32 %52, %.tr35
  %54 = tail call noundef i32 @_Z4walkiicc(i32 noundef %51, i32 noundef %53, i8 noundef signext %2, i8 noundef signext %3)
  %55 = add nsw i32 %54, %49
  %56 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 28), align 4, !tbaa !8
  %57 = add nsw i32 %56, %.tr
  %58 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 28), align 4, !tbaa !8
  %59 = add nsw i32 %58, %.tr35
  %60 = add nsw i32 %accumulator.tr, %55
  br label %tailrecurse

.loopexit:                                        ; preds = %9, %tailrecurse
  %accumulator.ret.tr = add nsw i32 %accumulator.tr, 0
  ret i32 %accumulator.ret.tr
}

attributes #0 = { mustprogress nofree 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}


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