// File: /workspace/github_c_makefile_star2_O2/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 0x9443(%rip), %edx      # 0xd2fc
leaq 0x4e6f(%rip), %rsi      # 0x8d2f
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
movl $0x1, 0x4(%rsp)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x4042
movl %eax, %r13d
pushq $0x1
popq %rbp
pushq $0x2
popq %rdx
leaq 0x4(%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 %r13d, %edi
movl %ebp, %esi
movq %r14, %rcx
movl %r15d, %r8d
callq 0x1070
leaq 0x58(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
movzwl 0x93c8(%rip), %eax      # 0xd2fc
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
pushq $0x10
popq %rdx
movl %r13d, %edi
callq 0x1150
testl %eax, %eax
js 0x4055
xorl %ebx, %ebx
leaq 0x18d0b20(%rip), %r14   # 0x18d4a78
leaq 0x10(%rsp), %r15
pushq $0x3c
popq %r12
pushq $0x10
popq %rbp
cmpl $0x0, 0x9375(%rip)      # 0xd2e0
je 0x405e
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 0x9633(%rip), %al       # 0xd5e0
movb %al, 0x17(%rsp)
movsd 0x412f(%rip), %xmm1     # 0x80e8
leaq 0x9660(%rip), %rax      # 0xd620
divsd (%rax), %xmm1
movsd 0x963c(%rip), %xmm0     # 0xd608
mulsd 0x411c(%rip), %xmm0     # 0x80f0
leaq 0x964d(%rip), %rax      # 0xd628
mulsd (%rax), %xmm0
ucomisd 0x4111(%rip), %xmm0     # 0x80f8
jb 0x3fef
sqrtsd %xmm0, %xmm0
jmp 0x4000
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 %r13d, %edi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movq %r14, %r8
movl %ebp, %r9d
callq 0x10e0
testq %rax, %rax
js 0x4068
incq %rbx
movl $0xc350, %edi           # imm = 0xC350
callq 0x11f0
jmp 0x3f64
leaq 0x4d0e(%rip), %rsi      # 0x8d57
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
jmp 0x4081
leaq 0x4d25(%rip), %rsi      # 0x8d81
jmp 0x406f
movl %r13d, %edi
callq 0x10f0
jmp 0x4079
leaq 0x4d3a(%rip), %rsi      # 0x8da9
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
movl %r13d, %edi
callq 0x10f0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq