; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/other/AllocateMinPages.ll (Z8findPagePiii)

; 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: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z7isValidPiiii(ptr noundef, i32 noundef, i32 noundef, i32 noundef) #0

; 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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z8findPagePiii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %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
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  br label %90

20:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 -2147483648, 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
  br label %21

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

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

26:                                               ; preds = %21
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load i32, ptr %9, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = icmp sgt i32 %31, %32
  br i1 %33, label %34, label %40

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

40:                                               ; preds = %34, %26
  br label %41

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

44:                                               ; preds = %25
  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
  br label %45

45:                                               ; preds = %58, %44
  %46 = load i32, ptr %11, align 4, !tbaa !9
  %47 = load i32, ptr %6, align 4, !tbaa !9
  %48 = icmp slt i32 %46, %47
  br i1 %48, label %50, label %49

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

50:                                               ; preds = %45
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load i32, ptr %11, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i32, ptr %51, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !9
  %56 = load i32, ptr %10, align 4, !tbaa !9
  %57 = add nsw i32 %56, %55
  store i32 %57, ptr %10, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %50
  %59 = load i32, ptr %11, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %11, align 4, !tbaa !9
  br label %45, !llvm.loop !13

61:                                               ; preds = %49
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %62 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %62, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %63 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %63, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  store i32 -1, ptr %14, align 4, !tbaa !9
  br label %64

64:                                               ; preds = %87, %61
  %65 = load i32, ptr %12, align 4, !tbaa !9
  %66 = load i32, ptr %13, align 4, !tbaa !9
  %67 = icmp sle i32 %65, %66
  br i1 %67, label %68, label %88

68:                                               ; preds = %64
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  %69 = load i32, ptr %12, align 4, !tbaa !9
  %70 = load i32, ptr %15, align 4, !tbaa !9
  %71 = load i32, ptr %12, align 4, !tbaa !9
  %72 = sub nsw i32 %70, %71
  %73 = sdiv i32 %72, 2
  %74 = add nsw i32 %69, %73
  store i32 %74, ptr %15, align 4, !tbaa !9
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = load i32, ptr %6, align 4, !tbaa !9
  %77 = load i32, ptr %7, align 4, !tbaa !9
  %78 = load i32, ptr %15, align 4, !tbaa !9
  %79 = call noundef zeroext i1 @_Z7isValidPiiii(ptr noundef %75, i32 noundef %76, i32 noundef %77, i32 noundef %78)
  br i1 %79, label %80, label %84

80:                                               ; preds = %68
  %81 = load i32, ptr %15, align 4, !tbaa !9
  store i32 %81, ptr %14, align 4, !tbaa !9
  %82 = load i32, ptr %15, align 4, !tbaa !9
  %83 = sub nsw i32 %82, 1
  store i32 %83, ptr %13, align 4, !tbaa !9
  br label %87

84:                                               ; preds = %68
  %85 = load i32, ptr %15, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %12, align 4, !tbaa !9
  br label %87

87:                                               ; preds = %84, %80
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  br label %64, !llvm.loop !14

88:                                               ; preds = %64
  %89 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %89, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  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 %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %90

90:                                               ; preds = %88, %19
  %91 = load i32, ptr %4, align 4
  ret i32 %91
}

attributes #0 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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 nounwind uwtable
declare dso_local noundef zeroext i1 @_Z7isValidPiiii(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z8findPagePiii(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp slt i32 %1, %2
  br i1 %4, label %.loopexit, label %.preheader46

.preheader46:                                     ; preds = %3
  %5 = icmp sgt i32 %1, 0
  br i1 %5, label %.lr.ph.preheader, label %.lr.ph58.preheader

.lr.ph.preheader:                                 ; preds = %.preheader46
  %wide.trip.count = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  br i1 %min.iters.check, label %.lr.ph.preheader94, label %vector.ph

.lr.ph.preheader94:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.03947.ph = phi i32 [ -2147483648, %.lr.ph.preheader ], [ %11, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %wide.trip.count, 2147483640
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ <i32 -2147483648, i32 -2147483648, i32 -2147483648, i32 -2147483648>, %vector.ph ], [ %8, %vector.body ]
  %vec.phi73 = phi <4 x i32> [ <i32 -2147483648, i32 -2147483648, i32 -2147483648, i32 -2147483648>, %vector.ph ], [ %9, %vector.body ]
  %6 = getelementptr inbounds i32, ptr %0, i64 %index
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %wide.load = load <4 x i32>, ptr %6, align 4, !tbaa !5
  %wide.load74 = load <4 x i32>, ptr %7, align 4, !tbaa !5
  %8 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %wide.load, <4 x i32> %vec.phi)
  %9 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %wide.load74, <4 x i32> %vec.phi73)
  %index.next = add nuw i64 %index, 8
  %10 = icmp eq i64 %index.next, %n.vec
  br i1 %10, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %8, <4 x i32> %9)
  %11 = tail call i32 @llvm.vector.reduce.smax.v4i32(<4 x i32> %rdx.minmax)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.preheader45, label %.lr.ph.preheader94

.preheader45:                                     ; preds = %.lr.ph, %middle.block
  %spec.select.lcssa = phi i32 [ %11, %middle.block ], [ %spec.select, %.lr.ph ]
  br i1 %5, label %.lr.ph51.preheader, label %.preheader

.lr.ph51.preheader:                               ; preds = %.preheader45
  %wide.trip.count64 = zext nneg i32 %1 to i64
  %min.iters.check77 = icmp ult i32 %1, 8
  br i1 %min.iters.check77, label %.lr.ph51.preheader91, label %vector.ph78

.lr.ph51.preheader91:                             ; preds = %middle.block75, %.lr.ph51.preheader
  %indvars.iv61.ph = phi i64 [ 0, %.lr.ph51.preheader ], [ %n.vec80, %middle.block75 ]
  %.03749.ph = phi i32 [ 0, %.lr.ph51.preheader ], [ %17, %middle.block75 ]
  br label %.lr.ph51

vector.ph78:                                      ; preds = %.lr.ph51.preheader
  %n.vec80 = and i64 %wide.trip.count, 2147483640
  br label %vector.body82

vector.body82:                                    ; preds = %vector.body82, %vector.ph78
  %index83 = phi i64 [ 0, %vector.ph78 ], [ %index.next88, %vector.body82 ]
  %vec.phi84 = phi <4 x i32> [ zeroinitializer, %vector.ph78 ], [ %14, %vector.body82 ]
  %vec.phi85 = phi <4 x i32> [ zeroinitializer, %vector.ph78 ], [ %15, %vector.body82 ]
  %12 = getelementptr inbounds i32, ptr %0, i64 %index83
  %13 = getelementptr inbounds i8, ptr %12, i64 16
  %wide.load86 = load <4 x i32>, ptr %12, align 4, !tbaa !5
  %wide.load87 = load <4 x i32>, ptr %13, align 4, !tbaa !5
  %14 = add <4 x i32> %wide.load86, %vec.phi84
  %15 = add <4 x i32> %wide.load87, %vec.phi85
  %index.next88 = add nuw i64 %index83, 8
  %16 = icmp eq i64 %index.next88, %n.vec80
  br i1 %16, label %middle.block75, label %vector.body82, !llvm.loop !13

middle.block75:                                   ; preds = %vector.body82
  %bin.rdx = add <4 x i32> %15, %14
  %17 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n89 = icmp eq i64 %n.vec80, %wide.trip.count
  br i1 %cmp.n89, label %.preheader, label %.lr.ph51.preheader91

.lr.ph:                                           ; preds = %.lr.ph.preheader94, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader94 ]
  %.03947 = phi i32 [ %spec.select, %.lr.ph ], [ %.03947.ph, %.lr.ph.preheader94 ]
  %18 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %spec.select = tail call i32 @llvm.smax.i32(i32 %19, i32 %.03947)
  %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 %.preheader45, label %.lr.ph, !llvm.loop !14

.preheader:                                       ; preds = %.lr.ph51, %middle.block75, %.preheader45
  %.037.lcssa = phi i32 [ 0, %.preheader45 ], [ %17, %middle.block75 ], [ %22, %.lr.ph51 ]
  %.not53 = icmp sgt i32 %spec.select.lcssa, %.037.lcssa
  br i1 %.not53, label %.loopexit, label %.lr.ph58.preheader

.lr.ph58.preheader:                               ; preds = %.preheader46, %.preheader
  %.03255.ph = phi i32 [ %.037.lcssa, %.preheader ], [ 0, %.preheader46 ]
  %.03454.ph = phi i32 [ %spec.select.lcssa, %.preheader ], [ -2147483648, %.preheader46 ]
  br label %.lr.ph58

.lr.ph51:                                         ; preds = %.lr.ph51.preheader91, %.lr.ph51
  %indvars.iv61 = phi i64 [ %indvars.iv.next62, %.lr.ph51 ], [ %indvars.iv61.ph, %.lr.ph51.preheader91 ]
  %.03749 = phi i32 [ %22, %.lr.ph51 ], [ %.03749.ph, %.lr.ph51.preheader91 ]
  %20 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv61
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = add nsw i32 %21, %.03749
  %indvars.iv.next62 = add nuw nsw i64 %indvars.iv61, 1
  %exitcond65.not = icmp eq i64 %indvars.iv.next62, %wide.trip.count64
  br i1 %exitcond65.not, label %.preheader, label %.lr.ph51, !llvm.loop !15

.lr.ph58:                                         ; preds = %.lr.ph58.preheader, %.lr.ph58
  %.057 = phi i32 [ %25, %.lr.ph58 ], [ undef, %.lr.ph58.preheader ]
  %.03156 = phi i32 [ %.1, %.lr.ph58 ], [ -1, %.lr.ph58.preheader ]
  %.03255 = phi i32 [ %.133, %.lr.ph58 ], [ %.03255.ph, %.lr.ph58.preheader ]
  %.03454 = phi i32 [ %.135, %.lr.ph58 ], [ %.03454.ph, %.lr.ph58.preheader ]
  %23 = sub nsw i32 %.057, %.03454
  %24 = sdiv i32 %23, 2
  %25 = add nsw i32 %24, %.03454
  %26 = tail call noundef zeroext i1 @_Z7isValidPiiii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %25)
  %27 = add nsw i32 %25, -1
  %28 = add nsw i32 %25, 1
  %.135 = select i1 %26, i32 %.03454, i32 %28
  %.133 = select i1 %26, i32 %27, i32 %.03255
  %.1 = select i1 %26, i32 %25, i32 %.03156
  %.not = icmp sgt i32 %.135, %.133
  br i1 %.not, label %.loopexit, label %.lr.ph58, !llvm.loop !16

.loopexit:                                        ; preds = %.lr.ph58, %.preheader, %3
  %.041 = phi i32 [ -1, %3 ], [ -1, %.preheader ], [ %.1, %.lr.ph58 ]
  ret i32 %.041
}

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.smax.v4i32(<4 x 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 = { 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 #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++ 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, !11, !12}
!14 = distinct !{!14, !10, !12, !11}
!15 = distinct !{!15, !10, !12, !11}
!16 = distinct !{!16, !10}


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