; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/test/test_hw_concurrency.ll (ZN7Harness20LimitNumberOfThreadsEi)

; 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.17 = external hidden unnamed_addr constant [93 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [50 x i8], align 1
@_ZN7HarnessL8maxProcsE = external hidden global i32, align 4
@.str.20 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [95 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [39 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [68 x i8], align 1
@.str.24 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [32 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: mustprogress uwtable
declare dso_local void @_Z13ReportWarningPKciS0_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 uwtable
define dso_local noundef i32 @_ZN7Harness20LimitNumberOfThreadsEi(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca %struct.cpu_set_t, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = icmp sge i32 %10, 1
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  br label %14

13:                                               ; preds = %1
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.17, i32 noundef 55, ptr noundef @.str.18, ptr noundef @.str.19)
  br label %14

14:                                               ; preds = %13, %12
  %15 = call noundef i32 @_ZN7HarnessL11GetMaxProcsEv()
  store i32 %15, ptr @_ZN7HarnessL8maxProcsE, align 4, !tbaa !5
  %16 = load i32, ptr @_ZN7HarnessL8maxProcsE, align 4, !tbaa !5
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %19, label %21

19:                                               ; preds = %14
  %20 = load i32, ptr @_ZN7HarnessL8maxProcsE, align 4, !tbaa !5
  store i32 %20, ptr %2, align 4
  br label %74

21:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 128, ptr %4) #5
  br label %22

22:                                               ; preds = %21
  call void @llvm.memset.p0.i64(ptr align 8 %4, i8 0, i64 128, i1 false)
  br label %23

23:                                               ; preds = %22
  br label %24

24:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 1024, ptr %5, align 4, !tbaa !5
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = load i32, ptr @_ZN7HarnessL8maxProcsE, align 4, !tbaa !5
  %27 = icmp sge i32 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %24
  br label %30

29:                                               ; preds = %24
  call void @_Z13ReportWarningPKciS0_S0_(ptr noundef @.str.17, i32 noundef 87, ptr noundef @.str.20, ptr noundef @.str.21)
  br label %30

30:                                               ; preds = %29, %28
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = icmp sle i32 %31, 1024
  br i1 %32, label %33, label %34

33:                                               ; preds = %30
  br label %35

34:                                               ; preds = %30
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.17, i32 noundef 89, ptr noundef @.str.22, ptr noundef @.str.23)
  br label %35

35:                                               ; preds = %34, %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %62, %35
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = icmp slt i32 %37, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %65

41:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  store i64 %43, ptr %7, align 8, !tbaa !9
  %44 = load i64, ptr %7, align 8, !tbaa !9
  %45 = udiv i64 %44, 8
  %46 = icmp ult i64 %45, 128
  br i1 %46, label %47, label %58

47:                                               ; preds = %41
  %48 = load i64, ptr %7, align 8, !tbaa !9
  %49 = urem i64 %48, 64
  %50 = shl i64 1, %49
  %51 = getelementptr inbounds %struct.cpu_set_t, ptr %4, i32 0, i32 0
  %52 = getelementptr inbounds [16 x i64], ptr %51, i64 0, i64 0
  %53 = load i64, ptr %7, align 8, !tbaa !9
  %54 = udiv i64 %53, 64
  %55 = getelementptr inbounds i64, ptr %52, i64 %54
  %56 = load i64, ptr %55, align 8, !tbaa !9
  %57 = or i64 %56, %50
  store i64 %57, ptr %55, align 8, !tbaa !9
  br label %59

58:                                               ; preds = %41
  br label %59

59:                                               ; preds = %58, %47
  %60 = phi i64 [ %57, %47 ], [ 0, %58 ]
  store i64 %60, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  %61 = load i64, ptr %8, align 8, !tbaa !9
  br label %62

62:                                               ; preds = %59
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %6, align 4, !tbaa !5
  br label %36, !llvm.loop !11

65:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %66 = call i32 @getpid() #5
  %67 = call i32 @sched_setaffinity(i32 noundef %66, i64 noundef 128, ptr noundef %4) #5
  store i32 %67, ptr %9, align 4, !tbaa !5
  %68 = load i32, ptr %9, align 4, !tbaa !5
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %71, label %70

70:                                               ; preds = %65
  br label %72

71:                                               ; preds = %65
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.17, i32 noundef 94, ptr noundef @.str.24, ptr noundef @.str.25)
  br label %72

72:                                               ; preds = %71, %70
  %73 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %73, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 128, ptr %4) #5
  br label %74

74:                                               ; preds = %72, %19
  %75 = load i32, ptr %2, align 4
  ret i32 %75
}

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZN7HarnessL11GetMaxProcsEv() #3

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

; Function Attrs: nounwind
declare i32 @getpid() #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 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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.17 = external hidden unnamed_addr constant [93 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [50 x i8], align 1
@_ZN7HarnessL8maxProcsE = external hidden local_unnamed_addr global i32, align 4
@.str.20 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [95 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [39 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [68 x i8], align 1
@.str.24 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [32 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 uwtable
declare dso_local void @_Z13ReportWarningPKciS0_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 uwtable
define dso_local noundef i32 @_ZN7Harness20LimitNumberOfThreadsEi(i32 noundef %0) local_unnamed_addr #1 {
  %2 = alloca %struct.cpu_set_t, align 8
  %3 = icmp sgt i32 %0, 0
  br i1 %3, label %5, label %4

4:                                                ; preds = %1
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.17, i32 noundef 55, ptr noundef nonnull @.str.18, ptr noundef nonnull @.str.19)
  br label %5

5:                                                ; preds = %1, %4
  %6 = tail call noundef i32 @_ZN7HarnessL11GetMaxProcsEv()
  store i32 %6, ptr @_ZN7HarnessL8maxProcsE, align 4, !tbaa !5
  %7 = icmp slt i32 %6, %0
  br i1 %7, label %43, label %8

8:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %2) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(128) %2, i8 0, i64 128, i1 false)
  %9 = icmp slt i32 %6, 1025
  br i1 %9, label %11, label %10

10:                                               ; preds = %8
  tail call void @_Z13ReportWarningPKciS0_S0_(ptr noundef nonnull @.str.17, i32 noundef 87, ptr noundef nonnull @.str.20, ptr noundef nonnull @.str.21)
  br label %11

11:                                               ; preds = %8, %10
  %12 = icmp slt i32 %0, 1025
  br i1 %12, label %13, label %.thread

.thread:                                          ; preds = %11
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.17, i32 noundef 89, ptr noundef nonnull @.str.22, ptr noundef nonnull @.str.23)
  br label %.lr.ph.preheader

13:                                               ; preds = %11
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.thread, %13
  %wide.trip.count = zext i32 %0 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %14 = icmp eq i32 %0, 1
  br i1 %14, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 4294967294
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %40, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %40 ]
  %lcmp.mod.not = icmp ne i64 %xtraiter, 0
  %15 = icmp ult i64 %indvars.iv.unr, 1024
  %or.cond = select i1 %lcmp.mod.not, i1 %15, i1 false
  br i1 %or.cond, label %16, label %._crit_edge

16:                                               ; preds = %._crit_edge.loopexit.unr-lcssa
  %17 = and i64 %indvars.iv.unr, 63
  %18 = shl nuw i64 1, %17
  %19 = lshr i64 %indvars.iv.unr, 6
  %20 = getelementptr inbounds i64, ptr %2, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !9
  %22 = or i64 %21, %18
  store i64 %22, ptr %20, align 8, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %16, %13
  %23 = tail call i32 @getpid() #5
  %24 = call i32 @sched_setaffinity(i32 noundef %23, i64 noundef 128, ptr noundef nonnull %2) #5
  %.not = icmp eq i32 %24, 0
  br i1 %.not, label %42, label %41

.lr.ph:                                           ; preds = %40, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %40 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %40 ]
  %25 = icmp ult i64 %indvars.iv, 1024
  br i1 %25, label %26, label %.lr.ph.1

26:                                               ; preds = %.lr.ph
  %27 = and i64 %indvars.iv, 62
  %28 = shl nuw nsw i64 1, %27
  %29 = lshr i64 %indvars.iv, 6
  %30 = getelementptr inbounds i64, ptr %2, i64 %29
  %31 = load i64, ptr %30, align 8, !tbaa !9
  %32 = or i64 %31, %28
  store i64 %32, ptr %30, align 8, !tbaa !9
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %26
  %33 = icmp ult i64 %indvars.iv, 1024
  br i1 %33, label %34, label %40

34:                                               ; preds = %.lr.ph.1
  %indvars.iv.next = and i64 %indvars.iv, 62
  %35 = shl nuw i64 2, %indvars.iv.next
  %36 = lshr i64 %indvars.iv, 6
  %37 = getelementptr inbounds i64, ptr %2, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !9
  %39 = or i64 %38, %35
  store i64 %39, ptr %37, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %34, %.lr.ph.1
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11

41:                                               ; preds = %._crit_edge
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.17, i32 noundef 94, ptr noundef nonnull @.str.24, ptr noundef nonnull @.str.25)
  br label %42

42:                                               ; preds = %._crit_edge, %41
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %2) #5
  br label %43

43:                                               ; preds = %5, %42
  %.0 = phi i32 [ %0, %42 ], [ %6, %5 ]
  ret i32 %.0
}

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZN7HarnessL11GetMaxProcsEv() local_unnamed_addr #3

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

; Function Attrs: nounwind
declare i32 @getpid() 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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