; LLVM IR from HANDS-FREE/OpenRE :: 3_OS/STM32F1/RTOS/uCOS-II/CORE (os_core)

; 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.os_tcb = type { ptr, ptr, ptr, i32, i16, i16, ptr, ptr, ptr, ptr, ptr, i16, i32, i8, i8, i8, i8, i8, i8, i8, i8, i32, i32, i32, ptr, i32, ptr, [1 x i32] }

@OSRunning = external dso_local global i8, align 1
@OSIntNesting = external dso_local global i8, align 1
@OSLockNesting = external dso_local global i8, align 1
@OSTCBPrioTbl = external dso_local global [64 x ptr], align 16
@OSPrioHighRdy = external dso_local global i8, align 1
@OSTCBHighRdy = external dso_local global ptr, align 8
@OSPrioCur = external dso_local global i8, align 1
@OSCtxSwCtr = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @OSIntExit() #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  %2 = load i8, ptr @OSRunning, align 1, !tbaa !9
  %3 = zext i8 %2 to i32
  %4 = icmp eq i32 %3, 1
  br i1 %4, label %5, label %42

5:                                                ; preds = %0
  %6 = call i32 @OS_CPU_SR_Save()
  store i32 %6, ptr %1, align 4, !tbaa !5
  %7 = load i8, ptr @OSIntNesting, align 1, !tbaa !9
  %8 = zext i8 %7 to i32
  %9 = icmp ugt i32 %8, 0
  br i1 %9, label %10, label %13

10:                                               ; preds = %5
  %11 = load i8, ptr @OSIntNesting, align 1, !tbaa !9
  %12 = add i8 %11, -1
  store i8 %12, ptr @OSIntNesting, align 1, !tbaa !9
  br label %13

13:                                               ; preds = %10, %5
  %14 = load i8, ptr @OSIntNesting, align 1, !tbaa !9
  %15 = zext i8 %14 to i32
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %17, label %40

17:                                               ; preds = %13
  %18 = load i8, ptr @OSLockNesting, align 1, !tbaa !9
  %19 = zext i8 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %39

21:                                               ; preds = %17
  call void @OS_SchedNew()
  %22 = load i8, ptr @OSPrioHighRdy, align 1, !tbaa !9
  %23 = zext i8 %22 to i64
  %24 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %23
  %25 = load ptr, ptr %24, align 8, !tbaa !10
  store ptr %25, ptr @OSTCBHighRdy, align 8, !tbaa !10
  %26 = load i8, ptr @OSPrioHighRdy, align 1, !tbaa !9
  %27 = zext i8 %26 to i32
  %28 = load i8, ptr @OSPrioCur, align 1, !tbaa !9
  %29 = zext i8 %28 to i32
  %30 = icmp ne i32 %27, %29
  br i1 %30, label %31, label %38

31:                                               ; preds = %21
  %32 = load ptr, ptr @OSTCBHighRdy, align 8, !tbaa !10
  %33 = getelementptr inbounds %struct.os_tcb, ptr %32, i32 0, i32 21
  %34 = load i32, ptr %33, align 8, !tbaa !12
  %35 = add i32 %34, 1
  store i32 %35, ptr %33, align 8, !tbaa !12
  %36 = load i32, ptr @OSCtxSwCtr, align 4, !tbaa !5
  %37 = add i32 %36, 1
  store i32 %37, ptr @OSCtxSwCtr, align 4, !tbaa !5
  call void @OSIntCtxSw()
  br label %38

38:                                               ; preds = %31, %21
  br label %39

39:                                               ; preds = %38, %17
  br label %40

40:                                               ; preds = %39, %13
  %41 = load i32, ptr %1, align 4, !tbaa !5
  call void @OS_CPU_SR_Restore(i32 noundef %41)
  br label %42

42:                                               ; preds = %40, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

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

declare i32 @OS_CPU_SR_Save() #2

; Function Attrs: nounwind uwtable
declare hidden void @OS_SchedNew() #0

declare void @OSIntCtxSw() #2

declare void @OS_CPU_SR_Restore(i32 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!13, !6, i64 88}
!13 = !{!"os_tcb", !11, i64 0, !11, i64 8, !11, i64 16, !6, i64 24, !14, i64 28, !14, i64 30, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !14, i64 72, !6, i64 76, !7, i64 80, !7, i64 81, !7, i64 82, !7, i64 83, !7, i64 84, !7, i64 85, !7, i64 86, !7, i64 87, !6, i64 88, !6, i64 92, !6, i64 96, !11, i64 104, !6, i64 112, !11, i64 120, !7, i64 128}
!14 = !{!"short", !7, i64 0}


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

@OSRunning = external dso_local local_unnamed_addr global i8, align 1
@OSIntNesting = external dso_local local_unnamed_addr global i8, align 1
@OSLockNesting = external dso_local local_unnamed_addr global i8, align 1
@OSTCBPrioTbl = external dso_local local_unnamed_addr global [64 x ptr], align 16
@OSPrioHighRdy = external dso_local local_unnamed_addr global i8, align 1
@OSTCBHighRdy = external dso_local local_unnamed_addr global ptr, align 8
@OSPrioCur = external dso_local local_unnamed_addr global i8, align 1
@OSCtxSwCtr = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local void @OSIntExit() local_unnamed_addr #0 {
  %1 = load i8, ptr @OSRunning, align 1, !tbaa !5
  %2 = icmp eq i8 %1, 1
  br i1 %2, label %3, label %26

3:                                                ; preds = %0
  %4 = tail call i32 @OS_CPU_SR_Save() #2
  %5 = load i8, ptr @OSIntNesting, align 1, !tbaa !5
  %.not = icmp eq i8 %5, 0
  br i1 %.not, label %9, label %6

6:                                                ; preds = %3
  %7 = add i8 %5, -1
  store i8 %7, ptr @OSIntNesting, align 1, !tbaa !5
  %8 = icmp eq i8 %7, 0
  br label %9

9:                                                ; preds = %6, %3
  %10 = phi i1 [ %8, %6 ], [ true, %3 ]
  %11 = load i8, ptr @OSLockNesting, align 1
  %12 = icmp eq i8 %11, 0
  %or.cond = select i1 %10, i1 %12, i1 false
  br i1 %or.cond, label %13, label %25

13:                                               ; preds = %9
  tail call void @OS_SchedNew()
  %14 = load i8, ptr @OSPrioHighRdy, align 1, !tbaa !5
  %15 = zext i8 %14 to i64
  %16 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %15
  %17 = load ptr, ptr %16, align 8, !tbaa !8
  store ptr %17, ptr @OSTCBHighRdy, align 8, !tbaa !8
  %18 = load i8, ptr @OSPrioCur, align 1, !tbaa !5
  %.not3 = icmp eq i8 %14, %18
  br i1 %.not3, label %25, label %19

19:                                               ; preds = %13
  %20 = getelementptr inbounds i8, ptr %17, i64 88
  %21 = load i32, ptr %20, align 8, !tbaa !10
  %22 = add i32 %21, 1
  store i32 %22, ptr %20, align 8, !tbaa !10
  %23 = load i32, ptr @OSCtxSwCtr, align 4, !tbaa !14
  %24 = add i32 %23, 1
  store i32 %24, ptr @OSCtxSwCtr, align 4, !tbaa !14
  tail call void @OSIntCtxSw() #2
  br label %25

25:                                               ; preds = %19, %13, %9
  tail call void @OS_CPU_SR_Restore(i32 noundef %4) #2
  br label %26

26:                                               ; preds = %25, %0
  ret void
}

declare i32 @OS_CPU_SR_Save() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @OS_SchedNew() local_unnamed_addr #0

declare void @OSIntCtxSw() local_unnamed_addr #1

declare void @OS_CPU_SR_Restore(i32 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !12, i64 88}
!11 = !{!"os_tcb", !9, i64 0, !9, i64 8, !9, i64 16, !12, i64 24, !13, i64 28, !13, i64 30, !9, i64 32, !9, i64 40, !9, i64 48, !9, i64 56, !9, i64 64, !13, i64 72, !12, i64 76, !6, i64 80, !6, i64 81, !6, i64 82, !6, i64 83, !6, i64 84, !6, i64 85, !6, i64 86, !6, i64 87, !12, i64 88, !12, i64 92, !12, i64 96, !9, i64 104, !12, i64 112, !9, i64 120, !6, i64 128}
!12 = !{!"int", !6, i64 0}
!13 = !{!"short", !6, i64 0}
!14 = !{!12, !12, i64 0}


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