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

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

20:                                               ; preds = %15, %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %155

21:                                               ; preds = %15
  br label %22

22:                                               ; preds = %34, %21
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = call zeroext i16 @ring_buf_get_data_size(ptr noundef %23)
  store i16 %24, ptr %9, align 2, !tbaa !9
  %25 = zext i16 %24 to i32
  %26 = icmp slt i32 %25, 1
  br i1 %26, label %27, label %35

27:                                               ; preds = %22
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.ring_buf_t, ptr %28, i32 0, i32 8
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = call i64 @xQueueSemaphoreTake(ptr noundef %30, i32 noundef -1)
  %32 = icmp ne i64 %31, 0
  br i1 %32, label %34, label %33

33:                                               ; preds = %27
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %155

34:                                               ; preds = %27
  br label %22, !llvm.loop !14

35:                                               ; preds = %22
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.ring_buf_t, ptr %36, i32 0, i32 6
  %38 = load ptr, ptr %37, align 8, !tbaa !16
  %39 = call i64 @xQueueSemaphoreTake(ptr noundef %38, i32 noundef -1)
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #4
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.ring_buf_t, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.ring_buf_t, ptr %43, i32 0, i32 3
  %45 = load i16, ptr %44, align 8, !tbaa !17
  %46 = zext i16 %45 to i32
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %42, i64 %47
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.ring_buf_t, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !18
  %52 = ptrtoint ptr %48 to i64
  %53 = ptrtoint ptr %51 to i64
  %54 = sub i64 %52, %53
  %55 = trunc i64 %54 to i16
  store i16 %55, ptr %11, align 2, !tbaa !9
  %56 = load i16, ptr %7, align 2, !tbaa !9
  %57 = zext i16 %56 to i32
  %58 = load i16, ptr %9, align 2, !tbaa !9
  %59 = zext i16 %58 to i32
  %60 = icmp slt i32 %57, %59
  br i1 %60, label %61, label %64

61:                                               ; preds = %35
  %62 = load i16, ptr %7, align 2, !tbaa !9
  %63 = zext i16 %62 to i32
  br label %67

64:                                               ; preds = %35
  %65 = load i16, ptr %9, align 2, !tbaa !9
  %66 = zext i16 %65 to i32
  br label %67

67:                                               ; preds = %64, %61
  %68 = phi i32 [ %63, %61 ], [ %66, %64 ]
  %69 = trunc i32 %68 to i16
  store i16 %69, ptr %7, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #4
  %70 = load i16, ptr %7, align 2, !tbaa !9
  %71 = zext i16 %70 to i32
  %72 = load i16, ptr %11, align 2, !tbaa !9
  %73 = zext i16 %72 to i32
  %74 = icmp slt i32 %71, %73
  br i1 %74, label %75, label %78

75:                                               ; preds = %67
  %76 = load i16, ptr %7, align 2, !tbaa !9
  %77 = zext i16 %76 to i32
  br label %81

78:                                               ; preds = %67
  %79 = load i16, ptr %11, align 2, !tbaa !9
  %80 = zext i16 %79 to i32
  br label %81

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

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

131:                                              ; preds = %102, %81
  %132 = load i16, ptr %7, align 2, !tbaa !9
  %133 = zext i16 %132 to i32
  %134 = load ptr, ptr %5, align 8, !tbaa !5
  %135 = getelementptr inbounds %struct.ring_buf_t, ptr %134, i32 0, i32 4
  %136 = load i16, ptr %135, align 2, !tbaa !19
  %137 = zext i16 %136 to i32
  %138 = sub nsw i32 %137, %133
  %139 = trunc i32 %138 to i16
  store i16 %139, ptr %135, align 2, !tbaa !19
  %140 = load i16, ptr %7, align 2, !tbaa !9
  %141 = zext i16 %140 to i32
  store i32 %141, ptr %8, align 4, !tbaa !20
  %142 = load i32, ptr %8, align 4, !tbaa !20
  %143 = icmp sgt i32 %142, 0
  br i1 %143, label %144, label %149

144:                                              ; preds = %131
  %145 = load ptr, ptr %5, align 8, !tbaa !5
  %146 = getelementptr inbounds %struct.ring_buf_t, ptr %145, i32 0, i32 7
  %147 = load ptr, ptr %146, align 8, !tbaa !22
  %148 = call i64 @xQueueGenericSend(ptr noundef %147, ptr noundef null, i32 noundef 0, i64 noundef 0)
  br label %149

149:                                              ; preds = %144, %131
  %150 = load ptr, ptr %5, align 8, !tbaa !5
  %151 = getelementptr inbounds %struct.ring_buf_t, ptr %150, i32 0, i32 6
  %152 = load ptr, ptr %151, align 8, !tbaa !16
  %153 = call i64 @xQueueGenericSend(ptr noundef %152, ptr noundef null, i32 noundef 0, i64 noundef 0)
  %154 = load i32, ptr %8, align 4, !tbaa !20
  store i32 %154, ptr %4, align 4
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 2, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #4
  br label %155

155:                                              ; preds = %149, %33, %20
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %156 = load i32, ptr %4, align 4
  ret i32 %156
}

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 = !{!"short", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 26, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!13 = !{!12, !6, i64 56}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!12, !6, i64 40}
!17 = !{!12, !10, i64 24}
!18 = !{!12, !6, i64 8}
!19 = !{!12, !10, i64 26}
!20 = !{!21, !21, i64 0}
!21 = !{!"int", !7, i64 0}
!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 range(i32 -1, 65536) i32 @ring_buf_read(ptr noundef %0, ptr nocapture noundef writeonly %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %0, null
  br i1 %4, label %.loopexit, label %5

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

.preheader:                                       ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 56
  br label %10

10:                                               ; preds = %.preheader, %13
  %11 = tail call zeroext i16 @ring_buf_get_data_size(ptr noundef nonnull %0)
  %12 = icmp eq i16 %11, 0
  br i1 %12, label %13, label %16

13:                                               ; preds = %10
  %14 = load ptr, ptr %9, align 8, !tbaa !11
  %15 = tail call i64 @xQueueSemaphoreTake(ptr noundef %14, i32 noundef -1) #4
  %.not43 = icmp eq i64 %15, 0
  br i1 %.not43, label %.loopexit, label %10, !llvm.loop !12

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

39:                                               ; preds = %16
  %40 = getelementptr inbounds i8, ptr %1, i64 %35
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = sub nsw i32 %32, %34
  %43 = sext i32 %42 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %40, ptr align 1 %41, i64 %43, i1 false)
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = zext i16 %31 to i64
  %46 = getelementptr inbounds i8, ptr %44, i64 %45
  %47 = sub nsw i64 0, %35
  %48 = getelementptr inbounds i8, ptr %46, i64 %47
  store ptr %48, ptr %25, align 8, !tbaa !16
  br label %49

49:                                               ; preds = %39, %16
  %50 = getelementptr inbounds i8, ptr %0, i64 26
  %51 = load i16, ptr %50, align 2, !tbaa !17
  %52 = sub i16 %51, %31
  store i16 %52, ptr %50, align 2, !tbaa !17
  %.not = icmp eq i16 %2, 0
  br i1 %.not, label %57, label %53

53:                                               ; preds = %49
  %54 = getelementptr inbounds i8, ptr %0, i64 48
  %55 = load ptr, ptr %54, align 8, !tbaa !18
  %56 = tail call i64 @xQueueGenericSend(ptr noundef %55, ptr noundef null, i32 noundef 0, i64 noundef 0) #4
  br label %57

57:                                               ; preds = %53, %49
  %58 = load ptr, ptr %17, align 8, !tbaa !14
  %59 = tail call i64 @xQueueGenericSend(ptr noundef %58, ptr noundef null, i32 noundef 0, i64 noundef 0) #4
  br label %.loopexit

.loopexit:                                        ; preds = %13, %3, %5, %57
  %.0 = phi i32 [ %32, %57 ], [ -1, %5 ], [ -1, %3 ], [ -1, %13 ]
  ret i32 %.0
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.umin.i16(i16, i16) #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