// File: /workspace/github_c_makefile_star2_O0/jech[P]dht/dht.c
// Function: new_node
static struct node *
new_node(const unsigned char *id, const struct sockaddr *sa, int salen,
         int confirm)
{
    struct bucket *b;
    struct node *n;
    int mybucket;

 again:

    b = find_bucket(id, sa->sa_family);
    if(b == NULL)
        return NULL;

    if(id_cmp(id, myid) == 0)
        return NULL;

    if(is_martian(sa) || node_blacklisted(sa, salen))
        return NULL;

    mybucket = in_bucket(myid, b);

    if(confirm == 2)
        b->time = now.tv_sec;

    n = b->nodes;
    while(n) {
        if(id_cmp(n->id, id) == 0) {
            if(confirm || n->time < now.tv_sec - 15 * 60) {
                /* Known node.  Update stuff. */
                memcpy((struct sockaddr*)&n->ss, sa, salen);
                if(confirm)
                    n->time = now.tv_sec;
                if(confirm >= 2) {
                    n->reply_time = now.tv_sec;
                    n->pinged = 0;
                    n->pinged_time = 0;
                }
            }
            if(confirm == 2)
                add_search_node(id, sa, salen);
            return n;
        }
        n = n->next;
    }

    /* New node. */

    if(mybucket) {
        if(sa->sa_family == AF_INET)
            mybucket_grow_time = now.tv_sec;
        else
            mybucket6_grow_time = now.tv_sec;
    }

    /* First, try to get rid of a known-bad node. */
    n = b->nodes;
    while(n) {
        if(n->pinged >= 3 && n->pinged_time < now.tv_sec - 15) {
            memcpy(n->id, id, 20);
            memcpy((struct sockaddr*)&n->ss, sa, salen);
            n->time = confirm ? now.tv_sec : 0;
            n->reply_time = confirm >= 2 ? now.tv_sec : 0;
            n->pinged_time = 0;
            n->pinged = 0;
            if(confirm == 2)
                add_search_node(id, sa, salen);
            return n;
        }
        n = n->next;
    }

    if(b->count >= b->max_count) {
        /* Bucket full.  Ping a dubious node */
        int dubious = 0;
        n = b->nodes;
        while(n) {
            /* Pick the first dubious node that we haven't pinged in the
               last 15 seconds.  This gives nodes the time to reply, but
               tends to concentrate on the same nodes, so that we get rid
               of bad nodes fast. */
            if(!node_good(n)) {
                dubious = 1;
                if(n->pinged_time < now.tv_sec - 15) {
                    unsigned char tid[4];
                    debugf("Sending ping to dubious node.\n");
                    make_tid(tid, "pn", 0);
                    send_ping((struct sockaddr*)&n->ss, n->sslen,
                              tid, 4);
                    n->pinged++;
                    n->pinged_time = now.tv_sec;
                    break;
                }
            }
            n = n->next;
        }

        if(mybucket && !dubious) {
            int rc;
            rc = split_bucket(b);
            if(rc > 0)
                goto again;
            return NULL;
        }

        /* No space for this node.  Cache it away for later. */
        if(confirm || b->cached.ss_family == 0) {
            memcpy(&b->cached, sa, salen);
            b->cachedlen = salen;
        }

        if(confirm == 2)
            add_search_node(id, sa, salen);
        return NULL;
    }

    /* Create a new node. */
    n = calloc(1, sizeof(struct node));
    if(n == NULL)
        return NULL;
    memcpy(n->id, id, 20);
    memcpy(&n->ss, sa, salen);
    n->sslen = salen;
    n->time = confirm ? now.tv_sec : 0;
    n->reply_time = confirm >= 2 ? now.tv_sec : 0;
    n->next = b->nodes;
    b->nodes = n;
    b->count++;
    if(confirm == 2)
        add_search_node(id, sa, salen);
    return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movzwl (%rax), %esi
callq 0x39a0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x74ae
movq $0x0, -0x8(%rbp)
jmp 0x79d0
movq -0x10(%rbp), %rdi
leaq 0x9777(%rip), %rsi      # 0x10c30
callq 0x3b70
cmpl $0x0, %eax
jne 0x74d0
movq $0x0, -0x8(%rbp)
jmp 0x79d0
movq -0x18(%rbp), %rdi
callq 0x64c0
cmpl $0x0, %eax
jne 0x74ef
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x6640
cmpl $0x0, %eax
je 0x74fc
movq $0x0, -0x8(%rbp)
jmp 0x79d0
movq -0x28(%rbp), %rsi
leaq 0x9729(%rip), %rdi      # 0x10c30
callq 0x9840
movl %eax, -0x34(%rbp)
cmpl $0x2, -0x20(%rbp)
jne 0x7524
movq 0x96f4(%rip), %rcx      # 0x10c10
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x7610
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3b70
cmpl $0x0, %eax
jne 0x75fc
cmpl $0x0, -0x20(%rbp)
jne 0x7575
movq -0x30(%rbp), %rax
movq 0xa0(%rax), %rax
movq 0x96a7(%rip), %rcx      # 0x10c10
subq $0x384, %rcx            # imm = 0x384
cmpq %rcx, %rax
jge 0x75d9
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x2200
cmpl $0x0, -0x20(%rbp)
je 0x75a2
movq 0x9679(%rip), %rcx      # 0x10c10
movq -0x30(%rbp), %rax
movq %rcx, 0xa0(%rax)
cmpl $0x2, -0x20(%rbp)
jl 0x75d7
movq 0x9661(%rip), %rcx      # 0x10c10
movq -0x30(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0xb8(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0xb0(%rax)
jmp 0x75d9
cmpl $0x2, -0x20(%rbp)
jne 0x75ef
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xa280
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x79d0
movq -0x30(%rbp), %rax
movq 0xc0(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x7530
cmpl $0x0, -0x34(%rbp)
je 0x7642
movq -0x18(%rbp), %rax
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0x7632
movq 0x95e7(%rip), %rax      # 0x10c10
movq %rax, 0x9650(%rip)      # 0x10c80
jmp 0x7640
movq 0x95d7(%rip), %rax      # 0x10c10
movq %rax, 0x9648(%rip)      # 0x10c88
jmp 0x7642
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x7762
movq -0x30(%rbp), %rax
cmpl $0x3, 0xb8(%rax)
jl 0x774e
movq -0x30(%rbp), %rax
movq 0xb0(%rax), %rax
movq 0x9594(%rip), %rcx      # 0x10c10
subq $0xf, %rcx
cmpq %rcx, %rax
jge 0x774e
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movl 0x10(%rcx), %ecx
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x2200
cmpl $0x0, -0x20(%rbp)
je 0x76cd
movq 0x9549(%rip), %rax      # 0x10c10
movq %rax, -0x48(%rbp)
jmp 0x76d5
xorl %eax, %eax
movq %rax, -0x48(%rbp)
jmp 0x76d5
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0xa0(%rax)
cmpl $0x2, -0x20(%rbp)
jl 0x76f7
movq 0x951f(%rip), %rax      # 0x10c10
movq %rax, -0x50(%rbp)
jmp 0x76ff
xorl %eax, %eax
movq %rax, -0x50(%rbp)
jmp 0x76ff
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0xb0(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0xb8(%rax)
cmpl $0x2, -0x20(%rbp)
jne 0x7741
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xa280
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x79d0
movq -0x30(%rbp), %rax
movq 0xc0(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x764e
movq -0x28(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jl 0x78cd
movl $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x7845
movq -0x30(%rbp), %rdi
callq 0x4330
cmpl $0x0, %eax
jne 0x7831
movl $0x1, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0xb0(%rax), %rax
movq 0x9451(%rip), %rcx      # 0x10c10
subq $0xf, %rcx
cmpq %rcx, %rax
jge 0x782f
leaq 0x4ff3(%rip), %rdi      # 0xc7c2
movb $0x0, %al
callq 0x3aa0
leaq -0x3c(%rbp), %rdi
leaq 0x4c06(%rip), %rsi      # 0xc3e7
xorl %edx, %edx
callq 0x9990
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0x30(%rbp), %rax
movl 0x98(%rax), %esi
leaq -0x3c(%rbp), %rdx
movl $0x4, %ecx
callq 0x99e0
movq -0x30(%rbp), %rax
movl 0xb8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xb8(%rax)
movq 0x93ee(%rip), %rcx      # 0x10c10
movq -0x30(%rbp), %rax
movq %rcx, 0xb0(%rax)
jmp 0x7845
jmp 0x7831
movq -0x30(%rbp), %rax
movq 0xc0(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x7789
cmpl $0x0, -0x34(%rbp)
je 0x7875
cmpl $0x0, -0x38(%rbp)
jne 0x7875
movq -0x28(%rbp), %rdi
callq 0xa320
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jle 0x7868
jmp 0x7486
movq $0x0, -0x8(%rbp)
jmp 0x79d0
cmpl $0x0, -0x20(%rbp)
jne 0x7888
movq -0x28(%rbp), %rax
movzwl 0x30(%rax), %eax
cmpl $0x0, %eax
jne 0x78aa
movq -0x28(%rbp), %rdi
addq $0x30, %rdi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x2200
movl -0x1c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xb0(%rax)
cmpl $0x2, -0x20(%rbp)
jne 0x78c0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xa280
movq $0x0, -0x8(%rbp)
jmp 0x79d0
movl $0x1, %edi
movl $0xc8, %esi
callq 0x21a0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x78f4
movq $0x0, -0x8(%rbp)
jmp 0x79d0
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movl 0x10(%rcx), %ecx
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x2200
movl -0x1c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x98(%rax)
cmpl $0x0, -0x20(%rbp)
je 0x7945
movq 0x92d1(%rip), %rax      # 0x10c10
movq %rax, -0x58(%rbp)
jmp 0x794d
xorl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0x794d
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0xa0(%rax)
cmpl $0x2, -0x20(%rbp)
jl 0x796f
movq 0x92a7(%rip), %rax      # 0x10c10
movq %rax, -0x60(%rbp)
jmp 0x7977
xorl %eax, %eax
movq %rax, -0x60(%rbp)
jmp 0x7977
movq -0x60(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0xc0(%rax)
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x18(%rax)
cmpl $0x2, -0x20(%rbp)
jne 0x79c8
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xa280
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)