; LLVM IR from Dimandragon/non-parametric-dsp :: alglib-cpp/src/ap.ll (ZN11alglib_impl5rmaxrElPKNS_9ae_matrixElPNS_8ae_stateE)

; 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.alglib_impl::ae_matrix" = type { i64, i64, i64, i64, i8, %"struct.alglib_impl::ae_dyn_block", %union.anon }
%"struct.alglib_impl::ae_dyn_block" = type { ptr, ptr, ptr, ptr }
%union.anon = type { ptr }

; 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: mustprogress nounwind uwtable
define dso_local noundef double @_ZN11alglib_impl5rmaxrElPKNS_9ae_matrixElPNS_8ae_stateE(i64 noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #1 {
  %5 = alloca double, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca double, align 8
  %12 = alloca double, align 8
  %13 = alloca i32, align 4
  store i64 %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %14 = load i64, ptr %6, align 8, !tbaa !5
  %15 = icmp sge i64 %14, 16
  br i1 %15, label %16, label %17

16:                                               ; preds = %4
  br label %17

17:                                               ; preds = %16, %4
  %18 = load i64, ptr %6, align 8, !tbaa !5
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  store double 0.000000e+00, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %56

21:                                               ; preds = %17
  %22 = load ptr, ptr %7, align 8, !tbaa !9
  %23 = getelementptr inbounds %"struct.alglib_impl::ae_matrix", ptr %22, i32 0, i32 6
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = load i64, ptr %8, align 8, !tbaa !5
  %26 = getelementptr inbounds ptr, ptr %24, i64 %25
  %27 = load ptr, ptr %26, align 8, !tbaa !9
  %28 = getelementptr inbounds double, ptr %27, i64 0
  %29 = load double, ptr %28, align 8, !tbaa !12
  store double %29, ptr %12, align 8, !tbaa !12
  store i64 1, ptr %10, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %51, %21
  %31 = load i64, ptr %10, align 8, !tbaa !5
  %32 = load i64, ptr %6, align 8, !tbaa !5
  %33 = sub nsw i64 %32, 1
  %34 = icmp sle i64 %31, %33
  br i1 %34, label %35, label %54

35:                                               ; preds = %30
  %36 = load ptr, ptr %7, align 8, !tbaa !9
  %37 = getelementptr inbounds %"struct.alglib_impl::ae_matrix", ptr %36, i32 0, i32 6
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  %39 = load i64, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds ptr, ptr %38, i64 %39
  %41 = load ptr, ptr %40, align 8, !tbaa !9
  %42 = load i64, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds double, ptr %41, i64 %42
  %44 = load double, ptr %43, align 8, !tbaa !12
  store double %44, ptr %11, align 8, !tbaa !12
  %45 = load double, ptr %11, align 8, !tbaa !12
  %46 = load double, ptr %12, align 8, !tbaa !12
  %47 = fcmp ogt double %45, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %35
  %49 = load double, ptr %11, align 8, !tbaa !12
  store double %49, ptr %12, align 8, !tbaa !12
  br label %50

50:                                               ; preds = %48, %35
  br label %51

51:                                               ; preds = %50
  %52 = load i64, ptr %10, align 8, !tbaa !5
  %53 = add nsw i64 %52, 1
  store i64 %53, ptr %10, align 8, !tbaa !5
  br label %30, !llvm.loop !14

54:                                               ; preds = %30
  %55 = load double, ptr %12, align 8, !tbaa !12
  store double %55, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %56

56:                                               ; preds = %54, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %57 = load double, ptr %5, align 8
  ret double %57
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"double", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef double @_ZN11alglib_impl5rmaxrElPKNS_9ae_matrixElPNS_8ae_stateE(i64 noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2, ptr nocapture noundef readnone %3) local_unnamed_addr #0 {
  %5 = icmp eq i64 %0, 0
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %4
  %7 = getelementptr inbounds i8, ptr %1, i64 72
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds ptr, ptr %8, i64 %2
  %10 = load ptr, ptr %9, align 8, !tbaa !8
  %11 = load double, ptr %10, align 8, !tbaa !10
  %.not.not18 = icmp sgt i64 %0, 1
  br i1 %.not.not18, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %6
  %12 = add nsw i64 %0, -1
  %13 = add nsw i64 %0, -2
  %xtraiter = and i64 %12, 3
  %14 = icmp ult i64 %13, 3
  br i1 %14, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %12, -4
  %invariant.gep = getelementptr inbounds i8, ptr %10, i64 8
  %invariant.gep22 = getelementptr inbounds i8, ptr %10, i64 16
  %invariant.gep24 = getelementptr inbounds i8, ptr %10, i64 24
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.020 = phi double [ %11, %.lr.ph.preheader.new ], [ %.1.3, %.lr.ph ]
  %.01519 = phi i64 [ 1, %.lr.ph.preheader.new ], [ %24, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %15 = getelementptr inbounds double, ptr %10, i64 %.01519
  %16 = load double, ptr %15, align 8, !tbaa !10
  %17 = fcmp ogt double %16, %.020
  %.1 = select i1 %17, double %16, double %.020
  %gep = getelementptr inbounds double, ptr %invariant.gep, i64 %.01519
  %18 = load double, ptr %gep, align 8, !tbaa !10
  %19 = fcmp ogt double %18, %.1
  %.1.1 = select i1 %19, double %18, double %.1
  %gep23 = getelementptr inbounds double, ptr %invariant.gep22, i64 %.01519
  %20 = load double, ptr %gep23, align 8, !tbaa !10
  %21 = fcmp ogt double %20, %.1.1
  %.1.2 = select i1 %21, double %20, double %.1.1
  %gep25 = getelementptr inbounds double, ptr %invariant.gep24, i64 %.01519
  %22 = load double, ptr %gep25, align 8, !tbaa !10
  %23 = fcmp ogt double %22, %.1.2
  %.1.3 = select i1 %23, double %22, double %.1.2
  %24 = add nuw nsw i64 %.01519, 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 %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !12

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %.1.lcssa.ph = phi double [ poison, %.lr.ph.preheader ], [ %.1.3, %.lr.ph ]
  %.020.unr = phi double [ %11, %.lr.ph.preheader ], [ %.1.3, %.lr.ph ]
  %.01519.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %24, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil
  %.020.epil = phi double [ %.1.epil, %.lr.ph.epil ], [ %.020.unr, %.loopexit.loopexit.unr-lcssa ]
  %.01519.epil = phi i64 [ %28, %.lr.ph.epil ], [ %.01519.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %25 = getelementptr inbounds double, ptr %10, i64 %.01519.epil
  %26 = load double, ptr %25, align 8, !tbaa !10
  %27 = fcmp ogt double %26, %.020.epil
  %.1.epil = select i1 %27, double %26, double %.020.epil
  %28 = add nuw nsw i64 %.01519.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 %.lr.ph.epil, !llvm.loop !14

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil, %6, %4
  %.014 = phi double [ 0.000000e+00, %4 ], [ %11, %6 ], [ %.1.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %.1.epil, %.lr.ph.epil ]
  ret double %.014
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"double", !6, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.unroll.disable"}


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