; LLVM IR from turi-code/SFrame :: oss_src/fileio/oss_webstor/sysutils.ll (ZNK7webstor8internal9EventSync4waitEj)

; 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.webstor::internal::EventWaiter" = type { %union.pthread_mutex_t, %union.pthread_cond_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 }
%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 }
%struct.timespec = type { i64, i64 }
%struct.timeval = type { i64, i64 }

; 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
declare i32 @pthread_mutex_lock(ptr noundef) #1

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNK7webstor8internal9EventSync10add_waiterEPNS0_11EventWaiterE(ptr noundef nonnull align 8 dereferenceable(92), ptr noundef) #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNK7webstor8internal9EventSync13remove_waiterEPNS0_11EventWaiterE(ptr noundef nonnull align 8 dereferenceable(92), ptr noundef) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK7webstor8internal9EventSync9get_stateEv(ptr noundef nonnull align 8 dereferenceable(92)) #3 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZNK7webstor8internal9EventSync4waitEj(ptr noundef nonnull align 8 dereferenceable(92) %0, i32 noundef %1) #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca %"class.webstor::internal::EventWaiter", align 8
  %6 = alloca %struct.timespec, align 8
  %7 = alloca %struct.timeval, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %10 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 88, ptr %5) #5
  call void @_ZN7webstor8internal11EventWaiterC1Ev(ptr noundef nonnull align 8 dereferenceable(88) %5)
  call void @_ZNK7webstor8internal9EventSync10add_waiterEPNS0_11EventWaiterE(ptr noundef nonnull align 8 dereferenceable(92) %10, ptr noundef %5)
  %11 = call noundef i32 @_ZNK7webstor8internal9EventSync9get_stateEv(ptr noundef nonnull align 8 dereferenceable(92) %10)
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %56, label %13

13:                                               ; preds = %2
  %14 = getelementptr inbounds %"class.webstor::internal::EventWaiter", ptr %5, i32 0, i32 0
  %15 = call i32 @pthread_mutex_lock(ptr noundef %14) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #5
  %16 = call i32 @gettimeofday(ptr noundef %7, ptr noundef null) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = udiv i32 %17, 1000
  store i32 %18, ptr %8, align 4, !tbaa !9
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = urem i32 %19, 1000
  store i32 %20, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = mul i32 %21, 1000000
  store i32 %22, ptr %9, align 4, !tbaa !9
  %23 = getelementptr inbounds %struct.timeval, ptr %7, i32 0, i32 1
  %24 = load i64, ptr %23, align 8, !tbaa !11
  %25 = mul nsw i64 %24, 1000
  %26 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 1
  store i64 %25, ptr %26, align 8, !tbaa !14
  %27 = getelementptr inbounds %struct.timeval, ptr %7, i32 0, i32 0
  %28 = load i64, ptr %27, align 8, !tbaa !16
  %29 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 0
  store i64 %28, ptr %29, align 8, !tbaa !17
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = zext i32 %30 to i64
  %32 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 1
  %33 = load i64, ptr %32, align 8, !tbaa !14
  %34 = add nsw i64 %33, %31
  store i64 %34, ptr %32, align 8, !tbaa !14
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 0
  %38 = load i64, ptr %37, align 8, !tbaa !17
  %39 = add nsw i64 %38, %36
  store i64 %39, ptr %37, align 8, !tbaa !17
  %40 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 1
  %41 = load i64, ptr %40, align 8, !tbaa !14
  %42 = icmp sgt i64 %41, 1000000000
  br i1 %42, label %43, label %50

43:                                               ; preds = %13
  %44 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 0
  %45 = load i64, ptr %44, align 8, !tbaa !17
  %46 = add nsw i64 %45, 1
  store i64 %46, ptr %44, align 8, !tbaa !17
  %47 = getelementptr inbounds %struct.timespec, ptr %6, i32 0, i32 1
  %48 = load i64, ptr %47, align 8, !tbaa !14
  %49 = sub nsw i64 %48, 1000000000
  store i64 %49, ptr %47, align 8, !tbaa !14
  br label %50

50:                                               ; preds = %43, %13
  %51 = getelementptr inbounds %"class.webstor::internal::EventWaiter", ptr %5, i32 0, i32 1
  %52 = getelementptr inbounds %"class.webstor::internal::EventWaiter", ptr %5, i32 0, i32 0
  %53 = call i32 @pthread_cond_timedwait(ptr noundef %51, ptr noundef %52, ptr noundef %6)
  %54 = getelementptr inbounds %"class.webstor::internal::EventWaiter", ptr %5, i32 0, i32 0
  %55 = call i32 @pthread_mutex_unlock(ptr noundef %54) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #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 %56

56:                                               ; preds = %50, %2
  call void @_ZNK7webstor8internal9EventSync13remove_waiterEPNS0_11EventWaiterE(ptr noundef nonnull align 8 dereferenceable(92) %10, ptr noundef %5)
  %57 = call noundef i32 @_ZNK7webstor8internal9EventSync9get_stateEv(ptr noundef nonnull align 8 dereferenceable(92) %10)
  %58 = icmp ne i32 %57, 0
  call void @llvm.lifetime.end.p0(i64 88, ptr %5) #5
  ret i1 %58
}

; Function Attrs: nounwind
declare i32 @gettimeofday(ptr noundef, ptr noundef) #1

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

declare void @_ZN7webstor8internal11EventWaiterC1Ev(ptr)

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

!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.webstor::internal::EventWaiter" = type { %union.pthread_mutex_t, %union.pthread_cond_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 }
%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 }
%struct.timespec = type { i64, i64 }
%struct.timeval = type { i64, i64 }

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

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNK7webstor8internal9EventSync10add_waiterEPNS0_11EventWaiterE(ptr noundef nonnull align 8 dereferenceable(92), ptr noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNK7webstor8internal9EventSync13remove_waiterEPNS0_11EventWaiterE(ptr noundef nonnull align 8 dereferenceable(92), ptr noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK7webstor8internal9EventSync9get_stateEv(ptr noundef nonnull align 8 dereferenceable(92)) local_unnamed_addr #3 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZNK7webstor8internal9EventSync4waitEj(ptr noundef nonnull align 8 dereferenceable(92) %0, i32 noundef %1) local_unnamed_addr #2 align 2 {
  %3 = alloca %"class.webstor::internal::EventWaiter", align 8
  %4 = alloca %struct.timespec, align 8
  %5 = alloca %struct.timeval, align 8
  call void @llvm.lifetime.start.p0(i64 88, ptr nonnull %3) #6
  call void @_ZN7webstor8internal11EventWaiterC1Ev(ptr noundef nonnull align 8 dereferenceable(88) %3)
  call void @_ZNK7webstor8internal9EventSync10add_waiterEPNS0_11EventWaiterE(ptr noundef nonnull align 8 dereferenceable(92) %0, ptr noundef nonnull %3)
  %6 = call noundef i32 @_ZNK7webstor8internal9EventSync9get_stateEv(ptr noundef nonnull align 8 dereferenceable(92) %0)
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %7, label %30

7:                                                ; preds = %2
  %8 = call i32 @pthread_mutex_lock(ptr noundef nonnull %3) #6
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #6
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #6
  %9 = call i32 @gettimeofday(ptr noundef nonnull %5, ptr noundef null) #6
  %10 = udiv i32 %1, 1000
  %11 = urem i32 %1, 1000
  %12 = mul nuw nsw i32 %11, 1000000
  %13 = getelementptr inbounds i8, ptr %5, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = mul nsw i64 %14, 1000
  %16 = getelementptr inbounds i8, ptr %4, i64 8
  %17 = load i64, ptr %5, align 8, !tbaa !10
  %18 = zext nneg i32 %12 to i64
  %19 = add nsw i64 %15, %18
  store i64 %19, ptr %16, align 8, !tbaa !11
  %20 = zext nneg i32 %10 to i64
  %21 = add nsw i64 %17, %20
  store i64 %21, ptr %4, align 8, !tbaa !13
  %22 = icmp sgt i64 %19, 1000000000
  br i1 %22, label %23, label %26

23:                                               ; preds = %7
  %24 = add nsw i64 %21, 1
  store i64 %24, ptr %4, align 8, !tbaa !13
  %25 = add nsw i64 %19, -1000000000
  store i64 %25, ptr %16, align 8, !tbaa !11
  br label %26

26:                                               ; preds = %23, %7
  %27 = getelementptr inbounds i8, ptr %3, i64 40
  %28 = call i32 @pthread_cond_timedwait(ptr noundef nonnull %27, ptr noundef nonnull %3, ptr noundef nonnull %4)
  %29 = call i32 @pthread_mutex_unlock(ptr noundef nonnull %3) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #6
  br label %30

30:                                               ; preds = %26, %2
  call void @_ZNK7webstor8internal9EventSync13remove_waiterEPNS0_11EventWaiterE(ptr noundef nonnull align 8 dereferenceable(92) %0, ptr noundef nonnull %3)
  %31 = call noundef i32 @_ZNK7webstor8internal9EventSync9get_stateEv(ptr noundef nonnull align 8 dereferenceable(92) %0)
  %32 = icmp ne i32 %31, 0
  call void @llvm.lifetime.end.p0(i64 88, ptr nonnull %3) #6
  ret i1 %32
}

; Function Attrs: nofree nounwind
declare noundef i32 @gettimeofday(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #4

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

declare void @_ZN7webstor8internal11EventWaiterC1Ev(ptr) local_unnamed_addr

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #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 = { nofree 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 #5 = { "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 #6 = { 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, !7, i64 8}
!6 = !{!"_ZTS7timeval", !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 0}
!11 = !{!12, !7, i64 8}
!12 = !{!"_ZTS8timespec", !7, i64 0, !7, i64 8}
!13 = !{!12, !7, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass