// File: /workspace/github_c_makefile_star2_O1/hiperiondev[P]hpsdrsim/src/hpsdr_newprotocol.c
// Function: rx_thread
void* rx_thread(void *data) {
    int sock;
    struct sockaddr_in addr;
    // One instance of this thread is started for each DDC
    unsigned long seqnum;
    unsigned char buffer[1444];
    int yes = 1;
    int i;
    long wait;
    double i0sample, q0sample;
    double i1sample, q1sample;
    double irsample, qrsample;
    double fac;
    int sample;
    unsigned char *p;
    int noisept, tonept;
    int myddc;
    int sync, size;
    int myadc, syncadc;
    int rxptr;
    int divptr;
    int decimation;
    unsigned int seed;

    struct timespec delay;

    syncadc = 0;

    myddc = (int) (uintptr_t) data;
    if (myddc < 0 || myddc >= NUMRECEIVERS)
        return NULL;

    dbg_printf(1, "-- Start rx_thread %d port: %d\n", myddc, ddc0_port + myddc);

    seqnum = 0;
    // unique seed value for random number generator
    seed = ((uintptr_t) &seed) & 0xffffff;

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

    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &yes, sizeof(yes));
    setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void*) &yes, sizeof(yes));

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

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

    tonept = noisept = 0;
    clock_gettime(CLOCK_MONOTONIC, &delay);
    dbg_printf(1, "RX thread %d, enabled=%d\n", myddc, ddcenable[myddc]);
    rxptr = txptr - 4096;
    if (rxptr < 0)
        rxptr += NEWRTXLEN;
    divptr = 0;

    while (run) {
        if (ddcenable[myddc] <= 0 || rxrate[myddc] == 0 || rxfreq[myddc] == 0) {
            usleep(5000);
            clock_gettime(CLOCK_MONOTONIC, &delay);
            rxptr = txptr - 4096;
            if (rxptr < 0)
                rxptr += NEWRTXLEN;
            continue;
        }
        decimation = 1536 / rxrate[myddc];
        myadc = adcmap[myddc];
        // for simplicity, we only allow for a single "synchronized" DDC,
        // this well covers the PURESIGNAL and DIVERSITY cases
        sync = 0;
        i = syncddc[myddc];
        while (i) {
            sync++;
            i = i >> 1;
        }
        // sync == 0 means no synchronizatsion
        // sync == 1,2,3  means synchronization with DDC0,1,2
        // Usually we send 238 samples per buffer, but with synchronization
        // we send 119 sample *pairs*.
        if (sync) {
            size = 119;
            wait = 119000000L / rxrate[myddc]; // time for these samples in nano-secs
            syncadc = adcmap[sync - 1];
        } else {
            size = 238;
            wait = 238000000L / rxrate[myddc]; // time for these samples in nano-secs
        }

        // ADC0 RX: noise + 800Hz signal at -73 dBm
        // ADC0 TX: noise + distorted TX signal
        // ADC1 RX: noise
        // ADC1 TX: HERMES only: original TX signal
        // ADC2   : original TX signal
        p = buffer;
        *p++ = (seqnum >> 24) & 0xFF;
        *p++ = (seqnum >> 16) & 0xFF;
        *p++ = (seqnum >> 8) & 0xFF;
        *p++ = (seqnum >> 0) & 0xFF;
        seqnum += 1;
        // do not use time stamps
        *p++ = 0;
        *p++ = 0;
        *p++ = 0;
        *p++ = 0;
        *p++ = 0;
        *p++ = 0;
        *p++ = 0;
        *p++ = 0;
        // 24 bits per sample *ALWAYS*
        *p++ = 0;
        *p++ = 24;
        *p++ = 0;
        *p++ = sync ? 2 * size : size;  // should be 238 in either case
        for (i = 0; i < size; i++) {
            // produce noise depending on the ADC
            i1sample = i0sample = noiseItab[noisept];
            q1sample = q0sample = noiseQtab[noisept++];
            if (noisept == LENNOISE)
                noisept = rand_r(&seed) / NOISEDIV;
            // PS: produce sample PAIRS,
            // a) distorted TX data (with Drive and Attenuation)
            // b) original TX data (normalized)
            //
            // DIV: produce sample PAIRS,
            // a) add man-made-noise on I-sample of RX channel
            // b) add man-made-noise on Q-sample of "synced" channel
            if (sync && (rxrate[myadc] == 192) && ptt && (syncadc == adc)) {
                irsample = isample[rxptr];
                qrsample = qsample[rxptr++];
                if (rxptr >= NEWRTXLEN)
                    rxptr = 0;
                fac = txatt_dbl * txdrv_dbl * (IM3a + IM3b * (irsample * irsample + qrsample * qrsample) * txdrv_dbl * txdrv_dbl);
                if (myadc == 0) {
                    i0sample += irsample * fac;
                    q0sample += qrsample * fac;
                }
                i1sample = irsample * 0.2899;
                q1sample = qrsample * 0.2899;
            } else if (myadc == 0) {
                i0sample += toneItab[tonept] * 0.0002239 * rxatt0_dbl;
                q0sample += toneQtab[tonept] * 0.0002239 * rxatt0_dbl;
                tonept += decimation;
                if (tonept >= LENTONE)
                    tonept = 0;
            }
            if (diversity && !sync && myadc == 0) {
                i0sample += 0.0001 * rxatt0_dbl * divtab[divptr];
                divptr += decimation;
                if (divptr >= LENDIV)
                    divptr = 0;
            }
            if (diversity && !sync && myadc == 1) {
                q0sample += 0.0002 * rxatt1_dbl * divtab[divptr];
                divptr += decimation;
                if (divptr >= LENDIV)
                    divptr = 0;
            }
            if (diversity && sync && !ptt) {
                if (myadc == 0)
                    i0sample += 0.0001 * rxatt0_dbl * divtab[divptr];
                if (syncadc == 1)
                    q1sample += 0.0002 * rxatt1_dbl * divtab[divptr];
                divptr += decimation;
                if (divptr >= LENDIV)
                    divptr = 0;
            }
            if (sync) {
                sample = i0sample * 8388607.0;
                *p++ = (sample >> 16) & 0xFF;
                *p++ = (sample >> 8) & 0xFF;
                *p++ = (sample >> 0) & 0xFF;
                sample = q0sample * 8388607.0;
                *p++ = (sample >> 16) & 0xFF;
                *p++ = (sample >> 8) & 0xFF;
                *p++ = (sample >> 0) & 0xFF;
                sample = i1sample * 8388607.0;
                *p++ = (sample >> 16) & 0xFF;
                *p++ = (sample >> 8) & 0xFF;
                *p++ = (sample >> 0) & 0xFF;
                sample = q1sample * 8388607.0;
                *p++ = (sample >> 16) & 0xFF;
                *p++ = (sample >> 8) & 0xFF;
                *p++ = (sample >> 0) & 0xFF;
            } else {
                sample = i0sample * 8388607.0;
                *p++ = (sample >> 16) & 0xFF;
                *p++ = (sample >> 8) & 0xFF;
                *p++ = (sample >> 0) & 0xFF;
                sample = q0sample * 8388607.0;
                *p++ = (sample >> 16) & 0xFF;
                *p++ = (sample >> 8) & 0xFF;
                *p++ = (sample >> 0) & 0xFF;
            }
        }
        delay.tv_nsec += wait;
        while (delay.tv_nsec >= 1000000000) {
            delay.tv_nsec -= 1000000000;
            delay.tv_sec++;
        }

        clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &delay, NULL);

        if (sendto(sock, buffer, 1444, 0, (struct sockaddr*) &addr_new, sizeof(addr_new)) < 0) {
            dbg_printf(1, "***** ERROR: RX thread sendto\n");
            break;
        }
    }
    close(sock);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x618, %rsp            # imm = 0x618
movq %rdi, %r15
movl $0x1, 0xc(%rsp)
cmpl $0x3, %r15d
ja 0x3cfd
movl 0x8cb1(%rip), %ecx      # 0xc308
addl %r15d, %ecx
leaq 0x5573(%rip), %rsi      # 0x8bd4
movl $0x1, %edi
movl %r15d, %edx
xorl %eax, %eax
callq 0x13aa
leaq 0x8(%rsp), %rax
movl %eax, %ecx
andl $0xfffffc, %ecx         # imm = 0xFFFFFC
movl %ecx, (%rax)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x3cce
movl %eax, %ebx
leaq 0xc(%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
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rsi
movapd %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movl 0x8c16(%rip), %eax      # 0xc308
addl %r15d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x3ce3
leaq 0x10(%rsp), %r13
movl $0x1, %edi
movq %r13, %rsi
callq 0x1090
movl %r15d, %ebp
andl $0x3, %ebp
movl %ebp, %eax
leaq 0x8c6e(%rip), %rcx      # 0xc3a0
movl (%rcx,%rax,4), %ecx
leaq 0x54f4(%rip), %rsi      # 0x8c30
movl $0x1, %edi
movl %r15d, %edx
xorl %eax, %eax
callq 0x13aa
movl 0x89ef(%rip), %eax      # 0xc140
leal -0x1000(%rax), %ecx
leal 0xeb40(%rax), %r12d
cmpl $0x1000, %eax           # imm = 0x1000
cmovgel %ecx, %r12d
leal (,%rbp,4), %eax
movq %rax, 0x40(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, (%rsp)
xorl %r15d, %r15d
cmpl $0x0, 0x8b47(%rip)      # 0xc2e0
je 0x3cf6
leaq 0x8bfa(%rip), %rax      # 0xc3a0
cmpl $0x0, (%rax,%rbp,4)
jle 0x37ca
leaq 0x8d4d(%rip), %rax      # 0xc500
movslq (%rax,%rbp,4), %rsi
testq %rsi, %rsi
je 0x37ca
leaq 0x8c0d(%rip), %rax      # 0xc3d0
cmpq $0x0, (%rax,%rbp,8)
jne 0x37ff
movl $0x1388, %edi           # imm = 0x1388
callq 0x11f0
movl $0x1, %edi
movq %r13, %rsi
callq 0x1090
movl 0x8959(%rip), %eax      # 0xc140
leal -0x1000(%rax), %ecx
leal 0xeb40(%rax), %r12d
cmpl $0x1000, %eax           # imm = 0x1000
cmovgel %ecx, %r12d
jmp 0x3792
movq %rbp, 0x38(%rsp)
xorl %ebp, %ebp
movl $0x600, %eax            # imm = 0x600
xorl %edx, %edx
idivl %esi
movl %eax, %r9d
movq 0x40(%rsp), %rdx
leaq 0x8ba2(%rip), %rax      # 0xc3c0
movl (%rdx,%rax), %eax
testl %eax, %eax
je 0x3834
xorl %ebp, %ebp
movl %eax, %ecx
incl %ebp
sarl %ecx
cmpl $0x1, %eax
movl %ecx, %eax
ja 0x3829
leaq 0x8b75(%rip), %rcx      # 0xc3b0
movslq (%rdx,%rcx), %r13
testl %ebp, %ebp
movl $0x0, %edi
leaq 0x17b4de3(%rip), %r8    # 0x17b8630
movsd 0x486b(%rip), %xmm6     # 0x80c0
movl %ebx, 0x4(%rsp)
je 0x3872
movslq %ebp, %rax
movl -0x4(%rcx,%rax,4), %eax
movl %eax, (%rsp)
movl $0x77, %r11d
movl $0x717cbc0, %eax        # imm = 0x717CBC0
jmp 0x387d
movl $0xee, %r11d
movl $0xe2f9780, %eax        # imm = 0xE2F9780
testl %ebp, %ebp
setne %cl
xorl %edx, %edx
idivq %rsi
movq %rax, 0x30(%rsp)
movq 0x20(%rsp), %rax
bswapl %eax
movl %eax, 0x70(%rsp)
leaq 0x74(%rsp), %rax
movb $0x0, 0x8(%rax)
movq $0x0, (%rax)
movl %r11d, %eax
shll %cl, %eax
movw $0x18, 0x7d(%rsp)
movb %al, 0x7f(%rsp)
leaq 0x80(%rsp), %rbx
movslq %r14d, %rax
leaq 0x8d67(%rip), %rcx      # 0xc630
movsd (%rcx,%rax,8), %xmm8
leal 0x1(%rax), %r14d
leaq 0xbc0d56(%rip), %rcx    # 0xbc4630
movsd (%rcx,%rax,8), %xmm7
cmpl $0x177000, %r14d        # imm = 0x177000
jne 0x395d
leaq 0x8(%rsp), %rdi
movl %r9d, %r14d
movq %rbx, 0x58(%rsp)
movq %r15, %rbx
movl %ebp, %r15d
movl %r11d, %ebp
movsd %xmm7, 0x50(%rsp)
movsd %xmm8, 0x48(%rsp)
callq 0x10b0
movsd 0x48(%rsp), %xmm8
movsd 0x50(%rsp), %xmm7
movl %ebp, %r11d
movl %r15d, %ebp
movq %rbx, %r15
movq 0x58(%rsp), %rbx
movl %r14d, %r9d
movsd 0x478a(%rip), %xmm6     # 0x80c0
leaq 0x17b4cf3(%rip), %r8    # 0x17b8630
xorl %edi, %edi
movslq %eax, %r14
imulq $-0x447c854f, %r14, %rax # imm = 0xBB837AB1
shrq $0x20, %rax
addl %eax, %r14d
movl %r14d, %eax
shrl $0x1f, %eax
sarl $0xb, %r14d
addl %eax, %r14d
testl %ebp, %ebp
je 0x3a45
leaq 0x8b94(%rip), %rax      # 0xc500
cmpl $0xc0, (%rax,%r13,4)
jne 0x3a45
cmpl $0x0, 0x8c47(%rip)      # 0xc5c8
je 0x3a45
movl (%rsp), %eax
cmpl 0x8b60(%rip), %eax      # 0xc4f0
jne 0x3a45
leal 0x1(%r12), %eax
cmpl $0xfb3f, %r12d          # imm = 0xFB3F
cmovgel %edi, %eax
movslq %r12d, %rcx
leaq 0x17d4081(%rip), %rdx   # 0x17d7a30
movsd (%rdx,%rcx,8), %xmm2
leaq 0x1852075(%rip), %rdx   # 0x1855a30
movsd (%rdx,%rcx,8), %xmm3
testl %r13d, %r13d
jne 0x3a1e
movsd 0x8c1b(%rip), %xmm0     # 0xc5e8
movsd 0x8753(%rip), %xmm1     # 0xc128
mulsd %xmm0, %xmm1
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
movapd %xmm2, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm4, %xmm5
mulsd 0x46a3(%rip), %xmm5     # 0x8098
mulsd %xmm0, %xmm5
mulsd %xmm0, %xmm5
addsd 0x469b(%rip), %xmm5     # 0x80a0
mulsd %xmm1, %xmm5
movapd %xmm2, %xmm0
mulsd %xmm5, %xmm0
addsd %xmm0, %xmm8
mulsd %xmm3, %xmm5
addsd %xmm5, %xmm7
movapd %xmm8, %xmm0
movapd %xmm7, %xmm1
movsd 0x4679(%rip), %xmm4     # 0x80a8
mulsd %xmm4, %xmm2
mulsd %xmm4, %xmm3
movapd %xmm2, %xmm8
movapd %xmm3, %xmm7
movl %eax, %r12d
jmp 0x3aaf
testl %r13d, %r13d
je 0x3a55
movapd %xmm7, %xmm1
movapd %xmm8, %xmm0
jmp 0x3aaf
movq 0x28(%rsp), %rcx
movslq %ecx, %rcx
leaq 0x1796bcc(%rip), %rax   # 0x179a630
movsd (%rax,%rcx,8), %xmm0
movsd 0x461f(%rip), %xmm3     # 0x8090
mulsd %xmm3, %xmm0
movsd 0x86bb(%rip), %xmm2     # 0xc138
mulsd %xmm2, %xmm0
addsd %xmm8, %xmm0
leaq 0x1778ba3(%rip), %rax   # 0x177c630
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm1
addsd %xmm7, %xmm1
addl %r9d, %ecx
cmpl $0x3c00, %ecx           # imm = 0x3C00
cmovgel %edi, %ecx
movq %rcx, 0x28(%rsp)
testl %ebp, %ebp
setne %dl
leaq 0x8b55(%rip), %rax      # 0xc610
movl (%rax), %eax
testl %eax, %eax
sete %cl
orb %dl, %cl
jne 0x3af6
testl %r13d, %r13d
jne 0x3af6
movsd 0x8665(%rip), %xmm2     # 0xc138
mulsd 0x45d5(%rip), %xmm2     # 0x80b0
movslq %r15d, %r15
mulsd (%r8,%r15,8), %xmm2
addsd %xmm2, %xmm0
addl %r9d, %r15d
cmpl $0x3e80, %r15d          # imm = 0x3E80
cmovgel %edi, %r15d
cmpl $0x1, %r13d
setne %dl
orb %dl, %cl
jne 0x3b2c
movsd 0x8627(%rip), %xmm2     # 0xc130
mulsd 0x45a7(%rip), %xmm2     # 0x80b8
movslq %r15d, %r15
mulsd (%r8,%r15,8), %xmm2
addsd %xmm2, %xmm1
addl %r9d, %r15d
cmpl $0x3e80, %r15d          # imm = 0x3E80
cmovgel %edi, %r15d
testl %ebp, %ebp
je 0x3b90
testl %eax, %eax
je 0x3b90
cmpl $0x0, 0x8a8d(%rip)      # 0xc5c8
jne 0x3b90
testl %r13d, %r13d
jne 0x3b5f
movsd 0x85ee(%rip), %xmm2     # 0xc138
mulsd 0x455e(%rip), %xmm2     # 0x80b0
movslq %r15d, %rax
mulsd (%r8,%rax,8), %xmm2
addsd %xmm2, %xmm0
cmpl $0x1, (%rsp)
jne 0x3b82
movsd 0x85c3(%rip), %xmm2     # 0xc130
mulsd 0x4543(%rip), %xmm2     # 0x80b8
movslq %r15d, %rax
mulsd (%r8,%rax,8), %xmm2
addsd %xmm2, %xmm7
addl %r9d, %r15d
cmpl $0x3e80, %r15d          # imm = 0x3E80
cmovgel %edi, %r15d
mulsd %xmm6, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, %edx
shrl $0x10, %edx
mulsd %xmm6, %xmm1
cvttsd2si %xmm1, %eax
movl %eax, %esi
shrl $0x10, %esi
movb %dl, (%rbx)
movb %ch, 0x1(%rbx)
movb %cl, 0x2(%rbx)
movb %sil, 0x3(%rbx)
movb %ah, 0x4(%rbx)
movb %al, 0x5(%rbx)
testl %ebp, %ebp
je 0x3bf4
mulsd %xmm6, %xmm8
cvttsd2si %xmm8, %eax
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x6(%rbx)
movb %ah, 0x7(%rbx)
movb %al, 0x8(%rbx)
mulsd %xmm6, %xmm7
cvttsd2si %xmm7, %eax
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x9(%rbx)
movb %ah, 0xa(%rbx)
movb %al, 0xb(%rbx)
addq $0xc, %rbx
jmp 0x3bf8
addq $0x6, %rbx
decl %r11d
jne 0x38bf
movq 0x30(%rsp), %rdi
addq 0x18(%rsp), %rdi
movq %rdi, 0x18(%rsp)
cmpq $0x3b9aca00, %rdi       # imm = 0x3B9ACA00
jl 0x3c75
movl $0x773593ff, %eax       # imm = 0x773593FF
cmpq %rax, %rdi
movl $0x773593ff, %ecx       # imm = 0x773593FF
cmovlq %rdi, %rcx
movq 0x10(%rsp), %rsi
movq %rdi, %rax
subq %rcx, %rax
addq $0x3b9ac9ff, %rax       # imm = 0x3B9AC9FF
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rcx # imm = 0x44B82FA09B5A53
mulq %rcx
shrq $0xb, %rdx
imulq $-0x3b9aca00, %rdx, %rax # imm = 0xC4653600
movq $-0x3b9aca00, %rcx      # imm = 0xC4653600
addq %rcx, %rdi
addq %rax, %rdi
leaq (%rsi,%rdx), %rax
incq %rax
movq %rdi, 0x18(%rsp)
movq %rax, 0x10(%rsp)
incq 0x20(%rsp)
movl $0x1, %edi
movl $0x1, %esi
leaq 0x10(%rsp), %r13
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x11b0
movl $0x5a4, %edx            # imm = 0x5A4
movl 0x4(%rsp), %ebx
movl %ebx, %edi
leaq 0x70(%rsp), %rsi
xorl %ecx, %ecx
leaq 0x18cfdcc(%rip), %r8    # 0x18d3a78
movl $0x10, %r9d
callq 0x10e0
testq %rax, %rax
movq 0x38(%rsp), %rbp
jns 0x3792
leaq 0x4f7e(%rip), %rsi      # 0x8c4a
jmp 0x3cea
leaq 0x4f1f(%rip), %rsi      # 0x8bf4
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
jmp 0x3cfd
leaq 0x4f29(%rip), %rsi      # 0x8c13
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
movl %ebx, %edi
callq 0x10f0
xorl %eax, %eax
addq $0x618, %rsp            # imm = 0x618
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq