// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
xorl %eax, %eax
cmpl $0x2, %edi
sete %al
leaq 0x650f(%rip), %rdx      # 0xdc50
leaq 0x6510(%rip), %rcx      # 0xdc58
cmoveq %rdx, %rcx
movq (%rcx), %r12
leal 0x2(,%rax,8), %eax
movl %eax, 0x4(%rsp)
cmpl $0xa, %edi
leaq 0x1ddf(%rip), %rax      # 0x9543
leaq 0x1bbd(%rip), %rcx      # 0x9328
cmoveq %rax, %rcx
movq %rcx, 0x30(%rsp)
movl $0x1e, %esi
testq %r12, %r12
je 0x7a49
movl 0x1c(%r12), %eax
leal 0x7(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x3, %ecx
movl $0x258, %eax            # imm = 0x258
xorl %edx, %edx
idivl %ecx
cmpl $0x1f, %eax
cmovll %esi, %eax
movq 0x6468(%rip), %rcx      # 0xdc10
subq %rax, %rcx
cmpq %rcx, 0x20(%r12)
jge 0x7a30
leaq 0x4(%r12), %r15
movq %r15, %rdi
callq 0x8330
movl %eax, %ebx
movq 0xb8(%r12), %rdi
testq %rdi, %rdi
je 0x77e0
addq $0x4, %rdi
callq 0x8330
movl %eax, %r13d
jmp 0x77e6
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl %r13d, %ebx
cmovgl %ebx, %r13d
cmpl $0x9f, %r13d
jl 0x780c
movl 0x10(%r15), %eax
movl %eax, 0x20(%rsp)
movups (%r15), %xmm0
movaps %xmm0, 0x10(%rsp)
jmp 0x7898
leal 0x1(%r13), %eax
leal 0x8(%r13), %ebp
testl %eax, %eax
cmovnsl %eax, %ebp
movl %ebp, %r14d
sarl $0x3, %r14d
movslq %r14d, %rbx
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2200
andl $-0x8, %ebp
negl %ebp
leal 0x1(%r13,%rbp), %ecx
movl %ecx, 0x8(%rsp)
movl $0xff00, %ebp           # imm = 0xFF00
shrl %cl, %ebp
andb (%r15,%rbx), %bpl
movb %bpl, 0x10(%rsp,%rbx)
callq 0x2230
movl $0xff, %edx
movl 0x8(%rsp), %ecx
shrl %cl, %edx
andl %edx, %eax
orb %bpl, %al
movb %al, 0x10(%rsp,%rbx)
cmpl $0x96, %r13d
jg 0x7898
cmpl $0x13, %r14d
movl $0x12, %eax
cmovll %eax, %r14d
incl %r14d
leaq 0x1(%rbx), %r15
callq 0x2230
movb %al, 0x11(%rsp,%rbx)
movq %r15, %rbx
cmpq %r15, %r14
jne 0x7883
cmpq $0x0, 0xb8(%r12)
movq %r12, %rbp
je 0x78c2
cmpl $0x0, 0x18(%r12)
je 0x78ba
callq 0x2230
movq %r12, %rbp
testb $0x7, %al
jne 0x78c2
movq 0xb8(%r12), %rbp
cmpl $0x0, 0x18(%rbp)
je 0x78d1
callq 0x2230
testb $0x7, %al
jne 0x7915
cmpl $0x2, (%r12)
leaq 0x637b(%rip), %rax      # 0xdc58
leaq 0x636c(%rip), %rcx      # 0xdc50
cmoveq %rcx, %rax
movq (%rax), %rcx
cmpq %r12, %rcx
je 0x7906
movq %rcx, %rax
movq 0xb8(%rcx), %rcx
testq %rcx, %rcx
je 0x7906
cmpq %r12, %rcx
jne 0x78f0
jmp 0x7908
xorl %eax, %eax
testq %rax, %rax
je 0x7915
cmpl $0x0, 0x18(%rax)
cmovgq %rax, %rbp
testq %rbp, %rbp
je 0x7a2b
movq %rbp, %rdi
callq 0x8373
testq %rax, %rax
je 0x7a2b
movq %rax, %r13
cmpl $0x0, 0x488f(%rip)      # 0xc1c8
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
js 0x79a2
cmpl $0x0, 0x4885(%rip)      # 0xc1cc
js 0x79a2
leaq 0x10(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x35d4
testq %rax, %rax
je 0x7969
movl 0x18(%rax), %ecx
movl $0x3, %ebx
cmpl 0x1c(%rax), %ecx
jl 0x79a2
callq 0x2230
movabsq $0x14c1bacf914c1bad, %rcx # imm = 0x14C1BACF914C1BAD
imulq %rcx, %rax
movabsq $0x3759f22983759f2, %rcx # imm = 0x3759F22983759F2
addq %rcx, %rax
xorl %ecx, %ecx
movabsq $0x6eb3e45306eb3e4, %rdx # imm = 0x6EB3E45306EB3E4
cmpq %rdx, %rax
setbe %cl
leal -0x1(,%rcx,4), %ebx
leaq 0x1f6c(%rip), %rdi      # 0x9915
movq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x366b
movl $0x6e66, 0xc(%rsp)      # imm = 0x6E66
movq %r13, %rdi
addq $0x18, %rdi
movl 0x98(%r13), %esi
movq 0x623e(%rip), %rax      # 0xdc10
addq $-0xf, %rax
xorl %r9d, %r9d
cmpq %rax, 0xa8(%r13)
setge %r9b
leaq 0xc(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl %ebx, %r8d
callq 0x83bd
movl 0xb8(%r13), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0xb8(%r13)
movq 0x6202(%rip), %rcx      # 0xdc10
movq %rcx, 0xb0(%r13)
cmpl $0x2, %eax
jl 0x7a22
movq %rbp, %rdi
callq 0x80ff
xorl %eax, %eax
movl $0x1e, %esi
jmp 0x7a3a
movl $0x1e, %esi
movq 0xb8(%r12), %r12
movb $0x1, %al
testb %al, %al
jne 0x7779
movl $0x1, %eax
jmp 0x7a4b
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq