; LLVM IR from eclipse-threadx/threadx :: common/src/tx_timer_system_activate.ll (tx_timer_system_activate)

; 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_TIMER_INTERNAL_STRUCT = type { i64, i64, ptr, i64, ptr, ptr, ptr }

@_tx_timer_current_ptr = external global ptr, align 8
@_tx_timer_list_end = external global ptr, align 8
@_tx_timer_list_start = external global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @_tx_timer_system_activate(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %9, i32 0, i32 0
  %11 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %11, ptr %7, align 8, !tbaa !12
  %12 = load i64, ptr %7, align 8, !tbaa !12
  %13 = icmp ne i64 %12, 0
  br i1 %13, label %14, label %83

14:                                               ; preds = %1
  %15 = load i64, ptr %7, align 8, !tbaa !12
  %16 = icmp ne i64 %15, 4294967295
  br i1 %16, label %17, label %82

17:                                               ; preds = %14
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %18, i32 0, i32 6
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %81

22:                                               ; preds = %17
  %23 = load i64, ptr %7, align 8, !tbaa !12
  %24 = icmp ugt i64 %23, 32
  br i1 %24, label %25, label %26

25:                                               ; preds = %22
  store i64 31, ptr %8, align 8, !tbaa !12
  br label %29

26:                                               ; preds = %22
  %27 = load i64, ptr %7, align 8, !tbaa !12
  %28 = sub i64 %27, 1
  store i64 %28, ptr %8, align 8, !tbaa !12
  br label %29

29:                                               ; preds = %26, %25
  %30 = load ptr, ptr @_tx_timer_current_ptr, align 8, !tbaa !5
  %31 = load i64, ptr %8, align 8, !tbaa !12
  %32 = getelementptr inbounds ptr, ptr %30, i64 %31
  store ptr %32, ptr %3, align 8, !tbaa !5
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = load ptr, ptr @_tx_timer_list_end, align 8, !tbaa !5
  %35 = icmp uge ptr %33, %34
  br i1 %35, label %36, label %46

36:                                               ; preds = %29
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = load ptr, ptr @_tx_timer_list_end, align 8, !tbaa !5
  %39 = ptrtoint ptr %37 to i64
  %40 = ptrtoint ptr %38 to i64
  %41 = sub i64 %39, %40
  %42 = sdiv exact i64 %41, 8
  store i64 %42, ptr %6, align 8, !tbaa !12
  %43 = load ptr, ptr @_tx_timer_list_start, align 8, !tbaa !5
  %44 = load i64, ptr %6, align 8, !tbaa !12
  %45 = getelementptr inbounds ptr, ptr %43, i64 %44
  store ptr %45, ptr %3, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %36, %29
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = load ptr, ptr %47, align 8, !tbaa !5
  %49 = icmp eq ptr %48, null
  br i1 %49, label %50, label %59

50:                                               ; preds = %46
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %52, i32 0, i32 4
  store ptr %51, ptr %53, align 8, !tbaa !14
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %55, i32 0, i32 5
  store ptr %54, ptr %56, align 8, !tbaa !15
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %57, ptr %58, align 8, !tbaa !5
  br label %77

59:                                               ; preds = %46
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  store ptr %61, ptr %4, align 8, !tbaa !5
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %62, i32 0, i32 5
  %64 = load ptr, ptr %63, align 8, !tbaa !15
  store ptr %64, ptr %5, align 8, !tbaa !5
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %66, i32 0, i32 4
  store ptr %65, ptr %67, align 8, !tbaa !14
  %68 = load ptr, ptr %2, align 8, !tbaa !5
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %69, i32 0, i32 5
  store ptr %68, ptr %70, align 8, !tbaa !15
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = load ptr, ptr %2, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %72, i32 0, i32 4
  store ptr %71, ptr %73, align 8, !tbaa !14
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = load ptr, ptr %2, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %75, i32 0, i32 5
  store ptr %74, ptr %76, align 8, !tbaa !15
  br label %77

77:                                               ; preds = %59, %50
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = load ptr, ptr %2, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.TX_TIMER_INTERNAL_STRUCT, ptr %79, i32 0, i32 6
  store ptr %78, ptr %80, align 8, !tbaa !13
  br label %81

81:                                               ; preds = %77, %17
  br label %82

82:                                               ; preds = %81, %14
  br label %83

83:                                               ; preds = %82, %1
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret void
}

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

; 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 = { 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, !11, i64 0}
!10 = !{!"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}
!11 = !{!"long", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !6, i64 48}
!14 = !{!10, !6, i64 32}
!15 = !{!10, !6, 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_timer_current_ptr = external local_unnamed_addr global ptr, align 8
@_tx_timer_list_end = external local_unnamed_addr global ptr, align 8
@_tx_timer_list_start = external local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_tx_timer_system_activate(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr %0, align 8, !tbaa !5
  switch i64 %2, label %3 [
    i64 4294967295, label %30
    i64 0, label %30
  ]

3:                                                ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 48
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %30

7:                                                ; preds = %3
  %8 = icmp ugt i64 %2, 32
  %9 = add nsw i64 %2, -1
  %.0 = select i1 %8, i64 31, i64 %9
  %10 = load ptr, ptr @_tx_timer_current_ptr, align 8, !tbaa !12
  %11 = getelementptr inbounds ptr, ptr %10, i64 %.0
  %12 = load ptr, ptr @_tx_timer_list_end, align 8, !tbaa !12
  %.not = icmp ult ptr %11, %12
  %13 = ptrtoint ptr %11 to i64
  %14 = ptrtoint ptr %12 to i64
  %15 = sub i64 %13, %14
  %16 = load ptr, ptr @_tx_timer_list_start, align 8
  %17 = getelementptr inbounds i8, ptr %16, i64 %15
  %.029 = select i1 %.not, ptr %11, ptr %17
  %18 = load ptr, ptr %.029, align 8, !tbaa !12
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %23

20:                                               ; preds = %7
  %21 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr %0, ptr %21, align 8, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %0, i64 40
  store ptr %0, ptr %22, align 8, !tbaa !14
  store ptr %0, ptr %.029, align 8, !tbaa !12
  br label %29

23:                                               ; preds = %7
  %24 = getelementptr inbounds i8, ptr %18, i64 40
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  %26 = getelementptr inbounds i8, ptr %25, i64 32
  store ptr %0, ptr %26, align 8, !tbaa !13
  store ptr %0, ptr %24, align 8, !tbaa !14
  %27 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr %18, ptr %27, align 8, !tbaa !13
  %28 = getelementptr inbounds i8, ptr %0, i64 40
  store ptr %25, ptr %28, align 8, !tbaa !14
  br label %29

29:                                               ; preds = %23, %20
  store ptr %.029, ptr %4, align 8, !tbaa !11
  br label %30

30:                                               ; preds = %1, %1, %29, %3
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) 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" }

!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, !7, i64 0}
!6 = !{!"TX_TIMER_INTERNAL_STRUCT", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 48}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !10, i64 32}
!14 = !{!6, !10, i64 40}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass