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

; 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 }

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @complex_matrix_transpose(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  store i64 %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %42, %4
  %13 = load i64, ptr %9, align 8, !tbaa !9
  %14 = load i64, ptr %7, align 8, !tbaa !9
  %15 = icmp ult i64 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %45

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

18:                                               ; preds = %38, %17
  %19 = load i64, ptr %11, align 8, !tbaa !9
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = icmp ult i64 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %41

23:                                               ; preds = %18
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load i64, ptr %11, align 8, !tbaa !9
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = mul i64 %25, %26
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = add i64 %27, %28
  %30 = getelementptr inbounds %struct.ComplexFloat, ptr %24, i64 %29
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load i64, ptr %9, align 8, !tbaa !9
  %33 = load i64, ptr %8, align 8, !tbaa !9
  %34 = mul i64 %32, %33
  %35 = load i64, ptr %11, align 8, !tbaa !9
  %36 = add i64 %34, %35
  %37 = getelementptr inbounds %struct.ComplexFloat, ptr %31, i64 %36
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %30, ptr align 4 %37, i64 8, i1 false), !tbaa.struct !11
  br label %38

38:                                               ; preds = %23
  %39 = load i64, ptr %11, align 8, !tbaa !9
  %40 = add i64 %39, 1
  store i64 %40, ptr %11, align 8, !tbaa !9
  br label %18, !llvm.loop !14

41:                                               ; preds = %22
  br label %42

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

45:                                               ; preds = %16
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree 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 = !{!"long", !7, i64 0}
!11 = !{i64 0, i64 4, !12, i64 4, i64 4, !12}
!12 = !{!13, !13, i64 0}
!13 = !{!"float", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @complex_matrix_transpose(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %.not = icmp eq i64 %2, 0
  %.not19 = icmp eq i64 %3, 0
  %or.cond = or i1 %.not, %.not19
  br i1 %or.cond, label %._crit_edge18, label %.preheader.us.preheader

.preheader.us.preheader:                          ; preds = %4
  %5 = add i64 %3, %2
  %6 = shl i64 %5, 3
  %7 = getelementptr i8, ptr %1, i64 %6
  %scevgep = getelementptr i8, ptr %7, i64 -8
  %8 = mul i64 %3, %2
  %9 = shl i64 %8, 3
  %scevgep22 = getelementptr i8, ptr %0, i64 %9
  %min.iters.check = icmp ugt i64 %3, 7
  %ident.check.not = icmp eq i64 %2, 1
  %or.cond24 = and i1 %min.iters.check, %ident.check.not
  %bound0 = icmp ugt ptr %scevgep22, %1
  %bound1 = icmp ugt ptr %scevgep, %0
  %found.conflict = and i1 %bound0, %bound1
  %.mask = and i64 %3, 1152921504606846976
  %stride.check = icmp ne i64 %.mask, 0
  %10 = or i1 %found.conflict, %stride.check
  %n.vec = and i64 %3, -1152921504606846980
  %cmp.n = icmp eq i64 %n.vec, %3
  %xtraiter = and i64 %3, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.01317.us = phi i64 [ %40, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  %invariant.gep.us = getelementptr %struct.ComplexFloat, ptr %1, i64 %.01317.us
  %11 = mul i64 %.01317.us, %3
  %12 = getelementptr %struct.ComplexFloat, ptr %0, i64 %11
  %or.cond24.not = xor i1 %or.cond24, true
  %brmerge = or i1 %or.cond24.not, %10
  br i1 %brmerge, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.preheader.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.preheader.us ]
  %13 = getelementptr %struct.ComplexFloat, ptr %invariant.gep.us, i64 %index
  %14 = getelementptr %struct.ComplexFloat, ptr %12, i64 %index
  %15 = getelementptr i8, ptr %14, i64 16
  %wide.load = load <2 x i64>, ptr %14, align 4, !alias.scope !5
  %wide.load23 = load <2 x i64>, ptr %15, align 4, !alias.scope !5
  %16 = getelementptr i8, ptr %13, i64 16
  store <2 x i64> %wide.load, ptr %13, align 4, !alias.scope !8, !noalias !5
  store <2 x i64> %wide.load23, ptr %16, align 4, !alias.scope !8, !noalias !5
  %index.next = add nuw i64 %index, 4
  %17 = icmp eq i64 %index.next, %n.vec
  br i1 %17, label %middle.block, label %vector.body, !llvm.loop !10

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %.preheader.us, %middle.block
  %.016.us.ph = phi i64 [ 0, %.preheader.us ], [ %n.vec, %middle.block ]
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %.016.us.prol = phi i64 [ %21, %scalar.ph.prol ], [ %.016.us.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %18 = mul i64 %.016.us.prol, %2
  %gep.us.prol = getelementptr %struct.ComplexFloat, ptr %invariant.gep.us, i64 %18
  %19 = getelementptr %struct.ComplexFloat, ptr %12, i64 %.016.us.prol
  %20 = load i64, ptr %19, align 4
  store i64 %20, ptr %gep.us.prol, align 4
  %21 = add nuw i64 %.016.us.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 %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !14

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %.016.us.unr = phi i64 [ %.016.us.ph, %scalar.ph.preheader ], [ %21, %scalar.ph.prol ]
  %22 = sub i64 %.016.us.ph, %3
  %23 = icmp ugt i64 %22, -4
  br i1 %23, label %._crit_edge.us, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %.016.us = phi i64 [ %39, %scalar.ph ], [ %.016.us.unr, %scalar.ph.prol.loopexit ]
  %24 = mul i64 %.016.us, %2
  %gep.us = getelementptr %struct.ComplexFloat, ptr %invariant.gep.us, i64 %24
  %25 = getelementptr %struct.ComplexFloat, ptr %12, i64 %.016.us
  %26 = load i64, ptr %25, align 4
  store i64 %26, ptr %gep.us, align 4
  %27 = add nuw i64 %.016.us, 1
  %28 = mul i64 %27, %2
  %gep.us.1 = getelementptr %struct.ComplexFloat, ptr %invariant.gep.us, i64 %28
  %29 = getelementptr %struct.ComplexFloat, ptr %12, i64 %27
  %30 = load i64, ptr %29, align 4
  store i64 %30, ptr %gep.us.1, align 4
  %31 = add nuw i64 %.016.us, 2
  %32 = mul i64 %31, %2
  %gep.us.2 = getelementptr %struct.ComplexFloat, ptr %invariant.gep.us, i64 %32
  %33 = getelementptr %struct.ComplexFloat, ptr %12, i64 %31
  %34 = load i64, ptr %33, align 4
  store i64 %34, ptr %gep.us.2, align 4
  %35 = add nuw i64 %.016.us, 3
  %36 = mul i64 %35, %2
  %gep.us.3 = getelementptr %struct.ComplexFloat, ptr %invariant.gep.us, i64 %36
  %37 = getelementptr %struct.ComplexFloat, ptr %12, i64 %35
  %38 = load i64, ptr %37, align 4
  store i64 %38, ptr %gep.us.3, align 4
  %39 = add nuw i64 %.016.us, 4
  %exitcond.not.3 = icmp eq i64 %39, %3
  br i1 %exitcond.not.3, label %._crit_edge.us, label %scalar.ph, !llvm.loop !16

._crit_edge.us:                                   ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %40 = add nuw i64 %.01317.us, 1
  %exitcond21.not = icmp eq i64 %40, %2
  br i1 %exitcond21.not, label %._crit_edge18, label %.preheader.us, !llvm.loop !17

._crit_edge18:                                    ; preds = %._crit_edge.us, %4
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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" }

!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 = distinct !{!6, !7}
!7 = distinct !{!7, !"LVerDomain"}
!8 = !{!9}
!9 = distinct !{!9, !7}
!10 = distinct !{!10, !11, !12, !13}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.unroll.disable"}
!16 = distinct !{!16, !11, !12}
!17 = distinct !{!17, !11}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass