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

; 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
declare dso_local i32 @clock_gettime(i32 noundef, ptr noundef) #0

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @clock_nanosleep(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %struct.timespec, align 8
  %12 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #4
  call void @llvm.memset.p0.i64(ptr align 8 %11, i8 0, i64 16, i1 false)
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = load ptr, ptr %8, align 8, !tbaa !9
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = load ptr, ptr %7, align 8, !tbaa !9
  %17 = call zeroext i1 @UTILS_ValidateTimespec(ptr noundef %16)
  %18 = zext i1 %17 to i32
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %4
  store i32 22, ptr %9, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %20, %4
  %22 = load i32, ptr %9, align 4, !tbaa !5
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %28

24:                                               ; preds = %21
  %25 = call i32 @clock_gettime(i32 noundef 0, ptr noundef %11)
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %28

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

28:                                               ; preds = %27, %24, %21
  %29 = load i32, ptr %9, align 4, !tbaa !5
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %59

31:                                               ; preds = %28
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = and i32 %32, 1
  %34 = icmp eq i32 %33, 1
  br i1 %34, label %35, label %51

35:                                               ; preds = %31
  %36 = call i32 @clock_gettime(i32 noundef 0, ptr noundef %11)
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %39

38:                                               ; preds = %35
  store i32 22, ptr %9, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %38, %35
  %40 = load i32, ptr %9, align 4, !tbaa !5
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %50

42:                                               ; preds = %39
  %43 = load ptr, ptr %7, align 8, !tbaa !9
  %44 = call i32 @UTILS_AbsoluteTimespecToDeltaTicks(ptr noundef %43, ptr noundef %11, ptr noundef %10)
  %45 = icmp eq i32 %44, 0
  br i1 %45, label %46, label %50

46:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %47 = call i32 @xTaskGetTickCount()
  store i32 %47, ptr %12, align 4, !tbaa !5
  %48 = load i32, ptr %10, align 4, !tbaa !5
  %49 = call i64 @xTaskDelayUntil(ptr noundef %12, i32 noundef %48)
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %50

50:                                               ; preds = %46, %42, %39
  br label %58

51:                                               ; preds = %31
  %52 = load ptr, ptr %7, align 8, !tbaa !9
  %53 = call i32 @UTILS_TimespecToTicks(ptr noundef %52, ptr noundef %10)
  %54 = icmp eq i32 %53, 0
  br i1 %54, label %55, label %57

55:                                               ; preds = %51
  %56 = load i32, ptr %10, align 4, !tbaa !5
  call void @vTaskDelay(i32 noundef %56)
  br label %57

57:                                               ; preds = %55, %51
  br label %58

58:                                               ; preds = %57, %50
  br label %59

59:                                               ; preds = %58, %28
  %60 = load i32, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 %60
}

declare zeroext i1 @UTILS_ValidateTimespec(ptr noundef) #3

declare i32 @UTILS_AbsoluteTimespecToDeltaTicks(ptr noundef, ptr noundef, ptr noundef) #3

declare i32 @xTaskGetTickCount() #3

declare i64 @xTaskDelayUntil(ptr noundef, i32 noundef) #3

declare i32 @UTILS_TimespecToTicks(ptr noundef, ptr noundef) #3

declare void @vTaskDelay(i32 noundef) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, 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"

%struct.timespec = type { i64, i64 }

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 23) i32 @clock_nanosleep(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr nocapture noundef readnone %3) local_unnamed_addr #0 {
  %5 = alloca i32, align 4
  %6 = alloca %struct.timespec, align 8
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %6) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %6, i8 0, i64 16, i1 false)
  %8 = tail call zeroext i1 @UTILS_ValidateTimespec(ptr noundef %2) #4
  br i1 %8, label %9, label %.thread15

9:                                                ; preds = %4
  %10 = call i32 @clock_gettime(i32 noundef 0, ptr noundef nonnull %6)
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %.thread, label %.thread15

.thread:                                          ; preds = %9
  %11 = and i32 %1, 1
  %.not10 = icmp eq i32 %11, 0
  br i1 %.not10, label %21, label %12

12:                                               ; preds = %.thread
  %13 = call i32 @clock_gettime(i32 noundef 0, ptr noundef nonnull %6)
  %.not11 = icmp eq i32 %13, 0
  br i1 %.not11, label %14, label %.thread15

14:                                               ; preds = %12
  %15 = call i32 @UTILS_AbsoluteTimespecToDeltaTicks(ptr noundef %2, ptr noundef nonnull %6, ptr noundef nonnull %5) #4
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %17, label %.thread15

17:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %7) #4
  %18 = call i32 @xTaskGetTickCount() #4
  store i32 %18, ptr %7, align 4, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = call i64 @xTaskDelayUntil(ptr noundef nonnull %7, i32 noundef %19) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %7) #4
  br label %.thread15

21:                                               ; preds = %.thread
  %22 = call i32 @UTILS_TimespecToTicks(ptr noundef %2, ptr noundef nonnull %5) #4
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %.thread15

24:                                               ; preds = %21
  %25 = load i32, ptr %5, align 4, !tbaa !5
  call void @vTaskDelay(i32 noundef %25) #4
  br label %.thread15

.thread15:                                        ; preds = %4, %9, %17, %14, %12, %24, %21
  %.3 = phi i32 [ 0, %17 ], [ 0, %14 ], [ 22, %12 ], [ 0, %24 ], [ 0, %21 ], [ 22, %9 ], [ 22, %4 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #4
  ret i32 %.3
}

declare zeroext i1 @UTILS_ValidateTimespec(ptr noundef) local_unnamed_addr #3

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

declare i32 @xTaskGetTickCount() local_unnamed_addr #3

declare i64 @xTaskDelayUntil(ptr noundef, i32 noundef) local_unnamed_addr #3

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

declare void @vTaskDelay(i32 noundef) local_unnamed_addr #3

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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