// File: /workspace/github_c_makefile_star2_O1/hiperiondev[P]hpsdrsim/src/hpsdr_newprotocol.c
// Function: audio_thread
void* audio_thread(void *data) {
    dbg_printf(1, "-- Start audio_thread port: %d\n", audio_port);
    int sock;
    struct sockaddr_in addr;
    socklen_t lenaddr = sizeof(addr);
    unsigned long seqnum, seqold;
    unsigned char buffer[260];
    int yes = 1;
    int rc;
    int i;
    unsigned char *p;
    int16_t lsample, rsample;
    struct timeval tv;

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

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

    seqnum = 0;
    while (run) {
        rc = recvfrom(sock, buffer, 260, 0, (struct sockaddr*) &addr, &lenaddr);
        if (rc < 0 && errno != EAGAIN) {
            dbg_printf(1, "***** ERROR: Audio thread: recvmsg\n");
            break;
        }
        if (rc < 0)
            continue;
        if (rc != 260) {
            dbg_printf(1, "Received Audio packet with incorrect 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, "Audio thread: SEQ ERROR, old=%lu new=%lu\n", seqold, seqnum);
        }
        p = buffer + 4;
        for (i = 0; i < 64; i++) {
            lsample = ((signed char) *p++) << 8;
            lsample |= (*p++ & 0xff);
            rsample = ((signed char) *p++) << 8;
            rsample |= (*p++ & 0xff);
        }
    }
    close(sock);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl 0x7ea4(%rip), %edx      # 0xc300
leaq 0x4962(%rip), %rsi      # 0x8dc5
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 0x45e6
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 0x20(%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
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movzwl 0x7de4(%rip), %eax      # 0xc300
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x45fb
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0xc(%rsp), %r13
xorl %ebp, %ebp
movq %rbp, %r14
cmpl $0x0, 0x7d8d(%rip)      # 0xc2e0
je 0x4617
movl $0x104, %edx            # imm = 0x104
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
movq %r12, %r8
movq %r13, %r9
callq 0x1140
testl %eax, %eax
jns 0x4583
callq 0x1030
cmpl $0xb, (%rax)
je 0x454c
jmp 0x4604
cmpl $0x104, %eax            # imm = 0x104
jne 0x4632
movl 0x30(%rsp), %eax
shll $0x18, %eax
movzbl 0x31(%rsp), %ecx
shll $0x10, %ecx
cltq
orq %rcx, %rax
movzbl 0x32(%rsp), %ecx
shll $0x8, %ecx
orq %rax, %rcx
movzbl 0x33(%rsp), %ebp
orq %rcx, %rbp
testl %ebp, %ebp
sete %al
leaq 0x1(%r14), %rcx
cmpq %rbp, %rcx
sete %cl
orb %al, %cl
jne 0x4549
movl $0x1, %edi
leaq 0x4898(%rip), %rsi      # 0x8e6c
movq %r14, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x13aa
jmp 0x4549
leaq 0x47f8(%rip), %rsi      # 0x8de5
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
jmp 0x461e
leaq 0x47ff(%rip), %rsi      # 0x8e01
jmp 0x460b
leaq 0x4810(%rip), %rsi      # 0x8e1b
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
movl %ebx, %edi
callq 0x10f0
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4806(%rip), %rsi      # 0x8e3f
jmp 0x460b
nop