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

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

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

16:                                               ; preds = %2
  store i8 1, ptr %3, align 1
  store i32 1, ptr %10, align 4
  br label %50

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

30:                                               ; preds = %40, %17
  %31 = load i8, ptr %6, align 1, !tbaa !17
  %32 = zext i8 %31 to i32
  %33 = icmp ult i32 %32, 8
  br i1 %33, label %34, label %43

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

40:                                               ; preds = %34
  %41 = load i8, ptr %6, align 1, !tbaa !17
  %42 = add i8 %41, 1
  store i8 %42, ptr %6, align 1, !tbaa !17
  br label %30, !llvm.loop !18

43:                                               ; preds = %30
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.os_event, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !20
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.os_mbox_data, ptr %47, i32 0, i32 0
  store ptr %46, ptr %48, align 8, !tbaa !21
  %49 = load i32, ptr %9, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %49)
  store i8 0, ptr %3, align 1
  store i32 1, ptr %10, align 4
  br label %50

50:                                               ; preds = %43, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  %51 = load i8, ptr %3, align 1
  ret i8 %51
}

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 16}
!16 = !{!"os_mbox_data", !6, i64 0, !7, i64 8, !7, i64 16}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!12, !6, i64 8}
!21 = !{!16, !6, i64 0}


; 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 @OSMboxQuery(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, 1
  br i1 %.not, label %4, label %35

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 16
  store i8 %7, ptr %8, align 8, !tbaa !12
  %9 = getelementptr i8, ptr %0, i64 19
  %10 = getelementptr i8, ptr %1, i64 8
  %11 = getelementptr i8, ptr %0, i64 20
  %12 = load i8, ptr %9, align 1, !tbaa !14
  %13 = getelementptr i8, ptr %1, i64 9
  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 10
  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 11
  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 12
  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 13
  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 14
  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 15
  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
  store ptr %34, ptr %1, align 8, !tbaa !16
  tail call void @OS_CPU_SR_Restore(i32 noundef %5) #2
  br label %35

35:                                               ; preds = %2, %4
  %.014 = phi i8 [ 0, %4 ], [ 1, %2 ]
  ret i8 %.014
}

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 16}
!13 = !{!"os_mbox_data", !9, i64 0, !7, i64 8, !7, i64 16}
!14 = !{!7, !7, i64 0}
!15 = !{!6, !9, i64 8}
!16 = !{!13, !9, i64 0}


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