; LLVM IR from niessner/Matterport :: code/gaps/pkgs/R2Shapes/R2Grid.ll (ZNK6R2Grid10PercentileEd)

; 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.R2Grid = type { %class.R2Affine, %class.R2Affine, double, ptr, [2 x i32], i32, i32 }
%class.R2Affine = type <{ %class.R2Transformation, %class.R3Matrix, i32, [4 x i8] }>
%class.R2Transformation = type { ptr }
%class.R3Matrix = type { [3 x [3 x 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 @_ZNK6R2Grid10PercentileEd(ptr noundef nonnull align 8 dereferenceable(208) %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 %class.R2Grid, ptr %12, i32 0, i32 6
  %14 = load i32, ptr %13, align 4, !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 %80

17:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  store i32 0, ptr %6, align 4, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %18 = getelementptr inbounds %class.R2Grid, ptr %12, i32 0, i32 6
  %19 = load i32, ptr %18, align 4, !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 !17
  br label %26

26:                                               ; preds = %53, %17
  %27 = load i32, ptr %8, align 4, !tbaa !17
  %28 = getelementptr inbounds %class.R2Grid, ptr %12, i32 0, i32 6
  %29 = load i32, ptr %28, align 4, !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 %class.R2Grid, ptr %12, i32 0, i32 3
  %34 = load ptr, ptr %33, align 8, !tbaa !18
  %35 = load i32, ptr %8, align 4, !tbaa !17
  %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 une double %38, -3.210000e+02
  br i1 %39, label %40, label %52

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

52:                                               ; preds = %40, %32
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %8, align 4, !tbaa !17
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %8, align 4, !tbaa !17
  br label %26, !llvm.loop !19

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

59:                                               ; preds = %56
  store double -3.210000e+02, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %79

60:                                               ; preds = %56
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = load i32, ptr %6, align 4, !tbaa !17
  %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 double, ptr %5, align 8, !tbaa !9
  %65 = load i32, ptr %6, align 4, !tbaa !17
  %66 = sitofp i32 %65 to double
  %67 = fmul double %64, %66
  %68 = fptosi double %67 to i32
  store i32 %68, ptr %10, align 4, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = load i32, ptr %10, align 4, !tbaa !17
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds double, ptr %69, i64 %71
  %73 = load double, ptr %72, align 8, !tbaa !9
  store double %73, ptr %11, align 8, !tbaa !9
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = icmp eq ptr %74, null
  br i1 %75, label %77, label %76

76:                                               ; preds = %60
  call void @_ZdaPv(ptr noundef %74) #8
  br label %77

77:                                               ; preds = %76, %60
  %78 = load double, ptr %11, align 8, !tbaa !9
  store double %78, 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 %79

79:                                               ; preds = %77, %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 %80

80:                                               ; preds = %79, %16
  %81 = load double, ptr %3, align 8
  ret double %81
}

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, !16, i64 204}
!12 = !{!"_ZTS6R2Grid", !13, i64 0, !13, i64 88, !10, i64 176, !6, i64 184, !7, i64 192, !16, i64 200, !16, i64 204}
!13 = !{!"_ZTS8R2Affine", !14, i64 0, !15, i64 8, !16, i64 80}
!14 = !{!"_ZTS16R2Transformation"}
!15 = !{!"_ZTS8R3Matrix", !7, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!16, !16, i64 0}
!18 = !{!12, !6, i64 184}
!19 = distinct !{!19, !20}
!20 = !{!"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 @_ZNK6R2Grid10PercentileEd(ptr nocapture noundef nonnull readonly align 8 dereferenceable(208) %0, double noundef %1) local_unnamed_addr #2 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 204
  %4 = load i32, ptr %3, align 4, !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 184
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %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
  %.117.lcssa.ph = phi i32 [ poison, %.lr.ph ], [ %.117.1, %40 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %40 ]
  %.01622.unr = phi i32 [ 0, %.lr.ph ], [ %.117.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 !16
  %18 = fcmp une double %17, -3.210000e+02
  br i1 %18, label %19, label %._crit_edge

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

._crit_edge:                                      ; preds = %.epil.preheader, %19, %._crit_edge.unr-lcssa
  %.117.lcssa = phi i32 [ %.117.lcssa.ph, %._crit_edge.unr-lcssa ], [ %20, %19 ], [ %.01622.unr, %.epil.preheader ]
  %23 = icmp eq i32 %.117.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 ]
  %.01622 = phi i32 [ 0, %.lr.ph.new ], [ %.117.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 !16
  %27 = fcmp une double %26, -3.210000e+02
  br i1 %27, label %28, label %32

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

32:                                               ; preds = %24, %28
  %.117 = phi i32 [ %29, %28 ], [ %.01622, %24 ]
  %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 !16
  %35 = fcmp une double %34, -3.210000e+02
  br i1 %35, label %36, label %40

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

40:                                               ; preds = %36, %32
  %.117.1 = phi i32 [ %37, %36 ], [ %.117, %32 ]
  %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 !17

41:                                               ; preds = %._crit_edge
  %42 = sext i32 %.117.lcssa to i64
  tail call void @qsort(ptr noundef nonnull %11, i64 noundef %42, i64 noundef 8, ptr noundef nonnull @_Z16RNCompareScalarsPKvS0_)
  %43 = sitofp i32 %.117.lcssa to double
  %44 = fmul double %43, %1
  %45 = fptosi double %44 to i32
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds double, ptr %11, i64 %46
  %48 = load double, ptr %47, align 8, !tbaa !16
  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 ], [ %48, %41 ], [ -3.210000e+02, %._crit_edge ], [ -3.210000e+02, %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, !12, i64 204}
!6 = !{!"_ZTS6R2Grid", !7, i64 0, !7, i64 88, !13, i64 176, !14, i64 184, !10, i64 192, !12, i64 200, !12, i64 204}
!7 = !{!"_ZTS8R2Affine", !8, i64 0, !9, i64 8, !12, i64 80}
!8 = !{!"_ZTS16R2Transformation"}
!9 = !{!"_ZTS8R3Matrix", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"int", !10, i64 0}
!13 = !{!"double", !10, i64 0}
!14 = !{!"any pointer", !10, i64 0}
!15 = !{!6, !14, i64 184}
!16 = !{!13, !13, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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