// File: /workspace/github_c_makefile_star2_O2/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 0xa604(%rip), %edx      # 0xd2f0
leaq 0x5798(%rip), %rsi      # 0x848b
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
movl $0x10, 0x14(%rsp)
movl $0x1, 0x10(%rsp)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x3088
movl %eax, %ebx
pushq $0x1
popq %rbp
pushq $0x2
popq %rdx
leaq 0x10(%rsp), %r14
pushq $0x4
popq %r15
movl %eax, %edi
movl %ebp, %esi
movq %r14, %rcx
movl %r15d, %r8d
callq 0x1070
pushq $0xf
popq %rdx
movl %ebx, %edi
movl %ebp, %esi
movq %r14, %rcx
movl %r15d, %r8d
callq 0x1070
leaq 0x30(%rsp), %rcx
andq $0x0, (%rcx)
movq $0x2710, 0x8(%rcx)      # imm = 0x2710
pushq $0x14
popq %rdx
pushq $0x10
popq %r14
movl %ebx, %edi
movl %ebp, %esi
movl %r14d, %r8d
callq 0x1070
leaq 0x20(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
movzwl 0xa55f(%rip), %eax      # 0xd2f0
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl %r14d, %edx
callq 0x1150
testl %eax, %eax
js 0x309b
movl %ebx, 0xc(%rsp)
leaq 0x53(%rsp), %r13
leaq 0x5764(%rip), %r12      # 0x851f
leaq 0xa5de(%rip), %rbx      # 0xd3a0
leaq 0x5828(%rip), %r14      # 0x85f1
xorl %r15d, %r15d
cmpl $0x0, 0xa50d(%rip)      # 0xd2e0
je 0x30c4
movl $0x5a4, %edx            # imm = 0x5A4
movl 0xc(%rsp), %edi
leaq 0x40(%rsp), %rsi
xorl %ecx, %ecx
leaq 0x20(%rsp), %r8
leaq 0x14(%rsp), %r9
callq 0x1140
testl %eax, %eax
jns 0x2e0b
callq 0x1030
cmpl $0xb, (%rax)
je 0x2dcc
jmp 0x30b0
cmpl $0x5a4, %eax            # imm = 0x5A4
jne 0x30b7
movzbl 0x41(%rsp), %eax
shll $0x10, %eax
movzbl 0x42(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x43(%rsp), %r12d
orq %rcx, %r12
testq %r12, %r12
sete %al
leaq 0x1(%r15), %rcx
cmpq %rcx, %r12
sete %cl
orb %al, %cl
jne 0x2e5b
movl %ebp, %edi
leaq 0x570d(%rip), %rsi      # 0x855b
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x131a
movq %r12, 0x18(%rsp)
movl 0xa68a(%rip), %eax      # 0xd4f0
movzbl 0x44(%rsp), %edx
cmpl %edx, %eax
je 0x2e8b
movl %edx, 0xa67b(%rip)      # 0xd4f0
movl %ebp, %edi
leaq 0x5701(%rip), %rsi      # 0x857f
xorl %eax, %eax
callq 0x131a
movl 0xa665(%rip), %eax      # 0xd4f0
leaq 0xa4ce(%rip), %r12      # 0xd360
xorl %r15d, %r15d
movslq %eax, %rcx
cmpq %rcx, %r15
jge 0x2ed7
movzbl 0x45(%rsp), %edx
xorl %ecx, %ecx
btl %r15d, %edx
setb %cl
cmpl (%r12), %ecx
je 0x2ece
movl %ecx, (%r12)
movl %ebp, %edi
leaq 0x56d9(%rip), %rsi      # 0x8597
movl %r15d, %edx
xorl %eax, %eax
callq 0x131a
movl 0xa622(%rip), %eax      # 0xd4f0
incq %r15
addq $0x4, %r12
jmp 0x2e95
leaq 0xa4a2(%rip), %r12      # 0xd380
xorl %r15d, %r15d
movslq %eax, %rcx
cmpq %rcx, %r15
jge 0x2f23
movzbl 0x46(%rsp), %edx
xorl %ecx, %ecx
btl %r15d, %edx
setb %cl
cmpl (%r12), %ecx
je 0x2f1a
movl %ecx, (%r12)
movl %ebp, %edi
leaq 0x56a2(%rip), %rsi      # 0x85ac
movl %r15d, %edx
xorl %eax, %eax
callq 0x131a
movl 0xa5d6(%rip), %eax      # 0xd4f0
incq %r15
addq $0x4, %r12
jmp 0x2ee1
xorl %r12d, %r12d
xorl %r15d, %r15d
cmpq $0x10, %r12
je 0x3072
movzbl -0x2(%r13), %r8d
xorl %eax, %eax
leaq 0xa46f(%rip), %rcx      # 0xd3b0
cmpl %r8d, (%r12,%rcx)
je 0x2f54
leaq 0xa462(%rip), %rax      # 0xd3b0
movl %r8d, (%r12,%rax)
movl %ebp, %eax
movzwl -0x1(%r13), %ecx
rolw $0x8, %cx
movzwl %cx, %r9d
leaq 0xa598(%rip), %rcx      # 0xd500
cmpl (%r12,%rcx), %r9d
je 0x2f7b
leaq 0xa58b(%rip), %rax      # 0xd500
movl %r9d, (%r12,%rax)
movl %ebp, %eax
leaq 0xa43e(%rip), %rcx      # 0xd3c0
movl (%r12,%rcx), %edx
movzbl 0x593(%rsp,%r15), %ecx
cmpl %ecx, %edx
je 0x2fa2
leaq 0xa426(%rip), %rax      # 0xd3c0
movl %ecx, (%r12,%rax)
movl %ecx, %edx
movl %ebp, %eax
movzbl 0x47(%rsp), %esi
xorl %ecx, %ecx
btl %r15d, %esi
setb %cl
cmpl (%r12,%rbx), %ecx
jne 0x2fbf
testl %eax, %eax
jne 0x2fc3
jmp 0x3062
movl %ecx, (%r12,%rbx)
movl %edx, (%rsp)
movl %ebp, %edi
leaq 0x55f2(%rip), %rsi      # 0x85c1
movl %r15d, %edx
xorl %eax, %eax
callq 0x131a
leaq 0xa3e0(%rip), %rax      # 0xd3c0
movl (%r12,%rax), %eax
btl %r15d, %eax
jae 0x3062
movl %ebp, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x131a
movl %ebp, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x131a
movl %ebp, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x131a
movl %ebp, %edi
leaq 0x55e4(%rip), %rsi      # 0x85fb
xorl %eax, %eax
callq 0x131a
movl %ebp, %edi
leaq 0x55f8(%rip), %rsi      # 0x861f
xorl %eax, %eax
callq 0x131a
movl %ebp, %edi
leaq 0x5622(%rip), %rsi      # 0x8659
xorl %eax, %eax
callq 0x131a
movl %ebp, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x131a
movl %ebp, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x131a
movl %ebp, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x131a
incq %r15
addq $0x4, %r12
addq $0x6, %r13
jmp 0x2f29
movq 0x18(%rsp), %r15
leaq 0x54a1(%rip), %r12      # 0x851f
leaq 0x53(%rsp), %r13
jmp 0x2dcc
leaq 0x5423(%rip), %rsi      # 0x84b2
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
jmp 0x30cd
leaq 0x5432(%rip), %rsi      # 0x84d4
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
movl %ebx, %edi
jmp 0x30c8
leaq 0x543d(%rip), %r12      # 0x84f4
pushq $0x1
popq %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x131a
movl 0xc(%rsp), %edi
callq 0x10f0
xorl %eax, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq