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

; 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_tensor_t = type { i32, ptr, i64, ptr, ptr, i8, i8, i8, i32, i64, i64 }
%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
declare float @sqrtf(float noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @llvm.prefetch.p0(ptr nocapture readonly, i32 immarg, i32 immarg, i32 immarg) #2

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

16:                                               ; preds = %2
  store i32 -1, ptr %3, align 4
  br label %84

17:                                               ; preds = %2
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

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

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store i64 1024, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %22, i32 0, i32 10
  %24 = load i64, ptr %23, align 8, !tbaa !11
  %25 = udiv i64 %24, 1024
  store i64 %25, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %26, i32 0, i32 10
  %28 = load i64, ptr %27, align 8, !tbaa !11
  %29 = urem i64 %28, 1024
  store i64 %29, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store float 0.000000e+00, ptr %9, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %61, %21
  %31 = load i64, ptr %10, align 8, !tbaa !9
  %32 = load i64, ptr %7, align 8, !tbaa !9
  %33 = icmp ult i64 %31, %32
  br i1 %33, label %35, label %34

34:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %64

35:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %36 = load i64, ptr %10, align 8, !tbaa !9
  %37 = mul i64 %36, 1024
  store i64 %37, ptr %11, align 8, !tbaa !9
  %38 = load i64, ptr %10, align 8, !tbaa !9
  %39 = add i64 %38, 1
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = icmp ult i64 %39, %40
  br i1 %41, label %42, label %50

42:                                               ; preds = %35
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %43, i32 0, i32 3
  %45 = load ptr, ptr %44, align 8, !tbaa !17
  %46 = load i64, ptr %10, align 8, !tbaa !9
  %47 = add i64 %46, 1
  %48 = mul i64 %47, 1024
  %49 = getelementptr inbounds %struct.ComplexFloat, ptr %45, i64 %48
  call void @llvm.prefetch.p0(ptr %49, i32 0, i32 3, i32 1)
  br label %50

50:                                               ; preds = %42, %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %51, i32 0, i32 3
  %53 = load ptr, ptr %52, align 8, !tbaa !17
  %54 = load i64, ptr %11, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.ComplexFloat, ptr %53, i64 %54
  %56 = call double @simd_tensor_norm(ptr noundef %55, i64 noundef 1024)
  %57 = fptrunc double %56 to float
  store float %57, ptr %12, align 4, !tbaa !15
  %58 = load float, ptr %12, align 4, !tbaa !15
  %59 = load float, ptr %9, align 4, !tbaa !15
  %60 = fadd float %59, %58
  store float %60, ptr %9, align 4, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  br label %61

61:                                               ; preds = %50
  %62 = load i64, ptr %10, align 8, !tbaa !9
  %63 = add i64 %62, 1
  store i64 %63, ptr %10, align 8, !tbaa !9
  br label %30, !llvm.loop !18

64:                                               ; preds = %34
  %65 = load i64, ptr %8, align 8, !tbaa !9
  %66 = icmp ugt i64 %65, 0
  br i1 %66, label %67, label %80

67:                                               ; preds = %64
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %68, i32 0, i32 3
  %70 = load ptr, ptr %69, align 8, !tbaa !17
  %71 = load i64, ptr %7, align 8, !tbaa !9
  %72 = mul i64 %71, 1024
  %73 = getelementptr inbounds %struct.ComplexFloat, ptr %70, i64 %72
  %74 = load i64, ptr %8, align 8, !tbaa !9
  %75 = call double @simd_tensor_norm(ptr noundef %73, i64 noundef %74)
  %76 = fptrunc double %75 to float
  store float %76, ptr %13, align 4, !tbaa !15
  %77 = load float, ptr %13, align 4, !tbaa !15
  %78 = load float, ptr %9, align 4, !tbaa !15
  %79 = fadd float %78, %77
  store float %79, ptr %9, align 4, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  br label %80

80:                                               ; preds = %67, %64
  %81 = load float, ptr %9, align 4, !tbaa !15
  %82 = call float @sqrtf(float noundef %81) #5
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  store float %82, ptr %83, align 4, !tbaa !15
  store i32 0, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %84

84:                                               ; preds = %80, %20, %16
  %85 = load i32, ptr %3, align 4
  ret i32 %85
}

declare double @simd_tensor_norm(ptr noundef, i64 noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite) }
attributes #3 = { 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 #4 = { "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, !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 56}
!12 = !{!"quantum_geometric_tensor_t", !13, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !6, i64 32, !14, i64 40, !14, i64 41, !14, i64 42, !13, i64 44, !10, i64 48, !10, i64 56}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"float", !7, i64 0}
!17 = !{!12, !6, i64 24}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; 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: mustprogress nofree nounwind willreturn memory(write)
declare float @sqrtf(float noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @llvm.prefetch.p0(ptr nocapture readonly, i32 immarg, i32 immarg, i32 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @geometric_tensor_norm(ptr noundef writeonly %0, ptr noundef readonly %1) local_unnamed_addr #2 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq ptr %1, null
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %31, label %5

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %1, i64 56
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = lshr i64 %7, 10
  %9 = and i64 %7, 1023
  %.not35 = icmp ult i64 %7, 1024
  br i1 %.not35, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %10 = getelementptr inbounds i8, ptr %1, i64 24
  br label %11

._crit_edge:                                      ; preds = %16, %5
  %.024.lcssa = phi float [ 0.000000e+00, %5 ], [ %20, %16 ]
  %.not = icmp eq i64 %9, 0
  br i1 %.not, label %29, label %21

11:                                               ; preds = %.lr.ph, %16
  %.02434 = phi float [ 0.000000e+00, %.lr.ph ], [ %20, %16 ]
  %.02533 = phi i64 [ 0, %.lr.ph ], [ %12, %16 ]
  %12 = add nuw nsw i64 %.02533, 1
  %13 = icmp ult i64 %12, %8
  %.pre = load ptr, ptr %10, align 8, !tbaa !13
  br i1 %13, label %14, label %16

14:                                               ; preds = %11
  %.idx = shl i64 %12, 13
  %15 = getelementptr inbounds i8, ptr %.pre, i64 %.idx
  tail call void @llvm.prefetch.p0(ptr %15, i32 0, i32 3, i32 1)
  br label %16

16:                                               ; preds = %14, %11
  %.idx32 = shl i64 %.02533, 13
  %17 = getelementptr inbounds i8, ptr %.pre, i64 %.idx32
  %18 = tail call double @simd_tensor_norm(ptr noundef %17, i64 noundef 1024) #4
  %19 = fptrunc double %18 to float
  %20 = fadd float %.02434, %19
  %exitcond.not = icmp eq i64 %12, %8
  br i1 %exitcond.not, label %._crit_edge, label %11, !llvm.loop !14

21:                                               ; preds = %._crit_edge
  %22 = getelementptr inbounds i8, ptr %1, i64 24
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = and i64 %7, -1024
  %25 = getelementptr inbounds %struct.ComplexFloat, ptr %23, i64 %24
  %26 = tail call double @simd_tensor_norm(ptr noundef %25, i64 noundef %9) #4
  %27 = fptrunc double %26 to float
  %28 = fadd float %.024.lcssa, %27
  br label %29

29:                                               ; preds = %21, %._crit_edge
  %.1 = phi float [ %28, %21 ], [ %.024.lcssa, %._crit_edge ]
  %30 = tail call float @sqrtf(float noundef %.1) #4
  store float %30, ptr %0, align 4, !tbaa !16
  br label %31

31:                                               ; preds = %2, %29
  %.0 = phi i32 [ 0, %29 ], [ -1, %2 ]
  ret i32 %.0
}

declare double @simd_tensor_norm(ptr noundef, i64 noundef) local_unnamed_addr #3

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite) }
attributes #2 = { 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 #3 = { "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 = { 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, !11, i64 56}
!6 = !{!"quantum_geometric_tensor_t", !7, i64 0, !10, i64 8, !11, i64 16, !10, i64 24, !10, i64 32, !12, i64 40, !12, i64 41, !12, i64 42, !7, i64 44, !11, i64 48, !11, i64 56}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!6, !10, i64 24}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !17, i64 0}
!17 = !{!"float", !8, i64 0}


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