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

; 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.timespec = type { i64, i64 }
%struct.pthread_mutex_internal = type { i64, %struct.xSTATIC_QUEUE, ptr, %struct.pthread_mutexattr_internal }
%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] }
%struct.pthread_mutexattr_internal = type { i32 }

; 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: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @pthread_mutex_timedlock(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
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca %struct.timespec, 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) #5
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %10, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 -1, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  store i64 0, ptr %8, align 8, !tbaa !11
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  call void @prvInitializeStaticMutex(ptr noundef %11)
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %12, i32 0, i32 0
  %14 = load i64, ptr %13, align 8, !tbaa !13
  %15 = icmp eq i64 %14, 1
  %16 = zext i1 %15 to i32
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %20

18:                                               ; preds = %2
  call void @vPortRaiseBASEPRI()
  br label %19

19:                                               ; preds = %19, %18
  br label %19

20:                                               ; preds = %2
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %35

23:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 16, ptr %9) #5
  call void @llvm.memset.p0.i64(ptr align 8 %9, i8 0, i64 16, i1 false)
  %24 = call i32 @clock_gettime(i32 noundef 0, ptr noundef %9)
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %23
  store i32 22, ptr %5, align 4, !tbaa !9
  br label %30

27:                                               ; preds = %23
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = call i32 @UTILS_AbsoluteTimespecToDeltaTicks(ptr noundef %28, ptr noundef %9, ptr noundef %7)
  store i32 %29, ptr %5, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %27, %26
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = icmp eq i32 %31, 116
  br i1 %32, label %33, label %34

33:                                               ; preds = %30
  store i32 0, ptr %7, align 4, !tbaa !9
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %33, %30
  call void @llvm.lifetime.end.p0(i64 16, ptr %9) #5
  br label %35

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

38:                                               ; preds = %35
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %39, i32 0, i32 3
  %41 = getelementptr inbounds %struct.pthread_mutexattr_internal, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !17
  %43 = icmp eq i32 %42, 1
  br i1 %43, label %44, label %51

44:                                               ; preds = %38
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %45, i32 0, i32 2
  %47 = load ptr, ptr %46, align 8, !tbaa !18
  %48 = call ptr @xTaskGetCurrentTaskHandle()
  %49 = icmp eq ptr %47, %48
  br i1 %49, label %50, label %51

50:                                               ; preds = %44
  store i32 45, ptr %5, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %50, %44, %38, %35
  %52 = load i32, ptr %5, align 4, !tbaa !9
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %79

54:                                               ; preds = %51
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %55, i32 0, i32 3
  %57 = getelementptr inbounds %struct.pthread_mutexattr_internal, ptr %56, i32 0, i32 0
  %58 = load i32, ptr %57, align 8, !tbaa !17
  %59 = icmp eq i32 %58, 2
  br i1 %59, label %60, label %65

60:                                               ; preds = %54
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %61, i32 0, i32 1
  %63 = load i32, ptr %7, align 4, !tbaa !9
  %64 = call i64 @xQueueTakeMutexRecursive(ptr noundef %62, i32 noundef %63)
  store i64 %64, ptr %8, align 8, !tbaa !11
  br label %70

65:                                               ; preds = %54
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %66, i32 0, i32 1
  %68 = load i32, ptr %7, align 4, !tbaa !9
  %69 = call i64 @xQueueSemaphoreTake(ptr noundef %67, i32 noundef %68)
  store i64 %69, ptr %8, align 8, !tbaa !11
  br label %70

70:                                               ; preds = %65, %60
  %71 = load i64, ptr %8, align 8, !tbaa !11
  %72 = icmp eq i64 %71, 1
  br i1 %72, label %73, label %77

73:                                               ; preds = %70
  %74 = call ptr @xTaskGetCurrentTaskHandle()
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %75, i32 0, i32 2
  store ptr %74, ptr %76, align 8, !tbaa !18
  br label %78

77:                                               ; preds = %70
  store i32 116, ptr %5, align 4, !tbaa !9
  br label %78

78:                                               ; preds = %77, %73
  br label %79

79:                                               ; preds = %78, %51
  %80 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  ret i32 %80
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @vPortRaiseBASEPRI() #3

declare i32 @clock_gettime(i32 noundef, ptr noundef) #4

declare i32 @UTILS_AbsoluteTimespecToDeltaTicks(ptr noundef, ptr noundef, ptr noundef) #4

declare ptr @xTaskGetCurrentTaskHandle() #4

declare i64 @xQueueTakeMutexRecursive(ptr noundef, i32 noundef) #4

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { alwaysinline 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 #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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"pthread_mutex_internal", !12, i64 0, !15, i64 8, !6, i64 152, !16, i64 160}
!15 = !{!"xSTATIC_QUEUE", !7, i64 0, !7, i64 24, !7, i64 32, !7, i64 112, !7, i64 136, !7, i64 138}
!16 = !{!"pthread_mutexattr_internal", !10, i64 0}
!17 = !{!14, !10, i64 160}
!18 = !{!14, !6, i64 152}


; 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.timespec = type { i64, i64 }

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @pthread_mutex_timedlock(ptr noundef %0, ptr noundef %1) local_unnamed_addr #2 {
  %3 = alloca i32, align 4
  %4 = alloca %struct.timespec, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #5
  store i32 -1, ptr %3, align 4, !tbaa !5
  tail call void @prvInitializeStaticMutex(ptr noundef %0)
  %5 = load i64, ptr %0, align 8, !tbaa !9
  %.not = icmp eq i64 %5, 1
  br i1 %.not, label %8, label %6

6:                                                ; preds = %2
  tail call void @vPortRaiseBASEPRI()
  br label %7

7:                                                ; preds = %7, %6
  br label %7

8:                                                ; preds = %2
  %.not17 = icmp eq ptr %1, null
  br i1 %.not17, label %.thread20, label %9

9:                                                ; preds = %8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  %10 = call i32 @clock_gettime(i32 noundef 0, ptr noundef nonnull %4) #5
  %.not18 = icmp eq i32 %10, 0
  br i1 %.not18, label %11, label %.thread24

.thread24:                                        ; preds = %9
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #5
  br label %.thread29

11:                                               ; preds = %9
  %12 = call i32 @UTILS_AbsoluteTimespecToDeltaTicks(ptr noundef nonnull %1, ptr noundef nonnull %4, ptr noundef nonnull %3) #5
  %13 = icmp eq i32 %12, 116
  br i1 %13, label %.thread22, label %14

.thread22:                                        ; preds = %11
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #5
  br label %.thread20

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #5
  %15 = icmp eq i32 %12, 0
  br i1 %15, label %.thread20, label %.thread29

.thread20:                                        ; preds = %8, %.thread22, %14
  %16 = getelementptr inbounds i8, ptr %0, i64 160
  %17 = load i32, ptr %16, align 8, !tbaa !15
  %18 = icmp eq i32 %17, 1
  br i1 %18, label %19, label %.thread26

19:                                               ; preds = %.thread20
  %20 = getelementptr inbounds i8, ptr %0, i64 152
  %21 = load ptr, ptr %20, align 8, !tbaa !16
  %22 = call ptr @xTaskGetCurrentTaskHandle() #5
  %23 = icmp eq ptr %21, %22
  br i1 %23, label %.thread29, label %..thread26_crit_edge

..thread26_crit_edge:                             ; preds = %19
  %.pre = load i32, ptr %16, align 8, !tbaa !15
  br label %.thread26

.thread26:                                        ; preds = %..thread26_crit_edge, %.thread20
  %24 = phi i32 [ %.pre, %..thread26_crit_edge ], [ %17, %.thread20 ]
  %25 = icmp eq i32 %24, 2
  %26 = getelementptr inbounds i8, ptr %0, i64 8
  %27 = load i32, ptr %3, align 4, !tbaa !5
  br i1 %25, label %28, label %30

28:                                               ; preds = %.thread26
  %29 = call i64 @xQueueTakeMutexRecursive(ptr noundef nonnull %26, i32 noundef %27) #5
  br label %32

30:                                               ; preds = %.thread26
  %31 = call i64 @xQueueSemaphoreTake(ptr noundef nonnull %26, i32 noundef %27) #5
  br label %32

32:                                               ; preds = %30, %28
  %.0 = phi i64 [ %29, %28 ], [ %31, %30 ]
  %33 = icmp eq i64 %.0, 1
  br i1 %33, label %34, label %.thread29

34:                                               ; preds = %32
  %35 = call ptr @xTaskGetCurrentTaskHandle() #5
  %36 = getelementptr inbounds i8, ptr %0, i64 152
  store ptr %35, ptr %36, align 8, !tbaa !16
  br label %.thread29

.thread29:                                        ; preds = %14, %19, %.thread24, %32, %34
  %.4 = phi i32 [ 0, %34 ], [ 116, %32 ], [ 22, %.thread24 ], [ 45, %19 ], [ %12, %14 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #5
  ret i32 %.4
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @vPortRaiseBASEPRI() local_unnamed_addr #3

declare i32 @clock_gettime(i32 noundef, ptr noundef) local_unnamed_addr #4

declare i32 @UTILS_AbsoluteTimespecToDeltaTicks(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #4

declare ptr @xTaskGetCurrentTaskHandle() local_unnamed_addr #4

declare i64 @xQueueTakeMutexRecursive(ptr noundef, i32 noundef) local_unnamed_addr #4

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { alwaysinline 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"pthread_mutex_internal", !11, i64 0, !12, i64 8, !13, i64 152, !14, i64 160}
!11 = !{!"long", !7, i64 0}
!12 = !{!"xSTATIC_QUEUE", !7, i64 0, !7, i64 24, !7, i64 32, !7, i64 112, !7, i64 136, !7, i64 138}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!"pthread_mutexattr_internal", !6, i64 0}
!15 = !{!10, !6, i64 160}
!16 = !{!10, !13, i64 152}


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