; LLVM IR from mlcommons/tiny :: benchmark/interface/Middlewares/ST/threadx/common/src (txe_mutex_create)

; 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.TX_THREAD_STRUCT = type { i64, i64, ptr, ptr, ptr, i64, i64, i64, ptr, ptr, ptr, i32, i32, i32, i32, i32, ptr, ptr, i64, %struct.TX_TIMER_INTERNAL_STRUCT, ptr, ptr, ptr, ptr, i64, ptr, i32, i32, ptr, ptr, ptr, ptr, i32, i32, i32, i32, ptr, ptr, ptr, i64 }
%struct.TX_TIMER_INTERNAL_STRUCT = type { i64, i64, ptr, i64, ptr, ptr, ptr }
%struct.TX_MUTEX_STRUCT = type { i64, ptr, i32, ptr, i32, i32, ptr, i32, ptr, ptr, i32, ptr, ptr }

@_tx_thread_preempt_disable = external global i32, align 4
@_tx_mutex_created_ptr = external global ptr, align 8
@_tx_mutex_created_count = external global i64, align 8
@_tx_thread_current_ptr = external global ptr, align 8
@_tx_timer_thread = external global %struct.TX_THREAD_STRUCT, align 8
@_tx_thread_system_state = external global i64, align 8

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

16:                                               ; preds = %4
  store i32 28, ptr %10, align 4, !tbaa !9
  br label %65

17:                                               ; preds = %4
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = zext i32 %18 to i64
  %20 = icmp ne i64 %19, 96
  br i1 %20, label %21, label %22

21:                                               ; preds = %17
  store i32 28, ptr %10, align 4, !tbaa !9
  br label %64

22:                                               ; preds = %17
  %23 = call i32 @__disable_interrupts()
  store i32 %23, ptr %9, align 4, !tbaa !9
  %24 = load volatile i32, ptr @_tx_thread_preempt_disable, align 4, !tbaa !9
  %25 = add i32 %24, 1
  store volatile i32 %25, ptr @_tx_thread_preempt_disable, align 4, !tbaa !9
  %26 = load i32, ptr %9, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %26)
  %27 = load ptr, ptr @_tx_mutex_created_ptr, align 8, !tbaa !5
  store ptr %27, ptr %12, align 8, !tbaa !5
  store i64 0, ptr %11, align 8, !tbaa !11
  br label %28

28:                                               ; preds = %42, %22
  %29 = load i64, ptr %11, align 8, !tbaa !11
  %30 = load i64, ptr @_tx_mutex_created_count, align 8, !tbaa !11
  %31 = icmp ult i64 %29, %30
  br i1 %31, label %32, label %45

32:                                               ; preds = %28
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load ptr, ptr %12, align 8, !tbaa !5
  %35 = icmp eq ptr %33, %34
  br i1 %35, label %36, label %37

36:                                               ; preds = %32
  br label %45

37:                                               ; preds = %32
  %38 = load ptr, ptr %12, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.TX_MUTEX_STRUCT, ptr %38, i32 0, i32 8
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  store ptr %40, ptr %12, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %37
  br label %42

42:                                               ; preds = %41
  %43 = load i64, ptr %11, align 8, !tbaa !11
  %44 = add i64 %43, 1
  store i64 %44, ptr %11, align 8, !tbaa !11
  br label %28, !llvm.loop !15

45:                                               ; preds = %36, %28
  %46 = call i32 @__disable_interrupts()
  store i32 %46, ptr %9, align 4, !tbaa !9
  %47 = load volatile i32, ptr @_tx_thread_preempt_disable, align 4, !tbaa !9
  %48 = add i32 %47, -1
  store volatile i32 %48, ptr @_tx_thread_preempt_disable, align 4, !tbaa !9
  %49 = load i32, ptr %9, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %49)
  call void @_tx_thread_system_preempt_check()
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = load ptr, ptr %12, align 8, !tbaa !5
  %52 = icmp eq ptr %50, %51
  br i1 %52, label %53, label %54

53:                                               ; preds = %45
  store i32 28, ptr %10, align 4, !tbaa !9
  br label %63

54:                                               ; preds = %45
  %55 = load i32, ptr %7, align 4, !tbaa !9
  %56 = icmp ne i32 %55, 1
  br i1 %56, label %57, label %62

57:                                               ; preds = %54
  %58 = load i32, ptr %7, align 4, !tbaa !9
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %61

60:                                               ; preds = %57
  store i32 31, ptr %10, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %60, %57
  br label %62

62:                                               ; preds = %61, %54
  br label %63

63:                                               ; preds = %62, %53
  br label %64

64:                                               ; preds = %63, %21
  br label %65

65:                                               ; preds = %64, %16
  %66 = load i32, ptr %10, align 4, !tbaa !9
  %67 = icmp eq i32 %66, 0
  br i1 %67, label %68, label %88

68:                                               ; preds = %65
  %69 = load ptr, ptr @_tx_thread_current_ptr, align 8, !tbaa !5
  store ptr %69, ptr %13, align 8, !tbaa !5
  %70 = load ptr, ptr %13, align 8, !tbaa !5
  %71 = icmp eq ptr %70, @_tx_timer_thread
  br i1 %71, label %72, label %73

72:                                               ; preds = %68
  store i32 19, ptr %10, align 4, !tbaa !9
  br label %73

73:                                               ; preds = %72, %68
  %74 = load volatile i64, ptr @_tx_thread_system_state, align 8, !tbaa !11
  %75 = call i32 @_tx_ipsr_get()
  %76 = zext i32 %75 to i64
  %77 = or i64 %74, %76
  %78 = icmp ne i64 %77, 0
  br i1 %78, label %79, label %87

79:                                               ; preds = %73
  %80 = load volatile i64, ptr @_tx_thread_system_state, align 8, !tbaa !11
  %81 = call i32 @_tx_ipsr_get()
  %82 = zext i32 %81 to i64
  %83 = or i64 %80, %82
  %84 = icmp ult i64 %83, 4042322160
  br i1 %84, label %85, label %86

85:                                               ; preds = %79
  store i32 19, ptr %10, align 4, !tbaa !9
  br label %86

86:                                               ; preds = %85, %79
  br label %87

87:                                               ; preds = %86, %73
  br label %88

88:                                               ; preds = %87, %65
  %89 = load i32, ptr %10, align 4, !tbaa !9
  %90 = icmp eq i32 %89, 0
  br i1 %90, label %91, label %96

91:                                               ; preds = %88
  %92 = load ptr, ptr %5, align 8, !tbaa !5
  %93 = load ptr, ptr %6, align 8, !tbaa !5
  %94 = load i32, ptr %7, align 4, !tbaa !9
  %95 = call i32 @_tx_mutex_create(ptr noundef %92, ptr noundef %93, i32 noundef %94)
  store i32 %95, ptr %10, align 4, !tbaa !9
  br label %96

96:                                               ; preds = %91, %88
  %97 = load i32, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 %97
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @__disable_interrupts() #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @__restore_interrupt(i32 noundef) #2

declare void @_tx_thread_system_preempt_check() #3

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @_tx_ipsr_get() #2

declare i32 @_tx_mutex_create(ptr noundef, ptr noundef, i32 noundef) #3

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

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 = { alwaysinline 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 = { 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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 56}
!14 = !{!"TX_MUTEX_STRUCT", !12, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !10, i64 32, !10, i64 36, !6, i64 40, !10, i64 48, !6, i64 56, !6, i64 64, !10, i64 72, !6, i64 80, !6, i64 88}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; 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.TX_THREAD_STRUCT = type { i64, i64, ptr, ptr, ptr, i64, i64, i64, ptr, ptr, ptr, i32, i32, i32, i32, i32, ptr, ptr, i64, %struct.TX_TIMER_INTERNAL_STRUCT, ptr, ptr, ptr, ptr, i64, ptr, i32, i32, ptr, ptr, ptr, ptr, i32, i32, i32, i32, ptr, ptr, ptr, i64 }
%struct.TX_TIMER_INTERNAL_STRUCT = type { i64, i64, ptr, i64, ptr, ptr, ptr }

@_tx_thread_preempt_disable = external global i32, align 4
@_tx_mutex_created_ptr = external local_unnamed_addr global ptr, align 8
@_tx_mutex_created_count = external local_unnamed_addr global i64, align 8
@_tx_thread_current_ptr = external local_unnamed_addr global ptr, align 8
@_tx_timer_thread = external global %struct.TX_THREAD_STRUCT, align 8
@_tx_thread_system_state = external global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @_txe_mutex_create(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp ne ptr %0, null
  %.not = icmp eq i32 %3, 96
  %or.cond28 = and i1 %5, %.not
  br i1 %or.cond28, label %6, label %.thread31

6:                                                ; preds = %4
  %7 = tail call i32 @__disable_interrupts()
  %8 = load volatile i32, ptr @_tx_thread_preempt_disable, align 4, !tbaa !5
  %9 = add i32 %8, 1
  store volatile i32 %9, ptr @_tx_thread_preempt_disable, align 4, !tbaa !5
  tail call void @__restore_interrupt(i32 noundef %7)
  %10 = load i64, ptr @_tx_mutex_created_count, align 8, !tbaa !9
  %.033 = load ptr, ptr @_tx_mutex_created_ptr, align 8, !tbaa !11
  %11 = icmp eq i64 %10, 0
  %12 = icmp eq ptr %.033, %0
  %or.cond2434 = select i1 %11, i1 true, i1 %12
  br i1 %or.cond2434, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %.lr.ph
  %.036 = phi ptr [ %.0, %.lr.ph ], [ %.033, %6 ]
  %.02035 = phi i64 [ %14, %.lr.ph ], [ 0, %6 ]
  %13 = getelementptr inbounds i8, ptr %.036, i64 56
  %14 = add nuw i64 %.02035, 1
  %.0 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = icmp uge i64 %14, %10
  %16 = icmp eq ptr %.0, %0
  %or.cond24 = select i1 %15, i1 true, i1 %16
  br i1 %or.cond24, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %6
  %.lcssa = phi i1 [ %12, %6 ], [ %16, %.lr.ph ]
  %17 = tail call i32 @__disable_interrupts()
  %18 = load volatile i32, ptr @_tx_thread_preempt_disable, align 4, !tbaa !5
  %19 = add i32 %18, -1
  store volatile i32 %19, ptr @_tx_thread_preempt_disable, align 4, !tbaa !5
  tail call void @__restore_interrupt(i32 noundef %17)
  tail call void @_tx_thread_system_preempt_check() #3
  br i1 %.lcssa, label %.thread31, label %20

20:                                               ; preds = %._crit_edge
  %or.cond = icmp ult i32 %2, 2
  br i1 %or.cond, label %21, label %34

21:                                               ; preds = %20
  %22 = load ptr, ptr @_tx_thread_current_ptr, align 8, !tbaa !11
  %23 = icmp eq ptr %22, @_tx_timer_thread
  %spec.select26 = select i1 %23, i32 19, i32 0
  %24 = load volatile i64, ptr @_tx_thread_system_state, align 8, !tbaa !9
  %25 = tail call i32 @_tx_ipsr_get()
  %26 = zext i32 %25 to i64
  %27 = or i64 %24, %26
  %.not23 = icmp eq i64 %27, 0
  br i1 %.not23, label %34, label %28

28:                                               ; preds = %21
  %29 = load volatile i64, ptr @_tx_thread_system_state, align 8, !tbaa !9
  %30 = tail call i32 @_tx_ipsr_get()
  %31 = zext i32 %30 to i64
  %32 = or i64 %29, %31
  %33 = icmp ult i64 %32, 4042322160
  br i1 %33, label %.thread31, label %34

34:                                               ; preds = %28, %21, %20
  %.2 = phi i32 [ %spec.select26, %21 ], [ 31, %20 ], [ %spec.select26, %28 ]
  %35 = icmp eq i32 %.2, 0
  br i1 %35, label %36, label %.thread31

36:                                               ; preds = %34
  %37 = tail call i32 @_tx_mutex_create(ptr noundef nonnull %0, ptr noundef %1, i32 noundef %2) #3
  br label %.thread31

.thread31:                                        ; preds = %28, %._crit_edge, %4, %36, %34
  %.3 = phi i32 [ %37, %36 ], [ %.2, %34 ], [ 28, %4 ], [ 28, %._crit_edge ], [ 19, %28 ]
  ret i32 %.3
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @__disable_interrupts() local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @__restore_interrupt(i32 noundef) local_unnamed_addr #1

declare void @_tx_thread_system_preempt_check() local_unnamed_addr #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @_tx_ipsr_get() local_unnamed_addr #1

declare i32 @_tx_mutex_create(ptr noundef, ptr noundef, i32 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 = { alwaysinline 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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