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

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

18:                                               ; preds = %74, %5
  %19 = load i32, ptr %14, align 4, !tbaa !5
  %20 = load i32, ptr %9, align 4, !tbaa !5
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 2, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  br label %77

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #2
  %24 = load ptr, ptr %8, align 8, !tbaa !9
  %25 = load i32, ptr %14, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds ptr, ptr %24, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !9
  %29 = load i32, ptr %28, align 4, !tbaa !5
  store i32 %29, ptr %16, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #2
  %30 = load ptr, ptr %8, align 8, !tbaa !9
  %31 = load i32, ptr %14, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds ptr, ptr %30, i64 %32
  %34 = load ptr, ptr %33, align 8, !tbaa !9
  %35 = getelementptr inbounds i32, ptr %34, i64 1
  %36 = load i32, ptr %35, align 4, !tbaa !5
  store i32 %36, ptr %17, align 4, !tbaa !5
  %37 = load i32, ptr %16, align 4, !tbaa !5
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = icmp eq i32 %37, %38
  br i1 %39, label %40, label %44

40:                                               ; preds = %23
  %41 = load i32, ptr %17, align 4, !tbaa !5
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = sub nsw i32 %41, %42
  store i32 %43, ptr %13, align 4, !tbaa !5
  br label %54

44:                                               ; preds = %23
  %45 = load i32, ptr %17, align 4, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = icmp eq i32 %45, %46
  br i1 %47, label %48, label %52

48:                                               ; preds = %44
  %49 = load i32, ptr %16, align 4, !tbaa !5
  %50 = load i32, ptr %6, align 4, !tbaa !5
  %51 = sub nsw i32 %49, %50
  store i32 %51, ptr %13, align 4, !tbaa !5
  br label %53

52:                                               ; preds = %44
  store i32 4, ptr %15, align 4
  br label %71

53:                                               ; preds = %48
  br label %54

54:                                               ; preds = %53, %40
  %55 = load i32, ptr %13, align 4, !tbaa !5
  %56 = icmp slt i32 %55, 0
  br i1 %56, label %57, label %60

57:                                               ; preds = %54
  %58 = load i32, ptr %13, align 4, !tbaa !5
  %59 = sub nsw i32 0, %58
  br label %62

60:                                               ; preds = %54
  %61 = load i32, ptr %13, align 4, !tbaa !5
  br label %62

62:                                               ; preds = %60, %57
  %63 = phi i32 [ %59, %57 ], [ %61, %60 ]
  store i32 %63, ptr %13, align 4, !tbaa !5
  %64 = load i32, ptr %13, align 4, !tbaa !5
  %65 = load i32, ptr %11, align 4, !tbaa !5
  %66 = icmp slt i32 %64, %65
  br i1 %66, label %67, label %70

67:                                               ; preds = %62
  %68 = load i32, ptr %13, align 4, !tbaa !5
  store i32 %68, ptr %11, align 4, !tbaa !5
  %69 = load i32, ptr %14, align 4, !tbaa !5
  store i32 %69, ptr %12, align 4, !tbaa !5
  br label %70

70:                                               ; preds = %67, %62
  store i32 0, ptr %15, align 4
  br label %71

71:                                               ; preds = %70, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #2
  %72 = load i32, ptr %15, align 4
  switch i32 %72, label %79 [
    i32 0, label %73
    i32 4, label %74
  ]

73:                                               ; preds = %71
  br label %74

74:                                               ; preds = %73, %71
  %75 = load i32, ptr %14, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %14, align 4, !tbaa !5
  br label %18, !llvm.loop !11

77:                                               ; preds = %22
  %78 = load i32, ptr %12, align 4, !tbaa !5
  store i32 1, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  ret i32 %78

79:                                               ; preds = %71
  unreachable
}

; 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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 @nearestValidPoint(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2, i32 noundef %3, ptr nocapture noundef readnone %4) local_unnamed_addr #0 {
  %6 = icmp sgt i32 %3, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %5
  %wide.trip.count = zext nneg i32 %3 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %23, %5
  %.022.lcssa = phi i32 [ -1, %5 ], [ %.224, %23 ]
  ret i32 %.022.lcssa

.lr.ph:                                           ; preds = %.lr.ph.preheader, %23
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %23 ]
  %.033 = phi i32 [ 10240, %.lr.ph.preheader ], [ %.2, %23 ]
  %.02232 = phi i32 [ -1, %.lr.ph.preheader ], [ %.224, %23 ]
  %7 = getelementptr inbounds ptr, ptr %2, i64 %indvars.iv
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = load i32, ptr %8, align 4, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %8, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !9
  %12 = icmp eq i32 %9, %0
  br i1 %12, label %13, label %15

13:                                               ; preds = %.lr.ph
  %14 = sub nsw i32 %11, %1
  br label %19

15:                                               ; preds = %.lr.ph
  %16 = icmp eq i32 %11, %1
  br i1 %16, label %17, label %23

17:                                               ; preds = %15
  %18 = sub nsw i32 %9, %0
  br label %19

19:                                               ; preds = %17, %13
  %.026 = phi i32 [ %14, %13 ], [ %18, %17 ]
  %20 = tail call i32 @llvm.abs.i32(i32 %.026, i1 true)
  %21 = icmp slt i32 %20, %.033
  %22 = trunc nuw nsw i64 %indvars.iv to i32
  %spec.select = select i1 %21, i32 %22, i32 %.02232
  %spec.select30 = tail call i32 @llvm.smin.i32(i32 %20, i32 %.033)
  br label %23

23:                                               ; preds = %15, %19
  %.224 = phi i32 [ %spec.select, %19 ], [ %.02232, %15 ]
  %.2 = phi i32 [ %spec.select30, %19 ], [ %.033, %15 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, 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 = !{!"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"}


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