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

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

@g_NumMasters = external dso_local global i32, align 4
@g_LeavesExecuted = external dso_local global ptr, align 8
@g_TestFailures = external dso_local global [8 x i32], align 16
@g_CurConfig = external dso_local global i32, align 4
@.str.21 = external hidden unnamed_addr constant [94 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [35 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z11ReportErrorPKciS0_S0_(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #0

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z14PrepareGlobalsi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %3 = load i32, ptr @g_NumMasters, align 4, !tbaa !5
  %4 = icmp ne i32 %3, 0
  br i1 %4, label %9, label %5

5:                                                ; preds = %1
  %6 = load ptr, ptr @g_LeavesExecuted, align 8, !tbaa !9
  %7 = icmp ne ptr %6, null
  br i1 %7, label %9, label %8

8:                                                ; preds = %5
  br label %10

9:                                                ; preds = %5, %1
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.21, i32 noundef 86, ptr noundef @.str.22, ptr noundef null)
  br label %10

10:                                               ; preds = %9, %8
  %11 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %11, ptr @g_NumMasters, align 4, !tbaa !5
  %12 = load ptr, ptr @g_LeavesExecuted, align 8, !tbaa !9
  %13 = icmp ne ptr %12, null
  br i1 %13, label %22, label %14

14:                                               ; preds = %10
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %16, i64 8)
  %18 = extractvalue { i64, i1 } %17, 1
  %19 = extractvalue { i64, i1 } %17, 0
  %20 = select i1 %18, i64 -1, i64 %19
  %21 = call noalias noundef nonnull ptr @_Znam(i64 noundef %20) #4
  store ptr %21, ptr @g_LeavesExecuted, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %14, %10
  store i32 0, ptr @g_CurConfig, align 4, !tbaa !5
  %23 = load ptr, ptr @g_LeavesExecuted, align 8, !tbaa !9
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = mul i64 8, %25
  call void @llvm.memset.p0.i64(ptr align 8 %23, i8 0, i64 %26, i1 false)
  call void @llvm.memset.p0.i64(ptr align 16 @g_TestFailures, i8 0, i64 32, i1 false)
  ret void
}

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

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 #4 = { builtin allocsize(0) }

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


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

@g_NumMasters = external dso_local local_unnamed_addr global i32, align 4
@g_LeavesExecuted = external dso_local local_unnamed_addr global ptr, align 8
@g_TestFailures = external dso_local local_unnamed_addr global [8 x i32], align 16
@g_CurConfig = external dso_local local_unnamed_addr global i32, align 4
@.str.21 = external hidden unnamed_addr constant [94 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [35 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z11ReportErrorPKciS0_S0_(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z14PrepareGlobalsi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @g_NumMasters, align 4, !tbaa !5
  %3 = icmp ne i32 %2, 0
  %4 = load ptr, ptr @g_LeavesExecuted, align 8
  %5 = icmp ne ptr %4, null
  %or.cond = select i1 %3, i1 true, i1 %5
  br i1 %or.cond, label %6, label %.thread

.thread:                                          ; preds = %1
  store i32 %0, ptr @g_NumMasters, align 4, !tbaa !5
  br label %7

6:                                                ; preds = %1
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.21, i32 noundef 86, ptr noundef nonnull @.str.22, ptr noundef null)
  %.pr = load ptr, ptr @g_LeavesExecuted, align 8, !tbaa !9
  store i32 %0, ptr @g_NumMasters, align 4, !tbaa !5
  %.not = icmp eq ptr %.pr, null
  br i1 %.not, label %7, label %13

7:                                                ; preds = %.thread, %6
  %8 = zext nneg i32 %0 to i64
  %9 = icmp slt i32 %0, 0
  %10 = shl nuw nsw i64 %8, 3
  %11 = select i1 %9, i64 -1, i64 %10
  %12 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %11) #3
  store ptr %12, ptr @g_LeavesExecuted, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %7, %6
  %14 = phi ptr [ %12, %7 ], [ %.pr, %6 ]
  store i32 0, ptr @g_CurConfig, align 4, !tbaa !5
  %15 = sext i32 %0 to i64
  %16 = shl nsw i64 %15, 3
  tail call void @llvm.memset.p0.i64(ptr nonnull align 8 %14, i8 0, i64 %16, i1 false)
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) @g_TestFailures, i8 0, i64 32, i1 false)
  ret void
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #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 nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { builtin allocsize(0) }

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


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass