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

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

@OSIntNesting = external global i8, align 1

; 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 @OSMutexQuery(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 i8, ptr @OSIntNesting, align 1, !tbaa !11
  %12 = zext i8 %11 to i32
  %13 = icmp ugt i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  store i8 6, ptr %3, align 1
  store i32 1, ptr %10, align 4
  br label %78

15:                                               ; preds = %2
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.os_event, ptr %16, i32 0, i32 0
  %18 = load i8, ptr %17, align 8, !tbaa !12
  %19 = zext i8 %18 to i32
  %20 = icmp ne i32 %19, 4
  br i1 %20, label %21, label %22

21:                                               ; preds = %15
  store i8 1, ptr %3, align 1
  store i32 1, ptr %10, align 4
  br label %78

22:                                               ; preds = %15
  %23 = call i32 @OS_CPU_SR_Save()
  store i32 %23, ptr %9, align 4, !tbaa !9
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.os_event, ptr %24, i32 0, i32 2
  %26 = load i16, ptr %25, align 8, !tbaa !15
  %27 = zext i16 %26 to i32
  %28 = ashr i32 %27, 8
  %29 = trunc i32 %28 to i8
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.os_mutex_data, ptr %30, i32 0, i32 4
  store i8 %29, ptr %31, align 1, !tbaa !16
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.os_event, ptr %32, i32 0, i32 2
  %34 = load i16, ptr %33, align 8, !tbaa !15
  %35 = zext i16 %34 to i32
  %36 = and i32 %35, 255
  %37 = trunc i32 %36 to i8
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.os_mutex_data, ptr %38, i32 0, i32 3
  store i8 %37, ptr %39, align 1, !tbaa !18
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.os_mutex_data, ptr %40, i32 0, i32 3
  %42 = load i8, ptr %41, align 1, !tbaa !18
  %43 = zext i8 %42 to i32
  %44 = icmp eq i32 %43, 255
  br i1 %44, label %45, label %48

45:                                               ; preds = %22
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.os_mutex_data, ptr %46, i32 0, i32 2
  store i8 1, ptr %47, align 1, !tbaa !19
  br label %51

48:                                               ; preds = %22
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.os_mutex_data, ptr %49, i32 0, i32 2
  store i8 0, ptr %50, align 1, !tbaa !19
  br label %51

51:                                               ; preds = %48, %45
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.os_event, ptr %52, i32 0, i32 3
  %54 = load i8, ptr %53, align 2, !tbaa !20
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.os_mutex_data, ptr %55, i32 0, i32 1
  store i8 %54, ptr %56, align 1, !tbaa !21
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.os_event, ptr %57, i32 0, i32 4
  %59 = getelementptr inbounds [8 x i8], ptr %58, i64 0, i64 0
  store ptr %59, ptr %7, align 8, !tbaa !5
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.os_mutex_data, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds [8 x i8], ptr %61, i64 0, i64 0
  store ptr %62, ptr %8, align 8, !tbaa !5
  store i8 0, ptr %6, align 1, !tbaa !11
  br label %63

63:                                               ; preds = %73, %51
  %64 = load i8, ptr %6, align 1, !tbaa !11
  %65 = zext i8 %64 to i32
  %66 = icmp ult i32 %65, 8
  br i1 %66, label %67, label %76

67:                                               ; preds = %63
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = getelementptr inbounds i8, ptr %68, i32 1
  store ptr %69, ptr %7, align 8, !tbaa !5
  %70 = load i8, ptr %68, align 1, !tbaa !11
  %71 = load ptr, ptr %8, align 8, !tbaa !5
  %72 = getelementptr inbounds i8, ptr %71, i32 1
  store ptr %72, ptr %8, align 8, !tbaa !5
  store i8 %70, ptr %71, align 1, !tbaa !11
  br label %73

73:                                               ; preds = %67
  %74 = load i8, ptr %6, align 1, !tbaa !11
  %75 = add i8 %74, 1
  store i8 %75, ptr %6, align 1, !tbaa !11
  br label %63, !llvm.loop !22

76:                                               ; preds = %63
  %77 = load i32, ptr %9, align 4, !tbaa !9
  call void @OS_CPU_SR_Restore(i32 noundef %77)
  store i8 0, ptr %3, align 1
  store i32 1, ptr %10, align 4
  br label %78

78:                                               ; preds = %76, %21, %14
  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
  %79 = load i8, ptr %3, align 1
  ret i8 %79
}

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 = !{!7, !7, i64 0}
!12 = !{!13, !7, i64 0}
!13 = !{!"os_event", !7, i64 0, !6, i64 8, !14, i64 16, !7, i64 18, !7, i64 19}
!14 = !{!"short", !7, i64 0}
!15 = !{!13, !14, i64 16}
!16 = !{!17, !7, i64 11}
!17 = !{!"os_mutex_data", !7, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11}
!18 = !{!17, !7, i64 10}
!19 = !{!17, !7, i64 9}
!20 = !{!13, !7, i64 18}
!21 = !{!17, !7, i64 8}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


; 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

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

4:                                                ; preds = %2
  %5 = load i8, ptr %0, align 8, !tbaa !8
  %.not20 = icmp eq i8 %5, 4
  br i1 %.not20, label %6, label %43

6:                                                ; preds = %4
  %7 = tail call i32 @OS_CPU_SR_Save() #2
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load i16, ptr %8, align 8, !tbaa !12
  %10 = lshr i16 %9, 8
  %11 = trunc nuw i16 %10 to i8
  %12 = getelementptr inbounds i8, ptr %1, i64 11
  store i8 %11, ptr %12, align 1, !tbaa !13
  %13 = trunc i16 %9 to i8
  %14 = getelementptr inbounds i8, ptr %1, i64 10
  store i8 %13, ptr %14, align 1, !tbaa !15
  %.mask = and i16 %9, 255
  %15 = icmp eq i16 %.mask, 255
  %spec.select = zext i1 %15 to i8
  %16 = getelementptr inbounds i8, ptr %1, i64 9
  store i8 %spec.select, ptr %16, align 1
  %17 = getelementptr inbounds i8, ptr %0, i64 18
  %18 = load i8, ptr %17, align 2, !tbaa !16
  %19 = getelementptr inbounds i8, ptr %1, i64 8
  store i8 %18, ptr %19, align 1, !tbaa !17
  %20 = getelementptr i8, ptr %0, i64 19
  %21 = getelementptr i8, ptr %0, i64 20
  %22 = load i8, ptr %20, align 1, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %1, i64 1
  store i8 %22, ptr %1, align 1, !tbaa !5
  %24 = getelementptr i8, ptr %0, i64 21
  %25 = load i8, ptr %21, align 1, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %1, i64 2
  store i8 %25, ptr %23, align 1, !tbaa !5
  %27 = getelementptr i8, ptr %0, i64 22
  %28 = load i8, ptr %24, align 1, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %1, i64 3
  store i8 %28, ptr %26, align 1, !tbaa !5
  %30 = getelementptr i8, ptr %0, i64 23
  %31 = load i8, ptr %27, align 1, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %1, i64 4
  store i8 %31, ptr %29, align 1, !tbaa !5
  %33 = getelementptr i8, ptr %0, i64 24
  %34 = load i8, ptr %30, align 1, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %1, i64 5
  store i8 %34, ptr %32, align 1, !tbaa !5
  %36 = getelementptr i8, ptr %0, i64 25
  %37 = load i8, ptr %33, align 1, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %1, i64 6
  store i8 %37, ptr %35, align 1, !tbaa !5
  %39 = getelementptr i8, ptr %0, i64 26
  %40 = load i8, ptr %36, align 1, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %1, i64 7
  store i8 %40, ptr %38, align 1, !tbaa !5
  %42 = load i8, ptr %39, align 1, !tbaa !5
  store i8 %42, ptr %41, align 1, !tbaa !5
  tail call void @OS_CPU_SR_Restore(i32 noundef %7) #2
  br label %43

43:                                               ; preds = %4, %2, %6
  %.019 = phi i8 [ 0, %6 ], [ 6, %2 ], [ 1, %4 ]
  ret i8 %.019
}

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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !6, i64 0}
!9 = !{!"os_event", !6, i64 0, !10, i64 8, !11, i64 16, !6, i64 18, !6, i64 19}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!9, !11, i64 16}
!13 = !{!14, !6, i64 11}
!14 = !{!"os_mutex_data", !6, i64 0, !6, i64 8, !6, i64 9, !6, i64 10, !6, i64 11}
!15 = !{!14, !6, i64 10}
!16 = !{!9, !6, i64 18}
!17 = !{!14, !6, i64 8}


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