; LLVM IR from mlcommons/tiny :: benchmark/interface/Middlewares/ST/threadx/common/src (txe_semaphore_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_SEMAPHORE_STRUCT = type { i64, ptr, i64, ptr, i32, ptr, ptr, ptr }

@_tx_thread_preempt_disable = external global i32, align 4
@_tx_semaphore_created_ptr = external global ptr, align 8
@_tx_semaphore_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_semaphore_create(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %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 i64 %2, ptr %7, align 8, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !11
  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 !11
  %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 12, ptr %10, align 4, !tbaa !11
  br label %62

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

21:                                               ; preds = %17
  store i32 12, ptr %10, align 4, !tbaa !11
  br label %61

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

28:                                               ; preds = %42, %22
  %29 = load i64, ptr %11, align 8, !tbaa !9
  %30 = load i64, ptr @_tx_semaphore_created_count, align 8, !tbaa !9
  %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_SEMAPHORE_STRUCT, ptr %38, i32 0, i32 5
  %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 !9
  %44 = add i64 %43, 1
  store i64 %44, ptr %11, align 8, !tbaa !9
  br label %28, !llvm.loop !15

45:                                               ; preds = %36, %28
  %46 = call i32 @__disable_interrupts()
  store i32 %46, ptr %9, align 4, !tbaa !11
  %47 = load volatile i32, ptr @_tx_thread_preempt_disable, align 4, !tbaa !11
  %48 = add i32 %47, -1
  store volatile i32 %48, ptr @_tx_thread_preempt_disable, align 4, !tbaa !11
  %49 = load i32, ptr %9, align 4, !tbaa !11
  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 12, ptr %10, align 4, !tbaa !11
  br label %60

54:                                               ; preds = %45
  %55 = load ptr, ptr @_tx_thread_current_ptr, align 8, !tbaa !5
  store ptr %55, ptr %13, align 8, !tbaa !5
  %56 = load ptr, ptr %13, align 8, !tbaa !5
  %57 = icmp eq ptr %56, @_tx_timer_thread
  br i1 %57, label %58, label %59

58:                                               ; preds = %54
  store i32 19, ptr %10, align 4, !tbaa !11
  br label %59

59:                                               ; preds = %58, %54
  br label %60

60:                                               ; preds = %59, %53
  br label %61

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

62:                                               ; preds = %61, %16
  %63 = load i32, ptr %10, align 4, !tbaa !11
  %64 = icmp eq i32 %63, 0
  br i1 %64, label %65, label %80

65:                                               ; preds = %62
  %66 = load volatile i64, ptr @_tx_thread_system_state, align 8, !tbaa !9
  %67 = call i32 @_tx_ipsr_get()
  %68 = zext i32 %67 to i64
  %69 = or i64 %66, %68
  %70 = icmp ne i64 %69, 0
  br i1 %70, label %71, label %79

71:                                               ; preds = %65
  %72 = load volatile i64, ptr @_tx_thread_system_state, align 8, !tbaa !9
  %73 = call i32 @_tx_ipsr_get()
  %74 = zext i32 %73 to i64
  %75 = or i64 %72, %74
  %76 = icmp ult i64 %75, 4042322160
  br i1 %76, label %77, label %78

77:                                               ; preds = %71
  store i32 19, ptr %10, align 4, !tbaa !11
  br label %78

78:                                               ; preds = %77, %71
  br label %79

79:                                               ; preds = %78, %65
  br label %80

80:                                               ; preds = %79, %62
  %81 = load i32, ptr %10, align 4, !tbaa !11
  %82 = icmp eq i32 %81, 0
  br i1 %82, label %83, label %88

83:                                               ; preds = %80
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = load i64, ptr %7, align 8, !tbaa !9
  %87 = call i32 @_tx_semaphore_create(ptr noundef %84, ptr noundef %85, i64 noundef %86)
  store i32 %87, ptr %10, align 4, !tbaa !11
  br label %88

88:                                               ; preds = %83, %80
  %89 = load i32, ptr %10, align 4, !tbaa !11
  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 %89
}

; 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_semaphore_create(ptr noundef, ptr noundef, i64 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 40}
!14 = !{!"TX_SEMAPHORE_STRUCT", !10, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !12, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!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_semaphore_created_ptr = external local_unnamed_addr global ptr, align 8
@_tx_semaphore_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_semaphore_create(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp ne ptr %0, null
  %.not = icmp eq i32 %3, 64
  %or.cond23 = and i1 %5, %.not
  br i1 %or.cond23, label %6, label %.thread29

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_semaphore_created_count, align 8, !tbaa !9
  %.032 = load ptr, ptr @_tx_semaphore_created_ptr, align 8, !tbaa !11
  %11 = icmp eq i64 %10, 0
  %12 = icmp eq ptr %.032, %0
  %or.cond33 = select i1 %11, i1 true, i1 %12
  br i1 %or.cond33, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %.lr.ph
  %.035 = phi ptr [ %.0, %.lr.ph ], [ %.032, %6 ]
  %.01734 = phi i64 [ %14, %.lr.ph ], [ 0, %6 ]
  %13 = getelementptr inbounds i8, ptr %.035, i64 40
  %14 = add nuw i64 %.01734, 1
  %.0 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = icmp uge i64 %14, %10
  %16 = icmp eq ptr %.0, %0
  %or.cond = select i1 %15, i1 true, i1 %16
  br i1 %or.cond, 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 %.thread29, label %20

20:                                               ; preds = %._crit_edge
  %21 = load ptr, ptr @_tx_thread_current_ptr, align 8, !tbaa !11
  %.not31 = icmp eq ptr %21, @_tx_timer_thread
  br i1 %.not31, label %.thread29, label %22

22:                                               ; preds = %20
  %23 = load volatile i64, ptr @_tx_thread_system_state, align 8, !tbaa !9
  %24 = tail call i32 @_tx_ipsr_get()
  %25 = zext i32 %24 to i64
  %26 = or i64 %23, %25
  %.not20 = icmp eq i64 %26, 0
  br i1 %.not20, label %.thread26, label %27

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

.thread26:                                        ; preds = %27, %22
  %33 = tail call i32 @_tx_semaphore_create(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %2) #3
  br label %.thread29

.thread29:                                        ; preds = %20, %27, %._crit_edge, %4, %.thread26
  %.2 = phi i32 [ %33, %.thread26 ], [ 12, %4 ], [ 12, %._crit_edge ], [ 19, %27 ], [ 19, %20 ]
  ret i32 %.2
}

; 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_semaphore_create(ptr noundef, ptr noundef, i64 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