// File: /workspace/github_c_makefile_star2_O2/jech[P]dht/dht.c
// Function: bucket_maintenance
static int
bucket_maintenance(int af)
{
    struct bucket *b;

    b = af == AF_INET ? buckets : buckets6;

    while(b) {
        /* 10 minutes for an 8-node bucket */
        int to = MAX(600 / (b->max_count / 8), 30);
        struct bucket *q;
        if(b->time < now.tv_sec - to) {
            /* This bucket hasn't seen any positive confirmation for a long
               time.  Pick a random id in this bucket's range, and send
               a request to a random node. */
            unsigned char id[20];
            struct node *n;
            int rc;

            rc = bucket_random(b, id);
            if(rc < 0)
                memcpy(id, b->first, 20);

            q = b;
            /* If the bucket is empty, we try to fill it from a neighbour.
               We also sometimes do it gratuitiously to recover from
               buckets full of broken nodes. */
            if(q->next && (q->count == 0 || (random() & 7) == 0))
                q = b->next;
            if(q->count == 0 || (random() & 7) == 0) {
                struct bucket *r;
                r = previous_bucket(b);
                if(r && r->count > 0)
                    q = r;
            }

            if(q) {
                n = random_node(q);
                if(n) {
                    unsigned char tid[4];
                    int want = -1;

                    if(dht_socket >= 0 && dht_socket6 >= 0) {
                        struct bucket *otherbucket;
                        otherbucket =
                            find_bucket(id, af == AF_INET ? AF_INET6 : AF_INET);
                        if(otherbucket &&
                           otherbucket->count < otherbucket->max_count)
                            /* The corresponding bucket in the other family
                               is not full -- querying both is useful. */
                            want = WANT4 | WANT6;
                        else if(random() % 37 == 0)
                            /* Most of the time, this just adds overhead.
                               However, it might help stitch back one of
                               the DHTs after a network collapse, so query
                               both, but only very occasionally. */
                            want = WANT4 | WANT6;
                    }

                    debugf("Sending find_node for%s bucket maintenance.\n",
                           af == AF_INET6 ? " IPv6" : "");
                    make_tid(tid, "fn", 0);
                    send_find_node((struct sockaddr*)&n->ss, n->sslen,
                                   tid, 4, id, want,
                                   n->reply_time >= now.tv_sec - 15);
                    pinged(n, q);
                    /* In order to avoid sending queries back-to-back,
                       give up for now and reschedule us soon. */
                    return 1;
                }
            }
        }
        b = b->next;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edi, 0xc(%rsp)
cmpl $0x2, %edi
leaq 0x6e9f(%rip), %rax      # 0xdc50
leaq 0x6ea0(%rip), %rcx      # 0xdc58
cmoveq %rax, %rcx
movq (%rcx), %r14
pushq $0x8
popq %rbp
pushq $0x1e
popq %r15
testq %r14, %r14
je 0x6f0f
movl 0x1c(%r14), %eax
cltd
idivl %ebp
movl %eax, %ecx
movl $0x258, %eax            # imm = 0x258
xorl %edx, %edx
idivl %ecx
cmpl $0x1f, %eax
cmovll %r15d, %eax
movq 0x6e21(%rip), %rcx      # 0xdc10
subq %rax, %rcx
cmpq %rcx, 0x20(%r14)
jge 0x6f03
leaq 0x4(%r14), %r12
movq %r12, %rdi
callq 0x77c5
movl %eax, %r13d
movq 0xb8(%r14), %rbx
pushq $-0x1
popq %rax
testq %rbx, %rbx
je 0x6e23
leaq 0x4(%rbx), %rdi
callq 0x77c5
cmpl %eax, %r13d
cmovgl %r13d, %eax
cmpl $0x9f, %eax
jl 0x6e46
movl 0x10(%r12), %eax
movl %eax, 0x20(%rsp)
movups (%r12), %xmm0
movaps %xmm0, 0x10(%rsp)
jmp 0x6eab
incl %eax
cltd
idivl %ebp
movl %edx, %ebx
movslq %eax, %r13
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2200
movl $0xff00, %r15d          # imm = 0xFF00
movl %ebx, %ecx
shrl %cl, %r15d
andb (%r12,%r13), %r15b
callq 0x2230
movl $0xff, %edx
movl %ebx, %ecx
shrl %cl, %edx
andl %edx, %eax
orb %r15b, %al
movb %al, 0x10(%rsp,%r13)
cmpq $0x12, %r13
jg 0x6ea0
leaq 0x1(%r13), %rbx
callq 0x2230
movb %al, 0x11(%rsp,%r13)
movq %rbx, %r13
jmp 0x6e87
movq 0xb8(%r14), %rbx
pushq $0x1e
popq %r15
movq %r14, %r12
testq %rbx, %rbx
je 0x6ed0
cmpl $0x0, 0x18(%r14)
movq %rbx, %r12
je 0x6ed0
callq 0x2230
movq %r14, %r12
testb $0x7, %al
jne 0x6ed0
movq 0xb8(%r14), %r12
cmpl $0x0, 0x18(%r12)
je 0x6ee1
callq 0x2230
testb $0x7, %al
jne 0x6ef6
movq %r14, %rdi
callq 0x348a
testq %rax, %rax
je 0x6ef6
cmpl $0x0, 0x18(%rax)
cmovgq %rax, %r12
movq %r12, %rdi
callq 0x782a
testq %rax, %rax
jne 0x6f16
movq 0xb8(%r14), %r14
jmp 0x6dc6
xorl %eax, %eax
jmp 0x6fea
movq %rax, %r13
cmpl $0x0, 0x52a8(%rip)      # 0xc1c8
pushq $-0x1
popq %rbp
js 0x6f76
cmpl $0x0, 0x52a0(%rip)      # 0xc1cc
movl 0xc(%rsp), %ebx
js 0x6f7a
xorl %eax, %eax
cmpl $0x2, %ebx
sete %al
leal 0x2(,%rax,8), %esi
leaq 0x10(%rsp), %rdi
callq 0x329c
testq %rax, %rax
je 0x6f5d
movl 0x18(%rax), %ecx
cmpl 0x1c(%rax), %ecx
jge 0x6f5d
pushq $0x3
popq %rbp
jmp 0x6f7a
callq 0x2230
pushq $0x25
popq %rcx
cqto
idivq %rcx
xorl %ebp, %ebp
negq %rdx
sbbl %ebp, %ebp
orl $0x3, %ebp
jmp 0x6f7a
movl 0xc(%rsp), %ebx
cmpl $0xa, %ebx
leaq 0x15af(%rip), %rax      # 0x8533
leaq 0x138d(%rip), %rsi      # 0x8318
cmoveq %rax, %rsi
leaq 0x196f(%rip), %rdi      # 0x8905
xorl %eax, %eax
callq 0x3324
leaq 0x24(%rsp), %rdx
movl $0x6e66, (%rdx)         # imm = 0x6E66
movq %r13, %rdi
addq $0x18, %rdi
movl 0x98(%r13), %esi
movq 0x6c53(%rip), %rax      # 0xdc10
addq $-0xf, %rax
xorl %r9d, %r9d
cmpq %rax, 0xa8(%r13)
setge %r9b
leaq 0x10(%rsp), %rcx
movl %ebp, %r8d
callq 0x7864
movq %r13, %rdi
movq %r12, %rsi
callq 0x74d5
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq