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

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

@morn_thread_order = external hidden thread_local global i32, align 4
@mThreadOrder.Sgn_valid = external hidden global i32, align 4
@morn_thread_signal = external hidden global %struct.MThreadSignal, align 8
@morn_thread_count = external hidden global i32, align 4

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mThreadOrder() #1 {
  %1 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @morn_thread_order)
  %2 = load i32, ptr %1, align 4, !tbaa !5
  %3 = icmp eq i32 %2, -1
  br i1 %3, label %4, label %25

4:                                                ; preds = %0
  br label %5

5:                                                ; preds = %4
  br label %6

6:                                                ; preds = %5
  %7 = load i32, ptr @mThreadOrder.Sgn_valid, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %16

9:                                                ; preds = %6
  %10 = atomicrmw xchg ptr getelementptr inbounds (%struct.MThreadSignal, ptr @morn_thread_signal, i32 0, i32 3), i32 -1135873169 seq_cst, align 8
  %11 = icmp ne i32 %10, -1135873169
  br i1 %11, label %12, label %15

12:                                               ; preds = %9
  %13 = call i32 @pthread_mutex_init(ptr noundef @morn_thread_signal, ptr noundef null) #3
  %14 = call i32 @pthread_rwlock_init(ptr noundef getelementptr inbounds (%struct.MThreadSignal, ptr @morn_thread_signal, i32 0, i32 2), ptr noundef null) #3
  br label %15

15:                                               ; preds = %12, %9
  store i32 1, ptr @mThreadOrder.Sgn_valid, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %15, %6
  br label %17

17:                                               ; preds = %16
  %18 = call i32 @pthread_mutex_lock(ptr noundef @morn_thread_signal) #3
  br label %19

19:                                               ; preds = %17
  %20 = load i32, ptr @morn_thread_count, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr @morn_thread_count, align 4, !tbaa !5
  %22 = load i32, ptr @morn_thread_count, align 4, !tbaa !5
  %23 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @morn_thread_order)
  store i32 %22, ptr %23, align 4, !tbaa !5
  %24 = call i32 @pthread_mutex_unlock(ptr noundef @morn_thread_signal) #3
  br label %25

25:                                               ; preds = %19, %0
  %26 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @morn_thread_order)
  %27 = load i32, ptr %26, align 4, !tbaa !5
  ret i32 %27
}

; 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_mutex_unlock(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { 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"}


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

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

@morn_thread_order = external hidden thread_local local_unnamed_addr global i32, align 4
@mThreadOrder.Sgn_valid = external hidden local_unnamed_addr global i32, align 4
@morn_thread_signal = external hidden global %struct.MThreadSignal, align 8
@morn_thread_count = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mThreadOrder() local_unnamed_addr #1 {
  %1 = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @morn_thread_order)
  %2 = load i32, ptr %1, align 4, !tbaa !5
  %3 = icmp eq i32 %2, -1
  br i1 %3, label %4, label %18

4:                                                ; preds = %0
  %5 = load i32, ptr @mThreadOrder.Sgn_valid, align 4, !tbaa !5
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %7, label %13

7:                                                ; preds = %4
  %8 = atomicrmw xchg ptr getelementptr inbounds (i8, ptr @morn_thread_signal, i64 144), i32 -1135873169 seq_cst, align 8
  %.not = icmp eq i32 %8, -1135873169
  br i1 %.not, label %12, label %9

9:                                                ; preds = %7
  %10 = tail call i32 @pthread_mutex_init(ptr noundef nonnull @morn_thread_signal, ptr noundef null) #3
  %11 = tail call i32 @pthread_rwlock_init(ptr noundef nonnull getelementptr inbounds (i8, ptr @morn_thread_signal, i64 88), ptr noundef null) #3
  br label %12

12:                                               ; preds = %9, %7
  store i32 1, ptr @mThreadOrder.Sgn_valid, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %4, %12
  %14 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @morn_thread_signal) #3
  %15 = load i32, ptr @morn_thread_count, align 4, !tbaa !5
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr @morn_thread_count, align 4, !tbaa !5
  store i32 %16, ptr %1, align 4, !tbaa !5
  %17 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @morn_thread_signal) #3
  %.pre = load i32, ptr %1, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %13, %0
  %19 = phi i32 [ %.pre, %13 ], [ %2, %0 ]
  ret i32 %19
}

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

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

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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { 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"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SimplifyCFGPass,TailCallElimPass