// File: /workspace/github_c_makefile_star2_O1/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 0x80a9(%rip), %edx      # 0xc30c
leaq 0x4c2c(%rip), %rsi      # 0x8e96
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
movl $0x1, 0xc(%rsp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1200
testl %eax, %eax
js 0x4402
movl %eax, %ebx
leaq 0xc(%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
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movaps %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
movzwl 0x801c(%rip), %eax      # 0xc30c
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1150
testl %eax, %eax
js 0x4417
leaq 0x30(%rsp), %r14
xorl %r13d, %r13d
movl $0x84, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x10d0
leaq 0x10(%rsp), %r15
movl $0x1, %edi
movq %r15, %rsi
callq 0x1090
movl $0x773593ff, %ebp       # imm = 0x773593FF
leaq 0x18cf737(%rip), %r12   # 0x18d3a78
cmpl $0x0, 0x7f98(%rip)      # 0xc2e0
je 0x442a
movl %r13d, %eax
bswapl %eax
movl %eax, 0x30(%rsp)
movq 0x18(%rsp), %rcx
leaq 0x145855(%rcx), %rax
movq %rax, 0x18(%rsp)
cmpq $0x3b8671ab, %rcx       # imm = 0x3B8671AB
jl 0x43bf
cmpq %rbp, %rax
cmovgeq %rbp, %rax
negq %rax
addq %rcx, %rax
addq $0x3baf2254, %rax       # imm = 0x3BAF2254
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rdx # imm = 0x44B82FA09B5A53
mulq %rdx
movq 0x10(%rsp), %rax
shrq $0xb, %rdx
imulq $-0x3b9aca00, %rdx, %rsi # imm = 0xC4653600
addq %rsi, %rcx
addq $-0x3b8671ab, %rcx      # imm = 0xC4798E55
addq %rdx, %rax
incq %rax
movq %rcx, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x1, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x11b0
movl $0x84, %edx
movl %ebx, %edi
movq %r14, %rsi
xorl %ecx, %ecx
movq %r12, %r8
movl $0x10, %r9d
callq 0x10e0
incq %r13
testq %rax, %rax
jns 0x4341
leaq 0x4af4(%rip), %rsi      # 0x8ef4
jmp 0x441e
leaq 0x4aab(%rip), %rsi      # 0x8eb4
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
jmp 0x4431
leaq 0x4ab7(%rip), %rsi      # 0x8ed5
movl $0x1, %edi
xorl %eax, %eax
callq 0x13aa
movl %ebx, %edi
callq 0x10f0
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq