; 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_mutexattr_internal = type { i32 }
%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] }

@xDefaultMutexAttributes = external hidden constant %struct.pthread_mutexattr_internal, align 4

; 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
define dso_local i32 @pthread_mutex_init(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca %struct.pthread_mutex_internal, 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
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %8, ptr %6, align 8, !tbaa !5
  %9 = load ptr, ptr %6, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store i32 12, ptr %5, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %11, %2
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %61

15:                                               ; preds = %12
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %7, i8 0, i64 168, i1 false)
  %17 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %7, i32 0, i32 0
  %18 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %7, i32 0, i32 1
  %19 = getelementptr inbounds %struct.xSTATIC_QUEUE, ptr %18, i32 0, i32 0
  %20 = getelementptr inbounds %struct.xSTATIC_QUEUE, ptr %18, i32 0, i32 1
  %21 = getelementptr inbounds %struct.xSTATIC_QUEUE, ptr %18, i32 0, i32 2
  %22 = getelementptr inbounds %struct.xSTATIC_QUEUE, ptr %18, i32 0, i32 3
  %23 = getelementptr inbounds %struct.xSTATIC_QUEUE, ptr %18, i32 0, i32 4
  %24 = getelementptr inbounds %struct.xSTATIC_QUEUE, ptr %18, i32 0, i32 5
  %25 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %7, i32 0, i32 2
  %26 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %7, i32 0, i32 3
  %27 = getelementptr inbounds %struct.pthread_mutexattr_internal, ptr %26, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %16, ptr align 8 %7, i64 168, i1 false), !tbaa.struct !11
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = icmp eq ptr %28, null
  br i1 %29, label %30, label %33

30:                                               ; preds = %15
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %31, i32 0, i32 3
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %32, ptr align 4 @xDefaultMutexAttributes, i64 4, i1 false), !tbaa.struct !15
  br label %37

33:                                               ; preds = %15
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %34, i32 0, i32 3
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %35, ptr align 4 %36, i64 4, i1 false), !tbaa.struct !15
  br label %37

37:                                               ; preds = %33, %30
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %38, i32 0, i32 3
  %40 = getelementptr inbounds %struct.pthread_mutexattr_internal, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %40, align 8, !tbaa !16
  %42 = icmp eq i32 %41, 2
  br i1 %42, label %43, label %47

43:                                               ; preds = %37
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %44, i32 0, i32 1
  %46 = call ptr @xQueueCreateMutexStatic(i8 noundef zeroext 4, ptr noundef %45)
  br label %51

47:                                               ; preds = %37
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %48, i32 0, i32 1
  %50 = call ptr @xQueueCreateMutexStatic(i8 noundef zeroext 1, ptr noundef %49)
  br label %51

51:                                               ; preds = %47, %43
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %52, i32 0, i32 1
  %54 = icmp eq ptr %53, null
  br i1 %54, label %55, label %57

55:                                               ; preds = %51
  store i32 11, ptr %5, align 4, !tbaa !9
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  call void @vPortFree(ptr noundef %56)
  br label %60

57:                                               ; preds = %51
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.pthread_mutex_internal, ptr %58, i32 0, i32 0
  store i64 1, ptr %59, align 8, !tbaa !20
  br label %60

60:                                               ; preds = %57, %55
  br label %61

61:                                               ; preds = %60, %12
  %62 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  ret i32 %62
}

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

; 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) #3

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

declare void @vPortFree(ptr noundef) #4

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 8, !12, i64 8, i64 24, !14, i64 32, i64 8, !14, i64 40, i64 80, !14, i64 120, i64 24, !14, i64 144, i64 2, !14, i64 146, i64 1, !14, i64 152, i64 8, !5, i64 160, i64 4, !9}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{i64 0, i64 4, !9}
!16 = !{!17, !10, i64 160}
!17 = !{!"pthread_mutex_internal", !13, i64 0, !18, i64 8, !6, i64 152, !19, i64 160}
!18 = !{!"xSTATIC_QUEUE", !7, i64 0, !7, i64 24, !7, i64 32, !7, i64 112, !7, i64 136, !7, i64 138}
!19 = !{!"pthread_mutexattr_internal", !10, i64 0}
!20 = !{!17, !13, i64 0}


; 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_mutexattr_internal = type { i32 }

@xDefaultMutexAttributes = external hidden local_unnamed_addr constant %struct.pthread_mutexattr_internal, align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 13) i32 @pthread_mutex_init(ptr noundef %0, ptr noundef readonly %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %9, label %3

3:                                                ; preds = %2
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(168) %0, i8 0, i64 168, i1 false)
  %4 = icmp eq ptr %1, null
  %spec.select = select i1 %4, ptr @xDefaultMutexAttributes, ptr %1
  %.sink = load i32, ptr %spec.select, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 160
  store i32 %.sink, ptr %5, align 8
  %6 = icmp eq i32 %.sink, 2
  %. = select i1 %6, i8 4, i8 1
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = tail call ptr @xQueueCreateMutexStatic(i8 noundef zeroext %., ptr noundef nonnull %7) #3
  store i64 1, ptr %0, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %3, %2
  %.1 = phi i32 [ 0, %3 ], [ 12, %2 ]
  ret i32 %.1
}

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

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

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"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}


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