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

15:                                               ; preds = %3
  %16 = load ptr, ptr %7, align 8, !tbaa !8
  store i8 17, ptr %16, align 1, !tbaa !5
  store i8 0, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %53

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

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

26:                                               ; preds = %22, %17
  %27 = load i8, ptr %5, align 1, !tbaa !5
  %28 = zext i8 %27 to i64
  %29 = getelementptr inbounds [64 x ptr], ptr @OSTCBPrioTbl, i64 0, i64 %28
  %30 = load ptr, ptr %29, align 8, !tbaa !8
  store ptr %30, ptr %8, align 8, !tbaa !8
  %31 = load ptr, ptr %8, align 8, !tbaa !8
  %32 = icmp eq ptr %31, null
  br i1 %32, label %33, label %36

33:                                               ; preds = %26
  %34 = load i32, ptr %10, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %34)
  %35 = load ptr, ptr %7, align 8, !tbaa !8
  store i8 67, ptr %35, align 1, !tbaa !5
  store i8 0, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %53

36:                                               ; preds = %26
  %37 = load ptr, ptr %8, align 8, !tbaa !8
  %38 = icmp eq ptr %37, inttoptr (i64 1 to ptr)
  br i1 %38, label %39, label %42

39:                                               ; preds = %36
  %40 = load i32, ptr %10, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %40)
  %41 = load ptr, ptr %7, align 8, !tbaa !8
  store i8 67, ptr %41, align 1, !tbaa !5
  store i8 0, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %53

42:                                               ; preds = %36
  %43 = load ptr, ptr %8, align 8, !tbaa !8
  %44 = getelementptr inbounds %struct.os_tcb, ptr %43, i32 0, i32 26
  %45 = load ptr, ptr %44, align 8, !tbaa !15
  %46 = load ptr, ptr %6, align 8, !tbaa !8
  store ptr %45, ptr %46, align 8, !tbaa !8
  %47 = load ptr, ptr %6, align 8, !tbaa !8
  %48 = load ptr, ptr %47, align 8, !tbaa !8
  %49 = call zeroext i8 @OS_StrLen(ptr noundef %48)
  store i8 %49, ptr %9, align 1, !tbaa !5
  %50 = load i32, ptr %10, align 4, !tbaa !10
  call void @OS_CPU_SR_Restore(i32 noundef %50)
  %51 = load ptr, ptr %7, align 8, !tbaa !8
  store i8 0, ptr %51, align 1, !tbaa !5
  %52 = load i8, ptr %9, align 1, !tbaa !5
  store i8 %52, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %53

53:                                               ; preds = %42, %39, %33, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %54 = load i8, ptr %4, align 1
  ret i8 %54
}

declare zeroext i8 @OS_StrLen(ptr noundef) #1

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 zeroext i8 @OSTaskNameGet(i8 noundef zeroext %0, ptr nocapture noundef writeonly %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 %20

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
  %.015 = phi i8 [ %11, %8 ], [ %0, %5 ]
  %13 = zext i8 %.015 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
  %18 = load ptr, ptr %17, align 8, !tbaa !14
  store ptr %18, ptr %1, align 8, !tbaa !8
  %19 = tail call zeroext i8 @OS_StrLen(ptr noundef %18) #2
  br label %.sink.split

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

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

declare zeroext i8 @OS_StrLen(ptr noundef) local_unnamed_addr #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, !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: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass