Solved by colesbury in post #11 
              
              
                The issue is that the line: 
b[:-1] = b[1:]

reads and writes overlapping elements of b. There’s a similar issue in the original snippet, which @bgobbi mentions in the original post. 
You can fix this by writing: 
b[:-1] = b[1:].clone()

At some point in the future, I’d like to add overlap detection…