; 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
define dso_local i64 @xStreamBufferSpacesAvailable(ptr noundef %0) #1 section "privileged_functions" {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %6, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %11

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

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

11:                                               ; preds = %1
  br label %12

12:                                               ; preds = %28, %11
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %13, i32 0, i32 0
  %15 = load volatile i64, ptr %14, align 8, !tbaa !10
  store i64 %15, ptr %5, align 8, !tbaa !13
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %16, i32 0, i32 2
  %18 = load i64, ptr %17, align 8, !tbaa !14
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %19, i32 0, i32 0
  %21 = load volatile i64, ptr %20, align 8, !tbaa !10
  %22 = add i64 %18, %21
  store i64 %22, ptr %4, align 8, !tbaa !13
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %23, i32 0, i32 1
  %25 = load volatile i64, ptr %24, align 8, !tbaa !15
  %26 = load i64, ptr %4, align 8, !tbaa !13
  %27 = sub i64 %26, %25
  store i64 %27, ptr %4, align 8, !tbaa !13
  br label %28

28:                                               ; preds = %12
  %29 = load i64, ptr %5, align 8, !tbaa !13
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %30, i32 0, i32 0
  %32 = load volatile i64, ptr %31, align 8, !tbaa !10
  %33 = icmp ne i64 %29, %32
  br i1 %33, label %12, label %34, !llvm.loop !16

34:                                               ; preds = %28
  %35 = load i64, ptr %4, align 8, !tbaa !13
  %36 = sub i64 %35, 1
  store i64 %36, ptr %4, align 8, !tbaa !13
  %37 = load i64, ptr %4, align 8, !tbaa !13
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %38, i32 0, i32 2
  %40 = load i64, ptr %39, align 8, !tbaa !14
  %41 = icmp uge i64 %37, %40
  br i1 %41, label %42, label %48

42:                                               ; preds = %34
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.StreamBufferDef_t, ptr %43, i32 0, i32 2
  %45 = load i64, ptr %44, align 8, !tbaa !14
  %46 = load i64, ptr %4, align 8, !tbaa !13
  %47 = sub i64 %46, %45
  store i64 %47, ptr %4, align 8, !tbaa !13
  br label %49

48:                                               ; preds = %34
  br label %49

49:                                               ; preds = %48, %42
  %50 = load i64, ptr %4, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i64 %50
}

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 = { 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 = !{i64 2148086972}
!10 = !{!11, !12, i64 0}
!11 = !{!"StreamBufferDef_t", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !7, i64 56, !12, i64 64}
!12 = !{!"long", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!11, !12, i64 16}
!15 = !{!11, !12, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; 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
define dso_local i64 @xStreamBufferSpacesAvailable(ptr noundef %0) local_unnamed_addr #0 section "privileged_functions" {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %6, label %.preheader

.preheader:                                       ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  br label %8

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

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

8:                                                ; preds = %.preheader, %8
  %9 = load volatile i64, ptr %0, align 8, !tbaa !12
  %10 = load volatile i64, ptr %0, align 8, !tbaa !12
  %11 = load volatile i64, ptr %5, align 8, !tbaa !13
  %12 = load volatile i64, ptr %0, align 8, !tbaa !12
  %.not = icmp eq i64 %9, %12
  br i1 %.not, label %13, label %8, !llvm.loop !14

13:                                               ; preds = %8
  %14 = add i64 %10, %4
  %15 = xor i64 %11, -1
  %16 = add i64 %14, %15
  %.not16 = icmp ult i64 %16, %4
  %17 = select i1 %.not16, i64 0, i64 %4
  %spec.select = sub i64 %16, %17
  ret i64 %spec.select
}

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


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