// File: /workspace/github_c_makefile_star2_O1/hiperiondev[P]hpsdrsim/src/hpsdr_newprotocol.c
// Function: ddc_specific_thread
void* ddc_specific_thread(void *data) {
    dbg_printf(1, "-- Start ddc_specific_thread port: %d\n", ddc_port);
    int sock;
    struct sockaddr_in addr;
    socklen_t lenaddr = sizeof(addr);
    unsigned long seqnum, seqold;
    struct timeval tv;
    unsigned char buffer[2000];
    int yes = 1;
    int rc;
    int i, j;

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
        dbg_printf(1, "***** ERROR: RX specific: socket\n");
        return NULL;
    }

    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &yes, sizeof(yes));
    setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void*) &yes, sizeof(yes));
    tv.tv_sec = 0;
    tv.tv_usec = 10000;
    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void*) &tv, sizeof(tv));

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(ddc_port);

    if (bind(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
        dbg_printf(1, "***** ERROR: RX specific: bind\n");
        close(sock);
        return NULL;
    }

    seqnum = 0;
    while (run) {
        rc = recvfrom(sock, buffer, 1444, 0, (struct sockaddr*) &addr, &lenaddr);
        if (rc < 0 && errno != EAGAIN) {
            dbg_printf(1, "***** ERROR: DDC specific thread: recvmsg\n");
            break;
        }
        if (rc < 0)
            continue;
        if (rc != 1444) {
            dbg_printf(1, "RXspec: Received DDC specific packet with incorrect length\n");
            break;
        }
        seqold = seqnum;
        seqnum = (buffer[0] >> 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
        if (seqnum != 0 && seqnum != seqold + 1) {
            dbg_printf(1, "RXspec: SEQ ERROR, old=%lu new=%lu\n", seqold, seqnum);
        }
        if (adc != buffer[4]) {
            adc = buffer[4];
            dbg_printf(1, "RX: Number of ADCs: %d\n", adc);
        }
        for (i = 0; i < adc; i++) {
            rc = (buffer[5] >> i) & 0x01;
            if (rc != adcdither[i]) {
                adcdither[i] = rc;
                dbg_printf(1, "RX: ADC%d dither=%d\n", i, rc);
            }
        }
        for (i = 0; i < adc; i++) {
            rc = (buffer[6] >> i) & 0x01;
            if (rc != adcrandom[i]) {
                adcrandom[i] = rc;
                dbg_printf(1, "RX: ADC%d random=%d\n", i, rc);
            }
        }

        for (i = 0; i < NUMRECEIVERS; i++) {
            int modified = 0;

            rc = buffer[17 + 6 * i];
            if (rc != adcmap[i]) {
                modified = 1;
                adcmap[i] = rc;
            }

            rc = (buffer[18 + 6 * i] << 8) + buffer[19 + 6 * i];
            if (rc != rxrate[i]) {
                modified = 1;
                rxrate[i] = rc;
                modified = 1;
            }

            if (syncddc[i] != buffer[1363 + i]) {
                syncddc[i] = buffer[1363 + i];
                modified = 1;
            }
            rc = (buffer[7 + (i / 8)] >> (i % 8)) & 0x01;
            if (rc != ddcenable[i]) {
                modified = 1;
                ddcenable[i] = rc;
            }
            if (modified) {
                dbg_printf(1, "RX: DDC%d Enable=%d ADC%d Rate=%d SyncMap=%02x\n", i, ddcenable[i], adcmap[i], rxrate[i], syncddc[i]);
                rc = 0;
                for (j = 0; j < 8; j++) {
                    rc += (syncddc[i] >> i) & 0x01;
                }
                if (rc > 1) {
                    dbg_printf(1, "WARNING:\n");
                    dbg_printf(1, "WARNING:\n");
                    dbg_printf(1, "WARNING:\n");
                    dbg_printf(1, "WARNING: more than two DDC sync'ed\n");
                    dbg_printf(1, "WARNING: this system is not prepared to handle this case\n");
                    dbg_printf(1, "WARNING: so are most of SDRs around!\n");
                    dbg_printf(1, "WARNING:\n");
                    dbg_printf(1, "WARNING:\n");
                    dbg_printf(1, "WARNING:\n");
                }
            }
        }
    }
    close(sock);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x818, %rsp            # imm = 0x818
movl 0x954d(%rip), %edx      # 0xc2f0
leaq 0x56d9(%rip), %rsi      # 0x8483
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
movl $0x10, 0x14(%rsp)
movl $0x1, 0x10(%rsp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x31a0
movl %eax, %ebx
leaq 0x10(%rsp), %r14
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
movl $0x4, %r8d
callq 0x1070
movl %ebx, %edi
movl $0x1, %esi
movl $0xf, %edx
movq %r14, %rcx
movl $0x4, %r8d
callq 0x1070
leaq 0x30(%rsp), %rcx
movq $0x0, (%rcx)
movq $0x2710, 0x8(%rcx)      # imm = 0x2710
movl %ebx, %edi
movl $0x1, %esi
movl $0x14, %edx
movl $0x10, %r8d
callq 0x1070
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movaps %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movzwl 0x948d(%rip), %eax      # 0xc2f0
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x31b5
leaq 0x40(%rsp), %r12
leaq 0x568c(%rip), %r15      # 0x8517
leaq 0x952e(%rip), %r13      # 0xc3c0
leaq 0x5750(%rip), %r14      # 0x85e9
xorl %ebp, %ebp
cmpl $0x0, 0x943e(%rip)      # 0xc2e0
je 0x31d9
movl $0x5a4, %edx            # imm = 0x5A4
movl %ebx, %edi
movq %r12, %rsi
xorl %ecx, %ecx
leaq 0x20(%rsp), %r8
leaq 0x14(%rsp), %r9
callq 0x1140
testl %eax, %eax
jns 0x2ed6
callq 0x1030
cmpl $0xb, (%rax)
je 0x2e9b
jmp 0x31c3
cmpl $0x5a4, %eax            # imm = 0x5A4
jne 0x31ca
movzbl 0x41(%rsp), %eax
shll $0x10, %eax
movzbl 0x42(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x43(%rsp), %r15d
orq %rcx, %r15
testq %r15, %r15
sete %al
leaq 0x1(%rbp), %rcx
cmpq %rcx, %r15
sete %cl
orb %al, %cl
jne 0x2f29
movl $0x1, %edi
leaq 0x5637(%rip), %rsi      # 0x8553
movq %rbp, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x13aa
movq %r15, 0x18(%rsp)
movzbl 0x44(%rsp), %edx
cmpl %edx, 0x95b7(%rip)      # 0xc4f0
leaq 0x564f(%rip), %r12      # 0x858f
je 0x2f5b
movl %edx, 0x95a8(%rip)      # 0xc4f0
movl $0x1, %edi
leaq 0x5623(%rip), %rsi      # 0x8577
xorl %eax, %eax
callq 0x13aa
cmpl $0x0, 0x958e(%rip)      # 0xc4f0
jle 0x2fa6
leaq 0x93f5(%rip), %r15      # 0xc360
xorl %ebp, %ebp
movzbl 0x45(%rsp), %eax
xorl %ecx, %ecx
btl %ebp, %eax
setb %cl
cmpl (%r15), %ecx
je 0x2f93
movl %ecx, (%r15)
movl $0x1, %edi
movq %r12, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x13aa
incq %rbp
movslq 0x9553(%rip), %rax      # 0xc4f0
addq $0x4, %r15
cmpq %rax, %rbp
jl 0x2f6d
cmpl $0x0, 0x9543(%rip)      # 0xc4f0
leaq 0x55f0(%rip), %r12      # 0x85a4
jle 0x2ff8
leaq 0x93c3(%rip), %r15      # 0xc380
xorl %ebp, %ebp
movzbl 0x46(%rsp), %eax
xorl %ecx, %ecx
btl %ebp, %eax
setb %cl
cmpl (%r15), %ecx
je 0x2fe5
movl %ecx, (%r15)
movl $0x1, %edi
movq %r12, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x13aa
incq %rbp
movslq 0x9501(%rip), %rax      # 0xc4f0
addq $0x4, %r15
cmpq %rax, %rbp
jl 0x2fbf
xorl %r12d, %r12d
leaq 0x53(%rsp), %r15
xorl %ebp, %ebp
movzbl -0x2(%r15), %ecx
xorl %eax, %eax
leaq 0x93a0(%rip), %rdx      # 0xc3b0
cmpl %ecx, (%r12,%rdx)
je 0x3026
leaq 0x9393(%rip), %rax      # 0xc3b0
movl %ecx, (%r12,%rax)
movl $0x1, %eax
movzwl -0x1(%r15), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
leaq 0x94c7(%rip), %rdx      # 0xc500
cmpl (%r12,%rdx), %ecx
je 0x304f
leaq 0x94ba(%rip), %rax      # 0xc500
movl %ecx, (%r12,%rax)
movl $0x1, %eax
movzbl 0x593(%rsp,%rbp), %ecx
cmpl %ecx, (%r12,%r13)
je 0x3066
movl %ecx, (%r12,%r13)
movl $0x1, %eax
movzbl 0x47(%rsp), %edx
xorl %ecx, %ecx
btl %ebp, %edx
setb %cl
leaq 0x9326(%rip), %rdx      # 0xc3a0
cmpl (%r12,%rdx), %ecx
je 0x3090
leaq 0x9319(%rip), %rax      # 0xc3a0
movl %ecx, (%r12,%rax)
movl $0x1, %eax
testl %eax, %eax
je 0x3175
leaq 0x9301(%rip), %rax      # 0xc3a0
movl (%r12,%rax), %ecx
leaq 0x9306(%rip), %rax      # 0xc3b0
movl (%r12,%rax), %r8d
leaq 0x944b(%rip), %rax      # 0xc500
movl (%r12,%rax), %r9d
movl (%r12,%r13), %eax
movl %eax, (%rsp)
movl $0x1, %edi
leaq 0x54ed(%rip), %rsi      # 0x85b9
movl %ebp, %edx
xorl %eax, %eax
callq 0x13aa
movl (%r12,%r13), %eax
btl %ebp, %eax
jae 0x3175
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13aa
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13aa
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13aa
movl $0x1, %edi
leaq 0x54d8(%rip), %rsi      # 0x85f3
xorl %eax, %eax
callq 0x13aa
movl $0x1, %edi
leaq 0x54e9(%rip), %rsi      # 0x8617
xorl %eax, %eax
callq 0x13aa
movl $0x1, %edi
leaq 0x5510(%rip), %rsi      # 0x8651
xorl %eax, %eax
callq 0x13aa
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13aa
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13aa
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13aa
incq %rbp
addq $0x6, %r15
addq $0x4, %r12
cmpq $0x4, %rbp
jne 0x3002
movq 0x18(%rsp), %rbp
leaq 0x5381(%rip), %r15      # 0x8517
leaq 0x40(%rsp), %r12
jmp 0x2e9b
leaq 0x5303(%rip), %rsi      # 0x84aa
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
jmp 0x31e0
leaq 0x5310(%rip), %rsi      # 0x84cc
movl $0x1, %edi
jmp 0x31d2
leaq 0x5322(%rip), %r15      # 0x84ec
movl $0x1, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x13aa
movl %ebx, %edi
callq 0x10f0
xorl %eax, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq