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

; 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.RtApi = type <{ ptr, %"class.std::__cxx11::basic_ostringstream", %"class.std::__cxx11::basic_string", i8, [7 x i8], %"struct.RtApi::RtApiStream", i8, [7 x i8] }>
%"class.std::__cxx11::basic_ostringstream" = type { %"class.std::basic_ostream.base", %"class.std::__cxx11::basic_stringbuf", %"class.std::basic_ios" }
%"class.std::basic_ostream.base" = type { ptr }
%"class.std::__cxx11::basic_stringbuf" = type { %"class.std::basic_streambuf", i32, %"class.std::__cxx11::basic_string" }
%"class.std::basic_streambuf" = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, %"class.std::locale" }
%"class.std::locale" = type { ptr }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }
%"struct.RtApi::RtApiStream" = type { [2 x i32], ptr, i32, i32, [2 x ptr], ptr, [2 x i8], i8, [2 x i8], [2 x i8], i32, i32, i32, [2 x i32], [2 x i32], [2 x i32], [2 x i64], i64, [2 x i64], i32, %struct.CallbackInfo, [2 x %"struct.RtApi::ConvertInfo"], double }
%struct.CallbackInfo = type { ptr, i32, ptr, ptr, ptr, ptr, i8, i8, i32 }
%"struct.RtApi::ConvertInfo" = type { i32, i32, i32, i64, i64, %"class.std::vector.3", %"class.std::vector.3" }
%"class.std::vector.3" = type { %"struct.std::_Vector_base.4" }
%"struct.std::_Vector_base.4" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, 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 uwtable
define dso_local noundef i64 @_ZN5RtApi16getStreamLatencyEv(ptr noundef nonnull align 8 dereferenceable(793) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8
  call void @_ZN5RtApi12verifyStreamEv(ptr noundef nonnull align 8 dereferenceable(793) %4)
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 0, ptr %3, align 8, !tbaa !9
  %5 = getelementptr inbounds %class.RtApi, ptr %4, i32 0, i32 5
  %6 = getelementptr inbounds %"struct.RtApi::RtApiStream", ptr %5, i32 0, i32 2
  %7 = load i32, ptr %6, align 8, !tbaa !11
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %14, label %9

9:                                                ; preds = %1
  %10 = getelementptr inbounds %class.RtApi, ptr %4, i32 0, i32 5
  %11 = getelementptr inbounds %"struct.RtApi::RtApiStream", ptr %10, i32 0, i32 2
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = icmp eq i32 %12, 2
  br i1 %13, label %14, label %19

14:                                               ; preds = %9, %1
  %15 = getelementptr inbounds %class.RtApi, ptr %4, i32 0, i32 5
  %16 = getelementptr inbounds %"struct.RtApi::RtApiStream", ptr %15, i32 0, i32 16
  %17 = getelementptr inbounds [2 x i64], ptr %16, i64 0, i64 0
  %18 = load i64, ptr %17, align 8, !tbaa !9
  store i64 %18, ptr %3, align 8, !tbaa !9
  br label %19

19:                                               ; preds = %14, %9
  %20 = getelementptr inbounds %class.RtApi, ptr %4, i32 0, i32 5
  %21 = getelementptr inbounds %"struct.RtApi::RtApiStream", ptr %20, i32 0, i32 2
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = icmp eq i32 %22, 1
  br i1 %23, label %29, label %24

24:                                               ; preds = %19
  %25 = getelementptr inbounds %class.RtApi, ptr %4, i32 0, i32 5
  %26 = getelementptr inbounds %"struct.RtApi::RtApiStream", ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 8, !tbaa !11
  %28 = icmp eq i32 %27, 2
  br i1 %28, label %29, label %36

29:                                               ; preds = %24, %19
  %30 = getelementptr inbounds %class.RtApi, ptr %4, i32 0, i32 5
  %31 = getelementptr inbounds %"struct.RtApi::RtApiStream", ptr %30, i32 0, i32 16
  %32 = getelementptr inbounds [2 x i64], ptr %31, i64 0, i64 1
  %33 = load i64, ptr %32, align 8, !tbaa !9
  %34 = load i64, ptr %3, align 8, !tbaa !9
  %35 = add i64 %34, %33
  store i64 %35, ptr %3, align 8, !tbaa !9
  br label %36

36:                                               ; preds = %29, %24
  %37 = load i64, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i64 %37
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5RtApi12verifyStreamEv(ptr noundef nonnull align 8 dereferenceable(793)) #1 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !23, i64 440}
!12 = !{!"_ZTS5RtApi", !13, i64 8, !19, i64 384, !21, i64 416, !22, i64 424, !21, i64 792}
!13 = !{!"_ZTSNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEE", !14, i64 0, !15, i64 8}
!14 = !{!"_ZTSSo"}
!15 = !{!"_ZTSNSt7__cxx1115basic_stringbufIcSt11char_traitsIcESaIcEEE", !16, i64 0, !18, i64 64, !19, i64 72}
!16 = !{!"_ZTSSt15basic_streambufIcSt11char_traitsIcEE", !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !17, i64 56}
!17 = !{!"_ZTSSt6locale", !6, i64 0}
!18 = !{!"_ZTSSt13_Ios_Openmode", !7, i64 0}
!19 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !20, i64 0, !10, i64 8, !7, i64 16}
!20 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !6, i64 0}
!21 = !{!"bool", !7, i64 0}
!22 = !{!"_ZTSN5RtApi11RtApiStreamE", !7, i64 0, !6, i64 8, !23, i64 16, !24, i64 20, !7, i64 24, !6, i64 40, !7, i64 48, !21, i64 50, !7, i64 51, !7, i64 53, !25, i64 56, !25, i64 60, !25, i64 64, !7, i64 68, !7, i64 76, !7, i64 84, !7, i64 96, !10, i64 112, !7, i64 120, !25, i64 136, !26, i64 144, !7, i64 200, !27, i64 360}
!23 = !{!"_ZTSN5RtApi10StreamModeE", !7, i64 0}
!24 = !{!"_ZTSN5RtApi11StreamStateE", !7, i64 0}
!25 = !{!"int", !7, i64 0}
!26 = !{!"_ZTS12CallbackInfo", !6, i64 0, !25, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !21, i64 48, !21, i64 49, !25, i64 52}
!27 = !{!"double", !7, i64 0}


; 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 uwtable
define dso_local noundef i64 @_ZN5RtApi16getStreamLatencyEv(ptr noundef nonnull align 8 dereferenceable(793) %0) local_unnamed_addr #0 align 2 {
  tail call void @_ZN5RtApi12verifyStreamEv(ptr noundef nonnull align 8 dereferenceable(793) %0)
  %2 = getelementptr inbounds i8, ptr %0, i64 440
  %3 = load i32, ptr %2, align 8, !tbaa !5
  switch i32 %3, label %7 [
    i32 0, label %4
    i32 2, label %4
  ]

4:                                                ; preds = %1, %1
  %5 = getelementptr inbounds i8, ptr %0, i64 520
  %6 = load i64, ptr %5, align 8, !tbaa !26
  br label %7

7:                                                ; preds = %1, %4
  %.0 = phi i64 [ %6, %4 ], [ 0, %1 ]
  %.off = add i32 %3, -1
  %switch = icmp ult i32 %.off, 2
  %8 = getelementptr inbounds i8, ptr %0, i64 528
  %9 = load i64, ptr %8, align 8
  %10 = select i1 %switch, i64 %9, i64 0
  %.1 = add i64 %10, %.0
  ret i64 %.1
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5RtApi12verifyStreamEv(ptr noundef nonnull align 8 dereferenceable(793)) local_unnamed_addr #0 align 2

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

!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, !21, i64 440}
!6 = !{!"_ZTS5RtApi", !7, i64 8, !16, i64 384, !19, i64 416, !20, i64 424, !19, i64 792}
!7 = !{!"_ZTSNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEE", !8, i64 0, !9, i64 8}
!8 = !{!"_ZTSSo"}
!9 = !{!"_ZTSNSt7__cxx1115basic_stringbufIcSt11char_traitsIcESaIcEEE", !10, i64 0, !15, i64 64, !16, i64 72}
!10 = !{!"_ZTSSt15basic_streambufIcSt11char_traitsIcEE", !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !14, i64 56}
!11 = !{!"any pointer", !12, i64 0}
!12 = !{!"omnipotent char", !13, i64 0}
!13 = !{!"Simple C++ TBAA"}
!14 = !{!"_ZTSSt6locale", !11, i64 0}
!15 = !{!"_ZTSSt13_Ios_Openmode", !12, i64 0}
!16 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !17, i64 0, !18, i64 8, !12, i64 16}
!17 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !11, i64 0}
!18 = !{!"long", !12, i64 0}
!19 = !{!"bool", !12, i64 0}
!20 = !{!"_ZTSN5RtApi11RtApiStreamE", !12, i64 0, !11, i64 8, !21, i64 16, !22, i64 20, !12, i64 24, !11, i64 40, !12, i64 48, !19, i64 50, !12, i64 51, !12, i64 53, !23, i64 56, !23, i64 60, !23, i64 64, !12, i64 68, !12, i64 76, !12, i64 84, !12, i64 96, !18, i64 112, !12, i64 120, !23, i64 136, !24, i64 144, !12, i64 200, !25, i64 360}
!21 = !{!"_ZTSN5RtApi10StreamModeE", !12, i64 0}
!22 = !{!"_ZTSN5RtApi11StreamStateE", !12, i64 0}
!23 = !{!"int", !12, i64 0}
!24 = !{!"_ZTS12CallbackInfo", !11, i64 0, !23, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !19, i64 48, !19, i64 49, !23, i64 52}
!25 = !{!"double", !12, i64 0}
!26 = !{!18, !18, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass