; LLVM IR from erwincoumans/tiny-differentiable-simulator :: third_party/tiny_audio/b3SoundSource.ll (ZN13b3SoundSource10startSoundEb)

; 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"

%class.b3SoundSource = type { ptr, ptr }
%struct.b3SoundSourceInternalData = type { [2 x %struct.b3SoundOscillator], %class.b3ADSR, ptr, ptr }
%struct.b3SoundOscillator = type { i32, double, double, double, %struct.b3WavTicker }
%struct.b3WavTicker = type <{ %"class.std::vector", i8, [7 x i8], double, double, double, double, double, i32, [4 x i8] }>
%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl" }
%"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl" = type { %"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl_data" }
%"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%class.b3ADSR = type <{ ptr, i32, [4 x i8], double, double, double, double, double, double, double, i8, [7 x i8] }>

; 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

declare noundef zeroext i1 @_ZNK6b3ADSR6isIdleEv(ptr noundef nonnull align 8 dereferenceable(73)) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN13b3SoundSource10startSoundEb(ptr noundef nonnull align 8 dereferenceable(16) %0, i1 noundef zeroext %1) #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = zext i1 %1 to i8
  store i8 %6, ptr %4, align 1, !tbaa !9
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %class.b3SoundSource, ptr %7, i32 0, i32 1
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = getelementptr inbounds %struct.b3SoundSourceInternalData, ptr %9, i32 0, i32 1
  %11 = call noundef zeroext i1 @_ZNK6b3ADSR6isIdleEv(ptr noundef nonnull align 8 dereferenceable(73) %10)
  br i1 %11, label %12, label %83

12:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !13
  br label %13

13:                                               ; preds = %79, %12
  %14 = load i32, ptr %5, align 4, !tbaa !13
  %15 = icmp slt i32 %14, 2
  br i1 %15, label %17, label %16

16:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %82

17:                                               ; preds = %13
  %18 = getelementptr inbounds %class.b3SoundSource, ptr %7, i32 0, i32 1
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %20 = getelementptr inbounds %struct.b3SoundSourceInternalData, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %5, align 4, !tbaa !13
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [2 x %struct.b3SoundOscillator], ptr %20, i64 0, i64 %22
  call void @_ZN17b3SoundOscillator5resetEv(ptr noundef nonnull align 8 dereferenceable(112) %23)
  %24 = getelementptr inbounds %class.b3SoundSource, ptr %7, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = getelementptr inbounds %struct.b3SoundSourceInternalData, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %5, align 4, !tbaa !13
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [2 x %struct.b3SoundOscillator], ptr %26, i64 0, i64 %28
  %30 = getelementptr inbounds %struct.b3SoundOscillator, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 8, !tbaa !15
  %32 = icmp eq i32 %31, 3
  br i1 %32, label %33, label %78

33:                                               ; preds = %17
  %34 = getelementptr inbounds %class.b3SoundSource, ptr %7, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = getelementptr inbounds %struct.b3SoundSourceInternalData, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %5, align 4, !tbaa !13
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [2 x %struct.b3SoundOscillator], ptr %36, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.b3SoundOscillator, ptr %39, i32 0, i32 4
  %41 = getelementptr inbounds %struct.b3WavTicker, ptr %40, i32 0, i32 6
  %42 = load double, ptr %41, align 8, !tbaa !23
  %43 = fcmp olt double %42, 0.000000e+00
  br i1 %43, label %44, label %60

44:                                               ; preds = %33
  %45 = getelementptr inbounds %class.b3SoundSource, ptr %7, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = getelementptr inbounds %struct.b3SoundSourceInternalData, ptr %46, i32 0, i32 2
  %48 = load ptr, ptr %47, align 8, !tbaa !24
  %49 = call noundef i32 @_ZNK13b3ReadWavFile12getNumFramesEv(ptr noundef nonnull align 8 dereferenceable(53) %48)
  %50 = sitofp i32 %49 to double
  %51 = fsub double %50, 1.000000e+00
  %52 = getelementptr inbounds %class.b3SoundSource, ptr %7, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !11
  %54 = getelementptr inbounds %struct.b3SoundSourceInternalData, ptr %53, i32 0, i32 0
  %55 = load i32, ptr %5, align 4, !tbaa !13
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [2 x %struct.b3SoundOscillator], ptr %54, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.b3SoundOscillator, ptr %57, i32 0, i32 4
  %59 = getelementptr inbounds %struct.b3WavTicker, ptr %58, i32 0, i32 3
  store double %51, ptr %59, align 8, !tbaa !27
  br label %69

60:                                               ; preds = %33
  %61 = getelementptr inbounds %class.b3SoundSource, ptr %7, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !11
  %63 = getelementptr inbounds %struct.b3SoundSourceInternalData, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %5, align 4, !tbaa !13
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [2 x %struct.b3SoundOscillator], ptr %63, i64 0, i64 %65
  %67 = getelementptr inbounds %struct.b3SoundOscillator, ptr %66, i32 0, i32 4
  %68 = getelementptr inbounds %struct.b3WavTicker, ptr %67, i32 0, i32 3
  store double 0.000000e+00, ptr %68, align 8, !tbaa !27
  br label %69

69:                                               ; preds = %60, %44
  %70 = getelementptr inbounds %class.b3SoundSource, ptr %7, i32 0, i32 1
  %71 = load ptr, ptr %70, align 8, !tbaa !11
  %72 = getelementptr inbounds %struct.b3SoundSourceInternalData, ptr %71, i32 0, i32 0
  %73 = load i32, ptr %5, align 4, !tbaa !13
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [2 x %struct.b3SoundOscillator], ptr %72, i64 0, i64 %74
  %76 = getelementptr inbounds %struct.b3SoundOscillator, ptr %75, i32 0, i32 4
  %77 = getelementptr inbounds %struct.b3WavTicker, ptr %76, i32 0, i32 1
  store i8 0, ptr %77, align 8, !tbaa !28
  br label %78

78:                                               ; preds = %69, %17
  br label %79

79:                                               ; preds = %78
  %80 = load i32, ptr %5, align 4, !tbaa !13
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %5, align 4, !tbaa !13
  br label %13, !llvm.loop !29

82:                                               ; preds = %16
  br label %83

83:                                               ; preds = %82, %2
  %84 = getelementptr inbounds %class.b3SoundSource, ptr %7, i32 0, i32 1
  %85 = load ptr, ptr %84, align 8, !tbaa !11
  %86 = getelementptr inbounds %struct.b3SoundSourceInternalData, ptr %85, i32 0, i32 1
  %87 = load i8, ptr %4, align 1, !tbaa !9, !range !31, !noundef !32
  %88 = trunc i8 %87 to i1
  call void @_ZN6b3ADSR5keyOnEb(ptr noundef nonnull align 8 dereferenceable(73) %86, i1 noundef zeroext %88)
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN17b3SoundOscillator5resetEv(ptr noundef nonnull align 8 dereferenceable(112)) #3 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK13b3ReadWavFile12getNumFramesEv(ptr noundef nonnull align 8 dereferenceable(53)) #3 align 2

declare void @_ZN6b3ADSR5keyOnEb(ptr noundef nonnull align 8 dereferenceable(73), i1 noundef zeroext) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { mustprogress 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 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS13b3SoundSource", !6, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !14, i64 0}
!16 = !{!"_ZTS17b3SoundOscillator", !14, i64 0, !17, i64 8, !17, i64 16, !17, i64 24, !18, i64 32}
!17 = !{!"double", !7, i64 0}
!18 = !{!"_ZTS11b3WavTicker", !19, i64 0, !10, i64 24, !17, i64 32, !17, i64 40, !17, i64 48, !17, i64 56, !17, i64 64, !14, i64 72}
!19 = !{!"_ZTSSt6vectorIdSaIdEE", !20, i64 0}
!20 = !{!"_ZTSSt12_Vector_baseIdSaIdEE", !21, i64 0}
!21 = !{!"_ZTSNSt12_Vector_baseIdSaIdEE12_Vector_implE", !22, i64 0}
!22 = !{!"_ZTSNSt12_Vector_baseIdSaIdEE17_Vector_impl_dataE", !6, i64 0, !6, i64 8, !6, i64 16}
!23 = !{!16, !17, i64 88}
!24 = !{!25, !6, i64 304}
!25 = !{!"_ZTS25b3SoundSourceInternalData", !7, i64 0, !26, i64 224, !6, i64 304, !6, i64 312}
!26 = !{!"_ZTS6b3ADSR", !14, i64 8, !17, i64 16, !17, i64 24, !17, i64 32, !17, i64 40, !17, i64 48, !17, i64 56, !17, i64 64, !10, i64 72}
!27 = !{!16, !17, i64 64}
!28 = !{!16, !10, i64 56}
!29 = distinct !{!29, !30}
!30 = !{!"llvm.loop.mustprogress"}
!31 = !{i8 0, i8 2}
!32 = !{}


; 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"

declare noundef zeroext i1 @_ZNK6b3ADSR6isIdleEv(ptr noundef nonnull align 8 dereferenceable(73)) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN13b3SoundSource10startSoundEb(ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %0, i1 noundef zeroext %1) local_unnamed_addr #1 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 224
  %6 = tail call noundef zeroext i1 @_ZNK6b3ADSR6isIdleEv(ptr noundef nonnull align 8 dereferenceable(73) %5)
  %.pre = load ptr, ptr %3, align 8, !tbaa !5
  br i1 %6, label %.preheader.preheader, label %.loopexit

.preheader.preheader:                             ; preds = %2
  tail call void @_ZN17b3SoundOscillator5resetEv(ptr noundef nonnull align 8 dereferenceable(112) %.pre)
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = load i32, ptr %7, align 8, !tbaa !10
  %9 = icmp eq i32 %8, 3
  br i1 %9, label %10, label %.preheader.1

10:                                               ; preds = %.preheader.preheader
  %11 = getelementptr inbounds i8, ptr %7, i64 88
  %12 = load double, ptr %11, align 8, !tbaa !20
  %13 = fcmp olt double %12, 0.000000e+00
  br i1 %13, label %14, label %21

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %7, i64 304
  %16 = load ptr, ptr %15, align 8, !tbaa !21
  %17 = tail call noundef i32 @_ZNK13b3ReadWavFile12getNumFramesEv(ptr noundef nonnull align 8 dereferenceable(53) %16)
  %18 = sitofp i32 %17 to double
  %19 = fadd double %18, -1.000000e+00
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %10, %14
  %.sink12 = phi ptr [ %20, %14 ], [ %7, %10 ]
  %.sink = phi double [ %19, %14 ], [ 0.000000e+00, %10 ]
  %22 = getelementptr inbounds i8, ptr %.sink12, i64 64
  store double %.sink, ptr %22, align 8, !tbaa !24
  %23 = getelementptr inbounds i8, ptr %.sink12, i64 56
  store i8 0, ptr %23, align 8, !tbaa !25
  br label %.preheader.1

.preheader.1:                                     ; preds = %.preheader.preheader, %21
  %24 = phi ptr [ %7, %.preheader.preheader ], [ %.sink12, %21 ]
  %25 = getelementptr inbounds i8, ptr %24, i64 112
  tail call void @_ZN17b3SoundOscillator5resetEv(ptr noundef nonnull align 8 dereferenceable(112) %25)
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 112
  %28 = load i32, ptr %27, align 8, !tbaa !10
  %29 = icmp eq i32 %28, 3
  br i1 %29, label %30, label %.loopexit

30:                                               ; preds = %.preheader.1
  %31 = getelementptr inbounds i8, ptr %26, i64 200
  %32 = load double, ptr %31, align 8, !tbaa !20
  %33 = fcmp olt double %32, 0.000000e+00
  br i1 %33, label %34, label %41

34:                                               ; preds = %30
  %35 = getelementptr inbounds i8, ptr %26, i64 304
  %36 = load ptr, ptr %35, align 8, !tbaa !21
  %37 = tail call noundef i32 @_ZNK13b3ReadWavFile12getNumFramesEv(ptr noundef nonnull align 8 dereferenceable(53) %36)
  %38 = sitofp i32 %37 to double
  %39 = fadd double %38, -1.000000e+00
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %30, %34
  %.sink15 = phi ptr [ %40, %34 ], [ %26, %30 ]
  %.sink13 = phi double [ %39, %34 ], [ 0.000000e+00, %30 ]
  %42 = getelementptr inbounds i8, ptr %.sink15, i64 176
  store double %.sink13, ptr %42, align 8, !tbaa !24
  %43 = getelementptr inbounds i8, ptr %.sink15, i64 168
  store i8 0, ptr %43, align 8, !tbaa !25
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader.1, %41, %2
  %44 = phi ptr [ %.pre, %2 ], [ %.sink15, %41 ], [ %26, %.preheader.1 ]
  %45 = getelementptr inbounds i8, ptr %44, i64 224
  tail call void @_ZN6b3ADSR5keyOnEb(ptr noundef nonnull align 8 dereferenceable(73) %45, i1 noundef zeroext %1)
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN17b3SoundOscillator5resetEv(ptr noundef nonnull align 8 dereferenceable(112)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK13b3ReadWavFile12getNumFramesEv(ptr noundef nonnull align 8 dereferenceable(53)) local_unnamed_addr #2 align 2

declare void @_ZN6b3ADSR5keyOnEb(ptr noundef nonnull align 8 dereferenceable(73), i1 noundef zeroext) local_unnamed_addr #0

attributes #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 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 = { 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" }

!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 8}
!6 = !{!"_ZTS13b3SoundSource", !7, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !12, i64 0}
!11 = !{!"_ZTS17b3SoundOscillator", !12, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !14, i64 32}
!12 = !{!"int", !8, i64 0}
!13 = !{!"double", !8, i64 0}
!14 = !{!"_ZTS11b3WavTicker", !15, i64 0, !19, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !12, i64 72}
!15 = !{!"_ZTSSt6vectorIdSaIdEE", !16, i64 0}
!16 = !{!"_ZTSSt12_Vector_baseIdSaIdEE", !17, i64 0}
!17 = !{!"_ZTSNSt12_Vector_baseIdSaIdEE12_Vector_implE", !18, i64 0}
!18 = !{!"_ZTSNSt12_Vector_baseIdSaIdEE17_Vector_impl_dataE", !7, i64 0, !7, i64 8, !7, i64 16}
!19 = !{!"bool", !8, i64 0}
!20 = !{!11, !13, i64 88}
!21 = !{!22, !7, i64 304}
!22 = !{!"_ZTS25b3SoundSourceInternalData", !8, i64 0, !23, i64 224, !7, i64 304, !7, i64 312}
!23 = !{!"_ZTS6b3ADSR", !12, i64 8, !13, i64 16, !13, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !19, i64 72}
!24 = !{!11, !13, i64 64}
!25 = !{!11, !19, i64 56}


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