// File: /workspace/github_c_makefile_star2_O1/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 $0xa8, %rsp
movl 0x90e9(%rip), %edx      # 0xc2f4
leaq 0x5465(%rip), %rsi      # 0x8677
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
movl $0x10, 0xc(%rsp)
movl $0x1, 0x8(%rsp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x35d6
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
leaq 0x98(%rsp), %rcx
movq $0x0, (%rcx)
movq $0x2710, 0x8(%rcx)      # imm = 0x2710
movl %ebx, %edi
movl $0x1, %esi
movl $0x14, %edx
movl $0x10, %r8d
callq 0x1070
xorpd %xmm0, %xmm0
leaq 0x80(%rsp), %rsi
movapd %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movzwl 0x9021(%rip), %eax      # 0xc2f4
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x35eb
leaq 0x10(%rsp), %r15
leaq 0x80(%rsp), %r12
leaq 0xc(%rsp), %r13
xorl %r14d, %r14d
movq %r14, %rbp
cmpl $0x0, 0x8fd2(%rip)      # 0xc2e0
je 0x3607
movl $0x3c, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
movq %r12, %r8
movq %r13, %r9
callq 0x1140
testl %eax, %eax
jns 0x333e
callq 0x1030
cmpl $0xb, (%rax)
je 0x3307
jmp 0x35f4
cmpl $0x3c, %eax
jne 0x3622
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 0x338f
movl $0x1, %edi
leaq 0x5389(%rip), %rsi      # 0x870b
movq %rbp, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x13aa
movzbl 0x14(%rsp), %edx
cmpl %edx, 0x9176(%rip)      # 0xc510
je 0x33b5
movl %edx, 0x916e(%rip)      # 0xc510
movl $0x1, %edi
leaq 0x537d(%rip), %rsi      # 0x872b
xorl %eax, %eax
callq 0x13aa
movzbl 0x15(%rsp), %edx
cmpl %edx, 0x9154(%rip)      # 0xc514
je 0x33db
movl %edx, 0x914c(%rip)      # 0xc514
movl $0x1, %edi
leaq 0x536f(%rip), %rsi      # 0x8743
xorl %eax, %eax
callq 0x13aa
movzbl 0x16(%rsp), %edx
cmpl %edx, 0x9132(%rip)      # 0xc518
je 0x3401
movl %edx, 0x912a(%rip)      # 0xc518
movl $0x1, %edi
leaq 0x5360(%rip), %rsi      # 0x875a
xorl %eax, %eax
callq 0x13aa
movzwl 0x17(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
cmpl 0x9109(%rip), %edx      # 0xc51c
je 0x342e
movl %edx, 0x9101(%rip)      # 0xc51c
movl $0x1, %edi
leaq 0x534f(%rip), %rsi      # 0x8776
xorl %eax, %eax
callq 0x13aa
movzbl 0x19(%rsp), %edx
cmpl %edx, 0x90e7(%rip)      # 0xc520
je 0x3454
movl %edx, 0x90df(%rip)      # 0xc520
movl $0x1, %edi
leaq 0x5343(%rip), %rsi      # 0x8790
xorl %eax, %eax
callq 0x13aa
movzbl 0x1a(%rsp), %edx
cmpl %edx, 0x90c5(%rip)      # 0xc524
je 0x347a
movl %edx, 0x90bd(%rip)      # 0xc524
movl $0x1, %edi
leaq 0x5339(%rip), %rsi      # 0x87ac
xorl %eax, %eax
callq 0x13aa
movzwl 0x1b(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
cmpl %edx, 0x909c(%rip)      # 0xc528
je 0x34a7
movl %edx, 0x9094(%rip)      # 0xc528
movl $0x1, %edi
leaq 0x531f(%rip), %rsi      # 0x87bf
xorl %eax, %eax
callq 0x13aa
movzbl 0x1d(%rsp), %edx
cmpl %edx, 0x907a(%rip)      # 0xc52c
je 0x34cd
movl %edx, 0x9072(%rip)      # 0xc52c
movl $0x1, %edi
leaq 0x5314(%rip), %rsi      # 0x87da
xorl %eax, %eax
callq 0x13aa
movzwl 0x1e(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
cmpl %edx, 0x9051(%rip)      # 0xc530
je 0x34fa
movl %edx, 0x9049(%rip)      # 0xc530
movl $0x1, %edi
leaq 0x52fe(%rip), %rsi      # 0x87f1
xorl %eax, %eax
callq 0x13aa
movzbl 0x20(%rsp), %edx
cmpl %edx, 0x902f(%rip)      # 0xc534
je 0x3520
movl %edx, 0x9027(%rip)      # 0xc534
movl $0x1, %edi
leaq 0x52f1(%rip), %rsi      # 0x880a
xorl %eax, %eax
callq 0x13aa
movzbl 0x42(%rsp), %edx
cmpl %edx, 0x900d(%rip)      # 0xc538
je 0x3546
movl %edx, 0x9005(%rip)      # 0xc538
movl $0x1, %edi
leaq 0x52ea(%rip), %rsi      # 0x8829
xorl %eax, %eax
callq 0x13aa
movzbl 0x43(%rsp), %eax
cmpl %eax, 0x8feb(%rip)      # 0xc53c
je 0x3580
movl %eax, 0x8fe3(%rip)      # 0xc53c
cvtsi2sd %eax, %xmm0
mulsd 0x4b1b(%rip), %xmm0     # 0x8080
addsd 0x4b1b(%rip), %xmm0     # 0x8088
movl $0x1, %edi
leaq 0x52ce(%rip), %rsi      # 0x8847
movb $0x1, %al
callq 0x13aa
movzbl 0x4b(%rsp), %eax
cmpl %eax, 0x8fb5(%rip)      # 0xc540
je 0x3304
movl %eax, 0x8fa9(%rip)      # 0xc540
cvtsi2sd %eax, %xmm1
mulsd 0x4acd(%rip), %xmm1     # 0x8070
movsd 0x4acd(%rip), %xmm0     # 0x8078
callq 0x10a0
movsd %xmm0, 0x8b70(%rip)     # 0xc128
movl 0x8f82(%rip), %edx      # 0xc540
movl $0x1, %edi
leaq 0x5297(%rip), %rsi      # 0x8861
xorl %eax, %eax
callq 0x13aa
jmp 0x3304
leaq 0x50c1(%rip), %rsi      # 0x869e
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
jmp 0x360e
leaq 0x50ce(%rip), %rsi      # 0x86c0
jmp 0x35fb
leaq 0x50e0(%rip), %rsi      # 0x86db
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
movl %ebx, %edi
callq 0x10f0
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50d0(%rip), %rsi      # 0x86f9
jmp 0x35fb