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

; 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::CondVar" = type { %union.pthread_cond_t, ptr }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { 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.20 = external hidden unnamed_addr constant [13 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 @_ZN7cascadb7CondVar4waitEj(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) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #5
  %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) #5
  %37 = getelementptr inbounds %"class.cascadb::CondVar", ptr %12, i32 0, i32 0
  %38 = getelementptr inbounds %"class.cascadb::CondVar", ptr %12, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !18
  %40 = getelementptr inbounds %"class.cascadb::Mutex", ptr %39, i32 0, i32 1
  %41 = call i32 @pthread_cond_timedwait(ptr noundef %37, ptr noundef %40, ptr noundef %6)
  store i32 %41, ptr %8, align 4, !tbaa !9
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = icmp eq i32 %42, 0
  br i1 %43, label %44, label %45

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

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

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

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

52:                                               ; preds = %49
  call void @__cxa_throw(ptr %50, ptr @_ZTISt9exception, ptr @_ZNSt9exceptionD1Ev) #6
  unreachable

53:                                               ; preds = %49
  %54 = landingpad { ptr, i32 }
          cleanup
  %55 = extractvalue { ptr, i32 } %54, 0
  store ptr %55, ptr %10, align 8
  %56 = extractvalue { ptr, i32 } %54, 1
  store i32 %56, ptr %11, align 4
  call void @__cxa_free_exception(ptr %50) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #5
  br label %59

57:                                               ; preds = %48, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #5
  %58 = load i1, ptr %3, align 1
  ret i1 %58

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

declare i32 @pthread_cond_timedwait(ptr noundef, ptr noundef, ptr noundef) #4

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 = { "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 }
attributes #6 = { 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, !6, i64 48}
!19 = !{!"_ZTSN7cascadb7CondVarE", !7, i64 0, !6, i64 48}


; 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.20 = external hidden unnamed_addr constant [13 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 @_ZN7cascadb7CondVar4waitEj(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) #5
  %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 48
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %16, i64 8
  %18 = call i32 @pthread_cond_timedwait(ptr noundef nonnull %0, ptr noundef nonnull %17, ptr noundef nonnull %3)
  switch i32 %18, label %19 [
    i32 110, label %24
    i32 0, label %24
  ]

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

21:                                               ; preds = %19
  call void @__cxa_throw(ptr %20, ptr nonnull @_ZTISt9exception, ptr nonnull @_ZNSt9exceptionD1Ev) #6
  unreachable

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

24:                                               ; preds = %2, %2
  %25 = icmp eq i32 %18, 0
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #5
  ret i1 %25
}

declare i32 @pthread_cond_timedwait(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #4

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 = { "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 }
attributes #6 = { 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 48}
!12 = !{!"_ZTSN7cascadb7CondVarE", !8, i64 0, !13, i64 48}
!13 = !{!"any pointer", !8, i64 0}


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