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

; 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_flag_grp = type { i8, ptr, i16, ptr }
%struct.os_flag_node = type { ptr, ptr, ptr, ptr, i16, i8 }

@OSIntNesting = external global i8, align 1
@OSFlagFreeList = external global ptr, align 8
@.str = external hidden unnamed_addr constant [2 x 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

; 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 ptr @OSFlagDel(ptr noundef %0, i8 noundef zeroext %1, ptr noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca i8, align 1
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i8 %1, ptr %6, align 1, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !10
  %13 = load i8, ptr @OSIntNesting, align 1, !tbaa !9
  %14 = zext i8 %13 to i32
  %15 = icmp ugt i32 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %3
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 15, ptr %17, align 1, !tbaa !9
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %18, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %98

19:                                               ; preds = %3
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.os_flag_grp, ptr %20, i32 0, i32 0
  %22 = load i8, ptr %21, align 8, !tbaa !12
  %23 = zext i8 %22 to i32
  %24 = icmp ne i32 %23, 5
  br i1 %24, label %25, label %28

25:                                               ; preds = %19
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 1, ptr %26, align 1, !tbaa !9
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %27, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %98

28:                                               ; preds = %19
  %29 = call i32 @OS_CPU_SR_Save()
  store i32 %29, ptr %11, align 4, !tbaa !10
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.os_flag_grp, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !15
  %33 = icmp ne ptr %32, null
  br i1 %33, label %34, label %35

34:                                               ; preds = %28
  store i8 1, ptr %8, align 1, !tbaa !9
  br label %36

35:                                               ; preds = %28
  store i8 0, ptr %8, align 1, !tbaa !9
  br label %36

36:                                               ; preds = %35, %34
  %37 = load i8, ptr %6, align 1, !tbaa !9
  %38 = zext i8 %37 to i32
  switch i32 %38, label %92 [
    i32 0, label %39
    i32 1, label %61
  ]

39:                                               ; preds = %36
  %40 = load i8, ptr %8, align 1, !tbaa !9
  %41 = zext i8 %40 to i32
  %42 = icmp eq i32 %41, 0
  br i1 %42, label %43, label %56

43:                                               ; preds = %39
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.os_flag_grp, ptr %44, i32 0, i32 3
  store ptr @.str, ptr %45, align 8, !tbaa !16
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.os_flag_grp, ptr %46, i32 0, i32 0
  store i8 0, ptr %47, align 8, !tbaa !12
  %48 = load ptr, ptr @OSFlagFreeList, align 8, !tbaa !5
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.os_flag_grp, ptr %49, i32 0, i32 1
  store ptr %48, ptr %50, align 8, !tbaa !15
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.os_flag_grp, ptr %51, i32 0, i32 2
  store i16 0, ptr %52, align 8, !tbaa !17
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %53, ptr @OSFlagFreeList, align 8, !tbaa !5
  %54 = load i32, ptr %11, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %54)
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %55, align 1, !tbaa !9
  store ptr null, ptr %10, align 8, !tbaa !5
  br label %60

56:                                               ; preds = %39
  %57 = load i32, ptr %11, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %57)
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 73, ptr %58, align 1, !tbaa !9
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %59, ptr %10, align 8, !tbaa !5
  br label %60

60:                                               ; preds = %56, %43
  br label %96

61:                                               ; preds = %36
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.os_flag_grp, ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %63, align 8, !tbaa !15
  store ptr %64, ptr %9, align 8, !tbaa !5
  br label %65

65:                                               ; preds = %68, %61
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = icmp ne ptr %66, null
  br i1 %67, label %68, label %74

68:                                               ; preds = %65
  %69 = load ptr, ptr %9, align 8, !tbaa !5
  %70 = call zeroext i8 @OS_FlagTaskRdy(ptr noundef %69, i16 noundef zeroext 0)
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.os_flag_node, ptr %71, i32 0, i32 0
  %73 = load ptr, ptr %72, align 8, !tbaa !18
  store ptr %73, ptr %9, align 8, !tbaa !5
  br label %65, !llvm.loop !20

74:                                               ; preds = %65
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.os_flag_grp, ptr %75, i32 0, i32 3
  store ptr @.str, ptr %76, align 8, !tbaa !16
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.os_flag_grp, ptr %77, i32 0, i32 0
  store i8 0, ptr %78, align 8, !tbaa !12
  %79 = load ptr, ptr @OSFlagFreeList, align 8, !tbaa !5
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.os_flag_grp, ptr %80, i32 0, i32 1
  store ptr %79, ptr %81, align 8, !tbaa !15
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.os_flag_grp, ptr %82, i32 0, i32 2
  store i16 0, ptr %83, align 8, !tbaa !17
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %84, ptr @OSFlagFreeList, align 8, !tbaa !5
  %85 = load i32, ptr %11, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %85)
  %86 = load i8, ptr %8, align 1, !tbaa !9
  %87 = zext i8 %86 to i32
  %88 = icmp eq i32 %87, 1
  br i1 %88, label %89, label %90

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

90:                                               ; preds = %89, %74
  %91 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %91, align 1, !tbaa !9
  store ptr null, ptr %10, align 8, !tbaa !5
  br label %96

92:                                               ; preds = %36
  %93 = load i32, ptr %11, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %93)
  %94 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 7, ptr %94, align 1, !tbaa !9
  %95 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %95, ptr %10, align 8, !tbaa !5
  br label %96

96:                                               ; preds = %92, %90, %60
  %97 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %97, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %98

98:                                               ; preds = %96, %25, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  %99 = load ptr, ptr %4, align 8
  ret ptr %99
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @OS_FlagTaskRdy(ptr noundef, i16 noundef zeroext) #2

declare void @OS_Sched() #1

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 = !{!"any pointer", !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 = !{!"int", !7, i64 0}
!12 = !{!13, !7, i64 0}
!13 = !{!"os_flag_grp", !7, i64 0, !6, i64 8, !14, i64 16, !6, i64 24}
!14 = !{!"short", !7, i64 0}
!15 = !{!13, !6, i64 8}
!16 = !{!13, !6, i64 24}
!17 = !{!13, !14, i64 16}
!18 = !{!19, !6, i64 0}
!19 = !{!"os_flag_node", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !14, i64 32, !7, i64 34}
!20 = distinct !{!20, !21}
!21 = !{!"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"

@OSIntNesting = external local_unnamed_addr global i8, align 1
@OSFlagFreeList = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [2 x 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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @OSFlagDel(ptr noundef %0, i8 noundef zeroext %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #1 {
  %4 = load i8, ptr @OSIntNesting, align 1, !tbaa !5
  %.not = icmp eq i8 %4, 0
  br i1 %.not, label %5, label %26

5:                                                ; preds = %3
  %6 = load i8, ptr %0, align 8, !tbaa !8
  %.not37 = icmp eq i8 %6, 5
  br i1 %.not37, label %7, label %26

7:                                                ; preds = %5
  %8 = tail call i32 @OS_CPU_SR_Save() #2
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %.not38.not = icmp eq ptr %10, null
  switch i8 %1, label %25 [
    i8 0, label %11
    i8 1, label %.preheader
  ]

.preheader:                                       ; preds = %7
  br i1 %.not38.not, label %.critedge, label %.lr.ph

11:                                               ; preds = %7
  br i1 %.not38.not, label %12, label %16

12:                                               ; preds = %11
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr @.str, ptr %13, align 8, !tbaa !13
  store i8 0, ptr %0, align 8, !tbaa !8
  %14 = load ptr, ptr @OSFlagFreeList, align 8, !tbaa !12
  store ptr %14, ptr %9, align 8, !tbaa !14
  %15 = getelementptr inbounds i8, ptr %0, i64 16
  store i16 0, ptr %15, align 8, !tbaa !15
  store ptr %0, ptr @OSFlagFreeList, align 8, !tbaa !12
  tail call void @OS_CPU_SR_Restore(i32 noundef %8) #2
  br label %26

16:                                               ; preds = %11
  tail call void @OS_CPU_SR_Restore(i32 noundef %8) #2
  br label %26

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.03441 = phi ptr [ %.034.pr, %.lr.ph ], [ %10, %.preheader ]
  %17 = tail call zeroext i8 @OS_FlagTaskRdy(ptr noundef nonnull %.03441, i16 noundef zeroext 0)
  %.034.pr = load ptr, ptr %.03441, align 8, !tbaa !12
  %.not39 = icmp eq ptr %.034.pr, null
  br i1 %.not39, label %._crit_edge, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph
  %18 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr @.str, ptr %18, align 8, !tbaa !13
  store i8 0, ptr %0, align 8, !tbaa !8
  %19 = load ptr, ptr @OSFlagFreeList, align 8, !tbaa !12
  store ptr %19, ptr %9, align 8, !tbaa !14
  %20 = getelementptr inbounds i8, ptr %0, i64 16
  store i16 0, ptr %20, align 8, !tbaa !15
  store ptr %0, ptr @OSFlagFreeList, align 8, !tbaa !12
  tail call void @OS_CPU_SR_Restore(i32 noundef %8) #2
  br i1 %.not38.not, label %26, label %21

21:                                               ; preds = %._crit_edge
  tail call void @OS_Sched() #2
  br label %26

.critedge:                                        ; preds = %.preheader
  %22 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr @.str, ptr %22, align 8, !tbaa !13
  store i8 0, ptr %0, align 8, !tbaa !8
  %23 = load ptr, ptr @OSFlagFreeList, align 8, !tbaa !12
  store ptr %23, ptr %9, align 8, !tbaa !14
  %24 = getelementptr inbounds i8, ptr %0, i64 16
  store i16 0, ptr %24, align 8, !tbaa !15
  store ptr %0, ptr @OSFlagFreeList, align 8, !tbaa !12
  tail call void @OS_CPU_SR_Restore(i32 noundef %8) #2
  br label %26

25:                                               ; preds = %7
  tail call void @OS_CPU_SR_Restore(i32 noundef %8) #2
  br label %26

26:                                               ; preds = %._crit_edge, %21, %.critedge, %5, %3, %25, %16, %12
  %.sink = phi i8 [ 7, %25 ], [ 73, %16 ], [ 0, %12 ], [ 15, %3 ], [ 1, %5 ], [ 0, %.critedge ], [ 0, %21 ], [ 0, %._crit_edge ]
  %.036 = phi ptr [ %0, %25 ], [ %0, %16 ], [ null, %12 ], [ %0, %3 ], [ %0, %5 ], [ null, %.critedge ], [ null, %21 ], [ null, %._crit_edge ]
  store i8 %.sink, ptr %2, align 1, !tbaa !5
  ret ptr %.036
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @OS_FlagTaskRdy(ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

declare void @OS_Sched() local_unnamed_addr #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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !6, i64 0}
!9 = !{!"os_flag_grp", !6, i64 0, !10, i64 8, !11, i64 16, !10, i64 24}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!10, !10, i64 0}
!13 = !{!9, !10, i64 24}
!14 = !{!9, !10, i64 8}
!15 = !{!9, !11, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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