// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r14
pushq %rbx
movq %rdi, %rbx
movl 0x38(%rdi), %eax
cmpq $0x3, %rax
ja 0x272f
leaq 0x1a5c(%rip), %rcx      # 0x4008
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testl %esi, %esi
je 0x272f
movl $0x1, 0x38(%rbx)
andq $0x0, 0x50(%rbx)
andq $0x0, 0x40(%rbx)
jmp 0x272f
movq 0x50(%rbx), %rax
movq %rax, %rcx
subq 0x40(%rbx), %rcx
imulq $0x28, 0x48(%rbx), %rdx
cmpq %rdx, %rcx
jbe 0x262d
movl $0x3, 0x38(%rbx)
jmp 0x272f
movq %rbx, %rdi
callq 0x2534
jmp 0x272f
testl %esi, %esi
jne 0x272f
movl $0x2, 0x38(%rbx)
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rcx
subq 0x40(%rbx), %rcx
testq %rax, %rax
je 0x2645
testq %rcx, %rcx
js 0x267b
cvtsi2ss %rcx, %xmm0
jmp 0x2690
testl %esi, %esi
je 0x272f
movl $0x1, 0x38(%rbx)
movq %rax, 0x40(%rbx)
jmp 0x272f
movq %rcx, 0x48(%rbx)
xorl %r14d, %r14d
cmpb $0x1, 0x7c(%rbx)
jne 0x26c3
movq 0x3977(%rip), %rdi      # 0x5fd0
leaq 0x19d0(%rip), %rsi      # 0x4030
callq 0x2130
movq 0x48(%rbx), %rsi
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20a0
jmp 0x26c3
movq %rcx, %rdx
shrq %rdx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
testq %rax, %rax
js 0x269c
cvtsi2ss %rax, %xmm1
jmp 0x26b1
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
mulss 0x194b(%rip), %xmm1     # 0x4004
xorl %r14d, %r14d
ucomiss %xmm1, %xmm0
seta %r14b
movq %rbx, %rdi
movl %r14d, %esi
callq 0x236c
cmpb $0x1, 0x7c(%rbx)
jne 0x272f
movq 0x38f5(%rip), %rdi      # 0x5fd0
leaq 0x1955(%rip), %rsi      # 0x4037
callq 0x2130
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq 0x50(%rbx), %rsi
movq %rax, %rdi
callq 0x20d0
leaq 0x192d(%rip), %rsi      # 0x403c
movq %rax, %rdi
callq 0x2130
movq 0x50(%rbx), %rsi
subq 0x40(%rbx), %rsi
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20a0
incq 0x50(%rbx)
popq %rbx
popq %r14
popq %rbp
retq