// File: /workspace/github_c_makefile_star2_O2/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 0x90bb(%rip), %edx      # 0xd300
leaq 0x4b81(%rip), %rsi      # 0x8dcd
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 0x43ad
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 0x20(%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 0x10(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
movzwl 0x9016(%rip), %eax      # 0xd300
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl %r14d, %edx
callq 0x1150
testl %eax, %eax
js 0x43c0
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %r12
leaq 0xc(%rsp), %r13
xorl %ebp, %ebp
movq %rbp, %r14
cmpl $0x0, 0x8fc1(%rip)      # 0xd2e0
je 0x43da
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 0x434c
callq 0x1030
cmpl $0xb, (%rax)
je 0x4318
jmp 0x43c9
cmpl $0x104, %eax            # imm = 0x104
jne 0x43f5
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 0x4315
pushq $0x1
popq %rdi
leaq 0x4ad9(%rip), %rsi      # 0x8e74
movq %r14, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x131a
jmp 0x4315
leaq 0x4a39(%rip), %rsi      # 0x8ded
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
jmp 0x43e1
leaq 0x4a42(%rip), %rsi      # 0x8e09
jmp 0x43d0
leaq 0x4a53(%rip), %rsi      # 0x8e23
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
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 0x4a4b(%rip), %rsi      # 0x8e47
jmp 0x43d0
nop