// File: /workspace/github_c_makefile_star2_O3/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 0x94ab(%rip), %edx      # 0xc2f0
leaq 0x5637(%rip), %rsi      # 0x8483
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
movl $0x10, 0x14(%rsp)
movl $0x1, 0x10(%rsp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x3225
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
xorl %r14d, %r14d
leaq 0x30(%rsp), %rcx
movq %r14, (%rcx)
movq $0x2710, 0x8(%rcx)      # imm = 0x2710
movl %ebx, %edi
movl $0x1, %esi
movl $0x14, %edx
movl $0x10, %r8d
callq 0x1070
leaq 0x20(%rsp), %rsi
movq %r14, 0x8(%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movzwl 0x93ee(%rip), %eax      # 0xc2f0
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x323a
leaq 0x40(%rsp), %r12
leaq 0x55ed(%rip), %r15      # 0x8517
leaq 0x946f(%rip), %r13      # 0xc3a0
leaq 0x56b1(%rip), %r14      # 0x85e9
xorl %ebp, %ebp
cmpl $0x0, 0x939f(%rip)      # 0xc2e0
je 0x325e
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 0x2f75
callq 0x1030
cmpl $0xb, (%rax)
je 0x2f3a
jmp 0x3248
cmpl $0x5a4, %eax            # imm = 0x5A4
jne 0x324f
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 0x2fc8
movl $0x1, %edi
leaq 0x5598(%rip), %rsi      # 0x8553
movq %rbp, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x13ba
movl 0x9522(%rip), %eax      # 0xc4f0
movzbl 0x44(%rsp), %edx
cmpl %edx, %eax
leaq 0x55c8(%rip), %r12      # 0x85a4
je 0x2ffd
movl %edx, 0x950c(%rip)      # 0xc4f0
movl $0x1, %edi
leaq 0x5587(%rip), %rsi      # 0x8577
xorl %eax, %eax
callq 0x13ba
movl 0x94f3(%rip), %eax      # 0xc4f0
movq %r15, 0x18(%rsp)
testl %eax, %eax
jle 0x309a
leaq 0x934f(%rip), %r15      # 0xc360
xorl %ebp, %ebp
movzbl 0x45(%rsp), %edx
xorl %ecx, %ecx
btl %ebp, %edx
setb %cl
cmpl (%r15), %ecx
je 0x3043
movl %ecx, (%r15)
movl $0x1, %edi
leaq 0x555b(%rip), %rsi      # 0x858f
movl %ebp, %edx
xorl %eax, %eax
callq 0x13ba
movl 0x94ad(%rip), %eax      # 0xc4f0
incq %rbp
movslq %eax, %rcx
addq $0x4, %r15
cmpq %rcx, %rbp
jl 0x3013
testl %eax, %eax
jle 0x309a
leaq 0x9323(%rip), %r15      # 0xc380
xorl %ebp, %ebp
movzbl 0x46(%rsp), %edx
xorl %ecx, %ecx
btl %ebp, %edx
setb %cl
cmpl (%r15), %ecx
je 0x308b
movl %ecx, (%r15)
movl $0x1, %edi
movq %r12, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x13ba
movl 0x9465(%rip), %eax      # 0xc4f0
incq %rbp
movslq %eax, %rcx
addq $0x4, %r15
cmpq %rcx, %rbp
jl 0x305f
xorl %r12d, %r12d
leaq 0x53(%rsp), %r15
xorl %ebp, %ebp
movzbl -0x2(%r15), %r8d
xorl %eax, %eax
leaq 0x92fe(%rip), %rcx      # 0xc3b0
cmpl %r8d, (%r12,%rcx)
je 0x30c8
leaq 0x92f1(%rip), %rax      # 0xc3b0
movl %r8d, (%r12,%rax)
movl $0x1, %eax
movzwl -0x1(%r15), %ecx
rolw $0x8, %cx
movzwl %cx, %r9d
leaq 0x9424(%rip), %rcx      # 0xc500
cmpl (%r12,%rcx), %r9d
je 0x30f2
leaq 0x9417(%rip), %rax      # 0xc500
movl %r9d, (%r12,%rax)
movl $0x1, %eax
leaq 0x92c7(%rip), %rcx      # 0xc3c0
movl (%r12,%rcx), %edx
movzbl 0x593(%rsp,%rbp), %ecx
cmpl %ecx, %edx
je 0x311b
leaq 0x92b0(%rip), %rax      # 0xc3c0
movl %ecx, (%r12,%rax)
movl $0x1, %eax
movl %ecx, %edx
movzbl 0x47(%rsp), %esi
xorl %ecx, %ecx
btl %ebp, %esi
setb %cl
cmpl (%r12,%r13), %ecx
jne 0x3137
testl %eax, %eax
jne 0x313b
jmp 0x31fa
movl %ecx, (%r12,%r13)
movl %edx, (%rsp)
movl $0x1, %edi
leaq 0x546f(%rip), %rsi      # 0x85b9
movl %ebp, %edx
xorl %eax, %eax
callq 0x13ba
leaq 0x9266(%rip), %rax      # 0xc3c0
movl (%r12,%rax), %eax
btl %ebp, %eax
jae 0x31fa
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13ba
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13ba
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13ba
movl $0x1, %edi
leaq 0x5453(%rip), %rsi      # 0x85f3
xorl %eax, %eax
callq 0x13ba
movl $0x1, %edi
leaq 0x5464(%rip), %rsi      # 0x8617
xorl %eax, %eax
callq 0x13ba
movl $0x1, %edi
leaq 0x548b(%rip), %rsi      # 0x8651
xorl %eax, %eax
callq 0x13ba
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13ba
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13ba
movl $0x1, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x13ba
incq %rbp
addq $0x6, %r15
addq $0x4, %r12
cmpq $0x4, %rbp
jne 0x30a4
movq 0x18(%rsp), %rbp
leaq 0x52fc(%rip), %r15      # 0x8517
leaq 0x40(%rsp), %r12
jmp 0x2f3a
leaq 0x527e(%rip), %rsi      # 0x84aa
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
jmp 0x3265
leaq 0x528b(%rip), %rsi      # 0x84cc
movl $0x1, %edi
jmp 0x3257
leaq 0x529d(%rip), %r15      # 0x84ec
movl $0x1, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x13ba
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