; LLVM IR from ctlst-tech/catpilot :: os/freertos/osa/FreeRTOS-Plus-POSIX/source (FreeRTOS_POSIX_utils)

; 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: 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 uwtable
define dso_local i32 @UTILS_TimespecToTicks(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !11
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %14, label %11

11:                                               ; preds = %2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %11, %2
  store i32 22, ptr %5, align 4, !tbaa !9
  br label %25

15:                                               ; preds = %11
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %24

18:                                               ; preds = %15
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = call zeroext i1 @UTILS_ValidateTimespec(ptr noundef %19)
  %21 = zext i1 %20 to i32
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %18
  store i32 22, ptr %5, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %23, %18, %15
  br label %25

25:                                               ; preds = %24, %14
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %65

28:                                               ; preds = %25
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.timespec, ptr %29, i32 0, i32 0
  %31 = load i64, ptr %30, align 8, !tbaa !13
  %32 = mul nsw i64 1000, %31
  store i64 %32, ptr %6, align 8, !tbaa !11
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.timespec, ptr %33, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !15
  %36 = sdiv i64 %35, 1000000
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.timespec, ptr %37, i32 0, i32 1
  %39 = load i64, ptr %38, align 8, !tbaa !15
  %40 = srem i64 %39, 1000000
  %41 = icmp ne i64 %40, 0
  %42 = zext i1 %41 to i32
  %43 = sext i32 %42 to i64
  %44 = add nsw i64 %36, %43
  store i64 %44, ptr %7, align 8, !tbaa !11
  %45 = load i64, ptr %7, align 8, !tbaa !11
  %46 = load i64, ptr %6, align 8, !tbaa !11
  %47 = add nsw i64 %46, %45
  store i64 %47, ptr %6, align 8, !tbaa !11
  %48 = load i64, ptr %6, align 8, !tbaa !11
  %49 = icmp slt i64 %48, 0
  br i1 %49, label %50, label %51

50:                                               ; preds = %28
  store i32 22, ptr %5, align 4, !tbaa !9
  br label %61

51:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 4, ptr %8, align 4, !tbaa !9
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = zext i32 %52 to i64
  %54 = icmp eq i64 %53, 4
  br i1 %54, label %55, label %60

55:                                               ; preds = %51
  %56 = load i64, ptr %6, align 8, !tbaa !11
  %57 = icmp sgt i64 %56, 4294967295
  br i1 %57, label %58, label %59

58:                                               ; preds = %55
  store i32 22, ptr %5, align 4, !tbaa !9
  br label %59

59:                                               ; preds = %58, %55
  br label %60

60:                                               ; preds = %59, %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %61

61:                                               ; preds = %60, %50
  %62 = load i64, ptr %6, align 8, !tbaa !11
  %63 = trunc i64 %62 to i32
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 %63, ptr %64, align 4, !tbaa !9
  br label %65

65:                                               ; preds = %61, %25
  %66 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %66
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @UTILS_ValidateTimespec(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"timespec", !12, i64 0, !12, i64 8}
!15 = !{!14, !12, i64 8}


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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 23) i32 @UTILS_TimespecToTicks(ptr noundef %0, ptr noundef writeonly %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq ptr %1, null
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %.thread, label %5

5:                                                ; preds = %2
  %6 = tail call zeroext i1 @UTILS_ValidateTimespec(ptr noundef nonnull %0)
  br i1 %6, label %7, label %.thread

7:                                                ; preds = %5
  %8 = load i64, ptr %0, align 8, !tbaa !5
  %9 = mul nsw i64 %8, 1000
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load i64, ptr %10, align 8, !tbaa !10
  %12 = sdiv i64 %11, 1000000
  %13 = srem i64 %11, 1000000
  %14 = icmp ne i64 %13, 0
  %15 = zext i1 %14 to i64
  %16 = add i64 %12, %9
  %17 = add i64 %16, %15
  %18 = icmp ugt i64 %17, 4294967295
  %.2 = select i1 %18, i32 22, i32 0
  %19 = trunc i64 %17 to i32
  store i32 %19, ptr %1, align 4, !tbaa !11
  br label %.thread

.thread:                                          ; preds = %2, %7, %5
  %.3 = phi i32 [ %.2, %7 ], [ 22, %5 ], [ 22, %2 ]
  ret i32 %.3
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @UTILS_ValidateTimespec(ptr noundef) local_unnamed_addr #0

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

!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 = !{!"timespec", !7, i64 0, !7, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !8, i64 0}


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