// File: /workspace/github_c_makefile_star2_O3/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, (%rsp)
cmpl $0x2, %edi
leaq 0x6594(%rip), %rcx      # 0xdc50
leaq 0x6595(%rip), %rax      # 0xdc58
cmoveq %rcx, %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x790e
movl $0x1e, %ebp
movl 0x1c(%rbx), %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 %ebp, %eax
movq 0x6514(%rip), %rcx      # 0xdc10
subq %rax, %rcx
cmpq %rcx, 0x20(%rbx)
jge 0x78fe
leaq 0x4(%rbx), %r15
movl $0x13, %eax
movl $0xffffff68, %edx       # imm = 0xFFFFFF68
movl %eax, %ecx
movzbl (%r15,%rcx), %esi
testl %esi, %esi
jne 0x7731
addl $0x8, %edx
addl $-0x1, %eax
jb 0x7717
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x774d
movl $0x7, %eax
movl %esi, %edi
movl %eax, %ecx
shll %cl, %edi
testb %dil, %dil
js 0x774b
addl $-0x1, %eax
jb 0x7736
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl %edx, %eax
movq 0xb8(%rbx), %rcx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x779f
movl $0x13, %edi
movl $0xffffff68, %edx       # imm = 0xFFFFFF68
movl %edi, %esi
movzbl 0x4(%rcx,%rsi), %esi
testl %esi, %esi
jne 0x777e
addl $0x8, %edx
addl $-0x1, %edi
jb 0x7769
jmp 0x779f
movl $0x7, %r12d
movl %esi, %edi
movl %r12d, %ecx
shll %cl, %edi
testb %dil, %dil
js 0x779c
addl $-0x1, %r12d
jb 0x7784
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
subl %edx, %r12d
cmpl %r12d, %eax
cmovgl %eax, %r12d
cmpl $0x9f, %r12d
jl 0x77c5
movl 0x10(%r15), %eax
movl %eax, 0x20(%rsp)
movups (%r15), %xmm0
movaps %xmm0, 0x10(%rsp)
jmp 0x7871
leal 0x1(%r12), %r14d
leal 0x8(%r12), %ebp
testl %r14d, %r14d
cmovnsl %r14d, %ebp
movl %ebp, %eax
sarl $0x3, %eax
movl %eax, 0x4(%rsp)
movslq %eax, %rdx
movq %rdx, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x2200
andl $-0x8, %ebp
negl %ebp
addl %r12d, %ebp
incl %ebp
movl $0xff00, %r13d          # imm = 0xFF00
movl %ebp, %ecx
shrl %cl, %r13d
movq 0x8(%rsp), %rax
andb (%r15,%rax), %r13b
callq 0x2230
movl $0xff, %edx
movl %ebp, %ecx
movl $0x1e, %ebp
shrl %cl, %edx
andl %edx, %eax
orb %r13b, %al
movq 0x8(%rsp), %rcx
movb %al, 0x10(%rsp,%rcx)
cmpl $0x96, %r12d
jg 0x7871
shrl $0x3, %r14d
movl 0x4(%rsp), %edx
cmpl $0x13, %edx
movl $0x12, %eax
cmovgel %edx, %eax
movl %r14d, %ecx
subl %edx, %ecx
leal (%rcx,%rax), %r15d
incl %r15d
leaq 0x1(%r14), %r12
callq 0x2230
movb %al, 0x11(%rsp,%r14)
movq %r12, %r14
cmpq %r12, %r15
jne 0x785b
movq 0xb8(%rbx), %rax
movq %rbx, %r14
testq %rax, %rax
je 0x789c
cmpl $0x0, 0x18(%rbx)
movq %rax, %r14
je 0x789c
callq 0x2230
movq %rbx, %r14
testb $0x7, %al
jne 0x789c
movq 0xb8(%rbx), %r14
cmpl $0x0, 0x18(%r14)
je 0x78ac
callq 0x2230
testb $0x7, %al
jne 0x78ee
cmpl $0x2, (%rbx)
leaq 0x63a2(%rip), %rax      # 0xdc58
leaq 0x6393(%rip), %rcx      # 0xdc50
cmoveq %rcx, %rax
movq (%rax), %rax
cmpq %rbx, %rax
je 0x78ee
movq %rax, %r15
movq 0xb8(%rax), %rax
testq %rax, %rax
je 0x78e4
cmpq %rbx, %rax
jne 0x78c9
cmpl $0x0, 0x18(%r15)
jg 0x78f1
movq %r14, %r15
testq %r14, %r14
jne 0x78f1
jmp 0x78fe
movq %r14, %r15
movq %r15, %rdi
callq 0x8385
testq %rax, %rax
jne 0x791f
movq 0xb8(%rbx), %rbx
testq %rbx, %rbx
jne 0x76d8
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
cmpl $0x0, 0x489f(%rip)      # 0xc1c8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
js 0x79a2
cmpl $0x0, 0x4895(%rip)      # 0xc1cc
movl (%rsp), %ebx
js 0x79a5
xorl %eax, %eax
cmpl $0x2, %ebx
sete %al
leal 0x2(,%rax,8), %esi
leaq 0x10(%rsp), %rdi
callq 0x3593
testq %rax, %rax
je 0x7967
movl 0x18(%rax), %ecx
movl $0x3, %ebp
cmpl 0x1c(%rax), %ecx
jl 0x79a5
callq 0x2230
movabsq $0x14c1bacf914c1bad, %rcx # imm = 0x14C1BACF914C1BAD
imulq %rax, %rcx
movabsq $0x3759f22983759f2, %rax # imm = 0x3759F22983759F2
addq %rcx, %rax
movabsq $0x6eb3e45306eb3e4, %rcx # imm = 0x6EB3E45306EB3E4
xorl %edx, %edx
cmpq %rcx, %rax
setbe %dl
leal -0x1(,%rdx,4), %ebp
jmp 0x79a5
movl (%rsp), %ebx
cmpl $0xa, %ebx
leaq 0x1b84(%rip), %rax      # 0x9533
leaq 0x1962(%rip), %rsi      # 0x9318
cmoveq %rax, %rsi
leaq 0x1f44(%rip), %rdi      # 0x9905
xorl %eax, %eax
callq 0x362c
leaq 0x24(%rsp), %rdx
movl $0x6e66, (%rdx)         # imm = 0x6E66
movq %r12, %rdi
addq $0x18, %rdi
movl 0x98(%r12), %esi
movq 0x6227(%rip), %rax      # 0xdc10
addq $-0xf, %rax
xorl %r9d, %r9d
cmpq %rax, 0xa8(%r12)
setge %r9b
leaq 0x10(%rsp), %rcx
movl %ebp, %r8d
callq 0x83cf
movl 0xb8(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0xb8(%r12)
movq 0x61ed(%rip), %rcx      # 0xdc10
movq %rcx, 0xb0(%r12)
cmpl $0x2, %eax
jl 0x7a38
movq %r15, %rdi
callq 0x80e9
movl $0x1, %eax
jmp 0x7910