; LLVM IR from niessner/Matterport :: code/gaps/pkgs/R3Shapes/R3Grid.ll (ZNK6R3Grid10PercentileEd)

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

%class.R3Grid = type { %class.R3Affine, %class.R3Affine, double, double, ptr, [3 x i32], i32, i32, i32 }
%class.R3Affine = type { %class.R3Transformation, %class.R4Matrix, %class.R4Matrix, %class.RNFlags }
%class.R3Transformation = type { ptr }
%class.R4Matrix = type { [4 x [4 x double]] }
%class.RNFlags = type { i64 }

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64) #0

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) #3

; Function Attrs: mustprogress uwtable
define dso_local noundef double @_ZNK6R3Grid10PercentileEd(ptr noundef nonnull align 8 dereferenceable(592) %0, double noundef %1) #4 align 2 {
  %3 = alloca double, align 8
  %4 = alloca ptr, align 8
  %5 = alloca double, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca double, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store double %1, ptr %5, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8
  %11 = getelementptr inbounds %class.R3Grid, ptr %10, i32 0, i32 8
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  store double 0.000000e+00, ptr %3, align 8
  br label %65

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %16 = getelementptr inbounds %class.R3Grid, ptr %10, i32 0, i32 8
  %17 = load i32, ptr %16, align 4, !tbaa !11
  %18 = sext i32 %17 to i64
  %19 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %18, i64 8)
  %20 = extractvalue { i64, i1 } %19, 1
  %21 = extractvalue { i64, i1 } %19, 0
  %22 = select i1 %20, i64 -1, i64 %21
  %23 = call noalias noundef nonnull ptr @_Znam(i64 noundef %22) #7
  store ptr %23, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  store i32 0, ptr %7, align 4, !tbaa !19
  br label %24

24:                                               ; preds = %41, %15
  %25 = load i32, ptr %7, align 4, !tbaa !19
  %26 = getelementptr inbounds %class.R3Grid, ptr %10, i32 0, i32 8
  %27 = load i32, ptr %26, align 4, !tbaa !11
  %28 = icmp slt i32 %25, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %44

30:                                               ; preds = %24
  %31 = getelementptr inbounds %class.R3Grid, ptr %10, i32 0, i32 4
  %32 = load ptr, ptr %31, align 8, !tbaa !20
  %33 = load i32, ptr %7, align 4, !tbaa !19
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds double, ptr %32, i64 %34
  %36 = load double, ptr %35, align 8, !tbaa !9
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load i32, ptr %7, align 4, !tbaa !19
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds double, ptr %37, i64 %39
  store double %36, ptr %40, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %30
  %42 = load i32, ptr %7, align 4, !tbaa !19
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %7, align 4, !tbaa !19
  br label %24, !llvm.loop !21

44:                                               ; preds = %29
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds %class.R3Grid, ptr %10, i32 0, i32 8
  %47 = load i32, ptr %46, align 4, !tbaa !11
  %48 = sext i32 %47 to i64
  call void @qsort(ptr noundef %45, i64 noundef %48, i64 noundef 8, ptr noundef @_Z16RNCompareScalarsPKvS0_)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  %49 = load double, ptr %5, align 8, !tbaa !9
  %50 = getelementptr inbounds %class.R3Grid, ptr %10, i32 0, i32 8
  %51 = load i32, ptr %50, align 4, !tbaa !11
  %52 = sitofp i32 %51 to double
  %53 = fmul double %49, %52
  %54 = fptosi double %53 to i32
  store i32 %54, ptr %8, align 4, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load i32, ptr %8, align 4, !tbaa !19
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds double, ptr %55, i64 %57
  %59 = load double, ptr %58, align 8, !tbaa !9
  store double %59, ptr %9, align 8, !tbaa !9
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = icmp eq ptr %60, null
  br i1 %61, label %63, label %62

62:                                               ; preds = %44
  call void @_ZdaPv(ptr noundef %60) #8
  br label %63

63:                                               ; preds = %62, %44
  %64 = load double, ptr %9, align 8, !tbaa !9
  store double %64, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  br label %65

65:                                               ; preds = %63, %14
  %66 = load double, ptr %3, align 8
  ret double %66
}

declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #5

declare noundef i32 @_Z16RNCompareScalarsPKvS0_(ptr noundef, ptr noundef) #5

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { nobuiltin allocsize(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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nobuiltin nounwind "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 #4 = { 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 #5 = { "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 #6 = { nounwind }
attributes #7 = { builtin allocsize(0) }
attributes #8 = { builtin 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 = !{!"double", !7, i64 0}
!11 = !{!12, !18, i64 588}
!12 = !{!"_ZTS6R3Grid", !13, i64 0, !13, i64 272, !10, i64 544, !10, i64 552, !6, i64 560, !7, i64 568, !18, i64 580, !18, i64 584, !18, i64 588}
!13 = !{!"_ZTS8R3Affine", !14, i64 0, !15, i64 8, !15, i64 136, !16, i64 264}
!14 = !{!"_ZTS16R3Transformation"}
!15 = !{!"_ZTS8R4Matrix", !7, i64 0}
!16 = !{!"_ZTS7RNFlags", !17, i64 0}
!17 = !{!"long", !7, i64 0}
!18 = !{!"int", !7, i64 0}
!19 = !{!18, !18, i64 0}
!20 = !{!12, !6, i64 560}
!21 = distinct !{!21, !22}
!22 = !{!"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: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #0

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef double @_ZNK6R3Grid10PercentileEd(ptr nocapture noundef nonnull readonly align 8 dereferenceable(592) %0, double noundef %1) local_unnamed_addr #2 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 588
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %47, label %6

6:                                                ; preds = %2
  %7 = sext i32 %4 to i64
  %8 = icmp slt i32 %4, 0
  %9 = shl nsw i64 %7, 3
  %10 = select i1 %8, i64 -1, i64 %9
  %11 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %10) #5
  %12 = icmp sgt i32 %4, 0
  br i1 %12, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %6
  %13 = ptrtoint ptr %11 to i64
  %14 = getelementptr inbounds i8, ptr %0, i64 560
  %15 = load ptr, ptr %14, align 8, !tbaa !17
  %wide.trip.count = zext nneg i32 %4 to i64
  %min.iters.check = icmp ult i32 %4, 6
  %16 = ptrtoint ptr %15 to i64
  %17 = sub i64 %13, %16
  %diff.check = icmp ult i64 %17, 32
  %or.cond = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  %xtraiter = and i64 %wide.trip.count, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %scalar.ph.prol ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %18 = getelementptr inbounds double, ptr %15, i64 %indvars.iv.prol
  %19 = load double, ptr %18, align 8, !tbaa !18
  %20 = getelementptr inbounds double, ptr %11, i64 %indvars.iv.prol
  store double %19, ptr %20, align 8, !tbaa !18
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !19

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %21 = sub nsw i64 %indvars.iv.ph, %wide.trip.count
  %22 = icmp ugt i64 %21, -4
  br i1 %22, label %._crit_edge, label %scalar.ph

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

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %23 = getelementptr inbounds double, ptr %15, i64 %index
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %wide.load = load <2 x double>, ptr %23, align 8, !tbaa !18
  %wide.load17 = load <2 x double>, ptr %24, align 8, !tbaa !18
  %25 = getelementptr inbounds double, ptr %11, i64 %index
  %26 = getelementptr inbounds i8, ptr %25, i64 16
  store <2 x double> %wide.load, ptr %25, align 8, !tbaa !18
  store <2 x double> %wide.load17, ptr %26, align 8, !tbaa !18
  %index.next = add nuw i64 %index, 4
  %27 = icmp eq i64 %index.next, %n.vec
  br i1 %27, label %middle.block, label %vector.body, !llvm.loop !21

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block, %6
  tail call void @qsort(ptr noundef nonnull %11, i64 noundef %7, i64 noundef 8, ptr noundef nonnull @_Z16RNCompareScalarsPKvS0_)
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sitofp i32 %28 to double
  %30 = fmul double %29, %1
  %31 = fptosi double %30 to i32
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds double, ptr %11, i64 %32
  %34 = load double, ptr %33, align 8, !tbaa !18
  tail call void @_ZdaPv(ptr noundef nonnull %11) #6
  br label %47

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %scalar.ph ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %35 = getelementptr inbounds double, ptr %15, i64 %indvars.iv
  %36 = load double, ptr %35, align 8, !tbaa !18
  %37 = getelementptr inbounds double, ptr %11, i64 %indvars.iv
  store double %36, ptr %37, align 8, !tbaa !18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %38 = getelementptr inbounds double, ptr %15, i64 %indvars.iv.next
  %39 = load double, ptr %38, align 8, !tbaa !18
  %40 = getelementptr inbounds double, ptr %11, i64 %indvars.iv.next
  store double %39, ptr %40, align 8, !tbaa !18
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %41 = getelementptr inbounds double, ptr %15, i64 %indvars.iv.next.1
  %42 = load double, ptr %41, align 8, !tbaa !18
  %43 = getelementptr inbounds double, ptr %11, i64 %indvars.iv.next.1
  store double %42, ptr %43, align 8, !tbaa !18
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %44 = getelementptr inbounds double, ptr %15, i64 %indvars.iv.next.2
  %45 = load double, ptr %44, align 8, !tbaa !18
  %46 = getelementptr inbounds double, ptr %11, i64 %indvars.iv.next.2
  store double %45, ptr %46, align 8, !tbaa !18
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %._crit_edge, label %scalar.ph, !llvm.loop !25

47:                                               ; preds = %2, %._crit_edge
  %.0 = phi double [ %34, %._crit_edge ], [ 0.000000e+00, %2 ]
  ret double %.0
}

; Function Attrs: nofree
declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #3

declare noundef i32 @_Z16RNCompareScalarsPKvS0_(ptr noundef, ptr noundef) #4

attributes #0 = { nobuiltin allocsize(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 = { nobuiltin nounwind "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 = { 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 #3 = { nofree "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 #4 = { "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 #5 = { builtin allocsize(0) }
attributes #6 = { builtin 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, !16, i64 588}
!6 = !{!"_ZTS6R3Grid", !7, i64 0, !7, i64 272, !14, i64 544, !14, i64 552, !15, i64 560, !10, i64 568, !16, i64 580, !16, i64 584, !16, i64 588}
!7 = !{!"_ZTS8R3Affine", !8, i64 0, !9, i64 8, !9, i64 136, !12, i64 264}
!8 = !{!"_ZTS16R3Transformation"}
!9 = !{!"_ZTS8R4Matrix", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"_ZTS7RNFlags", !13, i64 0}
!13 = !{!"long", !10, i64 0}
!14 = !{!"double", !10, i64 0}
!15 = !{!"any pointer", !10, i64 0}
!16 = !{!"int", !10, i64 0}
!17 = !{!6, !15, i64 560}
!18 = !{!14, !14, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.unroll.disable"}
!21 = distinct !{!21, !22, !23, !24}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!"llvm.loop.isvectorized", i32 1}
!24 = !{!"llvm.loop.unroll.runtime.disable"}
!25 = distinct !{!25, !22, !23}


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