// File: /workspace/github_c_makefile_star2_O3/hiperiondev[P]hpsdrsim/src/hpsdr_newprotocol.c
// Function: send_highprio_thread
void* send_highprio_thread(void *data) {
    dbg_printf(1, "-- Start send_highprio_thread port: %d\n", shp_port);
    int sock;
    struct sockaddr_in addr;
    unsigned long seqnum;
    unsigned char buffer[60];
    int yes = 1;
    int rc;
    unsigned char *p;

    seqnum = 0;

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

    if (bind(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
        dbg_printf(1, "***** ERROR: SendHighPrio thread: bind\n");
        close(sock);
        return NULL;
    }

    seqnum = 0;
    while (1) {
        if (!run) {
            close(sock);
            break;
        }
        // prepare buffer
        memset(buffer, 0, 60);
        p = buffer;
        *p++ = (seqnum >> 24) & 0xFF;
        *p++ = (seqnum >> 16) & 0xFF;
        *p++ = (seqnum >> 8) & 0xFF;
        *p++ = (seqnum >> 0) & 0xFF;
        *p++ = 0;  // no PTT and CW attached
        *p++ = 0;  // no ADC overload
        *p++ = 0;
        *p++ = txdrive;

        p += 6;

        rc = (int) ((4095.0 / c1) * sqrt(100.0 * txlevel * c2));
        *p++ = (rc >> 8) & 0xFF;
        *p++ = (rc) & 0xFF;

        buffer[49] = 63;  // about 13 volts supply

        if (sendto(sock, buffer, 60, 0, (struct sockaddr*) &addr_new, sizeof(addr_new)) < 0) {
            dbg_printf(1, "***** ERROR: HP send thread sendto\n");
            break;
        }
        seqnum++;
        usleep(50000);  // wait 50 msec then send again
    }
    close(sock);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl 0x813d(%rip), %edx      # 0xc2fc
leaq 0x4b61(%rip), %rsi      # 0x8d27
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
movl $0x1, 0x4(%rsp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x436f
movl %eax, %ebp
leaq 0x4(%rsp), %r14
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
movl $0x4, %r8d
callq 0x1070
movl %ebp, %edi
movl $0x1, %esi
movl $0xf, %edx
movq %r14, %rcx
movl $0x4, %r8d
callq 0x1070
leaq 0x58(%rsp), %rsi
movq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movzwl 0x80ae(%rip), %eax      # 0xc2fc
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebp, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x4384
cmpl $0x0, 0x806f(%rip)      # 0xc2e0
je 0x4366
xorl %ebx, %ebx
leaq 0x83a0(%rip), %r12      # 0xc620
leaq 0x83a1(%rip), %r13      # 0xc628
leaq 0x18cf7ea(%rip), %r14   # 0x18d3a78
leaq 0x10(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movups %xmm0, 0x3c(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl %ebx, %eax
shrl $0x18, %eax
movb %al, 0x10(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x11(%rsp)
movb %bh, 0x12(%rsp)
movb %bl, 0x13(%rsp)
movb $0x0, 0x15(%rsp)
movb 0x8311(%rip), %al       # 0xc5e0
movb %al, 0x17(%rsp)
movsd 0x3e05(%rip), %xmm1     # 0x80e0
divsd (%r12), %xmm1
movsd 0x831f(%rip), %xmm0     # 0xc608
mulsd 0x3df7(%rip), %xmm0     # 0x80e8
mulsd (%r13), %xmm0
ucomisd 0x3df1(%rip), %xmm0     # 0x80f0
jb 0x4307
sqrtsd %xmm0, %xmm0
jmp 0x4318
movsd %xmm1, 0x8(%rsp)
callq 0x11c0
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
movb %ah, 0x1e(%rsp)
movb %al, 0x1f(%rsp)
movb $0x3f, 0x41(%rsp)
movl $0x3c, %edx
movl %ebp, %edi
movq %r15, %rsi
xorl %ecx, %ecx
movq %r14, %r8
movl $0x10, %r9d
callq 0x10e0
testq %rax, %rax
js 0x438d
incq %rbx
movl $0xc350, %edi           # imm = 0xC350
callq 0x11f0
cmpl $0x0, 0x7f80(%rip)      # 0xc2e0
jne 0x4293
movl %ebp, %edi
callq 0x10f0
jmp 0x43a0
leaq 0x49d9(%rip), %rsi      # 0x8d4f
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
jmp 0x43a7
leaq 0x49ee(%rip), %rsi      # 0x8d79
jmp 0x4394
leaq 0x4a0d(%rip), %rsi      # 0x8da1
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
movl %ebp, %edi
callq 0x10f0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq