#include <stdio.h>

static unsigned int pair2sci(unsigned int l[2]) {
//return((l[0]<<8) | l[1]);
return((256*l[0]) + l[1]);
}


unsigned int hash2integer(unsigned char h[32]) {
  unsigned int x = 0;
  unsigned int z = 0;
  for (int i = 0; i < 32; i++) {
    if (h[i] == 0) {
      x += 8;
      continue;
    } else if (h[i] < 2) {
      x += 7;
      z = h[i+1];
    } else if (h[i] < 4) {
      x += 6;
      z = (h[i+1] / 2) + ((h[i] % 2) * 128);
    } else if (h[i] < 8) {
      x += 5;
      z = (h[i+1] / 4) + ((h[i] % 4) * 64);
    } else if (h[i] < 16) {
      x += 4;
      z = (h[i+1] / 8) + ((h[i] % 8) * 32);
    } else if (h[i] < 32) {
      x += 3;
      z = (h[i+1] / 16) + ((h[i] % 16) * 16);
    } else if (h[i] < 64) {
      x += 2;
      z = (h[i+1] / 32) + ((h[i] % 32) * 8);
    } else if (h[i] < 128) {
      x += 1;
      z = (h[i+1] / 64) + ((h[i] % 64) * 4);
    } else {
      z = (h[i+1] / 128) + ((h[i] % 128) * 2);
    }
    break;
  }
  unsigned int y[2];
  y[0] = x;
  y[1] = z;
  return(pair2sci(y));
}

void main() {
        unsigned char h[32] = {
                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
        };
        for (int i=0; i<0xff; i++) {
                h[0] -= 1;
                printf("%x %d\n", h[0], hash2integer(h));
        }
}
