// File: /workspace/github_c_makefile_star2_O0/c-zhou[P]yahs/link.c
// Function: estimate_noise
int estimate_noise(inter_link_mat_t *link_mat)
{
    uint32_t i, j, k, n;
    uint32_t bin, *hist;
    uint64_t total;
    double l;
    inter_link_t *inter_link;

    bin = 4096;
    hist = (uint32_t *) calloc(bin, sizeof(uint32_t));
    for (i = 0; i < link_mat->n; ++i) {
        inter_link = &link_mat->links[i];
        n = inter_link->n;
        for (j = 0; j < n; ++j) {
            for (k = 0; k < 4; ++k) {
                l = inter_link->link[k][j];
                if (l < .0) 
                    continue;
                ++hist[MIN(bin - 1, (int) l)];
            }
        }
    }
   
    hist[0] = 0;

    total = 0;
    for (i = 0; i < bin; ++i)
        total += hist[i];
    total >>= 1;

#ifdef DEBUG_NOISE
    for (i = 0; i < bin; ++i)
        fprintf(stderr, "[DEBUG_NOISE::%s] noise bin %d %d\n", __func__, i, hist[i]);
#endif

    n = 0;
    while (hist[n] < total) {
        ++n;
        if (n == bin)
            break;
        hist[n] += hist[n - 1];
    }

    if (n == 4096)
        fprintf(stderr, "[W::%s] noise esimation might be incorrect %d\n", __func__, n);
    
    free(hist);

    return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1000, -0x1c(%rbp)    # imm = 0x1000
movl -0x1c(%rbp), %eax
movl %eax, %edi
movl $0x4, %esi
callq 0x2290
movq %rax, -0x28(%rbp)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x10cc8
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc(%rbp), %ecx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x10cb8
movl $0x0, -0x14(%rbp)
cmpl $0x4, -0x14(%rbp)
jae 0x10ca8
movq -0x40(%rbp), %rax
movl -0x14(%rbp), %ecx
movq 0x20(%rax,%rcx,8), %rax
movl -0x10(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x38(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x38(%rbp), %xmm0
jbe 0x10c61
jmp 0x10c9d
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x1c(%rbp), %eax
subl $0x1, %eax
cvttsd2si -0x38(%rbp), %ecx
cmpl %ecx, %eax
jae 0x10c83
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x10c8b
cvttsd2si -0x38(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x10c36
jmp 0x10caa
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x10c23
jmp 0x10cba
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x10bed
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
movq $0x0, -0x30(%rbp)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x10d06
movq -0x28(%rbp), %rax
movl -0xc(%rbp), %ecx
movl (%rax,%rcx,4), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x10ce1
movq -0x30(%rbp), %rax
shrq %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x18(%rbp)
movq -0x28(%rbp), %rax
movl -0x18(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpq -0x30(%rbp), %rax
jae 0x10d59
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x10d3b
jmp 0x10d59
movq -0x28(%rbp), %rax
movl -0x18(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movl -0x18(%rbp), %ecx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x10d18
cmpl $0x1000, -0x18(%rbp)    # imm = 0x1000
jne 0x10d84
movq 0x2828f(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movl -0x18(%rbp), %ecx
leaq 0x20ab8(%rip), %rsi     # 0x3182e
leaq 0x20ae0(%rip), %rdx     # 0x3185d
movb $0x0, %al
callq 0x22c0
movq -0x28(%rbp), %rdi
callq 0x2050
movl -0x18(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)