// "unsigned long" is 64-bit on x86_64 so changed to uint32_t to test this like it would be on a 32-bit platform
// commented out the delay logic so that it thinks it's just received all 1s
//
// output:
//  $ g++ buggy.cpp -o buggy && ./buggy
//  7fffffff


#include <stdio.h>
#include <stdint.h>

uint32_t receiveProtocol1(unsigned int changeCount){

      uint32_t /* unsigned long */ code = 0;
      //unsigned long delay = RCSwitch::timings[0] / 31;
      //unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01;    

      for (int i = 1; i<changeCount ; i=i+2) {

          if (0 /* RCSwitch::timings[i] > delay-delayTolerance && RCSwitch::timings[i] < delay+delayTolerance && RCSwitch::timings[i+1] > delay*3-delayTolerance && RCSwitch::timings[i+1] < delay*3+delayTolerance */) {
            code = code << 1;
          } else if (1 /*RCSwitch::timings[i] > delay*3-delayTolerance && RCSwitch::timings[i] < delay*3+delayTolerance && RCSwitch::timings[i+1] > delay-delayTolerance && RCSwitch::timings[i+1] < delay+delayTolerance*/) {
            code+=1;
            code = code << 1;
          } else {
            // Failed
            i = changeCount;
            code = 0;
          }
      }

      code = code >> 1;
      return code;

      /*
    if (changeCount > 6) {    // ignore < 4bit values as there are no devices sending 4bit values => noise
      RCSwitch::nReceivedValue = code;
      RCSwitch::nReceivedBitlength = changeCount / 2;
      RCSwitch::nReceivedDelay = delay;
      RCSwitch::nReceivedProtocol = 1;
    }

    if (code == 0){
        return false;
    }else if (code != 0){
        return true;
    }
    */

}

int main() {
   printf("%x\n", receiveProtocol1(65));
}