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

; 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_event = type { i8, ptr, i16, i8, [8 x i8] }
%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] }

@OSIntNesting = external global i8, align 1
@OSTCBCur = external global ptr, align 8
@OSTCBPrioTbl = external global [64 x ptr], align 16

; 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
declare hidden void @OSMutex_RdyAtPrio(ptr noundef, i8 noundef zeroext) #2

declare zeroext i8 @OS_EventTaskRdy(ptr noundef, ptr noundef, i8 noundef zeroext, i8 noundef zeroext) #1

declare void @OS_Sched() #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @OSMutexPost(ptr noundef %0) #2 {
  %2 = alloca i8, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  %8 = load i8, ptr @OSIntNesting, align 1, !tbaa !11
  %9 = zext i8 %8 to i32
  %10 = icmp ugt i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store i8 5, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %102

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.os_event, ptr %13, i32 0, i32 0
  %15 = load i8, ptr %14, align 8, !tbaa !12
  %16 = zext i8 %15 to i32
  %17 = icmp ne i32 %16, 4
  br i1 %17, label %18, label %19

18:                                               ; preds = %12
  store i8 1, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %102

19:                                               ; preds = %12
  %20 = call i32 @OS_CPU_SR_Save()
  store i32 %20, ptr %6, align 4, !tbaa !9
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.os_event, ptr %21, i32 0, i32 2
  %23 = load i16, ptr %22, align 8, !tbaa !15
  %24 = zext i16 %23 to i32
  %25 = ashr i32 %24, 8
  %26 = trunc i32 %25 to i8
  store i8 %26, ptr %4, align 1, !tbaa !11
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.os_event, ptr %27, i32 0, i32 2
  %29 = load i16, ptr %28, align 8, !tbaa !15
  %30 = zext i16 %29 to i32
  %31 = and i32 %30, 255
  %32 = trunc i32 %31 to i8
  store i8 %32, ptr %5, align 1, !tbaa !11
  %33 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.os_event, ptr %34, i32 0, i32 1
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  %37 = icmp ne ptr %33, %36
  br i1 %37, label %38, label %40

38:                                               ; preds = %19
  %39 = load i32, ptr %6, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %39)
  store i8 100, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %102

40:                                               ; preds = %19
  %41 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.os_tcb, ptr %41, i32 0, i32 15
  %43 = load i8, ptr %42, align 2, !tbaa !17
  %44 = zext i8 %43 to i32
  %45 = load i8, ptr %4, align 1, !tbaa !11
  %46 = zext i8 %45 to i32
  %47 = icmp eq i32 %44, %46
  br i1 %47, label %48, label %51

48:                                               ; preds = %40
  %49 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %50 = load i8, ptr %5, align 1, !tbaa !11
  call void @OSMutex_RdyAtPrio(ptr noundef %49, i8 noundef zeroext %50)
  br label %51

51:                                               ; preds = %48, %40
  %52 = load i8, ptr %4, align 1, !tbaa !11
  %53 = zext i8 %52 to i64
  %54 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %53
  store ptr inttoptr (i64 1 to ptr), ptr %54, align 8, !tbaa !5
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.os_event, ptr %55, i32 0, i32 3
  %57 = load i8, ptr %56, align 2, !tbaa !19
  %58 = zext i8 %57 to i32
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %92

60:                                               ; preds = %51
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = call zeroext i8 @OS_EventTaskRdy(ptr noundef %61, ptr noundef null, i8 noundef zeroext 16, i8 noundef zeroext 0)
  store i8 %62, ptr %5, align 1, !tbaa !11
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.os_event, ptr %63, i32 0, i32 2
  %65 = load i16, ptr %64, align 8, !tbaa !15
  %66 = zext i16 %65 to i32
  %67 = and i32 %66, 65280
  %68 = trunc i32 %67 to i16
  store i16 %68, ptr %64, align 8, !tbaa !15
  %69 = load i8, ptr %5, align 1, !tbaa !11
  %70 = zext i8 %69 to i32
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.os_event, ptr %71, i32 0, i32 2
  %73 = load i16, ptr %72, align 8, !tbaa !15
  %74 = zext i16 %73 to i32
  %75 = or i32 %74, %70
  %76 = trunc i32 %75 to i16
  store i16 %76, ptr %72, align 8, !tbaa !15
  %77 = load i8, ptr %5, align 1, !tbaa !11
  %78 = zext i8 %77 to i64
  %79 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %78
  %80 = load ptr, ptr %79, align 8, !tbaa !5
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.os_event, ptr %81, i32 0, i32 1
  store ptr %80, ptr %82, align 8, !tbaa !16
  %83 = load i8, ptr %5, align 1, !tbaa !11
  %84 = zext i8 %83 to i32
  %85 = load i8, ptr %4, align 1, !tbaa !11
  %86 = zext i8 %85 to i32
  %87 = icmp sle i32 %84, %86
  br i1 %87, label %88, label %90

88:                                               ; preds = %60
  %89 = load i32, ptr %6, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %89)
  call void @OS_Sched()
  store i8 120, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %102

90:                                               ; preds = %60
  %91 = load i32, ptr %6, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %91)
  call void @OS_Sched()
  store i8 0, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %102

92:                                               ; preds = %51
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.os_event, ptr %93, i32 0, i32 2
  %95 = load i16, ptr %94, align 8, !tbaa !15
  %96 = zext i16 %95 to i32
  %97 = or i32 %96, 255
  %98 = trunc i32 %97 to i16
  store i16 %98, ptr %94, align 8, !tbaa !15
  %99 = load ptr, ptr %3, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.os_event, ptr %99, i32 0, i32 1
  store ptr null, ptr %100, align 8, !tbaa !16
  %101 = load i32, ptr %6, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %101)
  store i8 0, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %102

102:                                              ; preds = %92, %90, %88, %38, %18, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  %103 = load i8, ptr %2, align 1
  ret i8 %103
}

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


; 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
@OSTCBCur = external local_unnamed_addr global ptr, align 8
@OSTCBPrioTbl = external local_unnamed_addr global [64 x ptr], align 16

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
declare hidden void @OSMutex_RdyAtPrio(ptr noundef, i8 noundef zeroext) local_unnamed_addr #1

declare zeroext i8 @OS_EventTaskRdy(ptr noundef, ptr noundef, i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #0

declare void @OS_Sched() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 121) i8 @OSMutexPost(ptr noundef %0) local_unnamed_addr #1 {
  %2 = load i8, ptr @OSIntNesting, align 1, !tbaa !5
  %.not = icmp eq i8 %2, 0
  br i1 %.not, label %3, label %38

3:                                                ; preds = %1
  %4 = load i8, ptr %0, align 8, !tbaa !8
  %.not23 = icmp eq i8 %4, 4
  br i1 %.not23, label %5, label %38

5:                                                ; preds = %3
  %6 = tail call i32 @OS_CPU_SR_Save() #2
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load i16, ptr %7, align 8, !tbaa !12
  %9 = lshr i16 %8, 8
  %10 = zext nneg i16 %9 to i64
  %11 = trunc i16 %8 to i8
  %12 = load ptr, ptr @OSTCBCur, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %.not24 = icmp eq ptr %12, %14
  br i1 %.not24, label %16, label %15

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

16:                                               ; preds = %5
  %17 = getelementptr inbounds i8, ptr %12, i64 82
  %18 = load i8, ptr %17, align 2, !tbaa !15
  %19 = zext i8 %18 to i16
  %20 = icmp eq i16 %9, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %16
  tail call void @OSMutex_RdyAtPrio(ptr noundef nonnull %12, i8 noundef zeroext %11)
  br label %22

22:                                               ; preds = %21, %16
  %23 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %10
  store ptr inttoptr (i64 1 to ptr), ptr %23, align 8, !tbaa !13
  %24 = getelementptr inbounds i8, ptr %0, i64 18
  %25 = load i8, ptr %24, align 2, !tbaa !18
  %.not25 = icmp eq i8 %25, 0
  br i1 %.not25, label %35, label %26

26:                                               ; preds = %22
  %27 = tail call zeroext i8 @OS_EventTaskRdy(ptr noundef nonnull %0, ptr noundef null, i8 noundef zeroext 16, i8 noundef zeroext 0) #2
  %28 = load i16, ptr %7, align 8, !tbaa !12
  %29 = and i16 %28, -256
  %30 = zext i8 %27 to i16
  %31 = or disjoint i16 %29, %30
  store i16 %31, ptr %7, align 8, !tbaa !12
  %32 = zext i8 %27 to i64
  %33 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %32
  %34 = load ptr, ptr %33, align 8, !tbaa !13
  store ptr %34, ptr %13, align 8, !tbaa !14
  %.not26 = icmp ult i16 %9, %30
  tail call void @OS_CPU_SR_Restore(i32 noundef %6) #2
  tail call void @OS_Sched() #2
  %. = select i1 %.not26, i8 0, i8 120
  br label %38

35:                                               ; preds = %22
  %36 = load i16, ptr %7, align 8, !tbaa !12
  %37 = or i16 %36, 255
  store i16 %37, ptr %7, align 8, !tbaa !12
  store ptr null, ptr %13, align 8, !tbaa !14
  tail call void @OS_CPU_SR_Restore(i32 noundef %6) #2
  br label %38

38:                                               ; preds = %26, %3, %1, %35, %15
  %.0 = phi i8 [ 100, %15 ], [ 0, %35 ], [ 5, %1 ], [ 1, %3 ], [ %., %26 ]
  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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !6, i64 0}
!9 = !{!"os_event", !6, i64 0, !10, i64 8, !11, i64 16, !6, i64 18, !6, i64 19}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!9, !11, i64 16}
!13 = !{!10, !10, i64 0}
!14 = !{!9, !10, i64 8}
!15 = !{!16, !6, i64 82}
!16 = !{!"os_tcb", !10, i64 0, !10, i64 8, !10, i64 16, !17, i64 24, !11, i64 28, !11, i64 30, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !11, i64 72, !17, 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, !17, i64 88, !17, i64 92, !17, i64 96, !10, i64 104, !17, i64 112, !10, i64 120, !6, i64 128}
!17 = !{!"int", !6, i64 0}
!18 = !{!9, !6, i64 18}


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