// File: /workspace/github_c_makefile_star2_O3/hiperiondev[P]hpsdrsim/src/hpsdr_newprotocol.c
// Function: duc_specific_thread
void* duc_specific_thread(void *data) {
    dbg_printf(1, "-- Start duc_specific_thread port: %d\n", duc_port);
    int sock;
    struct sockaddr_in addr;
    socklen_t lenaddr = sizeof(addr);
    unsigned long seqnum, seqold;
    struct timeval tv;
    unsigned char buffer[100];
    int yes = 1;
    int rc;

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

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

    seqnum = 0;
    while (run) {
        rc = recvfrom(sock, buffer, 60, 0, (struct sockaddr*) &addr, &lenaddr);
        if (rc < 0 && errno != EAGAIN) {
            dbg_printf(1, "***** ERROR: TXspec: recvmsg\n");
            break;
        }
        if (rc < 0)
            continue;
        if (rc != 60) {
            dbg_printf(1, "TX: wrong 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, "TX: SEQ ERROR, old=%lu new=%lu\n", seqold, seqnum);
        }
        if (dac != buffer[4]) {
            dac = buffer[4];
            dbg_printf(1, "TX: Number of DACs: %d\n", dac);
        }
        if (cwmode != buffer[5]) {
            cwmode = buffer[5];
            dbg_printf(1, "TX: CW mode bits = %x\n", cwmode);
        }
        if (sidelevel != buffer[6]) {
            sidelevel = buffer[6];
            dbg_printf(1, "TX: CW side tone level: %d\n", sidelevel);
        }
        rc = (buffer[7] << 8) + buffer[8];
        if (rc != sidefreq) {
            sidefreq = rc;
            dbg_printf(1, "TX: CW sidetone freq: %d\n", sidefreq);
        }
        if (speed != buffer[9]) {
            speed = buffer[9];
            dbg_printf(1, "TX: CW keyer speed: %d wpm\n", speed);
        }
        if (weight != buffer[10]) {
            weight = buffer[10];
            dbg_printf(1, "TX: CW weight: %d\n", weight);
        }
        rc = (buffer[11] << 8) + buffer[12];
        if (hang != rc) {
            hang = rc;
            dbg_printf(1, "TX: CW hang time: %d msec\n", hang);
        }
        if (delay != buffer[13]) {
            delay = buffer[13];
            dbg_printf(1, "TX: RF delay: %d msec\n", delay);
        }
        rc = (buffer[14] << 8) + buffer[15];
        if (txrate != rc) {
            txrate = rc;
            dbg_printf(1, "TX: DUC sample rate: %d\n", rc);
        }
        if (ducbits != buffer[16]) {
            ducbits = buffer[16];
            dbg_printf(1, "TX: DUC sample width: %d bits\n", ducbits);
        }
        if (orion != buffer[50]) {
            orion = buffer[50];
            dbg_printf(1, "TX: ORION bits (mic etc): %x\n", orion);
        }
        if (gain != buffer[51]) {
            gain = buffer[51];
            dbg_printf(1, "TX: LineIn Gain (dB): %f\n", 12.0 - 1.5 * gain);
        }
        if (txatt != buffer[59]) {
            txatt = buffer[59];
            txatt_dbl = pow(10.0, -0.05 * txatt);
            dbg_printf(1, "TX: ATT DUC0/ADC0: %d\n", txatt);
        }
    }
    close(sock);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl 0x9064(%rip), %edx      # 0xc2f4
leaq 0x53e0(%rip), %rsi      # 0x8677
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
movl $0x10, 0xc(%rsp)
movl $0x1, 0x8(%rsp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x3650
movl %eax, %ebx
leaq 0x8(%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 0x88(%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 0x78(%rsp), %rsi
movq %r14, 0x8(%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movzwl 0x8fa4(%rip), %eax      # 0xc2f4
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x3665
leaq 0x10(%rsp), %r15
leaq 0x78(%rsp), %r12
leaq 0xc(%rsp), %r13
xorl %r14d, %r14d
movq %r14, %rbp
cmpl $0x0, 0x8f58(%rip)      # 0xc2e0
je 0x3681
movl $0x3c, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
movq %r12, %r8
movq %r13, %r9
callq 0x1140
testl %eax, %eax
jns 0x33b8
callq 0x1030
cmpl $0xb, (%rax)
je 0x3381
jmp 0x366e
cmpl $0x3c, %eax
jne 0x369c
movzbl 0x11(%rsp), %eax
shll $0x10, %eax
movzbl 0x12(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x13(%rsp), %r14d
orq %rcx, %r14
testq %r14, %r14
sete %al
leaq 0x1(%rbp), %rcx
cmpq %rcx, %r14
sete %cl
orb %al, %cl
jne 0x3409
movl $0x1, %edi
leaq 0x530f(%rip), %rsi      # 0x870b
movq %rbp, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x13ba
movzbl 0x14(%rsp), %edx
cmpl %edx, 0x90fc(%rip)      # 0xc510
je 0x342f
movl %edx, 0x90f4(%rip)      # 0xc510
movl $0x1, %edi
leaq 0x5303(%rip), %rsi      # 0x872b
xorl %eax, %eax
callq 0x13ba
movzbl 0x15(%rsp), %edx
cmpl %edx, 0x90da(%rip)      # 0xc514
je 0x3455
movl %edx, 0x90d2(%rip)      # 0xc514
movl $0x1, %edi
leaq 0x52f5(%rip), %rsi      # 0x8743
xorl %eax, %eax
callq 0x13ba
movzbl 0x16(%rsp), %edx
cmpl %edx, 0x90b8(%rip)      # 0xc518
je 0x347b
movl %edx, 0x90b0(%rip)      # 0xc518
movl $0x1, %edi
leaq 0x52e6(%rip), %rsi      # 0x875a
xorl %eax, %eax
callq 0x13ba
movzwl 0x17(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
cmpl 0x908f(%rip), %edx      # 0xc51c
je 0x34a8
movl %edx, 0x9087(%rip)      # 0xc51c
movl $0x1, %edi
leaq 0x52d5(%rip), %rsi      # 0x8776
xorl %eax, %eax
callq 0x13ba
movzbl 0x19(%rsp), %edx
cmpl %edx, 0x906d(%rip)      # 0xc520
je 0x34ce
movl %edx, 0x9065(%rip)      # 0xc520
movl $0x1, %edi
leaq 0x52c9(%rip), %rsi      # 0x8790
xorl %eax, %eax
callq 0x13ba
movzbl 0x1a(%rsp), %edx
cmpl %edx, 0x904b(%rip)      # 0xc524
je 0x34f4
movl %edx, 0x9043(%rip)      # 0xc524
movl $0x1, %edi
leaq 0x52bf(%rip), %rsi      # 0x87ac
xorl %eax, %eax
callq 0x13ba
movzwl 0x1b(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
cmpl %edx, 0x9022(%rip)      # 0xc528
je 0x3521
movl %edx, 0x901a(%rip)      # 0xc528
movl $0x1, %edi
leaq 0x52a5(%rip), %rsi      # 0x87bf
xorl %eax, %eax
callq 0x13ba
movzbl 0x1d(%rsp), %edx
cmpl %edx, 0x9000(%rip)      # 0xc52c
je 0x3547
movl %edx, 0x8ff8(%rip)      # 0xc52c
movl $0x1, %edi
leaq 0x529a(%rip), %rsi      # 0x87da
xorl %eax, %eax
callq 0x13ba
movzwl 0x1e(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
cmpl %edx, 0x8fd7(%rip)      # 0xc530
je 0x3574
movl %edx, 0x8fcf(%rip)      # 0xc530
movl $0x1, %edi
leaq 0x5284(%rip), %rsi      # 0x87f1
xorl %eax, %eax
callq 0x13ba
movzbl 0x20(%rsp), %edx
cmpl %edx, 0x8fb5(%rip)      # 0xc534
je 0x359a
movl %edx, 0x8fad(%rip)      # 0xc534
movl $0x1, %edi
leaq 0x5277(%rip), %rsi      # 0x880a
xorl %eax, %eax
callq 0x13ba
movzbl 0x42(%rsp), %edx
cmpl %edx, 0x8f93(%rip)      # 0xc538
je 0x35c0
movl %edx, 0x8f8b(%rip)      # 0xc538
movl $0x1, %edi
leaq 0x5270(%rip), %rsi      # 0x8829
xorl %eax, %eax
callq 0x13ba
movzbl 0x43(%rsp), %eax
cmpl %eax, 0x8f71(%rip)      # 0xc53c
je 0x35fa
movl %eax, 0x8f69(%rip)      # 0xc53c
cvtsi2sd %eax, %xmm0
mulsd 0x4aa1(%rip), %xmm0     # 0x8080
addsd 0x4aa1(%rip), %xmm0     # 0x8088
movl $0x1, %edi
leaq 0x5254(%rip), %rsi      # 0x8847
movb $0x1, %al
callq 0x13ba
movzbl 0x4b(%rsp), %eax
cmpl %eax, 0x8f3b(%rip)      # 0xc540
je 0x337e
movl %eax, 0x8f2f(%rip)      # 0xc540
cvtsi2sd %eax, %xmm1
mulsd 0x4a53(%rip), %xmm1     # 0x8070
movsd 0x4a53(%rip), %xmm0     # 0x8078
callq 0x10a0
movsd %xmm0, 0x8af6(%rip)     # 0xc128
movl 0x8f08(%rip), %edx      # 0xc540
movl $0x1, %edi
leaq 0x521d(%rip), %rsi      # 0x8861
xorl %eax, %eax
callq 0x13ba
jmp 0x337e
leaq 0x5047(%rip), %rsi      # 0x869e
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
jmp 0x3688
leaq 0x5054(%rip), %rsi      # 0x86c0
jmp 0x3675
leaq 0x5066(%rip), %rsi      # 0x86db
movl $0x1, %edi
xorl %eax, %eax
callq 0x13ba
movl %ebx, %edi
callq 0x10f0
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5056(%rip), %rsi      # 0x86f9
jmp 0x3675