; LLVM IR from pms67/HadesFCS :: Firmware/F4Quad/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.xSTREAM_BUFFER = type { i64, i64, i64, i64, ptr, ptr, ptr, i8 }

; 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

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @xStreamBufferSend(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) #2 {
  %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.xSTREAM_BUFFER, 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 %35

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
  br label %36

35:                                               ; preds = %25
  br label %36

36:                                               ; preds = %35, %32
  %37 = load i32, ptr %8, align 4, !tbaa !11
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %70

39:                                               ; preds = %36
  call void @vTaskSetTimeOutState(ptr noundef %13)
  br label %40

40:                                               ; preds = %66, %39
  call void @vPortEnterCritical()
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = call i64 @xStreamBufferSpacesAvailable(ptr noundef %41)
  store i64 %42, ptr %11, align 8, !tbaa !9
  %43 = load i64, ptr %11, align 8, !tbaa !9
  %44 = load i64, ptr %12, align 8, !tbaa !9
  %45 = icmp ult i64 %43, %44
  br i1 %45, label %46, label %60

46:                                               ; preds = %40
  %47 = call i64 @xTaskNotifyStateClear(ptr noundef null)
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.xSTREAM_BUFFER, ptr %48, i32 0, i32 5
  %50 = load volatile ptr, ptr %49, align 8, !tbaa !15
  %51 = icmp eq ptr %50, null
  %52 = zext i1 %51 to i32
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %56

54:                                               ; preds = %46
  call void @vPortRaiseBASEPRI()
  br label %55

55:                                               ; preds = %55, %54
  br label %55

56:                                               ; preds = %46
  %57 = call ptr @xTaskGetCurrentTaskHandle()
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.xSTREAM_BUFFER, ptr %58, i32 0, i32 5
  store volatile ptr %57, ptr %59, align 8, !tbaa !15
  br label %61

60:                                               ; preds = %40
  call void @vPortExitCritical()
  br label %69

61:                                               ; preds = %56
  call void @vPortExitCritical()
  %62 = load i32, ptr %8, align 4, !tbaa !11
  %63 = call i64 @xTaskNotifyWait(i32 noundef 0, i32 noundef -1, ptr noundef null, i32 noundef %62)
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.xSTREAM_BUFFER, ptr %64, i32 0, i32 5
  store volatile ptr null, ptr %65, align 8, !tbaa !15
  br label %66

66:                                               ; preds = %61
  %67 = call i64 @xTaskCheckForTimeOut(ptr noundef %13, ptr noundef %8)
  %68 = icmp eq i64 %67, 0
  br i1 %68, label %40, label %69, !llvm.loop !16

69:                                               ; preds = %66, %60
  br label %71

70:                                               ; preds = %36
  br label %71

71:                                               ; preds = %70, %69
  %72 = load i64, ptr %11, align 8, !tbaa !9
  %73 = icmp eq i64 %72, 0
  br i1 %73, label %74, label %77

74:                                               ; preds = %71
  %75 = load ptr, ptr %9, align 8, !tbaa !5
  %76 = call i64 @xStreamBufferSpacesAvailable(ptr noundef %75)
  store i64 %76, ptr %11, align 8, !tbaa !9
  br label %78

77:                                               ; preds = %71
  br label %78

78:                                               ; preds = %77, %74
  %79 = load ptr, ptr %9, align 8, !tbaa !5
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = load i64, ptr %7, align 8, !tbaa !9
  %82 = load i64, ptr %11, align 8, !tbaa !9
  %83 = load i64, ptr %12, align 8, !tbaa !9
  %84 = call i64 @prvWriteMessageToBuffer(ptr noundef %79, ptr noundef %80, i64 noundef %81, i64 noundef %82, i64 noundef %83)
  store i64 %84, ptr %10, align 8, !tbaa !9
  %85 = load i64, ptr %10, align 8, !tbaa !9
  %86 = icmp ugt i64 %85, 0
  br i1 %86, label %87, label %110

87:                                               ; preds = %78
  %88 = load ptr, ptr %9, align 8, !tbaa !5
  %89 = call i64 @prvBytesInBuffer(ptr noundef %88)
  %90 = load ptr, ptr %9, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.xSTREAM_BUFFER, ptr %90, i32 0, i32 3
  %92 = load i64, ptr %91, align 8, !tbaa !18
  %93 = icmp uge i64 %89, %92
  br i1 %93, label %94, label %108

94:                                               ; preds = %87
  call void @vTaskSuspendAll()
  %95 = load ptr, ptr %9, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.xSTREAM_BUFFER, ptr %95, i32 0, i32 4
  %97 = load volatile ptr, ptr %96, align 8, !tbaa !19
  %98 = icmp ne ptr %97, null
  br i1 %98, label %99, label %106

99:                                               ; preds = %94
  %100 = load ptr, ptr %9, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.xSTREAM_BUFFER, ptr %100, i32 0, i32 4
  %102 = load volatile ptr, ptr %101, align 8, !tbaa !19
  %103 = call i64 @xTaskGenericNotify(ptr noundef %102, i32 noundef 0, i32 noundef 0, ptr noundef null)
  %104 = load ptr, ptr %9, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.xSTREAM_BUFFER, ptr %104, i32 0, i32 4
  store volatile ptr null, ptr %105, align 8, !tbaa !19
  br label %106

106:                                              ; preds = %99, %94
  %107 = call i64 @xTaskResumeAll()
  br label %109

108:                                              ; preds = %87
  br label %109

109:                                              ; preds = %108, %106
  br label %111

110:                                              ; preds = %78
  br label %111

111:                                              ; preds = %110, %109
  %112 = 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 %112
}

declare void @vTaskSetTimeOutState(ptr noundef) #3

declare void @vPortEnterCritical() #3

declare i64 @xTaskNotifyStateClear(ptr noundef) #3

declare ptr @xTaskGetCurrentTaskHandle() #3

declare void @vPortExitCritical() #3

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

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

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

declare void @vTaskSuspendAll() #3

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

declare i64 @xTaskResumeAll() #3

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 = { 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 = { 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 = !{!"xSTREAM_BUFFER", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !7, i64 56}
!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

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @xStreamBufferSend(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #2 {
  %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 = shl i8 %16, 3
  %18 = and i8 %17, 8
  %19 = zext nneg i8 %18 to i64
  %spec.select = add i64 %19, %2
  %.not30 = icmp eq i32 %3, 0
  br i1 %.not30, label %.thread, label %20

20:                                               ; preds = %14
  call void @vTaskSetTimeOutState(ptr noundef nonnull %6) #4
  %21 = getelementptr inbounds i8, ptr %0, i64 40
  br label %22

22:                                               ; preds = %30, %20
  call void @vPortEnterCritical() #4
  %23 = call i64 @xStreamBufferSpacesAvailable(ptr noundef nonnull %0)
  %24 = icmp ult i64 %23, %spec.select
  br i1 %24, label %25, label %36

25:                                               ; preds = %22
  %26 = call i64 @xTaskNotifyStateClear(ptr noundef null) #4
  %27 = load volatile ptr, ptr %21, align 8, !tbaa !13
  %.not31 = icmp eq ptr %27, null
  br i1 %.not31, label %30, label %28

28:                                               ; preds = %25
  call void @vPortRaiseBASEPRI()
  br label %29

29:                                               ; preds = %29, %28
  br label %29

30:                                               ; preds = %25
  %31 = call ptr @xTaskGetCurrentTaskHandle() #4
  store volatile ptr %31, ptr %21, align 8, !tbaa !13
  call void @vPortExitCritical() #4
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = call i64 @xTaskNotifyWait(i32 noundef 0, i32 noundef -1, ptr noundef null, i32 noundef %32) #4
  store volatile ptr null, ptr %21, align 8, !tbaa !13
  %34 = call i64 @xTaskCheckForTimeOut(ptr noundef nonnull %6, ptr noundef nonnull %5) #4
  %35 = icmp eq i64 %34, 0
  br i1 %35, label %22, label %.loopexit, !llvm.loop !14

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

.loopexit:                                        ; preds = %30, %36
  %37 = icmp eq i64 %23, 0
  br i1 %37, label %.thread, label %39

.thread:                                          ; preds = %14, %.loopexit
  %38 = call i64 @xStreamBufferSpacesAvailable(ptr noundef nonnull %0)
  br label %39

39:                                               ; preds = %.loopexit, %.thread
  %.1 = phi i64 [ %38, %.thread ], [ %23, %.loopexit ]
  %40 = call i64 @prvWriteMessageToBuffer(ptr noundef nonnull %0, ptr noundef nonnull %1, i64 noundef %2, i64 noundef %.1, i64 noundef %spec.select)
  %.not32 = icmp eq i64 %40, 0
  br i1 %.not32, label %53, label %41

41:                                               ; preds = %39
  %42 = call i64 @prvBytesInBuffer(ptr noundef nonnull %0)
  %43 = getelementptr inbounds i8, ptr %0, i64 24
  %44 = load i64, ptr %43, align 8, !tbaa !16
  %.not33 = icmp ult i64 %42, %44
  br i1 %.not33, label %53, label %45

45:                                               ; preds = %41
  call void @vTaskSuspendAll() #4
  %46 = getelementptr inbounds i8, ptr %0, i64 32
  %47 = load volatile ptr, ptr %46, align 8, !tbaa !17
  %.not34 = icmp eq ptr %47, null
  br i1 %.not34, label %51, label %48

48:                                               ; preds = %45
  %49 = load volatile ptr, ptr %46, align 8, !tbaa !17
  %50 = call i64 @xTaskGenericNotify(ptr noundef %49, i32 noundef 0, i32 noundef 0, ptr noundef null) #4
  store volatile ptr null, ptr %46, align 8, !tbaa !17
  br label %51

51:                                               ; preds = %48, %45
  %52 = call i64 @xTaskResumeAll() #4
  br label %53

53:                                               ; preds = %39, %51, %41
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %6) #4
  ret i64 %40
}

declare void @vTaskSetTimeOutState(ptr noundef) local_unnamed_addr #3

declare void @vPortEnterCritical() local_unnamed_addr #3

declare i64 @xTaskNotifyStateClear(ptr noundef) local_unnamed_addr #3

declare ptr @xTaskGetCurrentTaskHandle() local_unnamed_addr #3

declare void @vPortExitCritical() local_unnamed_addr #3

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

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

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

declare void @vTaskSuspendAll() local_unnamed_addr #3

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

declare i64 @xTaskResumeAll() local_unnamed_addr #3

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 = { 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 = { 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 = !{!"xSTREAM_BUFFER", !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 = !{!"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