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

; 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.profiling_context_t = type { [1024 x %struct.profiling_span_t], i64, i64 }
%struct.profiling_span_t = type { [256 x i8], i32, i64, i64, double }
%struct.profiling_stats_t = type { double, double, double, double, double, double, i64 }

@global_profiling_context = external hidden global ptr, align 8

; 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
define dso_local void @geometric_get_profiling_stats(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = icmp ne ptr %5, null
  br i1 %6, label %7, label %10

7:                                                ; preds = %1
  %8 = load ptr, ptr @global_profiling_context, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %11, label %10

10:                                               ; preds = %7, %1
  br label %85

11:                                               ; preds = %7
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %12, i8 0, i64 56, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %76, %11
  %14 = load i64, ptr %3, align 8, !tbaa !9
  %15 = load ptr, ptr @global_profiling_context, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.profiling_context_t, ptr %15, i32 0, i32 2
  %17 = load i64, ptr %16, align 8, !tbaa !11
  %18 = icmp ult i64 %14, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %79

20:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %21 = load ptr, ptr @global_profiling_context, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.profiling_context_t, ptr %21, i32 0, i32 0
  %23 = load i64, ptr %3, align 8, !tbaa !9
  %24 = getelementptr inbounds [1024 x %struct.profiling_span_t], ptr %22, i64 0, i64 %23
  store ptr %24, ptr %4, align 8, !tbaa !5
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.profiling_span_t, ptr %25, i32 0, i32 4
  %27 = load double, ptr %26, align 8, !tbaa !13
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.profiling_stats_t, ptr %28, i32 0, i32 0
  %30 = load double, ptr %29, align 8, !tbaa !17
  %31 = fadd double %30, %27
  store double %31, ptr %29, align 8, !tbaa !17
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.profiling_span_t, ptr %32, i32 0, i32 1
  %34 = load i32, ptr %33, align 8, !tbaa !19
  switch i32 %34, label %67 [
    i32 1, label %35
    i32 2, label %43
    i32 3, label %51
    i32 4, label %59
  ]

35:                                               ; preds = %20
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.profiling_span_t, ptr %36, i32 0, i32 4
  %38 = load double, ptr %37, align 8, !tbaa !13
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.profiling_stats_t, ptr %39, i32 0, i32 1
  %41 = load double, ptr %40, align 8, !tbaa !20
  %42 = fadd double %41, %38
  store double %42, ptr %40, align 8, !tbaa !20
  br label %75

43:                                               ; preds = %20
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.profiling_span_t, ptr %44, i32 0, i32 4
  %46 = load double, ptr %45, align 8, !tbaa !13
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.profiling_stats_t, ptr %47, i32 0, i32 2
  %49 = load double, ptr %48, align 8, !tbaa !21
  %50 = fadd double %49, %46
  store double %50, ptr %48, align 8, !tbaa !21
  br label %75

51:                                               ; preds = %20
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.profiling_span_t, ptr %52, i32 0, i32 4
  %54 = load double, ptr %53, align 8, !tbaa !13
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.profiling_stats_t, ptr %55, i32 0, i32 3
  %57 = load double, ptr %56, align 8, !tbaa !22
  %58 = fadd double %57, %54
  store double %58, ptr %56, align 8, !tbaa !22
  br label %75

59:                                               ; preds = %20
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.profiling_span_t, ptr %60, i32 0, i32 4
  %62 = load double, ptr %61, align 8, !tbaa !13
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.profiling_stats_t, ptr %63, i32 0, i32 4
  %65 = load double, ptr %64, align 8, !tbaa !23
  %66 = fadd double %65, %62
  store double %66, ptr %64, align 8, !tbaa !23
  br label %75

67:                                               ; preds = %20
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.profiling_span_t, ptr %68, i32 0, i32 4
  %70 = load double, ptr %69, align 8, !tbaa !13
  %71 = load ptr, ptr %2, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.profiling_stats_t, ptr %71, i32 0, i32 5
  %73 = load double, ptr %72, align 8, !tbaa !24
  %74 = fadd double %73, %70
  store double %74, ptr %72, align 8, !tbaa !24
  br label %75

75:                                               ; preds = %67, %59, %51, %43, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %76

76:                                               ; preds = %75
  %77 = load i64, ptr %3, align 8, !tbaa !9
  %78 = add i64 %77, 1
  store i64 %78, ptr %3, align 8, !tbaa !9
  br label %13, !llvm.loop !25

79:                                               ; preds = %19
  %80 = load ptr, ptr @global_profiling_context, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.profiling_context_t, ptr %80, i32 0, i32 2
  %82 = load i64, ptr %81, align 8, !tbaa !11
  %83 = load ptr, ptr %2, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.profiling_stats_t, ptr %83, i32 0, i32 6
  store i64 %82, ptr %84, align 8, !tbaa !27
  br label %85

85:                                               ; preds = %79, %10
  ret void
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 294920}
!12 = !{!"", !7, i64 0, !10, i64 294912, !10, i64 294920}
!13 = !{!14, !16, i64 280}
!14 = !{!"", !7, i64 0, !15, i64 256, !10, i64 264, !10, i64 272, !16, i64 280}
!15 = !{!"int", !7, i64 0}
!16 = !{!"double", !7, i64 0}
!17 = !{!18, !16, i64 0}
!18 = !{!"", !16, i64 0, !16, i64 8, !16, i64 16, !16, i64 24, !16, i64 32, !16, i64 40, !10, i64 48}
!19 = !{!14, !15, i64 256}
!20 = !{!18, !16, i64 8}
!21 = !{!18, !16, i64 16}
!22 = !{!18, !16, i64 24}
!23 = !{!18, !16, i64 32}
!24 = !{!18, !16, i64 40}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}
!27 = !{!18, !10, i64 48}


; 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.profiling_span_t = type { [256 x i8], i32, i64, i64, double }

@global_profiling_context = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @geometric_get_profiling_stats(ptr noundef writeonly %0) local_unnamed_addr #0 {
  %2 = icmp ne ptr %0, null
  %3 = load ptr, ptr @global_profiling_context, align 8
  %4 = icmp ne ptr %3, null
  %or.cond = select i1 %2, i1 %4, i1 false
  br i1 %or.cond, label %5, label %46

5:                                                ; preds = %1
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(56) %0, i8 0, i64 56, i1 false)
  %6 = load ptr, ptr @global_profiling_context, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 294920
  %8 = load i64, ptr %7, align 8, !tbaa !9
  %.not = icmp eq i64 %8, 0
  br i1 %.not, label %14, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 32
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = getelementptr inbounds i8, ptr %0, i64 40
  br label %16

._crit_edge:                                      ; preds = %39
  store double %26, ptr %0, align 8, !tbaa !12
  br label %14

14:                                               ; preds = %._crit_edge, %5
  %15 = getelementptr inbounds i8, ptr %0, i64 48
  store i64 %8, ptr %15, align 8, !tbaa !15
  br label %46

16:                                               ; preds = %.lr.ph, %39
  %17 = phi double [ 0.000000e+00, %.lr.ph ], [ %40, %39 ]
  %18 = phi double [ 0.000000e+00, %.lr.ph ], [ %41, %39 ]
  %19 = phi double [ 0.000000e+00, %.lr.ph ], [ %42, %39 ]
  %20 = phi double [ 0.000000e+00, %.lr.ph ], [ %43, %39 ]
  %21 = phi double [ 0.000000e+00, %.lr.ph ], [ %44, %39 ]
  %22 = phi double [ 0.000000e+00, %.lr.ph ], [ %26, %39 ]
  %.021 = phi i64 [ 0, %.lr.ph ], [ %45, %39 ]
  %23 = getelementptr inbounds [1024 x %struct.profiling_span_t], ptr %6, i64 0, i64 %.021
  %24 = getelementptr inbounds i8, ptr %23, i64 280
  %25 = load double, ptr %24, align 8, !tbaa !16
  %26 = fadd double %25, %22
  %27 = getelementptr inbounds i8, ptr %23, i64 256
  %28 = load i32, ptr %27, align 8, !tbaa !19
  switch i32 %28, label %37 [
    i32 1, label %29
    i32 2, label %31
    i32 3, label %33
    i32 4, label %35
  ]

29:                                               ; preds = %16
  %30 = fadd double %25, %18
  store double %30, ptr %12, align 8, !tbaa !20
  br label %39

31:                                               ; preds = %16
  %32 = fadd double %25, %19
  store double %32, ptr %11, align 8, !tbaa !21
  br label %39

33:                                               ; preds = %16
  %34 = fadd double %25, %20
  store double %34, ptr %10, align 8, !tbaa !22
  br label %39

35:                                               ; preds = %16
  %36 = fadd double %25, %21
  store double %36, ptr %9, align 8, !tbaa !23
  br label %39

37:                                               ; preds = %16
  %38 = fadd double %25, %17
  store double %38, ptr %13, align 8, !tbaa !24
  br label %39

39:                                               ; preds = %37, %35, %33, %31, %29
  %40 = phi double [ %38, %37 ], [ %17, %35 ], [ %17, %33 ], [ %17, %31 ], [ %17, %29 ]
  %41 = phi double [ %18, %37 ], [ %18, %35 ], [ %18, %33 ], [ %18, %31 ], [ %30, %29 ]
  %42 = phi double [ %19, %37 ], [ %19, %35 ], [ %19, %33 ], [ %32, %31 ], [ %19, %29 ]
  %43 = phi double [ %20, %37 ], [ %20, %35 ], [ %34, %33 ], [ %20, %31 ], [ %20, %29 ]
  %44 = phi double [ %21, %37 ], [ %36, %35 ], [ %21, %33 ], [ %21, %31 ], [ %21, %29 ]
  %45 = add nuw i64 %.021, 1
  %exitcond.not = icmp eq i64 %45, %8
  br i1 %exitcond.not, label %._crit_edge, label %16, !llvm.loop !25

46:                                               ; preds = %1, %14
  ret void
}

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

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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 nounwind willreturn memory(argmem: write) }

!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, !11, i64 294920}
!10 = !{!"", !7, i64 0, !11, i64 294912, !11, i64 294920}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"", !14, i64 0, !14, i64 8, !14, i64 16, !14, i64 24, !14, i64 32, !14, i64 40, !11, i64 48}
!14 = !{!"double", !7, i64 0}
!15 = !{!13, !11, i64 48}
!16 = !{!17, !14, i64 280}
!17 = !{!"", !7, i64 0, !18, i64 256, !11, i64 264, !11, i64 272, !14, i64 280}
!18 = !{!"int", !7, i64 0}
!19 = !{!17, !18, i64 256}
!20 = !{!13, !14, i64 8}
!21 = !{!13, !14, i64 16}
!22 = !{!13, !14, i64 24}
!23 = !{!13, !14, i64 32}
!24 = !{!13, !14, i64 40}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}


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