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

; 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 }
%struct.sem_internal_t = type { %struct.xSTATIC_QUEUE, i32 }
%struct.xSTATIC_QUEUE = type { [3 x ptr], %union.anon, [2 x %struct.xSTATIC_LIST], [3 x i64], [2 x i8], i8 }
%union.anon = type { ptr }
%struct.xSTATIC_LIST = type { i64, ptr, %struct.xSTATIC_MINI_LIST_ITEM }
%struct.xSTATIC_MINI_LIST_ITEM = type { i32, [2 x ptr] }

@FreeRTOS_errno = external global i32, align 4

; 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 @sem_timedwait(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca %struct.timespec, align 8
  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) #5
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %10, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 -1, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.sem_internal_t, ptr %11, i32 0, i32 1
  %13 = call i32 @Atomic_Decrement_u32(ptr noundef %12)
  store i32 %13, ptr %8, align 4, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %35

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

21:                                               ; preds = %16
  store i32 0, ptr %7, align 4, !tbaa !9
  store i32 22, ptr %5, align 4, !tbaa !9
  br label %34

22:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 16, ptr %9) #5
  call void @llvm.memset.p0.i64(ptr align 8 %9, i8 0, i64 16, i1 false)
  %23 = call i32 @clock_gettime(i32 noundef 0, ptr noundef %9)
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %26

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

26:                                               ; preds = %22
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = call i32 @UTILS_AbsoluteTimespecToDeltaTicks(ptr noundef %27, ptr noundef %9, ptr noundef %7)
  store i32 %28, ptr %5, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %26, %25
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = icmp eq i32 %30, 116
  br i1 %31, label %32, label %33

32:                                               ; preds = %29
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %32, %29
  call void @llvm.lifetime.end.p0(i64 16, ptr %9) #5
  br label %34

34:                                               ; preds = %33, %21
  br label %35

35:                                               ; preds = %34, %2
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = icmp sgt i32 %36, 0
  br i1 %37, label %38, label %39

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

39:                                               ; preds = %35
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.sem_internal_t, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %7, align 4, !tbaa !9
  %43 = call i64 @xQueueSemaphoreTake(ptr noundef %41, i32 noundef %42)
  %44 = icmp ne i64 %43, 1
  br i1 %44, label %45, label %52

45:                                               ; preds = %39
  %46 = load i32, ptr %5, align 4, !tbaa !9
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  store i32 116, ptr @FreeRTOS_errno, align 4, !tbaa !9
  br label %51

49:                                               ; preds = %45
  %50 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %50, ptr @FreeRTOS_errno, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %49, %48
  store i32 -1, ptr %5, align 4, !tbaa !9
  br label %53

52:                                               ; preds = %39
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %53

53:                                               ; preds = %52, %51
  br label %54

54:                                               ; preds = %53, %38
  %55 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  ret i32 %55
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @Atomic_Decrement_u32(ptr noundef) #2

declare zeroext i1 @UTILS_ValidateTimespec(ptr noundef) #3

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

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

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

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

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

@FreeRTOS_errno = external local_unnamed_addr global i32, align 4

; 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 uwtable
define dso_local range(i32 -1, 1) i32 @sem_timedwait(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = alloca i32, align 4
  %4 = alloca %struct.timespec, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #5
  store i32 -1, ptr %3, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 144
  %6 = tail call i32 @Atomic_Decrement_u32(ptr noundef nonnull %5)
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %16, label %7

7:                                                ; preds = %2
  %8 = tail call zeroext i1 @UTILS_ValidateTimespec(ptr noundef nonnull %1) #5
  br i1 %8, label %10, label %9

9:                                                ; preds = %7
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %16

10:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  %11 = call i32 @clock_gettime(i32 noundef 0, ptr noundef nonnull %4) #5
  %.not11 = icmp eq i32 %11, 0
  br i1 %.not11, label %12, label %.thread

12:                                               ; preds = %10
  %13 = call i32 @UTILS_AbsoluteTimespecToDeltaTicks(ptr noundef nonnull %1, ptr noundef nonnull %4, ptr noundef nonnull %3) #5
  %14 = icmp eq i32 %13, 116
  br i1 %14, label %15, label %.thread

15:                                               ; preds = %12
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %10, %15, %12
  %.014 = phi i32 [ 116, %15 ], [ %13, %12 ], [ 22, %10 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #5
  br label %16

16:                                               ; preds = %9, %.thread, %2
  %.1 = phi i32 [ 22, %9 ], [ %.014, %.thread ], [ 0, %2 ]
  %17 = icmp sgt i32 %6, 0
  br i1 %17, label %23, label %18

18:                                               ; preds = %16
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = call i64 @xQueueSemaphoreTake(ptr noundef %0, i32 noundef %19) #5
  %.not12 = icmp eq i64 %20, 1
  br i1 %.not12, label %23, label %21

21:                                               ; preds = %18
  %22 = icmp eq i32 %.1, 0
  %..1 = select i1 %22, i32 116, i32 %.1
  store i32 %..1, ptr @FreeRTOS_errno, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %18, %16, %21
  %.2 = phi i32 [ -1, %21 ], [ 0, %16 ], [ 0, %18 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #5
  ret i32 %.2
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @Atomic_Decrement_u32(ptr noundef) local_unnamed_addr #2

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

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

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

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

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

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


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