; 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

declare void @vTaskSuspendAll() #1

declare void @vPortFree(ptr noundef) #1

declare i64 @xTaskResumeAll() #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @pthread_equal(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @pthread_self() #2

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

15:                                               ; preds = %2
  store i32 45, ptr %5, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %15, %2
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %26

19:                                               ; preds = %16
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.pthread_internal, ptr %20, i32 0, i32 5
  %22 = call i64 @xQueueSemaphoreTake(ptr noundef %21, i32 noundef 0)
  %23 = icmp ne i64 %22, 1
  br i1 %23, label %24, label %25

24:                                               ; preds = %19
  store i32 45, ptr %5, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %24, %19
  br label %26

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

29:                                               ; preds = %26
  %30 = call ptr @pthread_self()
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = call i32 @pthread_equal(ptr noundef %30, ptr noundef %31)
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %35

34:                                               ; preds = %29
  store i32 45, ptr %5, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %34, %29
  br label %36

36:                                               ; preds = %35, %26
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %39, label %66

39:                                               ; preds = %36
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.pthread_internal, ptr %40, i32 0, i32 4
  %42 = call i64 @xQueueSemaphoreTake(ptr noundef %41, i32 noundef -1)
  call void @vTaskSuspendAll()
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.pthread_internal, ptr %43, i32 0, i32 4
  %45 = call i64 @xQueueGenericSend(ptr noundef %44, ptr noundef null, i32 noundef 0, i64 noundef 0)
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.pthread_internal, ptr %46, i32 0, i32 4
  call void @vQueueDelete(ptr noundef %47)
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.pthread_internal, ptr %48, i32 0, i32 5
  %50 = call i64 @xQueueGenericSend(ptr noundef %49, ptr noundef null, i32 noundef 0, i64 noundef 0)
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.pthread_internal, ptr %51, i32 0, i32 5
  call void @vQueueDelete(ptr noundef %52)
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.pthread_internal, ptr %53, i32 0, i32 3
  %55 = load ptr, ptr %54, align 8, !tbaa !16
  call void @vTaskDelete(ptr noundef %55)
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = icmp ne ptr %56, null
  br i1 %57, label %58, label %63

58:                                               ; preds = %39
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.pthread_internal, ptr %59, i32 0, i32 6
  %61 = load ptr, ptr %60, align 8, !tbaa !17
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %61, ptr %62, align 8, !tbaa !5
  br label %63

63:                                               ; preds = %58, %39
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  call void @vPortFree(ptr noundef %64)
  %65 = call i64 @xTaskResumeAll()
  br label %66

66:                                               ; preds = %63, %36
  %67 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret i32 %67
}

declare i64 @xQueueSemaphoreTake(ptr noundef, i32 noundef) #1

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

declare void @vQueueDelete(ptr noundef) #1

declare void @vTaskDelete(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 = !{!"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}
!17 = !{!12, !6, i64 320}


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

declare void @vTaskSuspendAll() local_unnamed_addr #0

declare void @vPortFree(ptr noundef) local_unnamed_addr #0

declare i64 @xTaskResumeAll() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @pthread_self() local_unnamed_addr #1

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

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 176
  %7 = tail call i64 @xQueueSemaphoreTake(ptr noundef nonnull %6, i32 noundef 0) #2
  %.not18 = icmp eq i64 %7, 1
  br i1 %.not18, label %.thread, label %.thread27

.thread:                                          ; preds = %5
  %8 = tail call ptr @pthread_self()
  %9 = tail call i32 @pthread_equal(ptr noundef %8, ptr noundef nonnull %0)
  %.not19 = icmp eq i32 %9, 0
  br i1 %.not19, label %10, label %.thread27

10:                                               ; preds = %.thread
  %11 = getelementptr inbounds i8, ptr %0, i64 32
  %12 = tail call i64 @xQueueSemaphoreTake(ptr noundef nonnull %11, i32 noundef -1) #2
  tail call void @vTaskSuspendAll() #2
  %13 = tail call i64 @xQueueGenericSend(ptr noundef nonnull %11, ptr noundef null, i32 noundef 0, i64 noundef 0) #2
  tail call void @vQueueDelete(ptr noundef nonnull %11) #2
  %14 = tail call i64 @xQueueGenericSend(ptr noundef nonnull %6, ptr noundef null, i32 noundef 0, i64 noundef 0) #2
  tail call void @vQueueDelete(ptr noundef nonnull %6) #2
  %15 = getelementptr inbounds i8, ptr %0, i64 24
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  tail call void @vTaskDelete(ptr noundef %16) #2
  %.not20 = icmp eq ptr %1, null
  br i1 %.not20, label %20, label %17

17:                                               ; preds = %10
  %18 = getelementptr inbounds i8, ptr %0, i64 320
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  store ptr %19, ptr %1, align 8, !tbaa !15
  br label %20

20:                                               ; preds = %17, %10
  tail call void @vPortFree(ptr noundef nonnull %0) #2
  %21 = tail call i64 @xTaskResumeAll() #2
  br label %.thread27

.thread27:                                        ; preds = %2, %5, %.thread, %20
  %.229 = phi i32 [ 0, %20 ], [ 45, %.thread ], [ 45, %5 ], [ 45, %2 ]
  ret i32 %.229
}

declare i64 @xQueueSemaphoreTake(ptr noundef, i32 noundef) local_unnamed_addr #0

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

declare void @vQueueDelete(ptr noundef) local_unnamed_addr #0

declare void @vTaskDelete(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, !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}
!14 = !{!6, !11, i64 320}
!15 = !{!11, !11, i64 0}


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