; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (1184)

; 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: nounwind uwtable
define dso_local i32 @distanceBetweenBusStops(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %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
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 1, ptr %12, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %39, %4
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = add nsw i32 %17, -1
  store i32 %18, ptr %6, align 4, !tbaa !9
  %19 = icmp ne i32 %17, 0
  br i1 %19, label %20, label %45

20:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds i32, ptr %21, i32 1
  store ptr %22, ptr %5, align 8, !tbaa !5
  %23 = load i32, ptr %21, align 4, !tbaa !9
  store i32 %23, ptr %13, align 4, !tbaa !9
  %24 = load i32, ptr %12, align 4, !tbaa !9
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = icmp sle i32 %24, %25
  br i1 %26, label %27, label %31

27:                                               ; preds = %20
  %28 = load i32, ptr %13, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = add nsw i32 %29, %28
  store i32 %30, ptr %9, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %27, %20
  %32 = load i32, ptr %12, align 4, !tbaa !9
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = icmp sle i32 %32, %33
  br i1 %34, label %35, label %39

35:                                               ; preds = %31
  %36 = load i32, ptr %13, align 4, !tbaa !9
  %37 = load i32, ptr %10, align 4, !tbaa !9
  %38 = add nsw i32 %37, %36
  store i32 %38, ptr %10, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %35, %31
  %40 = load i32, ptr %13, align 4, !tbaa !9
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = add nsw i32 %41, %40
  store i32 %42, ptr %11, align 4, !tbaa !9
  %43 = load i32, ptr %12, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  br label %16, !llvm.loop !11

45:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = load i32, ptr %8, align 4, !tbaa !9
  %48 = icmp sgt i32 %46, %47
  br i1 %48, label %49, label %53

49:                                               ; preds = %45
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = load i32, ptr %10, align 4, !tbaa !9
  %52 = sub nsw i32 %50, %51
  store i32 %52, ptr %14, align 4, !tbaa !9
  br label %57

53:                                               ; preds = %45
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = sub nsw i32 %54, %55
  store i32 %56, ptr %14, align 4, !tbaa !9
  br label %57

57:                                               ; preds = %53, %49
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = load i32, ptr %14, align 4, !tbaa !9
  %60 = sub nsw i32 %58, %59
  store i32 %60, ptr %15, align 4, !tbaa !9
  %61 = load i32, ptr %14, align 4, !tbaa !9
  %62 = load i32, ptr %15, align 4, !tbaa !9
  %63 = icmp slt i32 %61, %62
  br i1 %63, label %64, label %66

64:                                               ; preds = %57
  %65 = load i32, ptr %14, align 4, !tbaa !9
  br label %68

66:                                               ; preds = %57
  %67 = load i32, ptr %15, align 4, !tbaa !9
  br label %68

68:                                               ; preds = %66, %64
  %69 = phi i32 [ %65, %64 ], [ %67, %66 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret i32 %69
}

; 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 = { 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 = { 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 = !{!"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"}


; 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: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @distanceBetweenBusStops(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %.not33 = icmp eq i32 %1, 0
  br i1 %.not33, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %5 = zext i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  br i1 %min.iters.check, label %.lr.ph.preheader61, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %5, 4294967288
  %6 = shl nuw nsw i64 %n.vec, 2
  %ind.end = getelementptr i8, ptr %0, i64 %6
  %.cast = trunc nuw i64 %n.vec to i32
  %ind.end42 = sub i32 %1, %.cast
  %.cast44 = trunc nuw i64 %n.vec to i32
  %ind.end45 = or disjoint i32 %.cast44, 1
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %2, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %broadcast.splatinsert55 = insertelement <4 x i32> poison, i32 %3, i64 0
  %broadcast.splat56 = shufflevector <4 x i32> %broadcast.splatinsert55, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <4 x i32> [ <i32 1, i32 2, i32 3, i32 4>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %20, %vector.body ]
  %vec.phi48 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %21, %vector.body ]
  %vec.phi49 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %18, %vector.body ]
  %vec.phi50 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %19, %vector.body ]
  %vec.phi51 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %12, %vector.body ]
  %vec.phi52 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %13, %vector.body ]
  %step.add = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %offset.idx = shl i64 %index, 2
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %7 = getelementptr i8, ptr %next.gep, i64 16
  %wide.load = load <4 x i32>, ptr %next.gep, align 4, !tbaa !5
  %wide.load54 = load <4 x i32>, ptr %7, align 4, !tbaa !5
  %8 = icmp sgt <4 x i32> %vec.ind, %broadcast.splat
  %9 = icmp sgt <4 x i32> %step.add, %broadcast.splat
  %10 = select <4 x i1> %8, <4 x i32> zeroinitializer, <4 x i32> %wide.load
  %11 = select <4 x i1> %9, <4 x i32> zeroinitializer, <4 x i32> %wide.load54
  %12 = add <4 x i32> %10, %vec.phi51
  %13 = add <4 x i32> %11, %vec.phi52
  %14 = icmp sgt <4 x i32> %vec.ind, %broadcast.splat56
  %15 = icmp sgt <4 x i32> %step.add, %broadcast.splat56
  %16 = select <4 x i1> %14, <4 x i32> zeroinitializer, <4 x i32> %wide.load
  %17 = select <4 x i1> %15, <4 x i32> zeroinitializer, <4 x i32> %wide.load54
  %18 = add <4 x i32> %16, %vec.phi49
  %19 = add <4 x i32> %17, %vec.phi50
  %20 = add <4 x i32> %wide.load, %vec.phi
  %21 = add <4 x i32> %wide.load54, %vec.phi48
  %index.next = add nuw i64 %index, 8
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %22 = icmp eq i64 %index.next, %n.vec
  br i1 %22, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %21, %20
  %23 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %bin.rdx57 = add <4 x i32> %19, %18
  %24 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx57)
  %bin.rdx58 = add <4 x i32> %13, %12
  %25 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx58)
  %cmp.n = icmp eq i64 %n.vec, %5
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader61

