// File: /workspace/github_c_makefile_star2_O2/hiperiondev[P]hpsdrsim/src/hpsdr_newprotocol.c
// Function: mic_thread
void* mic_thread(void *data) {
    dbg_printf(1, "-- Start mic_thread port: %d\n", mic_port);
    int sock;
    struct sockaddr_in addr;
    unsigned long seqnum;
    unsigned char buffer[132];
    unsigned char *p;
    int yes = 1;
    struct timespec delay;

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
        dbg_printf(1, "***** ERROR: Mic thread: socket\n");
        return NULL;
    }

    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &yes, sizeof(yes));
    setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void*) &yes, sizeof(yes));

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(mic_port);

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

    seqnum = 0;
    memset(buffer, 0, 132);
    clock_gettime(CLOCK_MONOTONIC, &delay);
    while (run) {
        // update seq number
        p = buffer;
        *p++ = (seqnum >> 24) & 0xFF;
        *p++ = (seqnum >> 16) & 0xFF;
        *p++ = (seqnum >> 8) & 0xFF;
        *p++ = (seqnum >> 0) & 0xFF;
        seqnum++;
        // 64 samples with 48000 kHz, makes 1333333 nsec
        delay.tv_nsec += 1333333;
        while (delay.tv_nsec >= 1000000000) {
            delay.tv_nsec -= 1000000000;
            delay.tv_sec++;
        }

        clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &delay, NULL);

        if (sendto(sock, buffer, 132, 0, (struct sockaddr*) &addr_new, sizeof(addr_new)) < 0) {
            dbg_printf(1, "***** ERROR: Mic thread sendto\n");
            break;
        }
    }
    close(sock);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl 0x9263(%rip), %edx      # 0xd30c
leaq 0x4dee(%rip), %rsi      # 0x8e9e
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
movl $0x1, 0xc(%rsp)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x41ef
movl %eax, %ebx
pushq $0x1
popq %rbp
pushq $0x2
popq %rdx
leaq 0xc(%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), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
movzwl 0x91ea(%rip), %eax      # 0xd30c
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x1150
testl %eax, %eax
js 0x4202
leaq 0x30(%rsp), %rdi
xorl %r14d, %r14d
movl $0x84, %edx
xorl %esi, %esi
callq 0x10d0
pushq $0x1
popq %rbp
leaq 0x10(%rsp), %r15
movl %ebp, %edi
movq %r15, %rsi
callq 0x1090
leaq 0x18d090f(%rip), %r12   # 0x18d4a78
pushq $0x10
popq %r13
cmpl $0x0, 0x916c(%rip)      # 0xd2e0
je 0x4213
movl %r14d, %eax
bswapl %eax
movl %eax, 0x30(%rsp)
movq 0x18(%rsp), %rax
movl $0x145855, %ecx         # imm = 0x145855
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rcx
incq %rcx
cmpq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
jl 0x41b7
addq $-0x3b9aca00, %rax      # imm = 0xC4653600
movq %rax, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
jmp 0x419a
incq %r14
movl %ebp, %edi
movl %ebp, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x11b0
movl $0x84, %edx
movl %ebx, %edi
leaq 0x30(%rsp), %rsi
xorl %ecx, %ecx
movq %r12, %r8
movl %r13d, %r9d
callq 0x10e0
testq %rax, %rax
jns 0x416d
leaq 0x4d0f(%rip), %rsi      # 0x8efc
jmp 0x4209
leaq 0x4cc6(%rip), %rsi      # 0x8ebc
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
jmp 0x421a
leaq 0x4cd4(%rip), %rsi      # 0x8edd
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x131a
movl %ebx, %edi
callq 0x10f0
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq