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

; 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
@OSLockNesting = external global i8, align 1
@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

declare void @OS_Sched() #1

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

14:                                               ; preds = %3
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 1, ptr %15, align 1, !tbaa !14
  store i32 1, ptr %8, align 4
  br label %78

16:                                               ; preds = %3
  %17 = load i8, ptr @OSIntNesting, align 1, !tbaa !14
  %18 = zext i8 %17 to i32
  %19 = icmp ugt i32 %18, 0
  br i1 %19, label %20, label %22

20:                                               ; preds = %16
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 2, ptr %21, align 1, !tbaa !14
  store i32 1, ptr %8, align 4
  br label %78

22:                                               ; preds = %16
  %23 = load i8, ptr @OSLockNesting, align 1, !tbaa !14
  %24 = zext i8 %23 to i32
  %25 = icmp ugt i32 %24, 0
  br i1 %25, label %26, label %28

26:                                               ; preds = %22
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 13, ptr %27, align 1, !tbaa !14
  store i32 1, ptr %8, align 4
  br label %78

28:                                               ; preds = %22
  %29 = call i32 @OS_CPU_SR_Save()
  store i32 %29, ptr %7, align 4, !tbaa !9
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.os_event, ptr %30, i32 0, i32 2
  %32 = load i16, ptr %31, align 8, !tbaa !15
  %33 = zext i16 %32 to i32
  %34 = icmp ugt i32 %33, 0
  br i1 %34, label %35, label %42

35:                                               ; preds = %28
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.os_event, ptr %36, i32 0, i32 2
  %38 = load i16, ptr %37, align 8, !tbaa !15
  %39 = add i16 %38, -1
  store i16 %39, ptr %37, align 8, !tbaa !15
  %40 = load i32, ptr %7, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %40)
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %41, align 1, !tbaa !14
  store i32 1, ptr %8, align 4
  br label %78

42:                                               ; preds = %28
  %43 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.os_tcb, ptr %43, i32 0, i32 13
  %45 = load i8, ptr %44, align 8, !tbaa !16
  %46 = zext i8 %45 to i32
  %47 = or i32 %46, 1
  %48 = trunc i32 %47 to i8
  store i8 %48, ptr %44, align 8, !tbaa !16
  %49 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.os_tcb, ptr %49, i32 0, i32 14
  store i8 0, ptr %50, align 1, !tbaa !18
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.os_tcb, ptr %52, i32 0, i32 12
  store i32 %51, ptr %53, align 4, !tbaa !19
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  call void @OS_EventTaskWait(ptr noundef %54)
  %55 = load i32, ptr %7, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %55)
  call void @OS_Sched()
  %56 = call i32 @OS_CPU_SR_Save()
  store i32 %56, ptr %7, align 4, !tbaa !9
  %57 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.os_tcb, ptr %57, i32 0, i32 14
  %59 = load i8, ptr %58, align 1, !tbaa !18
  %60 = zext i8 %59 to i32
  switch i32 %60, label %66 [
    i32 0, label %61
    i32 2, label %63
    i32 1, label %65
  ]

61:                                               ; preds = %42
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %62, align 1, !tbaa !14
  br label %70

63:                                               ; preds = %42
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 14, ptr %64, align 1, !tbaa !14
  br label %70

65:                                               ; preds = %42
  br label %66

66:                                               ; preds = %65, %42
  %67 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  call void @OS_EventTaskRemove(ptr noundef %67, ptr noundef %68)
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 10, ptr %69, align 1, !tbaa !14
  br label %70

70:                                               ; preds = %66, %63, %61
  %71 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.os_tcb, ptr %71, i32 0, i32 13
  store i8 0, ptr %72, align 8, !tbaa !16
  %73 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.os_tcb, ptr %73, i32 0, i32 14
  store i8 0, ptr %74, align 1, !tbaa !18
  %75 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.os_tcb, ptr %75, i32 0, i32 8
  store ptr null, ptr %76, align 8, !tbaa !20
  %77 = load i32, ptr %7, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %77)
  store i32 0, ptr %8, align 4
  br label %78

78:                                               ; preds = %70, %35, %26, %20, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %79 = load i32, ptr %8, align 4
  switch i32 %79, label %81 [
    i32 0, label %80
    i32 1, label %80
  ]

80:                                               ; preds = %78, %78
  ret void

81:                                               ; preds = %78
  unreachable
}

declare void @OS_EventTaskWait(ptr noundef) #1

declare void @OS_EventTaskRemove(ptr noundef, ptr noundef) #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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"os_event", !7, i64 0, !6, i64 8, !13, i64 16, !7, i64 18, !7, i64 19}
!13 = !{!"short", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!12, !13, i64 16}
!16 = !{!17, !7, i64 80}
!17 = !{!"os_tcb", !6, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !13, i64 28, !13, i64 30, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !13, 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}
!18 = !{!17, !7, i64 81}
!19 = !{!17, !10, i64 76}
!20 = !{!17, !6, i64 48}


; 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
@OSLockNesting = external local_unnamed_addr global i8, align 1
@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

declare void @OS_Sched() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @OSSemPend(ptr noundef %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #1 {
  %4 = load i8, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i8 %4, 3
  br i1 %.not, label %6, label %5

5:                                                ; preds = %3
  store i8 1, ptr %2, align 1, !tbaa !11
  br label %36

6:                                                ; preds = %3
  %7 = load i8, ptr @OSIntNesting, align 1, !tbaa !11
  %.not15 = icmp eq i8 %7, 0
  br i1 %.not15, label %9, label %8

8:                                                ; preds = %6
  store i8 2, ptr %2, align 1, !tbaa !11
  br label %36

9:                                                ; preds = %6
  %10 = load i8, ptr @OSLockNesting, align 1, !tbaa !11
  %.not16 = icmp eq i8 %10, 0
  br i1 %.not16, label %12, label %11

11:                                               ; preds = %9
  store i8 13, ptr %2, align 1, !tbaa !11
  br label %36

12:                                               ; preds = %9
  %13 = tail call i32 @OS_CPU_SR_Save() #2
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load i16, ptr %14, align 8, !tbaa !12
  %.not17 = icmp eq i16 %15, 0
  br i1 %.not17, label %18, label %16

16:                                               ; preds = %12
  %17 = add i16 %15, -1
  store i16 %17, ptr %14, align 8, !tbaa !12
  tail call void @OS_CPU_SR_Restore(i32 noundef %13) #2
  store i8 0, ptr %2, align 1, !tbaa !11
  br label %36

18:                                               ; preds = %12
  %19 = load ptr, ptr @OSTCBCur, align 8, !tbaa !13
  %20 = getelementptr inbounds i8, ptr %19, i64 80
  %21 = load i8, ptr %20, align 8, !tbaa !14
  %22 = or i8 %21, 1
  store i8 %22, ptr %20, align 8, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %19, i64 81
  store i8 0, ptr %23, align 1, !tbaa !17
  %24 = getelementptr inbounds i8, ptr %19, i64 76
  store i32 %1, ptr %24, align 4, !tbaa !18
  tail call void @OS_EventTaskWait(ptr noundef nonnull %0) #2
  tail call void @OS_CPU_SR_Restore(i32 noundef %13) #2
  tail call void @OS_Sched() #2
  %25 = tail call i32 @OS_CPU_SR_Save() #2
  %26 = load ptr, ptr @OSTCBCur, align 8, !tbaa !13
  %27 = getelementptr inbounds i8, ptr %26, i64 81
  %28 = load i8, ptr %27, align 1, !tbaa !17
  switch i8 %28, label %30 [
    i8 0, label %31
    i8 2, label %29
  ]

29:                                               ; preds = %18
  br label %31

30:                                               ; preds = %18
  tail call void @OS_EventTaskRemove(ptr noundef nonnull %26, ptr noundef nonnull %0) #2
  br label %31

31:                                               ; preds = %18, %30, %29
  %.sink = phi i8 [ 10, %30 ], [ 14, %29 ], [ %28, %18 ]
  store i8 %.sink, ptr %2, align 1, !tbaa !11
  %32 = load ptr, ptr @OSTCBCur, align 8, !tbaa !13
  %33 = getelementptr inbounds i8, ptr %32, i64 80
  store i8 0, ptr %33, align 8, !tbaa !14
  %34 = getelementptr inbounds i8, ptr %32, i64 81
  store i8 0, ptr %34, align 1, !tbaa !17
  %35 = getelementptr inbounds i8, ptr %32, i64 48
  store ptr null, ptr %35, align 8, !tbaa !19
  tail call void @OS_CPU_SR_Restore(i32 noundef %25) #2
  br label %36

36:                                               ; preds = %31, %16, %11, %8, %5
  ret void
}

declare void @OS_EventTaskWait(ptr noundef) local_unnamed_addr #0

declare void @OS_EventTaskRemove(ptr noundef, ptr noundef) 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, !7, i64 0}
!6 = !{!"os_event", !7, i64 0, !9, i64 8, !10, i64 16, !7, i64 18, !7, i64 19}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!6, !10, i64 16}
!13 = !{!9, !9, i64 0}
!14 = !{!15, !7, i64 80}
!15 = !{!"os_tcb", !9, i64 0, !9, i64 8, !9, i64 16, !16, i64 24, !10, i64 28, !10, i64 30, !9, i64 32, !9, i64 40, !9, i64 48, !9, i64 56, !9, i64 64, !10, i64 72, !16, 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, !16, i64 88, !16, i64 92, !16, i64 96, !9, i64 104, !16, i64 112, !9, i64 120, !7, i64 128}
!16 = !{!"int", !7, i64 0}
!17 = !{!15, !7, i64 81}
!18 = !{!15, !16, i64 76}
!19 = !{!15, !9, i64 48}


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