; 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

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

declare i32 @ulSetInterruptMask() #2

declare i64 @xTaskGenericNotifyFromISR(ptr noundef, i64 noundef, i32 noundef, i32 noundef, ptr noundef, ptr noundef) #2 section "privileged_functions"

declare void @vClearInterruptMask(i32 noundef) #2

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

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

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

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

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

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

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

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

31:                                               ; preds = %24
  store i64 8, ptr %12, align 8, !tbaa !9
  br label %33

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

33:                                               ; preds = %32, %31
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = call i64 @prvBytesInBuffer(ptr noundef %34)
  store i64 %35, ptr %11, align 8, !tbaa !9
  %36 = load i64, ptr %11, align 8, !tbaa !9
  %37 = load i64, ptr %12, align 8, !tbaa !9
  %38 = icmp ugt i64 %36, %37
  br i1 %38, label %39, label %67

39:                                               ; preds = %33
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i64, ptr %7, align 8, !tbaa !9
  %43 = load i64, ptr %11, align 8, !tbaa !9
  %44 = call i64 @prvReadMessageFromBuffer(ptr noundef %40, ptr noundef %41, i64 noundef %42, i64 noundef %43)
  store i64 %44, ptr %10, align 8, !tbaa !9
  %45 = load i64, ptr %10, align 8, !tbaa !9
  %46 = icmp ne i64 %45, 0
  br i1 %46, label %47, label %65

47:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %48 = call i32 @ulSetInterruptMask()
  %49 = zext i32 %48 to i64
  store i64 %49, ptr %13, align 8, !tbaa !9
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %50, i32 0, i32 5
  %52 = load volatile ptr, ptr %51, align 8, !tbaa !15
  %53 = icmp ne ptr %52, null
  br i1 %53, label %54, label %62

54:                                               ; preds = %47
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %55, i32 0, i32 5
  %57 = load volatile ptr, ptr %56, align 8, !tbaa !15
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = call i64 @xTaskGenericNotifyFromISR(ptr noundef %57, i64 noundef 0, i32 noundef 0, i32 noundef 0, ptr noundef null, ptr noundef %58)
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %60, i32 0, i32 5
  store volatile ptr null, ptr %61, align 8, !tbaa !15
  br label %62

62:                                               ; preds = %54, %47
  %63 = load i64, ptr %13, align 8, !tbaa !9
  %64 = trunc i64 %63 to i32
  call void @vClearInterruptMask(i32 noundef %64)
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  br label %66

65:                                               ; preds = %39
  br label %66

66:                                               ; preds = %65, %62
  br label %68

67:                                               ; preds = %33
  br label %68

68:                                               ; preds = %67, %66
  %69 = 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 %69
}

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 = { 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 = !{i64 2148094185}
!12 = !{i64 2148094327}
!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}


; 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 hidden i64 @prvBytesInBuffer(ptr noundef) local_unnamed_addr #0 section "privileged_functions"

declare i32 @ulSetInterruptMask() local_unnamed_addr #1

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

declare void @vClearInterruptMask(i32 noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @xStreamBufferReceiveFromISR(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #0 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
  %17 = tail call i64 @prvBytesInBuffer(ptr noundef nonnull %0)
  %18 = icmp ugt i64 %17, %.
  br i1 %18, label %19, label %29

19:                                               ; preds = %12
  %20 = tail call i64 @prvReadMessageFromBuffer(ptr noundef nonnull %0, ptr noundef nonnull %1, i64 noundef %2, i64 noundef %17)
  %.not22 = icmp eq i64 %20, 0
  br i1 %.not22, label %29, label %21

21:                                               ; preds = %19
  %22 = tail call i32 @ulSetInterruptMask() #2
  %23 = getelementptr inbounds i8, ptr %0, i64 40
  %24 = load volatile ptr, ptr %23, align 8, !tbaa !13
  %.not23 = icmp eq ptr %24, null
  br i1 %.not23, label %28, label %25

25:                                               ; preds = %21
  %26 = load volatile ptr, ptr %23, align 8, !tbaa !13
  %27 = tail call i64 @xTaskGenericNotifyFromISR(ptr noundef %26, i64 noundef 0, i32 noundef 0, i32 noundef 0, ptr noundef null, ptr noundef %3) #2
  store volatile ptr null, ptr %23, align 8, !tbaa !13
  br label %28

28:                                               ; preds = %25, %21
  tail call void @vClearInterruptMask(i32 noundef %22) #2
  br label %29

29:                                               ; preds = %12, %28, %19
  %.0 = phi i64 [ %20, %28 ], [ 0, %19 ], [ 0, %12 ]
  ret i64 %.0
}

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 = { 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 2148094185}
!6 = !{i64 2148094327}
!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 40}


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