; 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_stk_data = type { i32, i32 }
%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
@OSTCBCur = external global ptr, align 8

; 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

; 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 @OSTaskStkChk(i8 noundef zeroext %0, ptr noundef %1) #2 {
  %3 = alloca i8, align 1
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i8 %0, ptr %4, align 1, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !10
  %12 = load ptr, ptr %5, align 8, !tbaa !8
  %13 = getelementptr inbounds %struct.os_stk_data, ptr %12, i32 0, i32 0
  store i32 0, ptr %13, align 4, !tbaa !12
  %14 = load ptr, ptr %5, align 8, !tbaa !8
  %15 = getelementptr inbounds %struct.os_stk_data, ptr %14, i32 0, i32 1
  store i32 0, ptr %15, align 4, !tbaa !14
  %16 = call i32 @OS_CPU_SR_Save()
  store i32 %16, ptr %10, align 4, !tbaa !10
  %17 = load i8, ptr %4, align 1, !tbaa !5
  %18 = zext i8 %17 to i32
  %19 = icmp eq i32 %18, 255
  br i1 %19, label %20, label %24

20:                                               ; preds = %2
  %21 = load ptr, ptr @OSTCBCur, align 8, !tbaa !8
  %22 = getelementptr inbounds %struct.os_tcb, ptr %21, i32 0, i32 15
  %23 = load i8, ptr %22, align 2, !tbaa !15
  store i8 %23, ptr %4, align 1, !tbaa !5
  br label %24

24:                                               ; preds = %20, %2
  %25 = load i8, ptr %4, align 1, !tbaa !5
  %26 = zext i8 %25 to i64
  %27 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !8
  store ptr %28, ptr %6, align 8, !tbaa !8
  %29 = load ptr, ptr %6, align 8, !tbaa !8
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %33

31:                                               ; preds = %24
  %32 = load i32, ptr %10, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %32)
  store i8 67, ptr %3, align 1
  store i32 1, ptr %11, align 4
  br label %78

33:                                               ; preds = %24
  %34 = load ptr, ptr %6, align 8, !tbaa !8
  %35 = icmp eq ptr %34, inttoptr (i64 1 to ptr)
  br i1 %35, label %36, label %38

36:                                               ; preds = %33
  %37 = load i32, ptr %10, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %37)
  store i8 67, ptr %3, align 1
  store i32 1, ptr %11, align 4
  br label %78

38:                                               ; preds = %33
  %39 = load ptr, ptr %6, align 8, !tbaa !8
  %40 = getelementptr inbounds %struct.os_tcb, ptr %39, i32 0, i32 4
  %41 = load i16, ptr %40, align 4, !tbaa !18
  %42 = zext i16 %41 to i32
  %43 = and i32 %42, 1
  %44 = icmp eq i32 %43, 0
  br i1 %44, label %45, label %47

45:                                               ; preds = %38
  %46 = load i32, ptr %10, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %46)
  store i8 69, ptr %3, align 1
  store i32 1, ptr %11, align 4
  br label %78

47:                                               ; preds = %38
  store i32 0, ptr %8, align 4, !tbaa !10
  %48 = load ptr, ptr %6, align 8, !tbaa !8
  %49 = getelementptr inbounds %struct.os_tcb, ptr %48, i32 0, i32 3
  %50 = load i32, ptr %49, align 8, !tbaa !19
  store i32 %50, ptr %9, align 4, !tbaa !10
  %51 = load ptr, ptr %6, align 8, !tbaa !8
  %52 = getelementptr inbounds %struct.os_tcb, ptr %51, i32 0, i32 2
  %53 = load ptr, ptr %52, align 8, !tbaa !20
  store ptr %53, ptr %7, align 8, !tbaa !8
  %54 = load i32, ptr %10, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %54)
  br label %55

55:                                               ; preds = %60, %47
  %56 = load ptr, ptr %7, align 8, !tbaa !8
  %57 = getelementptr inbounds i32, ptr %56, i32 1
  store ptr %57, ptr %7, align 8, !tbaa !8
  %58 = load i32, ptr %56, align 4, !tbaa !10
  %59 = icmp eq i32 %58, 0
  br i1 %59, label %60, label %63

60:                                               ; preds = %55
  %61 = load i32, ptr %8, align 4, !tbaa !10
  %62 = add i32 %61, 1
  store i32 %62, ptr %8, align 4, !tbaa !10
  br label %55, !llvm.loop !21

63:                                               ; preds = %55
  %64 = load i32, ptr %8, align 4, !tbaa !10
  %65 = zext i32 %64 to i64
  %66 = mul i64 %65, 4
  %67 = trunc i64 %66 to i32
  %68 = load ptr, ptr %5, align 8, !tbaa !8
  %69 = getelementptr inbounds %struct.os_stk_data, ptr %68, i32 0, i32 0
  store i32 %67, ptr %69, align 4, !tbaa !12
  %70 = load i32, ptr %9, align 4, !tbaa !10
  %71 = load i32, ptr %8, align 4, !tbaa !10
  %72 = sub i32 %70, %71
  %73 = zext i32 %72 to i64
  %74 = mul i64 %73, 4
  %75 = trunc i64 %74 to i32
  %76 = load ptr, ptr %5, align 8, !tbaa !8
  %77 = getelementptr inbounds %struct.os_stk_data, ptr %76, i32 0, i32 1
  store i32 %75, ptr %77, align 4, !tbaa !14
  store i8 0, ptr %3, align 1
  store i32 1, ptr %11, align 4
  br label %78

78:                                               ; preds = %63, %45, %36, %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %79 = load i8, ptr %3, align 1
  ret i8 %79
}

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 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!13, !11, i64 0}
!13 = !{!"os_stk_data", !11, i64 0, !11, i64 4}
!14 = !{!13, !11, i64 4}
!15 = !{!16, !6, i64 82}
!16 = !{!"os_tcb", !9, i64 0, !9, i64 8, !9, i64 16, !11, i64 24, !17, i64 28, !17, i64 30, !9, i64 32, !9, i64 40, !9, i64 48, !9, i64 56, !9, i64 64, !17, i64 72, !11, 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, !11, i64 88, !11, i64 92, !11, i64 96, !9, i64 104, !11, i64 112, !9, i64 120, !6, i64 128}
!17 = !{!"short", !6, i64 0}
!18 = !{!16, !17, i64 28}
!19 = !{!16, !11, i64 24}
!20 = !{!16, !9, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


; 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
@OSTCBCur = external local_unnamed_addr global ptr, align 8

declare i32 @OS_CPU_SR_Save() local_unnamed_addr #0

declare void @OS_CPU_SR_Restore(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 70) i8 @OSTaskStkChk(i8 noundef zeroext %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  store i32 0, ptr %1, align 4, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %1, i64 4
  store i32 0, ptr %3, align 4, !tbaa !10
  %4 = tail call i32 @OS_CPU_SR_Save() #2
  %5 = icmp eq i8 %0, -1
  br i1 %5, label %6, label %10

6:                                                ; preds = %2
  %7 = load ptr, ptr @OSTCBCur, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %7, i64 82
  %9 = load i8, ptr %8, align 2, !tbaa !13
  br label %10

10:                                               ; preds = %6, %2
  %.022 = phi i8 [ %9, %6 ], [ %0, %2 ]
  %11 = zext i8 %.022 to i64
  %12 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %11
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %magicptr = ptrtoint ptr %13 to i64
  switch i64 %magicptr, label %16 [
    i64 0, label %14
    i64 1, label %15
  ]

14:                                               ; preds = %10
  tail call void @OS_CPU_SR_Restore(i32 noundef %4) #2
  br label %36

15:                                               ; preds = %10
  tail call void @OS_CPU_SR_Restore(i32 noundef %4) #2
  br label %36

16:                                               ; preds = %10
  %17 = getelementptr inbounds i8, ptr %13, i64 28
  %18 = load i16, ptr %17, align 4, !tbaa !16
  %19 = and i16 %18, 1
  %20 = icmp eq i16 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %16
  tail call void @OS_CPU_SR_Restore(i32 noundef %4) #2
  br label %36

22:                                               ; preds = %16
  %23 = getelementptr inbounds i8, ptr %13, i64 24
  %24 = load i32, ptr %23, align 8, !tbaa !17
  %25 = getelementptr inbounds i8, ptr %13, i64 16
  %26 = load ptr, ptr %25, align 8, !tbaa !18
  tail call void @OS_CPU_SR_Restore(i32 noundef %4) #2
  %27 = load i32, ptr %26, align 4, !tbaa !19
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %22, %.lr.ph
  %.02025 = phi i32 [ %30, %.lr.ph ], [ 0, %22 ]
  %.02124 = phi ptr [ %29, %.lr.ph ], [ %26, %22 ]
  %29 = getelementptr inbounds i8, ptr %.02124, i64 4
  %30 = add i32 %.02025, 1
  %31 = load i32, ptr %29, align 4, !tbaa !19
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %.lr.ph, label %._crit_edge, !llvm.loop !20

._crit_edge:                                      ; preds = %.lr.ph, %22
  %.020.lcssa = phi i32 [ 0, %22 ], [ %30, %.lr.ph ]
  %33 = shl i32 %.020.lcssa, 2
  store i32 %33, ptr %1, align 4, !tbaa !5
  %34 = sub i32 %24, %.020.lcssa
  %35 = shl i32 %34, 2
  store i32 %35, ptr %3, align 4, !tbaa !10
  br label %36

36:                                               ; preds = %._crit_edge, %21, %15, %14
  %.0 = phi i8 [ 67, %14 ], [ 67, %15 ], [ 69, %21 ], [ 0, %._crit_edge ]
  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, !7, i64 0}
!6 = !{!"os_stk_data", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!14, !8, i64 82}
!14 = !{!"os_tcb", !12, i64 0, !12, i64 8, !12, i64 16, !7, i64 24, !15, i64 28, !15, i64 30, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !15, i64 72, !7, i64 76, !8, i64 80, !8, i64 81, !8, i64 82, !8, i64 83, !8, i64 84, !8, i64 85, !8, i64 86, !8, i64 87, !7, i64 88, !7, i64 92, !7, i64 96, !12, i64 104, !7, i64 112, !12, i64 120, !8, i64 128}
!15 = !{!"short", !8, i64 0}
!16 = !{!14, !15, i64 28}
!17 = !{!14, !7, i64 24}
!18 = !{!14, !12, i64 16}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass