// File: /workspace/github_c_makefile_star2_O2/jech[P]dht/dht.c
// Function: dht_init
int
dht_init(int s, int s6, const unsigned char *id, const unsigned char *v)
{
    int rc;

    if(dht_socket >= 0 || dht_socket6 >= 0 || buckets || buckets6) {
        errno = EBUSY;
        return -1;
    }

    searches = NULL;
    numsearches = 0;

    storage = NULL;
    numstorage = 0;

    if(s >= 0) {
        buckets = calloc(1, sizeof(struct bucket));
        if(buckets == NULL)
            return -1;
        buckets->max_count = 128;
        buckets->af = AF_INET;
    }

    if(s6 >= 0) {
        buckets6 = calloc(1, sizeof(struct bucket));
        if(buckets6 == NULL)
            return -1;
        buckets6->max_count = 128;
        buckets6->af = AF_INET6;
    }

    memcpy(myid, id, 20);
    if(v) {
        memcpy(my_v, "1:v4:", 5);
        memcpy(my_v + 5, v, 4);
        have_v = 1;
    } else {
        have_v = 0;
    }

    dht_gettimeofday(&now, NULL);

    mybucket_grow_time = now.tv_sec;
    mybucket6_grow_time = now.tv_sec;
    confirm_nodes_time = now.tv_sec + random() % 3;

    search_id = random() & 0xFFFF;
    search_time = 0;

    next_blacklisted = 0;

    token_bucket_time = now.tv_sec;
    token_bucket_tokens = MAX_TOKEN_BUCKET_TOKENS;

    memset(secret, 0, sizeof(secret));
    rc = rotate_secrets();
    if(rc < 0)
        goto fail;

    dht_socket = s;
    dht_socket6 = s6;

    expire_buckets(buckets);
    expire_buckets(buckets6);

    return 1;

 fail:
    free(buckets);
    buckets = NULL;
    free(buckets6);
    buckets6 = NULL;
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpl $0x0, 0x8204(%rip)      # 0xc1c8
jns 0x4162
cmpl $0x0, 0x81fb(%rip)      # 0xc1cc
jns 0x4162
cmpq $0x0, 0x9c71(%rip)      # 0xdc50
jne 0x4162
cmpq $0x0, 0x9c6b(%rip)      # 0xdc58
jne 0x4162
movq %rcx, %r15
movq %rdx, %r12
movl %esi, %ebp
movl %edi, %r14d
andq $0x0, 0x9c02(%rip)      # 0xdc08
andl $0x0, 0x9c5b(%rip)      # 0xdc68
andq $0x0, 0x9c4b(%rip)      # 0xdc60
andl $0x0, 0x9c50(%rip)      # 0xdc6c
pushq $-0x1
popq %rbx
testl %edi, %edi
js 0x404d
pushq $0x1
popq %rdi
movl $0xc0, %esi
callq 0x21a0
movq %rax, 0x9c19(%rip)      # 0xdc50
testq %rax, %rax
je 0x4170
movl $0x80, 0x1c(%rax)
movl $0x2, (%rax)
testl %ebp, %ebp
js 0x407b
pushq $0x1
popq %rdi
movl $0xc0, %esi
callq 0x21a0
movq %rax, 0x9bf3(%rip)      # 0xdc58
testq %rax, %rax
je 0x4170
movl $0x80, 0x1c(%rax)
movl $0xa, (%rax)
movl 0x10(%r12), %eax
movl %eax, 0x9bba(%rip)      # 0xdc40
movups (%r12), %xmm0
movaps %xmm0, 0x9b9e(%rip)     # 0xdc30
testq %r15, %r15
je 0x40b1
movb $0x3a, 0x9bd6(%rip)     # 0xdc74
movl $0x34763a31, 0x9bc8(%rip) # imm = 0x34763A31
movl (%r15), %eax
movl %eax, 0x9bc4(%rip)      # 0xdc75
testq %r15, %r15
setne 0x9bc1(%rip)            # 0xdc7c
leaq 0x9b4e(%rip), %rdi      # 0xdc10
xorl %esi, %esi
callq 0x2130
movq 0x9b40(%rip), %r15      # 0xdc10
movq %r15, 0x9ba9(%rip)      # 0xdc80
movq %r15, 0x9baa(%rip)      # 0xdc88
callq 0x2230
pushq $0x3
popq %rcx
cqto
idivq %rcx
addq %r15, %rdx
movq %rdx, 0x9b9b(%rip)      # 0xdc90
callq 0x2230
andq $0x0, 0x9b46(%rip)      # 0xdc48
andl $0x0, 0x9b8f(%rip)      # 0xdc98
movw %ax, 0x9b10(%rip)       # 0xdc20
movq 0x9af9(%rip), %rax      # 0xdc10
movq %rax, 0x9b82(%rip)      # 0xdca0
movl $0x190, 0x9b80(%rip)    # imm = 0x190
andq $0x0, 0x9b80(%rip)      # 0xdcb0
callq 0x41a5
testl %eax, %eax
js 0x417b
movl %r14d, 0x8088(%rip)     # 0xc1c8
movl %ebp, 0x8086(%rip)      # 0xc1cc
movq 0x9b03(%rip), %rdi      # 0xdc50
callq 0x41f2
movq 0x9aff(%rip), %rdi      # 0xdc58
callq 0x41f2
pushq $0x1
jmp 0x416f
callq 0x2060
movl $0x10, (%rax)
pushq $-0x1
popq %rbx
movl %ebx, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x9ace(%rip), %rdi      # 0xdc50
callq 0x2040
andq $0x0, 0x9ac1(%rip)      # 0xdc50
movq 0x9ac2(%rip), %rdi      # 0xdc58
callq 0x2040
andq $0x0, 0x9ab5(%rip)      # 0xdc58
jmp 0x4170