; LLVM IR from CactiLab/Sherloc-Cortex-M-CFVD :: Sherloc_runtime/freertos (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.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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare void @vPortEnterCritical() #1

declare void @vPortExitCritical() #1

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

declare i64 @xTaskGenericNotifyStateClear(ptr noundef, i64 noundef) #1 section "privileged_functions"

declare ptr @xTaskGetCurrentTaskHandle() #1 section "privileged_functions"

declare i64 @xTaskGenericNotifyWait(i64 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef) #1 section "privileged_functions"

declare void @vTaskSuspendAll() #1 section "privileged_functions"

declare i64 @xTaskGenericNotify(ptr noundef, i64 noundef, i32 noundef, i32 noundef, ptr noundef) #1 section "privileged_functions"

declare i64 @xTaskResumeAll() #1 section "privileged_functions"

; Function Attrs: nounwind uwtable
define dso_local i64 @xStreamBufferReceive(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) #2 section "privileged_functions" {
  %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
  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) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %13, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 0, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %18

16:                                               ; preds = %4
  call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !13
  br label %17

17:                                               ; preds = %17, %16
  br label %17

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

21:                                               ; preds = %18
  call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !14
  br label %22

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

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

30:                                               ; preds = %23
  store i64 8, ptr %12, align 8, !tbaa !9
  br label %32

31:                                               ; preds = %23
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %31, %30
  %33 = load i32, ptr %8, align 4, !tbaa !11
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %69

35:                                               ; preds = %32
  call void @vPortEnterCritical()
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = call i64 @prvBytesInBuffer(ptr noundef %36)
  store i64 %37, ptr %11, align 8, !tbaa !9
  %38 = load i64, ptr %11, align 8, !tbaa !9
  %39 = load i64, ptr %12, align 8, !tbaa !9
  %40 = icmp ule i64 %38, %39
  br i1 %40, label %41, label %55

41:                                               ; preds = %35
  %42 = call i64 @xTaskGenericNotifyStateClear(ptr noundef null, i64 noundef 0)
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %43, i32 0, i32 4
  %45 = load volatile ptr, ptr %44, align 8, !tbaa !17
  %46 = icmp eq ptr %45, null
  %47 = zext i1 %46 to i32
  %48 = icmp eq i32 %47, 0
  br i1 %48, label %49, label %51

49:                                               ; preds = %41
  call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #3, !srcloc !18
  br label %50

50:                                               ; preds = %50, %49
  br label %50

51:                                               ; preds = %41
  %52 = call ptr @xTaskGetCurrentTaskHandle()
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %53, i32 0, i32 4
  store volatile ptr %52, ptr %54, align 8, !tbaa !17
  br label %56

55:                                               ; preds = %35
  br label %56

56:                                               ; preds = %55, %51
  call void @vPortExitCritical()
  %57 = load i64, ptr %11, align 8, !tbaa !9
  %58 = load i64, ptr %12, align 8, !tbaa !9
  %59 = icmp ule i64 %57, %58
  br i1 %59, label %60, label %67

60:                                               ; preds = %56
  %61 = load i32, ptr %8, align 4, !tbaa !11
  %62 = call i64 @xTaskGenericNotifyWait(i64 noundef 0, i32 noundef 0, i32 noundef 0, ptr noundef null, i32 noundef %61)
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %63, i32 0, i32 4
  store volatile ptr null, ptr %64, align 8, !tbaa !17
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = call i64 @prvBytesInBuffer(ptr noundef %65)
  store i64 %66, ptr %11, align 8, !tbaa !9
  br label %68

67:                                               ; preds = %56
  br label %68

68:                                               ; preds = %67, %60
  br label %72

69:                                               ; preds = %32
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = call i64 @prvBytesInBuffer(ptr noundef %70)
  store i64 %71, ptr %11, align 8, !tbaa !9
  br label %72

72:                                               ; preds = %69, %68
  %73 = load i64, ptr %11, align 8, !tbaa !9
  %74 = load i64, ptr %12, align 8, !tbaa !9
  %75 = icmp ugt i64 %73, %74
  br i1 %75, label %76, label %100

76:                                               ; preds = %72
  %77 = load ptr, ptr %9, align 8, !tbaa !5
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = load i64, ptr %7, align 8, !tbaa !9
  %80 = load i64, ptr %11, align 8, !tbaa !9
  %81 = call i64 @prvReadMessageFromBuffer(ptr noundef %77, ptr noundef %78, i64 noundef %79, i64 noundef %80)
  store i64 %81, ptr %10, align 8, !tbaa !9
  %82 = load i64, ptr %10, align 8, !tbaa !9
  %83 = icmp ne i64 %82, 0
  br i1 %83, label %84, label %98

84:                                               ; preds = %76
  call void @vTaskSuspendAll()
  %85 = load ptr, ptr %9, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %85, i32 0, i32 5
  %87 = load volatile ptr, ptr %86, align 8, !tbaa !19
  %88 = icmp ne ptr %87, null
  br i1 %88, label %89, label %96

89:                                               ; preds = %84
  %90 = load ptr, ptr %9, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %90, i32 0, i32 5
  %92 = load volatile ptr, ptr %91, align 8, !tbaa !19
  %93 = call i64 @xTaskGenericNotify(ptr noundef %92, i64 noundef 0, i32 noundef 0, i32 noundef 0, ptr noundef null)
  %94 = load ptr, ptr %9, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %94, i32 0, i32 5
  store volatile ptr null, ptr %95, align 8, !tbaa !19
  br label %96

96:                                               ; preds = %89, %84
  %97 = call i64 @xTaskResumeAll()
  br label %99

98:                                               ; preds = %76
  br label %99

99:                                               ; preds = %98, %96
  br label %101

100:                                              ; preds = %72
  br label %101

101:                                              ; preds = %100, %99
  %102 = load i64, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret i64 %102
}

; Function Attrs: nounwind uwtable
declare hidden i64 @prvReadMessageFromBuffer(ptr noundef, ptr noundef, i64 noundef, i64 noundef) #2 section "privileged_functions"

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { 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 = !{i64 2148091769}
!14 = !{i64 2148091911}
!15 = !{!16, !7, i64 56}
!16 = !{!"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}
!17 = !{!16, !6, i64 32}
!18 = !{i64 2148092317}
!19 = !{!16, !6, i64 40}


; 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"

declare void @vPortEnterCritical() local_unnamed_addr #0

declare void @vPortExitCritical() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i64 @prvBytesInBuffer(ptr noundef) local_unnamed_addr #1 section "privileged_functions"

declare i64 @xTaskGenericNotifyStateClear(ptr noundef, i64 noundef) local_unnamed_addr #0 section "privileged_functions"

declare ptr @xTaskGetCurrentTaskHandle() local_unnamed_addr #0 section "privileged_functions"

declare i64 @xTaskGenericNotifyWait(i64 noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0 section "privileged_functions"

declare void @vTaskSuspendAll() local_unnamed_addr #0 section "privileged_functions"

declare i64 @xTaskGenericNotify(ptr noundef, i64 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0 section "privileged_functions"

declare i64 @xTaskResumeAll() local_unnamed_addr #0 section "privileged_functions"

; Function Attrs: nounwind uwtable
define dso_local i64 @xStreamBufferReceive(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #1 section "privileged_functions" {
  %5 = icmp eq ptr %1, null
  br i1 %5, label %6, label %8

6:                                                ; preds = %4
  tail call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !5
  br label %7

7:                                                ; preds = %7, %6
  br label %7

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

10:                                               ; preds = %8
  tail call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !6
  br label %11

11:                                               ; preds = %11, %10
  br label %11

12:                                               ; preds = %8
  %13 = getelementptr inbounds i8, ptr %0, i64 56
  %14 = load i8, ptr %13, align 8, !tbaa !7
  %15 = shl i8 %14, 3
  %16 = and i8 %15, 8
  %. = zext nneg i8 %16 to i64
  %.not33 = icmp eq i32 %3, 0
  br i1 %.not33, label %29, label %17

17:                                               ; preds = %12
  tail call void @vPortEnterCritical() #2
  %18 = tail call i64 @prvBytesInBuffer(ptr noundef nonnull %0)
  %.not34 = icmp ugt i64 %18, %.
  br i1 %.not34, label %.critedge, label %19

19:                                               ; preds = %17
  %20 = tail call i64 @xTaskGenericNotifyStateClear(ptr noundef null, i64 noundef 0) #2
  %21 = getelementptr inbounds i8, ptr %0, i64 32
  %22 = load volatile ptr, ptr %21, align 8, !tbaa !13
  %.not35 = icmp eq ptr %22, null
  br i1 %.not35, label %25, label %23

23:                                               ; preds = %19
  tail call void asm sideeffect " cpsid i ", "~{memory},~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !14
  br label %24

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

25:                                               ; preds = %19
  %26 = tail call ptr @xTaskGetCurrentTaskHandle() #2
  store volatile ptr %26, ptr %21, align 8, !tbaa !13
  tail call void @vPortExitCritical() #2
  %27 = tail call i64 @xTaskGenericNotifyWait(i64 noundef 0, i32 noundef 0, i32 noundef 0, ptr noundef null, i32 noundef %3) #2
  store volatile ptr null, ptr %21, align 8, !tbaa !13
  %28 = tail call i64 @prvBytesInBuffer(ptr noundef nonnull %0)
  br label %31

29:                                               ; preds = %12
  %30 = tail call i64 @prvBytesInBuffer(ptr noundef nonnull %0)
  br label %31

.critedge:                                        ; preds = %17
  tail call void @vPortExitCritical() #2
  br label %31

31:                                               ; preds = %.critedge, %25, %29
  %.026 = phi i64 [ %28, %25 ], [ %30, %29 ], [ %18, %.critedge ]
  %32 = icmp ugt i64 %.026, %.
  br i1 %32, label %33, label %43

33:                                               ; preds = %31
  %34 = tail call i64 @prvReadMessageFromBuffer(ptr noundef nonnull %0, ptr noundef nonnull %1, i64 noundef %2, i64 noundef %.026)
  %.not36 = icmp eq i64 %34, 0
  br i1 %.not36, label %43, label %35

35:                                               ; preds = %33
  tail call void @vTaskSuspendAll() #2
  %36 = getelementptr inbounds i8, ptr %0, i64 40
  %37 = load volatile ptr, ptr %36, align 8, !tbaa !15
  %.not37 = icmp eq ptr %37, null
  br i1 %.not37, label %41, label %38

38:                                               ; preds = %35
  %39 = load volatile ptr, ptr %36, align 8, !tbaa !15
  %40 = tail call i64 @xTaskGenericNotify(ptr noundef %39, i64 noundef 0, i32 noundef 0, i32 noundef 0, ptr noundef null) #2
  store volatile ptr null, ptr %36, align 8, !tbaa !15
  br label %41

41:                                               ; preds = %38, %35
  %42 = tail call i64 @xTaskResumeAll() #2
  br label %43

43:                                               ; preds = %31, %41, %33
  %.027 = phi i64 [ %34, %41 ], [ 0, %33 ], [ 0, %31 ]
  ret i64 %.027
}

; Function Attrs: nounwind uwtable
declare hidden i64 @prvReadMessageFromBuffer(ptr noundef, ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1 section "privileged_functions"

attributes #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 = { 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 }

!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 = !{i64 2148091769}
!6 = !{i64 2148091911}
!7 = !{!8, !10, i64 56}
!8 = !{!"StreamBufferDef_t", !9, i64 0, !9, i64 8, !9, i64 16, !9, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !10, i64 56, !9, i64 64}
!9 = !{!"long", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!"any pointer", !10, i64 0}
!13 = !{!8, !12, i64 32}
!14 = !{i64 2148092317}
!15 = !{!8, !12, i64 40}


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