; LLVM IR from andreypudov/algorithms :: Core/Threads (PThreads)

; 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.array_tag = type { ptr, i32, i32, %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@threads = external dso_local global ptr, align 8
@is_initialized = external dso_local global i32, align 4

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @thread_alloc(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %5 = load i32, ptr @is_initialized, align 4, !tbaa !9
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %7, label %9

7:                                                ; preds = %2
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 -1, ptr %8, align 4, !tbaa !9
  br label %46

9:                                                ; preds = %2
  %10 = load ptr, ptr @threads, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.array_tag, ptr %10, i32 0, i32 3
  %12 = call i32 @pthread_mutex_lock(ptr noundef %11) #4
  %13 = load ptr, ptr @threads, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.array_tag, ptr %13, i32 0, i32 2
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = load ptr, ptr @threads, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.array_tag, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 8, !tbaa !13
  %19 = icmp eq i32 %15, %18
  br i1 %19, label %20, label %25

20:                                               ; preds = %9
  %21 = load ptr, ptr @threads, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.array_tag, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 8, !tbaa !13
  %24 = mul nsw i32 %23, 2
  call void @array_resize(ptr noundef @threads, i32 noundef %24)
  br label %25

25:                                               ; preds = %20, %9
  %26 = call noalias ptr @malloc(i64 noundef 8) #5
  %27 = load ptr, ptr @threads, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.array_tag, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !14
  %30 = load ptr, ptr @threads, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.array_tag, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 4, !tbaa !11
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds ptr, ptr %29, i64 %33
  store ptr %26, ptr %34, align 8, !tbaa !5
  %35 = load ptr, ptr @threads, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.array_tag, ptr %35, i32 0, i32 2
  %37 = load i32, ptr %36, align 4, !tbaa !11
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  store i32 %37, ptr %38, align 4, !tbaa !9
  %39 = load ptr, ptr @threads, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.array_tag, ptr %39, i32 0, i32 2
  %41 = load i32, ptr %40, align 4, !tbaa !11
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %40, align 4, !tbaa !11
  %43 = load ptr, ptr @threads, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.array_tag, ptr %43, i32 0, i32 3
  %45 = call i32 @pthread_mutex_unlock(ptr noundef %44) #4
  br label %46

46:                                               ; preds = %25, %7
  ret void
}

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

declare void @array_resize(ptr noundef, i32 noundef) #3

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

attributes #0 = { nounwind 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 #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 = { "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 }
attributes #5 = { nounwind 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 = !{!"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, !10, i64 12}
!12 = !{!"array_tag", !6, i64 0, !10, i64 8, !10, i64 12, !7, i64 16}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !6, 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"

@threads = external dso_local global ptr, align 8
@is_initialized = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @thread_alloc(ptr nocapture noundef writeonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = load i32, ptr @is_initialized, align 4, !tbaa !5
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %5, label %6

5:                                                ; preds = %2
  store i32 -1, ptr %1, align 4, !tbaa !5
  br label %31

6:                                                ; preds = %2
  %7 = load ptr, ptr @threads, align 8, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  %9 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %8) #4
  %10 = load ptr, ptr @threads, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %10, i64 12
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %10, i64 8
  %14 = load i32, ptr %13, align 8, !tbaa !13
  %15 = icmp eq i32 %12, %14
  br i1 %15, label %16, label %18

16:                                               ; preds = %6
  %17 = shl nsw i32 %12, 1
  tail call void @array_resize(ptr noundef nonnull @threads, i32 noundef %17) #4
  %.pre = load ptr, ptr @threads, align 8, !tbaa !9
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.pre, i64 12
  %.pre2 = load i32, ptr %.phi.trans.insert, align 4, !tbaa !11
  br label %18

18:                                               ; preds = %16, %6
  %19 = phi i32 [ %.pre2, %16 ], [ %12, %6 ]
  %20 = phi ptr [ %.pre, %16 ], [ %10, %6 ]
  %21 = tail call noalias dereferenceable_or_null(8) ptr @malloc(i64 noundef 8) #5
  %22 = load ptr, ptr %20, align 8, !tbaa !14
  %23 = sext i32 %19 to i64
  %24 = getelementptr inbounds ptr, ptr %22, i64 %23
  store ptr %21, ptr %24, align 8, !tbaa !9
  %25 = load ptr, ptr @threads, align 8, !tbaa !9
  %26 = getelementptr inbounds i8, ptr %25, i64 12
  %27 = load i32, ptr %26, align 4, !tbaa !11
  store i32 %27, ptr %0, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %26, align 4, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %25, i64 16
  %30 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %29) #4
  br label %31

31:                                               ; preds = %18, %5
  ret void
}

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

declare void @array_resize(ptr noundef, i32 noundef) local_unnamed_addr #3

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

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { 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 = { "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 }
attributes #5 = { nounwind 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 12}
!12 = !{!"array_tag", !10, i64 0, !6, i64 8, !6, i64 12, !7, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !10, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass