; LLVM IR from brpapa/algorithms :: solutions/uva/11094.ll (Z9floodFilliicc)

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

@R = external dso_local global i32, align 4
@C = external dso_local global i32, align 4
@grid = external dso_local global [22 x [22 x i8]], align 16
@dr = external dso_local global [4 x i32], align 16
@dc = external dso_local global [4 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z9floodFilliicc(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
  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
  %12 = load i32, ptr %7, align 4, !tbaa !5
  %13 = icmp eq i32 %12, -1
  br i1 %13, label %14, label %17

14:                                               ; preds = %4
  %15 = load i32, ptr @C, align 4, !tbaa !5
  %16 = sub nsw i32 %15, 1
  store i32 %16, ptr %7, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %14, %4
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = load i32, ptr @C, align 4, !tbaa !5
  %20 = icmp eq i32 %18, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %17
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %21, %17
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %41, label %25

25:                                               ; preds = %22
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = load i32, ptr @R, align 4, !tbaa !5
  %28 = icmp eq i32 %26, %27
  br i1 %28, label %41, label %29

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

41:                                               ; preds = %29, %25, %22
  store i32 0, ptr %5, align 4
  br label %77

42:                                               ; preds = %29
  %43 = load i8, ptr %9, align 1, !tbaa !9
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [22 x [22 x i8]], ptr @grid, i64 0, i64 %45
  %47 = load i32, ptr %7, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [22 x i8], ptr %46, i64 0, i64 %48
  store i8 %43, ptr %49, 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 %50

50:                                               ; preds = %72, %42
  %51 = load i32, ptr %11, align 4, !tbaa !5
  %52 = icmp slt i32 %51, 4
  br i1 %52, label %54, label %53

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

54:                                               ; preds = %50
  %55 = load i32, ptr %6, align 4, !tbaa !5
  %56 = load i32, ptr %11, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [4 x i32], ptr @dr, i64 0, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !5
  %60 = add nsw i32 %55, %59
  %61 = load i32, ptr %7, align 4, !tbaa !5
  %62 = load i32, ptr %11, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [4 x i32], ptr @dc, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = add nsw i32 %61, %65
  %67 = load i8, ptr %8, align 1, !tbaa !9
  %68 = load i8, ptr %9, align 1, !tbaa !9
  %69 = call noundef i32 @_Z9floodFilliicc(i32 noundef %60, i32 noundef %66, i8 noundef signext %67, i8 noundef signext %68)
  %70 = load i32, ptr %10, align 4, !tbaa !5
  %71 = add nsw i32 %70, %69
  store i32 %71, ptr %10, align 4, !tbaa !5
  br label %72

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

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

77:                                               ; preds = %75, %41
  %78 = load i32, ptr %5, align 4
  ret i32 %78
}

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

@R = external dso_local local_unnamed_addr global i32, align 4
@C = external dso_local local_unnamed_addr global i32, align 4
@grid = external dso_local local_unnamed_addr global [22 x [22 x i8]], align 16
@dr = external dso_local local_unnamed_addr global [4 x i32], align 16
@dc = external dso_local local_unnamed_addr global [4 x i32], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z9floodFilliicc(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 ], [ %39, %.loopexit.loopexit ]
  %.tr = phi i32 [ %0, %4 ], [ %36, %.loopexit.loopexit ]
  %.tr30 = phi i32 [ %1, %4 ], [ %38, %.loopexit.loopexit ]
  %5 = icmp eq i32 %.tr30, -1
  %6 = load i32, ptr @C, align 4
  %7 = add nsw i32 %6, -1
  %.023 = select i1 %5, i32 %7, i32 %.tr30
  %8 = icmp eq i32 %.023, %6
  %spec.store.select = select i1 %8, i32 0, i32 %.023
  %9 = icmp slt i32 %.tr, 0
  %10 = load i32, ptr @R, align 4
  %11 = icmp eq i32 %10, %.tr
  %or.cond = select i1 %9, i1 true, i1 %11
  br i1 %or.cond, label %.loopexit, label %12

12:                                               ; preds = %tailrecurse
  %13 = zext nneg i32 %.tr to i64
  %14 = sext i32 %spec.store.select to i64
  %15 = getelementptr inbounds [22 x [22 x i8]], ptr @grid, i64 0, i64 %13, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %.not = icmp eq i8 %16, %2
  br i1 %.not, label %.loopexit.loopexit, label %.loopexit

.loopexit.loopexit:                               ; preds = %12
  store i8 %3, ptr %15, align 1, !tbaa !5
  %17 = load i32, ptr @dr, align 16, !tbaa !8
  %18 = add nsw i32 %17, %.tr
  %19 = load i32, ptr @dc, align 16, !tbaa !8
  %20 = add nsw i32 %19, %spec.store.select
  %21 = tail call noundef i32 @_Z9floodFilliicc(i32 noundef %18, i32 noundef %20, i8 noundef signext %2, i8 noundef signext %3)
  %22 = add nsw i32 %21, 1
  %23 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 4), align 4, !tbaa !8
  %24 = add nsw i32 %23, %.tr
  %25 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 4), align 4, !tbaa !8
  %26 = add nsw i32 %25, %spec.store.select
  %27 = tail call noundef i32 @_Z9floodFilliicc(i32 noundef %24, i32 noundef %26, i8 noundef signext %2, i8 noundef signext %3)
  %28 = add nsw i32 %27, %22
  %29 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 8), align 8, !tbaa !8
  %30 = add nsw i32 %29, %.tr
  %31 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 8), align 8, !tbaa !8
  %32 = add nsw i32 %31, %spec.store.select
  %33 = tail call noundef i32 @_Z9floodFilliicc(i32 noundef %30, i32 noundef %32, i8 noundef signext %2, i8 noundef signext %3)
  %34 = add nsw i32 %33, %28
  %35 = load i32, ptr getelementptr inbounds (i8, ptr @dr, i64 12), align 4, !tbaa !8
  %36 = add nsw i32 %35, %.tr
  %37 = load i32, ptr getelementptr inbounds (i8, ptr @dc, i64 12), align 4, !tbaa !8
  %38 = add nsw i32 %37, %spec.store.select
  %39 = add nsw i32 %accumulator.tr, %34
  br label %tailrecurse

.loopexit:                                        ; preds = %tailrecurse, %12
  %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