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

; 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.qnsearch_s = type { ptr, i32, float, float, float, float, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, float, ptr, i32 }

; 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 i32 @qnsearch_compute_gradient(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca float, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.qnsearch_s, ptr %5, i32 0, i32 6
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.qnsearch_s, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.qnsearch_s, ptr %11, i32 0, i32 1
  %13 = load i32, ptr %12, align 8, !tbaa !14
  %14 = zext i32 %13 to i64
  %15 = mul i64 %14, 4
  call void @llvm.memmove.p0.p0.i64(ptr align 4 %7, ptr align 4 %10, i64 %15, i1 false)
  store i32 0, ptr %3, align 4, !tbaa !15
  br label %16

16:                                               ; preds = %73, %1
  %17 = load i32, ptr %3, align 4, !tbaa !15
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.qnsearch_s, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 8, !tbaa !14
  %21 = icmp ult i32 %17, %20
  br i1 %21, label %22, label %76

22:                                               ; preds = %16
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.qnsearch_s, ptr %23, i32 0, i32 3
  %25 = load float, ptr %24, align 8, !tbaa !16
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.qnsearch_s, ptr %26, i32 0, i32 6
  %28 = load ptr, ptr %27, align 8, !tbaa !9
  %29 = load i32, ptr %3, align 4, !tbaa !15
  %30 = zext i32 %29 to i64
  %31 = getelementptr inbounds float, ptr %28, i64 %30
  %32 = load float, ptr %31, align 4, !tbaa !17
  %33 = fadd float %32, %25
  store float %33, ptr %31, align 4, !tbaa !17
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.qnsearch_s, ptr %34, i32 0, i32 13
  %36 = load ptr, ptr %35, align 8, !tbaa !18
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.qnsearch_s, ptr %37, i32 0, i32 15
  %39 = load ptr, ptr %38, align 8, !tbaa !19
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.qnsearch_s, ptr %40, i32 0, i32 6
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.qnsearch_s, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 8, !tbaa !14
  %46 = call float %36(ptr noundef %39, ptr noundef %42, i32 noundef %45)
  store float %46, ptr %4, align 4, !tbaa !17
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.qnsearch_s, ptr %47, i32 0, i32 3
  %49 = load float, ptr %48, align 8, !tbaa !16
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.qnsearch_s, ptr %50, i32 0, i32 6
  %52 = load ptr, ptr %51, align 8, !tbaa !9
  %53 = load i32, ptr %3, align 4, !tbaa !15
  %54 = zext i32 %53 to i64
  %55 = getelementptr inbounds float, ptr %52, i64 %54
  %56 = load float, ptr %55, align 4, !tbaa !17
  %57 = fsub float %56, %49
  store float %57, ptr %55, align 4, !tbaa !17
  %58 = load float, ptr %4, align 4, !tbaa !17
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.qnsearch_s, ptr %59, i32 0, i32 14
  %61 = load float, ptr %60, align 8, !tbaa !20
  %62 = fsub float %58, %61
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.qnsearch_s, ptr %63, i32 0, i32 3
  %65 = load float, ptr %64, align 8, !tbaa !16
  %66 = fdiv float %62, %65
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.qnsearch_s, ptr %67, i32 0, i32 11
  %69 = load ptr, ptr %68, align 8, !tbaa !21
  %70 = load i32, ptr %3, align 4, !tbaa !15
  %71 = zext i32 %70 to i64
  %72 = getelementptr inbounds float, ptr %69, i64 %71
  store float %66, ptr %72, align 4, !tbaa !17
  br label %73

73:                                               ; preds = %22
  %74 = load i32, ptr %3, align 4, !tbaa !15
  %75 = add i32 %74, 1
  store i32 %75, ptr %3, align 4, !tbaa !15
  br label %16, !llvm.loop !22

76:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 0
}

; 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) #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 nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 32}
!10 = !{!"qnsearch_s", !6, i64 0, !11, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !12, i64 96, !6, i64 104, !11, i64 112}
!11 = !{!"int", !7, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = !{!10, !6, i64 0}
!14 = !{!10, !11, i64 8}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !12, i64 16}
!17 = !{!12, !12, i64 0}
!18 = !{!10, !6, i64 88}
!19 = !{!10, !6, i64 104}
!20 = !{!10, !12, i64 96}
!21 = !{!10, !6, i64 72}
!22 = distinct !{!22, !23}
!23 = !{!"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 noundef i32 @qnsearch_compute_gradient(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %0, align 8, !tbaa !12
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !13
  %7 = zext i32 %6 to i64
  %8 = shl nuw nsw i64 %7, 2
  tail call void @llvm.memmove.p0.p0.i64(ptr align 4 %3, ptr align 4 %4, i64 %8, i1 false)
  %9 = load i32, ptr %5, align 8, !tbaa !13
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %10 = getelementptr inbounds i8, ptr %0, i64 16
  %11 = getelementptr inbounds i8, ptr %0, i64 88
  %12 = getelementptr inbounds i8, ptr %0, i64 104
  %13 = getelementptr inbounds i8, ptr %0, i64 96
  %14 = getelementptr inbounds i8, ptr %0, i64 72
  %.pre = load ptr, ptr %2, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %.lr.ph, %15
  %16 = phi ptr [ %.pre, %.lr.ph ], [ %26, %15 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %15 ]
  %17 = phi i32 [ %9, %.lr.ph ], [ %36, %15 ]
  %18 = load float, ptr %10, align 8, !tbaa !14
  %19 = getelementptr inbounds float, ptr %16, i64 %indvars.iv
  %20 = load float, ptr %19, align 4, !tbaa !15
  %21 = fadd float %18, %20
  store float %21, ptr %19, align 4, !tbaa !15
  %22 = load ptr, ptr %11, align 8, !tbaa !16
  %23 = load ptr, ptr %12, align 8, !tbaa !17
  %24 = tail call float %22(ptr noundef %23, ptr noundef %16, i32 noundef %17) #2
  %25 = load float, ptr %10, align 8, !tbaa !14
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds float, ptr %26, i64 %indvars.iv
  %28 = load float, ptr %27, align 4, !tbaa !15
  %29 = fsub float %28, %25
  store float %29, ptr %27, align 4, !tbaa !15
  %30 = load float, ptr %13, align 8, !tbaa !18
  %31 = fsub float %24, %30
  %32 = load float, ptr %10, align 8, !tbaa !14
  %33 = fdiv float %31, %32
  %34 = load ptr, ptr %14, align 8, !tbaa !19
  %35 = getelementptr inbounds float, ptr %34, i64 %indvars.iv
  store float %33, ptr %35, align 4, !tbaa !15
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %36 = load i32, ptr %5, align 8, !tbaa !13
  %37 = zext i32 %36 to i64
  %38 = icmp ult i64 %indvars.iv.next, %37
  br i1 %38, label %15, label %._crit_edge, !llvm.loop !20

._crit_edge:                                      ; preds = %15, %1
  ret i32 0
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr 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 = { mustprogress 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, !7, i64 32}
!6 = !{!"qnsearch_s", !7, i64 0, !10, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !11, i64 96, !7, i64 104, !10, i64 112}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"float", !8, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!6, !11, i64 16}
!15 = !{!11, !11, i64 0}
!16 = !{!6, !7, i64 88}
!17 = !{!6, !7, i64 104}
!18 = !{!6, !11, i64 96}
!19 = !{!6, !7, i64 72}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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