; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/tbb (tbb_misc_ex)

; 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::internal::affinity_helper" = type <{ ptr, i32, [4 x i8] }>

@_ZN3tbb8internalL12process_maskE = external hidden global ptr, align 8
@_ZN3tbb8internalL9num_masksE = external hidden global i32, align 4

; Function Attrs: mustprogress uwtable
declare hidden void @_ZN3tbb8internalL24set_thread_affinity_maskEmPK9cpu_set_t(i64 noundef, ptr noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3tbb8internal15affinity_helper21protect_affinity_maskEb(ptr noundef nonnull align 8 dereferenceable(12) %0, i1 noundef zeroext %1) #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  %5 = zext i1 %1 to i8
  store i8 %5, ptr %4, align 1, !tbaa !9
  %6 = load ptr, ptr %3, align 8
  %7 = getelementptr inbounds %"class.tbb::internal::affinity_helper", ptr %6, i32 0, i32 0
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %55

10:                                               ; preds = %2
  %11 = load i32, ptr @_ZN3tbb8internalL9num_masksE, align 4, !tbaa !14
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %55

13:                                               ; preds = %10
  %14 = load i32, ptr @_ZN3tbb8internalL9num_masksE, align 4, !tbaa !14
  %15 = sext i32 %14 to i64
  %16 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %15, i64 128)
  %17 = extractvalue { i64, i1 } %16, 1
  %18 = extractvalue { i64, i1 } %16, 0
  %19 = select i1 %17, i64 -1, i64 %18
  %20 = call noalias noundef nonnull ptr @_Znam(i64 noundef %19) #5
  %21 = getelementptr inbounds %"class.tbb::internal::affinity_helper", ptr %6, i32 0, i32 0
  store ptr %20, ptr %21, align 8, !tbaa !11
  %22 = getelementptr inbounds %"class.tbb::internal::affinity_helper", ptr %6, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  %24 = load i32, ptr @_ZN3tbb8internalL9num_masksE, align 4, !tbaa !14
  %25 = sext i32 %24 to i64
  %26 = mul i64 128, %25
  call void @llvm.memset.p0.i64(ptr align 8 %23, i8 0, i64 %26, i1 false)
  %27 = load i32, ptr @_ZN3tbb8internalL9num_masksE, align 4, !tbaa !14
  %28 = sext i32 %27 to i64
  %29 = mul i64 128, %28
  %30 = getelementptr inbounds %"class.tbb::internal::affinity_helper", ptr %6, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  call void @_ZN3tbb8internalL24get_thread_affinity_maskEmP9cpu_set_t(i64 noundef %29, ptr noundef %31)
  %32 = load i8, ptr %4, align 1, !tbaa !9, !range !15, !noundef !16
  %33 = trunc i8 %32 to i1
  br i1 %33, label %34, label %52

34:                                               ; preds = %13
  %35 = load ptr, ptr @_ZN3tbb8internalL12process_maskE, align 8, !tbaa !5
  %36 = getelementptr inbounds %"class.tbb::internal::affinity_helper", ptr %6, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !11
  %38 = load i32, ptr @_ZN3tbb8internalL9num_masksE, align 4, !tbaa !14
  %39 = sext i32 %38 to i64
  %40 = mul i64 128, %39
  %41 = call i32 @memcmp(ptr noundef %35, ptr noundef %37, i64 noundef %40) #6
  %42 = getelementptr inbounds %"class.tbb::internal::affinity_helper", ptr %6, i32 0, i32 1
  store i32 %41, ptr %42, align 8, !tbaa !17
  %43 = getelementptr inbounds %"class.tbb::internal::affinity_helper", ptr %6, i32 0, i32 1
  %44 = load i32, ptr %43, align 8, !tbaa !17
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %51

46:                                               ; preds = %34
  %47 = load i32, ptr @_ZN3tbb8internalL9num_masksE, align 4, !tbaa !14
  %48 = sext i32 %47 to i64
  %49 = mul i64 128, %48
  %50 = load ptr, ptr @_ZN3tbb8internalL12process_maskE, align 8, !tbaa !5
  call void @_ZN3tbb8internalL24set_thread_affinity_maskEmPK9cpu_set_t(i64 noundef %49, ptr noundef %50)
  br label %51

51:                                               ; preds = %46, %34
  br label %54

52:                                               ; preds = %13
  %53 = getelementptr inbounds %"class.tbb::internal::affinity_helper", ptr %6, i32 0, i32 1
  store i32 1, ptr %53, align 8, !tbaa !17
  br label %54

54:                                               ; preds = %52, %51
  br label %55

55:                                               ; preds = %54, %10, %2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64) #1

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #2

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

; Function Attrs: mustprogress uwtable
declare hidden void @_ZN3tbb8internalL24get_thread_affinity_maskEmP9cpu_set_t(i64 noundef, ptr noundef) #0

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #4

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 speculatable willreturn memory(none) }
attributes #2 = { nobuiltin allocsize(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 willreturn memory(read) "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 = { builtin allocsize(0) }
attributes #6 = { nounwind willreturn memory(read) }

!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 0}
!12 = !{!"_ZTSN3tbb8internal15affinity_helperE", !6, i64 0, !13, i64 8}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!12, !13, i64 8}


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

@_ZN3tbb8internalL12process_maskE = external hidden local_unnamed_addr global ptr, align 8
@_ZN3tbb8internalL9num_masksE = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress uwtable
declare hidden void @_ZN3tbb8internalL24set_thread_affinity_maskEmPK9cpu_set_t(i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3tbb8internal15affinity_helper21protect_affinity_maskEb(ptr nocapture noundef nonnull align 8 dereferenceable(12) %0, i1 noundef zeroext %1) local_unnamed_addr #0 align 2 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  %5 = load i32, ptr @_ZN3tbb8internalL9num_masksE, align 4
  %6 = icmp ne i32 %5, 0
  %or.cond = select i1 %4, i1 %6, i1 false
  br i1 %or.cond, label %7, label %24

7:                                                ; preds = %2
  %8 = sext i32 %5 to i64
  %9 = icmp slt i32 %5, 0
  %10 = shl nsw i64 %8, 7
  %11 = select i1 %9, i64 -1, i64 %10
  %12 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %11) #4
  store ptr %12, ptr %0, align 8, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr nonnull align 8 %12, i8 0, i64 %10, i1 false)
  tail call void @_ZN3tbb8internalL24get_thread_affinity_maskEmP9cpu_set_t(i64 noundef %10, ptr noundef nonnull %12)
  br i1 %1, label %13, label %22

13:                                               ; preds = %7
  %14 = load ptr, ptr @_ZN3tbb8internalL12process_maskE, align 8, !tbaa !11
  %15 = load ptr, ptr %0, align 8, !tbaa !5
  %16 = load i32, ptr @_ZN3tbb8internalL9num_masksE, align 4, !tbaa !12
  %17 = sext i32 %16 to i64
  %18 = shl nsw i64 %17, 7
  %19 = tail call i32 @memcmp(ptr noundef %14, ptr noundef %15, i64 noundef %18) #5
  %20 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %19, ptr %20, align 8, !tbaa !13
  %.not = icmp eq i32 %19, 0
  br i1 %.not, label %24, label %21

21:                                               ; preds = %13
  tail call void @_ZN3tbb8internalL24set_thread_affinity_maskEmPK9cpu_set_t(i64 noundef %18, ptr noundef %14)
  br label %24

22:                                               ; preds = %7
  %23 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 1, ptr %23, align 8, !tbaa !13
  br label %24

24:                                               ; preds = %22, %21, %13, %2
  ret void
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress uwtable
declare hidden void @_ZN3tbb8internalL24get_thread_affinity_maskEmP9cpu_set_t(i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @memcmp(ptr nocapture noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #3

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 = { nobuiltin allocsize(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 nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { builtin allocsize(0) }
attributes #5 = { nounwind willreturn memory(read) }

!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 0}
!6 = !{!"_ZTSN3tbb8internal15affinity_helperE", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !10, i64 8}


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