; LLVM IR from weicao/cascadb :: src/sys/posix/posix_sys.ll (ZN7cascadb5Mutex8lock_tryEj)

; 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"

%"class.std::exception" = type { ptr }
%struct.timespec = type { i64, i64 }
%struct.timeval = type { i64, i64 }
%"class.cascadb::Mutex" = type { i8, %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@_ZTISt9exception = external constant ptr
@.str.6 = external hidden unnamed_addr constant [10 x i8], align 1

declare i32 @__gxx_personality_v0(...)

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

declare ptr @__cxa_allocate_exception(i64)

; Function Attrs: mustprogress uwtable
declare hidden void @_ZN7cascadbL22pthread_call_exceptionEPKci(ptr dead_on_unwind noalias writable sret(%"class.std::exception") align 8, ptr noundef, i32 noundef) #1

declare void @__cxa_free_exception(ptr)

; Function Attrs: nounwind
declare void @_ZNSt9exceptionD1Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #2

declare void @__cxa_throw(ptr, ptr, ptr)

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

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN7cascadb5Mutex8lock_tryEj(ptr noundef nonnull align 8 dereferenceable(48) %0, i32 noundef %1) #1 align 2 personality ptr @__gxx_personality_v0 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca %struct.timespec, align 8
  %7 = alloca %struct.timeval, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %12 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #4
  %13 = call { i64, i64 } @_ZN7cascadb3nowEv()
  %14 = getelementptr inbounds { i64, i64 }, ptr %7, i32 0, i32 0
  %15 = extractvalue { i64, i64 } %13, 0
  store i64 %15, ptr %14, align 8
  %16 = getelementptr inbounds { i64, i64 }, ptr %7, i32 0, i32 1
  %17 = extractvalue { i64, i64 } %13, 1
  store i64 %17, ptr %16, align 8
  %18 = getelementptr inbounds %struct.timeval, ptr %7, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !11
  %20 = mul nsw i64 %19, 1000
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = mul i32 %21, 1000000
  %23 = zext i32 %22 to i64
  %24 = add nsw i64 %20, %23
  %25 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 1
  store i64 %24, ptr %25, align 8, !tbaa !14
  %26 = getelementptr inbounds %struct.timeval, ptr %7, i32 0, i32 0
  %27 = load i64, ptr %26, align 8, !tbaa !16
  %28 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 1
  %29 = load i64, ptr %28, align 8, !tbaa !14
  %30 = sdiv i64 %29, 1000000000
  %31 = add nsw i64 %27, %30
  %32 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 0
  store i64 %31, ptr %32, align 8, !tbaa !17
  %33 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !14
  %35 = srem i64 %34, 1000000000
  %36 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 1
  store i64 %35, ptr %36, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %37 = getelementptr inbounds %"class.cascadb::Mutex", ptr %12, i32 0, i32 1
  %38 = call i32 @pthread_mutex_timedlock(ptr noundef %37, ptr noundef %6) #4
  store i32 %38, ptr %8, align 4, !tbaa !9
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %41, label %43

41:                                               ; preds = %2
  %42 = getelementptr inbounds %"class.cascadb::Mutex", ptr %12, i32 0, i32 0
  store i8 1, ptr %42, align 8, !tbaa !18
  store i1 true, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %55

43:                                               ; preds = %2
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = icmp eq i32 %44, 110
  br i1 %45, label %46, label %47

46:                                               ; preds = %43
  store i1 false, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %55

47:                                               ; preds = %43
  %48 = call ptr @__cxa_allocate_exception(i64 8) #4
  %49 = load i32, ptr %8, align 4, !tbaa !9
  invoke void @_ZN7cascadbL22pthread_call_exceptionEPKci(ptr dead_on_unwind writable sret(%"class.std::exception") align 8 %48, ptr noundef @.str.6, i32 noundef %49)
          to label %50 unwind label %51

50:                                               ; preds = %47
  call void @__cxa_throw(ptr %48, ptr @_ZTISt9exception, ptr @_ZNSt9exceptionD1Ev) #5
  unreachable

51:                                               ; preds = %47
  %52 = landingpad { ptr, i32 }
          cleanup
  %53 = extractvalue { ptr, i32 } %52, 0
  store ptr %53, ptr %10, align 8
  %54 = extractvalue { ptr, i32 } %52, 1
  store i32 %54, ptr %11, align 4
  call void @__cxa_free_exception(ptr %48) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #4
  br label %57

55:                                               ; preds = %46, %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #4
  %56 = load i1, ptr %3, align 1
  ret i1 %56

57:                                               ; preds = %51
  %58 = load ptr, ptr %10, align 8
  %59 = load i32, ptr %11, align 4
  %60 = insertvalue { ptr, i32 } poison, ptr %58, 0
  %61 = insertvalue { ptr, i32 } %60, i32 %59, 1
  resume { ptr, i32 } %61
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local { i64, i64 } @_ZN7cascadb3nowEv() #3

; Function Attrs: nounwind
declare i32 @pthread_mutex_timedlock(ptr noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { 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 #4 = { nounwind }
attributes #5 = { noreturn }

!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}
!11 = !{!12, !13, i64 8}
!12 = !{!"_ZTS7timeval", !13, i64 0, !13, i64 8}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !13, i64 8}
!15 = !{!"_ZTS8timespec", !13, i64 0, !13, i64 8}
!16 = !{!12, !13, i64 0}
!17 = !{!15, !13, i64 0}
!18 = !{!19, !20, i64 0}
!19 = !{!"_ZTSN7cascadb5MutexE", !20, i64 0, !7, i64 8}
!20 = !{!"bool", !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"

%"class.std::exception" = type { ptr }
%struct.timespec = type { i64, i64 }

@_ZTISt9exception = external constant ptr
@.str.6 = external hidden unnamed_addr constant [10 x i8], align 1

declare i32 @__gxx_personality_v0(...)

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

declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr

; Function Attrs: mustprogress uwtable
declare hidden void @_ZN7cascadbL22pthread_call_exceptionEPKci(ptr dead_on_unwind noalias writable sret(%"class.std::exception") align 8, ptr noundef, i32 noundef) local_unnamed_addr #1

declare void @__cxa_free_exception(ptr) local_unnamed_addr

; Function Attrs: nounwind
declare void @_ZNSt9exceptionD1Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #2

declare void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr

; 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 uwtable
define dso_local noundef zeroext i1 @_ZN7cascadb5Mutex8lock_tryEj(ptr noundef nonnull align 8 dereferenceable(48) %0, i32 noundef %1) local_unnamed_addr #1 align 2 personality ptr @__gxx_personality_v0 {
  %3 = alloca %struct.timespec, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #4
  %4 = tail call { i64, i64 } @_ZN7cascadb3nowEv()
  %5 = extractvalue { i64, i64 } %4, 0
  %6 = extractvalue { i64, i64 } %4, 1
  %7 = mul nsw i64 %6, 1000
  %8 = mul i32 %1, 1000000
  %9 = zext i32 %8 to i64
  %10 = add nsw i64 %7, %9
  %11 = getelementptr inbounds i8, ptr %3, i64 8
  %12 = sdiv i64 %10, 1000000000
  %13 = add nsw i64 %12, %5
  store i64 %13, ptr %3, align 8, !tbaa !5
  %14 = srem i64 %10, 1000000000
  store i64 %14, ptr %11, align 8, !tbaa !10
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  %16 = call i32 @pthread_mutex_timedlock(ptr noundef nonnull %15, ptr noundef nonnull %3) #4
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %2
  store i8 1, ptr %0, align 8, !tbaa !11
  br label %26

19:                                               ; preds = %2
  %20 = icmp eq i32 %16, 110
  br i1 %20, label %26, label %21

21:                                               ; preds = %19
  %22 = call ptr @__cxa_allocate_exception(i64 8) #4
  invoke void @_ZN7cascadbL22pthread_call_exceptionEPKci(ptr dead_on_unwind writable sret(%"class.std::exception") align 8 %22, ptr noundef nonnull @.str.6, i32 noundef %16)
          to label %23 unwind label %24

23:                                               ; preds = %21
  call void @__cxa_throw(ptr %22, ptr nonnull @_ZTISt9exception, ptr nonnull @_ZNSt9exceptionD1Ev) #5
  unreachable

24:                                               ; preds = %21
  %25 = landingpad { ptr, i32 }
          cleanup
  call void @__cxa_free_exception(ptr %22) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  resume { ptr, i32 } %25

26:                                               ; preds = %19, %18
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  ret i1 %17
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local { i64, i64 } @_ZN7cascadb3nowEv() local_unnamed_addr #3

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { 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 #4 = { nounwind }
attributes #5 = { noreturn }

!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, !7, i64 0}
!6 = !{!"_ZTS8timespec", !7, i64 0, !7, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTSN7cascadb5MutexE", !13, i64 0, !8, i64 8}
!13 = !{!"bool", !8, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass