; LLVM IR from CactiLab/Sherloc-Cortex-M-CFVD :: Sherloc_runtime/freertos/portable/GCC/ARM_CM23/secure (secure_heap)

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

@xBlockAllocatedBit = external hidden global i64, align 8
@xFreeBytesRemaining = external hidden global i64, align 8

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

; Function Attrs: nounwind uwtable
declare hidden void @prvInsertBlockIntoFreeList(ptr noundef) #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
define dso_local void @vPortFree(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %5, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %8, label %60

8:                                                ; preds = %1
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %9, i64 -16
  store ptr %10, ptr %3, align 8, !tbaa !5
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %11, ptr %4, align 8, !tbaa !5
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %12, i32 0, i32 1
  %14 = load i64, ptr %13, align 8, !tbaa !9
  %15 = load i64, ptr @xBlockAllocatedBit, align 8, !tbaa !12
  %16 = and i64 %14, %15
  %17 = icmp ne i64 %16, 0
  %18 = zext i1 %17 to i32
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %22

20:                                               ; preds = %8
  call void asm sideeffect "msr primask, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #2, !srcloc !13
  call void asm sideeffect "msr primask_ns, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #2, !srcloc !14
  br label %21

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

22:                                               ; preds = %8
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = icmp eq ptr %25, null
  %27 = zext i1 %26 to i32
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %22
  call void asm sideeffect "msr primask, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #2, !srcloc !16
  call void asm sideeffect "msr primask_ns, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #2, !srcloc !17
  br label %30

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

31:                                               ; preds = %22
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !9
  %35 = load i64, ptr @xBlockAllocatedBit, align 8, !tbaa !12
  %36 = and i64 %34, %35
  %37 = icmp ne i64 %36, 0
  br i1 %37, label %38, label %58

38:                                               ; preds = %31
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !15
  %42 = icmp eq ptr %41, null
  br i1 %42, label %43, label %56

43:                                               ; preds = %38
  %44 = load i64, ptr @xBlockAllocatedBit, align 8, !tbaa !12
  %45 = xor i64 %44, -1
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %46, i32 0, i32 1
  %48 = load i64, ptr %47, align 8, !tbaa !9
  %49 = and i64 %48, %45
  store i64 %49, ptr %47, align 8, !tbaa !9
  call void asm sideeffect "msr primask_ns, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #2, !srcloc !18
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %50, i32 0, i32 1
  %52 = load i64, ptr %51, align 8, !tbaa !9
  %53 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !12
  %54 = add i64 %53, %52
  store i64 %54, ptr @xFreeBytesRemaining, align 8, !tbaa !12
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  call void @prvInsertBlockIntoFreeList(ptr noundef %55)
  call void asm sideeffect "msr primask_ns, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 0) #2, !srcloc !19
  br label %57

56:                                               ; preds = %38
  br label %57

57:                                               ; preds = %56, %43
  br label %59

58:                                               ; preds = %31
  br label %59

59:                                               ; preds = %58, %57
  br label %60

60:                                               ; preds = %59, %1
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret void
}

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 = !{!10, !11, i64 8}
!10 = !{!"A_BLOCK_LINK", !6, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{i64 2147781937}
!14 = !{i64 2147782093}
!15 = !{!10, !6, i64 0}
!16 = !{i64 2147782564}
!17 = !{i64 2147782720}
!18 = !{i64 2147782890}
!19 = !{i64 2147783048}


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

@xBlockAllocatedBit = external hidden local_unnamed_addr global i64, align 8
@xFreeBytesRemaining = external hidden local_unnamed_addr global i64, align 8

; Function Attrs: nounwind uwtable
declare hidden void @prvInsertBlockIntoFreeList(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @vPortFree(ptr noundef %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %20, label %2

2:                                                ; preds = %1
  %3 = getelementptr inbounds i8, ptr %0, i64 -16
  %4 = getelementptr inbounds i8, ptr %0, i64 -8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = load i64, ptr @xBlockAllocatedBit, align 8, !tbaa !11
  %7 = and i64 %6, %5
  %.not11 = icmp eq i64 %7, 0
  br i1 %.not11, label %8, label %10

8:                                                ; preds = %2
  tail call void asm sideeffect "msr primask, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #1, !srcloc !12
  tail call void asm sideeffect "msr primask_ns, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #1, !srcloc !13
  br label %9

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

10:                                               ; preds = %2
  %11 = load ptr, ptr %3, align 8, !tbaa !14
  %.not12 = icmp eq ptr %11, null
  br i1 %.not12, label %14, label %12

12:                                               ; preds = %10
  tail call void asm sideeffect "msr primask, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #1, !srcloc !15
  tail call void asm sideeffect "msr primask_ns, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #1, !srcloc !16
  br label %13

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

14:                                               ; preds = %10
  %15 = xor i64 %6, -1
  %16 = and i64 %5, %15
  store i64 %16, ptr %4, align 8, !tbaa !5
  tail call void asm sideeffect "msr primask_ns, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 1) #1, !srcloc !17
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !11
  %19 = add i64 %18, %17
  store i64 %19, ptr @xFreeBytesRemaining, align 8, !tbaa !11
  tail call void @prvInsertBlockIntoFreeList(ptr noundef nonnull %3)
  tail call void asm sideeffect "msr primask_ns, $0", "r,~{memory},~{dirflag},~{fpsr},~{flags}"(i32 0) #1, !srcloc !18
  br label %20

20:                                               ; preds = %14, %1
  ret void
}

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, !10, i64 8}
!6 = !{!"A_BLOCK_LINK", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{i64 2147781937}
!13 = !{i64 2147782093}
!14 = !{!6, !7, i64 0}
!15 = !{i64 2147782564}
!16 = !{i64 2147782720}
!17 = !{i64 2147782890}
!18 = !{i64 2147783048}


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