; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/old/concurrent_queue_v2.ll (ZN3tbb8internal21concurrent_queue_baseC2Em)

; 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.tbb::cache_aligned_allocator" = type { i8 }
%"class.tbb::internal::concurrent_queue_base" = type { ptr, ptr, i64, i64, i64 }

@_ZTVN3tbb8internal21concurrent_queue_baseE = external dso_local unnamed_addr constant { [6 x ptr] }, align 8

; 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 void @_ZN3tbb8internal21concurrent_queue_baseC2Em(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1) unnamed_addr #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca %"class.tbb::cache_aligned_allocator", align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %6 = load ptr, ptr %3, align 8
  store ptr getelementptr inbounds inrange(-16, 32) ({ [6 x ptr] }, ptr @_ZTVN3tbb8internal21concurrent_queue_baseE, i32 0, i32 0, i32 2), ptr %6, align 8, !tbaa !11
  %7 = load i64, ptr %4, align 8, !tbaa !9
  %8 = icmp ule i64 %7, 8
  br i1 %8, label %9, label %10

9:                                                ; preds = %2
  br label %33

10:                                               ; preds = %2
  %11 = load i64, ptr %4, align 8, !tbaa !9
  %12 = icmp ule i64 %11, 16
  br i1 %12, label %13, label %14

13:                                               ; preds = %10
  br label %31

14:                                               ; preds = %10
  %15 = load i64, ptr %4, align 8, !tbaa !9
  %16 = icmp ule i64 %15, 32
  br i1 %16, label %17, label %18

17:                                               ; preds = %14
  br label %29

18:                                               ; preds = %14
  %19 = load i64, ptr %4, align 8, !tbaa !9
  %20 = icmp ule i64 %19, 64
  br i1 %20, label %21, label %22

21:                                               ; preds = %18
  br label %27

22:                                               ; preds = %18
  %23 = load i64, ptr %4, align 8, !tbaa !9
  %24 = icmp ule i64 %23, 128
  %25 = zext i1 %24 to i64
  %26 = select i1 %24, i32 2, i32 1
  br label %27

27:                                               ; preds = %22, %21
  %28 = phi i32 [ 4, %21 ], [ %26, %22 ]
  br label %29

29:                                               ; preds = %27, %17
  %30 = phi i32 [ 8, %17 ], [ %28, %27 ]
  br label %31

31:                                               ; preds = %29, %13
  %32 = phi i32 [ 16, %13 ], [ %30, %29 ]
  br label %33

33:                                               ; preds = %31, %9
  %34 = phi i32 [ 32, %9 ], [ %32, %31 ]
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds %"class.tbb::internal::concurrent_queue_base", ptr %6, i32 0, i32 3
  store i64 %35, ptr %36, align 8, !tbaa !13
  %37 = load i64, ptr %4, align 8, !tbaa !9
  %38 = icmp ugt i64 %37, 1
  br i1 %38, label %39, label %41

39:                                               ; preds = %33
  %40 = load i64, ptr %4, align 8, !tbaa !9
  br label %42

41:                                               ; preds = %33
  br label %42

42:                                               ; preds = %41, %39
  %43 = phi i64 [ %40, %39 ], [ 2, %41 ]
  %44 = udiv i64 -1, %43
  %45 = getelementptr inbounds %"class.tbb::internal::concurrent_queue_base", ptr %6, i32 0, i32 2
  store i64 %44, ptr %45, align 8, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #4
  call void @_ZN3tbb23cache_aligned_allocatorINS_8internal20concurrent_queue_repEEC2Ev(ptr noundef nonnull align 1 dereferenceable(1) %5) #4
  %46 = call noundef ptr @_ZN3tbb23cache_aligned_allocatorINS_8internal20concurrent_queue_repEE8allocateEmPKv(ptr noundef nonnull align 1 dereferenceable(1) %5, i64 noundef 1, ptr noundef null)
  %47 = getelementptr inbounds %"class.tbb::internal::concurrent_queue_base", ptr %6, i32 0, i32 1
  store ptr %46, ptr %47, align 8, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #4
  %48 = getelementptr inbounds %"class.tbb::internal::concurrent_queue_base", ptr %6, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !16
  call void @llvm.memset.p0.i64(ptr align 1 %49, i8 0, i64 576, i1 false)
  %50 = load i64, ptr %4, align 8, !tbaa !9
  %51 = getelementptr inbounds %"class.tbb::internal::concurrent_queue_base", ptr %6, i32 0, i32 4
  store i64 %50, ptr %51, align 8, !tbaa !17
  ret void
}

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN3tbb23cache_aligned_allocatorINS_8internal20concurrent_queue_repEE8allocateEmPKv(ptr noundef nonnull align 1 dereferenceable(1), i64 noundef, ptr noundef) #1 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"vtable pointer", !8, i64 0}
!13 = !{!14, !10, i64 24}
!14 = !{!"_ZTSN3tbb8internal21concurrent_queue_baseE", !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!15 = !{!14, !10, i64 16}
!16 = !{!14, !6, i64 8}
!17 = !{!14, !10, i64 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"

%"class.tbb::cache_aligned_allocator" = type { i8 }

@_ZTVN3tbb8internal21concurrent_queue_baseE = external dso_local unnamed_addr constant { [6 x ptr] }, align 8

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

; Function Attrs: mustprogress 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 void @_ZN3tbb8internal21concurrent_queue_baseC2Em(ptr nocapture noundef nonnull writeonly align 8 dereferenceable(40) %0, i64 noundef %1) unnamed_addr #1 align 2 {
  %3 = alloca %"class.tbb::cache_aligned_allocator", align 1
  store ptr getelementptr inbounds inrange(-16, 32) (i8, ptr @_ZTVN3tbb8internal21concurrent_queue_baseE, i64 16), ptr %0, align 8, !tbaa !5
  %4 = icmp ult i64 %1, 9
  br i1 %4, label %14, label %5

5:                                                ; preds = %2
  %6 = icmp ult i64 %1, 17
  br i1 %6, label %14, label %7

7:                                                ; preds = %5
  %8 = icmp ult i64 %1, 33
  br i1 %8, label %14, label %9

9:                                                ; preds = %7
  %10 = icmp ult i64 %1, 65
  br i1 %10, label %14, label %11

11:                                               ; preds = %9
  %12 = icmp ult i64 %1, 129
  %13 = select i1 %12, i64 2, i64 1
  br label %14

14:                                               ; preds = %5, %11, %9, %7, %2
  %15 = phi i64 [ 32, %2 ], [ 16, %5 ], [ 8, %7 ], [ %13, %11 ], [ 4, %9 ]
  %16 = getelementptr inbounds i8, ptr %0, i64 24
  store i64 %15, ptr %16, align 8, !tbaa !8
  %17 = tail call i64 @llvm.umax.i64(i64 %1, i64 2)
  %18 = udiv i64 -1, %17
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 %18, ptr %19, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %3) #5
  call void @_ZN3tbb23cache_aligned_allocatorINS_8internal20concurrent_queue_repEEC2Ev(ptr noundef nonnull align 1 dereferenceable(1) %3) #5
  %20 = call noundef ptr @_ZN3tbb23cache_aligned_allocatorINS_8internal20concurrent_queue_repEE8allocateEmPKv(ptr noundef nonnull align 1 dereferenceable(1) %3, i64 noundef 1, ptr noundef null)
  %21 = getelementptr inbounds i8, ptr %0, i64 8
  store ptr %20, ptr %21, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %3) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(576) %20, i8 0, i64 576, i1 false)
  %22 = getelementptr inbounds i8, ptr %0, i64 32
  store i64 %1, ptr %22, align 8, !tbaa !15
  ret void
}

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN3tbb23cache_aligned_allocatorINS_8internal20concurrent_queue_repEE8allocateEmPKv(ptr noundef nonnull align 1 dereferenceable(1), i64 noundef, ptr noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #4

attributes #0 = { mustprogress 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 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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 = !{!"vtable pointer", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !12, i64 24}
!9 = !{!"_ZTSN3tbb8internal21concurrent_queue_baseE", !10, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!10 = !{!"any pointer", !11, i64 0}
!11 = !{!"omnipotent char", !7, i64 0}
!12 = !{!"long", !11, i64 0}
!13 = !{!9, !12, i64 16}
!14 = !{!9, !10, i64 8}
!15 = !{!9, !12, i64 32}


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