; LLVM IR from Eslam-Rizk/Elite-DriveGuard :: Application/Middlewares/Third_Party/FreeRTOS/Source (stream_buffer)

; 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.xTIME_OUT = type { i64, i32 }
%struct.StreamBufferDef_t = type { i64, i64, i64, i64, ptr, ptr, ptr, i8, i64 }

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @vPortRaiseBASEPRI() #1

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

declare void @vPortEnterCritical() #2

declare void @vPortExitCritical() #2

; Function Attrs: nounwind uwtable
declare dso_local i64 @xStreamBufferSpacesAvailable(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden i64 @prvBytesInBuffer(ptr noundef) #3

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

18:                                               ; preds = %4
  call void @vPortRaiseBASEPRI()
  br label %19

19:                                               ; preds = %19, %18
  br label %19

20:                                               ; preds = %4
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %25

23:                                               ; preds = %20
  call void @vPortRaiseBASEPRI()
  br label %24

24:                                               ; preds = %24, %23
  br label %24

25:                                               ; preds = %20
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %26, i32 0, i32 7
  %28 = load i8, ptr %27, align 8, !tbaa !13
  %29 = zext i8 %28 to i32
  %30 = and i32 %29, 1
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %43

32:                                               ; preds = %25
  %33 = load i64, ptr %12, align 8, !tbaa !9
  %34 = add i64 %33, 8
  store i64 %34, ptr %12, align 8, !tbaa !9
  %35 = load i64, ptr %12, align 8, !tbaa !9
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = icmp ugt i64 %35, %36
  %38 = zext i1 %37 to i32
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %42

40:                                               ; preds = %32
  call void @vPortRaiseBASEPRI()
  br label %41

41:                                               ; preds = %41, %40
  br label %41

42:                                               ; preds = %32
  br label %44

43:                                               ; preds = %25
  br label %44

44:                                               ; preds = %43, %42
  %45 = load i32, ptr %8, align 4, !tbaa !11
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %78

47:                                               ; preds = %44
  call void @vTaskSetTimeOutState(ptr noundef %13)
  br label %48

48:                                               ; preds = %74, %47
  call void @vPortEnterCritical()
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = call i64 @xStreamBufferSpacesAvailable(ptr noundef %49)
  store i64 %50, ptr %11, align 8, !tbaa !9
  %51 = load i64, ptr %11, align 8, !tbaa !9
  %52 = load i64, ptr %12, align 8, !tbaa !9
  %53 = icmp ult i64 %51, %52
  br i1 %53, label %54, label %68

54:                                               ; preds = %48
  %55 = call i64 @xTaskNotifyStateClear(ptr noundef null)
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %56, i32 0, i32 5
  %58 = load volatile ptr, ptr %57, align 8, !tbaa !15
  %59 = icmp eq ptr %58, null
  %60 = zext i1 %59 to i32
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %62, label %64

62:                                               ; preds = %54
  call void @vPortRaiseBASEPRI()
  br label %63

63:                                               ; preds = %63, %62
  br label %63

64:                                               ; preds = %54
  %65 = call ptr @xTaskGetCurrentTaskHandle()
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %66, i32 0, i32 5
  store volatile ptr %65, ptr %67, align 8, !tbaa !15
  br label %69

68:                                               ; preds = %48
  call void @vPortExitCritical()
  br label %77

69:                                               ; preds = %64
  call void @vPortExitCritical()
  %70 = load i32, ptr %8, align 4, !tbaa !11
  %71 = call i64 @xTaskNotifyWait(i32 noundef 0, i32 noundef 0, ptr noundef null, i32 noundef %70)
  %72 = load ptr, ptr %9, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %72, i32 0, i32 5
  store volatile ptr null, ptr %73, align 8, !tbaa !15
  br label %74

74:                                               ; preds = %69
  %75 = call i64 @xTaskCheckForTimeOut(ptr noundef %13, ptr noundef %8)
  %76 = icmp eq i64 %75, 0
  br i1 %76, label %48, label %77, !llvm.loop !16

77:                                               ; preds = %74, %68
  br label %79

78:                                               ; preds = %44
  br label %79

79:                                               ; preds = %78, %77
  %80 = load i64, ptr %11, align 8, !tbaa !9
  %81 = icmp eq i64 %80, 0
  br i1 %81, label %82, label %85

82:                                               ; preds = %79
  %83 = load ptr, ptr %9, align 8, !tbaa !5
  %84 = call i64 @xStreamBufferSpacesAvailable(ptr noundef %83)
  store i64 %84, ptr %11, align 8, !tbaa !9
  br label %86

85:                                               ; preds = %79
  br label %86

86:                                               ; preds = %85, %82
  %87 = load ptr, ptr %9, align 8, !tbaa !5
  %88 = load ptr, ptr %6, align 8, !tbaa !5
  %89 = load i64, ptr %7, align 8, !tbaa !9
  %90 = load i64, ptr %11, align 8, !tbaa !9
  %91 = load i64, ptr %12, align 8, !tbaa !9
  %92 = call i64 @prvWriteMessageToBuffer(ptr noundef %87, ptr noundef %88, i64 noundef %89, i64 noundef %90, i64 noundef %91)
  store i64 %92, ptr %10, align 8, !tbaa !9
  %93 = load i64, ptr %10, align 8, !tbaa !9
  %94 = icmp ugt i64 %93, 0
  br i1 %94, label %95, label %118

95:                                               ; preds = %86
  %96 = load ptr, ptr %9, align 8, !tbaa !5
  %97 = call i64 @prvBytesInBuffer(ptr noundef %96)
  %98 = load ptr, ptr %9, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %98, i32 0, i32 3
  %100 = load i64, ptr %99, align 8, !tbaa !18
  %101 = icmp uge i64 %97, %100
  br i1 %101, label %102, label %116

102:                                              ; preds = %95
  call void @vTaskSuspendAll()
  %103 = load ptr, ptr %9, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %103, i32 0, i32 4
  %105 = load volatile ptr, ptr %104, align 8, !tbaa !19
  %106 = icmp ne ptr %105, null
  br i1 %106, label %107, label %114

107:                                              ; preds = %102
  %108 = load ptr, ptr %9, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %108, i32 0, i32 4
  %110 = load volatile ptr, ptr %109, align 8, !tbaa !19
  %111 = call i64 @xTaskGenericNotify(ptr noundef %110, i32 noundef 0, i32 noundef 0, ptr noundef null)
  %112 = load ptr, ptr %9, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %112, i32 0, i32 4
  store volatile ptr null, ptr %113, align 8, !tbaa !19
  br label %114

114:                                              ; preds = %107, %102
  %115 = call i64 @xTaskResumeAll()
  br label %117

116:                                              ; preds = %95
  br label %117

117:                                              ; preds = %116, %114
  br label %119

118:                                              ; preds = %86
  br label %119

119:                                              ; preds = %118, %117
  %120 = load i64, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret i64 %120
}

declare void @vTaskSetTimeOutState(ptr noundef) #2

declare i64 @xTaskNotifyStateClear(ptr noundef) #2

declare ptr @xTaskGetCurrentTaskHandle() #2

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

declare i64 @xTaskCheckForTimeOut(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i64 @prvWriteMessageToBuffer(ptr noundef, ptr noundef, i64 noundef, i64 noundef, i64 noundef) #3

declare void @vTaskSuspendAll() #2

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

declare i64 @xTaskResumeAll() #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { 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 #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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !7, i64 56}
!14 = !{!"StreamBufferDef_t", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !7, i64 56, !10, i64 64}
!15 = !{!14, !6, i64 40}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!14, !10, i64 24}
!19 = !{!14, !6, i64 32}


; 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.xTIME_OUT = type { i64, i32 }

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @vPortRaiseBASEPRI() local_unnamed_addr #1

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

declare void @vPortEnterCritical() local_unnamed_addr #2

declare void @vPortExitCritical() local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local i64 @xStreamBufferSpacesAvailable(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden i64 @prvBytesInBuffer(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local i64 @xStreamBufferSend(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #3 {
  %5 = alloca i32, align 4
  %6 = alloca %struct.xTIME_OUT, align 8
  store i32 %3, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %6) #4
  %7 = icmp eq ptr %1, null
  br i1 %7, label %8, label %10

8:                                                ; preds = %4
  tail call void @vPortRaiseBASEPRI()
  br label %9

9:                                                ; preds = %9, %8
  br label %9

10:                                               ; preds = %4
  %11 = icmp eq ptr %0, null
  br i1 %11, label %12, label %14

12:                                               ; preds = %10
  tail call void @vPortRaiseBASEPRI()
  br label %13

13:                                               ; preds = %13, %12
  br label %13

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %0, i64 56
  %16 = load i8, ptr %15, align 8, !tbaa !9
  %17 = and i8 %16, 1
  %.not = icmp eq i8 %17, 0
  br i1 %.not, label %23, label %18

18:                                               ; preds = %14
  %19 = add nuw i64 %2, 8
  %20 = icmp ugt i64 %2, -9
  br i1 %20, label %21, label %23

21:                                               ; preds = %18
  tail call void @vPortRaiseBASEPRI()
  br label %22

22:                                               ; preds = %22, %21
  br label %22

23:                                               ; preds = %14, %18
  %.0 = phi i64 [ %19, %18 ], [ %2, %14 ]
  %.not32 = icmp eq i32 %3, 0
  br i1 %.not32, label %.thread, label %24

24:                                               ; preds = %23
  call void @vTaskSetTimeOutState(ptr noundef nonnull %6) #4
  %25 = getelementptr inbounds i8, ptr %0, i64 40
  br label %26

26:                                               ; preds = %34, %24
  call void @vPortEnterCritical() #4
  %27 = call i64 @xStreamBufferSpacesAvailable(ptr noundef nonnull %0)
  %28 = icmp ult i64 %27, %.0
  br i1 %28, label %29, label %40

29:                                               ; preds = %26
  %30 = call i64 @xTaskNotifyStateClear(ptr noundef null) #4
  %31 = load volatile ptr, ptr %25, align 8, !tbaa !13
  %.not33 = icmp eq ptr %31, null
  br i1 %.not33, label %34, label %32

32:                                               ; preds = %29
  call void @vPortRaiseBASEPRI()
  br label %33

33:                                               ; preds = %33, %32
  br label %33

34:                                               ; preds = %29
  %35 = call ptr @xTaskGetCurrentTaskHandle() #4
  store volatile ptr %35, ptr %25, align 8, !tbaa !13
  call void @vPortExitCritical() #4
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = call i64 @xTaskNotifyWait(i32 noundef 0, i32 noundef 0, ptr noundef null, i32 noundef %36) #4
  store volatile ptr null, ptr %25, align 8, !tbaa !13
  %38 = call i64 @xTaskCheckForTimeOut(ptr noundef nonnull %6, ptr noundef nonnull %5) #4
  %39 = icmp eq i64 %38, 0
  br i1 %39, label %26, label %.loopexit, !llvm.loop !14

40:                                               ; preds = %26
  call void @vPortExitCritical() #4
  br label %.loopexit

.loopexit:                                        ; preds = %34, %40
  %41 = icmp eq i64 %27, 0
  br i1 %41, label %.thread, label %43

.thread:                                          ; preds = %23, %.loopexit
  %42 = call i64 @xStreamBufferSpacesAvailable(ptr noundef nonnull %0)
  br label %43

43:                                               ; preds = %.loopexit, %.thread
  %.1 = phi i64 [ %42, %.thread ], [ %27, %.loopexit ]
  %44 = call i64 @prvWriteMessageToBuffer(ptr noundef nonnull %0, ptr noundef nonnull %1, i64 noundef %2, i64 noundef %.1, i64 noundef %.0)
  %.not34 = icmp eq i64 %44, 0
  br i1 %.not34, label %57, label %45

45:                                               ; preds = %43
  %46 = call i64 @prvBytesInBuffer(ptr noundef nonnull %0)
  %47 = getelementptr inbounds i8, ptr %0, i64 24
  %48 = load i64, ptr %47, align 8, !tbaa !16
  %.not35 = icmp ult i64 %46, %48
  br i1 %.not35, label %57, label %49

49:                                               ; preds = %45
  call void @vTaskSuspendAll() #4
  %50 = getelementptr inbounds i8, ptr %0, i64 32
  %51 = load volatile ptr, ptr %50, align 8, !tbaa !17
  %.not36 = icmp eq ptr %51, null
  br i1 %.not36, label %55, label %52

52:                                               ; preds = %49
  %53 = load volatile ptr, ptr %50, align 8, !tbaa !17
  %54 = call i64 @xTaskGenericNotify(ptr noundef %53, i32 noundef 0, i32 noundef 0, ptr noundef null) #4
  store volatile ptr null, ptr %50, align 8, !tbaa !17
  br label %55

55:                                               ; preds = %52, %49
  %56 = call i64 @xTaskResumeAll() #4
  br label %57

57:                                               ; preds = %43, %55, %45
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %6) #4
  ret i64 %44
}

declare void @vTaskSetTimeOutState(ptr noundef) local_unnamed_addr #2

declare i64 @xTaskNotifyStateClear(ptr noundef) local_unnamed_addr #2

declare ptr @xTaskGetCurrentTaskHandle() local_unnamed_addr #2

declare i64 @xTaskNotifyWait(i32 noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare i64 @xTaskCheckForTimeOut(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i64 @prvWriteMessageToBuffer(ptr noundef, ptr noundef, i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #3

declare void @vTaskSuspendAll() local_unnamed_addr #2

declare i64 @xTaskGenericNotify(ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

declare i64 @xTaskResumeAll() local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { 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 #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, !7, i64 56}
!10 = !{!"StreamBufferDef_t", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !7, i64 56, !11, i64 64}
!11 = !{!"long", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!10, !12, i64 40}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!10, !11, i64 24}
!17 = !{!10, !12, i64 32}


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