// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movq %rcx, %r15
movl %edx, %ebp
movl %esi, %ebx
movq %rdi, %r13
movl %edx, %esi
callq 0x35d4
testq %rax, %rax
je 0x3316
movq %rax, %r14
movl %ebx, 0x14(%rsp)
testq %r15, %r15
movq %rax, (%rsp)
je 0x32bf
movq %r13, %rdi
callq 0x362b
testq %rax, %rax
je 0x32bf
movq %rax, %r12
movq %r15, %rbx
movl 0x14(%rax), %esi
leaq 0x604e(%rip), %rdi      # 0x9270
xorl %eax, %eax
callq 0x366b
cmpl $0x0, 0x14(%r12)
jle 0x32b8
movl $0x8, %r14d
xorl %r15d, %r15d
movq 0x20(%r12), %rcx
movzwl 0x12(%rcx,%r14), %eax
rolw $0x8, %ax
movzwl 0x10(%rcx,%r14), %edx
cmpl $0x10, %edx
je 0x327c
cmpl $0x4, %edx
jne 0x32a7
movl (%rcx,%r14), %ecx
movl %ecx, 0x20(%rsp)
movw %ax, 0x24(%rsp)
movl $0x6, %r8d
movq 0x8(%rsp), %rdi
movl $0x1, %esi
jmp 0x329d
movdqu (%rcx,%r14), %xmm0
movdqa %xmm0, 0x20(%rsp)
movw %ax, 0x30(%rsp)
movl $0x12, %r8d
movq 0x8(%rsp), %rdi
movl $0x2, %esi
movq %r13, %rdx
leaq 0x20(%rsp), %rcx
callq *%rbx
incq %r15
movslq 0x14(%r12), %rax
addq $0x20, %r14
cmpq %rax, %r15
jl 0x323e
movq %rbx, %r15
movq (%rsp), %r14
movq 0xa942(%rip), %r12      # 0xdc08
movl $0x1, %edx
testq %r12, %r12
sete %al
je 0x3331
cmpl %ebp, 0x4(%r12)
jne 0x330c
movdqu 0x10(%r12), %xmm0
movdqu (%r13), %xmm1
pcmpeqb %xmm0, %xmm1
movd 0x20(%r12), %xmm0
movd 0x10(%r13), %xmm2
pcmpeqb %xmm0, %xmm2
pand %xmm1, %xmm2
pmovmskb %xmm2, %ecx
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0x3326
movq 0xce8(%r12), %r12
jmp 0x32cb
callq 0x2060
movl $0x61, (%rax)
jmp 0x35c0
xorl %edx, %edx
cmpl $0x0, 0x28(%r12)
setne %dl
testb $0x1, %al
movl %edx, 0x10(%rsp)
je 0x33de
movq 0xa8c4(%rip), %rbx      # 0xdc08
xorl %r12d, %r12d
testq %rbx, %rbx
je 0x3374
movq %rbx, %rax
cmpl $0x0, 0x28(%rax)
je 0x3368
testq %r12, %r12
je 0x3365
movq 0x8(%r12), %rcx
cmpq 0x8(%rax), %rcx
jle 0x3368
movq %rax, %r12
movq 0xce8(%rax), %rax
testq %rax, %rax
jne 0x334f
testq %r12, %r12
je 0x3392
movq $-0xe88, %rax           # imm = 0xF178
addq 0xa889(%rip), %rax      # 0xdc10
cmpq %rax, 0x8(%r12)
jl 0x349c
movl 0xa8cf(%rip), %r14d     # 0xdc68
cmpl $0x3ff, %r14d           # imm = 0x3FF
jg 0x348c
movl $0x1, %edi
movl $0xcf0, %esi            # imm = 0xCF0
callq 0x21a0
testq %rax, %rax
je 0x348c
movq %rbx, 0xce8(%rax)
movq %rax, 0xa83c(%rip)      # 0xdc08
incl %r14d
movl %r14d, 0xa892(%rip)     # 0xdc68
movq %rax, %r12
jmp 0x349c
movl $0x0, 0x28(%r12)
movl 0xce0(%r12), %ecx
testl %ecx, %ecx
jle 0x34f3
movq %r15, 0x18(%rsp)
leaq 0x30(%r12), %rbx
movq $-0x1c20, %r14          # imm = 0xE3E0
movl $0x1, %r15d
xorl %r13d, %r13d
movq 0xa7f8(%rip), %rax      # 0xdc10
addq %r14, %rax
cmpl $0x2, %ecx
cmovll %r15d, %ecx
imulq $0xe8, %rcx, %rcx
xorl %edi, %edi
cmpl $0x2, 0xe0(%r12,%rdi)
jg 0x346a
cmpq %rax, 0xd8(%r12,%rdi)
jl 0x346a
movl %r13d, 0xe0(%r12,%rdi)
movq $0x0, 0x10c(%r12,%rdi)
movl %r13d, 0x114(%r12,%rdi)
addq $0xe8, %rdi
cmpq %rdi, %rcx
jne 0x342b
jmp 0x3481
addq %rbx, %rdi
movq %r12, %rsi
callq 0x3719
movl 0xce0(%r12), %ecx
testl %ecx, %ecx
jg 0x3411
movq 0x18(%rsp), %r15
movq (%rsp), %r14
jmp 0x34f3
testq %r12, %r12
je 0x3499
cmpl $0x0, 0x28(%r12)
jne 0x349c
xorl %r12d, %r12d
testq %r12, %r12
movq (%rsp), %r14
je 0x35b5
movl %ebp, 0x4(%r12)
movzwl 0xa76b(%rip), %eax      # 0xdc20
movl %eax, %ecx
incl %ecx
movw %cx, 0xa760(%rip)       # 0xdc20
movw %ax, (%r12)
movq $0x0, 0x8(%r12)
movdqu (%r13), %xmm0
movdqu %xmm0, 0x10(%r12)
movl 0x10(%r13), %eax
movl %eax, 0x20(%r12)
xorl %eax, %eax
movl %eax, 0x28(%r12)
movl %eax, 0xce0(%r12)
movl 0x14(%rsp), %eax
movw %ax, 0x24(%r12)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3794
cmpl $0xd, 0xce0(%r12)
jg 0x356d
cmpl $0x2, (%r14)
leaq 0xa732(%rip), %rax      # 0xdc50
leaq 0xa733(%rip), %rcx      # 0xdc58
cmoveq %rax, %rcx
movq (%rcx), %rax
cmpq %r14, %rax
je 0x3547
movq %rax, %rbx
movq 0xb8(%rax), %rax
testq %rax, %rax
je 0x3547
cmpq %r14, %rax
jne 0x3531
jmp 0x3549
xorl %ebx, %ebx
movq 0xb8(%r14), %rdi
testq %rdi, %rdi
je 0x355d
movq %r12, %rsi
callq 0x3794
testq %rbx, %rbx
je 0x356d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3794
cmpl $0xd, 0xce0(%r12)
jg 0x3591
leaq 0xa6b1(%rip), %rdi      # 0xdc30
movl %ebp, %esi
callq 0x35d4
movq %rax, %rdi
movq %r12, %rsi
callq 0x3794
movq %r12, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
callq 0x37db
movq 0xa668(%rip), %rax      # 0xdc10
movq %rax, 0xa699(%rip)      # 0xdc48
movl 0x10(%rsp), %eax
jmp 0x35c5
callq 0x2060
movl $0x1c, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq