// File: /workspace/github_c_makefile_star2_O2/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 0xa1fc(%rip), %edx      # 0xd2f4
leaq 0x5580(%rip), %rsi      # 0x867f
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
movl $0x10, 0xc(%rsp)
movl $0x1, 0x8(%rsp)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x3493
movl %eax, %ebx
pushq $0x1
popq %rbp
pushq $0x2
popq %rdx
leaq 0x8(%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 0x88(%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 0x78(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
movzwl 0xa154(%rip), %eax      # 0xd2f4
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl %r14d, %edx
callq 0x1150
testl %eax, %eax
js 0x34a6
leaq 0x10(%rsp), %r15
pushq $0x3c
popq %r12
leaq 0x78(%rsp), %r13
leaq 0xc(%rsp), %rbp
xorl %ecx, %ecx
movq %rcx, %r14
cmpl $0x0, 0xa107(%rip)      # 0xd2e0
je 0x34c0
movl %ebx, %edi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movq %r13, %r8
movq %rbp, %r9
callq 0x1140
testl %eax, %eax
jns 0x3207
callq 0x1030
cmpl $0xb, (%rax)
je 0x31d2
jmp 0x34af
cmpl $0x3c, %eax
jne 0x34db
movzbl 0x11(%rsp), %eax
shll $0x10, %eax
movzbl 0x12(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x13(%rsp), %edx
orq %rcx, %rdx
testq %rdx, %rdx
sete %al
leaq 0x1(%r14), %rcx
movq %rdx, (%rsp)
cmpq %rcx, %rdx
sete %cl
orb %al, %cl
jne 0x325a
pushq $0x1
popq %rdi
leaq 0x54c7(%rip), %rsi      # 0x8713
movq %r14, %rdx
movq (%rsp), %rcx
xorl %eax, %eax
callq 0x131a
movzbl 0x14(%rsp), %edx
cmpl %edx, 0xa2ab(%rip)      # 0xd510
pushq $0x1
popq %r14
je 0x3282
movl %edx, 0xa29f(%rip)      # 0xd510
movl %r14d, %edi
leaq 0x54b8(%rip), %rsi      # 0x8733
xorl %eax, %eax
callq 0x131a
movzbl 0x15(%rsp), %edx
cmpl %edx, 0xa287(%rip)      # 0xd514
je 0x32a6
movl %edx, 0xa27f(%rip)      # 0xd514
movl %r14d, %edi
leaq 0x54ac(%rip), %rsi      # 0x874b
xorl %eax, %eax
callq 0x131a
movzbl 0x16(%rsp), %edx
cmpl %edx, 0xa267(%rip)      # 0xd518
je 0x32ca
movl %edx, 0xa25f(%rip)      # 0xd518
movl %r14d, %edi
leaq 0x549f(%rip), %rsi      # 0x8762
xorl %eax, %eax
callq 0x131a
movzwl 0x17(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
cmpl 0xa240(%rip), %edx      # 0xd51c
je 0x32f5
movl %edx, 0xa238(%rip)      # 0xd51c
movl %r14d, %edi
leaq 0x5490(%rip), %rsi      # 0x877e
xorl %eax, %eax
callq 0x131a
movzbl 0x19(%rsp), %edx
cmpl %edx, 0xa220(%rip)      # 0xd520
je 0x3319
movl %edx, 0xa218(%rip)      # 0xd520
movl %r14d, %edi
leaq 0x5486(%rip), %rsi      # 0x8798
xorl %eax, %eax
callq 0x131a
movzbl 0x1a(%rsp), %edx
cmpl %edx, 0xa200(%rip)      # 0xd524
je 0x333d
movl %edx, 0xa1f8(%rip)      # 0xd524
movl %r14d, %edi
leaq 0x547e(%rip), %rsi      # 0x87b4
xorl %eax, %eax
callq 0x131a
movzwl 0x1b(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
cmpl %edx, 0xa1d9(%rip)      # 0xd528
je 0x3368
movl %edx, 0xa1d1(%rip)      # 0xd528
movl %r14d, %edi
leaq 0x5466(%rip), %rsi      # 0x87c7
xorl %eax, %eax
callq 0x131a
movzbl 0x1d(%rsp), %edx
cmpl %edx, 0xa1b9(%rip)      # 0xd52c
je 0x338c
movl %edx, 0xa1b1(%rip)      # 0xd52c
movl %r14d, %edi
leaq 0x545d(%rip), %rsi      # 0x87e2
xorl %eax, %eax
callq 0x131a
movzwl 0x1e(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
cmpl %edx, 0xa192(%rip)      # 0xd530
je 0x33b7
movl %edx, 0xa18a(%rip)      # 0xd530
movl %r14d, %edi
leaq 0x5449(%rip), %rsi      # 0x87f9
xorl %eax, %eax
callq 0x131a
movzbl 0x20(%rsp), %edx
cmpl %edx, 0xa172(%rip)      # 0xd534
je 0x33db
movl %edx, 0xa16a(%rip)      # 0xd534
movl %r14d, %edi
leaq 0x543e(%rip), %rsi      # 0x8812
xorl %eax, %eax
callq 0x131a
movzbl 0x42(%rsp), %edx
cmpl %edx, 0xa152(%rip)      # 0xd538
je 0x33ff
movl %edx, 0xa14a(%rip)      # 0xd538
movl %r14d, %edi
leaq 0x5439(%rip), %rsi      # 0x8831
xorl %eax, %eax
callq 0x131a
movzbl 0x43(%rsp), %eax
cmpl %eax, 0xa132(%rip)      # 0xd53c
je 0x3437
movl %eax, 0xa12a(%rip)      # 0xd53c
cvtsi2sd %eax, %xmm0
mulsd 0x4c72(%rip), %xmm0     # 0x8090
addsd 0x4c72(%rip), %xmm0     # 0x8098
movl %r14d, %edi
leaq 0x541f(%rip), %rsi      # 0x884f
movb $0x1, %al
callq 0x131a
movzbl 0x4b(%rsp), %eax
cmpl %eax, 0xa0fe(%rip)      # 0xd540
movq (%rsp), %rcx
je 0x31cf
movl %eax, 0xa0ee(%rip)      # 0xd540
cvtsi2sd %eax, %xmm1
mulsd 0x4c22(%rip), %xmm1     # 0x8080
movsd 0x4c22(%rip), %xmm0     # 0x8088
callq 0x10a0
movsd %xmm0, 0x9cb5(%rip)     # 0xd128
movl 0xa0c7(%rip), %edx      # 0xd540
movl %r14d, %edi
leaq 0x53e6(%rip), %rsi      # 0x8869
xorl %eax, %eax
callq 0x131a
movq (%rsp), %rcx
jmp 0x31cf
leaq 0x520c(%rip), %rsi      # 0x86a6
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
jmp 0x34c7
leaq 0x521b(%rip), %rsi      # 0x86c8
jmp 0x34b6
leaq 0x522d(%rip), %rsi      # 0x86e3
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
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 0x521f(%rip), %rsi      # 0x8701
jmp 0x34b6