// File: veghead[P]vintage-wireless-keypad/cletus.cpp
// Function: main
int main(int argc, char* argv[]) {
    std::string filename = "/dev/stdin";
    if (argc > 1) {
        filename = argv[1];
    }
    usage();
    std::ifstream data(filename, std::ios::binary|std::ios::in);
    uint8_t buf[2];
    int level = 0;
    int lowcount = 0;
    int lowlen = LOW_AFTER_SAMPLES;
    Cletus cletus;
    
    while(!data.eof()) {
        data.read((char *)buf,2);
        
        // convert the I/Q data to a nice real plot
        int i = buf[0] - 127;
        int q = buf[1] - 127;
        int sig = sqrt(i * i + q * q);

        // filter out everything but an on or off signal.
        if (sig > SIGNAL_THRESHOLD) {
            level = 1;
            lowcount = 0;
        } else if (level == 1) {
            if (lowcount++ > lowlen) {
                level = 0;
                lowcount = 0;
            }
        }
        cletus.processSample(level);
    }
    
    data.close();
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x300, %rsp            # imm = 0x300
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0x2250
movq -0x2f8(%rbp), %rdx
leaq 0x36bf(%rip), %rsi      # 0x60a6
leaq -0x30(%rbp), %rdi
callq 0x21c0
jmp 0x29f2
leaq -0x31(%rbp), %rdi
callq 0x2170
cmpl $0x1, -0x8(%rbp)
jle 0x2a41
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x30(%rbp), %rdi
callq 0x21b0
jmp 0x2a14
jmp 0x2a41
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x2170
jmp 0x2c36
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c2d
callq 0x2960
jmp 0x2a48
movl $0x4, %edi
movl $0x8, %esi
callq 0x2e90
movl %eax, -0x2fc(%rbp)
jmp 0x2a5f
movl -0x2fc(%rbp), %edx
leaq -0x250(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x2200
jmp 0x2a77
movl $0x0, -0x258(%rbp)
movl $0x0, -0x25c(%rbp)
movl $0xa, -0x260(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x2eb0
jmp 0x2aa3
jmp 0x2aa5
movq -0x250(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x250(%rbp,%rax), %rdi
callq 0x2260
movb %al, -0x2fd(%rbp)
jmp 0x2ac5
movb -0x2fd(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2ad6
jmp 0x2bdf
leaq -0x250(%rbp), %rdi
leaq -0x252(%rbp), %rsi
movl $0x2, %edx
callq 0x2190
jmp 0x2af0
movzbl -0x252(%rbp), %eax
subl $0x7f, %eax
movl %eax, -0x2e4(%rbp)
movzbl -0x251(%rbp), %eax
subl $0x7f, %eax
movl %eax, -0x2e8(%rbp)
movl -0x2e4(%rbp), %eax
imull -0x2e4(%rbp), %eax
movl -0x2e8(%rbp), %ecx
imull -0x2e8(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm0
callq 0x2100
cvttsd2si %xmm0, %eax
movl %eax, -0x2ec(%rbp)
cmpl $0xa, -0x2ec(%rbp)
jle 0x2b8c
movl $0x1, -0x258(%rbp)
movl $0x0, -0x25c(%rbp)
jmp 0x2bc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x32d0
jmp 0x2c21
cmpl $0x1, -0x258(%rbp)
jne 0x2bc4
movl -0x25c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x25c(%rbp)
cmpl -0x260(%rbp), %eax
jle 0x2bc2
movl $0x0, -0x258(%rbp)
movl $0x0, -0x25c(%rbp)
jmp 0x2bc4
jmp 0x2bc6
movl -0x258(%rbp), %esi
leaq -0x2e0(%rbp), %rdi
callq 0x26e0
jmp 0x2bda
jmp 0x2aa5
leaq -0x250(%rbp), %rdi
callq 0x2030
jmp 0x2bed
movl $0x0, -0x4(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x32d0
leaq -0x250(%rbp), %rdi
callq 0x2050
leaq -0x30(%rbp), %rdi
callq 0x20d0
movl -0x4(%rbp), %eax
addq $0x300, %rsp            # imm = 0x300
popq %rbp
retq
leaq -0x250(%rbp), %rdi
callq 0x2050
leaq -0x30(%rbp), %rdi
callq 0x20d0
movq -0x40(%rbp), %rdi
callq 0x2240
nop