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

; 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 }
%struct.quantum_geometric_state_t = type { i32, i64, i64, ptr, ptr, ptr, ptr, i8, i32 }
%struct.geometric_encoding_config_t = type { float, i32, ptr }

; 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: 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) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden <2 x float> @complex_float_multiply(<2 x float>, <2 x float>) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @validate_geometric_encoding(ptr noundef %0, ptr noundef %1, ptr noundef %2) #3 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca %struct.ComplexFloat, align 4
  %9 = alloca i64, align 8
  %10 = alloca %struct.ComplexFloat, align 4
  %11 = alloca %struct.ComplexFloat, align 4
  %12 = alloca %struct.ComplexFloat, align 4
  %13 = alloca i32, align 4
  %14 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %23

17:                                               ; preds = %3
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %23

20:                                               ; preds = %17
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %24, label %23

23:                                               ; preds = %20, %17, %3
  store i32 -1, ptr %4, align 4
  br label %112

24:                                               ; preds = %20
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 1, ptr %25, align 1, !tbaa !9
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.quantum_geometric_state_t, ptr %26, i32 0, i32 7
  %28 = load i8, ptr %27, align 8, !tbaa !11, !range !15, !noundef !16
  %29 = trunc i8 %28 to i1
  br i1 %29, label %30, label %75

30:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  call void @llvm.memset.p0.i64(ptr align 4 %8, i8 0, i64 8, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  store i64 0, ptr %9, align 8, !tbaa !17
  br label %31

31:                                               ; preds = %57, %30
  %32 = load i64, ptr %9, align 8, !tbaa !17
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.quantum_geometric_state_t, ptr %33, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !18
  %36 = icmp ult i64 %32, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  br label %60

38:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.quantum_geometric_state_t, ptr %39, i32 0, i32 3
  %41 = load ptr, ptr %40, align 8, !tbaa !19
  %42 = load i64, ptr %9, align 8, !tbaa !17
  %43 = getelementptr inbounds %struct.ComplexFloat, ptr %41, i64 %42
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.quantum_geometric_state_t, ptr %44, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !19
  %47 = load i64, ptr %9, align 8, !tbaa !17
  %48 = getelementptr inbounds %struct.ComplexFloat, ptr %46, i64 %47
  %49 = load <2 x float>, ptr %48, align 4
  %50 = call <2 x float> @complex_float_conjugate(<2 x float> %49)
  store <2 x float> %50, ptr %12, align 4
  %51 = load <2 x float>, ptr %43, align 4
  %52 = load <2 x float>, ptr %12, align 4
  %53 = call <2 x float> @complex_float_multiply(<2 x float> %51, <2 x float> %52)
  store <2 x float> %53, ptr %11, align 4
  %54 = load <2 x float>, ptr %8, align 4
  %55 = load <2 x float>, ptr %11, align 4
  %56 = call <2 x float> @complex_float_add(<2 x float> %54, <2 x float> %55)
  store <2 x float> %56, ptr %10, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %8, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !20
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  br label %57

57:                                               ; preds = %38
  %58 = load i64, ptr %9, align 8, !tbaa !17
  %59 = add i64 %58, 1
  store i64 %59, ptr %9, align 8, !tbaa !17
  br label %31, !llvm.loop !23

60:                                               ; preds = %37
  %61 = getelementptr inbounds %struct.ComplexFloat, ptr %8, i32 0, i32 0
  %62 = load float, ptr %61, align 4, !tbaa !25
  %63 = fsub float %62, 1.000000e+00
  %64 = call float @llvm.fabs.f32(float %63)
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.geometric_encoding_config_t, ptr %65, i32 0, i32 0
  %67 = load float, ptr %66, align 8, !tbaa !27
  %68 = fcmp ogt float %64, %67
  br i1 %68, label %69, label %71

69:                                               ; preds = %60
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %70, align 1, !tbaa !9
  store i32 0, ptr %4, align 4
  store i32 1, ptr %13, align 4
  br label %72

71:                                               ; preds = %60
  store i32 0, ptr %13, align 4
  br label %72

72:                                               ; preds = %71, %69
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  %73 = load i32, ptr %13, align 4
  switch i32 %73, label %114 [
    i32 0, label %74
    i32 1, label %112
  ]

74:                                               ; preds = %72
  br label %75

75:                                               ; preds = %74, %24
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.geometric_encoding_config_t, ptr %76, i32 0, i32 1
  %78 = load i32, ptr %77, align 4, !tbaa !29
  %79 = and i32 %78, 2
  %80 = icmp ne i32 %79, 0
  br i1 %80, label %81, label %111

81:                                               ; preds = %75
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #6
  store i64 0, ptr %14, align 8, !tbaa !17
  br label %82

82:                                               ; preds = %105, %81
  %83 = load i64, ptr %14, align 8, !tbaa !17
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.quantum_geometric_state_t, ptr %84, i32 0, i32 1
  %86 = load i64, ptr %85, align 8, !tbaa !18
  %87 = icmp ult i64 %83, %86
  br i1 %87, label %89, label %88

88:                                               ; preds = %82
  store i32 5, ptr %13, align 4
  br label %108

89:                                               ; preds = %82
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.quantum_geometric_state_t, ptr %90, i32 0, i32 3
  %92 = load ptr, ptr %91, align 8, !tbaa !19
  %93 = load i64, ptr %14, align 8, !tbaa !17
  %94 = getelementptr inbounds %struct.ComplexFloat, ptr %92, i64 %93
  %95 = load <2 x float>, ptr %94, align 4
  %96 = call float @complex_float_abs(<2 x float> %95)
  %97 = load ptr, ptr %6, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.geometric_encoding_config_t, ptr %97, i32 0, i32 0
  %99 = load float, ptr %98, align 8, !tbaa !27
  %100 = fadd float 1.000000e+00, %99
  %101 = fcmp ogt float %96, %100
  br i1 %101, label %102, label %104

102:                                              ; preds = %89
  %103 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %103, align 1, !tbaa !9
  store i32 0, ptr %4, align 4
  store i32 1, ptr %13, align 4
  br label %108

104:                                              ; preds = %89
  br label %105

105:                                              ; preds = %104
  %106 = load i64, ptr %14, align 8, !tbaa !17
  %107 = add i64 %106, 1
  store i64 %107, ptr %14, align 8, !tbaa !17
  br label %82, !llvm.loop !30

108:                                              ; preds = %102, %88
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #6
  %109 = load i32, ptr %13, align 4
  switch i32 %109, label %114 [
    i32 5, label %110
    i32 1, label %112
  ]

110:                                              ; preds = %108
  br label %111

111:                                              ; preds = %110, %75
  store i32 0, ptr %4, align 4
  br label %112

112:                                              ; preds = %111, %108, %72, %23
  %113 = load i32, ptr %4, align 4
  ret i32 %113

114:                                              ; preds = %108, %72
  unreachable
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden <2 x float> @complex_float_add(<2 x float>, <2 x float>) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden <2 x float> @complex_float_conjugate(<2 x float>) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fabs.f32(float) #5

; Function Attrs: inlinehint nounwind uwtable
declare hidden float @complex_float_abs(<2 x float>) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"_Bool", !7, i64 0}
!11 = !{!12, !10, i64 56}
!12 = !{!"quantum_geometric_state_t", !13, i64 0, !14, i64 8, !14, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !10, i64 56, !13, i64 60}
!13 = !{!"int", !7, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!14, !14, i64 0}
!18 = !{!12, !14, i64 8}
!19 = !{!12, !6, i64 24}
!20 = !{i64 0, i64 4, !21, i64 4, i64 4, !21}
!21 = !{!22, !22, i64 0}
!22 = !{!"float", !7, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!26, !22, i64 0}
!26 = !{!"", !22, i64 0, !22, i64 4}
!27 = !{!28, !22, i64 0}
!28 = !{!"geometric_encoding_config_t", !22, i64 0, !13, i64 4, !6, i64 8}
!29 = !{!28, !13, i64 4}
!30 = distinct !{!30, !24}


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

; Function Attrs: inlinehint nounwind uwtable
declare hidden <2 x float> @complex_float_multiply(<2 x float>, <2 x float>) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @validate_geometric_encoding(ptr noundef readonly %0, ptr noundef readonly %1, ptr noundef writeonly %2) local_unnamed_addr #1 {
  %4 = icmp ne ptr %0, null
  %5 = icmp ne ptr %1, null
  %or.cond = and i1 %4, %5
  %6 = icmp ne ptr %2, null
  %or.cond3 = and i1 %or.cond, %6
  br i1 %or.cond3, label %7, label %.loopexit

7:                                                ; preds = %3
  store i8 1, ptr %2, align 1, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 56
  %9 = load i8, ptr %8, align 8, !tbaa !9, !range !14, !noundef !15
  %10 = trunc nuw i8 %9 to i1
  br i1 %10, label %.preheader41, label %.thread

.preheader41:                                     ; preds = %7
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !16
  %.not46 = icmp eq i64 %12, 0
  br i1 %.not46, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader41
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  br label %18

._crit_edge:                                      ; preds = %18, %.preheader41
  %.sroa.014.0.lcssa = phi <2 x float> [ zeroinitializer, %.preheader41 ], [ %25, %18 ]
  %.sroa.014.0.vec.extract = extractelement <2 x float> %.sroa.014.0.lcssa, i64 0
  %14 = fadd float %.sroa.014.0.vec.extract, -1.000000e+00
  %15 = tail call float @llvm.fabs.f32(float %14)
  %16 = load float, ptr %1, align 8, !tbaa !17
  %17 = fcmp ogt float %15, %16
  br i1 %17, label %.loopexit.sink.split, label %.thread

18:                                               ; preds = %.lr.ph, %18
  %.sroa.014.043 = phi <2 x float> [ zeroinitializer, %.lr.ph ], [ %25, %18 ]
  %.03442 = phi i64 [ 0, %.lr.ph ], [ %26, %18 ]
  %19 = load ptr, ptr %13, align 8, !tbaa !20
  %20 = getelementptr inbounds %struct.ComplexFloat, ptr %19, i64 %.03442
  %21 = load <2 x float>, ptr %20, align 4
  %22 = tail call <2 x float> @complex_float_conjugate(<2 x float> %21)
  %23 = load <2 x float>, ptr %20, align 4
  %24 = tail call <2 x float> @complex_float_multiply(<2 x float> %23, <2 x float> %22)
  %25 = tail call <2 x float> @complex_float_add(<2 x float> %.sroa.014.043, <2 x float> %24)
  %26 = add nuw i64 %.03442, 1
  %27 = load i64, ptr %11, align 8, !tbaa !16
  %28 = icmp ult i64 %26, %27
  br i1 %28, label %18, label %._crit_edge, !llvm.loop !21

.thread:                                          ; preds = %._crit_edge, %7
  %29 = getelementptr inbounds i8, ptr %1, i64 4
  %30 = load i32, ptr %29, align 4, !tbaa !23
  %31 = and i32 %30, 2
  %.not = icmp eq i32 %31, 0
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %.thread
  %32 = getelementptr inbounds i8, ptr %0, i64 8
  %33 = load i64, ptr %32, align 8, !tbaa !16
  %.not47 = icmp eq i64 %33, 0
  br i1 %.not47, label %.loopexit, label %.lr.ph45

.lr.ph45:                                         ; preds = %.preheader
  %34 = getelementptr inbounds i8, ptr %0, i64 24
  br label %39

35:                                               ; preds = %39
  %36 = add nuw i64 %.044, 1
  %37 = load i64, ptr %32, align 8, !tbaa !16
  %38 = icmp ult i64 %36, %37
  br i1 %38, label %39, label %.loopexit, !llvm.loop !24

39:                                               ; preds = %.lr.ph45, %35
  %.044 = phi i64 [ 0, %.lr.ph45 ], [ %36, %35 ]
  %40 = load ptr, ptr %34, align 8, !tbaa !20
  %41 = getelementptr inbounds %struct.ComplexFloat, ptr %40, i64 %.044
  %42 = load <2 x float>, ptr %41, align 4
  %43 = tail call float @complex_float_abs(<2 x float> %42)
  %44 = load float, ptr %1, align 8, !tbaa !17
  %45 = fadd float %44, 1.000000e+00
  %46 = fcmp ogt float %43, %45
  br i1 %46, label %.loopexit.sink.split, label %35

.loopexit.sink.split:                             ; preds = %39, %._crit_edge
  store i8 0, ptr %2, align 1, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %35, %.loopexit.sink.split, %.preheader, %.thread, %3
  %.3 = phi i32 [ -1, %3 ], [ 0, %.thread ], [ 0, %.preheader ], [ 0, %.loopexit.sink.split ], [ 0, %35 ]
  ret i32 %.3
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden <2 x float> @complex_float_add(<2 x float>, <2 x float>) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden <2 x float> @complex_float_conjugate(<2 x float>) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fabs.f32(float) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden float @complex_float_abs(<2 x float>) local_unnamed_addr #0

attributes #0 = { inlinehint 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 = { 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 = { mustprogress 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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 56}
!10 = !{!"quantum_geometric_state_t", !11, i64 0, !12, i64 8, !12, i64 16, !13, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !6, i64 56, !11, i64 60}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = !{!10, !12, i64 8}
!17 = !{!18, !19, i64 0}
!18 = !{!"geometric_encoding_config_t", !19, i64 0, !11, i64 4, !13, i64 8}
!19 = !{!"float", !7, i64 0}
!20 = !{!10, !13, i64 24}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!18, !11, i64 4}
!24 = distinct !{!24, !22}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass