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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; 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

; Function Attrs: nounwind uwtable
define dso_local void @thread_mutex_init(ptr noundef %0, ptr noundef %1, ptr noundef %2) #4 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %9 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %9, align 4, !tbaa !9
  %10 = load i32, ptr @is_initialized, align 4, !tbaa !9
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %14

12:                                               ; preds = %3
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 -1, ptr %13, align 4, !tbaa !9
  store i32 1, ptr %8, align 4
  br label %85

14:                                               ; preds = %3
  %15 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.array_tag, ptr %15, i32 0, i32 3
  %17 = call i32 @pthread_mutex_lock(ptr noundef %16) #5
  %18 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.array_tag, ptr %18, i32 0, i32 2
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = load ptr, ptr @mutexes, 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 = icmp eq i32 %20, %23
  br i1 %24, label %25, label %30

25:                                               ; preds = %14
  %26 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.array_tag, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 8, !tbaa !13
  %29 = mul nsw i32 %28, 2
  call void @array_resize(ptr noundef @mutexes, i32 noundef %29)
  br label %30

30:                                               ; preds = %25, %14
  %31 = call noalias ptr @malloc(i64 noundef 40) #6
  %32 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.array_tag, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  %35 = load ptr, ptr @mutexes, 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 = sext i32 %37 to i64
  %39 = getelementptr inbounds ptr, ptr %34, i64 %38
  store ptr %31, ptr %39, align 8, !tbaa !5
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = icmp eq i32 %41, -1
  br i1 %42, label %43, label %44

43:                                               ; preds = %30
  store ptr null, ptr %7, align 8, !tbaa !5
  br label %53

44:                                               ; preds = %30
  %45 = load ptr, ptr @mutex_attributes, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.array_tag, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !14
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds ptr, ptr %47, i64 %50
  %52 = load ptr, ptr %51, align 8, !tbaa !5
  store ptr %52, ptr %7, align 8, !tbaa !5
  br label %53

53:                                               ; preds = %44, %43
  %54 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.array_tag, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !14
  %57 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.array_tag, ptr %57, i32 0, i32 2
  %59 = load i32, ptr %58, align 4, !tbaa !11
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds ptr, ptr %56, i64 %60
  %62 = load ptr, ptr %61, align 8, !tbaa !5
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = call i32 @pthread_mutex_init(ptr noundef %62, ptr noundef %63) #5
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %64, ptr %65, align 4, !tbaa !9
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = load i32, ptr %66, align 4, !tbaa !9
  %68 = icmp ne i32 %67, 0
  br i1 %68, label %69, label %73

69:                                               ; preds = %53
  %70 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.array_tag, ptr %70, i32 0, i32 3
  %72 = call i32 @pthread_mutex_unlock(ptr noundef %71) #5
  store i32 1, ptr %8, align 4
  br label %85

73:                                               ; preds = %53
  %74 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.array_tag, ptr %74, i32 0, i32 2
  %76 = load i32, ptr %75, align 4, !tbaa !11
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 %76, ptr %77, align 4, !tbaa !9
  %78 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.array_tag, ptr %78, i32 0, i32 2
  %80 = load i32, ptr %79, align 4, !tbaa !11
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %79, align 4, !tbaa !11
  %82 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.array_tag, ptr %82, i32 0, i32 3
  %84 = call i32 @pthread_mutex_unlock(ptr noundef %83) #5
  store i32 0, ptr %8, align 4
  br label %85

85:                                               ; preds = %73, %69, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  %86 = load i32, ptr %8, align 4
  switch i32 %86, label %88 [
    i32 0, label %87
    i32 1, label %87
  ]

87:                                               ; preds = %85, %85
  ret void

88:                                               ; preds = %85
  unreachable
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 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 #5 = { nounwind }
attributes #6 = { 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"

@mutexes = external dso_local global ptr, align 8
@mutex_attributes = external dso_local local_unnamed_addr 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
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #1

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

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

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

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

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

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

19:                                               ; preds = %17, %7
  %20 = phi i32 [ %.pre10, %17 ], [ %13, %7 ]
  %21 = phi ptr [ %.pre, %17 ], [ %11, %7 ]
  %22 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %23 = load ptr, ptr %21, align 8, !tbaa !14
  %24 = sext i32 %20 to i64
  %25 = getelementptr inbounds ptr, ptr %23, i64 %24
  store ptr %22, ptr %25, align 8, !tbaa !9
  %26 = load i32, ptr %1, align 4, !tbaa !5
  %27 = icmp eq i32 %26, -1
  br i1 %27, label %34, label %28

28:                                               ; preds = %19
  %29 = load ptr, ptr @mutex_attributes, align 8, !tbaa !9
  %30 = load ptr, ptr %29, align 8, !tbaa !14
  %31 = sext i32 %26 to i64
  %32 = getelementptr inbounds ptr, ptr %30, i64 %31
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %19, %28
  %.0 = phi ptr [ %33, %28 ], [ null, %19 ]
  %35 = load ptr, ptr @mutexes, align 8, !tbaa !9
  %36 = load ptr, ptr %35, align 8, !tbaa !14
  %37 = getelementptr inbounds i8, ptr %35, i64 12
  %38 = load i32, ptr %37, align 4, !tbaa !11
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds ptr, ptr %36, i64 %39
  %41 = load ptr, ptr %40, align 8, !tbaa !9
  %42 = tail call i32 @pthread_mutex_init(ptr noundef %41, ptr noundef %.0) #4
  store i32 %42, ptr %2, align 4, !tbaa !5
  %.not = icmp eq i32 %42, 0
  %43 = load ptr, ptr @mutexes, align 8, !tbaa !9
  br i1 %.not, label %47, label %44

44:                                               ; preds = %34
  %45 = getelementptr inbounds i8, ptr %43, i64 16
  %46 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %45) #4
  br label %53

47:                                               ; preds = %34
  %48 = getelementptr inbounds i8, ptr %43, i64 12
  %49 = load i32, ptr %48, align 4, !tbaa !11
  store i32 %49, ptr %0, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %48, align 4, !tbaa !11
  %51 = getelementptr inbounds i8, ptr %43, i64 16
  %52 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %51) #4
  br label %53

53:                                               ; preds = %47, %44, %6
  ret void
}

; Function Attrs: nounwind
declare i32 @pthread_mutex_init(ptr noundef, ptr noundef) local_unnamed_addr #1

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 "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 = { "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 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 }
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,SimplifyCFGPass,TailCallElimPass