; LLVM IR from kala13x/libxutils :: src/sys (thread)

; 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.XTask = type { ptr, ptr, %struct.XThread, i32, i32, i32 }
%struct.XThread = type { ptr, ptr, i64, i32, i8, i8 }

; 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 ptr @XTask_WorkerThread(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %8, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.XTask, ptr %9, i32 0, i32 5
  %11 = atomicrmw xchg ptr %10, i32 4 seq_cst, align 8
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #3
  store i8 0, ptr %4, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !10
  br label %12

12:                                               ; preds = %66, %64, %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.XTask, ptr %13, i32 0, i32 4
  %15 = atomicrmw add ptr %14, i32 0 seq_cst, align 4
  %16 = add i32 %15, 0
  store i32 %16, ptr %5, align 4, !tbaa !10
  %17 = icmp ne i32 %16, 9
  br i1 %17, label %18, label %67

18:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.XTask, ptr %19, i32 0, i32 3
  %21 = atomicrmw add ptr %20, i32 0 seq_cst, align 8
  %22 = add i32 %21, 0
  store i32 %22, ptr %6, align 4, !tbaa !10
  %23 = load i32, ptr %5, align 4, !tbaa !10
  %24 = icmp eq i32 %23, 8
  br i1 %24, label %25, label %41

25:                                               ; preds = %18
  %26 = load i8, ptr %4, align 1, !tbaa !9
  %27 = icmp ne i8 %26, 0
  br i1 %27, label %39, label %28

28:                                               ; preds = %25
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.XTask, ptr %29, i32 0, i32 5
  %31 = atomicrmw xchg ptr %30, i32 5 seq_cst, align 8
  %32 = load i32, ptr %6, align 4, !tbaa !10
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %36

34:                                               ; preds = %28
  %35 = load i32, ptr %6, align 4, !tbaa !10
  br label %37

36:                                               ; preds = %28
  br label %37

37:                                               ; preds = %36, %34
  %38 = phi i32 [ %35, %34 ], [ 10000, %36 ]
  store i32 %38, ptr %6, align 4, !tbaa !10
  store i8 1, ptr %4, align 1, !tbaa !9
  br label %39

39:                                               ; preds = %37, %25
  %40 = load i32, ptr %6, align 4, !tbaa !10
  call void @xusleep(i32 noundef %40)
  store i32 2, ptr %7, align 4
  br label %64, !llvm.loop !12

41:                                               ; preds = %18
  %42 = load i8, ptr %4, align 1, !tbaa !9
  %43 = icmp ne i8 %42, 0
  br i1 %43, label %44, label %48

44:                                               ; preds = %41
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.XTask, ptr %45, i32 0, i32 5
  %47 = atomicrmw xchg ptr %46, i32 4 seq_cst, align 8
  store i8 0, ptr %4, align 1, !tbaa !9
  br label %48

48:                                               ; preds = %44, %41
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.XTask, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !14
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.XTask, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !18
  %55 = call i32 %51(ptr noundef %54)
  %56 = icmp slt i32 %55, 0
  br i1 %56, label %57, label %58

57:                                               ; preds = %48
  store i32 3, ptr %7, align 4
  br label %64

58:                                               ; preds = %48
  %59 = load i32, ptr %6, align 4, !tbaa !10
  %60 = icmp ne i32 %59, 0
  br i1 %60, label %61, label %63

61:                                               ; preds = %58
  %62 = load i32, ptr %6, align 4, !tbaa !10
  call void @xusleep(i32 noundef %62)
  br label %63

63:                                               ; preds = %61, %58
  store i32 0, ptr %7, align 4
  br label %64

64:                                               ; preds = %63, %57, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %65 = load i32, ptr %7, align 4
  switch i32 %65, label %71 [
    i32 0, label %66
    i32 2, label %12
    i32 3, label %67
  ]

66:                                               ; preds = %64
  br label %12, !llvm.loop !12

67:                                               ; preds = %64, %12
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.XTask, ptr %68, i32 0, i32 5
  %70 = atomicrmw xchg ptr %69, i32 6 seq_cst, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret ptr null

71:                                               ; preds = %64
  unreachable
}

declare void @xusleep(i32 noundef) #2

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 = { "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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 8}
!15 = !{!"XTask", !6, i64 0, !6, i64 8, !16, i64 16, !11, i64 48, !11, i64 52, !11, i64 56}
!16 = !{!"XThread", !6, i64 0, !6, i64 8, !17, i64 16, !11, i64 24, !7, i64 28, !7, i64 29}
!17 = !{!"long", !7, i64 0}
!18 = !{!15, !6, i64 0}


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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @XTask_WorkerThread(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 56
  %3 = atomicrmw xchg ptr %2, i32 4 seq_cst, align 8
  %4 = getelementptr inbounds i8, ptr %0, i64 52
  %5 = atomicrmw or ptr %4, i32 0 seq_cst, align 4
  %.not29 = icmp eq i32 %5, 9
  br i1 %.not29, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 48
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  br label %8

8:                                                ; preds = %.lr.ph, %25
  %9 = phi i32 [ %5, %.lr.ph ], [ %26, %25 ]
  %.01830 = phi i8 [ 0, %.lr.ph ], [ %.3, %25 ]
  %10 = atomicrmw or ptr %6, i32 0 seq_cst, align 8
  %11 = icmp eq i32 %9, 8
  %.not23 = icmp eq i8 %.01830, 0
  br i1 %11, label %12, label %16

12:                                               ; preds = %8
  br i1 %.not23, label %13, label %.sink.split, !llvm.loop !5

13:                                               ; preds = %12
  %14 = atomicrmw xchg ptr %2, i32 5 seq_cst, align 8
  %.not24 = icmp eq i32 %10, 0
  %15 = select i1 %.not24, i32 10000, i32 %10
  br label %.sink.split, !llvm.loop !5

16:                                               ; preds = %8
  br i1 %.not23, label %19, label %17

17:                                               ; preds = %16
  %18 = atomicrmw xchg ptr %2, i32 4 seq_cst, align 8
  br label %19

19:                                               ; preds = %17, %16
  %20 = load ptr, ptr %7, align 8, !tbaa !7
  %21 = load ptr, ptr %0, align 8, !tbaa !15
  %22 = tail call i32 %20(ptr noundef %21) #2
  %23 = icmp slt i32 %22, 0
  br i1 %23, label %.thread, label %24

24:                                               ; preds = %19
  %.not22 = icmp eq i32 %10, 0
  br i1 %.not22, label %25, label %.sink.split

.sink.split:                                      ; preds = %24, %12, %13
  %.sink = phi i32 [ %10, %12 ], [ %15, %13 ], [ %10, %24 ]
  %.3.ph = phi i8 [ 1, %12 ], [ 1, %13 ], [ 0, %24 ]
  tail call void @xusleep(i32 noundef %.sink) #2
  br label %25

25:                                               ; preds = %.sink.split, %24
  %.3 = phi i8 [ 0, %24 ], [ %.3.ph, %.sink.split ]
  %26 = atomicrmw or ptr %4, i32 0 seq_cst, align 4
  %.not = icmp eq i32 %26, 9
  br i1 %.not, label %.thread, label %8

.thread:                                          ; preds = %25, %19, %1
  %27 = atomicrmw xchg ptr %2, i32 6 seq_cst, align 8
  ret ptr null
}

declare void @xusleep(i32 noundef) local_unnamed_addr #1

attributes #0 = { 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 #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 }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !9, i64 8}
!8 = !{!"XTask", !9, i64 0, !9, i64 8, !12, i64 16, !14, i64 48, !14, i64 52, !14, i64 56}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!"XThread", !9, i64 0, !9, i64 8, !13, i64 16, !14, i64 24, !10, i64 28, !10, i64 29}
!13 = !{!"long", !10, i64 0}
!14 = !{!"int", !10, i64 0}
!15 = !{!8, !9, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass