; LLVM IR from samtupy/b32tts_wrapper :: src (sonic)

; 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.sonicStreamStruct = type { ptr, ptr, ptr, ptr, ptr, float, float, float, float, float, float, float, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, 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 @sonicReadFloatFromStream(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.sonicStreamStruct, ptr %13, i32 0, i32 20
  %15 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %15, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %16 = load i32, ptr %8, align 4, !tbaa !9
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %80

19:                                               ; preds = %3
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = icmp sgt i32 %20, %21
  br i1 %22, label %23, label %28

23:                                               ; preds = %19
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = sub nsw i32 %24, %25
  store i32 %26, ptr %9, align 4, !tbaa !9
  %27 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %27, ptr %8, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %23, %19
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.sonicStreamStruct, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  store ptr %31, ptr %10, align 8, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.sonicStreamStruct, ptr %33, i32 0, i32 15
  %35 = load i32, ptr %34, align 8, !tbaa !15
  %36 = mul nsw i32 %32, %35
  store i32 %36, ptr %11, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %41, %28
  %38 = load i32, ptr %11, align 4, !tbaa !9
  %39 = add nsw i32 %38, -1
  store i32 %39, ptr %11, align 4, !tbaa !9
  %40 = icmp ne i32 %38, 0
  br i1 %40, label %41, label %50

41:                                               ; preds = %37
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds i16, ptr %42, i32 1
  store ptr %43, ptr %10, align 8, !tbaa !5
  %44 = load i16, ptr %42, align 2, !tbaa !16
  %45 = sext i16 %44 to i32
  %46 = sitofp i32 %45 to float
  %47 = fdiv float %46, 3.276700e+04
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds float, ptr %48, i32 1
  store ptr %49, ptr %6, align 8, !tbaa !5
  store float %47, ptr %48, align 4, !tbaa !18
  br label %37, !llvm.loop !19

50:                                               ; preds = %37
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = icmp sgt i32 %51, 0
  br i1 %52, label %53, label %75

53:                                               ; preds = %50
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.sonicStreamStruct, ptr %54, i32 0, i32 1
  %56 = load ptr, ptr %55, align 8, !tbaa !14
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.sonicStreamStruct, ptr %57, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8, !tbaa !14
  %60 = load i32, ptr %8, align 4, !tbaa !9
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.sonicStreamStruct, ptr %61, i32 0, i32 15
  %63 = load i32, ptr %62, align 8, !tbaa !15
  %64 = mul nsw i32 %60, %63
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds i16, ptr %59, i64 %65
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = mul i64 %68, 2
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.sonicStreamStruct, ptr %70, i32 0, i32 15
  %72 = load i32, ptr %71, align 8, !tbaa !15
  %73 = sext i32 %72 to i64
  %74 = mul i64 %69, %73
  call void @llvm.memmove.p0.p0.i64(ptr align 2 %56, ptr align 2 %66, i64 %74, i1 false)
  br label %75

75:                                               ; preds = %53, %50
  %76 = load i32, ptr %9, align 4, !tbaa !9
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.sonicStreamStruct, ptr %77, i32 0, i32 20
  store i32 %76, ptr %78, align 4, !tbaa !11
  %79 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %79, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %80

80:                                               ; preds = %75, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %81 = load i32, ptr %4, align 4
  ret i32 %81
}

; 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 100}
!12 = !{!"sonicStreamStruct", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !13, i64 40, !13, i64 44, !13, i64 48, !13, i64 52, !13, i64 56, !13, i64 60, !13, i64 64, !10, i64 68, !10, i64 72, !10, i64 76, !10, i64 80, !10, i64 84, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !10, i64 108, !10, i64 112, !10, i64 116, !10, i64 120, !10, i64 124, !10, i64 128, !10, i64 132}
!13 = !{!"float", !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{!12, !10, i64 80}
!16 = !{!17, !17, i64 0}
!17 = !{!"short", !7, i64 0}
!18 = !{!13, !13, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local i32 @sonicReadFloatFromStream(ptr nocapture noundef %0, ptr nocapture noundef writeonly %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 100
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %36, label %7

7:                                                ; preds = %3
  %8 = icmp sgt i32 %5, %2
  %9 = sub nsw i32 %5, %2
  %spec.select = tail call i32 @llvm.smin.i32(i32 %5, i32 %2)
  %spec.select30 = select i1 %8, i32 %9, i32 0
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %0, i64 80
  %13 = load i32, ptr %12, align 8, !tbaa !13
  %14 = mul nsw i32 %13, %spec.select
  %.not31 = icmp eq i32 %14, 0
  br i1 %.not31, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %7
  %15 = zext i32 %14 to i64
  %min.iters.check = icmp ult i32 %14, 4
  br i1 %min.iters.check, label %.lr.ph.preheader41, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %15, 4294967292
  %.cast = trunc nuw i64 %n.vec to i32
  %ind.end = sub i32 %14, %.cast
  %16 = shl nuw nsw i64 %n.vec, 1
  %ind.end35 = getelementptr i8, ptr %11, i64 %16
  %17 = shl nuw nsw i64 %n.vec, 2
  %ind.end37 = getelementptr i8, ptr %1, i64 %17
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %11, i64 %offset.idx
  %offset.idx39 = shl i64 %index, 2
  %next.gep40 = getelementptr i8, ptr %1, i64 %offset.idx39
  %wide.load = load <4 x i16>, ptr %next.gep, align 2, !tbaa !14
  %18 = sitofp <4 x i16> %wide.load to <4 x float>
  %19 = fdiv <4 x float> %18, <float 3.276700e+04, float 3.276700e+04, float 3.276700e+04, float 3.276700e+04>
  store <4 x float> %19, ptr %next.gep40, align 4, !tbaa !16
  %index.next = add nuw i64 %index, 4
  %20 = icmp eq i64 %index.next, %n.vec
  br i1 %20, label %middle.block, label %vector.body, !llvm.loop !17

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %15
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader41

.lr.ph.preheader41:                               ; preds = %middle.block, %.lr.ph.preheader
  %.034.ph = phi i32 [ %14, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.02333.ph = phi ptr [ %11, %.lr.ph.preheader ], [ %ind.end35, %middle.block ]
  %.02732.ph = phi ptr [ %1, %.lr.ph.preheader ], [ %ind.end37, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader41, %.lr.ph
  %.034 = phi i32 [ %21, %.lr.ph ], [ %.034.ph, %.lr.ph.preheader41 ]
  %.02333 = phi ptr [ %22, %.lr.ph ], [ %.02333.ph, %.lr.ph.preheader41 ]
  %.02732 = phi ptr [ %26, %.lr.ph ], [ %.02732.ph, %.lr.ph.preheader41 ]
  %21 = add nsw i32 %.034, -1
  %22 = getelementptr inbounds i8, ptr %.02333, i64 2
  %23 = load i16, ptr %.02333, align 2, !tbaa !14
  %24 = sitofp i16 %23 to float
  %25 = fdiv float %24, 3.276700e+04
  %26 = getelementptr inbounds i8, ptr %.02732, i64 4
  store float %25, ptr %.02732, align 4, !tbaa !16
  %.not = icmp eq i32 %21, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !21

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %7
  %27 = icmp sgt i32 %spec.select30, 0
  br i1 %27, label %28, label %35

28:                                               ; preds = %._crit_edge
  %29 = sext i32 %14 to i64
  %30 = getelementptr inbounds i16, ptr %11, i64 %29
  %31 = shl nuw i32 %spec.select30, 1
  %32 = zext i32 %31 to i64
  %33 = sext i32 %13 to i64
  %34 = mul nsw i64 %33, %32
  tail call void @llvm.memmove.p0.p0.i64(ptr align 2 %11, ptr align 2 %30, i64 %34, i1 false)
  br label %35

35:                                               ; preds = %28, %._crit_edge
  store i32 %spec.select30, ptr %4, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %3, %35
  %.026 = phi i32 [ %spec.select, %35 ], [ 0, %3 ]
  ret i32 %.026
}

; 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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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" }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !11, i64 100}
!6 = !{!"sonicStreamStruct", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !11, i64 68, !11, i64 72, !11, i64 76, !11, i64 80, !11, i64 84, !11, i64 88, !11, i64 92, !11, i64 96, !11, i64 100, !11, i64 104, !11, i64 108, !11, i64 112, !11, i64 116, !11, i64 120, !11, i64 124, !11, i64 128, !11, i64 132}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"float", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !11, i64 80}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !8, i64 0}
!16 = !{!10, !10, i64 0}
!17 = distinct !{!17, !18, !19, !20}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !18, !20, !19}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass