// File: veghead[P]vintage-wireless-keypad/cletus.cpp
// Function: Cletus::processSample(int)
void Cletus::processSample(int level) {

    switch(state) {
        case(CletusState::QUIET):
            // All is quiet. Keep going until we get a signal
            if (level) {
                state = CletusState::MARK;
                lastchange = sample_num = 0;
            }
            break;
        case(CletusState::MARK):
            // We have our first high signal.
            // Read until we hit the next low and 
            // record the time as a "tick"
            if (!level) {
                state = CletusState::SPACE;
                if (0 == ticklength) {
                    ticklength = sample_num - lastchange;
                    if (debug) std::cout << "Tick: " << ticklength << std::endl;
                    foundbit(0);
                } else if (sample_num - lastchange > (ticklength * LONG_TICK_THRESHOLD)) {
                    foundbit(1);
                } else {
                    foundbit(0);
                }
                if (debug) std::cout << "SL: " << std::dec << sample_num << " L" << (sample_num - lastchange) << std::endl;
            }
            break;
        case(CletusState::SPACE):
            if ((sample_num - lastchange) > (ticklength * SILENCE_TICK_LENGTH)) {
                state = CletusState::ABORT;
            } else if (level) {
                state = CletusState::MARK;
                lastchange = sample_num;
            }
            break;
        case(CletusState::ABORT):
            // Start agin
            reset();
            break;
        default:
            break;
    }
    sample_num++;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movl 0x38(%rax), %eax
movq %rax, -0x18(%rbp)
subq $0x3, %rax
ja 0x293b
movq -0x18(%rbp), %rax
leaq 0x38f9(%rip), %rcx      # 0x600c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, -0xc(%rbp)
je 0x273d
movq -0x20(%rbp), %rax
movl $0x1, 0x38(%rax)
movq $0x0, 0x50(%rax)
movq $0x0, 0x40(%rax)
jmp 0x293d
cmpl $0x0, -0xc(%rbp)
jne 0x28ee
movq -0x20(%rbp), %rcx
movl $0x2, 0x38(%rcx)
xorl %eax, %eax
cmpq 0x48(%rcx), %rax
jne 0x27b7
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rcx
subq 0x40(%rax), %rcx
movq %rcx, 0x48(%rax)
testb $0x1, 0x7c(%rax)
je 0x27a7
movq 0x7854(%rip), %rdi      # 0x9fd0
leaq 0x38b1(%rip), %rsi      # 0x6034
callq 0x2140
movq %rax, %rdi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x2040
movq %rax, %rdi
movq 0x7816(%rip), %rsi      # 0x9fb8
callq 0x2160
movq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x23c0
jmp 0x287e
movq -0x20(%rbp), %rdx
movq 0x50(%rdx), %rax
movq 0x40(%rdx), %rcx
movq 0x48(%rdx), %rdx
movq %rdx, -0x30(%rbp)
subq %rcx, %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x28(%rbp)
testq %rax, %rax
movss %xmm0, -0x24(%rbp)
js 0x2803
movss -0x28(%rbp), %xmm0
movss %xmm0, -0x24(%rbp)
movq -0x30(%rbp), %rax
movss -0x24(%rbp), %xmm0
movss %xmm0, -0x3c(%rbp)
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x38(%rbp)
testq %rax, %rax
movss %xmm0, -0x34(%rbp)
js 0x2846
movss -0x38(%rbp), %xmm0
movss %xmm0, -0x34(%rbp)
movss -0x3c(%rbp), %xmm0
movss -0x34(%rbp), %xmm1
movss 0x37ac(%rip), %xmm2     # 0x6004
mulss %xmm2, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x2871
movq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x23c0
jmp 0x287c
movq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x23c0
jmp 0x287e
movq -0x20(%rbp), %rax
testb $0x1, 0x7c(%rax)
je 0x28ec
movq 0x7741(%rip), %rdi      # 0x9fd0
leaq 0x37a5(%rip), %rsi      # 0x603b
callq 0x2140
movq %rax, %rdi
leaq 0x5bb(%rip), %rsi       # 0x2e60
callq 0x2070
movq %rax, %rdi
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rsi
callq 0x2040
movq %rax, %rdi
leaq 0x377c(%rip), %rsi      # 0x6040
callq 0x2140
movq %rax, %rdi
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rsi
subq 0x40(%rax), %rsi
callq 0x2040
movq %rax, %rdi
movq 0x76d1(%rip), %rsi      # 0x9fb8
callq 0x2160
jmp 0x28ee
jmp 0x293d
movq -0x20(%rbp), %rcx
movq 0x50(%rcx), %rax
subq 0x40(%rcx), %rax
imulq $0x28, 0x48(%rcx), %rcx
cmpq %rcx, %rax
jbe 0x2913
movq -0x20(%rbp), %rax
movl $0x3, 0x38(%rax)
jmp 0x292e
cmpl $0x0, -0xc(%rbp)
je 0x292c
movq -0x20(%rbp), %rax
movl $0x1, 0x38(%rax)
movq 0x50(%rax), %rcx
movq %rcx, 0x40(%rax)
jmp 0x292e
jmp 0x293d
movq -0x20(%rbp), %rdi
callq 0x2650
jmp 0x293d
jmp 0x293d
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x50(%rax)
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)