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

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

@RN_TWO_PI = external constant double, align 8

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_Z8RNIsZerodd(double noundef, double noundef) #1

; Function Attrs: nounwind
declare double @sqrt(double noundef) #2

; Function Attrs: nounwind
declare double @exp(double noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6R3Grid5GaussEdi(ptr noundef nonnull align 8 dereferenceable(592) %0, double noundef %1, i32 noundef %2) #3 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca double, align 8
  %6 = alloca i32, align 4
  %7 = alloca double, align 8
  %8 = alloca double, align 8
  %9 = alloca double, align 8
  %10 = alloca double, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca double, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store double %1, ptr %5, align 8, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !11
  %14 = load ptr, ptr %4, align 8
  %15 = load double, ptr %5, align 8, !tbaa !9
  %16 = fcmp oeq double %15, 0.000000e+00
  br i1 %16, label %17, label %18

17:                                               ; preds = %3
  br label %76

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %19 = load double, ptr @RN_TWO_PI, align 8, !tbaa !9
  %20 = call double @sqrt(double noundef %19) #4
  store double %20, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %21 = load double, ptr %7, align 8, !tbaa !9
  %22 = load double, ptr %5, align 8, !tbaa !9
  %23 = fmul double %21, %22
  store double %23, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %24 = load double, ptr %8, align 8, !tbaa !9
  %25 = load double, ptr %8, align 8, !tbaa !9
  %26 = fmul double %24, %25
  %27 = load double, ptr %8, align 8, !tbaa !9
  %28 = fmul double %26, %27
  %29 = fdiv double 1.000000e+00, %28
  store double %29, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %30 = load double, ptr %5, align 8, !tbaa !9
  %31 = fmul double -2.000000e+00, %30
  %32 = load double, ptr %5, align 8, !tbaa !9
  %33 = fmul double %31, %32
  store double %33, ptr %10, align 8, !tbaa !9
  %34 = load double, ptr %10, align 8, !tbaa !9
  %35 = call noundef i32 @_Z8RNIsZerodd(double noundef %34, double noundef 0x3EB0C6F7A0B5ED8D)
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %18
  store i32 1, ptr %11, align 4
  br label %74

38:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !11
  br label %39

39:                                               ; preds = %70, %38
  %40 = load i32, ptr %12, align 4, !tbaa !11
  %41 = getelementptr inbounds %class.R3Grid, ptr %14, i32 0, i32 8
  %42 = load i32, ptr %41, align 4, !tbaa !13
  %43 = icmp slt i32 %40, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %39
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %73

45:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %46 = getelementptr inbounds %class.R3Grid, ptr %14, i32 0, i32 4
  %47 = load ptr, ptr %46, align 8, !tbaa !20
  %48 = load i32, ptr %12, align 4, !tbaa !11
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds double, ptr %47, i64 %49
  %51 = load double, ptr %50, align 8, !tbaa !9
  store double %51, ptr %13, align 8, !tbaa !9
  %52 = load i32, ptr %6, align 4, !tbaa !11
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %58

54:                                               ; preds = %45
  %55 = load double, ptr %13, align 8, !tbaa !9
  %56 = load double, ptr %13, align 8, !tbaa !9
  %57 = fmul double %56, %55
  store double %57, ptr %13, align 8, !tbaa !9
  br label %58

58:                                               ; preds = %54, %45
  %59 = load double, ptr %9, align 8, !tbaa !9
  %60 = load double, ptr %13, align 8, !tbaa !9
  %61 = load double, ptr %10, align 8, !tbaa !9
  %62 = fdiv double %60, %61
  %63 = call double @exp(double noundef %62) #4
  %64 = fmul double %59, %63
  %65 = getelementptr inbounds %class.R3Grid, ptr %14, i32 0, i32 4
  %66 = load ptr, ptr %65, align 8, !tbaa !20
  %67 = load i32, ptr %12, align 4, !tbaa !11
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds double, ptr %66, i64 %68
  store double %64, ptr %69, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  br label %70

70:                                               ; preds = %58
  %71 = load i32, ptr %12, align 4, !tbaa !11
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %12, align 4, !tbaa !11
  br label %39, !llvm.loop !21

73:                                               ; preds = %44
  store i32 0, ptr %11, align 4
  br label %74

74:                                               ; preds = %73, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  %75 = load i32, ptr %11, align 4
  switch i32 %75, label %77 [
    i32 0, label %76
    i32 1, label %76
  ]

76:                                               ; preds = %74, %74, %17
  ret void

77:                                               ; preds = %74
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 #2 = { 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 #3 = { 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 #4 = { 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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 588}
!14 = !{!"_ZTS6R3Grid", !15, i64 0, !15, i64 272, !10, i64 544, !10, i64 552, !6, i64 560, !7, i64 568, !12, i64 580, !12, i64 584, !12, i64 588}
!15 = !{!"_ZTS8R3Affine", !16, i64 0, !17, i64 8, !17, i64 136, !18, i64 264}
!16 = !{!"_ZTS16R3Transformation"}
!17 = !{!"_ZTS8R4Matrix", !7, i64 0}
!18 = !{!"_ZTS7RNFlags", !19, i64 0}
!19 = !{!"long", !7, i64 0}
!20 = !{!14, !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"

@RN_TWO_PI = external local_unnamed_addr constant double, align 8

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_Z8RNIsZerodd(double noundef, double noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @exp(double noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6R3Grid5GaussEdi(ptr nocapture noundef nonnull readonly align 8 dereferenceable(592) %0, double noundef %1, i32 noundef %2) local_unnamed_addr #2 align 2 {
  %4 = fcmp oeq double %1, 0.000000e+00
  br i1 %4, label %.loopexit, label %5

5:                                                ; preds = %3
  %6 = load double, ptr @RN_TWO_PI, align 8, !tbaa !5
  %7 = tail call double @sqrt(double noundef %6) #3
  %8 = fmul double %7, %1
  %9 = fmul double %8, %8
  %10 = fmul double %8, %9
  %11 = fdiv double 1.000000e+00, %10
  %12 = fmul double %1, -2.000000e+00
  %13 = fmul double %12, %1
  %14 = tail call noundef i32 @_Z8RNIsZerodd(double noundef %13, double noundef 0x3EB0C6F7A0B5ED8D)
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %5
  %15 = getelementptr inbounds i8, ptr %0, i64 588
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = icmp sgt i32 %16, 0
  br i1 %17, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader
  %18 = getelementptr inbounds i8, ptr %0, i64 560
  %.not21 = icmp eq i32 %2, 0
  %.pre28 = load ptr, ptr %18, align 8, !tbaa !18
  br i1 %.not21, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %.lr.ph.split.us
  %19 = phi ptr [ %25, %.lr.ph.split.us ], [ %.pre28, %.lr.ph ]
  %indvars.iv25 = phi i64 [ %indvars.iv.next26, %.lr.ph.split.us ], [ 0, %.lr.ph ]
  %20 = getelementptr inbounds double, ptr %19, i64 %indvars.iv25
  %21 = load double, ptr %20, align 8, !tbaa !5
  %22 = fdiv double %21, %13
  %23 = tail call double @exp(double noundef %22) #3
  %24 = fmul double %11, %23
  %25 = load ptr, ptr %18, align 8, !tbaa !18
  %26 = getelementptr inbounds double, ptr %25, i64 %indvars.iv25
  store double %24, ptr %26, align 8, !tbaa !5
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, 1
  %27 = load i32, ptr %15, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = icmp slt i64 %indvars.iv.next26, %28
  br i1 %29, label %.lr.ph.split.us, label %.loopexit, !llvm.loop !19

.lr.ph.split:                                     ; preds = %.lr.ph, %.lr.ph.split
  %30 = phi ptr [ %37, %.lr.ph.split ], [ %.pre28, %.lr.ph ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph.split ], [ 0, %.lr.ph ]
  %31 = getelementptr inbounds double, ptr %30, i64 %indvars.iv
  %32 = load double, ptr %31, align 8, !tbaa !5
  %33 = fmul double %32, %32
  %34 = fdiv double %33, %13
  %35 = tail call double @exp(double noundef %34) #3
  %36 = fmul double %11, %35
  %37 = load ptr, ptr %18, align 8, !tbaa !18
  %38 = getelementptr inbounds double, ptr %37, i64 %indvars.iv
  store double %36, ptr %38, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %39 = load i32, ptr %15, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = icmp slt i64 %indvars.iv.next, %40
  br i1 %41, label %.lr.ph.split, label %.loopexit, !llvm.loop !19

.loopexit:                                        ; preds = %.lr.ph.split, %.lr.ph.split.us, %.preheader, %5, %3
  ret void
}

attributes #0 = { inlinehint 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 #1 = { mustprogress nofree nounwind willreturn memory(write) "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 = { 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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !17, i64 588}
!10 = !{!"_ZTS6R3Grid", !11, i64 0, !11, i64 272, !6, i64 544, !6, i64 552, !16, i64 560, !7, i64 568, !17, i64 580, !17, i64 584, !17, i64 588}
!11 = !{!"_ZTS8R3Affine", !12, i64 0, !13, i64 8, !13, i64 136, !14, i64 264}
!12 = !{!"_ZTS16R3Transformation"}
!13 = !{!"_ZTS8R4Matrix", !7, i64 0}
!14 = !{!"_ZTS7RNFlags", !15, i64 0}
!15 = !{!"long", !7, i64 0}
!16 = !{!"any pointer", !7, i64 0}
!17 = !{!"int", !7, i64 0}
!18 = !{!10, !16, i64 560}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass