// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl 0x38(%rdi), %eax
cmpq $0x3, %rax
ja 0x2751
leaq 0x1939(%rip), %rcx      # 0x4008
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testl %esi, %esi
je 0x2751
movl $0x1, 0x38(%rbx)
xorl %eax, %eax
movq %rax, 0x50(%rbx)
jmp 0x274d
movq 0x50(%rbx), %rax
movq %rax, %rcx
subq 0x40(%rbx), %rcx
movq 0x48(%rbx), %rdx
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rdx
cmpq %rdx, %rcx
jbe 0x2742
movl $0x3, 0x38(%rbx)
jmp 0x2751
movq %rbx, %rdi
callq 0x262e
jmp 0x2751
testl %esi, %esi
jne 0x2751
movl $0x2, 0x38(%rbx)
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rcx
subq 0x40(%rbx), %rcx
testq %rax, %rax
je 0x2760
testq %rcx, %rcx
js 0x279e
cvtsi2ss %rcx, %xmm0
jmp 0x27b3
testl %esi, %esi
je 0x2751
movl $0x1, 0x38(%rbx)
movq %rax, 0x40(%rbx)
incq 0x50(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, 0x48(%rbx)
xorl %r14d, %r14d
cmpb $0x1, 0x7c(%rbx)
jne 0x27e6
movq 0x385c(%rip), %r15      # 0x5fd0
leaq 0x18b5(%rip), %rsi      # 0x4030
movl $0x6, %edx
movq %r15, %rdi
callq 0x2170
movq 0x48(%rbx), %rsi
movq %r15, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20a0
jmp 0x27e6
movq %rcx, %rdx
shrq %rdx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
testq %rax, %rax
js 0x27bf
cvtsi2ss %rax, %xmm1
jmp 0x27d4
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
mulss 0x1828(%rip), %xmm1     # 0x4004
xorl %r14d, %r14d
ucomiss %xmm1, %xmm0
seta %r14b
movq %rbx, %rdi
movl %r14d, %esi
callq 0x23bc
cmpb $0x1, 0x7c(%rbx)
jne 0x2751
movq 0x37ce(%rip), %r14      # 0x5fd0
leaq 0x182e(%rip), %rsi      # 0x4037
movl $0x4, %edx
movq %r14, %rdi
callq 0x2170
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%r14,%rax)
movq 0x50(%rbx), %rsi
movq %r14, %rdi
callq 0x20d0
movq %rax, %r14
leaq 0x17f9(%rip), %rsi      # 0x403c
movl $0x2, %edx
movq %rax, %rdi
callq 0x2170
movq 0x50(%rbx), %rsi
subq 0x40(%rbx), %rsi
movq %r14, %rdi
callq 0x20d0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20e0
jmp 0x2751