 #include <stdint.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <dlfcn.h>
 #include "sassi_intrinsics.h"
 #include "sassi_lazyallocator.hpp"
 #include <sassi/sassi-core.hpp>

static sassi::lazy_allocator counterInitializer(
  /* Initialize the counters. */
  []() {
    unsigned long long instr_counts[SASSI_NUM_OPCODES];
    bzero(instr_counts, sizeof(instr_counts));
    // Initialize the array we allocated on the device.
    CHECK_CUDA_ERROR(cudaMemcpyToSymbol(dynamic_instr_counts, &instr_counts, sizeof(instr_counts)));
  }, sassi_finalize);


///  Write out the statistics we've gathered.
/// 
///////////////////////////////////////////////////////////////////////////////////
void sassi_finalize(sassi::lazy_allocator::device_reset_reason reason)
{
  FILE *resultFile = fopen("sassi-ophist.txt", "w");
  for (unsigned i = 0; i < SASSI_NUM_OPCODES; i++) {
    if (dynamic_instr_counts[i] > 0) {
      fprintf(resultFile, "%-10.10s: %llu\n", SASSIInstrOpcodeStrings[i], dynamic_instr_counts[i]);
    }
  }
  fclose(resultFile);
