// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0x8(%rbp)
cmpl $0x2, -0x8(%rbp)
jne 0x9071
movq 0x7be5(%rip), %rax      # 0x10c50
movq %rax, -0x70(%rbp)
jmp 0x907c
movq 0x7be0(%rip), %rax      # 0x10c58
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x92f7
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl $0x258, %eax            # imm = 0x258
cltd
idivl %ecx
cmpl $0x1e, %eax
jl 0x90cb
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl $0x258, %eax            # imm = 0x258
cltd
idivl %ecx
movl %eax, -0x74(%rbp)
jmp 0x90d5
movl $0x1e, %eax
movl %eax, -0x74(%rbp)
jmp 0x90d5
movl -0x74(%rbp), %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x7b26(%rip), %rcx      # 0x10c10
movslq -0x14(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jge 0x92e3
movq -0x10(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xadf0
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jge 0x912a
movq -0x10(%rbp), %rax
movq 0x4(%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0xc(%rax), %rcx
movq %rcx, -0x38(%rbp)
movl 0x14(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0xb8(%rax)
je 0x9168
movq -0x20(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x9159
callq 0x2230
andq $0x7, %rax
cmpq $0x0, %rax
jne 0x9168
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x9181
callq 0x2230
andq $0x7, %rax
cmpq $0x0, %rax
jne 0x91a9
movq -0x10(%rbp), %rdi
callq 0x3de0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x91a7
movq -0x58(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jle 0x91a7
movq -0x58(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x91a9
cmpq $0x0, -0x20(%rbp)
je 0x92e1
movq -0x20(%rbp), %rdi
callq 0xafb0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x92df
movl $0xffffffff, -0x60(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, 0x5fee(%rip)      # 0xf1c8
jl 0x9245
cmpl $0x0, 0x5fe9(%rip)      # 0xf1cc
jl 0x9245
leaq -0x40(%rbp), %rdi
movl -0x8(%rbp), %ecx
movl $0x2, %esi
movl $0xa, %eax
cmpl $0x2, %ecx
cmovel %eax, %esi
callq 0x39a0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x9225
movq -0x68(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x68(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jge 0x9225
movl $0x3, -0x60(%rbp)
jmp 0x9243
callq 0x2230
movl $0x25, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0x9241
movl $0x3, -0x60(%rbp)
jmp 0x9243
jmp 0x9245
movl -0x8(%rbp), %ecx
leaq 0x30cf(%rip), %rsi      # 0xc31e
leaq 0x32ef(%rip), %rax      # 0xc545
cmpl $0xa, %ecx
cmoveq %rax, %rsi
leaq 0x36b3(%rip), %rdi      # 0xc917
movb $0x0, %al
callq 0x3aa0
leaq -0x5c(%rbp), %rdi
leaq 0x317b(%rip), %rsi      # 0xc3f1
xorl %edx, %edx
callq 0x9990
movq -0x48(%rbp), %rdi
addq $0x18, %rdi
movq -0x48(%rbp), %rax
movl 0x98(%rax), %esi
leaq -0x5c(%rbp), %rdx
leaq -0x40(%rbp), %r8
movl -0x60(%rbp), %r9d
movq -0x48(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x7963(%rip), %rcx      # 0x10c10
subq $0xf, %rcx
cmpq %rcx, %rax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl $0x4, %ecx
movl %eax, (%rsp)
callq 0xb040
movq -0x48(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xa090
movl $0x1, -0x4(%rbp)
jmp 0x92fe
jmp 0x92e1
jmp 0x92e3
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rax
movq %rax, -0x10(%rbp)
jmp 0x9084
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)