// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x630, %rsp            # imm = 0x630
movq %rdi, -0x10(%rbp)
movl 0xa41f(%rip), %edx      # 0x10314
movl $0x1, %edi
leaq 0x6d60(%rip), %rsi      # 0xcc61
movb $0x0, %al
callq 0x1370
movl $0x10, -0x28(%rbp)
movl $0x1, -0x5e4(%rbp)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1230
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x5f50
movl $0x1, %edi
leaq 0x6d42(%rip), %rsi      # 0xcc7e
movb $0x0, %al
callq 0x1370
movq $0x0, -0x8(%rbp)
jmp 0x63a3
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x5e4(%rbp), %rcx
movl $0x4, %r8d
callq 0x1060
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0xf, %edx
leaq -0x5e4(%rbp), %rcx
movl $0x4, %r8d
callq 0x1060
movq $0x0, -0x628(%rbp)
movq $0x2710, -0x620(%rbp) # imm = 0x2710
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0x14, %edx
leaq -0x628(%rbp), %rcx
movl $0x10, %r8d
callq 0x1060
leaq -0x24(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10f0
movw $0x2, -0x24(%rbp)
xorl %edi, %edi
callq 0x10e0
movl %eax, -0x20(%rbp)
movl 0xa32b(%rip), %eax      # 0x10314
movzwl %ax, %edi
callq 0x10b0
movw %ax, -0x22(%rbp)
movl -0x14(%rbp), %edi
leaq -0x24(%rbp), %rsi
movl $0x10, %edx
callq 0x1180
cmpl $0x0, %eax
jge 0x6033
movl $0x1, %edi
leaq 0x6c80(%rip), %rsi      # 0xcc97
movb $0x0, %al
callq 0x1370
movl -0x14(%rbp), %edi
callq 0x1110
movq $0x0, -0x8(%rbp)
jmp 0x63a3
movq $0x0, -0x30(%rbp)
cmpl $0x0, 0xa2ae(%rip)      # 0x102f0
je 0x6393
movl -0x14(%rbp), %edi
leaq -0x5e0(%rbp), %rsi
movl $0x5a4, %edx            # imm = 0x5A4
xorl %ecx, %ecx
leaq -0x24(%rbp), %r8
leaq -0x28(%rbp), %r9
callq 0x1170
movl %eax, -0x5e8(%rbp)
cmpl $0x0, -0x5e8(%rbp)
jge 0x6097
callq 0x1030
cmpl $0xb, (%rax)
je 0x6097
movl $0x1, %edi
leaq 0x6c23(%rip), %rsi      # 0xccae
movb $0x0, %al
callq 0x1370
jmp 0x6393
cmpl $0x0, -0x5e8(%rbp)
jge 0x60a2
jmp 0x603b
cmpl $0x5a4, -0x5e8(%rbp)    # imm = 0x5A4
je 0x60c6
movl $0x1, %edi
leaq 0x6c15(%rip), %rsi      # 0xcccf
movb $0x0, %al
callq 0x1370
jmp 0x6393
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movzbl -0x5e0(%rbp), %eax
shll $0x18, %eax
movzbl -0x5df(%rbp), %ecx
shll $0x10, %ecx
addl %ecx, %eax
movzbl -0x5de(%rbp), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movzbl -0x5dd(%rbp), %ecx
addl %ecx, %eax
cltq
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x6132
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
je 0x6132
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl $0x1, %edi
leaq 0x6bce(%rip), %rsi      # 0xccf9
movb $0x0, %al
callq 0x1370
leaq -0x5e0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x5f8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x618(%rbp)
movl $0x0, -0x5ec(%rbp)
cmpl $0xf0, -0x5ec(%rbp)
jge 0x6362
movq -0x5f8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x5f8(%rbp)
movsbl (%rax), %eax
shll $0x10, %eax
movl %eax, -0x5fc(%rbp)
movq -0x5f8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x5f8(%rbp)
movzbl (%rax), %eax
shll $0x8, %eax
andl $0xff00, %eax           # imm = 0xFF00
orl -0x5fc(%rbp), %eax
movl %eax, -0x5fc(%rbp)
movq -0x5f8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x5f8(%rbp)
movzbl (%rax), %eax
andl $0xff, %eax
orl -0x5fc(%rbp), %eax
movl %eax, -0x5fc(%rbp)
cvtsi2sdl -0x5fc(%rbp), %xmm0
movsd 0x5ee9(%rip), %xmm1     # 0xc0d8
divsd %xmm1, %xmm0
movsd %xmm0, -0x608(%rbp)
movq -0x5f8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x5f8(%rbp)
movsbl (%rax), %eax
shll $0x10, %eax
movl %eax, -0x5fc(%rbp)
movq -0x5f8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x5f8(%rbp)
movzbl (%rax), %eax
shll $0x8, %eax
andl $0xff00, %eax           # imm = 0xFF00
orl -0x5fc(%rbp), %eax
movl %eax, -0x5fc(%rbp)
movq -0x5f8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x5f8(%rbp)
movzbl (%rax), %eax
andl $0xff, %eax
orl -0x5fc(%rbp), %eax
movl %eax, -0x5fc(%rbp)
cvtsi2sdl -0x5fc(%rbp), %xmm0
movsd 0x5e57(%rip), %xmm1     # 0xc0d8
divsd %xmm1, %xmm0
movsd %xmm0, -0x610(%rbp)
movsd 0x5e3b(%rip), %xmm0     # 0xc0d0
mulsd -0x608(%rbp), %xmm0
movsd %xmm0, -0x608(%rbp)
movsd 0x5e23(%rip), %xmm0     # 0xc0d0
mulsd -0x610(%rbp), %xmm0
movsd %xmm0, -0x610(%rbp)
movsd -0x608(%rbp), %xmm0
movslq 0x9e84(%rip), %rcx      # 0x10150
leaq 0x17d577d(%rip), %rax   # 0x17dba50
movsd %xmm0, (%rax,%rcx,8)
movsd -0x610(%rbp), %xmm0
movl 0x9e6a(%rip), %eax      # 0x10150
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x9e5f(%rip)      # 0x10150
movslq %eax, %rcx
leaq 0x1853755(%rip), %rax   # 0x1859a50
movsd %xmm0, (%rax,%rcx,8)
cmpl $0xfb40, 0x9e46(%rip)   # imm = 0xFB40
jl 0x6316
movl $0x0, 0x9e3a(%rip)      # 0x10150
movsd -0x608(%rbp), %xmm0
movsd -0x608(%rbp), %xmm2
movsd -0x610(%rbp), %xmm1
mulsd -0x610(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
addsd -0x618(%rbp), %xmm0
movsd %xmm0, -0x618(%rbp)
movl -0x5ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5ec(%rbp)
jmp 0x6159
movsd -0x618(%rbp), %xmm0
mulsd 0xa286(%rip), %xmm0     # 0x105f8
mulsd 0xa27e(%rip), %xmm0     # 0x105f8
movsd 0x5d46(%rip), %xmm1     # 0xc0c8
mulsd %xmm1, %xmm0
movsd %xmm0, 0xa28a(%rip)     # 0x10618
jmp 0x603b
movl -0x14(%rbp), %edi
callq 0x1110
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x630, %rsp            # imm = 0x630
popq %rbp
retq