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

; 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

; Function Attrs: nounwind uwtable
define dso_local i32 @_tx_thread_reset(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 0, ptr %5, align 4, !tbaa !9
  %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 !5
  store ptr %7, ptr %4, align 8, !tbaa !5
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = icmp eq ptr %8, %9
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store i32 32, ptr %5, align 4, !tbaa !9
  br label %25

12:                                               ; preds = %1
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %13, i32 0, i32 12
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = icmp ne i32 %15, 1
  br i1 %16, label %17, label %24

17:                                               ; preds = %12
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %18, i32 0, i32 12
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = icmp ne i32 %20, 2
  br i1 %21, label %22, label %23

22:                                               ; preds = %17
  store i32 32, ptr %5, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %22, %17
  br label %24

24:                                               ; preds = %23, %12
  br label %25

25:                                               ; preds = %24, %11
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %42

28:                                               ; preds = %25
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %29, i32 0, i32 12
  store i32 32, ptr %30, align 4, !tbaa !11
  %31 = load i32, ptr %3, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %31)
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %32, i32 0, i32 3
  %34 = load ptr, ptr %33, align 8, !tbaa !15
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %35, i32 0, i32 5
  %37 = load i64, ptr %36, align 8, !tbaa !16
  call void @llvm.memset.p0.i64(ptr align 1 %34, i8 -17, i64 %37, i1 false)
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  call void @_tx_thread_stack_build(ptr noundef %38, ptr noundef @_tx_thread_shell_entry)
  %39 = call i32 @__disable_interrupts()
  store i32 %39, ptr %3, align 4, !tbaa !9
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.TX_THREAD_STRUCT, ptr %40, i32 0, i32 12
  store i32 3, ptr %41, align 4, !tbaa !11
  br label %42

42:                                               ; preds = %28, %25
  %43 = load i32, ptr %3, align 4, !tbaa !9
  call void @__restore_interrupt(i32 noundef %43)
  %44 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  ret i32 %44
}

; 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

declare void @_tx_thread_stack_build(ptr noundef, ptr noundef) #4

declare void @_tx_thread_shell_entry() #4

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { "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 #5 = { 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 92}
!12 = !{!"TX_THREAD_STRUCT", !13, i64 0, !13, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !13, i64 40, !13, i64 48, !13, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !6, i64 112, !6, i64 120, !13, i64 128, !14, i64 136, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !13, i64 224, !6, i64 232, !10, i64 240, !10, i64 244, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !10, i64 280, !10, i64 284, !10, i64 288, !10, i64 292, !6, i64 296, !6, i64 304, !6, i64 312, !13, i64 320}
!13 = !{!"long", !7, i64 0}
!14 = !{!"TX_TIMER_INTERNAL_STRUCT", !13, i64 0, !13, i64 8, !6, i64 16, !13, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!15 = !{!12, !6, i64 24}
!16 = !{!12, !13, i64 40}


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

@_tx_thread_current_ptr = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 33) i32 @_tx_thread_reset(ptr 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, %0
  br i1 %4, label %.thread, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 92
  %7 = load i32, ptr %6, align 4, !tbaa !9
  %.off = add i32 %7, -1
  %switch = icmp ult i32 %.off, 2
  br i1 %switch, label %8, label %.thread

8:                                                ; preds = %5
  store i32 32, ptr %6, align 4, !tbaa !9
  tail call void @__restore_interrupt(i32 noundef %2)
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load ptr, ptr %9, align 8, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %0, i64 40
  %12 = load i64, ptr %11, align 8, !tbaa !15
  tail call void @llvm.memset.p0.i64(ptr align 1 %10, i8 -17, i64 %12, i1 false)
  tail call void @_tx_thread_stack_build(ptr noundef nonnull %0, ptr noundef nonnull @_tx_thread_shell_entry) #4
  %13 = tail call i32 @__disable_interrupts()
  store i32 3, ptr %6, align 4, !tbaa !9
  br label %.thread

.thread:                                          ; preds = %1, %8, %5
  %.018 = phi i32 [ 0, %8 ], [ 32, %5 ], [ 32, %1 ]
  %.012 = phi i32 [ %13, %8 ], [ %2, %5 ], [ %2, %1 ]
  tail call void @__restore_interrupt(i32 noundef %.012)
  ret i32 %.018
}

; 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

declare void @_tx_thread_stack_build(ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @_tx_thread_shell_entry() #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 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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, !12, i64 92}
!10 = !{!"TX_THREAD_STRUCT", !11, i64 0, !11, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !11, i64 48, !11, 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, !11, i64 128, !13, i64 136, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !11, 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, !11, i64 320}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"TX_TIMER_INTERNAL_STRUCT", !11, i64 0, !11, i64 8, !6, i64 16, !11, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!14 = !{!10, !6, i64 24}
!15 = !{!10, !11, i64 40}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass