// File: /workspace/github_c_makefile_star2_O3/jech[P]dht/dht.c
// Function: storage_store
static int
storage_store(const unsigned char *id,
              const struct sockaddr *sa, unsigned short port)
{
    int i, len;
    struct storage *st;
    unsigned char *ip;

    if(sa->sa_family == AF_INET) {
        struct sockaddr_in *sin = (struct sockaddr_in*)sa;
        ip = (unsigned char*)&sin->sin_addr;
        len = 4;
    } else if(sa->sa_family == AF_INET6) {
        struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa;
        ip = (unsigned char*)&sin6->sin6_addr;
        len = 16;
    } else {
        return -1;
    }

    st = find_storage(id);

    if(st == NULL) {
        if(numstorage >= DHT_MAX_HASHES)
            return -1;
        st = calloc(1, sizeof(struct storage));
        if(st == NULL) return -1;
        memcpy(st->id, id, 20);
        st->next = storage;
        storage = st;
        numstorage++;
    }

    for(i = 0; i < st->numpeers; i++) {
        if(st->peers[i].port == port && st->peers[i].len == len &&
           memcmp(st->peers[i].ip, ip, len) == 0)
            break;
    }

    if(i < st->numpeers) {
        /* Already there, only need to refresh */
        st->peers[i].time = now.tv_sec;
        return 0;
    } else {
        struct peer *p;
        if(i >= st->maxpeers) {
            /* Need to expand the array. */
            struct peer *new_peers;
            int n;
            if(st->maxpeers >= DHT_MAX_PEERS)
                return 0;
            n = st->maxpeers == 0 ? 2 : 2 * st->maxpeers;
            n = MIN(n, DHT_MAX_PEERS);
            new_peers = realloc(st->peers, n * sizeof(struct peer));
            if(new_peers == NULL)
                return -1;
            st->peers = new_peers;
            st->maxpeers = n;
        }
        p = &st->peers[st->numpeers++];
        p->time = now.tv_sec;
        p->len = len;
        memcpy(p->ip, ip, len);
        p->port = port;
        return 1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebx
movq %rdi, %r12
movq %rsi, 0x8(%rsp)
movzwl (%rsi), %eax
cmpl $0x2, %eax
je 0x73f2
cmpl $0xa, %eax
jne 0x7576
movl $0x10, %ebp
movl $0x8, %r13d
jmp 0x73fd
movl $0x4, %ebp
movl $0x4, %r13d
movq %r12, %rdi
callq 0x35ea
movq %rax, %r15
testq %rax, %rax
je 0x7485
addq %r13, 0x8(%rsp)
movslq 0x14(%r15), %r14
movl %ebp, %eax
movq %rax, 0x10(%rsp)
testq %r14, %r14
jle 0x74e7
movq %r15, 0x20(%rsp)
movq 0x20(%r15), %r12
leaq 0x8(%r12), %r13
movq %r14, 0x18(%rsp)
shlq $0x5, %r14
xorl %r15d, %r15d
cmpw %bx, 0x1a(%r12,%r15)
jne 0x746d
movzwl 0x18(%r12,%r15), %eax
cmpl %eax, %ebp
jne 0x746d
leaq (%r15,%r13), %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2260
testl %eax, %eax
je 0x7585
addq $0x20, %r15
cmpq %r15, %r14
jne 0x7440
movq 0x18(%rsp), %r14
movl %r14d, %ecx
movq 0x20(%rsp), %r15
jmp 0x74e9
movl 0x67e0(%rip), %r14d     # 0xdc6c
cmpl $0x3fff, %r14d          # imm = 0x3FFF
jg 0x7576
movl $0x1, %edi
movl $0x30, %esi
callq 0x21a0
testq %rax, %rax
je 0x7576
movq %rax, %r15
movl 0x10(%r12), %eax
movl %eax, 0x10(%r15)
movups (%r12), %xmm0
movups %xmm0, (%r15)
movq 0x6793(%rip), %rax      # 0xdc60
movq %rax, 0x28(%r15)
movq %r15, 0x6788(%rip)      # 0xdc60
incl %r14d
movl %r14d, 0x678a(%rip)     # 0xdc6c
jmp 0x740d
xorl %ecx, %ecx
movl 0x18(%r15), %eax
cmpl %eax, %ecx
jge 0x74f7
movq 0x20(%r15), %rax
jmp 0x7538
cmpl $0x7ff, %eax            # imm = 0x7FF
jg 0x7576
leal (%rax,%rax), %ecx
movl $0x800, %edx            # imm = 0x800
cmpl %edx, %ecx
cmovll %ecx, %edx
testl %eax, %eax
movl $0x2, %r14d
cmovnel %edx, %r14d
movq 0x20(%r15), %rdi
movslq %r14d, %rsi
shlq $0x5, %rsi
callq 0x2270
testq %rax, %rax
je 0x7576
movq %rax, 0x20(%r15)
movl %r14d, 0x18(%r15)
movl 0x14(%r15), %r14d
leal 0x1(%r14), %ecx
movl %ecx, 0x14(%r15)
movslq %r14d, %rcx
shlq $0x5, %rcx
movq 0x66c2(%rip), %rdx      # 0xdc10
leaq (%rax,%rcx), %r15
addq $0x8, %r15
movq %rdx, -0x8(%r15)
movw %bp, 0x10(%r15)
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x2200
movw %bx, 0x12(%r15)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6684(%rip), %rax      # 0xdc10
movq %rax, (%r12,%r15)
jmp 0x7576