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

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
        dbg_printf(1, "***** ERROR: TX: 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(duc0_port);

    if (bind(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
        dbg_printf(1, "***** ERROR: TX: 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: TX thread: recvmsg\n");
            break;
        }
        if (rc < 0)
            continue;
        if (rc != 1444) {
            dbg_printf(1, "Received TX 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, "TXthread: SEQ ERROR, old=%lu new=%lu\n", seqold, seqnum);
        }
        p = buffer + 4;
        sum = 0.0;
        for (i = 0; i < 240; i++) {
            // process 240 TX iq samples
            sample = (int) ((signed char) (*p++)) << 16;
            sample |= (int) ((((unsigned char) (*p++)) << 8) & 0xFF00);
            sample |= (int) ((unsigned char) (*p++) & 0xFF);
            di = (double) sample / 8388608.0;
            sample = (int) ((signed char) (*p++)) << 16;
            sample |= (int) ((((unsigned char) (*p++)) << 8) & 0xFF00);
            sample |= (int) ((unsigned char) (*p++) & 0xFF);
            dq = (double) sample / 8388608.0;

            // I don't know why (perhaps the CFFIR in the SDR program)
            // but somehow I must multiply the samples to get the correct
            // strength
            di *= 1.118;
            dq *= 1.118;

            // put TX samples into ring buffer
            isample[txptr] = di;
            qsample[txptr++] = dq;
            if (txptr >= NEWRTXLEN)
                txptr = 0;

            // accumulate TX power
            sum += (di * di + dq * dq);
        }
        txlevel = sum * txdrv_dbl * txdrv_dbl * 0.0041667;
    }
    close(sock);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5d8, %rsp            # imm = 0x5D8
movl 0x85b9(%rip), %edx      # 0xc304
leaq 0x4f17(%rip), %rsi      # 0x8c69
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
movl $0x10, 0x4(%rsp)
movl $0x1, (%rsp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x3fbe
movl %eax, %ebx
movq %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 0x20(%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
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movapd %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movzwl 0x84fa(%rip), %eax      # 0xc304
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x3fd3
leaq 0x10(%rsp), %r12
leaq 0x4(%rsp), %r13
leaq 0x17d3bf9(%rip), %rbp   # 0x17d7a30
leaq 0x1851bf2(%rip), %r14   # 0x1855a30
xorl %r15d, %r15d
cmpl $0x0, 0x8498(%rip)      # 0xc2e0
je 0x3fef
movl $0x5a4, %edx            # imm = 0x5A4
movl %ebx, %edi
leaq 0x30(%rsp), %rsi
xorl %ecx, %ecx
movq %r12, %r8
movq %r13, %r9
callq 0x1140
testl %eax, %eax
jns 0x3e7a
callq 0x1030
cmpl $0xb, (%rax)
je 0x3e41
jmp 0x3fdc
cmpl $0x5a4, %eax            # imm = 0x5A4
jne 0x400a
movl 0x30(%rsp), %eax
shll $0x18, %eax
movzbl 0x31(%rsp), %ecx
shll $0x10, %ecx
cltq
orq %rcx, %rax
movzbl 0x32(%rsp), %edx
shll $0x8, %edx
orq %rax, %rdx
movzbl 0x33(%rsp), %ecx
orq %rdx, %rcx
testl %ecx, %ecx
sete %al
leaq 0x1(%r15), %rsi
cmpq %rcx, %rsi
sete %sil
orb %al, %sil
jne 0x3ee1
movl $0x1, %edi
leaq 0x4e34(%rip), %rsi      # 0x8d01
movq %r15, %rdx
xorl %eax, %eax
movq %rcx, 0x8(%rsp)
callq 0x13aa
movq 0x8(%rsp), %rcx
xorpd %xmm0, %xmm0
movl $0x4, %eax
movl 0x824f(%rip), %r8d      # 0xc140
xorl %edi, %edi
movsd 0x41cd(%rip), %xmm3     # 0x80c8
movsd 0x41cd(%rip), %xmm4     # 0x80d0
movsbl 0x30(%rsp,%rax), %edx
shll $0x10, %edx
movzbl 0x31(%rsp,%rax), %esi
shll $0x8, %esi
orl %edx, %esi
movzbl 0x32(%rsp,%rax), %edx
orl %esi, %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
mulsd %xmm3, %xmm1
movsbl 0x33(%rsp,%rax), %edx
shll $0x10, %edx
movzbl 0x34(%rsp,%rax), %esi
shll $0x8, %esi
orl %edx, %esi
movzbl 0x35(%rsp,%rax), %edx
orl %esi, %edx
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
mulsd %xmm3, %xmm2
mulsd %xmm4, %xmm1
mulsd %xmm4, %xmm2
movslq %r8d, %rdx
movsd %xmm1, (%rbp,%rdx,8)
leal 0x1(%rdx), %r8d
movsd %xmm2, (%r14,%rdx,8)
cmpl $0xfb3f, %edx           # imm = 0xFB3F
cmovgel %edi, %r8d
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
addsd %xmm1, %xmm0
addq $0x6, %rax
cmpl $0x5a4, %eax            # imm = 0x5A4
jne 0x3f03
movl %r8d, 0x81aa(%rip)      # 0xc140
movsd 0x864a(%rip), %xmm1     # 0xc5e8
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
mulsd 0x412a(%rip), %xmm0     # 0x80d8
movsd %xmm0, 0x8652(%rip)     # 0xc608
movq %rcx, %r15
jmp 0x3e41
leaq 0x4cc1(%rip), %rsi      # 0x8c86
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
jmp 0x3ff6
leaq 0x4cc5(%rip), %rsi      # 0x8c9f
jmp 0x3fe3
leaq 0x4cd3(%rip), %rsi      # 0x8cb6
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
movl %ebx, %edi
callq 0x10f0
xorl %eax, %eax
addq $0x5d8, %rsp            # imm = 0x5D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4cc6(%rip), %rsi      # 0x8cd7
jmp 0x3fe3