; LLVM IR from Try/Tempest :: Engine/thirdparty/openal-soft/common/ringbuffer.ll (ZN10RingBuffer6CreateEmmb)

; 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.std::unique_ptr" = type { %"struct.std::__uniq_ptr_data" }
%"struct.std::__uniq_ptr_data" = type { %"class.std::__uniq_ptr_impl" }
%"class.std::__uniq_ptr_impl" = type { %"class.std::tuple" }
%"class.std::tuple" = type { %"struct.std::_Tuple_impl" }
%"struct.std::_Tuple_impl" = type { %"struct.std::_Head_base.1" }
%"struct.std::_Head_base.1" = type { ptr }

@.str = external hidden unnamed_addr constant [26 x i8], align 1
@_ZTISt14overflow_error = external constant ptr

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN10RingBuffer6CreateEmmb(ptr dead_on_unwind noalias writable sret(%"class.std::unique_ptr") align 8 %0, i64 noundef %1, i64 noundef %2, i1 noundef zeroext %3) #0 align 2 personality ptr @__gxx_personality_v0 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i8, align 1
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca i1, align 1
  store ptr %0, ptr %5, align 8
  store i64 %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !5
  %14 = zext i1 %3 to i8
  store i8 %14, ptr %8, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  store i64 0, ptr %9, align 8, !tbaa !5
  %15 = load i64, ptr %6, align 8, !tbaa !5
  %16 = icmp ugt i64 %15, 0
  br i1 %16, label %17, label %44

17:                                               ; preds = %4
  %18 = load i64, ptr %6, align 8, !tbaa !5
  %19 = sub i64 %18, 1
  store i64 %19, ptr %9, align 8, !tbaa !5
  %20 = load i64, ptr %9, align 8, !tbaa !5
  %21 = lshr i64 %20, 1
  %22 = load i64, ptr %9, align 8, !tbaa !5
  %23 = or i64 %22, %21
  store i64 %23, ptr %9, align 8, !tbaa !5
  %24 = load i64, ptr %9, align 8, !tbaa !5
  %25 = lshr i64 %24, 2
  %26 = load i64, ptr %9, align 8, !tbaa !5
  %27 = or i64 %26, %25
  store i64 %27, ptr %9, align 8, !tbaa !5
  %28 = load i64, ptr %9, align 8, !tbaa !5
  %29 = lshr i64 %28, 4
  %30 = load i64, ptr %9, align 8, !tbaa !5
  %31 = or i64 %30, %29
  store i64 %31, ptr %9, align 8, !tbaa !5
  %32 = load i64, ptr %9, align 8, !tbaa !5
  %33 = lshr i64 %32, 8
  %34 = load i64, ptr %9, align 8, !tbaa !5
  %35 = or i64 %34, %33
  store i64 %35, ptr %9, align 8, !tbaa !5
  %36 = load i64, ptr %9, align 8, !tbaa !5
  %37 = lshr i64 %36, 16
  %38 = load i64, ptr %9, align 8, !tbaa !5
  %39 = or i64 %38, %37
  store i64 %39, ptr %9, align 8, !tbaa !5
  %40 = load i64, ptr %9, align 8, !tbaa !5
  %41 = lshr i64 %40, 32
  %42 = load i64, ptr %9, align 8, !tbaa !5
  %43 = or i64 %42, %41
  store i64 %43, ptr %9, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %17, %4
  %45 = load i64, ptr %9, align 8, !tbaa !5
  %46 = add i64 %45, 1
  store i64 %46, ptr %9, align 8, !tbaa !5
  %47 = load i64, ptr %9, align 8, !tbaa !5
  %48 = load i64, ptr %6, align 8, !tbaa !5
  %49 = icmp ult i64 %47, %48
  br i1 %49, label %61, label %50

50:                                               ; preds = %44
  %51 = load i64, ptr %9, align 8, !tbaa !5
  %52 = call noundef i64 @_ZNSt14numeric_limitsImE3maxEv() #5
  %53 = lshr i64 %52, 1
  %54 = icmp ugt i64 %51, %53
  br i1 %54, label %61, label %55

55:                                               ; preds = %50
  %56 = load i64, ptr %9, align 8, !tbaa !5
  %57 = call noundef i64 @_ZNSt14numeric_limitsImE3maxEv() #5
  %58 = load i64, ptr %7, align 8, !tbaa !5
  %59 = udiv i64 %57, %58
  %60 = icmp ugt i64 %56, %59
  br i1 %60, label %61, label %68

61:                                               ; preds = %55, %50, %44
  %62 = call ptr @__cxa_allocate_exception(i64 16) #5
  invoke void @_ZNSt14overflow_errorC1EPKc(ptr noundef nonnull align 8 dereferenceable(16) %62, ptr noundef @.str)
          to label %63 unwind label %64

63:                                               ; preds = %61
  call void @__cxa_throw(ptr %62, ptr @_ZTISt14overflow_error, ptr @_ZNSt14overflow_errorD1Ev) #6
  unreachable

64:                                               ; preds = %61
  %65 = landingpad { ptr, i32 }
          cleanup
  %66 = extractvalue { ptr, i32 } %65, 0
  store ptr %66, ptr %10, align 8
  %67 = extractvalue { ptr, i32 } %65, 1
  store i32 %67, ptr %11, align 4
  call void @__cxa_free_exception(ptr %62) #5
  br label %94

68:                                               ; preds = %55
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %69 = load i64, ptr %9, align 8, !tbaa !5
  %70 = load i64, ptr %7, align 8, !tbaa !5
  %71 = mul i64 %69, %70
  store i64 %71, ptr %12, align 8, !tbaa !5
  store i1 false, ptr %13, align 1
  %72 = load i64, ptr %12, align 8, !tbaa !5
  %73 = call noundef ptr @_ZN10RingBuffernwEm8FamCount(i64 noundef 192, i64 noundef %72)
  %74 = load i8, ptr %8, align 1, !tbaa !9, !range !11, !noundef !12
  %75 = trunc i8 %74 to i1
  br i1 %75, label %76, label %78

76:                                               ; preds = %68
  %77 = load i64, ptr %6, align 8, !tbaa !5
  br label %80

78:                                               ; preds = %68
  %79 = load i64, ptr %9, align 8, !tbaa !5
  br label %80

80:                                               ; preds = %78, %76
  %81 = phi i64 [ %77, %76 ], [ %79, %78 ]
  %82 = load i64, ptr %9, align 8, !tbaa !5
  %83 = sub i64 %82, 1
  %84 = load i64, ptr %7, align 8, !tbaa !5
  %85 = load i64, ptr %12, align 8, !tbaa !5
  invoke void @_ZN10RingBufferC2Emmmm(ptr noundef nonnull align 64 dereferenceable(176) %73, i64 noundef %81, i64 noundef %83, i64 noundef %84, i64 noundef %85)
          to label %86 unwind label %88

86:                                               ; preds = %80
  call void @_ZNSt10unique_ptrI10RingBufferSt14default_deleteIS0_EEC2IS2_vEEPS0_(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef %73) #5
  store i1 true, ptr %13, align 1
  %87 = load i1, ptr %13, align 1
  br i1 %87, label %93, label %92

88:                                               ; preds = %80
  %89 = landingpad { ptr, i32 }
          cleanup
  %90 = extractvalue { ptr, i32 } %89, 0
  store ptr %90, ptr %10, align 8
  %91 = extractvalue { ptr, i32 } %89, 1
  store i32 %91, ptr %11, align 4
  call void @_ZN10RingBufferdlEPv8FamCount(ptr noundef %73, i64 noundef %72) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  br label %94

92:                                               ; preds = %86
  call void @_ZNSt10unique_ptrI10RingBufferSt14default_deleteIS0_EED2Ev(ptr noundef nonnull align 8 dereferenceable(8) %0) #5
  br label %93

93:                                               ; preds = %92, %86
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  ret void

94:                                               ; preds = %88, %64
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  br label %95

95:                                               ; preds = %94
  %96 = load ptr, ptr %10, align 8
  %97 = load i32, ptr %11, align 4
  %98 = insertvalue { ptr, i32 } poison, ptr %96, 0
  %99 = insertvalue { ptr, i32 } %98, i32 %97, 1
  resume { ptr, i32 } %99
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNSt14numeric_limitsImE3maxEv() #2 align 2

declare ptr @__cxa_allocate_exception(i64)

declare void @_ZNSt14overflow_errorC1EPKc(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef) unnamed_addr #3

declare i32 @__gxx_personality_v0(...)

declare void @__cxa_free_exception(ptr)

; Function Attrs: nounwind
declare void @_ZNSt14overflow_errorD1Ev(ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #4

declare void @__cxa_throw(ptr, ptr, ptr)

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN10RingBuffernwEm8FamCount(i64 noundef, i64 noundef) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN10RingBufferC2Emmmm(ptr noundef nonnull align 64 dereferenceable(176), i64 noundef, i64 noundef, i64 noundef, i64 noundef) unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN10RingBufferdlEPv8FamCount(ptr noundef, i64 noundef) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt10unique_ptrI10RingBufferSt14default_deleteIS0_EEC2IS2_vEEPS0_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) unnamed_addr #2 align 2

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #3 = { "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 "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 #5 = { nounwind }
attributes #6 = { noreturn }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}


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

%"class.std::unique_ptr" = type { %"struct.std::__uniq_ptr_data" }
%"struct.std::__uniq_ptr_data" = type { %"class.std::__uniq_ptr_impl" }
%"class.std::__uniq_ptr_impl" = type { %"class.std::tuple" }
%"class.std::tuple" = type { %"struct.std::_Tuple_impl" }
%"struct.std::_Tuple_impl" = type { %"struct.std::_Head_base.1" }
%"struct.std::_Head_base.1" = type { ptr }

@.str = external hidden unnamed_addr constant [26 x i8], align 1
@_ZTISt14overflow_error = external constant ptr

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN10RingBuffer6CreateEmmb(ptr dead_on_unwind noalias writable sret(%"class.std::unique_ptr") align 8 %0, i64 noundef %1, i64 noundef %2, i1 noundef zeroext %3) local_unnamed_addr #0 align 2 personality ptr @__gxx_personality_v0 {
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %19, label %5

5:                                                ; preds = %4
  %6 = add i64 %1, -1
  %7 = lshr i64 %6, 1
  %8 = or i64 %7, %6
  %9 = lshr i64 %8, 2
  %10 = or i64 %9, %8
  %11 = lshr i64 %10, 4
  %12 = or i64 %11, %10
  %13 = lshr i64 %12, 8
  %14 = or i64 %13, %12
  %15 = lshr i64 %14, 16
  %16 = or i64 %15, %14
  %17 = lshr i64 %16, 32
  %18 = or i64 %17, %16
  br label %19

19:                                               ; preds = %5, %4
  %.0 = phi i64 [ %18, %5 ], [ 0, %4 ]
  %20 = add i64 %.0, 1
  %21 = icmp ult i64 %20, %1
  br i1 %21, label %30, label %22

22:                                               ; preds = %19
  %23 = tail call noundef i64 @_ZNSt14numeric_limitsImE3maxEv() #4
  %24 = lshr i64 %23, 1
  %25 = icmp ugt i64 %20, %24
  br i1 %25, label %30, label %26

26:                                               ; preds = %22
  %27 = tail call noundef i64 @_ZNSt14numeric_limitsImE3maxEv() #4
  %28 = udiv i64 %27, %2
  %29 = icmp ugt i64 %20, %28
  br i1 %29, label %30, label %35

30:                                               ; preds = %26, %22, %19
  %31 = tail call ptr @__cxa_allocate_exception(i64 16) #4
  invoke void @_ZNSt14overflow_errorC1EPKc(ptr noundef nonnull align 8 dereferenceable(16) %31, ptr noundef nonnull @.str)
          to label %32 unwind label %33

32:                                               ; preds = %30
  tail call void @__cxa_throw(ptr nonnull %31, ptr nonnull @_ZTISt14overflow_error, ptr nonnull @_ZNSt14overflow_errorD1Ev) #5
  unreachable

33:                                               ; preds = %30
  %34 = landingpad { ptr, i32 }
          cleanup
  tail call void @__cxa_free_exception(ptr %31) #4
  resume { ptr, i32 } %34

35:                                               ; preds = %26
  %36 = mul i64 %20, %2
  %37 = tail call noundef ptr @_ZN10RingBuffernwEm8FamCount(i64 noundef 192, i64 noundef %36)
  %38 = select i1 %3, i64 %1, i64 %20
  tail call void @_ZN10RingBufferC2Emmmm(ptr noundef nonnull align 64 dereferenceable(176) %37, i64 noundef %38, i64 noundef %.0, i64 noundef %2, i64 noundef %36)
  tail call void @_ZNSt10unique_ptrI10RingBufferSt14default_deleteIS0_EEC2IS2_vEEPS0_(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef nonnull %37) #4
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNSt14numeric_limitsImE3maxEv() local_unnamed_addr #1 align 2

declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr

declare void @_ZNSt14overflow_errorC1EPKc(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef) unnamed_addr #2

declare i32 @__gxx_personality_v0(...)

declare void @__cxa_free_exception(ptr) local_unnamed_addr

; Function Attrs: nounwind
declare void @_ZNSt14overflow_errorD1Ev(ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #3

declare void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN10RingBuffernwEm8FamCount(i64 noundef, i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN10RingBufferC2Emmmm(ptr noundef nonnull align 64 dereferenceable(176), i64 noundef, i64 noundef, i64 noundef, i64 noundef) unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt10unique_ptrI10RingBufferSt14default_deleteIS0_EEC2IS2_vEEPS0_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) unnamed_addr #1 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" }
attributes #1 = { 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 #2 = { "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 = { nounwind "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 }
attributes #5 = { noreturn }

!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)"}


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