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

; 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::RWLock" = type { %union.pthread_rwlock_t }
%union.pthread_rwlock_t = type { %struct.__pthread_rwlock_arch_t }
%struct.__pthread_rwlock_arch_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i8, [7 x i8], i64, i32 }

@_ZTISt9exception = external constant ptr
@.str.12 = external hidden unnamed_addr constant [12 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 nounwind uwtable
declare dso_local { i64, i64 } @_ZN7cascadb3nowEv() #3

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN7cascadb6RWLock13try_read_lockEj(ptr noundef nonnull align 8 dereferenceable(56) %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::RWLock", ptr %12, i32 0, i32 0
  %38 = call i32 @pthread_rwlock_timedrdlock(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 %42

41:                                               ; preds = %2
  store i1 true, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %54

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

45:                                               ; preds = %42
  store i1 false, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %54

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

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

50:                                               ; preds = %46
  %51 = landingpad { ptr, i32 }
          cleanup
  %52 = extractvalue { ptr, i32 } %51, 0
  store ptr %52, ptr %10, align 8
  %53 = extractvalue { ptr, i32 } %51, 1
  store i32 %53, ptr %11, align 4
  call void @__cxa_free_exception(ptr %47) #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 %56

54:                                               ; preds = %45, %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
  %55 = load i1, ptr %3, align 1
  ret i1 %55

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

; Function Attrs: nounwind
declare i32 @pthread_rwlock_timedrdlock(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}


; 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.12 = external hidden unnamed_addr constant [12 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 nounwind uwtable
declare dso_local { i64, i64 } @_ZN7cascadb3nowEv() local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN7cascadb6RWLock13try_read_lockEj(ptr noundef nonnull align 8 dereferenceable(56) %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 = call i32 @pthread_rwlock_timedrdlock(ptr noundef nonnull %0, ptr noundef nonnull %3) #4
  switch i32 %15, label %16 [
    i32 110, label %21
    i32 0, label %21
  ]

16:                                               ; preds = %2
  %17 = call ptr @__cxa_allocate_exception(i64 8) #4
  invoke void @_ZN7cascadbL22pthread_call_exceptionEPKci(ptr dead_on_unwind writable sret(%"class.std::exception") align 8 %17, ptr noundef nonnull @.str.12, i32 noundef %15)
          to label %18 unwind label %19

18:                                               ; preds = %16
  call void @__cxa_throw(ptr %17, ptr nonnull @_ZTISt9exception, ptr nonnull @_ZNSt9exceptionD1Ev) #5
  unreachable

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

21:                                               ; preds = %2, %2
  %22 = icmp eq i32 %15, 0
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  ret i1 %22
}

; Function Attrs: nounwind
declare i32 @pthread_rwlock_timedrdlock(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}


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