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

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

@.str.3 = external hidden unnamed_addr constant [38 x i8], align 1

declare void @geometric_log_error(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 zeroext i1 @qg_tensor_scale(ptr noundef %0, float noundef %1) #2 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca float, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store float %1, ptr %5, align 4, !tbaa !9
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  br i1 %8, label %9, label %14

9:                                                ; preds = %2
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.tensor_t, ptr %10, i32 0, i32 2
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = icmp ne ptr %12, null
  br i1 %13, label %15, label %14

14:                                               ; preds = %9, %2
  call void (ptr, ...) @geometric_log_error(ptr noundef @.str.3)
  store i1 false, ptr %3, align 1
  br label %46

15:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 0, ptr %6, align 8, !tbaa !15
  br label %16

16:                                               ; preds = %42, %15
  %17 = load i64, ptr %6, align 8, !tbaa !15
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.tensor_t, ptr %18, i32 0, i32 4
  %20 = load i64, ptr %19, align 8, !tbaa !16
  %21 = icmp ult i64 %17, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %45

23:                                               ; preds = %16
  %24 = load float, ptr %5, align 4, !tbaa !9
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.tensor_t, ptr %25, i32 0, i32 2
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  %28 = load i64, ptr %6, align 8, !tbaa !15
  %29 = getelementptr inbounds %struct.ComplexFloat, ptr %27, i64 %28
  %30 = getelementptr inbounds %struct.ComplexFloat, ptr %29, i32 0, i32 0
  %31 = load float, ptr %30, align 4, !tbaa !17
  %32 = fmul float %31, %24
  store float %32, ptr %30, align 4, !tbaa !17
  %33 = load float, ptr %5, align 4, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.tensor_t, ptr %34, i32 0, i32 2
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  %37 = load i64, ptr %6, align 8, !tbaa !15
  %38 = getelementptr inbounds %struct.ComplexFloat, ptr %36, i64 %37
  %39 = getelementptr inbounds %struct.ComplexFloat, ptr %38, i32 0, i32 1
  %40 = load float, ptr %39, align 4, !tbaa !19
  %41 = fmul float %40, %33
  store float %41, ptr %39, align 4, !tbaa !19
  br label %42

42:                                               ; preds = %23
  %43 = load i64, ptr %6, align 8, !tbaa !15
  %44 = add i64 %43, 1
  store i64 %44, ptr %6, align 8, !tbaa !15
  br label %16, !llvm.loop !20

45:                                               ; preds = %22
  store i1 true, ptr %3, align 1
  br label %46

46:                                               ; preds = %45, %14
  %47 = load i1, ptr %3, align 1
  ret i1 %47
}

attributes #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 = { 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 = { 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 = !{!"float", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"tensor_t", !13, i64 0, !6, i64 8, !6, i64 16, !14, i64 24, !13, i64 32, !6, i64 40, !14, i64 48, !6, i64 56, !6, i64 64}
!13 = !{!"long", !7, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !13, i64 32}
!17 = !{!18, !10, i64 0}
!18 = !{!"", !10, i64 0, !10, i64 4}
!19 = !{!18, !10, i64 4}
!20 = distinct !{!20, !21}
!21 = !{!"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 }

@.str.3 = external hidden unnamed_addr constant [38 x i8], align 1

declare void @geometric_log_error(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @qg_tensor_scale(ptr noundef readonly %0, float noundef %1) local_unnamed_addr #1 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %37, label %3

3:                                                ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %.not13 = icmp eq ptr %5, null
  br i1 %.not13, label %37, label %.preheader

.preheader:                                       ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 32
  %7 = load i64, ptr %6, align 8, !tbaa !12
  %.not15 = icmp eq i64 %7, 0
  br i1 %.not15, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %min.iters.check = icmp ult i64 %7, 12
  br i1 %min.iters.check, label %.lr.ph.preheader20, label %vector.scevcheck

.lr.ph.preheader20:                               ; preds = %middle.block, %vector.scevcheck, %.lr.ph.preheader
  %.014.ph = phi i64 [ 0, %vector.scevcheck ], [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %8 = sub i64 %7, %.014.ph
  %xtraiter = and i64 %8, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol.preheader

.lr.ph.prol.preheader:                            ; preds = %.lr.ph.preheader20
  %9 = insertelement <2 x float> poison, float %1, i64 0
  %10 = shufflevector <2 x float> %9, <2 x float> poison, <2 x i32> zeroinitializer
  br label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.prol, %.lr.ph.prol.preheader
  %.014.prol = phi i64 [ %14, %.lr.ph.prol ], [ %.014.ph, %.lr.ph.prol.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.prol.preheader ]
  %11 = getelementptr inbounds %struct.ComplexFloat, ptr %5, i64 %.014.prol
  %12 = load <2 x float>, ptr %11, align 4, !tbaa !13
  %13 = fmul <2 x float> %12, %10
  store <2 x float> %13, ptr %11, align 4, !tbaa !13
  %14 = add nuw i64 %.014.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !15

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader20
  %.014.unr = phi i64 [ %.014.ph, %.lr.ph.preheader20 ], [ %14, %.lr.ph.prol ]
  %15 = sub i64 %.014.ph, %7
  %16 = icmp ugt i64 %15, -4
  br i1 %16, label %.loopexit, label %.lr.ph.preheader20.new

.lr.ph.preheader20.new:                           ; preds = %.lr.ph.prol.loopexit
  %17 = insertelement <2 x float> poison, float %1, i64 0
  %18 = shufflevector <2 x float> %17, <2 x float> poison, <2 x i32> zeroinitializer
  %invariant.gep = getelementptr i8, ptr %5, i64 8
  %19 = insertelement <2 x float> poison, float %1, i64 0
  %20 = shufflevector <2 x float> %19, <2 x float> poison, <2 x i32> zeroinitializer
  %invariant.gep21 = getelementptr i8, ptr %5, i64 16
  %21 = insertelement <2 x float> poison, float %1, i64 0
  %22 = shufflevector <2 x float> %21, <2 x float> poison, <2 x i32> zeroinitializer
  %invariant.gep23 = getelementptr i8, ptr %5, i64 24
  %23 = insertelement <2 x float> poison, float %1, i64 0
  %24 = shufflevector <2 x float> %23, <2 x float> poison, <2 x i32> zeroinitializer
  br label %.lr.ph

vector.scevcheck:                                 ; preds = %.lr.ph.preheader
  %25 = add i64 %7, -1
  %mul.result = shl i64 %25, 3
  %26 = getelementptr i8, ptr %5, i64 %mul.result
  %27 = icmp ult ptr %26, %5
  %scevgep = getelementptr i8, ptr %5, i64 4
  %mul.result17 = shl i64 %25, 3
  %mul.overflow18 = icmp ugt i64 %25, 2305843009213693951
  %28 = getelementptr i8, ptr %scevgep, i64 %mul.result17
  %29 = icmp ult ptr %28, %scevgep
  %30 = or i1 %29, %mul.overflow18
  %31 = or i1 %27, %30
  br i1 %31, label %.lr.ph.preheader20, label %vector.ph

vector.ph:                                        ; preds = %vector.scevcheck
  %n.vec = and i64 %7, 4611686018427387902
  %broadcast.splatinsert = insertelement <2 x float> poison, float %1, i64 0
  %32 = shufflevector <2 x float> %broadcast.splatinsert, <2 x float> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %33 = getelementptr inbounds %struct.ComplexFloat, ptr %5, i64 %index
  %34 = load <4 x float>, ptr %33, align 4, !tbaa !13
  %35 = getelementptr inbounds %struct.ComplexFloat, ptr %5, i64 %index
  %interleaved.vec = fmul <4 x float> %34, %32
  store <4 x float> %interleaved.vec, ptr %35, align 4, !tbaa !13
  %index.next = add nuw i64 %index, 2
  %36 = icmp eq i64 %index.next, %n.vec
  br i1 %36, label %middle.block, label %vector.body, !llvm.loop !17

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %7, %n.vec
  br i1 %cmp.n, label %.loopexit, label %.lr.ph.preheader20

37:                                               ; preds = %3, %2
  tail call void (ptr, ...) @geometric_log_error(ptr noundef nonnull @.str.3) #2
  br label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader20.new
  %.014 = phi i64 [ %.014.unr, %.lr.ph.preheader20.new ], [ %47, %.lr.ph ]
  %38 = getelementptr inbounds %struct.ComplexFloat, ptr %5, i64 %.014
  %39 = load <2 x float>, ptr %38, align 4, !tbaa !13
  %40 = fmul <2 x float> %39, %18
  store <2 x float> %40, ptr %38, align 4, !tbaa !13
  %gep = getelementptr %struct.ComplexFloat, ptr %invariant.gep, i64 %.014
  %41 = load <2 x float>, ptr %gep, align 4, !tbaa !13
  %42 = fmul <2 x float> %41, %20
  store <2 x float> %42, ptr %gep, align 4, !tbaa !13
  %gep22 = getelementptr %struct.ComplexFloat, ptr %invariant.gep21, i64 %.014
  %43 = load <2 x float>, ptr %gep22, align 4, !tbaa !13
  %44 = fmul <2 x float> %43, %22
  store <2 x float> %44, ptr %gep22, align 4, !tbaa !13
  %gep24 = getelementptr %struct.ComplexFloat, ptr %invariant.gep23, i64 %.014
  %45 = load <2 x float>, ptr %gep24, align 4, !tbaa !13
  %46 = fmul <2 x float> %45, %24
  store <2 x float> %46, ptr %gep24, align 4, !tbaa !13
  %47 = add nuw i64 %.014, 4
  %exitcond.not.3 = icmp eq i64 %47, %7
  br i1 %exitcond.not.3, label %.loopexit, label %.lr.ph, !llvm.loop !21

.loopexit:                                        ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %.preheader, %37
  %.011 = phi i1 [ false, %37 ], [ true, %.preheader ], [ true, %middle.block ], [ true, %.lr.ph ], [ true, %.lr.ph.prol.loopexit ]
  ret i1 %.011
}

attributes #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 = { 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 = { 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, !10, i64 16}
!6 = !{!"tensor_t", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24, !7, i64 32, !10, i64 40, !11, i64 48, !10, i64 56, !10, i64 64}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!6, !7, i64 32}
!13 = !{!14, !14, i64 0}
!14 = !{!"float", !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.unroll.disable"}
!17 = distinct !{!17, !18, !19, !20}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !18, !19}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalDCEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass,VectorCombinePass