; LLVM IR from qc-tum/chemtensor :: src/tensor (qnumber)

; 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 void @qnumber_outer_sum(i32 noundef %0, ptr noalias noundef %1, i64 noundef %2, i32 noundef %3, ptr noalias noundef %4, i64 noundef %5, ptr noalias noundef %6) #1 {
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca i32, align 4
  %17 = alloca i64, align 8
  store i32 %0, ptr %8, align 4, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !9
  store i64 %2, ptr %10, align 8, !tbaa !11
  store i32 %3, ptr %11, align 4, !tbaa !5
  store ptr %4, ptr %12, align 8, !tbaa !9
  store i64 %5, ptr %13, align 8, !tbaa !11
  store ptr %6, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  store i64 0, ptr %15, align 8, !tbaa !11
  br label %18

18:                                               ; preds = %54, %7
  %19 = load i64, ptr %15, align 8, !tbaa !11
  %20 = load i64, ptr %10, align 8, !tbaa !11
  %21 = icmp slt i64 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 2, ptr %16, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  br label %57

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

24:                                               ; preds = %50, %23
  %25 = load i64, ptr %17, align 8, !tbaa !11
  %26 = load i64, ptr %13, align 8, !tbaa !11
  %27 = icmp slt i64 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 5, ptr %16, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #2
  br label %53

29:                                               ; preds = %24
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = load ptr, ptr %9, align 8, !tbaa !9
  %32 = load i64, ptr %15, align 8, !tbaa !11
  %33 = getelementptr inbounds i32, ptr %31, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = mul nsw i32 %30, %34
  %36 = load i32, ptr %11, align 4, !tbaa !5
  %37 = load ptr, ptr %12, align 8, !tbaa !9
  %38 = load i64, ptr %17, align 8, !tbaa !11
  %39 = getelementptr inbounds i32, ptr %37, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = mul nsw i32 %36, %40
  %42 = add nsw i32 %35, %41
  %43 = load ptr, ptr %14, align 8, !tbaa !9
  %44 = load i64, ptr %15, align 8, !tbaa !11
  %45 = load i64, ptr %13, align 8, !tbaa !11
  %46 = mul nsw i64 %44, %45
  %47 = load i64, ptr %17, align 8, !tbaa !11
  %48 = add nsw i64 %46, %47
  %49 = getelementptr inbounds i32, ptr %43, i64 %48
  store i32 %42, ptr %49, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %29
  %51 = load i64, ptr %17, align 8, !tbaa !11
  %52 = add nsw i64 %51, 1
  store i64 %52, ptr %17, align 8, !tbaa !11
  br label %24, !llvm.loop !13

53:                                               ; preds = %28
  br label %54

54:                                               ; preds = %53
  %55 = load i64, ptr %15, align 8, !tbaa !11
  %56 = add nsw i64 %55, 1
  store i64 %56, ptr %15, align 8, !tbaa !11
  br label %18, !llvm.loop !15

57:                                               ; preds = %22
  ret void
}

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


; 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: readwrite) uwtable
define dso_local void @qnumber_outer_sum(i32 noundef %0, ptr noalias nocapture noundef readonly %1, i64 noundef %2, i32 noundef %3, ptr noalias nocapture noundef readonly %4, i64 noundef %5, ptr noalias nocapture noundef writeonly %6) local_unnamed_addr #0 {
  %8 = icmp sgt i64 %2, 0
  %9 = icmp sgt i64 %5, 0
  %or.cond = and i1 %8, %9
  br i1 %or.cond, label %.preheader.us.preheader, label %._crit_edge19

.preheader.us.preheader:                          ; preds = %7
  %min.iters.check = icmp ult i64 %5, 8
  %n.vec = and i64 %5, 9223372036854775800
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %3, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %cmp.n = icmp eq i64 %n.vec, %5
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.01518.us = phi i64 [ %30, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  %10 = getelementptr inbounds i32, ptr %1, i64 %.01518.us
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = mul nsw i32 %11, %0
  %13 = mul nuw nsw i64 %.01518.us, %5
  %14 = getelementptr i32, ptr %6, i64 %13
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.preheader.us
  %broadcast.splatinsert23 = insertelement <4 x i32> poison, i32 %12, i64 0
  %broadcast.splat24 = shufflevector <4 x i32> %broadcast.splatinsert23, <4 x i32> 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 ]
  %15 = getelementptr inbounds i32, ptr %4, i64 %index
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  %wide.load = load <4 x i32>, ptr %15, align 4, !tbaa !5
  %wide.load22 = load <4 x i32>, ptr %16, align 4, !tbaa !5
  %17 = mul nsw <4 x i32> %wide.load, %broadcast.splat
  %18 = mul nsw <4 x i32> %wide.load22, %broadcast.splat
  %19 = add nsw <4 x i32> %17, %broadcast.splat24
  %20 = add nsw <4 x i32> %18, %broadcast.splat24
  %21 = getelementptr i32, ptr %14, i64 %index
  %22 = getelementptr i8, ptr %21, i64 16
  store <4 x i32> %19, ptr %21, align 4, !tbaa !5
  store <4 x i32> %20, ptr %22, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %23 = icmp eq i64 %index.next, %n.vec
  br i1 %23, label %middle.block, label %vector.body, !llvm.loop !9

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

scalar.ph.preheader:                              ; preds = %middle.block, %.preheader.us
  %.017.us.ph = phi i64 [ 0, %.preheader.us ], [ %n.vec, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %.017.us = phi i64 [ %29, %scalar.ph ], [ %.017.us.ph, %scalar.ph.preheader ]
  %24 = getelementptr inbounds i32, ptr %4, i64 %.017.us
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = mul nsw i32 %25, %3
  %27 = add nsw i32 %26, %12
  %28 = getelementptr i32, ptr %14, i64 %.017.us
  store i32 %27, ptr %28, align 4, !tbaa !5
  %29 = add nuw nsw i64 %.017.us, 1
  %exitcond.not = icmp eq i64 %29, %5
  br i1 %exitcond.not, label %._crit_edge.us, label %scalar.ph, !llvm.loop !13

._crit_edge.us:                                   ; preds = %scalar.ph, %middle.block
  %30 = add nuw nsw i64 %.01518.us, 1
  %exitcond21.not = icmp eq i64 %30, %2
  br i1 %exitcond21.not, label %._crit_edge19, label %.preheader.us, !llvm.loop !14

._crit_edge19:                                    ; preds = %._crit_edge.us, %7
  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, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = distinct !{!14, !10}


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