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

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

@OSTCBPrioTbl = external global [64 x ptr], align 16
@OSRdyTbl = external global [8 x i8], align 1
@OSRdyGrp = external global i8, align 1
@OSRunning = external global i8, align 1

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

declare i32 @OS_CPU_SR_Save() #1

declare void @OS_CPU_SR_Restore(i32 noundef) #1

declare void @OS_Sched() #1

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @OSTaskResume(i8 noundef zeroext %0) #2 {
  %2 = alloca i8, align 1
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i8 %0, ptr %3, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !8
  %7 = call i32 @OS_CPU_SR_Save()
  store i32 %7, ptr %5, align 4, !tbaa !8
  %8 = load i8, ptr %3, align 1, !tbaa !5
  %9 = zext i8 %8 to i64
  %10 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %9
  %11 = load ptr, ptr %10, align 8, !tbaa !10
  store ptr %11, ptr %4, align 8, !tbaa !10
  %12 = load ptr, ptr %4, align 8, !tbaa !10
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %16

14:                                               ; preds = %1
  %15 = load i32, ptr %5, align 4, !tbaa !8
  call void @OS_CPU_SR_Restore(i32 noundef %15)
  store i8 70, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %81

16:                                               ; preds = %1
  %17 = load ptr, ptr %4, align 8, !tbaa !10
  %18 = icmp eq ptr %17, inttoptr (i64 1 to ptr)
  br i1 %18, label %19, label %21

19:                                               ; preds = %16
  %20 = load i32, ptr %5, align 4, !tbaa !8
  call void @OS_CPU_SR_Restore(i32 noundef %20)
  store i8 67, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %81

21:                                               ; preds = %16
  %22 = load ptr, ptr %4, align 8, !tbaa !10
  %23 = getelementptr inbounds %struct.os_tcb, ptr %22, i32 0, i32 13
  %24 = load i8, ptr %23, align 8, !tbaa !12
  %25 = zext i8 %24 to i32
  %26 = and i32 %25, 8
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %79

28:                                               ; preds = %21
  %29 = load ptr, ptr %4, align 8, !tbaa !10
  %30 = getelementptr inbounds %struct.os_tcb, ptr %29, i32 0, i32 13
  %31 = load i8, ptr %30, align 8, !tbaa !12
  %32 = zext i8 %31 to i32
  %33 = and i32 %32, 247
  %34 = trunc i32 %33 to i8
  store i8 %34, ptr %30, align 8, !tbaa !12
  %35 = load ptr, ptr %4, align 8, !tbaa !10
  %36 = getelementptr inbounds %struct.os_tcb, ptr %35, i32 0, i32 13
  %37 = load i8, ptr %36, align 8, !tbaa !12
  %38 = zext i8 %37 to i32
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %76

40:                                               ; preds = %28
  %41 = load ptr, ptr %4, align 8, !tbaa !10
  %42 = getelementptr inbounds %struct.os_tcb, ptr %41, i32 0, i32 12
  %43 = load i32, ptr %42, align 4, !tbaa !15
  %44 = icmp eq i32 %43, 0
  br i1 %44, label %45, label %73

45:                                               ; preds = %40
  %46 = load ptr, ptr %4, align 8, !tbaa !10
  %47 = getelementptr inbounds %struct.os_tcb, ptr %46, i32 0, i32 19
  %48 = load i8, ptr %47, align 2, !tbaa !16
  %49 = zext i8 %48 to i32
  %50 = load i8, ptr @OSRdyGrp, align 1, !tbaa !5
  %51 = zext i8 %50 to i32
  %52 = or i32 %51, %49
  %53 = trunc i32 %52 to i8
  store i8 %53, ptr @OSRdyGrp, align 1, !tbaa !5
  %54 = load ptr, ptr %4, align 8, !tbaa !10
  %55 = getelementptr inbounds %struct.os_tcb, ptr %54, i32 0, i32 18
  %56 = load i8, ptr %55, align 1, !tbaa !17
  %57 = zext i8 %56 to i32
  %58 = load ptr, ptr %4, align 8, !tbaa !10
  %59 = getelementptr inbounds %struct.os_tcb, ptr %58, i32 0, i32 17
  %60 = load i8, ptr %59, align 4, !tbaa !18
  %61 = zext i8 %60 to i64
  %62 = getelementptr inbounds [8 x i8], ptr @OSRdyTbl, i64 0, i64 %61
  %63 = load i8, ptr %62, align 1, !tbaa !5
  %64 = zext i8 %63 to i32
  %65 = or i32 %64, %57
  %66 = trunc i32 %65 to i8
  store i8 %66, ptr %62, align 1, !tbaa !5
  %67 = load i32, ptr %5, align 4, !tbaa !8
  call void @OS_CPU_SR_Restore(i32 noundef %67)
  %68 = load i8, ptr @OSRunning, align 1, !tbaa !5
  %69 = zext i8 %68 to i32
  %70 = icmp eq i32 %69, 1
  br i1 %70, label %71, label %72

71:                                               ; preds = %45
  call void @OS_Sched()
  br label %72

72:                                               ; preds = %71, %45
  br label %75

73:                                               ; preds = %40
  %74 = load i32, ptr %5, align 4, !tbaa !8
  call void @OS_CPU_SR_Restore(i32 noundef %74)
  br label %75

75:                                               ; preds = %73, %72
  br label %78

76:                                               ; preds = %28
  %77 = load i32, ptr %5, align 4, !tbaa !8
  call void @OS_CPU_SR_Restore(i32 noundef %77)
  br label %78

78:                                               ; preds = %76, %75
  store i8 0, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %81

79:                                               ; preds = %21
  %80 = load i32, ptr %5, align 4, !tbaa !8
  call void @OS_CPU_SR_Restore(i32 noundef %80)
  store i8 68, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %81

81:                                               ; preds = %79, %78, %19, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %82 = load i8, ptr %2, align 1
  ret i8 %82
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = { 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 = !{!"int", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !6, i64 0}
!12 = !{!13, !6, i64 80}
!13 = !{!"os_tcb", !11, i64 0, !11, i64 8, !11, i64 16, !9, 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, !9, 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, !9, i64 88, !9, i64 92, !9, i64 96, !11, i64 104, !9, i64 112, !11, i64 120, !6, i64 128}
!14 = !{!"short", !6, i64 0}
!15 = !{!13, !9, i64 76}
!16 = !{!13, !6, i64 86}
!17 = !{!13, !6, i64 85}
!18 = !{!13, !6, i64 84}


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

@OSTCBPrioTbl = external local_unnamed_addr global [64 x ptr], align 16
@OSRdyTbl = external local_unnamed_addr global [8 x i8], align 1
@OSRdyGrp = external local_unnamed_addr global i8, align 1
@OSRunning = external local_unnamed_addr global i8, align 1

declare i32 @OS_CPU_SR_Save() local_unnamed_addr #0

declare void @OS_CPU_SR_Restore(i32 noundef) local_unnamed_addr #0

declare void @OS_Sched() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 71) i8 @OSTaskResume(i8 noundef zeroext %0) local_unnamed_addr #1 {
  %2 = tail call i32 @OS_CPU_SR_Save() #2
  %3 = zext i8 %0 to i64
  %4 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %3
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %magicptr = ptrtoint ptr %5 to i64
  switch i64 %magicptr, label %8 [
    i64 0, label %6
    i64 1, label %7
  ]

6:                                                ; preds = %1
  tail call void @OS_CPU_SR_Restore(i32 noundef %2) #2
  br label %38

7:                                                ; preds = %1
  tail call void @OS_CPU_SR_Restore(i32 noundef %2) #2
  br label %38

8:                                                ; preds = %1
  %9 = getelementptr inbounds i8, ptr %5, i64 80
  %10 = load i8, ptr %9, align 8, !tbaa !9
  %11 = and i8 %10, 8
  %.not = icmp eq i8 %11, 0
  br i1 %.not, label %37, label %12

12:                                               ; preds = %8
  %13 = and i8 %10, -9
  store i8 %13, ptr %9, align 8, !tbaa !9
  %14 = icmp eq i8 %13, 0
  br i1 %14, label %15, label %36

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %5, i64 76
  %17 = load i32, ptr %16, align 4, !tbaa !13
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %35

19:                                               ; preds = %15
  %20 = getelementptr inbounds i8, ptr %5, i64 86
  %21 = load i8, ptr %20, align 2, !tbaa !14
  %22 = load i8, ptr @OSRdyGrp, align 1, !tbaa !15
  %23 = or i8 %22, %21
  store i8 %23, ptr @OSRdyGrp, align 1, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %5, i64 85
  %25 = load i8, ptr %24, align 1, !tbaa !16
  %26 = getelementptr inbounds i8, ptr %5, i64 84
  %27 = load i8, ptr %26, align 4, !tbaa !17
  %28 = zext i8 %27 to i64
  %29 = getelementptr inbounds [8 x i8], ptr @OSRdyTbl, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !15
  %31 = or i8 %30, %25
  store i8 %31, ptr %29, align 1, !tbaa !15
  tail call void @OS_CPU_SR_Restore(i32 noundef %2) #2
  %32 = load i8, ptr @OSRunning, align 1, !tbaa !15
  %33 = icmp eq i8 %32, 1
  br i1 %33, label %34, label %38

34:                                               ; preds = %19
  tail call void @OS_Sched() #2
  br label %38

35:                                               ; preds = %15
  tail call void @OS_CPU_SR_Restore(i32 noundef %2) #2
  br label %38

36:                                               ; preds = %12
  tail call void @OS_CPU_SR_Restore(i32 noundef %2) #2
  br label %38

37:                                               ; preds = %8
  tail call void @OS_CPU_SR_Restore(i32 noundef %2) #2
  br label %38

38:                                               ; preds = %36, %19, %34, %35, %37, %7, %6
  %.0 = phi i8 [ 70, %6 ], [ 67, %7 ], [ 68, %37 ], [ 0, %35 ], [ 0, %34 ], [ 0, %19 ], [ 0, %36 ]
  ret i8 %.0
}

attributes #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 = { 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 = { 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, !7, i64 80}
!10 = !{!"os_tcb", !6, i64 0, !6, i64 8, !6, i64 16, !11, i64 24, !12, i64 28, !12, i64 30, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !12, i64 72, !11, 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, !11, i64 88, !11, i64 92, !11, i64 96, !6, i64 104, !11, i64 112, !6, i64 120, !7, i64 128}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!10, !11, i64 76}
!14 = !{!10, !7, i64 86}
!15 = !{!7, !7, i64 0}
!16 = !{!10, !7, i64 85}
!17 = !{!10, !7, i64 84}


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