; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/perf/perf.ll (ZN4Perf8internal12PinTheThreadEiRN3tbb6atomicIiEE)

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

%struct.cpu_set_t = type { [16 x i64] }

@.str.22 = external hidden unnamed_addr constant [80 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [244 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [25 x i8], align 1

; 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
declare dso_local void @_Z11ReportErrorPKciS0_S0_(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN4Perf8internal6gettidEv() #3

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN4Perf8internal12PinTheThreadEiRN3tbb6atomicIiEE(i32 noundef %0, ptr noundef nonnull align 4 dereferenceable(4) %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca %struct.cpu_set_t, align 8
  %6 = alloca %struct.cpu_set_t, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 128, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 128, ptr %6) #5
  br label %12

12:                                               ; preds = %2
  call void @llvm.memset.p0.i64(ptr align 8 %6, i8 0, i64 128, i1 false)
  br label %13

13:                                               ; preds = %12
  br label %14

14:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  store i64 %16, ptr %7, align 8, !tbaa !11
  %17 = load i64, ptr %7, align 8, !tbaa !11
  %18 = udiv i64 %17, 8
  %19 = icmp ult i64 %18, 128
  br i1 %19, label %20, label %31

20:                                               ; preds = %14
  %21 = load i64, ptr %7, align 8, !tbaa !11
  %22 = urem i64 %21, 64
  %23 = shl i64 1, %22
  %24 = getelementptr inbounds %struct.cpu_set_t, ptr %6, i32 0, i32 0
  %25 = getelementptr inbounds [16 x i64], ptr %24, i64 0, i64 0
  %26 = load i64, ptr %7, align 8, !tbaa !11
  %27 = udiv i64 %26, 64
  %28 = getelementptr inbounds i64, ptr %25, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !11
  %30 = or i64 %29, %23
  store i64 %30, ptr %28, align 8, !tbaa !11
  br label %32

31:                                               ; preds = %14
  br label %32

32:                                               ; preds = %31, %20
  %33 = phi i64 [ %30, %20 ], [ 0, %31 ]
  store i64 %33, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  %34 = load i64, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  store i64 %36, ptr %9, align 8, !tbaa !11
  %37 = load i64, ptr %9, align 8, !tbaa !11
  %38 = udiv i64 %37, 8
  %39 = icmp ult i64 %38, 128
  br i1 %39, label %40, label %53

40:                                               ; preds = %32
  %41 = getelementptr inbounds %struct.cpu_set_t, ptr %6, i32 0, i32 0
  %42 = getelementptr inbounds [16 x i64], ptr %41, i64 0, i64 0
  %43 = load i64, ptr %9, align 8, !tbaa !11
  %44 = udiv i64 %43, 64
  %45 = getelementptr inbounds i64, ptr %42, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !11
  %47 = load i64, ptr %9, align 8, !tbaa !11
  %48 = urem i64 %47, 64
  %49 = shl i64 1, %48
  %50 = and i64 %46, %49
  %51 = icmp ne i64 %50, 0
  %52 = zext i1 %51 to i32
  br label %54

53:                                               ; preds = %32
  br label %54

54:                                               ; preds = %53, %40
  %55 = phi i32 [ %52, %40 ], [ 0, %53 ]
  store i32 %55, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  %56 = load i32, ptr %10, align 4, !tbaa !5
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %59

58:                                               ; preds = %54
  br label %60

59:                                               ; preds = %54
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.22, i32 noundef 348, ptr noundef @.str.28, ptr noundef @.str.29)
  br label %60

60:                                               ; preds = %59, %58
  br label %61

61:                                               ; preds = %60
  call void @llvm.memset.p0.i64(ptr align 8 %5, i8 0, i64 128, i1 false)
  br label %62

62:                                               ; preds = %61
  br label %63

63:                                               ; preds = %62
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  %64 = call noundef i32 @_ZN4Perf8internal6gettidEv()
  %65 = call i32 @sched_getaffinity(i32 noundef %64, i64 noundef 128, ptr noundef %5) #5
  store i32 %65, ptr %11, align 4, !tbaa !5
  %66 = load i32, ptr %11, align 4, !tbaa !5
  %67 = icmp eq i32 %66, 0
  br i1 %67, label %68, label %69

68:                                               ; preds = %63
  br label %70

69:                                               ; preds = %63
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.22, i32 noundef 357, ptr noundef @.str.30, ptr noundef @.str.31)
  br label %70

70:                                               ; preds = %69, %68
  %71 = call noundef i32 @_ZN4Perf8internal6gettidEv()
  %72 = call i32 @sched_setaffinity(i32 noundef %71, i64 noundef 128, ptr noundef %6) #5
  store i32 %72, ptr %11, align 4, !tbaa !5
  %73 = load i32, ptr %11, align 4, !tbaa !5
  %74 = icmp eq i32 %73, 0
  br i1 %74, label %75, label %76

75:                                               ; preds = %70
  br label %77

76:                                               ; preds = %70
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.22, i32 noundef 359, ptr noundef @.str.30, ptr noundef @.str.32)
  br label %77

77:                                               ; preds = %76, %75
  %78 = load ptr, ptr %4, align 8, !tbaa !9
  %79 = call noundef i32 @_ZN3tbb8internal27atomic_impl_with_arithmeticIiicEmmEv(ptr noundef nonnull align 4 dereferenceable(4) %78)
  br label %80

80:                                               ; preds = %84, %77
  %81 = load ptr, ptr %4, align 8, !tbaa !9
  %82 = call noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4) %81)
  %83 = icmp ne i32 %82, 0
  br i1 %83, label %84, label %86

84:                                               ; preds = %80
  %85 = call i32 @sched_yield() #5
  br label %80, !llvm.loop !13

86:                                               ; preds = %80
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 128, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 128, ptr %5) #5
  ret i1 true
}

; Function Attrs: nounwind
declare i32 @sched_getaffinity(i32 noundef, i64 noundef, ptr noundef) #4

; Function Attrs: nounwind
declare i32 @sched_setaffinity(i32 noundef, i64 noundef, ptr noundef) #4

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN3tbb8internal27atomic_impl_with_arithmeticIiicEmmEv(ptr noundef nonnull align 4 dereferenceable(4)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4)) #1 align 2

; Function Attrs: nounwind
declare i32 @sched_yield() #4

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 "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 }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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

%struct.cpu_set_t = type { [16 x i64] }

@.str.22 = external hidden unnamed_addr constant [80 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [244 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [25 x i8], align 1

; 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
declare dso_local void @_Z11ReportErrorPKciS0_S0_(ptr noundef, i32 noundef, ptr noundef, ptr 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 nounwind uwtable
declare dso_local noundef i32 @_ZN4Perf8internal6gettidEv() local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN4Perf8internal12PinTheThreadEiRN3tbb6atomicIiEE(i32 noundef %0, ptr noundef nonnull align 4 dereferenceable(4) %1) local_unnamed_addr #1 {
  %3 = alloca %struct.cpu_set_t, align 8
  %4 = alloca %struct.cpu_set_t, align 8
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %3) #5
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %4) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(128) %4, i8 0, i64 128, i1 false)
  %5 = icmp ult i32 %0, 1024
  br i1 %5, label %.critedge, label %.critedge15

.critedge:                                        ; preds = %2
  %6 = zext nneg i32 %0 to i64
  %7 = and i64 %6, 63
  %8 = shl nuw i64 1, %7
  %9 = lshr i64 %6, 6
  %10 = getelementptr inbounds i64, ptr %4, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = or i64 %11, %8
  store i64 %12, ptr %10, align 8, !tbaa !5
  br label %13

.critedge15:                                      ; preds = %2
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.22, i32 noundef 348, ptr noundef nonnull @.str.28, ptr noundef nonnull @.str.29)
  br label %13

13:                                               ; preds = %.critedge, %.critedge15
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(128) %3, i8 0, i64 128, i1 false)
  %14 = tail call noundef i32 @_ZN4Perf8internal6gettidEv()
  %15 = call i32 @sched_getaffinity(i32 noundef %14, i64 noundef 128, ptr noundef nonnull %3) #5
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.22, i32 noundef 357, ptr noundef nonnull @.str.30, ptr noundef nonnull @.str.31)
  br label %18

18:                                               ; preds = %13, %17
  %19 = call noundef i32 @_ZN4Perf8internal6gettidEv()
  %20 = call i32 @sched_setaffinity(i32 noundef %19, i64 noundef 128, ptr noundef nonnull %4) #5
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.22, i32 noundef 359, ptr noundef nonnull @.str.30, ptr noundef nonnull @.str.32)
  br label %23

23:                                               ; preds = %18, %22
  %24 = call noundef i32 @_ZN3tbb8internal27atomic_impl_with_arithmeticIiicEmmEv(ptr noundef nonnull align 4 dereferenceable(4) %1)
  %25 = call noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4) %1)
  %.not1416 = icmp eq i32 %25, 0
  br i1 %.not1416, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %23, %.lr.ph
  %26 = call i32 @sched_yield() #5
  %27 = call noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4) %1)
  %.not14 = icmp eq i32 %27, 0
  br i1 %.not14, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %23
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %4) #5
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %3) #5
  ret i1 true
}

; Function Attrs: nounwind
declare i32 @sched_getaffinity(i32 noundef, i64 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind
declare i32 @sched_setaffinity(i32 noundef, i64 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN3tbb8internal27atomic_impl_with_arithmeticIiicEmmEv(ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1 align 2

; Function Attrs: nounwind
declare i32 @sched_yield() local_unnamed_addr #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 nocallback nofree nounwind willreturn memory(argmem: write) }
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 "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 }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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