// File: /workspace/github_c_makefile_star2_O2/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 $0x638, %rsp            # imm = 0x638
movq %rdi, %r15
movl $0x1, 0x1c(%rsp)
cmpl $0x3, %r15d
ja 0x3b9c
movl 0x9df8(%rip), %ecx      # 0xd308
addl %r15d, %ecx
leaq 0x56c2(%rip), %rsi      # 0x8bdc
pushq $0x1
popq %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x131a
leaq 0x18(%rsp), %rax
movl %eax, %ecx
andl $0xfffffc, %ecx         # imm = 0xFFFFFC
movl %ecx, (%rax)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x3b71
movl %eax, %ebx
pushq $0x1
popq %rbp
pushq $0x2
popq %rdx
leaq 0x1c(%rsp), %r14
pushq $0x4
popq %r12
movl %eax, %edi
movl %ebp, %esi
movq %r14, %rcx
movl %r12d, %r8d
callq 0x1070
pushq $0xf
popq %rdx
movl %ebx, %edi
movl %ebp, %esi
movq %r14, %rcx
movl %r12d, %r8d
callq 0x1070
leaq 0x80(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
movl 0x9d70(%rip), %eax      # 0xd308
addl %r15d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x1150
testl %eax, %eax
js 0x3b84
pushq $0x1
popq %rbp
leaq 0x20(%rsp), %r13
movl %ebp, %edi
movq %r13, %rsi
callq 0x1090
movl %r15d, %r8d
andl $0x3, %r8d
movl %r8d, %eax
leaq 0x9dc8(%rip), %rcx      # 0xd3a0
movl (%rcx,%rax,4), %ecx
leaq 0x5656(%rip), %rsi      # 0x8c38
movl %ebp, %edi
movl %r15d, %edx
movq %r8, %r15
xorl %eax, %eax
callq 0x131a
movl 0x9b49(%rip), %eax      # 0xd140
leal -0x1000(%rax), %ecx
leal 0xeb40(%rax), %r12d
cmpl $0x1000, %eax           # imm = 0x1000
cmovgel %ecx, %r12d
leal (,%r15,4), %eax
movq %rax, 0x60(%rsp)
movsd 0x4aa6(%rip), %xmm6     # 0x80c8
movl $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r14d, %r14d
cmpl $0x0, 0x9c92(%rip)      # 0xd2e0
je 0x3b95
leaq 0x9d45(%rip), %rax      # 0xd3a0
cmpl $0x0, (%rax,%r15,4)
jle 0x367f
leaq 0x9e97(%rip), %rax      # 0xd500
movl (%rax,%r15,4), %esi
testl %esi, %esi
je 0x367f
leaq 0x9d58(%rip), %rax      # 0xd3d0
cmpq $0x0, (%rax,%r15,8)
jne 0x36b9
movl $0x1388, %edi           # imm = 0x1388
callq 0x11f0
movl %ebp, %edi
movq %r13, %rsi
callq 0x1090
movsd 0x4a2d(%rip), %xmm6     # 0x80c8
movl 0x9a9f(%rip), %eax      # 0xd140
leal -0x1000(%rax), %ecx
leal 0xeb40(%rax), %r12d
cmpl $0x1000, %eax           # imm = 0x1000
cmovgel %ecx, %r12d
jmp 0x3647
movl %ebx, 0x14(%rsp)
movq 0x60(%rsp), %rax
leaq 0x9ce7(%rip), %rcx      # 0xd3b0
movslq (%rax,%rcx), %rbx
leaq 0x9cec(%rip), %rcx      # 0xd3c0
movl (%rax,%rcx), %eax
xorl %r13d, %r13d
movl 0x40(%rsp), %r8d
testl %eax, %eax
je 0x36ea
sarl %eax
incq %r13
jmp 0x36df
movq %r15, 0x58(%rsp)
movl $0x600, %eax            # imm = 0x600
xorl %edx, %edx
idivl %esi
movl %eax, 0x8(%rsp)
testl %r13d, %r13d
je 0x371c
leaq 0x9ca8(%rip), %rax      # 0xd3b0
movl -0x4(%rax,%r13,4), %eax
movl %eax, 0xc(%rsp)
movl $0x717cbc0, %eax        # imm = 0x717CBC0
pushq $0x77
popq %r15
jmp 0x3727
movl $0xee, %r15d
movl $0xe2f9780, %eax        # imm = 0xE2F9780
testl %r13d, %r13d
setne %cl
xorl %edx, %edx
idivl %esi
movl %eax, 0x10(%rsp)
movq 0x30(%rsp), %rax
bswapl %eax
movl %eax, 0x90(%rsp)
leaq 0x94(%rsp), %rax
movb $0x0, 0x8(%rax)
andq $0x0, (%rax)
movl %r15d, %eax
shll %cl, %eax
movw $0x18, 0x9d(%rsp)
movb %al, 0x9f(%rsp)
leaq 0xa0(%rsp), %rbp
movq %rbx, 0x68(%rsp)
subl $0x1, %r15d
jb 0x3ad9
movslq %r8d, %rax
leaq 0x9ea6(%rip), %rcx      # 0xd630
movsd (%rcx,%rax,8), %xmm3
leal 0x1(%rax), %r8d
leaq 0xbc1e96(%rip), %rcx    # 0xbc5630
movsd (%rcx,%rax,8), %xmm7
cmpl $0x177000, %r8d         # imm = 0x177000
jne 0x37dd
leaq 0x18(%rsp), %rdi
movapd %xmm7, 0x40(%rsp)
movapd %xmm3, 0x70(%rsp)
callq 0x10b0
movapd 0x70(%rsp), %xmm3
movapd 0x40(%rsp), %xmm7
movsd 0x48f6(%rip), %xmm6     # 0x80c8
cltd
movl $0xaec, %ecx            # imm = 0xAEC
idivl %ecx
movl %eax, %r8d
testl %r13d, %r13d
je 0x38c7
leaq 0x9d13(%rip), %rax      # 0xd500
cmpl $0xc0, (%rax,%rbx,4)
jne 0x38c7
cmpl $0x0, 0x9dc7(%rip)      # 0xd5c8
je 0x38c7
movl 0xc(%rsp), %eax
cmpl 0x9cdf(%rip), %eax      # 0xd4f0
jne 0x38c7
leal 0x1(%r12), %edx
cmpl $0xfb3f, %r12d          # imm = 0xFB3F
movl $0x0, %eax
cmovgel %eax, %edx
movslq %r12d, %rax
leaq 0x17d51fb(%rip), %rcx   # 0x17d8a30
movsd (%rcx,%rax,8), %xmm1
leaq 0x18531ef(%rip), %rcx   # 0x1856a30
movsd (%rcx,%rax,8), %xmm2
unpcklpd %xmm3, %xmm7            # xmm7 = xmm7[0],xmm3[0]
testl %ebx, %ebx
jne 0x38a6
movsd 0x9d92(%rip), %xmm0     # 0xd5e8
movsd 0x98ca(%rip), %xmm3     # 0xd128
mulsd %xmm0, %xmm3
movapd %xmm2, %xmm4
mulsd %xmm2, %xmm4
movapd %xmm1, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm5
mulsd 0x4822(%rip), %xmm5     # 0x80a0
mulsd %xmm0, %xmm5
mulsd %xmm0, %xmm5
addsd 0x481a(%rip), %xmm5     # 0x80a8
mulsd %xmm3, %xmm5
movapd %xmm2, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
unpcklpd %xmm5, %xmm5            # xmm5 = xmm5[0,0]
mulpd %xmm0, %xmm5
addpd %xmm5, %xmm7
movapd %xmm7, %xmm0
movsd 0x47fe(%rip), %xmm3     # 0x80b0
mulsd %xmm3, %xmm1
mulsd %xmm3, %xmm2
movapd %xmm1, %xmm3
movapd %xmm2, %xmm7
movl %edx, %r12d
jmp 0x3926
movapd %xmm7, %xmm0
unpcklpd %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0]
testl %ebx, %ebx
jne 0x3926
movq 0x38(%rsp), %rcx
movslq %ecx, %rcx
leaq 0x1779d4e(%rip), %rax   # 0x177d630
movsd (%rax,%rcx,8), %xmm1
leaq 0x1797d42(%rip), %rax   # 0x179b630
movhpd (%rax,%rcx,8), %xmm1    # xmm1 = xmm1[0],mem[0]
mulpd 0x4755(%rip), %xmm1     # 0x8050
movsd 0x9835(%rip), %xmm2     # 0xd138
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm1, %xmm2
addpd %xmm2, %xmm0
addl 0x8(%rsp), %ecx
cmpl $0x3c00, %ecx           # imm = 0x3C00
movl $0x0, %eax
cmovgel %eax, %ecx
movq %rcx, 0x38(%rsp)
testl %r13d, %r13d
setne %dl
leaq 0x9cdd(%rip), %rax      # 0xd610
movl (%rax), %ecx
testl %ecx, %ecx
sete %al
orb %dl, %al
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
movslq %r14d, %rdx
jne 0x396f
testl %ebx, %ebx
jne 0x396f
movsd 0x97e3(%rip), %xmm2     # 0xd138
mulsd 0x475b(%rip), %xmm2     # 0x80b8
leaq 0x17b5ccc(%rip), %rax   # 0x17b9630
mulsd (%rax,%rdx,8), %xmm2
addsd %xmm2, %xmm1
jmp 0x399b
cmpl $0x1, %ebx
setne %sil
orb %sil, %al
jne 0x39b0
movsd 0x97ad(%rip), %xmm2     # 0xd130
mulsd 0x4735(%rip), %xmm2     # 0x80c0
leaq 0x17b5c9e(%rip), %rax   # 0x17b9630
mulsd (%rax,%rdx,8), %xmm2
addsd %xmm2, %xmm0
addl 0x8(%rsp), %edx
cmpl $0x3e80, %edx           # imm = 0x3E80
movl $0x0, %eax
cmovgel %eax, %edx
movl %edx, %r14d
testl %r13d, %r13d
je 0x3a27
testl %ecx, %ecx
je 0x3a27
cmpl $0x0, 0x9c08(%rip)      # 0xd5c8
jne 0x3a27
movslq %r14d, %rax
testl %ebx, %ebx
jne 0x39e9
movsd 0x9767(%rip), %xmm2     # 0xd138
mulsd 0x46df(%rip), %xmm2     # 0x80b8
leaq 0x17b5c50(%rip), %rcx   # 0x17b9630
mulsd (%rcx,%rax,8), %xmm2
addsd %xmm2, %xmm1
cmpl $0x1, 0xc(%rsp)
jne 0x3a10
movsd 0x9738(%rip), %xmm2     # 0xd130
mulsd 0x46c0(%rip), %xmm2     # 0x80c0
leaq 0x17b5c29(%rip), %rcx   # 0x17b9630
mulsd (%rcx,%rax,8), %xmm2
addsd %xmm2, %xmm7
addl 0x8(%rsp), %r14d
cmpl $0x3e80, %r14d          # imm = 0x3E80
movl $0x0, %eax
cmovgel %eax, %r14d
jmp 0x3a30
testl %r13d, %r13d
je 0x3abe
mulsd %xmm6, %xmm1
cvttsd2si %xmm1, %eax
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, (%rbp)
movb %ah, 0x1(%rbp)
movb %al, 0x2(%rbp)
mulsd %xmm6, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x3(%rbp)
movb %ah, 0x4(%rbp)
leaq 0x6(%rbp), %r9
movb %al, 0x5(%rbp)
pushq $0x7
popq %rax
pushq $0x8
popq %r10
pushq $0x9
popq %rcx
movapd %xmm7, %xmm0
pushq $0xa
popq %rdi
pushq $0xb
popq %rdx
pushq $0xc
popq %rsi
mulsd %xmm6, %xmm3
cvttsd2si %xmm3, %ebx
movl %ebx, %r11d
shrl $0x10, %r11d
movb %r11b, (%r9)
movb %bh, (%rbp,%rax)
movb %bl, (%rbp,%r10)
mulsd %xmm6, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, %r9d
shrl $0x10, %r9d
movb %r9b, (%rbp,%rcx)
movb %ah, (%rbp,%rdi)
addq %rbp, %rsi
movb %al, (%rbp,%rdx)
movq %rsi, %rbp
movq 0x68(%rsp), %rbx
jmp 0x3776
movapd %xmm1, %xmm3
pushq $0x1
popq %rax
movq %rbp, %r9
pushq $0x2
popq %r10
pushq $0x3
popq %rcx
pushq $0x4
popq %rdi
pushq $0x5
popq %rdx
pushq $0x6
jmp 0x3a76
movl %r8d, 0x40(%rsp)
movslq 0x10(%rsp), %rax
addq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %rcx
incq %rcx
movl 0x14(%rsp), %ebx
pushq $0x1
popq %rbp
leaq 0x20(%rsp), %r13
movq 0x58(%rsp), %r15
cmpq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
jl 0x3b23
addq $-0x3b9aca00, %rax      # imm = 0xC4653600
movq %rax, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
incq %rcx
jmp 0x3b06
incq 0x30(%rsp)
movl %ebp, %edi
movl %ebp, %esi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x11b0
movl $0x5a4, %edx            # imm = 0x5A4
movl %ebx, %edi
leaq 0x90(%rsp), %rsi
xorl %ecx, %ecx
leaq 0x18d0f2a(%rip), %r8    # 0x18d4a78
pushq $0x10
popq %r9
callq 0x10e0
movsd 0x4569(%rip), %xmm6     # 0x80c8
testq %rax, %rax
jns 0x3647
leaq 0x50e3(%rip), %rsi      # 0x8c52
jmp 0x3b8b
leaq 0x5084(%rip), %rsi      # 0x8bfc
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
jmp 0x3b9c
leaq 0x5090(%rip), %rsi      # 0x8c1b
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
movl %ebx, %edi
callq 0x10f0
xorl %eax, %eax
addq $0x638, %rsp            # imm = 0x638
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq