; LLVM IR from ctlst-tech/catpilot :: os/freertos/osa/FreeRTOS-Plus-POSIX/source (FreeRTOS_POSIX_pthread)

; 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.pthread_internal = type { %struct.pthread_attr_internal, ptr, ptr, ptr, %struct.xSTATIC_QUEUE, %struct.xSTATIC_QUEUE, ptr }
%struct.pthread_attr_internal = type { i16, i16 }
%struct.xSTATIC_QUEUE = type { [3 x ptr], %union.anon, [2 x %struct.xSTATIC_LIST], [3 x i64], [2 x i8], i8 }
%union.anon = type { ptr }
%struct.xSTATIC_LIST = type { i64, ptr, %struct.xSTATIC_MINI_LIST_ITEM }
%struct.xSTATIC_MINI_LIST_ITEM = type { i32, [2 x ptr] }

; 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
declare dso_local i32 @pthread_attr_setdetachstate(ptr noundef, i32 noundef) #1

declare void @vTaskSuspendAll() #2

declare void @vPortFree(ptr noundef) #2

declare i64 @xTaskResumeAll() #2

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 noundef) #2

declare void @vQueueDelete(ptr noundef) #2

declare void @vTaskDelete(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @pthread_detach(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %6, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.pthread_internal, ptr %7, i32 0, i32 0
  %9 = getelementptr inbounds %struct.pthread_attr_internal, ptr %8, i32 0, i32 1
  %10 = load i16, ptr %9, align 2, !tbaa !11
  %11 = zext i16 %10 to i32
  %12 = and i32 %11, 32768
  %13 = icmp eq i32 %12, 32768
  br i1 %13, label %15, label %14

14:                                               ; preds = %1
  store i32 22, ptr %3, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %14, %1
  %16 = load i32, ptr %3, align 4, !tbaa !9
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %54

18:                                               ; preds = %15
  call void @vTaskSuspendAll()
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.pthread_internal, ptr %19, i32 0, i32 3
  %21 = load ptr, ptr %20, align 8, !tbaa !16
  %22 = call i32 @eTaskGetState(ptr noundef %21)
  store i32 %22, ptr %5, align 4, !tbaa !9
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = icmp eq i32 %23, 4
  br i1 %24, label %28, label %25

25:                                               ; preds = %18
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 5
  br i1 %27, label %28, label %29

28:                                               ; preds = %25, %18
  store i32 22, ptr %3, align 4, !tbaa !9
  br label %52

29:                                               ; preds = %25
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.pthread_internal, ptr %30, i32 0, i32 4
  %32 = call i64 @xQueueGenericSend(ptr noundef %31, ptr noundef null, i32 noundef 0, i64 noundef 0)
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.pthread_internal, ptr %33, i32 0, i32 4
  call void @vQueueDelete(ptr noundef %34)
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.pthread_internal, ptr %35, i32 0, i32 5
  %37 = call i64 @xQueueGenericSend(ptr noundef %36, ptr noundef null, i32 noundef 0, i64 noundef 0)
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.pthread_internal, ptr %38, i32 0, i32 5
  call void @vQueueDelete(ptr noundef %39)
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = icmp eq i32 %40, 3
  br i1 %41, label %42, label %47

42:                                               ; preds = %29
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.pthread_internal, ptr %43, i32 0, i32 3
  %45 = load ptr, ptr %44, align 8, !tbaa !16
  call void @vTaskDelete(ptr noundef %45)
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  call void @vPortFree(ptr noundef %46)
  br label %51

47:                                               ; preds = %29
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.pthread_internal, ptr %48, i32 0, i32 0
  %50 = call i32 @pthread_attr_setdetachstate(ptr noundef %49, i32 noundef 0)
  br label %51

51:                                               ; preds = %47, %42
  br label %52

52:                                               ; preds = %51, %28
  %53 = call i64 @xTaskResumeAll()
  br label %54

54:                                               ; preds = %52, %15
  %55 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %55
}

declare i32 @eTaskGetState(ptr 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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !14, i64 2}
!12 = !{!"pthread_internal", !13, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !15, i64 32, !15, i64 176, !6, i64 320}
!13 = !{!"pthread_attr_internal", !14, i64 0, !14, i64 2}
!14 = !{!"short", !7, i64 0}
!15 = !{!"xSTATIC_QUEUE", !7, i64 0, !7, i64 24, !7, i64 32, !7, i64 112, !7, i64 136, !7, i64 138}
!16 = !{!12, !6, 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"

; Function Attrs: nounwind uwtable
declare dso_local i32 @pthread_attr_setdetachstate(ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @vTaskSuspendAll() local_unnamed_addr #1

declare void @vPortFree(ptr noundef) local_unnamed_addr #1

declare i64 @xTaskResumeAll() local_unnamed_addr #1

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 noundef) local_unnamed_addr #1

declare void @vQueueDelete(ptr noundef) local_unnamed_addr #1

declare void @vTaskDelete(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 23) i32 @pthread_detach(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 2
  %3 = load i16, ptr %2, align 2, !tbaa !5
  %.not = icmp slt i16 %3, 0
  br i1 %.not, label %4, label %21

4:                                                ; preds = %1
  tail call void @vTaskSuspendAll() #2
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load ptr, ptr %5, align 8, !tbaa !13
  %7 = tail call i32 @eTaskGetState(ptr noundef %6) #2
  %8 = and i32 %7, -2
  %or.cond = icmp eq i32 %8, 4
  br i1 %or.cond, label %19, label %9

9:                                                ; preds = %4
  %10 = getelementptr inbounds i8, ptr %0, i64 32
  %11 = tail call i64 @xQueueGenericSend(ptr noundef nonnull %10, ptr noundef null, i32 noundef 0, i64 noundef 0) #2
  tail call void @vQueueDelete(ptr noundef nonnull %10) #2
  %12 = getelementptr inbounds i8, ptr %0, i64 176
  %13 = tail call i64 @xQueueGenericSend(ptr noundef nonnull %12, ptr noundef null, i32 noundef 0, i64 noundef 0) #2
  tail call void @vQueueDelete(ptr noundef nonnull %12) #2
  %14 = icmp eq i32 %7, 3
  br i1 %14, label %15, label %17

15:                                               ; preds = %9
  %16 = load ptr, ptr %5, align 8, !tbaa !13
  tail call void @vTaskDelete(ptr noundef %16) #2
  tail call void @vPortFree(ptr noundef nonnull %0) #2
  br label %19

17:                                               ; preds = %9
  %18 = tail call i32 @pthread_attr_setdetachstate(ptr noundef nonnull %0, i32 noundef 0)
  br label %19

19:                                               ; preds = %4, %15, %17
  %.1 = phi i32 [ 0, %15 ], [ 0, %17 ], [ 22, %4 ]
  %20 = tail call i64 @xTaskResumeAll() #2
  br label %21

21:                                               ; preds = %19, %1
  %.2 = phi i32 [ %.1, %19 ], [ 22, %1 ]
  ret i32 %.2
}

declare i32 @eTaskGetState(ptr 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 = !{!6, !8, i64 2}
!6 = !{!"pthread_internal", !7, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !12, i64 32, !12, i64 176, !11, i64 320}
!7 = !{!"pthread_attr_internal", !8, i64 0, !8, i64 2}
!8 = !{!"short", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!"xSTATIC_QUEUE", !9, i64 0, !9, i64 24, !9, i64 32, !9, i64 112, !9, i64 136, !9, i64 138}
!13 = !{!6, !11, i64 24}


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