// File: /workspace/github_c_makefile_star2_O2/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 0x971d(%rip), %edx      # 0xd304
leaq 0x5083(%rip), %rsi      # 0x8c71
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
movl $0x10, 0x4(%rsp)
movl $0x1, (%rsp)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x3e37
movl %eax, %ebx
pushq $0x1
popq %rbp
pushq $0x2
popq %rdx
movq %rsp, %r14
pushq $0x4
popq %r15
movl %eax, %edi
movl %ebp, %esi
movq %r14, %rcx
movl %r15d, %r8d
callq 0x1070
pushq $0xf
popq %rdx
movl %ebx, %edi
movl %ebp, %esi
movq %r14, %rcx
movl %r15d, %r8d
callq 0x1070
leaq 0x20(%rsp), %rcx
andq $0x0, (%rcx)
movq $0x2710, 0x8(%rcx)      # imm = 0x2710
pushq $0x14
popq %rdx
pushq $0x10
popq %r14
movl %ebx, %edi
movl %ebp, %esi
movl %r14d, %r8d
callq 0x1070
leaq 0x10(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
movzwl 0x967b(%rip), %eax      # 0xd304
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl %r14d, %edx
callq 0x1150
testl %eax, %eax
js 0x3e4a
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0x4(%rsp), %r13
leaq 0x1852d77(%rip), %r14   # 0x1856a30
xorl %ebp, %ebp
cmpl $0x0, 0x961e(%rip)      # 0xd2e0
je 0x3e64
movl $0x5a4, %edx            # imm = 0x5A4
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
movq %r12, %r8
movq %r13, %r9
callq 0x1140
testl %eax, %eax
jns 0x3cf2
callq 0x1030
cmpl $0xb, (%rax)
je 0x3cbb
jmp 0x3e53
cmpl $0x5a4, %eax            # imm = 0x5A4
jne 0x3e7f
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 0x3d57
pushq $0x1
popq %rdi
leaq 0x4fc6(%rip), %rsi      # 0x8d09
movq %rbp, %rdx
xorl %eax, %eax
movq %rcx, 0x8(%rsp)
callq 0x131a
movq 0x8(%rsp), %rcx
xorpd %xmm0, %xmm0
movl 0x93df(%rip), %edi      # 0xd140
pushq $0x4
popq %rax
leaq 0x17d4cc5(%rip), %r8    # 0x17d8a30
cmpl $0x5a4, %eax            # imm = 0x5A4
je 0x3e09
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
cvtsi2sd %edx, %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
cvtsi2sd %edx, %xmm2
movsd 0x4318(%rip), %xmm3     # 0x80d0
mulsd %xmm3, %xmm1
mulsd %xmm3, %xmm2
movsd 0x4310(%rip), %xmm3     # 0x80d8
mulsd %xmm3, %xmm1
mulsd %xmm3, %xmm2
movslq %edi, %rdx
movsd %xmm1, (%r8,%rdx,8)
leal 0x1(%rdx), %edi
movsd %xmm2, (%r14,%rdx,8)
cmpl $0xfb3f, %edx           # imm = 0xFB3F
movl $0x0, %edx
cmovgel %edx, %edi
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
addsd %xmm1, %xmm0
addq $0x6, %rax
jmp 0x3d6b
movl %edi, 0x9331(%rip)      # 0xd140
movsd 0x97d1(%rip), %xmm1     # 0xd5e8
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
mulsd 0x42b9(%rip), %xmm0     # 0x80e0
movsd %xmm0, 0x97d9(%rip)     # 0xd608
movq %rcx, %rbp
jmp 0x3cbb
leaq 0x4e50(%rip), %rsi      # 0x8c8e
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
jmp 0x3e6b
leaq 0x4e56(%rip), %rsi      # 0x8ca7
jmp 0x3e5a
leaq 0x4e64(%rip), %rsi      # 0x8cbe
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
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 0x4e59(%rip), %rsi      # 0x8cdf
jmp 0x3e5a