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

; 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.R3Quaternion = type { [4 x double] }

@RN_EPSILON = external global double, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN12R3Quaternion9NormalizeEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #0 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca double, align 8
  %4 = alloca double, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  store double 0.000000e+00, ptr %3, align 8, !tbaa !9
  %7 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %8 = getelementptr inbounds [4 x double], ptr %7, i64 0, i64 0
  %9 = load double, ptr %8, align 8, !tbaa !9
  %10 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %11 = getelementptr inbounds [4 x double], ptr %10, i64 0, i64 0
  %12 = load double, ptr %11, align 8, !tbaa !9
  %13 = load double, ptr %3, align 8, !tbaa !9
  %14 = call double @llvm.fmuladd.f64(double %9, double %12, double %13)
  store double %14, ptr %3, align 8, !tbaa !9
  %15 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %16 = getelementptr inbounds [4 x double], ptr %15, i64 0, i64 1
  %17 = load double, ptr %16, align 8, !tbaa !9
  %18 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %19 = getelementptr inbounds [4 x double], ptr %18, i64 0, i64 1
  %20 = load double, ptr %19, align 8, !tbaa !9
  %21 = load double, ptr %3, align 8, !tbaa !9
  %22 = call double @llvm.fmuladd.f64(double %17, double %20, double %21)
  store double %22, ptr %3, align 8, !tbaa !9
  %23 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %24 = getelementptr inbounds [4 x double], ptr %23, i64 0, i64 2
  %25 = load double, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %27 = getelementptr inbounds [4 x double], ptr %26, i64 0, i64 2
  %28 = load double, ptr %27, align 8, !tbaa !9
  %29 = load double, ptr %3, align 8, !tbaa !9
  %30 = call double @llvm.fmuladd.f64(double %25, double %28, double %29)
  store double %30, ptr %3, align 8, !tbaa !9
  %31 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %32 = getelementptr inbounds [4 x double], ptr %31, i64 0, i64 3
  %33 = load double, ptr %32, align 8, !tbaa !9
  %34 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %35 = getelementptr inbounds [4 x double], ptr %34, i64 0, i64 3
  %36 = load double, ptr %35, align 8, !tbaa !9
  %37 = load double, ptr %3, align 8, !tbaa !9
  %38 = call double @llvm.fmuladd.f64(double %33, double %36, double %37)
  store double %38, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %39 = load double, ptr %3, align 8, !tbaa !9
  %40 = call double @sqrt(double noundef %39) #5
  store double %40, ptr %4, align 8, !tbaa !9
  %41 = load double, ptr %4, align 8, !tbaa !9
  %42 = load double, ptr @RN_EPSILON, align 8, !tbaa !9
  %43 = call noundef i32 @_Z8RNIsZerodd(double noundef %41, double noundef %42)
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %46

45:                                               ; preds = %1
  store i32 1, ptr %5, align 4
  br label %67

46:                                               ; preds = %1
  %47 = load double, ptr %4, align 8, !tbaa !9
  %48 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %49 = getelementptr inbounds [4 x double], ptr %48, i64 0, i64 0
  %50 = load double, ptr %49, align 8, !tbaa !9
  %51 = fdiv double %50, %47
  store double %51, ptr %49, align 8, !tbaa !9
  %52 = load double, ptr %4, align 8, !tbaa !9
  %53 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %54 = getelementptr inbounds [4 x double], ptr %53, i64 0, i64 1
  %55 = load double, ptr %54, align 8, !tbaa !9
  %56 = fdiv double %55, %52
  store double %56, ptr %54, align 8, !tbaa !9
  %57 = load double, ptr %4, align 8, !tbaa !9
  %58 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %59 = getelementptr inbounds [4 x double], ptr %58, i64 0, i64 2
  %60 = load double, ptr %59, align 8, !tbaa !9
  %61 = fdiv double %60, %57
  store double %61, ptr %59, align 8, !tbaa !9
  %62 = load double, ptr %4, align 8, !tbaa !9
  %63 = getelementptr inbounds %class.R3Quaternion, ptr %6, i32 0, i32 0
  %64 = getelementptr inbounds [4 x double], ptr %63, i64 0, i64 3
  %65 = load double, ptr %64, align 8, !tbaa !9
  %66 = fdiv double %65, %62
  store double %66, ptr %64, align 8, !tbaa !9
  store i32 0, ptr %5, align 4
  br label %67

67:                                               ; preds = %46, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  %68 = load i32, ptr %5, align 4
  switch i32 %68, label %70 [
    i32 0, label %69
    i32 1, label %69
  ]

69:                                               ; preds = %67, %67
  ret void

70:                                               ; preds = %67
  unreachable
}

; 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: nounwind
declare double @sqrt(double noundef) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #3

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 #5 = { 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}


; 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_EPSILON = external local_unnamed_addr global double, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN12R3Quaternion9NormalizeEv(ptr nocapture noundef nonnull align 8 dereferenceable(32) %0) local_unnamed_addr #0 align 2 {
  %2 = load double, ptr %0, align 8, !tbaa !5
  %3 = tail call double @llvm.fmuladd.f64(double %2, double %2, double 0.000000e+00)
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load double, ptr %4, align 8, !tbaa !5
  %6 = tail call double @llvm.fmuladd.f64(double %5, double %5, double %3)
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load double, ptr %7, align 8, !tbaa !5
  %9 = tail call double @llvm.fmuladd.f64(double %8, double %8, double %6)
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  %11 = load double, ptr %10, align 8, !tbaa !5
  %12 = tail call double @llvm.fmuladd.f64(double %11, double %11, double %9)
  %sqrt = tail call double @llvm.sqrt.f64(double %12)
  %13 = load double, ptr @RN_EPSILON, align 8, !tbaa !5
  %14 = tail call noundef i32 @_Z8RNIsZerodd(double noundef %sqrt, double noundef %13)
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %15, label %22

15:                                               ; preds = %1
  %16 = load <2 x double>, ptr %0, align 8, !tbaa !5
  %17 = insertelement <2 x double> poison, double %sqrt, i64 0
  %18 = shufflevector <2 x double> %17, <2 x double> poison, <2 x i32> zeroinitializer
  %19 = fdiv <2 x double> %16, %18
  store <2 x double> %19, ptr %0, align 8, !tbaa !5
  %20 = load <2 x double>, ptr %7, align 8, !tbaa !5
  %21 = fdiv <2 x double> %20, %18
  store <2 x double> %21, ptr %7, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %1, %15
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.sqrt.f64(double) #3

attributes #0 = { 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 nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 #3 = { 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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; Effective O3 passes: AggressiveInstCombinePass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass