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

; 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_q = type { ptr, ptr, ptr, ptr, ptr, i16, i16 }
%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 ptr @OSQPend(ptr noundef %0, i32 noundef %1, ptr noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.os_event, ptr %12, i32 0, i32 0
  %14 = load i8, ptr %13, align 8, !tbaa !11
  %15 = zext i8 %14 to i32
  %16 = icmp ne i32 %15, 2
  br i1 %16, label %17, label %19

17:                                               ; preds = %3
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 1, ptr %18, align 1, !tbaa !14
  store ptr null, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %110

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

23:                                               ; preds = %19
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 2, ptr %24, align 1, !tbaa !14
  store ptr null, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %110

25:                                               ; preds = %19
  %26 = load i8, ptr @OSLockNesting, align 1, !tbaa !14
  %27 = zext i8 %26 to i32
  %28 = icmp ugt i32 %27, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %25
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 13, ptr %30, align 1, !tbaa !14
  store ptr null, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %110

31:                                               ; preds = %25
  %32 = call i32 @OS_CPU_SR_Save()
  store i32 %32, ptr %10, align 4, !tbaa !9
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.os_event, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !15
  store ptr %35, ptr %9, align 8, !tbaa !5
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.os_q, ptr %36, i32 0, i32 6
  %38 = load i16, ptr %37, align 2, !tbaa !16
  %39 = zext i16 %38 to i32
  %40 = icmp ugt i32 %39, 0
  br i1 %40, label %41, label %68

41:                                               ; preds = %31
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.os_q, ptr %42, i32 0, i32 4
  %44 = load ptr, ptr %43, align 8, !tbaa !18
  %45 = getelementptr inbounds ptr, ptr %44, i32 1
  store ptr %45, ptr %43, align 8, !tbaa !18
  %46 = load ptr, ptr %44, align 8, !tbaa !5
  store ptr %46, ptr %8, align 8, !tbaa !5
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.os_q, ptr %47, i32 0, i32 6
  %49 = load i16, ptr %48, align 2, !tbaa !16
  %50 = add i16 %49, -1
  store i16 %50, ptr %48, align 2, !tbaa !16
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.os_q, ptr %51, i32 0, i32 4
  %53 = load ptr, ptr %52, align 8, !tbaa !18
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.os_q, ptr %54, i32 0, i32 2
  %56 = load ptr, ptr %55, align 8, !tbaa !19
  %57 = icmp eq ptr %53, %56
  br i1 %57, label %58, label %64

58:                                               ; preds = %41
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.os_q, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !20
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.os_q, ptr %62, i32 0, i32 4
  store ptr %61, ptr %63, align 8, !tbaa !18
  br label %64

64:                                               ; preds = %58, %41
  %65 = load i32, ptr %10, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %65)
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %66, align 1, !tbaa !14
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %67, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %110

68:                                               ; preds = %31
  %69 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.os_tcb, ptr %69, i32 0, i32 13
  %71 = load i8, ptr %70, align 8, !tbaa !21
  %72 = zext i8 %71 to i32
  %73 = or i32 %72, 4
  %74 = trunc i32 %73 to i8
  store i8 %74, ptr %70, align 8, !tbaa !21
  %75 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.os_tcb, ptr %75, i32 0, i32 14
  store i8 0, ptr %76, align 1, !tbaa !23
  %77 = load i32, ptr %6, align 4, !tbaa !9
  %78 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.os_tcb, ptr %78, i32 0, i32 12
  store i32 %77, ptr %79, align 4, !tbaa !24
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  call void @OS_EventTaskWait(ptr noundef %80)
  %81 = load i32, ptr %10, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %81)
  call void @OS_Sched()
  %82 = call i32 @OS_CPU_SR_Save()
  store i32 %82, ptr %10, align 4, !tbaa !9
  %83 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.os_tcb, ptr %83, i32 0, i32 14
  %85 = load i8, ptr %84, align 1, !tbaa !23
  %86 = zext i8 %85 to i32
  switch i32 %86, label %95 [
    i32 0, label %87
    i32 2, label %92
    i32 1, label %94
  ]

87:                                               ; preds = %68
  %88 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.os_tcb, ptr %88, i32 0, i32 9
  %90 = load ptr, ptr %89, align 8, !tbaa !25
  store ptr %90, ptr %8, align 8, !tbaa !5
  %91 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %91, align 1, !tbaa !14
  br label %99

92:                                               ; preds = %68
  store ptr null, ptr %8, align 8, !tbaa !5
  %93 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 14, ptr %93, align 1, !tbaa !14
  br label %99

94:                                               ; preds = %68
  br label %95

95:                                               ; preds = %94, %68
  %96 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %97 = load ptr, ptr %5, align 8, !tbaa !5
  call void @OS_EventTaskRemove(ptr noundef %96, ptr noundef %97)
  store ptr null, ptr %8, align 8, !tbaa !5
  %98 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 10, ptr %98, align 1, !tbaa !14
  br label %99

99:                                               ; preds = %95, %92, %87
  %100 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.os_tcb, ptr %100, i32 0, i32 13
  store i8 0, ptr %101, align 8, !tbaa !21
  %102 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.os_tcb, ptr %102, i32 0, i32 14
  store i8 0, ptr %103, align 1, !tbaa !23
  %104 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.os_tcb, ptr %104, i32 0, i32 8
  store ptr null, ptr %105, align 8, !tbaa !26
  %106 = load ptr, ptr @OSTCBCur, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.os_tcb, ptr %106, i32 0, i32 9
  store ptr null, ptr %107, align 8, !tbaa !25
  %108 = load i32, ptr %10, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %108)
  %109 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %109, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %110

110:                                              ; preds = %99, %64, %29, %23, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %111 = load ptr, ptr %4, align 8
  ret ptr %111
}

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, !6, i64 8}
!16 = !{!17, !13, i64 42}
!17 = !{!"os_q", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !13, i64 40, !13, i64 42}
!18 = !{!17, !6, i64 32}
!19 = !{!17, !6, i64 16}
!20 = !{!17, !6, i64 8}
!21 = !{!22, !7, i64 80}
!22 = !{!"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}
!23 = !{!22, !7, i64 81}
!24 = !{!22, !10, i64 76}
!25 = !{!22, !6, i64 56}
!26 = !{!22, !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 ptr @OSQPend(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, 2
  br i1 %.not, label %6, label %5

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

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

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

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

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

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

18:                                               ; preds = %12
  %19 = getelementptr inbounds i8, ptr %15, i64 32
  %20 = load ptr, ptr %19, align 8, !tbaa !15
  %21 = getelementptr inbounds i8, ptr %20, i64 8
  store ptr %21, ptr %19, align 8, !tbaa !15
  %22 = load ptr, ptr %20, align 8, !tbaa !16
  %23 = add i16 %17, -1
  store i16 %23, ptr %16, align 2, !tbaa !13
  %24 = getelementptr inbounds i8, ptr %15, i64 16
  %25 = load ptr, ptr %24, align 8, !tbaa !17
  %26 = icmp eq ptr %21, %25
  br i1 %26, label %27, label %30

27:                                               ; preds = %18
  %28 = getelementptr inbounds i8, ptr %15, i64 8
  %29 = load ptr, ptr %28, align 8, !tbaa !18
  store ptr %29, ptr %19, align 8, !tbaa !15
  br label %30

30:                                               ; preds = %27, %18
  tail call void @OS_CPU_SR_Restore(i32 noundef %13) #3
  store i8 0, ptr %2, align 1, !tbaa !11
  br label %51

31:                                               ; preds = %12
  %32 = load ptr, ptr @OSTCBCur, align 8, !tbaa !16
  %33 = getelementptr inbounds i8, ptr %32, i64 80
  %34 = load i8, ptr %33, align 8, !tbaa !19
  %35 = or i8 %34, 4
  store i8 %35, ptr %33, align 8, !tbaa !19
  %36 = getelementptr inbounds i8, ptr %32, i64 81
  store i8 0, ptr %36, align 1, !tbaa !22
  %37 = getelementptr inbounds i8, ptr %32, i64 76
  store i32 %1, ptr %37, align 4, !tbaa !23
  tail call void @OS_EventTaskWait(ptr noundef nonnull %0) #3
  tail call void @OS_CPU_SR_Restore(i32 noundef %13) #3
  tail call void @OS_Sched() #3
  %38 = tail call i32 @OS_CPU_SR_Save() #3
  %39 = load ptr, ptr @OSTCBCur, align 8, !tbaa !16
  %40 = getelementptr inbounds i8, ptr %39, i64 81
  %41 = load i8, ptr %40, align 1, !tbaa !22
  switch i8 %41, label %45 [
    i8 0, label %42
    i8 2, label %46
  ]

42:                                               ; preds = %31
  %43 = getelementptr inbounds i8, ptr %39, i64 56
  %44 = load ptr, ptr %43, align 8, !tbaa !24
  br label %46

45:                                               ; preds = %31
  tail call void @OS_EventTaskRemove(ptr noundef nonnull %39, ptr noundef nonnull %0) #3
  br label %46

46:                                               ; preds = %31, %45, %42
  %.sink = phi i8 [ 10, %45 ], [ 0, %42 ], [ 14, %31 ]
  %.024 = phi ptr [ null, %45 ], [ %44, %42 ], [ null, %31 ]
  store i8 %.sink, ptr %2, align 1, !tbaa !11
  %47 = load ptr, ptr @OSTCBCur, align 8, !tbaa !16
  %48 = getelementptr inbounds i8, ptr %47, i64 80
  store i8 0, ptr %48, align 8, !tbaa !19
  %49 = getelementptr inbounds i8, ptr %47, i64 81
  store i8 0, ptr %49, align 1, !tbaa !22
  %50 = getelementptr inbounds i8, ptr %47, i64 48
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %50, i8 0, i64 16, i1 false)
  tail call void @OS_CPU_SR_Restore(i32 noundef %38) #3
  br label %51

51:                                               ; preds = %46, %30, %11, %8, %5
  %.0 = phi ptr [ null, %5 ], [ null, %8 ], [ null, %11 ], [ %22, %30 ], [ %.024, %46 ]
  ret ptr %.0
}

declare void @OS_EventTaskWait(ptr noundef) local_unnamed_addr #0

declare void @OS_EventTaskRemove(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !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, !9, i64 8}
!13 = !{!14, !10, i64 42}
!14 = !{!"os_q", !9, i64 0, !9, i64 8, !9, i64 16, !9, i64 24, !9, i64 32, !10, i64 40, !10, i64 42}
!15 = !{!14, !9, i64 32}
!16 = !{!9, !9, i64 0}
!17 = !{!14, !9, i64 16}
!18 = !{!14, !9, i64 8}
!19 = !{!20, !7, i64 80}
!20 = !{!"os_tcb", !9, i64 0, !9, i64 8, !9, i64 16, !21, 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, !21, 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, !21, i64 88, !21, i64 92, !21, i64 96, !9, i64 104, !21, i64 112, !9, i64 120, !7, i64 128}
!21 = !{!"int", !7, i64 0}
!22 = !{!20, !7, i64 81}
!23 = !{!20, !21, i64 76}
!24 = !{!20, !9, i64 56}


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