; 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
@mutexes = external dso_local global ptr, align 8
@thread_attributes = external dso_local global ptr, align 8
@mutex_attributes = external dso_local global ptr, align 8
@thread_keys = external dso_local global ptr, align 8
@once_ctrls = external dso_local global ptr, align 8

; 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
declare dso_local void @thread_cancel(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @thread_destroy(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 1, ptr %3, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %14, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = load ptr, ptr @threads, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.array_tag, ptr %7, i32 0, i32 2
  %9 = load i32, ptr %8, align 4, !tbaa !11
  %10 = icmp slt i32 %6, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %17

12:                                               ; preds = %5
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  call void @thread_cancel(ptr noundef %3, ptr noundef %13)
  br label %14

14:                                               ; preds = %12
  %15 = load i32, ptr %3, align 4, !tbaa !9
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !13

17:                                               ; preds = %11
  %18 = load ptr, ptr @threads, align 8, !tbaa !5
  call void @array_delete(ptr noundef %18)
  %19 = load ptr, ptr @thread_attributes, align 8, !tbaa !5
  call void @array_delete(ptr noundef %19)
  %20 = load ptr, ptr @thread_keys, align 8, !tbaa !5
  call void @array_delete(ptr noundef %20)
  %21 = load ptr, ptr @once_ctrls, align 8, !tbaa !5
  call void @array_delete(ptr noundef %21)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %31, %17
  %23 = load i32, ptr %4, align 4, !tbaa !9
  %24 = load ptr, ptr @mutexes, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.array_tag, ptr %24, i32 0, i32 2
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = icmp slt i32 %23, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %34

29:                                               ; preds = %22
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  call void @thread_mutex_destroy(ptr noundef %4, ptr noundef %30)
  br label %31

31:                                               ; preds = %29
  %32 = load i32, ptr %4, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %4, align 4, !tbaa !9
  br label %22, !llvm.loop !15

34:                                               ; preds = %28
  %35 = load ptr, ptr @mutexes, align 8, !tbaa !5
  call void @array_delete(ptr noundef %35)
  %36 = load ptr, ptr @mutex_attributes, align 8, !tbaa !5
  call void @array_delete(ptr noundef %36)
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 0, ptr %37, align 4, !tbaa !9
  ret void
}

declare void @array_delete(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @thread_mutex_destroy(ptr noundef, ptr noundef) #1

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 = { "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 = !{!"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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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
@mutexes = external dso_local local_unnamed_addr global ptr, align 8
@thread_attributes = external dso_local local_unnamed_addr global ptr, align 8
@mutex_attributes = external dso_local local_unnamed_addr global ptr, align 8
@thread_keys = external dso_local local_unnamed_addr global ptr, align 8
@once_ctrls = external dso_local local_unnamed_addr global ptr, align 8

; 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
declare dso_local void @thread_cancel(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @thread_destroy(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  store i32 1, ptr %2, align 4, !tbaa !5
  %4 = load ptr, ptr @threads, align 8, !tbaa !9
  %5 = getelementptr inbounds i8, ptr %4, i64 12
  %6 = load i32, ptr %5, align 4, !tbaa !11
  %7 = icmp sgt i32 %6, 1
  br i1 %7, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %1
  %.lcssa4 = phi ptr [ %4, %1 ], [ %17, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  call void @array_delete(ptr noundef nonnull %.lcssa4) #3
  %8 = load ptr, ptr @thread_attributes, align 8, !tbaa !9
  call void @array_delete(ptr noundef %8) #3
  %9 = load ptr, ptr @thread_keys, align 8, !tbaa !9
  call void @array_delete(ptr noundef %9) #3
  %10 = load ptr, ptr @once_ctrls, align 8, !tbaa !9
  call void @array_delete(ptr noundef %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  %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 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph6, label %._crit_edge7

.lr.ph:                                           ; preds = %1, %.lr.ph
  call void @thread_cancel(ptr noundef nonnull %2, ptr noundef %0)
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %2, align 4, !tbaa !5
  %17 = load ptr, ptr @threads, align 8, !tbaa !9
  %18 = getelementptr inbounds i8, ptr %17, i64 12
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = icmp slt i32 %16, %19
  br i1 %20, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge7:                                     ; preds = %.lr.ph6, %._crit_edge
  %.lcssa = phi ptr [ %11, %._crit_edge ], [ %24, %.lr.ph6 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  call void @array_delete(ptr noundef nonnull %.lcssa) #3
  %21 = load ptr, ptr @mutex_attributes, align 8, !tbaa !9
  call void @array_delete(ptr noundef %21) #3
  store i32 0, ptr %0, align 4, !tbaa !5
  ret void

.lr.ph6:                                          ; preds = %._crit_edge, %.lr.ph6
  call void @thread_mutex_destroy(ptr noundef nonnull %3, ptr noundef %0)
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %3, align 4, !tbaa !5
  %24 = load ptr, ptr @mutexes, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %24, i64 12
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = icmp slt i32 %23, %26
  br i1 %27, label %.lr.ph6, label %._crit_edge7, !llvm.loop !15
}

declare void @array_delete(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @thread_mutex_destroy(ptr noundef, ptr noundef) local_unnamed_addr #1

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 = { "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 = !{!"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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass