; LLVM IR from af-jk/rtos :: src (stos)

; 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.stos_kernel = type { ptr, ptr, ptr, ptr, %struct.stos_tcb }
%struct.stos_tcb = type { ptr, ptr, i32, i32, i32, i32, ptr, ptr }

@stos_ker = external hidden global %struct.stos_kernel, align 8

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; 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 @STOS_RemoveTask(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  br label %111

9:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store ptr @stos_ker, ptr %3, align 8, !tbaa !5
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.stos_tcb, ptr %10, i32 0, i32 2
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = icmp eq i32 %12, 1
  br i1 %13, label %14, label %15

14:                                               ; preds = %9
  store ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 1), ptr %3, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %14, %9
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %20

19:                                               ; preds = %15
  store i32 1, ptr %4, align 4
  br label %109

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  store ptr %22, ptr %5, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %34, %20
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.stos_tcb, ptr %24, i32 0, i32 6
  %26 = load ptr, ptr %25, align 8, !tbaa !12
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %32

28:                                               ; preds = %23
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = icmp ne ptr %29, %30
  br label %32

32:                                               ; preds = %28, %23
  %33 = phi i1 [ false, %23 ], [ %31, %28 ]
  br i1 %33, label %34, label %38

34:                                               ; preds = %32
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.stos_tcb, ptr %35, i32 0, i32 6
  %37 = load ptr, ptr %36, align 8, !tbaa !12
  store ptr %37, ptr %5, align 8, !tbaa !5
  br label %23, !llvm.loop !13

38:                                               ; preds = %32
  store ptr %5, ptr %3, align 8, !tbaa !5
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.stos_tcb, ptr %40, i32 0, i32 7
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  %43 = icmp eq ptr %42, null
  br i1 %43, label %44, label %68

44:                                               ; preds = %38
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = load ptr, ptr %45, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.stos_tcb, ptr %46, i32 0, i32 6
  %48 = load ptr, ptr %47, align 8, !tbaa !12
  %49 = getelementptr inbounds %struct.stos_tcb, ptr %48, i32 0, i32 7
  store ptr null, ptr %49, align 8, !tbaa !15
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.stos_tcb, ptr %50, i32 0, i32 2
  %52 = load i32, ptr %51, align 8, !tbaa !9
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %59

54:                                               ; preds = %44
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = load ptr, ptr %55, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.stos_tcb, ptr %56, i32 0, i32 6
  %58 = load ptr, ptr %57, align 8, !tbaa !12
  store ptr %58, ptr @stos_ker, align 8, !tbaa !16
  br label %64

59:                                               ; preds = %44
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.stos_tcb, ptr %61, i32 0, i32 6
  %63 = load ptr, ptr %62, align 8, !tbaa !12
  store ptr %63, ptr getelementptr inbounds (%struct.stos_kernel, ptr @stos_ker, i32 0, i32 1), align 8, !tbaa !18
  br label %64

64:                                               ; preds = %59, %54
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = load ptr, ptr %65, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.stos_tcb, ptr %66, i32 0, i32 6
  store ptr null, ptr %67, align 8, !tbaa !12
  store i32 1, ptr %4, align 4
  br label %108

68:                                               ; preds = %38
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = load ptr, ptr %69, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.stos_tcb, ptr %70, i32 0, i32 6
  %72 = load ptr, ptr %71, align 8, !tbaa !12
  %73 = icmp eq ptr %72, null
  br i1 %73, label %74, label %83

74:                                               ; preds = %68
  %75 = load ptr, ptr %3, align 8, !tbaa !5
  %76 = load ptr, ptr %75, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.stos_tcb, ptr %76, i32 0, i32 7
  %78 = load ptr, ptr %77, align 8, !tbaa !15
  %79 = getelementptr inbounds %struct.stos_tcb, ptr %78, i32 0, i32 6
  store ptr null, ptr %79, align 8, !tbaa !12
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = load ptr, ptr %80, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.stos_tcb, ptr %81, i32 0, i32 7
  store ptr null, ptr %82, align 8, !tbaa !15
  store i32 1, ptr %4, align 4
  br label %108

83:                                               ; preds = %68
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = load ptr, ptr %84, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.stos_tcb, ptr %85, i32 0, i32 6
  %87 = load ptr, ptr %86, align 8, !tbaa !12
  %88 = load ptr, ptr %3, align 8, !tbaa !5
  %89 = load ptr, ptr %88, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.stos_tcb, ptr %89, i32 0, i32 7
  %91 = load ptr, ptr %90, align 8, !tbaa !15
  %92 = getelementptr inbounds %struct.stos_tcb, ptr %91, i32 0, i32 6
  store ptr %87, ptr %92, align 8, !tbaa !12
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = load ptr, ptr %93, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.stos_tcb, ptr %94, i32 0, i32 7
  %96 = load ptr, ptr %95, align 8, !tbaa !15
  %97 = load ptr, ptr %3, align 8, !tbaa !5
  %98 = load ptr, ptr %97, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.stos_tcb, ptr %98, i32 0, i32 6
  %100 = load ptr, ptr %99, align 8, !tbaa !12
  %101 = getelementptr inbounds %struct.stos_tcb, ptr %100, i32 0, i32 7
  store ptr %96, ptr %101, align 8, !tbaa !15
  %102 = load ptr, ptr %3, align 8, !tbaa !5
  %103 = load ptr, ptr %102, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.stos_tcb, ptr %103, i32 0, i32 6
  store ptr null, ptr %104, align 8, !tbaa !12
  %105 = load ptr, ptr %3, align 8, !tbaa !5
  %106 = load ptr, ptr %105, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.stos_tcb, ptr %106, i32 0, i32 7
  store ptr null, ptr %107, align 8, !tbaa !15
  store i32 0, ptr %4, align 4
  br label %108

108:                                              ; preds = %83, %74, %64
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %109

109:                                              ; preds = %108, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  %110 = load i32, ptr %4, align 4
  switch i32 %110, label %112 [
    i32 0, label %111
    i32 1, label %111
  ]

111:                                              ; preds = %109, %109, %8
  ret void

112:                                              ; preds = %109
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 16}
!10 = !{!"stos_tcb", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !6, i64 32, !6, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 32}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!10, !6, i64 40}
!16 = !{!17, !6, i64 0}
!17 = !{!"stos_kernel", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32}
!18 = !{!17, !6, i64 8}


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

%struct.stos_kernel = type { ptr, ptr, ptr, ptr, %struct.stos_tcb }
%struct.stos_tcb = type { ptr, ptr, i32, i32, i32, i32, ptr, ptr }

@stos_ker = external hidden local_unnamed_addr global %struct.stos_kernel, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @STOS_RemoveTask(ptr noundef readonly %0) local_unnamed_addr #0 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %29, label %3

3:                                                ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i32 %5, 1
  %.val = load ptr, ptr getelementptr inbounds (i8, ptr @stos_ker, i64 8), align 8
  %stos_ker.val = load ptr, ptr @stos_ker, align 8
  %7 = select i1 %6, ptr %.val, ptr %stos_ker.val
  %8 = icmp eq ptr %7, null
  br i1 %8, label %29, label %.preheader

.preheader:                                       ; preds = %3, %.preheader
  %.0 = phi ptr [ %10, %.preheader ], [ %7, %3 ]
  %9 = getelementptr inbounds i8, ptr %.0, i64 32
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %11 = icmp ne ptr %10, null
  %12 = icmp ne ptr %.0, %0
  %13 = and i1 %12, %11
  br i1 %13, label %.preheader, label %14, !llvm.loop !12

14:                                               ; preds = %.preheader
  %15 = getelementptr inbounds i8, ptr %.0, i64 32
  %16 = getelementptr inbounds i8, ptr %.0, i64 40
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %22

19:                                               ; preds = %14
  %20 = getelementptr inbounds i8, ptr %10, i64 40
  store ptr null, ptr %20, align 8, !tbaa !14
  %21 = icmp eq i32 %5, 0
  %stos_ker. = select i1 %21, ptr @stos_ker, ptr getelementptr inbounds (i8, ptr @stos_ker, i64 8)
  store ptr %10, ptr %stos_ker., align 8, !tbaa !15
  store ptr null, ptr %15, align 8, !tbaa !11
  br label %29

22:                                               ; preds = %14
  %23 = icmp eq ptr %10, null
  %24 = getelementptr inbounds i8, ptr %17, i64 32
  br i1 %23, label %25, label %26

25:                                               ; preds = %22
  store ptr null, ptr %24, align 8, !tbaa !11
  store ptr null, ptr %16, align 8, !tbaa !14
  br label %29

26:                                               ; preds = %22
  store ptr %10, ptr %24, align 8, !tbaa !11
  %27 = load ptr, ptr %15, align 8, !tbaa !11
  %28 = getelementptr inbounds i8, ptr %27, i64 40
  store ptr %17, ptr %28, align 8, !tbaa !14
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %15, i8 0, i64 16, i1 false)
  br label %29

29:                                               ; preds = %3, %26, %25, %19, %1
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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, !10, i64 16}
!6 = !{!"stos_tcb", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !7, i64 32, !7, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 32}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!6, !7, i64 40}
!15 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass