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

; 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_tcb = type { ptr, ptr, ptr, i32, i16, i16, ptr, ptr, ptr, ptr, ptr, i16, i32, i8, i8, i8, i8, i8, i8, i8, i8, i32, i32, i32, ptr, i32, ptr, [1 x i32] }

@OSTCBPrioTbl = external global [64 x ptr], align 16
@OSTCBCur = external global ptr, align 8
@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 void @OSTaskNameSet(i8 noundef zeroext %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca i8, align 1
  %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 i8 %0, ptr %4, align 1, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !8
  store ptr %2, ptr %6, align 8, !tbaa !8
  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 !10
  %10 = load i8, ptr @OSIntNesting, align 1, !tbaa !5
  %11 = zext i8 %10 to i32
  %12 = icmp ugt i32 %11, 0
  br i1 %12, label %13, label %15

13:                                               ; preds = %3
  %14 = load ptr, ptr %6, align 8, !tbaa !8
  store i8 18, ptr %14, align 1, !tbaa !5
  store i32 1, ptr %9, align 4
  br label %46

15:                                               ; preds = %3
  %16 = call i32 @OS_CPU_SR_Save()
  store i32 %16, ptr %8, align 4, !tbaa !10
  %17 = load i8, ptr %4, align 1, !tbaa !5
  %18 = zext i8 %17 to i32
  %19 = icmp eq i32 %18, 255
  br i1 %19, label %20, label %24

20:                                               ; preds = %15
  %21 = load ptr, ptr @OSTCBCur, align 8, !tbaa !8
  %22 = getelementptr inbounds %struct.os_tcb, ptr %21, i32 0, i32 15
  %23 = load i8, ptr %22, align 2, !tbaa !12
  store i8 %23, ptr %4, align 1, !tbaa !5
  br label %24

24:                                               ; preds = %20, %15
  %25 = load i8, ptr %4, align 1, !tbaa !5
  %26 = zext i8 %25 to i64
  %27 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !8
  store ptr %28, ptr %7, align 8, !tbaa !8
  %29 = load ptr, ptr %7, align 8, !tbaa !8
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %34

31:                                               ; preds = %24
  %32 = load i32, ptr %8, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %32)
  %33 = load ptr, ptr %6, align 8, !tbaa !8
  store i8 67, ptr %33, align 1, !tbaa !5
  store i32 1, ptr %9, align 4
  br label %46

34:                                               ; preds = %24
  %35 = load ptr, ptr %7, align 8, !tbaa !8
  %36 = icmp eq ptr %35, inttoptr (i64 1 to ptr)
  br i1 %36, label %37, label %40

37:                                               ; preds = %34
  %38 = load i32, ptr %8, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %38)
  %39 = load ptr, ptr %6, align 8, !tbaa !8
  store i8 67, ptr %39, align 1, !tbaa !5
  store i32 1, ptr %9, align 4
  br label %46

40:                                               ; preds = %34
  %41 = load ptr, ptr %5, align 8, !tbaa !8
  %42 = load ptr, ptr %7, align 8, !tbaa !8
  %43 = getelementptr inbounds %struct.os_tcb, ptr %42, i32 0, i32 26
  store ptr %41, ptr %43, align 8, !tbaa !15
  %44 = load i32, ptr %8, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %44)
  %45 = load ptr, ptr %6, align 8, !tbaa !8
  store i8 0, ptr %45, align 1, !tbaa !5
  store i32 0, ptr %9, align 4
  br label %46

46:                                               ; preds = %40, %37, %31, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %47 = load i32, ptr %9, align 4
  switch i32 %47, label %49 [
    i32 0, label %48
    i32 1, label %48
  ]

48:                                               ; preds = %46, %46
  ret void

49:                                               ; preds = %46
  unreachable
}

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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!13, !6, i64 82}
!13 = !{!"os_tcb", !9, i64 0, !9, i64 8, !9, i64 16, !11, i64 24, !14, i64 28, !14, i64 30, !9, i64 32, !9, i64 40, !9, i64 48, !9, i64 56, !9, i64 64, !14, i64 72, !11, i64 76, !6, i64 80, !6, i64 81, !6, i64 82, !6, i64 83, !6, i64 84, !6, i64 85, !6, i64 86, !6, i64 87, !11, i64 88, !11, i64 92, !11, i64 96, !9, i64 104, !11, i64 112, !9, i64 120, !6, i64 128}
!14 = !{!"short", !6, i64 0}
!15 = !{!13, !9, i64 120}


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

@OSTCBPrioTbl = external local_unnamed_addr global [64 x ptr], align 16
@OSTCBCur = external local_unnamed_addr global ptr, align 8
@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 void @OSTaskNameSet(i8 noundef zeroext %0, ptr noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #1 {
  %4 = load i8, ptr @OSIntNesting, align 1, !tbaa !5
  %.not = icmp eq i8 %4, 0
  br i1 %.not, label %5, label %18

5:                                                ; preds = %3
  %6 = tail call i32 @OS_CPU_SR_Save() #2
  %7 = icmp eq i8 %0, -1
  br i1 %7, label %8, label %12

8:                                                ; preds = %5
  %9 = load ptr, ptr @OSTCBCur, align 8, !tbaa !8
  %10 = getelementptr inbounds i8, ptr %9, i64 82
  %11 = load i8, ptr %10, align 2, !tbaa !10
  br label %12

12:                                               ; preds = %8, %5
  %.0 = phi i8 [ %11, %8 ], [ %0, %5 ]
  %13 = zext i8 %.0 to i64
  %14 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !8
  %switch = icmp ult ptr %15, inttoptr (i64 2 to ptr)
  br i1 %switch, label %.sink.split, label %16

16:                                               ; preds = %12
  %17 = getelementptr inbounds i8, ptr %15, i64 120
  store ptr %1, ptr %17, align 8, !tbaa !14
  br label %.sink.split

.sink.split:                                      ; preds = %12, %16
  %.sink.ph = phi i8 [ 0, %16 ], [ 67, %12 ]
  tail call void @OS_CPU_SR_Restore(i32 noundef %6) #2
  br label %18

18:                                               ; preds = %.sink.split, %3
  %.sink = phi i8 [ 18, %3 ], [ %.sink.ph, %.sink.split ]
  store i8 %.sink, ptr %2, align 1, !tbaa !5
  ret void
}

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, !6, i64 82}
!11 = !{!"os_tcb", !9, i64 0, !9, i64 8, !9, i64 16, !12, i64 24, !13, i64 28, !13, i64 30, !9, i64 32, !9, i64 40, !9, i64 48, !9, i64 56, !9, i64 64, !13, i64 72, !12, i64 76, !6, i64 80, !6, i64 81, !6, i64 82, !6, i64 83, !6, i64 84, !6, i64 85, !6, i64 86, !6, i64 87, !12, i64 88, !12, i64 92, !12, i64 96, !9, i64 104, !12, i64 112, !9, i64 120, !6, i64 128}
!12 = !{!"int", !6, i64 0}
!13 = !{!"short", !6, i64 0}
!14 = !{!11, !9, i64 120}


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