; LLVM IR from jingweizhanghuai/Morn :: src/ptc (morn_thread_pool)

; 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.HandleThreadPool = type { ptr, i32, ptr, i32, i32, i32, i32, i32, %struct.MThreadSignal, ptr }
%struct.MThreadSignal = type { %union.pthread_mutex_t, %union.pthread_cond_t, %union.pthread_rwlock_t, i32 }
%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 }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }
%union.pthread_rwlock_t = type { %struct.__pthread_rwlock_arch_t }
%struct.__pthread_rwlock_arch_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i8, [7 x i8], i64, i32 }
%struct.MList = type { i32, ptr }
%struct.ThreadPoolData = type { i64, %struct.MThreadSignal, ptr, ptr, ptr, ptr, i32 }

@morn_thread_pool_handle = external dso_local global ptr, align 8
@endThreadPool.Sgn_valid = external hidden global i32, align 4

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

5:                                                ; preds = %77, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.HandleThreadPool, ptr %7, i32 0, i32 1
  %9 = load i32, ptr %8, align 8, !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) #4
  br label %80

12:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.HandleThreadPool, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !14
  %16 = getelementptr inbounds %struct.MList, ptr %15, i32 0, i32 1
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  %18 = load i32, ptr %3, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds ptr, ptr %17, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  store ptr %21, ptr %4, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %27, %12
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.ThreadPoolData, ptr %23, i32 0, i32 6
  %25 = load volatile i32, ptr %24, align 8, !tbaa !17
  %26 = icmp eq i32 %25, 1
  br i1 %26, label %27, label %28

27:                                               ; preds = %22
  br label %22, !llvm.loop !20

28:                                               ; preds = %22
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.ThreadPoolData, ptr %29, i32 0, i32 3
  store ptr null, ptr %30, align 8, !tbaa !22
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.ThreadPoolData, ptr %31, i32 0, i32 6
  store volatile i32 1, ptr %32, align 8, !tbaa !17
  br label %33

33:                                               ; preds = %28
  br label %34

34:                                               ; preds = %33
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr @endThreadPool.Sgn_valid, align 4, !tbaa !9
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %54

38:                                               ; preds = %35
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.ThreadPoolData, ptr %39, i32 0, i32 1
  %41 = getelementptr inbounds %struct.MThreadSignal, ptr %40, i32 0, i32 3
  %42 = atomicrmw xchg ptr %41, i32 -1135873169 seq_cst, align 8
  %43 = icmp ne i32 %42, -1135873169
  br i1 %43, label %44, label %53

44:                                               ; preds = %38
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.ThreadPoolData, ptr %45, i32 0, i32 1
  %47 = getelementptr inbounds %struct.MThreadSignal, ptr %46, i32 0, i32 0
  %48 = call i32 @pthread_mutex_init(ptr noundef %47, ptr noundef null) #4
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.ThreadPoolData, ptr %49, i32 0, i32 1
  %51 = getelementptr inbounds %struct.MThreadSignal, ptr %50, i32 0, i32 2
  %52 = call i32 @pthread_rwlock_init(ptr noundef %51, ptr noundef null) #4
  br label %53

53:                                               ; preds = %44, %38
  store i32 1, ptr @endThreadPool.Sgn_valid, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %53, %35
  br label %55

55:                                               ; preds = %54
  br label %56

56:                                               ; preds = %55
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.ThreadPoolData, ptr %57, i32 0, i32 1
  %59 = getelementptr inbounds %struct.MThreadSignal, ptr %58, i32 0, i32 0
  %60 = call i32 @pthread_mutex_lock(ptr noundef %59) #4
  br label %61

61:                                               ; preds = %56
  br label %62

62:                                               ; preds = %61
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.ThreadPoolData, ptr %63, i32 0, i32 1
  %65 = getelementptr inbounds %struct.MThreadSignal, ptr %64, i32 0, i32 1
  %66 = call i32 @pthread_cond_signal(ptr noundef %65) #4
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.ThreadPoolData, ptr %67, i32 0, i32 1
  %69 = getelementptr inbounds %struct.MThreadSignal, ptr %68, i32 0, i32 0
  %70 = call i32 @pthread_mutex_unlock(ptr noundef %69) #4
  br label %71

71:                                               ; preds = %62
  br label %72

72:                                               ; preds = %71
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.ThreadPoolData, ptr %73, i32 0, i32 0
  %75 = load i64, ptr %74, align 8, !tbaa !23
  %76 = call i32 @pthread_join(i64 noundef %75, ptr noundef null)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %77

77:                                               ; preds = %72
  %78 = load i32, ptr %3, align 4, !tbaa !9
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !24

80:                                               ; preds = %11
  %81 = load ptr, ptr %2, align 8, !tbaa !5
  %82 = load ptr, ptr @morn_thread_pool_handle, align 8, !tbaa !5
  %83 = icmp eq ptr %81, %82
  br i1 %83, label %84, label %88

84:                                               ; preds = %80
  %85 = load ptr, ptr %2, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.HandleThreadPool, ptr %85, i32 0, i32 0
  %87 = load ptr, ptr %86, align 8, !tbaa !14
  call void @mListRelease(ptr noundef %87)
  store ptr null, ptr @morn_thread_pool_handle, align 8, !tbaa !5
  br label %88

88:                                               ; preds = %84, %80
  %89 = load ptr, ptr %2, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.HandleThreadPool, ptr %89, i32 0, i32 2
  %91 = load ptr, ptr %90, align 8, !tbaa !25
  %92 = icmp ne ptr %91, null
  br i1 %92, label %93, label %97

93:                                               ; preds = %88
  %94 = load ptr, ptr %2, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.HandleThreadPool, ptr %94, i32 0, i32 2
  %96 = load ptr, ptr %95, align 8, !tbaa !25
  call void @mListRelease(ptr noundef %96)
  br label %97

97:                                               ; preds = %93, %88
  ret void
}

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

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

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

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

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

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

declare i32 @pthread_join(i64 noundef, ptr noundef) #3

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

declare void @mListRelease(ptr noundef) #3

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

!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 8}
!12 = !{!"HandleThreadPool", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !13, i64 48, !6, i64 200}
!13 = !{!"MThreadSignal", !7, i64 0, !7, i64 40, !7, i64 88, !10, i64 144}
!14 = !{!12, !6, i64 0}
!15 = !{!16, !6, i64 8}
!16 = !{!"MList", !10, i64 0, !6, i64 8}
!17 = !{!18, !10, i64 192}
!18 = !{!"ThreadPoolData", !19, i64 0, !13, i64 8, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !10, i64 192}
!19 = !{!"long", !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!18, !6, i64 168}
!23 = !{!18, !19, i64 0}
!24 = distinct !{!24, !21}
!25 = !{!12, !6, 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"

@morn_thread_pool_handle = external dso_local local_unnamed_addr global ptr, align 8
@endThreadPool.Sgn_valid = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @endThreadPool(ptr noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %29, %1
  %5 = load ptr, ptr @morn_thread_pool_handle, align 8, !tbaa !12
  %6 = icmp eq ptr %5, %0
  br i1 %6, label %40, label %42

.lr.ph:                                           ; preds = %1, %29
  %indvars.iv = phi i64 [ %indvars.iv.next, %29 ], [ 0, %1 ]
  %7 = load ptr, ptr %0, align 8, !tbaa !13
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  %10 = getelementptr inbounds ptr, ptr %9, i64 %indvars.iv
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %11, i64 192
  br label %13

13:                                               ; preds = %13, %.lr.ph
  %14 = load volatile i32, ptr %12, align 8, !tbaa !16
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %13, label %16, !llvm.loop !19

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %11, i64 168
  store ptr null, ptr %17, align 8, !tbaa !21
  store volatile i32 1, ptr %12, align 8, !tbaa !16
  %18 = load i32, ptr @endThreadPool.Sgn_valid, align 4, !tbaa !22
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %29

20:                                               ; preds = %16
  %21 = getelementptr inbounds i8, ptr %11, i64 152
  %22 = atomicrmw xchg ptr %21, i32 -1135873169 seq_cst, align 8
  %.not20 = icmp eq i32 %22, -1135873169
  br i1 %.not20, label %28, label %23

23:                                               ; preds = %20
  %24 = getelementptr inbounds i8, ptr %11, i64 8
  %25 = tail call i32 @pthread_mutex_init(ptr noundef nonnull %24, ptr noundef null) #3
  %26 = getelementptr inbounds i8, ptr %11, i64 96
  %27 = tail call i32 @pthread_rwlock_init(ptr noundef nonnull %26, ptr noundef null) #3
  br label %28

28:                                               ; preds = %23, %20
  store i32 1, ptr @endThreadPool.Sgn_valid, align 4, !tbaa !22
  br label %29

29:                                               ; preds = %28, %16
  %30 = getelementptr inbounds i8, ptr %11, i64 8
  %31 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %30) #3
  %32 = getelementptr inbounds i8, ptr %11, i64 48
  %33 = tail call i32 @pthread_cond_signal(ptr noundef nonnull %32) #3
  %34 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %30) #3
  %35 = load i64, ptr %11, align 8, !tbaa !23
  %36 = tail call i32 @pthread_join(i64 noundef %35, ptr noundef null) #3
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %37 = load i32, ptr %2, align 8, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = icmp slt i64 %indvars.iv.next, %38
  br i1 %39, label %.lr.ph, label %._crit_edge, !llvm.loop !24

40:                                               ; preds = %._crit_edge
  %41 = load ptr, ptr %0, align 8, !tbaa !13
  tail call void @mListRelease(ptr noundef %41) #3
  store ptr null, ptr @morn_thread_pool_handle, align 8, !tbaa !12
  br label %42

42:                                               ; preds = %40, %._crit_edge
  %43 = getelementptr inbounds i8, ptr %0, i64 16
  %44 = load ptr, ptr %43, align 8, !tbaa !25
  %.not = icmp eq ptr %44, null
  br i1 %.not, label %46, label %45

45:                                               ; preds = %42
  tail call void @mListRelease(ptr noundef nonnull %44) #3
  br label %46

46:                                               ; preds = %45, %42
  ret void
}

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

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

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

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

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

declare i32 @pthread_join(i64 noundef, ptr noundef) local_unnamed_addr #2

declare void @mListRelease(ptr noundef) local_unnamed_addr #2

attributes #0 = { 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 #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 }

!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, !10, i64 8}
!6 = !{!"HandleThreadPool", !7, i64 0, !10, i64 8, !7, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !11, i64 48, !7, i64 200}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"MThreadSignal", !8, i64 0, !8, i64 40, !8, i64 88, !10, i64 144}
!12 = !{!7, !7, i64 0}
!13 = !{!6, !7, i64 0}
!14 = !{!15, !7, i64 8}
!15 = !{!"MList", !10, i64 0, !7, i64 8}
!16 = !{!17, !10, i64 192}
!17 = !{!"ThreadPoolData", !18, i64 0, !11, i64 8, !7, i64 160, !7, i64 168, !7, i64 176, !7, i64 184, !10, i64 192}
!18 = !{!"long", !8, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!17, !7, i64 168}
!22 = !{!10, !10, i64 0}
!23 = !{!17, !18, i64 0}
!24 = distinct !{!24, !20}
!25 = !{!6, !7, i64 16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass