; 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.10 = 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
define dso_local void @simd_tensor_contract(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5, i64 noundef %6, i64 noundef %7, i64 noundef %8) #2 {
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i64, align 8
  %17 = alloca i64, align 8
  %18 = alloca i64, align 8
  %19 = alloca i64, align 8
  %20 = alloca i64, align 8
  %21 = alloca i64, align 8
  %22 = alloca i64, align 8
  %23 = alloca i64, align 8
  store ptr %0, ptr %10, align 8, !tbaa !5
  store ptr %1, ptr %11, align 8, !tbaa !5
  store ptr %2, ptr %12, align 8, !tbaa !5
  store ptr %3, ptr %13, align 8, !tbaa !5
  store ptr %4, ptr %14, align 8, !tbaa !5
  store ptr %5, ptr %15, align 8, !tbaa !5
  store i64 %6, ptr %16, align 8, !tbaa !9
  store i64 %7, ptr %17, align 8, !tbaa !9
  store i64 %8, ptr %18, align 8, !tbaa !9
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %50

26:                                               ; preds = %9
  %27 = load ptr, ptr %11, align 8, !tbaa !5
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %50

29:                                               ; preds = %26
  %30 = load ptr, ptr %12, align 8, !tbaa !5
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %50

32:                                               ; preds = %29
  %33 = load ptr, ptr %13, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %50

35:                                               ; preds = %32
  %36 = load ptr, ptr %14, align 8, !tbaa !5
  %37 = icmp ne ptr %36, null
  br i1 %37, label %38, label %50

38:                                               ; preds = %35
  %39 = load ptr, ptr %15, align 8, !tbaa !5
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %50

41:                                               ; preds = %38
  %42 = load i64, ptr %16, align 8, !tbaa !9
  %43 = icmp eq i64 %42, 0
  br i1 %43, label %50, label %44

44:                                               ; preds = %41
  %45 = load i64, ptr %17, align 8, !tbaa !9
  %46 = icmp eq i64 %45, 0
  br i1 %46, label %50, label %47

47:                                               ; preds = %44
  %48 = load i64, ptr %18, align 8, !tbaa !9
  %49 = icmp eq i64 %48, 0
  br i1 %49, label %50, label %51

50:                                               ; preds = %47, %44, %41, %38, %35, %32, %29, %26, %9
  call void (ptr, ...) @geometric_log_error(ptr noundef @.str.10)
  br label %86

51:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #5
  store i64 1, ptr %19, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %20) #5
  store i64 0, ptr %20, align 8, !tbaa !9
  br label %52

52:                                               ; preds = %67, %51
  %53 = load i64, ptr %20, align 8, !tbaa !9
  %54 = load i64, ptr %16, align 8, !tbaa !9
  %55 = icmp ult i64 %53, %54
  br i1 %55, label %57, label %56

56:                                               ; preds = %52
  call void @llvm.lifetime.end.p0(i64 8, ptr %20) #5
  br label %70

57:                                               ; preds = %52
  %58 = load ptr, ptr %13, align 8, !tbaa !5
  %59 = load ptr, ptr %15, align 8, !tbaa !5
  %60 = load i64, ptr %20, align 8, !tbaa !9
  %61 = getelementptr inbounds i64, ptr %59, i64 %60
  %62 = load i64, ptr %61, align 8, !tbaa !9
  %63 = getelementptr inbounds i64, ptr %58, i64 %62
  %64 = load i64, ptr %63, align 8, !tbaa !9
  %65 = load i64, ptr %19, align 8, !tbaa !9
  %66 = mul i64 %65, %64
  store i64 %66, ptr %19, align 8, !tbaa !9
  br label %67

67:                                               ; preds = %57
  %68 = load i64, ptr %20, align 8, !tbaa !9
  %69 = add i64 %68, 1
  store i64 %69, ptr %20, align 8, !tbaa !9
  br label %52, !llvm.loop !11

70:                                               ; preds = %56
  call void @llvm.lifetime.start.p0(i64 8, ptr %21) #5
  %71 = load i64, ptr %17, align 8, !tbaa !9
  %72 = load i64, ptr %16, align 8, !tbaa !9
  %73 = sub i64 %71, %72
  store i64 %73, ptr %21, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %22) #5
  %74 = load i64, ptr %18, align 8, !tbaa !9
  %75 = load i64, ptr %16, align 8, !tbaa !9
  %76 = sub i64 %74, %75
  store i64 %76, ptr %22, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %23) #5
  store i64 1, ptr %23, align 8, !tbaa !9
  %77 = load i64, ptr %23, align 8, !tbaa !9
  %78 = load i64, ptr %19, align 8, !tbaa !9
  %79 = call i64 @min(i64 noundef %77, i64 noundef %78)
  store i64 %79, ptr %23, align 8, !tbaa !9
  %80 = load ptr, ptr %10, align 8, !tbaa !5
  %81 = load ptr, ptr %11, align 8, !tbaa !5
  %82 = load ptr, ptr %12, align 8, !tbaa !5
  %83 = load i64, ptr %23, align 8, !tbaa !9
  %84 = load i64, ptr %21, align 8, !tbaa !9
  %85 = load i64, ptr %22, align 8, !tbaa !9
  call void @simd_tensor_contract_block(ptr noundef %80, ptr noundef %81, ptr noundef %82, i64 noundef %83, i64 noundef %84, i64 noundef %85)
  call void @llvm.lifetime.end.p0(i64 8, ptr %23) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %22) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %21) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #5
  br label %86

86:                                               ; preds = %70, %50
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @min(i64 noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local void @simd_tensor_contract_block(ptr noundef, ptr noundef, ptr noundef, i64 noundef, i64 noundef, i64 noundef) #4

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 = { inlinehint 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 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 #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 = 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.10 = 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
define dso_local void @simd_tensor_contract(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef readonly %3, ptr noundef readnone %4, ptr noundef readonly %5, i64 noundef %6, i64 noundef %7, i64 noundef %8) local_unnamed_addr #1 {
  %10 = insertelement <4 x ptr> poison, ptr %0, i64 0
  %11 = insertelement <4 x ptr> %10, ptr %1, i64 1
  %12 = insertelement <4 x ptr> %11, ptr %2, i64 2
  %13 = insertelement <4 x ptr> %12, ptr %3, i64 3
  %14 = icmp eq <4 x ptr> %13, zeroinitializer
  %15 = icmp eq ptr %4, null
  %16 = icmp eq ptr %5, null
  %17 = icmp eq i64 %6, 0
  %18 = icmp eq i64 %7, 0
  %19 = icmp eq i64 %8, 0
  %20 = bitcast <4 x i1> %14 to i4
  %21 = icmp ne i4 %20, 0
  %op.rdx = or i1 %21, %15
  %op.rdx58 = or i1 %16, %17
  %op.rdx59 = or i1 %18, %19
  %op.rdx60 = or i1 %op.rdx, %op.rdx58
  %op.rdx61 = or i1 %op.rdx60, %op.rdx59
  br i1 %op.rdx61, label %24, label %.preheader.preheader

.preheader.preheader:                             ; preds = %9
  %22 = add i64 %6, -1
  %xtraiter = and i64 %6, 3
  %23 = icmp ult i64 %22, 3
  br i1 %23, label %.unr-lcssa, label %.preheader.preheader.new

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

24:                                               ; preds = %9
  tail call void (ptr, ...) @geometric_log_error(ptr noundef nonnull @.str.10) #4
  br label %58

.unr-lcssa:                                       ; preds = %.preheader, %.preheader.preheader
  %.lcssa.ph = phi i64 [ poison, %.preheader.preheader ], [ %56, %.preheader ]
  %.057.unr = phi i64 [ 1, %.preheader.preheader ], [ %56, %.preheader ]
  %.04256.unr = phi i64 [ 0, %.preheader.preheader ], [ %57, %.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
  %.057.epil = phi i64 [ %29, %.preheader.epil ], [ %.057.unr, %.unr-lcssa ]
  %.04256.epil = phi i64 [ %30, %.preheader.epil ], [ %.04256.unr, %.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader.epil ], [ 0, %.unr-lcssa ]
  %25 = getelementptr inbounds i64, ptr %5, i64 %.04256.epil
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = getelementptr inbounds i64, ptr %3, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = mul i64 %28, %.057.epil
  %30 = add nuw i64 %.04256.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 ], [ %29, %.preheader.epil ]
  %31 = sub i64 %7, %6
  %32 = sub i64 %8, %6
  %33 = tail call i64 @min(i64 noundef 1, i64 noundef %.lcssa)
  tail call void @simd_tensor_contract_block(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %33, i64 noundef %31, i64 noundef %32)
  br label %58

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %.057 = phi i64 [ 1, %.preheader.preheader.new ], [ %56, %.preheader ]
  %.04256 = phi i64 [ 0, %.preheader.preheader.new ], [ %57, %.preheader ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.3, %.preheader ]
  %34 = getelementptr inbounds i64, ptr %5, i64 %.04256
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = getelementptr inbounds i64, ptr %3, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = mul i64 %37, %.057
  %39 = or disjoint i64 %.04256, 1
  %40 = getelementptr inbounds i64, ptr %5, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = getelementptr inbounds i64, ptr %3, i64 %41
  %43 = load i64, ptr %42, align 8, !tbaa !5
  %44 = mul i64 %43, %38
  %45 = or disjoint i64 %.04256, 2
  %46 = getelementptr inbounds i64, ptr %5, i64 %45
  %47 = load i64, ptr %46, align 8, !tbaa !5
  %48 = getelementptr inbounds i64, ptr %3, i64 %47
  %49 = load i64, ptr %48, align 8, !tbaa !5
  %50 = mul i64 %49, %44
  %51 = or disjoint i64 %.04256, 3
  %52 = getelementptr inbounds i64, ptr %5, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !5
  %54 = getelementptr inbounds i64, ptr %3, i64 %53
  %55 = load i64, ptr %54, align 8, !tbaa !5
  %56 = mul i64 %55, %50
  %57 = add nuw i64 %.04256, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %.unr-lcssa, label %.preheader, !llvm.loop !11

58:                                               ; preds = %.epilog-lcssa, %24
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @min(i64 noundef, i64 noundef) local_unnamed_addr #2

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

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 = { inlinehint 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 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 #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 = !{!"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