; 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
@OSTCBCur = external global ptr, align 8
@OSRdyTbl = external global [8 x i8], align 1
@OSRdyGrp = 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 @OSTaskSuspend(i8 noundef zeroext %0) #2 {
  %2 = alloca i8, align 1
  %3 = alloca i8, align 1
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i8 %0, ptr %3, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !8
  %9 = call i32 @OS_CPU_SR_Save()
  store i32 %9, ptr %7, align 4, !tbaa !8
  %10 = load i8, ptr %3, align 1, !tbaa !5
  %11 = zext i8 %10 to i32
  %12 = icmp eq i32 %11, 255
  br i1 %12, label %13, label %17

13:                                               ; preds = %1
  %14 = load ptr, ptr @OSTCBCur, align 8, !tbaa !10
  %15 = getelementptr inbounds %struct.os_tcb, ptr %14, i32 0, i32 15
  %16 = load i8, ptr %15, align 2, !tbaa !12
  store i8 %16, ptr %3, align 1, !tbaa !5
  store i8 1, ptr %4, align 1, !tbaa !5
  br label %28

17:                                               ; preds = %1
  %18 = load i8, ptr %3, align 1, !tbaa !5
  %19 = zext i8 %18 to i32
  %20 = load ptr, ptr @OSTCBCur, align 8, !tbaa !10
  %21 = getelementptr inbounds %struct.os_tcb, ptr %20, i32 0, i32 15
  %22 = load i8, ptr %21, align 2, !tbaa !12
  %23 = zext i8 %22 to i32
  %24 = icmp eq i32 %19, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %17
  store i8 1, ptr %4, align 1, !tbaa !5
  br label %27

26:                                               ; preds = %17
  store i8 0, ptr %4, align 1, !tbaa !5
  br label %27

27:                                               ; preds = %26, %25
  br label %28

28:                                               ; preds = %27, %13
  %29 = load i8, ptr %3, align 1, !tbaa !5
  %30 = zext i8 %29 to i64
  %31 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %30
  %32 = load ptr, ptr %31, align 8, !tbaa !10
  store ptr %32, ptr %5, align 8, !tbaa !10
  %33 = load ptr, ptr %5, align 8, !tbaa !10
  %34 = icmp eq ptr %33, null
  br i1 %34, label %35, label %37

35:                                               ; preds = %28
  %36 = load i32, ptr %7, align 4, !tbaa !8
  call void @OS_CPU_SR_Restore(i32 noundef %36)
  store i8 72, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %91

37:                                               ; preds = %28
  %38 = load ptr, ptr %5, align 8, !tbaa !10
  %39 = icmp eq ptr %38, inttoptr (i64 1 to ptr)
  br i1 %39, label %40, label %42

40:                                               ; preds = %37
  %41 = load i32, ptr %7, align 4, !tbaa !8
  call void @OS_CPU_SR_Restore(i32 noundef %41)
  store i8 67, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %91

42:                                               ; preds = %37
  %43 = load ptr, ptr %5, align 8, !tbaa !10
  %44 = getelementptr inbounds %struct.os_tcb, ptr %43, i32 0, i32 17
  %45 = load i8, ptr %44, align 4, !tbaa !15
  store i8 %45, ptr %6, align 1, !tbaa !5
  %46 = load ptr, ptr %5, align 8, !tbaa !10
  %47 = getelementptr inbounds %struct.os_tcb, ptr %46, i32 0, i32 18
  %48 = load i8, ptr %47, align 1, !tbaa !16
  %49 = zext i8 %48 to i32
  %50 = xor i32 %49, -1
  %51 = trunc i32 %50 to i8
  %52 = zext i8 %51 to i32
  %53 = load i8, ptr %6, align 1, !tbaa !5
  %54 = zext i8 %53 to i64
  %55 = getelementptr inbounds [8 x i8], ptr @OSRdyTbl, i64 0, i64 %54
  %56 = load i8, ptr %55, align 1, !tbaa !5
  %57 = zext i8 %56 to i32
  %58 = and i32 %57, %52
  %59 = trunc i32 %58 to i8
  store i8 %59, ptr %55, align 1, !tbaa !5
  %60 = load i8, ptr %6, align 1, !tbaa !5
  %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 = icmp eq i32 %64, 0
  br i1 %65, label %66, label %78

66:                                               ; preds = %42
  %67 = load ptr, ptr %5, align 8, !tbaa !10
  %68 = getelementptr inbounds %struct.os_tcb, ptr %67, i32 0, i32 19
  %69 = load i8, ptr %68, align 2, !tbaa !17
  %70 = zext i8 %69 to i32
  %71 = xor i32 %70, -1
  %72 = trunc i32 %71 to i8
  %73 = zext i8 %72 to i32
  %74 = load i8, ptr @OSRdyGrp, align 1, !tbaa !5
  %75 = zext i8 %74 to i32
  %76 = and i32 %75, %73
  %77 = trunc i32 %76 to i8
  store i8 %77, ptr @OSRdyGrp, align 1, !tbaa !5
  br label %78

78:                                               ; preds = %66, %42
  %79 = load ptr, ptr %5, align 8, !tbaa !10
  %80 = getelementptr inbounds %struct.os_tcb, ptr %79, i32 0, i32 13
  %81 = load i8, ptr %80, align 8, !tbaa !18
  %82 = zext i8 %81 to i32
  %83 = or i32 %82, 8
  %84 = trunc i32 %83 to i8
  store i8 %84, ptr %80, align 8, !tbaa !18
  %85 = load i32, ptr %7, align 4, !tbaa !8
  call void @OS_CPU_SR_Restore(i32 noundef %85)
  %86 = load i8, ptr %4, align 1, !tbaa !5
  %87 = zext i8 %86 to i32
  %88 = icmp eq i32 %87, 1
  br i1 %88, label %89, label %90

89:                                               ; preds = %78
  call void @OS_Sched()
  br label %90

90:                                               ; preds = %89, %78
  store i8 0, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %91

91:                                               ; preds = %90, %40, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  %92 = load i8, ptr %2, align 1
  ret i8 %92
}

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 82}
!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, !6, i64 84}
!16 = !{!13, !6, i64 85}
!17 = !{!13, !6, i64 86}
!18 = !{!13, !6, i64 80}


; 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
@OSRdyTbl = external local_unnamed_addr global [8 x i8], align 1
@OSRdyGrp = 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, 73) i8 @OSTaskSuspend(i8 noundef zeroext %0) local_unnamed_addr #1 {
  %2 = tail call i32 @OS_CPU_SR_Save() #2
  %3 = icmp eq i8 %0, -1
  %4 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 82
  %6 = load i8, ptr %5, align 2, !tbaa !9
  %7 = icmp eq i8 %6, %0
  %8 = select i1 %3, i1 true, i1 %7
  %.015 = select i1 %3, i8 %6, i8 %0
  %9 = zext i8 %.015 to i64
  %10 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %9
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %magicptr = ptrtoint ptr %11 to i64
  switch i64 %magicptr, label %14 [
    i64 0, label %12
    i64 1, label %13
  ]

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

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

14:                                               ; preds = %1
  %15 = getelementptr inbounds i8, ptr %11, i64 84
  %16 = load i8, ptr %15, align 4, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %11, i64 85
  %18 = load i8, ptr %17, align 1, !tbaa !14
  %19 = xor i8 %18, -1
  %20 = zext i8 %16 to i64
  %21 = getelementptr inbounds [8 x i8], ptr @OSRdyTbl, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !15
  %23 = and i8 %22, %19
  store i8 %23, ptr %21, align 1, !tbaa !15
  %24 = icmp eq i8 %23, 0
  br i1 %24, label %25, label %31

25:                                               ; preds = %14
  %26 = getelementptr inbounds i8, ptr %11, i64 86
  %27 = load i8, ptr %26, align 2, !tbaa !16
  %28 = xor i8 %27, -1
  %29 = load i8, ptr @OSRdyGrp, align 1, !tbaa !15
  %30 = and i8 %29, %28
  store i8 %30, ptr @OSRdyGrp, align 1, !tbaa !15
  br label %31

31:                                               ; preds = %25, %14
  %32 = getelementptr inbounds i8, ptr %11, i64 80
  %33 = load i8, ptr %32, align 8, !tbaa !17
  %34 = or i8 %33, 8
  store i8 %34, ptr %32, align 8, !tbaa !17
  tail call void @OS_CPU_SR_Restore(i32 noundef %2) #2
  br i1 %8, label %35, label %36

35:                                               ; preds = %31
  tail call void @OS_Sched() #2
  br label %36

36:                                               ; preds = %31, %35, %13, %12
  %.0 = phi i8 [ 72, %12 ], [ 67, %13 ], [ 0, %35 ], [ 0, %31 ]
  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 82}
!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, !7, i64 84}
!14 = !{!10, !7, i64 85}
!15 = !{!7, !7, i64 0}
!16 = !{!10, !7, i64 86}
!17 = !{!10, !7, i64 80}


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