; LLVM IR from PyCee/NUS_library :: src/math (NUS_quaternion)

; 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.NUS_quaternion = type { double, double, double, double }

; Function Attrs: nounwind uwtable
declare dso_local void @nus_quaternion_build(ptr dead_on_unwind noalias writable sret(%struct.NUS_quaternion) align 8, double noundef, double noundef, double noundef, double noundef) #0

; 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #2

; Function Attrs: nounwind uwtable
define dso_local void @nus_quaternion_normalize(ptr dead_on_unwind noalias writable sret(%struct.NUS_quaternion) align 8 %0, ptr noundef byval(%struct.NUS_quaternion) align 8 %1) #0 {
  %3 = alloca double, align 8
  %4 = alloca i32, align 4
  %5 = alloca double, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %6 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 0
  %7 = load double, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 0
  %9 = load double, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 1
  %11 = load double, ptr %10, align 8, !tbaa !10
  %12 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 1
  %13 = load double, ptr %12, align 8, !tbaa !10
  %14 = fmul double %11, %13
  %15 = call double @llvm.fmuladd.f64(double %7, double %9, double %14)
  %16 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 2
  %17 = load double, ptr %16, align 8, !tbaa !11
  %18 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 2
  %19 = load double, ptr %18, align 8, !tbaa !11
  %20 = call double @llvm.fmuladd.f64(double %17, double %19, double %15)
  %21 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 3
  %22 = load double, ptr %21, align 8, !tbaa !12
  %23 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 3
  %24 = load double, ptr %23, align 8, !tbaa !12
  %25 = call double @llvm.fmuladd.f64(double %22, double %24, double %20)
  store double %25, ptr %3, align 8, !tbaa !13
  %26 = load double, ptr %3, align 8, !tbaa !13
  %27 = fcmp oge double 1.000000e-05, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %2
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 8 %1, i64 32, i1 false), !tbaa.struct !14
  store i32 1, ptr %4, align 4
  br label %49

29:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %30 = load double, ptr %3, align 8, !tbaa !13
  %31 = call double @sqrt(double noundef %30) #5
  %32 = fdiv double 1.000000e+00, %31
  store double %32, ptr %5, align 8, !tbaa !13
  %33 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 0
  %34 = load double, ptr %33, align 8, !tbaa !5
  %35 = load double, ptr %5, align 8, !tbaa !13
  %36 = fmul double %34, %35
  %37 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 1
  %38 = load double, ptr %37, align 8, !tbaa !10
  %39 = load double, ptr %5, align 8, !tbaa !13
  %40 = fmul double %38, %39
  %41 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 2
  %42 = load double, ptr %41, align 8, !tbaa !11
  %43 = load double, ptr %5, align 8, !tbaa !13
  %44 = fmul double %42, %43
  %45 = getelementptr inbounds %struct.NUS_quaternion, ptr %1, i32 0, i32 3
  %46 = load double, ptr %45, align 8, !tbaa !12
  %47 = load double, ptr %5, align 8, !tbaa !13
  %48 = fmul double %46, %47
  call void @nus_quaternion_build(ptr dead_on_unwind writable sret(%struct.NUS_quaternion) align 8 %0, double noundef %36, double noundef %40, double noundef %44, double noundef %48)
  store i32 1, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  br label %49

49:                                               ; preds = %29, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

attributes #0 = { nounwind 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #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, !7, i64 0}
!6 = !{!"NUS_quaternion", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24}
!7 = !{!"double", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!6, !7, i64 16}
!12 = !{!6, !7, i64 24}
!13 = !{!7, !7, i64 0}
!14 = !{i64 0, i64 8, !13, i64 8, i64 8, !13, i64 16, i64 8, !13, i64 24, i64 8, !13}


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

%struct.NUS_quaternion = type { double, double, double, double }

; Function Attrs: nounwind uwtable
declare dso_local void @nus_quaternion_build(ptr dead_on_unwind noalias writable sret(%struct.NUS_quaternion) align 8, double noundef, double noundef, double noundef, double noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @nus_quaternion_normalize(ptr dead_on_unwind noalias writable sret(%struct.NUS_quaternion) align 8 %0, ptr nocapture noundef readonly byval(%struct.NUS_quaternion) align 8 %1) local_unnamed_addr #0 {
  %3 = load double, ptr %1, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %1, i64 8
  %5 = load double, ptr %4, align 8, !tbaa !10
  %6 = fmul double %5, %5
  %7 = tail call double @llvm.fmuladd.f64(double %3, double %3, double %6)
  %8 = getelementptr inbounds i8, ptr %1, i64 16
  %9 = load double, ptr %8, align 8, !tbaa !11
  %10 = tail call double @llvm.fmuladd.f64(double %9, double %9, double %7)
  %11 = getelementptr inbounds i8, ptr %1, i64 24
  %12 = load double, ptr %11, align 8, !tbaa !12
  %13 = tail call double @llvm.fmuladd.f64(double %12, double %12, double %10)
  %14 = fcmp ugt double %13, 1.000000e-05
  br i1 %14, label %16, label %15

15:                                               ; preds = %2
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef nonnull align 8 dereferenceable(32) %1, i64 32, i1 false), !tbaa.struct !13
  br label %22

16:                                               ; preds = %2
  %sqrt = tail call double @llvm.sqrt.f64(double %13)
  %17 = fdiv double 1.000000e+00, %sqrt
  %18 = fmul double %3, %17
  %19 = fmul double %5, %17
  %20 = fmul double %9, %17
  %21 = fmul double %12, %17
  tail call void @nus_quaternion_build(ptr dead_on_unwind writable sret(%struct.NUS_quaternion) align 8 %0, double noundef %18, double noundef %19, double noundef %20, double noundef %21)
  br label %22

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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

attributes #0 = { nounwind 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 0}
!6 = !{!"NUS_quaternion", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24}
!7 = !{!"double", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!6, !7, i64 16}
!12 = !{!6, !7, i64 24}
!13 = !{i64 0, i64 8, !14, i64 8, i64 8, !14, i64 16, i64 8, !14, i64 24, i64 8, !14}
!14 = !{!7, !7, i64 0}


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