; LLVM IR from openssl/openssl :: crypto/ml_dsa (ml_dsa_matrix)

; 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.poly_st = type { [256 x i32] }
%struct.matrix_st = type { ptr, i64, i64 }
%struct.vector_st = type { ptr, i64 }

; Function Attrs: nounwind uwtable
define dso_local void @ossl_ml_dsa_matrix_mult_vector(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %struct.poly_st, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.matrix_st, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  store ptr %13, ptr %9, align 8, !tbaa !5
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  call void @vector_zero(ptr noundef %14)
  store i64 0, ptr %7, align 8, !tbaa !12
  br label %15

15:                                               ; preds = %50, %3
  %16 = load i64, ptr %7, align 8, !tbaa !12
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.matrix_st, ptr %17, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = icmp ult i64 %16, %19
  br i1 %20, label %21, label %53

21:                                               ; preds = %15
  store i64 0, ptr %8, align 8, !tbaa !12
  br label %22

22:                                               ; preds = %46, %21
  %23 = load i64, ptr %8, align 8, !tbaa !12
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.matrix_st, ptr %24, i32 0, i32 2
  %26 = load i64, ptr %25, align 8, !tbaa !14
  %27 = icmp ult i64 %23, %26
  br i1 %27, label %28, label %49

28:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 1024, ptr %10) #4
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.poly_st, ptr %29, i32 1
  store ptr %30, ptr %9, align 8, !tbaa !5
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.vector_st, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !15
  %34 = load i64, ptr %8, align 8, !tbaa !12
  %35 = getelementptr inbounds %struct.poly_st, ptr %33, i64 %34
  call void @ossl_ml_dsa_poly_ntt_mult(ptr noundef %29, ptr noundef %35, ptr noundef %10)
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.vector_st, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  %39 = load i64, ptr %7, align 8, !tbaa !12
  %40 = getelementptr inbounds %struct.poly_st, ptr %38, i64 %39
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.vector_st, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !15
  %44 = load i64, ptr %7, align 8, !tbaa !12
  %45 = getelementptr inbounds %struct.poly_st, ptr %43, i64 %44
  call void @poly_add(ptr noundef %10, ptr noundef %40, ptr noundef %45)
  call void @llvm.lifetime.end.p0(i64 1024, ptr %10) #4
  br label %46

46:                                               ; preds = %28
  %47 = load i64, ptr %8, align 8, !tbaa !12
  %48 = add i64 %47, 1
  store i64 %48, ptr %8, align 8, !tbaa !12
  br label %22, !llvm.loop !17

49:                                               ; preds = %22
  br label %50

50:                                               ; preds = %49
  %51 = load i64, ptr %7, align 8, !tbaa !12
  %52 = add i64 %51, 1
  store i64 %52, ptr %7, align 8, !tbaa !12
  br label %15, !llvm.loop !19

53:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @vector_zero(ptr noundef) #2

declare void @ossl_ml_dsa_poly_ntt_mult(ptr noundef, ptr noundef, ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @poly_add(ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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


; 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.poly_st = type { [256 x i32] }

; Function Attrs: nounwind uwtable
define dso_local void @ossl_ml_dsa_matrix_mult_vector(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.poly_st, align 4
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  tail call void @vector_zero(ptr noundef %2)
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !11
  %.not = icmp eq i64 %7, 0
  br i1 %.not, label %._crit_edge20, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !12
  %.not21 = icmp eq i64 %9, 0
  br i1 %.not21, label %._crit_edge20, label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %10 = phi i64 [ %20, %._crit_edge ], [ %7, %.preheader.lr.ph ]
  %11 = phi i64 [ %21, %._crit_edge ], [ 1, %.preheader.lr.ph ]
  %.019 = phi ptr [ %.1.lcssa, %._crit_edge ], [ %5, %.preheader.lr.ph ]
  %.01518 = phi i64 [ %22, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %.not22 = icmp eq i64 %11, 0
  br i1 %.not22, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.117 = phi ptr [ %12, %.lr.ph ], [ %.019, %.preheader ]
  %.01416 = phi i64 [ %17, %.lr.ph ], [ 0, %.preheader ]
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %4) #4
  %12 = getelementptr inbounds i8, ptr %.117, i64 1024
  %13 = load ptr, ptr %1, align 8, !tbaa !13
  %14 = getelementptr inbounds %struct.poly_st, ptr %13, i64 %.01416
  call void @ossl_ml_dsa_poly_ntt_mult(ptr noundef %.117, ptr noundef %14, ptr noundef nonnull %4) #4
  %15 = load ptr, ptr %2, align 8, !tbaa !13
  %16 = getelementptr inbounds %struct.poly_st, ptr %15, i64 %.01518
  call void @poly_add(ptr noundef nonnull %4, ptr noundef %16, ptr noundef %16)
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %4) #4
  %17 = add nuw i64 %.01416, 1
  %18 = load i64, ptr %8, align 8, !tbaa !12
  %19 = icmp ult i64 %17, %18
  br i1 %19, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i64, ptr %6, align 8, !tbaa !11
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %20 = phi i64 [ %10, %.preheader ], [ %.pre, %._crit_edge.loopexit ]
  %21 = phi i64 [ 0, %.preheader ], [ %18, %._crit_edge.loopexit ]
  %.1.lcssa = phi ptr [ %.019, %.preheader ], [ %12, %._crit_edge.loopexit ]
  %22 = add nuw i64 %.01518, 1
  %23 = icmp ult i64 %22, %20
  br i1 %23, label %.preheader, label %._crit_edge20, !llvm.loop !17

._crit_edge20:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %3
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @vector_zero(ptr noundef) local_unnamed_addr #2

declare void @ossl_ml_dsa_poly_ntt_mult(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @poly_add(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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, !7, i64 0}
!6 = !{!"matrix_st", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !10, i64 16}
!13 = !{!14, !7, i64 0}
!14 = !{!"vector_st", !7, i64 0, !10, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16, !18}
!18 = !{!"llvm.loop.unswitch.partial.disable"}


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