// File: /workspace/github_cpp_makefile_star2_O1/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 0x274b
leaq 0x193f(%rip), %rcx      # 0x4008
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testl %esi, %esi
je 0x274b
movl $0x1, 0x38(%rbx)
xorl %eax, %eax
movq %rax, 0x50(%rbx)
jmp 0x2747
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 0x273c
movl $0x3, 0x38(%rbx)
jmp 0x274b
movq %rbx, %rdi
callq 0x2628
jmp 0x274b
testl %esi, %esi
jne 0x274b
movl $0x2, 0x38(%rbx)
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rcx
subq 0x40(%rbx), %rcx
testq %rax, %rax
je 0x275a
testq %rcx, %rcx
js 0x2798
cvtsi2ss %rcx, %xmm0
jmp 0x27ad
testl %esi, %esi
je 0x274b
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 0x27e0
movq 0x3862(%rip), %r15      # 0x5fd0
leaq 0x18c3(%rip), %rsi      # 0x4038
movl $0x6, %edx
movq %r15, %rdi
callq 0x2180
movq 0x48(%rbx), %rsi
movq %r15, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20a0
jmp 0x27e0
movq %rcx, %rdx
shrq %rdx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
testq %rax, %rax
js 0x27b9
cvtsi2ss %rax, %xmm1
jmp 0x27ce
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
mulss 0x182e(%rip), %xmm1     # 0x4004
xorl %r14d, %r14d
ucomiss %xmm1, %xmm0
seta %r14b
movq %rbx, %rdi
movl %r14d, %esi
callq 0x23cc
cmpb $0x1, 0x7c(%rbx)
jne 0x274b
movq 0x37d4(%rip), %r14      # 0x5fd0
leaq 0x183c(%rip), %rsi      # 0x403f
movl $0x4, %edx
movq %r14, %rdi
callq 0x2180
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 0x1807(%rip), %rsi      # 0x4044
movl $0x2, %edx
movq %rax, %rdi
callq 0x2180
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 0x2160
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20e0
jmp 0x274b