; 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
@thread_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

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

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

17:                                               ; preds = %5
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  store i32 -1, ptr %18, align 4, !tbaa !9
  store i32 1, ptr %13, align 4
  br label %92

19:                                               ; preds = %5
  %20 = load ptr, ptr @threads, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load i32, ptr %21, align 4, !tbaa !9
  %23 = call i32 @is_valid(ptr noundef %20, i32 noundef %22)
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %27

25:                                               ; preds = %19
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  store i32 -2, ptr %26, align 4, !tbaa !9
  store i32 1, ptr %13, align 4
  br label %92

27:                                               ; preds = %19
  %28 = load ptr, ptr @threads, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.array_tag, ptr %28, i32 0, i32 3
  %30 = call i32 @pthread_mutex_lock(ptr noundef %29) #5
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = icmp eq i32 %32, -1
  br i1 %33, label %34, label %40

34:                                               ; preds = %27
  %35 = call noalias ptr @malloc(i64 noundef 56) #6
  store ptr %35, ptr %11, align 8, !tbaa !5
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = call i32 @pthread_attr_init(ptr noundef %36) #5
  %38 = load ptr, ptr %11, align 8, !tbaa !5
  %39 = call i32 @pthread_attr_setdetachstate(ptr noundef %38, i32 noundef 0) #5
  br label %60

40:                                               ; preds = %27
  %41 = load ptr, ptr @thread_attributes, align 8, !tbaa !5
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load i32, ptr %42, align 4, !tbaa !9
  %44 = call i32 @is_valid(ptr noundef %41, i32 noundef %43)
  %45 = icmp eq i32 %44, 0
  br i1 %45, label %46, label %51

46:                                               ; preds = %40
  %47 = load ptr, ptr @threads, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.array_tag, ptr %47, i32 0, i32 3
  %49 = call i32 @pthread_mutex_unlock(ptr noundef %48) #5
  %50 = load ptr, ptr %10, align 8, !tbaa !5
  store i32 -2, ptr %50, align 4, !tbaa !9
  store i32 1, ptr %13, align 4
  br label %92

51:                                               ; preds = %40
  %52 = load ptr, ptr @thread_attributes, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.array_tag, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !11
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds ptr, ptr %54, i64 %57
  %59 = load ptr, ptr %58, align 8, !tbaa !5
  store ptr %59, ptr %11, align 8, !tbaa !5
  br label %60

60:                                               ; preds = %51, %34
  %61 = load ptr, ptr @threads, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.array_tag, ptr %61, i32 0, i32 0
  %63 = load ptr, ptr %62, align 8, !tbaa !11
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = load i32, ptr %64, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds ptr, ptr %63, i64 %66
  %68 = load ptr, ptr %67, align 8, !tbaa !5
  %69 = load ptr, ptr %11, align 8, !tbaa !5
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = load ptr, ptr %70, align 8, !tbaa !5
  %72 = load ptr, ptr %9, align 8, !tbaa !5
  %73 = call i32 @pthread_create(ptr noundef %68, ptr noundef %69, ptr noundef %71, ptr noundef %72) #5
  %74 = load ptr, ptr %10, align 8, !tbaa !5
  store i32 %73, ptr %74, align 4, !tbaa !9
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  %76 = load i32, ptr %75, align 4, !tbaa !9
  %77 = icmp eq i32 %76, -1
  br i1 %77, label %78, label %80

78:                                               ; preds = %60
  %79 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %79) #5
  br label %80

80:                                               ; preds = %78, %60
  %81 = load ptr, ptr %10, align 8, !tbaa !5
  %82 = load i32, ptr %81, align 4, !tbaa !9
  %83 = icmp ne i32 %82, 0
  br i1 %83, label %84, label %88

84:                                               ; preds = %80
  %85 = load ptr, ptr @threads, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.array_tag, ptr %85, i32 0, i32 3
  %87 = call i32 @pthread_mutex_unlock(ptr noundef %86) #5
  store i32 1, ptr %13, align 4
  br label %92

88:                                               ; preds = %80
  %89 = load ptr, ptr @threads, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.array_tag, ptr %89, i32 0, i32 3
  %91 = call i32 @pthread_mutex_unlock(ptr noundef %90) #5
  store i32 0, ptr %13, align 4
  br label %92

92:                                               ; preds = %88, %84, %46, %25, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  %93 = load i32, ptr %13, align 4
  switch i32 %93, label %95 [
    i32 0, label %94
    i32 1, label %94
  ]

94:                                               ; preds = %92, %92
  ret void

95:                                               ; preds = %92
  unreachable
}

declare i32 @is_valid(ptr noundef, i32 noundef) #4

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

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

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

; Function Attrs: nounwind
declare void @free(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 = { 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 = { "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, !6, i64 0}
!12 = !{!"array_tag", !6, i64 0, !10, i64 8, !10, i64 12, !7, i64 16}


; 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 local_unnamed_addr global ptr, align 8
@thread_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

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

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

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

9:                                                ; preds = %5
  %10 = load ptr, ptr @threads, align 8, !tbaa !9
  %11 = load i32, ptr %0, align 4, !tbaa !5
  %12 = tail call i32 @is_valid(ptr noundef %10, i32 noundef %11) #5
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %9
  store i32 -2, ptr %4, align 4, !tbaa !5
  br label %56

15:                                               ; preds = %9
  %16 = load ptr, ptr @threads, align 8, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %18 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %17) #5
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = icmp eq i32 %19, -1
  br i1 %20, label %21, label %25

21:                                               ; preds = %15
  %22 = tail call noalias dereferenceable_or_null(56) ptr @malloc(i64 noundef 56) #6
  %23 = tail call i32 @pthread_attr_init(ptr noundef %22) #5
  %24 = tail call i32 @pthread_attr_setdetachstate(ptr noundef %22, i32 noundef 0) #5
  br label %40

25:                                               ; preds = %15
  %26 = load ptr, ptr @thread_attributes, align 8, !tbaa !9
  %27 = tail call i32 @is_valid(ptr noundef %26, i32 noundef %19) #5
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %33

29:                                               ; preds = %25
  %30 = load ptr, ptr @threads, align 8, !tbaa !9
  %31 = getelementptr inbounds i8, ptr %30, i64 16
  %32 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %31) #5
  store i32 -2, ptr %4, align 4, !tbaa !5
  br label %56

33:                                               ; preds = %25
  %34 = load ptr, ptr @thread_attributes, align 8, !tbaa !9
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = load i32, ptr %1, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds ptr, ptr %35, i64 %37
  %39 = load ptr, ptr %38, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %33, %21
  %.0 = phi ptr [ %22, %21 ], [ %39, %33 ]
  %41 = load ptr, ptr @threads, align 8, !tbaa !9
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  %43 = load i32, ptr %0, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds ptr, ptr %42, i64 %44
  %46 = load ptr, ptr %45, align 8, !tbaa !9
  %47 = load ptr, ptr %2, align 8, !tbaa !9
  %48 = tail call i32 @pthread_create(ptr noundef %46, ptr noundef %.0, ptr noundef %47, ptr noundef %3) #5
  store i32 %48, ptr %4, align 4, !tbaa !5
  %49 = load i32, ptr %1, align 4, !tbaa !5
  %50 = icmp eq i32 %49, -1
  br i1 %50, label %51, label %52

51:                                               ; preds = %40
  tail call void @free(ptr noundef %.0) #5
  br label %52

52:                                               ; preds = %51, %40
  %53 = load ptr, ptr @threads, align 8, !tbaa !9
  %54 = getelementptr inbounds i8, ptr %53, i64 16
  %55 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %54) #5
  br label %56

56:                                               ; preds = %52, %29, %14, %8
  ret void
}

declare i32 @is_valid(ptr noundef, i32 noundef) local_unnamed_addr #3

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

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

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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #4

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 = { 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 #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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 #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 = !{!"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, !10, i64 0}
!12 = !{!"array_tag", !10, i64 0, !6, i64 8, !6, i64 12, !7, i64 16}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass