// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movl 0x9bf7(%rip), %edx      # 0x1031c
movl $0x1, %edi
leaq 0x675d(%rip), %rsi      # 0xce8e
movb $0x0, %al
callq 0x1370
movl $0x1, -0xcc(%rbp)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1230
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x6779
movl $0x1, %edi
leaq 0x6747(%rip), %rsi      # 0xceac
movb $0x0, %al
callq 0x1370
movq $0x0, -0x8(%rbp)
jmp 0x69c9
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0xcc(%rbp), %rcx
movl $0x4, %r8d
callq 0x1060
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0xf, %edx
leaq -0xcc(%rbp), %rcx
movl $0x4, %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 0x9b3f(%rip), %eax      # 0x1031c
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 0x6827
movl $0x1, %edi
leaq 0x66c2(%rip), %rsi      # 0xcecd
movb $0x0, %al
callq 0x1370
movl -0x14(%rbp), %edi
callq 0x1110
movq $0x0, -0x8(%rbp)
jmp 0x69c9
movq $0x0, -0x30(%rbp)
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x84, %edx
callq 0x10f0
movl $0x1, %edi
leaq -0xe0(%rbp), %rsi
callq 0x1080
cmpl $0x0, 0x9a96(%rip)      # 0x102f0
je 0x69b9
leaq -0xc0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x30(%rbp), %rax
shrq $0x18, %rax
andq $0xff, %rax
movb %al, %cl
movq -0xc8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xc8(%rbp)
movb %cl, (%rax)
movq -0x30(%rbp), %rax
shrq $0x10, %rax
andq $0xff, %rax
movb %al, %cl
movq -0xc8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xc8(%rbp)
movb %cl, (%rax)
movq -0x30(%rbp), %rax
shrq $0x8, %rax
andq $0xff, %rax
movb %al, %cl
movq -0xc8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xc8(%rbp)
movb %cl, (%rax)
movq -0x30(%rbp), %rax
shrq $0x0, %rax
andq $0xff, %rax
movb %al, %cl
movq -0xc8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xc8(%rbp)
movb %cl, (%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0xd8(%rbp), %rax
addq $0x145855, %rax         # imm = 0x145855
movq %rax, -0xd8(%rbp)
cmpq $0x3b9aca00, -0xd8(%rbp) # imm = 0x3B9ACA00
jl 0x695f
movq -0xd8(%rbp), %rax
subq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
movq %rax, -0xd8(%rbp)
movq -0xe0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
jmp 0x692a
movl $0x1, %esi
leaq -0xe0(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
movl %esi, %edi
callq 0x11e0
movl -0x14(%rbp), %edi
leaq -0xc0(%rbp), %rsi
movl $0x84, %edx
xorl %ecx, %ecx
leaq 0x18d110a(%rip), %r8    # 0x18d7a98
movl $0x10, %r9d
callq 0x1100
cmpq $0x0, %rax
jge 0x69b4
movl $0x1, %edi
leaq 0x6541(%rip), %rsi      # 0xceec
movb $0x0, %al
callq 0x1370
jmp 0x69b9
jmp 0x6853
movl -0x14(%rbp), %edi
callq 0x1110
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)