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

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

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

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

10:                                               ; preds = %1
  %11 = load i32, ptr %3, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %11)
  store i32 19, ptr %4, align 4, !tbaa !9
  br label %57

12:                                               ; preds = %1
  %13 = load volatile i64, ptr @_tx_thread_system_state, align 8, !tbaa !5
  %14 = call i32 @_tx_ipsr_get()
  %15 = zext i32 %14 to i64
  %16 = or i64 %13, %15
  %17 = icmp ne i64 %16, 0
  br i1 %17, label %18, label %20

18:                                               ; preds = %12
  %19 = load i32, ptr %3, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %19)
  store i32 19, ptr %4, align 4, !tbaa !9
  br label %56

20:                                               ; preds = %12
  %21 = load ptr, ptr %5, align 8, !tbaa !11
  %22 = icmp eq ptr %21, @_tx_timer_thread
  br i1 %22, label %23, label %25

23:                                               ; preds = %20
  %24 = load i32, ptr %3, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %24)
  store i32 19, ptr %4, align 4, !tbaa !9
  br label %55

25:                                               ; preds = %20
  %26 = load i64, ptr %2, align 8, !tbaa !5
  %27 = icmp eq i64 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %25
  %29 = load i32, ptr %3, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %29)
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %54

30:                                               ; preds = %25
  %31 = load volatile i32, ptr @_tx_thread_preempt_disable, align 4, !tbaa !9
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %35

33:                                               ; preds = %30
  %34 = load i32, ptr %3, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %34)
  store i32 19, ptr %4, align 4, !tbaa !9
  br label %53

35:                                               ; preds = %30
  %36 = load ptr, ptr %5, align 8, !tbaa !11
  %37 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %36, i32 0, i32 12
  store i32 4, ptr %37, align 4, !tbaa !13
  %38 = load ptr, ptr %5, align 8, !tbaa !11
  %39 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %38, i32 0, i32 14
  store i32 1, ptr %39, align 4, !tbaa !16
  %40 = load ptr, ptr %5, align 8, !tbaa !11
  %41 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %40, i32 0, i32 27
  store i32 0, ptr %41, align 4, !tbaa !17
  %42 = load i64, ptr %2, align 8, !tbaa !5
  %43 = load ptr, ptr %5, align 8, !tbaa !11
  %44 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %43, i32 0, i32 19
  %45 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %44, i32 0, i32 0
  store i64 %42, ptr %45, align 8, !tbaa !18
  %46 = load volatile i32, ptr @_tx_thread_preempt_disable, align 4, !tbaa !9
  %47 = add i32 %46, 1
  store volatile i32 %47, ptr @_tx_thread_preempt_disable, align 4, !tbaa !9
  %48 = load i32, ptr %3, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %48)
  %49 = load ptr, ptr %5, align 8, !tbaa !11
  call void @_tx_thread_system_suspend(ptr noundef %49)
  %50 = load ptr, ptr %5, align 8, !tbaa !11
  %51 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %50, i32 0, i32 27
  %52 = load i32, ptr %51, align 4, !tbaa !17
  store i32 %52, ptr %4, align 4, !tbaa !9
  br label %53

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

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

55:                                               ; preds = %54, %23
  br label %56

56:                                               ; preds = %55, %18
  br label %57

57:                                               ; preds = %56, %10
  %58 = load i32, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret i32 %58
}

; 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

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

declare void @_tx_thread_system_suspend(ptr 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 = !{!"long", !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 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !10, i64 92}
!14 = !{!"TX_THREAD_STRUCT", !6, i64 0, !6, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !12, i64 112, !12, i64 120, !6, i64 128, !15, i64 136, !12, i64 192, !12, i64 200, !12, i64 208, !12, i64 216, !6, i64 224, !12, i64 232, !10, i64 240, !10, i64 244, !12, i64 248, !12, i64 256, !12, i64 264, !12, i64 272, !10, i64 280, !10, i64 284, !10, i64 288, !10, i64 292, !12, i64 296, !12, i64 304, !12, i64 312, !6, i64 320}
!15 = !{!"TX_TIMER_INTERNAL_STRUCT", !6, i64 0, !6, i64 8, !12, i64 16, !6, i64 24, !12, i64 32, !12, i64 40, !12, i64 48}
!16 = !{!14, !10, i64 100}
!17 = !{!14, !10, i64 244}
!18 = !{!14, !6, i64 136}


; 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_current_ptr = external local_unnamed_addr global ptr, align 8
@_tx_thread_system_state = external global i64, align 8
@_tx_timer_thread = external global %struct.TX_THREAD_STRUCT, align 8
@_tx_thread_preempt_disable = external global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @_tx_thread_sleep(i64 noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @__disable_interrupts()
  %3 = load ptr, ptr @_tx_thread_current_ptr, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %6

5:                                                ; preds = %1
  tail call void @__restore_interrupt(i32 noundef %2)
  br label %29

6:                                                ; preds = %1
  %7 = load volatile i64, ptr @_tx_thread_system_state, align 8, !tbaa !9
  %8 = tail call i32 @_tx_ipsr_get()
  %9 = zext i32 %8 to i64
  %10 = or i64 %7, %9
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %12, label %11

11:                                               ; preds = %6
  tail call void @__restore_interrupt(i32 noundef %2)
  br label %29

12:                                               ; preds = %6
  %13 = icmp eq ptr %3, @_tx_timer_thread
  br i1 %13, label %14, label %15

14:                                               ; preds = %12
  tail call void @__restore_interrupt(i32 noundef %2)
  br label %29

15:                                               ; preds = %12
  %16 = icmp eq i64 %0, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %15
  tail call void @__restore_interrupt(i32 noundef %2)
  br label %29

18:                                               ; preds = %15
  %19 = load volatile i32, ptr @_tx_thread_preempt_disable, align 4, !tbaa !11
  %.not18 = icmp eq i32 %19, 0
  br i1 %.not18, label %21, label %20

20:                                               ; preds = %18
  tail call void @__restore_interrupt(i32 noundef %2)
  br label %29

21:                                               ; preds = %18
  %22 = getelementptr inbounds i8, ptr %3, i64 92
  store i32 4, ptr %22, align 4, !tbaa !13
  %23 = getelementptr inbounds i8, ptr %3, i64 100
  store i32 1, ptr %23, align 4, !tbaa !16
  %24 = getelementptr inbounds i8, ptr %3, i64 244
  store i32 0, ptr %24, align 4, !tbaa !17
  %25 = getelementptr inbounds i8, ptr %3, i64 136
  store i64 %0, ptr %25, align 8, !tbaa !18
  %26 = load volatile i32, ptr @_tx_thread_preempt_disable, align 4, !tbaa !11
  %27 = add i32 %26, 1
  store volatile i32 %27, ptr @_tx_thread_preempt_disable, align 4, !tbaa !11
  tail call void @__restore_interrupt(i32 noundef %2)
  tail call void @_tx_thread_system_suspend(ptr noundef nonnull %3) #3
  %28 = load i32, ptr %24, align 4, !tbaa !17
  br label %29

29:                                               ; preds = %11, %17, %21, %20, %14, %5
  %.0 = phi i32 [ 19, %5 ], [ 19, %11 ], [ 19, %14 ], [ 0, %17 ], [ 19, %20 ], [ %28, %21 ]
  ret i32 %.0
}

; 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

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

declare void @_tx_thread_system_suspend(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 = { 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 = !{!"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, !12, i64 92}
!14 = !{!"TX_THREAD_STRUCT", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !12, i64 88, !12, i64 92, !12, i64 96, !12, i64 100, !12, i64 104, !6, i64 112, !6, i64 120, !10, i64 128, !15, i64 136, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !10, i64 224, !6, i64 232, !12, i64 240, !12, i64 244, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !12, i64 280, !12, i64 284, !12, i64 288, !12, i64 292, !6, i64 296, !6, i64 304, !6, i64 312, !10, i64 320}
!15 = !{!"TX_TIMER_INTERNAL_STRUCT", !10, i64 0, !10, i64 8, !6, i64 16, !10, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!16 = !{!14, !12, i64 100}
!17 = !{!14, !12, i64 244}
!18 = !{!14, !10, i64 136}


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