; LLVM IR from Softmotions/iowow :: src/utils (iwth)

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

; Function Attrs: nounwind uwtable
define dso_local i64 @iw_cond_timed_wait_ms(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca %struct.timespec, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %12) #4
  %14 = load ptr, ptr %9, align 8, !tbaa !5
  store i8 0, ptr %14, align 1, !tbaa !11
  %15 = call i64 @iwp_clock_get_time(i32 noundef 0, ptr noundef %12)
  store i64 %15, ptr %10, align 8, !tbaa !9
  %16 = load i64, ptr %10, align 8, !tbaa !9
  %17 = icmp ne i64 %16, 0
  %18 = xor i1 %17, true
  %19 = xor i1 %18, true
  %20 = zext i1 %19 to i32
  %21 = sext i32 %20 to i64
  %22 = call i64 @llvm.expect.i64(i64 %21, i64 0)
  %23 = icmp ne i64 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %4
  %25 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %25, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %70

26:                                               ; preds = %4
  %27 = load i64, ptr %8, align 8, !tbaa !9
  %28 = sdiv i64 %27, 1000
  %29 = getelementptr inbounds %struct.timespec, ptr %12, i32 0, i32 0
  %30 = load i64, ptr %29, align 8, !tbaa !13
  %31 = add nsw i64 %30, %28
  store i64 %31, ptr %29, align 8, !tbaa !13
  %32 = load i64, ptr %8, align 8, !tbaa !9
  %33 = srem i64 %32, 1000
  %34 = mul nsw i64 %33, 1000000
  %35 = getelementptr inbounds %struct.timespec, ptr %12, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !15
  %37 = add nsw i64 %36, %34
  store i64 %37, ptr %35, align 8, !tbaa !15
  %38 = getelementptr inbounds %struct.timespec, ptr %12, i32 0, i32 1
  %39 = load i64, ptr %38, align 8, !tbaa !15
  %40 = icmp sge i64 %39, 1000000000
  br i1 %40, label %41, label %48

41:                                               ; preds = %26
  %42 = getelementptr inbounds %struct.timespec, ptr %12, i32 0, i32 0
  %43 = load i64, ptr %42, align 8, !tbaa !13
  %44 = add nsw i64 %43, 1
  store i64 %44, ptr %42, align 8, !tbaa !13
  %45 = getelementptr inbounds %struct.timespec, ptr %12, i32 0, i32 1
  %46 = load i64, ptr %45, align 8, !tbaa !15
  %47 = sub nsw i64 %46, 1000000000
  store i64 %47, ptr %45, align 8, !tbaa !15
  br label %48

48:                                               ; preds = %41, %26
  br label %49

49:                                               ; preds = %53, %48
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = call i32 @pthread_cond_timedwait(ptr noundef %50, ptr noundef %51, ptr noundef %12)
  store i32 %52, ptr %11, align 4, !tbaa !16
  br label %53

53:                                               ; preds = %49
  %54 = load i32, ptr %11, align 4, !tbaa !16
  %55 = icmp eq i32 %54, 4
  br i1 %55, label %49, label %56, !llvm.loop !18

56:                                               ; preds = %53
  %57 = load i32, ptr %11, align 4, !tbaa !16
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %68

59:                                               ; preds = %56
  %60 = load i32, ptr %11, align 4, !tbaa !16
  %61 = icmp eq i32 %60, 110
  br i1 %61, label %62, label %64

62:                                               ; preds = %59
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  store i8 1, ptr %63, align 1, !tbaa !11
  br label %67

64:                                               ; preds = %59
  %65 = load i32, ptr %11, align 4, !tbaa !16
  %66 = call i64 @iwrc_set_errno(i64 noundef 70008, i32 noundef %65)
  store i64 %66, ptr %10, align 8, !tbaa !9
  br label %67

67:                                               ; preds = %64, %62
  br label %68

68:                                               ; preds = %67, %56
  %69 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %69, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %70

70:                                               ; preds = %68, %24
  call void @llvm.lifetime.end.p0(i64 16, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %71 = load i64, ptr %5, align 8
  ret i64 %71
}

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

declare i64 @iwp_clock_get_time(i32 noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #3

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

declare i64 @iwrc_set_errno(i64 noundef, i32 noundef) #2

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { nocallback nofree nosync nounwind willreturn memory(none) }
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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"timespec", !10, i64 0, !10, i64 8}
!15 = !{!14, !10, i64 8}
!16 = !{!17, !17, i64 0}
!17 = !{!"int", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; 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 uwtable
define dso_local i64 @iw_cond_timed_wait_ms(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = alloca %struct.timespec, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #3
  store i8 0, ptr %3, align 1, !tbaa !5
  %6 = call i64 @iwp_clock_get_time(i32 noundef 0, ptr noundef nonnull %5) #3
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %7, label %.loopexit, !prof !9

7:                                                ; preds = %4
  %8 = sdiv i64 %2, 1000
  %9 = load i64, ptr %5, align 8, !tbaa !10
  %10 = add nsw i64 %9, %8
  store i64 %10, ptr %5, align 8, !tbaa !10
  %11 = srem i64 %2, 1000
  %12 = mul nsw i64 %11, 1000000
  %13 = getelementptr inbounds i8, ptr %5, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !13
  %15 = add nsw i64 %14, %12
  store i64 %15, ptr %13, align 8, !tbaa !13
  %16 = icmp sgt i64 %15, 999999999
  br i1 %16, label %17, label %.preheader

17:                                               ; preds = %7
  %18 = add nsw i64 %10, 1
  store i64 %18, ptr %5, align 8, !tbaa !10
  %19 = add nsw i64 %15, -1000000000
  store i64 %19, ptr %13, align 8, !tbaa !13
  br label %.preheader

.preheader:                                       ; preds = %17, %7
  br label %20

20:                                               ; preds = %.preheader, %20
  %21 = call i32 @pthread_cond_timedwait(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %5) #3
  switch i32 %21, label %23 [
    i32 4, label %20
    i32 0, label %.loopexit
    i32 110, label %22
  ]

22:                                               ; preds = %20
  store i8 1, ptr %3, align 1, !tbaa !5
  br label %.loopexit

23:                                               ; preds = %20
  %24 = call i64 @iwrc_set_errno(i64 noundef 70008, i32 noundef %21) #3
  br label %.loopexit

.loopexit:                                        ; preds = %20, %23, %22, %4
  %.0 = phi i64 [ %6, %4 ], [ 0, %22 ], [ %24, %23 ], [ 0, %20 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #3
  ret i64 %.0
}

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

declare i64 @iwp_clock_get_time(i32 noundef, ptr noundef) local_unnamed_addr #2

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

declare i64 @iwrc_set_errno(i64 noundef, i32 noundef) local_unnamed_addr #2

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

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { 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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!10 = !{!11, !12, i64 0}
!11 = !{!"timespec", !12, i64 0, !12, i64 8}
!12 = !{!"long", !7, i64 0}
!13 = !{!11, !12, i64 8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass