; 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 }

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

15:                                               ; preds = %2
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  store i8 1, ptr %16, align 1, !tbaa !14
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %57

17:                                               ; preds = %2
  %18 = call i32 @OS_CPU_SR_Save()
  store i32 %18, ptr %8, align 4, !tbaa !9
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.os_event, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  store ptr %21, ptr %7, align 8, !tbaa !5
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.os_q, ptr %22, i32 0, i32 6
  %24 = load i16, ptr %23, align 2, !tbaa !16
  %25 = zext i16 %24 to i32
  %26 = icmp ugt i32 %25, 0
  br i1 %26, label %27, label %52

27:                                               ; preds = %17
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.os_q, ptr %28, i32 0, i32 4
  %30 = load ptr, ptr %29, align 8, !tbaa !18
  %31 = getelementptr inbounds ptr, ptr %30, i32 1
  store ptr %31, ptr %29, align 8, !tbaa !18
  %32 = load ptr, ptr %30, align 8, !tbaa !5
  store ptr %32, ptr %6, align 8, !tbaa !5
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.os_q, ptr %33, i32 0, i32 6
  %35 = load i16, ptr %34, align 2, !tbaa !16
  %36 = add i16 %35, -1
  store i16 %36, ptr %34, align 2, !tbaa !16
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.os_q, ptr %37, i32 0, i32 4
  %39 = load ptr, ptr %38, align 8, !tbaa !18
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.os_q, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !19
  %43 = icmp eq ptr %39, %42
  br i1 %43, label %44, label %50

44:                                               ; preds = %27
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.os_q, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !20
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.os_q, ptr %48, i32 0, i32 4
  store ptr %47, ptr %49, align 8, !tbaa !18
  br label %50

50:                                               ; preds = %44, %27
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  store i8 0, ptr %51, align 1, !tbaa !14
  br label %54

52:                                               ; preds = %17
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  store i8 31, ptr %53, align 1, !tbaa !14
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %54

54:                                               ; preds = %52, %50
  %55 = load i32, ptr %8, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %55)
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %56, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %57

57:                                               ; preds = %54, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %58 = load ptr, ptr %3, align 8
  ret ptr %58
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare i32 @OS_CPU_SR_Save() #2

declare void @OS_CPU_SR_Restore(i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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}


; 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"

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

4:                                                ; preds = %2
  store i8 1, ptr %1, align 1, !tbaa !11
  br label %24

5:                                                ; preds = %2
  %6 = tail call i32 @OS_CPU_SR_Save() #2
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  %9 = getelementptr inbounds i8, ptr %8, i64 42
  %10 = load i16, ptr %9, align 2, !tbaa !13
  %.not15 = icmp eq i16 %10, 0
  br i1 %.not15, label %23, label %11

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %8, i64 32
  %13 = load ptr, ptr %12, align 8, !tbaa !15
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  store ptr %14, ptr %12, align 8, !tbaa !15
  %15 = load ptr, ptr %13, align 8, !tbaa !16
  %16 = add i16 %10, -1
  store i16 %16, ptr %9, align 2, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %8, i64 16
  %18 = load ptr, ptr %17, align 8, !tbaa !17
  %19 = icmp eq ptr %14, %18
  br i1 %19, label %20, label %23

20:                                               ; preds = %11
  %21 = getelementptr inbounds i8, ptr %8, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !18
  store ptr %22, ptr %12, align 8, !tbaa !15
  br label %23

23:                                               ; preds = %5, %11, %20
  %storemerge = phi i8 [ 0, %20 ], [ 0, %11 ], [ 31, %5 ]
  %.014 = phi ptr [ %15, %20 ], [ %15, %11 ], [ null, %5 ]
  store i8 %storemerge, ptr %1, align 1, !tbaa !11
  tail call void @OS_CPU_SR_Restore(i32 noundef %6) #2
  br label %24

24:                                               ; preds = %23, %4
  %.0 = phi ptr [ null, %4 ], [ %.014, %23 ]
  ret ptr %.0
}

declare i32 @OS_CPU_SR_Save() local_unnamed_addr #1

declare void @OS_CPU_SR_Restore(i32 noundef) local_unnamed_addr #1

attributes #0 = { 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 #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 }

!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}


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