// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x39a0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x35ab
callq 0x2060
movl $0x61, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x398f
cmpq $0x0, -0x20(%rbp)
je 0x36f0
movq -0x10(%rbp), %rdi
callq 0x3a50
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x36ee
movq -0x38(%rbp), %rax
movl 0x14(%rax), %esi
leaq 0x8c84(%rip), %rdi      # 0xc260
movb $0x0, %al
callq 0x3aa0
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x36ec
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzwl 0x1a(%rax), %edi
callq 0x2100
movw %ax, -0x42(%rbp)
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzwl 0x18(%rax), %eax
cmpl $0x4, %eax
jne 0x3676
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x60(%rbp)
movw -0x42(%rbp), %ax
movw %ax, -0x5c(%rbp)
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq -0x60(%rbp), %rcx
movl $0x1, %esi
movl $0x6, %r8d
callq *%rax
jmp 0x36dc
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzwl 0x18(%rax), %eax
cmpl $0x10, %eax
jne 0x36da
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq %rcx, -0x60(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movw -0x42(%rbp), %ax
movw %ax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq -0x60(%rbp), %rcx
movl $0x2, %esi
movl $0x12, %r8d
callq *%rax
jmp 0x36dc
jmp 0x36de
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x35ea
jmp 0x36ee
jmp 0x36f0
movq 0xd511(%rip), %rax      # 0x10c08
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x3737
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
cmpl -0x18(%rbp), %eax
jne 0x3726
movq -0x30(%rbp), %rdi
addq $0x10, %rdi
movq -0x10(%rbp), %rsi
callq 0x3b70
cmpl $0x0, %eax
jne 0x3726
jmp 0x3737
movq -0x30(%rbp), %rax
movq 0xce8(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x36fb
xorl %eax, %eax
cmpq $0x0, -0x30(%rbp)
movb %al, -0x81(%rbp)
je 0x3759
movq -0x30(%rbp), %rax
cmpl $0x0, 0x28(%rax)
setne %al
xorb $-0x1, %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x68(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x3836
movq -0x30(%rbp), %rax
movl $0x0, 0x28(%rax)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0xce0(%rcx), %eax
jge 0x3831
movq -0x30(%rbp), %rax
addq $0x30, %rax
movslq -0x6c(%rbp), %rcx
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpl $0x3, 0xb0(%rax)
jge 0x37dc
movq -0x78(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0xd440(%rip), %rcx      # 0x10c10
subq $0x1c20, %rcx           # imm = 0x1C20
cmpq %rcx, %rax
jge 0x37eb
movq -0x78(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x3ba0
jmp 0x377d
movq -0x78(%rbp), %rax
movl $0x0, 0xb0(%rax)
movq -0x78(%rbp), %rax
movl $0x0, 0xdc(%rax)
movq -0x78(%rbp), %rax
movl $0x0, 0xe0(%rax)
movq -0x78(%rbp), %rax
movl $0x0, 0xe4(%rax)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x3784
jmp 0x38c5
callq 0x3c50
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x385d
callq 0x2060
movl $0x1c, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x398f
movl -0x18(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
movw 0xd3b2(%rip), %cx       # 0x10c20
movw %cx, %ax
addw $0x1, %ax
movw %ax, 0xd3a4(%rip)       # 0x10c20
movq -0x30(%rbp), %rax
movw %cx, (%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x18(%rax)
movl 0x10(%rcx), %ecx
movl %ecx, 0x20(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0xce0(%rax)
movl -0x14(%rbp), %eax
movw %ax, %cx
movq -0x30(%rbp), %rax
movw %cx, 0x24(%rax)
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x3d70
movq -0x30(%rbp), %rax
cmpl $0xe, 0xce0(%rax)
jge 0x3932
movq -0x40(%rbp), %rdi
callq 0x3de0
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rax
cmpq $0x0, 0xb8(%rax)
je 0x391c
movq -0x40(%rbp), %rax
movq 0xb8(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x3d70
cmpq $0x0, -0x80(%rbp)
je 0x3930
movq -0x80(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x3d70
jmp 0x3932
movq -0x30(%rbp), %rax
cmpl $0xe, 0xce0(%rax)
jge 0x395a
movl -0x18(%rbp), %esi
leaq 0xd2e7(%rip), %rdi      # 0x10c30
callq 0x39a0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x3d70
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x3e80
movq 0xd29e(%rip), %rax      # 0x10c10
movq %rax, 0xd2cf(%rip)      # 0x10c48
cmpl $0x0, -0x68(%rbp)
je 0x3988
movl $0x0, -0x4(%rbp)
jmp 0x398f
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)