; 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_attr_internal = type { i16, i16 }
%struct.sched_param = type { i32 }
%struct.pthread_internal = type { %struct.pthread_attr_internal, ptr, ptr, ptr, %struct.xSTATIC_QUEUE, %struct.xSTATIC_QUEUE, ptr }
%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] }

@xDefaultThreadAttributes = external hidden constant %struct.pthread_attr_internal, align 2
@.str = external hidden unnamed_addr constant [8 x i8], align 1

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @pthread_create(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca %struct.sched_param, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  store ptr null, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  call void @llvm.memset.p0.i64(ptr align 4 %11, i8 0, i64 4, i1 false)
  %12 = call ptr @pvPortMalloc(i64 noundef 328)
  store ptr %12, ptr %10, align 8, !tbaa !5
  %13 = load ptr, ptr %10, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %16

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

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

19:                                               ; preds = %16
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %25

22:                                               ; preds = %19
  %23 = load ptr, ptr %10, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.pthread_internal, ptr %23, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %24, ptr align 2 @xDefaultThreadAttributes, i64 4, i1 false), !tbaa.struct !11
  br label %29

25:                                               ; preds = %19
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.pthread_internal, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %27, ptr align 2 %28, i64 4, i1 false), !tbaa.struct !11
  br label %29

29:                                               ; preds = %25, %22
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.pthread_internal, ptr %30, i32 0, i32 0
  %32 = getelementptr inbounds %struct.pthread_attr_internal, ptr %31, i32 0, i32 1
  %33 = load i16, ptr %32, align 2, !tbaa !14
  %34 = zext i16 %33 to i32
  %35 = and i32 %34, 32767
  %36 = getelementptr inbounds %struct.sched_param, ptr %11, i32 0, i32 0
  store i32 %35, ptr %36, align 4, !tbaa !18
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.pthread_internal, ptr %38, i32 0, i32 2
  store ptr %37, ptr %39, align 8, !tbaa !20
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = load ptr, ptr %10, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.pthread_internal, ptr %41, i32 0, i32 1
  store ptr %40, ptr %42, align 8, !tbaa !21
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.pthread_internal, ptr %43, i32 0, i32 0
  %45 = getelementptr inbounds %struct.pthread_attr_internal, ptr %44, i32 0, i32 1
  %46 = load i16, ptr %45, align 2, !tbaa !14
  %47 = zext i16 %46 to i32
  %48 = and i32 %47, 32768
  %49 = icmp eq i32 %48, 32768
  br i1 %49, label %50, label %57

50:                                               ; preds = %29
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.pthread_internal, ptr %51, i32 0, i32 5
  %53 = call ptr @xQueueCreateMutexStatic(i8 noundef zeroext 1, ptr noundef %52)
  %54 = load ptr, ptr %10, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.pthread_internal, ptr %54, i32 0, i32 4
  %56 = call ptr @xQueueGenericCreateStatic(i64 noundef 1, i64 noundef 0, ptr noundef null, ptr noundef %55, i8 noundef zeroext 3)
  br label %57

57:                                               ; preds = %50, %29
  br label %58

58:                                               ; preds = %57, %16
  %59 = load i32, ptr %9, align 4, !tbaa !9
  %60 = icmp eq i32 %59, 0
  br i1 %60, label %61, label %88

61:                                               ; preds = %58
  call void @vTaskSuspendAll()
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.pthread_internal, ptr %62, i32 0, i32 0
  %64 = getelementptr inbounds %struct.pthread_attr_internal, ptr %63, i32 0, i32 0
  %65 = load i16, ptr %64, align 8, !tbaa !22
  %66 = zext i16 %65 to i64
  %67 = udiv i64 %66, 4
  %68 = trunc i64 %67 to i16
  %69 = load ptr, ptr %10, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.sched_param, ptr %11, i32 0, i32 0
  %71 = load i32, ptr %70, align 4, !tbaa !18
  %72 = sext i32 %71 to i64
  %73 = load ptr, ptr %10, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.pthread_internal, ptr %73, i32 0, i32 3
  %75 = call i64 @xTaskCreate(ptr noundef @prvRunThread, ptr noundef @.str, i16 noundef zeroext %68, ptr noundef %69, i64 noundef %72, ptr noundef %74)
  %76 = icmp ne i64 %75, 1
  br i1 %76, label %77, label %79

77:                                               ; preds = %61
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  call void @vPortFree(ptr noundef %78)
  store i32 11, ptr %9, align 4, !tbaa !9
  br label %86

79:                                               ; preds = %61
  %80 = load ptr, ptr %10, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.pthread_internal, ptr %80, i32 0, i32 3
  %82 = load ptr, ptr %81, align 8, !tbaa !23
  %83 = load ptr, ptr %10, align 8, !tbaa !5
  call void @vTaskSetApplicationTaskTag(ptr noundef %82, ptr noundef %83)
  %84 = load ptr, ptr %10, align 8, !tbaa !5
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %84, ptr %85, align 8, !tbaa !5
  br label %86

86:                                               ; preds = %79, %77
  %87 = call i64 @xTaskResumeAll()
  br label %88

88:                                               ; preds = %86, %58
  %89 = load i32, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  ret i32 %89
}

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

declare ptr @pvPortMalloc(i64 noundef) #4

declare ptr @xQueueCreateMutexStatic(i8 noundef zeroext, ptr noundef) #4

declare ptr @xQueueGenericCreateStatic(i64 noundef, i64 noundef, ptr noundef, ptr noundef, i8 noundef zeroext) #4

declare void @vTaskSuspendAll() #4

declare i64 @xTaskCreate(ptr noundef, ptr noundef, i16 noundef zeroext, ptr noundef, i64 noundef, ptr noundef) #4

; Function Attrs: nounwind uwtable
declare hidden void @prvRunThread(ptr noundef) #2

declare void @vPortFree(ptr noundef) #4

declare void @vTaskSetApplicationTaskTag(ptr noundef, ptr noundef) #4

declare i64 @xTaskResumeAll() #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { "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 #5 = { 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 = !{i64 0, i64 2, !12, i64 2, i64 2, !12}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !13, i64 2}
!15 = !{!"pthread_internal", !16, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !17, i64 32, !17, i64 176, !6, i64 320}
!16 = !{!"pthread_attr_internal", !13, i64 0, !13, i64 2}
!17 = !{!"xSTATIC_QUEUE", !7, i64 0, !7, i64 24, !7, i64 32, !7, i64 112, !7, i64 136, !7, i64 138}
!18 = !{!19, !10, i64 0}
!19 = !{!"sched_param", !10, i64 0}
!20 = !{!15, !6, i64 16}
!21 = !{!15, !6, i64 8}
!22 = !{!15, !13, i64 0}
!23 = !{!15, !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"

%struct.pthread_attr_internal = type { i16, i16 }

@xDefaultThreadAttributes = external hidden local_unnamed_addr constant %struct.pthread_attr_internal, align 2
@.str = external hidden unnamed_addr constant [8 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 12) i32 @pthread_create(ptr nocapture noundef writeonly %0, ptr noundef readonly %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = tail call ptr @pvPortMalloc(i64 noundef 328) #2
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %28, label %6

6:                                                ; preds = %4
  %7 = icmp eq ptr %1, null
  %xDefaultThreadAttributes. = select i1 %7, ptr @xDefaultThreadAttributes, ptr %1
  %storemerge = load i32, ptr %xDefaultThreadAttributes., align 2
  store i32 %storemerge, ptr %5, align 8
  %8 = lshr i32 %storemerge, 16
  %9 = and i32 %8, 32767
  %10 = zext nneg i32 %9 to i64
  %11 = getelementptr inbounds i8, ptr %5, i64 16
  store ptr %3, ptr %11, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %5, i64 8
  store ptr %2, ptr %12, align 8, !tbaa !13
  %.not24 = icmp sgt i32 %storemerge, -1
  br i1 %.not24, label %18, label %13

13:                                               ; preds = %6
  %14 = getelementptr inbounds i8, ptr %5, i64 176
  %15 = tail call ptr @xQueueCreateMutexStatic(i8 noundef zeroext 1, ptr noundef nonnull %14) #2
  %16 = getelementptr inbounds i8, ptr %5, i64 32
  %17 = tail call ptr @xQueueGenericCreateStatic(i64 noundef 1, i64 noundef 0, ptr noundef null, ptr noundef nonnull %16, i8 noundef zeroext 3) #2
  br label %18

18:                                               ; preds = %6, %13
  tail call void @vTaskSuspendAll() #2
  %19 = load i16, ptr %5, align 8, !tbaa !14
  %20 = lshr i16 %19, 2
  %21 = getelementptr inbounds i8, ptr %5, i64 24
  %22 = tail call i64 @xTaskCreate(ptr noundef nonnull @prvRunThread, ptr noundef nonnull @.str, i16 noundef zeroext %20, ptr noundef nonnull %5, i64 noundef %10, ptr noundef nonnull %21) #2
  %.not25 = icmp eq i64 %22, 1
  br i1 %.not25, label %24, label %23

23:                                               ; preds = %18
  tail call void @vPortFree(ptr noundef nonnull %5) #2
  br label %26

24:                                               ; preds = %18
  %25 = load ptr, ptr %21, align 8, !tbaa !15
  tail call void @vTaskSetApplicationTaskTag(ptr noundef %25, ptr noundef nonnull %5) #2
  store ptr %5, ptr %0, align 8, !tbaa !16
  br label %26

26:                                               ; preds = %24, %23
  %.1 = phi i32 [ 11, %23 ], [ 0, %24 ]
  %27 = tail call i64 @xTaskResumeAll() #2
  br label %28

28:                                               ; preds = %4, %26
  %.2 = phi i32 [ %.1, %26 ], [ 11, %4 ]
  ret i32 %.2
}

declare ptr @pvPortMalloc(i64 noundef) local_unnamed_addr #1

declare ptr @xQueueCreateMutexStatic(i8 noundef zeroext, ptr noundef) local_unnamed_addr #1

declare ptr @xQueueGenericCreateStatic(i64 noundef, i64 noundef, ptr noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #1

declare void @vTaskSuspendAll() local_unnamed_addr #1

declare i64 @xTaskCreate(ptr noundef, ptr noundef, i16 noundef zeroext, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @prvRunThread(ptr noundef) #0

declare void @vPortFree(ptr noundef) local_unnamed_addr #1

declare void @vTaskSetApplicationTaskTag(ptr noundef, ptr noundef) local_unnamed_addr #1

declare i64 @xTaskResumeAll() 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, !11, i64 16}
!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 8}
!14 = !{!6, !8, i64 0}
!15 = !{!6, !11, i64 24}
!16 = !{!11, !11, i64 0}


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