; LLVM IR from ahampriyanshu/algo-ds-101 :: Algorithms/Graph/Johnson_algorithm/Johnson_algorithm.ll (Z11minDistancePiPbi)

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z11minDistancePiPbi(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 2147483647, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %40, %3
  %11 = load i32, ptr %9, align 4, !tbaa !9
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %43

15:                                               ; preds = %10
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %16, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !11, !range !13, !noundef !14
  %21 = trunc i8 %20 to i1
  %22 = zext i1 %21 to i32
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %39

24:                                               ; preds = %15
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %25, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = icmp sle i32 %29, %30
  br i1 %31, label %32, label %39

32:                                               ; preds = %24
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %33, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !9
  store i32 %37, ptr %7, align 4, !tbaa !9
  %38 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %38, ptr %8, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %32, %24, %15
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %9, align 4, !tbaa !9
  br label %10, !llvm.loop !15

43:                                               ; preds = %14
  %44 = load i32, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %44
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"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: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i32 @_Z11minDistancePiPbi(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %2, 0
  tail call void @llvm.assume(i1 %4)
  %wide.trip.count = zext nneg i32 %2 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %5 = icmp eq i32 %2, 1
  br i1 %5, label %._crit_edge.unr-lcssa, label %.new

.new:                                             ; preds = %3
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

._crit_edge.unr-lcssa:                            ; preds = %27, %3
  %.1.lcssa.ph = phi i32 [ poison, %3 ], [ %.1.1, %27 ]
  %indvars.iv.unr = phi i64 [ 0, %3 ], [ %indvars.iv.next.1, %27 ]
  %.01117.unr = phi i32 [ undef, %3 ], [ %.1.1, %27 ]
  %.01216.unr = phi i32 [ 2147483647, %3 ], [ %.113.1, %27 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.unr-lcssa
  %6 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.unr
  %7 = load i8, ptr %6, align 1, !tbaa !5, !range !9, !noundef !10
  %8 = icmp eq i8 %7, 0
  br i1 %8, label %9, label %._crit_edge

9:                                                ; preds = %.lr.ph.epil
  %10 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %.not.epil = icmp sgt i32 %11, %.01216.unr
  %12 = trunc nuw nsw i64 %indvars.iv.unr to i32
  %spec.select15.epil = select i1 %.not.epil, i32 %.01117.unr, i32 %12
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %9, %._crit_edge.unr-lcssa
  %.1.lcssa = phi i32 [ %.1.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.01117.unr, %.lr.ph.epil ], [ %spec.select15.epil, %9 ]
  ret i32 %.1.lcssa

.lr.ph:                                           ; preds = %27, %.new
  %indvars.iv = phi i64 [ 0, %.new ], [ %indvars.iv.next.1, %27 ]
  %.01117 = phi i32 [ undef, %.new ], [ %.1.1, %27 ]
  %.01216 = phi i32 [ 2147483647, %.new ], [ %.113.1, %27 ]
  %niter = phi i64 [ 0, %.new ], [ %niter.next.1, %27 ]
  %13 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv
  %14 = load i8, ptr %13, align 1, !tbaa !5, !range !9, !noundef !10
  %15 = icmp eq i8 %14, 0
  br i1 %15, label %16, label %.lr.ph.1

16:                                               ; preds = %.lr.ph
  %17 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %.not = icmp sgt i32 %18, %.01216
  %spec.select = tail call i32 @llvm.smin.i32(i32 %18, i32 %.01216)
  %19 = trunc nuw nsw i64 %indvars.iv to i32
  %spec.select15 = select i1 %.not, i32 %.01117, i32 %19
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %16, %.lr.ph
  %.113 = phi i32 [ %.01216, %.lr.ph ], [ %spec.select, %16 ]
  %.1 = phi i32 [ %.01117, %.lr.ph ], [ %spec.select15, %16 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %20 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.next
  %21 = load i8, ptr %20, align 1, !tbaa !5, !range !9, !noundef !10
  %22 = icmp eq i8 %21, 0
  br i1 %22, label %23, label %27

23:                                               ; preds = %.lr.ph.1
  %24 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %25 = load i32, ptr %24, align 4, !tbaa !11
  %.not.1 = icmp sgt i32 %25, %.113
  %spec.select.1 = tail call i32 @llvm.smin.i32(i32 %25, i32 %.113)
  %26 = trunc nuw nsw i64 %indvars.iv.next to i32
  %spec.select15.1 = select i1 %.not.1, i32 %.1, i32 %26
  br label %27

27:                                               ; preds = %23, %.lr.ph.1
  %.113.1 = phi i32 [ %.113, %.lr.ph.1 ], [ %spec.select.1, %23 ]
  %.1.1 = phi i32 [ %.1, %.lr.ph.1 ], [ %spec.select15.1, %23 ]
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.unr-lcssa, label %.lr.ph, !llvm.loop !13
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #2

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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 speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }

!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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass