; LLVM IR from jgaeddert/liquid-dsp :: src/optim/src (gradsearch)

; 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 @gradsearch_gradient(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, float noundef %4, ptr noundef %5) #1 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca float, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca float, align 4
  %16 = alloca float, align 4
  %17 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !9
  store float %4, ptr %11, align 4, !tbaa !11
  store ptr %5, ptr %12, align 8, !tbaa !5
  %18 = load i32, ptr %10, align 4, !tbaa !9
  %19 = zext i32 %18 to i64
  %20 = call ptr @llvm.stacksave.p0()
  store ptr %20, ptr %13, align 8
  %21 = alloca float, i64 %19, align 16
  store i64 %19, ptr %14, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = load i32, ptr %10, align 4, !tbaa !9
  %26 = call float %22(ptr noundef %23, ptr noundef %24, i32 noundef %25)
  store float %26, ptr %16, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  store i32 0, ptr %17, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %55, %6
  %28 = load i32, ptr %17, align 4, !tbaa !9
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = icmp ult i32 %28, %29
  br i1 %30, label %31, label %58

31:                                               ; preds = %27
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = load i32, ptr %10, align 4, !tbaa !9
  %34 = zext i32 %33 to i64
  %35 = mul i64 %34, 4
  call void @llvm.memmove.p0.p0.i64(ptr align 16 %21, ptr align 4 %32, i64 %35, i1 false)
  %36 = load float, ptr %11, align 4, !tbaa !11
  %37 = load i32, ptr %17, align 4, !tbaa !9
  %38 = zext i32 %37 to i64
  %39 = getelementptr inbounds float, ptr %21, i64 %38
  %40 = load float, ptr %39, align 4, !tbaa !11
  %41 = fadd float %40, %36
  store float %41, ptr %39, align 4, !tbaa !11
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = load i32, ptr %10, align 4, !tbaa !9
  %45 = call float %42(ptr noundef %43, ptr noundef %21, i32 noundef %44)
  store float %45, ptr %15, align 4, !tbaa !11
  %46 = load float, ptr %15, align 4, !tbaa !11
  %47 = load float, ptr %16, align 4, !tbaa !11
  %48 = fsub float %46, %47
  %49 = load float, ptr %11, align 4, !tbaa !11
  %50 = fdiv float %48, %49
  %51 = load ptr, ptr %12, align 8, !tbaa !5
  %52 = load i32, ptr %17, align 4, !tbaa !9
  %53 = zext i32 %52 to i64
  %54 = getelementptr inbounds float, ptr %51, i64 %53
  store float %50, ptr %54, align 4, !tbaa !11
  br label %55

55:                                               ; preds = %31
  %56 = load i32, ptr %17, align 4, !tbaa !9
  %57 = add i32 %56, 1
  store i32 %57, ptr %17, align 4, !tbaa !9
  br label %27, !llvm.loop !13

58:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  %59 = load ptr, ptr %13, align 8
  call void @llvm.stackrestore.p0(ptr %59)
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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: nounwind uwtable
define dso_local void @gradsearch_gradient(ptr nocapture noundef readonly %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, float noundef %4, ptr nocapture noundef writeonly %5) local_unnamed_addr #0 {
  %7 = zext i32 %3 to i64
  %8 = alloca float, i64 %7, align 16
  %9 = tail call float %0(ptr noundef %1, ptr noundef %2, i32 noundef %3) #2
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6
  %10 = shl nuw nsw i64 %7, 2
  br label %11

11:                                               ; preds = %.lr.ph, %11
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %11 ]
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %8, ptr align 4 %2, i64 %10, i1 false)
  %12 = getelementptr inbounds float, ptr %8, i64 %indvars.iv
  %13 = load float, ptr %12, align 4, !tbaa !5
  %14 = fadd float %13, %4
  store float %14, ptr %12, align 4, !tbaa !5
  %15 = call float %0(ptr noundef %1, ptr noundef nonnull %8, i32 noundef %3) #2
  %16 = fsub float %15, %9
  %17 = fdiv float %16, %4
  %18 = getelementptr inbounds float, ptr %5, i64 %indvars.iv
  store float %17, ptr %18, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %7
  br i1 %exitcond.not, label %._crit_edge, label %11, !llvm.loop !9

._crit_edge:                                      ; preds = %11, %6
  ret void
}

; 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

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 nounwind willreturn memory(argmem: readwrite) }
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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass