; LLVM IR from eProsima/Fast-DDS :: test/realtime/mutex_testing_tool (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.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i32 }

@_ZN8eprosima7fastdds19g_tmutex_thread_pidE = external global %"struct.std::atomic", align 4
@_ZN8eprosima7fastdds18g_origin_lock_funcE = external global ptr, align 8
@.str = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local i32 @pthread_mutex_lock(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %4 = call noundef i32 @_ZNKSt13__atomic_baseIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4) @_ZN8eprosima7fastdds19g_tmutex_thread_pidE) #5
  store i32 %4, ptr %3, align 4, !tbaa !9
  %5 = load i32, ptr %3, align 4, !tbaa !9
  %6 = icmp ne i32 0, %5
  br i1 %6, label %7, label %15

7:                                                ; preds = %1
  %8 = load i32, ptr %3, align 4, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = call i64 (i64, ...) @syscall(i64 noundef 186) #5
  %11 = icmp eq i64 %9, %10
  br i1 %11, label %12, label %14

12:                                               ; preds = %7
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  call void @_ZN8eprosima7fastdds20tmutex_record_mutex_ENS0_8LockTypeEP15pthread_mutex_t(i32 noundef 0, ptr noundef %13)
  br label %14

14:                                               ; preds = %12, %7
  br label %15

15:                                               ; preds = %14, %1
  %16 = load ptr, ptr @_ZN8eprosima7fastdds18g_origin_lock_funcE, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %20

18:                                               ; preds = %15
  %19 = call ptr @dlsym(ptr noundef inttoptr (i64 -1 to ptr), ptr noundef @.str) #5
  store ptr %19, ptr @_ZN8eprosima7fastdds18g_origin_lock_funcE, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %18, %15
  %21 = load ptr, ptr @_ZN8eprosima7fastdds18g_origin_lock_funcE, align 8, !tbaa !5
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = call noundef i32 %21(ptr noundef %22)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  ret i32 %23
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNKSt13__atomic_baseIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4)) #2 align 2

; Function Attrs: nounwind
declare i64 @syscall(i64 noundef, ...) #3

declare void @_ZN8eprosima7fastdds20tmutex_record_mutex_ENS0_8LockTypeEP15pthread_mutex_t(i32 noundef, ptr noundef) #4

; Function Attrs: nounwind
declare ptr @dlsym(ptr noundef, ptr noundef) #3

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

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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 "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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, 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.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i32 }

@_ZN8eprosima7fastdds19g_tmutex_thread_pidE = external global %"struct.std::atomic", align 4
@_ZN8eprosima7fastdds18g_origin_lock_funcE = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @pthread_mutex_lock(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef i32 @_ZNKSt13__atomic_baseIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4) @_ZN8eprosima7fastdds19g_tmutex_thread_pidE) #4
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %8, label %3

3:                                                ; preds = %1
  %4 = sext i32 %2 to i64
  %5 = tail call i64 (i64, ...) @syscall(i64 noundef 186) #4
  %6 = icmp eq i64 %5, %4
  br i1 %6, label %7, label %8

7:                                                ; preds = %3
  tail call void @_ZN8eprosima7fastdds20tmutex_record_mutex_ENS0_8LockTypeEP15pthread_mutex_t(i32 noundef 0, ptr noundef %0)
  br label %8

8:                                                ; preds = %3, %7, %1
  %9 = load ptr, ptr @_ZN8eprosima7fastdds18g_origin_lock_funcE, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %13

11:                                               ; preds = %8
  %12 = tail call ptr @dlsym(ptr noundef nonnull inttoptr (i64 -1 to ptr), ptr noundef nonnull @.str) #4
  store ptr %12, ptr @_ZN8eprosima7fastdds18g_origin_lock_funcE, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %11, %8
  %14 = phi ptr [ %12, %11 ], [ %9, %8 ]
  %15 = tail call noundef i32 %14(ptr noundef %0)
  ret i32 %15
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNKSt13__atomic_baseIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1 align 2

; Function Attrs: nounwind
declare i64 @syscall(i64 noundef, ...) local_unnamed_addr #2

declare void @_ZN8eprosima7fastdds20tmutex_record_mutex_ENS0_8LockTypeEP15pthread_mutex_t(i32 noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare ptr @dlsym(ptr noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 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 "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 = { "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 = { 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++ TBAA"}


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass