// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
cmpl $0x0, 0xa46b(%rip)      # 0xf1c8
jge 0x4d7c
cmpl $0x0, 0xa466(%rip)      # 0xf1cc
jge 0x4d7c
cmpq $0x0, 0xbee0(%rip)      # 0x10c50
jne 0x4d7c
cmpq $0x0, 0xbede(%rip)      # 0x10c58
je 0x4d93
callq 0x2060
movl $0x10, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4fd8
movq $0x0, 0xbe6a(%rip) # 0x10c08
movl $0x0, 0xbec0(%rip)      # 0x10c68
movq $0x0, 0xbead(%rip) # 0x10c60
movl $0x0, 0xbeaf(%rip)      # 0x10c6c
cmpl $0x0, -0x8(%rbp)
jl 0x4e0a
movl $0x1, %edi
movl $0xc0, %esi
callq 0x21a0
movq %rax, 0xbe77(%rip)      # 0x10c50
cmpq $0x0, 0xbe6f(%rip)      # 0x10c50
jne 0x4def
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4fd8
movq 0xbe5a(%rip), %rax      # 0x10c50
movl $0x80, 0x1c(%rax)
movq 0xbe4c(%rip), %rax      # 0x10c50
movl $0x2, (%rax)
cmpl $0x0, -0xc(%rbp)
jl 0x4e57
movl $0x1, %edi
movl $0xc0, %esi
callq 0x21a0
movq %rax, 0xbe32(%rip)      # 0x10c58
cmpq $0x0, 0xbe2a(%rip)      # 0x10c58
jne 0x4e3c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4fd8
movq 0xbe15(%rip), %rax      # 0x10c58
movl $0x80, 0x1c(%rax)
movq 0xbe07(%rip), %rax      # 0x10c58
movl $0xa, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq %rcx, 0xbdcb(%rip)      # 0x10c30
movq 0x8(%rax), %rcx
movq %rcx, 0xbdc8(%rip)      # 0x10c38
movl 0x10(%rax), %eax
movl %eax, 0xbdc7(%rip)      # 0x10c40
cmpq $0x0, -0x20(%rbp)
je 0x4eb0
movl 0x7499(%rip), %eax      # 0xc31f
movl %eax, 0xbde4(%rip)      # 0x10c70
movb 0x7491(%rip), %al       # 0xc323
movb %al, 0xbddc(%rip)       # 0x10c74
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, 0xbdd1(%rip)      # 0x10c75
movl $0x1, 0xbdce(%rip)      # 0x10c7c
jmp 0x4eba
movl $0x0, 0xbdc2(%rip)      # 0x10c7c
leaq 0xbd4f(%rip), %rdi      # 0x10c10
xorl %eax, %eax
movl %eax, %esi
callq 0x2140
movq 0xbd3f(%rip), %rax      # 0x10c10
movq %rax, 0xbda8(%rip)      # 0x10c80
movq 0xbd31(%rip), %rax      # 0x10c10
movq %rax, 0xbda2(%rip)      # 0x10c88
movq 0xbd23(%rip), %rax      # 0x10c10
movq %rax, -0x30(%rbp)
callq 0x2230
movl $0x3, %ecx
cqto
idivq %rcx
movq -0x30(%rbp), %rax
addq %rdx, %rax
movq %rax, 0xbd82(%rip)      # 0x10c90
callq 0x2230
andq $0xffff, %rax           # imm = 0xFFFF
movw %ax, 0xbd00(%rip)       # 0x10c20
movq $0x0, 0xbd1d(%rip) # 0x10c48
movl $0x0, 0xbd63(%rip)      # 0x10c98
movq 0xbcd4(%rip), %rax      # 0x10c10
movq %rax, 0xbd5d(%rip)      # 0x10ca0
movl $0x190, 0xbd5b(%rip)    # imm = 0x190
leaq 0xbd58(%rip), %rdi      # 0x10cac
xorl %esi, %esi
movl $0x8, %edx
callq 0x2150
callq 0x4ff0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x4f70
jmp 0x4fa3
movl -0x8(%rbp), %eax
movl %eax, 0xa24f(%rip)      # 0xf1c8
movl -0xc(%rbp), %eax
movl %eax, 0xa24a(%rip)      # 0xf1cc
movq 0xbcc7(%rip), %rdi      # 0x10c50
callq 0x5070
movq 0xbcc3(%rip), %rdi      # 0x10c58
callq 0x5070
movl $0x1, -0x4(%rbp)
jmp 0x4fd8
movq 0xbca6(%rip), %rdi      # 0x10c50
callq 0x2040
movq $0x0, 0xbc96(%rip) # 0x10c50
movq 0xbc97(%rip), %rdi      # 0x10c58
callq 0x2040
movq $0x0, 0xbc87(%rip) # 0x10c58
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)