// File: /workspace/github_c_makefile_star2_O3/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 0x8436(%rip), %edx      # 0xc304
leaq 0x4d94(%rip), %rsi      # 0x8c69
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
movl $0x10, 0x4(%rsp)
movl $0x1, (%rsp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x4136
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
xorl %r14d, %r14d
leaq 0x20(%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 0x10(%rsp), %rsi
movq %r14, 0x8(%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movzwl 0x837c(%rip), %eax      # 0xc304
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x414b
leaq 0x10(%rsp), %r12
leaq 0x4(%rsp), %r13
leaq 0x1851a7b(%rip), %r15   # 0x1855a30
xorl %ebp, %ebp
cmpl $0x0, 0x8322(%rip)      # 0xc2e0
je 0x4167
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 0x3ff0
callq 0x1030
cmpl $0xb, (%rax)
je 0x3fb7
jmp 0x4154
cmpl $0x5a4, %eax            # imm = 0x5A4
jne 0x4182
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(%rbp), %rsi
cmpq %rcx, %rsi
sete %sil
orb %al, %sil
jne 0x4057
movl $0x1, %edi
leaq 0x4cbe(%rip), %rsi      # 0x8d01
movq %rbp, %rdx
xorl %eax, %eax
movq %rcx, 0x8(%rsp)
callq 0x13ba
movq 0x8(%rsp), %rcx
xorpd %xmm0, %xmm0
movl $0x4, %eax
movl 0x80da(%rip), %edi      # 0xc140
movsd 0x405a(%rip), %xmm3     # 0x80c8
movsd 0x405a(%rip), %xmm4     # 0x80d0
leaq 0x17d39b3(%rip), %r8    # 0x17d7a30
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 %edi, %rdx
movsd %xmm1, (%r8,%rdx,8)
leal 0x1(%rdx), %edi
movsd %xmm2, (%r15,%rdx,8)
cmpl $0xfb3f, %edx           # imm = 0xFB3F
cmovgel %r14d, %edi
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
addsd %xmm1, %xmm0
addq $0x6, %rax
cmpl $0x5a4, %eax            # imm = 0x5A4
jne 0x407d
movl %edi, 0x8032(%rip)      # 0xc140
movsd 0x84d2(%rip), %xmm1     # 0xc5e8
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
mulsd 0x3fb2(%rip), %xmm0     # 0x80d8
movsd %xmm0, 0x84da(%rip)     # 0xc608
movq %rcx, %rbp
jmp 0x3fb7
leaq 0x4b49(%rip), %rsi      # 0x8c86
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
jmp 0x416e
leaq 0x4b4d(%rip), %rsi      # 0x8c9f
jmp 0x415b
leaq 0x4b5b(%rip), %rsi      # 0x8cb6
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
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 0x4b4e(%rip), %rsi      # 0x8cd7
jmp 0x415b