// File: /workspace/github_c_makefile_star2_O2/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, (%rsp)
movq %rcx, %r14
movl %edx, %ebp
movl %esi, %ebx
movq %rdi, %r12
movl %edx, %esi
callq 0x329c
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x3015
movl %ebx, 0xc(%rsp)
testq %r14, %r14
je 0x3028
movq %r12, %rdi
callq 0x32ee
testq %rax, %rax
je 0x3028
movq %rax, %r15
movq %r14, %r13
movl 0x14(%rax), %esi
leaq 0x52cd(%rip), %rdi      # 0x8260
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x3324
pushq $0x8
popq %r14
movslq 0x14(%r15), %rax
cmpq %rax, %rbx
jge 0x3025
movq 0x20(%r15), %rcx
movzwl 0x12(%rcx,%r14), %eax
rolw $0x8, %ax
movzwl 0x10(%rcx,%r14), %edx
cmpl $0x10, %edx
je 0x2fe7
cmpl $0x4, %edx
jne 0x300c
movl (%rcx,%r14), %ecx
movl %ecx, 0x10(%rsp)
movw %ax, 0x14(%rsp)
movq (%rsp), %rdi
pushq $0x1
popq %rsi
movq %r12, %rdx
leaq 0x10(%rsp), %rcx
pushq $0x6
jmp 0x3007
movups (%rcx,%r14), %xmm0
movaps %xmm0, 0x10(%rsp)
movw %ax, 0x20(%rsp)
movq (%rsp), %rdi
pushq $0x2
popq %rsi
movq %r12, %rdx
leaq 0x10(%rsp), %rcx
pushq $0x12
popq %r8
callq *%r13
incq %rbx
addq $0x20, %r14
jmp 0x2fa0
callq 0x2060
movl $0x61, (%rax)
jmp 0x3286
movq %r13, %r14
pushq $0x1
popq %r15
leaq 0xabd5(%rip), %r13      # 0xdc08
movq (%r13), %r13
testq %r13, %r13
je 0x305f
cmpl %ebp, 0x4(%r13)
jne 0x3056
leaq 0x10(%r13), %rdi
movq %r12, %rsi
callq 0x33d2
testl %eax, %eax
je 0x30fb
addq $0xce8, %r13            # imm = 0xCE8
jmp 0x3033
leaq 0xaba2(%rip), %rax      # 0xdc08
xorl %r13d, %r13d
movq (%rax), %rax
testq %rax, %rax
je 0x3091
cmpl $0x0, 0x28(%rax)
je 0x3089
testq %r13, %r13
je 0x3086
movq 0x8(%r13), %rcx
cmpq 0x8(%rax), %rcx
jle 0x3089
movq %rax, %r13
addq $0xce8, %rax            # imm = 0xCE8
jmp 0x3069
testq %r13, %r13
je 0x30ae
movq $-0xe88, %rax           # imm = 0xF178
addq 0xab6c(%rip), %rax      # 0xdc10
cmpq %rax, 0x8(%r13)
jl 0x31a6
movl 0xabb4(%rip), %ebx      # 0xdc68
cmpl $0x3ff, %ebx            # imm = 0x3FF
jg 0x3192
pushq $0x1
popq %rdi
movl $0xcf0, %esi            # imm = 0xCF0
callq 0x21a0
testq %rax, %rax
je 0x3192
movq 0xab2b(%rip), %rcx      # 0xdc08
movq %rcx, 0xce8(%rax)
movq %rax, 0xab1d(%rip)      # 0xdc08
incl %ebx
movl %ebx, 0xab75(%rip)      # 0xdc68
movq %rax, %r13
jmp 0x31a6
movq %r14, 0x28(%rsp)
movl 0x28(%r13), %ebx
andl $0x0, 0x28(%r13)
leaq 0x30(%r13), %r14
movq $-0x1c20, %r15          # imm = 0xE3E0
xorl %r12d, %r12d
movl 0xce0(%r13), %ecx
movq 0xaaeb(%rip), %rax      # 0xdc10
addq %r15, %rax
testl %ecx, %ecx
cmovlel %r12d, %ecx
imulq $0xe8, %rcx, %rcx
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x3182
cmpl $0x2, 0xe0(%r13,%rdi)
jg 0x3175
cmpq %rax, 0xd8(%r13,%rdi)
jl 0x3175
andl $0x0, 0xe0(%r13,%rdi)
andq $0x0, 0x10c(%r13,%rdi)
andl $0x0, 0x114(%r13,%rdi)
addq $0xe8, %rdi
jmp 0x3137
addq %r14, %rdi
movq %r13, %rsi
callq 0x33da
jmp 0x3117
xorl %r15d, %r15d
testl %ebx, %ebx
setne %r15b
movq 0x28(%rsp), %r14
jmp 0x31e6
testq %r13, %r13
je 0x327b
cmpl $0x0, 0x28(%r13)
je 0x327b
movl %ebp, 0x4(%r13)
movzwl 0xaa6f(%rip), %eax      # 0xdc20
movl %eax, %ecx
incl %ecx
movw %cx, 0xaa64(%rip)       # 0xdc20
movw %ax, (%r13)
andq $0x0, 0x8(%r13)
movups (%r12), %xmm0
movups %xmm0, 0x10(%r13)
movl 0x10(%r12), %eax
movl %eax, 0x20(%r13)
andl $0x0, 0x28(%r13)
andl $0x0, 0xce0(%r13)
movl 0xc(%rsp), %eax
movw %ax, 0x24(%r13)
movq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x3443
cmpl $0xd, 0xce0(%r13)
jg 0x325c
movq %r12, %rdi
callq 0x348a
movq %rax, %rbx
movq 0xb8(%r12), %rdi
testq %rdi, %rdi
je 0x3229
movq %r13, %rsi
callq 0x3443
testq %rbx, %rbx
je 0x3239
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3443
cmpl $0xd, 0xce0(%r13)
jg 0x325c
leaq 0xa9e6(%rip), %rdi      # 0xdc30
movl %ebp, %esi
callq 0x329c
movq %rax, %rdi
movq %r13, %rsi
callq 0x3443
movq %r13, %rdi
movq %r14, %rsi
movq (%rsp), %rdx
callq 0x34bf
movq 0xa99e(%rip), %rax      # 0xdc10
movq %rax, 0xa9cf(%rip)      # 0xdc48
jmp 0x328a
callq 0x2060
movl $0x1c, (%rax)
pushq $-0x1
popq %r15
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq