// File: /workspace/github_c_makefile_star2_O1/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 %rbx
pushq %rax
cmpl $0x0, 0x7e15(%rip)      # 0xc1c8
jns 0x456c
cmpl $0x0, 0x7e0c(%rip)      # 0xc1cc
jns 0x456c
cmpq $0x0, 0x9882(%rip)      # 0xdc50
jne 0x456c
cmpq $0x0, 0x987c(%rip)      # 0xdc58
jne 0x456c
movq %rcx, %r14
movq %rdx, %r15
movl %esi, %ebx
movl %edi, %ebp
xorl %eax, %eax
movq %rax, 0x9813(%rip)      # 0xdc08
movl %eax, 0x986d(%rip)      # 0xdc68
movq %rax, 0x985e(%rip)      # 0xdc60
movl %eax, 0x9864(%rip)      # 0xdc6c
testl %edi, %edi
js 0x4438
movl $0x1, %edi
movl $0xc0, %esi
callq 0x21a0
movq %rax, 0x982e(%rip)      # 0xdc50
testq %rax, %rax
je 0x4577
movl $0x80, 0x1c(%rax)
movl $0x2, (%rax)
testl %ebx, %ebx
js 0x4468
movl $0x1, %edi
movl $0xc0, %esi
callq 0x21a0
movq %rax, 0x9806(%rip)      # 0xdc58
testq %rax, %rax
je 0x4577
movl $0x80, 0x1c(%rax)
movl $0xa, (%rax)
movl 0x10(%r15), %eax
movl %eax, 0x97ce(%rip)      # 0xdc40
movups (%r15), %xmm0
movaps %xmm0, 0x97b3(%rip)     # 0xdc30
testq %r14, %r14
je 0x449c
movb $0x3a, 0x97eb(%rip)     # 0xdc74
movl $0x34763a31, 0x97dd(%rip) # imm = 0x34763A31
movl (%r14), %eax
movl %eax, 0x97d9(%rip)      # 0xdc75
testq %r14, %r14
setne 0x97d6(%rip)            # 0xdc7c
leaq 0x9763(%rip), %rdi      # 0xdc10
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x2130
movq 0x9752(%rip), %r15      # 0xdc10
movq %r15, 0x97bb(%rip)      # 0xdc80
movq %r15, 0x97bc(%rip)      # 0xdc88
callq 0x2230
movq %rax, %rcx
movabsq $0x5555555555555556, %rdx # imm = 0x5555555555555556
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
addq %rdx, %rax
leaq (%rax,%rax,2), %rax
subq %rax, %rcx
addq %r15, %rcx
movq %rcx, 0x9794(%rip)      # 0xdc90
callq 0x2230
movw %ax, 0x9718(%rip)       # 0xdc20
movq %r14, 0x9739(%rip)      # 0xdc48
movl $0x0, 0x977f(%rip)      # 0xdc98
movq 0x96f0(%rip), %rax      # 0xdc10
movq %rax, 0x9779(%rip)      # 0xdca0
movl $0x190, 0x9777(%rip)    # imm = 0x190
movq %r14, 0x9778(%rip)      # 0xdcb0
callq 0x45b1
testl %eax, %eax
js 0x4587
movl %ebp, 0x7c81(%rip)      # 0xc1c8
movl %ebx, 0x7c7f(%rip)      # 0xc1cc
movq 0x96fc(%rip), %rdi      # 0xdc50
callq 0x461e
movq 0x96f8(%rip), %rdi      # 0xdc58
callq 0x461e
movl $0x1, %eax
jmp 0x457c
callq 0x2060
movl $0x10, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x96c2(%rip), %rdi      # 0xdc50
callq 0x2040
xorl %ebx, %ebx
movq %rbx, 0x96b4(%rip)      # 0xdc50
movq 0x96b5(%rip), %rdi      # 0xdc58
callq 0x2040
movq %rbx, 0x96a9(%rip)      # 0xdc58
jmp 0x4577