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

; 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: nounwind uwtable
define dso_local i64 @qg_tensor_get_size(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %13

10:                                               ; preds = %2
  %11 = load i64, ptr %5, align 8, !tbaa !9
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %10, %2
  store i64 0, ptr %3, align 8
  br label %32

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

15:                                               ; preds = %27, %14
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = load i64, ptr %5, align 8, !tbaa !9
  %18 = icmp ult i64 %16, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %30

20:                                               ; preds = %15
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load i64, ptr %7, align 8, !tbaa !9
  %23 = getelementptr inbounds i64, ptr %21, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = load i64, ptr %6, align 8, !tbaa !9
  %26 = mul i64 %25, %24
  store i64 %26, ptr %6, align 8, !tbaa !9
  br label %27

27:                                               ; preds = %20
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = add i64 %28, 1
  store i64 %29, ptr %7, align 8, !tbaa !9
  br label %15, !llvm.loop !11

30:                                               ; preds = %19
  %31 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %31, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %32

32:                                               ; preds = %30, %13
  %33 = load i64, ptr %3, align 8
  ret i64 %33
}

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

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i64 @qg_tensor_get_size(ptr noundef readonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq i64 %1, 0
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %.loopexit, label %.preheader.preheader

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

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

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %.015 = phi i64 [ 0, %.preheader.preheader.new ], [ %38, %.preheader ]
  %.01014 = phi i64 [ 1, %.preheader.preheader.new ], [ %37, %.preheader ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.7, %.preheader ]
  %7 = getelementptr inbounds i64, ptr %0, i64 %.015
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = mul i64 %8, %.01014
  %10 = or disjoint i64 %.015, 1
  %11 = getelementptr inbounds i64, ptr %0, i64 %10
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = mul i64 %12, %9
  %14 = or disjoint i64 %.015, 2
  %15 = getelementptr inbounds i64, ptr %0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = mul i64 %16, %13
  %18 = or disjoint i64 %.015, 3
  %19 = getelementptr inbounds i64, ptr %0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = mul i64 %20, %17
  %22 = or disjoint i64 %.015, 4
  %23 = getelementptr inbounds i64, ptr %0, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !5
  %25 = mul i64 %24, %21
  %26 = or disjoint i64 %.015, 5
  %27 = getelementptr inbounds i64, ptr %0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = mul i64 %28, %25
  %30 = or disjoint i64 %.015, 6
  %31 = getelementptr inbounds i64, ptr %0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = mul i64 %32, %29
  %34 = or disjoint i64 %.015, 7
  %35 = getelementptr inbounds i64, ptr %0, i64 %34
  %36 = load i64, ptr %35, align 8, !tbaa !5
  %37 = mul i64 %36, %33
  %38 = add nuw i64 %.015, 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 %.loopexit.loopexit.unr-lcssa, label %.preheader, !llvm.loop !9

.loopexit.loopexit.unr-lcssa:                     ; preds = %.preheader, %.preheader.preheader
  %.lcssa.ph = phi i64 [ poison, %.preheader.preheader ], [ %37, %.preheader ]
  %.015.unr = phi i64 [ 0, %.preheader.preheader ], [ %38, %.preheader ]
  %.01014.unr = phi i64 [ 1, %.preheader.preheader ], [ %37, %.preheader ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.preheader.epil

.preheader.epil:                                  ; preds = %.loopexit.loopexit.unr-lcssa, %.preheader.epil
  %.015.epil = phi i64 [ %42, %.preheader.epil ], [ %.015.unr, %.loopexit.loopexit.unr-lcssa ]
  %.01014.epil = phi i64 [ %41, %.preheader.epil ], [ %.01014.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader.epil ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %39 = getelementptr inbounds i64, ptr %0, i64 %.015.epil
  %40 = load i64, ptr %39, align 8, !tbaa !5
  %41 = mul i64 %40, %.01014.epil
  %42 = add nuw i64 %.015.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 %.loopexit, label %.preheader.epil, !llvm.loop !11

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.preheader.epil, %2
  %.011 = phi i64 [ 0, %2 ], [ %.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %41, %.preheader.epil ]
  ret i64 %.011
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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, 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.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}


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