; 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.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: nounwind uwtable
declare hidden void @prvInitializeStaticMutex(ptr noundef) #1

declare ptr @xTaskGetCurrentTaskHandle() #2

; Function Attrs: nounwind uwtable
define dso_local i32 @pthread_mutex_unlock(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  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
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %5, ptr %4, align 8, !tbaa !5
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  call void @prvInitializeStaticMutex(ptr noundef %6)
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %7, i32 0, i32 3
  %9 = getelementptr inbounds %struct.pthread_mutexattr_internal, ptr %8, i32 0, i32 0
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = icmp eq i32 %10, 1
  br i1 %11, label %18, label %12

12:                                               ; preds = %1
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %13, i32 0, i32 3
  %15 = getelementptr inbounds %struct.pthread_mutexattr_internal, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = icmp eq i32 %16, 2
  br i1 %17, label %18, label %25

18:                                               ; preds = %12, %1
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %19, i32 0, i32 2
  %21 = load ptr, ptr %20, align 8, !tbaa !16
  %22 = call ptr @xTaskGetCurrentTaskHandle()
  %23 = icmp ne ptr %21, %22
  br i1 %23, label %24, label %25

24:                                               ; preds = %18
  store i32 1, ptr %3, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %24, %18, %12
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %49

28:                                               ; preds = %25
  call void @vTaskSuspendAll()
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %29, i32 0, i32 3
  %31 = getelementptr inbounds %struct.pthread_mutexattr_internal, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 8, !tbaa !11
  %33 = icmp eq i32 %32, 2
  br i1 %33, label %34, label %38

34:                                               ; preds = %28
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %35, i32 0, i32 1
  %37 = call i64 @xQueueGiveMutexRecursive(ptr noundef %36)
  br label %42

38:                                               ; preds = %28
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %39, i32 0, i32 1
  %41 = call i64 @xQueueGenericSend(ptr noundef %40, ptr noundef null, i32 noundef 0, i64 noundef 0)
  br label %42

42:                                               ; preds = %38, %34
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %43, i32 0, i32 1
  %45 = call ptr @xQueueGetMutexHolder(ptr noundef %44)
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %46, i32 0, i32 2
  store ptr %45, ptr %47, align 8, !tbaa !16
  %48 = call i64 @xTaskResumeAll()
  br label %49

49:                                               ; preds = %42, %25
  %50 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %50
}

declare void @vTaskSuspendAll() #2

declare i64 @xQueueGiveMutexRecursive(ptr noundef) #2

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

declare ptr @xQueueGetMutexHolder(ptr noundef) #2

declare i64 @xTaskResumeAll() #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, !10, i64 160}
!12 = !{!"pthread_mutex_internal", !13, i64 0, !14, i64 8, !6, i64 152, !15, i64 160}
!13 = !{!"long", !7, i64 0}
!14 = !{!"xSTATIC_QUEUE", !7, i64 0, !7, i64 24, !7, i64 32, !7, i64 112, !7, i64 136, !7, i64 138}
!15 = !{!"pthread_mutexattr_internal", !10, i64 0}
!16 = !{!12, !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"

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

declare ptr @xTaskGetCurrentTaskHandle() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @pthread_mutex_unlock(ptr noundef %0) local_unnamed_addr #0 {
  tail call void @prvInitializeStaticMutex(ptr noundef %0)
  %2 = getelementptr inbounds i8, ptr %0, i64 160
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %.off = add i32 %3, -1
  %switch = icmp ult i32 %.off, 2
  br i1 %switch, label %4, label %.thread

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 152
  %6 = load ptr, ptr %5, align 8, !tbaa !14
  %7 = tail call ptr @xTaskGetCurrentTaskHandle() #2
  %.not = icmp eq ptr %6, %7
  br i1 %.not, label %.thread, label %20

.thread:                                          ; preds = %1, %4
  tail call void @vTaskSuspendAll() #2
  %8 = load i32, ptr %2, align 8, !tbaa !5
  %9 = icmp eq i32 %8, 2
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  br i1 %9, label %11, label %13

11:                                               ; preds = %.thread
  %12 = tail call i64 @xQueueGiveMutexRecursive(ptr noundef nonnull %10) #2
  br label %15

13:                                               ; preds = %.thread
  %14 = tail call i64 @xQueueGenericSend(ptr noundef nonnull %10, ptr noundef null, i32 noundef 0, i64 noundef 0) #2
  br label %15

15:                                               ; preds = %13, %11
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  %17 = tail call ptr @xQueueGetMutexHolder(ptr noundef nonnull %16) #2
  %18 = getelementptr inbounds i8, ptr %0, i64 152
  store ptr %17, ptr %18, align 8, !tbaa !14
  %19 = tail call i64 @xTaskResumeAll() #2
  br label %20

20:                                               ; preds = %15, %4
  %.014 = phi i32 [ 0, %15 ], [ 1, %4 ]
  ret i32 %.014
}

declare void @vTaskSuspendAll() local_unnamed_addr #1

declare i64 @xQueueGiveMutexRecursive(ptr noundef) local_unnamed_addr #1

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

declare ptr @xQueueGetMutexHolder(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, !13, i64 160}
!6 = !{!"pthread_mutex_internal", !7, i64 0, !10, i64 8, !11, i64 152, !12, i64 160}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"xSTATIC_QUEUE", !8, i64 0, !8, i64 24, !8, i64 32, !8, i64 112, !8, i64 136, !8, i64 138}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"pthread_mutexattr_internal", !13, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!6, !11, i64 152}


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