.lr.ph.preheader61:                               ; preds = %middle.block, %.lr.ph.preheader
  %.039.ph = phi ptr [ %0, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.02538.ph = phi i32 [ %1, %.lr.ph.preheader ], [ %ind.end42, %middle.block ]
  %.02637.ph = phi i32 [ 1, %.lr.ph.preheader ], [ %ind.end45, %middle.block ]
  %.02736.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %23, %middle.block ]
  %.02835.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %24, %middle.block ]
  %.02934.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %25, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader61, %.lr.ph
  %.039 = phi ptr [ %27, %.lr.ph ], [ %.039.ph, %.lr.ph.preheader61 ]
  %.02538 = phi i32 [ %26, %.lr.ph ], [ %.02538.ph, %.lr.ph.preheader61 ]
  %.02637 = phi i32 [ %32, %.lr.ph ], [ %.02637.ph, %.lr.ph.preheader61 ]
  %.02736 = phi i32 [ %31, %.lr.ph ], [ %.02736.ph, %.lr.ph.preheader61 ]
  %.02835 = phi i32 [ %.1, %.lr.ph ], [ %.02835.ph, %.lr.ph.preheader61 ]
  %.02934 = phi i32 [ %spec.select, %.lr.ph ], [ %.02934.ph, %.lr.ph.preheader61 ]
  %26 = add nsw i32 %.02538, -1
  %27 = getelementptr inbounds i8, ptr %.039, i64 4
  %28 = load i32, ptr %.039, align 4, !tbaa !5
  %.not31 = icmp sgt i32 %.02637, %2
  %29 = select i1 %.not31, i32 0, i32 %28
  %spec.select = add nsw i32 %29, %.02934
  %.not32 = icmp sgt i32 %.02637, %3
  %30 = select i1 %.not32, i32 0, i32 %28
  %.1 = add nsw i32 %30, %.02835
  %31 = add nsw i32 %28, %.02736
  %32 = add nuw nsw i32 %.02637, 1
  %.not = icmp eq i32 %26, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %4
  %.029.lcssa = phi i32 [ 0, %4 ], [ %25, %middle.block ], [ %spec.select, %.lr.ph ]
  %.028.lcssa = phi i32 [ 0, %4 ], [ %24, %middle.block ], [ %.1, %.lr.ph ]
  %.027.lcssa = phi i32 [ 0, %4 ], [ %23, %middle.block ], [ %31, %.lr.ph ]
  %33 = icmp sgt i32 %2, %3
  %34 = sub nsw i32 %.029.lcssa, %.028.lcssa
  %35 = sub nsw i32 %.028.lcssa, %.029.lcssa
  %.024 = select i1 %33, i32 %34, i32 %35
  %36 = sub nsw i32 %.027.lcssa, %.024
  %37 = tail call i32 @llvm.smin.i32(i32 %.024, i32 %36)
  ret i32 %37
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #1

attributes #0 = { nofree norecurse nosync nounwind memory(read, 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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass