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

; Function Attrs: nounwind uwtable
declare dso_local i32 @calculate_metric(ptr noundef, i64 noundef, i64 noundef, ptr 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: nounwind uwtable
define dso_local i32 @validate_geometric_metric(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca double, align 8
  %10 = alloca i32, align 4
  %11 = alloca double, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %17

14:                                               ; preds = %2
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %18, label %17

17:                                               ; preds = %14, %2
  store i32 -1, ptr %3, align 4
  br label %87

18:                                               ; preds = %14
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  store i8 1, ptr %19, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 0, ptr %6, align 8, !tbaa !11
  br label %20

20:                                               ; preds = %81, %18
  %21 = load i64, ptr %6, align 8, !tbaa !11
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.quantum_geometric_state_t, ptr %22, i32 0, i32 2
  %24 = load i64, ptr %23, align 8, !tbaa !13
  %25 = icmp ult i64 %21, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %20
  store i32 2, ptr %7, align 4
  br label %84

27:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store i64 0, ptr %8, align 8, !tbaa !11
  br label %28

28:                                               ; preds = %75, %27
  %29 = load i64, ptr %8, align 8, !tbaa !11
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.quantum_geometric_state_t, ptr %30, i32 0, i32 2
  %32 = load i64, ptr %31, align 8, !tbaa !13
  %33 = icmp ult i64 %29, %32
  br i1 %33, label %35, label %34

34:                                               ; preds = %28
  store i32 5, ptr %7, align 4
  br label %78

35:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = load i64, ptr %6, align 8, !tbaa !11
  %38 = load i64, ptr %8, align 8, !tbaa !11
  %39 = call i32 @calculate_metric(ptr noundef %36, i64 noundef %37, i64 noundef %38, ptr noundef %9)
  store i32 %39, ptr %10, align 4, !tbaa !16
  %40 = load i32, ptr %10, align 4, !tbaa !16
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %44

42:                                               ; preds = %35
  %43 = load i32, ptr %10, align 4, !tbaa !16
  store i32 %43, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %72

44:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = load i64, ptr %8, align 8, !tbaa !11
  %47 = load i64, ptr %6, align 8, !tbaa !11
  %48 = call i32 @calculate_metric(ptr noundef %45, i64 noundef %46, i64 noundef %47, ptr noundef %11)
  store i32 %48, ptr %10, align 4, !tbaa !16
  %49 = load i32, ptr %10, align 4, !tbaa !16
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %53

51:                                               ; preds = %44
  %52 = load i32, ptr %10, align 4, !tbaa !16
  store i32 %52, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %71

53:                                               ; preds = %44
  %54 = load double, ptr %9, align 8, !tbaa !17
  %55 = load double, ptr %11, align 8, !tbaa !17
  %56 = fsub double %54, %55
  %57 = call double @llvm.fabs.f64(double %56)
  %58 = fcmp ogt double %57, 0x3EB0C6F7A0B5ED8D
  br i1 %58, label %59, label %61

59:                                               ; preds = %53
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  store i8 0, ptr %60, align 1, !tbaa !9
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %71

61:                                               ; preds = %53
  %62 = load i64, ptr %6, align 8, !tbaa !11
  %63 = load i64, ptr %8, align 8, !tbaa !11
  %64 = icmp eq i64 %62, %63
  br i1 %64, label %65, label %70

65:                                               ; preds = %61
  %66 = load double, ptr %9, align 8, !tbaa !17
  %67 = fcmp ole double %66, 0.000000e+00
  br i1 %67, label %68, label %70

68:                                               ; preds = %65
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  store i8 0, ptr %69, align 1, !tbaa !9
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %71

70:                                               ; preds = %65, %61
  store i32 0, ptr %7, align 4
  br label %71

71:                                               ; preds = %70, %68, %59, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %72

72:                                               ; preds = %71, %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  %73 = load i32, ptr %7, align 4
  switch i32 %73, label %78 [
    i32 0, label %74
  ]

74:                                               ; preds = %72
  br label %75

75:                                               ; preds = %74
  %76 = load i64, ptr %8, align 8, !tbaa !11
  %77 = add i64 %76, 1
  store i64 %77, ptr %8, align 8, !tbaa !11
  br label %28, !llvm.loop !19

78:                                               ; preds = %72, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %79 = load i32, ptr %7, align 4
  switch i32 %79, label %84 [
    i32 5, label %80
  ]

80:                                               ; preds = %78
  br label %81

81:                                               ; preds = %80
  %82 = load i64, ptr %6, align 8, !tbaa !11
  %83 = add i64 %82, 1
  store i64 %83, ptr %6, align 8, !tbaa !11
  br label %20, !llvm.loop !21

84:                                               ; preds = %78, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %85 = load i32, ptr %7, align 4
  switch i32 %85, label %89 [
    i32 2, label %86
    i32 1, label %87
  ]

86:                                               ; preds = %84
  store i32 0, ptr %3, align 4
  br label %87

87:                                               ; preds = %86, %84, %17
  %88 = load i32, ptr %3, align 4
  ret i32 %88

89:                                               ; preds = %84
  unreachable
}

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

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 = { 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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !12, i64 16}
!14 = !{!"quantum_geometric_state_t", !15, i64 0, !12, i64 8, !12, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !10, i64 56, !15, i64 60}
!15 = !{!"int", !7, i64 0}
!16 = !{!15, !15, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"double", !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}


; 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 i32 @calculate_metric(ptr noundef, i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @validate_geometric_metric(ptr noundef %0, ptr noundef writeonly %1) local_unnamed_addr #0 {
  %3 = alloca double, align 8
  %4 = alloca double, align 8
  %5 = icmp ne ptr %0, null
  %6 = icmp ne ptr %1, null
  %or.cond = and i1 %5, %6
  br i1 %or.cond, label %7, label %.thread50

7:                                                ; preds = %2
  store i8 1, ptr %1, align 1, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %.not5362.not = icmp eq i64 %9, 0
  br i1 %.not5362.not, label %.thread50, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %7, %._crit_edge
  %.03163 = phi i64 [ %25, %._crit_edge ], [ 0, %7 ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %22
  %.061 = phi i64 [ %23, %22 ], [ 0, %.lr.ph.preheader ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %10 = call i32 @calculate_metric(ptr noundef nonnull %0, i64 noundef %.03163, i64 noundef %.061, ptr noundef nonnull %3)
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %11, label %.thread

.thread:                                          ; preds = %.lr.ph
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  br label %.thread50

11:                                               ; preds = %.lr.ph
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %12 = call i32 @calculate_metric(ptr noundef nonnull %0, i64 noundef %.061, i64 noundef %.03163, ptr noundef nonnull %4)
  %.not40 = icmp eq i32 %12, 0
  br i1 %.not40, label %13, label %.thread45

13:                                               ; preds = %11
  %14 = load double, ptr %3, align 8
  %15 = load double, ptr %4, align 8, !tbaa !14
  %16 = fsub double %14, %15
  %17 = call double @llvm.fabs.f64(double %16)
  %18 = fcmp ogt double %17, 0x3EB0C6F7A0B5ED8D
  br i1 %18, label %.thread45.sink.split, label %19

19:                                               ; preds = %13
  %20 = icmp eq i64 %.03163, %.061
  %21 = fcmp ole double %14, 0.000000e+00
  %or.cond4 = select i1 %20, i1 %21, i1 false
  br i1 %or.cond4, label %.thread45.sink.split, label %22

.thread45.sink.split:                             ; preds = %19, %13
  store i8 0, ptr %1, align 1, !tbaa !5
  br label %.thread45

.thread45:                                        ; preds = %11, %.thread45.sink.split
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  br label %.thread50

22:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  %23 = add nuw i64 %.061, 1
  %24 = load i64, ptr %8, align 8, !tbaa !9
  %.not41 = icmp ult i64 %23, %24
  br i1 %.not41, label %.lr.ph, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %22
  %25 = add nuw i64 %.03163, 1
  %.not53 = icmp ult i64 %25, %24
  br i1 %.not53, label %.lr.ph.preheader, label %.thread50, !llvm.loop !18

.thread50:                                        ; preds = %._crit_edge, %7, %.thread45, %.thread, %2
  %.6 = phi i32 [ -1, %2 ], [ %12, %.thread45 ], [ %10, %.thread ], [ 0, %7 ], [ 0, %._crit_edge ]
  ret i32 %.6
}

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

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 willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 16}
!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 = !{!15, !15, i64 0}
!15 = !{!"double", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17, !19}
!19 = !{!"llvm.loop.unswitch.partial.disable"}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass