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

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

%struct.R3MeshProperty = type { ptr, [1024 x i8], i32, ptr, double, double, double, double, double, double }

; 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 @_ZNK14R3MeshProperty10PercentileEd(ptr noundef nonnull align 8 dereferenceable(1096) %0, double noundef %1) #4 align 2 {
  %3 = alloca double, align 8
  %4 = alloca ptr, align 8
  %5 = alloca double, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca double, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store double %1, ptr %5, align 8, !tbaa !9
  %12 = load ptr, ptr %4, align 8
  %13 = getelementptr inbounds %struct.R3MeshProperty, ptr %12, i32 0, i32 2
  %14 = load i32, ptr %13, align 8, !tbaa !11
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %17

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

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

26:                                               ; preds = %53, %17
  %27 = load i32, ptr %8, align 4, !tbaa !14
  %28 = getelementptr inbounds %struct.R3MeshProperty, ptr %12, i32 0, i32 2
  %29 = load i32, ptr %28, align 8, !tbaa !11
  %30 = icmp slt i32 %27, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  br label %56

32:                                               ; preds = %26
  %33 = getelementptr inbounds %struct.R3MeshProperty, ptr %12, i32 0, i32 3
  %34 = load ptr, ptr %33, align 8, !tbaa !15
  %35 = load i32, ptr %8, align 4, !tbaa !14
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds double, ptr %34, i64 %36
  %38 = load double, ptr %37, align 8, !tbaa !9
  %39 = fcmp oeq double %38, -1.000000e+00
  br i1 %39, label %40, label %41

40:                                               ; preds = %32
  br label %53

41:                                               ; preds = %32
  %42 = getelementptr inbounds %struct.R3MeshProperty, ptr %12, i32 0, i32 3
  %43 = load ptr, ptr %42, align 8, !tbaa !15
  %44 = load i32, ptr %8, align 4, !tbaa !14
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds double, ptr %43, i64 %45
  %47 = load double, ptr %46, align 8, !tbaa !9
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !14
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %6, align 4, !tbaa !14
  %51 = sext i32 %49 to i64
  %52 = getelementptr inbounds double, ptr %48, i64 %51
  store double %47, ptr %52, align 8, !tbaa !9
  br label %53

53:                                               ; preds = %41, %40
  %54 = load i32, ptr %8, align 4, !tbaa !14
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %8, align 4, !tbaa !14
  br label %26, !llvm.loop !16

56:                                               ; preds = %31
  %57 = load i32, ptr %6, align 4, !tbaa !14
  %58 = icmp eq i32 %57, 0
  br i1 %58, label %59, label %60

59:                                               ; preds = %56
  store double 0.000000e+00, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %87

60:                                               ; preds = %56
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = load i32, ptr %6, align 4, !tbaa !14
  %63 = sext i32 %62 to i64
  call void @qsort(ptr noundef %61, i64 noundef %63, i64 noundef 8, ptr noundef @_Z16RNCompareScalarsPKvS0_)
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #6
  %64 = load i32, ptr %6, align 4, !tbaa !14
  %65 = sitofp i32 %64 to double
  %66 = load double, ptr %5, align 8, !tbaa !9
  %67 = fmul double %65, %66
  %68 = fdiv double %67, 1.000000e+02
  %69 = fptosi double %68 to i32
  store i32 %69, ptr %10, align 4, !tbaa !14
  %70 = load i32, ptr %10, align 4, !tbaa !14
  %71 = load i32, ptr %6, align 4, !tbaa !14
  %72 = icmp sge i32 %70, %71
  br i1 %72, label %73, label %76

73:                                               ; preds = %60
  %74 = load i32, ptr %6, align 4, !tbaa !14
  %75 = sub nsw i32 %74, 1
  store i32 %75, ptr %10, align 4, !tbaa !14
  br label %76

76:                                               ; preds = %73, %60
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  %78 = load i32, ptr %10, align 4, !tbaa !14
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds double, ptr %77, i64 %79
  %81 = load double, ptr %80, align 8, !tbaa !9
  store double %81, ptr %11, align 8, !tbaa !9
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = icmp eq ptr %82, null
  br i1 %83, label %85, label %84

84:                                               ; preds = %76
  call void @_ZdaPv(ptr noundef %82) #8
  br label %85

85:                                               ; preds = %84, %76
  %86 = load double, ptr %11, align 8, !tbaa !9
  store double %86, ptr %3, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #6
  br label %87

87:                                               ; preds = %85, %59
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  br label %88

88:                                               ; preds = %87, %16
  %89 = load double, ptr %3, align 8
  ret double %89
}

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, !13, i64 1032}
!12 = !{!"_ZTS14R3MeshProperty", !6, i64 0, !7, i64 8, !13, i64 1032, !6, i64 1040, !10, i64 1048, !10, i64 1056, !10, i64 1064, !10, i64 1072, !10, i64 1080, !10, i64 1088}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!12, !6, i64 1040}
!16 = distinct !{!16, !17}
!17 = !{!"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 @_ZNK14R3MeshProperty10PercentileEd(ptr nocapture noundef nonnull readonly align 8 dereferenceable(1096) %0, double noundef %1) local_unnamed_addr #2 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 1032
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %._crit_edge.thread, 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.thread

.lr.ph:                                           ; preds = %6
  %13 = getelementptr inbounds i8, ptr %0, i64 1040
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  %wide.trip.count = zext nneg i32 %4 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %15 = icmp eq i32 %4, 1
  br i1 %15, label %._crit_edge.unr-lcssa, label %.lr.ph.new

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

._crit_edge.unr-lcssa:                            ; preds = %40, %.lr.ph
  %.122.lcssa.ph = phi i32 [ poison, %.lr.ph ], [ %.122.1, %40 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %40 ]
  %.02126.unr = phi i32 [ 0, %.lr.ph ], [ %.122.1, %40 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.unr-lcssa
  %16 = getelementptr inbounds double, ptr %14, i64 %indvars.iv.unr
  %17 = load double, ptr %16, align 8, !tbaa !13
  %18 = fcmp oeq double %17, -1.000000e+00
  br i1 %18, label %._crit_edge, label %19

19:                                               ; preds = %.epil.preheader
  %20 = add nsw i32 %.02126.unr, 1
  %21 = sext i32 %.02126.unr to i64
  %22 = getelementptr inbounds double, ptr %11, i64 %21
  store double %17, ptr %22, align 8, !tbaa !13
  br label %._crit_edge

._crit_edge:                                      ; preds = %.epil.preheader, %19, %._crit_edge.unr-lcssa
  %.122.lcssa = phi i32 [ %.122.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.02126.unr, %.epil.preheader ], [ %20, %19 ]
  %23 = icmp eq i32 %.122.lcssa, 0
  br i1 %23, label %._crit_edge.thread, label %41

24:                                               ; preds = %40, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %40 ]
  %.02126 = phi i32 [ 0, %.lr.ph.new ], [ %.122.1, %40 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %40 ]
  %25 = getelementptr inbounds double, ptr %14, i64 %indvars.iv
  %26 = load double, ptr %25, align 8, !tbaa !13
  %27 = fcmp oeq double %26, -1.000000e+00
  br i1 %27, label %32, label %28

28:                                               ; preds = %24
  %29 = add nsw i32 %.02126, 1
  %30 = sext i32 %.02126 to i64
  %31 = getelementptr inbounds double, ptr %11, i64 %30
  store double %26, ptr %31, align 8, !tbaa !13
  br label %32

32:                                               ; preds = %24, %28
  %.122 = phi i32 [ %.02126, %24 ], [ %29, %28 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %33 = getelementptr inbounds double, ptr %14, i64 %indvars.iv.next
  %34 = load double, ptr %33, align 8, !tbaa !13
  %35 = fcmp oeq double %34, -1.000000e+00
  br i1 %35, label %40, label %36

36:                                               ; preds = %32
  %37 = add nsw i32 %.122, 1
  %38 = sext i32 %.122 to i64
  %39 = getelementptr inbounds double, ptr %11, i64 %38
  store double %34, ptr %39, align 8, !tbaa !13
  br label %40

40:                                               ; preds = %36, %32
  %.122.1 = phi i32 [ %.122, %32 ], [ %37, %36 ]
  %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 %24, !llvm.loop !14

41:                                               ; preds = %._crit_edge
  %42 = sext i32 %.122.lcssa to i64
  tail call void @qsort(ptr noundef nonnull %11, i64 noundef %42, i64 noundef 8, ptr noundef nonnull @_Z16RNCompareScalarsPKvS0_)
  %43 = sitofp i32 %.122.lcssa to double
  %44 = fmul double %43, %1
  %45 = fdiv double %44, 1.000000e+02
  %46 = fptosi double %45 to i32
  %.not = icmp sgt i32 %.122.lcssa, %46
  %47 = add nsw i32 %.122.lcssa, -1
  %spec.select = select i1 %.not, i32 %46, i32 %47
  %48 = sext i32 %spec.select to i64
  %49 = getelementptr inbounds double, ptr %11, i64 %48
  %50 = load double, ptr %49, align 8, !tbaa !13
  tail call void @_ZdaPv(ptr noundef nonnull %11) #6
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %6, %41, %._crit_edge, %2
  %.1 = phi double [ 0.000000e+00, %2 ], [ %50, %41 ], [ 0.000000e+00, %._crit_edge ], [ 0.000000e+00, %6 ]
  ret double %.1
}

; 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, !10, i64 1032}
!6 = !{!"_ZTS14R3MeshProperty", !7, i64 0, !8, i64 8, !10, i64 1032, !7, i64 1040, !11, i64 1048, !11, i64 1056, !11, i64 1064, !11, i64 1072, !11, i64 1080, !11, i64 1088}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"double", !8, i64 0}
!12 = !{!6, !7, i64 1040}
!13 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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