#!/usr/bin/env bash
set -euo pipefail

elf="${1:?usage: check_task03_elf.sh ELF [tool-prefix]}"
prefix="${2:-riscv64-unknown-elf-}"
nm="${prefix}nm"
objdump="${prefix}objdump"

test -s "$elf"
for symbol in pvPortMalloc vPortFree prvInsertBlockIntoFreeList ucHeap \
              heap4_reset_checkpoint heap4_alloc_a_checkpoint \
              heap4_alloc_b_checkpoint heap4_alloc_c_checkpoint \
              heap4_free_a_checkpoint heap4_fragmented_checkpoint \
              heap4_coalesced_checkpoint task03_debug_checkpoint \
              g_initial_stats g_after_allocations_stats g_fragmented_stats \
              g_final_stats g_rv32_layout_expected g_task03_pass; do
  "$nm" -a "$elf" | grep -E "[[:space:]]${symbol}$" >/dev/null
done
"$objdump" -d "$elf" | grep -E 'addi[[:space:]]+sp,sp,-' >/dev/null
printf 'PASS Task 3 links upstream heap_4 and eight observable checkpoints: %s\n' "$elf"
