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

; 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 zeroext i8 @OSQQuery(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = 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 1, ptr %7) #3
  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 %4, 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 %18

17:                                               ; preds = %2
  store i8 1, ptr %3, align 1
  store i32 1, ptr %11, align 4
  br label %75

18:                                               ; preds = %2
  %19 = call i32 @OS_CPU_SR_Save()
  store i32 %19, ptr %10, align 4, !tbaa !9
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.os_event, ptr %20, i32 0, i32 3
  %22 = load i8, ptr %21, align 2, !tbaa !14
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.os_q_data, ptr %23, i32 0, i32 4
  store i8 %22, ptr %24, align 4, !tbaa !15
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.os_event, ptr %25, i32 0, i32 4
  %27 = getelementptr inbounds [8 x i8], ptr %26, i64 0, i64 0
  store ptr %27, ptr %8, align 8, !tbaa !5
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.os_q_data, ptr %28, i32 0, i32 3
  %30 = getelementptr inbounds [8 x i8], ptr %29, i64 0, i64 0
  store ptr %30, ptr %9, align 8, !tbaa !5
  store i8 0, ptr %7, align 1, !tbaa !17
  br label %31

31:                                               ; preds = %41, %18
  %32 = load i8, ptr %7, align 1, !tbaa !17
  %33 = zext i8 %32 to i32
  %34 = icmp ult i32 %33, 8
  br i1 %34, label %35, label %44

35:                                               ; preds = %31
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i32 1
  store ptr %37, ptr %8, align 8, !tbaa !5
  %38 = load i8, ptr %36, align 1, !tbaa !17
  %39 = load ptr, ptr %9, align 8, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %39, i32 1
  store ptr %40, ptr %9, align 8, !tbaa !5
  store i8 %38, ptr %39, align 1, !tbaa !17
  br label %41

41:                                               ; preds = %35
  %42 = load i8, ptr %7, align 1, !tbaa !17
  %43 = add i8 %42, 1
  store i8 %43, ptr %7, align 1, !tbaa !17
  br label %31, !llvm.loop !18

44:                                               ; preds = %31
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.os_event, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !20
  store ptr %47, ptr %6, align 8, !tbaa !5
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.os_q, ptr %48, i32 0, i32 6
  %50 = load i16, ptr %49, align 2, !tbaa !21
  %51 = zext i16 %50 to i32
  %52 = icmp ugt i32 %51, 0
  br i1 %52, label %53, label %60

53:                                               ; preds = %44
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.os_q, ptr %54, i32 0, i32 4
  %56 = load ptr, ptr %55, align 8, !tbaa !23
  %57 = load ptr, ptr %56, align 8, !tbaa !5
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.os_q_data, ptr %58, i32 0, i32 0
  store ptr %57, ptr %59, align 8, !tbaa !24
  br label %63

60:                                               ; preds = %44
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.os_q_data, ptr %61, i32 0, i32 0
  store ptr null, ptr %62, align 8, !tbaa !24
  br label %63

63:                                               ; preds = %60, %53
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.os_q, ptr %64, i32 0, i32 6
  %66 = load i16, ptr %65, align 2, !tbaa !21
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.os_q_data, ptr %67, i32 0, i32 1
  store i16 %66, ptr %68, align 8, !tbaa !25
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.os_q, ptr %69, i32 0, i32 5
  %71 = load i16, ptr %70, align 8, !tbaa !26
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.os_q_data, ptr %72, i32 0, i32 2
  store i16 %71, ptr %73, align 2, !tbaa !27
  %74 = load i32, ptr %10, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %74)
  store i8 0, ptr %3, align 1
  store i32 1, ptr %11, align 4
  br label %75

75:                                               ; preds = %63, %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
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %76 = load i8, ptr %3, align 1
  ret i8 %76
}

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 = !{!12, !7, i64 18}
!15 = !{!16, !7, i64 20}
!16 = !{!"os_q_data", !6, i64 0, !13, i64 8, !13, i64 10, !7, i64 12, !7, i64 20}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!12, !6, i64 8}
!21 = !{!22, !13, i64 42}
!22 = !{!"os_q", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !13, i64 40, !13, i64 42}
!23 = !{!22, !6, i64 32}
!24 = !{!16, !6, i64 0}
!25 = !{!16, !13, i64 8}
!26 = !{!22, !13, i64 40}
!27 = !{!16, !13, i64 10}


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

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 zeroext range(i8 0, 2) i8 @OSQQuery(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = load i8, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i8 %3, 2
  br i1 %.not, label %4, label %46

4:                                                ; preds = %2
  %5 = tail call i32 @OS_CPU_SR_Save() #2
  %6 = getelementptr inbounds i8, ptr %0, i64 18
  %7 = load i8, ptr %6, align 2, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %1, i64 20
  store i8 %7, ptr %8, align 4, !tbaa !12
  %9 = getelementptr i8, ptr %0, i64 19
  %10 = getelementptr i8, ptr %1, i64 12
  %11 = getelementptr i8, ptr %0, i64 20
  %12 = load i8, ptr %9, align 1, !tbaa !14
  %13 = getelementptr i8, ptr %1, i64 13
  store i8 %12, ptr %10, align 1, !tbaa !14
  %14 = getelementptr i8, ptr %0, i64 21
  %15 = load i8, ptr %11, align 1, !tbaa !14
  %16 = getelementptr i8, ptr %1, i64 14
  store i8 %15, ptr %13, align 1, !tbaa !14
  %17 = getelementptr i8, ptr %0, i64 22
  %18 = load i8, ptr %14, align 1, !tbaa !14
  %19 = getelementptr i8, ptr %1, i64 15
  store i8 %18, ptr %16, align 1, !tbaa !14
  %20 = getelementptr i8, ptr %0, i64 23
  %21 = load i8, ptr %17, align 1, !tbaa !14
  %22 = getelementptr i8, ptr %1, i64 16
  store i8 %21, ptr %19, align 1, !tbaa !14
  %23 = getelementptr i8, ptr %0, i64 24
  %24 = load i8, ptr %20, align 1, !tbaa !14
  %25 = getelementptr i8, ptr %1, i64 17
  store i8 %24, ptr %22, align 1, !tbaa !14
  %26 = getelementptr i8, ptr %0, i64 25
  %27 = load i8, ptr %23, align 1, !tbaa !14
  %28 = getelementptr i8, ptr %1, i64 18
  store i8 %27, ptr %25, align 1, !tbaa !14
  %29 = getelementptr i8, ptr %0, i64 26
  %30 = load i8, ptr %26, align 1, !tbaa !14
  %31 = getelementptr i8, ptr %1, i64 19
  store i8 %30, ptr %28, align 1, !tbaa !14
  %32 = load i8, ptr %29, align 1, !tbaa !14
  store i8 %32, ptr %31, align 1, !tbaa !14
  %33 = getelementptr inbounds i8, ptr %0, i64 8
  %34 = load ptr, ptr %33, align 8, !tbaa !15
  %35 = getelementptr inbounds i8, ptr %34, i64 42
  %36 = load i16, ptr %35, align 2, !tbaa !16
  %.not22 = icmp eq i16 %36, 0
  br i1 %.not22, label %41, label %37

37:                                               ; preds = %4
  %38 = getelementptr inbounds i8, ptr %34, i64 32
  %39 = load ptr, ptr %38, align 8, !tbaa !18
  %40 = load ptr, ptr %39, align 8, !tbaa !19
  br label %41

41:                                               ; preds = %4, %37
  %storemerge = phi ptr [ %40, %37 ], [ null, %4 ]
  store ptr %storemerge, ptr %1, align 8, !tbaa !20
  %42 = getelementptr inbounds i8, ptr %1, i64 8
  store i16 %36, ptr %42, align 8, !tbaa !21
  %43 = getelementptr inbounds i8, ptr %34, i64 40
  %44 = load i16, ptr %43, align 8, !tbaa !22
  %45 = getelementptr inbounds i8, ptr %1, i64 10
  store i16 %44, ptr %45, align 2, !tbaa !23
  tail call void @OS_CPU_SR_Restore(i32 noundef %5) #2
  br label %46

46:                                               ; preds = %2, %41
  %.021 = phi i8 [ 0, %41 ], [ 1, %2 ]
  ret i8 %.021
}

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 = !{!6, !7, i64 18}
!12 = !{!13, !7, i64 20}
!13 = !{!"os_q_data", !9, i64 0, !10, i64 8, !10, i64 10, !7, i64 12, !7, i64 20}
!14 = !{!7, !7, i64 0}
!15 = !{!6, !9, i64 8}
!16 = !{!17, !10, i64 42}
!17 = !{!"os_q", !9, i64 0, !9, i64 8, !9, i64 16, !9, i64 24, !9, i64 32, !10, i64 40, !10, i64 42}
!18 = !{!17, !9, i64 32}
!19 = !{!9, !9, i64 0}
!20 = !{!13, !9, i64 0}
!21 = !{!13, !10, i64 8}
!22 = !{!17, !10, i64 40}
!23 = !{!13, !10, i64 10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopIdiomRecognizePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass