; LLVM IR from intrepidcs/icsneoapi :: src/OEvent.ll (ZN6OEvent4WaitEm)

; 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.timespec = type { i64, i64 }
%class.OEvent = type <{ ptr, %union.pthread_cond_t, %union.pthread_mutex_t, i8, i8, [6 x i8] }>
%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 }
%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 }

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN6OEvent4WaitEm(ptr noundef nonnull align 8 dereferenceable(98) %0, i64 noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca %struct.timespec, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %9 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !11
  %10 = getelementptr inbounds %class.OEvent, ptr %9, i32 0, i32 2
  %11 = call i32 @pthread_mutex_lock(ptr noundef %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %12 = getelementptr inbounds %class.OEvent, ptr %9, i32 0, i32 4
  %13 = load i8, ptr %12, align 1, !tbaa !13, !range !16, !noundef !17
  %14 = trunc i8 %13 to i1
  br i1 %14, label %63, label %15

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %16 = load i64, ptr %4, align 8, !tbaa !9
  %17 = udiv i64 %16, 1000
  %18 = trunc i64 %17 to i32
  store i32 %18, ptr %8, align 4, !tbaa !11
  %19 = load i64, ptr %4, align 8, !tbaa !9
  %20 = urem i64 %19, 1000
  store i64 %20, ptr %4, align 8, !tbaa !9
  %21 = call i32 @clock_gettime(i32 noundef 0, ptr noundef %7) #4
  %22 = load i32, ptr %8, align 4, !tbaa !11
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds %struct.timespec, ptr %7, i32 0, i32 0
  %25 = load i64, ptr %24, align 8, !tbaa !18
  %26 = add nsw i64 %25, %23
  store i64 %26, ptr %24, align 8, !tbaa !18
  %27 = load i64, ptr %4, align 8, !tbaa !9
  %28 = mul i64 %27, 1000000
  store i64 %28, ptr %6, align 8, !tbaa !9
  %29 = load i64, ptr %6, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.timespec, ptr %7, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !20
  %32 = add i64 %29, %31
  %33 = icmp uge i64 %32, 1000000000
  br i1 %33, label %34, label %44

34:                                               ; preds = %15
  %35 = getelementptr inbounds %struct.timespec, ptr %7, i32 0, i32 0
  %36 = load i64, ptr %35, align 8, !tbaa !18
  %37 = add nsw i64 %36, 1
  store i64 %37, ptr %35, align 8, !tbaa !18
  %38 = load i64, ptr %6, align 8, !tbaa !9
  %39 = sub i64 %38, 1000000000
  store i64 %39, ptr %6, align 8, !tbaa !9
  %40 = load i64, ptr %6, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.timespec, ptr %7, i32 0, i32 1
  %42 = load i64, ptr %41, align 8, !tbaa !20
  %43 = add i64 %42, %40
  store i64 %43, ptr %41, align 8, !tbaa !20
  br label %50

44:                                               ; preds = %15
  %45 = load i64, ptr %4, align 8, !tbaa !9
  %46 = mul i64 %45, 1000000
  %47 = getelementptr inbounds %struct.timespec, ptr %7, i32 0, i32 1
  %48 = load i64, ptr %47, align 8, !tbaa !20
  %49 = add i64 %48, %46
  store i64 %49, ptr %47, align 8, !tbaa !20
  br label %50

50:                                               ; preds = %44, %34
  %51 = getelementptr inbounds %class.OEvent, ptr %9, i32 0, i32 1
  %52 = getelementptr inbounds %class.OEvent, ptr %9, i32 0, i32 2
  %53 = call i32 @pthread_cond_timedwait(ptr noundef %51, ptr noundef %52, ptr noundef %7)
  store i32 %53, ptr %5, align 4, !tbaa !11
  %54 = getelementptr inbounds %class.OEvent, ptr %9, i32 0, i32 3
  %55 = load i8, ptr %54, align 8, !tbaa !21, !range !16, !noundef !17
  %56 = trunc i8 %55 to i1
  br i1 %56, label %62, label %57

57:                                               ; preds = %50
  %58 = load i32, ptr %5, align 4, !tbaa !11
  %59 = icmp eq i32 %58, 0
  br i1 %59, label %60, label %62

60:                                               ; preds = %57
  %61 = getelementptr inbounds %class.OEvent, ptr %9, i32 0, i32 4
  store i8 0, ptr %61, align 1, !tbaa !13
  br label %62

62:                                               ; preds = %60, %57, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #4
  br label %63

63:                                               ; preds = %62, %2
  %64 = getelementptr inbounds %class.OEvent, ptr %9, i32 0, i32 2
  %65 = call i32 @pthread_mutex_unlock(ptr noundef %64) #4
  %66 = load i32, ptr %5, align 4, !tbaa !11
  %67 = sext i32 %66 to i64
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret i64 %67
}

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

; Function Attrs: nounwind
declare i32 @clock_gettime(i32 noundef, ptr noundef) #0

declare i32 @pthread_cond_timedwait(ptr noundef, 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) #2

attributes #0 = { 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 #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !15, i64 97}
!14 = !{!"_ZTS6OEvent", !7, i64 8, !7, i64 56, !15, i64 96, !15, i64 97}
!15 = !{!"bool", !7, i64 0}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!19, !10, i64 0}
!19 = !{!"_ZTS8timespec", !10, i64 0, !10, i64 8}
!20 = !{!19, !10, i64 8}
!21 = !{!14, !15, i64 96}


; 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.timespec = type { i64, i64 }

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i64 -2147483648, 2147483648) i64 @_ZN6OEvent4WaitEm(ptr noundef nonnull align 8 dereferenceable(98) %0, i64 noundef %1) local_unnamed_addr #1 align 2 {
  %3 = alloca %struct.timespec, align 8
  %4 = getelementptr inbounds i8, ptr %0, i64 56
  %5 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %4) #4
  %6 = getelementptr inbounds i8, ptr %0, i64 97
  %7 = load i8, ptr %6, align 1, !tbaa !5, !range !10, !noundef !11
  %8 = trunc nuw i8 %7 to i1
  br i1 %8, label %34, label %9

9:                                                ; preds = %2
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #4
  %10 = udiv i64 %1, 1000
  %11 = urem i64 %1, 1000
  %12 = call i32 @clock_gettime(i32 noundef 0, ptr noundef nonnull %3) #4
  %sext = shl i64 %10, 32
  %13 = ashr exact i64 %sext, 32
  %14 = load i64, ptr %3, align 8, !tbaa !12
  %15 = add nsw i64 %14, %13
  store i64 %15, ptr %3, align 8, !tbaa !12
  %16 = mul nuw nsw i64 %11, 1000000
  %17 = getelementptr inbounds i8, ptr %3, i64 8
  %18 = load i64, ptr %17, align 8, !tbaa !15
  %19 = add i64 %18, %16
  %20 = icmp ugt i64 %19, 999999999
  br i1 %20, label %21, label %24

21:                                               ; preds = %9
  %22 = add nsw i64 %15, 1
  store i64 %22, ptr %3, align 8, !tbaa !12
  %23 = add i64 %19, -1000000000
  br label %24

24:                                               ; preds = %9, %21
  %storemerge = phi i64 [ %23, %21 ], [ %19, %9 ]
  store i64 %storemerge, ptr %17, align 8, !tbaa !15
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  %26 = call i32 @pthread_cond_timedwait(ptr noundef nonnull %25, ptr noundef nonnull %4, ptr noundef nonnull %3)
  %27 = getelementptr inbounds i8, ptr %0, i64 96
  %28 = load i8, ptr %27, align 8, !tbaa !16, !range !10, !noundef !11
  %29 = trunc nuw i8 %28 to i1
  %30 = icmp ne i32 %26, 0
  %or.cond.not = select i1 %29, i1 true, i1 %30
  br i1 %or.cond.not, label %32, label %31

31:                                               ; preds = %24
  store i8 0, ptr %6, align 1, !tbaa !5
  br label %32

32:                                               ; preds = %31, %24
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  %33 = sext i32 %26 to i64
  br label %34

34:                                               ; preds = %32, %2
  %.0 = phi i64 [ 0, %2 ], [ %33, %32 ]
  %35 = call i32 @pthread_mutex_unlock(ptr noundef nonnull %4) #4
  ret i64 %.0
}

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

; Function Attrs: nounwind
declare i32 @clock_gettime(i32 noundef, ptr noundef) local_unnamed_addr #0

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

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

attributes #0 = { 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 #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !9, i64 97}
!6 = !{!"_ZTS6OEvent", !7, i64 8, !7, i64 56, !9, i64 96, !9, i64 97}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"bool", !7, i64 0}
!10 = !{i8 0, i8 2}
!11 = !{}
!12 = !{!13, !14, i64 0}
!13 = !{!"_ZTS8timespec", !14, i64 0, !14, i64 8}
!14 = !{!"long", !7, i64 0}
!15 = !{!13, !14, i64 8}
!16 = !{!6, !9, i64 96}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass