// File: /workspace/github_c_makefile_star2_O3/jech[P]dht/dht.c
// Function: dht_search
int
dht_search(const unsigned char *id, int port, int af,
           dht_callback_t *callback, void *closure)
{
    struct search *sr;
    struct storage *st;
    struct bucket *b = find_bucket(id, af);

    if(b == NULL) {
        errno = EAFNOSUPPORT;
        return -1;
    }

    /* Try to answer this search locally.  In a fully grown DHT this
       is very unlikely, but people are running modified versions of
       this code in private DHTs with very few nodes.  What's wrong
       with flooding? */
    if(callback) {
        st = find_storage(id);
        if(st) {
            unsigned short swapped;
            unsigned char buf[18];
            int i;

            debugf("Found local data (%d peers).\n", st->numpeers);

            for(i = 0; i < st->numpeers; i++) {
                swapped = htons(st->peers[i].port);
                if(st->peers[i].len == 4) {
                    memcpy(buf, st->peers[i].ip, 4);
                    memcpy(buf + 4, &swapped, 2);
                    (*callback)(closure, DHT_EVENT_VALUES, id,
                                (void*)buf, 6);
                } else if(st->peers[i].len == 16) {
                    memcpy(buf, st->peers[i].ip, 16);
                    memcpy(buf + 16, &swapped, 2);
                    (*callback)(closure, DHT_EVENT_VALUES6, id,
                                (void*)buf, 18);
                }
            }
        }
    }

    sr = searches;
    while(sr) {
        if(sr->af == af && id_cmp(sr->id, id) == 0)
            break;
        sr = sr->next;
    }

    int sr_duplicate = sr && !sr->done;

    if(sr) {
        /* We're reusing data from an old search.  Reusing the same tid
           means that we can merge replies for both searches. */
        int i;
        sr->done = 0;
    again:
        for(i = 0; i < sr->numnodes; i++) {
            struct search_node *n;
            n = &sr->nodes[i];
            /* Discard any doubtful nodes. */
            if(n->pinged >= 3 || n->reply_time < now.tv_sec - 7200) {
                flush_search_node(n, sr);
                goto again;
            }
            n->pinged = 0;
            n->token_len = 0;
            n->replied = 0;
            n->acked = 0;
        }
    } else {
        sr = new_search();
        if(sr == NULL) {
            errno = ENOSPC;
            return -1;
        }
        sr->af = af;
        sr->tid = search_id++;
        sr->step_time = 0;
        memcpy(sr->id, id, 20);
        sr->done = 0;
        sr->numnodes = 0;
    }

    sr->port = port;

    insert_search_bucket(b, sr);

    if(sr->numnodes < SEARCH_NODES) {
        struct bucket *p = previous_bucket(b);
        if(b->next)
            insert_search_bucket(b->next, sr);
        if(p)
            insert_search_bucket(p, sr);
    }
    if(sr->numnodes < SEARCH_NODES)
        insert_search_bucket(find_bucket(myid, af), sr);

    search_step(sr, callback, closure);
    search_time = now.tv_sec;
    if(sr_duplicate) {
        return 0;
    } else {
        return 1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x10(%rsp)
movq %rcx, %r15
movl %edx, %ebp
movl %esi, %ebx
movq %rdi, %r12
movl %edx, %esi
callq 0x3593
testq %rax, %rax
je 0x33a1
movq %rax, %r14
movl %ebx, 0x1c(%rsp)
testq %r15, %r15
movq %rax, (%rsp)
je 0x329c
movq %r12, %rdi
callq 0x35ea
testq %rax, %rax
je 0x329c
movq %rax, %r13
movq %r15, %rbx
movl 0x14(%rax), %esi
leaq 0x605e(%rip), %rdi      # 0x9260
xorl %eax, %eax
callq 0x362c
cmpl $0x0, 0x14(%r13)
jle 0x3295
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x20(%r13), %rcx
movzwl 0x12(%rcx,%r14), %eax
rolw $0x8, %ax
movzwl 0x10(%rcx,%r14), %edx
cmpl $0x10, %edx
je 0x325a
cmpl $0x4, %edx
jne 0x3285
movl (%rcx,%r14), %ecx
movl %ecx, 0x20(%rsp)
movw %ax, 0x24(%rsp)
movl $0x6, %r8d
movq 0x10(%rsp), %rdi
movl $0x1, %esi
jmp 0x327b
movdqu (%rcx,%r14), %xmm0
movdqa %xmm0, 0x20(%rsp)
movw %ax, 0x30(%rsp)
movl $0x12, %r8d
movq 0x10(%rsp), %rdi
movl $0x2, %esi
movq %r12, %rdx
leaq 0x20(%rsp), %rcx
callq *%rbx
incq %r15
movslq 0x14(%r13), %rax
addq $0x20, %r14
cmpq %rax, %r15
jl 0x321d
movq %rbx, %r15
movq (%rsp), %r14
movq 0xa965(%rip), %rsi      # 0xdc08
movb $0x1, %dl
testq %rsi, %rsi
je 0x3343
movq %rsi, %r13
cmpl %ebp, 0x4(%r13)
jne 0x32eb
movdqu 0x10(%r13), %xmm0
movdqu (%r12), %xmm1
pcmpeqb %xmm0, %xmm1
movd 0x20(%r13), %xmm0
movd 0x10(%r12), %xmm2
pcmpeqb %xmm0, %xmm2
pand %xmm1, %xmm2
pmovmskb %xmm2, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x341d
movq 0xce8(%r13), %r13
testq %r13, %r13
jne 0x32b1
testq %rsi, %rsi
je 0x3343
xorl %r13d, %r13d
movq %rsi, %rax
cmpl $0x0, 0x28(%rax)
je 0x331a
testq %r13, %r13
je 0x3317
movq 0x8(%r13), %rcx
cmpq 0x8(%rax), %rcx
jle 0x331a
movq %rax, %r13
movq 0xce8(%rax), %rax
testq %rax, %rax
jne 0x3302
testq %r13, %r13
je 0x3343
movq $-0xe88, %rax           # imm = 0xF178
addq 0xa8d7(%rip), %rax      # 0xdc10
cmpq %rax, 0x8(%r13)
jl 0x33bd
xorl %edx, %edx
jmp 0x3346
xorl %r13d, %r13d
movl 0xa91b(%rip), %r14d     # 0xdc68
cmpl $0x3ff, %r14d           # imm = 0x3FF
jg 0x33ae
movl $0x1, %edi
movq %rsi, 0x8(%rsp)
movl $0xcf0, %esi            # imm = 0xCF0
movq %r15, %rbx
movl %edx, %r15d
callq 0x21a0
movq 0x8(%rsp), %rcx
movl %r15d, %edx
movq %rbx, %r15
testq %rax, %rax
je 0x33ae
movq %rcx, 0xce8(%rax)
movq %rax, 0xa87a(%rip)      # 0xdc08
incl %r14d
movl %r14d, 0xa8d0(%rip)     # 0xdc68
movq %rax, %r13
movq (%rsp), %r14
jmp 0x33bd
callq 0x2060
movl $0x61, (%rax)
jmp 0x3413
testb %dl, %dl
movq (%rsp), %r14
jne 0x3408
cmpl $0x0, 0x28(%r13)
je 0x3408
movl %ebp, 0x4(%r13)
movzwl 0xa858(%rip), %eax      # 0xdc20
movl %eax, %ecx
incl %ecx
movw %cx, 0xa84d(%rip)       # 0xdc20
movw %ax, (%r13)
movq $0x0, 0x8(%r13)
movdqu (%r12), %xmm0
movdqu %xmm0, 0x10(%r13)
movl 0x10(%r12), %eax
movl %eax, 0x20(%r13)
xorl %r12d, %r12d
movl %r12d, 0x28(%r13)
movl %r12d, 0xce0(%r13)
jmp 0x34c3
callq 0x2060
movl $0x1c, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x3584
cmpl $0x0, 0x28(%r13)
sete %r12b
movl $0x0, 0x28(%r13)
movl 0xce0(%r13), %ecx
testl %ecx, %ecx
jle 0x34c3
movq %r15, 0x8(%rsp)
leaq 0x30(%r13), %rbx
movq $-0x1c20, %r14          # imm = 0xE3E0
xorl %r15d, %r15d
movq 0xa7b9(%rip), %rax      # 0xdc10
addq %r14, %rax
movl %ecx, %ecx
imulq $0xe8, %rcx, %rcx
xorl %edi, %edi
cmpl $0x2, 0xe0(%r13,%rdi)
jg 0x34a4
cmpq %rax, 0xd8(%r13,%rdi)
jl 0x34a4
movl %r15d, 0xe0(%r13,%rdi)
movq $0x0, 0x10c(%r13,%rdi)
movl %r15d, 0x114(%r13,%rdi)
addq $0xe8, %rdi
cmpq %rdi, %rcx
jne 0x3465
jmp 0x34ba
addq %rbx, %rdi
movq %r13, %rsi
callq 0x36da
movl 0xce0(%r13), %ecx
testl %ecx, %ecx
jg 0x3450
movq 0x8(%rsp), %r15
movq (%rsp), %r14
movl 0x1c(%rsp), %eax
movw %ax, 0x24(%r13)
movq %r14, %rdi
movq %r13, %rsi
callq 0x3755
cmpl $0xd, 0xce0(%r13)
jg 0x355e
cmpl $0x2, (%r14)
leaq 0xa764(%rip), %rax      # 0xdc50
leaq 0xa765(%rip), %rcx      # 0xdc58
cmoveq %rax, %rcx
movq (%rcx), %rax
cmpq %r14, %rax
je 0x3515
movq %rax, %rbx
movq 0xb8(%rax), %rax
testq %rax, %rax
je 0x3515
cmpq %r14, %rax
jne 0x34ff
jmp 0x3517
xorl %ebx, %ebx
movq 0xb8(%r14), %rdi
testq %rdi, %rdi
je 0x352b
movq %r13, %rsi
callq 0x3755
testq %rbx, %rbx
je 0x353b
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3755
cmpl $0xd, 0xce0(%r13)
jg 0x355e
leaq 0xa6e4(%rip), %rdi      # 0xdc30
movl %ebp, %esi
callq 0x3593
movq %rax, %rdi
movq %r13, %rsi
callq 0x3755
movq %r13, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
callq 0x379c
movq 0xa69b(%rip), %rax      # 0xdc10
movq %rax, 0xa6cc(%rip)      # 0xdc48
xorb $0x1, %r12b
movzbl %r12b, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq