; LLVM IR from ponylang/ponyc :: src/libponyrt/platform (threads)

; 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.rlimit = type { i64, i64 }
%union.pthread_attr_t = type { i64, [48 x i8] }
%struct.cpu_set_t = type { [16 x i64] }

@_numa_node_of_cpu = external hidden global ptr, align 8
@_numa_alloc_onnode = external hidden global ptr, align 8
@use_numa = external hidden global 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: nounwind uwtable
define dso_local zeroext i1 @ponyint_thread_create(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca i8, align 1
  %11 = alloca %struct.rlimit, align 8
  %12 = alloca %union.pthread_attr_t, align 8
  %13 = alloca ptr, align 8
  %14 = alloca %struct.cpu_set_t, align 8
  %15 = alloca i64, align 8
  %16 = alloca i64, align 8
  %17 = alloca i32, align 4
  %18 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #4
  store i8 1, ptr %9, align 1, !tbaa !11
  %19 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #4
  store i8 0, ptr %10, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 56, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  store ptr %12, ptr %13, align 8, !tbaa !5
  %20 = load ptr, ptr %13, align 8, !tbaa !5
  %21 = call i32 @pthread_attr_init(ptr noundef %20) #4
  %22 = call i32 @getrlimit(i32 noundef 3, ptr noundef %11) #4
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %87

24:                                               ; preds = %4
  %25 = getelementptr inbounds %struct.rlimit, ptr %11, i32 0, i32 0
  %26 = load i64, ptr %25, align 8, !tbaa !13
  %27 = icmp ne i64 %26, -1
  br i1 %27, label %28, label %87

28:                                               ; preds = %24
  %29 = getelementptr inbounds %struct.rlimit, ptr %11, i32 0, i32 0
  %30 = load i64, ptr %29, align 8, !tbaa !13
  %31 = call i64 @__sysconf(i32 noundef 75) #4
  %32 = icmp uge i64 %30, %31
  br i1 %32, label %33, label %87

33:                                               ; preds = %28
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = icmp ne i32 %34, -1
  br i1 %35, label %36, label %79

36:                                               ; preds = %33
  %37 = load i8, ptr @use_numa, align 1, !tbaa !11, !range !16, !noundef !17
  %38 = trunc i8 %37 to i1
  br i1 %38, label %39, label %79

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

40:                                               ; preds = %39
  call void @llvm.memset.p0.i64(ptr align 8 %14, i8 0, i64 128, i1 false)
  br label %41

41:                                               ; preds = %40
  br label %42

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

48:                                               ; preds = %42
  %49 = load i64, ptr %15, align 8, !tbaa !18
  %50 = urem i64 %49, 64
  %51 = shl i64 1, %50
  %52 = getelementptr inbounds %struct.cpu_set_t, ptr %14, i32 0, i32 0
  %53 = getelementptr inbounds [16 x i64], ptr %52, i64 0, i64 0
  %54 = load i64, ptr %15, align 8, !tbaa !18
  %55 = udiv i64 %54, 64
  %56 = getelementptr inbounds i64, ptr %53, i64 %55
  %57 = load i64, ptr %56, align 8, !tbaa !18
  %58 = or i64 %57, %51
  store i64 %58, ptr %56, align 8, !tbaa !18
  br label %60

59:                                               ; preds = %42
  br label %60

60:                                               ; preds = %59, %48
  %61 = phi i64 [ %58, %48 ], [ 0, %59 ]
  store i64 %61, ptr %16, align 8, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  %62 = load i64, ptr %16, align 8, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  %63 = load ptr, ptr @_numa_node_of_cpu, align 8, !tbaa !5
  %64 = load i32, ptr %7, align 4, !tbaa !9
  %65 = call i32 %63(i32 noundef %64)
  store i32 %65, ptr %17, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #4
  %66 = load ptr, ptr @_numa_alloc_onnode, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.rlimit, ptr %11, i32 0, i32 0
  %68 = load i64, ptr %67, align 8, !tbaa !13
  %69 = load i32, ptr %17, align 4, !tbaa !9
  %70 = call ptr %66(i64 noundef %68, i32 noundef %69)
  store ptr %70, ptr %18, align 8, !tbaa !5
  %71 = load ptr, ptr %18, align 8, !tbaa !5
  %72 = icmp ne ptr %71, null
  br i1 %72, label %73, label %78

73:                                               ; preds = %60
  %74 = load ptr, ptr %18, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.rlimit, ptr %11, i32 0, i32 0
  %76 = load i64, ptr %75, align 8, !tbaa !13
  %77 = call i32 @pthread_attr_setstack(ptr noundef %12, ptr noundef %74, i64 noundef %76) #4
  store i8 1, ptr %10, align 1, !tbaa !11
  br label %78

78:                                               ; preds = %73, %60
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 128, ptr %14) #4
  br label %79

79:                                               ; preds = %78, %36, %33
  %80 = load i8, ptr %10, align 1, !tbaa !11, !range !16, !noundef !17
  %81 = trunc i8 %80 to i1
  br i1 %81, label %86, label %82

82:                                               ; preds = %79
  %83 = getelementptr inbounds %struct.rlimit, ptr %11, i32 0, i32 0
  %84 = load i64, ptr %83, align 8, !tbaa !13
  %85 = call i32 @pthread_attr_setstacksize(ptr noundef %12, i64 noundef %84) #4
  br label %86

86:                                               ; preds = %82, %79
  br label %88

87:                                               ; preds = %28, %24, %4
  store ptr null, ptr %13, align 8, !tbaa !5
  br label %88

88:                                               ; preds = %87, %86
  %89 = load ptr, ptr %5, align 8, !tbaa !5
  %90 = load ptr, ptr %13, align 8, !tbaa !5
  %91 = load ptr, ptr %6, align 8, !tbaa !5
  %92 = load ptr, ptr %8, align 8, !tbaa !5
  %93 = call i32 @pthread_create(ptr noundef %89, ptr noundef %90, ptr noundef %91, ptr noundef %92) #4
  %94 = icmp ne i32 %93, 0
  br i1 %94, label %95, label %96

95:                                               ; preds = %88
  store i8 0, ptr %9, align 1, !tbaa !11
  br label %96

96:                                               ; preds = %95, %88
  %97 = call i32 @pthread_attr_destroy(ptr noundef %12) #4
  %98 = load i8, ptr %9, align 1, !tbaa !11, !range !16, !noundef !17
  %99 = trunc i8 %98 to i1
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 56, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #4
  ret i1 %99
}

; Function Attrs: nounwind
declare i32 @pthread_attr_init(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @getrlimit(i32 noundef, ptr noundef) #2

; Function Attrs: nounwind
declare i64 @__sysconf(i32 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: nounwind
declare i32 @pthread_attr_setstack(ptr noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind
declare i32 @pthread_attr_setstacksize(ptr noundef, i64 noundef) #2

; Function Attrs: nounwind
declare i32 @pthread_create(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind
declare i32 @pthread_attr_destroy(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"rlimit", !15, i64 0, !15, i64 8}
!15 = !{!"long", !7, i64 0}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!15, !15, 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"

%struct.rlimit = type { i64, i64 }
%union.pthread_attr_t = type { i64, [48 x i8] }

@_numa_node_of_cpu = external hidden local_unnamed_addr global ptr, align 8
@_numa_alloc_onnode = external hidden local_unnamed_addr global ptr, align 8
@use_numa = external hidden local_unnamed_addr global 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: nounwind uwtable
define dso_local zeroext i1 @ponyint_thread_create(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = alloca %struct.rlimit, align 8
  %6 = alloca %union.pthread_attr_t, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 56, ptr nonnull %6) #3
  %7 = call i32 @pthread_attr_init(ptr noundef nonnull %6) #3
  %8 = call i32 @getrlimit(i32 noundef 3, ptr noundef nonnull %5) #3
  %9 = icmp eq i32 %8, 0
  %10 = load i64, ptr %5, align 8
  %11 = icmp ne i64 %10, -1
  %or.cond = select i1 %9, i1 %11, i1 false
  br i1 %or.cond, label %12, label %29

12:                                               ; preds = %4
  %13 = call i64 @__sysconf(i32 noundef 75) #3
  %.not = icmp ult i64 %10, %13
  br i1 %.not, label %29, label %14

14:                                               ; preds = %12
  %.not24 = icmp eq i32 %2, -1
  br i1 %.not24, label %.critedge, label %15

15:                                               ; preds = %14
  %16 = load i8, ptr @use_numa, align 1, !tbaa !5, !range !9, !noundef !10
  %17 = trunc nuw i8 %16 to i1
  br i1 %17, label %18, label %.critedge

18:                                               ; preds = %15
  %19 = load ptr, ptr @_numa_node_of_cpu, align 8, !tbaa !11
  %20 = call i32 %19(i32 noundef %2) #3
  %21 = load ptr, ptr @_numa_alloc_onnode, align 8, !tbaa !11
  %22 = load i64, ptr %5, align 8, !tbaa !13
  %23 = call ptr %21(i64 noundef %22, i32 noundef %20) #3
  %.not25.not = icmp eq ptr %23, null
  br i1 %.not25.not, label %.critedge, label %24

24:                                               ; preds = %18
  %25 = load i64, ptr %5, align 8, !tbaa !13
  %26 = call i32 @pthread_attr_setstack(ptr noundef nonnull %6, ptr noundef nonnull %23, i64 noundef %25) #3
  br label %29

.critedge:                                        ; preds = %18, %15, %14
  %27 = load i64, ptr %5, align 8, !tbaa !13
  %28 = call i32 @pthread_attr_setstacksize(ptr noundef nonnull %6, i64 noundef %27) #3
  br label %29

29:                                               ; preds = %24, %4, %12, %.critedge
  %.020 = phi ptr [ %6, %24 ], [ %6, %.critedge ], [ null, %12 ], [ null, %4 ]
  %30 = call i32 @pthread_create(ptr noundef %0, ptr noundef %.020, ptr noundef %1, ptr noundef %3) #3
  %.not26 = icmp eq i32 %30, 0
  %31 = call i32 @pthread_attr_destroy(ptr noundef nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 56, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #3
  ret i1 %.not26
}

; Function Attrs: nounwind
declare i32 @pthread_attr_init(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @getrlimit(i32 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i64 @__sysconf(i32 noundef) local_unnamed_addr #2

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

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

; Function Attrs: nounwind
declare i32 @pthread_create(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @pthread_attr_destroy(ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { 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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"rlimit", !15, i64 0, !15, i64 8}
!15 = !{!"long", !7, i64 0}


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