; LLVM IR from tsotchke/quantum_geometric_tensor :: src/quantum_geometric/core (simd_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"

@.str.8 = external hidden unnamed_addr constant [50 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
declare dso_local void @simd_complex_multiply(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #2

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

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

18:                                               ; preds = %15
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %27

21:                                               ; preds = %18
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %27

24:                                               ; preds = %21
  %25 = load i64, ptr %10, align 8, !tbaa !9
  %26 = icmp eq i64 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %24, %21, %18, %15, %5
  call void (ptr, ...) @geometric_log_error(ptr noundef @.str.8)
  br label %49

28:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  store i64 1, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %29

29:                                               ; preds = %41, %28
  %30 = load i64, ptr %12, align 8, !tbaa !9
  %31 = load i64, ptr %10, 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 %12) #4
  br label %44

34:                                               ; preds = %29
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = load i64, ptr %12, align 8, !tbaa !9
  %37 = getelementptr inbounds i64, ptr %35, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !9
  %39 = load i64, ptr %11, align 8, !tbaa !9
  %40 = mul i64 %39, %38
  store i64 %40, ptr %11, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %34
  %42 = load i64, ptr %12, align 8, !tbaa !9
  %43 = add i64 %42, 1
  store i64 %43, ptr %12, align 8, !tbaa !9
  br label %29, !llvm.loop !11

44:                                               ; preds = %33
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = load i64, ptr %11, align 8, !tbaa !9
  call void @simd_complex_multiply(ptr noundef %45, ptr noundef %46, ptr noundef %47, i64 noundef %48)
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %49

49:                                               ; preds = %44, %27
  ret void
}

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"="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 = { 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 = { 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 = distinct !{!11, !12}
!12 = !{!"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"

@.str.8 = external hidden unnamed_addr constant [50 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local void @simd_complex_multiply(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @simd_tensor_multiply(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef readonly %3, i64 noundef %4) local_unnamed_addr #2 {
  %6 = insertelement <4 x ptr> poison, ptr %0, i64 0
  %7 = insertelement <4 x ptr> %6, ptr %1, i64 1
  %8 = insertelement <4 x ptr> %7, ptr %2, i64 2
  %9 = insertelement <4 x ptr> %8, ptr %3, i64 3
  %10 = icmp eq <4 x ptr> %9, zeroinitializer
  %11 = icmp eq i64 %4, 0
  %12 = bitcast <4 x i1> %10 to i4
  %13 = icmp ne i4 %12, 0
  %op.rdx = or i1 %13, %11
  br i1 %op.rdx, label %16, label %.preheader.preheader

.preheader.preheader:                             ; preds = %5
  %14 = add i64 %4, -1
  %xtraiter = and i64 %4, 7
  %15 = icmp ult i64 %14, 7
  br i1 %15, label %.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = and i64 %4, -8
  br label %.preheader

16:                                               ; preds = %5
  tail call void (ptr, ...) @geometric_log_error(ptr noundef nonnull @.str.8) #3
  br label %53

.unr-lcssa:                                       ; preds = %.preheader, %.preheader.preheader
  %.lcssa.ph = phi i64 [ poison, %.preheader.preheader ], [ %51, %.preheader ]
  %.030.unr = phi i64 [ 0, %.preheader.preheader ], [ %52, %.preheader ]
  %.02129.unr = phi i64 [ 1, %.preheader.preheader ], [ %51, %.preheader ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.epilog-lcssa, label %.preheader.epil

.preheader.epil:                                  ; preds = %.unr-lcssa, %.preheader.epil
  %.030.epil = phi i64 [ %20, %.preheader.epil ], [ %.030.unr, %.unr-lcssa ]
  %.02129.epil = phi i64 [ %19, %.preheader.epil ], [ %.02129.unr, %.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader.epil ], [ 0, %.unr-lcssa ]
  %17 = getelementptr inbounds i64, ptr %3, i64 %.030.epil
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = mul i64 %18, %.02129.epil
  %20 = add nuw i64 %.030.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.epilog-lcssa, label %.preheader.epil, !llvm.loop !9

.epilog-lcssa:                                    ; preds = %.preheader.epil, %.unr-lcssa
  %.lcssa = phi i64 [ %.lcssa.ph, %.unr-lcssa ], [ %19, %.preheader.epil ]
  tail call void @simd_complex_multiply(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %.lcssa)
  br label %53

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %.030 = phi i64 [ 0, %.preheader.preheader.new ], [ %52, %.preheader ]
  %.02129 = phi i64 [ 1, %.preheader.preheader.new ], [ %51, %.preheader ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.7, %.preheader ]
  %21 = getelementptr inbounds i64, ptr %3, i64 %.030
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = mul i64 %22, %.02129
  %24 = or disjoint i64 %.030, 1
  %25 = getelementptr inbounds i64, ptr %3, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = mul i64 %26, %23
  %28 = or disjoint i64 %.030, 2
  %29 = getelementptr inbounds i64, ptr %3, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %31 = mul i64 %30, %27
  %32 = or disjoint i64 %.030, 3
  %33 = getelementptr inbounds i64, ptr %3, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !5
  %35 = mul i64 %34, %31
  %36 = or disjoint i64 %.030, 4
  %37 = getelementptr inbounds i64, ptr %3, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !5
  %39 = mul i64 %38, %35
  %40 = or disjoint i64 %.030, 5
  %41 = getelementptr inbounds i64, ptr %3, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !5
  %43 = mul i64 %42, %39
  %44 = or disjoint i64 %.030, 6
  %45 = getelementptr inbounds i64, ptr %3, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !5
  %47 = mul i64 %46, %43
  %48 = or disjoint i64 %.030, 7
  %49 = getelementptr inbounds i64, ptr %3, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !5
  %51 = mul i64 %50, %47
  %52 = add nuw i64 %.030, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %.unr-lcssa, label %.preheader, !llvm.loop !11

53:                                               ; preds = %.epilog-lcssa, %16
  ret void
}

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"="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 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalDCEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass