; LLVM IR from ctlst-tech/catpilot :: os/freertos/services/misc (ring_buf)

; 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.ring_buf_t = type { ptr, ptr, ptr, i16, i16, ptr, ptr, ptr, ptr }

; 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
declare dso_local zeroext i16 @ring_buf_get_data_size(ptr noundef) #1

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @ring_buf_read_timeout(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i16, align 2
  %12 = alloca i32, align 4
  %13 = alloca i16, align 2
  %14 = alloca i16, align 2
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #4
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %22, label %17

17:                                               ; preds = %4
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.ring_buf_t, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = icmp eq ptr %20, null
  br i1 %21, label %22, label %23

22:                                               ; preds = %17, %4
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %152

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

26:                                               ; preds = %23
  store i32 -1, ptr %9, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %26, %23
  br label %28

28:                                               ; preds = %41, %27
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = call zeroext i16 @ring_buf_get_data_size(ptr noundef %29)
  store i16 %30, ptr %11, align 2, !tbaa !14
  %31 = zext i16 %30 to i32
  %32 = icmp slt i32 %31, 1
  br i1 %32, label %33, label %42

33:                                               ; preds = %28
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.ring_buf_t, ptr %34, i32 0, i32 8
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = load i32, ptr %9, align 4, !tbaa !9
  %38 = call i64 @xQueueSemaphoreTake(ptr noundef %36, i32 noundef %37)
  %39 = icmp ne i64 %38, 0
  br i1 %39, label %41, label %40

40:                                               ; preds = %33
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %152

41:                                               ; preds = %33
  br label %28, !llvm.loop !16

42:                                               ; preds = %28
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.ring_buf_t, ptr %43, i32 0, i32 6
  %45 = load ptr, ptr %44, align 8, !tbaa !18
  %46 = call i64 @xQueueSemaphoreTake(ptr noundef %45, i32 noundef -1)
  call void @llvm.lifetime.start.p0(i64 2, ptr %13) #4
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.ring_buf_t, ptr %47, i32 0, i32 2
  %49 = load ptr, ptr %48, align 8, !tbaa !11
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ring_buf_t, ptr %50, i32 0, i32 3
  %52 = load i16, ptr %51, align 8, !tbaa !19
  %53 = zext i16 %52 to i32
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i8, ptr %49, i64 %54
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.ring_buf_t, ptr %56, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !20
  %59 = ptrtoint ptr %55 to i64
  %60 = ptrtoint ptr %58 to i64
  %61 = sub i64 %59, %60
  %62 = trunc i64 %61 to i16
  store i16 %62, ptr %13, align 2, !tbaa !14
  %63 = load i32, ptr %8, align 4, !tbaa !9
  %64 = load i16, ptr %11, align 2, !tbaa !14
  %65 = zext i16 %64 to i32
  %66 = icmp ult i32 %63, %65
  br i1 %66, label %67, label %69

67:                                               ; preds = %42
  %68 = load i32, ptr %8, align 4, !tbaa !9
  br label %72

69:                                               ; preds = %42
  %70 = load i16, ptr %11, align 2, !tbaa !14
  %71 = zext i16 %70 to i32
  br label %72

72:                                               ; preds = %69, %67
  %73 = phi i32 [ %68, %67 ], [ %71, %69 ]
  store i32 %73, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %14) #4
  %74 = load i32, ptr %8, align 4, !tbaa !9
  %75 = load i16, ptr %13, align 2, !tbaa !14
  %76 = zext i16 %75 to i32
  %77 = icmp ult i32 %74, %76
  br i1 %77, label %78, label %80

78:                                               ; preds = %72
  %79 = load i32, ptr %8, align 4, !tbaa !9
  br label %83

80:                                               ; preds = %72
  %81 = load i16, ptr %13, align 2, !tbaa !14
  %82 = zext i16 %81 to i32
  br label %83

83:                                               ; preds = %80, %78
  %84 = phi i32 [ %79, %78 ], [ %82, %80 ]
  %85 = trunc i32 %84 to i16
  store i16 %85, ptr %14, align 2, !tbaa !14
  %86 = load ptr, ptr %7, align 8, !tbaa !5
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.ring_buf_t, ptr %87, i32 0, i32 1
  %89 = load ptr, ptr %88, align 8, !tbaa !20
  %90 = load i16, ptr %14, align 2, !tbaa !14
  %91 = zext i16 %90 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %86, ptr align 1 %89, i64 %91, i1 false)
  %92 = load i16, ptr %14, align 2, !tbaa !14
  %93 = zext i16 %92 to i32
  %94 = load ptr, ptr %6, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.ring_buf_t, ptr %94, i32 0, i32 1
  %96 = load ptr, ptr %95, align 8, !tbaa !20
  %97 = sext i32 %93 to i64
  %98 = getelementptr inbounds i8, ptr %96, i64 %97
  store ptr %98, ptr %95, align 8, !tbaa !20
  %99 = load i32, ptr %8, align 4, !tbaa !9
  %100 = load i16, ptr %14, align 2, !tbaa !14
  %101 = zext i16 %100 to i32
  %102 = icmp ugt i32 %99, %101
  br i1 %102, label %103, label %130

103:                                              ; preds = %83
  %104 = load ptr, ptr %7, align 8, !tbaa !5
  %105 = load i16, ptr %14, align 2, !tbaa !14
  %106 = zext i16 %105 to i32
  %107 = sext i32 %106 to i64
  %108 = getelementptr inbounds i8, ptr %104, i64 %107
  %109 = load ptr, ptr %6, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.ring_buf_t, ptr %109, i32 0, i32 2
  %111 = load ptr, ptr %110, align 8, !tbaa !11
  %112 = load i32, ptr %8, align 4, !tbaa !9
  %113 = load i16, ptr %14, align 2, !tbaa !14
  %114 = zext i16 %113 to i32
  %115 = sub i32 %112, %114
  %116 = zext i32 %115 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %108, ptr align 1 %111, i64 %116, i1 false)
  %117 = load ptr, ptr %6, align 8, !tbaa !5
  %118 = getelementptr inbounds %struct.ring_buf_t, ptr %117, i32 0, i32 2
  %119 = load ptr, ptr %118, align 8, !tbaa !11
  %120 = load i32, ptr %8, align 4, !tbaa !9
  %121 = zext i32 %120 to i64
  %122 = getelementptr inbounds i8, ptr %119, i64 %121
  %123 = load i16, ptr %14, align 2, !tbaa !14
  %124 = zext i16 %123 to i32
  %125 = sext i32 %124 to i64
  %126 = sub i64 0, %125
  %127 = getelementptr inbounds i8, ptr %122, i64 %126
  %128 = load ptr, ptr %6, align 8, !tbaa !5
  %129 = getelementptr inbounds %struct.ring_buf_t, ptr %128, i32 0, i32 1
  store ptr %127, ptr %129, align 8, !tbaa !20
  br label %130

130:                                              ; preds = %103, %83
  %131 = load i32, ptr %8, align 4, !tbaa !9
  %132 = load ptr, ptr %6, align 8, !tbaa !5
  %133 = getelementptr inbounds %struct.ring_buf_t, ptr %132, i32 0, i32 4
  %134 = load i16, ptr %133, align 2, !tbaa !21
  %135 = zext i16 %134 to i32
  %136 = sub i32 %135, %131
  %137 = trunc i32 %136 to i16
  store i16 %137, ptr %133, align 2, !tbaa !21
  %138 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %138, ptr %10, align 4, !tbaa !9
  %139 = load i32, ptr %10, align 4, !tbaa !9
  %140 = icmp sgt i32 %139, 0
  br i1 %140, label %141, label %146

141:                                              ; preds = %130
  %142 = load ptr, ptr %6, align 8, !tbaa !5
  %143 = getelementptr inbounds %struct.ring_buf_t, ptr %142, i32 0, i32 7
  %144 = load ptr, ptr %143, align 8, !tbaa !22
  %145 = call i64 @xQueueGenericSend(ptr noundef %144, ptr noundef null, i32 noundef 0, i64 noundef 0)
  br label %146

146:                                              ; preds = %141, %130
  %147 = load ptr, ptr %6, align 8, !tbaa !5
  %148 = getelementptr inbounds %struct.ring_buf_t, ptr %147, i32 0, i32 6
  %149 = load ptr, ptr %148, align 8, !tbaa !18
  %150 = call i64 @xQueueGenericSend(ptr noundef %149, ptr noundef null, i32 noundef 0, i64 noundef 0)
  %151 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %151, ptr %5, align 4
  store i32 1, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 2, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #4
  br label %152

152:                                              ; preds = %146, %40, %22
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %153 = load i32, ptr %5, align 4
  ret i32 %153
}

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 = { "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 nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !13, i64 24, !13, i64 26, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!13 = !{!"short", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!12, !6, i64 56}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !6, i64 40}
!19 = !{!12, !13, i64 24}
!20 = !{!12, !6, i64 8}
!21 = !{!12, !13, i64 26}
!22 = !{!12, !6, i64 48}


; 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
declare dso_local zeroext i16 @ring_buf_get_data_size(ptr noundef) local_unnamed_addr #0

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

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @ring_buf_read_timeout(ptr noundef %0, ptr nocapture noundef writeonly %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq ptr %0, null
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %4
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %.loopexit, label %10

10:                                               ; preds = %6
  %11 = icmp eq i32 %3, 0
  %spec.store.select = select i1 %11, i32 -1, i32 %3
  %12 = getelementptr inbounds i8, ptr %0, i64 56
  br label %13

13:                                               ; preds = %16, %10
  %14 = tail call zeroext i16 @ring_buf_get_data_size(ptr noundef nonnull %0)
  %15 = icmp eq i16 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %13
  %17 = load ptr, ptr %12, align 8, !tbaa !11
  %18 = tail call i64 @xQueueSemaphoreTake(ptr noundef %17, i32 noundef %spec.store.select) #4
  %.not45 = icmp eq i64 %18, 0
  br i1 %.not45, label %.loopexit, label %13, !llvm.loop !12

19:                                               ; preds = %13
  %20 = zext i16 %14 to i32
  %21 = getelementptr inbounds i8, ptr %0, i64 40
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = tail call i64 @xQueueSemaphoreTake(ptr noundef %22, i32 noundef -1) #4
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %0, i64 24
  %26 = load i16, ptr %25, align 8, !tbaa !15
  %27 = zext i16 %26 to i64
  %28 = getelementptr inbounds i8, ptr %24, i64 %27
  %29 = getelementptr inbounds i8, ptr %0, i64 8
  %30 = load ptr, ptr %29, align 8, !tbaa !16
  %31 = ptrtoint ptr %28 to i64
  %32 = ptrtoint ptr %30 to i64
  %33 = sub i64 %31, %32
  %34 = trunc i64 %33 to i32
  %35 = tail call i32 @llvm.umin.i32(i32 %20, i32 %2)
  %36 = and i32 %34, 65535
  %37 = tail call i32 @llvm.umin.i32(i32 %35, i32 %36)
  %38 = zext nneg i32 %37 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %1, ptr align 1 %30, i64 %38, i1 false)
  %39 = load ptr, ptr %29, align 8, !tbaa !16
  %40 = getelementptr inbounds i8, ptr %39, i64 %38
  store ptr %40, ptr %29, align 8, !tbaa !16
  %41 = icmp ult i32 %36, %35
  br i1 %41, label %42, label %52

42:                                               ; preds = %19
  %43 = getelementptr inbounds i8, ptr %1, i64 %38
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = sub nsw i32 %35, %37
  %46 = zext i32 %45 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %43, ptr align 1 %44, i64 %46, i1 false)
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = zext nneg i32 %35 to i64
  %49 = getelementptr inbounds i8, ptr %47, i64 %48
  %50 = sub nsw i64 0, %38
  %51 = getelementptr inbounds i8, ptr %49, i64 %50
  store ptr %51, ptr %29, align 8, !tbaa !16
  br label %52

52:                                               ; preds = %42, %19
  %53 = getelementptr inbounds i8, ptr %0, i64 26
  %54 = load i16, ptr %53, align 2, !tbaa !17
  %55 = trunc nuw i32 %35 to i16
  %56 = sub i16 %54, %55
  store i16 %56, ptr %53, align 2, !tbaa !17
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %61, label %57

57:                                               ; preds = %52
  %58 = getelementptr inbounds i8, ptr %0, i64 48
  %59 = load ptr, ptr %58, align 8, !tbaa !18
  %60 = tail call i64 @xQueueGenericSend(ptr noundef %59, ptr noundef null, i32 noundef 0, i64 noundef 0) #4
  br label %61

61:                                               ; preds = %57, %52
  %62 = load ptr, ptr %21, align 8, !tbaa !14
  %63 = tail call i64 @xQueueGenericSend(ptr noundef %62, ptr noundef null, i32 noundef 0, i64 noundef 0) #4
  br label %.loopexit

.loopexit:                                        ; preds = %16, %4, %6, %61
  %.0 = phi i32 [ %35, %61 ], [ -1, %6 ], [ -1, %4 ], [ -1, %16 ]
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #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 = { "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 nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind speculatable 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, !7, i64 16}
!6 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !10, i64 26, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!6, !7, i64 56}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!6, !7, i64 40}
!15 = !{!6, !10, i64 24}
!16 = !{!6, !7, i64 8}
!17 = !{!6, !10, i64 26}
!18 = !{!6, !7, i64 48}


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