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

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

@_tx_thread_system_state = 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

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

13:                                               ; preds = %4
  store i32 2, ptr %9, align 4, !tbaa !11
  br label %48

14:                                               ; preds = %4
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.TX_BYTE_POOL_STRUCT, ptr %15, i32 0, i32 0
  %17 = load i64, ptr %16, align 8, !tbaa !13
  %18 = icmp ne i64 %17, 1113150533
  br i1 %18, label %19, label %20

19:                                               ; preds = %14
  store i32 2, ptr %9, align 4, !tbaa !11
  br label %47

20:                                               ; preds = %14
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %24

23:                                               ; preds = %20
  store i32 3, ptr %9, align 4, !tbaa !11
  br label %46

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

27:                                               ; preds = %24
  store i32 5, ptr %9, align 4, !tbaa !11
  br label %45

28:                                               ; preds = %24
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.TX_BYTE_POOL_STRUCT, ptr %30, i32 0, i32 7
  %32 = load i64, ptr %31, align 8, !tbaa !15
  %33 = icmp ugt i64 %29, %32
  br i1 %33, label %34, label %35

34:                                               ; preds = %28
  store i32 5, ptr %9, align 4, !tbaa !11
  br label %44

35:                                               ; preds = %28
  %36 = load i64, ptr %8, align 8, !tbaa !9
  %37 = icmp ne i64 %36, 0
  br i1 %37, label %38, label %43

38:                                               ; preds = %35
  %39 = load i64, ptr @_tx_thread_system_state, align 8, !tbaa !9
  %40 = icmp ne i64 %39, 0
  br i1 %40, label %41, label %42

41:                                               ; preds = %38
  store i32 4, ptr %9, align 4, !tbaa !11
  br label %42

42:                                               ; preds = %41, %38
  br label %43

43:                                               ; preds = %42, %35
  br label %44

44:                                               ; preds = %43, %34
  br label %45

45:                                               ; preds = %44, %27
  br label %46

46:                                               ; preds = %45, %23
  br label %47

47:                                               ; preds = %46, %19
  br label %48

48:                                               ; preds = %47, %13
  %49 = load i32, ptr %9, align 4, !tbaa !11
  %50 = icmp eq i32 %49, 0
  br i1 %50, label %51, label %57

51:                                               ; preds = %48
  %52 = load ptr, ptr @_tx_thread_current_ptr, align 8, !tbaa !5
  store ptr %52, ptr %10, align 8, !tbaa !5
  %53 = load ptr, ptr %10, align 8, !tbaa !5
  %54 = icmp eq ptr %53, @_tx_timer_thread
  br i1 %54, label %55, label %56

55:                                               ; preds = %51
  store i32 19, ptr %9, align 4, !tbaa !11
  br label %56

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

57:                                               ; preds = %56, %48
  %58 = load i32, ptr %9, align 4, !tbaa !11
  %59 = icmp eq i32 %58, 0
  br i1 %59, label %60, label %69

60:                                               ; preds = %57
  %61 = load i64, ptr @_tx_thread_system_state, align 8, !tbaa !9
  %62 = icmp ne i64 %61, 0
  br i1 %62, label %63, label %68

63:                                               ; preds = %60
  %64 = load i64, ptr @_tx_thread_system_state, align 8, !tbaa !9
  %65 = icmp ult i64 %64, 4042322160
  br i1 %65, label %66, label %67

66:                                               ; preds = %63
  store i32 19, ptr %9, align 4, !tbaa !11
  br label %67

67:                                               ; preds = %66, %63
  br label %68

68:                                               ; preds = %67, %60
  br label %69

69:                                               ; preds = %68, %57
  %70 = load i32, ptr %9, align 4, !tbaa !11
  %71 = icmp eq i32 %70, 0
  br i1 %71, label %72, label %78

72:                                               ; preds = %69
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = load i64, ptr %7, align 8, !tbaa !9
  %76 = load i64, ptr %8, align 8, !tbaa !9
  %77 = call i32 @_tx_byte_allocate(ptr noundef %73, ptr noundef %74, i64 noundef %75, i64 noundef %76)
  store i32 %77, ptr %9, align 4, !tbaa !11
  br label %78

78:                                               ; preds = %72, %69
  %79 = load i32, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret i32 %79
}

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

declare i32 @_tx_byte_allocate(ptr noundef, ptr noundef, i64 noundef, i64 noundef) #2

; 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 = { "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, !10, i64 0}
!14 = !{!"TX_BYTE_POOL_STRUCT", !10, i64 0, !6, i64 8, !10, i64 16, !12, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !10, i64 56, !6, i64 64, !6, i64 72, !12, i64 80, !6, i64 88, !6, i64 96}
!15 = !{!14, !10, i64 56}


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

@_tx_thread_system_state = 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

; Function Attrs: nounwind uwtable
define dso_local i32 @_txe_byte_allocate(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq ptr %0, null
  br i1 %5, label %.thread, label %6

6:                                                ; preds = %4
  %7 = load i64, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i64 %7, 1113150533
  br i1 %.not, label %8, label %.thread

8:                                                ; preds = %6
  %9 = icmp eq ptr %1, null
  br i1 %9, label %.thread, label %10

10:                                               ; preds = %8
  %11 = icmp eq i64 %2, 0
  br i1 %11, label %.thread, label %12

12:                                               ; preds = %10
  %13 = getelementptr inbounds i8, ptr %0, i64 56
  %14 = load i64, ptr %13, align 8, !tbaa !12
  %15 = icmp ult i64 %14, %2
  br i1 %15, label %.thread, label %16

16:                                               ; preds = %12
  %17 = icmp eq i64 %3, 0
  %18 = load i64, ptr @_tx_thread_system_state, align 8
  %.fr30 = freeze i64 %18
  %19 = icmp eq i64 %.fr30, 0
  %or.cond.not = or i1 %17, %19
  %20 = load ptr, ptr @_tx_thread_current_ptr, align 8
  %21 = icmp eq ptr %20, @_tx_timer_thread
  %spec.select23 = select i1 %21, i32 19, i32 0
  %spec.select27 = select i1 %or.cond.not, i32 %spec.select23, i32 4
  br label %.thread

.thread:                                          ; preds = %16, %12, %10, %8, %6, %4
  %22 = phi i32 [ 5, %12 ], [ 5, %10 ], [ 3, %8 ], [ 2, %6 ], [ 2, %4 ], [ %spec.select27, %16 ]
  %23 = icmp eq i32 %22, 0
  %24 = load i64, ptr @_tx_thread_system_state, align 8
  %25 = add i64 %24, -1
  %26 = icmp ult i64 %25, 4042322159
  %or.cond5 = select i1 %23, i1 %26, i1 false
  %spec.store.select = select i1 %or.cond5, i32 19, i32 %22
  %27 = icmp eq i32 %spec.store.select, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %.thread
  %29 = tail call i32 @_tx_byte_allocate(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) #2
  br label %30

30:                                               ; preds = %28, %.thread
  %.2 = phi i32 [ %29, %28 ], [ %spec.store.select, %.thread ]
  ret i32 %.2
}

declare i32 @_tx_byte_allocate(ptr noundef, ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #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 = { "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 = { 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, !7, i64 0}
!6 = !{!"TX_BYTE_POOL_STRUCT", !7, i64 0, !10, i64 8, !7, i64 16, !11, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !7, i64 56, !10, i64 64, !10, i64 72, !11, i64 80, !10, i64 88, !10, i64 96}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !7, i64 56}


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