// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movl 0x991b(%rip), %edx      # 0x10310
movl $0x1, %edi
leaq 0x63bc(%rip), %rsi      # 0xcdbd
movb $0x0, %al
callq 0x1370
movl $0x10, -0x28(%rbp)
movl $0x1, -0x144(%rbp)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1230
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x6a50
movl $0x1, %edi
leaq 0x63a1(%rip), %rsi      # 0xcddd
movb $0x0, %al
callq 0x1370
movq $0x0, -0x8(%rbp)
jmp 0x6d24
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x144(%rbp), %rcx
movl $0x4, %r8d
callq 0x1060
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0xf, %edx
leaq -0x144(%rbp), %rcx
movl $0x4, %r8d
callq 0x1060
movq $0x0, -0x170(%rbp)
movq $0x2710, -0x168(%rbp) # imm = 0x2710
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0x14, %edx
leaq -0x170(%rbp), %rcx
movl $0x10, %r8d
callq 0x1060
leaq -0x24(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10f0
movw $0x2, -0x24(%rbp)
xorl %edi, %edi
callq 0x10e0
movl %eax, -0x20(%rbp)
movl 0x9827(%rip), %eax      # 0x10310
movzwl %ax, %edi
callq 0x10b0
movw %ax, -0x22(%rbp)
movl -0x14(%rbp), %edi
leaq -0x24(%rbp), %rsi
movl $0x10, %edx
callq 0x1180
cmpl $0x0, %eax
jge 0x6b33
movl $0x1, %edi
leaq 0x62e2(%rip), %rsi      # 0xcdf9
movb $0x0, %al
callq 0x1370
movl -0x14(%rbp), %edi
callq 0x1110
movq $0x0, -0x8(%rbp)
jmp 0x6d24
movq $0x0, -0x30(%rbp)
cmpl $0x0, 0x97ae(%rip)      # 0x102f0
je 0x6d14
movl -0x14(%rbp), %edi
leaq -0x140(%rbp), %rsi
movl $0x104, %edx            # imm = 0x104
xorl %ecx, %ecx
leaq -0x24(%rbp), %r8
leaq -0x28(%rbp), %r9
callq 0x1170
movl %eax, -0x148(%rbp)
cmpl $0x0, -0x148(%rbp)
jge 0x6b97
callq 0x1030
cmpl $0xb, (%rax)
je 0x6b97
movl $0x1, %edi
leaq 0x6288(%rip), %rsi      # 0xce13
movb $0x0, %al
callq 0x1370
jmp 0x6d14
cmpl $0x0, -0x148(%rbp)
jge 0x6ba2
jmp 0x6b3b
cmpl $0x104, -0x148(%rbp)    # imm = 0x104
je 0x6bc6
movl $0x1, %edi
leaq 0x627d(%rip), %rsi      # 0xce37
movb $0x0, %al
callq 0x1370
jmp 0x6d14
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movzbl -0x140(%rbp), %eax
shll $0x18, %eax
movzbl -0x13f(%rbp), %ecx
shll $0x10, %ecx
addl %ecx, %eax
movzbl -0x13e(%rbp), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movzbl -0x13d(%rbp), %ecx
addl %ecx, %eax
cltq
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x6c32
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
je 0x6c32
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl $0x1, %edi
leaq 0x6239(%rip), %rsi      # 0xce64
movb $0x0, %al
callq 0x1370
leaq -0x140(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x158(%rbp)
movl $0x0, -0x14c(%rbp)
cmpl $0x40, -0x14c(%rbp)
jge 0x6d0f
movq -0x158(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x158(%rbp)
movsbl (%rax), %eax
shll $0x8, %eax
movw %ax, -0x15a(%rbp)
movq -0x158(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x158(%rbp)
movzbl (%rax), %ecx
andl $0xff, %ecx
movswl -0x15a(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x15a(%rbp)
movq -0x158(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x158(%rbp)
movsbl (%rax), %eax
shll $0x8, %eax
movw %ax, -0x15c(%rbp)
movq -0x158(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x158(%rbp)
movzbl (%rax), %ecx
andl $0xff, %ecx
movswl -0x15c(%rbp), %eax
orl %ecx, %eax
movw %ax, -0x15c(%rbp)
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x6c4e
jmp 0x6b3b
movl -0x14(%rbp), %edi
callq 0x1110
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)