; LLVM IR from tsotchke/quantum_geometric_tensor :: src/quantum_geometric/core (complex_arithmetic)

; 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.ComplexFloat = type { float, float }

; 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: nounwind uwtable
declare dso_local float @complex_abs(<2 x float>) #1

; Function Attrs: nounwind
declare float @sqrtf(float noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local float @complex_arg(<2 x float>) #1

; Function Attrs: nounwind
declare float @cosf(float noundef) #2

; Function Attrs: nounwind
declare float @sinf(float noundef) #2

; Function Attrs: nounwind uwtable
define dso_local <2 x float> @complex_sqrt(<2 x float> %0) #1 {
  %2 = alloca %struct.ComplexFloat, align 4
  %3 = alloca %struct.ComplexFloat, align 4
  %4 = alloca float, align 4
  %5 = alloca float, align 4
  %6 = alloca float, align 4
  %7 = alloca float, align 4
  store <2 x float> %0, ptr %3, align 4
  %8 = getelementptr inbounds %struct.ComplexFloat, ptr %3, i32 0, i32 1
  %9 = load float, ptr %8, align 4, !tbaa !5
  %10 = fcmp oeq float %9, 0.000000e+00
  br i1 %10, label %11, label %28

11:                                               ; preds = %1
  %12 = getelementptr inbounds %struct.ComplexFloat, ptr %3, i32 0, i32 0
  %13 = load float, ptr %12, align 4, !tbaa !10
  %14 = fcmp oge float %13, 0.000000e+00
  br i1 %14, label %15, label %21

15:                                               ; preds = %11
  %16 = getelementptr inbounds %struct.ComplexFloat, ptr %2, i32 0, i32 0
  %17 = getelementptr inbounds %struct.ComplexFloat, ptr %3, i32 0, i32 0
  %18 = load float, ptr %17, align 4, !tbaa !10
  %19 = call float @sqrtf(float noundef %18) #3
  store float %19, ptr %16, align 4, !tbaa !10
  %20 = getelementptr inbounds %struct.ComplexFloat, ptr %2, i32 0, i32 1
  store float 0.000000e+00, ptr %20, align 4, !tbaa !5
  br label %47

21:                                               ; preds = %11
  %22 = getelementptr inbounds %struct.ComplexFloat, ptr %2, i32 0, i32 0
  store float 0.000000e+00, ptr %22, align 4, !tbaa !10
  %23 = getelementptr inbounds %struct.ComplexFloat, ptr %2, i32 0, i32 1
  %24 = getelementptr inbounds %struct.ComplexFloat, ptr %3, i32 0, i32 0
  %25 = load float, ptr %24, align 4, !tbaa !10
  %26 = fneg float %25
  %27 = call float @sqrtf(float noundef %26) #3
  store float %27, ptr %23, align 4, !tbaa !5
  br label %47

28:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %29 = load <2 x float>, ptr %3, align 4
  %30 = call float @complex_abs(<2 x float> %29)
  store float %30, ptr %4, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %31 = load <2 x float>, ptr %3, align 4
  %32 = call float @complex_arg(<2 x float> %31)
  store float %32, ptr %5, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %33 = load float, ptr %4, align 4, !tbaa !11
  %34 = call float @sqrtf(float noundef %33) #3
  store float %34, ptr %6, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %35 = load float, ptr %5, align 4, !tbaa !11
  %36 = fmul float %35, 5.000000e-01
  store float %36, ptr %7, align 4, !tbaa !11
  %37 = load float, ptr %6, align 4, !tbaa !11
  %38 = load float, ptr %7, align 4, !tbaa !11
  %39 = call float @cosf(float noundef %38) #3
  %40 = fmul float %37, %39
  %41 = getelementptr inbounds %struct.ComplexFloat, ptr %2, i32 0, i32 0
  store float %40, ptr %41, align 4, !tbaa !10
  %42 = load float, ptr %6, align 4, !tbaa !11
  %43 = load float, ptr %7, align 4, !tbaa !11
  %44 = call float @sinf(float noundef %43) #3
  %45 = fmul float %42, %44
  %46 = getelementptr inbounds %struct.ComplexFloat, ptr %2, i32 0, i32 1
  store float %45, ptr %46, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %47

47:                                               ; preds = %28, %21, %15
  %48 = load <2 x float>, ptr %2, align 4
  ret <2 x float> %48
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="64" "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 = { 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 4}
!6 = !{!"", !7, i64 0, !7, i64 4}
!7 = !{!"float", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!7, !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"

; Function Attrs: nounwind uwtable
declare dso_local float @complex_abs(<2 x float>) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local float @complex_arg(<2 x float>) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local <2 x float> @complex_sqrt(<2 x float> %0) local_unnamed_addr #0 {
  %.sroa.0.4.vec.extract = extractelement <2 x float> %0, i64 1
  %2 = fcmp oeq float %.sroa.0.4.vec.extract, 0.000000e+00
  br i1 %2, label %3, label %10

3:                                                ; preds = %1
  %.sroa.0.0.vec.extract = extractelement <2 x float> %0, i64 0
  %4 = fcmp ult float %.sroa.0.0.vec.extract, 0.000000e+00
  br i1 %4, label %7, label %5

5:                                                ; preds = %3
  %6 = tail call float @sqrtf(float noundef %.sroa.0.0.vec.extract) #2
  %.sroa.012.4.vec.insert = insertelement <2 x float> <float poison, float 0.000000e+00>, float %6, i64 0
  br label %22

7:                                                ; preds = %3
  %8 = fneg float %.sroa.0.0.vec.extract
  %9 = tail call float @sqrtf(float noundef %8) #2
  %.sroa.012.4.vec.insert19 = insertelement <2 x float> <float 0.000000e+00, float poison>, float %9, i64 1
  br label %22

10:                                               ; preds = %1
  %11 = tail call float @complex_abs(<2 x float> %0)
  %12 = tail call float @complex_arg(<2 x float> %0)
  %13 = tail call float @sqrtf(float noundef %11) #2
  %14 = fmul float %12, 5.000000e-01
  %15 = tail call float @cosf(float noundef %14) #2
  %16 = tail call float @sinf(float noundef %14) #2
  %17 = insertelement <2 x float> poison, float %13, i64 0
  %18 = shufflevector <2 x float> %17, <2 x float> poison, <2 x i32> zeroinitializer
  %19 = insertelement <2 x float> poison, float %15, i64 0
  %20 = insertelement <2 x float> %19, float %16, i64 1
  %21 = fmul <2 x float> %18, %20
  br label %22

22:                                               ; preds = %10, %7, %5
  %.sroa.012.0 = phi <2 x float> [ %.sroa.012.4.vec.insert, %5 ], [ %.sroa.012.4.vec.insert19, %7 ], [ %21, %10 ]
  ret <2 x float> %.sroa.012.0
}

attributes #0 = { nounwind uwtable "min-legal-vector-width"="64" "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 = { 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)"}


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