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

; 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 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 i32 @geometric_tensor_scale(ptr noundef %0, ptr noundef %1, <2 x float> %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca %struct.ComplexFloat, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store <2 x float> %2, ptr %5, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  br label %90

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

19:                                               ; preds = %16
  store i32 -1, ptr %4, align 4
  br label %90

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

29:                                               ; preds = %68, %20
  %30 = load i64, ptr %11, align 8, !tbaa !9
  %31 = load i64, ptr %9, align 8, !tbaa !9
  %32 = icmp ult i64 %30, %31
  br i1 %32, label %34, label %33

33:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %71

34:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %35 = load i64, ptr %11, align 8, !tbaa !9
  %36 = mul i64 %35, 1024
  store i64 %36, ptr %12, align 8, !tbaa !9
  %37 = load i64, ptr %11, align 8, !tbaa !9
  %38 = add i64 %37, 1
  %39 = load i64, ptr %9, align 8, !tbaa !9
  %40 = icmp ult i64 %38, %39
  br i1 %40, label %41, label %56

41:                                               ; preds = %34
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %42, i32 0, i32 3
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = load i64, ptr %11, align 8, !tbaa !9
  %46 = add i64 %45, 1
  %47 = mul i64 %46, 1024
  %48 = getelementptr inbounds %struct.ComplexFloat, ptr %44, i64 %47
  call void @llvm.prefetch.p0(ptr %48, i32 0, i32 3, i32 1)
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %49, i32 0, i32 3
  %51 = load ptr, ptr %50, align 8, !tbaa !15
  %52 = load i64, ptr %11, align 8, !tbaa !9
  %53 = add i64 %52, 1
  %54 = mul i64 %53, 1024
  %55 = getelementptr inbounds %struct.ComplexFloat, ptr %51, i64 %54
  call void @llvm.prefetch.p0(ptr %55, i32 1, i32 3, i32 1)
  br label %56

56:                                               ; preds = %41, %34
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %57, i32 0, i32 3
  %59 = load ptr, ptr %58, align 8, !tbaa !15
  %60 = load i64, ptr %12, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.ComplexFloat, ptr %59, i64 %60
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %62, i32 0, i32 3
  %64 = load ptr, ptr %63, align 8, !tbaa !15
  %65 = load i64, ptr %12, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.ComplexFloat, ptr %64, i64 %65
  %67 = load <2 x float>, ptr %5, align 4
  call void @simd_tensor_scale(ptr noundef %61, ptr noundef %66, <2 x float> %67, i64 noundef 1024)
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %68

68:                                               ; preds = %56
  %69 = load i64, ptr %11, align 8, !tbaa !9
  %70 = add i64 %69, 1
  store i64 %70, ptr %11, align 8, !tbaa !9
  br label %29, !llvm.loop !16

71:                                               ; preds = %33
  %72 = load i64, ptr %10, align 8, !tbaa !9
  %73 = icmp ugt i64 %72, 0
  br i1 %73, label %74, label %89

74:                                               ; preds = %71
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %75, i32 0, i32 3
  %77 = load ptr, ptr %76, align 8, !tbaa !15
  %78 = load i64, ptr %9, align 8, !tbaa !9
  %79 = mul i64 %78, 1024
  %80 = getelementptr inbounds %struct.ComplexFloat, ptr %77, i64 %79
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.quantum_geometric_tensor_t, ptr %81, i32 0, i32 3
  %83 = load ptr, ptr %82, align 8, !tbaa !15
  %84 = load i64, ptr %9, align 8, !tbaa !9
  %85 = mul i64 %84, 1024
  %86 = getelementptr inbounds %struct.ComplexFloat, ptr %83, i64 %85
  %87 = load i64, ptr %10, align 8, !tbaa !9
  %88 = load <2 x float>, ptr %5, align 4
  call void @simd_tensor_scale(ptr noundef %80, ptr noundef %86, <2 x float> %88, i64 noundef %87)
  br label %89

89:                                               ; preds = %74, %71
  store i32 0, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %90

90:                                               ; preds = %89, %19, %15
  %91 = load i32, ptr %4, align 4
  ret i32 %91
}

declare void @simd_tensor_scale(ptr noundef, ptr noundef, <2 x float>, i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite) }
attributes #2 = { 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 = { "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, !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 = !{!12, !6, i64 24}
!16 = distinct !{!16, !17}
!17 = !{!"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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @llvm.prefetch.p0(ptr nocapture readonly, i32 immarg, i32 immarg, i32 immarg) #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @geometric_tensor_scale(ptr noundef readonly %0, ptr noundef readonly %1, <2 x float> %2) local_unnamed_addr #1 {
  %4 = icmp eq ptr %0, null
  %5 = icmp eq ptr %1, null
  %or.cond = or i1 %4, %5
  br i1 %or.cond, label %32, label %6

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

.lr.ph:                                           ; preds = %6
  %11 = getelementptr inbounds i8, ptr %1, i64 24
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  br label %13

._crit_edge:                                      ; preds = %21, %6
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %32, label %24

13:                                               ; preds = %.lr.ph, %21
  %.02531 = phi i64 [ 0, %.lr.ph ], [ %15, %21 ]
  %14 = shl nuw i64 %.02531, 10
  %15 = add nuw nsw i64 %.02531, 1
  %16 = icmp ult i64 %15, %9
  %.pre = load ptr, ptr %12, align 8, !tbaa !13
  %.pre33 = load ptr, ptr %11, align 8, !tbaa !13
  br i1 %16, label %17, label %21

17:                                               ; preds = %13
  %18 = shl nuw i64 %15, 10
  %19 = getelementptr inbounds %struct.ComplexFloat, ptr %.pre33, i64 %18
  tail call void @llvm.prefetch.p0(ptr nonnull %19, i32 0, i32 3, i32 1)
  %20 = getelementptr inbounds %struct.ComplexFloat, ptr %.pre, i64 %18
  tail call void @llvm.prefetch.p0(ptr nonnull %20, i32 1, i32 3, i32 1)
  br label %21

21:                                               ; preds = %17, %13
  %22 = getelementptr inbounds %struct.ComplexFloat, ptr %.pre, i64 %14
  %23 = getelementptr inbounds %struct.ComplexFloat, ptr %.pre33, i64 %14
  tail call void @simd_tensor_scale(ptr noundef %22, ptr noundef %23, <2 x float> %2, i64 noundef 1024) #3
  %exitcond.not = icmp eq i64 %15, %9
  br i1 %exitcond.not, label %._crit_edge, label %13, !llvm.loop !14

24:                                               ; preds = %._crit_edge
  %25 = getelementptr inbounds i8, ptr %0, i64 24
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = and i64 %8, -1024
  %28 = getelementptr inbounds %struct.ComplexFloat, ptr %26, i64 %27
  %29 = getelementptr inbounds i8, ptr %1, i64 24
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = getelementptr inbounds %struct.ComplexFloat, ptr %30, i64 %27
  tail call void @simd_tensor_scale(ptr noundef %28, ptr noundef %31, <2 x float> %2, i64 noundef %10) #3
  br label %32

32:                                               ; preds = %._crit_edge, %24, %3
  %.0 = phi i32 [ -1, %3 ], [ 0, %24 ], [ 0, %._crit_edge ]
  ret i32 %.0
}

declare void @simd_tensor_scale(ptr noundef, ptr noundef, <2 x float>, i64 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite, inaccessiblemem: 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 = { "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, !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"}


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