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

; 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_flag_grp = type { i8, ptr, i16, ptr }

@OSIntNesting = external global i8, align 1
@OSFlagFreeList = external global ptr, align 8
@.str = external hidden unnamed_addr constant [2 x 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 ptr @OSFlagCreate(i16 noundef zeroext %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i16 %0, ptr %4, align 2, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !11
  %9 = load i8, ptr @OSIntNesting, align 1, !tbaa !13
  %10 = zext i8 %9 to i32
  %11 = icmp ugt i32 %10, 0
  br i1 %11, label %12, label %14

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

14:                                               ; preds = %2
  %15 = call i32 @OS_CPU_SR_Save()
  store i32 %15, ptr %7, align 4, !tbaa !11
  %16 = load ptr, ptr @OSFlagFreeList, align 8, !tbaa !9
  store ptr %16, ptr %6, align 8, !tbaa !9
  %17 = load ptr, ptr %6, align 8, !tbaa !9
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %34

19:                                               ; preds = %14
  %20 = load ptr, ptr @OSFlagFreeList, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.os_flag_grp, ptr %20, i32 0, i32 1
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  store ptr %22, ptr @OSFlagFreeList, align 8, !tbaa !9
  %23 = load ptr, ptr %6, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.os_flag_grp, ptr %23, i32 0, i32 0
  store i8 5, ptr %24, align 8, !tbaa !16
  %25 = load i16, ptr %4, align 2, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.os_flag_grp, ptr %26, i32 0, i32 2
  store i16 %25, ptr %27, align 8, !tbaa !17
  %28 = load ptr, ptr %6, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.os_flag_grp, ptr %28, i32 0, i32 1
  store ptr null, ptr %29, align 8, !tbaa !14
  %30 = load ptr, ptr %6, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.os_flag_grp, ptr %30, i32 0, i32 3
  store ptr @.str, ptr %31, align 8, !tbaa !18
  %32 = load i32, ptr %7, align 4, !tbaa !11
  call void @OS_CPU_SR_Restore(i32 noundef %32)
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  store i8 0, ptr %33, align 1, !tbaa !13
  br label %37

34:                                               ; preds = %14
  %35 = load i32, ptr %7, align 4, !tbaa !11
  call void @OS_CPU_SR_Restore(i32 noundef %35)
  %36 = load ptr, ptr %5, align 8, !tbaa !9
  store i8 114, ptr %36, align 1, !tbaa !13
  br label %37

37:                                               ; preds = %34, %19
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %38, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %39

39:                                               ; preds = %37, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %40 = load ptr, ptr %3, align 8
  ret ptr %40
}

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


; 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
@OSFlagFreeList = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [2 x 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 ptr @OSFlagCreate(i16 noundef zeroext %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 %12

4:                                                ; preds = %2
  %5 = tail call i32 @OS_CPU_SR_Save() #2
  %6 = load ptr, ptr @OSFlagFreeList, align 8, !tbaa !8
  %.not13 = icmp eq ptr %6, null
  br i1 %.not13, label %.sink.split, label %7

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %6, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !10
  store ptr %9, ptr @OSFlagFreeList, align 8, !tbaa !8
  store i8 5, ptr %6, align 8, !tbaa !13
  %10 = getelementptr inbounds i8, ptr %6, i64 16
  store i16 %0, ptr %10, align 8, !tbaa !14
  store ptr null, ptr %8, align 8, !tbaa !10
  %11 = getelementptr inbounds i8, ptr %6, i64 24
  store ptr @.str, ptr %11, align 8, !tbaa !15
  br label %.sink.split

.sink.split:                                      ; preds = %4, %7
  %storemerge14.ph = phi i8 [ 0, %7 ], [ 114, %4 ]
  %.0.ph = phi ptr [ %6, %7 ], [ null, %4 ]
  tail call void @OS_CPU_SR_Restore(i32 noundef %5) #2
  br label %12

12:                                               ; preds = %.sink.split, %2
  %storemerge14 = phi i8 [ 16, %2 ], [ %storemerge14.ph, %.sink.split ]
  %.0 = phi ptr [ null, %2 ], [ %.0.ph, %.sink.split ]
  store i8 %storemerge14, ptr %1, align 1, !tbaa !5
  ret ptr %.0
}

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, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !9, i64 8}
!11 = !{!"os_flag_grp", !6, i64 0, !9, i64 8, !12, i64 16, !9, i64 24}
!12 = !{!"short", !6, i64 0}
!13 = !{!11, !6, i64 0}
!14 = !{!11, !12, i64 16}
!15 = !{!11, !9, i64 24}


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