// File: /workspace/github_c_makefile_star2_O1/jech[P]shncpd/ra.c
// Function: setup_ra_socket
int
setup_ra_socket()
{
    int s, i, rc, one = 1, ff = 255;
    struct icmp6_filter filter;

    if(ra_socket >= 0) {
        close(ra_socket);
        ra_socket = -1;
    }

    s = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
    if(s < 0)
        return -1;

    rc = setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ff, sizeof(ff));
    if(rc < 0)
        goto fail;

    rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ff, sizeof(ff));
    if(rc < 0)
        goto fail;

    rc = setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &one, sizeof(one));
    if(rc < 0)
        goto fail;

    for(i = 0; i < numinterfaces; i++) {
        struct ipv6_mreq mreq;
        const unsigned char all_routers[16] =
            {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02};
        if(interfaces[i].ifindex <= 0)
            continue;
        memset(&mreq, 0, sizeof(mreq));
        memcpy(&mreq.ipv6mr_multiaddr, &all_routers, 16);
        mreq.ipv6mr_interface = interfaces[i].ifindex;
        rc = setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP,
                        (char*)&mreq, sizeof(mreq));
        if(rc < 0)
            goto fail;
    }

    ICMP6_FILTER_SETBLOCKALL(&filter);
    ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);

    rc = setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter));
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_GETFD, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_GETFL, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
    if(rc < 0)
        goto fail;

    ra_socket = s;
    return s;

fail:
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0x1, 0xc(%rsp)
movl $0xff, 0x8(%rsp)
movl 0xbbf4(%rip), %edi      # 0x131ec
testl %edi, %edi
js 0x760b
callq 0x2190
movl $0xffffffff, 0xbbe1(%rip) # imm = 0xFFFFFFFF
movl $0xa, %edi
movl $0x3, %esi
movl $0x3a, %edx
callq 0x2390
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x77ab
movl %eax, %ebp
leaq 0x8(%rsp), %rcx
movl %eax, %edi
movl $0x29, %esi
movl $0x10, %edx
movl $0x4, %r8d
callq 0x20a0
testl %eax, %eax
js 0x77ab
leaq 0x8(%rsp), %rcx
movl %ebp, %edi
movl $0x29, %esi
movl $0x12, %edx
movl $0x4, %r8d
callq 0x20a0
testl %eax, %eax
js 0x77ab
leaq 0xc(%rsp), %rcx
movl %ebp, %edi
movl $0x29, %esi
movl $0x33, %edx
movl $0x4, %r8d
callq 0x20a0
testl %eax, %eax
js 0x77ab
leaq 0xbbff(%rip), %r15      # 0x132a0
cmpl $0x0, (%r15)
jle 0x771a
leaq 0xbc02(%rip), %r12      # 0x132b0
addq $0x8, %r12
xorl %r13d, %r13d
leaq 0x10(%rsp), %r14
movl (%r12), %eax
testl %eax, %eax
jle 0x76f6
movdqa 0x6636(%rip), %xmm0     # 0xdd00
movdqa %xmm0, 0x10(%rsp)
movl %eax, 0x20(%rsp)
movl %ebp, %edi
movl $0x29, %esi
movl $0x14, %edx
movq %r14, %rcx
movl $0x14, %r8d
callq 0x20a0
shrl $0x1e, %eax
andl $-0x2, %eax
jmp 0x76fb
movl $0x5, %eax
cmpl $0x5, %eax
je 0x7708
testl %eax, %eax
jne 0x77ab
incq %r13
movslq (%r15), %rax
addq $0x98, %r12
cmpq %rax, %r13
jl 0x76ba
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rcx
movdqa %xmm0, 0x10(%rcx)
movdqa %xmm0, (%rcx)
andb $-0x21, 0x10(%rcx)
movl %ebp, %edi
movl $0x3a, %esi
movl $0x1, %edx
movl $0x20, %r8d
callq 0x20a0
testl %eax, %eax
js 0x77ab
movl %ebp, %edi
movl $0x1, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20b0
testl %eax, %eax
js 0x77ab
orl $0x1, %eax
movl %ebp, %edi
movl $0x2, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x20b0
testl %eax, %eax
js 0x77ab
movl %ebp, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20b0
testl %eax, %eax
js 0x77ab
orl $0x800, %eax            # imm = 0x800
movl %ebp, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x20b0
testl %eax, %eax
js 0x77ab
movl %ebp, 0xba43(%rip)      # 0x131ec
movl %ebp, %ebx
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq