// File: /workspace/github_c_makefile_star2_O0/jech[P]dht/dht.c
// Function: insert_closest_node
static int
insert_closest_node(unsigned char *nodes, int numnodes,
                    const unsigned char *id, struct node *n)
{
    int i, size;

    if(n->ss.ss_family == AF_INET)
        size = 26;
    else if(n->ss.ss_family == AF_INET6)
        size = 38;
    else
        abort();

    for(i = 0; i< numnodes; i++) {
        if(id_cmp(n->id, nodes + size * i) == 0)
            return numnodes;
        if(xorcmp(n->id, nodes + size * i, id) < 0)
            break;
    }

    if(i == 8)
        return numnodes;

    if(numnodes < 8)
        numnodes++;

    if(i < numnodes - 1)
        memmove(nodes + size * (i + 1), nodes + size * i,
                size * (numnodes - i - 1));

    if(n->ss.ss_family == AF_INET) {
        struct sockaddr_in *sin = (struct sockaddr_in*)&n->ss;
        memcpy(nodes + size * i, n->id, 20);
        memcpy(nodes + size * i + 20, &sin->sin_addr, 4);
        memcpy(nodes + size * i + 24, &sin->sin_port, 2);
    } else if(n->ss.ss_family == AF_INET6) {
        struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&n->ss;
        memcpy(nodes + size * i, n->id, 20);
        memcpy(nodes + size * i + 20, &sin6->sin6_addr, 16);
        memcpy(nodes + size * i + 36, &sin6->sin6_port, 2);
    } else {
        abort();
    }

    return numnodes;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzwl 0x18(%rax), %eax
cmpl $0x2, %eax
jne 0xbc9d
movl $0x1a, -0x30(%rbp)
jmp 0xbcba
movq -0x28(%rbp), %rax
movzwl 0x18(%rax), %eax
cmpl $0xa, %eax
jne 0xbcb3
movl $0x26, -0x30(%rbp)
jmp 0xbcb8
callq 0x2050
jmp 0xbcba
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xbd23
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x30(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
addq %rax, %rsi
callq 0x3b70
cmpl $0x0, %eax
jne 0xbcf2
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xbe97
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x30(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
addq %rax, %rsi
movq -0x20(%rbp), %rdx
callq 0xa930
cmpl $0x0, %eax
jge 0xbd16
jmp 0xbd23
jmp 0xbd18
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xbcc1
cmpl $0x8, -0x2c(%rbp)
jne 0xbd34
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xbe97
cmpl $0x8, -0x14(%rbp)
jge 0xbd43
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl -0x2c(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xbd8c
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %eax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
imull %ecx, %eax
cltq
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movl -0x30(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
addq %rax, %rsi
movl -0x30(%rbp), %eax
movl -0x14(%rbp), %ecx
subl -0x2c(%rbp), %ecx
subl $0x1, %ecx
imull %ecx, %eax
movslq %eax, %rdx
callq 0x2290
movq -0x28(%rbp), %rax
movzwl 0x18(%rax), %eax
cmpl $0x2, %eax
jne 0xbe07
movq -0x28(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x2c(%rbp), %ecx
movslq %ecx, %rcx
movq -0x28(%rbp), %rdx
movq (%rdx), %rsi
movq %rsi, (%rax,%rcx)
movq 0x8(%rdx), %rsi
movq %rsi, 0x8(%rax,%rcx)
movl 0x10(%rdx), %edx
movl %edx, 0x10(%rax,%rcx)
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x2c(%rbp), %ecx
movslq %ecx, %rcx
movq -0x38(%rbp), %rdx
movl 0x4(%rdx), %edx
movl %edx, 0x14(%rax,%rcx)
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x2c(%rbp), %ecx
movslq %ecx, %rcx
movq -0x38(%rbp), %rdx
movw 0x2(%rdx), %dx
movw %dx, 0x18(%rax,%rcx)
jmp 0xbe91
movq -0x28(%rbp), %rax
movzwl 0x18(%rax), %eax
cmpl $0xa, %eax
jne 0xbe8a
movq -0x28(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x2c(%rbp), %ecx
movslq %ecx, %rcx
movq -0x28(%rbp), %rdx
movq (%rdx), %rsi
movq %rsi, (%rax,%rcx)
movq 0x8(%rdx), %rsi
movq %rsi, 0x8(%rax,%rcx)
movl 0x10(%rdx), %edx
movl %edx, 0x10(%rax,%rcx)
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x2c(%rbp), %ecx
movslq %ecx, %rcx
movq -0x40(%rbp), %rdx
movq 0x8(%rdx), %rsi
movq %rsi, 0x14(%rax,%rcx)
movq 0x10(%rdx), %rdx
movq %rdx, 0x1c(%rax,%rcx)
movq -0x10(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x2c(%rbp), %ecx
movslq %ecx, %rcx
movq -0x40(%rbp), %rdx
movw 0x2(%rdx), %dx
movw %dx, 0x24(%rax,%rcx)
jmp 0xbe8f
callq 0x2050
jmp 0xbe91
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq